rrc_gNB_NGAP.c 69.9 KB
Newer Older
Xue Song's avatar
Xue Song committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
 */

22 23 24 25 26 27 28 29 30
/*! \file rrc_gNB_NGAP.h
 * \brief rrc NGAP procedures for gNB
 * \author Yoshio INOUE, Masayuki HARADA
 * \date 2020
 * \version 0.1
 * \email: yoshio.inoue@fujitsu.com,masayuki.harada@fujitsu.com
 *         (yoshio.inoue%40fujitsu.com%2cmasayuki.harada%40fujitsu.com) 
 */

Xue Song's avatar
Xue Song committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "rrc_gNB_NGAP.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
#include "rrc_eNB_S1AP.h"
#include "gnb_config.h"
#include "common/ran_context.h"
#include "gtpv1u.h"

#include "asn1_conversions.h"
#include "intertask_interface.h"
#include "pdcp.h"
#include "pdcp_primitives.h"

#include "msc.h"

#include "gtpv1u_eNB_task.h"
46
#include "gtpv1u_gNB_task.h"
Xue Song's avatar
Xue Song committed
47
#include "RRC/LTE/rrc_eNB_GTPV1U.h"
48
#include "RRC/NR/rrc_gNB_GTPV1U.h"
Xue Song's avatar
Xue Song committed
49 50 51

#include "S1AP_NAS-PDU.h"
#include "executables/softmodem-common.h"
Xue Song's avatar
Xue Song committed
52
#include "UTIL/OSA/osa_defs.h"
53 54
#include "ngap_gNB_defs.h"
#include "ngap_gNB_ue_context.h"
Xue Song's avatar
Xue Song committed
55
#include "ngap_gNB_management_procedures.h"
56
#include "NR_ULInformationTransfer.h"
Xue Song's avatar
Xue Song committed
57
#include "RRC/NR/MESSAGES/asn1_msg.h"
58 59
#include "NR_UERadioAccessCapabilityInformation.h"
#include "NR_UE-CapabilityRAT-ContainerList.h"
60 61
#include "NGAP_Cause.h"
#include "NGAP_CauseRadioNetwork.h"
62
#include "f1ap_messages_types.h"
Xue Song's avatar
Xue Song committed
63 64 65 66 67 68

extern RAN_CONTEXT_t RC;

/* Value to indicate an invalid UE initial id */
static const uint16_t UE_INITIAL_ID_INVALID = 0;

Xue Song's avatar
Xue Song committed
69 70 71 72 73 74 75 76 77 78 79 80
/* Masks for NGAP Encryption algorithms, NEA0 is always supported (not coded) */
static const uint16_t NGAP_ENCRYPTION_NEA1_MASK = 0x8000;
static const uint16_t NGAP_ENCRYPTION_NEA2_MASK = 0x4000;
static const uint16_t NGAP_ENCRYPTION_NEA3_MASK = 0x2000;

/* Masks for NGAP Integrity algorithms, NIA0 is always supported (not coded) */
static const uint16_t NGAP_INTEGRITY_NIA1_MASK = 0x8000;
static const uint16_t NGAP_INTEGRITY_NIA2_MASK = 0x4000;
static const uint16_t NGAP_INTEGRITY_NIA3_MASK = 0x2000;

#define INTEGRITY_ALGORITHM_NONE NR_IntegrityProtAlgorithm_nia0

Xue Song's avatar
Xue Song committed
81 82 83 84 85 86 87 88 89 90 91 92
/*! \fn uint16_t get_next_ue_initial_id(uint8_t mod_id)
 *\brief provide an UE initial ID for NGAP initial communication.
 *\param mod_id Instance ID of gNB.
 *\return the UE initial ID.
 */
