From 2da09aba806a4ac29b9f979cf923c4725213973c Mon Sep 17 00:00:00 2001
From: Robert Schmidt <robert.schmidt@openairinterface.org>
Date: Tue, 9 Jul 2024 09:57:59 +0200
Subject: [PATCH] nas_config(): provide entire IP address to configure

Prior to this commit, there was a global variable baseNetAddress that
could be set independently through a setter and also through the
configuration module. This baseNetAddress (16 bits IPv4) would then be
complemented with two more bytes in nas_config().

However, this is counter-productive, as not only we have a global
variable that is avoidable (we can give the entire address to
nas_config() directly), but it also would not work with IPv6. Hence,
modify to give nas_config() the entire address. A follow-up commit will
add IPv6 support.
---
 openair2/LAYER2/PDCP_v10.1.0/pdcp.c           | 12 +++---
 openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c     |  7 ++--
 openair2/RRC/LTE/rrc_UE.c                     | 11 ++++--
 openair2/RRC/LTE/rrc_eNB.c                    |  7 ++--
 openair2/RRC/NAS/nas_config.c                 | 37 +++----------------
 openair2/RRC/NAS/nas_config.h                 | 35 ++++--------------
 .../ESM/MSG/PduSessionEstablishmentAccept.c   | 30 ++++++++-------
 openair3/NAS/NR_UE/nr_nas_msg_sim.c           | 25 +++++--------
 8 files changed, 56 insertions(+), 108 deletions(-)

diff --git a/openair2/LAYER2/PDCP_v10.1.0/pdcp.c b/openair2/LAYER2/PDCP_v10.1.0/pdcp.c
index 992357e8e79..3a64bbf68ef 100644
--- a/openair2/LAYER2/PDCP_v10.1.0/pdcp.c
+++ b/openair2/LAYER2/PDCP_v10.1.0/pdcp.c
@@ -2296,28 +2296,26 @@ uint64_t pdcp_module_init( uint64_t pdcp_optmask, int id) {
 
   pdcp_params.optmask = pdcp_params.optmask | pdcp_optmask ;
 
-  nas_getparams();
-
   if (UE_NAS_USE_TUN) {
     int num_if = (NFAPI_MODE == NFAPI_UE_STUB_PNF || IS_SOFTMODEM_SIML1 || NFAPI_MODE == NFAPI_MODE_STANDALONE_PNF) ? MAX_MOBILES_PER_ENB : 1;
     netlink_init_tun("oaitun_ue", num_if, id);
     if (IS_SOFTMODEM_NOS1)
-      nas_config(1, 1, 2, "oaitun_ue");
+      nas_config(1, "10.0.1.2", "oaitun_ue");
     netlink_init_mbms_tun("oaitun_uem", id + 1);
-    nas_config(1, 2, 2, "oaitun_uem");
+    nas_config(1, "10.0.2.2", "oaitun_uem");
     LOG_I(PDCP, "UE pdcp will use tun interface\n");
   } else if (ENB_NAS_USE_TUN) {
     netlink_init_tun("oaitun_enb", 1, 0);
-    nas_config(1, 1, 1, "oaitun_enb");
+    nas_config(1, "10.0.1.1", "oaitun_enb");
     if (pdcp_optmask & ENB_NAS_USE_TUN_W_MBMS_BIT) {
       netlink_init_mbms_tun("oaitun_enm", 1);
-      nas_config(1, 2, 1, "oaitun_enm");
+      nas_config(1, "10.0.2.1", "oaitun_enm");
       LOG_I(PDCP, "ENB pdcp will use mbms tun interface\n");
     }
     LOG_I(PDCP, "ENB pdcp will use tun interface\n");
   } else if (pdcp_optmask & ENB_NAS_USE_TUN_W_MBMS_BIT) {
     netlink_init_mbms_tun("oaitun_enm", 0);
-    nas_config(1, 2, 1, "oaitun_enm");
+    nas_config(1, "10.0.2.1", "oaitun_enm");
     LOG_I(PDCP, "ENB pdcp will use mbms tun interface\n");
   }
 
diff --git a/openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c b/openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c
index 51feb0d955f..5146cf3cee8 100644
--- a/openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c
+++ b/openair2/LAYER2/nr_pdcp/nr_pdcp_oai_api.c
@@ -610,8 +610,6 @@ uint64_t nr_pdcp_module_init(uint64_t _pdcp_optmask, int id)
 
   pdcp_optmask = pdcp_optmask | _pdcp_optmask ;
 
-  nas_getparams();
-
   if (UE_NAS_USE_TUN) {
     char *ifprefix = get_softmodem_params()->nsa ? "oaitun_nrue" : "oaitun_ue";
     int num_if = (NFAPI_MODE == NFAPI_UE_STUB_PNF || IS_SOFTMODEM_SIML1 || NFAPI_MODE == NFAPI_MODE_STANDALONE_PNF)
@@ -619,7 +617,8 @@ uint64_t nr_pdcp_module_init(uint64_t _pdcp_optmask, int id)
                      : 1;
     netlink_init_tun(ifprefix, num_if, id);
     if (IS_SOFTMODEM_NOS1) {
-      nas_config(1, 1, !get_softmodem_params()->nsa ? 2 : 3, ifprefix);
+      const char *ip = !get_softmodem_params()->nsa ? "10.0.1.2" : "10.0.1.3";
+      nas_config(1, ip, ifprefix);
       set_qfi_pduid(7, 10);
     }
     LOG_I(PDCP, "UE pdcp will use tun interface\n");
@@ -627,7 +626,7 @@ uint64_t nr_pdcp_module_init(uint64_t _pdcp_optmask, int id)
   } else if (ENB_NAS_USE_TUN) {
     char *ifprefix = get_softmodem_params()->nsa ? "oaitun_gnb" : "oaitun_enb";
     netlink_init_tun(ifprefix, 1, id);
-    nas_config(1, 1, 1, ifprefix);
+    nas_config(1, "10.0.1.1", ifprefix);
     LOG_I(PDCP, "ENB pdcp will use tun interface\n");
     start_pdcp_tun_enb();
   }
diff --git a/openair2/RRC/LTE/rrc_UE.c b/openair2/RRC/LTE/rrc_UE.c
index 9de1e525270..d125892d83b 100644
--- a/openair2/RRC/LTE/rrc_UE.c
+++ b/openair2/RRC/LTE/rrc_UE.c
@@ -765,10 +765,13 @@ rrc_ue_establish_drb(
     ip_addr_offset4 = 1;
     LOG_I(OIP,"[UE %d] trying to bring up the OAI interface %d, IP X.Y.%d.%d\n", ue_mod_idP, ip_addr_offset3+ue_mod_idP,
           ip_addr_offset3+ue_mod_idP+1,ip_addr_offset4+ue_mod_idP+1);
-    oip_ifup = nas_config(ip_addr_offset3 + ue_mod_idP + 1, // interface_id
-                          UE_NAS_USE_TUN ? 1 : (ip_addr_offset3 + ue_mod_idP + 1), // third_octet
-                          ip_addr_offset4 + ue_mod_idP + 1, // fourth_octet
-                          "oaitun_oip");
+    char ip[20];
+    snprintf(ip,
+             sizeof(ip),
+             "10.0.%d.%d",
+             UE_NAS_USE_TUN ? 1 : (ip_addr_offset3 + ue_mod_idP + 1),
+             ip_addr_offset4 + ue_mod_idP + 1);
+    oip_ifup = nas_config(ip_addr_offset3 + ue_mod_idP + 1, ip, "oaitun_oip");
 
     if (oip_ifup == 0 && (!UE_NAS_USE_TUN)) { // interface is up --> send a config the DRB
       LOG_I(OIP,"[UE %d] Config the ue net interface %d to send/receive pkt on DRB %ld to/from the protocol stack\n",
diff --git a/openair2/RRC/LTE/rrc_eNB.c b/openair2/RRC/LTE/rrc_eNB.c
index 3828df4cb74..cda6ac20130 100644
--- a/openair2/RRC/LTE/rrc_eNB.c
+++ b/openair2/RRC/LTE/rrc_eNB.c
@@ -5255,10 +5255,9 @@ rrc_eNB_process_RRCConnectionReconfigurationComplete(
             LOG_I(OIP, "[eNB %d] trying to bring up the OAI interface oai%d\n",
                   ctxt_pP->module_id,
                   ctxt_pP->module_id);
-            oip_ifup = nas_config(ctxt_pP->module_id, // interface index
-                                  ctxt_pP->module_id + 1, // third octet
-                                  ctxt_pP->module_id + 1, // fourth octet
-                                  "oaitun_oai");
+            char ip[20];
+            snprintf(ip, sizeof(ip), "10.0.%d.%d", ctxt_pP->module_id + 1, ctxt_pP->module_id + 1);
+            oip_ifup = nas_config(ctxt_pP->module_id, ip, "oaitun_oai");
 
             if (oip_ifup == 0) { // interface is up --> send a config the DRB
               module_id_t ue_module_id;
diff --git a/openair2/RRC/NAS/nas_config.c b/openair2/RRC/NAS/nas_config.c
index f9e349bccf1..4606ade6ba0 100644
--- a/openair2/RRC/NAS/nas_config.c
+++ b/openair2/RRC/NAS/nas_config.c
@@ -29,31 +29,6 @@
 
 #include "nas_config.h"
 #include "common/utils/LOG/log.h"
-#include "common/config/config_userapi.h"
-
-//default values according to the examples,
-
-char *baseNetAddress ;
-#define NASHLP_NETPREFIX "<NAS network prefix, two first bytes of network addresses>\n"
-void nas_getparams(void) {
-  // this datamodel require this static because we partially keep data like baseNetAddress (malloc on a global)
-  // but we loose the opther attributes in nasoptions between two calls if is is not static !
-  // clang-format off
-  paramdef_t nasoptions[] = {
-    /*--------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
-    /*                                            configuration parameters for netlink, includes network parameters when running in noS1 mode                             */
-    /*   optname                     helpstr                paramflags           XXXptr                               defXXXval               type                 numelt */
-    /*--------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
-    {"NetworkPrefix",    NASHLP_NETPREFIX,       0,              .strptr=&baseNetAddress,        .defstrval="10.0",            TYPE_STRING,  0 },
-  };
-  // clang-format on
-  config_get(config_get_if(), nasoptions, sizeofArray(nasoptions), "nas.noS1");
-}
-
-void setBaseNetAddress (char *baseAddr) {
-  strcpy(baseNetAddress,baseAddr);
-}
-
 
 /*
  * \brief set a genneric interface parameter
@@ -104,21 +79,19 @@ static bool change_interface_state(int sock_fd, const char *ifn, if_action_t if_
 }
 
 // non blocking full configuration of the interface (address, and the two lest octets of the address)
-int nas_config(int interface_id, int thirdOctet, int fourthOctet, const char *ifpref)
+bool nas_config(int interface_id, const char *ip, const char *ifpref)
 {
-  char ipAddress[20];
   char interfaceName[IFNAMSIZ];
-  sprintf(ipAddress, "%s.%d.%d", baseNetAddress,thirdOctet,fourthOctet);
   snprintf(interfaceName, sizeof(interfaceName), "%s%d", ifpref, interface_id);
 
   int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
   if (sock_fd < 0) {
     LOG_E(UTIL, "Failed creating socket for interface management: %d, %s\n", errno, strerror(errno));
-    return 1;
+    return false;
   }
 
   change_interface_state(sock_fd, interfaceName, INTERFACE_DOWN);
-  bool success = setInterfaceParameter(sock_fd, interfaceName, ipAddress, SIOCSIFADDR);
+  bool success = setInterfaceParameter(sock_fd, interfaceName, ip, SIOCSIFADDR);
   // set the machine network mask
   if (success)
     success = setInterfaceParameter(sock_fd, interfaceName, "255.255.255.0", SIOCSIFNETMASK);
@@ -127,9 +100,9 @@ int nas_config(int interface_id, int thirdOctet, int fourthOctet, const char *if
     success = change_interface_state(sock_fd, interfaceName, INTERFACE_UP);
 
   if (success)
-    LOG_I(OIP, "Interface %s successfully configured, ip address %s\n", interfaceName, ipAddress);
+    LOG_I(OIP, "Interface %s successfully configured, ip address %s\n", interfaceName, ip);
   else
-    LOG_E(OIP, "Interface %s couldn't be configured (ip address %s)\n", interfaceName, ipAddress);
+    LOG_E(OIP, "Interface %s couldn't be configured (ip address %s)\n", interfaceName, ip);
 
   close(sock_fd);
   return success;
diff --git a/openair2/RRC/NAS/nas_config.h b/openair2/RRC/NAS/nas_config.h
index fefb12f937c..265e7b57601 100644
--- a/openair2/RRC/NAS/nas_config.h
+++ b/openair2/RRC/NAS/nas_config.h
@@ -19,44 +19,25 @@
  *      contact@openairinterface.org
  */
 
-/*! \file nas_config.h
- * \brief Configures the nasmesh interface
- * \author Daniel Camara and navid nikaein
- * \date 2006-2011
- * \version 0.1
- * \company Eurecom
- */
 #ifndef NAS_CONFIG_H_
 #define NAS_CONFIG_H_
 
+#include <stdbool.h>
 #include <netinet/in.h>
 
-/*! \fn void void nas_getparams(void)(void)
- * \brief This function get parameters used to configure network interface when running in noS1 mode
- * \note
- * @ingroup  _nas
- */
-void nas_getparams(void);
-
 /*! \fn int  nas_config(char*, int, int)
  * \brief This function initializes the nasmesh interface using the basic values,
  * basic address, network mask and broadcast address, as the default configured
  * ones
- * \param[in] interfaceName, the name of the interface, e.g. nasmesh0 or nasmesh1
- * \param[in] third octet of the ip address e.g. for the 10.1.2.3 address would be 2
- * \param[in] fourth octet of the ip address e.g. for the 10.1.2.3 address would be 3
- * \return 0 on success, otherwise 1, if couldn't open a socket and 2 if the ioctl fails
- * \note
- * @ingroup  _nas
- */
-int nas_config(int interface_id, int thirdOctet, int fourthOctet, const char *ifprefix);
-
-/*! \fn void  setBaseNetAddress(char*)
- * \brief This function sets the basic network address used
- * \param[in] baseAddr, the new basic address e.g.for 10.0.1.2, would be 10.0
+ * \param[in] interface_id number of this interface, prepended after interface
+ * name
+ * \param[in] ip IPv4 address of this interface as a string
+ * \param[in] ifprefix interface name prefix to which an interface number will
+ * be appended
+ * \return true on success, otherwise false
  * \note
  * @ingroup  _nas
  */
-void setBaseNetAddress(char *baseAddr);
+bool nas_config(int interface_id, const char *ip, const char *ifprefix);
 
 #endif /*NAS_CONFIG_H_*/
diff --git a/openair3/NAS/COMMON/ESM/MSG/PduSessionEstablishmentAccept.c b/openair3/NAS/COMMON/ESM/MSG/PduSessionEstablishmentAccept.c
index 99ea466e2ee..b83dd100dd7 100644
--- a/openair3/NAS/COMMON/ESM/MSG/PduSessionEstablishmentAccept.c
+++ b/openair3/NAS/COMMON/ESM/MSG/PduSessionEstablishmentAccept.c
@@ -25,8 +25,6 @@
 #include "openair2/RRC/NAS/nas_config.h"
 #include "openair2/SDAP/nr_sdap/nr_sdap.h"
 
-extern char *baseNetAddress;
-
 static uint16_t getShort(uint8_t *input)
 {
   uint16_t tmp16;
@@ -105,19 +103,23 @@ void capture_pdu_session_establishment_accept_msg(uint8_t *buffer, uint32_t msg_
         psea_msg.pdu_addr_ie.pdu_length = *curPtr++;
         psea_msg.pdu_addr_ie.pdu_type = *curPtr++;
 
+        DevAssert(psea_msg.pdu_addr_ie.pdu_type == PDU_SESSION_TYPE_IPV4);
         if (psea_msg.pdu_addr_ie.pdu_type == PDU_SESSION_TYPE_IPV4) {
-          psea_msg.pdu_addr_ie.pdu_addr_oct1 = *curPtr++;
-          psea_msg.pdu_addr_ie.pdu_addr_oct2 = *curPtr++;
-          psea_msg.pdu_addr_ie.pdu_addr_oct3 = *curPtr++;
-          psea_msg.pdu_addr_ie.pdu_addr_oct4 = *curPtr++;
-          nas_getparams();
-          sprintf(baseNetAddress, "%d.%d", psea_msg.pdu_addr_ie.pdu_addr_oct1, psea_msg.pdu_addr_ie.pdu_addr_oct2);
-          nas_config(1, psea_msg.pdu_addr_ie.pdu_addr_oct3, psea_msg.pdu_addr_ie.pdu_addr_oct4, "oaitun_ue");
-          LOG_T(NAS, "PDU SESSION ESTABLISHMENT ACCEPT - Received UE IP: %d.%d.%d.%d\n",
-                psea_msg.pdu_addr_ie.pdu_addr_oct1,
-                psea_msg.pdu_addr_ie.pdu_addr_oct2,
-                psea_msg.pdu_addr_ie.pdu_addr_oct3,
-                psea_msg.pdu_addr_ie.pdu_addr_oct4);
+          pdu_address_t *addr = &psea_msg.pdu_addr_ie;
+          addr->pdu_addr_oct1 = *curPtr++;
+          addr->pdu_addr_oct2 = *curPtr++;
+          addr->pdu_addr_oct3 = *curPtr++;
+          addr->pdu_addr_oct4 = *curPtr++;
+          char ip[20];
+          snprintf(ip,
+                   sizeof(ip),
+                   "%d.%d.%d.%d",
+                   addr->pdu_addr_oct1,
+                   addr->pdu_addr_oct2,
+                   addr->pdu_addr_oct3,
+                   addr->pdu_addr_oct4);
+          nas_config(1, ip, "oaitun_ue");
+          LOG_T(NAS, "PDU SESSION ESTABLISHMENT ACCEPT - Received UE IP: %s\n", ip);
         } else {
           curPtr += psea_msg.pdu_addr_ie.pdu_length;
         }
diff --git a/openair3/NAS/NR_UE/nr_nas_msg_sim.c b/openair3/NAS/NR_UE/nr_nas_msg_sim.c
index 8f816844379..fd4088aab8b 100644
--- a/openair3/NAS/NR_UE/nr_nas_msg_sim.c
+++ b/openair3/NAS/NR_UE/nr_nas_msg_sim.c
@@ -53,7 +53,6 @@
 #include "openair3/SECU/nas_stream_eia2.h"
 #include "openair3/UTILS/conversions.h"
 
-extern char *baseNetAddress;
 extern uint16_t NB_UE_INST;
 static nr_ue_nas_t nr_ue_nas = {0};
 static nr_nas_msg_snssai_t nas_allowed_nssai[8];
@@ -842,11 +841,11 @@ static void generateRegistrationComplete(nr_ue_nas_t *nas, as_nas_info_t *initia
 void decodeDownlinkNASTransport(as_nas_info_t *initialNasMsg, uint8_t * pdu_buffer){
   uint8_t msg_type = *(pdu_buffer + 16);
   if(msg_type == FGS_PDU_SESSION_ESTABLISHMENT_ACC){
-    sprintf(baseNetAddress, "%d.%d", *(pdu_buffer + 39),*(pdu_buffer + 40));
-    int third_octet = *(pdu_buffer + 41);
-    int fourth_octet = *(pdu_buffer + 42);
+    uint8_t *ip_p = pdu_buffer + 39;
+    char ip[20];
+    sprintf(ip, "%d.%d.%d.%d", *(ip_p), *(ip_p + 1), *(ip_p + 2), *(ip_p + 3));
     LOG_A(NAS, "Received PDU Session Establishment Accept\n");
-    nas_config(1, third_octet, fourth_octet, "oaitun_ue");
+    nas_config(1, ip, "oaitun_ue");
   } else {
     LOG_E(NAS, "Received unexpected message in DLinformationTransfer %d\n", msg_type);
   }
@@ -1428,17 +1427,11 @@ void *nas_nrue(void *args_p)
             while (offset < payload_container_length) {
               if (*(payload_container + offset) == 0x29) { // PDU address IEI
                 if ((*(payload_container + offset + 1) == 0x05) && (*(payload_container + offset + 2) == 0x01)) { // IPV4
-                  nas_getparams();
-                  sprintf(baseNetAddress, "%d.%d", *(payload_container + offset + 3), *(payload_container + offset + 4));
-                  int third_octet = *(payload_container + offset + 5);
-                  int fourth_octet = *(payload_container + offset + 6);
-                  LOG_I(NAS,
-                        "Received PDU Session Establishment Accept, UE IP: %d.%d.%d.%d\n",
-                        *(payload_container + offset + 3),
-                        *(payload_container + offset + 4),
-                        *(payload_container + offset + 5),
-                        *(payload_container + offset + 6));
-                  nas_config(1, third_octet, fourth_octet, "oaitun_ue");
+                  uint8_t *ip_p = payload_container + offset + 3;
+                  char ip[20];
+                  snprintf(ip, sizeof(ip), "%d.%d.%d.%d", *(ip_p), *(ip_p + 1), *(ip_p + 2), *(ip_p + 3));
+                  LOG_I(NAS, "Received PDU Session Establishment Accept, UE IP: %s\n", ip);
+                  nas_config(1, ip, "oaitun_ue");
                   break;
                 }
               }
-- 
GitLab