summaryrefslogtreecommitdiff
path: root/cesar/cp/eoc/multi_sta/action/src/multi_sta_action.c
blob: 1fd85ca2695dff427687f26f1309c3ae4e217e86 (plain)
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
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
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/eoc/multi_sta/action/src/multi_sta_action.c
 * \brief   multi_sta Action functions.
 * \ingroup cp_multista_action
 *
 */

#include "common/std.h"

/* Private headers. */
#include "cp/inc/context.h"
    /* TODO: "cl_eoc_mactotei_entry_insert" should not be called.
     * It's a private function.*/
#include "cl/inc/context.h"
#include "cp/eoc/multi_sta_fsm/inc/tables.h"
#include "cp/eoc/inc/dbg_print.h"

/* Public headers. */
#include "cp/eoc/cco/action/vs_eoc_master.h"
#include "cp/eoc/multi_sta/action/multi_sta_action.h"
#include "cp/eoc/multi_sta_fsm/fsm.h"

/* Config headers. */
#include "config/cp/eoc/multi/sta/wl/allowed.h"
#include "config/cl/eoc.h"


/**
 * manage association of a station.
 * \param  ctx  the module context.
 * \param  assoc_req  CM_ASSOC.REQ MME msg having being received
 */

#define _BRANCH(state, event, to) \
    ((CP_EOC_MULTI_STA_FSM_STATE_ ## state) << 16 \
     | (CP_EOC_MULTI_STA_FSM_EVENT_TYPE_ ## event) << 8 \
     | (CP_EOC_MULTI_STA_FSM_STATE_ ## to))

static void
cp_eoc_multi_sta_action__assoc_req_common (cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_eoc_multi_sta_fsm_event_t event;
    cp_net_t *net;
    cp_tei_t tei = MAC_TEI_UNASSOCIATED;
    cp_msg_cc_assoc_cnf_t cnf;
    cp_sta_t *sta;
    bool added;
    mfs_tx_t *mfs;

    dbg_assert (ctx);
    dbg_assert (mme);

    /* Add the station to the AVLN. */
    net = cp_sta_mgr_get_our_avln (ctx);

    cnf.nid = cp_net_get_nid (ctx, net);
    cnf.snid = cp_net_get_snid (ctx, net);

    sta = cp_sta_mgr_sta_get_from_mac (ctx, mme->peer.mac);

#if CONFIG_CP_EOC_MULTI_STA_WL_ALLOWED
    cp_sta_own_data_t *own = cp_sta_mgr_get_sta_own_data (ctx);

    /* The station already exists get the TEI. */
    if (sta)
        tei = cp_sta_get_tei (sta);

    /* Check the White List restriction. */
    if ((MAC_TEI_IS_STA (tei)) && (sta->multi_sta.allowed == true)
        && (sta->multi_sta.to_leave == false))
    {
        cnf.result = ctx->cco_action.wl_accept_all ?
            CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS_WL_ACCEPT_ALL
            : CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS;
        cnf.lease_time_min = CP_LEASE_ASSOC_MIN;
    }
    else
    {
        cnf.result = ctx->cco_action.wl_complete ?
        CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_PERMANANT_RESSOURCE_EXHAUSTION :
        CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_TEMPORARY_RESSOURCE_EXHAUSTION;
        own->num_bad_assoc_failure ++;
    }
#else
    /* The station already exists get the TEI. */
    if (sta)
        tei = cp_sta_get_tei (sta);

    if (MAC_TEI_IS_STA (tei))
    {
        cnf.result = CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS;
        cnf.lease_time_min = CP_LEASE_ASSOC_MIN;
    }
    else
    {
        cnf.result = ctx->cco_action.wl_complete ?
        CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_PERMANANT_RESSOURCE_EXHAUSTION :
        CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_TEMPORARY_RESSOURCE_EXHAUSTION;
    }
#endif /* CONFIG_CP_EOC_MULTI_STA_WL_ALLOWED */

    /* Send the answer. */
    cnf.sta_tei = tei;

    /* find and change properly */
    mme->peer.tei = MAC_TEI_BCAST;

#if CONFIG_CP_EOC_MULTI_STA_WL_ALLOWED
    /* Check allowed field. */
    if ((MAC_TEI_IS_STA (tei)) && (sta->multi_sta.allowed == true))
    {
        if (sta->multi_sta.to_leave == false)
        {
            CP_TRACE (MULTI_STA_ASSOC, TRACE_U64 (mme->peer.mac), tei);
            cp_msg_cc_assoc_cnf_send (ctx, &mme->peer, &cnf);

            mfs = mac_store_mfs_add_tx (
                ctx->mac_store, false, true, MAC_LID_NONE, tei, &added);
            if (added)
                sar_mfs_add (ctx->sar, (mfs_t *)mfs);
            if (mfs)
                blk_release (mfs);

            /* Branch */
            cp_eoc_multi_sta_fsm_event_t *e = &event;
            cp_eoc_multi_sta_fsm_event_new (
                ctx, CP_EOC_MULTI_STA_FSM_EVENT_TYPE_CC_ASSOC_REQ, e);

            sta->fsm.handled_event = e;

            /*sta in the white list*/
            if (sta->fsm.state == CP_EOC_MULTI_STA_FSM_STATE_disconnected)
            {
                cp_eoc_multi_sta_fsm_branch_ (
                    ctx,
                    _BRANCH (disconnected, CC_ASSOC_REQ, associated),
                    sta);
                sta->associated_date_ms = cp_sta_core_get_date_ms (ctx);
                DBG_PRINT_2 ("sta: dsc->asc, tei=%d", tei);
            }
            else if (sta->fsm.state
                     == CP_EOC_MULTI_STA_FSM_STATE_unassociated)
            {
                cp_eoc_multi_sta_fsm_branch_ (
                    ctx,
                    _BRANCH (unassociated, CC_ASSOC_REQ, associated),
                    sta);
                sta->associated_date_ms = cp_sta_core_get_date_ms (ctx);
                DBG_PRINT_2 ("sta: una->asc, tei=%d", tei);
            }

            slab_release (sta);
        }
    }
    /* Sta not registered or not in the white list. */
    else
    {
        cp_eoc_multi_sta_fsm_event_t *e = &event;
        cp_eoc_multi_sta_fsm_event_new (
            ctx, CP_EOC_MULTI_STA_FSM_EVENT_TYPE_CC_ASSOC_REQ, e);
        sta->fsm.handled_event = e;
        cp_msg_cc_assoc_cnf_send (ctx, &mme->peer, &cnf);
        if (sta->fsm.state == CP_EOC_MULTI_STA_FSM_STATE_disconnected)
        {
            cp_eoc_multi_sta_fsm_branch_ (
                ctx,
                _BRANCH (disconnected, CC_ASSOC_REQ, unassociated),
                sta);
            DBG_PRINT_2 ("sta: dsc->una, tei=%d", tei);
        }
        else if (sta->fsm.state
                 == CP_EOC_MULTI_STA_FSM_STATE_unassociated)
        {
            DBG_PRINT_2 ("sta: una, tei=%d", tei);
            sta->fsm.handled_event = NULL;
        }

        slab_release (sta);
    }
#else
    if (tei)
    {
        cp_sta_mgr_commit_to_dataplane (ctx);
        /* Update the last request date in the sta. */
        CP_TRACE (MULTI_STA_ASSOC, TRACE_U64 (mme->peer.mac), tei);
        cp_msg_cc_assoc_cnf_send (ctx, &mme->peer, &cnf);

        mfs = mac_store_mfs_add_tx (ctx->mac_store, false, true,
                                    MAC_LID_NONE, tei, &added);
        if (added)
            sar_mfs_add (ctx->sar, (mfs_t *)mfs);
        if (mfs)
            blk_release (mfs);
        /* Branch. */
        cp_eoc_multi_sta_fsm_event_t *e = &event;
        cp_eoc_multi_sta_fsm_event_new (
            ctx, CP_EOC_MULTI_STA_FSM_EVENT_TYPE_CC_ASSOC_REQ, e);

        sta->fsm.handled_event = e;

        /* Sta in the white list. */
        cp_eoc_multi_sta_fsm_branch_ (
            ctx, _BRANCH (disconnected, CC_ASSOC_REQ, associated), sta);
        slab_release (sta);

    }
    /* Check allowed field as well for WL implementation. */
    if (!tei)
    {
        sta = cp_sta_mgr_sta_add (ctx, net, 0, mme->peer.mac);

        cp_eoc_multi_sta_fsm_event_t *e = &event;
        cp_eoc_multi_sta_fsm_event_new (
            ctx, CP_EOC_MULTI_STA_FSM_EVENT_TYPE_CC_ASSOC_REQ, e);

        sta->fsm.handled_event = e;

        /* Sta not in the white list. */
        cp_eoc_multi_sta_fsm_branch_
            (ctx, _BRANCH (disconnected, CC_ASSOC_REQ, unassociated), sta);

        slab_release (sta);
    }
#endif /* CONFIG_CP_EOC_MULTI_STA_WL_ALLOWED */
}

void
cp_eoc_multi_sta_action__assoc_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_msg_cc_assoc_req_t assoc;

    dbg_assert_ptr (ctx);
    dbg_assert_ptr (mme);

    cp_sta_t *sta;
    cp_tei_t tei = MAC_TEI_UNASSOCIATED;

    /* TODO take in consideration request type, is it new or renew*/
    if (cp_msg_cc_assoc_req_receive (ctx, mme, &assoc))
    {
        cp_eoc_multi_sta_action__assoc_req_common (ctx, mme);
    }
    else
    {
        sta = cp_sta_mgr_sta_get_from_mac (ctx, mme->peer.mac);
        tei = cp_sta_get_tei (sta);
        sta->fsm.handled_event = NULL;
        DBG_PRINT_2 ("wrong assoc_req, tei=%d", tei);
        slab_release (sta);
    }
}

void
cp_eoc_multi_sta_action__get_key_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_eoc_cco_action__cco__cm_get_key_req_pid0 (ctx, mme);
}

void
cp_eoc_cco_action__cco__cm_get_key_req_pid0 (
    cp_t *ctx, cp_mme_rx_t * get_key_req)
{
    cp_eoc_multi_sta_fsm_event_t event;
    cp_msg_cm_get_key_req_t req;
    cp_msg_cm_get_key_cnf_t cnf;
    cp_net_t *net = NULL;
    cp_sta_t *sta = NULL;
    bool added, acceptable = false;
    mfs_tx_t *mfs;

    dbg_assert (ctx);
    dbg_assert (get_key_req);

    if (cp_msg_cm_get_key_req_receive (ctx, get_key_req, &req)
        && cp_secu_protocol_check (NULL, &get_key_req->prun,
                                   CP_SECU_PROTOCOL_RUN_CHECK_RESULT_NEW)
        && MAC_TEI_IS_STA (get_key_req->peer.tei))
        acceptable = true;

    /* Get the data in the payload of the mme. */
    if (acceptable)
    {
        if (req.key_type == CP_MSG_KEY_NEK)
        {
            /* Get the network. */
            net = cp_sta_mgr_get_our_avln (ctx);
            /* Get the station. */
            sta = cp_sta_mgr_sta_get_assoc (ctx, net, get_key_req->peer.tei);

            if (sta)
            {
                cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED;
                cp_sta_set_assoc_confirmed (ctx, sta, true);
                sta->fsm.handled_event = NULL;

                /* All keys granted. */
                uint i;

                mac_nek_t *nek = bsu_nek_get_current (ctx->bsu);
                cnf.eks = nek->eks;
                for (i = 0; i < COUNT (nek->nek_enc); i++)
                    cnf.key.key[i] = nek->nek_enc[i];

                for (i = 0; i < MAC_CAP_NB; i++)
                {
                    mfs = mac_store_mfs_add_tx (
                        ctx->mac_store, false, false, MAC_LLID_MIN + i,
                        get_key_req->peer.tei, &added);
                    if (added)
                        sar_mfs_add (ctx->sar, (mfs_t *)mfs);
                    if (mfs)
                    {
                        mfs->cap = i;
                        blk_release (mfs);
                    }
                }

                /* Send the TEI map: Not needed in EoC. */
                slab_release (sta);
            }
            else
            {
                cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_REQUEST_REFUSED;

                sta = cp_sta_mgr_sta_get_from_mac (ctx,
                                                   get_key_req->peer.mac);
                if (sta)
                {
                    sta->fsm.handled_event = NULL;
                    DBG_PRINT_2 ("not auth, all keys not granted, tei=%d",
                                 get_key_req->peer.tei);
                    slab_release (sta);
                }
            }
            cp_secu_protocol_next (&get_key_req->prun, &ctx->rnd, false);
        } /* Use it to ack previous neck key. */
        else if (req.key_type == CP_MSG_KEY_TEK)
        {
            uint i;

            mac_nek_t *nek = bsu_nek_get_current (ctx->bsu);
            cnf.eks = nek->eks;
            for (i = 0; i < COUNT (nek->nek_enc); i++)
                cnf.key.key[i] = nek->nek_enc[i];

            /* Get the network. */
            net = cp_sta_mgr_get_our_avln (ctx);
            /* Get the station. */
            sta = cp_sta_mgr_sta_get_assoc (ctx, net, get_key_req->peer.tei);

            if (sta)
            {
                cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED;
                cp_sta_set_authenticated (ctx, sta, true);

                cp_eoc_multi_sta_fsm_event_t *e = &event;
                cp_eoc_multi_sta_fsm_event_new (
                    ctx, CP_EOC_MULTI_STA_FSM_EVENT_TYPE_CC_GET_KEY_REQ, e);

                sta->fsm.handled_event = e;

                /* all keys granted*/
                cp_eoc_multi_sta_fsm_branch_ (
                    ctx,
                    _BRANCH (associated, CC_GET_KEY_REQ, authenticated),
                    sta);

                sta->authenticated_to_unassociated = true;
#if CONFIG_CL_EOC_ROUTE
                arch_dsr_lock ();
                added = cl_eoc_mactotei_entry_insert (
                    ctx->cl, get_key_req->peer.mac, get_key_req->peer.tei);
                arch_dsr_unlock ();
                dbg_assert (added);
#endif
                slab_release (sta);
            }
            cp_secu_protocol_next (&get_key_req->prun, &ctx->rnd, true);
        }
    }
    /* Refuse the request. */
    else
    {
        cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_REQUEST_REFUSED;

        sta = cp_sta_mgr_sta_get_from_mac (ctx, get_key_req->peer.mac);
        if (sta)
        {
            sta->fsm.handled_event = NULL;
            DBG_PRINT_2 ("not auth, all keys not granted, tei=%d",
                        get_key_req->peer.tei);
            slab_release (sta);
        }
    }

    /* Send message to peer. Mark station authenticated. */
    cnf.key_type = req.key_type;
    cnf.nid = req.nid;
    cp_msg_cm_get_key_cnf_send (ctx, &get_key_req->peer, get_key_req->peks,
                                &get_key_req->prun, &cnf);

    if ((req.key_type == CP_MSG_KEY_TEK)
        && (cnf.result == CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED))
    {
        cp_eoc_cco_action_vs__cco__vs_set_out_lev_ind (
            ctx, &get_key_req->peer);
        DBG_PRINT_2 ("sta: auth, tei=%d", get_key_req->peer.tei);
        cp_sta_own_data_t *own = cp_sta_mgr_get_sta_own_data (ctx);
        own->num_good_assoc_auth ++;
    }
}

void
cp_eoc_multi_sta_action_put_sta_unassociated (cp_t *ctx, cp_sta_t *station)
{
    dbg_assert (ctx);
    dbg_assert (station);

    station->fsm.state = CP_EOC_MULTI_STA_FSM_STATE_unassociated;
}

cp_tei_t
cp_eoc_multi_sta_action_compute_tei (cp_t *ctx)
{
    static uint tei = MAC_TEI_STA_MIN_EOC - 1;

    tei ++;
    if (tei <= MAC_TEI_STA_MAX)
        return tei;
    else
        return MAC_TEI_UNASSOCIATED;
}

#undef _BRANCH