open5gs/src/pcrf/pcrf-context.c

347 lines
14 KiB
C
Raw Normal View History

2019-07-11 13:22:22 +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 "pcrf-context.h"
2017-08-17 05:15:08 +00:00
static pcrf_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 __pcrf_log_domain;
2017-08-17 05:15:08 +00:00
static int context_initialized = 0;
pcrf_context_t *pcrf_self(void)
2017-08-17 05:15:08 +00:00
{
return &self;
}
2019-05-06 11:43:50 +00:00
void pcrf_context_init(void)
2017-08-17 05:15:08 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_assert(context_initialized == 0);
2017-08-17 05:15:08 +00:00
/* Initial FreeDiameter Config */
2019-09-13 12:07:47 +00:00
memset(&g_diam_conf, 0, sizeof(ogs_diam_config_t));
2017-08-17 05:15:08 +00:00
/* Initialize PCRF context */
memset(&self, 0, sizeof(pcrf_context_t));
2019-09-13 12:07:47 +00:00
self.diam_config = &g_diam_conf;
2017-08-17 05:15:08 +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(&__pcrf_log_domain, "pcrf", ogs_core()->log.level);
ogs_thread_mutex_init(&self.db_lock);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_init(&self.hash_lock);
self.ip_hash = ogs_hash_make();
2021-06-06 13:35:46 +00:00
ogs_assert(self.ip_hash);
2017-08-17 05:15:08 +00:00
context_initialized = 1;
}
2019-05-06 11:43:50 +00:00
void pcrf_context_final(void)
2017-08-17 05:15:08 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_assert(context_initialized == 1);
ogs_assert(self.ip_hash);
ogs_hash_destroy(self.ip_hash);
ogs_thread_mutex_destroy(&self.hash_lock);
2017-08-17 05:15:08 +00:00
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_destroy(&self.db_lock);
2017-08-17 05:15:08 +00:00
context_initialized = 0;
}
static int pcrf_context_prepare(void)
2017-08-17 05:15:08 +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;
2022-02-18 21:33:46 +00:00
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-08-17 05:15:08 +00:00
}
static int pcrf_context_validation(void)
2017-08-17 05:15:08 +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)) {
2019-04-27 14:54:30 +00:00
ogs_error("No pcrf.freeDiameter in '%s'",
ogs_app()->file);
2019-04-27 14:54:30 +00:00
return OGS_ERROR;
2017-08-17 05:15:08 +00:00
}
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-08-17 05:15:08 +00:00
}
int pcrf_context_parse_config(void)
2017-08-17 05:15:08 +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-08-17 05:15:08 +00:00
document = ogs_app()->document;
2019-04-27 14:54:30 +00:00
ogs_assert(document);
2017-08-17 05:15:08 +00:00
rv = pcrf_context_prepare();
2019-04-27 14:54:30 +00:00
if (rv != OGS_OK) return rv;
2017-08-17 05:15:08 +00:00
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_init(&root_iter, document);
2019-07-11 13:22:22 +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:22:22 +00:00
if (!strcmp(root_key, "pcrf")) {
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_t pcrf_iter;
ogs_yaml_iter_recurse(&root_iter, &pcrf_iter);
2019-07-11 13:22:22 +00:00
while (ogs_yaml_iter_next(&pcrf_iter)) {
2019-04-27 14:54:30 +00:00
const char *pcrf_key = ogs_yaml_iter_key(&pcrf_iter);
ogs_assert(pcrf_key);
2019-07-11 13:22:22 +00:00
if (!strcmp(pcrf_key, "freeDiameter")) {
2022-02-18 21:33:46 +00:00
yaml_node_t *node =
yaml_document_get_node(document, pcrf_iter.pair->value);
2019-04-27 14:54:30 +00:00
ogs_assert(node);
2019-07-11 13:22:22 +00:00
if (node->type == YAML_SCALAR_NODE) {
2019-09-13 12:07:47 +00:00
self.diam_conf_path = ogs_yaml_iter_value(&pcrf_iter);
2019-07-11 13:22:22 +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(&pcrf_iter, &fd_iter);
2019-07-11 13:22:22 +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:22:22 +00:00
if (!strcmp(fd_key, "identity")) {
2022-02-18 21:33:46 +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:22:22 +00:00
} else if (!strcmp(fd_key, "realm")) {
2022-02-18 21:33:46 +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:22:22 +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:22:22 +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:22:22 +00:00
} else if (!strcmp(fd_key, "listen_on")) {
2022-02-18 21:33:46 +00:00
self.diam_config->cnf_addr =
2019-04-27 14:54:30 +00:00
ogs_yaml_iter_value(&fd_iter);
2021-06-21 13:36:38 +00:00
} else if (!strcmp(fd_key, "no_fwd")) {
self.diam_config->cnf_flags.no_fwd =
ogs_yaml_iter_bool(&fd_iter);
2019-07-11 13:22:22 +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:22:22 +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:22:22 +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:22:22 +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:22:22 +00:00
} else if (ogs_yaml_iter_type(&ext_array) ==
YAML_SCALAR_NODE) {
break;
2019-07-11 13:22:22 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_assert_if_reached();
2019-07-11 13:22:22 +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);
if (!strcmp(ext_key, "module"))
{
2019-07-11 13:22:22 +00:00
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:22:22 +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-04-27 14:54:30 +00:00
} while(ogs_yaml_iter_type(&ext_array) ==
YAML_SEQUENCE_NODE);
2019-07-11 13:22:22 +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:22:22 +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:22:22 +00:00
YAML_MAPPING_NODE) {
memcpy(&conn_iter, &conn_array,
2019-04-27 14:54:30 +00:00
sizeof(ogs_yaml_iter_t));
2019-07-11 13:22:22 +00:00
} else if (ogs_yaml_iter_type(&conn_array)
== YAML_SEQUENCE_NODE) {
2019-04-27 14:54:30 +00:00
if (!ogs_yaml_iter_next(&conn_array))
break;
2020-04-26 19:36:05 +00:00
ogs_yaml_iter_recurse(
&conn_array, &conn_iter);
2019-07-11 13:22:22 +00:00
} else if (ogs_yaml_iter_type(&conn_array)
== YAML_SCALAR_NODE) {
break;
2019-07-11 13:22:22 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_assert_if_reached();
2019-07-11 13:22:22 +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:22:22 +00:00
if (!strcmp(conn_key, "identity")) {
identity = ogs_yaml_iter_value(
&conn_iter);
} else if (!strcmp(conn_key, "addr")) {
addr = ogs_yaml_iter_value(
&conn_iter);
} 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:22:22 +00:00
} else
ogs_warn("unknown key `%s`",
conn_key);
}
2019-07-11 13:22:22 +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:22:22 +00:00
} while (ogs_yaml_iter_type(&conn_array) ==
YAML_SEQUENCE_NODE);
2019-07-11 13:22:22 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_warn("unknown key `%s`", fd_key);
}
}
2019-07-11 13:22:22 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_warn("unknown key `%s`", pcrf_key);
2017-08-17 05:15:08 +00:00
}
}
}
rv = pcrf_context_validation();
2019-04-27 14:54:30 +00:00
if (rv != OGS_OK) return rv;
2017-08-17 05:15:08 +00:00
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-08-17 05:15:08 +00:00
}
2021-01-01 02:07:08 +00:00
int pcrf_db_qos_data(
char *imsi_bcd, char *apn, ogs_session_data_t *session_data)
2017-08-17 05:15:08 +00:00
{
int rv, i;
2021-01-01 02:07:08 +00:00
char *supi = NULL;
2017-08-24 08:05:10 +00:00
2019-04-27 14:54:30 +00:00
ogs_assert(imsi_bcd);
ogs_assert(apn);
2021-01-01 02:07:08 +00:00
ogs_assert(session_data);
2017-08-24 08:05:10 +00:00
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_lock(&self.db_lock);
2021-01-01 02:07:08 +00:00
supi = ogs_msprintf("%s-%s", OGS_ID_SUPI_TYPE_IMSI, imsi_bcd);
ogs_assert(supi);
2017-08-24 08:05:10 +00:00
/* For EPC, we'll use [S_NSSAI = NULL] */
rv = ogs_dbi_session_data(supi, NULL, apn, OGS_DBI_NO_CHARGING_CHAR, session_data);
2017-08-24 08:05:10 +00:00
/* For EPC, we need to inialize Flow-Status in Pcc-Rule */
for (i = 0; i < session_data->num_of_pcc_rule; i++) {
ogs_pcc_rule_t *pcc_rule = &session_data->pcc_rule[i];
pcc_rule->flow_status = OGS_DIAM_RX_FLOW_STATUS_ENABLED;
}
2021-01-01 02:07:08 +00:00
ogs_free(supi);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.db_lock);
2017-08-24 08:05:10 +00:00
return rv;
}
2021-04-05 08:09:39 +00:00
void pcrf_sess_set_ipv4(const void *key, uint8_t *sid)
{
2019-04-27 14:54:30 +00:00
ogs_assert(self.ip_hash);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_lock(&self.hash_lock);
2019-09-13 12:07:47 +00:00
ogs_hash_set(self.ip_hash, key, OGS_IPV4_LEN, sid);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.hash_lock);
}
2021-04-05 08:09:39 +00:00
void pcrf_sess_set_ipv6(const void *key, uint8_t *sid)
{
2019-04-27 14:54:30 +00:00
ogs_assert(self.ip_hash);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_lock(&self.hash_lock);
2021-03-15 01:01:55 +00:00
ogs_hash_set(self.ip_hash, key, OGS_IPV6_DEFAULT_PREFIX_LEN >> 3, sid);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.hash_lock);
}
2019-04-27 14:54:30 +00:00
uint8_t *pcrf_sess_find_by_ipv4(const void *key)
{
2019-04-27 14:54:30 +00:00
uint8_t *sid = NULL;
ogs_assert(key);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_lock(&self.hash_lock);
2019-09-13 12:07:47 +00:00
sid = (uint8_t *)ogs_hash_get(self.ip_hash, key, OGS_IPV4_LEN);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.hash_lock);
2022-02-18 21:33:46 +00:00
return sid;
}
2019-04-27 14:54:30 +00:00
uint8_t *pcrf_sess_find_by_ipv6(const void *key)
{
2019-04-27 14:54:30 +00:00
uint8_t *sid = NULL;
ogs_assert(key);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_lock(&self.hash_lock);
2021-03-15 01:01:55 +00:00
sid = (uint8_t *)ogs_hash_get(
self.ip_hash, key, OGS_IPV6_DEFAULT_PREFIX_LEN >> 3);
2019-04-27 14:54:30 +00:00
ogs_thread_mutex_unlock(&self.hash_lock);
return sid;
}