esm_main.c 18.8 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
/*******************************************************************************
    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.
Cedric Roux's avatar
 
Cedric Roux committed
15

Lionel Gauthier's avatar
GPLv3  
Lionel Gauthier committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.

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

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

 *******************************************************************************/
/*****************************************************************************
30
Source      esm_main.c
Cedric Roux's avatar
 
Cedric Roux committed
31

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

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

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

38
Subsystem   EPS Session Management
Cedric Roux's avatar
 
Cedric Roux committed
39

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

42 43
Description Defines the EPS Session Management procedure call manager,
        the main entry point for elementary ESM processing.
Cedric Roux's avatar
 
Cedric Roux committed
44 45 46 47 48 49 50

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

#include "esm_main.h"
#include "commonDef.h"
#include "nas_log.h"

51
#include "emmData.h"
Cedric Roux's avatar
 
Cedric Roux committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include "esmData.h"
#include "esm_pt.h"
#include "esm_ebr.h"

/****************************************************************************/
/****************  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  *******************/
/****************************************************************************/

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

#ifdef NAS_UE
/****************************************************************************
 **                                                                        **
71
 ** Name:    esm_main_initialize()                                     **
Cedric Roux's avatar
 
Cedric Roux committed
72 73 74
 **                                                                        **
 ** Description: Initializes ESM internal data                             **
 **                                                                        **
75 76
 ** Inputs:  cb:        The user notification callback             **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
77
 **                                                                        **
78 79 80
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
81 82 83 84 85 86
 **                                                                        **
 ***************************************************************************/
void esm_main_initialize(esm_indication_callback_t cb)
{
    LOG_FUNC_IN;

87 88
    int i;

Cedric Roux's avatar
 
Cedric Roux committed
89 90 91 92
    /* Total number of active EPS bearer contexts */
    _esm_data.n_ebrs = 0;
    /* List of active PDN connections */
    _esm_data.n_pdns = 0;
93
    for (i = 0; i < ESM_DATA_PDN_MAX + 1; i++) {
94 95 96
        _esm_data.pdn[i].pid = -1;
        _esm_data.pdn[i].is_active = FALSE;
        _esm_data.pdn[i].data = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }
    /* Emergency bearer services indicator */
    _esm_data.emergency = FALSE;

    /* Initialize the procedure transaction identity manager */
    esm_pt_initialize();

    /* Initialize the EPS bearer context manager */
    esm_ebr_initialize(cb);

    LOG_FUNC_OUT;
}
#endif
#ifdef NAS_MME
/****************************************************************************
 **                                                                        **
113
 ** Name:    esm_main_initialize()                                     **
Cedric Roux's avatar
 
Cedric Roux committed
114 115 116
 **                                                                        **
 ** Description: Initializes ESM internal data                             **
 **                                                                        **
117 118
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
119
 **                                                                        **
120 121 122
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
123 124 125 126 127 128 129 130 131 132
 **                                                                        **
 ***************************************************************************/
void esm_main_initialize(void)
{
    int i;

    LOG_FUNC_IN;

    /* Retreive MME supported configuration data */
    if (mme_api_get_esm_config(&_esm_data.conf) != RETURNok) {
133
        LOG_TRACE(ERROR, "ESM-MAIN  - Failed to get MME configuration data");
Cedric Roux's avatar
 
Cedric Roux committed
134
    }
135
# if !defined(EPC_BUILD)
Cedric Roux's avatar
 
Cedric Roux committed
136 137
    /* Initialize ESM contexts */
    for (i = 0; i < ESM_DATA_NB_UE_MAX; i++) {
138
        _esm_data.ctx[i] = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
139
    }
140
# endif
Cedric Roux's avatar
 
Cedric Roux committed
141 142 143 144 145 146 147 148 149 150

    /* Initialize the EPS bearer context manager */
    esm_ebr_initialize();

    LOG_FUNC_OUT;
}
#endif

/****************************************************************************
 **                                                                        **
151
 ** Name:        esm_main_cleanup()                                        **
Cedric Roux's avatar
 
Cedric Roux committed
152 153 154
 **                                                                        **
 ** Description: Performs the EPS Session Management clean up procedure    **
 **                                                                        **
155 156
 ** Inputs:      None                                                      **
 **                  Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
157
 **                                                                        **
158
 ** Outputs:     None                                                      **
159 160
 **                  Return:    None                                       **
 **                  Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
161 162 163 164 165 166
 **                                                                        **
 ***************************************************************************/
void esm_main_cleanup(void)
{
    LOG_FUNC_IN;

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#ifdef NAS_UE
    {
        int i;
        int pid;
        int bid;

        /* De-activate EPS bearers and clean up PDN connections */
        for (pid = 0; pid < ESM_DATA_PDN_MAX; pid++) {
            if (_esm_data.pdn[pid].data) {
                esm_pdn_t *pdn = _esm_data.pdn[pid].data;
                if (pdn->apn.length > 0) {
                    free(pdn->apn.value);
                }
                /* Release EPS bearer contexts */
                for (bid = 0; bid < pdn->n_bearers; bid++) {
                    if (pdn->bearer[bid]) {
                        LOG_TRACE(WARNING, "ESM-MAIN  - Release EPS bearer "
                                  "context (ebi=%d)", pdn->bearer[bid]->ebi);
                        /* Delete the TFT */
                        for (i = 0; i < pdn->bearer[bid]->tft.n_pkfs; i++) {
                            if (pdn->bearer[bid]->tft.pkf[i]) {
                                free(pdn->bearer[bid]->tft.pkf[i]);
                            }
190
                        }
191
                        free(pdn->bearer[bid]);
192 193
                    }
                }
194 195
                /* Release the PDN connection */
                free(_esm_data.pdn[pid].data);
196 197
            }
        }
Cedric Roux's avatar
 
Cedric Roux committed
198 199 200 201 202 203 204 205 206
    }
#endif

    LOG_FUNC_OUT;
}

#ifdef NAS_UE
/****************************************************************************
 **                                                                        **
207
 ** Name:    esm_main_get_nb_pdn_max()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
208 209
 **                                                                        **
 ** Description: Get the maximum number of PDN connections that may be in  **
210
 **      a defined state at the same time                          **
Cedric Roux's avatar
 
Cedric Roux committed
211
 **                                                                        **
212 213
 ** Inputs:  None                                                      **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
214
 **                                                                        **
215 216 217 218
 ** Outputs:     None                                                      **
 **      Return:    The maximum number of PDN connections that **
 **             may be defined                             **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
219 220 221 222 223 224 225 226 227 228 229
 **                                                                        **
 ***************************************************************************/
int esm_main_get_nb_pdns_max(void)
{
    LOG_FUNC_IN;

    LOG_FUNC_RETURN (ESM_DATA_PDN_MAX);
}

/****************************************************************************
 **                                                                        **
230
 ** Name:    esm_main_get_nb_pdns()                                    **
Cedric Roux's avatar
 
Cedric Roux committed
231 232 233
 **                                                                        **
 ** Description: Get the number of active PDN connections                  **
 **                                                                        **
234 235
 ** Inputs:  None                                                      **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
236
 **                                                                        **
237 238 239
 ** Outputs:     None                                                      **
 **      Return:    The number of active PDN connections       **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
240 241 242 243 244 245 246 247 248 249 250
 **                                                                        **
 ***************************************************************************/
int esm_main_get_nb_pdns(void)
{
    LOG_FUNC_IN;

    LOG_FUNC_RETURN (_esm_data.n_pdns);
}