//------------------------------------------------------------------------------
static uint16_t
get_next_ue_initial_id(
    const module_id_t mod_id
)
//------------------------------------------------------------------------------
{
93 94
  static uint16_t ue_initial_id[NUMBER_OF_gNB_MAX];
  ue_initial_id[mod_id]++;
Xue Song's avatar
Xue Song committed
95

96 97 98 99
  /* Never use UE_INITIAL_ID_INVALID this is the invalid id! */
  if (ue_initial_id[mod_id] == UE_INITIAL_ID_INVALID) {
    ue_initial_id[mod_id]++;
  }
Xue Song's avatar
Xue Song committed
100

101
  return ue_initial_id[mod_id];
Xue Song's avatar
Xue Song committed
102 103
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117
//------------------------------------------------------------------------------
/*
* Get the UE NG struct containing hashtables NG_id/UE_id.
* Is also used to set the NG_id of the UE, depending on inputs.
*/
struct rrc_ue_ngap_ids_s *
rrc_gNB_NGAP_get_ue_ids(
    gNB_RRC_INST   *const rrc_instance_pP,
    const uint16_t ue_initial_id,
    const uint32_t gNB_ue_ngap_idP
)
//------------------------------------------------------------------------------
{
    rrc_ue_ngap_ids_t *result = NULL;
118 119 120 121 122 123 124 125 126 127 128
    rrc_ue_ngap_ids_t *result2 = NULL;
  /*****************************/
  instance_t instance = 0;
  ngap_gNB_instance_t *ngap_gNB_instance_p = NULL;
  ngap_gNB_ue_context_t *ue_desc_p = NULL;
  rrc_gNB_ue_context_t *ue_context_p = NULL;
  /*****************************/
  hashtable_rc_t     h_rc;

  if (ue_initial_id != UE_INITIAL_ID_INVALID) {
    h_rc = hashtable_get(rrc_instance_pP->initial_id2_ngap_ids, (hash_key_t)ue_initial_id, (void **)&result);
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    if (h_rc == HASH_TABLE_OK) {
      if (gNB_ue_ngap_idP > 0) {
        h_rc = hashtable_get(rrc_instance_pP->ngap_id2_ngap_ids, (hash_key_t)gNB_ue_ngap_idP, (void **)&result2);

        if (h_rc != HASH_TABLE_OK) { // this case is equivalent to associate gNB_ue_ngap_idP and ue_initial_id
          result2 = malloc(sizeof(*result2));

          if (NULL != result2) {
            *result2 = *result;
            result2->gNB_ue_ngap_id = gNB_ue_ngap_idP;
            result->gNB_ue_ngap_id  = gNB_ue_ngap_idP;
            h_rc = hashtable_insert(rrc_instance_pP->ngap_id2_ngap_ids, (hash_key_t)gNB_ue_ngap_idP, result2);

            if (h_rc != HASH_TABLE_OK) {
              LOG_E(NGAP, "[gNB %ld] Error while hashtable_insert in ngap_id2_ngap_ids gNB_ue_ngap_idP %"PRIu32"\n",
                    rrc_instance_pP - RC.nrrrc[0],
                    gNB_ue_ngap_idP);
            }
          }
        } else { // here we should check that the association was done correctly
          if ((result->ue_initial_id != result2->ue_initial_id) || (result->gNB_ue_ngap_id != result2->gNB_ue_ngap_id)) {
            LOG_E(NGAP, "[gNB %ld] Error while hashtable_get, two rrc_ue_ngap_ids_t that should be equal, are not:\n \
              ue_initial_id 1 = %"PRIu16",\n \
              ue_initial_id 2 = %"PRIu16",\n \
              gNB_ue_ngap_idP 1 = %"PRIu32",\n \
              gNB_ue_ngap_idP 2 = %"PRIu32"\n",
                  rrc_instance_pP - RC.nrrrc[0],
                  result->ue_initial_id,
                  result2->ue_initial_id,
                  result->gNB_ue_ngap_id,
                  result2->gNB_ue_ngap_id);
            // Still return *result
          }
        }
Xue Song's avatar
Xue Song committed
164
      } // end if if (gNB_ue_ngap_idP > 0)
165 166 167 168 169
    } else { // end if (h_rc == HASH_TABLE_OK)
      LOG_E(NGAP, "[gNB %ld] In hashtable_get, couldn't find in initial_id2_ngap_ids ue_initial_id %"PRIu16"\n",
            rrc_instance_pP - RC.nrrrc[0],
            ue_initial_id);
      return NULL;
Xue Song's avatar
Xue Song committed
170 171 172 173 174
      /*
      * At the moment this is written, this case shouldn't (cannot) happen and is equivalent to an error.
      * One could try to find the struct instance based on ngap_id2_ngap_ids and gNB_ue_ngap_idP (if > 0),
      * but this behavior is not expected at the moment.
      */
175 176
    } // end else (h_rc != HASH_TABLE_OK)
  } else { // end if (ue_initial_id != UE_INITIAL_ID_INVALID)
Xue Song's avatar
Xue Song committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    if (gNB_ue_ngap_idP > 0) {
      h_rc = hashtable_get(rrc_instance_pP->ngap_id2_ngap_ids, (hash_key_t)gNB_ue_ngap_idP, (void **)&result);

      if (h_rc != HASH_TABLE_OK) {
        /*
        * This case is uncommon, but can happen when:
        * -> if the first NAS message was a Detach Request (non exhaustiv), the UE RRC context exist
        * but is not associated with gNB_ue_ngap_id
        * -> ... (?)
        */
        LOG_E(NGAP, "[gNB %ld] In hashtable_get, couldn't find in ngap_id2_ngap_ids gNB_ue_ngap_idP %"PRIu32", trying to find it through NGAP context\n",
              rrc_instance_pP - RC.nrrrc[0],
              gNB_ue_ngap_idP);
        instance = GNB_MODULE_ID_TO_INSTANCE(rrc_instance_pP - RC.nrrrc[0]); // get gNB instance
        ngap_gNB_instance_p = ngap_gNB_get_instance(instance); // get ngap_gNB_instance

        if (ngap_gNB_instance_p != NULL) {
          ue_desc_p = ngap_gNB_get_ue_context(ngap_gNB_instance_p, gNB_ue_ngap_idP); // get s1ap_eNB_ue_context
        } else {
196
          LOG_E(NGAP, "[gNB instance %ld] Couldn't find the gNB NGAP context\n",
Xue Song's avatar
Xue Song committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                instance);
          return NULL;
        }

        if (ue_desc_p != NULL) {
          struct ngap_gNB_ue_context_s *ngap_ue_context_p = NULL;

          if ((ngap_ue_context_p = RB_REMOVE(ngap_ue_map, &ngap_gNB_instance_p->ngap_ue_head, ue_desc_p)) != NULL) {
            LOG_E(NR_RRC, "Removed UE context gNB_ue_ngap_id %u\n", ngap_ue_context_p->gNB_ue_ngap_id);
            ngap_gNB_free_ue_context(ngap_ue_context_p);
          } else {
            LOG_E(NR_RRC, "Removing UE context gNB_ue_ngap_id %u: did not find context\n",ue_desc_p->gNB_ue_ngap_id);
          }

          return NULL; //skip the operation below to avoid loop
          result = rrc_gNB_NGAP_get_ue_ids(rrc_instance_pP, ue_desc_p->ue_initial_id, gNB_ue_ngap_idP);

          if (ue_desc_p->ue_initial_id != UE_INITIAL_ID_INVALID) {
            result = rrc_gNB_NGAP_get_ue_ids(rrc_instance_pP, ue_desc_p->ue_initial_id, gNB_ue_ngap_idP);
216

Xue Song's avatar
Xue Song committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            if (result != NULL) {
              ue_context_p = rrc_gNB_get_ue_context(RC.nrrrc[GNB_INSTANCE_TO_MODULE_ID(instance)], result->ue_rnti);

              if ((ue_context_p != NULL) && (ue_context_p->ue_context.gNB_ue_ngap_id == 0)) {
                ue_context_p->ue_context.gNB_ue_ngap_id = gNB_ue_ngap_idP;
              } else {
                LOG_E(NR_RRC, "[gNB %ld] Incoherence between RRC context and NGAP context (%d != %d) for UE RNTI %d or UE RRC context doesn't exist\n",
                      rrc_instance_pP - RC.nrrrc[0],
                      (ue_context_p==NULL)?99999:ue_context_p->ue_context.gNB_ue_ngap_id,
                      gNB_ue_ngap_idP,
                      result->ue_rnti);
              }
            }
          } else {
            LOG_E(NGAP, "[gNB %ld] NGAP context found but ue_initial_id is invalid (0)\n", rrc_instance_pP - RC.nrrrc[0]);
            return NULL;
          }
        } else {
          LOG_E(NGAP, "[gNB %ld] In hashtable_get, couldn't find in ngap_id2_ngap_ids gNB_ue_ngap_idP %"PRIu32", because ue_initial_id is invalid in NGAP context\n",
                rrc_instance_pP - RC.nrrrc[0],
                gNB_ue_ngap_idP);
          return NULL;
        }
      } // end if (h_rc != HASH_TABLE_OK)
    } // end if (gNB_ue_ngap_idP > 0)
242
  } // end else (ue_initial_id == UE_INITIAL_ID_INVALID)
243

244
  return result;
245 246 247 248 249 250 251 252 253 254 255
}

//------------------------------------------------------------------------------
static struct rrc_gNB_ue_context_s *
rrc_gNB_get_ue_context_from_ngap_ids(
    const instance_t  instanceP,
    const uint16_t    ue_initial_idP,
    const uint32_t    gNB_ue_ngap_idP
) 
//------------------------------------------------------------------------------
{
256 257
  rrc_ue_ngap_ids_t *temp = NULL;
  temp = rrc_gNB_NGAP_get_ue_ids(RC.nrrrc[GNB_INSTANCE_TO_MODULE_ID(instanceP)], ue_initial_idP, gNB_ue_ngap_idP);
258

259 260 261
  if (temp != NULL) {
    return rrc_gNB_get_ue_context(RC.nrrrc[GNB_INSTANCE_TO_MODULE_ID(instanceP)], temp->ue_rnti);
  }
262

263
  return NULL;
264 265
}

Xue Song's avatar
Xue Song committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
/*! \fn void process_gNB_security_key (const protocol_ctxt_t* const ctxt_pP, eNB_RRC_UE_t * const ue_context_pP, uint8_t *security_key)
 *\brief save security key.
 *\param ctxt_pP         Running context.
 *\param ue_context_pP   UE context.
 *\param security_key_pP The security key received from NGAP.
 */
//------------------------------------------------------------------------------
void process_gNB_security_key (
  const protocol_ctxt_t *const ctxt_pP,
  rrc_gNB_ue_context_t  *const ue_context_pP,
  uint8_t               *security_key_pP
)
//------------------------------------------------------------------------------
{
  char ascii_buffer[65];
  uint8_t i;
  /* Saves the security key */
  memcpy (ue_context_pP->ue_context.kgnb, security_key_pP, SECURITY_KEY_LENGTH);
  memset (ue_context_pP->ue_context.nh, 0, SECURITY_KEY_LENGTH);
  ue_context_pP->ue_context.nh_ncc = -1;

  for (i = 0; i < 32; i++) {
    sprintf(&ascii_buffer[2 * i], "%02X", ue_context_pP->ue_context.kgnb[i]);
  }

  ascii_buffer[2 * i] = '\0';
  LOG_I(NR_RRC, "[gNB %d][UE %x] Saved security key %s\n", ctxt_pP->module_id, ue_context_pP->ue_context.rnti, ascii_buffer);
}

Xue Song's avatar
Xue Song committed
295 296 297 298 299
//------------------------------------------------------------------------------
void
nr_rrc_pdcp_config_security(
    const protocol_ctxt_t  *const ctxt_pP,
    rrc_gNB_ue_context_t   *const ue_context_pP,
Cedric Roux's avatar
Cedric Roux committed
300
    const uint8_t          enable_ciphering
Xue Song's avatar
Xue Song committed
301 302 303
)
//------------------------------------------------------------------------------
{
304
  NR_SRB_ToAddModList_t              *SRB_configList = ue_context_pP->ue_context.SRB_configList;
305
  (void)SRB_configList;
306 307 308
  uint8_t                            *kRRCenc = NULL;
  uint8_t                            *kRRCint = NULL;
  uint8_t                            *kUPenc = NULL;
rmagueta's avatar
rmagueta committed
309
  static int                         print_keys= 1;
Xue Song's avatar
Xue Song committed
310

311
#ifndef PHYSIM
rmagueta's avatar
rmagueta committed
312 313 314

  uint8_t *k_kdf = NULL;

315 316 317 318 319 320 321 322 323 324 325 326
  /* Derive the keys from kgnb */
  if (SRB_configList != NULL) {
    k_kdf = NULL;
    nr_derive_key_up_enc(ue_context_pP->ue_context.ciphering_algorithm,
                         ue_context_pP->ue_context.kgnb,
                         &k_kdf);
    /* kUPenc: last 128 bits of key derivation function which returns 256 bits */
    kUPenc = malloc(16);
    if (kUPenc == NULL) exit(1);
    memcpy(kUPenc, k_kdf+16, 16);
    free(k_kdf);
  }
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  k_kdf = NULL;
  nr_derive_key_rrc_enc(ue_context_pP->ue_context.ciphering_algorithm,
                        ue_context_pP->ue_context.kgnb,
                        &k_kdf);
  /* kRRCenc: last 128 bits of key derivation function which returns 256 bits */
  kRRCenc = malloc(16);
  if (kRRCenc == NULL) exit(1);
  memcpy(kRRCenc, k_kdf+16, 16);
  free(k_kdf);

  k_kdf = NULL;
  nr_derive_key_rrc_int(ue_context_pP->ue_context.integrity_algorithm,
                        ue_context_pP->ue_context.kgnb,
                        &k_kdf);
  /* kRRCint: last 128 bits of key derivation function which returns 256 bits */
  kRRCint = malloc(16);
  if (kRRCint == NULL) exit(1);
  memcpy(kRRCint, k_kdf+16, 16);
  free(k_kdf);
347
#endif
348 349 350
  if (!IS_SOFTMODEM_IQPLAYER) {
    SET_LOG_DUMP(DEBUG_SECURITY) ;
  }
Xue Song's avatar
Xue Song committed
351 352


353 354 355 356
  if ( LOG_DUMPFLAG( DEBUG_SECURITY ) ) {
    if (print_keys == 1 ) {
      print_keys =0;
      LOG_DUMPMSG(NR_RRC, DEBUG_SECURITY, ue_context_pP->ue_context.kgnb, 32,"\nKgNB:" );
357 358
      LOG_DUMPMSG(NR_RRC, DEBUG_SECURITY, kRRCenc, 16,"\nKRRCenc:" );
      LOG_DUMPMSG(NR_RRC, DEBUG_SECURITY, kRRCint, 16,"\nKRRCint:" );
Xue Song's avatar
Xue Song committed
359
    }
360
  }
Xue Song's avatar
Xue Song committed
361

362 363 364 365 366
  pdcp_config_set_security(
      ctxt_pP,
      NULL,      /* pdcp_pP not used anymore in NR */
      DCCH,
      DCCH+2,
Cedric Roux's avatar
Cedric Roux committed
367 368 369 370 371
      enable_ciphering ?
             ue_context_pP->ue_context.ciphering_algorithm
          | (ue_context_pP->ue_context.integrity_algorithm << 4)
        :    0
          | (ue_context_pP->ue_context.integrity_algorithm << 4),
372 373 374
      kRRCenc,
      kRRCint,
      kUPenc);
Xue Song's avatar
Xue Song committed
375 376
}

Xue Song's avatar
Xue Song committed
377 378 379 380 381 382 383 384 385 386 387 388
//------------------------------------------------------------------------------
/*
* Initial UE NAS message on S1AP.
*/
void
rrc_gNB_send_NGAP_NAS_FIRST_REQ(
    const protocol_ctxt_t     *const ctxt_pP,
    rrc_gNB_ue_context_t      *ue_context_pP,
    NR_RRCSetupComplete_IEs_t *rrcSetupComplete
)
//------------------------------------------------------------------------------
{
389 390 391
  // gNB_RRC_INST *rrc = RC.nrrrc[ctxt_pP->module_id];
  MessageDef         *message_p         = NULL;
  rrc_ue_ngap_ids_t  *rrc_ue_ngap_ids_p = NULL;
392
  hashtable_rc_t      h_rc;
393

394
  message_p = itti_alloc_new_message(TASK_RRC_GNB, 0, NGAP_NAS_FIRST_REQ);
395 396 397 398 399 400 401 402
  memset(&message_p->ittiMsg.ngap_nas_first_req, 0, sizeof(ngap_nas_first_req_t));
  ue_context_pP->ue_context.ue_initial_id = get_next_ue_initial_id(ctxt_pP->module_id);
  NGAP_NAS_FIRST_REQ(message_p).ue_initial_id = ue_context_pP->ue_context.ue_initial_id;
  rrc_ue_ngap_ids_p = malloc(sizeof(rrc_ue_ngap_ids_t));
  rrc_ue_ngap_ids_p->ue_initial_id  = ue_context_pP->ue_context.ue_initial_id;
  rrc_ue_ngap_ids_p->gNB_ue_ngap_id = UE_INITIAL_ID_INVALID;
  rrc_ue_ngap_ids_p->ue_rnti        = ctxt_pP->rnti;

403 404 405
  h_rc = hashtable_insert(RC.nrrrc[ctxt_pP->module_id]->initial_id2_ngap_ids,
                          (hash_key_t)ue_context_pP->ue_context.ue_initial_id,
                          rrc_ue_ngap_ids_p);
406

407 408 409 410 411
  if (h_rc != HASH_TABLE_OK) {
    LOG_E(NGAP, "[gNB %d] Error while hashtable_insert in initial_id2_ngap_ids ue_initial_id %u\n",
          ctxt_pP->module_id,
          ue_context_pP->ue_context.ue_initial_id);
  }
Xue Song's avatar
Xue Song committed
412

413 414 415 416 417 418 419
  /* Assume that cause is coded in the same way in RRC and NGap, just check that the value is in NGap range */
  AssertFatal(ue_context_pP->ue_context.establishment_cause < NGAP_RRC_CAUSE_LAST,
              "Establishment cause invalid (%jd/%d) for gNB %d!",
              ue_context_pP->ue_context.establishment_cause,
              NGAP_RRC_CAUSE_LAST,
              ctxt_pP->module_id);
  NGAP_NAS_FIRST_REQ(message_p).establishment_cause = ue_context_pP->ue_context.establishment_cause;
420
  
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  /* Forward NAS message */
  NGAP_NAS_FIRST_REQ(message_p).nas_pdu.buffer = rrcSetupComplete->dedicatedNAS_Message.buf;
  NGAP_NAS_FIRST_REQ(message_p).nas_pdu.length = rrcSetupComplete->dedicatedNAS_Message.size;
  // extract_imsi(NGAP_NAS_FIRST_REQ (message_p).nas_pdu.buffer,
  //              NGAP_NAS_FIRST_REQ (message_p).nas_pdu.length,
  //              ue_context_pP);

  /* Fill UE identities with available information */
  NGAP_NAS_FIRST_REQ(message_p).ue_identity.presenceMask       = NGAP_UE_IDENTITIES_NONE;
  /* Fill s-TMSI */
  NGAP_NAS_FIRST_REQ(message_p).ue_identity.s_tmsi.amf_set_id  = ue_context_pP->ue_context.Initialue_identity_5g_s_TMSI.amf_set_id;
  NGAP_NAS_FIRST_REQ(message_p).ue_identity.s_tmsi.amf_pointer = ue_context_pP->ue_context.Initialue_identity_5g_s_TMSI.amf_pointer;
  NGAP_NAS_FIRST_REQ(message_p).ue_identity.s_tmsi.m_tmsi      = ue_context_pP->ue_context.Initialue_identity_5g_s_TMSI.fiveg_tmsi;

  /* selected_plmn_identity: IE is 1-based, convert to 0-based (C array) */
  int selected_plmn_identity = rrcSetupComplete->selectedPLMN_Identity - 1;
  NGAP_NAS_FIRST_REQ(message_p).selected_plmn_identity = selected_plmn_identity;

  if (rrcSetupComplete->registeredAMF != NULL) {
440 441 442 443 444 445 446 447 448 449 450 451
      NR_RegisteredAMF_t *r_amf = rrcSetupComplete->registeredAMF;
      NGAP_NAS_FIRST_REQ(message_p).ue_identity.presenceMask |= NGAP_UE_IDENTITIES_guami;

      if (r_amf->plmn_Identity != NULL) {
          if ((r_amf->plmn_Identity->mcc != NULL) && (r_amf->plmn_Identity->mcc->list.count > 0)) {
              /* Use first indicated PLMN MCC if it is defined */
              NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.mcc = *r_amf->plmn_Identity->mcc->list.array[selected_plmn_identity];
              LOG_I(NGAP, "[gNB %d] Build NGAP_NAS_FIRST_REQ adding in s_TMSI: GUMMEI MCC %u ue %x\n",
                  ctxt_pP->module_id,
                  NGAP_NAS_FIRST_REQ (message_p).ue_identity.guami.mcc,
                  ue_context_pP->ue_context.rnti);
          }
Xue Song's avatar
Xue Song committed
452

453 454 455 456 457 458 459 460 461 462
          if (r_amf->plmn_Identity->mnc.list.count > 0) {
              /* Use first indicated PLMN MNC if it is defined */
              NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.mnc = *r_amf->plmn_Identity->mnc.list.array[selected_plmn_identity];
              LOG_I(NGAP, "[gNB %d] Build NGAP_NAS_FIRST_REQ adding in s_TMSI: GUMMEI MNC %u ue %x\n",
                  ctxt_pP->module_id,
                  NGAP_NAS_FIRST_REQ (message_p).ue_identity.guami.mnc,
                  ue_context_pP->ue_context.rnti);
          }
      } else {
          /* TODO */
463
      }
Xue Song's avatar
Xue Song committed
464

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
      /* amf_Identifier */
      uint32_t amf_Id = BIT_STRING_to_uint32(&r_amf->amf_Identifier);
      NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.amf_region_id = amf_Id >> 16;
      NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.amf_set_id    = ue_context_pP->ue_context.Initialue_identity_5g_s_TMSI.amf_set_id;
      NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.amf_pointer   = ue_context_pP->ue_context.Initialue_identity_5g_s_TMSI.amf_pointer;

      ue_context_pP->ue_context.ue_guami.mcc = NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.mcc;
      ue_context_pP->ue_context.ue_guami.mnc = NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.mnc;
      ue_context_pP->ue_context.ue_guami.mnc_len = NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.mnc_len;
      ue_context_pP->ue_context.ue_guami.amf_region_id = NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.amf_region_id;
      ue_context_pP->ue_context.ue_guami.amf_set_id = NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.amf_set_id;
      ue_context_pP->ue_context.ue_guami.amf_pointer = NGAP_NAS_FIRST_REQ(message_p).ue_identity.guami.amf_pointer;

      MSC_LOG_TX_MESSAGE(MSC_NGAP_GNB,
                          MSC_NGAP_AMF,
                          (const char *)&message_p->ittiMsg.ngap_nas_first_req,
                          sizeof(ngap_nas_first_req_t),
                          MSC_AS_TIME_FMT" NGAP_NAS_FIRST_REQ gNB %u UE %x",
                          MSC_AS_TIME_ARGS(ctxt_pP),
                          ctxt_pP->module_id,
                          ctxt_pP->rnti);
      LOG_I(NGAP, "[gNB %d] Build NGAP_NAS_FIRST_REQ adding in s_TMSI: GUAMI amf_set_id %u amf_region_id %u ue %x\n",
487
            ctxt_pP->module_id,
488 489
            NGAP_NAS_FIRST_REQ (message_p).ue_identity.guami.amf_set_id,
            NGAP_NAS_FIRST_REQ (message_p).ue_identity.guami.amf_region_id,
490 491 492 493
            ue_context_pP->ue_context.rnti);
  }

  itti_send_msg_to_task (TASK_NGAP, ctxt_pP->instance, message_p);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
}

//------------------------------------------------------------------------------
int
rrc_gNB_process_NGAP_INITIAL_CONTEXT_SETUP_REQ(
    MessageDef *msg_p,
    const char *msg_name,
    instance_t instance
)
//------------------------------------------------------------------------------
{
    uint16_t                        ue_initial_id;
    uint32_t                        gNB_ue_ngap_id;
    rrc_gNB_ue_context_t            *ue_context_p = NULL;
    protocol_ctxt_t                 ctxt;
Xue Song's avatar
Xue Song committed
509
    uint8_t                         pdu_sessions_done = 0;
510 511 512 513
    gtpv1u_gnb_create_tunnel_req_t  create_tunnel_req;
    gtpv1u_gnb_create_tunnel_resp_t create_tunnel_resp;
    uint8_t                         inde_list[NR_NB_RB_MAX - 3]= {0};
    int                             ret = 0;
514

515 516
    ue_initial_id  = NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).ue_initial_id;
    gNB_ue_ngap_id = NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).gNB_ue_ngap_id;
