sgw_lite_handlers.c 34.6 KB
Newer Older
Cedric Roux's avatar
 
Cedric Roux 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
29
30
31
32
33
34
35
36
37
38
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2012 Eurecom

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope 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
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fr/openairinterface
  Address      : EURECOM, Campus SophiaTech, 450 Route des Chappes
                 06410 Biot FRANCE

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

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "assertions.h"
#include "common_types.h"
#include "intertask_interface.h"
39
#include "mme_config.h"
Cedric Roux's avatar
 
Cedric Roux committed
40
41
42
43
44
45
46
47
48
49
50
51
52
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
83
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
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
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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637

#include "sgw_lite_defs.h"
#include "sgw_lite_handlers.h"
#include "sgw_lite_context_manager.h"
#include "sgw_lite.h"

extern sgw_app_t sgw_app;

int sgw_lite_handle_create_session_request(SgwCreateSessionRequest *session_req_p)
{
    mme_sgw_tunnel_t*                          new_endpoint                                  = NULL;
    s_plus_p_gw_eps_bearer_context_information_t* s_plus_p_gw_eps_bearer_context_information = NULL;
    sgw_eps_bearer_entry_t*                    eps_bearer_entry_p                              = NULL;
    MessageDef*                                message_p                                     = NULL;

    /* Upon reception of create session request from MME,
     * S-GW should create UE, eNB and MME contexts and forward message to P-GW.
     */
    if (session_req_p->rat_type != RAT_EUTRAN) {
        SPGW_APP_WARNING("Received session request with RAT != RAT_TYPE_EUTRAN: type %d\n",
                    session_req_p->rat_type);
    }

    /* As we are abstracting GTP-C transport, FTeid ip address is useless.
     * We just use the teid to identify MME tunnel. Normally we received either:
     * - ipv4 address if ipv4 flag is set
     * - ipv6 address if ipv6 flag is set
     * - ipv4 and ipv6 if both flags are set
     * Communication between MME and S-GW involves S11 interface so we are expecting
     * S11_MME_GTP_C (11) as interface_type.
     */
    if ((session_req_p->sender_fteid_for_cp.teid == 0) &&
            (session_req_p->sender_fteid_for_cp.interface_type != S11_MME_GTP_C)) {
        /* MME sent request with teid = 0. This is not valid... */
        SPGW_APP_WARNING("F-TEID parameter mismatch\n");
        return -1;
    }
    new_endpoint = sgw_lite_cm_create_s11_tunnel(session_req_p->sender_fteid_for_cp.teid, sgw_lite_get_new_S11_tunnel_id());
    if (new_endpoint == NULL) {
        SPGW_APP_WARNING("Could not create new tunnel endpoint between S-GW and MME "
                    "for S11 abstraction\n");
        return -1;
    }

    SPGW_APP_DEBUG("Rx CREATE-SESSION-REQUEST MME S11 teid %u S-GW S11 teid %u APN %s EPS bearer Id %d\n", new_endpoint->remote_teid, new_endpoint->local_teid, session_req_p->apn, session_req_p->bearer_to_create.eps_bearer_id);
    SPGW_APP_DEBUG("                          IMSI %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",IMSI(&session_req_p->imsi));

    s_plus_p_gw_eps_bearer_context_information = sgw_lite_cm_create_bearer_context_information_in_collection(new_endpoint->local_teid);
    if (s_plus_p_gw_eps_bearer_context_information != NULL) {
        /* We try to create endpoint for S11 interface. A NULL endpoint means that
        * either the teid is already in list of known teid or ENOMEM error has been
        * raised during malloc.
        */

        //--------------------------------------------------
        // copy informations from create session request to bearer context information
        //--------------------------------------------------
        memcpy(s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.imsi.digit, session_req_p->imsi.digit, IMSI_DIGITS_MAX);
        memcpy(s_plus_p_gw_eps_bearer_context_information->pgw_eps_bearer_context_information.imsi.digit, session_req_p->imsi.digit, IMSI_DIGITS_MAX);
        s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.imsi_unauthenticated_indicator = 1;
        s_plus_p_gw_eps_bearer_context_information->pgw_eps_bearer_context_information.imsi_unauthenticated_indicator = 1;

        s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.mme_teid_for_S11               = session_req_p->sender_fteid_for_cp.teid;
        s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.s_gw_teid_for_S11_S4           = new_endpoint->local_teid;
        s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.trxn                           = session_req_p->trxn;
        s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.peer_ip                        = session_req_p->peer_ip;
        // may use ntohl or reverse, will see

        FTEID_T_2_IP_ADDRESS_T((&session_req_p->sender_fteid_for_cp) , (&s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.mme_ip_address_for_S11));

        //--------------------------------------
        // PDN connection
        //--------------------------------------
        /*pdn_connection = sgw_lite_cm_create_pdn_connection();

        if (pdn_connection == NULL) {
            // Malloc failed, may be ENOMEM error
            SPGW_APP_ERROR("Failed to create new PDN connection\n");
            return -1;
        }*/
        memset(&s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.pdn_connection, 0, sizeof(sgw_pdn_connection_t));
        s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.pdn_connection.sgw_eps_bearers = hashtbl_create(12, NULL, NULL);
        if ( s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.pdn_connection.sgw_eps_bearers == NULL) {
            SPGW_APP_ERROR("Failed to create eps bearers collection object\n");
            DevMessage("Failed to create eps bearers collection object\n");
            return -1;
        }
        
        if (session_req_p->apn) {
        	s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.pdn_connection.apn_in_use     =  strdup(session_req_p->apn);
        } else {
        	s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.pdn_connection.apn_in_use     = "NO APN";
        }
        s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.pdn_connection.default_bearer =  session_req_p->bearer_to_create.eps_bearer_id;

        //obj_hashtbl_insert(s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.pdn_connections, pdn_connection->apn_in_use, strlen(pdn_connection->apn_in_use), pdn_connection);
        //--------------------------------------
        // EPS bearer entry
        //--------------------------------------
        eps_bearer_entry_p = sgw_lite_cm_create_eps_bearer_entry_in_collection(
        		s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_information.pdn_connection.sgw_eps_bearers,
        		session_req_p->bearer_to_create.eps_bearer_id);

        sgw_lite_display_s11teid2mme_mappings();
        sgw_lite_display_s11_bearer_context_information_mapping();

        if (eps_bearer_entry_p == NULL) {
            SPGW_APP_ERROR("Failed to create new EPS bearer entry\n");
            // TO DO FREE new_bearer_context_information_p and by cascade...
            return -1;

        }
        eps_bearer_entry_p->eps_bearer_qos =  session_req_p->bearer_to_create.bearer_level_qos;


        //s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_informationteid = teid;

        /* Trying to insert the new tunnel into the tree.
         * If collision_p is not NULL (0), it means tunnel is already present.
         */

        //s_plus_p_gw_eps_bearer_context_information->sgw_eps_bearer_context_informations_gw_ip_address_for_S11_S4 =

        /* Establishing EPS bearer. Requesting S1-U (GTPV1-U) task to create a
        * tunnel for S1 user plane interface. If status in response is successfull (0),
        * the tunnel endpoint is locally ready.
        */
        message_p = alloc_new_message(TASK_SPGW_APP, GTPV1U_CREATE_TUNNEL_REQ);
        if (message_p == NULL) {
        	sgw_lite_cm_remove_s11_tunnel(new_endpoint->remote_teid);
            return -1;
        }

        message_p->msg.gtpv1uCreateTunnelReq.context_teid  = new_endpoint->local_teid;
        message_p->msg.gtpv1uCreateTunnelReq.eps_bearer_id = session_req_p->bearer_to_create.eps_bearer_id;
        SPGW_APP_DEBUG("Tx GTPV1U_CREATE_TUNNEL_REQ -> TASK_GTPV1_U, Context: S-GW S11 teid %u eps bearer id %d (from session req)\n",
        		message_p->msg.gtpv1uCreateTunnelReq.context_teid,
        		message_p->msg.gtpv1uCreateTunnelReq.eps_bearer_id);
        return send_msg_to_task(TASK_GTPV1_U, INSTANCE_DEFAULT, message_p);
    } else {
        SPGW_APP_WARNING("Could not create new transaction for SESSION_CREATE message\n");
        free(new_endpoint);
        return -1;
    }
}

