srsLTE/srsue/hdr/stack/upper/nas.h

280 lines
10 KiB
C
Raw Normal View History

/**
2022-04-29 00:28:44 -07:00
* Copyright 2013-2022 Software Radio Systems Limited
2017-05-18 03:52:29 -07:00
*
2021-04-22 01:59:40 -07:00
* This file is part of srsRAN.
2017-05-18 03:52:29 -07:00
*
2021-04-22 01:59:40 -07:00
* srsRAN is free software: you can redistribute it and/or modify
2021-03-28 14:12:42 -07:00
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
2021-04-22 01:59:40 -07:00
* srsRAN is distributed in the hope that it will be useful,
2021-03-28 14:12:42 -07:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-05-18 03:52:29 -07:00
*
2021-03-28 14:12:42 -07:00
* A copy of the GNU Affero General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
2017-05-18 03:52:29 -07:00
*
*/
2018-03-31 10:04:04 -07:00
#ifndef SRSUE_NAS_H
#define SRSUE_NAS_H
2017-05-18 03:52:29 -07:00
#include "nas_base.h"
2021-03-19 03:45:56 -07:00
#include "srsran/asn1/liblte_mme.h"
#include "srsran/common/buffer_pool.h"
#include "srsran/common/common.h"
#include "srsran/common/nas_pcap.h"
#include "srsran/common/security.h"
#include "srsran/common/stack_procedure.h"
#include "srsran/common/task_scheduler.h"
#include "srsran/interfaces/ue_nas_interfaces.h"
#include "srsran/srslog/srslog.h"
#include "srsue/hdr/stack/upper/nas_config.h"
#include "srsue/hdr/stack/upper/nas_emm_state.h"
#include "srsue/hdr/stack/upper/nas_metrics.h"
2017-05-18 03:52:29 -07:00
2021-03-19 03:45:56 -07:00
using srsran::byte_buffer_t;
2017-05-18 03:52:29 -07:00
namespace srsue {
class usim_interface_nas;
class gw_interface_nas;
class rrc_interface_nas;
/**
* @brief This class implements the NAS layer of a EUTRA UE.
*
* The class is *NOT* thread-safe.
*
*/
class nas : public nas_interface_rrc, public srsran::timer_callback, public nas_base
{
2017-08-31 09:07:54 -07:00
public:
explicit nas(srslog::basic_logger& logger_, srsran::task_sched_handle task_sched_);
virtual ~nas();
2021-05-04 08:43:34 -07:00
int init(usim_interface_nas* usim_, rrc_interface_nas* rrc_, gw_interface_nas* gw_, const nas_args_t& args_);
2017-08-31 09:07:54 -07:00
void stop();
void run_tti();
2017-06-21 09:29:17 -07:00
// Metrics getter
void get_metrics(nas_metrics_t* m);
2017-06-21 09:29:17 -07:00
2017-08-31 09:07:54 -07:00
// RRC interface
void left_rrc_connected() override;
bool connection_request_completed(bool outcome) override;
2021-03-19 03:45:56 -07:00
bool paging(srsran::s_tmsi_t* ue_identity) override;
void set_barring(srsran::barring_t barring) override;
void write_pdu(uint32_t lcid, srsran::unique_byte_buffer_t pdu) override;
uint32_t get_k_enb_count() override;
bool get_k_asme(uint8_t* k_asme_, uint32_t n) override;
uint32_t get_ipv4_addr() override;
bool get_ipv6_addr(uint8_t* ipv6_addr) override;
void plmn_search_completed(const found_plmn_t found_plmns[MAX_FOUND_PLMNS], int nof_plmns) final;
2017-06-21 09:29:17 -07:00
// Stack interface
bool switch_on();
bool switch_off();
bool enable_data();
bool disable_data();
2021-03-19 03:45:56 -07:00
void start_service_request(srsran::establishment_cause_t cause_);
// Stack+RRC interface
bool is_registered() override;
2017-06-21 09:29:17 -07:00
2019-10-11 08:32:00 -07:00
// timer callback
void timer_expired(uint32_t timeout_id) override;
2019-10-11 08:32:00 -07:00
2017-08-31 09:07:54 -07:00
private:
2022-01-12 06:01:47 -08:00
rrc_interface_nas* rrc = nullptr;
usim_interface_nas* usim = nullptr;
gw_interface_nas* gw = nullptr;
2017-06-21 09:29:17 -07:00
bool running = false;
2017-05-18 03:52:29 -07:00
nas_args_t cfg = {};
emm_state_t state = {};
2017-06-21 09:29:17 -07:00
2021-03-19 03:45:56 -07:00
srsran::barring_t current_barring = srsran::barring_t::none;
2021-03-19 03:45:56 -07:00
srsran::plmn_id_t current_plmn;
srsran::plmn_id_t home_plmn;
2021-03-19 03:45:56 -07:00
std::vector<srsran::plmn_id_t> known_plmns;
2017-06-21 09:29:17 -07:00
2019-05-24 05:38:49 -07:00
typedef enum { DEFAULT_EPS_BEARER = 0, DEDICATED_EPS_BEARER } eps_bearer_type_t;
typedef struct {
eps_bearer_type_t type;
uint8_t eps_bearer_id;
uint8_t linked_eps_bearer_id;
} eps_bearer_t;
typedef std::map<uint8_t, eps_bearer_t> eps_bearer_map_t;
typedef std::pair<uint8_t, eps_bearer_t> eps_bearer_map_pair_t;
eps_bearer_map_t eps_bearer;
2022-01-12 06:01:47 -08:00
bool have_guti = false;
bool have_ctxt = false;
bool auth_request = false;
uint8_t current_sec_hdr = LIBLTE_MME_SECURITY_HDR_TYPE_PLAIN_NAS;
2017-06-21 09:29:17 -07:00
const uint32_t max_attach_attempts = 5; // Sec. 5.5.1.2.6
uint32_t attach_attempt_counter = 0;
uint32_t ip_addr = 0;
uint8_t ipv6_if_id[8] = {};
2017-06-21 09:29:17 -07:00
uint8_t chap_id = 0;
2018-05-02 07:49:16 -07:00
uint8_t transaction_id = 0;
2017-06-21 09:29:17 -07:00
2019-10-11 08:32:00 -07:00
// timers
2021-03-19 03:45:56 -07:00
srsran::task_sched_handle task_sched;
srsran::timer_handler::unique_timer t3402; // started when attach attempt counter reached 5
srsran::timer_handler::unique_timer t3410; // started when attach request is sent, on expiry, start t3411
srsran::timer_handler::unique_timer t3411; // started when attach failed
srsran::timer_handler::unique_timer t3421; // started when detach request is sent
srsran::timer_handler::unique_timer reattach_timer; // started to trigger delayed re-attach
2019-10-11 08:32:00 -07:00
// Values according to TS 24.301 Sec 10.2
const uint32_t t3402_duration_ms = 12 * 60 * 1000; // 12m
const uint32_t t3410_duration_ms = 15 * 1000; // 15s
const uint32_t t3411_duration_ms = 10 * 1000; // 10s
const uint32_t t3421_duration_ms = 15 * 1000; // 15s
const uint32_t reattach_timer_duration_ms = 2 * 1000; // 2s (arbitrarily chosen to delay re-attach)
2019-10-11 08:32:00 -07:00
// TS 23.003 Sec. 6.2.2 IMEISV's last two octets are Software Version Number (SVN)
// which identifies the software version number of the mobile equipment
const uint8_t ue_svn_oct1 = 0x5;
const uint8_t ue_svn_oct2 = 0x3;
2017-08-31 09:07:54 -07:00
// Security
2022-01-12 06:01:47 -08:00
bool eia_caps[8] = {};
bool eea_caps[8] = {};
2017-06-21 09:29:17 -07:00
// Airplane mode simulation
typedef enum { DISABLED = 0, ENABLED } airplane_mode_state_t;
Upgrade loggers in srsue (#2163) * Replaced UE logger in the ue class. * Replaced loggers in the main phy class and prach. * Replaced loggers in phy common and ta_control. * Replace loggers in cc and sf workers. * Replaced loggers in intra_measure, scell_recv, search, sfn_sync, sync. * Remove last uses of the old loggers in the main phy class. * Remove stray newline in logs. * Replaced loggers in ue gw. * - Started to replace loggers in the ue stack. - Replaced loggers in usim and pcsc. - Adapted nas and usim tests. * Replace loggers in nas. * Added missing log init calls in two previously modified tests. * Replaced logger in nas idle procs. * Replaced loggers in nas emm state. * Replaced loggers in tft packet filter and adapted tft test. * Replaced loggers in main RRC class. * Replaced loggers in RRC cell. * Replaced loggers in RRC meas. * Replaced loggers in rrc procedures. * Started logger replacement in MAC layer, more precisely in demux and dl_harq classes. Been unable to inject loggers in construction for dl_tb_process due to very weird static assertions in the std::vector code being the type not constructible which is not true, so instead use the main MAC logger directly. * Replaced loggers in mac mux class. * Replaced loggers in mac pro_bsr. * Replaced loggers in mac proc phr. * Replaced loggers in mac proc SR and RA. * Replace loggers in mac UL HARQ. * Replaced loggers in main ue stack class. * Fixed nas test crashing due to a null string. * Ported mac_test to use the new loggers. * Removed TTI reporting for the PHY log as the old logger did. * Replaced loggers in UE phy tests. * Configure loggers in nas_test. * Replaced loggers in rrc_meas_test. * Replaced loggers in rrc_reconfig_test. * Added missing newline in tft_test. * Fix compilation errors in TTCN3 tests. * Fix linker error detected in CI and warning. * Replaced loggers in TTCN3 tests. * Fix a text replace error in some log messages. * Remove trailing newlines from log entries. * Remove old logger from rrc. * Flush backend before printing the test status. * - Fix compilation error from previous rebase. - Remove trailing newlines from some missing log entries.
2021-01-28 08:17:43 -08:00
airplane_mode_state_t airplane_mode_state = {};
2021-03-19 03:45:56 -07:00
srsran::timer_handler::unique_timer airplane_mode_sim_timer;
// Security
2021-03-19 03:45:56 -07:00
int apply_security_config(srsran::unique_byte_buffer_t& pdu, uint8_t sec_hdr_type);
void reset_security_context();
void set_k_enb_count(uint32_t count);
bool check_cap_replay(LIBLTE_MME_UE_SECURITY_CAPABILITIES_STRUCT* caps);
2017-06-21 09:29:17 -07:00
// NAS Connection Initiation/Termination
2021-03-19 03:45:56 -07:00
void start_attach_request(srsran::establishment_cause_t cause_);
bool detach_request(const bool switch_off);
// PLMN Selection Helpers
void start_plmn_selection_proc();
void select_plmn();
2017-08-31 09:07:54 -07:00
// Parsers
2021-03-19 03:45:56 -07:00
void parse_attach_accept(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_attach_reject(uint32_t lcid, srsran::unique_byte_buffer_t pdu, const uint8_t sec_hdr_type);
2021-03-19 03:45:56 -07:00
void parse_authentication_request(uint32_t lcid, srsran::unique_byte_buffer_t pdu, const uint8_t sec_hdr_type);
void parse_authentication_reject(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_identity_request(srsran::unique_byte_buffer_t pdu, const uint8_t sec_hdr_type);
void parse_security_mode_command(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_service_reject(uint32_t lcid, srsran::unique_byte_buffer_t pdu, const uint8_t sec_hdr_type);
2021-03-19 03:45:56 -07:00
void parse_esm_information_request(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_emm_information(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_detach_request(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_emm_status(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_activate_dedicated_eps_bearer_context_request(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_deactivate_eps_bearer_context_request(srsran::unique_byte_buffer_t pdu);
void parse_activate_test_mode(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_close_ue_test_loop(uint32_t lcid, srsran::unique_byte_buffer_t pdu);
void parse_modify_eps_bearer_context_request(srsran::unique_byte_buffer_t pdu);
2017-06-21 09:29:17 -07:00
// Packet generators
2021-03-19 03:45:56 -07:00
void gen_attach_request(srsran::unique_byte_buffer_t& msg);
void gen_service_request(srsran::unique_byte_buffer_t& msg);
2017-08-31 09:07:54 -07:00
// Senders
void send_attach_complete(const uint8_t& transaction_id, const uint8_t& eps_bearer_id);
void send_identity_response(uint8 id_type);
void send_service_request();
void send_esm_information_response(const uint8 proc_transaction_id);
void send_authentication_response(const uint8_t* res, const size_t res_len);
void send_authentication_failure(const uint8_t cause, const uint8_t* auth_fail_param);
void gen_pdn_connectivity_request(LIBLTE_BYTE_MSG_STRUCT* msg);
2017-11-23 10:42:48 -08:00
void send_security_mode_reject(uint8_t cause);
void send_attach_request();
2018-08-05 00:37:58 -07:00
void send_detach_request(bool switch_off);
2018-08-07 06:18:07 -07:00
void send_detach_accept();
void send_activate_dedicated_eps_bearer_context_accept(const uint8_t& proc_transaction_id,
const uint8_t& eps_bearer_id);
void send_deactivate_eps_bearer_context_accept(const uint8_t& proc_transaction_id, const uint8_t& eps_bearer_id);
void send_modify_eps_bearer_context_accept(const uint8_t& proc_transaction_id, const uint8_t& eps_bearer_id);
void send_activate_test_mode_complete();
void send_close_ue_test_loop_complete();
2017-11-23 10:42:48 -08:00
2020-05-07 05:45:38 -07:00
// Airplane mode simulator helpers
void start_airplane_mode_sim();
void airplane_mode_sim_switch_off();
void airplane_mode_sim_switch_on();
// Misc helpers
void clear_eps_bearer();
2020-05-07 05:45:38 -07:00
// FSM Helpers
void enter_state(emm_state_t state_);
void enter_emm_null();
void enter_emm_deregistered(emm_state_t::deregistered_substate_t substate);
void enter_emm_deregistered_initiated();
2019-10-09 02:53:27 -07:00
2017-11-23 10:42:48 -08:00
// security context persistence file
2021-09-17 08:28:35 -07:00
bool read_ctxt_file(nas_sec_ctxt* ctxt_, nas_sec_base_ctxt* ctxt_base_);
bool write_ctxt_file(nas_sec_ctxt ctxt_, nas_sec_base_ctxt ctxt_base_);
// ctxt file helpers
std::string hex_to_string(uint8_t* hex, int size);
bool string_to_hex(std::string hex_str, uint8_t* hex, uint32_t len);
std::string emm_info_str(LIBLTE_MME_EMM_INFORMATION_MSG_STRUCT* info);
template <class T>
bool readvar(std::istream& file, const char* key, T* var)
{
std::string line;
size_t len = strlen(key);
std::getline(file, line);
if (line.substr(0, len).compare(key)) {
return false;
}
2019-11-28 07:09:15 -08:00
*var = (T)strtol(line.substr(len).c_str(), NULL, 10);
return true;
}
bool readvar(std::istream& file, const char* key, uint8_t* var, int varlen)
{
std::string line;
size_t len = strlen(key);
std::getline(file, line);
if (line.substr(0, len).compare(key)) {
return false;
}
std::string tmp = line.substr(len);
if (!string_to_hex(tmp, var, varlen)) {
return false;
}
return true;
}
// NAS Idle procedures
class plmn_search_proc; // PLMN selection proc (fwd declared)
2021-03-19 03:45:56 -07:00
srsran::proc_manager_list_t callbacks;
srsran::proc_t<plmn_search_proc> plmn_searcher;
2017-08-31 09:07:54 -07:00
};
2017-05-18 03:52:29 -07:00
} // namespace srsue
2018-03-31 10:04:04 -07:00
#endif // SRSUE_NAS_H