open5gs/src/hss/hss_context.c

262 lines
5.9 KiB
C
Raw Normal View History

2017-04-06 11:44:52 +00:00
#define TRACE_MODULE _hss_context
2017-03-02 02:43:26 +00:00
#include "core_debug.h"
#include "core_pool.h"
2017-03-02 13:47:43 +00:00
#include "core_lib.h"
2017-03-02 02:43:26 +00:00
2017-04-06 10:20:33 +00:00
#include "hss_context.h"
2017-03-02 02:43:26 +00:00
2017-04-10 02:29:46 +00:00
#define MAX_NUM_OF_PROFILE 8
2017-03-02 13:47:43 +00:00
2017-04-06 11:44:52 +00:00
static hss_context_t self;
2017-03-02 13:47:43 +00:00
2017-04-10 02:29:46 +00:00
pool_declare(hss_profile_pool, hss_profile_t, MAX_NUM_OF_PROFILE);
2017-04-06 11:44:52 +00:00
pool_declare(hss_ue_pool, hss_ue_t, MAX_NUM_OF_UE);
2017-03-02 02:43:26 +00:00
2017-04-06 11:44:52 +00:00
hss_context_t* hss_self()
2017-03-02 13:47:43 +00:00
{
return &self;
}
2017-04-06 11:44:52 +00:00
status_t hss_context_init(void)
2017-03-02 02:43:26 +00:00
{
2017-03-24 12:19:24 +00:00
char buf[HSS_KEY_LEN];
2017-04-09 15:27:19 +00:00
2017-04-10 02:29:46 +00:00
hss_profile_id_t profile_id = 1;
hss_profile_t *profile;
2017-04-06 11:58:13 +00:00
hss_ue_t *ue;
2017-03-02 13:47:43 +00:00
2017-04-06 11:44:52 +00:00
memset(&self, 0, sizeof(hss_context_t));
2017-03-02 13:47:43 +00:00
2017-04-10 02:29:46 +00:00
pool_init(&hss_profile_pool, MAX_NUM_OF_PROFILE);
2017-04-06 12:44:30 +00:00
pool_init(&hss_ue_pool, MAX_NUM_OF_UE);
2017-04-10 02:29:46 +00:00
list_init(&self.profile_list);
list_init(&self.ue_list);
profile = hss_profile_add(profile_id);
d_assert(profile, return -1, "UE context add failed");
#define OP "5F1D289C5D354D0A140C2548F5F3E3BA"
#define AMF "8000"
#define OPc "E8ED289DEBA952E4283B54E88E6183CA"
#define K "465B5CE8B199B49FAA5F0A2EE238A6BC"
memcpy(profile->op, CORE_HEX(OP, strlen(OP), buf), HSS_KEY_LEN);
memcpy(profile->amf, CORE_HEX(AMF, strlen(AMF), buf), HSS_AMF_LEN);
memcpy(profile->k, CORE_HEX(K, strlen(K), buf), HSS_KEY_LEN);
profile->sqn = 64;
2017-03-07 07:19:18 +00:00
2017-04-10 04:22:23 +00:00
#define HSS_SUBSCRIBER_STATUS_SERVICE_GRANTED 0
#define HSS_SUBSCRIBER_STATUS_OPERATOR_DETERMINED_BARRING 1
profile->subscriber_status = HSS_SUBSCRIBER_STATUS_SERVICE_GRANTED;
#define HSS_NETWORK_ACCESS_MODE_PACKET_AND_CIRCUIT 0
#define HSS_NETWORK_ACCESS_MODE_RESERVED 1
#define HSS_NETWORK_ACCESS_MODE_ONLY_PACKET 2
profile->network_access_mode = HSS_NETWORK_ACCESS_MODE_ONLY_PACKET;
2017-04-10 04:47:35 +00:00
profile->max_bandwidth_ul = 102400; /* Kbps */
profile->max_bandwidth_dl = 102400; /* Kbps */
2017-04-10 04:22:23 +00:00
2017-03-07 07:19:18 +00:00
#define UE1_IMSI "001010123456800"
#define UE2_IMSI "001010123456796"
#define UE3_IMSI "001010123456819"
#define UE3_RAND "20080c3818183b52 2614162c07601d0d"
2017-04-10 02:29:46 +00:00
ue = hss_ue_add(profile_id, UE1_IMSI);
2017-04-06 11:58:13 +00:00
d_assert(ue, return -1, "UE context add failed");
2017-03-07 07:19:18 +00:00
2017-04-10 02:29:46 +00:00
ue = hss_ue_add(profile_id, UE2_IMSI);
2017-04-06 11:58:13 +00:00
d_assert(ue, return -1, "UE context add failed");
2017-03-07 07:19:18 +00:00
2017-04-10 02:29:46 +00:00
ue = hss_ue_add(profile_id, UE3_IMSI);
2017-04-06 11:58:13 +00:00
d_assert(ue, return -1, "UE context add failed");
memcpy(ue->rand, CORE_HEX(UE3_RAND, strlen(UE3_RAND), buf),
2017-03-25 01:45:49 +00:00
RAND_LEN);
2017-03-07 07:19:18 +00:00
2017-03-02 02:43:26 +00:00
return CORE_OK;
}
2017-04-06 11:44:52 +00:00
void hss_context_final(void)
2017-03-02 02:43:26 +00:00
{
2017-04-06 11:44:52 +00:00
hss_ue_remove_all();
2017-04-10 02:29:46 +00:00
hss_profile_remove_all();
2017-03-07 07:19:18 +00:00
2017-04-06 11:44:52 +00:00
pool_final(&hss_ue_pool);
2017-04-10 02:29:46 +00:00
pool_final(&hss_profile_pool);
2017-03-02 02:43:26 +00:00
return;
}
2017-04-10 02:29:46 +00:00
hss_profile_t* hss_profile_add(hss_profile_id_t id)
{
hss_profile_t *profile = NULL;
pool_alloc_node(&hss_profile_pool, &profile);
d_assert(profile, return NULL, "HSS-UE context allocation failed");
memset(profile, 0, sizeof(hss_profile_t));
profile->id = id;
list_append(&self.profile_list, profile);
return profile;
}
status_t hss_profile_remove(hss_profile_t *profile)
{
d_assert(profile, return CORE_ERROR, "Null param");
list_remove(&self.profile_list, profile);
pool_free_node(&hss_profile_pool, profile);
return CORE_OK;
}
status_t hss_profile_remove_all()
{
hss_profile_t *profile = NULL, *next_profile = NULL;
profile = list_first(&self.profile_list);
while (profile)
{
next_profile = list_next(profile);
hss_profile_remove(profile);
profile = next_profile;
}
return CORE_OK;
}
hss_profile_t* hss_profile_find_by_id(hss_profile_id_t id)
{
hss_profile_t *profile = NULL;
profile = list_first(&self.profile_list);
while (profile)
{
if (profile->id == id)
break;
profile = list_next(profile);
}
return profile;
}
hss_profile_t* hss_profile_find_by_name(c_int8_t *name)
{
hss_profile_t *profile = NULL;
profile = list_first(&self.profile_list);
while (profile)
{
if (strcmp(profile->name, name) == 0)
break;
profile = list_next(profile);
}
return profile;
}
hss_profile_t* hss_profile_first()
2017-03-02 02:43:26 +00:00
{
2017-04-10 02:29:46 +00:00
return list_first(&self.profile_list);
}
hss_profile_t* hss_profile_next(hss_profile_t *profile)
{
return list_next(profile);
}
hss_ue_t* hss_ue_add(hss_profile_id_t id, c_int8_t *imsi_bcd)
{
hss_profile_t *profile = NULL;
2017-04-06 11:58:13 +00:00
hss_ue_t *ue = NULL;
2017-03-02 02:43:26 +00:00
2017-04-10 02:29:46 +00:00
profile = hss_profile_find_by_id(id);
d_assert(profile, return NULL, "Can't find Profile = %d", id);
2017-04-06 11:58:13 +00:00
pool_alloc_node(&hss_ue_pool, &ue);
d_assert(ue, return NULL, "HSS-UE context allocation failed");
2017-03-02 02:43:26 +00:00
2017-04-06 11:58:13 +00:00
memset(ue, 0, sizeof(hss_ue_t));
2017-03-02 02:43:26 +00:00
2017-04-10 02:29:46 +00:00
memcpy(ue->k, profile->k, HSS_KEY_LEN);
memcpy(ue->op, profile->op, HSS_KEY_LEN);
memcpy(ue->amf, profile->amf, HSS_AMF_LEN);
2017-03-03 07:22:09 +00:00
2017-04-10 04:22:23 +00:00
strcpy(ue->imsi_bcd, imsi_bcd);
core_bcd_to_buffer(ue->imsi_bcd, ue->msisdn, &ue->msisdn_len);
2017-04-10 02:29:46 +00:00
core_generate_random_bytes(ue->rand, RAND_LEN);
ue->sqn = profile->sqn;
2017-04-10 04:22:23 +00:00
strcpy(ue->apn, profile->apn);
ue->subscriber_status = profile->subscriber_status;
ue->network_access_mode = profile->network_access_mode;
ue->max_bandwidth_ul = profile->max_bandwidth_ul;
ue->max_bandwidth_dl = profile->max_bandwidth_dl;
2017-03-02 02:43:26 +00:00
2017-04-10 02:29:46 +00:00
list_append(&self.ue_list, ue);
2017-04-06 11:58:13 +00:00
return ue;
2017-03-02 02:43:26 +00:00
}
2017-04-06 11:58:13 +00:00
status_t hss_ue_remove(hss_ue_t *ue)
2017-03-02 02:43:26 +00:00
{
2017-04-06 11:58:13 +00:00
d_assert(ue, return CORE_ERROR, "Null param");
2017-03-02 02:43:26 +00:00
2017-04-06 11:58:13 +00:00
list_remove(&self.ue_list, ue);
pool_free_node(&hss_ue_pool, ue);
2017-03-02 02:43:26 +00:00
return CORE_OK;
}
2017-04-06 11:44:52 +00:00
status_t hss_ue_remove_all()
2017-03-02 02:43:26 +00:00
{
2017-04-06 11:58:13 +00:00
hss_ue_t *ue = NULL, *next_ue = NULL;
2017-03-02 02:43:26 +00:00
2017-04-06 11:58:13 +00:00
ue = list_first(&self.ue_list);
while (ue)
2017-03-02 02:43:26 +00:00
{
2017-04-06 11:58:13 +00:00
next_ue = list_next(ue);
2017-03-02 02:43:26 +00:00
2017-04-06 11:58:13 +00:00
hss_ue_remove(ue);
2017-03-02 02:43:26 +00:00
2017-04-06 11:58:13 +00:00
ue = next_ue;
2017-03-02 02:43:26 +00:00
}
return CORE_OK;
}
2017-04-10 02:29:46 +00:00
hss_ue_t* hss_ue_find_by_imsi_bcd(c_int8_t *imsi_bcd)
2017-03-02 02:43:26 +00:00
{
2017-04-06 11:58:13 +00:00
hss_ue_t *ue = NULL;
2017-03-02 02:43:26 +00:00
2017-04-06 11:58:13 +00:00
ue = list_first(&self.ue_list);
while (ue)
2017-03-02 02:43:26 +00:00
{
2017-04-10 02:29:46 +00:00
if (strcmp(ue->imsi_bcd, imsi_bcd) == 0)
2017-03-02 02:43:26 +00:00
break;
2017-04-06 11:58:13 +00:00
ue = list_next(ue);
2017-03-02 02:43:26 +00:00
}
2017-04-06 11:58:13 +00:00
return ue;
2017-03-02 02:43:26 +00:00
}
2017-04-06 11:44:52 +00:00
hss_ue_t* hss_ue_first()
2017-03-02 02:43:26 +00:00
{
2017-04-06 11:58:13 +00:00
return list_first(&self.ue_list);
2017-03-02 02:43:26 +00:00
}
2017-04-06 11:58:13 +00:00
hss_ue_t* hss_ue_next(hss_ue_t *ue)
2017-03-02 02:43:26 +00:00
{
2017-04-06 11:58:13 +00:00
return list_next(ue);
2017-03-02 02:43:26 +00:00
}