rrc_eNB_S1AP.c 101 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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
 */

winckel's avatar
winckel committed
22 23
/*! \file rrc_eNB_S1AP.c
 * \brief rrc S1AP procedures for eNB
24
 * \author Navid Nikaein, Laurent Winckel, Sebastien ROUX, and Lionel GAUTHIER
25
 * \date 2013-2016
winckel's avatar
winckel committed
26 27
 * \version 1.0
 * \company Eurecom
28
 * \email: navid.nikaein@eurecom.fr
winckel's avatar
winckel committed
29
 */
30

31 32
# include "rrc_defs.h"
# include "rrc_extern.h"
winckel's avatar
winckel committed
33
# include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
34
# include "RRC/LTE/MESSAGES/asn1_msg.h"
35
# include "rrc_eNB_UE_context.h"
winckel's avatar
winckel committed
36
# include "rrc_eNB_S1AP.h"
gauthier's avatar
gauthier committed
37
# include "enb_config.h"
38
# include "common/ran_context.h"
39
# include "gtpv1u.h"
winckel's avatar
winckel committed
40

41 42
# include "s1ap_eNB_defs.h"
# include "s1ap_eNB_management_procedures.h"
43
# include "s1ap_eNB_ue_context.h"
44 45 46 47 48
#include "asn1_conversions.h"
#include "intertask_interface.h"
#include "pdcp.h"
#include "pdcp_primitives.h"
#include "s1ap_eNB.h"
winckel's avatar
winckel committed
49 50


51
#include "UTIL/OSA/osa_defs.h"
52
#include "msc.h"
53

54
#include "LTE_UERadioAccessCapabilityInformation.h"
Cedric Roux's avatar
Cedric Roux committed
55

Cedric Roux's avatar
Cedric Roux committed
56
#include "gtpv1u_eNB_task.h"
57
#include "RRC/LTE/rrc_eNB_GTPV1U.h"
Cedric Roux's avatar
Cedric Roux committed
58

59
#include "TLVDecoder.h"
60
#include "S1AP_NAS-PDU.h"
61
#include "flexran_agent_common_internal.h"
Cedric Roux's avatar
Cedric Roux committed
62

63 64
extern RAN_CONTEXT_t RC;

65 66 67
extern int
gtpv1u_delete_s1u_tunnel(
  const instance_t instanceP,
68
  const gtpv1u_enb_delete_tunnel_req_t *const req_pP);
69

winckel's avatar
winckel committed
70 71 72
/* Value to indicate an invalid UE initial id */
static const uint16_t UE_INITIAL_ID_INVALID = 0;

73
/* Masks for S1AP Encryption algorithms, EEA0 is always supported (not coded) */
74 75
static const uint16_t S1AP_ENCRYPTION_EEA1_MASK = 0x8000;
static const uint16_t S1AP_ENCRYPTION_EEA2_MASK = 0x4000;
76 77

/* Masks for S1AP Integrity algorithms, EIA0 is always supported (not coded) */
78 79
static const uint16_t S1AP_INTEGRITY_EIA1_MASK = 0x8000;
static const uint16_t S1AP_INTEGRITY_EIA2_MASK = 0x4000;
80

81
#if (LTE_RRC_VERSION >= MAKE_VERSION(9, 2, 0))
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
82
  #define INTEGRITY_ALGORITHM_NONE LTE_SecurityAlgorithmConfig__integrityProtAlgorithm_eia0_v920
83
#else
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
84 85 86 87 88
  #ifdef EXMIMO_IOT
    #define INTEGRITY_ALGORITHM_NONE LTE_SecurityAlgorithmConfig__integrityProtAlgorithm_eia2
  #else
    #define INTEGRITY_ALGORITHM_NONE LTE_SecurityAlgorithmConfig__integrityProtAlgorithm_reserved
  #endif
89
#endif
90

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
91
void extract_imsi(uint8_t *pdu_buf, uint32_t pdu_len, rrc_eNB_ue_context_t *ue_context_pP) {
92 93 94 95 96 97 98
  /* Process NAS message locally to get the IMSI */
  nas_message_t nas_msg;
  memset(&nas_msg, 0, sizeof(nas_message_t));
  int size = 0;
  nas_message_security_header_t *header = &nas_msg.header;
  /* Decode the first octet of the header (security header type or EPS
  * bearer identity, and protocol discriminator) */
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
99
  DECODE_U8((char *) pdu_buf, *(uint8_t *) (header), size);
100 101 102

  /* Decode NAS message only if decodable*/
  if (!(header->security_header_type <= SECURITY_HEADER_TYPE_INTEGRITY_PROTECTED
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
103 104
        && header->protocol_discriminator == EPS_MOBILITY_MANAGEMENT_MESSAGE
        && pdu_len > NAS_MESSAGE_SECURITY_HEADER_SIZE))
105 106 107 108 109 110 111 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 147 148 149 150 151 152 153 154 155 156 157 158
    return;

  if (header->security_header_type != SECURITY_HEADER_TYPE_NOT_PROTECTED) {
    /* Decode the message authentication code */
    DECODE_U32((char *) pdu_buf+size, header->message_authentication_code, size);
    /* Decode the sequence number */
    DECODE_U8((char *) pdu_buf+size, header->sequence_number, size);
  }

  /* Note: the value of the pointer (i.e. the address) is given by value, so we
   * can modify it as we want. The callee retains the original address! */
  pdu_buf += size;
  pdu_len -= size;
  /* Decode plain NAS message */
  EMM_msg *e_msg = &nas_msg.plain.emm;
  emm_msg_header_t *emm_header = &e_msg->header;
  /* First decode the EMM message header */
  int e_head_size = 0;

  /* Check that buffer contains more than only the header */
  if (pdu_len <= sizeof(emm_msg_header_t))
    return;

  /* Decode the security header type and the protocol discriminator */
  DECODE_U8(pdu_buf + e_head_size, *(uint8_t *)(emm_header), e_head_size);
  /* Decode the message type */
  DECODE_U8(pdu_buf + e_head_size, emm_header->message_type, e_head_size);

  /* Check that this is the right message */
  if (emm_header->protocol_discriminator != EPS_MOBILITY_MANAGEMENT_MESSAGE)
    return;

  pdu_buf += e_head_size;
  pdu_len -= e_head_size;

  if (emm_header->message_type == IDENTITY_RESPONSE) {
    decode_identity_response(&e_msg->identity_response, pdu_buf, pdu_len);

    if (e_msg->identity_response.mobileidentity.imsi.typeofidentity == MOBILE_IDENTITY_IMSI) {
      memcpy(&ue_context_pP->ue_context.imsi,
             &e_msg->identity_response.mobileidentity.imsi,
             sizeof(ImsiMobileIdentity_t));
    }
  } else if (emm_header->message_type == ATTACH_REQUEST) {
    decode_attach_request(&e_msg->attach_request, pdu_buf, pdu_len);

    if (e_msg->attach_request.oldgutiorimsi.imsi.typeofidentity == MOBILE_IDENTITY_IMSI) {
      /* the following is very dirty, we cast (implicitly) from
       * ImsiEpsMobileIdentity_t to ImsiMobileIdentity_t*/
      memcpy(&ue_context_pP->ue_context.imsi,
             &e_msg->attach_request.oldgutiorimsi.imsi,
             sizeof(ImsiMobileIdentity_t));
    }
  }
159
}
160

161

162
//------------------------------------------------------------------------------
163
/*
164 165
* Get the UE S1 struct containing hashtables S1_id/UE_id.
* Is also used to set the S1_id of the UE, depending on inputs.
166
*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
167
struct rrc_ue_s1ap_ids_s *
168
rrc_eNB_S1AP_get_ue_ids(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
169
  eNB_RRC_INST *const rrc_instance_pP,
170 171 172 173 174
  const uint16_t ue_initial_id,
  const uint32_t eNB_ue_s1ap_id
)
//------------------------------------------------------------------------------
{
175 176
  rrc_ue_s1ap_ids_t *result = NULL;
  rrc_ue_s1ap_ids_t *result2 = NULL;
177 178 179 180
  /*****************************/
  instance_t instance = 0;
  s1ap_eNB_instance_t *s1ap_eNB_instance_p = NULL;
  s1ap_eNB_ue_context_t *ue_desc_p = NULL;
181
  rrc_eNB_ue_context_t *ue_context_p = NULL;
182
  /*****************************/
183 184 185
  hashtable_rc_t     h_rc;

  if (ue_initial_id != UE_INITIAL_ID_INVALID) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
186
    h_rc = hashtable_get(rrc_instance_pP->initial_id2_s1ap_ids, (hash_key_t)ue_initial_id, (void **)&result);
187

188 189
    if (h_rc == HASH_TABLE_OK) {
      if (eNB_ue_s1ap_id > 0) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
190 191
        h_rc = hashtable_get(rrc_instance_pP->s1ap_id2_s1ap_ids, (hash_key_t)eNB_ue_s1ap_id, (void **)&result2);

192 193
        if (h_rc != HASH_TABLE_OK) { // this case is equivalent to associate eNB_ue_s1ap_id and ue_initial_id
          result2 = malloc(sizeof(*result2));
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
194

195 196 197 198 199 200
          if (NULL != result2) {
            *result2 = *result;
            result2->eNB_ue_s1ap_id = eNB_ue_s1ap_id;
            result->eNB_ue_s1ap_id  = eNB_ue_s1ap_id;
            h_rc = hashtable_insert(rrc_instance_pP->s1ap_id2_s1ap_ids, (hash_key_t)eNB_ue_s1ap_id, result2);

201
            if (h_rc != HASH_TABLE_OK) {
Cedric Roux's avatar
Cedric Roux committed
202
              LOG_E(S1AP, "[eNB %ld] Error while hashtable_insert in s1ap_id2_s1ap_ids eNB_ue_s1ap_id %"PRIu32"\n",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
203 204
                    rrc_instance_pP - RC.rrc[0],
                    eNB_ue_s1ap_id);
205
            }
206 207 208
          }
        } else { // here we should check that the association was done correctly
          if ((result->ue_initial_id != result2->ue_initial_id) || (result->eNB_ue_s1ap_id != result2->eNB_ue_s1ap_id)) {
209 210 211 212
            LOG_E(S1AP, "[eNB %ld] Error while hashtable_get, two rrc_ue_s1ap_ids_t that should be equal, are not:\n \
              ue_initial_id 1 = %"PRIu16",\n \
              ue_initial_id 2 = %"PRIu16",\n \
              eNB_ue_s1ap_id 1 = %"PRIu32",\n \
213
              eNB_ue_s1ap_id 2 = %"PRIu32"\n",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
214 215 216 217 218
                  rrc_instance_pP - RC.rrc[0],
                  result->ue_initial_id,
                  result2->ue_initial_id,
                  result->eNB_ue_s1ap_id,
                  result2->eNB_ue_s1ap_id);
219
            // Still return *result
220 221 222 223
          }
        }
      } // end if if (eNB_ue_s1ap_id > 0)
    } else { // end if (h_rc == HASH_TABLE_OK)
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
224 225 226
      LOG_E(S1AP, "[eNB %ld] In hashtable_get, couldn't find in initial_id2_s1ap_ids ue_initial_id %"PRIu16"\n",
            rrc_instance_pP - RC.rrc[0],
            ue_initial_id);
227
      return NULL;
228 229 230 231 232 233 234 235
      /*
      * 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 s1ap_id2_s1ap_ids and eNB_ue_s1ap_id (if > 0),
      * but this behavior is not expected at the moment.
      */
    } // end else (h_rc != HASH_TABLE_OK)
  } else { // end if (ue_initial_id != UE_INITIAL_ID_INVALID)
    if (eNB_ue_s1ap_id > 0) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
236
      h_rc = hashtable_get(rrc_instance_pP->s1ap_id2_s1ap_ids, (hash_key_t)eNB_ue_s1ap_id, (void **)&result);
237 238 239 240

      if (h_rc != HASH_TABLE_OK) {
        /*
        * This case is uncommon, but can happen when:
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
241
        * -> if the first NAS message was a Detach Request (non exhaustiv), the UE RRC context exist
242 243 244
        * but is not associated with eNB_ue_s1ap_id
        * -> ... (?)
        */
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
245 246 247
        LOG_E(S1AP, "[eNB %ld] In hashtable_get, couldn't find in s1ap_id2_s1ap_ids eNB_ue_s1ap_id %"PRIu32", trying to find it through S1AP context\n",
              rrc_instance_pP - RC.rrc[0],
              eNB_ue_s1ap_id);
248 249
        instance = ENB_MODULE_ID_TO_INSTANCE(rrc_instance_pP - RC.rrc[0]); // get eNB instance
        s1ap_eNB_instance_p = s1ap_eNB_get_instance(instance); // get s1ap_eNB_instance
250 251 252 253 254 255 256 257

        if (s1ap_eNB_instance_p != NULL) {
          ue_desc_p = s1ap_eNB_get_ue_context(s1ap_eNB_instance_p, eNB_ue_s1ap_id); // get s1ap_eNB_ue_context
        } else {
          LOG_E(S1AP, "[eNB instance %d] Couldn't find the eNB S1AP context\n",
              instance);
          return NULL;
        }
258 259

        if (ue_desc_p != NULL) {
260 261 262 263 264 265 266 267
          struct s1ap_eNB_ue_context_s *s1ap_ue_context_p = NULL;
          if ((s1ap_ue_context_p = RB_REMOVE(s1ap_ue_map, &s1ap_eNB_instance_p->s1ap_ue_head, ue_desc_p)) != NULL) {
            LOG_E(RRC, "Removed UE context eNB_ue_s1ap_id %u\n", s1ap_ue_context_p->eNB_ue_s1ap_id);
            s1ap_eNB_free_ue_context(s1ap_ue_context_p);
          } else {
            LOG_E(RRC, "Removing UE context eNB_ue_s1ap_id %u: did not find context\n",ue_desc_p->eNB_ue_s1ap_id);
          }
          return NULL; //skip the operation below to avoid loop
268
          result = rrc_eNB_S1AP_get_ue_ids(rrc_instance_pP, ue_desc_p->ue_initial_id, eNB_ue_s1ap_id);
269

270 271 272 273 274 275 276 277
          if (result != NULL) {
            ue_context_p = rrc_eNB_get_ue_context(RC.rrc[ENB_INSTANCE_TO_MODULE_ID(instance)], result->ue_rnti);

            if ((ue_context_p != NULL) && (ue_context_p->ue_context.eNB_ue_s1ap_id == 0)) {
              ue_context_p->ue_context.eNB_ue_s1ap_id = eNB_ue_s1ap_id;
            } else {
              LOG_E(RRC, "[eNB %ld] Incoherence between RRC context and S1AP context (%d != %d) for UE RNTI %d or UE RRC context doesn't exist\n",
                    rrc_instance_pP - RC.rrc[0],
frtabu's avatar
frtabu committed
278
                    (ue_context_p==NULL)?99999:ue_context_p->ue_context.eNB_ue_s1ap_id,
279 280 281
                    eNB_ue_s1ap_id,
                    result->ue_rnti);
            }
282
          }
283
        } else {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
284 285 286
          LOG_E(S1AP, "[eNB %ld] In hashtable_get, couldn't find in s1ap_id2_s1ap_ids eNB_ue_s1ap_id %"PRIu32", even when looking at S1AP context\n",
                rrc_instance_pP - RC.rrc[0],
                eNB_ue_s1ap_id);
287
          return NULL;
288 289 290 291 292
        }
      } // end if (h_rc != HASH_TABLE_OK)
    } // end if (eNB_ue_s1ap_id > 0)
  } // end else (ue_initial_id == UE_INITIAL_ID_INVALID)

293
  return result;
294
}
295

296
//------------------------------------------------------------------------------
297
/*
298
* Remove UE ids (ue_initial_id and S1_id) from hashtables.
299
*/
300 301
void
rrc_eNB_S1AP_remove_ue_ids(
302 303
  eNB_RRC_INST *const rrc_instance_pP,
  struct rrc_ue_s1ap_ids_s *const ue_ids_pP
304 305 306
)
//------------------------------------------------------------------------------
{
307
  hashtable_rc_t h_rc;
308

309 310 311
  if (rrc_instance_pP == NULL) {
    LOG_E(RRC, "Bad RRC instance\n");
    return;
312 313
  }

314 315 316
  if (ue_ids_pP == NULL) {
    LOG_E(RRC, "Trying to free a NULL S1AP UE IDs\n");
    return;
winckel's avatar
winckel committed
317
  }
318

frtabu's avatar
frtabu committed
319 320
  const uint16_t ue_initial_id  = ue_ids_pP->ue_initial_id;
  const uint32_t eNB_ue_s1ap_id = ue_ids_pP->eNB_ue_s1ap_id;
321

322
  if (eNB_ue_s1ap_id > 0) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
323 324
    h_rc = hashtable_remove(rrc_instance_pP->s1ap_id2_s1ap_ids, (hash_key_t)eNB_ue_s1ap_id);

325
    if (h_rc != HASH_TABLE_OK) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
326 327 328 329
      LOG_W(RRC, "S1AP Did not find entry in hashtable s1ap_id2_s1ap_ids for eNB_ue_s1ap_id %u\n", eNB_ue_s1ap_id);
    } else {
      LOG_W(RRC, "S1AP removed entry in hashtable s1ap_id2_s1ap_ids for eNB_ue_s1ap_id %u\n", eNB_ue_s1ap_id);
    }
