rrc_gNB_nsa.c 24.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * 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
 */

/*! \file rrc_gNB_nsa.c
 * \brief rrc NSA procedures for gNB
 * \author Raymond Knopp
 * \date 2019
 * \version 1.0
 * \company Eurecom
 * \email: raymond.knopp@eurecom.fr
 */

31
#include "nr_rrc_defs.h"
32
#include "NR_RRCReconfiguration.h"
33
#include "NR_UE-NR-Capability.h"
34 35
//#include "NR_UE-CapabilityRAT-ContainerList.h"
#include "LTE_UE-CapabilityRAT-ContainerList.h"
Raymond Knopp's avatar
Raymond Knopp committed
36
#include "NR_CG-Config.h"
37
//#include "NR_SRB-ToAddModList.h"
Francesco Mani's avatar
Francesco Mani committed
38
#include "openair2/LAYER2/NR_MAC_gNB/mac_proto.h"
39
#include "openair2/RRC/LTE/rrc_eNB_GTPV1U.h"
40
#include "executables/softmodem-common.h"
41
#include "executables/nr-softmodem.h"
laurent's avatar
laurent committed
42
#include <openair2/RRC/NR/rrc_gNB_UE_context.h>
Laurent Thomas's avatar
Laurent Thomas committed
43
#include <openair3/ocp-gtpu/gtp_itf.h>
44
#include "UTIL/OSA/osa_defs.h"
45

46 47 48 49 50 51 52 53
extern boolean_t nr_rrc_pdcp_config_asn1_req(
    const protocol_ctxt_t *const  ctxt_pP,
    NR_SRB_ToAddModList_t  *const srb2add_list,
    NR_DRB_ToAddModList_t  *const drb2add_list,
    NR_DRB_ToReleaseList_t *const drb2release_list,
    const uint8_t                   security_modeP,
    uint8_t                  *const kRRCenc,
    uint8_t                  *const kRRCint,
54 55
    uint8_t                  *const kUPenc,
    uint8_t                  *const kUPint
56 57 58 59 60 61 62 63 64
    ,LTE_PMCH_InfoList_r9_t  *pmch_InfoList_r9
    ,rb_id_t                 *const defaultDRB,
    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);

extern rlc_op_status_t nr_rrc_rlc_config_asn1_req (const protocol_ctxt_t   * const ctxt_pP,
    const NR_SRB_ToAddModList_t   * const srb2add_listP,
    const NR_DRB_ToAddModList_t   * const drb2add_listP,
    const NR_DRB_ToReleaseList_t  * const drb2release_listP,
    const LTE_PMCH_InfoList_r9_t * const pmch_InfoList_r9_pP,
rmagueta's avatar
rmagueta committed
65
    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);
66

67
void rrc_parse_ue_capabilities(gNB_RRC_INST *rrc, NR_UE_CapabilityRAT_ContainerList_t *UE_CapabilityRAT_ContainerList, x2ap_ENDC_sgnb_addition_req_t *m, NR_CG_ConfigInfo_IEs_t  *cg_config_info) {
68
  struct rrc_gNB_ue_context_s        *ue_context_p = NULL;
69

laurent's avatar
laurent committed
70 71
  OCTET_STRING_t *ueCapabilityRAT_Container_nr=NULL;
  OCTET_STRING_t *ueCapabilityRAT_Container_MRDC=NULL;
Cedric Roux's avatar
Cedric Roux committed
72
  asn_dec_rval_t dec_rval;
laurent's avatar
laurent committed
73
  int list_size=0;
74

Raymond Knopp's avatar
Raymond Knopp committed
75
  AssertFatal(UE_CapabilityRAT_ContainerList!=NULL,"UE_CapabilityRAT_ContainerList is null\n");
76
  AssertFatal((list_size=UE_CapabilityRAT_ContainerList->list.count) >= 2, "UE_CapabilityRAT_ContainerList->list.size %d < 2\n",UE_CapabilityRAT_ContainerList->list.count);
77 78

  for (int i=0; i<list_size; i++) {
79 80
    if (UE_CapabilityRAT_ContainerList->list.array[i]->rat_Type == NR_RAT_Type_nr) ueCapabilityRAT_Container_nr = &UE_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container;
    else if (UE_CapabilityRAT_ContainerList->list.array[i]->rat_Type == NR_RAT_Type_eutra_nr) ueCapabilityRAT_Container_MRDC = &UE_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container;
81
  }
82 83

  // decode and store capabilities
Raymond Knopp's avatar
Raymond Knopp committed
84
  ue_context_p = rrc_gNB_allocate_new_UE_context(rrc);
Cedric Roux's avatar
Cedric Roux committed
85 86 87 88 89 90 91 92 93 94

  if (ueCapabilityRAT_Container_nr != NULL) {
    dec_rval = uper_decode(NULL,
                           &asn_DEF_NR_UE_NR_Capability,
                           (void **)&ue_context_p->ue_context.UE_Capability_nr,
                           ueCapabilityRAT_Container_nr->buf,
                           ueCapabilityRAT_Container_nr->size, 0, 0);

    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
      LOG_E(RRC, "Failed to decode UE NR capabilities (%zu bytes) container size %lu\n", dec_rval.consumed,ueCapabilityRAT_Container_nr->size);
95 96 97 98
      ASN_STRUCT_FREE(asn_DEF_NR_UE_NR_Capability,
                      ue_context_p->ue_context.UE_Capability_nr);
      ue_context_p->ue_context.UE_Capability_nr = 0;
      AssertFatal(1==0,"exiting\n");
Cedric Roux's avatar
Cedric Roux committed
99
    }
100
  }
