summaryrefslogtreecommitdiff
path: root/cesar/cp2/cco/action/src/cco_action.c
blob: 0a4b5400bf807685154d552fd1f3e5cd6dd3281f (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
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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp2/cco/action/src/cco_action.c
 * \brief   CCo Action functions.
 * \ingroup cp2_cco
 *
 */
#include <string.h>
#include "common/std.h"
#include "common/defs/homeplugAV.h"

#include "lib/set.h"
#include "lib/utils.h"
#include "lib/rnd.h"

#include "common/defs/ethernet.h"

#include "cl/cl_mactotei.h"

#include "mac/common/ntb.h"
#include "mac/common/timings.h"

#include "cp2/defs.h"
#include "cp2/cp.h"
#include "cp2/fsm/fsm.h"
#include "cp2/msg/msg.h"

#include "cp2/cco/action/cco_action.h"
#include "cp2/sta/mgr/sta_mgr.h"
#include "cp2/sta/mgr/net.h"
#include "cp2/sta/mgr/sta.h"

#include "cp2/beacon/beacon.h"

#include "cp2/inc/context.h"
#include "cp2/cco/action/inc/cco_action.h"

/**
 * Search for an available TEI in the AVLN list.
 * \param  ctx  the module context.
 * \return  return the TEI found.
 *
 *  Get the AVLN from the station manager (our AVLN). Lookup in the station
 * a hole of TEIs. It no hole is found and no more STAs are available
 * it means that all the TEIs are used in the AVLN. \Warn The TEI = 0
 * is reserved and 255 too
 */
u8
cp_cco_action_tei_compute (cp_cco_action_t *ctx)
{
    uint flag;
    uint flag_rot;
    uint index;
    uint i;
    uint tei;
    dbg_assert (ctx);

    for (index = 0; index < CP_CCO_ACTION_TEI_FLAGS_ROW; index ++)
    {
        flag = ~ctx->tei_flags[index];
        flag_rot = flag;
        for (i = (index == 0 ? 1 : 0) ;
             i < sizeof(uint) * 8
             && ((index == CP_CCO_ACTION_TEI_FLAGS_ROW - 1) ?
                    (flag_rot == 0x80000000 ? 0 : 1) : flag_rot);
             i++)
        {
            flag_rot = flag >> i;
            tei = flag_rot & BITS_ONES(i);
            if (tei == true)
            {
                // Store the bit in the correct position.
                flag_rot = (1 << i);
                ctx->tei_flags[index] |= flag_rot;

                // Compute the TEI value.
                tei = i + (index * sizeof(uint) * 8) + 1;
                return tei;
            }
        }
    }

    // No TEI are available.
    return 0;
}

/**
 * Release a TEI.
 * \param  ctx  the module context.
 * \param  tei  The TEI value to release.
 */
void
cp_cco_action_tei_release (cp_cco_action_t *ctx, u8 tei)
{
    uint row;

    dbg_assert (ctx);
    dbg_assert (tei);

    row = tei / CP_CCO_ACTION_TEI_FLAGS_ROW_SIZE_BITS;

    ctx->tei_flags[row] &= ~(1 << ((tei - 1)
                                  % CP_CCO_ACTION_TEI_FLAGS_ROW_SIZE_BITS));
}


/**
 * initialisation of CCo action module.
 * \param  ctx  the module context.
 *
 */
void
cp_cco_action_init (cp_t *ctx)
{
    uint i;
    dbg_assert (ctx);

    for (i = 0; i < CP_CCO_ACTION_TEI_FLAGS_ROW; i++)
        ctx->cco_action.tei_flags[i] = 0;

    lib_rnd_init (&ctx->cco_action.rnd, mac_ntb());
}

/**
 * Uninitialisation of CCo action module.
 * \param  ctx  the module context.
 *
 */
void
cp_cco_action_uninit (cp_t *ctx)
{
}

/**
 * Change the SNID.
 * \param  ctx  the module context.
 *
 *  It shall request the SNIDs in use from the station manager to choose
 * one randomly in the available SNIDs
 */
void
cp_cco_action_snid_change (cp_t *ctx)
{
    u16 flags;
    u16 table [16];
    uint nb_elem;
    uint i;
    dbg_assert (ctx);

    // Get the present flags.
    flags = ~cp_sta_mgr_get_present_snids (ctx);

    // fill the table.
    for (i = 0, nb_elem = 0; i < 16; i++)
    {
        if ((flags >> i) & true)
        {
            table[nb_elem] = i;
            nb_elem ++;
        }
    }


    // Chose the new SNID.
    i = lib_rnd32 (&ctx->cco_action.rnd) % nb_elem;

    // Provide the SNID to the beacon module.
    cp_beacon_change_snid (ctx, table[i]);
}

/**
 * perform garbage actions of CCo's responsibility.
 * \param  ctx  the module context.
 */
void
cp_cco_action_garbage (cp_t *ctx)
{
    cp_sta_t *sta;
    cp_sta_t *sta_next;
    cp_net_t *net;
    dbg_assert (ctx);

    net = cp_sta_mgr_get_our_avln (ctx);

    sta = cp_net_get_first (ctx, net, CP_STA_STATE_ASSOCIATED);
    while (sta)
    {
        slab_addref (sta);
        sta_next = cp_net_get_next (ctx, net, sta);
        if (less_mod2p32(sta->tei_lease_ms, /*cp_sta_core_date_ms (ctx)*/ 0))
        {
            // Remove the node from the tei lease.
            cp_cco_action_manage_sta_leave (ctx, NULL, sta);
        }
        slab_release (sta);
        sta = sta_next;
    }

    sta = cp_net_get_first (ctx, net, CP_STA_STATE_EXISTS);
    while (sta)
    {
        slab_addref (sta);
        sta_next = cp_net_get_next (ctx, net, sta);
        if (less_mod2p32(sta->tei_lease_ms, /*cp_sta_core_date_ms (ctx)*/ 0))
        {
            // Remove the node from the tei lease.
            cp_net_sta_remove (ctx, net, sta);
        }

        slab_release (sta);
        sta = sta_next;
    }
}

/**
 * generate a new NEK value.
 * \param  ctx  the module context.
 *
 *   NEK shall be used no more than 1 hour, so must be changed after 1
 * hour  for that purpose, NEK expiration date is checked at every occurrence
 * of  the global garbage collector timer and if current NEK expiration
 * date reached,  a new NEK value shall be generated by calling this function
 */
void
cp_cco_action_gen_nek (cp_t *ctx)
{
    u8 key[16];
    uint random_num;
    uint i;

    dbg_assert (ctx);

    // Get the key.
    random_num = lib_rnd32 (&ctx->rnd);
    cp_secu_aes_generate_key (random_num, key);

    // Set the key.
    if (ctx->mac_config->nek[0].nek[0] == 0)
    {
        for (i = 0; i < 4; i++)
            ctx->mac_config->nek[0].nek[i] = ((u32*) key)[i];

        // Get the key.
        random_num = lib_rnd32 (&ctx->rnd);
        cp_secu_aes_generate_key (random_num, key);

        for (i = 0; i < 4; i++)
            ctx->mac_config->nek[1].nek[i] = ((u32*) key)[i];
    }
    else
    {
        ctx->mac_config->nek[0] = ctx->mac_config->nek[1];
        for (i = 0; i < 4; i++)
            ctx->mac_config->nek[1].nek[i] = ((u32*) key)[i];
    }
}

/**
 * manage association of a station.
 * \param  ctx  the module context.
 * \param  assoc_req  CM_ASSOC.REQ MME msg having being received
 */
void
cp_cco_action_manage_sta_assoc (cp_t *ctx, cp_mme_rx_t * assoc_req)
{
    cp_msg_cc_assoc_req_t assoc;
    cp_msg_cc_assoc_cnf_t cnf;
    cp_sta_t *sta;
    cp_net_t *net;
    uint tei;

    dbg_assert (ctx);
    dbg_assert (assoc_req);

    /* Reading the data in the MME. */
    cp_msg_cc_assoc_req_receive(ctx, assoc_req, &assoc);

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

    if (assoc.request_type == CP_MSG_CC_ASSOC_REQ_TYPE_NEW)
    {
        // If the station already exists.
        // Read the mac to TEI table of the CL to get the TEI of the station.
        tei = cl_mactotei_table_find_tei_from_mac (ctx->cl,
                                                   assoc_req->peer.mac);
        if (tei)
        {
            // The station already exists.
            // Remove the station from the AVLN.
            sta = cp_net_get_sta (ctx, net, tei);
            cp_cco_action_manage_sta_leave (ctx, NULL, sta);
            slab_release (sta);
        }


        /* Try to get a TEI. */
        tei = cp_cco_action_tei_compute (&ctx->cco_action);

        sta = cp_net_sta_add (ctx, net, tei, assoc_req->peer.mac);

        if (tei)
        {
            /* Add the TEI lease corresponding to an unassociated STA. */
            cp_net_commit_to_dataplane (ctx, net);
            sta->tei_lease_ms =
                MAC_SEC_TO_MS(CP_CCO_ACTION_LEASE_ASSOC_MIN * 60);

            cnf.result = CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS;
            cnf.lease_time_min = CP_CCO_ACTION_LEASE_ASSOC_MIN;
        }
        else
        {
            cnf.result =
             CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_TEMPORARY_RESSOURCE_EXHAUSTION;
            sta->tei_lease_ms =
                MAC_SEC_TO_MS(CP_CCO_ACTION_LEASE_UNASSOC_MIN * 60);
        }

        cnf.sta_tei = cp_sta_get_tei (sta);
    }
    else if (assoc.request_type == CP_MSG_CC_ASSOC_REQ_TYPE_RENEW)
    {
        /* Renew the TEI. */
        sta = cp_net_get_sta (ctx, net, assoc_req->peer.tei);

        if (sta)
        {
            /* Renew the TEI. */
            sta->tei_lease_ms =
                MAC_SEC_TO_MS(CP_CCO_ACTION_LEASE_AUTH_MIN * 60);

            cnf.result = CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS;
            cnf.sta_tei = cp_sta_get_tei (sta);
            cnf.lease_time_min = CP_CCO_ACTION_LEASE_AUTH_MIN;
        }
        else
        {
            cnf.result = CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_OTHER_REASON;
            cnf.sta_tei = 0;
            cnf.lease_time_min = 0;
        }
    }
    else
        dbg_assert (assoc.request_type == CP_MSG_CC_ASSOC_REQ_TYPE_NB);

    /* Finishing to fill the cnf context. */
    cnf.nid = cp_net_get_nid (ctx, net);
    cnf.snid = cp_net_get_snid (ctx, net);

    /* Send the answer. */
    cp_msg_cc_assoc_cnf_send (ctx, &CP_MME_PEER(assoc_req->peer.mac,
                                                tei), &cnf);
    if (tei
        && assoc.request_type != CP_MSG_CC_ASSOC_REQ_TYPE_RENEW)
    {
        // Send the TEI MAPs.
        cp_mme_tx_t *mme_tx;
        cp_sta_t *sta_list;
        cp_mme_peer_t *peer;

        // Send unicast.
        // It will send all the TEI MAP to the station just associated.
        peer = &CP_MME_PEER (assoc_req->peer.mac, tei);
        mme_tx = cp_msg_cc_set_tei_map_ind_send_begin (ctx, peer,
                        CP_MSG_CC_SET_TEI_MAP_IND_MODE_UPDATE,
                        cp_net_get_num_stas (ctx, net));

        sta_list = cp_net_get_first (ctx, net, CP_STA_STATE_ASSOCIATED);
        while (sta_list)
        {
            enum cp_msg_cc_set_tei_map_ind_status_t station_status;

            switch (cp_sta_get_state (sta_list))
            {
            case CP_STA_STATE_ASSOCIATED:
                station_status = CP_MSG_CC_SET_TEI_MAP_IND_STATUS_ASSOCIATED;
                break;
            case CP_STA_STATE_AUTHENTICATED:
                station_status =
                    CP_MSG_CC_SET_TEI_MAP_IND_STATUS_AUTHENTICATED;
                break;
            default:
                station_status =
                    CP_MSG_CC_SET_TEI_MAP_IND_STATUS_DISASSOCIATED;
            }

            cp_msg_cc_set_tei_map_ind_send_sta (ctx, mme_tx,
                cp_sta_get_tei (sta_list),
                cp_sta_get_mac_address (sta_list),
                station_status);

            sta_list = cp_net_get_next (ctx, net, sta_list);
        }

        cp_msg_cc_set_tei_map_ind_send_end (ctx, mme_tx);

        // Send broadcast.
        // Send the TEI and the Mac address of the station just associated to
        // all the station of the network.

        peer = &CP_MME_PEER (MAC_BROADCAST);
        mme_tx = cp_msg_cc_set_tei_map_ind_send_begin (ctx, peer,
                        CP_MSG_CC_SET_TEI_MAP_IND_MODE_ADD, 1);

        cp_msg_cc_set_tei_map_ind_send_sta (ctx, mme_tx,
            cp_sta_get_tei (sta),
            cp_sta_get_mac_address (sta),
            CP_MSG_CC_SET_TEI_MAP_IND_STATUS_ASSOCIATED);

        cp_msg_cc_set_tei_map_ind_send_end (ctx, mme_tx);
    }

    /* Release the reference on the station. */
    slab_release (sta);
}

/**
 * manage authentication of a station.
 * \param  ctx  the module context.
 * \param  get_key_req  CM_GET_KEY.REQ MME msg having being decrypted
 */
void
cp_cco_action_manage_sta_auth (cp_t *ctx, cp_mme_rx_t * get_key_req)
{
    cp_msg_cm_get_key_req_t req;
    cp_msg_cm_get_key_cnf_t cnf;
    cp_net_t *net;
    cp_sta_t *sta;
    cp_sta_own_data_t *own_data;
    cp_mme_peks_t peks;
    cp_secu_protocol_run_t prun;

    dbg_assert (ctx);
    dbg_assert (get_key_req);
    dbg_assert (get_key_req->peer.tei);

    // get the data in the payload of the mme.
    cp_msg_cm_get_key_req_receive (ctx, get_key_req, &req);

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

    if (sta)
    {
        // The station is now considered as authenticated.
        cp_sta_set_state (sta, CP_STA_STATE_AUTHENTICATED);

        cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED;

        if (req.key_type == CP_MSG_KEY_NEK)
        {
            cnf.key = ctx->cco_action.nek;
        }
        else
            dbg_assert (req.key_type == CP_MSG_KEY_NEK);

        slab_release (sta);
    }
    // Refuse the request.
    else
    {
        cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_REQUEST_REFUSED;
    }

    cnf.key_type = req.key_type;
    cnf.nid = req.nid;
    cp_msg_cm_get_key_cnf_send (ctx, &CP_MME_PEER (get_key_req->peer.mac,
                                                   get_key_req->peer.tei),
                                peks, &prun, &cnf);
}

/**
 * manage explicit leave request of a station.
 * \param  ctx  the module context.
 * \param  leave_req  The MME leave req message.
 * \param  sta  provided by the garbage function to send a CC_LEAVE_IND.
 *
 * Remove the station from the station manager
 */
void
cp_cco_action_manage_sta_leave (cp_t *ctx, cp_mme_rx_t * leave_req,
                                cp_sta_t *sta)
{
    cp_mme_peer_t *peer;
    cp_net_t *net;
    cp_mme_tx_t *mme;
    uint status;

    dbg_assert (ctx);

    /* Get the network of the station. */
    net = cp_sta_mgr_get_our_avln (ctx);

    // The station has sent a lave request.
    if (leave_req)
    {
        dbg_assert (leave_req->peer.tei);

        /* Get the station. */
        sta = cp_net_get_sta (ctx, net, leave_req->peer.tei);
        peer = &CP_MME_PEER (leave_req->peer.mac, leave_req->peer.tei);
        cp_msg_cc_leave_cnf_send (ctx, peer);
    }
    else
    {
        peer = &CP_MME_PEER (cp_sta_get_mac_address (sta),
                            cp_sta_get_tei (sta));

        cp_msg_cc_leave_ind_send (ctx, peer,
                                  CP_MSG_CC_LEAVE_IND_REASON_TEI_LEASE_EXPIRED,
                                  cp_net_get_nid(ctx, net));
    }

    // Send the TEI MAP to the station.
    peer = &CP_MME_PEER (MAC_BROADCAST);

    switch (cp_sta_get_state (sta))
    {
    case CP_STA_STATE_ASSOCIATED:
        status = CP_MSG_CC_SET_TEI_MAP_IND_STATUS_ASSOCIATED;
        break;
    case CP_STA_STATE_AUTHENTICATED:
        status = CP_MSG_CC_SET_TEI_MAP_IND_STATUS_AUTHENTICATED;
        break;
    default:
        status = CP_MSG_CC_SET_TEI_MAP_IND_STATUS_DISASSOCIATED;
    }

    mme = cp_msg_cc_set_tei_map_ind_send_begin (ctx, peer,
                                CP_MSG_CC_SET_TEI_MAP_IND_MODE_DELETE,
                                1);
    cp_msg_cc_set_tei_map_ind_send_sta (ctx, mme, cp_sta_get_tei (sta),
                                        cp_sta_get_mac_address (sta),
                                        status);
    cp_msg_cc_set_tei_map_ind_send_end (ctx, mme);

    // Remove the station.
    cp_net_sta_remove (ctx, net, sta);
    cp_net_commit_to_dataplane (ctx, net);

    /* Verify if the network is empty, if it is post an event to the FSM. */
    if (cp_net_is_empty (ctx, net))
    {
        cp_fsm_event_t *event;
        event = cp_fsm_event_bare_new (ctx,
                                       CP_FSM_EVENT_TYPE_all_sta_leaved);
        cp_fsm_post (ctx, event);
    }
}