330 331 332 333
  }

  if (ue_initial_id != UE_INITIAL_ID_INVALID) {
    h_rc = hashtable_remove(rrc_instance_pP->initial_id2_s1ap_ids, (hash_key_t)ue_initial_id);
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
334

335 336 337 338 339
    if (h_rc != HASH_TABLE_OK) {
      LOG_W(RRC, "S1AP Did not find entry in hashtable initial_id2_s1ap_ids for ue_initial_id %u\n", ue_initial_id);
    } else {
      LOG_W(RRC, "S1AP removed entry in hashtable initial_id2_s1ap_ids for ue_initial_id %u\n", ue_initial_id);
    }
340
  }
winckel's avatar
winckel committed
341 342
}

343 344
/*! \fn uint16_t get_next_ue_initial_id(uint8_t mod_id)
 *\brief provide an UE initial ID for S1AP initial communication.
winckel's avatar
winckel committed
345
 *\param mod_id Instance ID of eNB.
346
 *\return the UE initial ID.
winckel's avatar
winckel committed
347
 */
348 349 350 351 352 353
//------------------------------------------------------------------------------
static uint16_t
get_next_ue_initial_id(
  const module_id_t mod_id
)
//------------------------------------------------------------------------------
354
{
355 356
  static uint16_t ue_initial_id[NUMBER_OF_eNB_MAX];
  ue_initial_id[mod_id]++;
winckel's avatar
winckel committed
357

358 359 360 361
  /* 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]++;
  }
winckel's avatar
winckel committed
362

363
  return ue_initial_id[mod_id];
364
}
365

366 367


winckel's avatar
winckel committed
368

winckel's avatar
winckel committed
369
/*! \fn uint8_t get_UE_index_from_s1ap_ids(uint8_t mod_id, uint16_t ue_initial_id, uint32_t eNB_ue_s1ap_id)
winckel's avatar
winckel committed
370 371 372 373 374 375 376
 *\brief retrieve UE index in the eNB from the UE initial ID if not equal to UE_INDEX_INVALID or
 *\brief from the eNB_ue_s1ap_id previously transmitted by S1AP.
 *\param mod_id Instance ID of eNB.
 *\param ue_initial_id The UE initial ID sent to S1AP.
 *\param eNB_ue_s1ap_id The value sent by S1AP.
 *\return the UE index or UE_INDEX_INVALID if not found.
 */
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
377
static struct rrc_eNB_ue_context_s *
378 379 380 381
rrc_eNB_get_ue_context_from_s1ap_ids(
  const instance_t  instanceP,
  const uint16_t    ue_initial_idP,
  const uint32_t    eNB_ue_s1ap_idP
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
382 383
) {
  rrc_ue_s1ap_ids_t *temp = NULL;
384
  temp = rrc_eNB_S1AP_get_ue_ids(RC.rrc[ENB_INSTANCE_TO_MODULE_ID(instanceP)], ue_initial_idP, eNB_ue_s1ap_idP);
winckel's avatar
winckel committed
385

386 387
  if (temp != NULL) {
    return rrc_eNB_get_ue_context(RC.rrc[ENB_INSTANCE_TO_MODULE_ID(instanceP)], temp->ue_rnti);
winckel's avatar
winckel committed
388 389
  }

390
  return NULL;
winckel's avatar
winckel committed
391 392
}

393 394 395 396 397
/*! \fn e_SecurityAlgorithmConfig__cipheringAlgorithm rrc_eNB_select_ciphering(uint16_t algorithms)
 *\brief analyze available encryption algorithms bit mask and return the relevant one.
 *\param algorithms The bit mask of available algorithms received from S1AP.
 *\return the selected algorithm.
 */
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
398 399
static LTE_CipheringAlgorithm_r12_t rrc_eNB_select_ciphering(uint16_t algorithms) {
  //#warning "Forced   return SecurityAlgorithmConfig__cipheringAlgorithm_eea0, to be deleted in future"
400
  return LTE_CipheringAlgorithm_r12_eea0;
401

402
  if (algorithms & S1AP_ENCRYPTION_EEA2_MASK) {
403
    return LTE_CipheringAlgorithm_r12_eea2;
404 405 406
  }

  if (algorithms & S1AP_ENCRYPTION_EEA1_MASK) {
407
    return LTE_CipheringAlgorithm_r12_eea1;
408 409
  }

410
  return LTE_CipheringAlgorithm_r12_eea0;
411 412 413 414 415 416 417
}

/*! \fn e_SecurityAlgorithmConfig__integrityProtAlgorithm rrc_eNB_select_integrity(uint16_t algorithms)
 *\brief analyze available integrity algorithms bit mask and return the relevant one.
 *\param algorithms The bit mask of available algorithms received from S1AP.
 *\return the selected algorithm.
 */
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
418
static e_LTE_SecurityAlgorithmConfig__integrityProtAlgorithm rrc_eNB_select_integrity(uint16_t algorithms) {
419
  if (algorithms & S1AP_INTEGRITY_EIA2_MASK) {
420
    return LTE_SecurityAlgorithmConfig__integrityProtAlgorithm_eia2;
421 422 423
  }

  if (algorithms & S1AP_INTEGRITY_EIA1_MASK) {
424
    return LTE_SecurityAlgorithmConfig__integrityProtAlgorithm_eia1;
425 426 427 428 429 430 431 432 433 434 435 436
  }

  return INTEGRITY_ALGORITHM_NONE;
}

/*! \fn int rrc_eNB_process_security (uint8_t mod_id, uint8_t ue_index, security_capabilities_t *security_capabilities)
 *\brief save and analyze available security algorithms bit mask and select relevant ones.
 *\param mod_id Instance ID of eNB.
 *\param ue_index Instance ID of UE in the eNB.
 *\param security_capabilities The security capabilities received from S1AP.
 *\return TRUE if at least one algorithm has been changed else FALSE.
 */
437
int
438
rrc_eNB_process_security(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
439 440 441 442
  const protocol_ctxt_t *const ctxt_pP,
  rrc_eNB_ue_context_t          *const ue_context_pP,
  security_capabilities_t *security_capabilities_pP
) {
443 444 445
  boolean_t                                             changed = FALSE;
  LTE_CipheringAlgorithm_r12_t                          cipheringAlgorithm;
  e_LTE_SecurityAlgorithmConfig__integrityProtAlgorithm integrityProtAlgorithm;
446
  /* Save security parameters */
447
  ue_context_pP->ue_context.security_capabilities = *security_capabilities_pP;
448 449
  // translation
  LOG_D(RRC,
Cedric Roux's avatar
Cedric Roux committed
450
        "[eNB %d] NAS security_capabilities.encryption_algorithms %u AS ciphering_algorithm %lu NAS security_capabilities.integrity_algorithms %u AS integrity_algorithm %u\n",
451 452
        ctxt_pP->module_id,
        ue_context_pP->ue_context.security_capabilities.encryption_algorithms,
Cedric Roux's avatar
Cedric Roux committed
453
        (unsigned long)ue_context_pP->ue_context.ciphering_algorithm,
454 455
        ue_context_pP->ue_context.security_capabilities.integrity_algorithms,
        ue_context_pP->ue_context.integrity_algorithm);
456
  /* Select relevant algorithms */
457
  cipheringAlgorithm = rrc_eNB_select_ciphering (ue_context_pP->ue_context.security_capabilities.encryption_algorithms);
458

459 460
  if (ue_context_pP->ue_context.ciphering_algorithm != cipheringAlgorithm) {
    ue_context_pP->ue_context.ciphering_algorithm = cipheringAlgorithm;
461
    changed = TRUE;
462 463
  }

464
  integrityProtAlgorithm = rrc_eNB_select_integrity (ue_context_pP->ue_context.security_capabilities.integrity_algorithms);
465

466 467
  if (ue_context_pP->ue_context.integrity_algorithm != integrityProtAlgorithm) {
    ue_context_pP->ue_context.integrity_algorithm = integrityProtAlgorithm;
468
    changed = TRUE;
469 470
  }

Cedric Roux's avatar
Cedric Roux committed
471
  LOG_I (RRC, "[eNB %d][UE %x] Selected security algorithms (%p): %lx, %x, %s\n",
472 473 474
         ctxt_pP->module_id,
         ue_context_pP->ue_context.rnti,
         security_capabilities_pP,
Cedric Roux's avatar
Cedric Roux committed
475
         (unsigned long)cipheringAlgorithm,
476 477
         integrityProtAlgorithm,
         changed ? "changed" : "same");
478 479 480
  return changed;
}

481
/*! \fn void process_eNB_security_key (const protocol_ctxt_t* const ctxt_pP, eNB_RRC_UE_t * const ue_context_pP, uint8_t *security_key)
482
 *\brief save security key.
483 484 485
 *\param ctxt_pP         Running context.
 *\param ue_context_pP   UE context.
 *\param security_key_pP The security key received from S1AP.
486
 */
487
//------------------------------------------------------------------------------
488
void process_eNB_security_key (
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
489 490 491
  const protocol_ctxt_t *const ctxt_pP,
  rrc_eNB_ue_context_t          *const ue_context_pP,
  uint8_t               *security_key_pP
492 493
)
//------------------------------------------------------------------------------
494
{
495 496 497
  char ascii_buffer[65];
  uint8_t i;
  /* Saves the security key */
498
  memcpy (ue_context_pP->ue_context.kenb, security_key_pP, SECURITY_KEY_LENGTH);
499 500
  memset (ue_context_pP->ue_context.nh, 0, SECURITY_KEY_LENGTH);
  ue_context_pP->ue_context.nh_ncc = -1;
501 502

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

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

510

511
//------------------------------------------------------------------------------
512
void
513
rrc_pdcp_config_security(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
514 515
  const protocol_ctxt_t *const ctxt_pP,
  rrc_eNB_ue_context_t          *const ue_context_pP,
516 517 518
  const uint8_t send_security_mode_command
)
//------------------------------------------------------------------------------
519
{
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
520
  LTE_SRB_ToAddModList_t             *SRB_configList = ue_context_pP->ue_context.SRB_configList;
521 522 523 524
  uint8_t                            *kRRCenc = NULL;
  uint8_t                            *kRRCint = NULL;
  uint8_t                            *kUPenc = NULL;
  pdcp_t                             *pdcp_p   = NULL;
525
  static int                          print_keys= 1;
526 527
  hashtable_rc_t                      h_rc;
  hash_key_t                          key;
528

529 530
  /* Derive the keys from kenb */
  if (SRB_configList != NULL) {
531 532 533
    derive_key_up_enc(ue_context_pP->ue_context.ciphering_algorithm,
                      ue_context_pP->ue_context.kenb,
                      &kUPenc);
534
  }
535

536 537 538 539 540 541
  derive_key_rrc_enc(ue_context_pP->ue_context.ciphering_algorithm,
                     ue_context_pP->ue_context.kenb,
                     &kRRCenc);
  derive_key_rrc_int(ue_context_pP->ue_context.integrity_algorithm,
                     ue_context_pP->ue_context.kenb,
                     &kRRCint);
542
#if !defined(USRP_REC_PLAY)
543
  SET_LOG_DUMP(DEBUG_SECURITY) ;
544
#endif
545

546 547 548 549 550 551
  if ( LOG_DUMPFLAG( DEBUG_SECURITY ) ) {
    if (print_keys ==1 ) {
      print_keys =0;
      LOG_DUMPMSG(RRC, DEBUG_SECURITY, ue_context_pP->ue_context.kenb, 32,"\nKeNB:" );
      LOG_DUMPMSG(RRC, DEBUG_SECURITY, kRRCenc, 32,"\nKRRCenc:" );
      LOG_DUMPMSG(RRC, DEBUG_SECURITY, kRRCint, 32,"\nKRRCint:" );
552
    }
553 554
  }

555
  key = PDCP_COLL_KEY_VALUE(ctxt_pP->module_id, ctxt_pP->rnti, ctxt_pP->enb_flag, DCCH, SRB_FLAG_YES);
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
556
  h_rc = hashtable_get(pdcp_coll_p, key, (void **)&pdcp_p);
557

558
  if (h_rc == HASH_TABLE_OK) {
559
    pdcp_config_set_security(
560
      ctxt_pP,
561 562 563 564
      pdcp_p,
      DCCH,
      DCCH+2,
      (send_security_mode_command == TRUE)  ?
565 566 567
      0 | (ue_context_pP->ue_context.integrity_algorithm << 4) :
      (ue_context_pP->ue_context.ciphering_algorithm )         |
      (ue_context_pP->ue_context.integrity_algorithm << 4),
568 569 570
      kRRCenc,
      kRRCint,
      kUPenc);
571 572 573 574 575 576
  } else {
    LOG_E(RRC,
          PROTOCOL_RRC_CTXT_UE_FMT"Could not get PDCP instance for SRB DCCH %u\n",
          PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
          DCCH);
  }
577 578
}

579 580 581
//------------------------------------------------------------------------------
void
rrc_eNB_send_S1AP_INITIAL_CONTEXT_SETUP_RESP(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
582 583
  const protocol_ctxt_t *const ctxt_pP,
  rrc_eNB_ue_context_t          *const ue_context_pP
584 585
)
//------------------------------------------------------------------------------
586
{
587
  MessageDef      *msg_p         = NULL;
winckel's avatar
winckel committed
588 589 590 591
  int e_rab;
  int e_rabs_done = 0;
  int e_rabs_failed = 0;
  msg_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_INITIAL_CONTEXT_SETUP_RESP);
592
  S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).eNB_ue_s1ap_id = ue_context_pP->ue_context.eNB_ue_s1ap_id;
593

594 595
  for (e_rab = 0; e_rab < ue_context_pP->ue_context.nb_of_e_rabs; e_rab++) {
    if (ue_context_pP->ue_context.e_rab[e_rab].status == E_RAB_STATUS_DONE) {
winckel's avatar
winckel committed
596
      e_rabs_done++;
597
      S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).e_rabs[e_rab].e_rab_id = ue_context_pP->ue_context.e_rab[e_rab].param.e_rab_id;
winckel's avatar
winckel committed
598
      // TODO add other information from S1-U when it will be integrated
599 600
      S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).e_rabs[e_rab].gtp_teid = ue_context_pP->ue_context.enb_gtp_teid[e_rab];
      S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).e_rabs[e_rab].eNB_addr = ue_context_pP->ue_context.enb_gtp_addrs[e_rab];
601
      S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).e_rabs[e_rab].eNB_addr.length = 4;
602
      ue_context_pP->ue_context.e_rab[e_rab].status = E_RAB_STATUS_ESTABLISHED;
603
    } else {
winckel's avatar
winckel committed
604
      e_rabs_failed++;
605
      ue_context_pP->ue_context.e_rab[e_rab].status = E_RAB_STATUS_FAILED;
606
      S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).e_rabs_failed[e_rab].e_rab_id = ue_context_pP->ue_context.e_rab[e_rab].param.e_rab_id;
winckel's avatar
winckel committed
607 608 609
      // TODO add cause when it will be integrated
    }
  }
610 611

  MSC_LOG_TX_MESSAGE(
612 613 614 615 616 617 618 619 620
    MSC_RRC_ENB,
    MSC_S1AP_ENB,
    (const char *)&S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p),
    sizeof(s1ap_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,
    S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).eNB_ue_s1ap_id,
    e_rabs_done, e_rabs_failed);
winckel's avatar
winckel committed
621 622
  S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).nb_of_e_rabs = e_rabs_done;
  S1AP_INITIAL_CONTEXT_SETUP_RESP (msg_p).nb_of_e_rabs_failed = e_rabs_failed;
623
  itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, msg_p);
winckel's avatar
winckel committed
624 625
}

626 627 628
//------------------------------------------------------------------------------
void
rrc_eNB_send_S1AP_UPLINK_NAS(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
629 630 631
  const protocol_ctxt_t    *const ctxt_pP,
  rrc_eNB_ue_context_t             *const ue_context_pP,
  LTE_UL_DCCH_Message_t        *const ul_dcch_msg
632 633
)
//------------------------------------------------------------------------------
634
{
winckel's avatar
winckel committed
635
  {
636
    LTE_ULInformationTransfer_t *ulInformationTransfer = &ul_dcch_msg->message.choice.c1.choice.ulInformationTransfer;
winckel's avatar
winckel committed
637

638
    if ((ulInformationTransfer->criticalExtensions.present == LTE_ULInformationTransfer__criticalExtensions_PR_c1)
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
639 640 641 642
        && (ulInformationTransfer->criticalExtensions.choice.c1.present
            == LTE_ULInformationTransfer__criticalExtensions__c1_PR_ulInformationTransfer_r8)
        && (ulInformationTransfer->criticalExtensions.choice.c1.choice.ulInformationTransfer_r8.dedicatedInfoType.present
            == LTE_ULInformationTransfer_r8_IEs__dedicatedInfoType_PR_dedicatedInfoNAS)) {
winckel's avatar
winckel committed
643
      /* This message hold a dedicated info NAS payload, forward it to NAS */
644
      struct LTE_ULInformationTransfer_r8_IEs__dedicatedInfoType *dedicatedInfoType =
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
645
        &ulInformationTransfer->criticalExtensions.choice.c1.choice.ulInformationTransfer_r8.dedicatedInfoType;
winckel's avatar
winckel committed
646 647 648 649 650 651
      uint32_t pdu_length;
      uint8_t *pdu_buffer;
      MessageDef *msg_p;
      pdu_length = dedicatedInfoType->choice.dedicatedInfoNAS.size;
      pdu_buffer = dedicatedInfoType->choice.dedicatedInfoNAS.buf;
      msg_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_UPLINK_NAS);
652
      S1AP_UPLINK_NAS (msg_p).eNB_ue_s1ap_id = ue_context_pP->ue_context.eNB_ue_s1ap_id;
winckel's avatar
winckel committed
653 654
      S1AP_UPLINK_NAS (msg_p).nas_pdu.length = pdu_length;
      S1AP_UPLINK_NAS (msg_p).nas_pdu.buffer = pdu_buffer;
655 656 657
      extract_imsi(S1AP_UPLINK_NAS (msg_p).nas_pdu.buffer,
                   S1AP_UPLINK_NAS (msg_p).nas_pdu.length,
                   ue_context_pP);
658
      itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, msg_p);
winckel's avatar
winckel committed
659 660 661 662
    }
  }
}

