blob: 64642a0b3f934b5b5da8528fe67aecf55b5464b1 [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;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100137 const struct xprt_ops *xprt;
138 void *xprt_ctx;
139 struct wait_event wait_event;
140 struct wait_event *subs;
141};
142
143DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
144 "quic_conn_ctx_pool", sizeof(struct quic_conn_ctx));
145
146DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
147
148DECLARE_POOL(pool_head_quic_connection_id,
149 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
150
151DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
152
153DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
154
155DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
156
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100157DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
158
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159DECLARE_POOL(pool_head_quic_tx_frm, "quic_tx_frm_pool", sizeof(struct quic_tx_frm));
160
161DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
162
163DECLARE_STATIC_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
164
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100165DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100166
167static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
168 struct quic_enc_level *qel);
169
170int qc_prep_phdshk_pkts(struct quic_conn *qc);
171
172/* Add traces to <buf> depending on <frm> TX frame type. */
173static inline void chunk_tx_frm_appendf(struct buffer *buf,
174 const struct quic_tx_frm *frm)
175{
176 switch (frm->type) {
177 case QUIC_FT_CRYPTO:
178 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
179 (unsigned long long)frm->crypto.offset,
180 (unsigned long long)frm->crypto.len);
181 break;
182 default:
183 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
184 }
185}
186
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100187/* Only for debug purpose */
188struct enc_debug_info {
189 unsigned char *payload;
190 size_t payload_len;
191 unsigned char *aad;
192 size_t aad_len;
193 uint64_t pn;
194};
195
196/* Initializes a enc_debug_info struct (only for debug purpose) */
197static inline void enc_debug_info_init(struct enc_debug_info *edi,
198 unsigned char *payload, size_t payload_len,
199 unsigned char *aad, size_t aad_len, uint64_t pn)
200{
201 edi->payload = payload;
202 edi->payload_len = payload_len;
203 edi->aad = aad;
204 edi->aad_len = aad_len;
205 edi->pn = pn;
206}
207
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100208/* Trace callback for QUIC.
209 * These traces always expect that arg1, if non-null, is of type connection.
210 */
211static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
212 const struct ist where, const struct ist func,
213 const void *a1, const void *a2, const void *a3, const void *a4)
214{
215 const struct connection *conn = a1;
216
217 if (conn) {
218 struct quic_tls_secrets *secs;
219 struct quic_conn *qc;
220
221 qc = conn->qc;
222 chunk_appendf(&trace_buf, " : conn@%p", conn);
223 if ((mask & QUIC_EV_CONN_INIT) && qc) {
224 chunk_appendf(&trace_buf, "\n odcid");
225 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100226 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100227 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100228 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100229 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100230 }
231
232 if (mask & QUIC_EV_CONN_ADDDATA) {
233 const enum ssl_encryption_level_t *level = a2;
234 const size_t *len = a3;
235
236 if (level) {
237 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
238
239 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
240 }
241 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100242 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100243 }
244 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
245 /* Initial read & write secrets. */
246 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
247 const unsigned char *rx_sec = a2;
248 const unsigned char *tx_sec = a3;
249
250 secs = &qc->els[level].tls_ctx.rx;
251 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
252 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
253 if (rx_sec)
254 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
255 quic_tls_keys_hexdump(&trace_buf, secs);
256 }
257 secs = &qc->els[level].tls_ctx.tx;
258 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
259 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
260 if (tx_sec)
261 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
262 quic_tls_keys_hexdump(&trace_buf, secs);
263 }
264 }
265 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
266 const enum ssl_encryption_level_t *level = a2;
267 const unsigned char *secret = a3;
268 const size_t *secret_len = a4;
269
270 if (level) {
271 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
272
273 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
274 if (secret && secret_len)
275 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
276 secs = &qc->els[lvl].tls_ctx.rx;
277 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
278 quic_tls_keys_hexdump(&trace_buf, secs);
279 }
280 }
281
282 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
283 const enum ssl_encryption_level_t *level = a2;
284 const unsigned char *secret = a3;
285 const size_t *secret_len = a4;
286
287 if (level) {
288 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
289
290 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
291 if (secret && secret_len)
292 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
293 secs = &qc->els[lvl].tls_ctx.tx;
294 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
295 quic_tls_keys_hexdump(&trace_buf, secs);
296 }
297
298 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100299
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100300 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100301 const struct quic_tx_packet *pkt = a2;
302 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100303 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304
305 if (qel) {
306 struct quic_pktns *pktns;
307
308 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100309 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
310 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100311 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100312 (unsigned long long)qc->path->cwnd,
313 (unsigned long long)qc->path->prep_in_flight,
314 (unsigned long long)qc->path->in_flight,
315 (unsigned long long)pktns->tx.in_flight,
316 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100317 }
318 if (pkt) {
319 const struct quic_tx_frm *frm;
320 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
321 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
322 list_for_each_entry(frm, &pkt->frms, list)
323 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100324 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
325 }
326
327 if (room) {
328 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
329 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
330 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 }
332 }
333
334 if (mask & QUIC_EV_CONN_HDSHK) {
335 const enum quic_handshake_state *state = a2;
336 const int *err = a3;
337
338 if (state)
339 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
340 if (err)
341 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
342 }
343
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100344 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100345 const struct quic_rx_packet *pkt = a2;
346 const unsigned long *pktlen = a3;
347 const SSL *ssl = a4;
348
349 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100350 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100351 pkt, quic_packet_type_enc_level_char(pkt->type));
352 if (pkt->pnl)
353 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
354 (unsigned long long)pkt->pn);
355 if (pkt->token_len)
356 chunk_appendf(&trace_buf, " toklen=%llu",
357 (unsigned long long)pkt->token_len);
358 if (pkt->aad_len)
359 chunk_appendf(&trace_buf, " aadlen=%llu",
360 (unsigned long long)pkt->aad_len);
361 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
362 pkt->flags, (unsigned long long)pkt->len);
363 }
364 if (pktlen)
365 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
366 if (ssl) {
367 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
368 chunk_appendf(&trace_buf, " el=%c",
369 quic_enc_level_char(ssl_to_quic_enc_level(level)));
370 }
371 }
372
373 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
374 const struct quic_rx_packet *pkt = a2;
375 const struct quic_rx_crypto_frm *cf = a3;
376 const SSL *ssl = a4;
377
378 if (pkt)
379 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
380 quic_packet_type_enc_level_char(pkt->type),
381 (unsigned long long)pkt->pn);
382 if (cf)
383 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
384 (unsigned long long)cf->offset_node.key,
385 (unsigned long long)cf->len);
386 if (ssl) {
387 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
388 chunk_appendf(&trace_buf, " el=%c",
389 quic_enc_level_char(ssl_to_quic_enc_level(level)));
390 }
391 }
392
393 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
394 const struct quic_frame *frm = a2;
395
396 if (frm)
397 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
398 }
399
400 if (mask & QUIC_EV_CONN_PHPKTS) {
401 const struct quic_enc_level *qel = a2;
402
403 if (qel) {
404 struct quic_pktns *pktns;
405
406 pktns = qc->pktns;
407 chunk_appendf(&trace_buf,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100408 " 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 +0100409 quic_enc_level_char_from_qel(qel, qc),
410 !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100411 (unsigned long long)qc->path->cwnd,
412 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100414 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
415 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100416 }
417 }
418
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100419 if (mask & QUIC_EV_CONN_ENCPKT) {
420 const struct enc_debug_info *edi = a2;
421
422 if (edi)
423 chunk_appendf(&trace_buf,
424 " payload=@%p payload_len=%llu"
425 " aad=@%p aad_len=%llu pn=%llu",
426 edi->payload, (unsigned long long)edi->payload_len,
427 edi->aad, (unsigned long long)edi->aad_len,
428 (unsigned long long)edi->pn);
429 }
430
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100431 if (mask & QUIC_EV_CONN_RMHP) {
432 const struct quic_rx_packet *pkt = a2;
433
434 if (pkt) {
435 const int *ret = a3;
436
437 chunk_appendf(&trace_buf, " pkt@%p", pkt);
438 if (ret && *ret)
439 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
440 pkt->pnl, (unsigned long long)pkt->pn);
441 }
442 }
443
444 if (mask & QUIC_EV_CONN_PRSAFRM) {
445 const struct quic_tx_frm *frm = a2;
446 const unsigned long *val1 = a3;
447 const unsigned long *val2 = a4;
448
449 if (frm)
450 chunk_tx_frm_appendf(&trace_buf, frm);
451 if (val1)
452 chunk_appendf(&trace_buf, " %lu", *val1);
453 if (val2)
454 chunk_appendf(&trace_buf, "..%lu", *val2);
455 }
456
457 if (mask & QUIC_EV_CONN_RTTUPDT) {
458 const unsigned int *rtt_sample = a2;
459 const unsigned int *ack_delay = a3;
460 const struct quic_loss *ql = a4;
461
462 if (rtt_sample)
463 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
464 if (ack_delay)
465 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
466 if (ql)
467 chunk_appendf(&trace_buf,
468 " srtt=%ums rttvar=%ums min_rtt=%ums",
469 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
470 }
471 if (mask & QUIC_EV_CONN_CC) {
472 const struct quic_cc_event *ev = a2;
473 const struct quic_cc *cc = a3;
474
475 if (a2)
476 quic_cc_event_trace(&trace_buf, ev);
477 if (a3)
478 quic_cc_state_trace(&trace_buf, cc);
479 }
480
481 if (mask & QUIC_EV_CONN_PKTLOSS) {
482 const struct quic_pktns *pktns = a2;
483 const struct list *lost_pkts = a3;
484 struct quic_conn *qc = conn->qc;
485
486 if (pktns) {
487 chunk_appendf(&trace_buf, " pktns=%s",
488 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
489 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
490 if (pktns->tx.loss_time)
491 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100492 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100493 }
494 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
495 struct quic_tx_packet *pkt;
496
497 chunk_appendf(&trace_buf, " lost_pkts:");
498 list_for_each_entry(pkt, lost_pkts, list)
499 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
500 }
501 }
502
503 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
504 struct quic_conn *qc = conn->qc;
505 const struct quic_pktns *pktns = a2;
506 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100507 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100509 if (ifae_pkts)
510 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
511 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100512 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100513 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100514 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100515 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
516 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100517 if (mask & QUIC_EV_CONN_STIMER) {
518 if (pktns->tx.loss_time)
519 chunk_appendf(&trace_buf, " loss_time=%dms",
520 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
521 }
522 if (mask & QUIC_EV_CONN_SPTO) {
523 if (pktns->tx.time_of_last_eliciting)
524 chunk_appendf(&trace_buf, " tole=%dms",
525 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
526 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100527 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 }
529 }
530
531 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
532 chunk_appendf(&trace_buf,
533 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
534 }
535 }
536
537 if (mask & QUIC_EV_CONN_SPPKTS) {
538 const struct quic_tx_packet *pkt = a2;
539
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100540 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
541 (unsigned long long)qc->path->cwnd,
542 (unsigned long long)qc->path->prep_in_flight,
543 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100544 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100545 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100546 (unsigned long)pkt->pn_node.key,
547 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
548 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100549 (unsigned long long)pkt->in_flight_len,
550 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100551 }
552 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100553
554 if (mask & QUIC_EV_CONN_SSLALERT) {
555 const uint8_t *alert = a2;
556 const enum ssl_encryption_level_t *level = a3;
557
558 if (alert)
559 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
560 if (level)
561 chunk_appendf(&trace_buf, " el=%c",
562 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
563 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100564
565 if (mask & QUIC_EV_CONN_BCFRMS) {
566 const size_t *sz1 = a2;
567 const size_t *sz2 = a3;
568 const size_t *sz3 = a4;
569
570 if (sz1)
571 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
572 if (sz2)
573 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
574 if (sz3)
575 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
576 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100577
578 if (mask & QUIC_EV_CONN_PSTRM) {
579 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100580
581 if (a2) {
582 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100583
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100584 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
585 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
586 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
587 (unsigned long long)s->id,
588 (unsigned long long)s->offset,
589 (unsigned long long)s->len);
590 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100591 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100592 }
593 if (mask & QUIC_EV_CONN_LPKT) {
594 const struct quic_rx_packet *pkt = a2;
595
596 if (conn)
597 chunk_appendf(&trace_buf, " xprt_ctx@%p", conn->xprt_ctx);
598 if (pkt)
599 chunk_appendf(&trace_buf, " type=0x%02x %s",
600 pkt->type, qc_pkt_long(pkt) ? "long" : "short");
601 }
602
603}
604
605/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
606static inline int quic_peer_validated_addr(struct quic_conn_ctx *ctx)
607{
608 struct quic_conn *qc;
609
610 qc = ctx->conn->qc;
611 if (objt_server(qc->conn->target))
612 return 1;
613
614 if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
615 (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200616 (qc->state & QUIC_HS_ST_COMPLETE))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100617 return 1;
618
619 return 0;
620}
621
622/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
623 * both loss detection and PTO and schedule the task assiated to this timer if needed.
624 */
625static inline void qc_set_timer(struct quic_conn_ctx *ctx)
626{
627 struct quic_conn *qc;
628 struct quic_pktns *pktns;
629 unsigned int pto;
630
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100631 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
632 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100633 qc = ctx->conn->qc;
634 pktns = quic_loss_pktns(qc);
635 if (tick_isset(pktns->tx.loss_time)) {
636 qc->timer = pktns->tx.loss_time;
637 goto out;
638 }
639
640 /* XXX TODO: anti-amplification: the timer must be
641 * cancelled for a server which reached the anti-amplification limit.
642 */
643
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100644 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
645 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100646 /* Timer cancellation. */
647 qc->timer = TICK_ETERNITY;
648 goto out;
649 }
650
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +0200651 pktns = quic_pto_pktns(qc, qc->state & QUIC_HS_ST_COMPLETE, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100652 if (tick_isset(pto))
653 qc->timer = pto;
654 out:
655 task_schedule(qc->timer_task, qc->timer);
656 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
657}
658
659#ifndef OPENSSL_IS_BORINGSSL
660int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
661 const uint8_t *read_secret,
662 const uint8_t *write_secret, size_t secret_len)
663{
664 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
665 struct quic_tls_ctx *tls_ctx =
666 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
667 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
668
669 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
670 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
671 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
672 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
673
674 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
675 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
676 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
677 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
678 read_secret, secret_len)) {
679 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
680 return 0;
681 }
682
683 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
684 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
685 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
686 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
687 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
688 write_secret, secret_len)) {
689 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
690 return 0;
691 }
692
693 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
694 if (objt_server(conn->target) && level == ssl_encryption_application) {
695 const unsigned char *buf;
696 size_t buflen;
697
698 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
699 if (!buflen)
700 return 0;
701
702 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
703 return 0;
704 }
705 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
706
707 return 1;
708}
709#else
710/* ->set_read_secret callback to derive the RX secrets at <level> encryption
711 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500712 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100713 */
714int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
715 const SSL_CIPHER *cipher,
716 const uint8_t *secret, size_t secret_len)
717{
718 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
719 struct quic_tls_ctx *tls_ctx =
720 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
721
722 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
723 tls_ctx->rx.aead = tls_aead(cipher);
724 tls_ctx->rx.md = tls_md(cipher);
725 tls_ctx->rx.hp = tls_hp(cipher);
726
727 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
728 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
729 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
730 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
731 secret, secret_len)) {
732 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
733 goto err;
734 }
735
736 if (objt_server(conn->target) && level == ssl_encryption_application) {
737 const unsigned char *buf;
738 size_t buflen;
739
740 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
741 if (!buflen)
742 goto err;
743
744 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
745 goto err;
746 }
747
748 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
749 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
750
751 return 1;
752
753 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100754 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100755 return 0;
756}
757
758/* ->set_write_secret callback to derive the TX secrets at <level>
759 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500760 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100761 */
762int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
763 const SSL_CIPHER *cipher,
764 const uint8_t *secret, size_t secret_len)
765{
766 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
767 struct quic_tls_ctx *tls_ctx =
768 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
769
770 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
771 tls_ctx->tx.aead = tls_aead(cipher);
772 tls_ctx->tx.md = tls_md(cipher);
773 tls_ctx->tx.hp = tls_hp(cipher);
774
775 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
776 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
777 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
778 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
779 secret, secret_len)) {
780 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
781 goto err;
782 }
783
784 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
785 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
786
787 return 1;
788
789 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100790 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100791 return 0;
792}
793#endif
794
795/* This function copies the CRYPTO data provided by the TLS stack found at <data>
796 * with <len> as size in CRYPTO buffers dedicated to store the information about
797 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
798 * It fails only if it could not managed to allocate enough CRYPTO buffers to
799 * store all the data.
800 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
801 */
802static int quic_crypto_data_cpy(struct quic_enc_level *qel,
803 const unsigned char *data, size_t len)
804{
805 struct quic_crypto_buf **qcb;
806 /* The remaining byte to store in CRYPTO buffers. */
807 size_t cf_offset, cf_len, *nb_buf;
808 unsigned char *pos;
809
810 nb_buf = &qel->tx.crypto.nb_buf;
811 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
812 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
813 cf_len = len;
814
815 while (len) {
816 size_t to_copy, room;
817
818 pos = (*qcb)->data + (*qcb)->sz;
819 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
820 to_copy = len > room ? room : len;
821 if (to_copy) {
822 memcpy(pos, data, to_copy);
823 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
824 qel->tx.crypto.sz += to_copy;
825 (*qcb)->sz += to_copy;
826 pos += to_copy;
827 len -= to_copy;
828 data += to_copy;
829 }
830 else {
831 struct quic_crypto_buf **tmp;
832
833 tmp = realloc(qel->tx.crypto.bufs,
834 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
835 if (tmp) {
836 qel->tx.crypto.bufs = tmp;
837 qcb = &qel->tx.crypto.bufs[*nb_buf];
838 *qcb = pool_alloc(pool_head_quic_crypto_buf);
839 if (!*qcb)
840 return 0;
841
842 (*qcb)->sz = 0;
843 ++*nb_buf;
844 }
845 else {
846 break;
847 }
848 }
849 }
850
851 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
852 * have been buffered.
853 */
854 if (!len) {
855 struct quic_tx_frm *frm;
856
857 frm = pool_alloc(pool_head_quic_tx_frm);
858 if (!frm)
859 return 0;
860
861 frm->type = QUIC_FT_CRYPTO;
862 frm->crypto.offset = cf_offset;
863 frm->crypto.len = cf_len;
Willy Tarreau2b718102021-04-21 07:32:39 +0200864 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100865 }
866
867 return len == 0;
868}
869
870
871/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
872 * wants to provide the QUIC layer with CRYPTO data.
873 * Returns 1 if succeeded, 0 if not.
874 */
875int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
876 const uint8_t *data, size_t len)
877{
878 struct connection *conn;
879 enum quic_tls_enc_level tel;
880 struct quic_enc_level *qel;
881
882 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
883 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
884 tel = ssl_to_quic_enc_level(level);
885 qel = &conn->qc->els[tel];
886
887 if (tel == -1) {
888 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
889 goto err;
890 }
891
892 if (!quic_crypto_data_cpy(qel, data, len)) {
893 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
894 goto err;
895 }
896
897 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
898 conn, &level, &len);
899
900 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
901 return 1;
902
903 err:
904 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
905 return 0;
906}
907
908int ha_quic_flush_flight(SSL *ssl)
909{
910 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
911
912 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
913 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
914
915 return 1;
916}
917
918int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
919{
920 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
921
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100922 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100923 return 1;
924}
925
926/* QUIC TLS methods */
927static SSL_QUIC_METHOD ha_quic_method = {
928#ifdef OPENSSL_IS_BORINGSSL
929 .set_read_secret = ha_set_rsec,
930 .set_write_secret = ha_set_wsec,
931#else
932 .set_encryption_secrets = ha_quic_set_encryption_secrets,
933#endif
934 .add_handshake_data = ha_quic_add_handshake_data,
935 .flush_flight = ha_quic_flush_flight,
936 .send_alert = ha_quic_send_alert,
937};
938
939/* Initialize the TLS context of a listener with <bind_conf> as configuration.
940 * Returns an error count.
941 */
942int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
943{
944 struct proxy *curproxy = bind_conf->frontend;
945 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
946 int cfgerr = 0;
947
948#if 0
949 /* XXX Did not manage to use this. */
950 const char *ciphers =
951 "TLS_AES_128_GCM_SHA256:"
952 "TLS_AES_256_GCM_SHA384:"
953 "TLS_CHACHA20_POLY1305_SHA256:"
954 "TLS_AES_128_CCM_SHA256";
955#endif
956 const char *groups = "P-256:X25519:P-384:P-521";
957 long options =
958 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
959 SSL_OP_SINGLE_ECDH_USE |
960 SSL_OP_CIPHER_SERVER_PREFERENCE;
961 SSL_CTX *ctx;
962
963 ctx = SSL_CTX_new(TLS_server_method());
964 bind_conf->initial_ctx = ctx;
965
966 SSL_CTX_set_options(ctx, options);
967#if 0
968 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
969 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
970 "for bind '%s' at [%s:%d].\n",
971 curproxy->id, ciphers,
972 bind_conf->arg, bind_conf->file, bind_conf->line);
973 cfgerr++;
974 }
975#endif
976
977 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
978 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
979 "for bind '%s' at [%s:%d].\n",
980 curproxy->id, groups,
981 bind_conf->arg, bind_conf->file, bind_conf->line);
982 cfgerr++;
983 }
984
985 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
986 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
987 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
988 SSL_CTX_set_default_verify_paths(ctx);
989
990#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
991#ifdef OPENSSL_IS_BORINGSSL
992 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
993 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
994#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
995 if (bind_conf->ssl_conf.early_data) {
996 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
997 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
998 }
999 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1000 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1001#else
1002 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1003#endif
1004 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1005#endif
1006 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1007
1008 return cfgerr;
1009}
1010
1011/* Decode an expected packet number from <truncated_on> its truncated value,
1012 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1013 * the number of bits used to encode this packet number (its length in bytes * 8).
1014 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1015 */
1016static uint64_t decode_packet_number(uint64_t largest_pn,
1017 uint32_t truncated_pn, unsigned int pn_nbits)
1018{
1019 uint64_t expected_pn = largest_pn + 1;
1020 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1021 uint64_t pn_hwin = pn_win / 2;
1022 uint64_t pn_mask = pn_win - 1;
1023 uint64_t candidate_pn;
1024
1025
1026 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1027 /* Note that <pn_win> > <pn_hwin>. */
1028 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1029 candidate_pn + pn_hwin <= expected_pn)
1030 return candidate_pn + pn_win;
1031
1032 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1033 return candidate_pn - pn_win;
1034
1035 return candidate_pn;
1036}
1037
1038/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1039 * cryptographic context.
1040 * <largest_pn> is the largest received packet number and <pn> the address of
1041 * the packet number field for this packet with <byte0> address of its first byte.
1042 * <end> points to one byte past the end of this packet.
1043 * Returns 1 if succeeded, 0 if not.
1044 */
1045static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1046 int64_t largest_pn, unsigned char *pn,
1047 unsigned char *byte0, const unsigned char *end,
1048 struct quic_conn_ctx *ctx)
1049{
1050 int ret, outlen, i, pnlen;
1051 uint64_t packet_number;
1052 uint32_t truncated_pn = 0;
1053 unsigned char mask[5] = {0};
1054 unsigned char *sample;
1055 EVP_CIPHER_CTX *cctx;
1056 unsigned char *hp_key;
1057
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001058 /* Check there is enough data in this packet. */
1059 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1060 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1061 return 0;
1062 }
1063
1064 cctx = EVP_CIPHER_CTX_new();
1065 if (!cctx) {
1066 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1067 return 0;
1068 }
1069
1070 ret = 0;
1071 sample = pn + QUIC_PACKET_PN_MAXLEN;
1072
1073 hp_key = tls_ctx->rx.hp_key;
1074 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1075 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1076 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1077 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1078 goto out;
1079 }
1080
1081 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1082 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1083 for (i = 0; i < pnlen; i++) {
1084 pn[i] ^= mask[i + 1];
1085 truncated_pn = (truncated_pn << 8) | pn[i];
1086 }
1087
1088 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1089 /* Store remaining information for this unprotected header */
1090 pkt->pn = packet_number;
1091 pkt->pnl = pnlen;
1092
1093 ret = 1;
1094
1095 out:
1096 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001097
1098 return ret;
1099}
1100
1101/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1102 * address, with <payload_len> as payload length, <aad> as address of
1103 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1104 * context.
1105 * Returns 1 if succeeded, 0 if not.
1106 */
1107static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1108 unsigned char *aad, size_t aad_len, uint64_t pn,
1109 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1110{
1111 unsigned char iv[12];
1112 unsigned char *tx_iv = tls_ctx->tx.iv;
1113 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001114 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001115
1116 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1117 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001118 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001119 }
1120
1121 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1122 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1123 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001124 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001125 }
1126
1127 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001128
1129 err:
1130 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1131 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1132 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001133}
1134
1135/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1136 * Returns 1 if succeeded, 0 if not.
1137 */
1138static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1139{
1140 int ret;
1141 unsigned char iv[12];
1142 unsigned char *rx_iv = tls_ctx->rx.iv;
1143 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1144
1145 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1146 return 0;
1147
1148 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1149 pkt->data, pkt->aad_len,
1150 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1151 if (!ret)
1152 return 0;
1153
1154 /* Update the packet length (required to parse the frames). */
1155 pkt->len = pkt->aad_len + ret;
1156
1157 return 1;
1158}
1159
1160/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
1161static inline void qc_treat_acked_tx_frm(struct quic_tx_frm *frm,
1162 struct quic_conn_ctx *ctx)
1163{
1164 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001165 LIST_DELETE(&frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001166 pool_free(pool_head_quic_tx_frm, frm);
1167}
1168
1169/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1170 * deallocating them, and their TX frames.
1171 * Returns the last node reached to be used for the next range.
1172 * May be NULL if <largest> node could not be found.
1173 */
1174static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1175 struct list *newly_acked_pkts,
1176 struct eb64_node *largest_node,
1177 uint64_t largest, uint64_t smallest,
1178 struct quic_conn_ctx *ctx)
1179{
1180 struct eb64_node *node;
1181 struct quic_tx_packet *pkt;
1182
1183 if (largest_node)
1184 node = largest_node;
1185 else {
1186 node = eb64_lookup(pkts, largest);
1187 while (!node && largest > smallest) {
1188 node = eb64_lookup(pkts, --largest);
1189 }
1190 }
1191
1192 while (node && node->key >= smallest) {
1193 struct quic_tx_frm *frm, *frmbak;
1194
1195 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1196 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001197 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001198 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1199 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1200 qc_treat_acked_tx_frm(frm, ctx);
1201 node = eb64_prev(node);
1202 eb64_delete(&pkt->pn_node);
1203 }
1204
1205 return node;
1206}
1207
1208/* Treat <frm> frame whose packet it is attached to has just been detected as non
1209 * acknowledged.
1210 */
1211static inline void qc_treat_nacked_tx_frm(struct quic_tx_frm *frm,
1212 struct quic_pktns *pktns,
1213 struct quic_conn_ctx *ctx)
1214{
1215 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001216 LIST_DELETE(&frm->list);
1217 LIST_INSERT(&pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001218}
1219
1220
1221/* Free the TX packets of <pkts> list */
1222static inline void free_quic_tx_pkts(struct list *pkts)
1223{
1224 struct quic_tx_packet *pkt, *tmp;
1225
1226 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001227 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001228 eb64_delete(&pkt->pn_node);
1229 pool_free(pool_head_quic_tx_packet, pkt);
1230 }
1231}
1232
1233/* Send a packet loss event nofification to the congestion controller
1234 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1235 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1236 * at <now_us> current time.
1237 * Always succeeds.
1238 */
1239static inline void qc_cc_loss_event(struct quic_conn *qc,
1240 unsigned int lost_bytes,
1241 unsigned int newest_time_sent,
1242 unsigned int period,
1243 unsigned int now_us)
1244{
1245 struct quic_cc_event ev = {
1246 .type = QUIC_CC_EVT_LOSS,
1247 .loss.now_ms = now_ms,
1248 .loss.max_ack_delay = qc->max_ack_delay,
1249 .loss.lost_bytes = lost_bytes,
1250 .loss.newest_time_sent = newest_time_sent,
1251 .loss.period = period,
1252 };
1253
1254 quic_cc_event(&qc->path->cc, &ev);
1255}
1256
1257/* Send a packet ack event nofication for each newly acked packet of
1258 * <newly_acked_pkts> list and free them.
1259 * Always succeeds.
1260 */
1261static inline void qc_treat_newly_acked_pkts(struct quic_conn_ctx *ctx,
1262 struct list *newly_acked_pkts)
1263{
1264 struct quic_conn *qc = ctx->conn->qc;
1265 struct quic_tx_packet *pkt, *tmp;
1266 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1267
1268 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1269 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001270 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001271 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001272 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001273 ev.ack.acked = pkt->in_flight_len;
1274 ev.ack.time_sent = pkt->time_sent;
1275 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001276 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001277 eb64_delete(&pkt->pn_node);
1278 pool_free(pool_head_quic_tx_packet, pkt);
1279 }
1280
1281}
1282
1283/* Handle <pkts> list of lost packets detected at <now_us> handling
1284 * their TX frames.
1285 * Send a packet loss event to the congestion controller if
1286 * in flight packet have been lost.
1287 * Also frees the packet in <pkts> list.
1288 * Never fails.
1289 */
1290static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
1291 struct quic_conn_ctx *ctx,
1292 struct list *pkts,
1293 uint64_t now_us)
1294{
1295 struct quic_conn *qc = ctx->conn->qc;
1296 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
1297 struct quic_tx_frm *frm, *frmbak;
1298 uint64_t lost_bytes;
1299
1300 lost_bytes = 0;
1301 oldest_lost = newest_lost = NULL;
1302 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1303 lost_bytes += pkt->in_flight_len;
1304 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001305 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001306 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001307 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308 /* Treat the frames of this lost packet. */
1309 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1310 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001311 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001312 if (!oldest_lost) {
1313 oldest_lost = newest_lost = pkt;
1314 }
1315 else {
1316 if (newest_lost != oldest_lost)
1317 pool_free(pool_head_quic_tx_packet, newest_lost);
1318 newest_lost = pkt;
1319 }
1320 }
1321
1322 if (lost_bytes) {
1323 /* Sent a packet loss event to the congestion controller. */
1324 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1325 newest_lost->time_sent - oldest_lost->time_sent, now_us);
1326 pool_free(pool_head_quic_tx_packet, oldest_lost);
1327 if (newest_lost != oldest_lost)
1328 pool_free(pool_head_quic_tx_packet, newest_lost);
1329 }
1330}
1331
1332/* Look for packet loss from sent packets for <qel> encryption level of a
1333 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1334 * their tree if deemed as lost or set the <loss_time> value the packet number
1335 * space if any not deemed lost.
1336 * Should be called after having received an ACK frame with newly acknowledged
1337 * packets or when the the loss detection timer has expired.
1338 * Always succeeds.
1339 */
1340static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1341 struct quic_conn *qc,
1342 struct list *lost_pkts)
1343{
1344 struct eb_root *pkts;
1345 struct eb64_node *node;
1346 struct quic_loss *ql;
1347 unsigned int loss_delay;
1348
1349 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1350 pkts = &pktns->tx.pkts;
1351 pktns->tx.loss_time = TICK_ETERNITY;
1352 if (eb_is_empty(pkts))
1353 goto out;
1354
1355 ql = &qc->path->loss;
1356 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1357 loss_delay += loss_delay >> 3;
1358 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1359
1360 node = eb64_first(pkts);
1361 while (node) {
1362 struct quic_tx_packet *pkt;
1363 int64_t largest_acked_pn;
1364 unsigned int loss_time_limit, time_sent;
1365
1366 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1367 largest_acked_pn = pktns->tx.largest_acked_pn;
1368 node = eb64_next(node);
1369 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1370 break;
1371
1372 time_sent = pkt->time_sent;
1373 loss_time_limit = tick_add(time_sent, loss_delay);
1374 if (tick_is_le(time_sent, now_ms) ||
1375 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1376 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001377 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001378 }
1379 else {
1380 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1381 }
1382 }
1383
1384 out:
1385 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1386}
1387
1388/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1389 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001390 * 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 +01001391 * acked ack-eliciting packet.
1392 * Return 1, if succeeded, 0 if not.
1393 */
1394static inline int qc_parse_ack_frm(struct quic_frame *frm, struct quic_conn_ctx *ctx,
1395 struct quic_enc_level *qel,
1396 unsigned int *rtt_sample,
1397 const unsigned char **pos, const unsigned char *end)
1398{
1399 struct quic_ack *ack = &frm->ack;
1400 uint64_t smallest, largest;
1401 struct eb_root *pkts;
1402 struct eb64_node *largest_node;
1403 unsigned int time_sent, pkt_flags;
1404 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1405 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1406
1407 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1408 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1409 ctx->conn,, &ack->largest_ack);
1410 goto err;
1411 }
1412
1413 if (ack->first_ack_range > ack->largest_ack) {
1414 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1415 ctx->conn,, &ack->first_ack_range);
1416 goto err;
1417 }
1418
1419 largest = ack->largest_ack;
1420 smallest = largest - ack->first_ack_range;
1421 pkts = &qel->pktns->tx.pkts;
1422 pkt_flags = 0;
1423 largest_node = NULL;
1424 time_sent = 0;
1425
1426 if ((int64_t)ack->largest_ack > qel->pktns->tx.largest_acked_pn) {
1427 largest_node = eb64_lookup(pkts, largest);
1428 if (!largest_node) {
1429 TRACE_DEVEL("Largest acked packet not found",
1430 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1431 goto err;
1432 }
1433
1434 time_sent = eb64_entry(&largest_node->node,
1435 struct quic_tx_packet, pn_node)->time_sent;
1436 }
1437
1438 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1439 ctx->conn,, &largest, &smallest);
1440 do {
1441 uint64_t gap, ack_range;
1442
1443 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1444 largest_node, largest, smallest, ctx);
1445 if (!ack->ack_range_num--)
1446 break;
1447
1448 if (!quic_dec_int(&gap, pos, end))
1449 goto err;
1450
1451 if (smallest < gap + 2) {
1452 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1453 ctx->conn,, &gap, &smallest);
1454 goto err;
1455 }
1456
1457 largest = smallest - gap - 2;
1458 if (!quic_dec_int(&ack_range, pos, end))
1459 goto err;
1460
1461 if (largest < ack_range) {
1462 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1463 ctx->conn,, &largest, &ack_range);
1464 goto err;
1465 }
1466
1467 /* Do not use this node anymore. */
1468 largest_node = NULL;
1469 /* Next range */
1470 smallest = largest - ack_range;
1471
1472 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1473 ctx->conn,, &largest, &smallest);
1474 } while (1);
1475
1476 /* Flag this packet number space as having received an ACK. */
1477 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED;
1478
1479 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1480 *rtt_sample = tick_remain(time_sent, now_ms);
1481 qel->pktns->tx.largest_acked_pn = ack->largest_ack;
1482 }
1483
1484 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1485 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1486 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1487 if (!LIST_ISEMPTY(&lost_pkts))
1488 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1489 }
1490 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1491 if (quic_peer_validated_addr(ctx))
1492 ctx->conn->qc->path->loss.pto_count = 0;
1493 qc_set_timer(ctx);
1494 }
1495
1496
1497 return 1;
1498
1499 err:
1500 free_quic_tx_pkts(&newly_acked_pkts);
1501 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1502 return 0;
1503}
1504
1505/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1506 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001507 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001508 * Return 1 if succeeded, 0 if not.
1509 */
1510static inline int qc_provide_cdata(struct quic_enc_level *el,
1511 struct quic_conn_ctx *ctx,
1512 const unsigned char *data, size_t len,
1513 struct quic_rx_packet *pkt,
1514 struct quic_rx_crypto_frm *cf)
1515{
1516 int ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001517 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001518
1519 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1520 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001521 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001522 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1523 TRACE_PROTO("SSL_provide_quic_data() error",
1524 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1525 goto err;
1526 }
1527
1528 el->rx.crypto.offset += len;
1529 TRACE_PROTO("in order CRYPTO data",
1530 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1531
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001532 if (qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001533 ssl_err = SSL_do_handshake(ctx->ssl);
1534 if (ssl_err != 1) {
1535 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1536 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1537 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001538 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001539 goto out;
1540 }
1541
1542 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001543 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001544 goto err;
1545 }
1546
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001547 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001548 if (objt_listener(ctx->conn->target))
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001549 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001550 else
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001551 qc->state = QUIC_HS_ST_COMPLETE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001552 } else {
1553 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1554 if (ssl_err != 1) {
1555 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1556 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1557 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001558 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001559 goto out;
1560 }
1561
1562 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001563 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001564 goto err;
1565 }
1566
1567 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001568 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569 }
1570
1571 out:
1572 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1573 return 1;
1574
1575 err:
1576 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1577 return 0;
1578}
1579
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001580/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1581 * <pkt> RX packet.
1582 * Return it if succeeded, NULL if not.
1583 */
1584static inline
1585struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1586 struct quic_rx_packet *pkt)
1587{
1588 struct quic_rx_strm_frm *frm;
1589
1590 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1591 if (frm) {
1592 frm->offset_node.key = stream_frm->offset;
1593 frm->len = stream_frm->len;
1594 frm->data = stream_frm->data;
1595 frm->pkt = pkt;
1596 }
1597
1598 return frm;
1599}
1600
1601/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1602 * several streams, depending on the already open onces.
1603 * Return this node if succeeded, NULL if not.
1604 */
1605static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1606{
1607 unsigned int strm_type;
1608 int64_t sub_id;
1609 struct eb64_node *strm_node;
1610
1611 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1612
1613 strm_type = id & QCS_ID_TYPE_MASK;
1614 sub_id = id >> QCS_ID_TYPE_SHIFT;
1615 strm_node = NULL;
1616 if (qc_local_stream_id(qcc, id)) {
1617 /* Local streams: this stream must be already opened. */
1618 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1619 if (!strm_node) {
1620 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1621 goto out;
1622 }
1623 }
1624 else {
1625 /* Remote streams. */
1626 struct eb_root *strms;
1627 uint64_t largest_id;
1628 enum qcs_type qcs_type;
1629
1630 strms = &qcc->streams_by_id;
1631 qcs_type = qcs_id_type(id);
1632 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1633 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1634 goto out;
1635 }
1636
1637 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1638 * correct value.
1639 */
1640 largest_id = qcc->strms[qcs_type].largest_id;
1641 if (sub_id > (int64_t)largest_id) {
1642 /* RFC: "A stream ID that is used out of order results in all streams
1643 * of that type with lower-numbered stream IDs also being opened".
1644 * So, let's "open" these streams.
1645 */
1646 int64_t i;
1647 struct qcs *qcs;
1648
1649 qcs = NULL;
1650 for (i = largest_id + 1; i <= sub_id; i++) {
1651 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1652 if (!qcs) {
1653 TRACE_PROTO("Could not allocate a new stream",
1654 QUIC_EV_CONN_PSTRM, qcc->conn);
1655 goto out;
1656 }
1657
1658 qcc->strms[qcs_type].largest_id = i;
1659 }
1660 if (qcs)
1661 strm_node = &qcs->by_id;
1662 }
1663 else {
1664 strm_node = eb64_lookup(strms, id);
1665 }
1666 }
1667
1668 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1669 return strm_node;
1670
1671 out:
1672 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1673 return NULL;
1674}
1675
1676/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1677 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1678 * to reflect the data which have been consumed.
1679 */
1680static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1681{
1682 size_t ret;
1683
1684 ret = 0;
1685 while (strm_frm->len) {
1686 size_t try;
1687
1688 try = b_contig_space(buf);
1689 if (!try)
1690 break;
1691
1692 if (try > strm_frm->len)
1693 try = strm_frm->len;
1694 memcpy(b_tail(buf), strm_frm->data, try);
1695 strm_frm->len -= try;
1696 strm_frm->offset += try;
1697 b_add(buf, try);
1698 ret += try;
1699 }
1700
1701 return ret;
1702}
1703
1704/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1705 * streams may be open. The data are copied to the stream RX buffer if possible.
1706 * If not, the STREAM frame is stored to be treated again later.
1707 * We rely on the flow control so that not to store too much STREAM frames.
1708 * Return 1 if succeeded, 0 if not.
1709 */
1710static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1711 struct quic_stream *strm_frm,
1712 struct quic_conn *qc)
1713{
1714 struct qcs *strm;
1715 struct eb64_node *strm_node, *frm_node;
1716 struct quic_rx_strm_frm *frm;
1717
1718 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1719 if (!strm_node) {
1720 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1721 return 0;
1722 }
1723
1724 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1725 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1726 /* FIXME: handle the case where this frame overlap others */
1727 if (frm_node) {
1728 TRACE_PROTO("Already existing stream data",
1729 QUIC_EV_CONN_PSTRM, qc->conn);
1730 goto out;
1731 }
1732
1733 if (strm_frm->offset == strm->rx.offset) {
1734 int ret;
1735
1736 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1737 goto store_frm;
1738
1739 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1740 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1741 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1742 return 0;
1743 }
1744
1745 strm->rx.offset += ret;
1746 }
1747
1748 if (!strm_frm->len)
1749 goto out;
1750
1751 store_frm:
1752 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1753 if (!frm) {
1754 TRACE_PROTO("Could not alloc RX STREAM frame",
1755 QUIC_EV_CONN_PSTRM, qc->conn);
1756 return 0;
1757 }
1758
1759 eb64_insert(&strm->frms, &frm->offset_node);
1760 quic_rx_packet_refinc(pkt);
1761
1762 out:
1763 return 1;
1764}
1765
1766/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1767 * streams may be open. The data are copied to the stream RX buffer if possible.
1768 * If not, the STREAM frame is stored to be treated again later.
1769 * We rely on the flow control so that not to store too much STREAM frames.
1770 * Return 1 if succeeded, 0 if not.
1771 */
1772static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1773 struct quic_stream *strm_frm,
1774 struct quic_conn *qc)
1775{
1776 struct qcs *strm;
1777 struct eb64_node *strm_node, *frm_node;
1778 struct quic_rx_strm_frm *frm;
1779 size_t strm_frm_len;
1780
1781 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1782 if (!strm_node) {
1783 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1784 return 0;
1785 }
1786
1787 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1788 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1789 /* FIXME: handle the case where this frame overlap others */
1790 if (frm_node) {
1791 TRACE_PROTO("Already existing stream data",
1792 QUIC_EV_CONN_PSTRM, qc->conn);
1793 goto out;
1794 }
1795
1796 strm_frm_len = strm_frm->len;
1797 if (strm_frm->offset == strm->rx.offset) {
1798 int ret;
1799
1800 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1801 goto store_frm;
1802
1803 /* qc_strm_cpy() will modify the offset, depending on the number
1804 * of bytes copied.
1805 */
1806 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1807 /* Inform the application of the arrival of this new stream */
1808 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1809 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1810 return 0;
1811 }
1812
1813 if (ret)
1814 ruqs_notify_recv(strm);
1815
1816 strm_frm->offset += ret;
1817 }
1818 /* Take this frame into an account for the stream flow control */
1819 strm->rx.offset += strm_frm_len;
1820 /* It all the data were provided to the application, there is no need to
1821 * store any more inforamtion for it.
1822 */
1823 if (!strm_frm->len)
1824 goto out;
1825
1826 store_frm:
1827 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1828 if (!frm) {
1829 TRACE_PROTO("Could not alloc RX STREAM frame",
1830 QUIC_EV_CONN_PSTRM, qc->conn);
1831 return 0;
1832 }
1833
1834 eb64_insert(&strm->frms, &frm->offset_node);
1835 quic_rx_packet_refinc(pkt);
1836
1837 out:
1838 return 1;
1839}
1840
1841static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1842 struct quic_stream *strm_frm,
1843 struct quic_conn *qc)
1844{
1845 if (strm_frm->id & QCS_ID_DIR_BIT)
1846 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1847 else
1848 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1849}
1850
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1852 * as I/O handler context and <qel> as encryption level.
1853 * Returns 1 if succeeded, 0 if failed.
1854 */
1855static int qc_parse_pkt_frms(struct quic_rx_packet *pkt, struct quic_conn_ctx *ctx,
1856 struct quic_enc_level *qel)
1857{
1858 struct quic_frame frm;
1859 const unsigned char *pos, *end;
1860 struct quic_conn *conn = ctx->conn->qc;
1861
1862 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1863 /* Skip the AAD */
1864 pos = pkt->data + pkt->aad_len;
1865 end = pkt->data + pkt->len;
1866
1867 while (pos < end) {
1868 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1869 goto err;
1870
1871 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001872 case QUIC_FT_PADDING:
1873 if (pos != end) {
1874 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1875 goto err;
1876 }
1877 break;
1878 case QUIC_FT_PING:
1879 break;
1880 case QUIC_FT_ACK:
1881 {
1882 unsigned int rtt_sample;
1883
1884 rtt_sample = 0;
1885 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1886 goto err;
1887
1888 if (rtt_sample) {
1889 unsigned int ack_delay;
1890
1891 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1892 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1893 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1894 }
1895 tasklet_wakeup(ctx->wait_event.tasklet);
1896 break;
1897 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898 case QUIC_FT_CRYPTO:
1899 if (frm.crypto.offset != qel->rx.crypto.offset) {
1900 struct quic_rx_crypto_frm *cf;
1901
1902 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1903 if (!cf) {
1904 TRACE_DEVEL("CRYPTO frame allocation failed",
1905 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1906 goto err;
1907 }
1908
1909 cf->offset_node.key = frm.crypto.offset;
1910 cf->len = frm.crypto.len;
1911 cf->data = frm.crypto.data;
1912 cf->pkt = pkt;
1913 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1914 quic_rx_packet_refinc(pkt);
1915 }
1916 else {
1917 /* XXX TO DO: <cf> is used only for the traces. */
1918 struct quic_rx_crypto_frm cf = { };
1919
1920 cf.offset_node.key = frm.crypto.offset;
1921 cf.len = frm.crypto.len;
1922 if (!qc_provide_cdata(qel, ctx,
1923 frm.crypto.data, frm.crypto.len,
1924 pkt, &cf))
1925 goto err;
1926 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001927 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001928 case QUIC_FT_STREAM_8:
1929 case QUIC_FT_STREAM_9:
1930 case QUIC_FT_STREAM_A:
1931 case QUIC_FT_STREAM_B:
1932 case QUIC_FT_STREAM_C:
1933 case QUIC_FT_STREAM_D:
1934 case QUIC_FT_STREAM_E:
1935 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001936 {
1937 struct quic_stream *stream = &frm.stream;
1938
1939 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1940 if (objt_listener(ctx->conn->target)) {
1941 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1942 goto err;
1943 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1944 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001945
1946 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1947 goto err;
1948
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001949 break;
1950 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001951 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001952 break;
1953 case QUIC_FT_CONNECTION_CLOSE:
1954 case QUIC_FT_CONNECTION_CLOSE_APP:
1955 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001956 case QUIC_FT_HANDSHAKE_DONE:
1957 if (objt_listener(ctx->conn->target))
1958 goto err;
1959
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001960 conn->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001961 break;
1962 default:
1963 goto err;
1964 }
1965 }
1966
1967 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1968 * has successfully parse a Handshake packet. The Initial encryption must also
1969 * be discarded.
1970 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001971 if (conn->state == QUIC_HS_ST_SERVER_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001972 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1973 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1974 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1975 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001976 conn->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001977 }
1978
1979 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1980 return 1;
1981
1982 err:
1983 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1984 return 0;
1985}
1986
1987/* Prepare as much as possible handshake packets for the QUIC connection
1988 * with <ctx> as I/O handler context.
1989 * Returns 1 if succeeded, or 0 if something wrong happened.
1990 */
1991static int qc_prep_hdshk_pkts(struct quic_conn_ctx *ctx)
1992{
1993 struct quic_conn *qc;
1994 enum quic_tls_enc_level tel, next_tel;
1995 struct quic_enc_level *qel;
1996 struct q_buf *wbuf;
1997 /* A boolean to flag <wbuf> as reusable, even if not empty. */
1998 int reuse_wbuf;
1999
2000 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2001 qc = ctx->conn->qc;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002002 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002003 TRACE_DEVEL("unknown enc. levels",
2004 QUIC_EV_CONN_PHPKTS, ctx->conn);
2005 goto err;
2006 }
2007
2008 reuse_wbuf = 0;
2009 wbuf = q_wbuf(qc);
2010 qel = &qc->els[tel];
Ilya Shipitsin01881082021-08-07 14:41:56 +05002011 /* When entering this function, the writer buffer must be empty.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002012 * Most of the time it points to the reader buffer.
2013 */
2014 while ((q_buf_empty(wbuf) || reuse_wbuf)) {
2015 ssize_t ret;
2016 enum quic_pkt_type pkt_type;
2017
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002018 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002019 /* Do not build any more packet f the TX secrets are not available or
2020 * f there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002021 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002022 * and if there is no more CRYPTO data available or in flight
2023 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002024 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002025 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2026 (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002027 !qc->tx.nb_pto_dgrams &&
2028 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002029 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002030 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
2031 /* Consume the buffer if we were supposed to reuse it. */
2032 if (reuse_wbuf)
2033 wbuf = q_next_wbuf(qc);
2034 break;
2035 }
2036
2037 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002038 ret = qc_build_hdshk_pkt(wbuf, qc, pkt_type, qel);
2039 switch (ret) {
2040 case -2:
2041 goto err;
2042 case -1:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002043 if (!reuse_wbuf)
2044 goto out;
2045
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002046 /* Not enough room in <wbuf>. */
2047 wbuf = q_next_wbuf(qc);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002048 reuse_wbuf = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002049 continue;
2050 case 0:
2051 goto out;
2052 default:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002053 reuse_wbuf = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002054 /* Discard the Initial encryption keys as soon as
2055 * a handshake packet could be built.
2056 */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002057 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002058 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2059 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2060 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2061 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002062 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002063 }
2064 /* Special case for Initial packets: when they have all
2065 * been sent, select the next level.
2066 */
2067 if ((LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight) &&
2068 tel == QUIC_TLS_ENC_LEVEL_INITIAL) {
2069 tel = next_tel;
2070 qel = &qc->els[tel];
2071 if (LIST_ISEMPTY(&qel->pktns->tx.frms)) {
2072 /* If there is no more data for the next level, let's
2073 * consume a buffer. This is the case for a client
2074 * which sends only one Initial packet, then wait
2075 * for additional CRYPTO data from the server to enter the
2076 * next level.
2077 */
2078 wbuf = q_next_wbuf(qc);
2079 }
2080 else {
2081 /* Let's try to reuse this buffer. */
2082 reuse_wbuf = 1;
2083 }
2084 }
2085 else {
2086 wbuf = q_next_wbuf(qc);
2087 }
2088 }
2089 }
2090
2091 out:
2092 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
2093 return 1;
2094
2095 err:
2096 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
2097 return 0;
2098}
2099
2100/* Send the QUIC packets which have been prepared for QUIC connections
2101 * with <ctx> as I/O handler context.
2102 */
2103int qc_send_ppkts(struct quic_conn_ctx *ctx)
2104{
2105 struct quic_conn *qc;
2106 struct buffer tmpbuf = { };
2107 struct q_buf *rbuf;
2108
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002109 qc = ctx->conn->qc;
2110 for (rbuf = q_rbuf(qc); !q_buf_empty(rbuf) ; rbuf = q_next_rbuf(qc)) {
2111 struct quic_tx_packet *p, *q;
2112 unsigned int time_sent;
2113
2114 tmpbuf.area = (char *)rbuf->area;
2115 tmpbuf.size = tmpbuf.data = rbuf->data;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002116 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002117 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
2118 &tmpbuf, tmpbuf.data, 0) <= 0)
2119 break;
2120
2121 qc->tx.bytes += tmpbuf.data;
2122 time_sent = now_ms;
2123 /* Reset this buffer to make it available for the next packet to prepare. */
2124 q_buf_reset(rbuf);
2125 /* Remove from <rbuf> the packets which have just been sent. */
2126 list_for_each_entry_safe(p, q, &rbuf->pkts, list) {
2127 p->time_sent = time_sent;
2128 if (p->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2129 p->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002130 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002131 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002132 qc->path->in_flight += p->in_flight_len;
2133 p->pktns->tx.in_flight += p->in_flight_len;
2134 if (p->in_flight_len)
2135 qc_set_timer(ctx);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002136 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, p);
Willy Tarreau2b718102021-04-21 07:32:39 +02002137 LIST_DELETE(&p->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002138 }
2139 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002140
2141 return 1;
2142}
2143
2144/* Build all the frames which must be sent just after the handshake have succeeded.
2145 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2146 * a HANDSHAKE_DONE frame.
2147 * Return 1 if succeeded, 0 if not.
2148 */
2149static int quic_build_post_handshake_frames(struct quic_conn *conn)
2150{
2151 int i;
2152 struct quic_frame *frm;
2153
2154 /* Only servers must send a HANDSHAKE_DONE frame. */
2155 if (!objt_server(conn->conn->target)) {
2156 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002157 if (!frm)
2158 return 0;
2159
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002160 frm->type = QUIC_FT_HANDSHAKE_DONE;
Willy Tarreau2b718102021-04-21 07:32:39 +02002161 LIST_APPEND(&conn->tx.frms_to_send, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002162 }
2163
Frédéric Lécaille5aa41432021-01-28 16:22:52 +01002164 for (i = 1; i < conn->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002165 struct quic_connection_id *cid;
2166
2167 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002168 cid = new_quic_cid(&conn->cids, i);
2169 if (!frm || !cid)
2170 goto err;
2171
2172 quic_connection_id_to_frm_cpy(frm, cid);
Willy Tarreau2b718102021-04-21 07:32:39 +02002173 LIST_APPEND(&conn->tx.frms_to_send, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002174 }
2175
2176 return 1;
2177
2178 err:
2179 free_quic_conn_cids(conn);
2180 return 0;
2181}
2182
2183/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002184void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002185{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002186 struct eb64_node *n;
2187 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002188
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002189 n = eb64_first(&arngs->root);
2190 while (n) {
2191 struct eb64_node *next;
2192
2193 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2194 next = eb64_next(n);
2195 eb64_delete(n);
2196 free(ar);
2197 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002198 }
2199}
2200
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002201/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2202 * descending order.
2203 */
2204static inline size_t sack_gap(struct quic_arng_node *p,
2205 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002206{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002207 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002208}
2209
2210
2211/* Remove the last elements of <ack_ranges> list of ack range updating its
2212 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002213 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002214 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002215static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002216{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002217 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002218
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002219 last = eb64_last(&arngs->root);
2220 while (last && arngs->enc_sz > limit) {
2221 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002222
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002223 prev = eb64_prev(last);
2224 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002225 return 0;
2226
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002227 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2228 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2229 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2230 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2231 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2232 --arngs->sz;
2233 eb64_delete(last);
2234 pool_free(pool_head_quic_arng, last);
2235 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002236 }
2237
2238 return 1;
2239}
2240
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002241/* Set the encoded size of <arngs> QUIC ack ranges. */
2242static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2243{
2244 struct eb64_node *node, *next;
2245 struct quic_arng_node *ar, *ar_next;
2246
2247 node = eb64_last(&arngs->root);
2248 if (!node)
2249 return;
2250
2251 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2252 arngs->enc_sz = quic_int_getsize(ar->last) +
2253 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2254
2255 while ((next = eb64_prev(node))) {
2256 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2257 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2258 quic_int_getsize(ar_next->last - ar_next->first.key);
2259 node = next;
2260 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2261 }
2262}
2263
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002264/* Insert <ar> ack range into <argns> tree of ack ranges.
2265 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002266 */
2267static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002268struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2269 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002270{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002271 struct quic_arng_node *new_ar;
2272
2273 new_ar = pool_alloc(pool_head_quic_arng);
2274 if (new_ar) {
2275 new_ar->first.key = ar->first;
2276 new_ar->last = ar->last;
2277 eb64_insert(&arngs->root, &new_ar->first);
2278 arngs->sz++;
2279 }
2280
2281 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002282}
2283
2284/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002285 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002286 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002287 *
2288 * Descending order
2289 * ------------->
2290 * range1 range2
2291 * ..........|--------|..............|--------|
2292 * ^ ^ ^ ^
2293 * | | | |
2294 * last1 first1 last2 first2
2295 * ..........+--------+--------------+--------+......
2296 * diff1 gap12 diff2
2297 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002298 * To encode the previous list of ranges we must encode integers as follows in
2299 * descending order:
2300 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002301 * with diff1 = last1 - first1
2302 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002303 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002304 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002305 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002306int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2307 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002308{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002309 struct eb64_node *le;
2310 struct quic_arng_node *new_node;
2311 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002312
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002313 new = NULL;
2314 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002315 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002316 if (!new_node)
2317 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002319 goto out;
2320 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002321
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002322 le = eb64_lookup_le(&arngs->root, ar->first);
2323 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002324 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002325 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002326 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002327 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002328 else {
2329 struct quic_arng_node *le_ar =
2330 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002331
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002332 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002333 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002334 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002335
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002336 if (le_ar->last + 1 >= ar->first) {
2337 le_ar->last = ar->last;
2338 new = le;
2339 new_node = le_ar;
2340 }
2341 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002342 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002343 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002344 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002345
2346 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002347 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002348 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002349
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002350 /* Verify that the new inserted node does not overlap the nodes
2351 * which follow it.
2352 */
2353 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002354 struct eb64_node *next;
2355 struct quic_arng_node *next_node;
2356
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002357 while ((next = eb64_next(new))) {
2358 next_node =
2359 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002360 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002361 break;
2362
2363 if (next_node->last > new_node->last)
2364 new_node->last = next_node->last;
2365 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002366 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002367 /* Decrement the size of these ranges. */
2368 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002369 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002370 }
2371
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002372 quic_arngs_set_enc_sz(arngs);
2373
2374 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002375 return 1;
2376}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002377/* Remove the header protection of packets at <el> encryption level.
2378 * Always succeeds.
2379 */
2380static inline void qc_rm_hp_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx)
2381{
2382 struct quic_tls_ctx *tls_ctx;
2383 struct quic_rx_packet *pqpkt, *qqpkt;
2384 struct quic_enc_level *app_qel;
2385
2386 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2387 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2388 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002389 if (el == app_qel && objt_listener(ctx->conn->target) &&
2390 ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002391 TRACE_PROTO("hp not removed (handshake not completed)",
2392 QUIC_EV_CONN_ELRMHP, ctx->conn);
2393 goto out;
2394 }
2395 tls_ctx = &el->tls_ctx;
2396 list_for_each_entry_safe(pqpkt, qqpkt, &el->rx.pqpkts, list) {
2397 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2398 pqpkt->data + pqpkt->pn_offset,
2399 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2400 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2401 /* XXX TO DO XXX */
2402 }
2403 else {
2404 /* The AAD includes the packet number field */
2405 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2406 /* Store the packet into the tree of packets to decrypt. */
2407 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002408 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002409 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002410 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002411 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2412 }
2413 quic_rx_packet_list_del(pqpkt);
2414 }
2415
2416 out:
2417 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2418}
2419
2420/* Process all the CRYPTO frame at <el> encryption level.
2421 * Return 1 if succeeded, 0 if not.
2422 */
2423static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
2424 struct quic_conn_ctx *ctx)
2425{
2426 struct eb64_node *node;
2427
2428 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
2429 node = eb64_first(&el->rx.crypto.frms);
2430 while (node) {
2431 struct quic_rx_crypto_frm *cf;
2432
2433 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2434 if (cf->offset_node.key != el->rx.crypto.offset)
2435 break;
2436
2437 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2438 goto err;
2439
2440 node = eb64_next(node);
2441 quic_rx_packet_refdec(cf->pkt);
2442 eb64_delete(&cf->offset_node);
2443 pool_free(pool_head_quic_rx_crypto_frm, cf);
2444 }
2445
2446 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2447 return 1;
2448
2449 err:
2450 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2451 return 0;
2452}
2453
2454/* Process all the packets at <el> encryption level.
2455 * Return 1 if succeeded, 0 if not.
2456 */
2457int qc_treat_rx_pkts(struct quic_enc_level *el, struct quic_conn_ctx *ctx)
2458{
2459 struct quic_tls_ctx *tls_ctx;
2460 struct eb64_node *node;
2461
2462 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2463 tls_ctx = &el->tls_ctx;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002464 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002465 node = eb64_first(&el->rx.pkts);
2466 while (node) {
2467 struct quic_rx_packet *pkt;
2468
2469 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2470 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2471 /* Drop the packet */
2472 TRACE_PROTO("packet decryption failed -> dropped",
2473 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2474 }
2475 else {
Frédéric Lécaillec4b93ea2021-06-04 10:12:43 +02002476 if (!qc_parse_pkt_frms(pkt, ctx, el)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002477 /* Drop the packet */
2478 TRACE_PROTO("packet parsing failed -> dropped",
2479 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2480 }
2481 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002482 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2483
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002484 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
2485 el->pktns->rx.nb_ack_eliciting++;
2486 if (!(el->pktns->rx.nb_ack_eliciting & 1))
2487 el->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
2488 }
2489
2490 /* Update the largest packet number. */
2491 if (pkt->pn > el->pktns->rx.largest_pn)
2492 el->pktns->rx.largest_pn = pkt->pn;
2493
2494 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille654c6912021-06-04 10:27:23 +02002495 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002496 TRACE_DEVEL("Could not update ack range list",
2497 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002498 }
2499 }
2500 node = eb64_next(node);
2501 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002502 }
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002503 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002504
2505 if (!qc_treat_rx_crypto_frms(el, ctx))
2506 goto err;
2507
2508 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2509 return 1;
2510
2511 err:
2512 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2513 return 0;
2514}
2515
2516/* Called during handshakes to parse and build Initial and Handshake packets for QUIC
2517 * connections with <ctx> as I/O handler context.
2518 * Returns 1 if succeeded, 0 if not.
2519 */
2520int qc_do_hdshk(struct quic_conn_ctx *ctx)
2521{
2522 int ssl_err;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002523 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002524 enum quic_tls_enc_level tel, next_tel;
2525 struct quic_enc_level *qel, *next_qel;
2526 struct quic_tls_ctx *tls_ctx;
2527
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002528 qc = ctx->conn->qc;
2529 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002530 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002531 if (!quic_get_tls_enc_levels(&tel, &next_tel, qc->state))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002532 goto err;
2533
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002534 qel = &qc->els[tel];
2535 next_qel = &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002536
2537 next_level:
2538 tls_ctx = &qel->tls_ctx;
2539
2540 /* If the header protection key for this level has been derived,
2541 * remove the packet header protections.
2542 */
2543 if (!LIST_ISEMPTY(&qel->rx.pqpkts) &&
2544 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2545 qc_rm_hp_pkts(qel, ctx);
2546
2547 if (!eb_is_empty(&qel->rx.pkts) &&
2548 !qc_treat_rx_pkts(qel, ctx))
2549 goto err;
2550
2551 if (!qc_prep_hdshk_pkts(ctx))
2552 goto err;
2553
2554 if (!qc_send_ppkts(ctx))
2555 goto err;
2556
2557 /* Check if there is something to do for the next level.
2558 */
2559 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
2560 (!LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
2561 qel = next_qel;
2562 goto next_level;
2563 }
2564
2565 /* If the handshake has not been completed -> out! */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002566 if (qc->state < QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002567 goto out;
2568
2569 /* Discard the Handshake keys. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002570 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2571 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002572 qc_set_timer(ctx);
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002573 if (!quic_build_post_handshake_frames(qc) ||
2574 !qc_prep_phdshk_pkts(qc) ||
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 !qc_send_ppkts(ctx))
2576 goto err;
2577
2578 out:
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002579 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002580 return 1;
2581
2582 err:
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002583 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002584 return 0;
2585}
2586
2587/* Allocate a new QUIC connection and return it if succeeded, NULL if not. */
2588struct quic_conn *new_quic_conn(uint32_t version)
2589{
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002590 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002591
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002592 qc = pool_zalloc(pool_head_quic_conn);
2593 if (qc) {
2594 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002595 }
2596
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002597 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002598}
2599
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002600/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002601static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2602{
2603 int i;
2604
2605 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2606 if (qel->tx.crypto.bufs[i]) {
2607 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2608 qel->tx.crypto.bufs[i] = NULL;
2609 }
2610 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002611 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612}
2613
2614/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002615 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002616 * Returns 1 if succeeded, 0 if not.
2617 */
2618static int quic_conn_enc_level_init(struct quic_conn *qc,
2619 enum quic_tls_enc_level level)
2620{
2621 struct quic_enc_level *qel;
2622
2623 qel = &qc->els[level];
2624 qel->level = quic_to_ssl_enc_level(level);
2625 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2626 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2627 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2628 qel->tls_ctx.rx.flags = 0;
2629 qel->tls_ctx.tx.flags = 0;
2630
2631 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02002632 HA_RWLOCK_INIT(&qel->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002633 LIST_INIT(&qel->rx.pqpkts);
2634
2635 /* Allocate only one buffer. */
2636 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2637 if (!qel->tx.crypto.bufs)
2638 goto err;
2639
2640 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2641 if (!qel->tx.crypto.bufs[0])
2642 goto err;
2643
2644 qel->tx.crypto.bufs[0]->sz = 0;
2645 qel->tx.crypto.nb_buf = 1;
2646
2647 qel->tx.crypto.sz = 0;
2648 qel->tx.crypto.offset = 0;
2649
2650 return 1;
2651
2652 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002653 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002654 return 0;
2655}
2656
2657/* Release the memory allocated for <buf> array of buffers, with <nb> as size.
2658 * Never fails.
2659 */
2660static inline void free_quic_conn_tx_bufs(struct q_buf **bufs, size_t nb)
2661{
2662 struct q_buf **p;
2663
2664 if (!bufs)
2665 return;
2666
2667 p = bufs;
2668 while (--nb) {
2669 if (!*p) {
2670 p++;
2671 continue;
2672 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002673 ha_free(&(*p)->area);
2674 ha_free(p);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002675 p++;
2676 }
2677 free(bufs);
2678}
2679
2680/* Allocate an array or <nb> buffers of <sz> bytes each.
2681 * Return this array if succeeded, NULL if failed.
2682 */
2683static inline struct q_buf **quic_conn_tx_bufs_alloc(size_t nb, size_t sz)
2684{
2685 int i;
2686 struct q_buf **bufs, **p;
2687
2688 bufs = calloc(nb, sizeof *bufs);
2689 if (!bufs)
2690 return NULL;
2691
2692 i = 0;
2693 p = bufs;
2694 while (i++ < nb) {
2695 *p = calloc(1, sizeof **p);
2696 if (!*p)
2697 goto err;
2698
2699 (*p)->area = malloc(sz);
2700 if (!(*p)->area)
2701 goto err;
2702
2703 (*p)->pos = (*p)->area;
2704 (*p)->end = (*p)->area + sz;
2705 (*p)->data = 0;
2706 LIST_INIT(&(*p)->pkts);
2707 p++;
2708 }
2709
2710 return bufs;
2711
2712 err:
2713 free_quic_conn_tx_bufs(bufs, nb);
2714 return NULL;
2715}
2716
2717/* Release all the memory allocated for <conn> QUIC connection. */
2718static void quic_conn_free(struct quic_conn *conn)
2719{
2720 int i;
2721
2722 free_quic_conn_cids(conn);
2723 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2724 quic_conn_enc_level_uninit(&conn->els[i]);
2725 free_quic_conn_tx_bufs(conn->tx.bufs, conn->tx.nb_buf);
2726 if (conn->timer_task)
2727 task_destroy(conn->timer_task);
2728 pool_free(pool_head_quic_conn, conn);
2729}
2730
2731/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002732static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002733{
2734 struct quic_conn_ctx *conn_ctx;
2735 struct quic_conn *qc;
2736 struct quic_pktns *pktns;
2737
2738
2739 conn_ctx = task->context;
2740 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002741 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2742 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002743 task->expire = TICK_ETERNITY;
2744 pktns = quic_loss_pktns(qc);
2745 if (tick_isset(pktns->tx.loss_time)) {
2746 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2747
2748 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2749 if (!LIST_ISEMPTY(&lost_pkts))
2750 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2751 qc_set_timer(conn_ctx);
2752 goto out;
2753 }
2754
2755 if (qc->path->in_flight) {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002756 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002757 pktns->tx.pto_probe = 1;
2758 }
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002759 else if (objt_server(qc->conn->target) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2761 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2762
2763 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2764 hel->pktns->tx.pto_probe = 1;
2765 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2766 iel->pktns->tx.pto_probe = 1;
2767 }
2768 qc->tx.nb_pto_dgrams = QUIC_MAX_NB_PTO_DGRAMS;
2769 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2770 qc->path->loss.pto_count++;
2771
2772 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002773 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002774
2775 return task;
2776}
2777
2778/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2779 * connections used to identify the first Initial packets of client connecting
2780 * to listeners. This parameter must be NULL for QUIC connections attached
2781 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2782 * <scid> is the source connection ID with <scid_len> as length.
2783 * Returns 1 if succeeded, 0 if not.
2784 */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002785static int qc_new_conn_init(struct quic_conn *qc, int ipv4,
2786 struct eb_root *quic_initial_clients,
2787 struct eb_root *quic_clients,
2788 unsigned char *dcid, size_t dcid_len,
2789 unsigned char *scid, size_t scid_len, int server)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002790{
2791 int i;
2792 /* Initial CID. */
2793 struct quic_connection_id *icid;
2794
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002795 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002796 qc->cids = EB_ROOT;
2797 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002798 if (server) {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002799 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002800 /* Copy the initial DCID. */
2801 qc->odcid.len = dcid_len;
2802 if (qc->odcid.len)
2803 memcpy(qc->odcid.data, dcid, dcid_len);
2804
2805 /* Copy the SCID as our DCID for this connection. */
2806 if (scid_len)
2807 memcpy(qc->dcid.data, scid, scid_len);
2808 qc->dcid.len = scid_len;
2809 }
2810 /* QUIC Client (outgoing connection to servers) */
2811 else {
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002812 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813 if (dcid_len)
2814 memcpy(qc->dcid.data, dcid, dcid_len);
2815 qc->dcid.len = dcid_len;
2816 }
2817
2818 /* Initialize the output buffer */
2819 qc->obuf.pos = qc->obuf.data;
2820
2821 icid = new_quic_cid(&qc->cids, 0);
2822 if (!icid)
2823 return 0;
2824
2825 /* Select our SCID which is the first CID with 0 as sequence number. */
2826 qc->scid = icid->cid;
2827
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002828 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002829 if (server)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002830 ebmb_insert(quic_initial_clients, &qc->odcid_node, qc->odcid.len);
2831
2832 /* Insert our SCID, the connection ID for the QUIC client. */
2833 ebmb_insert(quic_clients, &qc->scid_node, qc->scid.len);
2834
2835 /* Packet number spaces initialization. */
2836 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2837 quic_pktns_init(&qc->pktns[i]);
2838 /* QUIC encryption level context initialization. */
2839 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
2840 if (!quic_conn_enc_level_init(qc, i))
2841 goto err;
2842 /* Initialize the packet number space. */
2843 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2844 }
2845
2846 /* TX part. */
2847 LIST_INIT(&qc->tx.frms_to_send);
2848 qc->tx.bufs = quic_conn_tx_bufs_alloc(QUIC_CONN_TX_BUFS_NB, QUIC_CONN_TX_BUF_SZ);
2849 if (!qc->tx.bufs)
2850 goto err;
2851
2852 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2853 qc->tx.wbuf = qc->tx.rbuf = 0;
2854 qc->tx.bytes = 0;
2855 qc->tx.nb_pto_dgrams = 0;
2856 /* RX part. */
2857 qc->rx.bytes = 0;
2858
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859 /* XXX TO DO: Only one path at this time. */
2860 qc->path = &qc->paths[0];
2861 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2862
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002863 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864
2865 return 1;
2866
2867 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002868 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002869 quic_conn_free(qc);
2870 return 0;
2871}
2872
2873/* Initialize the timer task of <qc> QUIC connection.
2874 * Returns 1 if succeeded, 0 if not.
2875 */
2876static int quic_conn_init_timer(struct quic_conn *qc)
2877{
2878 qc->timer_task = task_new(MAX_THREADS_MASK);
2879 if (!qc->timer_task)
2880 return 0;
2881
2882 qc->timer = TICK_ETERNITY;
2883 qc->timer_task->process = process_timer;
2884 qc->timer_task->context = qc->conn->xprt_ctx;
2885
2886 return 1;
2887}
2888
2889/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2890 * past one byte of this buffer.
2891 */
2892static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2893 struct quic_rx_packet *pkt)
2894{
2895 unsigned char dcid_len, scid_len;
2896
2897 /* Version */
2898 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2899 return 0;
2900
2901 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2902
2903 /* Destination Connection ID Length */
2904 dcid_len = *(*buf)++;
2905 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2906 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2907 /* XXX MUST BE DROPPED */
2908 return 0;
2909
2910 if (dcid_len) {
2911 /* Check that the length of this received DCID matches the CID lengths
2912 * of our implementation for non Initials packets only.
2913 */
2914 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2915 return 0;
2916
2917 memcpy(pkt->dcid.data, *buf, dcid_len);
2918 }
2919
2920 pkt->dcid.len = dcid_len;
2921 *buf += dcid_len;
2922
2923 /* Source Connection ID Length */
2924 scid_len = *(*buf)++;
2925 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2926 /* XXX MUST BE DROPPED */
2927 return 0;
2928
2929 if (scid_len)
2930 memcpy(pkt->scid.data, *buf, scid_len);
2931 pkt->scid.len = scid_len;
2932 *buf += scid_len;
2933
2934 return 1;
2935}
2936
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002937/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
2938 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
2939 * encryption level matching with the packet type. <*qel> may be null if not found.
2940 * Note that <ctx> may be null (for Initial packets).
2941 */
2942static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
2943 struct quic_conn *qc, struct quic_conn_ctx *ctx,
2944 struct quic_enc_level **qel)
2945{
2946 enum quic_tls_enc_level tel;
2947
2948 /* Special case without connection context (firt Initial packets) */
2949 if (!ctx) {
2950 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2951 return 1;
2952 }
2953
2954 tel = quic_packet_type_enc_level(pkt->type);
2955 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2956 *qel = NULL;
2957 return 0;
2958 }
2959
2960 *qel = &qc->els[tel];
2961 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
2962 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
2963 return 0;
2964 }
2965
2966 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002967 (tel != QUIC_TLS_ENC_LEVEL_APP || ctx->conn->qc->state >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002968 return 1;
2969
2970 return 0;
2971}
2972
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002973/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
2974 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
2975 * byte past the end of the buffer containing this packet and <beg> the address of
2976 * the packet first byte.
2977 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
2978 * Returns 1 if succeeded, 0 if not.
2979 */
2980static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
2981 unsigned char **buf, unsigned char *beg,
2982 const unsigned char *end,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002983 struct quic_conn *qc, struct quic_conn_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002984{
2985 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002986 struct quic_enc_level *qel;
2987 /* Only for traces. */
2988 struct quic_rx_packet *qpkt_trace;
2989
2990 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002991 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002992 /* The packet number is here. This is also the start minus
2993 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
2994 * protection.
2995 */
2996 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002997 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002998 /* Note that the following function enables us to unprotect the packet
2999 * number and its length subsequently used to decrypt the entire
3000 * packets.
3001 */
3002 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3003 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003004 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003005 goto err;
3006 }
3007
3008 /* The AAD includes the packet number field found at <pn>. */
3009 pkt->aad_len = pn - beg + pkt->pnl;
3010 qpkt_trace = pkt;
3011 /* Store the packet */
3012 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02003013 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille9fccace2021-06-04 10:33:32 +02003015 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003016 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003017 else if (qel) {
3018 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003019 pkt->pn_offset = pn - beg;
3020 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3021 }
3022
3023 memcpy(pkt->data, beg, pkt->len);
3024 /* Updtate the offset of <*buf> for the next QUIC packet. */
3025 *buf = beg + pkt->len;
3026
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003027 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003028 return 1;
3029
3030 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003031 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003032 return 0;
3033}
3034
3035/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3036 * Also set <*long_header> to 1 if this form is long, 0 if not.
3037 */
3038static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3039 unsigned char byte0, int *long_header)
3040{
3041 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3042 pkt->type =
3043 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3044 *long_header = 1;
3045 }
3046 else {
3047 pkt->type = QUIC_PACKET_TYPE_SHORT;
3048 *long_header = 0;
3049 }
3050}
3051
3052static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3053 struct quic_rx_packet *pkt,
3054 struct quic_dgram_ctx *dgram_ctx,
3055 struct sockaddr_storage *saddr)
3056{
3057 unsigned char *beg;
3058 uint64_t len;
3059 struct quic_conn *qc;
3060 struct eb_root *cids;
3061 struct ebmb_node *node;
3062 struct connection *srv_conn;
3063 struct quic_conn_ctx *conn_ctx;
3064 int long_header;
3065
3066 qc = NULL;
3067 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3068 if (end <= *buf)
3069 goto err;
3070
3071 /* Fixed bit */
3072 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3073 /* XXX TO BE DISCARDED */
3074 goto err;
3075
3076 srv_conn = dgram_ctx->owner;
3077 beg = *buf;
3078 /* Header form */
3079 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3080 if (long_header) {
3081 size_t cid_lookup_len;
3082
3083 if (!quic_packet_read_long_header(buf, end, pkt))
3084 goto err;
3085
3086 /* For Initial packets, and for servers (QUIC clients connections),
3087 * there is no Initial connection IDs storage.
3088 */
3089 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3090 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3091 cid_lookup_len = pkt->dcid.len;
3092 }
3093 else {
3094 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3095 cid_lookup_len = QUIC_CID_LEN;
3096 }
3097
3098 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3099 if (!node)
3100 goto err;
3101
3102 qc = ebmb_entry(node, struct quic_conn, scid_node);
3103
3104 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3105 qc->dcid.len = pkt->scid.len;
3106 if (pkt->scid.len)
3107 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3108 }
3109
3110 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3111 uint64_t token_len;
3112
3113 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3114 goto err;
3115
3116 /* XXX TO DO XXX 0 value means "the token is not present".
3117 * A server which sends an Initial packet must not set the token.
3118 * So, a client which receives an Initial packet with a token
3119 * MUST discard the packet or generate a connection error with
3120 * PROTOCOL_VIOLATION as type.
3121 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3122 */
3123 pkt->token_len = token_len;
3124 }
3125 }
3126 else {
3127 /* XXX TO DO: Short header XXX */
3128 if (end - *buf < QUIC_CID_LEN)
3129 goto err;
3130
3131 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3132 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3133 if (!node)
3134 goto err;
3135
3136 qc = ebmb_entry(node, struct quic_conn, scid_node);
3137 *buf += QUIC_CID_LEN;
3138 }
3139 /* Store the DCID used for this packet to check the packet which
3140 * come in this UDP datagram match with it.
3141 */
3142 if (!dgram_ctx->dcid_node)
3143 dgram_ctx->dcid_node = node;
3144 /* Only packets packets with long headers and not RETRY or VERSION as type
3145 * have a length field.
3146 */
3147 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3148 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3149 goto err;
3150
3151 pkt->len = len;
3152 }
3153 else if (!long_header) {
3154 /* A short packet is the last one of an UDP datagram. */
3155 pkt->len = end - *buf;
3156 }
3157
3158 conn_ctx = qc->conn->xprt_ctx;
3159
3160 /* Increase the total length of this packet by the header length. */
3161 pkt->len += *buf - beg;
3162 /* Do not check the DCID node before the length. */
3163 if (dgram_ctx->dcid_node != node) {
3164 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3165 goto err;
3166 }
3167
3168 if (pkt->len > sizeof pkt->data) {
3169 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3170 goto err;
3171 }
3172
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003173 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003174 goto err;
3175
3176 /* Wake the tasklet of the QUIC connection packet handler. */
3177 if (conn_ctx)
3178 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3179
3180 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3181
3182 return pkt->len;
3183
3184 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003185 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003186 return -1;
3187}
3188
3189static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3190 struct quic_rx_packet *pkt,
3191 struct quic_dgram_ctx *dgram_ctx,
3192 struct sockaddr_storage *saddr)
3193{
3194 unsigned char *beg;
3195 uint64_t len;
3196 struct quic_conn *qc;
3197 struct eb_root *cids;
3198 struct ebmb_node *node;
3199 struct listener *l;
3200 struct quic_conn_ctx *conn_ctx;
3201 int long_header = 0;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003202 /* boolean to denote if a connection exists for this packet.
3203 * This does not mean there is an xprt context for it.
3204 */
3205 int found_conn = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003206
3207 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003208 conn_ctx = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003209 TRACE_ENTER(QUIC_EV_CONN_LPKT);
3210 if (end <= *buf)
3211 goto err;
3212
3213 /* Fixed bit */
3214 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3215 /* XXX TO BE DISCARDED */
3216 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3217 goto err;
3218 }
3219
3220 l = dgram_ctx->owner;
3221 beg = *buf;
3222 /* Header form */
3223 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3224 if (long_header) {
3225 unsigned char dcid_len;
3226
3227 if (!quic_packet_read_long_header(buf, end, pkt)) {
3228 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3229 goto err;
3230 }
3231
3232 dcid_len = pkt->dcid.len;
3233 /* For Initial packets, and for servers (QUIC clients connections),
3234 * there is no Initial connection IDs storage.
3235 */
3236 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3237 /* DCIDs of first packets coming from clients may have the same values.
3238 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3239 */
3240 quic_cid_saddr_cat(&pkt->dcid, saddr);
3241 cids = &l->rx.odcids;
3242 }
3243 else {
3244 if (pkt->dcid.len != QUIC_CID_LEN) {
3245 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3246 goto err;
3247 }
3248
3249 cids = &l->rx.cids;
3250 }
3251
3252 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3253 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3254 cids == &l->rx.odcids) {
3255 /* Switch to the definitive tree ->cids containing the final CIDs. */
3256 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3257 if (node) {
3258 /* If found, signal this with NULL as special value for <cids>. */
3259 pkt->dcid.len = dcid_len;
3260 cids = NULL;
3261 }
3262 }
3263
3264 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003265 int ipv4;
3266 struct quic_cid *odcid;
3267
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003268 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3269 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3270 goto err;
3271 }
3272
3273 qc = new_quic_conn(pkt->version);
3274 if (!qc) {
3275 TRACE_PROTO("Non allocated new connection", QUIC_EV_CONN_LPKT);
3276 goto err;
3277 }
3278
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003279 pkt->saddr = *saddr;
3280 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3281 * of <saddr>.
3282 */
3283 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003284 ipv4 = saddr->ss_family == AF_INET;
3285 if (!qc_new_conn_init(qc, ipv4, &l->rx.odcids, &l->rx.cids,
3286 pkt->dcid.data, pkt->dcid.len,
3287 pkt->scid.data, pkt->scid.len, 1))
3288 goto err;
3289
3290 odcid = &qc->rx.params.original_destination_connection_id;
3291 /* Copy the transport parameters. */
3292 qc->rx.params = l->bind_conf->quic_params;
3293 /* Copy original_destination_connection_id transport parameter. */
3294 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3295 odcid->len = pkt->odcid_len;
3296 /* Copy the initial source connection ID. */
3297 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3298 qc->enc_params_len =
3299 quic_transport_params_encode(qc->enc_params,
3300 qc->enc_params + sizeof qc->enc_params,
3301 &qc->rx.params, 1);
3302 if (!qc->enc_params_len)
3303 goto err;
3304
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003305 /* NOTE: the socket address has been concatenated to the destination ID
3306 * chosen by the client for Initial packets.
3307 */
3308 if (!qc_new_isecs(qc, pkt->dcid.data, pkt->odcid_len, 1)) {
3309 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3310 goto err;
3311 }
3312
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003313 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003314 /* This is the DCID node sent in this packet by the client. */
3315 node = &qc->odcid_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003316 }
3317 else {
3318 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3319 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3320 else
3321 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003322 conn_ctx = qc->conn->xprt_ctx;
3323 found_conn = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003324 }
3325
3326 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3327 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003328
3329 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3330 end - *buf < token_len) {
3331 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3332 goto err;
3333 }
3334
3335 /* XXX TO DO XXX 0 value means "the token is not present".
3336 * A server which sends an Initial packet must not set the token.
3337 * So, a client which receives an Initial packet with a token
3338 * MUST discard the packet or generate a connection error with
3339 * PROTOCOL_VIOLATION as type.
3340 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3341 */
3342 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003343 }
3344 }
3345 else {
3346 if (end - *buf < QUIC_CID_LEN) {
3347 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3348 goto err;
3349 }
3350
3351 cids = &l->rx.cids;
3352 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3353 if (!node) {
3354 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3355 goto err;
3356 }
3357
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003358 found_conn = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003360 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003361 *buf += QUIC_CID_LEN;
3362 }
3363
3364 /* Store the DCID used for this packet to check the packet which
3365 * come in this UDP datagram match with it.
3366 */
3367 if (!dgram_ctx->dcid_node) {
3368 dgram_ctx->dcid_node = node;
3369 dgram_ctx->qc = qc;
3370 }
3371
3372 /* Only packets packets with long headers and not RETRY or VERSION as type
3373 * have a length field.
3374 */
3375 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3376 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3377 end - *buf < len) {
3378 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3379 goto err;
3380 }
3381
3382 pkt->len = len;
3383 }
3384 else if (!long_header) {
3385 /* A short packet is the last one of an UDP datagram. */
3386 pkt->len = end - *buf;
3387 }
3388
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003389 /* Increase the total length of this packet by the header length. */
3390 pkt->len += *buf - beg;
3391 /* Do not check the DCID node before the length. */
3392 if (dgram_ctx->dcid_node != node) {
3393 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3394 goto err;
3395 }
3396
3397 if (pkt->len > sizeof pkt->data) {
3398 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3399 goto err;
3400 }
3401
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003402 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3403 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3404 goto err;
3405 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003407 if (conn_ctx) {
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003408 /* Wake the tasklet of the QUIC connection packet handler. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003409 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003410 }
3411 else if (!found_conn) {
3412 /* Enqueue this packet. */
3413 LIST_APPEND(&l->rx.qpkts, &pkt->rx_list);
3414 /* Try to accept a new connection. */
3415 listener_accept(l);
3416 }
3417
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003418 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3419
3420 return pkt->len;
3421
3422 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003423 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003424 qc ? qc->conn : NULL, pkt);
3425 return -1;
3426}
3427
3428/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3429 * in advance. This is the reponsability of the caller to check there is enough room in this
3430 * buffer to build a long header.
3431 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3432 */
3433static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3434 int type, size_t pn_len, struct quic_conn *conn)
3435{
3436 if (type > QUIC_PACKET_TYPE_RETRY)
3437 return 0;
3438
3439 /* #0 byte flags */
3440 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3441 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3442 /* Version */
3443 quic_write_uint32(buf, end, conn->version);
3444 *(*buf)++ = conn->dcid.len;
3445 /* Destination connection ID */
3446 if (conn->dcid.len) {
3447 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3448 *buf += conn->dcid.len;
3449 }
3450 /* Source connection ID */
3451 *(*buf)++ = conn->scid.len;
3452 if (conn->scid.len) {
3453 memcpy(*buf, conn->scid.data, conn->scid.len);
3454 *buf += conn->scid.len;
3455 }
3456
3457 return 1;
3458}
3459
3460/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3461 * in advance. This is the reponsability of the caller to check there is enough room in this
3462 * buffer to build a long header.
3463 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3464 */
3465static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3466 size_t pn_len, struct quic_conn *conn)
3467{
3468 /* #0 byte flags */
3469 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3470 /* Destination connection ID */
3471 if (conn->dcid.len) {
3472 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3473 *buf += conn->dcid.len;
3474 }
3475
3476 return 1;
3477}
3478
3479/* Apply QUIC header protection to the packet with <buf> as first byte address,
3480 * <pn> as address of the Packet number field, <pnlen> being this field length
3481 * with <aead> as AEAD cipher and <key> as secret key.
3482 * Returns 1 if succeeded or 0 if failed.
3483 */
3484static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3485 const EVP_CIPHER *aead, const unsigned char *key)
3486{
3487 int i, ret, outlen;
3488 EVP_CIPHER_CTX *ctx;
3489 /* We need an IV of at least 5 bytes: one byte for bytes #0
3490 * and at most 4 bytes for the packet number
3491 */
3492 unsigned char mask[5] = {0};
3493
3494 ret = 0;
3495 ctx = EVP_CIPHER_CTX_new();
3496 if (!ctx)
3497 return 0;
3498
3499 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3500 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3501 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3502 goto out;
3503
3504 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3505 for (i = 0; i < pnlen; i++)
3506 pn[i] ^= mask[i + 1];
3507
3508 ret = 1;
3509
3510 out:
3511 EVP_CIPHER_CTX_free(ctx);
3512
3513 return ret;
3514}
3515
3516/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3517 * ACK ranges if needed to a value below <limit> in bytes.
3518 * Return 1 if succeeded, 0 if not.
3519 */
3520static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3521{
3522 size_t room, ack_delay_sz;
3523
3524 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3525 /* A frame is made of 1 byte for the frame type. */
3526 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003527 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003528 return 0;
3529
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003530 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003531}
3532
3533/* Prepare as most as possible CRYPTO frames from prebuilt CRYPTO frames for <qel>
3534 * encryption level to be encoded in a buffer with <room> as available room,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003535 * and <*len> the packet Length field initialized with the number of bytes already present
3536 * in this buffer which must be taken into an account for the Length packet field value.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003537 * <headlen> is the number of bytes already present in this packet before building
Frédéric Lécailleea604992020-12-24 13:01:37 +01003538 * CRYPTO frames.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003539 * This is the responsibility of the caller to check that <*len> < <room> as this is
3540 * the responsibility to check that <headlen> < quic_path_prep_data(conn->path).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003541 * Update consequently <*len> to reflect the size of these CRYPTO frames built
3542 * by this function. Also attach these CRYPTO frames to <pkt> QUIC packet.
3543 * Return 1 if succeeded, 0 if not.
3544 */
3545static inline int qc_build_cfrms(struct quic_tx_packet *pkt,
Frédéric Lécailleea604992020-12-24 13:01:37 +01003546 size_t room, size_t *len, size_t headlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003547 struct quic_enc_level *qel,
3548 struct quic_conn *conn)
3549{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003550 int ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003551 struct quic_tx_frm *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003552
Frédéric Lécailleea604992020-12-24 13:01:37 +01003553 ret = 0;
3554 /* If we are not probing we must take into an account the congestion
3555 * control window.
3556 */
3557 if (!conn->tx.nb_pto_dgrams)
3558 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
3559 TRACE_PROTO("************** CRYPTO frames build (headlen)",
3560 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
3562 /* header length, data length, frame length. */
3563 size_t hlen, dlen, cflen;
3564
Frédéric Lécailleea604992020-12-24 13:01:37 +01003565 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3566 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3567 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003568 break;
3569
3570 /* Compute the length of this CRYPTO frame header */
3571 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3572 /* Compute the data length of this CRyPTO frame. */
3573 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
Frédéric Lécailleea604992020-12-24 13:01:37 +01003574 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3575 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003576 if (!dlen)
3577 break;
3578
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003579 pkt->cdata_len += dlen;
3580 /* CRYPTO frame length. */
3581 cflen = hlen + quic_int_getsize(dlen) + dlen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003582 TRACE_PROTO(" CRYPTO frame length (cflen)",
3583 QUIC_EV_CONN_BCFRMS, conn->conn, &cflen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003584 /* Add the CRYPTO data length and its encoded length to the packet
3585 * length and the length of this length.
3586 */
3587 *len += cflen;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003588 room -= cflen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003589 if (dlen == cf->crypto.len) {
3590 /* <cf> CRYPTO data have been consumed. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003591 LIST_DELETE(&cf->list);
3592 LIST_APPEND(&pkt->frms, &cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003593 }
3594 else {
3595 struct quic_tx_frm *new_cf;
3596
3597 new_cf = pool_alloc(pool_head_quic_tx_frm);
3598 if (!new_cf) {
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003599 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003600 return 0;
3601 }
3602
3603 new_cf->type = QUIC_FT_CRYPTO;
3604 new_cf->crypto.len = dlen;
3605 new_cf->crypto.offset = cf->crypto.offset;
Willy Tarreau2b718102021-04-21 07:32:39 +02003606 LIST_APPEND(&pkt->frms, &new_cf->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003607 /* Consume <dlen> bytes of the current frame. */
3608 cf->crypto.len -= dlen;
3609 cf->crypto.offset += dlen;
3610 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003611 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612 }
3613
Frédéric Lécailleea604992020-12-24 13:01:37 +01003614 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615}
3616
3617/* This function builds a clear handshake packet used during a QUIC TLS handshakes
3618 * into <wbuf> the current <wbuf> for <conn> QUIC connection with <qel> as QUIC
3619 * TLS encryption level for outgoing packets filling it with as much as CRYPTO
3620 * data as possible from <offset> offset in the CRYPTO data stream. Note that
3621 * this offset value is updated by the length of the CRYPTO frame used to embed
3622 * the CRYPTO data if this packet and only if the packet is successfully built.
3623 * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
3624 * reserved so that to be sure there is enough room to build this AEAD TAG after
3625 * having successfully returned from this function and to be sure the position
3626 * pointer of <wbuf> may be safely incremented by QUIC_TLS_TAG_LEN. After having
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003627 * returned from this function, <wbuf> position will point one past the last
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003628 * byte of the payload with the confidence there is at least QUIC_TLS_TAG_LEN bytes
3629 * available packet to encrypt this packet.
3630 * This function also update the value of <buf_pn> pointer to point to the packet
3631 * number field in this packet. <pn_len> will also have the packet number
3632 * length as value.
3633 *
3634 * Return the length of the packet if succeeded minus QUIC_TLS_TAG_LEN, or -1 if
3635 * failed (not enough room in <wbuf> to build this packet plus QUIC_TLS_TAG_LEN
3636 * bytes), -2 if there are too much CRYPTO data in flight to build a packet.
3637 */
3638static ssize_t qc_do_build_hdshk_pkt(struct q_buf *wbuf,
3639 struct quic_tx_packet *pkt, int pkt_type,
3640 int64_t pn, size_t *pn_len,
3641 unsigned char **buf_pn,
3642 struct quic_enc_level *qel,
3643 struct quic_conn *conn)
3644{
3645 unsigned char *beg, *pos;
3646 const unsigned char *end;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003647 size_t len, len_frms, token_fields_len, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003648 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3649 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3650 struct quic_crypto *crypto = &frm.crypto;
3651 size_t ack_frm_len;
3652 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003653 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003654
Frédéric Lécailleea604992020-12-24 13:01:37 +01003655 /* Length field value with CRYPTO frames if present. */
3656 len_frms = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003657 beg = pos = q_buf_getpos(wbuf);
3658 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003659 /* When not probing and not acking, reduce the size of this buffer to respect
3660 * the congestion controller window.
3661 */
3662 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3663 size_t path_room;
3664
3665 path_room = quic_path_prep_data(conn->path);
3666 if (end - beg > path_room)
3667 end = beg + path_room;
3668 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669
3670 /* For a server, the token field of an Initial packet is empty. */
3671 token_fields_len = pkt_type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3672
3673 /* Check there is enough room to build the header followed by a token. */
3674 if (end - pos < QUIC_LONG_PACKET_MINLEN + conn->dcid.len +
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003675 conn->scid.len + token_fields_len + QUIC_TLS_TAG_LEN) {
3676 ssize_t room = end - pos;
3677 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3678 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003679 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003680 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003681
3682 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3683 end -= QUIC_TLS_TAG_LEN;
3684 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3685 /* packet number length */
3686 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3687
3688 quic_build_packet_long_header(&pos, end, pkt_type, *pn_len, conn);
3689
3690 /* Encode the token length (0) for an Initial packet. */
3691 if (pkt_type == QUIC_PACKET_TYPE_INITIAL)
3692 *pos++ = 0;
3693
3694 /* Build an ACK frame if required. */
3695 ack_frm_len = 0;
3696 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003697 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003698 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003699 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003700 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003701 if (!ack_frm_len) {
3702 ssize_t room = end - pos;
3703 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3704 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003705 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003706 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707
3708 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3709 }
3710
3711 /* Length field value without the CRYPTO frames data length. */
3712 len = ack_frm_len + *pn_len;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003713 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003714 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003715
3716 len_frms = len + QUIC_TLS_TAG_LEN;
3717 if (!qc_build_cfrms(pkt, end - pos, &len_frms, pos - beg, qel, conn)) {
3718 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3719 conn->conn, NULL, NULL, &room);
3720 goto err;
3721 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003722 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003723
3724 add_ping_frm = 0;
3725 padding_len = 0;
3726 if (objt_server(conn->conn->target) &&
3727 pkt_type == QUIC_PACKET_TYPE_INITIAL &&
3728 len < QUIC_INITIAL_PACKET_MINLEN) {
3729 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3730 }
3731 else if (LIST_ISEMPTY(&pkt->frms)) {
3732 if (qel->pktns->tx.pto_probe) {
3733 /* If we cannot send a CRYPTO frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003734 add_ping_frm = 1;
3735 len += 1;
3736 }
3737 /* If there is no frame at all to follow, add at least a PADDING frame. */
3738 if (!ack_frm_len)
3739 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3740 }
3741
3742 /* Length (of the remaining data). Must not fail because, the buffer size
3743 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
3744 * for the encryption TAG. It must be taken into an account for the length
3745 * of this packet.
3746 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01003747 if (len_frms)
3748 len = len_frms;
3749 else
3750 len += QUIC_TLS_TAG_LEN;
3751 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003752
3753 /* Packet number field address. */
3754 *buf_pn = pos;
3755
3756 /* Packet number encoding. */
3757 quic_packet_number_encode(&pos, end, pn, *pn_len);
3758
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003759 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3760 ssize_t room = end - pos;
3761 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3762 conn->conn, NULL, NULL, &room);
3763 goto err;
3764 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003765
3766 /* Crypto frame */
3767 if (!LIST_ISEMPTY(&pkt->frms)) {
3768 struct quic_tx_frm *cf;
3769
3770 list_for_each_entry(cf, &pkt->frms, list) {
3771 crypto->offset = cf->crypto.offset;
3772 crypto->len = cf->crypto.len;
3773 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003774 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3775 ssize_t room = end - pos;
3776 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3777 conn->conn, NULL, NULL, &room);
3778 goto err;
3779 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003780 }
3781 }
3782
3783 /* Build a PING frame if needed. */
3784 if (add_ping_frm) {
3785 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003786 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3787 ssize_t room = end - pos;
3788 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3789 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003790 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003791 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792 }
3793
3794 /* Build a PADDING frame if needed. */
3795 if (padding_len) {
3796 frm.type = QUIC_FT_PADDING;
3797 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003798 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3799 ssize_t room = end - pos;
3800 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3801 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003802 goto err;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003803 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003804 }
3805
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003806 /* Always reset this variable as this function has no idea
3807 * if it was set. It is handle by the loss detection timer.
3808 */
3809 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003810
3811 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003812 return pos - beg;
3813
3814 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003815 return -1;
3816}
3817
3818static inline void quic_tx_packet_init(struct quic_tx_packet *pkt)
3819{
3820 pkt->cdata_len = 0;
3821 pkt->in_flight_len = 0;
3822 LIST_INIT(&pkt->frms);
3823}
3824
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003825/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003826static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3827{
3828 struct quic_tx_frm *frm, *frmbak;
3829
3830 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003831 LIST_DELETE(&frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003832 pool_free(pool_head_quic_tx_frm, frm);
3833 }
3834 pool_free(pool_head_quic_tx_packet, pkt);
3835}
3836
3837/* Build a handshake packet into <buf> packet buffer with <pkt_type> as packet
3838 * type for <qc> QUIC connection from CRYPTO data stream at <*offset> offset to
3839 * be encrypted at <qel> encryption level.
3840 * Return -2 if the packet could not be encrypted for any reason, -1 if there was
3841 * not enough room in <buf> to build the packet, or the size of the built packet
3842 * if succeeded (may be zero if there is too much crypto data in flight to build the packet).
3843 */
3844static ssize_t qc_build_hdshk_pkt(struct q_buf *buf, struct quic_conn *qc, int pkt_type,
3845 struct quic_enc_level *qel)
3846{
3847 /* The pointer to the packet number field. */
3848 unsigned char *buf_pn;
3849 unsigned char *beg, *end, *payload;
3850 int64_t pn;
3851 size_t pn_len, payload_len, aad_len;
3852 ssize_t pkt_len;
3853 struct quic_tls_ctx *tls_ctx;
3854 struct quic_tx_packet *pkt;
3855
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003856 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003857 pkt = pool_alloc(pool_head_quic_tx_packet);
3858 if (!pkt) {
3859 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
3860 return -2;
3861 }
3862
3863 quic_tx_packet_init(pkt);
3864 beg = q_buf_getpos(buf);
3865 pn_len = 0;
3866 buf_pn = NULL;
3867 pn = qel->pktns->tx.next_pn + 1;
3868 pkt_len = qc_do_build_hdshk_pkt(buf, pkt, pkt_type, pn, &pn_len, &buf_pn, qel, qc);
3869 if (pkt_len <= 0) {
3870 free_quic_tx_packet(pkt);
3871 return pkt_len;
3872 }
3873
3874 end = beg + pkt_len;
3875 payload = buf_pn + pn_len;
3876 payload_len = end - payload;
3877 aad_len = payload - beg;
3878
3879 tls_ctx = &qel->tls_ctx;
3880 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn))
3881 goto err;
3882
3883 end += QUIC_TLS_TAG_LEN;
3884 pkt_len += QUIC_TLS_TAG_LEN;
3885 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
3886 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
3887 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
3888 goto err;
3889 }
3890
3891 /* Now that a correct packet is built, let us set the position pointer of
3892 * <buf> buffer for the next packet.
3893 */
3894 q_buf_setpos(buf, end);
3895 /* Consume a packet number. */
3896 ++qel->pktns->tx.next_pn;
3897 /* Attach the built packet to its tree. */
3898 pkt->pn_node.key = qel->pktns->tx.next_pn;
3899 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003900 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003901 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003902 qc->path->prep_in_flight += pkt_len;
3903 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003904 pkt->pktns = qel->pktns;
3905 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
3906 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
3907 buf->data += pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003908 /* Attach this packet to <buf>. */
Willy Tarreau2b718102021-04-21 07:32:39 +02003909 LIST_APPEND(&buf->pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003910 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
3911
3912 return pkt_len;
3913
3914 err:
3915 free_quic_tx_packet(pkt);
3916 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
3917 return -2;
3918}
3919
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003920/* Prepare a clear post handhskake packet for <conn> QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003921 * Return the length of this packet if succeeded, -1 <wbuf> was full.
3922 */
3923static ssize_t qc_do_build_phdshk_apkt(struct q_buf *wbuf,
3924 struct quic_tx_packet *pkt,
3925 int64_t pn, size_t *pn_len,
3926 unsigned char **buf_pn, struct quic_enc_level *qel,
3927 struct quic_conn *conn)
3928{
3929 const unsigned char *beg, *end;
3930 unsigned char *pos;
3931 struct quic_frame *frm, *sfrm;
3932 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
3933 size_t fake_len, ack_frm_len;
3934 int64_t largest_acked_pn;
3935
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003936 TRACE_ENTER(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003937 beg = pos = q_buf_getpos(wbuf);
3938 end = q_buf_end(wbuf);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003939 /* When not probing and not acking, reduce the size of this buffer to respect
3940 * the congestion controller window.
3941 */
3942 if (!conn->tx.nb_pto_dgrams && !(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
3943 size_t path_room;
3944
3945 path_room = quic_path_prep_data(conn->path);
3946 if (end - beg > path_room)
3947 end = beg + path_room;
3948 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003949 largest_acked_pn = qel->pktns->tx.largest_acked_pn;
3950 /* Packet number length */
3951 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
3952 /* Check there is enough room to build this packet (without payload). */
3953 if (end - pos < QUIC_SHORT_PACKET_MINLEN + sizeof_quic_cid(&conn->dcid) +
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003954 *pn_len + QUIC_TLS_TAG_LEN) {
3955 ssize_t room = end - pos;
3956 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3957 conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003958 goto err;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003959 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003960
3961 /* Reserve enough room at the end of the packet for the AEAD TAG. */
3962 end -= QUIC_TLS_TAG_LEN;
3963 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3964 /* Packet number field. */
3965 *buf_pn = pos;
3966 /* Packet number encoding. */
3967 quic_packet_number_encode(&pos, end, pn, *pn_len);
3968
3969 /* Build an ACK frame if required. */
3970 ack_frm_len = 0;
3971 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003972 !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003973 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003974 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003975 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - pos);
3976 if (!ack_frm_len)
3977 goto err;
3978
3979 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
3980 }
3981
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003982 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3983 ssize_t room = end - pos;
3984 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
3985 conn->conn, NULL, NULL, &room);
3986 goto err;
3987 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003988
3989 fake_len = ack_frm_len;
3990 if (!LIST_ISEMPTY(&qel->pktns->tx.frms) &&
Frédéric Lécailleea604992020-12-24 13:01:37 +01003991 !qc_build_cfrms(pkt, end - pos, &fake_len, pos - beg, qel, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003992 ssize_t room = end - pos;
3993 TRACE_PROTO("some CRYPTO frames could not be built",
3994 QUIC_EV_CONN_PAPKT, conn->conn, NULL, NULL, &room);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003995 goto err;
3996 }
3997
3998 /* Crypto frame */
3999 if (!LIST_ISEMPTY(&pkt->frms)) {
4000 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4001 struct quic_crypto *crypto = &frm.crypto;
4002 struct quic_tx_frm *cf;
4003
4004 list_for_each_entry(cf, &pkt->frms, list) {
4005 crypto->offset = cf->crypto.offset;
4006 crypto->len = cf->crypto.len;
4007 crypto->qel = qel;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004008 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4009 ssize_t room = end - pos;
4010 TRACE_PROTO("Not enough room", QUIC_EV_CONN_PAPKT,
4011 conn->conn, NULL, NULL, &room);
4012 goto err;
4013 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004014 }
4015 }
4016
4017 /* Encode a maximum of frames. */
4018 list_for_each_entry_safe(frm, sfrm, &conn->tx.frms_to_send, list) {
4019 unsigned char *ppos;
4020
4021 ppos = pos;
4022 if (!qc_build_frm(&ppos, end, frm, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004023 TRACE_DEVEL("Frames not built", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004024 break;
4025 }
4026
Willy Tarreau2b718102021-04-21 07:32:39 +02004027 LIST_DELETE(&frm->list);
4028 LIST_APPEND(&pkt->frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004029 pos = ppos;
4030 }
4031
4032 out:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004033 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004034 return pos - beg;
4035
4036 err:
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004037 TRACE_DEVEL("leaving in error (buffer full)", QUIC_EV_CONN_PAPKT, conn->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004038 return -1;
4039}
4040
4041/* Prepare a post handhskake packet at Application encryption level for <conn>
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004042 * QUIC connection.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004043 * Return the length of this packet if succeeded, -1 if <wbuf> was full,
4044 * -2 in case of major error (encryption failure).
4045 */
4046static ssize_t qc_build_phdshk_apkt(struct q_buf *wbuf, struct quic_conn *qc)
4047{
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004048 /* A pointer to the packet number field in <buf> */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004049 unsigned char *buf_pn;
4050 unsigned char *beg, *end, *payload;
4051 int64_t pn;
4052 size_t pn_len, aad_len, payload_len;
4053 ssize_t pkt_len;
4054 struct quic_tls_ctx *tls_ctx;
4055 struct quic_enc_level *qel;
4056 struct quic_tx_packet *pkt;
4057
4058 TRACE_ENTER(QUIC_EV_CONN_PAPKT, qc->conn);
4059 pkt = pool_alloc(pool_head_quic_tx_packet);
4060 if (!pkt) {
4061 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_PAPKT, qc->conn);
4062 return -2;
4063 }
4064
4065 quic_tx_packet_init(pkt);
4066 beg = q_buf_getpos(wbuf);
4067 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4068 pn_len = 0;
4069 buf_pn = NULL;
4070 pn = qel->pktns->tx.next_pn + 1;
4071 pkt_len = qc_do_build_phdshk_apkt(wbuf, pkt, pn, &pn_len, &buf_pn, qel, qc);
4072 if (pkt_len <= 0) {
4073 free_quic_tx_packet(pkt);
4074 return pkt_len;
4075 }
4076
4077 end = beg + pkt_len;
4078 payload = buf_pn + pn_len;
4079 payload_len = end - payload;
4080 aad_len = payload - beg;
4081
4082 tls_ctx = &qel->tls_ctx;
4083 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 +01004084 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004085
4086 end += QUIC_TLS_TAG_LEN;
4087 pkt_len += QUIC_TLS_TAG_LEN;
4088 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4089 tls_ctx->tx.hp, tls_ctx->tx.hp_key))
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004090 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004091
4092 q_buf_setpos(wbuf, end);
4093 /* Consume a packet number. */
4094 ++qel->pktns->tx.next_pn;
4095 /* Attach the built packet to its tree. */
4096 pkt->pn_node.key = qel->pktns->tx.next_pn;
4097 eb64_insert(&qel->pktns->tx.pkts, &pkt->pn_node);
4098 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004099 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004100 pkt->in_flight_len = pkt_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004101 qc->path->prep_in_flight += pkt_len;
4102 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004103 pkt->pktns = qel->pktns;
4104 /* Increment the number of bytes in <buf> buffer by the length of this packet. */
4105 wbuf->data += pkt_len;
4106 /* Attach this packet to <buf>. */
Willy Tarreau2b718102021-04-21 07:32:39 +02004107 LIST_APPEND(&wbuf->pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004108
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004109 TRACE_LEAVE(QUIC_EV_CONN_PAPKT, qc->conn, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004110
4111 return pkt_len;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004112
4113 err:
4114 free_quic_tx_packet(pkt);
4115 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PAPKT, qc->conn);
4116 return -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004117}
4118
4119/* Prepare a maximum of QUIC Application level packets from <ctx> QUIC
4120 * connection I/O handler context.
4121 * Returns 1 if succeeded, 0 if not.
4122 */
4123int qc_prep_phdshk_pkts(struct quic_conn *qc)
4124{
4125 struct q_buf *wbuf;
4126 struct quic_enc_level *qel;
4127
4128 TRACE_ENTER(QUIC_EV_CONN_PAPKTS, qc->conn);
4129 wbuf = q_wbuf(qc);
4130 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
4131 while (q_buf_empty(wbuf)) {
4132 ssize_t ret;
4133
4134 if (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
4135 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004136 qc->path->prep_in_flight >= qc->path->cwnd)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004137 TRACE_DEVEL("nothing more to do",
4138 QUIC_EV_CONN_PAPKTS, qc->conn);
4139 break;
4140 }
4141
4142 ret = qc_build_phdshk_apkt(wbuf, qc);
4143 switch (ret) {
4144 case -1:
4145 /* Not enough room left in <wbuf>. */
4146 wbuf = q_next_wbuf(qc);
4147 continue;
4148 case -2:
4149 return 0;
4150 default:
4151 /* XXX TO CHECK: consume a buffer. */
4152 wbuf = q_next_wbuf(qc);
4153 continue;
4154 }
4155 }
4156 TRACE_LEAVE(QUIC_EV_CONN_PAPKTS, qc->conn);
4157
4158 return 1;
4159}
4160
4161/* QUIC connection packet handler task. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004162struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004163{
4164 struct quic_conn_ctx *ctx = context;
4165
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004166 if (ctx->conn->qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004167 qc_do_hdshk(ctx);
4168 }
4169 else {
4170 struct quic_conn *qc = ctx->conn->qc;
4171
4172 /* XXX TO DO: may fail!!! XXX */
4173 qc_treat_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_APP], ctx);
4174 qc_prep_phdshk_pkts(qc);
4175 qc_send_ppkts(ctx);
4176 }
4177
Willy Tarreau74163142021-03-13 11:30:19 +01004178 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004179}
4180
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004181/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4182 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004184static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4185 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004186{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004187 size_t try, done = 0;
4188
4189 if (!conn_ctrl_ready(conn))
4190 return 0;
4191
4192 if (!fd_recv_ready(conn->handle.fd))
4193 return 0;
4194
4195 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196
4197 /* read the largest possible block. For this, we perform only one call
4198 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004199 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004200 */
4201 while (count > 0) {
4202 try = b_contig_space(buf);
4203 if (!try)
4204 break;
4205
4206 if (try > count)
4207 try = count;
4208
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004209 b_add(buf, try);
4210 done += try;
4211 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004212 }
4213
4214 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4215 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4216
4217 leave:
4218 return done;
4219
4220 read0:
4221 conn_sock_read0(conn);
4222 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4223
4224 /* Now a final check for a possible asynchronous low-level error
4225 * report. This can happen when a connection receives a reset
4226 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4227 * we might have come from there by just checking POLL_HUP instead
4228 * of recv()'s return value 0, so we have no way to tell there was
4229 * an error without checking.
4230 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004231 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004232 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4233 goto leave;
4234}
4235
4236
4237/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4238 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4239 * other pending data for example, but this flag is ignored at the moment.
4240 * Only one call to send() is performed, unless the buffer wraps, in which case
4241 * a second call may be performed. The connection's flags are updated with
4242 * whatever special event is detected (error, empty). The caller is responsible
4243 * for taking care of those events and avoiding the call if inappropriate. The
4244 * function does not call the connection's polling update function, so the caller
4245 * is responsible for this. It's up to the caller to update the buffer's contents
4246 * based on the return value.
4247 */
4248static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4249{
4250 ssize_t ret;
4251 size_t try, done;
4252 int send_flag;
4253
4254 if (!conn_ctrl_ready(conn))
4255 return 0;
4256
4257 if (!fd_send_ready(conn->handle.fd))
4258 return 0;
4259
4260 done = 0;
4261 /* send the largest possible block. For this we perform only one call
4262 * to send() unless the buffer wraps and we exactly fill the first hunk,
4263 * in which case we accept to do it once again.
4264 */
4265 while (count) {
4266 try = b_contig_data(buf, done);
4267 if (try > count)
4268 try = count;
4269
4270 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4271 if (try < count || flags & CO_SFL_MSG_MORE)
4272 send_flag |= MSG_MORE;
4273
4274 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4275 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4276 if (ret > 0) {
4277 count -= ret;
4278 done += ret;
4279
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004280 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004281 conn->flags |= CO_FL_WAIT_L4L6;
4282 /* if the system buffer is full, don't insist */
4283 if (ret < try)
4284 break;
4285 }
4286 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4287 /* nothing written, we need to poll for write first */
4288 fd_cant_send(conn->handle.fd);
4289 break;
4290 }
4291 else if (errno != EINTR) {
4292 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4293 break;
4294 }
4295 }
4296 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4297 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4298
4299 if (done > 0) {
4300 /* we count the total bytes sent, and the send rate for 32-byte
4301 * blocks. The reason for the latter is that freq_ctr are
4302 * limited to 4GB and that it's not enough per second.
4303 */
4304 _HA_ATOMIC_ADD(&global.out_bytes, done);
4305 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4306 }
4307 return done;
4308}
4309
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004310/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4311 * event subscriber <es> is not allowed to change from a previous call as long
4312 * as at least one event is still subscribed. The <event_type> must only be a
4313 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4314 */
4315static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4316{
4317 return conn_subscribe(conn, xprt_ctx, event_type, es);
4318}
4319
4320/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4321 * The <es> pointer is not allowed to differ from the one passed to the
4322 * subscribe() call. It always returns zero.
4323 */
4324static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4325{
4326 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4327}
4328
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004329/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4330 * connection with <xprt_ctx> as address of the xprt context.
4331 * Returns 1 if succeeded, 0 if not.
4332 */
4333static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4334{
4335 struct quic_conn_ctx *ctx;
4336
4337 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4338
4339 if (*xprt_ctx)
4340 goto out;
4341
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004342 ctx = pool_alloc(pool_head_quic_conn_ctx);
4343 if (!ctx) {
4344 conn->err_code = CO_ER_SYS_MEMLIM;
4345 goto err;
4346 }
4347
4348 ctx->wait_event.tasklet = tasklet_new();
4349 if (!ctx->wait_event.tasklet) {
4350 conn->err_code = CO_ER_SYS_MEMLIM;
4351 goto err;
4352 }
4353
4354 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4355 ctx->wait_event.tasklet->context = ctx;
4356 ctx->wait_event.events = 0;
4357 ctx->conn = conn;
4358 ctx->subs = NULL;
4359 ctx->xprt_ctx = NULL;
4360
4361 ctx->xprt = xprt_get(XPRT_QUIC);
4362 if (objt_server(conn->target)) {
4363 /* Server */
4364 struct server *srv = __objt_server(conn->target);
4365 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004366 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004367 int ssl_err, ipv4;
4368
4369 ssl_err = SSL_ERROR_NONE;
4370 if (RAND_bytes(dcid, sizeof dcid) != 1)
4371 goto err;
4372
4373 conn->qc = new_quic_conn(QUIC_PROTOCOL_VERSION_DRAFT_28);
4374 if (!conn->qc)
4375 goto err;
4376
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004377 qc = conn->qc;
4378 qc->conn = conn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004379 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004380 if (!qc_new_conn_init(qc, ipv4, NULL, &srv->cids,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004381 dcid, sizeof dcid, NULL, 0, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004382 goto err;
4383
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004384 if (!qc_new_isecs(qc, dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004385 goto err;
4386
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004387 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4388 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4389 goto err;
4390
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004391 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004392 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004393 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4394 qc->enc_params_len =
4395 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4396 &qc->rx.params, 0);
4397 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004398 goto err;
4399
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004400 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004401 SSL_set_connect_state(ctx->ssl);
4402 ssl_err = SSL_do_handshake(ctx->ssl);
4403 if (ssl_err != 1) {
4404 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4405 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
4406 TRACE_PROTO("SSL handshake",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004407 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004408 }
4409 else {
4410 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004411 QUIC_EV_CONN_HDSHK, ctx->conn, &qc->state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412 goto err;
4413 }
4414 }
4415 }
4416 else if (objt_listener(conn->target)) {
4417 /* Listener */
4418 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004419 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004420
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004421 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4422 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4423 goto err;
4424
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004425 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004426 SSL_set_accept_state(ctx->ssl);
4427 }
4428
4429 *xprt_ctx = ctx;
4430
4431 /* Leave init state and start handshake */
4432 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004433
4434 out:
4435 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4436
4437 return 0;
4438
4439 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004440 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004441 tasklet_free(ctx->wait_event.tasklet);
4442 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004443 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004444 return -1;
4445}
4446
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004447/* Start the QUIC transport layer */
4448static int qc_xprt_start(struct connection *conn, void *ctx)
4449{
4450 struct quic_conn *qc;
4451 struct quic_conn_ctx *qctx = ctx;
4452
4453 qc = conn->qc;
4454 if (!quic_conn_init_timer(qc)) {
4455 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4456 return 0;
4457 }
4458
4459 tasklet_wakeup(qctx->wait_event.tasklet);
4460 return 1;
4461}
4462
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004463/* transport-layer operations for QUIC connections. */
4464static struct xprt_ops ssl_quic = {
4465 .snd_buf = quic_conn_from_buf,
4466 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004467 .subscribe = quic_conn_subscribe,
4468 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004469 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004470 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004471 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4472 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4473 .name = "QUIC",
4474};
4475
4476__attribute__((constructor))
4477static void __quic_conn_init(void)
4478{
4479 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4480 xprt_register(XPRT_QUIC, &ssl_quic);
4481}
4482
4483__attribute__((destructor))
4484static void __quic_conn_deinit(void)
4485{
4486 BIO_meth_free(ha_quic_meth);
4487}
4488
4489/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4490 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4491 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004492 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004493 */
4494static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4495 struct sockaddr_storage *saddr, qpkt_read_func *func)
4496{
4497 unsigned char *pos;
4498 const unsigned char *end;
4499 struct quic_dgram_ctx dgram_ctx = {
4500 .dcid_node = NULL,
4501 .owner = owner,
4502 };
4503
4504 pos = (unsigned char *)buf;
4505 end = pos + len;
4506
4507 do {
4508 int ret;
4509 struct quic_rx_packet *pkt;
4510
Willy Tarreaue4498932021-03-22 21:13:05 +01004511 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004512 if (!pkt)
4513 goto err;
4514
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004515 quic_rx_packet_refinc(pkt);
4516 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4517 if (ret == -1) {
4518 size_t pkt_len;
4519
4520 pkt_len = pkt->len;
4521 free_quic_rx_packet(pkt);
4522 /* If the packet length could not be found, we cannot continue. */
4523 if (!pkt_len)
4524 break;
4525 }
4526 } while (pos < end);
4527
4528 /* Increasing the received bytes counter by the UDP datagram length
4529 * if this datagram could be associated to a connection.
4530 */
4531 if (dgram_ctx.qc)
4532 dgram_ctx.qc->rx.bytes += len;
4533
4534 return pos - (unsigned char *)buf;
4535
4536 err:
4537 return -1;
4538}
4539
4540ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4541 struct sockaddr_storage *saddr)
4542{
4543 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4544}
4545
4546ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4547 struct sockaddr_storage *saddr)
4548{
4549 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4550}
4551
4552/* QUIC I/O handler for connection to local listeners or remove servers
4553 * depending on <listener> boolean value, with <fd> as socket file
4554 * descriptor and <ctx> as context.
4555 */
4556static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4557{
4558 ssize_t ret;
4559 size_t done = 0;
4560 struct buffer *buf = get_trash_chunk();
4561 /* Source address */
4562 struct sockaddr_storage saddr = {0};
4563 socklen_t saddrlen = sizeof saddr;
4564
4565 if (!fd_recv_ready(fd))
4566 return 0;
4567
4568 do {
4569 ret = recvfrom(fd, buf->area, buf->size, 0,
4570 (struct sockaddr *)&saddr, &saddrlen);
4571 if (ret < 0) {
4572 if (errno == EINTR)
4573 continue;
4574 if (errno == EAGAIN)
4575 fd_cant_recv(fd);
4576 goto out;
4577 }
4578 } while (0);
4579
4580 done = buf->data = ret;
4581 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4582
4583 out:
4584 return done;
4585}
4586
4587/* QUIC I/O handler for connections to local listeners with <fd> as socket
4588 * file descriptor.
4589 */
4590void quic_fd_handler(int fd)
4591{
Willy Tarreauf5090652021-04-06 17:23:40 +02004592 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004593 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4594}
4595
4596/* QUIC I/O handler for connections to remote servers with <fd> as socket
4597 * file descriptor.
4598 */
4599void quic_conn_fd_handler(int fd)
4600{
Willy Tarreauf5090652021-04-06 17:23:40 +02004601 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004602 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4603}
4604
4605/*
4606 * Local variables:
4607 * c-indent-level: 8
4608 * c-basic-offset: 8
4609 * End:
4610 */