esm_main.c 17.4 KB
Newer Older
Cedric Roux's avatar
 
Cedric Roux committed
1
/*****************************************************************************
2 3
            Eurecom OpenAirInterface 3
            Copyright(c) 2012 Eurecom
Cedric Roux's avatar
 
Cedric Roux committed
4

5
Source      esm_main.c
Cedric Roux's avatar
 
Cedric Roux committed
6

7
Version     0.1
Cedric Roux's avatar
 
Cedric Roux committed
8

9
Date        2012/12/04
Cedric Roux's avatar
 
Cedric Roux committed
10

11
Product     NAS stack
Cedric Roux's avatar
 
Cedric Roux committed
12

13
Subsystem   EPS Session Management
Cedric Roux's avatar
 
Cedric Roux committed
14

15
Author      Frederic Maurel
Cedric Roux's avatar
 
Cedric Roux committed
16

17 18
Description Defines the EPS Session Management procedure call manager,
        the main entry point for elementary ESM processing.
Cedric Roux's avatar
 
Cedric Roux committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

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

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

#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
/****************************************************************************
 **                                                                        **
45
 ** Name:    esm_main_initialize()                                     **
Cedric Roux's avatar
 
Cedric Roux committed
46 47 48
 **                                                                        **
 ** Description: Initializes ESM internal data                             **
 **                                                                        **
49 50
 ** Inputs:  cb:        The user notification callback             **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
51
 **                                                                        **
52 53 54
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
55 56 57 58 59 60 61 62 63 64 65
 **                                                                        **
 ***************************************************************************/
void esm_main_initialize(esm_indication_callback_t cb)
{
    LOG_FUNC_IN;

    /* Total number of active EPS bearer contexts */
    _esm_data.n_ebrs = 0;
    /* List of active PDN connections */
    _esm_data.n_pdns = 0;
    for (int i = 0; i < ESM_DATA_PDN_MAX + 1; i++) {
66 67 68
        _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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    }
    /* 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
/****************************************************************************
 **                                                                        **
85
 ** Name:    esm_main_initialize()                                     **
Cedric Roux's avatar
 
Cedric Roux committed
86 87 88
 **                                                                        **
 ** Description: Initializes ESM internal data                             **
 **                                                                        **
89 90
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
91
 **                                                                        **
92 93 94
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
95 96 97 98 99 100 101 102 103 104
 **                                                                        **
 ***************************************************************************/
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) {
105
        LOG_TRACE(ERROR, "ESM-MAIN  - Failed to get MME configuration data");
Cedric Roux's avatar
 
Cedric Roux committed
106 107 108 109
    }

    /* Initialize ESM contexts */
    for (i = 0; i < ESM_DATA_NB_UE_MAX; i++) {
110
        _esm_data.ctx[i] = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
111 112 113 114 115 116 117 118 119 120 121
    }

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

    LOG_FUNC_OUT;
}
#endif

/****************************************************************************
 **                                                                        **
122
 ** Name:    esm_main_cleanup()                                        **
Cedric Roux's avatar
 
Cedric Roux committed
123 124 125
 **                                                                        **
 ** Description: Performs the EPS Session Management clean up procedure    **
 **                                                                        **
126 127
 ** Inputs:  None                                                      **
 **          Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
128
 **                                                                        **
129 130 131
 ** Outputs:     None                                                      **
 **          Return:    None                                       **
 **          Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
132 133 134 135 136 137 138 139 140
 **                                                                        **
 ***************************************************************************/
void esm_main_cleanup(void)
{
    LOG_FUNC_IN;

#ifdef NAS_UE
    /* De-activate EPS bearers and clean up PDN connections */
    for (int pid = 0; pid < ESM_DATA_PDN_MAX; pid++) {
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        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 (int 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 (int i = 0; i < pdn->bearer[bid]->tft.n_pkfs; i++) {
                        if (pdn->bearer[bid]->tft.pkf[i]) {
                            free(pdn->bearer[bid]->tft.pkf[i]);
                        }
                    }
                    free(pdn->bearer[bid]);
                }
            }
            /* Release the PDN connection */
            free(_esm_data.pdn[pid].data);
        }
Cedric Roux's avatar
 
Cedric Roux committed
163 164 165 166 167 168 169 170 171
    }
#endif

    LOG_FUNC_OUT;
}

#ifdef NAS_UE
/****************************************************************************
 **                                                                        **
172
 ** Name:    esm_main_get_nb_pdn_max()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
173 174
 **                                                                        **
 ** Description: Get the maximum number of PDN connections that may be in  **
175
 **      a defined state at the same time                          **
Cedric Roux's avatar
 
Cedric Roux committed
176
 **                                                                        **
177 178
 ** Inputs:  None                                                      **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
179
 **                                                                        **
180 181 182 183
 ** Outputs:     None                                                      **
 **      Return:    The maximum number of PDN connections that **
 **             may be defined                             **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
184 185 186 187 188 189 190 191 192 193 194
 **                                                                        **
 ***************************************************************************/
int esm_main_get_nb_pdns_max(void)
{
    LOG_FUNC_IN;

    LOG_FUNC_RETURN (ESM_DATA_PDN_MAX);
}

