#include <haproxy/quic_frame.h>
#include <haproxy/quic_enc.h>
#include <haproxy/quic_loss.h>
+#include <haproxy/quic_rx.h>
#include <haproxy/quic_ssl.h>
#include <haproxy/quic_sock.h>
#include <haproxy/quic_stats.h>
#include <haproxy/quic_stream.h>
#include <haproxy/quic_tp.h>
#include <haproxy/quic_trace.h>
+#include <haproxy/quic_tx.h>
#include <haproxy/cbuf.h>
#include <haproxy/proto_quic.h>
#include <haproxy/quic_tls.h>
static BIO_METHOD *ha_quic_meth;
-DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
DECLARE_POOL(pool_head_quic_connection_id,
"quic_connection_id", sizeof(struct quic_connection_id));
-DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
-DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
-DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
DECLARE_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf", sizeof(struct quic_crypto_buf));
DECLARE_STATIC_POOL(pool_head_quic_cstream, "quic_cstream", sizeof(struct quic_cstream));
DECLARE_POOL(pool_head_quic_frame, "quic_frame", sizeof(struct quic_frame));
-static struct quic_connection_id *new_quic_cid(struct eb_root *root,
- struct quic_conn *qc,
- const struct quic_cid *odcid,
- const struct sockaddr_storage *saddr);
-static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
- struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
- struct list *frms, struct quic_conn *qc,
- const struct quic_version *ver, size_t dglen, int pkt_type,
- int must_ack, int padding, int probe, int cc, int *err);
struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
-static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack);
-static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack);
static int quic_conn_init_timer(struct quic_conn *qc);
static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
-static inline int quic_peer_validated_addr(struct quic_conn *qc)
+int quic_peer_validated_addr(struct quic_conn *qc)
{
if (!qc_is_listener(qc))
return 1;
/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
* both loss detection and PTO and schedule the task assiated to this timer if needed.
*/
-static inline void qc_set_timer(struct quic_conn *qc)
+void qc_set_timer(struct quic_conn *qc)
{
struct quic_pktns *pktns;
unsigned int pto;
return 1;
}
-/* Decode an expected packet number from <truncated_on> its truncated value,
- * depending on <largest_pn> the largest received packet number, and <pn_nbits>
- * the number of bits used to encode this packet number (its length in bytes * 8).
- * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
+/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
+ * and all STREAM data are acknowledged. The MUX is responsible to have set
+ * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
+ *
+ * TODO this should also be called on lost packet detection
*/
-static uint64_t decode_packet_number(uint64_t largest_pn,
- uint32_t truncated_pn, unsigned int pn_nbits)
+void qc_check_close_on_released_mux(struct quic_conn *qc)
{
- uint64_t expected_pn = largest_pn + 1;
- uint64_t pn_win = (uint64_t)1 << pn_nbits;
- uint64_t pn_hwin = pn_win / 2;
- uint64_t pn_mask = pn_win - 1;
- uint64_t candidate_pn;
+ TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
+ if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
+ /* Reuse errcode which should have been previously set by the MUX on release. */
+ quic_set_connection_close(qc, qc->err);
+ tasklet_wakeup(qc->wait_event.tasklet);
+ }
- candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
- /* Note that <pn_win> > <pn_hwin>. */
- if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
- candidate_pn + pn_hwin <= expected_pn)
- return candidate_pn + pn_win;
+ TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
+}
- if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
- return candidate_pn - pn_win;
+int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
+ const char **str, int *len);
- return candidate_pn;
-}
+/* Finalize <qc> QUIC connection:
-/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
- * cryptographic context.
- * <largest_pn> is the largest received packet number and <pn> the address of
- * the packet number field for this packet with <byte0> address of its first byte.
- * <end> points to one byte past the end of this packet.
- * Returns 1 if succeeded, 0 if not.
+ * MUST be called after having received the remote transport parameters which
+ * are parsed when the TLS callback for the ClientHello message is called upon
+ * SSL_do_handshake() calls, not necessarily at the first time as this TLS
+ * message may be split between packets
+ * Return 1 if succeeded, 0 if not.
*/
-static int qc_do_rm_hp(struct quic_conn *qc,
- struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
- int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
+int qc_conn_finalize(struct quic_conn *qc, int server)
{
- int ret, i, pnlen;
- uint64_t packet_number;
- uint32_t truncated_pn = 0;
- unsigned char mask[5] = {0};
- unsigned char *sample;
+ int ret = 0;
- TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
+ TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
- ret = 0;
+ if (qc->flags & QUIC_FL_CONN_FINALIZED)
+ goto finalized;
- /* Check there is enough data in this packet. */
- if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
- TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
- goto leave;
- }
+ if (!quic_tls_finalize(qc, server))
+ goto out;
- sample = pn + QUIC_PACKET_PN_MAXLEN;
+ /* This connection is functional (ready to send/receive) */
+ qc->flags |= QUIC_FL_CONN_FINALIZED;
- if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
- TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
- goto leave;
- }
+ finalized:
+ ret = 1;
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
+ return ret;
+}
- *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
- pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
- for (i = 0; i < pnlen; i++) {
- pn[i] ^= mask[i + 1];
- truncated_pn = (truncated_pn << 8) | pn[i];
- }
+void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
+{
+ TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
+
+ if (frm->type == QUIC_FT_CONNECTION_CLOSE)
+ quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
+ else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
+ if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
+ goto out;
- packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
- /* Store remaining information for this unprotected header */
- pkt->pn = packet_number;
- pkt->pnl = pnlen;
+ qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
+ }
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
- return ret;
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
}
-/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
- * address, with <payload_len> as payload length, <aad> as address of
- * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
- * context.
+/* Cancel a request on connection <qc> for stream id <id>. This is useful when
+ * the client opens a new stream but the MUX has already been released. A
+ * STOP_SENDING + RESET_STREAM frames are prepared for emission.
+ *
+ * TODO this function is closely related to H3. Its place should be in H3 layer
+ * instead of quic-conn but this requires an architecture adjustment.
*
- * TODO no error is expected as encryption is done in place but encryption
- * manual is unclear. <fail> will be set to true if an error is detected.
+ * Returns 1 on success else 0.
*/
-static void quic_packet_encrypt(unsigned char *payload, size_t payload_len,
- unsigned char *aad, size_t aad_len, uint64_t pn,
- struct quic_tls_ctx *tls_ctx, struct quic_conn *qc,
- int *fail)
+int qc_h3_request_reject(struct quic_conn *qc, uint64_t id)
{
- unsigned char iv[QUIC_TLS_IV_LEN];
- unsigned char *tx_iv = tls_ctx->tx.iv;
- size_t tx_iv_sz = tls_ctx->tx.ivlen;
- struct enc_debug_info edi;
+ int ret = 0;
+ struct quic_frame *ss, *rs;
+ struct quic_enc_level *qel = qc->ael;
+ const uint64_t app_error_code = H3_REQUEST_REJECTED;
- TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
- *fail = 0;
+ TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
- quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn);
+ /* Do not emit rejection for unknown unidirectional stream as it is
+ * forbidden to close some of them (H3 control stream and QPACK
+ * encoder/decoder streams).
+ */
+ if (quic_stream_is_uni(id)) {
+ ret = 1;
+ goto out;
+ }
- if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
- tls_ctx->tx.ctx, tls_ctx->tx.aead, iv)) {
- TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
- *fail = 1;
- enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
+ ss = qc_frm_alloc(QUIC_FT_STOP_SENDING);
+ if (!ss) {
+ TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
+ goto out;
}
- TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
-}
+ ss->stop_sending.id = id;
+ ss->stop_sending.app_error_code = app_error_code;
-/* Select the correct TLS cipher context to used to decipher <pkt> packet
- * attached to <qc> connection from <qel> encryption level.
- */
-static inline struct quic_tls_ctx *qc_select_tls_ctx(struct quic_conn *qc,
- struct quic_enc_level *qel,
- struct quic_rx_packet *pkt)
-{
- return pkt->type != QUIC_PACKET_TYPE_INITIAL ? &qel->tls_ctx :
- pkt->version == qc->negotiated_version ? qc->nictx : &qel->tls_ctx;
+ rs = qc_frm_alloc(QUIC_FT_RESET_STREAM);
+ if (!rs) {
+ TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
+ qc_frm_free(qc, &ss);
+ goto out;
+ }
+
+ rs->reset_stream.id = id;
+ rs->reset_stream.app_error_code = app_error_code;
+ rs->reset_stream.final_size = 0;
+
+ LIST_APPEND(&qel->pktns->tx.frms, &ss->list);
+ LIST_APPEND(&qel->pktns->tx.frms, &rs->list);
+ ret = 1;
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
+ return ret;
}
-/* Decrypt <pkt> packet using encryption level <qel> for <qc> connection.
- * Decryption is done in place in packet buffer.
+/* Build a NEW_CONNECTION_ID frame for <conn_id> CID of <qc> connection.
*
* Returns 1 on success else 0.
*/
-static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel,
- struct quic_rx_packet *pkt)
+int qc_build_new_connection_id_frm(struct quic_conn *qc,
+ struct quic_connection_id *conn_id)
{
- int ret, kp_changed;
- unsigned char iv[QUIC_TLS_IV_LEN];
- struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
- EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
- unsigned char *rx_iv = tls_ctx->rx.iv;
- size_t rx_iv_sz = tls_ctx->rx.ivlen;
- unsigned char *rx_key = tls_ctx->rx.key;
-
- TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
-
- ret = 0;
- kp_changed = 0;
-
- if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
- /* The two tested bits are not at the same position,
- * this is why they are first both inversed.
- */
- if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
- if (pkt->pn < tls_ctx->rx.pn) {
- /* The lowest packet number of a previous key phase
- * cannot be null if it really stores previous key phase
- * secrets.
- */
- // TODO: check if BUG_ON() more suitable
- if (!qc->ku.prv_rx.pn) {
- TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
- goto leave;
- }
-
- rx_ctx = qc->ku.prv_rx.ctx;
- rx_iv = qc->ku.prv_rx.iv;
- rx_key = qc->ku.prv_rx.key;
- }
- else if (pkt->pn > qel->pktns->rx.largest_pn) {
- /* Next key phase */
- TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc);
- kp_changed = 1;
- rx_ctx = qc->ku.nxt_rx.ctx;
- rx_iv = qc->ku.nxt_rx.iv;
- rx_key = qc->ku.nxt_rx.key;
- }
- }
- }
+ int ret = 0;
+ struct quic_frame *frm;
+ struct quic_enc_level *qel;
- quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn);
+ TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
- ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
- pkt->data, pkt->aad_len,
- rx_ctx, tls_ctx->rx.aead, rx_key, iv);
- if (!ret) {
- TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
+ qel = qc->ael;
+ frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
+ if (!frm) {
+ TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
goto leave;
}
- /* Update the keys only if the packet decryption succeeded. */
- if (kp_changed) {
- quic_tls_rotate_keys(qc);
- /* Toggle the Key Phase bit */
- tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
- /* Store the lowest packet number received for the current key phase */
- tls_ctx->rx.pn = pkt->pn;
- /* Prepare the next key update */
- if (!quic_tls_key_update(qc)) {
- TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
- goto leave;
- }
- }
-
- /* Update the packet length (required to parse the frames). */
- pkt->len -= QUIC_TLS_TAG_LEN;
+ quic_connection_id_to_frm_cpy(frm, conn_id);
+ LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
ret = 1;
leave:
- TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
+ TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
return ret;
}
-/* Release <frm> frame and mark its copies as acknowledged */
-void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
+/* Remove a <qc> quic-conn from its ha_thread_ctx list. If <closing> is true,
+ * it will immediately be reinserted in the ha_thread_ctx quic_conns_clo list.
+ */
+void qc_detach_th_ctx_list(struct quic_conn *qc, int closing)
{
- uint64_t pn;
- struct quic_frame *origin, *f, *tmp;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+ struct bref *bref, *back;
- /* Identify this frame: a frame copy or one of its copies */
- origin = frm->origin ? frm->origin : frm;
- /* Ensure the source of the copies is flagged as acked, <frm> being
- * possibly a copy of <origin>
- */
- origin->flags |= QUIC_FL_TX_FRAME_ACKED;
- /* Mark all the copy of <origin> as acknowledged. We must
- * not release the packets (releasing the frames) at this time as
- * they are possibly also to be acknowledged alongside the
- * the current one.
+ /* Detach CLI context watchers currently dumping this connection.
+ * Reattach them to the next quic_conn instance.
*/
- list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
- if (f->pkt) {
- f->flags |= QUIC_FL_TX_FRAME_ACKED;
- f->origin = NULL;
- LIST_DEL_INIT(&f->ref);
- pn = f->pkt->pn_node.key;
- TRACE_DEVEL("mark frame as acked from packet",
- QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
- }
- else {
- TRACE_DEVEL("freeing unsent frame",
- QUIC_EV_CONN_PRSAFRM, qc, f);
- LIST_DEL_INIT(&f->ref);
- qc_frm_free(qc, &f);
+ list_for_each_entry_safe(bref, back, &qc->back_refs, users) {
+ /* Remove watcher from this quic_conn instance. */
+ LIST_DEL_INIT(&bref->users);
+
+ /* Attach it to next instance unless it was the last list element. */
+ if (qc->el_th_ctx.n != &th_ctx->quic_conns &&
+ qc->el_th_ctx.n != &th_ctx->quic_conns_clo) {
+ struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx,
+ struct quic_conn *,
+ el_th_ctx);
+ LIST_APPEND(&next->back_refs, &bref->users);
}
+ bref->ref = qc->el_th_ctx.n;
+ __ha_barrier_store();
}
- LIST_DEL_INIT(&frm->list);
- pn = frm->pkt->pn_node.key;
- quic_tx_packet_refdec(frm->pkt);
- TRACE_DEVEL("freeing frame from packet",
- QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
- qc_frm_free(qc, &frm);
-
- TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
-}
-
-/* Schedule a CONNECTION_CLOSE emission on <qc> if the MUX has been released
- * and all STREAM data are acknowledged. The MUX is responsible to have set
- * <qc.err> before as it is reused for the CONNECTION_CLOSE frame.
- *
- * TODO this should also be called on lost packet detection
- */
-void qc_check_close_on_released_mux(struct quic_conn *qc)
-{
- TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
- if (qc->mux_state == QC_MUX_RELEASED && eb_is_empty(&qc->streams_by_id)) {
- /* Reuse errcode which should have been previously set by the MUX on release. */
- quic_set_connection_close(qc, qc->err);
- tasklet_wakeup(qc->wait_event.tasklet);
- }
+ /* Remove quic_conn from global ha_thread_ctx list. */
+ LIST_DEL_INIT(&qc->el_th_ctx);
- TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
+ if (closing)
+ LIST_APPEND(&th_ctx->quic_conns_clo, &qc->el_th_ctx);
}
-/* Remove from <stream> the acknowledged frames.
- *
- * Returns 1 if at least one frame was removed else 0.
+
+/* Copy at <pos> position a stateless reset token depending on the
+ * <salt> salt input. This is the cluster secret which will be derived
+ * as HKDF input secret to generate this token.
+ * Return 1 if succeeded, 0 if not.
*/
-static int quic_stream_try_to_consume(struct quic_conn *qc,
- struct qc_stream_desc *stream)
+int quic_stateless_reset_token_cpy(unsigned char *pos, size_t len,
+ const unsigned char *salt, size_t saltlen)
{
+ /* Input secret */
+ const unsigned char *key = (const unsigned char *)global.cluster_secret;
+ size_t keylen = strlen(global.cluster_secret);
+ /* Info */
+ const unsigned char label[] = "stateless token";
+ size_t labellen = sizeof label - 1;
int ret;
- struct eb64_node *frm_node;
-
- TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
-
- ret = 0;
- frm_node = eb64_first(&stream->acked_frms);
- while (frm_node) {
- struct qf_stream *strm_frm;
- struct quic_frame *frm;
- size_t offset, len;
-
- strm_frm = eb64_entry(frm_node, struct qf_stream, offset);
- offset = strm_frm->offset.key;
- len = strm_frm->len;
-
- if (offset > stream->ack_offset)
- break;
-
- if (qc_stream_desc_ack(&stream, offset, len)) {
- /* cf. next comment : frame may be freed at this stage. */
- TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
- qc, stream ? strm_frm : NULL, stream);
- ret = 1;
- }
-
- /* If stream is NULL after qc_stream_desc_ack(), it means frame
- * has been freed. with the stream frames tree. Nothing to do
- * anymore in here.
- */
- if (!stream) {
- qc_check_close_on_released_mux(qc);
- ret = 1;
- goto leave;
- }
- frm_node = eb64_next(frm_node);
- eb64_delete(&strm_frm->offset);
-
- frm = container_of(strm_frm, struct quic_frame, stream);
- qc_release_frm(qc, frm);
- }
-
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
+ ret = quic_hkdf_extract_and_expand(EVP_sha256(), pos, len,
+ key, keylen, salt, saltlen, label, labellen);
return ret;
}
-/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
-static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
- struct quic_frame *frm)
+/* Initialize the stateless reset token attached to <conn_id> connection ID.
+ * Returns 1 if succeeded, 0 if not.
+ */
+static int quic_stateless_reset_token_init(struct quic_connection_id *conn_id)
{
- TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
- TRACE_PROTO("RX ack TX frm", QUIC_EV_CONN_PRSAFRM, qc, frm);
-
- switch (frm->type) {
- case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
- {
- struct qf_stream *strm_frm = &frm->stream;
- struct eb64_node *node = NULL;
- struct qc_stream_desc *stream = NULL;
- const size_t offset = strm_frm->offset.key;
- const size_t len = strm_frm->len;
-
- /* do not use strm_frm->stream as the qc_stream_desc instance
- * might be freed at this stage. Use the id to do a proper
- * lookup.
- *
- * TODO if lookup operation impact on the perf is noticeable,
- * implement a refcount on qc_stream_desc instances.
- */
- node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
- if (!node) {
- TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
- qc_release_frm(qc, frm);
- /* early return */
- goto leave;
- }
- stream = eb64_entry(node, struct qc_stream_desc, by_id);
-
- TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
- if (offset <= stream->ack_offset) {
- if (qc_stream_desc_ack(&stream, offset, len)) {
- TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
- qc, strm_frm, stream);
- }
-
- if (!stream) {
- /* no need to continue if stream freed. */
- TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
- qc_release_frm(qc, frm);
- qc_check_close_on_released_mux(qc);
- break;
- }
+ int ret;
- TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
- qc, strm_frm, stream);
- qc_release_frm(qc, frm);
- }
- else {
- eb64_insert(&stream->acked_frms, &strm_frm->offset);
- }
+ if (global.cluster_secret) {
+ /* Output secret */
+ unsigned char *token = conn_id->stateless_reset_token;
+ size_t tokenlen = sizeof conn_id->stateless_reset_token;
+ /* Salt */
+ const unsigned char *cid = conn_id->cid.data;
+ size_t cidlen = conn_id->cid.len;
- quic_stream_try_to_consume(qc, stream);
+ ret = quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
}
- break;
- default:
- qc_release_frm(qc, frm);
+ else {
+ /* TODO: RAND_bytes() should be replaced */
+ ret = RAND_bytes(conn_id->stateless_reset_token,
+ sizeof conn_id->stateless_reset_token) == 1;
}
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+ return ret;
}
-/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
- * deallocating them, and their TX frames.
- * May be NULL if <largest> node could not be found.
+/* Generate a CID directly derived from <orig> CID and <addr> address.
+ *
+ * Returns the derived CID.
*/
-static inline void qc_ackrng_pkts(struct quic_conn *qc,
- struct eb_root *pkts,
- unsigned int *pkt_flags,
- struct list *newly_acked_pkts,
- struct eb64_node *largest_node,
- uint64_t largest, uint64_t smallest)
+struct quic_cid quic_derive_cid(const struct quic_cid *orig,
+ const struct sockaddr_storage *addr)
{
- struct eb64_node *node;
- struct quic_tx_packet *pkt;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+ struct quic_cid cid;
+ const struct sockaddr_in *in;
+ const struct sockaddr_in6 *in6;
+ char *pos = trash.area;
+ size_t idx = 0;
+ uint64_t hash;
+ int i;
- node = eb64_lookup_ge(pkts, smallest);
- if (!node)
- goto leave;
+ /* Prepare buffer for hash using original CID first. */
+ memcpy(pos, orig->data, orig->len);
+ idx += orig->len;
- largest_node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
- if (!largest_node)
- goto leave;
+ /* Concatenate client address. */
+ switch (addr->ss_family) {
+ case AF_INET:
+ in = (struct sockaddr_in *)addr;
- while (node && node->key <= largest_node->key) {
- struct quic_frame *frm, *frmbak;
-
- pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
- *pkt_flags |= pkt->flags;
- LIST_INSERT(newly_acked_pkts, &pkt->list);
- TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
- list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
- qc_treat_acked_tx_frm(qc, frm);
- /* If there are others packet in the same datagram <pkt> is attached to,
- * detach the previous one and the next one from <pkt>.
- */
- quic_tx_packet_dgram_detach(pkt);
- node = eb64_next(node);
- eb64_delete(&pkt->pn_node);
+ memcpy(&pos[idx], &in->sin_addr, sizeof(in->sin_addr));
+ idx += sizeof(in->sin_addr);
+ memcpy(&pos[idx], &in->sin_port, sizeof(in->sin_port));
+ idx += sizeof(in->sin_port);
+ break;
+
+ case AF_INET6:
+ in6 = (struct sockaddr_in6 *)addr;
+
+ memcpy(&pos[idx], &in6->sin6_addr, sizeof(in6->sin6_addr));
+ idx += sizeof(in6->sin6_addr);
+ memcpy(&pos[idx], &in6->sin6_port, sizeof(in6->sin6_port));
+ idx += sizeof(in6->sin6_port);
+ break;
+
+ default:
+ /* TODO to implement */
+ ABORT_NOW();
}
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+ /* Avoid similar values between multiple haproxy process. */
+ memcpy(&pos[idx], boot_seed, sizeof(boot_seed));
+ idx += sizeof(boot_seed);
+
+ /* Hash the final buffer content. */
+ hash = XXH64(pos, idx, 0);
+
+ for (i = 0; i < sizeof(hash); ++i)
+ cid.data[i] = hash >> ((sizeof(hash) * 7) - (8 * i));
+ cid.len = sizeof(hash);
+
+ return cid;
}
-/* Remove all frames from <pkt_frm_list> and reinsert them in the same order
- * they have been sent into <pktns_frm_list>. The loss counter of each frame is
- * incremented and checked if it does not exceed retransmission limit.
+/* Retrieve the thread ID associated to QUIC connection ID <cid> of length
+ * <cid_len>. CID may be not found on the CID tree because it is an ODCID. In
+ * this case, it will derived using client address <cli_addr> as hash
+ * parameter. However, this is done only if <pos> points to an INITIAL or 0RTT
+ * packet of length <len>.
*
- * Returns 1 on success, 0 if a frame loss limit is exceeded. A
- * CONNECTION_CLOSE is scheduled in this case.
+ * Returns the thread ID or a negative error code.
*/
-static inline int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
- struct quic_tx_packet *pkt,
- struct list *pktns_frm_list)
+int quic_get_cid_tid(const unsigned char *cid, size_t cid_len,
+ const struct sockaddr_storage *cli_addr,
+ unsigned char *pos, size_t len)
{
- struct quic_frame *frm, *frmbak;
- struct list *pkt_frm_list = &pkt->frms;
- uint64_t pn = pkt->pn_node.key;
- int close = 0;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
-
- list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
- /* First remove this frame from the packet it was attached to */
- LIST_DEL_INIT(&frm->list);
- quic_tx_packet_refdec(pkt);
- /* At this time, this frame is not freed but removed from its packet */
- frm->pkt = NULL;
- /* Remove any reference to this frame */
- qc_frm_unref(frm, qc);
- switch (frm->type) {
- case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
- {
- struct qf_stream *strm_frm = &frm->stream;
- struct eb64_node *node = NULL;
- struct qc_stream_desc *stream_desc;
-
- node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
- if (!node) {
- TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
- TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
- qc, frm, &pn);
- qc_frm_free(qc, &frm);
- continue;
- }
+ struct quic_cid_tree *tree;
+ struct quic_connection_id *conn_id;
+ struct ebmb_node *node;
- stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
- /* Do not resend this frame if in the "already acked range" */
- if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
- TRACE_DEVEL("ignored frame in already acked range",
- QUIC_EV_CONN_PRSAFRM, qc, frm);
- qc_frm_free(qc, &frm);
- continue;
- }
- else if (strm_frm->offset.key < stream_desc->ack_offset) {
- uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
+ tree = &quic_cid_trees[_quic_cid_tree_idx(cid)];
+ HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
+ node = ebmb_lookup(&tree->root, cid, cid_len);
+ HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
- qc_stream_frm_mv_fwd(frm, diff);
- TRACE_DEVEL("updated partially acked frame",
- QUIC_EV_CONN_PRSAFRM, qc, frm);
- }
- break;
- }
+ if (!node) {
+ struct quic_cid orig, derive_cid;
+ struct quic_rx_packet pkt;
- default:
- break;
- }
+ if (!qc_parse_hd_form(&pkt, &pos, pos + len))
+ goto not_found;
- /* Do not resend probing packet with old data */
- if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
- TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
- qc, frm, &pn);
- if (frm->origin)
- LIST_DEL_INIT(&frm->ref);
- qc_frm_free(qc, &frm);
- continue;
+ if (pkt.type != QUIC_PACKET_TYPE_INITIAL &&
+ pkt.type != QUIC_PACKET_TYPE_0RTT) {
+ goto not_found;
}
- if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
- TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
- TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
- qc, frm, &pn);
- qc_frm_free(qc, &frm);
- }
- else {
- if (++frm->loss_count >= global.tune.quic_max_frame_loss) {
- TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc);
- quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR));
- qc_notify_err(qc);
- close = 1;
- }
+ memcpy(orig.data, cid, cid_len);
+ orig.len = cid_len;
+ derive_cid = quic_derive_cid(&orig, cli_addr);
- LIST_APPEND(pktns_frm_list, &frm->list);
- TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
- }
+ tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
+ HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
+ node = ebmb_lookup(&tree->root, cid, cid_len);
+ HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
}
- end:
- TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
- return !close;
-}
-
-/* Free <pkt> TX packet and its attached frames.
- * This is the responsibility of the caller to remove this packet of
- * any data structure it was possibly attached to.
- */
-static inline void free_quic_tx_packet(struct quic_conn *qc,
- struct quic_tx_packet *pkt)
-{
- struct quic_frame *frm, *frmbak;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
-
- if (!pkt)
- goto leave;
+ if (!node)
+ goto not_found;
- list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
- qc_frm_free(qc, &frm);
- pool_free(pool_head_quic_tx_packet, pkt);
+ conn_id = ebmb_entry(node, struct quic_connection_id, node);
+ return HA_ATOMIC_LOAD(&conn_id->tid);
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ not_found:
+ return -1;
}
-/* Free the TX packets of <pkts> list */
-static inline void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
+/* Allocate a new CID and attach it to <root> ebtree.
+ *
+ * If <orig> and <addr> params are non null, the new CID value is directly
+ * derived from them. Else a random value is generated. The CID is then marked
+ * with the current thread ID.
+ *
+ * Returns the new CID if succeeded, NULL if not.
+ */
+struct quic_connection_id *new_quic_cid(struct eb_root *root,
+ struct quic_conn *qc,
+ const struct quic_cid *orig,
+ const struct sockaddr_storage *addr)
{
- struct quic_tx_packet *pkt, *tmp;
+ struct quic_connection_id *conn_id;
TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
- list_for_each_entry_safe(pkt, tmp, pkts, list) {
- LIST_DELETE(&pkt->list);
- eb64_delete(&pkt->pn_node);
- free_quic_tx_packet(qc, pkt);
+ /* Caller must set either none or both values. */
+ BUG_ON(!!orig != !!addr);
+
+ conn_id = pool_alloc(pool_head_quic_connection_id);
+ if (!conn_id) {
+ TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
+ goto err;
}
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
-}
+ conn_id->cid.len = QUIC_HAP_CID_LEN;
-/* Send a packet ack event nofication for each newly acked packet of
- * <newly_acked_pkts> list and free them.
- * Always succeeds.
- */
-static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
- struct list *newly_acked_pkts)
-{
- struct quic_tx_packet *pkt, *tmp;
- struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
-
- TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
-
- list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
- pkt->pktns->tx.in_flight -= pkt->in_flight_len;
- qc->path->prep_in_flight -= pkt->in_flight_len;
- qc->path->in_flight -= pkt->in_flight_len;
- if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
- qc->path->ifae_pkts--;
- /* If this packet contained an ACK frame, proceed to the
- * acknowledging of range of acks from the largest acknowledged
- * packet number which was sent in an ACK frame by this packet.
- */
- if (pkt->largest_acked_pn != -1)
- qc_treat_ack_of_ack(qc, &pkt->pktns->rx.arngs, pkt->largest_acked_pn);
- ev.ack.acked = pkt->in_flight_len;
- ev.ack.time_sent = pkt->time_sent;
- quic_cc_event(&qc->path->cc, &ev);
- LIST_DELETE(&pkt->list);
- eb64_delete(&pkt->pn_node);
- quic_tx_packet_refdec(pkt);
+ if (!orig) {
+ /* TODO: RAND_bytes() should be replaced */
+ if (RAND_bytes(conn_id->cid.data, conn_id->cid.len) != 1) {
+ TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
+ goto err;
+ }
+ }
+ else {
+ /* Derive the new CID value from original CID. */
+ conn_id->cid = quic_derive_cid(orig, addr);
}
- TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
-
-}
+ if (quic_stateless_reset_token_init(conn_id) != 1) {
+ TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
+ goto err;
+ }
-/* Release all the frames attached to <pktns> packet number space */
-static inline void qc_release_pktns_frms(struct quic_conn *qc,
- struct quic_pktns *pktns)
-{
- struct quic_frame *frm, *frmbak;
+ conn_id->qc = qc;
+ HA_ATOMIC_STORE(&conn_id->tid, tid);
- TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
+ conn_id->seq_num.key = qc ? qc->next_cid_seq_num++ : 0;
+ conn_id->retire_prior_to = 0;
+ /* insert the allocated CID in the quic_conn tree */
+ if (root)
+ eb64_insert(root, &conn_id->seq_num);
- list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list)
- qc_frm_free(qc, &frm);
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return conn_id;
- TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
+ err:
+ pool_free(pool_head_quic_connection_id, conn_id);
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return NULL;
}
-/* Handle <pkts> list of lost packets detected at <now_us> handling their TX
- * frames. Send a packet loss event to the congestion controller if in flight
- * packet have been lost. Also frees the packet in <pkts> list.
- *
- * Returns 1 on success else 0 if loss limit has been exceeded. A
- * CONNECTION_CLOSE was prepared to close the connection ASAP.
- */
-static inline int qc_release_lost_pkts(struct quic_conn *qc,
- struct quic_pktns *pktns,
- struct list *pkts,
- uint64_t now_us)
+/* QUIC connection packet handler task (post handshake) */
+struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
{
- struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
- int close = 0;
+ struct quic_conn *qc = context;
+ struct quic_enc_level *qel;
- TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+ TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
- if (LIST_ISEMPTY(pkts))
- goto leave;
+ qel = qc->ael;
+ TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
- oldest_lost = newest_lost = NULL;
- list_for_each_entry_safe(pkt, tmp, pkts, list) {
- struct list tmp = LIST_HEAD_INIT(tmp);
-
- pkt->pktns->tx.in_flight -= pkt->in_flight_len;
- qc->path->prep_in_flight -= pkt->in_flight_len;
- qc->path->in_flight -= pkt->in_flight_len;
- if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
- qc->path->ifae_pkts--;
- /* Treat the frames of this lost packet. */
- if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms))
- close = 1;
- LIST_DELETE(&pkt->list);
- if (!oldest_lost) {
- oldest_lost = newest_lost = pkt;
- }
- else {
- if (newest_lost != oldest_lost)
- quic_tx_packet_refdec(newest_lost);
- newest_lost = pkt;
- }
- }
+ if (qc_test_fd(qc))
+ qc_rcv_buf(qc);
- if (!close) {
- if (newest_lost) {
- /* Sent a congestion event to the controller */
- struct quic_cc_event ev = { };
+ /* Prepare post-handshake frames
+ * - after connection is instantiated (accept is done)
+ * - handshake state is completed (may not be the case here in 0-RTT)
+ */
+ if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && qc->conn &&
+ qc->state >= QUIC_HS_ST_COMPLETE) {
+ quic_build_post_handshake_frames(qc);
+ }
- ev.type = QUIC_CC_EVT_LOSS;
- ev.loss.time_sent = newest_lost->time_sent;
+ /* Retranmissions */
+ if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
+ TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
+ qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
+ if (!qc_dgrams_retransmit(qc))
+ goto out;
+ }
- quic_cc_event(&qc->path->cc, &ev);
- }
+ if (!qc_treat_rx_pkts(qc)) {
+ TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
+ goto out;
+ }
- /* If an RTT have been already sampled, <rtt_min> has been set.
- * We must check if we are experiencing a persistent congestion.
- * If this is the case, the congestion controller must re-enter
- * slow start state.
- */
- if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
- unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
+ if (qc->flags & QUIC_FL_CONN_TO_KILL) {
+ TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
+ goto out;
+ }
- if (quic_loss_persistent_congestion(&qc->path->loss, period,
- now_ms, qc->max_ack_delay))
- qc->path->cc.algo->slow_start(&qc->path->cc);
- }
+ if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
+ !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
+ TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
+ goto out;
}
- /* <oldest_lost> cannot be NULL at this stage because we have ensured
- * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a
- * possible overflow on a 0 byte region with O2 optimization.
- */
- ALREADY_CHECKED(oldest_lost);
- quic_tx_packet_refdec(oldest_lost);
- if (newest_lost != oldest_lost)
- quic_tx_packet_refdec(newest_lost);
+ /* XXX TODO: how to limit the list frames to send */
+ if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
+ TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
+ goto out;
+ }
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
- return !close;
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
+ return t;
}
-/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
- * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
- * if the largest acked packet was newly acked and if there was at least one newly
- * acked ack-eliciting packet.
- * Return 1, if succeeded, 0 if not.
- */
-static inline int qc_parse_ack_frm(struct quic_conn *qc,
- struct quic_frame *frm,
- struct quic_enc_level *qel,
- unsigned int *rtt_sample,
- const unsigned char **pos, const unsigned char *end)
+/* QUIC connection packet handler task. */
+struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
{
- struct qf_ack *ack_frm = &frm->ack;
- uint64_t smallest, largest;
- struct eb_root *pkts;
- struct eb64_node *largest_node;
- unsigned int time_sent, pkt_flags;
- struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
- struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
- int ret = 0;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+ int ret;
+ struct quic_conn *qc = context;
+ struct buffer *buf = NULL;
+ int st;
- if (ack_frm->largest_ack > qel->pktns->tx.next_pn) {
- TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
- qc, NULL, &ack_frm->largest_ack);
- goto err;
- }
+ TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
- if (ack_frm->first_ack_range > ack_frm->largest_ack) {
- TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
- qc, NULL, &ack_frm->first_ack_range);
- goto err;
- }
+ st = qc->state;
+ TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
- largest = ack_frm->largest_ack;
- smallest = largest - ack_frm->first_ack_range;
- pkts = &qel->pktns->tx.pkts;
- pkt_flags = 0;
- largest_node = NULL;
- time_sent = 0;
-
- if ((int64_t)ack_frm->largest_ack > qel->pktns->rx.largest_acked_pn) {
- largest_node = eb64_lookup(pkts, largest);
- if (!largest_node) {
- TRACE_DEVEL("Largest acked packet not found",
- QUIC_EV_CONN_PRSAFRM, qc);
- }
- else {
- time_sent = eb64_entry(largest_node,
- struct quic_tx_packet, pn_node)->time_sent;
- }
+ /* Retranmissions */
+ if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
+ TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
+ qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
+ if (!qc_dgrams_retransmit(qc))
+ goto out;
}
- TRACE_PROTO("RX ack range", QUIC_EV_CONN_PRSAFRM,
- qc, NULL, &largest, &smallest);
- do {
- uint64_t gap, ack_range;
-
- qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
- largest_node, largest, smallest);
- if (!ack_frm->ack_range_num--)
- break;
+ if (qc_test_fd(qc))
+ qc_rcv_buf(qc);
- if (!quic_dec_int(&gap, pos, end)) {
- TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
- goto err;
- }
+ if (!qc_treat_rx_pkts(qc))
+ goto out;
- if (smallest < gap + 2) {
- TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
- qc, NULL, &gap, &smallest);
- goto err;
- }
+ if (qc->flags & QUIC_FL_CONN_TO_KILL) {
+ TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
+ goto out;
+ }
- largest = smallest - gap - 2;
- if (!quic_dec_int(&ack_range, pos, end)) {
- TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
- goto err;
- }
+ if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
+ !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
+ goto out;
- if (largest < ack_range) {
- TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
- qc, NULL, &largest, &ack_range);
- goto err;
+ st = qc->state;
+ if (st >= QUIC_HS_ST_COMPLETE) {
+ if (!(qc->flags & QUIC_FL_CONN_HPKTNS_DCD)) {
+ /* Discard the Handshake packet number space. */
+ TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
+ quic_pktns_discard(qc->hel->pktns, qc);
+ qc_set_timer(qc);
+ qc_el_rx_pkts_del(qc->hel);
+ qc_release_pktns_frms(qc, qc->hel->pktns);
}
-
- /* Do not use this node anymore. */
- largest_node = NULL;
- /* Next range */
- smallest = largest - ack_range;
-
- TRACE_PROTO("RX next ack range", QUIC_EV_CONN_PRSAFRM,
- qc, NULL, &largest, &smallest);
- } while (1);
-
- if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
- *rtt_sample = tick_remain(time_sent, now_ms);
- qel->pktns->rx.largest_acked_pn = ack_frm->largest_ack;
}
- if (!LIST_ISEMPTY(&newly_acked_pkts)) {
- if (!eb_is_empty(&qel->pktns->tx.pkts)) {
- qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
- if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms))
- goto leave;
- }
- qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
- if (quic_peer_validated_addr(qc))
- qc->path->loss.pto_count = 0;
- qc_set_timer(qc);
- qc_notify_send(qc);
- }
+ buf = qc_txb_alloc(qc);
+ if (!buf)
+ goto out;
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
- return ret;
-
- err:
- free_quic_tx_pkts(qc, &newly_acked_pkts);
- goto leave;
-}
-
-int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
- const char **str, int *len);
-
-/* Finalize <qc> QUIC connection:
-
- * MUST be called after having received the remote transport parameters which
- * are parsed when the TLS callback for the ClientHello message is called upon
- * SSL_do_handshake() calls, not necessarily at the first time as this TLS
- * message may be split between packets
- * Return 1 if succeeded, 0 if not.
- */
-int qc_conn_finalize(struct quic_conn *qc, int server)
-{
- int ret = 0;
-
- TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
-
- if (qc->flags & QUIC_FL_CONN_FINALIZED)
- goto finalized;
-
- if (!quic_tls_finalize(qc, server))
- goto out;
-
- /* This connection is functional (ready to send/receive) */
- qc->flags |= QUIC_FL_CONN_FINALIZED;
-
- finalized:
- ret = 1;
- out:
- TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
- return ret;
-}
-
-/* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc>
- * connection. <fin> is true if FIN bit is set on frame type.
- *
- * Return 1 on success. On error, 0 is returned. In this case, the packet
- * containing the frame must not be acknowledged.
- */
-static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
- struct qf_stream *strm_frm,
- struct quic_conn *qc, char fin)
-{
- int ret;
-
- /* RFC9000 13.1. Packet Processing
- *
- * A packet MUST NOT be acknowledged until packet protection has been
- * successfully removed and all frames contained in the packet have
- * been processed. For STREAM frames, this means the data has been
- * enqueued in preparation to be received by the application protocol,
- * but it does not require that data be delivered and consumed.
- */
- TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
-
- ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
- strm_frm->offset.key, fin, (char *)strm_frm->data);
-
- /* frame rejected - packet must not be acknowledeged */
- TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
- return !ret;
-}
-
-/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
- * for <qc> QUIC connection.
- * This is a best effort function which never fails even if no memory could be
- * allocated to duplicate these frames.
- */
-static void qc_dup_pkt_frms(struct quic_conn *qc,
- struct list *pkt_frm_list, struct list *out_frm_list)
-{
- struct quic_frame *frm, *frmbak;
- struct list tmp = LIST_HEAD_INIT(tmp);
-
- TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
-
- list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
- struct quic_frame *dup_frm, *origin;
-
- if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
- TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
- continue;
- }
-
- switch (frm->type) {
- case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
- {
- struct qf_stream *strm_frm = &frm->stream;
- struct eb64_node *node = NULL;
- struct qc_stream_desc *stream_desc;
-
- node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
- if (!node) {
- TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
- continue;
- }
-
- stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
- /* Do not resend this frame if in the "already acked range" */
- if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
- TRACE_DEVEL("ignored frame in already acked range",
- QUIC_EV_CONN_PRSAFRM, qc, frm);
- continue;
- }
- else if (strm_frm->offset.key < stream_desc->ack_offset) {
- uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
-
- qc_stream_frm_mv_fwd(frm, diff);
- TRACE_DEVEL("updated partially acked frame",
- QUIC_EV_CONN_PRSAFRM, qc, frm);
- }
-
- strm_frm->dup = 1;
- break;
- }
-
- default:
- break;
- }
-
- /* If <frm> is already a copy of another frame, we must take
- * its original frame as source for the copy.
- */
- origin = frm->origin ? frm->origin : frm;
- dup_frm = qc_frm_dup(origin);
- if (!dup_frm) {
- TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
- break;
- }
-
- TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
- if (origin->pkt) {
- TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
- qc, NULL, &origin->pkt->pn_node.key);
- }
- else {
- /* <origin> is a frame which was sent from a packet detected as lost. */
- TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
- }
-
- LIST_APPEND(&tmp, &dup_frm->list);
- }
-
- LIST_SPLICE(out_frm_list, &tmp);
-
- TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
-}
-
-/* Boolean function which return 1 if <pkt> TX packet is only made of
- * already acknowledged frame.
- */
-static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt)
-{
- struct quic_frame *frm;
-
- list_for_each_entry(frm, &pkt->frms, list)
- if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED))
- return 0;
-
- return 1;
-}
-
-/* Prepare a fast retransmission from <qel> encryption level */
-static void qc_prep_fast_retrans(struct quic_conn *qc,
- struct quic_pktns *pktns,
- struct list *frms1, struct list *frms2)
-{
- struct eb_root *pkts = &pktns->tx.pkts;
- struct list *frms = frms1;
- struct eb64_node *node;
- struct quic_tx_packet *pkt;
-
- TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
-
- BUG_ON(frms1 == frms2);
-
- pkt = NULL;
- node = eb64_first(pkts);
- start:
- while (node) {
- struct quic_tx_packet *p;
-
- p = eb64_entry(node, struct quic_tx_packet, pn_node);
- node = eb64_next(node);
- /* Skip the empty and coalesced packets */
- TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
- "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key,
- LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED),
- qc_pkt_with_only_acked_frms(p));
- if (!LIST_ISEMPTY(&p->frms) && !qc_pkt_with_only_acked_frms(p)) {
- pkt = p;
- break;
- }
- }
-
- if (!pkt)
- goto leave;
-
- /* When building a packet from another one, the field which may increase the
- * packet size is the packet number. And the maximum increase is 4 bytes.
- */
- if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
- pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
- qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
- TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
- goto leave;
- }
-
- TRACE_PROTO("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
- qc_dup_pkt_frms(qc, &pkt->frms, frms);
- if (frms == frms1 && frms2) {
- frms = frms2;
- goto start;
- }
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
-}
-
-/* Prepare a fast retransmission during a handshake after a client
- * has resent Initial packets. According to the RFC a server may retransmit
- * Initial packets send them coalescing with others (Handshake here).
- * (Listener only function).
- */
-static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
- struct list *ifrms, struct list *hfrms)
-{
- struct list itmp = LIST_HEAD_INIT(itmp);
- struct list htmp = LIST_HEAD_INIT(htmp);
-
- struct quic_enc_level *iqel = qc->iel;
- struct quic_enc_level *hqel = qc->hel;
- struct quic_enc_level *qel = iqel;
- struct eb_root *pkts;
- struct eb64_node *node;
- struct quic_tx_packet *pkt;
- struct list *tmp = &itmp;
-
- TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
- start:
- pkt = NULL;
- pkts = &qel->pktns->tx.pkts;
- node = eb64_first(pkts);
- /* Skip the empty packet (they have already been retransmitted) */
- while (node) {
- struct quic_tx_packet *p;
-
- p = eb64_entry(node, struct quic_tx_packet, pn_node);
- TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
- "--> pn=%llu (%d %d)", (ull)p->pn_node.key,
- LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED));
- if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) &&
- !qc_pkt_with_only_acked_frms(p)) {
- pkt = p;
- break;
- }
-
- node = eb64_next(node);
- }
-
- if (!pkt)
- goto end;
-
- /* When building a packet from another one, the field which may increase the
- * packet size is the packet number. And the maximum increase is 4 bytes.
- */
- if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
- size_t dglen = pkt->len + 4;
- size_t may_send = 3 * qc->rx.bytes - qc->tx.prep_bytes;
-
- dglen += pkt->next ? pkt->next->len + 4 : 0;
- if (dglen > may_send) {
- qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
- TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
- if (pkt->next)
- TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next);
- if (qel == iqel && may_send >= QUIC_INITIAL_PACKET_MINLEN)
- TRACE_PROTO("will probe Initial packet number space", QUIC_EV_CONN_SPPKTS, qc);
- goto end;
- }
- }
-
- qel->pktns->tx.pto_probe += 1;
-
- /* No risk to loop here, #packet per datagram is bounded */
- requeue:
- TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
- qc_dup_pkt_frms(qc, &pkt->frms, tmp);
- if (qel == iqel) {
- if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
- pkt = pkt->next;
- tmp = &htmp;
- hqel->pktns->tx.pto_probe += 1;
- TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc);
- goto requeue;
- }
- }
-
- end:
- LIST_SPLICE(ifrms, &itmp);
- LIST_SPLICE(hfrms, &htmp);
-
- TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
-}
-
-static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
-{
- TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
-
- if (frm->type == QUIC_FT_CONNECTION_CLOSE)
- quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
- else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
- if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
- goto out;
-
- qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
- }
-
- out:
- TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
-}
-
-/* Cancel a request on connection <qc> for stream id <id>. This is useful when
- * the client opens a new stream but the MUX has already been released. A
- * STOP_SENDING + RESET_STREAM frames are prepared for emission.
- *
- * TODO this function is closely related to H3. Its place should be in H3 layer
- * instead of quic-conn but this requires an architecture adjustment.
- *
- * Returns 1 on success else 0.
- */
-static int qc_h3_request_reject(struct quic_conn *qc, uint64_t id)
-{
- int ret = 0;
- struct quic_frame *ss, *rs;
- struct quic_enc_level *qel = qc->ael;
- const uint64_t app_error_code = H3_REQUEST_REJECTED;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
-
- /* Do not emit rejection for unknown unidirectional stream as it is
- * forbidden to close some of them (H3 control stream and QPACK
- * encoder/decoder streams).
- */
- if (quic_stream_is_uni(id)) {
- ret = 1;
- goto out;
- }
-
- ss = qc_frm_alloc(QUIC_FT_STOP_SENDING);
- if (!ss) {
- TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
- goto out;
- }
-
- ss->stop_sending.id = id;
- ss->stop_sending.app_error_code = app_error_code;
-
- rs = qc_frm_alloc(QUIC_FT_RESET_STREAM);
- if (!rs) {
- TRACE_ERROR("failed to allocate quic_frame", QUIC_EV_CONN_PRSHPKT, qc);
- qc_frm_free(qc, &ss);
- goto out;
- }
-
- rs->reset_stream.id = id;
- rs->reset_stream.app_error_code = app_error_code;
- rs->reset_stream.final_size = 0;
-
- LIST_APPEND(&qel->pktns->tx.frms, &ss->list);
- LIST_APPEND(&qel->pktns->tx.frms, &rs->list);
- ret = 1;
- out:
- TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
- return ret;
-}
-
-/* Release the underlying memory use by <ncbuf> non-contiguous buffer */
-static void quic_free_ncbuf(struct ncbuf *ncbuf)
-{
- struct buffer buf;
-
- if (ncb_is_null(ncbuf))
- return;
-
- buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
- b_free(&buf);
- offer_buffers(NULL, 1);
-
- *ncbuf = NCBUF_NULL;
-}
-
-/* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */
-static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf)
-{
- struct buffer buf = BUF_NULL;
-
- if (!ncb_is_null(ncbuf))
- return ncbuf;
-
- b_alloc(&buf);
- BUG_ON(b_is_null(&buf));
-
- *ncbuf = ncb_make(buf.area, buf.size, 0);
- ncb_init(ncbuf, 0);
-
- return ncbuf;
-}
-
-/* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn.
- * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the
- * speed up handshake completion may be run after having received duplicated
- * CRYPTO data.
- */
-static int qc_handle_crypto_frm(struct quic_conn *qc,
- struct qf_crypto *crypto_frm, struct quic_rx_packet *pkt,
- struct quic_enc_level *qel, int *fast_retrans)
-{
- int ret = 0;
- enum ncb_ret ncb_ret;
- /* XXX TO DO: <cfdebug> is used only for the traces. */
- struct quic_rx_crypto_frm cfdebug = {
- .offset_node.key = crypto_frm->offset,
- .len = crypto_frm->len,
- };
- struct quic_cstream *cstream = qel->cstream;
- struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
-
- if (unlikely(crypto_frm->offset < cstream->rx.offset)) {
- size_t diff;
-
- if (crypto_frm->offset + crypto_frm->len <= cstream->rx.offset) {
- /* Nothing to do */
- TRACE_PROTO("Already received CRYPTO data",
- QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
- if (qc_is_listener(qc) && qel == qc->iel &&
- !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
- *fast_retrans = 1;
- goto done;
- }
-
- TRACE_PROTO("Partially already received CRYPTO data",
- QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
-
- diff = cstream->rx.offset - crypto_frm->offset;
- crypto_frm->len -= diff;
- crypto_frm->data += diff;
- crypto_frm->offset = cstream->rx.offset;
- }
-
- if (crypto_frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) {
- if (!qc_ssl_provide_quic_data(&qel->cstream->rx.ncbuf, qel->level,
- qc->xprt_ctx, crypto_frm->data, crypto_frm->len)) {
- // trace already emitted by function above
- goto leave;
- }
-
- cstream->rx.offset += crypto_frm->len;
- TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel);
- goto done;
- }
-
- if (!quic_get_ncbuf(ncbuf) ||
- ncb_is_null(ncbuf)) {
- TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc);
- goto leave;
- }
-
- /* crypto_frm->offset > cstream-trx.offset */
- ncb_ret = ncb_add(ncbuf, crypto_frm->offset - cstream->rx.offset,
- (const char *)crypto_frm->data, crypto_frm->len, NCB_ADD_COMPARE);
- if (ncb_ret != NCB_RET_OK) {
- if (ncb_ret == NCB_RET_DATA_REJ) {
- TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc);
- quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
- qc_notify_err(qc);
- }
- else if (ncb_ret == NCB_RET_GAP_SIZE) {
- TRACE_ERROR("cannot bufferize frame due to gap size limit",
- QUIC_EV_CONN_PRSHPKT, qc);
- }
- goto leave;
- }
-
- done:
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
- return ret;
-}
-
-/* Build a NEW_CONNECTION_ID frame for <conn_id> CID of <qc> connection.
- *
- * Returns 1 on success else 0.
- */
-static int qc_build_new_connection_id_frm(struct quic_conn *qc,
- struct quic_connection_id *conn_id)
-{
- int ret = 0;
- struct quic_frame *frm;
- struct quic_enc_level *qel;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
-
- qel = qc->ael;
- frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
- if (!frm) {
- TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
- goto leave;
- }
-
- quic_connection_id_to_frm_cpy(frm, conn_id);
- LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
- return ret;
-}
-
-
-/* Handle RETIRE_CONNECTION_ID frame from <frm> frame.
- * Return 1 if succeeded, 0 if not. If succeeded, also set <to_retire>
- * to the CID to be retired if not already retired.
- */
-static int qc_handle_retire_connection_id_frm(struct quic_conn *qc,
- struct quic_frame *frm,
- struct quic_cid *dcid,
- struct quic_connection_id **to_retire)
-{
- int ret = 0;
- struct qf_retire_connection_id *rcid_frm = &frm->retire_connection_id;
- struct eb64_node *node;
- struct quic_connection_id *conn_id;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
-
- /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
- * Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number greater
- * than any previously sent to the peer MUST be treated as a connection error
- * of type PROTOCOL_VIOLATION.
- */
- if (rcid_frm->seq_num >= qc->next_cid_seq_num) {
- TRACE_PROTO("CID seq. number too big", QUIC_EV_CONN_PSTRM, qc, frm);
- goto protocol_violation;
- }
-
- /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
- * The sequence number specified in a RETIRE_CONNECTION_ID frame MUST NOT refer to
- * the Destination Connection ID field of the packet in which the frame is contained.
- * The peer MAY treat this as a connection error of type PROTOCOL_VIOLATION.
- */
- node = eb64_lookup(&qc->cids, rcid_frm->seq_num);
- if (!node) {
- TRACE_PROTO("CID already retired", QUIC_EV_CONN_PSTRM, qc, frm);
- goto out;
- }
-
- conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
- /* Note that the length of <dcid> has already been checked. It must match the
- * length of the CIDs which have been provided to the peer.
- */
- if (!memcmp(dcid->data, conn_id->cid.data, QUIC_HAP_CID_LEN)) {
- TRACE_PROTO("cannot retire the current CID", QUIC_EV_CONN_PSTRM, qc, frm);
- goto protocol_violation;
- }
-
- *to_retire = conn_id;
- out:
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
- return ret;
- protocol_violation:
- quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
- qc_notify_err(qc);
- goto leave;
-}
-
-/* Remove a <qc> quic-conn from its ha_thread_ctx list. If <closing> is true,
- * it will immediately be reinserted in the ha_thread_ctx quic_conns_clo list.
- */
-static void qc_detach_th_ctx_list(struct quic_conn *qc, int closing)
-{
- struct bref *bref, *back;
-
- /* Detach CLI context watchers currently dumping this connection.
- * Reattach them to the next quic_conn instance.
- */
- list_for_each_entry_safe(bref, back, &qc->back_refs, users) {
- /* Remove watcher from this quic_conn instance. */
- LIST_DEL_INIT(&bref->users);
-
- /* Attach it to next instance unless it was the last list element. */
- if (qc->el_th_ctx.n != &th_ctx->quic_conns &&
- qc->el_th_ctx.n != &th_ctx->quic_conns_clo) {
- struct quic_conn *next = LIST_NEXT(&qc->el_th_ctx,
- struct quic_conn *,
- el_th_ctx);
- LIST_APPEND(&next->back_refs, &bref->users);
- }
- bref->ref = qc->el_th_ctx.n;
- __ha_barrier_store();
- }
-
- /* Remove quic_conn from global ha_thread_ctx list. */
- LIST_DEL_INIT(&qc->el_th_ctx);
-
- if (closing)
- LIST_APPEND(&th_ctx->quic_conns_clo, &qc->el_th_ctx);
-}
-
-/* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel>
- * as encryption level.
- * Returns 1 if succeeded, 0 if failed.
- */
-static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt,
- struct quic_enc_level *qel)
-{
- struct quic_frame frm;
- const unsigned char *pos, *end;
- int fast_retrans = 0, ret = 0;
-
- TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
- /* Skip the AAD */
- pos = pkt->data + pkt->aad_len;
- end = pkt->data + pkt->len;
-
- while (pos < end) {
- if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
- // trace already emitted by function above
- goto leave;
- }
-
- switch (frm.type) {
- case QUIC_FT_PADDING:
- break;
- case QUIC_FT_PING:
- break;
- case QUIC_FT_ACK:
- {
- unsigned int rtt_sample;
-
- rtt_sample = UINT_MAX;
- if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
- // trace already emitted by function above
- goto leave;
- }
-
- if (rtt_sample != UINT_MAX) {
- unsigned int ack_delay;
-
- ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
- qc->state >= QUIC_HS_ST_CONFIRMED ?
- MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
- MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
- quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
- }
- break;
- }
- case QUIC_FT_RESET_STREAM:
- if (qc->mux_state == QC_MUX_READY) {
- struct qf_reset_stream *rs_frm = &frm.reset_stream;
- qcc_recv_reset_stream(qc->qcc, rs_frm->id, rs_frm->app_error_code, rs_frm->final_size);
- }
- break;
- case QUIC_FT_STOP_SENDING:
- {
- struct qf_stop_sending *ss_frm = &frm.stop_sending;
- if (qc->mux_state == QC_MUX_READY) {
- if (qcc_recv_stop_sending(qc->qcc, ss_frm->id,
- ss_frm->app_error_code)) {
- TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
- goto leave;
- }
- }
- break;
- }
- case QUIC_FT_CRYPTO:
- if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans))
- goto leave;
- break;
- case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
- {
- struct qf_stream *strm_frm = &frm.stream;
- unsigned nb_streams = qc->rx.strms[qcs_id_type(strm_frm->id)].nb_streams;
- const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT;
-
- /* The upper layer may not be allocated. */
- if (qc->mux_state != QC_MUX_READY) {
- if ((strm_frm->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
- TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
- }
- else {
- TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
- if (qc->app_ops == &h3_ops) {
- if (!qc_h3_request_reject(qc, strm_frm->id)) {
- TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc);
- /* This packet will not be acknowledged */
- goto leave;
- }
- }
- else {
- /* This packet will not be acknowledged */
- goto leave;
- }
- }
-
- break;
- }
-
- if (!qc_handle_strm_frm(pkt, strm_frm, qc, fin)) {
- TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
- goto leave;
- }
-
- break;
- }
- case QUIC_FT_MAX_DATA:
- if (qc->mux_state == QC_MUX_READY) {
- struct qf_max_data *md_frm = &frm.max_data;
- qcc_recv_max_data(qc->qcc, md_frm->max_data);
- }
- break;
- case QUIC_FT_MAX_STREAM_DATA:
- if (qc->mux_state == QC_MUX_READY) {
- struct qf_max_stream_data *msd_frm = &frm.max_stream_data;
- if (qcc_recv_max_stream_data(qc->qcc, msd_frm->id,
- msd_frm->max_stream_data)) {
- TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
- goto leave;
- }
- }
- break;
- case QUIC_FT_MAX_STREAMS_BIDI:
- case QUIC_FT_MAX_STREAMS_UNI:
- break;
- case QUIC_FT_DATA_BLOCKED:
- qc->cntrs.data_blocked++;
- break;
- case QUIC_FT_STREAM_DATA_BLOCKED:
- qc->cntrs.stream_data_blocked++;
- break;
- case QUIC_FT_STREAMS_BLOCKED_BIDI:
- qc->cntrs.streams_blocked_bidi++;
- break;
- case QUIC_FT_STREAMS_BLOCKED_UNI:
- qc->cntrs.streams_blocked_uni++;
- break;
- case QUIC_FT_NEW_CONNECTION_ID:
- /* XXX TO DO XXX */
- break;
- case QUIC_FT_RETIRE_CONNECTION_ID:
- {
- struct quic_connection_id *conn_id = NULL;
-
- if (!qc_handle_retire_connection_id_frm(qc, &frm, &pkt->dcid, &conn_id))
- goto leave;
-
- if (!conn_id)
- break;
-
- ebmb_delete(&conn_id->node);
- eb64_delete(&conn_id->seq_num);
- pool_free(pool_head_quic_connection_id, conn_id);
- TRACE_PROTO("CID retired", QUIC_EV_CONN_PSTRM, qc);
-
- conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
- if (!conn_id) {
- TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
- }
- else {
- quic_cid_insert(conn_id);
- qc_build_new_connection_id_frm(qc, conn_id);
- }
- break;
- }
- case QUIC_FT_CONNECTION_CLOSE:
- case QUIC_FT_CONNECTION_CLOSE_APP:
- /* Increment the error counters */
- qc_cc_err_count_inc(qc, &frm);
- if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
- if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
- qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
- HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
- }
- TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
- /* RFC 9000 10.2. Immediate Close:
- * The closing and draining connection states exist to ensure
- * that connections close cleanly and that delayed or reordered
- * packets are properly discarded. These states SHOULD persist
- * for at least three times the current PTO interval...
- *
- * Rearm the idle timeout only one time when entering draining
- * state.
- */
- qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
- qc_detach_th_ctx_list(qc, 1);
- qc_idle_timer_do_rearm(qc, 0);
- qc_notify_err(qc);
- }
- break;
- case QUIC_FT_HANDSHAKE_DONE:
- if (qc_is_listener(qc)) {
- TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
- QUIC_EV_CONN_PRSHPKT, qc);
- goto leave;
- }
-
- qc->state = QUIC_HS_ST_CONFIRMED;
- break;
- default:
- TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
- goto leave;
- }
- }
-
- /* Flag this packet number space as having received a packet. */
- qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
-
- if (fast_retrans && qc->iel && qc->hel) {
- struct quic_enc_level *iqel = qc->iel;
- struct quic_enc_level *hqel = qc->hel;
-
- TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
- qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
- qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
- }
-
- /* The server must switch from INITIAL to HANDSHAKE handshake state when it
- * has successfully parse a Handshake packet. The Initial encryption must also
- * be discarded.
- */
- if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) {
- if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
- if (qc->ipktns && !quic_tls_pktns_is_dcd(qc, qc->ipktns)) {
- /* Discard the handshake packet number space. */
- TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
- quic_pktns_discard(qc->ipktns, qc);
- qc_set_timer(qc);
- qc_el_rx_pkts_del(qc->iel);
- qc_release_pktns_frms(qc, qc->ipktns);
- }
- if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
- qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
- }
- }
-
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
- return ret;
-}
-
-
-/* Allocate Tx buffer from <qc> quic-conn if needed.
- *
- * Returns allocated buffer or NULL on error.
- */
-static struct buffer *qc_txb_alloc(struct quic_conn *qc)
-{
- struct buffer *buf = &qc->tx.buf;
- if (!b_alloc(buf))
- return NULL;
-
- return buf;
-}
-
-/* Free Tx buffer from <qc> if it is empty. */
-static void qc_txb_release(struct quic_conn *qc)
-{
- struct buffer *buf = &qc->tx.buf;
-
- /* For the moment sending function is responsible to purge the buffer
- * entirely. It may change in the future but this requires to be able
- * to reuse old data.
- * For the momemt we do not care to leave data in the buffer for
- * a connection which is supposed to be killed asap.
- */
- BUG_ON_HOT(buf && b_data(buf));
-
- if (!b_data(buf)) {
- b_free(buf);
- offer_buffers(NULL, 1);
- }
-}
-
-/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
- * must contains the address of the first packet stored in the payload.
- *
- * Caller is responsible that there is enough space in the buffer.
- */
-static void qc_txb_store(struct buffer *buf, uint16_t length,
- struct quic_tx_packet *first_pkt)
-{
- const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
- BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
-
- write_u16(b_tail(buf), length);
- write_ptr(b_tail(buf) + sizeof(length), first_pkt);
- b_add(buf, hdlen + length);
-}
-
-/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
- * with <frms> as ack-eliciting frame list to send, 0 if not.
- * <cc> must equal to 1 if an immediate close was asked, 0 if not.
- * <probe> must equalt to 1 if a probing packet is required, 0 if not.
- * Also set <*must_ack> to inform the caller if an acknowledgement should be sent.
- */
-static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
- struct quic_enc_level *qel, int cc, int probe,
- int *must_ack)
-{
- int force_ack = qel == qc->iel || qel == qc->hel;
- int nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
-
- /* An acknowledgement must be sent if this has been forced by the caller,
- * typically during the handshake when the packets must be acknowledged as
- * soon as possible. This is also the case when the ack delay timer has been
- * triggered, or at least every QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK packets.
- */
- *must_ack = (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED) ||
- ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
- (force_ack || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK));
-
- TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_PHPKTS, qc, 0, 0, 0,
- "has_sec=%d cc=%d probe=%d must_ack=%d frms=%d prep_in_fligh=%llu cwnd=%llu",
- quic_tls_has_tx_sec(qel), cc, probe, *must_ack, LIST_ISEMPTY(frms),
- (ullong)qc->path->prep_in_flight, (ullong)qc->path->cwnd);
-
- /* Do not build any more packet if the TX secrets are not available or
- * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
- * and if there is no more packets to send upon PTO expiration
- * and if there is no more ack-eliciting frames to send or in flight
- * congestion control limit is reached for prepared data
- */
- if (!quic_tls_has_tx_sec(qel) ||
- (!cc && !probe && !*must_ack &&
- (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
- return 0;
- }
-
- return 1;
-}
-
-/* Prepare as much as possible QUIC packets for sending from prebuilt frames
- * <frms>. Each packet is stored in a distinct datagram written to <buf>.
- *
- * Each datagram is prepended by a two fields header : the datagram length and
- * the address of the packet contained in the datagram.
- *
- * Returns the number of bytes prepared in packets if succeeded (may be 0), or
- * -1 if something wrong happened.
- */
-static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
- struct list *frms)
-{
- int ret = -1;
- struct quic_enc_level *qel;
- unsigned char *end, *pos;
- struct quic_tx_packet *pkt;
- size_t total;
- /* Each datagram is prepended with its length followed by the address
- * of the first packet in the datagram.
- */
- const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
-
- TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
-
- qel = qc->ael;
- total = 0;
- pos = (unsigned char *)b_tail(buf);
- while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
- int err, probe, cc, must_ack;
-
- TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel, frms);
- probe = 0;
- cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
- /* We do not probe if an immediate close was asked */
- if (!cc)
- probe = qel->pktns->tx.pto_probe;
-
- if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack))
- break;
-
- /* Leave room for the datagram header */
- pos += dg_headlen;
- if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
- end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
- }
- else {
- end = pos + qc->path->mtu;
- }
-
- pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
- QUIC_PACKET_TYPE_SHORT, must_ack, 0, probe, cc, &err);
- switch (err) {
- case -2:
- // trace already emitted by function above
- goto leave;
- case -1:
- /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
- * MTU, we are here because of the congestion control window. There is
- * no need to try to reuse this buffer.
- */
- TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
- goto out;
- default:
- break;
- }
-
- /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
- BUG_ON(!pkt);
-
- if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
- pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
-
- total += pkt->len;
-
- /* Write datagram header. */
- qc_txb_store(buf, pkt->len, pkt);
- }
-
- out:
- ret = total;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
- return ret;
-}
-
-/* Free all frames in <l> list. In addition also remove all these frames
- * from the original ones if they are the results of duplications.
- */
-static inline void qc_free_frm_list(struct quic_conn *qc, struct list *l)
-{
- struct quic_frame *frm, *frmbak;
-
- list_for_each_entry_safe(frm, frmbak, l, list) {
- LIST_DEL_INIT(&frm->ref);
- qc_frm_free(qc, &frm);
- }
-}
-
-/* Free <pkt> TX packet and all the packets coalesced to it. */
-static inline void qc_free_tx_coalesced_pkts(struct quic_conn *qc,
- struct quic_tx_packet *p)
-{
- struct quic_tx_packet *pkt, *nxt_pkt;
-
- for (pkt = p; pkt; pkt = nxt_pkt) {
- qc_free_frm_list(qc, &pkt->frms);
- nxt_pkt = pkt->next;
- pool_free(pool_head_quic_tx_packet, pkt);
- }
-}
-
-/* Purge <buf> TX buffer from its prepare packets. */
-static void qc_purge_tx_buf(struct quic_conn *qc, struct buffer *buf)
-{
- while (b_contig_data(buf, 0)) {
- uint16_t dglen;
- struct quic_tx_packet *pkt;
- size_t headlen = sizeof dglen + sizeof pkt;
-
- dglen = read_u16(b_head(buf));
- pkt = read_ptr(b_head(buf) + sizeof dglen);
- qc_free_tx_coalesced_pkts(qc, pkt);
- b_del(buf, dglen + headlen);
- }
-
- BUG_ON(b_data(buf));
-}
-
-/* Send datagrams stored in <buf>.
- *
- * This function returns 1 for success. On error, there is several behavior
- * depending on underlying sendto() error :
- * - for an unrecoverable error, 0 is returned and connection is killed.
- * - a transient error is handled differently if connection has its owned
- * socket. If this is the case, 0 is returned and socket is subscribed on the
- * poller. The other case is assimilated to a success case with 1 returned.
- * Remaining data are purged from the buffer and will eventually be detected
- * as lost which gives the opportunity to retry sending.
- */
-int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
-{
- int ret = 0;
- struct quic_conn *qc;
- char skip_sendto = 0;
-
- qc = ctx->qc;
- TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
- while (b_contig_data(buf, 0)) {
- unsigned char *pos;
- struct buffer tmpbuf = { };
- struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
- uint16_t dglen;
- size_t headlen = sizeof dglen + sizeof first_pkt;
- unsigned int time_sent;
-
- pos = (unsigned char *)b_head(buf);
- dglen = read_u16(pos);
- BUG_ON_HOT(!dglen); /* this should not happen */
-
- pos += sizeof dglen;
- first_pkt = read_ptr(pos);
- pos += sizeof first_pkt;
- tmpbuf.area = (char *)pos;
- tmpbuf.size = tmpbuf.data = dglen;
-
- TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc);
- /* If sendto is on error just skip the call to it for the rest
- * of the loop but continue to purge the buffer. Data will be
- * transmitted when QUIC packets are detected as lost on our
- * side.
- *
- * TODO use fd-monitoring to detect when send operation can be
- * retry. This should improve the bandwidth without relying on
- * retransmission timer. However, it requires a major rework on
- * quic-conn fd management.
- */
- if (!skip_sendto) {
- int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
- if (ret < 0) {
- TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc, first_pkt);
- qc_kill_conn(qc);
- qc_free_tx_coalesced_pkts(qc, first_pkt);
- b_del(buf, dglen + headlen);
- qc_purge_tx_buf(qc, buf);
- goto leave;
- }
- else if (!ret) {
- /* Connection owned socket : poller will wake us up when transient error is cleared. */
- if (qc_test_fd(qc)) {
- TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc);
- goto leave;
- }
-
- /* No connection owned-socket : rely on retransmission to retry sending. */
- skip_sendto = 1;
- TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
- }
- }
-
- b_del(buf, dglen + headlen);
- qc->tx.bytes += tmpbuf.data;
- time_sent = now_ms;
-
- for (pkt = first_pkt; pkt; pkt = next_pkt) {
- /* RFC 9000 14.1 Initial datagram size
- * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
- * Initial packets to at least the smallest allowed maximum datagram size of
- * 1200 bytes.
- */
- qc->cntrs.sent_pkt++;
- BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL &&
- (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
- dglen < QUIC_INITIAL_PACKET_MINLEN);
-
- pkt->time_sent = time_sent;
- if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
- pkt->pktns->tx.time_of_last_eliciting = time_sent;
- qc->path->ifae_pkts++;
- if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
- qc_idle_timer_rearm(qc, 0, 0);
- }
- if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
- (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
- qc->flags |= QUIC_FL_CONN_CLOSING;
- qc_detach_th_ctx_list(qc, 1);
-
- /* RFC 9000 10.2. Immediate Close:
- * The closing and draining connection states exist to ensure
- * that connections close cleanly and that delayed or reordered
- * packets are properly discarded. These states SHOULD persist
- * for at least three times the current PTO interval...
- *
- * Rearm the idle timeout only one time when entering closing
- * state.
- */
- qc_idle_timer_do_rearm(qc, 0);
- if (qc->timer_task) {
- task_destroy(qc->timer_task);
- qc->timer_task = NULL;
- }
- }
- qc->path->in_flight += pkt->in_flight_len;
- pkt->pktns->tx.in_flight += pkt->in_flight_len;
- if (pkt->in_flight_len)
- qc_set_timer(qc);
- TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
- next_pkt = pkt->next;
- quic_tx_packet_refinc(pkt);
- eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
- }
- }
-
- ret = 1;
-leave:
- TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
-
- return ret;
-}
-
-/* Copy at <pos> position a stateless reset token depending on the
- * <salt> salt input. This is the cluster secret which will be derived
- * as HKDF input secret to generate this token.
- * Return 1 if succeeded, 0 if not.
- */
-static int quic_stateless_reset_token_cpy(unsigned char *pos, size_t len,
- const unsigned char *salt, size_t saltlen)
-{
- /* Input secret */
- const unsigned char *key = (const unsigned char *)global.cluster_secret;
- size_t keylen = strlen(global.cluster_secret);
- /* Info */
- const unsigned char label[] = "stateless token";
- size_t labellen = sizeof label - 1;
- int ret;
-
- ret = quic_hkdf_extract_and_expand(EVP_sha256(), pos, len,
- key, keylen, salt, saltlen, label, labellen);
- return ret;
-}
-
-/* Initialize the stateless reset token attached to <conn_id> connection ID.
- * Returns 1 if succeeded, 0 if not.
- */
-static int quic_stateless_reset_token_init(struct quic_connection_id *conn_id)
-{
- int ret;
-
- if (global.cluster_secret) {
- /* Output secret */
- unsigned char *token = conn_id->stateless_reset_token;
- size_t tokenlen = sizeof conn_id->stateless_reset_token;
- /* Salt */
- const unsigned char *cid = conn_id->cid.data;
- size_t cidlen = conn_id->cid.len;
-
- ret = quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
- }
- else {
- /* TODO: RAND_bytes() should be replaced */
- ret = RAND_bytes(conn_id->stateless_reset_token,
- sizeof conn_id->stateless_reset_token) == 1;
- }
-
- return ret;
-}
-
-/* Generate a CID directly derived from <orig> CID and <addr> address.
- *
- * Returns the derived CID.
- */
-struct quic_cid quic_derive_cid(const struct quic_cid *orig,
- const struct sockaddr_storage *addr)
-{
- struct quic_cid cid;
- const struct sockaddr_in *in;
- const struct sockaddr_in6 *in6;
- char *pos = trash.area;
- size_t idx = 0;
- uint64_t hash;
- int i;
-
- /* Prepare buffer for hash using original CID first. */
- memcpy(pos, orig->data, orig->len);
- idx += orig->len;
-
- /* Concatenate client address. */
- switch (addr->ss_family) {
- case AF_INET:
- in = (struct sockaddr_in *)addr;
-
- memcpy(&pos[idx], &in->sin_addr, sizeof(in->sin_addr));
- idx += sizeof(in->sin_addr);
- memcpy(&pos[idx], &in->sin_port, sizeof(in->sin_port));
- idx += sizeof(in->sin_port);
- break;
-
- case AF_INET6:
- in6 = (struct sockaddr_in6 *)addr;
-
- memcpy(&pos[idx], &in6->sin6_addr, sizeof(in6->sin6_addr));
- idx += sizeof(in6->sin6_addr);
- memcpy(&pos[idx], &in6->sin6_port, sizeof(in6->sin6_port));
- idx += sizeof(in6->sin6_port);
- break;
-
- default:
- /* TODO to implement */
- ABORT_NOW();
- }
-
- /* Avoid similar values between multiple haproxy process. */
- memcpy(&pos[idx], boot_seed, sizeof(boot_seed));
- idx += sizeof(boot_seed);
-
- /* Hash the final buffer content. */
- hash = XXH64(pos, idx, 0);
-
- for (i = 0; i < sizeof(hash); ++i)
- cid.data[i] = hash >> ((sizeof(hash) * 7) - (8 * i));
- cid.len = sizeof(hash);
-
- return cid;
-}
-
-/* Retrieve the thread ID associated to QUIC connection ID <cid> of length
- * <cid_len>. CID may be not found on the CID tree because it is an ODCID. In
- * this case, it will derived using client address <cli_addr> as hash
- * parameter. However, this is done only if <pos> points to an INITIAL or 0RTT
- * packet of length <len>.
- *
- * Returns the thread ID or a negative error code.
- */
-int quic_get_cid_tid(const unsigned char *cid, size_t cid_len,
- const struct sockaddr_storage *cli_addr,
- unsigned char *pos, size_t len)
-{
- struct quic_cid_tree *tree;
- struct quic_connection_id *conn_id;
- struct ebmb_node *node;
-
- tree = &quic_cid_trees[_quic_cid_tree_idx(cid)];
- HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
- node = ebmb_lookup(&tree->root, cid, cid_len);
- HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
-
- if (!node) {
- struct quic_cid orig, derive_cid;
- struct quic_rx_packet pkt;
-
- if (!qc_parse_hd_form(&pkt, &pos, pos + len))
- goto not_found;
-
- if (pkt.type != QUIC_PACKET_TYPE_INITIAL &&
- pkt.type != QUIC_PACKET_TYPE_0RTT) {
- goto not_found;
- }
-
- memcpy(orig.data, cid, cid_len);
- orig.len = cid_len;
- derive_cid = quic_derive_cid(&orig, cli_addr);
-
- tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
- HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
- node = ebmb_lookup(&tree->root, cid, cid_len);
- HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
- }
-
- if (!node)
- goto not_found;
-
- conn_id = ebmb_entry(node, struct quic_connection_id, node);
- return HA_ATOMIC_LOAD(&conn_id->tid);
-
- not_found:
- return -1;
-}
-
-/* Allocate a new CID and attach it to <root> ebtree.
- *
- * If <orig> and <addr> params are non null, the new CID value is directly
- * derived from them. Else a random value is generated. The CID is then marked
- * with the current thread ID.
- *
- * Returns the new CID if succeeded, NULL if not.
- */
-static struct quic_connection_id *new_quic_cid(struct eb_root *root,
- struct quic_conn *qc,
- const struct quic_cid *orig,
- const struct sockaddr_storage *addr)
-{
- struct quic_connection_id *conn_id;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
-
- /* Caller must set either none or both values. */
- BUG_ON(!!orig != !!addr);
-
- conn_id = pool_alloc(pool_head_quic_connection_id);
- if (!conn_id) {
- TRACE_ERROR("cid allocation failed", QUIC_EV_CONN_TXPKT, qc);
- goto err;
- }
-
- conn_id->cid.len = QUIC_HAP_CID_LEN;
-
- if (!orig) {
- /* TODO: RAND_bytes() should be replaced */
- if (RAND_bytes(conn_id->cid.data, conn_id->cid.len) != 1) {
- TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT, qc);
- goto err;
- }
- }
- else {
- /* Derive the new CID value from original CID. */
- conn_id->cid = quic_derive_cid(orig, addr);
- }
-
- if (quic_stateless_reset_token_init(conn_id) != 1) {
- TRACE_ERROR("quic_stateless_reset_token_init() failed", QUIC_EV_CONN_TXPKT, qc);
- goto err;
- }
-
- conn_id->qc = qc;
- HA_ATOMIC_STORE(&conn_id->tid, tid);
-
- conn_id->seq_num.key = qc ? qc->next_cid_seq_num++ : 0;
- conn_id->retire_prior_to = 0;
- /* insert the allocated CID in the quic_conn tree */
- if (root)
- eb64_insert(root, &conn_id->seq_num);
-
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return conn_id;
-
- err:
- pool_free(pool_head_quic_connection_id, conn_id);
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return NULL;
-}
-
-/* Build all the frames which must be sent just after the handshake have succeeded.
- * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
- * a HANDSHAKE_DONE frame.
- * Return 1 if succeeded, 0 if not.
- */
-static int quic_build_post_handshake_frames(struct quic_conn *qc)
-{
- int ret = 0, max;
- struct quic_enc_level *qel;
- struct quic_frame *frm, *frmbak;
- struct list frm_list = LIST_HEAD_INIT(frm_list);
- struct eb64_node *node;
-
- TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
-
- qel = qc->ael;
- /* Only servers must send a HANDSHAKE_DONE frame. */
- if (qc_is_listener(qc)) {
- frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
- if (!frm) {
- TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
- goto leave;
- }
-
- LIST_APPEND(&frm_list, &frm->list);
- }
-
- /* Initialize <max> connection IDs minus one: there is
- * already one connection ID used for the current connection. Also limit
- * the number of connection IDs sent to the peer to 4 (3 from this function
- * plus 1 for the current connection.
- * Note that active_connection_id_limit >= 2: this has been already checked
- * when receiving this parameter.
- */
- max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3);
- while (max--) {
- struct quic_connection_id *conn_id;
-
- frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
- if (!frm) {
- TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
- goto err;
- }
-
- conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
- if (!conn_id) {
- qc_frm_free(qc, &frm);
- TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
- goto err;
- }
-
- /* TODO To prevent CID tree locking, all CIDs created here
- * could be allocated at the same time as the first one.
- */
- quic_cid_insert(conn_id);
-
- quic_connection_id_to_frm_cpy(frm, conn_id);
- LIST_APPEND(&frm_list, &frm->list);
- }
-
- LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
- qc->flags &= ~QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
-
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
- return ret;
-
- err:
- /* free the frames */
- list_for_each_entry_safe(frm, frmbak, &frm_list, list)
- qc_frm_free(qc, &frm);
-
- /* The first CID sequence number value used to allocated CIDs by this function is 1,
- * 0 being the sequence number of the CID for this connection.
- */
- node = eb64_lookup_ge(&qc->cids, 1);
- while (node) {
- struct quic_connection_id *conn_id;
-
- conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
- if (conn_id->seq_num.key >= max)
- break;
-
- node = eb64_next(node);
- quic_cid_delete(conn_id);
-
- eb64_delete(&conn_id->seq_num);
- pool_free(pool_head_quic_connection_id, conn_id);
- }
- goto leave;
-}
-
-/* Detect the value of the spin bit to be used. */
-static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
- struct quic_enc_level *qel)
-{
- uint64_t largest_pn = qel->pktns->rx.largest_pn;
-
- if (qel != qc->ael || largest_pn == -1 ||
- pkt->pn <= largest_pn)
- return;
-
- if (qc_is_listener(qc)) {
- if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
- qc->flags |= QUIC_FL_CONN_SPIN_BIT;
- else
- qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
- }
- else {
- if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
- qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
- else
- qc->flags |= QUIC_FL_CONN_SPIN_BIT;
- }
-}
-
-/* Remove the header protection of packets at <el> encryption level.
- * Always succeeds.
- */
-static inline void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
-{
- struct quic_rx_packet *pqpkt, *pkttmp;
-
- TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
- /* A server must not process incoming 1-RTT packets before the handshake is complete. */
- if (el == qc->ael && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
- TRACE_PROTO("RX hp not removed (handshake not completed)",
- QUIC_EV_CONN_ELRMHP, qc);
- goto out;
- }
-
- list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
- struct quic_tls_ctx *tls_ctx;
-
- tls_ctx = qc_select_tls_ctx(qc, el, pqpkt);
- if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
- pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
- TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
- }
- else {
- qc_handle_spin_bit(qc, pqpkt, el);
- /* The AAD includes the packet number field */
- pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
- /* Store the packet into the tree of packets to decrypt. */
- pqpkt->pn_node.key = pqpkt->pn;
- eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
- quic_rx_packet_refinc(pqpkt);
- TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
- }
- LIST_DELETE(&pqpkt->list);
- quic_rx_packet_refdec(pqpkt);
- }
-
- out:
- TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
-}
-
-/* Process all the CRYPTO frame at <el> encryption level. This is the
- * responsibility of the called to ensure there exists a CRYPTO data
- * stream for this level.
- * Return 1 if succeeded, 0 if not.
- */
-static inline int qc_treat_rx_crypto_frms(struct quic_conn *qc,
- struct quic_enc_level *el,
- struct ssl_sock_ctx *ctx)
-{
- int ret = 0;
- struct ncbuf *ncbuf;
- struct quic_cstream *cstream = el->cstream;
- ncb_sz_t data;
-
- TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
-
- BUG_ON(!cstream);
- ncbuf = &cstream->rx.ncbuf;
- if (ncb_is_null(ncbuf))
- goto done;
-
- /* TODO not working if buffer is wrapping */
- while ((data = ncb_data(ncbuf, 0))) {
- const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
-
- if (!qc_ssl_provide_quic_data(&el->cstream->rx.ncbuf, el->level,
- ctx, cdata, data))
- goto leave;
-
- cstream->rx.offset += data;
- TRACE_DEVEL("buffered crypto data were provided to TLS stack",
- QUIC_EV_CONN_PHPKTS, qc, el);
- }
-
- done:
- ret = 1;
- leave:
- if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
- TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
- quic_free_ncbuf(ncbuf);
- }
- TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
- return ret;
-}
-
-/* Check if it's possible to remove header protection for packets related to
- * encryption level <qel>. If <qel> is NULL, assume it's false.
- *
- * Return true if the operation is possible else false.
- */
-static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
-{
- int ret = 0;
-
- TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
-
- if (!qel)
- goto cant_rm_hp;
-
- if (!quic_tls_has_rx_sec(qel)) {
- TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
- goto cant_rm_hp;
- }
-
- if (qel == qc->ael && qc->state < QUIC_HS_ST_COMPLETE) {
- TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
- goto cant_rm_hp;
- }
-
- /* check if the connection layer is ready before using app level */
- if ((qel == qc->ael || qel == qc->eel) &&
- qc->mux_state == QC_MUX_NULL) {
- TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
- goto cant_rm_hp;
- }
-
- ret = 1;
- cant_rm_hp:
- TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
- return ret;
-}
-
-/* Process all the packets for all the encryption levels listed in <qc> QUIC connection.
- * Return 1 if succeeded, 0 if not.
- */
-int qc_treat_rx_pkts(struct quic_conn *qc)
-{
- int ret = 0;
- struct eb64_node *node;
- int64_t largest_pn = -1;
- unsigned int largest_pn_time_received = 0;
- struct quic_enc_level *qel, *qelbak;
-
- TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
-
- list_for_each_entry_safe(qel, qelbak, &qc->qel_list, list) {
- /* Treat packets waiting for header packet protection decryption */
- if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
- qc_rm_hp_pkts(qc, qel);
-
- node = eb64_first(&qel->rx.pkts);
- while (node) {
- struct quic_rx_packet *pkt;
-
- pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
- TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
- qc, pkt, NULL, qc->xprt_ctx->ssl);
- if (!qc_pkt_decrypt(qc, qel, pkt)) {
- /* Drop the packet */
- TRACE_ERROR("packet decryption failed -> dropped",
- QUIC_EV_CONN_RXPKT, qc, pkt);
- }
- else {
- if (!qc_parse_pkt_frms(qc, pkt, qel)) {
- /* Drop the packet */
- TRACE_ERROR("packet parsing failed -> dropped",
- QUIC_EV_CONN_RXPKT, qc, pkt);
- qc->cntrs.dropped_parsing++;
- }
- else {
- struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
-
- if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
- int arm_ack_timer =
- qc->state >= QUIC_HS_ST_COMPLETE &&
- qel->pktns == qc->apktns;
-
- qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
- qel->pktns->rx.nb_aepkts_since_last_ack++;
- qc_idle_timer_rearm(qc, 1, arm_ack_timer);
- }
- if (pkt->pn > largest_pn) {
- largest_pn = pkt->pn;
- largest_pn_time_received = pkt->time_received;
- }
- /* Update the list of ranges to acknowledge. */
- if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
- TRACE_ERROR("Could not update ack range list",
- QUIC_EV_CONN_RXPKT, qc);
- }
- }
- node = eb64_next(node);
- eb64_delete(&pkt->pn_node);
- quic_rx_packet_refdec(pkt);
- }
-
- if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
- /* Update the largest packet number. */
- qel->pktns->rx.largest_pn = largest_pn;
- /* Update the largest acknowledged packet timestamps */
- qel->pktns->rx.largest_time_received = largest_pn_time_received;
- qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
- }
-
- if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
- // trace already emitted by function above
- goto leave;
- }
-
- /* Release the Initial encryption level and packet number space. */
- if ((qc->flags & QUIC_FL_CONN_IPKTNS_DCD) && qel == qc->iel) {
- qc_enc_level_free(qc, &qc->iel);
- quic_pktns_release(qc, &qc->ipktns);
- }
-
- largest_pn = -1;
- }
-
- out:
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
- return ret;
-}
-
-/* Flush txbuf for <qc> connection. This must be called prior to a packet
- * preparation when txbuf contains older data. A send will be conducted for
- * these data.
- *
- * Returns 1 on success : buffer is empty and can be use for packet
- * preparation. On error 0 is returned.
- */
-static int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
-{
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
-
- /* This operation can only be conducted if txbuf is not empty. This
- * case only happens for connection with their owned socket due to an
- * older transient sendto() error.
- */
- BUG_ON(!qc_test_fd(qc));
-
- if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
- if (qc->flags & QUIC_FL_CONN_TO_KILL)
- qc_txb_release(qc);
- TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
- return 0;
- }
-
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return 1;
-}
-
-/* Try to send application frames from list <frms> on connection <qc>.
- *
- * Use qc_send_app_probing wrapper when probing with old data.
- *
- * Returns 1 on success. Some data might not have been sent due to congestion,
- * in this case they are left in <frms> input list. The caller may subscribe on
- * quic-conn to retry later.
- *
- * Returns 0 on critical error.
- * TODO review and classify more distinctly transient from definitive errors to
- * allow callers to properly handle it.
- */
-static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
-{
- int status = 0, ret;
- struct buffer *buf;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
-
- buf = qc_txb_alloc(qc);
- if (!buf) {
- TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
- goto err;
- }
-
- if (b_data(buf) && !qc_purge_txbuf(qc, buf))
- goto err;
-
- /* Prepare and send packets until we could not further prepare packets. */
- do {
- /* Currently buf cannot be non-empty at this stage. Even if a
- * previous sendto() has failed it is emptied to simulate
- * packet emission and rely on QUIC lost detection to try to
- * emit it.
- */
- BUG_ON_HOT(b_data(buf));
- b_reset(buf);
-
- ret = qc_prep_app_pkts(qc, buf, frms);
-
- if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
- if (qc->flags & QUIC_FL_CONN_TO_KILL)
- qc_txb_release(qc);
- goto err;
- }
- } while (ret > 0);
-
- qc_txb_release(qc);
- if (ret < 0)
- goto err;
-
- status = 1;
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return status;
-
- err:
- TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
- return 0;
-}
-
-/* Try to send application frames from list <frms> on connection <qc>. Use this
- * function when probing is required.
- *
- * Returns the result from qc_send_app_pkts function.
- */
-static forceinline int qc_send_app_probing(struct quic_conn *qc,
- struct list *frms)
-{
- int ret;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
-
- TRACE_PROTO("preparing old data (probing)", QUIC_EV_CONN_FRMLIST, qc, frms);
- qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
- ret = qc_send_app_pkts(qc, frms);
- qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
-
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return ret;
-}
-
-/* Try to send application frames from list <frms> on connection <qc>. This
- * function is provided for MUX upper layer usage only.
- *
- * Returns the result from qc_send_app_pkts function.
- */
-int qc_send_mux(struct quic_conn *qc, struct list *frms)
-{
- int ret;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
- BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
-
- if (qc->conn->flags & CO_FL_SOCK_WR_SH) {
- qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH;
- TRACE_DEVEL("connection on error", QUIC_EV_CONN_TXPKT, qc);
- return 0;
- }
-
- /* Try to send post handshake frames first unless on 0-RTT. */
- if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) &&
- qc->state >= QUIC_HS_ST_COMPLETE) {
- quic_build_post_handshake_frames(qc);
- qc_send_app_pkts(qc, &qc->ael->pktns->tx.frms);
- }
-
- TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
- qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
- ret = qc_send_app_pkts(qc, frms);
- qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
-
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return ret;
-}
-
-/* Return the encryption level following the one which contains <el> list head
- * depending on <retrans> TX mode (retranmission or not).
- */
-static inline struct quic_enc_level *qc_list_next_qel(struct list *el, int retrans)
-{
- return !retrans ? LIST_NEXT(el, struct quic_enc_level *, list) :
- LIST_NEXT(el, struct quic_enc_level *, retrans);
-}
-
-/* Return the encryption level following <qel> depending on <retrans> TX mode
- * (retranmission or not).
- */
-static inline struct quic_enc_level *qc_next_qel(struct quic_enc_level *qel, int retrans)
-{
- struct list *el = !retrans ? &qel->list : &qel->retrans;
-
- return qc_list_next_qel(el, retrans);
-}
-
-/* Return 1 if <qel> is at the head of its list, 0 if not. */
-static inline int qc_qel_is_head(struct quic_enc_level *qel, struct list *l,
- int retrans)
-{
- return !retrans ? &qel->list == l : &qel->retrans == l;
-}
-
-/* Prepare as much as possible QUIC datagrams/packets for sending from <qels>
- * list of encryption levels. Several packets can be coalesced into a single
- * datagram. The result is written into <buf>. Note that if <qels> is NULL,
- * the encryption levels which will be used are those currently allocated
- * and attached to the connection.
- *
- * Each datagram is prepended by a two fields header : the datagram length and
- * the address of first packet in the datagram.
- *
- * Returns the number of bytes prepared in datragrams/packets if succeeded
- * (may be 0), or -1 if something wrong happened.
- */
-static int qc_prep_hpkts(struct quic_conn *qc, struct buffer *buf, struct list *qels)
-{
- int ret, retrans, padding;
- struct quic_tx_packet *first_pkt, *prv_pkt;
- unsigned char *end, *pos;
- const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
- uint16_t dglen;
- size_t total;
- struct list *qel_list;
- struct quic_enc_level *qel;
-
- TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
- /* Currently qc_prep_pkts() does not handle buffer wrapping so the
- * caller must ensure that buf is reset.
- */
- BUG_ON_HOT(buf->head || buf->data);
-
- ret = -1;
- retrans = !!qels;
- padding = 0;
- first_pkt = prv_pkt = NULL;
- end = pos = (unsigned char *)b_head(buf);
- dglen = 0;
- total = 0;
-
- qel_list = qels ? qels : &qc->qel_list;
- qel = qc_list_next_qel(qel_list, retrans);
- while (!qc_qel_is_head(qel, qel_list, retrans)) {
- struct quic_tls_ctx *tls_ctx;
- const struct quic_version *ver;
- struct list *frms, *next_frms;
- struct quic_enc_level *next_qel;
-
- if (qel == qc->eel) {
- /* Next encryption level */
- qel = qc_next_qel(qel, retrans);
- continue;
- }
-
- if (qc->negotiated_version) {
- ver = qc->negotiated_version;
- if (qel == qc->iel)
- tls_ctx = qc->nictx;
- else
- tls_ctx = &qel->tls_ctx;
- }
- else {
- ver = qc->original_version;
- tls_ctx = &qel->tls_ctx;
- }
-
- if (!qels)
- frms = &qel->pktns->tx.frms;
- else
- frms = qel->retrans_frms;
-
- next_qel = qc_next_qel(qel, retrans);
- next_frms = qc_qel_is_head(next_qel, qel_list, retrans) ? NULL :
- !qels ? &next_qel->pktns->tx.frms : next_qel->retrans_frms;
-
- /* Build as much as datagrams at <qel> encryption level. */
- while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
- int err, probe, cc, must_ack;
- enum quic_pkt_type pkt_type;
- struct quic_tx_packet *cur_pkt;
-
- TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
- probe = 0;
- cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
- /* We do not probe if an immediate close was asked */
- if (!cc)
- probe = qel->pktns->tx.pto_probe;
-
- if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) {
- if (prv_pkt && qc_qel_is_head(next_qel, qel_list, retrans))
- qc_txb_store(buf, dglen, first_pkt);
-
- TRACE_DEVEL("next encryption level", QUIC_EV_CONN_PHPKTS, qc);
- break;
- }
-
- if (!prv_pkt) {
- /* Leave room for the datagram header */
- pos += dg_headlen;
- if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
- end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
- }
- else {
- end = pos + qc->path->mtu;
- }
- }
-
- /* RFC 9000 14.1 Initial datagram size
- * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
- * Initial packets to at least the smallest allowed maximum datagram size of
- * 1200 bytes.
- *
- * Ensure that no ack-eliciting packets are sent into too small datagrams
- */
- if (qel == qc->iel && !LIST_ISEMPTY(frms)) {
- if (end - pos < QUIC_INITIAL_PACKET_MINLEN) {
- TRACE_PROTO("No more enough room to build an Initial packet",
- QUIC_EV_CONN_PHPKTS, qc);
- break;
- }
-
- /* Pad this Initial packet if there is no ack-eliciting frames to send from
- * the next packet number space.
- */
- if (!next_frms || LIST_ISEMPTY(next_frms))
- padding = 1;
- }
-
- pkt_type = quic_enc_level_pkt_type(qc, qel);
- cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
- qc, ver, dglen, pkt_type,
- must_ack, padding, probe, cc, &err);
- switch (err) {
- case -2:
- // trace already emitted by function above
- goto leave;
- case -1:
- /* If there was already a correct packet present, set the
- * current datagram as prepared into <cbuf>.
- */
- if (prv_pkt)
- qc_txb_store(buf, dglen, first_pkt);
- TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
- goto out;
- default:
- break;
- }
-
- /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
- BUG_ON(!cur_pkt);
-
- total += cur_pkt->len;
- dglen += cur_pkt->len;
-
- if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
- cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
-
- /* keep trace of the first packet in the datagram */
- if (!first_pkt)
- first_pkt = cur_pkt;
-
- /* Attach the current one to the previous one and vice versa */
- if (prv_pkt) {
- prv_pkt->next = cur_pkt;
- cur_pkt->prev = prv_pkt;
- cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
- }
-
- /* If there is no more packet to build for this encryption level,
- * select the next one <next_qel>, if any, to coalesce a packet in
- * the same datagram, except if <qel> is the Application data
- * encryption level which cannot be selected to do that.
- */
- if (LIST_ISEMPTY(frms) && qel != qc->ael &&
- !qc_qel_is_head(next_qel, qel_list, retrans)) {
- if (qel == qc->iel &&
- (!qc_is_listener(qc) ||
- cur_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING))
- padding = 1;
-
- prv_pkt = cur_pkt;
- break;
- }
- else {
- qc_txb_store(buf, dglen, first_pkt);
- first_pkt = NULL;
- dglen = 0;
- padding = 0;
- prv_pkt = NULL;
- }
- }
-
- /* Next encryption level */
- qel = next_qel;
- }
-
- out:
- ret = total;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
- return ret;
-}
-
-/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
- * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
- * QUIC connection. <old_data> is used as boolean to send data already sent but
- * not already acknowledged (in flight).
- * Returns 1 if succeeded, 0 if not.
- */
-int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
- struct quic_enc_level *qel1, struct quic_enc_level *qel2)
-{
- int ret, status = 0;
- struct buffer *buf = qc_txb_alloc(qc);
- struct list qels = LIST_HEAD_INIT(qels);
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
-
- if (!buf) {
- TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
- goto leave;
- }
-
- if (b_data(buf) && !qc_purge_txbuf(qc, buf))
- goto out;
-
- /* Currently buf cannot be non-empty at this stage. Even if a previous
- * sendto() has failed it is emptied to simulate packet emission and
- * rely on QUIC lost detection to try to emit it.
- */
- BUG_ON_HOT(b_data(buf));
- b_reset(buf);
-
- if (old_data) {
- TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
- qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
- }
-
- if (qel1) {
- BUG_ON(LIST_INLIST(&qel1->retrans));
- LIST_APPEND(&qels, &qel1->retrans);
- }
-
- if (qel2) {
- BUG_ON(LIST_INLIST(&qel2->retrans));
- LIST_APPEND(&qels, &qel2->retrans);
- }
-
- ret = qc_prep_hpkts(qc, buf, &qels);
- if (ret == -1) {
- qc_txb_release(qc);
- goto out;
- }
-
- if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
- if (qc->flags & QUIC_FL_CONN_TO_KILL)
- qc_txb_release(qc);
- goto out;
- }
-
- qc_txb_release(qc);
- status = 1;
-
- out:
- if (qel1) {
- LIST_DEL_INIT(&qel1->retrans);
- qel1->retrans_frms = NULL;
- }
-
- if (qel2) {
- LIST_DEL_INIT(&qel2->retrans);
- qel2->retrans_frms = NULL;
- }
-
- TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
- qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return status;
-}
-
-/* Retransmit up to two datagrams depending on packet number space.
- * Return 0 when failed, 0 if not.
- */
-static int qc_dgrams_retransmit(struct quic_conn *qc)
-{
- int ret = 0;
- struct quic_pktns *ipktns = qc->ipktns;
- struct quic_pktns *hpktns = qc->hpktns;
- struct quic_pktns *apktns = qc->apktns;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
-
- /* Note that if the Initial packet number space is not discarded,
- * this is also the case for the Handshake packet number space.
- */
- if (ipktns && (ipktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED)) {
- int i;
-
- for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
- struct list ifrms = LIST_HEAD_INIT(ifrms);
- struct list hfrms = LIST_HEAD_INIT(hfrms);
- struct list qels = LIST_HEAD_INIT(qels);
-
- qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
- TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
- TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
- if (!LIST_ISEMPTY(&ifrms)) {
- ipktns->tx.pto_probe = 1;
- if (!LIST_ISEMPTY(&hfrms))
- hpktns->tx.pto_probe = 1;
- qc->iel->retrans_frms = &ifrms;
- qc->hel->retrans_frms = &hfrms;
- if (!qc_send_hdshk_pkts(qc, 1, qc->iel, qc->hel))
- goto leave;
- /* Put back unsent frames in their packet number spaces */
- LIST_SPLICE(&ipktns->tx.frms, &ifrms);
- LIST_SPLICE(&hpktns->tx.frms, &hfrms);
- }
- else {
- /* We are in the case where the anti-amplification limit will be
- * reached after having sent this datagram. There is no need to
- * send more than one datagram.
- */
- ipktns->tx.pto_probe = 1;
- qc->iel->retrans_frms = &ifrms;
- if (!qc_send_hdshk_pkts(qc, 0, qc->iel, NULL))
- goto leave;
-
- break;
- }
- }
- TRACE_STATE("no more need to probe Initial packet number space",
- QUIC_EV_CONN_TXPKT, qc);
- ipktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
- hpktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
- }
- else {
- int i;
-
- if (hpktns && (hpktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED)) {
- hpktns->tx.pto_probe = 0;
- for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
- struct list frms1 = LIST_HEAD_INIT(frms1);
-
- qc_prep_fast_retrans(qc, hpktns, &frms1, NULL);
- TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
- if (!LIST_ISEMPTY(&frms1)) {
- hpktns->tx.pto_probe = 1;
- qc->hel->retrans_frms = &frms1;
- if (!qc_send_hdshk_pkts(qc, 1, qc->hel, NULL))
- goto leave;
-
- /* Put back unsent frames into their packet number spaces */
- LIST_SPLICE(&hpktns->tx.frms, &frms1);
- }
- }
- TRACE_STATE("no more need to probe Handshake packet number space",
- QUIC_EV_CONN_TXPKT, qc);
- hpktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
- }
- else if (apktns && (apktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED)) {
- struct list frms2 = LIST_HEAD_INIT(frms2);
- struct list frms1 = LIST_HEAD_INIT(frms1);
-
- apktns->tx.pto_probe = 0;
- qc_prep_fast_retrans(qc, apktns, &frms1, &frms2);
- TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
- TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
- if (!LIST_ISEMPTY(&frms1)) {
- apktns->tx.pto_probe = 1;
- if (!qc_send_app_probing(qc, &frms1)) {
- qc_free_frm_list(qc, &frms2);
- goto leave;
- }
-
- /* Put back unsent frames into their packet number spaces */
- LIST_SPLICE(&apktns->tx.frms, &frms1);
- }
- if (!LIST_ISEMPTY(&frms2)) {
- apktns->tx.pto_probe = 1;
- if (!qc_send_app_probing(qc, &frms2))
- goto leave;
- /* Put back unsent frames into their packet number spaces */
- LIST_SPLICE(&apktns->tx.frms, &frms2);
- }
- TRACE_STATE("no more need to probe 01RTT packet number space",
- QUIC_EV_CONN_TXPKT, qc);
- apktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
- }
- }
-
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return ret;
-}
-
-/* QUIC connection packet handler task (post handshake) */
-struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
-{
- struct quic_conn *qc = context;
- struct quic_enc_level *qel;
-
- TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
-
- qel = qc->ael;
- TRACE_STATE("connection handshake state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
-
- if (qc_test_fd(qc))
- qc_rcv_buf(qc);
-
- /* Prepare post-handshake frames
- * - after connection is instantiated (accept is done)
- * - handshake state is completed (may not be the case here in 0-RTT)
- */
- if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) && qc->conn &&
- qc->state >= QUIC_HS_ST_COMPLETE) {
- quic_build_post_handshake_frames(qc);
- }
-
- /* Retranmissions */
- if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
- TRACE_STATE("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
- qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
- if (!qc_dgrams_retransmit(qc))
- goto out;
- }
-
- if (!qc_treat_rx_pkts(qc)) {
- TRACE_DEVEL("qc_treat_rx_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
- goto out;
- }
-
- if (qc->flags & QUIC_FL_CONN_TO_KILL) {
- TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_IO_CB, qc);
- goto out;
- }
-
- if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
- !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
- TRACE_STATE("draining connection (must not send packets)", QUIC_EV_CONN_IO_CB, qc);
- goto out;
- }
-
- /* XXX TODO: how to limit the list frames to send */
- if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms)) {
- TRACE_DEVEL("qc_send_app_pkts() failed", QUIC_EV_CONN_IO_CB, qc);
- goto out;
- }
-
- out:
- TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
- return t;
-}
-
-/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
-static int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
-{
- return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
- (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
- qel->pktns->tx.pto_probe ||
- !LIST_ISEMPTY(&qel->pktns->tx.frms);
-}
-
-/* QUIC connection packet handler task. */
-struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
-{
- int ret;
- struct quic_conn *qc = context;
- struct buffer *buf = NULL;
- int st;
-
- TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
-
- st = qc->state;
- TRACE_PROTO("connection state", QUIC_EV_CONN_IO_CB, qc, &st);
-
- /* Retranmissions */
- if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
- TRACE_DEVEL("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
- qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
- if (!qc_dgrams_retransmit(qc))
- goto out;
- }
-
- if (qc_test_fd(qc))
- qc_rcv_buf(qc);
-
- if (!qc_treat_rx_pkts(qc))
- goto out;
-
- if (qc->flags & QUIC_FL_CONN_TO_KILL) {
- TRACE_DEVEL("connection to be killed", QUIC_EV_CONN_PHPKTS, qc);
- goto out;
- }
-
- if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
- !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
- goto out;
-
- st = qc->state;
- if (st >= QUIC_HS_ST_COMPLETE) {
- if (!(qc->flags & QUIC_FL_CONN_HPKTNS_DCD)) {
- /* Discard the Handshake packet number space. */
- TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
- quic_pktns_discard(qc->hel->pktns, qc);
- qc_set_timer(qc);
- qc_el_rx_pkts_del(qc->hel);
- qc_release_pktns_frms(qc, qc->hel->pktns);
- }
- }
-
- buf = qc_txb_alloc(qc);
- if (!buf)
- goto out;
-
- if (b_data(buf) && !qc_purge_txbuf(qc, buf))
- goto out;
-
- /* Currently buf cannot be non-empty at this stage. Even if a previous
- * sendto() has failed it is emptied to simulate packet emission and
- * rely on QUIC lost detection to try to emit it.
- */
- BUG_ON_HOT(b_data(buf));
- b_reset(buf);
-
- ret = qc_prep_hpkts(qc, buf, NULL);
- if (ret == -1) {
- qc_txb_release(qc);
- goto out;
- }
-
- if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
- if (qc->flags & QUIC_FL_CONN_TO_KILL)
- qc_txb_release(qc);
- goto out;
- }
-
- qc_txb_release(qc);
-
- out:
- /* Release the Handshake encryption level and packet number space if
- * the Handshake is confirmed and if there is no need to send
- * anymore Handshake packets.
- */
- if (quic_tls_pktns_is_dcd(qc, qc->hpktns) &&
- !qc_need_sending(qc, qc->hel)) {
- /* Ensure Initial packet encryption level and packet number space have
- * been released.
- */
- qc_enc_level_free(qc, &qc->iel);
- quic_pktns_release(qc, &qc->ipktns);
- qc_enc_level_free(qc, &qc->hel);
- quic_pktns_release(qc, &qc->hpktns);
- /* Also release the negotiated Inital TLS context. */
- quic_nictx_free(qc);
- }
-
- TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st);
- TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
- return t;
-}
-
-/* Release the memory allocated for <cs> CRYPTO stream */
-void quic_cstream_free(struct quic_cstream *cs)
-{
- if (!cs) {
- /* This is the case for ORTT encryption level */
- return;
- }
-
- quic_free_ncbuf(&cs->rx.ncbuf);
-
- qc_stream_desc_release(cs->desc);
- pool_free(pool_head_quic_cstream, cs);
-}
-
-/* Allocate a new QUIC stream for <qc>.
- * Return it if succeeded, NULL if not.
- */
-struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
-{
- struct quic_cstream *cs, *ret_cs = NULL;
-
- TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
- cs = pool_alloc(pool_head_quic_cstream);
- if (!cs) {
- TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
- goto leave;
- }
-
- cs->rx.offset = 0;
- cs->rx.ncbuf = NCBUF_NULL;
- cs->rx.offset = 0;
-
- cs->tx.offset = 0;
- cs->tx.sent_offset = 0;
- cs->tx.buf = BUF_NULL;
- cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
- if (!cs->desc) {
- TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
- goto err;
- }
-
- ret_cs = cs;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
- return ret_cs;
-
- err:
- pool_free(pool_head_quic_cstream, cs);
- goto leave;
-}
-
-/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
- * This is not the case if the remote peer address is not validated and if
- * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
- */
-static int qc_may_probe_ipktns(struct quic_conn *qc)
-{
- return quic_peer_validated_addr(qc) ||
- (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
-}
-
-/* Callback called upon loss detection and PTO timer expirations. */
-struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
-{
- struct quic_conn *qc = ctx;
- struct quic_pktns *pktns;
-
- TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
- TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
- NULL, NULL, &qc->path->ifae_pkts);
-
- task->expire = TICK_ETERNITY;
- pktns = quic_loss_pktns(qc);
-
- if (qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL)) {
- TRACE_PROTO("cancelled action (draining state)", QUIC_EV_CONN_PTIMER, qc);
- task = NULL;
- goto out;
- }
-
- if (tick_isset(pktns->tx.loss_time)) {
- struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
-
- qc_packet_loss_lookup(pktns, qc, &lost_pkts);
- if (!LIST_ISEMPTY(&lost_pkts))
- tasklet_wakeup(qc->wait_event.tasklet);
- if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
- qc_set_timer(qc);
- goto out;
- }
-
- if (qc->path->in_flight) {
- pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
- if (!pktns->tx.in_flight) {
- TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
- goto out;
- }
-
- if (pktns == qc->ipktns) {
- if (qc_may_probe_ipktns(qc)) {
- qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
- pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
- TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
- }
- else {
- TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
- }
- if (qc->hpktns->tx.in_flight) {
- qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
- qc->hpktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
- TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
- }
- }
- else if (pktns == qc->hpktns) {
- TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
- qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
- pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
- if (qc->ipktns && qc->ipktns->tx.in_flight) {
- if (qc_may_probe_ipktns(qc)) {
- qc->ipktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
- TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
- }
- else {
- TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
- }
- }
- }
- else if (pktns == qc->apktns) {
- pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
- /* Wake up upper layer if waiting to send new data. */
- if (!qc_notify_send(qc)) {
- TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
- qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
- pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
- }
- }
- }
- else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
- if (quic_tls_has_tx_sec(qc->hel))
- qc->hel->pktns->tx.pto_probe = 1;
- if (quic_tls_has_tx_sec(qc->iel))
- qc->iel->pktns->tx.pto_probe = 1;
- }
-
- tasklet_wakeup(qc->wait_event.tasklet);
- qc->path->loss.pto_count++;
-
- out:
- TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
- TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
-
- return task;
-}
-
-/* Parse the Retry token from buffer <token> with <end> a pointer to
- * one byte past the end of this buffer. This will extract the ODCID
- * which will be stored into <odcid>
- *
- * Returns 0 on success else non-zero.
- */
-static int parse_retry_token(struct quic_conn *qc,
- const unsigned char *token, const unsigned char *end,
- struct quic_cid *odcid)
-{
- int ret = 0;
- uint64_t odcid_len;
- uint32_t timestamp;
- uint32_t now_sec = (uint32_t)date.tv_sec;
-
- TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
-
- if (!quic_dec_int(&odcid_len, &token, end)) {
- TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
- goto leave;
- }
-
- /* RFC 9000 7.2. Negotiating Connection IDs:
- * When an Initial packet is sent by a client that has not previously
- * received an Initial or Retry packet from the server, the client
- * populates the Destination Connection ID field with an unpredictable
- * value. This Destination Connection ID MUST be at least 8 bytes in length.
- */
- if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
- TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
- goto leave;
- }
-
- if (end - token < odcid_len + sizeof timestamp) {
- TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
- goto leave;
- }
-
- timestamp = ntohl(read_u32(token + odcid_len));
- /* check if elapsed time is +/- QUIC_RETRY_DURATION_SEC
- * to tolerate token generator is not perfectly time synced
- */
- if ((uint32_t)(now_sec - timestamp) > QUIC_RETRY_DURATION_SEC &&
- (uint32_t)(timestamp - now_sec) > QUIC_RETRY_DURATION_SEC) {
- TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
- goto leave;
- }
-
- ret = 1;
- memcpy(odcid->data, token, odcid_len);
- odcid->len = odcid_len;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
- return !ret;
-}
-
-/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
- * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
- * for QUIC servers (or haproxy listeners).
- * <dcid> is the destination connection ID, <scid> is the source connection ID.
- * This latter <scid> CID as the same value on the wire as the one for <conn_id>
- * which is the first CID of this connection but a different internal representation used to build
- * NEW_CONNECTION_ID frames. This is the responsability of the caller to insert
- * <conn_id> in the CIDs tree for this connection (qc->cids).
- * <token> is the token found to be used for this connection with <token_len> as
- * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
- * Returns the connection if succeeded, NULL if not.
- */
-static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
- struct quic_cid *dcid, struct quic_cid *scid,
- const struct quic_cid *token_odcid,
- struct quic_connection_id *conn_id,
- struct sockaddr_storage *local_addr,
- struct sockaddr_storage *peer_addr,
- int server, int token, void *owner)
-{
- int i;
- struct quic_conn *qc;
- struct listener *l = NULL;
- struct quic_cc_algo *cc_algo = NULL;
-
- TRACE_ENTER(QUIC_EV_CONN_INIT);
-
- qc = pool_alloc(pool_head_quic_conn);
- if (!qc) {
- TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
- goto err;
- }
-
- /* Initialize in priority qc members required for a safe dealloc. */
- qc->nictx = NULL;
- /* Prevents these CID to be dumped by TRACE() calls */
- qc->scid.len = qc->odcid.len = qc->dcid.len = 0;
- /* required to use MTLIST_IN_LIST */
- MT_LIST_INIT(&qc->accept_list);
-
- LIST_INIT(&qc->rx.pkt_list);
-
- qc->streams_by_id = EB_ROOT_UNIQUE;
-
- /* Required to call free_quic_conn_cids() from quic_conn_release() */
- qc->cids = EB_ROOT;
- qc_init_fd(qc);
-
- LIST_INIT(&qc->back_refs);
- LIST_INIT(&qc->el_th_ctx);
-
- qc->wait_event.tasklet = NULL;
-
- /* Required to destroy <qc> tasks from quic_conn_release() */
- qc->timer_task = NULL;
- qc->idle_timer_task = NULL;
-
- qc->xprt_ctx = NULL;
- qc->conn = NULL;
- qc->qcc = NULL;
- qc->app_ops = NULL;
- qc->path = NULL;
-
- /* Keyupdate: required to safely call quic_tls_ku_free() from
- * quic_conn_release().
- */
- quic_tls_ku_reset(&qc->ku.prv_rx);
- quic_tls_ku_reset(&qc->ku.nxt_rx);
- quic_tls_ku_reset(&qc->ku.nxt_tx);
-
- /* Encryption levels */
- qc->iel = qc->eel = qc->hel = qc->ael = NULL;
- LIST_INIT(&qc->qel_list);
- /* Packet number spaces */
- qc->ipktns = qc->hpktns = qc->apktns = NULL;
- LIST_INIT(&qc->pktns_list);
-
- /* Required to safely call quic_conn_prx_cntrs_update() from quic_conn_release(). */
- qc->prx_counters = NULL;
-
- /* Now proceeds to allocation of qc members. */
- qc->rx.buf.area = pool_alloc(pool_head_quic_conn_rxbuf);
- if (!qc->rx.buf.area) {
- TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
- goto err;
- }
-
- /* QUIC Server (or listener). */
- if (server) {
- struct proxy *prx;
-
- l = owner;
- prx = l->bind_conf->frontend;
- cc_algo = l->bind_conf->quic_cc_algo;
-
- qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
- &quic_stats_module);
- qc->flags = QUIC_FL_CONN_LISTENER;
- qc->state = QUIC_HS_ST_SERVER_INITIAL;
- /* Copy the client original DCID. */
- qc->odcid = *dcid;
- /* Copy the packet SCID to reuse it as DCID for sending */
- qc->dcid = *scid;
- qc->tx.buf = BUF_NULL;
- qc->li = l;
- }
- /* QUIC Client (outgoing connection to servers) */
- else {
- qc->state = QUIC_HS_ST_CLIENT_INITIAL;
- if (dcid->len)
- memcpy(qc->dcid.data, dcid->data, dcid->len);
- qc->dcid.len = dcid->len;
- qc->li = NULL;
- }
- qc->mux_state = QC_MUX_NULL;
- qc->err = quic_err_transport(QC_ERR_NO_ERROR);
-
- conn_id->qc = qc;
-
- if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
- is_addr(local_addr)) {
- TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
- qc_alloc_fd(qc, local_addr, peer_addr);
-
- /* haproxy soft-stop is supported only for QUIC connections
- * with their owned socket.
- */
- if (qc_test_fd(qc))
- _HA_ATOMIC_INC(&jobs);
- }
-
- /* Select our SCID which is the first CID with 0 as sequence number. */
- qc->scid = conn_id->cid;
-
- if (!qc_enc_level_alloc(qc, &qc->ipktns, &qc->iel, ssl_encryption_initial)) {
- TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
- goto err;
- }
-
- qc->original_version = qv;
- qc->negotiated_version = NULL;
- qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
- TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
- TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
- /* TX part. */
- LIST_INIT(&qc->tx.frms_to_send);
- qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
- qc->tx.wbuf = qc->tx.rbuf = 0;
- qc->tx.bytes = qc->tx.prep_bytes = 0;
- memset(&qc->tx.params, 0, sizeof(qc->tx.params));
- qc->tx.buf = BUF_NULL;
- /* RX part. */
- qc->rx.bytes = 0;
- memset(&qc->rx.params, 0, sizeof(qc->rx.params));
- qc->rx.buf = b_make(qc->rx.buf.area, QUIC_CONN_RX_BUFSZ, 0, 0);
- for (i = 0; i < QCS_MAX_TYPES; i++)
- qc->rx.strms[i].nb_streams = 0;
-
- qc->nb_pkt_for_cc = 1;
- qc->nb_pkt_since_cc = 0;
-
- if (!quic_tls_ku_init(qc)) {
- TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
- goto err;
- }
-
- qc->max_ack_delay = 0;
- /* Only one path at this time (multipath not supported) */
- qc->path = &qc->paths[0];
- quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
-
- qc->stream_buf_count = 0;
- memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
- memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
-
- if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
- conn_id->stateless_reset_token,
- dcid->data, dcid->len,
- qc->scid.data, qc->scid.len, token_odcid))
- goto err;
-
- /* Initialize the idle timeout of the connection at the "max_idle_timeout"
- * value from local transport parameters.
- */
- qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
- qc->wait_event.tasklet = tasklet_new();
- if (!qc->wait_event.tasklet) {
- TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
- goto err;
- }
- qc->wait_event.tasklet->process = quic_conn_io_cb;
- qc->wait_event.tasklet->context = qc;
- qc->wait_event.events = 0;
- qc->subs = NULL;
-
- if (qc_alloc_ssl_sock_ctx(qc) ||
- !quic_conn_init_timer(qc) ||
- !quic_conn_init_idle_timer_task(qc))
- goto err;
-
- if (!qc_new_isecs(qc, &qc->iel->tls_ctx, qc->original_version, dcid->data, dcid->len, 1))
- goto err;
-
- /* Counters initialization */
- memset(&qc->cntrs, 0, sizeof qc->cntrs);
-
- LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
- qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
-
- TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
-
- return qc;
-
- err:
- quic_conn_release(qc);
- TRACE_LEAVE(QUIC_EV_CONN_INIT);
- return NULL;
-}
-
-/* Update the proxy counters of <qc> QUIC connection from its counters */
-static inline void quic_conn_prx_cntrs_update(struct quic_conn *qc)
-{
- if (!qc->prx_counters)
- return;
-
- HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt, qc->cntrs.dropped_pkt);
- HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt_bufoverrun, qc->cntrs.dropped_pkt_bufoverrun);
- HA_ATOMIC_ADD(&qc->prx_counters->dropped_parsing, qc->cntrs.dropped_parsing);
- HA_ATOMIC_ADD(&qc->prx_counters->socket_full, qc->cntrs.socket_full);
- HA_ATOMIC_ADD(&qc->prx_counters->sendto_err, qc->cntrs.sendto_err);
- HA_ATOMIC_ADD(&qc->prx_counters->sendto_err_unknown, qc->cntrs.sendto_err_unknown);
- HA_ATOMIC_ADD(&qc->prx_counters->sent_pkt, qc->cntrs.sent_pkt);
- /* It is possible that ->path was not initialized. For instance if a
- * QUIC connection allocation has failed.
- */
- if (qc->path)
- HA_ATOMIC_ADD(&qc->prx_counters->lost_pkt, qc->path->loss.nb_lost_pkt);
- HA_ATOMIC_ADD(&qc->prx_counters->conn_migration_done, qc->cntrs.conn_migration_done);
- /* Stream related counters */
- HA_ATOMIC_ADD(&qc->prx_counters->data_blocked, qc->cntrs.data_blocked);
- HA_ATOMIC_ADD(&qc->prx_counters->stream_data_blocked, qc->cntrs.stream_data_blocked);
- HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_bidi, qc->cntrs.streams_blocked_bidi);
- HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_uni, qc->cntrs.streams_blocked_uni);
-}
-
-/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
- * The connection tasklet is killed.
- *
- * This function must only be called by the thread responsible of the quic_conn
- * tasklet.
- */
-void quic_conn_release(struct quic_conn *qc)
-{
- struct eb64_node *node;
- struct quic_rx_packet *pkt, *pktback;
-
- TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
-
- if (!qc)
- goto leave;
-
- /* We must not free the quic-conn if the MUX is still allocated. */
- BUG_ON(qc->mux_state == QC_MUX_READY);
-
- if (qc_test_fd(qc))
- _HA_ATOMIC_DEC(&jobs);
-
- /* Close quic-conn socket fd. */
- qc_release_fd(qc, 0);
-
- /* in the unlikely (but possible) case the connection was just added to
- * the accept_list we must delete it from there.
- */
- MT_LIST_DELETE(&qc->accept_list);
-
- /* free remaining stream descriptors */
- node = eb64_first(&qc->streams_by_id);
- while (node) {
- struct qc_stream_desc *stream;
-
- stream = eb64_entry(node, struct qc_stream_desc, by_id);
- node = eb64_next(node);
-
- /* all streams attached to the quic-conn are released, so
- * qc_stream_desc_free will liberate the stream instance.
- */
- BUG_ON(!stream->release);
- qc_stream_desc_free(stream, 1);
- }
-
- /* Purge Rx packet list. */
- list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
- LIST_DELETE(&pkt->qc_rx_pkt_list);
- pool_free(pool_head_quic_rx_packet, pkt);
- }
-
- task_destroy(qc->idle_timer_task);
- qc->idle_timer_task = NULL;
-
- task_destroy(qc->timer_task);
- qc->timer_task = NULL;
-
- tasklet_free(qc->wait_event.tasklet);
-
- /* remove the connection from receiver cids trees */
- free_quic_conn_cids(qc);
-
- /* free the SSL sock context */
- qc_free_ssl_sock_ctx(&qc->xprt_ctx);
-
- quic_tls_ku_free(qc);
- if (qc->ael) {
- struct quic_tls_ctx *actx = &qc->ael->tls_ctx;
-
- /* Secrets used by keyupdate */
- pool_free(pool_head_quic_tls_secret, actx->rx.secret);
- pool_free(pool_head_quic_tls_secret, actx->tx.secret);
- }
-
- qc_enc_level_free(qc, &qc->iel);
- qc_enc_level_free(qc, &qc->eel);
- qc_enc_level_free(qc, &qc->hel);
- qc_enc_level_free(qc, &qc->ael);
-
- quic_tls_ctx_free(&qc->nictx);
-
- quic_pktns_release(qc, &qc->ipktns);
- quic_pktns_release(qc, &qc->hpktns);
- quic_pktns_release(qc, &qc->apktns);
-
- qc_detach_th_ctx_list(qc, 0);
-
- quic_conn_prx_cntrs_update(qc);
- pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
- qc->rx.buf.area = NULL;
- pool_free(pool_head_quic_conn, qc);
- qc = NULL;
-
- TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
-}
-
-/* Initialize the timer task of <qc> QUIC connection.
- * Returns 1 if succeeded, 0 if not.
- */
-static int quic_conn_init_timer(struct quic_conn *qc)
-{
- int ret = 0;
- /* Attach this task to the same thread ID used for the connection */
- TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
-
- qc->timer_task = task_new_here();
- if (!qc->timer_task) {
- TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
- goto leave;
- }
-
- qc->timer = TICK_ETERNITY;
- qc->timer_task->process = qc_process_timer;
- qc->timer_task->context = qc;
-
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
- return ret;
-}
-
-/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
- * connection. */
-static void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
-{
- unsigned int expire;
-
- if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
- TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
- qc->ack_expire = TICK_ETERNITY;
- task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
- }
- else {
- expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
- qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
- if (arm_ack) {
- /* Arm the ack timer only if not already armed. */
- if (!tick_isset(qc->ack_expire)) {
- qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
- qc->idle_timer_task->expire = qc->ack_expire;
- task_queue(qc->idle_timer_task);
- TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
- }
- }
- else {
- qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
- task_queue(qc->idle_timer_task);
- TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
- }
- }
-}
-
-/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
- * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
- * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
- * which must be rearmed.
- */
-static void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
-{
- TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
-
- if (read) {
- qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
- }
- else {
- qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
- }
- qc_idle_timer_do_rearm(qc, arm_ack);
-
- TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
-}
-
-/* The task handling the idle timeout */
-struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
-{
- struct quic_conn *qc = ctx;
- struct quic_counters *prx_counters = qc->prx_counters;
- unsigned int qc_flags = qc->flags;
-
- TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
-
- if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
- goto requeue;
-
- if (tick_is_expired(qc->ack_expire, now_ms)) {
- TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
- qc->ack_expire = TICK_ETERNITY;
- /* Note that ->idle_expire is always set. */
- t->expire = qc->idle_expire;
- /* Do not wakeup the I/O handler in DRAINING state or if the
- * connection must be killed as soon as possible.
- */
- if (!(qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL))) {
- qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
- tasklet_wakeup(qc->wait_event.tasklet);
- }
-
- goto requeue;
- }
-
- TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
- /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
- * might free the quic-conn too early via quic_close().
- */
- qc_notify_err(qc);
-
- /* If the MUX is still alive, keep the quic-conn. The MUX is
- * responsible to call quic_close to release it.
- */
- qc->flags |= QUIC_FL_CONN_EXP_TIMER;
- if (qc->mux_state != QC_MUX_READY) {
- quic_conn_release(qc);
- qc = NULL;
- }
-
- /* TODO if the quic-conn cannot be freed because of the MUX, we may at
- * least clean some parts of it such as the tasklet.
- */
-
- if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
- qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
- TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
- HA_ATOMIC_DEC(&prx_counters->half_open_conn);
- }
-
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
- return NULL;
-
- requeue:
- TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
- return t;
-}
-
-/* Initialize the idle timeout task for <qc>.
- * Returns 1 if succeeded, 0 if not.
- */
-static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
-{
- int ret = 0;
-
- TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
-
- qc->idle_timer_task = task_new_here();
- if (!qc->idle_timer_task) {
- TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
- goto leave;
- }
-
- qc->idle_timer_task->process = qc_idle_timer_task;
- qc->idle_timer_task->context = qc;
- qc->ack_expire = TICK_ETERNITY;
- qc_idle_timer_rearm(qc, 1, 0);
- task_queue(qc->idle_timer_task);
-
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
- return ret;
-}
-
-/* Parse into <pkt> a long header located at <*pos> position, <end> begin a pointer to the end
- * past one byte of this buffer.
- */
-static inline int quic_packet_read_long_header(unsigned char **pos, const unsigned char *end,
- struct quic_rx_packet *pkt)
-{
- int ret = 0;
- unsigned char dcid_len, scid_len;
-
- TRACE_ENTER(QUIC_EV_CONN_RXPKT);
-
- if (end == *pos) {
- TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
- goto leave;
- }
-
- /* Destination Connection ID Length */
- dcid_len = *(*pos)++;
- /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
- if (dcid_len > QUIC_CID_MAXLEN || end - *pos < dcid_len + 1) {
- TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
- goto leave;
- }
-
- if (dcid_len) {
- /* Check that the length of this received DCID matches the CID lengths
- * of our implementation for non Initials packets only.
- */
- if (pkt->version && pkt->version->num &&
- pkt->type != QUIC_PACKET_TYPE_INITIAL &&
- pkt->type != QUIC_PACKET_TYPE_0RTT &&
- dcid_len != QUIC_HAP_CID_LEN) {
- TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
- goto leave;
- }
-
- memcpy(pkt->dcid.data, *pos, dcid_len);
- }
-
- pkt->dcid.len = dcid_len;
- *pos += dcid_len;
-
- /* Source Connection ID Length */
- scid_len = *(*pos)++;
- if (scid_len > QUIC_CID_MAXLEN || end - *pos < scid_len) {
- TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
- goto leave;
- }
-
- if (scid_len)
- memcpy(pkt->scid.data, *pos, scid_len);
- pkt->scid.len = scid_len;
- *pos += scid_len;
-
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
- return ret;
-}
-
-/* Insert <pkt> RX packet in its <qel> RX packets tree */
-static void qc_pkt_insert(struct quic_conn *qc,
- struct quic_rx_packet *pkt, struct quic_enc_level *qel)
-{
- TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
-
- pkt->pn_node.key = pkt->pn;
- quic_rx_packet_refinc(pkt);
- eb64_insert(&qel->rx.pkts, &pkt->pn_node);
-
- TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
-}
-
-/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
- * address of the packet first byte, using the keys from encryption level <el>.
- *
- * If header protection has been successfully removed, packet data are copied
- * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
- * proceeded, and the packet is inserted into <qc> protected packets tree. In
- * both cases, packet can now be considered handled by the <qc> connection.
- *
- * If header protection cannot be removed due to <el> secrets already
- * discarded, no operation is conducted.
- *
- * Returns 1 on success : packet data is now handled by the connection. On
- * error 0 is returned : packet should be dropped by the caller.
- */
-static inline int qc_try_rm_hp(struct quic_conn *qc,
- struct quic_rx_packet *pkt,
- unsigned char *beg,
- struct quic_enc_level **el)
-{
- int ret = 0;
- unsigned char *pn = NULL; /* Packet number field */
- enum quic_tls_enc_level tel;
- struct quic_enc_level *qel;
- /* Only for traces. */
-
- TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
- BUG_ON(!pkt->pn_offset);
-
- /* The packet number is here. This is also the start minus
- * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
- * protection.
- */
- pn = beg + pkt->pn_offset;
-
- tel = quic_packet_type_enc_level(pkt->type);
- qel = qc_quic_enc_level(qc, tel);
- if (!qel) {
- struct quic_enc_level **qc_qel = qel_to_qel_addr(qc, tel);
- struct quic_pktns **qc_pktns = qel_to_quic_pktns(qc, tel);
-
- if (!qc_enc_level_alloc(qc, qc_pktns, qc_qel, quic_to_ssl_enc_level(tel))) {
- TRACE_PROTO("Could not allocated an encryption level", QUIC_EV_CONN_ADDDATA, qc);
- goto out;
- }
-
- qel = *qc_qel;
- }
-
- if (qc_qel_may_rm_hp(qc, qel)) {
- struct quic_tls_ctx *tls_ctx = qc_select_tls_ctx(qc, qel, pkt);
-
- /* Note that the following function enables us to unprotect the packet
- * number and its length subsequently used to decrypt the entire
- * packets.
- */
- if (!qc_do_rm_hp(qc, pkt, tls_ctx,
- qel->pktns->rx.largest_pn, pn, beg)) {
- TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
- goto out;
- }
-
- qc_handle_spin_bit(qc, pkt, qel);
- /* The AAD includes the packet number field. */
- pkt->aad_len = pkt->pn_offset + pkt->pnl;
- if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
- TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
- goto out;
- }
-
- TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
- }
- else {
- TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
- LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
- quic_rx_packet_refinc(pkt);
- }
-
- *el = qel;
- /* No reference counter incrementation here!!! */
- LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
- memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
- pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
- b_add(&qc->rx.buf, pkt->len);
-
- ret = 1;
- out:
- TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
- return ret;
-}
-
-/* Return the QUIC version (quic_version struct) with <version> as version number
- * if supported or NULL if not.
- */
-static inline const struct quic_version *qc_supported_version(uint32_t version)
-{
- int i;
-
- if (unlikely(!version))
- return &quic_version_VN_reserved;
-
- for (i = 0; i < quic_versions_nb; i++)
- if (quic_versions[i].num == version)
- return &quic_versions[i];
-
- return NULL;
-}
-
-/* Parse a QUIC packet header starting at <pos> position without exceeding <end>.
- * Version and type are stored in <pkt> packet instance. Type is set to unknown
- * on two occasions : for unsupported version, in this case version field is
- * set to NULL; for Version Negotiation packet with version number set to 0.
- *
- * Returns 1 on success else 0.
- */
-int qc_parse_hd_form(struct quic_rx_packet *pkt,
- unsigned char **pos, const unsigned char *end)
-{
- uint32_t version;
- int ret = 0;
- const unsigned char byte0 = **pos;
-
- TRACE_ENTER(QUIC_EV_CONN_RXPKT);
- pkt->version = NULL;
- pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
-
- (*pos)++;
- if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
- unsigned char type =
- (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
-
- /* Version */
- if (!quic_read_uint32(&version, (const unsigned char **)pos, end)) {
- TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
- goto out;
- }
-
- pkt->version = qc_supported_version(version);
- if (version && pkt->version) {
- if (version != QUIC_PROTOCOL_VERSION_2) {
- pkt->type = type;
- }
- else {
- switch (type) {
- case 0:
- pkt->type = QUIC_PACKET_TYPE_RETRY;
- break;
- case 1:
- pkt->type = QUIC_PACKET_TYPE_INITIAL;
- break;
- case 2:
- pkt->type = QUIC_PACKET_TYPE_0RTT;
- break;
- case 3:
- pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
- break;
- }
- }
- }
- }
- else {
- if (byte0 & QUIC_PACKET_SPIN_BIT)
- pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
- pkt->type = QUIC_PACKET_TYPE_SHORT;
- }
-
- ret = 1;
- out:
- TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
- return ret;
-}
-
-/*
- * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
- * address <addr>.
- * Implementation of RFC9000 6. Version Negotiation
- *
- * TODO implement a rate-limiting sending of Version Negotiation packets
- *
- * Returns 0 on success else non-zero
- */
-static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
- struct quic_rx_packet *pkt)
-{
- char buf[256];
- int ret = 0, i = 0, j;
- uint32_t version;
- const socklen_t addrlen = get_addr_len(addr);
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT);
- /*
- * header form
- * long header, fixed bit to 0 for Version Negotiation
- */
- /* TODO: RAND_bytes() should be replaced? */
- if (RAND_bytes((unsigned char *)buf, 1) != 1) {
- TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
- goto out;
- }
-
- buf[i++] |= '\x80';
- /* null version for Version Negotiation */
- buf[i++] = '\x00';
- buf[i++] = '\x00';
- buf[i++] = '\x00';
- buf[i++] = '\x00';
-
- /* source connection id */
- buf[i++] = pkt->scid.len;
- memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
- i += pkt->scid.len;
-
- /* destination connection id */
- buf[i++] = pkt->dcid.len;
- memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
- i += pkt->dcid.len;
-
- /* supported version */
- for (j = 0; j < quic_versions_nb; j++) {
- version = htonl(quic_versions[j].num);
- memcpy(&buf[i], &version, sizeof(version));
- i += sizeof(version);
- }
-
- if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
+ if (b_data(buf) && !qc_purge_txbuf(qc, buf))
goto out;
- ret = 1;
- out:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
- return !ret;
-}
-
-/* Send a stateless reset packet depending on <pkt> RX packet information
- * from <fd> UDP socket to <dst>
- * Return 1 if succeeded, 0 if not.
- */
-static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
- struct quic_rx_packet *rxpkt)
-{
- int ret = 0, pktlen, rndlen;
- unsigned char pkt[64];
- const socklen_t addrlen = get_addr_len(dstaddr);
- struct proxy *prx;
- struct quic_counters *prx_counters;
-
- TRACE_ENTER(QUIC_EV_STATELESS_RST);
-
- prx = l->bind_conf->frontend;
- prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
- /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
- * The resulting minimum size of 21 bytes does not guarantee that a Stateless
- * Reset is difficult to distinguish from other packets if the recipient requires
- * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
- * that all packets it sends are at least 22 bytes longer than the minimum
- * connection ID length that it requests the peer to include in its packets,
- * adding PADDING frames as necessary. This ensures that any Stateless Reset
- * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
- * An endpoint that sends a Stateless Reset in response to a packet that is
- * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
- * than the packet it responds to.
- */
-
- /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
- pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
- pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
- rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
-
- /* Put a header of random bytes */
- /* TODO: RAND_bytes() should be replaced */
- if (RAND_bytes(pkt, rndlen) != 1) {
- TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
- goto leave;
- }
-
- /* Clear the most significant bit, and set the second one */
- *pkt = (*pkt & ~0x80) | 0x40;
- if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
- rxpkt->dcid.data, rxpkt->dcid.len))
- goto leave;
-
- if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
- goto leave;
-
- ret = 1;
- HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
- TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
- leave:
- TRACE_LEAVE(QUIC_EV_STATELESS_RST);
- return ret;
-}
-
-/* QUIC server only function.
- * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
- * This is the responsibility of the caller to check <aad> size is big enough
- * to contain these data.
- * Return the number of bytes copied to <aad>.
- */
-static int quic_generate_retry_token_aad(unsigned char *aad,
- uint32_t version,
- const struct quic_cid *dcid,
- const struct quic_cid *scid,
- const struct sockaddr_storage *addr)
-{
- unsigned char *p;
-
- p = aad;
- *(uint32_t *)p = htonl(version);
- p += sizeof version;
- memcpy(p, dcid->data, dcid->len);
- p += dcid->len;
- p += quic_saddr_cpy(p, addr);
- memcpy(p, scid->data, scid->len);
- p += scid->len;
-
- return p - aad;
-}
-
-/* QUIC server only function.
- * Generate the token to be used in Retry packets. The token is written to
- * <token> with <len> as length. <odcid> is the original destination connection
- * ID and <dcid> is our side destination connection ID (or client source
- * connection ID).
- * Returns the length of the encoded token or 0 on error.
- */
-static int quic_generate_retry_token(unsigned char *token, size_t len,
- const uint32_t version,
- const struct quic_cid *odcid,
- const struct quic_cid *scid,
- const struct quic_cid *dcid,
- struct sockaddr_storage *addr)
-{
- int ret = 0;
- unsigned char *p;
- unsigned char aad[sizeof(uint32_t) + QUIC_CID_MAXLEN +
- sizeof(in_port_t) + sizeof(struct in6_addr) +
- QUIC_CID_MAXLEN];
- size_t aadlen;
- unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
- unsigned char key[QUIC_TLS_KEY_LEN];
- unsigned char iv[QUIC_TLS_IV_LEN];
- const unsigned char *sec = (const unsigned char *)global.cluster_secret;
- size_t seclen = strlen(global.cluster_secret);
- EVP_CIPHER_CTX *ctx = NULL;
- const EVP_CIPHER *aead = EVP_aes_128_gcm();
- uint32_t timestamp = (uint32_t)date.tv_sec;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT);
-
- /* The token is made of the token format byte, the ODCID prefixed by its one byte
- * length, the creation timestamp, an AEAD TAG, and finally
- * the random bytes used to derive the secret to encrypt the token.
- */
- if (1 + odcid->len + 1 + sizeof(timestamp) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN > len)
- goto err;
-
- aadlen = quic_generate_retry_token_aad(aad, version, scid, dcid, addr);
- /* TODO: RAND_bytes() should be replaced */
- if (RAND_bytes(salt, sizeof salt) != 1) {
- TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
- goto err;
- }
-
- if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
- salt, sizeof salt, sec, seclen)) {
- TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
- goto err;
- }
-
- if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
- TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
- goto err;
- }
-
- /* Token build */
- p = token;
- *p++ = QUIC_TOKEN_FMT_RETRY,
- *p++ = odcid->len;
- memcpy(p, odcid->data, odcid->len);
- p += odcid->len;
- write_u32(p, htonl(timestamp));
- p += sizeof timestamp;
-
- /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
- if (!quic_tls_encrypt(token + 1, p - token - 1, aad, aadlen, ctx, aead, iv)) {
- TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
- goto err;
- }
-
- p += QUIC_TLS_TAG_LEN;
- memcpy(p, salt, sizeof salt);
- p += sizeof salt;
- EVP_CIPHER_CTX_free(ctx);
-
- ret = p - token;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
- return ret;
-
- err:
- if (ctx)
- EVP_CIPHER_CTX_free(ctx);
- goto leave;
-}
-
-/* QUIC server only function.
- *
- * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
- * the UDP datagram containing <pkt> and <l> is the listener instance on which
- * it was received. If the token is valid, the ODCID of <qc> QUIC connection
- * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
- * to validate the token but it can be NULL : in this case the version will be
- * retrieved from the packet.
- *
- * Return 1 if succeeded, 0 if not.
- */
-
-static int quic_retry_token_check(struct quic_rx_packet *pkt,
- struct quic_dgram *dgram,
- struct listener *l,
- struct quic_conn *qc,
- struct quic_cid *odcid)
-{
- struct proxy *prx;
- struct quic_counters *prx_counters;
- int ret = 0;
- unsigned char *token = pkt->token;
- const uint64_t tokenlen = pkt->token_len;
- unsigned char buf[128];
- unsigned char aad[sizeof(uint32_t) + QUIC_CID_MAXLEN +
- sizeof(in_port_t) + sizeof(struct in6_addr) +
- QUIC_CID_MAXLEN];
- size_t aadlen;
- const unsigned char *salt;
- unsigned char key[QUIC_TLS_KEY_LEN];
- unsigned char iv[QUIC_TLS_IV_LEN];
- const unsigned char *sec = (const unsigned char *)global.cluster_secret;
- size_t seclen = strlen(global.cluster_secret);
- EVP_CIPHER_CTX *ctx = NULL;
- const EVP_CIPHER *aead = EVP_aes_128_gcm();
- const struct quic_version *qv = qc ? qc->original_version :
- pkt->version;
-
- TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
-
- /* The caller must ensure this. */
- BUG_ON(!global.cluster_secret || !pkt->token_len);
-
- prx = l->bind_conf->frontend;
- prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
-
- if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
- /* TODO: New token check */
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
- goto leave;
- }
-
- if (sizeof buf < tokenlen) {
- TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
- goto err;
- }
-
- /* The token is made of the token format byte, the ODCID prefixed by its one byte
- * length, the creation timestamp, an AEAD TAG, and finally
- * the random bytes used to derive the secret to encrypt the token.
+ /* Currently buf cannot be non-empty at this stage. Even if a previous
+ * sendto() has failed it is emptied to simulate packet emission and
+ * rely on QUIC lost detection to try to emit it.
*/
- if (tokenlen < 2 + QUIC_ODCID_MINLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN ||
- tokenlen > 2 + QUIC_CID_MAXLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN) {
- TRACE_ERROR("invalid token length", QUIC_EV_CONN_LPKT, qc);
- goto err;
- }
-
- aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->dcid, &pkt->scid, &dgram->saddr);
- salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
- if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
- salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
- TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
- goto err;
- }
-
- if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
- TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
- goto err;
- }
-
- /* The token is prefixed by a one-byte length format which is not ciphered. */
- if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
- ctx, aead, key, iv)) {
- TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
- goto err;
- }
-
- if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
- TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
- goto err;
- }
-
- EVP_CIPHER_CTX_free(ctx);
-
- ret = 1;
- HA_ATOMIC_INC(&prx_counters->retry_validated);
-
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
- return ret;
-
- err:
- HA_ATOMIC_INC(&prx_counters->retry_error);
- if (ctx)
- EVP_CIPHER_CTX_free(ctx);
- goto leave;
-}
-
-/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
- * the Initial <pkt> packet.
- *
- * Returns 0 on success else non-zero.
- */
-static int send_retry(int fd, struct sockaddr_storage *addr,
- struct quic_rx_packet *pkt, const struct quic_version *qv)
-{
- int ret = 0;
- unsigned char buf[128];
- int i = 0, token_len;
- const socklen_t addrlen = get_addr_len(addr);
- struct quic_cid scid;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT);
-
- /* long header(1) | fixed bit(1) | packet type QUIC_PACKET_TYPE_RETRY(2) | unused random bits(4)*/
- buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
- (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT) |
- statistical_prng_range(16);
- /* version */
- write_n32(&buf[i], qv->num);
- i += sizeof(uint32_t);
-
- /* Use the SCID from <pkt> for Retry DCID. */
- buf[i++] = pkt->scid.len;
- memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
- i += pkt->scid.len;
-
- /* Generate a new CID to be used as SCID for the Retry packet. */
- scid.len = QUIC_HAP_CID_LEN;
- /* TODO: RAND_bytes() should be replaced */
- if (RAND_bytes(scid.data, scid.len) != 1) {
- TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
- goto out;
- }
-
- buf[i++] = scid.len;
- memcpy(&buf[i], scid.data, scid.len);
- i += scid.len;
+ BUG_ON_HOT(b_data(buf));
+ b_reset(buf);
- /* token */
- if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
- &pkt->dcid, &scid, &pkt->scid, addr))) {
- TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
+ ret = qc_prep_hpkts(qc, buf, NULL);
+ if (ret == -1) {
+ qc_txb_release(qc);
goto out;
}
- i += token_len;
-
- /* token integrity tag */
- if ((sizeof(buf) - i < QUIC_TLS_TAG_LEN) ||
- !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
- pkt->dcid.len, buf, i, qv)) {
- TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
+ if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
+ if (qc->flags & QUIC_FL_CONN_TO_KILL)
+ qc_txb_release(qc);
goto out;
}
- i += QUIC_TLS_TAG_LEN;
-
- if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
- TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
- goto out;
- }
+ qc_txb_release(qc);
- ret = 1;
out:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
- return !ret;
-}
-
-/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
- * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
- * is used.
- *
- * Returns the instance or NULL if not found.
- */
-static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
- struct listener *l,
- struct sockaddr_storage *saddr,
- int *new_tid)
-{
- struct quic_conn *qc = NULL;
- struct ebmb_node *node;
- struct quic_connection_id *conn_id;
- struct quic_cid_tree *tree;
- uint conn_id_tid;
-
- TRACE_ENTER(QUIC_EV_CONN_RXPKT);
- *new_tid = -1;
-
- /* First look into DCID tree. */
- tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)];
- HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
- node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len);
-
- /* If not found on an Initial/0-RTT packet, it could be because an
- * ODCID is reused by the client. Calculate the derived CID value to
- * retrieve it from the DCID tree.
+ /* Release the Handshake encryption level and packet number space if
+ * the Handshake is confirmed and if there is no need to send
+ * anymore Handshake packets.
*/
- if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
- pkt->type == QUIC_PACKET_TYPE_0RTT)) {
- const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr);
-
- HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
-
- tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
- HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
- node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len);
- }
-
- if (!node)
- goto end;
-
- conn_id = ebmb_entry(node, struct quic_connection_id, node);
- conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid);
- if (conn_id_tid != tid) {
- *new_tid = conn_id_tid;
- goto end;
+ if (quic_tls_pktns_is_dcd(qc, qc->hpktns) &&
+ !qc_need_sending(qc, qc->hel)) {
+ /* Ensure Initial packet encryption level and packet number space have
+ * been released.
+ */
+ qc_enc_level_free(qc, &qc->iel);
+ quic_pktns_release(qc, &qc->ipktns);
+ qc_enc_level_free(qc, &qc->hel);
+ quic_pktns_release(qc, &qc->hpktns);
+ /* Also release the negotiated Inital TLS context. */
+ quic_nictx_free(qc);
}
- qc = conn_id->qc;
-
- end:
- HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
- TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
- return qc;
-}
-
-/* Check that all the bytes between <pos> included and <end> address
- * excluded are null. This is the responsibility of the caller to
- * check that there is at least one byte between <pos> end <end>.
- * Return 1 if this all the bytes are null, 0 if not.
- */
-static inline int quic_padding_check(const unsigned char *pos,
- const unsigned char *end)
-{
- while (pos < end && !*pos)
- pos++;
- return pos == end;
+ TRACE_PROTO("ssl error", QUIC_EV_CONN_IO_CB, qc, &st);
+ TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
+ return t;
}
-/* Find the associated connection to the packet <pkt> or create a new one if
- * this is an Initial packet. <dgram> is the datagram containing the packet and
- * <l> is the listener instance on which it was received.
- *
- * By default, <new_tid> is set to -1. However, if thread affinity has been
- * chanbed, it will be set to its new thread ID.
- *
- * Returns the quic-conn instance or NULL if not found or thread affinity
- * changed.
- */
-static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
- struct quic_dgram *dgram,
- struct listener *l,
- int *new_tid)
+/* Release the memory allocated for <cs> CRYPTO stream */
+void quic_cstream_free(struct quic_cstream *cs)
{
- struct quic_cid token_odcid = { .len = 0 };
- struct quic_conn *qc = NULL;
- struct proxy *prx;
- struct quic_counters *prx_counters;
-
- TRACE_ENTER(QUIC_EV_CONN_LPKT);
-
- *new_tid = -1;
-
- prx = l->bind_conf->frontend;
- prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
-
- qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid);
-
- /* If connection already created or rebinded on another thread. */
- if (!qc && *new_tid != -1 && tid != *new_tid)
- goto out;
-
- if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
- BUG_ON(!pkt->version); /* This must not happen. */
-
- if (global.cluster_secret && pkt->token_len) {
- if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
- goto err;
- }
-
- if (!qc) {
- struct quic_cid_tree *tree;
- struct ebmb_node *node;
- struct quic_connection_id *conn_id;
- int ipv4;
-
- if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
- HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
- TRACE_PROTO("Initial without token, sending retry",
- QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
- TRACE_ERROR("Error during Retry generation",
- QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- goto out;
- }
-
- HA_ATOMIC_INC(&prx_counters->retry_sent);
- goto out;
- }
-
- /* RFC 9000 7.2. Negotiating Connection IDs:
- * When an Initial packet is sent by a client that has not previously
- * received an Initial or Retry packet from the server, the client
- * populates the Destination Connection ID field with an unpredictable
- * value. This Destination Connection ID MUST be at least 8 bytes in length.
- */
- if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
- TRACE_PROTO("dropped packet",
- QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- goto err;
- }
-
- pkt->saddr = dgram->saddr;
- ipv4 = dgram->saddr.ss_family == AF_INET;
-
- /* Generate the first connection CID. This is derived from the client
- * ODCID and address. This allows to retrieve the connection from the
- * ODCID without storing it in the CID tree. This is an interesting
- * optimization as the client is expected to stop using its ODCID in
- * favor of our generated value.
- */
- conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr);
- if (!conn_id)
- goto err;
-
- qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
- conn_id, &dgram->daddr, &pkt->saddr, 1,
- !!pkt->token_len, l);
- if (qc == NULL) {
- pool_free(pool_head_quic_connection_id, conn_id);
- goto err;
- }
+ if (!cs) {
+ /* This is the case for ORTT encryption level */
+ return;
+ }
- tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
- HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
- node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len);
- if (node != &conn_id->node) {
- pool_free(pool_head_quic_connection_id, conn_id);
+ quic_free_ncbuf(&cs->rx.ncbuf);
- conn_id = ebmb_entry(node, struct quic_connection_id, node);
- *new_tid = HA_ATOMIC_LOAD(&conn_id->tid);
- quic_conn_release(qc);
- qc = NULL;
- }
- else {
- /* From here, <qc> is the correct connection for this <pkt> Initial
- * packet. <conn_id> must be inserted in the CIDs tree for this
- * connection.
- */
- eb64_insert(&qc->cids, &conn_id->seq_num);
- /* Initialize the next CID sequence number to be used for this connection. */
- qc->next_cid_seq_num = 1;
- }
- HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
+ qc_stream_desc_release(cs->desc);
+ pool_free(pool_head_quic_cstream, cs);
+}
- if (*new_tid != -1)
- goto out;
+/* Allocate a new QUIC stream for <qc>.
+ * Return it if succeeded, NULL if not.
+ */
+struct quic_cstream *quic_cstream_new(struct quic_conn *qc)
+{
+ struct quic_cstream *cs, *ret_cs = NULL;
- HA_ATOMIC_INC(&prx_counters->half_open_conn);
- }
+ TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
+ cs = pool_alloc(pool_head_quic_cstream);
+ if (!cs) {
+ TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
+ goto leave;
}
- else if (!qc) {
- TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
- TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
+
+ cs->rx.offset = 0;
+ cs->rx.ncbuf = NCBUF_NULL;
+ cs->rx.offset = 0;
+
+ cs->tx.offset = 0;
+ cs->tx.sent_offset = 0;
+ cs->tx.buf = BUF_NULL;
+ cs->desc = qc_stream_desc_new((uint64_t)-1, -1, cs, qc);
+ if (!cs->desc) {
+ TRACE_ERROR("crypto stream allocation failed", QUIC_EV_CONN_INIT, qc);
goto err;
}
- out:
+ ret_cs = cs;
+ leave:
TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
- return qc;
+ return ret_cs;
err:
- if (qc)
- qc->cntrs.dropped_pkt++;
- else
- HA_ATOMIC_INC(&prx_counters->dropped_pkt);
- TRACE_LEAVE(QUIC_EV_CONN_LPKT);
- return NULL;
+ pool_free(pool_head_quic_cstream, cs);
+ goto leave;
}
-/* Parse a QUIC packet starting at <pos>. Data won't be read after <end> even
- * if the packet is incomplete. This function will populate fields of <pkt>
- * instance, most notably its length. <dgram> is the UDP datagram which
- * contains the parsed packet. <l> is the listener instance on which it was
- * received.
- *
- * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
- * the real packet value or to cover all data between <pos> and <end> : this is
- * useful to reject a whole datagram.
- */
-static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
- unsigned char *pos, const unsigned char *end,
- struct quic_dgram *dgram, struct listener *l)
+/* Callback called upon loss detection and PTO timer expirations. */
+struct task *qc_process_timer(struct task *task, void *ctx, unsigned int state)
{
- const unsigned char *beg = pos;
- struct proxy *prx;
- struct quic_counters *prx_counters;
+ struct quic_conn *qc = ctx;
+ struct quic_pktns *pktns;
- TRACE_ENTER(QUIC_EV_CONN_LPKT);
+ TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc);
+ TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc,
+ NULL, NULL, &qc->path->ifae_pkts);
- prx = l->bind_conf->frontend;
- prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
+ task->expire = TICK_ETERNITY;
+ pktns = quic_loss_pktns(qc);
- if (end <= pos) {
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
- goto drop;
+ if (qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL)) {
+ TRACE_PROTO("cancelled action (draining state)", QUIC_EV_CONN_PTIMER, qc);
+ task = NULL;
+ goto out;
}
- /* Fixed bit */
- if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
- if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
- quic_padding_check(pos, end)) {
- /* Some browsers may pad the remaining datagram space with null bytes.
- * That is what we called add padding out of QUIC packets. Such
- * datagrams must be considered as valid. But we can only consume
- * the remaining space.
- */
- pkt->len = end - pos;
- goto drop_silent;
- }
-
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
- goto drop;
- }
+ if (tick_isset(pktns->tx.loss_time)) {
+ struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
- /* Header form */
- if (!qc_parse_hd_form(pkt, &pos, end)) {
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
- goto drop;
+ qc_packet_loss_lookup(pktns, qc, &lost_pkts);
+ if (!LIST_ISEMPTY(&lost_pkts))
+ tasklet_wakeup(qc->wait_event.tasklet);
+ if (qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms))
+ qc_set_timer(qc);
+ goto out;
}
- if (pkt->type != QUIC_PACKET_TYPE_SHORT) {
- uint64_t len;
- TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
-
- if (!quic_packet_read_long_header(&pos, end, pkt)) {
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
- goto drop;
- }
-
- /* When multiple QUIC packets are coalesced on the same UDP datagram,
- * they must have the same DCID.
- */
- if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
- (pkt->dcid.len != dgram->dcid_len ||
- memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
- goto drop;
- }
-
- /* Retry of Version Negotiation packets are only sent by servers */
- if (pkt->type == QUIC_PACKET_TYPE_RETRY ||
- (pkt->version && !pkt->version->num)) {
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
- goto drop;
- }
-
- /* RFC9000 6. Version Negotiation */
- if (!pkt->version) {
- /* unsupported version, send Negotiation packet */
- if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
- TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
- goto drop_silent;
- }
-
- TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
- goto drop_silent;
+ if (qc->path->in_flight) {
+ pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_CONFIRMED, NULL);
+ if (!pktns->tx.in_flight) {
+ TRACE_PROTO("No in flight packets to probe with", QUIC_EV_CONN_TXPKT, qc);
+ goto out;
}
- /* For Initial packets, and for servers (QUIC clients connections),
- * there is no Initial connection IDs storage.
- */
- if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
- uint64_t token_len;
-
- if (!quic_dec_int(&token_len, (const unsigned char **)&pos, end) ||
- end - pos < token_len) {
- TRACE_PROTO("Packet dropped",
- QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- goto drop;
+ if (pktns == qc->ipktns) {
+ if (qc_may_probe_ipktns(qc)) {
+ qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
+ pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
+ TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
}
-
- /* TODO Retry should be automatically activated if
- * suspect network usage is detected.
- */
- if (global.cluster_secret && !token_len) {
- if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
- TRACE_PROTO("Initial without token, sending retry",
- QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
- TRACE_PROTO("Error during Retry generation",
- QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- goto drop_silent;
- }
-
- HA_ATOMIC_INC(&prx_counters->retry_sent);
- goto drop_silent;
- }
+ else {
+ TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
}
- else if (!global.cluster_secret && token_len) {
- /* Impossible case: a token was received without configured
- * cluster secret.
- */
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
- NULL, NULL, NULL, pkt->version);
- goto drop;
+ if (qc->hpktns->tx.in_flight) {
+ qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
+ qc->hpktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
+ TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
}
-
- pkt->token = pos;
- pkt->token_len = token_len;
- pos += pkt->token_len;
}
- else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
- if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
- TRACE_PROTO("Packet dropped",
- QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- goto drop;
+ else if (pktns == qc->hpktns) {
+ TRACE_STATE("needs to probe Handshake packet number space", QUIC_EV_CONN_TXPKT, qc);
+ qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
+ pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
+ if (qc->ipktns && qc->ipktns->tx.in_flight) {
+ if (qc_may_probe_ipktns(qc)) {
+ qc->ipktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
+ TRACE_STATE("needs to probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
+ }
+ else {
+ TRACE_STATE("Cannot probe Initial packet number space", QUIC_EV_CONN_TXPKT, qc);
+ }
}
}
-
- if (!quic_dec_int(&len, (const unsigned char **)&pos, end) ||
- end - pos < len) {
- TRACE_PROTO("Packet dropped",
- QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
- goto drop;
+ else if (pktns == qc->apktns) {
+ pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
+ /* Wake up upper layer if waiting to send new data. */
+ if (!qc_notify_send(qc)) {
+ TRACE_STATE("needs to probe 01RTT packet number space", QUIC_EV_CONN_TXPKT, qc);
+ qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
+ pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
+ }
}
+ }
+ else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
+ if (quic_tls_has_tx_sec(qc->hel))
+ qc->hel->pktns->tx.pto_probe = 1;
+ if (quic_tls_has_tx_sec(qc->iel))
+ qc->iel->pktns->tx.pto_probe = 1;
+ }
- /* Packet Number is stored here. Packet Length totalizes the
- * rest of the content.
- */
- pkt->pn_offset = pos - beg;
- pkt->len = pkt->pn_offset + len;
-
- /* RFC 9000. Initial Datagram Size
- *
- * A server MUST discard an Initial packet that is carried in a UDP datagram
- * with a payload that is smaller than the smallest allowed maximum datagram
- * size of 1200 bytes.
- */
- if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
- dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
- TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
- HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
- goto drop;
- }
+ tasklet_wakeup(qc->wait_event.tasklet);
+ qc->path->loss.pto_count++;
- /* Interrupt parsing after packet length retrieval : this
- * ensures that only the packet is dropped but not the whole
- * datagram.
- */
- if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
- TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
- goto drop;
- }
- }
- else {
- TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
- if (end - pos < QUIC_HAP_CID_LEN) {
- TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
- goto drop;
- }
+ out:
+ TRACE_PROTO("process timer", QUIC_EV_CONN_PTIMER, qc, pktns);
+ TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc);
- memcpy(pkt->dcid.data, pos, QUIC_HAP_CID_LEN);
- pkt->dcid.len = QUIC_HAP_CID_LEN;
+ return task;
+}
- /* When multiple QUIC packets are coalesced on the same UDP datagram,
- * they must have the same DCID.
- */
- if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
- (pkt->dcid.len != dgram->dcid_len ||
- memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
- TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
- goto drop;
- }
+/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
+ * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
+ * for QUIC servers (or haproxy listeners).
+ * <dcid> is the destination connection ID, <scid> is the source connection ID.
+ * This latter <scid> CID as the same value on the wire as the one for <conn_id>
+ * which is the first CID of this connection but a different internal representation used to build
+ * NEW_CONNECTION_ID frames. This is the responsability of the caller to insert
+ * <conn_id> in the CIDs tree for this connection (qc->cids).
+ * <token> is the token found to be used for this connection with <token_len> as
+ * length. Endpoints addresses are specified via <local_addr> and <peer_addr>.
+ * Returns the connection if succeeded, NULL if not.
+ */
+struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
+ struct quic_cid *dcid, struct quic_cid *scid,
+ const struct quic_cid *token_odcid,
+ struct quic_connection_id *conn_id,
+ struct sockaddr_storage *local_addr,
+ struct sockaddr_storage *peer_addr,
+ int server, int token, void *owner)
+{
+ int i;
+ struct quic_conn *qc;
+ struct listener *l = NULL;
+ struct quic_cc_algo *cc_algo = NULL;
- pos += QUIC_HAP_CID_LEN;
+ TRACE_ENTER(QUIC_EV_CONN_INIT);
- pkt->pn_offset = pos - beg;
- /* A short packet is the last one of a UDP datagram. */
- pkt->len = end - beg;
+ qc = pool_alloc(pool_head_quic_conn);
+ if (!qc) {
+ TRACE_ERROR("Could not allocate a new connection", QUIC_EV_CONN_INIT);
+ goto err;
}
- TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
- TRACE_LEAVE(QUIC_EV_CONN_LPKT);
- return 0;
+ /* Initialize in priority qc members required for a safe dealloc. */
+ qc->nictx = NULL;
+ /* Prevents these CID to be dumped by TRACE() calls */
+ qc->scid.len = qc->odcid.len = qc->dcid.len = 0;
+ /* required to use MTLIST_IN_LIST */
+ MT_LIST_INIT(&qc->accept_list);
- drop:
- HA_ATOMIC_INC(&prx_counters->dropped_pkt);
- drop_silent:
- if (!pkt->len)
- pkt->len = end - beg;
- TRACE_PROTO("RX pkt parsing failed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
- TRACE_LEAVE(QUIC_EV_CONN_LPKT);
- return -1;
-}
+ LIST_INIT(&qc->rx.pkt_list);
-/* Check if received packet <pkt> should be drop due to <qc> already in closing
- * state. This can be true if a CONNECTION_CLOSE has already been emitted for
- * this connection.
- *
- * Returns false if connection is not in closing state else true. The caller
- * should drop the whole datagram in the last case to not mess up <qc>
- * CONNECTION_CLOSE rate limit counter.
- */
-static int qc_rx_check_closing(struct quic_conn *qc,
- struct quic_rx_packet *pkt)
-{
- if (!(qc->flags & QUIC_FL_CONN_CLOSING))
- return 0;
+ qc->streams_by_id = EB_ROOT_UNIQUE;
- TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
+ /* Required to call free_quic_conn_cids() from quic_conn_release() */
+ qc->cids = EB_ROOT;
+ qc_init_fd(qc);
- /* Check if CONNECTION_CLOSE rate reemission is reached. */
- if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
- qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
- qc->nb_pkt_for_cc++;
- qc->nb_pkt_since_cc = 0;
- }
+ LIST_INIT(&qc->back_refs);
+ LIST_INIT(&qc->el_th_ctx);
- return 1;
-}
+ qc->wait_event.tasklet = NULL;
-/* React to a connection migration initiated on <qc> by a client with the new
- * path addresses <peer_addr>/<local_addr>.
- *
- * Returns 0 on success else non-zero.
- */
-static int qc_handle_conn_migration(struct quic_conn *qc,
- const struct sockaddr_storage *peer_addr,
- const struct sockaddr_storage *local_addr)
-{
- TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
+ /* Required to destroy <qc> tasks from quic_conn_release() */
+ qc->timer_task = NULL;
+ qc->idle_timer_task = NULL;
- /* RFC 9000. Connection Migration
- *
- * If the peer sent the disable_active_migration transport parameter,
- * an endpoint also MUST NOT send packets (including probing packets;
- * see Section 9.1) from a different local address to the address the peer
- * used during the handshake, unless the endpoint has acted on a
- * preferred_address transport parameter from the peer.
- */
- if (qc->li->bind_conf->quic_params.disable_active_migration) {
- TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
- goto err;
- }
+ qc->xprt_ctx = NULL;
+ qc->conn = NULL;
+ qc->qcc = NULL;
+ qc->app_ops = NULL;
+ qc->path = NULL;
- /* RFC 9000 9. Connection Migration
- *
- * The design of QUIC relies on endpoints retaining a stable address for
- * the duration of the handshake. An endpoint MUST NOT initiate
- * connection migration before the handshake is confirmed, as defined in
- * Section 4.1.2 of [QUIC-TLS].
+ /* Keyupdate: required to safely call quic_tls_ku_free() from
+ * quic_conn_release().
*/
- if (qc->state < QUIC_HS_ST_COMPLETE) {
- TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
- goto err;
- }
+ quic_tls_ku_reset(&qc->ku.prv_rx);
+ quic_tls_ku_reset(&qc->ku.nxt_rx);
+ quic_tls_ku_reset(&qc->ku.nxt_tx);
- /* RFC 9000 9. Connection Migration
- *
- * TODO
- * An endpoint MUST
- * perform path validation (Section 8.2) if it detects any change to a
- * peer's address, unless it has previously validated that address.
- */
+ /* Encryption levels */
+ qc->iel = qc->eel = qc->hel = qc->ael = NULL;
+ LIST_INIT(&qc->qel_list);
+ /* Packet number spaces */
+ qc->ipktns = qc->hpktns = qc->apktns = NULL;
+ LIST_INIT(&qc->pktns_list);
- /* Update quic-conn owned socket if in used.
- * TODO try to reuse it instead of closing and opening a new one.
- */
- if (qc_test_fd(qc)) {
- /* TODO try to reuse socket instead of closing it and opening a new one. */
- TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
- qc_release_fd(qc, 1);
- /* TODO need to adjust <jobs> on socket allocation failure. */
- qc_alloc_fd(qc, local_addr, peer_addr);
+ /* Required to safely call quic_conn_prx_cntrs_update() from quic_conn_release(). */
+ qc->prx_counters = NULL;
+
+ /* Now proceeds to allocation of qc members. */
+ qc->rx.buf.area = pool_alloc(pool_head_quic_conn_rxbuf);
+ if (!qc->rx.buf.area) {
+ TRACE_ERROR("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
+ goto err;
}
- qc->local_addr = *local_addr;
- qc->peer_addr = *peer_addr;
- qc->cntrs.conn_migration_done++;
+ /* QUIC Server (or listener). */
+ if (server) {
+ struct proxy *prx;
- TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
- return 0;
+ l = owner;
+ prx = l->bind_conf->frontend;
+ cc_algo = l->bind_conf->quic_cc_algo;
- err:
- TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
- return 1;
-}
+ qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
+ &quic_stats_module);
+ qc->flags = QUIC_FL_CONN_LISTENER;
+ qc->state = QUIC_HS_ST_SERVER_INITIAL;
+ /* Copy the client original DCID. */
+ qc->odcid = *dcid;
+ /* Copy the packet SCID to reuse it as DCID for sending */
+ qc->dcid = *scid;
+ qc->tx.buf = BUF_NULL;
+ qc->li = l;
+ }
+ /* QUIC Client (outgoing connection to servers) */
+ else {
+ qc->state = QUIC_HS_ST_CLIENT_INITIAL;
+ if (dcid->len)
+ memcpy(qc->dcid.data, dcid->data, dcid->len);
+ qc->dcid.len = dcid->len;
+ qc->li = NULL;
+ }
+ qc->mux_state = QC_MUX_NULL;
+ qc->err = quic_err_transport(QC_ERR_NO_ERROR);
-/* Release the memory for the RX packets which are no more referenced
- * and consume their payloads which have been copied to the RX buffer
- * for the connection.
- * Always succeeds.
- */
-static inline void quic_rx_pkts_del(struct quic_conn *qc)
-{
- struct quic_rx_packet *pkt, *pktback;
+ conn_id->qc = qc;
- list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
- TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
- "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
- (long long)pkt->pn_node.key,
- pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
- (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
- if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
- size_t cdata;
-
- cdata = b_contig_data(&qc->rx.buf, 0);
- TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
- "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
- if (cdata && !*b_head(&qc->rx.buf)) {
- /* Consume the remaining data */
- b_del(&qc->rx.buf, cdata);
- }
- break;
- }
+ if ((global.tune.options & GTUNE_QUIC_SOCK_PER_CONN) &&
+ is_addr(local_addr)) {
+ TRACE_USER("Allocate a socket for QUIC connection", QUIC_EV_CONN_INIT, qc);
+ qc_alloc_fd(qc, local_addr, peer_addr);
+
+ /* haproxy soft-stop is supported only for QUIC connections
+ * with their owned socket.
+ */
+ if (qc_test_fd(qc))
+ _HA_ATOMIC_INC(&jobs);
+ }
- if (pkt->refcnt)
- break;
+ /* Select our SCID which is the first CID with 0 as sequence number. */
+ qc->scid = conn_id->cid;
- b_del(&qc->rx.buf, pkt->raw_len);
- LIST_DELETE(&pkt->qc_rx_pkt_list);
- pool_free(pool_head_quic_rx_packet, pkt);
+ if (!qc_enc_level_alloc(qc, &qc->ipktns, &qc->iel, ssl_encryption_initial)) {
+ TRACE_ERROR("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
+ goto err;
}
- /* In frequent cases the buffer will be emptied at this stage. */
- b_realign_if_empty(&qc->rx.buf);
-}
+ qc->original_version = qv;
+ qc->negotiated_version = NULL;
+ qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
+ TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
+ TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
+ /* TX part. */
+ LIST_INIT(&qc->tx.frms_to_send);
+ qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
+ qc->tx.wbuf = qc->tx.rbuf = 0;
+ qc->tx.bytes = qc->tx.prep_bytes = 0;
+ memset(&qc->tx.params, 0, sizeof(qc->tx.params));
+ qc->tx.buf = BUF_NULL;
+ /* RX part. */
+ qc->rx.bytes = 0;
+ memset(&qc->rx.params, 0, sizeof(qc->rx.params));
+ qc->rx.buf = b_make(qc->rx.buf.area, QUIC_CONN_RX_BUFSZ, 0, 0);
+ for (i = 0; i < QCS_MAX_TYPES; i++)
+ qc->rx.strms[i].nb_streams = 0;
-/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
- * into <qc> receive buffer after header protection removal procedure.
- *
- * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
- * must point to packet buffer first byte.
- *
- * <tasklist_head> may be non-NULL when the caller treat several datagrams for
- * different quic-conn. In this case, each quic-conn tasklet will be appended
- * to it in order to be woken up after the current task.
- *
- * The caller can safely removed the packet data. If packet refcount was not
- * incremented by this function, it means that the connection did not handled
- * it and it should be freed by the caller.
- */
-static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
- struct quic_dgram *dgram, unsigned char *beg,
- struct list **tasklist_head)
-{
- const struct quic_version *qv = pkt->version;
- struct quic_enc_level *qel = NULL;
- size_t b_cspace;
+ qc->nb_pkt_for_cc = 1;
+ qc->nb_pkt_since_cc = 0;
- TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
- TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
-
- if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
- qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
- TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
- QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
- TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
- QUIC_EV_CONN_LPKT, qc);
- /* Reset the anti-amplification bit. It will be set again
- * when sending the next packet if reached again.
- */
- qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
- qc_set_timer(qc);
- if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
- task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
+ if (!quic_tls_ku_init(qc)) {
+ TRACE_ERROR("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
+ goto err;
}
- /* Drop asap packet whose packet number space is discarded. */
- if (quic_tls_pkt_type_pktns_dcd(qc, pkt->type)) {
- TRACE_PROTO("Discarded packet number space", QUIC_EV_CONN_TRMHP, qc);
- goto drop_silent;
- }
+ qc->max_ack_delay = 0;
+ /* Only one path at this time (multipath not supported) */
+ qc->path = &qc->paths[0];
+ quic_path_init(qc->path, ipv4, cc_algo ? cc_algo : default_quic_cc_algo, qc);
- if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
- TRACE_PROTO("Connection error",
- QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
- goto out;
- }
+ qc->stream_buf_count = 0;
+ memcpy(&qc->local_addr, local_addr, sizeof(qc->local_addr));
+ memcpy(&qc->peer_addr, peer_addr, sizeof qc->peer_addr);
- pkt->raw_len = pkt->len;
- quic_rx_pkts_del(qc);
- b_cspace = b_contig_space(&qc->rx.buf);
- if (b_cspace < pkt->len) {
- TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
- "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
- /* Do not consume buf if space not at the end. */
- if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
- TRACE_PROTO("Packet dropped",
- QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
- qc->cntrs.dropped_pkt_bufoverrun++;
- goto drop_silent;
- }
+ if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
+ conn_id->stateless_reset_token,
+ dcid->data, dcid->len,
+ qc->scid.data, qc->scid.len, token_odcid))
+ goto err;
- /* Let us consume the remaining contiguous space. */
- if (b_cspace) {
- b_putchr(&qc->rx.buf, 0x00);
- b_cspace--;
- }
- b_add(&qc->rx.buf, b_cspace);
- if (b_contig_space(&qc->rx.buf) < pkt->len) {
- TRACE_PROTO("Too big packet",
- QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
- qc->cntrs.dropped_pkt_bufoverrun++;
- goto drop_silent;
- }
+ /* Initialize the idle timeout of the connection at the "max_idle_timeout"
+ * value from local transport parameters.
+ */
+ qc->max_idle_timeout = qc->rx.params.max_idle_timeout;
+ qc->wait_event.tasklet = tasklet_new();
+ if (!qc->wait_event.tasklet) {
+ TRACE_ERROR("tasklet_new() failed", QUIC_EV_CONN_TXPKT);
+ goto err;
}
+ qc->wait_event.tasklet->process = quic_conn_io_cb;
+ qc->wait_event.tasklet->context = qc;
+ qc->wait_event.events = 0;
+ qc->subs = NULL;
- if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
- TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
- goto drop;
- }
+ if (qc_alloc_ssl_sock_ctx(qc) ||
+ !quic_conn_init_timer(qc) ||
+ !quic_conn_init_idle_timer_task(qc))
+ goto err;
- TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
- if (pkt->aad_len)
- qc_pkt_insert(qc, pkt, qel);
- out:
- *tasklist_head = tasklet_wakeup_after(*tasklist_head,
- qc->wait_event.tasklet);
+ if (!qc_new_isecs(qc, &qc->iel->tls_ctx, qc->original_version, dcid->data, dcid->len, 1))
+ goto err;
- drop_silent:
- TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
- TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
- return;
+ /* Counters initialization */
+ memset(&qc->cntrs, 0, sizeof qc->cntrs);
- drop:
- qc->cntrs.dropped_pkt++;
- TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
- TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
-}
+ LIST_APPEND(&th_ctx->quic_conns, &qc->el_th_ctx);
+ qc->qc_epoch = HA_ATOMIC_LOAD(&qc_epoch);
-/* This function builds into a buffer at <pos> position a QUIC long packet header,
- * <end> being one byte past the end of this buffer.
- * Return 1 if enough room to build this header, 0 if not.
- */
-static int quic_build_packet_long_header(unsigned char **pos, const unsigned char *end,
- int type, size_t pn_len,
- struct quic_conn *qc, const struct quic_version *ver)
-{
- int ret = 0;
+ TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
- TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
+ return qc;
- if (end - *pos < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
- TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
- goto leave;
- }
+ err:
+ quic_conn_release(qc);
+ TRACE_LEAVE(QUIC_EV_CONN_INIT);
+ return NULL;
+}
- type = quic_pkt_type(type, ver->num);
- /* #0 byte flags */
- *(*pos)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
- (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
- /* Version */
- quic_write_uint32(pos, end, ver->num);
- *(*pos)++ = qc->dcid.len;
- /* Destination connection ID */
- if (qc->dcid.len) {
- memcpy(*pos, qc->dcid.data, qc->dcid.len);
- *pos += qc->dcid.len;
- }
- /* Source connection ID */
- *(*pos)++ = qc->scid.len;
- if (qc->scid.len) {
- memcpy(*pos, qc->scid.data, qc->scid.len);
- *pos += qc->scid.len;
- }
+/* Update the proxy counters of <qc> QUIC connection from its counters */
+static inline void quic_conn_prx_cntrs_update(struct quic_conn *qc)
+{
+ if (!qc->prx_counters)
+ return;
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
- return ret;
+ HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt, qc->cntrs.dropped_pkt);
+ HA_ATOMIC_ADD(&qc->prx_counters->dropped_pkt_bufoverrun, qc->cntrs.dropped_pkt_bufoverrun);
+ HA_ATOMIC_ADD(&qc->prx_counters->dropped_parsing, qc->cntrs.dropped_parsing);
+ HA_ATOMIC_ADD(&qc->prx_counters->socket_full, qc->cntrs.socket_full);
+ HA_ATOMIC_ADD(&qc->prx_counters->sendto_err, qc->cntrs.sendto_err);
+ HA_ATOMIC_ADD(&qc->prx_counters->sendto_err_unknown, qc->cntrs.sendto_err_unknown);
+ HA_ATOMIC_ADD(&qc->prx_counters->sent_pkt, qc->cntrs.sent_pkt);
+ /* It is possible that ->path was not initialized. For instance if a
+ * QUIC connection allocation has failed.
+ */
+ if (qc->path)
+ HA_ATOMIC_ADD(&qc->prx_counters->lost_pkt, qc->path->loss.nb_lost_pkt);
+ HA_ATOMIC_ADD(&qc->prx_counters->conn_migration_done, qc->cntrs.conn_migration_done);
+ /* Stream related counters */
+ HA_ATOMIC_ADD(&qc->prx_counters->data_blocked, qc->cntrs.data_blocked);
+ HA_ATOMIC_ADD(&qc->prx_counters->stream_data_blocked, qc->cntrs.stream_data_blocked);
+ HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_bidi, qc->cntrs.streams_blocked_bidi);
+ HA_ATOMIC_ADD(&qc->prx_counters->streams_blocked_uni, qc->cntrs.streams_blocked_uni);
}
-/* This function builds into a buffer at <pos> position a QUIC short packet header,
- * <end> being one byte past the end of this buffer.
- * Return 1 if enough room to build this header, 0 if not.
+/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
+ * The connection tasklet is killed.
+ *
+ * This function must only be called by the thread responsible of the quic_conn
+ * tasklet.
*/
-static int quic_build_packet_short_header(unsigned char **pos, const unsigned char *end,
- size_t pn_len, struct quic_conn *qc,
- unsigned char tls_flags)
+void quic_conn_release(struct quic_conn *qc)
{
- int ret = 0;
- unsigned char spin_bit =
- (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
+ struct eb64_node *node;
+ struct quic_rx_packet *pkt, *pktback;
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+ TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
- if (end - *pos < 1 + qc->dcid.len) {
- TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
+ if (!qc)
goto leave;
- }
- /* #0 byte flags */
- *(*pos)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
- ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
- /* Destination connection ID */
- if (qc->dcid.len) {
- memcpy(*pos, qc->dcid.data, qc->dcid.len);
- *pos += qc->dcid.len;
- }
+ /* We must not free the quic-conn if the MUX is still allocated. */
+ BUG_ON(qc->mux_state == QC_MUX_READY);
- ret = 1;
- leave:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return ret;
-}
+ if (qc_test_fd(qc))
+ _HA_ATOMIC_DEC(&jobs);
-/* Apply QUIC header protection to the packet with <pos> as first byte address,
- * <pn> as address of the Packet number field, <pnlen> being this field length
- * with <aead> as AEAD cipher and <key> as secret key.
- *
- * TODO no error is expected as encryption is done in place but encryption
- * manual is unclear. <fail> will be set to true if an error is detected.
- */
-void quic_apply_header_protection(struct quic_conn *qc, unsigned char *pos,
- unsigned char *pn, size_t pnlen,
- struct quic_tls_ctx *tls_ctx, int *fail)
+ /* Close quic-conn socket fd. */
+ qc_release_fd(qc, 0);
-{
- int i;
- /* We need an IV of at least 5 bytes: one byte for bytes #0
- * and at most 4 bytes for the packet number
+ /* in the unlikely (but possible) case the connection was just added to
+ * the accept_list we must delete it from there.
*/
- unsigned char mask[5] = {0};
- EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
+ MT_LIST_DELETE(&qc->accept_list);
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+ /* free remaining stream descriptors */
+ node = eb64_first(&qc->streams_by_id);
+ while (node) {
+ struct qc_stream_desc *stream;
- *fail = 0;
+ stream = eb64_entry(node, struct qc_stream_desc, by_id);
+ node = eb64_next(node);
- if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
- TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
- *fail = 1;
- goto out;
+ /* all streams attached to the quic-conn are released, so
+ * qc_stream_desc_free will liberate the stream instance.
+ */
+ BUG_ON(!stream->release);
+ qc_stream_desc_free(stream, 1);
}
- *pos ^= mask[0] & (*pos & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
- for (i = 0; i < pnlen; i++)
- pn[i] ^= mask[i + 1];
+ /* Purge Rx packet list. */
+ list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
+ LIST_DELETE(&pkt->qc_rx_pkt_list);
+ pool_free(pool_head_quic_rx_packet, pkt);
+ }
- out:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
-}
+ task_destroy(qc->idle_timer_task);
+ qc->idle_timer_task = NULL;
-/* Prepare into <outlist> as most as possible ack-eliciting frame from their
- * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
- * with <room> as available room, and <*len> the packet Length field initialized
- * with the number of bytes already present in this buffer which must be taken
- * into an account for the Length packet field value. <headlen> is the number of
- * bytes already present in this packet before building frames.
- *
- * Update consequently <*len> to reflect the size of these frames built
- * by this function. Also attach these frames to <l> frame list.
- * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
- */
-static inline int qc_build_frms(struct list *outlist, struct list *inlist,
- size_t room, size_t *len, size_t headlen,
- struct quic_enc_level *qel,
- struct quic_conn *qc)
-{
- int ret;
- struct quic_frame *cf, *cfbak;
+ task_destroy(qc->timer_task);
+ qc->timer_task = NULL;
- TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
+ tasklet_free(qc->wait_event.tasklet);
- ret = 0;
- if (*len > room)
- goto leave;
+ /* remove the connection from receiver cids trees */
+ free_quic_conn_cids(qc);
- /* If we are not probing we must take into an account the congestion
- * control window.
- */
- if (!qel->pktns->tx.pto_probe) {
- size_t remain = quic_path_prep_data(qc->path);
+ /* free the SSL sock context */
+ qc_free_ssl_sock_ctx(&qc->xprt_ctx);
- if (headlen > remain)
- goto leave;
+ quic_tls_ku_free(qc);
+ if (qc->ael) {
+ struct quic_tls_ctx *actx = &qc->ael->tls_ctx;
- room = QUIC_MIN(room, remain - headlen);
+ /* Secrets used by keyupdate */
+ pool_free(pool_head_quic_tls_secret, actx->rx.secret);
+ pool_free(pool_head_quic_tls_secret, actx->tx.secret);
}
- TRACE_PROTO("TX frms build (headlen)",
- QUIC_EV_CONN_BCFRMS, qc, &headlen);
-
- /* NOTE: switch/case block inside a loop, a successful status must be
- * returned by this function only if at least one frame could be built
- * in the switch/case block.
- */
- list_for_each_entry_safe(cf, cfbak, inlist, list) {
- /* header length, data length, frame length. */
- size_t hlen, dlen, dlen_sz, avail_room, flen;
-
- if (!room)
- break;
-
- switch (cf->type) {
- case QUIC_FT_CRYPTO:
- TRACE_DEVEL(" New CRYPTO frame build (room, len)",
- QUIC_EV_CONN_BCFRMS, qc, &room, len);
- /* Compute the length of this CRYPTO frame header */
- hlen = 1 + quic_int_getsize(cf->crypto.offset);
- /* Compute the data length of this CRyPTO frame. */
- dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
- TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
- QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
- if (!dlen)
- continue;
-
- /* CRYPTO frame length. */
- flen = hlen + quic_int_getsize(dlen) + dlen;
- TRACE_DEVEL(" CRYPTO frame length (flen)",
- QUIC_EV_CONN_BCFRMS, qc, &flen);
- /* Add the CRYPTO data length and its encoded length to the packet
- * length and the length of this length.
- */
- *len += flen;
- room -= flen;
- if (dlen == cf->crypto.len) {
- /* <cf> CRYPTO data have been consumed. */
- LIST_DEL_INIT(&cf->list);
- LIST_APPEND(outlist, &cf->list);
- }
- else {
- struct quic_frame *new_cf;
-
- new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
- if (!new_cf) {
- TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
- continue;
- }
-
- new_cf->crypto.len = dlen;
- new_cf->crypto.offset = cf->crypto.offset;
- new_cf->crypto.qel = qel;
- TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
- if (cf->origin) {
- TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
- /* This <cf> frame was duplicated */
- LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
- new_cf->origin = cf->origin;
- /* Detach the remaining CRYPTO frame from its original frame */
- LIST_DEL_INIT(&cf->ref);
- cf->origin = NULL;
- }
- LIST_APPEND(outlist, &new_cf->list);
- /* Consume <dlen> bytes of the current frame. */
- cf->crypto.len -= dlen;
- cf->crypto.offset += dlen;
- }
- break;
-
- case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
- if (cf->stream.dup) {
- struct eb64_node *node = NULL;
- struct qc_stream_desc *stream_desc = NULL;
- struct qf_stream *strm_frm = &cf->stream;
-
- /* As this frame has been already lost, ensure the stream is always
- * available or the range of this frame is not consumed before
- * resending it.
- */
- node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
- if (!node) {
- TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
- qc_frm_free(qc, &cf);
- continue;
- }
-
- stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
- if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
- TRACE_DEVEL("ignored frame frame in already acked range",
- QUIC_EV_CONN_PRSAFRM, qc, cf);
- qc_frm_free(qc, &cf);
- continue;
- }
- else if (strm_frm->offset.key < stream_desc->ack_offset) {
- uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
+ qc_enc_level_free(qc, &qc->iel);
+ qc_enc_level_free(qc, &qc->eel);
+ qc_enc_level_free(qc, &qc->hel);
+ qc_enc_level_free(qc, &qc->ael);
- qc_stream_frm_mv_fwd(cf, diff);
- TRACE_DEVEL("updated partially acked frame",
- QUIC_EV_CONN_PRSAFRM, qc, cf);
- }
- }
- /* Note that these frames are accepted in short packets only without
- * "Length" packet field. Here, <*len> is used only to compute the
- * sum of the lengths of the already built frames for this packet.
- *
- * Compute the length of this STREAM frame "header" made a all the field
- * excepting the variable ones. Note that +1 is for the type of this frame.
- */
- hlen = 1 + quic_int_getsize(cf->stream.id) +
- ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
- /* Compute the data length of this STREAM frame. */
- avail_room = room - hlen - *len;
- if ((ssize_t)avail_room <= 0)
- continue;
-
- TRACE_DEVEL(" New STREAM frame build (room, len)",
- QUIC_EV_CONN_BCFRMS, qc, &room, len);
-
- /* hlen contains STREAM id and offset. Ensure there is
- * enough room for length field.
- */
- if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
- dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
- cf->stream.len);
- dlen_sz = quic_int_getsize(dlen);
- flen = hlen + dlen_sz + dlen;
- }
- else {
- dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
- flen = hlen + dlen;
- }
+ quic_tls_ctx_free(&qc->nictx);
- if (cf->stream.len && !dlen) {
- /* Only a small gap is left on buffer, not
- * enough to encode the STREAM data length.
- */
- continue;
- }
+ quic_pktns_release(qc, &qc->ipktns);
+ quic_pktns_release(qc, &qc->hpktns);
+ quic_pktns_release(qc, &qc->apktns);
- TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
- QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
- TRACE_DEVEL(" STREAM frame length (flen)",
- QUIC_EV_CONN_BCFRMS, qc, &flen);
- /* Add the STREAM data length and its encoded length to the packet
- * length and the length of this length.
- */
- *len += flen;
- room -= flen;
- if (dlen == cf->stream.len) {
- /* <cf> STREAM data have been consumed. */
- LIST_DEL_INIT(&cf->list);
- LIST_APPEND(outlist, &cf->list);
-
- /* Do not notify MUX on retransmission. */
- if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
- qcc_streams_sent_done(cf->stream.stream->ctx,
- cf->stream.len,
- cf->stream.offset.key);
- }
- }
- else {
- struct quic_frame *new_cf;
- struct buffer cf_buf;
+ qc_detach_th_ctx_list(qc, 0);
- new_cf = qc_frm_alloc(cf->type);
- if (!new_cf) {
- TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
- continue;
- }
+ quic_conn_prx_cntrs_update(qc);
+ pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
+ qc->rx.buf.area = NULL;
+ pool_free(pool_head_quic_conn, qc);
+ qc = NULL;
- new_cf->stream.stream = cf->stream.stream;
- new_cf->stream.buf = cf->stream.buf;
- new_cf->stream.id = cf->stream.id;
- new_cf->stream.offset = cf->stream.offset;
- new_cf->stream.len = dlen;
- new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
- /* FIN bit reset */
- new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
- new_cf->stream.data = cf->stream.data;
- new_cf->stream.dup = cf->stream.dup;
- TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
- if (cf->origin) {
- TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
- /* This <cf> frame was duplicated */
- LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
- new_cf->origin = cf->origin;
- /* Detach this STREAM frame from its origin */
- LIST_DEL_INIT(&cf->ref);
- cf->origin = NULL;
- }
- LIST_APPEND(outlist, &new_cf->list);
- cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
- /* Consume <dlen> bytes of the current frame. */
- cf_buf = b_make(b_orig(cf->stream.buf),
- b_size(cf->stream.buf),
- (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
- cf->stream.len -= dlen;
- cf->stream.offset.key += dlen;
- cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
-
- /* Do not notify MUX on retransmission. */
- if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
- qcc_streams_sent_done(new_cf->stream.stream->ctx,
- new_cf->stream.len,
- new_cf->stream.offset.key);
- }
- }
+ TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
+}
- /* TODO the MUX is notified about the frame sending via
- * previous qcc_streams_sent_done call. However, the
- * sending can fail later, for example if the sendto
- * system call returns an error. As the MUX has been
- * notified, the transport layer is responsible to
- * bufferize and resent the announced data later.
- */
-
- break;
-
- default:
- flen = qc_frm_len(cf);
- BUG_ON(!flen);
- if (flen > room)
- continue;
-
- *len += flen;
- room -= flen;
- LIST_DEL_INIT(&cf->list);
- LIST_APPEND(outlist, &cf->list);
- break;
- }
+/* Initialize the timer task of <qc> QUIC connection.
+ * Returns 1 if succeeded, 0 if not.
+ */
+static int quic_conn_init_timer(struct quic_conn *qc)
+{
+ int ret = 0;
+ /* Attach this task to the same thread ID used for the connection */
+ TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
- /* Successful status as soon as a frame could be built */
- ret = 1;
+ qc->timer_task = task_new_here();
+ if (!qc->timer_task) {
+ TRACE_ERROR("timer task allocation failed", QUIC_EV_CONN_NEW, qc);
+ goto leave;
}
+ qc->timer = TICK_ETERNITY;
+ qc->timer_task->process = qc_process_timer;
+ qc->timer_task->context = qc;
+
+ ret = 1;
leave:
- TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
+ TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
return ret;
}
-/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
- * is used as return parameter and should be zero'ed by the caller.
- */
-static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
- struct quic_frame *out)
+/* Rearm the idle timer or the ack timer (if not already armde) for <qc> QUIC
+ * connection. */
+void qc_idle_timer_do_rearm(struct quic_conn *qc, int arm_ack)
{
- /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
- *
- * A CONNECTION_CLOSE frame should be sent in several packets with
- * different encryption levels depending on the client context. This is
- * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
- * more details on how to implement it.
- */
- TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
-
-
- if (qc->err.app) {
- if (unlikely(qel == qc->iel || qel == qc->hel)) {
- /* RFC 9000 10.2.3. Immediate Close during the Handshake
- *
- * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
- * packet could expose application state or be used to alter application
- * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
- * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
- * Handshake packets. Otherwise, information about the application
- * state might be revealed. Endpoints MUST clear the value of the
- * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
- * converting to a CONNECTION_CLOSE of type 0x1c.
- */
- out->type = QUIC_FT_CONNECTION_CLOSE;
- out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
- out->connection_close.reason_phrase_len = 0;
+ unsigned int expire;
+
+ if (stopping && qc->flags & (QUIC_FL_CONN_CLOSING|QUIC_FL_CONN_DRAINING)) {
+ TRACE_PROTO("executing idle timer immediately on stopping", QUIC_EV_CONN_IDLE_TIMER, qc);
+ qc->ack_expire = TICK_ETERNITY;
+ task_wakeup(qc->idle_timer_task, TASK_WOKEN_MSG);
+ }
+ else {
+ expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
+ qc->idle_expire = tick_add(now_ms, MS_TO_TICKS(expire));
+ if (arm_ack) {
+ /* Arm the ack timer only if not already armed. */
+ if (!tick_isset(qc->ack_expire)) {
+ qc->ack_expire = tick_add(now_ms, MS_TO_TICKS(QUIC_ACK_DELAY));
+ qc->idle_timer_task->expire = qc->ack_expire;
+ task_queue(qc->idle_timer_task);
+ TRACE_PROTO("ack timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
+ }
}
else {
- out->type = QUIC_FT_CONNECTION_CLOSE_APP;
- out->connection_close.error_code = qc->err.code;
+ qc->idle_timer_task->expire = tick_first(qc->ack_expire, qc->idle_expire);
+ task_queue(qc->idle_timer_task);
+ TRACE_PROTO("idle timer armed", QUIC_EV_CONN_IDLE_TIMER, qc);
}
}
- else {
- out->type = QUIC_FT_CONNECTION_CLOSE;
- out->connection_close.error_code = qc->err.code;
- }
- TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
-
}
-/* This function builds a clear packet from <pkt> information (its type)
- * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
- * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
- * filling the buffer with as much frames as possible from <frms> list of
- * prebuilt frames.
- * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
- * reserved so that to ensure there is enough room to build this AEAD TAG after
- * having returned from this function.
- * This function also updates the value of <buf_pn> pointer to point to the packet
- * number field in this packet. <pn_len> will also have the packet number
- * length as value.
- *
- * Return 1 if succeeded (enough room to buile this packet), O if not.
+/* Rearm the idle timer or ack timer for <qc> QUIC connection depending on <read>
+ * and <arm_ack> booleans. The former is set to 1 when receiving a packet ,
+ * and 0 when sending packet. <arm_ack> is set to 1 if this is the ack timer
+ * which must be rearmed.
*/
-static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
- size_t dglen, struct quic_tx_packet *pkt,
- int64_t pn, size_t *pn_len, unsigned char **buf_pn,
- int must_ack, int padding, int cc, int probe,
- struct quic_enc_level *qel, struct quic_conn *qc,
- const struct quic_version *ver, struct list *frms)
+void qc_idle_timer_rearm(struct quic_conn *qc, int read, int arm_ack)
{
- unsigned char *beg, *payload;
- size_t len, len_sz, len_frms, padding_len;
- struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
- struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
- struct quic_frame cc_frm = { };
- size_t ack_frm_len, head_len;
- int64_t rx_largest_acked_pn;
- int add_ping_frm;
- struct list frm_list = LIST_HEAD_INIT(frm_list);
- struct quic_frame *cf;
- int ret = 0;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
-
- /* Length field value with CRYPTO frames if present. */
- len_frms = 0;
- beg = pos;
- /* When not probing, and no immediate close is required, reduce the size of this
- * buffer to respect the congestion controller window.
- * This size will be limited if we have ack-eliciting frames to send from <frms>.
- */
- if (!probe && !LIST_ISEMPTY(frms) && !cc) {
- size_t path_room;
+ TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
- path_room = quic_path_prep_data(qc->path);
- if (end - beg > path_room)
- end = beg + path_room;
+ if (read) {
+ qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
}
-
- /* Ensure there is enough room for the TLS encryption tag and a zero token
- * length field if any.
- */
- if (end - pos < QUIC_TLS_TAG_LEN +
- (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
- goto no_room;
-
- end -= QUIC_TLS_TAG_LEN;
- rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
- /* packet number length */
- *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
- /* Build the header */
- if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
- !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
- (pkt->type != QUIC_PACKET_TYPE_SHORT &&
- !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
- goto no_room;
-
- /* Encode the token length (0) for an Initial packet. */
- if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
- if (end <= pos)
- goto no_room;
-
- *pos++ = 0;
+ else {
+ qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
}
+ qc_idle_timer_do_rearm(qc, arm_ack);
- head_len = pos - beg;
- /* Build an ACK frame if required. */
- ack_frm_len = 0;
- /* Do not ack and probe at the same time. */
- if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
- struct quic_arngs *arngs = &qel->pktns->rx.arngs;
- BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
- ack_frm.tx_ack.arngs = arngs;
- if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
- qel->pktns->tx.ack_delay =
- quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
- qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
- }
- ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
- /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
- * smallest frame (PING) and <*pn_len> more for the packet number. Note
- * that from here, we do not know if we will have to send a PING frame.
- * This will be decided after having computed the ack-eliciting frames
- * to be added to this packet.
- */
- if (end - pos <= 1 + *pn_len)
- goto no_room;
+ TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
+}
- ack_frm_len = qc_frm_len(&ack_frm);
- if (ack_frm_len > end - 1 - *pn_len - pos)
- goto no_room;
- }
+/* The task handling the idle timeout */
+struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
+{
+ struct quic_conn *qc = ctx;
+ struct quic_counters *prx_counters = qc->prx_counters;
+ unsigned int qc_flags = qc->flags;
- /* Length field value without the ack-eliciting frames. */
- len = ack_frm_len + *pn_len;
- len_frms = 0;
- if (!cc && !LIST_ISEMPTY(frms)) {
- ssize_t room = end - pos;
+ TRACE_ENTER(QUIC_EV_CONN_IDLE_TIMER, qc);
- TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
- /* Initialize the length of the frames built below to <len>.
- * If any frame could be successfully built by qc_build_frms(),
- * we will have len_frms > len.
- */
- len_frms = len;
- if (!qc_build_frms(&frm_list, frms,
- end - pos, &len_frms, pos - beg, qel, qc)) {
- TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
- qc, NULL, NULL, &room);
- if (!ack_frm_len && !qel->pktns->tx.pto_probe)
- goto no_room;
- }
- }
+ if ((state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER && !tick_is_expired(t->expire, now_ms))
+ goto requeue;
- /* Length (of the remaining data). Must not fail because, the buffer size
- * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
- * for the encryption tag. It must be taken into an account for the length
- * of this packet.
- */
- if (len_frms)
- len = len_frms + QUIC_TLS_TAG_LEN;
- else
- len += QUIC_TLS_TAG_LEN;
- /* CONNECTION_CLOSE frame */
- if (cc) {
- qc_build_cc_frm(qc, qel, &cc_frm);
- len += qc_frm_len(&cc_frm);
- }
- add_ping_frm = 0;
- padding_len = 0;
- len_sz = quic_int_getsize(len);
- /* Add this packet size to <dglen> */
- dglen += head_len + len_sz + len;
- /* Note that <padding> is true only when building an Handshake packet
- * coalesced to an Initial packet.
- */
- if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
- /* This is a maximum padding size */
- padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
- /* The length field value is of this packet is <len> + <padding_len>
- * the size of which may be greater than the initial computed size
- * <len_sz>. So, let's deduce the difference between these to packet
- * sizes from <padding_len>.
+ if (tick_is_expired(qc->ack_expire, now_ms)) {
+ TRACE_PROTO("ack timer expired", QUIC_EV_CONN_IDLE_TIMER, qc);
+ qc->ack_expire = TICK_ETERNITY;
+ /* Note that ->idle_expire is always set. */
+ t->expire = qc->idle_expire;
+ /* Do not wakeup the I/O handler in DRAINING state or if the
+ * connection must be killed as soon as possible.
*/
- padding_len -= quic_int_getsize(len + padding_len) - len_sz;
- len += padding_len;
- }
- else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
- len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
- }
- else if (LIST_ISEMPTY(&frm_list)) {
- if (qel->pktns->tx.pto_probe) {
- /* If we cannot send a frame, we send a PING frame. */
- add_ping_frm = 1;
- len += 1;
- dglen += 1;
- /* Note that only we are in the case where this Initial packet
- * is not coalesced to an Handshake packet. We must directly
- * pad the datragram.
- */
- if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
- if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
- padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
- padding_len -= quic_int_getsize(len + padding_len) - len_sz;
- len += padding_len;
- }
- }
- else {
- /* Note that +1 is for the PING frame */
- if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
- len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
- }
- }
- else {
- /* If there is no frame at all to follow, add at least a PADDING frame. */
- if (!ack_frm_len && !cc)
- len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
+ if (!(qc->flags & (QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_TO_KILL))) {
+ qc->flags |= QUIC_FL_CONN_ACK_TIMER_FIRED;
+ tasklet_wakeup(qc->wait_event.tasklet);
}
- }
-
- if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
- goto no_room;
- /* Packet number field address. */
- *buf_pn = pos;
-
- /* Packet number encoding. */
- if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
- goto no_room;
+ goto requeue;
+ }
- /* payload building (ack-eliciting or not frames) */
- payload = pos;
- if (ack_frm_len) {
- if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
- goto no_room;
+ TRACE_PROTO("idle timer task running", QUIC_EV_CONN_IDLE_TIMER, qc);
+ /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
+ * might free the quic-conn too early via quic_close().
+ */
+ qc_notify_err(qc);
- pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
- pkt->flags |= QUIC_FL_TX_PACKET_ACK;
+ /* If the MUX is still alive, keep the quic-conn. The MUX is
+ * responsible to call quic_close to release it.
+ */
+ qc->flags |= QUIC_FL_CONN_EXP_TIMER;
+ if (qc->mux_state != QC_MUX_READY) {
+ quic_conn_release(qc);
+ qc = NULL;
}
- /* Ack-eliciting frames */
- if (!LIST_ISEMPTY(&frm_list)) {
- struct quic_frame *tmp_cf;
- list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
- if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
- ssize_t room = end - pos;
- TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
- qc, NULL, NULL, &room);
- /* Note that <cf> was added from <frms> to <frm_list> list by
- * qc_build_frms().
- */
- LIST_DEL_INIT(&cf->list);
- LIST_INSERT(frms, &cf->list);
- continue;
- }
+ /* TODO if the quic-conn cannot be freed because of the MUX, we may at
+ * least clean some parts of it such as the tasklet.
+ */
- quic_tx_packet_refinc(pkt);
- cf->pkt = pkt;
- }
+ if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
+ qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
+ TRACE_DEVEL("dec half open counter", QUIC_EV_CONN_IDLE_TIMER, qc);
+ HA_ATOMIC_DEC(&prx_counters->half_open_conn);
}
- /* Build a PING frame if needed. */
- if (add_ping_frm) {
- frm.type = QUIC_FT_PING;
- if (!qc_build_frm(&pos, end, &frm, pkt, qc))
- goto no_room;
- }
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
+ return NULL;
- /* Build a CONNECTION_CLOSE frame if needed. */
- if (cc) {
- if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
- goto no_room;
+ requeue:
+ TRACE_LEAVE(QUIC_EV_CONN_IDLE_TIMER, qc);
+ return t;
+}
- pkt->flags |= QUIC_FL_TX_PACKET_CC;
- }
+/* Initialize the idle timeout task for <qc>.
+ * Returns 1 if succeeded, 0 if not.
+ */
+static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
+{
+ int ret = 0;
- /* Build a PADDING frame if needed. */
- if (padding_len) {
- frm.type = QUIC_FT_PADDING;
- frm.padding.len = padding_len;
- if (!qc_build_frm(&pos, end, &frm, pkt, qc))
- goto no_room;
- }
+ TRACE_ENTER(QUIC_EV_CONN_NEW, qc);
- if (pos == payload) {
- /* No payload was built because of congestion control */
- TRACE_PROTO("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
- goto no_room;
+ qc->idle_timer_task = task_new_here();
+ if (!qc->idle_timer_task) {
+ TRACE_ERROR("Idle timer task allocation failed", QUIC_EV_CONN_NEW, qc);
+ goto leave;
}
- /* If this packet is ack-eliciting and we are probing let's
- * decrement the PTO probe counter.
- */
- if ((pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
- qel->pktns->tx.pto_probe)
- qel->pktns->tx.pto_probe--;
-
- pkt->len = pos - beg;
- LIST_SPLICE(&pkt->frms, &frm_list);
+ qc->idle_timer_task->process = qc_idle_timer_task;
+ qc->idle_timer_task->context = qc;
+ qc->ack_expire = TICK_ETERNITY;
+ qc_idle_timer_rearm(qc, 1, 0);
+ task_queue(qc->idle_timer_task);
ret = 1;
- TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
leave:
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
return ret;
-
- no_room:
- /* Replace the pre-built frames which could not be add to this packet */
- LIST_SPLICE(frms, &frm_list);
- TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
- goto leave;
}
-static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
-{
- pkt->type = type;
- pkt->len = 0;
- pkt->in_flight_len = 0;
- pkt->pn_node.key = (uint64_t)-1;
- LIST_INIT(&pkt->frms);
- pkt->time_sent = TICK_ETERNITY;
- pkt->next = NULL;
- pkt->prev = NULL;
- pkt->largest_acked_pn = -1;
- pkt->flags = 0;
- pkt->refcnt = 0;
-}
-
-/* Build a packet into a buffer at <pos> position, <end> pointing to one byte past
- * the end of this buffer, with <pkt_type> as packet type for <qc> QUIC connection
- * at <qel> encryption level with <frms> list of prebuilt frames.
- *
- * Return -2 if the packet could not be allocated or encrypted for any reason,
- * -1 if there was not enough room to build a packet.
- * XXX NOTE XXX
- * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
- * possible (to fill an MTU), the unique reason why this function may fail is the congestion
- * control window limitation.
+/* Return the QUIC version (quic_version struct) with <version> as version number
+ * if supported or NULL if not.
*/
-static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
- const unsigned char *end,
- struct quic_enc_level *qel,
- struct quic_tls_ctx *tls_ctx, struct list *frms,
- struct quic_conn *qc, const struct quic_version *ver,
- size_t dglen, int pkt_type, int must_ack,
- int padding, int probe, int cc, int *err)
+const struct quic_version *qc_supported_version(uint32_t version)
{
- struct quic_tx_packet *ret_pkt = NULL;
- /* The pointer to the packet number field. */
- unsigned char *buf_pn;
- unsigned char *first_byte, *last_byte, *payload;
- int64_t pn;
- size_t pn_len, payload_len, aad_len;
- struct quic_tx_packet *pkt;
- int encrypt_failure = 0;
-
- TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
- TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
- *err = 0;
- pkt = pool_alloc(pool_head_quic_tx_packet);
- if (!pkt) {
- TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
- *err = -2;
- goto err;
- }
-
- quic_tx_packet_init(pkt, pkt_type);
- first_byte = *pos;
- pn_len = 0;
- buf_pn = NULL;
-
- pn = qel->pktns->tx.next_pn + 1;
- if (!qc_do_build_pkt(*pos, end, dglen, pkt, pn, &pn_len, &buf_pn,
- must_ack, padding, cc, probe, qel, qc, ver, frms)) {
- // trace already emitted by function above
- *err = -1;
- goto err;
- }
-
- last_byte = first_byte + pkt->len;
- payload = buf_pn + pn_len;
- payload_len = last_byte - payload;
- aad_len = payload - first_byte;
-
- quic_packet_encrypt(payload, payload_len, first_byte, aad_len, pn, tls_ctx, qc, &encrypt_failure);
- if (encrypt_failure) {
- /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
- WARN_ON("quic_packet_encrypt failure");
- *err = -2;
- goto err;
- }
-
- last_byte += QUIC_TLS_TAG_LEN;
- pkt->len += QUIC_TLS_TAG_LEN;
- quic_apply_header_protection(qc, first_byte, buf_pn, pn_len, tls_ctx, &encrypt_failure);
- if (encrypt_failure) {
- /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
- WARN_ON("quic_apply_header_protection failure");
- *err = -2;
- goto err;
- }
-
- /* Consume a packet number */
- qel->pktns->tx.next_pn++;
- qc->tx.prep_bytes += pkt->len;
- if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
- qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
- TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
- }
-
- /* Now that a correct packet is built, let us consume <*pos> buffer. */
- *pos = last_byte;
- /* Attach the built packet to its tree. */
- pkt->pn_node.key = pn;
- /* Set the packet in fligth length for in flight packet only. */
- if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
- pkt->in_flight_len = pkt->len;
- qc->path->prep_in_flight += pkt->len;
- }
- /* Always reset this flag */
- qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
- if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
- qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
- qel->pktns->rx.nb_aepkts_since_last_ack = 0;
- qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
- if (tick_isset(qc->ack_expire)) {
- qc->ack_expire = TICK_ETERNITY;
- qc->idle_timer_task->expire = qc->idle_expire;
- task_queue(qc->idle_timer_task);
- TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
- }
- }
+ int i;
- pkt->pktns = qel->pktns;
+ if (unlikely(!version))
+ return &quic_version_VN_reserved;
- ret_pkt = pkt;
- leave:
- TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
- TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
- return ret_pkt;
+ for (i = 0; i < quic_versions_nb; i++)
+ if (quic_versions[i].num == version)
+ return &quic_versions[i];
- err:
- /* TODO: what about the frames which have been built
- * for this packet.
- */
- free_quic_tx_packet(qc, pkt);
- goto leave;
+ return NULL;
}
-
static void __quic_conn_init(void)
{
ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
}
REGISTER_POST_DEINIT(__quic_conn_deinit);
-/* Handle a new <dgram> received. Parse each QUIC packets and copied their
- * content to a quic-conn instance. The datagram content can be released after
- * this function.
- *
- * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
- * to the connection instance. <li> is the attached listener. The caller is
- * responsible to ensure that the first packet is destined to this connection
- * by comparing CIDs.
- *
- * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
- * function will thus retrieve the connection from the CID tree or allocate a
- * new one if possible. <li> is the listener attached to the receiver.
- *
- * Returns 0 on success else non-zero. If an error happens, some packets from
- * the datagram may not have been parsed.
- */
-int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
- struct listener *li)
-{
- struct quic_rx_packet *pkt;
- struct quic_conn *qc = NULL;
- unsigned char *pos, *end;
- struct list *tasklist_head = NULL;
-
- TRACE_ENTER(QUIC_EV_CONN_LPKT);
-
- pos = dgram->buf;
- end = pos + dgram->len;
- do {
- pkt = pool_alloc(pool_head_quic_rx_packet);
- if (!pkt) {
- TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
- goto err;
- }
-
- LIST_INIT(&pkt->qc_rx_pkt_list);
- pkt->version = NULL;
- pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
- pkt->pn_offset = 0;
- pkt->len = 0;
- pkt->raw_len = 0;
- pkt->token = NULL;
- pkt->token_len = 0;
- pkt->aad_len = 0;
- pkt->data = NULL;
- pkt->pn_node.key = (uint64_t)-1;
- pkt->refcnt = 0;
- pkt->flags = 0;
- pkt->time_received = now_ms;
-
- /* Set flag if pkt is the first one in dgram. */
- if (pos == dgram->buf)
- pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
-
- quic_rx_packet_refinc(pkt);
- if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
- goto next;
-
- /* Search quic-conn instance for first packet of the datagram.
- * quic_rx_packet_parse() is responsible to discard packets
- * with different DCID as the first one in the same datagram.
- */
- if (!qc) {
- int new_tid = -1;
-
- qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid);
- /* qc is NULL if receiving a non Initial packet for an
- * unknown connection or on connection affinity rebind.
- */
- if (!qc) {
- if (new_tid >= 0) {
- MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams,
- &dgram->handler_list);
- tasklet_wakeup(quic_dghdlrs[new_tid].task);
- goto out;
- }
-
- /* Skip the entire datagram. */
- pkt->len = end - pos;
- goto next;
- }
-
- dgram->qc = qc;
- }
-
- if (qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED)
- qc_finalize_affinity_rebind(qc);
-
- if (qc_rx_check_closing(qc, pkt)) {
- /* Skip the entire datagram. */
- pkt->len = end - pos;
- goto next;
- }
-
- /* Detect QUIC connection migration. */
- if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
- if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
- /* Skip the entire datagram. */
- TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
- pkt->len = end - pos;
- goto next;
- }
- }
-
- qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
-
- next:
- pos += pkt->len;
- quic_rx_packet_refdec(pkt);
-
- /* Free rejected packets */
- if (!pkt->refcnt) {
- BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
- pool_free(pool_head_quic_rx_packet, pkt);
- }
- } while (pos < end);
-
- /* Increasing the received bytes counter by the UDP datagram length
- * if this datagram could be associated to a connection.
- */
- if (dgram->qc)
- dgram->qc->rx.bytes += dgram->len;
-
- /* This must never happen. */
- BUG_ON(pos > end);
- BUG_ON(pos < end || pos > dgram->buf + dgram->len);
- /* Mark this datagram as consumed */
- HA_ATOMIC_STORE(&dgram->buf, NULL);
-
- out:
- TRACE_LEAVE(QUIC_EV_CONN_LPKT);
- return 0;
-
- err:
- /* Mark this datagram as consumed as maybe at least some packets were parsed. */
- HA_ATOMIC_STORE(&dgram->buf, NULL);
- TRACE_LEAVE(QUIC_EV_CONN_LPKT);
- return -1;
-}
-
/* Check if connection ID <dcid> of length <dcid_len> belongs to <qc> local
* CIDs. This can be used to determine if a datagram is addressed to the right
* connection instance.
TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
}
-/* Wake-up upper layer for sending if all conditions are met :
- * - room in congestion window or probe packet to sent
- * - socket FD ready to sent or listener socket used
- *
- * Returns 1 if upper layer has been woken up else 0.
- */
-int qc_notify_send(struct quic_conn *qc)
-{
- const struct quic_pktns *pktns = qc->apktns;
-
- if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
- /* RFC 9002 7.5. Probe Timeout
- *
- * Probe packets MUST NOT be blocked by the congestion controller.
- */
- if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
- (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
- tasklet_wakeup(qc->subs->tasklet);
- qc->subs->events &= ~SUB_RETRY_SEND;
- if (!qc->subs->events)
- qc->subs = NULL;
-
- return 1;
- }
- }
-
- return 0;
-}
-
/* Move a <qc> QUIC connection and its resources from the current thread to the
* new one <new_tid> optionally in association with <new_li> (since it may need
* to change when migrating to a thread from a different group, otherwise leave
--- /dev/null
+/*
+ * QUIC protocol implementation. Lower layer with internal features implemented
+ * here such as QUIC encryption, idle timeout, acknowledgement and
+ * retransmission.
+ *
+ * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ */
+
+#include <haproxy/quic_rx.h>
+
+#include <haproxy/h3.h>
+#include <haproxy/list.h>
+#include <haproxy/ncbuf.h>
+#include <haproxy/proto_quic.h>
+#include <haproxy/quic_ack.h>
+#include <haproxy/quic_sock.h>
+#include <haproxy/quic_stream.h>
+#include <haproxy/quic_ssl.h>
+#include <haproxy/quic_tls.h>
+#include <haproxy/quic_tx.h>
+#include <haproxy/trace.h>
+
+#define TRACE_SOURCE &trace_quic
+
+DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
+DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
+DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet", sizeof(struct quic_rx_packet));
+
+/* Decode an expected packet number from <truncated_on> its truncated value,
+ * depending on <largest_pn> the largest received packet number, and <pn_nbits>
+ * the number of bits used to encode this packet number (its length in bytes * 8).
+ * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
+ */
+static uint64_t decode_packet_number(uint64_t largest_pn,
+ uint32_t truncated_pn, unsigned int pn_nbits)
+{
+ uint64_t expected_pn = largest_pn + 1;
+ uint64_t pn_win = (uint64_t)1 << pn_nbits;
+ uint64_t pn_hwin = pn_win / 2;
+ uint64_t pn_mask = pn_win - 1;
+ uint64_t candidate_pn;
+
+
+ candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
+ /* Note that <pn_win> > <pn_hwin>. */
+ if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
+ candidate_pn + pn_hwin <= expected_pn)
+ return candidate_pn + pn_win;
+
+ if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
+ return candidate_pn - pn_win;
+
+ return candidate_pn;
+}
+
+/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
+ * cryptographic context.
+ * <largest_pn> is the largest received packet number and <pn> the address of
+ * the packet number field for this packet with <byte0> address of its first byte.
+ * <end> points to one byte past the end of this packet.
+ * Returns 1 if succeeded, 0 if not.
+ */
+static int qc_do_rm_hp(struct quic_conn *qc,
+ struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
+ int64_t largest_pn, unsigned char *pn, unsigned char *byte0)
+{
+ int ret, i, pnlen;
+ uint64_t packet_number;
+ uint32_t truncated_pn = 0;
+ unsigned char mask[5] = {0};
+ unsigned char *sample;
+
+ TRACE_ENTER(QUIC_EV_CONN_RMHP, qc);
+
+ ret = 0;
+
+ /* Check there is enough data in this packet. */
+ if (pkt->len - (pn - byte0) < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
+ TRACE_PROTO("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
+ goto leave;
+ }
+
+ sample = pn + QUIC_PACKET_PN_MAXLEN;
+
+ if (!quic_tls_aes_decrypt(mask, sample, sizeof mask, tls_ctx->rx.hp_ctx)) {
+ TRACE_ERROR("HP removing failed", QUIC_EV_CONN_RMHP, qc, pkt);
+ goto leave;
+ }
+
+ *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
+ pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
+ for (i = 0; i < pnlen; i++) {
+ pn[i] ^= mask[i + 1];
+ truncated_pn = (truncated_pn << 8) | pn[i];
+ }
+
+ packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
+ /* Store remaining information for this unprotected header */
+ pkt->pn = packet_number;
+ pkt->pnl = pnlen;
+
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_RMHP, qc);
+ return ret;
+}
+
+/* Decrypt <pkt> packet using encryption level <qel> for <qc> connection.
+ * Decryption is done in place in packet buffer.
+ *
+ * Returns 1 on success else 0.
+ */
+static int qc_pkt_decrypt(struct quic_conn *qc, struct quic_enc_level *qel,
+ struct quic_rx_packet *pkt)
+{
+ int ret, kp_changed;
+ unsigned char iv[QUIC_TLS_IV_LEN];
+ struct quic_tls_ctx *tls_ctx =
+ qc_select_tls_ctx(qc, qel, pkt->type, pkt->version);
+ EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
+ unsigned char *rx_iv = tls_ctx->rx.iv;
+ size_t rx_iv_sz = tls_ctx->rx.ivlen;
+ unsigned char *rx_key = tls_ctx->rx.key;
+
+ TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
+
+ ret = 0;
+ kp_changed = 0;
+
+ if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
+ /* The two tested bits are not at the same position,
+ * this is why they are first both inversed.
+ */
+ if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
+ if (pkt->pn < tls_ctx->rx.pn) {
+ /* The lowest packet number of a previous key phase
+ * cannot be null if it really stores previous key phase
+ * secrets.
+ */
+ // TODO: check if BUG_ON() more suitable
+ if (!qc->ku.prv_rx.pn) {
+ TRACE_ERROR("null previous packet number", QUIC_EV_CONN_RXPKT, qc);
+ goto leave;
+ }
+
+ rx_ctx = qc->ku.prv_rx.ctx;
+ rx_iv = qc->ku.prv_rx.iv;
+ rx_key = qc->ku.prv_rx.key;
+ }
+ else if (pkt->pn > qel->pktns->rx.largest_pn) {
+ /* Next key phase */
+ TRACE_PROTO("Key phase changed", QUIC_EV_CONN_RXPKT, qc);
+ kp_changed = 1;
+ rx_ctx = qc->ku.nxt_rx.ctx;
+ rx_iv = qc->ku.nxt_rx.iv;
+ rx_key = qc->ku.nxt_rx.key;
+ }
+ }
+ }
+
+ quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn);
+
+ ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
+ pkt->data, pkt->aad_len,
+ rx_ctx, tls_ctx->rx.aead, rx_key, iv);
+ if (!ret) {
+ TRACE_ERROR("quic_tls_decrypt() failed", QUIC_EV_CONN_RXPKT, qc);
+ goto leave;
+ }
+
+ /* Update the keys only if the packet decryption succeeded. */
+ if (kp_changed) {
+ quic_tls_rotate_keys(qc);
+ /* Toggle the Key Phase bit */
+ tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
+ /* Store the lowest packet number received for the current key phase */
+ tls_ctx->rx.pn = pkt->pn;
+ /* Prepare the next key update */
+ if (!quic_tls_key_update(qc)) {
+ TRACE_ERROR("quic_tls_key_update() failed", QUIC_EV_CONN_RXPKT, qc);
+ goto leave;
+ }
+ }
+
+ /* Update the packet length (required to parse the frames). */
+ pkt->len -= QUIC_TLS_TAG_LEN;
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
+ return ret;
+}
+
+/* Remove from <stream> the acknowledged frames.
+ *
+ * Returns 1 if at least one frame was removed else 0.
+ */
+static int quic_stream_try_to_consume(struct quic_conn *qc,
+ struct qc_stream_desc *stream)
+{
+ int ret;
+ struct eb64_node *frm_node;
+
+ TRACE_ENTER(QUIC_EV_CONN_ACKSTRM, qc);
+
+ ret = 0;
+ frm_node = eb64_first(&stream->acked_frms);
+ while (frm_node) {
+ struct qf_stream *strm_frm;
+ struct quic_frame *frm;
+ size_t offset, len;
+
+ strm_frm = eb64_entry(frm_node, struct qf_stream, offset);
+ offset = strm_frm->offset.key;
+ len = strm_frm->len;
+
+ if (offset > stream->ack_offset)
+ break;
+
+ if (qc_stream_desc_ack(&stream, offset, len)) {
+ /* cf. next comment : frame may be freed at this stage. */
+ TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
+ qc, stream ? strm_frm : NULL, stream);
+ ret = 1;
+ }
+
+ /* If stream is NULL after qc_stream_desc_ack(), it means frame
+ * has been freed. with the stream frames tree. Nothing to do
+ * anymore in here.
+ */
+ if (!stream) {
+ qc_check_close_on_released_mux(qc);
+ ret = 1;
+ goto leave;
+ }
+
+ frm_node = eb64_next(frm_node);
+ eb64_delete(&strm_frm->offset);
+
+ frm = container_of(strm_frm, struct quic_frame, stream);
+ qc_release_frm(qc, frm);
+ }
+
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_ACKSTRM, qc);
+ return ret;
+}
+
+/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
+static void qc_treat_acked_tx_frm(struct quic_conn *qc, struct quic_frame *frm)
+{
+ TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+ TRACE_PROTO("RX ack TX frm", QUIC_EV_CONN_PRSAFRM, qc, frm);
+
+ switch (frm->type) {
+ case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
+ {
+ struct qf_stream *strm_frm = &frm->stream;
+ struct eb64_node *node = NULL;
+ struct qc_stream_desc *stream = NULL;
+ const size_t offset = strm_frm->offset.key;
+ const size_t len = strm_frm->len;
+
+ /* do not use strm_frm->stream as the qc_stream_desc instance
+ * might be freed at this stage. Use the id to do a proper
+ * lookup.
+ *
+ * TODO if lookup operation impact on the perf is noticeable,
+ * implement a refcount on qc_stream_desc instances.
+ */
+ node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
+ if (!node) {
+ TRACE_DEVEL("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
+ qc_release_frm(qc, frm);
+ /* early return */
+ goto leave;
+ }
+ stream = eb64_entry(node, struct qc_stream_desc, by_id);
+
+ TRACE_DEVEL("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
+ if (offset <= stream->ack_offset) {
+ if (qc_stream_desc_ack(&stream, offset, len)) {
+ TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
+ qc, strm_frm, stream);
+ }
+
+ if (!stream) {
+ /* no need to continue if stream freed. */
+ TRACE_DEVEL("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
+ qc_release_frm(qc, frm);
+ qc_check_close_on_released_mux(qc);
+ break;
+ }
+
+ TRACE_DEVEL("stream consumed", QUIC_EV_CONN_ACKSTRM,
+ qc, strm_frm, stream);
+ qc_release_frm(qc, frm);
+ }
+ else {
+ eb64_insert(&stream->acked_frms, &strm_frm->offset);
+ }
+
+ quic_stream_try_to_consume(qc, stream);
+ }
+ break;
+ default:
+ qc_release_frm(qc, frm);
+ }
+
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+}
+
+/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
+ * deallocating them, and their TX frames.
+ * May be NULL if <largest> node could not be found.
+ */
+static void qc_ackrng_pkts(struct quic_conn *qc, struct eb_root *pkts,
+ unsigned int *pkt_flags, struct list *newly_acked_pkts,
+ struct eb64_node *largest_node,
+ uint64_t largest, uint64_t smallest)
+{
+ struct eb64_node *node;
+ struct quic_tx_packet *pkt;
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+
+ node = eb64_lookup_ge(pkts, smallest);
+ if (!node)
+ goto leave;
+
+ largest_node = largest_node ? largest_node : eb64_lookup_le(pkts, largest);
+ if (!largest_node)
+ goto leave;
+
+ while (node && node->key <= largest_node->key) {
+ struct quic_frame *frm, *frmbak;
+
+ pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
+ *pkt_flags |= pkt->flags;
+ LIST_INSERT(newly_acked_pkts, &pkt->list);
+ TRACE_DEVEL("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
+ list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
+ qc_treat_acked_tx_frm(qc, frm);
+ /* If there are others packet in the same datagram <pkt> is attached to,
+ * detach the previous one and the next one from <pkt>.
+ */
+ quic_tx_packet_dgram_detach(pkt);
+ node = eb64_next(node);
+ eb64_delete(&pkt->pn_node);
+ }
+
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+}
+
+/* Remove all frames from <pkt_frm_list> and reinsert them in the same order
+ * they have been sent into <pktns_frm_list>. The loss counter of each frame is
+ * incremented and checked if it does not exceed retransmission limit.
+ *
+ * Returns 1 on success, 0 if a frame loss limit is exceeded. A
+ * CONNECTION_CLOSE is scheduled in this case.
+ */
+static int qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
+ struct quic_tx_packet *pkt,
+ struct list *pktns_frm_list)
+{
+ struct quic_frame *frm, *frmbak;
+ struct list *pkt_frm_list = &pkt->frms;
+ uint64_t pn = pkt->pn_node.key;
+ int close = 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+
+ list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
+ /* First remove this frame from the packet it was attached to */
+ LIST_DEL_INIT(&frm->list);
+ quic_tx_packet_refdec(pkt);
+ /* At this time, this frame is not freed but removed from its packet */
+ frm->pkt = NULL;
+ /* Remove any reference to this frame */
+ qc_frm_unref(frm, qc);
+ switch (frm->type) {
+ case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
+ {
+ struct qf_stream *strm_frm = &frm->stream;
+ struct eb64_node *node = NULL;
+ struct qc_stream_desc *stream_desc;
+
+ node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
+ if (!node) {
+ TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
+ TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
+ qc, frm, &pn);
+ qc_frm_free(qc, &frm);
+ continue;
+ }
+
+ stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
+ /* Do not resend this frame if in the "already acked range" */
+ if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
+ TRACE_DEVEL("ignored frame in already acked range",
+ QUIC_EV_CONN_PRSAFRM, qc, frm);
+ qc_frm_free(qc, &frm);
+ continue;
+ }
+ else if (strm_frm->offset.key < stream_desc->ack_offset) {
+ uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
+
+ qc_stream_frm_mv_fwd(frm, diff);
+ TRACE_DEVEL("updated partially acked frame",
+ QUIC_EV_CONN_PRSAFRM, qc, frm);
+ }
+ break;
+ }
+
+ default:
+ break;
+ }
+
+ /* Do not resend probing packet with old data */
+ if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
+ TRACE_DEVEL("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
+ qc, frm, &pn);
+ if (frm->origin)
+ LIST_DEL_INIT(&frm->ref);
+ qc_frm_free(qc, &frm);
+ continue;
+ }
+
+ if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
+ TRACE_DEVEL("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
+ TRACE_DEVEL("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
+ qc, frm, &pn);
+ qc_frm_free(qc, &frm);
+ }
+ else {
+ if (++frm->loss_count >= global.tune.quic_max_frame_loss) {
+ TRACE_ERROR("retransmission limit reached, closing the connection", QUIC_EV_CONN_PRSAFRM, qc);
+ quic_set_connection_close(qc, quic_err_transport(QC_ERR_INTERNAL_ERROR));
+ qc_notify_err(qc);
+ close = 1;
+ }
+
+ LIST_APPEND(pktns_frm_list, &frm->list);
+ TRACE_DEVEL("frame requeued", QUIC_EV_CONN_PRSAFRM, qc, frm);
+ }
+ }
+
+ end:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+ return !close;
+}
+
+/* Send a packet ack event nofication for each newly acked packet of
+ * <newly_acked_pkts> list and free them.
+ * Always succeeds.
+ */
+static void qc_treat_newly_acked_pkts(struct quic_conn *qc,
+ struct list *newly_acked_pkts)
+{
+ struct quic_tx_packet *pkt, *tmp;
+ struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+
+ list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
+ pkt->pktns->tx.in_flight -= pkt->in_flight_len;
+ qc->path->prep_in_flight -= pkt->in_flight_len;
+ qc->path->in_flight -= pkt->in_flight_len;
+ if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
+ qc->path->ifae_pkts--;
+ /* If this packet contained an ACK frame, proceed to the
+ * acknowledging of range of acks from the largest acknowledged
+ * packet number which was sent in an ACK frame by this packet.
+ */
+ if (pkt->largest_acked_pn != -1)
+ qc_treat_ack_of_ack(qc, &pkt->pktns->rx.arngs, pkt->largest_acked_pn);
+ ev.ack.acked = pkt->in_flight_len;
+ ev.ack.time_sent = pkt->time_sent;
+ quic_cc_event(&qc->path->cc, &ev);
+ LIST_DELETE(&pkt->list);
+ eb64_delete(&pkt->pn_node);
+ quic_tx_packet_refdec(pkt);
+ }
+
+ TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+
+}
+
+/* Handle <pkts> list of lost packets detected at <now_us> handling their TX
+ * frames. Send a packet loss event to the congestion controller if in flight
+ * packet have been lost. Also frees the packet in <pkts> list.
+ *
+ * Returns 1 on success else 0 if loss limit has been exceeded. A
+ * CONNECTION_CLOSE was prepared to close the connection ASAP.
+ */
+int qc_release_lost_pkts(struct quic_conn *qc, struct quic_pktns *pktns,
+ struct list *pkts, uint64_t now_us)
+{
+ struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
+ int close = 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+
+ if (LIST_ISEMPTY(pkts))
+ goto leave;
+
+ oldest_lost = newest_lost = NULL;
+ list_for_each_entry_safe(pkt, tmp, pkts, list) {
+ struct list tmp = LIST_HEAD_INIT(tmp);
+
+ pkt->pktns->tx.in_flight -= pkt->in_flight_len;
+ qc->path->prep_in_flight -= pkt->in_flight_len;
+ qc->path->in_flight -= pkt->in_flight_len;
+ if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
+ qc->path->ifae_pkts--;
+ /* Treat the frames of this lost packet. */
+ if (!qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms))
+ close = 1;
+ LIST_DELETE(&pkt->list);
+ if (!oldest_lost) {
+ oldest_lost = newest_lost = pkt;
+ }
+ else {
+ if (newest_lost != oldest_lost)
+ quic_tx_packet_refdec(newest_lost);
+ newest_lost = pkt;
+ }
+ }
+
+ if (!close) {
+ if (newest_lost) {
+ /* Sent a congestion event to the controller */
+ struct quic_cc_event ev = { };
+
+ ev.type = QUIC_CC_EVT_LOSS;
+ ev.loss.time_sent = newest_lost->time_sent;
+
+ quic_cc_event(&qc->path->cc, &ev);
+ }
+
+ /* If an RTT have been already sampled, <rtt_min> has been set.
+ * We must check if we are experiencing a persistent congestion.
+ * If this is the case, the congestion controller must re-enter
+ * slow start state.
+ */
+ if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
+ unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
+
+ if (quic_loss_persistent_congestion(&qc->path->loss, period,
+ now_ms, qc->max_ack_delay))
+ qc->path->cc.algo->slow_start(&qc->path->cc);
+ }
+ }
+
+ /* <oldest_lost> cannot be NULL at this stage because we have ensured
+ * that <pkts> list is not empty. Without this, GCC 12.2.0 reports a
+ * possible overflow on a 0 byte region with O2 optimization.
+ */
+ ALREADY_CHECKED(oldest_lost);
+ quic_tx_packet_refdec(oldest_lost);
+ if (newest_lost != oldest_lost)
+ quic_tx_packet_refdec(newest_lost);
+
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+ return !close;
+}
+
+/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
+ * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
+ * if the largest acked packet was newly acked and if there was at least one newly
+ * acked ack-eliciting packet.
+ * Return 1, if succeeded, 0 if not.
+ */
+static int qc_parse_ack_frm(struct quic_conn *qc,
+ struct quic_frame *frm,
+ struct quic_enc_level *qel,
+ unsigned int *rtt_sample,
+ const unsigned char **pos, const unsigned char *end)
+{
+ struct qf_ack *ack_frm = &frm->ack;
+ uint64_t smallest, largest;
+ struct eb_root *pkts;
+ struct eb64_node *largest_node;
+ unsigned int time_sent, pkt_flags;
+ struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
+ struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
+ int ret = 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+
+ if (ack_frm->largest_ack > qel->pktns->tx.next_pn) {
+ TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
+ qc, NULL, &ack_frm->largest_ack);
+ goto err;
+ }
+
+ if (ack_frm->first_ack_range > ack_frm->largest_ack) {
+ TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
+ qc, NULL, &ack_frm->first_ack_range);
+ goto err;
+ }
+
+ largest = ack_frm->largest_ack;
+ smallest = largest - ack_frm->first_ack_range;
+ pkts = &qel->pktns->tx.pkts;
+ pkt_flags = 0;
+ largest_node = NULL;
+ time_sent = 0;
+
+ if ((int64_t)ack_frm->largest_ack > qel->pktns->rx.largest_acked_pn) {
+ largest_node = eb64_lookup(pkts, largest);
+ if (!largest_node) {
+ TRACE_DEVEL("Largest acked packet not found",
+ QUIC_EV_CONN_PRSAFRM, qc);
+ }
+ else {
+ time_sent = eb64_entry(largest_node,
+ struct quic_tx_packet, pn_node)->time_sent;
+ }
+ }
+
+ TRACE_PROTO("RX ack range", QUIC_EV_CONN_PRSAFRM,
+ qc, NULL, &largest, &smallest);
+ do {
+ uint64_t gap, ack_range;
+
+ qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
+ largest_node, largest, smallest);
+ if (!ack_frm->ack_range_num--)
+ break;
+
+ if (!quic_dec_int(&gap, pos, end)) {
+ TRACE_ERROR("quic_dec_int(gap) failed", QUIC_EV_CONN_PRSAFRM, qc);
+ goto err;
+ }
+
+ if (smallest < gap + 2) {
+ TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
+ qc, NULL, &gap, &smallest);
+ goto err;
+ }
+
+ largest = smallest - gap - 2;
+ if (!quic_dec_int(&ack_range, pos, end)) {
+ TRACE_ERROR("quic_dec_int(ack_range) failed", QUIC_EV_CONN_PRSAFRM, qc);
+ goto err;
+ }
+
+ if (largest < ack_range) {
+ TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
+ qc, NULL, &largest, &ack_range);
+ goto err;
+ }
+
+ /* Do not use this node anymore. */
+ largest_node = NULL;
+ /* Next range */
+ smallest = largest - ack_range;
+
+ TRACE_PROTO("RX next ack range", QUIC_EV_CONN_PRSAFRM,
+ qc, NULL, &largest, &smallest);
+ } while (1);
+
+ if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
+ *rtt_sample = tick_remain(time_sent, now_ms);
+ qel->pktns->rx.largest_acked_pn = ack_frm->largest_ack;
+ }
+
+ if (!LIST_ISEMPTY(&newly_acked_pkts)) {
+ if (!eb_is_empty(&qel->pktns->tx.pkts)) {
+ qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
+ if (!qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms))
+ goto leave;
+ }
+ qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
+ if (quic_peer_validated_addr(qc))
+ qc->path->loss.pto_count = 0;
+ qc_set_timer(qc);
+ qc_notify_send(qc);
+ }
+
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+ return ret;
+
+ err:
+ free_quic_tx_pkts(qc, &newly_acked_pkts);
+ goto leave;
+}
+
+/* Parse a STREAM frame <strm_frm> received in <pkt> packet for <qc>
+ * connection. <fin> is true if FIN bit is set on frame type.
+ *
+ * Return 1 on success. On error, 0 is returned. In this case, the packet
+ * containing the frame must not be acknowledged.
+ */
+static int qc_handle_strm_frm(struct quic_rx_packet *pkt,
+ struct qf_stream *strm_frm,
+ struct quic_conn *qc, char fin)
+{
+ int ret;
+
+ /* RFC9000 13.1. Packet Processing
+ *
+ * A packet MUST NOT be acknowledged until packet protection has been
+ * successfully removed and all frames contained in the packet have
+ * been processed. For STREAM frames, this means the data has been
+ * enqueued in preparation to be received by the application protocol,
+ * but it does not require that data be delivered and consumed.
+ */
+ TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
+
+ ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
+ strm_frm->offset.key, fin, (char *)strm_frm->data);
+
+ /* frame rejected - packet must not be acknowledeged */
+ TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
+ return !ret;
+}
+
+/* Release the underlying memory use by <ncbuf> non-contiguous buffer */
+void quic_free_ncbuf(struct ncbuf *ncbuf)
+{
+ struct buffer buf;
+
+ if (ncb_is_null(ncbuf))
+ return;
+
+ buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
+ b_free(&buf);
+ offer_buffers(NULL, 1);
+
+ *ncbuf = NCBUF_NULL;
+}
+
+/* Allocate the underlying required memory for <ncbuf> non-contiguous buffer */
+static struct ncbuf *quic_get_ncbuf(struct ncbuf *ncbuf)
+{
+ struct buffer buf = BUF_NULL;
+
+ if (!ncb_is_null(ncbuf))
+ return ncbuf;
+
+ b_alloc(&buf);
+ BUG_ON(b_is_null(&buf));
+
+ *ncbuf = ncb_make(buf.area, buf.size, 0);
+ ncb_init(ncbuf, 0);
+
+ return ncbuf;
+}
+
+/* Parse <frm> CRYPTO frame coming with <pkt> packet at <qel> <qc> connectionn.
+ * Returns 1 if succeeded, 0 if not. Also set <*fast_retrans> to 1 if the
+ * speed up handshake completion may be run after having received duplicated
+ * CRYPTO data.
+ */
+static int qc_handle_crypto_frm(struct quic_conn *qc,
+ struct qf_crypto *crypto_frm, struct quic_rx_packet *pkt,
+ struct quic_enc_level *qel, int *fast_retrans)
+{
+ int ret = 0;
+ enum ncb_ret ncb_ret;
+ /* XXX TO DO: <cfdebug> is used only for the traces. */
+ struct quic_rx_crypto_frm cfdebug = {
+ .offset_node.key = crypto_frm->offset,
+ .len = crypto_frm->len,
+ };
+ struct quic_cstream *cstream = qel->cstream;
+ struct ncbuf *ncbuf = &qel->cstream->rx.ncbuf;
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
+
+ if (unlikely(crypto_frm->offset < cstream->rx.offset)) {
+ size_t diff;
+
+ if (crypto_frm->offset + crypto_frm->len <= cstream->rx.offset) {
+ /* Nothing to do */
+ TRACE_PROTO("Already received CRYPTO data",
+ QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
+ if (qc_is_listener(qc) && qel == qc->iel &&
+ !(qc->flags & QUIC_FL_CONN_HANDSHAKE_SPEED_UP))
+ *fast_retrans = 1;
+ goto done;
+ }
+
+ TRACE_PROTO("Partially already received CRYPTO data",
+ QUIC_EV_CONN_RXPKT, qc, pkt, &cfdebug);
+
+ diff = cstream->rx.offset - crypto_frm->offset;
+ crypto_frm->len -= diff;
+ crypto_frm->data += diff;
+ crypto_frm->offset = cstream->rx.offset;
+ }
+
+ if (crypto_frm->offset == cstream->rx.offset && ncb_is_empty(ncbuf)) {
+ if (!qc_ssl_provide_quic_data(&qel->cstream->rx.ncbuf, qel->level,
+ qc->xprt_ctx, crypto_frm->data, crypto_frm->len)) {
+ // trace already emitted by function above
+ goto leave;
+ }
+
+ cstream->rx.offset += crypto_frm->len;
+ TRACE_DEVEL("increment crypto level offset", QUIC_EV_CONN_PHPKTS, qc, qel);
+ goto done;
+ }
+
+ if (!quic_get_ncbuf(ncbuf) ||
+ ncb_is_null(ncbuf)) {
+ TRACE_ERROR("CRYPTO ncbuf allocation failed", QUIC_EV_CONN_PRSHPKT, qc);
+ goto leave;
+ }
+
+ /* crypto_frm->offset > cstream-trx.offset */
+ ncb_ret = ncb_add(ncbuf, crypto_frm->offset - cstream->rx.offset,
+ (const char *)crypto_frm->data, crypto_frm->len, NCB_ADD_COMPARE);
+ if (ncb_ret != NCB_RET_OK) {
+ if (ncb_ret == NCB_RET_DATA_REJ) {
+ TRACE_ERROR("overlapping data rejected", QUIC_EV_CONN_PRSHPKT, qc);
+ quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
+ qc_notify_err(qc);
+ }
+ else if (ncb_ret == NCB_RET_GAP_SIZE) {
+ TRACE_ERROR("cannot bufferize frame due to gap size limit",
+ QUIC_EV_CONN_PRSHPKT, qc);
+ }
+ goto leave;
+ }
+
+ done:
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
+ return ret;
+}
+
+/* Handle RETIRE_CONNECTION_ID frame from <frm> frame.
+ * Return 1 if succeeded, 0 if not. If succeeded, also set <to_retire>
+ * to the CID to be retired if not already retired.
+ */
+static int qc_handle_retire_connection_id_frm(struct quic_conn *qc,
+ struct quic_frame *frm,
+ struct quic_cid *dcid,
+ struct quic_connection_id **to_retire)
+{
+ int ret = 0;
+ struct qf_retire_connection_id *rcid_frm = &frm->retire_connection_id;
+ struct eb64_node *node;
+ struct quic_connection_id *conn_id;
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
+
+ /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
+ * Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number greater
+ * than any previously sent to the peer MUST be treated as a connection error
+ * of type PROTOCOL_VIOLATION.
+ */
+ if (rcid_frm->seq_num >= qc->next_cid_seq_num) {
+ TRACE_PROTO("CID seq. number too big", QUIC_EV_CONN_PSTRM, qc, frm);
+ goto protocol_violation;
+ }
+
+ /* RFC 9000 19.16. RETIRE_CONNECTION_ID Frames:
+ * The sequence number specified in a RETIRE_CONNECTION_ID frame MUST NOT refer to
+ * the Destination Connection ID field of the packet in which the frame is contained.
+ * The peer MAY treat this as a connection error of type PROTOCOL_VIOLATION.
+ */
+ node = eb64_lookup(&qc->cids, rcid_frm->seq_num);
+ if (!node) {
+ TRACE_PROTO("CID already retired", QUIC_EV_CONN_PSTRM, qc, frm);
+ goto out;
+ }
+
+ conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
+ /* Note that the length of <dcid> has already been checked. It must match the
+ * length of the CIDs which have been provided to the peer.
+ */
+ if (!memcmp(dcid->data, conn_id->cid.data, QUIC_HAP_CID_LEN)) {
+ TRACE_PROTO("cannot retire the current CID", QUIC_EV_CONN_PSTRM, qc, frm);
+ goto protocol_violation;
+ }
+
+ *to_retire = conn_id;
+ out:
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
+ return ret;
+ protocol_violation:
+ quic_set_connection_close(qc, quic_err_transport(QC_ERR_PROTOCOL_VIOLATION));
+ qc_notify_err(qc);
+ goto leave;
+}
+
+/* Parse all the frames of <pkt> QUIC packet for QUIC connection <qc> and <qel>
+ * as encryption level.
+ * Returns 1 if succeeded, 0 if failed.
+ */
+static int qc_parse_pkt_frms(struct quic_conn *qc, struct quic_rx_packet *pkt,
+ struct quic_enc_level *qel)
+{
+ struct quic_frame frm;
+ const unsigned char *pos, *end;
+ int fast_retrans = 0, ret = 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
+ /* Skip the AAD */
+ pos = pkt->data + pkt->aad_len;
+ end = pkt->data + pkt->len;
+
+ while (pos < end) {
+ if (!qc_parse_frm(&frm, pkt, &pos, end, qc)) {
+ // trace already emitted by function above
+ goto leave;
+ }
+
+ switch (frm.type) {
+ case QUIC_FT_PADDING:
+ break;
+ case QUIC_FT_PING:
+ break;
+ case QUIC_FT_ACK:
+ {
+ unsigned int rtt_sample;
+
+ rtt_sample = UINT_MAX;
+ if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end)) {
+ // trace already emitted by function above
+ goto leave;
+ }
+
+ if (rtt_sample != UINT_MAX) {
+ unsigned int ack_delay;
+
+ ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
+ qc->state >= QUIC_HS_ST_CONFIRMED ?
+ MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
+ MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
+ quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
+ }
+ break;
+ }
+ case QUIC_FT_RESET_STREAM:
+ if (qc->mux_state == QC_MUX_READY) {
+ struct qf_reset_stream *rs_frm = &frm.reset_stream;
+ qcc_recv_reset_stream(qc->qcc, rs_frm->id, rs_frm->app_error_code, rs_frm->final_size);
+ }
+ break;
+ case QUIC_FT_STOP_SENDING:
+ {
+ struct qf_stop_sending *ss_frm = &frm.stop_sending;
+ if (qc->mux_state == QC_MUX_READY) {
+ if (qcc_recv_stop_sending(qc->qcc, ss_frm->id,
+ ss_frm->app_error_code)) {
+ TRACE_ERROR("qcc_recv_stop_sending() failed", QUIC_EV_CONN_PRSHPKT, qc);
+ goto leave;
+ }
+ }
+ break;
+ }
+ case QUIC_FT_CRYPTO:
+ if (!qc_handle_crypto_frm(qc, &frm.crypto, pkt, qel, &fast_retrans))
+ goto leave;
+ break;
+ case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
+ {
+ struct qf_stream *strm_frm = &frm.stream;
+ unsigned nb_streams = qc->rx.strms[qcs_id_type(strm_frm->id)].nb_streams;
+ const char fin = frm.type & QUIC_STREAM_FRAME_TYPE_FIN_BIT;
+
+ /* The upper layer may not be allocated. */
+ if (qc->mux_state != QC_MUX_READY) {
+ if ((strm_frm->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
+ TRACE_DATA("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
+ }
+ else {
+ TRACE_DEVEL("No mux for new stream", QUIC_EV_CONN_PRSHPKT, qc);
+ if (qc->app_ops == &h3_ops) {
+ if (!qc_h3_request_reject(qc, strm_frm->id)) {
+ TRACE_ERROR("error on request rejection", QUIC_EV_CONN_PRSHPKT, qc);
+ /* This packet will not be acknowledged */
+ goto leave;
+ }
+ }
+ else {
+ /* This packet will not be acknowledged */
+ goto leave;
+ }
+ }
+
+ break;
+ }
+
+ if (!qc_handle_strm_frm(pkt, strm_frm, qc, fin)) {
+ TRACE_ERROR("qc_handle_strm_frm() failed", QUIC_EV_CONN_PRSHPKT, qc);
+ goto leave;
+ }
+
+ break;
+ }
+ case QUIC_FT_MAX_DATA:
+ if (qc->mux_state == QC_MUX_READY) {
+ struct qf_max_data *md_frm = &frm.max_data;
+ qcc_recv_max_data(qc->qcc, md_frm->max_data);
+ }
+ break;
+ case QUIC_FT_MAX_STREAM_DATA:
+ if (qc->mux_state == QC_MUX_READY) {
+ struct qf_max_stream_data *msd_frm = &frm.max_stream_data;
+ if (qcc_recv_max_stream_data(qc->qcc, msd_frm->id,
+ msd_frm->max_stream_data)) {
+ TRACE_ERROR("qcc_recv_max_stream_data() failed", QUIC_EV_CONN_PRSHPKT, qc);
+ goto leave;
+ }
+ }
+ break;
+ case QUIC_FT_MAX_STREAMS_BIDI:
+ case QUIC_FT_MAX_STREAMS_UNI:
+ break;
+ case QUIC_FT_DATA_BLOCKED:
+ qc->cntrs.data_blocked++;
+ break;
+ case QUIC_FT_STREAM_DATA_BLOCKED:
+ qc->cntrs.stream_data_blocked++;
+ break;
+ case QUIC_FT_STREAMS_BLOCKED_BIDI:
+ qc->cntrs.streams_blocked_bidi++;
+ break;
+ case QUIC_FT_STREAMS_BLOCKED_UNI:
+ qc->cntrs.streams_blocked_uni++;
+ break;
+ case QUIC_FT_NEW_CONNECTION_ID:
+ /* XXX TO DO XXX */
+ break;
+ case QUIC_FT_RETIRE_CONNECTION_ID:
+ {
+ struct quic_connection_id *conn_id = NULL;
+
+ if (!qc_handle_retire_connection_id_frm(qc, &frm, &pkt->dcid, &conn_id))
+ goto leave;
+
+ if (!conn_id)
+ break;
+
+ ebmb_delete(&conn_id->node);
+ eb64_delete(&conn_id->seq_num);
+ pool_free(pool_head_quic_connection_id, conn_id);
+ TRACE_PROTO("CID retired", QUIC_EV_CONN_PSTRM, qc);
+
+ conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
+ if (!conn_id) {
+ TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
+ }
+ else {
+ quic_cid_insert(conn_id);
+ qc_build_new_connection_id_frm(qc, conn_id);
+ }
+ break;
+ }
+ case QUIC_FT_CONNECTION_CLOSE:
+ case QUIC_FT_CONNECTION_CLOSE_APP:
+ /* Increment the error counters */
+ qc_cc_err_count_inc(qc, &frm);
+ if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
+ if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
+ qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
+ HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
+ }
+ TRACE_STATE("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
+ /* RFC 9000 10.2. Immediate Close:
+ * The closing and draining connection states exist to ensure
+ * that connections close cleanly and that delayed or reordered
+ * packets are properly discarded. These states SHOULD persist
+ * for at least three times the current PTO interval...
+ *
+ * Rearm the idle timeout only one time when entering draining
+ * state.
+ */
+ qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
+ qc_detach_th_ctx_list(qc, 1);
+ qc_idle_timer_do_rearm(qc, 0);
+ qc_notify_err(qc);
+ }
+ break;
+ case QUIC_FT_HANDSHAKE_DONE:
+ if (qc_is_listener(qc)) {
+ TRACE_ERROR("non accepted QUIC_FT_HANDSHAKE_DONE frame",
+ QUIC_EV_CONN_PRSHPKT, qc);
+ goto leave;
+ }
+
+ qc->state = QUIC_HS_ST_CONFIRMED;
+ break;
+ default:
+ TRACE_ERROR("unknosw frame type", QUIC_EV_CONN_PRSHPKT, qc);
+ goto leave;
+ }
+ }
+
+ /* Flag this packet number space as having received a packet. */
+ qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
+
+ if (fast_retrans && qc->iel && qc->hel) {
+ struct quic_enc_level *iqel = qc->iel;
+ struct quic_enc_level *hqel = qc->hel;
+
+ TRACE_PROTO("speeding up handshake completion", QUIC_EV_CONN_PRSHPKT, qc);
+ qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
+ qc->flags |= QUIC_FL_CONN_HANDSHAKE_SPEED_UP;
+ }
+
+ /* The server must switch from INITIAL to HANDSHAKE handshake state when it
+ * has successfully parse a Handshake packet. The Initial encryption must also
+ * be discarded.
+ */
+ if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(qc)) {
+ if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
+ if (qc->ipktns && !quic_tls_pktns_is_dcd(qc, qc->ipktns)) {
+ /* Discard the handshake packet number space. */
+ TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
+ quic_pktns_discard(qc->ipktns, qc);
+ qc_set_timer(qc);
+ qc_el_rx_pkts_del(qc->iel);
+ qc_release_pktns_frms(qc, qc->ipktns);
+ }
+ if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
+ qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
+ }
+ }
+
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
+ return ret;
+}
+
+/* Detect the value of the spin bit to be used. */
+static inline void qc_handle_spin_bit(struct quic_conn *qc, struct quic_rx_packet *pkt,
+ struct quic_enc_level *qel)
+{
+ uint64_t largest_pn = qel->pktns->rx.largest_pn;
+
+ if (qel != qc->ael || largest_pn == -1 ||
+ pkt->pn <= largest_pn)
+ return;
+
+ if (qc_is_listener(qc)) {
+ if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
+ qc->flags |= QUIC_FL_CONN_SPIN_BIT;
+ else
+ qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
+ }
+ else {
+ if (pkt->flags & QUIC_FL_RX_PACKET_SPIN_BIT)
+ qc->flags &= ~QUIC_FL_CONN_SPIN_BIT;
+ else
+ qc->flags |= QUIC_FL_CONN_SPIN_BIT;
+ }
+}
+
+/* Remove the header protection of packets at <el> encryption level.
+ * Always succeeds.
+ */
+static void qc_rm_hp_pkts(struct quic_conn *qc, struct quic_enc_level *el)
+{
+ struct quic_rx_packet *pqpkt, *pkttmp;
+
+ TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
+ /* A server must not process incoming 1-RTT packets before the handshake is complete. */
+ if (el == qc->ael && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
+ TRACE_PROTO("RX hp not removed (handshake not completed)",
+ QUIC_EV_CONN_ELRMHP, qc);
+ goto out;
+ }
+
+ list_for_each_entry_safe(pqpkt, pkttmp, &el->rx.pqpkts, list) {
+ struct quic_tls_ctx *tls_ctx;
+
+ tls_ctx = qc_select_tls_ctx(qc, el, pqpkt->type, pqpkt->version);
+ if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
+ pqpkt->data + pqpkt->pn_offset, pqpkt->data)) {
+ TRACE_ERROR("RX hp removing error", QUIC_EV_CONN_ELRMHP, qc);
+ }
+ else {
+ qc_handle_spin_bit(qc, pqpkt, el);
+ /* The AAD includes the packet number field */
+ pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
+ /* Store the packet into the tree of packets to decrypt. */
+ pqpkt->pn_node.key = pqpkt->pn;
+ eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
+ quic_rx_packet_refinc(pqpkt);
+ TRACE_PROTO("RX hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
+ }
+ LIST_DELETE(&pqpkt->list);
+ quic_rx_packet_refdec(pqpkt);
+ }
+
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
+}
+
+/* Process all the CRYPTO frame at <el> encryption level. This is the
+ * responsibility of the called to ensure there exists a CRYPTO data
+ * stream for this level.
+ * Return 1 if succeeded, 0 if not.
+ */
+static int qc_treat_rx_crypto_frms(struct quic_conn *qc,
+ struct quic_enc_level *el,
+ struct ssl_sock_ctx *ctx)
+{
+ int ret = 0;
+ struct ncbuf *ncbuf;
+ struct quic_cstream *cstream = el->cstream;
+ ncb_sz_t data;
+
+ TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
+
+ BUG_ON(!cstream);
+ ncbuf = &cstream->rx.ncbuf;
+ if (ncb_is_null(ncbuf))
+ goto done;
+
+ /* TODO not working if buffer is wrapping */
+ while ((data = ncb_data(ncbuf, 0))) {
+ const unsigned char *cdata = (const unsigned char *)ncb_head(ncbuf);
+
+ if (!qc_ssl_provide_quic_data(&el->cstream->rx.ncbuf, el->level,
+ ctx, cdata, data))
+ goto leave;
+
+ cstream->rx.offset += data;
+ TRACE_DEVEL("buffered crypto data were provided to TLS stack",
+ QUIC_EV_CONN_PHPKTS, qc, el);
+ }
+
+ done:
+ ret = 1;
+ leave:
+ if (!ncb_is_null(ncbuf) && ncb_is_empty(ncbuf)) {
+ TRACE_DEVEL("freeing crypto buf", QUIC_EV_CONN_PHPKTS, qc, el);
+ quic_free_ncbuf(ncbuf);
+ }
+ TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
+ return ret;
+}
+
+/* Check if it's possible to remove header protection for packets related to
+ * encryption level <qel>. If <qel> is NULL, assume it's false.
+ *
+ * Return true if the operation is possible else false.
+ */
+static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
+{
+ int ret = 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
+
+ if (!qel)
+ goto cant_rm_hp;
+
+ if (!quic_tls_has_rx_sec(qel)) {
+ TRACE_PROTO("non available secrets", QUIC_EV_CONN_TRMHP, qc);
+ goto cant_rm_hp;
+ }
+
+ if (qel == qc->ael && qc->state < QUIC_HS_ST_COMPLETE) {
+ TRACE_PROTO("handshake not complete", QUIC_EV_CONN_TRMHP, qc);
+ goto cant_rm_hp;
+ }
+
+ /* check if the connection layer is ready before using app level */
+ if ((qel == qc->ael || qel == qc->eel) &&
+ qc->mux_state == QC_MUX_NULL) {
+ TRACE_PROTO("connection layer not ready", QUIC_EV_CONN_TRMHP, qc);
+ goto cant_rm_hp;
+ }
+
+ ret = 1;
+ cant_rm_hp:
+ TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
+ return ret;
+}
+
+/* Process all the packets for all the encryption levels listed in <qc> QUIC connection.
+ * Return 1 if succeeded, 0 if not.
+ */
+int qc_treat_rx_pkts(struct quic_conn *qc)
+{
+ int ret = 0;
+ struct eb64_node *node;
+ int64_t largest_pn = -1;
+ unsigned int largest_pn_time_received = 0;
+ struct quic_enc_level *qel, *qelbak;
+
+ TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
+
+ list_for_each_entry_safe(qel, qelbak, &qc->qel_list, list) {
+ /* Treat packets waiting for header packet protection decryption */
+ if (!LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
+ qc_rm_hp_pkts(qc, qel);
+
+ node = eb64_first(&qel->rx.pkts);
+ while (node) {
+ struct quic_rx_packet *pkt;
+
+ pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
+ TRACE_DATA("new packet", QUIC_EV_CONN_RXPKT,
+ qc, pkt, NULL, qc->xprt_ctx->ssl);
+ if (!qc_pkt_decrypt(qc, qel, pkt)) {
+ /* Drop the packet */
+ TRACE_ERROR("packet decryption failed -> dropped",
+ QUIC_EV_CONN_RXPKT, qc, pkt);
+ }
+ else {
+ if (!qc_parse_pkt_frms(qc, pkt, qel)) {
+ /* Drop the packet */
+ TRACE_ERROR("packet parsing failed -> dropped",
+ QUIC_EV_CONN_RXPKT, qc, pkt);
+ qc->cntrs.dropped_parsing++;
+ }
+ else {
+ struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
+
+ if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
+ int arm_ack_timer =
+ qc->state >= QUIC_HS_ST_COMPLETE &&
+ qel->pktns == qc->apktns;
+
+ qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
+ qel->pktns->rx.nb_aepkts_since_last_ack++;
+ qc_idle_timer_rearm(qc, 1, arm_ack_timer);
+ }
+ if (pkt->pn > largest_pn) {
+ largest_pn = pkt->pn;
+ largest_pn_time_received = pkt->time_received;
+ }
+ /* Update the list of ranges to acknowledge. */
+ if (!quic_update_ack_ranges_list(qc, &qel->pktns->rx.arngs, &ar))
+ TRACE_ERROR("Could not update ack range list",
+ QUIC_EV_CONN_RXPKT, qc);
+ }
+ }
+ node = eb64_next(node);
+ eb64_delete(&pkt->pn_node);
+ quic_rx_packet_refdec(pkt);
+ }
+
+ if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
+ /* Update the largest packet number. */
+ qel->pktns->rx.largest_pn = largest_pn;
+ /* Update the largest acknowledged packet timestamps */
+ qel->pktns->rx.largest_time_received = largest_pn_time_received;
+ qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
+ }
+
+ if (qel->cstream && !qc_treat_rx_crypto_frms(qc, qel, qc->xprt_ctx)) {
+ // trace already emitted by function above
+ goto leave;
+ }
+
+ /* Release the Initial encryption level and packet number space. */
+ if ((qc->flags & QUIC_FL_CONN_IPKTNS_DCD) && qel == qc->iel) {
+ qc_enc_level_free(qc, &qc->iel);
+ quic_pktns_release(qc, &qc->ipktns);
+ }
+
+ largest_pn = -1;
+ }
+
+ out:
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
+ return ret;
+}
+
+/* Parse the Retry token from buffer <token> with <end> a pointer to
+ * one byte past the end of this buffer. This will extract the ODCID
+ * which will be stored into <odcid>
+ *
+ * Returns 0 on success else non-zero.
+ */
+static int parse_retry_token(struct quic_conn *qc,
+ const unsigned char *token, const unsigned char *end,
+ struct quic_cid *odcid)
+{
+ int ret = 0;
+ uint64_t odcid_len;
+ uint32_t timestamp;
+ uint32_t now_sec = (uint32_t)date.tv_sec;
+
+ TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
+
+ if (!quic_dec_int(&odcid_len, &token, end)) {
+ TRACE_ERROR("quic_dec_int() error", QUIC_EV_CONN_LPKT, qc);
+ goto leave;
+ }
+
+ /* RFC 9000 7.2. Negotiating Connection IDs:
+ * When an Initial packet is sent by a client that has not previously
+ * received an Initial or Retry packet from the server, the client
+ * populates the Destination Connection ID field with an unpredictable
+ * value. This Destination Connection ID MUST be at least 8 bytes in length.
+ */
+ if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN) {
+ TRACE_ERROR("wrong ODCID length", QUIC_EV_CONN_LPKT, qc);
+ goto leave;
+ }
+
+ if (end - token < odcid_len + sizeof timestamp) {
+ TRACE_ERROR("too long ODCID length", QUIC_EV_CONN_LPKT, qc);
+ goto leave;
+ }
+
+ timestamp = ntohl(read_u32(token + odcid_len));
+ /* check if elapsed time is +/- QUIC_RETRY_DURATION_SEC
+ * to tolerate token generator is not perfectly time synced
+ */
+ if ((uint32_t)(now_sec - timestamp) > QUIC_RETRY_DURATION_SEC &&
+ (uint32_t)(timestamp - now_sec) > QUIC_RETRY_DURATION_SEC) {
+ TRACE_ERROR("token has expired", QUIC_EV_CONN_LPKT, qc);
+ goto leave;
+ }
+
+ ret = 1;
+ memcpy(odcid->data, token, odcid_len);
+ odcid->len = odcid_len;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
+ return !ret;
+}
+
+/* Parse into <pkt> a long header located at <*pos> position, <end> begin a pointer to the end
+ * past one byte of this buffer.
+ */
+static inline int quic_packet_read_long_header(unsigned char **pos, const unsigned char *end,
+ struct quic_rx_packet *pkt)
+{
+ int ret = 0;
+ unsigned char dcid_len, scid_len;
+
+ TRACE_ENTER(QUIC_EV_CONN_RXPKT);
+
+ if (end == *pos) {
+ TRACE_ERROR("buffer data consumed", QUIC_EV_CONN_RXPKT);
+ goto leave;
+ }
+
+ /* Destination Connection ID Length */
+ dcid_len = *(*pos)++;
+ /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
+ if (dcid_len > QUIC_CID_MAXLEN || end - *pos < dcid_len + 1) {
+ TRACE_ERROR("too long DCID", QUIC_EV_CONN_RXPKT);
+ goto leave;
+ }
+
+ if (dcid_len) {
+ /* Check that the length of this received DCID matches the CID lengths
+ * of our implementation for non Initials packets only.
+ */
+ if (pkt->version && pkt->version->num &&
+ pkt->type != QUIC_PACKET_TYPE_INITIAL &&
+ pkt->type != QUIC_PACKET_TYPE_0RTT &&
+ dcid_len != QUIC_HAP_CID_LEN) {
+ TRACE_ERROR("wrong DCID length", QUIC_EV_CONN_RXPKT);
+ goto leave;
+ }
+
+ memcpy(pkt->dcid.data, *pos, dcid_len);
+ }
+
+ pkt->dcid.len = dcid_len;
+ *pos += dcid_len;
+
+ /* Source Connection ID Length */
+ scid_len = *(*pos)++;
+ if (scid_len > QUIC_CID_MAXLEN || end - *pos < scid_len) {
+ TRACE_ERROR("too long SCID", QUIC_EV_CONN_RXPKT);
+ goto leave;
+ }
+
+ if (scid_len)
+ memcpy(pkt->scid.data, *pos, scid_len);
+ pkt->scid.len = scid_len;
+ *pos += scid_len;
+
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
+ return ret;
+}
+
+/* Insert <pkt> RX packet in its <qel> RX packets tree */
+static void qc_pkt_insert(struct quic_conn *qc,
+ struct quic_rx_packet *pkt, struct quic_enc_level *qel)
+{
+ TRACE_ENTER(QUIC_EV_CONN_RXPKT, qc);
+
+ pkt->pn_node.key = pkt->pn;
+ quic_rx_packet_refinc(pkt);
+ eb64_insert(&qel->rx.pkts, &pkt->pn_node);
+
+ TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
+}
+
+/* Try to remove the header protection of <pkt> QUIC packet with <beg> the
+ * address of the packet first byte, using the keys from encryption level <el>.
+ *
+ * If header protection has been successfully removed, packet data are copied
+ * into <qc> Rx buffer. If <el> secrets are not yet available, the copy is also
+ * proceeded, and the packet is inserted into <qc> protected packets tree. In
+ * both cases, packet can now be considered handled by the <qc> connection.
+ *
+ * If header protection cannot be removed due to <el> secrets already
+ * discarded, no operation is conducted.
+ *
+ * Returns 1 on success : packet data is now handled by the connection. On
+ * error 0 is returned : packet should be dropped by the caller.
+ */
+static int qc_try_rm_hp(struct quic_conn *qc, struct quic_rx_packet *pkt,
+ unsigned char *beg, struct quic_enc_level **el)
+{
+ int ret = 0;
+ unsigned char *pn = NULL; /* Packet number field */
+ enum quic_tls_enc_level tel;
+ struct quic_enc_level *qel;
+ /* Only for traces. */
+
+ TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
+ BUG_ON(!pkt->pn_offset);
+
+ /* The packet number is here. This is also the start minus
+ * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
+ * protection.
+ */
+ pn = beg + pkt->pn_offset;
+
+ tel = quic_packet_type_enc_level(pkt->type);
+ qel = qc_quic_enc_level(qc, tel);
+ if (!qel) {
+ struct quic_enc_level **qc_qel = qel_to_qel_addr(qc, tel);
+ struct quic_pktns **qc_pktns = qel_to_quic_pktns(qc, tel);
+
+ if (!qc_enc_level_alloc(qc, qc_pktns, qc_qel, quic_to_ssl_enc_level(tel))) {
+ TRACE_PROTO("Could not allocated an encryption level", QUIC_EV_CONN_ADDDATA, qc);
+ goto out;
+ }
+
+ qel = *qc_qel;
+ }
+
+ if (qc_qel_may_rm_hp(qc, qel)) {
+ struct quic_tls_ctx *tls_ctx =
+ qc_select_tls_ctx(qc, qel, pkt->type, pkt->version);
+
+ /* Note that the following function enables us to unprotect the packet
+ * number and its length subsequently used to decrypt the entire
+ * packets.
+ */
+ if (!qc_do_rm_hp(qc, pkt, tls_ctx,
+ qel->pktns->rx.largest_pn, pn, beg)) {
+ TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
+ goto out;
+ }
+
+ qc_handle_spin_bit(qc, pkt, qel);
+ /* The AAD includes the packet number field. */
+ pkt->aad_len = pkt->pn_offset + pkt->pnl;
+ if (pkt->len - pkt->aad_len < QUIC_TLS_TAG_LEN) {
+ TRACE_PROTO("Too short packet", QUIC_EV_CONN_TRMHP, qc);
+ goto out;
+ }
+
+ TRACE_PROTO("RX hp removed", QUIC_EV_CONN_TRMHP, qc, pkt);
+ }
+ else {
+ TRACE_PROTO("RX hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
+ LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
+ quic_rx_packet_refinc(pkt);
+ }
+
+ *el = qel;
+ /* No reference counter incrementation here!!! */
+ LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
+ memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
+ pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
+ b_add(&qc->rx.buf, pkt->len);
+
+ ret = 1;
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc);
+ return ret;
+}
+
+/* Parse a QUIC packet header starting at <pos> position without exceeding <end>.
+ * Version and type are stored in <pkt> packet instance. Type is set to unknown
+ * on two occasions : for unsupported version, in this case version field is
+ * set to NULL; for Version Negotiation packet with version number set to 0.
+ *
+ * Returns 1 on success else 0.
+ */
+int qc_parse_hd_form(struct quic_rx_packet *pkt,
+ unsigned char **pos, const unsigned char *end)
+{
+ uint32_t version;
+ int ret = 0;
+ const unsigned char byte0 = **pos;
+
+ TRACE_ENTER(QUIC_EV_CONN_RXPKT);
+ pkt->version = NULL;
+ pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
+
+ (*pos)++;
+ if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
+ unsigned char type =
+ (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
+
+ /* Version */
+ if (!quic_read_uint32(&version, (const unsigned char **)pos, end)) {
+ TRACE_ERROR("could not read the packet version", QUIC_EV_CONN_RXPKT);
+ goto out;
+ }
+
+ pkt->version = qc_supported_version(version);
+ if (version && pkt->version) {
+ if (version != QUIC_PROTOCOL_VERSION_2) {
+ pkt->type = type;
+ }
+ else {
+ switch (type) {
+ case 0:
+ pkt->type = QUIC_PACKET_TYPE_RETRY;
+ break;
+ case 1:
+ pkt->type = QUIC_PACKET_TYPE_INITIAL;
+ break;
+ case 2:
+ pkt->type = QUIC_PACKET_TYPE_0RTT;
+ break;
+ case 3:
+ pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
+ break;
+ }
+ }
+ }
+ }
+ else {
+ if (byte0 & QUIC_PACKET_SPIN_BIT)
+ pkt->flags |= QUIC_FL_RX_PACKET_SPIN_BIT;
+ pkt->type = QUIC_PACKET_TYPE_SHORT;
+ }
+
+ ret = 1;
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_RXPKT);
+ return ret;
+}
+
+/* QUIC server only function.
+ *
+ * Check the validity of the Retry token from Initial packet <pkt>. <dgram> is
+ * the UDP datagram containing <pkt> and <l> is the listener instance on which
+ * it was received. If the token is valid, the ODCID of <qc> QUIC connection
+ * will be put into <odcid>. <qc> is used to retrieve the QUIC version needed
+ * to validate the token but it can be NULL : in this case the version will be
+ * retrieved from the packet.
+ *
+ * Return 1 if succeeded, 0 if not.
+ */
+
+static int quic_retry_token_check(struct quic_rx_packet *pkt,
+ struct quic_dgram *dgram,
+ struct listener *l,
+ struct quic_conn *qc,
+ struct quic_cid *odcid)
+{
+ struct proxy *prx;
+ struct quic_counters *prx_counters;
+ int ret = 0;
+ unsigned char *token = pkt->token;
+ const uint64_t tokenlen = pkt->token_len;
+ unsigned char buf[128];
+ unsigned char aad[sizeof(uint32_t) + QUIC_CID_MAXLEN +
+ sizeof(in_port_t) + sizeof(struct in6_addr) +
+ QUIC_CID_MAXLEN];
+ size_t aadlen;
+ const unsigned char *salt;
+ unsigned char key[QUIC_TLS_KEY_LEN];
+ unsigned char iv[QUIC_TLS_IV_LEN];
+ const unsigned char *sec = (const unsigned char *)global.cluster_secret;
+ size_t seclen = strlen(global.cluster_secret);
+ EVP_CIPHER_CTX *ctx = NULL;
+ const EVP_CIPHER *aead = EVP_aes_128_gcm();
+ const struct quic_version *qv = qc ? qc->original_version :
+ pkt->version;
+
+ TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
+
+ /* The caller must ensure this. */
+ BUG_ON(!global.cluster_secret || !pkt->token_len);
+
+ prx = l->bind_conf->frontend;
+ prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
+
+ if (*pkt->token != QUIC_TOKEN_FMT_RETRY) {
+ /* TODO: New token check */
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
+ goto leave;
+ }
+
+ if (sizeof buf < tokenlen) {
+ TRACE_ERROR("too short buffer", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ /* The token is made of the token format byte, the ODCID prefixed by its one byte
+ * length, the creation timestamp, an AEAD TAG, and finally
+ * the random bytes used to derive the secret to encrypt the token.
+ */
+ if (tokenlen < 2 + QUIC_ODCID_MINLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN ||
+ tokenlen > 2 + QUIC_CID_MAXLEN + sizeof(uint32_t) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN) {
+ TRACE_ERROR("invalid token length", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ aadlen = quic_generate_retry_token_aad(aad, qv->num, &pkt->dcid, &pkt->scid, &dgram->saddr);
+ salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
+ if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
+ salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
+ TRACE_ERROR("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ if (!quic_tls_rx_ctx_init(&ctx, aead, key)) {
+ TRACE_ERROR("quic_tls_rx_ctx_init() failed", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ /* The token is prefixed by a one-byte length format which is not ciphered. */
+ if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
+ ctx, aead, key, iv)) {
+ TRACE_ERROR("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ if (parse_retry_token(qc, buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
+ TRACE_ERROR("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ EVP_CIPHER_CTX_free(ctx);
+
+ ret = 1;
+ HA_ATOMIC_INC(&prx_counters->retry_validated);
+
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
+ return ret;
+
+ err:
+ HA_ATOMIC_INC(&prx_counters->retry_error);
+ if (ctx)
+ EVP_CIPHER_CTX_free(ctx);
+ goto leave;
+}
+
+/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is an
+ * INITIAL or 0RTT type, we may have to use client address <saddr> if an ODCID
+ * is used.
+ *
+ * Returns the instance or NULL if not found.
+ */
+static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
+ struct listener *l,
+ struct sockaddr_storage *saddr,
+ int *new_tid)
+{
+ struct quic_conn *qc = NULL;
+ struct ebmb_node *node;
+ struct quic_connection_id *conn_id;
+ struct quic_cid_tree *tree;
+ uint conn_id_tid;
+
+ TRACE_ENTER(QUIC_EV_CONN_RXPKT);
+ *new_tid = -1;
+
+ /* First look into DCID tree. */
+ tree = &quic_cid_trees[_quic_cid_tree_idx(pkt->dcid.data)];
+ HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
+ node = ebmb_lookup(&tree->root, pkt->dcid.data, pkt->dcid.len);
+
+ /* If not found on an Initial/0-RTT packet, it could be because an
+ * ODCID is reused by the client. Calculate the derived CID value to
+ * retrieve it from the DCID tree.
+ */
+ if (!node && (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
+ pkt->type == QUIC_PACKET_TYPE_0RTT)) {
+ const struct quic_cid derive_cid = quic_derive_cid(&pkt->dcid, saddr);
+
+ HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
+
+ tree = &quic_cid_trees[quic_cid_tree_idx(&derive_cid)];
+ HA_RWLOCK_RDLOCK(QC_CID_LOCK, &tree->lock);
+ node = ebmb_lookup(&tree->root, derive_cid.data, derive_cid.len);
+ }
+
+ if (!node)
+ goto end;
+
+ conn_id = ebmb_entry(node, struct quic_connection_id, node);
+ conn_id_tid = HA_ATOMIC_LOAD(&conn_id->tid);
+ if (conn_id_tid != tid) {
+ *new_tid = conn_id_tid;
+ goto end;
+ }
+ qc = conn_id->qc;
+
+ end:
+ HA_RWLOCK_RDUNLOCK(QC_CID_LOCK, &tree->lock);
+ TRACE_LEAVE(QUIC_EV_CONN_RXPKT, qc);
+ return qc;
+}
+
+/* Check that all the bytes between <pos> included and <end> address
+ * excluded are null. This is the responsibility of the caller to
+ * check that there is at least one byte between <pos> end <end>.
+ * Return 1 if this all the bytes are null, 0 if not.
+ */
+static inline int quic_padding_check(const unsigned char *pos,
+ const unsigned char *end)
+{
+ while (pos < end && !*pos)
+ pos++;
+
+ return pos == end;
+}
+
+/* Find the associated connection to the packet <pkt> or create a new one if
+ * this is an Initial packet. <dgram> is the datagram containing the packet and
+ * <l> is the listener instance on which it was received.
+ *
+ * By default, <new_tid> is set to -1. However, if thread affinity has been
+ * chanbed, it will be set to its new thread ID.
+ *
+ * Returns the quic-conn instance or NULL if not found or thread affinity
+ * changed.
+ */
+static struct quic_conn *quic_rx_pkt_retrieve_conn(struct quic_rx_packet *pkt,
+ struct quic_dgram *dgram,
+ struct listener *l,
+ int *new_tid)
+{
+ struct quic_cid token_odcid = { .len = 0 };
+ struct quic_conn *qc = NULL;
+ struct proxy *prx;
+ struct quic_counters *prx_counters;
+
+ TRACE_ENTER(QUIC_EV_CONN_LPKT);
+
+ *new_tid = -1;
+
+ prx = l->bind_conf->frontend;
+ prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
+
+ qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr, new_tid);
+
+ /* If connection already created or rebinded on another thread. */
+ if (!qc && *new_tid != -1 && tid != *new_tid)
+ goto out;
+
+ if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
+ BUG_ON(!pkt->version); /* This must not happen. */
+
+ if (global.cluster_secret && pkt->token_len) {
+ if (!quic_retry_token_check(pkt, dgram, l, qc, &token_odcid))
+ goto err;
+ }
+
+ if (!qc) {
+ struct quic_cid_tree *tree;
+ struct ebmb_node *node;
+ struct quic_connection_id *conn_id;
+ int ipv4;
+
+ if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
+ HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
+ TRACE_PROTO("Initial without token, sending retry",
+ QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
+ TRACE_ERROR("Error during Retry generation",
+ QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ goto out;
+ }
+
+ HA_ATOMIC_INC(&prx_counters->retry_sent);
+ goto out;
+ }
+
+ /* RFC 9000 7.2. Negotiating Connection IDs:
+ * When an Initial packet is sent by a client that has not previously
+ * received an Initial or Retry packet from the server, the client
+ * populates the Destination Connection ID field with an unpredictable
+ * value. This Destination Connection ID MUST be at least 8 bytes in length.
+ */
+ if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
+ TRACE_PROTO("dropped packet",
+ QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ goto err;
+ }
+
+ pkt->saddr = dgram->saddr;
+ ipv4 = dgram->saddr.ss_family == AF_INET;
+
+ /* Generate the first connection CID. This is derived from the client
+ * ODCID and address. This allows to retrieve the connection from the
+ * ODCID without storing it in the CID tree. This is an interesting
+ * optimization as the client is expected to stop using its ODCID in
+ * favor of our generated value.
+ */
+ conn_id = new_quic_cid(NULL, NULL, &pkt->dcid, &pkt->saddr);
+ if (!conn_id)
+ goto err;
+
+ qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &token_odcid,
+ conn_id, &dgram->daddr, &pkt->saddr, 1,
+ !!pkt->token_len, l);
+ if (qc == NULL) {
+ pool_free(pool_head_quic_connection_id, conn_id);
+ goto err;
+ }
+
+ tree = &quic_cid_trees[quic_cid_tree_idx(&conn_id->cid)];
+ HA_RWLOCK_WRLOCK(QC_CID_LOCK, &tree->lock);
+ node = ebmb_insert(&tree->root, &conn_id->node, conn_id->cid.len);
+ if (node != &conn_id->node) {
+ pool_free(pool_head_quic_connection_id, conn_id);
+
+ conn_id = ebmb_entry(node, struct quic_connection_id, node);
+ *new_tid = HA_ATOMIC_LOAD(&conn_id->tid);
+ quic_conn_release(qc);
+ qc = NULL;
+ }
+ else {
+ /* From here, <qc> is the correct connection for this <pkt> Initial
+ * packet. <conn_id> must be inserted in the CIDs tree for this
+ * connection.
+ */
+ eb64_insert(&qc->cids, &conn_id->seq_num);
+ /* Initialize the next CID sequence number to be used for this connection. */
+ qc->next_cid_seq_num = 1;
+ }
+ HA_RWLOCK_WRUNLOCK(QC_CID_LOCK, &tree->lock);
+
+ if (*new_tid != -1)
+ goto out;
+
+ HA_ATOMIC_INC(&prx_counters->half_open_conn);
+ }
+ }
+ else if (!qc) {
+ TRACE_PROTO("RX non Initial pkt without connection", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
+ TRACE_ERROR("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
+ return qc;
+
+ err:
+ if (qc)
+ qc->cntrs.dropped_pkt++;
+ else
+ HA_ATOMIC_INC(&prx_counters->dropped_pkt);
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT);
+ return NULL;
+}
+
+/* Parse a QUIC packet starting at <pos>. Data won't be read after <end> even
+ * if the packet is incomplete. This function will populate fields of <pkt>
+ * instance, most notably its length. <dgram> is the UDP datagram which
+ * contains the parsed packet. <l> is the listener instance on which it was
+ * received.
+ *
+ * Returns 0 on success else non-zero. Packet length is guaranteed to be set to
+ * the real packet value or to cover all data between <pos> and <end> : this is
+ * useful to reject a whole datagram.
+ */
+static int quic_rx_pkt_parse(struct quic_rx_packet *pkt,
+ unsigned char *pos, const unsigned char *end,
+ struct quic_dgram *dgram, struct listener *l)
+{
+ const unsigned char *beg = pos;
+ struct proxy *prx;
+ struct quic_counters *prx_counters;
+
+ TRACE_ENTER(QUIC_EV_CONN_LPKT);
+
+ prx = l->bind_conf->frontend;
+ prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
+
+ if (end <= pos) {
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+
+ /* Fixed bit */
+ if (!(*pos & QUIC_PACKET_FIXED_BIT)) {
+ if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
+ quic_padding_check(pos, end)) {
+ /* Some browsers may pad the remaining datagram space with null bytes.
+ * That is what we called add padding out of QUIC packets. Such
+ * datagrams must be considered as valid. But we can only consume
+ * the remaining space.
+ */
+ pkt->len = end - pos;
+ goto drop_silent;
+ }
+
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+
+ /* Header form */
+ if (!qc_parse_hd_form(pkt, &pos, end)) {
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+
+ if (pkt->type != QUIC_PACKET_TYPE_SHORT) {
+ uint64_t len;
+ TRACE_PROTO("long header packet received", QUIC_EV_CONN_LPKT);
+
+ if (!quic_packet_read_long_header(&pos, end, pkt)) {
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+
+ /* When multiple QUIC packets are coalesced on the same UDP datagram,
+ * they must have the same DCID.
+ */
+ if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
+ (pkt->dcid.len != dgram->dcid_len ||
+ memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+
+ /* Retry of Version Negotiation packets are only sent by servers */
+ if (pkt->type == QUIC_PACKET_TYPE_RETRY ||
+ (pkt->version && !pkt->version->num)) {
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+
+ /* RFC9000 6. Version Negotiation */
+ if (!pkt->version) {
+ /* unsupported version, send Negotiation packet */
+ if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
+ TRACE_ERROR("VN packet not sent", QUIC_EV_CONN_LPKT);
+ goto drop_silent;
+ }
+
+ TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
+ goto drop_silent;
+ }
+
+ /* For Initial packets, and for servers (QUIC clients connections),
+ * there is no Initial connection IDs storage.
+ */
+ if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
+ uint64_t token_len;
+
+ if (!quic_dec_int(&token_len, (const unsigned char **)&pos, end) ||
+ end - pos < token_len) {
+ TRACE_PROTO("Packet dropped",
+ QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ goto drop;
+ }
+
+ /* TODO Retry should be automatically activated if
+ * suspect network usage is detected.
+ */
+ if (global.cluster_secret && !token_len) {
+ if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
+ TRACE_PROTO("Initial without token, sending retry",
+ QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ if (send_retry(l->rx.fd, &dgram->saddr, pkt, pkt->version)) {
+ TRACE_PROTO("Error during Retry generation",
+ QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ goto drop_silent;
+ }
+
+ HA_ATOMIC_INC(&prx_counters->retry_sent);
+ goto drop_silent;
+ }
+ }
+ else if (!global.cluster_secret && token_len) {
+ /* Impossible case: a token was received without configured
+ * cluster secret.
+ */
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT,
+ NULL, NULL, NULL, pkt->version);
+ goto drop;
+ }
+
+ pkt->token = pos;
+ pkt->token_len = token_len;
+ pos += pkt->token_len;
+ }
+ else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
+ if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
+ TRACE_PROTO("Packet dropped",
+ QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ goto drop;
+ }
+ }
+
+ if (!quic_dec_int(&len, (const unsigned char **)&pos, end) ||
+ end - pos < len) {
+ TRACE_PROTO("Packet dropped",
+ QUIC_EV_CONN_LPKT, NULL, NULL, NULL, pkt->version);
+ goto drop;
+ }
+
+ /* Packet Number is stored here. Packet Length totalizes the
+ * rest of the content.
+ */
+ pkt->pn_offset = pos - beg;
+ pkt->len = pkt->pn_offset + len;
+
+ /* RFC 9000. Initial Datagram Size
+ *
+ * A server MUST discard an Initial packet that is carried in a UDP datagram
+ * with a payload that is smaller than the smallest allowed maximum datagram
+ * size of 1200 bytes.
+ */
+ if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
+ dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
+ TRACE_PROTO("RX too short datagram with an Initial packet", QUIC_EV_CONN_LPKT);
+ HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
+ goto drop;
+ }
+
+ /* Interrupt parsing after packet length retrieval : this
+ * ensures that only the packet is dropped but not the whole
+ * datagram.
+ */
+ if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
+ TRACE_PROTO("RX 0-RTT packet not supported", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+ }
+ else {
+ TRACE_PROTO("RX short header packet", QUIC_EV_CONN_LPKT);
+ if (end - pos < QUIC_HAP_CID_LEN) {
+ TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+
+ memcpy(pkt->dcid.data, pos, QUIC_HAP_CID_LEN);
+ pkt->dcid.len = QUIC_HAP_CID_LEN;
+
+ /* When multiple QUIC packets are coalesced on the same UDP datagram,
+ * they must have the same DCID.
+ */
+ if (!(pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST) &&
+ (pkt->dcid.len != dgram->dcid_len ||
+ memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
+ TRACE_PROTO("RX pkt dropped", QUIC_EV_CONN_LPKT);
+ goto drop;
+ }
+
+ pos += QUIC_HAP_CID_LEN;
+
+ pkt->pn_offset = pos - beg;
+ /* A short packet is the last one of a UDP datagram. */
+ pkt->len = end - beg;
+ }
+
+ TRACE_PROTO("RX pkt parsed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT);
+ return 0;
+
+ drop:
+ HA_ATOMIC_INC(&prx_counters->dropped_pkt);
+ drop_silent:
+ if (!pkt->len)
+ pkt->len = end - beg;
+ TRACE_PROTO("RX pkt parsing failed", QUIC_EV_CONN_LPKT, NULL, pkt, NULL, pkt->version);
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT);
+ return -1;
+}
+
+/* Check if received packet <pkt> should be drop due to <qc> already in closing
+ * state. This can be true if a CONNECTION_CLOSE has already been emitted for
+ * this connection.
+ *
+ * Returns false if connection is not in closing state else true. The caller
+ * should drop the whole datagram in the last case to not mess up <qc>
+ * CONNECTION_CLOSE rate limit counter.
+ */
+static int qc_rx_check_closing(struct quic_conn *qc,
+ struct quic_rx_packet *pkt)
+{
+ if (!(qc->flags & QUIC_FL_CONN_CLOSING))
+ return 0;
+
+ TRACE_STATE("Closing state connection", QUIC_EV_CONN_LPKT, qc, NULL, NULL, pkt->version);
+
+ /* Check if CONNECTION_CLOSE rate reemission is reached. */
+ if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
+ qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
+ qc->nb_pkt_for_cc++;
+ qc->nb_pkt_since_cc = 0;
+ }
+
+ return 1;
+}
+
+/* React to a connection migration initiated on <qc> by a client with the new
+ * path addresses <peer_addr>/<local_addr>.
+ *
+ * Returns 0 on success else non-zero.
+ */
+static int qc_handle_conn_migration(struct quic_conn *qc,
+ const struct sockaddr_storage *peer_addr,
+ const struct sockaddr_storage *local_addr)
+{
+ TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
+
+ /* RFC 9000. Connection Migration
+ *
+ * If the peer sent the disable_active_migration transport parameter,
+ * an endpoint also MUST NOT send packets (including probing packets;
+ * see Section 9.1) from a different local address to the address the peer
+ * used during the handshake, unless the endpoint has acted on a
+ * preferred_address transport parameter from the peer.
+ */
+ if (qc->li->bind_conf->quic_params.disable_active_migration) {
+ TRACE_ERROR("Active migration was disabled, datagram dropped", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ /* RFC 9000 9. Connection Migration
+ *
+ * The design of QUIC relies on endpoints retaining a stable address for
+ * the duration of the handshake. An endpoint MUST NOT initiate
+ * connection migration before the handshake is confirmed, as defined in
+ * Section 4.1.2 of [QUIC-TLS].
+ */
+ if (qc->state < QUIC_HS_ST_COMPLETE) {
+ TRACE_STATE("Connection migration during handshake rejected", QUIC_EV_CONN_LPKT, qc);
+ goto err;
+ }
+
+ /* RFC 9000 9. Connection Migration
+ *
+ * TODO
+ * An endpoint MUST
+ * perform path validation (Section 8.2) if it detects any change to a
+ * peer's address, unless it has previously validated that address.
+ */
+
+ /* Update quic-conn owned socket if in used.
+ * TODO try to reuse it instead of closing and opening a new one.
+ */
+ if (qc_test_fd(qc)) {
+ /* TODO try to reuse socket instead of closing it and opening a new one. */
+ TRACE_STATE("Connection migration detected, allocate a new connection socket", QUIC_EV_CONN_LPKT, qc);
+ qc_release_fd(qc, 1);
+ /* TODO need to adjust <jobs> on socket allocation failure. */
+ qc_alloc_fd(qc, local_addr, peer_addr);
+ }
+
+ qc->local_addr = *local_addr;
+ qc->peer_addr = *peer_addr;
+ qc->cntrs.conn_migration_done++;
+
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
+ return 0;
+
+ err:
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
+ return 1;
+}
+
+/* Release the memory for the RX packets which are no more referenced
+ * and consume their payloads which have been copied to the RX buffer
+ * for the connection.
+ * Always succeeds.
+ */
+static void quic_rx_pkts_del(struct quic_conn *qc)
+{
+ struct quic_rx_packet *pkt, *pktback;
+
+ list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
+ TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
+ "pkt #%lld(type=%d,len=%llu,rawlen=%llu,refcnt=%u) (diff: %zd)",
+ (long long)pkt->pn_node.key,
+ pkt->type, (ull)pkt->len, (ull)pkt->raw_len, pkt->refcnt,
+ (unsigned char *)b_head(&qc->rx.buf) - pkt->data);
+ if (pkt->data != (unsigned char *)b_head(&qc->rx.buf)) {
+ size_t cdata;
+
+ cdata = b_contig_data(&qc->rx.buf, 0);
+ TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
+ "cdata=%llu *b_head()=0x%x", (ull)cdata, *b_head(&qc->rx.buf));
+ if (cdata && !*b_head(&qc->rx.buf)) {
+ /* Consume the remaining data */
+ b_del(&qc->rx.buf, cdata);
+ }
+ break;
+ }
+
+ if (pkt->refcnt)
+ break;
+
+ b_del(&qc->rx.buf, pkt->raw_len);
+ LIST_DELETE(&pkt->qc_rx_pkt_list);
+ pool_free(pool_head_quic_rx_packet, pkt);
+ }
+
+ /* In frequent cases the buffer will be emptied at this stage. */
+ b_realign_if_empty(&qc->rx.buf);
+}
+
+/* Handle a parsed packet <pkt> by the connection <qc>. Data will be copied
+ * into <qc> receive buffer after header protection removal procedure.
+ *
+ * <dgram> must be set to the datagram which contains the QUIC packet. <beg>
+ * must point to packet buffer first byte.
+ *
+ * <tasklist_head> may be non-NULL when the caller treat several datagrams for
+ * different quic-conn. In this case, each quic-conn tasklet will be appended
+ * to it in order to be woken up after the current task.
+ *
+ * The caller can safely removed the packet data. If packet refcount was not
+ * incremented by this function, it means that the connection did not handled
+ * it and it should be freed by the caller.
+ */
+static void qc_rx_pkt_handle(struct quic_conn *qc, struct quic_rx_packet *pkt,
+ struct quic_dgram *dgram, unsigned char *beg,
+ struct list **tasklist_head)
+{
+ const struct quic_version *qv = pkt->version;
+ struct quic_enc_level *qel = NULL;
+ size_t b_cspace;
+
+ TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
+ TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
+
+ if (pkt->flags & QUIC_FL_RX_PACKET_DGRAM_FIRST &&
+ qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
+ TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
+ QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
+ TRACE_DEVEL("needs to wakeup the timer task after the amplification limit was reached",
+ QUIC_EV_CONN_LPKT, qc);
+ /* Reset the anti-amplification bit. It will be set again
+ * when sending the next packet if reached again.
+ */
+ qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
+ qc_set_timer(qc);
+ if (qc->timer_task && tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
+ task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
+ }
+
+ /* Drop asap packet whose packet number space is discarded. */
+ if (quic_tls_pkt_type_pktns_dcd(qc, pkt->type)) {
+ TRACE_PROTO("Discarded packet number space", QUIC_EV_CONN_TRMHP, qc);
+ goto drop_silent;
+ }
+
+ if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
+ TRACE_PROTO("Connection error",
+ QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
+ goto out;
+ }
+
+ pkt->raw_len = pkt->len;
+ quic_rx_pkts_del(qc);
+ b_cspace = b_contig_space(&qc->rx.buf);
+ if (b_cspace < pkt->len) {
+ TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_LPKT, qc, 0, 0, 0,
+ "bspace=%llu pkt->len=%llu", (ull)b_cspace, (ull)pkt->len);
+ /* Do not consume buf if space not at the end. */
+ if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
+ TRACE_PROTO("Packet dropped",
+ QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
+ qc->cntrs.dropped_pkt_bufoverrun++;
+ goto drop_silent;
+ }
+
+ /* Let us consume the remaining contiguous space. */
+ if (b_cspace) {
+ b_putchr(&qc->rx.buf, 0x00);
+ b_cspace--;
+ }
+ b_add(&qc->rx.buf, b_cspace);
+ if (b_contig_space(&qc->rx.buf) < pkt->len) {
+ TRACE_PROTO("Too big packet",
+ QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
+ qc->cntrs.dropped_pkt_bufoverrun++;
+ goto drop_silent;
+ }
+ }
+
+ if (!qc_try_rm_hp(qc, pkt, beg, &qel)) {
+ TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
+ goto drop;
+ }
+
+ TRACE_DATA("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
+ if (pkt->aad_len)
+ qc_pkt_insert(qc, pkt, qel);
+ out:
+ *tasklist_head = tasklet_wakeup_after(*tasklist_head,
+ qc->wait_event.tasklet);
+
+ drop_silent:
+ TRACE_PROTO("RX pkt", QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL);
+ return;
+
+ drop:
+ qc->cntrs.dropped_pkt++;
+ TRACE_PROTO("packet drop", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
+}
+
+/* Handle a new <dgram> received. Parse each QUIC packets and copied their
+ * content to a quic-conn instance. The datagram content can be released after
+ * this function.
+ *
+ * If datagram has been received on a quic-conn owned FD, <from_qc> must be set
+ * to the connection instance. <li> is the attached listener. The caller is
+ * responsible to ensure that the first packet is destined to this connection
+ * by comparing CIDs.
+ *
+ * If datagram has been received on a receiver FD, <from_qc> will be NULL. This
+ * function will thus retrieve the connection from the CID tree or allocate a
+ * new one if possible. <li> is the listener attached to the receiver.
+ *
+ * Returns 0 on success else non-zero. If an error happens, some packets from
+ * the datagram may not have been parsed.
+ */
+int quic_dgram_parse(struct quic_dgram *dgram, struct quic_conn *from_qc,
+ struct listener *li)
+{
+ struct quic_rx_packet *pkt;
+ struct quic_conn *qc = NULL;
+ unsigned char *pos, *end;
+ struct list *tasklist_head = NULL;
+
+ TRACE_ENTER(QUIC_EV_CONN_LPKT);
+
+ pos = dgram->buf;
+ end = pos + dgram->len;
+ do {
+ pkt = pool_alloc(pool_head_quic_rx_packet);
+ if (!pkt) {
+ TRACE_ERROR("RX packet allocation failed", QUIC_EV_CONN_LPKT);
+ goto err;
+ }
+
+ LIST_INIT(&pkt->qc_rx_pkt_list);
+ pkt->version = NULL;
+ pkt->type = QUIC_PACKET_TYPE_UNKNOWN;
+ pkt->pn_offset = 0;
+ pkt->len = 0;
+ pkt->raw_len = 0;
+ pkt->token = NULL;
+ pkt->token_len = 0;
+ pkt->aad_len = 0;
+ pkt->data = NULL;
+ pkt->pn_node.key = (uint64_t)-1;
+ pkt->refcnt = 0;
+ pkt->flags = 0;
+ pkt->time_received = now_ms;
+
+ /* Set flag if pkt is the first one in dgram. */
+ if (pos == dgram->buf)
+ pkt->flags |= QUIC_FL_RX_PACKET_DGRAM_FIRST;
+
+ quic_rx_packet_refinc(pkt);
+ if (quic_rx_pkt_parse(pkt, pos, end, dgram, li))
+ goto next;
+
+ /* Search quic-conn instance for first packet of the datagram.
+ * quic_rx_packet_parse() is responsible to discard packets
+ * with different DCID as the first one in the same datagram.
+ */
+ if (!qc) {
+ int new_tid = -1;
+
+ qc = from_qc ? from_qc : quic_rx_pkt_retrieve_conn(pkt, dgram, li, &new_tid);
+ /* qc is NULL if receiving a non Initial packet for an
+ * unknown connection or on connection affinity rebind.
+ */
+ if (!qc) {
+ if (new_tid >= 0) {
+ MT_LIST_APPEND(&quic_dghdlrs[new_tid].dgrams,
+ &dgram->handler_list);
+ tasklet_wakeup(quic_dghdlrs[new_tid].task);
+ goto out;
+ }
+
+ /* Skip the entire datagram. */
+ pkt->len = end - pos;
+ goto next;
+ }
+
+ dgram->qc = qc;
+ }
+
+ if (qc->flags & QUIC_FL_CONN_AFFINITY_CHANGED)
+ qc_finalize_affinity_rebind(qc);
+
+ if (qc_rx_check_closing(qc, pkt)) {
+ /* Skip the entire datagram. */
+ pkt->len = end - pos;
+ goto next;
+ }
+
+ /* Detect QUIC connection migration. */
+ if (ipcmp(&qc->peer_addr, &dgram->saddr, 1)) {
+ if (qc_handle_conn_migration(qc, &dgram->saddr, &dgram->daddr)) {
+ /* Skip the entire datagram. */
+ TRACE_ERROR("error during connection migration, datagram dropped", QUIC_EV_CONN_LPKT, qc);
+ pkt->len = end - pos;
+ goto next;
+ }
+ }
+
+ qc_rx_pkt_handle(qc, pkt, dgram, pos, &tasklist_head);
+
+ next:
+ pos += pkt->len;
+ quic_rx_packet_refdec(pkt);
+
+ /* Free rejected packets */
+ if (!pkt->refcnt) {
+ BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
+ pool_free(pool_head_quic_rx_packet, pkt);
+ }
+ } while (pos < end);
+
+ /* Increasing the received bytes counter by the UDP datagram length
+ * if this datagram could be associated to a connection.
+ */
+ if (dgram->qc)
+ dgram->qc->rx.bytes += dgram->len;
+
+ /* This must never happen. */
+ BUG_ON(pos > end);
+ BUG_ON(pos < end || pos > dgram->buf + dgram->len);
+ /* Mark this datagram as consumed */
+ HA_ATOMIC_STORE(&dgram->buf, NULL);
+
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT);
+ return 0;
+
+ err:
+ /* Mark this datagram as consumed as maybe at least some packets were parsed. */
+ HA_ATOMIC_STORE(&dgram->buf, NULL);
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT);
+ return -1;
+}
+
+/*
+ * Local variables:
+ * c-indent-level: 8
+ * c-basic-offset: 8
+ * End:
+ */
--- /dev/null
+/*
+ * QUIC protocol implementation. Lower layer with internal features implemented
+ * here such as QUIC encryption, idle timeout, acknowledgement and
+ * retransmission.
+ *
+ * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ */
+
+#include <haproxy/quic_tx.h>
+
+#include <haproxy/pool.h>
+#include <haproxy/trace.h>
+#include <haproxy/quic_sock.h>
+#include <haproxy/quic_tls.h>
+#include <haproxy/quic_trace.h>
+#include <haproxy/ssl_sock-t.h>
+
+#define TRACE_SOURCE &trace_quic
+
+DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet", sizeof(struct quic_tx_packet));
+
+static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
+ struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
+ struct list *frms, struct quic_conn *qc,
+ const struct quic_version *ver, size_t dglen, int pkt_type,
+ int must_ack, int padding, int probe, int cc, int *err);
+
+static void quic_packet_encrypt(unsigned char *payload, size_t payload_len,
+ unsigned char *aad, size_t aad_len, uint64_t pn,
+ struct quic_tls_ctx *tls_ctx, struct quic_conn *qc,
+ int *fail)
+{
+ unsigned char iv[QUIC_TLS_IV_LEN];
+ unsigned char *tx_iv = tls_ctx->tx.iv;
+ size_t tx_iv_sz = tls_ctx->tx.ivlen;
+ struct enc_debug_info edi;
+
+ TRACE_ENTER(QUIC_EV_CONN_ENCPKT, qc);
+ *fail = 0;
+
+ quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn);
+
+ if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
+ tls_ctx->tx.ctx, tls_ctx->tx.aead, iv)) {
+ TRACE_ERROR("QUIC packet encryption failed", QUIC_EV_CONN_ENCPKT, qc);
+ *fail = 1;
+ enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
+ }
+
+ TRACE_LEAVE(QUIC_EV_CONN_ENCPKT, qc);
+}
+
+/* Free <pkt> TX packet and its attached frames.
+ * This is the responsibility of the caller to remove this packet of
+ * any data structure it was possibly attached to.
+ */
+static inline void free_quic_tx_packet(struct quic_conn *qc,
+ struct quic_tx_packet *pkt)
+{
+ struct quic_frame *frm, *frmbak;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ if (!pkt)
+ goto leave;
+
+ list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
+ qc_frm_free(qc, &frm);
+ pool_free(pool_head_quic_tx_packet, pkt);
+
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+}
+
+/* Free the TX packets of <pkts> list */
+void free_quic_tx_pkts(struct quic_conn *qc, struct list *pkts)
+{
+ struct quic_tx_packet *pkt, *tmp;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ list_for_each_entry_safe(pkt, tmp, pkts, list) {
+ LIST_DELETE(&pkt->list);
+ eb64_delete(&pkt->pn_node);
+ free_quic_tx_packet(qc, pkt);
+ }
+
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+}
+
+/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
+ * for <qc> QUIC connection.
+ * This is a best effort function which never fails even if no memory could be
+ * allocated to duplicate these frames.
+ */
+static void qc_dup_pkt_frms(struct quic_conn *qc,
+ struct list *pkt_frm_list, struct list *out_frm_list)
+{
+ struct quic_frame *frm, *frmbak;
+ struct list tmp = LIST_HEAD_INIT(tmp);
+
+ TRACE_ENTER(QUIC_EV_CONN_PRSAFRM, qc);
+
+ list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
+ struct quic_frame *dup_frm, *origin;
+
+ if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
+ TRACE_DEVEL("already acknowledged frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
+ continue;
+ }
+
+ switch (frm->type) {
+ case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
+ {
+ struct qf_stream *strm_frm = &frm->stream;
+ struct eb64_node *node = NULL;
+ struct qc_stream_desc *stream_desc;
+
+ node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
+ if (!node) {
+ TRACE_DEVEL("ignored frame for a released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
+ continue;
+ }
+
+ stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
+ /* Do not resend this frame if in the "already acked range" */
+ if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
+ TRACE_DEVEL("ignored frame in already acked range",
+ QUIC_EV_CONN_PRSAFRM, qc, frm);
+ continue;
+ }
+ else if (strm_frm->offset.key < stream_desc->ack_offset) {
+ uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
+
+ qc_stream_frm_mv_fwd(frm, diff);
+ TRACE_DEVEL("updated partially acked frame",
+ QUIC_EV_CONN_PRSAFRM, qc, frm);
+ }
+
+ strm_frm->dup = 1;
+ break;
+ }
+
+ default:
+ break;
+ }
+
+ /* If <frm> is already a copy of another frame, we must take
+ * its original frame as source for the copy.
+ */
+ origin = frm->origin ? frm->origin : frm;
+ dup_frm = qc_frm_dup(origin);
+ if (!dup_frm) {
+ TRACE_ERROR("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
+ break;
+ }
+
+ TRACE_DEVEL("built probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
+ if (origin->pkt) {
+ TRACE_DEVEL("duplicated from packet", QUIC_EV_CONN_PRSAFRM,
+ qc, NULL, &origin->pkt->pn_node.key);
+ }
+ else {
+ /* <origin> is a frame which was sent from a packet detected as lost. */
+ TRACE_DEVEL("duplicated from lost packet", QUIC_EV_CONN_PRSAFRM, qc);
+ }
+
+ LIST_APPEND(&tmp, &dup_frm->list);
+ }
+
+ LIST_SPLICE(out_frm_list, &tmp);
+
+ TRACE_LEAVE(QUIC_EV_CONN_PRSAFRM, qc);
+}
+
+/* Boolean function which return 1 if <pkt> TX packet is only made of
+ * already acknowledged frame.
+ */
+static inline int qc_pkt_with_only_acked_frms(struct quic_tx_packet *pkt)
+{
+ struct quic_frame *frm;
+
+ list_for_each_entry(frm, &pkt->frms, list)
+ if (!(frm->flags & QUIC_FL_TX_FRAME_ACKED))
+ return 0;
+
+ return 1;
+}
+
+/* Prepare a fast retransmission from <qel> encryption level */
+static void qc_prep_fast_retrans(struct quic_conn *qc,
+ struct quic_pktns *pktns,
+ struct list *frms1, struct list *frms2)
+{
+ struct eb_root *pkts = &pktns->tx.pkts;
+ struct list *frms = frms1;
+ struct eb64_node *node;
+ struct quic_tx_packet *pkt;
+
+ TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
+
+ BUG_ON(frms1 == frms2);
+
+ pkt = NULL;
+ node = eb64_first(pkts);
+ start:
+ while (node) {
+ struct quic_tx_packet *p;
+
+ p = eb64_entry(node, struct quic_tx_packet, pn_node);
+ node = eb64_next(node);
+ /* Skip the empty and coalesced packets */
+ TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
+ "--> pn=%llu (%d %d %d)", (ull)p->pn_node.key,
+ LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED),
+ qc_pkt_with_only_acked_frms(p));
+ if (!LIST_ISEMPTY(&p->frms) && !qc_pkt_with_only_acked_frms(p)) {
+ pkt = p;
+ break;
+ }
+ }
+
+ if (!pkt)
+ goto leave;
+
+ /* When building a packet from another one, the field which may increase the
+ * packet size is the packet number. And the maximum increase is 4 bytes.
+ */
+ if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
+ pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
+ qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
+ TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
+ goto leave;
+ }
+
+ TRACE_PROTO("duplicating packet", QUIC_EV_CONN_SPPKTS, qc, pkt);
+ qc_dup_pkt_frms(qc, &pkt->frms, frms);
+ if (frms == frms1 && frms2) {
+ frms = frms2;
+ goto start;
+ }
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
+}
+
+/* Prepare a fast retransmission during a handshake after a client
+ * has resent Initial packets. According to the RFC a server may retransmit
+ * Initial packets send them coalescing with others (Handshake here).
+ * (Listener only function).
+ */
+void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
+ struct list *ifrms, struct list *hfrms)
+{
+ struct list itmp = LIST_HEAD_INIT(itmp);
+ struct list htmp = LIST_HEAD_INIT(htmp);
+
+ struct quic_enc_level *iqel = qc->iel;
+ struct quic_enc_level *hqel = qc->hel;
+ struct quic_enc_level *qel = iqel;
+ struct eb_root *pkts;
+ struct eb64_node *node;
+ struct quic_tx_packet *pkt;
+ struct list *tmp = &itmp;
+
+ TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
+ start:
+ pkt = NULL;
+ pkts = &qel->pktns->tx.pkts;
+ node = eb64_first(pkts);
+ /* Skip the empty packet (they have already been retransmitted) */
+ while (node) {
+ struct quic_tx_packet *p;
+
+ p = eb64_entry(node, struct quic_tx_packet, pn_node);
+ TRACE_PRINTF(TRACE_LEVEL_PROTO, QUIC_EV_CONN_SPPKTS, qc, 0, 0, 0,
+ "--> pn=%llu (%d %d)", (ull)p->pn_node.key,
+ LIST_ISEMPTY(&p->frms), !!(p->flags & QUIC_FL_TX_PACKET_COALESCED));
+ if (!LIST_ISEMPTY(&p->frms) && !(p->flags & QUIC_FL_TX_PACKET_COALESCED) &&
+ !qc_pkt_with_only_acked_frms(p)) {
+ pkt = p;
+ break;
+ }
+
+ node = eb64_next(node);
+ }
+
+ if (!pkt)
+ goto end;
+
+ /* When building a packet from another one, the field which may increase the
+ * packet size is the packet number. And the maximum increase is 4 bytes.
+ */
+ if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
+ size_t dglen = pkt->len + 4;
+ size_t may_send = 3 * qc->rx.bytes - qc->tx.prep_bytes;
+
+ dglen += pkt->next ? pkt->next->len + 4 : 0;
+ if (dglen > may_send) {
+ qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
+ TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt);
+ if (pkt->next)
+ TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_SPPKTS, qc, pkt->next);
+ if (qel == iqel && may_send >= QUIC_INITIAL_PACKET_MINLEN)
+ TRACE_PROTO("will probe Initial packet number space", QUIC_EV_CONN_SPPKTS, qc);
+ goto end;
+ }
+ }
+
+ qel->pktns->tx.pto_probe += 1;
+
+ /* No risk to loop here, #packet per datagram is bounded */
+ requeue:
+ TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
+ qc_dup_pkt_frms(qc, &pkt->frms, tmp);
+ if (qel == iqel) {
+ if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
+ pkt = pkt->next;
+ tmp = &htmp;
+ hqel->pktns->tx.pto_probe += 1;
+ TRACE_DEVEL("looping for next packet", QUIC_EV_CONN_SPPKTS, qc);
+ goto requeue;
+ }
+ }
+
+ end:
+ LIST_SPLICE(ifrms, &itmp);
+ LIST_SPLICE(hfrms, &htmp);
+
+ TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
+}
+
+/* Allocate Tx buffer from <qc> quic-conn if needed.
+ *
+ * Returns allocated buffer or NULL on error.
+ */
+struct buffer *qc_txb_alloc(struct quic_conn *qc)
+{
+ struct buffer *buf = &qc->tx.buf;
+ if (!b_alloc(buf))
+ return NULL;
+
+ return buf;
+}
+
+/* Free Tx buffer from <qc> if it is empty. */
+void qc_txb_release(struct quic_conn *qc)
+{
+ struct buffer *buf = &qc->tx.buf;
+
+ /* For the moment sending function is responsible to purge the buffer
+ * entirely. It may change in the future but this requires to be able
+ * to reuse old data.
+ * For the momemt we do not care to leave data in the buffer for
+ * a connection which is supposed to be killed asap.
+ */
+ BUG_ON_HOT(buf && b_data(buf));
+
+ if (!b_data(buf)) {
+ b_free(buf);
+ offer_buffers(NULL, 1);
+ }
+}
+
+/* Commit a datagram payload written into <buf> of length <length>. <first_pkt>
+ * must contains the address of the first packet stored in the payload.
+ *
+ * Caller is responsible that there is enough space in the buffer.
+ */
+static void qc_txb_store(struct buffer *buf, uint16_t length,
+ struct quic_tx_packet *first_pkt)
+{
+ const size_t hdlen = sizeof(uint16_t) + sizeof(void *);
+ BUG_ON_HOT(b_contig_space(buf) < hdlen); /* this must not happen */
+
+ write_u16(b_tail(buf), length);
+ write_ptr(b_tail(buf) + sizeof(length), first_pkt);
+ b_add(buf, hdlen + length);
+}
+
+/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
+ * with <frms> as ack-eliciting frame list to send, 0 if not.
+ * <cc> must equal to 1 if an immediate close was asked, 0 if not.
+ * <probe> must equalt to 1 if a probing packet is required, 0 if not.
+ * Also set <*must_ack> to inform the caller if an acknowledgement should be sent.
+ */
+static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
+ struct quic_enc_level *qel, int cc, int probe,
+ int *must_ack)
+{
+ int force_ack = qel == qc->iel || qel == qc->hel;
+ int nb_aepkts_since_last_ack = qel->pktns->rx.nb_aepkts_since_last_ack;
+
+ /* An acknowledgement must be sent if this has been forced by the caller,
+ * typically during the handshake when the packets must be acknowledged as
+ * soon as possible. This is also the case when the ack delay timer has been
+ * triggered, or at least every QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK packets.
+ */
+ *must_ack = (qc->flags & QUIC_FL_CONN_ACK_TIMER_FIRED) ||
+ ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
+ (force_ack || nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK));
+
+ TRACE_PRINTF(TRACE_LEVEL_DEVELOPER, QUIC_EV_CONN_PHPKTS, qc, 0, 0, 0,
+ "has_sec=%d cc=%d probe=%d must_ack=%d frms=%d prep_in_fligh=%llu cwnd=%llu",
+ quic_tls_has_tx_sec(qel), cc, probe, *must_ack, LIST_ISEMPTY(frms),
+ (ullong)qc->path->prep_in_flight, (ullong)qc->path->cwnd);
+
+ /* Do not build any more packet if the TX secrets are not available or
+ * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
+ * and if there is no more packets to send upon PTO expiration
+ * and if there is no more ack-eliciting frames to send or in flight
+ * congestion control limit is reached for prepared data
+ */
+ if (!quic_tls_has_tx_sec(qel) ||
+ (!cc && !probe && !*must_ack &&
+ (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
+ return 0;
+ }
+
+ return 1;
+}
+
+/* Prepare as much as possible QUIC packets for sending from prebuilt frames
+ * <frms>. Each packet is stored in a distinct datagram written to <buf>.
+ *
+ * Each datagram is prepended by a two fields header : the datagram length and
+ * the address of the packet contained in the datagram.
+ *
+ * Returns the number of bytes prepared in packets if succeeded (may be 0), or
+ * -1 if something wrong happened.
+ */
+static int qc_prep_app_pkts(struct quic_conn *qc, struct buffer *buf,
+ struct list *frms)
+{
+ int ret = -1;
+ struct quic_enc_level *qel;
+ unsigned char *end, *pos;
+ struct quic_tx_packet *pkt;
+ size_t total;
+ /* Each datagram is prepended with its length followed by the address
+ * of the first packet in the datagram.
+ */
+ const size_t dg_headlen = sizeof(uint16_t) + sizeof(pkt);
+
+ TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
+
+ qel = qc->ael;
+ total = 0;
+ pos = (unsigned char *)b_tail(buf);
+ while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen) {
+ int err, probe, cc, must_ack;
+
+ TRACE_PROTO("TX prep app pkts", QUIC_EV_CONN_PHPKTS, qc, qel, frms);
+ probe = 0;
+ cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
+ /* We do not probe if an immediate close was asked */
+ if (!cc)
+ probe = qel->pktns->tx.pto_probe;
+
+ if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack))
+ break;
+
+ /* Leave room for the datagram header */
+ pos += dg_headlen;
+ if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
+ end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
+ }
+ else {
+ end = pos + qc->path->mtu;
+ }
+
+ pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0,
+ QUIC_PACKET_TYPE_SHORT, must_ack, 0, probe, cc, &err);
+ switch (err) {
+ case -2:
+ // trace already emitted by function above
+ goto leave;
+ case -1:
+ /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
+ * MTU, we are here because of the congestion control window. There is
+ * no need to try to reuse this buffer.
+ */
+ TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
+ goto out;
+ default:
+ break;
+ }
+
+ /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
+ BUG_ON(!pkt);
+
+ if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
+ pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
+
+ total += pkt->len;
+
+ /* Write datagram header. */
+ qc_txb_store(buf, pkt->len, pkt);
+ }
+
+ out:
+ ret = total;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
+ return ret;
+}
+
+/* Free all frames in <l> list. In addition also remove all these frames
+ * from the original ones if they are the results of duplications.
+ */
+static inline void qc_free_frm_list(struct quic_conn *qc, struct list *l)
+{
+ struct quic_frame *frm, *frmbak;
+
+ list_for_each_entry_safe(frm, frmbak, l, list) {
+ LIST_DEL_INIT(&frm->ref);
+ qc_frm_free(qc, &frm);
+ }
+}
+
+/* Free <pkt> TX packet and all the packets coalesced to it. */
+static inline void qc_free_tx_coalesced_pkts(struct quic_conn *qc,
+ struct quic_tx_packet *p)
+{
+ struct quic_tx_packet *pkt, *nxt_pkt;
+
+ for (pkt = p; pkt; pkt = nxt_pkt) {
+ qc_free_frm_list(qc, &pkt->frms);
+ nxt_pkt = pkt->next;
+ pool_free(pool_head_quic_tx_packet, pkt);
+ }
+}
+
+/* Purge <buf> TX buffer from its prepare packets. */
+static void qc_purge_tx_buf(struct quic_conn *qc, struct buffer *buf)
+{
+ while (b_contig_data(buf, 0)) {
+ uint16_t dglen;
+ struct quic_tx_packet *pkt;
+ size_t headlen = sizeof dglen + sizeof pkt;
+
+ dglen = read_u16(b_head(buf));
+ pkt = read_ptr(b_head(buf) + sizeof dglen);
+ qc_free_tx_coalesced_pkts(qc, pkt);
+ b_del(buf, dglen + headlen);
+ }
+
+ BUG_ON(b_data(buf));
+}
+
+/* Send datagrams stored in <buf>.
+ *
+ * This function returns 1 for success. On error, there is several behavior
+ * depending on underlying sendto() error :
+ * - for an unrecoverable error, 0 is returned and connection is killed.
+ * - a transient error is handled differently if connection has its owned
+ * socket. If this is the case, 0 is returned and socket is subscribed on the
+ * poller. The other case is assimilated to a success case with 1 returned.
+ * Remaining data are purged from the buffer and will eventually be detected
+ * as lost which gives the opportunity to retry sending.
+ */
+int qc_send_ppkts(struct buffer *buf, struct ssl_sock_ctx *ctx)
+{
+ int ret = 0;
+ struct quic_conn *qc;
+ char skip_sendto = 0;
+
+ qc = ctx->qc;
+ TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
+ while (b_contig_data(buf, 0)) {
+ unsigned char *pos;
+ struct buffer tmpbuf = { };
+ struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
+ uint16_t dglen;
+ size_t headlen = sizeof dglen + sizeof first_pkt;
+ unsigned int time_sent;
+
+ pos = (unsigned char *)b_head(buf);
+ dglen = read_u16(pos);
+ BUG_ON_HOT(!dglen); /* this should not happen */
+
+ pos += sizeof dglen;
+ first_pkt = read_ptr(pos);
+ pos += sizeof first_pkt;
+ tmpbuf.area = (char *)pos;
+ tmpbuf.size = tmpbuf.data = dglen;
+
+ TRACE_PROTO("TX dgram", QUIC_EV_CONN_SPPKTS, qc);
+ /* If sendto is on error just skip the call to it for the rest
+ * of the loop but continue to purge the buffer. Data will be
+ * transmitted when QUIC packets are detected as lost on our
+ * side.
+ *
+ * TODO use fd-monitoring to detect when send operation can be
+ * retry. This should improve the bandwidth without relying on
+ * retransmission timer. However, it requires a major rework on
+ * quic-conn fd management.
+ */
+ if (!skip_sendto) {
+ int ret = qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0);
+ if (ret < 0) {
+ TRACE_ERROR("sendto fatal error", QUIC_EV_CONN_SPPKTS, qc, first_pkt);
+ qc_kill_conn(qc);
+ qc_free_tx_coalesced_pkts(qc, first_pkt);
+ b_del(buf, dglen + headlen);
+ qc_purge_tx_buf(qc, buf);
+ goto leave;
+ }
+ else if (!ret) {
+ /* Connection owned socket : poller will wake us up when transient error is cleared. */
+ if (qc_test_fd(qc)) {
+ TRACE_ERROR("sendto error, subscribe to poller", QUIC_EV_CONN_SPPKTS, qc);
+ goto leave;
+ }
+
+ /* No connection owned-socket : rely on retransmission to retry sending. */
+ skip_sendto = 1;
+ TRACE_ERROR("sendto error, simulate sending for the rest of data", QUIC_EV_CONN_SPPKTS, qc);
+ }
+ }
+
+ b_del(buf, dglen + headlen);
+ qc->tx.bytes += tmpbuf.data;
+ time_sent = now_ms;
+
+ for (pkt = first_pkt; pkt; pkt = next_pkt) {
+ /* RFC 9000 14.1 Initial datagram size
+ * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
+ * Initial packets to at least the smallest allowed maximum datagram size of
+ * 1200 bytes.
+ */
+ qc->cntrs.sent_pkt++;
+ BUG_ON_HOT(pkt->type == QUIC_PACKET_TYPE_INITIAL &&
+ (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
+ dglen < QUIC_INITIAL_PACKET_MINLEN);
+
+ pkt->time_sent = time_sent;
+ if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
+ pkt->pktns->tx.time_of_last_eliciting = time_sent;
+ qc->path->ifae_pkts++;
+ if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
+ qc_idle_timer_rearm(qc, 0, 0);
+ }
+ if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
+ (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
+ qc->flags |= QUIC_FL_CONN_CLOSING;
+ qc_detach_th_ctx_list(qc, 1);
+
+ /* RFC 9000 10.2. Immediate Close:
+ * The closing and draining connection states exist to ensure
+ * that connections close cleanly and that delayed or reordered
+ * packets are properly discarded. These states SHOULD persist
+ * for at least three times the current PTO interval...
+ *
+ * Rearm the idle timeout only one time when entering closing
+ * state.
+ */
+ qc_idle_timer_do_rearm(qc, 0);
+ if (qc->timer_task) {
+ task_destroy(qc->timer_task);
+ qc->timer_task = NULL;
+ }
+ }
+ qc->path->in_flight += pkt->in_flight_len;
+ pkt->pktns->tx.in_flight += pkt->in_flight_len;
+ if (pkt->in_flight_len)
+ qc_set_timer(qc);
+ TRACE_PROTO("TX pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
+ next_pkt = pkt->next;
+ quic_tx_packet_refinc(pkt);
+ eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
+ }
+ }
+
+ ret = 1;
+leave:
+ TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
+
+ return ret;
+}
+
+/* Build all the frames which must be sent just after the handshake have succeeded.
+ * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
+ * a HANDSHAKE_DONE frame.
+ * Return 1 if succeeded, 0 if not.
+ */
+int quic_build_post_handshake_frames(struct quic_conn *qc)
+{
+ int ret = 0, max;
+ struct quic_enc_level *qel;
+ struct quic_frame *frm, *frmbak;
+ struct list frm_list = LIST_HEAD_INIT(frm_list);
+ struct eb64_node *node;
+
+ TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
+
+ qel = qc->ael;
+ /* Only servers must send a HANDSHAKE_DONE frame. */
+ if (qc_is_listener(qc)) {
+ frm = qc_frm_alloc(QUIC_FT_HANDSHAKE_DONE);
+ if (!frm) {
+ TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
+ goto leave;
+ }
+
+ LIST_APPEND(&frm_list, &frm->list);
+ }
+
+ /* Initialize <max> connection IDs minus one: there is
+ * already one connection ID used for the current connection. Also limit
+ * the number of connection IDs sent to the peer to 4 (3 from this function
+ * plus 1 for the current connection.
+ * Note that active_connection_id_limit >= 2: this has been already checked
+ * when receiving this parameter.
+ */
+ max = QUIC_MIN(qc->tx.params.active_connection_id_limit - 1, (uint64_t)3);
+ while (max--) {
+ struct quic_connection_id *conn_id;
+
+ frm = qc_frm_alloc(QUIC_FT_NEW_CONNECTION_ID);
+ if (!frm) {
+ TRACE_ERROR("frame allocation error", QUIC_EV_CONN_IO_CB, qc);
+ goto err;
+ }
+
+ conn_id = new_quic_cid(&qc->cids, qc, NULL, NULL);
+ if (!conn_id) {
+ qc_frm_free(qc, &frm);
+ TRACE_ERROR("CID allocation error", QUIC_EV_CONN_IO_CB, qc);
+ goto err;
+ }
+
+ /* TODO To prevent CID tree locking, all CIDs created here
+ * could be allocated at the same time as the first one.
+ */
+ quic_cid_insert(conn_id);
+
+ quic_connection_id_to_frm_cpy(frm, conn_id);
+ LIST_APPEND(&frm_list, &frm->list);
+ }
+
+ LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
+ qc->flags &= ~QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS;
+
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc);
+ return ret;
+
+ err:
+ /* free the frames */
+ list_for_each_entry_safe(frm, frmbak, &frm_list, list)
+ qc_frm_free(qc, &frm);
+
+ /* The first CID sequence number value used to allocated CIDs by this function is 1,
+ * 0 being the sequence number of the CID for this connection.
+ */
+ node = eb64_lookup_ge(&qc->cids, 1);
+ while (node) {
+ struct quic_connection_id *conn_id;
+
+ conn_id = eb64_entry(node, struct quic_connection_id, seq_num);
+ if (conn_id->seq_num.key >= max)
+ break;
+
+ node = eb64_next(node);
+ quic_cid_delete(conn_id);
+
+ eb64_delete(&conn_id->seq_num);
+ pool_free(pool_head_quic_connection_id, conn_id);
+ }
+ goto leave;
+}
+
+/* Flush txbuf for <qc> connection. This must be called prior to a packet
+ * preparation when txbuf contains older data. A send will be conducted for
+ * these data.
+ *
+ * Returns 1 on success : buffer is empty and can be use for packet
+ * preparation. On error 0 is returned.
+ */
+int qc_purge_txbuf(struct quic_conn *qc, struct buffer *buf)
+{
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ /* This operation can only be conducted if txbuf is not empty. This
+ * case only happens for connection with their owned socket due to an
+ * older transient sendto() error.
+ */
+ BUG_ON(!qc_test_fd(qc));
+
+ if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
+ if (qc->flags & QUIC_FL_CONN_TO_KILL)
+ qc_txb_release(qc);
+ TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
+ return 0;
+ }
+
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return 1;
+}
+
+/* Try to send application frames from list <frms> on connection <qc>.
+ *
+ * Use qc_send_app_probing wrapper when probing with old data.
+ *
+ * Returns 1 on success. Some data might not have been sent due to congestion,
+ * in this case they are left in <frms> input list. The caller may subscribe on
+ * quic-conn to retry later.
+ *
+ * Returns 0 on critical error.
+ * TODO review and classify more distinctly transient from definitive errors to
+ * allow callers to properly handle it.
+ */
+int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
+{
+ int status = 0, ret;
+ struct buffer *buf;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ buf = qc_txb_alloc(qc);
+ if (!buf) {
+ TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
+ goto err;
+ }
+
+ if (b_data(buf) && !qc_purge_txbuf(qc, buf))
+ goto err;
+
+ /* Prepare and send packets until we could not further prepare packets. */
+ do {
+ /* Currently buf cannot be non-empty at this stage. Even if a
+ * previous sendto() has failed it is emptied to simulate
+ * packet emission and rely on QUIC lost detection to try to
+ * emit it.
+ */
+ BUG_ON_HOT(b_data(buf));
+ b_reset(buf);
+
+ ret = qc_prep_app_pkts(qc, buf, frms);
+
+ if (b_data(buf) && !qc_send_ppkts(buf, qc->xprt_ctx)) {
+ if (qc->flags & QUIC_FL_CONN_TO_KILL)
+ qc_txb_release(qc);
+ goto err;
+ }
+ } while (ret > 0);
+
+ qc_txb_release(qc);
+ if (ret < 0)
+ goto err;
+
+ status = 1;
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return status;
+
+ err:
+ TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TXPKT, qc);
+ return 0;
+}
+
+/* Try to send application frames from list <frms> on connection <qc>. Use this
+ * function when probing is required.
+ *
+ * Returns the result from qc_send_app_pkts function.
+ */
+static forceinline int qc_send_app_probing(struct quic_conn *qc,
+ struct list *frms)
+{
+ int ret;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ TRACE_PROTO("preparing old data (probing)", QUIC_EV_CONN_FRMLIST, qc, frms);
+ qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
+ ret = qc_send_app_pkts(qc, frms);
+ qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
+
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return ret;
+}
+
+/* Try to send application frames from list <frms> on connection <qc>. This
+ * function is provided for MUX upper layer usage only.
+ *
+ * Returns the result from qc_send_app_pkts function.
+ */
+int qc_send_mux(struct quic_conn *qc, struct list *frms)
+{
+ int ret;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+ BUG_ON(qc->mux_state != QC_MUX_READY); /* Only MUX can uses this function so it must be ready. */
+
+ if (qc->conn->flags & CO_FL_SOCK_WR_SH) {
+ qc->conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH;
+ TRACE_DEVEL("connection on error", QUIC_EV_CONN_TXPKT, qc);
+ return 0;
+ }
+
+ /* Try to send post handshake frames first unless on 0-RTT. */
+ if ((qc->flags & QUIC_FL_CONN_NEED_POST_HANDSHAKE_FRMS) &&
+ qc->state >= QUIC_HS_ST_COMPLETE) {
+ quic_build_post_handshake_frames(qc);
+ qc_send_app_pkts(qc, &qc->ael->pktns->tx.frms);
+ }
+
+ TRACE_STATE("preparing data (from MUX)", QUIC_EV_CONN_TXPKT, qc);
+ qc->flags |= QUIC_FL_CONN_TX_MUX_CONTEXT;
+ ret = qc_send_app_pkts(qc, frms);
+ qc->flags &= ~QUIC_FL_CONN_TX_MUX_CONTEXT;
+
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return ret;
+}
+
+/* Return the encryption level following the one which contains <el> list head
+ * depending on <retrans> TX mode (retranmission or not).
+ */
+static inline struct quic_enc_level *qc_list_next_qel(struct list *el, int retrans)
+{
+ return !retrans ? LIST_NEXT(el, struct quic_enc_level *, list) :
+ LIST_NEXT(el, struct quic_enc_level *, retrans);
+}
+
+/* Return the encryption level following <qel> depending on <retrans> TX mode
+ * (retranmission or not).
+ */
+static inline struct quic_enc_level *qc_next_qel(struct quic_enc_level *qel, int retrans)
+{
+ struct list *el = !retrans ? &qel->list : &qel->retrans;
+
+ return qc_list_next_qel(el, retrans);
+}
+
+/* Return 1 if <qel> is at the head of its list, 0 if not. */
+static inline int qc_qel_is_head(struct quic_enc_level *qel, struct list *l,
+ int retrans)
+{
+ return !retrans ? &qel->list == l : &qel->retrans == l;
+}
+
+/* Prepare as much as possible QUIC datagrams/packets for sending from <qels>
+ * list of encryption levels. Several packets can be coalesced into a single
+ * datagram. The result is written into <buf>. Note that if <qels> is NULL,
+ * the encryption levels which will be used are those currently allocated
+ * and attached to the connection.
+ *
+ * Each datagram is prepended by a two fields header : the datagram length and
+ * the address of first packet in the datagram.
+ *
+ * Returns the number of bytes prepared in datragrams/packets if succeeded
+ * (may be 0), or -1 if something wrong happened.
+ */
+int qc_prep_hpkts(struct quic_conn *qc, struct buffer *buf, struct list *qels)
+{
+ int ret, retrans, padding;
+ struct quic_tx_packet *first_pkt, *prv_pkt;
+ unsigned char *end, *pos;
+ const size_t dg_headlen = sizeof(uint16_t) + sizeof(first_pkt);
+ uint16_t dglen;
+ size_t total;
+ struct list *qel_list;
+ struct quic_enc_level *qel;
+
+ TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
+ /* Currently qc_prep_pkts() does not handle buffer wrapping so the
+ * caller must ensure that buf is reset.
+ */
+ BUG_ON_HOT(buf->head || buf->data);
+
+ ret = -1;
+ retrans = !!qels;
+ padding = 0;
+ first_pkt = prv_pkt = NULL;
+ end = pos = (unsigned char *)b_head(buf);
+ dglen = 0;
+ total = 0;
+
+ qel_list = qels ? qels : &qc->qel_list;
+ qel = qc_list_next_qel(qel_list, retrans);
+ while (!qc_qel_is_head(qel, qel_list, retrans)) {
+ struct quic_tls_ctx *tls_ctx;
+ const struct quic_version *ver;
+ struct list *frms, *next_frms;
+ struct quic_enc_level *next_qel;
+
+ if (qel == qc->eel) {
+ /* Next encryption level */
+ qel = qc_next_qel(qel, retrans);
+ continue;
+ }
+
+ if (qc->negotiated_version) {
+ ver = qc->negotiated_version;
+ if (qel == qc->iel)
+ tls_ctx = qc->nictx;
+ else
+ tls_ctx = &qel->tls_ctx;
+ }
+ else {
+ ver = qc->original_version;
+ tls_ctx = &qel->tls_ctx;
+ }
+
+ if (!qels)
+ frms = &qel->pktns->tx.frms;
+ else
+ frms = qel->retrans_frms;
+
+ next_qel = qc_next_qel(qel, retrans);
+ next_frms = qc_qel_is_head(next_qel, qel_list, retrans) ? NULL :
+ !qels ? &next_qel->pktns->tx.frms : next_qel->retrans_frms;
+
+ /* Build as much as datagrams at <qel> encryption level. */
+ while (b_contig_space(buf) >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
+ int err, probe, cc, must_ack;
+ enum quic_pkt_type pkt_type;
+ struct quic_tx_packet *cur_pkt;
+
+ TRACE_PROTO("TX prep pkts", QUIC_EV_CONN_PHPKTS, qc, qel);
+ probe = 0;
+ cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
+ /* We do not probe if an immediate close was asked */
+ if (!cc)
+ probe = qel->pktns->tx.pto_probe;
+
+ if (!qc_may_build_pkt(qc, frms, qel, cc, probe, &must_ack)) {
+ if (prv_pkt && qc_qel_is_head(next_qel, qel_list, retrans))
+ qc_txb_store(buf, dglen, first_pkt);
+
+ TRACE_DEVEL("next encryption level", QUIC_EV_CONN_PHPKTS, qc);
+ break;
+ }
+
+ if (!prv_pkt) {
+ /* Leave room for the datagram header */
+ pos += dg_headlen;
+ if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
+ end = pos + QUIC_MIN((uint64_t)qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
+ }
+ else {
+ end = pos + qc->path->mtu;
+ }
+ }
+
+ /* RFC 9000 14.1 Initial datagram size
+ * a server MUST expand the payload of all UDP datagrams carrying ack-eliciting
+ * Initial packets to at least the smallest allowed maximum datagram size of
+ * 1200 bytes.
+ *
+ * Ensure that no ack-eliciting packets are sent into too small datagrams
+ */
+ if (qel == qc->iel && !LIST_ISEMPTY(frms)) {
+ if (end - pos < QUIC_INITIAL_PACKET_MINLEN) {
+ TRACE_PROTO("No more enough room to build an Initial packet",
+ QUIC_EV_CONN_PHPKTS, qc);
+ break;
+ }
+
+ /* Pad this Initial packet if there is no ack-eliciting frames to send from
+ * the next packet number space.
+ */
+ if (!next_frms || LIST_ISEMPTY(next_frms))
+ padding = 1;
+ }
+
+ pkt_type = quic_enc_level_pkt_type(qc, qel);
+ cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
+ qc, ver, dglen, pkt_type,
+ must_ack, padding, probe, cc, &err);
+ switch (err) {
+ case -2:
+ // trace already emitted by function above
+ goto leave;
+ case -1:
+ /* If there was already a correct packet present, set the
+ * current datagram as prepared into <cbuf>.
+ */
+ if (prv_pkt)
+ qc_txb_store(buf, dglen, first_pkt);
+ TRACE_PROTO("could not prepare anymore packet", QUIC_EV_CONN_PHPKTS, qc, qel);
+ goto out;
+ default:
+ break;
+ }
+
+ /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
+ BUG_ON(!cur_pkt);
+
+ total += cur_pkt->len;
+ dglen += cur_pkt->len;
+
+ if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
+ cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
+
+ /* keep trace of the first packet in the datagram */
+ if (!first_pkt)
+ first_pkt = cur_pkt;
+
+ /* Attach the current one to the previous one and vice versa */
+ if (prv_pkt) {
+ prv_pkt->next = cur_pkt;
+ cur_pkt->prev = prv_pkt;
+ cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
+ }
+
+ /* If there is no more packet to build for this encryption level,
+ * select the next one <next_qel>, if any, to coalesce a packet in
+ * the same datagram, except if <qel> is the Application data
+ * encryption level which cannot be selected to do that.
+ */
+ if (LIST_ISEMPTY(frms) && qel != qc->ael &&
+ !qc_qel_is_head(next_qel, qel_list, retrans)) {
+ if (qel == qc->iel &&
+ (!qc_is_listener(qc) ||
+ cur_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING))
+ padding = 1;
+
+ prv_pkt = cur_pkt;
+ break;
+ }
+ else {
+ qc_txb_store(buf, dglen, first_pkt);
+ first_pkt = NULL;
+ dglen = 0;
+ padding = 0;
+ prv_pkt = NULL;
+ }
+ }
+
+ /* Next encryption level */
+ qel = next_qel;
+ }
+
+ out:
+ ret = total;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
+ return ret;
+}
+
+/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
+ * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
+ * QUIC connection. <old_data> is used as boolean to send data already sent but
+ * not already acknowledged (in flight).
+ * Returns 1 if succeeded, 0 if not.
+ */
+int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
+ struct quic_enc_level *qel1, struct quic_enc_level *qel2)
+{
+ int ret, status = 0;
+ struct buffer *buf = qc_txb_alloc(qc);
+ struct list qels = LIST_HEAD_INIT(qels);
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ if (!buf) {
+ TRACE_ERROR("buffer allocation failed", QUIC_EV_CONN_TXPKT, qc);
+ goto leave;
+ }
+
+ if (b_data(buf) && !qc_purge_txbuf(qc, buf))
+ goto out;
+
+ /* Currently buf cannot be non-empty at this stage. Even if a previous
+ * sendto() has failed it is emptied to simulate packet emission and
+ * rely on QUIC lost detection to try to emit it.
+ */
+ BUG_ON_HOT(b_data(buf));
+ b_reset(buf);
+
+ if (old_data) {
+ TRACE_STATE("old data for probing asked", QUIC_EV_CONN_TXPKT, qc);
+ qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
+ }
+
+ if (qel1) {
+ BUG_ON(LIST_INLIST(&qel1->retrans));
+ LIST_APPEND(&qels, &qel1->retrans);
+ }
+
+ if (qel2) {
+ BUG_ON(LIST_INLIST(&qel2->retrans));
+ LIST_APPEND(&qels, &qel2->retrans);
+ }
+
+ ret = qc_prep_hpkts(qc, buf, &qels);
+ if (ret == -1) {
+ qc_txb_release(qc);
+ goto out;
+ }
+
+ if (ret && !qc_send_ppkts(buf, qc->xprt_ctx)) {
+ if (qc->flags & QUIC_FL_CONN_TO_KILL)
+ qc_txb_release(qc);
+ goto out;
+ }
+
+ qc_txb_release(qc);
+ status = 1;
+
+ out:
+ if (qel1) {
+ LIST_DEL_INIT(&qel1->retrans);
+ qel1->retrans_frms = NULL;
+ }
+
+ if (qel2) {
+ LIST_DEL_INIT(&qel2->retrans);
+ qel2->retrans_frms = NULL;
+ }
+
+ TRACE_STATE("no more need old data for probing", QUIC_EV_CONN_TXPKT, qc);
+ qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return status;
+}
+
+/* Retransmit up to two datagrams depending on packet number space.
+ * Return 0 when failed, 0 if not.
+ */
+int qc_dgrams_retransmit(struct quic_conn *qc)
+{
+ int ret = 0;
+ struct quic_pktns *ipktns = qc->ipktns;
+ struct quic_pktns *hpktns = qc->hpktns;
+ struct quic_pktns *apktns = qc->apktns;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ /* Note that if the Initial packet number space is not discarded,
+ * this is also the case for the Handshake packet number space.
+ */
+ if (ipktns && (ipktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED)) {
+ int i;
+
+ for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
+ struct list ifrms = LIST_HEAD_INIT(ifrms);
+ struct list hfrms = LIST_HEAD_INIT(hfrms);
+ struct list qels = LIST_HEAD_INIT(qels);
+
+ qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
+ TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
+ TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
+ if (!LIST_ISEMPTY(&ifrms)) {
+ ipktns->tx.pto_probe = 1;
+ if (!LIST_ISEMPTY(&hfrms))
+ hpktns->tx.pto_probe = 1;
+ qc->iel->retrans_frms = &ifrms;
+ qc->hel->retrans_frms = &hfrms;
+ if (!qc_send_hdshk_pkts(qc, 1, qc->iel, qc->hel))
+ goto leave;
+ /* Put back unsent frames in their packet number spaces */
+ LIST_SPLICE(&ipktns->tx.frms, &ifrms);
+ LIST_SPLICE(&hpktns->tx.frms, &hfrms);
+ }
+ else {
+ /* We are in the case where the anti-amplification limit will be
+ * reached after having sent this datagram. There is no need to
+ * send more than one datagram.
+ */
+ ipktns->tx.pto_probe = 1;
+ qc->iel->retrans_frms = &ifrms;
+ if (!qc_send_hdshk_pkts(qc, 0, qc->iel, NULL))
+ goto leave;
+
+ break;
+ }
+ }
+ TRACE_STATE("no more need to probe Initial packet number space",
+ QUIC_EV_CONN_TXPKT, qc);
+ ipktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
+ hpktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
+ }
+ else {
+ int i;
+
+ if (hpktns && (hpktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED)) {
+ hpktns->tx.pto_probe = 0;
+ for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
+ struct list frms1 = LIST_HEAD_INIT(frms1);
+
+ qc_prep_fast_retrans(qc, hpktns, &frms1, NULL);
+ TRACE_DEVEL("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
+ if (!LIST_ISEMPTY(&frms1)) {
+ hpktns->tx.pto_probe = 1;
+ qc->hel->retrans_frms = &frms1;
+ if (!qc_send_hdshk_pkts(qc, 1, qc->hel, NULL))
+ goto leave;
+
+ /* Put back unsent frames into their packet number spaces */
+ LIST_SPLICE(&hpktns->tx.frms, &frms1);
+ }
+ }
+ TRACE_STATE("no more need to probe Handshake packet number space",
+ QUIC_EV_CONN_TXPKT, qc);
+ hpktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
+ }
+ else if (apktns && (apktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED)) {
+ struct list frms2 = LIST_HEAD_INIT(frms2);
+ struct list frms1 = LIST_HEAD_INIT(frms1);
+
+ apktns->tx.pto_probe = 0;
+ qc_prep_fast_retrans(qc, apktns, &frms1, &frms2);
+ TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
+ TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
+ if (!LIST_ISEMPTY(&frms1)) {
+ apktns->tx.pto_probe = 1;
+ if (!qc_send_app_probing(qc, &frms1)) {
+ qc_free_frm_list(qc, &frms2);
+ goto leave;
+ }
+
+ /* Put back unsent frames into their packet number spaces */
+ LIST_SPLICE(&apktns->tx.frms, &frms1);
+ }
+ if (!LIST_ISEMPTY(&frms2)) {
+ apktns->tx.pto_probe = 1;
+ if (!qc_send_app_probing(qc, &frms2))
+ goto leave;
+ /* Put back unsent frames into their packet number spaces */
+ LIST_SPLICE(&apktns->tx.frms, &frms2);
+ }
+ TRACE_STATE("no more need to probe 01RTT packet number space",
+ QUIC_EV_CONN_TXPKT, qc);
+ apktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
+ }
+ }
+
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return ret;
+}
+
+/* Returns a boolean if <qc> needs to emit frames for <qel> encryption level. */
+int qc_need_sending(struct quic_conn *qc, struct quic_enc_level *qel)
+{
+ return (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) ||
+ (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) ||
+ qel->pktns->tx.pto_probe ||
+ !LIST_ISEMPTY(&qel->pktns->tx.frms);
+}
+
+/* Return 1 if <qc> connection may probe the Initial packet number space, 0 if not.
+ * This is not the case if the remote peer address is not validated and if
+ * it cannot send at least QUIC_INITIAL_PACKET_MINLEN bytes.
+ */
+int qc_may_probe_ipktns(struct quic_conn *qc)
+{
+ return quic_peer_validated_addr(qc) ||
+ (int)(3 * qc->rx.bytes - qc->tx.prep_bytes) >= QUIC_INITIAL_PACKET_MINLEN;
+}
+
+/*
+ * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
+ * address <addr>.
+ * Implementation of RFC9000 6. Version Negotiation
+ *
+ * TODO implement a rate-limiting sending of Version Negotiation packets
+ *
+ * Returns 0 on success else non-zero
+ */
+int send_version_negotiation(int fd, struct sockaddr_storage *addr,
+ struct quic_rx_packet *pkt)
+{
+ char buf[256];
+ int ret = 0, i = 0, j;
+ uint32_t version;
+ const socklen_t addrlen = get_addr_len(addr);
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT);
+ /*
+ * header form
+ * long header, fixed bit to 0 for Version Negotiation
+ */
+ /* TODO: RAND_bytes() should be replaced? */
+ if (RAND_bytes((unsigned char *)buf, 1) != 1) {
+ TRACE_ERROR("RAND_bytes() error", QUIC_EV_CONN_TXPKT);
+ goto out;
+ }
+
+ buf[i++] |= '\x80';
+ /* null version for Version Negotiation */
+ buf[i++] = '\x00';
+ buf[i++] = '\x00';
+ buf[i++] = '\x00';
+ buf[i++] = '\x00';
+
+ /* source connection id */
+ buf[i++] = pkt->scid.len;
+ memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
+ i += pkt->scid.len;
+
+ /* destination connection id */
+ buf[i++] = pkt->dcid.len;
+ memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
+ i += pkt->dcid.len;
+
+ /* supported version */
+ for (j = 0; j < quic_versions_nb; j++) {
+ version = htonl(quic_versions[j].num);
+ memcpy(&buf[i], &version, sizeof(version));
+ i += sizeof(version);
+ }
+
+ if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
+ goto out;
+
+ ret = 1;
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
+ return !ret;
+}
+
+/* Send a stateless reset packet depending on <pkt> RX packet information
+ * from <fd> UDP socket to <dst>
+ * Return 1 if succeeded, 0 if not.
+ */
+int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
+ struct quic_rx_packet *rxpkt)
+{
+ int ret = 0, pktlen, rndlen;
+ unsigned char pkt[64];
+ const socklen_t addrlen = get_addr_len(dstaddr);
+ struct proxy *prx;
+ struct quic_counters *prx_counters;
+
+ TRACE_ENTER(QUIC_EV_STATELESS_RST);
+
+ prx = l->bind_conf->frontend;
+ prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
+ /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
+ * The resulting minimum size of 21 bytes does not guarantee that a Stateless
+ * Reset is difficult to distinguish from other packets if the recipient requires
+ * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
+ * that all packets it sends are at least 22 bytes longer than the minimum
+ * connection ID length that it requests the peer to include in its packets,
+ * adding PADDING frames as necessary. This ensures that any Stateless Reset
+ * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
+ * An endpoint that sends a Stateless Reset in response to a packet that is
+ * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
+ * than the packet it responds to.
+ */
+
+ /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
+ pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
+ pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
+ rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
+
+ /* Put a header of random bytes */
+ /* TODO: RAND_bytes() should be replaced */
+ if (RAND_bytes(pkt, rndlen) != 1) {
+ TRACE_ERROR("RAND_bytes() failed", QUIC_EV_STATELESS_RST);
+ goto leave;
+ }
+
+ /* Clear the most significant bit, and set the second one */
+ *pkt = (*pkt & ~0x80) | 0x40;
+ if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
+ rxpkt->dcid.data, rxpkt->dcid.len))
+ goto leave;
+
+ if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
+ goto leave;
+
+ ret = 1;
+ HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
+ TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
+ leave:
+ TRACE_LEAVE(QUIC_EV_STATELESS_RST);
+ return ret;
+}
+
+/* QUIC server only function.
+ * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
+ * This is the responsibility of the caller to check <aad> size is big enough
+ * to contain these data.
+ * Return the number of bytes copied to <aad>.
+ */
+int quic_generate_retry_token_aad(unsigned char *aad,
+ uint32_t version,
+ const struct quic_cid *dcid,
+ const struct quic_cid *scid,
+ const struct sockaddr_storage *addr)
+{
+ unsigned char *p;
+
+ p = aad;
+ *(uint32_t *)p = htonl(version);
+ p += sizeof version;
+ memcpy(p, dcid->data, dcid->len);
+ p += dcid->len;
+ p += quic_saddr_cpy(p, addr);
+ memcpy(p, scid->data, scid->len);
+ p += scid->len;
+
+ return p - aad;
+}
+
+/* QUIC server only function.
+ * Generate the token to be used in Retry packets. The token is written to
+ * <token> with <len> as length. <odcid> is the original destination connection
+ * ID and <dcid> is our side destination connection ID (or client source
+ * connection ID).
+ * Returns the length of the encoded token or 0 on error.
+ */
+static int quic_generate_retry_token(unsigned char *token, size_t len,
+ const uint32_t version,
+ const struct quic_cid *odcid,
+ const struct quic_cid *scid,
+ const struct quic_cid *dcid,
+ struct sockaddr_storage *addr)
+{
+ int ret = 0;
+ unsigned char *p;
+ unsigned char aad[sizeof(uint32_t) + QUIC_CID_MAXLEN +
+ sizeof(in_port_t) + sizeof(struct in6_addr) +
+ QUIC_CID_MAXLEN];
+ size_t aadlen;
+ unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
+ unsigned char key[QUIC_TLS_KEY_LEN];
+ unsigned char iv[QUIC_TLS_IV_LEN];
+ const unsigned char *sec = (const unsigned char *)global.cluster_secret;
+ size_t seclen = strlen(global.cluster_secret);
+ EVP_CIPHER_CTX *ctx = NULL;
+ const EVP_CIPHER *aead = EVP_aes_128_gcm();
+ uint32_t timestamp = (uint32_t)date.tv_sec;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT);
+
+ /* The token is made of the token format byte, the ODCID prefixed by its one byte
+ * length, the creation timestamp, an AEAD TAG, and finally
+ * the random bytes used to derive the secret to encrypt the token.
+ */
+ if (1 + odcid->len + 1 + sizeof(timestamp) + QUIC_TLS_TAG_LEN + QUIC_RETRY_TOKEN_SALTLEN > len)
+ goto err;
+
+ aadlen = quic_generate_retry_token_aad(aad, version, scid, dcid, addr);
+ /* TODO: RAND_bytes() should be replaced */
+ if (RAND_bytes(salt, sizeof salt) != 1) {
+ TRACE_ERROR("RAND_bytes()", QUIC_EV_CONN_TXPKT);
+ goto err;
+ }
+
+ if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
+ salt, sizeof salt, sec, seclen)) {
+ TRACE_ERROR("quic_tls_derive_retry_token_secret() failed", QUIC_EV_CONN_TXPKT);
+ goto err;
+ }
+
+ if (!quic_tls_tx_ctx_init(&ctx, aead, key)) {
+ TRACE_ERROR("quic_tls_tx_ctx_init() failed", QUIC_EV_CONN_TXPKT);
+ goto err;
+ }
+
+ /* Token build */
+ p = token;
+ *p++ = QUIC_TOKEN_FMT_RETRY,
+ *p++ = odcid->len;
+ memcpy(p, odcid->data, odcid->len);
+ p += odcid->len;
+ write_u32(p, htonl(timestamp));
+ p += sizeof timestamp;
+
+ /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
+ if (!quic_tls_encrypt(token + 1, p - token - 1, aad, aadlen, ctx, aead, iv)) {
+ TRACE_ERROR("quic_tls_encrypt() failed", QUIC_EV_CONN_TXPKT);
+ goto err;
+ }
+
+ p += QUIC_TLS_TAG_LEN;
+ memcpy(p, salt, sizeof salt);
+ p += sizeof salt;
+ EVP_CIPHER_CTX_free(ctx);
+
+ ret = p - token;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
+ return ret;
+
+ err:
+ if (ctx)
+ EVP_CIPHER_CTX_free(ctx);
+ goto leave;
+}
+
+/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
+ * the Initial <pkt> packet.
+ *
+ * Returns 0 on success else non-zero.
+ */
+int send_retry(int fd, struct sockaddr_storage *addr,
+ struct quic_rx_packet *pkt, const struct quic_version *qv)
+{
+ int ret = 0;
+ unsigned char buf[128];
+ int i = 0, token_len;
+ const socklen_t addrlen = get_addr_len(addr);
+ struct quic_cid scid;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT);
+
+ /* long header(1) | fixed bit(1) | packet type QUIC_PACKET_TYPE_RETRY(2) | unused random bits(4)*/
+ buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
+ (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT) |
+ statistical_prng_range(16);
+ /* version */
+ write_n32(&buf[i], qv->num);
+ i += sizeof(uint32_t);
+
+ /* Use the SCID from <pkt> for Retry DCID. */
+ buf[i++] = pkt->scid.len;
+ memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
+ i += pkt->scid.len;
+
+ /* Generate a new CID to be used as SCID for the Retry packet. */
+ scid.len = QUIC_HAP_CID_LEN;
+ /* TODO: RAND_bytes() should be replaced */
+ if (RAND_bytes(scid.data, scid.len) != 1) {
+ TRACE_ERROR("RAND_bytes() failed", QUIC_EV_CONN_TXPKT);
+ goto out;
+ }
+
+ buf[i++] = scid.len;
+ memcpy(&buf[i], scid.data, scid.len);
+ i += scid.len;
+
+ /* token */
+ if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
+ &pkt->dcid, &scid, &pkt->scid, addr))) {
+ TRACE_ERROR("quic_generate_retry_token() failed", QUIC_EV_CONN_TXPKT);
+ goto out;
+ }
+
+ i += token_len;
+
+ /* token integrity tag */
+ if ((sizeof(buf) - i < QUIC_TLS_TAG_LEN) ||
+ !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
+ pkt->dcid.len, buf, i, qv)) {
+ TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
+ goto out;
+ }
+
+ i += QUIC_TLS_TAG_LEN;
+
+ if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0) {
+ TRACE_ERROR("quic_tls_generate_retry_integrity_tag() failed", QUIC_EV_CONN_TXPKT);
+ goto out;
+ }
+
+ ret = 1;
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT);
+ return !ret;
+}
+
+/* This function builds into a buffer at <pos> position a QUIC long packet header,
+ * <end> being one byte past the end of this buffer.
+ * Return 1 if enough room to build this header, 0 if not.
+ */
+static int quic_build_packet_long_header(unsigned char **pos, const unsigned char *end,
+ int type, size_t pn_len,
+ struct quic_conn *qc, const struct quic_version *ver)
+{
+ int ret = 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_LPKT, qc);
+
+ if (end - *pos < sizeof ver->num + qc->dcid.len + qc->scid.len + 3) {
+ TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
+ goto leave;
+ }
+
+ type = quic_pkt_type(type, ver->num);
+ /* #0 byte flags */
+ *(*pos)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
+ (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
+ /* Version */
+ quic_write_uint32(pos, end, ver->num);
+ *(*pos)++ = qc->dcid.len;
+ /* Destination connection ID */
+ if (qc->dcid.len) {
+ memcpy(*pos, qc->dcid.data, qc->dcid.len);
+ *pos += qc->dcid.len;
+ }
+ /* Source connection ID */
+ *(*pos)++ = qc->scid.len;
+ if (qc->scid.len) {
+ memcpy(*pos, qc->scid.data, qc->scid.len);
+ *pos += qc->scid.len;
+ }
+
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc);
+ return ret;
+}
+
+/* This function builds into a buffer at <pos> position a QUIC short packet header,
+ * <end> being one byte past the end of this buffer.
+ * Return 1 if enough room to build this header, 0 if not.
+ */
+static int quic_build_packet_short_header(unsigned char **pos, const unsigned char *end,
+ size_t pn_len, struct quic_conn *qc,
+ unsigned char tls_flags)
+{
+ int ret = 0;
+ unsigned char spin_bit =
+ (qc->flags & QUIC_FL_CONN_SPIN_BIT) ? QUIC_PACKET_SPIN_BIT : 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ if (end - *pos < 1 + qc->dcid.len) {
+ TRACE_DEVEL("not enough room", QUIC_EV_CONN_LPKT, qc);
+ goto leave;
+ }
+
+ /* #0 byte flags */
+ *(*pos)++ = QUIC_PACKET_FIXED_BIT | spin_bit |
+ ((tls_flags & QUIC_FL_TLS_KP_BIT_SET) ? QUIC_PACKET_KEY_PHASE_BIT : 0) | (pn_len - 1);
+ /* Destination connection ID */
+ if (qc->dcid.len) {
+ memcpy(*pos, qc->dcid.data, qc->dcid.len);
+ *pos += qc->dcid.len;
+ }
+
+ ret = 1;
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return ret;
+}
+
+/* Apply QUIC header protection to the packet with <pos> as first byte address,
+ * <pn> as address of the Packet number field, <pnlen> being this field length
+ * with <aead> as AEAD cipher and <key> as secret key.
+ *
+ * TODO no error is expected as encryption is done in place but encryption
+ * manual is unclear. <fail> will be set to true if an error is detected.
+ */
+void quic_apply_header_protection(struct quic_conn *qc, unsigned char *pos,
+ unsigned char *pn, size_t pnlen,
+ struct quic_tls_ctx *tls_ctx, int *fail)
+
+{
+ int i;
+ /* We need an IV of at least 5 bytes: one byte for bytes #0
+ * and at most 4 bytes for the packet number
+ */
+ unsigned char mask[5] = {0};
+ EVP_CIPHER_CTX *aes_ctx = tls_ctx->tx.hp_ctx;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ *fail = 0;
+
+ if (!quic_tls_aes_encrypt(mask, pn + QUIC_PACKET_PN_MAXLEN, sizeof mask, aes_ctx)) {
+ TRACE_ERROR("could not apply header protection", QUIC_EV_CONN_TXPKT, qc);
+ *fail = 1;
+ goto out;
+ }
+
+ *pos ^= mask[0] & (*pos & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
+ for (i = 0; i < pnlen; i++)
+ pn[i] ^= mask[i + 1];
+
+ out:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+}
+
+/* Prepare into <outlist> as most as possible ack-eliciting frame from their
+ * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
+ * with <room> as available room, and <*len> the packet Length field initialized
+ * with the number of bytes already present in this buffer which must be taken
+ * into an account for the Length packet field value. <headlen> is the number of
+ * bytes already present in this packet before building frames.
+ *
+ * Update consequently <*len> to reflect the size of these frames built
+ * by this function. Also attach these frames to <l> frame list.
+ * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
+ */
+static int qc_build_frms(struct list *outlist, struct list *inlist,
+ size_t room, size_t *len, size_t headlen,
+ struct quic_enc_level *qel,
+ struct quic_conn *qc)
+{
+ int ret;
+ struct quic_frame *cf, *cfbak;
+
+ TRACE_ENTER(QUIC_EV_CONN_BCFRMS, qc);
+
+ ret = 0;
+ if (*len > room)
+ goto leave;
+
+ /* If we are not probing we must take into an account the congestion
+ * control window.
+ */
+ if (!qel->pktns->tx.pto_probe) {
+ size_t remain = quic_path_prep_data(qc->path);
+
+ if (headlen > remain)
+ goto leave;
+
+ room = QUIC_MIN(room, remain - headlen);
+ }
+
+ TRACE_PROTO("TX frms build (headlen)",
+ QUIC_EV_CONN_BCFRMS, qc, &headlen);
+
+ /* NOTE: switch/case block inside a loop, a successful status must be
+ * returned by this function only if at least one frame could be built
+ * in the switch/case block.
+ */
+ list_for_each_entry_safe(cf, cfbak, inlist, list) {
+ /* header length, data length, frame length. */
+ size_t hlen, dlen, dlen_sz, avail_room, flen;
+
+ if (!room)
+ break;
+
+ switch (cf->type) {
+ case QUIC_FT_CRYPTO:
+ TRACE_DEVEL(" New CRYPTO frame build (room, len)",
+ QUIC_EV_CONN_BCFRMS, qc, &room, len);
+ /* Compute the length of this CRYPTO frame header */
+ hlen = 1 + quic_int_getsize(cf->crypto.offset);
+ /* Compute the data length of this CRyPTO frame. */
+ dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
+ TRACE_DEVEL(" CRYPTO data length (hlen, crypto.len, dlen)",
+ QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
+ if (!dlen)
+ continue;
+
+ /* CRYPTO frame length. */
+ flen = hlen + quic_int_getsize(dlen) + dlen;
+ TRACE_DEVEL(" CRYPTO frame length (flen)",
+ QUIC_EV_CONN_BCFRMS, qc, &flen);
+ /* Add the CRYPTO data length and its encoded length to the packet
+ * length and the length of this length.
+ */
+ *len += flen;
+ room -= flen;
+ if (dlen == cf->crypto.len) {
+ /* <cf> CRYPTO data have been consumed. */
+ LIST_DEL_INIT(&cf->list);
+ LIST_APPEND(outlist, &cf->list);
+ }
+ else {
+ struct quic_frame *new_cf;
+
+ new_cf = qc_frm_alloc(QUIC_FT_CRYPTO);
+ if (!new_cf) {
+ TRACE_ERROR("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
+ continue;
+ }
+
+ new_cf->crypto.len = dlen;
+ new_cf->crypto.offset = cf->crypto.offset;
+ new_cf->crypto.qel = qel;
+ TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
+ if (cf->origin) {
+ TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
+ /* This <cf> frame was duplicated */
+ LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
+ new_cf->origin = cf->origin;
+ /* Detach the remaining CRYPTO frame from its original frame */
+ LIST_DEL_INIT(&cf->ref);
+ cf->origin = NULL;
+ }
+ LIST_APPEND(outlist, &new_cf->list);
+ /* Consume <dlen> bytes of the current frame. */
+ cf->crypto.len -= dlen;
+ cf->crypto.offset += dlen;
+ }
+ break;
+
+ case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
+ if (cf->stream.dup) {
+ struct eb64_node *node = NULL;
+ struct qc_stream_desc *stream_desc = NULL;
+ struct qf_stream *strm_frm = &cf->stream;
+
+ /* As this frame has been already lost, ensure the stream is always
+ * available or the range of this frame is not consumed before
+ * resending it.
+ */
+ node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
+ if (!node) {
+ TRACE_DEVEL("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
+ qc_frm_free(qc, &cf);
+ continue;
+ }
+
+ stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
+ if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
+ TRACE_DEVEL("ignored frame frame in already acked range",
+ QUIC_EV_CONN_PRSAFRM, qc, cf);
+ qc_frm_free(qc, &cf);
+ continue;
+ }
+ else if (strm_frm->offset.key < stream_desc->ack_offset) {
+ uint64_t diff = stream_desc->ack_offset - strm_frm->offset.key;
+
+ qc_stream_frm_mv_fwd(cf, diff);
+ TRACE_DEVEL("updated partially acked frame",
+ QUIC_EV_CONN_PRSAFRM, qc, cf);
+ }
+ }
+ /* Note that these frames are accepted in short packets only without
+ * "Length" packet field. Here, <*len> is used only to compute the
+ * sum of the lengths of the already built frames for this packet.
+ *
+ * Compute the length of this STREAM frame "header" made a all the field
+ * excepting the variable ones. Note that +1 is for the type of this frame.
+ */
+ hlen = 1 + quic_int_getsize(cf->stream.id) +
+ ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
+ /* Compute the data length of this STREAM frame. */
+ avail_room = room - hlen - *len;
+ if ((ssize_t)avail_room <= 0)
+ continue;
+
+ TRACE_DEVEL(" New STREAM frame build (room, len)",
+ QUIC_EV_CONN_BCFRMS, qc, &room, len);
+
+ /* hlen contains STREAM id and offset. Ensure there is
+ * enough room for length field.
+ */
+ if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
+ dlen = QUIC_MIN((uint64_t)max_available_room(avail_room, &dlen_sz),
+ cf->stream.len);
+ dlen_sz = quic_int_getsize(dlen);
+ flen = hlen + dlen_sz + dlen;
+ }
+ else {
+ dlen = QUIC_MIN((uint64_t)avail_room, cf->stream.len);
+ flen = hlen + dlen;
+ }
+
+ if (cf->stream.len && !dlen) {
+ /* Only a small gap is left on buffer, not
+ * enough to encode the STREAM data length.
+ */
+ continue;
+ }
+
+ TRACE_DEVEL(" STREAM data length (hlen, stream.len, dlen)",
+ QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
+ TRACE_DEVEL(" STREAM frame length (flen)",
+ QUIC_EV_CONN_BCFRMS, qc, &flen);
+ /* Add the STREAM data length and its encoded length to the packet
+ * length and the length of this length.
+ */
+ *len += flen;
+ room -= flen;
+ if (dlen == cf->stream.len) {
+ /* <cf> STREAM data have been consumed. */
+ LIST_DEL_INIT(&cf->list);
+ LIST_APPEND(outlist, &cf->list);
+
+ /* Do not notify MUX on retransmission. */
+ if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
+ qcc_streams_sent_done(cf->stream.stream->ctx,
+ cf->stream.len,
+ cf->stream.offset.key);
+ }
+ }
+ else {
+ struct quic_frame *new_cf;
+ struct buffer cf_buf;
+
+ new_cf = qc_frm_alloc(cf->type);
+ if (!new_cf) {
+ TRACE_ERROR("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
+ continue;
+ }
+
+ new_cf->stream.stream = cf->stream.stream;
+ new_cf->stream.buf = cf->stream.buf;
+ new_cf->stream.id = cf->stream.id;
+ new_cf->stream.offset = cf->stream.offset;
+ new_cf->stream.len = dlen;
+ new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
+ /* FIN bit reset */
+ new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
+ new_cf->stream.data = cf->stream.data;
+ new_cf->stream.dup = cf->stream.dup;
+ TRACE_DEVEL("split frame", QUIC_EV_CONN_PRSAFRM, qc, new_cf);
+ if (cf->origin) {
+ TRACE_DEVEL("duplicated frame", QUIC_EV_CONN_PRSAFRM, qc);
+ /* This <cf> frame was duplicated */
+ LIST_APPEND(&cf->origin->reflist, &new_cf->ref);
+ new_cf->origin = cf->origin;
+ /* Detach this STREAM frame from its origin */
+ LIST_DEL_INIT(&cf->ref);
+ cf->origin = NULL;
+ }
+ LIST_APPEND(outlist, &new_cf->list);
+ cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
+ /* Consume <dlen> bytes of the current frame. */
+ cf_buf = b_make(b_orig(cf->stream.buf),
+ b_size(cf->stream.buf),
+ (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
+ cf->stream.len -= dlen;
+ cf->stream.offset.key += dlen;
+ cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
+
+ /* Do not notify MUX on retransmission. */
+ if (qc->flags & QUIC_FL_CONN_TX_MUX_CONTEXT) {
+ qcc_streams_sent_done(new_cf->stream.stream->ctx,
+ new_cf->stream.len,
+ new_cf->stream.offset.key);
+ }
+ }
+
+ /* TODO the MUX is notified about the frame sending via
+ * previous qcc_streams_sent_done call. However, the
+ * sending can fail later, for example if the sendto
+ * system call returns an error. As the MUX has been
+ * notified, the transport layer is responsible to
+ * bufferize and resent the announced data later.
+ */
+
+ break;
+
+ default:
+ flen = qc_frm_len(cf);
+ BUG_ON(!flen);
+ if (flen > room)
+ continue;
+
+ *len += flen;
+ room -= flen;
+ LIST_DEL_INIT(&cf->list);
+ LIST_APPEND(outlist, &cf->list);
+ break;
+ }
+
+ /* Successful status as soon as a frame could be built */
+ ret = 1;
+ }
+
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_BCFRMS, qc);
+ return ret;
+}
+
+/* Generate a CONNECTION_CLOSE frame for <qc> on <qel> encryption level. <out>
+ * is used as return parameter and should be zero'ed by the caller.
+ */
+static void qc_build_cc_frm(struct quic_conn *qc, struct quic_enc_level *qel,
+ struct quic_frame *out)
+{
+ /* TODO improve CONNECTION_CLOSE on Initial/Handshake encryption levels
+ *
+ * A CONNECTION_CLOSE frame should be sent in several packets with
+ * different encryption levels depending on the client context. This is
+ * to ensure that the client can decrypt it. See RFC 9000 10.2.3 for
+ * more details on how to implement it.
+ */
+ TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
+
+
+ if (qc->err.app) {
+ if (unlikely(qel == qc->iel || qel == qc->hel)) {
+ /* RFC 9000 10.2.3. Immediate Close during the Handshake
+ *
+ * Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake
+ * packet could expose application state or be used to alter application
+ * state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a
+ * CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or
+ * Handshake packets. Otherwise, information about the application
+ * state might be revealed. Endpoints MUST clear the value of the
+ * Reason Phrase field and SHOULD use the APPLICATION_ERROR code when
+ * converting to a CONNECTION_CLOSE of type 0x1c.
+ */
+ out->type = QUIC_FT_CONNECTION_CLOSE;
+ out->connection_close.error_code = QC_ERR_APPLICATION_ERROR;
+ out->connection_close.reason_phrase_len = 0;
+ }
+ else {
+ out->type = QUIC_FT_CONNECTION_CLOSE_APP;
+ out->connection_close.error_code = qc->err.code;
+ }
+ }
+ else {
+ out->type = QUIC_FT_CONNECTION_CLOSE;
+ out->connection_close.error_code = qc->err.code;
+ }
+ TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
+
+}
+
+/* This function builds a clear packet from <pkt> information (its type)
+ * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
+ * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
+ * filling the buffer with as much frames as possible from <frms> list of
+ * prebuilt frames.
+ * The trailing QUIC_TLS_TAG_LEN bytes of this packet are not built. But they are
+ * reserved so that to ensure there is enough room to build this AEAD TAG after
+ * having returned from this function.
+ * This function also updates the value of <buf_pn> pointer to point to the packet
+ * number field in this packet. <pn_len> will also have the packet number
+ * length as value.
+ *
+ * Return 1 if succeeded (enough room to buile this packet), O if not.
+ */
+static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
+ size_t dglen, struct quic_tx_packet *pkt,
+ int64_t pn, size_t *pn_len, unsigned char **buf_pn,
+ int must_ack, int padding, int cc, int probe,
+ struct quic_enc_level *qel, struct quic_conn *qc,
+ const struct quic_version *ver, struct list *frms)
+{
+ unsigned char *beg, *payload;
+ size_t len, len_sz, len_frms, padding_len;
+ struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
+ struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
+ struct quic_frame cc_frm = { };
+ size_t ack_frm_len, head_len;
+ int64_t rx_largest_acked_pn;
+ int add_ping_frm;
+ struct list frm_list = LIST_HEAD_INIT(frm_list);
+ struct quic_frame *cf;
+ int ret = 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+
+ /* Length field value with CRYPTO frames if present. */
+ len_frms = 0;
+ beg = pos;
+ /* When not probing, and no immediate close is required, reduce the size of this
+ * buffer to respect the congestion controller window.
+ * This size will be limited if we have ack-eliciting frames to send from <frms>.
+ */
+ if (!probe && !LIST_ISEMPTY(frms) && !cc) {
+ size_t path_room;
+
+ path_room = quic_path_prep_data(qc->path);
+ if (end - beg > path_room)
+ end = beg + path_room;
+ }
+
+ /* Ensure there is enough room for the TLS encryption tag and a zero token
+ * length field if any.
+ */
+ if (end - pos < QUIC_TLS_TAG_LEN +
+ (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
+ goto no_room;
+
+ end -= QUIC_TLS_TAG_LEN;
+ rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
+ /* packet number length */
+ *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
+ /* Build the header */
+ if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
+ !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
+ (pkt->type != QUIC_PACKET_TYPE_SHORT &&
+ !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
+ goto no_room;
+
+ /* Encode the token length (0) for an Initial packet. */
+ if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
+ if (end <= pos)
+ goto no_room;
+
+ *pos++ = 0;
+ }
+
+ head_len = pos - beg;
+ /* Build an ACK frame if required. */
+ ack_frm_len = 0;
+ /* Do not ack and probe at the same time. */
+ if ((must_ack || (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) && !qel->pktns->tx.pto_probe) {
+ struct quic_arngs *arngs = &qel->pktns->rx.arngs;
+ BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
+ ack_frm.tx_ack.arngs = arngs;
+ if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
+ qel->pktns->tx.ack_delay =
+ quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
+ qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
+ }
+ ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
+ /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
+ * smallest frame (PING) and <*pn_len> more for the packet number. Note
+ * that from here, we do not know if we will have to send a PING frame.
+ * This will be decided after having computed the ack-eliciting frames
+ * to be added to this packet.
+ */
+ if (end - pos <= 1 + *pn_len)
+ goto no_room;
+
+ ack_frm_len = qc_frm_len(&ack_frm);
+ if (ack_frm_len > end - 1 - *pn_len - pos)
+ goto no_room;
+ }
+
+ /* Length field value without the ack-eliciting frames. */
+ len = ack_frm_len + *pn_len;
+ len_frms = 0;
+ if (!cc && !LIST_ISEMPTY(frms)) {
+ ssize_t room = end - pos;
+
+ TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
+ /* Initialize the length of the frames built below to <len>.
+ * If any frame could be successfully built by qc_build_frms(),
+ * we will have len_frms > len.
+ */
+ len_frms = len;
+ if (!qc_build_frms(&frm_list, frms,
+ end - pos, &len_frms, pos - beg, qel, qc)) {
+ TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
+ qc, NULL, NULL, &room);
+ if (!ack_frm_len && !qel->pktns->tx.pto_probe)
+ goto no_room;
+ }
+ }
+
+ /* Length (of the remaining data). Must not fail because, the buffer size
+ * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
+ * for the encryption tag. It must be taken into an account for the length
+ * of this packet.
+ */
+ if (len_frms)
+ len = len_frms + QUIC_TLS_TAG_LEN;
+ else
+ len += QUIC_TLS_TAG_LEN;
+ /* CONNECTION_CLOSE frame */
+ if (cc) {
+ qc_build_cc_frm(qc, qel, &cc_frm);
+ len += qc_frm_len(&cc_frm);
+ }
+ add_ping_frm = 0;
+ padding_len = 0;
+ len_sz = quic_int_getsize(len);
+ /* Add this packet size to <dglen> */
+ dglen += head_len + len_sz + len;
+ /* Note that <padding> is true only when building an Handshake packet
+ * coalesced to an Initial packet.
+ */
+ if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
+ /* This is a maximum padding size */
+ padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
+ /* The length field value is of this packet is <len> + <padding_len>
+ * the size of which may be greater than the initial computed size
+ * <len_sz>. So, let's deduce the difference between these to packet
+ * sizes from <padding_len>.
+ */
+ padding_len -= quic_int_getsize(len + padding_len) - len_sz;
+ len += padding_len;
+ }
+ else if (len_frms && len_frms < QUIC_PACKET_PN_MAXLEN) {
+ len += padding_len = QUIC_PACKET_PN_MAXLEN - len_frms;
+ }
+ else if (LIST_ISEMPTY(&frm_list)) {
+ if (qel->pktns->tx.pto_probe) {
+ /* If we cannot send a frame, we send a PING frame. */
+ add_ping_frm = 1;
+ len += 1;
+ dglen += 1;
+ /* Note that only we are in the case where this Initial packet
+ * is not coalesced to an Handshake packet. We must directly
+ * pad the datragram.
+ */
+ if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
+ if (dglen < QUIC_INITIAL_PACKET_MINLEN) {
+ padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
+ padding_len -= quic_int_getsize(len + padding_len) - len_sz;
+ len += padding_len;
+ }
+ }
+ else {
+ /* Note that +1 is for the PING frame */
+ if (*pn_len + 1 < QUIC_PACKET_PN_MAXLEN)
+ len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len - 1;
+ }
+ }
+ else {
+ /* If there is no frame at all to follow, add at least a PADDING frame. */
+ if (!ack_frm_len && !cc)
+ len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
+ }
+ }
+
+ if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
+ goto no_room;
+
+ /* Packet number field address. */
+ *buf_pn = pos;
+
+ /* Packet number encoding. */
+ if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
+ goto no_room;
+
+ /* payload building (ack-eliciting or not frames) */
+ payload = pos;
+ if (ack_frm_len) {
+ if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
+ goto no_room;
+
+ pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
+ pkt->flags |= QUIC_FL_TX_PACKET_ACK;
+ }
+
+ /* Ack-eliciting frames */
+ if (!LIST_ISEMPTY(&frm_list)) {
+ struct quic_frame *tmp_cf;
+ list_for_each_entry_safe(cf, tmp_cf, &frm_list, list) {
+ if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
+ ssize_t room = end - pos;
+ TRACE_PROTO("Not enough room", QUIC_EV_CONN_TXPKT,
+ qc, NULL, NULL, &room);
+ /* Note that <cf> was added from <frms> to <frm_list> list by
+ * qc_build_frms().
+ */
+ LIST_DEL_INIT(&cf->list);
+ LIST_INSERT(frms, &cf->list);
+ continue;
+ }
+
+ quic_tx_packet_refinc(pkt);
+ cf->pkt = pkt;
+ }
+ }
+
+ /* Build a PING frame if needed. */
+ if (add_ping_frm) {
+ frm.type = QUIC_FT_PING;
+ if (!qc_build_frm(&pos, end, &frm, pkt, qc))
+ goto no_room;
+ }
+
+ /* Build a CONNECTION_CLOSE frame if needed. */
+ if (cc) {
+ if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
+ goto no_room;
+
+ pkt->flags |= QUIC_FL_TX_PACKET_CC;
+ }
+
+ /* Build a PADDING frame if needed. */
+ if (padding_len) {
+ frm.type = QUIC_FT_PADDING;
+ frm.padding.len = padding_len;
+ if (!qc_build_frm(&pos, end, &frm, pkt, qc))
+ goto no_room;
+ }
+
+ if (pos == payload) {
+ /* No payload was built because of congestion control */
+ TRACE_PROTO("limited by congestion control", QUIC_EV_CONN_TXPKT, qc);
+ goto no_room;
+ }
+
+ /* If this packet is ack-eliciting and we are probing let's
+ * decrement the PTO probe counter.
+ */
+ if ((pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) &&
+ qel->pktns->tx.pto_probe)
+ qel->pktns->tx.pto_probe--;
+
+ pkt->len = pos - beg;
+ LIST_SPLICE(&pkt->frms, &frm_list);
+
+ ret = 1;
+ TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_TXPKT, qc, pkt);
+ leave:
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return ret;
+
+ no_room:
+ /* Replace the pre-built frames which could not be add to this packet */
+ LIST_SPLICE(frms, &frm_list);
+ TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
+ goto leave;
+}
+
+static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
+{
+ pkt->type = type;
+ pkt->len = 0;
+ pkt->in_flight_len = 0;
+ pkt->pn_node.key = (uint64_t)-1;
+ LIST_INIT(&pkt->frms);
+ pkt->time_sent = TICK_ETERNITY;
+ pkt->next = NULL;
+ pkt->prev = NULL;
+ pkt->largest_acked_pn = -1;
+ pkt->flags = 0;
+ pkt->refcnt = 0;
+}
+
+/* Build a packet into a buffer at <pos> position, <end> pointing to one byte past
+ * the end of this buffer, with <pkt_type> as packet type for <qc> QUIC connection
+ * at <qel> encryption level with <frms> list of prebuilt frames.
+ *
+ * Return -2 if the packet could not be allocated or encrypted for any reason,
+ * -1 if there was not enough room to build a packet.
+ * XXX NOTE XXX
+ * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
+ * possible (to fill an MTU), the unique reason why this function may fail is the congestion
+ * control window limitation.
+ */
+static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
+ const unsigned char *end,
+ struct quic_enc_level *qel,
+ struct quic_tls_ctx *tls_ctx, struct list *frms,
+ struct quic_conn *qc, const struct quic_version *ver,
+ size_t dglen, int pkt_type, int must_ack,
+ int padding, int probe, int cc, int *err)
+{
+ struct quic_tx_packet *ret_pkt = NULL;
+ /* The pointer to the packet number field. */
+ unsigned char *buf_pn;
+ unsigned char *first_byte, *last_byte, *payload;
+ int64_t pn;
+ size_t pn_len, payload_len, aad_len;
+ struct quic_tx_packet *pkt;
+ int encrypt_failure = 0;
+
+ TRACE_ENTER(QUIC_EV_CONN_TXPKT, qc);
+ TRACE_PROTO("TX pkt build", QUIC_EV_CONN_TXPKT, qc, NULL, qel);
+ *err = 0;
+ pkt = pool_alloc(pool_head_quic_tx_packet);
+ if (!pkt) {
+ TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_TXPKT, qc);
+ *err = -2;
+ goto err;
+ }
+
+ quic_tx_packet_init(pkt, pkt_type);
+ first_byte = *pos;
+ pn_len = 0;
+ buf_pn = NULL;
+
+ pn = qel->pktns->tx.next_pn + 1;
+ if (!qc_do_build_pkt(*pos, end, dglen, pkt, pn, &pn_len, &buf_pn,
+ must_ack, padding, cc, probe, qel, qc, ver, frms)) {
+ // trace already emitted by function above
+ *err = -1;
+ goto err;
+ }
+
+ last_byte = first_byte + pkt->len;
+ payload = buf_pn + pn_len;
+ payload_len = last_byte - payload;
+ aad_len = payload - first_byte;
+
+ quic_packet_encrypt(payload, payload_len, first_byte, aad_len, pn, tls_ctx, qc, &encrypt_failure);
+ if (encrypt_failure) {
+ /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
+ WARN_ON("quic_packet_encrypt failure");
+ *err = -2;
+ goto err;
+ }
+
+ last_byte += QUIC_TLS_TAG_LEN;
+ pkt->len += QUIC_TLS_TAG_LEN;
+ quic_apply_header_protection(qc, first_byte, buf_pn, pn_len, tls_ctx, &encrypt_failure);
+ if (encrypt_failure) {
+ /* TODO Unrecoverable failure, unencrypted data should be returned to the caller. */
+ WARN_ON("quic_apply_header_protection failure");
+ *err = -2;
+ goto err;
+ }
+
+ /* Consume a packet number */
+ qel->pktns->tx.next_pn++;
+ qc->tx.prep_bytes += pkt->len;
+ if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc)) {
+ qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
+ TRACE_PROTO("anti-amplification limit reached", QUIC_EV_CONN_TXPKT, qc);
+ }
+
+ /* Now that a correct packet is built, let us consume <*pos> buffer. */
+ *pos = last_byte;
+ /* Attach the built packet to its tree. */
+ pkt->pn_node.key = pn;
+ /* Set the packet in fligth length for in flight packet only. */
+ if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
+ pkt->in_flight_len = pkt->len;
+ qc->path->prep_in_flight += pkt->len;
+ }
+ /* Always reset this flag */
+ qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
+ if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
+ qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
+ qel->pktns->rx.nb_aepkts_since_last_ack = 0;
+ qc->flags &= ~QUIC_FL_CONN_ACK_TIMER_FIRED;
+ if (tick_isset(qc->ack_expire)) {
+ qc->ack_expire = TICK_ETERNITY;
+ qc->idle_timer_task->expire = qc->idle_expire;
+ task_queue(qc->idle_timer_task);
+ TRACE_PROTO("ack timer cancelled", QUIC_EV_CONN_IDLE_TIMER, qc);
+ }
+ }
+
+ pkt->pktns = qel->pktns;
+
+ ret_pkt = pkt;
+ leave:
+ TRACE_PROTO("TX pkt built", QUIC_EV_CONN_TXPKT, qc, ret_pkt);
+ TRACE_LEAVE(QUIC_EV_CONN_TXPKT, qc);
+ return ret_pkt;
+
+ err:
+ /* TODO: what about the frames which have been built
+ * for this packet.
+ */
+ free_quic_tx_packet(qc, pkt);
+ goto leave;
+}
+
+/* Wake-up upper layer for sending if all conditions are met :
+ * - room in congestion window or probe packet to sent
+ * - socket FD ready to sent or listener socket used
+ *
+ * Returns 1 if upper layer has been woken up else 0.
+ */
+int qc_notify_send(struct quic_conn *qc)
+{
+ const struct quic_pktns *pktns = qc->apktns;
+
+ if (qc->subs && qc->subs->events & SUB_RETRY_SEND) {
+ /* RFC 9002 7.5. Probe Timeout
+ *
+ * Probe packets MUST NOT be blocked by the congestion controller.
+ */
+ if ((quic_path_prep_data(qc->path) || pktns->tx.pto_probe) &&
+ (!qc_test_fd(qc) || !fd_send_active(qc->fd))) {
+ tasklet_wakeup(qc->subs->tasklet);
+ qc->subs->events &= ~SUB_RETRY_SEND;
+ if (!qc->subs->events)
+ qc->subs = NULL;
+
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Local variables:
+ * c-indent-level: 8
+ * c-basic-offset: 8
+ * End:
+ */