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 
ndis_get_oid(struct wpa_driver_ndis_data * drv,unsigned int oid,char * data,size_t len)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 
ndis_set_oid(struct wpa_driver_ndis_data * drv,unsigned int oid,const char * data,size_t len)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 
ndis_set_auth_mode(struct wpa_driver_ndis_data * drv,int mode)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 
ndis_get_auth_mode(struct wpa_driver_ndis_data * drv)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 
ndis_set_encr_status(struct wpa_driver_ndis_data * drv,int encr)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 
ndis_get_encr_status(struct wpa_driver_ndis_data * drv)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 
wpa_driver_ndis_get_bssid(void * priv,u8 * bssid)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 
wpa_driver_ndis_get_ssid(void * priv,u8 * ssid)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 
wpa_driver_ndis_set_ssid(struct wpa_driver_ndis_data * drv,const u8 * ssid,size_t ssid_len)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  */
wpa_driver_ndis_radio_off(struct wpa_driver_ndis_data * drv)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). */
wpa_driver_ndis_disconnect(struct wpa_driver_ndis_data * drv)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 
wpa_driver_ndis_deauthenticate(void * priv,const u8 * addr,u16 reason_code)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 
wpa_driver_ndis_scan_timeout(void * eloop_ctx,void * timeout_ctx)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 
wpa_driver_ndis_scan_native80211(struct wpa_driver_ndis_data * drv,struct wpa_driver_scan_params * params)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 
wpa_driver_ndis_scan(void * priv,struct wpa_driver_scan_params * params)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 
wpa_scan_get_ie(const struct wpa_scan_res * res,u8 ie)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 
wpa_driver_ndis_add_scan_ssid(struct wpa_scan_res * r,NDIS_802_11_SSID * ssid)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 
wpa_driver_ndis_get_scan_results(void * priv)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 
wpa_driver_ndis_remove_key(struct wpa_driver_ndis_data * drv,int key_idx,const u8 * addr,const u8 * bssid,int pairwise)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 
wpa_driver_ndis_add_wep(struct wpa_driver_ndis_data * drv,int pairwise,int key_idx,int set_tx,const u8 * key,size_t key_len)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 
wpa_driver_ndis_set_key(const char * ifname,void * priv,enum wpa_alg alg,const u8 * addr,int key_idx,int set_tx,const u8 * seq,size_t seq_len,const u8 * key,size_t key_len)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
wpa_driver_ndis_associate(void * priv,struct wpa_driver_associate_params * params)1037 wpa_driver_ndis_associate(void *priv,
1038 			  struct wpa_driver_associate_params *params)
1039 {
1040 	struct wpa_driver_ndis_data *drv = priv;
1041 	u32 auth_mode, encr, priv_mode, mode;
1042 	u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1043 
1044 	drv->mode = params->mode;
1045 
1046 	/* Note: Setting OID_802_11_INFRASTRUCTURE_MODE clears current keys,
1047 	 * so static WEP keys needs to be set again after this. */
1048 	if (params->mode == IEEE80211_MODE_IBSS) {
1049 		mode = Ndis802_11IBSS;
1050 		/* Need to make sure that BSSID polling is enabled for
1051 		 * IBSS mode. */
1052 		eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
1053 		eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1054 				       drv, NULL);
1055 	} else
1056 		mode = Ndis802_11Infrastructure;
1057 	if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
1058 			 (char *) &mode, sizeof(mode)) < 0) {
1059 		wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
1060 			   "OID_802_11_INFRASTRUCTURE_MODE (%d)",
1061 			   (int) mode);
1062 		/* Try to continue anyway */
1063 	}
1064 
1065 	if (params->key_mgmt_suite == WPA_KEY_MGMT_NONE ||
1066 	    params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1067 		/* Re-set WEP keys if static WEP configuration is used. */
1068 		int i;
1069 		for (i = 0; i < 4; i++) {
1070 			if (!params->wep_key[i])
1071 				continue;
1072 			wpa_printf(MSG_DEBUG, "NDIS: Re-setting static WEP "
1073 				   "key %d", i);
1074 			wpa_driver_ndis_set_key(drv->ifname, drv, WPA_ALG_WEP,
1075 						bcast, i,
1076 						i == params->wep_tx_keyidx,
1077 						NULL, 0, params->wep_key[i],
1078 						params->wep_key_len[i]);
1079 		}
1080 	}
1081 
1082 	if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
1083 		if (params->auth_alg & WPA_AUTH_ALG_SHARED) {
1084 			if (params->auth_alg & WPA_AUTH_ALG_OPEN)
1085 				auth_mode = Ndis802_11AuthModeAutoSwitch;
1086 			else
1087 				auth_mode = Ndis802_11AuthModeShared;
1088 		} else
1089 			auth_mode = Ndis802_11AuthModeOpen;
1090 		priv_mode = Ndis802_11PrivFilterAcceptAll;
1091 	} else if (params->wpa_ie[0] == WLAN_EID_RSN) {
1092 		priv_mode = Ndis802_11PrivFilter8021xWEP;
1093 		if (params->key_mgmt_suite == WPA_KEY_MGMT_PSK)
1094 			auth_mode = Ndis802_11AuthModeWPA2PSK;
1095 		else
1096 			auth_mode = Ndis802_11AuthModeWPA2;
1097 #ifdef CONFIG_WPS
1098 	} else if (params->key_mgmt_suite == WPA_KEY_MGMT_WPS) {
1099 		auth_mode = Ndis802_11AuthModeOpen;
1100 		priv_mode = Ndis802_11PrivFilterAcceptAll;
1101 		if (params->wps == WPS_MODE_PRIVACY) {
1102 			u8 dummy_key[5] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
1103 			/*
1104 			 * Some NDIS drivers refuse to associate in open mode
1105 			 * configuration due to Privacy field mismatch, so use
1106 			 * a workaround to make the configuration look like
1107 			 * matching one for WPS provisioning.
1108 			 */
1109 			wpa_printf(MSG_DEBUG, "NDIS: Set dummy WEP key as a "
1110 				   "workaround to allow driver to associate "
1111 				   "for WPS");
1112 			wpa_driver_ndis_set_key(drv->ifname, drv, WPA_ALG_WEP,
1113 						bcast, 0, 1,
1114 						NULL, 0, dummy_key,
1115 						sizeof(dummy_key));
1116 		}
1117 #endif /* CONFIG_WPS */
1118 	} else {
1119 		priv_mode = Ndis802_11PrivFilter8021xWEP;
1120 		if (params->key_mgmt_suite == WPA_KEY_MGMT_WPA_NONE)
1121 			auth_mode = Ndis802_11AuthModeWPANone;
1122 		else if (params->key_mgmt_suite == WPA_KEY_MGMT_PSK)
1123 			auth_mode = Ndis802_11AuthModeWPAPSK;
1124 		else
1125 			auth_mode = Ndis802_11AuthModeWPA;
1126 	}
1127 
1128 	switch (params->pairwise_suite) {
1129 	case WPA_CIPHER_CCMP:
1130 		encr = Ndis802_11Encryption3Enabled;
1131 		break;
1132 	case WPA_CIPHER_TKIP:
1133 		encr = Ndis802_11Encryption2Enabled;
1134 		break;
1135 	case WPA_CIPHER_WEP40:
1136 	case WPA_CIPHER_WEP104:
1137 		encr = Ndis802_11Encryption1Enabled;
1138 		break;
1139 	case WPA_CIPHER_NONE:
1140 #ifdef CONFIG_WPS
1141 		if (params->wps == WPS_MODE_PRIVACY) {
1142 			encr = Ndis802_11Encryption1Enabled;
1143 			break;
1144 		}
1145 #endif /* CONFIG_WPS */
1146 		if (params->group_suite == WPA_CIPHER_CCMP)
1147 			encr = Ndis802_11Encryption3Enabled;
1148 		else if (params->group_suite == WPA_CIPHER_TKIP)
1149 			encr = Ndis802_11Encryption2Enabled;
1150 		else
1151 			encr = Ndis802_11EncryptionDisabled;
1152 		break;
1153 	default:
1154 #ifdef CONFIG_WPS
1155 		if (params->wps == WPS_MODE_PRIVACY) {
1156 			encr = Ndis802_11Encryption1Enabled;
1157 			break;
1158 		}
1159 #endif /* CONFIG_WPS */
1160 		encr = Ndis802_11EncryptionDisabled;
1161 		break;
1162 	};
1163 
1164 	if (ndis_set_oid(drv, OID_802_11_PRIVACY_FILTER,
1165 			 (char *) &priv_mode, sizeof(priv_mode)) < 0) {
1166 		wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
1167 			   "OID_802_11_PRIVACY_FILTER (%d)",
1168 			   (int) priv_mode);
1169 		/* Try to continue anyway */
1170 	}
1171 
1172 	ndis_set_auth_mode(drv, auth_mode);
1173 	ndis_set_encr_status(drv, encr);
1174 
1175 	if (params->bssid) {
1176 		ndis_set_oid(drv, OID_802_11_BSSID, (char *) params->bssid,
1177 			     ETH_ALEN);
1178 		drv->oid_bssid_set = 1;
1179 	} else if (drv->oid_bssid_set) {
1180 		ndis_set_oid(drv, OID_802_11_BSSID, "\xff\xff\xff\xff\xff\xff",
1181 			     ETH_ALEN);
1182 		drv->oid_bssid_set = 0;
1183 	}
1184 
1185 	return wpa_driver_ndis_set_ssid(drv, params->ssid, params->ssid_len);
1186 }
1187 
1188 
wpa_driver_ndis_set_pmkid(struct wpa_driver_ndis_data * drv)1189 static int wpa_driver_ndis_set_pmkid(struct wpa_driver_ndis_data *drv)
1190 {
1191 	int len, count, i, ret;
1192 	struct ndis_pmkid_entry *entry;
1193 	NDIS_802_11_PMKID *p;
1194 
1195 	count = 0;
1196 	entry = drv->pmkid;
1197 	while (entry) {
1198 		count++;
1199 		if (count >= drv->no_of_pmkid)
1200 			break;
1201 		entry = entry->next;
1202 	}
1203 	len = 8 + count * sizeof(BSSID_INFO);
1204 	p = os_zalloc(len);
1205 	if (p == NULL)
1206 		return -1;
1207 
1208 	p->Length = len;
1209 	p->BSSIDInfoCount = count;
1210 	entry = drv->pmkid;
1211 	for (i = 0; i < count; i++) {
1212 		os_memcpy(&p->BSSIDInfo[i].BSSID, entry->bssid, ETH_ALEN);
1213 		os_memcpy(&p->BSSIDInfo[i].PMKID, entry->pmkid, 16);
1214 		entry = entry->next;
1215 	}
1216 	wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID", (u8 *) p, len);
1217 	ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) p, len);
1218 	os_free(p);
1219 	return ret;
1220 }
1221 
1222 
wpa_driver_ndis_add_pmkid(void * priv,struct wpa_pmkid_params * params)1223 static int wpa_driver_ndis_add_pmkid(void *priv,
1224 				     struct wpa_pmkid_params *params)
1225 {
1226 	struct wpa_driver_ndis_data *drv = priv;
1227 	struct ndis_pmkid_entry *entry, *prev;
1228 	const u8 *bssid = params->bssid;
1229 	const u8 *pmkid = params->pmkid;
1230 
1231 	if (!bssid || !pmkid)
1232 		return -1;
1233 	if (drv->no_of_pmkid == 0)
1234 		return 0;
1235 
1236 	prev = NULL;
1237 	entry = drv->pmkid;
1238 	while (entry) {
1239 		if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0)
1240 			break;
1241 		prev = entry;
1242 		entry = entry->next;
1243 	}
1244 
1245 	if (entry) {
1246 		/* Replace existing entry for this BSSID and move it into the
1247 		 * beginning of the list. */
1248 		os_memcpy(entry->pmkid, pmkid, 16);
1249 		if (prev) {
1250 			prev->next = entry->next;
1251 			entry->next = drv->pmkid;
1252 			drv->pmkid = entry;
1253 		}
1254 	} else {
1255 		entry = os_malloc(sizeof(*entry));
1256 		if (entry) {
1257 			os_memcpy(entry->bssid, bssid, ETH_ALEN);
1258 			os_memcpy(entry->pmkid, pmkid, 16);
1259 			entry->next = drv->pmkid;
1260 			drv->pmkid = entry;
1261 		}
1262 	}
1263 
1264 	return wpa_driver_ndis_set_pmkid(drv);
1265 }
1266 
1267 
wpa_driver_ndis_remove_pmkid(void * priv,struct wpa_pmkid_params * params)1268 static int wpa_driver_ndis_remove_pmkid(void *priv,
1269 					struct wpa_pmkid_params *params)
1270 {
1271 	struct wpa_driver_ndis_data *drv = priv;
1272 	struct ndis_pmkid_entry *entry, *prev;
1273 	const u8 *bssid = params->bssid;
1274 	const u8 *pmkid = params->pmkid;
1275 
1276 	if (!bssid || !pmkid)
1277 		return -1;
1278 	if (drv->no_of_pmkid == 0)
1279 		return 0;
1280 
1281 	entry = drv->pmkid;
1282 	prev = NULL;
1283 	while (entry) {
1284 		if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0 &&
1285 		    os_memcmp(entry->pmkid, pmkid, 16) == 0) {
1286 			if (prev)
1287 				prev->next = entry->next;
1288 			else
1289 				drv->pmkid = entry->next;
1290 			os_free(entry);
1291 			break;
1292 		}
1293 		prev = entry;
1294 		entry = entry->next;
1295 	}
1296 	return wpa_driver_ndis_set_pmkid(drv);
1297 }
1298 
1299 
wpa_driver_ndis_flush_pmkid(void * priv)1300 static int wpa_driver_ndis_flush_pmkid(void *priv)
1301 {
1302 	struct wpa_driver_ndis_data *drv = priv;
1303 	NDIS_802_11_PMKID p;
1304 	struct ndis_pmkid_entry *pmkid, *prev;
1305 	int prev_authmode, ret;
1306 
1307 	if (drv->no_of_pmkid == 0)
1308 		return 0;
1309 
1310 	pmkid = drv->pmkid;
1311 	drv->pmkid = NULL;
1312 	while (pmkid) {
1313 		prev = pmkid;
1314 		pmkid = pmkid->next;
1315 		os_free(prev);
1316 	}
1317 
1318 	/*
1319 	 * Some drivers may refuse OID_802_11_PMKID if authMode is not set to
1320 	 * WPA2, so change authMode temporarily, if needed.
1321 	 */
1322 	prev_authmode = ndis_get_auth_mode(drv);
1323 	if (prev_authmode != Ndis802_11AuthModeWPA2)
1324 		ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA2);
1325 
1326 	os_memset(&p, 0, sizeof(p));
1327 	p.Length = 8;
1328 	p.BSSIDInfoCount = 0;
1329 	wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID (flush)",
1330 		    (u8 *) &p, 8);
1331 	ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) &p, 8);
1332 
1333 	if (prev_authmode != Ndis802_11AuthModeWPA2)
1334 		ndis_set_auth_mode(drv, prev_authmode);
1335 
1336 	return ret;
1337 }
1338 
1339 
wpa_driver_ndis_get_associnfo(struct wpa_driver_ndis_data * drv)1340 static int wpa_driver_ndis_get_associnfo(struct wpa_driver_ndis_data *drv)
1341 {
1342 	char buf[512], *pos;
1343 	NDIS_802_11_ASSOCIATION_INFORMATION *ai;
1344 	int len;
1345 	union wpa_event_data data;
1346 	NDIS_802_11_BSSID_LIST_EX *b;
1347 	size_t blen, i;
1348 
1349 	len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION, buf,
1350 			   sizeof(buf));
1351 	if (len < 0) {
1352 		wpa_printf(MSG_DEBUG, "NDIS: failed to get association "
1353 			   "information");
1354 		return -1;
1355 	}
1356 	if (len > sizeof(buf)) {
1357 		/* Some drivers seem to be producing incorrect length for this
1358 		 * data. Limit the length to the current buffer size to avoid
1359 		 * crashing in hexdump. The data seems to be otherwise valid,
1360 		 * so better try to use it. */
1361 		wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association "
1362 			   "information length %d", len);
1363 		len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION,
1364 				   buf, sizeof(buf));
1365 		if (len < -1) {
1366 			wpa_printf(MSG_DEBUG, "NDIS: re-reading association "
1367 				   "information failed");
1368 			return -1;
1369 		}
1370 		if (len > sizeof(buf)) {
1371 			wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association"
1372 				   " information length %d (re-read)", len);
1373 			len = sizeof(buf);
1374 		}
1375 	}
1376 	wpa_hexdump(MSG_MSGDUMP, "NDIS: association information",
1377 		    (u8 *) buf, len);
1378 	if (len < sizeof(*ai)) {
1379 		wpa_printf(MSG_DEBUG, "NDIS: too short association "
1380 			   "information");
1381 		return -1;
1382 	}
1383 	ai = (NDIS_802_11_ASSOCIATION_INFORMATION *) buf;
1384 	wpa_printf(MSG_DEBUG, "NDIS: ReqFixed=0x%x RespFixed=0x%x off_req=%d "
1385 		   "off_resp=%d len_req=%d len_resp=%d",
1386 		   ai->AvailableRequestFixedIEs, ai->AvailableResponseFixedIEs,
1387 		   (int) ai->OffsetRequestIEs, (int) ai->OffsetResponseIEs,
1388 		   (int) ai->RequestIELength, (int) ai->ResponseIELength);
1389 
1390 	if (ai->OffsetRequestIEs + ai->RequestIELength > (unsigned) len ||
1391 	    ai->OffsetResponseIEs + ai->ResponseIELength > (unsigned) len) {
1392 		wpa_printf(MSG_DEBUG, "NDIS: association information - "
1393 			   "IE overflow");
1394 		return -1;
1395 	}
1396 
1397 	wpa_hexdump(MSG_MSGDUMP, "NDIS: Request IEs",
1398 		    (u8 *) buf + ai->OffsetRequestIEs, ai->RequestIELength);
1399 	wpa_hexdump(MSG_MSGDUMP, "NDIS: Response IEs",
1400 		    (u8 *) buf + ai->OffsetResponseIEs, ai->ResponseIELength);
1401 
1402 	os_memset(&data, 0, sizeof(data));
1403 	data.assoc_info.req_ies = (u8 *) buf + ai->OffsetRequestIEs;
1404 	data.assoc_info.req_ies_len = ai->RequestIELength;
1405 	data.assoc_info.resp_ies = (u8 *) buf + ai->OffsetResponseIEs;
1406 	data.assoc_info.resp_ies_len = ai->ResponseIELength;
1407 
1408 	blen = 65535;
1409 	b = os_zalloc(blen);
1410 	if (b == NULL)
1411 		goto skip_scan_results;
1412 	len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
1413 	if (len < 0) {
1414 		wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
1415 		os_free(b);
1416 		b = NULL;
1417 		goto skip_scan_results;
1418 	}
1419 	wpa_printf(MSG_DEBUG, "NDIS: %d BSSID items to process for AssocInfo",
1420 		   (unsigned int) b->NumberOfItems);
1421 
1422 	pos = (char *) &b->Bssid[0];
1423 	for (i = 0; i < b->NumberOfItems; i++) {
1424 		NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos;
1425 		if (os_memcmp(drv->bssid, bss->MacAddress, ETH_ALEN) == 0 &&
1426 		    bss->IELength > sizeof(NDIS_802_11_FIXED_IEs)) {
1427 			data.assoc_info.beacon_ies =
1428 				((u8 *) bss->IEs) +
1429 				sizeof(NDIS_802_11_FIXED_IEs);
1430 			data.assoc_info.beacon_ies_len =
1431 				bss->IELength - sizeof(NDIS_802_11_FIXED_IEs);
1432 			wpa_hexdump(MSG_MSGDUMP, "NDIS: Beacon IEs",
1433 				    data.assoc_info.beacon_ies,
1434 				    data.assoc_info.beacon_ies_len);
1435 			break;
1436 		}
1437 		pos += bss->Length;
1438 		if (pos > (char *) b + blen)
1439 			break;
1440 	}
1441 
1442 skip_scan_results:
1443 	wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
1444 
1445 	os_free(b);
1446 
1447 	return 0;
1448 }
1449 
1450 
wpa_driver_ndis_poll_timeout(void * eloop_ctx,void * timeout_ctx)1451 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx)
1452 {
1453 	struct wpa_driver_ndis_data *drv = eloop_ctx;
1454 	u8 bssid[ETH_ALEN];
1455 	int poll;
1456 
1457 	if (drv->wired)
1458 		return;
1459 
1460 	if (wpa_driver_ndis_get_bssid(drv, bssid)) {
1461 		/* Disconnected */
1462 		if (!is_zero_ether_addr(drv->bssid)) {
1463 			os_memset(drv->bssid, 0, ETH_ALEN);
1464 			wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1465 		}
1466 	} else {
1467 		/* Connected */
1468 		if (os_memcmp(drv->bssid, bssid, ETH_ALEN) != 0) {
1469 			os_memcpy(drv->bssid, bssid, ETH_ALEN);
1470 			wpa_driver_ndis_get_associnfo(drv);
1471 			wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1472 		}
1473 	}
1474 
1475 	/* When using integrated NDIS event receiver, we can skip BSSID
1476 	 * polling when using infrastructure network. However, when using
1477 	 * IBSS mode, many driver do not seem to generate connection event,
1478 	 * so we need to enable BSSID polling to figure out when IBSS network
1479 	 * has been formed.
1480 	 */
1481 	poll = drv->mode == IEEE80211_MODE_IBSS;
1482 #ifndef CONFIG_NDIS_EVENTS_INTEGRATED
1483 #ifndef _WIN32_WCE
1484 	poll = 1;
1485 #endif /* _WIN32_WCE */
1486 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
1487 
1488 	if (poll) {
1489 		eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1490 					drv, NULL);
1491 	}
1492 }
1493 
1494 
wpa_driver_ndis_poll(void * priv)1495 static void wpa_driver_ndis_poll(void *priv)
1496 {
1497 	struct wpa_driver_ndis_data *drv = priv;
1498 	eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
1499 	wpa_driver_ndis_poll_timeout(drv, NULL);
1500 }
1501 
1502 
1503 /* Called when driver generates Media Connect Event by calling
1504  * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_CONNECT */
wpa_driver_ndis_event_connect(struct wpa_driver_ndis_data * drv)1505 void wpa_driver_ndis_event_connect(struct wpa_driver_ndis_data *drv)
1506 {
1507 	wpa_printf(MSG_DEBUG, "NDIS: Media Connect Event");
1508 	if (wpa_driver_ndis_get_bssid(drv, drv->bssid) == 0) {
1509 		wpa_driver_ndis_get_associnfo(drv);
1510 		wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1511 	}
1512 }
1513 
1514 
1515 /* Called when driver generates Media Disconnect Event by calling
1516  * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_DISCONNECT */
wpa_driver_ndis_event_disconnect(struct wpa_driver_ndis_data * drv)1517 void wpa_driver_ndis_event_disconnect(struct wpa_driver_ndis_data *drv)
1518 {
1519 	wpa_printf(MSG_DEBUG, "NDIS: Media Disconnect Event");
1520 	os_memset(drv->bssid, 0, ETH_ALEN);
1521 	wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1522 }
1523 
1524 
wpa_driver_ndis_event_auth(struct wpa_driver_ndis_data * drv,const u8 * data,size_t data_len)1525 static void wpa_driver_ndis_event_auth(struct wpa_driver_ndis_data *drv,
1526 				       const u8 *data, size_t data_len)
1527 {
1528 	NDIS_802_11_AUTHENTICATION_REQUEST *req;
1529 	int pairwise = 0, group = 0;
1530 	union wpa_event_data event;
1531 
1532 	if (data_len < sizeof(*req)) {
1533 		wpa_printf(MSG_DEBUG, "NDIS: Too short Authentication Request "
1534 			   "Event (len=%lu)", data_len);
1535 		return;
1536 	}
1537 	req = (NDIS_802_11_AUTHENTICATION_REQUEST *) data;
1538 
1539 	wpa_printf(MSG_DEBUG, "NDIS: Authentication Request Event: "
1540 		   "Bssid " MACSTR " Flags 0x%x",
1541 		   MAC2STR(req->Bssid), (int) req->Flags);
1542 
1543 	if ((req->Flags & NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR) ==
1544 	    NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR)
1545 		pairwise = 1;
1546 	else if ((req->Flags & NDIS_802_11_AUTH_REQUEST_GROUP_ERROR) ==
1547 	    NDIS_802_11_AUTH_REQUEST_GROUP_ERROR)
1548 		group = 1;
1549 
1550 	if (pairwise || group) {
1551 		os_memset(&event, 0, sizeof(event));
1552 		event.michael_mic_failure.unicast = pairwise;
1553 		wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE,
1554 				     &event);
1555 	}
1556 }
1557 
1558 
wpa_driver_ndis_event_pmkid(struct wpa_driver_ndis_data * drv,const u8 * data,size_t data_len)1559 static void wpa_driver_ndis_event_pmkid(struct wpa_driver_ndis_data *drv,
1560 					const u8 *data, size_t data_len)
1561 {
1562 	NDIS_802_11_PMKID_CANDIDATE_LIST *pmkid;
1563 	size_t i;
1564 	union wpa_event_data event;
1565 
1566 	if (data_len < 8) {
1567 		wpa_printf(MSG_DEBUG, "NDIS: Too short PMKID Candidate List "
1568 			   "Event (len=%lu)", data_len);
1569 		return;
1570 	}
1571 	pmkid = (NDIS_802_11_PMKID_CANDIDATE_LIST *) data;
1572 	wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List Event - Version %d "
1573 		   "NumCandidates %d",
1574 		   (int) pmkid->Version, (int) pmkid->NumCandidates);
1575 
1576 	if (pmkid->Version != 1) {
1577 		wpa_printf(MSG_DEBUG, "NDIS: Unsupported PMKID Candidate List "
1578 			   "Version %d", (int) pmkid->Version);
1579 		return;
1580 	}
1581 
1582 	if (data_len < 8 + pmkid->NumCandidates * sizeof(PMKID_CANDIDATE)) {
1583 		wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List underflow");
1584 		return;
1585 	}
1586 
1587 	os_memset(&event, 0, sizeof(event));
1588 	for (i = 0; i < pmkid->NumCandidates; i++) {
1589 		PMKID_CANDIDATE *p = &pmkid->CandidateList[i];
1590 		wpa_printf(MSG_DEBUG, "NDIS: %lu: " MACSTR " Flags 0x%x",
1591 			   i, MAC2STR(p->BSSID), (int) p->Flags);
1592 		os_memcpy(event.pmkid_candidate.bssid, p->BSSID, ETH_ALEN);
1593 		event.pmkid_candidate.index = i;
1594 		event.pmkid_candidate.preauth =
1595 			p->Flags & NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
1596 		wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE,
1597 				     &event);
1598 	}
1599 }
1600 
1601 
1602 /* Called when driver calls NdisMIndicateStatus() with
1603  * NDIS_STATUS_MEDIA_SPECIFIC_INDICATION */
wpa_driver_ndis_event_media_specific(struct wpa_driver_ndis_data * drv,const u8 * data,size_t data_len)1604 void wpa_driver_ndis_event_media_specific(struct wpa_driver_ndis_data *drv,
1605 					  const u8 *data, size_t data_len)
1606 {
1607 	NDIS_802_11_STATUS_INDICATION *status;
1608 
1609 	if (data == NULL || data_len < sizeof(*status))
1610 		return;
1611 
1612 	wpa_hexdump(MSG_DEBUG, "NDIS: Media Specific Indication",
1613 		    data, data_len);
1614 
1615 	status = (NDIS_802_11_STATUS_INDICATION *) data;
1616 	data += sizeof(status);
1617 	data_len -= sizeof(status);
1618 
1619 	switch (status->StatusType) {
1620 	case Ndis802_11StatusType_Authentication:
1621 		wpa_driver_ndis_event_auth(drv, data, data_len);
1622 		break;
1623 	case Ndis802_11StatusType_PMKID_CandidateList:
1624 		wpa_driver_ndis_event_pmkid(drv, data, data_len);
1625 		break;
1626 	default:
1627 		wpa_printf(MSG_DEBUG, "NDIS: Unknown StatusType %d",
1628 			   (int) status->StatusType);
1629 		break;
1630 	}
1631 }
1632 
1633 
1634 /* Called when an adapter is added */
wpa_driver_ndis_event_adapter_arrival(struct wpa_driver_ndis_data * drv)1635 void wpa_driver_ndis_event_adapter_arrival(struct wpa_driver_ndis_data *drv)
1636 {
1637 	union wpa_event_data event;
1638 	int i;
1639 
1640 	wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Arrival");
1641 
1642 	for (i = 0; i < 30; i++) {
1643 		/* Re-open Packet32/NDISUIO connection */
1644 		wpa_driver_ndis_adapter_close(drv);
1645 		if (wpa_driver_ndis_adapter_init(drv) < 0 ||
1646 		    wpa_driver_ndis_adapter_open(drv) < 0) {
1647 			wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialization "
1648 				   "(%d) failed", i);
1649 			os_sleep(1, 0);
1650 		} else {
1651 			wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialized");
1652 			break;
1653 		}
1654 	}
1655 
1656 	os_memset(&event, 0, sizeof(event));
1657 	os_strlcpy(event.interface_status.ifname, drv->ifname,
1658 		   sizeof(event.interface_status.ifname));
1659 	event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1660 	wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1661 }
1662 
1663 
1664 /* Called when an adapter is removed */
wpa_driver_ndis_event_adapter_removal(struct wpa_driver_ndis_data * drv)1665 void wpa_driver_ndis_event_adapter_removal(struct wpa_driver_ndis_data *drv)
1666 {
1667 	union wpa_event_data event;
1668 
1669 	wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Removal");
1670 	os_memset(&event, 0, sizeof(event));
1671 	os_strlcpy(event.interface_status.ifname, drv->ifname,
1672 		   sizeof(event.interface_status.ifname));
1673 	event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
1674 	wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1675 }
1676 
1677 
1678 static void
wpa_driver_ndis_get_wpa_capability(struct wpa_driver_ndis_data * drv)1679 wpa_driver_ndis_get_wpa_capability(struct wpa_driver_ndis_data *drv)
1680 {
1681 	wpa_printf(MSG_DEBUG, "NDIS: verifying driver WPA capability");
1682 
1683 	if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA) == 0 &&
1684 	    ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPA) {
1685 		wpa_printf(MSG_DEBUG, "NDIS: WPA key management supported");
1686 		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1687 	}
1688 
1689 	if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPAPSK) == 0 &&
1690 	    ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPAPSK) {
1691 		wpa_printf(MSG_DEBUG, "NDIS: WPA-PSK key management "
1692 			   "supported");
1693 		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1694 	}
1695 
1696 	if (ndis_set_encr_status(drv, Ndis802_11Encryption3Enabled) == 0 &&
1697 	    ndis_get_encr_status(drv) == Ndis802_11Encryption3KeyAbsent) {
1698 		wpa_printf(MSG_DEBUG, "NDIS: CCMP encryption supported");
1699 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1700 	}
1701 
1702 	if (ndis_set_encr_status(drv, Ndis802_11Encryption2Enabled) == 0 &&
1703 	    ndis_get_encr_status(drv) == Ndis802_11Encryption2KeyAbsent) {
1704 		wpa_printf(MSG_DEBUG, "NDIS: TKIP encryption supported");
1705 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1706 	}
1707 
1708 	if (ndis_set_encr_status(drv, Ndis802_11Encryption1Enabled) == 0 &&
1709 	    ndis_get_encr_status(drv) == Ndis802_11Encryption1KeyAbsent) {
1710 		wpa_printf(MSG_DEBUG, "NDIS: WEP encryption supported");
1711 		drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1712 			WPA_DRIVER_CAPA_ENC_WEP104;
1713 	}
1714 
1715 	if (ndis_set_auth_mode(drv, Ndis802_11AuthModeShared) == 0 &&
1716 	    ndis_get_auth_mode(drv) == Ndis802_11AuthModeShared) {
1717 		drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1718 	}
1719 
1720 	if (ndis_set_auth_mode(drv, Ndis802_11AuthModeOpen) == 0 &&
1721 	    ndis_get_auth_mode(drv) == Ndis802_11AuthModeOpen) {
1722 		drv->capa.auth |= WPA_DRIVER_AUTH_OPEN;
1723 	}
1724 
1725 	ndis_set_encr_status(drv, Ndis802_11EncryptionDisabled);
1726 
1727 	/* Could also verify OID_802_11_ADD_KEY error reporting and
1728 	 * support for OID_802_11_ASSOCIATION_INFORMATION. */
1729 
1730 	if (drv->capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA &&
1731 	    drv->capa.enc & (WPA_DRIVER_CAPA_ENC_TKIP |
1732 			     WPA_DRIVER_CAPA_ENC_CCMP)) {
1733 		wpa_printf(MSG_DEBUG, "NDIS: driver supports WPA");
1734 		drv->has_capability = 1;
1735 	} else {
1736 		wpa_printf(MSG_DEBUG, "NDIS: no WPA support found");
1737 	}
1738 
1739 	wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1740 		   "enc 0x%x auth 0x%x",
1741 		   drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1742 }
1743 
1744 
wpa_driver_ndis_get_capability(struct wpa_driver_ndis_data * drv)1745 static void wpa_driver_ndis_get_capability(struct wpa_driver_ndis_data *drv)
1746 {
1747 	char buf[512];
1748 	int len;
1749 	size_t i;
1750 	NDIS_802_11_CAPABILITY *c;
1751 
1752 	drv->capa.flags = WPA_DRIVER_FLAGS_DRIVER_IE;
1753 
1754 	len = ndis_get_oid(drv, OID_802_11_CAPABILITY, buf, sizeof(buf));
1755 	if (len < 0) {
1756 		wpa_driver_ndis_get_wpa_capability(drv);
1757 		return;
1758 	}
1759 
1760 	wpa_hexdump(MSG_MSGDUMP, "OID_802_11_CAPABILITY", (u8 *) buf, len);
1761 	c = (NDIS_802_11_CAPABILITY *) buf;
1762 	if (len < sizeof(*c) || c->Version != 2) {
1763 		wpa_printf(MSG_DEBUG, "NDIS: unsupported "
1764 			   "OID_802_11_CAPABILITY data");
1765 		return;
1766 	}
1767 	wpa_printf(MSG_DEBUG, "NDIS: Driver supports OID_802_11_CAPABILITY - "
1768 		   "NoOfPMKIDs %d NoOfAuthEncrPairs %d",
1769 		   (int) c->NoOfPMKIDs,
1770 		   (int) c->NoOfAuthEncryptPairsSupported);
1771 	drv->has_capability = 1;
1772 	drv->no_of_pmkid = c->NoOfPMKIDs;
1773 	for (i = 0; i < c->NoOfAuthEncryptPairsSupported; i++) {
1774 		NDIS_802_11_AUTHENTICATION_ENCRYPTION *ae;
1775 		ae = &c->AuthenticationEncryptionSupported[i];
1776 		if ((char *) (ae + 1) > buf + len) {
1777 			wpa_printf(MSG_DEBUG, "NDIS: auth/encr pair list "
1778 				   "overflow");
1779 			break;
1780 		}
1781 		wpa_printf(MSG_MSGDUMP, "NDIS: %lu - auth %d encr %d",
1782 			   i, (int) ae->AuthModeSupported,
1783 			   (int) ae->EncryptStatusSupported);
1784 		switch (ae->AuthModeSupported) {
1785 		case Ndis802_11AuthModeOpen:
1786 			drv->capa.auth |= WPA_DRIVER_AUTH_OPEN;
1787 			break;
1788 		case Ndis802_11AuthModeShared:
1789 			drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1790 			break;
1791 		case Ndis802_11AuthModeWPA:
1792 			drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1793 			break;
1794 		case Ndis802_11AuthModeWPAPSK:
1795 			drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1796 			break;
1797 		case Ndis802_11AuthModeWPA2:
1798 			drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2;
1799 			break;
1800 		case Ndis802_11AuthModeWPA2PSK:
1801 			drv->capa.key_mgmt |=
1802 				WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1803 			break;
1804 		case Ndis802_11AuthModeWPANone:
1805 			drv->capa.key_mgmt |=
1806 				WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE;
1807 			break;
1808 		default:
1809 			break;
1810 		}
1811 		switch (ae->EncryptStatusSupported) {
1812 		case Ndis802_11Encryption1Enabled:
1813 			drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40;
1814 			drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP104;
1815 			break;
1816 		case Ndis802_11Encryption2Enabled:
1817 			drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1818 			break;
1819 		case Ndis802_11Encryption3Enabled:
1820 			drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1821 			break;
1822 		default:
1823 			break;
1824 		}
1825 	}
1826 
1827 	wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1828 		   "enc 0x%x auth 0x%x",
1829 		   drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1830 }
1831 
1832 
wpa_driver_ndis_get_capa(void * priv,struct wpa_driver_capa * capa)1833 static int wpa_driver_ndis_get_capa(void *priv, struct wpa_driver_capa *capa)
1834 {
1835 	struct wpa_driver_ndis_data *drv = priv;
1836 	if (!drv->has_capability)
1837 		return -1;
1838 	os_memcpy(capa, &drv->capa, sizeof(*capa));
1839 	return 0;
1840 }
1841 
1842 
wpa_driver_ndis_get_ifname(void * priv)1843 static const char * wpa_driver_ndis_get_ifname(void *priv)
1844 {
1845 	struct wpa_driver_ndis_data *drv = priv;
1846 	return drv->ifname;
1847 }
1848 
1849 
wpa_driver_ndis_get_mac_addr(void * priv)1850 static const u8 * wpa_driver_ndis_get_mac_addr(void *priv)
1851 {
1852 	struct wpa_driver_ndis_data *drv = priv;
1853 	return drv->own_addr;
1854 }
1855 
1856 
1857 #ifdef _WIN32_WCE
1858 
1859 #define NDISUIO_MSG_SIZE (sizeof(NDISUIO_DEVICE_NOTIFICATION) + 512)
1860 
ndisuio_notification_receive(void * eloop_data,void * user_ctx)1861 static void ndisuio_notification_receive(void *eloop_data, void *user_ctx)
1862 {
1863 	struct wpa_driver_ndis_data *drv = eloop_data;
1864 	NDISUIO_DEVICE_NOTIFICATION *hdr;
1865 	u8 buf[NDISUIO_MSG_SIZE];
1866 	DWORD len, flags;
1867 
1868 	if (!ReadMsgQueue(drv->event_queue, buf, NDISUIO_MSG_SIZE, &len, 0,
1869 			  &flags)) {
1870 		wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1871 			   "ReadMsgQueue failed: %d", (int) GetLastError());
1872 		return;
1873 	}
1874 
1875 	if (len < sizeof(NDISUIO_DEVICE_NOTIFICATION)) {
1876 		wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1877 			   "Too short message (len=%d)", (int) len);
1878 		return;
1879 	}
1880 
1881 	hdr = (NDISUIO_DEVICE_NOTIFICATION *) buf;
1882 	wpa_printf(MSG_DEBUG, "NDIS: Notification received: len=%d type=0x%x",
1883 		   (int) len, hdr->dwNotificationType);
1884 
1885 	switch (hdr->dwNotificationType) {
1886 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL
1887 	case NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL:
1888 		wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_ARRIVAL");
1889 		wpa_driver_ndis_event_adapter_arrival(drv);
1890 		break;
1891 #endif
1892 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
1893 	case NDISUIO_NOTIFICATION_ADAPTER_REMOVAL:
1894 		wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_REMOVAL");
1895 		wpa_driver_ndis_event_adapter_removal(drv);
1896 		break;
1897 #endif
1898 	case NDISUIO_NOTIFICATION_MEDIA_CONNECT:
1899 		wpa_printf(MSG_DEBUG, "NDIS: MEDIA_CONNECT");
1900 		SetEvent(drv->connected_event);
1901 		wpa_driver_ndis_event_connect(drv);
1902 		break;
1903 	case NDISUIO_NOTIFICATION_MEDIA_DISCONNECT:
1904 		ResetEvent(drv->connected_event);
1905 		wpa_printf(MSG_DEBUG, "NDIS: MEDIA_DISCONNECT");
1906 		wpa_driver_ndis_event_disconnect(drv);
1907 		break;
1908 	case NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION:
1909 		wpa_printf(MSG_DEBUG, "NDIS: MEDIA_SPECIFIC_NOTIFICATION");
1910 #if _WIN32_WCE == 420 || _WIN32_WCE == 0x420
1911 		wpa_driver_ndis_event_media_specific(
1912 			drv, hdr->pvStatusBuffer, hdr->uiStatusBufferSize);
1913 #else
1914 		wpa_driver_ndis_event_media_specific(
1915 			drv, ((const u8 *) hdr) + hdr->uiOffsetToStatusBuffer,
1916 			(size_t) hdr->uiStatusBufferSize);
1917 #endif
1918 		break;
1919 	default:
1920 		wpa_printf(MSG_DEBUG, "NDIS: Unknown notification type 0x%x",
1921 			   hdr->dwNotificationType);
1922 		break;
1923 	}
1924 }
1925 
1926 
ndisuio_notification_deinit(struct wpa_driver_ndis_data * drv)1927 static void ndisuio_notification_deinit(struct wpa_driver_ndis_data *drv)
1928 {
1929 	NDISUIO_REQUEST_NOTIFICATION req;
1930 
1931 	memset(&req, 0, sizeof(req));
1932 	req.hMsgQueue = drv->event_queue;
1933 	req.dwNotificationTypes = 0;
1934 
1935 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION,
1936 			     &req, sizeof(req), NULL, 0, NULL, NULL)) {
1937 		wpa_printf(MSG_INFO, "ndisuio_notification_deinit: "
1938 			   "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d",
1939 			   (int) GetLastError());
1940 	}
1941 
1942 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_CANCEL_NOTIFICATION,
1943 			     NULL, 0, NULL, 0, NULL, NULL)) {
1944 		wpa_printf(MSG_INFO, "ndisuio_notification_deinit: "
1945 			   "IOCTL_NDISUIO_CANCEL_NOTIFICATION failed: %d",
1946 			   (int) GetLastError());
1947 	}
1948 
1949 	if (drv->event_queue) {
1950 		eloop_unregister_event(drv->event_queue,
1951 				       sizeof(drv->event_queue));
1952 		CloseHandle(drv->event_queue);
1953 		drv->event_queue = NULL;
1954 	}
1955 
1956 	if (drv->connected_event) {
1957 		CloseHandle(drv->connected_event);
1958 		drv->connected_event = NULL;
1959 	}
1960 }
1961 
1962 
ndisuio_notification_init(struct wpa_driver_ndis_data * drv)1963 static int ndisuio_notification_init(struct wpa_driver_ndis_data *drv)
1964 {
1965 	MSGQUEUEOPTIONS opt;
1966 	NDISUIO_REQUEST_NOTIFICATION req;
1967 
1968 	drv->connected_event =
1969 		CreateEvent(NULL, TRUE, FALSE, TEXT("WpaSupplicantConnected"));
1970 	if (drv->connected_event == NULL) {
1971 		wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1972 			   "CreateEvent failed: %d",
1973 			   (int) GetLastError());
1974 		return -1;
1975 	}
1976 
1977 	memset(&opt, 0, sizeof(opt));
1978 	opt.dwSize = sizeof(opt);
1979 	opt.dwMaxMessages = 5;
1980 	opt.cbMaxMessage = NDISUIO_MSG_SIZE;
1981 	opt.bReadAccess = TRUE;
1982 
1983 	drv->event_queue = CreateMsgQueue(NULL, &opt);
1984 	if (drv->event_queue == NULL) {
1985 		wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1986 			   "CreateMsgQueue failed: %d",
1987 			   (int) GetLastError());
1988 		ndisuio_notification_deinit(drv);
1989 		return -1;
1990 	}
1991 
1992 	memset(&req, 0, sizeof(req));
1993 	req.hMsgQueue = drv->event_queue;
1994 	req.dwNotificationTypes =
1995 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL
1996 		NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL |
1997 #endif
1998 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
1999 		NDISUIO_NOTIFICATION_ADAPTER_REMOVAL |
2000 #endif
2001 		NDISUIO_NOTIFICATION_MEDIA_CONNECT |
2002 		NDISUIO_NOTIFICATION_MEDIA_DISCONNECT |
2003 		NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION;
2004 
2005 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION,
2006 			     &req, sizeof(req), NULL, 0, NULL, NULL)) {
2007 		wpa_printf(MSG_INFO, "ndisuio_notification_init: "
2008 			   "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d",
2009 			   (int) GetLastError());
2010 		ndisuio_notification_deinit(drv);
2011 		return -1;
2012 	}
2013 
2014 	eloop_register_event(drv->event_queue, sizeof(drv->event_queue),
2015 			     ndisuio_notification_receive, drv, NULL);
2016 
2017 	return 0;
2018 }
2019 #endif /* _WIN32_WCE */
2020 
2021 
wpa_driver_ndis_get_names(struct wpa_driver_ndis_data * drv)2022 static int wpa_driver_ndis_get_names(struct wpa_driver_ndis_data *drv)
2023 {
2024 #ifdef CONFIG_USE_NDISUIO
2025 	NDISUIO_QUERY_BINDING *b;
2026 	size_t blen = sizeof(*b) + 1024;
2027 	int i, error, found = 0;
2028 	DWORD written;
2029 	char name[256], desc[256], *dpos;
2030 	WCHAR *pos;
2031 	size_t j, len, dlen;
2032 
2033 	b = os_malloc(blen);
2034 	if (b == NULL)
2035 		return -1;
2036 
2037 	for (i = 0; ; i++) {
2038 		os_memset(b, 0, blen);
2039 		b->BindingIndex = i;
2040 		if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
2041 				     b, sizeof(NDISUIO_QUERY_BINDING), b, blen,
2042 				     &written, NULL)) {
2043 			error = (int) GetLastError();
2044 			if (error == ERROR_NO_MORE_ITEMS)
2045 				break;
2046 			wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
2047 				   "failed: %d", error);
2048 			break;
2049 		}
2050 
2051 		pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
2052 		len = b->DeviceNameLength;
2053 		if (len >= sizeof(name))
2054 			len = sizeof(name) - 1;
2055 		for (j = 0; j < len; j++)
2056 			name[j] = (char) pos[j];
2057 		name[len] = '\0';
2058 
2059 		pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
2060 		len = b->DeviceDescrLength;
2061 		if (len >= sizeof(desc))
2062 			len = sizeof(desc) - 1;
2063 		for (j = 0; j < len; j++)
2064 			desc[j] = (char) pos[j];
2065 		desc[len] = '\0';
2066 
2067 		wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
2068 
2069 		if (os_strstr(name, drv->ifname)) {
2070 			wpa_printf(MSG_DEBUG, "NDIS: Interface name match");
2071 			found = 1;
2072 			break;
2073 		}
2074 
2075 		if (os_strncmp(desc, drv->ifname, os_strlen(drv->ifname)) == 0)
2076 		{
2077 			wpa_printf(MSG_DEBUG, "NDIS: Interface description "
2078 				   "match");
2079 			found = 1;
2080 			break;
2081 		}
2082 	}
2083 
2084 	if (!found) {
2085 		wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
2086 			   drv->ifname);
2087 		os_free(b);
2088 		return -1;
2089 	}
2090 
2091 	os_strlcpy(drv->ifname,
2092 		   os_strncmp(name, "\\DEVICE\\", 8) == 0 ? name + 8 : name,
2093 		   sizeof(drv->ifname));
2094 #ifdef _WIN32_WCE
2095 	drv->adapter_name = wpa_strdup_tchar(drv->ifname);
2096 	if (drv->adapter_name == NULL) {
2097 		wpa_printf(MSG_ERROR, "NDIS: Failed to allocate memory for "
2098 			   "adapter name");
2099 		os_free(b);
2100 		return -1;
2101 	}
2102 #endif /* _WIN32_WCE */
2103 
2104 	dpos = os_strstr(desc, " - ");
2105 	if (dpos)
2106 		dlen = dpos - desc;
2107 	else
2108 		dlen = os_strlen(desc);
2109 	drv->adapter_desc = os_malloc(dlen + 1);
2110 	if (drv->adapter_desc) {
2111 		os_memcpy(drv->adapter_desc, desc, dlen);
2112 		drv->adapter_desc[dlen] = '\0';
2113 	}
2114 	os_free(b);
2115 	if (drv->adapter_desc == NULL)
2116 		return -1;
2117 
2118 	wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2119 		   drv->adapter_desc);
2120 
2121 	return 0;
2122 #else /* CONFIG_USE_NDISUIO */
2123 	PTSTR _names;
2124 	char *names, *pos, *pos2;
2125 	ULONG len;
2126 	BOOLEAN res;
2127 #define MAX_ADAPTERS 32
2128 	char *name[MAX_ADAPTERS];
2129 	char *desc[MAX_ADAPTERS];
2130 	int num_name, num_desc, i, found_name, found_desc;
2131 	size_t dlen;
2132 
2133 	wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
2134 		   PacketGetVersion());
2135 
2136 	len = 8192;
2137 	_names = os_zalloc(len);
2138 	if (_names == NULL)
2139 		return -1;
2140 
2141 	res = PacketGetAdapterNames(_names, &len);
2142 	if (!res && len > 8192) {
2143 		os_free(_names);
2144 		_names = os_zalloc(len);
2145 		if (_names == NULL)
2146 			return -1;
2147 		res = PacketGetAdapterNames(_names, &len);
2148 	}
2149 
2150 	if (!res) {
2151 		wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
2152 			   "(PacketGetAdapterNames)");
2153 		os_free(_names);
2154 		return -1;
2155 	}
2156 
2157 	names = (char *) _names;
2158 	if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') {
2159 		wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in "
2160 			   "UNICODE");
2161 		/* Convert to ASCII */
2162 		pos2 = pos = names;
2163 		while (pos2 < names + len) {
2164 			if (pos2[0] == '\0' && pos2[1] == '\0' &&
2165 			    pos2[2] == '\0' && pos2[3] == '\0') {
2166 				pos2 += 4;
2167 				break;
2168 			}
2169 			*pos++ = pos2[0];
2170 			pos2 += 2;
2171 		}
2172 		os_memcpy(pos + 2, names, pos - names);
2173 		pos += 2;
2174 	} else
2175 		pos = names;
2176 
2177 	num_name = 0;
2178 	while (pos < names + len) {
2179 		name[num_name] = pos;
2180 		while (*pos && pos < names + len)
2181 			pos++;
2182 		if (pos + 1 >= names + len) {
2183 			os_free(names);
2184 			return -1;
2185 		}
2186 		pos++;
2187 		num_name++;
2188 		if (num_name >= MAX_ADAPTERS) {
2189 			wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
2190 			os_free(names);
2191 			return -1;
2192 		}
2193 		if (*pos == '\0') {
2194 			wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
2195 				   num_name);
2196 			pos++;
2197 			break;
2198 		}
2199 	}
2200 
2201 	num_desc = 0;
2202 	while (pos < names + len) {
2203 		desc[num_desc] = pos;
2204 		while (*pos && pos < names + len)
2205 			pos++;
2206 		if (pos + 1 >= names + len) {
2207 			os_free(names);
2208 			return -1;
2209 		}
2210 		pos++;
2211 		num_desc++;
2212 		if (num_desc >= MAX_ADAPTERS) {
2213 			wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
2214 				   "descriptions");
2215 			os_free(names);
2216 			return -1;
2217 		}
2218 		if (*pos == '\0') {
2219 			wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
2220 				   "found", num_name);
2221 			pos++;
2222 			break;
2223 		}
2224 	}
2225 
2226 	/*
2227 	 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
2228 	 * descriptions. Fill in dummy descriptors to work around this.
2229 	 */
2230 	while (num_desc < num_name)
2231 		desc[num_desc++] = "dummy description";
2232 
2233 	if (num_name != num_desc) {
2234 		wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and "
2235 			   "description counts (%d != %d)",
2236 			   num_name, num_desc);
2237 		os_free(names);
2238 		return -1;
2239 	}
2240 
2241 	found_name = found_desc = -1;
2242 	for (i = 0; i < num_name; i++) {
2243 		wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s",
2244 			   i, name[i], desc[i]);
2245 		if (found_name == -1 && os_strstr(name[i], drv->ifname))
2246 			found_name = i;
2247 		if (found_desc == -1 &&
2248 		    os_strncmp(desc[i], drv->ifname, os_strlen(drv->ifname)) ==
2249 		    0)
2250 			found_desc = i;
2251 	}
2252 
2253 	if (found_name < 0 && found_desc >= 0) {
2254 		wpa_printf(MSG_DEBUG, "NDIS: Matched interface '%s' based on "
2255 			   "description '%s'",
2256 			   name[found_desc], desc[found_desc]);
2257 		found_name = found_desc;
2258 		os_strlcpy(drv->ifname,
2259 			   os_strncmp(name[found_desc], "\\Device\\NPF_", 12)
2260 			   == 0 ? name[found_desc] + 12 : name[found_desc],
2261 			   sizeof(drv->ifname));
2262 	}
2263 
2264 	if (found_name < 0) {
2265 		wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
2266 			   drv->ifname);
2267 		os_free(names);
2268 		return -1;
2269 	}
2270 
2271 	i = found_name;
2272 	pos = os_strrchr(desc[i], '(');
2273 	if (pos) {
2274 		dlen = pos - desc[i];
2275 		pos--;
2276 		if (pos > desc[i] && *pos == ' ')
2277 			dlen--;
2278 	} else {
2279 		dlen = os_strlen(desc[i]);
2280 	}
2281 	drv->adapter_desc = os_malloc(dlen + 1);
2282 	if (drv->adapter_desc) {
2283 		os_memcpy(drv->adapter_desc, desc[i], dlen);
2284 		drv->adapter_desc[dlen] = '\0';
2285 	}
2286 	os_free(names);
2287 	if (drv->adapter_desc == NULL)
2288 		return -1;
2289 
2290 	wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2291 		   drv->adapter_desc);
2292 
2293 	return 0;
2294 #endif /* CONFIG_USE_NDISUIO */
2295 }
2296 
2297 
2298 #if defined(CONFIG_NATIVE_WINDOWS) || defined(__CYGWIN__)
2299 #ifndef _WIN32_WCE
2300 /*
2301  * These structures are undocumented for WinXP; only WinCE version is
2302  * documented. These would be included wzcsapi.h if it were available. Some
2303  * changes here have been needed to make the structures match with WinXP SP2.
2304  * It is unclear whether these work with any other version.
2305  */
2306 
2307 typedef struct {
2308 	LPWSTR wszGuid;
2309 } INTF_KEY_ENTRY, *PINTF_KEY_ENTRY;
2310 
2311 typedef struct {
2312 	DWORD dwNumIntfs;
2313 	PINTF_KEY_ENTRY pIntfs;
2314 } INTFS_KEY_TABLE, *PINTFS_KEY_TABLE;
2315 
2316 typedef struct {
2317 	DWORD dwDataLen;
2318 	LPBYTE pData;
2319 } RAW_DATA, *PRAW_DATA;
2320 
2321 typedef struct {
2322 	LPWSTR wszGuid;
2323 	LPWSTR wszDescr;
2324 	ULONG ulMediaState;
2325 	ULONG ulMediaType;
2326 	ULONG ulPhysicalMediaType;
2327 	INT nInfraMode;
2328 	INT nAuthMode;
2329 	INT nWepStatus;
2330 #ifndef _WIN32_WCE
2331 	u8 pad[2]; /* why is this needed? */
2332 #endif /* _WIN32_WCE */
2333 	DWORD dwCtlFlags;
2334 	DWORD dwCapabilities; /* something added for WinXP SP2(?) */
2335 	RAW_DATA rdSSID;
2336 	RAW_DATA rdBSSID;
2337 	RAW_DATA rdBSSIDList;
2338 	RAW_DATA rdStSSIDList;
2339 	RAW_DATA rdCtrlData;
2340 #ifdef UNDER_CE
2341 	BOOL bInitialized;
2342 #endif
2343 	DWORD nWPAMCastCipher;
2344 	/* add some extra buffer for later additions since this interface is
2345 	 * far from stable */
2346 	u8 later_additions[100];
2347 } INTF_ENTRY, *PINTF_ENTRY;
2348 
2349 #define INTF_ALL 0xffffffff
2350 #define INTF_ALL_FLAGS 0x0000ffff
2351 #define INTF_CTLFLAGS 0x00000010
2352 #define INTFCTL_ENABLED 0x8000
2353 #endif /* _WIN32_WCE */
2354 
2355 
2356 #ifdef _WIN32_WCE
wpa_driver_ndis_rebind_adapter(struct wpa_driver_ndis_data * drv)2357 static int wpa_driver_ndis_rebind_adapter(struct wpa_driver_ndis_data *drv)
2358 {
2359 	HANDLE ndis;
2360 	TCHAR multi[100];
2361 	int len;
2362 
2363 	len = _tcslen(drv->adapter_name);
2364 	if (len > 80)
2365 		return -1;
2366 
2367 	ndis = CreateFile(DD_NDIS_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE,
2368 			  0, NULL, OPEN_EXISTING, 0, NULL);
2369 	if (ndis == INVALID_HANDLE_VALUE) {
2370 		wpa_printf(MSG_DEBUG, "NDIS: Failed to open file to NDIS "
2371 			   "device: %d", (int) GetLastError());
2372 		return -1;
2373 	}
2374 
2375 	len++;
2376 	memcpy(multi, drv->adapter_name, len * sizeof(TCHAR));
2377 	memcpy(&multi[len], TEXT("NDISUIO\0"), 9 * sizeof(TCHAR));
2378 	len += 9;
2379 
2380 	if (!DeviceIoControl(ndis, IOCTL_NDIS_REBIND_ADAPTER,
2381 			     multi, len * sizeof(TCHAR), NULL, 0, NULL, NULL))
2382 	{
2383 		wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDIS_REBIND_ADAPTER "
2384 			   "failed: 0x%x", (int) GetLastError());
2385 		wpa_hexdump_ascii(MSG_DEBUG, "NDIS: rebind multi_sz",
2386 				  (u8 *) multi, len * sizeof(TCHAR));
2387 		CloseHandle(ndis);
2388 		return -1;
2389 	}
2390 
2391 	CloseHandle(ndis);
2392 
2393 	wpa_printf(MSG_DEBUG, "NDIS: Requested NDIS rebind of NDISUIO "
2394 		   "protocol");
2395 
2396 	return 0;
2397 }
2398 #endif /* _WIN32_WCE */
2399 
2400 
wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data * drv,int enable)2401 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2402 				   int enable)
2403 {
2404 #ifdef _WIN32_WCE
2405 	HKEY hk, hk2;
2406 	LONG ret;
2407 	DWORD i, hnd, len;
2408 	TCHAR keyname[256], devname[256];
2409 
2410 #define WZC_DRIVER TEXT("Drivers\\BuiltIn\\ZeroConfig")
2411 
2412 	if (enable) {
2413 		HANDLE h;
2414 		h = ActivateDeviceEx(WZC_DRIVER, NULL, 0, NULL);
2415 		if (h == INVALID_HANDLE_VALUE || h == 0) {
2416 			wpa_printf(MSG_DEBUG, "NDIS: Failed to re-enable WZC "
2417 				   "- ActivateDeviceEx failed: %d",
2418 				   (int) GetLastError());
2419 			return -1;
2420 		}
2421 
2422 		wpa_printf(MSG_DEBUG, "NDIS: WZC re-enabled");
2423 		return wpa_driver_ndis_rebind_adapter(drv);
2424 	}
2425 
2426 	/*
2427 	 * Unfortunately, just disabling the WZC for an interface is not enough
2428 	 * to free NDISUIO for us, so need to disable and unload WZC completely
2429 	 * for now when using WinCE with NDISUIO. In addition, must request
2430 	 * NDISUIO protocol to be rebound to the adapter in order to free the
2431 	 * NDISUIO binding that WZC hold before us.
2432 	 */
2433 
2434 	/* Enumerate HKLM\Drivers\Active\* to find a handle to WZC. */
2435 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, DEVLOAD_ACTIVE_KEY, 0, 0, &hk);
2436 	if (ret != ERROR_SUCCESS) {
2437 		wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(DEVLOAD_ACTIVE_KEY) "
2438 			   "failed: %d %d", (int) ret, (int) GetLastError());
2439 		return -1;
2440 	}
2441 
2442 	for (i = 0; ; i++) {
2443 		len = sizeof(keyname);
2444 		ret = RegEnumKeyEx(hk, i, keyname, &len, NULL, NULL, NULL,
2445 				   NULL);
2446 		if (ret != ERROR_SUCCESS) {
2447 			wpa_printf(MSG_DEBUG, "NDIS: Could not find active "
2448 				   "WZC - assuming it is not running.");
2449 			RegCloseKey(hk);
2450 			return -1;
2451 		}
2452 
2453 		ret = RegOpenKeyEx(hk, keyname, 0, 0, &hk2);
2454 		if (ret != ERROR_SUCCESS) {
2455 			wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(active dev) "
2456 				   "failed: %d %d",
2457 				   (int) ret, (int) GetLastError());
2458 			continue;
2459 		}
2460 
2461 		len = sizeof(devname);
2462 		ret = RegQueryValueEx(hk2, DEVLOAD_DEVKEY_VALNAME, NULL, NULL,
2463 				      (LPBYTE) devname, &len);
2464 		if (ret != ERROR_SUCCESS) {
2465 			wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx("
2466 				   "DEVKEY_VALNAME) failed: %d %d",
2467 				   (int) ret, (int) GetLastError());
2468 			RegCloseKey(hk2);
2469 			continue;
2470 		}
2471 
2472 		if (_tcscmp(devname, WZC_DRIVER) == 0)
2473 			break;
2474 
2475 		RegCloseKey(hk2);
2476 	}
2477 
2478 	RegCloseKey(hk);
2479 
2480 	/* Found WZC - get handle to it. */
2481 	len = sizeof(hnd);
2482 	ret = RegQueryValueEx(hk2, DEVLOAD_HANDLE_VALNAME, NULL, NULL,
2483 			      (PUCHAR) &hnd, &len);
2484 	if (ret != ERROR_SUCCESS) {
2485 		wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx(HANDLE_VALNAME) "
2486 			   "failed: %d %d", (int) ret, (int) GetLastError());
2487 		RegCloseKey(hk2);
2488 		return -1;
2489 	}
2490 
2491 	RegCloseKey(hk2);
2492 
2493 	/* Deactivate WZC */
2494 	if (!DeactivateDevice((HANDLE) hnd)) {
2495 		wpa_printf(MSG_DEBUG, "NDIS: DeactivateDevice failed: %d",
2496 			   (int) GetLastError());
2497 		return -1;
2498 	}
2499 
2500 	wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily");
2501 	drv->wzc_disabled = 1;
2502 	return wpa_driver_ndis_rebind_adapter(drv);
2503 
2504 #else /* _WIN32_WCE */
2505 
2506 	HMODULE hm;
2507 	DWORD (WINAPI *wzc_enum_interf)(LPWSTR pSrvAddr,
2508 					PINTFS_KEY_TABLE pIntfs);
2509 	DWORD (WINAPI *wzc_query_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2510 					 PINTF_ENTRY pIntf,
2511 					 LPDWORD pdwOutFlags);
2512 	DWORD (WINAPI *wzc_set_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2513 				       PINTF_ENTRY pIntf, LPDWORD pdwOutFlags);
2514 	int ret = -1, j;
2515 	DWORD res;
2516 	INTFS_KEY_TABLE guids;
2517 	INTF_ENTRY intf;
2518 	char guid[128];
2519 	WCHAR *pos;
2520 	DWORD flags, i;
2521 
2522 	hm = LoadLibrary(TEXT("wzcsapi.dll"));
2523 	if (hm == NULL) {
2524 		wpa_printf(MSG_DEBUG, "NDIS: Failed to load wzcsapi.dll (%u) "
2525 			   "- WZC probably not running",
2526 			   (unsigned int) GetLastError());
2527 		return -1;
2528 	}
2529 
2530 #ifdef _WIN32_WCE
2531 	wzc_enum_interf = (void *) GetProcAddressA(hm, "WZCEnumInterfaces");
2532 	wzc_query_interf = (void *) GetProcAddressA(hm, "WZCQueryInterface");
2533 	wzc_set_interf = (void *) GetProcAddressA(hm, "WZCSetInterface");
2534 #else /* _WIN32_WCE */
2535 	wzc_enum_interf = (void *) GetProcAddress(hm, "WZCEnumInterfaces");
2536 	wzc_query_interf = (void *) GetProcAddress(hm, "WZCQueryInterface");
2537 	wzc_set_interf = (void *) GetProcAddress(hm, "WZCSetInterface");
2538 #endif /* _WIN32_WCE */
2539 
2540 	if (wzc_enum_interf == NULL || wzc_query_interf == NULL ||
2541 	    wzc_set_interf == NULL) {
2542 		wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces, "
2543 			   "WZCQueryInterface, or WZCSetInterface not found "
2544 			   "in wzcsapi.dll");
2545 		goto fail;
2546 	}
2547 
2548 	os_memset(&guids, 0, sizeof(guids));
2549 	res = wzc_enum_interf(NULL, &guids);
2550 	if (res != 0) {
2551 		wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces failed: %d; "
2552 			   "WZC service is apparently not running",
2553 			   (int) res);
2554 		goto fail;
2555 	}
2556 
2557 	wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces: %d interfaces",
2558 		   (int) guids.dwNumIntfs);
2559 
2560 	for (i = 0; i < guids.dwNumIntfs; i++) {
2561 		pos = guids.pIntfs[i].wszGuid;
2562 		for (j = 0; j < sizeof(guid); j++) {
2563 			guid[j] = (char) *pos;
2564 			if (*pos == 0)
2565 				break;
2566 			pos++;
2567 		}
2568 		guid[sizeof(guid) - 1] = '\0';
2569 		wpa_printf(MSG_DEBUG, "NDIS: intfs %d GUID '%s'",
2570 			   (int) i, guid);
2571 		if (os_strstr(drv->ifname, guid) == NULL)
2572 			continue;
2573 
2574 		wpa_printf(MSG_DEBUG, "NDIS: Current interface found from "
2575 			   "WZC");
2576 		break;
2577 	}
2578 
2579 	if (i >= guids.dwNumIntfs) {
2580 		wpa_printf(MSG_DEBUG, "NDIS: Current interface not found from "
2581 			   "WZC");
2582 		goto fail;
2583 	}
2584 
2585 	os_memset(&intf, 0, sizeof(intf));
2586 	intf.wszGuid = guids.pIntfs[i].wszGuid;
2587 	/* Set flags to verify that the structure has not changed. */
2588 	intf.dwCtlFlags = -1;
2589 	flags = 0;
2590 	res = wzc_query_interf(NULL, INTFCTL_ENABLED, &intf, &flags);
2591 	if (res != 0) {
2592 		wpa_printf(MSG_DEBUG, "NDIS: Could not query flags for the "
2593 			   "WZC interface: %d (0x%x)",
2594 			   (int) res, (int) res);
2595 		wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2596 			   (unsigned int) GetLastError());
2597 		goto fail;
2598 	}
2599 
2600 	wpa_printf(MSG_DEBUG, "NDIS: WZC interface flags 0x%x dwCtlFlags 0x%x",
2601 		   (int) flags, (int) intf.dwCtlFlags);
2602 
2603 	if (intf.dwCtlFlags == -1) {
2604 		wpa_printf(MSG_DEBUG, "NDIS: Looks like wzcsapi has changed "
2605 			   "again - could not disable WZC");
2606 		wpa_hexdump(MSG_MSGDUMP, "NDIS: intf",
2607 			    (u8 *) &intf, sizeof(intf));
2608 		goto fail;
2609 	}
2610 
2611 	if (enable) {
2612 		if (!(intf.dwCtlFlags & INTFCTL_ENABLED)) {
2613 			wpa_printf(MSG_DEBUG, "NDIS: Enabling WZC for this "
2614 				   "interface");
2615 			intf.dwCtlFlags |= INTFCTL_ENABLED;
2616 			res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2617 					     &flags);
2618 			if (res != 0) {
2619 				wpa_printf(MSG_DEBUG, "NDIS: Failed to enable "
2620 					   "WZC: %d (0x%x)",
2621 					   (int) res, (int) res);
2622 				wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2623 					   (unsigned int) GetLastError());
2624 				goto fail;
2625 			}
2626 			wpa_printf(MSG_DEBUG, "NDIS: Re-enabled WZC for this "
2627 				   "interface");
2628 			drv->wzc_disabled = 0;
2629 		}
2630 	} else {
2631 		if (intf.dwCtlFlags & INTFCTL_ENABLED) {
2632 			wpa_printf(MSG_DEBUG, "NDIS: Disabling WZC for this "
2633 				   "interface");
2634 			intf.dwCtlFlags &= ~INTFCTL_ENABLED;
2635 			res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2636 					     &flags);
2637 			if (res != 0) {
2638 				wpa_printf(MSG_DEBUG, "NDIS: Failed to "
2639 					   "disable WZC: %d (0x%x)",
2640 					   (int) res, (int) res);
2641 				wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2642 					   (unsigned int) GetLastError());
2643 				goto fail;
2644 			}
2645 			wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily "
2646 				   "for this interface");
2647 			drv->wzc_disabled = 1;
2648 		} else {
2649 			wpa_printf(MSG_DEBUG, "NDIS: WZC was not enabled for "
2650 				   "this interface");
2651 		}
2652 	}
2653 
2654 	ret = 0;
2655 
2656 fail:
2657 	FreeLibrary(hm);
2658 
2659 	return ret;
2660 #endif /* _WIN32_WCE */
2661 }
2662 
2663 #else /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2664 
wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data * drv,int enable)2665 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2666 				   int enable)
2667 {
2668 	return 0;
2669 }
2670 
2671 #endif /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2672 
2673 
2674 #ifdef CONFIG_USE_NDISUIO
2675 /*
2676  * l2_packet_ndis.c is sharing the same handle to NDISUIO, so we must be able
2677  * to export this handle. This is somewhat ugly, but there is no better
2678  * mechanism available to pass data from driver interface to l2_packet wrapper.
2679  */
2680 static HANDLE driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2681 
driver_ndis_get_ndisuio_handle(void)2682 HANDLE driver_ndis_get_ndisuio_handle(void)
2683 {
2684 	return driver_ndis_ndisuio_handle;
2685 }
2686 #endif /* CONFIG_USE_NDISUIO */
2687 
2688 
wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data * drv)2689 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv)
2690 {
2691 #ifdef CONFIG_USE_NDISUIO
2692 #ifndef _WIN32_WCE
2693 #define NDISUIO_DEVICE_NAME TEXT("\\\\.\\\\Ndisuio")
2694 	DWORD written;
2695 #endif /* _WIN32_WCE */
2696 	drv->ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2697 				  GENERIC_READ | GENERIC_WRITE, 0, NULL,
2698 				  OPEN_EXISTING,
2699 				  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
2700 				  INVALID_HANDLE_VALUE);
2701 	if (drv->ndisuio == INVALID_HANDLE_VALUE) {
2702 		wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to "
2703 			   "NDISUIO: %d", (int) GetLastError());
2704 		return -1;
2705 	}
2706 	driver_ndis_ndisuio_handle = drv->ndisuio;
2707 
2708 #ifndef _WIN32_WCE
2709 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0,
2710 			     NULL, 0, &written, NULL)) {
2711 		wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: "
2712 			   "%d", (int) GetLastError());
2713 		CloseHandle(drv->ndisuio);
2714 		drv->ndisuio = INVALID_HANDLE_VALUE;
2715 		return -1;
2716 	}
2717 #endif /* _WIN32_WCE */
2718 
2719 	return 0;
2720 #else /* CONFIG_USE_NDISUIO */
2721 	return 0;
2722 #endif /* CONFIG_USE_NDISUIO */
2723 }
2724 
2725 
wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data * drv)2726 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv)
2727 {
2728 #ifdef CONFIG_USE_NDISUIO
2729 	DWORD written;
2730 #define MAX_NDIS_DEVICE_NAME_LEN 256
2731 	WCHAR ifname[MAX_NDIS_DEVICE_NAME_LEN];
2732 	size_t len, i, pos;
2733 	const char *prefix = "\\DEVICE\\";
2734 
2735 #ifdef _WIN32_WCE
2736 	pos = 0;
2737 #else /* _WIN32_WCE */
2738 	pos = 8;
2739 #endif /* _WIN32_WCE */
2740 	len = pos + os_strlen(drv->ifname);
2741 	if (len >= MAX_NDIS_DEVICE_NAME_LEN)
2742 		return -1;
2743 	for (i = 0; i < pos; i++)
2744 		ifname[i] = (WCHAR) prefix[i];
2745 	for (i = pos; i < len; i++)
2746 		ifname[i] = (WCHAR) drv->ifname[i - pos];
2747 	ifname[i] = L'\0';
2748 
2749 	if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_OPEN_DEVICE,
2750 			     ifname, len * sizeof(WCHAR), NULL, 0, &written,
2751 			     NULL)) {
2752 		wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_OPEN_DEVICE "
2753 			   "failed: %d", (int) GetLastError());
2754 		wpa_hexdump_ascii(MSG_DEBUG, "NDIS: ifname",
2755 				  (const u8 *) ifname, len * sizeof(WCHAR));
2756 		CloseHandle(drv->ndisuio);
2757 		drv->ndisuio = INVALID_HANDLE_VALUE;
2758 		return -1;
2759 	}
2760 
2761 	wpa_printf(MSG_DEBUG, "NDIS: Opened NDISUIO device successfully");
2762 
2763 	return 0;
2764 #else /* CONFIG_USE_NDISUIO */
2765 	char ifname[128];
2766 	os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", drv->ifname);
2767 	drv->adapter = PacketOpenAdapter(ifname);
2768 	if (drv->adapter == NULL) {
2769 		wpa_printf(MSG_DEBUG, "NDIS: PacketOpenAdapter failed for "
2770 			   "'%s'", ifname);
2771 		return -1;
2772 	}
2773 	return 0;
2774 #endif /* CONFIG_USE_NDISUIO */
2775 }
2776 
2777 
wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data * drv)2778 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv)
2779 {
2780 #ifdef CONFIG_USE_NDISUIO
2781 	driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2782 	if (drv->ndisuio != INVALID_HANDLE_VALUE)
2783 		CloseHandle(drv->ndisuio);
2784 #else /* CONFIG_USE_NDISUIO */
2785 	if (drv->adapter)
2786 		PacketCloseAdapter(drv->adapter);
2787 #endif /* CONFIG_USE_NDISUIO */
2788 }
2789 
2790 
ndis_add_multicast(struct wpa_driver_ndis_data * drv)2791 static int ndis_add_multicast(struct wpa_driver_ndis_data *drv)
2792 {
2793 	if (ndis_set_oid(drv, OID_802_3_MULTICAST_LIST,
2794 			 (const char *) pae_group_addr, ETH_ALEN) < 0) {
2795 		wpa_printf(MSG_DEBUG, "NDIS: Failed to add PAE group address "
2796 			   "to the multicast list");
2797 		return -1;
2798 	}
2799 
2800 	return 0;
2801 }
2802 
2803 
wpa_driver_ndis_init(void * ctx,const char * ifname)2804 static void * wpa_driver_ndis_init(void *ctx, const char *ifname)
2805 {
2806 	struct wpa_driver_ndis_data *drv;
2807 	u32 mode;
2808 
2809 	drv = os_zalloc(sizeof(*drv));
2810 	if (drv == NULL)
2811 		return NULL;
2812 	drv->ctx = ctx;
2813 	/*
2814 	 * Compatibility code to strip possible prefix from the GUID. Previous
2815 	 * versions include \Device\NPF_ prefix for all names, but the internal
2816 	 * interface name is now only the GUI. Both Packet32 and NDISUIO
2817 	 * prefixes are supported.
2818 	 */
2819 	if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0)
2820 		ifname += 12;
2821 	else if (os_strncmp(ifname, "\\DEVICE\\", 8) == 0)
2822 		ifname += 8;
2823 	os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
2824 
2825 	if (wpa_driver_ndis_adapter_init(drv) < 0) {
2826 		os_free(drv);
2827 		return NULL;
2828 	}
2829 
2830 	if (wpa_driver_ndis_get_names(drv) < 0) {
2831 		wpa_driver_ndis_adapter_close(drv);
2832 		os_free(drv);
2833 		return NULL;
2834 	}
2835 
2836 	wpa_driver_ndis_set_wzc(drv, 0);
2837 
2838 	if (wpa_driver_ndis_adapter_open(drv) < 0) {
2839 		wpa_driver_ndis_adapter_close(drv);
2840 		os_free(drv);
2841 		return NULL;
2842 	}
2843 
2844 	if (ndis_get_oid(drv, OID_802_3_CURRENT_ADDRESS,
2845 			 (char *) drv->own_addr, ETH_ALEN) < 0) {
2846 		wpa_printf(MSG_DEBUG, "NDIS: Get OID_802_3_CURRENT_ADDRESS "
2847 			   "failed");
2848 		wpa_driver_ndis_adapter_close(drv);
2849 		os_free(drv);
2850 		return NULL;
2851 	}
2852 	wpa_driver_ndis_get_capability(drv);
2853 
2854 	/* Make sure that the driver does not have any obsolete PMKID entries.
2855 	 */
2856 	wpa_driver_ndis_flush_pmkid(drv);
2857 
2858 	/*
2859 	 * Disconnect to make sure that driver re-associates if it was
2860 	 * connected.
2861 	 */
2862 	wpa_driver_ndis_disconnect(drv);
2863 
2864 	eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout, drv, NULL);
2865 
2866 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2867 	drv->events = ndis_events_init(&drv->events_pipe, &drv->event_avail,
2868 				       drv->ifname, drv->adapter_desc);
2869 	if (drv->events == NULL) {
2870 		wpa_driver_ndis_deinit(drv);
2871 		return NULL;
2872 	}
2873 	eloop_register_event(drv->event_avail, sizeof(drv->event_avail),
2874 			     wpa_driver_ndis_event_pipe_cb, drv, NULL);
2875 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2876 
2877 #ifdef _WIN32_WCE
2878 	if (ndisuio_notification_init(drv) < 0) {
2879 		wpa_driver_ndis_deinit(drv);
2880 		return NULL;
2881 	}
2882 #endif /* _WIN32_WCE */
2883 
2884 	/* Set mode here in case card was configured for ad-hoc mode
2885 	 * previously. */
2886 	mode = Ndis802_11Infrastructure;
2887 	if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
2888 			 (char *) &mode, sizeof(mode)) < 0) {
2889 		char buf[8];
2890 		int res;
2891 		wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
2892 			   "OID_802_11_INFRASTRUCTURE_MODE (%d)",
2893 			   (int) mode);
2894 		/* Try to continue anyway */
2895 
2896 		res = ndis_get_oid(drv, OID_DOT11_CURRENT_OPERATION_MODE, buf,
2897 				   sizeof(buf));
2898 		if (res > 0) {
2899 			wpa_printf(MSG_INFO, "NDIS: The driver seems to use "
2900 				   "Native 802.11 OIDs. These are not yet "
2901 				   "fully supported.");
2902 			drv->native80211 = 1;
2903 		} else if (!drv->has_capability || drv->capa.enc == 0) {
2904 			/*
2905 			 * Note: This will also happen with NDIS 6 drivers with
2906 			 * Vista.
2907 			 */
2908 			wpa_printf(MSG_DEBUG, "NDIS: Driver did not provide "
2909 				   "any wireless capabilities - assume it is "
2910 				   "a wired interface");
2911 			drv->wired = 1;
2912 			drv->capa.flags |= WPA_DRIVER_FLAGS_WIRED;
2913 			drv->has_capability = 1;
2914 			ndis_add_multicast(drv);
2915 		}
2916 	}
2917 
2918 	return drv;
2919 }
2920 
2921 
wpa_driver_ndis_deinit(void * priv)2922 static void wpa_driver_ndis_deinit(void *priv)
2923 {
2924 	struct wpa_driver_ndis_data *drv = priv;
2925 
2926 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2927 	if (drv->events) {
2928 		eloop_unregister_event(drv->event_avail,
2929 				       sizeof(drv->event_avail));
2930 		ndis_events_deinit(drv->events);
2931 	}
2932 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2933 
2934 #ifdef _WIN32_WCE
2935 	ndisuio_notification_deinit(drv);
2936 #endif /* _WIN32_WCE */
2937 
2938 	eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
2939 	eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
2940 	wpa_driver_ndis_flush_pmkid(drv);
2941 	wpa_driver_ndis_disconnect(drv);
2942 	if (wpa_driver_ndis_radio_off(drv) < 0) {
2943 		wpa_printf(MSG_DEBUG, "NDIS: failed to disassociate and turn "
2944 			   "radio off");
2945 	}
2946 
2947 	wpa_driver_ndis_adapter_close(drv);
2948 
2949 	if (drv->wzc_disabled)
2950 		wpa_driver_ndis_set_wzc(drv, 1);
2951 
2952 #ifdef _WIN32_WCE
2953 	os_free(drv->adapter_name);
2954 #endif /* _WIN32_WCE */
2955 	os_free(drv->adapter_desc);
2956 	os_free(drv);
2957 }
2958 
2959 
2960 static struct wpa_interface_info *
wpa_driver_ndis_get_interfaces(void * global_priv)2961 wpa_driver_ndis_get_interfaces(void *global_priv)
2962 {
2963 	struct wpa_interface_info *iface = NULL, *niface;
2964 
2965 #ifdef CONFIG_USE_NDISUIO
2966 	NDISUIO_QUERY_BINDING *b;
2967 	size_t blen = sizeof(*b) + 1024;
2968 	int i, error;
2969 	DWORD written;
2970 	char name[256], desc[256];
2971 	WCHAR *pos;
2972 	size_t j, len;
2973 	HANDLE ndisuio;
2974 
2975 	ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2976 			     GENERIC_READ | GENERIC_WRITE, 0, NULL,
2977 			     OPEN_EXISTING,
2978 			     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
2979 			     INVALID_HANDLE_VALUE);
2980 	if (ndisuio == INVALID_HANDLE_VALUE) {
2981 		wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to "
2982 			   "NDISUIO: %d", (int) GetLastError());
2983 		return NULL;
2984 	}
2985 
2986 #ifndef _WIN32_WCE
2987 	if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0,
2988 			     NULL, 0, &written, NULL)) {
2989 		wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: "
2990 			   "%d", (int) GetLastError());
2991 		CloseHandle(ndisuio);
2992 		return NULL;
2993 	}
2994 #endif /* _WIN32_WCE */
2995 
2996 	b = os_malloc(blen);
2997 	if (b == NULL) {
2998 		CloseHandle(ndisuio);
2999 		return NULL;
3000 	}
3001 
3002 	for (i = 0; ; i++) {
3003 		os_memset(b, 0, blen);
3004 		b->BindingIndex = i;
3005 		if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
3006 				     b, sizeof(NDISUIO_QUERY_BINDING), b, blen,
3007 				     &written, NULL)) {
3008 			error = (int) GetLastError();
3009 			if (error == ERROR_NO_MORE_ITEMS)
3010 				break;
3011 			wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
3012 				   "failed: %d", error);
3013 			break;
3014 		}
3015 
3016 		pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
3017 		len = b->DeviceNameLength;
3018 		if (len >= sizeof(name))
3019 			len = sizeof(name) - 1;
3020 		for (j = 0; j < len; j++)
3021 			name[j] = (char) pos[j];
3022 		name[len] = '\0';
3023 
3024 		pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
3025 		len = b->DeviceDescrLength;
3026 		if (len >= sizeof(desc))
3027 			len = sizeof(desc) - 1;
3028 		for (j = 0; j < len; j++)
3029 			desc[j] = (char) pos[j];
3030 		desc[len] = '\0';
3031 
3032 		wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
3033 
3034 		niface = os_zalloc(sizeof(*niface));
3035 		if (niface == NULL)
3036 			break;
3037 		niface->drv_name = "ndis";
3038 		if (os_strncmp(name, "\\DEVICE\\", 8) == 0)
3039 			niface->ifname = os_strdup(name + 8);
3040 		else
3041 			niface->ifname = os_strdup(name);
3042 		if (niface->ifname == NULL) {
3043 			os_free(niface);
3044 			break;
3045 		}
3046 		niface->desc = os_strdup(desc);
3047 		niface->next = iface;
3048 		iface = niface;
3049 	}
3050 
3051 	os_free(b);
3052 	CloseHandle(ndisuio);
3053 #else /* CONFIG_USE_NDISUIO */
3054 	PTSTR _names;
3055 	char *names, *pos, *pos2;
3056 	ULONG len;
3057 	BOOLEAN res;
3058 	char *name[MAX_ADAPTERS];
3059 	char *desc[MAX_ADAPTERS];
3060 	int num_name, num_desc, i;
3061 
3062 	wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
3063 		   PacketGetVersion());
3064 
3065 	len = 8192;
3066 	_names = os_zalloc(len);
3067 	if (_names == NULL)
3068 		return NULL;
3069 
3070 	res = PacketGetAdapterNames(_names, &len);
3071 	if (!res && len > 8192) {
3072 		os_free(_names);
3073 		_names = os_zalloc(len);
3074 		if (_names == NULL)
3075 			return NULL;
3076 		res = PacketGetAdapterNames(_names, &len);
3077 	}
3078 
3079 	if (!res) {
3080 		wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
3081 			   "(PacketGetAdapterNames)");
3082 		os_free(_names);
3083 		return NULL;
3084 	}
3085 
3086 	names = (char *) _names;
3087 	if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') {
3088 		wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in "
3089 			   "UNICODE");
3090 		/* Convert to ASCII */
3091 		pos2 = pos = names;
3092 		while (pos2 < names + len) {
3093 			if (pos2[0] == '\0' && pos2[1] == '\0' &&
3094 			    pos2[2] == '\0' && pos2[3] == '\0') {
3095 				pos2 += 4;
3096 				break;
3097 			}
3098 			*pos++ = pos2[0];
3099 			pos2 += 2;
3100 		}
3101 		os_memcpy(pos + 2, names, pos - names);
3102 		pos += 2;
3103 	} else
3104 		pos = names;
3105 
3106 	num_name = 0;
3107 	while (pos < names + len) {
3108 		name[num_name] = pos;
3109 		while (*pos && pos < names + len)
3110 			pos++;
3111 		if (pos + 1 >= names + len) {
3112 			os_free(names);
3113 			return NULL;
3114 		}
3115 		pos++;
3116 		num_name++;
3117 		if (num_name >= MAX_ADAPTERS) {
3118 			wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
3119 			os_free(names);
3120 			return NULL;
3121 		}
3122 		if (*pos == '\0') {
3123 			wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
3124 				   num_name);
3125 			pos++;
3126 			break;
3127 		}
3128 	}
3129 
3130 	num_desc = 0;
3131 	while (pos < names + len) {
3132 		desc[num_desc] = pos;
3133 		while (*pos && pos < names + len)
3134 			pos++;
3135 		if (pos + 1 >= names + len) {
3136 			os_free(names);
3137 			return NULL;
3138 		}
3139 		pos++;
3140 		num_desc++;
3141 		if (num_desc >= MAX_ADAPTERS) {
3142 			wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
3143 				   "descriptions");
3144 			os_free(names);
3145 			return NULL;
3146 		}
3147 		if (*pos == '\0') {
3148 			wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
3149 				   "found", num_name);
3150 			pos++;
3151 			break;
3152 		}
3153 	}
3154 
3155 	/*
3156 	 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
3157 	 * descriptions. Fill in dummy descriptors to work around this.
3158 	 */
3159 	while (num_desc < num_name)
3160 		desc[num_desc++] = "dummy description";
3161 
3162 	if (num_name != num_desc) {
3163 		wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and "
3164 			   "description counts (%d != %d)",
3165 			   num_name, num_desc);
3166 		os_free(names);
3167 		return NULL;
3168 	}
3169 
3170 	for (i = 0; i < num_name; i++) {
3171 		niface = os_zalloc(sizeof(*niface));
3172 		if (niface == NULL)
3173 			break;
3174 		niface->drv_name = "ndis";
3175 		if (os_strncmp(name[i], "\\Device\\NPF_", 12) == 0)
3176 			niface->ifname = os_strdup(name[i] + 12);
3177 		else
3178 			niface->ifname = os_strdup(name[i]);
3179 		if (niface->ifname == NULL) {
3180 			os_free(niface);
3181 			break;
3182 		}
3183 		niface->desc = os_strdup(desc[i]);
3184 		niface->next = iface;
3185 		iface = niface;
3186 	}
3187 
3188 #endif /* CONFIG_USE_NDISUIO */
3189 
3190 	return iface;
3191 }
3192 
3193 
3194 static const char *ndis_drv_name = "ndis";
3195 static const char *ndis_drv_desc = "Windows NDIS driver";
3196 
3197 struct wpa_driver_ops wpa_driver_ndis_ops;
3198 
driver_ndis_init_ops(void)3199 void driver_ndis_init_ops(void)
3200 {
3201 	os_memset(&wpa_driver_ndis_ops, 0, sizeof(wpa_driver_ndis_ops));
3202 	wpa_driver_ndis_ops.name = ndis_drv_name;
3203 	wpa_driver_ndis_ops.desc = ndis_drv_desc;
3204 	wpa_driver_ndis_ops.get_bssid = wpa_driver_ndis_get_bssid;
3205 	wpa_driver_ndis_ops.get_ssid = wpa_driver_ndis_get_ssid;
3206 	wpa_driver_ndis_ops.set_key = wpa_driver_ndis_set_key;
3207 	wpa_driver_ndis_ops.init = wpa_driver_ndis_init;
3208 	wpa_driver_ndis_ops.deinit = wpa_driver_ndis_deinit;
3209 	wpa_driver_ndis_ops.deauthenticate = wpa_driver_ndis_deauthenticate;
3210 	wpa_driver_ndis_ops.associate = wpa_driver_ndis_associate;
3211 	wpa_driver_ndis_ops.add_pmkid = wpa_driver_ndis_add_pmkid;
3212 	wpa_driver_ndis_ops.remove_pmkid = wpa_driver_ndis_remove_pmkid;
3213 	wpa_driver_ndis_ops.flush_pmkid = wpa_driver_ndis_flush_pmkid;
3214 	wpa_driver_ndis_ops.get_capa = wpa_driver_ndis_get_capa;
3215 	wpa_driver_ndis_ops.poll = wpa_driver_ndis_poll;
3216 	wpa_driver_ndis_ops.get_ifname = wpa_driver_ndis_get_ifname;
3217 	wpa_driver_ndis_ops.get_mac_addr = wpa_driver_ndis_get_mac_addr;
3218 	wpa_driver_ndis_ops.get_scan_results2 =
3219 		wpa_driver_ndis_get_scan_results;
3220 	wpa_driver_ndis_ops.get_interfaces = wpa_driver_ndis_get_interfaces;
3221 	wpa_driver_ndis_ops.scan2 = wpa_driver_ndis_scan;
3222 }
3223