summaryrefslogtreecommitdiff
path: root/cesar/cp/sta/action/src/assoc.c
blob: c56a60744e4c30c33a33f4e98dbbb2651af36503 (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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/sta/action/src/assoc.c
 * \brief   STA action, association related definitions.
 * \ingroup cp_sta_action
 */
#include "common/std.h"

#include "action.h"

#include "cp/inc/context.h"
#include "cp/fsm/fsm.h"
#include "cp/msg/msg.h"
#include "cp/beacon/beacon.h"
#include "cp/beacon/ntb/ntb.h"
#include "lib/rnd.h"
#include "cp/secu/defs.h"

#define RETRY_TIMEOUT_MS 1000
#define RENEW_MARGIN_MS (5 * 60 * 1000)
#define LEAVING_TIMEOUT_MS (40 * 3)
#define LEAVE_WAIT_TIMEOUT_MS 1000

void
cp_sta_action_assoc_init (cp_t *ctx)
{
    dbg_assert (ctx);
    ctx->sta_action.assoc.cco_net = NULL;
}

void
cp_sta_action_sc_assoc_start (cp_t *ctx, cp_net_t *cco_net, cp_sta_t *cco)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (cco_net);
    /* cco can be NULL. */

    /* Check NID. */
    dbg_assert (cp_sta_own_data_get_nid (ctx)
                == cp_net_get_nid (ctx, cco_net));
    /* Store CCo net. */
    ctx->sta_action.assoc.cco_net = cco_net;

    /* Do we know the CCo? */
    if (cco)
        /* Store CCo peer. */
        cp_sta_get_peer (cco, &ctx->sta_action.assoc.peer);
    else
    {
        /* Let's try to find the CCo from the net. */
        cp_sta_t *cco_from_net = cp_net_get_cco (ctx, cco_net);
        if (cco_from_net)
        {
            /* Get peer CCo. */
            cp_sta_get_peer (cco_from_net, &ctx->sta_action.assoc.peer);
            /* Release. */
            slab_release (cco_from_net);
        }
        else
        {
            /* We do not know the CCo, broadcast to all STA. */
            ctx->sta_action.assoc.peer = CP_MME_PEER (MAC_BROADCAST,
                                                      MAC_TEI_UNASSOCIATED);
        }
    }
    ctx->sta_action.assoc.retry = 3;
    /* Trigger to_sc_assoc event. */
    cp_fsm_trigger_new_event (ctx, bare, to_sc_assoc);
}

void
cp_sta_action_assoc_start (cp_t *ctx, cp_net_t *cco_net, cp_sta_t *cco)
{
    dbg_assert (ctx);
    dbg_assert (cco_net);
    dbg_assert (cco);
    /* Remember CCo to associate with. */
    dbg_assert (cp_sta_own_data_get_nid (ctx)
                == cp_net_get_nid (ctx, cco_net));
    ctx->sta_action.assoc.cco_net = cco_net;
    cp_sta_get_peer (cco, &ctx->sta_action.assoc.peer);
    ctx->sta_action.assoc.retry = 3;
    /* Trigger to_assoc event. */
    cp_fsm_trigger_new_event (ctx, bare, to_assoc);
}

void
cp_sta_action_assoc_leave (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Trigger to_leave event. */
    cp_fsm_trigger_new_event (ctx, bare, to_leave);
}

void
cp_sta_action_assoc__start_retry_timer (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_fsm_event_t *event = cp_fsm_event_bare_new (
        ctx, CP_FSM_EVENT_TYPE_assoc_timeout);
    cp_sta_core_gen_timed_event (ctx, &ctx->sta_action.assoc.timer, event,
                                 RETRY_TIMEOUT_MS);
}

void
cp_sta_action_assoc__stop_retry_timer (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.assoc.timer);
}

void
cp_sta_action_assoc__unassociated__enter (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_sta_own_data_set_authenticated_status (ctx, false);
    cp_sta_mgr_set_our_avln (ctx, NULL);
    cp_sta_own_data_set_tei (ctx, 0);
// TODO    cp_beacon_ntb_clear (ctx);
    cp_fsm_trigger_new_event (ctx, bare, left);
}

