Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / src / drivers / driver_ndis.c
blobda4f90f41655ec2ab1e5f590510993af0e3b16c4
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #ifdef __CYGWIN__
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"
19 int close(int fd);
20 #else /* __CYGWIN__ */
21 #include "includes.h"
22 #endif /* __CYGWIN__ */
23 #ifdef CONFIG_USE_NDISUIO
24 #include <winsock2.h>
25 #else /* CONFIG_USE_NDISUIO */
26 #include <Packet32.h>
27 #endif /* CONFIG_USE_NDISUIO */
28 #include <ntddndis.h>
30 #ifdef _WIN32_WCE
31 #include <winioctl.h>
32 #include <nuiouser.h>
33 #include <devload.h>
34 #endif /* _WIN32_WCE */
36 #include "common.h"
37 #include "driver.h"
38 #include "eloop.h"
39 #include "ieee802_11_defs.h"
40 #include "driver_ndis.h"
42 int wpa_driver_register_event_cb(struct wpa_driver_ndis_data *drv);
43 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
44 void wpa_driver_ndis_event_pipe_cb(void *eloop_data, void *user_data);
45 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
47 static void wpa_driver_ndis_deinit(void *priv);
48 static void wpa_driver_ndis_poll(void *drv);
49 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx);
50 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv);
51 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv);
52 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv);
55 /* FIX: to be removed once this can be compiled with the complete NDIS
56 * header files */
57 #ifndef OID_802_11_BSSID
58 #define OID_802_11_BSSID 0x0d010101
59 #define OID_802_11_SSID 0x0d010102
60 #define OID_802_11_INFRASTRUCTURE_MODE 0x0d010108
61 #define OID_802_11_ADD_WEP 0x0D010113
62 #define OID_802_11_REMOVE_WEP 0x0D010114
63 #define OID_802_11_DISASSOCIATE 0x0D010115
64 #define OID_802_11_BSSID_LIST 0x0d010217
65 #define OID_802_11_AUTHENTICATION_MODE 0x0d010118
66 #define OID_802_11_PRIVACY_FILTER 0x0d010119
67 #define OID_802_11_BSSID_LIST_SCAN 0x0d01011A
68 #define OID_802_11_WEP_STATUS 0x0d01011B
69 #define OID_802_11_ENCRYPTION_STATUS OID_802_11_WEP_STATUS
70 #define OID_802_11_ADD_KEY 0x0d01011D
71 #define OID_802_11_REMOVE_KEY 0x0d01011E
72 #define OID_802_11_ASSOCIATION_INFORMATION 0x0d01011F
73 #define OID_802_11_TEST 0x0d010120
74 #define OID_802_11_CAPABILITY 0x0d010122
75 #define OID_802_11_PMKID 0x0d010123
77 #define NDIS_802_11_LENGTH_SSID 32
78 #define NDIS_802_11_LENGTH_RATES 8
79 #define NDIS_802_11_LENGTH_RATES_EX 16
81 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
83 typedef struct NDIS_802_11_SSID {
84 ULONG SsidLength;
85 UCHAR Ssid[NDIS_802_11_LENGTH_SSID];
86 } NDIS_802_11_SSID;
88 typedef LONG NDIS_802_11_RSSI;
90 typedef enum NDIS_802_11_NETWORK_TYPE {
91 Ndis802_11FH,
92 Ndis802_11DS,
93 Ndis802_11OFDM5,
94 Ndis802_11OFDM24,
95 Ndis802_11NetworkTypeMax
96 } NDIS_802_11_NETWORK_TYPE;
98 typedef struct NDIS_802_11_CONFIGURATION_FH {
99 ULONG Length;
100 ULONG HopPattern;
101 ULONG HopSet;
102 ULONG DwellTime;
103 } NDIS_802_11_CONFIGURATION_FH;
105 typedef struct NDIS_802_11_CONFIGURATION {
106 ULONG Length;
107 ULONG BeaconPeriod;
108 ULONG ATIMWindow;
109 ULONG DSConfig;
110 NDIS_802_11_CONFIGURATION_FH FHConfig;
111 } NDIS_802_11_CONFIGURATION;
113 typedef enum NDIS_802_11_NETWORK_INFRASTRUCTURE {
114 Ndis802_11IBSS,
115 Ndis802_11Infrastructure,
116 Ndis802_11AutoUnknown,
117 Ndis802_11InfrastructureMax
118 } NDIS_802_11_NETWORK_INFRASTRUCTURE;
120 typedef enum NDIS_802_11_AUTHENTICATION_MODE {
121 Ndis802_11AuthModeOpen,
122 Ndis802_11AuthModeShared,
123 Ndis802_11AuthModeAutoSwitch,
124 Ndis802_11AuthModeWPA,
125 Ndis802_11AuthModeWPAPSK,
126 Ndis802_11AuthModeWPANone,
127 Ndis802_11AuthModeWPA2,
128 Ndis802_11AuthModeWPA2PSK,
129 Ndis802_11AuthModeMax
130 } NDIS_802_11_AUTHENTICATION_MODE;
132 typedef enum NDIS_802_11_WEP_STATUS {
133 Ndis802_11WEPEnabled,
134 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
135 Ndis802_11WEPDisabled,
136 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
137 Ndis802_11WEPKeyAbsent,
138 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
139 Ndis802_11WEPNotSupported,
140 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
141 Ndis802_11Encryption2Enabled,
142 Ndis802_11Encryption2KeyAbsent,
143 Ndis802_11Encryption3Enabled,
144 Ndis802_11Encryption3KeyAbsent
145 } NDIS_802_11_WEP_STATUS, NDIS_802_11_ENCRYPTION_STATUS;
147 typedef enum NDIS_802_11_PRIVACY_FILTER {
148 Ndis802_11PrivFilterAcceptAll,
149 Ndis802_11PrivFilter8021xWEP
150 } NDIS_802_11_PRIVACY_FILTER;
152 typedef UCHAR NDIS_802_11_RATES[NDIS_802_11_LENGTH_RATES];
153 typedef UCHAR NDIS_802_11_RATES_EX[NDIS_802_11_LENGTH_RATES_EX];
155 typedef struct NDIS_WLAN_BSSID_EX {
156 ULONG Length;
157 NDIS_802_11_MAC_ADDRESS MacAddress; /* BSSID */
158 UCHAR Reserved[2];
159 NDIS_802_11_SSID Ssid;
160 ULONG Privacy;
161 NDIS_802_11_RSSI Rssi;
162 NDIS_802_11_NETWORK_TYPE NetworkTypeInUse;
163 NDIS_802_11_CONFIGURATION Configuration;
164 NDIS_802_11_NETWORK_INFRASTRUCTURE InfrastructureMode;
165 NDIS_802_11_RATES_EX SupportedRates;
166 ULONG IELength;
167 UCHAR IEs[1];
168 } NDIS_WLAN_BSSID_EX;
170 typedef struct NDIS_802_11_BSSID_LIST_EX {
171 ULONG NumberOfItems;
172 NDIS_WLAN_BSSID_EX Bssid[1];
173 } NDIS_802_11_BSSID_LIST_EX;
175 typedef struct NDIS_802_11_FIXED_IEs {
176 UCHAR Timestamp[8];
177 USHORT BeaconInterval;
178 USHORT Capabilities;
179 } NDIS_802_11_FIXED_IEs;
181 typedef struct NDIS_802_11_WEP {
182 ULONG Length;
183 ULONG KeyIndex;
184 ULONG KeyLength;
185 UCHAR KeyMaterial[1];
186 } NDIS_802_11_WEP;
188 typedef ULONG NDIS_802_11_KEY_INDEX;
189 typedef ULONGLONG NDIS_802_11_KEY_RSC;
191 typedef struct NDIS_802_11_KEY {
192 ULONG Length;
193 ULONG KeyIndex;
194 ULONG KeyLength;
195 NDIS_802_11_MAC_ADDRESS BSSID;
196 NDIS_802_11_KEY_RSC KeyRSC;
197 UCHAR KeyMaterial[1];
198 } NDIS_802_11_KEY;
200 typedef struct NDIS_802_11_REMOVE_KEY {
201 ULONG Length;
202 ULONG KeyIndex;
203 NDIS_802_11_MAC_ADDRESS BSSID;
204 } NDIS_802_11_REMOVE_KEY;
206 typedef struct NDIS_802_11_AI_REQFI {
207 USHORT Capabilities;
208 USHORT ListenInterval;
209 NDIS_802_11_MAC_ADDRESS CurrentAPAddress;
210 } NDIS_802_11_AI_REQFI;
212 typedef struct NDIS_802_11_AI_RESFI {
213 USHORT Capabilities;
214 USHORT StatusCode;
215 USHORT AssociationId;
216 } NDIS_802_11_AI_RESFI;
218 typedef struct NDIS_802_11_ASSOCIATION_INFORMATION {
219 ULONG Length;
220 USHORT AvailableRequestFixedIEs;
221 NDIS_802_11_AI_REQFI RequestFixedIEs;
222 ULONG RequestIELength;
223 ULONG OffsetRequestIEs;
224 USHORT AvailableResponseFixedIEs;
225 NDIS_802_11_AI_RESFI ResponseFixedIEs;
226 ULONG ResponseIELength;
227 ULONG OffsetResponseIEs;
228 } NDIS_802_11_ASSOCIATION_INFORMATION;
230 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
231 NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
232 NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
233 } NDIS_802_11_AUTHENTICATION_ENCRYPTION;
235 typedef struct NDIS_802_11_CAPABILITY {
236 ULONG Length;
237 ULONG Version;
238 ULONG NoOfPMKIDs;
239 ULONG NoOfAuthEncryptPairsSupported;
240 NDIS_802_11_AUTHENTICATION_ENCRYPTION
241 AuthenticationEncryptionSupported[1];
242 } NDIS_802_11_CAPABILITY;
244 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
246 typedef struct BSSID_INFO {
247 NDIS_802_11_MAC_ADDRESS BSSID;
248 NDIS_802_11_PMKID_VALUE PMKID;
249 } BSSID_INFO;
251 typedef struct NDIS_802_11_PMKID {
252 ULONG Length;
253 ULONG BSSIDInfoCount;
254 BSSID_INFO BSSIDInfo[1];
255 } NDIS_802_11_PMKID;
257 typedef enum NDIS_802_11_STATUS_TYPE {
258 Ndis802_11StatusType_Authentication,
259 Ndis802_11StatusType_PMKID_CandidateList = 2,
260 Ndis802_11StatusTypeMax
261 } NDIS_802_11_STATUS_TYPE;
263 typedef struct NDIS_802_11_STATUS_INDICATION {
264 NDIS_802_11_STATUS_TYPE StatusType;
265 } NDIS_802_11_STATUS_INDICATION;
267 typedef struct PMKID_CANDIDATE {
268 NDIS_802_11_MAC_ADDRESS BSSID;
269 ULONG Flags;
270 } PMKID_CANDIDATE;
272 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
274 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
275 ULONG Version;
276 ULONG NumCandidates;
277 PMKID_CANDIDATE CandidateList[1];
278 } NDIS_802_11_PMKID_CANDIDATE_LIST;
280 typedef struct NDIS_802_11_AUTHENTICATION_REQUEST {
281 ULONG Length;
282 NDIS_802_11_MAC_ADDRESS Bssid;
283 ULONG Flags;
284 } NDIS_802_11_AUTHENTICATION_REQUEST;
286 #define NDIS_802_11_AUTH_REQUEST_REAUTH 0x01
287 #define NDIS_802_11_AUTH_REQUEST_KEYUPDATE 0x02
288 #define NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR 0x06
289 #define NDIS_802_11_AUTH_REQUEST_GROUP_ERROR 0x0E
291 #endif /* OID_802_11_BSSID */
294 #ifndef OID_802_11_PMKID
295 /* Platform SDK for XP did not include WPA2, so add needed definitions */
297 #define OID_802_11_CAPABILITY 0x0d010122
298 #define OID_802_11_PMKID 0x0d010123
300 #define Ndis802_11AuthModeWPA2 6
301 #define Ndis802_11AuthModeWPA2PSK 7
303 #define Ndis802_11StatusType_PMKID_CandidateList 2
305 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
306 NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
307 NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
308 } NDIS_802_11_AUTHENTICATION_ENCRYPTION;
310 typedef struct NDIS_802_11_CAPABILITY {
311 ULONG Length;
312 ULONG Version;
313 ULONG NoOfPMKIDs;
314 ULONG NoOfAuthEncryptPairsSupported;
315 NDIS_802_11_AUTHENTICATION_ENCRYPTION
316 AuthenticationEncryptionSupported[1];
317 } NDIS_802_11_CAPABILITY;
319 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
321 typedef struct BSSID_INFO {
322 NDIS_802_11_MAC_ADDRESS BSSID;
323 NDIS_802_11_PMKID_VALUE PMKID;
324 } BSSID_INFO;
326 typedef struct NDIS_802_11_PMKID {
327 ULONG Length;
328 ULONG BSSIDInfoCount;
329 BSSID_INFO BSSIDInfo[1];
330 } NDIS_802_11_PMKID;
332 typedef struct PMKID_CANDIDATE {
333 NDIS_802_11_MAC_ADDRESS BSSID;
334 ULONG Flags;
335 } PMKID_CANDIDATE;
337 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
339 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
340 ULONG Version;
341 ULONG NumCandidates;
342 PMKID_CANDIDATE CandidateList[1];
343 } NDIS_802_11_PMKID_CANDIDATE_LIST;
345 #endif /* OID_802_11_CAPABILITY */
348 #ifdef CONFIG_USE_NDISUIO
349 #ifndef _WIN32_WCE
350 #ifdef __MINGW32_VERSION
351 typedef ULONG NDIS_OID;
352 #endif /* __MINGW32_VERSION */
353 /* from nuiouser.h */
354 #define FSCTL_NDISUIO_BASE FILE_DEVICE_NETWORK
356 #define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \
357 CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access)
359 #define IOCTL_NDISUIO_OPEN_DEVICE \
360 _NDISUIO_CTL_CODE(0x200, METHOD_BUFFERED, \
361 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
363 #define IOCTL_NDISUIO_QUERY_OID_VALUE \
364 _NDISUIO_CTL_CODE(0x201, METHOD_BUFFERED, \
365 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
367 #define IOCTL_NDISUIO_SET_OID_VALUE \
368 _NDISUIO_CTL_CODE(0x205, METHOD_BUFFERED, \
369 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
371 #define IOCTL_NDISUIO_SET_ETHER_TYPE \
372 _NDISUIO_CTL_CODE(0x202, METHOD_BUFFERED, \
373 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
375 #define IOCTL_NDISUIO_QUERY_BINDING \
376 _NDISUIO_CTL_CODE(0x203, METHOD_BUFFERED, \
377 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
379 #define IOCTL_NDISUIO_BIND_WAIT \
380 _NDISUIO_CTL_CODE(0x204, METHOD_BUFFERED, \
381 FILE_READ_ACCESS | FILE_WRITE_ACCESS)
383 typedef struct _NDISUIO_QUERY_OID
385 NDIS_OID Oid;
386 UCHAR Data[sizeof(ULONG)];
387 } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;
389 typedef struct _NDISUIO_SET_OID
391 NDIS_OID Oid;
392 UCHAR Data[sizeof(ULONG)];
393 } NDISUIO_SET_OID, *PNDISUIO_SET_OID;
395 typedef struct _NDISUIO_QUERY_BINDING
397 ULONG BindingIndex;
398 ULONG DeviceNameOffset;
399 ULONG DeviceNameLength;
400 ULONG DeviceDescrOffset;
401 ULONG DeviceDescrLength;
402 } NDISUIO_QUERY_BINDING, *PNDISUIO_QUERY_BINDING;
403 #endif /* _WIN32_WCE */
404 #endif /* CONFIG_USE_NDISUIO */
407 static int ndis_get_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
408 char *data, size_t len)
410 #ifdef CONFIG_USE_NDISUIO
411 NDISUIO_QUERY_OID *o;
412 size_t buflen = sizeof(*o) + len;
413 DWORD written;
414 int ret;
415 size_t hdrlen;
417 o = os_zalloc(buflen);
418 if (o == NULL)
419 return -1;
420 o->Oid = oid;
421 #ifdef _WIN32_WCE
422 o->ptcDeviceName = drv->adapter_name;
423 #endif /* _WIN32_WCE */
424 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_OID_VALUE,
425 o, sizeof(NDISUIO_QUERY_OID), o, buflen, &written,
426 NULL)) {
427 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_QUERY_OID_VALUE "
428 "failed (oid=%08x): %d", oid, (int) GetLastError());
429 os_free(o);
430 return -1;
432 hdrlen = sizeof(NDISUIO_QUERY_OID) - sizeof(o->Data);
433 if (written < hdrlen) {
434 wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d); "
435 "too short", oid, (unsigned int) written);
436 os_free(o);
437 return -1;
439 written -= hdrlen;
440 if (written > len) {
441 wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d) > "
442 "len (%d)",oid, (unsigned int) written, len);
443 os_free(o);
444 return -1;
446 os_memcpy(data, o->Data, written);
447 ret = written;
448 os_free(o);
449 return ret;
450 #else /* CONFIG_USE_NDISUIO */
451 char *buf;
452 PACKET_OID_DATA *o;
453 int ret;
455 buf = os_zalloc(sizeof(*o) + len);
456 if (buf == NULL)
457 return -1;
458 o = (PACKET_OID_DATA *) buf;
459 o->Oid = oid;
460 o->Length = len;
462 if (!PacketRequest(drv->adapter, FALSE, o)) {
463 wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
464 __func__, oid, len);
465 os_free(buf);
466 return -1;
468 if (o->Length > len) {
469 wpa_printf(MSG_DEBUG, "%s: oid=0x%x Length (%d) > len (%d)",
470 __func__, oid, (unsigned int) o->Length, len);
471 os_free(buf);
472 return -1;
474 os_memcpy(data, o->Data, o->Length);
475 ret = o->Length;
476 os_free(buf);
477 return ret;
478 #endif /* CONFIG_USE_NDISUIO */
482 static int ndis_set_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
483 const char *data, size_t len)
485 #ifdef CONFIG_USE_NDISUIO
486 NDISUIO_SET_OID *o;
487 size_t buflen, reallen;
488 DWORD written;
489 char txt[50];
491 os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
492 wpa_hexdump_key(MSG_MSGDUMP, txt, data, len);
494 buflen = sizeof(*o) + len;
495 reallen = buflen - sizeof(o->Data);
496 o = os_zalloc(buflen);
497 if (o == NULL)
498 return -1;
499 o->Oid = oid;
500 #ifdef _WIN32_WCE
501 o->ptcDeviceName = drv->adapter_name;
502 #endif /* _WIN32_WCE */
503 if (data)
504 os_memcpy(o->Data, data, len);
505 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_SET_OID_VALUE,
506 o, reallen, NULL, 0, &written, NULL)) {
507 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_SET_OID_VALUE "
508 "(oid=%08x) failed: %d", oid, (int) GetLastError());
509 os_free(o);
510 return -1;
512 os_free(o);
513 return 0;
514 #else /* CONFIG_USE_NDISUIO */
515 char *buf;
516 PACKET_OID_DATA *o;
517 char txt[50];
519 os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
520 wpa_hexdump_key(MSG_MSGDUMP, txt, data, len);
522 buf = os_zalloc(sizeof(*o) + len);
523 if (buf == NULL)
524 return -1;
525 o = (PACKET_OID_DATA *) buf;
526 o->Oid = oid;
527 o->Length = len;
528 if (data)
529 os_memcpy(o->Data, data, len);
531 if (!PacketRequest(drv->adapter, TRUE, o)) {
532 wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
533 __func__, oid, len);
534 os_free(buf);
535 return -1;
537 os_free(buf);
538 return 0;
539 #endif /* CONFIG_USE_NDISUIO */
543 static int ndis_set_auth_mode(struct wpa_driver_ndis_data *drv, int mode)
545 u32 auth_mode = mode;
546 if (ndis_set_oid(drv, OID_802_11_AUTHENTICATION_MODE,
547 (char *) &auth_mode, sizeof(auth_mode)) < 0) {
548 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
549 "OID_802_11_AUTHENTICATION_MODE (%d)",
550 (int) auth_mode);
551 return -1;
553 return 0;
557 static int ndis_get_auth_mode(struct wpa_driver_ndis_data *drv)
559 u32 auth_mode;
560 int res;
561 res = ndis_get_oid(drv, OID_802_11_AUTHENTICATION_MODE,
562 (char *) &auth_mode, sizeof(auth_mode));
563 if (res != sizeof(auth_mode)) {
564 wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
565 "OID_802_11_AUTHENTICATION_MODE");
566 return -1;
568 return auth_mode;
572 static int ndis_set_encr_status(struct wpa_driver_ndis_data *drv, int encr)
574 u32 encr_status = encr;
575 if (ndis_set_oid(drv, OID_802_11_ENCRYPTION_STATUS,
576 (char *) &encr_status, sizeof(encr_status)) < 0) {
577 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
578 "OID_802_11_ENCRYPTION_STATUS (%d)", encr);
579 return -1;
581 return 0;
585 static int ndis_get_encr_status(struct wpa_driver_ndis_data *drv)
587 u32 encr;
588 int res;
589 res = ndis_get_oid(drv, OID_802_11_ENCRYPTION_STATUS,
590 (char *) &encr, sizeof(encr));
591 if (res != sizeof(encr)) {
592 wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
593 "OID_802_11_ENCRYPTION_STATUS");
594 return -1;
596 return encr;
600 static int wpa_driver_ndis_get_bssid(void *priv, u8 *bssid)
602 struct wpa_driver_ndis_data *drv = priv;
604 if (drv->wired) {
606 * Report PAE group address as the "BSSID" for wired
607 * connection.
609 bssid[0] = 0x01;
610 bssid[1] = 0x80;
611 bssid[2] = 0xc2;
612 bssid[3] = 0x00;
613 bssid[4] = 0x00;
614 bssid[5] = 0x03;
615 return 0;
618 return ndis_get_oid(drv, OID_802_11_BSSID, bssid, ETH_ALEN) < 0 ?
619 -1 : 0;
623 static int wpa_driver_ndis_get_ssid(void *priv, u8 *ssid)
625 struct wpa_driver_ndis_data *drv = priv;
626 NDIS_802_11_SSID buf;
627 int res;
629 res = ndis_get_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
630 if (res < 4) {
631 wpa_printf(MSG_DEBUG, "NDIS: Failed to get SSID");
632 if (drv->wired) {
633 wpa_printf(MSG_DEBUG, "NDIS: Allow get_ssid failure "
634 "with a wired interface");
635 return 0;
637 return -1;
639 os_memcpy(ssid, buf.Ssid, buf.SsidLength);
640 return buf.SsidLength;
644 static int wpa_driver_ndis_set_ssid(struct wpa_driver_ndis_data *drv,
645 const u8 *ssid, size_t ssid_len)
647 NDIS_802_11_SSID buf;
649 os_memset(&buf, 0, sizeof(buf));
650 buf.SsidLength = ssid_len;
651 os_memcpy(buf.Ssid, ssid, ssid_len);
653 * Make sure radio is marked enabled here so that scan request will not
654 * force SSID to be changed to a random one in order to enable radio at
655 * that point.
657 drv->radio_enabled = 1;
658 return ndis_set_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
662 /* Disconnect using OID_802_11_DISASSOCIATE. This will also turn the radio off.
664 static int wpa_driver_ndis_radio_off(struct wpa_driver_ndis_data *drv)
666 drv->radio_enabled = 0;
667 return ndis_set_oid(drv, OID_802_11_DISASSOCIATE, " ", 4);
671 /* Disconnect by setting SSID to random (i.e., likely not used). */
672 static int wpa_driver_ndis_disconnect(struct wpa_driver_ndis_data *drv)
674 char ssid[32];
675 int i;
676 for (i = 0; i < 32; i++)
677 ssid[i] = rand() & 0xff;
678 return wpa_driver_ndis_set_ssid(drv, ssid, 32);
682 static int wpa_driver_ndis_deauthenticate(void *priv, const u8 *addr,
683 int reason_code)
685 struct wpa_driver_ndis_data *drv = priv;
686 return wpa_driver_ndis_disconnect(drv);
690 static int wpa_driver_ndis_disassociate(void *priv, const u8 *addr,
691 int reason_code)
693 struct wpa_driver_ndis_data *drv = priv;
694 return wpa_driver_ndis_disconnect(drv);
698 static int wpa_driver_ndis_set_wpa(void *priv, int enabled)
700 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
701 return 0;
705 static void wpa_driver_ndis_scan_timeout(void *eloop_ctx, void *timeout_ctx)
707 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
708 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
712 static int wpa_driver_ndis_scan(void *priv, const u8 *ssid, size_t ssid_len)
714 struct wpa_driver_ndis_data *drv = priv;
715 int res;
717 if (!drv->radio_enabled) {
718 wpa_printf(MSG_DEBUG, "NDIS: turning radio on before the first"
719 " scan");
720 if (wpa_driver_ndis_disconnect(drv) < 0) {
721 wpa_printf(MSG_DEBUG, "NDIS: failed to enable radio");
723 drv->radio_enabled = 1;
726 res = ndis_set_oid(drv, OID_802_11_BSSID_LIST_SCAN, " ", 4);
727 eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
728 eloop_register_timeout(7, 0, wpa_driver_ndis_scan_timeout, drv,
729 drv->ctx);
730 return res;
734 static struct wpa_scan_results * wpa_driver_ndis_get_scan_results(void *priv)
736 struct wpa_driver_ndis_data *drv = priv;
737 NDIS_802_11_BSSID_LIST_EX *b;
738 size_t blen, count, i;
739 int len;
740 char *pos;
741 struct wpa_scan_results *results;
742 struct wpa_scan_res *r;
744 blen = 65535;
745 b = os_zalloc(blen);
746 if (b == NULL)
747 return NULL;
748 len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
749 if (len < 0) {
750 wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
751 os_free(b);
752 return NULL;
754 count = b->NumberOfItems;
756 results = os_zalloc(sizeof(*results));
757 if (results == NULL) {
758 os_free(b);
759 return NULL;
761 results->res = os_zalloc(count * sizeof(struct wpa_scan_res *));
762 if (results->res == NULL) {
763 os_free(results);
764 os_free(b);
765 return NULL;
768 pos = (char *) &b->Bssid[0];
769 for (i = 0; i < count; i++) {
770 NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos;
771 NDIS_802_11_FIXED_IEs *fixed;
773 if (bss->IELength < sizeof(NDIS_802_11_FIXED_IEs)) {
774 wpa_printf(MSG_DEBUG, "NDIS: too small IELength=%d",
775 (int) bss->IELength);
776 break;
778 if (((char *) bss->IEs) + bss->IELength > (char *) b + blen) {
780 * Some NDIS drivers have been reported to include an
781 * entry with an invalid IELength in scan results and
782 * this has crashed wpa_supplicant, so validate the
783 * returned value before using it.
785 wpa_printf(MSG_DEBUG, "NDIS: skipped invalid scan "
786 "result IE (BSSID=" MACSTR ") IELength=%d",
787 MAC2STR(bss->MacAddress),
788 (int) bss->IELength);
789 break;
792 r = os_zalloc(sizeof(*r) + bss->IELength -
793 sizeof(NDIS_802_11_FIXED_IEs));
794 if (r == NULL)
795 break;
797 os_memcpy(r->bssid, bss->MacAddress, ETH_ALEN);
798 r->level = (int) bss->Rssi;
799 r->freq = bss->Configuration.DSConfig / 1000;
800 fixed = (NDIS_802_11_FIXED_IEs *) bss->IEs;
801 r->beacon_int = WPA_GET_LE16((u8 *) &fixed->BeaconInterval);
802 r->caps = WPA_GET_LE16((u8 *) &fixed->Capabilities);
803 r->tsf = WPA_GET_LE64(fixed->Timestamp);
804 os_memcpy(r + 1, bss->IEs + sizeof(NDIS_802_11_FIXED_IEs),
805 bss->IELength - sizeof(NDIS_802_11_FIXED_IEs));
806 r->ie_len = bss->IELength - sizeof(NDIS_802_11_FIXED_IEs);
808 results->res[results->num++] = r;
810 pos += bss->Length;
811 if (pos > (char *) b + blen)
812 break;
815 os_free(b);
817 return results;
821 static int wpa_driver_ndis_remove_key(struct wpa_driver_ndis_data *drv,
822 int key_idx, const u8 *addr,
823 const u8 *bssid, int pairwise)
825 NDIS_802_11_REMOVE_KEY rkey;
826 NDIS_802_11_KEY_INDEX index;
827 int res, res2;
829 os_memset(&rkey, 0, sizeof(rkey));
831 rkey.Length = sizeof(rkey);
832 rkey.KeyIndex = key_idx;
833 if (pairwise)
834 rkey.KeyIndex |= 1 << 30;
835 os_memcpy(rkey.BSSID, bssid, ETH_ALEN);
837 res = ndis_set_oid(drv, OID_802_11_REMOVE_KEY, (char *) &rkey,
838 sizeof(rkey));
839 if (!pairwise) {
840 index = key_idx;
841 res2 = ndis_set_oid(drv, OID_802_11_REMOVE_WEP,
842 (char *) &index, sizeof(index));
843 } else
844 res2 = 0;
846 if (res < 0 && res2 < 0)
847 return -1;
848 return 0;
852 static int wpa_driver_ndis_add_wep(struct wpa_driver_ndis_data *drv,
853 int pairwise, int key_idx, int set_tx,
854 const u8 *key, size_t key_len)
856 NDIS_802_11_WEP *wep;
857 size_t len;
858 int res;
860 len = 12 + key_len;
861 wep = os_zalloc(len);
862 if (wep == NULL)
863 return -1;
864 wep->Length = len;
865 wep->KeyIndex = key_idx;
866 if (set_tx)
867 wep->KeyIndex |= 1 << 31;
868 #if 0 /* Setting bit30 does not seem to work with some NDIS drivers */
869 if (pairwise)
870 wep->KeyIndex |= 1 << 30;
871 #endif
872 wep->KeyLength = key_len;
873 os_memcpy(wep->KeyMaterial, key, key_len);
875 wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_WEP",
876 (char *) wep, len);
877 res = ndis_set_oid(drv, OID_802_11_ADD_WEP, (char *) wep, len);
879 os_free(wep);
881 return res;
885 static int wpa_driver_ndis_set_key(void *priv, wpa_alg alg, const u8 *addr,
886 int key_idx, int set_tx,
887 const u8 *seq, size_t seq_len,
888 const u8 *key, size_t key_len)
890 struct wpa_driver_ndis_data *drv = priv;
891 size_t len, i;
892 NDIS_802_11_KEY *nkey;
893 int res, pairwise;
894 u8 bssid[ETH_ALEN];
896 if (addr == NULL || os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
897 ETH_ALEN) == 0) {
898 /* Group Key */
899 pairwise = 0;
900 if (wpa_driver_ndis_get_bssid(drv, bssid) < 0)
901 os_memset(bssid, 0xff, ETH_ALEN);
902 } else {
903 /* Pairwise Key */
904 pairwise = 1;
905 os_memcpy(bssid, addr, ETH_ALEN);
908 if (alg == WPA_ALG_NONE || key_len == 0) {
909 return wpa_driver_ndis_remove_key(drv, key_idx, addr, bssid,
910 pairwise);
913 if (alg == WPA_ALG_WEP) {
914 return wpa_driver_ndis_add_wep(drv, pairwise, key_idx, set_tx,
915 key, key_len);
918 len = 12 + 6 + 6 + 8 + key_len;
920 nkey = os_zalloc(len);
921 if (nkey == NULL)
922 return -1;
924 nkey->Length = len;
925 nkey->KeyIndex = key_idx;
926 if (set_tx)
927 nkey->KeyIndex |= 1 << 31;
928 if (pairwise)
929 nkey->KeyIndex |= 1 << 30;
930 if (seq && seq_len)
931 nkey->KeyIndex |= 1 << 29;
932 nkey->KeyLength = key_len;
933 os_memcpy(nkey->BSSID, bssid, ETH_ALEN);
934 if (seq && seq_len) {
935 for (i = 0; i < seq_len; i++)
936 nkey->KeyRSC |= (ULONGLONG) seq[i] << (i * 8);
938 if (alg == WPA_ALG_TKIP && key_len == 32) {
939 os_memcpy(nkey->KeyMaterial, key, 16);
940 os_memcpy(nkey->KeyMaterial + 16, key + 24, 8);
941 os_memcpy(nkey->KeyMaterial + 24, key + 16, 8);
942 } else {
943 os_memcpy(nkey->KeyMaterial, key, key_len);
946 wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_KEY",
947 (char *) nkey, len);
948 res = ndis_set_oid(drv, OID_802_11_ADD_KEY, (char *) nkey, len);
949 os_free(nkey);
951 return res;
955 static int
956 wpa_driver_ndis_associate(void *priv,
957 struct wpa_driver_associate_params *params)
959 struct wpa_driver_ndis_data *drv = priv;
960 u32 auth_mode, encr, priv_mode, mode;
962 drv->mode = params->mode;
964 /* Note: Setting OID_802_11_INFRASTRUCTURE_MODE clears current keys,
965 * so static WEP keys needs to be set again after this. */
966 if (params->mode == IEEE80211_MODE_IBSS) {
967 mode = Ndis802_11IBSS;
968 /* Need to make sure that BSSID polling is enabled for
969 * IBSS mode. */
970 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
971 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
972 drv, NULL);
973 } else
974 mode = Ndis802_11Infrastructure;
975 if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
976 (char *) &mode, sizeof(mode)) < 0) {
977 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
978 "OID_802_11_INFRASTRUCTURE_MODE (%d)",
979 (int) mode);
980 /* Try to continue anyway */
983 if (params->key_mgmt_suite == KEY_MGMT_NONE ||
984 params->key_mgmt_suite == KEY_MGMT_802_1X_NO_WPA) {
985 /* Re-set WEP keys if static WEP configuration is used. */
986 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
987 int i;
988 for (i = 0; i < 4; i++) {
989 if (!params->wep_key[i])
990 continue;
991 wpa_printf(MSG_DEBUG, "NDIS: Re-setting static WEP "
992 "key %d", i);
993 wpa_driver_ndis_set_key(drv, WPA_ALG_WEP, bcast, i,
994 i == params->wep_tx_keyidx,
995 NULL, 0, params->wep_key[i],
996 params->wep_key_len[i]);
1000 if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
1001 if (params->auth_alg & AUTH_ALG_SHARED_KEY) {
1002 if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
1003 auth_mode = Ndis802_11AuthModeAutoSwitch;
1004 else
1005 auth_mode = Ndis802_11AuthModeShared;
1006 } else
1007 auth_mode = Ndis802_11AuthModeOpen;
1008 priv_mode = Ndis802_11PrivFilterAcceptAll;
1009 } else if (params->wpa_ie[0] == WLAN_EID_RSN) {
1010 priv_mode = Ndis802_11PrivFilter8021xWEP;
1011 if (params->key_mgmt_suite == KEY_MGMT_PSK)
1012 auth_mode = Ndis802_11AuthModeWPA2PSK;
1013 else
1014 auth_mode = Ndis802_11AuthModeWPA2;
1015 } else {
1016 priv_mode = Ndis802_11PrivFilter8021xWEP;
1017 if (params->key_mgmt_suite == KEY_MGMT_WPA_NONE)
1018 auth_mode = Ndis802_11AuthModeWPANone;
1019 else if (params->key_mgmt_suite == KEY_MGMT_PSK)
1020 auth_mode = Ndis802_11AuthModeWPAPSK;
1021 else
1022 auth_mode = Ndis802_11AuthModeWPA;
1025 switch (params->pairwise_suite) {
1026 case CIPHER_CCMP:
1027 encr = Ndis802_11Encryption3Enabled;
1028 break;
1029 case CIPHER_TKIP:
1030 encr = Ndis802_11Encryption2Enabled;
1031 break;
1032 case CIPHER_WEP40:
1033 case CIPHER_WEP104:
1034 encr = Ndis802_11Encryption1Enabled;
1035 break;
1036 case CIPHER_NONE:
1037 if (params->group_suite == CIPHER_CCMP)
1038 encr = Ndis802_11Encryption3Enabled;
1039 else if (params->group_suite == CIPHER_TKIP)
1040 encr = Ndis802_11Encryption2Enabled;
1041 else
1042 encr = Ndis802_11EncryptionDisabled;
1043 break;
1044 default:
1045 encr = Ndis802_11EncryptionDisabled;
1048 if (ndis_set_oid(drv, OID_802_11_PRIVACY_FILTER,
1049 (char *) &priv_mode, sizeof(priv_mode)) < 0) {
1050 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
1051 "OID_802_11_PRIVACY_FILTER (%d)",
1052 (int) priv_mode);
1053 /* Try to continue anyway */
1056 ndis_set_auth_mode(drv, auth_mode);
1057 ndis_set_encr_status(drv, encr);
1059 if (params->bssid) {
1060 ndis_set_oid(drv, OID_802_11_BSSID, params->bssid, ETH_ALEN);
1061 drv->oid_bssid_set = 1;
1062 } else if (drv->oid_bssid_set) {
1063 ndis_set_oid(drv, OID_802_11_BSSID, "\xff\xff\xff\xff\xff\xff",
1064 ETH_ALEN);
1065 drv->oid_bssid_set = 0;
1068 return wpa_driver_ndis_set_ssid(drv, params->ssid, params->ssid_len);
1072 static int wpa_driver_ndis_set_pmkid(struct wpa_driver_ndis_data *drv)
1074 int len, count, i, ret;
1075 struct ndis_pmkid_entry *entry;
1076 NDIS_802_11_PMKID *p;
1078 count = 0;
1079 entry = drv->pmkid;
1080 while (entry) {
1081 count++;
1082 if (count >= drv->no_of_pmkid)
1083 break;
1084 entry = entry->next;
1086 len = 8 + count * sizeof(BSSID_INFO);
1087 p = os_zalloc(len);
1088 if (p == NULL)
1089 return -1;
1091 p->Length = len;
1092 p->BSSIDInfoCount = count;
1093 entry = drv->pmkid;
1094 for (i = 0; i < count; i++) {
1095 os_memcpy(&p->BSSIDInfo[i].BSSID, entry->bssid, ETH_ALEN);
1096 os_memcpy(&p->BSSIDInfo[i].PMKID, entry->pmkid, 16);
1097 entry = entry->next;
1099 wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID", (char *) p, len);
1100 ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) p, len);
1101 os_free(p);
1102 return ret;
1106 static int wpa_driver_ndis_add_pmkid(void *priv, const u8 *bssid,
1107 const u8 *pmkid)
1109 struct wpa_driver_ndis_data *drv = priv;
1110 struct ndis_pmkid_entry *entry, *prev;
1112 if (drv->no_of_pmkid == 0)
1113 return 0;
1115 prev = NULL;
1116 entry = drv->pmkid;
1117 while (entry) {
1118 if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0)
1119 break;
1120 prev = entry;
1121 entry = entry->next;
1124 if (entry) {
1125 /* Replace existing entry for this BSSID and move it into the
1126 * beginning of the list. */
1127 os_memcpy(entry->pmkid, pmkid, 16);
1128 if (prev) {
1129 prev->next = entry->next;
1130 entry->next = drv->pmkid;
1131 drv->pmkid = entry;
1133 } else {
1134 entry = os_malloc(sizeof(*entry));
1135 if (entry) {
1136 os_memcpy(entry->bssid, bssid, ETH_ALEN);
1137 os_memcpy(entry->pmkid, pmkid, 16);
1138 entry->next = drv->pmkid;
1139 drv->pmkid = entry;
1143 return wpa_driver_ndis_set_pmkid(drv);
1147 static int wpa_driver_ndis_remove_pmkid(void *priv, const u8 *bssid,
1148 const u8 *pmkid)
1150 struct wpa_driver_ndis_data *drv = priv;
1151 struct ndis_pmkid_entry *entry, *prev;
1153 if (drv->no_of_pmkid == 0)
1154 return 0;
1156 entry = drv->pmkid;
1157 prev = NULL;
1158 while (entry) {
1159 if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0 &&
1160 os_memcmp(entry->pmkid, pmkid, 16) == 0) {
1161 if (prev)
1162 prev->next = entry->next;
1163 else
1164 drv->pmkid = entry->next;
1165 os_free(entry);
1166 break;
1168 prev = entry;
1169 entry = entry->next;
1171 return wpa_driver_ndis_set_pmkid(drv);
1175 static int wpa_driver_ndis_flush_pmkid(void *priv)
1177 struct wpa_driver_ndis_data *drv = priv;
1178 NDIS_802_11_PMKID p;
1179 struct ndis_pmkid_entry *pmkid, *prev;
1181 if (drv->no_of_pmkid == 0)
1182 return 0;
1184 pmkid = drv->pmkid;
1185 drv->pmkid = NULL;
1186 while (pmkid) {
1187 prev = pmkid;
1188 pmkid = pmkid->next;
1189 os_free(prev);
1192 os_memset(&p, 0, sizeof(p));
1193 p.Length = 8;
1194 p.BSSIDInfoCount = 0;
1195 wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID (flush)",
1196 (char *) &p, 8);
1197 return ndis_set_oid(drv, OID_802_11_PMKID, (char *) &p, 8);
1201 static int wpa_driver_ndis_get_associnfo(struct wpa_driver_ndis_data *drv)
1203 char buf[512], *pos;
1204 NDIS_802_11_ASSOCIATION_INFORMATION *ai;
1205 int len;
1206 union wpa_event_data data;
1207 NDIS_802_11_BSSID_LIST_EX *b;
1208 size_t blen, i;
1210 len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION, buf,
1211 sizeof(buf));
1212 if (len < 0) {
1213 wpa_printf(MSG_DEBUG, "NDIS: failed to get association "
1214 "information");
1215 return -1;
1217 if (len > sizeof(buf)) {
1218 /* Some drivers seem to be producing incorrect length for this
1219 * data. Limit the length to the current buffer size to avoid
1220 * crashing in hexdump. The data seems to be otherwise valid,
1221 * so better try to use it. */
1222 wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association "
1223 "information length %d", len);
1224 len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION,
1225 buf, sizeof(buf));
1226 if (len < -1) {
1227 wpa_printf(MSG_DEBUG, "NDIS: re-reading association "
1228 "information failed");
1229 return -1;
1231 if (len > sizeof(buf)) {
1232 wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association"
1233 " information length %d (re-read)", len);
1234 len = sizeof(buf);
1237 wpa_hexdump(MSG_MSGDUMP, "NDIS: association information", buf, len);
1238 if (len < sizeof(*ai)) {
1239 wpa_printf(MSG_DEBUG, "NDIS: too short association "
1240 "information");
1241 return -1;
1243 ai = (NDIS_802_11_ASSOCIATION_INFORMATION *) buf;
1244 wpa_printf(MSG_DEBUG, "NDIS: ReqFixed=0x%x RespFixed=0x%x off_req=%d "
1245 "off_resp=%d len_req=%d len_resp=%d",
1246 ai->AvailableRequestFixedIEs, ai->AvailableResponseFixedIEs,
1247 (int) ai->OffsetRequestIEs, (int) ai->OffsetResponseIEs,
1248 (int) ai->RequestIELength, (int) ai->ResponseIELength);
1250 if (ai->OffsetRequestIEs + ai->RequestIELength > (unsigned) len ||
1251 ai->OffsetResponseIEs + ai->ResponseIELength > (unsigned) len) {
1252 wpa_printf(MSG_DEBUG, "NDIS: association information - "
1253 "IE overflow");
1254 return -1;
1257 wpa_hexdump(MSG_MSGDUMP, "NDIS: Request IEs",
1258 buf + ai->OffsetRequestIEs, ai->RequestIELength);
1259 wpa_hexdump(MSG_MSGDUMP, "NDIS: Response IEs",
1260 buf + ai->OffsetResponseIEs, ai->ResponseIELength);
1262 os_memset(&data, 0, sizeof(data));
1263 data.assoc_info.req_ies = buf + ai->OffsetRequestIEs;
1264 data.assoc_info.req_ies_len = ai->RequestIELength;
1265 data.assoc_info.resp_ies = buf + ai->OffsetResponseIEs;
1266 data.assoc_info.resp_ies_len = ai->ResponseIELength;
1268 blen = 65535;
1269 b = os_zalloc(blen);
1270 if (b == NULL)
1271 goto skip_scan_results;
1272 len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
1273 if (len < 0) {
1274 wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
1275 os_free(b);
1276 b = NULL;
1277 goto skip_scan_results;
1279 wpa_printf(MSG_DEBUG, "NDIS: %d BSSID items to process for AssocInfo",
1280 (unsigned int) b->NumberOfItems);
1282 pos = (char *) &b->Bssid[0];
1283 for (i = 0; i < b->NumberOfItems; i++) {
1284 NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos;
1285 if (os_memcmp(drv->bssid, bss->MacAddress, ETH_ALEN) == 0 &&
1286 bss->IELength > sizeof(NDIS_802_11_FIXED_IEs)) {
1287 data.assoc_info.beacon_ies =
1288 ((u8 *) bss->IEs) +
1289 sizeof(NDIS_802_11_FIXED_IEs);
1290 data.assoc_info.beacon_ies_len =
1291 bss->IELength - sizeof(NDIS_802_11_FIXED_IEs);
1292 wpa_hexdump(MSG_MSGDUMP, "NDIS: Beacon IEs",
1293 data.assoc_info.beacon_ies,
1294 data.assoc_info.beacon_ies_len);
1295 break;
1297 pos += bss->Length;
1298 if (pos > (char *) b + blen)
1299 break;
1302 skip_scan_results:
1303 wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
1305 os_free(b);
1307 return 0;
1311 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx)
1313 struct wpa_driver_ndis_data *drv = eloop_ctx;
1314 u8 bssid[ETH_ALEN];
1315 int poll;
1317 if (drv->wired)
1318 return;
1320 if (wpa_driver_ndis_get_bssid(drv, bssid)) {
1321 /* Disconnected */
1322 if (!is_zero_ether_addr(drv->bssid)) {
1323 os_memset(drv->bssid, 0, ETH_ALEN);
1324 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1326 } else {
1327 /* Connected */
1328 if (os_memcmp(drv->bssid, bssid, ETH_ALEN) != 0) {
1329 os_memcpy(drv->bssid, bssid, ETH_ALEN);
1330 wpa_driver_ndis_get_associnfo(drv);
1331 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1335 /* When using integrated NDIS event receiver, we can skip BSSID
1336 * polling when using infrastructure network. However, when using
1337 * IBSS mode, many driver do not seem to generate connection event,
1338 * so we need to enable BSSID polling to figure out when IBSS network
1339 * has been formed.
1341 poll = drv->mode == IEEE80211_MODE_IBSS;
1342 #ifndef CONFIG_NDIS_EVENTS_INTEGRATED
1343 #ifndef _WIN32_WCE
1344 poll = 1;
1345 #endif /* _WIN32_WCE */
1346 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
1348 if (poll) {
1349 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1350 drv, NULL);
1355 static void wpa_driver_ndis_poll(void *priv)
1357 struct wpa_driver_ndis_data *drv = priv;
1358 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
1359 wpa_driver_ndis_poll_timeout(drv, NULL);
1363 /* Called when driver generates Media Connect Event by calling
1364 * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_CONNECT */
1365 void wpa_driver_ndis_event_connect(struct wpa_driver_ndis_data *drv)
1367 wpa_printf(MSG_DEBUG, "NDIS: Media Connect Event");
1368 if (wpa_driver_ndis_get_bssid(drv, drv->bssid) == 0) {
1369 wpa_driver_ndis_get_associnfo(drv);
1370 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1375 /* Called when driver generates Media Disconnect Event by calling
1376 * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_DISCONNECT */
1377 void wpa_driver_ndis_event_disconnect(struct wpa_driver_ndis_data *drv)
1379 wpa_printf(MSG_DEBUG, "NDIS: Media Disconnect Event");
1380 os_memset(drv->bssid, 0, ETH_ALEN);
1381 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1385 static void wpa_driver_ndis_event_auth(struct wpa_driver_ndis_data *drv,
1386 const u8 *data, size_t data_len)
1388 NDIS_802_11_AUTHENTICATION_REQUEST *req;
1389 int pairwise = 0, group = 0;
1390 union wpa_event_data event;
1392 if (data_len < sizeof(*req)) {
1393 wpa_printf(MSG_DEBUG, "NDIS: Too short Authentication Request "
1394 "Event (len=%d)", data_len);
1395 return;
1397 req = (NDIS_802_11_AUTHENTICATION_REQUEST *) data;
1399 wpa_printf(MSG_DEBUG, "NDIS: Authentication Request Event: "
1400 "Bssid " MACSTR " Flags 0x%x",
1401 MAC2STR(req->Bssid), (int) req->Flags);
1403 if ((req->Flags & NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR) ==
1404 NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR)
1405 pairwise = 1;
1406 else if ((req->Flags & NDIS_802_11_AUTH_REQUEST_GROUP_ERROR) ==
1407 NDIS_802_11_AUTH_REQUEST_GROUP_ERROR)
1408 group = 1;
1410 if (pairwise || group) {
1411 os_memset(&event, 0, sizeof(event));
1412 event.michael_mic_failure.unicast = pairwise;
1413 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE,
1414 &event);
1419 static void wpa_driver_ndis_event_pmkid(struct wpa_driver_ndis_data *drv,
1420 const u8 *data, size_t data_len)
1422 NDIS_802_11_PMKID_CANDIDATE_LIST *pmkid;
1423 size_t i;
1424 union wpa_event_data event;
1426 if (data_len < 8) {
1427 wpa_printf(MSG_DEBUG, "NDIS: Too short PMKID Candidate List "
1428 "Event (len=%d)", data_len);
1429 return;
1431 pmkid = (NDIS_802_11_PMKID_CANDIDATE_LIST *) data;
1432 wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List Event - Version %d "
1433 "NumCandidates %d",
1434 (int) pmkid->Version, (int) pmkid->NumCandidates);
1436 if (pmkid->Version != 1) {
1437 wpa_printf(MSG_DEBUG, "NDIS: Unsupported PMKID Candidate List "
1438 "Version %d", (int) pmkid->Version);
1439 return;
1442 if (data_len < 8 + pmkid->NumCandidates * sizeof(PMKID_CANDIDATE)) {
1443 wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List underflow");
1444 return;
1447 os_memset(&event, 0, sizeof(event));
1448 for (i = 0; i < pmkid->NumCandidates; i++) {
1449 PMKID_CANDIDATE *p = &pmkid->CandidateList[i];
1450 wpa_printf(MSG_DEBUG, "NDIS: %d: " MACSTR " Flags 0x%x",
1451 i, MAC2STR(p->BSSID), (int) p->Flags);
1452 os_memcpy(event.pmkid_candidate.bssid, p->BSSID, ETH_ALEN);
1453 event.pmkid_candidate.index = i;
1454 event.pmkid_candidate.preauth =
1455 p->Flags & NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
1456 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE,
1457 &event);
1462 /* Called when driver calls NdisMIndicateStatus() with
1463 * NDIS_STATUS_MEDIA_SPECIFIC_INDICATION */
1464 void wpa_driver_ndis_event_media_specific(struct wpa_driver_ndis_data *drv,
1465 const u8 *data, size_t data_len)
1467 NDIS_802_11_STATUS_INDICATION *status;
1469 if (data == NULL || data_len < sizeof(*status))
1470 return;
1472 wpa_hexdump(MSG_DEBUG, "NDIS: Media Specific Indication",
1473 data, data_len);
1475 status = (NDIS_802_11_STATUS_INDICATION *) data;
1476 data += sizeof(status);
1477 data_len -= sizeof(status);
1479 switch (status->StatusType) {
1480 case Ndis802_11StatusType_Authentication:
1481 wpa_driver_ndis_event_auth(drv, data, data_len);
1482 break;
1483 case Ndis802_11StatusType_PMKID_CandidateList:
1484 wpa_driver_ndis_event_pmkid(drv, data, data_len);
1485 break;
1486 default:
1487 wpa_printf(MSG_DEBUG, "NDIS: Unknown StatusType %d",
1488 (int) status->StatusType);
1489 break;
1494 /* Called when an adapter is added */
1495 void wpa_driver_ndis_event_adapter_arrival(struct wpa_driver_ndis_data *drv)
1497 union wpa_event_data event;
1498 int i;
1500 wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Arrival");
1502 for (i = 0; i < 30; i++) {
1503 /* Re-open Packet32/NDISUIO connection */
1504 wpa_driver_ndis_adapter_close(drv);
1505 if (wpa_driver_ndis_adapter_init(drv) < 0 ||
1506 wpa_driver_ndis_adapter_open(drv) < 0) {
1507 wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialization "
1508 "(%d) failed", i);
1509 os_sleep(1, 0);
1510 } else {
1511 wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialized");
1512 break;
1516 os_memset(&event, 0, sizeof(event));
1517 os_strlcpy(event.interface_status.ifname, drv->ifname,
1518 sizeof(event.interface_status.ifname));
1519 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1520 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1524 /* Called when an adapter is removed */
1525 void wpa_driver_ndis_event_adapter_removal(struct wpa_driver_ndis_data *drv)
1527 union wpa_event_data event;
1529 wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Removal");
1530 os_memset(&event, 0, sizeof(event));
1531 os_strlcpy(event.interface_status.ifname, drv->ifname,
1532 sizeof(event.interface_status.ifname));
1533 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
1534 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1538 static void
1539 wpa_driver_ndis_get_wpa_capability(struct wpa_driver_ndis_data *drv)
1541 wpa_printf(MSG_DEBUG, "NDIS: verifying driver WPA capability");
1543 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA) == 0 &&
1544 ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPA) {
1545 wpa_printf(MSG_DEBUG, "NDIS: WPA key management supported");
1546 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1549 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPAPSK) == 0 &&
1550 ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPAPSK) {
1551 wpa_printf(MSG_DEBUG, "NDIS: WPA-PSK key management "
1552 "supported");
1553 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1556 if (ndis_set_encr_status(drv, Ndis802_11Encryption3Enabled) == 0 &&
1557 ndis_get_encr_status(drv) == Ndis802_11Encryption3KeyAbsent) {
1558 wpa_printf(MSG_DEBUG, "NDIS: CCMP encryption supported");
1559 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1562 if (ndis_set_encr_status(drv, Ndis802_11Encryption2Enabled) == 0 &&
1563 ndis_get_encr_status(drv) == Ndis802_11Encryption2KeyAbsent) {
1564 wpa_printf(MSG_DEBUG, "NDIS: TKIP encryption supported");
1565 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1568 if (ndis_set_encr_status(drv, Ndis802_11Encryption1Enabled) == 0 &&
1569 ndis_get_encr_status(drv) == Ndis802_11Encryption1KeyAbsent) {
1570 wpa_printf(MSG_DEBUG, "NDIS: WEP encryption supported");
1571 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1572 WPA_DRIVER_CAPA_ENC_WEP104;
1575 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeShared) == 0 &&
1576 ndis_get_auth_mode(drv) == Ndis802_11AuthModeShared) {
1577 drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1580 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeOpen) == 0 &&
1581 ndis_get_auth_mode(drv) == Ndis802_11AuthModeOpen) {
1582 drv->capa.auth |= WPA_DRIVER_AUTH_OPEN;
1585 ndis_set_encr_status(drv, Ndis802_11EncryptionDisabled);
1587 /* Could also verify OID_802_11_ADD_KEY error reporting and
1588 * support for OID_802_11_ASSOCIATION_INFORMATION. */
1590 if (drv->capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA &&
1591 drv->capa.enc & (WPA_DRIVER_CAPA_ENC_TKIP |
1592 WPA_DRIVER_CAPA_ENC_CCMP)) {
1593 wpa_printf(MSG_DEBUG, "NDIS: driver supports WPA");
1594 drv->has_capability = 1;
1595 } else {
1596 wpa_printf(MSG_DEBUG, "NDIS: no WPA support found");
1599 wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1600 "enc 0x%x auth 0x%x",
1601 drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1605 static void wpa_driver_ndis_get_capability(struct wpa_driver_ndis_data *drv)
1607 char buf[512];
1608 int len;
1609 size_t i;
1610 NDIS_802_11_CAPABILITY *c;
1612 drv->capa.flags = WPA_DRIVER_FLAGS_DRIVER_IE;
1614 len = ndis_get_oid(drv, OID_802_11_CAPABILITY, buf, sizeof(buf));
1615 if (len < 0) {
1616 wpa_driver_ndis_get_wpa_capability(drv);
1617 return;
1620 wpa_hexdump(MSG_MSGDUMP, "OID_802_11_CAPABILITY", buf, len);
1621 c = (NDIS_802_11_CAPABILITY *) buf;
1622 if (len < sizeof(*c) || c->Version != 2) {
1623 wpa_printf(MSG_DEBUG, "NDIS: unsupported "
1624 "OID_802_11_CAPABILITY data");
1625 return;
1627 wpa_printf(MSG_DEBUG, "NDIS: Driver supports OID_802_11_CAPABILITY - "
1628 "NoOfPMKIDs %d NoOfAuthEncrPairs %d",
1629 (int) c->NoOfPMKIDs,
1630 (int) c->NoOfAuthEncryptPairsSupported);
1631 drv->has_capability = 1;
1632 drv->no_of_pmkid = c->NoOfPMKIDs;
1633 for (i = 0; i < c->NoOfAuthEncryptPairsSupported; i++) {
1634 NDIS_802_11_AUTHENTICATION_ENCRYPTION *ae;
1635 ae = &c->AuthenticationEncryptionSupported[i];
1636 if ((char *) (ae + 1) > buf + len) {
1637 wpa_printf(MSG_DEBUG, "NDIS: auth/encr pair list "
1638 "overflow");
1639 break;
1641 wpa_printf(MSG_MSGDUMP, "NDIS: %d - auth %d encr %d",
1642 i, (int) ae->AuthModeSupported,
1643 (int) ae->EncryptStatusSupported);
1644 switch (ae->AuthModeSupported) {
1645 case Ndis802_11AuthModeOpen:
1646 drv->capa.auth |= WPA_DRIVER_AUTH_OPEN;
1647 break;
1648 case Ndis802_11AuthModeShared:
1649 drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1650 break;
1651 case Ndis802_11AuthModeWPA:
1652 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1653 break;
1654 case Ndis802_11AuthModeWPAPSK:
1655 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1656 break;
1657 case Ndis802_11AuthModeWPA2:
1658 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2;
1659 break;
1660 case Ndis802_11AuthModeWPA2PSK:
1661 drv->capa.key_mgmt |=
1662 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1663 break;
1664 case Ndis802_11AuthModeWPANone:
1665 drv->capa.key_mgmt |=
1666 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE;
1667 break;
1668 default:
1669 break;
1671 switch (ae->EncryptStatusSupported) {
1672 case Ndis802_11Encryption1Enabled:
1673 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40;
1674 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP104;
1675 break;
1676 case Ndis802_11Encryption2Enabled:
1677 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1678 break;
1679 case Ndis802_11Encryption3Enabled:
1680 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1681 break;
1682 default:
1683 break;
1687 wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1688 "enc 0x%x auth 0x%x",
1689 drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1693 static int wpa_driver_ndis_get_capa(void *priv, struct wpa_driver_capa *capa)
1695 struct wpa_driver_ndis_data *drv = priv;
1696 if (!drv->has_capability)
1697 return -1;
1698 os_memcpy(capa, &drv->capa, sizeof(*capa));
1699 return 0;
1703 static const char * wpa_driver_ndis_get_ifname(void *priv)
1705 struct wpa_driver_ndis_data *drv = priv;
1706 return drv->ifname;
1710 static const u8 * wpa_driver_ndis_get_mac_addr(void *priv)
1712 struct wpa_driver_ndis_data *drv = priv;
1713 return drv->own_addr;
1717 #ifdef _WIN32_WCE
1719 #define NDISUIO_MSG_SIZE (sizeof(NDISUIO_DEVICE_NOTIFICATION) + 512)
1721 static void ndisuio_notification_receive(void *eloop_data, void *user_ctx)
1723 struct wpa_driver_ndis_data *drv = eloop_data;
1724 NDISUIO_DEVICE_NOTIFICATION *hdr;
1725 u8 buf[NDISUIO_MSG_SIZE];
1726 DWORD len, flags;
1728 if (!ReadMsgQueue(drv->event_queue, buf, NDISUIO_MSG_SIZE, &len, 0,
1729 &flags)) {
1730 wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1731 "ReadMsgQueue failed: %d", (int) GetLastError());
1732 return;
1735 if (len < sizeof(NDISUIO_DEVICE_NOTIFICATION)) {
1736 wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1737 "Too short message (len=%d)", (int) len);
1738 return;
1741 hdr = (NDISUIO_DEVICE_NOTIFICATION *) buf;
1742 wpa_printf(MSG_DEBUG, "NDIS: Notification received: len=%d type=0x%x",
1743 (int) len, hdr->dwNotificationType);
1745 switch (hdr->dwNotificationType) {
1746 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL
1747 case NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL:
1748 wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_ARRIVAL");
1749 wpa_driver_ndis_event_adapter_arrival(drv);
1750 break;
1751 #endif
1752 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
1753 case NDISUIO_NOTIFICATION_ADAPTER_REMOVAL:
1754 wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_REMOVAL");
1755 wpa_driver_ndis_event_adapter_removal(drv);
1756 break;
1757 #endif
1758 case NDISUIO_NOTIFICATION_MEDIA_CONNECT:
1759 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_CONNECT");
1760 SetEvent(drv->connected_event);
1761 wpa_driver_ndis_event_connect(drv);
1762 break;
1763 case NDISUIO_NOTIFICATION_MEDIA_DISCONNECT:
1764 ResetEvent(drv->connected_event);
1765 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_DISCONNECT");
1766 wpa_driver_ndis_event_disconnect(drv);
1767 break;
1768 case NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION:
1769 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_SPECIFIC_NOTIFICATION");
1770 #if _WIN32_WCE == 420 || _WIN32_WCE == 0x420
1771 wpa_driver_ndis_event_media_specific(
1772 drv, hdr->pvStatusBuffer, hdr->uiStatusBufferSize);
1773 #else
1774 wpa_driver_ndis_event_media_specific(
1775 drv, ((const u8 *) hdr) + hdr->uiOffsetToStatusBuffer,
1776 (size_t) hdr->uiStatusBufferSize);
1777 #endif
1778 break;
1779 default:
1780 wpa_printf(MSG_DEBUG, "NDIS: Unknown notification type 0x%x",
1781 hdr->dwNotificationType);
1782 break;
1787 static void ndisuio_notification_deinit(struct wpa_driver_ndis_data *drv)
1789 NDISUIO_REQUEST_NOTIFICATION req;
1791 memset(&req, 0, sizeof(req));
1792 req.hMsgQueue = drv->event_queue;
1793 req.dwNotificationTypes = 0;
1795 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION,
1796 &req, sizeof(req), NULL, 0, NULL, NULL)) {
1797 wpa_printf(MSG_INFO, "ndisuio_notification_deinit: "
1798 "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d",
1799 (int) GetLastError());
1802 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_CANCEL_NOTIFICATION,
1803 NULL, 0, NULL, 0, NULL, NULL)) {
1804 wpa_printf(MSG_INFO, "ndisuio_notification_deinit: "
1805 "IOCTL_NDISUIO_CANCEL_NOTIFICATION failed: %d",
1806 (int) GetLastError());
1809 if (drv->event_queue) {
1810 eloop_unregister_event(drv->event_queue,
1811 sizeof(drv->event_queue));
1812 CloseHandle(drv->event_queue);
1813 drv->event_queue = NULL;
1816 if (drv->connected_event) {
1817 CloseHandle(drv->connected_event);
1818 drv->connected_event = NULL;
1823 static int ndisuio_notification_init(struct wpa_driver_ndis_data *drv)
1825 MSGQUEUEOPTIONS opt;
1826 NDISUIO_REQUEST_NOTIFICATION req;
1828 drv->connected_event =
1829 CreateEvent(NULL, TRUE, FALSE, TEXT("WpaSupplicantConnected"));
1830 if (drv->connected_event == NULL) {
1831 wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1832 "CreateEvent failed: %d",
1833 (int) GetLastError());
1834 return -1;
1837 memset(&opt, 0, sizeof(opt));
1838 opt.dwSize = sizeof(opt);
1839 opt.dwMaxMessages = 5;
1840 opt.cbMaxMessage = NDISUIO_MSG_SIZE;
1841 opt.bReadAccess = TRUE;
1843 drv->event_queue = CreateMsgQueue(NULL, &opt);
1844 if (drv->event_queue == NULL) {
1845 wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1846 "CreateMsgQueue failed: %d",
1847 (int) GetLastError());
1848 ndisuio_notification_deinit(drv);
1849 return -1;
1852 memset(&req, 0, sizeof(req));
1853 req.hMsgQueue = drv->event_queue;
1854 req.dwNotificationTypes =
1855 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL
1856 NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL |
1857 #endif
1858 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
1859 NDISUIO_NOTIFICATION_ADAPTER_REMOVAL |
1860 #endif
1861 NDISUIO_NOTIFICATION_MEDIA_CONNECT |
1862 NDISUIO_NOTIFICATION_MEDIA_DISCONNECT |
1863 NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION;
1865 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION,
1866 &req, sizeof(req), NULL, 0, NULL, NULL)) {
1867 wpa_printf(MSG_INFO, "ndisuio_notification_init: "
1868 "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d",
1869 (int) GetLastError());
1870 ndisuio_notification_deinit(drv);
1871 return -1;
1874 eloop_register_event(drv->event_queue, sizeof(drv->event_queue),
1875 ndisuio_notification_receive, drv, NULL);
1877 return 0;
1879 #endif /* _WIN32_WCE */
1882 static int wpa_driver_ndis_get_names(struct wpa_driver_ndis_data *drv)
1884 #ifdef CONFIG_USE_NDISUIO
1885 NDISUIO_QUERY_BINDING *b;
1886 size_t blen = sizeof(*b) + 1024;
1887 int i, error, found = 0;
1888 DWORD written;
1889 char name[256], desc[256], *dpos;
1890 WCHAR *pos;
1891 size_t j, len, dlen;
1893 b = os_malloc(blen);
1894 if (b == NULL)
1895 return -1;
1897 for (i = 0; ; i++) {
1898 os_memset(b, 0, blen);
1899 b->BindingIndex = i;
1900 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
1901 b, sizeof(NDISUIO_QUERY_BINDING), b, blen,
1902 &written, NULL)) {
1903 error = (int) GetLastError();
1904 if (error == ERROR_NO_MORE_ITEMS)
1905 break;
1906 wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
1907 "failed: %d", error);
1908 break;
1911 pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
1912 len = b->DeviceNameLength;
1913 if (len >= sizeof(name))
1914 len = sizeof(name) - 1;
1915 for (j = 0; j < len; j++)
1916 name[j] = (char) pos[j];
1917 name[len] = '\0';
1919 pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
1920 len = b->DeviceDescrLength;
1921 if (len >= sizeof(desc))
1922 len = sizeof(desc) - 1;
1923 for (j = 0; j < len; j++)
1924 desc[j] = (char) pos[j];
1925 desc[len] = '\0';
1927 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
1929 if (os_strstr(name, drv->ifname)) {
1930 wpa_printf(MSG_DEBUG, "NDIS: Interface name match");
1931 found = 1;
1932 break;
1935 if (os_strncmp(desc, drv->ifname, os_strlen(drv->ifname)) == 0)
1937 wpa_printf(MSG_DEBUG, "NDIS: Interface description "
1938 "match");
1939 found = 1;
1940 break;
1944 if (!found) {
1945 wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
1946 drv->ifname);
1947 os_free(b);
1948 return -1;
1951 os_strlcpy(drv->ifname,
1952 os_strncmp(name, "\\DEVICE\\", 8) == 0 ? name + 8 : name,
1953 sizeof(drv->ifname));
1954 #ifdef _WIN32_WCE
1955 drv->adapter_name = wpa_strdup_tchar(drv->ifname);
1956 if (drv->adapter_name == NULL) {
1957 wpa_printf(MSG_ERROR, "NDIS: Failed to allocate memory for "
1958 "adapter name");
1959 os_free(b);
1960 return -1;
1962 #endif /* _WIN32_WCE */
1964 dpos = os_strstr(desc, " - ");
1965 if (dpos)
1966 dlen = dpos - desc;
1967 else
1968 dlen = os_strlen(desc);
1969 drv->adapter_desc = os_malloc(dlen + 1);
1970 if (drv->adapter_desc) {
1971 os_memcpy(drv->adapter_desc, desc, dlen);
1972 drv->adapter_desc[dlen] = '\0';
1975 os_free(b);
1977 if (drv->adapter_desc == NULL)
1978 return -1;
1980 wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
1981 drv->adapter_desc);
1983 return 0;
1984 #else /* CONFIG_USE_NDISUIO */
1985 PTSTR _names;
1986 char *names, *pos, *pos2;
1987 ULONG len;
1988 BOOLEAN res;
1989 #define MAX_ADAPTERS 32
1990 char *name[MAX_ADAPTERS];
1991 char *desc[MAX_ADAPTERS];
1992 int num_name, num_desc, i, found_name, found_desc;
1993 size_t dlen;
1995 wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
1996 PacketGetVersion());
1998 len = 8192;
1999 _names = os_zalloc(len);
2000 if (_names == NULL)
2001 return -1;
2003 res = PacketGetAdapterNames(_names, &len);
2004 if (!res && len > 8192) {
2005 os_free(_names);
2006 _names = os_zalloc(len);
2007 if (_names == NULL)
2008 return -1;
2009 res = PacketGetAdapterNames(_names, &len);
2012 if (!res) {
2013 wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
2014 "(PacketGetAdapterNames)");
2015 os_free(_names);
2016 return -1;
2019 names = (char *) _names;
2020 if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') {
2021 wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in "
2022 "UNICODE");
2023 /* Convert to ASCII */
2024 pos2 = pos = names;
2025 while (pos2 < names + len) {
2026 if (pos2[0] == '\0' && pos2[1] == '\0' &&
2027 pos2[2] == '\0' && pos2[3] == '\0') {
2028 pos2 += 4;
2029 break;
2031 *pos++ = pos2[0];
2032 pos2 += 2;
2034 os_memcpy(pos + 2, names, pos - names);
2035 pos += 2;
2036 } else
2037 pos = names;
2039 num_name = 0;
2040 while (pos < names + len) {
2041 name[num_name] = pos;
2042 while (*pos && pos < names + len)
2043 pos++;
2044 if (pos + 1 >= names + len) {
2045 os_free(names);
2046 return -1;
2048 pos++;
2049 num_name++;
2050 if (num_name >= MAX_ADAPTERS) {
2051 wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
2052 os_free(names);
2053 return -1;
2055 if (*pos == '\0') {
2056 wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
2057 num_name);
2058 pos++;
2059 break;
2063 num_desc = 0;
2064 while (pos < names + len) {
2065 desc[num_desc] = pos;
2066 while (*pos && pos < names + len)
2067 pos++;
2068 if (pos + 1 >= names + len) {
2069 os_free(names);
2070 return -1;
2072 pos++;
2073 num_desc++;
2074 if (num_desc >= MAX_ADAPTERS) {
2075 wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
2076 "descriptions");
2077 os_free(names);
2078 return -1;
2080 if (*pos == '\0') {
2081 wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
2082 "found", num_name);
2083 pos++;
2084 break;
2089 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
2090 * descriptions. Fill in dummy descriptors to work around this.
2092 while (num_desc < num_name)
2093 desc[num_desc++] = "dummy description";
2095 if (num_name != num_desc) {
2096 wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and "
2097 "description counts (%d != %d)",
2098 num_name, num_desc);
2099 os_free(names);
2100 return -1;
2103 found_name = found_desc = -1;
2104 for (i = 0; i < num_name; i++) {
2105 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s",
2106 i, name[i], desc[i]);
2107 if (found_name == -1 && os_strstr(name[i], drv->ifname))
2108 found_name = i;
2109 if (found_desc == -1 &&
2110 os_strncmp(desc[i], drv->ifname, os_strlen(drv->ifname)) ==
2112 found_desc = i;
2115 if (found_name < 0 && found_desc >= 0) {
2116 wpa_printf(MSG_DEBUG, "NDIS: Matched interface '%s' based on "
2117 "description '%s'",
2118 name[found_desc], desc[found_desc]);
2119 found_name = found_desc;
2120 os_strlcpy(drv->ifname,
2121 os_strncmp(name[found_desc], "\\Device\\NPF_", 12)
2122 == 0 ? name[found_desc] + 12 : name[found_desc],
2123 sizeof(drv->ifname));
2126 if (found_name < 0) {
2127 wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
2128 drv->ifname);
2129 os_free(names);
2130 return -1;
2133 i = found_name;
2134 pos = os_strrchr(desc[i], '(');
2135 if (pos) {
2136 dlen = pos - desc[i];
2137 pos--;
2138 if (pos > desc[i] && *pos == ' ')
2139 dlen--;
2140 } else {
2141 dlen = os_strlen(desc[i]);
2143 drv->adapter_desc = os_malloc(dlen + 1);
2144 if (drv->adapter_desc) {
2145 os_memcpy(drv->adapter_desc, desc[i], dlen);
2146 drv->adapter_desc[dlen] = '\0';
2149 os_free(names);
2151 if (drv->adapter_desc == NULL)
2152 return -1;
2154 wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2155 drv->adapter_desc);
2157 return 0;
2158 #endif /* CONFIG_USE_NDISUIO */
2162 #if defined(CONFIG_NATIVE_WINDOWS) || defined(__CYGWIN__)
2163 #ifndef _WIN32_WCE
2165 * These structures are undocumented for WinXP; only WinCE version is
2166 * documented. These would be included wzcsapi.h if it were available. Some
2167 * changes here have been needed to make the structures match with WinXP SP2.
2168 * It is unclear whether these work with any other version.
2171 typedef struct {
2172 LPWSTR wszGuid;
2173 } INTF_KEY_ENTRY, *PINTF_KEY_ENTRY;
2175 typedef struct {
2176 DWORD dwNumIntfs;
2177 PINTF_KEY_ENTRY pIntfs;
2178 } INTFS_KEY_TABLE, *PINTFS_KEY_TABLE;
2180 typedef struct {
2181 DWORD dwDataLen;
2182 LPBYTE pData;
2183 } RAW_DATA, *PRAW_DATA;
2185 typedef struct {
2186 LPWSTR wszGuid;
2187 LPWSTR wszDescr;
2188 ULONG ulMediaState;
2189 ULONG ulMediaType;
2190 ULONG ulPhysicalMediaType;
2191 INT nInfraMode;
2192 INT nAuthMode;
2193 INT nWepStatus;
2194 #ifndef _WIN32_WCE
2195 u8 pad[2]; /* why is this needed? */
2196 #endif /* _WIN32_WCE */
2197 DWORD dwCtlFlags;
2198 DWORD dwCapabilities; /* something added for WinXP SP2(?) */
2199 RAW_DATA rdSSID;
2200 RAW_DATA rdBSSID;
2201 RAW_DATA rdBSSIDList;
2202 RAW_DATA rdStSSIDList;
2203 RAW_DATA rdCtrlData;
2204 #ifdef UNDER_CE
2205 BOOL bInitialized;
2206 #endif
2207 DWORD nWPAMCastCipher;
2208 /* add some extra buffer for later additions since this interface is
2209 * far from stable */
2210 u8 later_additions[100];
2211 } INTF_ENTRY, *PINTF_ENTRY;
2213 #define INTF_ALL 0xffffffff
2214 #define INTF_ALL_FLAGS 0x0000ffff
2215 #define INTF_CTLFLAGS 0x00000010
2216 #define INTFCTL_ENABLED 0x8000
2217 #endif /* _WIN32_WCE */
2220 #ifdef _WIN32_WCE
2221 static int wpa_driver_ndis_rebind_adapter(struct wpa_driver_ndis_data *drv)
2223 HANDLE ndis;
2224 TCHAR multi[100];
2225 int len;
2227 len = _tcslen(drv->adapter_name);
2228 if (len > 80)
2229 return -1;
2231 ndis = CreateFile(DD_NDIS_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE,
2232 0, NULL, OPEN_EXISTING, 0, NULL);
2233 if (ndis == INVALID_HANDLE_VALUE) {
2234 wpa_printf(MSG_DEBUG, "NDIS: Failed to open file to NDIS "
2235 "device: %d", (int) GetLastError());
2236 return -1;
2239 len++;
2240 memcpy(multi, drv->adapter_name, len * sizeof(TCHAR));
2241 memcpy(&multi[len], TEXT("NDISUIO\0"), 9 * sizeof(TCHAR));
2242 len += 9;
2244 if (!DeviceIoControl(ndis, IOCTL_NDIS_REBIND_ADAPTER,
2245 multi, len * sizeof(TCHAR), NULL, 0, NULL, NULL))
2247 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDIS_REBIND_ADAPTER "
2248 "failed: 0x%x", (int) GetLastError());
2249 wpa_hexdump_ascii(MSG_DEBUG, "NDIS: rebind multi_sz",
2250 (u8 *) multi, len * sizeof(TCHAR));
2251 CloseHandle(ndis);
2252 return -1;
2255 CloseHandle(ndis);
2257 wpa_printf(MSG_DEBUG, "NDIS: Requested NDIS rebind of NDISUIO "
2258 "protocol");
2260 return 0;
2262 #endif /* _WIN32_WCE */
2265 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2266 int enable)
2268 #ifdef _WIN32_WCE
2269 HKEY hk, hk2;
2270 LONG ret;
2271 DWORD i, hnd, len;
2272 TCHAR keyname[256], devname[256];
2274 #define WZC_DRIVER TEXT("Drivers\\BuiltIn\\ZeroConfig")
2276 if (enable) {
2277 HANDLE h;
2278 h = ActivateDeviceEx(WZC_DRIVER, NULL, 0, NULL);
2279 if (h == INVALID_HANDLE_VALUE || h == 0) {
2280 wpa_printf(MSG_DEBUG, "NDIS: Failed to re-enable WZC "
2281 "- ActivateDeviceEx failed: %d",
2282 (int) GetLastError());
2283 return -1;
2286 wpa_printf(MSG_DEBUG, "NDIS: WZC re-enabled");
2287 return wpa_driver_ndis_rebind_adapter(drv);
2291 * Unfortunately, just disabling the WZC for an interface is not enough
2292 * to free NDISUIO for us, so need to disable and unload WZC completely
2293 * for now when using WinCE with NDISUIO. In addition, must request
2294 * NDISUIO protocol to be rebound to the adapter in order to free the
2295 * NDISUIO binding that WZC hold before us.
2298 /* Enumerate HKLM\Drivers\Active\* to find a handle to WZC. */
2299 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, DEVLOAD_ACTIVE_KEY, 0, 0, &hk);
2300 if (ret != ERROR_SUCCESS) {
2301 wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(DEVLOAD_ACTIVE_KEY) "
2302 "failed: %d %d", (int) ret, (int) GetLastError());
2303 return -1;
2306 for (i = 0; ; i++) {
2307 len = sizeof(keyname);
2308 ret = RegEnumKeyEx(hk, i, keyname, &len, NULL, NULL, NULL,
2309 NULL);
2310 if (ret != ERROR_SUCCESS) {
2311 wpa_printf(MSG_DEBUG, "NDIS: Could not find active "
2312 "WZC - assuming it is not running.");
2313 RegCloseKey(hk);
2314 return -1;
2317 ret = RegOpenKeyEx(hk, keyname, 0, 0, &hk2);
2318 if (ret != ERROR_SUCCESS) {
2319 wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(active dev) "
2320 "failed: %d %d",
2321 (int) ret, (int) GetLastError());
2322 continue;
2325 len = sizeof(devname);
2326 ret = RegQueryValueEx(hk2, DEVLOAD_DEVKEY_VALNAME, NULL, NULL,
2327 (LPBYTE) devname, &len);
2328 if (ret != ERROR_SUCCESS) {
2329 wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx("
2330 "DEVKEY_VALNAME) failed: %d %d",
2331 (int) ret, (int) GetLastError());
2332 RegCloseKey(hk2);
2333 continue;
2336 if (_tcscmp(devname, WZC_DRIVER) == 0)
2337 break;
2339 RegCloseKey(hk2);
2342 RegCloseKey(hk);
2344 /* Found WZC - get handle to it. */
2345 len = sizeof(hnd);
2346 ret = RegQueryValueEx(hk2, DEVLOAD_HANDLE_VALNAME, NULL, NULL,
2347 (PUCHAR) &hnd, &len);
2348 if (ret != ERROR_SUCCESS) {
2349 wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx(HANDLE_VALNAME) "
2350 "failed: %d %d", (int) ret, (int) GetLastError());
2351 RegCloseKey(hk2);
2352 return -1;
2355 RegCloseKey(hk2);
2357 /* Deactivate WZC */
2358 if (!DeactivateDevice((HANDLE) hnd)) {
2359 wpa_printf(MSG_DEBUG, "NDIS: DeactivateDevice failed: %d",
2360 (int) GetLastError());
2361 return -1;
2364 wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily");
2365 drv->wzc_disabled = 1;
2366 return wpa_driver_ndis_rebind_adapter(drv);
2368 #else /* _WIN32_WCE */
2370 HMODULE hm;
2371 DWORD (WINAPI *wzc_enum_interf)(LPWSTR pSrvAddr,
2372 PINTFS_KEY_TABLE pIntfs);
2373 DWORD (WINAPI *wzc_query_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2374 PINTF_ENTRY pIntf,
2375 LPDWORD pdwOutFlags);
2376 DWORD (WINAPI *wzc_set_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2377 PINTF_ENTRY pIntf, LPDWORD pdwOutFlags);
2378 int ret = -1, j;
2379 DWORD res;
2380 INTFS_KEY_TABLE guids;
2381 INTF_ENTRY intf;
2382 char guid[128];
2383 WCHAR *pos;
2384 DWORD flags, i;
2386 hm = LoadLibrary(TEXT("wzcsapi.dll"));
2387 if (hm == NULL) {
2388 wpa_printf(MSG_DEBUG, "NDIS: Failed to load wzcsapi.dll (%u) "
2389 "- WZC probably not running",
2390 (unsigned int) GetLastError());
2391 return -1;
2394 #ifdef _WIN32_WCE
2395 wzc_enum_interf = (void *) GetProcAddressA(hm, "WZCEnumInterfaces");
2396 wzc_query_interf = (void *) GetProcAddressA(hm, "WZCQueryInterface");
2397 wzc_set_interf = (void *) GetProcAddressA(hm, "WZCSetInterface");
2398 #else /* _WIN32_WCE */
2399 wzc_enum_interf = (void *) GetProcAddress(hm, "WZCEnumInterfaces");
2400 wzc_query_interf = (void *) GetProcAddress(hm, "WZCQueryInterface");
2401 wzc_set_interf = (void *) GetProcAddress(hm, "WZCSetInterface");
2402 #endif /* _WIN32_WCE */
2404 if (wzc_enum_interf == NULL || wzc_query_interf == NULL ||
2405 wzc_set_interf == NULL) {
2406 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces, "
2407 "WZCQueryInterface, or WZCSetInterface not found "
2408 "in wzcsapi.dll");
2409 goto fail;
2412 os_memset(&guids, 0, sizeof(guids));
2413 res = wzc_enum_interf(NULL, &guids);
2414 if (res != 0) {
2415 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces failed: %d; "
2416 "WZC service is apparently not running",
2417 (int) res);
2418 goto fail;
2421 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces: %d interfaces",
2422 (int) guids.dwNumIntfs);
2424 for (i = 0; i < guids.dwNumIntfs; i++) {
2425 pos = guids.pIntfs[i].wszGuid;
2426 for (j = 0; j < sizeof(guid); j++) {
2427 guid[j] = (char) *pos;
2428 if (*pos == 0)
2429 break;
2430 pos++;
2432 guid[sizeof(guid) - 1] = '\0';
2433 wpa_printf(MSG_DEBUG, "NDIS: intfs %d GUID '%s'",
2434 (int) i, guid);
2435 if (os_strstr(drv->ifname, guid) == NULL)
2436 continue;
2438 wpa_printf(MSG_DEBUG, "NDIS: Current interface found from "
2439 "WZC");
2440 break;
2443 if (i >= guids.dwNumIntfs) {
2444 wpa_printf(MSG_DEBUG, "NDIS: Current interface not found from "
2445 "WZC");
2446 goto fail;
2449 os_memset(&intf, 0, sizeof(intf));
2450 intf.wszGuid = guids.pIntfs[i].wszGuid;
2451 /* Set flags to verify that the structure has not changed. */
2452 intf.dwCtlFlags = -1;
2453 flags = 0;
2454 res = wzc_query_interf(NULL, INTFCTL_ENABLED, &intf, &flags);
2455 if (res != 0) {
2456 wpa_printf(MSG_DEBUG, "NDIS: Could not query flags for the "
2457 "WZC interface: %d (0x%x)",
2458 (int) res, (int) res);
2459 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2460 (unsigned int) GetLastError());
2461 goto fail;
2464 wpa_printf(MSG_DEBUG, "NDIS: WZC interface flags 0x%x dwCtlFlags 0x%x",
2465 (int) flags, (int) intf.dwCtlFlags);
2467 if (intf.dwCtlFlags == -1) {
2468 wpa_printf(MSG_DEBUG, "NDIS: Looks like wzcsapi has changed "
2469 "again - could not disable WZC");
2470 wpa_hexdump(MSG_MSGDUMP, "NDIS: intf",
2471 (u8 *) &intf, sizeof(intf));
2472 goto fail;
2475 if (enable) {
2476 if (!(intf.dwCtlFlags & INTFCTL_ENABLED)) {
2477 wpa_printf(MSG_DEBUG, "NDIS: Enabling WZC for this "
2478 "interface");
2479 intf.dwCtlFlags |= INTFCTL_ENABLED;
2480 res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2481 &flags);
2482 if (res != 0) {
2483 wpa_printf(MSG_DEBUG, "NDIS: Failed to enable "
2484 "WZC: %d (0x%x)",
2485 (int) res, (int) res);
2486 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2487 (unsigned int) GetLastError());
2488 goto fail;
2490 wpa_printf(MSG_DEBUG, "NDIS: Re-enabled WZC for this "
2491 "interface");
2492 drv->wzc_disabled = 0;
2494 } else {
2495 if (intf.dwCtlFlags & INTFCTL_ENABLED) {
2496 wpa_printf(MSG_DEBUG, "NDIS: Disabling WZC for this "
2497 "interface");
2498 intf.dwCtlFlags &= ~INTFCTL_ENABLED;
2499 res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2500 &flags);
2501 if (res != 0) {
2502 wpa_printf(MSG_DEBUG, "NDIS: Failed to "
2503 "disable WZC: %d (0x%x)",
2504 (int) res, (int) res);
2505 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2506 (unsigned int) GetLastError());
2507 goto fail;
2509 wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily "
2510 "for this interface");
2511 drv->wzc_disabled = 1;
2512 } else {
2513 wpa_printf(MSG_DEBUG, "NDIS: WZC was not enabled for "
2514 "this interface");
2518 ret = 0;
2520 fail:
2521 FreeLibrary(hm);
2523 return ret;
2524 #endif /* _WIN32_WCE */
2527 #else /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2529 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2530 int enable)
2532 return 0;
2535 #endif /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2538 #ifdef CONFIG_USE_NDISUIO
2540 * l2_packet_ndis.c is sharing the same handle to NDISUIO, so we must be able
2541 * to export this handle. This is somewhat ugly, but there is no better
2542 * mechanism available to pass data from driver interface to l2_packet wrapper.
2544 static HANDLE driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2546 HANDLE driver_ndis_get_ndisuio_handle(void)
2548 return driver_ndis_ndisuio_handle;
2550 #endif /* CONFIG_USE_NDISUIO */
2553 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv)
2555 #ifdef CONFIG_USE_NDISUIO
2556 #ifndef _WIN32_WCE
2557 #define NDISUIO_DEVICE_NAME TEXT("\\\\.\\\\Ndisuio")
2558 DWORD written;
2559 #endif /* _WIN32_WCE */
2560 drv->ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2561 GENERIC_READ | GENERIC_WRITE, 0, NULL,
2562 OPEN_EXISTING,
2563 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
2564 INVALID_HANDLE_VALUE);
2565 if (drv->ndisuio == INVALID_HANDLE_VALUE) {
2566 wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to "
2567 "NDISUIO: %d", (int) GetLastError());
2568 return -1;
2570 driver_ndis_ndisuio_handle = drv->ndisuio;
2572 #ifndef _WIN32_WCE
2573 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0,
2574 NULL, 0, &written, NULL)) {
2575 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: "
2576 "%d", (int) GetLastError());
2577 CloseHandle(drv->ndisuio);
2578 drv->ndisuio = INVALID_HANDLE_VALUE;
2579 return -1;
2581 #endif /* _WIN32_WCE */
2583 return 0;
2584 #else /* CONFIG_USE_NDISUIO */
2585 return 0;
2586 #endif /* CONFIG_USE_NDISUIO */
2590 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv)
2592 #ifdef CONFIG_USE_NDISUIO
2593 DWORD written;
2594 #define MAX_NDIS_DEVICE_NAME_LEN 256
2595 WCHAR ifname[MAX_NDIS_DEVICE_NAME_LEN];
2596 size_t len, i, pos;
2597 const char *prefix = "\\DEVICE\\";
2599 #ifdef _WIN32_WCE
2600 pos = 0;
2601 #else /* _WIN32_WCE */
2602 pos = 8;
2603 #endif /* _WIN32_WCE */
2604 len = pos + os_strlen(drv->ifname);
2605 if (len >= MAX_NDIS_DEVICE_NAME_LEN)
2606 return -1;
2607 for (i = 0; i < pos; i++)
2608 ifname[i] = (WCHAR) prefix[i];
2609 for (i = pos; i < len; i++)
2610 ifname[i] = (WCHAR) drv->ifname[i - pos];
2611 ifname[i] = L'\0';
2613 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_OPEN_DEVICE,
2614 ifname, len * sizeof(WCHAR), NULL, 0, &written,
2615 NULL)) {
2616 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_OPEN_DEVICE "
2617 "failed: %d", (int) GetLastError());
2618 wpa_hexdump_ascii(MSG_DEBUG, "NDIS: ifname",
2619 (const u8 *) ifname, len * sizeof(WCHAR));
2620 CloseHandle(drv->ndisuio);
2621 drv->ndisuio = INVALID_HANDLE_VALUE;
2622 return -1;
2625 wpa_printf(MSG_DEBUG, "NDIS: Opened NDISUIO device successfully");
2627 return 0;
2628 #else /* CONFIG_USE_NDISUIO */
2629 char ifname[128];
2630 os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", drv->ifname);
2631 drv->adapter = PacketOpenAdapter(ifname);
2632 if (drv->adapter == NULL) {
2633 wpa_printf(MSG_DEBUG, "NDIS: PacketOpenAdapter failed for "
2634 "'%s'", ifname);
2635 return -1;
2637 return 0;
2638 #endif /* CONFIG_USE_NDISUIO */
2642 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv)
2644 #ifdef CONFIG_USE_NDISUIO
2645 driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2646 if (drv->ndisuio != INVALID_HANDLE_VALUE)
2647 CloseHandle(drv->ndisuio);
2648 #else /* CONFIG_USE_NDISUIO */
2649 if (drv->adapter)
2650 PacketCloseAdapter(drv->adapter);
2651 #endif /* CONFIG_USE_NDISUIO */
2655 static void * wpa_driver_ndis_init(void *ctx, const char *ifname)
2657 struct wpa_driver_ndis_data *drv;
2658 u32 mode;
2660 drv = os_zalloc(sizeof(*drv));
2661 if (drv == NULL)
2662 return NULL;
2663 drv->ctx = ctx;
2665 * Compatibility code to strip possible prefix from the GUID. Previous
2666 * versions include \Device\NPF_ prefix for all names, but the internal
2667 * interface name is now only the GUI. Both Packet32 and NDISUIO
2668 * prefixes are supported.
2670 if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0)
2671 ifname += 12;
2672 else if (os_strncmp(ifname, "\\DEVICE\\", 8) == 0)
2673 ifname += 8;
2674 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
2676 if (wpa_driver_ndis_adapter_init(drv) < 0) {
2677 os_free(drv);
2678 return NULL;
2681 if (wpa_driver_ndis_get_names(drv) < 0) {
2682 wpa_driver_ndis_adapter_close(drv);
2683 os_free(drv);
2684 return NULL;
2687 wpa_driver_ndis_set_wzc(drv, 0);
2689 if (wpa_driver_ndis_adapter_open(drv) < 0) {
2690 wpa_driver_ndis_adapter_close(drv);
2691 os_free(drv);
2692 return NULL;
2695 if (ndis_get_oid(drv, OID_802_3_CURRENT_ADDRESS,
2696 drv->own_addr, ETH_ALEN) < 0) {
2697 wpa_printf(MSG_DEBUG, "NDIS: Get OID_802_3_CURRENT_ADDRESS "
2698 "failed");
2699 wpa_driver_ndis_adapter_close(drv);
2700 os_free(drv);
2701 return NULL;
2703 wpa_driver_ndis_get_capability(drv);
2705 /* Make sure that the driver does not have any obsolete PMKID entries.
2707 wpa_driver_ndis_flush_pmkid(drv);
2710 * Disconnect to make sure that driver re-associates if it was
2711 * connected.
2713 wpa_driver_ndis_disconnect(drv);
2715 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout, drv, NULL);
2717 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2718 drv->events = ndis_events_init(&drv->events_pipe, &drv->event_avail,
2719 drv->ifname, drv->adapter_desc);
2720 if (drv->events == NULL) {
2721 wpa_driver_ndis_deinit(drv);
2722 return NULL;
2724 eloop_register_event(drv->event_avail, sizeof(drv->event_avail),
2725 wpa_driver_ndis_event_pipe_cb, drv, NULL);
2726 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2728 #ifdef _WIN32_WCE
2729 if (ndisuio_notification_init(drv) < 0) {
2730 wpa_driver_ndis_deinit(drv);
2731 return NULL;
2733 #endif /* _WIN32_WCE */
2735 /* Set mode here in case card was configured for ad-hoc mode
2736 * previously. */
2737 mode = Ndis802_11Infrastructure;
2738 if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
2739 (char *) &mode, sizeof(mode)) < 0) {
2740 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
2741 "OID_802_11_INFRASTRUCTURE_MODE (%d)",
2742 (int) mode);
2743 /* Try to continue anyway */
2745 if (!drv->has_capability && drv->capa.enc == 0) {
2746 wpa_printf(MSG_DEBUG, "NDIS: Driver did not provide "
2747 "any wireless capabilities - assume it is "
2748 "a wired interface");
2749 drv->wired = 1;
2753 return drv;
2757 static void wpa_driver_ndis_deinit(void *priv)
2759 struct wpa_driver_ndis_data *drv = priv;
2761 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2762 if (drv->events) {
2763 eloop_unregister_event(drv->event_avail,
2764 sizeof(drv->event_avail));
2765 ndis_events_deinit(drv->events);
2767 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2769 #ifdef _WIN32_WCE
2770 ndisuio_notification_deinit(drv);
2771 #endif /* _WIN32_WCE */
2773 eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
2774 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
2775 wpa_driver_ndis_flush_pmkid(drv);
2776 wpa_driver_ndis_disconnect(drv);
2777 if (wpa_driver_ndis_radio_off(drv) < 0) {
2778 wpa_printf(MSG_DEBUG, "NDIS: failed to disassociate and turn "
2779 "radio off");
2782 wpa_driver_ndis_adapter_close(drv);
2784 if (drv->wzc_disabled)
2785 wpa_driver_ndis_set_wzc(drv, 1);
2787 #ifdef _WIN32_WCE
2788 os_free(drv->adapter_name);
2789 #endif /* _WIN32_WCE */
2790 os_free(drv->adapter_desc);
2791 os_free(drv);
2795 const struct wpa_driver_ops wpa_driver_ndis_ops = {
2796 "ndis",
2797 "Windows NDIS driver",
2798 wpa_driver_ndis_get_bssid,
2799 wpa_driver_ndis_get_ssid,
2800 wpa_driver_ndis_set_wpa,
2801 wpa_driver_ndis_set_key,
2802 wpa_driver_ndis_init,
2803 wpa_driver_ndis_deinit,
2804 NULL /* set_param */,
2805 NULL /* set_countermeasures */,
2806 NULL /* set_drop_unencrypted */,
2807 wpa_driver_ndis_scan,
2808 NULL /* get_scan_results */,
2809 wpa_driver_ndis_deauthenticate,
2810 wpa_driver_ndis_disassociate,
2811 wpa_driver_ndis_associate,
2812 NULL /* set_auth_alg */,
2813 wpa_driver_ndis_add_pmkid,
2814 wpa_driver_ndis_remove_pmkid,
2815 wpa_driver_ndis_flush_pmkid,
2816 wpa_driver_ndis_get_capa,
2817 wpa_driver_ndis_poll,
2818 wpa_driver_ndis_get_ifname,
2819 wpa_driver_ndis_get_mac_addr,
2820 NULL /* send_eapol */,
2821 NULL /* set_operstate */,
2822 NULL /* mlme_setprotection */,
2823 NULL /* get_hw_feature_data */,
2824 NULL /* set_channel */,
2825 NULL /* set_ssid */,
2826 NULL /* set_bssid */,
2827 NULL /* send_mlme */,
2828 NULL /* mlme_add_sta */,
2829 NULL /* mlme_remove_sta */,
2830 NULL /* update_ft_ies */,
2831 NULL /* send_ft_action */,
2832 wpa_driver_ndis_get_scan_results