101

Cedric Roux's avatar
Cedric Roux committed
102 103 104 105 106 107 108 109
  if (ueCapabilityRAT_Container_MRDC != NULL) {
    dec_rval = uper_decode(NULL,
                           &asn_DEF_NR_UE_MRDC_Capability,
                           (void **)&ue_context_p->ue_context.UE_Capability_MRDC,
                           ueCapabilityRAT_Container_MRDC->buf,
                           ueCapabilityRAT_Container_MRDC->size, 0, 0);

    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
110 111 112 113 114
      LOG_E(RRC, "Failed to decode UE MRDC capabilities (%zu bytes)\n", dec_rval.consumed);
      ASN_STRUCT_FREE(asn_DEF_NR_UE_MRDC_Capability,
                      ue_context_p->ue_context.UE_Capability_MRDC);
      ue_context_p->ue_context.UE_Capability_MRDC = 0;
      AssertFatal(1==0,"exiting\n");
Cedric Roux's avatar
Cedric Roux committed
115
    }
116 117 118 119
  }

  // dump ue_capabilities

Cedric Roux's avatar
Cedric Roux committed
120
  if ( LOG_DEBUGFLAG(DEBUG_ASN1) && ueCapabilityRAT_Container_nr != NULL ) {
121 122
    xer_fprint(stdout, &asn_DEF_NR_UE_NR_Capability, ue_context_p->ue_context.UE_Capability_nr);
  }
123

Cedric Roux's avatar
Cedric Roux committed
124
  if ( LOG_DEBUGFLAG(DEBUG_ASN1) && ueCapabilityRAT_Container_MRDC != NULL ) {
125 126
    xer_fprint(stdout, &asn_DEF_NR_UE_MRDC_Capability, ue_context_p->ue_context.UE_Capability_MRDC);
  }
127
  LOG_A(NR_RRC, "Successfully decoded UE NR capabilities (NR and MRDC)\n");
128

129
  rrc_add_nsa_user(rrc,ue_context_p, m);
130 131
}

Cedric Roux's avatar
Cedric Roux committed
132 133 134 135
/* generate prototypes for the tree management functions (RB_INSERT used in rrc_add_nsa_user) */
RB_PROTOTYPE(rrc_nr_ue_tree_s, rrc_gNB_ue_context_s, entries,
             rrc_gNB_compare_ue_rnti_id);

