Commit f31e6e34 authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen

Fix issue for IPv6 address allocation

parent 94f86e2d
......@@ -19,6 +19,7 @@
* contact@openairinterface.org
*/
#include "string.hpp"
#include "logger.hpp"
#include <stdarg.h>
#include <algorithm>
......@@ -100,22 +101,33 @@ void util::ipv4_to_bstring(struct in_addr ipv4_address, bstring str) {
}
void util::ipv6_to_bstring(struct in6_addr ipv6_address, bstring str) {
unsigned char bitstream_addr[8];
for (int i = 0; i <= 7; i++)
bitstream_addr[i] = (uint8_t)(ipv6_address.s6_addr16[i]);
str->slen = 8;
memcpy(str->data, bitstream_addr, sizeof(bitstream_addr));
char str_addr6[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, &ipv6_address, str_addr6, sizeof(str_addr6))) {
std::string ipv6_addr_str((char*) str_addr6, INET6_ADDRSTRLEN);
//Logger::smf_app().info(" Ipv6 address....: %s", ipv6_addr_str.c_str());
unsigned char buf_in6_addr[sizeof(struct in6_addr)];
if (inet_pton(AF_INET6, util::trim(ipv6_addr_str).c_str(), buf_in6_addr) ==
1) {
str->slen = 16;
memcpy(str->data, buf_in6_addr, sizeof(buf_in6_addr));
}
}
}
void util::ipv4v6_to_bstring(
void util::ipv4v6_to_pdu_address_information(
struct in_addr ipv4_address, struct in6_addr ipv6_address, bstring str) {
unsigned char bitstream_addr[12];
// TODO: to be updated to remove the hardcoded value
std::string ipv6_addr = "2001:4860:4860:0:0:0:0:2";
char str_addr6[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, &ipv6_address, str_addr6, sizeof(str_addr6))) {
std::string ipv6_addr_str((char*) str_addr6, INET6_ADDRSTRLEN);
//Logger::smf_app().info(" Ipv6 address....: %s", ipv6_addr_str.c_str());
unsigned char buf_in6_addr[sizeof(struct in6_addr)];
if (inet_pton(AF_INET6, util::trim(ipv6_addr).c_str(), buf_in6_addr) == 1) {
// memcpy(&p, buf_in6_addr, sizeof(struct in6_addr));
for (int i = 0; i <= 7; i++) bitstream_addr[i] = (uint8_t)(buf_in6_addr[i]);
if (inet_pton(AF_INET6, util::trim(ipv6_addr_str).c_str(), buf_in6_addr) ==
1) {
for (int i = 0; i <= 7; i++)
bitstream_addr[i] = (uint8_t)(buf_in6_addr[i]);
}
}
bitstream_addr[8] = (uint8_t)((ipv4_address.s_addr) & 0x000000ff);
......
......@@ -46,8 +46,18 @@ std::string& rtrim(std::string& s);
std::string& trim(std::string& s);
void ipv4_to_bstring(struct in_addr ipv4_address, bstring str);
void ipv6_to_bstring(struct in6_addr ipv6_address, bstring str);
void ipv4v6_to_bstring(
/*
* Create a PDU Address Information in form of a bstring (byte 0-7: IPv6 prefix,
* 8-11: Ipv4 Address)
* @param [struct in_addr] ipv4_address: IPv4 address
* @param [struct in6_addr ] ipv6_address: IPv6 address
* @param [bstring] str: store the PDU Address Information
* @return void
*/
void ipv4v6_to_pdu_address_information(
struct in_addr ipv4_address, struct in6_addr ipv6_address, bstring str);
void string_to_bstring(const std::string& str, bstring bstr);
......
......@@ -82,6 +82,7 @@ void smf_app_task(void*);
int smf_app::apply_config(const smf_config& cfg) {
Logger::smf_app().info("Apply config...");
paa_t paa = {};
for (int ia = 0; ia < cfg.num_dnn; ia++) {
if (cfg.dnn[ia].pool_id_iv4 >= 0) {
int pool_id = cfg.dnn[ia].pool_id_iv4;
......@@ -91,13 +92,18 @@ int smf_app::apply_config(const smf_config& cfg) {
cfg.dnn[ia].dnn, pool_id, cfg.ue_pool_range_low[pool_id], range);
// TODO: check with dnn_label
Logger::smf_app().info("Applied config %s", cfg.dnn[ia].dnn.c_str());
paa.ipv4_address = cfg.ue_pool_range_low[pool_id];
}
if (cfg.dnn[ia].pool_id_iv6 >= 0) {
int pool_id = cfg.dnn[ia].pool_id_iv6;
paa_dynamic::get_instance().add_pool(
cfg.dnn[ia].dnn, pool_id, cfg.paa_pool6_prefix[pool_id],
cfg.paa_pool6_prefix_len[pool_id]);
paa.ipv6_address = cfg.paa_pool6_prefix[pool_id];
// TODO: check with dnn_label
Logger::smf_app().info(
"Applied config for IPv6 %s", cfg.dnn[ia].dnn.c_str());
}
}
......
......@@ -873,8 +873,14 @@ void smf_config::display() {
dnn[i].pool_id_iv4, range_low.c_str(), range_high.c_str());
}
if (dnn[i].pool_id_iv6 >= 0) {
if (inet_ntop(
AF_INET6, &paa_pool6_prefix[dnn[i].pool_id_iv6], str_addr6,
sizeof(str_addr6))) {
Logger::smf_app().info(
" " SMF_CONFIG_STRING_IPV6_POOL ": %d", dnn[i].pool_id_iv6);
" " SMF_CONFIG_STRING_IPV6_POOL ": %d (%s / %d)",
dnn[i].pool_id_iv6, str_addr6,
paa_pool6_prefix_len[dnn[i].pool_id_iv6]);
}
}
}
......
......@@ -176,7 +176,7 @@ bool smf_n1::create_n1_pdu_session_establishment_accept(
paa.pdu_session_type.pdu_session_type == PDU_SESSION_TYPE_E_IPV4V6) {
sm_msg->pdu_session_establishment_accept.pduaddress
.pdu_address_information = bfromcstralloc(12, "\0");
util::ipv4v6_to_bstring(
util::ipv4v6_to_pdu_address_information(
paa.ipv4_address, paa.ipv6_address,
sm_msg->pdu_session_establishment_accept.pduaddress
.pdu_address_information);
......
......@@ -182,13 +182,15 @@ class paa_dynamic {
ipv4_pool pool(first, range);
ipv4_pools[uint32pool_id] = pool;
}
if (!dnns.count(dnn_label)) {
dnn_dynamic_pools adp = {};
if (dnns.count(dnn_label)) {
adp = dnns[dnn_label];
}
adp.add_ipv4_pool_id(uint32pool_id);
dnns[dnn_label] = adp;
}
}
}
void add_pool(
const std::string& dnn_label, const int pool_id,
......@@ -199,13 +201,15 @@ class paa_dynamic {
ipv6_pool pool(prefix, prefix_len);
ipv6_pools[uint32pool_id] = pool;
}
if (!dnns.count(dnn_label)) {
dnn_dynamic_pools adp = {};
if (dnns.count(dnn_label)) {
adp = dnns[dnn_label];
}
adp.add_ipv6_pool_id(uint32pool_id);
dnns[dnn_label] = adp;
}
}
}
bool get_free_paa(const std::string& dnn_label, paa_t& paa) {
if (dnns.count(dnn_label)) {
......@@ -227,10 +231,13 @@ class paa_dynamic {
paa.pdu_session_type.pdu_session_type == PDU_SESSION_TYPE_E_IPV4V6) {
bool success = false;
std::vector<uint32_t>::const_iterator it4 = {};
uint32_t ipv4_pool_id = 0;
for (it4 = dnn_pool.ipv4_pool_ids.begin();
it4 != dnn_pool.ipv4_pool_ids.end(); ++it4) {
if (ipv4_pools[*it4].alloc_address(paa.ipv4_address)) {
success = true;
ipv4_pool_id = *it4;
break;
}
}
if (success) {
......@@ -241,16 +248,7 @@ class paa_dynamic {
return true;
}
}
// ipv4_pools[*it4].free_address(paa.ipv4_address);
// TODO: To be fixed the error for IPv6 addr allocation, so assign a
// fixed IPv6 addr here!
std::string ipv6_addr = "2001:4860:4860::2";
unsigned char buf_in6_addr[sizeof(struct in6_addr)];
if (inet_pton(
AF_INET6, util::trim(ipv6_addr).c_str(), buf_in6_addr) == 1) {
memcpy(&paa.ipv6_address, buf_in6_addr, sizeof(struct in6_addr));
return true;
}
ipv4_pools[ipv4_pool_id].free_address(paa.ipv4_address);
}
Logger::smf_app().warn(
"Could not get PAA PDU_SESSION_TYPE_E_IPV4V6 for DNN %s",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment