summaryrefslogtreecommitdiff
path: root/cesar/cp/sta/mgr/src/sta_own_data.c
blob: bca8688a1fec4fc16d4acf38f59151c7067fbb5f (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
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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/sta/data/src/sta_own_data.c
 * \brief   Station data source.
 * \ingroup cp_sta_data
 *
 */
#include "common/std.h"
#include "string.h"

#include "lib/dbg.h"
#include "lib/bitstream.h"
#include "cp/fsm/fsm.h"
#include "bsu/bsu.h"
#include "hal/gpio/gpio.h"

/* Public interfaces. */
#include "cp/sta/mgr/sta_own_data.h"
#include "cp/msg/msg.h"

/* Private interfaces. */
#include "cp/inc/context.h"
#include "cp/sta/mgr/inc/sta_own_data.h"

/**
 * Initialise the station own data to default values.
 * \param  ctx  the module context.
 */
void
cp_sta_own_data_init (cp_t *ctx)
{
    dbg_assert (ctx);
    memset (&ctx->sta_mgr.sta_own_data, 0,
            sizeof (cp_sta_own_data_private_t));
    /* Store the hybrid mode. */
    ctx->sta_mgr.sta_own_data.public.hybrid_mode =
        MAC_COEXISTENCE_HYBRID_DELIMITERS_MODE;
    /* Get an snid. */
    ctx->sta_mgr.sta_own_data.snid =
        lib_rnd_uniform (&ctx->rnd, HPAV_SNID_NB);
}

/**
 * Uninitialise the station own data to default values.
 * \param  ctx  the module context.
 */
void
cp_sta_own_data_uninit (cp_t *ctx)
{
    dbg_assert (ctx);
    memset ((u8*) &ctx->sta_mgr.sta_own_data,
            0, sizeof (cp_sta_own_data_private_t));
}

/**
 * Set the TEI of the station.
 * \param  ctx  the module context.
 * \param  tei  The TEI station.
 */
void
cp_sta_own_data_set_tei (cp_t * ctx, u8 tei)
{
    dbg_assert (ctx);
    dbg_assert (ctx->mac_config);

    if (tei != ctx->sta_mgr.sta_own_data.tei)
    {
        ctx->sta_mgr.sta_own_data.tei = tei;
        ctx->mac_config->tei = tei;

        cp_fsm_post_new_event (ctx, bare, sta_status_changed);
    }
}

/**
 * Get the value of the constant TEI.
 * \param  ctx  the module context.
 * \return  Get the station TEI.
 */
cp_tei_t
cp_sta_own_data_get_tei (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.tei;
}

/**
 * Set the station Mac address.
 * \param  ctx  the module context.
 * \param  mac_addr  The Mac address of the station.
 */
void
cp_sta_own_data_set_mac_address (cp_t *ctx, mac_t mac_addr)
{
    dbg_assert (ctx);
    dbg_assert (mac_addr);
    dbg_assert (ctx->mac_config);

    // Add the mac address to the sta own data.
    ctx->sta_mgr.sta_own_data.mac_addr = mac_addr;

    // Add the mac address to the mac config referred in the CP.
    ctx->mac_config->sta_mac_address = mac_addr;
}

/**
 * Get the station mac address.
 * \param  ctx  the module context.
 * \return  Get the station Mac address.
 *
 */
mac_t
cp_sta_own_data_get_mac_address (cp_t *ctx)
{
    return ctx->sta_mgr.sta_own_data.mac_addr;
}

/**
 * Set the Station NPW.
 * \param  ctx  the module context.
 * \param  npw  The new NPW.
 *
 * TODO : inform the driver ?
 */
void
cp_sta_own_data_set_npw (cp_t *ctx, const char * npw)
{
    dbg_assert (ctx);
    dbg_assert (npw);

    // Store the NPW.
    strcpy(ctx->sta_mgr.sta_own_data.npw, npw);

    // TODO : inform the driver ?
}

/**
 * Return the pointer to the NPW as a constant data.
 * \param  ctx  the module context.
 * \return  Get the station NPW.
 *
 *  The user shall call the set_npw function to modify the NPW
 */
const char *
cp_sta_own_data_get_npw (cp_t *ctx)
{
    dbg_assert (ctx);
    return ctx->sta_mgr.sta_own_data.npw;
}

bool
cp_sta_own_data_set_nmk (cp_t *ctx, const cp_key_t nmk,
                         cp_msg_drv_sta_set_key_type_t type)
{
    dbg_assert (ctx);
    dbg_assert (type < CP_MSG_DRV_STA_SET_KEY_TYPE_NB);

    uint i;
    cp_key_t current_key = cp_sta_own_data_get_nmk (ctx);

    for (i = 0; i < COUNT(nmk.key) ;i++)
        if (nmk.key[i] != current_key.key[i])
            break;

    if (i < COUNT(nmk.key))
    {
        ctx->sta_mgr.sta_own_data.nmk = nmk;

        /* Report new computed values to HLE. */
        cp_mme_peer_t peer = CP_MME_PEER (cp_sta_own_data_get_mac_address (ctx),
                                          MAC_TEI_FOREIGN);
        cp_nid_t nid = 0;
        cp_security_level_t sl = 0;

        if (type == CP_MSG_DRV_STA_SET_KEY_TYPE_CHANGE_NID)
            nid = cp_sta_own_data_get_nid (ctx);
        else
            sl = cp_sta_own_data_get_security_level (ctx);

        cp_msg_drv_sta_set_key_ind_send (ctx, &peer,
                                         nmk,
                                         type,
                                         nid,
                                         sl);
        return true;
    }

    return false;
}

/**
 * Get the NMK of the AVLN.
 * \param  ctx  the module context.
 * \return  Get the station NMK
 */
cp_key_t
cp_sta_own_data_get_nmk (cp_t *ctx)
{
    dbg_assert (ctx);
    return ctx->sta_mgr.sta_own_data.nmk;
}

/**
 * Set the device password.
 * \param  ctx  the module context.
 * \param  dpw  The new device password.
 * \param  length  The length in bytes of the DPW provided.
 *
 * // TODO : inform the driver ?
 */
void
cp_sta_own_data_set_dpw (cp_t *ctx, const char *dpw)
{
    dbg_assert (ctx);
    dbg_assert (dpw);

    strcpy (ctx->sta_mgr.sta_own_data.dpw, dpw);

    // TODO : inform the driver ?
}

/**
 * Return the Device password.
 * \param  ctx  the module context.
 * \return  Get the station DPW.
 */
const char*
cp_sta_own_data_get_dpw (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.dpw;
}

/**
 * Set the security level.
 * \param  ctx  the module context.
 * \param  sl  The security level.
 *
 * // TODO : inform the driver ?
 */
void
cp_sta_own_data_set_security_level (cp_t *ctx,
                                    cp_security_level_t sl)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (sl < CP_SECURITY_LEVEL_NB);

    ctx->sta_mgr.sta_own_data.security_level = sl;

    // TODO : inform the driver ?
}

/**
 * Return the security level.
 * \param  ctx  the module context.
 * \return Get the security level.
 *
 */
cp_security_level_t
cp_sta_own_data_get_security_level (cp_t *ctx)
{
    dbg_assert (ctx);
    return ctx->sta_mgr.sta_own_data.security_level;
}

/**
 * Set the CCo status of the station.
 * \param  ctx  the module context.
 * \param  status  The current CCo status of the station.
 *
 *  It shall send a message to the Linux Driver
 */
void
cp_sta_own_data_set_was_cco (cp_t *ctx, bool status)
{
    dbg_assert (ctx);
    ctx->sta_mgr.sta_own_data.was_cco = status;

    // inform the driver
    // TODO.
}

/**
 * Get the CCo status of the station after a reboot.
 * \param  ctx  the module context.
 * \return  \todo fill this
 *
 */
bool
cp_sta_own_data_get_was_cco (cp_t *ctx)
{
    dbg_assert (ctx);
    return ctx->sta_mgr.sta_own_data.was_cco;
}

/**
 * Set the HFID.
 *
 * \param  hfid  the HFID to set.
 * \param  data  the human identifier.
 */
static void
cp_sta_own_data_set_hfid (const char *hfid, const char *data)
{
    dbg_assert (data);
    dbg_assert (hfid);

    strcpy ((char *) hfid, (char *) data);
}

/**
 * Set the AVLN HFID.
 *
 * \param  ctx  the module context.
 * \param  data  the human identifier.
 */
void
cp_sta_own_data_set_hfid_avln (cp_t *ctx, const char* data)
{
    cp_sta_own_data_t *sta;

    dbg_assert (ctx);
    sta = cp_sta_mgr_get_sta_own_data (ctx);
    cp_sta_own_data_set_hfid (sta->hfid_avln, data);
}

/**
 * Set the User HFID.
 *
 * \param  ctx  the module context.
 * \param  data  the human identifier.
 */
void
cp_sta_own_data_set_hfid_user (cp_t *ctx, const char* data)
{
    cp_sta_own_data_t *sta;

    dbg_assert (ctx);
    sta = cp_sta_mgr_get_sta_own_data (ctx);
    cp_sta_own_data_set_hfid (sta->hfid_user, data);
}

/**
 * Set the Manufacturer HFID.
 *
 * \param  ctx  the module context.
 * \param  data  the human identifier.
 */
void
cp_sta_own_data_set_hfid_manufacturer (cp_t *ctx, const char* data)
{
    cp_sta_own_data_t *sta;

    dbg_assert (ctx);
    sta = cp_sta_mgr_get_sta_own_data (ctx);
    cp_sta_own_data_set_hfid (sta->hfid_manufacturer, data);
}

/**
 * Set the GLID to send proxy data.
 * \param  ctx  the module context.
 * \param  glid  The GLID to use when a proxy beacon shall be sent.
 */
void
cp_sta_own_data_set_pco_glid (cp_t *ctx, u8 glid)
{
    dbg_assert (ctx);
    dbg_assert ((glid >= MAC_GLID_MIN) || (glid == 0));

    ctx->sta_mgr.sta_own_data.pco_glid = glid;
}

/**
 * Get the PCO GLID.
 * \param  ctx  the module context.
 * \return  The GLID to send proxy data.
 */
u8
cp_sta_own_data_get_pco_glid (cp_t *ctx)
{
    dbg_assert (ctx);

    return (ctx->sta_mgr.sta_own_data.pco_glid);
}

/**
 * Get the PCO status.
 * \param  ctx  the module context.
 * \return  The PCO status.
 *
 */
bool
cp_sta_own_data_get_pco_status (cp_t *ctx)
{
    dbg_assert (ctx);

    return (ctx->sta_mgr.sta_own_data.pco_glid);
}

/**
 * Set the PCO status.
 * \param  ctx  the module context.
 * \param  pco  The PCO status.
 *
 */
void
cp_sta_own_data_set_pco_status (cp_t *ctx, bool pco)
{
    dbg_assert (ctx);

    if (cp_sta_own_data_get_pco_status (ctx) != pco)
    {
        if (pco == false)
            ctx->sta_mgr.sta_own_data.pco_glid = 0;
        else
            ctx->sta_mgr.sta_own_data.pco_glid |= (pco << 7);
        cp_fsm_post_new_event (ctx, bare, sta_status_changed);
    }
}

/**
 * Set the CCo status of the station.
 * \param  ctx  the CP context.
 */
void
cp_sta_own_data_set_cco_status (cp_t *ctx, bool cco)
{
    cp_sta_t *sta;
    cp_net_t *net;
    dbg_assert (ctx);
    if (ctx->sta_mgr.sta_own_data.is_cco != cco)
    {
        dbg_assert (ctx->sta_mgr.sta_own_data.tei != MAC_TEI_UNASSOCIATED);
        ctx->sta_mgr.sta_own_data.is_cco = cco;

        // If the net has already a CCo.
        if (cco)
        {
            net = cp_sta_mgr_get_our_avln (ctx);
            sta = cp_net_get_cco (ctx, net);
            if (sta)
            {
                cp_net_set_cco (ctx, net, MAC_TEI_UNASSOCIATED);
                slab_release (sta);
            }
        }

        cp_fsm_post_new_event (ctx, bare, sta_status_changed);
    }
}