void
cp_sta_action_assoc__unassociated__to_assoc (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Send association request. */
    cp_msg_cc_assoc_req_t data = {
        CP_MSG_CC_ASSOC_REQ_TYPE_NEW,
        cp_sta_own_data_get_nid (ctx),
        CP_CCO_LEVEL,
        CP_PCO_CAP
    };
    cp_msg_cc_assoc_req_send (ctx, &ctx->sta_action.assoc.peer, &data);
}

void
cp_sta_action_assoc__wait_assoc_cnf__cc_assoc_cnf__common (cp_t *ctx,
                                                           cp_mme_rx_t *mme,
                                                           bool sc)
{
    cp_msg_cc_assoc_cnf_t cnf;
    dbg_assert (ctx);
    dbg_assert (mme);
    /* Check response. */
    if ((mme->peer.mac != ctx->sta_action.assoc.peer.mac
	 && ctx->sta_action.assoc.peer.mac != MAC_BROADCAST)
	|| mme->peer.tei == 0
	|| !cp_msg_cc_assoc_cnf_receive (ctx, mme, &cnf)
	|| cnf.nid != cp_sta_own_data_get_nid (ctx))
    {
        /* Unrelated message, drop. */
        if (!sc)
            cp_fsm_branch (ctx, WAIT_ASSOC_CONF, CC_ASSOC_CNF, unrelated);
        else
            cp_fsm_branch (ctx, SC_WAIT_ASSOC_CONF, CC_ASSOC_CNF, unrelated);
    }
    else if (cnf.result != CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS)
    {
        /* Failure indication. */
        if (!sc)
            cp_fsm_branch (ctx, WAIT_ASSOC_CONF, CC_ASSOC_CNF, nok);
        else
            cp_fsm_branch (ctx, SC_WAIT_ASSOC_CONF, CC_ASSOC_CNF, nok);
    }
    else
    {
        cp_net_t *our_net;
        cp_sta_own_data_t *own_data = cp_sta_mgr_get_sta_own_data (ctx);
        /* Start lease timer, it can not expire before we go to authenticated
         * state because authentication timeout is shorter. */
        int lease_time_ms = cnf.lease_time_min * 60 * 1000 - RENEW_MARGIN_MS;
        if (lease_time_ms > 0)
        {
            cp_fsm_event_t *event = cp_fsm_event_bare_new (
                ctx, CP_FSM_EVENT_TYPE_renew);
            cp_sta_core_gen_timed_event (ctx,
                                         &ctx->sta_action.assoc.lease_timer,
                                         event, lease_time_ms);
        }
        /* Update state. */
        cp_sta_own_data_set_tei (ctx, cnf.sta_tei);
        /* Set our net to the CCo net. */
        our_net = ctx->sta_action.assoc.cco_net;
        cp_sta_mgr_set_our_avln (ctx, our_net);
        /* Promote UCCo to CCo.
           Or the CCo's mac address was not know. */
        if ((ctx->sta_action.assoc.peer.tei == MAC_TEI_UNASSOCIATED)
            || (ctx->sta_action.assoc.peer.mac == MAC_BROADCAST))
        {
            cp_sta_t *cco = cp_sta_mgr_sta_add (ctx, our_net,
                                                mme->peer.tei,
                                                mme->peer.mac);
            cp_net_set_cco (ctx, our_net, mme->peer.tei);
            ctx->sta_action.assoc.peer = mme->peer;
            slab_release (cco);
        }
        /* Not in SC procedure. */
        if (!sc)
        {
            /* Send get key request. */
            cp_msg_cm_get_key_req_t get_key = {
                .relayed = false, .key_type = CP_MSG_KEY_NEK,
                .nid = cp_sta_own_data_get_nid (ctx) };
            cp_secu_protocol_run_new (&ctx->sta_action.assoc.prun, 0,
                                      &ctx->rnd);
            cp_msg_cm_get_key_req_send (ctx, &mme->peer, CP_MME_PEKS_NMK,
                                        &ctx->sta_action.assoc.prun, &get_key);
            /* Change state. */
            cp_fsm_branch (ctx, WAIT_ASSOC_CONF, CC_ASSOC_CNF, ok);
        }
        else
        {
            /* In SC procedure. */
            /* Ensure SC peer is correct, so let's copy it back (to handle TEI
             * change (UCCO to CCO) and/or MAC change (broadcast). */
            ctx->sta_action.sc.peer = mme->peer;
            /* Nothing to send. */
            /* Change state. */
            cp_fsm_branch (ctx, SC_WAIT_ASSOC_CONF, CC_ASSOC_CNF, ok);
        }
        /* Set the TEI track to the CCo's TEI to keep synchronisation. */
        own_data->tei_track = ctx->sta_action.assoc.peer.tei;
    }
}

void
cp_sta_action_assoc__wait_assoc_cnf__cc_assoc_cnf (cp_t *ctx,
                                                   cp_mme_rx_t *mme)
{
    cp_sta_action_assoc__wait_assoc_cnf__cc_assoc_cnf__common (ctx, mme,
                                                               false);
}

void
cp_sta_action_assoc__sc_wait_assoc_cnf__cc_assoc_cnf (cp_t *ctx,
                                                         cp_mme_rx_t *mme)
{
    cp_sta_action_assoc__wait_assoc_cnf__cc_assoc_cnf__common (ctx, mme,
                                                               true);
}

void
cp_sta_action_assoc__wait_assoc_cnf__timeout_common (cp_t *ctx, bool sc)
{
    dbg_assert (ctx);
    ctx->sta_action.assoc.retry--;
    if (ctx->sta_action.assoc.retry)
    {
        /* Restart timer. */
        cp_sta_action_assoc__start_retry_timer (ctx);
        /* Resend association request. */

        cp_sta_action_assoc__unassociated__to_assoc (ctx);
        if (!sc)
            cp_fsm_branch (ctx, WAIT_ASSOC_CONF, assoc_timeout, retry);
        else
            cp_fsm_branch (ctx, SC_WAIT_ASSOC_CONF, assoc_timeout, retry);
    }
    else
    {
        /* Give up. */
        if (!sc)
            cp_fsm_branch (ctx, WAIT_ASSOC_CONF, assoc_timeout, no_retry);
        else
            cp_fsm_branch (ctx, SC_WAIT_ASSOC_CONF, assoc_timeout, no_retry);
    }
}

void
cp_sta_action_assoc__wait_assoc_cnf__timeout (cp_t *ctx)
{
    cp_sta_action_assoc__wait_assoc_cnf__timeout_common (ctx, false);
}

void
cp_sta_action_assoc__sc_wait_assoc_cnf__timeout (cp_t *ctx)
{
    cp_sta_action_assoc__wait_assoc_cnf__timeout_common (ctx, true);
}

void
cp_sta_action_assoc__cm_get_key_cnf__common (cp_t *ctx, cp_mme_rx_t
                                             *mme, bool sc)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    cp_msg_cm_get_key_cnf_t cnf;
    /* Check response. */
    if (!cp_mme_peer_cmp (&mme->peer, &ctx->sta_action.assoc.peer)
        || !cp_msg_cm_get_key_cnf_receive (ctx, mme, &cnf)
        || (!cp_secu_protocol_check
            (&ctx->sta_action.assoc.prun, &mme->prun,
             CP_SECU_PROTOCOL_RUN_CHECK_RESULT_LAST))
        || cnf.nid != cp_sta_own_data_get_nid (ctx)
        || cnf.key_type != CP_MSG_KEY_NEK
        || (sc && mme->peks != CP_MME_PEKS_NMK))
    {
        /* Unrelated message, drop. */
        if (!sc)
            cp_fsm_branch (ctx, ASSOCIATED, CM_GET_KEY_CNF_PID0, unrelated);
        else
            cp_fsm_branch (ctx, SC_NMK_EXCHANGED, CM_GET_KEY_CNF_PID0, unrelated);
    }
    else if (cnf.result != CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED)
    {
        /* Failure indication. */
        if (!sc)
            cp_fsm_branch (ctx, ASSOCIATED, CM_GET_KEY_CNF_PID0, nok);
        else
            cp_fsm_branch (ctx, SC_NMK_EXCHANGED, CM_GET_KEY_CNF_PID0, nok);
    }
    else
    {
        /* Use the new key. */
        cp_sta_own_data_set_authenticated_status (ctx, true);
        cp_beacon_change_nek (ctx, cnf.eks, cnf.key, true);
        /* Signal joined state. */
        cp_fsm_trigger_new_event (ctx, bare, joined);
        /* Reset beacon loss counter. */
        ctx->sta_action.assoc.beacon_loss = 0;
        if (!sc)
            cp_fsm_branch (ctx, ASSOCIATED, CM_GET_KEY_CNF_PID0, ok);
        else
            cp_fsm_branch (ctx, SC_NMK_EXCHANGED, CM_GET_KEY_CNF_PID0, ok);
    }
}

