xref: /freebsd/contrib/wpa/src/drivers/driver_ndis.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*
2  * WPA Supplicant - Windows/NDIS driver interface
3  * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifdef __CYGWIN__
10 /* Avoid some header file conflicts by not including standard headers for
11  * cygwin builds when Packet32.h is included. */
12 #include "build_config.h"
13 int close(int fd);
14 #else /* __CYGWIN__ */
15 #include "includes.h"
16 #endif /* __CYGWIN__ */
17 #ifdef CONFIG_USE_NDISUIO
18 #include <winsock2.h>
19 #else /* CONFIG_USE_NDISUIO */
20 #include <Packet32.h>
21 #endif /* CONFIG_USE_NDISUIO */
22 #ifdef __MINGW32_VERSION
23 #include <ddk/ntddndis.h>
24 #else /* __MINGW32_VERSION */
25 #include <ntddndis.h>
26 #endif /* __MINGW32_VERSION */
27 
28 #ifdef _WIN32_WCE
29 #include <winioctl.h>
30 #include <nuiouser.h>
31 #include <devload.h>
32 #endif /* _WIN32_WCE */
33 
34 #include "common.h"
35 #include "driver.h"
36 #include "eloop.h"
37 #include "common/ieee802_11_defs.h"
38 #include "common/ieee802_11_common.h"
39 #include "driver_ndis.h"
40 
41 int wpa_driver_register_event_cb(struct wpa_driver_ndis_data *drv);
42 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
43 void wpa_driver_ndis_event_pipe_cb(void *eloop_data, void *user_data);
44 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
45 
46 static void wpa_driver_ndis_deinit(void *priv);
47 static void wpa_driver_ndis_poll(void *drv);
48 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx);
49 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv);
50 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv);
51 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv);
52 
53 
54 static const u8 pae_group_addr[ETH_ALEN] =
55 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
56 
57 
58 /* FIX: to be removed once this can be compiled with the complete NDIS
59  * header files */
60 #ifndef OID_802_11_BSSID
61 #define OID_802_11_BSSID 			0x0d010101
62 #define OID_802_11_SSID 			0x0d010102
63 #define OID_802_11_INFRASTRUCTURE_MODE		0x0d010108
64 #define OID_802_11_ADD_WEP			0x0D010113
65 #define OID_802_11_REMOVE_WEP			0x0D010114
66 #define OID_802_11_DISASSOCIATE			0x0D010115
67 #define OID_802_11_BSSID_LIST 			0x0d010217
68 #define OID_802_11_AUTHENTICATION_MODE		0x0d010118
69 #define OID_802_11_PRIVACY_FILTER		0x0d010119
70 #define OID_802_11_BSSID_LIST_SCAN 		0x0d01011A
71 #define OID_802_11_WEP_STATUS	 		0x0d01011B
72 #define OID_802_11_ENCRYPTION_STATUS OID_802_11_WEP_STATUS
73 #define OID_802_11_ADD_KEY 			0x0d01011D
74 #define OID_802_11_REMOVE_KEY 			0x0d01011E
75 #define OID_802_11_ASSOCIATION_INFORMATION	0x0d01011F
76 #define OID_802_11_TEST 			0x0d010120
77 #define OID_802_11_CAPABILITY 			0x0d010122
78 #define OID_802_11_PMKID 			0x0d010123
79 
80 #define NDIS_802_11_LENGTH_SSID 32
81 #define NDIS_802_11_LENGTH_RATES 8
82 #define NDIS_802_11_LENGTH_RATES_EX 16
83 
84 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
85 
86 typedef struct NDIS_802_11_SSID {
87 	ULONG SsidLength;
88 	UCHAR Ssid[NDIS_802_11_LENGTH_SSID];
89 } NDIS_802_11_SSID;
90 
91 typedef LONG NDIS_802_11_RSSI;
92 
93 typedef enum NDIS_802_11_NETWORK_TYPE {
94 	Ndis802_11FH,
95 	Ndis802_11DS,
96 	Ndis802_11OFDM5,
97 	Ndis802_11OFDM24,
98 	Ndis802_11NetworkTypeMax
99 } NDIS_802_11_NETWORK_TYPE;
100 
101 typedef struct NDIS_802_11_CONFIGURATION_FH {
102 	ULONG Length;
103 	ULONG HopPattern;
104 	ULONG HopSet;
105 	ULONG DwellTime;
106 } NDIS_802_11_CONFIGURATION_FH;
107 
108 typedef struct NDIS_802_11_CONFIGURATION {
109 	ULONG Length;
110 	ULONG BeaconPeriod;
111 	ULONG ATIMWindow;
112 	ULONG DSConfig;
113 	NDIS_802_11_CONFIGURATION_FH FHConfig;
114 } NDIS_802_11_CONFIGURATION;
115 
116 typedef enum NDIS_802_11_NETWORK_INFRASTRUCTURE {
117 	Ndis802_11IBSS,
118 	Ndis802_11Infrastructure,
119 	Ndis802_11AutoUnknown,
120 	Ndis802_11InfrastructureMax
121 } NDIS_802_11_NETWORK_INFRASTRUCTURE;
122 
123 typedef enum NDIS_802_11_AUTHENTICATION_MODE {
124 	Ndis802_11AuthModeOpen,
125 	Ndis802_11AuthModeShared,
126 	Ndis802_11AuthModeAutoSwitch,
127 	Ndis802_11AuthModeWPA,
128 	Ndis802_11AuthModeWPAPSK,
129 	Ndis802_11AuthModeWPANone,
130 	Ndis802_11AuthModeWPA2,
131 	Ndis802_11AuthModeWPA2PSK,
132 	Ndis802_11AuthModeMax
133 } NDIS_802_11_AUTHENTICATION_MODE;
134 
135 typedef enum NDIS_802_11_WEP_STATUS {
136 	Ndis802_11WEPEnabled,
137 	Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
138 	Ndis802_11WEPDisabled,
139 	Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
140 	Ndis802_11WEPKeyAbsent,
141 	Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
142 	Ndis802_11WEPNotSupported,
143 	Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
144 	Ndis802_11Encryption2Enabled,
145 	Ndis802_11Encryption2KeyAbsent,
146 	Ndis802_11Encryption3Enabled,
147 	Ndis802_11Encryption3KeyAbsent
148 } NDIS_802_11_WEP_STATUS, NDIS_802_11_ENCRYPTION_STATUS;
149 
150 typedef enum NDIS_802_11_PRIVACY_FILTER {
151 	Ndis802_11PrivFilterAcceptAll,
152 	Ndis802_11PrivFilter8021xWEP
153 } NDIS_802_11_PRIVACY_FILTER;
154 
155 typedef UCHAR NDIS_802_11_RATES[NDIS_802_11_LENGTH_RATES];
156 typedef UCHAR NDIS_802_11_RATES_EX[NDIS_802_11_LENGTH_RATES_EX];
157 
158 typedef struct NDIS_WLAN_BSSID_EX {
159 	ULONG Length;
160 	NDIS_802_11_MAC_ADDRESS MacAddress; /* BSSID */
161 	UCHAR Reserved[2];
162 	NDIS_802_11_SSID Ssid;
163 	ULONG Privacy;
164 	NDIS_802_11_RSSI Rssi;
165 	NDIS_802_11_NETWORK_TYPE NetworkTypeInUse;
166 	NDIS_802_11_CONFIGURATION Configuration;
167 	NDIS_802_11_NETWORK_INFRASTRUCTURE InfrastructureMode;
168 	NDIS_802_11_RATES_EX SupportedRates;
169 	ULONG IELength;
170 	UCHAR IEs[1];
171 } NDIS_WLAN_BSSID_EX;
172 
173 typedef struct NDIS_802_11_BSSID_LIST_EX {
174 	ULONG NumberOfItems;
175 	NDIS_WLAN_BSSID_EX Bssid[1];
176 } NDIS_802_11_BSSID_LIST_EX;
177 
178 typedef struct NDIS_802_11_FIXED_IEs {
179 	UCHAR Timestamp[8];
180 	USHORT BeaconInterval;
181 	USHORT Capabilities;
182 } NDIS_802_11_FIXED_IEs;
183 
184 typedef struct NDIS_802_11_WEP {
185 	ULONG Length;
186 	ULONG KeyIndex;
187 	ULONG KeyLength;
188 	UCHAR KeyMaterial[1];
189 } NDIS_802_11_WEP;
190 
191 typedef ULONG NDIS_802_11_KEY_INDEX;
192 typedef ULONGLONG NDIS_802_11_KEY_RSC;
193 
194 typedef struct NDIS_802_11_KEY {
195 	ULONG Length;
196 	ULONG KeyIndex;
197 	ULONG KeyLength;
198 	NDIS_802_11_MAC_ADDRESS BSSID;
199 	NDIS_802_11_KEY_RSC KeyRSC;
200 	UCHAR KeyMaterial[1];
201 } NDIS_802_11_KEY;
202 
203 typedef struct NDIS_802_11_REMOVE_KEY {
204 	ULONG Length;
205 	ULONG KeyIndex;
206 	NDIS_802_11_MAC_ADDRESS BSSID;
207 } NDIS_802_11_REMOVE_KEY;
208 
209 typedef struct NDIS_802_11_AI_REQFI {
210 	USHORT Capabilities;
211 	USHORT ListenInterval;
212 	NDIS_802_11_MAC_ADDRESS CurrentAPAddress;
213 } NDIS_802_11_AI_REQFI;
214 
215 typedef struct NDIS_802_11_AI_RESFI {
216 	USHORT Capabilities;
217 	USHORT StatusCode;
218 	USHORT AssociationId;
219 } NDIS_802_11_AI_RESFI;
220 
221 typedef struct NDIS_802_11_ASSOCIATION_INFORMATION {
222 	ULONG Length;
223 	USHORT AvailableRequestFixedIEs;
224 	NDIS_802_11_AI_REQFI RequestFixedIEs;
225 	ULONG RequestIELength;
226 	ULONG OffsetRequestIEs;
227 	USHORT AvailableResponseFixedIEs;
228 	NDIS_802_11_AI_RESFI ResponseFixedIEs;
229 	ULONG ResponseIELength;
230 	ULONG OffsetResponseIEs;
231 } NDIS_802_11_ASSOCIATION_INFORMATION;
232 
233 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
234 	NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
235 	NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
236 } NDIS_802_11_AUTHENTICATION_ENCRYPTION;
237 
238 typedef struct NDIS_802_11_CAPABILITY {
239 	ULONG Length;
240 	ULONG Version;
241 	ULONG NoOfPMKIDs;
242 	ULONG NoOfAuthEncryptPairsSupported;
243 	NDIS_802_11_AUTHENTICATION_ENCRYPTION
244 		AuthenticationEncryptionSupported[1];
245 } NDIS_802_11_CAPABILITY;
246 
247 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
248 
249 typedef struct BSSID_INFO {
250 	NDIS_802_11_MAC_ADDRESS BSSID;
251 	NDIS_802_11_PMKID_VALUE PMKID;
252 } BSSID_INFO;
253 
254 typedef struct NDIS_802_11_PMKID {
255 	ULONG Length;
256 	ULONG BSSIDInfoCount;
257 	BSSID_INFO BSSIDInfo[1];
258 } NDIS_802_11_PMKID;
259 
260 typedef enum NDIS_802_11_STATUS_TYPE {
261 	Ndis802_11StatusType_Authentication,
262 	Ndis802_11StatusType_PMKID_CandidateList = 2,
263 	Ndis802_11StatusTypeMax
264 } NDIS_802_11_STATUS_TYPE;
265 
266 typedef struct NDIS_802_11_STATUS_INDICATION {
267 	NDIS_802_11_STATUS_TYPE StatusType;
268 } NDIS_802_11_STATUS_INDICATION;
269 
270 typedef struct PMKID_CANDIDATE {
271 	NDIS_802_11_MAC_ADDRESS BSSID;
272 	ULONG Flags;
273 } PMKID_CANDIDATE;
274 
275 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
276 
277 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
278 	ULONG Version;
279 	ULONG NumCandidates;
280 	PMKID_CANDIDATE CandidateList[1];
281 } NDIS_802_11_PMKID_CANDIDATE_LIST;
282 
283 typedef struct NDIS_802_11_AUTHENTICATION_REQUEST {
284 	ULONG Length;
285 	NDIS_802_11_MAC_ADDRESS Bssid;
286 	ULONG Flags;
287 } NDIS_802_11_AUTHENTICATION_REQUEST;
288 
289 #define NDIS_802_11_AUTH_REQUEST_REAUTH			0x01
290 #define NDIS_802_11_AUTH_REQUEST_KEYUPDATE		0x02
291 #define NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR		0x06
292 #define NDIS_802_11_AUTH_REQUEST_GROUP_ERROR		0x0E
293 
294 #endif /* OID_802_11_BSSID */
295 
296 
297 #ifndef OID_802_11_PMKID
298 /* Platform SDK for XP did not include WPA2, so add needed definitions */
299 
300 #define OID_802_11_CAPABILITY 			0x0d010122
301 #define OID_802_11_PMKID 			0x0d010123
302 
303 #define Ndis802_11AuthModeWPA2 6
304 #define Ndis802_11AuthModeWPA2PSK 7
305 
306 #define Ndis802_11StatusType_PMKID_CandidateList 2
307 
308 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
309 	NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
310 	NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
311 } NDIS_802_11_AUTHENTICATION_ENCRYPTION;
312 
313 typedef struct NDIS_802_11_CAPABILITY {
314 	ULONG Length;
315 	ULONG Version;
316 	ULONG NoOfPMKIDs;
317 	ULONG NoOfAuthEncryptPairsSupported;
318 	NDIS_802_11_AUTHENTICATION_ENCRYPTION
319 		AuthenticationEncryptionSupported[1];
320 } NDIS_802_11_CAPABILITY;
321 
322 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
323 
324 typedef struct BSSID_INFO {
325 	NDIS_802_11_MAC_ADDRESS BSSID;
326 	NDIS_802_11_PMKID_VALUE PMKID;
327 } BSSID_INFO;
328 
329 typedef struct NDIS_802_11_PMKID {
330 	ULONG Length;
331 	ULONG BSSIDInfoCount;
332 	BSSID_INFO BSSIDInfo[1];
333 } NDIS_802_11_PMKID;
334 
335 typedef struct PMKID_CANDIDATE {
336 	NDIS_802_11_MAC_ADDRESS BSSID;
337 	ULONG Flags;
338 } PMKID_CANDIDATE;
339 
340 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
341 
342 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
343 	ULONG Version;
344 	ULONG NumCandidates;
345 	PMKID_CANDIDATE CandidateList[1];
346 } NDIS_802_11_PMKID_CANDIDATE_LIST;
347 
348 #endif /* OID_802_11_CAPABILITY */
349 
350 
351 #ifndef OID_DOT11_CURRENT_OPERATION_MODE
352 /* Native 802.11 OIDs */
353 #define OID_DOT11_NDIS_START 0x0D010300
354 #define OID_DOT11_CURRENT_OPERATION_MODE (OID_DOT11_NDIS_START + 8)
355 #define OID_DOT11_SCAN_REQUEST (OID_DOT11_NDIS_START + 11)
356 
357 typedef enum _DOT11_BSS_TYPE {
358 	dot11_BSS_type_infrastructure = 1,
359 	dot11_BSS_type_independent = 2,
360 	dot11_BSS_type_any = 3
361 } DOT11_BSS_TYPE, * PDOT11_BSS_TYPE;
362 
363 typedef UCHAR DOT11_MAC_ADDRESS[6];
364 typedef DOT11_MAC_ADDRESS * PDOT11_MAC_ADDRESS;
365 
366 typedef enum _DOT11_SCAN_TYPE {
367 	dot11_scan_type_active = 1,
368 	dot11_scan_type_passive = 2,
369 	dot11_scan_type_auto = 3,
370 	dot11_scan_type_forced = 0x80000000
371 } DOT11_SCAN_TYPE, * PDOT11_SCAN_TYPE;
372 
373 typedef struct _DOT11_SCAN_REQUEST_V2 {
374 	DOT11_BSS_TYPE dot11BSSType;
375 	DOT11_MAC_ADDRESS dot11BSSID;
376 	DOT11_SCAN_TYPE dot11ScanType;
377 	BOOLEAN bRestrictedScan;
378 	ULONG udot11SSIDsOffset;
379 	ULONG uNumOfdot11SSIDs;
380 	BOOLEAN bUseRequestIE;
381 	ULONG uRequestIDsOffset;
382 	ULONG uNumOfRequestIDs;
383 	ULONG uPhyTypeInfosOffset;
384 	ULONG uNumOfPhyTypeInfos;
385 	ULONG uIEsOffset;
386 	ULONG uIEsLength;
387 	UCHAR ucBuffer[1];
388 } DOT11_SCAN_REQUEST_V2, * PDOT11_SCAN_REQUEST_V2;
389 
390 #endif /* OID_DOT11_CURRENT_OPERATION_MODE */
391 
392 #ifdef CONFIG_USE_NDISUIO
393 #ifndef _WIN32_WCE
394 #ifdef __MINGW32_VERSION
395 typedef ULONG NDIS_OID;
396 #endif /* __MINGW32_VERSION */
397 /* from nuiouser.h */
398 #define FSCTL_NDISUIO_BASE      FILE_DEVICE_NETWORK
399 
400 #define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \
401 	CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access)
402 
403 #define IOCTL_NDISUIO_OPEN_DEVICE \
404 	_NDISUIO_CTL_CODE(0x200, METHOD_BUFFERED, \
405 			  FILE_READ_ACCESS | FILE_WRITE_ACCESS)
406 
407 #define IOCTL_NDISUIO_QUERY_OID_VALUE \
408 	_NDISUIO_CTL_CODE(0x201, METHOD_BUFFERED, \
409 			  FILE_READ_ACCESS | FILE_WRITE_ACCESS)
410 
411 #define IOCTL_NDISUIO_SET_OID_VALUE \
412 	_NDISUIO_CTL_CODE(0x205, METHOD_BUFFERED, \
413 			  FILE_READ_ACCESS | FILE_WRITE_ACCESS)
414 
415 #define IOCTL_NDISUIO_SET_ETHER_TYPE \
416 	_NDISUIO_CTL_CODE(0x202, METHOD_BUFFERED, \
417 			  FILE_READ_ACCESS | FILE_WRITE_ACCESS)
418 
419 #define IOCTL_NDISUIO_QUERY_BINDING \
420 	_NDISUIO_CTL_CODE(0x203, METHOD_BUFFERED, \
421 			  FILE_READ_ACCESS | FILE_WRITE_ACCESS)
422 
423 #define IOCTL_NDISUIO_BIND_WAIT \
424 	_NDISUIO_CTL_CODE(0x204, METHOD_BUFFERED, \
425 			  FILE_READ_ACCESS | FILE_WRITE_ACCESS)
426 
427 typedef struct _NDISUIO_QUERY_OID
428 {
429     NDIS_OID Oid;
430     UCHAR Data[sizeof(ULONG)];
431 } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;
432 
433 typedef struct _NDISUIO_SET_OID
434 {
435     NDIS_OID Oid;
436     UCHAR Data[sizeof(ULONG)];
437 } NDISUIO_SET_OID, *PNDISUIO_SET_OID;
438 
439 typedef struct _NDISUIO_QUERY_BINDING
440 {
441 	ULONG BindingIndex;
442 	ULONG DeviceNameOffset;
443 	ULONG DeviceNameLength;
444 	ULONG DeviceDescrOffset;
445 	ULONG DeviceDescrLength;
446 } NDISUIO_QUERY_BINDING, *PNDISUIO_QUERY_BINDING;
447 #endif /* _WIN32_WCE */
448 #endif /* CONFIG_USE_NDISUIO */
449 
450 
451 static int ndis_get_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
452 			char *data, size_t len)
453 {
454 #ifdef CONFIG_USE_NDISUIO
455 	NDISUIO_QUERY_OID *o;
456 	size_t buflen = sizeof(*o) + len;
457 	DWORD written;
458 	int ret;
459 	size_t hdrlen;
460 
461 	o = os_zalloc(buflen);
462 	if (o == NULL)
463 		return -1;
464 	o->Oid = oid;
465 #ifdef _WIN32_WCE
466 	o->ptcDeviceName = drv->adapter_name;
467 #endif /* _WIN32_WCE */
468 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_OID_VALUE,
469 			     o, sizeof(NDISUIO_QUERY_OID), o, buflen, &written,
470 			     NULL)) {
471 		wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_QUERY_OID_VALUE "
472 			   "failed (oid=%08x): %d", oid, (int) GetLastError());
473 		os_free(o);
474 		return -1;
475 	}
476 	hdrlen = sizeof(NDISUIO_QUERY_OID) - sizeof(o->Data);
477 	if (written < hdrlen) {
478 		wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d); "
479 			   "too short", oid, (unsigned int) written);
480 		os_free(o);
481 		return -1;
482 	}
483 	written -= hdrlen;
484 	if (written > len) {
485 		wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d) > "
486 			   "len (%d)",oid, (unsigned int) written, len);
487 		os_free(o);
488 		return -1;
489 	}
490 	os_memcpy(data, o->Data, written);
491 	ret = written;
492 	os_free(o);
493 	return ret;
494 #else /* CONFIG_USE_NDISUIO */
495 	char *buf;
496 	PACKET_OID_DATA *o;
497 	int ret;
498 
499 	buf = os_zalloc(sizeof(*o) + len);
500 	if (buf == NULL)
501 		return -1;
502 	o = (PACKET_OID_DATA *) buf;
503 	o->Oid = oid;
504 	o->Length = len;
505 
506 	if (!PacketRequest(drv->adapter, FALSE, o)) {
507 		wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%lu) failed",
508 			   __func__, oid, len);
509 		os_free(buf);
510 		return -1;
511 	}
512 	if (o->Length > len) {
513 		wpa_printf(MSG_DEBUG, "%s: oid=0x%x Length (%d) > len (%lu)",
514 			   __func__, oid, (unsigned int) o->Length, len);
515 		os_free(buf);
516 		return -1;
517 	}
518 	os_memcpy(data, o->Data, o->Length);
519 	ret = o->Length;
520 	os_free(buf);
521 	return ret;
522 #endif /* CONFIG_USE_NDISUIO */
523 }
524 
525 
526 static int ndis_set_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
527 			const char *data, size_t len)
528 {
529 #ifdef CONFIG_USE_NDISUIO
530 	NDISUIO_SET_OID *o;
531 	size_t buflen, reallen;
532 	DWORD written;
533 	char txt[50];
534 
535 	os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
536 	wpa_hexdump_key(MSG_MSGDUMP, txt, (const u8 *) data, len);
537 
538 	buflen = sizeof(*o) + len;
539 	reallen = buflen - sizeof(o->Data);
540 	o = os_zalloc(buflen);
541 	if (o == NULL)
542 		return -1;
543 	o->Oid = oid;
544 #ifdef _WIN32_WCE
545 	o->ptcDeviceName = drv->adapter_name;
546 #endif /* _WIN32_WCE */
547 	if (data)
548 		os_memcpy(o->Data, data, len);
549 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_SET_OID_VALUE,
550 			     o, reallen, NULL, 0, &written, NULL)) {
551 		wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_SET_OID_VALUE "
552 			   "(oid=%08x) failed: %d", oid, (int) GetLastError());
553 		os_free(o);
554 		return -1;
555 	}
556 	os_free(o);
557 	return 0;
558 #else /* CONFIG_USE_NDISUIO */
559 	char *buf;
560 	PACKET_OID_DATA *o;
561 	char txt[50];
562 
563 	os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
564 	wpa_hexdump_key(MSG_MSGDUMP, txt, (const u8 *) data, len);
565 
566 	buf = os_zalloc(sizeof(*o) + len);
567 	if (buf == NULL)
568 		return -1;
569 	o = (PACKET_OID_DATA *) buf;
570 	o->Oid = oid;
571 	o->Length = len;
572 	if (data)
573 		os_memcpy(o->Data, data, len);
574 
575 	if (!PacketRequest(drv->adapter, TRUE, o)) {
576 		wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%lu) failed",
577 			   __func__, oid, len);
578 		os_free(buf);
579 		return -1;
580 	}
581 	os_free(buf);
582 	return 0;
583 #endif /* CONFIG_USE_NDISUIO */
584 }
585 
586 
587 static int ndis_set_auth_mode(struct wpa_driver_ndis_data *drv, int mode)
588 {
589 	u32 auth_mode = mode;
590 	if (ndis_set_oid(drv, OID_802_11_AUTHENTICATION_MODE,
591 			 (char *) &auth_mode, sizeof(auth_mode)) < 0) {
592 		wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
593 			   "OID_802_11_AUTHENTICATION_MODE (%d)",
594 			   (int) auth_mode);
595 		return -1;
596 	}
597 	return 0;
598 }
599 
600 
601 static int ndis_get_auth_mode(struct wpa_driver_ndis_data *drv)
602 {
603 	u32 auth_mode;
604 	int res;
605 	res = ndis_get_oid(drv, OID_802_11_AUTHENTICATION_MODE,
606 			   (char *) &auth_mode, sizeof(auth_mode));
607 	if (res != sizeof(auth_mode)) {
608 		wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
609 			   "OID_802_11_AUTHENTICATION_MODE");
610 		return -1;
611 	}
612 	return auth_mode;
613 }
614 
615 
616 static int ndis_set_encr_status(struct wpa_driver_ndis_data *drv, int encr)
617 {
618 	u32 encr_status = encr;
619 	if (ndis_set_oid(drv, OID_802_11_ENCRYPTION_STATUS,
620 			 (char *) &encr_status, sizeof(encr_status)) < 0) {
621 		wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
622 			   "OID_802_11_ENCRYPTION_STATUS (%d)", encr);
623 		return -1;
624 	}
625 	return 0;
626 }
627 
628 
629 static int ndis_get_encr_status(struct wpa_driver_ndis_data *drv)
630 {
631 	u32 encr;
632 	int res;
633 	res = ndis_get_oid(drv, OID_802_11_ENCRYPTION_STATUS,
634 			   (char *) &encr, sizeof(encr));
635 	if (res != sizeof(encr)) {
636 		wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
637 			   "OID_802_11_ENCRYPTION_STATUS");
638 		return -1;
639 	}
640 	return encr;
641 }
642 
643 
644 static int wpa_driver_ndis_get_bssid(void *priv, u8 *bssid)
645 {
646 	struct wpa_driver_ndis_data *drv = priv;
647 
648 	if (drv->wired) {
649 		/*
650 		 * Report PAE group address as the "BSSID" for wired
651 		 * connection.
652 		 */
653 		os_memcpy(bssid, pae_group_addr, ETH_ALEN);
654 		return 0;
655 	}
656 
657 	return ndis_get_oid(drv, OID_802_11_BSSID, (char *) bssid, ETH_ALEN) <
658 		0 ? -1 : 0;
659 }
660 
661 
662 static int wpa_driver_ndis_get_ssid(void *priv, u8 *ssid)
663 {
664 	struct wpa_driver_ndis_data *drv = priv;
665 	NDIS_802_11_SSID buf;
666 	int res;
667 
668 	res = ndis_get_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
669 	if (res < 4) {
670 		wpa_printf(MSG_DEBUG, "NDIS: Failed to get SSID");
671 		if (drv->wired) {
672 			wpa_printf(MSG_DEBUG, "NDIS: Allow get_ssid failure "
673 				   "with a wired interface");
674 			return 0;
675 		}
676 		return -1;
677 	}
678 	os_memcpy(ssid, buf.Ssid, buf.SsidLength);
679 	return buf.SsidLength;
680 }
681 
682 
683 static int wpa_driver_ndis_set_ssid(struct wpa_driver_ndis_data *drv,
684 				    const u8 *ssid, size_t ssid_len)
685 {
686 	NDIS_802_11_SSID buf;
687 
688 	os_memset(&buf, 0, sizeof(buf));
689 	buf.SsidLength = ssid_len;
690 	os_memcpy(buf.Ssid, ssid, ssid_len);
691 	/*
692 	 * Make sure radio is marked enabled here so that scan request will not
693 	 * force SSID to be changed to a random one in order to enable radio at
694 	 * that point.
695 	 */
696 	drv->radio_enabled = 1;
697 	return ndis_set_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
698 }
699 
700 
701 /* Disconnect using OID_802_11_DISASSOCIATE. This will also turn the radio off.
702  */
703 static int wpa_driver_ndis_radio_off(struct wpa_driver_ndis_data *drv)
704 {
705 	drv->radio_enabled = 0;
706 	return ndis_set_oid(drv, OID_802_11_DISASSOCIATE, "    ", 4);
707 }
708 
709 
710 /* Disconnect by setting SSID to random (i.e., likely not used). */
711 static int wpa_driver_ndis_disconnect(struct wpa_driver_ndis_data *drv)
712 {
713 	char ssid[SSID_MAX_LEN];
714 	int i;
715 	for (i = 0; i < SSID_MAX_LEN; i++)
716 		ssid[i] = rand() & 0xff;
717 	return wpa_driver_ndis_set_ssid(drv, (u8 *) ssid, SSID_MAX_LEN);
718 }
719 
720 
721 static int wpa_driver_ndis_deauthenticate(void *priv, const u8 *addr,
722 					  u16 reason_code)
723 {
724 	struct wpa_driver_ndis_data *drv = priv;
725 	return wpa_driver_ndis_disconnect(drv);
726 }
727 
728 
729 static void wpa_driver_ndis_scan_timeout(void *eloop_ctx, void *timeout_ctx)
730 {
731 	wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
732 	wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
733 }
734 
735 
736 static int wpa_driver_ndis_scan_native80211(
737 	struct wpa_driver_ndis_data *drv,
738 	struct wpa_driver_scan_params *params)
739 {
740 	DOT11_SCAN_REQUEST_V2 req;
741 	int res;
742 
743 	os_memset(&req, 0, sizeof(req));
744 	req.dot11BSSType = dot11_BSS_type_any;
745 	os_memset(req.dot11BSSID, 0xff, ETH_ALEN);
746 	req.dot11ScanType = dot11_scan_type_auto;
747 	res = ndis_set_oid(drv, OID_DOT11_SCAN_REQUEST, (char *) &req,
748 			   sizeof(req));
749 	eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
750 	eloop_register_timeout(7, 0, wpa_driver_ndis_scan_timeout, drv,
751 			       drv->ctx);
752 	return res;
753 }
754 
755 
756 static int wpa_driver_ndis_scan(void *priv,
757 				struct wpa_driver_scan_params *params)
758 {
759 	struct wpa_driver_ndis_data *drv = priv;
760 	int res;
761 
762 	if (drv->native80211)
763 		return wpa_driver_ndis_scan_native80211(drv, params);
764 
765 	if (!drv->radio_enabled) {
766 		wpa_printf(MSG_DEBUG, "NDIS: turning radio on before the first"
767 			   " scan");
768 		if (wpa_driver_ndis_disconnect(drv) < 0) {
769 			wpa_printf(MSG_DEBUG, "NDIS: failed to enable radio");
770 		}
771 		drv->radio_enabled = 1;
772 	}
773 
774 	res = ndis_set_oid(drv, OID_802_11_BSSID_LIST_SCAN, "    ", 4);
775 	eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
776 	eloop_register_timeout(7, 0, wpa_driver_ndis_scan_timeout, drv,
777 			       drv->ctx);
778 	return res;
779 }
780 
781 
782 static const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
783 {
784 	return get_ie((const u8 *) (res + 1), res->ie_len, ie);
785 }
786 
787 
788 static struct wpa_scan_res * wpa_driver_ndis_add_scan_ssid(
789 	struct wpa_scan_res *r, NDIS_802_11_SSID *ssid)
790 {
791 	struct wpa_scan_res *nr;
792 	u8 *pos;
793 
794 	if (wpa_scan_get_ie(r, WLAN_EID_SSID))
795 		return r; /* SSID IE already present */
796 
797 	if (ssid->SsidLength == 0 || ssid->SsidLength > SSID_MAX_LEN)
798 		return r; /* No valid SSID inside scan data */
799 
800 	nr = os_realloc(r, sizeof(*r) + r->ie_len + 2 + ssid->SsidLength);
801 	if (nr == NULL)
802 		return r;
803 
804 	pos = ((u8 *) (nr + 1)) + nr->ie_len;
805 	*pos++ = WLAN_EID_SSID;
806 	*pos++ = ssid->SsidLength;
807 	os_memcpy(pos, ssid->Ssid, ssid->SsidLength);
808 	nr->ie_len += 2 + ssid->SsidLength;
809 
810 	return nr;
811 }
812 
813 
814 static struct wpa_scan_results * wpa_driver_ndis_get_scan_results(void *priv)
815 {
816 	struct wpa_driver_ndis_data *drv = priv;
817 	NDIS_802_11_BSSID_LIST_EX *b;
818 	size_t blen, count, i;
819 	int len;
820 	char *pos;
821 	struct wpa_scan_results *results;
822 	struct wpa_scan_res *r;
823 
824 	blen = 65535;
825 	b = os_zalloc(blen);
826 	if (b == NULL)
827 		return NULL;
828 	len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
829 	if (len < 0) {
830 		wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
831 		os_free(b);
832 		return NULL;
833 	}
834 	count = b->NumberOfItems;
835 
836 	results = os_zalloc(sizeof(*results));
837 	if (results == NULL) {
838 		os_free(b);
839 		return NULL;
840 	}
841 	results->res = os_calloc(count, sizeof(struct wpa_scan_res *));
842 	if (results->res == NULL) {
843 		os_free(results);
844 		os_free(b);
845 		return NULL;
846 	}
847 
848 	pos = (char *) &b->Bssid[0];
849 	for (i = 0; i < count; i++) {
850 		NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos;
851 		NDIS_802_11_FIXED_IEs *fixed;
852 
853 		if (bss->IELength < sizeof(NDIS_802_11_FIXED_IEs)) {
854 			wpa_printf(MSG_DEBUG, "NDIS: too small IELength=%d",
855 				   (int) bss->IELength);
856 			break;
857 		}
858 		if (((char *) bss->IEs) + bss->IELength  > (char *) b + blen) {
859 			/*
860 			 * Some NDIS drivers have been reported to include an
861 			 * entry with an invalid IELength in scan results and
862 			 * this has crashed wpa_supplicant, so validate the
863 			 * returned value before using it.
864 			 */
865 			wpa_printf(MSG_DEBUG, "NDIS: skipped invalid scan "
866 				   "result IE (BSSID=" MACSTR ") IELength=%d",
867 				   MAC2STR(bss->MacAddress),
868 				   (int) bss->IELength);
869 			break;
870 		}
871 
872 		r = os_zalloc(sizeof(*r) + bss->IELength -
873 			      sizeof(NDIS_802_11_FIXED_IEs));
874 		if (r == NULL)
875 			break;
876 
877 		os_memcpy(r->bssid, bss->MacAddress, ETH_ALEN);
878 		r->level = (int) bss->Rssi;
879 		r->freq = bss->Configuration.DSConfig / 1000;
880 		fixed = (NDIS_802_11_FIXED_IEs *) bss->IEs;
881 		r->beacon_int = WPA_GET_LE16((u8 *) &fixed->BeaconInterval);
882 		r->caps = WPA_GET_LE16((u8 *) &fixed->Capabilities);
883 		r->tsf = WPA_GET_LE64(fixed->Timestamp);
884 		os_memcpy(r + 1, bss->IEs + sizeof(NDIS_802_11_FIXED_IEs),
885 			  bss->IELength - sizeof(NDIS_802_11_FIXED_IEs));
886 		r->ie_len = bss->IELength - sizeof(NDIS_802_11_FIXED_IEs);
887 		r = wpa_driver_ndis_add_scan_ssid(r, &bss->Ssid);
888 
889 		results->res[results->num++] = r;
890 
891 		pos += bss->Length;
892 		if (pos > (char *) b + blen)
893 			break;
894 	}
895 
896 	os_free(b);
897 
898 	return results;
899 }
900 
901 
902 static int wpa_driver_ndis_remove_key(struct wpa_driver_ndis_data *drv,
903 				      int key_idx, const u8 *addr,
904 				      const u8 *bssid, int pairwise)
905 {
906 	NDIS_802_11_REMOVE_KEY rkey;
907 	NDIS_802_11_KEY_INDEX index;
908 	int res, res2;
909 
910 	os_memset(&rkey, 0, sizeof(rkey));
911 
912 	rkey.Length = sizeof(rkey);
913 	rkey.KeyIndex = key_idx;
914 	if (pairwise)
915 		rkey.KeyIndex |= 1 << 30;
916 	os_memcpy(rkey.BSSID, bssid, ETH_ALEN);
917 
918 	res = ndis_set_oid(drv, OID_802_11_REMOVE_KEY, (char *) &rkey,
919 			   sizeof(rkey));
920 	if (!pairwise) {
921 		index = key_idx;
922 		res2 = ndis_set_oid(drv, OID_802_11_REMOVE_WEP,
923 				    (char *) &index, sizeof(index));
924 	} else
925 		res2 = 0;
926 
927 	if (res < 0 && res2 < 0)
928 		return -1;
929 	return 0;
930 }
931 
932 
933 static int wpa_driver_ndis_add_wep(struct wpa_driver_ndis_data *drv,
934 				   int pairwise, int key_idx, int set_tx,
935 				   const u8 *key, size_t key_len)
936 {
937 	NDIS_802_11_WEP *wep;
938 	size_t len;
939 	int res;
940 
941 	len = 12 + key_len;
942 	wep = os_zalloc(len);
943 	if (wep == NULL)
944 		return -1;
945 	wep->Length = len;
946 	wep->KeyIndex = key_idx;
947 	if (set_tx)
948 		wep->KeyIndex |= 1 << 31;
949 #if 0 /* Setting bit30 does not seem to work with some NDIS drivers */
950 	if (pairwise)
951 		wep->KeyIndex |= 1 << 30;
952 #endif
953 	wep->KeyLength = key_len;
954 	os_memcpy(wep->KeyMaterial, key, key_len);
955 
956 	wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_WEP",
957 			(u8 *) wep, len);
958 	res = ndis_set_oid(drv, OID_802_11_ADD_WEP, (char *) wep, len);
959 
960 	os_free(wep);
961 
962 	return res;
963 }
964 
965 
966 static int wpa_driver_ndis_set_key(const char *ifname, void *priv,
967 				   enum wpa_alg alg, const u8 *addr,
968 				   int key_idx, int set_tx,
969 				   const u8 *seq, size_t seq_len,
970 				   const u8 *key, size_t key_len)
971 {
972 	struct wpa_driver_ndis_data *drv = priv;
973 	size_t len, i;
974 	NDIS_802_11_KEY *nkey;
975 	int res, pairwise;
976 	u8 bssid[ETH_ALEN];
977 
978 	if (addr == NULL || is_broadcast_ether_addr(addr)) {
979 		/* Group Key */
980 		pairwise = 0;
981 		if (wpa_driver_ndis_get_bssid(drv, bssid) < 0)
982 			os_memset(bssid, 0xff, ETH_ALEN);
983 	} else {
984 		/* Pairwise Key */
985 		pairwise = 1;
986 		os_memcpy(bssid, addr, ETH_ALEN);
987 	}
988 
989 	if (alg == WPA_ALG_NONE || key_len == 0) {
990 		return wpa_driver_ndis_remove_key(drv, key_idx, addr, bssid,
991 						  pairwise);
992 	}
993 
994 	if (alg == WPA_ALG_WEP) {
995 		return wpa_driver_ndis_add_wep(drv, pairwise, key_idx, set_tx,
996 					       key, key_len);
997 	}
998 
999 	len = 12 + 6 + 6 + 8 + key_len;
1000 
1001 	nkey = os_zalloc(len);
1002 	if (nkey == NULL)
1003 		return -1;
1004 
1005 	nkey->Length = len;
1006 	nkey->KeyIndex = key_idx;
1007 	if (set_tx)
1008 		nkey->KeyIndex |= 1 << 31;
1009 	if (pairwise)
1010 		nkey->KeyIndex |= 1 << 30;
1011 	if (seq && seq_len)
1012 		nkey->KeyIndex |= 1 << 29;
1013 	nkey->KeyLength = key_len;
1014 	os_memcpy(nkey->BSSID, bssid, ETH_ALEN);
1015 	if (seq && seq_len) {
1016 		for (i = 0; i < seq_len; i++)
1017 			nkey->KeyRSC |= (ULONGLONG) seq[i] << (i * 8);
1018 	}
1019 	if (alg == WPA_ALG_TKIP && key_len == 32) {
1020 		os_memcpy(nkey->KeyMaterial, key, 16);
1021 		os_memcpy(nkey->KeyMaterial + 16, key + 24, 8);
1022 		os_memcpy(nkey->KeyMaterial + 24, key + 16, 8);
1023 	} else {
1024 		os_memcpy(nkey->KeyMaterial, key, key_len);
1025 	}
1026 
1027 	wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_KEY",
1028 			(u8 *) nkey, len);
1029 	res = ndis_set_oid(drv, OID_802_11_ADD_KEY, (char *) nkey, len);
1030 	os_free(nkey);
1031 
1032 	return res;
1033 }
1034 
1035 
1036 static int
1037 wpa_driver_ndis_set_key_wrapper(void *priv,
1038 				struct wpa_driver_set_key_params *params)
1039 {
1040 	return wpa_driver_ndis_set_key(params->ifname, priv,
1041 				       params->alg, params->addr,
1042 				       params->key_idx, params->set_tx,
1043 				       params->seq, params->seq_len,
1044 				       params->key, params->key_len);
1045 }
1046 
1047 
1048 static int
1049 wpa_driver_ndis_associate(void *priv,
1050 			  struct wpa_driver_associate_params *params)
1051 {
1052 	struct wpa_driver_ndis_data *drv = priv;
1053 	u32 auth_mode, encr, priv_mode, mode;
1054 	u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1055 
1056 	drv->mode = params->mode;
1057 
1058 	/* Note: Setting OID_802_11_INFRASTRUCTURE_MODE clears current keys,
1059 	 * so static WEP keys needs to be set again after this. */
1060 	if (params->mode == IEEE80211_MODE_IBSS) {
1061 		mode = Ndis802_11IBSS;
1062 		/* Need to make sure that BSSID polling is enabled for
1063 		 * IBSS mode. */
1064 		eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
1065 		eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1066 				       drv, NULL);
1067 	} else
1068 		mode = Ndis802_11Infrastructure;
1069 	if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
1070 			 (char *) &mode, sizeof(mode)) < 0) {
1071 		wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
1072 			   "OID_802_11_INFRASTRUCTURE_MODE (%d)",
1073 			   (int) mode);
1074 		/* Try to continue anyway */
1075 	}
1076 
1077 	if (params->key_mgmt_suite == WPA_KEY_MGMT_NONE ||
1078 	    params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1079 		/* Re-set WEP keys if static WEP configuration is used. */
1080 		int i;
1081 		for (i = 0; i < 4; i++) {
1082 			if (!params->wep_key[i])
1083 				continue;
1084 			wpa_printf(MSG_DEBUG, "NDIS: Re-setting static WEP "
1085 				   "key %d", i);
1086 			wpa_driver_ndis_set_key(drv->ifname, drv, WPA_ALG_WEP,
1087 						bcast, i,
1088 						i == params->wep_tx_keyidx,
1089 						NULL, 0, params->wep_key[i],
1090 						params->wep_key_len[i]);
1091 		}
1092 	}
1093 
1094 	if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
1095 		if (params->auth_alg & WPA_AUTH_ALG_SHARED) {
1096 			if (params->auth_alg & WPA_AUTH_ALG_OPEN)
1097 				auth_mode = Ndis802_11AuthModeAutoSwitch;
1098 			else
1099 				auth_mode = Ndis802_11AuthModeShared;
1100 		} else
1101 			auth_mode = Ndis802_11AuthModeOpen;
1102 		priv_mode = Ndis802_11PrivFilterAcceptAll;
1103 	} else if (params->wpa_ie[0] == WLAN_EID_RSN) {
1104 		priv_mode = Ndis802_11PrivFilter8021xWEP;
1105 		if (params->key_mgmt_suite == WPA_KEY_MGMT_PSK)
1106 			auth_mode = Ndis802_11AuthModeWPA2PSK;
1107 		else
1108 			auth_mode = Ndis802_11AuthModeWPA2;
1109 #ifdef CONFIG_WPS
1110 	} else if (params->key_mgmt_suite == WPA_KEY_MGMT_WPS) {
1111 		auth_mode = Ndis802_11AuthModeOpen;
1112 		priv_mode = Ndis802_11PrivFilterAcceptAll;
1113 		if (params->wps == WPS_MODE_PRIVACY) {
1114 			u8 stub_key[5] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
1115 			/*
1116 			 * Some NDIS drivers refuse to associate in open mode
1117 			 * configuration due to Privacy field mismatch, so use
1118 			 * a workaround to make the configuration look like
1119 			 * matching one for WPS provisioning.
1120 			 */
1121 			wpa_printf(MSG_DEBUG, "NDIS: Set stub WEP key as a "
1122 				   "workaround to allow driver to associate "
1123 				   "for WPS");
1124 			wpa_driver_ndis_set_key(drv->ifname, drv, WPA_ALG_WEP,
1125 						bcast, 0, 1,
1126 						NULL, 0, stub_key,
1127 						sizeof(stub_key));
1128 		}
1129 #endif /* CONFIG_WPS */
1130 	} else {
1131 		priv_mode = Ndis802_11PrivFilter8021xWEP;
1132 		if (params->key_mgmt_suite == WPA_KEY_MGMT_WPA_NONE)
1133 			auth_mode = Ndis802_11AuthModeWPANone;
1134 		else if (params->key_mgmt_suite == WPA_KEY_MGMT_PSK)
1135 			auth_mode = Ndis802_11AuthModeWPAPSK;
1136 		else
1137 			auth_mode = Ndis802_11AuthModeWPA;
1138 	}
1139 
1140 	switch (params->pairwise_suite) {
1141 	case WPA_CIPHER_CCMP:
1142 		encr = Ndis802_11Encryption3Enabled;
1143 		break;
1144 	case WPA_CIPHER_TKIP:
1145 		encr = Ndis802_11Encryption2Enabled;
1146 		break;
1147 	case WPA_CIPHER_WEP40:
1148 	case WPA_CIPHER_WEP104:
1149 		encr = Ndis802_11Encryption1Enabled;
1150 		break;
1151 	case WPA_CIPHER_NONE:
1152 #ifdef CONFIG_WPS
1153 		if (params->wps == WPS_MODE_PRIVACY) {
1154 			encr = Ndis802_11Encryption1Enabled;
1155 			break;
1156 		}
1157 #endif /* CONFIG_WPS */
1158 		if (params->group_suite == WPA_CIPHER_CCMP)
1159 			encr = Ndis802_11Encryption3Enabled;
1160 		else if (params->group_suite == WPA_CIPHER_TKIP)
1161 			encr = Ndis802_11Encryption2Enabled;
1162 		else
1163 			encr = Ndis802_11EncryptionDisabled;
1164 		break;
1165 	default:
1166 #ifdef CONFIG_WPS
1167 		if (params->wps == WPS_MODE_PRIVACY) {
1168 			encr = Ndis802_11Encryption1Enabled;
1169 			break;
1170 		}
1171 #endif /* CONFIG_WPS */
1172 		encr = Ndis802_11EncryptionDisabled;
1173 		break;
1174 	};
1175 
1176 	if (ndis_set_oid(drv, OID_802_11_PRIVACY_FILTER,
1177 			 (char *) &priv_mode, sizeof(priv_mode)) < 0) {
1178 		wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
1179 			   "OID_802_11_PRIVACY_FILTER (%d)",
1180 			   (int) priv_mode);
1181 		/* Try to continue anyway */
1182 	}
1183 
1184 	ndis_set_auth_mode(drv, auth_mode);
1185 	ndis_set_encr_status(drv, encr);
1186 
1187 	if (params->bssid) {
1188 		ndis_set_oid(drv, OID_802_11_BSSID, (char *) params->bssid,
1189 			     ETH_ALEN);
1190 		drv->oid_bssid_set = 1;
1191 	} else if (drv->oid_bssid_set) {
1192 		ndis_set_oid(drv, OID_802_11_BSSID, "\xff\xff\xff\xff\xff\xff",
1193 			     ETH_ALEN);
1194 		drv->oid_bssid_set = 0;
1195 	}
1196 
1197 	return wpa_driver_ndis_set_ssid(drv, params->ssid, params->ssid_len);
1198 }
1199 
1200 
1201 static int wpa_driver_ndis_set_pmkid(struct wpa_driver_ndis_data *drv)
1202 {
1203 	int len, count, i, ret;
1204 	struct ndis_pmkid_entry *entry;
1205 	NDIS_802_11_PMKID *p;
1206 
1207 	count = 0;
1208 	entry = drv->pmkid;
1209 	while (entry) {
1210 		count++;
1211 		if (count >= drv->no_of_pmkid)
1212 			break;
1213 		entry = entry->next;
1214 	}
1215 	len = 8 + count * sizeof(BSSID_INFO);
1216 	p = os_zalloc(len);
1217 	if (p == NULL)
1218 		return -1;
1219 
1220 	p->Length = len;
1221 	p->BSSIDInfoCount = count;
1222 	entry = drv->pmkid;
1223 	for (i = 0; i < count; i++) {
1224 		os_memcpy(&p->BSSIDInfo[i].BSSID, entry->bssid, ETH_ALEN);
1225 		os_memcpy(&p->BSSIDInfo[i].PMKID, entry->pmkid, 16);
1226 		entry = entry->next;
1227 	}
1228 	wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID", (u8 *) p, len);
1229 	ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) p, len);
1230 	os_free(p);
1231 	return ret;
1232 }
1233 
1234 
1235 static int wpa_driver_ndis_add_pmkid(void *priv,
1236 				     struct wpa_pmkid_params *params)
1237 {
1238 	struct wpa_driver_ndis_data *drv = priv;
1239 	struct ndis_pmkid_entry *entry, *prev;
1240 	const u8 *bssid = params->bssid;
1241 	const u8 *pmkid = params->pmkid;
1242 
1243 	if (!bssid || !pmkid)
1244 		return -1;
1245 	if (drv->no_of_pmkid == 0)
1246 		return 0;
1247 
1248 	prev = NULL;
1249 	entry = drv->pmkid;
1250 	while (entry) {
1251 		if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0)
1252 			break;
1253 		prev = entry;
1254 		entry = entry->next;
1255 	}
1256 
1257 	if (entry) {
1258 		/* Replace existing entry for this BSSID and move it into the
1259 		 * beginning of the list. */
1260 		os_memcpy(entry->pmkid, pmkid, 16);
1261 		if (prev) {
1262 			prev->next = entry->next;
1263 			entry->next = drv->pmkid;
1264 			drv->pmkid = entry;
1265 		}
1266 	} else {
1267 		entry = os_malloc(sizeof(*entry));
1268 		if (entry) {
1269 			os_memcpy(entry->bssid, bssid, ETH_ALEN);
1270 			os_memcpy(entry->pmkid, pmkid, 16);
1271 			entry->next = drv->pmkid;
1272 			drv->pmkid = entry;
1273 		}
1274 	}
1275 
1276 	return wpa_driver_ndis_set_pmkid(drv);
1277 }
1278 
1279 
1280 static int wpa_driver_ndis_remove_pmkid(void *priv,
1281 					struct wpa_pmkid_params *params)
1282 {
1283 	struct wpa_driver_ndis_data *drv = priv;
1284 	struct ndis_pmkid_entry *entry, *prev;
1285 	const u8 *bssid = params->bssid;
1286 	const u8 *pmkid = params->pmkid;
1287 
1288 	if (!bssid || !pmkid)
1289 		return -1;
1290 	if (drv->no_of_pmkid == 0)
1291 		return 0;
1292 
1293 	entry = drv->pmkid;
1294 	prev = NULL;
1295 	while (entry) {
1296 		if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0 &&
1297 		    os_memcmp(entry->pmkid, pmkid, 16) == 0) {
1298 			if (prev)
1299 				prev->next = entry->next;
1300 			else
1301 				drv->pmkid = entry->next;
1302 			os_free(entry);
1303 			break;
1304 		}
1305 		prev = entry;
1306 		entry = entry->next;
1307 	}
1308 	return wpa_driver_ndis_set_pmkid(drv);
1309 }
1310 
1311 
1312 static int wpa_driver_ndis_flush_pmkid(void *priv)
1313 {
1314 	struct wpa_driver_ndis_data *drv = priv;
1315 	NDIS_802_11_PMKID p;
1316 	struct ndis_pmkid_entry *pmkid, *prev;
1317 	int prev_authmode, ret;
1318 
1319 	if (drv->no_of_pmkid == 0)
1320 		return 0;
1321 
1322 	pmkid = drv->pmkid;
1323 	drv->pmkid = NULL;
1324 	while (pmkid) {
1325 		prev = pmkid;
1326 		pmkid = pmkid->next;
1327 		os_free(prev);
1328 	}
1329 
1330 	/*
1331 	 * Some drivers may refuse OID_802_11_PMKID if authMode is not set to
1332 	 * WPA2, so change authMode temporarily, if needed.
1333 	 */
1334 	prev_authmode = ndis_get_auth_mode(drv);
1335 	if (prev_authmode != Ndis802_11AuthModeWPA2)
1336 		ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA2);
1337 
1338 	os_memset(&p, 0, sizeof(p));
1339 	p.Length = 8;
1340 	p.BSSIDInfoCount = 0;
1341 	wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID (flush)",
1342 		    (u8 *) &p, 8);
1343 	ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) &p, 8);
1344 
1345 	if (prev_authmode != Ndis802_11AuthModeWPA2)
1346 		ndis_set_auth_mode(drv, prev_authmode);
1347 
1348 	return ret;
1349 }
1350 
1351 
1352 static int wpa_driver_ndis_get_associnfo(struct wpa_driver_ndis_data *drv)
1353 {
1354 	char buf[512], *pos;
1355 	NDIS_802_11_ASSOCIATION_INFORMATION *ai;
1356 	int len;
1357 	union wpa_event_data data;
1358 	NDIS_802_11_BSSID_LIST_EX *b;
1359 	size_t blen, i;
1360 
1361 	len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION, buf,
1362 			   sizeof(buf));
1363 	if (len < 0) {
1364 		wpa_printf(MSG_DEBUG, "NDIS: failed to get association "
1365 			   "information");
1366 		return -1;
1367 	}
1368 	if (len > sizeof(buf)) {
1369 		/* Some drivers seem to be producing incorrect length for this
1370 		 * data. Limit the length to the current buffer size to avoid
1371 		 * crashing in hexdump. The data seems to be otherwise valid,
1372 		 * so better try to use it. */
1373 		wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association "
1374 			   "information length %d", len);
1375 		len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION,
1376 				   buf, sizeof(buf));
1377 		if (len < -1) {
1378 			wpa_printf(MSG_DEBUG, "NDIS: re-reading association "
1379 				   "information failed");
1380 			return -1;
1381 		}
1382 		if (len > sizeof(buf)) {
1383 			wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association"
1384 				   " information length %d (re-read)", len);
1385 			len = sizeof(buf);
1386 		}
1387 	}
1388 	wpa_hexdump(MSG_MSGDUMP, "NDIS: association information",
1389 		    (u8 *) buf, len);
1390 	if (len < sizeof(*ai)) {
1391 		wpa_printf(MSG_DEBUG, "NDIS: too short association "
1392 			   "information");
1393 		return -1;
1394 	}
1395 	ai = (NDIS_802_11_ASSOCIATION_INFORMATION *) buf;
1396 	wpa_printf(MSG_DEBUG, "NDIS: ReqFixed=0x%x RespFixed=0x%x off_req=%d "
1397 		   "off_resp=%d len_req=%d len_resp=%d",
1398 		   ai->AvailableRequestFixedIEs, ai->AvailableResponseFixedIEs,
1399 		   (int) ai->OffsetRequestIEs, (int) ai->OffsetResponseIEs,
1400 		   (int) ai->RequestIELength, (int) ai->ResponseIELength);
1401 
1402 	if (ai->OffsetRequestIEs + ai->RequestIELength > (unsigned) len ||
1403 	    ai->OffsetResponseIEs + ai->ResponseIELength > (unsigned) len) {
1404 		wpa_printf(MSG_DEBUG, "NDIS: association information - "
1405 			   "IE overflow");
1406 		return -1;
1407 	}
1408 
1409 	wpa_hexdump(MSG_MSGDUMP, "NDIS: Request IEs",
1410 		    (u8 *) buf + ai->OffsetRequestIEs, ai->RequestIELength);
1411 	wpa_hexdump(MSG_MSGDUMP, "NDIS: Response IEs",
1412 		    (u8 *) buf + ai->OffsetResponseIEs, ai->ResponseIELength);
1413 
1414 	os_memset(&data, 0, sizeof(data));
1415 	data.assoc_info.req_ies = (u8 *) buf + ai->OffsetRequestIEs;
1416 	data.assoc_info.req_ies_len = ai->RequestIELength;
1417 	data.assoc_info.resp_ies = (u8 *) buf + ai->OffsetResponseIEs;
1418 	data.assoc_info.resp_ies_len = ai->ResponseIELength;
1419 
1420 	blen = 65535;
1421 	b = os_zalloc(blen);
1422 	if (b == NULL)
1423 		goto skip_scan_results;
1424 	len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
1425 	if (len < 0) {
1426 		wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
1427 		os_free(b);
1428 		b = NULL;
1429 		goto skip_scan_results;
1430 	}
1431 	wpa_printf(MSG_DEBUG, "NDIS: %d BSSID items to process for AssocInfo",
1432 		   (unsigned int) b->NumberOfItems);
1433 
1434 	pos = (char *) &b->Bssid[0];
1435 	for (i = 0; i < b->NumberOfItems; i++) {
1436 		NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos;
1437 		if (os_memcmp(drv->bssid, bss->MacAddress, ETH_ALEN) == 0 &&
1438 		    bss->IELength > sizeof(NDIS_802_11_FIXED_IEs)) {
1439 			data.assoc_info.beacon_ies =
1440 				((u8 *) bss->IEs) +
1441 				sizeof(NDIS_802_11_FIXED_IEs);
1442 			data.assoc_info.beacon_ies_len =
1443 				bss->IELength - sizeof(NDIS_802_11_FIXED_IEs);
1444 			wpa_hexdump(MSG_MSGDUMP, "NDIS: Beacon IEs",
1445 				    data.assoc_info.beacon_ies,
1446 				    data.assoc_info.beacon_ies_len);
1447 			break;
1448 		}
1449 		pos += bss->Length;
1450 		if (pos > (char *) b + blen)
1451 			break;
1452 	}
1453 
1454 skip_scan_results:
1455 	wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
1456 
1457 	os_free(b);
1458 
1459 	return 0;
1460 }
1461 
1462 
1463 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx)
1464 {
1465 	struct wpa_driver_ndis_data *drv = eloop_ctx;
1466 	u8 bssid[ETH_ALEN];
1467 	int poll;
1468 
1469 	if (drv->wired)
1470 		return;
1471 
1472 	if (wpa_driver_ndis_get_bssid(drv, bssid)) {
1473 		/* Disconnected */
1474 		if (!is_zero_ether_addr(drv->bssid)) {
1475 			os_memset(drv->bssid, 0, ETH_ALEN);
1476 			wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1477 		}
1478 	} else {
1479 		/* Connected */
1480 		if (os_memcmp(drv->bssid, bssid, ETH_ALEN) != 0) {
1481 			os_memcpy(drv->bssid, bssid, ETH_ALEN);
1482 			wpa_driver_ndis_get_associnfo(drv);
1483 			wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1484 		}
1485 	}
1486 
1487 	/* When using integrated NDIS event receiver, we can skip BSSID
1488 	 * polling when using infrastructure network. However, when using
1489 	 * IBSS mode, many driver do not seem to generate connection event,
1490 	 * so we need to enable BSSID polling to figure out when IBSS network
1491 	 * has been formed.
1492 	 */
1493 	poll = drv->mode == IEEE80211_MODE_IBSS;
1494 #ifndef CONFIG_NDIS_EVENTS_INTEGRATED
1495 #ifndef _WIN32_WCE
1496 	poll = 1;
1497 #endif /* _WIN32_WCE */
1498 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
1499 
1500 	if (poll) {
1501 		eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1502 					drv, NULL);
1503 	}
1504 }
1505 
1506 
1507 static void wpa_driver_ndis_poll(void *priv)
1508 {
1509 	struct wpa_driver_ndis_data *drv = priv;
1510 	eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
1511 	wpa_driver_ndis_poll_timeout(drv, NULL);
1512 }
1513 
1514 
1515 /* Called when driver generates Media Connect Event by calling
1516  * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_CONNECT */
1517 void wpa_driver_ndis_event_connect(struct wpa_driver_ndis_data *drv)
1518 {
1519 	wpa_printf(MSG_DEBUG, "NDIS: Media Connect Event");
1520 	if (wpa_driver_ndis_get_bssid(drv, drv->bssid) == 0) {
1521 		wpa_driver_ndis_get_associnfo(drv);
1522 		wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1523 	}
1524 }
1525 
1526 
1527 /* Called when driver generates Media Disconnect Event by calling
1528  * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_DISCONNECT */
1529 void wpa_driver_ndis_event_disconnect(struct wpa_driver_ndis_data *drv)
1530 {
1531 	wpa_printf(MSG_DEBUG, "NDIS: Media Disconnect Event");
1532 	os_memset(drv->bssid, 0, ETH_ALEN);
1533 	wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1534 }
1535 
1536 
1537 static void wpa_driver_ndis_event_auth(struct wpa_driver_ndis_data *drv,
1538 				       const u8 *data, size_t data_len)
1539 {
1540 	NDIS_802_11_AUTHENTICATION_REQUEST *req;
1541 	int pairwise = 0, group = 0;
1542 	union wpa_event_data event;
1543 
1544 	if (data_len < sizeof(*req)) {
1545 		wpa_printf(MSG_DEBUG, "NDIS: Too short Authentication Request "
1546 			   "Event (len=%lu)", data_len);
1547 		return;
1548 	}
1549 	req = (NDIS_802_11_AUTHENTICATION_REQUEST *) data;
1550 
1551 	wpa_printf(MSG_DEBUG, "NDIS: Authentication Request Event: "
1552 		   "Bssid " MACSTR " Flags 0x%x",
1553 		   MAC2STR(req->Bssid), (int) req->Flags);
1554 
1555 	if ((req->Flags & NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR) ==
1556 	    NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR)
1557 		pairwise = 1;
1558 	else if ((req->Flags & NDIS_802_11_AUTH_REQUEST_GROUP_ERROR) ==
1559 	    NDIS_802_11_AUTH_REQUEST_GROUP_ERROR)
1560 		group = 1;
1561 
1562 	if (pairwise || group) {
1563 		os_memset(&event, 0, sizeof(event));
1564 		event.michael_mic_failure.unicast = pairwise;
1565 		wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE,
1566 				     &event);
1567 	}
1568 }
1569 
1570 
1571 static void wpa_driver_ndis_event_pmkid(struct wpa_driver_ndis_data *drv,
1572 					const u8 *data, size_t data_len)
1573 {
1574 	NDIS_802_11_PMKID_CANDIDATE_LIST *pmkid;
1575 	size_t i;
1576 	union wpa_event_data event;
1577 
1578 	if (data_len < 8) {
1579 		wpa_printf(MSG_DEBUG, "NDIS: Too short PMKID Candidate List "
1580 			   "Event (len=%lu)", data_len);
1581 		return;
1582 	}
1583 	pmkid = (NDIS_802_11_PMKID_CANDIDATE_LIST *) data;
1584 	wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List Event - Version %d "
1585 		   "NumCandidates %d",
1586 		   (int) pmkid->Version, (int) pmkid->NumCandidates);
1587 
1588 	if (pmkid->Version != 1) {
1589 		wpa_printf(MSG_DEBUG, "NDIS: Unsupported PMKID Candidate List "
1590 			   "Version %d", (int) pmkid->Version);
1591 		return;
1592 	}
1593 
1594 	if (data_len < 8 + pmkid->NumCandidates * sizeof(PMKID_CANDIDATE)) {
1595 		wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List underflow");
1596 		return;
1597 	}
1598 
1599 	os_memset(&event, 0, sizeof(event));
1600 	for (i = 0; i < pmkid->NumCandidates; i++) {
1601 		PMKID_CANDIDATE *p = &pmkid->CandidateList[i];
1602 		wpa_printf(MSG_DEBUG, "NDIS: %lu: " MACSTR " Flags 0x%x",
1603 			   i, MAC2STR(p->BSSID), (int) p->Flags);
1604 		os_memcpy(event.pmkid_candidate.bssid, p->BSSID, ETH_ALEN);
1605 		event.pmkid_candidate.index = i;
1606 		event.pmkid_candidate.preauth =
1607 			p->Flags & NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
1608 		wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE,
1609 				     &event);
1610 	}
1611 }
1612 
1613 
1614 /* Called when driver calls NdisMIndicateStatus() with
1615  * NDIS_STATUS_MEDIA_SPECIFIC_INDICATION */
1616 void wpa_driver_ndis_event_media_specific(struct wpa_driver_ndis_data *drv,
1617 					  const u8 *data, size_t data_len)
1618 {
1619 	NDIS_802_11_STATUS_INDICATION *status;
1620 
1621 	if (data == NULL || data_len < sizeof(*status))
1622 		return;
1623 
1624 	wpa_hexdump(MSG_DEBUG, "NDIS: Media Specific Indication",
1625 		    data, data_len);
1626 
1627 	status = (NDIS_802_11_STATUS_INDICATION *) data;
1628 	data += sizeof(status);
1629 	data_len -= sizeof(status);
1630 
1631 	switch (status->StatusType) {
1632 	case Ndis802_11StatusType_Authentication:
1633 		wpa_driver_ndis_event_auth(drv, data, data_len);
1634 		break;
1635 	case Ndis802_11StatusType_PMKID_CandidateList:
1636 		wpa_driver_ndis_event_pmkid(drv, data, data_len);
1637 		break;
1638 	default:
1639 		wpa_printf(MSG_DEBUG, "NDIS: Unknown StatusType %d",
1640 			   (int) status->StatusType);
1641 		break;
1642 	}
1643 }
1644 
1645 
1646 /* Called when an adapter is added */
1647 void wpa_driver_ndis_event_adapter_arrival(struct wpa_driver_ndis_data *drv)
1648 {
1649 	union wpa_event_data event;
1650 	int i;
1651 
1652 	wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Arrival");
1653 
1654 	for (i = 0; i < 30; i++) {
1655 		/* Re-open Packet32/NDISUIO connection */
1656 		wpa_driver_ndis_adapter_close(drv);
1657 		if (wpa_driver_ndis_adapter_init(drv) < 0 ||
1658 		    wpa_driver_ndis_adapter_open(drv) < 0) {
1659 			wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialization "
1660 				   "(%d) failed", i);
1661 			os_sleep(1, 0);
1662 		} else {
1663 			wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialized");
1664 			break;
1665 		}
1666 	}
1667 
1668 	os_memset(&event, 0, sizeof(event));
1669 	os_strlcpy(event.interface_status.ifname, drv->ifname,
1670 		   sizeof(event.interface_status.ifname));
1671 	event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1672 	wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1673 }
1674 
1675 
1676 /* Called when an adapter is removed */
1677 void wpa_driver_ndis_event_adapter_removal(struct wpa_driver_ndis_data *drv)
1678 {
1679 	union wpa_event_data event;
1680 
1681 	wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Removal");
1682 	os_memset(&event, 0, sizeof(event));
1683 	os_strlcpy(event.interface_status.ifname, drv->ifname,
1684 		   sizeof(event.interface_status.ifname));
1685 	event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
1686 	wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1687 }
1688 
1689 
1690 static void
1691 wpa_driver_ndis_get_wpa_capability(struct wpa_driver_ndis_data *drv)
1692 {
1693 	wpa_printf(MSG_DEBUG, "NDIS: verifying driver WPA capability");
1694 
1695 	if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA) == 0 &&
1696 	    ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPA) {
1697 		wpa_printf(MSG_DEBUG, "NDIS: WPA key management supported");
1698 		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1699 	}
1700 
1701 	if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPAPSK) == 0 &&
1702 	    ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPAPSK) {
1703 		wpa_printf(MSG_DEBUG, "NDIS: WPA-PSK key management "
1704 			   "supported");
1705 		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1706 	}
1707 
1708 	if (ndis_set_encr_status(drv, Ndis802_11Encryption3Enabled) == 0 &&
1709 	    ndis_get_encr_status(drv) == Ndis802_11Encryption3KeyAbsent) {
1710 		wpa_printf(MSG_DEBUG, "NDIS: CCMP encryption supported");
1711 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1712 	}
1713 
1714 	if (ndis_set_encr_status(drv, Ndis802_11Encryption2Enabled) == 0 &&
1715 	    ndis_get_encr_status(drv) == Ndis802_11Encryption2KeyAbsent) {
1716 		wpa_printf(MSG_DEBUG, "NDIS: TKIP encryption supported");
1717 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1718 	}
1719 
1720 	if (ndis_set_encr_status(drv, Ndis802_11Encryption1Enabled) == 0 &&
1721 	    ndis_get_encr_status(drv) == Ndis802_11Encryption1KeyAbsent) {
1722 		wpa_printf(MSG_DEBUG, "NDIS: WEP encryption supported");
1723 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1724 			WPA_DRIVER_CAPA_ENC_WEP104;
1725 	}
1726 
1727 	if (ndis_set_auth_mode(drv, Ndis802_11AuthModeShared) == 0 &&
1728 	    ndis_get_auth_mode(drv) == Ndis802_11AuthModeShared) {
1729 		drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1730 	}
1731 
1732 	if (ndis_set_auth_mode(drv, Ndis802_11AuthModeOpen) == 0 &&
1733 	    ndis_get_auth_mode(drv) == Ndis802_11AuthModeOpen) {
1734 		drv->capa.auth |= WPA_DRIVER_AUTH_OPEN;
1735 	}
1736 
1737 	ndis_set_encr_status(drv, Ndis802_11EncryptionDisabled);
1738 
1739 	/* Could also verify OID_802_11_ADD_KEY error reporting and
1740 	 * support for OID_802_11_ASSOCIATION_INFORMATION. */
1741 
1742 	if (drv->capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA &&
1743 	    drv->capa.enc & (WPA_DRIVER_CAPA_ENC_TKIP |
1744 			     WPA_DRIVER_CAPA_ENC_CCMP)) {
1745 		wpa_printf(MSG_DEBUG, "NDIS: driver supports WPA");
1746 		drv->has_capability = 1;
1747 	} else {
1748 		wpa_printf(MSG_DEBUG, "NDIS: no WPA support found");
1749 	}
1750 
1751 	wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1752 		   "enc 0x%x auth 0x%x",
1753 		   drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1754 }
1755 
1756 
1757 static void wpa_driver_ndis_get_capability(struct wpa_driver_ndis_data *drv)
1758 {
1759 	char buf[512];
1760 	int len;
1761 	size_t i;
1762 	NDIS_802_11_CAPABILITY *c;
1763 
1764 	drv->capa.flags = WPA_DRIVER_FLAGS_DRIVER_IE;
1765 
1766 	len = ndis_get_oid(drv, OID_802_11_CAPABILITY, buf, sizeof(buf));
1767 	if (len < 0) {
1768 		wpa_driver_ndis_get_wpa_capability(drv);
1769 		return;
1770 	}
1771 
1772 	wpa_hexdump(MSG_MSGDUMP, "OID_802_11_CAPABILITY", (u8 *) buf, len);
1773 	c = (NDIS_802_11_CAPABILITY *) buf;
1774 	if (len < sizeof(*c) || c->Version != 2) {
1775 		wpa_printf(MSG_DEBUG, "NDIS: unsupported "
1776 			   "OID_802_11_CAPABILITY data");
1777 		return;
1778 	}
1779 	wpa_printf(MSG_DEBUG, "NDIS: Driver supports OID_802_11_CAPABILITY - "
1780 		   "NoOfPMKIDs %d NoOfAuthEncrPairs %d",
1781 		   (int) c->NoOfPMKIDs,
1782 		   (int) c->NoOfAuthEncryptPairsSupported);
1783 	drv->has_capability = 1;
1784 	drv->no_of_pmkid = c->NoOfPMKIDs;
1785 	for (i = 0; i < c->NoOfAuthEncryptPairsSupported; i++) {
1786 		NDIS_802_11_AUTHENTICATION_ENCRYPTION *ae;
1787 		ae = &c->AuthenticationEncryptionSupported[i];
1788 		if ((char *) (ae + 1) > buf + len) {
1789 			wpa_printf(MSG_DEBUG, "NDIS: auth/encr pair list "
1790 				   "overflow");
1791 			break;
1792 		}
1793 		wpa_printf(MSG_MSGDUMP, "NDIS: %lu - auth %d encr %d",
1794 			   i, (int) ae->AuthModeSupported,
1795 			   (int) ae->EncryptStatusSupported);
1796 		switch (ae->AuthModeSupported) {
1797 		case Ndis802_11AuthModeOpen:
1798 			drv->capa.auth |= WPA_DRIVER_AUTH_OPEN;
1799 			break;
1800 		case Ndis802_11AuthModeShared:
1801 			drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1802 			break;
1803 		case Ndis802_11AuthModeWPA:
1804 			drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1805 			break;
1806 		case Ndis802_11AuthModeWPAPSK:
1807 			drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1808 			break;
1809 		case Ndis802_11AuthModeWPA2:
1810 			drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2;
1811 			break;
1812 		case Ndis802_11AuthModeWPA2PSK:
1813 			drv->capa.key_mgmt |=
1814 				WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1815 			break;
1816 		case Ndis802_11AuthModeWPANone:
1817 			drv->capa.key_mgmt |=
1818 				WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE;
1819 			break;
1820 		default:
1821 			break;
1822 		}
1823 		switch (ae->EncryptStatusSupported) {
1824 		case Ndis802_11Encryption1Enabled:
1825 			drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40;
1826 			drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP104;
1827 			break;
1828 		case Ndis802_11Encryption2Enabled:
1829 			drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1830 			break;
1831 		case Ndis802_11Encryption3Enabled:
1832 			drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1833 			break;
1834 		default:
1835 			break;
1836 		}
1837 	}
1838 
1839 	wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1840 		   "enc 0x%x auth 0x%x",
1841 		   drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1842 }
1843 
1844 
1845 static int wpa_driver_ndis_get_capa(void *priv, struct wpa_driver_capa *capa)
1846 {
1847 	struct wpa_driver_ndis_data *drv = priv;
1848 	if (!drv->has_capability)
1849 		return -1;
1850 	os_memcpy(capa, &drv->capa, sizeof(*capa));
1851 	return 0;
1852 }
1853 
1854 
1855 static const char * wpa_driver_ndis_get_ifname(void *priv)
1856 {
1857 	struct wpa_driver_ndis_data *drv = priv;
1858 	return drv->ifname;
1859 }
1860 
1861 
1862 static const u8 * wpa_driver_ndis_get_mac_addr(void *priv)
1863 {
1864 	struct wpa_driver_ndis_data *drv = priv;
1865 	return drv->own_addr;
1866 }
1867 
1868 
1869 #ifdef _WIN32_WCE
1870 
1871 #define NDISUIO_MSG_SIZE (sizeof(NDISUIO_DEVICE_NOTIFICATION) + 512)
1872 
1873 static void ndisuio_notification_receive(void *eloop_data, void *user_ctx)
1874 {
1875 	struct wpa_driver_ndis_data *drv = eloop_data;
1876 	NDISUIO_DEVICE_NOTIFICATION *hdr;
1877 	u8 buf[NDISUIO_MSG_SIZE];
1878 	DWORD len, flags;
1879 
1880 	if (!ReadMsgQueue(drv->event_queue, buf, NDISUIO_MSG_SIZE, &len, 0,
1881 			  &flags)) {
1882 		wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1883 			   "ReadMsgQueue failed: %d", (int) GetLastError());
1884 		return;
1885 	}
1886 
1887 	if (len < sizeof(NDISUIO_DEVICE_NOTIFICATION)) {
1888 		wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1889 			   "Too short message (len=%d)", (int) len);
1890 		return;
1891 	}
1892 
1893 	hdr = (NDISUIO_DEVICE_NOTIFICATION *) buf;
1894 	wpa_printf(MSG_DEBUG, "NDIS: Notification received: len=%d type=0x%x",
1895 		   (int) len, hdr->dwNotificationType);
1896 
1897 	switch (hdr->dwNotificationType) {
1898 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL
1899 	case NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL:
1900 		wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_ARRIVAL");
1901 		wpa_driver_ndis_event_adapter_arrival(drv);
1902 		break;
1903 #endif
1904 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
1905 	case NDISUIO_NOTIFICATION_ADAPTER_REMOVAL:
1906 		wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_REMOVAL");
1907 		wpa_driver_ndis_event_adapter_removal(drv);
1908 		break;
1909 #endif
1910 	case NDISUIO_NOTIFICATION_MEDIA_CONNECT:
1911 		wpa_printf(MSG_DEBUG, "NDIS: MEDIA_CONNECT");
1912 		SetEvent(drv->connected_event);
1913 		wpa_driver_ndis_event_connect(drv);
1914 		break;
1915 	case NDISUIO_NOTIFICATION_MEDIA_DISCONNECT:
1916 		ResetEvent(drv->connected_event);
1917 		wpa_printf(MSG_DEBUG, "NDIS: MEDIA_DISCONNECT");
1918 		wpa_driver_ndis_event_disconnect(drv);
1919 		break;
1920 	case NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION:
1921 		wpa_printf(MSG_DEBUG, "NDIS: MEDIA_SPECIFIC_NOTIFICATION");
1922 #if _WIN32_WCE == 420 || _WIN32_WCE == 0x420
1923 		wpa_driver_ndis_event_media_specific(
1924 			drv, hdr->pvStatusBuffer, hdr->uiStatusBufferSize);
1925 #else
1926 		wpa_driver_ndis_event_media_specific(
1927 			drv, ((const u8 *) hdr) + hdr->uiOffsetToStatusBuffer,
1928 			(size_t) hdr->uiStatusBufferSize);
1929 #endif
1930 		break;
1931 	default:
1932 		wpa_printf(MSG_DEBUG, "NDIS: Unknown notification type 0x%x",
1933 			   hdr->dwNotificationType);
1934 		break;
1935 	}
1936 }
1937 
1938 
1939 static void ndisuio_notification_deinit(struct wpa_driver_ndis_data *drv)
1940 {
1941 	NDISUIO_REQUEST_NOTIFICATION req;
1942 
1943 	memset(&req, 0, sizeof(req));
1944 	req.hMsgQueue = drv->event_queue;
1945 	req.dwNotificationTypes = 0;
1946 
1947 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION,
1948 			     &req, sizeof(req), NULL, 0, NULL, NULL)) {
1949 		wpa_printf(MSG_INFO, "ndisuio_notification_deinit: "
1950 			   "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d",
1951 			   (int) GetLastError());
1952 	}
1953 
1954 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_CANCEL_NOTIFICATION,
1955 			     NULL, 0, NULL, 0, NULL, NULL)) {
1956 		wpa_printf(MSG_INFO, "ndisuio_notification_deinit: "
1957 			   "IOCTL_NDISUIO_CANCEL_NOTIFICATION failed: %d",
1958 			   (int) GetLastError());
1959 	}
1960 
1961 	if (drv->event_queue) {
1962 		eloop_unregister_event(drv->event_queue,
1963 				       sizeof(drv->event_queue));
1964 		CloseHandle(drv->event_queue);
1965 		drv->event_queue = NULL;
1966 	}
1967 
1968 	if (drv->connected_event) {
1969 		CloseHandle(drv->connected_event);
1970 		drv->connected_event = NULL;
1971 	}
1972 }
1973 
1974 
1975 static int ndisuio_notification_init(struct wpa_driver_ndis_data *drv)
1976 {
1977 	MSGQUEUEOPTIONS opt;
1978 	NDISUIO_REQUEST_NOTIFICATION req;
1979 
1980 	drv->connected_event =
1981 		CreateEvent(NULL, TRUE, FALSE, TEXT("WpaSupplicantConnected"));
1982 	if (drv->connected_event == NULL) {
1983 		wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1984 			   "CreateEvent failed: %d",
1985 			   (int) GetLastError());
1986 		return -1;
1987 	}
1988 
1989 	memset(&opt, 0, sizeof(opt));
1990 	opt.dwSize = sizeof(opt);
1991 	opt.dwMaxMessages = 5;
1992 	opt.cbMaxMessage = NDISUIO_MSG_SIZE;
1993 	opt.bReadAccess = TRUE;
1994 
1995 	drv->event_queue = CreateMsgQueue(NULL, &opt);
1996 	if (drv->event_queue == NULL) {
1997 		wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1998 			   "CreateMsgQueue failed: %d",
1999 			   (int) GetLastError());
2000 		ndisuio_notification_deinit(drv);
2001 		return -1;
2002 	}
2003 
2004 	memset(&req, 0, sizeof(req));
2005 	req.hMsgQueue = drv->event_queue;
2006 	req.dwNotificationTypes =
2007 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL
2008 		NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL |
2009 #endif
2010 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
2011 		NDISUIO_NOTIFICATION_ADAPTER_REMOVAL |
2012 #endif
2013 		NDISUIO_NOTIFICATION_MEDIA_CONNECT |
2014 		NDISUIO_NOTIFICATION_MEDIA_DISCONNECT |
2015 		NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION;
2016 
2017 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION,
2018 			     &req, sizeof(req), NULL, 0, NULL, NULL)) {
2019 		wpa_printf(MSG_INFO, "ndisuio_notification_init: "
2020 			   "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d",
2021 			   (int) GetLastError());
2022 		ndisuio_notification_deinit(drv);
2023 		return -1;
2024 	}
2025 
2026 	eloop_register_event(drv->event_queue, sizeof(drv->event_queue),
2027 			     ndisuio_notification_receive, drv, NULL);
2028 
2029 	return 0;
2030 }
2031 #endif /* _WIN32_WCE */
2032 
2033 
2034 static int wpa_driver_ndis_get_names(struct wpa_driver_ndis_data *drv)
2035 {
2036 #ifdef CONFIG_USE_NDISUIO
2037 	NDISUIO_QUERY_BINDING *b;
2038 	size_t blen = sizeof(*b) + 1024;
2039 	int i, error, found = 0;
2040 	DWORD written;
2041 	char name[256], desc[256], *dpos;
2042 	WCHAR *pos;
2043 	size_t j, len, dlen;
2044 
2045 	b = os_malloc(blen);
2046 	if (b == NULL)
2047 		return -1;
2048 
2049 	for (i = 0; ; i++) {
2050 		os_memset(b, 0, blen);
2051 		b->BindingIndex = i;
2052 		if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
2053 				     b, sizeof(NDISUIO_QUERY_BINDING), b, blen,
2054 				     &written, NULL)) {
2055 			error = (int) GetLastError();
2056 			if (error == ERROR_NO_MORE_ITEMS)
2057 				break;
2058 			wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
2059 				   "failed: %d", error);
2060 			break;
2061 		}
2062 
2063 		pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
2064 		len = b->DeviceNameLength;
2065 		if (len >= sizeof(name))
2066 			len = sizeof(name) - 1;
2067 		for (j = 0; j < len; j++)
2068 			name[j] = (char) pos[j];
2069 		name[len] = '\0';
2070 
2071 		pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
2072 		len = b->DeviceDescrLength;
2073 		if (len >= sizeof(desc))
2074 			len = sizeof(desc) - 1;
2075 		for (j = 0; j < len; j++)
2076 			desc[j] = (char) pos[j];
2077 		desc[len] = '\0';
2078 
2079 		wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
2080 
2081 		if (os_strstr(name, drv->ifname)) {
2082 			wpa_printf(MSG_DEBUG, "NDIS: Interface name match");
2083 			found = 1;
2084 			break;
2085 		}
2086 
2087 		if (os_strncmp(desc, drv->ifname, os_strlen(drv->ifname)) == 0)
2088 		{
2089 			wpa_printf(MSG_DEBUG, "NDIS: Interface description "
2090 				   "match");
2091 			found = 1;
2092 			break;
2093 		}
2094 	}
2095 
2096 	if (!found) {
2097 		wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
2098 			   drv->ifname);
2099 		os_free(b);
2100 		return -1;
2101 	}
2102 
2103 	os_strlcpy(drv->ifname,
2104 		   os_strncmp(name, "\\DEVICE\\", 8) == 0 ? name + 8 : name,
2105 		   sizeof(drv->ifname));
2106 #ifdef _WIN32_WCE
2107 	drv->adapter_name = wpa_strdup_tchar(drv->ifname);
2108 	if (drv->adapter_name == NULL) {
2109 		wpa_printf(MSG_ERROR, "NDIS: Failed to allocate memory for "
2110 			   "adapter name");
2111 		os_free(b);
2112 		return -1;
2113 	}
2114 #endif /* _WIN32_WCE */
2115 
2116 	dpos = os_strstr(desc, " - ");
2117 	if (dpos)
2118 		dlen = dpos - desc;
2119 	else
2120 		dlen = os_strlen(desc);
2121 	drv->adapter_desc = os_malloc(dlen + 1);
2122 	if (drv->adapter_desc) {
2123 		os_memcpy(drv->adapter_desc, desc, dlen);
2124 		drv->adapter_desc[dlen] = '\0';
2125 	}
2126 	os_free(b);
2127 	if (drv->adapter_desc == NULL)
2128 		return -1;
2129 
2130 	wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2131 		   drv->adapter_desc);
2132 
2133 	return 0;
2134 #else /* CONFIG_USE_NDISUIO */
2135 	PTSTR _names;
2136 	char *names, *pos, *pos2;
2137 	ULONG len;
2138 	BOOLEAN res;
2139 #define MAX_ADAPTERS 32
2140 	char *name[MAX_ADAPTERS];
2141 	char *desc[MAX_ADAPTERS];
2142 	int num_name, num_desc, i, found_name, found_desc;
2143 	size_t dlen;
2144 
2145 	wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
2146 		   PacketGetVersion());
2147 
2148 	len = 8192;
2149 	_names = os_zalloc(len);
2150 	if (_names == NULL)
2151 		return -1;
2152 
2153 	res = PacketGetAdapterNames(_names, &len);
2154 	if (!res && len > 8192) {
2155 		os_free(_names);
2156 		_names = os_zalloc(len);
2157 		if (_names == NULL)
2158 			return -1;
2159 		res = PacketGetAdapterNames(_names, &len);
2160 	}
2161 
2162 	if (!res) {
2163 		wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
2164 			   "(PacketGetAdapterNames)");
2165 		os_free(_names);
2166 		return -1;
2167 	}
2168 
2169 	names = (char *) _names;
2170 	if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') {
2171 		wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in "
2172 			   "UNICODE");
2173 		/* Convert to ASCII */
2174 		pos2 = pos = names;
2175 		while (pos2 < names + len) {
2176 			if (pos2[0] == '\0' && pos2[1] == '\0' &&
2177 			    pos2[2] == '\0' && pos2[3] == '\0') {
2178 				pos2 += 4;
2179 				break;
2180 			}
2181 			*pos++ = pos2[0];
2182 			pos2 += 2;
2183 		}
2184 		os_memcpy(pos + 2, names, pos - names);
2185 		pos += 2;
2186 	} else
2187 		pos = names;
2188 
2189 	num_name = 0;
2190 	while (pos < names + len) {
2191 		name[num_name] = pos;
2192 		while (*pos && pos < names + len)
2193 			pos++;
2194 		if (pos + 1 >= names + len) {
2195 			os_free(names);
2196 			return -1;
2197 		}
2198 		pos++;
2199 		num_name++;
2200 		if (num_name >= MAX_ADAPTERS) {
2201 			wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
2202 			os_free(names);
2203 			return -1;
2204 		}
2205 		if (*pos == '\0') {
2206 			wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
2207 				   num_name);
2208 			pos++;
2209 			break;
2210 		}
2211 	}
2212 
2213 	num_desc = 0;
2214 	while (pos < names + len) {
2215 		desc[num_desc] = pos;
2216 		while (*pos && pos < names + len)
2217 			pos++;
2218 		if (pos + 1 >= names + len) {
2219 			os_free(names);
2220 			return -1;
2221 		}
2222 		pos++;
2223 		num_desc++;
2224 		if (num_desc >= MAX_ADAPTERS) {
2225 			wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
2226 				   "descriptions");
2227 			os_free(names);
2228 			return -1;
2229 		}
2230 		if (*pos == '\0') {
2231 			wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
2232 				   "found", num_name);
2233 			pos++;
2234 			break;
2235 		}
2236 	}
2237 
2238 	/*
2239 	 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
2240 	 * descriptions. Fill in stub descriptors to work around this.
2241 	 */
2242 	while (num_desc < num_name)
2243 		desc[num_desc++] = "stub description";
2244 
2245 	if (num_name != num_desc) {
2246 		wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and "
2247 			   "description counts (%d != %d)",
2248 			   num_name, num_desc);
2249 		os_free(names);
2250 		return -1;
2251 	}
2252 
2253 	found_name = found_desc = -1;
2254 	for (i = 0; i < num_name; i++) {
2255 		wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s",
2256 			   i, name[i], desc[i]);
2257 		if (found_name == -1 && os_strstr(name[i], drv->ifname))
2258 			found_name = i;
2259 		if (found_desc == -1 &&
2260 		    os_strncmp(desc[i], drv->ifname, os_strlen(drv->ifname)) ==
2261 		    0)
2262 			found_desc = i;
2263 	}
2264 
2265 	if (found_name < 0 && found_desc >= 0) {
2266 		wpa_printf(MSG_DEBUG, "NDIS: Matched interface '%s' based on "
2267 			   "description '%s'",
2268 			   name[found_desc], desc[found_desc]);
2269 		found_name = found_desc;
2270 		os_strlcpy(drv->ifname,
2271 			   os_strncmp(name[found_desc], "\\Device\\NPF_", 12)
2272 			   == 0 ? name[found_desc] + 12 : name[found_desc],
2273 			   sizeof(drv->ifname));
2274 	}
2275 
2276 	if (found_name < 0) {
2277 		wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
2278 			   drv->ifname);
2279 		os_free(names);
2280 		return -1;
2281 	}
2282 
2283 	i = found_name;
2284 	pos = os_strrchr(desc[i], '(');
2285 	if (pos) {
2286 		dlen = pos - desc[i];
2287 		pos--;
2288 		if (pos > desc[i] && *pos == ' ')
2289 			dlen--;
2290 	} else {
2291 		dlen = os_strlen(desc[i]);
2292 	}
2293 	drv->adapter_desc = os_malloc(dlen + 1);
2294 	if (drv->adapter_desc) {
2295 		os_memcpy(drv->adapter_desc, desc[i], dlen);
2296 		drv->adapter_desc[dlen] = '\0';
2297 	}
2298 	os_free(names);
2299 	if (drv->adapter_desc == NULL)
2300 		return -1;
2301 
2302 	wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2303 		   drv->adapter_desc);
2304 
2305 	return 0;
2306 #endif /* CONFIG_USE_NDISUIO */
2307 }
2308 
2309 
2310 #if defined(CONFIG_NATIVE_WINDOWS) || defined(__CYGWIN__)
2311 #ifndef _WIN32_WCE
2312 /*
2313  * These structures are undocumented for WinXP; only WinCE version is
2314  * documented. These would be included wzcsapi.h if it were available. Some
2315  * changes here have been needed to make the structures match with WinXP SP2.
2316  * It is unclear whether these work with any other version.
2317  */
2318 
2319 typedef struct {
2320 	LPWSTR wszGuid;
2321 } INTF_KEY_ENTRY, *PINTF_KEY_ENTRY;
2322 
2323 typedef struct {
2324 	DWORD dwNumIntfs;
2325 	PINTF_KEY_ENTRY pIntfs;
2326 } INTFS_KEY_TABLE, *PINTFS_KEY_TABLE;
2327 
2328 typedef struct {
2329 	DWORD dwDataLen;
2330 	LPBYTE pData;
2331 } RAW_DATA, *PRAW_DATA;
2332 
2333 typedef struct {
2334 	LPWSTR wszGuid;
2335 	LPWSTR wszDescr;
2336 	ULONG ulMediaState;
2337 	ULONG ulMediaType;
2338 	ULONG ulPhysicalMediaType;
2339 	INT nInfraMode;
2340 	INT nAuthMode;
2341 	INT nWepStatus;
2342 #ifndef _WIN32_WCE
2343 	u8 pad[2]; /* why is this needed? */
2344 #endif /* _WIN32_WCE */
2345 	DWORD dwCtlFlags;
2346 	DWORD dwCapabilities; /* something added for WinXP SP2(?) */
2347 	RAW_DATA rdSSID;
2348 	RAW_DATA rdBSSID;
2349 	RAW_DATA rdBSSIDList;
2350 	RAW_DATA rdStSSIDList;
2351 	RAW_DATA rdCtrlData;
2352 #ifdef UNDER_CE
2353 	BOOL bInitialized;
2354 #endif
2355 	DWORD nWPAMCastCipher;
2356 	/* add some extra buffer for later additions since this interface is
2357 	 * far from stable */
2358 	u8 later_additions[100];
2359 } INTF_ENTRY, *PINTF_ENTRY;
2360 
2361 #define INTF_ALL 0xffffffff
2362 #define INTF_ALL_FLAGS 0x0000ffff
2363 #define INTF_CTLFLAGS 0x00000010
2364 #define INTFCTL_ENABLED 0x8000
2365 #endif /* _WIN32_WCE */
2366 
2367 
2368 #ifdef _WIN32_WCE
2369 static int wpa_driver_ndis_rebind_adapter(struct wpa_driver_ndis_data *drv)
2370 {
2371 	HANDLE ndis;
2372 	TCHAR multi[100];
2373 	int len;
2374 
2375 	len = _tcslen(drv->adapter_name);
2376 	if (len > 80)
2377 		return -1;
2378 
2379 	ndis = CreateFile(DD_NDIS_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE,
2380 			  0, NULL, OPEN_EXISTING, 0, NULL);
2381 	if (ndis == INVALID_HANDLE_VALUE) {
2382 		wpa_printf(MSG_DEBUG, "NDIS: Failed to open file to NDIS "
2383 			   "device: %d", (int) GetLastError());
2384 		return -1;
2385 	}
2386 
2387 	len++;
2388 	memcpy(multi, drv->adapter_name, len * sizeof(TCHAR));
2389 	memcpy(&multi[len], TEXT("NDISUIO\0"), 9 * sizeof(TCHAR));
2390 	len += 9;
2391 
2392 	if (!DeviceIoControl(ndis, IOCTL_NDIS_REBIND_ADAPTER,
2393 			     multi, len * sizeof(TCHAR), NULL, 0, NULL, NULL))
2394 	{
2395 		wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDIS_REBIND_ADAPTER "
2396 			   "failed: 0x%x", (int) GetLastError());
2397 		wpa_hexdump_ascii(MSG_DEBUG, "NDIS: rebind multi_sz",
2398 				  (u8 *) multi, len * sizeof(TCHAR));
2399 		CloseHandle(ndis);
2400 		return -1;
2401 	}
2402 
2403 	CloseHandle(ndis);
2404 
2405 	wpa_printf(MSG_DEBUG, "NDIS: Requested NDIS rebind of NDISUIO "
2406 		   "protocol");
2407 
2408 	return 0;
2409 }
2410 #endif /* _WIN32_WCE */
2411 
2412 
2413 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2414 				   int enable)
2415 {
2416 #ifdef _WIN32_WCE
2417 	HKEY hk, hk2;
2418 	LONG ret;
2419 	DWORD i, hnd, len;
2420 	TCHAR keyname[256], devname[256];
2421 
2422 #define WZC_DRIVER TEXT("Drivers\\BuiltIn\\ZeroConfig")
2423 
2424 	if (enable) {
2425 		HANDLE h;
2426 		h = ActivateDeviceEx(WZC_DRIVER, NULL, 0, NULL);
2427 		if (h == INVALID_HANDLE_VALUE || h == 0) {
2428 			wpa_printf(MSG_DEBUG, "NDIS: Failed to re-enable WZC "
2429 				   "- ActivateDeviceEx failed: %d",
2430 				   (int) GetLastError());
2431 			return -1;
2432 		}
2433 
2434 		wpa_printf(MSG_DEBUG, "NDIS: WZC re-enabled");
2435 		return wpa_driver_ndis_rebind_adapter(drv);
2436 	}
2437 
2438 	/*
2439 	 * Unfortunately, just disabling the WZC for an interface is not enough
2440 	 * to free NDISUIO for us, so need to disable and unload WZC completely
2441 	 * for now when using WinCE with NDISUIO. In addition, must request
2442 	 * NDISUIO protocol to be rebound to the adapter in order to free the
2443 	 * NDISUIO binding that WZC hold before us.
2444 	 */
2445 
2446 	/* Enumerate HKLM\Drivers\Active\* to find a handle to WZC. */
2447 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, DEVLOAD_ACTIVE_KEY, 0, 0, &hk);
2448 	if (ret != ERROR_SUCCESS) {
2449 		wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(DEVLOAD_ACTIVE_KEY) "
2450 			   "failed: %d %d", (int) ret, (int) GetLastError());
2451 		return -1;
2452 	}
2453 
2454 	for (i = 0; ; i++) {
2455 		len = sizeof(keyname);
2456 		ret = RegEnumKeyEx(hk, i, keyname, &len, NULL, NULL, NULL,
2457 				   NULL);
2458 		if (ret != ERROR_SUCCESS) {
2459 			wpa_printf(MSG_DEBUG, "NDIS: Could not find active "
2460 				   "WZC - assuming it is not running.");
2461 			RegCloseKey(hk);
2462 			return -1;
2463 		}
2464 
2465 		ret = RegOpenKeyEx(hk, keyname, 0, 0, &hk2);
2466 		if (ret != ERROR_SUCCESS) {
2467 			wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(active dev) "
2468 				   "failed: %d %d",
2469 				   (int) ret, (int) GetLastError());
2470 			continue;
2471 		}
2472 
2473 		len = sizeof(devname);
2474 		ret = RegQueryValueEx(hk2, DEVLOAD_DEVKEY_VALNAME, NULL, NULL,
2475 				      (LPBYTE) devname, &len);
2476 		if (ret != ERROR_SUCCESS) {
2477 			wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx("
2478 				   "DEVKEY_VALNAME) failed: %d %d",
2479 				   (int) ret, (int) GetLastError());
2480 			RegCloseKey(hk2);
2481 			continue;
2482 		}
2483 
2484 		if (_tcscmp(devname, WZC_DRIVER) == 0)
2485 			break;
2486 
2487 		RegCloseKey(hk2);
2488 	}
2489 
2490 	RegCloseKey(hk);
2491 
2492 	/* Found WZC - get handle to it. */
2493 	len = sizeof(hnd);
2494 	ret = RegQueryValueEx(hk2, DEVLOAD_HANDLE_VALNAME, NULL, NULL,
2495 			      (PUCHAR) &hnd, &len);
2496 	if (ret != ERROR_SUCCESS) {
2497 		wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx(HANDLE_VALNAME) "
2498 			   "failed: %d %d", (int) ret, (int) GetLastError());
2499 		RegCloseKey(hk2);
2500 		return -1;
2501 	}
2502 
2503 	RegCloseKey(hk2);
2504 
2505 	/* Deactivate WZC */
2506 	if (!DeactivateDevice((HANDLE) hnd)) {
2507 		wpa_printf(MSG_DEBUG, "NDIS: DeactivateDevice failed: %d",
2508 			   (int) GetLastError());
2509 		return -1;
2510 	}
2511 
2512 	wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily");
2513 	drv->wzc_disabled = 1;
2514 	return wpa_driver_ndis_rebind_adapter(drv);
2515 
2516 #else /* _WIN32_WCE */
2517 
2518 	HMODULE hm;
2519 	DWORD (WINAPI *wzc_enum_interf)(LPWSTR pSrvAddr,
2520 					PINTFS_KEY_TABLE pIntfs);
2521 	DWORD (WINAPI *wzc_query_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2522 					 PINTF_ENTRY pIntf,
2523 					 LPDWORD pdwOutFlags);
2524 	DWORD (WINAPI *wzc_set_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2525 				       PINTF_ENTRY pIntf, LPDWORD pdwOutFlags);
2526 	int ret = -1, j;
2527 	DWORD res;
2528 	INTFS_KEY_TABLE guids;
2529 	INTF_ENTRY intf;
2530 	char guid[128];
2531 	WCHAR *pos;
2532 	DWORD flags, i;
2533 
2534 	hm = LoadLibrary(TEXT("wzcsapi.dll"));
2535 	if (hm == NULL) {
2536 		wpa_printf(MSG_DEBUG, "NDIS: Failed to load wzcsapi.dll (%u) "
2537 			   "- WZC probably not running",
2538 			   (unsigned int) GetLastError());
2539 		return -1;
2540 	}
2541 
2542 #ifdef _WIN32_WCE
2543 	wzc_enum_interf = (void *) GetProcAddressA(hm, "WZCEnumInterfaces");
2544 	wzc_query_interf = (void *) GetProcAddressA(hm, "WZCQueryInterface");
2545 	wzc_set_interf = (void *) GetProcAddressA(hm, "WZCSetInterface");
2546 #else /* _WIN32_WCE */
2547 	wzc_enum_interf = (void *) GetProcAddress(hm, "WZCEnumInterfaces");
2548 	wzc_query_interf = (void *) GetProcAddress(hm, "WZCQueryInterface");
2549 	wzc_set_interf = (void *) GetProcAddress(hm, "WZCSetInterface");
2550 #endif /* _WIN32_WCE */
2551 
2552 	if (wzc_enum_interf == NULL || wzc_query_interf == NULL ||
2553 	    wzc_set_interf == NULL) {
2554 		wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces, "
2555 			   "WZCQueryInterface, or WZCSetInterface not found "
2556 			   "in wzcsapi.dll");
2557 		goto fail;
2558 	}
2559 
2560 	os_memset(&guids, 0, sizeof(guids));
2561 	res = wzc_enum_interf(NULL, &guids);
2562 	if (res != 0) {
2563 		wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces failed: %d; "
2564 			   "WZC service is apparently not running",
2565 			   (int) res);
2566 		goto fail;
2567 	}
2568 
2569 	wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces: %d interfaces",
2570 		   (int) guids.dwNumIntfs);
2571 
2572 	for (i = 0; i < guids.dwNumIntfs; i++) {
2573 		pos = guids.pIntfs[i].wszGuid;
2574 		for (j = 0; j < sizeof(guid); j++) {
2575 			guid[j] = (char) *pos;
2576 			if (*pos == 0)
2577 				break;
2578 			pos++;
2579 		}
2580 		guid[sizeof(guid) - 1] = '\0';
2581 		wpa_printf(MSG_DEBUG, "NDIS: intfs %d GUID '%s'",
2582 			   (int) i, guid);
2583 		if (os_strstr(drv->ifname, guid) == NULL)
2584 			continue;
2585 
2586 		wpa_printf(MSG_DEBUG, "NDIS: Current interface found from "
2587 			   "WZC");
2588 		break;
2589 	}
2590 
2591 	if (i >= guids.dwNumIntfs) {
2592 		wpa_printf(MSG_DEBUG, "NDIS: Current interface not found from "
2593 			   "WZC");
2594 		goto fail;
2595 	}
2596 
2597 	os_memset(&intf, 0, sizeof(intf));
2598 	intf.wszGuid = guids.pIntfs[i].wszGuid;
2599 	/* Set flags to verify that the structure has not changed. */
2600 	intf.dwCtlFlags = -1;
2601 	flags = 0;
2602 	res = wzc_query_interf(NULL, INTFCTL_ENABLED, &intf, &flags);
2603 	if (res != 0) {
2604 		wpa_printf(MSG_DEBUG, "NDIS: Could not query flags for the "
2605 			   "WZC interface: %d (0x%x)",
2606 			   (int) res, (int) res);
2607 		wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2608 			   (unsigned int) GetLastError());
2609 		goto fail;
2610 	}
2611 
2612 	wpa_printf(MSG_DEBUG, "NDIS: WZC interface flags 0x%x dwCtlFlags 0x%x",
2613 		   (int) flags, (int) intf.dwCtlFlags);
2614 
2615 	if (intf.dwCtlFlags == -1) {
2616 		wpa_printf(MSG_DEBUG, "NDIS: Looks like wzcsapi has changed "
2617 			   "again - could not disable WZC");
2618 		wpa_hexdump(MSG_MSGDUMP, "NDIS: intf",
2619 			    (u8 *) &intf, sizeof(intf));
2620 		goto fail;
2621 	}
2622 
2623 	if (enable) {
2624 		if (!(intf.dwCtlFlags & INTFCTL_ENABLED)) {
2625 			wpa_printf(MSG_DEBUG, "NDIS: Enabling WZC for this "
2626 				   "interface");
2627 			intf.dwCtlFlags |= INTFCTL_ENABLED;
2628 			res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2629 					     &flags);
2630 			if (res != 0) {
2631 				wpa_printf(MSG_DEBUG, "NDIS: Failed to enable "
2632 					   "WZC: %d (0x%x)",
2633 					   (int) res, (int) res);
2634 				wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2635 					   (unsigned int) GetLastError());
2636 				goto fail;
2637 			}
2638 			wpa_printf(MSG_DEBUG, "NDIS: Re-enabled WZC for this "
2639 				   "interface");
2640 			drv->wzc_disabled = 0;
2641 		}
2642 	} else {
2643 		if (intf.dwCtlFlags & INTFCTL_ENABLED) {
2644 			wpa_printf(MSG_DEBUG, "NDIS: Disabling WZC for this "
2645 				   "interface");
2646 			intf.dwCtlFlags &= ~INTFCTL_ENABLED;
2647 			res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2648 					     &flags);
2649 			if (res != 0) {
2650 				wpa_printf(MSG_DEBUG, "NDIS: Failed to "
2651 					   "disable WZC: %d (0x%x)",
2652 					   (int) res, (int) res);
2653 				wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2654 					   (unsigned int) GetLastError());
2655 				goto fail;
2656 			}
2657 			wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily "
2658 				   "for this interface");
2659 			drv->wzc_disabled = 1;
2660 		} else {
2661 			wpa_printf(MSG_DEBUG, "NDIS: WZC was not enabled for "
2662 				   "this interface");
2663 		}
2664 	}
2665 
2666 	ret = 0;
2667 
2668 fail:
2669 	FreeLibrary(hm);
2670 
2671 	return ret;
2672 #endif /* _WIN32_WCE */
2673 }
2674 
2675 #else /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2676 
2677 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2678 				   int enable)
2679 {
2680 	return 0;
2681 }
2682 
2683 #endif /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2684 
2685 
2686 #ifdef CONFIG_USE_NDISUIO
2687 /*
2688  * l2_packet_ndis.c is sharing the same handle to NDISUIO, so we must be able
2689  * to export this handle. This is somewhat ugly, but there is no better
2690  * mechanism available to pass data from driver interface to l2_packet wrapper.
2691  */
2692 static HANDLE driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2693 
2694 HANDLE driver_ndis_get_ndisuio_handle(void)
2695 {
2696 	return driver_ndis_ndisuio_handle;
2697 }
2698 #endif /* CONFIG_USE_NDISUIO */
2699 
2700 
2701 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv)
2702 {
2703 #ifdef CONFIG_USE_NDISUIO
2704 #ifndef _WIN32_WCE
2705 #define NDISUIO_DEVICE_NAME TEXT("\\\\.\\\\Ndisuio")
2706 	DWORD written;
2707 #endif /* _WIN32_WCE */
2708 	drv->ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2709 				  GENERIC_READ | GENERIC_WRITE, 0, NULL,
2710 				  OPEN_EXISTING,
2711 				  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
2712 				  INVALID_HANDLE_VALUE);
2713 	if (drv->ndisuio == INVALID_HANDLE_VALUE) {
2714 		wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to "
2715 			   "NDISUIO: %d", (int) GetLastError());
2716 		return -1;
2717 	}
2718 	driver_ndis_ndisuio_handle = drv->ndisuio;
2719 
2720 #ifndef _WIN32_WCE
2721 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0,
2722 			     NULL, 0, &written, NULL)) {
2723 		wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: "
2724 			   "%d", (int) GetLastError());
2725 		CloseHandle(drv->ndisuio);
2726 		drv->ndisuio = INVALID_HANDLE_VALUE;
2727 		return -1;
2728 	}
2729 #endif /* _WIN32_WCE */
2730 
2731 	return 0;
2732 #else /* CONFIG_USE_NDISUIO */
2733 	return 0;
2734 #endif /* CONFIG_USE_NDISUIO */
2735 }
2736 
2737 
2738 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv)
2739 {
2740 #ifdef CONFIG_USE_NDISUIO
2741 	DWORD written;
2742 #define MAX_NDIS_DEVICE_NAME_LEN 256
2743 	WCHAR ifname[MAX_NDIS_DEVICE_NAME_LEN];
2744 	size_t len, i, pos;
2745 	const char *prefix = "\\DEVICE\\";
2746 
2747 #ifdef _WIN32_WCE
2748 	pos = 0;
2749 #else /* _WIN32_WCE */
2750 	pos = 8;
2751 #endif /* _WIN32_WCE */
2752 	len = pos + os_strlen(drv->ifname);
2753 	if (len >= MAX_NDIS_DEVICE_NAME_LEN)
2754 		return -1;
2755 	for (i = 0; i < pos; i++)
2756 		ifname[i] = (WCHAR) prefix[i];
2757 	for (i = pos; i < len; i++)
2758 		ifname[i] = (WCHAR) drv->ifname[i - pos];
2759 	ifname[i] = L'\0';
2760 
2761 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_OPEN_DEVICE,
2762 			     ifname, len * sizeof(WCHAR), NULL, 0, &written,
2763 			     NULL)) {
2764 		wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_OPEN_DEVICE "
2765 			   "failed: %d", (int) GetLastError());
2766 		wpa_hexdump_ascii(MSG_DEBUG, "NDIS: ifname",
2767 				  (const u8 *) ifname, len * sizeof(WCHAR));
2768 		CloseHandle(drv->ndisuio);
2769 		drv->ndisuio = INVALID_HANDLE_VALUE;
2770 		return -1;
2771 	}
2772 
2773 	wpa_printf(MSG_DEBUG, "NDIS: Opened NDISUIO device successfully");
2774 
2775 	return 0;
2776 #else /* CONFIG_USE_NDISUIO */
2777 	char ifname[128];
2778 	os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", drv->ifname);
2779 	drv->adapter = PacketOpenAdapter(ifname);
2780 	if (drv->adapter == NULL) {
2781 		wpa_printf(MSG_DEBUG, "NDIS: PacketOpenAdapter failed for "
2782 			   "'%s'", ifname);
2783 		return -1;
2784 	}
2785 	return 0;
2786 #endif /* CONFIG_USE_NDISUIO */
2787 }
2788 
2789 
2790 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv)
2791 {
2792 #ifdef CONFIG_USE_NDISUIO
2793 	driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2794 	if (drv->ndisuio != INVALID_HANDLE_VALUE)
2795 		CloseHandle(drv->ndisuio);
2796 #else /* CONFIG_USE_NDISUIO */
2797 	if (drv->adapter)
2798 		PacketCloseAdapter(drv->adapter);
2799 #endif /* CONFIG_USE_NDISUIO */
2800 }
2801 
2802 
2803 static int ndis_add_multicast(struct wpa_driver_ndis_data *drv)
2804 {
2805 	if (ndis_set_oid(drv, OID_802_3_MULTICAST_LIST,
2806 			 (const char *) pae_group_addr, ETH_ALEN) < 0) {
2807 		wpa_printf(MSG_DEBUG, "NDIS: Failed to add PAE group address "
2808 			   "to the multicast list");
2809 		return -1;
2810 	}
2811 
2812 	return 0;
2813 }
2814 
2815 
2816 static void * wpa_driver_ndis_init(void *ctx, const char *ifname)
2817 {
2818 	struct wpa_driver_ndis_data *drv;
2819 	u32 mode;
2820 	int i;
2821 
2822 	drv = os_zalloc(sizeof(*drv));
2823 	if (drv == NULL)
2824 		return NULL;
2825 	drv->ctx = ctx;
2826 	/*
2827 	 * Compatibility code to strip possible prefix from the GUID. Previous
2828 	 * versions include \Device\NPF_ prefix for all names, but the internal
2829 	 * interface name is now only the GUI. Both Packet32 and NDISUIO
2830 	 * prefixes are supported.
2831 	 */
2832 	if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0)
2833 		ifname += 12;
2834 	else if (os_strncmp(ifname, "\\DEVICE\\", 8) == 0)
2835 		ifname += 8;
2836 	os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
2837 
2838 	if (wpa_driver_ndis_adapter_init(drv) < 0) {
2839 		os_free(drv);
2840 		return NULL;
2841 	}
2842 
2843 	if (wpa_driver_ndis_get_names(drv) < 0) {
2844 		wpa_driver_ndis_adapter_close(drv);
2845 		os_free(drv);
2846 		return NULL;
2847 	}
2848 
2849 	wpa_driver_ndis_set_wzc(drv, 0);
2850 
2851 	if (wpa_driver_ndis_adapter_open(drv) < 0) {
2852 		wpa_driver_ndis_adapter_close(drv);
2853 		os_free(drv);
2854 		return NULL;
2855 	}
2856 
2857 	if (ndis_get_oid(drv, OID_802_3_CURRENT_ADDRESS,
2858 			 (char *) drv->own_addr, ETH_ALEN) < 0) {
2859 		wpa_printf(MSG_DEBUG, "NDIS: Get OID_802_3_CURRENT_ADDRESS "
2860 			   "failed");
2861 		wpa_driver_ndis_adapter_close(drv);
2862 		os_free(drv);
2863 		return NULL;
2864 	}
2865 	wpa_driver_ndis_get_capability(drv);
2866 
2867 	/* Update per interface supported AKMs */
2868 	for (i = 0; i < WPA_IF_MAX; i++)
2869 		drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
2870 
2871 
2872 	/* Make sure that the driver does not have any obsolete PMKID entries.
2873 	 */
2874 	wpa_driver_ndis_flush_pmkid(drv);
2875 
2876 	/*
2877 	 * Disconnect to make sure that driver re-associates if it was
2878 	 * connected.
2879 	 */
2880 	wpa_driver_ndis_disconnect(drv);
2881 
2882 	eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout, drv, NULL);
2883 
2884 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2885 	drv->events = ndis_events_init(&drv->events_pipe, &drv->event_avail,
2886 				       drv->ifname, drv->adapter_desc);
2887 	if (drv->events == NULL) {
2888 		wpa_driver_ndis_deinit(drv);
2889 		return NULL;
2890 	}
2891 	eloop_register_event(drv->event_avail, sizeof(drv->event_avail),
2892 			     wpa_driver_ndis_event_pipe_cb, drv, NULL);
2893 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2894 
2895 #ifdef _WIN32_WCE
2896 	if (ndisuio_notification_init(drv) < 0) {
2897 		wpa_driver_ndis_deinit(drv);
2898 		return NULL;
2899 	}
2900 #endif /* _WIN32_WCE */
2901 
2902 	/* Set mode here in case card was configured for ad-hoc mode
2903 	 * previously. */
2904 	mode = Ndis802_11Infrastructure;
2905 	if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
2906 			 (char *) &mode, sizeof(mode)) < 0) {
2907 		char buf[8];
2908 		int res;
2909 		wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
2910 			   "OID_802_11_INFRASTRUCTURE_MODE (%d)",
2911 			   (int) mode);
2912 		/* Try to continue anyway */
2913 
2914 		res = ndis_get_oid(drv, OID_DOT11_CURRENT_OPERATION_MODE, buf,
2915 				   sizeof(buf));
2916 		if (res > 0) {
2917 			wpa_printf(MSG_INFO, "NDIS: The driver seems to use "
2918 				   "Native 802.11 OIDs. These are not yet "
2919 				   "fully supported.");
2920 			drv->native80211 = 1;
2921 		} else if (!drv->has_capability || drv->capa.enc == 0) {
2922 			/*
2923 			 * Note: This will also happen with NDIS 6 drivers with
2924 			 * Vista.
2925 			 */
2926 			wpa_printf(MSG_DEBUG, "NDIS: Driver did not provide "
2927 				   "any wireless capabilities - assume it is "
2928 				   "a wired interface");
2929 			drv->wired = 1;
2930 			drv->capa.flags |= WPA_DRIVER_FLAGS_WIRED;
2931 			drv->has_capability = 1;
2932 			ndis_add_multicast(drv);
2933 		}
2934 	}
2935 
2936 	return drv;
2937 }
2938 
2939 
2940 static void wpa_driver_ndis_deinit(void *priv)
2941 {
2942 	struct wpa_driver_ndis_data *drv = priv;
2943 
2944 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2945 	if (drv->events) {
2946 		eloop_unregister_event(drv->event_avail,
2947 				       sizeof(drv->event_avail));
2948 		ndis_events_deinit(drv->events);
2949 	}
2950 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2951 
2952 #ifdef _WIN32_WCE
2953 	ndisuio_notification_deinit(drv);
2954 #endif /* _WIN32_WCE */
2955 
2956 	eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
2957 	eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
2958 	wpa_driver_ndis_flush_pmkid(drv);
2959 	wpa_driver_ndis_disconnect(drv);
2960 	if (wpa_driver_ndis_radio_off(drv) < 0) {
2961 		wpa_printf(MSG_DEBUG, "NDIS: failed to disassociate and turn "
2962 			   "radio off");
2963 	}
2964 
2965 	wpa_driver_ndis_adapter_close(drv);
2966 
2967 	if (drv->wzc_disabled)
2968 		wpa_driver_ndis_set_wzc(drv, 1);
2969 
2970 #ifdef _WIN32_WCE
2971 	os_free(drv->adapter_name);
2972 #endif /* _WIN32_WCE */
2973 	os_free(drv->adapter_desc);
2974 	os_free(drv);
2975 }
2976 
2977 
2978 static struct wpa_interface_info *
2979 wpa_driver_ndis_get_interfaces(void *global_priv)
2980 {
2981 	struct wpa_interface_info *iface = NULL, *niface;
2982 
2983 #ifdef CONFIG_USE_NDISUIO
2984 	NDISUIO_QUERY_BINDING *b;
2985 	size_t blen = sizeof(*b) + 1024;
2986 	int i, error;
2987 	DWORD written;
2988 	char name[256], desc[256];
2989 	WCHAR *pos;
2990 	size_t j, len;
2991 	HANDLE ndisuio;
2992 
2993 	ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2994 			     GENERIC_READ | GENERIC_WRITE, 0, NULL,
2995 			     OPEN_EXISTING,
2996 			     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
2997 			     INVALID_HANDLE_VALUE);
2998 	if (ndisuio == INVALID_HANDLE_VALUE) {
2999 		wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to "
3000 			   "NDISUIO: %d", (int) GetLastError());
3001 		return NULL;
3002 	}
3003 
3004 #ifndef _WIN32_WCE
3005 	if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0,
3006 			     NULL, 0, &written, NULL)) {
3007 		wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: "
3008 			   "%d", (int) GetLastError());
3009 		CloseHandle(ndisuio);
3010 		return NULL;
3011 	}
3012 #endif /* _WIN32_WCE */
3013 
3014 	b = os_malloc(blen);
3015 	if (b == NULL) {
3016 		CloseHandle(ndisuio);
3017 		return NULL;
3018 	}
3019 
3020 	for (i = 0; ; i++) {
3021 		os_memset(b, 0, blen);
3022 		b->BindingIndex = i;
3023 		if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
3024 				     b, sizeof(NDISUIO_QUERY_BINDING), b, blen,
3025 				     &written, NULL)) {
3026 			error = (int) GetLastError();
3027 			if (error == ERROR_NO_MORE_ITEMS)
3028 				break;
3029 			wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
3030 				   "failed: %d", error);
3031 			break;
3032 		}
3033 
3034 		pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
3035 		len = b->DeviceNameLength;
3036 		if (len >= sizeof(name))
3037 			len = sizeof(name) - 1;
3038 		for (j = 0; j < len; j++)
3039 			name[j] = (char) pos[j];
3040 		name[len] = '\0';
3041 
3042 		pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
3043 		len = b->DeviceDescrLength;
3044 		if (len >= sizeof(desc))
3045 			len = sizeof(desc) - 1;
3046 		for (j = 0; j < len; j++)
3047 			desc[j] = (char) pos[j];
3048 		desc[len] = '\0';
3049 
3050 		wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
3051 
3052 		niface = os_zalloc(sizeof(*niface));
3053 		if (niface == NULL)
3054 			break;
3055 		niface->drv_name = "ndis";
3056 		if (os_strncmp(name, "\\DEVICE\\", 8) == 0)
3057 			niface->ifname = os_strdup(name + 8);
3058 		else
3059 			niface->ifname = os_strdup(name);
3060 		if (niface->ifname == NULL) {
3061 			os_free(niface);
3062 			break;
3063 		}
3064 		niface->desc = os_strdup(desc);
3065 		niface->next = iface;
3066 		iface = niface;
3067 	}
3068 
3069 	os_free(b);
3070 	CloseHandle(ndisuio);
3071 #else /* CONFIG_USE_NDISUIO */
3072 	PTSTR _names;
3073 	char *names, *pos, *pos2;
3074 	ULONG len;
3075 	BOOLEAN res;
3076 	char *name[MAX_ADAPTERS];
3077 	char *desc[MAX_ADAPTERS];
3078 	int num_name, num_desc, i;
3079 
3080 	wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
3081 		   PacketGetVersion());
3082 
3083 	len = 8192;
3084 	_names = os_zalloc(len);
3085 	if (_names == NULL)
3086 		return NULL;
3087 
3088 	res = PacketGetAdapterNames(_names, &len);
3089 	if (!res && len > 8192) {
3090 		os_free(_names);
3091 		_names = os_zalloc(len);
3092 		if (_names == NULL)
3093 			return NULL;
3094 		res = PacketGetAdapterNames(_names, &len);
3095 	}
3096 
3097 	if (!res) {
3098 		wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
3099 			   "(PacketGetAdapterNames)");
3100 		os_free(_names);
3101 		return NULL;
3102 	}
3103 
3104 	names = (char *) _names;
3105 	if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') {
3106 		wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in "
3107 			   "UNICODE");
3108 		/* Convert to ASCII */
3109 		pos2 = pos = names;
3110 		while (pos2 < names + len) {
3111 			if (pos2[0] == '\0' && pos2[1] == '\0' &&
3112 			    pos2[2] == '\0' && pos2[3] == '\0') {
3113 				pos2 += 4;
3114 				break;
3115 			}
3116 			*pos++ = pos2[0];
3117 			pos2 += 2;
3118 		}
3119 		os_memcpy(pos + 2, names, pos - names);
3120 		pos += 2;
3121 	} else
3122 		pos = names;
3123 
3124 	num_name = 0;
3125 	while (pos < names + len) {
3126 		name[num_name] = pos;
3127 		while (*pos && pos < names + len)
3128 			pos++;
3129 		if (pos + 1 >= names + len) {
3130 			os_free(names);
3131 			return NULL;
3132 		}
3133 		pos++;
3134 		num_name++;
3135 		if (num_name >= MAX_ADAPTERS) {
3136 			wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
3137 			os_free(names);
3138 			return NULL;
3139 		}
3140 		if (*pos == '\0') {
3141 			wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
3142 				   num_name);
3143 			pos++;
3144 			break;
3145 		}
3146 	}
3147 
3148 	num_desc = 0;
3149 	while (pos < names + len) {
3150 		desc[num_desc] = pos;
3151 		while (*pos && pos < names + len)
3152 			pos++;
3153 		if (pos + 1 >= names + len) {
3154 			os_free(names);
3155 			return NULL;
3156 		}
3157 		pos++;
3158 		num_desc++;
3159 		if (num_desc >= MAX_ADAPTERS) {
3160 			wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
3161 				   "descriptions");
3162 			os_free(names);
3163 			return NULL;
3164 		}
3165 		if (*pos == '\0') {
3166 			wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
3167 				   "found", num_name);
3168 			pos++;
3169 			break;
3170 		}
3171 	}
3172 
3173 	/*
3174 	 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
3175 	 * descriptions. Fill in stub descriptors to work around this.
3176 	 */
3177 	while (num_desc < num_name)
3178 		desc[num_desc++] = "stub description";
3179 
3180 	if (num_name != num_desc) {
3181 		wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and "
3182 			   "description counts (%d != %d)",
3183 			   num_name, num_desc);
3184 		os_free(names);
3185 		return NULL;
3186 	}
3187 
3188 	for (i = 0; i < num_name; i++) {
3189 		niface = os_zalloc(sizeof(*niface));
3190 		if (niface == NULL)
3191 			break;
3192 		niface->drv_name = "ndis";
3193 		if (os_strncmp(name[i], "\\Device\\NPF_", 12) == 0)
3194 			niface->ifname = os_strdup(name[i] + 12);
3195 		else
3196 			niface->ifname = os_strdup(name[i]);
3197 		if (niface->ifname == NULL) {
3198 			os_free(niface);
3199 			break;
3200 		}
3201 		niface->desc = os_strdup(desc[i]);
3202 		niface->next = iface;
3203 		iface = niface;
3204 	}
3205 
3206 #endif /* CONFIG_USE_NDISUIO */
3207 
3208 	return iface;
3209 }
3210 
3211 
3212 static const char *ndis_drv_name = "ndis";
3213 static const char *ndis_drv_desc = "Windows NDIS driver";
3214 
3215 struct wpa_driver_ops wpa_driver_ndis_ops;
3216 
3217 void driver_ndis_init_ops(void)
3218 {
3219 	os_memset(&wpa_driver_ndis_ops, 0, sizeof(wpa_driver_ndis_ops));
3220 	wpa_driver_ndis_ops.name = ndis_drv_name;
3221 	wpa_driver_ndis_ops.desc = ndis_drv_desc;
3222 	wpa_driver_ndis_ops.get_bssid = wpa_driver_ndis_get_bssid;
3223 	wpa_driver_ndis_ops.get_ssid = wpa_driver_ndis_get_ssid;
3224 	wpa_driver_ndis_ops.set_key = wpa_driver_ndis_set_key_wrapper;
3225 	wpa_driver_ndis_ops.init = wpa_driver_ndis_init;
3226 	wpa_driver_ndis_ops.deinit = wpa_driver_ndis_deinit;
3227 	wpa_driver_ndis_ops.deauthenticate = wpa_driver_ndis_deauthenticate;
3228 	wpa_driver_ndis_ops.associate = wpa_driver_ndis_associate;
3229 	wpa_driver_ndis_ops.add_pmkid = wpa_driver_ndis_add_pmkid;
3230 	wpa_driver_ndis_ops.remove_pmkid = wpa_driver_ndis_remove_pmkid;
3231 	wpa_driver_ndis_ops.flush_pmkid = wpa_driver_ndis_flush_pmkid;
3232 	wpa_driver_ndis_ops.get_capa = wpa_driver_ndis_get_capa;
3233 	wpa_driver_ndis_ops.poll = wpa_driver_ndis_poll;
3234 	wpa_driver_ndis_ops.get_ifname = wpa_driver_ndis_get_ifname;
3235 	wpa_driver_ndis_ops.get_mac_addr = wpa_driver_ndis_get_mac_addr;
3236 	wpa_driver_ndis_ops.get_scan_results2 =
3237 		wpa_driver_ndis_get_scan_results;
3238 	wpa_driver_ndis_ops.get_interfaces = wpa_driver_ndis_get_interfaces;
3239 	wpa_driver_ndis_ops.scan2 = wpa_driver_ndis_scan;
3240 }
3241