rrc_gNB_NGAP.c 44.4 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"
Xue Song's avatar
Xue Song committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

extern RAN_CONTEXT_t RC;

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

/*! \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
)
//------------------------------------------------------------------------------
{
    static uint16_t ue_initial_id[NUMBER_OF_gNB_MAX];
    ue_initial_id[mod_id]++;

    /* 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]++;
    }

    return ue_initial_id[mod_id];
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100
//------------------------------------------------------------------------------
/*
* 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;
101 102 103 104 105 106 107 108 109 110 111
    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);
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    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
147
      } // end if if (gNB_ue_ngap_idP > 0)
148 149 150 151 152
    } 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
153 154 155 156 157
      /*
      * 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.
      */
158 159
    } // end else (h_rc != HASH_TABLE_OK)
  } else { // end if (ue_initial_id != UE_INITIAL_ID_INVALID)
Xue Song's avatar
Xue Song committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    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 {
          LOG_E(NGAP, "[gNB instance %d] Couldn't find the gNB NGAP context\n",
                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);

            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)
225
  } // end else (ue_initial_id == UE_INITIAL_ID_INVALID)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

    return result;
}

//------------------------------------------------------------------------------
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
) 
//------------------------------------------------------------------------------
{
    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);

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

    return NULL;
}

Xue Song's avatar
Xue Song committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
//------------------------------------------------------------------------------
void
nr_rrc_pdcp_config_security(
    const protocol_ctxt_t  *const ctxt_pP,
    rrc_gNB_ue_context_t   *const ue_context_pP,
    const uint8_t          send_security_mode_command
)
//------------------------------------------------------------------------------
{
    NR_SRB_ToAddModList_t              *SRB_configList = ue_context_pP->ue_context.SRB_configList;
    uint8_t                            *kRRCenc = NULL;
    uint8_t                            *kRRCint = NULL;
    uint8_t                            *kUPenc = NULL;
    pdcp_t                             *pdcp_p   = NULL;
    static int                          print_keys= 1;
    hashtable_rc_t                      h_rc;
    hash_key_t                          key;

267
#ifndef PHYSIM
Xue Song's avatar
Xue Song committed
268 269 270 271 272 273 274 275 276 277 278 279 280
    /* Derive the keys from kgnb */
    if (SRB_configList != NULL) {
        derive_key_up_enc(ue_context_pP->ue_context.ciphering_algorithm,
                        ue_context_pP->ue_context.kgnb,
                        &kUPenc);
    }

    derive_key_rrc_enc(ue_context_pP->ue_context.ciphering_algorithm,
                        ue_context_pP->ue_context.kgnb,
                        &kRRCenc);
    derive_key_rrc_int(ue_context_pP->ue_context.integrity_algorithm,
                        ue_context_pP->ue_context.kgnb,
                        &kRRCint);
281
#endif
Xue Song's avatar
Xue Song committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    if (!IS_SOFTMODEM_IQPLAYER) {
        SET_LOG_DUMP(DEBUG_SECURITY) ;
    }


    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:" );
        LOG_DUMPMSG(NR_RRC, DEBUG_SECURITY, kRRCenc, 32,"\nKRRCenc:" );
        LOG_DUMPMSG(NR_RRC, DEBUG_SECURITY, kRRCint, 32,"\nKRRCint:" );
        }
    }

    key = PDCP_COLL_KEY_VALUE(ctxt_pP->module_id, ctxt_pP->rnti, ctxt_pP->enb_flag, DCCH, SRB_FLAG_YES);
    h_rc = hashtable_get(pdcp_coll_p, key, (void **)&pdcp_p);

    if (h_rc == HASH_TABLE_OK) {
        pdcp_config_set_security(
        ctxt_pP,
        pdcp_p,
        DCCH,
        DCCH+2,
        (send_security_mode_command == TRUE)  ?
        0 | (ue_context_pP->ue_context.integrity_algorithm << 4) :
        (ue_context_pP->ue_context.ciphering_algorithm )         |
        (ue_context_pP->ue_context.integrity_algorithm << 4),
        kRRCenc,
        kRRCint,
        kUPenc);
    } else {
        LOG_E(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_UE_FMT"Could not get PDCP instance for SRB DCCH %u\n",
            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
            DCCH);
    }
}

