mme_app_bearer.c 32 KB
Newer Older
Cedric Roux's avatar
 
Cedric Roux committed
1
/*******************************************************************************
Lionel Gauthier's avatar
GPLv3  
Lionel Gauthier committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.

 *******************************************************************************/

30 31 32 33 34 35
/*! \file mme_app_bearer.c
* \brief
* \author Sebastien ROUX, Lionel Gauthier
* \company Eurecom
* \email: lionel.gauthier@eurecom.fr
*/
Cedric Roux's avatar
 
Cedric Roux committed
36 37 38 39 40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "intertask_interface.h"
41
#include "mme_config.h"
Cedric Roux's avatar
 
Cedric Roux committed
42 43 44 45 46 47 48 49

#include "mme_app_extern.h"
#include "mme_app_ue_context.h"
#include "mme_app_defs.h"

#include "secu_defs.h"

#include "assertions.h"
50
#include "common_types.h"
Cedric Roux's avatar
 
Cedric Roux committed
51 52 53



54 55 56 57 58 59 60 61 62 63 64 65
int
mme_app_send_s11_create_session_req(
    struct ue_context_s * const ue_context_pP)
{
  uint8_t                     i                 = 0;
  task_id_t                   to_task           = TASK_UNKNOWN;
  /* Keep the identifier to the default APN */
  context_identifier_t        context_identifier;
  MessageDef                 *message_p         = NULL;
  SgwCreateSessionRequest    *session_request_p = NULL;
  struct apn_configuration_s *default_apn_p     = NULL;

Cedric Roux's avatar
 
Cedric Roux committed
66

67
  DevAssert(ue_context_pP != NULL);
Cedric Roux's avatar
 
Cedric Roux committed
68 69

#if defined(DISABLE_STANDALONE_EPC)
70
  to_task = TASK_S11;
Cedric Roux's avatar
 
Cedric Roux committed
71
#else
72
  to_task = TASK_SPGW_APP;
Cedric Roux's avatar
 
Cedric Roux committed
73 74 75
#endif


76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  MME_APP_DEBUG("Handling imsi %"IMSI_FORMAT"\n", ue_context_pP->imsi);

  if (ue_context_pP->sub_status != SS_SERVICE_GRANTED) {
      /* HSS rejected the bearer creation or roaming is not allowed for this
       * UE. This result will trigger an ESM Failure message sent to UE.
       */
      DevMessage("Not implemented: ACCESS NOT GRANTED, send ESM Failure to NAS\n");
  }

  message_p = itti_alloc_new_message(TASK_MME_APP, SGW_CREATE_SESSION_REQUEST);

  /* WARNING:
   * Some parameters should be provided by NAS Layer:
   * - ue_time_zone
   * - mei
   * - uli
   * - uci
   * Some parameters should be provided by HSS:
   * - PGW address for CP
   * - paa
   * - ambr
   * and by MME Application layer:
   * - selection_mode
   * Set these parameters with random values for now.
   */

  session_request_p = &message_p->ittiMsg.sgwCreateSessionRequest;
  memset(session_request_p, 0, sizeof(SgwCreateSessionRequest));

  /* As the create session request is the first exchanged message and as
   * no tunnel had been previously setup, the distant teid is set to 0.
   * The remote teid will be provided in the response message.
   */
  session_request_p->teid = 0;

  MME_APP_IMSI_TO_STRING(ue_context_pP->imsi, (char *)session_request_p->imsi.digit);
  // message content was set to 0
  session_request_p->imsi.length = strlen((const char *)session_request_p->imsi.digit);

  /* Copy the MSISDN */
  memcpy(
      session_request_p->msisdn.digit,
      ue_context_pP->msisdn,
      ue_context_pP->msisdn_length);
  session_request_p->msisdn.length = ue_context_pP->msisdn_length;

  session_request_p->rat_type = RAT_EUTRAN;

  /* Copy the subscribed ambr to the sgw create session request message */
  memcpy(
      &session_request_p->ambr,
      &ue_context_pP->subscribed_ambr,
      sizeof(ambr_t));

  if (ue_context_pP->apn_profile.nb_apns == 0) {
      DevMessage("No APN returned by the HSS");
  }

  context_identifier = ue_context_pP->apn_profile.context_identifier;
  for (i = 0; i < ue_context_pP->apn_profile.nb_apns; i++) {
      default_apn_p = &ue_context_pP->apn_profile.apn_configuration[i];
      /* OK we got our default APN */
      if (default_apn_p->context_identifier == context_identifier)
          break;
  }

  if (!default_apn_p) {
      /* Unfortunately we didn't find our default APN... */
      DevMessage("No default APN found");
  }

  memcpy(&session_request_p->bearer_to_create.bearer_level_qos.gbr,
         &default_apn_p->ambr, sizeof(ambr_t));
  memcpy(&session_request_p->bearer_to_create.bearer_level_qos.mbr,
         &default_apn_p->ambr, sizeof(ambr_t));