517

518
    ue_context_p   = rrc_gNB_get_ue_context_from_ngap_ids(instance, ue_initial_id, gNB_ue_ngap_id);
519
    LOG_I(NR_RRC, "[gNB %ld] Received %s: ue_initial_id %d, gNB_ue_ngap_id %u \n",
520
        instance, msg_name, ue_initial_id, gNB_ue_ngap_id);
521

522
    if (ue_context_p == NULL) {
523 524 525 526 527 528 529 530
      /* Can not associate this message to an UE index, send a failure to NGAP and discard it! */
      MessageDef *msg_fail_p = NULL;
      LOG_W(NR_RRC, "[gNB %ld] In NGAP_INITIAL_CONTEXT_SETUP_REQ: unknown UE from NGAP ids (%d, %u)\n", instance, ue_initial_id, gNB_ue_ngap_id);
      msg_fail_p = itti_alloc_new_message (TASK_RRC_GNB, 0, NGAP_INITIAL_CONTEXT_SETUP_FAIL);
      NGAP_INITIAL_CONTEXT_SETUP_FAIL (msg_fail_p).gNB_ue_ngap_id = gNB_ue_ngap_id;
      // TODO add failure cause when defined!
      itti_send_msg_to_task (TASK_NGAP, instance, msg_fail_p);
      return (-1);
531
    } else {
532 533 534 535
      PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, GNB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
      ue_context_p->ue_context.gNB_ue_ngap_id = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).gNB_ue_ngap_id;
      ue_context_p->ue_context.amf_ue_ngap_id = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).amf_ue_ngap_id;
      ue_context_p->ue_context.nas_pdu_flag = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).nas_pdu_flag;
536

537 538 539
      uint8_t nb_pdusessions_tosetup = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).nb_of_pdusessions;
      if (nb_pdusessions_tosetup != 0) {
        memset(&create_tunnel_req, 0, sizeof(gtpv1u_gnb_create_tunnel_req_t));
540
        for (int i = 0; i < NR_NB_RB_MAX - 3; i++) {
541
          if(ue_context_p->ue_context.pduSession[i].status >= PDU_SESSION_STATUS_DONE)
542
            continue;
543 544
          ue_context_p->ue_context.pduSession[i].status        = PDU_SESSION_STATUS_NEW;
          ue_context_p->ue_context.pduSession[i].param         = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).pdusession_param[pdu_sessions_done];
545
          create_tunnel_req.pdusession_id[pdu_sessions_done]   = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).pdusession_param[pdu_sessions_done].pdusession_id;
546
          create_tunnel_req.incoming_rb_id[pdu_sessions_done]  = i+1;
547
          create_tunnel_req.upf_NGu_teid[pdu_sessions_done]    = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).pdusession_param[pdu_sessions_done].gtp_teid;
548
          create_tunnel_req.upf_addr[pdu_sessions_done].length = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).pdusession_param[pdu_sessions_done].upf_addr.length;
549
          memcpy(create_tunnel_req.upf_addr[pdu_sessions_done].buffer,
550
                  NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).pdusession_param[pdu_sessions_done].upf_addr.buffer,
551 552 553
                  sizeof(uint8_t)*20);
          LOG_I(NR_RRC, "PDUSESSION SETUP: local index %d teid %u, pdusession id %d \n",
                i,
554 555
                create_tunnel_req.upf_NGu_teid[pdu_sessions_done],
                create_tunnel_req.pdusession_id[pdu_sessions_done]);
556 557 558 559 560 561 562
          inde_list[pdu_sessions_done] = i;
          pdu_sessions_done++;

          if(pdu_sessions_done >= nb_pdusessions_tosetup) {
            break;
          }
        }
563

564 565 566
        ue_context_p->ue_context.nb_of_pdusessions = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).nb_of_pdusessions;
        create_tunnel_req.rnti                     = ue_context_p->ue_context.rnti;
        create_tunnel_req.num_tunnels              = pdu_sessions_done;
567

568 569 570 571 572 573 574 575 576 577 578 579 580
        ret = gtpv1u_create_ngu_tunnel(
                instance,
                &create_tunnel_req,
                &create_tunnel_resp);
        if (ret != 0) {
          LOG_E(NR_RRC,"rrc_gNB_process_NGAP_INITIAL_CONTEXT_SETUP_REQ : gtpv1u_create_ngu_tunnel failed,start to release UE %x\n",ue_context_p->ue_context.rnti);
          ue_context_p->ue_context.ue_release_timer_ng = 1;
          ue_context_p->ue_context.ue_release_timer_thres_ng = 100;
          ue_context_p->ue_context.ue_release_timer = 0;
          ue_context_p->ue_context.ue_reestablishment_timer = 0;
          ue_context_p->ue_context.ul_failure_timer = 20000;
          ue_context_p->ue_context.ul_failure_timer = 0;
          return (0);
Xue Song's avatar
Xue Song committed
581
        }
582 583 584 585 586 587 588

        nr_rrc_gNB_process_GTPV1U_CREATE_TUNNEL_RESP(
          &ctxt,
          &create_tunnel_resp,
          &inde_list[0]);
        ue_context_p->ue_context.setup_pdu_sessions += nb_pdusessions_tosetup;
        ue_context_p->ue_context.established_pdu_sessions_flag = 1;
589
      }
590

591
      /* NAS PDU */
592 593 594 595
      if (NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).nas_pdu_flag == 1) {
        ue_context_p->ue_context.nas_pdu_flag   = NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).nas_pdu_flag;
        ue_context_p->ue_context.nas_pdu.length = NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).nas_pdu.length;
        ue_context_p->ue_context.nas_pdu.buffer = NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).nas_pdu.buffer;