136
void rrc_add_nsa_user(gNB_RRC_INST *rrc,struct rrc_gNB_ue_context_s *ue_context_p, x2ap_ENDC_sgnb_addition_req_t *m) {
137
  // generate nr-Config-r15 containers for LTE RRC : inside message for X2 EN-DC (CG-Config Message from 38.331)
138
  rrc_gNB_carrier_data_t *carrier=&rrc->carrier;
139
  MessageDef *msg;
140
  msg = itti_alloc_new_message(TASK_RRC_ENB, 0, X2AP_ENDC_SGNB_ADDITION_REQ_ACK);
141 142
  gtpv1u_enb_create_tunnel_req_t  create_tunnel_req;
  gtpv1u_enb_create_tunnel_resp_t create_tunnel_resp;
Laurent's avatar
Laurent committed
143
  protocol_ctxt_t ctxt={0};
144
  unsigned char *kUPenc = NULL;
Cedric Roux's avatar
Cedric Roux committed
145
  unsigned char *kUPint = NULL;
146
  int i;
147 148 149 150 151

  // In case of phy-test and do-ra mode, read UE capabilities directly from file
  if (get_softmodem_params()->phy_test == 1 || get_softmodem_params()->do_ra == 1) {
    NR_UE_NR_Capability_t* UE_Capability_nr = NULL;
    char UE_NR_Capability_xer[65536];
152
    FILE *f = NULL;
francescomani's avatar
francescomani committed
153 154
    if (uecap_file)
      f = fopen(uecap_file, "r");
155 156 157
    if(f){
      size_t size = fread(UE_NR_Capability_xer, 1, sizeof UE_NR_Capability_xer, f);
      if (size == 0 || size == sizeof UE_NR_Capability_xer)
francescomani's avatar
francescomani committed
158
        LOG_E(NR_RRC,"UE Capabilities XER file %s is too large (%ld)\n", uecap_file, size);
159 160
      else {
        UE_Capability_nr = CALLOC(1,sizeof(NR_UE_NR_Capability_t));
francescomani's avatar
francescomani committed
161
        asn_dec_rval_t dec_rval = xer_decode(0, &asn_DEF_NR_UE_NR_Capability, (void *)&UE_Capability_nr, UE_NR_Capability_xer, size);
162
        assert(dec_rval.code == RC_OK);
francescomani's avatar
francescomani committed
163
        xer_fprint(stdout,&asn_DEF_NR_UE_NR_Capability,(void *)UE_Capability_nr);
164 165
      }
    }
166 167
    else
      LOG_E(NR_RRC,"Could not open UE Capabilities input file. Not handling OAI UE Capabilities.\n");
168 169 170
    ue_context_p->ue_context.UE_Capability_nr = UE_Capability_nr;
  }

171
  // NR RRCReconfiguration
172
  AssertFatal(rrc->Nb_ue < MAX_NR_RRC_UE_CONTEXTS,"cannot add another UE\n");
Raymond Knopp's avatar
Raymond Knopp committed
173 174
  ue_context_p->ue_context.reconfig = calloc(1,sizeof(NR_RRCReconfiguration_t));
  ue_context_p->ue_context.secondaryCellGroup = calloc(1,sizeof(NR_CellGroupConfig_t));
175
  memset((void *)ue_context_p->ue_context.reconfig,0,sizeof(NR_RRCReconfiguration_t));
Raymond Knopp's avatar
Raymond Knopp committed
176 177
  ue_context_p->ue_context.reconfig->rrc_TransactionIdentifier=0;
  ue_context_p->ue_context.reconfig->criticalExtensions.present = NR_RRCReconfiguration__criticalExtensions_PR_rrcReconfiguration;
178
  NR_RRCReconfiguration_IEs_t *reconfig_ies=calloc(1,sizeof(NR_RRCReconfiguration_IEs_t));
Raymond Knopp's avatar
Raymond Knopp committed
179
  ue_context_p->ue_context.reconfig->criticalExtensions.choice.rrcReconfiguration = reconfig_ies;
180
  ue_context_p->ue_context.rb_config = calloc(1,sizeof(NR_RRCReconfiguration_t));
181
  if (get_softmodem_params()->phy_test == 1 || get_softmodem_params()->do_ra == 1 || get_softmodem_params()->sa == 1){
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    fill_default_rbconfig(ue_context_p->ue_context.rb_config,
                          5 /* EPS bearer ID */,
                          1 /* drb ID */,
                          NR_CipheringAlgorithm_nea0,
                          NR_SecurityConfig__keyToUse_master);
  } else {
    /* TODO: handle more than one bearer */
    if (m == NULL) {
      LOG_E(RRC, "fatal: m==NULL\n");
      exit(1);
    }
    if (m->nb_e_rabs_tobeadded != 1) {
      LOG_E(RRC, "fatal: m->nb_e_rabs_tobeadded = %d, should be 1\n", m->nb_e_rabs_tobeadded);
      exit(1);
    }

    /* store security key and security capabilities */
    memcpy(ue_context_p->ue_context.kgnb, m->kgnb, 32);
    ue_context_p->ue_context.security_capabilities.nRencryption_algorithms = m->security_capabilities.encryption_algorithms;
    ue_context_p->ue_context.security_capabilities.nRintegrity_algorithms = m->security_capabilities.integrity_algorithms;

    /* 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.
     */
    /* preset nea0 as fallback */
    ue_context_p->ue_context.ciphering_algorithm = 0;
    for (i = 0; i < rrc->security.ciphering_algorithms_count; i++) {
      int nea_mask[4] = {
        0,
        0x8000,  /* nea1 */
        0x4000,  /* nea2 */
        0x2000   /* nea3 */
      };
      if (rrc->security.ciphering_algorithms[i] == 0) {
        /* nea0 already preselected */
        break;
      }
      if (ue_context_p->ue_context.security_capabilities.nRencryption_algorithms & nea_mask[rrc->security.ciphering_algorithms[i]]) {
        ue_context_p->ue_context.ciphering_algorithm = rrc->security.ciphering_algorithms[i];
        break;
      }
    }

    LOG_I(RRC, "selecting ciphering algorithm %d\n", (int)ue_context_p->ue_context.ciphering_algorithm);

    /* integrity: no integrity protection for DRB in ENDC mode
     * as written in 38.331: "If UE is connected to E-UTRA/EPC, this field
     * indicates the integrity protection algorithm to be used for SRBs
     * configured with NR PDCP, as specified in TS 33.501"
     * So nothing for DRB. Plus a test with a COTS UE revealed that it
     * did not apply integrity on the DRB.
     */
    ue_context_p->ue_context.integrity_algorithm = 0;

    LOG_I(RRC, "selecting integrity algorithm %d\n", ue_context_p->ue_context.integrity_algorithm);

    /* derive UP security key */
    nr_derive_key_up_enc(ue_context_p->ue_context.ciphering_algorithm,
                         ue_context_p->ue_context.kgnb,
243
                         &kUPenc);
Cedric Roux's avatar
Cedric Roux committed
244 245
    nr_derive_key_up_int(ue_context_p->ue_context.integrity_algorithm,
                         ue_context_p->ue_context.kgnb,
246
                         &kUPint);
Cedric Roux's avatar
Cedric Roux committed
247

248 249 250 251 252 253 254 255 256 257 258 259 260 261
    e_NR_CipheringAlgorithm cipher_algo;
    switch (ue_context_p->ue_context.ciphering_algorithm) {
    case 0: cipher_algo = NR_CipheringAlgorithm_nea0; break;
    case 1: cipher_algo = NR_CipheringAlgorithm_nea1; break;
    case 2: cipher_algo = NR_CipheringAlgorithm_nea2; break;
    case 3: cipher_algo = NR_CipheringAlgorithm_nea3; break;
    default: LOG_E(RRC, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__); exit(1);
    }

    fill_default_rbconfig(ue_context_p->ue_context.rb_config,
                          m->e_rabs_tobeadded[0].e_rab_id,
                          m->e_rabs_tobeadded[0].drb_ID,
                          cipher_algo,
                          NR_SecurityConfig__keyToUse_secondary);
262
  }
263 264
  if (ue_context_p->ue_context.spCellConfig) {
    fill_default_reconfig(carrier->servingcellconfigcommon,
265 266 267 268
                          ue_context_p->ue_context.spCellConfig->spCellConfigDedicated,
                          reconfig_ies,
                          ue_context_p->ue_context.secondaryCellGroup,
                          carrier->pdsch_AntennaPorts,
269
                          carrier->minRXTXTIME,
270 271 272
                          carrier->do_CSIRS,
                          carrier->do_SRS,
                          ue_context_p->local_uid);
273 274
  } else {
    fill_default_reconfig(carrier->servingcellconfigcommon,
275 276 277 278
                          NULL,
                          reconfig_ies,
                          ue_context_p->ue_context.secondaryCellGroup,
                          carrier->pdsch_AntennaPorts,
279
                          carrier->minRXTXTIME,
280 281 282
                          carrier->do_CSIRS,
                          carrier->do_SRS,
                          ue_context_p->local_uid);
283
  }
284
  ue_context_p->ue_id_rnti = ue_context_p->ue_context.secondaryCellGroup->spCellConfig->reconfigurationWithSync->newUE_Identity;
Raymond Knopp's avatar
Raymond Knopp committed
285
  NR_CG_Config_t *CG_Config = calloc(1,sizeof(*CG_Config));
286 287 288 289 290 291 292 293 294 295
  memset((void *)CG_Config,0,sizeof(*CG_Config));
  //int CG_Config_size = generate_CG_Config(rrc,CG_Config,ue_context_p->ue_context.reconfig,ue_context_p->ue_context.rb_config);
  generate_CG_Config(rrc,CG_Config,ue_context_p->ue_context.reconfig,ue_context_p->ue_context.rb_config);

  if(m!=NULL) {
    uint8_t inde_list[m->nb_e_rabs_tobeadded];
    memset(inde_list, 0, m->nb_e_rabs_tobeadded*sizeof(uint8_t));

    if (m->nb_e_rabs_tobeadded>0) {
      for (int i=0; i<m->nb_e_rabs_tobeadded; i++) {
296
        // Add the new E-RABs at the corresponding rrc ue context of the gNB 
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        ue_context_p->ue_context.e_rab[i].param.e_rab_id = m->e_rabs_tobeadded[i].e_rab_id;
        ue_context_p->ue_context.e_rab[i].param.gtp_teid = m->e_rabs_tobeadded[i].gtp_teid;
        memcpy(&ue_context_p->ue_context.e_rab[i].param.sgw_addr, &m->e_rabs_tobeadded[i].sgw_addr, sizeof(transport_layer_addr_t));
        ue_context_p->ue_context.nb_of_e_rabs++;
        //Fill the required E-RAB specific information for the creation of the S1-U tunnel between the gNB and the SGW
        create_tunnel_req.eps_bearer_id[i]       = ue_context_p->ue_context.e_rab[i].param.e_rab_id;
        create_tunnel_req.sgw_S1u_teid[i]        = ue_context_p->ue_context.e_rab[i].param.gtp_teid;
        memcpy(&create_tunnel_req.sgw_addr[i], &ue_context_p->ue_context.e_rab[i].param.sgw_addr, sizeof(transport_layer_addr_t));
        inde_list[i] = i;
        LOG_I(RRC,"S1-U tunnel: index %d target sgw ip %d.%d.%d.%d length %d gtp teid %u\n",
              i,
              create_tunnel_req.sgw_addr[i].buffer[0],
              create_tunnel_req.sgw_addr[i].buffer[1],
              create_tunnel_req.sgw_addr[i].buffer[2],
              create_tunnel_req.sgw_addr[i].buffer[3],
              create_tunnel_req.sgw_addr[i].length,
              create_tunnel_req.sgw_S1u_teid[i]);
      }

      create_tunnel_req.rnti           = ue_context_p->ue_id_rnti;
      create_tunnel_req.num_tunnels    = m->nb_e_rabs_tobeadded;
      RB_INSERT(rrc_nr_ue_tree_s, &RC.nrrrc[rrc->module_id]->rrc_ue_head, ue_context_p);
      PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, rrc->module_id, GNB_FLAG_YES, ue_context_p->ue_id_rnti, 0, 0,rrc->module_id);
320
      memset(&create_tunnel_resp, 0, sizeof(create_tunnel_resp));
321
      if (!IS_SOFTMODEM_NOS1) {
Melissa Elkadi's avatar
Melissa Elkadi committed
322
        LOG_D(RRC, "Calling gtpv1u_create_s1u_tunnel()\n");
323 324 325 326 327 328 329 330 331
        gtpv1u_create_s1u_tunnel(
          ctxt.instance,
          &create_tunnel_req,
          &create_tunnel_resp);
        rrc_gNB_process_GTPV1U_CREATE_TUNNEL_RESP(
          &ctxt,
          &create_tunnel_resp,
          &inde_list[0]);
      }
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
      X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).nb_e_rabs_admitted_tobeadded = m->nb_e_rabs_tobeadded;
      X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).target_assoc_id = m->target_assoc_id;

      for(int i=0; i<ue_context_p->ue_context.nb_of_e_rabs; i++) {
        X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].e_rab_id = ue_context_p->ue_context.e_rab[i].param.e_rab_id;
        X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gtp_teid = create_tunnel_resp.enb_S1u_teid[i];
        memcpy(&X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gnb_addr, &create_tunnel_resp.enb_addr, sizeof(transport_layer_addr_t));
        //The length field in the X2AP targetting structure is expected in bits but the create_tunnel_resp returns the address length in bytes
        X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gnb_addr.length = create_tunnel_resp.enb_addr.length*8;
        LOG_I(RRC,"S1-U create_tunnel_resp tunnel: index %d target gNB ip %d.%d.%d.%d length %d gtp teid %u\n",
              i,
              create_tunnel_resp.enb_addr.buffer[0],
              create_tunnel_resp.enb_addr.buffer[1],
              create_tunnel_resp.enb_addr.buffer[2],
              create_tunnel_resp.enb_addr.buffer[3],
              create_tunnel_resp.enb_addr.length,
              create_tunnel_resp.enb_S1u_teid[i]);
        LOG_I(RRC,"X2AP sGNB Addition Request: index %d target gNB ip %d.%d.%d.%d length %d gtp teid %u\n",
              i,
              X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gnb_addr.buffer[0],
              X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gnb_addr.buffer[1],
              X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gnb_addr.buffer[2],
              X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gnb_addr.buffer[3],
              X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gnb_addr.length,
              X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).e_rabs_admitted_tobeadded[i].gtp_teid);
      }
    } else
      LOG_W(RRC, "No E-RAB to be added received from SgNB Addition Request message \n");
