Attach.c 105 KB
Newer Older
Lionel Gauthier's avatar
GPLv3  
Lionel Gauthier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*******************************************************************************
    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.

 *******************************************************************************/
Cedric Roux's avatar
 
Cedric Roux committed
29 30
/*****************************************************************************

Cedric Roux's avatar
Cedric Roux committed
31
Source      Attach.c
Cedric Roux's avatar
 
Cedric Roux committed
32

Cedric Roux's avatar
Cedric Roux committed
33
Version     0.1
Cedric Roux's avatar
 
Cedric Roux committed
34

Cedric Roux's avatar
Cedric Roux committed
35
Date        2012/12/04
Cedric Roux's avatar
 
Cedric Roux committed
36

Cedric Roux's avatar
Cedric Roux committed
37
Product     NAS stack
Cedric Roux's avatar
 
Cedric Roux committed
38

Cedric Roux's avatar
Cedric Roux committed
39
Subsystem   EPS Mobility Management
Cedric Roux's avatar
 
Cedric Roux committed
40

Cedric Roux's avatar
Cedric Roux committed
41
Author      Frederic Maurel
Cedric Roux's avatar
 
Cedric Roux committed
42

Cedric Roux's avatar
Cedric Roux committed
43 44
Description Defines the attach related EMM procedure executed by the
        Non-Access Stratum.
Cedric Roux's avatar
 
Cedric Roux committed
45

Cedric Roux's avatar
Cedric Roux committed
46 47 48 49 50
        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.
Cedric Roux's avatar
 
Cedric Roux committed
51

Cedric Roux's avatar
Cedric Roux committed
52 53 54 55 56
        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.
Cedric Roux's avatar
 
Cedric Roux committed
57

Cedric Roux's avatar
Cedric Roux committed
58 59
        The network may also initiate the activation of additional
        dedicated bearers for the support of a specific service.
Cedric Roux's avatar
 
Cedric Roux committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73

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

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

#include "emmData.h"

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

74 75
#include "NasSecurityAlgorithms.h"

Cedric Roux's avatar
 
Cedric Roux committed
76 77
#ifdef NAS_MME
#include "mme_api.h"
78
#include "mme_config.h"
79
# if defined(NAS_BUILT_IN_EPC)
Cedric Roux's avatar
Cedric Roux committed
80 81
#   include "nas_itti_messaging.h"
# endif
Cedric Roux's avatar
 
Cedric Roux committed
82 83
#endif

Cedric Roux's avatar
Cedric Roux committed
84 85
#include <string.h> // memcmp, memcpy
#include <stdlib.h> // malloc, free
Cedric Roux's avatar
 
Cedric Roux committed
86 87 88 89 90 91 92 93 94 95

/****************************************************************************/
/****************  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 */
Cedric Roux's avatar
Cedric Roux committed
96
static const char *_emm_attach_type_str[] = {
97
  "EPS", "IMSI", "EMERGENCY", "RESERVED"
Cedric Roux's avatar
 
Cedric Roux committed
98 99 100 101
};

/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
102
 *      Internal data handled by the attach procedure in the UE
Cedric Roux's avatar
 
Cedric Roux committed
103 104 105 106 107 108
 * --------------------------------------------------------------------------
 */
#ifdef NAS_UE
/*
 * Timer handlers
 */
Cedric Roux's avatar
Cedric Roux committed
109 110 111
void *_emm_attach_t3410_handler(void *);
static void *_emm_attach_t3411_handler(void *);
static void *_emm_attach_t3402_handler(void *);
Cedric Roux's avatar
 
Cedric Roux committed
112 113 114 115

/*
 * Abnormal case attach procedure
 */
Cedric Roux's avatar
Cedric Roux committed
116
static void _emm_attach_abnormal_cases_bcd(emm_sap_t *);
Cedric Roux's avatar
 
Cedric Roux committed
117 118 119 120 121

/*
 * Internal data used for attach procedure
 */
static struct {
Cedric Roux's avatar
Cedric Roux committed
122
#define EMM_ATTACH_COUNTER_MAX  5
123
  unsigned int attempt_count; /* Counter used to limit the number of
Cedric Roux's avatar
Cedric Roux committed
124
                 * subsequently rejected attach attempts */
Cedric Roux's avatar
 
Cedric Roux committed
125 126 127 128 129
} _emm_attach_data = {0};
#endif // NAS_UE

/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
130
 *      Internal data handled by the attach procedure in the MME
Cedric Roux's avatar
 
Cedric Roux committed
131 132 133 134 135 136
 * --------------------------------------------------------------------------
 */
#ifdef NAS_MME
/*
 * Timer handlers
 */
Cedric Roux's avatar
Cedric Roux committed
137
static void *_emm_attach_t3450_handler(void *);
Cedric Roux's avatar
 
Cedric Roux committed
138 139 140 141

/*
 * Functions that may initiate EMM common procedures
 */
Cedric Roux's avatar
Cedric Roux committed
142 143 144
static int _emm_attach_identify(void *);
static int _emm_attach_security(void *);
static int _emm_attach(void *);
Cedric Roux's avatar
 
Cedric Roux committed
145 146 147 148

/*
 * Abnormal case attach procedures
 */
Cedric Roux's avatar
Cedric Roux committed
149 150 151 152 153 154 155
static int _emm_attach_release(void *);
static int _emm_attach_reject(void *);
static int _emm_attach_abort(void *);

static int _emm_attach_have_changed(const emm_data_context_t *ctx,
                                    emm_proc_attach_type_t type, int ksi,
                                    GUTI_t *guti, imsi_t *imsi, imei_t *imei,
156 157
                                    int eea, int eia, int ucs2, int uea, int uia, int gea,
                                    int umts_present, int gprs_present);
Cedric Roux's avatar
Cedric Roux committed
158 159 160
static int _emm_attach_update(emm_data_context_t *ctx, unsigned int ueid,
                              emm_proc_attach_type_t type, int ksi,
                              GUTI_t *guti, imsi_t *imsi, imei_t *imei,
161 162
                              int eea, int eia, int ucs2, int uea, int uia, int gea,
                              int umts_present, int gprs_present,
163
                              const OctetString *esm_msg_pP);
Cedric Roux's avatar
 
Cedric Roux committed
164 165 166 167

/*
 * Internal data used for attach procedure
 */
168
typedef struct attach_data_s {
169
  unsigned int ueid;          /* UE identifier        */
Cedric Roux's avatar
Cedric Roux committed
170
#define ATTACH_COUNTER_MAX  5
171 172
  unsigned int retransmission_count;  /* Retransmission counter   */
  OctetString esm_msg;        /* ESM message to be sent within
Cedric Roux's avatar
Cedric Roux committed
173
                     * the Attach Accept message    */
Cedric Roux's avatar
 
Cedric Roux committed
174 175
} attach_data_t;

Cedric Roux's avatar
Cedric Roux committed
176
static int _emm_attach_accept(emm_data_context_t *emm_ctx, attach_data_t *data);
Cedric Roux's avatar
 
Cedric Roux committed
177 178 179 180 181 182 183 184 185

#endif // NAS_MME

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

/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
186
 *          Attach procedure executed by the UE
Cedric Roux's avatar
 
Cedric Roux committed
187 188 189 190 191
 * --------------------------------------------------------------------------
 */
#ifdef NAS_UE
/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
192
 ** Name:    emm_proc_attach()                                         **
Cedric Roux's avatar
 
Cedric Roux committed
193 194
 **                                                                        **
 ** Description: Initiate EPS attach procedure to register a UE in PS mode **
Cedric Roux's avatar
Cedric Roux committed
195 196
 **      of operation for EPS services only, or register a UE for  **
 **      emergency bearer services.                                **
Cedric Roux's avatar
 
Cedric Roux committed
197 198
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.2                         **
Cedric Roux's avatar
Cedric Roux committed
199 200 201 202
 **      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.                                                **
Cedric Roux's avatar
 
Cedric Roux committed
203
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
204 205
 ** Inputs:  type:      Type of the requested attach               **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
206
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
207 208 209
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3402, T3410, T3411                        **
Cedric Roux's avatar
 
Cedric Roux committed
210 211 212 213
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach(emm_proc_attach_type_t type)
{
214
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
215

216 217 218 219
  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;
Cedric Roux's avatar
 
Cedric Roux committed
220

221 222
  LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach type = %s (%d)",
            _emm_attach_type_str[type], type);
Cedric Roux's avatar
 
Cedric Roux committed
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
  /* Update the emergency bearer service indicator */
  if (type == EMM_ATTACH_TYPE_EMERGENCY) {
    _emm_data.is_emergency = TRUE;
  }

  /* 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 */
  if (_emm_data.is_emergency) {
    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 */
  emm_as->plmnID = &_emm_data.splmn;
  /*
   * 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" */
  if (_emm_data.AttachWithImsi) {
    /* Check whether the selected PLMN is neither the registered PLMN
     * nor in the list of equivalent PLMNs */
    if ( (!_emm_data.is_rplmn) && (!_emm_data.is_eplmn) ) {
      LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach with IMSI");
      /* Include the IMSI */
      emm_as->UEid.imsi = _emm_data.imsi;
Cedric Roux's avatar
 
Cedric Roux committed
261
    } else {
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
      LOG_TRACE(INFO,
                "EMM-PROC  - Initiate EPS attach with NO IMSI, is registered PLMN %d, is equivalent PLMN %d",
                _emm_data.is_rplmn,
                _emm_data.is_eplmn);
    }
  } else if (_emm_data.guti) {
    LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach with GUTI");
    /* Include a valid GUTI and the last visited registered TAI */
    emm_as->UEid.guti = _emm_data.guti;
    emm_as->UEid.tai = _emm_data.tai;
  } else if (!_emm_data.is_emergency) {
    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 */
    emm_as->UEid.imsi = _emm_data.imsi;
  } else {
    /* The UE is attaching for emergency bearer services and
     * does not hold a valid GUTI */
    if (_emm_data.imsi) {
      /* Include the IMSI if valid (USIM is present) */
      LOG_TRACE(INFO, "EMM-PROC  - Initiate EPS attach with IMSI cause is emergency and no GUTI");
      emm_as->UEid.imsi = _emm_data.imsi;
Cedric Roux's avatar
Cedric Roux committed
283
    } else {
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
      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 */
      emm_as->UEid.imei = _emm_data.imei;
    }
  }

  /* Setup EPS NAS security data */
  emm_as_set_security_data(&emm_as->sctx, _emm_data.security, FALSE, FALSE);
  emm_as->ksi = EMM_AS_NO_KEY_AVAILABLE;

  if (_emm_data.security) {
    if (_emm_data.security->type != EMM_KSI_NOT_AVAILABLE) {
      emm_as->ksi = _emm_data.security->eksi;
    }

    LOG_TRACE(INFO, "EMM-PROC  - eps_encryption 0x%X", _emm_data.security->capability.eps_encryption);
    LOG_TRACE(INFO, "EMM-PROC  - eps_integrity  0x%X", _emm_data.security->capability.eps_integrity);
    emm_as->encryption = _emm_data.security->capability.eps_encryption;
    emm_as->integrity = _emm_data.security->capability.eps_integrity;
  }

  /*
   * 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 */
  esm_sap.data.pdn_connect.pdn_type = NET_PDN_TYPE_IPV4V6;
  esm_sap.data.pdn_connect.apn = NULL;
  esm_sap.data.pdn_connect.is_emergency = _emm_data.is_emergency;
  rc = esm_sap_send(&esm_sap);

  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,
                                        emm_proc_attach_release, NULL);

    if (rc != RETURNok) {
      LOG_TRACE(WARNING, "Failed to initialize EMM procedure handler");
      LOG_FUNC_RETURN (RETURNerror);
Cedric Roux's avatar
 
Cedric Roux committed
329 330
    }

331 332 333 334 335 336 337
    /* Start T3410 timer */
    T3410.id = nas_timer_start(T3410.sec, _emm_attach_t3410_handler, NULL);
    LOG_TRACE(INFO,"EMM-PROC  - Timer T3410 (%d) expires in %ld seconds",
              T3410.id, T3410.sec);
    /* Stop T3402 and T3411 timers if running */
    T3402.id = nas_timer_stop(T3402.id);
    T3411.id = nas_timer_stop(T3411.id);
Cedric Roux's avatar
Cedric Roux committed
338

339 340 341 342 343 344 345 346 347 348 349
    /*
     * 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;
    rc = emm_sap_send(&emm_sap);
  }
Cedric Roux's avatar
 
Cedric Roux committed
350

351
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
352 353 354 355
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
356
 ** Name:    emm_proc_attach_request()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
357 358
 **                                                                        **
 ** Description: Performs the attach procedure upon receipt of indication  **
Cedric Roux's avatar
Cedric Roux committed
359 360
 **      from lower layers that Attach Request message has been    **
 **      successfully delivered to the network.                    **
Cedric Roux's avatar
 
Cedric Roux committed
361
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
362 363
 ** Inputs:  args:      Not used                                   **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
364
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
365 366 367
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
368 369
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
370
int emm_proc_attach_request(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
371
{
372
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
373

374 375
  emm_sap_t emm_sap;
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
376

377 378 379 380 381
  /*
   * Notify EMM that Attach Request has been sent to the network
   */
  emm_sap.primitive = EMMREG_ATTACH_REQ;
  rc = emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
382

383
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
384 385 386 387
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
388
 ** Name:    emm_proc_attach_accept()                                  **
Cedric Roux's avatar
 
Cedric Roux committed
389 390 391 392
 **                                                                        **
 ** Description: Performs the attach procedure accepted by the network.    **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.4                         **