void
cp_sta_action_assoc__sc_nmk_exchanged__cm_get_key_cnf_pid_0 (cp_t *ctx,
                                                             cp_mme_rx_t *
                                                             mme)
{
    cp_sta_action_assoc__cm_get_key_cnf__common (ctx, mme, true);
}

void
cp_sta_action_assoc__associated__cm_get_key_cnf_pid_0 (cp_t *ctx,
                                                       cp_mme_rx_t *mme)
{
    cp_sta_action_assoc__cm_get_key_cnf__common (ctx, mme, false);
}

void
cp_sta_action_assoc__associated__timeout (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Stop lease timer. */
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.assoc.lease_timer);
}

void
cp_sta_action_assoc__associated__to_leave (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Stop lease timer. */
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.assoc.lease_timer);
}

void
cp_sta_action_assoc__sc_associated__cm_get_key_req_pid_3 (cp_t *ctx,
                                                          cp_mme_rx_t *mme)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (mme);

    cp_msg_cm_get_key_req_t req;
    /* Check response (note, the MME should be sent by the STA with which are
     * doing the SC (it can be different from the CCo); */
    if (!cp_mme_peer_cmp (&mme->peer, &ctx->sta_action.sc.peer)
        || !cp_msg_cm_get_key_req_receive (ctx, mme, &req)
        || (!cp_secu_protocol_check
            (NULL, &mme->prun,
             CP_SECU_PROTOCOL_RUN_CHECK_RESULT_NEW))
        || (req.nid != cp_sta_own_data_get_nid (ctx))
        || (req.key_type != CP_MSG_KEY_HASH_KEY)
        || (mme->peks != CP_MME_PEKS_SPC_NOT_EMBEDDED)
        )
    {
        /* Unrelated message, drop it. */
        cp_fsm_branch (ctx, SC_ASSOCIATED, CM_GET_KEY_REQ_PID3, unrelated);
    }
    else
    {
        /* Message is valid, reply. */
        /* Copy security protocol run for authentication. */
        ctx->sta_action.assoc.prun = mme->prun;
        /* Prepare message. */
        cp_msg_cm_get_key_cnf_t cnf;
        cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED;
        cnf.key_type = CP_MSG_KEY_HASH_KEY;
        cnf.nid = cp_sta_own_data_get_nid (ctx);
        cnf.eks = CP_MME_PEKS_TEK_MIN;
        /* Generate an hash key. */
        cp_secu_generate_hash (ctx, lib_rnd32 (&ctx->rnd), cnf.hash_key,
                                  CP_SECU_HASH_KEY_FOR_TEK_SIZE);
        /* Generate TEK with hash key from CM_HET_KEY REQ and CNF. */
        cp_secu_tek_gen ((u32 *)req.hash_key, (u32 *)cnf.hash_key,
                         &ctx->sta_action.sc.tek);
        /* Send CM_GET_KEY.CNF. */
        cp_secu_protocol_next (&ctx->sta_action.assoc.prun, &ctx->rnd, false);
        cp_msg_cm_get_key_cnf_send (ctx, &ctx->sta_action.sc.peer,
                                    CP_MME_PEKS_SPC_NOT_EMBEDDED,
                                    &ctx->sta_action.assoc.prun,
                                    &cnf);
        /* Set TEK for encryption/decryption of cm_encrypted payload. */
        cp_sta_own_data_set_tek (ctx, ctx->sta_action.sc.tek);
        cp_fsm_branch (ctx, SC_ASSOCIATED, CM_GET_KEY_REQ_PID3, ok);
    }
}

