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
This commit is contained in:
Guido Casati
2025-04-15 19:54:19 +02:00
parent f3eb713084
commit 5c4595e699
6 changed files with 50 additions and 124 deletions
@@ -1,7 +1,6 @@
add_library(fgs_5gmm_ies_lib OBJECT
FGSMobileIdentity.c
FGSRegistrationType.c
FGSRegistrationResult.c
FGMMCapability.c
NrUESecurityCapability.c
FGCNasMessageContainer.c
@@ -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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#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;
}
@@ -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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#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_*/
@@ -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(&registration_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(&registration_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);
@@ -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];
+7 -1
View File
@@ -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) {