596
      }
Xue Song's avatar
Xue Song committed
597
        
598 599 600
      /* security */
      rrc_gNB_process_security(&ctxt, ue_context_p, &(NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).security_capabilities));
      process_gNB_security_key (
601 602
        &ctxt,
        ue_context_p,
603
        NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).security_key);
604

Cedric Roux's avatar
Cedric Roux committed
605
      /* configure only integrity, ciphering comes after receiving SecurityModeComplete */
606
      nr_rrc_pdcp_config_security(&ctxt, ue_context_p, 0);
607

608
      rrc_gNB_generate_SecurityModeCommand (&ctxt, ue_context_p);
609

610
    // in case, send the S1SP initial context response if it is not sent with the attach complete message
611
    if (ue_context_p->ue_context.StatusRrc == NR_RRC_RECONFIGURED) {
612 613
        LOG_I(NR_RRC, "Sending rrc_gNB_send_NGAP_INITIAL_CONTEXT_SETUP_RESP, cause %ld\n", ue_context_p->ue_context.reestablishment_cause);
        rrc_gNB_send_NGAP_INITIAL_CONTEXT_SETUP_RESP(&ctxt,ue_context_p);
614
    }
615 616 617

    return 0;
  }
618
}
619 620 621 622 623 624 625 626 627 628

//------------------------------------------------------------------------------
void
rrc_gNB_send_NGAP_INITIAL_CONTEXT_SETUP_RESP(
  const protocol_ctxt_t *const ctxt_pP,
  rrc_gNB_ue_context_t          *const ue_context_pP
)
//------------------------------------------------------------------------------
{
  MessageDef      *msg_p         = NULL;
629
  int pdusession;
630 631 632
  int pdu_sessions_done = 0;
  int pdu_sessions_failed = 0;
  int qos_flow_index = 0;
633
  msg_p = itti_alloc_new_message (TASK_RRC_ENB, 0, NGAP_INITIAL_CONTEXT_SETUP_RESP);
634 635
  NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).gNB_ue_ngap_id = ue_context_pP->ue_context.gNB_ue_ngap_id;

636
  for (pdusession = 0; pdusession < ue_context_pP->ue_context.nb_of_pdusessions; pdusession++) {
637
    if (ue_context_pP->ue_context.pduSession[pdusession].status == PDU_SESSION_STATUS_DONE) {
638
      pdu_sessions_done++;
639
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions[pdusession].pdusession_id = ue_context_pP->ue_context.pduSession[pdusession].param.pdusession_id;
640
      // TODO add other information from S1-U when it will be integrated
641 642 643
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions[pdusession].gtp_teid = ue_context_pP->ue_context.gnb_gtp_teid[pdusession];
      memcpy(NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions[pdusession].gNB_addr.buffer , ue_context_pP->ue_context.gnb_gtp_addrs[pdusession].buffer, 20);
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions[pdusession].gNB_addr.length = 4;
644 645 646
      ue_context_pP->ue_context.pduSession[pdusession].status = PDU_SESSION_STATUS_ESTABLISHED;
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions[pdusession].nb_of_qos_flow = ue_context_pP->ue_context.pduSession[pdusession].param.nb_qos;
      for (qos_flow_index = 0; qos_flow_index < ue_context_pP->ue_context.pduSession[pdusession].param.nb_qos; qos_flow_index++) {
647
        NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions[pdusession].associated_qos_flows[qos_flow_index].qfi =
648
            ue_context_pP->ue_context.pduSession[pdusession].param.qos[qos_flow_index].qfi;
649 650
        NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions[pdusession].associated_qos_flows[qos_flow_index].qos_flow_mapping_ind = QOSFLOW_MAPPING_INDICATION_DL;
      }
651
    } else {
652
      pdu_sessions_failed++;
653 654
      ue_context_pP->ue_context.pduSession[pdusession].status = PDU_SESSION_STATUS_FAILED;
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions_failed[pdusession].pdusession_id = ue_context_pP->ue_context.pduSession[pdusession].param.pdusession_id;
655
      // TODO add cause when it will be integrated
656 657
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions_failed[pdusession].cause = NGAP_Cause_PR_radioNetwork;
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions_failed[pdusession].cause_value = NGAP_CauseRadioNetwork_unknown_PDU_session_ID;
658 659 660 661 662 663 664 665 666 667 668 669
    }
  }

  MSC_LOG_TX_MESSAGE(
    MSC_RRC_GNB,
    MSC_S1AP_ENB,
    (const char *)&NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p),
    sizeof(ngap_initial_context_setup_resp_t),
    MSC_AS_TIME_FMT" INITIAL_CONTEXT_SETUP_RESP UE %X eNB_ue_s1ap_id %u e_rabs:%u succ %u fail",
    MSC_AS_TIME_ARGS(ctxt_pP),
    ue_context_pP->ue_id_rnti,
    NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).gNB_ue_ngap_id,
670 671 672
    pdu_sessions_done, pdu_sessions_failed);
  NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).nb_of_pdusessions = pdu_sessions_done;
  NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).nb_of_pdusessions_failed = pdu_sessions_failed;
673 674 675
  itti_send_msg_to_task (TASK_NGAP, ctxt_pP->instance, msg_p);
}

Cedric Roux's avatar
Cedric Roux committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
static NR_CipheringAlgorithm_t rrc_gNB_select_ciphering(
    const protocol_ctxt_t *const ctxt_pP,
    uint16_t algorithms)
{
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt_pP->module_id];
  int i;
  /* preset nea0 as fallback */
  int ret = 0;

  /* Select ciphering algorithm based on gNB configuration file and
   * UE's supported algorithms.
   * We take the first from the list that is supported by the UE.
   * The ordering of the list comes from the configuration file.
   */
  for (i = 0; i < rrc->security.ciphering_algorithms_count; i++) {
    int nea_mask[4] = {
      0,
      NGAP_ENCRYPTION_NEA1_MASK,
      NGAP_ENCRYPTION_NEA2_MASK,
      NGAP_ENCRYPTION_NEA3_MASK
    };
    if (rrc->security.ciphering_algorithms[i] == 0) {
      /* nea0 */
      break;
    }
    if (algorithms & nea_mask[rrc->security.ciphering_algorithms[i]]) {
      ret = rrc->security.ciphering_algorithms[i];
      break;
    }
Xue Song's avatar
Xue Song committed
705 706
  }

Cedric Roux's avatar
Cedric Roux committed
707
  LOG_I(RRC, "selecting ciphering algorithm %d\n", ret);
Xue Song's avatar
Xue Song committed
708

Cedric Roux's avatar
Cedric Roux committed
709
  return ret;
710 711
}

Cedric Roux's avatar
Cedric Roux committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
static e_NR_IntegrityProtAlgorithm rrc_gNB_select_integrity(
    const protocol_ctxt_t *const ctxt_pP,
    uint16_t algorithms)
{
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt_pP->module_id];
  int i;
  /* preset nia0 as fallback */
  int ret = 0;

  /* Select integrity algorithm based on gNB configuration file and
   * UE's supported algorithms.
   * We take the first from the list that is supported by the UE.
   * The ordering of the list comes from the configuration file.
   */
  for (i = 0; i < rrc->security.integrity_algorithms_count; i++) {
    int nia_mask[4] = {
      0,
      NGAP_INTEGRITY_NIA1_MASK,
      NGAP_INTEGRITY_NIA2_MASK,
      NGAP_INTEGRITY_NIA3_MASK
    };
    if (rrc->security.integrity_algorithms[i] == 0) {
      /* nia0 */
      break;
    }
    if (algorithms & nia_mask[rrc->security.integrity_algorithms[i]]) {
      ret = rrc->security.integrity_algorithms[i];
      break;
    }
Xue Song's avatar
Xue Song committed
741 742
  }

Cedric Roux's avatar
Cedric Roux committed
743
  LOG_I(RRC, "selecting integrity algorithm %d\n", ret);
744

Cedric Roux's avatar
Cedric Roux committed
745
  return ret;
746 747 748 749 750 751
}

int
rrc_gNB_process_security(
  const protocol_ctxt_t *const ctxt_pP,
  rrc_gNB_ue_context_t *const ue_context_pP,
752
  ngap_security_capabilities_t *security_capabilities_pP
753 754 755 756 757 758 759
) {
  boolean_t                                             changed = FALSE;
  NR_CipheringAlgorithm_t                               cipheringAlgorithm;
  e_NR_IntegrityProtAlgorithm                           integrityProtAlgorithm;
  /* Save security parameters */
  ue_context_pP->ue_context.security_capabilities = *security_capabilities_pP;
  // translation
rmagueta's avatar
rmagueta committed
760
  LOG_D(NR_RRC,
Xue Song's avatar
Xue Song committed
761
        "[gNB %d] NAS security_capabilities.encryption_algorithms %u AS ciphering_algorithm %lu NAS security_capabilities.integrity_algorithms %u AS integrity_algorithm %u\n",
762
        ctxt_pP->module_id,
763
        ue_context_pP->ue_context.security_capabilities.nRencryption_algorithms,
764
        (unsigned long)ue_context_pP->ue_context.ciphering_algorithm,
765
        ue_context_pP->ue_context.security_capabilities.nRintegrity_algorithms,
766 767
        ue_context_pP->ue_context.integrity_algorithm);
  /* Select relevant algorithms */
Cedric Roux's avatar
Cedric Roux committed
768
  cipheringAlgorithm = rrc_gNB_select_ciphering(ctxt_pP, ue_context_pP->ue_context.security_capabilities.nRencryption_algorithms);
769 770 771 772 773 774

  if (ue_context_pP->ue_context.ciphering_algorithm != cipheringAlgorithm) {
    ue_context_pP->ue_context.ciphering_algorithm = cipheringAlgorithm;
    changed = TRUE;
  }

Cedric Roux's avatar
Cedric Roux committed
775
  integrityProtAlgorithm = rrc_gNB_select_integrity(ctxt_pP, ue_context_pP->ue_context.security_capabilities.nRintegrity_algorithms);
776 777 778 779 780 781

  if (ue_context_pP->ue_context.integrity_algorithm != integrityProtAlgorithm) {
    ue_context_pP->ue_context.integrity_algorithm = integrityProtAlgorithm;
    changed = TRUE;
  }

Xue Song's avatar
Xue Song committed
782
  LOG_I (NR_RRC, "[gNB %d][UE %x] Selected security algorithms (%p): %lx, %x, %s\n",
783 784 785 786 787 788 789 790
         ctxt_pP->module_id,
         ue_context_pP->ue_context.rnti,
         security_capabilities_pP,
         (unsigned long)cipheringAlgorithm,
         integrityProtAlgorithm,
         changed ? "changed" : "same");
  return changed;
}
Xue Song's avatar
Xue Song committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

