blob: 9d36777b25e7564d7a3fcb49f63f12466d209880 [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
2263/* Insert in <root> ebtree <node> node with <ar> as range value.
2264 * Returns the ebtree node which has been inserted.
2265 */
2266static inline
2267struct eb64_node *quic_insert_new_range(struct eb_root *root,
2268 struct quic_arng_node *node,
2269 struct quic_arng *ar)
2270{
2271 node->first.key = ar->first;
2272 node->last = ar->last;
2273 return eb64_insert(root, &node->first);
2274}
2275
2276/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002277 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002278 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002279 *
2280 * Descending order
2281 * ------------->
2282 * range1 range2
2283 * ..........|--------|..............|--------|
2284 * ^ ^ ^ ^
2285 * | | | |
2286 * last1 first1 last2 first2
2287 * ..........+--------+--------------+--------+......
2288 * diff1 gap12 diff2
2289 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002290 * To encode the previous list of ranges we must encode integers as follows in
2291 * descending order:
2292 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002293 * with diff1 = last1 - first1
2294 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002295 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002296 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002297 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002298int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2299 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002300{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002301 struct eb64_node *le;
2302 struct quic_arng_node *new_node;
2303 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002304
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002305 new = NULL;
2306 if (eb_is_empty(&arngs->root)) {
2307 /* First range insertion. */
2308 new_node = pool_alloc(pool_head_quic_arng);
2309 if (!new_node)
2310 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002311
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002312 quic_insert_new_range(&arngs->root, new_node, ar);
2313 /* Increment the size of these ranges. */
2314 arngs->sz++;
2315 goto out;
2316 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002317
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002318 le = eb64_lookup_le(&arngs->root, ar->first);
2319 if (!le) {
2320 /* New insertion */
2321 new_node = pool_alloc(pool_head_quic_arng);
2322 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323 return 0;
2324
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002325 new = quic_insert_new_range(&arngs->root, new_node, ar);
2326 /* Increment the size of these ranges. */
2327 arngs->sz++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002328 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002329 else {
2330 struct quic_arng_node *le_ar =
2331 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002332
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002333 /* Already existing range */
2334 if (le_ar->first.key <= ar->first && le_ar->last >= ar->last)
2335 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002336
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002337 if (le_ar->last + 1 >= ar->first) {
2338 le_ar->last = ar->last;
2339 new = le;
2340 new_node = le_ar;
2341 }
2342 else {
2343 /* New insertion */
2344 new_node = pool_alloc(pool_head_quic_arng);
2345 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002346 return 0;
2347
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002348 new = quic_insert_new_range(&arngs->root, new_node, ar);
2349 /* Increment the size of these ranges. */
2350 arngs->sz++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002351 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002352 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002353
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002354 /* Verify that the new inserted node does not overlap the nodes
2355 * which follow it.
2356 */
2357 if (new) {
2358 uint64_t new_node_last;
2359 struct eb64_node *next;
2360 struct quic_arng_node *next_node;
2361
2362 new_node_last = new_node->last;
2363 while ((next = eb64_next(new))) {
2364 next_node =
2365 eb64_entry(&next->node, struct quic_arng_node, first);
2366 if (new_node_last + 1 < next_node->first.key)
2367 break;
2368
2369 if (next_node->last > new_node->last)
2370 new_node->last = next_node->last;
2371 eb64_delete(next);
2372 free(next_node);
2373 /* Decrement the size of these ranges. */
2374 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002375 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002376 }
2377
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002378 quic_arngs_set_enc_sz(arngs);
2379
2380 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002381 return 1;
2382}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002383/* Remove the header protection of packets at <el> encryption level.
2384 * Always succeeds.
2385 */
2386static inline void qc_rm_hp_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx)
2387{
2388 struct quic_tls_ctx *tls_ctx;
2389 struct quic_rx_packet *pqpkt, *qqpkt;
2390 struct quic_enc_level *app_qel;
2391
2392 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2393 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2394 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
2395 if (el == app_qel && objt_listener(ctx->conn->target) && ctx->state < QUIC_HS_ST_COMPLETE) {
2396 TRACE_PROTO("hp not removed (handshake not completed)",
2397 QUIC_EV_CONN_ELRMHP, ctx->conn);
2398 goto out;
2399 }
2400 tls_ctx = &el->tls_ctx;
2401 list_for_each_entry_safe(pqpkt, qqpkt, &el->rx.pqpkts, list) {
2402 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2403 pqpkt->data + pqpkt->pn_offset,
2404 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2405 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2406 /* XXX TO DO XXX */
2407 }
2408 else {
2409 /* The AAD includes the packet number field */
2410 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2411 /* Store the packet into the tree of packets to decrypt. */
2412 pqpkt->pn_node.key = pqpkt->pn;
2413 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2414 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2415 }
2416 quic_rx_packet_list_del(pqpkt);
2417 }
2418
2419 out:
2420 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2421}
2422
2423/* Process all the CRYPTO frame at <el> encryption level.
2424 * Return 1 if succeeded, 0 if not.
2425 */
2426static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
2427 struct quic_conn_ctx *ctx)
2428{
2429 struct eb64_node *node;
2430
2431 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
2432 node = eb64_first(&el->rx.crypto.frms);
2433 while (node) {
2434 struct quic_rx_crypto_frm *cf;
2435
2436 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2437 if (cf->offset_node.key != el->rx.crypto.offset)
2438 break;
2439
2440 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2441 goto err;
2442
2443 node = eb64_next(node);
2444 quic_rx_packet_refdec(cf->pkt);
2445 eb64_delete(&cf->offset_node);
2446 pool_free(pool_head_quic_rx_crypto_frm, cf);
2447 }
2448
2449 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2450 return 1;
2451
2452 err:
2453 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2454 return 0;
2455}
2456
2457/* Process all the packets at <el> encryption level.
2458 * Return 1 if succeeded, 0 if not.
2459 */
2460int qc_treat_rx_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx)
2461{
2462 struct quic_tls_ctx *tls_ctx;
2463 struct eb64_node *node;
2464
2465 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2466 tls_ctx = &el->tls_ctx;
2467 node = eb64_first(&el->rx.pkts);
2468 while (node) {
2469 struct quic_rx_packet *pkt;
2470
2471 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2472 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2473 /* Drop the packet */
2474 TRACE_PROTO("packet decryption failed -> dropped",
2475 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2476 }
2477 else {
2478 int drop;
2479
2480 drop = 0;
2481 if (!qc_parse_pkt_frms(pkt, ctx, el))
2482 drop = 1;
2483
2484 if (drop) {
2485 /* Drop the packet */
2486 TRACE_PROTO("packet parsing failed -> dropped",
2487 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2488 }
2489 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002490 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2491
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
2493 el->pktns->rx.nb_ack_eliciting++;
2494 if (!(el->pktns->rx.nb_ack_eliciting & 1))
2495 el->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
2496 }
2497
2498 /* Update the largest packet number. */
2499 if (pkt->pn > el->pktns->rx.largest_pn)
2500 el->pktns->rx.largest_pn = pkt->pn;
2501
2502 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002503 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002504 TRACE_DEVEL("Could not update ack range list",
2505 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2506 node = eb64_next(node);
2507 quic_rx_packet_eb64_delete(&pkt->pn_node);
2508 free_quic_rx_packet(pkt);
2509 goto err;
2510 }
2511
2512 }
2513 }
2514 node = eb64_next(node);
2515 quic_rx_packet_eb64_delete(&pkt->pn_node);
2516 free_quic_rx_packet(pkt);
2517 }
2518
2519 if (!qc_treat_rx_crypto_frms(el, ctx))
2520 goto err;
2521
2522 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2523 return 1;
2524
2525 err:
2526 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2527 return 0;
2528}
2529
2530/* Called during handshakes to parse and build Initial and Handshake packets for QUIC
2531 * connections with <ctx> as I/O handler context.
2532 * Returns 1 if succeeded, 0 if not.
2533 */
2534int qc_do_hdshk(struct quic_conn_ctx *ctx)
2535{
2536 int ssl_err;
2537 struct quic_conn *quic_conn;
2538 enum quic_tls_enc_level tel, next_tel;
2539 struct quic_enc_level *qel, *next_qel;
2540 struct quic_tls_ctx *tls_ctx;
2541
2542 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
2543
2544 ssl_err = SSL_ERROR_NONE;
2545 quic_conn = ctx->conn->qc;
2546 if (!quic_get_tls_enc_levels(&tel, &next_tel, ctx->state))
2547 goto err;
2548
2549 qel = &quic_conn->els[tel];
2550 next_qel = &quic_conn->els[next_tel];
2551
2552 next_level:
2553 tls_ctx = &qel->tls_ctx;
2554
2555 /* If the header protection key for this level has been derived,
2556 * remove the packet header protections.
2557 */
2558 if (!LIST_ISEMPTY(&qel->rx.pqpkts) &&
2559 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2560 qc_rm_hp_pkts(qel, ctx);
2561
2562 if (!eb_is_empty(&qel->rx.pkts) &&
2563 !qc_treat_rx_pkts(qel, ctx))
2564 goto err;
2565
2566 if (!qc_prep_hdshk_pkts(ctx))
2567 goto err;
2568
2569 if (!qc_send_ppkts(ctx))
2570 goto err;
2571
2572 /* Check if there is something to do for the next level.
2573 */
2574 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
2575 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
2576 qel = next_qel;
2577 goto next_level;
2578 }
2579
2580 /* If the handshake has not been completed -> out! */
2581 if (ctx->state < QUIC_HS_ST_COMPLETE)
2582 goto out;
2583
2584 /* Discard the Handshake keys. */
2585 quic_tls_discard_keys(&quic_conn->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2586 quic_pktns_discard(quic_conn->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, quic_conn);
2587 qc_set_timer(ctx);
2588 if (!quic_build_post_handshake_frames(quic_conn) ||
2589 !qc_prep_phdshk_pkts(quic_conn) ||
2590 !qc_send_ppkts(ctx))
2591 goto err;
2592
2593 out:
2594 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state);
2595 return 1;
2596
2597 err:
2598 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
2599 return 0;
2600}
2601
2602/* Allocate a new QUIC connection and return it if succeeded, NULL if not. */
2603struct quic_conn *new_quic_conn(uint32_t version)
2604{
2605 struct quic_conn *quic_conn;
2606
Willy Tarreaue4498932021-03-22 21:13:05 +01002607 quic_conn = pool_zalloc(pool_head_quic_conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608 if (quic_conn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002609 quic_conn->version = version;
2610 }
2611
2612 return quic_conn;
2613}
2614
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002615/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002616static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2617{
2618 int i;
2619
2620 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2621 if (qel->tx.crypto.bufs[i]) {
2622 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2623 qel->tx.crypto.bufs[i] = NULL;
2624 }
2625 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002626 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002627}
2628
2629/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002630 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 * Returns 1 if succeeded, 0 if not.
2632 */
2633static int quic_conn_enc_level_init(struct quic_conn *qc,
2634 enum quic_tls_enc_level level)
2635{
2636 struct quic_enc_level *qel;
2637
2638 qel = &qc->els[level];
2639 qel->level = quic_to_ssl_enc_level(level);
2640 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2641 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2642 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2643 qel->tls_ctx.rx.flags = 0;
2644 qel->tls_ctx.tx.flags = 0;
2645
2646 qel->rx.pkts = EB_ROOT;
2647 LIST_INIT(&qel->rx.pqpkts);
2648
2649 /* Allocate only one buffer. */
2650 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2651 if (!qel->tx.crypto.bufs)
2652 goto err;
2653
2654 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2655 if (!qel->tx.crypto.bufs[0])
2656 goto err;
2657
2658 qel->tx.crypto.bufs[0]->sz = 0;
2659 qel->tx.crypto.nb_buf = 1;
2660
2661 qel->tx.crypto.sz = 0;
2662 qel->tx.crypto.offset = 0;
2663
2664 return 1;
2665
2666 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002667 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 return 0;
2669}
2670
2671/* Release the memory allocated for <buf> array of buffers, with <nb> as size.
2672 * Never fails.
2673 */
2674static inline void free_quic_conn_tx_bufs(struct q_buf **bufs, size_t nb)
2675{
2676 struct q_buf **p;
2677
2678 if (!bufs)
2679 return;
2680
2681 p = bufs;
2682 while (--nb) {
2683 if (!*p) {
2684 p++;
2685 continue;
2686 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002687 ha_free(&(*p)->area);
2688 ha_free(p);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002689 p++;
2690 }
2691 free(bufs);
2692}
2693
2694/* Allocate an array or <nb> buffers of <sz> bytes each.
2695 * Return this array if succeeded, NULL if failed.
2696 */
2697static inline struct q_buf **quic_conn_tx_bufs_alloc(size_t nb, size_t sz)
2698{
2699 int i;
2700 struct q_buf **bufs, **p;
2701
2702 bufs = calloc(nb, sizeof *bufs);
2703 if (!bufs)
2704 return NULL;
2705
2706 i = 0;
2707 p = bufs;
2708 while (i++ < nb) {
2709 *p = calloc(1, sizeof **p);
2710 if (!*p)
2711 goto err;
2712
2713 (*p)->area = malloc(sz);
2714 if (!(*p)->area)
2715 goto err;
2716
2717 (*p)->pos = (*p)->area;
2718 (*p)->end = (*p)->area + sz;
2719 (*p)->data = 0;
2720 LIST_INIT(&(*p)->pkts);
2721 p++;
2722 }
2723
2724 return bufs;
2725
2726 err:
2727 free_quic_conn_tx_bufs(bufs, nb);
2728 return NULL;
2729}
2730
2731/* Release all the memory allocated for <conn> QUIC connection. */
2732static void quic_conn_free(struct quic_conn *conn)
2733{
2734 int i;
2735
2736 free_quic_conn_cids(conn);
2737 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2738 quic_conn_enc_level_uninit(&conn->els[i]);
2739 free_quic_conn_tx_bufs(conn->tx.bufs, conn->tx.nb_buf);
2740 if (conn->timer_task)
2741 task_destroy(conn->timer_task);
2742 pool_free(pool_head_quic_conn, conn);
2743}
2744
2745/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002746static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002747{
2748 struct quic_conn_ctx *conn_ctx;
2749 struct quic_conn *qc;
2750 struct quic_pktns *pktns;
2751
2752
2753 conn_ctx = task->context;
2754 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002755 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2756 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002757 task->expire = TICK_ETERNITY;
2758 pktns = quic_loss_pktns(qc);
2759 if (tick_isset(pktns->tx.loss_time)) {
2760 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2761
2762 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2763 if (!LIST_ISEMPTY(&lost_pkts))
2764 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2765 qc_set_timer(conn_ctx);
2766 goto out;
2767 }
2768
2769 if (qc->path->in_flight) {
2770 pktns = quic_pto_pktns(qc, conn_ctx->state >= QUIC_HS_ST_COMPLETE, NULL);
2771 pktns->tx.pto_probe = 1;
2772 }
2773 else if (objt_server(qc->conn->target) && conn_ctx->state <= QUIC_HS_ST_COMPLETE) {
2774 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2775 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2776
2777 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2778 hel->pktns->tx.pto_probe = 1;
2779 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2780 iel->pktns->tx.pto_probe = 1;
2781 }
2782 qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS;
2783 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2784 qc->path->loss.pto_count++;
2785
2786 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002787 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002788
2789 return task;
2790}
2791
2792/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2793 * connections used to identify the first Initial packets of client connecting
2794 * to listeners. This parameter must be NULL for QUIC connections attached
2795 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2796 * <scid> is the source connection ID with <scid_len> as length.
2797 * Returns 1 if succeeded, 0 if not.
2798 */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002799static int qc_new_conn_init(struct quic_conn *qc, int ipv4,
2800 struct eb_root *quic_initial_clients,
2801 struct eb_root *quic_clients,
2802 unsigned char *dcid, size_t dcid_len,
2803 unsigned char *scid, size_t scid_len, int server)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002804{
2805 int i;
2806 /* Initial CID. */
2807 struct quic_connection_id *icid;
2808
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002809 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002810 qc->cids = EB_ROOT;
2811 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002812 if (server) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813 /* Copy the initial DCID. */
2814 qc->odcid.len = dcid_len;
2815 if (qc->odcid.len)
2816 memcpy(qc->odcid.data, dcid, dcid_len);
2817
2818 /* Copy the SCID as our DCID for this connection. */
2819 if (scid_len)
2820 memcpy(qc->dcid.data, scid, scid_len);
2821 qc->dcid.len = scid_len;
2822 }
2823 /* QUIC Client (outgoing connection to servers) */
2824 else {
2825 if (dcid_len)
2826 memcpy(qc->dcid.data, dcid, dcid_len);
2827 qc->dcid.len = dcid_len;
2828 }
2829
2830 /* Initialize the output buffer */
2831 qc->obuf.pos = qc->obuf.data;
2832
2833 icid = new_quic_cid(&qc->cids, 0);
2834 if (!icid)
2835 return 0;
2836
2837 /* Select our SCID which is the first CID with 0 as sequence number. */
2838 qc->scid = icid->cid;
2839
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002840 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002841 if (server)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002842 ebmb_insert(quic_initial_clients, &qc->odcid_node, qc->odcid.len);
2843
2844 /* Insert our SCID, the connection ID for the QUIC client. */
2845 ebmb_insert(quic_clients, &qc->scid_node, qc->scid.len);
2846
2847 /* Packet number spaces initialization. */
2848 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2849 quic_pktns_init(&qc->pktns[i]);
2850 /* QUIC encryption level context initialization. */
2851 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
2852 if (!quic_conn_enc_level_init(qc, i))
2853 goto err;
2854 /* Initialize the packet number space. */
2855 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2856 }
2857
2858 /* TX part. */
2859 LIST_INIT(&qc->tx.frms_to_send);
2860 qc->tx.bufs = quic_conn_tx_bufs_alloc(QUIC_CONN_TX_BUFS_NB, QUIC_CONN_TX_BUF_SZ);
2861 if (!qc->tx.bufs)
2862 goto err;
2863
2864 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2865 qc->tx.wbuf = qc->tx.rbuf = 0;
2866 qc->tx.bytes = 0;
2867 qc->tx.nb_pto_dgrams = 0;
2868 /* RX part. */
2869 qc->rx.bytes = 0;
2870
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002871 /* XXX TO DO: Only one path at this time. */
2872 qc->path = &qc->paths[0];
2873 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2874
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002875 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002876
2877 return 1;
2878
2879 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002880 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002881 quic_conn_free(qc);
2882 return 0;
2883}
2884
2885/* Initialize the timer task of <qc> QUIC connection.
2886 * Returns 1 if succeeded, 0 if not.
2887 */
2888static int quic_conn_init_timer(struct quic_conn *qc)
2889{
2890 qc->timer_task = task_new(MAX_THREADS_MASK);
2891 if (!qc->timer_task)
2892 return 0;
2893
2894 qc->timer = TICK_ETERNITY;
2895 qc->timer_task->process = process_timer;
2896 qc->timer_task->context = qc->conn->xprt_ctx;
2897
2898 return 1;
2899}
2900
2901/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2902 * past one byte of this buffer.
2903 */
2904static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2905 struct quic_rx_packet *pkt)
2906{
2907 unsigned char dcid_len, scid_len;
2908
2909 /* Version */
2910 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2911 return 0;
2912
2913 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2914
2915 /* Destination Connection ID Length */
2916 dcid_len = *(*buf)++;
2917 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2918 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2919 /* XXX MUST BE DROPPED */
2920 return 0;
2921
2922 if (dcid_len) {
2923 /* Check that the length of this received DCID matches the CID lengths
2924 * of our implementation for non Initials packets only.
2925 */
2926 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2927 return 0;
2928
2929 memcpy(pkt->dcid.data, *buf, dcid_len);
2930 }
2931
2932 pkt->dcid.len = dcid_len;
2933 *buf += dcid_len;
2934
2935 /* Source Connection ID Length */
2936 scid_len = *(*buf)++;
2937 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2938 /* XXX MUST BE DROPPED */
2939 return 0;
2940
2941 if (scid_len)
2942 memcpy(pkt->scid.data, *buf, scid_len);
2943 pkt->scid.len = scid_len;
2944 *buf += scid_len;
2945
2946 return 1;
2947}
2948
2949/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
2950 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
2951 * byte past the end of the buffer containing this packet and <beg> the address of
2952 * the packet first byte.
2953 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
2954 * Returns 1 if succeeded, 0 if not.
2955 */
2956static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
2957 unsigned char **buf, unsigned char *beg,
2958 const unsigned char *end,
2959 struct quic_conn_ctx *ctx)
2960{
2961 unsigned char *pn = NULL; /* Packet number field */
2962 enum quic_tls_enc_level tel;
2963 struct quic_enc_level *qel;
2964 /* Only for traces. */
2965 struct quic_rx_packet *qpkt_trace;
2966
2967 qpkt_trace = NULL;
2968 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx->conn);
2969 /* The packet number is here. This is also the start minus
2970 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
2971 * protection.
2972 */
2973 pn = *buf;
2974 tel = quic_packet_type_enc_level(pkt->type);
2975 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2976 TRACE_DEVEL("Wrong enc. level", QUIC_EV_CONN_TRMHP, ctx->conn);
2977 goto err;
2978 }
2979
2980 qel = &ctx->conn->qc->els[tel];
2981
2982 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
2983 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
2984 goto err;
2985 }
2986
2987 if ((qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
2988 (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->state >= QUIC_HS_ST_COMPLETE)) {
2989 /* Note that the following function enables us to unprotect the packet
2990 * number and its length subsequently used to decrypt the entire
2991 * packets.
2992 */
2993 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
2994 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
2995 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx->conn);
2996 goto err;
2997 }
2998
2999 /* The AAD includes the packet number field found at <pn>. */
3000 pkt->aad_len = pn - beg + pkt->pnl;
3001 qpkt_trace = pkt;
3002 /* Store the packet */
3003 pkt->pn_node.key = pkt->pn;
3004 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3005 }
3006 else {
3007 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx->conn, pkt);
3008 pkt->pn_offset = pn - beg;
3009 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3010 }
3011
3012 memcpy(pkt->data, beg, pkt->len);
3013 /* Updtate the offset of <*buf> for the next QUIC packet. */
3014 *buf = beg + pkt->len;
3015
3016 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx->conn, qpkt_trace);
3017 return 1;
3018
3019 err:
3020 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx->conn, qpkt_trace);
3021 return 0;
3022}
3023
3024/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3025 * Also set <*long_header> to 1 if this form is long, 0 if not.
3026 */
3027static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3028 unsigned char byte0, int *long_header)
3029{
3030 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3031 pkt->type =
3032 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3033 *long_header = 1;
3034 }
3035 else {
3036 pkt->type = QUIC_PACKET_TYPE_SHORT;
3037 *long_header = 0;
3038 }
3039}
3040
3041static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3042 struct quic_rx_packet *pkt,
3043 struct quic_dgram_ctx *dgram_ctx,
3044 struct sockaddr_storage *saddr)
3045{
3046 unsigned char *beg;
3047 uint64_t len;
3048 struct quic_conn *qc;
3049 struct eb_root *cids;
3050 struct ebmb_node *node;
3051 struct connection *srv_conn;
3052 struct quic_conn_ctx *conn_ctx;
3053 int long_header;
3054
3055 qc = NULL;
3056 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3057 if (end <= *buf)
3058 goto err;
3059
3060 /* Fixed bit */
3061 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3062 /* XXX TO BE DISCARDED */
3063 goto err;
3064
3065 srv_conn = dgram_ctx->owner;
3066 beg = *buf;
3067 /* Header form */
3068 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3069 if (long_header) {
3070 size_t cid_lookup_len;
3071
3072 if (!quic_packet_read_long_header(buf, end, pkt))
3073 goto err;
3074
3075 /* For Initial packets, and for servers (QUIC clients connections),
3076 * there is no Initial connection IDs storage.
3077 */
3078 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3079 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3080 cid_lookup_len = pkt->dcid.len;
3081 }
3082 else {
3083 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3084 cid_lookup_len = QUIC_CID_LEN;
3085 }
3086
3087 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3088 if (!node)
3089 goto err;
3090
3091 qc = ebmb_entry(node, struct quic_conn, scid_node);
3092
3093 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3094 qc->dcid.len = pkt->scid.len;
3095 if (pkt->scid.len)
3096 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3097 }
3098
3099 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3100 uint64_t token_len;
3101
3102 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3103 goto err;
3104
3105 /* XXX TO DO XXX 0 value means "the token is not present".
3106 * A server which sends an Initial packet must not set the token.
3107 * So, a client which receives an Initial packet with a token
3108 * MUST discard the packet or generate a connection error with
3109 * PROTOCOL_VIOLATION as type.
3110 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3111 */
3112 pkt->token_len = token_len;
3113 }
3114 }
3115 else {
3116 /* XXX TO DO: Short header XXX */
3117 if (end - *buf < QUIC_CID_LEN)
3118 goto err;
3119
3120 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3121 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3122 if (!node)
3123 goto err;
3124
3125 qc = ebmb_entry(node, struct quic_conn, scid_node);
3126 *buf += QUIC_CID_LEN;
3127 }
3128 /* Store the DCID used for this packet to check the packet which
3129 * come in this UDP datagram match with it.
3130 */
3131 if (!dgram_ctx->dcid_node)
3132 dgram_ctx->dcid_node = node;
3133 /* Only packets packets with long headers and not RETRY or VERSION as type
3134 * have a length field.
3135 */
3136 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3137 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3138 goto err;
3139
3140 pkt->len = len;
3141 }
3142 else if (!long_header) {
3143 /* A short packet is the last one of an UDP datagram. */
3144 pkt->len = end - *buf;
3145 }
3146
3147 conn_ctx = qc->conn->xprt_ctx;
3148
3149 /* Increase the total length of this packet by the header length. */
3150 pkt->len += *buf - beg;
3151 /* Do not check the DCID node before the length. */
3152 if (dgram_ctx->dcid_node != node) {
3153 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3154 goto err;
3155 }
3156
3157 if (pkt->len > sizeof pkt->data) {
3158 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3159 goto err;
3160 }
3161
3162 if (!qc_try_rm_hp(pkt, buf, beg, end, conn_ctx))
3163 goto err;
3164
3165 /* Wake the tasklet of the QUIC connection packet handler. */
3166 if (conn_ctx)
3167 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3168
3169 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3170
3171 return pkt->len;
3172
3173 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003174 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175 return -1;
3176}
3177
3178static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3179 struct quic_rx_packet *pkt,
3180 struct quic_dgram_ctx *dgram_ctx,
3181 struct sockaddr_storage *saddr)
3182{
3183 unsigned char *beg;
3184 uint64_t len;
3185 struct quic_conn *qc;
3186 struct eb_root *cids;
3187 struct ebmb_node *node;
3188 struct listener *l;
3189 struct quic_conn_ctx *conn_ctx;
3190 int long_header = 0;
3191
3192 qc = NULL;
3193 TRACE_ENTER(QUIC_EV_CONN_LPKT);
3194 if (end <= *buf)
3195 goto err;
3196
3197 /* Fixed bit */
3198 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3199 /* XXX TO BE DISCARDED */
3200 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3201 goto err;
3202 }
3203
3204 l = dgram_ctx->owner;
3205 beg = *buf;
3206 /* Header form */
3207 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3208 if (long_header) {
3209 unsigned char dcid_len;
3210
3211 if (!quic_packet_read_long_header(buf, end, pkt)) {
3212 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3213 goto err;
3214 }
3215
3216 dcid_len = pkt->dcid.len;
3217 /* For Initial packets, and for servers (QUIC clients connections),
3218 * there is no Initial connection IDs storage.
3219 */
3220 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3221 /* DCIDs of first packets coming from clients may have the same values.
3222 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3223 */
3224 quic_cid_saddr_cat(&pkt->dcid, saddr);
3225 cids = &l->rx.odcids;
3226 }
3227 else {
3228 if (pkt->dcid.len != QUIC_CID_LEN) {
3229 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3230 goto err;
3231 }
3232
3233 cids = &l->rx.cids;
3234 }
3235
3236 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3237 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3238 cids == &l->rx.odcids) {
3239 /* Switch to the definitive tree ->cids containing the final CIDs. */
3240 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3241 if (node) {
3242 /* If found, signal this with NULL as special value for <cids>. */
3243 pkt->dcid.len = dcid_len;
3244 cids = NULL;
3245 }
3246 }
3247
3248 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003249 int ipv4;
3250 struct quic_cid *odcid;
3251
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003252 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3253 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3254 goto err;
3255 }
3256
3257 qc = new_quic_conn(pkt->version);
3258 if (!qc) {
3259 TRACE_PROTO("Non allocated new connection", QUIC_EV_CONN_LPKT);
3260 goto err;
3261 }
3262
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003263 pkt->saddr = *saddr;
3264 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3265 * of <saddr>.
3266 */
3267 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003268 ipv4 = saddr->ss_family == AF_INET;
3269 if (!qc_new_conn_init(qc, ipv4, &l->rx.odcids, &l->rx.cids,
3270 pkt->dcid.data, pkt->dcid.len,
3271 pkt->scid.data, pkt->scid.len, 1))
3272 goto err;
3273
3274 odcid = &qc->rx.params.original_destination_connection_id;
3275 /* Copy the transport parameters. */
3276 qc->rx.params = l->bind_conf->quic_params;
3277 /* Copy original_destination_connection_id transport parameter. */
3278 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3279 odcid->len = pkt->odcid_len;
3280 /* Copy the initial source connection ID. */
3281 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3282 qc->enc_params_len =
3283 quic_transport_params_encode(qc->enc_params,
3284 qc->enc_params + sizeof qc->enc_params,
3285 &qc->rx.params, 1);
3286 if (!qc->enc_params_len)
3287 goto err;
3288
3289 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003290 /* Enqueue this packet. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003291 LIST_APPEND(&l->rx.qpkts, &pkt->rx_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003292 /* Try to accept a new connection. */
3293 listener_accept(l);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003294 /* This is the DCID node sent in this packet by the client. */
3295 node = &qc->odcid_node;
3296 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003297 }
3298 else {
3299 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3300 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3301 else
3302 qc = ebmb_entry(node, struct quic_conn, scid_node);
3303 }
3304
3305 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3306 uint64_t token_len;
3307 struct quic_tls_ctx *ctx =
3308 &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx;
3309
3310 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3311 end - *buf < token_len) {
3312 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3313 goto err;
3314 }
3315
3316 /* XXX TO DO XXX 0 value means "the token is not present".
3317 * A server which sends an Initial packet must not set the token.
3318 * So, a client which receives an Initial packet with a token
3319 * MUST discard the packet or generate a connection error with
3320 * PROTOCOL_VIOLATION as type.
3321 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3322 */
3323 pkt->token_len = token_len;
3324 /* NOTE: the socket address has been concatenated to the destination ID
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003325 * chosen by the client for Initial packets.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003326 */
3327 if (!ctx->rx.hp && !qc_new_isecs(qc->conn, pkt->dcid.data,
3328 pkt->odcid_len, 1)) {
3329 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3330 goto err;
3331 }
3332 }
3333 }
3334 else {
3335 if (end - *buf < QUIC_CID_LEN) {
3336 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3337 goto err;
3338 }
3339
3340 cids = &l->rx.cids;
3341 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3342 if (!node) {
3343 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3344 goto err;
3345 }
3346
3347 qc = ebmb_entry(node, struct quic_conn, scid_node);
3348 *buf += QUIC_CID_LEN;
3349 }
3350
3351 /* Store the DCID used for this packet to check the packet which
3352 * come in this UDP datagram match with it.
3353 */
3354 if (!dgram_ctx->dcid_node) {
3355 dgram_ctx->dcid_node = node;
3356 dgram_ctx->qc = qc;
3357 }
3358
3359 /* Only packets packets with long headers and not RETRY or VERSION as type
3360 * have a length field.
3361 */
3362 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3363 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3364 end - *buf < len) {
3365 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3366 goto err;
3367 }
3368
3369 pkt->len = len;
3370 }
3371 else if (!long_header) {
3372 /* A short packet is the last one of an UDP datagram. */
3373 pkt->len = end - *buf;
3374 }
3375
3376 /* Update the state if needed. */
3377 conn_ctx = qc->conn->xprt_ctx;
3378
3379 /* Increase the total length of this packet by the header length. */
3380 pkt->len += *buf - beg;
3381 /* Do not check the DCID node before the length. */
3382 if (dgram_ctx->dcid_node != node) {
3383 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3384 goto err;
3385 }
3386
3387 if (pkt->len > sizeof pkt->data) {
3388 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3389 goto err;
3390 }
3391
3392 if (!qc_try_rm_hp(pkt, buf, beg, end, conn_ctx)) {
3393 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3394 goto err;
3395 }
3396
3397 /* Wake the tasklet of the QUIC connection packet handler. */
3398 if (conn_ctx)
3399 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3400 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3401
3402 return pkt->len;
3403
3404 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003405 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406 qc ? qc->conn : NULL, pkt);
3407 return -1;
3408}
3409
3410/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3411 * in advance. This is the reponsability of the caller to check there is enough room in this
3412 * buffer to build a long header.
3413 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3414 */
3415static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3416 int type, size_t pn_len, struct quic_conn *conn)
3417{
3418 if (type > QUIC_PACKET_TYPE_RETRY)
3419 return 0;
3420
3421 /* #0 byte flags */
3422 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3423 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3424 /* Version */
3425 quic_write_uint32(buf, end, conn->version);
3426 *(*buf)++ = conn->dcid.len;
3427 /* Destination connection ID */
3428 if (conn->dcid.len) {
3429 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3430 *buf += conn->dcid.len;
3431 }
3432 /* Source connection ID */
3433 *(*buf)++ = conn->scid.len;
3434 if (conn->scid.len) {
3435 memcpy(*buf, conn->scid.data, conn->scid.len);
3436 *buf += conn->scid.len;
3437 }
3438
3439 return 1;
3440}
3441
3442/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3443 * in advance. This is the reponsability of the caller to check there is enough room in this
3444 * buffer to build a long header.
3445 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3446 */
3447static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3448 size_t pn_len, struct quic_conn *conn)
3449{
3450 /* #0 byte flags */
3451 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3452 /* Destination connection ID */
3453 if (conn->dcid.len) {
3454 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3455 *buf += conn->dcid.len;
3456 }
3457
3458 return 1;
3459}
3460
3461/* Apply QUIC header protection to the packet with <buf> as first byte address,
3462 * <pn> as address of the Packet number field, <pnlen> being this field length
3463 * with <aead> as AEAD cipher and <key> as secret key.
3464 * Returns 1 if succeeded or 0 if failed.
3465 */
3466static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3467 const EVP_CIPHER *aead, const unsigned char *key)
3468{
3469 int i, ret, outlen;
3470 EVP_CIPHER_CTX *ctx;
3471 /* We need an IV of at least 5 bytes: one byte for bytes #0
3472 * and at most 4 bytes for the packet number
3473 */
3474 unsigned char mask[5] = {0};
3475
3476 ret = 0;
3477 ctx = EVP_CIPHER_CTX_new();
3478 if (!ctx)
3479 return 0;
3480
3481 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3482 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3483 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3484 goto out;
3485
3486 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3487 for (i = 0; i < pnlen; i++)
3488 pn[i] ^= mask[i + 1];
3489
3490 ret = 1;
3491
3492 out:
3493 EVP_CIPHER_CTX_free(ctx);
3494
3495 return ret;
3496}
3497
3498/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3499 * ACK ranges if needed to a value below <limit> in bytes.
3500 * Return 1 if succeeded, 0 if not.
3501 */
3502static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3503{
3504 size_t room, ack_delay_sz;
3505
3506 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3507 /* A frame is made of 1 byte for the frame type. */
3508 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003509 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003510 return 0;
3511
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003512 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003513}
3514
3515/* Prepare as most as possible CRYPTO frames from prebuilt CRYPTO frames for <qel>
3516 * encryption level to be encoded in a buffer with <room> as available room,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003517 * and <*len> the packet Length field initialized with the number of bytes already present
3518 * in this buffer which must be taken into an account for the Length packet field value.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003519 * <headlen> is the number of bytes already present in this packet before building
Frédéric Lécailleea604992020-12-24 13:01:37 +01003520 * CRYPTO frames.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003521 * This is the responsibility of the caller to check that <*len> < <room> as this is
3522 * the responsibility to check that <headlen> < quic_path_prep_data(conn->path).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003523 * Update consequently <*len> to reflect the size of these CRYPTO frames built
3524 * by this function. Also attach these CRYPTO frames to <pkt> QUIC packet.
3525 * Return 1 if succeeded, 0 if not.
3526 */
3527static inline int qc_build_cfrms(struct quic_tx_packet *pkt,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003528 size_t room, size_t *len, size_t headlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003529 struct quic_enc_level *qel,
3530 struct quic_conn *conn)
3531{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003532 int ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003533 struct quic_tx_frm *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003534
Frédéric Lécailleea604992020-12-24 13:01:37 +01003535 ret = 0;
3536 /* If we are not probing we must take into an account the congestion
3537 * control window.
3538 */
3539 if (!conn->tx.nb_pto_dgrams)
3540 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
3541 TRACE_PROTO("************** CRYPTO frames build (headlen)",
3542 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003543 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
3544 /* header length, data length, frame length. */
3545 size_t hlen, dlen, cflen;
3546
Frédéric Lécailleea604992020-12-24 13:01:37 +01003547 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3548 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3549 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003550 break;
3551
3552 /* Compute the length of this CRYPTO frame header */
3553 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3554 /* Compute the data length of this CRyPTO frame. */
3555 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécailleea604992020-12-24 13:01:37 +01003556 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3557 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 if (!dlen)
3559 break;
3560
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561 pkt->cdata_len += dlen;
3562 /* CRYPTO frame length. */
3563 cflen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003564 TRACE_PROTO(" CRYPTO frame length (cflen)",
3565 QUIC_EV_CONN_BCFRMS, conn->conn, &cflen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003566 /* Add the CRYPTO data length and its encoded length to the packet
3567 * length and the length of this length.
3568 */
3569 *len += cflen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003570 room -= cflen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003571 if (dlen == cf->crypto.len) {
3572 /* <cf> CRYPTO data have been consumed. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003573 LIST_DELETE(&cf->list);
3574 LIST_APPEND(&pkt->frms, &cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003575 }
3576 else {
3577 struct quic_tx_frm *new_cf;
3578
3579 new_cf = pool_alloc(pool_head_quic_tx_frm);
3580 if (!new_cf) {
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003581 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003582 return 0;
3583 }
3584
3585 new_cf->type = QUIC_FT_CRYPTO;
3586 new_cf->crypto.len = dlen;
3587 new_cf->crypto.offset = cf->crypto.offset;
Willy Tarreau2b718102021-04-21 07:32:39 +02003588 LIST_APPEND(&pkt->frms, &new_cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003589 /* Consume <dlen> bytes of the current frame. */
3590 cf->crypto.len -= dlen;
3591 cf->crypto.offset += dlen;
3592 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003593 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003594 }
3595
Frédéric Lécailleea604992020-12-24 13:01:37 +01003596 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003597}
3598
3599/* This function builds a clear handshake packet used during a QUIC TLS handshakes
3600 * into <wbuf> the current <wbuf> for <conn> QUIC connection with <qel> as QUIC
3601 * TLS encryption level for outgoing packets filling it with as much as CRYPTO
3602 * data as possible from <offset> offset in the CRYPTO data stream. Note that
3603 * this offset value is updated by the length of the CRYPTO frame used to embed
3604 * the CRYPTO data if this packet and only if the packet is successfully built.
3605 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
3606 * reserved so that to be sure there is enough room to build this AEAD TAG after
3607 * having successfully returned from this function and to be sure the position
3608 * pointer of <wbuf> may be safely incremented by QUIC_TLS_TAG_LEN. After having
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003609 * returned from this function, <wbuf> position will point one past the last
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003610 * byte of the payload with the confidence there is at least QUIC_TLS_TAG_LEN bytes
3611 * available packet to encrypt this packet.
3612 * This function also update the value of <buf_pn> pointer to point to the packet
3613 * number field in this packet. <pn_len> will also have the packet number
3614 * length as value.
3615 *
3616 * Return the length of the packet if succeeded minus QUIC_TLS_TAG_LEN, or -1 if
3617 * failed (not enough room in <wbuf> to build this packet plus QUIC_TLS_TAG_LEN
3618 * bytes), -2 if there are too much CRYPTO data in flight to build a packet.
3619 */
3620static ssize_t qc_do_build_hdshk_pkt(struct q_buf *wbuf,
3621 struct quic_tx_packet *pkt, int pkt_type,
3622 int64_t pn, size_t *pn_len,
3623 unsigned char **buf_pn,
3624 struct quic_enc_level *qel,
3625 struct quic_conn *conn)
3626{
3627 unsigned char *beg, *pos;
3628 const unsigned char *end;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003629 size_t len, len_frms, token_fields_len, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003630 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3631 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3632 struct quic_crypto *crypto = &frm.crypto;
3633 size_t ack_frm_len;
3634 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003635 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003636
Frédéric Lécailleea604992020-12-24 13:01:37 +01003637 /* Length field value with CRYPTO frames if present. */
3638 len_frms = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003639 beg = pos = q_buf_getpos(wbuf);
3640 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003641 /* When not probing and not acking, reduce the size of this buffer to respect
3642 * the congestion controller window.
3643 */
3644 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3645 size_t path_room;
3646
3647 path_room = quic_path_prep_data(conn->path);
3648 if (end - beg > path_room)
3649 end = beg + path_room;
3650 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003651
3652 /* For a server, the token field of an Initial packet is empty. */
3653 token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3654
3655 /* Check there is enough room to build the header followed by a token. */
3656 if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len +
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003657 conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) {
3658 ssize_t room = end - pos;
3659 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3660 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003661 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003662 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663
3664 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3665 end -= QUIC_TLS_TAG_LEN;
3666 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3667 /* packet number length */
3668 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3669
3670 quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn);
3671
3672 /* Encode the token length (0) for an Initial packet. */
3673 if (pkt_type == QUIC_PACKET_TYPE_INITIAL)
3674 *pos++ = 0;
3675
3676 /* Build an ACK frame if required. */
3677 ack_frm_len = 0;
3678 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003679 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003680 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003681 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003682 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003683 if (!ack_frm_len) {
3684 ssize_t room = end - pos;
3685 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3686 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003687 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003688 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689
3690 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3691 }
3692
3693 /* Length field value without the CRYPTO frames data length. */
3694 len = ack_frm_len + *pn_len;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003695 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003696 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003697
3698 len_frms = len + QUIC_TLS_TAG_LEN;
3699 if (!qc_build_cfrms(pkt, end - pos, &len_frms, pos - beg, qel, conn)) {
3700 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3701 conn->conn, NULL, NULL, &room);
3702 goto err;
3703 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003704 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003705
3706 add_ping_frm = 0;
3707 padding_len = 0;
3708 if (objt_server(conn->conn->target) &&
3709 pkt_type == QUIC_PACKET_TYPE_INITIAL &&
3710 len < QUIC_INITIAL_PACKET_MINLEN) {
3711 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3712 }
3713 else if (LIST_ISEMPTY(&pkt->frms)) {
3714 if (qel->pktns->tx.pto_probe) {
3715 /* If we cannot send a CRYPTO frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003716 add_ping_frm = 1;
3717 len += 1;
3718 }
3719 /* If there is no frame at all to follow, add at least a PADDING frame. */
3720 if (!ack_frm_len)
3721 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3722 }
3723
3724 /* Length (of the remaining data). Must not fail because, the buffer size
3725 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
3726 * for the encryption TAG. It must be taken into an account for the length
3727 * of this packet.
3728 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01003729 if (len_frms)
3730 len = len_frms;
3731 else
3732 len += QUIC_TLS_TAG_LEN;
3733 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003734
3735 /* Packet number field address. */
3736 *buf_pn = pos;
3737
3738 /* Packet number encoding. */
3739 quic_packet_number_encode(&pos, end, pn, *pn_len);
3740
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003741 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3742 ssize_t room = end - pos;
3743 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3744 conn->conn, NULL, NULL, &room);
3745 goto err;
3746 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003747
3748 /* Crypto frame */
3749 if (!LIST_ISEMPTY(&pkt->frms)) {
3750 struct quic_tx_frm *cf;
3751
3752 list_for_each_entry(cf, &pkt->frms, list) {
3753 crypto->offset = cf->crypto.offset;
3754 crypto->len = cf->crypto.len;
3755 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003756 if (!qc_build_frm(&pos, end, &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 }
3764
3765 /* Build a PING frame if needed. */
3766 if (add_ping_frm) {
3767 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003768 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3769 ssize_t room = end - pos;
3770 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3771 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003772 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003773 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774 }
3775
3776 /* Build a PADDING frame if needed. */
3777 if (padding_len) {
3778 frm.type = QUIC_FT_PADDING;
3779 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003780 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3781 ssize_t room = end - pos;
3782 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3783 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003784 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003785 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003786 }
3787
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003788 /* Always reset this variable as this function has no idea
3789 * if it was set. It is handle by the loss detection timer.
3790 */
3791 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792
3793 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003794 return pos - beg;
3795
3796 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797 return -1;
3798}
3799
3800static inline void quic_tx_packet_init(struct quic_tx_packet *pkt)
3801{
3802 pkt->cdata_len = 0;
3803 pkt->in_flight_len = 0;
3804 LIST_INIT(&pkt->frms);
3805}
3806
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003807/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003808static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3809{
3810 struct quic_tx_frm *frm, *frmbak;
3811
3812 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003813 LIST_DELETE(&frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814 pool_free(pool_head_quic_tx_frm, frm);
3815 }
3816 pool_free(pool_head_quic_tx_packet, pkt);
3817}
3818
3819/* Build a handshake packet into <buf> packet buffer with <pkt_type> as packet
3820 * type for <qc> QUIC connection from CRYPTO data stream at <*offset> offset to
3821 * be encrypted at <qel> encryption level.
3822 * Return -2 if the packet could not be encrypted for any reason, -1 if there was
3823 * not enough room in <buf> to build the packet, or the size of the built packet
3824 * if succeeded (may be zero if there is too much crypto data in flight to build the packet).
3825 */
3826static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
3827 struct quic_enc_level *qel)
3828{
3829 /* The pointer to the packet number field. */
3830 unsigned char *buf_pn;
3831 unsigned char *beg, *end, *payload;
3832 int64_t pn;
3833 size_t pn_len, payload_len, aad_len;
3834 ssize_t pkt_len;
3835 struct quic_tls_ctx *tls_ctx;
3836 struct quic_tx_packet *pkt;
3837
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003838 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003839 pkt = pool_alloc(pool_head_quic_tx_packet);
3840 if (!pkt) {
3841 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
3842 return -2;
3843 }
3844
3845 quic_tx_packet_init(pkt);
3846 beg = q_buf_getpos(buf);
3847 pn_len = 0;
3848 buf_pn = NULL;
3849 pn = qel->pktns->tx.next_pn + 1;
3850 pkt_len = qc_do_build_hdshk_pkt(buf, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc);
3851 if (pkt_len <= 0) {
3852 free_quic_tx_packet(pkt);
3853 return pkt_len;
3854 }
3855
3856 end = beg + pkt_len;
3857 payload = buf_pn + pn_len;
3858 payload_len = end - payload;
3859 aad_len = payload - beg;
3860
3861 tls_ctx = &qel->tls_ctx;
3862 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn))
3863 goto err;
3864
3865 end += QUIC_TLS_TAG_LEN;
3866 pkt_len += QUIC_TLS_TAG_LEN;
3867 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3868 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
3869 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
3870 goto err;
3871 }
3872
3873 /* Now that a correct packet is built, let us set the position pointer of
3874 * <buf> buffer for the next packet.
3875 */
3876 q_buf_setpos(buf, end);
3877 /* Consume a packet number. */
3878 ++qel->pktns->tx.next_pn;
3879 /* Attach the built packet to its tree. */
3880 pkt->pn_node.key = qel->pktns->tx.next_pn;
3881 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003882 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003883 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003884 qc->path->prep_in_flight += pkt_len;
3885 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003886 pkt->pktns = qel->pktns;
3887 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
3888 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
3889 buf->data += pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003890 /* Attach this packet to <buf>. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003891 LIST_APPEND(&buf->pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003892 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
3893
3894 return pkt_len;
3895
3896 err:
3897 free_quic_tx_packet(pkt);
3898 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
3899 return -2;
3900}
3901
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003902/* Prepare a clear post handhskake packet for <conn> QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003903 * Return the length of this packet if succeeded, -1 <wbuf> was full.
3904 */
3905static ssize_t qc_do_build_phdshk_apkt(struct q_buf *wbuf,
3906 struct quic_tx_packet *pkt,
3907 int64_t pn, size_t *pn_len,
3908 unsigned char **buf_pn, struct quic_enc_level *qel,
3909 struct quic_conn *conn)
3910{
3911 const unsigned char *beg, *end;
3912 unsigned char *pos;
3913 struct quic_frame *frm, *sfrm;
3914 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3915 size_t fake_len, ack_frm_len;
3916 int64_t largest_acked_pn;
3917
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003918 TRACE_ENTER(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003919 beg = pos = q_buf_getpos(wbuf);
3920 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003921 /* When not probing and not acking, reduce the size of this buffer to respect
3922 * the congestion controller window.
3923 */
3924 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3925 size_t path_room;
3926
3927 path_room = quic_path_prep_data(conn->path);
3928 if (end - beg > path_room)
3929 end = beg + path_room;
3930 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003931 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3932 /* Packet number length */
3933 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3934 /* Check there is enough room to build this packet (without payload). */
3935 if (end - pos < QUIC_SHORT_PACKET_MINLEN + sizeof_quic_cid(&conn->dcid) +
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003936 *pn_len + QUIC_TLS_TAG_LEN) {
3937 ssize_t room = end - pos;
3938 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3939 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003940 goto err;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003941 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003942
3943 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3944 end -= QUIC_TLS_TAG_LEN;
3945 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3946 /* Packet number field. */
3947 *buf_pn = pos;
3948 /* Packet number encoding. */
3949 quic_packet_number_encode(&pos, end, pn, *pn_len);
3950
3951 /* Build an ACK frame if required. */
3952 ack_frm_len = 0;
3953 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003954 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003955 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003956 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
3958 if (!ack_frm_len)
3959 goto err;
3960
3961 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3962 }
3963
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003964 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3965 ssize_t room = end - pos;
3966 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3967 conn->conn, NULL, NULL, &room);
3968 goto err;
3969 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003970
3971 fake_len = ack_frm_len;
3972 if (!LIST_ISEMPTY(&qel->pktns->tx.frms) &&
Frédéric Lécailleea604992020-12-24 13:01:37 +01003973 !qc_build_cfrms(pkt, end - pos, &fake_len, pos - beg, qel, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003974 ssize_t room = end - pos;
3975 TRACE_PROTO("some CRYPTO frames could not be built",
3976 QUIC_EV_CONN_PAPKT, conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003977 goto err;
3978 }
3979
3980 /* Crypto frame */
3981 if (!LIST_ISEMPTY(&pkt->frms)) {
3982 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3983 struct quic_crypto *crypto = &frm.crypto;
3984 struct quic_tx_frm *cf;
3985
3986 list_for_each_entry(cf, &pkt->frms, list) {
3987 crypto->offset = cf->crypto.offset;
3988 crypto->len = cf->crypto.len;
3989 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003990 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3991 ssize_t room = end - pos;
3992 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3993 conn->conn, NULL, NULL, &room);
3994 goto err;
3995 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003996 }
3997 }
3998
3999 /* Encode a maximum of frames. */
4000 list_for_each_entry_safe(frm, sfrm, &conn->tx.frms_to_send, list) {
4001 unsigned char *ppos;
4002
4003 ppos = pos;
4004 if (!qc_build_frm(&ppos, end, frm, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004005 TRACE_DEVEL("Frames not built", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004006 break;
4007 }
4008
Willy Tarreau2b718102021-04-21 07:32:39 +02004009 LIST_DELETE(&frm->list);
4010 LIST_APPEND(&pkt->frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004011 pos = ppos;
4012 }
4013
4014 out:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004015 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004016 return pos - beg;
4017
4018 err:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004019 TRACE_DEVEL("leaving in error (buffer full)", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004020 return -1;
4021}
4022
4023/* Prepare a post handhskake packet at Application encryption level for <conn>
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004024 * QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004025 * Return the length of this packet if succeeded, -1 if <wbuf> was full,
4026 * -2 in case of major error (encryption failure).
4027 */
4028static ssize_t qc_build_phdshk_apkt(struct q_buf *wbuf, struct quic_conn *qc)
4029{
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004030 /* A pointer to the packet number field in <buf> */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004031 unsigned char *buf_pn;
4032 unsigned char *beg, *end, *payload;
4033 int64_t pn;
4034 size_t pn_len, aad_len, payload_len;
4035 ssize_t pkt_len;
4036 struct quic_tls_ctx *tls_ctx;
4037 struct quic_enc_level *qel;
4038 struct quic_tx_packet *pkt;
4039
4040 TRACE_ENTER(QUIC_EV_CONN_PAPKT, qc->conn);
4041 pkt = pool_alloc(pool_head_quic_tx_packet);
4042 if (!pkt) {
4043 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_PAPKT, qc->conn);
4044 return -2;
4045 }
4046
4047 quic_tx_packet_init(pkt);
4048 beg = q_buf_getpos(wbuf);
4049 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4050 pn_len = 0;
4051 buf_pn = NULL;
4052 pn = qel->pktns->tx.next_pn + 1;
4053 pkt_len = qc_do_build_phdshk_apkt(wbuf, pkt, pn, &pn_len, &buf_pn, qel, qc);
4054 if (pkt_len <= 0) {
4055 free_quic_tx_packet(pkt);
4056 return pkt_len;
4057 }
4058
4059 end = beg + pkt_len;
4060 payload = buf_pn + pn_len;
4061 payload_len = end - payload;
4062 aad_len = payload - beg;
4063
4064 tls_ctx = &qel->tls_ctx;
4065 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 +01004066 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004067
4068 end += QUIC_TLS_TAG_LEN;
4069 pkt_len += QUIC_TLS_TAG_LEN;
4070 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4071 tls_ctx->tx.hp, tls_ctx->tx.hp_key))
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004072 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004073
4074 q_buf_setpos(wbuf, end);
4075 /* Consume a packet number. */
4076 ++qel->pktns->tx.next_pn;
4077 /* Attach the built packet to its tree. */
4078 pkt->pn_node.key = qel->pktns->tx.next_pn;
4079 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
4080 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004081 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004082 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004083 qc->path->prep_in_flight += pkt_len;
4084 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004085 pkt->pktns = qel->pktns;
4086 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
4087 wbuf->data += pkt_len;
4088 /* Attach this packet to <buf>. */
Willy Tarreau2b718102021-04-21 07:32:39 +02004089 LIST_APPEND(&wbuf->pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004090
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004091 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, qc->conn, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004092
4093 return pkt_len;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004094
4095 err:
4096 free_quic_tx_packet(pkt);
4097 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PAPKT, qc->conn);
4098 return -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004099}
4100
4101/* Prepare a maximum of QUIC Application level packets from <ctx> QUIC
4102 * connection I/O handler context.
4103 * Returns 1 if succeeded, 0 if not.
4104 */
4105int qc_prep_phdshk_pkts(struct quic_conn *qc)
4106{
4107 struct q_buf *wbuf;
4108 struct quic_enc_level *qel;
4109
4110 TRACE_ENTER(QUIC_EV_CONN_PAPKTS, qc->conn);
4111 wbuf = q_wbuf(qc);
4112 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4113 while (q_buf_empty(wbuf)) {
4114 ssize_t ret;
4115
4116 if (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
4117 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004118 qc->path->prep_in_flight >= qc->path->cwnd)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004119 TRACE_DEVEL("nothing more to do",
4120 QUIC_EV_CONN_PAPKTS, qc->conn);
4121 break;
4122 }
4123
4124 ret = qc_build_phdshk_apkt(wbuf, qc);
4125 switch (ret) {
4126 case -1:
4127 /* Not enough room left in <wbuf>. */
4128 wbuf = q_next_wbuf(qc);
4129 continue;
4130 case -2:
4131 return 0;
4132 default:
4133 /* XXX TO CHECK: consume a buffer. */
4134 wbuf = q_next_wbuf(qc);
4135 continue;
4136 }
4137 }
4138 TRACE_LEAVE(QUIC_EV_CONN_PAPKTS, qc->conn);
4139
4140 return 1;
4141}
4142
4143/* QUIC connection packet handler task. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004144struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004145{
4146 struct quic_conn_ctx *ctx = context;
4147
4148 if (ctx->state < QUIC_HS_ST_COMPLETE) {
4149 qc_do_hdshk(ctx);
4150 }
4151 else {
4152 struct quic_conn *qc = ctx->conn->qc;
4153
4154 /* XXX TO DO: may fail!!! XXX */
4155 qc_treat_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_APP], ctx);
4156 qc_prep_phdshk_pkts(qc);
4157 qc_send_ppkts(ctx);
4158 }
4159
Willy Tarreau74163142021-03-13 11:30:19 +01004160 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004161}
4162
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004163/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4164 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004165 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004166static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4167 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004168{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004169 size_t try, done = 0;
4170
4171 if (!conn_ctrl_ready(conn))
4172 return 0;
4173
4174 if (!fd_recv_ready(conn->handle.fd))
4175 return 0;
4176
4177 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004178
4179 /* read the largest possible block. For this, we perform only one call
4180 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004181 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004182 */
4183 while (count > 0) {
4184 try = b_contig_space(buf);
4185 if (!try)
4186 break;
4187
4188 if (try > count)
4189 try = count;
4190
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004191 b_add(buf, try);
4192 done += try;
4193 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194 }
4195
4196 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4197 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4198
4199 leave:
4200 return done;
4201
4202 read0:
4203 conn_sock_read0(conn);
4204 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4205
4206 /* Now a final check for a possible asynchronous low-level error
4207 * report. This can happen when a connection receives a reset
4208 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4209 * we might have come from there by just checking POLL_HUP instead
4210 * of recv()'s return value 0, so we have no way to tell there was
4211 * an error without checking.
4212 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004213 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004214 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4215 goto leave;
4216}
4217
4218
4219/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4220 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4221 * other pending data for example, but this flag is ignored at the moment.
4222 * Only one call to send() is performed, unless the buffer wraps, in which case
4223 * a second call may be performed. The connection's flags are updated with
4224 * whatever special event is detected (error, empty). The caller is responsible
4225 * for taking care of those events and avoiding the call if inappropriate. The
4226 * function does not call the connection's polling update function, so the caller
4227 * is responsible for this. It's up to the caller to update the buffer's contents
4228 * based on the return value.
4229 */
4230static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4231{
4232 ssize_t ret;
4233 size_t try, done;
4234 int send_flag;
4235
4236 if (!conn_ctrl_ready(conn))
4237 return 0;
4238
4239 if (!fd_send_ready(conn->handle.fd))
4240 return 0;
4241
4242 done = 0;
4243 /* send the largest possible block. For this we perform only one call
4244 * to send() unless the buffer wraps and we exactly fill the first hunk,
4245 * in which case we accept to do it once again.
4246 */
4247 while (count) {
4248 try = b_contig_data(buf, done);
4249 if (try > count)
4250 try = count;
4251
4252 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4253 if (try < count || flags & CO_SFL_MSG_MORE)
4254 send_flag |= MSG_MORE;
4255
4256 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4257 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4258 if (ret > 0) {
4259 count -= ret;
4260 done += ret;
4261
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004262 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004263 conn->flags |= CO_FL_WAIT_L4L6;
4264 /* if the system buffer is full, don't insist */
4265 if (ret < try)
4266 break;
4267 }
4268 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4269 /* nothing written, we need to poll for write first */
4270 fd_cant_send(conn->handle.fd);
4271 break;
4272 }
4273 else if (errno != EINTR) {
4274 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4275 break;
4276 }
4277 }
4278 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4279 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4280
4281 if (done > 0) {
4282 /* we count the total bytes sent, and the send rate for 32-byte
4283 * blocks. The reason for the latter is that freq_ctr are
4284 * limited to 4GB and that it's not enough per second.
4285 */
4286 _HA_ATOMIC_ADD(&global.out_bytes, done);
4287 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4288 }
4289 return done;
4290}
4291
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004292/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4293 * event subscriber <es> is not allowed to change from a previous call as long
4294 * as at least one event is still subscribed. The <event_type> must only be a
4295 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4296 */
4297static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4298{
4299 return conn_subscribe(conn, xprt_ctx, event_type, es);
4300}
4301
4302/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4303 * The <es> pointer is not allowed to differ from the one passed to the
4304 * subscribe() call. It always returns zero.
4305 */
4306static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4307{
4308 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4309}
4310
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004311/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4312 * connection with <xprt_ctx> as address of the xprt context.
4313 * Returns 1 if succeeded, 0 if not.
4314 */
4315static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4316{
4317 struct quic_conn_ctx *ctx;
4318
4319 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4320
4321 if (*xprt_ctx)
4322 goto out;
4323
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004324 ctx = pool_alloc(pool_head_quic_conn_ctx);
4325 if (!ctx) {
4326 conn->err_code = CO_ER_SYS_MEMLIM;
4327 goto err;
4328 }
4329
4330 ctx->wait_event.tasklet = tasklet_new();
4331 if (!ctx->wait_event.tasklet) {
4332 conn->err_code = CO_ER_SYS_MEMLIM;
4333 goto err;
4334 }
4335
4336 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4337 ctx->wait_event.tasklet->context = ctx;
4338 ctx->wait_event.events = 0;
4339 ctx->conn = conn;
4340 ctx->subs = NULL;
4341 ctx->xprt_ctx = NULL;
4342
4343 ctx->xprt = xprt_get(XPRT_QUIC);
4344 if (objt_server(conn->target)) {
4345 /* Server */
4346 struct server *srv = __objt_server(conn->target);
4347 unsigned char dcid[QUIC_CID_LEN];
4348 struct quic_conn *quic_conn;
4349 int ssl_err, ipv4;
4350
4351 ssl_err = SSL_ERROR_NONE;
4352 if (RAND_bytes(dcid, sizeof dcid) != 1)
4353 goto err;
4354
4355 conn->qc = new_quic_conn(QUIC_PROTOCOL_VERSION_DRAFT_28);
4356 if (!conn->qc)
4357 goto err;
4358
4359 quic_conn = conn->qc;
4360 quic_conn->conn = conn;
4361 ipv4 = conn->dst->ss_family == AF_INET;
4362 if (!qc_new_conn_init(quic_conn, ipv4, NULL, &srv->cids,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004363 dcid, sizeof dcid, NULL, 0, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004364 goto err;
4365
4366 if (!qc_new_isecs(conn, dcid, sizeof dcid, 0))
4367 goto err;
4368
4369 ctx->state = QUIC_HS_ST_CLIENT_INITIAL;
4370 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4371 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4372 goto err;
4373
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01004374 quic_conn->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004375 /* Copy the initial source connection ID. */
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01004376 quic_cid_cpy(&quic_conn->rx.params.initial_source_connection_id, &quic_conn->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004377 quic_conn->enc_params_len =
4378 quic_transport_params_encode(quic_conn->enc_params,
4379 quic_conn->enc_params + sizeof quic_conn->enc_params,
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01004380 &quic_conn->rx.params, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004381 if (!quic_conn->enc_params_len)
4382 goto err;
4383
4384 SSL_set_quic_transport_params(ctx->ssl, quic_conn->enc_params, quic_conn->enc_params_len);
4385 SSL_set_connect_state(ctx->ssl);
4386 ssl_err = SSL_do_handshake(ctx->ssl);
4387 if (ssl_err != 1) {
4388 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4389 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
4390 TRACE_PROTO("SSL handshake",
4391 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
4392 }
4393 else {
4394 TRACE_DEVEL("SSL handshake error",
4395 QUIC_EV_CONN_HDSHK, ctx->conn, &ctx->state, &ssl_err);
4396 goto err;
4397 }
4398 }
4399 }
4400 else if (objt_listener(conn->target)) {
4401 /* Listener */
4402 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004403 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004404
4405 ctx->state = QUIC_HS_ST_SERVER_INITIAL;
4406
4407 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4408 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4409 goto err;
4410
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004411 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412 SSL_set_accept_state(ctx->ssl);
4413 }
4414
4415 *xprt_ctx = ctx;
4416
4417 /* Leave init state and start handshake */
4418 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004419
4420 out:
4421 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4422
4423 return 0;
4424
4425 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004426 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004427 tasklet_free(ctx->wait_event.tasklet);
4428 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004429 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004430 return -1;
4431}
4432
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004433/* Start the QUIC transport layer */
4434static int qc_xprt_start(struct connection *conn, void *ctx)
4435{
4436 struct quic_conn *qc;
4437 struct quic_conn_ctx *qctx = ctx;
4438
4439 qc = conn->qc;
4440 if (!quic_conn_init_timer(qc)) {
4441 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4442 return 0;
4443 }
4444
4445 tasklet_wakeup(qctx->wait_event.tasklet);
4446 return 1;
4447}
4448
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004449/* transport-layer operations for QUIC connections. */
4450static struct xprt_ops ssl_quic = {
4451 .snd_buf = quic_conn_from_buf,
4452 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004453 .subscribe = quic_conn_subscribe,
4454 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004455 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004456 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004457 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4458 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4459 .name = "QUIC",
4460};
4461
4462__attribute__((constructor))
4463static void __quic_conn_init(void)
4464{
4465 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4466 xprt_register(XPRT_QUIC, &ssl_quic);
4467}
4468
4469__attribute__((destructor))
4470static void __quic_conn_deinit(void)
4471{
4472 BIO_meth_free(ha_quic_meth);
4473}
4474
4475/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4476 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4477 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004478 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004479 */
4480static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4481 struct sockaddr_storage *saddr, qpkt_read_func *func)
4482{
4483 unsigned char *pos;
4484 const unsigned char *end;
4485 struct quic_dgram_ctx dgram_ctx = {
4486 .dcid_node = NULL,
4487 .owner = owner,
4488 };
4489
4490 pos = (unsigned char *)buf;
4491 end = pos + len;
4492
4493 do {
4494 int ret;
4495 struct quic_rx_packet *pkt;
4496
Willy Tarreaue4498932021-03-22 21:13:05 +01004497 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498 if (!pkt)
4499 goto err;
4500
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004501 quic_rx_packet_refinc(pkt);
4502 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4503 if (ret == -1) {
4504 size_t pkt_len;
4505
4506 pkt_len = pkt->len;
4507 free_quic_rx_packet(pkt);
4508 /* If the packet length could not be found, we cannot continue. */
4509 if (!pkt_len)
4510 break;
4511 }
4512 } while (pos < end);
4513
4514 /* Increasing the received bytes counter by the UDP datagram length
4515 * if this datagram could be associated to a connection.
4516 */
4517 if (dgram_ctx.qc)
4518 dgram_ctx.qc->rx.bytes += len;
4519
4520 return pos - (unsigned char *)buf;
4521
4522 err:
4523 return -1;
4524}
4525
4526ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4527 struct sockaddr_storage *saddr)
4528{
4529 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4530}
4531
4532ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4533 struct sockaddr_storage *saddr)
4534{
4535 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4536}
4537
4538/* QUIC I/O handler for connection to local listeners or remove servers
4539 * depending on <listener> boolean value, with <fd> as socket file
4540 * descriptor and <ctx> as context.
4541 */
4542static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4543{
4544 ssize_t ret;
4545 size_t done = 0;
4546 struct buffer *buf = get_trash_chunk();
4547 /* Source address */
4548 struct sockaddr_storage saddr = {0};
4549 socklen_t saddrlen = sizeof saddr;
4550
4551 if (!fd_recv_ready(fd))
4552 return 0;
4553
4554 do {
4555 ret = recvfrom(fd, buf->area, buf->size, 0,
4556 (struct sockaddr *)&saddr, &saddrlen);
4557 if (ret < 0) {
4558 if (errno == EINTR)
4559 continue;
4560 if (errno == EAGAIN)
4561 fd_cant_recv(fd);
4562 goto out;
4563 }
4564 } while (0);
4565
4566 done = buf->data = ret;
4567 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4568
4569 out:
4570 return done;
4571}
4572
4573/* QUIC I/O handler for connections to local listeners with <fd> as socket
4574 * file descriptor.
4575 */
4576void quic_fd_handler(int fd)
4577{
Willy Tarreauf5090652021-04-06 17:23:40 +02004578 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004579 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4580}
4581
4582/* QUIC I/O handler for connections to remote servers with <fd> as socket
4583 * file descriptor.
4584 */
4585void quic_conn_fd_handler(int fd)
4586{
Willy Tarreauf5090652021-04-06 17:23:40 +02004587 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004588 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4589}
4590
4591/*
4592 * Local variables:
4593 * c-indent-level: 8
4594 * c-basic-offset: 8
4595 * End:
4596 */