Cleanup and other fixes to decode_registration_accept

* adopt byte array
* add check on minimum buffer length
* improve comments
* remove call to LOG utility functions
* replace magic numbers with defines
* refined includes
This commit is contained in:
Guido Casati
2025-04-15 19:54:23 +02:00
parent 5c4595e699
commit 51661bd83c
3 changed files with 41 additions and 31 deletions
@@ -33,13 +33,11 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include "conversions.h" #include "conversions.h"
#include "TLVEncoder.h"
#include "TLVDecoder.h"
#include "RegistrationAccept.h" #include "RegistrationAccept.h"
#include "assertions.h"
#include "fgs_nas_utils.h" #include "fgs_nas_utils.h"
#include "common/utils/utils.h" #include "common/utils/utils.h"
#include "ds/byte_array.h" #include "ds/byte_array.h"
#include "fgmm_lib.h"
#define IEI_5G_GUTI 0x77 #define IEI_5G_GUTI 0x77
#define FGS_REGISTRATION_RESULT_LEN 2 #define FGS_REGISTRATION_RESULT_LEN 2
@@ -128,56 +126,65 @@ static int decode_nssai_ie(nr_nas_msg_snssai_t *nssai, const uint8_t *buf)
return decoded; return decoded;
} }
int decode_registration_accept(registration_accept_msg *registration_accept, const uint8_t *buffer, uint32_t len) size_t decode_registration_accept(registration_accept_msg *registration_accept, const byte_array_t buffer)
{ {
int dec = 0; int dec = 0;
const uint8_t *end = buffer + len; byte_array_t ba = buffer; // local copy of buffer
if (ba.len < REGISTRATION_ACCEPT_MIN_LEN) {
PRINT_NAS_ERROR("Registration Accept decoding failed: invalid buffer length\n");
return -1;
}
// 5GS registration result (Mandatory) // 5GS registration result (Mandatory)
byte_array_t ba = {.buf = buffer, .len = len};
if ((dec = decode_fgs_registration_result(registration_accept, ba)) < 0) if ((dec = decode_fgs_registration_result(registration_accept, ba)) < 0)
return dec; return -1;
buffer += dec; UPDATE_BYTE_ARRAY(ba, dec);
if (buffer < end && *buffer == IEI_5G_GUTI) { // 5G-GUTI (Optional)
if (ba.len > 0 && ba.buf[0] == IEI_5G_GUTI) {
registration_accept->guti = calloc_or_fail(1, sizeof(*registration_accept->guti)); registration_accept->guti = calloc_or_fail(1, sizeof(*registration_accept->guti));
if ((dec = decode_5gs_mobile_identity(registration_accept->guti, IEI_5G_GUTI, buffer, end - buffer)) < 0) { if ((dec = decode_5gs_mobile_identity(registration_accept->guti, IEI_5G_GUTI, ba.buf, ba.len)) < 0) {
PRINT_NAS_ERROR("Failed to decode 5GS Mobile Identity in Registration Accept\n"); PRINT_NAS_ERROR("Failed to decode 5GS Mobile Identity in Registration Accept\n");
return -1; return -1;
} }
buffer += dec; UPDATE_BYTE_ARRAY(ba, dec);
} }
// Allowed NSSAI (O) // Decode other Optional IEs
/* Optional Presence IEs */ while (ba.len > 0) {
while (buffer < end) { uint8_t iei = ba.buf[0];
const int iei = *buffer++; UPDATE_BYTE_ARRAY(ba, 1);
switch (iei) { switch (iei) {
case 0x15: // allowed NSSAI case 0x15: // allowed NSSAI
dec = decode_nssai_ie(registration_accept->nas_allowed_nssai, buffer); dec = decode_nssai_ie(registration_accept->nas_allowed_nssai, ba.buf);
buffer += dec; UPDATE_BYTE_ARRAY(ba, dec);
break; break;
case 0x31: // configured NSSAI case 0x31: // configured NSSAI
dec = decode_nssai_ie(registration_accept->config_nssai, buffer); dec = decode_nssai_ie(registration_accept->config_nssai, ba.buf);
buffer += dec; UPDATE_BYTE_ARRAY(ba, dec);
break; break;
default: default:
dec = *buffer++; // content length + 1 byte (Length IE) dec = ba.buf[0] + 1; // content length + 1 byte (Length IE)
buffer += dec; UPDATE_BYTE_ARRAY(ba, dec);
break; break;
} }
} }
return len; if(ba.len != 0) {
PRINT_NAS_ERROR("Failed to decode registration accept: ba.len = %ld\n", ba.len);
return -1;
}
return buffer.len;
} }
int encode_registration_accept(const registration_accept_msg *registration_accept, uint8_t *buffer, uint32_t len) int encode_registration_accept(const registration_accept_msg *registration_accept, uint8_t *buffer, uint32_t len)
{ {
int encoded = 0; int encoded = 0;
LOG_FUNC_IN;
byte_array_t ba = {.buf = buffer, .len = len}; byte_array_t ba = {.buf = buffer, .len = len};
encoded += encode_fgs_registration_result(ba, registration_accept); encoded += encode_fgs_registration_result(ba, registration_accept);
@@ -188,6 +195,5 @@ int encode_registration_accept(const registration_accept_msg *registration_accep
encoded += mi_enc; encoded += mi_enc;
} }
// todo ,Encoding optional fields return encoded;
LOG_FUNC_RETURN(encoded);
} }
@@ -35,6 +35,7 @@
#include "SecurityHeaderType.h" #include "SecurityHeaderType.h"
#include "MessageType.h" #include "MessageType.h"
#include "FGSMobileIdentity.h" #include "FGSMobileIdentity.h"
#include "ds/byte_array.h"
#ifndef REGISTRATION_ACCEPT_H_ #ifndef REGISTRATION_ACCEPT_H_
#define REGISTRATION_ACCEPT_H_ #define REGISTRATION_ACCEPT_H_
@@ -45,6 +46,8 @@ typedef enum {
FGS_REGISTRATION_RESULT_3GPP_AND_NON_3GPP = 3 FGS_REGISTRATION_RESULT_3GPP_AND_NON_3GPP = 3
} fgs_registration_result_t; } fgs_registration_result_t;
#define NAS_MAX_NUMBER_SLICES 8
// 9.11.3.37 of 3GPP TS 24.501 // 9.11.3.37 of 3GPP TS 24.501
typedef struct { typedef struct {
int sst; int sst;
@@ -66,13 +69,13 @@ typedef struct registration_accept_msg_tag {
bool sms_allowed; bool sms_allowed;
// 5G-GUTI (Optional) // 5G-GUTI (Optional)
FGSMobileIdentity *guti; FGSMobileIdentity *guti;
// Allowed NSSAI (O) // Allowed NSSAI (Optional)
nr_nas_msg_snssai_t nas_allowed_nssai[8]; nr_nas_msg_snssai_t nas_allowed_nssai[NAS_MAX_NUMBER_SLICES];
// Configured NSSAI (O) // Configured NSSAI (Optional)
nr_nas_msg_snssai_t config_nssai[8]; nr_nas_msg_snssai_t config_nssai[NAS_MAX_NUMBER_SLICES];
} registration_accept_msg; } registration_accept_msg;
int decode_registration_accept(registration_accept_msg *registrationaccept, const uint8_t *buffer, uint32_t len); size_t decode_registration_accept(registration_accept_msg *registrationaccept, const byte_array_t buffer);
int encode_registration_accept(const registration_accept_msg *registrationaccept, uint8_t *buffer, uint32_t len); int encode_registration_accept(const registration_accept_msg *registrationaccept, uint8_t *buffer, uint32_t len);
+2 -1
View File
@@ -1460,7 +1460,8 @@ static void handle_registration_accept(nr_ue_nas_t *nas, const uint8_t *pdu_buff
} }
pdu_buffer += decoded; pdu_buffer += decoded;
// plain payload // plain payload
if ((decoded = decode_registration_accept(&msg, pdu_buffer, end - pdu_buffer)) < 0) { const byte_array_t ba = {.buf = (uint8_t *)pdu_buffer, .len = end - pdu_buffer};
if ((decoded = decode_registration_accept(&msg, ba)) < 0) {
LOG_E(NAS, "Failed to decode NAS Registration Accept\n"); LOG_E(NAS, "Failed to decode NAS Registration Accept\n");
return; return;
} }