//------------------------------------------------------------------------------
int
rrc_gNB_process_NGAP_DOWNLINK_NAS(
  MessageDef *msg_p,
  const char *msg_name,
  instance_t  instance,
  mui_t      *rrc_gNB_mui
)
//------------------------------------------------------------------------------
{
    uint16_t ue_initial_id;
    uint32_t gNB_ue_ngap_id;
    uint32_t length;
    uint8_t *buffer;
    struct rrc_gNB_ue_context_s *ue_context_p = NULL;
    protocol_ctxt_t              ctxt;
    memset(&ctxt, 0, sizeof(protocol_ctxt_t));
    ue_initial_id  = NGAP_DOWNLINK_NAS (msg_p).ue_initial_id;
    gNB_ue_ngap_id = NGAP_DOWNLINK_NAS (msg_p).gNB_ue_ngap_id;
    ue_context_p = rrc_gNB_get_ue_context_from_ngap_ids(instance, ue_initial_id, gNB_ue_ngap_id);
812
    LOG_I(NR_RRC, "[gNB %ld] Received %s: ue_initial_id %d, gNB_ue_ngap_id %u\n",
Xue Song's avatar
Xue Song committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
            instance,
            msg_name,
            ue_initial_id,
            gNB_ue_ngap_id);

    if (ue_context_p == NULL) {
        MSC_LOG_RX_MESSAGE(
            MSC_RRC_GNB,
            MSC_NGAP_GNB,
            NULL,
            0,
            MSC_AS_TIME_FMT" DOWNLINK-NAS UE initial id %u gNB_ue_ngap_id %u",
            0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
            ue_initial_id,
            gNB_ue_ngap_id);
        /* Can not associate this message to an UE index, send a failure to NGAP and discard it! */
        MessageDef *msg_fail_p;
830
        LOG_W(NR_RRC, "[gNB %ld] In NGAP_DOWNLINK_NAS: unknown UE from NGAP ids (%d, %u)\n", instance, ue_initial_id, gNB_ue_ngap_id);
831
        msg_fail_p = itti_alloc_new_message (TASK_RRC_GNB, 0, NGAP_NAS_NON_DELIVERY_IND);
Xue Song's avatar
Xue Song committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
        NGAP_NAS_NON_DELIVERY_IND (msg_fail_p).gNB_ue_ngap_id = gNB_ue_ngap_id;
        NGAP_NAS_NON_DELIVERY_IND (msg_fail_p).nas_pdu.length = NGAP_DOWNLINK_NAS (msg_p).nas_pdu.length;
        NGAP_NAS_NON_DELIVERY_IND (msg_fail_p).nas_pdu.buffer = NGAP_DOWNLINK_NAS (msg_p).nas_pdu.buffer;
        // TODO add failure cause when defined!
        MSC_LOG_TX_MESSAGE(
            MSC_RRC_ENB,
            MSC_NGAP_GNB,
            (const char *)NULL,
            0,
            MSC_AS_TIME_FMT" NGAP_NAS_NON_DELIVERY_IND UE initial id %u gNB_ue_ngap_id %u (ue ctxt !found)",
            0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
            ue_initial_id,
            gNB_ue_ngap_id);
        itti_send_msg_to_task (TASK_NGAP, instance, msg_fail_p);
        return (-1);
    } else {
        PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, GNB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);

        /* Is it the first income from NGAP ? */
        if (ue_context_p->ue_context.gNB_ue_ngap_id == 0) {
            ue_context_p->ue_context.gNB_ue_ngap_id = NGAP_DOWNLINK_NAS (msg_p).gNB_ue_ngap_id;
        }

        MSC_LOG_RX_MESSAGE(
            MSC_RRC_GNB,
            MSC_NGAP_GNB,
            (const char *)NULL,
            0,
            MSC_AS_TIME_FMT" DOWNLINK-NAS UE initial id %u gNB_ue_ngap_id %u",
            0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
            ue_initial_id,
            NGAP_DOWNLINK_NAS (msg_p).gNB_ue_ngap_id);
        /* Create message for PDCP (DLInformationTransfer_t) */
        length = do_NR_DLInformationTransfer (
                instance,
                &buffer,
                rrc_gNB_get_next_transaction_identifier (instance),
                NGAP_DOWNLINK_NAS (msg_p).nas_pdu.length,
                NGAP_DOWNLINK_NAS (msg_p).nas_pdu.buffer);
        LOG_DUMPMSG(NR_RRC, DEBUG_RRC, buffer, length, "[MSG] RRC DL Information Transfer\n");
        /*
        * switch UL or DL NAS message without RRC piggybacked to SRB2 if active.
        */
875 876 877 878 879
       switch (RC.nrrrc[ctxt.module_id]->node_type) {
        case ngran_gNB_CU:
          /* Transfer data to PDCP */
          nr_rrc_data_req (
              &ctxt,
880
              ue_context_p->ue_context.Srb2.Active == 1 ? ue_context_p->ue_context.Srb2.Srb_info.Srb_id : ue_context_p->ue_context.Srb1.Srb_info.Srb_id,
881 882 883 884 885 886 887 888 889 890 891 892 893
              (*rrc_gNB_mui)++,
              SDU_CONFIRM_NO,
              length,
              buffer,
              PDCP_TRANSMISSION_MODE_CONTROL);
          break;

        case ngran_gNB_DU:
          // nothing to do for DU
          AssertFatal(1==0,"nothing to do for DU\n");
          break;

        case ngran_gNB:
894
        {
895
          // rrc_mac_config_req_gNB
Xue Song's avatar
Xue Song committed
896 897
#ifdef ITTI_SIM
        uint8_t *message_buffer;
Xue Song's avatar
Xue Song committed
898
        message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM, length);
Xue Song's avatar
Xue Song committed
899
        memcpy (message_buffer, buffer, length);
900
        MessageDef *message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_DCCH_DATA_IND);
Xue Song's avatar
Xue Song committed
901 902 903 904 905 906
        GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
        GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
        GNB_RRC_DCCH_DATA_IND (message_p).size  = length;
        itti_send_msg_to_task (TASK_RRC_UE_SIM, instance, message_p);
        LOG_I(NR_RRC, "Send DL NAS message \n");
#else
907 908 909
          /* Transfer data to PDCP */
          nr_rrc_data_req (
              &ctxt,
910
              ue_context_p->ue_context.Srb2.Active == 1 ? ue_context_p->ue_context.Srb2.Srb_info.Srb_id : ue_context_p->ue_context.Srb1.Srb_info.Srb_id,
911 912 913 914 915
              (*rrc_gNB_mui)++,
              SDU_CONFIRM_NO,
              length,
              buffer,
              PDCP_TRANSMISSION_MODE_CONTROL);
Xue Song's avatar
Xue Song committed
916
#endif
917
        }
918 919 920 921 922
          break;

        default :
            LOG_W(NR_RRC, "Unknown node type %d\n", RC.nrrrc[ctxt.module_id]->node_type);
      }
Xue Song's avatar
Xue Song committed
923 924 925
        return (0);
    }
}
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

//------------------------------------------------------------------------------
void
rrc_gNB_send_NGAP_UPLINK_NAS(
  const protocol_ctxt_t    *const ctxt_pP,
  rrc_gNB_ue_context_t     *const ue_context_pP,
  NR_UL_DCCH_Message_t     *const ul_dcch_msg
)
//------------------------------------------------------------------------------
{
    uint32_t pdu_length;
    uint8_t *pdu_buffer;
    MessageDef *msg_p;
    NR_ULInformationTransfer_t *ulInformationTransfer = ul_dcch_msg->message.choice.c1->choice.ulInformationTransfer;

    if (ulInformationTransfer->criticalExtensions.present == NR_ULInformationTransfer__criticalExtensions_PR_ulInformationTransfer) {
        pdu_length = ulInformationTransfer->criticalExtensions.choice.ulInformationTransfer->dedicatedNAS_Message->size;
        pdu_buffer = ulInformationTransfer->criticalExtensions.choice.ulInformationTransfer->dedicatedNAS_Message->buf;
944
        msg_p = itti_alloc_new_message (TASK_RRC_GNB, 0, NGAP_UPLINK_NAS);
945 946 947 948 949 950 951 952 953 954
        NGAP_UPLINK_NAS (msg_p).gNB_ue_ngap_id = ue_context_pP->ue_context.gNB_ue_ngap_id;
        NGAP_UPLINK_NAS (msg_p).nas_pdu.length = pdu_length;
        NGAP_UPLINK_NAS (msg_p).nas_pdu.buffer = pdu_buffer;
        // extract_imsi(NGAP_UPLINK_NAS (msg_p).nas_pdu.buffer,
        //               NGAP_UPLINK_NAS (msg_p).nas_pdu.length,
        //               ue_context_pP);
        itti_send_msg_to_task (TASK_NGAP, ctxt_pP->instance, msg_p);
        LOG_I(NR_RRC,"Send RRC GNB UL Information Transfer \n");
    }
}
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970

//------------------------------------------------------------------------------
void
rrc_gNB_send_NGAP_PDUSESSION_SETUP_RESP(
  const protocol_ctxt_t    *const ctxt_pP,
  rrc_gNB_ue_context_t     *const ue_context_pP,
  uint8_t                   xid
)
//------------------------------------------------------------------------------
{
  MessageDef *msg_p;
  int pdu_sessions_done = 0;
  int pdu_sessions_failed = 0;
  int pdusession;
  int qos_flow_index;

971
  msg_p = itti_alloc_new_message (TASK_RRC_GNB, 0, NGAP_PDUSESSION_SETUP_RESP);
972 973 974 975
  NGAP_PDUSESSION_SETUP_RESP(msg_p).gNB_ue_ngap_id = ue_context_pP->ue_context.gNB_ue_ngap_id;

  for (pdusession = 0; pdusession < ue_context_pP->ue_context.setup_pdu_sessions; pdusession++) {
    // if (xid == ue_context_pP->ue_context.pdusession[pdusession].xid) {
976 977
      if (ue_context_pP->ue_context.pduSession[pdusession].status == PDU_SESSION_STATUS_DONE) {
        NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].pdusession_id = ue_context_pP->ue_context.pduSession[pdusession].param.pdusession_id;
978
        // NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].pdusession_id = 1;
979
        NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].nb_of_qos_flow = ue_context_pP->ue_context.pduSession[pdusession].param.nb_qos;
980 981 982 983 984 985 986
        NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].gtp_teid = ue_context_pP->ue_context.gnb_gtp_teid[pdusession];
        NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].gNB_addr.pdu_session_type = PDUSessionType_ipv4;
        NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].gNB_addr.length = ue_context_pP->ue_context.gnb_gtp_addrs[pdusession].length;
        memcpy(NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].gNB_addr.buffer,
                ue_context_pP->ue_context.gnb_gtp_addrs[pdusession].buffer, sizeof(uint8_t)*20);
        for (qos_flow_index = 0; qos_flow_index < NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].nb_of_qos_flow; qos_flow_index++) {
          NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].associated_qos_flows[qos_flow_index].qfi =
987
            ue_context_pP->ue_context.pduSession[pdusession].param.qos[qos_flow_index].qfi;
988 989 990
          NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].associated_qos_flows[qos_flow_index].qos_flow_mapping_ind = QOSFLOW_MAPPING_INDICATION_DL;
        }

991
        ue_context_pP->ue_context.pduSession[pdusession].status = PDU_SESSION_STATUS_ESTABLISHED;
992
        LOG_I (NR_RRC,"gnb_gtp_addr (msg index %d, pdu_sessions index %d, status %d, xid %d): nb_of_pdusessions %d,  pdusession_id %d, teid: %u, addr: %d.%d.%d.%d \n ",
993
               pdu_sessions_done, pdusession, ue_context_pP->ue_context.pduSession[pdusession].status, xid,
994 995 996 997 998 999 1000 1001
               ue_context_pP->ue_context.nb_of_pdusessions,
               NGAP_PDUSESSION_SETUP_RESP (msg_p).pdusessions[pdu_sessions_done].pdusession_id,
               NGAP_PDUSESSION_SETUP_RESP (msg_p).pdusessions[pdu_sessions_done].gtp_teid,
               NGAP_PDUSESSION_SETUP_RESP (msg_p).pdusessions[pdu_sessions_done].gNB_addr.buffer[0],
               NGAP_PDUSESSION_SETUP_RESP (msg_p).pdusessions[pdu_sessions_done].gNB_addr.buffer[1],
               NGAP_PDUSESSION_SETUP_RESP (msg_p).pdusessions[pdu_sessions_done].gNB_addr.buffer[2],
               NGAP_PDUSESSION_SETUP_RESP (msg_p).pdusessions[pdu_sessions_done].gNB_addr.buffer[3]);
        pdu_sessions_done++;
