2020-11-26 01:25:22 -08:00
|
|
|
/**
|
2019-07-01 10:49:19 -07:00
|
|
|
*
|
2020-11-26 01:25:22 -08:00
|
|
|
* \section COPYRIGHT
|
2019-07-01 10:49:19 -07:00
|
|
|
*
|
2020-11-26 01:25:22 -08:00
|
|
|
* Copyright 2013-2020 Software Radio Systems Limited
|
2019-07-01 10:49:19 -07:00
|
|
|
*
|
2020-11-26 01:25:22 -08:00
|
|
|
* By using this file, you agree to the terms and conditions set
|
|
|
|
* forth in the LICENSE file which can be found at the top level of
|
|
|
|
* the distribution.
|
2019-07-01 10:49:19 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SRSLTE_PDCP_ENTITY_BASE_H
|
|
|
|
#define SRSLTE_PDCP_ENTITY_BASE_H
|
|
|
|
|
|
|
|
#include "srslte/common/buffer_pool.h"
|
|
|
|
#include "srslte/common/common.h"
|
2020-03-01 05:51:51 -08:00
|
|
|
#include "srslte/common/interfaces_common.h"
|
2020-03-25 07:57:29 -07:00
|
|
|
#include "srslte/common/logmap.h"
|
2019-07-01 10:49:19 -07:00
|
|
|
#include "srslte/common/security.h"
|
2020-07-08 14:06:15 -07:00
|
|
|
#include "srslte/common/task_scheduler.h"
|
2019-07-01 10:49:19 -07:00
|
|
|
#include "srslte/common/threads.h"
|
2020-03-01 05:51:51 -08:00
|
|
|
#include "srslte/common/timers.h"
|
2020-04-08 08:53:08 -07:00
|
|
|
#include "srslte/interfaces/pdcp_interface_types.h"
|
2020-07-13 08:19:38 -07:00
|
|
|
#include "srslte/upper/byte_buffer_queue.h"
|
2019-07-01 10:49:19 -07:00
|
|
|
|
|
|
|
namespace srslte {
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Structs and Defines common to both LTE and NR
|
|
|
|
* Ref: 3GPP TS 36.323 v10.1.0 and TS 38.323 v15.2.0
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define PDCP_PDU_TYPE_PDCP_STATUS_REPORT 0x0
|
|
|
|
#define PDCP_PDU_TYPE_INTERSPERSED_ROHC_FEEDBACK_PACKET 0x1
|
|
|
|
|
2019-07-25 04:22:42 -07:00
|
|
|
// Maximum supported PDCP SDU size is 9000 bytes.
|
|
|
|
// See TS 38.323 v15.2.0, section 4.3.1
|
2019-07-24 09:20:03 -07:00
|
|
|
#define PDCP_MAX_SDU_SIZE 9000
|
|
|
|
|
2019-07-01 10:49:19 -07:00
|
|
|
typedef enum {
|
|
|
|
PDCP_D_C_CONTROL_PDU = 0,
|
|
|
|
PDCP_D_C_DATA_PDU,
|
|
|
|
PDCP_D_C_N_ITEMS,
|
|
|
|
} pdcp_d_c_t;
|
|
|
|
static const char pdcp_d_c_text[PDCP_D_C_N_ITEMS][20] = {"Control PDU", "Data PDU"};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* PDCP Entity interface
|
|
|
|
* Common interface for LTE and NR PDCP entities
|
|
|
|
***************************************************************************/
|
|
|
|
class pdcp_entity_base
|
|
|
|
{
|
|
|
|
public:
|
2021-02-10 02:47:56 -08:00
|
|
|
pdcp_entity_base(task_sched_handle task_sched_, srslog::basic_logger& logger);
|
2020-05-20 12:16:06 -07:00
|
|
|
pdcp_entity_base(pdcp_entity_base&&) = default;
|
2019-07-01 10:49:19 -07:00
|
|
|
virtual ~pdcp_entity_base();
|
|
|
|
virtual void reset() = 0;
|
|
|
|
virtual void reestablish() = 0;
|
|
|
|
|
|
|
|
bool is_active() { return active; }
|
2019-07-08 09:53:37 -07:00
|
|
|
bool is_srb() { return cfg.rb_type == PDCP_RB_IS_SRB; }
|
|
|
|
bool is_drb() { return cfg.rb_type == PDCP_RB_IS_DRB; }
|
2019-07-01 10:49:19 -07:00
|
|
|
|
|
|
|
// RRC interface
|
2020-03-01 05:51:51 -08:00
|
|
|
void enable_integrity(srslte_direction_t direction = DIRECTION_TXRX)
|
|
|
|
{
|
|
|
|
// if either DL or UL is already enabled, both are enabled
|
|
|
|
if (integrity_direction == DIRECTION_TX && direction == DIRECTION_RX) {
|
|
|
|
integrity_direction = DIRECTION_TXRX;
|
|
|
|
} else if (integrity_direction == DIRECTION_RX && direction == DIRECTION_TX) {
|
|
|
|
integrity_direction = DIRECTION_TXRX;
|
|
|
|
} else {
|
|
|
|
integrity_direction = direction;
|
|
|
|
}
|
2021-02-10 02:47:56 -08:00
|
|
|
logger.debug("LCID=%d, integrity=%s", lcid, srslte_direction_text[integrity_direction]);
|
2020-03-01 05:51:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void enable_encryption(srslte_direction_t direction = DIRECTION_TXRX)
|
|
|
|
{
|
|
|
|
// if either DL or UL is already enabled, both are enabled
|
|
|
|
if (encryption_direction == DIRECTION_TX && direction == DIRECTION_RX) {
|
|
|
|
encryption_direction = DIRECTION_TXRX;
|
|
|
|
} else if (encryption_direction == DIRECTION_RX && direction == DIRECTION_TX) {
|
|
|
|
encryption_direction = DIRECTION_TXRX;
|
|
|
|
} else {
|
|
|
|
encryption_direction = direction;
|
|
|
|
}
|
2021-02-10 02:47:56 -08:00
|
|
|
logger.debug("LCID=%d encryption=%s", lcid, srslte_direction_text[integrity_direction]);
|
2020-04-08 11:53:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void enable_security_timed(srslte_direction_t direction, uint32_t sn)
|
|
|
|
{
|
|
|
|
switch (direction) {
|
|
|
|
case DIRECTION_TX:
|
|
|
|
enable_security_tx_sn = sn;
|
|
|
|
break;
|
|
|
|
case DIRECTION_RX:
|
|
|
|
enable_security_rx_sn = sn;
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-10 02:47:56 -08:00
|
|
|
logger.error("Timed security activation for direction %s not supported.", srslte_direction_text[direction]);
|
2020-04-08 11:53:46 -07:00
|
|
|
break;
|
|
|
|
}
|
2020-03-01 05:51:51 -08:00
|
|
|
}
|
2019-07-01 10:49:19 -07:00
|
|
|
|
2020-02-20 07:08:04 -08:00
|
|
|
void config_security(as_security_config_t sec_cfg_);
|
2019-07-01 10:49:19 -07:00
|
|
|
|
|
|
|
// GW/SDAP/RRC interface
|
2020-07-10 13:23:35 -07:00
|
|
|
virtual void write_sdu(unique_byte_buffer_t sdu) = 0;
|
2019-07-01 10:49:19 -07:00
|
|
|
|
|
|
|
// RLC interface
|
2020-09-15 04:21:39 -07:00
|
|
|
virtual void write_pdu(unique_byte_buffer_t pdu) = 0;
|
|
|
|
virtual void notify_delivery(const std::vector<uint32_t>& tx_count) = 0;
|
2020-06-16 04:08:34 -07:00
|
|
|
|
2020-07-08 14:06:15 -07:00
|
|
|
virtual void get_bearer_state(pdcp_lte_state_t* state) = 0;
|
2020-06-16 04:08:34 -07:00
|
|
|
virtual void set_bearer_state(const pdcp_lte_state_t& state) = 0;
|
2019-07-01 10:49:19 -07:00
|
|
|
|
2021-01-28 09:22:19 -08:00
|
|
|
virtual std::map<uint32_t, srslte::unique_byte_buffer_t> get_buffered_pdus() = 0;
|
|
|
|
|
2019-07-03 07:35:46 -07:00
|
|
|
// COUNT, HFN and SN helpers
|
|
|
|
uint32_t HFN(uint32_t count);
|
|
|
|
uint32_t SN(uint32_t count);
|
|
|
|
uint32_t COUNT(uint32_t hfn, uint32_t sn);
|
2019-07-23 07:49:34 -07:00
|
|
|
|
2019-07-01 10:49:19 -07:00
|
|
|
protected:
|
2021-02-10 02:47:56 -08:00
|
|
|
srslog::basic_logger& logger;
|
2020-07-08 14:06:15 -07:00
|
|
|
srslte::task_sched_handle task_sched;
|
2019-07-01 10:49:19 -07:00
|
|
|
|
2020-03-25 07:57:29 -07:00
|
|
|
bool active = false;
|
|
|
|
uint32_t lcid = 0;
|
2020-03-01 05:51:51 -08:00
|
|
|
srslte_direction_t integrity_direction = DIRECTION_NONE;
|
|
|
|
srslte_direction_t encryption_direction = DIRECTION_NONE;
|
2019-07-01 10:49:19 -07:00
|
|
|
|
2020-04-08 11:53:46 -07:00
|
|
|
int32_t enable_security_tx_sn = -1; // TX SN at which security will be enabled
|
|
|
|
int32_t enable_security_rx_sn = -1; // RX SN at which security will be enabled
|
|
|
|
|
2019-10-03 08:58:15 -07:00
|
|
|
pdcp_config_t cfg = {1,
|
|
|
|
PDCP_RB_IS_DRB,
|
|
|
|
SECURITY_DIRECTION_DOWNLINK,
|
|
|
|
SECURITY_DIRECTION_UPLINK,
|
|
|
|
PDCP_SN_LEN_12,
|
2019-11-26 06:01:16 -08:00
|
|
|
pdcp_t_reordering_t::ms500,
|
|
|
|
pdcp_discard_timer_t::infinity};
|
2019-07-03 07:35:46 -07:00
|
|
|
|
2020-02-20 07:08:04 -08:00
|
|
|
srslte::as_security_config_t sec_cfg = {};
|
2019-07-01 10:49:19 -07:00
|
|
|
|
2020-02-14 09:18:01 -08:00
|
|
|
// Security functions
|
2019-07-04 06:44:02 -07:00
|
|
|
void integrity_generate(uint8_t* msg, uint32_t msg_len, uint32_t count, uint8_t* mac);
|
|
|
|
bool integrity_verify(uint8_t* msg, uint32_t msg_len, uint32_t count, uint8_t* mac);
|
|
|
|
void cipher_encrypt(uint8_t* msg, uint32_t msg_len, uint32_t count, uint8_t* ct);
|
|
|
|
void cipher_decrypt(uint8_t* ct, uint32_t ct_len, uint32_t count, uint8_t* msg);
|
2020-02-14 09:18:01 -08:00
|
|
|
|
|
|
|
// Common packing functions
|
2020-09-28 12:57:43 -07:00
|
|
|
bool is_control_pdu(const unique_byte_buffer_t& pdu);
|
2020-02-17 07:11:20 -08:00
|
|
|
uint32_t read_data_header(const unique_byte_buffer_t& pdu);
|
2020-02-19 05:17:21 -08:00
|
|
|
void discard_data_header(const unique_byte_buffer_t& pdu);
|
2020-02-17 07:11:20 -08:00
|
|
|
void write_data_header(const srslte::unique_byte_buffer_t& sdu, uint32_t count);
|
|
|
|
void extract_mac(const unique_byte_buffer_t& pdu, uint8_t* mac);
|
|
|
|
void append_mac(const unique_byte_buffer_t& sdu, uint8_t* mac);
|
2019-07-01 10:49:19 -07:00
|
|
|
};
|
2019-07-03 07:35:46 -07:00
|
|
|
|
|
|
|
inline uint32_t pdcp_entity_base::HFN(uint32_t count)
|
|
|
|
{
|
2019-07-08 09:53:37 -07:00
|
|
|
return (count >> cfg.sn_len);
|
2019-07-03 07:35:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t pdcp_entity_base::SN(uint32_t count)
|
|
|
|
{
|
2019-07-23 07:49:34 -07:00
|
|
|
return count & (0xFFFFFFFF >> (32 - cfg.sn_len));
|
2019-07-03 07:35:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t pdcp_entity_base::COUNT(uint32_t hfn, uint32_t sn)
|
|
|
|
{
|
2019-07-08 09:53:37 -07:00
|
|
|
return (hfn << cfg.sn_len) | sn;
|
2019-07-03 07:35:46 -07:00
|
|
|
}
|
2019-10-29 08:31:27 -07:00
|
|
|
|
2019-07-01 10:49:19 -07:00
|
|
|
} // namespace srslte
|
2019-10-29 08:31:27 -07:00
|
|
|
|
2019-07-01 10:49:19 -07:00
|
|
|
#endif // SRSLTE_PDCP_ENTITY_BASE_H
|