forked from cozybit/authsae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ampe.c
1600 lines (1399 loc) · 44.3 KB
/
ampe.c
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) cozybit Inc., 2011
*
* Copyright holder grants permission for redistribution and use in source
* and binary forms, with or without modification, provided that the
* following conditions are met:
* 1. Redistribution of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer
* in all source files.
* 2. Redistribution in binary form must retain the above copyright
* notice, this list of conditions, and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* "DISCLAIMER OF LIABILITY
*
* THIS SOFTWARE IS PROVIDED BY DAN HARKINS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INDUSTRIAL LOUNGE BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE."
*
*/
#include "ampe.h"
#include <assert.h>
#include <errno.h>
#include <openssl/rand.h>
#include <string.h>
#include "chan.h"
#include "peer_lists.h"
#include "peers.h"
#include "rekey.h"
#include "sae.h"
/* Peer link cancel reasons */
#define MESH_LINK_CANCELLED 52
#define MESH_MAX_NEIGHBORS 53
#define MESH_CAPABILITY_POLICY_VIOLATION 54
#define MESH_CLOSE_RCVD 55
#define MESH_MAX_RETRIES 56
#define MESH_CONFIRM_TIMEOUT 57
#define MESH_SECURITY_INVALID_GTK 58
#define MESH_SECURITY_INCONSISTENT_PARAMS 59
#define MESH_SECURITY_INVALID_CAPABILITY 60
/* Mesh protocol identifiers */
#define MESH_CONFIG_PP_HWMP 1
#define MESH_CONFIG_PM_ALM 1
#define MESH_CONFIG_CC_NONE 0
#define MESH_CONFIG_SP_NEIGHBOR_OFFSET 1
#define MESH_CONFIG_AUTH_SAE 1
#ifndef BIT
#define BIT(x) (1UL << (x))
#endif
#define MESH_CAPA_ACCEPT_PEERINGS BIT(0)
#define MESH_CAPA_FORWARDING BIT(3)
#define MIC_IE_BODY_SIZE AES_BLOCK_SIZE
static const unsigned char akm_suite_selector[4] = {0x0,
0xf,
0xac,
0x8}; /* SAE */
static const unsigned char pw_suite_selector[4] = {0x0,
0xf,
0xac,
0x4}; /* CCMP */
static const unsigned char null_nonce[32] = {0};
static struct ampe_cb *cb;
/* IEs */
static unsigned char *sta_fixed_ies;
static unsigned char sta_fixed_ies_len;
/* Mesh group temporal key */
unsigned char mgtk_tx[KEY_LEN_AES_CCMP];
/* global configuration data */
static struct ampe_config ampe_conf;
enum plink_event {
PLINK_UNDEFINED,
OPN_ACPT,
OPN_RJCT,
CNF_ACPT,
CNF_RJCT,
CLS_ACPT,
CLS_IGNR,
REQ_RJCT
};
/* For debugging use */
static const char *mpl_states[] = {[PLINK_LISTEN] = "LISTEN",
[PLINK_OPN_SNT] = "OPN-SNT",
[PLINK_OPN_RCVD] = "OPN-RCVD",
[PLINK_CNF_RCVD] = "CNF_RCVD",
[PLINK_ESTAB] = "ESTAB",
[PLINK_HOLDING] = "HOLDING",
[PLINK_BLOCKED] = "BLOCKED"};
static const char *mpl_events[] = {
[PLINK_UNDEFINED] = "PLINK_UNDEFINED",
[OPN_ACPT] = "OPN_ACPT",
[OPN_RJCT] = "OPN_RJCT",
[CNF_ACPT] = "CNF_ACPT",
[CNF_RJCT] = "CNF_RJCT",
[CLS_ACPT] = "CLS_ACPT",
[CLS_IGNR] = "CLS_IGNR",
[REQ_RJCT] = "REQ_RJCT",
};
static int plink_frame_tx(
struct candidate *cand,
enum plink_action_code action,
unsigned short reason);
static inline void set_link_state(
struct candidate *cand,
enum plink_state state) {
cand->link_state = state;
cb->set_plink_state(cand->peer_mac, state, cand->cookie);
}
static void *memdup(const void *src, size_t size) {
void *dst = malloc(size);
if (dst)
memcpy(dst, src, size);
return dst;
}
static int plink_estab_count() {
struct candidate *peer;
int count = 0;
for_each_peer(peer) {
if (peer->link_state == PLINK_ESTAB)
count++;
}
return count;
}
static int plink_free_count(struct mesh_node *mesh) {
return MAX(mesh->conf->max_plinks - plink_estab_count(), 0);
}
static inline unsigned char *
start_of_ies(struct ieee80211_mgmt_frame *frame, int len, u16 *ie_len) {
int offset = 0;
switch (frame->action.action_code) {
case PLINK_OPEN:
offset = 2;
break;
case PLINK_CONFIRM:
offset = 4;
break;
case PLINK_CLOSE:
offset = 0;
}
if (ie_len)
*ie_len = len - 24 - sizeof(frame->action) - offset;
return (frame->action.u.var8 + offset);
}
static void derive_aek(struct candidate *cand) {
unsigned char context[AES_BLOCK_SIZE];
memcpy(context, akm_suite_selector, 4);
if (memcmp(cand->my_mac, cand->peer_mac, ETH_ALEN) < 0) {
memcpy(context + 4, cand->my_mac, ETH_ALEN);
memcpy(context + 10, cand->peer_mac, ETH_ALEN);
} else {
memcpy(context + 4, cand->peer_mac, ETH_ALEN);
memcpy(context + 10, cand->my_mac, ETH_ALEN);
}
prf(cand->pmk,
SHA256_DIGEST_LENGTH,
(unsigned char *)"AEK Derivation",
strlen("AEK Derivation"),
context,
sizeof(context),
cand->aek,
SHA256_DIGEST_LENGTH * 8);
sae_hexdump(AMPE_DEBUG_KEYS, "aek context: ", context, sizeof(context));
sae_hexdump(AMPE_DEBUG_KEYS, "aek: ", cand->aek, sizeof(cand->aek));
}
/*
* Determine and set the correct ht operation mode for all established peers
* according to 802.11-2016 10.26.3.5.
*
* Return MESH_CONF_CHANGED_HT bit if a new operation mode was selected.
*/
static uint32_t mesh_set_ht_op_mode(struct mesh_node *mesh) {
struct candidate *peer;
uint32_t changed = 0;
unsigned int ht_opmode;
bool no_ht = false, ht20 = false;
if (mesh->conf->channel_width == CHAN_WIDTH_20_NOHT)
return 0;
for_each_peer(peer) {
if (peer->link_state != PLINK_ESTAB)
continue;
switch (peer->ch_width) {
case CHAN_WIDTH_20_NOHT:
no_ht = true;
goto out;
case CHAN_WIDTH_20:
ht20 = true;
break;
default:
break;
}
}
out:
if (no_ht)
ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
else if (ht20 && mesh->conf->channel_width > CHAN_WIDTH_20)
ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
else
ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
if (ht_opmode != mesh->conf->ht_prot_mode) {
sae_debug(MESHD_DEBUG, "changing ht protection mode to: %d\n", ht_opmode);
mesh->conf->ht_prot_mode = ht_opmode;
changed = MESH_CONF_CHANGED_HT;
}
return changed;
}
static void peer_ampe_init(
struct ampe_config *aconf,
struct candidate *cand,
void *cookie) {
le16 llid;
assert(cand);
RAND_bytes((unsigned char *)&llid, sizeof(llid));
RAND_bytes(cand->my_nonce, sizeof(cand->my_nonce));
memset(cand->peer_nonce, 0, sizeof(cand->peer_nonce));
cand->cookie = cookie;
cand->my_lid = llid;
cand->peer_lid = 0;
set_link_state(cand, PLINK_LISTEN);
cand->timeout = aconf->retry_timeout_ms;
cand->conf = aconf;
memset(cand->mtk, 0, sizeof(cand->mtk));
memset(cand->mgtk, 0, sizeof(cand->mgtk));
memset(cand->igtk, 0, sizeof(cand->igtk));
cand->has_igtk = false;
if (aconf->mesh->conf->is_secure) {
derive_aek(cand);
siv_init(&cand->sivctx, cand->aek, SIV_256);
}
}
/**
* fsm_restart - restart a mesh peer link finite state machine
*
* @cand: mesh peer link to restart
*
* */
static inline void fsm_restart(struct candidate *cand) {
sae_debug(
AMPE_DEBUG_FSM,
"Deleting peer " MACSTR " to restart FSM\n",
MAC2STR(cand->peer_mac));
if (cb->delete_peer)
cb->delete_peer(cand->peer_mac);
}
static void plink_timer(void *data) {
le16 reason;
struct candidate *cand;
cand = (struct candidate *)data;
assert(cand);
sae_debug(
AMPE_DEBUG_FSM,
"Mesh plink timer for " MACSTR " fired on state %s\n",
MAC2STR(cand->peer_mac),
mpl_states[(cand->link_state > PLINK_BLOCKED) ? PLINK_UNDEFINED
: cand->link_state]);
reason = 0;
switch (cand->link_state) {
case PLINK_OPN_RCVD:
case PLINK_OPN_SNT:
/* retry timer */
sae_debug(
AMPE_DEBUG_FSM,
"Mesh plink:retries %d of %d\n",
cand->retries,
cand->conf->max_retries);
if (cand->retries < cand->conf->max_retries) {
cand->timeout = cand->conf->retry_timeout_ms;
sae_debug(
AMPE_DEBUG_FSM,
"Mesh plink for " MACSTR " (retry, timeout): %d %d\n",
MAC2STR(cand->peer_mac),
cand->retries,
cand->timeout);
++cand->retries;
cb->evl->rem_timeout(cand->t2);
cand->t2 =
cb->evl->add_timeout(SRV_MSEC(cand->timeout), plink_timer, cand);
plink_frame_tx(cand, PLINK_OPEN, 0);
break;
}
reason = MESH_MAX_RETRIES;
/* no break / fall through on else */
case PLINK_CNF_RCVD:
/* confirm timer */
if (!reason)
reason = MESH_CONFIRM_TIMEOUT;
set_link_state(cand, PLINK_HOLDING);
cb->evl->rem_timeout(cand->t2);
cand->t2 = cb->evl->add_timeout(
SRV_MSEC(cand->conf->holding_timeout_ms), plink_timer, cand);
plink_frame_tx(cand, PLINK_CLOSE, reason);
break;
case PLINK_HOLDING:
/* holding timer */
fsm_restart(cand);
break;
case PLINK_ESTAB:
/* nothing to do */
break;
default:
sae_debug(
AMPE_DEBUG_FSM,
"Timeout for peer " MACSTR " in state %d\n",
MAC2STR(cand->peer_mac),
cand->link_state);
break;
}
}
/**
* protect_frame - add in-place the MIC and the (encrypted) AMPE ie to a frame
* @cand: The candidate this frame is destined for
* @mgmt: The frame, populated with all the information elements up to
* where the MIC information element should go
* @mic_start: Pointer to where the mic and AMPE ies are to be written. Should
* point to the start of the IE, not the IE body.
* @len: On input, the total buffer size that contains this frame. On
* output, the actual lenght of the frame
* including the two information elements added by this function.
*
* Returns: The zero on success, or some error.
*/
static int protect_frame(
struct candidate *cand,
struct ieee80211_mgmt_frame *mgmt,
unsigned char *mic_start,
int *len) {
unsigned char *clear_ampe_ie;
unsigned char *ie;
unsigned short cat_to_mic_len;
struct mesh_node *mesh = cand->conf->mesh;
size_t ampe_ie_len;
unsigned char ftype = mgmt->action.action_code;
le16 igtk_keyid;
assert(mic_start && cand && mgmt && len);
ampe_ie_len = sizeof(struct ampe_ie);
if (ftype == PLINK_OPEN) {
/* MGTK + RSC + Exp */
ampe_ie_len += 16 + 8 + 4;
if (mesh->conf->pmf) {
/* IGTK KeyId + IPN + IGTK */
ampe_ie_len += 2 + 6 + 16;
}
}
if (mic_start + MIC_IE_BODY_SIZE + 2 + 2 + ampe_ie_len -
(unsigned char *)mgmt >
*len) {
sae_debug(AMPE_DEBUG_KEYS, "protect frame: buffer too small\n");
return -EINVAL;
}
clear_ampe_ie = malloc(ampe_ie_len + 2);
if (!clear_ampe_ie) {
sae_debug(AMPE_DEBUG_KEYS, "protect frame: out of memory\n");
return -ENOMEM;
}
/* IE: AMPE */
ie = clear_ampe_ie;
*ie++ = IEEE80211_EID_AMPE;
*ie++ = ampe_ie_len;
memcpy(ie, pw_suite_selector, 4);
ie += 4;
memcpy(ie, cand->my_nonce, 32);
ie += 32;
memcpy(ie, cand->peer_nonce, 32);
ie += 32;
if (ftype == PLINK_OPEN) {
memcpy(ie, mgtk_tx, 16);
ie += 16;
memset(ie, 0, 8); /* TODO: Populate Key RSC */
ie += 8;
memset(ie, 0xff, 4); /* expire in 13 decades or so */
ie += 4;
if (mesh->conf->pmf) {
igtk_keyid = htole16(mesh->igtk_keyid);
memcpy(ie, &igtk_keyid, 2);
ie += 2;
memcpy(ie, mesh->igtk_ipn, 6);
ie += 6;
memcpy(ie, mesh->igtk_tx, 16);
ie += 16;
}
}
/* IE: MIC */
ie = mic_start;
*ie++ = IEEE80211_EID_MIC;
*ie++ = MIC_IE_BODY_SIZE;
cat_to_mic_len = mic_start - (unsigned char *)&mgmt->action;
siv_encrypt(
&cand->sivctx,
clear_ampe_ie,
ie + MIC_IE_BODY_SIZE,
ampe_ie_len + 2,
ie,
3,
cand->my_mac,
ETH_ALEN,
cand->peer_mac,
ETH_ALEN,
&mgmt->action,
cat_to_mic_len);
*len = mic_start - (unsigned char *)mgmt + ampe_ie_len + 2 +
MIC_IE_BODY_SIZE + 2;
sae_debug(
AMPE_DEBUG_KEYS,
"Protecting frame from " MACSTR " to " MACSTR "\n",
MAC2STR(cand->my_mac),
MAC2STR(cand->peer_mac));
sae_debug(
AMPE_DEBUG_KEYS,
"Checking tricky lengths of protected frame %d, %zu\n",
cat_to_mic_len,
ampe_ie_len + 2);
sae_hexdump(
AMPE_DEBUG_KEYS,
"SIV- Put AAD[3]: ",
(unsigned char *)&mgmt->action,
cat_to_mic_len);
free(clear_ampe_ie);
return 0;
}
static bool protection_is_valid(
struct candidate *cand,
struct ieee80211_mgmt_frame *mgmt,
int len,
struct info_elems *elems) {
unsigned char *clear_ampe_ie;
struct info_elems ies_parsed;
struct mesh_node *mesh = cand->conf->mesh;
unsigned short ampe_ie_len, cat_to_mic_len;
int r;
unsigned int *key_expiration_p;
unsigned char ftype = mgmt->action.action_code;
unsigned char *gtkdata, *igtkdata;
assert(len && cand && mgmt);
if (!mesh->conf->is_secure)
return true;
if (!elems->mic || elems->mic_len != MIC_IE_BODY_SIZE) {
sae_debug(AMPE_DEBUG_KEYS, "Verify frame: invalid MIC\n");
return false;
}
/*
* ampe_ie_len is the length of the ciphertext (the encrypted
* AMPE IE) and it needs to be inferred from the total frame
* size
*/
ampe_ie_len = len - (elems->mic + elems->mic_len - (unsigned char *)mgmt);
/* expect at least MGTK + RSC + expiry for open */
if (ftype == PLINK_OPEN &&
ampe_ie_len < 2 + sizeof(struct ampe_ie) + 16 + 8 + 4) {
sae_debug(AMPE_DEBUG_KEYS, "Verify frame: AMPE IE too small\n");
return false;
/* if PMF, then we also need IGTKData */
if (mesh->conf->pmf) {
if (ampe_ie_len <
2 + sizeof(struct ampe_ie) + 16 + 8 + 4 + 2 + 6 + 16 /* IGTKData */) {
sae_debug(AMPE_DEBUG_KEYS, "Verify frame: AMPE IE missing IGTK\n");
return false;
}
}
}
clear_ampe_ie = malloc(ampe_ie_len);
if (!clear_ampe_ie) {
sae_debug(AMPE_DEBUG_KEYS, "Verify frame: out of memory\n");
return false;
}
/*
* cat_to_mic_len is the length of the contents of the frame
* from the category (inclusive) to the mic (exclusive)
*/
cat_to_mic_len = elems->mic - 2 - (unsigned char *)&mgmt->action;
r = siv_decrypt(
&cand->sivctx,
elems->mic + elems->mic_len,
clear_ampe_ie,
ampe_ie_len,
elems->mic,
3,
cand->peer_mac,
ETH_ALEN,
cand->my_mac,
ETH_ALEN,
&mgmt->action,
cat_to_mic_len);
sae_debug(
AMPE_DEBUG_KEYS,
"Checking protection to " MACSTR " from " MACSTR "\n",
MAC2STR(cand->my_mac),
MAC2STR(cand->peer_mac));
sae_debug(
AMPE_DEBUG_KEYS,
"Len checking cat-to-mic len:%d ampe ie full length: %d\n",
cat_to_mic_len,
ampe_ie_len);
sae_hexdump(
AMPE_DEBUG_KEYS,
"SIV- Got AAD[3]: ",
(unsigned char *)&mgmt->action,
cat_to_mic_len);
if (r != 1) {
sae_debug(AMPE_DEBUG_KEYS, "Protection check failed\n");
free(clear_ampe_ie);
return false;
}
if (ampe_ie_len != clear_ampe_ie[1] + 2) {
sae_debug(
AMPE_DEBUG_KEYS,
"AMPE -Invalid length (expected %d, got %d)\n",
ampe_ie_len,
clear_ampe_ie[1] + 2);
free(clear_ampe_ie);
return false;
}
sae_hexdump(AMPE_DEBUG_KEYS, "AMPE IE: ", clear_ampe_ie, ampe_ie_len);
parse_ies(clear_ampe_ie, ampe_ie_len, &ies_parsed);
if (memcmp(ies_parsed.ampe->peer_nonce, null_nonce, 32) != 0 &&
memcmp(ies_parsed.ampe->peer_nonce, cand->my_nonce, 32) != 0) {
sae_hexdump(
AMPE_DEBUG_KEYS, "IE peer_nonce ", ies_parsed.ampe->peer_nonce, 32);
sae_debug(AMPE_DEBUG_KEYS, "Unexpected nonce\n");
free(clear_ampe_ie);
return false;
}
if (memcmp(cand->peer_nonce, null_nonce, 32) == 0) {
memcpy(cand->peer_nonce, ies_parsed.ampe->local_nonce, 32);
}
/* everything from here on out requires MGTKData */
if (ftype != PLINK_OPEN)
return true;
gtkdata = ies_parsed.ampe->variable;
memcpy(cand->mgtk, gtkdata, sizeof(cand->mgtk));
sae_hexdump(
AMPE_DEBUG_KEYS, "Received mgtk: ", cand->mgtk, sizeof(cand->mgtk));
key_expiration_p = (unsigned int *)(gtkdata + 16 + 8);
cand->mgtk_expiration = le32toh(*key_expiration_p);
igtkdata = gtkdata + 16 + 8 + 4;
if (mesh->conf->pmf) {
cand->igtk_keyid = le16toh(*(u16 *)igtkdata);
igtkdata += 2 + 6;
memcpy(cand->igtk, igtkdata, sizeof(cand->igtk));
cand->has_igtk = true;
sae_hexdump(
AMPE_DEBUG_KEYS, "Received igtk: ", cand->igtk, sizeof(cand->igtk));
}
free(clear_ampe_ie);
return true;
}
static int plink_frame_tx(
struct candidate *cand,
enum plink_action_code action,
unsigned short reason) {
unsigned char *buf;
struct ieee80211_mgmt_frame *mgmt;
struct mesh_node *mesh;
struct ieee80211_supported_band *sband;
struct ht_cap_ie *ht_cap;
struct ht_op_ie *ht_op;
struct vht_op_ie *vht_op;
unsigned char *ie_len_ptr;
int len;
int ret;
unsigned char *ies;
unsigned char *pos;
u16 peering_proto;
u16 close_reason;
size_t alloc_len;
int peer_count;
unsigned char mesh_capa;
assert(cand);
assert(cand->conf);
mesh = cand->conf->mesh;
sband = &mesh->bands[mesh->band];
alloc_len = sizeof(struct ieee80211_mgmt_frame) + 2 + /* capability info */
2 + /* aid */
sta_fixed_ies_len + 2 + mesh->conf->meshid_len + /* mesh id */
2 + 7 + /* mesh config */
2 + 8 + sizeof(cand->pmkid) + /* mesh peering management */
2 + sizeof(struct ht_cap_ie) + /* HT capabilities */
2 + sizeof(struct ht_op_ie) + /* HT operation */
2 + 12 + /* VHT capabilities */
2 + 5 + /* VHT operation */
2 + 120 + /* AMPE, without Key Replay counter, 16 byte keys */
2 + MIC_IE_BODY_SIZE; /* MIC */
buf = calloc(1, alloc_len);
if (!buf)
return -1;
sae_debug(AMPE_DEBUG_FSM, "Mesh plink: Sending plink action %d\n", action);
mgmt = (struct ieee80211_mgmt_frame *)buf;
mgmt->frame_control =
htole16((IEEE802_11_FC_TYPE_MGMT << 2 | IEEE802_11_FC_STYPE_ACTION << 4));
memcpy(mgmt->da, cand->peer_mac, ETH_ALEN);
memcpy(mgmt->sa, cand->my_mac, ETH_ALEN);
memcpy(mgmt->bssid, cand->my_mac, ETH_ALEN);
mgmt->action.category = IEEE80211_CATEGORY_SELF_PROTECTED;
mgmt->action.action_code = action;
pos = mgmt->action.u.var8;
if (action != PLINK_CLOSE) {
/* capability info */
*pos++ = (mesh->conf->is_secure) ? 0x10 : 0;
*pos++ = 0;
if (action == PLINK_CONFIRM) {
/* AID */
uint16_t *aid = (uint16_t *)pos;
*aid = ieee_order(cand->association_id);
pos += 2;
}
}
ies = start_of_ies(mgmt, len, NULL);
/* IE: All the static IEs */
memcpy(ies, sta_fixed_ies, sta_fixed_ies_len);
ies += sta_fixed_ies_len;
/* IE: Mesh ID element */
*ies++ = IEEE80211_EID_MESH_ID;
*ies++ = mesh->conf->meshid_len;
memcpy((char *)ies, mesh->conf->meshid, mesh->conf->meshid_len);
ies += mesh->conf->meshid_len;
/* IE: mesh config */
*ies++ = IEEE80211_EID_MESH_CONFIG;
*ies++ = 7;
*ies++ = MESH_CONFIG_PP_HWMP;
*ies++ = MESH_CONFIG_PM_ALM;
*ies++ = MESH_CONFIG_CC_NONE;
*ies++ = MESH_CONFIG_SP_NEIGHBOR_OFFSET;
if (mesh->conf->is_secure) {
*ies++ = MESH_CONFIG_AUTH_SAE;
} else {
*ies++ = 0;
}
/* formation info */
peer_count = plink_estab_count();
*ies++ = MIN(peer_count, 63) << 1;
mesh_capa = MESH_CAPA_FORWARDING;
if (peer_count < mesh->conf->max_plinks)
mesh_capa |= MESH_CAPA_ACCEPT_PEERINGS;
*ies++ = mesh_capa;
/* IE: Mesh Peering Management element */
*ies++ = IEEE80211_EID_MESH_PEERING;
ie_len_ptr = ies;
ies++;
if (mesh->conf->is_secure) {
peering_proto = htole16(1);
} else {
peering_proto = 0;
}
memcpy(ies, &peering_proto, 2);
ies += 2;
memcpy(ies, &cand->my_lid, 2);
ies += 2;
if (cand->peer_lid && (action != PLINK_OPEN)) {
memcpy(ies, &cand->peer_lid, 2);
ies += 2;
}
if (action == PLINK_CLOSE) {
close_reason = htole16(reason);
memcpy(ies, &close_reason, 2);
ies += 2;
}
if (mesh->conf->is_secure) {
memcpy(ies, cand->pmkid, sizeof(cand->pmkid));
ies += sizeof(cand->pmkid);
}
*ie_len_ptr = ies - ie_len_ptr - 1;
if (action != PLINK_CLOSE &&
mesh->conf->channel_width != CHAN_WIDTH_20_NOHT &&
sband->ht_cap.ht_supported) {
/* HT IEs */
*ies++ = IEEE80211_EID_HT_CAPABILITY;
*ies++ = sizeof(struct ht_cap_ie);
ht_cap = (struct ht_cap_ie *)ies;
ht_cap->cap_info = htole16(sband->ht_cap.cap);
ht_cap->ampdu_params_info =
sband->ht_cap.ampdu_factor | (sband->ht_cap.ampdu_density << 2);
memcpy(&ht_cap->mcs, &sband->ht_cap.mcs, sizeof(struct mcs_info));
/* mac80211 apparently ignores the rest */
ies += sizeof(*ht_cap);
*ies++ = IEEE80211_EID_HT_OPERATION;
*ies++ = sizeof(struct ht_op_ie);
ht_op = (struct ht_op_ie *)ies;
ht_op->primary_chan =
ieee80211_frequency_to_channel(mesh->conf->control_freq);
switch (mesh->conf->channel_width) {
case CHAN_WIDTH_40:
if (mesh->conf->center_freq1 < mesh->conf->control_freq)
ht_op->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
else
ht_op->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
break;
case CHAN_WIDTH_20:
default:
ht_op->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
break;
}
if ((sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
mesh->conf->channel_width > CHAN_WIDTH_20)
ht_op->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
ht_op->operation_mode = htole16(mesh->conf->ht_prot_mode);
memset(ht_op->basic_set, 0, 16);
ht_op->basic_set[0] = 0xff; /* mandatory HT phy rates */
ies += sizeof(*ht_op);
}
if (action != PLINK_CLOSE &&
mesh->conf->channel_width != CHAN_WIDTH_20_NOHT &&
sband->vht_cap.vht_supported) {
*ies++ = IEEE80211_EID_VHT_CAPABILITY;
*ies++ = sizeof(sband->vht_cap.cap) + sizeof(sband->vht_cap.mcs);
memcpy(ies, &sband->vht_cap.cap, sizeof(sband->vht_cap.cap));
ies += sizeof(sband->vht_cap.cap);
memcpy(ies, &sband->vht_cap.mcs, sizeof(sband->vht_cap.mcs));
ies += sizeof(sband->vht_cap.mcs);
*ies++ = IEEE80211_EID_VHT_OPERATION;
*ies++ = 5;
vht_op = (struct vht_op_ie *)ies;
switch (mesh->conf->channel_width) {
case CHAN_WIDTH_80:
case CHAN_WIDTH_80P80:
case CHAN_WIDTH_160:
vht_op->width = 1;
break;
default:
vht_op->width = 0;
}
vht_op->center_chan1 =
ieee80211_frequency_to_channel(mesh->conf->center_freq1);
vht_op->center_chan2 =
ieee80211_frequency_to_channel(mesh->conf->center_freq2);
/* TODO allow configuring this for mixed capability STAs;
* see 802.11-2016 11.40.7
*/
memcpy(
&vht_op->basic_set,
&sband->vht_cap.mcs.rx_mcs_mask,
sizeof(vht_op->basic_set));
ies += sizeof(*vht_op);
}
if (mesh->conf->is_secure) {
/* IE: Add MIC and encrypted AMPE */
len = alloc_len;
ret = protect_frame(cand, (struct ieee80211_mgmt_frame *)buf, ies, &len);
if (ret) {
sae_debug(SAE_DEBUG_ERR, "Failed to protect frame\n");
free(buf);
return ret;
}
} else {
len = ies - buf;
}
if (cb->meshd_write_mgmt((char *)buf, len, cand->cookie) != len) {
sae_debug(
SAE_DEBUG_ERR,
"can't send a peering "
"frame to " MACSTR "\n",
MAC2STR(cand->peer_mac));
}
free(buf);
return 0;
}
static void derive_mtk(struct candidate *cand) {
unsigned char context[84];
unsigned char *p;
if (!ampe_conf.mesh->conf->is_secure)
return;
p = context;
if (memcmp(cand->my_nonce, cand->peer_nonce, 32) < 0) {
memcpy(p, cand->my_nonce, 32);
memcpy(p + 32, cand->peer_nonce, 32);
} else {
memcpy(p, cand->peer_nonce, 32);
memcpy(p + 32, cand->my_nonce, 32);
}
p += 64;
if (le16toh(cand->my_lid) < le16toh(cand->peer_lid)) {
memcpy(p, &cand->my_lid, 2);
memcpy(p + 2, &cand->peer_lid, 2);
} else {
memcpy(p, &cand->peer_lid, 2);
memcpy(p + 2, &cand->my_lid, 2);
}
p += 4;
memcpy(p, akm_suite_selector, sizeof(akm_suite_selector));
p += sizeof(akm_suite_selector);
if (memcmp(cand->my_mac, cand->peer_mac, ETH_ALEN) < 0) {
memcpy(p, cand->my_mac, ETH_ALEN);
memcpy(p + ETH_ALEN, cand->peer_mac, ETH_ALEN);
} else {
memcpy(p, cand->peer_mac, ETH_ALEN);
memcpy(p + ETH_ALEN, cand->my_mac, ETH_ALEN);
}
p += 12;
assert(p - context <= sizeof(context));
prf(cand->pmk,
SHA256_DIGEST_LENGTH,
(unsigned char *)"Temporal Key Derivation",
strlen("Temporal Key Derivation"),
context,
sizeof(context),
cand->mtk,
16 * 8);
sae_hexdump(AMPE_DEBUG_KEYS, "mtk context: ", context, sizeof(context));
sae_hexdump(AMPE_DEBUG_KEYS, "mtk: ", cand->mtk, sizeof(cand->mtk));
}
static uint32_t do_estab_peer_link(struct candidate *cand) {
uint32_t changed;
derive_mtk(cand);
cb->estab_peer_link(
cand->peer_mac,
cand->mtk,
sizeof(cand->mtk),
cand->mgtk,
sizeof(cand->mgtk),
cand->mgtk_expiration,
(cand->has_igtk) ? cand->igtk : NULL,
(cand->has_igtk) ? sizeof(cand->igtk) : 0,
cand->igtk_keyid,
cand->sup_rates,
cand->sup_rates_len,
cand->cookie);
set_link_state(cand, PLINK_ESTAB);
changed = mesh_set_ht_op_mode(cand->conf->mesh);
sae_debug(
AMPE_DEBUG_FSM,
"Mesh plink with " MACSTR " ESTABLISHED\n",
MAC2STR(cand->peer_mac));
rekey_verify_peer(cand);
return changed;
}
static void fsm_step(struct candidate *cand, enum plink_event event) {
struct ampe_config *aconf = cand->conf;
unsigned short reason = 0;
uint32_t changed = 0;
switch (cand->link_state) {
case PLINK_LISTEN:
switch (event) {
case CLS_ACPT:
fsm_restart(cand);
break;
case OPN_ACPT:
cand->timeout = aconf->retry_timeout_ms;
cb->evl->rem_timeout(cand->t2);
cand->t2 =
cb->evl->add_timeout(SRV_MSEC(cand->timeout), plink_timer, cand);
set_link_state(cand, PLINK_OPN_RCVD);
plink_frame_tx(cand, PLINK_OPEN, 0);
plink_frame_tx(cand, PLINK_CONFIRM, 0);
break;
default:
break;
}
break;
case PLINK_OPN_SNT:
switch (event) {
case OPN_RJCT:
case CNF_RJCT:
case REQ_RJCT:
reason = MESH_CAPABILITY_POLICY_VIOLATION;
/* no break */
case CLS_ACPT:
if (!reason)
reason = MESH_CLOSE_RCVD;
set_link_state(cand, PLINK_HOLDING);
cand->timeout = aconf->holding_timeout_ms;
cb->evl->rem_timeout(cand->t2);
cand->t2 =
cb->evl->add_timeout(SRV_MSEC(cand->timeout), plink_timer, cand);
plink_frame_tx(cand, PLINK_CLOSE, reason);
break;