Cedric Roux's avatar
Cedric Roux committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
 **      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                   **
408
 **      esm_msg_pP:   Activate default EPS bearer context re-    **
Cedric Roux's avatar
Cedric Roux committed
409 410 411 412 413 414
 **             quest ESM message                          **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data, T3412, T3402, T3423             **
Cedric Roux's avatar
 
Cedric Roux committed
415 416 417
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_accept(long t3412, long t3402, long t3423,
Cedric Roux's avatar
Cedric Roux committed
418 419
                           int n_tais, tai_t *tai, GUTI_t *guti,
                           int n_eplmns, plmn_t *eplmn,
420
                           const OctetString *esm_msg_pP)
Cedric Roux's avatar
 
Cedric Roux committed
421
{
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
  LOG_FUNC_IN;

  emm_sap_t emm_sap;
  esm_sap_t esm_sap;
  int rc;
  int i;
  int j;

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

  /* Stop timer T3410 */
  LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3410 (%d)", T3410.id);
  T3410.id = nas_timer_stop(T3410.id);

  /* Delete old TAI list and store the received TAI list */
  _emm_data.ltai.n_tais = n_tais;

  for (i = 0; (i < n_tais) && (i < EMM_DATA_TAI_MAX); i++) {
    _emm_data.ltai.tai[i] = tai[i];
  }

  /* Update periodic tracking area update timer value */
  T3412.sec = t3412;

  /* Update attach failure timer value */
  if ( !(t3402 < 0) ) {
    T3402.sec = t3402;
  }

  /* Update E-UTRAN deactivate ISR timer value */
  if ( !(t3423 < 0) ) {
    T3423.sec = t3423;
  }

  /* Delete old GUTI and store the new assigned GUTI if provided */
  if (guti) {
    *_emm_data.guti = *guti;
  }

  /* Update the stored list of equivalent PLMNs */
  _emm_data.nvdata.eplmn.n_plmns = 0;

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

      if (!_emm_data.is_emergency) {
        /* 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 */
        for (j = 0; j < _emm_data.fplmn.n_plmns; j++) {
          if (PLMNS_ARE_EQUAL(eplmn[i], _emm_data.fplmn.plmn[j])) {
            is_forbidden = TRUE;
            break;
          }
        }
      }
Cedric Roux's avatar
 
Cedric Roux committed
479

480 481 482 483
      if ( !is_forbidden ) {
        _emm_data.nvdata.eplmn.plmn[_emm_data.nvdata.eplmn.n_plmns++] =
          eplmn[i];
      }
Cedric Roux's avatar
 
Cedric Roux committed
484 485
    }

486 487 488 489
    /* Add the PLMN code of the registered PLMN that sent the list */
    if (_emm_data.nvdata.eplmn.n_plmns < EMM_DATA_EPLMN_MAX) {
      _emm_data.nvdata.eplmn.plmn[_emm_data.nvdata.eplmn.n_plmns++] =
        _emm_data.splmn;
Cedric Roux's avatar
 
Cedric Roux committed
490
    }
491
  }
Cedric Roux's avatar
 
Cedric Roux committed
492

493 494 495 496 497
  /*
   * 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;
498
  esm_sap.recv = esm_msg_pP;
499
  rc = esm_sap_send(&esm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
500

501 502 503 504 505 506 507 508 509 510 511
  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,
                                        NULL, NULL);

    if (rc != RETURNok) {
      LOG_TRACE(WARNING,
                "EMM-PROC  - Failed to initialize EMM procedure handler");
      LOG_FUNC_RETURN (RETURNerror);
Cedric Roux's avatar
 
Cedric Roux committed
512 513 514
    }

    /*
515 516 517
     * 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
Cedric Roux's avatar
 
Cedric Roux committed
518
     */
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
    emm_sap.primitive = EMMAS_DATA_REQ;
    emm_sap.u.emm_as.u.data.guti = _emm_data.guti;
    emm_sap.u.emm_as.u.data.ueid = 0;
    /* Setup EPS NAS security data */
    emm_as_set_security_data(&emm_sap.u.emm_as.u.data.sctx,
                             _emm_data.security, FALSE, TRUE);
    /* 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;
    rc = emm_sap_send(&emm_sap);
  } 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;
    rc = emm_sap_send(&emm_sap);
  } else {
    /*
     * ESM procedure failed and, received message has been discarded or
     * Status message has been returned; ignore ESM procedure failure
     */
    rc = RETURNok;
  }
Cedric Roux's avatar
 
Cedric Roux committed
547

548
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
549 550 551 552
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
553
 ** Name:    emm_proc_attach_reject()                                  **
Cedric Roux's avatar
 
Cedric Roux committed
554 555 556 557
 **                                                                        **
 ** Description: Performs the attach procedure rejected by the network.    **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.5                         **
Cedric Roux's avatar
Cedric Roux committed
558 559 560
 **      Upon receiving the ATTACH REJECT message, the UE shall    **
 **      stop timer T3410 and take actions depending on the EMM    **
 **      cause value received.                                     **
Cedric Roux's avatar
 
Cedric Roux committed
561
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
562 563
 ** Inputs:  emm_cause: EMM cause indicating why the network re-   **
 **             jected the attach request                  **
564
 **      esm_msg_pP:   PDN connectivity reject ESM message        **
Cedric Roux's avatar
Cedric Roux committed
565
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
566
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
567 568 569
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data, _emm_attach_data, T3410         **
Cedric Roux's avatar
 
Cedric Roux committed
570 571
 **                                                                        **
 ***************************************************************************/
572
int emm_proc_attach_reject(int emm_cause, const OctetString *esm_msg_pP)
Cedric Roux's avatar
 
Cedric Roux committed
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 607 608
  LOG_FUNC_IN;

  emm_sap_t emm_sap;
  int rc;

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

  /* Stop timer T3410 */
  LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3410 (%d)", T3410.id);
  T3410.id = nas_timer_stop(T3410.id);

  /* 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 */
    _emm_data.status = EU3_ROAMING_NOT_ALLOWED;
    /* Delete the GUTI */
    _emm_data.guti = NULL;
    /* Delete the last visited registered TAI */
    _emm_data.tai = NULL;

    /* Delete the eKSI */
    if (_emm_data.security) {
      _emm_data.security->type = EMM_KSI_NOT_AVAILABLE;
Cedric Roux's avatar
 
Cedric Roux committed
609 610
    }

611
    break;
Cedric Roux's avatar
Cedric Roux committed
612

613 614 615
  default :
    break;
  }
Cedric Roux's avatar
Cedric Roux committed
616

617 618 619 620 621 622 623 624 625 626 627
  /* 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 */
    _emm_data.usim_is_valid = FALSE;
    /* Delete the list of equivalent PLMNs */
    _emm_data.nvdata.eplmn.n_plmns = 0;
    break;
Cedric Roux's avatar
 
Cedric Roux committed
628

629 630 631 632 633 634 635 636
  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 */
    _emm_data.nvdata.eplmn.n_plmns = 0;
    /* Reset the attach attempt counter */
    _emm_attach_data.attempt_count = 0;
    break;
Cedric Roux's avatar
Cedric Roux committed
637

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
  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 */
    _emm_attach_data.attempt_count = 0;
    break;

  case EMM_CAUSE_ESM_FAILURE:

    /* 3GPP TS 24.301, section 5.5.1.2.6, case d */
    if (_emm_data.NAS_SignallingPriority != 1) {
      /* The UE is not configured for NAS signalling low priority;
       * set the attach attempt counter to 5 */
      _emm_attach_data.attempt_count = EMM_ATTACH_COUNTER_MAX;
    }

    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 */
    _emm_attach_data.attempt_count = EMM_ATTACH_COUNTER_MAX;
    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" */
    _emm_data.fplmn.plmn[_emm_data.fplmn.n_plmns++] = _emm_data.splmn;
    break;

  case EMM_CAUSE_TA_NOT_ALLOWED:
    /* Store the current TAI in the list of "forbidden tracking
     * areas for regional provision of service" */
    _emm_data.ftai.tai[_emm_data.ftai.n_tais++] = *_emm_data.tai;
    break;

  case EMM_CAUSE_ROAMING_NOT_ALLOWED:
    /* Store the current TAI in the list of "forbidden tracking
     * areas for roaming" */
    _emm_data.ftai_roaming.tai[_emm_data.ftai_roaming.n_tais++] = *_emm_data.tai;
    break;

  case EMM_CAUSE_EPS_NOT_ALLOWED_IN_PLMN:
    /* Store the PLMN identity in the "forbidden PLMNs for GPRS
     * service" list */
    _emm_data.fplmn_gprs.plmn[_emm_data.fplmn_gprs.n_plmns++] = _emm_data.splmn;
    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;
Cedric Roux's avatar
Cedric Roux committed
711

712 713 714 715 716 717 718 719 720
  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;
Cedric Roux's avatar
Cedric Roux committed
721

722 723 724 725 726 727 728 729 730 731
  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;
Cedric Roux's avatar
Cedric Roux committed
732

733 734 735 736 737 738 739 740
  case EMM_CAUSE_IMEI_NOT_ACCEPTED:
    if (_emm_data.is_emergency) {
      /*
       * 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;
Cedric Roux's avatar
 
Cedric Roux committed
741 742
    }

743
    /* break is volontary missing */
Cedric Roux's avatar
Cedric Roux committed
744

745 746 747 748 749 750
  default :
    /* Other values are considered as abnormal cases
     * 3GPP TS 24.301, section 5.5.1.2.6, case d */
    _emm_attach_abnormal_cases_bcd(&emm_sap);
    break;
  }
Cedric Roux's avatar
 
Cedric Roux committed
751

752
  rc = emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
753

754 755 756
  /*
   * Notify ESM that the network rejected connectivity to the PDN
   */
757
  if (esm_msg_pP != NULL) {
758 759 760
    esm_sap_t esm_sap;
    esm_sap.primitive = ESM_PDN_CONNECTIVITY_REJ;
    esm_sap.is_standalone = FALSE;
761
    esm_sap.recv = esm_msg_pP;
762 763
    rc = esm_sap_send(&esm_sap);
  }
Cedric Roux's avatar
 
Cedric Roux committed
764

765
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
766 767 768 769
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
770
 ** Name:    emm_proc_attach_complete()                                **
Cedric Roux's avatar
 
Cedric Roux committed
771 772
 **                                                                        **
 ** Description: Terminates the attach procedure when Attach Complete mes- **
Cedric Roux's avatar
Cedric Roux committed
773
 **      sage has been successfully delivered to the MME.          **
Cedric Roux's avatar
 
Cedric Roux committed
774 775
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.4                         **
Cedric Roux's avatar
Cedric Roux committed
776 777 778 779
 **      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.  **
Cedric Roux's avatar
 
Cedric Roux committed
780
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
781 782
 ** Inputs:  args:      Not used                                   **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
783
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
784 785 786
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data, _emm_attach_data                **
Cedric Roux's avatar
 