1002 1003
      } else if ((ue_context_pP->ue_context.pduSession[pdusession].status == PDU_SESSION_STATUS_NEW) ||
                 (ue_context_pP->ue_context.pduSession[pdusession].status == PDU_SESSION_STATUS_ESTABLISHED)) {
1004 1005
        LOG_D (NR_RRC,"PDU-SESSION is NEW or already ESTABLISHED\n");
      } else { /* to be improved */
1006 1007
        ue_context_pP->ue_context.pduSession[pdusession].status = PDU_SESSION_STATUS_FAILED;
        NGAP_PDUSESSION_SETUP_RESP (msg_p).pdusessions_failed[pdu_sessions_failed].pdusession_id = ue_context_pP->ue_context.pduSession[pdusession].param.pdusession_id;
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        pdu_sessions_failed++;
        // TODO add cause when it will be integrated
      }
        NGAP_PDUSESSION_SETUP_RESP(msg_p).nb_of_pdusessions = pdu_sessions_done;
        NGAP_PDUSESSION_SETUP_RESP(msg_p).nb_of_pdusessions_failed = pdu_sessions_failed;
    // } else {
    //   LOG_D(NR_RRC,"xid does not corresponds  (context pdu_sessions index %d, status %d, xid %d/%d) \n ",
    //         pdusession, ue_context_pP->ue_context.pdusession[pdusession].status, xid, ue_context_pP->ue_context.pdusession[pdusession].xid);
    // }
  }

  if ((pdu_sessions_done > 0) ) {
    LOG_I(NR_RRC,"NGAP_PDUSESSION_SETUP_RESP: sending the message: nb_of_pdusessions %d, total pdu_sessions %d, index %d\n",
          ue_context_pP->ue_context.nb_of_pdusessions, ue_context_pP->ue_context.setup_pdu_sessions, pdusession);
    MSC_LOG_TX_MESSAGE(
      MSC_RRC_GNB,
      MSC_NGAP_GNB,
      (const char *)&NGAP_PDUSESSION_SETUP_RESP (msg_p),
      sizeof(ngap_pdusession_setup_resp_t),
      MSC_AS_TIME_FMT" PDUSESSION_SETUP_RESP UE %X gNB_ue_ngap_id %u pdu_sessions:%u succ %u fail",
      MSC_AS_TIME_ARGS(ctxt_pP),
      ue_context_pP->ue_id_rnti,
      NGAP_PDUSESSION_SETUP_RESP (msg_p).gNB_ue_ngap_id,
      pdu_sessions_done, pdu_sessions_failed);
    itti_send_msg_to_task (TASK_NGAP, ctxt_pP->instance, msg_p);
  }

  for(int i = 0; i < NB_RB_MAX; i++) {
1036
    ue_context_pP->ue_context.pduSession[i].xid = -1;
1037 1038
  }

yaojie's avatar
yaojie committed
1039
  return;
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
}

//------------------------------------------------------------------------------
int
rrc_gNB_process_NGAP_PDUSESSION_SETUP_REQ(
  MessageDef *msg_p,
  const char *msg_name,
  instance_t instance
)
//------------------------------------------------------------------------------
{
  uint16_t                        ue_initial_id;
  uint32_t                        gNB_ue_ngap_id;
  rrc_gNB_ue_context_t            *ue_context_p = NULL;
  protocol_ctxt_t                 ctxt;
  gtpv1u_gnb_create_tunnel_req_t  create_tunnel_req;
  gtpv1u_gnb_create_tunnel_resp_t create_tunnel_resp;
  uint8_t                         pdu_sessions_done;
  uint8_t                         inde_list[NR_NB_RB_MAX - 3]= {0};
  int                             ret = 0;

  ue_initial_id  = NGAP_PDUSESSION_SETUP_REQ(msg_p).ue_initial_id;
  gNB_ue_ngap_id = NGAP_PDUSESSION_SETUP_REQ(msg_p).gNB_ue_ngap_id;
  ue_context_p   = rrc_gNB_get_ue_context_from_ngap_ids(instance, ue_initial_id, gNB_ue_ngap_id);
1064
  LOG_I(NR_RRC, "[gNB %ld] Received %s: ue_initial_id %d, gNB_ue_ngap_id %u \n",
1065 1066 1067 1068
    instance, msg_name, ue_initial_id, gNB_ue_ngap_id);

  if (ue_context_p == NULL) {
    MessageDef *msg_fail_p = NULL;
1069
    LOG_W(NR_RRC, "[gNB %ld] In NGAP_PDUSESSION_SETUP_REQ: unknown UE from NGAP ids (%d, %u)\n", instance, ue_initial_id, gNB_ue_ngap_id);
1070
    msg_fail_p = itti_alloc_new_message(TASK_RRC_GNB, 0, NGAP_PDUSESSION_SETUP_REQUEST_FAIL);
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    NGAP_PDUSESSION_SETUP_REQ(msg_fail_p).gNB_ue_ngap_id = gNB_ue_ngap_id;
    // TODO add failure cause when defined!
    itti_send_msg_to_task (TASK_NGAP, instance, msg_fail_p);
    return (-1);
  } else {
    memset(&create_tunnel_req, 0, sizeof(gtpv1u_gnb_create_tunnel_req_t));
    uint8_t nb_pdusessions_tosetup = NGAP_PDUSESSION_SETUP_REQ(msg_p).nb_pdusessions_tosetup;
    pdu_sessions_done = 0;

    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, GNB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
    for (int i = 0; i < NR_NB_RB_MAX - 3; i++) {
1082
      if(ue_context_p->ue_context.pduSession[i].status >= PDU_SESSION_STATUS_DONE)
1083
        continue;
1084 1085
      ue_context_p->ue_context.pduSession[i].status      = PDU_SESSION_STATUS_NEW;
      ue_context_p->ue_context.pduSession[i].param       = NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[pdu_sessions_done];
1086
      create_tunnel_req.pdusession_id[pdu_sessions_done] = NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[pdu_sessions_done].pdusession_id;
1087
      create_tunnel_req.incoming_rb_id[pdu_sessions_done]= i+1;
1088 1089
      create_tunnel_req.upf_NGu_teid[pdu_sessions_done]  = NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[pdu_sessions_done].gtp_teid;
      memcpy(create_tunnel_req.upf_addr[pdu_sessions_done].buffer,
1090
              NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[pdu_sessions_done].upf_addr.buffer,
1091
              sizeof(uint8_t)*20);
1092
      create_tunnel_req.upf_addr[pdu_sessions_done].length = NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[pdu_sessions_done].upf_addr.length;
1093 1094
      LOG_I(NR_RRC,"NGAP PDUSESSION SETUP REQ: local index %d teid %u, pdusession id %d \n",
            i,
1095 1096
            create_tunnel_req.upf_NGu_teid[pdu_sessions_done],
            create_tunnel_req.pdusession_id[pdu_sessions_done]);
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
      inde_list[pdu_sessions_done] = i;
      pdu_sessions_done++;

      if(pdu_sessions_done >= nb_pdusessions_tosetup) {
        break;
      }
    }

    ue_context_p->ue_context.nb_of_pdusessions = NGAP_PDUSESSION_SETUP_REQ(msg_p).nb_pdusessions_tosetup;
    ue_context_p->ue_context.gNB_ue_ngap_id    = NGAP_PDUSESSION_SETUP_REQ(msg_p).gNB_ue_ngap_id;
    ue_context_p->ue_context.amf_ue_ngap_id    = NGAP_PDUSESSION_SETUP_REQ(msg_p).amf_ue_ngap_id;
    create_tunnel_req.rnti                     = ue_context_p->ue_context.rnti;
    create_tunnel_req.num_tunnels              = pdu_sessions_done;

    ret = gtpv1u_create_ngu_tunnel(
            instance,
            &create_tunnel_req,
            &create_tunnel_resp);
    if (ret != 0) {
      LOG_E(NR_RRC,"rrc_gNB_process_NGAP_PDUSESSION_SETUP_REQ : gtpv1u_create_ngu_tunnel failed,start to release UE %x\n",ue_context_p->ue_context.rnti);
      ue_context_p->ue_context.ue_release_timer_ng = 1;
      ue_context_p->ue_context.ue_release_timer_thres_ng = 100;
      ue_context_p->ue_context.ue_release_timer = 0;
      ue_context_p->ue_context.ue_reestablishment_timer = 0;
      ue_context_p->ue_context.ul_failure_timer = 20000; // set ul_failure to 20000 for triggering rrc_eNB_send_S1AP_UE_CONTEXT_RELEASE_REQ
      // rrc_gNB_free_UE(ctxt.module_id,ue_context_p);
      ue_context_p->ue_context.ul_failure_timer = 0;
      return (0);
    }
    nr_rrc_gNB_process_GTPV1U_CREATE_TUNNEL_RESP(
      &ctxt,
      &create_tunnel_resp,
      &inde_list[0]);
    ue_context_p->ue_context.setup_pdu_sessions += nb_pdusessions_tosetup;

    // TEST 
1133 1134 1135
    // ue_context_p->ue_context.pdusession[0].status = PDU_SESSION_STATUS_DONE;
    // rrc_gNB_send_NGAP_PDUSESSION_SETUP_RESP(&ctxt, ue_context_p, 0);
    rrc_gNB_generate_dedicatedRRCReconfiguration(&ctxt, ue_context_p);
1136 1137
    return(0);
  }
yaojie's avatar
yaojie committed
1138
}
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

void
rrc_gNB_send_NGAP_UE_CONTEXT_RELEASE_REQ(
  const module_id_t gnb_mod_idP,
  const rrc_gNB_ue_context_t *const ue_context_pP,
  const ngap_Cause_t causeP,
  const long cause_valueP)