int sgw_lite_handle_sgi_endpoint_created(SGICreateEndpointResp *resp_p)
{
    task_id_t to_task;

    SgwCreateSessionResponse                     *create_session_response_p        = NULL;
    s_plus_p_gw_eps_bearer_context_information_t *new_bearer_context_information_p = NULL;
    MessageDef                                   *message_p                        = NULL;
    hashtbl_rc_t                                  hash_rc;

    SPGW_APP_DEBUG("Rx SGI_CREATE_ENDPOINT_RESPONSE,Context: S11 teid %u, SGW S1U teid %u EPS bearer id %u\n",
    		resp_p->context_teid, resp_p->sgw_S1u_teid, resp_p->eps_bearer_id);
    hash_rc = hashtbl_get(sgw_app.s11_bearer_context_information_hashtable, resp_p->context_teid, (void**)&new_bearer_context_information_p);

#if defined(ENABLE_STANDALONE_EPC)
    to_task = TASK_MME_APP;
#else
    to_task = TASK_S11;
#endif

    message_p = alloc_new_message(TASK_SPGW_APP, SGW_CREATE_SESSION_RESPONSE);

    if (message_p == NULL) {
        return -1;
    }

    create_session_response_p = &message_p->msg.sgwCreateSessionResponse;

    if (hash_rc == HASH_TABLE_OK) {
        create_session_response_p->teid = new_bearer_context_information_p->sgw_eps_bearer_context_information.mme_teid_for_S11;

        /* Preparing to send create session response on S11 abstraction interface.
         *  we set the cause value regarding the S1-U bearer establishment result status.
         */
        if (resp_p->status == 0) {
            uint32_t address = sgw_app.sgw_ip_address_for_S1u_S12_S4_up;
            create_session_response_p->bearer_context_created.s1u_sgw_fteid.teid           = resp_p->sgw_S1u_teid;
            create_session_response_p->bearer_context_created.s1u_sgw_fteid.interface_type = S1_U_SGW_GTP_U;
            create_session_response_p->bearer_context_created.s1u_sgw_fteid.ipv4           = 1;
            /* Should be filled in with S-GW S1-U local address. Running everything on localhost for now */
            create_session_response_p->bearer_context_created.s1u_sgw_fteid.ipv4_address   = address;

            /* Set the Cause information from bearer context created.
             * "Request accepted" is returned when the GTPv2 entity has accepted a control plane request.
             */
            create_session_response_p->cause                        = REQUEST_ACCEPTED;
            create_session_response_p->bearer_context_created.cause = REQUEST_ACCEPTED;
        } else {
            create_session_response_p->cause                        = M_PDN_APN_NOT_ALLOWED;
            create_session_response_p->bearer_context_created.cause = M_PDN_APN_NOT_ALLOWED;
        }
        create_session_response_p->s11_sgw_teid.teid                    = resp_p->context_teid;
        create_session_response_p->bearer_context_created.eps_bearer_id = resp_p->eps_bearer_id;
        create_session_response_p->trxn                                 = new_bearer_context_information_p->sgw_eps_bearer_context_information.trxn;
        create_session_response_p->peer_ip                              = new_bearer_context_information_p->sgw_eps_bearer_context_information.peer_ip;
    } else {
        create_session_response_p->cause                        = CONTEXT_NOT_FOUND;
        create_session_response_p->bearer_context_created.cause = CONTEXT_NOT_FOUND;
    }

    SPGW_APP_DEBUG("Tx CREATE-SESSION-RESPONSE MME -> %s, teid %u S-GW teid %u S1U teid %u EPS bearer id %u status %d\n",
                   to_task == TASK_MME_APP ? "TASK_MME_APP" : "TASK_S11",
                   create_session_response_p->teid,
                   create_session_response_p->s11_sgw_teid.teid,
                   create_session_response_p->bearer_context_created.s1u_sgw_fteid.teid,
                   create_session_response_p->bearer_context_created.eps_bearer_id,
                   create_session_response_p->bearer_context_created.cause);
    return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
}

