-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver_ndis.c
2842 lines (2442 loc) · 74.4 KB
/
driver_ndis.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
/*
* WPA Supplicant - Windows/NDIS driver interface
* Copyright (c) 2004-2006, Jouni Malinen <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*/
#ifdef __CYGWIN__
/* Avoid some header file conflicts by not including standard headers for
* cygwin builds when Packet32.h is included. */
#include "build_config.h"
int close(int fd);
#else /* __CYGWIN__ */
#include "includes.h"
#endif /* __CYGWIN__ */
#ifdef CONFIG_USE_NDISUIO
#include <winsock2.h>
#else /* CONFIG_USE_NDISUIO */
#include <Packet32.h>
#endif /* CONFIG_USE_NDISUIO */
#include <ntddndis.h>
#ifdef _WIN32_WCE
#include <winioctl.h>
#include <nuiouser.h>
#include <devload.h>
#endif /* _WIN32_WCE */
#include "common.h"
#include "driver.h"
#include "wpa_supplicant.h"
#include "l2_packet.h"
#include "eloop.h"
#include "wpa.h"
#include "driver_ndis.h"
int wpa_driver_register_event_cb(struct wpa_driver_ndis_data *drv);
#ifdef CONFIG_NDIS_EVENTS_INTEGRATED
void wpa_driver_ndis_event_pipe_cb(void *eloop_data, void *user_data);
#endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
static void wpa_driver_ndis_deinit(void *priv);
static void wpa_driver_ndis_poll(void *drv);
static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx);
static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv);
static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv);
static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv);
/* FIX: to be removed once this can be compiled with the complete NDIS
* header files */
#ifndef OID_802_11_BSSID
#define OID_802_11_BSSID 0x0d010101
#define OID_802_11_SSID 0x0d010102
#define OID_802_11_INFRASTRUCTURE_MODE 0x0d010108
#define OID_802_11_ADD_WEP 0x0D010113
#define OID_802_11_REMOVE_WEP 0x0D010114
#define OID_802_11_DISASSOCIATE 0x0D010115
#define OID_802_11_BSSID_LIST 0x0d010217
#define OID_802_11_AUTHENTICATION_MODE 0x0d010118
#define OID_802_11_PRIVACY_FILTER 0x0d010119
#define OID_802_11_BSSID_LIST_SCAN 0x0d01011A
#define OID_802_11_WEP_STATUS 0x0d01011B
#define OID_802_11_ENCRYPTION_STATUS OID_802_11_WEP_STATUS
#define OID_802_11_ADD_KEY 0x0d01011D
#define OID_802_11_REMOVE_KEY 0x0d01011E
#define OID_802_11_ASSOCIATION_INFORMATION 0x0d01011F
#define OID_802_11_TEST 0x0d010120
#define OID_802_11_CAPABILITY 0x0d010122
#define OID_802_11_PMKID 0x0d010123
#define NDIS_802_11_LENGTH_SSID 32
#define NDIS_802_11_LENGTH_RATES 8
#define NDIS_802_11_LENGTH_RATES_EX 16
typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
typedef struct NDIS_802_11_SSID {
ULONG SsidLength;
UCHAR Ssid[NDIS_802_11_LENGTH_SSID];
} NDIS_802_11_SSID;
typedef LONG NDIS_802_11_RSSI;
typedef enum NDIS_802_11_NETWORK_TYPE {
Ndis802_11FH,
Ndis802_11DS,
Ndis802_11OFDM5,
Ndis802_11OFDM24,
Ndis802_11NetworkTypeMax
} NDIS_802_11_NETWORK_TYPE;
typedef struct NDIS_802_11_CONFIGURATION_FH {
ULONG Length;
ULONG HopPattern;
ULONG HopSet;
ULONG DwellTime;
} NDIS_802_11_CONFIGURATION_FH;
typedef struct NDIS_802_11_CONFIGURATION {
ULONG Length;
ULONG BeaconPeriod;
ULONG ATIMWindow;
ULONG DSConfig;
NDIS_802_11_CONFIGURATION_FH FHConfig;
} NDIS_802_11_CONFIGURATION;
typedef enum NDIS_802_11_NETWORK_INFRASTRUCTURE {
Ndis802_11IBSS,
Ndis802_11Infrastructure,
Ndis802_11AutoUnknown,
Ndis802_11InfrastructureMax
} NDIS_802_11_NETWORK_INFRASTRUCTURE;
typedef enum NDIS_802_11_AUTHENTICATION_MODE {
Ndis802_11AuthModeOpen,
Ndis802_11AuthModeShared,
Ndis802_11AuthModeAutoSwitch,
Ndis802_11AuthModeWPA,
Ndis802_11AuthModeWPAPSK,
Ndis802_11AuthModeWPANone,
Ndis802_11AuthModeWPA2,
Ndis802_11AuthModeWPA2PSK,
Ndis802_11AuthModeMax
} NDIS_802_11_AUTHENTICATION_MODE;
typedef enum NDIS_802_11_WEP_STATUS {
Ndis802_11WEPEnabled,
Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
Ndis802_11WEPDisabled,
Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
Ndis802_11WEPKeyAbsent,
Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
Ndis802_11WEPNotSupported,
Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
Ndis802_11Encryption2Enabled,
Ndis802_11Encryption2KeyAbsent,
Ndis802_11Encryption3Enabled,
Ndis802_11Encryption3KeyAbsent
} NDIS_802_11_WEP_STATUS, NDIS_802_11_ENCRYPTION_STATUS;
typedef enum NDIS_802_11_PRIVACY_FILTER {
Ndis802_11PrivFilterAcceptAll,
Ndis802_11PrivFilter8021xWEP
} NDIS_802_11_PRIVACY_FILTER;
typedef UCHAR NDIS_802_11_RATES[NDIS_802_11_LENGTH_RATES];
typedef UCHAR NDIS_802_11_RATES_EX[NDIS_802_11_LENGTH_RATES_EX];
typedef struct NDIS_WLAN_BSSID_EX {
ULONG Length;
NDIS_802_11_MAC_ADDRESS MacAddress; /* BSSID */
UCHAR Reserved[2];
NDIS_802_11_SSID Ssid;
ULONG Privacy;
NDIS_802_11_RSSI Rssi;
NDIS_802_11_NETWORK_TYPE NetworkTypeInUse;
NDIS_802_11_CONFIGURATION Configuration;
NDIS_802_11_NETWORK_INFRASTRUCTURE InfrastructureMode;
NDIS_802_11_RATES_EX SupportedRates;
ULONG IELength;
UCHAR IEs[1];
} NDIS_WLAN_BSSID_EX;
typedef struct NDIS_802_11_BSSID_LIST_EX {
ULONG NumberOfItems;
NDIS_WLAN_BSSID_EX Bssid[1];
} NDIS_802_11_BSSID_LIST_EX;
typedef struct NDIS_802_11_FIXED_IEs {
UCHAR Timestamp[8];
USHORT BeaconInterval;
USHORT Capabilities;
} NDIS_802_11_FIXED_IEs;
typedef struct NDIS_802_11_WEP {
ULONG Length;
ULONG KeyIndex;
ULONG KeyLength;
UCHAR KeyMaterial[1];
} NDIS_802_11_WEP;
typedef ULONG NDIS_802_11_KEY_INDEX;
typedef ULONGLONG NDIS_802_11_KEY_RSC;
typedef struct NDIS_802_11_KEY {
ULONG Length;
ULONG KeyIndex;
ULONG KeyLength;
NDIS_802_11_MAC_ADDRESS BSSID;
NDIS_802_11_KEY_RSC KeyRSC;
UCHAR KeyMaterial[1];
} NDIS_802_11_KEY;
typedef struct NDIS_802_11_REMOVE_KEY {
ULONG Length;
ULONG KeyIndex;
NDIS_802_11_MAC_ADDRESS BSSID;
} NDIS_802_11_REMOVE_KEY;
typedef struct NDIS_802_11_AI_REQFI {
USHORT Capabilities;
USHORT ListenInterval;
NDIS_802_11_MAC_ADDRESS CurrentAPAddress;
} NDIS_802_11_AI_REQFI;
typedef struct NDIS_802_11_AI_RESFI {
USHORT Capabilities;
USHORT StatusCode;
USHORT AssociationId;
} NDIS_802_11_AI_RESFI;
typedef struct NDIS_802_11_ASSOCIATION_INFORMATION {
ULONG Length;
USHORT AvailableRequestFixedIEs;
NDIS_802_11_AI_REQFI RequestFixedIEs;
ULONG RequestIELength;
ULONG OffsetRequestIEs;
USHORT AvailableResponseFixedIEs;
NDIS_802_11_AI_RESFI ResponseFixedIEs;
ULONG ResponseIELength;
ULONG OffsetResponseIEs;
} NDIS_802_11_ASSOCIATION_INFORMATION;
typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
} NDIS_802_11_AUTHENTICATION_ENCRYPTION;
typedef struct NDIS_802_11_CAPABILITY {
ULONG Length;
ULONG Version;
ULONG NoOfPMKIDs;
ULONG NoOfAuthEncryptPairsSupported;
NDIS_802_11_AUTHENTICATION_ENCRYPTION
AuthenticationEncryptionSupported[1];
} NDIS_802_11_CAPABILITY;
typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
typedef struct BSSID_INFO {
NDIS_802_11_MAC_ADDRESS BSSID;
NDIS_802_11_PMKID_VALUE PMKID;
} BSSID_INFO;
typedef struct NDIS_802_11_PMKID {
ULONG Length;
ULONG BSSIDInfoCount;
BSSID_INFO BSSIDInfo[1];
} NDIS_802_11_PMKID;
typedef enum NDIS_802_11_STATUS_TYPE {
Ndis802_11StatusType_Authentication,
Ndis802_11StatusType_PMKID_CandidateList = 2,
Ndis802_11StatusTypeMax
} NDIS_802_11_STATUS_TYPE;
typedef struct NDIS_802_11_STATUS_INDICATION {
NDIS_802_11_STATUS_TYPE StatusType;
} NDIS_802_11_STATUS_INDICATION;
typedef struct PMKID_CANDIDATE {
NDIS_802_11_MAC_ADDRESS BSSID;
ULONG Flags;
} PMKID_CANDIDATE;
#define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
ULONG Version;
ULONG NumCandidates;
PMKID_CANDIDATE CandidateList[1];
} NDIS_802_11_PMKID_CANDIDATE_LIST;
typedef struct NDIS_802_11_AUTHENTICATION_REQUEST {
ULONG Length;
NDIS_802_11_MAC_ADDRESS Bssid;
ULONG Flags;
} NDIS_802_11_AUTHENTICATION_REQUEST;
#define NDIS_802_11_AUTH_REQUEST_REAUTH 0x01
#define NDIS_802_11_AUTH_REQUEST_KEYUPDATE 0x02
#define NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR 0x06
#define NDIS_802_11_AUTH_REQUEST_GROUP_ERROR 0x0E
#endif /* OID_802_11_BSSID */
#ifndef OID_802_11_PMKID
/* Platform SDK for XP did not include WPA2, so add needed definitions */
#define OID_802_11_CAPABILITY 0x0d010122
#define OID_802_11_PMKID 0x0d010123
#define Ndis802_11AuthModeWPA2 6
#define Ndis802_11AuthModeWPA2PSK 7
#define Ndis802_11StatusType_PMKID_CandidateList 2
typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
} NDIS_802_11_AUTHENTICATION_ENCRYPTION;
typedef struct NDIS_802_11_CAPABILITY {
ULONG Length;
ULONG Version;
ULONG NoOfPMKIDs;
ULONG NoOfAuthEncryptPairsSupported;
NDIS_802_11_AUTHENTICATION_ENCRYPTION
AuthenticationEncryptionSupported[1];
} NDIS_802_11_CAPABILITY;
typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
typedef struct BSSID_INFO {
NDIS_802_11_MAC_ADDRESS BSSID;
NDIS_802_11_PMKID_VALUE PMKID;
} BSSID_INFO;
typedef struct NDIS_802_11_PMKID {
ULONG Length;
ULONG BSSIDInfoCount;
BSSID_INFO BSSIDInfo[1];
} NDIS_802_11_PMKID;
typedef struct PMKID_CANDIDATE {
NDIS_802_11_MAC_ADDRESS BSSID;
ULONG Flags;
} PMKID_CANDIDATE;
#define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
ULONG Version;
ULONG NumCandidates;
PMKID_CANDIDATE CandidateList[1];
} NDIS_802_11_PMKID_CANDIDATE_LIST;
#endif /* OID_802_11_CAPABILITY */
#ifdef CONFIG_USE_NDISUIO
#ifndef _WIN32_WCE
#ifdef __MINGW32_VERSION
typedef ULONG NDIS_OID;
#endif /* __MINGW32_VERSION */
/* from nuiouser.h */
#define FSCTL_NDISUIO_BASE FILE_DEVICE_NETWORK
#define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \
CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access)
#define IOCTL_NDISUIO_OPEN_DEVICE \
_NDISUIO_CTL_CODE(0x200, METHOD_BUFFERED, \
FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define IOCTL_NDISUIO_QUERY_OID_VALUE \
_NDISUIO_CTL_CODE(0x201, METHOD_BUFFERED, \
FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define IOCTL_NDISUIO_SET_OID_VALUE \
_NDISUIO_CTL_CODE(0x205, METHOD_BUFFERED, \
FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define IOCTL_NDISUIO_SET_ETHER_TYPE \
_NDISUIO_CTL_CODE(0x202, METHOD_BUFFERED, \
FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define IOCTL_NDISUIO_QUERY_BINDING \
_NDISUIO_CTL_CODE(0x203, METHOD_BUFFERED, \
FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define IOCTL_NDISUIO_BIND_WAIT \
_NDISUIO_CTL_CODE(0x204, METHOD_BUFFERED, \
FILE_READ_ACCESS | FILE_WRITE_ACCESS)
typedef struct _NDISUIO_QUERY_OID
{
NDIS_OID Oid;
UCHAR Data[sizeof(ULONG)];
} NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;
typedef struct _NDISUIO_SET_OID
{
NDIS_OID Oid;
UCHAR Data[sizeof(ULONG)];
} NDISUIO_SET_OID, *PNDISUIO_SET_OID;
typedef struct _NDISUIO_QUERY_BINDING
{
ULONG BindingIndex;
ULONG DeviceNameOffset;
ULONG DeviceNameLength;
ULONG DeviceDescrOffset;
ULONG DeviceDescrLength;
} NDISUIO_QUERY_BINDING, *PNDISUIO_QUERY_BINDING;
#endif /* _WIN32_WCE */
#endif /* CONFIG_USE_NDISUIO */
static int ndis_get_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
char *data, size_t len)
{
#ifdef CONFIG_USE_NDISUIO
NDISUIO_QUERY_OID *o;
size_t buflen = sizeof(*o) + len;
DWORD written;
int ret;
size_t hdrlen;
o = os_zalloc(buflen);
if (o == NULL)
return -1;
o->Oid = oid;
#ifdef _WIN32_WCE
o->ptcDeviceName = drv->adapter_name;
#endif /* _WIN32_WCE */
if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_OID_VALUE,
o, sizeof(NDISUIO_QUERY_OID), o, buflen, &written,
NULL)) {
wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_QUERY_OID_VALUE "
"failed (oid=%08x): %d", oid, (int) GetLastError());
os_free(o);
return -1;
}
hdrlen = sizeof(NDISUIO_QUERY_OID) - sizeof(o->Data);
if (written < hdrlen) {
wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d); "
"too short", oid, (unsigned int) written);
os_free(o);
return -1;
}
written -= hdrlen;
if (written > len) {
wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d) > "
"len (%d)",oid, (unsigned int) written, len);
os_free(o);
return -1;
}
os_memcpy(data, o->Data, written);
ret = written;
os_free(o);
return ret;
#else /* CONFIG_USE_NDISUIO */
char *buf;
PACKET_OID_DATA *o;
int ret;
buf = os_zalloc(sizeof(*o) + len);
if (buf == NULL)
return -1;
o = (PACKET_OID_DATA *) buf;
o->Oid = oid;
o->Length = len;
if (!PacketRequest(drv->adapter, FALSE, o)) {
wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
__func__, oid, len);
os_free(buf);
return -1;
}
if (o->Length > len) {
wpa_printf(MSG_DEBUG, "%s: oid=0x%x Length (%d) > len (%d)",
__func__, oid, (unsigned int) o->Length, len);
os_free(buf);
return -1;
}
os_memcpy(data, o->Data, o->Length);
ret = o->Length;
os_free(buf);
return ret;
#endif /* CONFIG_USE_NDISUIO */
}
static int ndis_set_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
const char *data, size_t len)
{
#ifdef CONFIG_USE_NDISUIO
NDISUIO_SET_OID *o;
size_t buflen, reallen;
DWORD written;
char txt[50];
os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
wpa_hexdump_key(MSG_MSGDUMP, txt, data, len);
buflen = sizeof(*o) + len;
reallen = buflen - sizeof(o->Data);
o = os_zalloc(buflen);
if (o == NULL)
return -1;
o->Oid = oid;
#ifdef _WIN32_WCE
o->ptcDeviceName = drv->adapter_name;
#endif /* _WIN32_WCE */
if (data)
os_memcpy(o->Data, data, len);
if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_SET_OID_VALUE,
o, reallen, NULL, 0, &written, NULL)) {
wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_SET_OID_VALUE "
"(oid=%08x) failed: %d", oid, (int) GetLastError());
os_free(o);
return -1;
}
os_free(o);
return 0;
#else /* CONFIG_USE_NDISUIO */
char *buf;
PACKET_OID_DATA *o;
char txt[50];
os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
wpa_hexdump_key(MSG_MSGDUMP, txt, data, len);
buf = os_zalloc(sizeof(*o) + len);
if (buf == NULL)
return -1;
o = (PACKET_OID_DATA *) buf;
o->Oid = oid;
o->Length = len;
if (data)
os_memcpy(o->Data, data, len);
if (!PacketRequest(drv->adapter, TRUE, o)) {
wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
__func__, oid, len);
os_free(buf);
return -1;
}
os_free(buf);
return 0;
#endif /* CONFIG_USE_NDISUIO */
}
static int ndis_set_auth_mode(struct wpa_driver_ndis_data *drv, int mode)
{
u32 auth_mode = mode;
if (ndis_set_oid(drv, OID_802_11_AUTHENTICATION_MODE,
(char *) &auth_mode, sizeof(auth_mode)) < 0) {
wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
"OID_802_11_AUTHENTICATION_MODE (%d)",
(int) auth_mode);
return -1;
}
return 0;
}
static int ndis_get_auth_mode(struct wpa_driver_ndis_data *drv)
{
u32 auth_mode;
int res;
res = ndis_get_oid(drv, OID_802_11_AUTHENTICATION_MODE,
(char *) &auth_mode, sizeof(auth_mode));
if (res != sizeof(auth_mode)) {
wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
"OID_802_11_AUTHENTICATION_MODE");
return -1;
}
return auth_mode;
}
static int ndis_set_encr_status(struct wpa_driver_ndis_data *drv, int encr)
{
u32 encr_status = encr;
if (ndis_set_oid(drv, OID_802_11_ENCRYPTION_STATUS,
(char *) &encr_status, sizeof(encr_status)) < 0) {
wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
"OID_802_11_ENCRYPTION_STATUS (%d)", encr);
return -1;
}
return 0;
}
static int ndis_get_encr_status(struct wpa_driver_ndis_data *drv)
{
u32 encr;
int res;
res = ndis_get_oid(drv, OID_802_11_ENCRYPTION_STATUS,
(char *) &encr, sizeof(encr));
if (res != sizeof(encr)) {
wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
"OID_802_11_ENCRYPTION_STATUS");
return -1;
}
return encr;
}
static int wpa_driver_ndis_get_bssid(void *priv, u8 *bssid)
{
struct wpa_driver_ndis_data *drv = priv;
if (drv->wired) {
/*
* Report PAE group address as the "BSSID" for wired
* connection.
*/
bssid[0] = 0x01;
bssid[1] = 0x80;
bssid[2] = 0xc2;
bssid[3] = 0x00;
bssid[4] = 0x00;
bssid[5] = 0x03;
return 0;
}
return ndis_get_oid(drv, OID_802_11_BSSID, bssid, ETH_ALEN) < 0 ?
-1 : 0;
}
static int wpa_driver_ndis_get_ssid(void *priv, u8 *ssid)
{
struct wpa_driver_ndis_data *drv = priv;
NDIS_802_11_SSID buf;
int res;
res = ndis_get_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
if (res < 4) {
wpa_printf(MSG_DEBUG, "NDIS: Failed to get SSID");
if (drv->wired) {
wpa_printf(MSG_DEBUG, "NDIS: Allow get_ssid failure "
"with a wired interface");
return 0;
}
return -1;
}
os_memcpy(ssid, buf.Ssid, buf.SsidLength);
return buf.SsidLength;
}
static int wpa_driver_ndis_set_ssid(struct wpa_driver_ndis_data *drv,
const u8 *ssid, size_t ssid_len)
{
NDIS_802_11_SSID buf;
os_memset(&buf, 0, sizeof(buf));
buf.SsidLength = ssid_len;
os_memcpy(buf.Ssid, ssid, ssid_len);
/*
* Make sure radio is marked enabled here so that scan request will not
* force SSID to be changed to a random one in order to enable radio at
* that point.
*/
drv->radio_enabled = 1;
return ndis_set_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
}
/* Disconnect using OID_802_11_DISASSOCIATE. This will also turn the radio off.
*/
static int wpa_driver_ndis_radio_off(struct wpa_driver_ndis_data *drv)
{
drv->radio_enabled = 0;
return ndis_set_oid(drv, OID_802_11_DISASSOCIATE, " ", 4);
}
/* Disconnect by setting SSID to random (i.e., likely not used). */
static int wpa_driver_ndis_disconnect(struct wpa_driver_ndis_data *drv)
{
char ssid[32];
int i;
for (i = 0; i < 32; i++)
ssid[i] = rand() & 0xff;
return wpa_driver_ndis_set_ssid(drv, ssid, 32);
}
static int wpa_driver_ndis_deauthenticate(void *priv, const u8 *addr,
int reason_code)
{
struct wpa_driver_ndis_data *drv = priv;
return wpa_driver_ndis_disconnect(drv);
}
static int wpa_driver_ndis_disassociate(void *priv, const u8 *addr,
int reason_code)
{
struct wpa_driver_ndis_data *drv = priv;
return wpa_driver_ndis_disconnect(drv);
}
static int wpa_driver_ndis_set_wpa(void *priv, int enabled)
{
wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
return 0;
}
static void wpa_driver_ndis_scan_timeout(void *eloop_ctx, void *timeout_ctx)
{
wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
}
static int wpa_driver_ndis_scan(void *priv, const u8 *ssid, size_t ssid_len)
{
struct wpa_driver_ndis_data *drv = priv;
int res;
if (!drv->radio_enabled) {
wpa_printf(MSG_DEBUG, "NDIS: turning radio on before the first"
" scan");
if (wpa_driver_ndis_disconnect(drv) < 0) {
wpa_printf(MSG_DEBUG, "NDIS: failed to enable radio");
}
drv->radio_enabled = 1;
}
res = ndis_set_oid(drv, OID_802_11_BSSID_LIST_SCAN, " ", 4);
eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
eloop_register_timeout(7, 0, wpa_driver_ndis_scan_timeout, drv,
drv->ctx);
return res;
}
static void wpa_driver_ndis_get_ies(struct wpa_scan_result *res, u8 *ie,
size_t ie_len)
{
u8 *pos = ie;
u8 *end = ie + ie_len;
if (ie_len < sizeof(NDIS_802_11_FIXED_IEs))
return;
pos += sizeof(NDIS_802_11_FIXED_IEs);
/* wpa_hexdump(MSG_MSGDUMP, "IEs", pos, end - pos); */
while (pos + 1 < end && pos + 2 + pos[1] <= end) {
u8 ielen = 2 + pos[1];
if (ielen > SSID_MAX_WPA_IE_LEN) {
pos += ielen;
continue;
}
if (pos[0] == GENERIC_INFO_ELEM && pos[1] >= 4 &&
os_memcmp(pos + 2, "\x00\x50\xf2\x01", 4) == 0) {
os_memcpy(res->wpa_ie, pos, ielen);
res->wpa_ie_len = ielen;
} else if (pos[0] == RSN_INFO_ELEM) {
os_memcpy(res->rsn_ie, pos, ielen);
res->rsn_ie_len = ielen;
}
pos += ielen;
}
}
static int wpa_driver_ndis_get_scan_results(void *priv,
struct wpa_scan_result *results,
size_t max_size)
{
struct wpa_driver_ndis_data *drv = priv;
NDIS_802_11_BSSID_LIST_EX *b;
size_t blen, count, i;
int len, j;
char *pos;
blen = 65535;
b = os_zalloc(blen);
if (b == NULL)
return -1;
len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
if (len < 0) {
wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
os_free(b);
return -1;
}
count = b->NumberOfItems;
if (count > max_size)
count = max_size;
os_memset(results, 0, max_size * sizeof(struct wpa_scan_result));
pos = (char *) &b->Bssid[0];
for (i = 0; i < count; i++) {
NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos;
os_memcpy(results[i].bssid, bss->MacAddress, ETH_ALEN);
os_memcpy(results[i].ssid, bss->Ssid.Ssid,
bss->Ssid.SsidLength);
results[i].ssid_len = bss->Ssid.SsidLength;
if (bss->Privacy)
results[i].caps |= IEEE80211_CAP_PRIVACY;
if (bss->InfrastructureMode == Ndis802_11IBSS)
results[i].caps |= IEEE80211_CAP_IBSS;
else if (bss->InfrastructureMode == Ndis802_11Infrastructure)
results[i].caps |= IEEE80211_CAP_ESS;
results[i].level = (int) bss->Rssi;
results[i].freq = bss->Configuration.DSConfig / 1000;
for (j = 0; j < sizeof(bss->SupportedRates); j++) {
if ((bss->SupportedRates[j] & 0x7f) >
results[i].maxrate) {
results[i].maxrate =
bss->SupportedRates[j] & 0x7f;
}
}
if (((char *) bss->IEs) + bss->IELength > (char *) b + blen) {
/*
* Some NDIS drivers have been reported to include an
* entry with an invalid IELength in scan results and
* this has crashed wpa_supplicant, so validate the
* returned value before using it.
*/
wpa_printf(MSG_DEBUG, "NDIS: skipped invalid scan "
"result IE (BSSID=" MACSTR ") IELength=%d",
MAC2STR(results[i].bssid),
(int) bss->IELength);
break;
}
wpa_driver_ndis_get_ies(&results[i], bss->IEs, bss->IELength);
pos += bss->Length;
if (pos > (char *) b + blen)
break;
}
os_free(b);
return (int) count;
}
static int wpa_driver_ndis_remove_key(struct wpa_driver_ndis_data *drv,
int key_idx, const u8 *addr,
const u8 *bssid, int pairwise)
{
NDIS_802_11_REMOVE_KEY rkey;
NDIS_802_11_KEY_INDEX index;
int res, res2;
os_memset(&rkey, 0, sizeof(rkey));
rkey.Length = sizeof(rkey);
rkey.KeyIndex = key_idx;
if (pairwise)
rkey.KeyIndex |= 1 << 30;
os_memcpy(rkey.BSSID, bssid, ETH_ALEN);
res = ndis_set_oid(drv, OID_802_11_REMOVE_KEY, (char *) &rkey,
sizeof(rkey));
if (!pairwise) {
index = key_idx;
res2 = ndis_set_oid(drv, OID_802_11_REMOVE_WEP,
(char *) &index, sizeof(index));
} else
res2 = 0;
if (res < 0 && res2 < 0)
return -1;
return 0;
}
static int wpa_driver_ndis_add_wep(struct wpa_driver_ndis_data *drv,
int pairwise, int key_idx, int set_tx,
const u8 *key, size_t key_len)
{
NDIS_802_11_WEP *wep;
size_t len;
int res;
len = 12 + key_len;
wep = os_zalloc(len);
if (wep == NULL)
return -1;
wep->Length = len;
wep->KeyIndex = key_idx;
if (set_tx)
wep->KeyIndex |= 1 << 31;
#if 0 /* Setting bit30 does not seem to work with some NDIS drivers */
if (pairwise)
wep->KeyIndex |= 1 << 30;
#endif
wep->KeyLength = key_len;
os_memcpy(wep->KeyMaterial, key, key_len);
wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_WEP",
(char *) wep, len);
res = ndis_set_oid(drv, OID_802_11_ADD_WEP, (char *) wep, len);
os_free(wep);
return res;
}
static int wpa_driver_ndis_set_key(void *priv, wpa_alg alg, const u8 *addr,
int key_idx, int set_tx,
const u8 *seq, size_t seq_len,
const u8 *key, size_t key_len)
{
struct wpa_driver_ndis_data *drv = priv;
size_t len, i;
NDIS_802_11_KEY *nkey;
int res, pairwise;
u8 bssid[ETH_ALEN];
if (addr == NULL || os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
ETH_ALEN) == 0) {
/* Group Key */
pairwise = 0;
if (wpa_driver_ndis_get_bssid(drv, bssid) < 0)
os_memset(bssid, 0xff, ETH_ALEN);
} else {
/* Pairwise Key */
pairwise = 1;
os_memcpy(bssid, addr, ETH_ALEN);
}
if (alg == WPA_ALG_NONE || key_len == 0) {
return wpa_driver_ndis_remove_key(drv, key_idx, addr, bssid,
pairwise);
}
if (alg == WPA_ALG_WEP) {
return wpa_driver_ndis_add_wep(drv, pairwise, key_idx, set_tx,
key, key_len);
}
len = 12 + 6 + 6 + 8 + key_len;
nkey = os_zalloc(len);
if (nkey == NULL)
return -1;
nkey->Length = len;
nkey->KeyIndex = key_idx;
if (set_tx)
nkey->KeyIndex |= 1 << 31;
if (pairwise)
nkey->KeyIndex |= 1 << 30;
if (seq && seq_len)
nkey->KeyIndex |= 1 << 29;
nkey->KeyLength = key_len;
os_memcpy(nkey->BSSID, bssid, ETH_ALEN);
if (seq && seq_len) {
for (i = 0; i < seq_len; i++)
nkey->KeyRSC |= (ULONGLONG) seq[i] << (i * 8);
}
if (alg == WPA_ALG_TKIP && key_len == 32) {
os_memcpy(nkey->KeyMaterial, key, 16);
os_memcpy(nkey->KeyMaterial + 16, key + 24, 8);
os_memcpy(nkey->KeyMaterial + 24, key + 16, 8);
} else {
os_memcpy(nkey->KeyMaterial, key, key_len);
}
wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_KEY",
(char *) nkey, len);
res = ndis_set_oid(drv, OID_802_11_ADD_KEY, (char *) nkey, len);
os_free(nkey);
return res;
}
static int
wpa_driver_ndis_associate(void *priv,
struct wpa_driver_associate_params *params)
{
struct wpa_driver_ndis_data *drv = priv;
u32 auth_mode, encr, priv_mode, mode;
drv->mode = params->mode;
/* Note: Setting OID_802_11_INFRASTRUCTURE_MODE clears current keys,
* so static WEP keys needs to be set again after this. */
if (params->mode == IEEE80211_MODE_IBSS) {
mode = Ndis802_11IBSS;
/* Need to make sure that BSSID polling is enabled for
* IBSS mode. */
eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
drv, NULL);
} else
mode = Ndis802_11Infrastructure;
if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
(char *) &mode, sizeof(mode)) < 0) {
wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
"OID_802_11_INFRASTRUCTURE_MODE (%d)",
(int) mode);
/* Try to continue anyway */
}
if (params->key_mgmt_suite == KEY_MGMT_NONE ||