2 * WPA Supplicant - Windows/NDIS driver interface
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 /* Avoid some header file conflicts by not including standard headers for
17 * cygwin builds when Packet32.h is included. */
18 #include "build_config.h"
20 #else /* __CYGWIN__ */
22 #endif /* __CYGWIN__ */
23 #ifdef CONFIG_USE_NDISUIO
25 #else /* CONFIG_USE_NDISUIO */
27 #endif /* CONFIG_USE_NDISUIO */
28 #ifdef __MINGW32_VERSION
29 #include <ddk/ntddndis.h>
30 #else /* __MINGW32_VERSION */
32 #endif /* __MINGW32_VERSION */
38 #endif /* _WIN32_WCE */
43 #include "ieee802_11_defs.h"
44 #include "driver_ndis.h"
46 int wpa_driver_register_event_cb(struct wpa_driver_ndis_data *drv);
47 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
48 void wpa_driver_ndis_event_pipe_cb(void *eloop_data, void *user_data);
49 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
51 static void wpa_driver_ndis_deinit(void *priv);
52 static void wpa_driver_ndis_poll(void *drv);
53 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx);
54 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv);
55 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv);
56 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv);
59 static const u8 pae_group_addr[ETH_ALEN] =
60 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
63 /* FIX: to be removed once this can be compiled with the complete NDIS
65 #ifndef OID_802_11_BSSID
66 #define OID_802_3_MULTICAST_LIST 0x01010103
67 #define OID_802_11_BSSID 0x0d010101
68 #define OID_802_11_SSID 0x0d010102
69 #define OID_802_11_INFRASTRUCTURE_MODE 0x0d010108
70 #define OID_802_11_ADD_WEP 0x0D010113
71 #define OID_802_11_REMOVE_WEP 0x0D010114
72 #define OID_802_11_DISASSOCIATE 0x0D010115
73 #define OID_802_11_BSSID_LIST 0x0d010217
74 #define OID_802_11_AUTHENTICATION_MODE 0x0d010118
75 #define OID_802_11_PRIVACY_FILTER 0x0d010119
76 #define OID_802_11_BSSID_LIST_SCAN 0x0d01011A
77 #define OID_802_11_WEP_STATUS 0x0d01011B
78 #define OID_802_11_ENCRYPTION_STATUS OID_802_11_WEP_STATUS
79 #define OID_802_11_ADD_KEY 0x0d01011D
80 #define OID_802_11_REMOVE_KEY 0x0d01011E
81 #define OID_802_11_ASSOCIATION_INFORMATION 0x0d01011F
82 #define OID_802_11_TEST 0x0d010120
83 #define OID_802_11_CAPABILITY 0x0d010122
84 #define OID_802_11_PMKID 0x0d010123
86 #define NDIS_802_11_LENGTH_SSID 32
87 #define NDIS_802_11_LENGTH_RATES 8
88 #define NDIS_802_11_LENGTH_RATES_EX 16
90 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
92 typedef struct NDIS_802_11_SSID {
94 UCHAR Ssid[NDIS_802_11_LENGTH_SSID];
97 typedef LONG NDIS_802_11_RSSI;
99 typedef enum NDIS_802_11_NETWORK_TYPE {
104 Ndis802_11NetworkTypeMax
105 } NDIS_802_11_NETWORK_TYPE;
107 typedef struct NDIS_802_11_CONFIGURATION_FH {
112 } NDIS_802_11_CONFIGURATION_FH;
114 typedef struct NDIS_802_11_CONFIGURATION {
119 NDIS_802_11_CONFIGURATION_FH FHConfig;
120 } NDIS_802_11_CONFIGURATION;
122 typedef enum NDIS_802_11_NETWORK_INFRASTRUCTURE {
124 Ndis802_11Infrastructure,
125 Ndis802_11AutoUnknown,
126 Ndis802_11InfrastructureMax
127 } NDIS_802_11_NETWORK_INFRASTRUCTURE;
129 typedef enum NDIS_802_11_AUTHENTICATION_MODE {
130 Ndis802_11AuthModeOpen,
131 Ndis802_11AuthModeShared,
132 Ndis802_11AuthModeAutoSwitch,
133 Ndis802_11AuthModeWPA,
134 Ndis802_11AuthModeWPAPSK,
135 Ndis802_11AuthModeWPANone,
136 Ndis802_11AuthModeWPA2,
137 Ndis802_11AuthModeWPA2PSK,
138 Ndis802_11AuthModeMax
139 } NDIS_802_11_AUTHENTICATION_MODE;
141 typedef enum NDIS_802_11_WEP_STATUS {
142 Ndis802_11WEPEnabled,
143 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
144 Ndis802_11WEPDisabled,
145 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
146 Ndis802_11WEPKeyAbsent,
147 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
148 Ndis802_11WEPNotSupported,
149 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
150 Ndis802_11Encryption2Enabled,
151 Ndis802_11Encryption2KeyAbsent,
152 Ndis802_11Encryption3Enabled,
153 Ndis802_11Encryption3KeyAbsent
154 } NDIS_802_11_WEP_STATUS, NDIS_802_11_ENCRYPTION_STATUS;
156 typedef enum NDIS_802_11_PRIVACY_FILTER {
157 Ndis802_11PrivFilterAcceptAll,
158 Ndis802_11PrivFilter8021xWEP
159 } NDIS_802_11_PRIVACY_FILTER;
161 typedef UCHAR NDIS_802_11_RATES[NDIS_802_11_LENGTH_RATES];
162 typedef UCHAR NDIS_802_11_RATES_EX[NDIS_802_11_LENGTH_RATES_EX];
164 typedef struct NDIS_WLAN_BSSID_EX {
166 NDIS_802_11_MAC_ADDRESS MacAddress; /* BSSID */
168 NDIS_802_11_SSID Ssid;
170 NDIS_802_11_RSSI Rssi;
171 NDIS_802_11_NETWORK_TYPE NetworkTypeInUse;
172 NDIS_802_11_CONFIGURATION Configuration;
173 NDIS_802_11_NETWORK_INFRASTRUCTURE InfrastructureMode;
174 NDIS_802_11_RATES_EX SupportedRates;
177 } NDIS_WLAN_BSSID_EX;
179 typedef struct NDIS_802_11_BSSID_LIST_EX {
181 NDIS_WLAN_BSSID_EX Bssid[1];
182 } NDIS_802_11_BSSID_LIST_EX;
184 typedef struct NDIS_802_11_FIXED_IEs {
186 USHORT BeaconInterval;
188 } NDIS_802_11_FIXED_IEs;
190 typedef struct NDIS_802_11_WEP {
194 UCHAR KeyMaterial[1];
197 typedef ULONG NDIS_802_11_KEY_INDEX;
198 typedef ULONGLONG NDIS_802_11_KEY_RSC;
200 typedef struct NDIS_802_11_KEY {
204 NDIS_802_11_MAC_ADDRESS BSSID;
205 NDIS_802_11_KEY_RSC KeyRSC;
206 UCHAR KeyMaterial[1];
209 typedef struct NDIS_802_11_REMOVE_KEY {
212 NDIS_802_11_MAC_ADDRESS BSSID;
213 } NDIS_802_11_REMOVE_KEY;
215 typedef struct NDIS_802_11_AI_REQFI {
217 USHORT ListenInterval;
218 NDIS_802_11_MAC_ADDRESS CurrentAPAddress;
219 } NDIS_802_11_AI_REQFI;
221 typedef struct NDIS_802_11_AI_RESFI {
224 USHORT AssociationId;
225 } NDIS_802_11_AI_RESFI;
227 typedef struct NDIS_802_11_ASSOCIATION_INFORMATION {
229 USHORT AvailableRequestFixedIEs;
230 NDIS_802_11_AI_REQFI RequestFixedIEs;
231 ULONG RequestIELength;
232 ULONG OffsetRequestIEs;
233 USHORT AvailableResponseFixedIEs;
234 NDIS_802_11_AI_RESFI ResponseFixedIEs;
235 ULONG ResponseIELength;
236 ULONG OffsetResponseIEs;
237 } NDIS_802_11_ASSOCIATION_INFORMATION;
239 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
240 NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
241 NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
242 } NDIS_802_11_AUTHENTICATION_ENCRYPTION;
244 typedef struct NDIS_802_11_CAPABILITY {
248 ULONG NoOfAuthEncryptPairsSupported;
249 NDIS_802_11_AUTHENTICATION_ENCRYPTION
250 AuthenticationEncryptionSupported[1];
251 } NDIS_802_11_CAPABILITY;
253 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
255 typedef struct BSSID_INFO {
256 NDIS_802_11_MAC_ADDRESS BSSID;
257 NDIS_802_11_PMKID_VALUE PMKID;
260 typedef struct NDIS_802_11_PMKID {
262 ULONG BSSIDInfoCount;
263 BSSID_INFO BSSIDInfo[1];
266 typedef enum NDIS_802_11_STATUS_TYPE {
267 Ndis802_11StatusType_Authentication,
268 Ndis802_11StatusType_PMKID_CandidateList = 2,
269 Ndis802_11StatusTypeMax
270 } NDIS_802_11_STATUS_TYPE;
272 typedef struct NDIS_802_11_STATUS_INDICATION {
273 NDIS_802_11_STATUS_TYPE StatusType;
274 } NDIS_802_11_STATUS_INDICATION;
276 typedef struct PMKID_CANDIDATE {
277 NDIS_802_11_MAC_ADDRESS BSSID;
281 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
283 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
286 PMKID_CANDIDATE CandidateList[1];
287 } NDIS_802_11_PMKID_CANDIDATE_LIST;
289 typedef struct NDIS_802_11_AUTHENTICATION_REQUEST {
291 NDIS_802_11_MAC_ADDRESS Bssid;
293 } NDIS_802_11_AUTHENTICATION_REQUEST;
295 #define NDIS_802_11_AUTH_REQUEST_REAUTH 0x01
296 #define NDIS_802_11_AUTH_REQUEST_KEYUPDATE 0x02
297 #define NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR 0x06
298 #define NDIS_802_11_AUTH_REQUEST_GROUP_ERROR 0x0E
300 #endif /* OID_802_11_BSSID */
303 #ifndef OID_802_11_PMKID
304 /* Platform SDK for XP did not include WPA2, so add needed definitions */
306 #define OID_802_11_CAPABILITY 0x0d010122
307 #define OID_802_11_PMKID 0x0d010123
309 #define Ndis802_11AuthModeWPA2 6
310 #define Ndis802_11AuthModeWPA2PSK 7
312 #define Ndis802_11StatusType_PMKID_CandidateList 2
314 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
315 NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
316 NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
317 } NDIS_802_11_AUTHENTICATION_ENCRYPTION;
319 typedef struct NDIS_802_11_CAPABILITY {
323 ULONG NoOfAuthEncryptPairsSupported;
324 NDIS_802_11_AUTHENTICATION_ENCRYPTION
325 AuthenticationEncryptionSupported[1];
326 } NDIS_802_11_CAPABILITY;
328 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
330 typedef struct BSSID_INFO {
331 NDIS_802_11_MAC_ADDRESS BSSID;
332 NDIS_802_11_PMKID_VALUE PMKID;
335 typedef struct NDIS_802_11_PMKID {
337 ULONG BSSIDInfoCount;
338 BSSID_INFO BSSIDInfo[1];
341 typedef struct PMKID_CANDIDATE {
342 NDIS_802_11_MAC_ADDRESS BSSID;
346 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
348 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
351 PMKID_CANDIDATE CandidateList[1];
352 } NDIS_802_11_PMKID_CANDIDATE_LIST;
354 #endif /* OID_802_11_CAPABILITY */
357 #ifdef CONFIG_USE_NDISUIO
359 #ifdef __MINGW32_VERSION
360 typedef ULONG NDIS_OID;
361 #endif /* __MINGW32_VERSION */
362 /* from nuiouser.h */
363 #define FSCTL_NDISUIO_BASE FILE_DEVICE_NETWORK
365 #define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \
366 CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access)
368 #define IOCTL_NDISUIO_OPEN_DEVICE \
369 _NDISUIO_CTL_CODE(0x200, METHOD_BUFFERED, \
370 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
372 #define IOCTL_NDISUIO_QUERY_OID_VALUE \
373 _NDISUIO_CTL_CODE(0x201, METHOD_BUFFERED, \
374 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
376 #define IOCTL_NDISUIO_SET_OID_VALUE \
377 _NDISUIO_CTL_CODE(0x205, METHOD_BUFFERED, \
378 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
380 #define IOCTL_NDISUIO_SET_ETHER_TYPE \
381 _NDISUIO_CTL_CODE(0x202, METHOD_BUFFERED, \
382 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
384 #define IOCTL_NDISUIO_QUERY_BINDING \
385 _NDISUIO_CTL_CODE(0x203, METHOD_BUFFERED, \
386 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
388 #define IOCTL_NDISUIO_BIND_WAIT \
389 _NDISUIO_CTL_CODE(0x204, METHOD_BUFFERED, \
390 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
392 typedef struct _NDISUIO_QUERY_OID
395 UCHAR Data[sizeof(ULONG)];
396 } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;
398 typedef struct _NDISUIO_SET_OID
401 UCHAR Data[sizeof(ULONG)];
402 } NDISUIO_SET_OID, *PNDISUIO_SET_OID;
404 typedef struct _NDISUIO_QUERY_BINDING
407 ULONG DeviceNameOffset;
408 ULONG DeviceNameLength;
409 ULONG DeviceDescrOffset;
410 ULONG DeviceDescrLength;
411 } NDISUIO_QUERY_BINDING, *PNDISUIO_QUERY_BINDING;
412 #endif /* _WIN32_WCE */
413 #endif /* CONFIG_USE_NDISUIO */
416 static int ndis_get_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
417 char *data, size_t len)
419 #ifdef CONFIG_USE_NDISUIO
420 NDISUIO_QUERY_OID *o;
421 size_t buflen = sizeof(*o) + len;
426 o = os_zalloc(buflen);
431 o->ptcDeviceName = drv->adapter_name;
432 #endif /* _WIN32_WCE */
433 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_OID_VALUE,
434 o, sizeof(NDISUIO_QUERY_OID), o, buflen, &written,
436 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_QUERY_OID_VALUE "
437 "failed (oid=%08x): %d", oid, (int) GetLastError());
441 hdrlen = sizeof(NDISUIO_QUERY_OID) - sizeof(o->Data);
442 if (written < hdrlen) {
443 wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d); "
444 "too short", oid, (unsigned int) written);
450 wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d) > "
451 "len (%d)",oid, (unsigned int) written, len);
455 os_memcpy(data, o->Data, written);
459 #else /* CONFIG_USE_NDISUIO */
464 buf = os_zalloc(sizeof(*o) + len);
467 o = (PACKET_OID_DATA *) buf;
471 if (!PacketRequest(drv->adapter, FALSE, o)) {
472 wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
477 if (o->Length > len) {
478 wpa_printf(MSG_DEBUG, "%s: oid=0x%x Length (%d) > len (%d)",
479 __func__, oid, (unsigned int) o->Length, len);
483 os_memcpy(data, o->Data, o->Length);
487 #endif /* CONFIG_USE_NDISUIO */
491 static int ndis_set_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
492 const char *data, size_t len)
494 #ifdef CONFIG_USE_NDISUIO
496 size_t buflen, reallen;
500 os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
501 wpa_hexdump_key(MSG_MSGDUMP, txt, (const u8 *) data, len);
503 buflen = sizeof(*o) + len;
504 reallen = buflen - sizeof(o->Data);
505 o = os_zalloc(buflen);
510 o->ptcDeviceName = drv->adapter_name;
511 #endif /* _WIN32_WCE */
513 os_memcpy(o->Data, data, len);
514 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_SET_OID_VALUE,
515 o, reallen, NULL, 0, &written, NULL)) {
516 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_SET_OID_VALUE "
517 "(oid=%08x) failed: %d", oid, (int) GetLastError());
523 #else /* CONFIG_USE_NDISUIO */
528 os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
529 wpa_hexdump_key(MSG_MSGDUMP, txt, (const u8 *) data, len);
531 buf = os_zalloc(sizeof(*o) + len);
534 o = (PACKET_OID_DATA *) buf;
538 os_memcpy(o->Data, data, len);
540 if (!PacketRequest(drv->adapter, TRUE, o)) {
541 wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
548 #endif /* CONFIG_USE_NDISUIO */
552 static int ndis_set_auth_mode(struct wpa_driver_ndis_data *drv, int mode)
554 u32 auth_mode = mode;
555 if (ndis_set_oid(drv, OID_802_11_AUTHENTICATION_MODE,
556 (char *) &auth_mode, sizeof(auth_mode)) < 0) {
557 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
558 "OID_802_11_AUTHENTICATION_MODE (%d)",
566 static int ndis_get_auth_mode(struct wpa_driver_ndis_data *drv)
570 res = ndis_get_oid(drv, OID_802_11_AUTHENTICATION_MODE,
571 (char *) &auth_mode, sizeof(auth_mode));
572 if (res != sizeof(auth_mode)) {
573 wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
574 "OID_802_11_AUTHENTICATION_MODE");
581 static int ndis_set_encr_status(struct wpa_driver_ndis_data *drv, int encr)
583 u32 encr_status = encr;
584 if (ndis_set_oid(drv, OID_802_11_ENCRYPTION_STATUS,
585 (char *) &encr_status, sizeof(encr_status)) < 0) {
586 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
587 "OID_802_11_ENCRYPTION_STATUS (%d)", encr);
594 static int ndis_get_encr_status(struct wpa_driver_ndis_data *drv)
598 res = ndis_get_oid(drv, OID_802_11_ENCRYPTION_STATUS,
599 (char *) &encr, sizeof(encr));
600 if (res != sizeof(encr)) {
601 wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
602 "OID_802_11_ENCRYPTION_STATUS");
609 static int wpa_driver_ndis_get_bssid(void *priv, u8 *bssid)
611 struct wpa_driver_ndis_data *drv = priv;
615 * Report PAE group address as the "BSSID" for wired
618 os_memcpy(bssid, pae_group_addr, ETH_ALEN);
622 return ndis_get_oid(drv, OID_802_11_BSSID, (char *) bssid, ETH_ALEN) <
627 static int wpa_driver_ndis_get_ssid(void *priv, u8 *ssid)
629 struct wpa_driver_ndis_data *drv = priv;
630 NDIS_802_11_SSID buf;
633 res = ndis_get_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
635 wpa_printf(MSG_DEBUG, "NDIS: Failed to get SSID");
637 wpa_printf(MSG_DEBUG, "NDIS: Allow get_ssid failure "
638 "with a wired interface");
643 os_memcpy(ssid, buf.Ssid, buf.SsidLength);
644 return buf.SsidLength;
648 static int wpa_driver_ndis_set_ssid(struct wpa_driver_ndis_data *drv,
649 const u8 *ssid, size_t ssid_len)
651 NDIS_802_11_SSID buf;
653 os_memset(&buf, 0, sizeof(buf));
654 buf.SsidLength = ssid_len;
655 os_memcpy(buf.Ssid, ssid, ssid_len);
657 * Make sure radio is marked enabled here so that scan request will not
658 * force SSID to be changed to a random one in order to enable radio at
661 drv->radio_enabled = 1;
662 return ndis_set_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
666 /* Disconnect using OID_802_11_DISASSOCIATE. This will also turn the radio off.
668 static int wpa_driver_ndis_radio_off(struct wpa_driver_ndis_data *drv)
670 drv->radio_enabled = 0;
671 return ndis_set_oid(drv, OID_802_11_DISASSOCIATE, " ", 4);
675 /* Disconnect by setting SSID to random (i.e., likely not used). */
676 static int wpa_driver_ndis_disconnect(struct wpa_driver_ndis_data *drv)
680 for (i = 0; i < 32; i++)
681 ssid[i] = rand() & 0xff;
682 return wpa_driver_ndis_set_ssid(drv, (u8 *) ssid, 32);
686 static int wpa_driver_ndis_deauthenticate(void *priv, const u8 *addr,
689 struct wpa_driver_ndis_data *drv = priv;
690 return wpa_driver_ndis_disconnect(drv);
694 static int wpa_driver_ndis_disassociate(void *priv, const u8 *addr,
697 struct wpa_driver_ndis_data *drv = priv;
698 return wpa_driver_ndis_disconnect(drv);
702 static int wpa_driver_ndis_set_wpa(void *priv, int enabled)
704 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
709 static void wpa_driver_ndis_scan_timeout(void *eloop_ctx, void *timeout_ctx)
711 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
712 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
716 static int wpa_driver_ndis_scan(void *priv, const u8 *ssid, size_t ssid_len)
718 struct wpa_driver_ndis_data *drv = priv;
721 if (!drv->radio_enabled) {
722 wpa_printf(MSG_DEBUG, "NDIS: turning radio on before the first"
724 if (wpa_driver_ndis_disconnect(drv) < 0) {
725 wpa_printf(MSG_DEBUG, "NDIS: failed to enable radio");
727 drv->radio_enabled = 1;
730 res = ndis_set_oid(drv, OID_802_11_BSSID_LIST_SCAN, " ", 4);
731 eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
732 eloop_register_timeout(7, 0, wpa_driver_ndis_scan_timeout, drv,
738 static struct wpa_scan_res * wpa_driver_ndis_add_scan_ssid(
739 struct wpa_scan_res *r, NDIS_802_11_SSID *ssid)
741 struct wpa_scan_res *nr;
744 if (wpa_scan_get_ie(r, WLAN_EID_SSID))
745 return r; /* SSID IE already present */
747 if (ssid->SsidLength == 0 || ssid->SsidLength > 32)
748 return r; /* No valid SSID inside scan data */
750 nr = os_realloc(r, sizeof(*r) + r->ie_len + 2 + ssid->SsidLength);
754 pos = ((u8 *) (nr + 1)) + nr->ie_len;
755 *pos++ = WLAN_EID_SSID;
756 *pos++ = ssid->SsidLength;
757 os_memcpy(pos, ssid->Ssid, ssid->SsidLength);
758 nr->ie_len += 2 + ssid->SsidLength;
764 static struct wpa_scan_results * wpa_driver_ndis_get_scan_results(void *priv)
766 struct wpa_driver_ndis_data *drv = priv;
767 NDIS_802_11_BSSID_LIST_EX *b;
768 size_t blen, count, i;
771 struct wpa_scan_results *results;
772 struct wpa_scan_res *r;
778 len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
780 wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
784 count = b->NumberOfItems;
786 results = os_zalloc(sizeof(*results));
787 if (results == NULL) {
791 results->res = os_zalloc(count * sizeof(struct wpa_scan_res *));
792 if (results->res == NULL) {
798 pos = (char *) &b->Bssid[0];
799 for (i = 0; i < count; i++) {
800 NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos;
801 NDIS_802_11_FIXED_IEs *fixed;
803 if (bss->IELength < sizeof(NDIS_802_11_FIXED_IEs)) {
804 wpa_printf(MSG_DEBUG, "NDIS: too small IELength=%d",
805 (int) bss->IELength);
808 if (((char *) bss->IEs) + bss->IELength > (char *) b + blen) {
810 * Some NDIS drivers have been reported to include an
811 * entry with an invalid IELength in scan results and
812 * this has crashed wpa_supplicant, so validate the
813 * returned value before using it.
815 wpa_printf(MSG_DEBUG, "NDIS: skipped invalid scan "
816 "result IE (BSSID=" MACSTR ") IELength=%d",
817 MAC2STR(bss->MacAddress),
818 (int) bss->IELength);
822 r = os_zalloc(sizeof(*r) + bss->IELength -
823 sizeof(NDIS_802_11_FIXED_IEs));
827 os_memcpy(r->bssid, bss->MacAddress, ETH_ALEN);
828 r->level = (int) bss->Rssi;
829 r->freq = bss->Configuration.DSConfig / 1000;
830 fixed = (NDIS_802_11_FIXED_IEs *) bss->IEs;
831 r->beacon_int = WPA_GET_LE16((u8 *) &fixed->BeaconInterval);
832 r->caps = WPA_GET_LE16((u8 *) &fixed->Capabilities);
833 r->tsf = WPA_GET_LE64(fixed->Timestamp);
834 os_memcpy(r + 1, bss->IEs + sizeof(NDIS_802_11_FIXED_IEs),
835 bss->IELength - sizeof(NDIS_802_11_FIXED_IEs));
836 r->ie_len = bss->IELength - sizeof(NDIS_802_11_FIXED_IEs);
837 r = wpa_driver_ndis_add_scan_ssid(r, &bss->Ssid);
839 results->res[results->num++] = r;
842 if (pos > (char *) b + blen)
852 static int wpa_driver_ndis_remove_key(struct wpa_driver_ndis_data *drv,
853 int key_idx, const u8 *addr,
854 const u8 *bssid, int pairwise)
856 NDIS_802_11_REMOVE_KEY rkey;
857 NDIS_802_11_KEY_INDEX index;
860 os_memset(&rkey, 0, sizeof(rkey));
862 rkey.Length = sizeof(rkey);
863 rkey.KeyIndex = key_idx;
865 rkey.KeyIndex |= 1 << 30;
866 os_memcpy(rkey.BSSID, bssid, ETH_ALEN);
868 res = ndis_set_oid(drv, OID_802_11_REMOVE_KEY, (char *) &rkey,
872 res2 = ndis_set_oid(drv, OID_802_11_REMOVE_WEP,
873 (char *) &index, sizeof(index));
877 if (res < 0 && res2 < 0)
883 static int wpa_driver_ndis_add_wep(struct wpa_driver_ndis_data *drv,
884 int pairwise, int key_idx, int set_tx,
885 const u8 *key, size_t key_len)
887 NDIS_802_11_WEP *wep;
892 wep = os_zalloc(len);
896 wep->KeyIndex = key_idx;
898 wep->KeyIndex |= 1 << 31;
899 #if 0 /* Setting bit30 does not seem to work with some NDIS drivers */
901 wep->KeyIndex |= 1 << 30;
903 wep->KeyLength = key_len;
904 os_memcpy(wep->KeyMaterial, key, key_len);
906 wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_WEP",
908 res = ndis_set_oid(drv, OID_802_11_ADD_WEP, (char *) wep, len);
916 static int wpa_driver_ndis_set_key(void *priv, wpa_alg alg, const u8 *addr,
917 int key_idx, int set_tx,
918 const u8 *seq, size_t seq_len,
919 const u8 *key, size_t key_len)
921 struct wpa_driver_ndis_data *drv = priv;
923 NDIS_802_11_KEY *nkey;
927 if (addr == NULL || os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
931 if (wpa_driver_ndis_get_bssid(drv, bssid) < 0)
932 os_memset(bssid, 0xff, ETH_ALEN);
936 os_memcpy(bssid, addr, ETH_ALEN);
939 if (alg == WPA_ALG_NONE || key_len == 0) {
940 return wpa_driver_ndis_remove_key(drv, key_idx, addr, bssid,
944 if (alg == WPA_ALG_WEP) {
945 return wpa_driver_ndis_add_wep(drv, pairwise, key_idx, set_tx,
949 len = 12 + 6 + 6 + 8 + key_len;
951 nkey = os_zalloc(len);
956 nkey->KeyIndex = key_idx;
958 nkey->KeyIndex |= 1 << 31;
960 nkey->KeyIndex |= 1 << 30;
962 nkey->KeyIndex |= 1 << 29;
963 nkey->KeyLength = key_len;
964 os_memcpy(nkey->BSSID, bssid, ETH_ALEN);
965 if (seq && seq_len) {
966 for (i = 0; i < seq_len; i++)
967 nkey->KeyRSC |= (ULONGLONG) seq[i] << (i * 8);
969 if (alg == WPA_ALG_TKIP && key_len == 32) {
970 os_memcpy(nkey->KeyMaterial, key, 16);
971 os_memcpy(nkey->KeyMaterial + 16, key + 24, 8);
972 os_memcpy(nkey->KeyMaterial + 24, key + 16, 8);
974 os_memcpy(nkey->KeyMaterial, key, key_len);
977 wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_KEY",
979 res = ndis_set_oid(drv, OID_802_11_ADD_KEY, (char *) nkey, len);
987 wpa_driver_ndis_associate(void *priv,
988 struct wpa_driver_associate_params *params)
990 struct wpa_driver_ndis_data *drv = priv;
991 u32 auth_mode, encr, priv_mode, mode;
993 drv->mode = params->mode;
995 /* Note: Setting OID_802_11_INFRASTRUCTURE_MODE clears current keys,
996 * so static WEP keys needs to be set again after this. */
997 if (params->mode == IEEE80211_MODE_IBSS) {
998 mode = Ndis802_11IBSS;
999 /* Need to make sure that BSSID polling is enabled for
1001 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
1002 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1005 mode = Ndis802_11Infrastructure;
1006 if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
1007 (char *) &mode, sizeof(mode)) < 0) {
1008 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
1009 "OID_802_11_INFRASTRUCTURE_MODE (%d)",
1011 /* Try to continue anyway */
1014 if (params->key_mgmt_suite == KEY_MGMT_NONE ||
1015 params->key_mgmt_suite == KEY_MGMT_802_1X_NO_WPA) {
1016 /* Re-set WEP keys if static WEP configuration is used. */
1017 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1019 for (i = 0; i < 4; i++) {
1020 if (!params->wep_key[i])
1022 wpa_printf(MSG_DEBUG, "NDIS: Re-setting static WEP "
1024 wpa_driver_ndis_set_key(drv, WPA_ALG_WEP, bcast, i,
1025 i == params->wep_tx_keyidx,
1026 NULL, 0, params->wep_key[i],
1027 params->wep_key_len[i]);
1031 if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
1032 if (params->auth_alg & AUTH_ALG_SHARED_KEY) {
1033 if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
1034 auth_mode = Ndis802_11AuthModeAutoSwitch;
1036 auth_mode = Ndis802_11AuthModeShared;
1038 auth_mode = Ndis802_11AuthModeOpen;
1039 priv_mode = Ndis802_11PrivFilterAcceptAll;
1040 } else if (params->wpa_ie[0] == WLAN_EID_RSN) {
1041 priv_mode = Ndis802_11PrivFilter8021xWEP;
1042 if (params->key_mgmt_suite == KEY_MGMT_PSK)
1043 auth_mode = Ndis802_11AuthModeWPA2PSK;
1045 auth_mode = Ndis802_11AuthModeWPA2;
1047 } else if (params->key_mgmt_suite == KEY_MGMT_WPS) {
1048 auth_mode = Ndis802_11AuthModeOpen;
1049 priv_mode = Ndis802_11PrivFilterAcceptAll;
1050 #endif /* CONFIG_WPS */
1052 priv_mode = Ndis802_11PrivFilter8021xWEP;
1053 if (params->key_mgmt_suite == KEY_MGMT_WPA_NONE)
1054 auth_mode = Ndis802_11AuthModeWPANone;
1055 else if (params->key_mgmt_suite == KEY_MGMT_PSK)
1056 auth_mode = Ndis802_11AuthModeWPAPSK;
1058 auth_mode = Ndis802_11AuthModeWPA;
1061 switch (params->pairwise_suite) {
1063 encr = Ndis802_11Encryption3Enabled;
1066 encr = Ndis802_11Encryption2Enabled;
1070 encr = Ndis802_11Encryption1Enabled;
1073 if (params->group_suite == CIPHER_CCMP)
1074 encr = Ndis802_11Encryption3Enabled;
1075 else if (params->group_suite == CIPHER_TKIP)
1076 encr = Ndis802_11Encryption2Enabled;
1078 encr = Ndis802_11EncryptionDisabled;
1081 encr = Ndis802_11EncryptionDisabled;
1084 if (ndis_set_oid(drv, OID_802_11_PRIVACY_FILTER,
1085 (char *) &priv_mode, sizeof(priv_mode)) < 0) {
1086 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
1087 "OID_802_11_PRIVACY_FILTER (%d)",
1089 /* Try to continue anyway */
1092 ndis_set_auth_mode(drv, auth_mode);
1093 ndis_set_encr_status(drv, encr);
1095 if (params->bssid) {
1096 ndis_set_oid(drv, OID_802_11_BSSID, (char *) params->bssid,
1098 drv->oid_bssid_set = 1;
1099 } else if (drv->oid_bssid_set) {
1100 ndis_set_oid(drv, OID_802_11_BSSID, "\xff\xff\xff\xff\xff\xff",
1102 drv->oid_bssid_set = 0;
1105 return wpa_driver_ndis_set_ssid(drv, params->ssid, params->ssid_len);
1109 static int wpa_driver_ndis_set_pmkid(struct wpa_driver_ndis_data *drv)
1111 int len, count, i, ret;
1112 struct ndis_pmkid_entry *entry;
1113 NDIS_802_11_PMKID *p;
1119 if (count >= drv->no_of_pmkid)
1121 entry = entry->next;
1123 len = 8 + count * sizeof(BSSID_INFO);
1129 p->BSSIDInfoCount = count;
1131 for (i = 0; i < count; i++) {
1132 os_memcpy(&p->BSSIDInfo[i].BSSID, entry->bssid, ETH_ALEN);
1133 os_memcpy(&p->BSSIDInfo[i].PMKID, entry->pmkid, 16);
1134 entry = entry->next;
1136 wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID", (u8 *) p, len);
1137 ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) p, len);
1143 static int wpa_driver_ndis_add_pmkid(void *priv, const u8 *bssid,
1146 struct wpa_driver_ndis_data *drv = priv;
1147 struct ndis_pmkid_entry *entry, *prev;
1149 if (drv->no_of_pmkid == 0)
1155 if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0)
1158 entry = entry->next;
1162 /* Replace existing entry for this BSSID and move it into the
1163 * beginning of the list. */
1164 os_memcpy(entry->pmkid, pmkid, 16);
1166 prev->next = entry->next;
1167 entry->next = drv->pmkid;
1171 entry = os_malloc(sizeof(*entry));
1173 os_memcpy(entry->bssid, bssid, ETH_ALEN);
1174 os_memcpy(entry->pmkid, pmkid, 16);
1175 entry->next = drv->pmkid;
1180 return wpa_driver_ndis_set_pmkid(drv);
1184 static int wpa_driver_ndis_remove_pmkid(void *priv, const u8 *bssid,
1187 struct wpa_driver_ndis_data *drv = priv;
1188 struct ndis_pmkid_entry *entry, *prev;
1190 if (drv->no_of_pmkid == 0)
1196 if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0 &&
1197 os_memcmp(entry->pmkid, pmkid, 16) == 0) {
1199 prev->next = entry->next;
1201 drv->pmkid = entry->next;
1206 entry = entry->next;
1208 return wpa_driver_ndis_set_pmkid(drv);
1212 static int wpa_driver_ndis_flush_pmkid(void *priv)
1214 struct wpa_driver_ndis_data *drv = priv;
1215 NDIS_802_11_PMKID p;
1216 struct ndis_pmkid_entry *pmkid, *prev;
1217 int prev_authmode, ret;
1219 if (drv->no_of_pmkid == 0)
1226 pmkid = pmkid->next;
1231 * Some drivers may refuse OID_802_11_PMKID if authMode is not set to
1232 * WPA2, so change authMode temporarily, if needed.
1234 prev_authmode = ndis_get_auth_mode(drv);
1235 if (prev_authmode != Ndis802_11AuthModeWPA2)
1236 ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA2);
1238 os_memset(&p, 0, sizeof(p));
1240 p.BSSIDInfoCount = 0;
1241 wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID (flush)",
1243 ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) &p, 8);
1245 if (prev_authmode != Ndis802_11AuthModeWPA2)
1246 ndis_set_auth_mode(drv, prev_authmode);
1252 static int wpa_driver_ndis_get_associnfo(struct wpa_driver_ndis_data *drv)
1254 char buf[512], *pos;
1255 NDIS_802_11_ASSOCIATION_INFORMATION *ai;
1257 union wpa_event_data data;
1258 NDIS_802_11_BSSID_LIST_EX *b;
1261 len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION, buf,
1264 wpa_printf(MSG_DEBUG, "NDIS: failed to get association "
1268 if (len > sizeof(buf)) {
1269 /* Some drivers seem to be producing incorrect length for this
1270 * data. Limit the length to the current buffer size to avoid
1271 * crashing in hexdump. The data seems to be otherwise valid,
1272 * so better try to use it. */
1273 wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association "
1274 "information length %d", len);
1275 len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION,
1278 wpa_printf(MSG_DEBUG, "NDIS: re-reading association "
1279 "information failed");
1282 if (len > sizeof(buf)) {
1283 wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association"
1284 " information length %d (re-read)", len);
1288 wpa_hexdump(MSG_MSGDUMP, "NDIS: association information",
1290 if (len < sizeof(*ai)) {
1291 wpa_printf(MSG_DEBUG, "NDIS: too short association "
1295 ai = (NDIS_802_11_ASSOCIATION_INFORMATION *) buf;
1296 wpa_printf(MSG_DEBUG, "NDIS: ReqFixed=0x%x RespFixed=0x%x off_req=%d "
1297 "off_resp=%d len_req=%d len_resp=%d",
1298 ai->AvailableRequestFixedIEs, ai->AvailableResponseFixedIEs,
1299 (int) ai->OffsetRequestIEs, (int) ai->OffsetResponseIEs,
1300 (int) ai->RequestIELength, (int) ai->ResponseIELength);
1302 if (ai->OffsetRequestIEs + ai->RequestIELength > (unsigned) len ||
1303 ai->OffsetResponseIEs + ai->ResponseIELength > (unsigned) len) {
1304 wpa_printf(MSG_DEBUG, "NDIS: association information - "
1309 wpa_hexdump(MSG_MSGDUMP, "NDIS: Request IEs",
1310 (u8 *) buf + ai->OffsetRequestIEs, ai->RequestIELength);
1311 wpa_hexdump(MSG_MSGDUMP, "NDIS: Response IEs",
1312 (u8 *) buf + ai->OffsetResponseIEs, ai->ResponseIELength);
1314 os_memset(&data, 0, sizeof(data));
1315 data.assoc_info.req_ies = (u8 *) buf + ai->OffsetRequestIEs;
1316 data.assoc_info.req_ies_len = ai->RequestIELength;
1317 data.assoc_info.resp_ies = (u8 *) buf + ai->OffsetResponseIEs;
1318 data.assoc_info.resp_ies_len = ai->ResponseIELength;
1321 b = os_zalloc(blen);
1323 goto skip_scan_results;
1324 len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
1326 wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
1329 goto skip_scan_results;
1331 wpa_printf(MSG_DEBUG, "NDIS: %d BSSID items to process for AssocInfo",
1332 (unsigned int) b->NumberOfItems);
1334 pos = (char *) &b->Bssid[0];
1335 for (i = 0; i < b->NumberOfItems; i++) {
1336 NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos;
1337 if (os_memcmp(drv->bssid, bss->MacAddress, ETH_ALEN) == 0 &&
1338 bss->IELength > sizeof(NDIS_802_11_FIXED_IEs)) {
1339 data.assoc_info.beacon_ies =
1341 sizeof(NDIS_802_11_FIXED_IEs);
1342 data.assoc_info.beacon_ies_len =
1343 bss->IELength - sizeof(NDIS_802_11_FIXED_IEs);
1344 wpa_hexdump(MSG_MSGDUMP, "NDIS: Beacon IEs",
1345 data.assoc_info.beacon_ies,
1346 data.assoc_info.beacon_ies_len);
1350 if (pos > (char *) b + blen)
1355 wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
1363 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx)
1365 struct wpa_driver_ndis_data *drv = eloop_ctx;
1372 if (wpa_driver_ndis_get_bssid(drv, bssid)) {
1374 if (!is_zero_ether_addr(drv->bssid)) {
1375 os_memset(drv->bssid, 0, ETH_ALEN);
1376 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1380 if (os_memcmp(drv->bssid, bssid, ETH_ALEN) != 0) {
1381 os_memcpy(drv->bssid, bssid, ETH_ALEN);
1382 wpa_driver_ndis_get_associnfo(drv);
1383 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1387 /* When using integrated NDIS event receiver, we can skip BSSID
1388 * polling when using infrastructure network. However, when using
1389 * IBSS mode, many driver do not seem to generate connection event,
1390 * so we need to enable BSSID polling to figure out when IBSS network
1393 poll = drv->mode == IEEE80211_MODE_IBSS;
1394 #ifndef CONFIG_NDIS_EVENTS_INTEGRATED
1397 #endif /* _WIN32_WCE */
1398 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
1401 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1407 static void wpa_driver_ndis_poll(void *priv)
1409 struct wpa_driver_ndis_data *drv = priv;
1410 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
1411 wpa_driver_ndis_poll_timeout(drv, NULL);
1415 /* Called when driver generates Media Connect Event by calling
1416 * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_CONNECT */
1417 void wpa_driver_ndis_event_connect(struct wpa_driver_ndis_data *drv)
1419 wpa_printf(MSG_DEBUG, "NDIS: Media Connect Event");
1420 if (wpa_driver_ndis_get_bssid(drv, drv->bssid) == 0) {
1421 wpa_driver_ndis_get_associnfo(drv);
1422 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1427 /* Called when driver generates Media Disconnect Event by calling
1428 * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_DISCONNECT */
1429 void wpa_driver_ndis_event_disconnect(struct wpa_driver_ndis_data *drv)
1431 wpa_printf(MSG_DEBUG, "NDIS: Media Disconnect Event");
1432 os_memset(drv->bssid, 0, ETH_ALEN);
1433 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1437 static void wpa_driver_ndis_event_auth(struct wpa_driver_ndis_data *drv,
1438 const u8 *data, size_t data_len)
1440 NDIS_802_11_AUTHENTICATION_REQUEST *req;
1441 int pairwise = 0, group = 0;
1442 union wpa_event_data event;
1444 if (data_len < sizeof(*req)) {
1445 wpa_printf(MSG_DEBUG, "NDIS: Too short Authentication Request "
1446 "Event (len=%d)", data_len);
1449 req = (NDIS_802_11_AUTHENTICATION_REQUEST *) data;
1451 wpa_printf(MSG_DEBUG, "NDIS: Authentication Request Event: "
1452 "Bssid " MACSTR " Flags 0x%x",
1453 MAC2STR(req->Bssid), (int) req->Flags);
1455 if ((req->Flags & NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR) ==
1456 NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR)
1458 else if ((req->Flags & NDIS_802_11_AUTH_REQUEST_GROUP_ERROR) ==
1459 NDIS_802_11_AUTH_REQUEST_GROUP_ERROR)
1462 if (pairwise || group) {
1463 os_memset(&event, 0, sizeof(event));
1464 event.michael_mic_failure.unicast = pairwise;
1465 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE,
1471 static void wpa_driver_ndis_event_pmkid(struct wpa_driver_ndis_data *drv,
1472 const u8 *data, size_t data_len)
1474 NDIS_802_11_PMKID_CANDIDATE_LIST *pmkid;
1476 union wpa_event_data event;
1479 wpa_printf(MSG_DEBUG, "NDIS: Too short PMKID Candidate List "
1480 "Event (len=%d)", data_len);
1483 pmkid = (NDIS_802_11_PMKID_CANDIDATE_LIST *) data;
1484 wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List Event - Version %d "
1486 (int) pmkid->Version, (int) pmkid->NumCandidates);
1488 if (pmkid->Version != 1) {
1489 wpa_printf(MSG_DEBUG, "NDIS: Unsupported PMKID Candidate List "
1490 "Version %d", (int) pmkid->Version);
1494 if (data_len < 8 + pmkid->NumCandidates * sizeof(PMKID_CANDIDATE)) {
1495 wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List underflow");
1499 os_memset(&event, 0, sizeof(event));
1500 for (i = 0; i < pmkid->NumCandidates; i++) {
1501 PMKID_CANDIDATE *p = &pmkid->CandidateList[i];
1502 wpa_printf(MSG_DEBUG, "NDIS: %d: " MACSTR " Flags 0x%x",
1503 i, MAC2STR(p->BSSID), (int) p->Flags);
1504 os_memcpy(event.pmkid_candidate.bssid, p->BSSID, ETH_ALEN);
1505 event.pmkid_candidate.index = i;
1506 event.pmkid_candidate.preauth =
1507 p->Flags & NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
1508 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE,
1514 /* Called when driver calls NdisMIndicateStatus() with
1515 * NDIS_STATUS_MEDIA_SPECIFIC_INDICATION */
1516 void wpa_driver_ndis_event_media_specific(struct wpa_driver_ndis_data *drv,
1517 const u8 *data, size_t data_len)
1519 NDIS_802_11_STATUS_INDICATION *status;
1521 if (data == NULL || data_len < sizeof(*status))
1524 wpa_hexdump(MSG_DEBUG, "NDIS: Media Specific Indication",
1527 status = (NDIS_802_11_STATUS_INDICATION *) data;
1528 data += sizeof(status);
1529 data_len -= sizeof(status);
1531 switch (status->StatusType) {
1532 case Ndis802_11StatusType_Authentication:
1533 wpa_driver_ndis_event_auth(drv, data, data_len);
1535 case Ndis802_11StatusType_PMKID_CandidateList:
1536 wpa_driver_ndis_event_pmkid(drv, data, data_len);
1539 wpa_printf(MSG_DEBUG, "NDIS: Unknown StatusType %d",
1540 (int) status->StatusType);
1546 /* Called when an adapter is added */
1547 void wpa_driver_ndis_event_adapter_arrival(struct wpa_driver_ndis_data *drv)
1549 union wpa_event_data event;
1552 wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Arrival");
1554 for (i = 0; i < 30; i++) {
1555 /* Re-open Packet32/NDISUIO connection */
1556 wpa_driver_ndis_adapter_close(drv);
1557 if (wpa_driver_ndis_adapter_init(drv) < 0 ||
1558 wpa_driver_ndis_adapter_open(drv) < 0) {
1559 wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialization "
1563 wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialized");
1568 os_memset(&event, 0, sizeof(event));
1569 os_strlcpy(event.interface_status.ifname, drv->ifname,
1570 sizeof(event.interface_status.ifname));
1571 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1572 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1576 /* Called when an adapter is removed */
1577 void wpa_driver_ndis_event_adapter_removal(struct wpa_driver_ndis_data *drv)
1579 union wpa_event_data event;
1581 wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Removal");
1582 os_memset(&event, 0, sizeof(event));
1583 os_strlcpy(event.interface_status.ifname, drv->ifname,
1584 sizeof(event.interface_status.ifname));
1585 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
1586 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1591 wpa_driver_ndis_get_wpa_capability(struct wpa_driver_ndis_data *drv)
1593 wpa_printf(MSG_DEBUG, "NDIS: verifying driver WPA capability");
1595 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA) == 0 &&
1596 ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPA) {
1597 wpa_printf(MSG_DEBUG, "NDIS: WPA key management supported");
1598 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1601 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPAPSK) == 0 &&
1602 ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPAPSK) {
1603 wpa_printf(MSG_DEBUG, "NDIS: WPA-PSK key management "
1605 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1608 if (ndis_set_encr_status(drv, Ndis802_11Encryption3Enabled) == 0 &&
1609 ndis_get_encr_status(drv) == Ndis802_11Encryption3KeyAbsent) {
1610 wpa_printf(MSG_DEBUG, "NDIS: CCMP encryption supported");
1611 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1614 if (ndis_set_encr_status(drv, Ndis802_11Encryption2Enabled) == 0 &&
1615 ndis_get_encr_status(drv) == Ndis802_11Encryption2KeyAbsent) {
1616 wpa_printf(MSG_DEBUG, "NDIS: TKIP encryption supported");
1617 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1620 if (ndis_set_encr_status(drv, Ndis802_11Encryption1Enabled) == 0 &&
1621 ndis_get_encr_status(drv) == Ndis802_11Encryption1KeyAbsent) {
1622 wpa_printf(MSG_DEBUG, "NDIS: WEP encryption supported");
1623 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1624 WPA_DRIVER_CAPA_ENC_WEP104;
1627 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeShared) == 0 &&
1628 ndis_get_auth_mode(drv) == Ndis802_11AuthModeShared) {
1629 drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1632 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeOpen) == 0 &&
1633 ndis_get_auth_mode(drv) == Ndis802_11AuthModeOpen) {
1634 drv->capa.auth |= WPA_DRIVER_AUTH_OPEN;
1637 ndis_set_encr_status(drv, Ndis802_11EncryptionDisabled);
1639 /* Could also verify OID_802_11_ADD_KEY error reporting and
1640 * support for OID_802_11_ASSOCIATION_INFORMATION. */
1642 if (drv->capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA &&
1643 drv->capa.enc & (WPA_DRIVER_CAPA_ENC_TKIP |
1644 WPA_DRIVER_CAPA_ENC_CCMP)) {
1645 wpa_printf(MSG_DEBUG, "NDIS: driver supports WPA");
1646 drv->has_capability = 1;
1648 wpa_printf(MSG_DEBUG, "NDIS: no WPA support found");
1651 wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1652 "enc 0x%x auth 0x%x",
1653 drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1657 static void wpa_driver_ndis_get_capability(struct wpa_driver_ndis_data *drv)
1662 NDIS_802_11_CAPABILITY *c;
1664 drv->capa.flags = WPA_DRIVER_FLAGS_DRIVER_IE;
1666 len = ndis_get_oid(drv, OID_802_11_CAPABILITY, buf, sizeof(buf));
1668 wpa_driver_ndis_get_wpa_capability(drv);
1672 wpa_hexdump(MSG_MSGDUMP, "OID_802_11_CAPABILITY", (u8 *) buf, len);
1673 c = (NDIS_802_11_CAPABILITY *) buf;
1674 if (len < sizeof(*c) || c->Version != 2) {
1675 wpa_printf(MSG_DEBUG, "NDIS: unsupported "
1676 "OID_802_11_CAPABILITY data");
1679 wpa_printf(MSG_DEBUG, "NDIS: Driver supports OID_802_11_CAPABILITY - "
1680 "NoOfPMKIDs %d NoOfAuthEncrPairs %d",
1681 (int) c->NoOfPMKIDs,
1682 (int) c->NoOfAuthEncryptPairsSupported);
1683 drv->has_capability = 1;
1684 drv->no_of_pmkid = c->NoOfPMKIDs;
1685 for (i = 0; i < c->NoOfAuthEncryptPairsSupported; i++) {
1686 NDIS_802_11_AUTHENTICATION_ENCRYPTION *ae;
1687 ae = &c->AuthenticationEncryptionSupported[i];
1688 if ((char *) (ae + 1) > buf + len) {
1689 wpa_printf(MSG_DEBUG, "NDIS: auth/encr pair list "
1693 wpa_printf(MSG_MSGDUMP, "NDIS: %d - auth %d encr %d",
1694 i, (int) ae->AuthModeSupported,
1695 (int) ae->EncryptStatusSupported);
1696 switch (ae->AuthModeSupported) {
1697 case Ndis802_11AuthModeOpen:
1698 drv->capa.auth |= WPA_DRIVER_AUTH_OPEN;
1700 case Ndis802_11AuthModeShared:
1701 drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1703 case Ndis802_11AuthModeWPA:
1704 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1706 case Ndis802_11AuthModeWPAPSK:
1707 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1709 case Ndis802_11AuthModeWPA2:
1710 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2;
1712 case Ndis802_11AuthModeWPA2PSK:
1713 drv->capa.key_mgmt |=
1714 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1716 case Ndis802_11AuthModeWPANone:
1717 drv->capa.key_mgmt |=
1718 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE;
1723 switch (ae->EncryptStatusSupported) {
1724 case Ndis802_11Encryption1Enabled:
1725 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40;
1726 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP104;
1728 case Ndis802_11Encryption2Enabled:
1729 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1731 case Ndis802_11Encryption3Enabled:
1732 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1739 wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1740 "enc 0x%x auth 0x%x",
1741 drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1745 static int wpa_driver_ndis_get_capa(void *priv, struct wpa_driver_capa *capa)
1747 struct wpa_driver_ndis_data *drv = priv;
1748 if (!drv->has_capability)
1750 os_memcpy(capa, &drv->capa, sizeof(*capa));
1755 static const char * wpa_driver_ndis_get_ifname(void *priv)
1757 struct wpa_driver_ndis_data *drv = priv;
1762 static const u8 * wpa_driver_ndis_get_mac_addr(void *priv)
1764 struct wpa_driver_ndis_data *drv = priv;
1765 return drv->own_addr;
1771 #define NDISUIO_MSG_SIZE (sizeof(NDISUIO_DEVICE_NOTIFICATION) + 512)
1773 static void ndisuio_notification_receive(void *eloop_data, void *user_ctx)
1775 struct wpa_driver_ndis_data *drv = eloop_data;
1776 NDISUIO_DEVICE_NOTIFICATION *hdr;
1777 u8 buf[NDISUIO_MSG_SIZE];
1780 if (!ReadMsgQueue(drv->event_queue, buf, NDISUIO_MSG_SIZE, &len, 0,
1782 wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1783 "ReadMsgQueue failed: %d", (int) GetLastError());
1787 if (len < sizeof(NDISUIO_DEVICE_NOTIFICATION)) {
1788 wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1789 "Too short message (len=%d)", (int) len);
1793 hdr = (NDISUIO_DEVICE_NOTIFICATION *) buf;
1794 wpa_printf(MSG_DEBUG, "NDIS: Notification received: len=%d type=0x%x",
1795 (int) len, hdr->dwNotificationType);
1797 switch (hdr->dwNotificationType) {
1798 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL
1799 case NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL:
1800 wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_ARRIVAL");
1801 wpa_driver_ndis_event_adapter_arrival(drv);
1804 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
1805 case NDISUIO_NOTIFICATION_ADAPTER_REMOVAL:
1806 wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_REMOVAL");
1807 wpa_driver_ndis_event_adapter_removal(drv);
1810 case NDISUIO_NOTIFICATION_MEDIA_CONNECT:
1811 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_CONNECT");
1812 SetEvent(drv->connected_event);
1813 wpa_driver_ndis_event_connect(drv);
1815 case NDISUIO_NOTIFICATION_MEDIA_DISCONNECT:
1816 ResetEvent(drv->connected_event);
1817 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_DISCONNECT");
1818 wpa_driver_ndis_event_disconnect(drv);
1820 case NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION:
1821 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_SPECIFIC_NOTIFICATION");
1822 #if _WIN32_WCE == 420 || _WIN32_WCE == 0x420
1823 wpa_driver_ndis_event_media_specific(
1824 drv, hdr->pvStatusBuffer, hdr->uiStatusBufferSize);
1826 wpa_driver_ndis_event_media_specific(
1827 drv, ((const u8 *) hdr) + hdr->uiOffsetToStatusBuffer,
1828 (size_t) hdr->uiStatusBufferSize);
1832 wpa_printf(MSG_DEBUG, "NDIS: Unknown notification type 0x%x",
1833 hdr->dwNotificationType);
1839 static void ndisuio_notification_deinit(struct wpa_driver_ndis_data *drv)
1841 NDISUIO_REQUEST_NOTIFICATION req;
1843 memset(&req, 0, sizeof(req));
1844 req.hMsgQueue = drv->event_queue;
1845 req.dwNotificationTypes = 0;
1847 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION,
1848 &req, sizeof(req), NULL, 0, NULL, NULL)) {
1849 wpa_printf(MSG_INFO, "ndisuio_notification_deinit: "
1850 "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d",
1851 (int) GetLastError());
1854 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_CANCEL_NOTIFICATION,
1855 NULL, 0, NULL, 0, NULL, NULL)) {
1856 wpa_printf(MSG_INFO, "ndisuio_notification_deinit: "
1857 "IOCTL_NDISUIO_CANCEL_NOTIFICATION failed: %d",
1858 (int) GetLastError());
1861 if (drv->event_queue) {
1862 eloop_unregister_event(drv->event_queue,
1863 sizeof(drv->event_queue));
1864 CloseHandle(drv->event_queue);
1865 drv->event_queue = NULL;
1868 if (drv->connected_event) {
1869 CloseHandle(drv->connected_event);
1870 drv->connected_event = NULL;
1875 static int ndisuio_notification_init(struct wpa_driver_ndis_data *drv)
1877 MSGQUEUEOPTIONS opt;
1878 NDISUIO_REQUEST_NOTIFICATION req;
1880 drv->connected_event =
1881 CreateEvent(NULL, TRUE, FALSE, TEXT("WpaSupplicantConnected"));
1882 if (drv->connected_event == NULL) {
1883 wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1884 "CreateEvent failed: %d",
1885 (int) GetLastError());
1889 memset(&opt, 0, sizeof(opt));
1890 opt.dwSize = sizeof(opt);
1891 opt.dwMaxMessages = 5;
1892 opt.cbMaxMessage = NDISUIO_MSG_SIZE;
1893 opt.bReadAccess = TRUE;
1895 drv->event_queue = CreateMsgQueue(NULL, &opt);
1896 if (drv->event_queue == NULL) {
1897 wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1898 "CreateMsgQueue failed: %d",
1899 (int) GetLastError());
1900 ndisuio_notification_deinit(drv);
1904 memset(&req, 0, sizeof(req));
1905 req.hMsgQueue = drv->event_queue;
1906 req.dwNotificationTypes =
1907 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL
1908 NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL |
1910 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
1911 NDISUIO_NOTIFICATION_ADAPTER_REMOVAL |
1913 NDISUIO_NOTIFICATION_MEDIA_CONNECT |
1914 NDISUIO_NOTIFICATION_MEDIA_DISCONNECT |
1915 NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION;
1917 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION,
1918 &req, sizeof(req), NULL, 0, NULL, NULL)) {
1919 wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1920 "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d",
1921 (int) GetLastError());
1922 ndisuio_notification_deinit(drv);
1926 eloop_register_event(drv->event_queue, sizeof(drv->event_queue),
1927 ndisuio_notification_receive, drv, NULL);
1931 #endif /* _WIN32_WCE */
1934 static int wpa_driver_ndis_get_names(struct wpa_driver_ndis_data *drv)
1936 #ifdef CONFIG_USE_NDISUIO
1937 NDISUIO_QUERY_BINDING *b;
1938 size_t blen = sizeof(*b) + 1024;
1939 int i, error, found = 0;
1941 char name[256], desc[256], *dpos;
1943 size_t j, len, dlen;
1945 b = os_malloc(blen);
1949 for (i = 0; ; i++) {
1950 os_memset(b, 0, blen);
1951 b->BindingIndex = i;
1952 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
1953 b, sizeof(NDISUIO_QUERY_BINDING), b, blen,
1955 error = (int) GetLastError();
1956 if (error == ERROR_NO_MORE_ITEMS)
1958 wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
1959 "failed: %d", error);
1963 pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
1964 len = b->DeviceNameLength;
1965 if (len >= sizeof(name))
1966 len = sizeof(name) - 1;
1967 for (j = 0; j < len; j++)
1968 name[j] = (char) pos[j];
1971 pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
1972 len = b->DeviceDescrLength;
1973 if (len >= sizeof(desc))
1974 len = sizeof(desc) - 1;
1975 for (j = 0; j < len; j++)
1976 desc[j] = (char) pos[j];
1979 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
1981 if (os_strstr(name, drv->ifname)) {
1982 wpa_printf(MSG_DEBUG, "NDIS: Interface name match");
1987 if (os_strncmp(desc, drv->ifname, os_strlen(drv->ifname)) == 0)
1989 wpa_printf(MSG_DEBUG, "NDIS: Interface description "
1997 wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
2003 os_strlcpy(drv->ifname,
2004 os_strncmp(name, "\\DEVICE\\", 8) == 0 ? name + 8 : name,
2005 sizeof(drv->ifname));
2007 drv->adapter_name = wpa_strdup_tchar(drv->ifname);
2008 if (drv->adapter_name == NULL) {
2009 wpa_printf(MSG_ERROR, "NDIS: Failed to allocate memory for "
2014 #endif /* _WIN32_WCE */
2016 dpos = os_strstr(desc, " - ");
2020 dlen = os_strlen(desc);
2021 drv->adapter_desc = os_malloc(dlen + 1);
2022 if (drv->adapter_desc) {
2023 os_memcpy(drv->adapter_desc, desc, dlen);
2024 drv->adapter_desc[dlen] = '\0';
2029 if (drv->adapter_desc == NULL)
2032 wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2036 #else /* CONFIG_USE_NDISUIO */
2038 char *names, *pos, *pos2;
2041 #define MAX_ADAPTERS 32
2042 char *name[MAX_ADAPTERS];
2043 char *desc[MAX_ADAPTERS];
2044 int num_name, num_desc, i, found_name, found_desc;
2047 wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
2048 PacketGetVersion());
2051 _names = os_zalloc(len);
2055 res = PacketGetAdapterNames(_names, &len);
2056 if (!res && len > 8192) {
2058 _names = os_zalloc(len);
2061 res = PacketGetAdapterNames(_names, &len);
2065 wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
2066 "(PacketGetAdapterNames)");
2071 names = (char *) _names;
2072 if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') {
2073 wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in "
2075 /* Convert to ASCII */
2077 while (pos2 < names + len) {
2078 if (pos2[0] == '\0' && pos2[1] == '\0' &&
2079 pos2[2] == '\0' && pos2[3] == '\0') {
2086 os_memcpy(pos + 2, names, pos - names);
2092 while (pos < names + len) {
2093 name[num_name] = pos;
2094 while (*pos && pos < names + len)
2096 if (pos + 1 >= names + len) {
2102 if (num_name >= MAX_ADAPTERS) {
2103 wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
2108 wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
2116 while (pos < names + len) {
2117 desc[num_desc] = pos;
2118 while (*pos && pos < names + len)
2120 if (pos + 1 >= names + len) {
2126 if (num_desc >= MAX_ADAPTERS) {
2127 wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
2133 wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
2141 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
2142 * descriptions. Fill in dummy descriptors to work around this.
2144 while (num_desc < num_name)
2145 desc[num_desc++] = "dummy description";
2147 if (num_name != num_desc) {
2148 wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and "
2149 "description counts (%d != %d)",
2150 num_name, num_desc);
2155 found_name = found_desc = -1;
2156 for (i = 0; i < num_name; i++) {
2157 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s",
2158 i, name[i], desc[i]);
2159 if (found_name == -1 && os_strstr(name[i], drv->ifname))
2161 if (found_desc == -1 &&
2162 os_strncmp(desc[i], drv->ifname, os_strlen(drv->ifname)) ==
2167 if (found_name < 0 && found_desc >= 0) {
2168 wpa_printf(MSG_DEBUG, "NDIS: Matched interface '%s' based on "
2170 name[found_desc], desc[found_desc]);
2171 found_name = found_desc;
2172 os_strlcpy(drv->ifname,
2173 os_strncmp(name[found_desc], "\\Device\\NPF_", 12)
2174 == 0 ? name[found_desc] + 12 : name[found_desc],
2175 sizeof(drv->ifname));
2178 if (found_name < 0) {
2179 wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
2186 pos = os_strrchr(desc[i], '(');
2188 dlen = pos - desc[i];
2190 if (pos > desc[i] && *pos == ' ')
2193 dlen = os_strlen(desc[i]);
2195 drv->adapter_desc = os_malloc(dlen + 1);
2196 if (drv->adapter_desc) {
2197 os_memcpy(drv->adapter_desc, desc[i], dlen);
2198 drv->adapter_desc[dlen] = '\0';
2203 if (drv->adapter_desc == NULL)
2206 wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2210 #endif /* CONFIG_USE_NDISUIO */
2214 #if defined(CONFIG_NATIVE_WINDOWS) || defined(__CYGWIN__)
2217 * These structures are undocumented for WinXP; only WinCE version is
2218 * documented. These would be included wzcsapi.h if it were available. Some
2219 * changes here have been needed to make the structures match with WinXP SP2.
2220 * It is unclear whether these work with any other version.
2225 } INTF_KEY_ENTRY, *PINTF_KEY_ENTRY;
2229 PINTF_KEY_ENTRY pIntfs;
2230 } INTFS_KEY_TABLE, *PINTFS_KEY_TABLE;
2235 } RAW_DATA, *PRAW_DATA;
2242 ULONG ulPhysicalMediaType;
2247 u8 pad[2]; /* why is this needed? */
2248 #endif /* _WIN32_WCE */
2250 DWORD dwCapabilities; /* something added for WinXP SP2(?) */
2253 RAW_DATA rdBSSIDList;
2254 RAW_DATA rdStSSIDList;
2255 RAW_DATA rdCtrlData;
2259 DWORD nWPAMCastCipher;
2260 /* add some extra buffer for later additions since this interface is
2261 * far from stable */
2262 u8 later_additions[100];
2263 } INTF_ENTRY, *PINTF_ENTRY;
2265 #define INTF_ALL 0xffffffff
2266 #define INTF_ALL_FLAGS 0x0000ffff
2267 #define INTF_CTLFLAGS 0x00000010
2268 #define INTFCTL_ENABLED 0x8000
2269 #endif /* _WIN32_WCE */
2273 static int wpa_driver_ndis_rebind_adapter(struct wpa_driver_ndis_data *drv)
2279 len = _tcslen(drv->adapter_name);
2283 ndis = CreateFile(DD_NDIS_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE,
2284 0, NULL, OPEN_EXISTING, 0, NULL);
2285 if (ndis == INVALID_HANDLE_VALUE) {
2286 wpa_printf(MSG_DEBUG, "NDIS: Failed to open file to NDIS "
2287 "device: %d", (int) GetLastError());
2292 memcpy(multi, drv->adapter_name, len * sizeof(TCHAR));
2293 memcpy(&multi[len], TEXT("NDISUIO\0"), 9 * sizeof(TCHAR));
2296 if (!DeviceIoControl(ndis, IOCTL_NDIS_REBIND_ADAPTER,
2297 multi, len * sizeof(TCHAR), NULL, 0, NULL, NULL))
2299 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDIS_REBIND_ADAPTER "
2300 "failed: 0x%x", (int) GetLastError());
2301 wpa_hexdump_ascii(MSG_DEBUG, "NDIS: rebind multi_sz",
2302 (u8 *) multi, len * sizeof(TCHAR));
2309 wpa_printf(MSG_DEBUG, "NDIS: Requested NDIS rebind of NDISUIO "
2314 #endif /* _WIN32_WCE */
2317 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2324 TCHAR keyname[256], devname[256];
2326 #define WZC_DRIVER TEXT("Drivers\\BuiltIn\\ZeroConfig")
2330 h = ActivateDeviceEx(WZC_DRIVER, NULL, 0, NULL);
2331 if (h == INVALID_HANDLE_VALUE || h == 0) {
2332 wpa_printf(MSG_DEBUG, "NDIS: Failed to re-enable WZC "
2333 "- ActivateDeviceEx failed: %d",
2334 (int) GetLastError());
2338 wpa_printf(MSG_DEBUG, "NDIS: WZC re-enabled");
2339 return wpa_driver_ndis_rebind_adapter(drv);
2343 * Unfortunately, just disabling the WZC for an interface is not enough
2344 * to free NDISUIO for us, so need to disable and unload WZC completely
2345 * for now when using WinCE with NDISUIO. In addition, must request
2346 * NDISUIO protocol to be rebound to the adapter in order to free the
2347 * NDISUIO binding that WZC hold before us.
2350 /* Enumerate HKLM\Drivers\Active\* to find a handle to WZC. */
2351 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, DEVLOAD_ACTIVE_KEY, 0, 0, &hk);
2352 if (ret != ERROR_SUCCESS) {
2353 wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(DEVLOAD_ACTIVE_KEY) "
2354 "failed: %d %d", (int) ret, (int) GetLastError());
2358 for (i = 0; ; i++) {
2359 len = sizeof(keyname);
2360 ret = RegEnumKeyEx(hk, i, keyname, &len, NULL, NULL, NULL,
2362 if (ret != ERROR_SUCCESS) {
2363 wpa_printf(MSG_DEBUG, "NDIS: Could not find active "
2364 "WZC - assuming it is not running.");
2369 ret = RegOpenKeyEx(hk, keyname, 0, 0, &hk2);
2370 if (ret != ERROR_SUCCESS) {
2371 wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(active dev) "
2373 (int) ret, (int) GetLastError());
2377 len = sizeof(devname);
2378 ret = RegQueryValueEx(hk2, DEVLOAD_DEVKEY_VALNAME, NULL, NULL,
2379 (LPBYTE) devname, &len);
2380 if (ret != ERROR_SUCCESS) {
2381 wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx("
2382 "DEVKEY_VALNAME) failed: %d %d",
2383 (int) ret, (int) GetLastError());
2388 if (_tcscmp(devname, WZC_DRIVER) == 0)
2396 /* Found WZC - get handle to it. */
2398 ret = RegQueryValueEx(hk2, DEVLOAD_HANDLE_VALNAME, NULL, NULL,
2399 (PUCHAR) &hnd, &len);
2400 if (ret != ERROR_SUCCESS) {
2401 wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx(HANDLE_VALNAME) "
2402 "failed: %d %d", (int) ret, (int) GetLastError());
2409 /* Deactivate WZC */
2410 if (!DeactivateDevice((HANDLE) hnd)) {
2411 wpa_printf(MSG_DEBUG, "NDIS: DeactivateDevice failed: %d",
2412 (int) GetLastError());
2416 wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily");
2417 drv->wzc_disabled = 1;
2418 return wpa_driver_ndis_rebind_adapter(drv);
2420 #else /* _WIN32_WCE */
2423 DWORD (WINAPI *wzc_enum_interf)(LPWSTR pSrvAddr,
2424 PINTFS_KEY_TABLE pIntfs);
2425 DWORD (WINAPI *wzc_query_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2427 LPDWORD pdwOutFlags);
2428 DWORD (WINAPI *wzc_set_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2429 PINTF_ENTRY pIntf, LPDWORD pdwOutFlags);
2432 INTFS_KEY_TABLE guids;
2438 hm = LoadLibrary(TEXT("wzcsapi.dll"));
2440 wpa_printf(MSG_DEBUG, "NDIS: Failed to load wzcsapi.dll (%u) "
2441 "- WZC probably not running",
2442 (unsigned int) GetLastError());
2447 wzc_enum_interf = (void *) GetProcAddressA(hm, "WZCEnumInterfaces");
2448 wzc_query_interf = (void *) GetProcAddressA(hm, "WZCQueryInterface");
2449 wzc_set_interf = (void *) GetProcAddressA(hm, "WZCSetInterface");
2450 #else /* _WIN32_WCE */
2451 wzc_enum_interf = (void *) GetProcAddress(hm, "WZCEnumInterfaces");
2452 wzc_query_interf = (void *) GetProcAddress(hm, "WZCQueryInterface");
2453 wzc_set_interf = (void *) GetProcAddress(hm, "WZCSetInterface");
2454 #endif /* _WIN32_WCE */
2456 if (wzc_enum_interf == NULL || wzc_query_interf == NULL ||
2457 wzc_set_interf == NULL) {
2458 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces, "
2459 "WZCQueryInterface, or WZCSetInterface not found "
2464 os_memset(&guids, 0, sizeof(guids));
2465 res = wzc_enum_interf(NULL, &guids);
2467 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces failed: %d; "
2468 "WZC service is apparently not running",
2473 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces: %d interfaces",
2474 (int) guids.dwNumIntfs);
2476 for (i = 0; i < guids.dwNumIntfs; i++) {
2477 pos = guids.pIntfs[i].wszGuid;
2478 for (j = 0; j < sizeof(guid); j++) {
2479 guid[j] = (char) *pos;
2484 guid[sizeof(guid) - 1] = '\0';
2485 wpa_printf(MSG_DEBUG, "NDIS: intfs %d GUID '%s'",
2487 if (os_strstr(drv->ifname, guid) == NULL)
2490 wpa_printf(MSG_DEBUG, "NDIS: Current interface found from "
2495 if (i >= guids.dwNumIntfs) {
2496 wpa_printf(MSG_DEBUG, "NDIS: Current interface not found from "
2501 os_memset(&intf, 0, sizeof(intf));
2502 intf.wszGuid = guids.pIntfs[i].wszGuid;
2503 /* Set flags to verify that the structure has not changed. */
2504 intf.dwCtlFlags = -1;
2506 res = wzc_query_interf(NULL, INTFCTL_ENABLED, &intf, &flags);
2508 wpa_printf(MSG_DEBUG, "NDIS: Could not query flags for the "
2509 "WZC interface: %d (0x%x)",
2510 (int) res, (int) res);
2511 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2512 (unsigned int) GetLastError());
2516 wpa_printf(MSG_DEBUG, "NDIS: WZC interface flags 0x%x dwCtlFlags 0x%x",
2517 (int) flags, (int) intf.dwCtlFlags);
2519 if (intf.dwCtlFlags == -1) {
2520 wpa_printf(MSG_DEBUG, "NDIS: Looks like wzcsapi has changed "
2521 "again - could not disable WZC");
2522 wpa_hexdump(MSG_MSGDUMP, "NDIS: intf",
2523 (u8 *) &intf, sizeof(intf));
2528 if (!(intf.dwCtlFlags & INTFCTL_ENABLED)) {
2529 wpa_printf(MSG_DEBUG, "NDIS: Enabling WZC for this "
2531 intf.dwCtlFlags |= INTFCTL_ENABLED;
2532 res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2535 wpa_printf(MSG_DEBUG, "NDIS: Failed to enable "
2537 (int) res, (int) res);
2538 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2539 (unsigned int) GetLastError());
2542 wpa_printf(MSG_DEBUG, "NDIS: Re-enabled WZC for this "
2544 drv->wzc_disabled = 0;
2547 if (intf.dwCtlFlags & INTFCTL_ENABLED) {
2548 wpa_printf(MSG_DEBUG, "NDIS: Disabling WZC for this "
2550 intf.dwCtlFlags &= ~INTFCTL_ENABLED;
2551 res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2554 wpa_printf(MSG_DEBUG, "NDIS: Failed to "
2555 "disable WZC: %d (0x%x)",
2556 (int) res, (int) res);
2557 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2558 (unsigned int) GetLastError());
2561 wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily "
2562 "for this interface");
2563 drv->wzc_disabled = 1;
2565 wpa_printf(MSG_DEBUG, "NDIS: WZC was not enabled for "
2576 #endif /* _WIN32_WCE */
2579 #else /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2581 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2587 #endif /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2590 #ifdef CONFIG_USE_NDISUIO
2592 * l2_packet_ndis.c is sharing the same handle to NDISUIO, so we must be able
2593 * to export this handle. This is somewhat ugly, but there is no better
2594 * mechanism available to pass data from driver interface to l2_packet wrapper.
2596 static HANDLE driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2598 HANDLE driver_ndis_get_ndisuio_handle(void)
2600 return driver_ndis_ndisuio_handle;
2602 #endif /* CONFIG_USE_NDISUIO */
2605 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv)
2607 #ifdef CONFIG_USE_NDISUIO
2609 #define NDISUIO_DEVICE_NAME TEXT("\\\\.\\\\Ndisuio")
2611 #endif /* _WIN32_WCE */
2612 drv->ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2613 GENERIC_READ | GENERIC_WRITE, 0, NULL,
2615 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
2616 INVALID_HANDLE_VALUE);
2617 if (drv->ndisuio == INVALID_HANDLE_VALUE) {
2618 wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to "
2619 "NDISUIO: %d", (int) GetLastError());
2622 driver_ndis_ndisuio_handle = drv->ndisuio;
2625 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0,
2626 NULL, 0, &written, NULL)) {
2627 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: "
2628 "%d", (int) GetLastError());
2629 CloseHandle(drv->ndisuio);
2630 drv->ndisuio = INVALID_HANDLE_VALUE;
2633 #endif /* _WIN32_WCE */
2636 #else /* CONFIG_USE_NDISUIO */
2638 #endif /* CONFIG_USE_NDISUIO */
2642 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv)
2644 #ifdef CONFIG_USE_NDISUIO
2646 #define MAX_NDIS_DEVICE_NAME_LEN 256
2647 WCHAR ifname[MAX_NDIS_DEVICE_NAME_LEN];
2649 const char *prefix = "\\DEVICE\\";
2653 #else /* _WIN32_WCE */
2655 #endif /* _WIN32_WCE */
2656 len = pos + os_strlen(drv->ifname);
2657 if (len >= MAX_NDIS_DEVICE_NAME_LEN)
2659 for (i = 0; i < pos; i++)
2660 ifname[i] = (WCHAR) prefix[i];
2661 for (i = pos; i < len; i++)
2662 ifname[i] = (WCHAR) drv->ifname[i - pos];
2665 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_OPEN_DEVICE,
2666 ifname, len * sizeof(WCHAR), NULL, 0, &written,
2668 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_OPEN_DEVICE "
2669 "failed: %d", (int) GetLastError());
2670 wpa_hexdump_ascii(MSG_DEBUG, "NDIS: ifname",
2671 (const u8 *) ifname, len * sizeof(WCHAR));
2672 CloseHandle(drv->ndisuio);
2673 drv->ndisuio = INVALID_HANDLE_VALUE;
2677 wpa_printf(MSG_DEBUG, "NDIS: Opened NDISUIO device successfully");
2680 #else /* CONFIG_USE_NDISUIO */
2682 os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", drv->ifname);
2683 drv->adapter = PacketOpenAdapter(ifname);
2684 if (drv->adapter == NULL) {
2685 wpa_printf(MSG_DEBUG, "NDIS: PacketOpenAdapter failed for "
2690 #endif /* CONFIG_USE_NDISUIO */
2694 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv)
2696 #ifdef CONFIG_USE_NDISUIO
2697 driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2698 if (drv->ndisuio != INVALID_HANDLE_VALUE)
2699 CloseHandle(drv->ndisuio);
2700 #else /* CONFIG_USE_NDISUIO */
2702 PacketCloseAdapter(drv->adapter);
2703 #endif /* CONFIG_USE_NDISUIO */
2707 static int ndis_add_multicast(struct wpa_driver_ndis_data *drv)
2709 if (ndis_set_oid(drv, OID_802_3_MULTICAST_LIST,
2710 (const char *) pae_group_addr, ETH_ALEN) < 0) {
2711 wpa_printf(MSG_DEBUG, "NDIS: Failed to add PAE group address "
2712 "to the multicast list");
2720 static void * wpa_driver_ndis_init(void *ctx, const char *ifname)
2722 struct wpa_driver_ndis_data *drv;
2725 drv = os_zalloc(sizeof(*drv));
2730 * Compatibility code to strip possible prefix from the GUID. Previous
2731 * versions include \Device\NPF_ prefix for all names, but the internal
2732 * interface name is now only the GUI. Both Packet32 and NDISUIO
2733 * prefixes are supported.
2735 if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0)
2737 else if (os_strncmp(ifname, "\\DEVICE\\", 8) == 0)
2739 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
2741 if (wpa_driver_ndis_adapter_init(drv) < 0) {
2746 if (wpa_driver_ndis_get_names(drv) < 0) {
2747 wpa_driver_ndis_adapter_close(drv);
2752 wpa_driver_ndis_set_wzc(drv, 0);
2754 if (wpa_driver_ndis_adapter_open(drv) < 0) {
2755 wpa_driver_ndis_adapter_close(drv);
2760 if (ndis_get_oid(drv, OID_802_3_CURRENT_ADDRESS,
2761 (char *) drv->own_addr, ETH_ALEN) < 0) {
2762 wpa_printf(MSG_DEBUG, "NDIS: Get OID_802_3_CURRENT_ADDRESS "
2764 wpa_driver_ndis_adapter_close(drv);
2768 wpa_driver_ndis_get_capability(drv);
2770 /* Make sure that the driver does not have any obsolete PMKID entries.
2772 wpa_driver_ndis_flush_pmkid(drv);
2775 * Disconnect to make sure that driver re-associates if it was
2778 wpa_driver_ndis_disconnect(drv);
2780 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout, drv, NULL);
2782 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2783 drv->events = ndis_events_init(&drv->events_pipe, &drv->event_avail,
2784 drv->ifname, drv->adapter_desc);
2785 if (drv->events == NULL) {
2786 wpa_driver_ndis_deinit(drv);
2789 eloop_register_event(drv->event_avail, sizeof(drv->event_avail),
2790 wpa_driver_ndis_event_pipe_cb, drv, NULL);
2791 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2794 if (ndisuio_notification_init(drv) < 0) {
2795 wpa_driver_ndis_deinit(drv);
2798 #endif /* _WIN32_WCE */
2800 /* Set mode here in case card was configured for ad-hoc mode
2802 mode = Ndis802_11Infrastructure;
2803 if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
2804 (char *) &mode, sizeof(mode)) < 0) {
2805 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
2806 "OID_802_11_INFRASTRUCTURE_MODE (%d)",
2808 /* Try to continue anyway */
2810 if (!drv->has_capability && drv->capa.enc == 0) {
2811 wpa_printf(MSG_DEBUG, "NDIS: Driver did not provide "
2812 "any wireless capabilities - assume it is "
2813 "a wired interface");
2815 ndis_add_multicast(drv);
2823 static void wpa_driver_ndis_deinit(void *priv)
2825 struct wpa_driver_ndis_data *drv = priv;
2827 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2829 eloop_unregister_event(drv->event_avail,
2830 sizeof(drv->event_avail));
2831 ndis_events_deinit(drv->events);
2833 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2836 ndisuio_notification_deinit(drv);
2837 #endif /* _WIN32_WCE */
2839 eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
2840 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
2841 wpa_driver_ndis_flush_pmkid(drv);
2842 wpa_driver_ndis_disconnect(drv);
2843 if (wpa_driver_ndis_radio_off(drv) < 0) {
2844 wpa_printf(MSG_DEBUG, "NDIS: failed to disassociate and turn "
2848 wpa_driver_ndis_adapter_close(drv);
2850 if (drv->wzc_disabled)
2851 wpa_driver_ndis_set_wzc(drv, 1);
2854 os_free(drv->adapter_name);
2855 #endif /* _WIN32_WCE */
2856 os_free(drv->adapter_desc);
2861 static struct wpa_interface_info *
2862 wpa_driver_ndis_get_interfaces(void *global_priv)
2864 struct wpa_interface_info *iface = NULL, *niface;
2866 #ifdef CONFIG_USE_NDISUIO
2867 NDISUIO_QUERY_BINDING *b;
2868 size_t blen = sizeof(*b) + 1024;
2871 char name[256], desc[256];
2876 ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2877 GENERIC_READ | GENERIC_WRITE, 0, NULL,
2879 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
2880 INVALID_HANDLE_VALUE);
2881 if (ndisuio == INVALID_HANDLE_VALUE) {
2882 wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to "
2883 "NDISUIO: %d", (int) GetLastError());
2888 if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0,
2889 NULL, 0, &written, NULL)) {
2890 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: "
2891 "%d", (int) GetLastError());
2892 CloseHandle(ndisuio);
2895 #endif /* _WIN32_WCE */
2897 b = os_malloc(blen);
2899 CloseHandle(ndisuio);
2903 for (i = 0; ; i++) {
2904 os_memset(b, 0, blen);
2905 b->BindingIndex = i;
2906 if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
2907 b, sizeof(NDISUIO_QUERY_BINDING), b, blen,
2909 error = (int) GetLastError();
2910 if (error == ERROR_NO_MORE_ITEMS)
2912 wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
2913 "failed: %d", error);
2917 pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
2918 len = b->DeviceNameLength;
2919 if (len >= sizeof(name))
2920 len = sizeof(name) - 1;
2921 for (j = 0; j < len; j++)
2922 name[j] = (char) pos[j];
2925 pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
2926 len = b->DeviceDescrLength;
2927 if (len >= sizeof(desc))
2928 len = sizeof(desc) - 1;
2929 for (j = 0; j < len; j++)
2930 desc[j] = (char) pos[j];
2933 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
2935 niface = os_zalloc(sizeof(*niface));
2938 niface->drv_name = "ndis";
2939 if (os_strncmp(name, "\\DEVICE\\", 8) == 0)
2940 niface->ifname = os_strdup(name + 8);
2942 niface->ifname = os_strdup(name);
2943 if (niface->ifname == NULL) {
2947 niface->desc = os_strdup(desc);
2948 niface->next = iface;
2953 CloseHandle(ndisuio);
2954 #else /* CONFIG_USE_NDISUIO */
2956 char *names, *pos, *pos2;
2959 char *name[MAX_ADAPTERS];
2960 char *desc[MAX_ADAPTERS];
2961 int num_name, num_desc, i;
2963 wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
2964 PacketGetVersion());
2967 _names = os_zalloc(len);
2971 res = PacketGetAdapterNames(_names, &len);
2972 if (!res && len > 8192) {
2974 _names = os_zalloc(len);
2977 res = PacketGetAdapterNames(_names, &len);
2981 wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
2982 "(PacketGetAdapterNames)");
2987 names = (char *) _names;
2988 if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') {
2989 wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in "
2991 /* Convert to ASCII */
2993 while (pos2 < names + len) {
2994 if (pos2[0] == '\0' && pos2[1] == '\0' &&
2995 pos2[2] == '\0' && pos2[3] == '\0') {
3002 os_memcpy(pos + 2, names, pos - names);
3008 while (pos < names + len) {
3009 name[num_name] = pos;
3010 while (*pos && pos < names + len)
3012 if (pos + 1 >= names + len) {
3018 if (num_name >= MAX_ADAPTERS) {
3019 wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
3024 wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
3032 while (pos < names + len) {
3033 desc[num_desc] = pos;
3034 while (*pos && pos < names + len)
3036 if (pos + 1 >= names + len) {
3042 if (num_desc >= MAX_ADAPTERS) {
3043 wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
3049 wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
3057 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
3058 * descriptions. Fill in dummy descriptors to work around this.
3060 while (num_desc < num_name)
3061 desc[num_desc++] = "dummy description";
3063 if (num_name != num_desc) {
3064 wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and "
3065 "description counts (%d != %d)",
3066 num_name, num_desc);
3071 for (i = 0; i < num_name; i++) {
3072 niface = os_zalloc(sizeof(*niface));
3075 niface->drv_name = "ndis";
3076 if (os_strncmp(name[i], "\\Device\\NPF_", 12) == 0)
3077 niface->ifname = os_strdup(name[i] + 12);
3079 niface->ifname = os_strdup(name[i]);
3080 if (niface->ifname == NULL) {
3084 niface->desc = os_strdup(desc[i]);
3085 niface->next = iface;
3089 #endif /* CONFIG_USE_NDISUIO */
3095 const struct wpa_driver_ops wpa_driver_ndis_ops = {
3097 "Windows NDIS driver",
3098 wpa_driver_ndis_get_bssid,
3099 wpa_driver_ndis_get_ssid,
3100 wpa_driver_ndis_set_wpa,
3101 wpa_driver_ndis_set_key,
3102 wpa_driver_ndis_init,
3103 wpa_driver_ndis_deinit,
3104 NULL /* set_param */,
3105 NULL /* set_countermeasures */,
3106 NULL /* set_drop_unencrypted */,
3107 wpa_driver_ndis_scan,
3108 NULL /* get_scan_results */,
3109 wpa_driver_ndis_deauthenticate,
3110 wpa_driver_ndis_disassociate,
3111 wpa_driver_ndis_associate,
3112 NULL /* set_auth_alg */,
3113 wpa_driver_ndis_add_pmkid,
3114 wpa_driver_ndis_remove_pmkid,
3115 wpa_driver_ndis_flush_pmkid,
3116 wpa_driver_ndis_get_capa,
3117 wpa_driver_ndis_poll,
3118 wpa_driver_ndis_get_ifname,
3119 wpa_driver_ndis_get_mac_addr,
3120 NULL /* send_eapol */,
3121 NULL /* set_operstate */,
3122 NULL /* mlme_setprotection */,
3123 NULL /* get_hw_feature_data */,
3124 NULL /* set_channel */,
3125 NULL /* set_ssid */,
3126 NULL /* set_bssid */,
3127 NULL /* send_mlme */,
3128 NULL /* mlme_add_sta */,
3129 NULL /* mlme_remove_sta */,
3130 NULL /* update_ft_ies */,
3131 NULL /* send_ft_action */,
3132 wpa_driver_ndis_get_scan_results,
3133 NULL /* set_probe_req_ie */,
3134 NULL /* set_mode */,
3135 NULL /* set_country */,
3136 NULL /* global_init */,
3137 NULL /* global_deinit */,
3139 wpa_driver_ndis_get_interfaces