int sgw_lite_handle_gtpv1uCreateTunnelResp(Gtpv1uCreateTunnelResp *endpoint_created_p)
{
    task_id_t to_task;

    SgwCreateSessionResponse                          *create_session_response_p        = NULL;
    s_plus_p_gw_eps_bearer_context_information_t      *new_bearer_context_information_p = NULL;
    SGICreateEndpointReq                              *sgi_create_endpoint_req_p        = NULL;
    MessageDef                                        *message_p                        = NULL;
    sgw_eps_bearer_entry_t                            *eps_bearer_entry_p               = NULL;
    hashtbl_rc_t                                       hash_rc;

#if defined(ENABLE_STANDALONE_EPC)
    to_task = TASK_MME_APP;
#else
    to_task = TASK_S11;
#endif

    SPGW_APP_DEBUG("Rx GTPV1U_CREATE_TUNNEL_RESP, Context S-GW S11 teid %u, S-GW S1U teid %u EPS bearer id %u status %d\n",
                   endpoint_created_p->context_teid,
                   endpoint_created_p->S1u_teid,
                   endpoint_created_p->eps_bearer_id,
                   endpoint_created_p->status);
    hash_rc = hashtbl_get(sgw_app.s11_bearer_context_information_hashtable, endpoint_created_p->context_teid, (void**)&new_bearer_context_information_p);

    if (hash_rc == HASH_TABLE_OK) {
        hash_rc = hashtbl_get (new_bearer_context_information_p->sgw_eps_bearer_context_information.pdn_connection.sgw_eps_bearers, endpoint_created_p->eps_bearer_id, (void**)&eps_bearer_entry_p);

        DevAssert(hash_rc == HASH_TABLE_OK);

        SPGW_APP_DEBUG("Updated eps_bearer_entry_p eps_b_id %u with SGW S1U teid %u\n", endpoint_created_p->eps_bearer_id, endpoint_created_p->S1u_teid);
        eps_bearer_entry_p->s_gw_teid_for_S1u_S12_S4_up = endpoint_created_p->S1u_teid;

        sgw_lite_display_s11_bearer_context_information_mapping();

        /* SEND IP_FW_CREATE_IP_ENDPOINT_REQUEST to FW_IP task */
        message_p = alloc_new_message(TASK_SPGW_APP, SGI_CREATE_ENDPOINT_REQUEST);
        if (message_p == NULL) {
            return -1;
        }
        sgi_create_endpoint_req_p = &message_p->msg.sgiCreateEndpointReq;
        // IP forward will forward packets to this teid
        sgi_create_endpoint_req_p->context_teid  = endpoint_created_p->context_teid;
        sgi_create_endpoint_req_p->sgw_S1u_teid  = endpoint_created_p->S1u_teid;
        sgi_create_endpoint_req_p->eps_bearer_id = endpoint_created_p->eps_bearer_id;

        //create_sgi_endpoint_req_p->pdn_type = new_bearer_context_information_p->
        // TO DO TFT, QOS
        return send_msg_to_task(TASK_FW_IP, INSTANCE_DEFAULT, message_p);
    } else {
        SPGW_APP_DEBUG("Rx SGW_S1U_ENDPOINT_CREATED, Context: teid %u NOT FOUND\n", endpoint_created_p->context_teid);
        message_p = alloc_new_message(TASK_SPGW_APP, SGW_CREATE_SESSION_RESPONSE);
        if (message_p == NULL) {
            return -1;
        }
        create_session_response_p = &message_p->msg.sgwCreateSessionResponse;
        memset(create_session_response_p, 0, sizeof(SgwCreateSessionResponse));
        create_session_response_p->cause                        = CONTEXT_NOT_FOUND;
        create_session_response_p->bearer_context_created.cause = CONTEXT_NOT_FOUND;
        return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
    }
}