Xue Song's avatar
Xue Song committed
320 321 322 323 324 325 326 327 328 329 330 331
//------------------------------------------------------------------------------
/*
* 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
)
//------------------------------------------------------------------------------
{
Xue Song's avatar
Xue Song committed
332
    // gNB_RRC_INST *rrc = RC.nrrrc[ctxt_pP->module_id];
Xue Song's avatar
Xue Song committed
333 334
    MessageDef         *message_p         = NULL;
    rrc_ue_ngap_ids_t  *rrc_ue_ngap_ids_p = NULL;
335
    hashtable_rc_t      h_rc;
Xue Song's avatar
Xue Song committed
336 337 338 339 340 341 342 343 344 345

    message_p = itti_alloc_new_message(TASK_RRC_GNB, NGAP_NAS_FIRST_REQ);
    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;

346 347 348
    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);
Xue Song's avatar
Xue Song committed
349

350 351 352 353 354
    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
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

    /* 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;
    
    /* 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) {
        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);
            }

            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 */
        }

        /* 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",
              ctxt_pP->module_id,
              NGAP_NAS_FIRST_REQ (message_p).ue_identity.guami.amf_set_id,
              NGAP_NAS_FIRST_REQ (message_p).ue_identity.guami.amf_region_id,
              ue_context_pP->ue_context.rnti);
    }

    itti_send_msg_to_task (TASK_NGAP, ctxt_pP->instance, message_p);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
}

//------------------------------------------------------------------------------
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;

    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;

    ue_context_p   = rrc_gNB_get_ue_context_from_ngap_ids(instance, ue_initial_id, gNB_ue_ngap_id);
    LOG_I(NR_RRC, "[gNB %d] Received %s: ue_initial_id %d, gNB_ue_ngap_id %d \n",
        instance, msg_name, ue_initial_id, 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 = NULL;
Xue Song's avatar
Xue Song committed
463
        LOG_W(NR_RRC, "[gNB %d] In NGAP_INITIAL_CONTEXT_SETUP_REQ: unknown UE from NGAP ids (%d, %d)\n", instance, ue_initial_id, gNB_ue_ngap_id);
464 465 466 467 468 469 470 471 472
        msg_fail_p = itti_alloc_new_message (TASK_RRC_GNB, 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);
    } else {
        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;
heshanyun's avatar
heshanyun committed
473
        ue_context_p->ue_context.nas_pdu_flag = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).nas_pdu_flag;
474
        
475 476 477 478 479 480 481 482 483 484 485 486 487
        ue_context_p->ue_context.nb_of_pdusessions = NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).nb_of_pdusessions;
        for (int i = 0; i < ue_context_p->ue_context.nb_of_pdusessions; i++) {
            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[i];
            //create_tunnel_req.eps_bearer_id[i]       = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).e_rab_param[i].e_rab_id;
            //create_tunnel_req.sgw_S1u_teid[i]        = NGAP_INITIAL_CONTEXT_SETUP_REQ (msg_p).e_rab_param[i].gtp_teid;
            //memcpy(&create_tunnel_req.sgw_addr[i],
            //       &S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).e_rab_param[i].sgw_addr,
            //       sizeof(transport_layer_addr_t));
            //inde_list[create_tunnel_req.num_tunnels]= i;
            //create_tunnel_req.num_tunnels++;
        } 

Xue Song's avatar
Xue Song committed
488
        /* NAS PDU */
489
        ue_context_p->ue_context.nas_pdu_flag = NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).nas_pdu_flag;
Xue Song's avatar
Xue Song committed
490 491 492 493 494
        if (ue_context_p->ue_context.nas_pdu_flag == 1) {
            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;
        }
        
495
        /* TODO security */
496 497
        rrc_gNB_process_security(&ctxt, ue_context_p, &(NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p).security_capabilities));
        
498 499
        uint8_t send_security_mode_command = TRUE;

Xue Song's avatar
Xue Song committed
500
        nr_rrc_pdcp_config_security(
501 502 503
            &ctxt,
            ue_context_p,
            send_security_mode_command);
504 505 506 507 508

        if (send_security_mode_command) {
            rrc_gNB_generate_SecurityModeCommand (&ctxt, ue_context_p);
            send_security_mode_command = FALSE;

Xue Song's avatar
Xue Song committed
509
            nr_rrc_pdcp_config_security(
510 511 512
                &ctxt,
                ue_context_p,
                send_security_mode_command);
513
        } else {
514 515
            /* rrc_gNB_generate_UECapabilityEnquiry */
            rrc_gNB_generate_UECapabilityEnquiry(&ctxt, ue_context_p);
516 517 518 519 520
        }

        // in case, send the S1SP initial context response if it is not sent with the attach complete message
        if (ue_context_p->ue_context.Status == NR_RRC_RECONFIGURED) {
            LOG_I(NR_RRC, "Sending rrc_gNB_send_NGAP_INITIAL_CONTEXT_SETUP_RESP, cause %ld\n", ue_context_p->ue_context.reestablishment_cause);
521
            rrc_gNB_send_NGAP_INITIAL_CONTEXT_SETUP_RESP(&ctxt,ue_context_p);
522 523 524 525 526
        }

        return 0;
    }
}
527 528 529 530 531 532 533 534 535 536

