Attach.c 48.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

gauthier's avatar
 
gauthier committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/*****************************************************************************

Source      Attach.c

Version     0.1

Date        2012/12/04

Product     NAS stack

Subsystem   EPS Mobility Management

Author      Frederic Maurel

Description Defines the attach related EMM procedure executed by the
        Non-Access Stratum.

        To get internet connectivity from the network, the network
        have to know about the UE. When the UE is switched on, it
        has to initiate the attach procedure to get initial access
        to the network and register its presence to the Evolved
        Packet Core (EPC) network in order to receive EPS services.

        As a result of a successful attach procedure, a context is
        created for the UE in the MME, and a default bearer is esta-
        blished between the UE and the PDN-GW. The UE gets the home
        agent IPv4 and IPv6 addresses and full connectivity to the
        IP network.

        The network may also initiate the activation of additional
        dedicated bearers for the support of a specific service.

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

#include "emm_proc.h"
#include "networkDef.h"
#include "nas_log.h"
#include "nas_timer.h"

#include "emmData.h"
62
#include "emm_timers.h"
gauthier's avatar
 
gauthier committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

#include "emm_sap.h"
#include "esm_sap.h"
#include "emm_cause.h"

#include "NasSecurityAlgorithms.h"

#include <string.h> // memcmp, memcpy
#include <stdlib.h> // malloc, free

/****************************************************************************/
/****************  E X T E R N A L    D E F I N I T I O N S  ****************/
/****************************************************************************/

/****************************************************************************/
/*******************  L O C A L    D E F I N I T I O N S  *******************/
/****************************************************************************/

/* String representation of the EPS attach type */
static const char *_emm_attach_type_str[] = {
  "EPS", "IMSI", "EMERGENCY", "RESERVED"
};

/*
 * --------------------------------------------------------------------------
 *      Internal data handled by the attach procedure in the UE
 * --------------------------------------------------------------------------
 */
/*
 * Timer handlers
 */
94
static void *_emm_attach_t3411_handler(void *args);
gauthier's avatar
 
gauthier committed
95 96 97 98

/*
 * Abnormal case attach procedure
 */
99
static void _emm_attach_abnormal_cases_bcd(nas_user_t *user, emm_sap_t *);
gauthier's avatar
 
gauthier committed
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

/****************************************************************************/
/******************  E X P O R T E D    F U N C T I O N S  ******************/
/****************************************************************************/

/*
 * --------------------------------------------------------------------------
 *          Attach procedure executed by the UE
 * --------------------------------------------------------------------------
 */
/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach()                                         **
 **                                                                        **
 ** Description: Initiate EPS attach procedure to register a UE in PS mode **
 **      of operation for EPS services only, or register a UE for  **
 **      emergency bearer services.                                **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.2                         **
 **      In state EMM-DEREGISTERED, the UE initiates the attach    **
 **      procedure by sending an ATTACH REQUEST message to the MME,**
 **      starting timer T3410 and entering state EMM-REGISTERED-   **
 **      INITIATED.                                                **
 **                                                                        **
 ** Inputs:  type:      Type of the requested attach               **
125
 **      Others:    user->emm_data->                                 **
gauthier's avatar
 
gauthier committed
126 127 128 129 130 131
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3402, T3410, T3411                        **
 **                                                                        **
 ***************************************************************************/
132
int emm_proc_attach(nas_user_t *user, emm_proc_attach_type_t type)
gauthier's avatar
 
gauthier committed
133 134 135 136 137 138 139
{
  LOG_FUNC_IN;

  emm_sap_t emm_sap;
  emm_as_establish_t *emm_as = &emm_sap.u.emm_as.u.establish;
  esm_sap_t esm_sap;
  int rc;
140
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
141 142 143 144 145 146

  LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach type = %s (%d)",
            _emm_attach_type_str[type], type);

  /* Update the emergency bearer service indicator */
  if (type == EMM_ATTACH_TYPE_EMERGENCY) {
147
    user->emm_data->is_emergency = TRUE;
gauthier's avatar
 
gauthier committed
148 149 150 151 152 153 154 155
  }

  /* Setup initial NAS information message to transfer */
  emm_as->NASinfo = EMM_AS_NAS_INFO_ATTACH;
  /* Set the attach type */
  emm_as->type = type;

  /* Set the RRC connection establishment cause */
156
  if (user->emm_data->is_emergency) {
gauthier's avatar
 
gauthier committed
157 158 159 160 161 162 163 164
    emm_as->RRCcause = NET_ESTABLISH_CAUSE_EMERGENCY;
    emm_as->RRCtype = NET_ESTABLISH_TYPE_EMERGENCY_CALLS;
  } else {
    emm_as->RRCcause = NET_ESTABLISH_CAUSE_MO_SIGNAL;
    emm_as->RRCtype = NET_ESTABLISH_TYPE_ORIGINATING_SIGNAL;
  }

  /* Set the PLMN identifier of the selected PLMN */
165
  emm_as->plmnID = &user->emm_data->splmn;
gauthier's avatar
 
gauthier committed
166 167 168 169 170 171 172 173 174
  /*
   * Process the EPS mobile identity
   */
  emm_as->UEid.guti = NULL;
  emm_as->UEid.tai  = NULL;
  emm_as->UEid.imsi = NULL;
  emm_as->UEid.imei = NULL;

  /* Check whether the UE is configured for "AttachWithIMSI" */
