s1ap_eNB_handlers.c 16.2 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
39
/*******************************************************************************

  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

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

/*! \file s1ap_eNB_handlers.c
 * \brief s1ap messages handlers for eNB part
 * \author Sebastien ROUX <sebastien.roux@eurecom.fr>
 * \date 2013
 * \version 0.1
 */

#include <stdint.h>

40
41
#include "intertask_interface.h"

Cedric Roux's avatar
 
Cedric Roux committed
42
43
#include "s1ap_common.h"
#include "s1ap_ies_defs.h"
44
45
// #include "s1ap_eNB.h"
#include "s1ap_eNB_defs.h"
Cedric Roux's avatar
 
Cedric Roux committed
46
47
48
49
50
51
#include "s1ap_eNB_handlers.h"
#include "s1ap_eNB_decoder.h"

#include "s1ap_eNB_ue_context.h"
#include "s1ap_eNB_trace.h"
#include "s1ap_eNB_nas_procedures.h"
52
#include "s1ap_eNB_management_procedures.h"
Cedric Roux's avatar
 
Cedric Roux committed
53

54
#include "s1ap_eNB_default_values.h"
Cedric Roux's avatar
 
Cedric Roux committed
55
56
57

#include "conversions.h"

58
59
60
61
static
int s1ap_eNB_handle_s1_setup_response(uint32_t               assoc_id,
                                      uint32_t               stream,
                                      struct s1ap_message_s *message_p);
Cedric Roux's avatar
Cedric Roux committed
62
static
63
64
65
66
67
68
69
70
int s1ap_eNB_handle_s1_setup_failure(uint32_t               assoc_id,
                                     uint32_t               stream,
                                     struct s1ap_message_s *message_p);

static
int s1ap_eNB_handle_initial_context_request(uint32_t               assoc_id,
                                            uint32_t               stream,
                                            struct s1ap_message_s *message_p);
Cedric Roux's avatar
 
Cedric Roux committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

/* Handlers matrix. Only eNB related procedure present here */
s1ap_message_decoded_callback messages_callback[][3] = {
    { 0, 0, 0 }, /* HandoverPreparation */
    { 0, 0, 0 }, /* HandoverResourceAllocation */
    { 0, 0, 0 }, /* HandoverNotification */
    { 0, 0, 0 }, /* PathSwitchRequest */
    { 0, 0, 0 }, /* HandoverCancel */
    { 0, 0, 0 }, /* E_RABSetup */
    { 0, 0, 0 }, /* E_RABModify */
    { 0, 0, 0 }, /* E_RABRelease */
    { 0, 0, 0 }, /* E_RABReleaseIndication */
    { s1ap_eNB_handle_initial_context_request, 0, 0 }, /* InitialContextSetup */
    { 0, 0, 0 }, /* Paging */
    { s1ap_eNB_handle_nas_downlink, 0, 0 }, /* downlinkNASTransport */
    { 0, 0, 0 }, /* initialUEMessage */
    { 0, 0, 0 }, /* uplinkNASTransport */
    { 0, 0, 0 }, /* Reset */
    { 0, 0, 0 }, /* ErrorIndication */
    { 0, 0, 0 }, /* NASNonDeliveryIndication */
91
    { 0, s1ap_eNB_handle_s1_setup_response, s1ap_eNB_handle_s1_setup_failure }, /* S1Setup */
Cedric Roux's avatar
 
Cedric Roux committed
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
    { 0, 0, 0 }, /* UEContextReleaseRequest */
    { 0, 0, 0 }, /* DownlinkS1cdma2000tunneling */
    { 0, 0, 0 }, /* UplinkS1cdma2000tunneling */
    { 0, 0, 0 }, /* UEContextModification */
    { 0, 0, 0 }, /* UECapabilityInfoIndication */
    { 0, 0, 0 }, /* UEContextRelease */
    { 0, 0, 0 }, /* eNBStatusTransfer */
    { 0, 0, 0 }, /* MMEStatusTransfer */
    { s1ap_eNB_handle_deactivate_trace, 0, 0 }, /* DeactivateTrace */
    { s1ap_eNB_handle_trace_start, 0, 0 }, /* TraceStart */
    { 0, 0, 0 }, /* TraceFailureIndication */
    { 0, 0, 0 }, /* ENBConfigurationUpdate */
    { 0, 0, 0 }, /* MMEConfigurationUpdate */
    { 0, 0, 0 }, /* LocationReportingControl */
    { 0, 0, 0 }, /* LocationReportingFailureIndication */
    { 0, 0, 0 }, /* LocationReport */
    { 0, 0, 0 }, /* OverloadStart */
    { 0, 0, 0 }, /* OverloadStop */
    { 0, 0, 0 }, /* WriteReplaceWarning */
    { 0, 0, 0 }, /* eNBDirectInformationTransfer */
    { 0, 0, 0 }, /* MMEDirectInformationTransfer */
    { 0, 0, 0 }, /* PrivateMessage */
    { 0, 0, 0 }, /* eNBConfigurationTransfer */
    { 0, 0, 0 }, /* MMEConfigurationTransfer */
    { 0, 0, 0 }, /* CellTrafficTrace */
#if defined(UPDATE_RELEASE_9)
    { 0, 0, 0 }, /* Kill */
    { 0, 0, 0 }, /* DownlinkUEAssociatedLPPaTransport  */
    { 0, 0, 0 }, /* UplinkUEAssociatedLPPaTransport */
    { 0, 0, 0 }, /* DownlinkNonUEAssociatedLPPaTransport */
    { 0, 0, 0 }, /* UplinkNonUEAssociatedLPPaTransport */
#endif
};

