This repository has been archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmm_wpa.c
4075 lines (3374 loc) · 110 KB
/
cmm_wpa.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
/*
*************************************************************************
* Ralink Tech Inc.
* 5F., No.36, Taiyuan St., Jhubei City,
* Hsinchu County 302,
* Taiwan, R.O.C.
*
* (c) Copyright 2002-2010, Ralink Technology, Inc.
*
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
*************************************************************************/
#include "rt_config.h"
// WPA OUI
UCHAR OUI_WPA[3] = {0x00, 0x50, 0xF2};
UCHAR OUI_WPA_NONE_AKM[4] = {0x00, 0x50, 0xF2, 0x00};
UCHAR OUI_WPA_VERSION[4] = {0x00, 0x50, 0xF2, 0x01};
UCHAR OUI_WPA_WEP40[4] = {0x00, 0x50, 0xF2, 0x01};
UCHAR OUI_WPA_TKIP[4] = {0x00, 0x50, 0xF2, 0x02};
UCHAR OUI_WPA_CCMP[4] = {0x00, 0x50, 0xF2, 0x04};
UCHAR OUI_WPA_WEP104[4] = {0x00, 0x50, 0xF2, 0x05};
UCHAR OUI_WPA_8021X_AKM[4] = {0x00, 0x50, 0xF2, 0x01};
UCHAR OUI_WPA_PSK_AKM[4] = {0x00, 0x50, 0xF2, 0x02};
// WPA2 OUI
UCHAR OUI_WPA2[3] = {0x00, 0x0F, 0xAC};
UCHAR OUI_WPA2_WEP40[4] = {0x00, 0x0F, 0xAC, 0x01};
UCHAR OUI_WPA2_TKIP[4] = {0x00, 0x0F, 0xAC, 0x02};
UCHAR OUI_WPA2_CCMP[4] = {0x00, 0x0F, 0xAC, 0x04};
UCHAR OUI_WPA2_8021X_AKM[4] = {0x00, 0x0F, 0xAC, 0x01};
UCHAR OUI_WPA2_PSK_AKM[4] = {0x00, 0x0F, 0xAC, 0x02};
UCHAR OUI_WPA2_WEP104[4] = {0x00, 0x0F, 0xAC, 0x05};
static VOID ConstructEapolKeyData(
IN PMAC_TABLE_ENTRY pEntry,
IN UCHAR GroupKeyWepStatus,
IN UCHAR keyDescVer,
IN UCHAR MsgType,
IN UCHAR DefaultKeyIdx,
IN UCHAR *GTK,
IN UCHAR *RSNIE,
IN UCHAR RSNIE_LEN,
OUT PEAPOL_PACKET pMsg);
static VOID WpaEAPPacketAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem);
static VOID WpaEAPOLASFAlertAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem);
static VOID WpaEAPOLLogoffAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem);
static VOID WpaEAPOLStartAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem);
static VOID WpaEAPOLKeyAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem);
/*
==========================================================================
Description:
association state machine init, including state transition and timer init
Parameters:
S - pointer to the association state machine
==========================================================================
*/
VOID WpaStateMachineInit(
IN PRTMP_ADAPTER pAd,
IN STATE_MACHINE *S,
OUT STATE_MACHINE_FUNC Trans[])
{
StateMachineInit(S, (STATE_MACHINE_FUNC *)Trans, MAX_WPA_PTK_STATE, MAX_WPA_MSG, (STATE_MACHINE_FUNC)Drop, WPA_PTK, WPA_MACHINE_BASE);
StateMachineSetAction(S, WPA_PTK, MT2_EAPPacket, (STATE_MACHINE_FUNC)WpaEAPPacketAction);
StateMachineSetAction(S, WPA_PTK, MT2_EAPOLStart, (STATE_MACHINE_FUNC)WpaEAPOLStartAction);
StateMachineSetAction(S, WPA_PTK, MT2_EAPOLLogoff, (STATE_MACHINE_FUNC)WpaEAPOLLogoffAction);
StateMachineSetAction(S, WPA_PTK, MT2_EAPOLKey, (STATE_MACHINE_FUNC)WpaEAPOLKeyAction);
StateMachineSetAction(S, WPA_PTK, MT2_EAPOLASFAlert, (STATE_MACHINE_FUNC)WpaEAPOLASFAlertAction);
}
/*
==========================================================================
Description:
this is state machine function.
When receiving EAP packets which is for 802.1x authentication use.
Not use in PSK case
Return:
==========================================================================
*/
VOID WpaEAPPacketAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem)
{
}
VOID WpaEAPOLASFAlertAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem)
{
}
VOID WpaEAPOLLogoffAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem)
{
}
/*
==========================================================================
Description:
Start 4-way HS when rcv EAPOL_START which may create by our driver in assoc.c
Return:
==========================================================================
*/
VOID WpaEAPOLStartAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem)
{
MAC_TABLE_ENTRY *pEntry;
PHEADER_802_11 pHeader;
#ifdef CONFIG_STA_SUPPORT
#endif // CONFIG_STA_SUPPORT //
DBGPRINT(RT_DEBUG_TRACE, ("WpaEAPOLStartAction ===> \n"));
pHeader = (PHEADER_802_11)Elem->Msg;
//For normaol PSK, we enqueue an EAPOL-Start command to trigger the process.
if (Elem->MsgLen == 6)
pEntry = MacTableLookup(pAd, Elem->Msg);
else
{
pEntry = MacTableLookup(pAd, pHeader->Addr2);
}
if (pEntry)
{
DBGPRINT(RT_DEBUG_TRACE, (" PortSecured(%d), WpaState(%d), AuthMode(%d), PMKID_CacheIdx(%d) \n", pEntry->PortSecured, pEntry->WpaState, pEntry->AuthMode, pEntry->PMKID_CacheIdx));
if ((pEntry->PortSecured == WPA_802_1X_PORT_NOT_SECURED)
&& (pEntry->WpaState < AS_PTKSTART)
&& ((pEntry->AuthMode == Ndis802_11AuthModeWPAPSK) || (pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK) || ((pEntry->AuthMode == Ndis802_11AuthModeWPA2) && (pEntry->PMKID_CacheIdx != ENTRY_NOT_FOUND))))
{
pEntry->PrivacyFilter = Ndis802_11PrivFilter8021xWEP;
pEntry->WpaState = AS_INITPSK;
pEntry->PortSecured = WPA_802_1X_PORT_NOT_SECURED;
NdisZeroMemory(pEntry->R_Counter, sizeof(pEntry->R_Counter));
pEntry->ReTryCounter = PEER_MSG1_RETRY_TIMER_CTR;
WPAStart4WayHS(pAd, pEntry, PEER_MSG1_RETRY_EXEC_INTV);
}
}
}
/*
==========================================================================
Description:
This is state machine function.
When receiving EAPOL packets which is for 802.1x key management.
Use both in WPA, and WPAPSK case.
In this function, further dispatch to different functions according to the received packet. 3 categories are :
1. normal 4-way pairwisekey and 2-way groupkey handshake
2. MIC error (Countermeasures attack) report packet from STA.
3. Request for pairwise/group key update from STA
Return:
==========================================================================
*/
VOID WpaEAPOLKeyAction(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem)
{
MAC_TABLE_ENTRY *pEntry;
PHEADER_802_11 pHeader;
PEAPOL_PACKET pEapol_packet;
KEY_INFO peerKeyInfo;
UINT eapol_len;
#ifdef CONFIG_STA_SUPPORT
#endif // CONFIG_STA_SUPPORT //
DBGPRINT(RT_DEBUG_TRACE, ("WpaEAPOLKeyAction ===>\n"));
pHeader = (PHEADER_802_11)Elem->Msg;
pEapol_packet = (PEAPOL_PACKET)&Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];
eapol_len = CONV_ARRARY_TO_UINT16(pEapol_packet->Body_Len) + LENGTH_EAPOL_H;
NdisZeroMemory((PUCHAR)&peerKeyInfo, sizeof(peerKeyInfo));
NdisMoveMemory((PUCHAR)&peerKeyInfo, (PUCHAR)&pEapol_packet->KeyDesc.KeyInfo, sizeof(KEY_INFO));
*((USHORT *)&peerKeyInfo) = cpu2le16(*((USHORT *)&peerKeyInfo));
do
{
pEntry = MacTableLookup(pAd, pHeader->Addr2);
if (!pEntry || (!IS_ENTRY_CLIENT(pEntry) && !IS_ENTRY_APCLI(pEntry)))
break;
if (pEntry->AuthMode < Ndis802_11AuthModeWPA)
break;
DBGPRINT(RT_DEBUG_TRACE, ("Receive EAPoL-Key frame from STA %02X-%02X-%02X-%02X-%02X-%02X\n", PRINT_MAC(pEntry->Addr)));
if (eapol_len > Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H)
{
DBGPRINT(RT_DEBUG_ERROR, ("The length of EAPoL packet is invalid \n"));
break;
}
if (((pEapol_packet->ProVer != EAPOL_VER) && (pEapol_packet->ProVer != EAPOL_VER2)) ||
((pEapol_packet->KeyDesc.Type != WPA1_KEY_DESC) && (pEapol_packet->KeyDesc.Type != WPA2_KEY_DESC)))
{
DBGPRINT(RT_DEBUG_ERROR, ("Key descripter does not match with WPA rule\n"));
break;
}
// The value 1 shall be used for all EAPOL-Key frames to and from a STA when
// neither the group nor pairwise ciphers are CCMP for Key Descriptor 1.
if ((pEntry->WepStatus == Ndis802_11Encryption2Enabled) && (peerKeyInfo.KeyDescVer != KEY_DESC_TKIP))
{
DBGPRINT(RT_DEBUG_ERROR, ("Key descripter version not match(TKIP) \n"));
break;
}
// The value 2 shall be used for all EAPOL-Key frames to and from a STA when
// either the pairwise or the group cipher is AES-CCMP for Key Descriptor 2.
else if ((pEntry->WepStatus == Ndis802_11Encryption3Enabled) && (peerKeyInfo.KeyDescVer != KEY_DESC_AES))
{
DBGPRINT(RT_DEBUG_ERROR, ("Key descripter version not match(AES) \n"));
break;
}
// Check if this STA is in class 3 state and the WPA state is started
if ((pEntry->Sst == SST_ASSOC) && (pEntry->WpaState >= AS_INITPSK))
{
// Check the Key Ack (bit 7) of the Key Information to determine the Authenticator
// or not.
// An EAPOL-Key frame that is sent by the Supplicant in response to an EAPOL-
// Key frame from the Authenticator must not have the Ack bit set.
if (peerKeyInfo.KeyAck == 1)
{
// The frame is snet by Authenticator.
// So the Supplicant side shall handle this.
if ((peerKeyInfo.Secure == 0) && (peerKeyInfo.Request == 0) &&
(peerKeyInfo.Error == 0) && (peerKeyInfo.KeyType == PAIRWISEKEY))
{
// Process 1. the message 1 of 4-way HS in WPA or WPA2
// EAPOL-Key(0,0,1,0,P,0,0,ANonce,0,DataKD_M1)
// 2. the message 3 of 4-way HS in WPA
// EAPOL-Key(0,1,1,1,P,0,KeyRSC,ANonce,MIC,DataKD_M3)
if (peerKeyInfo.KeyMic == 0)
PeerPairMsg1Action(pAd, pEntry, Elem);
else
PeerPairMsg3Action(pAd, pEntry, Elem);
}
else if ((peerKeyInfo.Secure == 1) &&
(peerKeyInfo.KeyMic == 1) &&
(peerKeyInfo.Request == 0) &&
(peerKeyInfo.Error == 0))
{
// Process 1. the message 3 of 4-way HS in WPA2
// EAPOL-Key(1,1,1,1,P,0,KeyRSC,ANonce,MIC,DataKD_M3)
// 2. the message 1 of group KS in WPA or WPA2
// EAPOL-Key(1,1,1,0,G,0,Key RSC,0, MIC,GTK[N])
if (peerKeyInfo.KeyType == PAIRWISEKEY)
PeerPairMsg3Action(pAd, pEntry, Elem);
else
PeerGroupMsg1Action(pAd, pEntry, Elem);
}
}
else
{
// The frame is snet by Supplicant.
// So the Authenticator side shall handle this.
if ((peerKeyInfo.Request == 0) &&
(peerKeyInfo.Error == 0) &&
(peerKeyInfo.KeyMic == 1))
{
if (peerKeyInfo.Secure == 0 && peerKeyInfo.KeyType == PAIRWISEKEY)
{
// EAPOL-Key(0,1,0,0,P,0,0,SNonce,MIC,Data)
// Process 1. message 2 of 4-way HS in WPA or WPA2
// 2. message 4 of 4-way HS in WPA
if (CONV_ARRARY_TO_UINT16(pEapol_packet->KeyDesc.KeyDataLen) == 0)
{
PeerPairMsg4Action(pAd, pEntry, Elem);
}
else
{
PeerPairMsg2Action(pAd, pEntry, Elem);
}
}
else if (peerKeyInfo.Secure == 1 && peerKeyInfo.KeyType == PAIRWISEKEY)
{
// EAPOL-Key(1,1,0,0,P,0,0,0,MIC,0)
// Process message 4 of 4-way HS in WPA2
PeerPairMsg4Action(pAd, pEntry, Elem);
}
else if (peerKeyInfo.Secure == 1 && peerKeyInfo.KeyType == GROUPKEY)
{
// EAPOL-Key(1,1,0,0,G,0,0,0,MIC,0)
// Process message 2 of Group key HS in WPA or WPA2
PeerGroupMsg2Action(pAd, pEntry, &Elem->Msg[LENGTH_802_11], (Elem->MsgLen - LENGTH_802_11));
}
}
}
}
}while(FALSE);
}
/*
========================================================================
Routine Description:
Copy frame from waiting queue into relative ring buffer and set
appropriate ASIC register to kick hardware encryption before really
sent out to air.
Arguments:
pAd Pointer to our adapter
PNDIS_PACKET Pointer to outgoing Ndis frame
NumberOfFrag Number of fragment required
Return Value:
None
Note:
========================================================================
*/
VOID RTMPToWirelessSta(
IN PRTMP_ADAPTER pAd,
IN PMAC_TABLE_ENTRY pEntry,
IN PUCHAR pHeader802_3,
IN UINT HdrLen,
IN PUCHAR pData,
IN UINT DataLen,
IN BOOLEAN bClearFrame)
{
PNDIS_PACKET pPacket;
NDIS_STATUS Status;
if ((!pEntry) || (!IS_ENTRY_CLIENT(pEntry) && !IS_ENTRY_APCLI(pEntry)
))
return;
do {
// build a NDIS packet
Status = RTMPAllocateNdisPacket(pAd, &pPacket, pHeader802_3, HdrLen, pData, DataLen);
if (Status != NDIS_STATUS_SUCCESS)
break;
if (bClearFrame)
RTMP_SET_PACKET_CLEAR_EAP_FRAME(pPacket, 1);
else
RTMP_SET_PACKET_CLEAR_EAP_FRAME(pPacket, 0);
{
RTMP_SET_PACKET_SOURCE(pPacket, PKTSRC_NDIS);
RTMP_SET_PACKET_NET_DEVICE_MBSSID(pPacket, MAIN_MBSSID); // set a default value
if(pEntry->apidx != 0)
RTMP_SET_PACKET_NET_DEVICE_MBSSID(pPacket, pEntry->apidx);
RTMP_SET_PACKET_WCID(pPacket, (UCHAR)pEntry->Aid);
RTMP_SET_PACKET_MOREDATA(pPacket, FALSE);
}
#ifdef CONFIG_STA_SUPPORT
IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
{
// send out the packet
Status = STASendPacket(pAd, pPacket);
if (Status == NDIS_STATUS_SUCCESS)
{
UCHAR Index;
// Dequeue one frame from TxSwQueue0..3 queue and process it
// There are three place calling dequeue for TX ring.
// 1. Here, right after queueing the frame.
// 2. At the end of TxRingTxDone service routine.
// 3. Upon NDIS call RTMPSendPackets
if((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS)) &&
(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)))
{
for(Index = 0; Index < 5; Index ++)
if(pAd->TxSwQueue[Index].Number > 0)
RTMPDeQueuePacket(pAd, FALSE, Index, MAX_TX_PROCESS);
}
}
}
#endif // CONFIG_STA_SUPPORT //
} while (FALSE);
}
/*
==========================================================================
Description:
Check the validity of the received EAPoL frame
Return:
TRUE if all parameters are OK,
FALSE otherwise
==========================================================================
*/
BOOLEAN PeerWpaMessageSanity(
IN PRTMP_ADAPTER pAd,
IN PEAPOL_PACKET pMsg,
IN ULONG MsgLen,
IN UCHAR MsgType,
IN MAC_TABLE_ENTRY *pEntry)
{
UCHAR mic[LEN_KEY_DESC_MIC], digest[80], KEYDATA[MAX_LEN_OF_RSNIE];
BOOLEAN bReplayDiff = FALSE;
BOOLEAN bWPA2 = FALSE;
KEY_INFO EapolKeyInfo;
UCHAR GroupKeyIndex = 0;
NdisZeroMemory(mic, sizeof(mic));
NdisZeroMemory(digest, sizeof(digest));
NdisZeroMemory(KEYDATA, sizeof(KEYDATA));
NdisZeroMemory((PUCHAR)&EapolKeyInfo, sizeof(EapolKeyInfo));
NdisMoveMemory((PUCHAR)&EapolKeyInfo, (PUCHAR)&pMsg->KeyDesc.KeyInfo, sizeof(KEY_INFO));
*((USHORT *)&EapolKeyInfo) = cpu2le16(*((USHORT *)&EapolKeyInfo));
// Choose WPA2 or not
if ((pEntry->AuthMode == Ndis802_11AuthModeWPA2) || (pEntry->AuthMode == Ndis802_11AuthModeWPA2PSK))
bWPA2 = TRUE;
// 0. Check MsgType
if ((MsgType > EAPOL_GROUP_MSG_2) || (MsgType < EAPOL_PAIR_MSG_1))
{
DBGPRINT(RT_DEBUG_ERROR, ("The message type is invalid(%d)! \n", MsgType));
return FALSE;
}
// 1. Replay counter check
if (MsgType == EAPOL_PAIR_MSG_1 || MsgType == EAPOL_PAIR_MSG_3 || MsgType == EAPOL_GROUP_MSG_1) // For supplicant
{
// First validate replay counter, only accept message with larger replay counter.
// Let equal pass, some AP start with all zero replay counter
UCHAR ZeroReplay[LEN_KEY_DESC_REPLAY];
NdisZeroMemory(ZeroReplay, LEN_KEY_DESC_REPLAY);
if ((RTMPCompareMemory(pMsg->KeyDesc.ReplayCounter, pEntry->R_Counter, LEN_KEY_DESC_REPLAY) != 1) &&
(RTMPCompareMemory(pMsg->KeyDesc.ReplayCounter, ZeroReplay, LEN_KEY_DESC_REPLAY) != 0))
{
bReplayDiff = TRUE;
}
}
else if (MsgType == EAPOL_PAIR_MSG_2 || MsgType == EAPOL_PAIR_MSG_4 || MsgType == EAPOL_GROUP_MSG_2) // For authenticator
{
// check Replay Counter coresponds to MSG from authenticator, otherwise discard
if (!NdisEqualMemory(pMsg->KeyDesc.ReplayCounter, pEntry->R_Counter, LEN_KEY_DESC_REPLAY))
{
bReplayDiff = TRUE;
}
}
// Replay Counter different condition
if (bReplayDiff)
{
// send wireless event - for replay counter different
RTMPSendWirelessEvent(pAd, IW_REPLAY_COUNTER_DIFF_EVENT_FLAG, pEntry->Addr, pEntry->apidx, 0);
if (MsgType < EAPOL_GROUP_MSG_1)
{
DBGPRINT(RT_DEBUG_ERROR, ("Replay Counter Different in pairwise msg %d of 4-way handshake!\n", MsgType));
}
else
{
DBGPRINT(RT_DEBUG_ERROR, ("Replay Counter Different in group msg %d of 2-way handshake!\n", (MsgType - EAPOL_PAIR_MSG_4)));
}
hex_dump("Receive replay counter ", pMsg->KeyDesc.ReplayCounter, LEN_KEY_DESC_REPLAY);
hex_dump("Current replay counter ", pEntry->R_Counter, LEN_KEY_DESC_REPLAY);
return FALSE;
}
// 2. Verify MIC except Pairwise Msg1
if (MsgType != EAPOL_PAIR_MSG_1)
{
UCHAR rcvd_mic[LEN_KEY_DESC_MIC];
UINT eapol_len = CONV_ARRARY_TO_UINT16(pMsg->Body_Len) + 4;
// Record the received MIC for check later
NdisMoveMemory(rcvd_mic, pMsg->KeyDesc.KeyMic, LEN_KEY_DESC_MIC);
NdisZeroMemory(pMsg->KeyDesc.KeyMic, LEN_KEY_DESC_MIC);
if (EapolKeyInfo.KeyDescVer == KEY_DESC_TKIP) // TKIP
{
RT_HMAC_MD5(pEntry->PTK, LEN_PTK_KCK, (PUCHAR)pMsg, eapol_len, mic, MD5_DIGEST_SIZE);
}
else if (EapolKeyInfo.KeyDescVer == KEY_DESC_AES) // AES
{
RT_HMAC_SHA1(pEntry->PTK, LEN_PTK_KCK, (PUCHAR)pMsg, eapol_len, digest, SHA1_DIGEST_SIZE);
NdisMoveMemory(mic, digest, LEN_KEY_DESC_MIC);
}
if (!NdisEqualMemory(rcvd_mic, mic, LEN_KEY_DESC_MIC))
{
// send wireless event - for MIC different
RTMPSendWirelessEvent(pAd, IW_MIC_DIFF_EVENT_FLAG, pEntry->Addr, pEntry->apidx, 0);
if (MsgType < EAPOL_GROUP_MSG_1)
{
DBGPRINT(RT_DEBUG_ERROR, ("MIC Different in pairwise msg %d of 4-way handshake!\n", MsgType));
}
else
{
DBGPRINT(RT_DEBUG_ERROR, ("MIC Different in group msg %d of 2-way handshake!\n", (MsgType - EAPOL_PAIR_MSG_4)));
}
hex_dump("Received MIC", rcvd_mic, LEN_KEY_DESC_MIC);
hex_dump("Desired MIC", mic, LEN_KEY_DESC_MIC);
return FALSE;
}
}
// 1. Decrypt the Key Data field if GTK is included.
// 2. Extract the context of the Key Data field if it exist.
// The field in pairwise_msg_2_WPA1(WPA2) & pairwise_msg_3_WPA1 is clear.
// The field in group_msg_1_WPA1(WPA2) & pairwise_msg_3_WPA2 is encrypted.
if (CONV_ARRARY_TO_UINT16(pMsg->KeyDesc.KeyDataLen) > 0)
{
// Decrypt this field
if ((MsgType == EAPOL_PAIR_MSG_3 && bWPA2) || (MsgType == EAPOL_GROUP_MSG_1))
{
if(
(EapolKeyInfo.KeyDescVer == KEY_DESC_AES))
{
UINT aes_unwrap_len = 0;
// AES
AES_Key_Unwrap(pMsg->KeyDesc.KeyData,
CONV_ARRARY_TO_UINT16(pMsg->KeyDesc.KeyDataLen),
&pEntry->PTK[LEN_PTK_KCK], LEN_PTK_KEK,
KEYDATA, &aes_unwrap_len);
SET_UINT16_TO_ARRARY(pMsg->KeyDesc.KeyDataLen, aes_unwrap_len);
}
else
{
TKIP_GTK_KEY_UNWRAP(&pEntry->PTK[LEN_PTK_KCK],
pMsg->KeyDesc.KeyIv,
pMsg->KeyDesc.KeyData,
CONV_ARRARY_TO_UINT16(pMsg->KeyDesc.KeyDataLen),
KEYDATA);
}
if (!bWPA2 && (MsgType == EAPOL_GROUP_MSG_1))
GroupKeyIndex = EapolKeyInfo.KeyIndex;
}
else if ((MsgType == EAPOL_PAIR_MSG_2) || (MsgType == EAPOL_PAIR_MSG_3 && !bWPA2))
{
NdisMoveMemory(KEYDATA, pMsg->KeyDesc.KeyData, CONV_ARRARY_TO_UINT16(pMsg->KeyDesc.KeyDataLen));
}
else
{
return TRUE;
}
// Parse Key Data field to
// 1. verify RSN IE for pairwise_msg_2_WPA1(WPA2) ,pairwise_msg_3_WPA1(WPA2)
// 2. verify KDE format for pairwise_msg_3_WPA2, group_msg_1_WPA2
// 3. update shared key for pairwise_msg_3_WPA2, group_msg_1_WPA1(WPA2)
if (!RTMPParseEapolKeyData(pAd, KEYDATA,
CONV_ARRARY_TO_UINT16(pMsg->KeyDesc.KeyDataLen),
GroupKeyIndex, MsgType, bWPA2, pEntry))
{
return FALSE;
}
}
return TRUE;
}
/*
==========================================================================
Description:
This is a function to initilize 4-way handshake
Return:
==========================================================================
*/
VOID WPAStart4WayHS(
IN PRTMP_ADAPTER pAd,
IN MAC_TABLE_ENTRY *pEntry,
IN ULONG TimeInterval)
{
UCHAR Header802_3[14];
UCHAR *mpool;
PEAPOL_PACKET pEapolFrame;
PUINT8 pBssid = NULL;
UCHAR group_cipher = Ndis802_11WEPDisabled;
#ifdef CONFIG_STA_SUPPORT
#endif // CONFIG_STA_SUPPORT //
DBGPRINT(RT_DEBUG_TRACE, ("===> WPAStart4WayHS\n"));
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS | fRTMP_ADAPTER_HALT_IN_PROGRESS))
{
DBGPRINT(RT_DEBUG_ERROR, ("[ERROR]WPAStart4WayHS : The interface is closed...\n"));
return;
}
if (pBssid == NULL)
{
DBGPRINT(RT_DEBUG_ERROR, ("[ERROR]WPAStart4WayHS : No corresponding Authenticator.\n"));
return;
}
// Check the status
if ((pEntry->WpaState > AS_PTKSTART) || (pEntry->WpaState < AS_INITPMK))
{
DBGPRINT(RT_DEBUG_ERROR, ("[ERROR]WPAStart4WayHS : Not expect calling\n"));
return;
}
// Increment replay counter by 1
ADD_ONE_To_64BIT_VAR(pEntry->R_Counter);
// Randomly generate ANonce
GenRandom(pAd, (UCHAR *)pBssid, pEntry->ANonce);
// Allocate memory for output
os_alloc_mem(NULL, (PUCHAR *)&mpool, TX_EAPOL_BUFFER);
if (mpool == NULL)
{
DBGPRINT(RT_DEBUG_ERROR, ("!!!%s : no memory!!!\n", __FUNCTION__));
return;
}
pEapolFrame = (PEAPOL_PACKET)mpool;
NdisZeroMemory(pEapolFrame, TX_EAPOL_BUFFER);
// Construct EAPoL message - Pairwise Msg 1
// EAPOL-Key(0,0,1,0,P,0,0,ANonce,0,DataKD_M1)
ConstructEapolMsg(pEntry,
group_cipher,
EAPOL_PAIR_MSG_1,
0, // Default key index
pEntry->ANonce,
NULL, // TxRSC
NULL, // GTK
NULL, // RSNIE
0, // RSNIE length
pEapolFrame);
// Make outgoing frame
MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pBssid, EAPOL);
RTMPToWirelessSta(pAd, pEntry, Header802_3,
LENGTH_802_3, (PUCHAR)pEapolFrame,
CONV_ARRARY_TO_UINT16(pEapolFrame->Body_Len) + 4,
(pEntry->PortSecured == WPA_802_1X_PORT_SECURED) ? FALSE : TRUE);
// Trigger Retry Timer
RTMPModTimer(&pEntry->RetryTimer, TimeInterval);
// Update State
pEntry->WpaState = AS_PTKSTART;
os_free_mem(NULL, mpool);
DBGPRINT(RT_DEBUG_TRACE, ("<=== WPAStart4WayHS: send Msg1 of 4-way \n"));
}
/*
========================================================================
Routine Description:
Process Pairwise key Msg-1 of 4-way handshaking and send Msg-2
Arguments:
pAd Pointer to our adapter
Elem Message body
Return Value:
None
Note:
========================================================================
*/
VOID PeerPairMsg1Action(
IN PRTMP_ADAPTER pAd,
IN MAC_TABLE_ENTRY *pEntry,
IN MLME_QUEUE_ELEM *Elem)
{
UCHAR PTK[80];
UCHAR Header802_3[14];
PEAPOL_PACKET pMsg1;
UINT MsgLen;
UCHAR *mpool;
PEAPOL_PACKET pEapolFrame;
PUINT8 pCurrentAddr = NULL;
PUINT8 pmk_ptr = NULL;
UCHAR group_cipher = Ndis802_11WEPDisabled;
PUINT8 rsnie_ptr = NULL;
UCHAR rsnie_len = 0;
DBGPRINT(RT_DEBUG_TRACE, ("===> PeerPairMsg1Action \n"));
if ((!pEntry) || (!IS_ENTRY_CLIENT(pEntry) && !IS_ENTRY_APCLI(pEntry)))
return;
if (Elem->MsgLen < (LENGTH_802_11 + LENGTH_802_1_H + LENGTH_EAPOL_H + MIN_LEN_OF_EAPOL_KEY_MSG))
return;
#ifdef CONFIG_STA_SUPPORT
IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
{
pCurrentAddr = pAd->CurrentAddress;
pmk_ptr = pAd->StaCfg.PMK;
group_cipher = pAd->StaCfg.GroupCipher;
rsnie_ptr = pAd->StaCfg.RSN_IE;
rsnie_len = pAd->StaCfg.RSNIE_Len;
}
#endif // CONFIG_STA_SUPPORT //
// Store the received frame
pMsg1 = (PEAPOL_PACKET) &Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];
MsgLen = Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H;
// Sanity Check peer Pairwise message 1 - Replay Counter
if (PeerWpaMessageSanity(pAd, pMsg1, MsgLen, EAPOL_PAIR_MSG_1, pEntry) == FALSE)
return;
// Store Replay counter, it will use to verify message 3 and construct message 2
NdisMoveMemory(pEntry->R_Counter, pMsg1->KeyDesc.ReplayCounter, LEN_KEY_DESC_REPLAY);
// Store ANonce
NdisMoveMemory(pEntry->ANonce, pMsg1->KeyDesc.KeyNonce, LEN_KEY_DESC_NONCE);
// Generate random SNonce
GenRandom(pAd, (UCHAR *)pCurrentAddr, pEntry->SNonce);
{
// Calculate PTK(ANonce, SNonce)
WpaDerivePTK(pAd,
pmk_ptr,
pEntry->ANonce,
pEntry->Addr,
pEntry->SNonce,
pCurrentAddr,
PTK,
LEN_PTK);
// Save key to PTK entry
NdisMoveMemory(pEntry->PTK, PTK, LEN_PTK);
}
// Update WpaState
pEntry->WpaState = AS_PTKINIT_NEGOTIATING;
// Allocate memory for output
os_alloc_mem(NULL, (PUCHAR *)&mpool, TX_EAPOL_BUFFER);
if (mpool == NULL)
{
DBGPRINT(RT_DEBUG_ERROR, ("!!!%s : no memory!!!\n", __FUNCTION__));
return;
}
pEapolFrame = (PEAPOL_PACKET)mpool;
NdisZeroMemory(pEapolFrame, TX_EAPOL_BUFFER);
// Construct EAPoL message - Pairwise Msg 2
// EAPOL-Key(0,1,0,0,P,0,0,SNonce,MIC,DataKD_M2)
ConstructEapolMsg(pEntry,
group_cipher,
EAPOL_PAIR_MSG_2,
0, // DefaultKeyIdx
pEntry->SNonce,
NULL, // TxRsc
NULL, // GTK
(UCHAR *)rsnie_ptr,
rsnie_len,
pEapolFrame);
// Make outgoing frame
MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pCurrentAddr, EAPOL);
RTMPToWirelessSta(pAd, pEntry,
Header802_3, sizeof(Header802_3), (PUCHAR)pEapolFrame,
CONV_ARRARY_TO_UINT16(pEapolFrame->Body_Len) + 4, TRUE);
os_free_mem(NULL, mpool);
DBGPRINT(RT_DEBUG_TRACE, ("<=== PeerPairMsg1Action: send Msg2 of 4-way \n"));
}
/*
==========================================================================
Description:
When receiving the second packet of 4-way pairwisekey handshake.
Return:
==========================================================================
*/
VOID PeerPairMsg2Action(
IN PRTMP_ADAPTER pAd,
IN MAC_TABLE_ENTRY *pEntry,
IN MLME_QUEUE_ELEM *Elem)
{
UCHAR PTK[80];
BOOLEAN Cancelled;
PHEADER_802_11 pHeader;
UCHAR *mpool;
PEAPOL_PACKET pEapolFrame;
PEAPOL_PACKET pMsg2;
UINT MsgLen;
UCHAR Header802_3[LENGTH_802_3];
UCHAR TxTsc[6];
PUINT8 pBssid = NULL;
PUINT8 pmk_ptr = NULL;
PUINT8 gtk_ptr = NULL;
UCHAR default_key = 0;
UCHAR group_cipher = Ndis802_11WEPDisabled;
PUINT8 rsnie_ptr = NULL;
UCHAR rsnie_len = 0;
DBGPRINT(RT_DEBUG_TRACE, ("===> PeerPairMsg2Action \n"));
if ((!pEntry) || !IS_ENTRY_CLIENT(pEntry))
return;
if (Elem->MsgLen < (LENGTH_802_11 + LENGTH_802_1_H + LENGTH_EAPOL_H + MIN_LEN_OF_EAPOL_KEY_MSG))
return;
// check Entry in valid State
if (pEntry->WpaState < AS_PTKSTART)
return;
// pointer to 802.11 header
pHeader = (PHEADER_802_11)Elem->Msg;
// skip 802.11_header(24-byte) and LLC_header(8)
pMsg2 = (PEAPOL_PACKET)&Elem->Msg[LENGTH_802_11 + LENGTH_802_1_H];
MsgLen = Elem->MsgLen - LENGTH_802_11 - LENGTH_802_1_H;
// Store SNonce
NdisMoveMemory(pEntry->SNonce, pMsg2->KeyDesc.KeyNonce, LEN_KEY_DESC_NONCE);
{
// Derive PTK
WpaDerivePTK(pAd,
(UCHAR *)pmk_ptr,
pEntry->ANonce, // ANONCE
(UCHAR *)pBssid,
pEntry->SNonce, // SNONCE
pEntry->Addr,
PTK,
LEN_PTK);
NdisMoveMemory(pEntry->PTK, PTK, LEN_PTK);
}
// Sanity Check peer Pairwise message 2 - Replay Counter, MIC, RSNIE
if (PeerWpaMessageSanity(pAd, pMsg2, MsgLen, EAPOL_PAIR_MSG_2, pEntry) == FALSE)
return;
do
{
// Allocate memory for input
os_alloc_mem(NULL, (PUCHAR *)&mpool, TX_EAPOL_BUFFER);
if (mpool == NULL)
{
DBGPRINT(RT_DEBUG_ERROR, ("!!!%s : no memory!!!\n", __FUNCTION__));
return;
}
pEapolFrame = (PEAPOL_PACKET)mpool;
NdisZeroMemory(pEapolFrame, TX_EAPOL_BUFFER);
// delete retry timer
RTMPCancelTimer(&pEntry->RetryTimer, &Cancelled);
// Change state
pEntry->WpaState = AS_PTKINIT_NEGOTIATING;
// Increment replay counter by 1
ADD_ONE_To_64BIT_VAR(pEntry->R_Counter);
// Construct EAPoL message - Pairwise Msg 3
ConstructEapolMsg(pEntry,
group_cipher,
EAPOL_PAIR_MSG_3,
default_key,
pEntry->ANonce,
TxTsc,
(UCHAR *)gtk_ptr,
(UCHAR *)rsnie_ptr,
rsnie_len,
pEapolFrame);
// Make outgoing frame
MAKE_802_3_HEADER(Header802_3, pEntry->Addr, pBssid, EAPOL);
RTMPToWirelessSta(pAd, pEntry, Header802_3, LENGTH_802_3,
(PUCHAR)pEapolFrame,
CONV_ARRARY_TO_UINT16(pEapolFrame->Body_Len) + 4,
(pEntry->PortSecured == WPA_802_1X_PORT_SECURED) ? FALSE : TRUE);
pEntry->ReTryCounter = PEER_MSG3_RETRY_TIMER_CTR;
RTMPSetTimer(&pEntry->RetryTimer, PEER_MSG3_RETRY_EXEC_INTV);
// Update State
pEntry->WpaState = AS_PTKINIT_NEGOTIATING;
os_free_mem(NULL, mpool);
}while(FALSE);
DBGPRINT(RT_DEBUG_TRACE, ("<=== PeerPairMsg2Action: send Msg3 of 4-way \n"));
}
/*
========================================================================
Routine Description:
Process Pairwise key Msg 3 of 4-way handshaking and send Msg 4
Arguments:
pAd Pointer to our adapter
Elem Message body
Return Value:
None
Note:
========================================================================
*/
VOID PeerPairMsg3Action(
IN PRTMP_ADAPTER pAd,
IN MAC_TABLE_ENTRY *pEntry,
IN MLME_QUEUE_ELEM *Elem)
{
PHEADER_802_11 pHeader;
UCHAR Header802_3[14];
UCHAR *mpool;
PEAPOL_PACKET pEapolFrame;
PEAPOL_PACKET pMsg3;
UINT MsgLen;
PUINT8 pCurrentAddr = NULL;
UCHAR group_cipher = Ndis802_11WEPDisabled;
DBGPRINT(RT_DEBUG_TRACE, ("===> PeerPairMsg3Action \n"));
if ((!pEntry) || (!IS_ENTRY_CLIENT(pEntry) && !IS_ENTRY_APCLI(pEntry)))
return;
if (Elem->MsgLen < (LENGTH_802_11 + LENGTH_802_1_H + LENGTH_EAPOL_H + MIN_LEN_OF_EAPOL_KEY_MSG))
return;
#ifdef CONFIG_STA_SUPPORT
IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
{
pCurrentAddr = pAd->CurrentAddress;
group_cipher = pAd->StaCfg.GroupCipher;