663 664
//------------------------------------------------------------------------------
void rrc_eNB_send_S1AP_UE_CAPABILITIES_IND(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
665 666 667
  const protocol_ctxt_t *const ctxt_pP,
  rrc_eNB_ue_context_t          *const ue_context_pP,
  LTE_UL_DCCH_Message_t *ul_dcch_msg
668 669
)
//------------------------------------------------------------------------------
670
{
671
  LTE_UECapabilityInformation_t *ueCapabilityInformation = &ul_dcch_msg->message.choice.c1.choice.ueCapabilityInformation;
Cedric Roux's avatar
Cedric Roux committed
672 673 674
  /* 4096 is arbitrary, should be big enough */
  unsigned char buf[4096];
  unsigned char *buf2;
675
  LTE_UERadioAccessCapabilityInformation_t rac;
Cedric Roux's avatar
Cedric Roux committed
676

677 678
  if (ueCapabilityInformation->criticalExtensions.present != LTE_UECapabilityInformation__criticalExtensions_PR_c1
      || ueCapabilityInformation->criticalExtensions.choice.c1.present != LTE_UECapabilityInformation__criticalExtensions__c1_PR_ueCapabilityInformation_r8) {
Cedric Roux's avatar
Cedric Roux committed
679 680
    LOG_E(RRC, "[eNB %d][UE %x] bad UE capabilities\n", ctxt_pP->module_id, ue_context_pP->ue_context.rnti);
    return;
681
  }
Cedric Roux's avatar
Cedric Roux committed
682

683
  asn_enc_rval_t ret = uper_encode_to_buffer(&asn_DEF_LTE_UECapabilityInformation, NULL, ueCapabilityInformation, buf, 4096);
684

Cedric Roux's avatar
Cedric Roux committed
685 686
  if (ret.encoded == -1) abort();

687 688 689
  memset(&rac, 0, sizeof(LTE_UERadioAccessCapabilityInformation_t));
  rac.criticalExtensions.present = LTE_UERadioAccessCapabilityInformation__criticalExtensions_PR_c1;
  rac.criticalExtensions.choice.c1.present = LTE_UERadioAccessCapabilityInformation__criticalExtensions__c1_PR_ueRadioAccessCapabilityInformation_r8;
Cedric Roux's avatar
Cedric Roux committed
690 691 692 693 694
  rac.criticalExtensions.choice.c1.choice.ueRadioAccessCapabilityInformation_r8.ue_RadioAccessCapabilityInfo.buf = buf;
  rac.criticalExtensions.choice.c1.choice.ueRadioAccessCapabilityInformation_r8.ue_RadioAccessCapabilityInfo.size = (ret.encoded+7)/8;
  rac.criticalExtensions.choice.c1.choice.ueRadioAccessCapabilityInformation_r8.nonCriticalExtension = NULL;
  /* 8192 is arbitrary, should be big enough */
  buf2 = malloc16(8192);
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
695

Cedric Roux's avatar
Cedric Roux committed
696
  if (buf2 == NULL) abort();
697

698
  ret = uper_encode_to_buffer(&asn_DEF_LTE_UERadioAccessCapabilityInformation, NULL, &rac, buf2, 8192);
699

Cedric Roux's avatar
Cedric Roux committed
700 701 702 703 704 705 706 707
  if (ret.encoded == -1) abort();

  MessageDef *msg_p;
  msg_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_UE_CAPABILITIES_IND);
  S1AP_UE_CAPABILITIES_IND (msg_p).eNB_ue_s1ap_id = ue_context_pP->ue_context.eNB_ue_s1ap_id;
  S1AP_UE_CAPABILITIES_IND (msg_p).ue_radio_cap.length = (ret.encoded+7)/8;
  S1AP_UE_CAPABILITIES_IND (msg_p).ue_radio_cap.buffer = buf2;
  itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, msg_p);
708 709
}

710
//------------------------------------------------------------------------------
711
/*
712
* Initial UE NAS message on S1AP.
713
*/
714 715
void
rrc_eNB_send_S1AP_NAS_FIRST_REQ(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
716 717 718
  const protocol_ctxt_t *const ctxt_pP,
  rrc_eNB_ue_context_t *const ue_context_pP,
  LTE_RRCConnectionSetupComplete_r8_IEs_t *rrcConnectionSetupComplete
719 720
)
//------------------------------------------------------------------------------
721
{
722
  eNB_RRC_INST *rrc = RC.rrc[ctxt_pP->module_id];
winckel's avatar
winckel committed
723
  {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
724 725
    MessageDef         *message_p         = NULL;
    rrc_ue_s1ap_ids_t  *rrc_ue_s1ap_ids_p = NULL;
726
    hashtable_rc_t      h_rc;
727
    message_p = itti_alloc_new_message(TASK_RRC_ENB, S1AP_NAS_FIRST_REQ);
728
    memset(&message_p->ittiMsg.s1ap_nas_first_req, 0, sizeof(s1ap_nas_first_req_t));
729 730
    ue_context_pP->ue_context.ue_initial_id = get_next_ue_initial_id(ctxt_pP->module_id);
    S1AP_NAS_FIRST_REQ(message_p).ue_initial_id = ue_context_pP->ue_context.ue_initial_id;
731 732 733 734
    rrc_ue_s1ap_ids_p = malloc(sizeof(*rrc_ue_s1ap_ids_p));
    rrc_ue_s1ap_ids_p->ue_initial_id  = ue_context_pP->ue_context.ue_initial_id;
    rrc_ue_s1ap_ids_p->eNB_ue_s1ap_id = UE_INITIAL_ID_INVALID;
    rrc_ue_s1ap_ids_p->ue_rnti        = ctxt_pP->rnti;
735
    h_rc = hashtable_insert(RC.rrc[ctxt_pP->module_id]->initial_id2_s1ap_ids,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
736 737
                            (hash_key_t)ue_context_pP->ue_context.ue_initial_id,
                            rrc_ue_s1ap_ids_p);
738

739
    if (h_rc != HASH_TABLE_OK) {
740
      LOG_E(S1AP, "[eNB %d] Error while hashtable_insert in initial_id2_s1ap_ids ue_initial_id %u\n",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
741 742
            ctxt_pP->module_id,
            ue_context_pP->ue_context.ue_initial_id);
743
    }
winckel's avatar
winckel committed
744 745

    /* Assume that cause is coded in the same way in RRC and S1ap, just check that the value is in S1ap range */
746
    AssertFatal(ue_context_pP->ue_context.establishment_cause < RRC_CAUSE_LAST,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
747 748 749 750
                "Establishment cause invalid (%jd/%d) for eNB %d!",
                ue_context_pP->ue_context.establishment_cause,
                RRC_CAUSE_LAST,
                ctxt_pP->module_id);
751
    S1AP_NAS_FIRST_REQ (message_p).establishment_cause = ue_context_pP->ue_context.establishment_cause;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
752
    /* Forward NAS message */
753
    S1AP_NAS_FIRST_REQ (message_p).nas_pdu.buffer = rrcConnectionSetupComplete->dedicatedInfoNAS.buf;
winckel's avatar
winckel committed
754
    S1AP_NAS_FIRST_REQ (message_p).nas_pdu.length = rrcConnectionSetupComplete->dedicatedInfoNAS.size;
755 756 757
    extract_imsi(S1AP_NAS_FIRST_REQ (message_p).nas_pdu.buffer,
                 S1AP_NAS_FIRST_REQ (message_p).nas_pdu.length,
                 ue_context_pP);
winckel's avatar
winckel committed
758 759 760 761
    /* Fill UE identities with available information */
    {
      S1AP_NAS_FIRST_REQ (message_p).ue_identity.presenceMask = UE_IDENTITIES_NONE;

762
      if (ue_context_pP->ue_context.Initialue_identity_s_TMSI.presence) {
winckel's avatar
winckel committed
763
        /* Fill s-TMSI */
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
764
        UE_S_TMSI *s_TMSI = &ue_context_pP->ue_context.Initialue_identity_s_TMSI;
winckel's avatar
winckel committed
765 766 767
        S1AP_NAS_FIRST_REQ (message_p).ue_identity.presenceMask |= UE_IDENTITIES_s_tmsi;
        S1AP_NAS_FIRST_REQ (message_p).ue_identity.s_tmsi.mme_code = s_TMSI->mme_code;
        S1AP_NAS_FIRST_REQ (message_p).ue_identity.s_tmsi.m_tmsi = s_TMSI->m_tmsi;
768
        LOG_I(S1AP, "[eNB %d] Build S1AP_NAS_FIRST_REQ with s_TMSI: MME code %u M-TMSI %u ue %x\n",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
769 770 771 772
              ctxt_pP->module_id,
              S1AP_NAS_FIRST_REQ (message_p).ue_identity.s_tmsi.mme_code,
              S1AP_NAS_FIRST_REQ (message_p).ue_identity.s_tmsi.m_tmsi,
              ue_context_pP->ue_context.rnti);
773
      } // end if S-TMSI presence
winckel's avatar
winckel committed
774

775 776
      /* selected_plmn_identity: IE is 1-based, convert to 0-based (C array) */
      int selected_plmn_identity = rrcConnectionSetupComplete->selectedPLMN_Identity - 1;
777
      S1AP_NAS_FIRST_REQ(message_p).selected_plmn_identity = selected_plmn_identity;
778

winckel's avatar
winckel committed
779 780
      if (rrcConnectionSetupComplete->registeredMME != NULL) {
        /* Fill GUMMEI */
781
        struct LTE_RegisteredMME *r_mme = rrcConnectionSetupComplete->registeredMME;
winckel's avatar
winckel committed
782
        S1AP_NAS_FIRST_REQ (message_p).ue_identity.presenceMask |= UE_IDENTITIES_gummei;
783

winckel's avatar
winckel committed
784 785 786
        if (r_mme->plmn_Identity != NULL) {
          if ((r_mme->plmn_Identity->mcc != NULL) && (r_mme->plmn_Identity->mcc->list.count > 0)) {
            /* Use first indicated PLMN MCC if it is defined */
787
            S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mcc = *r_mme->plmn_Identity->mcc->list.array[selected_plmn_identity];
788
            LOG_I(S1AP, "[eNB %d] Build S1AP_NAS_FIRST_REQ adding in s_TMSI: GUMMEI MCC %u ue %x\n",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
789 790 791
                  ctxt_pP->module_id,
                  S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mcc,
                  ue_context_pP->ue_context.rnti);
winckel's avatar
winckel committed
792
          }
793

winckel's avatar
winckel committed
794 795
          if (r_mme->plmn_Identity->mnc.list.count > 0) {
            /* Use first indicated PLMN MNC if it is defined */
796
            S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mnc = *r_mme->plmn_Identity->mnc.list.array[selected_plmn_identity];
797 798
            LOG_I(S1AP, "[eNB %d] Build S1AP_NAS_FIRST_REQ adding in s_TMSI: GUMMEI MNC %u ue %x\n",
                  ctxt_pP->module_id,
799
                  S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mnc,
800
                  ue_context_pP->ue_context.rnti);
winckel's avatar
winckel committed
801
          }
802
        } else { // end if plmn_Identity != NULL
803 804 805
          S1AP_NAS_FIRST_REQ(message_p).ue_identity.gummei.mcc = rrc->configuration.mcc[selected_plmn_identity];
          S1AP_NAS_FIRST_REQ(message_p).ue_identity.gummei.mnc = rrc->configuration.mnc[selected_plmn_identity];
          S1AP_NAS_FIRST_REQ(message_p).ue_identity.gummei.mnc_len = rrc->configuration.mnc_digit_length[selected_plmn_identity];
806
        } // end else (plmn_Identity == NULL)
807

Lionel Gauthier's avatar
Lionel Gauthier committed
808
        S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mme_code     = BIT_STRING_to_uint8 (&r_mme->mmec);
winckel's avatar
winckel committed
809
        S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mme_group_id = BIT_STRING_to_uint16 (&r_mme->mmegi);
810 811 812 813 814
        ue_context_pP->ue_context.ue_gummei.mcc = S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mcc;
        ue_context_pP->ue_context.ue_gummei.mnc = S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mnc;
        ue_context_pP->ue_context.ue_gummei.mnc_len = S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mnc_len;
        ue_context_pP->ue_context.ue_gummei.mme_code = S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mme_code;
        ue_context_pP->ue_context.ue_gummei.mme_group_id = S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mme_group_id;
815
        MSC_LOG_TX_MESSAGE(MSC_S1AP_ENB,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
816 817 818 819 820 821 822
                           MSC_S1AP_MME,
                           (const char *)&message_p->ittiMsg.s1ap_nas_first_req,
                           sizeof(s1ap_nas_first_req_t),
                           MSC_AS_TIME_FMT" S1AP_NAS_FIRST_REQ eNB %u UE %x",
                           MSC_AS_TIME_ARGS(ctxt_pP),
                           ctxt_pP->module_id,
                           ctxt_pP->rnti);
823 824
        LOG_I(S1AP, "[eNB %d] Build S1AP_NAS_FIRST_REQ adding in s_TMSI: GUMMEI mme_code %u mme_group_id %u ue %x\n",
              ctxt_pP->module_id,
825 826
              S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mme_code,
              S1AP_NAS_FIRST_REQ (message_p).ue_identity.gummei.mme_group_id,
827
              ue_context_pP->ue_context.rnti);
828 829
      } // end if MME info present
    } // end "Fill UE identities with available information" sub-part
830
    itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, message_p);
winckel's avatar
winckel committed
831 832 833
  }
}

834

835 836 837
//------------------------------------------------------------------------------
int
rrc_eNB_process_S1AP_DOWNLINK_NAS(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
838 839
  MessageDef *msg_p,
  const char *msg_name,
840
  instance_t instance,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
841
  mui_t *rrc_eNB_mui
842 843
)
//------------------------------------------------------------------------------
844
{
845 846
  uint16_t ue_initial_id;
  uint32_t eNB_ue_s1ap_id;
winckel's avatar
winckel committed
847 848
  uint32_t length;
  uint8_t *buffer;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
849 850
  uint8_t srb_id;
  struct rrc_eNB_ue_context_s *ue_context_p = NULL;
851
  protocol_ctxt_t              ctxt;
852 853
  ue_initial_id = S1AP_DOWNLINK_NAS (msg_p).ue_initial_id;
  eNB_ue_s1ap_id = S1AP_DOWNLINK_NAS (msg_p).eNB_ue_s1ap_id;
854 855 856 857 858 859
  ue_context_p = rrc_eNB_get_ue_context_from_s1ap_ids(instance, ue_initial_id, eNB_ue_s1ap_id);
  LOG_I(RRC, "[eNB %d] Received %s: ue_initial_id %d, eNB_ue_s1ap_id %d\n",
        instance,
        msg_name,
        ue_initial_id,
        eNB_ue_s1ap_id);
winckel's avatar
winckel committed
860

861
  if (ue_context_p == NULL) {
862
    MSC_LOG_RX_MESSAGE(
863 864 865 866 867 868 869 870
      MSC_RRC_ENB,
      MSC_S1AP_ENB,
      NULL,
      0,
      MSC_AS_TIME_FMT" DOWNLINK-NAS UE initial id %u eNB_ue_s1ap_id %u",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      ue_initial_id,
      eNB_ue_s1ap_id);
winckel's avatar
winckel committed
871 872
    /* Can not associate this message to an UE index, send a failure to S1AP and discard it! */
    MessageDef *msg_fail_p;
873
    LOG_W(RRC, "[eNB %d] In S1AP_DOWNLINK_NAS: unknown UE from S1AP ids (%d, %d)\n", instance, ue_initial_id, eNB_ue_s1ap_id);
winckel's avatar
winckel committed
874
    msg_fail_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_NAS_NON_DELIVERY_IND);
875
    S1AP_NAS_NON_DELIVERY_IND (msg_fail_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
winckel's avatar
winckel committed
876 877 878
    S1AP_NAS_NON_DELIVERY_IND (msg_fail_p).nas_pdu.length = S1AP_DOWNLINK_NAS (msg_p).nas_pdu.length;
    S1AP_NAS_NON_DELIVERY_IND (msg_fail_p).nas_pdu.buffer = S1AP_DOWNLINK_NAS (msg_p).nas_pdu.buffer;
    // TODO add failure cause when defined!
879
    MSC_LOG_TX_MESSAGE(
880 881 882 883 884 885 886 887
      MSC_RRC_ENB,
      MSC_S1AP_ENB,
      (const char *)NULL,
      0,
      MSC_AS_TIME_FMT" S1AP_NAS_NON_DELIVERY_IND UE initial id %u eNB_ue_s1ap_id %u (ue ctxt !found)",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      ue_initial_id,
      eNB_ue_s1ap_id);
winckel's avatar
winckel committed
888 889
    itti_send_msg_to_task (TASK_S1AP, instance, msg_fail_p);
    return (-1);
890
  } else {
891
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
Raymond.Knopp's avatar
Raymond.Knopp committed
892 893
    srb_id = ue_context_p->ue_context.Srb2.Srb_info.Srb_id;

894
    /* Is it the first income from S1AP ? */
895 896
    if (ue_context_p->ue_context.eNB_ue_s1ap_id == 0) {
      ue_context_p->ue_context.eNB_ue_s1ap_id = S1AP_DOWNLINK_NAS (msg_p).eNB_ue_s1ap_id;
897
    }
898 899

    MSC_LOG_RX_MESSAGE(
900 901 902 903 904 905 906 907
      MSC_RRC_ENB,
      MSC_S1AP_ENB,
      (const char *)NULL,
      0,
      MSC_AS_TIME_FMT" DOWNLINK-NAS UE initial id %u eNB_ue_s1ap_id %u",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      ue_initial_id,
      S1AP_DOWNLINK_NAS (msg_p).eNB_ue_s1ap_id);
908
    /* Create message for PDCP (DLInformationTransfer_t) */
909 910 911 912
    length = do_DLInformationTransfer (
               instance,
               &buffer,
               rrc_eNB_get_next_transaction_identifier (instance),
913 914
               S1AP_DOWNLINK_NAS (msg_p).nas_pdu.length,
               S1AP_DOWNLINK_NAS (msg_p).nas_pdu.buffer);
915
    LOG_DUMPMSG(RRC,DEBUG_RRC,buffer,length,"[MSG] RRC DL Information Transfer\n");
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
916 917
    /*
     * switch UL or DL NAS message without RRC piggybacked to SRB2 if active.
918
     */
winckel's avatar
winckel committed
919
    /* Transfer data to PDCP */
920
    rrc_data_req (
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
921 922 923 924 925 926 927
      &ctxt,
      srb_id,
      *rrc_eNB_mui++,
      SDU_CONFIRM_NO,
      length,
      buffer,
      PDCP_TRANSMISSION_MODE_CONTROL);
winckel's avatar
winckel committed
928 929 930 931 932
    return (0);
  }
}

