open5gs/src/hss/hss_context.c

155 lines
3.3 KiB
C
Raw Normal View History

2017-03-04 14:21:05 +00:00
#define TRACE_MODULE _hss_ctx
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-03-02 13:47:43 +00:00
#define OP "5F1D289C5D354D0A140C2548F5F3E3BA"
#define OPc "E8ED289DEBA952E4283B54E88E6183CA"
#define AMF "8000"
static hss_ctx_t self;
2017-04-04 05:00:02 +00:00
pool_declare(user_pool, user_ctx_t, MAX_NUM_OF_UE);
2017-03-02 02:43:26 +00:00
2017-04-04 05:00:02 +00:00
static list_t g_user_list;
2017-03-02 02:43:26 +00:00
2017-03-02 13:47:43 +00:00
hss_ctx_t* hss_self()
{
return &self;
}
2017-03-02 02:43:26 +00:00
status_t hss_ctx_init(void)
{
2017-03-24 12:19:24 +00:00
char buf[HSS_KEY_LEN];
2017-04-04 05:00:02 +00:00
user_ctx_t *user;
2017-03-02 13:47:43 +00:00
2017-04-04 05:00:02 +00:00
pool_init(&user_pool, MAX_NUM_OF_UE);
2017-03-02 13:47:43 +00:00
memset(&self, 0, sizeof(hss_ctx_t));
2017-03-25 01:45:49 +00:00
memcpy(self.op, CORE_HEX(OP, strlen(OP), buf), HSS_KEY_LEN);
memcpy(self.amf, CORE_HEX(AMF, strlen(AMF), buf), HSS_AMF_LEN);
2017-03-07 07:19:18 +00:00
#define K "465B5CE8B199B49FAA5F0A2EE238A6BC"
#define UE1_IMSI "001010123456800"
#define UE2_IMSI "001010123456796"
#define UE3_IMSI "001010123456819"
#define UE3_RAND "20080c3818183b52 2614162c07601d0d"
2017-04-04 05:00:02 +00:00
user = hss_ctx_user_add();
d_assert(user, return -1, "UE context add failed");
2017-03-07 07:19:18 +00:00
2017-04-04 05:00:02 +00:00
strcpy((char*)user->imsi, UE1_IMSI);
user->imsi_len = strlen(UE1_IMSI);
memcpy(user->k, CORE_HEX(K, strlen(K), buf), HSS_KEY_LEN);
core_generate_random_bytes(user->rand, RAND_LEN);
user->sqn = 64;
2017-03-07 07:19:18 +00:00
2017-04-04 05:00:02 +00:00
user = hss_ctx_user_add();
d_assert(user, return -1, "UE context add failed");
2017-03-07 07:19:18 +00:00
2017-04-04 05:00:02 +00:00
strcpy((char*)user->imsi, UE2_IMSI);
user->imsi_len = strlen(UE2_IMSI);
memcpy(user->k, CORE_HEX(K, strlen(K), buf), HSS_KEY_LEN);
core_generate_random_bytes(user->rand, RAND_LEN);
user->sqn = 64;
2017-03-07 07:19:18 +00:00
2017-04-04 05:00:02 +00:00
user = hss_ctx_user_add();
d_assert(user, return -1, "UE context add failed");
2017-03-07 07:19:18 +00:00
2017-04-04 05:00:02 +00:00
strcpy((char*)user->imsi, UE3_IMSI);
user->imsi_len = strlen(UE3_IMSI);
memcpy(user->k, CORE_HEX(K, strlen(K), buf), HSS_KEY_LEN);
memcpy(user->rand, CORE_HEX(UE3_RAND, strlen(UE3_RAND), buf),
2017-03-25 01:45:49 +00:00
RAND_LEN);
2017-04-04 05:00:02 +00:00
user->sqn = 64;
2017-03-07 07:19:18 +00:00
2017-03-02 02:43:26 +00:00
return CORE_OK;
}
void hss_ctx_final(void)
{
2017-04-04 05:00:02 +00:00
hss_ctx_user_remove_all();
2017-03-07 07:19:18 +00:00
2017-04-04 05:00:02 +00:00
pool_final(&user_pool);
2017-03-02 02:43:26 +00:00
return;
}
2017-04-04 05:00:02 +00:00
user_ctx_t* hss_ctx_user_add()
2017-03-02 02:43:26 +00:00
{
2017-04-04 05:00:02 +00:00
user_ctx_t *user = NULL;
2017-03-02 02:43:26 +00:00
/* Allocate new eNB context */
2017-04-04 05:00:02 +00:00
pool_alloc_node(&user_pool, &user);
d_assert(user, return NULL, "HSS-UE context allocation failed");
2017-03-02 02:43:26 +00:00
/* Initialize eNB context */
2017-04-04 05:00:02 +00:00
memset(user, 0, sizeof(user_ctx_t));
2017-03-02 02:43:26 +00:00
/* Add new eNB context to list */
2017-04-04 05:00:02 +00:00
list_append(&g_user_list, user);
2017-03-03 07:22:09 +00:00
2017-04-04 05:00:02 +00:00
memcpy(user->op, self.op, HSS_KEY_LEN);
memcpy(user->amf, self.amf, HSS_AMF_LEN);
2017-03-02 02:43:26 +00:00
2017-04-04 05:00:02 +00:00
return user;
2017-03-02 02:43:26 +00:00
}
2017-04-04 05:00:02 +00:00
status_t hss_ctx_user_remove(user_ctx_t *user)
2017-03-02 02:43:26 +00:00
{
2017-04-04 05:00:02 +00:00
d_assert(user, return CORE_ERROR, "Null param");
2017-03-02 02:43:26 +00:00
2017-04-04 05:00:02 +00:00
list_remove(&g_user_list, user);
pool_free_node(&user_pool, user);
2017-03-02 02:43:26 +00:00
return CORE_OK;
}
2017-04-04 05:00:02 +00:00
status_t hss_ctx_user_remove_all()
2017-03-02 02:43:26 +00:00
{
2017-04-04 05:00:02 +00:00
user_ctx_t *user = NULL, *next_user = NULL;
2017-03-02 02:43:26 +00:00
2017-04-04 05:00:02 +00:00
user = list_first(&g_user_list);
while (user)
2017-03-02 02:43:26 +00:00
{
2017-04-04 05:00:02 +00:00
next_user = list_next(user);
2017-03-02 02:43:26 +00:00
2017-04-04 05:00:02 +00:00
hss_ctx_user_remove(user);
2017-03-02 02:43:26 +00:00
2017-04-04 05:00:02 +00:00
user = next_user;
2017-03-02 02:43:26 +00:00
}
return CORE_OK;
}
2017-04-04 05:00:02 +00:00
user_ctx_t* hss_ctx_user_find_by_imsi(c_uint8_t *imsi, c_uint8_t imsi_len)
2017-03-02 02:43:26 +00:00
{
2017-04-04 05:00:02 +00:00
user_ctx_t *user = NULL;
2017-03-02 02:43:26 +00:00
2017-04-04 05:00:02 +00:00
user = list_first(&g_user_list);
while (user)
2017-03-02 02:43:26 +00:00
{
2017-04-04 05:00:02 +00:00
if (memcmp(user->imsi, imsi, imsi_len) == 0)
2017-03-02 02:43:26 +00:00
break;
2017-04-04 05:00:02 +00:00
user = list_next(user);
2017-03-02 02:43:26 +00:00
}
2017-04-04 05:00:02 +00:00
return user;
2017-03-02 02:43:26 +00:00
}
2017-04-04 05:00:02 +00:00
user_ctx_t* hss_ctx_user_first()
2017-03-02 02:43:26 +00:00
{
2017-04-04 05:00:02 +00:00
return list_first(&g_user_list);
2017-03-02 02:43:26 +00:00
}
2017-04-04 05:00:02 +00:00
user_ctx_t* hss_ctx_user_next(user_ctx_t *user)
2017-03-02 02:43:26 +00:00
{
2017-04-04 05:00:02 +00:00
return list_next(user);
2017-03-02 02:43:26 +00:00
}