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

263 lines
7.9 KiB
C
Raw Normal View History

2019-04-26 19:27:38 +00:00
/*
* Copyright 2013-2019 Software Radio Systems Limited
2017-05-18 10:52:29 +00:00
*
2019-04-26 19:27:38 +00:00
* This file is part of srsLTE.
2017-05-18 10:52:29 +00:00
*
2019-04-26 19:27:38 +00:00
* srsLTE is free software: you can redistribute it and/or modify
2017-05-18 10:52:29 +00: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.
*
2019-04-26 19:27:38 +00:00
* srsLTE is distributed in the hope that it will be useful,
2017-05-18 10:52:29 +00: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.
*
* 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/.
*
*/
2018-03-31 17:04:04 +00:00
#ifndef SRSUE_NAS_H
#define SRSUE_NAS_H
2017-05-18 10:52:29 +00:00
#include "srslte/common/buffer_pool.h"
#include "srslte/common/log.h"
#include "srslte/common/common.h"
#include "srslte/interfaces/ue_interfaces.h"
#include "srslte/common/security.h"
#include "srslte/asn1/liblte_mme.h"
#include "srslte/common/nas_pcap.h"
2017-05-18 10:52:29 +00:00
using srslte::byte_buffer_t;
namespace srsue {
class nas_args_t
{
public:
nas_args_t() : force_imsi_attach(false) {}
std::string apn_name;
std::string apn_protocol;
std::string apn_user;
std::string apn_pass;
bool force_imsi_attach;
std::string eia;
std::string eea;
};
2017-05-18 10:52:29 +00:00
// EMM states (3GPP 24.302 v10.0.0)
2017-08-31 16:07:54 +00:00
typedef enum {
EMM_STATE_NULL = 0,
EMM_STATE_DEREGISTERED,
EMM_STATE_REGISTERED,
EMM_STATE_DEREGISTERED_INITIATED,
EMM_STATE_TAU_INITIATED,
EMM_STATE_N_ITEMS,
} emm_state_t;
static const char emm_state_text[EMM_STATE_N_ITEMS][100] = {"NULL",
"DEREGISTERED",
"REGISTERED",
"DEREGISTERED INITIATED",
"TRACKING AREA UPDATE INITIATED"};
2017-06-21 16:29:17 +00:00
2017-08-31 16:07:54 +00:00
class nas
: public nas_interface_rrc,
public nas_interface_ue,
public nas_interface_gw
{
2017-08-31 16:07:54 +00:00
public:
nas();
void init(usim_interface_nas* usim_,
rrc_interface_nas* rrc_,
gw_interface_nas* gw_,
srslte::log* nas_log_,
nas_args_t args_);
2017-08-31 16:07:54 +00:00
void stop();
2017-06-21 16:29:17 +00:00
2017-08-31 16:07:54 +00:00
emm_state_t get_state();
2017-06-21 16:29:17 +00:00
2017-08-31 16:07:54 +00:00
// RRC interface
2019-01-17 11:42:01 +00:00
void paging(asn1::rrc::s_tmsi_s* ue_identiy);
void set_barring(barring_t barring);
2019-05-13 14:34:15 +00:00
void write_pdu(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
uint32_t get_k_enb_count();
2017-08-31 16:07:54 +00:00
bool is_attached();
2017-11-23 18:42:48 +00:00
bool get_k_asme(uint8_t *k_asme_, uint32_t n);
2018-10-26 11:04:23 +00:00
uint32_t get_ipv4_addr();
bool get_ipv6_addr(uint8_t *ipv6_addr);
2017-06-21 16:29:17 +00:00
2017-08-31 16:07:54 +00:00
// UE interface
bool attach_request();
2018-08-05 07:37:58 +00:00
bool detach_request();
2017-06-21 16:29:17 +00:00
// PCAP
void start_pcap(srslte::nas_pcap *pcap_);
2017-08-31 16:07:54 +00:00
private:
srslte::byte_buffer_pool *pool;
srslte::log *nas_log;
rrc_interface_nas *rrc;
usim_interface_nas *usim;
gw_interface_nas *gw;
2017-06-21 16:29:17 +00:00
nas_args_t cfg;
2017-05-18 10:52:29 +00:00
2017-08-31 16:07:54 +00:00
emm_state_t state;
2017-06-21 16:29:17 +00:00
nas_interface_rrc::barring_t current_barring;
2019-01-17 11:42:01 +00:00
bool plmn_is_selected;
asn1::rrc::plmn_id_s current_plmn;
asn1::rrc::plmn_id_s home_plmn;
2019-01-17 11:42:01 +00:00
std::vector<asn1::rrc::plmn_id_s> known_plmns;
2017-06-21 16:29:17 +00:00
2017-11-23 18:42:48 +00:00
LIBLTE_MME_EMM_INFORMATION_MSG_STRUCT emm_info;
2017-06-21 16:29:17 +00:00
2017-11-23 18:42:48 +00:00
// Security context
struct nas_sec_ctxt{
uint8_t ksi;
uint8_t k_asme[32];
uint32_t tx_count;
uint32_t rx_count;
uint32_t k_enb_count;
srslte::CIPHERING_ALGORITHM_ID_ENUM cipher_algo;
srslte::INTEGRITY_ALGORITHM_ID_ENUM integ_algo;
LIBLTE_MME_EPS_MOBILE_ID_GUTI_STRUCT guti;
2017-11-23 18:42:48 +00:00
};
bool have_guti;
2017-11-23 18:42:48 +00:00
bool have_ctxt;
nas_sec_ctxt ctxt;
bool auth_request;
2017-06-21 16:29:17 +00:00
2017-08-31 16:07:54 +00:00
uint32_t ip_addr;
uint8_t ipv6_if_id[8];
2017-08-31 16:07:54 +00:00
uint8_t eps_bearer_id;
2017-06-21 16:29:17 +00:00
2018-05-02 14:49:16 +00:00
uint8_t chap_id;
2017-08-31 16:07:54 +00:00
uint8_t transaction_id;
2017-06-21 16:29:17 +00:00
2017-08-31 16:07:54 +00:00
// Security
bool eia_caps[8];
bool eea_caps[8];
2017-08-31 16:07:54 +00:00
uint8_t k_nas_enc[32];
uint8_t k_nas_int[32];
2017-06-21 16:29:17 +00:00
// PCAP
srslte::nas_pcap *pcap = NULL;
bool running;
bool rrc_connect();
void integrity_generate(uint8_t *key_128,
2017-08-31 16:07:54 +00:00
uint32_t count,
uint8_t direction,
uint8_t *msg,
uint32_t msg_len,
uint8_t *mac);
bool integrity_check(srslte::byte_buffer_t* pdu);
void cipher_encrypt(srslte::byte_buffer_t* pdu);
void cipher_decrypt(srslte::byte_buffer_t* pdu);
void set_k_enb_count(uint32_t count);
2017-11-23 18:42:48 +00:00
bool check_cap_replay(LIBLTE_MME_UE_SECURITY_CAPABILITIES_STRUCT *caps);
2017-06-21 16:29:17 +00:00
void select_plmn();
2017-08-31 16:07:54 +00:00
// Parsers
2019-05-13 14:34:15 +00:00
void parse_attach_accept(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_attach_reject(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_authentication_request(uint32_t lcid, srslte::unique_byte_buffer_t pdu, const uint8_t sec_hdr_type);
void parse_authentication_reject(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_identity_request(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_security_mode_command(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_service_reject(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_esm_information_request(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_emm_information(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_detach_request(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_emm_status(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_activate_dedicated_eps_bearer_context_request(uint32_t lcid, srslte::unique_byte_buffer_t pdu);
void parse_activate_test_mode(uint32_t lcid, srslte::unique_byte_buffer_t pdu, const uint8_t sec_hdr_type);
2017-06-21 16:29:17 +00:00
// Packet generators
void gen_attach_request(srslte::byte_buffer_t* msg);
void gen_service_request(srslte::byte_buffer_t* msg);
2017-08-31 16:07:54 +00:00
// Senders
void send_attach_complete(const uint8_t& transaction_id, const uint8_t& eps_bearer_id);
2018-09-20 09:55:42 +00:00
void send_identity_response(uint32_t lcid, 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, const uint8_t sec_hdr_type);
void send_authentication_failure(const uint8_t cause, const uint8_t* auth_fail_param);
2017-08-31 16:07:54 +00:00
void gen_pdn_connectivity_request(LIBLTE_BYTE_MSG_STRUCT *msg);
2017-11-23 18:42:48 +00:00
void send_security_mode_reject(uint8_t cause);
2018-08-05 07:37:58 +00:00
void send_detach_request(bool switch_off);
2018-08-07 13:18:07 +00: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_activate_test_mode_complete(const uint8_t sec_hdr_type);
2017-11-23 18:42:48 +00:00
// security context persistence file
bool read_ctxt_file(nas_sec_ctxt *ctxt);
bool write_ctxt_file(nas_sec_ctxt ctxt);
// 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;
}
*var = (T)atoi(line.substr(len).c_str());
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;
}
std::vector<uint8_t> split_string(const std::string input)
{
std::vector<uint8_t> list;
std::stringstream ss(input);
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
if (not substr.empty()) {
list.push_back(atoi(substr.c_str()));
}
}
return list;
}
2017-08-31 16:07:54 +00:00
};
2017-05-18 10:52:29 +00:00
} // namespace srsue
2018-03-31 17:04:04 +00:00
#endif // SRSUE_NAS_H