Cedric Roux committed
787 788
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
789
int emm_proc_attach_complete(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
790
{
791
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
792

793 794 795
  emm_sap_t emm_sap;
  esm_sap_t esm_sap;
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
796

797
  LOG_TRACE(INFO, "EMM-PROC  - EPS attach complete");
Cedric Roux's avatar
 
Cedric Roux committed
798

799 800
  /* Reset EMM procedure handler */
  (void) emm_proc_lowerlayer_initialize(NULL, NULL, NULL, NULL);
Cedric Roux's avatar
 
Cedric Roux committed
801

802 803 804 805 806 807 808
  /* Reset the attach attempt counter */
  _emm_attach_data.attempt_count = 0;
  /* TODO: Reset the tracking area updating attempt counter */

  /* Set the EPS update status to EU1 UPDATED */
  _emm_data.status = EU1_UPDATED;
  _emm_data.is_attached = TRUE;
Cedric Roux's avatar
 
Cedric Roux committed
809

810 811 812 813 814 815
  /*
   * Notify EMM that network attach complete message has been delivered
   * to the network
   */
  emm_sap.primitive = EMMREG_ATTACH_CNF;
  rc = emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
816

817
  if (rc != RETURNerror) {
Cedric Roux's avatar
 
Cedric Roux committed
818
    /*
819 820 821
     * Notify ESM that the Activate Default EPS Bearer Context Accept
     * message has been delivered to the network within the Attach
     * Complete message
Cedric Roux's avatar
 
Cedric Roux committed
822
     */
823 824 825 826
    esm_sap.primitive = ESM_DEFAULT_EPS_BEARER_CONTEXT_ACTIVATE_CNF;
    esm_sap.is_standalone = FALSE;
    rc = esm_sap_send(&esm_sap);
  }
Cedric Roux's avatar
 
Cedric Roux committed
827

828
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
829 830 831 832
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
833
 ** Name:    emm_proc_attach_failure()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
834 835
 **                                                                        **
 ** Description: Performs the attach procedure abnormal case upon receipt  **
Cedric Roux's avatar
Cedric Roux committed
836 837
 **      of transmission failure of Attach Request message or At-  **
 **      tach Complete message.                                    **
Cedric Roux's avatar
 
Cedric Roux committed
838 839
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.6, cases h and i          **
Cedric Roux's avatar
Cedric Roux committed
840 841
 **      The  UE  shall restart the attach  procedure when timer   **
 **      T3411 expires.                                            **
Cedric Roux's avatar
 
Cedric Roux committed
842
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
843 844 845 846 847 848
 ** 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                                       **
Cedric Roux's avatar
 
Cedric Roux committed
849
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
850 851 852
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3410, T3411                               **
Cedric Roux's avatar
 
Cedric Roux committed
853 854
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
855
int emm_proc_attach_failure(int is_initial, void *args)
Cedric Roux's avatar
 
Cedric Roux committed
856
{
857
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
858

859 860
  int rc = RETURNok;
  esm_sap_t esm_sap;
Cedric Roux's avatar
 
Cedric Roux committed
861

862
  LOG_TRACE(WARNING, "EMM-PROC  - EPS attach failure");
Cedric Roux's avatar
 
Cedric Roux committed
863

864 865
  /* Reset EMM procedure handler */
  (void) emm_proc_lowerlayer_initialize(NULL, NULL, NULL, NULL);
Cedric Roux's avatar
 
Cedric Roux committed
866

867 868 869 870 871
  /* Stop timer T3410 if still running */
  if (T3410.id != NAS_TIMER_INACTIVE_ID) {
    LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3410 (%d)", T3410.id);
    T3410.id = nas_timer_stop(T3410.id);
  }
Cedric Roux's avatar
 
Cedric Roux committed
872

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
  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;
  }
Cedric Roux's avatar
 
Cedric Roux committed
892

893
  rc = esm_sap_send(&esm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
894

895 896 897 898 899 900 901 902
  if (rc != RETURNerror) {
    /* Start T3411 timer */
    T3411.id = nas_timer_start(T3411.sec, _emm_attach_t3411_handler, NULL);
    LOG_TRACE(INFO, "EMM-PROC  - Timer T3411 (%d) expires in %ld seconds",
              T3411.id, T3411.sec);
  }

  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
903 904 905 906
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
907
 ** Name:    emm_proc_attach_release()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
908 909
 **                                                                        **
 ** Description: Performs the attach procedure abnormal case upon receipt  **
Cedric Roux's avatar
Cedric Roux committed
910
 **      of NAS signalling connection release indication.          **
Cedric Roux's avatar
 
Cedric Roux committed
911 912
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.6, case b                 **
Cedric Roux's avatar
Cedric Roux committed
913 914
 **      The attach procedure shall be aborted and the UE shall    **
 **      execute abnormal case attach procedure.                   **
Cedric Roux's avatar
 
Cedric Roux committed
915
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
916 917
 ** Inputs:  args:      Not used                                   **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
918
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
919 920 921
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
922 923
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
924
int emm_proc_attach_release(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
925
{
926
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
927

928 929
  emm_sap_t emm_sap;
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
930

931
  LOG_TRACE(WARNING, "EMM-PROC  - NAS signalling connection released");
Cedric Roux's avatar
 
Cedric Roux committed
932

933 934
  /* Execute abnormal case attach procedure */
  _emm_attach_abnormal_cases_bcd(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
935

936
  rc = emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
937

938
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
939 940 941 942
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
943
 ** Name:    emm_proc_attach_restart()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
944 945 946
 **                                                                        **
 ** Description: Restarts the attach procedure                             **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
947 948
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
949
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
950 951 952
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
953 954 955 956
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_restart(void)
{
957
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
958

959 960
  emm_sap_t emm_sap;
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
961

962
  LOG_TRACE(INFO, "EMM-PROC  - Restart EPS attach procedure");
Cedric Roux's avatar
 
Cedric Roux committed
963

964 965 966 967 968 969
  /*
   * Notify EMM that the attach procedure has to be restarted
   */
  emm_sap.primitive = EMMREG_ATTACH_INIT;
  emm_sap.u.emm_reg.u.attach.is_emergency = _emm_data.is_emergency;
  rc = emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
970

971
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
972 973 974 975
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
976
 ** Name:    emm_proc_attach_set_emergency()                           **
Cedric Roux's avatar
 
Cedric Roux committed
977 978 979
 **                                                                        **
 ** Description: Set the emergency bearer services indicator               **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
980 981
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
982
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
983 984 985
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
986 987 988 989
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_set_emergency(void)
{
990
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
991

992 993
  LOG_TRACE(WARNING, "EMM-PROC  - UE is now attached to the network for "
            "emergency bearer services only");
Cedric Roux's avatar
 
Cedric Roux committed
994

995
  _emm_data.is_emergency = TRUE;
Cedric Roux's avatar
 
Cedric Roux committed
996

997
  LOG_FUNC_RETURN(RETURNok);
Cedric Roux's avatar
 
Cedric Roux committed
998 999 1000 1001
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1002
 ** Name:    emm_proc_attach_set_detach()                              **
Cedric Roux's avatar
 
Cedric Roux committed
1003 1004
 **                                                                        **
 ** Description: Reset the network attachment indicator and enter state    **
Cedric Roux's avatar
Cedric Roux committed
1005
 **      EMM-DEREGISTERED
Cedric Roux's avatar
 
Cedric Roux committed
1006
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1007 1008
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1009
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1010 1011 1012
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
1013 1014 1015 1016
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_set_detach(void)
{
1017
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
1018

1019
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
1020

1021 1022
  LOG_TRACE(WARNING,
            "EMM-PROC  - UE is now locally detached from the network");
Cedric Roux's avatar
 
Cedric Roux committed
1023

1024 1025 1026 1027 1028 1029 1030 1031
  /* Reset the network attachment indicator */
  _emm_data.is_attached = FALSE;
  /*
   * Notify that the UE is locally detached from the network
   */
  emm_sap_t emm_sap;
  emm_sap.primitive = EMMREG_DETACH_CNF;
  rc = emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
1032

1033
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
1034 1035 1036 1037 1038 1039
}

#endif

/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1040
 *          Attach procedure executed by the MME
Cedric Roux's avatar
 
Cedric Roux committed
1041 1042 1043 1044 1045
 * --------------------------------------------------------------------------
 */
#ifdef NAS_MME
/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1046
 ** Name:    emm_proc_attach_request()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
1047 1048 1049 1050
 **                                                                        **
 ** Description: Performs the UE requested attach procedure                **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.3                         **
Cedric Roux's avatar
Cedric Roux committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
 **      The network may initiate EMM common procedures, e.g. the  **
 **      identification, authentication and security mode control  **
 **      procedures during the attach procedure, depending on the  **
 **      information received in the ATTACH REQUEST message (e.g.  **
 **      IMSI, GUTI and KSI).                                      **
 **                                                                        **
 ** Inputs:  ueid:      UE lower layer identifier                  **
 **      type:      Type of the requested attach               **
 **      native_ksi:    TRUE if the security context is of type    **
 **             native (for KSIASME)                       **
 **      ksi:       The NAS ket sey identifier                 **
 **      native_guti:   TRUE if the provided GUTI is native GUTI   **
 **      guti:      The GUTI if provided by the UE             **
 **      imsi:      The IMSI if provided by the UE             **
 **      imei:      The IMEI if provided by the UE             **
 **      tai:       Identifies the last visited tracking area  **
 **             the UE is registered to                    **
 **      eea:       Supported EPS encryption algorithms        **
 **      eia:       Supported EPS integrity algorithms         **
1070
 **      esm_msg_pP:   PDN connectivity request ESM message       **
Cedric Roux's avatar
Cedric Roux committed
1071 1072 1073 1074 1075
 **      Others:    _emm_data                                  **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
1076 1077
 **                                                                        **
 ***************************************************************************/
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
int emm_proc_attach_request(
  unsigned int ueid,
  emm_proc_attach_type_t type,
  int native_ksi, int ksi,
  int native_guti,
  GUTI_t *guti,
  imsi_t *imsi,
  imei_t *imei,
  tai_t    *tai,
  int eea, int eia, int ucs2, int uea, int uia, int gea,
  int umts_present, int gprs_present,
1089
  const OctetString *esm_msg_pP)
Cedric Roux's avatar
 
Cedric Roux committed
1090
{
1091
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
1092

1093 1094
  int rc;
  emm_data_context_t ue_ctx;
Cedric Roux's avatar
 
Cedric Roux committed
1095

1096 1097 1098
  LOG_TRACE(INFO, "EMM-PROC  - EPS attach type = %s (%d) requested (ueid=0x%08x)",
            _emm_attach_type_str[type], type, ueid);
  LOG_TRACE(INFO, "EMM-PROC  - umts_present = %u umts_present = %u",
1099
            umts_present, gprs_present);
Cedric Roux's avatar
 
Cedric Roux committed
1100

1101 1102 1103 1104
  /* Initialize the temporary UE context */
  memset(&ue_ctx, 0 , sizeof(emm_data_context_t));
  ue_ctx.is_dynamic = FALSE;
  ue_ctx.ueid = ueid;
Cedric Roux's avatar
 
Cedric Roux committed
1105

1106
#if !defined(NAS_BUILT_IN_EPC)
1107 1108 1109 1110 1111 1112 1113 1114 1115

  /* UE identifier sanity check */
  if (ueid >= EMM_DATA_NB_UE_MAX) {
    ue_ctx.emm_cause = EMM_CAUSE_ILLEGAL_UE;
    /* Do not accept UE with invalid identifier */
    rc = _emm_attach_reject(&ue_ctx);
    LOG_FUNC_RETURN(rc);
  }

Cedric Roux's avatar
 
Cedric Roux committed
1116 1117
#endif

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  /* 3GPP TS 24.301, section 5.5.1.1
   * MME not configured to support attach for emergency bearer services
   * shall reject any request to attach with an attach type set to "EPS
   * emergency attach".
   */
  if ( !(_emm_data.conf.features & MME_API_EMERGENCY_ATTACH) &&
       (type == EMM_ATTACH_TYPE_EMERGENCY) ) {
    ue_ctx.emm_cause = EMM_CAUSE_IMEI_NOT_ACCEPTED;
    /* Do not accept the UE to attach for emergency services */
    rc = _emm_attach_reject(&ue_ctx);
    LOG_FUNC_RETURN(rc);
  }
Cedric Roux's avatar
 
Cedric Roux committed
1130

1131 1132
  /* Get the UE's EMM context if it exists */
  emm_data_context_t **emm_ctx = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
1133

1134
#if defined(NAS_BUILT_IN_EPC)
1135
  emm_data_context_t *temp = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
1136

1137 1138
  temp    = emm_data_context_get(&_emm_data, ueid);
  emm_ctx = &temp;
Cedric Roux's avatar
 
Cedric Roux committed
1139
#else
1140
  emm_ctx = &_emm_data.ctx[ueid];
Cedric Roux's avatar
 
Cedric Roux committed
1141 1142
#endif

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
  if (*emm_ctx != NULL) {
    /* An EMM context already exists for the UE in the network */
    if (_emm_attach_have_changed(*emm_ctx, type, ksi, guti, imsi, imei,
                                 eea, eia, ucs2, uea, uia, gea, umts_present, gprs_present)) {
      /*
       * 3GPP TS 24.301, section 5.5.1.2.7, abnormal case e
       * The attach parameters have changed from the one received within
       * the previous Attach Request message;
       * the previously initiated attach procedure shall be aborted and
       * the new attach procedure shall be executed;
       */
      LOG_TRACE(WARNING, "EMM-PROC  - Attach parameters have changed");
      /*
       * Notify EMM that the attach procedure is aborted
       */
      emm_sap_t emm_sap;
      emm_sap.primitive = EMMREG_PROC_ABORT;
      emm_sap.u.emm_reg.ueid = ueid;
      emm_sap.u.emm_reg.ctx  = *emm_ctx;

      rc = emm_sap_send(&emm_sap);

      if (rc != RETURNerror) {
        /* Process new attach procedure */
        LOG_TRACE(WARNING, "EMM-PROC  - Initiate new attach procedure");
        rc = emm_proc_attach_request(ueid, type, native_ksi, ksi,
                                     native_guti, guti, imsi, imei,
                                     tai, eea, eia, ucs2, uea, uia, gea,
1171
                                     umts_present, gprs_present, esm_msg_pP);
1172 1173 1174
      }

      LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
Cedric Roux committed
1175
    } else {
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
      /* Continue with the previous attach procedure */
      LOG_TRACE(WARNING, "EMM-PROC  - Received duplicated Attach Request");
      LOG_FUNC_RETURN(RETURNok);
    }
  } else {
    /* Create UE's EMM context */
    *emm_ctx = (emm_data_context_t *)calloc(1, sizeof(emm_data_context_t));

    if (emm_ctx == NULL) {
      LOG_TRACE(WARNING, "EMM-PROC  - Failed to create EMM context");
      ue_ctx.emm_cause = EMM_CAUSE_ILLEGAL_UE;
      /* Do not accept the UE to attach to the network */
      rc = _emm_attach_reject(&ue_ctx);
      LOG_FUNC_RETURN(rc);
    }

    (*emm_ctx)->is_dynamic = TRUE;
    (*emm_ctx)->guti = NULL;
    (*emm_ctx)->old_guti = NULL;
    (*emm_ctx)->imsi = NULL;
    (*emm_ctx)->imei = NULL;
    (*emm_ctx)->security = NULL;
    (*emm_ctx)->esm_msg.length = 0;
    (*emm_ctx)->esm_msg.value = NULL;
    (*emm_ctx)->emm_cause = EMM_CAUSE_SUCCESS;
    (*emm_ctx)->_emm_fsm_status = EMM_INVALID;
    (*emm_ctx)->ueid = ueid;

    emm_fsm_set_status(ueid, *emm_ctx, EMM_DEREGISTERED);
1205
#if defined(NAS_BUILT_IN_EPC)
1206
    emm_data_context_add(&_emm_data, *(emm_ctx));
Cedric Roux's avatar
 
Cedric Roux committed
1207
#endif
1208 1209

#warning "TRICK TO SET TAC, BUT LOOK AT SPEC"
1210 1211 1212

    if (tai) {
      LOG_TRACE(WARNING,
1213 1214
                "EMM-PROC  - Set tac %u in context %u ",
                tai->tac);
1215 1216 1217
      (*emm_ctx)->tac = tai->tac;
    } else {
      LOG_TRACE(WARNING,
1218
                "EMM-PROC  - Could not set tac in context, cause tai is NULL ");
Cedric Roux's avatar
 
Cedric Roux committed
1219
    }
1220
  }
Cedric Roux's avatar
 
Cedric Roux committed
1221

1222 1223
  /* Update the EMM context with the current attach procedure parameters */
  rc = _emm_attach_update(*emm_ctx, ueid, type, ksi, guti, imsi, imei,
1224
                          eea, eia, ucs2, uea, uia, gea, umts_present, gprs_present, esm_msg_pP);
Cedric Roux's avatar
 
Cedric Roux committed
1225

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
  if (rc != RETURNok) {
    LOG_TRACE(WARNING, "EMM-PROC  - Failed to update EMM context");
    /* Do not accept the UE to attach to the network */
    (*emm_ctx)->emm_cause = EMM_CAUSE_ILLEGAL_UE;
    rc = _emm_attach_reject(*emm_ctx);
  } else {
    /*
     * Performs UE identification
     */
    rc = _emm_attach_identify(*emm_ctx);
  }

  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
1239 1240 1241 1242
}

/****************************************************************************
 **                                                                        **
1243
 ** Name:        emm_proc_attach_reject()                                  **
Cedric Roux's avatar
 
Cedric Roux committed
1244 1245 1246 1247
 **                                                                        **
 ** Description: Performs the protocol error abnormal case                 **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.7, case b                 **
1248 1249
 **              If the ATTACH REQUEST message is received with a protocol **
 **              error, the network shall return an ATTACH REJECT message. **
Cedric Roux's avatar
 
Cedric Roux committed
1250
 **                                                                        **
1251 1252 1253
 ** Inputs:  ueid:              UE lower layer identifier                  **
 **                  emm_cause: EMM cause code to be reported              **
 **                  Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1254
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1255
 ** Outputs:     None                                                      **
1256 1257
 **                  Return:    RETURNok, RETURNerror                      **
 **                  Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
1258 1259 1260 1261
 **                                                                        **
 ***************************************************************************/
int emm_proc_attach_reject(unsigned int ueid, int emm_cause)
{
1262
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
1263

1264
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
1265

1266 1267 1268 1269 1270
  /* Create temporary UE context */
  emm_data_context_t ue_ctx;
  memset(&ue_ctx, 0 , sizeof(emm_data_context_t));
  ue_ctx.is_dynamic = FALSE;
  ue_ctx.ueid = ueid;
Cedric Roux's avatar
 
Cedric Roux committed
1271

1272
  /* Update the EMM cause code */
1273
#if defined(NAS_BUILT_IN_EPC)
1274 1275

  if (ueid > 0)
Cedric Roux's avatar
 
Cedric Roux committed
1276
#else
1277
  if (ueid < EMM_DATA_NB_UE_MAX)
Cedric Roux's avatar
 
Cedric Roux committed
1278
#endif
1279 1280 1281 1282 1283
  {
    ue_ctx.emm_cause = emm_cause;
  } else {
    ue_ctx.emm_cause = EMM_CAUSE_ILLEGAL_UE;
  }
Cedric Roux's avatar
 
Cedric Roux committed
1284

1285 1286
  /* Do not accept attach request with protocol error */
  rc = _emm_attach_reject(&ue_ctx);
Cedric Roux's avatar
 
Cedric Roux committed
1287

1288
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
1289 1290 1291 1292
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1293
 ** Name:    emm_proc_attach_complete()                                **
Cedric Roux's avatar
 
Cedric Roux committed
1294 1295
 **                                                                        **
 ** Description: Terminates the attach procedure upon receiving Attach     **
Cedric Roux's avatar
Cedric Roux committed
1296
 **      Complete message from the UE.                             **
Cedric Roux's avatar
 
Cedric Roux committed
1297 1298
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.4                         **
Cedric Roux's avatar
Cedric Roux committed
1299 1300 1301
 **      Upon receiving an ATTACH COMPLETE message, the MME shall  **
 **      stop timer T3450, enter state EMM-REGISTERED and consider **
 **      the GUTI sent in the ATTACH ACCEPT message as valid.      **
Cedric Roux's avatar
 
Cedric Roux committed
1302
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1303
 ** Inputs:  ueid:      UE lower layer identifier                  **
1304
 **      esm_msg_pP:   Activate default EPS bearer context accept **
Cedric Roux's avatar
Cedric Roux committed
1305 1306
 **             ESM message                                **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
1307
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1308 1309 1310
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data, T3450                           **
Cedric Roux's avatar
 
Cedric Roux committed
1311 1312
 **                                                                        **
 ***************************************************************************/
1313
int emm_proc_attach_complete(unsigned int ueid, const OctetString *esm_msg_pP)
Cedric Roux's avatar
 
Cedric Roux committed
1314
{
1315 1316 1317 1318
  emm_data_context_t *emm_ctx = NULL;
  int rc = RETURNerror;
  emm_sap_t emm_sap;
  esm_sap_t esm_sap;
Cedric Roux's avatar
 
Cedric Roux committed
1319

1320
  LOG_FUNC_IN;
1321

1322
  LOG_TRACE(INFO, "EMM-PROC  - EPS attach complete (ueid=%u)", ueid);
Cedric Roux's avatar
 
Cedric Roux committed
1323

1324 1325 1326
  /* Stop timer T3450 */
  LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3450 (%d)", T3450.id);
  T3450.id = nas_timer_stop(T3450.id);
Cedric Roux's avatar
 
Cedric Roux committed
1327

1328 1329 1330 1331 1332 1333
  /* Release retransmission timer parameters */
  attach_data_t *data = (attach_data_t *)(emm_proc_common_get_args(ueid));

  if (data) {
    if (data->esm_msg.length > 0) {
      free(data->esm_msg.value);
Cedric Roux's avatar
 
Cedric Roux committed
1334 1335
    }

1336 1337 1338 1339
    free(data);
  }

  /* Get the UE context */
Cedric Roux's avatar
 
Cedric Roux committed
1340

1341
#if defined(NAS_BUILT_IN_EPC)
1342 1343 1344 1345 1346

  if (ueid > 0) {
    emm_ctx = emm_data_context_get(&_emm_data, ueid);
  }

Cedric Roux's avatar
 
Cedric Roux committed
1347
#else
1348 1349 1350 1351 1352

  if (ueid < EMM_DATA_NB_UE_MAX) {
    emm_ctx = _emm_data.ctx[ueid];
  }

Cedric Roux's avatar
 
Cedric Roux committed
1353 1354
#endif

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
  if (emm_ctx) {
    /* Delete the old GUTI and consider the GUTI sent in the Attach
     * Accept message as valid */
    emm_ctx->guti_is_new = FALSE;
    emm_ctx->old_guti = NULL;
    /*
     * Forward the Activate Default EPS Bearer Context Accept message
     * to the EPS session management sublayer
     */
    esm_sap.primitive = ESM_DEFAULT_EPS_BEARER_CONTEXT_ACTIVATE_CNF;
    esm_sap.is_standalone = FALSE;
    esm_sap.ueid = ueid;
1367
    esm_sap.recv = esm_msg_pP;
1368 1369 1370 1371 1372
    esm_sap.ctx  = emm_ctx;
    rc = esm_sap_send(&esm_sap);
  } else {
    LOG_TRACE(ERROR, "EMM-PROC  - No EMM context exists");
  }
Cedric Roux's avatar
 
Cedric Roux committed
1373

1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
  if ( (rc != RETURNerror) && (esm_sap.err == ESM_SAP_SUCCESS) ) {
    /* Set the network attachment indicator */
    emm_ctx->is_attached = TRUE;
    /*
     * Notify EMM that attach procedure has successfully completed
     */
    emm_sap.primitive = EMMREG_ATTACH_CNF;
    emm_sap.u.emm_reg.ueid = ueid;
    emm_sap.u.emm_reg.ctx  = emm_ctx;
    rc = emm_sap_send(&emm_sap);
  } else if (esm_sap.err != ESM_SAP_DISCARDED) {
    /*
     * Notify EMM that attach procedure failed
     */
    emm_sap.primitive = EMMREG_ATTACH_REJ;
    emm_sap.u.emm_reg.ueid = ueid;
    emm_sap.u.emm_reg.ctx  = emm_ctx;
    rc = emm_sap_send(&emm_sap);
  } else {
    /*
     * ESM procedure failed and, received message has been discarded or
     * Status message has been returned; ignore ESM procedure failure
     */
    rc = RETURNok;
  }
Cedric Roux's avatar
 
Cedric Roux committed
1399

1400
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
1401 1402
}

Cedric Roux's avatar
Cedric Roux committed
1403
#endif  // NAS_MME
Cedric Roux's avatar
 
Cedric Roux committed
1404 1405 1406 1407 1408 1409 1410 1411

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

#ifdef NAS_UE
/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1412
 *              Timer handlers
Cedric Roux's avatar
 
Cedric Roux committed
1413 1414 1415 1416 1417
 * --------------------------------------------------------------------------
 */

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1418
 ** Name:    _emm_attach_t3410_handler()                               **
Cedric Roux's avatar
 
Cedric Roux committed
1419 1420 1421 1422
 **                                                                        **
 ** Description: T3410 timeout handler                                     **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.6, case c                 **
Cedric Roux's avatar
Cedric Roux committed
1423 1424 1425 1426
 **      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.  **
Cedric Roux's avatar
 
Cedric Roux committed
1427
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1428 1429
 ** Inputs:  args:      handler parameters                         **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1430
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1431 1432 1433
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    T3410                                      **
Cedric Roux's avatar
 
Cedric Roux committed
1434 1435
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1436
void *_emm_attach_t3410_handler(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
1437
{
1438
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
1439

1440 1441
  emm_sap_t emm_sap;
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
1442

1443
  LOG_TRACE(WARNING, "EMM-PROC  - T3410 timer expired");
Cedric Roux's avatar
 
Cedric Roux committed
1444

1445 1446 1447 1448
  /* Stop T3410 timer */
  T3410.id = nas_timer_stop(T3410.id);
  /* Execute abnormal case attach procedure */
  _emm_attach_abnormal_cases_bcd(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
1449

1450
  rc = emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
1451

1452 1453 1454 1455 1456 1457
  if (rc != RETURNerror) {
    /* Locally release the NAS signalling connection */
    _emm_data.ecm_status = ECM_IDLE;
  }

  LOG_FUNC_RETURN(NULL);
Cedric Roux's avatar
 
Cedric Roux committed
1458 1459 1460 1461
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1462
 ** Name:    _emm_attach_t3411_handler()                               **
Cedric Roux's avatar
 
Cedric Roux committed
1463 1464 1465 1466
 **                                                                        **
 ** Description: T3411 timeout handler                                     **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.6                         **
Cedric Roux's avatar
Cedric Roux committed
1467 1468
 **      Upon T3411 timer expiration, the attach procedure shall   **
 **      be restarted, if still required by ESM sublayer.          **
Cedric Roux's avatar
 
Cedric Roux committed
1469
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1470 1471
 ** Inputs:  args:      handler parameters                         **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1472
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1473 1474 1475
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    T3411                                      **
Cedric Roux's avatar
 
Cedric Roux committed
1476 1477
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1478
static void *_emm_attach_t3411_handler(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
1479
{
1480
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
1481

1482
  emm_sap_t emm_sap;
Cedric Roux's avatar
 
Cedric Roux committed
1483

1484
  LOG_TRACE(WARNING, "EMM-PROC  - T3411 timer expired");
Cedric Roux's avatar
 
Cedric Roux committed
1485

1486 1487 1488 1489 1490 1491 1492 1493
  /* Stop T3411 timer */
  T3411.id = nas_timer_stop(T3411.id);
  /*
   * Notify EMM that timer T3411 expired and attach procedure has to be
   * restarted
   */
  emm_sap.primitive = EMMREG_ATTACH_INIT;
  emm_sap.u.emm_reg.u.attach.is_emergency = _emm_data.is_emergency;
Cedric Roux's avatar
 
Cedric Roux committed
1494

1495
  (void) emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
1496

1497
  LOG_FUNC_RETURN(NULL);
Cedric Roux's avatar
 
Cedric Roux committed
1498 1499 1500 1501
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1502
 ** Name:    _emm_attach_t3402_handler()                               **
Cedric Roux's avatar
 
Cedric Roux committed
1503 1504 1505
 **                                                                        **
 ** Description: T3402 timeout handler                                     **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1506
 **      Upon T3402 timer expiration:                              **
Cedric Roux's avatar
 
Cedric Roux committed
1507
 **              3GPP TS 24.301, section 5.5.1.1                           **
Cedric Roux's avatar
Cedric Roux committed
1508 1509
 **      the attach attempt counter shall be reset when the UE is  **
 **      in substate DEREGISTERED.ATTEMPTING-TO-ATTACH;            **
Cedric Roux's avatar
 
Cedric Roux committed
1510
 **              3GPP TS 24.301, section 5.2.2.3.3                         **
Cedric Roux's avatar
Cedric Roux committed
1511 1512
 **      the UE shall initiate an attach or combined attach proce- **
 **      dure in substate DEREGISTERED.ATTEMPTING-TO-ATTACH;       **
Cedric Roux's avatar
 
Cedric Roux committed
1513
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1514 1515
 ** Inputs:  args:      handler parameters                         **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1516
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1517 1518 1519
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _emm_attach_data, T3402                    **
Cedric Roux's avatar
 
Cedric Roux committed
1520 1521
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1522
static void *_emm_attach_t3402_handler(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
1523
{
1524
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
1525

1526
  emm_sap_t emm_sap;
Cedric Roux's avatar
 
Cedric Roux committed
1527

1528
  LOG_TRACE(WARNING, "EMM-PROC  - T3402 timer expired");
Cedric Roux's avatar
 
Cedric Roux committed
1529

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
  /* Stop T3402 timer */
  T3402.id = nas_timer_stop(T3402.id);
  /* Reset the attach attempt counter */
  _emm_attach_data.attempt_count = 0;
  /*
   * Notify EMM that timer T3402 expired and attach procedure has to be
   * restarted
   */
  emm_sap.primitive = EMMREG_ATTACH_INIT;
  emm_sap.u.emm_reg.u.attach.is_emergency = _emm_data.is_emergency;
Cedric Roux's avatar
 
Cedric Roux committed
1540

1541
  (void) emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
1542

1543
  LOG_FUNC_RETURN(NULL);
Cedric Roux's avatar
 
Cedric Roux committed
1544 1545 1546 1547
}

/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1548
 *              Abnormal cases in the UE
Cedric Roux's avatar
 
Cedric Roux committed
1549 1550 1551 1552 1553
 * --------------------------------------------------------------------------
 */

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1554
 ** Name:    _emm_attach_abnormal_cases_bcd()                          **
Cedric Roux's avatar
 
Cedric Roux committed
1555 1556 1557
 **                                                                        **
 ** Description: Performs the abnormal case attach procedure.              **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1558 1559 1560 1561 1562
 **      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.                 **
Cedric Roux's avatar
 
Cedric Roux committed
1563
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1564 1565
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1566
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1567 1568 1569 1570
 ** Outputs:     emm_sap:   EMM service access point                   **
 **      Return:    None                                       **
 **      Others:    _emm_data, _emm_attach_data, T3402, T3410, **
 **             T3411                                      **
Cedric Roux's avatar
 
Cedric Roux committed
1571 1572
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1573
static void _emm_attach_abnormal_cases_bcd(emm_sap_t *emm_sap)
Cedric Roux's avatar
 
Cedric Roux committed
1574
{
1575 1576 1577 1578
  LOG_FUNC_IN;

  LOG_TRACE(WARNING, "EMM-PROC  - Abnormal case, attach counter = %d",
            _emm_attach_data.attempt_count);
Cedric Roux's avatar
 
Cedric Roux committed
1579

1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
  /* Stop timer T3410 */
  if (T3410.id != NAS_TIMER_INACTIVE_ID) {
    LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3410 (%d)", T3410.id);
    T3410.id = nas_timer_stop(T3410.id);
  }

  if (_emm_attach_data.attempt_count < EMM_ATTACH_COUNTER_MAX) {
    /* Increment the attach attempt counter */
    _emm_attach_data.attempt_count += 1;
    /* Start T3411 timer */
    T3411.id = nas_timer_start(T3411.sec, _emm_attach_t3411_handler, NULL);
    LOG_TRACE(INFO, "EMM-PROC  - Timer T3411 (%d) expires in %ld seconds",
              T3411.id, T3411.sec);
    /*
     * 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 */
    _emm_data.guti = NULL;
    /* Delete the TAI list */
    _emm_data.ltai.n_tais = 0;
    /* Delete the last visited registered TAI */
    _emm_data.tai = NULL;
    /* Delete the list of equivalent PLMNs */
    _emm_data.nvdata.eplmn.n_plmns = 0;
Cedric Roux's avatar
 
Cedric Roux committed
1608

1609 1610 1611
    /* Delete the eKSI */
    if (_emm_data.security) {
      _emm_data.security->type = EMM_KSI_NOT_AVAILABLE;
Cedric Roux's avatar
 
Cedric Roux committed
1612 1613
    }

1614 1615
    /* Set the EPS update status to EU2 NOT UPDATED */
    _emm_data.status = EU2_NOT_UPDATED;
Cedric Roux's avatar
 
Cedric Roux committed
1616

1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
    /* Start T3402 timer */
    T3402.id = nas_timer_start(T3402.sec, _emm_attach_t3402_handler, NULL);
    LOG_TRACE(INFO, "EMM-PROC  - Timer T3402 (%d) expires in %ld seconds",
              T3402.id, T3402.sec);
    /*
     * 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;
Cedric Roux's avatar
 
Cedric Roux committed
1629 1630 1631 1632 1633 1634
}
#endif // NAS_UE

#ifdef NAS_MME
/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1635
 *              Timer handlers
Cedric Roux's avatar
 
Cedric Roux committed
1636 1637 1638 1639 1640
 * --------------------------------------------------------------------------
 */

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1641
 ** Name:    _emm_attach_t3450_handler()                               **
Cedric Roux's avatar
 
Cedric Roux committed
1642 1643 1644 1645
 **                                                                        **
 ** Description: T3450 timeout handler                                     **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.7, case c                 **
Cedric Roux's avatar
Cedric Roux committed
1646 1647 1648 1649 1650 1651
 **      On the first expiry of the timer T3450, the network shall **
 **      retransmit the ATTACH ACCEPT message and shall reset and  **
 **      restart timer T3450. This retransmission is repeated four **
 **      times, i.e. on the fifth expiry of timer T3450, the at-   **
 **      tach procedure shall be aborted and the MME enters state  **
 **      EMM-DEREGISTERED.                                         **
Cedric Roux's avatar
 
Cedric Roux committed
1652
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1653 1654
 ** Inputs:  args:      handler parameters                         **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1655
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1656 1657 1658
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1659 1660
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1661
static void *_emm_attach_t3450_handler(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
1662
{
1663
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
1664

1665
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
1666

1667
  attach_data_t *data = (attach_data_t *)(args);
Cedric Roux's avatar
 
Cedric Roux committed
1668

1669 1670
  /* Increment the retransmission counter */
  data->retransmission_count += 1;
Cedric Roux's avatar
 
Cedric Roux committed
1671

1672 1673
  LOG_TRACE(WARNING, "EMM-PROC  - T3450 timer expired, retransmission "
            "counter = %d", data->retransmission_count);
Cedric Roux's avatar
 
Cedric Roux committed
1674

1675 1676
  /* Get the UE's EMM context */
  emm_data_context_t *emm_ctx = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
1677

1678
#if defined(NAS_BUILT_IN_EPC)
1679
  emm_ctx = emm_data_context_get(&_emm_data, data->ueid);
Cedric Roux's avatar
 
Cedric Roux committed
1680
#else
1681
  emm_ctx = _emm_data.ctx[data->ueid];
Cedric Roux's avatar
 
Cedric Roux committed
1682 1683
#endif

1684 1685 1686 1687 1688 1689 1690
  if (data->retransmission_count < ATTACH_COUNTER_MAX) {
    /* Send attach accept message to the UE */
    rc = _emm_attach_accept(emm_ctx, data);
  } else {
    /* Abort the attach procedure */
    rc = _emm_attach_abort(data);
  }
Cedric Roux's avatar
 
Cedric Roux committed
1691

1692
  LOG_FUNC_RETURN(NULL);
Cedric Roux's avatar
 
Cedric Roux committed
1693 1694 1695 1696
}

/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1697
 *              Abnormal cases in the MME
Cedric Roux's avatar
 
Cedric Roux committed
1698 1699 1700 1701 1702
 * --------------------------------------------------------------------------
 */

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1703
 ** Name:    _emm_attach_release()                                     **
Cedric Roux's avatar
 
Cedric Roux committed
1704 1705 1706
 **                                                                        **
 ** Description: Releases the UE context data.                             **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1707 1708
 ** Inputs:  args:      Data to be released                        **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1709
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1710 1711 1712
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1713 1714
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1715
static int _emm_attach_release(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
1716
{
1717
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
1718

1719
  int rc = RETURNerror;
Cedric Roux's avatar
 
Cedric Roux committed
1720

1721
  emm_data_context_t *emm_ctx = (emm_data_context_t *)(args);
Cedric Roux's avatar
 
Cedric Roux committed
1722

1723 1724 1725
  if (emm_ctx) {
    LOG_TRACE(WARNING, "EMM-PROC  - Release UE context data (ueid=%u)",
              emm_ctx->ueid);
Cedric Roux's avatar
Cedric Roux committed
1726

1727
    unsigned int ueid = emm_ctx->ueid;
Cedric Roux's avatar
Cedric Roux committed
1728

1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
    if (emm_ctx->guti) {
      free(emm_ctx->guti);
      emm_ctx->guti = NULL;
    }

    if (emm_ctx->imsi) {
      free(emm_ctx->imsi);
      emm_ctx->imsi = NULL;
    }

    if (emm_ctx->imei) {
      free(emm_ctx->imei);
      emm_ctx->imei = NULL;
    }

    if (emm_ctx->esm_msg.length > 0) {
      free(emm_ctx->esm_msg.value);
      emm_ctx->esm_msg.value = NULL;
    }

    /* Release NAS security context */
    if (emm_ctx->security) {
      emm_security_context_t *security = emm_ctx->security;

      if (security->kasme.value) {
        free(security->kasme.value);
        security->kasme.value  = NULL;
        security->kasme.length = 0;
      }

      if (security->knas_enc.value) {
        free(security->knas_enc.value);
        security->knas_enc.value  = NULL;
        security->knas_enc.length = 0;
      }

      if (security->knas_int.value) {
        free(security->knas_int.value);
        security->knas_int.value  = NULL;
        security->knas_int.length = 0;
      }

      free(emm_ctx->security);
      emm_ctx->security = NULL;
    }

    /* Release the EMM context */
1776
#if defined(NAS_BUILT_IN_EPC)
1777
    emm_data_context_remove(&_emm_data, emm_ctx);
Cedric Roux's avatar
 
Cedric Roux committed
1778
#else
1779 1780
    free(_emm_data.ctx[ueid]);
    _emm_data.ctx[ueid] = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
1781
#endif
1782 1783 1784 1785 1786 1787 1788 1789 1790
    /*
     * Notify EMM that the attach procedure is aborted
     */
    emm_sap_t emm_sap;
    emm_sap.primitive = EMMREG_PROC_ABORT;
    emm_sap.u.emm_reg.ueid = ueid;
    emm_sap.u.emm_reg.ctx  = emm_ctx;
    rc = emm_sap_send(&emm_sap);
  }
Cedric Roux's avatar
 
Cedric Roux committed
1791

1792
  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
1793 1794 1795 1796
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1797
 ** Name:    _emm_attach_reject()                                      **
Cedric Roux's avatar
 
Cedric Roux committed
1798 1799 1800 1801
 **                                                                        **
 ** Description: Performs the attach procedure not accepted by the network.**
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.5                         **
Cedric Roux's avatar
Cedric Roux committed
1802 1803 1804
 **      If the attach request cannot be accepted by the network,  **
 **      the MME shall send an ATTACH REJECT message to the UE in- **
 **      including an appropriate EMM cause value.                 **
Cedric Roux's avatar
 
Cedric Roux committed
1805
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1806 1807
 ** Inputs:  args:      UE context data                            **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1808
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1809 1810 1811
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1812 1813
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1814
static int _emm_attach_reject(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
1815
{
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
  LOG_FUNC_IN;

  int rc = RETURNerror;

  emm_data_context_t *emm_ctx = (emm_data_context_t *)(args);

  if (emm_ctx) {
    emm_sap_t emm_sap;
    LOG_TRACE(WARNING, "EMM-PROC  - EMM attach procedure not accepted "
              "by the network (ueid=%08x, cause=%d)",
              emm_ctx->ueid, emm_ctx->emm_cause);
    /*
     * Notify EMM-AS SAP that Attach Reject message has to be sent
     * onto the network
     */
    emm_sap.primitive = EMMAS_ESTABLISH_REJ;
    emm_sap.u.emm_as.u.establish.ueid = emm_ctx->ueid;
    emm_sap.u.emm_as.u.establish.UEid.guti = NULL;

    if (emm_ctx->emm_cause == EMM_CAUSE_SUCCESS) {
      emm_ctx->emm_cause = EMM_CAUSE_ILLEGAL_UE;
Cedric Roux's avatar
 
Cedric Roux committed
1837 1838
    }

1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
    emm_sap.u.emm_as.u.establish.emm_cause = emm_ctx->emm_cause;
    emm_sap.u.emm_as.u.establish.NASinfo = EMM_AS_NAS_INFO_ATTACH;

    if (emm_ctx->emm_cause != EMM_CAUSE_ESM_FAILURE) {
      emm_sap.u.emm_as.u.establish.NASmsg.length = 0;
      emm_sap.u.emm_as.u.establish.NASmsg.value = NULL;
    } else if (emm_ctx->esm_msg.length > 0) {
      emm_sap.u.emm_as.u.establish.NASmsg = emm_ctx->esm_msg;
    } else {
      LOG_TRACE(ERROR, "EMM-PROC  - ESM message is missing");
      LOG_FUNC_RETURN(RETURNerror);
    }

    /* Setup EPS NAS security data */
    emm_as_set_security_data(&emm_sap.u.emm_as.u.establish.sctx,
                             emm_ctx->security, FALSE, TRUE);
    rc = emm_sap_send(&emm_sap);

    /* Release the UE context, even if the network failed to send the
     * ATTACH REJECT message */
    if (emm_ctx->is_dynamic) {
      rc = _emm_attach_release(emm_ctx);
    }
  }

  LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
1865 1866 1867 1868
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1869
 ** Name:    _emm_attach_abort()                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1870 1871 1872
 **                                                                        **
 ** Description: Aborts the attach procedure                               **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1873 1874
 ** Inputs:  args:      Attach procedure data to be released       **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1875
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1876 1877 1878
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3450                                      **
Cedric Roux's avatar
 
Cedric Roux committed
1879 1880
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1881
static int _emm_attach_abort(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
1882
{
1883 1884 1885
  int rc = RETURNerror;
  emm_data_context_t *ctx = NULL;
  attach_data_t *data;
Cedric Roux's avatar
 
Cedric Roux committed
1886

1887
  LOG_FUNC_IN;
1888

1889
  data = (attach_data_t *)(args);
Cedric Roux's avatar
 
Cedric Roux committed
1890

1891 1892 1893
  if (data) {
    unsigned int ueid = data->ueid;
    esm_sap_t esm_sap;
Cedric Roux's avatar
Cedric Roux committed
1894

1895 1896
    LOG_TRACE(WARNING, "EMM-PROC  - Abort the attach procedure (ueid=%u)",
              ueid);
Cedric Roux's avatar
Cedric Roux committed
1897

1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
    /* Stop timer T3450 */
    if (T3450.id != NAS_TIMER_INACTIVE_ID) {
      LOG_TRACE(INFO, "EMM-PROC  - Stop timer T3450 (%d)", T3450.id);
      T3450.id = nas_timer_stop(T3450.id);
    }

    /* Release retransmission timer parameters */
    if (data->esm_msg.length > 0) {
      free(data->esm_msg.value);
    }

    free(data);
Cedric Roux's avatar
Cedric Roux committed
1910

1911
#if defined(NAS_BUILT_IN_EPC)
1912
    ctx = emm_data_context_get(&_emm_data, ueid);
1913
#else
1914
    ctx = _emm_data.ctx[ueid];
1915 1916
#endif

1917 1918 1919 1920 1921 1922 1923 1924 1925
    /*
     * Notify ESM that the network locally refused PDN connectivity
     * to the UE
     */
    esm_sap.primitive = ESM_PDN_CONNECTIVITY_REJ;
    esm_sap.ueid = ueid;
    esm_sap.ctx  = ctx;
    esm_sap.recv = NULL;
    rc = esm_sap_send(&esm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
1926

1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
    if (rc != RETURNerror) {
      /*
       * Notify EMM that EPS attach procedure failed
       */
      emm_sap_t emm_sap;
      emm_sap.primitive = EMMREG_ATTACH_REJ;
      emm_sap.u.emm_reg.ueid = ueid;
      emm_sap.u.emm_reg.ctx  = ctx;
      rc = emm_sap_send(&emm_sap);

      if (rc != RETURNerror) {
        /* Release the UE context */
        rc = _emm_attach_release(ctx);
      }
    }
  }

  LOG_FUNC_RETURN (rc);
Cedric Roux's avatar
 
Cedric Roux committed
1945 1946 1947 1948
}

/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1949
 *      Functions that may initiate EMM common procedures
Cedric Roux's avatar
 
Cedric Roux committed
1950 1951 1952 1953 1954
 * --------------------------------------------------------------------------
 */

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1955
 ** Name:    _emm_attach_identify()                                    **
Cedric Roux's avatar
 
Cedric Roux committed
1956 1957
 **                                                                        **
 ** Description: Performs UE's identification. May initiates identifica-   **
Cedric Roux's avatar
Cedric Roux committed
1958 1959
 **      tion, authentication and security mode control EMM common **
 **      procedures.                                               **
Cedric Roux's avatar
 
Cedric Roux committed
1960
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1961 1962
 ** Inputs:  args:      Identification argument parameters         **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1963
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1964 1965 1966
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
1967 1968
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1969
static int _emm_attach_identify(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
1970
{
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
  int rc = RETURNerror;
  emm_data_context_t *emm_ctx = (emm_data_context_t *)(args);
  int guti_reallocation = FALSE;

  LOG_FUNC_IN;

  LOG_TRACE(INFO, "EMM-PROC  - Identify incoming UE (ueid=0x%08x) using %s",
            emm_ctx->ueid, (emm_ctx->imsi)? "IMSI" : (emm_ctx->guti)? "GUTI" :
            (emm_ctx->imei)? "IMEI" : "none");

  /*
   * UE's identification
   * -------------------
   */
  if (emm_ctx->imsi) {
    /* The UE identifies itself using an IMSI */
1987
#if defined(NAS_BUILT_IN_EPC)
1988 1989 1990
    if (!emm_ctx->security) {
      /* Ask upper layer to fetch new security context */
      nas_itti_auth_info_req(emm_ctx->ueid, emm_ctx->imsi, 1, NULL);
Cedric Roux's avatar
 
Cedric Roux committed
1991

1992 1993 1994 1995 1996
      rc = RETURNok;
    } else
#endif
    {
      rc = mme_api_identify_imsi(emm_ctx->imsi, &emm_ctx->vector);
1997

1998 1999 2000 2001 2002
      if (rc != RETURNok) {
        LOG_TRACE(WARNING, "EMM-PROC  - "
                  "Failed to identify the UE using provided IMSI");
        emm_ctx->emm_cause = EMM_CAUSE_ILLEGAL_UE;
      }
Cedric Roux's avatar
 
Cedric Roux committed
2003

2004 2005 2006 2007 2008
      guti_reallocation = TRUE;
    }
  } else if (emm_ctx->guti) {
    /* The UE identifies itself using a GUTI */
    rc = mme_api_identify_guti(emm_ctx->guti, &emm_ctx->vector);
2009

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
    if (rc != RETURNok) {
      LOG_TRACE(WARNING, "EMM-PROC  - Failed to identify the UE using "
                "provided GUTI (tmsi=%u)", emm_ctx->guti->m_tmsi);
      /* 3GPP TS 24.401, Figure 5.3.2.1-1, point 4
       * The UE was attempting to attach to the network using a GUTI
       * that is not known by the network; the MME shall initiate an
       * identification procedure to retrieve the IMSI from the UE.
       */
      rc = emm_proc_identification(emm_ctx->ueid,
                                   emm_ctx,
                                   EMM_IDENT_TYPE_IMSI,
                                   _emm_attach_identify,
                                   _emm_attach_release,
                                   _emm_attach_release);

      if (rc != RETURNok) {
        /* Failed to initiate the identification procedure */
        LOG_TRACE(WARNING, "EMM-PROC  - "
                  "Failed to initiate identification procedure");
Cedric Roux's avatar
Cedric Roux committed
2029
        emm_ctx->emm_cause = EMM_CAUSE_ILLEGAL_UE;
2030 2031 2032 2033 2034 2035 2036
        /* Do not accept the UE to attach to the network */
        rc = _emm_attach_reject(emm_ctx);
      }

      /* Relevant callback will be executed when identification
       * procedure completes */
      LOG_FUNC_RETURN(rc);
Cedric Roux's avatar
 
Cedric Roux committed
2037
    }
2038 2039 2040 2041
  } else if ( (emm_ctx->imei) && (emm_ctx->is_emergency) ) {
    /* The UE is attempting to attach to the network for emergency
     * services using an IMEI */
    rc = mme_api_identify_imei(emm_ctx->imei, &emm_ctx->vector);
Cedric Roux's avatar
 
Cedric Roux committed
2042

2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
    if (rc != RETURNok) {
      LOG_TRACE(WARNING, "EMM-PROC  - "
                "Failed to identify the UE using provided IMEI");
      emm_ctx->emm_cause = EMM_CAUSE_IMEI_NOT_ACCEPTED;
    }
  } else {
    LOG_TRACE(WARNING, "EMM-PROC  - UE's identity is not available");
    emm_ctx->emm_cause = EMM_CAUSE_ILLEGAL_UE;
  }

  /*
   * GUTI reallocation
   * -----------------
   */
  if ( (rc != RETURNerror) && guti_reallocation ) {
    /* Release the old GUTI */
    if (emm_ctx->old_guti) {
      free(emm_ctx->old_guti);
    }

    /* Save the GUTI previously used by the UE to identify itself */
    emm_ctx->old_guti = emm_ctx->guti;
    /* Allocate a new GUTI */
    emm_ctx->guti = (GUTI_t *)malloc(sizeof(GUTI_t));
    /* Request the MME to assign a GUTI to the UE */
    rc = mme_api_new_guti(emm_ctx->imsi, emm_ctx->guti,
                          &emm_ctx->tac, &emm_ctx->n_tacs);

    if (rc != RETURNok) {
      LOG_TRACE(WARNING, "EMM-PROC  - Failed to assign new GUTI");
      emm_ctx->emm_cause = EMM_CAUSE_ILLEGAL_UE;
    } else {
      LOG_TRACE(WARNING, "EMM-PROC  - New GUTI assigned to the UE "
                "(tmsi=%u)", emm_ctx->guti->m_tmsi);
      /* Update the GUTI indicator as new */
      emm_ctx->guti_is_new = TRUE;
    }
  }

  /*
   * UE's authentication
   * -------------------
   */
  if (rc != RETURNerror) {
    if (emm_ctx->security) {
      /* A security context exists for the UE in the network;
       * proceed with the attach procedure.
       */
      rc = _emm_attach(emm_ctx);
    } else if ( (emm_ctx->is_emergency) &&
                (_emm_data.conf.features & MME_API_UNAUTHENTICATED_IMSI) ) {
      /* 3GPP TS 24.301, section 5.5.1.2.3
       * 3GPP TS 24.401, Figure 5.3.2.1-1, point 5a
       * MME configured to support Emergency Attach for unauthenticated
       * IMSIs may choose to skip the authentication procedure even if
       * no EPS security context is available and proceed directly to the
       * execution of the security mode control procedure.
       */
      rc = _emm_attach_security(emm_ctx);
Cedric Roux's avatar
 
Cedric Roux committed
2102 2103
    }

2104
#if !defined(NAS_BUILT_IN_EPC)
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
    else {
      /* 3GPP TS 24.401, Figure 5.3.2.1-1, point 5a
       * No EMM context exists for the UE in the network; authentication
       * and NAS security setup to activate integrity protection and NAS
       * ciphering are mandatory.
       */
      auth_vector_t *auth = &emm_ctx->vector;
      const OctetString loc_rand = {AUTH_RAND_SIZE, (uint8_t *)auth->rand};
      const OctetString autn = {AUTH_AUTN_SIZE, (uint8_t *)auth->autn};
      rc = emm_proc_authentication(emm_ctx, emm_ctx->ueid, 0, // TODO: eksi != 0
                                   &loc_rand, &autn,
                                   _emm_attach_security,
                                   _emm_attach_release,
                                   _emm_attach_release);

      if (rc != RETURNok) {
        /* Failed to initiate the authentication procedure */
        LOG_TRACE(WARNING, "EMM-PROC  - "
                  "Failed to initiate authentication procedure");
        emm_ctx->emm_cause = EMM_CAUSE_ILLEGAL_UE;
      }
Cedric Roux's avatar
Cedric Roux committed
2126
    }
Cedric Roux's avatar
 
Cedric Roux committed
2127

2128 2129 2130 2131 2132 2133 2134 2135 2136
#endif
  }

  if (rc != RETURNok) {
    /* Do not accept the UE to attach to the network */
    rc = _emm_attach_reject(emm_ctx);
  }

  LOG_FUNC_RETURN (rc);
Cedric Roux's avatar
 
Cedric Roux committed
2137 2138 2139 2140
}

/****************************************************************************
 **                                                                        **
2141
 ** Name:        _emm_attach_security()                                    **
Cedric Roux's avatar
 
Cedric Roux committed
2142 2143 2144
 **                                                                        **
 ** Description: Initiates security mode control EMM common procedure.     **
 **                                                                        **
2145 2146
 ** Inputs:          args:      security argument parameters               **
 **                  Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
2147
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2148
 ** Outputs:     None                                                      **
2149 2150
 **                  Return:    RETURNok, RETURNerror                      **
 **                  Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
2151 2152
 **                                                                        **
 ***************************************************************************/
2153
#if defined(NAS_BUILT_IN_EPC)
2154 2155
int emm_attach_security(void *args)
{
2156
  return _emm_attach_security(args);
2157 2158 2159
}
#endif

Cedric Roux's avatar
Cedric Roux committed
2160
static int _emm_attach_security(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
2161
{
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
  LOG_FUNC_IN;

  int rc;
  emm_data_context_t *emm_ctx = (emm_data_context_t *)(args);

  LOG_TRACE(INFO, "EMM-PROC  - Setup NAS security (ueid=%u)", emm_ctx->ueid);

  /* Create new NAS security context */
  if (emm_ctx->security == NULL) {
    emm_ctx->security =
      (emm_security_context_t *)malloc(sizeof(emm_security_context_t));
  }

  if (emm_ctx->security) {
    memset(emm_ctx->security, 0, sizeof(emm_security_context_t));
    emm_ctx->security->type = EMM_KSI_NOT_AVAILABLE;
    emm_ctx->security->selected_algorithms.encryption = NAS_SECURITY_ALGORITHMS_EEA0;
    emm_ctx->security->selected_algorithms.integrity  = NAS_SECURITY_ALGORITHMS_EIA0;
  } else {
    LOG_TRACE(WARNING, "EMM-PROC  - Failed to create security context");
    emm_ctx->emm_cause = EMM_CAUSE_ILLEGAL_UE;
    /* Do not accept the UE to attach to the network */
    rc = _emm_attach_reject(emm_ctx);
    LOG_FUNC_RETURN(rc);
  }

  /* Initialize the security mode control procedure */
  rc = emm_proc_security_mode_control(emm_ctx->ueid, 0, // TODO: eksi != 0
                                      emm_ctx->eea, emm_ctx->eia,emm_ctx->ucs2,
                                      emm_ctx->uea, emm_ctx->uia, emm_ctx->gea,
                                      emm_ctx->umts_present, emm_ctx->gprs_present,
                                      _emm_attach, _emm_attach_release,
                                      _emm_attach_release);

  if (rc != RETURNok) {
    /* Failed to initiate the security mode control procedure */
    LOG_TRACE(WARNING, "EMM-PROC  - "
              "Failed to initiate security mode control procedure");
    emm_ctx->emm_cause = EMM_CAUSE_ILLEGAL_UE;
    /* Do not accept the UE to attach to the network */
    rc = _emm_attach_reject(emm_ctx);
  }

  LOG_FUNC_RETURN (rc);
Cedric Roux's avatar
 
Cedric Roux committed
2206 2207 2208 2209
}

/*
 * --------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
2210
 *              MME specific local functions
Cedric Roux's avatar
 
Cedric Roux committed
2211 2212 2213 2214 2215
 * --------------------------------------------------------------------------
 */

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2216
 ** Name:    _emm_attach()                                             **
Cedric Roux's avatar
 
Cedric Roux committed
2217 2218
 **                                                                        **
 ** Description: Performs the attach signalling procedure while a context  **
Cedric Roux's avatar
Cedric Roux committed
2219
 **      exists for the incoming UE in the network.                **
Cedric Roux's avatar
 
Cedric Roux committed
2220 2221
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.1.2.4                         **
Cedric Roux's avatar
Cedric Roux committed
2222 2223 2224
 **      Upon receiving the ATTACH REQUEST message, the MME shall  **
 **      send an ATTACH ACCEPT message to the UE and start timer   **
 **      T3450.                                                    **
Cedric Roux's avatar
 
Cedric Roux committed
2225
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2226 2227
 ** Inputs:  args:      attach argument parameters                 **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
2228
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2229 2230 2231
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
2232 2233
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
2234
static int _emm_attach(void *args)
Cedric Roux's avatar
 
Cedric Roux committed
2235
{
2236
  LOG_FUNC_IN;
Cedric Roux's avatar
 
Cedric Roux committed
2237

2238 2239
  esm_sap_t esm_sap;
  int rc;
Cedric Roux's avatar
 
Cedric Roux committed
2240

2241
  emm_data_context_t *emm_ctx = (emm_data_context_t *)(args);
Cedric Roux's avatar
 
Cedric Roux committed
2242

2243
  LOG_TRACE(INFO, "EMM-PROC  - Attach UE (ueid=%u)", emm_ctx->ueid);
Cedric Roux's avatar
 
Cedric Roux committed
2244

2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259
  /* 3GPP TS 24.401, Figure 5.3.2.1-1, point 5a
   * At this point, all NAS messages shall be protected by the NAS security
   * functions (integrity and ciphering) indicated by the MME unless the UE
   * is emergency attached and not successfully authenticated.
   */

  /*
   * Notify ESM that PDN connectivity is requested
   */
  esm_sap.primitive = ESM_PDN_CONNECTIVITY_REQ;
  esm_sap.is_standalone = FALSE;
  esm_sap.ueid = emm_ctx->ueid;
  esm_sap.ctx  = emm_ctx;
  esm_sap.recv = &emm_ctx->esm_msg;
  rc = esm_sap_send(&esm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
2260

2261
  if ( (rc != RETURNerror) && (esm_sap.err == ESM_SAP_SUCCESS) ) {
Cedric Roux's avatar
 
Cedric Roux committed
2262
    /*
2263
     * The attach request is accepted by the network
Cedric Roux's avatar
 
Cedric Roux committed
2264 2265
     */

2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
    /* Delete the stored UE radio capability information, if any */
    /* Store the UE network capability */
    /* Assign the TAI list the UE is registered to */

    /* Allocate parameters of the retransmission timer callback */
    attach_data_t *data = (attach_data_t *)calloc(1,sizeof(attach_data_t));

    if (data != NULL) {
      /* Setup ongoing EMM procedure callback functions */
      rc = emm_proc_common_initialize(emm_ctx->ueid, NULL, NULL, NULL,
                                      _emm_attach_abort, data);

      if (rc != RETURNok) {
        LOG_TRACE(WARNING,
                  "Failed to initialize EMM callback functions");
        free(data);
        LOG_FUNC_RETURN (RETURNerror);
      }

      /* Set the UE identifier */
      data->ueid = emm_ctx->ueid;
      /* Reset the retransmission counter */
      data->retransmission_count = 0;
2289
#if defined(ORIGINAL_CODE)
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
      /* Setup the ESM message container */
      data->esm_msg.value = (uint8_t *)malloc(esm_sap.send.length);

      if (data->esm_msg.value) {
        data->esm_msg.length = esm_sap.send.length;
        memcpy(data->esm_msg.value, esm_sap.send.value,
               esm_sap.send.length);
      } else {
        data->esm_msg.length = 0;
      }

      /* Send attach accept message to the UE */
      rc = _emm_attach_accept(emm_ctx, data);

      if (rc != RETURNerror) {
        if (emm_ctx->guti_is_new && emm_ctx->old_guti) {
          /* Implicit GUTI reallocation;
           * Notify EMM that common procedure has been initiated
           */
          emm_sap_t emm_sap;
          emm_sap.primitive = EMMREG_COMMON_PROC_REQ;
          emm_sap.u.emm_reg.ueid = data->ueid;
          rc = emm_sap_send(&emm_sap);
        }
      }

2316
#else
2317
      rc = RETURNok;
2318
#endif
Cedric Roux's avatar
 
Cedric Roux committed
2319
    }
2320 2321 2322 2323 2324
  } else if (esm_sap.err != ESM_SAP_DISCARDED) {
    /*
     * The attach procedure failed due to an ESM procedure failure
     */
    emm_ctx->emm_cause = EMM_CAUSE_ESM_FAILURE;
Cedric Roux's avatar
 
Cedric Roux committed
2325

2326 2327 2328 2329
    /* Setup the ESM message container to include PDN Connectivity Reject
     * message within the Attach Reject message */
    if (emm_ctx->esm_msg.length > 0) {
      free(emm_ctx->esm_msg.value);
Cedric Roux's avatar
 
Cedric Roux committed
2330 2331
    }

2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
    emm_ctx->esm_msg.value = (uint8_t *)malloc(esm_sap.send.length);

    if (emm_ctx->esm_msg.value) {
      emm_ctx->esm_msg.length = esm_sap.send.length;
      memcpy(emm_ctx->esm_msg.value, esm_sap.send.value,
             esm_sap.send.length);
      /* Send Attach Reject message */
      rc = _emm_attach_reject(emm_ctx);
    } else {
      emm_ctx->esm_msg.length = 0;
    }
  } else {
    /*
     * ESM procedure failed and, received message has been discarded or
     * Status message has been returned; ignore ESM procedure failure
     */
    rc = RETURNok;
  }

  if (rc != RETURNok) {
    /* The attach procedure failed */
    LOG_TRACE(WARNING, "EMM-PROC  - Failed to respond to Attach Request");
    emm_ctx->emm_cause = EMM_CAUSE_PROTOCOL_ERROR;
    /* Do not accept the UE to attach to the network */
    rc = _emm_attach_reject(emm_ctx);
  }

  LOG_FUNC_RETURN (rc);
Cedric Roux's avatar
 
Cedric Roux committed
2360 2361
}

2362 2363
int emm_cn_wrapper_attach_accept(emm_data_context_t *emm_ctx, void *data)
{
2364
  return _emm_attach_accept(emm_ctx,(attach_data_t *)data);
2365
}
Cedric Roux's avatar
 
Cedric Roux committed
2366 2367
/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2368
 ** Name:    _emm_attach_accept()                                      **
Cedric Roux's avatar
 
Cedric Roux committed
2369 2370 2371
 **                                                                        **
 ** Description: Sends ATTACH ACCEPT message and start timer T3450         **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2372 2373
 ** Inputs:  data:      Attach accept retransmission data          **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
2374
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2375 2376 2377
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3450                                      **
Cedric Roux's avatar
 
Cedric Roux committed
2378 2379
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
2380
static int _emm_attach_accept(emm_data_context_t *emm_ctx, attach_data_t *data)
Cedric Roux's avatar
 
Cedric Roux committed
2381
{
2382 2383 2384 2385 2386
  LOG_FUNC_IN;

  emm_sap_t emm_sap;
  int rc;

2387 2388 2389 2390 2391 2392
  // may be caused by timer not stopped when deleted context
  if (emm_ctx) {
    /*
     * Notify EMM-AS SAP that Attach Accept message together with an Activate
     * Default EPS Bearer Context Request message has to be sent to the UE
     */
2393

2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
    emm_sap.primitive = EMMAS_ESTABLISH_CNF;
    emm_sap.u.emm_as.u.establish.ueid = emm_ctx->ueid;

    if (emm_ctx->guti_is_new && emm_ctx->old_guti) {
      /* Implicit GUTI reallocation;
       * include the new assigned GUTI in the Attach Accept message  */
      LOG_TRACE(INFO,"EMM-PROC  - Implicit GUTI reallocation, include the new assigned GUTI in the Attach Accept message");
      emm_sap.u.emm_as.u.establish.UEid.guti = emm_ctx->old_guti;
      emm_sap.u.emm_as.u.establish.new_guti  = emm_ctx->guti;
    } else if (emm_ctx->guti_is_new && emm_ctx->guti) {
      /* include the new assigned GUTI in the Attach Accept message  */
      LOG_TRACE(INFO,"EMM-PROC  - Include the new assigned GUTI in the Attach Accept message");
      emm_sap.u.emm_as.u.establish.UEid.guti = emm_ctx->guti;
      emm_sap.u.emm_as.u.establish.new_guti  = emm_ctx->guti;
    } else {
      emm_sap.u.emm_as.u.establish.UEid.guti = emm_ctx->guti;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
2410
#warning "TEST LG FORCE GUTI IE IN ATTACH ACCEPT"
2411 2412 2413
      emm_sap.u.emm_as.u.establish.new_guti  = emm_ctx->guti;
      //emm_sap.u.emm_as.u.establish.new_guti  = NULL;
    }
2414

2415 2416 2417 2418 2419
    emm_sap.u.emm_as.u.establish.n_tacs  = emm_ctx->n_tacs;
    emm_sap.u.emm_as.u.establish.tac     = emm_ctx->tac;
    emm_sap.u.emm_as.u.establish.NASinfo = EMM_AS_NAS_INFO_ATTACH;
    /* Setup EPS NAS security data */
    emm_as_set_security_data(&emm_sap.u.emm_as.u.establish.sctx,
2420 2421
                           emm_ctx->security, FALSE, TRUE);

2422 2423 2424 2425 2426
    LOG_TRACE(INFO,"EMM-PROC  - encryption = 0x%X ", emm_sap.u.emm_as.u.establish.encryption);
    LOG_TRACE(INFO,"EMM-PROC  - integrity  = 0x%X ", emm_sap.u.emm_as.u.establish.integrity);
    emm_sap.u.emm_as.u.establish.encryption = emm_ctx->security->selected_algorithms.encryption;
    emm_sap.u.emm_as.u.establish.integrity  = emm_ctx->security->selected_algorithms.integrity;
    LOG_TRACE(INFO,"EMM-PROC  - encryption = 0x%X (0x%X)",
Lionel Gauthier's avatar
 
Lionel Gauthier committed
2427 2428
            emm_sap.u.emm_as.u.establish.encryption,
            emm_ctx->security->selected_algorithms.encryption);
2429
    LOG_TRACE(INFO,"EMM-PROC  - integrity  = 0x%X (0x%X)",
Lionel Gauthier's avatar
 
Lionel Gauthier committed
2430 2431 2432
            emm_sap.u.emm_as.u.establish.integrity,
            emm_ctx->security->selected_algorithms.integrity);

2433 2434
  /* Get the activate default EPS bearer context request message to
   * transfer within the ESM container of the attach accept message */
2435 2436
    emm_sap.u.emm_as.u.establish.NASmsg = data->esm_msg;
    LOG_TRACE(INFO,"EMM-PROC  - NASmsg  src size = %d NASmsg  dst size = %d ",
2437 2438
            data->esm_msg.length, emm_sap.u.emm_as.u.establish.NASmsg.length);

2439
    rc = emm_sap_send(&emm_sap);
Cedric Roux's avatar
 
Cedric Roux committed
2440

2441 2442 2443 2444 2445 2446 2447 2448
    if (rc != RETURNerror) {
      if (T3450.id != NAS_TIMER_INACTIVE_ID) {
        /* Re-start T3450 timer */
        T3450.id = nas_timer_restart(T3450.id);
      } else {
        /* Start T3450 timer */
        T3450.id = nas_timer_start(T3450.sec, _emm_attach_t3450_handler, data);
      }
Cedric Roux's avatar
 
Cedric Roux committed
2449

2450
      LOG_TRACE(INFO,"EMM-PROC  - Timer T3450 (%d) expires in %ld seconds",
2451
              T3450.id, T3450.sec);
2452 2453 2454
    }
  } else {
    LOG_TRACE(WARNING,"EMM-PROC  - emm_ctx NULL");
2455 2456
  }
  LOG_FUNC_RETURN (rc);
Cedric Roux's avatar
 
Cedric Roux committed
2457 2458 2459 2460
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2461
 ** Name:    _emm_attach_have_changed()                                **
Cedric Roux's avatar
 
Cedric Roux committed
2462 2463
 **                                                                        **
 ** Description: Check whether the given attach parameters differs from    **
Cedric Roux's avatar
Cedric Roux committed
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
 **      those previously stored when the attach procedure has     **
 **      been initiated.                                           **
 **                                                                        **
 ** Inputs:  ctx:       EMM context of the UE in the network       **
 **      type:      Type of the requested attach               **
 **      ksi:       Security ket sey identifier                **
 **      guti:      The GUTI provided by the UE                **
 **      imsi:      The IMSI provided by the UE                **
 **      imei:      The IMEI provided by the UE                **
 **      eea:       Supported EPS encryption algorithms        **
 **      eia:       Supported EPS integrity algorithms         **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    TRUE if at least one of the parameters     **
 **             differs; FALSE otherwise.                  **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
2481 2482
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
2483 2484 2485
static int _emm_attach_have_changed(const emm_data_context_t *ctx,
                                    emm_proc_attach_type_t type, int ksi,
                                    GUTI_t *guti, imsi_t *imsi, imei_t *imei,
2486 2487
                                    int eea, int eia, int ucs2, int uea, int uia, int gea,
                                    int umts_present, int gprs_present)
Cedric Roux's avatar
 
Cedric Roux committed
2488
{
2489
  LOG_FUNC_IN;
2490

2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
  /* Emergency bearer services indicator */
  if ( (type == EMM_ATTACH_TYPE_EMERGENCY) != ctx->is_emergency) {
    LOG_FUNC_RETURN (TRUE);
  }

  /* Security key set identifier */
  if (ksi != ctx->ksi) {
    LOG_FUNC_RETURN (TRUE);
  }

  /* Supported EPS encryption algorithms */
  if (eea != ctx->eea) {
    LOG_FUNC_RETURN (TRUE);
  }

  /* Supported EPS integrity algorithms */
  if (eia != ctx->eia) {
    LOG_FUNC_RETURN (TRUE);
  }

  if (umts_present != ctx->umts_present) {
    LOG_FUNC_RETURN (TRUE);
  }

  if (ctx->umts_present) {
    if (ucs2 != ctx->ucs2) {
      LOG_FUNC_RETURN (TRUE);
Cedric Roux's avatar
Cedric Roux committed
2518
    }
2519 2520 2521 2522

    /* Supported UMTS encryption algorithms */
    if (uea != ctx->uea) {
      LOG_FUNC_RETURN (TRUE);
Cedric Roux's avatar
 
Cedric Roux committed
2523
    }
2524 2525 2526 2527

    /* Supported UMTS integrity algorithms */
    if (uia != ctx->uia) {
      LOG_FUNC_RETURN (TRUE);
Cedric Roux's avatar
Cedric Roux committed
2528
    }
2529 2530 2531 2532 2533 2534 2535 2536 2537
  }

  if (gprs_present != ctx->gprs_present) {
    LOG_FUNC_RETURN (TRUE);
  }

  if (ctx->gprs_present) {
    if (gea != ctx->gea) {
      LOG_FUNC_RETURN (TRUE);
Cedric Roux's avatar
Cedric Roux committed
2538
    }
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
  }

  /* The GUTI if provided by the UE */
  if ( (guti) && (ctx->guti == NULL) ) {
    LOG_FUNC_RETURN (TRUE);
  }

  if ( (guti == NULL) && (ctx->guti) ) {
    LOG_FUNC_RETURN (TRUE);
  }

  if ( (guti) && (ctx->guti) ) {
    if (guti->m_tmsi != ctx->guti->m_tmsi) {
      LOG_FUNC_RETURN (TRUE);
Cedric Roux's avatar
 
Cedric Roux committed
2553
    }
2554 2555 2556

    if ( memcmp(&guti->gummei, &ctx->guti->gummei, sizeof(gummei_t)) != 0 ) {
      LOG_FUNC_RETURN (TRUE);
Cedric Roux's avatar
Cedric Roux committed
2557
    }
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
  }

  /* The IMSI if provided by the UE */
  if ( (imsi) && (ctx->imsi == NULL) ) {
    LOG_FUNC_RETURN (TRUE);
  }

  if ( (imsi == NULL) && (ctx->imsi) ) {
    LOG_FUNC_RETURN (TRUE);
  }

  if ( (imsi) && (ctx->imsi) ) {
    if ( memcmp(imsi, ctx->imsi, sizeof(imsi_t)) != 0 ) {
      LOG_FUNC_RETURN (TRUE);
Cedric Roux's avatar
Cedric Roux committed
2572
    }
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
  }

  /* The IMEI if provided by the UE */
  if ( (imei) && (ctx->imei == NULL) ) {
    LOG_FUNC_RETURN (TRUE);
  }

  if ( (imei == NULL) && (ctx->imei) ) {
    LOG_FUNC_RETURN (TRUE);
  }

  if ( (imei) && (ctx->imei) ) {
    if ( memcmp(imei, ctx->imei, sizeof(imei_t)) != 0 ) {
      LOG_FUNC_RETURN (TRUE);
Cedric Roux's avatar
 
Cedric Roux committed
2587
    }
2588 2589 2590
  }

  LOG_FUNC_RETURN (FALSE);
Cedric Roux's avatar
 
Cedric Roux committed
2591 2592 2593 2594
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
2595
 ** Name:    _emm_attach_update()                                      **
Cedric Roux's avatar
 
Cedric Roux committed
2596 2597
 **                                                                        **
 ** Description: Update the EMM context with the given attach procedure    **
Cedric Roux's avatar
Cedric Roux committed
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
 **      parameters.                                               **
 **                                                                        **
 ** Inputs:  ueid:      UE lower layer identifier                  **
 **      type:      Type of the requested attach               **
 **      ksi:       Security ket sey identifier                **
 **      guti:      The GUTI provided by the UE                **
 **      imsi:      The IMSI provided by the UE                **
 **      imei:      The IMEI provided by the UE                **
 **      eea:       Supported EPS encryption algorithms        **
 **      eia:       Supported EPS integrity algorithms         **
2608
 **      esm_msg_pP:   ESM message contained with the attach re-  **
Cedric Roux's avatar
Cedric Roux committed
2609 2610 2611 2612 2613 2614
 **             quest                                      **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     ctx:       EMM context of the UE in the network       **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
2615 2616
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
2617 2618 2619
static int _emm_attach_update(emm_data_context_t *ctx, unsigned int ueid,
                              emm_proc_attach_type_t type, int ksi,
                              GUTI_t *guti, imsi_t *imsi, imei_t *imei,
2620 2621
                              int eea, int eia, int ucs2, int uea, int uia, int gea,
                              int umts_present, int gprs_present,
2622
                              const OctetString *esm_msg_pP)
Cedric Roux's avatar
 
Cedric Roux committed
2623
{
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
  int mnc_length;
  LOG_FUNC_IN;
  /* UE identifier */
  ctx->ueid = ueid;
  /* Emergency bearer services indicator */
  ctx->is_emergency = (type == EMM_ATTACH_TYPE_EMERGENCY);
  /* Security key set identifier */
  ctx->ksi  = ksi;
  /* Supported EPS encryption algorithms */
  ctx->eea  = eea;
  /* Supported EPS integrity algorithms */
  ctx->eia  = eia;
  ctx->ucs2 = ucs2;
  ctx->uea  = uea;
  ctx->uia  = uia;
  ctx->gea  = gea;
  ctx->umts_present  = umts_present;
  ctx->gprs_present  = gprs_present;

  /* The GUTI if provided by the UE */
  if (guti) {
    LOG_TRACE(INFO, "EMM-PROC  - GUTI NOT NULL");

    if (ctx->guti == NULL) {
      ctx->guti = (GUTI_t *)malloc(sizeof(GUTI_t));
    }

    if (ctx->guti != NULL) {
      memcpy(ctx->guti, guti, sizeof(GUTI_t));
2653
    } else {
2654 2655 2656 2657 2658 2659 2660
      LOG_FUNC_RETURN (RETURNerror);
    }
  } else {
    if (ctx->guti == NULL) {
      ctx->guti = (GUTI_t *)calloc(1, sizeof(GUTI_t));
    }

2661
#warning "LG: We should assign the GUTI accordingly to the visited plmn id"
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695

    if ((ctx->guti != NULL) && (imsi)) {
      ctx->tac                         = mme_config.gummei.plmn_tac[0];
      ctx->guti->gummei.MMEcode        = mme_config.gummei.mmec[0];
      ctx->guti->gummei.MMEgid         = mme_config.gummei.mme_gid[0];
      ctx->guti->m_tmsi                = (uint32_t) ctx;

      mnc_length =  mme_config_find_mnc_length(
                      imsi->u.num.digit1,
                      imsi->u.num.digit2,
                      imsi->u.num.digit3,
                      imsi->u.num.digit4,
                      imsi->u.num.digit5,
                      imsi->u.num.digit6);

      if ((mnc_length == 2) || (mnc_length == 3)) {
        ctx->guti->gummei.plmn.MCCdigit1 = imsi->u.num.digit1;
        ctx->guti->gummei.plmn.MCCdigit2 = imsi->u.num.digit2;
        ctx->guti->gummei.plmn.MCCdigit3 = imsi->u.num.digit3;

        if (mnc_length == 2) {
          ctx->guti->gummei.plmn.MNCdigit1 = imsi->u.num.digit4;
          ctx->guti->gummei.plmn.MNCdigit2 = imsi->u.num.digit5;
          ctx->guti->gummei.plmn.MNCdigit3 = 15;
          LOG_TRACE(WARNING, "EMM-PROC  - Assign GUTI from IMSI %01X%01X%01X.%01X%01X.%04X.%02X.%08X to emm_data_context",
                    ctx->guti->gummei.plmn.MCCdigit1,
                    ctx->guti->gummei.plmn.MCCdigit2,
                    ctx->guti->gummei.plmn.MCCdigit3,
                    ctx->guti->gummei.plmn.MNCdigit1,
                    ctx->guti->gummei.plmn.MNCdigit2,
                    ctx->guti->gummei.MMEgid,
                    ctx->guti->gummei.MMEcode,
                    ctx->guti->m_tmsi
                   );
2696
        } else {
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
          ctx->guti->gummei.plmn.MNCdigit1 = imsi->u.num.digit5;
          ctx->guti->gummei.plmn.MNCdigit2 = imsi->u.num.digit6;
          ctx->guti->gummei.plmn.MNCdigit3 = imsi->u.num.digit4;
          LOG_TRACE(WARNING, "EMM-PROC  - Assign GUTI from IMSI %01X%01X%01X.%01X%01X%01X.%04X.%02X.%08X to emm_data_context",
                    ctx->guti->gummei.plmn.MCCdigit1,
                    ctx->guti->gummei.plmn.MCCdigit2,
                    ctx->guti->gummei.plmn.MCCdigit3,
                    ctx->guti->gummei.plmn.MNCdigit1,
                    ctx->guti->gummei.plmn.MNCdigit2,
                    ctx->guti->gummei.plmn.MNCdigit3,
                    ctx->guti->gummei.MMEgid,
                    ctx->guti->gummei.MMEcode,
                    ctx->guti->m_tmsi
                   );
2711
        }
2712 2713 2714 2715 2716 2717 2718 2719

        LOG_TRACE(WARNING, "EMM-PROC  - Set ctx->guti_is_new to emm_data_context");
        ctx->guti_is_new                 = TRUE;
      } else {
        LOG_FUNC_RETURN (RETURNerror);
      }
    } else {
      LOG_FUNC_RETURN (RETURNerror);
Cedric Roux's avatar
 
Cedric Roux committed
2720
    }
2721 2722 2723 2724 2725 2726
  }

  /* The IMSI if provided by the UE */
  if (imsi) {
    if (ctx->imsi == NULL) {
      ctx->imsi = (imsi_t *)malloc(sizeof(imsi_t));
Cedric Roux's avatar
 
Cedric Roux committed
2727
    }
2728 2729 2730 2731 2732

    if (ctx->imsi != NULL) {
      memcpy(ctx->imsi, imsi, sizeof(imsi_t));
    } else {
      LOG_FUNC_RETURN (RETURNerror);
Cedric Roux's avatar
 
Cedric Roux committed
2733
    }
2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
  }

  /* The IMEI if provided by the UE */
  if (imei) {
    if (ctx->imei == NULL) {
      ctx->imei = (imei_t *)malloc(sizeof(imei_t));
    }

    if (ctx->imei != NULL) {
      memcpy(ctx->imei, imei, sizeof(imei_t));
    } else {
      LOG_FUNC_RETURN (RETURNerror);
    }
  }

  /* The ESM message contained within the attach request */
2750 2751 2752 2753 2754
  if (esm_msg_pP->length > 0) {
    if (ctx->esm_msg.value != NULL) {
      free(ctx->esm_msg.value);
      ctx->esm_msg.value  = NULL;
      ctx->esm_msg.length = 0;
Cedric Roux's avatar
 
Cedric Roux committed
2755 2756
    }

2757 2758
    ctx->esm_msg.value = (uint8_t *)malloc(esm_msg_pP->length);

2759
    if (ctx->esm_msg.value != NULL) {
2760 2761 2762
      memcpy((char *)ctx->esm_msg.value,
              (char *)esm_msg_pP->value, esm_msg_pP->length);

2763 2764 2765 2766 2767
    } else {
      LOG_FUNC_RETURN (RETURNerror);
    }
  }

2768
  ctx->esm_msg.length = esm_msg_pP->length;
2769 2770 2771 2772
  /* Attachment indicator */
  ctx->is_attached = FALSE;

  LOG_FUNC_RETURN (RETURNok);
Cedric Roux's avatar
 
Cedric Roux committed
2773 2774 2775
}

#endif // NAS_MME