//------------------------------------------------------------------------------
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;
537
  int pdusession;
538 539 540 541 542
  int e_rabs_done = 0;
  int e_rabs_failed = 0;
  msg_p = itti_alloc_new_message (TASK_RRC_ENB, NGAP_INITIAL_CONTEXT_SETUP_RESP);
  NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).gNB_ue_ngap_id = ue_context_pP->ue_context.gNB_ue_ngap_id;

543 544
  for (pdusession = 0; pdusession < ue_context_pP->ue_context.nb_of_pdusessions; pdusession++) {
    if (ue_context_pP->ue_context.pdusession[pdusession].status == E_RAB_STATUS_DONE) {
545
      e_rabs_done++;
546
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions[pdusession].pdusession_id = ue_context_pP->ue_context.pdusession[pdusession].param.pdusession_id;
547
      // TODO add other information from S1-U when it will be integrated
548 549 550 551
      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;
      ue_context_pP->ue_context.pdusession[pdusession].status = E_RAB_STATUS_ESTABLISHED;
552 553
    } else {
      e_rabs_failed++;
554 555
      ue_context_pP->ue_context.pdusession[pdusession].status = E_RAB_STATUS_FAILED;
      NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).pdusessions_failed[pdusession].pdusession_id = ue_context_pP->ue_context.pdusession[pdusession].param.pdusession_id;
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
      // TODO add cause when it will be integrated
    }
  }

  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,
    e_rabs_done, e_rabs_failed);
  NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).nb_of_pdusessions = e_rabs_done;
  NGAP_INITIAL_CONTEXT_SETUP_RESP (msg_p).nb_of_pdusessions_failed = e_rabs_failed;
  itti_send_msg_to_task (TASK_NGAP, ctxt_pP->instance, msg_p);
}

static NR_CipheringAlgorithm_t rrc_gNB_select_ciphering(uint16_t algorithms) {
  //#warning "Forced   return NR_CipheringAlgorithm_nea0, to be deleted in future"
  return NR_CipheringAlgorithm_nea0;
}

static e_NR_IntegrityProtAlgorithm rrc_gNB_select_integrity(uint16_t algorithms) {

  // to be continue
  return NR_IntegrityProtAlgorithm_nia0;
}

int
rrc_gNB_process_security(
  const protocol_ctxt_t *const ctxt_pP,
  rrc_gNB_ue_context_t *const ue_context_pP,
590
  ngap_security_capabilities_t *security_capabilities_pP
591 592 593 594 595 596 597 598 599 600
) {
  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
  LOG_D(NR_RRC,
        "[eNB %d] NAS security_capabilities.encryption_algorithms %u AS ciphering_algorithm %lu NAS security_capabilities.integrity_algorithms %u AS integrity_algorithm %u\n",
        ctxt_pP->module_id,
601
        ue_context_pP->ue_context.security_capabilities.nRencryption_algorithms,
602
        (unsigned long)ue_context_pP->ue_context.ciphering_algorithm,
603
        ue_context_pP->ue_context.security_capabilities.nRintegrity_algorithms,
604 605
        ue_context_pP->ue_context.integrity_algorithm);
  /* Select relevant algorithms */
606
  cipheringAlgorithm = rrc_gNB_select_ciphering (ue_context_pP->ue_context.security_capabilities.nRencryption_algorithms);
607 608 609 610 611 612

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

613
  integrityProtAlgorithm = rrc_gNB_select_integrity (ue_context_pP->ue_context.security_capabilities.nRintegrity_algorithms);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628

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

  LOG_I (NR_RRC, "[eNB %d][UE %x] Selected security algorithms (%p): %lx, %x, %s\n",
         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
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 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 705 706 707 708 709 710 711 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

//------------------------------------------------------------------------------
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);
    LOG_I(NR_RRC, "[gNB %d] Received %s: ue_initial_id %d, gNB_ue_ngap_id %d\n",
            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;
        LOG_W(NR_RRC, "[gNB %d] In NGAP_DOWNLINK_NAS: unknown UE from NGAP ids (%d, %d)\n", instance, ue_initial_id, gNB_ue_ngap_id);
        msg_fail_p = itti_alloc_new_message (TASK_RRC_GNB, NGAP_NAS_NON_DELIVERY_IND);
        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.
        */
#ifdef ITTI_SIM
        MessageDef *message_p;
        uint8_t *message_buffer;
        message_buffer = itti_malloc (TASK_RRC_GNB_SIM, TASK_RRC_UE_SIM, length);
        memcpy (message_buffer, buffer, length);
        message_p = itti_alloc_new_message (TASK_RRC_GNB_SIM, GNB_RRC_DCCH_DATA_IND);
        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
        /* Transfer data to PDCP */
        nr_rrc_data_req (
            &ctxt,
            ue_context_p->ue_context.Srb2.Srb_info.Srb_id,
            (*rrc_gNB_mui)++,
            SDU_CONFIRM_NO,
            length,
            buffer,
            PDCP_TRANSMISSION_MODE_CONTROL);
#endif
        return (0);
    }
}
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766

