Sample Code

Windows Driver Samples/ Native Wifi IHV Service/ C++/ ihvsample/ ihvonexext.cpp/

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
/*++
 
Copyright (c) 2005 Microsoft Corporation
 
Abstract:
 
   Sample IHV Extensibility DLL to extend
   802.11 LWF driver for third party protocols.
 
 
--*/
 
#include "precomp.h"
 
 
 
 
 
#define MAX_BACKLOG         32
#define MAX_EXEMPTIONS      1
#define MAX_REGISTRATIONS   2
#define ETHTYPE_EAPOL       0x888e
#define EapolTypeEapolKey   0x03
 
 
DWORD
PlumbWEPKey
(
    HANDLE              hDot11SvcHandle,
    ULONG               uKeyIndex,
    DOT11_DIRECTION     direction,
    LPBYTE              pbKey,
    ULONG               uKeyLen
);
 
 
DWORD
ProcessRC4Key
(
    HANDLE                          hIhvExtAdapter,
    PDOT11_MSONEX_RESULT_PARAMS     pOneXResultParams,
    HANDLE                          hDot11SvcHandle,
    HANDLE                          hSecuritySessionID,
    ULONG                           uPktLen,
    PBYTE                           pbEapolPkt
);
 
 
 
// initialize onex data structure.
DWORD
GetNewOnexData
(
    PONEX_DATA* ppOnexData
)
{
    DWORD           dwResult    =   ERROR_SUCCESS;
    PONEX_DATA      pOnexData   =   NULL;
 
    ASSERT( ppOnexData );
 
    if (*ppOnexData)
    {
        dwResult = ERROR_INVALID_STATE;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    // allocate memory.
    pOnexData = (PONEX_DATA) PrivateMemoryAlloc( sizeof( ONEX_DATA ) );
    if (!pOnexData)
    {
        dwResult = ERROR_OUTOFMEMORY;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    // init fields here.
    pOnexData->lpfnReceivePacket    =   Do1xReceivePacket;
    pOnexData->lpfnIndicateResult   =   Do1xIndicateResult;
 
    // transfer data to caller.
    (*ppOnexData)   =   pOnexData;
    pOnexData       =   NULL;
 
error:
    if ( pOnexData )
    {
        FreeOnexData( &pOnexData );
    }
    return dwResult;
}
 
 
 
 
// free onex data structure.
VOID
FreeOnexData
(
    PONEX_DATA* ppOnexData
)
{
    PONEX_DATA      pOnexData   =   NULL;
 
    if ( ppOnexData && (*ppOnexData) )
    {
        // Freeing caller's variable.
        pOnexData       =   (*ppOnexData);
        (*ppOnexData)   =   NULL;
 
        RC4UtilsFreeResultParams( &(pOnexData->pOnexResultParams) );
        ZeroMemory( pOnexData , sizeof( ONEX_DATA ) );
        PrivateMemoryFree( pOnexData );
    }
}
 
 
// verify if result params are available and valid.
BOOL
IsOneXResultParamsAvailable
(
    PDOT11_MSONEX_RESULT_PARAMS     pOneXResultParams
)
{
    BOOL    bResult =   FALSE;
 
 
    bResult =
    (
        pOneXResultParams                   &&
        pOneXResultParams->pbMPPERecvKey    &&
        pOneXResultParams->dwMPPERecvKeyLen &&
        pOneXResultParams->pbMPPESendKey    &&
        pOneXResultParams->dwMPPESendKeyLen
    );
 
    TRACE_MESSAGE_VAL( "Onex Result Params Available = ", bResult );
 
    return bResult;
}
 
 
// free rc4 packet.
VOID
FreeRC4Pkt
(
    PCACHED_PKT pPkt
)
{
    if ( pPkt )
    {
        PrivateMemoryFree( pPkt->pbPkt );
        pPkt->pbPkt     =   NULL;
        pPkt->uPktLen   =   0;
    }
}
 
// flush rc4 packet cache.
VOID
FlushRC4PktCache
(
    PONEX_DATA  pOnexData
)
{
    ULONG i = 0;
 
    ASSERT( pOnexData );
 
    for (i = 0; i< RC4_CACHE_SIZE; i++)
    {
        FreeRC4Pkt( &(pOnexData->RC4Cache[i]) );
    }
}
 
 
// cache rc4 packet
DWORD
CacheRC4Pkt
(
    PONEX_DATA  pOnexData,
    ULONG       uPktLen,
    PBYTE       pbEapolPkt
)
{
    DWORD   dwResult    =   ERROR_SUCCESS;
    PBYTE   pbPktCopy   =   NULL;
 
    if ( !(pOnexData && uPktLen && pbEapolPkt) )
    {
        dwResult = ERROR_INVALID_PARAMETER;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    // copy the packet.
    pbPktCopy = (PBYTE) PrivateMemoryAlloc(uPktLen);
    if (!pbPktCopy)
    {
        dwResult = ERROR_OUTOFMEMORY;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    CopyMemory
    (
        pbPktCopy,
        pbEapolPkt,
        uPktLen
    );
 
    // free packet destination.
    FreeRC4Pkt( &(pOnexData->RC4Cache[pOnexData->uCacheFreeIdx]) );
 
    // move packet copy to cache.
    pOnexData->RC4Cache[ pOnexData->uCacheFreeIdx ].pbPkt   =   pbPktCopy;
    pOnexData->RC4Cache[ pOnexData->uCacheFreeIdx ].uPktLen =   uPktLen;
 
 
    TRACE_MESSAGE_VAL( "RC4 Packet cached, Index = ", pOnexData->uCacheFreeIdx );
 
    // increment index.
    pOnexData->uCacheFreeIdx = (pOnexData->uCacheFreeIdx + 1 ) % RC4_CACHE_SIZE;
 
error:
    return dwResult;
}
 
// process cached rc4 packets.
DWORD
ProcessCachedRC4Packets
(
    PADAPTER_DETAILS    pAdapterDetails
)
{
    DWORD       dwResult        =   ERROR_SUCCESS;
    ULONG       ulIndex         =   0;
    ULONG       uProcessIndex   =   0;
    PONEX_DATA  pOnexData       =   NULL;
 
    ASSERT( pAdapterDetails );
    ASSERT( pAdapterDetails->pOnexData );
 
    pOnexData = pAdapterDetails->pOnexData;
 
    // If no 1x result available, bail
    if (!IsOneXResultParamsAvailable( pOnexData->pOnexResultParams ))
    {
        dwResult = ERROR_SUCCESS;
        BAIL( );
    }
 
    TRACE_MESSAGE( "Processing cached RC4 packets." );
 
    // We process last 2 received frames
 
    for ( ulIndex = 1; ulIndex <= RC4_CACHE_SIZE; ulIndex++ )
    {
        uProcessIndex = ( pOnexData->uCacheFreeIdx - ulIndex) % RC4_CACHE_SIZE;
 
        if
        (
            ( pOnexData->RC4Cache[uProcessIndex].pbPkt )     &&
            ( pOnexData->RC4Cache[uProcessIndex].uPktLen )
        )
        {
            dwResult =
            ProcessRC4Key
            (
                (HANDLE) &(pAdapterDetails->Link),
                pOnexData->pOnexResultParams,
                pAdapterDetails->hDot11SvcHandle,
                pOnexData->hSecuritySessionID,
                pOnexData->RC4Cache[uProcessIndex].uPktLen,
                pOnexData->RC4Cache[uProcessIndex].pbPkt
            );
            BAIL_ON_WIN32_ERROR(dwResult);
 
            FreeRC4Pkt( &(pOnexData->RC4Cache[uProcessIndex]) );
 
        }
    }
 
error:
    return dwResult;
}
 
 
 
 
 
// pre-associate function for onex profile.
DWORD
WINAPI
Do1xPreAssociate
(
    PADAPTER_DETAILS    pAdapterDetails,
    DWORD*              pdwReasonCode
)
{
    DWORD                       dwResult                            =   ERROR_SUCCESS;
    BOOL                        bLocked                             =   FALSE;
    ULONG                       uNumExemptions                      =   0;
    ULONG                       uNumRegistrations                   =   0;
    DOT11_PRIVACY_EXEMPTION     PrivacyExemption[MAX_EXEMPTIONS]    =   {0};
    USHORT                      usRegistration[MAX_REGISTRATIONS]   =   {0};
 
    ASSERT( pAdapterDetails );
    ASSERT( pdwReasonCode );
 
    EnterCriticalSection( &g_csSynch );
    bLocked = TRUE;
 
    // Reason code is set before making calls that could fail.
    (*pdwReasonCode) = L2_REASON_CODE_IHV_INVALID_STATE;
 
    if ( nic_state_pre_assoc_started != pAdapterDetails->NicState )
    {
        dwResult = ERROR_INVALID_STATE;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    // Reason code is set before making calls that could fail.
    (*pdwReasonCode) = L2_REASON_CODE_IHV_OUTOFMEMORY;
 
    dwResult =
    GetNewOnexData
    (
        &(pAdapterDetails->pOnexData)
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
 
    // Reason code is set before making calls that could fail.
    (*pdwReasonCode) = L2_REASON_CODE_IHV_HARDWARE_FAILURE;
 
 
    TRACE_MESSAGE( "Setting Auth Algorithm." );
    dwResult =
    (g_pDot11ExtApi->Dot11ExtSetAuthAlgorithm)
    (
        pAdapterDetails->hDot11SvcHandle,
        DOT11_AUTH_ALGO_80211_OPEN
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
    TRACE_MESSAGE( "Setting Unicast cipher algorithm." );
    dwResult =
    (g_pDot11ExtApi->Dot11ExtSetUnicastCipherAlgorithm)
    (
        pAdapterDetails->hDot11SvcHandle,
        DOT11_CIPHER_ALGO_WEP
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
    TRACE_MESSAGE( "Setting Multicast cipher algorithm." );
    dwResult =
    (g_pDot11ExtApi->Dot11ExtSetMulticastCipherAlgorithm)
    (
        pAdapterDetails->hDot11SvcHandle,
        DOT11_CIPHER_ALGO_WEP
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
    TRACE_MESSAGE( "Setting exclude unencrypted flag." );
    dwResult =
    (g_pDot11ExtApi->Dot11ExtSetExcludeUnencrypted)
    (
        pAdapterDetails->hDot11SvcHandle,
        TRUE
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
    // set the exemption handler
 
    // In vanilla 1x, 802.1x packets are never encrypted
    PrivacyExemption[uNumExemptions].usExemptionActionType  =   DOT11_EXEMPT_ALWAYS;
    PrivacyExemption[uNumExemptions].usEtherType            =   htons(ETHTYPE_EAPOL);
    PrivacyExemption[uNumExemptions].usExemptionPacketType  =   DOT11_EXEMPT_UNICAST;
    uNumExemptions++;
    ASSERT(uNumExemptions <= MAX_EXEMPTIONS);
 
    usRegistration[uNumRegistrations] = htons(ETHTYPE_EAPOL);
    uNumRegistrations++;
    ASSERT(uNumRegistrations <= MAX_REGISTRATIONS);
 
    TRACE_MESSAGE( "Setting ethertype handling." );
    dwResult =
    g_pDot11ExtApi->Dot11ExtSetEtherTypeHandling
    (
        pAdapterDetails->hDot11SvcHandle,
        MAX_BACKLOG,
        uNumExemptions,
        PrivacyExemption,
        uNumRegistrations,
        usRegistration
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
 
    // Verified before, just after acquiring lock.
    ASSERT( nic_state_pre_assoc_started == pAdapterDetails->NicState  );
 
    pAdapterDetails->NicState = nic_state_pre_assoc_ended;
 
    // Reason code is set to SUCCESS.
    (*pdwReasonCode) = L2_REASON_CODE_SUCCESS;
 
    // populate post associate handler functions.
    pAdapterDetails->pPerformPostAssociateCompletionRoutine =   NULL;
    pAdapterDetails->pPerformPostAssociateRoutine           =   Do1xPostAssociate;
    pAdapterDetails->pStopPostAssociateRoutine              =   Do1xStopPostAssociate;
 
error:
    if ( bLocked )
    {
        LeaveCriticalSection( &g_csSynch );
    }
    return dwResult;
}
 
 
 
 
// post-associate function for onex profile.
DWORD
WINAPI
Do1xPostAssociate
(
    IN  PADAPTER_DETAILS                            pAdapterDetails,
    IN  HANDLE                                      hSecuritySessionID,
    IN  PDOT11_PORT_STATE                           pPortState,
    IN  ULONG                                       uDot11AssocParamsBytes,
    IN  PDOT11_ASSOCIATION_COMPLETION_PARAMETERS    pDot11AssocParams
)
{
    DWORD               dwResult        =   ERROR_SUCCESS;
    BOOL                bLocked         =   FALSE;
 
    ASSERT( pAdapterDetails );
 
    UNREFERENCED_PARAMETER( pPortState );
    UNREFERENCED_PARAMETER( uDot11AssocParamsBytes );
    UNREFERENCED_PARAMETER( pDot11AssocParams );
 
 
    EnterCriticalSection( &g_csSynch );
    bLocked = TRUE;
 
    // verify and change state.
    if
    (
        ( nic_state_post_assoc_started != pAdapterDetails->NicState &&
          nic_state_post_assoc_ended != pAdapterDetails->NicState
        )  ||
        ( !(pAdapterDetails->pOnexData) )
    )
    {
        dwResult = ERROR_INVALID_STATE;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
    pAdapterDetails->NicState = nic_state_onex_in_progress;
 
    // no need to free old id handle.
    pAdapterDetails->pOnexData->hSecuritySessionID = hSecuritySessionID;
 
    TRACE_MESSAGE( "Starting OneX." );
    dwResult =
    (g_pDot11ExtApi->Dot11ExtStartOneX)
    (
        pAdapterDetails->hDot11SvcHandle,
        NULL // EAP attributes
    );
    BAIL_ON_WIN32_ERROR( dwResult );
    pAdapterDetails->pOnexData->fMSOneXStarted = TRUE;
 
 
error:
    if ( bLocked )
    {
        LeaveCriticalSection( &g_csSynch );
    }
    return dwResult;
}
 
 
 
 
 
// receive rc4 key.
DWORD
ReceiveRC4Key
(
    PADAPTER_DETAILS    pAdapterDetails,
    ULONG               uPktLen,
    PEAPOL_PACKET       pEapolPkt
)
{
    DWORD   dwResult    =   ERROR_SUCCESS;
    BOOL    bLocked     =   FALSE;
 
    ASSERT( pAdapterDetails );
    ASSERT( uPktLen );
    ASSERT( pEapolPkt );
 
    EnterCriticalSection(&g_csSynch);
    bLocked = TRUE;
 
    ASSERT( pAdapterDetails->pOnexData );
 
    TRACE_MESSAGE( "Received RC4 Key packet." );
 
    if ( IsOneXResultParamsAvailable( pAdapterDetails->pOnexData->pOnexResultParams ) )
    {
        // 1x params are available
        dwResult =
        ProcessRC4Key
        (
            (HANDLE) &(pAdapterDetails->Link),
            pAdapterDetails->pOnexData->pOnexResultParams,
            pAdapterDetails->hDot11SvcHandle,
            pAdapterDetails->pOnexData->hSecuritySessionID,
            uPktLen,
            (PBYTE)pEapolPkt
        );
        BAIL_ON_WIN32_ERROR(dwResult);
    }
    else
    {
        dwResult =
        CacheRC4Pkt
        (
            pAdapterDetails->pOnexData,
            uPktLen,
            (PBYTE) pEapolPkt
        );
        BAIL_ON_WIN32_ERROR(dwResult);
    }
 
error:
    if (bLocked)
    {
        LeaveCriticalSection(&g_csSynch);
    }
    return dwResult;
}
 
 
 
 
// receive packet function for onex profile.
DWORD
WINAPI
Do1xReceivePacket
(
    PADAPTER_DETAILS    pAdapterDetails,
    DWORD               dwInBufferSize,
    LPVOID              pvInBuffer
)
{
    DWORD                           dwResult        =   ERROR_SUCCESS;
    HANDLE                          hDot11SvcHandle =   NULL;
    BOOL                            bLocked         =   FALSE;
    PDOT11_SECURITY_PACKET_HEADER   pSecurityPkt    =   NULL;
    PEAPOL_PACKET                   pEapolPkt       =   NULL;
    DWORD                           uReqdPktLen     =   0;
 
    ASSERT( pAdapterDetails );
 
    // Must include at least one byte of data
    uReqdPktLen = FIELD_OFFSET(DOT11_SECURITY_PACKET_HEADER, Data) + 1;
 
    if ( dwInBufferSize < uReqdPktLen )
    {
        dwResult = ERROR_INVALID_PARAMETER;
        BAIL_ON_WIN32_ERROR(dwResult);
    }
 
    if ( !pvInBuffer )
    {
        dwResult = ERROR_INVALID_PARAMETER;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    pSecurityPkt    =   (PDOT11_SECURITY_PACKET_HEADER) pvInBuffer;
    pEapolPkt       =   (PEAPOL_PACKET) pSecurityPkt->Data;
 
    EnterCriticalSection( &g_csSynch );
    bLocked = TRUE;
 
    // validate state.
    if
    (
        (!(pAdapterDetails->pOnexData))                                 ||
        (
            ( nic_state_onex_in_progress != pAdapterDetails->NicState  )    &&
            ( nic_state_post_assoc_ended != pAdapterDetails->NicState  )
        )
    )
    {
        dwResult = ERROR_INVALID_STATE;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    hDot11SvcHandle = pAdapterDetails->hDot11SvcHandle;
 
    TRACE_MESSAGE( "Received security packet." );
 
    // Ignoring version
    if ( EapolTypeEapolKey == pEapolPkt->PacketType )
    {
        dwResult =
        ReceiveRC4Key
        (
            pAdapterDetails,
            dwInBufferSize - FIELD_OFFSET(DOT11_SECURITY_PACKET_HEADER, Data),
            pEapolPkt
        );
        BAIL_ON_WIN32_ERROR(dwResult);
    }
    else
    {
        // leave lock before sending security packet to AC.
        LeaveCriticalSection( &g_csSynch );
        bLocked = FALSE;
 
        dwResult =
        (g_pDot11ExtApi->Dot11ExtProcessSecurityPacket)
        (
            hDot11SvcHandle,
            dwInBufferSize - FIELD_OFFSET(DOT11_SECURITY_PACKET_HEADER, Data),
            pSecurityPkt->Data
        );
        BAIL_ON_WIN32_ERROR(dwResult);
    }
 
 
error:
    if ( bLocked )
    {
        LeaveCriticalSection( &g_csSynch );
    }
    return dwResult;
}
 
 
// parameters for calling AC with post assoc completion.
typedef
struct _POST_ASSOC_COMPL_DATA
{
    HANDLE              hIhvExtAdapter;
    HANDLE              hDot11SvcHandle;
    HANDLE              hSecuritySessionID;
    DOT11_MAC_ADDRESS   PeerMacAddress;
    DWORD               dwSecurityReasonCode;
    DWORD               dwSecurityWin32Error;
}
POST_ASSOC_COMPL_DATA, *PPOST_ASSOC_COMPL_DATA;
 
 
// function to make the actual post assoc completion call.
// this function is started in a separate thread.
DWORD
WINAPI
PostAssocComplWorker
(
    LPVOID  pvCtxt
)
{
    DWORD                   dwResult        =   ERROR_SUCCESS;
    BOOL                    bLocked         =   FALSE;
    PPOST_ASSOC_COMPL_DATA  pCtxt           =   (PPOST_ASSOC_COMPL_DATA) pvCtxt;
    PADAPTER_DETAILS        pAdapterDetails =   NULL;
 
    ASSERT( pCtxt );
 
    EnterCriticalSection( &g_csSynch );
    bLocked = TRUE;
 
    dwResult =
    ReferenceAdapterDetails
    (
        pCtxt->hIhvExtAdapter,
        &pAdapterDetails
    );
    BAIL_ON_WIN32_ERROR( dwResult );
    ASSERT( pAdapterDetails );
 
    if ( nic_state_onex_in_progress != pAdapterDetails->NicState &&
         nic_state_post_assoc_ended != pAdapterDetails->NicState )
    {
        dwResult = ERROR_INVALID_STATE;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    pAdapterDetails->NicState = nic_state_post_assoc_ended;
 
    LeaveCriticalSection( &g_csSynch );
    bLocked = FALSE;
 
    TRACE_MESSAGE_VAL( "Calling PostAssocCompletion, Status = ", pCtxt->dwSecurityWin32Error );
 
    dwResult =
    (g_pDot11ExtApi->Dot11ExtPostAssociateCompletion)
    (
        pCtxt->hDot11SvcHandle,
        pCtxt->hSecuritySessionID,
        &(pCtxt->PeerMacAddress),
        pCtxt->dwSecurityReasonCode,
        pCtxt->dwSecurityWin32Error
    );
    BAIL_ON_WIN32_ERROR( dwResult );
 
error:
    if ( pAdapterDetails )
    {
        DerefenceAdapterDetails( pCtxt->hIhvExtAdapter );
    }
    if ( bLocked )
    {
        LeaveCriticalSection( &g_csSynch );
    }
    PrivateMemoryFree( pvCtxt );
    return dwResult;
}
 
 
// starts a worker thread for the post association completion.
DWORD
Do1xPostAssocCompletion
(
    HANDLE              hIhvExtAdapter,
    HANDLE              hDot11SvcHandle,
    HANDLE              hSecuritySessionID,
    PDOT11_MAC_ADDRESS  pPeer,
    DWORD               dwSecurityReasonCode,
    DWORD               dwSecurityWin32Error
)
{
    DWORD                   dwResult    =   ERROR_SUCCESS;
    PPOST_ASSOC_COMPL_DATA  pCtxt       =   NULL;
 
    // allocate memory and copy parameters.
    pCtxt = (PPOST_ASSOC_COMPL_DATA) PrivateMemoryAlloc(sizeof(POST_ASSOC_COMPL_DATA));
    if (!pCtxt)
    {
        dwResult = ERROR_OUTOFMEMORY;
        BAIL_ON_WIN32_ERROR(dwResult);
    }
 
    pCtxt->hIhvExtAdapter       =   hIhvExtAdapter;
    pCtxt->hDot11SvcHandle      =   hDot11SvcHandle;
    pCtxt->hSecuritySessionID   =   hSecuritySessionID;
 
    if (pPeer)
    {
        CopyMemory
        (
            &(pCtxt->PeerMacAddress),
            pPeer,
            sizeof(DOT11_MAC_ADDRESS)
        );
    }
 
    pCtxt->dwSecurityReasonCode =   dwSecurityReasonCode;
    pCtxt->dwSecurityWin32Error =   dwSecurityWin32Error;
 
    // start the new thread.
    dwResult =
    StartNewProtectedThread
    (
        hIhvExtAdapter,
        PostAssocComplWorker,
        pCtxt
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
    pCtxt = NULL;
 
error:
    PrivateMemoryFree( pCtxt );
    return dwResult;
}
 
 
// indicate result function for onex profile.
DWORD
WINAPI
Do1xIndicateResult
(
    PADAPTER_DETAILS                pAdapterDetails,
    DOT11_MSONEX_RESULT             msOneXResult,
    PDOT11_MSONEX_RESULT_PARAMS     pDot11MsOneXResultParams
)
{
    DWORD   dwResult    =   ERROR_SUCCESS;
    BOOL    bLocked     =   FALSE;
 
 
    EnterCriticalSection( &g_csSynch );
    bLocked = TRUE;
 
    ASSERT( pAdapterDetails );
    ASSERT( pAdapterDetails->pOnexData );
 
    // validate state.
    if ( nic_state_onex_in_progress != pAdapterDetails->NicState &&
         nic_state_post_assoc_ended != pAdapterDetails->NicState
       )
    {
        dwResult = ERROR_INVALID_STATE;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    if ( msOneXResult == DOT11_MSONEX_IN_PROGRESS )
    {
        // free the onex params if they exist
        RC4UtilsFreeResultParams( &( pAdapterDetails->pOnexData->pOnexResultParams) );
        FlushRC4PktCache( pAdapterDetails->pOnexData );
        TRACE_MESSAGE( "Received DOT11_MSONEX_IN_PROGRESS." );
    }
    else if ( msOneXResult == DOT11_MSONEX_FAILURE )
    {
        TRACE_MESSAGE( "Received DOT11_MSONEX_FAILURE." );
 
        // If failure, indicate right away
        dwResult =
        Do1xPostAssocCompletion
        (
            (HANDLE) &(pAdapterDetails->Link),
            pAdapterDetails->hDot11SvcHandle,
            pAdapterDetails->pOnexData->hSecuritySessionID,
            NULL,
            (pDot11MsOneXResultParams &&
             pDot11MsOneXResultParams->Dot11OneXReasonCode != ONEX_REASON_CODE_SUCCESS)?
                 pDot11MsOneXResultParams->Dot11OneXReasonCode:
                 L2_REASON_CODE_IHV_ONEX_FAILURE,
            msOneXResult
        );
        BAIL_ON_WIN32_ERROR(dwResult);
    }
    else if ( msOneXResult == DOT11_MSONEX_SUCCESS )
    {
        // success case.
        TRACE_MESSAGE( "Received DOT11_MSONEX_SUCCESS." );
 
        // free result params and get new result params.
        RC4UtilsFreeResultParams( &(pAdapterDetails->pOnexData->pOnexResultParams) );
 
        dwResult =
        RC4UtilsDecryptResultParams
        (
            pDot11MsOneXResultParams,
            &(pAdapterDetails->pOnexData->pOnexResultParams)
        );
        BAIL_ON_WIN32_ERROR(dwResult);
    }
 
    // Process cached RC4 packets if any
    dwResult =
    ProcessCachedRC4Packets
    (
        pAdapterDetails
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
error:
    if ( bLocked )
    {
        LeaveCriticalSection( &g_csSynch );
    }
    return dwResult;
}
 
 
// stop-post-associate function for onex profile.
DWORD
WINAPI
Do1xStopPostAssociate
(
    PADAPTER_DETAILS    pAdapterDetails,
    PDOT11_MAC_ADDRESS  pPeer,
    DOT11_ASSOC_STATUS  dot11AssocStatus
)
{
    DWORD   dwResult    =   ERROR_SUCCESS;
    BOOL    bLocked     =   FALSE;
 
    UNREFERENCED_PARAMETER( pPeer );
    UNREFERENCED_PARAMETER( dot11AssocStatus );
 
    EnterCriticalSection( &g_csSynch );
    bLocked = TRUE;
 
    TRACE_MESSAGE( "Performing StopPostAssociate." );
 
    if
    (
        ( nic_state_post_assoc_ended    !=  pAdapterDetails->NicState )     &&
        ( nic_state_onex_in_progress    !=  pAdapterDetails->NicState )     &&
        ( nic_state_post_assoc_started  !=  pAdapterDetails->NicState )
    )
    {
        dwResult = ERROR_INVALID_STATE;
        BAIL_ON_WIN32_ERROR( dwResult );
    }
 
    // Reset the port specific variables.
 
    if ( pAdapterDetails->pOnexData )
    {
        if(pAdapterDetails->pOnexData->fMSOneXStarted)
        {
            // NOTE THAT THE OS WILL STOP THE AUTH EVEN IF THIS CALL IS NOT MADE.
            // The sample should keep track of port downs and call start auth
            // once a port up is received.
            dwResult =
            (g_pDot11ExtApi->Dot11ExtStopOneX)
            (
                pAdapterDetails->hDot11SvcHandle
            );
            if(dwResult != ERROR_SUCCESS)
            {
                // log error
                dwResult = ERROR_SUCCESS;
            }
            else
            {
                pAdapterDetails->pOnexData->fMSOneXStarted = FALSE;
            }
        }
 
        // flush the rc4 packet cache.
        FlushRC4PktCache( pAdapterDetails->pOnexData );
 
        // free the result params.
        RC4UtilsFreeResultParams( &(pAdapterDetails->pOnexData->pOnexResultParams) );
    }
 
 
error:
    if ( bLocked )
    {
        LeaveCriticalSection( &g_csSynch );
    }
    return dwResult;
}
 
 
 
 
 
 
DWORD
PlumbWEPKey
(
    HANDLE              hDot11SvcHandle,
    ULONG               uKeyIndex,
    DOT11_DIRECTION     direction,
    LPBYTE              pbKey,
    ULONG               uKeyLen
)
{
    DWORD                           dwResult    =   ERROR_SUCCESS;
    PDOT11_CIPHER_DEFAULT_KEY_VALUE pDefaultKey =   NULL;
    ULONG                           uAllocLen   =   0;
    BOOL                            bLocked     =   FALSE;
 
    uAllocLen = FIELD_OFFSET(DOT11_CIPHER_DEFAULT_KEY_VALUE, ucKey) + uKeyLen;
 
    if (uAllocLen < uKeyLen)
    {
        dwResult = ERROR_ARITHMETIC_OVERFLOW;
        BAIL_ON_WIN32_ERROR(dwResult);
    }
 
    pDefaultKey = (PDOT11_CIPHER_DEFAULT_KEY_VALUE) PrivateMemoryAlloc(uAllocLen);
    if (!pDefaultKey)
    {
        dwResult = ERROR_NOT_ENOUGH_MEMORY;
        BAIL_ON_WIN32_ERROR(dwResult);
    }
 
    pDefaultKey->AlgorithmId    =   DOT11_CIPHER_ALGO_WEP;
    pDefaultKey->uKeyIndex      =   uKeyIndex;
    pDefaultKey->bDelete        =   FALSE;
    pDefaultKey->bStatic        =   FALSE;
    pDefaultKey->usKeyLength    =   (USHORT) uKeyLen;
 
    ZeroMemory(&(pDefaultKey->MacAddr), sizeof(DOT11_MAC_ADDRESS));
 
    CopyMemory
    (
        pDefaultKey->ucKey,
        pbKey,
        uKeyLen
    );
 
    EnterCriticalSection( &g_csSynch );
    bLocked = TRUE;
 
    TRACE_MESSAGE( "Setting default key." );
    TRACE_MESSAGE_VAL( "    Key Index  = ", uKeyIndex );
    TRACE_MESSAGE_VAL( "    Key Length = ", uKeyLen );
    TRACE_MESSAGE_VAL( "    Direction  = ", direction );
 
    dwResult =
    (g_pDot11ExtApi->Dot11ExtSetDefaultKey)
    (
        hDot11SvcHandle,
        pDefaultKey,
        direction
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
 
error:
    if (pDefaultKey)
    {
        SecureZeroMemory( pDefaultKey, uAllocLen );
        PrivateMemoryFree( pDefaultKey );
    }
    if ( bLocked )
    {
        LeaveCriticalSection( &g_csSynch );
    }
    return dwResult;
}
 
 
DWORD
ProcessRC4Key
(
    HANDLE                          hIhvExtAdapter,
    PDOT11_MSONEX_RESULT_PARAMS     pOneXResultParams,
    HANDLE                          hDot11SvcHandle,
    HANDLE                          hSecuritySessionID,
    ULONG                           uPktLen,
    PBYTE                           pbEapolPkt
)
{
    DWORD           dwResult        =   ERROR_SUCCESS;
    PEAPOL_PACKET   pEapolPkt       =   NULL;
    DWORD           dwKeyLen        =   0;
    DWORD           dwKeyIndex      =   0;
    LPBYTE          pbDecryptedKey  =   NULL;
    BOOL            bUCast          =   FALSE;
    BOOL            bLocked         =   FALSE;
 
 
    EnterCriticalSection( &g_csSynch );
    bLocked = TRUE;
 
    ASSERT( IsOneXResultParamsAvailable ( pOneXResultParams ) );
 
    TRACE_MESSAGE( "Processing RC4 key." );
 
    pEapolPkt   =   (PEAPOL_PACKET) pbEapolPkt;
 
    dwResult =
    RC4UtilsParseKeyPacket
    (
        pEapolPkt,
        uPktLen,
        pOneXResultParams,
        &bUCast,
        &pbDecryptedKey,
        &dwKeyLen,
        &dwKeyIndex
    );
    BAIL_ON_WIN32_ERROR( dwResult );
 
    dwResult =
    PlumbWEPKey
    (
        hDot11SvcHandle,
        dwKeyIndex,
        bUCast ? DOT11_DIR_BOTH : DOT11_DIR_INBOUND,
        pbDecryptedKey,
        dwKeyLen
    );
    BAIL_ON_WIN32_ERROR(dwResult);
 
    if ( bUCast )
    {
        TRACE_MESSAGE_VAL( "Setting default key ID, Key Index = ", dwKeyIndex );
 
        dwResult =
        (g_pDot11ExtApi->Dot11ExtSetDefaultKeyId)
        (
            hDot11SvcHandle,
            dwKeyIndex
        );
        BAIL_ON_WIN32_ERROR(dwResult);
 
        dwResult =
        Do1xPostAssocCompletion
        (
            hIhvExtAdapter,
            hDot11SvcHandle,
            hSecuritySessionID,
            NULL,
            L2_REASON_CODE_SUCCESS,
            ERROR_SUCCESS
        );
        BAIL_ON_WIN32_ERROR(dwResult);
    }
 
error:
    RC4UtilsFreeKeyMaterial( pbDecryptedKey, dwKeyLen );
    if ( bLocked )
    {
        LeaveCriticalSection( &g_csSynch );
    }
    return dwResult;
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS