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

gauthier's avatar
 
gauthier committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*****************************************************************************
Source      Detach.c

Version     0.1

Date        2013/05/07

Product     NAS stack

Subsystem   EPS Mobility Management

Author      Frederic Maurel

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

        The detach procedure is used by the UE to detach for EPS servi-
        ces, to disconnect from the last PDN it is connected to; by the
        network to inform the UE that it is detached for EPS services
        or non-EPS services or both, to disconnect the UE from the last
        PDN to which it is connected and to inform the UE to re-attach
        to the network and re-establish all PDN connections.

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

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

#include "emmData.h"
52
#include "emm_timers.h"
gauthier's avatar
 
gauthier committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

#include "emm_sap.h"
#include "esm_sap.h"
#include "msc.h"

#include <stdlib.h> // free

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

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

/* String representation of the detach type */
static const char *_emm_detach_type_str[] = {
  "EPS", "IMSI", "EPS/IMSI",
  "RE-ATTACH REQUIRED", "RE-ATTACH NOT REQUIRED", "RESERVED"
};

/*
 * --------------------------------------------------------------------------
 *      Internal data handled by the detach procedure in the UE
 * --------------------------------------------------------------------------
 */

/*
 * Abnormal case detach procedures
 */
83
static int _emm_detach_abort(nas_user_t *user, emm_proc_detach_type_t type);
gauthier's avatar
 
gauthier committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

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

/*
 * --------------------------------------------------------------------------
 *          Detach procedure executed by the UE
 * --------------------------------------------------------------------------
 */
/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_detach()                                         **
 **                                                                        **
 ** Description: Initiates the detach procedure in order for the UE to de- **
 **      tach for EPS services.                                    **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.2.2.1                         **
 **      In state EMM-REGISTERED or EMM-REGISTERED-INITIATED, the  **
 **      UE initiates the detach procedure by sending a DETACH RE- **
 **      QUEST message to the network, starting timer T3421 and    **
 **      entering state EMM-DEREGISTERED-INITIATED.                **
 **                                                                        **
 ** Inputs:  type:      Type of the requested detach               **
 **      switch_off:    Indicates whether the detach is required   **
 **             because the UE is switched off or not      **
110
 **      Others:    user->emm_data->                                 **
gauthier's avatar
 
gauthier committed
111 112 113 114 115
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **                                                                        **
 ***************************************************************************/
116
int emm_proc_detach(nas_user_t *user, emm_proc_detach_type_t type, int switch_off)
gauthier's avatar
 