int sgw_lite_handle_gtpv1uUpdateTunnelResp(Gtpv1uUpdateTunnelResp *endpoint_updated_p)
{
    SgwModifyBearerResponse                           *modify_response_p                = NULL;
    SGIUpdateEndpointReq                              *update_request_p                 = NULL;
    s_plus_p_gw_eps_bearer_context_information_t      *new_bearer_context_information_p = NULL;
    MessageDef                                        *message_p                        = NULL;
    sgw_eps_bearer_entry_t                            *eps_bearer_entry_p               = NULL;
    hashtbl_rc_t                                       hash_rc;
    task_id_t                                          to_task;

#if defined(ENABLE_STANDALONE_EPC)
    to_task = TASK_MME_APP;
#else
    to_task = TASK_S11;
#endif

    SPGW_APP_DEBUG("Rx GTPV1U_UPDATE_TUNNEL_RESP, Context teid %u, SGW S1U teid %u, eNB S1U teid %u, EPS bearer id %u, status %d\n",
                   endpoint_updated_p->context_teid,
                   endpoint_updated_p->sgw_S1u_teid,
                   endpoint_updated_p->enb_S1u_teid,
                   endpoint_updated_p->eps_bearer_id,
                   endpoint_updated_p->status);


    hash_rc = hashtbl_get(sgw_app.s11_bearer_context_information_hashtable, endpoint_updated_p->context_teid, (void**)&new_bearer_context_information_p);

    if (hash_rc == HASH_TABLE_OK) {
    	hash_rc = hashtbl_get (new_bearer_context_information_p->sgw_eps_bearer_context_information.pdn_connection.sgw_eps_bearers, endpoint_updated_p->eps_bearer_id, (void**)&eps_bearer_entry_p);

        if ((hash_rc == HASH_TABLE_KEY_NOT_EXISTS) || (hash_rc == HASH_TABLE_BAD_PARAMETER_HASHTABLE)) {
            SPGW_APP_DEBUG("Sending SGW_MODIFY_BEARER_RESPONSE trxn %p bearer %u CONTEXT_NOT_FOUND (sgw_eps_bearers)\n",
                    new_bearer_context_information_p->sgw_eps_bearer_context_information.trxn,
                    endpoint_updated_p->eps_bearer_id);

            message_p = alloc_new_message(TASK_SPGW_APP, SGW_MODIFY_BEARER_RESPONSE);
            if (message_p == NULL) {
                return -1;
            }
            modify_response_p = &message_p->msg.sgwModifyBearerResponse;
            memset(modify_response_p, 0, sizeof(SgwModifyBearerResponse));
            modify_response_p->present                                      = MODIFY_BEARER_RESPONSE_REM;
            modify_response_p->choice.bearer_for_removal.eps_bearer_id      = endpoint_updated_p->eps_bearer_id;
            modify_response_p->choice.bearer_for_removal.cause              = CONTEXT_NOT_FOUND;
            modify_response_p->cause                                        = CONTEXT_NOT_FOUND;
            modify_response_p->trxn                                         = new_bearer_context_information_p->sgw_eps_bearer_context_information.trxn;

            return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
        } else if (hash_rc == HASH_TABLE_OK) {

            message_p = alloc_new_message(TASK_SPGW_APP, SGI_UPDATE_ENDPOINT_REQUEST);
            if (message_p == NULL) {
                return -1;
            }
            update_request_p = &message_p->msg.sgiUpdateEndpointReq;
            memset(update_request_p, 0, sizeof(SGIUpdateEndpointReq));
            update_request_p->context_teid = endpoint_updated_p->context_teid;
            update_request_p->sgw_S1u_teid = endpoint_updated_p->sgw_S1u_teid;
            update_request_p->enb_S1u_teid = endpoint_updated_p->enb_S1u_teid;
            update_request_p->eps_bearer_id = endpoint_updated_p->eps_bearer_id;

            return send_msg_to_task(TASK_FW_IP, INSTANCE_DEFAULT, message_p);
        }
    } else {
        SPGW_APP_DEBUG("Sending SGW_MODIFY_BEARER_RESPONSE trxn %p bearer %u CONTEXT_NOT_FOUND (s11_bearer_context_information_hashtable)\n",
                new_bearer_context_information_p->sgw_eps_bearer_context_information.trxn,
                endpoint_updated_p->eps_bearer_id);

        message_p = alloc_new_message(TASK_SPGW_APP, SGW_MODIFY_BEARER_RESPONSE);
        if (message_p == NULL) {
            return -1;
        }
        modify_response_p = &message_p->msg.sgwModifyBearerResponse;
        memset(modify_response_p, 0, sizeof(SgwModifyBearerResponse));

        modify_response_p->present                                      = MODIFY_BEARER_RESPONSE_REM;
        modify_response_p->choice.bearer_for_removal.eps_bearer_id      = endpoint_updated_p->eps_bearer_id;
        modify_response_p->choice.bearer_for_removal.cause              = CONTEXT_NOT_FOUND;
        modify_response_p->cause                                        = CONTEXT_NOT_FOUND;
        modify_response_p->trxn                                         = new_bearer_context_information_p->sgw_eps_bearer_context_information.trxn;
        return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
    }
    return -1;
}