//------------------------------------------------------------------------------
{
  if (ue_context_pP == NULL) {
    LOG_E(RRC, "[gNB] In NGAP_UE_CONTEXT_RELEASE_REQ: invalid UE\n");
  } else {
    MSC_LOG_TX_MESSAGE(MSC_RRC_GNB,
                       MSC_NGAP_GNB,
                       NULL,
                       0,
                       "0 NGAP_UE_CONTEXT_RELEASE_REQ gNB_ue_ngap_id 0x%06"PRIX32" ",
                       ue_context_pP->ue_context.gNB_ue_ngap_id);
    MessageDef *msg_context_release_req_p = NULL;
1158
    msg_context_release_req_p = itti_alloc_new_message(TASK_RRC_GNB, 0, NGAP_UE_CONTEXT_RELEASE_REQ);
1159 1160 1161 1162 1163
    NGAP_UE_CONTEXT_RELEASE_REQ(msg_context_release_req_p).gNB_ue_ngap_id    = ue_context_pP->ue_context.gNB_ue_ngap_id;
    NGAP_UE_CONTEXT_RELEASE_REQ(msg_context_release_req_p).cause             = causeP;
    NGAP_UE_CONTEXT_RELEASE_REQ(msg_context_release_req_p).cause_value       = cause_valueP;
    NGAP_UE_CONTEXT_RELEASE_REQ(msg_context_release_req_p).nb_of_pdusessions = ue_context_pP->ue_context.setup_pdu_sessions;
    for (int pdusession = 0; pdusession < ue_context_pP->ue_context.setup_pdu_sessions; pdusession++) {
1164
      NGAP_UE_CONTEXT_RELEASE_REQ(msg_context_release_req_p).pdusessions[pdusession].pdusession_id = ue_context_pP->ue_context.pduSession[pdusession].param.pdusession_id;
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
    }
    itti_send_msg_to_task(TASK_NGAP, GNB_MODULE_ID_TO_INSTANCE(gnb_mod_idP), msg_context_release_req_p);
  }
}
/*------------------------------------------------------------------------------*/
int 
rrc_gNB_process_NGAP_UE_CONTEXT_RELEASE_REQ (
  MessageDef *msg_p, 
  const char *msg_name, 
  instance_t instance) 
{
  uint32_t gNB_ue_ngap_id;
  struct rrc_gNB_ue_context_s *ue_context_p = NULL;
  gNB_ue_ngap_id = NGAP_UE_CONTEXT_RELEASE_REQ(msg_p).gNB_ue_ngap_id;
  ue_context_p   = rrc_gNB_get_ue_context_from_ngap_ids(instance, UE_INITIAL_ID_INVALID, gNB_ue_ngap_id);

  if (ue_context_p == NULL) {
    /* Can not associate this message to an UE index, send a failure to ngAP and discard it! */
    MessageDef *msg_fail_p;
1184
    LOG_W(RRC, "[gNB %ld] In NGAP_UE_CONTEXT_RELEASE_REQ: unknown UE from gNB_ue_ngap_id (%u)\n",
1185 1186
          instance,
          gNB_ue_ngap_id);
1187
    msg_fail_p = itti_alloc_new_message(TASK_RRC_GNB, 0, NGAP_UE_CONTEXT_RELEASE_RESP); /* TODO change message ID. */
1188 1189 1190 1191 1192 1193 1194 1195 1196
    NGAP_UE_CONTEXT_RELEASE_RESP(msg_fail_p).gNB_ue_ngap_id = gNB_ue_ngap_id;
    // TODO add failure cause when defined!
    itti_send_msg_to_task(TASK_NGAP, instance, msg_fail_p);
    return (-1);
  } else {
    /* TODO release context. */
    /* Send the response */
    {
      MessageDef *msg_resp_p;
1197
      msg_resp_p = itti_alloc_new_message(TASK_RRC_GNB, 0, NGAP_UE_CONTEXT_RELEASE_RESP);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
      NGAP_UE_CONTEXT_RELEASE_RESP(msg_resp_p).gNB_ue_ngap_id = gNB_ue_ngap_id;
      itti_send_msg_to_task(TASK_NGAP, instance, msg_resp_p);
    }
    return (0);
  }
}

//-----------------------------------------------------------------------------
/*
* Process the NG command NGAP_UE_CONTEXT_RELEASE_COMMAND, sent by AMF.
* The gNB should remove all pdu session, NG context, and other context of the UE.
*/
int
rrc_gNB_process_NGAP_UE_CONTEXT_RELEASE_COMMAND(
  MessageDef *msg_p,
  const char *msg_name,
  instance_t instance) {
  //-----------------------------------------------------------------------------
  uint32_t gNB_ue_ngap_id = 0;
  protocol_ctxt_t ctxt;
  struct rrc_gNB_ue_context_s *ue_context_p = NULL;
  struct rrc_ue_ngap_ids_s *rrc_ue_ngap_ids = NULL;
  gNB_ue_ngap_id = NGAP_UE_CONTEXT_RELEASE_COMMAND(msg_p).gNB_ue_ngap_id;
  ue_context_p = rrc_gNB_get_ue_context_from_ngap_ids(instance, UE_INITIAL_ID_INVALID, gNB_ue_ngap_id);

  if (ue_context_p == NULL) {
    /* Can not associate this message to an UE index */
    MessageDef *msg_complete_p = NULL;
1226
    LOG_W(NR_RRC, "[gNB %ld] In NGAP_UE_CONTEXT_RELEASE_COMMAND: unknown UE from gNB_ue_ngap_id (%u)\n",
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
          instance,
          gNB_ue_ngap_id);
    MSC_LOG_EVENT(MSC_RRC_GNB, "0 NGAP_UE_CONTEXT_RELEASE_COMPLETE gNB_ue_ngap_id 0x%06"PRIX32" context not found",
                  gNB_ue_ngap_id);
    MSC_LOG_TX_MESSAGE(MSC_RRC_GNB,
                       MSC_NGAP_GNB,
                       NULL,
                       0,
                       "0 NGAP_UE_CONTEXT_RELEASE_COMPLETE gNB_ue_ngap_id 0x%06"PRIX32" ",
                       gNB_ue_ngap_id);
1237
    msg_complete_p = itti_alloc_new_message(TASK_RRC_GNB, 0, NGAP_UE_CONTEXT_RELEASE_COMPLETE);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    NGAP_UE_CONTEXT_RELEASE_COMPLETE(msg_complete_p).gNB_ue_ngap_id = gNB_ue_ngap_id;
    itti_send_msg_to_task(TASK_NGAP, instance, msg_complete_p);
    rrc_ue_ngap_ids = rrc_gNB_NGAP_get_ue_ids(RC.nrrrc[instance], UE_INITIAL_ID_INVALID, gNB_ue_ngap_id);

    if (rrc_ue_ngap_ids != NULL) {
      rrc_gNB_NGAP_remove_ue_ids(RC.nrrrc[instance], rrc_ue_ngap_ids);
    }

    return -1;
  } else {
    ue_context_p->ue_context.ue_release_timer_ng = 0;
1249
    ue_context_p->ue_context.ue_release_timer_thres_rrc = 1000;
1250
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, GNB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
1251
    ctxt.eNB_index = 0;
1252 1253 1254 1255
    rrc_gNB_generate_RRCRelease(&ctxt, ue_context_p);
    return 0;
  }
}
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267

void rrc_gNB_send_NGAP_UE_CONTEXT_RELEASE_COMPLETE(
  instance_t instance,
  uint32_t   gNB_ue_ngap_id) {
  MSC_LOG_TX_MESSAGE(MSC_RRC_GNB, MSC_NGAP_GNB, NULL, 0,
                     "0 NGAP_UE_CONTEXT_RELEASE_COMPLETE gNB_ue_ngap_id 0x%06"PRIX32" ",
                     gNB_ue_ngap_id);
  MessageDef *msg = itti_alloc_new_message(TASK_RRC_GNB, 0, NGAP_UE_CONTEXT_RELEASE_COMPLETE);
  NGAP_UE_CONTEXT_RELEASE_COMPLETE(msg).gNB_ue_ngap_id = gNB_ue_ngap_id;
  itti_send_msg_to_task(TASK_NGAP, instance, msg);
}

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
//------------------------------------------------------------------------------
/*
* Remove UE ids (ue_initial_id and ng_id) from hashtables.
*/
void
rrc_gNB_NGAP_remove_ue_ids(
  gNB_RRC_INST *const rrc_instance_pP,
  struct rrc_ue_ngap_ids_s *const ue_ids_pP
)
//------------------------------------------------------------------------------
{
  hashtable_rc_t h_rc;

  if (rrc_instance_pP == NULL) {
    LOG_E(NR_RRC, "Bad NR RRC instance\n");
    return;
  }

  if (ue_ids_pP == NULL) {
    LOG_E(NR_RRC, "Trying to free a NULL NGAP UE IDs\n");
    return;
  }

  const uint16_t ue_initial_id  = ue_ids_pP->ue_initial_id;
  const uint32_t gNB_ue_ngap_id = ue_ids_pP->gNB_ue_ngap_id;

  if (gNB_ue_ngap_id > 0) {
    h_rc = hashtable_remove(rrc_instance_pP->ngap_id2_ngap_ids, (hash_key_t)gNB_ue_ngap_id);

    if (h_rc != HASH_TABLE_OK) {
      LOG_W(NR_RRC, "NGAP Did not find entry in hashtable ngap_id2_ngap_ids for gNB_ue_ngap_id %u\n", gNB_ue_ngap_id);
    } else {
      LOG_W(NR_RRC, "NGAP removed entry in hashtable ngap_id2_ngap_ids for gNB_ue_ngap_id %u\n", gNB_ue_ngap_id);
    }
  }

  if (ue_initial_id != UE_INITIAL_ID_INVALID) {
    h_rc = hashtable_remove(rrc_instance_pP->initial_id2_ngap_ids, (hash_key_t)ue_initial_id);

    if (h_rc != HASH_TABLE_OK) {
      LOG_W(NR_RRC, "NGAP Did not find entry in hashtable initial_id2_ngap_ids for ue_initial_id %u\n", ue_initial_id);
    } else {
      LOG_W(NR_RRC, "NGAP removed entry in hashtable initial_id2_ngap_ids for ue_initial_id %u\n", ue_initial_id);
    }
  }
}
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
void
rrc_gNB_send_NGAP_UE_CAPABILITIES_IND(
  const protocol_ctxt_t    *const ctxt_pP,
  rrc_gNB_ue_context_t     *const ue_context_pP,
  NR_UL_DCCH_Message_t     *const ul_dcch_msg
)
//------------------------------------------------------------------------------
{
    NR_UE_CapabilityRAT_ContainerList_t *ueCapabilityRATContainerList = ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList;
    /* 4096 is arbitrary, should be big enough */
    unsigned char buf[4096];
    unsigned char *buf2;
    NR_UERadioAccessCapabilityInformation_t rac;
    
    if (ueCapabilityRATContainerList->list.count == 0) {
      LOG_W(RRC, "[gNB %d][UE %x] bad UE capabilities\n", ctxt_pP->module_id, ue_context_pP->ue_context.rnti);
    }
    
    asn_enc_rval_t ret = uper_encode_to_buffer(&asn_DEF_NR_UE_CapabilityRAT_ContainerList, NULL, ueCapabilityRATContainerList, buf, 4096);
    
    if (ret.encoded == -1) abort();
    
    memset(&rac, 0, sizeof(NR_UERadioAccessCapabilityInformation_t));
    rac.criticalExtensions.present = NR_UERadioAccessCapabilityInformation__criticalExtensions_PR_c1;
    rac.criticalExtensions.choice.c1 = calloc(1,sizeof(*rac.criticalExtensions.choice.c1));
    rac.criticalExtensions.choice.c1->present = NR_UERadioAccessCapabilityInformation__criticalExtensions__c1_PR_ueRadioAccessCapabilityInformation;
    rac.criticalExtensions.choice.c1->choice.ueRadioAccessCapabilityInformation = calloc(1,sizeof(NR_UERadioAccessCapabilityInformation_IEs_t));
    rac.criticalExtensions.choice.c1->choice.ueRadioAccessCapabilityInformation->ue_RadioAccessCapabilityInfo.buf = buf;
    rac.criticalExtensions.choice.c1->choice.ueRadioAccessCapabilityInformation->ue_RadioAccessCapabilityInfo.size = (ret.encoded+7)/8;
    rac.criticalExtensions.choice.c1->choice.ueRadioAccessCapabilityInformation->nonCriticalExtension = NULL;
    /* 8192 is arbitrary, should be big enough */
    buf2 = malloc16(8192);
    
    if (buf2 == NULL) abort();
    
    ret = uper_encode_to_buffer(&asn_DEF_NR_UERadioAccessCapabilityInformation, NULL, &rac, buf2, 8192);
    
    if (ret.encoded == -1) abort();

    MessageDef *msg_p;
1354
    msg_p = itti_alloc_new_message (TASK_RRC_GNB, 0, NGAP_UE_CAPABILITIES_IND);
1355 1356 1357 1358 1359 1360 1361
    NGAP_UE_CAPABILITIES_IND (msg_p).gNB_ue_ngap_id = ue_context_pP->ue_context.gNB_ue_ngap_id;
    NGAP_UE_CAPABILITIES_IND (msg_p).ue_radio_cap.length = (ret.encoded+7)/8;
    NGAP_UE_CAPABILITIES_IND (msg_p).ue_radio_cap.buffer = buf2;
    itti_send_msg_to_task (TASK_NGAP, ctxt_pP->instance, msg_p);
    LOG_I(NR_RRC,"Send message to ngap: NGAP_UE_CAPABILITIES_IND\n");
}

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
//------------------------------------------------------------------------------
void
rrc_gNB_send_NGAP_PDUSESSION_RELEASE_RESPONSE(
  const protocol_ctxt_t    *const ctxt_pP,
  rrc_gNB_ue_context_t     *const ue_context_pP,
  uint8_t                   xid
)
//------------------------------------------------------------------------------
{
  int pdu_sessions_released = 0;
  MessageDef   *msg_p;
1373
  msg_p = itti_alloc_new_message (TASK_RRC_GNB, 0, NGAP_PDUSESSION_RELEASE_RESPONSE);
1374 1375 1376
  NGAP_PDUSESSION_RELEASE_RESPONSE (msg_p).gNB_ue_ngap_id = ue_context_pP->ue_context.gNB_ue_ngap_id;

  for (int i = 0;  i < NB_RB_MAX; i++) {
1377
    if (xid == ue_context_pP->ue_context.pduSession[i].xid) {
1378
      NGAP_PDUSESSION_RELEASE_RESPONSE (msg_p).pdusession_release[pdu_sessions_released].pdusession_id =
1379
          ue_context_pP->ue_context.pduSession[i].param.pdusession_id;
1380 1381
      pdu_sessions_released++;
      //clear
1382
      memset(&ue_context_pP->ue_context.pduSession[i], 0, sizeof(pdu_session_param_t));
1383 1384 1385 1386 1387 1388 1389 1390
    }
  }

  NGAP_PDUSESSION_RELEASE_RESPONSE (msg_p).nb_of_pdusessions_released = pdu_sessions_released;
  NGAP_PDUSESSION_RELEASE_RESPONSE (msg_p).nb_of_pdusessions_failed = ue_context_pP->ue_context.nb_release_of_pdusessions;
  memcpy(&(NGAP_PDUSESSION_RELEASE_RESPONSE (msg_p).pdusessions_failed[0]), &ue_context_pP->ue_context.pdusessions_release_failed[0],
      sizeof(pdusession_failed_t)*ue_context_pP->ue_context.nb_release_of_pdusessions);
  ue_context_pP->ue_context.setup_pdu_sessions -= pdu_sessions_released;
1391
  LOG_I(NR_RRC,"NGAP PDUSESSION RELEASE RESPONSE: GNB_UE_NGAP_ID %u release_pdu_sessions %d setup_pdu_sessions %d \n",
1392 1393 1394 1395 1396 1397
        NGAP_PDUSESSION_RELEASE_RESPONSE (msg_p).gNB_ue_ngap_id,
        pdu_sessions_released, ue_context_pP->ue_context.setup_pdu_sessions);
  itti_send_msg_to_task (TASK_NGAP, ctxt_pP->instance, msg_p);

  //clear xid
  for(int i = 0; i < NB_RB_MAX; i++) {
1398
    ue_context_pP->ue_context.pduSession[i].xid = -1;
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
  }

  //clear release pdusessions
  ue_context_pP->ue_context.nb_release_of_pdusessions = 0;
  memset(&ue_context_pP->ue_context.pdusessions_release_failed[0], 0, sizeof(pdusession_failed_t)*NGAP_MAX_PDUSESSION);
}