360

Cedric Roux's avatar
Cedric Roux committed
361 362 363 364 365 366 367 368
    X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).MeNB_ue_x2_id = m->ue_x2_id;
    X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).SgNB_ue_x2_id = ue_context_p->ue_context.secondaryCellGroup->spCellConfig->reconfigurationWithSync->newUE_Identity;
    //X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).rrc_buffer_size = CG_Config_size; //Need to verify correct value for the buffer_size
    // Send to X2 entity to transport to MeNB
    asn_enc_rval_t enc_rval = uper_encode_to_buffer(&asn_DEF_NR_CG_Config,
                              NULL,
                              (void *)CG_Config,
                              X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).rrc_buffer,
369
                              sizeof(X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).rrc_buffer));
Cedric Roux's avatar
Cedric Roux committed
370 371
    X2AP_ENDC_SGNB_ADDITION_REQ_ACK(msg).rrc_buffer_size = (enc_rval.encoded+7)>>3;
    itti_send_msg_to_task(TASK_X2AP, ENB_MODULE_ID_TO_INSTANCE(0), msg); //Check right id instead of hardcoding
372
  } else if (get_softmodem_params()->do_ra || get_softmodem_params()->sa) {
373
    PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, rrc->module_id, GNB_FLAG_YES, ue_context_p->ue_id_rnti, 0, 0,rrc->module_id);