175
  if (user->emm_data->AttachWithImsi) {
gauthier's avatar
 
gauthier committed
176 177
    /* Check whether the selected PLMN is neither the registered PLMN
     * nor in the list of equivalent PLMNs */
178
    if ( (!user->emm_data->is_rplmn) && (!user->emm_data->is_eplmn) ) {
gauthier's avatar
 
gauthier committed
179 180
      LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach with IMSI");
      /* Include the IMSI */
181
      emm_as->UEid.imsi = user->emm_data->imsi;
gauthier's avatar
 
gauthier committed
182 183 184
    } else {
      LOG_TRACE(INFO,
                "EMM-PROC  - Initiate EPS attach with NO IMSI, is registered PLMN %d, is equivalent PLMN %d",
185 186
                user->emm_data->is_rplmn,
                user->emm_data->is_eplmn);
gauthier's avatar
 
gauthier committed
187
    }
188
  } else if (user->emm_data->guti) {
gauthier's avatar
 
gauthier committed
189 190
    LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach with GUTI");
    /* Include a valid GUTI and the last visited registered TAI */
191 192 193
    emm_as->UEid.guti = user->emm_data->guti;
    emm_as->UEid.tai = user->emm_data->tai;
  } else if (!user->emm_data->is_emergency) {
gauthier's avatar
 
gauthier committed
194 195
    LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach with IMSI cause is no emergency and no GUTI");
    /* Include the IMSI if no valid GUTI is available */
196
    emm_as->UEid.imsi = user->emm_data->imsi;
gauthier's avatar
 
gauthier committed
197 198 199
  } else {
    /* The UE is attaching for emergency bearer services and
     * does not hold a valid GUTI */
200
    if (user->emm_data->imsi) {
gauthier's avatar
 
gauthier committed
201 202
      /* Include the IMSI if valid (USIM is present) */
      LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach with IMSI cause is emergency and no GUTI");
203
      emm_as->UEid.imsi = user->emm_data->imsi;
gauthier's avatar
 
gauthier committed
204 205 206
    } else {
      LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach with IMSI cause is emergency and no GUTI and no IMSI");
      /* Include the IMEI if the IMSI is not valid */
207
      emm_as->UEid.imei = user->emm_data->imei;
gauthier's avatar
 
gauthier committed
208 209 210 211
    }
  }

  /* Setup EPS NAS security data */
212
  emm_as_set_security_data(&emm_as->sctx, user->emm_data->security, FALSE, FALSE);
gauthier's avatar
 
gauthier committed
213 214
  emm_as->ksi = EMM_AS_NO_KEY_AVAILABLE;

215 216 217
  if (user->emm_data->security) {
    if (user->emm_data->security->type != EMM_KSI_NOT_AVAILABLE) {
      emm_as->ksi = user->emm_data->security->eksi;
gauthier's avatar
 
gauthier committed
218 219
    }

220 221 222 223
    LOG_TRACE(INFO, "EMM-PROC  - eps_encryption 0x%X", user->emm_data->security->capability.eps_encryption);
    LOG_TRACE(INFO, "EMM-PROC  - eps_integrity  0x%X", user->emm_data->security->capability.eps_integrity);
    emm_as->encryption = user->emm_data->security->capability.eps_encryption;
    emm_as->integrity = user->emm_data->security->capability.eps_integrity;
gauthier's avatar
 
gauthier committed
224 225 226 227 228 229 230 231 232 233 234
  }

  /*
   * Notify ESM that initiation of a PDN connectivity procedure
   * is requested to setup a default EPS bearer
   */
  esm_sap.primitive = ESM_PDN_CONNECTIVITY_REQ;
  esm_sap.is_standalone = FALSE;
  esm_sap.data.pdn_connect.is_defined = TRUE;
  esm_sap.data.pdn_connect.cid = 1;
  /* TODO: PDN type should be set according to the IP capability of the UE */
235
  esm_sap.data.pdn_connect.pdn_type = NET_PDN_TYPE_IPV4;
gauthier's avatar
 
gauthier committed
236
  esm_sap.data.pdn_connect.apn = NULL;
237
  esm_sap.data.pdn_connect.is_emergency = user->emm_data->is_emergency;
238
  rc = esm_sap_send(user, &esm_sap);
gauthier's avatar
 