static const char *direction2String[] = {
    "", /* Nothing */
    "Originating message", /* originating message */
    "Successfull outcome", /* successfull outcome */
    "UnSuccessfull outcome", /* successfull outcome */
};

133
134
int s1ap_eNB_handle_message(uint32_t assoc_id, int32_t stream,
                            const uint8_t * const data, const uint32_t data_length)
Cedric Roux's avatar
 
Cedric Roux committed
135
136
137
{
    struct s1ap_message_s message;

138
    DevAssert(data != NULL);
Cedric Roux's avatar
 
Cedric Roux committed
139
140
141

    memset(&message, 0, sizeof(struct s1ap_message_s));

142
    if (s1ap_eNB_decode_pdu(&message, data, data_length) < 0) {
Cedric Roux's avatar
 
Cedric Roux committed
143
144
145
146
147
148
149
150
        S1AP_ERROR("Failed to decode PDU\n");
        return -1;
    }
    /* Checking procedure Code and direction of message */
    if (message.procedureCode > sizeof(messages_callback) / (3 * sizeof(
                s1ap_message_decoded_callback))
            || (message.direction > S1AP_PDU_PR_unsuccessfulOutcome)) {
        S1AP_ERROR("[SCTP %d] Either procedureCode %d or direction %d exceed expected\n",
151
                   assoc_id, message.procedureCode, message.direction);
Cedric Roux's avatar
 
Cedric Roux committed
152
153
154
155
156
157
158
        return -1;
    }
    /* No handler present.
     * This can mean not implemented or no procedure for eNB (wrong direction).
     */
    if (messages_callback[message.procedureCode][message.direction-1] == NULL) {
        S1AP_ERROR("[SCTP %d] No handler for procedureCode %d in %s\n",
159
                   assoc_id, message.procedureCode,
Cedric Roux's avatar
 
Cedric Roux committed
160
161
162
163
164
                   direction2String[message.direction]);
        return -1;
    }

    /* Calling the right handler */
165
166
    return (*messages_callback[message.procedureCode][message.direction-1])
        (assoc_id, stream, &message);
Cedric Roux's avatar
 
Cedric Roux committed
167
168
}

Cedric Roux's avatar
Cedric Roux committed
169
static
170
171
172
173
174
175
176
177
178
int s1ap_eNB_handle_s1_setup_failure(uint32_t               assoc_id,
                                     uint32_t               stream,
                                     struct s1ap_message_s *message_p)
{
    /* S1 Setup Failure == Non UE-related procedure -> stream 0 */
    if (stream != 0) {
        S1AP_WARN("[SCTP %d] Received s1 setup failure on stream != 0 (%d)\n",
                  assoc_id, stream);
    }
Cedric Roux's avatar
Cedric Roux committed
179
    S1AP_ERROR("Received s1 setup failure for MME... please check your parameters\n");
180
181
182
183
184
185
186

    return 0;
}

static
int s1ap_eNB_handle_s1_setup_response(uint32_t               assoc_id,
                                      uint32_t               stream,
Cedric Roux's avatar
 
Cedric Roux committed
187
188
                                      struct s1ap_message_s *message_p)
{
189
190
    S1ap_S1SetupResponseIEs_t *s1SetupResponse_p;
    s1ap_eNB_mme_data_t       *mme_desc_p;
Cedric Roux's avatar
 
Cedric Roux committed
191
192
193
194
    int i;

    DevAssert(message_p != NULL);

195
    s1SetupResponse_p = &message_p->msg.s1ap_S1SetupResponseIEs;
Cedric Roux's avatar
 
Cedric Roux committed
196
197

