From 5c4595e69954f5506ee9a01b3cea614da476a33c Mon Sep 17 00:00:00 2001 From: Guido Casati Date: Tue, 15 Apr 2025 18:34:47 +0200 Subject: [PATCH] Refactor 5GS registration result enc/dec functions * adopt byte_array * move enc/dec functions to Registration Accept library * simplify code * log result received in Registration Accept --- .../NAS/NR_UE/5GS/5GMM/IES/CMakeLists.txt | 1 - .../5GS/5GMM/IES/FGSRegistrationResult.c | 58 ------------------- .../5GS/5GMM/IES/FGSRegistrationResult.h | 55 ------------------ .../NR_UE/5GS/5GMM/MSG/RegistrationAccept.c | 38 ++++++++++-- .../NR_UE/5GS/5GMM/MSG/RegistrationAccept.h | 14 +++-- openair3/NAS/NR_UE/nr_nas_msg.c | 8 ++- 6 files changed, 50 insertions(+), 124 deletions(-) delete mode 100644 openair3/NAS/NR_UE/5GS/5GMM/IES/FGSRegistrationResult.c delete mode 100644 openair3/NAS/NR_UE/5GS/5GMM/IES/FGSRegistrationResult.h diff --git a/openair3/NAS/NR_UE/5GS/5GMM/IES/CMakeLists.txt b/openair3/NAS/NR_UE/5GS/5GMM/IES/CMakeLists.txt index e865dd0046..40bfcbabcd 100644 --- a/openair3/NAS/NR_UE/5GS/5GMM/IES/CMakeLists.txt +++ b/openair3/NAS/NR_UE/5GS/5GMM/IES/CMakeLists.txt @@ -1,7 +1,6 @@ add_library(fgs_5gmm_ies_lib OBJECT FGSMobileIdentity.c FGSRegistrationType.c - FGSRegistrationResult.c FGMMCapability.c NrUESecurityCapability.c FGCNasMessageContainer.c diff --git a/openair3/NAS/NR_UE/5GS/5GMM/IES/FGSRegistrationResult.c b/openair3/NAS/NR_UE/5GS/5GMM/IES/FGSRegistrationResult.c deleted file mode 100644 index 739cee7973..0000000000 --- a/openair3/NAS/NR_UE/5GS/5GMM/IES/FGSRegistrationResult.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The OpenAirInterface Software Alliance licenses this file to You under - * the OAI Public License, Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.openairinterface.org/?page_id=698 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *------------------------------------------------------------------------------- - * For more information about the OpenAirInterface (OAI) Software Alliance: - * contact@openairinterface.org - */ - -/*! \file FGSRegistrationResult.c - -\brief 5GS Registration result for registration request procedures -\author Yoshio INOUE, Masayuki HARADA -\email: yoshio.inoue@fujitsu.com,masayuki.harada@fujitsu.com -\date 2020 -\version 0.1 -*/ - -#include -#include -#include - -#include "TLVEncoder.h" -#include "TLVDecoder.h" -#include "FGSRegistrationResult.h" - -int decode_fgs_registration_result(FGSRegistrationResult *fgsregistrationresult, uint8_t iei, uint16_t value, uint32_t len) -{ - int decoded = 0; - uint16_t *buffer = &value; - fgsregistrationresult->registrationresult = *buffer & 0x7; - fgsregistrationresult->smsallowed = *buffer & 0x8; - decoded = decoded + 2; - return decoded; -} - -uint16_t encode_fgs_registration_result(const FGSRegistrationResult *fgsregistrationresult) -{ - uint16_t bufferReturn; - uint16_t *buffer = &bufferReturn; - uint8_t encoded = 0; - *(buffer + encoded) = 0x00 | (fgsregistrationresult->smsallowed & 0x8) | (fgsregistrationresult->registrationresult & 0x7); - encoded = encoded + 2; - - return bufferReturn; -} diff --git a/openair3/NAS/NR_UE/5GS/5GMM/IES/FGSRegistrationResult.h b/openair3/NAS/NR_UE/5GS/5GMM/IES/FGSRegistrationResult.h deleted file mode 100644 index 04f45417b8..0000000000 --- a/openair3/NAS/NR_UE/5GS/5GMM/IES/FGSRegistrationResult.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The OpenAirInterface Software Alliance licenses this file to You under - * the OAI Public License, Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.openairinterface.org/?page_id=698 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *------------------------------------------------------------------------------- - * For more information about the OpenAirInterface (OAI) Software Alliance: - * contact@openairinterface.org - */ - -/*! \file FGSRegistrationResult.h - -\brief 5GS Registration result for registration request procedures -\author Yoshio INOUE, Masayuki HARADA -\email: yoshio.inoue@fujitsu.com,masayuki.harada@fujitsu.com -\date 2020 -\version 0.1 -*/ - -#include -#include -#include - -#include "OctetString.h" - -#ifndef FGS_REGISTRATION_RESULT_H_ -#define FGS_REGISTRATION_RESULT_H_ -#define FGS_REGISTRATION_RESULT_3GPP 0b001 -#define FGS_REGISTRATION_RESULT_NON_3GPP 0b010 -#define FGS_REGISTRATION_RESULT_3GPP_AND_NON_3GPP 0b011 - -typedef struct { - uint8_t iei; - uint8_t resultlength; - uint8_t spare: 4; - uint8_t smsallowed: 1; - uint8_t registrationresult: 3; -} FGSRegistrationResult; - -uint16_t encode_fgs_registration_result(const FGSRegistrationResult *fgsregistrationresult); - -int decode_fgs_registration_result(FGSRegistrationResult *fgsregistrationresult, uint8_t iei, uint16_t value, uint32_t len); - -#endif /* FGS REGISTRATION RESULT_H_*/ diff --git a/openair3/NAS/NR_UE/5GS/5GMM/MSG/RegistrationAccept.c b/openair3/NAS/NR_UE/5GS/5GMM/MSG/RegistrationAccept.c index d92244a15f..dfcb1fce94 100644 --- a/openair3/NAS/NR_UE/5GS/5GMM/MSG/RegistrationAccept.c +++ b/openair3/NAS/NR_UE/5GS/5GMM/MSG/RegistrationAccept.c @@ -38,8 +38,36 @@ #include "RegistrationAccept.h" #include "assertions.h" #include "fgs_nas_utils.h" +#include "common/utils/utils.h" +#include "ds/byte_array.h" #define IEI_5G_GUTI 0x77 +#define FGS_REGISTRATION_RESULT_LEN 2 +#define REGISTRATION_ACCEPT_MIN_LEN FGS_REGISTRATION_RESULT_LEN // (5GS registration result) 2 octets + +/** @brief Decode 5GS registration result (9.11.3.6 3GPP TS 24.501) */ +static int decode_fgs_registration_result(registration_accept_msg *out, const byte_array_t buffer) +{ + if (buffer.len < FGS_REGISTRATION_RESULT_LEN) { + PRINT_NAS_ERROR("5GS registration result decoding failure: invalid buffer length\n"); + return -1; + } + out->result = buffer.buf[1] & 0x7; // skip length of contents octet + out->sms_allowed = buffer.buf[1] & 0x8; + return 2; // length 2 octets (no IEI) +} + +static int encode_fgs_registration_result(byte_array_t buffer, const registration_accept_msg *out) +{ + if (buffer.len < FGS_REGISTRATION_RESULT_LEN) { + PRINT_NAS_ERROR("5GS registration result encoding failure: invalid buffer length\n"); + return -1; + } + uint8_t *buf = buffer.buf; + buf[0] = 1; + buf[1] = 0x00 | (out->sms_allowed & 0x8) | (out->result & 0x7); + return 2; // encoded length 2 octets (no IEI) +} /** * @brief Allowed NSSAI from Registration Accept according to 3GPP TS 24.501 Table 8.2.7.1.1 @@ -105,8 +133,9 @@ int decode_registration_accept(registration_accept_msg *registration_accept, con int dec = 0; const uint8_t *end = buffer + len; - /* Decoding mandatory fields */ - if ((dec = decode_fgs_registration_result(®istration_accept->fgsregistrationresult, 0, *buffer, len)) < 0) + // 5GS registration result (Mandatory) + byte_array_t ba = {.buf = buffer, .len = len}; + if ((dec = decode_fgs_registration_result(registration_accept, ba)) < 0) return dec; buffer += dec; @@ -149,9 +178,8 @@ int encode_registration_accept(const registration_accept_msg *registration_accep int encoded = 0; LOG_FUNC_IN; - - *(buffer + encoded) = encode_fgs_registration_result(®istration_accept->fgsregistrationresult); - encoded = encoded + 2; + byte_array_t ba = {.buf = buffer, .len = len}; + encoded += encode_fgs_registration_result(ba, registration_accept); if (registration_accept->guti) { int mi_enc = encode_5gs_mobile_identity(registration_accept->guti, 0x77, buffer + encoded, len - encoded); diff --git a/openair3/NAS/NR_UE/5GS/5GMM/MSG/RegistrationAccept.h b/openair3/NAS/NR_UE/5GS/5GMM/MSG/RegistrationAccept.h index 6abf1e70ca..5f444e95f9 100644 --- a/openair3/NAS/NR_UE/5GS/5GMM/MSG/RegistrationAccept.h +++ b/openair3/NAS/NR_UE/5GS/5GMM/MSG/RegistrationAccept.h @@ -34,12 +34,17 @@ #include "SecurityHeaderType.h" #include "MessageType.h" -#include "FGSRegistrationResult.h" #include "FGSMobileIdentity.h" #ifndef REGISTRATION_ACCEPT_H_ #define REGISTRATION_ACCEPT_H_ +typedef enum { + FGS_REGISTRATION_RESULT_3GPP = 1, + FGS_REGISTRATION_RESULT_NON_3GPP = 2, + FGS_REGISTRATION_RESULT_3GPP_AND_NON_3GPP = 3 +} fgs_registration_result_t; + // 9.11.3.37 of 3GPP TS 24.501 typedef struct { int sst; @@ -56,9 +61,10 @@ typedef struct { */ typedef struct registration_accept_msg_tag { - /* Mandatory fields */ - FGSRegistrationResult fgsregistrationresult; - /* Optional fields */ + // 5GS registration result (Mandatory) + fgs_registration_result_t result; + bool sms_allowed; + // 5G-GUTI (Optional) FGSMobileIdentity *guti; // Allowed NSSAI (O) nr_nas_msg_snssai_t nas_allowed_nssai[8]; diff --git a/openair3/NAS/NR_UE/nr_nas_msg.c b/openair3/NAS/NR_UE/nr_nas_msg.c index 941e0d7cdc..08003bbcba 100644 --- a/openair3/NAS/NR_UE/nr_nas_msg.c +++ b/openair3/NAS/NR_UE/nr_nas_msg.c @@ -1438,7 +1438,6 @@ static void process_guti(Guti5GSMobileIdentity_t *guti, nr_ue_nas_t *nas) static void handle_registration_accept(nr_ue_nas_t *nas, const uint8_t *pdu_buffer, uint32_t msg_length) { - LOG_I(NAS, "[UE] Received REGISTRATION ACCEPT message\n"); registration_accept_msg msg = {0}; fgs_nas_message_security_header_t sp_header = {0}; const uint8_t *end = pdu_buffer + msg_length; @@ -1465,6 +1464,13 @@ static void handle_registration_accept(nr_ue_nas_t *nas, const uint8_t *pdu_buff LOG_E(NAS, "Failed to decode NAS Registration Accept\n"); return; } + LOG_I(NAS, + "Received Registration Accept with result %s\n", + msg.result == FGS_REGISTRATION_RESULT_3GPP ? "3GPP" + : msg.result == FGS_REGISTRATION_RESULT_NON_3GPP ? "non-3PP" + : "3GPP and non-3GPP"); + LOG_I(NAS, "SMS %s in 5GS Registration Result\n", msg.sms_allowed ? "allowed" : "not allowed"); + pdu_buffer += decoded; // process GUTI if (msg.guti) {