void
cp_sta_action_assoc__sc_tek_exchanged__cm_set_key_req_pid_3 (cp_t *ctx,
                                                             cp_mme_rx_t
                                                             *mme)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (mme);

    cp_msg_cm_set_key_req_t req;
    /* Check response (note, the MME should be sent by the STA with which are
     * doing the SC (it can be different from the CCo); */
    if (!cp_mme_peer_cmp (&mme->peer, &ctx->sta_action.sc.peer)
        || !cp_msg_cm_set_key_req_receive (ctx, mme, &req)
        || (!cp_secu_protocol_check
            (&ctx->sta_action.assoc.prun, &mme->prun,
             CP_SECU_PROTOCOL_RUN_CHECK_RESULT_NEXT))
        || (req.nid != cp_sta_own_data_get_nid (ctx))
        || (req.key_type != CP_MSG_KEY_NMK)
        )
    {
        /* Unrelated message, drop it. */
        cp_fsm_branch (ctx, SC_TEK_EXCHANGED, CM_SET_KEY_REQ_PID3, unrelated);
    }
    else
    {
        /* Message is valid. */
        /* Set NMK. */
        cp_sta_own_data_set_nmk (ctx, req.new_key);
        /* Copy security protocol run for authentication. */
        ctx->sta_action.assoc.prun = mme->prun;
        /* Prepare message for reply. */
        cp_msg_cm_set_key_cnf_t cnf;
        cnf.result = CP_MSG_CM_SET_KEY_CNF_RESULT_SUCCESS;
        cnf.cco_cap = CP_CCO_LEVEL;
        /* Send CM_SET_KEY.CNF. */
        cp_secu_protocol_next (&ctx->sta_action.assoc.prun, &ctx->rnd, true);
        cp_msg_cm_set_key_cnf_send (ctx, &ctx->sta_action.sc.peer,
                                    CP_MME_PEKS_NMK,
                                    &ctx->sta_action.assoc.prun,
                                    &cnf);
        /* Start authentication. */
        cp_msg_cm_get_key_req_t get_key = {
            .relayed = false, .key_type = CP_MSG_KEY_NEK,
            .nid = cp_sta_own_data_get_nid (ctx) };
        cp_secu_protocol_run_new (&ctx->sta_action.assoc.prun, 0,
                                  &ctx->rnd);
        cp_msg_cm_get_key_req_send (ctx, &ctx->sta_action.assoc.peer,
                                    CP_MME_PEKS_NMK,
                                    &ctx->sta_action.assoc.prun, &get_key);
        cp_fsm_branch (ctx, SC_TEK_EXCHANGED, CM_SET_KEY_REQ_PID3, ok);
    }
}

void
cp_sta_action_assoc__authenticated__leave (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Stop lease timer. */
    cp_sta_core_stop_timed_or_cyclic_event (
        ctx, &ctx->sta_action.assoc.lease_timer);
}

void
cp_sta_action_assoc__authenticated__renew (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Restart timer. */
    cp_fsm_event_t *event = cp_fsm_event_bare_new (
        ctx, CP_FSM_EVENT_TYPE_renew);
    cp_sta_core_gen_timed_event (ctx, &ctx->sta_action.assoc.lease_timer,
                                 event, RENEW_MARGIN_MS / 2);
    /* Get our CCo. */
    cp_net_t *our_net = cp_sta_mgr_get_our_avln (ctx);
    cp_mme_peer_t peer;
    cp_net_get_cco_peer (ctx, our_net, &peer);
    /* Send CC_ASSOC.REQ to renew lease. */
    cp_msg_cc_assoc_req_t req = {
        CP_MSG_CC_ASSOC_REQ_TYPE_RENEW,
        cp_sta_own_data_get_nid (ctx),
        CP_CCO_LEVEL, CP_PCO_CAP
    };
    cp_msg_cc_assoc_req_send (ctx, &peer, &req);
}

void
cp_sta_action_assoc__authenticated__cc_assoc_cnf (cp_t *ctx,
                                                  cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    /* Get our CCo. */
    cp_net_t *our_net = cp_sta_mgr_get_our_avln (ctx);
    cp_mme_peer_t peer;
    cp_net_get_cco_peer (ctx, our_net, &peer);
    /* Check received message. */
    cp_msg_cc_assoc_cnf_t cnf;
    if (cp_mme_peer_cmp (&mme->peer, &peer)
        && cp_msg_cc_assoc_cnf_receive (ctx, mme, &cnf)
        && cnf.result == CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS
        && cnf.nid == cp_sta_own_data_get_nid (ctx))
    {
        /* Update lease. */
        cp_sta_core_stop_timed_or_cyclic_event (
            ctx, &ctx->sta_action.assoc.lease_timer);
        int lease_time_ms = cnf.lease_time_min * 60 * 1000 - RENEW_MARGIN_MS;
        if (lease_time_ms > 0)
        {
            cp_fsm_event_t *event = cp_fsm_event_bare_new (
                ctx, CP_FSM_EVENT_TYPE_renew);
            cp_sta_core_gen_timed_event (ctx,
                                         &ctx->sta_action.assoc.lease_timer,
                                         event, lease_time_ms);
        }
    }
}