  session_request_p->bearer_to_create.bearer_level_qos.qci =
      default_apn_p->subscribed_qos.qci;

  session_request_p->bearer_to_create.bearer_level_qos.pvi =
      default_apn_p->subscribed_qos.allocation_retention_priority.pre_emp_vulnerability;
  session_request_p->bearer_to_create.bearer_level_qos.pci =
      default_apn_p->subscribed_qos.allocation_retention_priority.pre_emp_capability;
  session_request_p->bearer_to_create.bearer_level_qos.pl =
      default_apn_p->subscribed_qos.allocation_retention_priority.priority_level;

  /* Asking for default bearer in initial UE message.
   * Use the address of ue_context as unique TEID: Need to find better here
   * and will generate unique id only for 32 bits platforms.
   */
  session_request_p->sender_fteid_for_cp.teid           = (uint32_t)ue_context_pP;
  session_request_p->sender_fteid_for_cp.interface_type = S11_MME_GTP_C;
  session_request_p->bearer_to_create.eps_bearer_id     = 5;

  ue_context_pP->mme_s11_teid = session_request_p->sender_fteid_for_cp.teid;
  ue_context_pP->sgw_s11_teid = 0;

  memcpy(session_request_p->apn, default_apn_p->service_selection,
         default_apn_p->service_selection_length);

  /* Set PDN type for pdn_type and PAA even if this IE is redundant */
  session_request_p->pdn_type = default_apn_p->pdn_type;
  session_request_p->paa.pdn_type = default_apn_p->pdn_type;
  if (default_apn_p->nb_ip_address == 0) {
      /* UE DHCPv4 allocated ip address */
      memset(session_request_p->paa.ipv4_address, 0, 4);
      memset(session_request_p->paa.ipv6_address, 0, 16);
  } else {
      uint8_t j;

      for (j = 0; j < default_apn_p->nb_ip_address; j++) {
          ip_address_t *ip_address;
          ip_address = &default_apn_p->ip_address[j];
          if (ip_address->pdn_type == IPv4) {
              memcpy(session_request_p->paa.ipv4_address, ip_address->address.ipv4_address, 4);
          } else if (ip_address->pdn_type == IPv6) {
              memcpy(session_request_p->paa.ipv6_address, ip_address->address.ipv6_address, 16);
          }
//             free(ip_address);
      }
  }
Cedric Roux's avatar
 
Cedric Roux committed
197

198 199 200
  config_read_lock(&mme_config);
  session_request_p->peer_ip = mme_config.ipv4.sgw_ip_address_for_S11;
  config_unlock(&mme_config);
Cedric Roux's avatar
 
Cedric Roux committed
201

202 203 204 205 206 207 208 209 210 211 212
  session_request_p->serving_network.mcc[0] = ue_context_pP->e_utran_cgi.plmn.MCCdigit1;
  session_request_p->serving_network.mcc[1] = ue_context_pP->e_utran_cgi.plmn.MCCdigit2;
  session_request_p->serving_network.mcc[2] = ue_context_pP->e_utran_cgi.plmn.MCCdigit3;

  session_request_p->serving_network.mnc[0] = ue_context_pP->e_utran_cgi.plmn.MNCdigit1;
  session_request_p->serving_network.mnc[1] = ue_context_pP->e_utran_cgi.plmn.MNCdigit2;
  session_request_p->serving_network.mnc[2] = ue_context_pP->e_utran_cgi.plmn.MNCdigit3;

  session_request_p->selection_mode = MS_O_N_P_APN_S_V;
  return itti_send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
}
Cedric Roux's avatar
 
Cedric Roux committed
213 214 215



