open5gs/src/hss/hss-context.c

338 lines
13 KiB
C
Raw Normal View History

2019-07-11 13:16:32 +00:00
/*
* Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
*
* This file is part of Open5GS.
*
* This program is free software: you can redistribute it and/or modify
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-09-13 12:07:47 +00:00
#include "ogs-dbi.h"
2019-06-11 13:10:47 +00:00
#include "hss-context.h"
2017-03-02 02:43:26 +00:00
2017-04-06 11:44:52 +00:00
static hss_context_t self;
2019-09-13 12:07:47 +00:00
static ogs_diam_config_t g_diam_conf;
2019-04-27 14:54:30 +00:00
int __hss_log_domain;
2017-07-22 00:44:42 +00:00
static int context_initialized = 0;
2017-03-02 02:43:26 +00:00
hss_context_t* hss_self(void)
2017-03-02 13:47:43 +00:00
{
return &self;
}
2019-05-06 11:43:50 +00:00
void hss_context_init(void)
2017-03-02 02:43:26 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_assert(context_initialized == 0);
2017-03-02 13:47:43 +00:00
/* Initial FreeDiameter Config */
2019-09-13 12:07:47 +00:00
memset(&g_diam_conf, 0, sizeof(ogs_diam_config_t));
2017-07-22 00:44:42 +00:00
/* Initialize HSS context */
2017-04-06 11:44:52 +00:00
memset(&self, 0, sizeof(hss_context_t));
2019-09-13 12:07:47 +00:00
self.diam_config = &g_diam_conf;
2017-03-02 13:47:43 +00:00
2019-09-13 12:07:47 +00:00
ogs_log_install_domain(&__ogs_diam_domain, "diam", ogs_core()->log.level);
ogs_log_install_domain(&__ogs_dbi_domain, "dbi", ogs_core()->log.level);
2019-04-27 14:54:30 +00:00
ogs_log_install_domain(&__hss_log_domain, "hss", ogs_core()->log.level);
ogs_thread_mutex_init(&self.db_lock);
2017-07-22 01:52:13 +00:00
2017-07-22 00:44:42 +00:00
context_initialized = 1;
2017-03-02 02:43:26 +00:00
}
2019-05-06 11:43:50 +00:00
void hss_context_final(void)
2017-04-10 06:56:01 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_assert(context_initialized == 1);
2017-04-10 06:56:01 +00:00
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_destroy(&self.db_lock);
2017-07-22 01:52:13 +00:00
2017-07-22 00:44:42 +00:00
context_initialized = 0;
2017-04-10 06:56:01 +00:00
}
static int hss_context_prepare(void)
2017-07-30 13:29:27 +00:00
{
2019-09-13 12:07:47 +00:00
self.diam_config->cnf_port = DIAMETER_PORT;
self.diam_config->cnf_port_tls = DIAMETER_SECURE_PORT;
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-07-30 13:29:27 +00:00
}
static int hss_context_validation(void)
2017-07-30 13:29:27 +00:00
{
2019-09-13 12:07:47 +00:00
if (self.diam_conf_path == NULL &&
(self.diam_config->cnf_diamid == NULL ||
self.diam_config->cnf_diamrlm == NULL ||
self.diam_config->cnf_addr == NULL)) {
ogs_error("No hss.freeDiameter in '%s'", ogs_app()->file);
2019-04-27 14:54:30 +00:00
return OGS_ERROR;
2017-07-30 13:29:27 +00:00
}
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-07-30 13:29:27 +00:00
}
int hss_context_parse_config(void)
2017-07-30 13:29:27 +00:00
{
2019-04-27 14:54:30 +00:00
int rv;
2017-11-29 00:51:34 +00:00
yaml_document_t *document = NULL;
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_t root_iter;
2017-07-30 13:29:27 +00:00
document = ogs_app()->document;
2019-04-27 14:54:30 +00:00
ogs_assert(document);
2017-07-30 13:29:27 +00:00
rv = hss_context_prepare();
2019-04-27 14:54:30 +00:00
if (rv != OGS_OK) return rv;
2017-07-30 13:29:27 +00:00
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_init(&root_iter, document);
2019-07-11 13:16:32 +00:00
while (ogs_yaml_iter_next(&root_iter)) {
2019-04-27 14:54:30 +00:00
const char *root_key = ogs_yaml_iter_key(&root_iter);
ogs_assert(root_key);
2019-07-11 13:16:32 +00:00
if (!strcmp(root_key, "hss")) {
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_t hss_iter;
ogs_yaml_iter_recurse(&root_iter, &hss_iter);
2019-07-11 13:16:32 +00:00
while (ogs_yaml_iter_next(&hss_iter)) {
2019-04-27 14:54:30 +00:00
const char *hss_key = ogs_yaml_iter_key(&hss_iter);
ogs_assert(hss_key);
2019-07-11 13:16:32 +00:00
if (!strcmp(hss_key, "freeDiameter")) {
yaml_node_t *node =
yaml_document_get_node(document, hss_iter.pair->value);
2019-04-27 14:54:30 +00:00
ogs_assert(node);
2019-07-11 13:16:32 +00:00
if (node->type == YAML_SCALAR_NODE) {
2019-09-13 12:07:47 +00:00
self.diam_conf_path = ogs_yaml_iter_value(&hss_iter);
2019-07-11 13:16:32 +00:00
} else if (node->type == YAML_MAPPING_NODE) {
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_t fd_iter;
ogs_yaml_iter_recurse(&hss_iter, &fd_iter);
2019-07-11 13:16:32 +00:00
while (ogs_yaml_iter_next(&fd_iter)) {
2019-04-27 14:54:30 +00:00
const char *fd_key = ogs_yaml_iter_key(&fd_iter);
ogs_assert(fd_key);
2019-07-11 13:16:32 +00:00
if (!strcmp(fd_key, "identity")) {
2019-09-13 12:07:47 +00:00
self.diam_config->cnf_diamid =
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_value(&fd_iter);
2019-07-11 13:16:32 +00:00
} else if (!strcmp(fd_key, "realm")) {
2019-09-13 12:07:47 +00:00
self.diam_config->cnf_diamrlm =
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_value(&fd_iter);
2019-07-11 13:16:32 +00:00
} else if (!strcmp(fd_key, "port")) {
2019-04-27 14:54:30 +00:00
const char *v = ogs_yaml_iter_value(&fd_iter);
2019-09-13 12:07:47 +00:00
if (v) self.diam_config->cnf_port = atoi(v);
2019-07-11 13:16:32 +00:00
} else if (!strcmp(fd_key, "sec_port")) {
2019-04-27 14:54:30 +00:00
const char *v = ogs_yaml_iter_value(&fd_iter);
2019-09-13 12:07:47 +00:00
if (v) self.diam_config->cnf_port_tls = atoi(v);
2019-07-11 13:16:32 +00:00
} else if (!strcmp(fd_key, "listen_on")) {
2019-09-13 12:07:47 +00:00
self.diam_config->cnf_addr =
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_value(&fd_iter);
2019-07-11 13:16:32 +00:00
} else if (!strcmp(fd_key, "load_extension")) {
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_t ext_array, ext_iter;
ogs_yaml_iter_recurse(&fd_iter, &ext_array);
2019-07-11 13:16:32 +00:00
do {
const char *module = NULL;
const char *conf = NULL;
2019-04-27 14:54:30 +00:00
if (ogs_yaml_iter_type(&ext_array) ==
2019-07-11 13:16:32 +00:00
YAML_MAPPING_NODE) {
memcpy(&ext_iter, &ext_array,
2019-04-27 14:54:30 +00:00
sizeof(ogs_yaml_iter_t));
2019-07-11 13:16:32 +00:00
} else if (ogs_yaml_iter_type(&ext_array) ==
YAML_SEQUENCE_NODE) {
2019-04-27 14:54:30 +00:00
if (!ogs_yaml_iter_next(&ext_array))
break;
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_recurse(
&ext_array, &ext_iter);
2019-07-11 13:16:32 +00:00
} else if (ogs_yaml_iter_type(&ext_array) ==
YAML_SCALAR_NODE) {
break;
2019-07-11 13:16:32 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_assert_if_reached();
2019-07-11 13:16:32 +00:00
while (ogs_yaml_iter_next(&ext_iter)) {
const char *ext_key =
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_key(&ext_iter);
ogs_assert(ext_key);
2019-07-11 13:16:32 +00:00
if (!strcmp(ext_key, "module")) {
module = ogs_yaml_iter_value(
&ext_iter);
} else if (!strcmp(ext_key, "conf")) {
conf = ogs_yaml_iter_value(
&ext_iter);
} else
ogs_warn("unknown key `%s`",
ext_key);
}
2019-07-11 13:16:32 +00:00
if (module) {
2019-09-13 12:07:47 +00:00
self.diam_config->
ext[self.diam_config->num_of_ext].
module = module;
2019-09-13 12:07:47 +00:00
self.diam_config->
ext[self.diam_config->num_of_ext].
conf = conf;
2019-09-13 12:07:47 +00:00
self.diam_config->num_of_ext++;
}
2019-07-11 13:16:32 +00:00
} while (ogs_yaml_iter_type(&ext_array) ==
YAML_SEQUENCE_NODE);
2019-07-11 13:16:32 +00:00
} else if (!strcmp(fd_key, "connect")) {
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_t conn_array, conn_iter;
ogs_yaml_iter_recurse(&fd_iter, &conn_array);
2019-07-11 13:16:32 +00:00
do {
const char *identity = NULL;
const char *addr = NULL;
2019-04-27 14:54:30 +00:00
uint16_t port = 0;
2019-04-27 14:54:30 +00:00
if (ogs_yaml_iter_type(&conn_array) ==
2019-07-11 13:16:32 +00:00
YAML_MAPPING_NODE) {
memcpy(&conn_iter, &conn_array,
2019-04-27 14:54:30 +00:00
sizeof(ogs_yaml_iter_t));
2020-06-04 18:12:05 +00:00
} else if (ogs_yaml_iter_type(
&conn_array) ==
2019-07-11 13:16:32 +00:00
YAML_SEQUENCE_NODE) {
2019-04-27 14:54:30 +00:00
if (!ogs_yaml_iter_next(&conn_array))
break;
2020-06-04 18:12:05 +00:00
ogs_yaml_iter_recurse(
&conn_array, &conn_iter);
} else if (ogs_yaml_iter_type(
&conn_array) ==
2019-07-11 13:16:32 +00:00
YAML_SCALAR_NODE) {
break;
2019-07-11 13:16:32 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_assert_if_reached();
2019-07-11 13:16:32 +00:00
while (ogs_yaml_iter_next(&conn_iter)) {
const char *conn_key =
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_key(&conn_iter);
ogs_assert(conn_key);
2019-07-11 13:16:32 +00:00
if (!strcmp(conn_key, "identity")) {
identity = ogs_yaml_iter_value(
&conn_iter);
} else if (!strcmp(conn_key, "addr")) {
2020-06-04 18:12:05 +00:00
addr = ogs_yaml_iter_value(
&conn_iter);
2019-07-11 13:16:32 +00:00
} else if (!strcmp(conn_key, "port")) {
const char *v =
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_value(&conn_iter);
if (v) port = atoi(v);
2019-07-11 13:16:32 +00:00
} else
2020-06-04 18:12:05 +00:00
ogs_warn("unknown key `%s`",
conn_key);
}
2019-07-11 13:16:32 +00:00
if (identity && addr) {
2019-09-13 12:07:47 +00:00
self.diam_config->
conn[self.diam_config->num_of_conn].
identity = identity;
2019-09-13 12:07:47 +00:00
self.diam_config->
conn[self.diam_config->num_of_conn].
addr = addr;
2019-09-13 12:07:47 +00:00
self.diam_config->
conn[self.diam_config->num_of_conn].
port = port;
2019-09-13 12:07:47 +00:00
self.diam_config->num_of_conn++;
}
2019-07-11 13:16:32 +00:00
} while (ogs_yaml_iter_type(&conn_array) ==
YAML_SEQUENCE_NODE);
2019-07-11 13:16:32 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_warn("unknown key `%s`", fd_key);
}
}
2019-07-11 13:16:32 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_warn("unknown key `%s`", hss_key);
2017-07-30 13:29:27 +00:00
}
}
}
rv = hss_context_validation();
2019-04-27 14:54:30 +00:00
if (rv != OGS_OK) return rv;
2017-07-31 13:35:25 +00:00
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-07-31 13:35:25 +00:00
}
int hss_db_auth_info(char *imsi_bcd, ogs_dbi_auth_info_t *auth_info)
2017-04-10 06:56:01 +00:00
{
2019-09-13 12:07:47 +00:00
int rv;
char *supi = NULL;
2017-04-10 06:56:01 +00:00
2019-04-27 14:54:30 +00:00
ogs_assert(imsi_bcd);
ogs_assert(auth_info);
2017-04-10 06:56:01 +00:00
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_lock(&self.db_lock);
supi = ogs_msprintf("%s-%s", OGS_ID_SUPI_TYPE_IMSI, imsi_bcd);
ogs_assert(supi);
2017-07-22 01:52:13 +00:00
rv = ogs_dbi_auth_info(supi, auth_info);
2017-04-10 02:29:46 +00:00
ogs_free(supi);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.db_lock);
2017-07-22 01:52:13 +00:00
return rv;
2017-04-10 02:29:46 +00:00
}
2019-04-27 14:54:30 +00:00
int hss_db_update_rand_and_sqn(
char *imsi_bcd, uint8_t *rand, uint64_t sqn)
2017-04-10 02:29:46 +00:00
{
int rv;
char *supi = NULL;
2017-07-22 00:44:42 +00:00
ogs_assert(imsi_bcd);
2017-07-22 00:44:42 +00:00
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_lock(&self.db_lock);
supi = ogs_msprintf("%s-%s", OGS_ID_SUPI_TYPE_IMSI, imsi_bcd);
ogs_assert(supi);
2017-07-22 01:52:13 +00:00
rv = ogs_dbi_update_sqn(supi, sqn);
2017-07-22 00:44:42 +00:00
ogs_free(supi);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.db_lock);
2017-07-22 01:52:13 +00:00
return rv;
2017-04-10 02:29:46 +00:00
}
2019-04-27 14:54:30 +00:00
int hss_db_increment_sqn(char *imsi_bcd)
2017-04-10 02:29:46 +00:00
{
int rv;
char *supi = NULL;
2017-07-22 00:44:42 +00:00
ogs_assert(imsi_bcd);
2017-07-22 00:44:42 +00:00
ogs_thread_mutex_lock(&self.db_lock);
supi = ogs_msprintf("%s-%s", OGS_ID_SUPI_TYPE_IMSI, imsi_bcd);
ogs_assert(supi);
2017-04-10 02:29:46 +00:00
rv = ogs_dbi_increment_sqn(supi);
2017-07-22 01:52:13 +00:00
ogs_free(supi);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.db_lock);
2017-03-02 02:43:26 +00:00
2017-07-22 04:05:43 +00:00
return rv;
2017-03-02 02:43:26 +00:00
}
2019-04-27 14:54:30 +00:00
int hss_db_subscription_data(
char *imsi_bcd, ogs_subscription_data_t *subscription_data)
2017-03-02 02:43:26 +00:00
{
int rv;
char *supi = NULL;
2017-07-22 00:44:42 +00:00
2019-04-27 14:54:30 +00:00
ogs_assert(imsi_bcd);
ogs_assert(subscription_data);
2017-07-22 00:44:42 +00:00
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_lock(&self.db_lock);
supi = ogs_msprintf("%s-%s", OGS_ID_SUPI_TYPE_IMSI, imsi_bcd);
ogs_assert(supi);
2017-07-22 01:52:13 +00:00
rv = ogs_dbi_subscription_data(supi, subscription_data);
2017-07-22 01:52:13 +00:00
ogs_free(supi);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.db_lock);
2017-03-02 02:43:26 +00:00
2017-07-22 04:05:43 +00:00
return rv;
2017-03-02 02:43:26 +00:00
}