Cedric Roux's avatar
Cedric Roux committed
374 375
  }

376
  rrc->Nb_ue++;
377
  // configure MAC and RLC
378 379 380 381
  if (NODE_IS_DU(rrc->node_type)) {
    rrc_mac_config_req_gNB(rrc->module_id,
                           rrc->carrier.ssb_SubcarrierOffset,
                           rrc->carrier.pdsch_AntennaPorts,
382
			   rrc->carrier.pusch_AntennaPorts,
francescomani's avatar
francescomani committed
383
                           rrc->carrier.sib1_tda,
384
                           rrc->carrier.minRXTXTIME,
385
                           rrc->carrier.servingcellconfigcommon,
386
                           &rrc->carrier.mib,
387 388 389 390 391 392 393
                           1, // add_ue flag
                           ue_context_p->ue_id_rnti,
                           ue_context_p->ue_context.secondaryCellGroup);
  } else {
    rrc_mac_config_req_gNB(rrc->module_id,
                           rrc->carrier.ssb_SubcarrierOffset,
                           rrc->carrier.pdsch_AntennaPorts,
394
                           rrc->carrier.pusch_AntennaPorts,
francescomani's avatar
francescomani committed
395
                           rrc->carrier.sib1_tda,
396
                           rrc->carrier.minRXTXTIME,
397
                           NULL,
398
                           NULL,
399 400 401 402
                           1, // add_ue flag
                           ue_context_p->ue_id_rnti,
                           ue_context_p->ue_context.secondaryCellGroup);
  }