/****************************************************************************
 **                                                                        **
251
 ** Name:    esm_main_has_emergency()                                  **
Cedric Roux's avatar
 
Cedric Roux committed
252 253
 **                                                                        **
 ** Description: Check whether a PDN connection for emergency bearer ser-  **
254
 **      vices is established                                      **
Cedric Roux's avatar
 
Cedric Roux committed
255
 **                                                                        **
256 257
 ** Inputs:  None                                                      **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
258
 **                                                                        **
259 260 261 262
 ** Outputs:     None                                                      **
 **      Return:    TRUE if a PDN connection for emergency     **
 **             bearer services is established             **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
263 264 265 266 267 268 269 270 271 272 273
 **                                                                        **
 ***************************************************************************/
int esm_main_has_emergency(void)
{
    LOG_FUNC_IN;

    LOG_FUNC_RETURN (_esm_data.emergency);
}

/****************************************************************************
 **                                                                        **
274
 ** Name:    esm_main_get_pdn_status()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
275 276 277
 **                                                                        **
 ** Description: Get the status of the specified PDN connection            **
 **                                                                        **
278 279
 ** Inputs:  cid:       PDN connection identifier                  **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
280
 **                                                                        **
281 282 283 284 285 286 287
 ** Outputs:     state:     TRUE if the current state of the PDN con-  **
 **             nection is ACTIVE; FALSE otherwise.        **
 **      Return:    TRUE if the specified PDN connection has a **
 **             PDN context defined; FALSE if no any PDN   **
 **             context has been defined for the specified **
 **             connection.                                **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
288 289
 **                                                                        **
 ***************************************************************************/
290
int esm_main_get_pdn_status(int cid, int *state)
Cedric Roux's avatar
 
Cedric Roux committed
291 292 293 294 295 296
{
    LOG_FUNC_IN;

    unsigned int pid = cid - 1;

    if (pid >= ESM_DATA_PDN_MAX) {
297 298 299 300 301 302 303 304
        return (FALSE);
    } else if (pid != _esm_data.pdn[pid].pid) {
        LOG_TRACE(WARNING, "ESM-MAIN  - PDN connection %d is not defined", cid);
        return (FALSE);
    } else if (_esm_data.pdn[pid].data == NULL) {
        LOG_TRACE(ERROR, "ESM-MAIN  - PDN connection %d has not been allocated",
                  cid);
        return (FALSE);
Cedric Roux's avatar
 
Cedric Roux committed
305 306 307
    }

    if (_esm_data.pdn[pid].data->bearer[0] != NULL) {
308 309 310 311
        /* The status of a PDN connection is the status of the default EPS bearer
         * that has been assigned to this PDN connection at activation time */
        int ebi = _esm_data.pdn[pid].data->bearer[0]->ebi;
        *state = (esm_ebr_get_status(ebi) == ESM_EBR_ACTIVE);
Cedric Roux's avatar
 
Cedric Roux committed
312 313 314 315 316 317 318
    }
    /* The PDN connection has not been activated yet */
    LOG_FUNC_RETURN (TRUE);
}

/****************************************************************************
 **                                                                        **
319
 ** Name:    esm_main_get_pdn()                                        **
Cedric Roux's avatar
 
Cedric Roux committed
320 321 322
 **                                                                        **
 ** Description: Get parameters defined for the specified PDN connection   **
 **                                                                        **
323 324
 ** Inputs:  cid:       PDN connection identifier                  **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
325
 **                                                                        **
326 327 328 329 330 331
 ** Outputs:     type:      PDN connection type (IPv4, IPv6, IPv4v6)   **
 **      apn:       Access Point logical Name in used          **
 **      is_emergency:  Emergency bearer services indicator        **
 **      is_active: Active PDN connection indicator            **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
332 333
 **                                                                        **
 ***************************************************************************/
334 335
int esm_main_get_pdn(int cid, int *type, const char **apn,
                     int *is_emergency, int *is_active)
Cedric Roux's avatar
 