gauthier committed
239 240 241 242 243 244

  if (rc != RETURNerror) {
    /* Setup EMM procedure handler to be executed upon receiving
     * lower layer notification */
    rc = emm_proc_lowerlayer_initialize(emm_proc_attach_request,
                                        emm_proc_attach_failure,
245
                                        emm_proc_attach_release, user);
gauthier's avatar
 
gauthier committed
246 247 248 249 250 251 252

    if (rc != RETURNok) {
      LOG_TRACE(WARNING, "Failed to initialize EMM procedure handler");
      LOG_FUNC_RETURN (RETURNerror);
    }

    /* Start T3410 timer */
253
    emm_timers->T3410.id = nas_timer_start(emm_timers->T3410.sec, emm_attach_t3410_handler, user);
gauthier's avatar
 
gauthier committed
254
    LOG_TRACE(INFO,"EMM-PROC  - Timer T3410 (%d) expires in %ld seconds",
255
              emm_timers->T3410.id, emm_timers->T3410.sec);
gauthier's avatar
 
gauthier committed
256
    /* Stop T3402 and T3411 timers if running */
257 258
    emm_timers->T3402.id = nas_timer_stop(emm_timers->T3402.id);
    emm_timers->T3411.id = nas_timer_stop(emm_timers->T3411.id);
gauthier's avatar
 
gauthier committed
259 260 261 262 263 264 265 266 267 268

    /*
     * Notify EMM-AS SAP that a RRC connection establishment procedure
     * is requested from the Access-Stratum to send initial NAS message
     * attach request to the network
     */
    emm_sap.primitive = EMMAS_ESTABLISH_REQ;
    /* Get the PDN connectivity request to transfer within the ESM
     * container of the initial attach request message */
    emm_sap.u.emm_as.u.establish.NASmsg = esm_sap.send;
269
    rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  }

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_request()                                 **
 **                                                                        **
 ** Description: Performs the attach procedure upon receipt of indication  **
 **      from lower layers that Attach Request message has been    **
 **      successfully delivered to the network.                    **
 **                                                                        **
 ** Inputs:  args:      Not used                                   **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_request(void *args)
{
  LOG_FUNC_IN;
294
  nas_user_t *user=args;
gauthier's avatar
 
gauthier committed
295 296 297 298 299 300 301
  emm_sap_t emm_sap;
  int rc;

  /*
   * Notify EMM that Attach Request has been sent to the network
   */
  emm_sap.primitive = EMMREG_ATTACH_REQ;
302
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_accept()                                  **
 **                                                                        **
 ** Description: Performs the attach procedure accepted by the network.    **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.4                         **
 **      Upon receiving the ATTACH ACCEPT message, the UE shall    **
 **      stop timer T3410 and send an ATTACH COMPLETE message to   **
 **      the MME.                                                  **
 **                                                                        **
 ** Inputs:  t3412:     Value of the T3412 timer in seconds        **
 **      t3402:     Value of the T3402 timer in seconds        **
 **      t3423:     Value of the T3423 timer in seconds        **
 **      n_tais:    Number of tracking area identities contai- **
 **             ned in the TAI list                        **
 **      tai:       The TAI list that identifies the tracking  **
 **             areas the UE is registered to              **
 **      guti:      New UE's temporary identity assigned by    **
 **             the MME (GUTI reallocation)                **
 **      n_eplmns:  Number of equivalent PLMNs                 **
 **      eplmns:    List of equivalent PLMNs                   **
 **      esm_msg_pP:   Activate default EPS bearer context re-    **
 **             quest ESM message                          **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
335
 **      Others:    user->emm_data-> T3412, T3402, T3423             **
gauthier's avatar
 
gauthier committed
336 337
 **                                                                        **
 ***************************************************************************/
338
int emm_proc_attach_accept(nas_user_t *user, long t3412, long t3402, long t3423,
gauthier's avatar
 
gauthier committed
339 340 341 342 343 344 345 346 347 348 349
                           int n_tais, tai_t *tai, GUTI_t *guti,
                           int n_eplmns, plmn_t *eplmn,
                           const OctetString *esm_msg_pP)
{
  LOG_FUNC_IN;

  emm_sap_t emm_sap;
  esm_sap_t esm_sap;
  int rc;
  int i;
  int j;
350
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
351 352 353 354

  LOG_TRACE(INFO, "EMM-PROC  - EPS attach accepted by the network");

  /* Stop timer T3410 */
355 356
  LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3410 (%d)", emm_timers->T3410.id);
  emm_timers->T3410.id = nas_timer_stop(emm_timers->T3410.id);
gauthier's avatar
 
gauthier committed
357 358

  /* Delete old TAI list and store the received TAI list */
359
  user->emm_data->ltai.n_tais = n_tais;
gauthier's avatar
 
gauthier committed
360 361

  for (i = 0; (i < n_tais) && (i < EMM_DATA_TAI_MAX); i++) {
362
    user->emm_data->ltai.tai[i] = tai[i];
gauthier's avatar
 
gauthier committed
363 364 365
  }

  /* Update periodic tracking area update timer value */
366
  emm_timers->T3412.sec = t3412;
gauthier's avatar
 
gauthier committed
367 368 369

  /* Update attach failure timer value */
  if ( !(t3402 < 0) ) {
370
    emm_timers->T3402.sec = t3402;
gauthier's avatar
 
gauthier committed
371 372 373 374
  }

  /* Update E-UTRAN deactivate ISR timer value */
  if ( !(t3423 < 0) ) {
375
    emm_timers->T3423.sec = t3423;
gauthier's avatar
 
gauthier committed
376 377 378 379
  }

  /* Delete old GUTI and store the new assigned GUTI if provided */
  if (guti) {
380
    *user->emm_data->guti = *guti;
gauthier's avatar
 
gauthier committed
381 382 383
  }

  /* Update the stored list of equivalent PLMNs */
384
  user->emm_data->nvdata.eplmn.n_plmns = 0;
gauthier's avatar
 
gauthier committed
385 386 387 388 389

  if (n_eplmns > 0) {
    for (i = 0; (i < n_eplmns) && (i < EMM_DATA_EPLMN_MAX); i++) {
      int is_forbidden = FALSE;

390
      if (!user->emm_data->is_emergency) {
gauthier's avatar
 
gauthier committed
391 392 393
        /* If the attach procedure is not for emergency bearer
         * services, the UE shall remove from the list any PLMN
         * code that is already in the list of forbidden PLMNs */
394 395
        for (j = 0; j < user->emm_data->fplmn.n_plmns; j++) {
          if (PLMNS_ARE_EQUAL(eplmn[i], user->emm_data->fplmn.plmn[j])) {
gauthier's avatar
 
gauthier committed
396 397 398 399 400 401 402
            is_forbidden = TRUE;
            break;
          }
        }
      }

      if ( !is_forbidden ) {
403
        user->emm_data->nvdata.eplmn.plmn[user->emm_data->nvdata.eplmn.n_plmns++] =
gauthier's avatar
 
gauthier committed
404 405 406 407 408
          eplmn[i];
      }
    }

    /* Add the PLMN code of the registered PLMN that sent the list */
409 410 411
    if (user->emm_data->nvdata.eplmn.n_plmns < EMM_DATA_EPLMN_MAX) {
      user->emm_data->nvdata.eplmn.plmn[user->emm_data->nvdata.eplmn.n_plmns++] =
        user->emm_data->splmn;
gauthier's avatar
 
gauthier committed
412 413 414 415 416 417 418 419 420
    }
  }

  /*
   * Notify ESM that a default EPS bearer has to be activated
   */
  esm_sap.primitive = ESM_DEFAULT_EPS_BEARER_CONTEXT_ACTIVATE_REQ;
  esm_sap.is_standalone = FALSE;
  esm_sap.recv = esm_msg_pP;
421
  rc = esm_sap_send(user, &esm_sap);
gauthier's avatar
 
gauthier committed
422 423 424 425 426 427

  if ( (rc != RETURNerror) && (esm_sap.err == ESM_SAP_SUCCESS) ) {
    /* Setup EMM procedure handler to be executed upon receiving
     * lower layer notification */
    rc = emm_proc_lowerlayer_initialize(emm_proc_attach_complete,
                                        emm_proc_attach_failure,
428
                                        NULL, user);
gauthier's avatar
 
gauthier committed
429 430 431 432 433 434 435 436 437 438 439 440 441

    if (rc != RETURNok) {
      LOG_TRACE(WARNING,
                "EMM-PROC  - Failed to initialize EMM procedure handler");
      LOG_FUNC_RETURN (RETURNerror);
    }

    /*
     * Notify EMM-AS SAP that Attach Complete message together with
     * an Activate Default EPS Bearer Context Accept message has to
     * be sent to the network
     */
    emm_sap.primitive = EMMAS_DATA_REQ;
442
    emm_sap.u.emm_as.u.data.guti = user->emm_data->guti;
Frédéric Leroy's avatar
Frédéric Leroy committed
443
    emm_sap.u.emm_as.u.data.ueid = user->ueid;
gauthier's avatar
 
gauthier committed
444 445
    /* Setup EPS NAS security data */
    emm_as_set_security_data(&emm_sap.u.emm_as.u.data.sctx,
446
                             user->emm_data->security, FALSE, TRUE);
gauthier's avatar
 
gauthier committed
447 448 449 450 451
    /* Get the activate default EPS bearer context accept message
     * to be transfered within the ESM container of the attach
     * complete message */
    emm_sap.u.emm_as.u.data.NASinfo = EMM_AS_NAS_DATA_ATTACH;
    emm_sap.u.emm_as.u.data.NASmsg = esm_sap.send;
452
    rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
453 454 455 456 457 458 459 460
  } else if (esm_sap.err != ESM_SAP_DISCARDED) {
    /* 3GPP TS 24.301, section 5.5.1.2.6, case j
     * If the ACTIVATE DEFAULT BEARER CONTEXT REQUEST message combined
     * with the ATTACH ACCEPT is not accepted by the UE due to failure
     * in the UE ESM sublayer, then the UE shall initiate the detach
     * procedure by sending a DETACH REQUEST message to the network.
     */
    emm_sap.primitive = EMMREG_DETACH_INIT;
461
    rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
  } else {
    /*
     * ESM procedure failed and, received message has been discarded or
     * Status message has been returned; ignore ESM procedure failure
     */
    rc = RETURNok;
  }

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_reject()                                  **
 **                                                                        **
 ** Description: Performs the attach procedure rejected by the network.    **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.5                         **
 **      Upon receiving the ATTACH REJECT message, the UE shall    **
 **      stop timer T3410 and take actions depending on the EMM    **
 **      cause value received.                                     **
 **                                                                        **
 ** Inputs:  emm_cause: EMM cause indicating why the network re-   **
 **             jected the attach request                  **
 **      esm_msg_pP:   PDN connectivity reject ESM message        **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **                                                                        **
 ***************************************************************************/
493
int emm_proc_attach_reject(nas_user_t *user, int emm_cause, const OctetString *esm_msg_pP)
gauthier's avatar
 
gauthier committed
494 495 496 497 498
{
  LOG_FUNC_IN;

  emm_sap_t emm_sap;
  int rc;
499
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
500
  emm_attach_data_t *emm_attach_data = user->emm_data->emm_attach_data;
gauthier's avatar
 
gauthier committed
501 502 503 504 505

  LOG_TRACE(WARNING, "EMM-PROC  - EPS attach rejected by the network, "
            "EMM cause = %d", emm_cause);

  /* Stop timer T3410 */
506 507
  LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3410 (%d)", emm_timers->T3410.id);
  emm_timers->T3410.id = nas_timer_stop(emm_timers->T3410.id);
gauthier's avatar
 
gauthier committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

  /* Update the EPS update status, the GUTI, the visited registered TAI and
   * the eKSI */
  switch (emm_cause) {
  case EMM_CAUSE_ILLEGAL_UE:
  case EMM_CAUSE_ILLEGAL_ME:
  case EMM_CAUSE_EPS_NOT_ALLOWED:
  case EMM_CAUSE_BOTH_NOT_ALLOWED:
  case EMM_CAUSE_PLMN_NOT_ALLOWED:
  case EMM_CAUSE_NOT_AUTHORIZED_IN_PLMN:
  case EMM_CAUSE_EPS_NOT_ALLOWED_IN_PLMN:
  case EMM_CAUSE_TA_NOT_ALLOWED:
  case EMM_CAUSE_ROAMING_NOT_ALLOWED:
  case EMM_CAUSE_NO_SUITABLE_CELLS:
    /* Set the EPS update status to EU3 ROAMING NOT ALLOWED */
523
    user->emm_data->status = EU3_ROAMING_NOT_ALLOWED;
gauthier's avatar
 
gauthier committed
524
    /* Delete the GUTI */
525
    user->emm_data->guti = NULL;
gauthier's avatar
 
gauthier committed
526
    /* Delete the last visited registered TAI */
527
    user->emm_data->tai = NULL;
gauthier's avatar
 
gauthier committed
528 529

    /* Delete the eKSI */
530 531
    if (user->emm_data->security) {
      user->emm_data->security->type = EMM_KSI_NOT_AVAILABLE;
gauthier's avatar
 
gauthier committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    }

    break;

  default :
    break;
  }

  /* Update list of equivalents PLMNs and attach attempt counter */
  switch (emm_cause) {
  case EMM_CAUSE_ILLEGAL_UE:
  case EMM_CAUSE_ILLEGAL_ME:
  case EMM_CAUSE_EPS_NOT_ALLOWED:
  case EMM_CAUSE_BOTH_NOT_ALLOWED:
    /* Consider the USIM as invalid for EPS services */
547
    user->emm_data->usim_is_valid = FALSE;
gauthier's avatar
 
gauthier committed
548
    /* Delete the list of equivalent PLMNs */
549
    user->emm_data->nvdata.eplmn.n_plmns = 0;
gauthier's avatar
 
gauthier committed
550 551 552 553 554 555
    break;

  case EMM_CAUSE_PLMN_NOT_ALLOWED:
  case EMM_CAUSE_NOT_AUTHORIZED_IN_PLMN:
  case EMM_CAUSE_ROAMING_NOT_ALLOWED:
    /* Delete the list of equivalent PLMNs */
556
    user->emm_data->nvdata.eplmn.n_plmns = 0;
gauthier's avatar
 
gauthier committed
557
    /* Reset the attach attempt counter */
558
    emm_attach_data->attempt_count = 0;
gauthier's avatar
 
gauthier committed
559 560 561 562 563 564
    break;

  case EMM_CAUSE_TA_NOT_ALLOWED:
  case EMM_CAUSE_EPS_NOT_ALLOWED_IN_PLMN:
  case EMM_CAUSE_NO_SUITABLE_CELLS:
    /* Reset the attach attempt counter */
565
    emm_attach_data->attempt_count = 0;
gauthier's avatar
 
gauthier committed
566 567 568 569 570
    break;

  case EMM_CAUSE_ESM_FAILURE:

    /* 3GPP TS 24.301, section 5.5.1.2.6, case d */
571
    if (user->emm_data->NAS_SignallingPriority != 1) {
gauthier's avatar
 
gauthier committed
572 573
      /* The UE is not configured for NAS signalling low priority;
       * set the attach attempt counter to 5 */
574
      emm_attach_data->attempt_count = EMM_ATTACH_COUNTER_MAX;
gauthier's avatar
 
gauthier committed
575 576 577 578 579 580 581 582 583 584 585
    }

    break;

  case EMM_CAUSE_SEMANTICALLY_INCORRECT:
  case EMM_CAUSE_INVALID_MANDATORY_INFO:
  case EMM_CAUSE_MESSAGE_TYPE_NOT_IMPLEMENTED:
  case EMM_CAUSE_IE_NOT_IMPLEMENTED:
  case EMM_CAUSE_PROTOCOL_ERROR:
    /* 3GPP TS 24.301, section 5.5.1.2.6, case d
     * Set the attach attempt counter to 5 */
586
    emm_attach_data->attempt_count = EMM_ATTACH_COUNTER_MAX;
gauthier's avatar
 
gauthier committed
587 588 589 590 591 592 593 594 595 596 597
    break;

  default :
    break;
  }

  /* Update "forbidden lists" */
  switch (emm_cause) {
  case EMM_CAUSE_PLMN_NOT_ALLOWED:
  case EMM_CAUSE_NOT_AUTHORIZED_IN_PLMN:
    /* Store the PLMN identity in the "forbidden PLMN list" */
598
    user->emm_data->fplmn.plmn[user->emm_data->fplmn.n_plmns++] = user->emm_data->splmn;
gauthier's avatar
 
gauthier committed
599 600 601 602 603
    break;

  case EMM_CAUSE_TA_NOT_ALLOWED:
    /* Store the current TAI in the list of "forbidden tracking
     * areas for regional provision of service" */
604
    user->emm_data->ftai.tai[user->emm_data->ftai.n_tais++] = *user->emm_data->tai;
gauthier's avatar
 
gauthier committed
605 606 607 608 609
    break;

  case EMM_CAUSE_ROAMING_NOT_ALLOWED:
    /* Store the current TAI in the list of "forbidden tracking
     * areas for roaming" */
610
    user->emm_data->ftai_roaming.tai[user->emm_data->ftai_roaming.n_tais++] = *user->emm_data->tai;
gauthier's avatar
 
gauthier committed
611 612 613 614 615
    break;

  case EMM_CAUSE_EPS_NOT_ALLOWED_IN_PLMN:
    /* Store the PLMN identity in the "forbidden PLMNs for GPRS
     * service" list */
616
    user->emm_data->fplmn_gprs.plmn[user->emm_data->fplmn_gprs.n_plmns++] = user->emm_data->splmn;
gauthier's avatar
 
gauthier committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    break;

  default :
    break;
  }

  /* Update state of EMM sublayer */
  switch (emm_cause) {
  case EMM_CAUSE_ILLEGAL_UE:
  case EMM_CAUSE_ILLEGAL_ME:
  case EMM_CAUSE_EPS_NOT_ALLOWED:
  case EMM_CAUSE_BOTH_NOT_ALLOWED:
    /*
     * Notify EMM that EPS attach is rejected
     */
    emm_sap.primitive = EMMREG_ATTACH_REJ;
    break;

  case EMM_CAUSE_PLMN_NOT_ALLOWED:
  case EMM_CAUSE_NOT_AUTHORIZED_IN_PLMN:
  case EMM_CAUSE_EPS_NOT_ALLOWED_IN_PLMN:
    /*
     * Notify EMM that the UE has to perform a PLMN selection because
     * it is not allowed to operate in the currently selected PLMN
     */
    emm_sap.primitive = EMMREG_REGISTER_REQ;
    break;

  case EMM_CAUSE_TA_NOT_ALLOWED:
  case EMM_CAUSE_ROAMING_NOT_ALLOWED:
  case EMM_CAUSE_NO_SUITABLE_CELLS:
    /*
     * Notify EMM that the UE failed to register to the network for
     * EPS services because it is not allowed to operate in the
     * requested tracking area
     */
    emm_sap.primitive = EMMREG_REGISTER_REJ;
    break;

  case EMM_CAUSE_IMEI_NOT_ACCEPTED:
657
    if (user->emm_data->is_emergency) {
gauthier's avatar
 
gauthier committed
658 659 660 661 662 663 664 665 666 667 668 669 670
      /*
       * Notify EMM that the UE failed to register to the network
       * for emergency bearer services because "IMEI not accepted"
       */
      emm_sap.primitive = EMMREG_NO_IMSI;
      break;
    }

    /* break is volontary missing */

  default :
    /* Other values are considered as abnormal cases
     * 3GPP TS 24.301, section 5.5.1.2.6, case d */
671
    _emm_attach_abnormal_cases_bcd(user, &emm_sap);
gauthier's avatar
 
gauthier committed
672 673 674
    break;
  }

675
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
676 677 678 679 680 681 682 683 684

  /*
   * Notify ESM that the network rejected connectivity to the PDN
   */
  if (esm_msg_pP != NULL) {
    esm_sap_t esm_sap;
    esm_sap.primitive = ESM_PDN_CONNECTIVITY_REJ;
    esm_sap.is_standalone = FALSE;
    esm_sap.recv = esm_msg_pP;
685
    rc = esm_sap_send(user, &esm_sap);
gauthier's avatar
 
gauthier committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  }

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_complete()                                **
 **                                                                        **
 ** Description: Terminates the attach procedure when Attach Complete mes- **
 **      sage has been successfully delivered to the MME.          **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.4                         **
 **      Upon successfully sending the ATTACH COMPLETE message,    **
 **      the UE shall reset the attach attempt counter and tra-    **
 **      cking area updating attempt counter, enter state EMM-     **
 **      REGISTERED and set the EPS update status to EU1-UPDATED.  **
 **                                                                        **
 ** Inputs:  args:      Not used                                   **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_complete(void *args)
{
  LOG_FUNC_IN;

715 716
  nas_user_t *user = args;
  emm_attach_data_t *emm_attach_data = user->emm_data->emm_attach_data;
gauthier's avatar
 
gauthier committed
717 718 719 720 721 722 723 724 725 726
  emm_sap_t emm_sap;
  esm_sap_t esm_sap;
  int rc;

  LOG_TRACE(INFO, "EMM-PROC  - EPS attach complete");

  /* Reset EMM procedure handler */
  (void) emm_proc_lowerlayer_initialize(NULL, NULL, NULL, NULL);

  /* Reset the attach attempt counter */
727
  emm_attach_data->attempt_count = 0;
gauthier's avatar
 
gauthier committed
728 729 730
  /* TODO: Reset the tracking area updating attempt counter */

  /* Set the EPS update status to EU1 UPDATED */
731 732
  user->emm_data->status = EU1_UPDATED;
  user->emm_data->is_attached = TRUE;
gauthier's avatar
 
gauthier committed
733 734 735 736 737 738

  /*
   * Notify EMM that network attach complete message has been delivered
   * to the network
   */
  emm_sap.primitive = EMMREG_ATTACH_CNF;
739
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
740 741 742 743 744 745 746 747 748

  if (rc != RETURNerror) {
    /*
     * Notify ESM that the Activate Default EPS Bearer Context Accept
     * message has been delivered to the network within the Attach
     * Complete message
     */
    esm_sap.primitive = ESM_DEFAULT_EPS_BEARER_CONTEXT_ACTIVATE_CNF;
    esm_sap.is_standalone = FALSE;
749
    rc = esm_sap_send(user, &esm_sap);
gauthier's avatar
 
gauthier committed
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
  }

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_failure()                                 **
 **                                                                        **
 ** Description: Performs the attach procedure abnormal case upon receipt  **
 **      of transmission failure of Attach Request message or At-  **
 **      tach Complete message.                                    **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.6, cases h and i          **
 **      The  UE  shall restart the attach  procedure when timer   **
 **      T3411 expires.                                            **
 **                                                                        **
 ** Inputs:  is_initial:    TRUE if the NAS message that failed to be  **
 **             transfered is an initial NAS message (ESM  **
 **             message embedded within an Attach Request  **
 **             message)                                   **
 **          args:      Not used                                   **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3410, T3411                               **
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_failure(int is_initial, void *args)
{
  LOG_FUNC_IN;
  int rc = RETURNok;
  esm_sap_t esm_sap;
784
  nas_user_t *user=args;
785
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
786 787 788 789 790 791 792

  LOG_TRACE(WARNING, "EMM-PROC  - EPS attach failure");

  /* Reset EMM procedure handler */
  (void) emm_proc_lowerlayer_initialize(NULL, NULL, NULL, NULL);

  /* Stop timer T3410 if still running */
793 794 795
  if (emm_timers->T3410.id != NAS_TIMER_INACTIVE_ID) {
    LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3410 (%d)", emm_timers->T3410.id);
    emm_timers->T3410.id = nas_timer_stop(emm_timers->T3410.id);
gauthier's avatar
 
gauthier committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
  }

  if (is_initial) {
    /*
     * Notify ESM that the PDN CONNECTIVITY REQUEST message contained
     * in the ESM message container IE of the ATTACH REQUEST has failed
     * to be transmitted
     */
    esm_sap.primitive = ESM_PDN_CONNECTIVITY_REJ;
    esm_sap.is_standalone = FALSE;
    esm_sap.recv = NULL;
  } else {
    /*
     * Notify ESM that ACTIVATE DEFAULT EPS BEARER CONTEXT REQUEST message
     * contained in the ESM message container IE of the ATTACH COMPLETE
     * has failed to be transmitted
     */
    esm_sap.primitive = ESM_DEFAULT_EPS_BEARER_CONTEXT_ACTIVATE_REJ;
    esm_sap.is_standalone = FALSE;
    esm_sap.recv = NULL;
  }

818
  rc = esm_sap_send(user, &esm_sap);
gauthier's avatar
 
gauthier committed
819 820 821

  if (rc != RETURNerror) {
    /* Start T3411 timer */
822
    emm_timers->T3411.id = nas_timer_start(emm_timers->T3411.sec, _emm_attach_t3411_handler, NULL);
gauthier's avatar
 
gauthier committed
823
    LOG_TRACE(INFO, "EMM-PROC  - Timer T3411 (%d) expires in %ld seconds",
824
              emm_timers->T3411.id, emm_timers->T3411.sec);
gauthier's avatar
 
gauthier committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
  }

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_release()                                 **
 **                                                                        **
 ** Description: Performs the attach procedure abnormal case upon receipt  **
 **      of NAS signalling connection release indication.          **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.6, case b                 **
 **      The attach procedure shall be aborted and the UE shall    **
 **      execute abnormal case attach procedure.                   **
 **                                                                        **
 ** Inputs:  args:      Not used                                   **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_release(void *args)
{
  LOG_FUNC_IN;
852
  nas_user_t *user=args;
gauthier's avatar
 
gauthier committed
853 854 855 856 857 858
  emm_sap_t emm_sap;
  int rc;

  LOG_TRACE(WARNING, "EMM-PROC  - NAS signalling connection released");

  /* Execute abnormal case attach procedure */
859
  _emm_attach_abnormal_cases_bcd(user, &emm_sap);
gauthier's avatar
 
gauthier committed
860

861
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_restart()                                 **
 **                                                                        **
 ** Description: Restarts the attach procedure                             **
 **                                                                        **
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
 **                                                                        **
 ***************************************************************************/
880
int emm_proc_attach_restart(nas_user_t *user)
gauthier's avatar
 
gauthier committed
881 882 883 884 885 886 887 888 889 890 891 892
{
  LOG_FUNC_IN;

  emm_sap_t emm_sap;
  int rc;

  LOG_TRACE(INFO, "EMM-PROC  - Restart EPS attach procedure");

  /*
   * Notify EMM that the attach procedure has to be restarted
   */
  emm_sap.primitive = EMMREG_ATTACH_INIT;
893
  emm_sap.u.emm_reg.u.attach.is_emergency = user->emm_data->is_emergency;
894
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_set_emergency()                           **
 **                                                                        **
 ** Description: Set the emergency bearer services indicator               **
 **                                                                        **
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
910
 **      Others:    user->emm_data->                                 **
gauthier's avatar
 
gauthier committed
911 912
 **                                                                        **
 ***************************************************************************/
913
int emm_proc_attach_set_emergency(emm_data_t *emm_data)
gauthier's avatar
 
gauthier committed
914 915 916 917 918 919
{
  LOG_FUNC_IN;

  LOG_TRACE(WARNING, "EMM-PROC  - UE is now attached to the network for "
            "emergency bearer services only");

920
  emm_data->is_emergency = TRUE;
gauthier's avatar
 
gauthier committed
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936

  LOG_FUNC_RETURN(RETURNok);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_attach_set_detach()                              **
 **                                                                        **
 ** Description: Reset the network attachment indicator and enter state    **
 **      EMM-DEREGISTERED
 **                                                                        **
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
937
 **      Others:    user->emm_data->                                 **
gauthier's avatar
 
gauthier committed
938 939
 **                                                                        **
 ***************************************************************************/
940
int emm_proc_attach_set_detach(void *nas_user)
gauthier's avatar
 
gauthier committed
941 942 943
{
  LOG_FUNC_IN;

944
  nas_user_t *user=nas_user;
gauthier's avatar
 
gauthier committed
945 946 947 948 949 950
  int rc;

  LOG_TRACE(WARNING,
            "EMM-PROC  - UE is now locally detached from the network");

  /* Reset the network attachment indicator */
951
  user->emm_data->is_attached = FALSE;
gauthier's avatar
 
gauthier committed
952 953 954 955 956
  /*
   * Notify that the UE is locally detached from the network
   */
  emm_sap_t emm_sap;
  emm_sap.primitive = EMMREG_DETACH_CNF;
957
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

  LOG_FUNC_RETURN(rc);
}


/****************************************************************************/
/*********************  L O C A L    F U N C T I O N S  *********************/
/****************************************************************************/

/*
 * --------------------------------------------------------------------------
 *              Timer handlers
 * --------------------------------------------------------------------------
 */

/****************************************************************************
 **                                                                        **
 ** Name:    _emm_attach_t3410_handler()                               **
 **                                                                        **
 ** Description: T3410 timeout handler                                     **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.6, case c                 **
 **      Upon T3410 timer expiration, the attach procedure shall   **
 **      be aborted and the UE shall execute abnormal case attach  **
 **      procedure.                                                **
 **      The NAS signalling connection shall be released locally.  **
 **                                                                        **
 ** Inputs:  args:      handler parameters                         **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    T3410                                      **
 **                                                                        **
 ***************************************************************************/
993
void *emm_attach_t3410_handler(void *args)
gauthier's avatar
 
gauthier committed
994 995 996
{
  LOG_FUNC_IN;

997
  nas_user_t *user=args;
998
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
999 1000 1001 1002 1003 1004
  emm_sap_t emm_sap;
  int rc;

  LOG_TRACE(WARNING, "EMM-PROC  - T3410 timer expired");

  /* Stop T3410 timer */
1005
  emm_timers->T3410.id = nas_timer_stop(emm_timers->T3410.id);
gauthier's avatar
 
gauthier committed
1006
  /* Execute abnormal case attach procedure */
1007
  _emm_attach_abnormal_cases_bcd(user, &emm_sap);
gauthier's avatar
 
gauthier committed
1008

1009
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
1010 1011 1012

  if (rc != RETURNerror) {
    /* Locally release the NAS signalling connection */
1013
    user->emm_data->ecm_status = ECM_IDLE;
gauthier's avatar
 
gauthier committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
  }

  LOG_FUNC_RETURN(NULL);
}

/****************************************************************************
 **                                                                        **
 ** Name:    _emm_attach_t3411_handler()                               **
 **                                                                        **
 ** Description: T3411 timeout handler                                     **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.6                         **
 **      Upon T3411 timer expiration, the attach procedure shall   **
 **      be restarted, if still required by ESM sublayer.          **
 **                                                                        **
 ** Inputs:  args:      handler parameters                         **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    T3411                                      **
 **                                                                        **
 ***************************************************************************/
static void *_emm_attach_t3411_handler(void *args)
{
  LOG_FUNC_IN;

1041
  nas_user_t *user=args;
1042
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
1043 1044 1045 1046 1047
  emm_sap_t emm_sap;

  LOG_TRACE(WARNING, "EMM-PROC  - T3411 timer expired");

  /* Stop T3411 timer */
1048
  emm_timers->T3411.id = nas_timer_stop(emm_timers->T3411.id);
gauthier's avatar
 
gauthier committed
1049 1050 1051 1052 1053
  /*
   * Notify EMM that timer T3411 expired and attach procedure has to be
   * restarted
   */
  emm_sap.primitive = EMMREG_ATTACH_INIT;
1054
  emm_sap.u.emm_reg.u.attach.is_emergency = user->emm_data->is_emergency;
gauthier's avatar
 
gauthier committed
1055

1056
  (void) emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

  LOG_FUNC_RETURN(NULL);
}

/****************************************************************************
 **                                                                        **
 ** Name:    _emm_attach_t3402_handler()                               **
 **                                                                        **
 ** Description: T3402 timeout handler                                     **
 **                                                                        **
 **      Upon T3402 timer expiration:                              **
 **              3GPP TS 24.301, section 5.5.1.1                           **
 **      the attach attempt counter shall be reset when the UE is  **
 **      in substate DEREGISTERED.ATTEMPTING-TO-ATTACH;            **
 **              3GPP TS 24.301, section 5.2.2.3.3                         **
 **      the UE shall initiate an attach or combined attach proce- **
 **      dure in substate DEREGISTERED.ATTEMPTING-TO-ATTACH;       **
 **                                                                        **
 ** Inputs:  args:      handler parameters                         **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **                                                                        **
 ***************************************************************************/
static void *_emm_attach_t3402_handler(void *args)
{
  LOG_FUNC_IN;

1086
  nas_user_t *user = args;
1087
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
1088
  emm_attach_data_t *emm_attach_data = user->emm_data->emm_attach_data;
gauthier's avatar
 
gauthier committed
1089 1090 1091 1092 1093
  emm_sap_t emm_sap;

  LOG_TRACE(WARNING, "EMM-PROC  - T3402 timer expired");

  /* Stop T3402 timer */
1094
  emm_timers->T3402.id = nas_timer_stop(emm_timers->T3402.id);
gauthier's avatar
 
gauthier committed
1095
  /* Reset the attach attempt counter */
1096
  emm_attach_data->attempt_count = 0;
gauthier's avatar
 
gauthier committed
1097 1098 1099 1100 1101
  /*
   * Notify EMM that timer T3402 expired and attach procedure has to be
   * restarted
   */
  emm_sap.primitive = EMMREG_ATTACH_INIT;
1102
  emm_sap.u.emm_reg.u.attach.is_emergency = user->emm_data->is_emergency;
gauthier's avatar
 
gauthier committed
1103

1104
  (void) emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134

  LOG_FUNC_RETURN(NULL);
}

/*
 * --------------------------------------------------------------------------
 *              Abnormal cases in the UE
 * --------------------------------------------------------------------------
 */

/****************************************************************************
 **                                                                        **
 ** Name:    _emm_attach_abnormal_cases_bcd()                          **
 **                                                                        **
 ** Description: Performs the abnormal case attach procedure.              **
 **                                                                        **
 **      3GPP TS 24.301, section 5.5.1.2.6, cases b, c and d       **
 **      The Timer T3410 shall be stopped if still running, the    **
 **      attach attempt counter shall be incremented and the UE    **
 **      shall proceed depending on whether the attach attempt     **
 **      counter reached its maximum value or not.                 **
 **                                                                        **
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     emm_sap:   EMM service access point                   **
 **      Return:    None                                       **
 **             T3411                                      **
 **                                                                        **
 ***************************************************************************/
1135
static void _emm_attach_abnormal_cases_bcd(nas_user_t *user, emm_sap_t *emm_sap)
gauthier's avatar
 
gauthier committed
1136 1137
{
  LOG_FUNC_IN;
1138
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
1139
  emm_attach_data_t *emm_attach_data = user->emm_data->emm_attach_data;
gauthier's avatar
 
gauthier committed
1140
  LOG_TRACE(WARNING, "EMM-PROC  - Abnormal case, attach counter = %d",
1141
            emm_attach_data->attempt_count);
gauthier's avatar
 
gauthier committed
1142 1143

  /* Stop timer T3410 */
1144 1145 1146
  if (emm_timers->T3410.id != NAS_TIMER_INACTIVE_ID) {
    LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3410 (%d)", emm_timers->T3410.id);
    emm_timers->T3410.id = nas_timer_stop(emm_timers->T3410.id);
gauthier's avatar
 
gauthier committed
1147 1148
  }

1149
  if (emm_attach_data->attempt_count < EMM_ATTACH_COUNTER_MAX) {
gauthier's avatar
 
gauthier committed
1150
    /* Increment the attach attempt counter */
1151
    emm_attach_data->attempt_count += 1;
gauthier's avatar
 
gauthier committed
1152
    /* Start T3411 timer */
1153
    emm_timers->T3411.id = nas_timer_start(emm_timers->T3411.sec, _emm_attach_t3411_handler, NULL);
gauthier's avatar
 
gauthier committed
1154
    LOG_TRACE(INFO, "EMM-PROC  - Timer T3411 (%d) expires in %ld seconds",
1155
              emm_timers->T3411.id, emm_timers->T3411.sec);
gauthier's avatar
 
gauthier committed
1156 1157 1158 1159 1160 1161 1162 1163
    /*
     * Notify EMM that the attempt to attach for EPS services failed and
     * the attach attempt counter didn't reach its maximum value; network
     * attach procedure shall be restarted when timer T3411 expires.
     */
    emm_sap->primitive = EMMREG_ATTACH_FAILED;
  } else {
    /* Delete the GUTI */
1164
    user->emm_data->guti = NULL;
gauthier's avatar
 
gauthier committed
1165
    /* Delete the TAI list */
1166
    user->emm_data->ltai.n_tais = 0;
gauthier's avatar
 
gauthier committed
1167
    /* Delete the last visited registered TAI */
1168
    user->emm_data->tai = NULL;
gauthier's avatar
 
gauthier committed
1169
    /* Delete the list of equivalent PLMNs */
1170
    user->emm_data->nvdata.eplmn.n_plmns = 0;
gauthier's avatar
 
gauthier committed
1171 1172

    /* Delete the eKSI */
1173 1174
    if (user->emm_data->security) {
      user->emm_data->security->type = EMM_KSI_NOT_AVAILABLE;
gauthier's avatar
 
gauthier committed
1175 1176 1177
    }

    /* Set the EPS update status to EU2 NOT UPDATED */
1178
    user->emm_data->status = EU2_NOT_UPDATED;
gauthier's avatar
 
gauthier committed
1179 1180

    /* Start T3402 timer */
1181
    emm_timers->T3402.id = nas_timer_start(emm_timers->T3402.sec, _emm_attach_t3402_handler, user);
gauthier's avatar
 
gauthier committed
1182
    LOG_TRACE(INFO, "EMM-PROC  - Timer T3402 (%d) expires in %ld seconds",
1183
              emm_timers->T3402.id, emm_timers->T3402.sec);
gauthier's avatar
 
gauthier committed
1184 1185 1186 1187 1188 1189 1190 1191 1192
    /*
     * Notify EMM that the attempt to attach for EPS services failed and
     * the attach attempt counter reached its maximum value.
     */
    emm_sap->primitive = EMMREG_ATTACH_EXCEEDED;
  }

  LOG_FUNC_OUT;
}