/****************************************************************************
 **                                                                        **
195
 ** Name:    esm_main_get_nb_pdns()                                    **
Cedric Roux's avatar
 
Cedric Roux committed
196 197 198
 **                                                                        **
 ** Description: Get the number of active PDN connections                  **
 **                                                                        **
199 200
 ** Inputs:  None                                                      **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
201
 **                                                                        **
202 203 204
 ** Outputs:     None                                                      **
 **      Return:    The number of active PDN connections       **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
205 206 207 208 209 210 211 212 213 214 215
 **                                                                        **
 ***************************************************************************/
int esm_main_get_nb_pdns(void)
{
    LOG_FUNC_IN;

    LOG_FUNC_RETURN (_esm_data.n_pdns);
}

/****************************************************************************
 **                                                                        **
216
 ** Name:    esm_main_has_emergency()                                  **
Cedric Roux's avatar
 
Cedric Roux committed
217 218
 **                                                                        **
 ** Description: Check whether a PDN connection for emergency bearer ser-  **
219
 **      vices is established                                      **
Cedric Roux's avatar
 
Cedric Roux committed
220
 **                                                                        **
221 222
 ** Inputs:  None                                                      **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
223
 **                                                                        **
224 225 226 227
 ** Outputs:     None                                                      **
 **      Return:    TRUE if a PDN connection for emergency     **
 **             bearer services is established             **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
228 229 230 231 232 233 234 235 236 237 238
 **                                                                        **
 ***************************************************************************/
int esm_main_has_emergency(void)
{
    LOG_FUNC_IN;

    LOG_FUNC_RETURN (_esm_data.emergency);
}

/****************************************************************************
 **                                                                        **
239
 ** Name:    esm_main_get_pdn_status()                                 **
Cedric Roux's avatar
 
Cedric Roux committed
240 241 242
 **                                                                        **
 ** Description: Get the status of the specified PDN connection            **
 **                                                                        **
243 244
 ** Inputs:  cid:       PDN connection identifier                  **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
245
 **                                                                        **
246 247 248 249 250 251 252
 ** 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
253 254
 **                                                                        **
 ***************************************************************************/
255
int esm_main_get_pdn_status(int cid, int *state)
Cedric Roux's avatar
 
Cedric Roux committed
256 257 258 259 260 261
{
    LOG_FUNC_IN;

    unsigned int pid = cid - 1;

    if (pid >= ESM_DATA_PDN_MAX) {
262 263 264 265 266 267 268 269
        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
270 271 272
    }

    if (_esm_data.pdn[pid].data->bearer[0] != NULL) {
273 274 275 276
        /* 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
277 278 279 280 281 282 283
    }
    /* The PDN connection has not been activated yet */
    LOG_FUNC_RETURN (TRUE);
}

/****************************************************************************
 **                                                                        **
284
 ** Name:    esm_main_get_pdn()                                        **
Cedric Roux's avatar
 
Cedric Roux committed
285 286 287
 **                                                                        **
 ** Description: Get parameters defined for the specified PDN connection   **
 **                                                                        **
288 289
 ** Inputs:  cid:       PDN connection identifier                  **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
290
 **                                                                        **
291 292 293 294 295 296
 ** 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
297 298
 **                                                                        **
 ***************************************************************************/
299 300
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
301 302 303 304 305 306
{
    LOG_FUNC_IN;

    unsigned int pid = cid - 1;

    if (pid >= ESM_DATA_PDN_MAX) {
307 308 309 310 311 312 313 314
        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
315 316 317 318 319 320
    }

    /* 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) {
321
        *apn = (char *)(_esm_data.pdn[pid].data->apn.value);
Cedric Roux's avatar
 
Cedric Roux committed
322
    } else {
323
        *apn = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
324 325 326 327 328 329 330 331 332 333 334
    }
    /* 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);
}

/****************************************************************************
 **                                                                        **
335
 ** Name:    esm_main_get_pdn_addr()                                   **
Cedric Roux's avatar
 
Cedric Roux committed
336 337
 **                                                                        **
 ** Description: Get IP address(es) assigned to the specified PDN connec-  **
338
 **      tion                                                      **
Cedric Roux's avatar
 
Cedric Roux committed
339
 **                                                                        **
340 341
 ** Inputs:  cid:       PDN connection identifier                  **
 **      Others:    _esm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
342
 **                                                                        **
343 344 345 346
 ** Outputs:     ipv4adddr: IPv4 address                               **
 **      ipv6adddr: IPv6 address                               **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
347 348
 **                                                                        **
 ***************************************************************************/
349
int esm_main_get_pdn_addr(int cid, const char **ipv4addr, const char **ipv6addr)
Cedric Roux's avatar
 
Cedric Roux committed
350 351 352 353 354 355
{
    LOG_FUNC_IN;

    unsigned int pid = cid - 1;

    if (pid >= ESM_DATA_PDN_MAX) {
356 357 358 359 360 361 362 363 364 365 366
        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
367 368 369
    }

    if (_esm_data.pdn[pid].data->type == NET_PDN_TYPE_IPV4) {
370 371 372 373 374 375 376 377 378
        /* 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
379 380 381 382 383 384 385 386 387 388 389
    }

    LOG_FUNC_RETURN (RETURNok);
}

#endif // NAS_UE

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