Cedric Roux committed
336 337 338 339 340 341
{
    LOG_FUNC_IN;

    unsigned int pid = cid - 1;

    if (pid >= ESM_DATA_PDN_MAX) {
342 343 344 345 346 347 348 349
        return (RETURNerror);
    } else if (pid != _esm_data.pdn[pid].pid) {
        LOG_TRACE(WARNING, "ESM-MAIN  - PDN connection %d is not defined", cid);
        return (RETURNerror);
    } else if (_esm_data.pdn[pid].data == NULL) {
        LOG_TRACE(ERROR, "ESM-MAIN  - PDN connection %d has not been allocated",
                  cid);
        return (RETURNerror);
Cedric Roux's avatar
 
Cedric Roux committed
350 351 352 353 354 355
    }

    /* Get the PDN type */
    *type = _esm_data.pdn[pid].data->type;
    /* Get the Access Point Name */
    if (_esm_data.pdn[pid].data->apn.length > 0) {
356
        *apn = (char *)(_esm_data.pdn[pid].data->apn.value);
Cedric Roux's avatar
 
Cedric Roux committed
357
    } else {
358
        *apn = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
359 360 361 362 363 364 365 366 367 368 369
    }
    /* Get the emergency bearer services indicator */
    *is_emergency = _esm_data.pdn[pid].data->is_emergency;
    /* Get the active PDN connection indicator */
    *is_active = _esm_data.pdn[pid].is_active;

    LOG_FUNC_RETURN (RETURNok);
}

/****************************************************************************
 **                                                                        **
370
 ** Name:    esm_main_get_pdn_addr()                                   **
Cedric Roux's avatar
 
Cedric Roux committed
371 372
 **                                                                        **
 ** Description: Get IP address(es) assigned to the specified PDN connec-  **
373
 **      tion                                                      **
Cedric Roux's avatar
 
Cedric Roux committed
374
 **                                                                        **
375 376
 ** Inputs:  cid:       PDN connection identifier                  **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
377
 **                                                                        **
378 379 380 381
 ** Outputs:     ipv4adddr: IPv4 address                               **
 **      ipv6adddr: IPv6 address                               **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
382 383
 **                                                                        **
 ***************************************************************************/
384
int esm_main_get_pdn_addr(int cid, const char **ipv4addr, const char **ipv6addr)
Cedric Roux's avatar
 
Cedric Roux committed
385 386 387 388 389 390
{
    LOG_FUNC_IN;

    unsigned int pid = cid - 1;

    if (pid >= ESM_DATA_PDN_MAX) {
391 392 393 394 395 396 397 398 399 400 401
        return (RETURNerror);
    } else if (pid != _esm_data.pdn[pid].pid) {
        LOG_TRACE(WARNING, "ESM-MAIN  - PDN connection %d is not defined", cid);
        return (RETURNerror);
    } else if (_esm_data.pdn[pid].data == NULL) {
        LOG_TRACE(ERROR, "ESM-MAIN  - PDN connection %d has not been allocated",
                  cid);
        return (RETURNerror);
    } else if (!_esm_data.pdn[pid].is_active) {
        /* No any IP address has been assigned to this PDN connection */
        return (RETURNok);
Cedric Roux's avatar
 
Cedric Roux committed
402 403 404
    }

    if (_esm_data.pdn[pid].data->type == NET_PDN_TYPE_IPV4) {
405 406 407 408 409 410 411 412 413
        /* Get IPv4 address */
        *ipv4addr = _esm_data.pdn[pid].data->ip_addr;
    } else if (_esm_data.pdn[pid].data->type == NET_PDN_TYPE_IPV6) {
        /* Get IPv6 address */
        *ipv6addr = _esm_data.pdn[pid].data->ip_addr;
    } else {
        /* IPv4v6 dual-stack terminal */
        *ipv4addr = _esm_data.pdn[pid].data->ip_addr;
        *ipv6addr = _esm_data.pdn[pid].data->ip_addr+ESM_DATA_IPV4_ADDRESS_SIZE;
Cedric Roux's avatar
 
Cedric Roux committed
414 415 416 417 418 419 420 421 422 423 424
    }

    LOG_FUNC_RETURN (RETURNok);
}

#endif // NAS_UE

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