int sgw_lite_handle_sgi_endpoint_updated(SGIUpdateEndpointResp *resp_p)
{
    SgwModifyBearerResponse                           *modify_response_p              = NULL;
    s_plus_p_gw_eps_bearer_context_information_t      *new_bearer_context_information_p = NULL;
    MessageDef                                        *message_p                      = NULL;
    sgw_eps_bearer_entry_t                            *eps_bearer_entry_p               = NULL;
    hashtbl_rc_t                                       hash_rc;
    task_id_t                                          to_task;

#if defined(ENABLE_STANDALONE_EPC)
    to_task = TASK_MME_APP;
#else
    to_task = TASK_S11;
#endif

    SPGW_APP_DEBUG("Rx SGI_UPDATE_ENDPOINT_RESPONSE, Context teid %u, SGW S1U teid %u, eNB S1U teid %u, EPS bearer id %u, status %d\n",
                   resp_p->context_teid,
                   resp_p->sgw_S1u_teid,
                   resp_p->enb_S1u_teid,
                   resp_p->eps_bearer_id,
                   resp_p->status);

    message_p = alloc_new_message(TASK_SPGW_APP, SGW_MODIFY_BEARER_RESPONSE);
    if (message_p == NULL) {
        return -1;
    }
    modify_response_p = &message_p->msg.sgwModifyBearerResponse;
    memset(modify_response_p, 0, sizeof(SgwModifyBearerResponse));

    hash_rc = hashtbl_get(sgw_app.s11_bearer_context_information_hashtable, resp_p->context_teid, (void**)&new_bearer_context_information_p);


    if (hash_rc == HASH_TABLE_OK) {
        hash_rc = hashtbl_get (new_bearer_context_information_p->sgw_eps_bearer_context_information.pdn_connection.sgw_eps_bearers,
                               resp_p->eps_bearer_id,
                               (void**)&eps_bearer_entry_p);

        if ((hash_rc == HASH_TABLE_KEY_NOT_EXISTS) || (hash_rc == HASH_TABLE_BAD_PARAMETER_HASHTABLE)) {
            SPGW_APP_DEBUG("Rx SGI_UPDATE_ENDPOINT_RESPONSE: CONTEXT_NOT_FOUND (pdn_connection.sgw_eps_bearers context)\n");
            modify_response_p->teid                                         = resp_p->context_teid; // TO BE CHECKED IF IT IS THIS TEID
            modify_response_p->present                                      = MODIFY_BEARER_RESPONSE_REM;
            modify_response_p->choice.bearer_for_removal.eps_bearer_id      = resp_p->eps_bearer_id;
            modify_response_p->choice.bearer_for_removal.cause              = CONTEXT_NOT_FOUND;
            modify_response_p->cause                                        = CONTEXT_NOT_FOUND;
            modify_response_p->trxn                                         = 0;

            return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
        } else if (hash_rc == HASH_TABLE_OK) {
            SPGW_APP_DEBUG("Rx SGI_UPDATE_ENDPOINT_RESPONSE: REQUEST_ACCEPTED\n");

            // accept anyway
            modify_response_p->teid                                         = resp_p->context_teid; // TO BE CHECKED IF IT IS THIS TEID
            modify_response_p->present                              = MODIFY_BEARER_RESPONSE_MOD;
            modify_response_p->choice.bearer_modified.eps_bearer_id = resp_p->eps_bearer_id;
            modify_response_p->choice.bearer_modified.cause         = REQUEST_ACCEPTED;
            modify_response_p->cause                                = REQUEST_ACCEPTED;
            modify_response_p->trxn                                 = new_bearer_context_information_p->sgw_eps_bearer_context_information.trxn;
        }
        return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
    } else {
        SPGW_APP_DEBUG("Rx SGI_UPDATE_ENDPOINT_RESPONSE: CONTEXT_NOT_FOUND (S11 context)\n");

        modify_response_p->teid                                         = resp_p->context_teid; // TO BE CHECKED IF IT IS THIS TEID
        modify_response_p->present                                      = MODIFY_BEARER_RESPONSE_REM;
        modify_response_p->choice.bearer_for_removal.eps_bearer_id      = resp_p->eps_bearer_id;
        modify_response_p->choice.bearer_for_removal.cause              = CONTEXT_NOT_FOUND;
        modify_response_p->cause                                        = CONTEXT_NOT_FOUND;
        modify_response_p->trxn                                         = 0;

        return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
    }
}