216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
int
mme_app_handle_nas_pdn_connectivity_req(
        nas_pdn_connectivity_req_t * const nas_pdn_connectivity_req_pP)
{
  struct ue_context_s *ue_context_p = NULL;
  uint64_t             imsi         = 0;

  MME_APP_DEBUG("Received NAS_PDN_CONNECTIVITY_REQ from NAS\n");
  DevAssert(nas_pdn_connectivity_req_pP != NULL);

  MME_APP_STRING_TO_IMSI((char *)nas_pdn_connectivity_req_pP->imsi, &imsi);

  MME_APP_DEBUG("Handling imsi %"IMSI_FORMAT"\n", imsi);

  if ((ue_context_p = mme_ue_context_exists_imsi(&mme_app_desc.mme_ue_contexts,
                    imsi)) == NULL) {
      MME_APP_ERROR("That's embarrassing as we don't know this IMSI\n");
      return -1;
  }

  /* Consider the UE authenticated */
  ue_context_p->imsi_auth = IMSI_AUTHENTICATED;

  // Temp: save request, in near future merge wisely params in context
  memset(ue_context_p->pending_pdn_connectivity_req_imsi,0, 16);
  AssertFatal((nas_pdn_connectivity_req_pP->imsi_length > 0) &&
          (nas_pdn_connectivity_req_pP->imsi_length < 16),
          "BAD IMSI LENGTH %d", nas_pdn_connectivity_req_pP->imsi_length);

  AssertFatal((nas_pdn_connectivity_req_pP->imsi_length > 0) &&
          (nas_pdn_connectivity_req_pP->imsi_length < 16),
          "STOP ON IMSI LENGTH %d", nas_pdn_connectivity_req_pP->imsi_length);
  memcpy(ue_context_p->pending_pdn_connectivity_req_imsi,
          nas_pdn_connectivity_req_pP->imsi,
          nas_pdn_connectivity_req_pP->imsi_length);
  ue_context_p->pending_pdn_connectivity_req_imsi_length = nas_pdn_connectivity_req_pP->imsi_length;
  DUP_OCTET_STRING(nas_pdn_connectivity_req_pP->apn, ue_context_p->pending_pdn_connectivity_req_apn);
  FREE_OCTET_STRING(nas_pdn_connectivity_req_pP->apn)
  // dup OctetString
  DUP_OCTET_STRING(nas_pdn_connectivity_req_pP->pdn_addr, ue_context_p->pending_pdn_connectivity_req_pdn_addr);
  FREE_OCTET_STRING(nas_pdn_connectivity_req_pP->pdn_addr)
  ue_context_p->pending_pdn_connectivity_req_pti          = nas_pdn_connectivity_req_pP->pti;
  ue_context_p->pending_pdn_connectivity_req_ue_id        = nas_pdn_connectivity_req_pP->ue_id;

  memcpy(&ue_context_p->pending_pdn_connectivity_req_qos,
         &nas_pdn_connectivity_req_pP->qos,
         sizeof(network_qos_t));
  ue_context_p->pending_pdn_connectivity_req_proc_data    = nas_pdn_connectivity_req_pP->proc_data;
  nas_pdn_connectivity_req_pP->proc_data = NULL;
  ue_context_p->pending_pdn_connectivity_req_request_type = nas_pdn_connectivity_req_pP->request_type;

  //if ((nas_pdn_connectivity_req_pP->apn.value == NULL) || (nas_pdn_connectivity_req_pP->apn.length == 0)) {
      /* TODO: Get keys... */
      /* Now generate S6A ULR */
      return mme_app_send_s6a_update_location_req(ue_context_p);
  //} else {
      //return mme_app_send_s11_create_session_req(ue_context_p);

  //}
  //return -1;
}
Cedric Roux's avatar
 
Cedric Roux committed
277 278 279



280 281 282 283 284 285 286 287 288 289
// sent by NAS
void
mme_app_handle_conn_est_cnf(
        const nas_conn_est_cnf_t * const nas_conn_est_cnf_pP)
{
    struct ue_context_s                    *ue_context_p        = NULL;
    MessageDef                             *message_p           = NULL;
    mme_app_connection_establishment_cnf_t *establishment_cnf_p = NULL;
    bearer_context_t                       *current_bearer_p    = NULL;
    ebi_t                                   bearer_id           = 0;
290
    uint8_t                                 keNB[32];
Cedric Roux's avatar
 
Cedric Roux committed
291

292
    MME_APP_DEBUG("Received NAS_CONNECTION_ESTABLISHMENT_CNF from NAS\n");
Cedric Roux's avatar
 
Cedric Roux committed
293

294 295
    ue_context_p = mme_ue_context_exists_nas_ue_id(&mme_app_desc.mme_ue_contexts, nas_conn_est_cnf_pP->UEid);
    if (ue_context_p == NULL) {
296
        MME_APP_ERROR("UE context doesn't exist for UE 0x%08X/dec%u\n",
297 298
        		nas_conn_est_cnf_pP->UEid,
        		nas_conn_est_cnf_pP->UEid);
299
        return;
Cedric Roux's avatar
 
Cedric Roux committed
300 301
    }

302 303 304
    message_p           = itti_alloc_new_message(TASK_MME_APP, MME_APP_CONNECTION_ESTABLISHMENT_CNF);
    establishment_cnf_p = &message_p->ittiMsg.mme_app_connection_establishment_cnf;
    memset(establishment_cnf_p, 0, sizeof(mme_app_connection_establishment_cnf_t));
Cedric Roux's avatar
 
Cedric Roux committed
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    memcpy(&establishment_cnf_p->nas_conn_est_cnf,
            nas_conn_est_cnf_pP,
            sizeof(nas_conn_est_cnf_t));

    bearer_id = ue_context_p->default_bearer_id;
    current_bearer_p = &ue_context_p->eps_bearers[bearer_id];
    establishment_cnf_p->eps_bearer_id = bearer_id;

    establishment_cnf_p->bearer_s1u_sgw_fteid.interface_type = S1_U_SGW_GTP_U;
    establishment_cnf_p->bearer_s1u_sgw_fteid.teid = current_bearer_p->s_gw_teid;
    if ((current_bearer_p->s_gw_address.pdn_type == IPv4) ||
        (current_bearer_p->s_gw_address.pdn_type == IPv4_AND_v6)) {
        establishment_cnf_p->bearer_s1u_sgw_fteid.ipv4 = 1;
        memcpy(&establishment_cnf_p->bearer_s1u_sgw_fteid.ipv4_address,
                current_bearer_p->s_gw_address.address.ipv4_address,
                4);
Cedric Roux's avatar
 
Cedric Roux committed
322
    }
323 324 325 326 327 328 329 330 331 332 333
    if ((current_bearer_p->s_gw_address.pdn_type == IPv6) ||
            (current_bearer_p->s_gw_address.pdn_type == IPv4_AND_v6)) {
            establishment_cnf_p->bearer_s1u_sgw_fteid.ipv6 = 1;
            memcpy(establishment_cnf_p->bearer_s1u_sgw_fteid.ipv6_address,
                   current_bearer_p->s_gw_address.address.ipv6_address,
                   16);
        }
    establishment_cnf_p->bearer_qos_qci                    = current_bearer_p->qci;
    establishment_cnf_p->bearer_qos_prio_level             = current_bearer_p->prio_level;
    establishment_cnf_p->bearer_qos_pre_emp_vulnerability  = current_bearer_p->pre_emp_vulnerability;
    establishment_cnf_p->bearer_qos_pre_emp_capability     = current_bearer_p->pre_emp_capability;
334 335 336 337 338 339 340 341 342
#warning "Hardcoded AMBR"
    //establishment_cnf_p->ambr                              = ue_context_p->used_ambr;
    establishment_cnf_p->ambr.br_ul                        = 40000000;
    establishment_cnf_p->ambr.br_dl                        = 100000000;

    establishment_cnf_p->security_capabilities_encryption_algorithms = nas_conn_est_cnf_pP->selected_encryption_algorithm;
    establishment_cnf_p->security_capabilities_integrity_algorithms  = nas_conn_est_cnf_pP->selected_integrity_algorithm;
    MME_APP_DEBUG("security_capabilities_encryption_algorithms 0x%04X\n", establishment_cnf_p->security_capabilities_encryption_algorithms);
    MME_APP_DEBUG("security_capabilities_integrity_algorithms  0x%04X\n", establishment_cnf_p->security_capabilities_integrity_algorithms);
Cedric Roux's avatar
 
Cedric Roux committed
343

344
    MME_APP_DEBUG("Derive keNB with UL NAS COUNT %x\n", nas_conn_est_cnf_pP->ul_nas_count);
345
    derive_keNB(ue_context_p->vector_in_use->kasme, nas_conn_est_cnf_pP->ul_nas_count, keNB); //156
346 347
    memcpy(establishment_cnf_p->keNB, keNB, 32);

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    itti_send_msg_to_task(TASK_S1AP, INSTANCE_DEFAULT, message_p);
}



// sent by S1AP
void
mme_app_handle_conn_est_ind(
        const mme_app_connection_establishment_ind_t * const conn_est_ind_pP)
{
    struct ue_context_s *ue_context_p  = NULL;
    MessageDef          *message_p     = NULL;

    MME_APP_DEBUG("Received MME_APP_CONNECTION_ESTABLISHMENT_IND from S1AP\n");

    ue_context_p = mme_ue_context_exists_mme_ue_s1ap_id(
            &mme_app_desc.mme_ue_contexts,
            conn_est_ind_pP->mme_ue_s1ap_id);
    if (ue_context_p == NULL) {
367 368 369
        MME_APP_DEBUG("We didn't find this mme_ue_s1ap_id in list of UE: 0x%08x/dec%u\n",
        		conn_est_ind_pP->mme_ue_s1ap_id,
        		conn_est_ind_pP->mme_ue_s1ap_id);
370 371 372 373 374 375
        MME_APP_DEBUG("UE context doesn't exist -> create one\n");
        if ((ue_context_p = mme_create_new_ue_context()) == NULL) {
            /* Error during ue context malloc */
            /* TODO */
            DevMessage("mme_create_new_ue_context");
            return;
Cedric Roux's avatar
 
Cedric Roux committed
376
        }
377 378 379 380 381
        // S1AP UE ID AND NAS UE ID ARE THE SAME
        ue_context_p->mme_ue_s1ap_id = conn_est_ind_pP->mme_ue_s1ap_id;
        ue_context_p->ue_id          = conn_est_ind_pP->mme_ue_s1ap_id;
        DevAssert(mme_insert_ue_context(&mme_app_desc.mme_ue_contexts, ue_context_p) == 0);

382
        // tests
383 384 385 386
        ue_context_p = mme_ue_context_exists_mme_ue_s1ap_id(
                    &mme_app_desc.mme_ue_contexts,
                    conn_est_ind_pP->mme_ue_s1ap_id);
        AssertFatal(ue_context_p != NULL, "mme_ue_context_exists_mme_ue_s1ap_id Failed");
387 388 389 390
        ue_context_p = mme_ue_context_exists_nas_ue_id(
                    &mme_app_desc.mme_ue_contexts,
                    conn_est_ind_pP->mme_ue_s1ap_id);
        AssertFatal(ue_context_p != NULL, "mme_ue_context_exists_nas_ue_id Failed");
Cedric Roux's avatar
 
Cedric Roux committed
391 392
    }

393 394 395 396
    message_p  = itti_alloc_new_message(TASK_MME_APP, NAS_CONNECTION_ESTABLISHMENT_IND);
    memcpy(&NAS_CONN_EST_IND(message_p).nas,
            &conn_est_ind_pP->nas,
            sizeof (nas_establish_ind_t));
Cedric Roux's avatar
 
Cedric Roux committed
397

398 399
    itti_send_msg_to_task(TASK_NAS_MME, INSTANCE_DEFAULT, message_p);
}
Cedric Roux's avatar
 