/*------------------------------------------------------------------------------*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
933
int rrc_eNB_process_S1AP_INITIAL_CONTEXT_SETUP_REQ(MessageDef *msg_p, const char *msg_name, instance_t instance) {
934 935
  uint16_t                        ue_initial_id;
  uint32_t                        eNB_ue_s1ap_id;
936
  //MessageDef                     *message_gtpv1u_p = NULL;
937 938
  gtpv1u_enb_create_tunnel_req_t  create_tunnel_req;
  gtpv1u_enb_create_tunnel_resp_t create_tunnel_resp;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
939 940
  uint8_t                         inde_list[NB_RB_MAX - 3]= {0};
  struct rrc_eNB_ue_context_s *ue_context_p = NULL;
941
  protocol_ctxt_t              ctxt;
942 943
  ue_initial_id  = S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).ue_initial_id;
  eNB_ue_s1ap_id = S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).eNB_ue_s1ap_id;
944 945 946
  ue_context_p   = rrc_eNB_get_ue_context_from_s1ap_ids(instance, ue_initial_id, eNB_ue_s1ap_id);
  LOG_I(RRC, "[eNB %d] Received %s: ue_initial_id %d, eNB_ue_s1ap_id %d, nb_of_e_rabs %d\n",
        instance, msg_name, ue_initial_id, eNB_ue_s1ap_id, S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).nb_of_e_rabs);
winckel's avatar
winckel committed
947

948
  if (ue_context_p == NULL) {
949 950 951 952 953 954 955 956 957
    /* Can not associate this message to an UE index, send a failure to S1AP and discard it! */
    MessageDef *msg_fail_p = NULL;
    LOG_W(RRC, "[eNB %d] In S1AP_INITIAL_CONTEXT_SETUP_REQ: unknown UE from S1AP ids (%d, %d)\n", instance, ue_initial_id, eNB_ue_s1ap_id);
    msg_fail_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_INITIAL_CONTEXT_SETUP_FAIL);
    S1AP_INITIAL_CONTEXT_SETUP_FAIL (msg_fail_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
    // TODO add failure cause when defined!
    itti_send_msg_to_task (TASK_S1AP, instance, msg_fail_p);
    return (-1);
  } else {
958 959
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
    ue_context_p->ue_context.eNB_ue_s1ap_id = S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).eNB_ue_s1ap_id;
960
    ue_context_p->ue_context.mme_ue_s1ap_id = S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).mme_ue_s1ap_id;
961 962 963
    /* Save e RAB information for later */
    {
      int i;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
964
      memset(&create_tunnel_req, 0, sizeof(create_tunnel_req));
965
      ue_context_p->ue_context.nb_of_e_rabs = S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).nb_of_e_rabs;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
966

967 968 969
      for (i = 0; i < ue_context_p->ue_context.nb_of_e_rabs; i++) {
        ue_context_p->ue_context.e_rab[i].status = E_RAB_STATUS_NEW;
        ue_context_p->ue_context.e_rab[i].param = S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).e_rab_param[i];
970 971 972
        create_tunnel_req.eps_bearer_id[i]       = S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).e_rab_param[i].e_rab_id;
        create_tunnel_req.sgw_S1u_teid[i]        = S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).e_rab_param[i].gtp_teid;
        memcpy(&create_tunnel_req.sgw_addr[i],
973 974
               &S1AP_INITIAL_CONTEXT_SETUP_REQ (msg_p).e_rab_param[i].sgw_addr,
               sizeof(transport_layer_addr_t));
975 976
        inde_list[create_tunnel_req.num_tunnels]= i;
        create_tunnel_req.num_tunnels++;
977 978
      }

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
979 980
      create_tunnel_req.rnti       = ue_context_p->ue_context.rnti; // warning put zero above
      //      create_tunnel_req.num_tunnels    = i;
981 982 983 984 985
      gtpv1u_create_s1u_tunnel(
        instance,
        &create_tunnel_req,
        &create_tunnel_resp);
      rrc_eNB_process_GTPV1U_CREATE_TUNNEL_RESP(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
986 987 988
        &ctxt,
        &create_tunnel_resp,
        &inde_list[0]);
989
      ue_context_p->ue_context.setup_e_rabs=ue_context_p->ue_context.nb_of_e_rabs;
990 991 992
    }
    /* TODO parameters yet to process ... */
    {
993
      //      S1AP_INITIAL_CONTEXT_SETUP_REQ(msg_p).ue_ambr;
994
    }
995 996 997 998 999 1000 1001 1002
    rrc_eNB_process_security (
      &ctxt,
      ue_context_p,
      &S1AP_INITIAL_CONTEXT_SETUP_REQ(msg_p).security_capabilities);
    process_eNB_security_key (
      &ctxt,
      ue_context_p,
      S1AP_INITIAL_CONTEXT_SETUP_REQ(msg_p).security_key);
1003 1004
    {
      uint8_t send_security_mode_command = TRUE;
1005
#ifndef EXMIMO_IOT
1006

1007 1008
      if ((ue_context_p->ue_context.ciphering_algorithm == SecurityAlgorithmConfig__cipheringAlgorithm_eea0)
          && (ue_context_p->ue_context.integrity_algorithm == INTEGRITY_ALGORITHM_NONE)) {
1009 1010 1011
        send_security_mode_command = FALSE;
      }

1012
#endif
1013 1014 1015 1016
      rrc_pdcp_config_security(
        &ctxt,
        ue_context_p,
        send_security_mode_command);
1017

1018
      if (send_security_mode_command) {
1019 1020 1021
        rrc_eNB_generate_SecurityModeCommand (
          &ctxt,
          ue_context_p);
1022 1023
        send_security_mode_command = FALSE;
        // apply ciphering after RRC security command mode
1024 1025 1026 1027
        rrc_pdcp_config_security(
          &ctxt,
          ue_context_p,
          send_security_mode_command);
1028
      } else {
1029
        rrc_eNB_generate_UECapabilityEnquiry (&ctxt, ue_context_p);
1030
      }
winckel's avatar
winckel committed
1031
    }
1032

Navid Nikaein's avatar
Navid Nikaein committed
1033
    // in case, send the S1SP initial context response if it is not sent with the attach complete message
1034 1035
    if (ue_context_p->ue_context.Status == RRC_RECONFIGURED) {
       LOG_I(RRC, "Sending rrc_eNB_send_S1AP_INITIAL_CONTEXT_SETUP_RESP, cause %ld\n", ue_context_p->ue_context.reestablishment_cause);
Navid Nikaein's avatar
Navid Nikaein committed
1036
      //if(ue_context_p->ue_context.reestablishment_cause == ReestablishmentCause_spare1){}
1037 1038
       rrc_eNB_send_S1AP_INITIAL_CONTEXT_SETUP_RESP(&ctxt,ue_context_p);
    } 
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
/*
    if ((RC.rrc[ctxt.module_id]->node_type == ngran_eNB_CU) ||
        (RC.rrc[ctxt.module_id]->node_type == ngran_ng_eNB_CU) ||
        (RC.rrc[ctxt.module_id]->node_type == ngran_gNB_CU) ){

      message_p = itti_alloc_new_message (TASK_RRC_ENB, F1AP_UE_CONTEXT_SETUP_REQ);
      F1AP_UE_CONTEXT_SETUP_REQ (message_p).rrc_container =  ue_p->Srb0.Tx_buffer.Payload;

      F1AP_UE_CONTEXT_SETUP_REQ (message_p).rrc_container_length = ue_p->Srb0.Tx_buffer.payload_size;
      F1AP_UE_CONTEXT_SETUP_REQ (message_p).gNB_CU_ue_id     = 0;  
      F1AP_UE_CONTEXT_SETUP_REQ (message_p).gNB_DU_ue_id = 0;
      F1AP_UE_CONTEXT_SETUP_REQ (message_p).old_gNB_DU_ue_id  = 0xFFFFFFFF; // unknown 
      F1AP_UE_CONTEXT_SETUP_REQ (message_p).rnti = ue_p->rnti; 
      F1AP_UE_CONTEXT_SETUP_REQ (message_p).srb_id = CCCH;  
      F1AP_UE_CONTEXT_SETUP_REQ (message_p).execute_duplication      = 1;
      F1AP_UE_CONTEXT_SETUP_REQ (message_p).RAT_frequency_priority_information.en_dc      = 0; 
      itti_send_msg_to_task (TASK_CU_F1, ctxt_pP->module_id, message_p);
      LOG_D(RRC, "Send F1AP_UE_CONTEXT_SETUP_REQ with ITTI\n");

    }
*/

1061 1062
    return (0);
  }
winckel's avatar
winckel committed
1063 1064
}

1065
/*------------------------------------------------------------------------------*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1066
int rrc_eNB_process_S1AP_UE_CTXT_MODIFICATION_REQ(MessageDef *msg_p, const char *msg_name, instance_t instance) {
1067
  uint32_t eNB_ue_s1ap_id;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1068
  struct rrc_eNB_ue_context_s *ue_context_p = NULL;
1069
  protocol_ctxt_t              ctxt;
1070
  eNB_ue_s1ap_id = S1AP_UE_CTXT_MODIFICATION_REQ (msg_p).eNB_ue_s1ap_id;
1071
  ue_context_p   = rrc_eNB_get_ue_context_from_s1ap_ids(instance, UE_INITIAL_ID_INVALID, eNB_ue_s1ap_id);
1072

1073
  if (ue_context_p == NULL) {
1074 1075
    /* Can not associate this message to an UE index, send a failure to S1AP and discard it! */
    MessageDef *msg_fail_p;
Cedric Roux's avatar
Cedric Roux committed
1076
    LOG_W(RRC, "[eNB %d] In S1AP_UE_CTXT_MODIFICATION_REQ: unknown UE from eNB_ue_s1ap_id (%d)\n", instance, eNB_ue_s1ap_id);
1077 1078 1079 1080 1081
    msg_fail_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_UE_CTXT_MODIFICATION_FAIL);
    S1AP_UE_CTXT_MODIFICATION_FAIL (msg_fail_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
    // TODO add failure cause when defined!
    itti_send_msg_to_task (TASK_S1AP, instance, msg_fail_p);
    return (-1);
1082
  } else {
1083
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
1084 1085 1086
    /* TODO parameters yet to process ... */
    {
      if (S1AP_UE_CTXT_MODIFICATION_REQ(msg_p).present & S1AP_UE_CONTEXT_MODIFICATION_UE_AMBR) {
1087
        //        S1AP_UE_CTXT_MODIFICATION_REQ(msg_p).ue_ambr;
1088
      }
1089 1090 1091
    }

    if (S1AP_UE_CTXT_MODIFICATION_REQ(msg_p).present & S1AP_UE_CONTEXT_MODIFICATION_UE_SECU_CAP) {
1092 1093 1094 1095
      if (rrc_eNB_process_security (
            &ctxt,
            ue_context_p,
            &S1AP_UE_CTXT_MODIFICATION_REQ(msg_p).security_capabilities)) {
1096
        /* transmit the new security parameters to UE */
1097 1098 1099
        rrc_eNB_generate_SecurityModeCommand (
          &ctxt,
          ue_context_p);
1100 1101 1102 1103
      }
    }

    if (S1AP_UE_CTXT_MODIFICATION_REQ(msg_p).present & S1AP_UE_CONTEXT_MODIFICATION_SECURITY_KEY) {
1104 1105 1106 1107
      process_eNB_security_key (
        &ctxt,
        ue_context_p,
        S1AP_UE_CTXT_MODIFICATION_REQ(msg_p).security_key);
1108
      /* TODO reconfigure lower layers... */
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    }

    /* Send the response */
    {
      MessageDef *msg_resp_p;
      msg_resp_p = itti_alloc_new_message(TASK_RRC_ENB, S1AP_UE_CTXT_MODIFICATION_RESP);
      S1AP_UE_CTXT_MODIFICATION_RESP(msg_resp_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
      itti_send_msg_to_task(TASK_S1AP, instance, msg_resp_p);
    }
    return (0);
  }
}

/*------------------------------------------------------------------------------*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1123
int rrc_eNB_process_S1AP_UE_CONTEXT_RELEASE_REQ (MessageDef *msg_p, const char *msg_name, instance_t instance) {
1124
  uint32_t eNB_ue_s1ap_id;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1125
  struct rrc_eNB_ue_context_s *ue_context_p = NULL;
1126
  eNB_ue_s1ap_id = S1AP_UE_CONTEXT_RELEASE_REQ(msg_p).eNB_ue_s1ap_id;
1127
  ue_context_p   = rrc_eNB_get_ue_context_from_s1ap_ids(instance, UE_INITIAL_ID_INVALID, eNB_ue_s1ap_id);
1128

1129
  if (ue_context_p == NULL) {
1130 1131
    /* Can not associate this message to an UE index, send a failure to S1AP and discard it! */
    MessageDef *msg_fail_p;
Lionel Gauthier's avatar
Lionel Gauthier committed
1132
    LOG_W(RRC, "[eNB %d] In S1AP_UE_CONTEXT_RELEASE_REQ: unknown UE from eNB_ue_s1ap_id (%d)\n",
1133 1134
          instance,
          eNB_ue_s1ap_id);
1135 1136 1137 1138 1139
    msg_fail_p = itti_alloc_new_message(TASK_RRC_ENB, S1AP_UE_CONTEXT_RELEASE_RESP); /* TODO change message ID. */
    S1AP_UE_CONTEXT_RELEASE_RESP(msg_fail_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
    // TODO add failure cause when defined!
    itti_send_msg_to_task(TASK_S1AP, instance, msg_fail_p);
    return (-1);
1140
  } else {
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
    /* TODO release context. */
    /* Send the response */
    {
      MessageDef *msg_resp_p;
      msg_resp_p = itti_alloc_new_message(TASK_RRC_ENB, S1AP_UE_CONTEXT_RELEASE_RESP);
      S1AP_UE_CONTEXT_RELEASE_RESP(msg_resp_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
      itti_send_msg_to_task(TASK_S1AP, instance, msg_resp_p);
    }
    return (0);
  }
}