403

404 405 406 407 408 409 410 411 412
  if(m == NULL){
    LOG_W(RRC, "Calling RRC PDCP/RLC ASN1 request functions for protocol context %p with module_id %d, rnti %x, frame %d, subframe %d eNB_index %d \n", &ctxt,
                                                                                                                                                        ctxt.module_id,
                                                                                                                                                        ctxt.rnti,
                                                                                                                                                        ctxt.frame,
                                                                                                                                                        ctxt.subframe,
                                                                                                                                                        ctxt.eNB_index);
  }

413 414 415 416 417 418 419 420
  nr_rrc_pdcp_config_asn1_req(&ctxt,
                              get_softmodem_params()->sa ? ue_context_p->ue_context.rb_config->srb_ToAddModList : (NR_SRB_ToAddModList_t *) NULL,
                              ue_context_p->ue_context.rb_config->drb_ToAddModList ,
                              ue_context_p->ue_context.rb_config->drb_ToReleaseList,
                              (ue_context_p->ue_context.integrity_algorithm << 4) | ue_context_p->ue_context.ciphering_algorithm,
                              NULL,          /* kRRCenc - unused */
                              NULL,          /* kRRCint - unused */
                              kUPenc,        /* kUPenc  */
Cedric Roux's avatar
Cedric Roux committed
421
                              kUPint,        /* kUPint */
422 423 424
                              NULL,
                              NULL,
                              ue_context_p->ue_context.secondaryCellGroup->rlc_BearerToAddModList);