Cedric Roux committed
400 401 402



403 404 405
int
mme_app_handle_create_sess_resp(
        const SgwCreateSessionResponse * const create_sess_resp_pP)
Cedric Roux's avatar
 
Cedric Roux committed
406
{
407 408 409
    struct ue_context_s *ue_context_p     = NULL;
    bearer_context_t    *current_bearer_p = NULL;
    MessageDef          *message_p = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
410 411 412

    int16_t bearer_id;

413
    DevAssert(create_sess_resp_pP != NULL);
Cedric Roux's avatar
 
Cedric Roux committed
414

415
    MME_APP_DEBUG("Received SGW_CREATE_SESSION_RESPONSE from S+P-GW\n");
Cedric Roux's avatar
 
Cedric Roux committed
416 417

    ue_context_p = mme_ue_context_exists_s11_teid(&mme_app_desc.mme_ue_contexts,
418
                                                  create_sess_resp_pP->teid);
Cedric Roux's avatar
 
Cedric Roux committed
419 420
    if (ue_context_p == NULL) {
        MME_APP_DEBUG("We didn't find this teid in list of UE: %08x\n",
421
                      create_sess_resp_pP->teid);
Cedric Roux's avatar
 
Cedric Roux committed
422 423 424 425
        return -1;
    }

    /* Store the S-GW teid */
426
    ue_context_p->sgw_s11_teid = create_sess_resp_pP->s11_sgw_teid.teid;
Cedric Roux's avatar
 
Cedric Roux committed
427

428 429 430 431
    //---------------------------------------------------------
    // Process SgwCreateSessionResponse.bearer_context_created
    //---------------------------------------------------------
    bearer_id = create_sess_resp_pP->bearer_context_created.eps_bearer_id/* - 5*/;
Cedric Roux's avatar
 
Cedric Roux committed
432
    /* Depending on s11 result we have to send reject or accept for bearers */
433 434 435 436 437
    DevCheck((bearer_id < BEARERS_PER_UE) && (bearer_id >= 0),
            bearer_id,
            BEARERS_PER_UE,
            0);
    ue_context_p->default_bearer_id = bearer_id;
Cedric Roux's avatar
 
Cedric Roux committed
438

439
    if (create_sess_resp_pP->bearer_context_created.cause != REQUEST_ACCEPTED) {
Cedric Roux's avatar
 
Cedric Roux committed
440 441
        DevMessage("Cases where bearer cause != REQUEST_ACCEPTED are not handled\n");
    }
442
    DevAssert(create_sess_resp_pP->bearer_context_created.s1u_sgw_fteid.interface_type == S1_U_SGW_GTP_U);
Cedric Roux's avatar
 
Cedric Roux committed
443 444 445 446 447 448

    /* Updating statistics */
    mme_app_desc.mme_ue_contexts.nb_bearers_managed++;
    mme_app_desc.mme_ue_contexts.nb_bearers_since_last_stat++;

    current_bearer_p = &ue_context_p->eps_bearers[bearer_id];
449 450 451
    current_bearer_p->s_gw_teid = create_sess_resp_pP->bearer_context_created.s1u_sgw_fteid.teid;
    switch (create_sess_resp_pP->bearer_context_created.s1u_sgw_fteid.ipv4 +
        (create_sess_resp_pP->bearer_context_created.s1u_sgw_fteid.ipv6 << 1))
Cedric Roux's avatar
 
Cedric Roux committed
452 453 454 455 456 457 458 459 460 461
    {
        default:
        case 0: {
            /* No address provided: impossible case */
            DevMessage("No ip address for user-plane provided...\n");
        } break;
        case 1: {
            /* Only IPv4 address */
            current_bearer_p->s_gw_address.pdn_type = IPv4;
            memcpy(current_bearer_p->s_gw_address.address.ipv4_address,
462
                   &create_sess_resp_pP->bearer_context_created.s1u_sgw_fteid.ipv4_address, 4);
Cedric Roux's avatar
 
Cedric Roux committed
463 464 465 466 467
        } break;
        case 2: {
            /* Only IPv6 address */
            current_bearer_p->s_gw_address.pdn_type = IPv6;
            memcpy(current_bearer_p->s_gw_address.address.ipv6_address,
468
                   create_sess_resp_pP->bearer_context_created.s1u_sgw_fteid.ipv6_address, 16);
Cedric Roux's avatar
 
Cedric Roux committed
469 470 471 472 473
        } break;
        case 3: {
            /* Both IPv4 and Ipv6 */
            current_bearer_p->s_gw_address.pdn_type = IPv4_AND_v6;
            memcpy(current_bearer_p->s_gw_address.address.ipv4_address,
474
                   &create_sess_resp_pP->bearer_context_created.s1u_sgw_fteid.ipv4_address, 4);
Cedric Roux's avatar
 
Cedric Roux committed
475
            memcpy(current_bearer_p->s_gw_address.address.ipv6_address,
476
                   create_sess_resp_pP->bearer_context_created.s1u_sgw_fteid.ipv6_address, 16);
Cedric Roux's avatar
 
Cedric Roux committed
477 478
        } break;
    }
479 480 481 482 483 484 485
    current_bearer_p->p_gw_teid = create_sess_resp_pP->bearer_context_created.s5_s8_u_pgw_fteid.teid;
    memset(&current_bearer_p->p_gw_address,0, sizeof(ip_address_t));
    if (create_sess_resp_pP->bearer_context_created.bearer_level_qos != NULL) {
        current_bearer_p->qci                    = create_sess_resp_pP->bearer_context_created.bearer_level_qos->qci;
        current_bearer_p->prio_level             = create_sess_resp_pP->bearer_context_created.bearer_level_qos->pl;
        current_bearer_p->pre_emp_vulnerability  = create_sess_resp_pP->bearer_context_created.bearer_level_qos->pvi;
        current_bearer_p->pre_emp_capability     = create_sess_resp_pP->bearer_context_created.bearer_level_qos->pci;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
486 487 488 489 490 491 492 493 494
        MME_APP_DEBUG("%s set qci %u in bearer %u\n",
                __FUNCTION__,
                current_bearer_p->qci,
                ue_context_p->default_bearer_id);
    } else {
        // if null, it is not modified
        //current_bearer_p->qci                    = ue_context_p->pending_pdn_connectivity_req_qos.qci;
#warning "may force QCI here to 9"
        current_bearer_p->qci                    = 9;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
495 496 497
        current_bearer_p->prio_level             = 1;
        current_bearer_p->pre_emp_vulnerability  = 0;
        current_bearer_p->pre_emp_capability     = 0;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
498 499 500 501
        MME_APP_DEBUG("%s set qci %u in bearer %u (qos not modified by S/P-GW)\n",
                __FUNCTION__,
                current_bearer_p->qci,
                ue_context_p->default_bearer_id);
502
    }
Cedric Roux's avatar
 
Cedric Roux committed
503 504 505 506

    mme_app_dump_ue_contexts(&mme_app_desc.mme_ue_contexts);

    {
507
        //uint8_t *keNB = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
508

509 510 511 512
        message_p = itti_alloc_new_message(TASK_MME_APP, NAS_PDN_CONNECTIVITY_RSP);
        memset((void*)&message_p->ittiMsg.nas_pdn_connectivity_rsp,
                0,
                sizeof(nas_pdn_connectivity_rsp_t));
Cedric Roux's avatar
 
Cedric Roux committed
513

514 515 516
        // moved to NAS_CONNECTION_ESTABLISHMENT_CONF, keNB not handled in NAS MME
        //derive_keNB(ue_context_p->vector_in_use->kasme, 156, &keNB);
        //memcpy(NAS_PDN_CONNECTIVITY_RSP(message_p).keNB, keNB, 32);
Cedric Roux's avatar
 
Cedric Roux committed
517

518
        //free(keNB);
Cedric Roux's avatar
 
Cedric Roux committed
519

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
        NAS_PDN_CONNECTIVITY_RSP(message_p).pti            = ue_context_p->pending_pdn_connectivity_req_pti;   // NAS internal ref
        NAS_PDN_CONNECTIVITY_RSP(message_p).ue_id          = ue_context_p->pending_pdn_connectivity_req_ue_id; // NAS internal ref
        // TO REWORK:
        if ((ue_context_p->pending_pdn_connectivity_req_apn.value != NULL)
            && (ue_context_p->pending_pdn_connectivity_req_apn.length != 0)) {
            DUP_OCTET_STRING(
                    ue_context_p->pending_pdn_connectivity_req_apn,
                    NAS_PDN_CONNECTIVITY_RSP(message_p).apn);
            MME_APP_DEBUG("SET APN FROM NAS PDN CONNECTIVITY CREATE: %s\n", NAS_PDN_CONNECTIVITY_RSP(message_p).apn.value);
        } else {
            int i;
            context_identifier_t context_identifier = ue_context_p->apn_profile.context_identifier;
            for (i = 0; i < ue_context_p->apn_profile.nb_apns; i++) {
                if (ue_context_p->apn_profile.apn_configuration[i].context_identifier == context_identifier) {
                    AssertFatal(ue_context_p->apn_profile.apn_configuration[i].service_selection_length > 0, "Bad APN string (len = 0)");
                    if (ue_context_p->apn_profile.apn_configuration[i].service_selection_length > 0) {
                        NAS_PDN_CONNECTIVITY_RSP(message_p).apn.value  = malloc(ue_context_p->apn_profile.apn_configuration[i].service_selection_length + 1);
                        NAS_PDN_CONNECTIVITY_RSP(message_p).apn.length = ue_context_p->apn_profile.apn_configuration[i].service_selection_length;
                        AssertFatal(ue_context_p->apn_profile.apn_configuration[i].service_selection_length <= APN_MAX_LENGTH,
                                "Bad APN string length %d",
                                ue_context_p->apn_profile.apn_configuration[i].service_selection_length);
                        memcpy(NAS_PDN_CONNECTIVITY_RSP(message_p).apn.value,
                                ue_context_p->apn_profile.apn_configuration[i].service_selection,
                                ue_context_p->apn_profile.apn_configuration[i].service_selection_length);
                        NAS_PDN_CONNECTIVITY_RSP(message_p).apn.value[ue_context_p->apn_profile.apn_configuration[i].service_selection_length] = '\0';
                        MME_APP_DEBUG("SET APN FROM HSS ULA: %s\n", NAS_PDN_CONNECTIVITY_RSP(message_p).apn.value);
                        break;
                    }
                }
            }
        }
        MME_APP_DEBUG("APN: %s\n", NAS_PDN_CONNECTIVITY_RSP(message_p).apn.value);

        switch (create_sess_resp_pP->paa.pdn_type) {
            case IPv4:
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length = 4;
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value  = malloc(NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length+1);
                DevAssert(NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value != NULL);
                memcpy(
                        NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value,
                        create_sess_resp_pP->paa.ipv4_address,
                        NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length);
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value[NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length] = '0';
                break;
            case IPv6:
                DevAssert(create_sess_resp_pP->paa.ipv6_prefix_length == 64); // NAS seems to only support 64 bits
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length = create_sess_resp_pP->paa.ipv6_prefix_length/8;
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value  = malloc(NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length+1);
                DevAssert(NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value != NULL);
                memcpy(
                        NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value,
                        create_sess_resp_pP->paa.ipv6_address,
                        NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length);
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value[NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length] = '0';
                break;
            case IPv4_AND_v6:
                DevAssert(create_sess_resp_pP->paa.ipv6_prefix_length == 64); // NAS seems to only support 64 bits
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length = 4 + create_sess_resp_pP->paa.ipv6_prefix_length/8;
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value  = malloc(NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length+1);
                DevAssert(NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value != NULL);
                memcpy(
                        NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value,
                        create_sess_resp_pP->paa.ipv4_address,
                        4);
                memcpy(
                        &NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value[4],
                        create_sess_resp_pP->paa.ipv6_address,
                        create_sess_resp_pP->paa.ipv6_prefix_length/8);
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value[NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length] = '0';
                break;
            case IPv4_OR_v6:
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length = 4;
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value  = malloc(NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length+1);
                DevAssert(NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value != NULL);
                memcpy(
                        NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value,
                        create_sess_resp_pP->paa.ipv4_address,
                        NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length);
                NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.value[NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_addr.length] = '0';
                break;
            default:
                DevAssert(0);
        }

        NAS_PDN_CONNECTIVITY_RSP(message_p).pdn_type       = create_sess_resp_pP->paa.pdn_type;
        NAS_PDN_CONNECTIVITY_RSP(message_p).proc_data      = ue_context_p->pending_pdn_connectivity_req_proc_data;    // NAS internal ref
        ue_context_p->pending_pdn_connectivity_req_proc_data = NULL;
607 608 609 610 611 612 613 614 615 616 617

#warning "QOS hardcoded here"
        //memcpy(&NAS_PDN_CONNECTIVITY_RSP(message_p).qos,
        //        &ue_context_p->pending_pdn_connectivity_req_qos,
        //        sizeof(network_qos_t));
        NAS_PDN_CONNECTIVITY_RSP(message_p).qos.gbrUL    = 0;
        NAS_PDN_CONNECTIVITY_RSP(message_p).qos.gbrDL    = 0; /* Guaranteed Bit Rate for downlink */
        NAS_PDN_CONNECTIVITY_RSP(message_p).qos.mbrUL    = 50000; /* Maximum Bit Rate for uplink      */
        NAS_PDN_CONNECTIVITY_RSP(message_p).qos.mbrDL    = 100000; /* Maximum Bit Rate for downlink    */
        NAS_PDN_CONNECTIVITY_RSP(message_p).qos.qci      = 9; /* QoS Class Identifier         */

618 619 620 621
        NAS_PDN_CONNECTIVITY_RSP(message_p).request_type   = ue_context_p->pending_pdn_connectivity_req_request_type; // NAS internal ref
        ue_context_p->pending_pdn_connectivity_req_request_type = 0;

        // here at this point OctetString are saved in resp, no loss of memory (apn, pdn_addr)
Cedric Roux's avatar
 
Cedric Roux committed
622

623 624
        NAS_PDN_CONNECTIVITY_RSP(message_p).eNB_ue_s1ap_id = ue_context_p->eNB_ue_s1ap_id;
        NAS_PDN_CONNECTIVITY_RSP(message_p).mme_ue_s1ap_id = ue_context_p->mme_ue_s1ap_id;
Cedric Roux's avatar
 
Cedric Roux committed
625

626
        NAS_PDN_CONNECTIVITY_RSP(message_p).ebi = bearer_id;
Cedric Roux's avatar
 
Cedric Roux committed
627

628 629 630 631 632 633 634
        NAS_PDN_CONNECTIVITY_RSP(message_p).qci = current_bearer_p->qci;
        NAS_PDN_CONNECTIVITY_RSP(message_p).prio_level = current_bearer_p->prio_level;
        NAS_PDN_CONNECTIVITY_RSP(message_p).pre_emp_vulnerability = current_bearer_p->pre_emp_vulnerability;
        NAS_PDN_CONNECTIVITY_RSP(message_p).pre_emp_capability = current_bearer_p->pre_emp_capability;

        NAS_PDN_CONNECTIVITY_RSP(message_p).sgw_s1u_teid = current_bearer_p->s_gw_teid;
        memcpy(&NAS_PDN_CONNECTIVITY_RSP(message_p).sgw_s1u_address,
Cedric Roux's avatar
 
Cedric Roux committed
635 636
               &current_bearer_p->s_gw_address, sizeof(ip_address_t));

637
        memcpy(&NAS_PDN_CONNECTIVITY_RSP(message_p).ambr, &ue_context_p->subscribed_ambr,
Cedric Roux's avatar
 
Cedric Roux committed
638 639
               sizeof(ambr_t));

640
        return itti_send_msg_to_task(TASK_NAS_MME, INSTANCE_DEFAULT, message_p);
Cedric Roux's avatar
 
Cedric Roux committed
641 642 643 644
    }
    return 0;
}

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700