1153
//------------------------------------------------------------------------------
1154 1155 1156
/*
* Send the S1 command UE_CONTEXT_RELEASE_REQUEST to the MME.
*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1157
void
1158 1159 1160 1161 1162
rrc_eNB_send_S1AP_UE_CONTEXT_RELEASE_REQ(
  const module_id_t enb_mod_idP,
  const rrc_eNB_ue_context_t *const ue_context_pP,
  const s1ap_Cause_t causeP,
  const long cause_valueP)
1163
//------------------------------------------------------------------------------
1164
{
1165
  if (ue_context_pP == NULL) {
1166
    LOG_E(RRC, "[eNB] In S1AP_UE_CONTEXT_RELEASE_REQ: invalid UE\n");
Lionel Gauthier's avatar
Lionel Gauthier committed
1167
  } else {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1168 1169 1170 1171 1172 1173
    MSC_LOG_TX_MESSAGE(MSC_RRC_ENB,
                       MSC_S1AP_ENB,
                       NULL,
                       0,
                       "0 S1AP_UE_CONTEXT_RELEASE_REQ eNB_ue_s1ap_id 0x%06"PRIX32" ",
                       ue_context_pP->ue_context.eNB_ue_s1ap_id);
1174 1175
    MessageDef *msg_context_release_req_p = NULL;
    msg_context_release_req_p = itti_alloc_new_message(TASK_RRC_ENB, S1AP_UE_CONTEXT_RELEASE_REQ);
1176
    S1AP_UE_CONTEXT_RELEASE_REQ(msg_context_release_req_p).eNB_ue_s1ap_id = ue_context_pP->ue_context.eNB_ue_s1ap_id;
1177 1178
    S1AP_UE_CONTEXT_RELEASE_REQ(msg_context_release_req_p).cause          = causeP;
    S1AP_UE_CONTEXT_RELEASE_REQ(msg_context_release_req_p).cause_value    = cause_valueP;
1179
    itti_send_msg_to_task(TASK_S1AP, ENB_MODULE_ID_TO_INSTANCE(enb_mod_idP), msg_context_release_req_p);
Lionel Gauthier's avatar
Lionel Gauthier committed
1180 1181 1182
  }
}

1183 1184 1185 1186 1187
void rrc_eNB_send_S1AP_UE_CONTEXT_RELEASE_CPLT(
  module_id_t enb_mod_idP,
  uint32_t eNB_ue_s1ap_id
)
{
1188 1189 1190
  MSC_LOG_TX_MESSAGE(MSC_RRC_ENB, MSC_S1AP_ENB, NULL, 0,
                     "0 S1AP_UE_CONTEXT_RELEASE_COMPLETE eNB_ue_s1ap_id 0x%06"PRIX32" ",
                     eNB_ue_s1ap_id);
1191 1192 1193 1194 1195 1196

  MessageDef *msg = itti_alloc_new_message(TASK_RRC_ENB, S1AP_UE_CONTEXT_RELEASE_COMPLETE);
  S1AP_UE_CONTEXT_RELEASE_COMPLETE(msg).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
  itti_send_msg_to_task(TASK_S1AP, ENB_MODULE_ID_TO_INSTANCE(enb_mod_idP), msg);
}

Lionel Gauthier's avatar
Lionel Gauthier committed
1197

1198 1199 1200 1201 1202
//-----------------------------------------------------------------------------
/*
* Process the S1 command UE_CONTEXT_RELEASE_COMMAND, sent by MME.
* The eNB should remove all e-rab, S1 context, and other context of the UE.
*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1203
int
1204
rrc_eNB_process_S1AP_UE_CONTEXT_RELEASE_COMMAND(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1205 1206 1207 1208
  MessageDef *msg_p,
  const char *msg_name,
  instance_t instance) {
  //-----------------------------------------------------------------------------
1209
  uint32_t eNB_ue_s1ap_id = 0;
1210
  protocol_ctxt_t ctxt;
1211
  struct rrc_eNB_ue_context_s *ue_context_p = NULL;
1212
  struct rrc_ue_s1ap_ids_s *rrc_ue_s1ap_ids = NULL;
1213
  eNB_ue_s1ap_id = S1AP_UE_CONTEXT_RELEASE_COMMAND(msg_p).eNB_ue_s1ap_id;
1214
  ue_context_p = rrc_eNB_get_ue_context_from_s1ap_ids(instance, UE_INITIAL_ID_INVALID, eNB_ue_s1ap_id);
1215

1216
  if (ue_context_p == NULL) {
1217
    /* Can not associate this message to an UE index */
1218 1219
    MessageDef *msg_complete_p = NULL;
    LOG_W(RRC, "[eNB %d] In S1AP_UE_CONTEXT_RELEASE_COMMAND: unknown UE from eNB_ue_s1ap_id (%d)\n",
1220 1221
          instance,
          eNB_ue_s1ap_id);
1222
    MSC_LOG_EVENT(MSC_RRC_ENB, "0 S1AP_UE_CONTEXT_RELEASE_COMPLETE eNB_ue_s1ap_id 0x%06"PRIX32" context not found",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1223
                  eNB_ue_s1ap_id);
1224
    MSC_LOG_TX_MESSAGE(MSC_RRC_ENB,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1225 1226 1227 1228 1229
                       MSC_S1AP_ENB,
                       NULL,
                       0,
                       "0 S1AP_UE_CONTEXT_RELEASE_COMPLETE eNB_ue_s1ap_id 0x%06"PRIX32" ",
                       eNB_ue_s1ap_id);
1230 1231 1232
    msg_complete_p = itti_alloc_new_message(TASK_RRC_ENB, S1AP_UE_CONTEXT_RELEASE_COMPLETE);
    S1AP_UE_CONTEXT_RELEASE_COMPLETE(msg_complete_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
    itti_send_msg_to_task(TASK_S1AP, instance, msg_complete_p);
1233
    rrc_ue_s1ap_ids = rrc_eNB_S1AP_get_ue_ids(RC.rrc[instance], UE_INITIAL_ID_INVALID, eNB_ue_s1ap_id);
1234

1235
    if (rrc_ue_s1ap_ids != NULL) {
1236
      rrc_eNB_S1AP_remove_ue_ids(RC.rrc[instance], rrc_ue_s1ap_ids);
1237
    }
1238 1239

    return -1;
1240
  } else {
1241
    ue_context_p->ue_context.ue_release_timer_s1 = 0;
1242 1243
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
    rrc_eNB_generate_RRCConnectionRelease(&ctxt, ue_context_p);
1244
    return 0;
1245 1246 1247
  }
}

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1248
int rrc_eNB_process_S1AP_E_RAB_SETUP_REQ(MessageDef *msg_p, const char *msg_name, instance_t instance) {
1249 1250 1251 1252
  uint16_t                        ue_initial_id;
  uint32_t                        eNB_ue_s1ap_id;
  gtpv1u_enb_create_tunnel_req_t  create_tunnel_req;
  gtpv1u_enb_create_tunnel_resp_t create_tunnel_resp;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1253 1254
  uint8_t                         inde_list[NB_RB_MAX - 3]= {0};
  struct rrc_eNB_ue_context_s *ue_context_p = NULL;
1255
  protocol_ctxt_t              ctxt;
1256
  uint8_t                      e_rab_done;
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
  ue_initial_id  = S1AP_E_RAB_SETUP_REQ (msg_p).ue_initial_id;
  eNB_ue_s1ap_id = S1AP_E_RAB_SETUP_REQ (msg_p).eNB_ue_s1ap_id;
  ue_context_p   = rrc_eNB_get_ue_context_from_s1ap_ids(instance, ue_initial_id, eNB_ue_s1ap_id);
  LOG_I(RRC, "[eNB %d] Received %s: ue_initial_id %d, eNB_ue_s1ap_id %d, nb_of_e_rabs %d\n",
        instance, msg_name, ue_initial_id, eNB_ue_s1ap_id, S1AP_E_RAB_SETUP_REQ (msg_p).nb_e_rabs_tosetup);

  if (ue_context_p == NULL) {
    /* Can not associate this message to an UE index, send a failure to S1AP and discard it! */
    MessageDef *msg_fail_p = NULL;
    LOG_W(RRC, "[eNB %d] In S1AP_E_RAB_SETUP_REQ: unknown UE from S1AP ids (%d, %d)\n", instance, ue_initial_id, eNB_ue_s1ap_id);
    msg_fail_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_E_RAB_SETUP_REQUEST_FAIL);
    S1AP_E_RAB_SETUP_REQ  (msg_fail_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
    // TODO add failure cause when defined!
    itti_send_msg_to_task (TASK_S1AP, instance, msg_fail_p);
    return (-1);
  } else {
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
    ue_context_p->ue_context.eNB_ue_s1ap_id = S1AP_E_RAB_SETUP_REQ  (msg_p).eNB_ue_s1ap_id;
    /* Save e RAB information for later */
    {
      int i;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1278
      memset(&create_tunnel_req, 0, sizeof(create_tunnel_req));
1279
      uint8_t nb_e_rabs_tosetup = S1AP_E_RAB_SETUP_REQ  (msg_p).nb_e_rabs_tosetup;
1280
      e_rab_done = 0;
1281 1282 1283

      // keep the previous bearer
      // the index for the rec
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1284 1285 1286 1287 1288 1289
      for (i = 0;
           // i < nb_e_rabs_tosetup;
           i < NB_RB_MAX - 3;  // loop all e-rabs in e_rab[]
           i++) {
        //if (ue_context_p->ue_context.e_rab[i+ue_context_p->ue_context.setup_e_rabs].status == E_RAB_STATUS_DONE)
        //  LOG_W(RRC,"E-RAB already configured, reconfiguring\n");
1290 1291
        // check e-rab status, if e rab status is greater than E_RAB_STATUS_DONE, don't not config this one
        if(ue_context_p->ue_context.e_rab[i].status >= E_RAB_STATUS_DONE)
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1292 1293
          continue;

1294 1295 1296 1297 1298 1299 1300 1301
        //ue_context_p->ue_context.e_rab[i+ue_context_p->ue_context.setup_e_rabs].status = E_RAB_STATUS_NEW;
        //ue_context_p->ue_context.e_rab[i+ue_context_p->ue_context.setup_e_rabs].param = S1AP_E_RAB_SETUP_REQ  (msg_p).e_rab_setup_params[i];
        ue_context_p->ue_context.e_rab[i].status = E_RAB_STATUS_NEW;
        ue_context_p->ue_context.e_rab[i].param = S1AP_E_RAB_SETUP_REQ  (msg_p).e_rab_setup_params[e_rab_done];
        create_tunnel_req.eps_bearer_id[e_rab_done]       = S1AP_E_RAB_SETUP_REQ  (msg_p).e_rab_setup_params[e_rab_done].e_rab_id;
        create_tunnel_req.sgw_S1u_teid[e_rab_done]        = S1AP_E_RAB_SETUP_REQ  (msg_p).e_rab_setup_params[e_rab_done].gtp_teid;
        memcpy(&create_tunnel_req.sgw_addr[e_rab_done],
               & S1AP_E_RAB_SETUP_REQ (msg_p).e_rab_setup_params[e_rab_done].sgw_addr,
1302
               sizeof(transport_layer_addr_t));
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1303 1304 1305 1306
        LOG_I(RRC,"E_RAB setup REQ: local index %d teid %u, eps id %d \n",
              i,
              create_tunnel_req.sgw_S1u_teid[e_rab_done],
              create_tunnel_req.eps_bearer_id[i] );
1307
        inde_list[e_rab_done] = i;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1308 1309 1310 1311
        e_rab_done++;

        if(e_rab_done >= nb_e_rabs_tosetup) {
          break;
1312
        }
1313
      }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1314

1315 1316
      ue_context_p->ue_context.nb_of_e_rabs=nb_e_rabs_tosetup;
      create_tunnel_req.rnti       = ue_context_p->ue_context.rnti; // warning put zero above
1317
      create_tunnel_req.num_tunnels    = e_rab_done;
1318 1319 1320 1321 1322 1323
      // NN: not sure if we should create a new tunnel: need to check teid, etc.
      gtpv1u_create_s1u_tunnel(
        instance,
        &create_tunnel_req,
        &create_tunnel_resp);
      rrc_eNB_process_GTPV1U_CREATE_TUNNEL_RESP(
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1324 1325 1326
        &ctxt,
        &create_tunnel_resp,
        &inde_list[0]);
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
      ue_context_p->ue_context.setup_e_rabs+=nb_e_rabs_tosetup;
    }
    /* TODO parameters yet to process ... */
    {
      //      S1AP_INITIAL_CONTEXT_SETUP_REQ(msg_p).ue_ambr;
    }
    rrc_eNB_generate_dedicatedRRCConnectionReconfiguration(&ctxt, ue_context_p, 0);
    return (0);
  }
}

1338
/*NN: careful about the typcast of xid (long -> uint8_t*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1339 1340 1341
int rrc_eNB_send_S1AP_E_RAB_SETUP_RESP(const protocol_ctxt_t *const ctxt_pP,
                                       rrc_eNB_ue_context_t          *const ue_context_pP,
                                       uint8_t xid ) {
1342 1343 1344 1345 1346 1347
  MessageDef      *msg_p         = NULL;
  int e_rab;
  int e_rabs_done = 0;
  int e_rabs_failed = 0;
  msg_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_E_RAB_SETUP_RESP);
  S1AP_E_RAB_SETUP_RESP (msg_p).eNB_ue_s1ap_id = ue_context_pP->ue_context.eNB_ue_s1ap_id;
1348

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1349 1350
  for (e_rab = 0; e_rab <  ue_context_pP->ue_context.setup_e_rabs ; e_rab++) {
    /* only respond to the corresponding transaction */
1351
    //if (((xid+1)%4) == ue_context_pP->ue_context.e_rab[e_rab].xid) {
1352 1353
    if (xid == ue_context_pP->ue_context.e_rab[e_rab].xid) {
      if (ue_context_pP->ue_context.e_rab[e_rab].status == E_RAB_STATUS_DONE) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
        S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].e_rab_id = ue_context_pP->ue_context.e_rab[e_rab].param.e_rab_id;
        // TODO add other information from S1-U when it will be integrated
        S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].gtp_teid = ue_context_pP->ue_context.enb_gtp_teid[e_rab];
        S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].eNB_addr = ue_context_pP->ue_context.enb_gtp_addrs[e_rab];
        //S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rab].eNB_addr.length += 4;
        ue_context_pP->ue_context.e_rab[e_rab].status = E_RAB_STATUS_ESTABLISHED;
        LOG_I (RRC,"enb_gtp_addr (msg index %d, e_rab index %d, status %d, xid %d): nb_of_e_rabs %d,  e_rab_id %d, teid: %u, addr: %d.%d.%d.%d \n ",
               e_rabs_done,  e_rab, ue_context_pP->ue_context.e_rab[e_rab].status, xid,
               ue_context_pP->ue_context.nb_of_e_rabs,
               S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].e_rab_id,
               S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].gtp_teid,
               S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].eNB_addr.buffer[0],
               S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].eNB_addr.buffer[1],
               S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].eNB_addr.buffer[2],
               S1AP_E_RAB_SETUP_RESP (msg_p).e_rabs[e_rabs_done].eNB_addr.buffer[3]);
        e_rabs_done++;
      } else if ((ue_context_pP->ue_context.e_rab[e_rab].status == E_RAB_STATUS_NEW)  ||
                 (ue_context_pP->ue_context.e_rab[e_rab].status == E_RAB_STATUS_ESTABLISHED)) {
        LOG_D (RRC,"E-RAB is NEW or already ESTABLISHED\n");
      } else { /* to be improved */
        ue_context_pP->ue_context.e_rab[e_rab].status = E_RAB_STATUS_FAILED;
        S1AP_E_RAB_SETUP_RESP  (msg_p).e_rabs_failed[e_rabs_failed].e_rab_id = ue_context_pP->ue_context.e_rab[e_rab].param.e_rab_id;
        e_rabs_failed++;
        // TODO add cause when it will be integrated
      }

Navid Nikaein's avatar
Navid Nikaein committed
1380 1381
      S1AP_E_RAB_SETUP_RESP (msg_p).nb_of_e_rabs = e_rabs_done;
      S1AP_E_RAB_SETUP_RESP (msg_p).nb_of_e_rabs_failed = e_rabs_failed;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1382
      // NN: add conditions for e_rabs_failed
1383
    } else {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1384
      /*debug info for the xid */
1385
      LOG_D(RRC,"xid does not corresponds  (context e_rab index %d, status %d, xid %d/%d) \n ",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1386 1387
            e_rab, ue_context_pP->ue_context.e_rab[e_rab].status, xid, ue_context_pP->ue_context.e_rab[e_rab].xid);
    }
1388
  }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

  if ((e_rabs_done > 0) ) {
    LOG_I(RRC,"S1AP_E_RAB_SETUP_RESP: sending the message: nb_of_erabs %d, total e_rabs %d, index %d\n",
          ue_context_pP->ue_context.nb_of_e_rabs, ue_context_pP->ue_context.setup_e_rabs, e_rab);
    MSC_LOG_TX_MESSAGE(
      MSC_RRC_ENB,
      MSC_S1AP_ENB,
      (const char *)&S1AP_E_RAB_SETUP_RESP (msg_p),
      sizeof(s1ap_e_rab_setup_resp_t),
      MSC_AS_TIME_FMT" E_RAB_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,
      S1AP_E_RAB_SETUP_RESP (msg_p).eNB_ue_s1ap_id,
      e_rabs_done, e_rabs_failed);
    itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, msg_p);
  }

1406
  for(int i = 0; i < NB_RB_MAX; i++) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1407
    ue_context_pP->ue_context.e_rab[i].xid = -1;
1408
  }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1409

