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