Home | History | Annotate | Download | only in quic

Lines Matching refs:ch

46 DEFINE_LIST_OF_IMPL(ch, QUIC_CHANNEL);
48 static void ch_save_err_state(QUIC_CHANNEL *ch);
49 static int ch_rx(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads);
50 static int ch_tx(QUIC_CHANNEL *ch, int *notify_other_threads);
51 static int ch_tick_tls(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads);
52 static void ch_rx_handle_packet(QUIC_CHANNEL *ch, int channel_only);
53 static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch);
54 static int ch_retry(QUIC_CHANNEL *ch,
59 static int ch_restart(QUIC_CHANNEL *ch);
61 static void ch_cleanup(QUIC_CHANNEL *ch);
62 static int ch_generate_transport_params(QUIC_CHANNEL *ch);
83 static int ch_retry(QUIC_CHANNEL *ch,
88 static void ch_update_idle(QUIC_CHANNEL *ch);
89 static int ch_discard_el(QUIC_CHANNEL *ch,
91 static void ch_on_idle_timeout(QUIC_CHANNEL *ch);
92 static void ch_update_idle(QUIC_CHANNEL *ch);
93 static void ch_update_ping_deadline(QUIC_CHANNEL *ch);
94 static void ch_on_terminating_timeout(QUIC_CHANNEL *ch);
95 static void ch_start_terminating(QUIC_CHANNEL *ch,
100 static void ch_record_state_transition(QUIC_CHANNEL *ch, uint32_t new_state);
105 static QLOG *ch_get_qlog(QUIC_CHANNEL *ch)
110 if (ch->qlog != NULL)
111 return ch->qlog;
113 if (!ch->use_qlog)
116 if (ch->is_server && ch->init_dcid.id_len == 0)
119 qti.odcid = ch->init_dcid;
120 qti.title = ch->qlog_title;
123 qti.is_server = ch->is_server;
125 qti.now_cb_arg = ch;
126 if ((ch->qlog = ossl_qlog_new_from_env(&qti)) == NULL) {
127 ch->use_qlog = 0; /* don't try again */
131 return ch->qlog;
140 QUIC_CHANNEL *ch = arg;
142 return ch_get_qlog(ch);
157 static int ch_init(QUIC_CHANNEL *ch)
167 if (ch->port == NULL || ch->lcidm == NULL || ch->srtm == NULL)
170 rx_short_dcid_len = ossl_quic_port_get_rx_short_dcid_len(ch->port);
171 tx_init_dcid_len = ossl_quic_port_get_tx_init_dcid_len(ch->port);
174 if (!ch->is_server
175 && !ossl_quic_gen_rand_conn_id(ch->port->engine->libctx, tx_init_dcid_len,
176 &ch->init_dcid))
180 qtx_args.libctx = ch->port->engine->libctx;
182 qtx_args.get_qlog_cb_arg = ch;
184 ch->rx_max_udp_payload_size = qtx_args.mdpl;
186 ch->ping_deadline = ossl_time_infinite();
188 ch->qtx = ossl_qtx_new(&qtx_args);
189 if (ch->qtx == NULL)
192 ch->txpim = ossl_quic_txpim_new();
193 if (ch->txpim == NULL)
196 ch->cfq = ossl_quic_cfq_new();
197 if (ch->cfq == NULL)
200 if (!ossl_quic_txfc_init(&ch->conn_txfc, NULL))
207 ch->tx_init_max_stream_data_bidi_local = DEFAULT_INIT_STREAM_RXFC_WND;
208 ch->tx_init_max_stream_data_bidi_remote = DEFAULT_INIT_STREAM_RXFC_WND;
209 ch->tx_init_max_stream_data_uni = DEFAULT_INIT_STREAM_RXFC_WND;
211 if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL,
214 get_time, ch))
218 if (!ossl_quic_rxfc_init_standalone(&ch->crypto_rxfc[pn_space],
220 get_time, ch))
223 if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_bidi_rxfc,
225 get_time, ch))
228 if (!ossl_quic_rxfc_init_standalone(&ch->max_streams_uni_rxfc,
230 get_time, ch))
233 if (!ossl_statm_init(&ch->statm))
236 ch->have_statm = 1;
237 ch->cc_method = &ossl_cc_newreno_method;
238 if ((ch->cc_data = ch->cc_method->new(get_time, ch)) == NULL)
241 if ((ch->ackm = ossl_ackm_new(get_time, ch, &ch->statm,
242 ch->cc_method, ch->cc_data,
243 ch->is_server))
247 if (!ossl_quic_stream_map_init(&ch->qsm, get_stream_limit, ch,
248 &ch->max_streams_bidi_rxfc,
249 &ch->max_streams_uni_rxfc,
250 ch->is_server))
253 ch->have_qsm = 1;
255 if (!ch->is_server
256 && !ossl_quic_lcidm_generate_initial(ch->lcidm, ch, &ch->init_scid))
259 txp_args.cur_scid = ch->init_scid;
260 txp_args.cur_dcid = ch->init_dcid;
262 txp_args.qtx = ch->qtx;
263 txp_args.txpim = ch->txpim;
264 txp_args.cfq = ch->cfq;
265 txp_args.ackm = ch->ackm;
266 txp_args.qsm = &ch->qsm;
267 txp_args.conn_txfc = &ch->conn_txfc;
268 txp_args.conn_rxfc = &ch->conn_rxfc;
269 txp_args.max_streams_bidi_rxfc = &ch->max_streams_bidi_rxfc;
270 txp_args.max_streams_uni_rxfc = &ch->max_streams_uni_rxfc;
271 txp_args.cc_method = ch->cc_method;
272 txp_args.cc_data = ch->cc_data;
274 txp_args.now_arg = ch;
276 txp_args.get_qlog_cb_arg = ch;
280 ch->crypto_send[pn_space] = ossl_quic_sstream_new(INIT_CRYPTO_SEND_BUF_LEN);
281 if (ch->crypto_send[pn_space] == NULL)
284 txp_args.crypto[pn_space] = ch->crypto_send[pn_space];
287 ch->txp = ossl_quic_tx_packetiser_new(&txp_args);
288 if (ch->txp == NULL)
292 if (!ch->is_server)
293 ossl_quic_tx_packetiser_set_validated(ch->txp);
295 ossl_quic_tx_packetiser_set_ack_tx_cb(ch->txp, ch_on_txp_ack_tx, ch);
307 if (ch->qrx == NULL && ch->is_tserver_ch == 0) {
309 qrx_args.libctx = ch->port->engine->libctx;
310 qrx_args.demux = ch->port->demux;
314 if ((ch->qrx = ossl_qrx_new(&qrx_args)) == NULL)
318 if (ch->qrx != NULL) {
324 if (!ossl_qrx_set_late_validation_cb(ch->qrx,
326 ch))
329 if (!ossl_qrx_set_key_update_cb(ch->qrx,
331 ch))
336 ch->crypto_recv[pn_space] = ossl_quic_rstream_new(NULL, NULL, 0);
337 if (ch->crypto_recv[pn_space] == NULL)
342 tls_args.s = ch->tls;
344 tls_args.crypto_send_cb_arg = ch;
346 tls_args.crypto_recv_rcd_cb_arg = ch;
348 tls_args.crypto_release_rcd_cb_arg = ch;
350 tls_args.yield_secret_cb_arg = ch;
352 tls_args.got_transport_params_cb_arg = ch;
354 tls_args.handshake_complete_cb_arg = ch;
356 tls_args.alert_cb_arg = ch;
357 tls_args.is_server = ch->is_server;
360 if ((ch->qtls = ossl_quic_tls_new(&tls_args)) == NULL)
363 ch->tx_max_ack_delay = DEFAULT_MAX_ACK_DELAY;
364 ch->rx_max_ack_delay = QUIC_DEFAULT_MAX_ACK_DELAY;
365 ch->rx_ack_delay_exp = QUIC_DEFAULT_ACK_DELAY_EXP;
366 ch->rx_active_conn_id_limit = QUIC_MIN_ACTIVE_CONN_ID_LIMIT;
367 ch->tx_enc_level = QUIC_ENC_LEVEL_INITIAL;
368 ch->rx_enc_level = QUIC_ENC_LEVEL_INITIAL;
369 ch->txku_threshold_override = UINT64_MAX;
371 ch->max_idle_timeout_local_req = QUIC_DEFAULT_IDLE_TIMEOUT;
372 ch->max_idle_timeout_remote_req = 0;
373 ch->max_idle_timeout = ch->max_idle_timeout_local_req;
375 ossl_ackm_set_tx_max_ack_delay(ch->ackm, ossl_ms2time(ch->tx_max_ack_delay));
376 ossl_ackm_set_rx_max_ack_delay(ch->ackm, ossl_ms2time(ch->rx_max_ack_delay));
378 ch_update_idle(ch);
379 ossl_list_ch_insert_tail(&ch->port->channel_list, ch);
380 ch->on_port_list = 1;
384 ch_cleanup(ch);
388 static void ch_cleanup(QUIC_CHANNEL *ch)
392 if (ch->ackm != NULL)
396 ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space);
398 ossl_quic_lcidm_cull(ch->lcidm, ch);
399 ossl_quic_srtm_cull(ch->srtm, ch);
400 ossl_quic_tx_packetiser_free(ch->txp);
401 ossl_quic_txpim_free(ch->txpim);
402 ossl_quic_cfq_free(ch->cfq);
403 ossl_qtx_free(ch->qtx);
404 if (ch->cc_data != NULL)
405 ch->cc_method->free(ch->cc_data);
406 if (ch->have_statm)
407 ossl_statm_destroy(&ch->statm);
408 ossl_ackm_free(ch->ackm);
410 if (ch->have_qsm)
411 ossl_quic_stream_map_cleanup(&ch->qsm);
414 ossl_quic_sstream_free(ch->crypto_send[pn_space]);
415 ossl_quic_rstream_free(ch->crypto_recv[pn_space]);
418 ossl_qrx_pkt_release(ch->qrx_pkt);
419 ch->qrx_pkt = NULL;
421 ossl_quic_tls_free(ch->qtls);
422 ossl_qrx_free(ch->qrx);
423 OPENSSL_free(ch->local_transport_params);
424 OPENSSL_free((char *)ch->terminate_cause.reason);
425 OSSL_ERR_STATE_free(ch->err_state);
426 OPENSSL_free(ch->ack_range_scratch);
427 OPENSSL_free(ch->pending_new_token);
429 if (ch->on_port_list) {
430 ossl_list_ch_remove(&ch->port->channel_list, ch);
431 ch->on_port_list = 0;
435 if (ch->qlog != NULL)
436 ossl_qlog_flush(ch->qlog); /* best effort */
438 OPENSSL_free(ch->qlog_title);
439 ossl_qlog_free(ch->qlog);
443 int ossl_quic_channel_init(QUIC_CHANNEL *ch)
445 return ch_init(ch);
461 QUIC_CHANNEL *ch = NULL;
463 if ((ch = OPENSSL_zalloc(sizeof(*ch))) == NULL)
466 ch->port = args->port;
467 ch->is_server = args->is_server;
468 ch->tls = args->tls;
469 ch->lcidm = args->lcidm;
470 ch->srtm = args->srtm;
471 ch->qrx = args->qrx;
472 ch->is_tserver_ch = args->is_tserver_ch;
474 ch->use_qlog = args->use_qlog;
476 if (ch->use_qlog && args->qlog_title != NULL) {
477 if ((ch->qlog_title = OPENSSL_strdup(args->qlog_title)) == NULL) {
478 OPENSSL_free(ch);
484 return ch;
487 void ossl_quic_channel_free(QUIC_CHANNEL *ch)
489 if (ch == NULL)
492 ch_cleanup(ch);
493 OPENSSL_free(ch);
497 int ossl_quic_channel_set_mutator(QUIC_CHANNEL *ch,
502 if (ch->qtx == NULL)
505 ossl_qtx_set_mutator(ch->qtx, mutatecb, finishmutatecb, mutatearg);
509 int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr)
511 if (!ch->addressed_mode)
514 return BIO_ADDR_copy(peer_addr, &ch->cur_peer_addr);
517 int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr)
519 if (ch->state != QUIC_CHANNEL_STATE_IDLE)
523 BIO_ADDR_clear(&ch->cur_peer_addr);
524 ch->addressed_mode = 0;
528 if (!BIO_ADDR_copy(&ch->cur_peer_addr, peer_addr)) {
529 ch->addressed_mode = 0;
532 ch->addressed_mode = 1;
537 QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch)
539 return ossl_quic_port_get0_reactor(ch->port);
542 QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch)
544 return &ch->qsm;
547 OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch)
549 return &ch->statm;
552 SSL *ossl_quic_channel_get0_tls(QUIC_CHANNEL *ch)
554 return ch->tls;
562 int ossl_quic_channel_schedule_new_token(QUIC_CHANNEL *ch,
590 cfq_item = ossl_quic_cfq_add_frame(ch->cfq, 1,
606 size_t ossl_quic_channel_get_short_header_conn_id_len(QUIC_CHANNEL *ch)
608 return ossl_quic_port_get_rx_short_dcid_len(ch->port);
611 QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch,
614 return ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
617 int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch)
619 return ch != NULL && ch->state == QUIC_CHANNEL_STATE_ACTIVE;
622 int ossl_quic_channel_is_closing(const QUIC_CHANNEL *ch)
624 return ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING;
627 static int ossl_quic_channel_is_draining(const QUIC_CHANNEL *ch)
629 return ch->state == QUIC_CHANNEL_STATE_TERMINATING_DRAINING;
632 static int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch)
634 return ossl_quic_channel_is_closing(ch)
635 || ossl_quic_channel_is_draining(ch);
638 int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch)
640 return ch->state == QUIC_CHANNEL_STATE_TERMINATED;
643 int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch)
645 return ossl_quic_channel_is_terminating(ch)
646 || ossl_quic_channel_is_terminated(ch);
650 ossl_quic_channel_get_terminate_cause(const QUIC_CHANNEL *ch)
652 return ossl_quic_channel_is_term_any(ch) ? &ch->terminate_cause : NULL;
655 int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch)
657 return ch->handshake_complete;
660 int ossl_quic_channel_is_handshake_confirmed(const QUIC_CHANNEL *ch)
662 return ch->handshake_confirmed;
665 QUIC_DEMUX *ossl_quic_channel_get0_demux(QUIC_CHANNEL *ch)
667 return ch->port->demux;
670 QUIC_PORT *ossl_quic_channel_get0_port(QUIC_CHANNEL *ch)
672 return ch->port;
675 QUIC_ENGINE *ossl_quic_channel_get0_engine(QUIC_CHANNEL *ch)
677 return ossl_quic_port_get0_engine(ch->port);
680 CRYPTO_MUTEX *ossl_quic_channel_get_mutex(QUIC_CHANNEL *ch)
682 return ossl_quic_port_get0_mutex(ch->port);
685 int ossl_quic_channel_has_pending(const QUIC_CHANNEL *ch)
687 return ossl_quic_demux_has_pending(ch->port->demux)
688 || ossl_qrx_processed_read_pending(ch->qrx);
699 QUIC_CHANNEL *ch = arg;
701 return ossl_quic_port_get_time(ch->port);
707 QUIC_CHANNEL *ch = arg;
709 return uni ? ch->max_local_streams_uni : ch->max_local_streams_bidi;
718 QUIC_CHANNEL *ch = arg;
721 if (!ossl_ackm_is_rx_pn_processable(ch->ackm, pn, pn_space))
732 static void ch_trigger_txku(QUIC_CHANNEL *ch)
735 = ossl_quic_tx_packetiser_get_next_pn(ch->txp, QUIC_PN_SPACE_APP);
738 || !ossl_qtx_trigger_key_update(ch->qtx)) {
739 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0,
744 ch->txku_in_progress = 1;
745 ch->txku_pn = next_pn;
746 ch->rxku_expected = ch->ku_locally_initiated;
750 static int txku_in_progress(QUIC_CHANNEL *ch)
752 if (ch->txku_in_progress
753 && ossl_ackm_get_largest_acked(ch->ackm, QUIC_PN_SPACE_APP) >= ch->txku_pn) {
754 OSSL_TIME pto = ossl_ackm_get_pto_duration(ch->ackm);
765 ch->txku_in_progress = 0;
766 ch->txku_cooldown_deadline = ossl_time_add(get_time(ch),
770 return ch->txku_in_progress;
774 static int txku_allowed(QUIC_CHANNEL *ch)
776 return ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT /* Sanity check. */
778 && ch->handshake_confirmed
779 && !txku_in_progress(ch);
783 static int txku_recommendable(QUIC_CHANNEL *ch)
785 if (!txku_allowed(ch))
790 ossl_time_compare(get_time(ch), ch->txku_cooldown_deadline) >= 0
792 && !ch->rxku_in_progress
793 && !ch->rxku_pending_confirm;
797 static int txku_desirable(QUIC_CHANNEL *ch)
803 cur_pkt_count = ossl_qtx_get_cur_epoch_pkt_count(ch->qtx, enc_level);
804 max_pkt_count = ossl_qtx_get_max_epoch_pkt_count(ch->qtx, enc_level);
807 if (ch->txku_threshold_override != UINT64_MAX)
808 thresh_pkt_count = ch->txku_threshold_override;
814 static void ch_maybe_trigger_spontaneous_txku(QUIC_CHANNEL *ch)
816 if (!txku_recommendable(ch) || !txku_desirable(ch))
819 ch->ku_locally_initiated = 1;
820 ch_trigger_txku(ch);
824 static int rxku_allowed(QUIC_CHANNEL *ch)
841 return ch->handshake_confirmed && !ch->rxku_pending_confirm;
857 QUIC_CHANNEL *ch = arg;
865 assert(!ch->rxku_in_progress);
867 if (!rxku_allowed(ch))
871 else if (ch->ku_locally_initiated)
887 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
892 pto = ossl_ackm_get_pto_duration(ch->ackm);
894 ch->ku_locally_initiated = 0;
895 ch->rxku_in_progress = 1;
896 ch->rxku_pending_confirm = 1;
897 ch->rxku_trigger_pn = pn;
898 ch->rxku_update_end_deadline = ossl_time_add(get_time(ch), pto);
899 ch->rxku_expected = 0;
903 ch_trigger_txku(ch);
920 ossl_quic_tx_packetiser_schedule_ack(ch->txp, QUIC_PN_SPACE_APP);
925 static void ch_rxku_tick(QUIC_CHANNEL *ch)
927 if (!ch->rxku_in_progress
928 || ossl_time_compare(get_time(ch), ch->rxku_update_end_deadline) < 0)
931 ch->rxku_update_end_deadline = ossl_time_infinite();
932 ch->rxku_in_progress = 0;
934 if (!ossl_qrx_key_update_timeout(ch->qrx, /*normal=*/1))
935 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0,
943 QUIC_CHANNEL *ch = arg;
945 if (pn_space != QUIC_PN_SPACE_APP || !ch->rxku_pending_confirm
946 || !ossl_quic_frame_ack_contains_pn(ack, ch->rxku_trigger_pn))
953 ch->rxku_pending_confirm_done = 1;
964 QUIC_CHANNEL *ch = arg;
965 uint32_t enc_level = ch->tx_enc_level;
967 QUIC_SSTREAM *sstream = ch->crypto_send[pn_space];
993 QUIC_CHANNEL *ch = arg;
1011 for (i = QUIC_ENC_LEVEL_INITIAL; i < ch->rx_enc_level; ++i)
1012 if (i != QUIC_ENC_LEVEL_0RTT && !crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) {
1014 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1020 rstream = ch->crypto_recv[ossl_quic_enc_level_to_pn_space(ch->rx_enc_level)];
1030 QUIC_CHANNEL *ch = arg;
1033 uint32_t rx_pn_space = ossl_quic_enc_level_to_pn_space(ch->rx_enc_level);
1035 rstream = ch->crypto_recv[rx_pn_space];
1039 ossl_statm_get_rtt_info(ossl_quic_channel_get_statm(ch), &rtt_info);
1040 if (!ossl_quic_rxfc_on_retire(&ch->crypto_rxfc[rx_pn_space], bytes_read,
1053 QUIC_CHANNEL *ch = arg;
1081 if (enc_level <= ch->tx_enc_level)
1088 if (!ossl_qtx_provide_secret(ch->qtx, enc_level,
1093 ch->tx_enc_level = enc_level;
1096 if (enc_level <= ch->rx_enc_level)
1108 if (!crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) {
1110 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
1116 if (!ossl_qrx_provide_secret(ch->qrx, enc_level,
1121 ch->have_new_rx_secret = 1;
1122 ch->rx_enc_level = enc_level;
1130 QUIC_CHANNEL *ch = arg;
1132 if (!ossl_assert(!ch->handshake_complete))
1135 if (!ossl_assert(ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT))
1143 ossl_quic_tx_packetiser_set_validated(ch->txp);
1145 if (!ch->got_remote_transport_params) {
1150 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CRYPTO_MISSING_EXT,
1157 OPENSSL_free(ch->local_transport_params);
1158 ch->local_transport_params = NULL;
1161 ossl_qrx_allow_1rtt_processing(ch->qrx);
1164 ossl_quic_tx_packetiser_notify_handshake_complete(ch->txp);
1166 ch->handshake_complete = 1;
1168 if (ch->pending_new_token != NULL) {
1177 ossl_quic_channel_schedule_new_token(ch,
1178 ch->pending_new_token,
1179 ch->pending_new_token_len);
1180 OPENSSL_free(ch->pending_new_token);
1181 ch->pending_new_token = NULL;
1182 ch->pending_new_token_len = 0;
1185 if (ch->is_server) {
1189 ossl_quic_channel_on_handshake_confirmed(ch);
1191 ossl_quic_tx_packetiser_schedule_handshake_done(ch->txp);
1194 ch_record_state_transition(ch, ch->state);
1200 QUIC_CHANNEL *ch = arg;
1208 && ch->handshake_complete
1209 && ossl_quic_tls_is_cert_request(ch->qtls))
1210 ossl_quic_channel_raise_protocol_error(ch,
1223 && ch->handshake_complete
1224 && ossl_quic_tls_has_bad_max_early_data(ch->qtls))
1225 ossl_quic_channel_raise_protocol_error(ch,
1230 ossl_quic_channel_raise_protocol_error(ch,
1283 QUIC_CHANNEL *ch = arg;
1285 ossl_quic_stream_map_update_state(&ch->qsm, s);
1302 QUIC_CHANNEL *ch = arg;
1329 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ch->tls);
1335 if (ch->is_server && sc->hello_retry_request != SSL_HRR_NONE
1336 && ch->got_remote_transport_params) {
1337 ch->max_local_streams_bidi = 0;
1338 ch->max_local_streams_uni = 0;
1339 ch->got_local_transport_params = 0;
1340 OPENSSL_free(ch->local_transport_params);
1341 ch->local_transport_params = NULL;
1342 } else if (ch->got_remote_transport_params) {
1348 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0,
1364 if (ch->is_server) {
1376 if (!ossl_quic_conn_id_eq(&ch->init_dcid, &cid)) {
1386 if (ch->is_server) {
1396 if (!ch->doing_retry) {
1407 if (!ossl_quic_conn_id_eq(&ch->retry_scid, &cid)) {
1427 if (!ossl_quic_conn_id_eq(&ch->init_scid, &cid)) {
1447 ossl_quic_txfc_bump_cwm(&ch->conn_txfc, v);
1467 ch->rx_init_max_stream_data_bidi_remote = v;
1487 ch->rx_init_max_stream_data_bidi_local = v;
1490 ossl_quic_stream_map_visit(&ch->qsm, txfc_bump_cwm_bidi, &v);
1506 ch->rx_init_max_stream_data_uni = v;
1509 ossl_quic_stream_map_visit(&ch->qsm, txfc_bump_cwm_uni, &v);
1526 ch->rx_ack_delay_exp = (unsigned char)v;
1543 ch->rx_max_ack_delay = v;
1544 ossl_ackm_set_rx_max_ack_delay(ch->ackm,
1545 ossl_ms2time(ch->rx_max_ack_delay));
1563 assert(ch->max_local_streams_bidi == 0);
1564 ch->max_local_streams_bidi = v;
1581 assert(ch->max_local_streams_uni == 0);
1582 ch->max_local_streams_uni = v;
1598 ch->max_idle_timeout_remote_req = v;
1600 ch->max_idle_timeout = min_u64_ignore_0(ch->max_idle_timeout_local_req,
1601 ch->max_idle_timeout_remote_req);
1603 ch_update_idle(ch);
1621 ch->rx_max_udp_payload_size = v;
1638 ch->rx_active_conn_id_limit = v;
1652 if (ch->is_server) {
1662 if (!ossl_quic_srtm_add(ch->srtm, ch, ch->cur_remote_seq_num,
1687 if (ch->is_server) {
1692 if (ch->cur_remote_dcid.id_len == 0) {
1751 if (!ch->is_server) {
1757 if (ch->doing_retry && !got_retry_scid) {
1763 ch->got_remote_transport_params = 1;
1766 QLOG_EVENT_BEGIN(ch_get_qlog(ch), transport, parameters_set)
1771 &ch->init_dcid);
1774 &ch->init_dcid);
1777 &ch->retry_scid);
1780 ossl_quic_txfc_get_cwm(&ch->conn_txfc));
1783 ch->rx_init_max_stream_data_bidi_local);
1786 ch->rx_init_max_stream_data_bidi_remote);
1789 ch->rx_init_max_stream_data_uni);
1792 ch->max_local_streams_bidi);
1795 ch->max_local_streams_uni);
1797 QLOG_U64("ack_delay_exponent", ch->rx_ack_delay_exp);
1799 QLOG_U64("max_ack_delay", ch->rx_max_ack_delay);
1801 QLOG_U64("max_udp_payload_size", ch->rx_max_udp_payload_size);
1805 QLOG_U64("active_connection_id_limit", ch->rx_active_conn_id_limit);
1830 ossl_quic_stream_map_visit(&ch->qsm, do_update, ch);
1833 if (ch->is_server && !ch_generate_transport_params(ch)) {
1834 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR, 0,
1842 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_TRANSPORT_PARAMETER_ERROR,
1852 static int ch_generate_transport_params(QUIC_CHANNEL *ch)
1870 if (ch->odcid.id_len == 0)
1871 id_to_use = &ch->init_dcid;
1873 id_to_use = &ch->odcid;
1875 if (ch->local_transport_params != NULL || ch->got_local_transport_params)
1891 if (ch->is_server) {
1897 &ch->cur_local_cid))
1899 if (ch->odcid.id_len != 0)
1902 &ch->init_dcid))
1906 &ch->init_scid))
1911 ch->max_idle_timeout_local_req))
1922 if (ch->tx_max_ack_delay != QUIC_DEFAULT_MAX_ACK_DELAY
1924 ch->tx_max_ack_delay))
1928 ossl_quic_rxfc_get_cwm(&ch->conn_rxfc)))
1933 ch->tx_init_max_stream_data_bidi_local))
1937 ch->tx_init_max_stream_data_bidi_remote))
1941 ch->tx_init_max_stream_data_uni))
1945 ossl_quic_rxfc_get_cwm(&ch->max_streams_bidi_rxfc)))
1949 ossl_quic_rxfc_get_cwm(&ch->max_streams_uni_rxfc)))
1960 ch->local_transport_params = (unsigned char *)buf_mem->data;
1963 if (!ossl_quic_tls_set_transport_params(ch->qtls, ch->local_transport_params,
1968 QLOG_EVENT_BEGIN(ch_get_qlog(ch), transport, parameters_set)
1971 if (ch->is_server) {
1972 QLOG_CID("original_destination_connection_id", &ch->init_dcid);
1973 QLOG_CID("initial_source_connection_id", &ch->cur_local_cid);
1977 QLOG_U64("max_idle_timeout", ch->max_idle_timeout);
1980 QLOG_U64("max_ack_delay", ch->tx_max_ack_delay);
1981 QLOG_U64("initial_max_data", ossl_quic_rxfc_get_cwm(&ch->conn_rxfc));
1983 ch->tx_init_max_stream_data_bidi_local);
1985 ch->tx_init_max_stream_data_bidi_remote);
1987 ch->tx_init_max_stream_data_uni);
1989 ossl_quic_rxfc_get_cwm(&ch->max_streams_bidi_rxfc));
1991 ossl_quic_rxfc_get_cwm(&ch->max_streams_uni_rxfc));
1995 ch->got_local_transport_params = 1;
2015 void ossl_quic_channel_subtick(QUIC_CHANNEL *ch, QUIC_TICK_RESULT *res,
2037 if (ch->state == QUIC_CHANNEL_STATE_IDLE
2038 || ossl_quic_channel_is_terminated(ch)) {
2050 if (ossl_quic_channel_is_terminating(ch)) {
2051 now = get_time(ch);
2053 if (ossl_time_compare(now, ch->terminate_deadline) >= 0) {
2054 ch_on_terminating_timeout(ch);
2063 if (!ch->port->engine->inhibit_tick) {
2065 ch_rxku_tick(ch);
2069 ch->did_tls_tick = 0;
2070 ch->have_new_rx_secret = 0;
2071 ch_rx(ch, channel_only, &notify_other_threads);
2077 if (!ch->did_tls_tick)
2078 ch_tick_tls(ch, channel_only, &notify_other_threads);
2087 } while (ch->have_new_rx_secret);
2097 now = get_time(ch);
2098 if (ossl_time_compare(now, ch->idle_deadline) >= 0) {
2103 if (!ch->port->engine->inhibit_tick)
2104 ch_on_idle_timeout(ch);
2113 if (!ch->port->engine->inhibit_tick) {
2114 deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm);
2117 ossl_ackm_on_timeout(ch->ackm);
2120 if (ossl_time_compare(now, ch->ping_deadline) >= 0) {
2121 int pn_space = ossl_quic_enc_level_to_pn_space(ch->tx_enc_level);
2123 ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, pn_space);
2132 ch_update_ping_deadline(ch);
2136 ch_tx(ch, &notify_other_threads);
2139 ossl_quic_stream_map_gc(&ch->qsm);
2143 res->tick_deadline = ch_determine_next_tick_deadline(ch);
2150 res->net_read_desired = !ossl_quic_channel_is_terminated(ch);
2154 = (!ossl_quic_channel_is_terminated(ch)
2155 && ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0);
2160 static int ch_tick_tls(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads)
2169 ch->did_tls_tick = 1;
2170 ossl_quic_tls_tick(ch->qtls);
2172 if (ossl_quic_tls_get_error(ch->qtls, &error_code, &error_msg,
2174 ossl_quic_channel_raise_protocol_error_state(ch, error_code, 0,
2186 static void ch_rx_check_forged_pkt_limit(QUIC_CHANNEL *ch)
2198 if ((ch->el_discarded & (1U << enc_level)) != 0)
2201 if (enc_level > ch->rx_enc_level)
2204 l = ossl_qrx_get_max_forged_pkt_count(ch->qrx, enc_level);
2209 if (ossl_qrx_get_cur_forged_pkt_count(ch->qrx) < limit)
2212 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_AEAD_LIMIT_REACHED, 0,
2217 static int ch_rx(QUIC_CHANNEL *ch, int channel_only, int *notify_other_threads)
2220 const int closing = ossl_quic_channel_is_closing(ch);
2222 if (!ch->is_server && !ch->have_sent_any_pkt)
2230 assert(ch->qrx_pkt == NULL);
2232 if (!ossl_qrx_read_pkt(ch->qrx, &ch->qrx_pkt))
2238 ch->txp, ch->qrx_pkt->hdr->len);
2241 ch_update_idle(ch);
2242 ch_update_ping_deadline(ch);
2245 ch_rx_handle_packet(ch, channel_only); /* best effort */
2252 ossl_qrx_pkt_release(ch->qrx_pkt);
2253 ch->qrx_pkt = NULL;
2255 ch->have_sent_ack_eliciting_since_rx = 0;
2259 ch_rx_check_forged_pkt_limit(ch);
2269 ch->conn_close_queued = 1;
2299 /* Handles the packet currently in ch->qrx_pkt->hdr. */
2300 static void ch_rx_handle_packet(QUIC_CHANNEL *ch, int channel_only)
2303 int old_have_processed_any_pkt = ch->have_processed_any_pkt;
2308 assert(ch->qrx_pkt != NULL);
2315 if (!ossl_quic_channel_is_active(ch))
2318 if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)) {
2319 if (!ch->have_received_enc_pkt) {
2320 ch->cur_remote_dcid = ch->init_scid = ch->qrx_pkt->hdr->src_conn_id;
2321 ch->have_received_enc_pkt = 1;
2327 ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->init_scid);
2330 enc_level = ossl_quic_pkt_type_to_enc_level(ch->qrx_pkt->hdr->type);
2331 if ((ch->el_discarded & (1U << enc_level)) != 0)
2346 if (!ch->is_server
2347 && ch->qrx_pkt->peer != NULL
2348 && (BIO_ADDR_family(&ch->cur_peer_addr) == AF_INET
2350 || BIO_ADDR_family(&ch->cur_peer_addr) == AF_INET6
2353 && !bio_addr_eq(ch->qrx_pkt->peer, &ch->cur_peer_addr))
2356 if (!ch->is_server
2357 && ch->have_received_enc_pkt
2358 && ossl_quic_pkt_type_has_scid(ch->qrx_pkt->hdr->type)) {
2364 if (!ossl_quic_conn_id_eq(&ch->qrx_pkt->hdr->src_conn_id,
2365 &ch->init_scid))
2369 if (ossl_quic_pkt_type_has_version(ch->qrx_pkt->hdr->type)
2370 && ch->qrx_pkt->hdr->version != QUIC_VERSION_1)
2378 if (ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_VERSION_NEG) {
2384 if (ch->qrx_pkt->hdr->version != 0)
2409 ch->have_processed_any_pkt = 1;
2419 if (!PACKET_buf_init(&vpkt, ch->qrx_pkt->hdr->data,
2420 ch->qrx_pkt->hdr->len))
2436 ossl_quic_tx_packetiser_set_protocol_version(ch->txp, QUIC_VERSION_1);
2441 if (!ch_restart(ch))
2442 ossl_quic_channel_raise_protocol_error(ch,
2453 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_CONNECTION_REFUSED,
2458 ch->have_processed_any_pkt = 1;
2465 if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)
2466 && ch->qrx_pkt->hdr->reserved != 0) {
2467 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
2472 iovec.buf = ch->qrx_pkt->hdr->data;
2473 iovec.buf_len = ch->qrx_pkt->hdr->len;
2474 ossl_qlog_event_transport_packet_received(ch_get_qlog(ch), ch->qrx_pkt->hdr,
2475 ch->qrx_pkt->pn, &iovec, 1,
2476 ch->qrx_pkt->datagram_id);
2479 switch (ch->qrx_pkt->hdr->type) {
2481 if (ch->doing_retry || ch->is_server)
2493 if (ch->have_received_enc_pkt)
2496 if (ch->qrx_pkt->hdr->len <= QUIC_RETRY_INTEGRITY_TAG_LEN)
2509 if (!ossl_quic_validate_retry_integrity_tag(ch->port->engine->libctx,
2510 ch->port->engine->propq,
2511 ch->qrx_pkt->hdr,
2512 &ch->init_dcid))
2516 if (!ch_retry(ch, ch->qrx_pkt->hdr->data,
2517 ch->qrx_pkt->hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN,
2518 &ch->qrx_pkt->hdr->src_conn_id, old_have_processed_any_pkt))
2519 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR,
2524 if (!ch->is_server)
2538 if (ch->is_server && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE)
2543 ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL);
2545 if (ch->rxku_in_progress
2546 && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_1RTT
2547 && ch->qrx_pkt->pn >= ch->rxku_trigger_pn
2548 && ch->qrx_pkt->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)) {
2557 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_KEY_UPDATE_ERROR,
2562 if (!ch->is_server
2563 && ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_INITIAL
2564 && ch->qrx_pkt->hdr->token_len > 0) {
2582 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
2588 ossl_quic_handle_frames(ch, ch->qrx_pkt); /* best effort */
2590 if (ch->did_crypto_frame)
2591 ch_tick_tls(ch, channel_only, NULL);
2602 static int ch_tx(QUIC_CHANNEL *ch, int *notify_other_threads)
2614 if (ossl_quic_channel_is_draining(ch))
2617 if (ossl_quic_channel_is_closing(ch)) {
2628 if (!ch->conn_close_queued)
2631 ch->conn_close_queued = 0;
2635 ch_maybe_trigger_spontaneous_txku(ch);
2637 ch->rxku_pending_confirm_done = 0;
2648 res = ossl_quic_tx_packetiser_generate(ch->txp, &status);
2650 ch->have_sent_any_pkt = 1; /* Packet(s) were sent */
2651 ch->port->have_sent_any_pkt = 1;
2659 && !ch->have_sent_ack_eliciting_since_rx) {
2660 ch_update_idle(ch);
2661 ch->have_sent_ack_eliciting_since_rx = 1;
2664 if (!ch->is_server && status.sent_handshake)
2669 ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL);
2671 if (ch->rxku_pending_confirm_done)
2672 ch->rxku_pending_confirm = 0;
2674 ch_update_ping_deadline(ch);
2690 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INTERNAL_ERROR,
2698 switch (ossl_qtx_flush_net(ch->qtx)) {
2707 ossl_quic_port_raise_net_error(ch->port, ch);
2716 if (ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0)
2723 static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch)
2728 if (ossl_quic_channel_is_terminated(ch))
2731 deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm);
2740 if (ossl_qtx_is_enc_level_provisioned(ch->qtx, i)) {
2742 ossl_ackm_get_ack_deadline(ch->ackm,
2751 if (!ossl_time_is_infinite(ch->ping_deadline))
2752 deadline = ossl_time_min(deadline, ch->ping_deadline);
2756 ossl_quic_tx_packetiser_get_deadline(ch->txp));
2759 if (ossl_quic_channel_is_terminating(ch))
2761 ch->terminate_deadline);
2762 else if (!ossl_time_is_infinite(ch->idle_deadline))
2764 ch->idle_deadline);
2767 if (ch->rxku_in_progress)
2768 deadline = ossl_time_min(deadline, ch->rxku_update_end_deadline);
2779 * Record a state transition. This is not necessarily a change to ch->state but
2782 static void ch_record_state_transition(QUIC_CHANNEL *ch, uint32_t new_state)
2784 uint32_t old_state = ch->state;
2786 ch->state = new_state;
2788 ossl_qlog_event_connectivity_connection_state_updated(ch_get_qlog(ch),
2791 ch->handshake_complete,
2792 ch->handshake_confirmed);
2801 int ossl_quic_channel_start(QUIC_CHANNEL *ch)
2805 if (ch->is_server)
2812 if (ch->state != QUIC_CHANNEL_STATE_IDLE)
2817 if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr))
2823 if (!ch->is_server
2824 && ossl_quic_get_peer_token(ch->port->channel_ctx,
2825 &ch->cur_peer_addr,
2827 && !ossl_quic_tx_packetiser_set_initial_token(ch->txp, token->token,
2834 if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx,
2835 ch->port->engine->propq,
2836 &ch->init_dcid,
2837 ch->is_server,
2838 ch->qrx, ch->qtx))
2846 if (!ch->is_server && !ch->got_local_transport_params
2847 && !ch_generate_transport_params(ch))
2851 ch_record_state_transition(ch, QUIC_CHANNEL_STATE_ACTIVE);
2852 ch->doing_proactive_ver_neg = 0; /* not currently supported */
2854 ossl_qlog_event_connectivity_connection_started(ch_get_qlog(ch),
2855 &ch->init_dcid);
2857 /* Handshake layer: start (e.g. send CH). */
2858 if (!ch_tick_tls(ch, /*channel_only=*/0, NULL))
2861 ossl_quic_reactor_tick(ossl_quic_port_get0_reactor(ch->port), 0); /* best effort */
2871 void ossl_quic_channel_local_close(QUIC_CHANNEL *ch, uint64_t app_error_code,
2876 if (ossl_quic_channel_is_term_any(ch))
2883 ch_start_terminating(ch, &tcause, 0);
2891 * @ch: Pointer to the QUIC_CHANNEL structure.
2895 static int ch_restart(QUIC_CHANNEL *ch)
2901 return ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL,
2906 static int ch_retry(QUIC_CHANNEL *ch,
2919 if (ossl_quic_conn_id_eq(&ch->init_dcid, retry_scid))
2923 if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, retry_scid))
2933 if (!ossl_quic_tx_packetiser_set_initial_token(ch->txp, buf,
2940 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_INVALID_TOKEN, 0,
2946 ch->retry_scid = *retry_scid;
2947 ch->doing_retry = 1;
2966 if (!ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL,
2974 ch->port->engine->libctx,
2975 ch->port->engine->propq,
2976 &ch->retry_scid,
2978 ch->qrx, ch->qtx))
2985 static int ch_discard_el(QUIC_CHANNEL *ch,
2991 if ((ch->el_discarded & (1U << enc_level)) != 0)
2996 ossl_quic_tx_packetiser_discard_enc_level(ch->txp, enc_level);
2997 ossl_qrx_discard_enc_level(ch->qrx, enc_level);
2998 ossl_qtx_discard_enc_level(ch->qtx, enc_level);
3003 ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space);
3006 if (!ossl_assert(ch->crypto_send[pn_space] != NULL)
3007 || !ossl_assert(ch->crypto_recv[pn_space] != NULL))
3011 ossl_quic_sstream_free(ch->crypto_send[pn_space]);
3012 ch->crypto_send[pn_space] = NULL;
3014 ossl_quic_rstream_free(ch->crypto_recv[pn_space]);
3015 ch->crypto_recv[pn_space] = NULL;
3018 ch->el_discarded |= (1U << enc_level);
3023 int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch)
3025 if (ch->handshake_confirmed)
3028 if (!ch->handshake_complete) {
3033 ossl_quic_channel_raise_protocol_error(ch, OSSL_QUIC_ERR_PROTOCOL_VIOLATION,
3040 ch_discard_el(ch, QUIC_ENC_LEVEL_HANDSHAKE);
3041 ch->handshake_confirmed = 1;
3042 ch_record_state_transition(ch, ch->state);
3043 ossl_ackm_on_handshake_confirmed(ch->ackm);
3123 static void ch_start_terminating(QUIC_CHANNEL *ch,
3128 if (!ch->have_sent_any_pkt)
3131 switch (ch->state) {
3134 copy_tcause(&ch->terminate_cause, tcause);
3135 ch_on_terminating_timeout(ch);
3139 copy_tcause(&ch->terminate_cause, tcause);
3141 ossl_qlog_event_connectivity_connection_closed(ch_get_qlog(ch), tcause);
3144 ch_record_state_transition(ch, tcause->remote ? QUIC_CHANNEL_STATE_TERMINATING_DRAINING : QUIC_CHANNEL_STATE_TERMINATING_CLOSING);
3150 ch->terminate_deadline
3151 = ossl_time_add(get_time(ch),
3152 ossl_time_multiply(ossl_ackm_get_pto_duration(ch->ackm),
3159 f.error_code = ch->terminate_cause.error_code;
3160 f.frame_type = ch->terminate_cause.frame_type;
3161 f.is_app = ch->terminate_cause.app;
3162 f.reason = (char *)ch->terminate_cause.reason;
3163 f.reason_len = ch->terminate_cause.reason_len;
3164 ossl_quic_tx_packetiser_schedule_conn_close(ch->txp, &f);
3172 ch->conn_close_queued = 1;
3175 ch_on_terminating_timeout(ch);
3181 ch_on_terminating_timeout(ch);
3189 ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATING_DRAINING);
3199 ch_on_terminating_timeout(ch);
3210 void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch,
3215 if (!ossl_quic_channel_is_active(ch))
3224 ch_start_terminating(ch, &tcause, 0);
3232 static int ch_enqueue_retire_conn_id(QUIC_CHANNEL *ch, uint64_t seq_num)
3238 ossl_quic_srtm_remove(ch->srtm, ch, seq_num);
3255 if (ossl_quic_cfq_add_frame(ch->cfq, 1, QUIC_PN_SPACE_APP,
3267 ossl_quic_channel_raise_protocol_error(ch,
3275 void ossl_quic_channel_on_new_conn_id(QUIC_CHANNEL *ch,
3278 uint64_t new_remote_seq_num = ch->cur_remote_seq_num;
3279 uint64_t new_retire_prior_to = ch->cur_retire_prior_to;
3281 if (!ossl_quic_channel_is_active(ch))
3285 if (ch->cur_remote_dcid.id_len == 0) {
3287 ossl_quic_channel_raise_protocol_error(ch,
3311 ossl_quic_channel_raise_protocol_error(ch,
3333 if (new_retire_prior_to - ch->cur_retire_prior_to > 10) {
3334 ossl_quic_channel_raise_protocol_error(ch,
3342 if (new_remote_seq_num > ch->cur_remote_seq_num) {
3344 if (!ossl_quic_srtm_add(ch->srtm, ch, new_remote_seq_num,
3347 ch, OSSL_QUIC_ERR_CONNECTION_ID_LIMIT_ERROR,
3353 ch->cur_remote_seq_num = new_remote_seq_num;
3354 ch->cur_remote_dcid = f->conn_id;
3355 ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->cur_remote_dcid);
3378 while (new_retire_prior_to > ch->cur_retire_prior_to) {
3379 if (!ch_enqueue_retire_conn_id(ch, ch->cur_retire_prior_to))
3381 ++ch->cur_retire_prior_to;
3385 static void ch_save_err_state(QUIC_CHANNEL *ch)
3387 if (ch->err_state == NULL)
3388 ch->err_state = OSSL_ERR_STATE_new();
3390 if (ch->err_state == NULL)
3393 OSSL_ERR_STATE_save(ch->err_state);
3396 void ossl_quic_channel_inject(QUIC_CHANNEL *ch, QUIC_URXE *e)
3398 ossl_qrx_inject_urxe(ch->qrx, e);
3401 void ossl_quic_channel_inject_pkt(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpkt)
3403 ossl_qrx_inject_pkt(ch->qrx, qpkt);
3406 void ossl_quic_channel_on_stateless_reset(QUIC_CHANNEL *ch)
3412 ch_start_terminating(ch, &tcause, 0);
3415 void ossl_quic_channel_raise_net_error(QUIC_CHANNEL *ch)
3419 if (ch->net_error)
3422 ch->net_error = 1;
3432 ch_start_terminating(ch, &tcause, 1);
3435 int ossl_quic_channel_net_error(QUIC_CHANNEL *ch)
3437 return ch->net_error;
3440 void ossl_quic_channel_restore_err_state(QUIC_CHANNEL *ch)
3442 if (ch == NULL)
3445 if (!ossl_quic_port_is_running(ch->port))
3446 ossl_quic_port_restore_err_state(ch->port);
3448 OSSL_ERR_STATE_restore(ch->err_state);
3451 void ossl_quic_channel_raise_protocol_error_loc(QUIC_CHANNEL *ch,
3469 if (ch->protocol_error)
3513 ch_save_err_state(ch);
3520 ch->protocol_error = 1;
3521 ch_start_terminating(ch, &tcause, 0);
3528 static void ch_on_terminating_timeout(QUIC_CHANNEL *ch)
3530 ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATED);
3538 static OSSL_TIME ch_get_effective_idle_timeout_duration(QUIC_CHANNEL *ch)
3542 if (ch->max_idle_timeout == 0)
3553 pto = ossl_ackm_get_pto_duration(ch->ackm);
3554 return ossl_time_max(ossl_ms2time(ch->max_idle_timeout),
3562 static void ch_update_idle(QUIC_CHANNEL *ch)
3564 ch->idle_deadline = ossl_time_add(get_time(ch),
3565 ch_get_effective_idle_timeout_duration(ch));
3572 static void ch_update_ping_deadline(QUIC_CHANNEL *ch)
3576 idle_duration = ch_get_effective_idle_timeout_duration(ch);
3578 ch->ping_deadline = ossl_time_infinite();
3590 ch->ping_deadline = ossl_time_add(get_time(ch), max_span);
3594 static void ch_on_idle_timeout(QUIC_CHANNEL *ch)
3601 ch->terminate_cause.app = 0;
3602 ch->terminate_cause.error_code = OSSL_QUIC_LOCAL_ERR_IDLE_TIMEOUT;
3603 ch->terminate_cause.frame_type = 0;
3605 ch_record_state_transition(ch, QUIC_CHANNEL_STATE_TERMINATED);
3611 * This function configures a QUIC channel (`QUIC_CHANNEL *ch`) for a new
3616 * @param ch Pointer to the QUIC channel being initialized.
3624 static int ch_on_new_conn_common(QUIC_CHANNEL *ch, const BIO_ADDR *peer,
3630 if (!BIO_ADDR_copy(&ch->cur_peer_addr, peer))
3633 ch->init_dcid = *peer_dcid;
3634 ch->cur_remote_dcid = *peer_scid;
3635 ch->odcid.id_len = 0;
3638 ch->odcid = *peer_odcid;
3641 if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr))
3645 if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->cur_remote_dcid))
3648 if (!ossl_quic_tx_packetiser_set_cur_scid(ch->txp, &ch->cur_local_cid))
3652 ossl_qtx_set_qlog_cb(ch->qtx, ch_get_qlog_cb, ch);
3653 ossl_quic_tx_packetiser_set_qlog_cb(ch->txp, ch_get_qlog_cb, ch);
3659 if (!ossl_quic_provide_initial_secret(ch->port->engine->libctx,
3660 ch->port->engine->propq,
3661 &ch->init_dcid,
3663 NULL, ch->qtx))
3667 if (!ossl_quic_lcidm_enrol_odcid(ch->lcidm, ch, peer_odcid == NULL ? &ch->init_dcid : peer_odcid))
3671 ch_record_state_transition(ch, QUIC_CHANNEL_STATE_ACTIVE);
3672 ch->doing_proactive_ver_neg = 0; /* not currently supported */
3677 int ossl_quic_channel_on_new_conn(QUIC_CHANNEL *ch, const BIO_ADDR *peer,
3681 if (!ossl_assert(ch->state == QUIC_CHANNEL_STATE_IDLE && ch->is_server))
3685 if (!ossl_quic_lcidm_generate_initial(ch->lcidm, ch, &ch->cur_local_cid))
3688 return ch_on_new_conn_common(ch, peer, peer_scid, peer_dcid, NULL);
3700 * @param ch Pointer to the QUIC_CHANNEL structure representing the
3714 int ossl_quic_bind_channel(QUIC_CHANNEL *ch, const BIO_ADDR *peer,
3722 if (!ossl_assert(ch->state == QUIC_CHANNEL_STATE_IDLE && ch->is_server))
3725 ch->cur_local_cid = *peer_dcid;
3726 if (!ossl_quic_lcidm_bind_channel(ch->lcidm, ch, peer_dcid))
3733 return ch_on_new_conn_common(ch, peer, peer_scid, peer_dcid, peer_odcid);
3736 SSL *ossl_quic_channel_get0_ssl(QUIC_CHANNEL *ch)
3738 return ch->tls;
3741 static int ch_init_new_stream(QUIC_CHANNEL *ch, QUIC_STREAM *qs,
3746 int local_init = (ch->is_server == server_init);
3758 if (!ossl_quic_txfc_init(&qs->txfc, &ch->conn_txfc))
3761 if (ch->got_remote_transport_params) {
3771 cwm = ch->rx_init_max_stream_data_uni;
3773 cwm = ch->rx_init_max_stream_data_bidi_local;
3775 cwm = ch->rx_init_max_stream_data_bidi_remote;
3785 rxfc_wnd = ch->tx_init_max_stream_data_uni;
3787 rxfc_wnd = ch->tx_init_max_stream_data_bidi_local;
3789 rxfc_wnd = ch->tx_init_max_stream_data_bidi_remote;
3791 if (!ossl_quic_rxfc_init(&qs->rxfc, &ch->conn_rxfc,
3794 get_time, ch))
3807 static uint64_t *ch_get_local_stream_next_ordinal_ptr(QUIC_CHANNEL *ch,
3810 return is_uni ? &ch->next_local_stream_ordinal_uni
3811 : &ch->next_local_stream_ordinal_bidi;
3814 static const uint64_t *ch_get_local_stream_max_ptr(const QUIC_CHANNEL *ch,
3817 return is_uni ? &ch->max_local_streams_uni
3818 : &ch->max_local_streams_bidi;
3821 static const QUIC_RXFC *ch_get_remote_stream_count_rxfc(const QUIC_CHANNEL *ch,
3824 return is_uni ? &ch->max_streams_uni_rxfc
3825 : &ch->max_streams_bidi_rxfc;
3828 int ossl_quic_channel_is_new_local_stream_admissible(QUIC_CHANNEL *ch,
3831 const uint64_t *p_next_ordinal = ch_get_local_stream_next_ordinal_ptr(ch, is_uni);
3833 return ossl_quic_stream_map_is_local_allowed_by_stream_limit(&ch->qsm,
3838 uint64_t ossl_quic_channel_get_local_stream_count_avail(const QUIC_CHANNEL *ch,
3843 p_next_ordinal = ch_get_local_stream_next_ordinal_ptr((QUIC_CHANNEL *)ch,
3845 p_max = ch_get_local_stream_max_ptr(ch, is_uni);
3850 uint64_t ossl_quic_channel_get_remote_stream_count_avail(const QUIC_CHANNEL *ch,
3853 return ossl_quic_rxfc_get_credit(ch_get_remote_stream_count_rxfc(ch, is_uni));
3856 QUIC_STREAM *ossl_quic_channel_new_stream_local(QUIC_CHANNEL *ch, int is_uni)
3863 type = ch->is_server ? QUIC_STREAM_INITIATOR_SERVER
3866 p_next_ordinal = ch_get_local_stream_next_ordinal_ptr(ch, is_uni);
3878 if ((qs = ossl_quic_stream_map_alloc(&ch->qsm, stream_id, type)) == NULL)
3882 if (!ch_init_new_stream(ch, qs, /*can_send=*/1, /*can_recv=*/!is_uni))
3889 ossl_quic_stream_map_release(&ch->qsm, qs);
3893 QUIC_STREAM *ossl_quic_channel_new_stream_remote(QUIC_CHANNEL *ch,
3900 peer_role = ch->is_server
3909 qs = ossl_quic_stream_map_alloc(&ch->qsm, stream_id,
3914 if (!ch_init_new_stream(ch, qs, /*can_send=*/!is_uni, /*can_recv=*/1))
3917 if (ch->incoming_stream_auto_reject)
3918 ossl_quic_channel_reject_stream(ch, qs);
3920 ossl_quic_stream_map_push_accept_queue(&ch->qsm, qs);
3925 ossl_quic_stream_map_release(&ch->qsm, qs);
3929 void ossl_quic_channel_set_incoming_stream_auto_reject(QUIC_CHANNEL *ch,
3933 ch->incoming_stream_auto_reject = (enable != 0);
3934 ch->incoming_stream_auto_reject_aec = aec;
3937 void ossl_quic_channel_reject_stream(QUIC_CHANNEL *ch, QUIC_STREAM *qs)
3939 ossl_quic_stream_map_stop_sending_recv_part(&ch->qsm, qs,
3940 ch->incoming_stream_auto_reject_aec);
3942 ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, qs,
3943 ch->incoming_stream_auto_reject_aec);
3946 ossl_quic_stream_map_update_state(&ch->qsm, qs);
3950 int ossl_quic_channel_replace_local_cid(QUIC_CHANNEL *ch,
3954 if (!ossl_quic_lcidm_debug_remove(ch->lcidm, &ch->cur_local_cid))
3956 ch->cur_local_cid = *conn_id;
3958 if (!ossl_quic_tx_packetiser_set_cur_scid(ch->txp, &ch->cur_local_cid))
3961 if (!ossl_quic_lcidm_debug_add(ch->lcidm, ch, &ch->cur_local_cid,
3967 void ossl_quic_channel_set_msg_callback(QUIC_CHANNEL *ch,
3971 ch->msg_callback = msg_callback;
3972 ch->msg_callback_ssl = msg_callback_ssl;
3973 ossl_qtx_set_msg_callback(ch->qtx, msg_callback, msg_callback_ssl);
3974 ossl_quic_tx_packetiser_set_msg_callback(ch->txp, msg_callback,
3980 if (ch->is_tserver_ch == 0)
3981 ossl_qrx_set_msg_callback(ch->qrx, msg_callback, msg_callback_ssl);
3984 void ossl_quic_channel_set_msg_callback_arg(QUIC_CHANNEL *ch,
3987 ch->msg_callback_arg = msg_callback_arg;
3988 ossl_qtx_set_msg_callback_arg(ch->qtx, msg_callback_arg);
3989 ossl_quic_tx_packetiser_set_msg_callback_arg(ch->txp, msg_callback_arg);
3995 if (ch->is_tserver_ch == 0)
3996 ossl_qrx_set_msg_callback_arg(ch->qrx, msg_callback_arg);
3999 void ossl_quic_channel_set_txku_threshold_override(QUIC_CHANNEL *ch,
4002 ch->txku_threshold_override = tx_pkt_threshold;
4005 uint64_t ossl_quic_channel_get_tx_key_epoch(QUIC_CHANNEL *ch)
4007 return ossl_qtx_get_key_epoch(ch->qtx);
4010 uint64_t ossl_quic_channel_get_rx_key_epoch(QUIC_CHANNEL *ch)
4012 return ossl_qrx_get_key_epoch(ch->qrx);
4015 int ossl_quic_channel_trigger_txku(QUIC_CHANNEL *ch)
4017 if (!txku_allowed(ch))
4020 ch->ku_locally_initiated = 1;
4021 ch_trigger_txku(ch);
4025 int ossl_quic_channel_ping(QUIC_CHANNEL *ch)
4027 int pn_space = ossl_quic_enc_level_to_pn_space(ch->tx_enc_level);
4029 ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, pn_space);
4034 uint16_t ossl_quic_channel_get_diag_num_rx_ack(QUIC_CHANNEL *ch)
4036 return ch->diag_num_rx_ack;
4039 void ossl_quic_channel_get_diag_local_cid(QUIC_CHANNEL *ch, QUIC_CONN_ID *cid)
4041 *cid = ch->cur_local_cid;
4044 int ossl_quic_channel_have_generated_transport_params(const QUIC_CHANNEL *ch)
4046 return ch->got_local_transport_params;
4049 void ossl_quic_channel_set_max_idle_timeout_request(QUIC_CHANNEL *ch, uint64_t ms)
4051 ch->max_idle_timeout_local_req = ms;
4053 uint64_t ossl_quic_channel_get_max_idle_timeout_request(const QUIC_CHANNEL *ch)
4055 return ch->max_idle_timeout_local_req;
4058 uint64_t ossl_quic_channel_get_max_idle_timeout_peer_request(const QUIC_CHANNEL *ch)
4060 return ch->max_idle_timeout_remote_req;
4063 uint64_t ossl_quic_channel_get_max_idle_timeout_actual(const QUIC_CHANNEL *ch)
4065 return ch->max_idle_timeout;