2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/quic_channel.h"
11 #include "internal/quic_error.h"
12 #include "internal/quic_rx_depack.h"
13 #include "../ssl_local.h"
14 #include "quic_channel_local.h"
15 #include <openssl/rand.h>
17 #define INIT_DCID_LEN 8
18 #define INIT_CRYPTO_BUF_LEN 8192
19 #define INIT_APP_BUF_LEN 8192
21 static int ch_rx(QUIC_CHANNEL *ch);
22 static int ch_tx(QUIC_CHANNEL *ch);
23 static void ch_tick(QUIC_TICK_RESULT *res, void *arg);
24 static void ch_rx_handle_packet(QUIC_CHANNEL *ch);
25 static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch);
26 static int ch_retry(QUIC_CHANNEL *ch,
27 const unsigned char *retry_token,
28 size_t retry_token_len,
29 const QUIC_CONN_ID *retry_scid);
30 static void ch_cleanup(QUIC_CHANNEL *ch);
31 static int ch_generate_transport_params(QUIC_CHANNEL *ch);
32 static int ch_on_transport_params(const unsigned char *params,
35 static int ch_on_handshake_alert(void *arg, unsigned char alert_code);
36 static int ch_on_handshake_complete(void *arg);
37 static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction,
38 uint32_t suite_id, EVP_MD *md,
39 const unsigned char *secret,
42 static int ch_on_crypto_recv(unsigned char *buf, size_t buf_len,
43 size_t *bytes_read, void *arg);
44 static int crypto_ensure_empty(QUIC_RSTREAM *rstream);
45 static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len,
46 size_t *consumed, void *arg);
47 static OSSL_TIME get_time(void *arg);
48 static uint64_t get_stream_limit(int uni, void *arg);
49 static int rx_early_validate(QUIC_PN pn, int pn_space, void *arg);
50 static int ch_retry(QUIC_CHANNEL *ch,
51 const unsigned char *retry_token,
52 size_t retry_token_len,
53 const QUIC_CONN_ID *retry_scid);
54 static void ch_update_idle(QUIC_CHANNEL *ch);
55 static int ch_discard_el(QUIC_CHANNEL *ch,
57 static void ch_on_idle_timeout(QUIC_CHANNEL *ch);
58 static void ch_update_idle(QUIC_CHANNEL *ch);
59 static void ch_on_terminating_timeout(QUIC_CHANNEL *ch);
60 static void ch_start_terminating(QUIC_CHANNEL *ch,
61 const QUIC_TERMINATE_CAUSE *tcause);
63 static int gen_rand_conn_id(OSSL_LIB_CTX *libctx, size_t len, QUIC_CONN_ID *cid)
65 if (len > QUIC_MAX_CONN_ID_LEN)
68 cid->id_len = (unsigned char)len;
70 if (RAND_bytes_ex(libctx, cid->id, len, len * 8) != 1) {
79 * QUIC Channel Initialization and Teardown
80 * ========================================
82 static int ch_init(QUIC_CHANNEL *ch)
84 OSSL_QUIC_TX_PACKETISER_ARGS txp_args = {0};
85 OSSL_QTX_ARGS qtx_args = {0};
86 OSSL_QRX_ARGS qrx_args = {0};
87 QUIC_DHS_ARGS dhs_args = {0};
90 /* TODO(QUIC): This is only applicable to clients. */
91 if (!gen_rand_conn_id(ch->libctx, INIT_DCID_LEN, &ch->init_dcid))
94 /* We plug in a network write BIO to the QTX later when we get one. */
95 qtx_args.mdpl = QUIC_MIN_INITIAL_DGRAM_LEN;
96 ch->rx_max_udp_payload_size = qtx_args.mdpl;
98 ch->qtx = ossl_qtx_new(&qtx_args);
102 ch->txpim = ossl_quic_txpim_new();
103 if (ch->txpim == NULL)
106 ch->cfq = ossl_quic_cfq_new();
110 if (!ossl_quic_txfc_init(&ch->conn_txfc, NULL))
113 if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL,
119 if (!ossl_statm_init(&ch->statm))
123 ch->cc_method = &ossl_cc_dummy_method;
124 if ((ch->cc_data = ch->cc_method->new(NULL, NULL, NULL)) == NULL)
127 if ((ch->ackm = ossl_ackm_new(get_time, NULL, &ch->statm,
128 ch->cc_method, ch->cc_data)) == NULL)
131 if (!ossl_quic_stream_map_init(&ch->qsm, get_stream_limit, ch))
136 /* We use a zero-length SCID. */
137 txp_args.cur_dcid = ch->init_dcid;
138 txp_args.ack_delay_exponent = 3;
139 txp_args.qtx = ch->qtx;
140 txp_args.txpim = ch->txpim;
141 txp_args.cfq = ch->cfq;
142 txp_args.ackm = ch->ackm;
143 txp_args.qsm = &ch->qsm;
144 txp_args.conn_txfc = &ch->conn_txfc;
145 txp_args.conn_rxfc = &ch->conn_rxfc;
146 txp_args.cc_method = ch->cc_method;
147 txp_args.cc_data = ch->cc_data;
148 txp_args.now = get_time;
149 for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
150 ch->crypto_send[pn_space] = ossl_quic_sstream_new(INIT_CRYPTO_BUF_LEN);
151 if (ch->crypto_send[pn_space] == NULL)
154 txp_args.crypto[pn_space] = ch->crypto_send[pn_space];
157 ch->txp = ossl_quic_tx_packetiser_new(&txp_args);
161 if ((ch->demux = ossl_quic_demux_new(/*BIO=*/NULL, /*Short CID Len=*/0,
162 1200, get_time, NULL)) == NULL)
165 qrx_args.demux = ch->demux;
166 qrx_args.short_conn_id_len = 0; /* We use a zero-length SCID. */
167 qrx_args.max_deferred = 32;
169 if ((ch->qrx = ossl_qrx_new(&qrx_args)) == NULL)
172 if (!ossl_qrx_set_early_validation_cb(ch->qrx,
177 if (!ossl_qrx_add_dst_conn_id(ch->qrx, &txp_args.cur_scid))
180 for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
181 ch->crypto_recv[pn_space] = ossl_quic_rstream_new(NULL, NULL);
182 if (ch->crypto_recv[pn_space] == NULL)
186 if ((ch->stream0 = ossl_quic_stream_map_alloc(&ch->qsm, 0,
187 QUIC_STREAM_INITIATOR_CLIENT
188 | QUIC_STREAM_DIR_BIDI)) == NULL)
191 if ((ch->stream0->sstream = ossl_quic_sstream_new(INIT_APP_BUF_LEN)) == NULL)
194 if ((ch->stream0->rstream = ossl_quic_rstream_new(NULL, NULL)) == NULL)
197 if (!ossl_quic_txfc_init(&ch->stream0->txfc, &ch->conn_txfc))
200 if (!ossl_quic_rxfc_init(&ch->stream0->rxfc, &ch->conn_rxfc,
206 /* Plug in the dummy handshake layer. */
207 dhs_args.crypto_send_cb = ch_on_crypto_send;
208 dhs_args.crypto_send_cb_arg = ch;
209 dhs_args.crypto_recv_cb = ch_on_crypto_recv;
210 dhs_args.crypto_recv_cb_arg = ch;
211 dhs_args.yield_secret_cb = ch_on_handshake_yield_secret;
212 dhs_args.yield_secret_cb_arg = ch;
213 dhs_args.got_transport_params_cb = ch_on_transport_params;
214 dhs_args.got_transport_params_cb_arg= ch;
215 dhs_args.handshake_complete_cb = ch_on_handshake_complete;
216 dhs_args.handshake_complete_cb_arg = ch;
217 dhs_args.alert_cb = ch_on_handshake_alert;
218 dhs_args.alert_cb_arg = ch;
220 if ((ch->dhs = ossl_quic_dhs_new(&dhs_args)) == NULL)
224 * Determine the QUIC Transport Parameters and serialize the transport
225 * parameters block. (For servers, we do this later as we must defer
226 * generation until we have received the client's transport parameters.)
228 if (!ch->is_server && !ch_generate_transport_params(ch))
231 ch->rx_max_ack_delay = QUIC_DEFAULT_MAX_ACK_DELAY;
232 ch->rx_ack_delay_exp = QUIC_DEFAULT_ACK_DELAY_EXP;
233 ch->rx_active_conn_id_limit = QUIC_MIN_ACTIVE_CONN_ID_LIMIT;
234 ch->max_idle_timeout = QUIC_DEFAULT_IDLE_TIMEOUT;
235 ch->tx_enc_level = QUIC_ENC_LEVEL_INITIAL;
237 ossl_quic_reactor_init(&ch->rtor, ch_tick, ch,
238 ch_determine_next_tick_deadline(ch));
246 static void ch_cleanup(QUIC_CHANNEL *ch)
250 if (ch->ackm != NULL)
251 for (pn_space = QUIC_PN_SPACE_INITIAL;
252 pn_space < QUIC_PN_SPACE_NUM;
254 ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space);
256 ossl_quic_tx_packetiser_free(ch->txp);
257 ossl_quic_txpim_free(ch->txpim);
258 ossl_quic_cfq_free(ch->cfq);
259 ossl_qtx_free(ch->qtx);
260 if (ch->cc_data != NULL)
261 ch->cc_method->free(ch->cc_data);
263 ossl_statm_destroy(&ch->statm);
264 ossl_ackm_free(ch->ackm);
266 if (ch->stream0 != NULL) {
267 assert(ch->have_qsm);
268 ossl_quic_stream_map_release(&ch->qsm, ch->stream0); /* frees sstream */
272 ossl_quic_stream_map_cleanup(&ch->qsm);
274 for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
275 ossl_quic_sstream_free(ch->crypto_send[pn_space]);
276 ossl_quic_rstream_free(ch->crypto_recv[pn_space]);
279 ossl_qrx_pkt_release(ch->qrx_pkt);
282 ossl_quic_dhs_free(ch->dhs);
283 ossl_qrx_free(ch->qrx);
284 ossl_quic_demux_free(ch->demux);
285 OPENSSL_free(ch->local_transport_params);
286 BIO_free(ch->net_rbio);
287 BIO_free(ch->net_wbio);
290 QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args)
292 QUIC_CHANNEL *ch = NULL;
294 if ((ch = OPENSSL_zalloc(sizeof(*ch))) == NULL)
297 ch->libctx = args->libctx;
298 ch->propq = args->propq;
299 ch->is_server = args->is_server;
309 void ossl_quic_channel_free(QUIC_CHANNEL *ch)
318 int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr)
320 *peer_addr = ch->cur_peer_addr;
324 int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr)
326 ch->cur_peer_addr = *peer_addr;
330 QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch)
335 QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch)
340 OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch)
345 QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch,
348 return ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
351 int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch)
353 return ch != NULL && ch->state == QUIC_CHANNEL_STATE_ACTIVE;
356 int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch)
358 return ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING
359 || ch->state == QUIC_CHANNEL_STATE_TERMINATING_DRAINING;
362 int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch)
364 return ch->state == QUIC_CHANNEL_STATE_TERMINATED;
367 int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch)
369 return ossl_quic_channel_is_terminating(ch)
370 || ossl_quic_channel_is_terminated(ch);
373 int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch)
375 return ch->handshake_complete;
379 * QUIC Channel: Callbacks from Miscellaneous Subsidiary Components
380 * ================================================================
383 /* Used by various components. */
384 static OSSL_TIME get_time(void *arg)
386 return ossl_time_now();
390 static uint64_t get_stream_limit(int uni, void *arg)
392 QUIC_CHANNEL *ch = arg;
394 return uni ? ch->max_local_streams_uni : ch->max_local_streams_bidi;
398 * Called by QRX to determine if a packet is potentially invalid before trying
401 static int rx_early_validate(QUIC_PN pn, int pn_space, void *arg)
403 QUIC_CHANNEL *ch = arg;
405 /* Potential duplicates should not be processed. */
406 if (!ossl_ackm_is_rx_pn_processable(ch->ackm, pn, pn_space))
413 * QUIC Channel: Handshake Layer Event Handling
414 * ============================================
416 static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len,
417 size_t *consumed, void *arg)
420 QUIC_CHANNEL *ch = arg;
421 uint32_t enc_level = ch->tx_enc_level;
422 uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
423 QUIC_SSTREAM *sstream = ch->crypto_send[pn_space];
425 if (!ossl_assert(sstream != NULL))
428 ret = ossl_quic_sstream_append(sstream, buf, buf_len, consumed);
432 static int crypto_ensure_empty(QUIC_RSTREAM *rstream)
440 if (!ossl_quic_rstream_available(rstream, &avail, &is_fin))
446 static int ch_on_crypto_recv(unsigned char *buf, size_t buf_len,
447 size_t *bytes_read, void *arg)
449 QUIC_CHANNEL *ch = arg;
450 QUIC_RSTREAM *rstream;
451 int is_fin = 0; /* crypto stream is never finished, so we don't use this */
455 * After we move to a later EL we must not allow our peer to send any new
456 * bytes in the crypto stream on a previous EL. Retransmissions of old bytes
459 * In practice we will only move to a new EL when we have consumed all bytes
460 * which should be sent on the crypto stream at a previous EL. For example,
461 * the Handshake EL should not be provisioned until we have completely
462 * consumed a TLS 1.3 ServerHello. Thus when we provision an EL the output
463 * of ossl_quic_rstream_available() should be 0 for all lower ELs. Thus if a
464 * given EL is available we simply ensure we have not received any further
465 * bytes at a lower EL.
467 for (i = QUIC_ENC_LEVEL_INITIAL; i < ch->tx_enc_level; ++i)
468 if (i != QUIC_ENC_LEVEL_0RTT &&
469 !crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) {
470 /* Protocol violation (RFC 9001 s. 4.1.3) */
471 ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION,
472 OSSL_QUIC_FRAME_TYPE_CRYPTO,
473 "crypto stream data in wrong EL");
477 rstream = ch->crypto_recv[ossl_quic_enc_level_to_pn_space(ch->tx_enc_level)];
481 return ossl_quic_rstream_read(rstream, buf, buf_len, bytes_read,
485 static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction,
486 uint32_t suite_id, EVP_MD *md,
487 const unsigned char *secret,
491 QUIC_CHANNEL *ch = arg;
494 if (enc_level < QUIC_ENC_LEVEL_HANDSHAKE || enc_level >= QUIC_ENC_LEVEL_NUM)
498 if (enc_level <= ch->tx_enc_level)
500 * Does not make sense for us to try and provision an EL we have already
506 * Ensure all crypto streams for previous ELs are now empty of available
509 for (i = QUIC_ENC_LEVEL_INITIAL; i < enc_level; ++i)
510 if (!crypto_ensure_empty(ch->crypto_recv[i])) {
511 /* Protocol violation (RFC 9001 s. 4.1.3) */
512 ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION,
513 OSSL_QUIC_FRAME_TYPE_CRYPTO,
514 "crypto stream data in wrong EL");
520 if (!ossl_qtx_provide_secret(ch->qtx, enc_level,
525 ch->tx_enc_level = enc_level;
528 if (!ossl_qrx_provide_secret(ch->qrx, enc_level,
537 static int ch_on_handshake_complete(void *arg)
539 QUIC_CHANNEL *ch = arg;
541 if (ch->handshake_complete)
542 return 0; /* this should not happen twice */
544 if (!ossl_assert(ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT))
547 if (!ch->got_remote_transport_params)
549 * Was not a valid QUIC handshake if we did not get valid transport
554 /* Don't need transport parameters anymore. */
555 OPENSSL_free(ch->local_transport_params);
556 ch->local_transport_params = NULL;
558 /* Tell TXP the handshake is complete. */
559 ossl_quic_tx_packetiser_notify_handshake_complete(ch->txp);
561 ch->handshake_complete = 1;
565 static int ch_on_handshake_alert(void *arg, unsigned char alert_code)
567 QUIC_CHANNEL *ch = arg;
569 ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_CRYPTO_ERR_BEGIN + alert_code,
570 0, "handshake alert");
575 * QUIC Channel: Transport Parameter Handling
576 * ==========================================
580 * Called by handshake layer when we receive QUIC Transport Parameters from the
581 * peer. Note that these are not authenticated until the handshake is marked
584 static int ch_on_transport_params(const unsigned char *params,
588 QUIC_CHANNEL *ch = arg;
592 const unsigned char *body;
593 int got_orig_dcid = 0;
594 int got_initial_scid = 0;
595 int got_retry_scid = 0;
596 int got_initial_max_data = 0;
597 int got_initial_max_stream_data_bidi_local = 0;
598 int got_initial_max_stream_data_bidi_remote = 0;
599 int got_initial_max_stream_data_uni = 0;
600 int got_initial_max_streams_bidi = 0;
601 int got_initial_max_streams_uni = 0;
602 int got_ack_delay_exp = 0;
603 int got_max_ack_delay = 0;
604 int got_max_udp_payload_size = 0;
605 int got_max_idle_timeout = 0;
606 int got_active_conn_id_limit = 0;
609 if (ch->got_remote_transport_params)
612 if (!PACKET_buf_init(&pkt, params, params_len))
615 while (PACKET_remaining(&pkt) > 0) {
616 if (!ossl_quic_wire_peek_transport_param(&pkt, &id))
620 case QUIC_TPARAM_ORIG_DCID:
622 /* must not appear more than once */
625 if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid))
628 /* Must match our initial DCID. */
629 if (!ossl_quic_conn_id_eq(&ch->init_dcid, &cid))
635 case QUIC_TPARAM_RETRY_SCID:
636 if (got_retry_scid || !ch->doing_retry)
637 /* must not appear more than once or if retry not done */
640 if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid))
643 /* Must match Retry packet SCID. */
644 if (!ossl_quic_conn_id_eq(&ch->retry_scid, &cid))
650 case QUIC_TPARAM_INITIAL_SCID:
651 if (got_initial_scid)
652 /* must not appear more than once */
655 if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid))
658 /* Must match SCID of first Initial packet from server. */
659 if (!ossl_quic_conn_id_eq(&ch->init_scid, &cid))
662 got_initial_scid = 1;
665 case QUIC_TPARAM_INITIAL_MAX_DATA:
666 if (got_initial_max_data)
667 /* must not appear more than once */
670 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
673 ossl_quic_txfc_bump_cwm(&ch->conn_txfc, v);
674 got_initial_max_data = 1;
677 case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL:
678 if (got_initial_max_stream_data_bidi_local)
679 /* must not appear more than once */
682 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
686 * This is correct; the BIDI_LOCAL TP governs streams created by
687 * the endpoint which sends the TP, i.e., our peer.
689 ch->init_max_stream_data_bidi_remote = v;
690 got_initial_max_stream_data_bidi_local = 1;
693 case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE:
694 if (got_initial_max_stream_data_bidi_remote)
695 /* must not appear more than once */
698 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
702 * This is correct; the BIDI_REMOTE TP governs streams created
703 * by the endpoint which receives the TP, i.e., us.
705 ch->init_max_stream_data_bidi_local = v;
707 /* Apply to stream 0. */
708 ossl_quic_txfc_bump_cwm(&ch->stream0->txfc, v);
709 got_initial_max_stream_data_bidi_remote = 1;
712 case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI:
713 if (got_initial_max_stream_data_uni)
714 /* must not appear more than once */
717 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
720 ch->init_max_stream_data_uni_remote = v;
721 got_initial_max_stream_data_uni = 1;
724 case QUIC_TPARAM_ACK_DELAY_EXP:
725 if (got_ack_delay_exp)
726 /* must not appear more than once */
729 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
730 || v > QUIC_MAX_ACK_DELAY_EXP)
733 ch->rx_ack_delay_exp = (unsigned char)v;
734 got_ack_delay_exp = 1;
737 case QUIC_TPARAM_MAX_ACK_DELAY:
738 if (got_max_ack_delay)
739 /* must not appear more than once */
742 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
743 || v >= (((uint64_t)1) << 14))
746 ch->rx_max_ack_delay = v;
747 got_max_ack_delay = 1;
750 case QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI:
751 if (got_initial_max_streams_bidi)
752 /* must not appear more than once */
755 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
756 || v > (((uint64_t)1) << 60))
759 assert(ch->max_local_streams_bidi == 0);
760 ch->max_local_streams_bidi = v;
761 got_initial_max_streams_bidi = 1;
764 case QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI:
765 if (got_initial_max_streams_uni)
766 /* must not appear more than once */
769 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
770 || v > (((uint64_t)1) << 60))
773 assert(ch->max_local_streams_uni == 0);
774 ch->max_local_streams_uni = v;
775 got_initial_max_streams_uni = 1;
778 case QUIC_TPARAM_MAX_IDLE_TIMEOUT:
779 if (got_max_idle_timeout)
780 /* must not appear more than once */
783 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
786 if (v < ch->max_idle_timeout)
787 ch->max_idle_timeout = v;
790 got_max_idle_timeout = 1;
793 case QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE:
794 if (got_max_udp_payload_size)
795 /* must not appear more than once */
798 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
799 || v < QUIC_MIN_INITIAL_DGRAM_LEN)
802 ch->rx_max_udp_payload_size = v;
803 got_max_udp_payload_size = 1;
806 case QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT:
807 if (got_active_conn_id_limit)
808 /* must not appear more than once */
811 if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
812 || v < QUIC_MIN_ACTIVE_CONN_ID_LIMIT)
815 ch->rx_active_conn_id_limit = v;
816 got_active_conn_id_limit = 1;
820 * TODO(QUIC): Handle:
821 * QUIC_TPARAM_STATELESS_RESET_TOKEN
822 * QUIC_TPARAM_PREFERRED_ADDR
825 case QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION:
826 /* We do not currently handle migration, so nothing to do. */
828 /* Skip over and ignore. */
829 body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id,
838 if (!got_orig_dcid || !got_initial_scid || got_retry_scid != ch->doing_retry)
839 /* Transport parameters were not valid. */
842 ch->got_remote_transport_params = 1;
844 if (got_initial_max_data || got_initial_max_stream_data_bidi_remote
845 || got_initial_max_streams_bidi || got_initial_max_streams_uni)
846 /* If FC credit was bumped, we may now be able to send. */
847 ossl_quic_stream_map_update_state(&ch->qsm, ch->stream0);
849 /* If we are a server, we now generate our own transport parameters. */
850 if (ch->is_server && !ch_generate_transport_params(ch)) {
851 ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0,
859 ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_TRANSPORT_PARAMETER_ERROR,
860 0, "bad transport parameter");
865 * Called when we want to generate transport parameters. This is called
866 * immediately at instantiation time for a client and after we receive the
867 * client's transport parameters for a server.
869 static int ch_generate_transport_params(QUIC_CHANNEL *ch)
872 BUF_MEM *buf_mem = NULL;
877 if (ch->local_transport_params != NULL)
880 if ((buf_mem = BUF_MEM_new()) == NULL)
883 if (!WPACKET_init(&wpkt, buf_mem))
888 if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION,
892 if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_INITIAL_SCID,
896 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_IDLE_TIMEOUT,
897 ch->max_idle_timeout))
900 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE,
901 QUIC_MIN_INITIAL_DGRAM_LEN))
904 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT,
908 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_DATA,
909 ossl_quic_rxfc_get_cwm(&ch->conn_rxfc)))
913 * We actually want the default CWM for a new RXFC, but here we just use
914 * stream0 as a representative specimen. TODO(QUIC): revisit this when we
915 * support multiple streams.
917 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL,
918 ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc)))
921 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE,
922 ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc)))
925 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI,
926 ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc)))
929 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI,
933 if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI,
937 if (!WPACKET_get_total_written(&wpkt, &buf_len))
940 ch->local_transport_params = (unsigned char *)buf_mem->data;
941 buf_mem->data = NULL;
943 if (!WPACKET_finish(&wpkt))
948 if (!ossl_quic_dhs_set_transport_params(ch->dhs, ch->local_transport_params,
955 WPACKET_cleanup(&wpkt);
956 BUF_MEM_free(buf_mem);
961 * QUIC Channel: Ticker-Mutator
962 * ============================
966 * The central ticker function called by the reactor. This does everything, or
967 * at least everything network I/O related. Best effort - not allowed to fail
970 static void ch_tick(QUIC_TICK_RESULT *res, void *arg)
972 OSSL_TIME now, deadline;
973 QUIC_CHANNEL *ch = arg;
976 * When we tick the QUIC connection, we do everything we need to do
977 * periodically. In order, we:
979 * - handle any incoming data from the network;
980 * - handle any timer events which are due to fire (ACKM, etc.)
981 * - write any data to the network due to be sent, to the extent
983 * - determine the time at which we should next be ticked.
986 /* If we are in the TERMINATED state, there is nothing to do. */
987 if (ossl_quic_channel_is_terminated(ch)) {
988 res->want_net_read = 0;
989 res->want_net_write = 0;
990 res->tick_deadline = ossl_time_infinite();
995 * If we are in the TERMINATING state, check if the terminating timer has
998 if (ossl_quic_channel_is_terminating(ch)) {
999 now = ossl_time_now();
1001 if (ossl_time_compare(now, ch->terminate_deadline) >= 0) {
1002 ch_on_terminating_timeout(ch);
1003 res->want_net_read = 0;
1004 res->want_net_write = 0;
1005 res->tick_deadline = ossl_time_infinite();
1006 return; /* abort normal processing, nothing to do */
1010 /* Handle any incoming data from the network. */
1014 * Allow the handshake layer to check for any new incoming data and generate
1015 * new outgoing data.
1017 ossl_quic_dhs_tick(ch->dhs);
1020 * Handle any timer events which are due to fire; namely, the loss detection
1021 * deadline and the idle timeout.
1023 * ACKM ACK generation deadline is polled by TXP, so we don't need to handle
1026 now = ossl_time_now();
1027 if (ossl_time_compare(now, ch->idle_deadline) >= 0) {
1029 * Idle timeout differs from normal protocol violation because we do not
1030 * send a CONN_CLOSE frame; go straight to TERMINATED.
1032 ch_on_idle_timeout(ch);
1033 res->want_net_read = 0;
1034 res->want_net_write = 0;
1035 res->tick_deadline = ossl_time_infinite();
1039 deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm);
1040 if (!ossl_time_is_zero(deadline) && ossl_time_compare(now, deadline) >= 0)
1041 ossl_ackm_on_timeout(ch->ackm);
1043 /* Write any data to the network due to be sent. */
1046 /* Determine the time at which we should next be ticked. */
1047 res->tick_deadline = ch_determine_next_tick_deadline(ch);
1049 /* Always process network input. */
1050 res->want_net_read = 1;
1052 /* We want to write to the network if we have any in our queue. */
1053 res->want_net_write = (ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0);
1056 /* Process incoming packets and handle frames, if any. */
1057 static int ch_rx(QUIC_CHANNEL *ch)
1059 int handled_any = 0;
1061 if (!ch->have_sent_any_pkt)
1063 * We have not sent anything yet, therefore there is no need to check
1064 * for incoming data. TODO SERVER
1069 * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams
1070 * to the appropriate QRX instance.
1072 ossl_quic_demux_pump(ch->demux); /* best effort */
1075 assert(ch->qrx_pkt == NULL);
1077 if (!ossl_qrx_read_pkt(ch->qrx, &ch->qrx_pkt))
1083 ch_rx_handle_packet(ch); /* best effort */
1086 * Regardless of the outcome of frame handling, unref the packet.
1087 * This will free the packet unless something added another
1088 * reference to it during frame processing.
1090 ossl_qrx_pkt_release(ch->qrx_pkt);
1097 * When in TERMINATING - CLOSING, generate a CONN_CLOSE frame whenever we
1098 * process one or more incoming packets.
1100 if (handled_any && ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING)
1101 ch->conn_close_queued = 1;
1106 /* Handles the packet currently in ch->qrx_pkt->hdr. */
1107 static void ch_rx_handle_packet(QUIC_CHANNEL *ch)
1111 assert(ch->qrx_pkt != NULL);
1113 if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)) {
1114 if (!ch->have_received_enc_pkt) {
1115 ch->init_scid = ch->qrx_pkt->hdr->src_conn_id;
1116 ch->have_received_enc_pkt = 1;
1119 * We change to using the SCID in the first Initial packet as the
1122 ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->init_scid);
1125 enc_level = ossl_quic_pkt_type_to_enc_level(ch->qrx_pkt->hdr->type);
1126 if ((ch->el_discarded & (1U << enc_level)) != 0)
1127 /* Do not process packets from ELs we have already discarded. */
1131 /* Handle incoming packet. */
1132 switch (ch->qrx_pkt->hdr->type) {
1133 case QUIC_PKT_TYPE_RETRY:
1134 if (ch->doing_retry)
1135 /* It is not allowed to ask a client to do a retry more than
1139 /* TODO if server */
1141 if (ch->qrx_pkt->hdr->len <= QUIC_RETRY_INTEGRITY_TAG_LEN)
1142 /* Packets with zero-length Retry Tokens are invalid. */
1146 * TODO(QUIC): Theoretically this should probably be in the QRX.
1147 * However because validation is dependent on context (namely the
1148 * client's initial DCID) we can't do this cleanly. In the future we
1149 * should probably add a callback to the QRX to let it call us (via
1150 * the DEMUX) and ask us about the correct original DCID, rather
1151 * than allow the QRX to emit a potentially malformed packet to the
1152 * upper layers. However, special casing this will do for now.
1154 if (!ossl_quic_validate_retry_integrity_tag(ch->libctx,
1158 /* Malformed retry packet, ignore. */
1161 ch_retry(ch, ch->qrx_pkt->hdr->data,
1162 ch->qrx_pkt->hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN,
1163 &ch->qrx_pkt->hdr->src_conn_id);
1166 case QUIC_PKT_TYPE_VERSION_NEG:
1167 /* TODO(QUIC): Implement version negotiation */
1170 case QUIC_PKT_TYPE_0RTT:
1171 /* TODO if server */
1172 /* Clients should never receive 0-RTT packets */
1176 if (ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE)
1178 * We automatically drop INITIAL EL keys when first successfully
1179 * decrypting a HANDSHAKE packet, as per the RFC.
1181 ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL);
1183 /* This packet contains frames, pass to the RXDP. */
1184 ossl_quic_handle_frames(ch, ch->qrx_pkt); /* best effort */
1189 /* Try to generate packets and if possible, flush them to the network. */
1190 static int ch_tx(QUIC_CHANNEL *ch)
1192 if (ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING) {
1194 * While closing, only send CONN_CLOSE if we've received more traffic
1195 * from the peer. Once we tell the TXP to generate CONN_CLOSE, all
1196 * future calls to it generate CONN_CLOSE frames, so otherwise we would
1197 * just constantly generate CONN_CLOSE frames.
1199 if (!ch->conn_close_queued)
1202 ch->conn_close_queued = 0;
1206 * Send a packet, if we need to. Best effort. The TXP consults the CC and
1207 * applies any limitations imposed by it, so we don't need to do it here.
1209 * Best effort. In particular if TXP fails for some reason we should still
1210 * flush any queued packets which we already generated.
1212 if (ossl_quic_tx_packetiser_generate(ch->txp,
1213 TX_PACKETISER_ARCHETYPE_NORMAL)
1214 == TX_PACKETISER_RES_SENT_PKT)
1215 ch->have_sent_any_pkt = 1;
1217 ossl_qtx_flush_net(ch->qtx); /* best effort */
1221 /* Determine next tick deadline. */
1222 static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch)
1227 deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm);
1228 if (ossl_time_is_zero(deadline))
1229 deadline = ossl_time_infinite();
1231 for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
1232 deadline = ossl_time_min(deadline,
1233 ossl_ackm_get_ack_deadline(ch->ackm, pn_space));
1235 /* When will CC let us send more? */
1236 if (ossl_quic_tx_packetiser_has_pending(ch->txp, TX_PACKETISER_ARCHETYPE_NORMAL,
1237 TX_PACKETISER_BYPASS_CC))
1238 deadline = ossl_time_min(deadline,
1239 ch->cc_method->get_next_credit_time(ch->cc_data));
1241 /* Is the terminating timer armed? */
1242 if (ossl_quic_channel_is_terminating(ch))
1243 deadline = ossl_time_min(deadline,
1244 ch->terminate_deadline);
1245 else if (!ossl_time_is_infinite(ch->idle_deadline))
1246 deadline = ossl_time_min(deadline,
1253 * QUIC Channel: Network BIO Configuration
1254 * =======================================
1257 /* Determines whether we can support a given poll descriptor. */
1258 static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR *d)
1260 if (d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD && d->value.fd < 0)
1266 BIO *ossl_quic_channel_get_net_rbio(QUIC_CHANNEL *ch)
1268 return ch->net_rbio;
1271 BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch)
1273 return ch->net_wbio;
1276 int ossl_quic_channel_set0_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio)
1278 BIO_POLL_DESCRIPTOR d = {0};
1280 if (ch->net_rbio == net_rbio)
1283 if (net_rbio != NULL) {
1284 if (!BIO_get_rpoll_descriptor(net_rbio, &d))
1285 /* Non-pollable BIO */
1286 d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
1288 if (!validate_poll_descriptor(&d))
1292 ossl_quic_reactor_set_poll_r(&ch->rtor, &d);
1293 BIO_free(ch->net_rbio);
1294 ossl_quic_demux_set_bio(ch->demux, net_rbio);
1295 ch->net_rbio = net_rbio;
1299 int ossl_quic_channel_set0_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio)
1301 BIO_POLL_DESCRIPTOR d = {0};
1303 if (ch->net_wbio == net_wbio)
1306 if (net_wbio != NULL) {
1307 if (!BIO_get_wpoll_descriptor(net_wbio, &d))
1308 /* Non-pollable BIO */
1309 d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
1311 if (!validate_poll_descriptor(&d))
1315 ossl_quic_reactor_set_poll_w(&ch->rtor, &d);
1316 BIO_free(ch->net_wbio);
1317 ossl_qtx_set_bio(ch->qtx, net_wbio);
1318 ch->net_wbio = net_wbio;
1323 * QUIC Channel: Lifecycle Events
1324 * ==============================
1327 int ossl_quic_channel_start(QUIC_CHANNEL *ch)
1330 if (ch->state != QUIC_CHANNEL_STATE_IDLE)
1331 /* Calls to connect are idempotent */
1334 /* Inform QTX of peer address. */
1335 if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr))
1338 /* Plug in secrets for the Initial EL. */
1339 if (!ossl_quic_provide_initial_secret(ch->libctx,
1347 ch->state = QUIC_CHANNEL_STATE_ACTIVE;
1348 ch->doing_proactive_ver_neg = 0; /* not currently supported */
1350 /* Handshake layer: start (e.g. send CH). */
1351 if (!ossl_quic_dhs_tick(ch->dhs))
1354 ossl_quic_reactor_tick(&ch->rtor); /* best effort */
1358 /* Start a locally initiated connection shutdown. */
1359 void ossl_quic_channel_local_close(QUIC_CHANNEL *ch)
1361 QUIC_TERMINATE_CAUSE tcause = {0};
1363 if (ossl_quic_channel_is_term_any(ch))
1367 ch_start_terminating(ch, &tcause);
1370 static void free_token(const unsigned char *buf, size_t buf_len, void *arg)
1372 OPENSSL_free((unsigned char *)buf);
1375 /* Called when a server asks us to do a retry. */
1376 static int ch_retry(QUIC_CHANNEL *ch,
1377 const unsigned char *retry_token,
1378 size_t retry_token_len,
1379 const QUIC_CONN_ID *retry_scid)
1383 /* We change to using the SCID in the Retry packet as the DCID. */
1384 if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, retry_scid))
1388 * Now we retry. We will release the Retry packet immediately, so copy
1391 if ((buf = OPENSSL_malloc(retry_token_len)) == NULL)
1394 memcpy(buf, retry_token, retry_token_len);
1396 ossl_quic_tx_packetiser_set_initial_token(ch->txp, buf, retry_token_len,
1399 ch->retry_scid = *retry_scid;
1400 ch->doing_retry = 1;
1403 * We need to stimulate the Initial EL to generate the first CRYPTO frame
1404 * again. We can do this most cleanly by simply forcing the ACKM to consider
1405 * the first Initial packet as lost, which it effectively was as the server
1406 * hasn't processed it. This also maintains the desired behaviour with e.g.
1407 * PNs not resetting and so on.
1409 * The PN we used initially is always zero, because QUIC does not allow
1412 if (!ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL,
1417 * Plug in new secrets for the Initial EL. This is the only time we change
1418 * the secrets for an EL after we already provisioned it.
1420 if (!ossl_quic_provide_initial_secret(ch->libctx,
1430 /* Called when an EL is to be discarded. */
1431 static int ch_discard_el(QUIC_CHANNEL *ch,
1434 if (!ossl_assert(enc_level < QUIC_ENC_LEVEL_1RTT))
1437 if ((ch->el_discarded & (1U << enc_level)) != 0)
1441 /* Best effort for all of these. */
1442 ossl_quic_tx_packetiser_discard_enc_level(ch->txp, enc_level);
1443 ossl_qrx_discard_enc_level(ch->qrx, enc_level);
1444 ossl_qtx_discard_enc_level(ch->qtx, enc_level);
1446 if (enc_level != QUIC_ENC_LEVEL_0RTT) {
1447 uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
1449 ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space);
1451 /* We should still have crypto streams at this point. */
1452 assert(ch->crypto_send[pn_space] != NULL);
1453 assert(ch->crypto_recv[pn_space] != NULL);
1455 /* Get rid of the crypto stream state for the EL. */
1456 ossl_quic_sstream_free(ch->crypto_send[pn_space]);
1457 ch->crypto_send[pn_space] = NULL;
1459 ossl_quic_rstream_free(ch->crypto_recv[pn_space]);
1460 ch->crypto_recv[pn_space] = NULL;
1463 ch->el_discarded |= (1U << enc_level);
1467 /* Intended to be called by the RXDP. */
1468 int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch)
1470 if (ch->handshake_confirmed)
1473 if (!ch->handshake_complete) {
1475 * Does not make sense for handshake to be confirmed before it is
1478 ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION,
1479 OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE,
1480 "handshake cannot be confirmed "
1481 "before it is completed");
1485 ch_discard_el(ch, QUIC_ENC_LEVEL_HANDSHAKE);
1486 ch->handshake_confirmed = 1;
1491 * Master function used when we want to start tearing down a connection:
1493 * - If the connection is still IDLE we can go straight to TERMINATED;
1495 * - If we are already TERMINATED this is a no-op.
1497 * - If we are TERMINATING - CLOSING and we have now got a CONNECTION_CLOSE
1498 * from the peer (tcause->remote == 1), we move to TERMINATING - CLOSING.
1500 * - If we are TERMINATING - DRAINING, we remain here until the terminating
1503 * - Otherwise, we are in ACTIVE and move to TERMINATING - CLOSING.
1504 * if we caused the termination (e.g. we have sent a CONNECTION_CLOSE). Note
1505 * that we are considered to have caused a termination if we sent the first
1506 * CONNECTION_CLOSE frame, even if it is caused by a peer protocol
1507 * violation. If the peer sent the first CONNECTION_CLOSE frame, we move to
1508 * TERMINATING - DRAINING.
1510 * We record the termination cause structure passed on the first call only.
1511 * Any successive calls have their termination cause data discarded;
1512 * once we start sending a CONNECTION_CLOSE frame, we don't change the details
1515 static void ch_start_terminating(QUIC_CHANNEL *ch,
1516 const QUIC_TERMINATE_CAUSE *tcause)
1518 switch (ch->state) {
1520 case QUIC_CHANNEL_STATE_IDLE:
1521 ch->terminate_cause = *tcause;
1522 ch_on_terminating_timeout(ch);
1525 case QUIC_CHANNEL_STATE_ACTIVE:
1526 ch->state = tcause->remote ? QUIC_CHANNEL_STATE_TERMINATING_DRAINING
1527 : QUIC_CHANNEL_STATE_TERMINATING_CLOSING;
1528 ch->terminate_cause = *tcause;
1529 ch->terminate_deadline
1530 = ossl_time_add(ossl_time_now(),
1531 ossl_time_multiply(ossl_ackm_get_pto_duration(ch->ackm),
1534 if (!tcause->remote) {
1535 OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
1538 f.error_code = ch->terminate_cause.error_code;
1539 f.frame_type = ch->terminate_cause.frame_type;
1540 f.is_app = ch->terminate_cause.app;
1541 ossl_quic_tx_packetiser_schedule_conn_close(ch->txp, &f);
1542 ch->conn_close_queued = 1;
1546 case QUIC_CHANNEL_STATE_TERMINATING_CLOSING:
1548 ch->state = QUIC_CHANNEL_STATE_TERMINATING_DRAINING;
1552 case QUIC_CHANNEL_STATE_TERMINATING_DRAINING:
1553 /* We remain here until the timout expires. */
1556 case QUIC_CHANNEL_STATE_TERMINATED:
1563 void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch,
1564 OSSL_QUIC_FRAME_CONN_CLOSE *f)
1566 QUIC_TERMINATE_CAUSE tcause = {0};
1568 if (!ossl_quic_channel_is_active(ch))
1572 tcause.app = f->is_app;
1573 tcause.error_code = f->error_code;
1574 tcause.frame_type = f->frame_type;
1576 ch_start_terminating(ch, &tcause);
1579 void ossl_quic_channel_raise_protocol_error(QUIC_CHANNEL *ch,
1580 uint64_t error_code,
1581 uint64_t frame_type,
1584 QUIC_TERMINATE_CAUSE tcause = {0};
1586 tcause.error_code = error_code;
1587 tcause.frame_type = frame_type;
1589 ch_start_terminating(ch, &tcause);
1593 * Called once the terminating timer expires, meaning we move from TERMINATING
1596 static void ch_on_terminating_timeout(QUIC_CHANNEL *ch)
1598 ch->state = QUIC_CHANNEL_STATE_TERMINATED;
1602 * Updates our idle deadline. Called when an event happens which should bump the
1605 static void ch_update_idle(QUIC_CHANNEL *ch)
1607 if (ch->max_idle_timeout == 0)
1608 ch->idle_deadline = ossl_time_infinite();
1610 ch->idle_deadline = ossl_time_add(ossl_time_now(),
1611 ossl_ms2time(ch->max_idle_timeout));
1614 /* Called when the idle timeout expires. */
1615 static void ch_on_idle_timeout(QUIC_CHANNEL *ch)
1618 * Idle timeout does not have an error code associated with it because a
1619 * CONN_CLOSE is never sent for it. We shouldn't use this data once we reach
1620 * TERMINATED anyway.
1622 ch->terminate_cause.app = 0;
1623 ch->terminate_cause.error_code = UINT64_MAX;
1624 ch->terminate_cause.frame_type = 0;
1626 ch->state = QUIC_CHANNEL_STATE_TERMINATED;