//------------------------------------------------------------------------------
int
rrc_gNB_process_NGAP_PDUSESSION_RELEASE_COMMAND(
  MessageDef *msg_p,
  const char *msg_name,
  instance_t instance
)
//------------------------------------------------------------------------------
{
  uint32_t                        gNB_ue_ngap_id;
  rrc_gNB_ue_context_t           *ue_context_p = NULL;
  protocol_ctxt_t                 ctxt;
  pdusession_release_t            pdusession_release_params[NGAP_MAX_PDUSESSION];
  uint8_t                         nb_pdusessions_torelease;
  MessageDef                     *msg_delete_tunnels_p = NULL;
  uint8_t xid;
  int i, pdusession;
  uint8_t b_existed,is_existed;
1424
  uint8_t pdusession_release_drb = 0;
1425 1426 1427 1428 1429 1430 1431 1432 1433

  memcpy(&pdusession_release_params[0], &(NGAP_PDUSESSION_RELEASE_COMMAND (msg_p).pdusession_release_params[0]),
    sizeof(pdusession_release_t)*NGAP_MAX_PDUSESSION);
  gNB_ue_ngap_id = NGAP_PDUSESSION_RELEASE_COMMAND(msg_p).gNB_ue_ngap_id;
  nb_pdusessions_torelease = NGAP_PDUSESSION_RELEASE_COMMAND(msg_p).nb_pdusessions_torelease;
  if (nb_pdusessions_torelease > NGAP_MAX_PDUSESSION) {
    return -1;
  }
  ue_context_p   = rrc_gNB_get_ue_context_from_ngap_ids(instance, UE_INITIAL_ID_INVALID, gNB_ue_ngap_id);
1434
  LOG_I(NR_RRC, "[gNB %ld] Received %s: gNB_ue_ngap_id %u \n", instance, msg_name, gNB_ue_ngap_id);
1435 1436 1437 1438

  if (ue_context_p != NULL) {
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, GNB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
    xid = rrc_gNB_get_next_transaction_identifier(ctxt.module_id);
1439
    LOG_I(NR_RRC,"PDU Session Release Command: AMF_UE_NGAP_ID %lu  GNB_UE_NGAP_ID %u release_pdusessions %d \n",
1440
          NGAP_PDUSESSION_RELEASE_COMMAND (msg_p).amf_ue_ngap_id&0x000000FFFFFFFFFF, gNB_ue_ngap_id, nb_pdusessions_torelease);
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458

    for (pdusession = 0; pdusession < nb_pdusessions_torelease; pdusession++) {
      b_existed = 0;
      is_existed = 0;

      for (i = pdusession-1; i >= 0; i--) {
        if (pdusession_release_params[pdusession].pdusession_id == pdusession_release_params[i].pdusession_id) {
          is_existed = 1;
          break;
        }
      }

      if(is_existed == 1) {
        // pdusession_id is existed
        continue;
      }

      for (i = 0;  i < NR_NB_RB_MAX; i++) {
1459
        if (pdusession_release_params[pdusession].pdusession_id == ue_context_p->ue_context.pduSession[i].param.pdusession_id) {
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
          b_existed = 1;
          break;
        }
      }

      if(b_existed == 0) {
        // no pdusession_id
        LOG_I(NR_RRC, "no pdusession_id \n");
        ue_context_p->ue_context.pdusessions_release_failed[ue_context_p->ue_context.nb_release_of_pdusessions].pdusession_id = pdusession_release_params[pdusession].pdusession_id;
        ue_context_p->ue_context.pdusessions_release_failed[ue_context_p->ue_context.nb_release_of_pdusessions].cause = NGAP_CAUSE_RADIO_NETWORK;
        ue_context_p->ue_context.pdusessions_release_failed[ue_context_p->ue_context.nb_release_of_pdusessions].cause_value = 30;
        ue_context_p->ue_context.nb_release_of_pdusessions++;
      } else {
1473 1474
        if(ue_context_p->ue_context.pduSession[i].status == PDU_SESSION_STATUS_FAILED) {
          ue_context_p->ue_context.pduSession[i].xid = xid;
1475
          continue;
1476 1477 1478 1479
        } else if(ue_context_p->ue_context.pduSession[i].status == PDU_SESSION_STATUS_ESTABLISHED) {
          LOG_I(NR_RRC, "RELEASE pdusession %d \n", ue_context_p->ue_context.pduSession[i].param.pdusession_id);
          ue_context_p->ue_context.pduSession[i].status = PDU_SESSION_STATUS_TORELEASE;
          ue_context_p->ue_context.pduSession[i].xid = xid;
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
          pdusession_release_drb++;
        } else {
          // pdusession_id status NG
          ue_context_p->ue_context.pdusessions_release_failed[ue_context_p->ue_context.nb_release_of_pdusessions].pdusession_id = pdusession_release_params[pdusession].pdusession_id;
          ue_context_p->ue_context.pdusessions_release_failed[ue_context_p->ue_context.nb_release_of_pdusessions].cause = NGAP_CAUSE_RADIO_NETWORK;
          ue_context_p->ue_context.pdusessions_release_failed[ue_context_p->ue_context.nb_release_of_pdusessions].cause_value = 0;
          ue_context_p->ue_context.nb_release_of_pdusessions++;
        }
      }
    }

    if(pdusession_release_drb > 0) {
      //TODO RRCReconfiguration To UE
      LOG_I(NR_RRC, "Send RRCReconfiguration To UE \n");
      rrc_gNB_generate_dedicatedRRCReconfiguration_release(&ctxt, ue_context_p, xid, NGAP_PDUSESSION_RELEASE_COMMAND (msg_p).nas_pdu.length, NGAP_PDUSESSION_RELEASE_COMMAND (msg_p).nas_pdu.buffer);
    } else {
      //gtp tunnel delete
      LOG_I(NR_RRC, "gtp tunnel delete \n");
1498
      msg_delete_tunnels_p = itti_alloc_new_message(TASK_RRC_GNB, 0, GTPV1U_GNB_DELETE_TUNNEL_REQ);
1499 1500 1501 1502
      memset(&GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p), 0, sizeof(GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p)));
      GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).rnti = ue_context_p->ue_context.rnti;

      for(i = 0; i < NB_RB_MAX; i++) {
1503
        if(xid == ue_context_p->ue_context.pduSession[i].xid) {
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
          GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).pdusession_id[GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).num_pdusession++] = ue_context_p->ue_context.gnb_gtp_psi[i];
          ue_context_p->ue_context.gnb_gtp_teid[i] = 0;
          memset(&ue_context_p->ue_context.gnb_gtp_addrs[i], 0, sizeof(ue_context_p->ue_context.gnb_gtp_addrs[i]));
          ue_context_p->ue_context.gnb_gtp_psi[i]  = 0;
        }
      }

      itti_send_msg_to_task(TASK_GTPV1_U, instance, msg_delete_tunnels_p);
      //NGAP_PDUSESSION_RELEASE_RESPONSE
      rrc_gNB_send_NGAP_PDUSESSION_RELEASE_RESPONSE(&ctxt, ue_context_p, xid);
      LOG_I(NR_RRC, "Send PDU Session Release Response \n");
    }
  } else {
1517
    LOG_E(NR_RRC, "PDU Session Release Command: AMF_UE_NGAP_ID %lu  GNB_UE_NGAP_ID %u  Error ue_context_p NULL \n",
1518
          NGAP_PDUSESSION_RELEASE_COMMAND (msg_p).amf_ue_ngap_id&0x000000FFFFFFFFFF, NGAP_PDUSESSION_RELEASE_COMMAND(msg_p).gNB_ue_ngap_id);
1519 1520 1521 1522 1523
    return -1;
  }

  return 0;
}
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533

void nr_rrc_rx_tx(void) {
  // check timers

  // check if UEs are lost, to remove them from upper layers

  //

}