/*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <openssl/ssl.h>
#include "internal/quic_record_rx.h"
#include "quic_record_shared.h"
#include "internal/common.h"
#include "internal/list.h"
#include "../ssl_local.h"
/*
* Mark a packet in a bitfield.
*
* pkt_idx: index of packet within datagram.
*/
static ossl_inline void pkt_mark(uint64_t *bitf, size_t pkt_idx)
{
assert(pkt_idx < QUIC_MAX_PKT_PER_URXE);
*bitf |= ((uint64_t)1) << pkt_idx;
}
/* Returns 1 if a packet is in the bitfield. */
static ossl_inline int pkt_is_marked(const uint64_t *bitf, size_t pkt_idx)
{
assert(pkt_idx < QUIC_MAX_PKT_PER_URXE);
return (*bitf & (((uint64_t)1) << pkt_idx)) != 0;
}
/*
* RXE
* ===
*
* RX Entries (RXEs) store processed (i.e., decrypted) data received from the
* network. One RXE is used per received QUIC packet.
*/
typedef struct rxe_st RXE;
struct rxe_st {
OSSL_QRX_PKT pkt;
OSSL_LIST_MEMBER(rxe, RXE);
size_t data_len, alloc_len, refcount;
/* Extra fields for per-packet information. */
QUIC_PKT_HDR hdr; /* data/len are decrypted payload */
/* Decoded packet number. */
QUIC_PN pn;
/* Addresses copied from URXE. */
BIO_ADDR peer, local;
/* Time we received the packet (not when we processed it). */
OSSL_TIME time;
/* Total length of the datagram which contained this packet. */
size_t datagram_len;
/*
* alloc_len allocated bytes (of which data_len bytes are valid) follow this
* structure.
*/
};
DEFINE_LIST_OF(rxe, RXE);
typedef OSSL_LIST(rxe) RXE_LIST;
static ossl_inline unsigned char *rxe_data(const RXE *e)
{
return (unsigned char *)(e + 1);
}
/*
* QRL
* ===
*/
struct ossl_qrx_st {
OSSL_LIB_CTX *libctx;
const char *propq;
/* Demux to receive datagrams from. */
QUIC_DEMUX *demux;
/* Length of connection IDs used in short-header packets in bytes. */
size_t short_conn_id_len;
/* Maximum number of deferred datagrams buffered at any one time. */
size_t max_deferred;
/* Current count of deferred datagrams. */
size_t num_deferred;
/*
* List of URXEs which are filled with received encrypted data.
* These are returned to the DEMUX's free list as they are processed.
*/
QUIC_URXE_LIST urx_pending;
/*
* List of URXEs which we could not decrypt immediately and which are being
* kept in case they can be decrypted later.
*/
QUIC_URXE_LIST urx_deferred;
/*
* List of RXEs which are not currently in use. These are moved
* to the pending list as they are filled.
*/
RXE_LIST rx_free;
/*
* List of RXEs which are filled with decrypted packets ready to be passed
* to the user. A RXE is removed from all lists inside the QRL when passed
* to the user, then returned to the free list when the user returns it.
*/
RXE_LIST rx_pending;
/* Largest PN we have received and processed in a given PN space. */
QUIC_PN largest_pn[QUIC_PN_SPACE_NUM];
/* Per encryption-level state. */
OSSL_QRL_ENC_LEVEL_SET el_set;
/* Bytes we have received since this counter was last cleared. */
uint64_t bytes_received;
/*
* Number of forged packets we have received since the QRX was instantiated.
* Note that as per RFC 9001, this is connection-