void
cp_sta_action_assoc__authenticated__cm_set_key_req_pid_1 (cp_t *ctx,
                                                          cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    /* Get our CCo. */
    cp_net_t *our_net = cp_sta_mgr_get_our_avln (ctx);
    cp_mme_peer_t peer;
    cp_net_get_cco_peer (ctx, our_net, &peer);
    /* Check received message. */
    cp_msg_cm_set_key_req_t req;

    if (cp_mme_peer_cmp (&mme->peer, &peer)
        && cp_msg_cm_set_key_req_receive (ctx, mme, &req)
        && req.nid == cp_sta_own_data_get_nid (ctx))
    {
        if ((req.key_type == CP_MSG_KEY_NEK)
            && (mme->peks == CP_MME_PEKS_NMK)
            && (cp_secu_protocol_check
                (&ctx->sta_action.nek_prun, &mme->prun,
                 CP_SECU_PROTOCOL_RUN_CHECK_RESULT_NEXT)))
        {
            ctx->sta_action.nek_prun = mme->prun;
            /* Send response. */
            cp_msg_cm_set_key_cnf_t cnf = {
                CP_MSG_CM_SET_KEY_CNF_RESULT_SUCCESS,
                CP_CCO_LEVEL };
            cp_secu_protocol_next (&ctx->sta_action.nek_prun, &ctx->rnd,
                                   true);
            cp_msg_cm_set_key_cnf_send (ctx, &peer, CP_MME_PEKS_NMK,
                                        &ctx->sta_action.nek_prun,
                                        &cnf);
            /* Update NEK. */
            cp_beacon_change_nek (ctx, req.new_eks, req.new_key, false);
        }
        else if ((req.key_type == CP_MSG_KEY_NONCE_ONLY)
            && (mme->peks == CP_MME_PEKS_SPC_NOT_EMBEDDED
                || mme->peks == CP_MME_PEKS_NONE)
            && (cp_secu_protocol_check
                (NULL, &mme->prun, CP_SECU_PROTOCOL_RUN_CHECK_RESULT_NEW)))
        {
            /* Send response. */
            cp_msg_cm_set_key_cnf_t cnf = {
                CP_MSG_CM_SET_KEY_CNF_RESULT_SUCCESS,
                CP_CCO_LEVEL };

            ctx->sta_action.nek_prun = mme->prun;
            cp_secu_protocol_next (&ctx->sta_action.nek_prun, &ctx->rnd,
                                   false);
            ctx->sta_action.nek_prun.my_nonce = lib_rnd32 (&ctx->rnd);
            cp_msg_cm_set_key_cnf_send (ctx, &peer, mme->peks,
                                        &ctx->sta_action.nek_prun,
                                        &cnf);
        }
    }
    else
    {
        /* Send response. */
        cp_msg_cm_set_key_cnf_t cnf = {
            CP_MSG_CM_SET_KEY_CNF_RESULT_FAILURE,
            CP_CCO_LEVEL };
        cp_secu_protocol_run_t prun = mme->prun;
        prun.my_nonce = lib_rnd32 (&ctx->rnd);
        cp_secu_protocol_next (&prun, &ctx->rnd, true);
        cp_msg_cm_set_key_cnf_send (ctx, &peer, CP_MME_PEKS_NONE, &prun,
                                    &cnf);
    }
}

void
cp_sta_action_assoc__authenticated__to_leave (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Get our CCo. */
    cp_net_t *our_net = cp_sta_mgr_get_our_avln (ctx);
    cp_mme_peer_t peer;
    cp_net_get_cco_peer (ctx, our_net, &peer);
    /* Start timer. */
    cp_fsm_event_t *event = cp_fsm_event_bare_new (
        ctx, CP_FSM_EVENT_TYPE_assoc_timeout);
    cp_sta_core_gen_timed_event (ctx, &ctx->sta_action.assoc.timer, event,
                                 LEAVING_TIMEOUT_MS);
    /* Send leave request. */
    cp_msg_cc_leave_req_send (ctx, &peer,
                              CP_MSG_CC_LEAVE_REQ_REASON_USER_REQUEST);
}

