Commit 2fad0001 authored by Jaroslava Fiedlerova's avatar Jaroslava Fiedlerova

Merge remote-tracking branch 'origin/nr-ue-nas-service-request' into integration_2024_w49 (!3065)

NAS Service Request enc/dec implementation

NAS Service Request:

- encoding
- decoding
- unit test

Related to #852
parents c942fe43 44c9dec6
......@@ -1569,6 +1569,10 @@ set(libnas_emm_msg_OBJS
${NAS_SRC}COMMON/EMM/MSG/UplinkNasTransport.c
)
set(libnas_fgs_msg_OBJS
${NAS_SRC}COMMON/EMM/MSG/fgs_service_request.c
)
set(libnas_esm_msg_OBJS
${NAS_SRC}COMMON/ESM/MSG/ActivateDedicatedEpsBearerContextAccept.c
${NAS_SRC}COMMON/ESM/MSG/ActivateDedicatedEpsBearerContextReject.c
......@@ -1682,7 +1686,6 @@ set (libnas_utils_OBJS
${NAS_SRC}COMMON/UTIL/nas_timer.c
${NAS_SRC}COMMON/UTIL/socket.c
${NAS_SRC}COMMON/UTIL/stty.c
${NAS_SRC}COMMON/UTIL/TLVEncoder.c
${NAS_SRC}COMMON/UTIL/TLVDecoder.c
${NAS_SRC}COMMON/UTIL/OctetString.c
)
......@@ -1793,6 +1796,7 @@ add_library(LIB_NAS_SIMUE
${libnas_api_OBJS}
${libnas_ue_api_OBJS}
${libnas_emm_msg_OBJS}
${libnas_fgs_msg_OBJS}
${libnas_esm_msg_OBJS}
${libnas_ies_OBJS}
${libnas_utils_OBJS}
......@@ -1839,6 +1843,7 @@ include_directories(${NAS_SRC}UE)
include_directories(${NAS_SRC}UE/API/USER)
include_directories(${NAS_SRC}UE/API/USIM)
include_directories(${NAS_SRC}UE/EMM)
include_directories(${NAS_SRC}UE/EMM/MSG)
include_directories(${NAS_SRC}UE/EMM/SAP)
include_directories(${NAS_SRC}UE/ESM)
include_directories(${NAS_SRC}UE/ESM/SAP)
......@@ -1905,12 +1910,6 @@ add_executable(measurement_display
${OPENAIR_DIR}/common/utils/threadPool/measurement_display.c)
target_link_libraries (measurement_display minimal_lib)
add_executable(test5Gnas
${OPENAIR_DIR}/openair3/TEST/test5Gnas.c
)
target_link_libraries (test5Gnas LIB_5GNAS_GNB minimal_lib )
# lte-softmodem is both eNB and UE implementation
###################################################
......
......@@ -145,7 +145,6 @@ set(util_SRC
${OPENAIR_NAS_DIR}/COMMON/UTIL/OctetString.c
${OPENAIR_NAS_DIR}/COMMON/UTIL/parser.c
${OPENAIR_NAS_DIR}/COMMON/UTIL/TLVDecoder.c
${OPENAIR_NAS_DIR}/COMMON/UTIL/TLVEncoder.c
${OPENAIR_NAS_DIR}/COMMON/UTIL/socket.c
${OPENAIR_NAS_DIR}/COMMON/UTIL/stty.c
)
......
......@@ -31,7 +31,6 @@ set(CONF2UEDATA_LIB_SRC
${OPENAIR_DIR}/openair3/NAS/UE/API/USIM/aka_functions.c
${OPENAIR_DIR}/common/utils/mem/memory.c
${OPENAIR_DIR}/openair3/NAS/COMMON/UTIL/OctetString.c
${OPENAIR_DIR}/openair3/NAS/COMMON/UTIL/TLVEncoder.c
${OPENAIR_DIR}/common/utils/utils.c
)
......
......@@ -1332,7 +1332,6 @@ INPUT = \
@CMAKE_CURRENT_SOURCE_DIR@/../openair3/NAS/COMMON/UTIL/parser.h \
@CMAKE_CURRENT_SOURCE_DIR@/../openair3/NAS/COMMON/UTIL/device.h \
@CMAKE_CURRENT_SOURCE_DIR@/../openair3/NAS/COMMON/UTIL/device.c \
@CMAKE_CURRENT_SOURCE_DIR@/../openair3/NAS/COMMON/UTIL/TLVEncoder.c \
@CMAKE_CURRENT_SOURCE_DIR@/../openair3/NAS/COMMON/UTIL/OctetString.h \
@CMAKE_CURRENT_SOURCE_DIR@/../openair3/NAS/COMMON/UTIL/nas_timer.c \
@CMAKE_CURRENT_SOURCE_DIR@/../openair3/NAS/COMMON/UTIL/TLVDecoder.c \
......
......@@ -3,3 +3,4 @@ add_subdirectory(M3AP)
add_subdirectory(S1AP)
add_subdirectory(NGAP)
add_subdirectory(NRPPA)
add_subdirectory(NAS)
add_subdirectory(COMMON)
add_subdirectory(IES)
add_subdirectory(EMM)
add_subdirectory(MSG)
if(ENABLE_TESTS)
add_subdirectory(tests)
endif()
......@@ -47,7 +47,7 @@ int decode_attach_request(attach_request_msg *attach_request, uint8_t *buffer, u
LOG_FUNC_RETURN(decoded_result);
}
if ((decoded_result = decode_u8_nas_key_set_identifier(&attach_request->naskeysetidentifier, 0, *(buffer + decoded) >> 4, len - decoded)) < 0) {
if ((decoded_result = decode_nas_key_set_identifier(&attach_request->naskeysetidentifier, 0, *(buffer + decoded) >> 4)) < 0) {
// return decoded_result;
LOG_FUNC_RETURN(decoded_result);
}
......@@ -299,7 +299,8 @@ int encode_attach_request(attach_request_msg *attach_request, uint8_t *buffer, u
/* Checking IEI and pointer */
CHECK_PDU_POINTER_AND_LENGTH_ENCODER(buffer, ATTACH_REQUEST_MINIMUM_LENGTH, len);
*(buffer + encoded) = ((encode_u8_nas_key_set_identifier(&attach_request->naskeysetidentifier) & 0x0f) << 4) | (encode_u8_eps_attach_type(&attach_request->epsattachtype) & 0x0f);
*(buffer + encoded) = ((encode_nas_key_set_identifier(&attach_request->naskeysetidentifier, 0) & 0x0f) << 4)
| (encode_u8_eps_attach_type(&attach_request->epsattachtype) & 0x0f);
encoded++;
if ((encode_result =
......
......@@ -38,7 +38,8 @@ int decode_authentication_request(authentication_request_msg *authentication_req
CHECK_PDU_POINTER_AND_LENGTH_DECODER(buffer, AUTHENTICATION_REQUEST_MINIMUM_LENGTH, len);
/* Decoding mandatory fields */
if ((decoded_result = decode_u8_nas_key_set_identifier(&authentication_request->naskeysetidentifierasme, 0, *(buffer + decoded), len - decoded)) < 0)
if ((decoded_result = decode_nas_key_set_identifier(&authentication_request->naskeysetidentifierasme, 0, *(buffer + decoded)))
< 0)
return decoded_result;
decoded++;
......@@ -64,7 +65,7 @@ int encode_authentication_request(authentication_request_msg *authentication_req
/* Checking IEI and pointer */
CHECK_PDU_POINTER_AND_LENGTH_ENCODER(buffer, AUTHENTICATION_REQUEST_MINIMUM_LENGTH, len);
*(buffer + encoded) = ((encode_u8_nas_key_set_identifier(&authentication_request->naskeysetidentifierasme) & 0x0f) << 4) | 0x00;
*(buffer + encoded) = ((encode_nas_key_set_identifier(&authentication_request->naskeysetidentifierasme, 0) & 0x0f) << 4) | 0x00;
encoded++;
if ((encode_result =
......
add_library(fgs_lib OBJECT
fgs_service_request.c
)
target_link_libraries(fgs_lib PUBLIC fgs_ies_lib)
target_include_directories(fgs_lib PUBLIC
${CMAKE_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
......@@ -41,7 +41,7 @@ int decode_detach_request(detach_request_msg *detach_request, uint8_t *buffer, u
if ((decoded_result = decode_u8_detach_type(&detach_request->detachtype, 0, *(buffer + decoded) & 0x0f, len - decoded)) < 0)
return decoded_result;
if ((decoded_result = decode_u8_nas_key_set_identifier(&detach_request->naskeysetidentifier, 0, *(buffer + decoded) >> 4, len - decoded)) < 0)
if ((decoded_result = decode_nas_key_set_identifier(&detach_request->naskeysetidentifier, 0, *(buffer + decoded) >> 4)) < 0)
return decoded_result;
decoded++;
......@@ -62,7 +62,8 @@ int encode_detach_request(detach_request_msg *detach_request, uint8_t *buffer, u
/* Checking IEI and pointer */
CHECK_PDU_POINTER_AND_LENGTH_ENCODER(buffer, DETACH_REQUEST_MINIMUM_LENGTH, len);
*(buffer + encoded) = ((encode_u8_nas_key_set_identifier(&detach_request->naskeysetidentifier) << 4) | (encode_u8_detach_type(&detach_request->detachtype) & 0x0f));
*(buffer + encoded) = ((encode_nas_key_set_identifier(&detach_request->naskeysetidentifier, 0) << 4)
| (encode_u8_detach_type(&detach_request->detachtype) & 0x0f));
encoded++;
if ((encode_result = encode_eps_mobile_identity(&detach_request->gutiorimsi,
......
......@@ -41,7 +41,9 @@ int decode_extended_service_request(extended_service_request_msg *extended_servi
if ((decoded_result = decode_u8_service_type(&extended_service_request->servicetype, 0, *(buffer + decoded) >> 4, len - decoded)) < 0)
return decoded_result;
if ((decoded_result = decode_u8_nas_key_set_identifier(&extended_service_request->naskeysetidentifier, 0, *(buffer + decoded) & 0x0f, len - decoded)) < 0)
if ((decoded_result =
decode_nas_key_set_identifier(&extended_service_request->naskeysetidentifier, 0, *(buffer + decoded) & 0x0f))
< 0)
return decoded_result;
decoded++;
......@@ -62,7 +64,8 @@ int encode_extended_service_request(extended_service_request_msg *extended_servi
/* Checking IEI and pointer */
CHECK_PDU_POINTER_AND_LENGTH_ENCODER(buffer, EXTENDED_SERVICE_REQUEST_MINIMUM_LENGTH, len);
*(buffer + encoded) = ((encode_u8_service_type(&extended_service_request->servicetype) & 0x0f) << 4) | (encode_u8_nas_key_set_identifier(&extended_service_request->naskeysetidentifier) & 0x0f);
*(buffer + encoded) = ((encode_u8_service_type(&extended_service_request->servicetype) & 0x0f) << 4)
| (encode_nas_key_set_identifier(&extended_service_request->naskeysetidentifier, 0) & 0x0f);
encoded++;
if ((encode_result =
......
......@@ -33,6 +33,7 @@
#include <stdint.h>
#include "FGSDeregistrationRequestUEOriginating.h"
#include "NR_NAS_defs.h"
int encode_fgs_deregistration_request_ue_originating(fgs_deregistration_request_ue_originating_msg *drr,
uint8_t *buffer,
......@@ -45,7 +46,7 @@ int encode_fgs_deregistration_request_ue_originating(fgs_deregistration_request_
| ((dt->access_type & 0x3) << 4);
int encode_result;
if ((encode_result = encode_nas_key_set_identifier(&drr->naskeysetidentifier, 0, buffer + encoded, len - encoded)) < 0)
if ((encode_result = encode_nas_key_set_identifier(&drr->naskeysetidentifier, IEI_NULL)) < 0)
return encode_result;
encoded++;
......
......@@ -35,6 +35,7 @@
#include "nas_log.h"
#include "RegistrationRequest.h"
#include "NR_NAS_defs.h"
int decode_registration_request(registration_request_msg *registration_request, uint8_t *buffer, uint32_t len)
{
......@@ -51,7 +52,8 @@ int decode_registration_request(registration_request_msg *registration_request,
LOG_FUNC_RETURN(decoded_result);
}
if ((decoded_result = decode_u8_nas_key_set_identifier(&registration_request->naskeysetidentifier, 0, *(buffer + decoded) >> 4, len - decoded)) < 0) {
if ((decoded_result = decode_nas_key_set_identifier(&registration_request->naskeysetidentifier, IEI_NULL, *(buffer + decoded) >> 4))
< 0) {
// return decoded_result;
LOG_FUNC_RETURN(decoded_result);
}
......@@ -75,7 +77,8 @@ int encode_registration_request(registration_request_msg *registration_request,
int encoded = 0;
int encode_result = 0;
*(buffer + encoded) = ((encode_u8_nas_key_set_identifier(&registration_request->naskeysetidentifier) & 0x0f) << 4) | (encode_5gs_registration_type(&registration_request->fgsregistrationtype) & 0x0f);
*(buffer + encoded) = ((encode_nas_key_set_identifier(&registration_request->naskeysetidentifier, IEI_NULL) & 0x0f) << 4)
| (encode_5gs_registration_type(&registration_request->fgsregistrationtype) & 0x0f);
encoded++;
if ((encode_result =
......
......@@ -43,7 +43,8 @@ int decode_security_mode_command(security_mode_command_msg *security_mode_comman
else
decoded += decoded_result;
if ((decoded_result = decode_u8_nas_key_set_identifier(&security_mode_command->naskeysetidentifier, 0, *(buffer + decoded) & 0x0f, len - decoded)) < 0)
if ((decoded_result = decode_nas_key_set_identifier(&security_mode_command->naskeysetidentifier, 0, *(buffer + decoded) & 0x0f))
< 0)
return decoded_result;
decoded++;
......@@ -124,7 +125,7 @@ int encode_security_mode_command(security_mode_command_msg *security_mode_comman
else
encoded += encode_result;
*(buffer + encoded) = (encode_u8_nas_key_set_identifier(&security_mode_command->naskeysetidentifier) & 0x0f);
*(buffer + encoded) = (encode_nas_key_set_identifier(&security_mode_command->naskeysetidentifier, 0) & 0x0f);
encoded++;
if ((encode_result =
......
......@@ -41,7 +41,9 @@ int decode_tracking_area_update_request(tracking_area_update_request_msg *tracki
if ((decoded_result = decode_u8_eps_update_type(&tracking_area_update_request->epsupdatetype, 0, *(buffer + decoded) >> 4, len - decoded)) < 0)
return decoded_result;
if ((decoded_result = decode_u8_nas_key_set_identifier(&tracking_area_update_request->naskeysetidentifier, 0, *(buffer + decoded) & 0x0f, len - decoded)) < 0)
if ((decoded_result =
decode_nas_key_set_identifier(&tracking_area_update_request->naskeysetidentifier, 0, *(buffer + decoded) & 0x0f))
< 0)
return decoded_result;
decoded++;
......@@ -61,10 +63,10 @@ int decode_tracking_area_update_request(tracking_area_update_request_msg *tracki
switch(ieiDecoded) {
case TRACKING_AREA_UPDATE_REQUEST_NONCURRENT_NATIVE_NAS_KEY_SET_IDENTIFIER_IEI:
if ((decoded_result =
decode_nas_key_set_identifier(&tracking_area_update_request->noncurrentnativenaskeysetidentifier,
TRACKING_AREA_UPDATE_REQUEST_NONCURRENT_NATIVE_NAS_KEY_SET_IDENTIFIER_IEI,
buffer + decoded, len - decoded)) <= 0)
if ((decoded_result = decode_nas_key_set_identifier(&tracking_area_update_request->noncurrentnativenaskeysetidentifier,
TRACKING_AREA_UPDATE_REQUEST_NONCURRENT_NATIVE_NAS_KEY_SET_IDENTIFIER_IEI,
*(buffer + decoded)))
<= 0)
return decoded_result;
decoded += decoded_result;
......@@ -293,8 +295,8 @@ int encode_tracking_area_update_request(tracking_area_update_request_msg *tracki
/* Checking IEI and pointer */
CHECK_PDU_POINTER_AND_LENGTH_ENCODER(buffer, TRACKING_AREA_UPDATE_REQUEST_MINIMUM_LENGTH, len);
*(buffer + encoded) = ((encode_u8_eps_update_type(&tracking_area_update_request->epsupdatetype) & 0x0f) << 4) | (encode_u8_nas_key_set_identifier(
&tracking_area_update_request->naskeysetidentifier) & 0x0f);
*(buffer + encoded) = ((encode_u8_eps_update_type(&tracking_area_update_request->epsupdatetype) & 0x0f) << 4)
| (encode_nas_key_set_identifier(&tracking_area_update_request->naskeysetidentifier, 0) & 0x0f);
encoded++;
if ((encode_result =
......@@ -306,10 +308,9 @@ int encode_tracking_area_update_request(tracking_area_update_request_msg *tracki
if ((tracking_area_update_request->presencemask & TRACKING_AREA_UPDATE_REQUEST_NONCURRENT_NATIVE_NAS_KEY_SET_IDENTIFIER_PRESENT)
== TRACKING_AREA_UPDATE_REQUEST_NONCURRENT_NATIVE_NAS_KEY_SET_IDENTIFIER_PRESENT) {
if ((encode_result =
encode_nas_key_set_identifier(&tracking_area_update_request->noncurrentnativenaskeysetidentifier,
TRACKING_AREA_UPDATE_REQUEST_NONCURRENT_NATIVE_NAS_KEY_SET_IDENTIFIER_IEI,
buffer + encoded, len - encoded)) < 0)
if ((encode_result = encode_nas_key_set_identifier(&tracking_area_update_request->noncurrentnativenaskeysetidentifier,
TRACKING_AREA_UPDATE_REQUEST_NONCURRENT_NATIVE_NAS_KEY_SET_IDENTIFIER_IEI))
< 0)
// Return in case of error
return encode_result;
else
......
/*
* 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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "TLVEncoder.h"
#include "TLVDecoder.h"
#include "fgs_service_request.h"
#include "FGSMobileIdentity.h"
#include "NasKeySetIdentifier.h"
#include "ServiceType.h"
#include "NR_NAS_defs.h"
#define LEN_FGS_MOBILE_ID_CONTENTS_SIZE 2 // octets
#define MIN_LEN_FGS_SERVICE_REQUEST 10 // octets
/**
* @brief Encode 5GMM NAS Service Request message (8.2.16 of 3GPP TS 24.501)
*/
int encode_fgs_service_request(uint8_t *buffer, fgs_service_request_msg_t *service_request, uint32_t len)
{
// Return if buffer is shorter than min length
if (len < MIN_LEN_FGS_SERVICE_REQUEST)
return -1;
int encoded = 0;
// ngKSI + Service type (1 octet) (M)
*buffer = ((encode_nas_key_set_identifier(&service_request->naskeysetidentifier, IEI_NULL) & 0x0f) << 4)
| (service_request->serviceType & 0x0f);
encoded++;
len -= encoded;
// 5GS Mobile Identity (M) type 5G-S-TMSI (9 octets)
encoded += LEN_FGS_MOBILE_ID_CONTENTS_SIZE; // skip "Length of 5GS mobile identity contents" IE
len -= encoded;
encoded += encode_stmsi_5gs_mobile_identity(buffer + encoded, &service_request->fiveg_s_tmsi, len);
// encode length of 5GS mobile identity contents
uint16_t tmp = htons(encoded - LEN_FGS_MOBILE_ID_CONTENTS_SIZE - 1);
memcpy(buffer + 1, &tmp, sizeof(tmp));
return encoded;
}
/**
* @brief Decode 5GMM NAS Service Request message (8.2.16 of 3GPP TS 24.501)
*/
int decode_fgs_service_request(fgs_service_request_msg_t *sr, const uint8_t *buffer, uint32_t len)
{
uint32_t decoded = 0;
int decoded_rc = 0;
// Service type (1/2 octet) (M)
sr->serviceType = *buffer & 0x0f;
// KSI (1/2 octet) (M)
if ((decoded_rc = decode_nas_key_set_identifier(&sr->naskeysetidentifier, IEI_NULL, *buffer >> 4)) < 0) {
return decoded_rc;
}
decoded++;
// 5GS Mobile Identity (M) type 5G-S-TMSI (9 octets) (M)
decoded += LEN_FGS_MOBILE_ID_CONTENTS_SIZE; // skip "Length of 5GS mobile identity contents" IE
if ((decoded_rc = decode_stmsi_5gs_mobile_identity(&sr->fiveg_s_tmsi, buffer + decoded, len - decoded)) < 0) {
return decoded_rc;
}
decoded += decoded_rc;
return decoded;
}
......@@ -23,25 +23,40 @@
#include <stdlib.h>
#include <stdint.h>
#include "TLVEncoder.h"
#include "ProtocolDiscriminator.h"
#include "SecurityHeaderType.h"
#include "NasKeySetIdentifier.h"
#include "ServiceType.h"
#include "ExtendedProtocolDiscriminator.h"
#include "FGCNasMessageContainer.h"
//#include "PDUSessionStatus.h"
#include "MessageType.h"
#include "SpareHalfOctet.h"
#include "FGSMobileIdentity.h"
int errorCodeEncoder = 0;
#ifndef FGS_SERVICE_REQUEST_H_
#define FGS_SERVICE_REQUEST_H_
const char *const errorCodeStringEncoder[] = {
"No error",
"Buffer NULL",
"Buffer too short",
"Octet string too long for IEI",
"Wrong message type",
"Protocol not supported",
};
/**
* Message name: Service request (8.2.16 of 3GPP TS 24.501)
* Description: The SERVICE REQUEST message is sent by the UE to the AMF
* in order to request the establishment of an N1 NAS signalling
* connection and/or to request the establishment of user-plane
* resources for PDU sessions which are established without
* user-plane resources
* Direction: UE to network
*/
void tlv_encode_perror(void)
{
if (errorCodeEncoder >= 0)
// No error or TLV_DECODE_ERR_OK
return;
typedef struct {
/* Mandatory fields */
NasKeySetIdentifier naskeysetidentifier;
ServiceType serviceType: 4;
Stmsi5GSMobileIdentity_t fiveg_s_tmsi;
/* Optional fields */
FGCNasMessageContainer *fgsnasmessagecontainer;
} fgs_service_request_msg_t;
printf("error: (%d, %s)\n", errorCodeEncoder, errorCodeStringEncoder[errorCodeEncoder * -1]);
}
int encode_fgs_service_request(uint8_t *buffer, fgs_service_request_msg_t *servicerequest, uint32_t len);
int decode_fgs_service_request(fgs_service_request_msg_t *sr, const uint8_t *buffer, uint32_t len);
#endif /* ! defined(FGS_SERVICE_REQUEST_H_) */
message(STATUS "Add test nas_lib_test")
add_executable(nas_lib_test nas_lib_test.c)
add_dependencies(tests nas_lib_test)
add_test(NAME nas_lib_test COMMAND nas_lib_test)
target_link_libraries(nas_lib_test PRIVATE fgs_lib fgs_ies_lib)
target_include_directories(nas_lib_test PRIVATE
${CMAKE_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <arpa/inet.h>
#include "fgs_service_request.h"
#include "nr_nas_msg.h"
void exit_function(const char *file, const char *function, const int line, const char *s, const int assert)
{
printf("detected error at %s:%d:%s: %s\n", file, line, function, s);
abort();
}
/**
* @brief Equality check for NAS Service Request enc/dec
*/
static bool eq_service_request(const fgs_service_request_msg_t *a, const fgs_service_request_msg_t *b)
{
bool result = true;
result &= memcmp(&a->naskeysetidentifier, &b->naskeysetidentifier, sizeof(NasKeySetIdentifier)) == 0;
result &= a->serviceType == b->serviceType;
result &= memcmp(&a->fiveg_s_tmsi, &b->fiveg_s_tmsi, sizeof(Stmsi5GSMobileIdentity_t)) == 0;
return result;
}
/**
* @brief Test NAS Service Request enc/dec
*/
static void test_service_request(void)
{
// Dummy NAS Service Request message
uint16_t amf_set_id = 0x48;
uint16_t amf_pointer = 0x34;
uint32_t tmsi = 0x56789ABC;
fgs_service_request_msg_t original_msg = {
.naskeysetidentifier = {.tsc = NAS_KEY_SET_IDENTIFIER_NATIVE, .naskeysetidentifier = NAS_KEY_SET_IDENTIFIER_NOT_AVAILABLE},
.serviceType = SERVICE_TYPE_DATA,
.fiveg_s_tmsi = {.spare = 0,
.typeofidentity = FGS_MOBILE_IDENTITY_5GS_TMSI,
.amfsetid = amf_set_id,
.amfpointer = amf_pointer,
.tmsi = tmsi}};
uint8_t expected_encoded_data[] = {0x71,
0x00,
0x00,
0xF4,
(amf_set_id >> 2) & 0xFF,
(amf_set_id << 6) | (amf_pointer & 0x3F),
(tmsi >> 24) & 0xFF,
(tmsi >> 16) & 0xFF,
(tmsi >> 8) & 0xFF,
tmsi & 0xFF};
uint16_t tmp = htons(7); // length of 5GS mobile identity IE for 5G-S-TMSI (bytes)
memcpy(expected_encoded_data + 1, &tmp, sizeof(tmp));
// Buffer
uint8_t buffer[64];
memset(buffer, 0, sizeof(buffer));
// Encode NAS Service Request
int encoded_length = encode_fgs_service_request(buffer, &original_msg, sizeof(buffer));
AssertFatal(encoded_length >= 0, "encode_fgs_service_request() failed\n");
// Compare the raw encoded buffer with expected encoded data
AssertFatal(memcmp(buffer, expected_encoded_data, encoded_length) == 0, "Encoding mismatch!\n");
// Decode NAS Service Request
fgs_service_request_msg_t decoded_service_request = {0};
int decoded_length = decode_fgs_service_request(&decoded_service_request, buffer, sizeof(buffer));
AssertFatal(decoded_length >= 0, "decode_fgs_service_request() failed\n");
// Compare original and decoded messages
AssertFatal(eq_service_request(&original_msg, &decoded_service_request) == 0,
"test_service_request() failed: original and decoded messages do not match\n");
}
int main()
{
test_service_request();
return 0;
}
add_library(fgs_ies_lib OBJECT
NasKeySetIdentifier.c
FGSMobileIdentity.c
)
target_include_directories(fgs_ies_lib PUBLIC
${CMAKE_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
......@@ -31,18 +31,71 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "TLVEncoder.h"
#include "TLVDecoder.h"
#include "FGSMobileIdentity.h"
#define MIN_LEN_FGS_MOBILE_ID_IE 3 // type of identity "No identity"
#define LEN_5G_FGS_TMSI_IE 9
static int decode_guti_5gs_mobile_identity(Guti5GSMobileIdentity_t *guti, const uint8_t *buffer);
static int encode_guti_5gs_mobile_identity(Guti5GSMobileIdentity_t *guti, uint8_t *buffer);
static int encode_suci_5gs_mobile_identity(Suci5GSMobileIdentity_t *suci, uint8_t *buffer);
static int encode_imeisv_5gs_mobile_identity(Imeisv5GSMobileIdentity_t *imeisv, uint8_t *buffer);
int encode_stmsi_5gs_mobile_identity(uint8_t *buffer, Stmsi5GSMobileIdentity_t *stmsi, uint32_t len)
{
// Return if buffer is shorter than min length
if (len < LEN_5G_FGS_TMSI_IE)
return -1;
uint32_t encoded = 0;
// octet 4
*(buffer + encoded) = (DIGIT1 << 4) | ((stmsi->spare & 0x1) << 3) | (stmsi->typeofidentity & 0x7);
encoded++;
// octet 5-6
uint16_t temp = 0x00 | ((stmsi->amfsetid) << 6) | (stmsi->amfpointer & 0x3f);
IES_ENCODE_U16(buffer, encoded, temp);
// octet 7
IES_ENCODE_U32(buffer, encoded, stmsi->tmsi);
return encoded;
}
int decode_stmsi_5gs_mobile_identity(Stmsi5GSMobileIdentity_t *stmsi, const uint8_t *buffer, uint32_t len)
{
int decoded = 0;
uint16_t temp;
// Return if buffer is shorter than min length
if (len < LEN_5G_FGS_TMSI_IE)
return -1;
// Octet 4: spare (bit 3) and type of identity (bits 0-2)
stmsi->digit1 = DIGIT1;
stmsi->spare = (*(buffer + decoded) >> 3) & 0x1;
stmsi->typeofidentity = *(buffer + decoded) & 0x7;
// Validate type of identity
if (stmsi->typeofidentity != FGS_MOBILE_IDENTITY_5GS_TMSI) {
return -1;
}
decoded++;
// Octet 5-6: AMF Set ID (bits 6-15) and AMF Pointer (bits 0-5)
IES_DECODE_U16(buffer, decoded, temp);
stmsi->amfsetid = (temp >> 6) & 0x03ff;
stmsi->amfpointer = temp & 0x3f;
// Octet 7-10: 32-bit TMSI
IES_DECODE_U32(buffer, decoded, stmsi->tmsi);
return decoded;
}
int decode_5gs_mobile_identity(FGSMobileIdentity *fgsmobileidentity, uint8_t iei, const uint8_t *buffer, uint32_t len)
{
int decoded_rc = TLV_DECODE_VALUE_DOESNT_MATCH;
......@@ -77,6 +130,10 @@ int decode_5gs_mobile_identity(FGSMobileIdentity *fgsmobileidentity, uint8_t iei
int encode_5gs_mobile_identity(FGSMobileIdentity *fgsmobileidentity, uint8_t iei, uint8_t *buffer, uint32_t len)
{
// Return if buffer is shorter than min length
if (len < MIN_LEN_FGS_MOBILE_ID_IE)
return -1;
int encoded_rc = TLV_ENCODE_VALUE_DOESNT_MATCH;
uint32_t encoded = 0;
......@@ -86,6 +143,7 @@ int encode_5gs_mobile_identity(FGSMobileIdentity *fgsmobileidentity, uint8_t iei
}
encoded = encoded + 2;
len -= encoded;
if (fgsmobileidentity->guti.typeofidentity == FGS_MOBILE_IDENTITY_5G_GUTI) {
encoded_rc = encode_guti_5gs_mobile_identity(&fgsmobileidentity->guti,
......@@ -102,6 +160,10 @@ int encode_5gs_mobile_identity(FGSMobileIdentity *fgsmobileidentity, uint8_t iei
buffer + encoded);
}
if (fgsmobileidentity->stmsi.typeofidentity == FGS_MOBILE_IDENTITY_5GS_TMSI) {
encoded_rc = encode_stmsi_5gs_mobile_identity(buffer + encoded, &fgsmobileidentity->stmsi, len);
}
if (encoded_rc < 0) {
return encoded_rc;
}
......
......@@ -37,6 +37,8 @@
#ifndef FGS_MOBILE_IDENTITY_H_
#define FGS_MOBILE_IDENTITY_H_
// 4x 1bit according to 9.11.3.4 5GS mobile identity (type 5G-S-TMSI)
#define DIGIT1 0xF
typedef struct {
uint8_t spare:5;
......@@ -151,5 +153,8 @@ int encode_5gs_mobile_identity(FGSMobileIdentity *fgsmobileidentity, uint8_t iei
int decode_5gs_mobile_identity(FGSMobileIdentity *fgsmobileidentity, uint8_t iei, const uint8_t *buffer, uint32_t len);
int encode_stmsi_5gs_mobile_identity(uint8_t *buffer, Stmsi5GSMobileIdentity_t *stmsi, uint32_t len);
int decode_stmsi_5gs_mobile_identity(Stmsi5GSMobileIdentity_t *stmsi, const uint8_t *buffer, uint32_t len);
#endif /* FGS MOBILE IDENTITY_H_ */
......@@ -28,67 +28,34 @@
#include "TLVDecoder.h"
#include "NasKeySetIdentifier.h"
int decode_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei, uint8_t *buffer, uint32_t len)
/**
* @brief Decode the NAS key set identifier from one byte
*/
int decode_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei, uint8_t value)
{
int decoded = 0;
CHECK_PDU_POINTER_AND_LENGTH_DECODER(buffer, NAS_KEY_SET_IDENTIFIER_MINIMUM_LENGTH, len);
if (iei > 0) {
CHECK_IEI_DECODER((*buffer & 0xf0), iei);
CHECK_IEI_DECODER((value & 0xf0), iei);
}
naskeysetidentifier->tsc = (*(buffer + decoded) >> 3) & 0x1;
naskeysetidentifier->naskeysetidentifier = *(buffer + decoded) & 0x7;
decoded++;
#if defined (NAS_DEBUG)
dump_nas_key_set_identifier_xml(naskeysetidentifier, iei);
#endif
return decoded;
}
naskeysetidentifier->tsc = (value >> 3) & 0x1;
naskeysetidentifier->naskeysetidentifier = value & 0x7;
int decode_u8_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei, uint8_t value, uint32_t len)
{
int decoded = 0;
uint8_t *buffer = &value;
naskeysetidentifier->tsc = (*(buffer + decoded) >> 3) & 0x1;
naskeysetidentifier->naskeysetidentifier = *(buffer + decoded) & 0x7;
decoded++;
#if defined (NAS_DEBUG)
#if defined(NAS_DEBUG)
dump_nas_key_set_identifier_xml(naskeysetidentifier, iei);
#endif
return decoded;
}
int encode_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei, uint8_t *buffer, uint32_t len)
{
uint8_t encoded = 0;
/* Checking length and pointer */
CHECK_PDU_POINTER_AND_LENGTH_ENCODER(buffer, NAS_KEY_SET_IDENTIFIER_MINIMUM_LENGTH, len);
#if defined (NAS_DEBUG)
dump_nas_key_set_identifier_xml(naskeysetidentifier, iei);
#endif
*(buffer + encoded) = 0x00 | (iei & 0xf0) |
((naskeysetidentifier->tsc & 0x1) << 3) |
(naskeysetidentifier->naskeysetidentifier & 0x7);
encoded++;
return encoded;
return 1; // 1 octet
}
uint8_t encode_u8_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier)
/**
* @brief Encode the NAS key set identifier in one byte
*/
uint8_t encode_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei)
{
uint8_t bufferReturn;
uint8_t *buffer = &bufferReturn;
uint8_t encoded = 0;
uint8_t iei = 0;
#if defined (NAS_DEBUG)
dump_nas_key_set_identifier_xml(naskeysetidentifier, 0);
dump_nas_key_set_identifier_xml(naskeysetidentifier, iei);
#endif
*(buffer + encoded) = 0x00 | (iei & 0xf0) |
((naskeysetidentifier->tsc & 0x1) << 3) |
(naskeysetidentifier->naskeysetidentifier & 0x7);
encoded++;
return bufferReturn;
return ((iei & 0xf0) | (naskeysetidentifier->tsc & 0x1) << 3) | (naskeysetidentifier->naskeysetidentifier & 0x7);
}
void dump_nas_key_set_identifier_xml(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei)
......
......@@ -39,15 +39,11 @@ typedef struct NasKeySetIdentifier_tag {
uint8_t naskeysetidentifier:3;
} NasKeySetIdentifier;
int encode_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei, uint8_t *buffer, uint32_t len);
uint8_t encode_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei);
void dump_nas_key_set_identifier_xml(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei);
uint8_t encode_u8_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier);
int decode_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei, uint8_t *buffer, uint32_t len);
int decode_u8_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei, uint8_t value, uint32_t len);
int decode_nas_key_set_identifier(NasKeySetIdentifier *naskeysetidentifier, uint8_t iei, uint8_t value);
#endif /* NAS KEY SET IDENTIFIER_H_ */
......@@ -25,7 +25,6 @@
#include <common/utils/LOG/log.h>
#include <openair3/UICC/usim_interface.h>
#include <openair2/LAYER2/NR_MAC_gNB/nr_mac_gNB.h>
/* TS 24.007 possible L3 formats:
Table 11.1: Formats of information elements
......@@ -72,9 +71,9 @@ typedef enum {
TYPE_DEF(FGS_DEREGISTRATION_ACCEPT_UE_ORIGINATING, 0x46) \
TYPE_DEF(DeregistrationrequestUEterminated, 0x47) \
TYPE_DEF(DeregistrationacceptUEterminated, 0x48) \
TYPE_DEF(Servicerequest, 0x4c) \
TYPE_DEF(Servicereject, 0x4d) \
TYPE_DEF(Serviceaccept, 0x4e) \
TYPE_DEF(FGS_SERVICE_REQUEST, 0x4c) \
TYPE_DEF(FGS_SERVICE_REJECT, 0x4d) \
TYPE_DEF(FGS_SERVICE_ACCEPT, 0x4e) \
TYPE_DEF(Controlplaneservicerequest, 0x4f) \
TYPE_DEF(Networkslicespecificauthenticationcommand, 0x50) \
TYPE_DEF(Networkslicespecificauthenticationcomplete, 0x51) \
......@@ -257,6 +256,7 @@ typedef enum {
} cause_secu_id_t;
// IEI (information element identifier) are spread in each message definition
#define IEI_NULL 0x00
#define IEI_RAND 0x21
#define IEI_AUTN 0x20
#define IEI_EAP 0x78
......@@ -381,18 +381,6 @@ int resToresStar(uint8_t *msg, const uicc_t* uicc);
int identityResponse(void **msg, nr_user_nas_t *UE);
int authenticationResponse(void **msg, nr_user_nas_t *UE);
void UEprocessNAS(void *msg,nr_user_nas_t *UE);
void SGSabortUE(void *msg, NRUEcontext_t *UE) ;
void SGSregistrationReq(void *msg, NRUEcontext_t *UE);
void SGSderegistrationUEReq(void *msg, NRUEcontext_t *UE);
void SGSauthenticationResp(void *msg, NRUEcontext_t *UE);
void SGSidentityResp(void *msg, NRUEcontext_t *UE);
void SGSsecurityModeComplete(void *msg, NRUEcontext_t *UE);
void SGSregistrationComplete(void *msg, NRUEcontext_t *UE);
void processNAS(void *msg, NRUEcontext_t *UE);
int identityRequest(void **msg, NRUEcontext_t *UE);
int authenticationRequest(void **msg, NRUEcontext_t *UE);
int securityModeCommand(void **msg, NRUEcontext_t *UE);
void servingNetworkName(uint8_t *msg, char * imsiStr, int nmc_size);
#endif
......@@ -23,28 +23,4 @@
#include <stdlib.h>
#include <string.h>
#include "TLVDecoder.h"
int errorCodeDecoder = 0;
static const char* const errorCodeStringDecoder[] = {
"No error",
"Buffer NULL",
"Buffer too short",
"Unexpected IEI",
"Mandatory field not present",
"Wrong message type",
"EXT value doesn't match",
"Protocol not supported",
};
void tlv_decode_perror(void)
{
if (errorCodeDecoder >= 0)
// No error or TLV_DECODE_ERR_OK
return;
printf("TLV decoder error: (%d, %s)\n", errorCodeDecoder, errorCodeStringDecoder[errorCodeDecoder * -1]);
}
......@@ -95,48 +95,56 @@ typedef enum {
extern int errorCodeDecoder;
void tlv_decode_perror(void);
#define CHECK_PDU_POINTER_AND_LENGTH_DECODER(bUFFER, mINIMUMlENGTH, lENGTH) \
if (bUFFER == NULL) \
{ \
printf("(%s:%d) Got NULL pointer for the payload\n", \
__FILE__, __LINE__); \
errorCodeDecoder = TLV_DECODE_BUFFER_NULL; \
LOG_FUNC_RETURN(TLV_DECODE_BUFFER_NULL); \
} \
if (lENGTH < mINIMUMlENGTH) \
{ \
printf("(%s:%d) Expecting at least %d bytes, got %u\n", \
__FILE__, __LINE__, mINIMUMlENGTH, lENGTH); \
errorCodeDecoder = TLV_DECODE_BUFFER_TOO_SHORT; \
LOG_FUNC_RETURN(TLV_DECODE_BUFFER_TOO_SHORT); \
}
#define CHECK_LENGTH_DECODER(bUFFERlENGTH, lENGTH) \
if (bUFFERlENGTH < lENGTH) \
{ \
errorCodeDecoder = TLV_DECODE_BUFFER_TOO_SHORT; \
LOG_FUNC_RETURN(TLV_DECODE_BUFFER_TOO_SHORT); \
}
#define CHECK_MESSAGE_TYPE(mESSAGE_tYPE, bUFFER) \
{ \
if (mESSAGE_tYPE != bUFFER) \
{ \
errorCodeDecoder = TLV_DECODE_WRONG_MESSAGE_TYPE; \
LOG_FUNC_RETURN(errorCodeDecoder); \
} \
}
#define CHECK_IEI_DECODER(iEI, bUFFER) \
if(iEI != bUFFER) \
{ \
printf("IEI is different than the one expected." \
"(Got: 0x%x, expecting: 0x%x)\n", bUFFER, iEI); \
errorCodeDecoder = TLV_DECODE_UNEXPECTED_IEI; \
LOG_FUNC_RETURN(TLV_DECODE_UNEXPECTED_IEI); \
}
#ifdef ENABLE_TESTS
#define TLV_DEC_ERROR(...) fprintf(stderr, "TLV Decoder: " __VA_ARGS__)
#else
#define TLV_DEC_ERROR(...) // Do nothing
#endif
#endif /* define (TLV_DECODER_H_) */
#define CHECK_PDU_POINTER_AND_LENGTH_DECODER(bUFFER, mINIMUMlENGTH, lENGTH) \
do { \
if ((bUFFER) == NULL) { \
TLV_DEC_ERROR("(%s:%d) Got NULL pointer for the payload\n", __FILE__, __LINE__); \
return TLV_DECODE_BUFFER_NULL; \
} \
if ((lENGTH) < (mINIMUMlENGTH)) { \
TLV_DEC_ERROR("(%s:%d) Expecting at least %d bytes, got %u\n", __FILE__, __LINE__, (mINIMUMlENGTH), (lENGTH)); \
return TLV_DECODE_BUFFER_TOO_SHORT; \
} \
} while (0)
#define CHECK_LENGTH_DECODER(bUFFERlENGTH, lENGTH) \
do { \
if ((bUFFERlENGTH) < (lENGTH)) { \
TLV_DEC_ERROR("(%s:%d) Buffer is too short, length (%u) is less than the required length (%u)\n", \
__FILE__, \
__LINE__, \
(bUFFERlENGTH), \
(lENGTH)); \
return TLV_DECODE_BUFFER_TOO_SHORT; \
} \
} while (0)
#define CHECK_MESSAGE_TYPE(mESSAGE_TYPE, BUFFER) \
do { \
if ((MESSAGE_TYPE) != (BUFFER)) { \
TLV_DEC_ERROR("(%s:%d) Wrong message type: Got: %u, Expected: %u\n", __FILE__, __LINE__, (BUFFER), (MESSAGE_TYPE)); \
return TLV_DECODE_WRONG_MESSAGE_TYPE; \
} \
} while (0)
#define CHECK_IEI_DECODER(iEI, bUFFER) \
do { \
if ((iEI) != (bUFFER)) { \
TLV_DEC_ERROR( \
"IEI check failure: %s:%d: IEI is different than the one expected: " \
"(Got: 0x%x, expecting: 0x%x)\n", \
__FILE__, \
__LINE__, \
(bUFFER), \
(iEI)); \
return TLV_DECODE_UNEXPECTED_IEI; \
} \
} while (0)
#endif /* define (TLV_DECODER_H_) */
......@@ -84,32 +84,32 @@ typedef enum {
* it cannot be further processed */
#define TLV_ENCODE_FATAL_ERROR (TLV_ENCODE_VALUE_DOESNT_MATCH)
extern int errorCodeEncoder;
void tlv_encode_perror(void);
#define CHECK_PDU_POINTER_AND_LENGTH_ENCODER(bUFFER, mINIMUMlENGTH, lENGTH) \
if (bUFFER == NULL) \
{ \
printf("Got NULL pointer for the payload\n"); \
errorCodeEncoder = TLV_ENCODE_BUFFER_NULL; \
return TLV_ENCODE_BUFFER_NULL; \
} \
if (lENGTH < mINIMUMlENGTH) \
{ \
printf("(%s:%d) Expecting at least %d bytes, got %u\n", \
__FILE__, __LINE__, mINIMUMlENGTH, lENGTH); \
errorCodeEncoder = TLV_ENCODE_BUFFER_TOO_SHORT; \
return TLV_ENCODE_BUFFER_TOO_SHORT; \
}
#define CHECK_PDU_POINTER_ENCODER(bUFFER) \
if (bUFFER == NULL) \
{ \
printf("Got NULL pointer for the payload\n"); \
errorCodeEncoder = TLV_ENCODE_BUFFER_NULL; \
return TLV_ENCODE_BUFFER_NULL; \
}
#ifdef ENABLE_TESTS
#define TLV_ENC_ERROR(...) fprintf(stderr, "TLV Encoder: " __VA_ARGS__)
#else
#define TLV_ENC_ERROR(...) // Do nothing
#endif
#define CHECK_PDU_POINTER_AND_LENGTH_ENCODER(bUFFER, mINIMUMlENGTH, lENGTH) \
do { \
if ((bUFFER) == NULL) { \
TLV_ENC_ERROR("Got NULL pointer for the payload\n"); \
return TLV_ENCODE_BUFFER_NULL; \
} \
if ((lENGTH) < (mINIMUMlENGTH)) { \
TLV_ENC_ERROR("(%s:%d) Expecting at least %d bytes, got %u\n", __FILE__, __LINE__, (mINIMUMlENGTH), (lENGTH)); \
return TLV_ENCODE_BUFFER_TOO_SHORT; \
} \
} while (0)
#define CHECK_PDU_POINTER_ENCODER(bUFFER) \
do { \
if ((bUFFER) == NULL) { \
TLV_ENC_ERROR("Got NULL pointer for the payload\n"); \
errorCodeEncoder = TLV_ENCODE_BUFFER_NULL; \
return TLV_ENCODE_BUFFER_NULL; \
} \
} while (0)
#endif /* define (TLV_ENCODER_H_) */
......@@ -21,6 +21,7 @@
* contact@openairinterface.org
*/
#include <arpa/inet.h>
#include <openair3/NAS/COMMON/NR_NAS_defs.h>
#include <openair3/SECU/secu_defs.h>
#include <openair3/SECU/kdf.h>
......
......@@ -194,6 +194,18 @@ static int fill_guti(FGSMobileIdentity *mi, const Guti5GSMobileIdentity_t *guti)
return 13;
}
static int fill_fgstmsi(Stmsi5GSMobileIdentity_t *stmsi, const Guti5GSMobileIdentity_t *guti)
{
AssertFatal(guti != NULL, "UE has no GUTI\n");
stmsi->amfpointer = guti->amfpointer;
stmsi->amfsetid = guti->amfsetid;
stmsi->tmsi = guti->tmsi;
stmsi->digit1 = DIGIT1;
stmsi->spare = 0;
stmsi->typeofidentity = FGS_MOBILE_IDENTITY_5GS_TMSI;
return 10;
}
static int fill_imeisv(FGSMobileIdentity *mi, const uicc_t *uicc)
{
int i = 0;
......@@ -260,6 +272,9 @@ int mm_msg_encode(MM_msg *mm_msg, uint8_t *buffer, uint32_t len)
encode_result =
encode_fgs_deregistration_request_ue_originating(&mm_msg->fgs_deregistration_request_ue_originating, buffer, len);
break;
case FGS_SERVICE_REQUEST:
encode_result = encode_fgs_service_request(buffer, &mm_msg->service_request, len);
break;
default:
LOG_TRACE(ERROR, "EMM-MSG - Unexpected message type: 0x%x", mm_msg->header.message_type);
encode_result = TLV_ENCODE_WRONG_MESSAGE_TYPE;
......@@ -550,6 +565,84 @@ void generateRegistrationRequest(as_nas_info_t *initialNasMsg, nr_ue_nas_t *nas)
nas->registration_request_len = initialNasMsg->length;
}
void generateServiceRequest(as_nas_info_t *initialNasMsg, nr_ue_nas_t *nas)
{
LOG_I(NAS, "Generate initial NAS message: Service Request\n");
int size = 0;
fgs_nas_message_t nas_msg = {0};
memset(&nas_msg, 0, sizeof(nas_msg));
// NAS is security protected if has valid security contexts
bool security_protected = nas->security_container->ciphering_context && nas->security_container->integrity_context;
MM_msg *mm_msg;
if (security_protected) {
/* Set security protected 5GS NAS message header (see 9.1.1 of 3GPP TS 24.501) */
nas_msg.header.protocol_discriminator = FGS_MOBILITY_MANAGEMENT_MESSAGE;
nas_msg.header.security_header_type = INTEGRITY_PROTECTED;
nas_msg.header.sequence_number = nas->security.nas_count_ul & 0xff;
size += sizeof(fgs_nas_message_security_header_t);
mm_msg = &nas_msg.security_protected.plain.mm_msg;
} else {
// Set Mobility Management plain message header
mm_msg = &nas_msg.plain.mm_msg;
}
// Fill MM plain message header
mm_msg->header.ex_protocol_discriminator = FGS_MOBILITY_MANAGEMENT_MESSAGE;
mm_msg->header.security_header_type = PLAIN_5GS_MSG;
mm_msg->header.message_type = FGS_SERVICE_REQUEST;
size += sizeof(mm_msg_header_t);
// Fill Service Request
// Service Type
mm_msg->service_request.serviceType = SERVICE_TYPE_DATA;
// NAS key set identifier
mm_msg->service_request.naskeysetidentifier.naskeysetidentifier = NAS_KEY_SET_IDENTIFIER_NOT_AVAILABLE;
mm_msg->service_request.naskeysetidentifier.tsc = NAS_KEY_SET_IDENTIFIER_NATIVE;
size += 1;
// 5G-S-TMSI
size += fill_fgstmsi(&mm_msg->service_request.fiveg_s_tmsi, nas->guti);
/* message encoding */
initialNasMsg->nas_data = (Byte_t *)malloc(size * sizeof(Byte_t));
if (security_protected) {
// security protected encoding
int security_header_len = nas_protected_security_header_encode((char *)(initialNasMsg->nas_data), &(nas_msg.header), size);
initialNasMsg->length =
security_header_len
+ mm_msg_encode(mm_msg, (uint8_t *)(initialNasMsg->nas_data + security_header_len), size - security_header_len);
/* ciphering */
uint8_t buf[initialNasMsg->length - 7];
nas_stream_cipher_t stream_cipher;
stream_cipher.context = nas->security_container->ciphering_context;
AssertFatal(nas->security.nas_count_ul <= 0xffffff, "fatal: NAS COUNT UL too big (todo: fix that)\n");
stream_cipher.count = nas->security.nas_count_ul;
stream_cipher.bearer = 1;
stream_cipher.direction = 0;
stream_cipher.message = (unsigned char *)(initialNasMsg->nas_data + 7);
/* length in bits */
stream_cipher.blength = (initialNasMsg->length - 7) << 3;
stream_compute_encrypt(nas->security_container->ciphering_algorithm, &stream_cipher, buf);
memcpy(stream_cipher.message, buf, initialNasMsg->length - 7);
/* integrity protection */
uint8_t mac[4];
stream_cipher.context = nas->security_container->integrity_context;
stream_cipher.count = nas->security.nas_count_ul++;
stream_cipher.bearer = 1;
stream_cipher.direction = 0;
stream_cipher.message = (unsigned char *)(initialNasMsg->nas_data + 6);
/* length in bits */
stream_cipher.blength = (initialNasMsg->length - 6) << 3;
stream_compute_integrity(nas->security_container->integrity_algorithm, &stream_cipher, mac);
printf("Integrity protected initial NAS message: mac = %x %x %x %x \n", mac[0], mac[1], mac[2], mac[3]);
for (int i = 0; i < 4; i++)
initialNasMsg->nas_data[2 + i] = mac[i];
} else {
// plain encoding
initialNasMsg->length = mm_msg_encode(mm_msg, (uint8_t *)(initialNasMsg->nas_data), size);
LOG_I(NAS, "PLAIN_5GS_MSG initial NAS message: Service Request with length %d \n", initialNasMsg->length);
}
}
void generateIdentityResponse(as_nas_info_t *initialNasMsg, uint8_t identitytype, uicc_t *uicc)
{
int size = sizeof(mm_msg_header_t);
......
......@@ -36,6 +36,7 @@
#include "FGSNASSecurityModeComplete.h"
#include "FGSDeregistrationRequestUEOriginating.h"
#include "RegistrationComplete.h"
#include "EMM/MSG/fgs_service_request.h"
#include "as_message.h"
#include "FGSUplinkNasTransport.h"
#include <openair3/UICC/usim_interface.h>
......@@ -48,6 +49,9 @@
#define PAYLOAD_CONTAINER_LENGTH_MIN 3
#define PAYLOAD_CONTAINER_LENGTH_MAX 65537
/* 3GPP TS 24.501: 9.11.3.50 Service type */
#define SERVICE_TYPE_DATA 0x1
/* List of allowed NSSAI from NAS messaging. */
typedef struct {
int sst;
......@@ -106,6 +110,7 @@ typedef struct {
typedef union {
mm_msg_header_t header;
registration_request_msg registration_request;
fgs_service_request_msg_t service_request;
fgs_identiy_response_msg fgs_identity_response;
fgs_authentication_response_msg fgs_auth_response;
fgs_deregistration_request_ue_originating_msg fgs_deregistration_request_ue_originating;
......@@ -158,6 +163,7 @@ typedef struct {
nr_ue_nas_t *get_ue_nas_info(module_id_t module_id);
void generateRegistrationRequest(as_nas_info_t *initialNasMsg, nr_ue_nas_t *nas);
void generateServiceRequest(as_nas_info_t *initialNasMsg, nr_ue_nas_t *nas);
void *nas_nrue_task(void *args_p);
void *nas_nrue(void *args_p);
void nas_init_nrue(int num_ues);
......
#include <common/config/config_userapi.h>
#include <openair3/NAS/COMMON/NR_NAS_defs.h>
volatile int oai_exit;
configmodule_interface_t *uniqCfg = NULL;
int main(int argc, char **argv) {
if ((uniqCfg = load_configmodule(argc, argv, CONFIG_ENABLECMDLINEONLY)) == NULL) {
exit_fun("[SOFTMODEM] Error, configuration module init failed\n");
}
logInit();
char *resp;
nr_user_nas_t UErrc= {0};
NRUEcontext_t UEnas= {0};
// Network generate identity request after a sucessfull radio attach
int size=identityRequest((void **)&resp, &UEnas);
log_dump(NAS, resp, size, LOG_DUMP_CHAR," identity Request:\n" );
// UE process the message that it has received from phy layer in a "DL transfer" message
UEprocessNAS(resp, &UErrc);
// UE Scheduler should later call the response
size=identityResponse((void **)&resp, &UErrc);
log_dump(NAS, resp, size, LOG_DUMP_CHAR," identity Response:\n" );
// Now the gNB process the identity response
processNAS(resp, &UEnas);
// gNB scheduler should call the next query
size=authenticationRequest((void **)&resp, &UEnas);
log_dump(NAS, resp, size, LOG_DUMP_CHAR," authentication request:\n" );
// as above
UEprocessNAS(resp, &UErrc);
size=authenticationResponse((void **)&resp, &UErrc);
log_dump(NAS, resp, size, LOG_DUMP_CHAR," authentication response:\n" );
processNAS(resp, &UEnas);
return 0;
}
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