//------------------------------------------------------------------------------
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;
        msg_p = itti_alloc_new_message (TASK_RRC_GNB, NGAP_UPLINK_NAS);
        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");
    }
}
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

//------------------------------------------------------------------------------
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;

  msg_p = itti_alloc_new_message (TASK_RRC_GNB, NGAP_PDUSESSION_SETUP_RESP);
  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) {
      if (ue_context_pP->ue_context.pdusession[pdusession].status == NR_PDU_SESSION_STATUS_DONE) {
        NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].pdusession_id = ue_context_pP->ue_context.pdusession[pdusession].param.pdusession_id;
        // NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].pdusession_id = 1;
        NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].nb_of_qos_flow = ue_context_pP->ue_context.pdusession[pdusession].param.nb_qos;
        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 =
            ue_context_pP->ue_context.pdusession[pdusession].param.qos[qos_flow_index].qfi;
          NGAP_PDUSESSION_SETUP_RESP(msg_p).pdusessions[pdusession].associated_qos_flows[qos_flow_index].qos_flow_mapping_ind = QOSFLOW_MAPPING_INDICATION_DL;
        }

        ue_context_pP->ue_context.pdusession[pdusession].status = NR_PDU_SESSION_STATUS_ESTABLISHED;
        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 ",
               pdu_sessions_done, pdusession, ue_context_pP->ue_context.pdusession[pdusession].status, xid,
               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++;
      } else if ((ue_context_pP->ue_context.pdusession[pdusession].status == NR_PDU_SESSION_STATUS_NEW) ||
                 (ue_context_pP->ue_context.pdusession[pdusession].status == NR_PDU_SESSION_STATUS_ESTABLISHED)) {
        LOG_D (NR_RRC,"PDU-SESSION is NEW or already ESTABLISHED\n");
      } else { /* to be improved */
        ue_context_pP->ue_context.pdusession[pdusession].status = NR_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;
        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++) {
    ue_context_pP->ue_context.pdusession[i].xid = -1;
  }

yaojie's avatar
yaojie committed
851
  return;
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
}

//------------------------------------------------------------------------------
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);
  LOG_I(NR_RRC, "[gNB %d] Received %s: ue_initial_id %d, gNB_ue_ngap_id %d \n",
    instance, msg_name, ue_initial_id, gNB_ue_ngap_id);

  if (ue_context_p == NULL) {
    MessageDef *msg_fail_p = NULL;
    LOG_W(NR_RRC, "[gNB %d] In NGAP_PDUSESSION_SETUP_REQ: unknown UE from NGAP ids (%d, %d)\n", instance, ue_initial_id, gNB_ue_ngap_id);
    msg_fail_p = itti_alloc_new_message(TASK_RRC_GNB, NGAP_PDUSESSION_SETUP_REQUEST_FAIL);
    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++) {
      if(ue_context_p->ue_context.pdusession[i].status >= NR_PDU_SESSION_STATUS_DONE)
        continue;
      ue_context_p->ue_context.pdusession[i].status      = NR_PDU_SESSION_STATUS_NEW;
      ue_context_p->ue_context.pdusession[i].param       = NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[pdu_sessions_done];
      create_tunnel_req.pdusession_id[pdu_sessions_done] = NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[pdu_sessions_done].pdusession_id;
      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,
              NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[i].upf_addr.buffer,
              sizeof(uint8_t)*20);
      create_tunnel_req.upf_addr[pdu_sessions_done].length = NGAP_PDUSESSION_SETUP_REQ(msg_p).pdusession_setup_params[i].upf_addr.length;
      LOG_I(NR_RRC,"NGAP PDUSESSION SETUP REQ: local index %d teid %u, pdusession id %d \n",
            i,
            create_tunnel_req.upf_NGu_teid[i],
            create_tunnel_req.pdusession_id[i]);
      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 
    ue_context_p->ue_context.pdusession[0].status = NR_PDU_SESSION_STATUS_DONE;
    rrc_gNB_send_NGAP_PDUSESSION_SETUP_RESP(&ctxt, ue_context_p, 0);
    return(0);
  }
yaojie's avatar
yaojie committed
948
}