void
cp_sta_action_assoc__authenticated__cc_leave_ind (cp_t *ctx,
                                                  cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    /* Get our CCo. */
    cp_net_t *our_net = cp_sta_mgr_get_our_avln (ctx);
    cp_mme_peer_t peer;
    cp_net_get_cco_peer (ctx, our_net, &peer);
    /* Check received message. */
    enum cp_msg_cc_leave_ind_reason_t reason;
    cp_nid_t nid;
    if (cp_mme_peer_cmp (&mme->peer, &peer)
        && cp_msg_cc_leave_ind_receive (ctx, mme, &reason, &nid)
        && nid == cp_sta_own_data_get_nid (ctx))
    {
        cp_msg_cc_leave_rsp_send (ctx, &mme->peer);
        cp_fsm_branch (ctx, AUTHENTICATED, CC_LEAVE_IND, ok);
    }
    else
    {
        cp_fsm_branch (ctx, AUTHENTICATED, CC_LEAVE_IND, nok);
    }
}

void
cp_sta_action_assoc__authenticated__beacon (cp_t *ctx,
                                            cp_beacon_desc_t *beacon,
                                            cp_net_t *net, cp_sta_t *sta)
{
    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (net);
    dbg_assert (sta);
    if (cp_net_get_nid (ctx, net) == cp_sta_own_data_get_nid (ctx))
    {
        /* Only on central beacon, no support for proxy networking. */
        if (cp_sta_get_cco_status (sta))
        {
            ctx->sta_action.assoc.beacon_loss = 0;
        }
    }
}

void
cp_sta_action_assoc__authenticated__beacon_not_received (cp_t *ctx)
{
    dbg_assert (ctx);
    ctx->sta_action.assoc.beacon_loss++;
    if (ctx->sta_action.assoc.beacon_loss > CP_MAX_NO_BEACON)
    {
        cp_fsm_trigger_new_event (ctx, bare, avln_failure);
        cp_fsm_branch (ctx, AUTHENTICATED, BEACON_NOT_RECEIVED, avln_failure);
    }
    else
        cp_fsm_branch (ctx, AUTHENTICATED, BEACON_NOT_RECEIVED, else);
}

void
cp_sta_action_assoc__leaving__cc_leave_cnf (cp_t *ctx, cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    /* Get our CCo. */
    cp_net_t *our_net = cp_sta_mgr_get_our_avln (ctx);
    cp_mme_peer_t peer;
    cp_net_get_cco_peer (ctx, our_net, &peer);
    /* Check received message. */
    if (cp_mme_peer_cmp (&mme->peer, &peer))
    {
        /* Ignore bad MME (confirmation is sufficient, we do not expect any
         * parameter). */
        cp_msg_cc_leave_cnf_receive (ctx, mme);
        /* Stop timer. */
        cp_sta_core_stop_timed_or_cyclic_event (
            ctx, &ctx->sta_action.assoc.timer);
        cp_fsm_branch (ctx, LEAVING, CC_LEAVE_CNF, ok);
    }
    else
    {
        cp_fsm_branch (ctx, LEAVING, CC_LEAVE_CNF, unrelated);
    }
}

void
cp_sta_action_assoc__leaving__timeout (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Get our CCo. */
    cp_net_t *our_net = cp_sta_mgr_get_our_avln (ctx);
    cp_mme_peer_t peer;
    cp_net_get_cco_peer (ctx, our_net, &peer);
    /* Send leave request again. */
    cp_msg_cc_leave_req_send (ctx, &peer,
                              CP_MSG_CC_LEAVE_REQ_REASON_USER_REQUEST);
}

void
cp_sta_action_assoc__leave_wait__enter (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Start timer. */
    cp_fsm_event_t *event = cp_fsm_event_bare_new (
        ctx, CP_FSM_EVENT_TYPE_assoc_timeout);
    cp_sta_core_gen_timed_event (ctx, &ctx->sta_action.assoc.timer, event,
                                 LEAVE_WAIT_TIMEOUT_MS);
}

void
cp_sta_action_assoc__leave_wait__timeout (cp_t *ctx)
{
    dbg_assert (ctx);
}