Refactor 5G-S-TMSI: harmonize 5G-S-TMSI handling with centralized helpers

Centralizes 5G-S-TMSI bit manipulation logic for consistency and
maintainability. All manual bit operations replaced with documented
helper functions following TS 23.003 bit layout specification.

- Add helper functions for 5G-S-TMSI operations:

  * nr_construct_5g_s_tmsi_part1: 5G-S-TMSI construction is defined
    in 3GPP TS 23.003 and relevant at both gNB and UE side
    * Add helper functions to be later used in NG Paging
    * Adopt in NAS UE
  * nr_extract_5g_s_tmsi_part1() - extract Part1 (39 bits) from full
  * nr_extract_5g_s_tmsi_part2() - extract Part2 (9 bits) from full
  * nr_build_full_5g_s_tmsi() - reconstruct full from Part1+Part2
  * nr_deconstruct_5g_s_tmsi() - deconstruct into components
- Refactor manual bit manipulation to use helper functions:
  * asn1_msg.c: use nr_extract_5g_s_tmsi_part1() in RRCSetupRequest
  * asn1_msg.c: use nr_extract_5g_s_tmsi_part2() in RRCSetupComplete
  * rrc_gNB.c: use nr_build_full_5g_s_tmsi() when reconstructing from parts
  * rrc_gNB.c: use nr_deconstruct_5g_s_tmsi() when extracting components
- Optimize nr_construct_5g_s_tmsi_part1() to reuse extract function
This commit is contained in:
Guido Casati
2026-02-17 12:25:58 +01:00
parent 3c164ef138
commit f295252b2e
5 changed files with 77 additions and 8 deletions
+2 -2
View File
@@ -716,7 +716,7 @@ int do_RRCSetupRequest(uint8_t *buffer, size_t buffer_size, uint8_t *rv, uint64_
str->buf[3] = rv[3];
str->buf[4] = rv[4] & 0xfe;
} else {
uint64_t fiveG_S_TMSI_part1 = fiveG_S_TMSI & ((1ULL << 39) - 1);
uint64_t fiveG_S_TMSI_part1 = nr_extract_5g_s_tmsi_part1(fiveG_S_TMSI);
/** set the ue-Identity to ng-5G-S-TMSI-Part1
* ng-5G-S-TMSI-Part1: the rightmost 39 bits of 5G-S-TMSI
* BIT STRING (SIZE (39)) - 3GPP TS 38.331 */
@@ -840,7 +840,7 @@ int do_RRCSetupComplete(uint8_t *buffer,
str->size = 2;
str->bits_unused = 7;
str->buf = calloc_or_fail(str->size, sizeof(str->buf[0]));
uint16_t fiveG_s_tmsi_part2 = (fiveG_s_tmsi >> 39) & ((1ULL << 9) - 1);
uint16_t fiveG_s_tmsi_part2 = nr_extract_5g_s_tmsi_part2(fiveG_s_tmsi);
str->buf[0] = (fiveG_s_tmsi_part2 >> (8 - str->bits_unused)) & 0xFF;
str->buf[1] = (fiveG_s_tmsi_part2 << str->bits_unused) & 0xFF;
LOG_D(NR_RRC, "5G-S-TMSI part 2 %d in RRCSetupComplete (5G-S-TMSI %ld)\n", fiveG_s_tmsi_part2, fiveG_s_tmsi);
@@ -2,7 +2,7 @@ add_executable(test_asn1_msg
test_asn1_msg.cpp
${OPENAIR2_DIR}/LAYER2/nr_pdcp/asn1_utils.c
${OPENAIR2_DIR}/LAYER2/nr_rlc/asn1_utils.c)
target_link_libraries(test_asn1_msg PRIVATE nr_rrc SECURITY minimal_lib GTest::gtest UTIL)
target_link_libraries(test_asn1_msg PRIVATE nr_rrc SECURITY minimal_lib GTest::gtest UTIL nr_common)
add_dependencies(tests test_asn1_msg)
add_test(NAME test_asn1_msg
COMMAND ./test_asn1_msg)
+6 -4
View File
@@ -60,6 +60,7 @@
#include "common/openairinterface5g_limits.h"
#include "common/platform_constants.h"
#include "common/ran_context.h"
#include "common/utils/nr/nr_common.h"
#include "common_lib.h"
#include "constr_SEQUENCE.h"
#include "constr_TYPE.h"
@@ -2073,7 +2074,7 @@ static void handle_rrcSetupComplete(gNB_RRC_INST *rrc, gNB_RRC_UE_t *UE, const N
uint16_t stmsi_part2 = BIT_STRING_to_uint16(part2);
LOG_I(RRC, "s_tmsi part2 %d (%02x %02x)\n", stmsi_part2, part2->buf[0], part2->buf[1]);
// Part2 is leftmost 9, Part1 is rightmost 39 bits of 5G-S-TMSI
fiveg_s_TMSI = ((uint64_t) stmsi_part2) << 39 | UE->ng_5G_S_TMSI_Part1;
fiveg_s_TMSI = nr_build_full_5g_s_tmsi(UE->ng_5G_S_TMSI_Part1, stmsi_part2);
} else {
LOG_W(RRC, "UE %d received 5G-S-TMSI-Part2, but no 5G-S-TMSI-Part1 present, won't send 5G-S-TMSI to core\n", UE->rrc_ue_id);
UE->Initialue_identity_5g_s_TMSI.presence = false;
@@ -2090,9 +2091,10 @@ static void handle_rrcSetupComplete(gNB_RRC_INST *rrc, gNB_RRC_UE_t *UE, const N
}
if (UE->Initialue_identity_5g_s_TMSI.presence) {
uint16_t amf_set_id = fiveg_s_TMSI >> 38;
uint8_t amf_pointer = (fiveg_s_TMSI >> 32) & 0x3F;
uint32_t fiveg_tmsi = (uint32_t) fiveg_s_TMSI;
uint16_t amf_set_id;
uint8_t amf_pointer;
uint32_t fiveg_tmsi;
nr_deconstruct_5g_s_tmsi(fiveg_s_TMSI, &amf_set_id, &amf_pointer, &fiveg_tmsi);
LOG_I(NR_RRC,
"5g_s_TMSI: 0x%lX, amf_set_id: 0x%X (%d), amf_pointer: 0x%X (%d), 5g TMSI: 0x%X \n",
fiveg_s_TMSI,