1410 1411 1412
  return 0;
}

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1413
int rrc_eNB_process_S1AP_E_RAB_MODIFY_REQ(MessageDef *msg_p, const char *msg_name, instance_t instance) {
1414 1415 1416
  int                             i;
  uint16_t                        ue_initial_id;
  uint32_t                        eNB_ue_s1ap_id;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1417
  struct rrc_eNB_ue_context_s *ue_context_p = NULL;
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
  protocol_ctxt_t              ctxt;
  ue_initial_id  = S1AP_E_RAB_MODIFY_REQ (msg_p).ue_initial_id;
  eNB_ue_s1ap_id = S1AP_E_RAB_MODIFY_REQ (msg_p).eNB_ue_s1ap_id;
  ue_context_p   = rrc_eNB_get_ue_context_from_s1ap_ids(instance, ue_initial_id, eNB_ue_s1ap_id);
  LOG_D(RRC, "[eNB %d] Received %s: ue_initial_id %d, eNB_ue_s1ap_id %d, nb_of_e_rabs %d\n",
        instance, msg_name, ue_initial_id, eNB_ue_s1ap_id, S1AP_E_RAB_MODIFY_REQ (msg_p).nb_e_rabs_tomodify);

  if (ue_context_p == NULL) {
    /* Can not associate this message to an UE index, send a failure to S1AP and discard it! */
    LOG_W(RRC, "[eNB %d] In S1AP_E_RAB_MODIFY_REQ: unknown UE from S1AP ids (%d, %d)\n", instance, ue_initial_id, eNB_ue_s1ap_id);
    int nb_of_e_rabs_failed = 0;
    MessageDef *msg_fail_p = NULL;
    msg_fail_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_E_RAB_MODIFY_RESP);
    S1AP_E_RAB_MODIFY_RESP (msg_fail_p).eNB_ue_s1ap_id = S1AP_E_RAB_MODIFY_REQ (msg_p).eNB_ue_s1ap_id;
    S1AP_E_RAB_MODIFY_RESP (msg_fail_p).nb_of_e_rabs = 0;

    for (nb_of_e_rabs_failed = 0; nb_of_e_rabs_failed < S1AP_E_RAB_MODIFY_REQ (msg_p).nb_e_rabs_tomodify; nb_of_e_rabs_failed++) {
      S1AP_E_RAB_MODIFY_RESP (msg_fail_p).e_rabs_failed[nb_of_e_rabs_failed].e_rab_id =
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1436
        S1AP_E_RAB_MODIFY_REQ (msg_p).e_rab_modify_params[nb_of_e_rabs_failed].e_rab_id;
1437 1438 1439 1440
      S1AP_E_RAB_MODIFY_RESP (msg_fail_p).e_rabs_failed[nb_of_e_rabs_failed].cause = S1AP_CAUSE_RADIO_NETWORK;
      S1AP_E_RAB_MODIFY_RESP (msg_fail_p).e_rabs_failed[nb_of_e_rabs_failed].cause_value = 31;//S1ap_CauseRadioNetwork_multiple_E_RAB_ID_instances;
    }

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1441
    S1AP_E_RAB_MODIFY_RESP (msg_fail_p).nb_of_e_rabs_failed = nb_of_e_rabs_failed;
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
    itti_send_msg_to_task(TASK_S1AP, instance, msg_fail_p);
    return (-1);
  } else {
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
    ue_context_p->ue_context.eNB_ue_s1ap_id = eNB_ue_s1ap_id;
    /* Save e RAB information for later */
    {
      int j;
      boolean_t is_treated[S1AP_MAX_E_RAB] = {FALSE};
      uint8_t nb_of_failed_e_rabs = 0;

      // keep the previous bearer
      // the index for the rec
      for (i = 0; i < S1AP_E_RAB_MODIFY_REQ (msg_p).nb_e_rabs_tomodify; i++) {
        if (is_treated[i] == TRUE) {
          // already treated
          continue;
        }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1460

1461 1462
        for (j = i+1; j < S1AP_E_RAB_MODIFY_REQ (msg_p).nb_e_rabs_tomodify; j++) {
          if (is_treated[j] == FALSE &&
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1463
              S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[j].e_rab_id == S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].e_rab_id) {
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
            // handle multiple E-RAB ID
            ue_context_p->ue_context.modify_e_rab[j].status = E_RAB_STATUS_NEW;
            ue_context_p->ue_context.modify_e_rab[j].param.e_rab_id = S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[j].e_rab_id;
            ue_context_p->ue_context.modify_e_rab[j].cause = S1AP_CAUSE_RADIO_NETWORK;
            ue_context_p->ue_context.modify_e_rab[j].cause_value = 31;//S1ap_CauseRadioNetwork_multiple_E_RAB_ID_instances;
            nb_of_failed_e_rabs++;
            is_treated[i] = TRUE;
            is_treated[j] = TRUE;
          }
        }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1474

1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
        if (is_treated[i] == TRUE) {
          // handle multiple E-RAB ID
          ue_context_p->ue_context.modify_e_rab[i].status = E_RAB_STATUS_NEW;
          ue_context_p->ue_context.modify_e_rab[i].param.e_rab_id = S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].e_rab_id;
          ue_context_p->ue_context.modify_e_rab[i].cause = S1AP_CAUSE_RADIO_NETWORK;
          ue_context_p->ue_context.modify_e_rab[i].cause_value = 31;//S1ap_CauseRadioNetwork_multiple_E_RAB_ID_instances;
          nb_of_failed_e_rabs++;
          continue;
        }

        if (S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].nas_pdu.length == 0) {
          // nas_pdu.length == 0
          ue_context_p->ue_context.modify_e_rab[i].status = E_RAB_STATUS_NEW;
          ue_context_p->ue_context.modify_e_rab[i].param.e_rab_id = S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].e_rab_id;
          ue_context_p->ue_context.modify_e_rab[i].cause = S1AP_CAUSE_NAS;
          ue_context_p->ue_context.modify_e_rab[i].cause_value = 3;//S1ap_CauseNas_unspecified;
          nb_of_failed_e_rabs++;
          is_treated[i] = TRUE;
          continue;
        }

        for (j = 0; j < NB_RB_MAX-3; j++) {
          if (ue_context_p->ue_context.e_rab[j].param.e_rab_id == S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].e_rab_id) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1498
            if(ue_context_p->ue_context.e_rab[j].status == E_RAB_STATUS_TORELEASE || ue_context_p->ue_context.e_rab[j].status == E_RAB_STATUS_DONE) {
1499 1500
              break;
            }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1501

1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
            ue_context_p->ue_context.modify_e_rab[i].status = E_RAB_STATUS_NEW;
            ue_context_p->ue_context.modify_e_rab[i].cause = S1AP_CAUSE_NOTHING;
            ue_context_p->ue_context.modify_e_rab[i].param.e_rab_id = S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].e_rab_id;
            ue_context_p->ue_context.modify_e_rab[i].param.qos =  S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].qos;
            ue_context_p->ue_context.modify_e_rab[i].param.nas_pdu.length = S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].nas_pdu.length;
            ue_context_p->ue_context.modify_e_rab[i].param.nas_pdu.buffer = S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].nas_pdu.buffer;
            ue_context_p->ue_context.modify_e_rab[i].param.sgw_addr = ue_context_p->ue_context.e_rab[j].param.sgw_addr;
            ue_context_p->ue_context.modify_e_rab[i].param.gtp_teid = ue_context_p->ue_context.e_rab[j].param.gtp_teid;
            is_treated[i] = TRUE;
            break;
          }
        }

        if (is_treated[i] == FALSE) {
          // handle Unknown E-RAB ID
          ue_context_p->ue_context.modify_e_rab[i].status = E_RAB_STATUS_NEW;
          ue_context_p->ue_context.modify_e_rab[i].param.e_rab_id = S1AP_E_RAB_MODIFY_REQ(msg_p).e_rab_modify_params[i].e_rab_id;
          ue_context_p->ue_context.modify_e_rab[i].cause = S1AP_CAUSE_RADIO_NETWORK;
          ue_context_p->ue_context.modify_e_rab[i].cause_value = 30;//S1ap_CauseRadioNetwork_unknown_E_RAB_ID;
          nb_of_failed_e_rabs++;
          is_treated[i] = TRUE;
        }
      }

      ue_context_p->ue_context.nb_of_modify_e_rabs = S1AP_E_RAB_MODIFY_REQ  (msg_p).nb_e_rabs_tomodify;
      ue_context_p->ue_context.nb_of_failed_e_rabs = nb_of_failed_e_rabs;
    }
    /* TODO parameters yet to process ... */
    {
      //      S1AP_INITIAL_CONTEXT_SETUP_REQ(msg_p).ue_ambr;
    }

    if (ue_context_p->ue_context.nb_of_failed_e_rabs < ue_context_p->ue_context.nb_of_modify_e_rabs) {
      if (0 == rrc_eNB_modify_dedicatedRRCConnectionReconfiguration(&ctxt, ue_context_p, 0)) {
        return (0);
      }
    }

    {
      int nb_of_e_rabs_failed = 0;
      MessageDef *msg_fail_p = NULL;
      msg_fail_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_E_RAB_MODIFY_RESP);
      S1AP_E_RAB_MODIFY_RESP (msg_fail_p).eNB_ue_s1ap_id = S1AP_E_RAB_MODIFY_REQ (msg_p).eNB_ue_s1ap_id;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1545
      //      S1AP_E_RAB_MODIFY_RESP (msg_fail_p).e_rabs[S1AP_MAX_E_RAB];
1546 1547 1548 1549
      S1AP_E_RAB_MODIFY_RESP (msg_fail_p).nb_of_e_rabs = 0;

      for(nb_of_e_rabs_failed = 0; nb_of_e_rabs_failed < ue_context_p->ue_context.nb_of_failed_e_rabs; nb_of_e_rabs_failed++) {
        S1AP_E_RAB_MODIFY_RESP (msg_fail_p).e_rabs_failed[nb_of_e_rabs_failed].e_rab_id =
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1550
          ue_context_p->ue_context.modify_e_rab[nb_of_e_rabs_failed].param.e_rab_id;
1551 1552 1553
        S1AP_E_RAB_MODIFY_RESP (msg_fail_p).e_rabs_failed[nb_of_e_rabs_failed].cause = ue_context_p->ue_context.modify_e_rab[nb_of_e_rabs_failed].cause;
      }

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1554
      S1AP_E_RAB_MODIFY_RESP (msg_fail_p).nb_of_e_rabs_failed = nb_of_e_rabs_failed;
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
      itti_send_msg_to_task (TASK_S1AP, instance, msg_fail_p);
      ue_context_p->ue_context.nb_of_modify_e_rabs = 0;
      ue_context_p->ue_context.nb_of_failed_e_rabs = 0;
      memset(ue_context_p->ue_context.modify_e_rab, 0, sizeof(ue_context_p->ue_context.modify_e_rab));
      return (0);
    }
  }  // end of ue_context_p != NULL
}

/*NN: careful about the typcast of xid (long -> uint8_t*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1565 1566 1567
int rrc_eNB_send_S1AP_E_RAB_MODIFY_RESP(const protocol_ctxt_t *const ctxt_pP,
                                        rrc_eNB_ue_context_t          *const ue_context_pP,
                                        uint8_t xid ) {
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
  MessageDef      *msg_p         = NULL;
  int i;
  int e_rab;
  int e_rabs_done = 0;
  int e_rabs_failed = 0;
  msg_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_E_RAB_MODIFY_RESP);
  S1AP_E_RAB_MODIFY_RESP (msg_p).eNB_ue_s1ap_id = ue_context_pP->ue_context.eNB_ue_s1ap_id;

  for (e_rab = 0; e_rab < ue_context_pP->ue_context.nb_of_modify_e_rabs; e_rab++) {
    /* only respond to the corresponding transaction */
    if (xid == ue_context_pP->ue_context.modify_e_rab[e_rab].xid) {
      if (ue_context_pP->ue_context.modify_e_rab[e_rab].status == E_RAB_STATUS_DONE) {
        for (i = 0; i < ue_context_pP->ue_context.setup_e_rabs; i++) {
          if (ue_context_pP->ue_context.modify_e_rab[e_rab].param.e_rab_id == ue_context_pP->ue_context.e_rab[i].param.e_rab_id) {
            // Update ue_context_pP->ue_context.e_rab
            ue_context_pP->ue_context.e_rab[i].status = E_RAB_STATUS_ESTABLISHED;
            ue_context_pP->ue_context.e_rab[i].param.qos = ue_context_pP->ue_context.modify_e_rab[e_rab].param.qos;
            ue_context_pP->ue_context.e_rab[i].cause = S1AP_CAUSE_NOTHING;
            break;
          }
        }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1589

1590 1591
        if (i < ue_context_pP->ue_context.setup_e_rabs) {
          S1AP_E_RAB_MODIFY_RESP (msg_p).e_rabs[e_rabs_done].e_rab_id = ue_context_pP->ue_context.modify_e_rab[e_rab].param.e_rab_id;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1592
          // TODO add other information from S1-U when it will be integrated
1593
          LOG_D (RRC,"enb_gtp_addr (msg index %d, e_rab index %d, status %d, xid %d): nb_of_modify_e_rabs %d,  e_rab_id %d \n ",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1594 1595 1596
                 e_rabs_done,  e_rab, ue_context_pP->ue_context.modify_e_rab[e_rab].status, xid,
                 ue_context_pP->ue_context.nb_of_modify_e_rabs,
                 S1AP_E_RAB_MODIFY_RESP (msg_p).e_rabs[e_rabs_done].e_rab_id);
1597 1598 1599 1600 1601 1602 1603 1604 1605
          e_rabs_done++;
        } else {
          // unexpected
          S1AP_E_RAB_MODIFY_RESP (msg_p).e_rabs_failed[e_rabs_failed].e_rab_id = ue_context_pP->ue_context.modify_e_rab[e_rab].param.e_rab_id;
          S1AP_E_RAB_MODIFY_RESP (msg_p).e_rabs_failed[e_rabs_failed].cause = S1AP_CAUSE_RADIO_NETWORK;
          S1AP_E_RAB_MODIFY_RESP (msg_p).e_rabs_failed[e_rabs_failed].cause_value = 30;//S1ap_CauseRadioNetwork_unknown_E_RAB_ID;
          e_rabs_failed++;
        }
      } else if ((ue_context_pP->ue_context.modify_e_rab[e_rab].status == E_RAB_STATUS_NEW) ||
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1606
                 (ue_context_pP->ue_context.modify_e_rab[e_rab].status == E_RAB_STATUS_ESTABLISHED)) {
1607 1608 1609 1610 1611 1612 1613
        LOG_D (RRC,"E-RAB is NEW or already ESTABLISHED\n");
      } else {  /* status == E_RAB_STATUS_FAILED; */
        S1AP_E_RAB_MODIFY_RESP (msg_p).e_rabs_failed[e_rabs_failed].e_rab_id = ue_context_pP->ue_context.modify_e_rab[e_rab].param.e_rab_id;
        // add failure cause when defined
        S1AP_E_RAB_MODIFY_RESP (msg_p).e_rabs_failed[e_rabs_failed].cause = ue_context_pP->ue_context.modify_e_rab[e_rab].cause;
        e_rabs_failed++;
      }
Navid Nikaein's avatar
Navid Nikaein committed
1614
    } else {
1615
      /*debug info for the xid */
1616
      LOG_D(RRC,"xid does not corresponds  (context e_rab index %d, status %d, xid %d/%d) \n ",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1617
            e_rab, ue_context_pP->ue_context.modify_e_rab[e_rab].status, xid, ue_context_pP->ue_context.modify_e_rab[e_rab].xid);
Navid Nikaein's avatar
Navid Nikaein committed
1618
    }
1619
  }
1620 1621 1622

  S1AP_E_RAB_MODIFY_RESP (msg_p).nb_of_e_rabs = e_rabs_done;
  S1AP_E_RAB_MODIFY_RESP (msg_p).nb_of_e_rabs_failed = e_rabs_failed;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1623

1624 1625 1626
  // NN: add conditions for e_rabs_failed
  if (e_rabs_done > 0 || e_rabs_failed > 0) {
    LOG_D(RRC,"S1AP_E_RAB_MODIFY_RESP: sending the message: nb_of_modify_e_rabs %d, total e_rabs %d, index %d\n",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
          ue_context_pP->ue_context.nb_of_modify_e_rabs, ue_context_pP->ue_context.setup_e_rabs, e_rab);
    MSC_LOG_TX_MESSAGE(
      MSC_RRC_ENB,
      MSC_S1AP_ENB,
      (const char *)&S1AP_E_RAB_SETUP_RESP (msg_p),
      sizeof(s1ap_e_rab_setup_resp_t),
      MSC_AS_TIME_FMT" E_RAB_MODIFY_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,
      S1AP_E_RAB_MODIFY_RESP (msg_p).eNB_ue_s1ap_id,
      e_rabs_done, e_rabs_failed);
1638 1639 1640
    itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, msg_p);
  }

1641
  return 0;