int sgw_lite_handle_modify_bearer_request(SgwModifyBearerRequest *modify_bearer_p)
{
    SgwModifyBearerResponse                           *modify_response_p                = NULL;
    Gtpv1uUpdateTunnelReq                             *gtpv1u_update_tunnel_req_p       = NULL;
    s_plus_p_gw_eps_bearer_context_information_t      *new_bearer_context_information_p = NULL;
    MessageDef                                        *message_p                        = NULL;
    sgw_eps_bearer_entry_t                            *eps_bearer_entry_p               = NULL;
    hashtbl_rc_t                                       hash_rc;
    task_id_t                                          to_task;

#if defined(ENABLE_STANDALONE_EPC)
    to_task = TASK_MME_APP;
#else
    to_task = TASK_S11;
#endif

    SPGW_APP_DEBUG("Rx MODIFY_BEARER_REQUEST, teid %u, EPS bearer id %u\n",
                   modify_bearer_p->teid,
                   modify_bearer_p->bearer_context_to_modify.eps_bearer_id);

    sgw_lite_display_s11teid2mme_mappings();
    sgw_lite_display_s11_bearer_context_information_mapping();

    hash_rc = hashtbl_get(sgw_app.s11_bearer_context_information_hashtable, modify_bearer_p->teid, (void**)&new_bearer_context_information_p);

    if (hash_rc == HASH_TABLE_OK) {

        new_bearer_context_information_p->sgw_eps_bearer_context_information.pdn_connection.default_bearer = modify_bearer_p->bearer_context_to_modify.eps_bearer_id;
        new_bearer_context_information_p->sgw_eps_bearer_context_information.trxn = modify_bearer_p->trxn;

        hash_rc = hashtbl_is_key_exists (new_bearer_context_information_p->sgw_eps_bearer_context_information.pdn_connection.sgw_eps_bearers, modify_bearer_p->bearer_context_to_modify.eps_bearer_id);

        if (hash_rc == HASH_TABLE_KEY_NOT_EXISTS) {
            message_p = alloc_new_message(TASK_SPGW_APP, SGW_MODIFY_BEARER_RESPONSE);
            if (message_p == NULL) {
                return -1;
            }
            modify_response_p = &message_p->msg.sgwModifyBearerResponse;
            memset(modify_response_p, 0, sizeof(SgwModifyBearerResponse));
            modify_response_p->present                                      = MODIFY_BEARER_RESPONSE_REM;
            modify_response_p->choice.bearer_for_removal.eps_bearer_id      = modify_bearer_p->bearer_context_to_modify.eps_bearer_id;
            modify_response_p->choice.bearer_for_removal.cause              = CONTEXT_NOT_FOUND;
            modify_response_p->cause                                        = CONTEXT_NOT_FOUND;
            modify_response_p->trxn                                         = modify_bearer_p->trxn;
            return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
        } else if (hash_rc == HASH_TABLE_OK) {
            // TO DO
            hash_rc = hashtbl_get (new_bearer_context_information_p->sgw_eps_bearer_context_information.pdn_connection.sgw_eps_bearers, modify_bearer_p->bearer_context_to_modify.eps_bearer_id, (void**)&eps_bearer_entry_p);
            FTEID_T_2_IP_ADDRESS_T( (&modify_bearer_p->bearer_context_to_modify.s1_eNB_fteid) , (&eps_bearer_entry_p->enb_ip_address_for_S1u) );
            eps_bearer_entry_p->enb_teid_for_S1u = modify_bearer_p->bearer_context_to_modify.s1_eNB_fteid.teid;

            // UPDATE GTPV1U mapping tables with eNB references (teid, addresses)
            message_p = alloc_new_message(TASK_SPGW_APP, GTPV1U_UPDATE_TUNNEL_REQ);
            if (message_p == NULL) {
                return -1;
            }
            gtpv1u_update_tunnel_req_p = &message_p->msg.gtpv1uUpdateTunnelReq;
            memset(gtpv1u_update_tunnel_req_p, 0, sizeof(Gtpv1uUpdateTunnelReq));
            gtpv1u_update_tunnel_req_p->context_teid           = modify_bearer_p->teid;
            gtpv1u_update_tunnel_req_p->sgw_S1u_teid           = eps_bearer_entry_p->s_gw_teid_for_S1u_S12_S4_up;     ///< SGW S1U local Tunnel Endpoint Identifier
            gtpv1u_update_tunnel_req_p->enb_S1u_teid           = eps_bearer_entry_p->enb_teid_for_S1u;                ///< eNB S1U Tunnel Endpoint Identifier
            gtpv1u_update_tunnel_req_p->enb_ip_address_for_S1u = eps_bearer_entry_p->enb_ip_address_for_S1u;
            gtpv1u_update_tunnel_req_p->eps_bearer_id          = eps_bearer_entry_p->eps_bearer_id;
            SPGW_APP_DEBUG("Rx MODIFY_BEARER_REQUEST, gtpv1u_update_tunnel_req_p->context_teid           = %u\n",modify_bearer_p->teid);
            SPGW_APP_DEBUG("Rx MODIFY_BEARER_REQUEST, gtpv1u_update_tunnel_req_p->sgw_S1u_teid           = %u\n",gtpv1u_update_tunnel_req_p->sgw_S1u_teid);
            SPGW_APP_DEBUG("Rx MODIFY_BEARER_REQUEST, gtpv1u_update_tunnel_req_p->enb_S1u_teid           = %u\n",gtpv1u_update_tunnel_req_p->enb_S1u_teid);
            //SPGW_APP_DEBUG("Rx MODIFY_BEARER_REQUEST, gtpv1u_update_tunnel_req_p->enb_ip_address_for_S1u = %u\n",modify_bearer_p->enb_ip_address_for_S1u);
            SPGW_APP_DEBUG("Rx MODIFY_BEARER_REQUEST, gtpv1u_update_tunnel_req_p->eps_bearer_id          = %u\n",gtpv1u_update_tunnel_req_p->eps_bearer_id);

            return send_msg_to_task(TASK_GTPV1_U, INSTANCE_DEFAULT, message_p);
        }
    } else {
        message_p = alloc_new_message(TASK_SPGW_APP, SGW_MODIFY_BEARER_RESPONSE);
        if (message_p == NULL) {
            return -1;
        }
        modify_response_p = &message_p->msg.sgwModifyBearerResponse;

        modify_response_p->present                                      = MODIFY_BEARER_RESPONSE_REM;
        modify_response_p->choice.bearer_for_removal.eps_bearer_id      = modify_bearer_p->bearer_context_to_modify.eps_bearer_id;
        modify_response_p->choice.bearer_for_removal.cause              = CONTEXT_NOT_FOUND;
        modify_response_p->cause                                        = CONTEXT_NOT_FOUND;
        modify_response_p->trxn                                         = modify_bearer_p->trxn;
        return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
    }
    return -1;
}