    /* S1 Setup Response == Non UE-related procedure -> stream 0 */
198
    if (stream != 0) {
Cedric Roux's avatar
 
Cedric Roux committed
199
        S1AP_ERROR("[SCTP %d] Received s1 setup response on stream != 0 (%d)\n",
200
                   assoc_id, stream);
Cedric Roux's avatar
 
Cedric Roux committed
201
202
203
        return -1;
    }

204
    if ((mme_desc_p = s1ap_eNB_get_MME(NULL, assoc_id, 0)) == NULL) {
Cedric Roux's avatar
 
Cedric Roux committed
205
        S1AP_ERROR("[SCTP %d] Received S1 setup response for non existing "
206
                   "MME context\n", assoc_id);
Cedric Roux's avatar
 
Cedric Roux committed
207
208
209
210
211
212
213
214
215
        return -1;
    }

    /* The list of served gummei can contain at most 8 elements.
     * LTE related gummei is the first element in the list, i.e with an id of 0.
     */
    DevAssert(s1SetupResponse_p->servedGUMMEIs.list.count == 1);

    for (i = 0; i < s1SetupResponse_p->servedGUMMEIs.list.count; i++) {
216
217
        struct S1ap_ServedGUMMEIsItem *gummei_item_p;
        struct served_gummei_s        *new_gummei_p;
Cedric Roux's avatar
 
Cedric Roux committed
218
219
        int j;

220
        gummei_item_p = (struct S1ap_ServedGUMMEIsItem *)
Cedric Roux's avatar
 
Cedric Roux committed
221
222
223
224
225
226
227
228
                        s1SetupResponse_p->servedGUMMEIs.list.array[i];
        new_gummei_p = calloc(1, sizeof(struct served_gummei_s));

        STAILQ_INIT(&new_gummei_p->served_plmns);
        STAILQ_INIT(&new_gummei_p->served_group_ids);
        STAILQ_INIT(&new_gummei_p->mme_codes);

        for (j = 0; j < gummei_item_p->servedPLMNs.list.count; j++) {
229
            S1ap_PLMNidentity_t *plmn_identity_p;
Cedric Roux's avatar
 
Cedric Roux committed
230
231
232
233
234
235
236
237
238
239
            struct plmn_identity_s *new_plmn_identity_p;

            plmn_identity_p = gummei_item_p->servedPLMNs.list.array[i];
            new_plmn_identity_p = calloc(1, sizeof(struct plmn_identity_s));
            TBCD_TO_MCC_MNC(plmn_identity_p, new_plmn_identity_p->mcc,
                            new_plmn_identity_p->mnc);
            STAILQ_INSERT_TAIL(&new_gummei_p->served_plmns, new_plmn_identity_p, next);
            new_gummei_p->nb_served_plmns++;
        }
        for (j = 0; j < gummei_item_p->servedGroupIDs.list.count; j++) {
240
            S1ap_MME_Group_ID_t           *mme_group_id_p;
Cedric Roux's avatar
 
Cedric Roux committed
241
242
243
244
245
246
247
248
249
            struct served_group_id_s *new_group_id_p;

            mme_group_id_p = gummei_item_p->servedGroupIDs.list.array[i];
            new_group_id_p = calloc(1, sizeof(struct served_group_id_s));
            OCTET_STRING_TO_INT16(mme_group_id_p, new_group_id_p->mme_group_id);
            STAILQ_INSERT_TAIL(&new_gummei_p->served_group_ids, new_group_id_p, next);
            new_gummei_p->nb_group_id++;
        }
        for (j = 0; j < gummei_item_p->servedMMECs.list.count; j++) {
250
            S1ap_MME_Code_t        *mme_code_p;
Cedric Roux's avatar
 
Cedric Roux committed
251
252
253
254
255
256
257
258
259
260
261
262
            struct mme_code_s *new_mme_code_p;

            mme_code_p = gummei_item_p->servedMMECs.list.array[i];
            new_mme_code_p = calloc(1, sizeof(struct mme_code_s));

            OCTET_STRING_TO_INT8(mme_code_p, new_mme_code_p->mme_code);
            STAILQ_INSERT_TAIL(&new_gummei_p->mme_codes, new_mme_code_p, next);
            new_gummei_p->nb_mme_code++;
        }
        STAILQ_INSERT_TAIL(&mme_desc_p->served_gummei, new_gummei_p, next);
    }
    /* Free contents of the list */
263
    ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_S1ap_ServedGUMMEIs,
Cedric Roux's avatar
 
Cedric Roux committed
264
265
266
267
                                  (void *)&s1SetupResponse_p->servedGUMMEIs);
    /* Set the capacity of this MME */
    mme_desc_p->relative_mme_capacity = s1SetupResponse_p->relativeMMECapacity;
    /* Optionaly set the mme name */