/**
 * Get the CCo status of the station.
 * \param  ctx  the CP context.
 * \return  the boolean indicating the state.
 */
bool
cp_sta_own_data_get_cco_status (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.is_cco;

}

/**
 * Set the authentication status.
 * \param  ctx  the CP context.
 * \param  authenticated  the station status.
 */
void
cp_sta_own_data_set_authenticated_status (cp_t *ctx, bool authenticated)
{
    dbg_assert (ctx);
    dbg_assert (ctx->mac_config);
    if (ctx->sta_mgr.sta_own_data.authenticated != authenticated)
    {
        ctx->sta_mgr.sta_own_data.authenticated = authenticated;
        ctx->mac_config->authenticated = authenticated;
        cp_fsm_post_new_event (ctx, bare, sta_status_changed);
    }
}

/**
 * Get the authentication status.
 * \param  ctx  the CP context.
 * \return  the station authentication status.
 */
bool
cp_sta_own_data_get_authenticated_status (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.authenticated;
}

/**
 * Set the Station NID.
 * \param  ctx  the Control plane context.
 * \param  nid  the station NID.
 */
void
cp_sta_own_data_set_nid (cp_t *ctx, cp_nid_t nid)
{
    dbg_assert (ctx);
    dbg_assert (HPAV_NID_IS_VALID (nid));

    ctx->sta_mgr.sta_own_data.nid = nid;

    /* If the station AVLN is setted, modified the network. */
    if (ctx->sta_mgr.our_avln)
        ctx->sta_mgr.our_avln->nid = nid;

    /* Change the Security Level. */
    cp_sta_own_data_set_security_level (ctx, nid >> (HPAV_NID_SIZE_BITS - 2));

    bsu_update_nid (ctx->bsu, nid);
}

/**
 * Get the Station NID.
 * \param  ctx  the Control plane context.
 * \param  nid  the station NID.
 */
cp_nid_t
cp_sta_own_data_get_nid (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.nid;
}

/**
 * Set the Station SNID.
 * \param  ctx  the Control plane context.
 * \param  snid  the station SNID.
 */
void
cp_sta_own_data_set_snid (cp_t *ctx, cp_snid_t snid)
{
    dbg_assert (ctx);

    ctx->sta_mgr.sta_own_data.snid = snid;

    if (ctx->sta_mgr.our_avln)
        ctx->sta_mgr.our_avln->snid = snid;
}

/**
 * Get the Station SNID.
 * \param  ctx  the Control plane context.
 * \return  the station SNID.
 */
cp_snid_t
cp_sta_own_data_get_snid (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.snid;
}

void
cp_sta_own_data_set_tek (cp_t *ctx, const cp_key_t key)
{
    dbg_assert (ctx);

    memcpy (&ctx->sta_mgr.sta_own_data.tek, &key, sizeof (cp_key_t));
}

cp_key_t
cp_sta_own_data_get_tek (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.tek;
}

bool
cp_sta_own_data_get_backup_cco_status (cp_t *ctx)
{
    /* FIXME: this is a temporary fix. As soon we have implement CCo backup
     * functionality, we should return it. */
    return CP_BACKUP_CCO_CAP;
}

void
cp_sta_own_data_set_dak (cp_t *ctx, cp_key_t dak)
{
    uint i;
    dbg_assert (ctx);

    for (i = 0; i < COUNT (dak.key); i++)
        ctx->sta_mgr.sta_own_data.dak.key[i] = dak.key[i];
}

cp_key_t
cp_sta_own_data_get_dak (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.dak;
}

void
cp_sta_own_data_set_sc (cp_t *ctx, bool status)
{
    dbg_assert (ctx);

    if (ctx->sta_mgr.sta_own_data.sc != status)
    {
        ctx->sta_mgr.sta_own_data.sc = status;
        cp_fsm_post_new_event (ctx, bare, sta_status_changed);
    }
}

bool
cp_sta_own_data_get_sc (cp_t *ctx)
{
    dbg_assert (ctx);

    return ctx->sta_mgr.sta_own_data.sc;
}