int sgw_lite_handle_delete_session_request(SgwDeleteSessionRequest *delete_session_req_p)
{
    task_id_t    to_task;
    hashtbl_rc_t hash_rc;

    SgwDeleteSessionResponse                     *delete_session_resp_p;
    MessageDef                                   *message_p     = NULL;
    s_plus_p_gw_eps_bearer_context_information_t *ctx_p         = NULL;

#if defined(ENABLE_STANDALONE_EPC)
    to_task = TASK_MME_APP;
#else
    to_task = TASK_S11;
#endif

    message_p = alloc_new_message(TASK_SPGW_APP, SGW_DELETE_SESSION_RESPONSE);
    if (message_p == NULL) {
        return -1;
    }
    delete_session_resp_p = &message_p->msg.sgwDeleteSessionResponse;

    SPGW_APP_WARNING("Delete session handler needs to be completed...\n");

    if (delete_session_req_p->indication_flags & OI_FLAG) {
        SPGW_APP_DEBUG("OI flag is set for this message indicating the request"
                       "should be forwarded to P-GW entity\n");
    }

    hash_rc = hashtbl_get(sgw_app.s11_bearer_context_information_hashtable,
                          delete_session_req_p->teid,
                          (void**)&ctx_p);

    if (hash_rc == HASH_TABLE_OK) {
        if ((delete_session_req_p->sender_fteid_for_cp.ipv4 != 0) &&
            (delete_session_req_p->sender_fteid_for_cp.ipv6 != 0)) {
            /* Sender F-TEID IE present */
            if (delete_session_resp_p->teid != ctx_p->sgw_eps_bearer_context_information.mme_teid_for_S11) {
                delete_session_resp_p->teid  = ctx_p->sgw_eps_bearer_context_information.mme_teid_for_S11;
                delete_session_resp_p->cause = INVALID_PEER;
                SPGW_APP_DEBUG("Mismatch in MME Teid for CP\n");
            } else {
                delete_session_resp_p->teid = delete_session_req_p->sender_fteid_for_cp.teid;
            }
        } else {
            delete_session_resp_p->cause = REQUEST_ACCEPTED;
            delete_session_resp_p->teid  = ctx_p->sgw_eps_bearer_context_information.mme_teid_for_S11;
        }
        delete_session_resp_p->trxn    = delete_session_req_p->trxn;
        delete_session_resp_p->peer_ip = delete_session_req_p->peer_ip;

        return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
    } else {
        /* Context not found... set the cause to CONTEXT_NOT_FOUND
         * 3GPP TS 29.274 #7.2.10.1
         */
        message_p = alloc_new_message(TASK_SPGW_APP, SGW_DELETE_SESSION_RESPONSE);
        if (message_p == NULL) {
            return -1;
        }
        delete_session_resp_p = &message_p->msg.sgwDeleteSessionResponse;

        if ((delete_session_req_p->sender_fteid_for_cp.ipv4 == 0) &&
            (delete_session_req_p->sender_fteid_for_cp.ipv6 == 0)) {
            delete_session_resp_p->teid = 0;
        } else {
            delete_session_resp_p->teid = delete_session_req_p->sender_fteid_for_cp.teid;
        }
        delete_session_resp_p->cause   = CONTEXT_NOT_FOUND;
        delete_session_resp_p->trxn    = delete_session_req_p->trxn;
        delete_session_resp_p->peer_ip = delete_session_req_p->peer_ip;

        return send_msg_to_task(to_task, INSTANCE_DEFAULT, message_p);
    }

    return -1;
}