1642
}
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
int rrc_eNB_process_S1AP_E_RAB_RELEASE_COMMAND(MessageDef *msg_p, const char *msg_name, instance_t instance) {
  uint32_t                        eNB_ue_s1ap_id;
  struct rrc_eNB_ue_context_s    *ue_context_p = NULL;
  protocol_ctxt_t                 ctxt;
  e_rab_release_t e_rab_release_params[S1AP_MAX_E_RAB];
  uint8_t nb_e_rabs_torelease;
  int erab;
  int i;
  uint8_t b_existed,is_existed;
  uint8_t xid;
  uint8_t e_rab_release_drb;
  MessageDef                     *msg_delete_tunnels_p = NULL;
  e_rab_release_drb = 0;
  memcpy(&e_rab_release_params[0], &(S1AP_E_RAB_RELEASE_COMMAND (msg_p).e_rab_release_params[0]), sizeof(e_rab_release_t)*S1AP_MAX_E_RAB);
  eNB_ue_s1ap_id = S1AP_E_RAB_RELEASE_COMMAND (msg_p).eNB_ue_s1ap_id;
  nb_e_rabs_torelease = S1AP_E_RAB_RELEASE_COMMAND (msg_p).nb_e_rabs_torelease;
  ue_context_p   = rrc_eNB_get_ue_context_from_s1ap_ids(instance, UE_INITIAL_ID_INVALID, eNB_ue_s1ap_id);

  if(ue_context_p != NULL) {
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
    xid = rrc_eNB_get_next_transaction_identifier(ctxt.module_id);
    LOG_D(RRC,"S1AP-E-RAB Release Command: MME_UE_S1AP_ID %d  ENB_UE_S1AP_ID %d release_e_rabs %d \n",
          S1AP_E_RAB_RELEASE_COMMAND (msg_p).mme_ue_s1ap_id, eNB_ue_s1ap_id,nb_e_rabs_torelease);

    for(erab = 0; erab < nb_e_rabs_torelease; erab++) {
      b_existed = 0;
      is_existed = 0;

      for ( i = erab-1;  i>= 0; i--) {
        if (e_rab_release_params[erab].e_rab_id == e_rab_release_params[i].e_rab_id) {
          is_existed = 1;
          break;
1675
        }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1676
      }
1677

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1678 1679 1680 1681
      if(is_existed == 1) {
        //e_rab_id is existed
        continue;
      }
1682

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1683 1684 1685 1686
      for ( i = 0;  i < NB_RB_MAX; i++) {
        if (e_rab_release_params[erab].e_rab_id == ue_context_p->ue_context.e_rab[i].param.e_rab_id) {
          b_existed = 1;
          break;
1687
        }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1688
      }
1689

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
      if(b_existed == 0) {
        //no e_rab_id
        ue_context_p->ue_context.e_rabs_release_failed[ue_context_p->ue_context.nb_release_of_e_rabs].e_rab_id = e_rab_release_params[erab].e_rab_id;
        ue_context_p->ue_context.e_rabs_release_failed[ue_context_p->ue_context.nb_release_of_e_rabs].cause = S1AP_CAUSE_RADIO_NETWORK;
        ue_context_p->ue_context.e_rabs_release_failed[ue_context_p->ue_context.nb_release_of_e_rabs].cause_value = 30;
        ue_context_p->ue_context.nb_release_of_e_rabs++;
      } else {
        if(ue_context_p->ue_context.e_rab[i].status == E_RAB_STATUS_FAILED) {
          ue_context_p->ue_context.e_rab[i].xid = xid;
          continue;
        } else if(ue_context_p->ue_context.e_rab[i].status == E_RAB_STATUS_ESTABLISHED) {
          ue_context_p->ue_context.e_rab[i].status = E_RAB_STATUS_TORELEASE;
          ue_context_p->ue_context.e_rab[i].xid = xid;
          e_rab_release_drb++;
        } else {
          //e_rab_id status NG
          ue_context_p->ue_context.e_rabs_release_failed[ue_context_p->ue_context.nb_release_of_e_rabs].e_rab_id = e_rab_release_params[erab].e_rab_id;
          ue_context_p->ue_context.e_rabs_release_failed[ue_context_p->ue_context.nb_release_of_e_rabs].cause = S1AP_CAUSE_RADIO_NETWORK;
          ue_context_p->ue_context.e_rabs_release_failed[ue_context_p->ue_context.nb_release_of_e_rabs].cause_value = 0;
          ue_context_p->ue_context.nb_release_of_e_rabs++;
1710
        }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1711 1712 1713 1714 1715 1716
      }
    }

    if(e_rab_release_drb > 0) {
      //RRCConnectionReconfiguration To UE
      rrc_eNB_generate_dedicatedRRCConnectionReconfiguration_release(&ctxt, ue_context_p, xid, S1AP_E_RAB_RELEASE_COMMAND (msg_p).nas_pdu.length, S1AP_E_RAB_RELEASE_COMMAND (msg_p).nas_pdu.buffer);
1717
    } else {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
      //gtp tunnel delete
      msg_delete_tunnels_p = itti_alloc_new_message(TASK_RRC_ENB, GTPV1U_ENB_DELETE_TUNNEL_REQ);
      memset(&GTPV1U_ENB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p), 0, sizeof(GTPV1U_ENB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p)));
      GTPV1U_ENB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).rnti = ue_context_p->ue_context.rnti;

      for(i = 0; i < NB_RB_MAX; i++) {
        if(xid == ue_context_p->ue_context.e_rab[i].xid) {
          GTPV1U_ENB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).eps_bearer_id[GTPV1U_ENB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).num_erab++] = ue_context_p->ue_context.enb_gtp_ebi[i];
          ue_context_p->ue_context.enb_gtp_teid[i] = 0;
          memset(&ue_context_p->ue_context.enb_gtp_addrs[i], 0, sizeof(ue_context_p->ue_context.enb_gtp_addrs[i]));
          ue_context_p->ue_context.enb_gtp_ebi[i]  = 0;
        }
      }

      itti_send_msg_to_task(TASK_GTPV1_U, instance, msg_delete_tunnels_p);
      //S1AP_E_RAB_RELEASE_RESPONSE
      rrc_eNB_send_S1AP_E_RAB_RELEASE_RESPONSE(&ctxt, ue_context_p, xid);
1735
    }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1736 1737 1738 1739 1740
  } else {
    LOG_E(RRC,"S1AP-E-RAB Release Command: MME_UE_S1AP_ID %d  ENB_UE_S1AP_ID %d  Error ue_context_p NULL \n",
          S1AP_E_RAB_RELEASE_COMMAND (msg_p).mme_ue_s1ap_id, S1AP_E_RAB_RELEASE_COMMAND (msg_p).eNB_ue_s1ap_id);
    return -1;
  }
1741

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1742
  return 0;
1743 1744 1745
}


Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1746 1747 1748 1749 1750
int rrc_eNB_send_S1AP_E_RAB_RELEASE_RESPONSE(const protocol_ctxt_t *const ctxt_pP, rrc_eNB_ue_context_t *const ue_context_pP, uint8_t xid) {
  int e_rabs_released = 0;
  MessageDef   *msg_p;
  msg_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_E_RAB_RELEASE_RESPONSE);
  S1AP_E_RAB_RELEASE_RESPONSE (msg_p).eNB_ue_s1ap_id = ue_context_pP->ue_context.eNB_ue_s1ap_id;
1751

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1752 1753 1754 1755 1756 1757
  for (int i = 0;  i < NB_RB_MAX; i++) {
    if (xid == ue_context_pP->ue_context.e_rab[i].xid) {
      S1AP_E_RAB_RELEASE_RESPONSE (msg_p).e_rab_release[e_rabs_released].e_rab_id = ue_context_pP->ue_context.e_rab[i].param.e_rab_id;
      e_rabs_released++;
      //clear
      memset(&ue_context_pP->ue_context.e_rab[i],0,sizeof(e_rab_param_t));
1758
    }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
  }

  S1AP_E_RAB_RELEASE_RESPONSE (msg_p).nb_of_e_rabs_released = e_rabs_released;
  S1AP_E_RAB_RELEASE_RESPONSE (msg_p).nb_of_e_rabs_failed = ue_context_pP->ue_context.nb_release_of_e_rabs;
  memcpy(&(S1AP_E_RAB_RELEASE_RESPONSE (msg_p).e_rabs_failed[0]),&ue_context_pP->ue_context.e_rabs_release_failed[0],sizeof(e_rab_failed_t)*ue_context_pP->ue_context.nb_release_of_e_rabs);
  ue_context_pP->ue_context.setup_e_rabs -= e_rabs_released;
  LOG_I(RRC,"S1AP-E-RAB RELEASE RESPONSE: ENB_UE_S1AP_ID %d release_e_rabs %d setup_e_rabs %d \n",
        S1AP_E_RAB_RELEASE_RESPONSE (msg_p).eNB_ue_s1ap_id,
        e_rabs_released, ue_context_pP->ue_context.setup_e_rabs);
  itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, msg_p);

  //clear xid
  for(int i = 0; i < NB_RB_MAX; i++) {
    ue_context_pP->ue_context.e_rab[i].xid = -1;
  }

  //clear release e_rabs
  ue_context_pP->ue_context.nb_release_of_e_rabs = 0;
  memset(&ue_context_pP->ue_context.e_rabs_release_failed[0],0,sizeof(e_rab_failed_t)*S1AP_MAX_E_RAB);
  return 0;
1779 1780 1781
}

/*------------------------------------------------------------------------------*/
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1782
int rrc_eNB_process_PAGING_IND(MessageDef *msg_p, const char *msg_name, instance_t instance) {
1783 1784 1785 1786 1787 1788 1789
  const unsigned int Ttab[4] = {32,64,128,256};
  uint8_t Tc,Tue;  /* DRX cycle of UE */
  uint32_t pcch_nB;  /* 4T, 2T, T, T/2, T/4, T/8, T/16, T/32 */
  uint32_t N;  /* N: min(T,nB). total count of PF in one DRX cycle */
  uint32_t Ns = 0;  /* Ns: max(1,nB/T) */
  uint8_t i_s;  /* i_s = floor(UE_ID/N) mod Ns */
  uint32_t T;  /* DRX cycle */
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1790

1791
  for (uint16_t tai_size = 0; tai_size < S1AP_PAGING_IND(msg_p).tai_size; tai_size++) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1792 1793 1794 1795 1796 1797
    LOG_D(RRC,"[eNB %d] In S1AP_PAGING_IND: MCC %d, MNC %d, TAC %d\n", instance, S1AP_PAGING_IND(msg_p).plmn_identity[tai_size].mcc,
          S1AP_PAGING_IND(msg_p).plmn_identity[tai_size].mnc, S1AP_PAGING_IND(msg_p).tac[tai_size]);

    for (uint8_t j = 0; j < RC.rrc[instance]->configuration.num_plmn; j++) {
      if (RC.rrc[instance]->configuration.mcc[j] == S1AP_PAGING_IND(msg_p).plmn_identity[tai_size].mcc
          && RC.rrc[instance]->configuration.mnc[j] == S1AP_PAGING_IND(msg_p).plmn_identity[tai_size].mnc
1798
          && RC.rrc[instance]->configuration.tac == S1AP_PAGING_IND(msg_p).tac[tai_size]) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1799 1800 1801 1802
        for (uint8_t CC_id = 0; CC_id < MAX_NUM_CCs; CC_id++) {
          lte_frame_type_t frame_type = RC.eNB[instance][CC_id]->frame_parms.frame_type;
          /* get nB from configuration */
          /* get default DRX cycle from configuration */
1803
          Tc = (uint8_t)RC.rrc[instance]->configuration.radioresourceconfig[CC_id].pcch_defaultPagingCycle;
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1804 1805 1806 1807 1808 1809 1810 1811 1812

          if (Tc < LTE_PCCH_Config__defaultPagingCycle_rf32 || Tc > LTE_PCCH_Config__defaultPagingCycle_rf256) {
            continue;
          }

          Tue = (uint8_t)S1AP_PAGING_IND(msg_p).paging_drx;
          /* set T = min(Tc,Tue) */
          T = Tc < Tue ? Ttab[Tc] : Ttab[Tue];
          /* set pcch_nB = PCCH-Config->nB */
1813
	  pcch_nB = (uint32_t)RC.rrc[instance]->configuration.radioresourceconfig[CC_id].pcch_nB;
1814

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
          switch (pcch_nB) {
            case LTE_PCCH_Config__nB_fourT:
              Ns = 4;
              break;

            case LTE_PCCH_Config__nB_twoT:
              Ns = 2;
              break;

            default:
              Ns = 1;
              break;
          }

          /* set N = min(T,nB) */
          if (pcch_nB > LTE_PCCH_Config__nB_oneT) {
            switch (pcch_nB) {
              case LTE_PCCH_Config__nB_halfT:
                N = T/2;
                break;

              case LTE_PCCH_Config__nB_quarterT:
                N = T/4;
                break;

              case LTE_PCCH_Config__nB_oneEighthT:
                N = T/8;
                break;

              case LTE_PCCH_Config__nB_oneSixteenthT:
                N = T/16;
                break;

              case LTE_PCCH_Config__nB_oneThirtySecondT:
                N = T/32;
                break;

              default:
                /* pcch_nB error */
                LOG_E(RRC, "[eNB %d] In S1AP_PAGING_IND:  pcch_nB error (pcch_nB %d) \n",
                      instance, pcch_nB);
                return (-1);
            }
          } else {
            N = T;
          }

          /* insert data to UE_PF_PO or update data in UE_PF_PO */
          pthread_mutex_lock(&ue_pf_po_mutex);
          uint8_t i = 0;

          for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
            if ((UE_PF_PO[CC_id][i].enable_flag == TRUE && UE_PF_PO[CC_id][i].ue_index_value == (uint16_t)(S1AP_PAGING_IND(msg_p).ue_index_value))
                || (UE_PF_PO[CC_id][i].enable_flag != TRUE)) {
1869
              /* set T = min(Tc,Tue) */
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
              UE_PF_PO[CC_id][i].T = T;
              /* set UE_ID */
              UE_PF_PO[CC_id][i].ue_index_value = (uint16_t)S1AP_PAGING_IND(msg_p).ue_index_value;
              /* calculate PF and PO */
              /* set PF_min : SFN mod T = (T div N)*(UE_ID mod N) */
              UE_PF_PO[CC_id][i].PF_min = (T / N) * (UE_PF_PO[CC_id][i].ue_index_value % N);
              /* set PO */
              /* i_s = floor(UE_ID/N) mod Ns */
              i_s = (uint8_t)((UE_PF_PO[CC_id][i].ue_index_value / N) % Ns);

              if (Ns == 1) {
                UE_PF_PO[CC_id][i].PO = (frame_type==FDD) ? 9 : 0;
              } else if (Ns==2) {
                UE_PF_PO[CC_id][i].PO = (frame_type==FDD) ? (4+(5*i_s)) : (5*i_s);
              } else if (Ns==4) {
                UE_PF_PO[CC_id][i].PO = (frame_type==FDD) ? (4*(i_s&1)+(5*(i_s>>1))) : ((i_s&1)+(5*(i_s>>1)));
1886
              }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1887 1888 1889 1890

              if (UE_PF_PO[CC_id][i].enable_flag == TRUE) {
                //paging exist UE log
                LOG_D(RRC,"[eNB %d] CC_id %d In S1AP_PAGING_IND: Update exist UE %d, T %d, PF %d, PO %d\n", instance, CC_id, UE_PF_PO[CC_id][i].ue_index_value, T, UE_PF_PO[CC_id][i].PF_min, UE_PF_PO[CC_id][i].PO);
1891
              } else {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1892 1893 1894 1895
                /* set enable_flag */
                UE_PF_PO[CC_id][i].enable_flag = TRUE;
                //paging new UE log
                LOG_D(RRC,"[eNB %d] CC_id %d In S1AP_PAGING_IND: Insert a new UE %d, T %d, PF %d, PO %d\n", instance, CC_id, UE_PF_PO[CC_id][i].ue_index_value, T, UE_PF_PO[CC_id][i].PF_min, UE_PF_PO[CC_id][i].PO);
1896
              }
1897

Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1898 1899
              break;
            }
1900
          }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931

          pthread_mutex_unlock(&ue_pf_po_mutex);
          uint32_t length;
          uint8_t buffer[RRC_BUF_SIZE];
          uint8_t *message_buffer;
          /* Transfer data to PDCP */
          MessageDef *message_p;
          message_p = itti_alloc_new_message (TASK_RRC_ENB, RRC_PCCH_DATA_REQ);
          /* Create message for PDCP (DLInformationTransfer_t) */
          length = do_Paging (instance,
                              buffer,
                              S1AP_PAGING_IND(msg_p).ue_paging_identity,
                              S1AP_PAGING_IND(msg_p).cn_domain);

          if(length == -1) {
            LOG_I(RRC, "do_Paging error");
            return -1;
          }

          message_buffer = itti_malloc (TASK_RRC_ENB, TASK_PDCP_ENB, length);
          /* Uses a new buffer to avoid issue with PDCP buffer content that could be changed by PDCP (asynchronous message handling). */
          memcpy (message_buffer, buffer, length);
          RRC_PCCH_DATA_REQ (message_p).sdu_size  = length;
          RRC_PCCH_DATA_REQ (message_p).sdu_p     = message_buffer;
          RRC_PCCH_DATA_REQ (message_p).mode      = PDCP_TRANSMISSION_MODE_TRANSPARENT;  /* not used */
          RRC_PCCH_DATA_REQ (message_p).rnti      = P_RNTI;
          RRC_PCCH_DATA_REQ (message_p).ue_index  = i;
          RRC_PCCH_DATA_REQ (message_p).CC_id  = CC_id;
          LOG_D(RRC, "[eNB %d] CC_id %d In S1AP_PAGING_IND: send encdoed buffer to PDCP buffer_size %d\n", instance, CC_id, length);
          itti_send_msg_to_task (TASK_PDCP_ENB, instance, message_p);
        }
1932
      }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
1933
    }
1934 1935 1936 1937
  }

  return (0);
}
1938

1939
/*NN: careful about the typcast of xid (long -> uint8_t*/
1940 1941
int rrc_eNB_send_PATH_SWITCH_REQ(const protocol_ctxt_t *const ctxt_pP,
                                 rrc_eNB_ue_context_t          *const ue_context_pP) {
1942 1943 1944
  MessageDef      *msg_p         = NULL;
  int e_rab = 0;
  int e_rabs_done = 0;
1945
  rrc_ue_s1ap_ids_t  *rrc_ue_s1ap_ids_p = NULL;
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
  hashtable_rc_t      h_rc;
  gtpv1u_enb_create_tunnel_req_t  create_tunnel_req;
  gtpv1u_enb_create_tunnel_resp_t create_tunnel_resp;
  uint8_t inde_list[ue_context_pP->ue_context.nb_of_e_rabs];
  memset(inde_list, 0, ue_context_pP->ue_context.nb_of_e_rabs*sizeof(uint8_t));
  msg_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_PATH_SWITCH_REQ);
  ue_context_pP->ue_context.ue_initial_id = get_next_ue_initial_id (ctxt_pP->module_id);
  S1AP_PATH_SWITCH_REQ (msg_p).ue_initial_id = ue_context_pP->ue_context.ue_initial_id;
  rrc_ue_s1ap_ids_p = malloc(sizeof(*rrc_ue_s1ap_ids_p));
  rrc_ue_s1ap_ids_p->ue_initial_id  = ue_context_pP->ue_context.ue_initial_id;
  rrc_ue_s1ap_ids_p->eNB_ue_s1ap_id = UE_INITIAL_ID_INVALID;
  rrc_ue_s1ap_ids_p->ue_rnti        = ctxt_pP->rnti;
  h_rc = hashtable_insert(RC.rrc[ctxt_pP->module_id]->initial_id2_s1ap_ids,
		               (hash_key_t)ue_context_pP->ue_context.ue_initial_id,
                               rrc_ue_s1ap_ids_p);