void
mme_app_handle_initial_context_setup_rsp(
        const mme_app_initial_context_setup_rsp_t * const initial_ctxt_setup_rsp_pP)
{
    struct ue_context_s *ue_context_p  = NULL;
    MessageDef          *message_p     = NULL;
    task_id_t            to_task       = TASK_UNKNOWN;

    MME_APP_DEBUG("Received MME_APP_INITIAL_CONTEXT_SETUP_RSP from S1AP\n");

    ue_context_p = mme_ue_context_exists_mme_ue_s1ap_id(
            &mme_app_desc.mme_ue_contexts,
            initial_ctxt_setup_rsp_pP->mme_ue_s1ap_id);
    if (ue_context_p == NULL) {
        MME_APP_DEBUG("We didn't find this mme_ue_s1ap_id in list of UE: %08x %d(dec)\n",
                initial_ctxt_setup_rsp_pP->mme_ue_s1ap_id,
                initial_ctxt_setup_rsp_pP->mme_ue_s1ap_id);
        return;
    }

#if defined(DISABLE_STANDALONE_EPC)
  to_task = TASK_S11;
#else
  to_task = TASK_SPGW_APP;
#endif

  message_p = itti_alloc_new_message(TASK_MME_APP, SGW_MODIFY_BEARER_REQUEST);

    AssertFatal(message_p != NULL, "itti_alloc_new_message Failed");
    memset((void*)&message_p->ittiMsg.sgwModifyBearerRequest,
            0,
            sizeof(SgwModifyBearerRequest));

    SGW_MODIFY_BEARER_REQUEST(message_p).teid                      = ue_context_p->sgw_s11_teid;

    /* Delay Value in integer multiples of 50 millisecs, or zero */
    SGW_MODIFY_BEARER_REQUEST(message_p).delay_dl_packet_notif_req = 0; // TO DO

    SGW_MODIFY_BEARER_REQUEST(message_p).bearer_context_to_modify.eps_bearer_id  = initial_ctxt_setup_rsp_pP->eps_bearer_id;
    memcpy(&SGW_MODIFY_BEARER_REQUEST(message_p).bearer_context_to_modify.s1_eNB_fteid,
            &initial_ctxt_setup_rsp_pP->bearer_s1u_enb_fteid,
            sizeof(SGW_MODIFY_BEARER_REQUEST(message_p).bearer_context_to_modify.s1_eNB_fteid));

    SGW_MODIFY_BEARER_REQUEST(message_p).mme_fq_csid.node_id_type  = GLOBAL_UNICAST_IPv4; // TO DO
    SGW_MODIFY_BEARER_REQUEST(message_p).mme_fq_csid.csid          = 0; // TO DO ...
    SGW_MODIFY_BEARER_REQUEST(message_p).indication_flags          = 0; // TO DO
    SGW_MODIFY_BEARER_REQUEST(message_p).rat_type                  = RAT_EUTRAN;

    /* S11 stack specific parameter. Not used in standalone epc mode */
    SGW_MODIFY_BEARER_REQUEST(message_p).trxn                      = NULL;

    itti_send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
}