268
    if (s1SetupResponse_p->presenceMask & S1AP_S1SETUPRESPONSEIES_MMENAME_PRESENT) {
Cedric Roux's avatar
 
Cedric Roux committed
269
270
271
272
273
274
275
276
277
278
279
        mme_desc_p->mme_name = calloc(s1SetupResponse_p->mmEname.size + 1, sizeof(char));
        memcpy(mme_desc_p->mme_name, s1SetupResponse_p->mmEname.buf,
               s1SetupResponse_p->mmEname.size);
        /* Convert the mme name to a printable string */
        mme_desc_p->mme_name[s1SetupResponse_p->mmEname.size] = '\0';
    }
    /* The association is now ready as eNB and MME know parameters of each other.
     * Mark the association as UP to enable UE contexts creation.
     */
    mme_desc_p->state = S1AP_ENB_STATE_CONNECTED;

Cedric Roux's avatar
Cedric Roux committed
280
281
282
283
284
285
286
287
288
289
    /* We call back our self
     * -> generate a dummy initial UE message
     */
    {
        s1ap_nas_first_req_t s1ap_nas_first_req;

        memset(&s1ap_nas_first_req, 0, sizeof(s1ap_nas_first_req_t));

        s1ap_nas_first_req.rnti = 0xC03A;
        s1ap_nas_first_req.establishment_cause = RRC_CAUSE_MO_DATA;
winckel's avatar
winckel committed
290
        s1ap_nas_first_req.ue_identity.presenceMask = UE_IDENTITIES_gummei;
Cedric Roux's avatar
Cedric Roux committed
291

winckel's avatar
winckel committed
292
293
294
295
        s1ap_nas_first_req.ue_identity.gummei.mcc = 208;
        s1ap_nas_first_req.ue_identity.gummei.mnc = 34;
        s1ap_nas_first_req.ue_identity.gummei.mme_code = 0;
        s1ap_nas_first_req.ue_identity.gummei.mme_group_id = 0;
Cedric Roux's avatar
Cedric Roux committed
296

winckel's avatar
winckel committed
297
#if 0
Cedric Roux's avatar
Cedric Roux committed
298
        /* NAS Attach request with IMSI */
winckel's avatar
winckel committed
299
        static uint8_t nas_attach_req_imsi[] =
Cedric Roux's avatar
Cedric Roux committed
300
301
302
303
304
305
306
307
308
309
310
311
        {
            0x07, 0x41,
            /* EPS Mobile identity = IMSI */
            0x71, 0x08, 0x29, 0x80, 0x43, 0x21, 0x43, 0x65, 0x87,
            0xF9,
            /* End of EPS Mobile Identity */
            0x02, 0xE0, 0xE0, 0x00, 0x20, 0x02, 0x03,
            0xD0, 0x11, 0x27, 0x1A, 0x80, 0x80, 0x21, 0x10, 0x01, 0x00, 0x00,
            0x10, 0x81, 0x06, 0x00, 0x00, 0x00, 0x00, 0x83, 0x06, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x0A, 0x00, 0x52, 0x12, 0xF2,
            0x01, 0x27, 0x11,
        };
winckel's avatar
winckel committed
312
#endif
Cedric Roux's avatar
Cedric Roux committed
313
314

        /* NAS Attach request with GUTI */
winckel's avatar
winckel committed
315
        static uint8_t nas_attach_req_guti[] =
Cedric Roux's avatar
Cedric Roux committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
        {
            0x07, 0x41,
            /* EPS Mobile identity = IMSI */
            0x71, 0x0B, 0xF6, 0x12, 0xF2, 0x01, 0x80, 0x00, 0x01, 0xE0, 0x00,
            0xDA, 0x1F,
            /* End of EPS Mobile Identity */
            0x02, 0xE0, 0xE0, 0x00, 0x20, 0x02, 0x03,
            0xD0, 0x11, 0x27, 0x1A, 0x80, 0x80, 0x21, 0x10, 0x01, 0x00, 0x00,
            0x10, 0x81, 0x06, 0x00, 0x00, 0x00, 0x00, 0x83, 0x06, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x0A, 0x00, 0x52, 0x12, 0xF2,
            0x01, 0x27, 0x11,
        };

        s1ap_nas_first_req.nas_pdu.buffer = nas_attach_req_guti;
        s1ap_nas_first_req.nas_pdu.length = sizeof(nas_attach_req_guti);

332
333
        s1ap_eNB_handle_nas_first_req(mme_desc_p->s1ap_eNB_instance->instance,
                                      &s1ap_nas_first_req);
Cedric Roux's avatar
Cedric Roux committed
334
    }