1961

1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
  if (h_rc != HASH_TABLE_OK) {
    LOG_E(S1AP, "[eNB %d] Error while hashtable_insert in initial_id2_s1ap_ids ue_initial_id %u\n",
	  ctxt_pP->module_id, ue_context_pP->ue_context.ue_initial_id);
  }

  S1AP_PATH_SWITCH_REQ (msg_p).eNB_ue_s1ap_id = ue_context_pP->ue_context.eNB_ue_s1ap_id;
  S1AP_PATH_SWITCH_REQ (msg_p).mme_ue_s1ap_id = ue_context_pP->ue_context.mme_ue_s1ap_id;
  S1AP_PATH_SWITCH_REQ (msg_p).ue_gummei.mcc = ue_context_pP->ue_context.ue_gummei.mcc;
  S1AP_PATH_SWITCH_REQ (msg_p).ue_gummei.mnc = ue_context_pP->ue_context.ue_gummei.mnc;
  S1AP_PATH_SWITCH_REQ (msg_p).ue_gummei.mnc_len = ue_context_pP->ue_context.ue_gummei.mnc_len;
  S1AP_PATH_SWITCH_REQ (msg_p).ue_gummei.mme_code = ue_context_pP->ue_context.ue_gummei.mme_code;
  S1AP_PATH_SWITCH_REQ (msg_p).ue_gummei.mme_group_id = ue_context_pP->ue_context.ue_gummei.mme_group_id;
  S1AP_PATH_SWITCH_REQ (msg_p).security_capabilities.encryption_algorithms=ue_context_pP->ue_context.security_capabilities.encryption_algorithms;
  S1AP_PATH_SWITCH_REQ (msg_p).security_capabilities.integrity_algorithms=ue_context_pP->ue_context.security_capabilities.integrity_algorithms;
  LOG_I (RRC,"Path switch request: nb nb_of_e_rabs %u status %u\n",
          ue_context_pP->ue_context.nb_of_e_rabs,
          ue_context_pP->ue_context.e_rab[e_rab].status);
1979
  memset(&create_tunnel_req, 0, sizeof(create_tunnel_req));
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011

  // the context for UE to be handovered is obtained through ho_req message
  for (e_rab = 0; e_rab <  ue_context_pP->ue_context.nb_of_e_rabs ; e_rab++) {
    if (ue_context_pP->ue_context.e_rab[e_rab].status == E_RAB_STATUS_ESTABLISHED) {
      create_tunnel_req.eps_bearer_id[e_rabs_done]       = ue_context_pP->ue_context.e_rab[e_rab].param.e_rab_id;
      create_tunnel_req.sgw_S1u_teid[e_rabs_done]        = ue_context_pP->ue_context.e_rab[e_rab].param.gtp_teid;
      memcpy(&create_tunnel_req.sgw_addr[e_rabs_done],
             &ue_context_pP->ue_context.e_rab[e_rab].param.sgw_addr,
             sizeof(transport_layer_addr_t));
      inde_list[e_rabs_done] = e_rab;
      e_rabs_done++;
    }
  }

  S1AP_PATH_SWITCH_REQ (msg_p).nb_of_e_rabs = e_rabs_done;
  create_tunnel_req.rnti           = ue_context_pP->ue_context.rnti;
  create_tunnel_req.num_tunnels    = e_rabs_done;
  gtpv1u_create_s1u_tunnel(
      ctxt_pP->instance,
      &create_tunnel_req,
      &create_tunnel_resp);
  rrc_eNB_process_GTPV1U_CREATE_TUNNEL_RESP(
        ctxt_pP,
        &create_tunnel_resp,
        &inde_list[0]);

  for (e_rab = 0; e_rab < e_rabs_done; e_rab++) {
      S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].e_rab_id = create_tunnel_resp.eps_bearer_id[e_rab];
      S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].gtp_teid = create_tunnel_resp.enb_S1u_teid[e_rab];
      S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].eNB_addr = create_tunnel_resp.enb_addr;
      LOG_I (RRC,"enb_gtp_addr (msg index %d, e_rab index %d, status %d): nb_of_e_rabs %d,  e_rab_id %d, teid: %u, addr: %d.%d.%d.%d \n ",
	     e_rabs_done,  e_rab, ue_context_pP->ue_context.e_rab[inde_list[e_rab]].status,
2012
           S1AP_PATH_SWITCH_REQ (msg_p).nb_of_e_rabs,
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
	     S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].e_rab_id,
	     S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].gtp_teid,
	     S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].eNB_addr.buffer[0],
	     S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].eNB_addr.buffer[1],
	     S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].eNB_addr.buffer[2],
	     S1AP_PATH_SWITCH_REQ (msg_p).e_rabs_tobeswitched[e_rab].eNB_addr.buffer[3]);
  }

    // NN: add conditions for e_rabs_failed
    if (e_rabs_done > 0) {
      LOG_I(RRC,"S1AP_PATH_SWITCH_REQ: sending the message: nb_of_erabstobeswitched %d, total e_rabs %d, index %d\n",
2024
          S1AP_PATH_SWITCH_REQ (msg_p).nb_of_e_rabs, ue_context_pP->ue_context.setup_e_rabs, e_rab);
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
      MSC_LOG_TX_MESSAGE(
			 MSC_RRC_ENB,
			 MSC_S1AP_ENB,
			 (const char *)&S1AP_PATH_SWITCH_REQ (msg_p),
			 sizeof(s1ap_path_switch_req_t),
			 MSC_AS_TIME_FMT" PATH_SWITCH_REQ UE %X eNB_ue_s1ap_id %u e_rabs:%u succ",
			 MSC_AS_TIME_ARGS(ctxt_pP),
			 ue_context_pP->ue_id_rnti,
			 S1AP_PATH_SWITCH_REQ (msg_p).eNB_ue_s1ap_id,
			 e_rabs_done);
	itti_send_msg_to_task (TASK_S1AP, ctxt_pP->instance, msg_p);
     }

  return 0;
}

2041
int rrc_eNB_process_S1AP_PATH_SWITCH_REQ_ACK (MessageDef *msg_p, const char *msg_name, instance_t instance) {
2042 2043 2044 2045 2046
  uint16_t                        ue_initial_id;
  uint32_t                        eNB_ue_s1ap_id;
  //gtpv1u_enb_create_tunnel_req_t  create_tunnel_req;
  //gtpv1u_enb_create_tunnel_resp_t create_tunnel_resp;
  gtpv1u_enb_delete_tunnel_req_t  delete_tunnel_req;
2047
  struct rrc_eNB_ue_context_s *ue_context_p = NULL;
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
  protocol_ctxt_t              ctxt;
  int i;
  ue_initial_id  = S1AP_PATH_SWITCH_REQ_ACK (msg_p).ue_initial_id;
  eNB_ue_s1ap_id = S1AP_PATH_SWITCH_REQ_ACK (msg_p).eNB_ue_s1ap_id;
  ue_context_p   = rrc_eNB_get_ue_context_from_s1ap_ids(instance, ue_initial_id, eNB_ue_s1ap_id);
  LOG_I(RRC, "[eNB %d] Received %s: ue_initial_id %d, eNB_ue_s1ap_id %d, nb_of_e_rabs %d\n",
        instance, msg_name, ue_initial_id, eNB_ue_s1ap_id, S1AP_E_RAB_SETUP_REQ (msg_p).nb_e_rabs_tosetup);

  if (ue_context_p == NULL) {
    /* Can not associate this message to an UE index, send a failure to S1AP and discard it! */
    //MessageDef *msg_fail_p = NULL;
    LOG_W(RRC, "[eNB %d] In S1AP_PATH_SWITCH_REQ_ACK: unknown UE from S1AP ids (%d, %d)\n", instance, ue_initial_id, eNB_ue_s1ap_id);
    //msg_fail_p = itti_alloc_new_message (TASK_RRC_ENB, S1AP_PATH_SWITCH_REQ_ACK_FAIL);
    //S1AP_PATH_SWITCH_REQ_ACK  (msg_fail_p).eNB_ue_s1ap_id = eNB_ue_s1ap_id;
    // TODO add failure cause when defined!
    //itti_send_msg_to_task (TASK_S1AP, instance, msg_fail_p);
    return (-1);
  } else {
    PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_YES, ue_context_p->ue_context.rnti, 0, 0);
    ue_context_p->ue_context.eNB_ue_s1ap_id = S1AP_PATH_SWITCH_REQ_ACK (msg_p).eNB_ue_s1ap_id;
    ue_context_p->ue_context.mme_ue_s1ap_id = S1AP_PATH_SWITCH_REQ_ACK (msg_p).mme_ue_s1ap_id;
    /* Save e RAB information for later */
    {
2071 2072
      ue_context_p->ue_context.nb_release_of_e_rabs = S1AP_PATH_SWITCH_REQ_ACK (msg_p).nb_e_rabs_tobereleased;

2073 2074 2075 2076
      for (i = 0;
	   i < ue_context_p->ue_context.setup_e_rabs; // go over total number of e_rabs received through x2_ho_req msg
	   i++) {
	// assume that we are releasing all the DRBs
2077 2078 2079 2080
	ue_context_p->ue_context.e_rab[i].status = E_RAB_STATUS_REESTABLISHED;
        if (ue_context_p->ue_context.nb_release_of_e_rabs==0) {
          LOG_I(RRC,"Bearer re-established with ID: %d\n", ue_context_p->ue_context.e_rab[i].param.e_rab_id);
        }
2081 2082 2083
      }

      //memset(&create_tunnel_req, 0 , sizeof(create_tunnel_req));
2084
      uint8_t nb_e_rabs_tobeswitched = S1AP_PATH_SWITCH_REQ_ACK (msg_p).nb_e_rabs_tobeswitched;
2085 2086 2087

      // keep the previous bearer
      // the index for the rec
2088 2089
      if (nb_e_rabs_tobeswitched>0) {
        int e_rab_switch_index=0;
2090
      for (i = 0;
2091
	     i < ue_context_p->ue_context.setup_e_rabs; // go over total number of e_rabs received through x2_ho_req msg
2092 2093
	   i++) {
	/* Harmonize with enb_gtp_teid, enb_gtp_addrs, and enb_gtp_rbi vars in the top level structure */
2094 2095 2096 2097 2098 2099 2100
          if (ue_context_p->ue_context.e_rab[i].param.e_rab_id == S1AP_PATH_SWITCH_REQ_ACK (msg_p).e_rabs_tobeswitched[e_rab_switch_index].e_rab_id) {
	    ue_context_p->ue_context.e_rab[i].param.e_rab_id = S1AP_PATH_SWITCH_REQ_ACK (msg_p).e_rabs_tobeswitched[e_rab_switch_index].e_rab_id;
	    ue_context_p->ue_context.e_rab[i].param.sgw_addr= S1AP_PATH_SWITCH_REQ_ACK (msg_p).e_rabs_tobeswitched[e_rab_switch_index].sgw_addr;
	    ue_context_p->ue_context.e_rab[i].param.gtp_teid = S1AP_PATH_SWITCH_REQ_ACK (msg_p).e_rabs_tobeswitched[e_rab_switch_index].gtp_teid;
            e_rab_switch_index++;
          }
        }
2101 2102 2103
      }
    }
    ue_context_p->ue_context.ue_ambr=S1AP_PATH_SWITCH_REQ_ACK (msg_p).ue_ambr;
2104

2105 2106 2107 2108
    ue_context_p->ue_context.setup_e_rabs = ue_context_p->ue_context.setup_e_rabs - ue_context_p->ue_context.nb_release_of_e_rabs;
    ue_context_p->ue_context.nb_of_e_rabs = ue_context_p->ue_context.nb_of_e_rabs - ue_context_p->ue_context.nb_release_of_e_rabs;

    memset(&delete_tunnel_req, 0 , sizeof(delete_tunnel_req));
2109 2110

    if (ue_context_p->ue_context.nb_release_of_e_rabs>0) {
2111
      int e_rab_release_index=0;
2112
    for (i = 0;
2113
	   i < ue_context_p->ue_context.setup_e_rabs;
2114
	 i++) {
2115
        if (ue_context_p->ue_context.e_rab[i].param.e_rab_id == S1AP_PATH_SWITCH_REQ_ACK (msg_p).e_rabs_tobereleased[e_rab_release_index].e_rab_id) {
2116
      LOG_I(RRC,"Bearer released with ID: %d\n", ue_context_p->ue_context.e_rab[i].param.e_rab_id);
2117 2118 2119 2120 2121 2122 2123 2124 2125
          ue_context_p->ue_context.e_rab[i].status =  E_RAB_STATUS_TORELEASE;
          ue_context_p->ue_context.e_rabs_tobereleased[e_rab_release_index]=S1AP_PATH_SWITCH_REQ_ACK (msg_p).e_rabs_tobereleased[e_rab_release_index].e_rab_id;
          delete_tunnel_req.eps_bearer_id[e_rab_release_index] = S1AP_PATH_SWITCH_REQ_ACK (msg_p).e_rabs_tobereleased[e_rab_release_index].e_rab_id;
          e_rab_release_index++;
        }
        else {
          LOG_I(RRC,"Bearer re-established with ID: %d\n", ue_context_p->ue_context.e_rab[i].param.e_rab_id);
        }
      }
2126
    }
2127

2128
    if (ue_context_p->ue_context.nb_release_of_e_rabs>0){
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
      delete_tunnel_req.rnti= ue_context_p->ue_context.rnti;
      delete_tunnel_req.num_erab= ue_context_p->ue_context.nb_release_of_e_rabs;
      /* this could also be done through ITTI message */
      gtpv1u_delete_s1u_tunnel(instance,
			       &delete_tunnel_req);
      /* TBD: release the DRB not admitted */
      //rrc_eNB_generate_dedicatedRRCConnectionReconfiguration(&ctxt, ue_context_p, 0);
    }

    /* Security key */
    ue_context_p->ue_context.next_hop_chain_count=S1AP_PATH_SWITCH_REQ_ACK (msg_p).next_hop_chain_count;
    memcpy ( ue_context_p->ue_context.next_security_key,
	     S1AP_PATH_SWITCH_REQ_ACK (msg_p).next_security_key,
	     SECURITY_KEY_LENGTH);
2143 2144 2145

    rrc_eNB_send_X2AP_UE_CONTEXT_RELEASE(&ctxt, ue_context_p);

2146 2147 2148 2149
    return (0);
  }
}

2150 2151 2152 2153 2154 2155
int rrc_eNB_send_X2AP_UE_CONTEXT_RELEASE(const protocol_ctxt_t* const ctxt_pP, rrc_eNB_ue_context_t* const ue_context_pP) {

  MessageDef      *msg_p         = NULL;

  msg_p = itti_alloc_new_message (TASK_RRC_ENB, X2AP_UE_CONTEXT_RELEASE);

2156 2157
  X2AP_UE_CONTEXT_RELEASE (msg_p).rnti = ue_context_pP->ue_context.rnti;
  X2AP_UE_CONTEXT_RELEASE (msg_p).source_assoc_id = ue_context_pP->ue_context.handover_info->assoc_id;
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
  itti_send_msg_to_task (TASK_X2AP, ctxt_pP->instance, msg_p);
  return (0);
}

int s1ap_ue_context_release(instance_t instance, const uint32_t eNB_ue_s1ap_id){
  s1ap_eNB_instance_t *s1ap_eNB_instance_p = NULL;
  struct s1ap_eNB_ue_context_s *ue_context_p = NULL;

  s1ap_eNB_instance_p = s1ap_eNB_get_instance(instance);
  DevAssert(s1ap_eNB_instance_p != NULL);

  if ((ue_context_p = s1ap_eNB_get_ue_context(s1ap_eNB_instance_p,
                      eNB_ue_s1ap_id)) == NULL) {
    /* The context for this eNB ue s1ap id doesn't exist in the map of eNB UEs */
    LOG_W(RRC,"Failed to find ue context associated with eNB ue s1ap id: %u\n",
              eNB_ue_s1ap_id);
    return -1;
  }

  // release UE context
  struct s1ap_eNB_ue_context_s *ue_context2_p = NULL;

  if ((ue_context2_p = RB_REMOVE(s1ap_ue_map, &s1ap_eNB_instance_p->s1ap_ue_head, ue_context_p))
      != NULL) {
    LOG_W(RRC,"Removed UE context eNB_ue_s1ap_id %u\n",
              ue_context2_p->eNB_ue_s1ap_id);
    s1ap_eNB_free_ue_context(ue_context2_p);
  } else {
    LOG_W(RRC,"Removing UE context eNB_ue_s1ap_id %u: did not find context\n",
              ue_context_p->eNB_ue_s1ap_id);
  }
  /*RB_FOREACH(ue_context_p, s1ap_ue_map, &s1ap_eNB_instance_p->s1ap_ue_head) {
    S1AP_WARN("in s1ap_ue_map: UE context eNB_ue_s1ap_id %u mme_ue_s1ap_id %u state %u\n",
        ue_context_p->eNB_ue_s1ap_id, ue_context_p->mme_ue_s1ap_id,
        ue_context_p->ue_state);
  }*/

  return 0;
}