425 426

  nr_rrc_rlc_config_asn1_req (&ctxt,
427 428 429 430 431
                              get_softmodem_params()->sa ? ue_context_p->ue_context.rb_config->srb_ToAddModList : (NR_SRB_ToAddModList_t *) NULL,
                              ue_context_p->ue_context.rb_config->drb_ToAddModList,
                              ue_context_p->ue_context.rb_config->drb_ToReleaseList,
                              (LTE_PMCH_InfoList_r9_t *) NULL,
                              ue_context_p->ue_context.secondaryCellGroup->rlc_BearerToAddModList);
432

433 434
  LOG_D(RRC, "%s:%d: done RRC PDCP/RLC ASN1 request for UE rnti %x\n", __FUNCTION__, __LINE__, ctxt.rnti);

435 436
}

Cedric Roux's avatar
Cedric Roux committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
void rrc_remove_nsa_user(gNB_RRC_INST *rrc, int rnti) {
  protocol_ctxt_t      ctxt;
  rrc_gNB_ue_context_t *ue_context;
  int                  e_rab;

  LOG_D(RRC, "calling rrc_remove_nsa_user rnti %d\n", rnti);
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, rrc->module_id, GNB_FLAG_YES, rnti, 0, 0, rrc->module_id);

  ue_context = rrc_gNB_get_ue_context(rrc, rnti);
  if (ue_context == NULL) {
    LOG_W(RRC, "rrc_remove_nsa_user: rnti %d not found\n", rnti);
    return;
  }

  pdcp_remove_UE(&ctxt);
452

Cedric Roux's avatar
Cedric Roux committed
453 454 455
  rrc_rlc_remove_ue(&ctxt);

  mac_remove_nr_ue(rrc->module_id, rnti);
456 457 458 459
  gtpv1u_enb_delete_tunnel_req_t tmp={0};
  tmp.rnti=rnti;
  tmp.from_gnb=1;
  LOG_D(RRC, "ue_context->ue_context.nb_of_e_rabs %d will be deleted for rnti %d\n", ue_context->ue_context.nb_of_e_rabs, rnti);
Cedric Roux's avatar
Cedric Roux committed
460
  for (e_rab = 0; e_rab < ue_context->ue_context.nb_of_e_rabs; e_rab++) {
461
    tmp.eps_bearer_id[tmp.num_erab++]= ue_context->ue_context.gnb_gtp_ebi[e_rab];
Cedric Roux's avatar
Cedric Roux committed
462 463 464 465 466
    // erase data
    ue_context->ue_context.gnb_gtp_teid[e_rab] = 0;
    memset(&ue_context->ue_context.gnb_gtp_addrs[e_rab], 0, sizeof(ue_context->ue_context.gnb_gtp_addrs[e_rab]));
    ue_context->ue_context.gnb_gtp_ebi[e_rab] = 0;
  }
467
  gtpv1u_delete_s1u_tunnel(rrc->module_id,  &tmp);
Cedric Roux's avatar
Cedric Roux committed
468 469 470
  /* remove context */
  rrc_gNB_remove_ue_context(&ctxt, rrc, ue_context);
}