Cedric Roux's avatar
 
Cedric Roux committed
335
336
337
338

    return 0;
}

339
340
341
342
static
int s1ap_eNB_handle_initial_context_request(uint32_t               assoc_id,
                                            uint32_t               stream,
                                            struct s1ap_message_s *message_p)
Cedric Roux's avatar
 
Cedric Roux committed
343
344
345
346
{
    s1ap_eNB_mme_data_t   *mme_desc_p;
    s1ap_eNB_ue_context_t *ue_desc_p;

347
    S1ap_InitialContextSetupRequestIEs_t *initialContextSetupRequest_p;
Cedric Roux's avatar
 
Cedric Roux committed
348
349
    DevAssert(message_p != NULL);

350
    initialContextSetupRequest_p = &message_p->msg.s1ap_InitialContextSetupRequestIEs;
Cedric Roux's avatar
 
Cedric Roux committed
351

Cedric Roux's avatar
Cedric Roux committed
352
353
354
355
356
357
358
359
    if ((mme_desc_p = s1ap_eNB_get_MME(NULL, assoc_id, 0)) == NULL) {
        S1AP_ERROR("[SCTP %d] Received initial context setup request for non "
                   "existing MME context\n", assoc_id);
        return -1;
    }
    if ((ue_desc_p = s1ap_eNB_get_ue_context(mme_desc_p->s1ap_eNB_instance,
                     initialContextSetupRequest_p->eNB_UE_S1AP_ID)) == NULL) {
        S1AP_ERROR("[SCTP %d] Received initial context setup request for non "
360
        "existing UE context 0x%06x\n", assoc_id,
361
        initialContextSetupRequest_p->eNB_UE_S1AP_ID);
Cedric Roux's avatar
 
Cedric Roux committed
362
363
364
        return -1;
    }

Cedric Roux's avatar
Cedric Roux committed
365
366
367
368
369
370
    /* Initial context request = UE-related procedure -> stream != 0 */
    if (stream != ue_desc_p->stream) {
        S1AP_ERROR("[SCTP %d] Received UE-related procedure on stream (%d) whereas expecting (%d)\n",
                   assoc_id, stream, ue_desc_p->stream);
        return -1;
    }
Cedric Roux's avatar
 
Cedric Roux committed
371

372
    ue_desc_p->mme_ue_s1ap_id = initialContextSetupRequest_p->mme_ue_s1ap_id;
Cedric Roux's avatar
 
Cedric Roux committed
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
//     {
//         int i;
// 
//         extern int s1ap_eNB_handle_api_req(eNB_mme_desc_t     *eNB_desc_p,
//                                            s1ap_rrc_api_req_t *api_req_p);
// 
//         s1ap_rrc_api_req_t api_req;
//         s1ap_initial_ctxt_setup_resp_t *initial_ctxt_resp_p;
// 
//         memset(&api_req, 0, sizeof(s1ap_rrc_api_req_t));
// 
//         initial_ctxt_resp_p = &api_req.msg.initial_ctxt_resp;
//         api_req.api_req = S1AP_API_INITIAL_CONTEXT_SETUP_RESP;
// 
//         initial_ctxt_resp_p->eNB_ue_s1ap_id = ue_desc_p->eNB_ue_s1ap_id;
//         initial_ctxt_resp_p->e_rabs_failed = 0;
//         initial_ctxt_resp_p->nb_of_e_rabs
//         = initialContextSetupRequest_p->e_RABToBeSetupListCtxtSUReq.e_RABToBeSetupItemCtxtSUReq.count;
//         for (i = 0; i < initialContextSetupRequest_p->e_RABToBeSetupListCtxtSUReq.e_RABToBeSetupItemCtxtSUReq.count; i++)
//         {
//             struct E_RABToBeSetupItemCtxtSUReq_s *item;
//             item = (struct E_RABToBeSetupItemCtxtSUReq_s *)initialContextSetupRequest_p->e_RABToBeSetupListCtxtSUReq.e_RABToBeSetupItemCtxtSUReq.array[i];
//             initial_ctxt_resp_p->e_rabs = realloc(initial_ctxt_resp_p->e_rabs, i * sizeof(e_rab_setup_t));
//             initial_ctxt_resp_p->e_rabs[i].e_rab_id = 5;
// 
//         }
// 
//         s1ap_eNB_handle_api_req(eNB_desc_p, &api_req);
//     }
Cedric Roux's avatar
 
Cedric Roux committed
403
404
405

    return 0;
}