gauthier committed
117 118 119 120 121
{
  LOG_FUNC_IN;

  emm_sap_t emm_sap;
  emm_as_data_t *emm_as = &emm_sap.u.emm_as.u.data;
122
  emm_detach_data_t *emm_detach_data = user->emm_data->emm_detach_data;
gauthier's avatar
 
gauthier committed
123 124 125 126 127 128
  int rc;

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

  /* Initialize the detach procedure internal data */
129 130 131
  emm_detach_data->count = 0;
  emm_detach_data->switch_off = switch_off;
  emm_detach_data->type = type;
gauthier's avatar
 
gauthier committed
132 133 134

  /* Setup EMM procedure handler to be executed upon receiving
   * lower layer notification */
135
  rc = emm_proc_lowerlayer_initialize(user->lowerlayer_data, emm_proc_detach_request,
gauthier's avatar
 
gauthier committed
136
                                      emm_proc_detach_failure,
137
                                      emm_proc_detach_release, user);
gauthier's avatar
 
gauthier committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

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

  /* Setup NAS information message to transfer */
  emm_as->NASinfo = EMM_AS_NAS_INFO_DETACH;
  emm_as->NASmsg.length = 0;
  emm_as->NASmsg.value = NULL;
  /* Set the detach type */
  emm_as->type = type;
  /* Set the switch-off indicator */
  emm_as->switch_off = switch_off;
  /* Set the EPS mobile identity */
153
  emm_as->guti = user->emm_data->guti;
Frédéric Leroy's avatar
Frédéric Leroy committed
154
  emm_as->ueid = user->ueid;
gauthier's avatar
 
gauthier committed
155
  /* Setup EPS NAS security data */
156
  emm_as_set_security_data(&emm_as->sctx, user->emm_data->security, FALSE, TRUE);
gauthier's avatar
 
gauthier committed
157 158 159 160 161 162

  /*
   * Notify EMM-AS SAP that Detach Request message has to
   * be sent to the network
   */
  emm_sap.primitive = EMMAS_DATA_REQ;
163
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_detach_request()                                 **
 **                                                                        **
 ** Description: Performs the detach procedure upon receipt of indication  **
 **      from lower layers that Detach Request message has been    **
 **      successfully delivered to the network.                    **
 **                                                                        **
 ** Inputs:  args:      Not used                                   **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3421                                      **
 **                                                                        **
 ***************************************************************************/
int emm_proc_detach_request(void *args)
{
  LOG_FUNC_IN;

188
  nas_user_t *user = args;
189
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
190
  emm_detach_data_t *emm_detach_data = user->emm_data->emm_detach_data;
gauthier's avatar
 
gauthier committed
191 192 193
  emm_sap_t emm_sap;
  int rc;

194
  if ( !emm_detach_data->switch_off ) {
gauthier's avatar
 
gauthier committed
195
    /* Start T3421 timer */
196
    emm_timers->T3421.id = nas_timer_start(emm_timers->T3421.sec, emm_detach_t3421_handler, user);
gauthier's avatar
 
gauthier committed
197
    LOG_TRACE(INFO, "EMM-PROC  - Timer T3421 (%d) expires in %ld seconds",
198
              emm_timers->T3421.id, emm_timers->T3421.sec);
gauthier's avatar
 
gauthier committed
199 200 201 202 203 204
  }

  /*
   * Notify EMM that Detach Request has been sent to the network
   */
  emm_sap.primitive = EMMREG_DETACH_REQ;
205
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_detach_accept()                                  **
 **                                                                        **
 ** Description: Performs the UE initiated detach procedure for EPS servi- **
 **      ces only When the DETACH ACCEPT message is received from  **
 **      the network.                                              **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.2.2.2                         **
 **              Upon receiving the DETACH ACCEPT message, the UE shall    **
 **      stop timer T3421, locally deactivate all EPS bearer con-  **
 **      texts without peer-to-peer signalling and enter state EMM-**
 **      DEREGISTERED.                                             **
 **                                                                        **
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3421                                      **
 **                                                                        **
 ***************************************************************************/
232
int emm_proc_detach_accept(void* args)
gauthier's avatar
 
gauthier committed
233 234 235
{
  LOG_FUNC_IN;

236
  nas_user_t *user=args;
237
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
238 239 240 241 242
  int rc;

  LOG_TRACE(INFO, "EMM-PROC  - UE initiated detach procedure completion");

  /* Reset EMM procedure handler */
243
  (void) emm_proc_lowerlayer_initialize(user->lowerlayer_data, NULL, NULL, NULL, NULL);
gauthier's avatar
 
gauthier committed
244 245

  /* Stop timer T3421 */
246
  emm_timers->T3421.id = nas_timer_stop(emm_timers->T3421.id);
gauthier's avatar
 
gauthier committed
247 248 249 250 251 252 253

  /*
   * Notify ESM that all EPS bearer contexts have to be locally deactivated
   */
  esm_sap_t esm_sap;
  esm_sap.primitive = ESM_EPS_BEARER_CONTEXT_DEACTIVATE_REQ;
  esm_sap.data.eps_bearer_context_deactivate.ebi = ESM_SAP_ALL_EBI;
254
  rc = esm_sap_send(user, &esm_sap);
gauthier's avatar
 
gauthier committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

  /*
   * XXX - Upon receiving notification from ESM that all EPS bearer
   * contexts are locally deactivated, the UE is considered as
   * detached from the network and is entered state EMM-DEREGISTERED
   */
  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_detach_failure()                                 **
 **                                                                        **
 ** Description: Performs the detach procedure abnormal case upon receipt  **
 **          of transmission failure of Detach Request message.        **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.2.2.4, case h                 **
 **      The UE shall restart the detach procedure.                **
 **                                                                        **
 ** Inputs:  is_initial:    Not used                                   **
 **          args:      Not used                                   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
 **                                                                        **
 ***************************************************************************/
int emm_proc_detach_failure(int is_initial, void *args)
{
  LOG_FUNC_IN;

286 287
  nas_user_t *user = args;
  emm_detach_data_t *emm_detach_data = user->emm_data->emm_detach_data;
288
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
289 290 291 292 293 294
  emm_sap_t emm_sap;
  int rc;

  LOG_TRACE(WARNING, "EMM-PROC  - Network detach failure");

  /* Reset EMM procedure handler */
295
  (void) emm_proc_lowerlayer_initialize(user->lowerlayer_data, NULL, NULL, NULL, NULL);
gauthier's avatar
 
gauthier committed
296 297

  /* Stop timer T3421 */
298
  emm_timers->T3421.id = nas_timer_stop(emm_timers->T3421.id);
gauthier's avatar
 
gauthier committed
299 300 301 302 303

  /*
   * Notify EMM that detach procedure has to be restarted
   */
  emm_sap.primitive = EMMREG_DETACH_INIT;
304
  emm_sap.u.emm_reg.u.detach.switch_off = emm_detach_data->switch_off;
305
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

  LOG_FUNC_RETURN(rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:    emm_proc_detach_release()                                 **
 **                                                                        **
 ** Description: Performs the detach procedure abnormal case upon receipt  **
 **          of NAS signalling connection release indication before    **
 **      reception of Detach Accept message.                       **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.2.2.4, case b                 **
 **      The  detach procedure shall be aborted.                   **
 **                                                                        **
 ** Inputs:  args:      not used                                   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
 **                                                                        **
 ***************************************************************************/
int emm_proc_detach_release(void *args)
{
  LOG_FUNC_IN;

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

334 335
  nas_user_t *user = args;
  emm_detach_data_t *emm_detach_data = user->emm_data->emm_detach_data;
gauthier's avatar
 
gauthier committed
336
  /* Abort the detach procedure */
337
  int rc = _emm_detach_abort(user, emm_detach_data->type);
gauthier's avatar
 
gauthier committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

  LOG_FUNC_RETURN(rc);
}


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

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

/****************************************************************************
 **                                                                        **
 ** Name:    _emm_detach_t3421_handler()                               **
 **                                                                        **
 ** Description: T3421 timeout handler                                     **
 **                                                                        **
 **              3GPP TS 24.301, section 5.5.2.2.4 case c                  **
 **      On the first four expiries of the timer, the UE shall re- **
 **      transmit the DETACH REQUEST message and shall reset and   **
362
 **      restart timer emm_timers->T3421. On the fifth expiry of timer T3421,  **
gauthier's avatar
 
gauthier committed
363 364 365 366 367 368 369 370 371
 **      the detach procedure shall be aborted.                    **
 **                                                                        **
 ** Inputs:  args:      handler parameters                         **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    None                                       **
 **                                                                        **
 ***************************************************************************/
372
void *emm_detach_t3421_handler(void *args)
gauthier's avatar
 
gauthier committed
373 374 375
{
  LOG_FUNC_IN;

376 377
  nas_user_t *user = args;
  emm_detach_data_t *emm_detach_data = user->emm_data->emm_detach_data;
378
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
379 380 381
  int rc;

  /* Increment the retransmission counter */
382
  emm_detach_data->count += 1;
gauthier's avatar
 
gauthier committed
383 384

  LOG_TRACE(WARNING, "EMM-PROC  - T3421 timer expired, "
385
            "retransmission counter = %d", emm_detach_data->count);
gauthier's avatar
 
gauthier committed
386

387
  if (emm_detach_data->count < EMM_DETACH_COUNTER_MAX) {
gauthier's avatar
 
gauthier committed
388 389 390 391 392
    /* Retransmit the Detach Request message */
    emm_sap_t emm_sap;
    emm_as_data_t *emm_as = &emm_sap.u.emm_as.u.data;

    /* Stop timer T3421 */
393
    emm_timers->T3421.id = nas_timer_stop(emm_timers->T3421.id);
gauthier's avatar
 
gauthier committed
394 395 396 397 398 399

    /* Setup NAS information message to transfer */
    emm_as->NASinfo = EMM_AS_NAS_INFO_DETACH;
    emm_as->NASmsg.length = 0;
    emm_as->NASmsg.value = NULL;
    /* Set the detach type */
400
    emm_as->type = emm_detach_data->type;
gauthier's avatar
 
gauthier committed
401
    /* Set the switch-off indicator */
402
    emm_as->switch_off = emm_detach_data->switch_off;
gauthier's avatar
 
gauthier committed
403
    /* Set the EPS mobile identity */
404
    emm_as->guti = user->emm_data->guti;
Frédéric Leroy's avatar
Frédéric Leroy committed
405
    emm_as->ueid = user->ueid;
gauthier's avatar
 
gauthier committed
406
    /* Setup EPS NAS security data */
407
    emm_as_set_security_data(&emm_as->sctx, user->emm_data->security,
gauthier's avatar
 
gauthier committed
408 409 410 411 412 413 414
                             FALSE, TRUE);

    /*
     * Notify EMM-AS SAP that Detach Request message has to
     * be sent to the network
     */
    emm_sap.primitive = EMMAS_DATA_REQ;
415
    rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
416 417 418

    if (rc != RETURNerror) {
      /* Start T3421 timer */
419
      emm_timers->T3421.id = nas_timer_start(emm_timers->T3421.sec, emm_detach_t3421_handler, user);
gauthier's avatar
 
gauthier committed
420
      LOG_TRACE(INFO, "EMM-PROC  - Timer T3421 (%d) expires in %ld "
421
                "seconds", emm_timers->T3421.id, emm_timers->T3421.sec);
gauthier's avatar
 
gauthier committed
422 423 424
    }
  } else {
    /* Abort the detach procedure */
425
    rc = _emm_detach_abort(user, emm_detach_data->type);
gauthier's avatar
 
gauthier committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  }

  LOG_FUNC_RETURN(NULL);
}

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

/****************************************************************************
 **                                                                        **
 ** Name:    _emm_detach_abort()                                       **
 **                                                                        **
 ** Description: Aborts the detach procedure                               **
 **                                                                        **
 ** Inputs:  type:      not used                                   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    T3421                                      **
 **                                                                        **
 ***************************************************************************/
450
static int _emm_detach_abort(nas_user_t *user, emm_proc_detach_type_t type)
gauthier's avatar
 
gauthier committed
451 452 453
{
  LOG_FUNC_IN;

454
  emm_timers_t *emm_timers = user->emm_data->emm_timers;
gauthier's avatar
 
gauthier committed
455 456 457 458 459 460
  emm_sap_t emm_sap;
  int rc ;

  LOG_TRACE(WARNING, "EMM-PROC  - Abort the detach procedure");

  /* Reset EMM procedure handler */
461
  emm_proc_lowerlayer_initialize(user->lowerlayer_data, NULL, NULL, NULL, NULL);
gauthier's avatar
 
gauthier committed
462 463

  /* Stop timer T3421 */
464
  emm_timers->T3421.id = nas_timer_stop(emm_timers->T3421.id);
gauthier's avatar
 
gauthier committed
465 466 467 468 469 470

  /*
   * Notify EMM that detach procedure failed
   */
  emm_sap.primitive = EMMREG_DETACH_FAILED;
  emm_sap.u.emm_reg.u.detach.type = type;
471
  rc = emm_sap_send(user, &emm_sap);
gauthier's avatar
 
gauthier committed
472 473 474

  LOG_FUNC_RETURN (rc);
}