]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/wpa/src/drivers/driver_ndis.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / wpa / src / drivers / driver_ndis.c
1 /*
2  * WPA Supplicant - Windows/NDIS driver interface
3  * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
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 #ifdef __MINGW32_VERSION
29 #include <ddk/ntddndis.h>
30 #else /* __MINGW32_VERSION */
31 #include <ntddndis.h>
32 #endif /* __MINGW32_VERSION */
33
34 #ifdef _WIN32_WCE
35 #include <winioctl.h>
36 #include <nuiouser.h>
37 #include <devload.h>
38 #endif /* _WIN32_WCE */
39
40 #include "common.h"
41 #include "driver.h"
42 #include "eloop.h"
43 #include "ieee802_11_defs.h"
44 #include "driver_ndis.h"
45
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 */
50
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);
57
58
59 /* FIX: to be removed once this can be compiled with the complete NDIS
60  * header files */
61 #ifndef OID_802_11_BSSID
62 #define OID_802_11_BSSID                        0x0d010101
63 #define OID_802_11_SSID                         0x0d010102
64 #define OID_802_11_INFRASTRUCTURE_MODE          0x0d010108
65 #define OID_802_11_ADD_WEP                      0x0D010113
66 #define OID_802_11_REMOVE_WEP                   0x0D010114
67 #define OID_802_11_DISASSOCIATE                 0x0D010115
68 #define OID_802_11_BSSID_LIST                   0x0d010217
69 #define OID_802_11_AUTHENTICATION_MODE          0x0d010118
70 #define OID_802_11_PRIVACY_FILTER               0x0d010119
71 #define OID_802_11_BSSID_LIST_SCAN              0x0d01011A
72 #define OID_802_11_WEP_STATUS                   0x0d01011B
73 #define OID_802_11_ENCRYPTION_STATUS OID_802_11_WEP_STATUS
74 #define OID_802_11_ADD_KEY                      0x0d01011D
75 #define OID_802_11_REMOVE_KEY                   0x0d01011E
76 #define OID_802_11_ASSOCIATION_INFORMATION      0x0d01011F
77 #define OID_802_11_TEST                         0x0d010120
78 #define OID_802_11_CAPABILITY                   0x0d010122
79 #define OID_802_11_PMKID                        0x0d010123
80
81 #define NDIS_802_11_LENGTH_SSID 32
82 #define NDIS_802_11_LENGTH_RATES 8
83 #define NDIS_802_11_LENGTH_RATES_EX 16
84
85 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6];
86
87 typedef struct NDIS_802_11_SSID {
88         ULONG SsidLength;
89         UCHAR Ssid[NDIS_802_11_LENGTH_SSID];
90 } NDIS_802_11_SSID;
91
92 typedef LONG NDIS_802_11_RSSI;
93
94 typedef enum NDIS_802_11_NETWORK_TYPE {
95         Ndis802_11FH,
96         Ndis802_11DS,
97         Ndis802_11OFDM5,
98         Ndis802_11OFDM24,
99         Ndis802_11NetworkTypeMax
100 } NDIS_802_11_NETWORK_TYPE;
101
102 typedef struct NDIS_802_11_CONFIGURATION_FH {
103         ULONG Length;
104         ULONG HopPattern;
105         ULONG HopSet;
106         ULONG DwellTime;
107 } NDIS_802_11_CONFIGURATION_FH;
108
109 typedef struct NDIS_802_11_CONFIGURATION {
110         ULONG Length;
111         ULONG BeaconPeriod;
112         ULONG ATIMWindow;
113         ULONG DSConfig;
114         NDIS_802_11_CONFIGURATION_FH FHConfig;
115 } NDIS_802_11_CONFIGURATION;
116
117 typedef enum NDIS_802_11_NETWORK_INFRASTRUCTURE {
118         Ndis802_11IBSS,
119         Ndis802_11Infrastructure,
120         Ndis802_11AutoUnknown,
121         Ndis802_11InfrastructureMax
122 } NDIS_802_11_NETWORK_INFRASTRUCTURE;
123
124 typedef enum NDIS_802_11_AUTHENTICATION_MODE {
125         Ndis802_11AuthModeOpen,
126         Ndis802_11AuthModeShared,
127         Ndis802_11AuthModeAutoSwitch,
128         Ndis802_11AuthModeWPA,
129         Ndis802_11AuthModeWPAPSK,
130         Ndis802_11AuthModeWPANone,
131         Ndis802_11AuthModeWPA2,
132         Ndis802_11AuthModeWPA2PSK,
133         Ndis802_11AuthModeMax
134 } NDIS_802_11_AUTHENTICATION_MODE;
135
136 typedef enum NDIS_802_11_WEP_STATUS {
137         Ndis802_11WEPEnabled,
138         Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
139         Ndis802_11WEPDisabled,
140         Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
141         Ndis802_11WEPKeyAbsent,
142         Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
143         Ndis802_11WEPNotSupported,
144         Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
145         Ndis802_11Encryption2Enabled,
146         Ndis802_11Encryption2KeyAbsent,
147         Ndis802_11Encryption3Enabled,
148         Ndis802_11Encryption3KeyAbsent
149 } NDIS_802_11_WEP_STATUS, NDIS_802_11_ENCRYPTION_STATUS;
150
151 typedef enum NDIS_802_11_PRIVACY_FILTER {
152         Ndis802_11PrivFilterAcceptAll,
153         Ndis802_11PrivFilter8021xWEP
154 } NDIS_802_11_PRIVACY_FILTER;
155
156 typedef UCHAR NDIS_802_11_RATES[NDIS_802_11_LENGTH_RATES];
157 typedef UCHAR NDIS_802_11_RATES_EX[NDIS_802_11_LENGTH_RATES_EX];
158
159 typedef struct NDIS_WLAN_BSSID_EX {
160         ULONG Length;
161         NDIS_802_11_MAC_ADDRESS MacAddress; /* BSSID */
162         UCHAR Reserved[2];
163         NDIS_802_11_SSID Ssid;
164         ULONG Privacy;
165         NDIS_802_11_RSSI Rssi;
166         NDIS_802_11_NETWORK_TYPE NetworkTypeInUse;
167         NDIS_802_11_CONFIGURATION Configuration;
168         NDIS_802_11_NETWORK_INFRASTRUCTURE InfrastructureMode;
169         NDIS_802_11_RATES_EX SupportedRates;
170         ULONG IELength;
171         UCHAR IEs[1];
172 } NDIS_WLAN_BSSID_EX;
173
174 typedef struct NDIS_802_11_BSSID_LIST_EX {
175         ULONG NumberOfItems;
176         NDIS_WLAN_BSSID_EX Bssid[1];
177 } NDIS_802_11_BSSID_LIST_EX;
178
179 typedef struct NDIS_802_11_FIXED_IEs {
180         UCHAR Timestamp[8];
181         USHORT BeaconInterval;
182         USHORT Capabilities;
183 } NDIS_802_11_FIXED_IEs;
184
185 typedef struct NDIS_802_11_WEP {
186         ULONG Length;
187         ULONG KeyIndex;
188         ULONG KeyLength;
189         UCHAR KeyMaterial[1];
190 } NDIS_802_11_WEP;
191
192 typedef ULONG NDIS_802_11_KEY_INDEX;
193 typedef ULONGLONG NDIS_802_11_KEY_RSC;
194
195 typedef struct NDIS_802_11_KEY {
196         ULONG Length;
197         ULONG KeyIndex;
198         ULONG KeyLength;
199         NDIS_802_11_MAC_ADDRESS BSSID;
200         NDIS_802_11_KEY_RSC KeyRSC;
201         UCHAR KeyMaterial[1];
202 } NDIS_802_11_KEY;
203
204 typedef struct NDIS_802_11_REMOVE_KEY {
205         ULONG Length;
206         ULONG KeyIndex;
207         NDIS_802_11_MAC_ADDRESS BSSID;
208 } NDIS_802_11_REMOVE_KEY;
209
210 typedef struct NDIS_802_11_AI_REQFI {
211         USHORT Capabilities;
212         USHORT ListenInterval;
213         NDIS_802_11_MAC_ADDRESS CurrentAPAddress;
214 } NDIS_802_11_AI_REQFI;
215
216 typedef struct NDIS_802_11_AI_RESFI {
217         USHORT Capabilities;
218         USHORT StatusCode;
219         USHORT AssociationId;
220 } NDIS_802_11_AI_RESFI;
221
222 typedef struct NDIS_802_11_ASSOCIATION_INFORMATION {
223         ULONG Length;
224         USHORT AvailableRequestFixedIEs;
225         NDIS_802_11_AI_REQFI RequestFixedIEs;
226         ULONG RequestIELength;
227         ULONG OffsetRequestIEs;
228         USHORT AvailableResponseFixedIEs;
229         NDIS_802_11_AI_RESFI ResponseFixedIEs;
230         ULONG ResponseIELength;
231         ULONG OffsetResponseIEs;
232 } NDIS_802_11_ASSOCIATION_INFORMATION;
233
234 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
235         NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
236         NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
237 } NDIS_802_11_AUTHENTICATION_ENCRYPTION;
238
239 typedef struct NDIS_802_11_CAPABILITY {
240         ULONG Length;
241         ULONG Version;
242         ULONG NoOfPMKIDs;
243         ULONG NoOfAuthEncryptPairsSupported;
244         NDIS_802_11_AUTHENTICATION_ENCRYPTION
245                 AuthenticationEncryptionSupported[1];
246 } NDIS_802_11_CAPABILITY;
247
248 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
249
250 typedef struct BSSID_INFO {
251         NDIS_802_11_MAC_ADDRESS BSSID;
252         NDIS_802_11_PMKID_VALUE PMKID;
253 } BSSID_INFO;
254
255 typedef struct NDIS_802_11_PMKID {
256         ULONG Length;
257         ULONG BSSIDInfoCount;
258         BSSID_INFO BSSIDInfo[1];
259 } NDIS_802_11_PMKID;
260
261 typedef enum NDIS_802_11_STATUS_TYPE {
262         Ndis802_11StatusType_Authentication,
263         Ndis802_11StatusType_PMKID_CandidateList = 2,
264         Ndis802_11StatusTypeMax
265 } NDIS_802_11_STATUS_TYPE;
266
267 typedef struct NDIS_802_11_STATUS_INDICATION {
268         NDIS_802_11_STATUS_TYPE StatusType;
269 } NDIS_802_11_STATUS_INDICATION;
270
271 typedef struct PMKID_CANDIDATE {
272         NDIS_802_11_MAC_ADDRESS BSSID;
273         ULONG Flags;
274 } PMKID_CANDIDATE;
275
276 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
277
278 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
279         ULONG Version;
280         ULONG NumCandidates;
281         PMKID_CANDIDATE CandidateList[1];
282 } NDIS_802_11_PMKID_CANDIDATE_LIST;
283
284 typedef struct NDIS_802_11_AUTHENTICATION_REQUEST {
285         ULONG Length;
286         NDIS_802_11_MAC_ADDRESS Bssid;
287         ULONG Flags;
288 } NDIS_802_11_AUTHENTICATION_REQUEST;
289
290 #define NDIS_802_11_AUTH_REQUEST_REAUTH                 0x01
291 #define NDIS_802_11_AUTH_REQUEST_KEYUPDATE              0x02
292 #define NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR         0x06
293 #define NDIS_802_11_AUTH_REQUEST_GROUP_ERROR            0x0E
294
295 #endif /* OID_802_11_BSSID */
296
297
298 #ifndef OID_802_11_PMKID
299 /* Platform SDK for XP did not include WPA2, so add needed definitions */
300
301 #define OID_802_11_CAPABILITY                   0x0d010122
302 #define OID_802_11_PMKID                        0x0d010123
303
304 #define Ndis802_11AuthModeWPA2 6
305 #define Ndis802_11AuthModeWPA2PSK 7
306
307 #define Ndis802_11StatusType_PMKID_CandidateList 2
308
309 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION {
310         NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported;
311         NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported;
312 } NDIS_802_11_AUTHENTICATION_ENCRYPTION;
313
314 typedef struct NDIS_802_11_CAPABILITY {
315         ULONG Length;
316         ULONG Version;
317         ULONG NoOfPMKIDs;
318         ULONG NoOfAuthEncryptPairsSupported;
319         NDIS_802_11_AUTHENTICATION_ENCRYPTION
320                 AuthenticationEncryptionSupported[1];
321 } NDIS_802_11_CAPABILITY;
322
323 typedef UCHAR NDIS_802_11_PMKID_VALUE[16];
324
325 typedef struct BSSID_INFO {
326         NDIS_802_11_MAC_ADDRESS BSSID;
327         NDIS_802_11_PMKID_VALUE PMKID;
328 } BSSID_INFO;
329
330 typedef struct NDIS_802_11_PMKID {
331         ULONG Length;
332         ULONG BSSIDInfoCount;
333         BSSID_INFO BSSIDInfo[1];
334 } NDIS_802_11_PMKID;
335
336 typedef struct PMKID_CANDIDATE {
337         NDIS_802_11_MAC_ADDRESS BSSID;
338         ULONG Flags;
339 } PMKID_CANDIDATE;
340
341 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01
342
343 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST {
344         ULONG Version;
345         ULONG NumCandidates;
346         PMKID_CANDIDATE CandidateList[1];
347 } NDIS_802_11_PMKID_CANDIDATE_LIST;
348
349 #endif /* OID_802_11_CAPABILITY */
350
351
352 #ifdef CONFIG_USE_NDISUIO
353 #ifndef _WIN32_WCE
354 #ifdef __MINGW32_VERSION
355 typedef ULONG NDIS_OID;
356 #endif /* __MINGW32_VERSION */
357 /* from nuiouser.h */
358 #define FSCTL_NDISUIO_BASE      FILE_DEVICE_NETWORK
359
360 #define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \
361         CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access)
362
363 #define IOCTL_NDISUIO_OPEN_DEVICE \
364         _NDISUIO_CTL_CODE(0x200, METHOD_BUFFERED, \
365                           FILE_READ_ACCESS | FILE_WRITE_ACCESS)
366
367 #define IOCTL_NDISUIO_QUERY_OID_VALUE \
368         _NDISUIO_CTL_CODE(0x201, METHOD_BUFFERED, \
369                           FILE_READ_ACCESS | FILE_WRITE_ACCESS)
370
371 #define IOCTL_NDISUIO_SET_OID_VALUE \
372         _NDISUIO_CTL_CODE(0x205, METHOD_BUFFERED, \
373                           FILE_READ_ACCESS | FILE_WRITE_ACCESS)
374
375 #define IOCTL_NDISUIO_SET_ETHER_TYPE \
376         _NDISUIO_CTL_CODE(0x202, METHOD_BUFFERED, \
377                           FILE_READ_ACCESS | FILE_WRITE_ACCESS)
378
379 #define IOCTL_NDISUIO_QUERY_BINDING \
380         _NDISUIO_CTL_CODE(0x203, METHOD_BUFFERED, \
381                           FILE_READ_ACCESS | FILE_WRITE_ACCESS)
382
383 #define IOCTL_NDISUIO_BIND_WAIT \
384         _NDISUIO_CTL_CODE(0x204, METHOD_BUFFERED, \
385                           FILE_READ_ACCESS | FILE_WRITE_ACCESS)
386
387 typedef struct _NDISUIO_QUERY_OID
388 {
389     NDIS_OID Oid;
390     UCHAR Data[sizeof(ULONG)];
391 } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;
392
393 typedef struct _NDISUIO_SET_OID
394 {
395     NDIS_OID Oid;
396     UCHAR Data[sizeof(ULONG)];
397 } NDISUIO_SET_OID, *PNDISUIO_SET_OID;
398
399 typedef struct _NDISUIO_QUERY_BINDING
400 {
401         ULONG BindingIndex;
402         ULONG DeviceNameOffset;
403         ULONG DeviceNameLength;
404         ULONG DeviceDescrOffset;
405         ULONG DeviceDescrLength;
406 } NDISUIO_QUERY_BINDING, *PNDISUIO_QUERY_BINDING;
407 #endif /* _WIN32_WCE */
408 #endif /* CONFIG_USE_NDISUIO */
409
410
411 static int ndis_get_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
412                         char *data, size_t len)
413 {
414 #ifdef CONFIG_USE_NDISUIO
415         NDISUIO_QUERY_OID *o;
416         size_t buflen = sizeof(*o) + len;
417         DWORD written;
418         int ret;
419         size_t hdrlen;
420
421         o = os_zalloc(buflen);
422         if (o == NULL)
423                 return -1;
424         o->Oid = oid;
425 #ifdef _WIN32_WCE
426         o->ptcDeviceName = drv->adapter_name;
427 #endif /* _WIN32_WCE */
428         if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_OID_VALUE,
429                              o, sizeof(NDISUIO_QUERY_OID), o, buflen, &written,
430                              NULL)) {
431                 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_QUERY_OID_VALUE "
432                            "failed (oid=%08x): %d", oid, (int) GetLastError());
433                 os_free(o);
434                 return -1;
435         }
436         hdrlen = sizeof(NDISUIO_QUERY_OID) - sizeof(o->Data);
437         if (written < hdrlen) {
438                 wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d); "
439                            "too short", oid, (unsigned int) written);
440                 os_free(o);
441                 return -1;
442         }
443         written -= hdrlen;
444         if (written > len) {
445                 wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d) > "
446                            "len (%d)",oid, (unsigned int) written, len);
447                 os_free(o);
448                 return -1;
449         }
450         os_memcpy(data, o->Data, written);
451         ret = written;
452         os_free(o);
453         return ret;
454 #else /* CONFIG_USE_NDISUIO */
455         char *buf;
456         PACKET_OID_DATA *o;
457         int ret;
458
459         buf = os_zalloc(sizeof(*o) + len);
460         if (buf == NULL)
461                 return -1;
462         o = (PACKET_OID_DATA *) buf;
463         o->Oid = oid;
464         o->Length = len;
465
466         if (!PacketRequest(drv->adapter, FALSE, o)) {
467                 wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
468                            __func__, oid, len);
469                 os_free(buf);
470                 return -1;
471         }
472         if (o->Length > len) {
473                 wpa_printf(MSG_DEBUG, "%s: oid=0x%x Length (%d) > len (%d)",
474                            __func__, oid, (unsigned int) o->Length, len);
475                 os_free(buf);
476                 return -1;
477         }
478         os_memcpy(data, o->Data, o->Length);
479         ret = o->Length;
480         os_free(buf);
481         return ret;
482 #endif /* CONFIG_USE_NDISUIO */
483 }
484
485
486 static int ndis_set_oid(struct wpa_driver_ndis_data *drv, unsigned int oid,
487                         const char *data, size_t len)
488 {
489 #ifdef CONFIG_USE_NDISUIO
490         NDISUIO_SET_OID *o;
491         size_t buflen, reallen;
492         DWORD written;
493         char txt[50];
494
495         os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
496         wpa_hexdump_key(MSG_MSGDUMP, txt, (const u8 *) data, len);
497
498         buflen = sizeof(*o) + len;
499         reallen = buflen - sizeof(o->Data);
500         o = os_zalloc(buflen);
501         if (o == NULL)
502                 return -1;
503         o->Oid = oid;
504 #ifdef _WIN32_WCE
505         o->ptcDeviceName = drv->adapter_name;
506 #endif /* _WIN32_WCE */
507         if (data)
508                 os_memcpy(o->Data, data, len);
509         if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_SET_OID_VALUE,
510                              o, reallen, NULL, 0, &written, NULL)) {
511                 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_SET_OID_VALUE "
512                            "(oid=%08x) failed: %d", oid, (int) GetLastError());
513                 os_free(o);
514                 return -1;
515         }
516         os_free(o);
517         return 0;
518 #else /* CONFIG_USE_NDISUIO */
519         char *buf;
520         PACKET_OID_DATA *o;
521         char txt[50];
522
523         os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid);
524         wpa_hexdump_key(MSG_MSGDUMP, txt, (const u8 *) data, len);
525
526         buf = os_zalloc(sizeof(*o) + len);
527         if (buf == NULL)
528                 return -1;
529         o = (PACKET_OID_DATA *) buf;
530         o->Oid = oid;
531         o->Length = len;
532         if (data)
533                 os_memcpy(o->Data, data, len);
534
535         if (!PacketRequest(drv->adapter, TRUE, o)) {
536                 wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed",
537                            __func__, oid, len);
538                 os_free(buf);
539                 return -1;
540         }
541         os_free(buf);
542         return 0;
543 #endif /* CONFIG_USE_NDISUIO */
544 }
545
546
547 static int ndis_set_auth_mode(struct wpa_driver_ndis_data *drv, int mode)
548 {
549         u32 auth_mode = mode;
550         if (ndis_set_oid(drv, OID_802_11_AUTHENTICATION_MODE,
551                          (char *) &auth_mode, sizeof(auth_mode)) < 0) {
552                 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
553                            "OID_802_11_AUTHENTICATION_MODE (%d)",
554                            (int) auth_mode);
555                 return -1;
556         }
557         return 0;
558 }
559
560
561 static int ndis_get_auth_mode(struct wpa_driver_ndis_data *drv)
562 {
563         u32 auth_mode;
564         int res;
565         res = ndis_get_oid(drv, OID_802_11_AUTHENTICATION_MODE,
566                            (char *) &auth_mode, sizeof(auth_mode));
567         if (res != sizeof(auth_mode)) {
568                 wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
569                            "OID_802_11_AUTHENTICATION_MODE");
570                 return -1;
571         }
572         return auth_mode;
573 }
574
575
576 static int ndis_set_encr_status(struct wpa_driver_ndis_data *drv, int encr)
577 {
578         u32 encr_status = encr;
579         if (ndis_set_oid(drv, OID_802_11_ENCRYPTION_STATUS,
580                          (char *) &encr_status, sizeof(encr_status)) < 0) {
581                 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
582                            "OID_802_11_ENCRYPTION_STATUS (%d)", encr);
583                 return -1;
584         }
585         return 0;
586 }
587
588
589 static int ndis_get_encr_status(struct wpa_driver_ndis_data *drv)
590 {
591         u32 encr;
592         int res;
593         res = ndis_get_oid(drv, OID_802_11_ENCRYPTION_STATUS,
594                            (char *) &encr, sizeof(encr));
595         if (res != sizeof(encr)) {
596                 wpa_printf(MSG_DEBUG, "NDIS: Failed to get "
597                            "OID_802_11_ENCRYPTION_STATUS");
598                 return -1;
599         }
600         return encr;
601 }
602
603
604 static int wpa_driver_ndis_get_bssid(void *priv, u8 *bssid)
605 {
606         struct wpa_driver_ndis_data *drv = priv;
607
608         if (drv->wired) {
609                 /*
610                  * Report PAE group address as the "BSSID" for wired
611                  * connection.
612                  */
613                 bssid[0] = 0x01;
614                 bssid[1] = 0x80;
615                 bssid[2] = 0xc2;
616                 bssid[3] = 0x00;
617                 bssid[4] = 0x00;
618                 bssid[5] = 0x03;
619                 return 0;
620         }
621
622         return ndis_get_oid(drv, OID_802_11_BSSID, (char *) bssid, ETH_ALEN) <
623                 0 ? -1 : 0;
624 }
625
626
627 static int wpa_driver_ndis_get_ssid(void *priv, u8 *ssid)
628 {
629         struct wpa_driver_ndis_data *drv = priv;
630         NDIS_802_11_SSID buf;
631         int res;
632
633         res = ndis_get_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
634         if (res < 4) {
635                 wpa_printf(MSG_DEBUG, "NDIS: Failed to get SSID");
636                 if (drv->wired) {
637                         wpa_printf(MSG_DEBUG, "NDIS: Allow get_ssid failure "
638                                    "with a wired interface");
639                         return 0;
640                 }
641                 return -1;
642         }
643         os_memcpy(ssid, buf.Ssid, buf.SsidLength);
644         return buf.SsidLength;
645 }
646
647
648 static int wpa_driver_ndis_set_ssid(struct wpa_driver_ndis_data *drv,
649                                     const u8 *ssid, size_t ssid_len)
650 {
651         NDIS_802_11_SSID buf;
652
653         os_memset(&buf, 0, sizeof(buf));
654         buf.SsidLength = ssid_len;
655         os_memcpy(buf.Ssid, ssid, ssid_len);
656         /*
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
659          * that point.
660          */
661         drv->radio_enabled = 1;
662         return ndis_set_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf));
663 }
664
665
666 /* Disconnect using OID_802_11_DISASSOCIATE. This will also turn the radio off.
667  */
668 static int wpa_driver_ndis_radio_off(struct wpa_driver_ndis_data *drv)
669 {
670         drv->radio_enabled = 0;
671         return ndis_set_oid(drv, OID_802_11_DISASSOCIATE, "    ", 4);
672 }
673
674
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)
677 {
678         char ssid[32];
679         int i;
680         for (i = 0; i < 32; i++)
681                 ssid[i] = rand() & 0xff;
682         return wpa_driver_ndis_set_ssid(drv, (u8 *) ssid, 32);
683 }
684
685
686 static int wpa_driver_ndis_deauthenticate(void *priv, const u8 *addr,
687                                           int reason_code)
688 {
689         struct wpa_driver_ndis_data *drv = priv;
690         return wpa_driver_ndis_disconnect(drv);
691 }
692
693
694 static int wpa_driver_ndis_disassociate(void *priv, const u8 *addr,
695                                         int reason_code)
696 {
697         struct wpa_driver_ndis_data *drv = priv;
698         return wpa_driver_ndis_disconnect(drv);
699 }
700
701
702 static int wpa_driver_ndis_set_wpa(void *priv, int enabled)
703 {
704         wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
705         return 0;
706 }
707
708
709 static void wpa_driver_ndis_scan_timeout(void *eloop_ctx, void *timeout_ctx)
710 {
711         wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
712         wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
713 }
714
715
716 static int wpa_driver_ndis_scan(void *priv, const u8 *ssid, size_t ssid_len)
717 {
718         struct wpa_driver_ndis_data *drv = priv;
719         int res;
720
721         if (!drv->radio_enabled) {
722                 wpa_printf(MSG_DEBUG, "NDIS: turning radio on before the first"
723                            " scan");
724                 if (wpa_driver_ndis_disconnect(drv) < 0) {
725                         wpa_printf(MSG_DEBUG, "NDIS: failed to enable radio");
726                 }
727                 drv->radio_enabled = 1;
728         }
729
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,
733                                drv->ctx);
734         return res;
735 }
736
737
738 static struct wpa_scan_res * wpa_driver_ndis_add_scan_ssid(
739         struct wpa_scan_res *r, NDIS_802_11_SSID *ssid)
740 {
741         struct wpa_scan_res *nr;
742         u8 *pos;
743
744         if (wpa_scan_get_ie(r, WLAN_EID_SSID))
745                 return r; /* SSID IE already present */
746
747         if (ssid->SsidLength == 0 || ssid->SsidLength > 32)
748                 return r; /* No valid SSID inside scan data */
749
750         nr = os_realloc(r, sizeof(*r) + r->ie_len + 2 + ssid->SsidLength);
751         if (nr == NULL)
752                 return r;
753
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;
759
760         return nr;
761 }
762
763
764 static struct wpa_scan_results * wpa_driver_ndis_get_scan_results(void *priv)
765 {
766         struct wpa_driver_ndis_data *drv = priv;
767         NDIS_802_11_BSSID_LIST_EX *b;
768         size_t blen, count, i;
769         int len;
770         char *pos;
771         struct wpa_scan_results *results;
772         struct wpa_scan_res *r;
773
774         blen = 65535;
775         b = os_zalloc(blen);
776         if (b == NULL)
777                 return NULL;
778         len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
779         if (len < 0) {
780                 wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
781                 os_free(b);
782                 return NULL;
783         }
784         count = b->NumberOfItems;
785
786         results = os_zalloc(sizeof(*results));
787         if (results == NULL) {
788                 os_free(b);
789                 return NULL;
790         }
791         results->res = os_zalloc(count * sizeof(struct wpa_scan_res *));
792         if (results->res == NULL) {
793                 os_free(results);
794                 os_free(b);
795                 return NULL;
796         }
797
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;
802
803                 if (bss->IELength < sizeof(NDIS_802_11_FIXED_IEs)) {
804                         wpa_printf(MSG_DEBUG, "NDIS: too small IELength=%d",
805                                    (int) bss->IELength);
806                         break;
807                 }
808                 if (((char *) bss->IEs) + bss->IELength  > (char *) b + blen) {
809                         /*
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.
814                          */
815                         wpa_printf(MSG_DEBUG, "NDIS: skipped invalid scan "
816                                    "result IE (BSSID=" MACSTR ") IELength=%d",
817                                    MAC2STR(bss->MacAddress),
818                                    (int) bss->IELength);
819                         break;
820                 }
821
822                 r = os_zalloc(sizeof(*r) + bss->IELength -
823                               sizeof(NDIS_802_11_FIXED_IEs));
824                 if (r == NULL)
825                         break;
826
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);
838
839                 results->res[results->num++] = r;
840
841                 pos += bss->Length;
842                 if (pos > (char *) b + blen)
843                         break;
844         }
845
846         os_free(b);
847
848         return results;
849 }
850
851
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)
855 {
856         NDIS_802_11_REMOVE_KEY rkey;
857         NDIS_802_11_KEY_INDEX index;
858         int res, res2;
859
860         os_memset(&rkey, 0, sizeof(rkey));
861
862         rkey.Length = sizeof(rkey);
863         rkey.KeyIndex = key_idx;
864         if (pairwise)
865                 rkey.KeyIndex |= 1 << 30;
866         os_memcpy(rkey.BSSID, bssid, ETH_ALEN);
867
868         res = ndis_set_oid(drv, OID_802_11_REMOVE_KEY, (char *) &rkey,
869                            sizeof(rkey));
870         if (!pairwise) {
871                 index = key_idx;
872                 res2 = ndis_set_oid(drv, OID_802_11_REMOVE_WEP,
873                                     (char *) &index, sizeof(index));
874         } else
875                 res2 = 0;
876
877         if (res < 0 && res2 < 0)
878                 return -1;
879         return 0;
880 }
881
882
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)
886 {
887         NDIS_802_11_WEP *wep;
888         size_t len;
889         int res;
890
891         len = 12 + key_len;
892         wep = os_zalloc(len);
893         if (wep == NULL)
894                 return -1;
895         wep->Length = len;
896         wep->KeyIndex = key_idx;
897         if (set_tx)
898                 wep->KeyIndex |= 1 << 31;
899 #if 0 /* Setting bit30 does not seem to work with some NDIS drivers */
900         if (pairwise)
901                 wep->KeyIndex |= 1 << 30;
902 #endif
903         wep->KeyLength = key_len;
904         os_memcpy(wep->KeyMaterial, key, key_len);
905
906         wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_WEP",
907                         (u8 *) wep, len);
908         res = ndis_set_oid(drv, OID_802_11_ADD_WEP, (char *) wep, len);
909
910         os_free(wep);
911
912         return res;
913 }
914
915
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)
920 {
921         struct wpa_driver_ndis_data *drv = priv;
922         size_t len, i;
923         NDIS_802_11_KEY *nkey;
924         int res, pairwise;
925         u8 bssid[ETH_ALEN];
926
927         if (addr == NULL || os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
928                                       ETH_ALEN) == 0) {
929                 /* Group Key */
930                 pairwise = 0;
931                 if (wpa_driver_ndis_get_bssid(drv, bssid) < 0)
932                         os_memset(bssid, 0xff, ETH_ALEN);
933         } else {
934                 /* Pairwise Key */
935                 pairwise = 1;
936                 os_memcpy(bssid, addr, ETH_ALEN);
937         }
938
939         if (alg == WPA_ALG_NONE || key_len == 0) {
940                 return wpa_driver_ndis_remove_key(drv, key_idx, addr, bssid,
941                                                   pairwise);
942         }
943
944         if (alg == WPA_ALG_WEP) {
945                 return wpa_driver_ndis_add_wep(drv, pairwise, key_idx, set_tx,
946                                                key, key_len);
947         }
948
949         len = 12 + 6 + 6 + 8 + key_len;
950
951         nkey = os_zalloc(len);
952         if (nkey == NULL)
953                 return -1;
954
955         nkey->Length = len;
956         nkey->KeyIndex = key_idx;
957         if (set_tx)
958                 nkey->KeyIndex |= 1 << 31;
959         if (pairwise)
960                 nkey->KeyIndex |= 1 << 30;
961         if (seq && seq_len)
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);
968         }
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);
973         } else {
974                 os_memcpy(nkey->KeyMaterial, key, key_len);
975         }
976
977         wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_KEY",
978                         (u8 *) nkey, len);
979         res = ndis_set_oid(drv, OID_802_11_ADD_KEY, (char *) nkey, len);
980         os_free(nkey);
981
982         return res;
983 }
984
985
986 static int
987 wpa_driver_ndis_associate(void *priv,
988                           struct wpa_driver_associate_params *params)
989 {
990         struct wpa_driver_ndis_data *drv = priv;
991         u32 auth_mode, encr, priv_mode, mode;
992
993         drv->mode = params->mode;
994
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
1000                  * IBSS mode. */
1001                 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
1002                 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1003                                        drv, NULL);
1004         } else
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)",
1010                            (int) mode);
1011                 /* Try to continue anyway */
1012         }
1013
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 };
1018                 int i;
1019                 for (i = 0; i < 4; i++) {
1020                         if (!params->wep_key[i])
1021                                 continue;
1022                         wpa_printf(MSG_DEBUG, "NDIS: Re-setting static WEP "
1023                                    "key %d", i);
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]);
1028                 }
1029         }
1030
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;
1035                         else
1036                                 auth_mode = Ndis802_11AuthModeShared;
1037                 } else
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;
1044                 else
1045                         auth_mode = Ndis802_11AuthModeWPA2;
1046 #ifdef CONFIG_WPS
1047         } else if (params->key_mgmt_suite == KEY_MGMT_WPS) {
1048                 auth_mode = Ndis802_11AuthModeOpen;
1049                 priv_mode = Ndis802_11PrivFilterAcceptAll;
1050 #endif /* CONFIG_WPS */
1051         } else {
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;
1057                 else
1058                         auth_mode = Ndis802_11AuthModeWPA;
1059         }
1060
1061         switch (params->pairwise_suite) {
1062         case CIPHER_CCMP:
1063                 encr = Ndis802_11Encryption3Enabled;
1064                 break;
1065         case CIPHER_TKIP:
1066                 encr = Ndis802_11Encryption2Enabled;
1067                 break;
1068         case CIPHER_WEP40:
1069         case CIPHER_WEP104:
1070                 encr = Ndis802_11Encryption1Enabled;
1071                 break;
1072         case CIPHER_NONE:
1073                 if (params->group_suite == CIPHER_CCMP)
1074                         encr = Ndis802_11Encryption3Enabled;
1075                 else if (params->group_suite == CIPHER_TKIP)
1076                         encr = Ndis802_11Encryption2Enabled;
1077                 else
1078                         encr = Ndis802_11EncryptionDisabled;
1079                 break;
1080         default:
1081                 encr = Ndis802_11EncryptionDisabled;
1082         };
1083
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)",
1088                            (int) priv_mode);
1089                 /* Try to continue anyway */
1090         }
1091
1092         ndis_set_auth_mode(drv, auth_mode);
1093         ndis_set_encr_status(drv, encr);
1094
1095         if (params->bssid) {
1096                 ndis_set_oid(drv, OID_802_11_BSSID, (char *) params->bssid,
1097                              ETH_ALEN);
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",
1101                              ETH_ALEN);
1102                 drv->oid_bssid_set = 0;
1103         }
1104
1105         return wpa_driver_ndis_set_ssid(drv, params->ssid, params->ssid_len);
1106 }
1107
1108
1109 static int wpa_driver_ndis_set_pmkid(struct wpa_driver_ndis_data *drv)
1110 {
1111         int len, count, i, ret;
1112         struct ndis_pmkid_entry *entry;
1113         NDIS_802_11_PMKID *p;
1114
1115         count = 0;
1116         entry = drv->pmkid;
1117         while (entry) {
1118                 count++;
1119                 if (count >= drv->no_of_pmkid)
1120                         break;
1121                 entry = entry->next;
1122         }
1123         len = 8 + count * sizeof(BSSID_INFO);
1124         p = os_zalloc(len);
1125         if (p == NULL)
1126                 return -1;
1127
1128         p->Length = len;
1129         p->BSSIDInfoCount = count;
1130         entry = drv->pmkid;
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;
1135         }
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);
1138         os_free(p);
1139         return ret;
1140 }
1141
1142
1143 static int wpa_driver_ndis_add_pmkid(void *priv, const u8 *bssid,
1144                                      const u8 *pmkid)
1145 {
1146         struct wpa_driver_ndis_data *drv = priv;
1147         struct ndis_pmkid_entry *entry, *prev;
1148
1149         if (drv->no_of_pmkid == 0)
1150                 return 0;
1151
1152         prev = NULL;
1153         entry = drv->pmkid;
1154         while (entry) {
1155                 if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0)
1156                         break;
1157                 prev = entry;
1158                 entry = entry->next;
1159         }
1160
1161         if (entry) {
1162                 /* Replace existing entry for this BSSID and move it into the
1163                  * beginning of the list. */
1164                 os_memcpy(entry->pmkid, pmkid, 16);
1165                 if (prev) {
1166                         prev->next = entry->next;
1167                         entry->next = drv->pmkid;
1168                         drv->pmkid = entry;
1169                 }
1170         } else {
1171                 entry = os_malloc(sizeof(*entry));
1172                 if (entry) {
1173                         os_memcpy(entry->bssid, bssid, ETH_ALEN);
1174                         os_memcpy(entry->pmkid, pmkid, 16);
1175                         entry->next = drv->pmkid;
1176                         drv->pmkid = entry;
1177                 }
1178         }
1179
1180         return wpa_driver_ndis_set_pmkid(drv);
1181 }
1182
1183
1184 static int wpa_driver_ndis_remove_pmkid(void *priv, const u8 *bssid,
1185                                         const u8 *pmkid)
1186 {
1187         struct wpa_driver_ndis_data *drv = priv;
1188         struct ndis_pmkid_entry *entry, *prev;
1189
1190         if (drv->no_of_pmkid == 0)
1191                 return 0;
1192
1193         entry = drv->pmkid;
1194         prev = NULL;
1195         while (entry) {
1196                 if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0 &&
1197                     os_memcmp(entry->pmkid, pmkid, 16) == 0) {
1198                         if (prev)
1199                                 prev->next = entry->next;
1200                         else
1201                                 drv->pmkid = entry->next;
1202                         os_free(entry);
1203                         break;
1204                 }
1205                 prev = entry;
1206                 entry = entry->next;
1207         }
1208         return wpa_driver_ndis_set_pmkid(drv);
1209 }
1210
1211
1212 static int wpa_driver_ndis_flush_pmkid(void *priv)
1213 {
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;
1218
1219         if (drv->no_of_pmkid == 0)
1220                 return 0;
1221
1222         pmkid = drv->pmkid;
1223         drv->pmkid = NULL;
1224         while (pmkid) {
1225                 prev = pmkid;
1226                 pmkid = pmkid->next;
1227                 os_free(prev);
1228         }
1229
1230         /*
1231          * Some drivers may refuse OID_802_11_PMKID if authMode is not set to
1232          * WPA2, so change authMode temporarily, if needed.
1233          */
1234         prev_authmode = ndis_get_auth_mode(drv);
1235         if (prev_authmode != Ndis802_11AuthModeWPA2)
1236                 ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA2);
1237
1238         os_memset(&p, 0, sizeof(p));
1239         p.Length = 8;
1240         p.BSSIDInfoCount = 0;
1241         wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID (flush)",
1242                     (u8 *) &p, 8);
1243         ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) &p, 8);
1244
1245         if (prev_authmode != Ndis802_11AuthModeWPA2)
1246                 ndis_set_auth_mode(drv, prev_authmode);
1247
1248         return ret;
1249 }
1250
1251
1252 static int wpa_driver_ndis_get_associnfo(struct wpa_driver_ndis_data *drv)
1253 {
1254         char buf[512], *pos;
1255         NDIS_802_11_ASSOCIATION_INFORMATION *ai;
1256         int len;
1257         union wpa_event_data data;
1258         NDIS_802_11_BSSID_LIST_EX *b;
1259         size_t blen, i;
1260
1261         len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION, buf,
1262                            sizeof(buf));
1263         if (len < 0) {
1264                 wpa_printf(MSG_DEBUG, "NDIS: failed to get association "
1265                            "information");
1266                 return -1;
1267         }
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,
1276                                    buf, sizeof(buf));
1277                 if (len < -1) {
1278                         wpa_printf(MSG_DEBUG, "NDIS: re-reading association "
1279                                    "information failed");
1280                         return -1;
1281                 }
1282                 if (len > sizeof(buf)) {
1283                         wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association"
1284                                    " information length %d (re-read)", len);
1285                         len = sizeof(buf);
1286                 }
1287         }
1288         wpa_hexdump(MSG_MSGDUMP, "NDIS: association information",
1289                     (u8 *) buf, len);
1290         if (len < sizeof(*ai)) {
1291                 wpa_printf(MSG_DEBUG, "NDIS: too short association "
1292                            "information");
1293                 return -1;
1294         }
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);
1301
1302         if (ai->OffsetRequestIEs + ai->RequestIELength > (unsigned) len ||
1303             ai->OffsetResponseIEs + ai->ResponseIELength > (unsigned) len) {
1304                 wpa_printf(MSG_DEBUG, "NDIS: association information - "
1305                            "IE overflow");
1306                 return -1;
1307         }
1308
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);
1313
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;
1319
1320         blen = 65535;
1321         b = os_zalloc(blen);
1322         if (b == NULL)
1323                 goto skip_scan_results;
1324         len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen);
1325         if (len < 0) {
1326                 wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results");
1327                 os_free(b);
1328                 b = NULL;
1329                 goto skip_scan_results;
1330         }
1331         wpa_printf(MSG_DEBUG, "NDIS: %d BSSID items to process for AssocInfo",
1332                    (unsigned int) b->NumberOfItems);
1333
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 =
1340                                 ((u8 *) bss->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);
1347                         break;
1348                 }
1349                 pos += bss->Length;
1350                 if (pos > (char *) b + blen)
1351                         break;
1352         }
1353
1354 skip_scan_results:
1355         wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
1356
1357         os_free(b);
1358
1359         return 0;
1360 }
1361
1362
1363 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx)
1364 {
1365         struct wpa_driver_ndis_data *drv = eloop_ctx;
1366         u8 bssid[ETH_ALEN];
1367         int poll;
1368
1369         if (drv->wired)
1370                 return;
1371
1372         if (wpa_driver_ndis_get_bssid(drv, bssid)) {
1373                 /* Disconnected */
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);
1377                 }
1378         } else {
1379                 /* Connected */
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);
1384                 }
1385         }
1386
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
1391          * has been formed.
1392          */
1393         poll = drv->mode == IEEE80211_MODE_IBSS;
1394 #ifndef CONFIG_NDIS_EVENTS_INTEGRATED
1395 #ifndef _WIN32_WCE
1396         poll = 1;
1397 #endif /* _WIN32_WCE */
1398 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
1399
1400         if (poll) {
1401                 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout,
1402                                         drv, NULL);
1403         }
1404 }
1405
1406
1407 static void wpa_driver_ndis_poll(void *priv)
1408 {
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);
1412 }
1413
1414
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)
1418 {
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);
1423         }
1424 }
1425
1426
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)
1430 {
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);
1434 }
1435
1436
1437 static void wpa_driver_ndis_event_auth(struct wpa_driver_ndis_data *drv,
1438                                        const u8 *data, size_t data_len)
1439 {
1440         NDIS_802_11_AUTHENTICATION_REQUEST *req;
1441         int pairwise = 0, group = 0;
1442         union wpa_event_data event;
1443
1444         if (data_len < sizeof(*req)) {
1445                 wpa_printf(MSG_DEBUG, "NDIS: Too short Authentication Request "
1446                            "Event (len=%d)", data_len);
1447                 return;
1448         }
1449         req = (NDIS_802_11_AUTHENTICATION_REQUEST *) data;
1450
1451         wpa_printf(MSG_DEBUG, "NDIS: Authentication Request Event: "
1452                    "Bssid " MACSTR " Flags 0x%x",
1453                    MAC2STR(req->Bssid), (int) req->Flags);
1454
1455         if ((req->Flags & NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR) ==
1456             NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR)
1457                 pairwise = 1;
1458         else if ((req->Flags & NDIS_802_11_AUTH_REQUEST_GROUP_ERROR) ==
1459             NDIS_802_11_AUTH_REQUEST_GROUP_ERROR)
1460                 group = 1;
1461
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,
1466                                      &event);
1467         }
1468 }
1469
1470
1471 static void wpa_driver_ndis_event_pmkid(struct wpa_driver_ndis_data *drv,
1472                                         const u8 *data, size_t data_len)
1473 {
1474         NDIS_802_11_PMKID_CANDIDATE_LIST *pmkid;
1475         size_t i;
1476         union wpa_event_data event;
1477
1478         if (data_len < 8) {
1479                 wpa_printf(MSG_DEBUG, "NDIS: Too short PMKID Candidate List "
1480                            "Event (len=%d)", data_len);
1481                 return;
1482         }
1483         pmkid = (NDIS_802_11_PMKID_CANDIDATE_LIST *) data;
1484         wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List Event - Version %d "
1485                    "NumCandidates %d",
1486                    (int) pmkid->Version, (int) pmkid->NumCandidates);
1487
1488         if (pmkid->Version != 1) {
1489                 wpa_printf(MSG_DEBUG, "NDIS: Unsupported PMKID Candidate List "
1490                            "Version %d", (int) pmkid->Version);
1491                 return;
1492         }
1493
1494         if (data_len < 8 + pmkid->NumCandidates * sizeof(PMKID_CANDIDATE)) {
1495                 wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List underflow");
1496                 return;
1497         }
1498
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,
1509                                      &event);
1510         }
1511 }
1512
1513
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)
1518 {
1519         NDIS_802_11_STATUS_INDICATION *status;
1520
1521         if (data == NULL || data_len < sizeof(*status))
1522                 return;
1523
1524         wpa_hexdump(MSG_DEBUG, "NDIS: Media Specific Indication",
1525                     data, data_len);
1526
1527         status = (NDIS_802_11_STATUS_INDICATION *) data;
1528         data += sizeof(status);
1529         data_len -= sizeof(status);
1530
1531         switch (status->StatusType) {
1532         case Ndis802_11StatusType_Authentication:
1533                 wpa_driver_ndis_event_auth(drv, data, data_len);
1534                 break;
1535         case Ndis802_11StatusType_PMKID_CandidateList:
1536                 wpa_driver_ndis_event_pmkid(drv, data, data_len);
1537                 break;
1538         default:
1539                 wpa_printf(MSG_DEBUG, "NDIS: Unknown StatusType %d",
1540                            (int) status->StatusType);
1541                 break;
1542         }
1543 }
1544
1545
1546 /* Called when an adapter is added */
1547 void wpa_driver_ndis_event_adapter_arrival(struct wpa_driver_ndis_data *drv)
1548 {
1549         union wpa_event_data event;
1550         int i;
1551
1552         wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Arrival");
1553
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 "
1560                                    "(%d) failed", i);
1561                         os_sleep(1, 0);
1562                 } else {
1563                         wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialized");
1564                         break;
1565                 }
1566         }
1567
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);
1573 }
1574
1575
1576 /* Called when an adapter is removed */
1577 void wpa_driver_ndis_event_adapter_removal(struct wpa_driver_ndis_data *drv)
1578 {
1579         union wpa_event_data event;
1580
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);
1587 }
1588
1589
1590 static void
1591 wpa_driver_ndis_get_wpa_capability(struct wpa_driver_ndis_data *drv)
1592 {
1593         wpa_printf(MSG_DEBUG, "NDIS: verifying driver WPA capability");
1594
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;
1599         }
1600
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 "
1604                            "supported");
1605                 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1606         }
1607
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;
1612         }
1613
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;
1618         }
1619
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;
1625         }
1626
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;
1630         }
1631
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;
1635         }
1636
1637         ndis_set_encr_status(drv, Ndis802_11EncryptionDisabled);
1638
1639         /* Could also verify OID_802_11_ADD_KEY error reporting and
1640          * support for OID_802_11_ASSOCIATION_INFORMATION. */
1641
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;
1647         } else {
1648                 wpa_printf(MSG_DEBUG, "NDIS: no WPA support found");
1649         }
1650
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);
1654 }
1655
1656
1657 static void wpa_driver_ndis_get_capability(struct wpa_driver_ndis_data *drv)
1658 {
1659         char buf[512];
1660         int len;
1661         size_t i;
1662         NDIS_802_11_CAPABILITY *c;
1663
1664         drv->capa.flags = WPA_DRIVER_FLAGS_DRIVER_IE;
1665
1666         len = ndis_get_oid(drv, OID_802_11_CAPABILITY, buf, sizeof(buf));
1667         if (len < 0) {
1668                 wpa_driver_ndis_get_wpa_capability(drv);
1669                 return;
1670         }
1671
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");
1677                 return;
1678         }
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 "
1690                                    "overflow");
1691                         break;
1692                 }
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;
1699                         break;
1700                 case Ndis802_11AuthModeShared:
1701                         drv->capa.auth |= WPA_DRIVER_AUTH_SHARED;
1702                         break;
1703                 case Ndis802_11AuthModeWPA:
1704                         drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA;
1705                         break;
1706                 case Ndis802_11AuthModeWPAPSK:
1707                         drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1708                         break;
1709                 case Ndis802_11AuthModeWPA2:
1710                         drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2;
1711                         break;
1712                 case Ndis802_11AuthModeWPA2PSK:
1713                         drv->capa.key_mgmt |=
1714                                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1715                         break;
1716                 case Ndis802_11AuthModeWPANone:
1717                         drv->capa.key_mgmt |=
1718                                 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE;
1719                         break;
1720                 default:
1721                         break;
1722                 }
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;
1727                         break;
1728                 case Ndis802_11Encryption2Enabled:
1729                         drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1730                         break;
1731                 case Ndis802_11Encryption3Enabled:
1732                         drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1733                         break;
1734                 default:
1735                         break;
1736                 }
1737         }
1738
1739         wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x "
1740                    "enc 0x%x auth 0x%x",
1741                    drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth);
1742 }
1743
1744
1745 static int wpa_driver_ndis_get_capa(void *priv, struct wpa_driver_capa *capa)
1746 {
1747         struct wpa_driver_ndis_data *drv = priv;
1748         if (!drv->has_capability)
1749                 return -1;
1750         os_memcpy(capa, &drv->capa, sizeof(*capa));
1751         return 0;
1752 }
1753
1754
1755 static const char * wpa_driver_ndis_get_ifname(void *priv)
1756 {
1757         struct wpa_driver_ndis_data *drv = priv;
1758         return drv->ifname;
1759 }
1760
1761
1762 static const u8 * wpa_driver_ndis_get_mac_addr(void *priv)
1763 {
1764         struct wpa_driver_ndis_data *drv = priv;
1765         return drv->own_addr;
1766 }
1767
1768
1769 #ifdef _WIN32_WCE
1770
1771 #define NDISUIO_MSG_SIZE (sizeof(NDISUIO_DEVICE_NOTIFICATION) + 512)
1772
1773 static void ndisuio_notification_receive(void *eloop_data, void *user_ctx)
1774 {
1775         struct wpa_driver_ndis_data *drv = eloop_data;
1776         NDISUIO_DEVICE_NOTIFICATION *hdr;
1777         u8 buf[NDISUIO_MSG_SIZE];
1778         DWORD len, flags;
1779
1780         if (!ReadMsgQueue(drv->event_queue, buf, NDISUIO_MSG_SIZE, &len, 0,
1781                           &flags)) {
1782                 wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1783                            "ReadMsgQueue failed: %d", (int) GetLastError());
1784                 return;
1785         }
1786
1787         if (len < sizeof(NDISUIO_DEVICE_NOTIFICATION)) {
1788                 wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: "
1789                            "Too short message (len=%d)", (int) len);
1790                 return;
1791         }
1792
1793         hdr = (NDISUIO_DEVICE_NOTIFICATION *) buf;
1794         wpa_printf(MSG_DEBUG, "NDIS: Notification received: len=%d type=0x%x",
1795                    (int) len, hdr->dwNotificationType);
1796
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);
1802                 break;
1803 #endif
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);
1808                 break;
1809 #endif
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);
1814                 break;
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);
1819                 break;
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);
1825 #else
1826                 wpa_driver_ndis_event_media_specific(
1827                         drv, ((const u8 *) hdr) + hdr->uiOffsetToStatusBuffer,
1828                         (size_t) hdr->uiStatusBufferSize);
1829 #endif
1830                 break;
1831         default:
1832                 wpa_printf(MSG_DEBUG, "NDIS: Unknown notification type 0x%x",
1833                            hdr->dwNotificationType);
1834                 break;
1835         }
1836 }
1837
1838
1839 static void ndisuio_notification_deinit(struct wpa_driver_ndis_data *drv)
1840 {
1841         NDISUIO_REQUEST_NOTIFICATION req;
1842
1843         memset(&req, 0, sizeof(req));
1844         req.hMsgQueue = drv->event_queue;
1845         req.dwNotificationTypes = 0;
1846
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());
1852         }
1853
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());
1859         }
1860
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;
1866         }
1867
1868         if (drv->connected_event) {
1869                 CloseHandle(drv->connected_event);
1870                 drv->connected_event = NULL;
1871         }
1872 }
1873
1874
1875 static int ndisuio_notification_init(struct wpa_driver_ndis_data *drv)
1876 {
1877         MSGQUEUEOPTIONS opt;
1878         NDISUIO_REQUEST_NOTIFICATION req;
1879
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());
1886                 return -1;
1887         }
1888
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;
1894
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);
1901                 return -1;
1902         }
1903
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 |
1909 #endif
1910 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL
1911                 NDISUIO_NOTIFICATION_ADAPTER_REMOVAL |
1912 #endif
1913                 NDISUIO_NOTIFICATION_MEDIA_CONNECT |
1914                 NDISUIO_NOTIFICATION_MEDIA_DISCONNECT |
1915                 NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION;
1916
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);
1923                 return -1;
1924         }
1925
1926         eloop_register_event(drv->event_queue, sizeof(drv->event_queue),
1927                              ndisuio_notification_receive, drv, NULL);
1928
1929         return 0;
1930 }
1931 #endif /* _WIN32_WCE */
1932
1933
1934 static int wpa_driver_ndis_get_names(struct wpa_driver_ndis_data *drv)
1935 {
1936 #ifdef CONFIG_USE_NDISUIO
1937         NDISUIO_QUERY_BINDING *b;
1938         size_t blen = sizeof(*b) + 1024;
1939         int i, error, found = 0;
1940         DWORD written;
1941         char name[256], desc[256], *dpos;
1942         WCHAR *pos;
1943         size_t j, len, dlen;
1944
1945         b = os_malloc(blen);
1946         if (b == NULL)
1947                 return -1;
1948
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,
1954                                      &written, NULL)) {
1955                         error = (int) GetLastError();
1956                         if (error == ERROR_NO_MORE_ITEMS)
1957                                 break;
1958                         wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
1959                                    "failed: %d", error);
1960                         break;
1961                 }
1962
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];
1969                 name[len] = '\0';
1970
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];
1977                 desc[len] = '\0';
1978
1979                 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
1980
1981                 if (os_strstr(name, drv->ifname)) {
1982                         wpa_printf(MSG_DEBUG, "NDIS: Interface name match");
1983                         found = 1;
1984                         break;
1985                 }
1986
1987                 if (os_strncmp(desc, drv->ifname, os_strlen(drv->ifname)) == 0)
1988                 {
1989                         wpa_printf(MSG_DEBUG, "NDIS: Interface description "
1990                                    "match");
1991                         found = 1;
1992                         break;
1993                 }
1994         }
1995
1996         if (!found) {
1997                 wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
1998                            drv->ifname);
1999                 os_free(b);
2000                 return -1;
2001         }
2002
2003         os_strlcpy(drv->ifname,
2004                    os_strncmp(name, "\\DEVICE\\", 8) == 0 ? name + 8 : name,
2005                    sizeof(drv->ifname));
2006 #ifdef _WIN32_WCE
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 "
2010                            "adapter name");
2011                 os_free(b);
2012                 return -1;
2013         }
2014 #endif /* _WIN32_WCE */
2015
2016         dpos = os_strstr(desc, " - ");
2017         if (dpos)
2018                 dlen = dpos - desc;
2019         else
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';
2025         }
2026
2027         os_free(b);
2028
2029         if (drv->adapter_desc == NULL)
2030                 return -1;
2031
2032         wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2033                    drv->adapter_desc);
2034
2035         return 0;
2036 #else /* CONFIG_USE_NDISUIO */
2037         PTSTR _names;
2038         char *names, *pos, *pos2;
2039         ULONG len;
2040         BOOLEAN res;
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;
2045         size_t dlen;
2046
2047         wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
2048                    PacketGetVersion());
2049
2050         len = 8192;
2051         _names = os_zalloc(len);
2052         if (_names == NULL)
2053                 return -1;
2054
2055         res = PacketGetAdapterNames(_names, &len);
2056         if (!res && len > 8192) {
2057                 os_free(_names);
2058                 _names = os_zalloc(len);
2059                 if (_names == NULL)
2060                         return -1;
2061                 res = PacketGetAdapterNames(_names, &len);
2062         }
2063
2064         if (!res) {
2065                 wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
2066                            "(PacketGetAdapterNames)");
2067                 os_free(_names);
2068                 return -1;
2069         }
2070
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 "
2074                            "UNICODE");
2075                 /* Convert to ASCII */
2076                 pos2 = pos = names;
2077                 while (pos2 < names + len) {
2078                         if (pos2[0] == '\0' && pos2[1] == '\0' &&
2079                             pos2[2] == '\0' && pos2[3] == '\0') {
2080                                 pos2 += 4;
2081                                 break;
2082                         }
2083                         *pos++ = pos2[0];
2084                         pos2 += 2;
2085                 }
2086                 os_memcpy(pos + 2, names, pos - names);
2087                 pos += 2;
2088         } else
2089                 pos = names;
2090
2091         num_name = 0;
2092         while (pos < names + len) {
2093                 name[num_name] = pos;
2094                 while (*pos && pos < names + len)
2095                         pos++;
2096                 if (pos + 1 >= names + len) {
2097                         os_free(names);
2098                         return -1;
2099                 }
2100                 pos++;
2101                 num_name++;
2102                 if (num_name >= MAX_ADAPTERS) {
2103                         wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
2104                         os_free(names);
2105                         return -1;
2106                 }
2107                 if (*pos == '\0') {
2108                         wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
2109                                    num_name);
2110                         pos++;
2111                         break;
2112                 }
2113         }
2114
2115         num_desc = 0;
2116         while (pos < names + len) {
2117                 desc[num_desc] = pos;
2118                 while (*pos && pos < names + len)
2119                         pos++;
2120                 if (pos + 1 >= names + len) {
2121                         os_free(names);
2122                         return -1;
2123                 }
2124                 pos++;
2125                 num_desc++;
2126                 if (num_desc >= MAX_ADAPTERS) {
2127                         wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
2128                                    "descriptions");
2129                         os_free(names);
2130                         return -1;
2131                 }
2132                 if (*pos == '\0') {
2133                         wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
2134                                    "found", num_name);
2135                         pos++;
2136                         break;
2137                 }
2138         }
2139
2140         /*
2141          * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
2142          * descriptions. Fill in dummy descriptors to work around this.
2143          */
2144         while (num_desc < num_name)
2145                 desc[num_desc++] = "dummy description";
2146
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);
2151                 os_free(names);
2152                 return -1;
2153         }
2154
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))
2160                         found_name = i;
2161                 if (found_desc == -1 &&
2162                     os_strncmp(desc[i], drv->ifname, os_strlen(drv->ifname)) ==
2163                     0)
2164                         found_desc = i;
2165         }
2166
2167         if (found_name < 0 && found_desc >= 0) {
2168                 wpa_printf(MSG_DEBUG, "NDIS: Matched interface '%s' based on "
2169                            "description '%s'",
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));
2176         }
2177
2178         if (found_name < 0) {
2179                 wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'",
2180                            drv->ifname);
2181                 os_free(names);
2182                 return -1;
2183         }
2184
2185         i = found_name;
2186         pos = os_strrchr(desc[i], '(');
2187         if (pos) {
2188                 dlen = pos - desc[i];
2189                 pos--;
2190                 if (pos > desc[i] && *pos == ' ')
2191                         dlen--;
2192         } else {
2193                 dlen = os_strlen(desc[i]);
2194         }
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';
2199         }
2200
2201         os_free(names);
2202
2203         if (drv->adapter_desc == NULL)
2204                 return -1;
2205
2206         wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'",
2207                    drv->adapter_desc);
2208
2209         return 0;
2210 #endif /* CONFIG_USE_NDISUIO */
2211 }
2212
2213
2214 #if defined(CONFIG_NATIVE_WINDOWS) || defined(__CYGWIN__)
2215 #ifndef _WIN32_WCE
2216 /*
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.
2221  */
2222
2223 typedef struct {
2224         LPWSTR wszGuid;
2225 } INTF_KEY_ENTRY, *PINTF_KEY_ENTRY;
2226
2227 typedef struct {
2228         DWORD dwNumIntfs;
2229         PINTF_KEY_ENTRY pIntfs;
2230 } INTFS_KEY_TABLE, *PINTFS_KEY_TABLE;
2231
2232 typedef struct {
2233         DWORD dwDataLen;
2234         LPBYTE pData;
2235 } RAW_DATA, *PRAW_DATA;
2236
2237 typedef struct {
2238         LPWSTR wszGuid;
2239         LPWSTR wszDescr;
2240         ULONG ulMediaState;
2241         ULONG ulMediaType;
2242         ULONG ulPhysicalMediaType;
2243         INT nInfraMode;
2244         INT nAuthMode;
2245         INT nWepStatus;
2246 #ifndef _WIN32_WCE
2247         u8 pad[2]; /* why is this needed? */
2248 #endif /* _WIN32_WCE */
2249         DWORD dwCtlFlags;
2250         DWORD dwCapabilities; /* something added for WinXP SP2(?) */
2251         RAW_DATA rdSSID;
2252         RAW_DATA rdBSSID;
2253         RAW_DATA rdBSSIDList;
2254         RAW_DATA rdStSSIDList;
2255         RAW_DATA rdCtrlData;
2256 #ifdef UNDER_CE
2257         BOOL bInitialized;
2258 #endif
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;
2264
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 */
2270
2271
2272 #ifdef _WIN32_WCE
2273 static int wpa_driver_ndis_rebind_adapter(struct wpa_driver_ndis_data *drv)
2274 {
2275         HANDLE ndis;
2276         TCHAR multi[100];
2277         int len;
2278
2279         len = _tcslen(drv->adapter_name);
2280         if (len > 80)
2281                 return -1;
2282
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());
2288                 return -1;
2289         }
2290
2291         len++;
2292         memcpy(multi, drv->adapter_name, len * sizeof(TCHAR));
2293         memcpy(&multi[len], TEXT("NDISUIO\0"), 9 * sizeof(TCHAR));
2294         len += 9;
2295
2296         if (!DeviceIoControl(ndis, IOCTL_NDIS_REBIND_ADAPTER,
2297                              multi, len * sizeof(TCHAR), NULL, 0, NULL, NULL))
2298         {
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));
2303                 CloseHandle(ndis);
2304                 return -1;
2305         }
2306
2307         CloseHandle(ndis);
2308
2309         wpa_printf(MSG_DEBUG, "NDIS: Requested NDIS rebind of NDISUIO "
2310                    "protocol");
2311
2312         return 0;
2313 }
2314 #endif /* _WIN32_WCE */
2315
2316
2317 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2318                                    int enable)
2319 {
2320 #ifdef _WIN32_WCE
2321         HKEY hk, hk2;
2322         LONG ret;
2323         DWORD i, hnd, len;
2324         TCHAR keyname[256], devname[256];
2325
2326 #define WZC_DRIVER TEXT("Drivers\\BuiltIn\\ZeroConfig")
2327
2328         if (enable) {
2329                 HANDLE h;
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());
2335                         return -1;
2336                 }
2337
2338                 wpa_printf(MSG_DEBUG, "NDIS: WZC re-enabled");
2339                 return wpa_driver_ndis_rebind_adapter(drv);
2340         }
2341
2342         /*
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.
2348          */
2349
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());
2355                 return -1;
2356         }
2357
2358         for (i = 0; ; i++) {
2359                 len = sizeof(keyname);
2360                 ret = RegEnumKeyEx(hk, i, keyname, &len, NULL, NULL, NULL,
2361                                    NULL);
2362                 if (ret != ERROR_SUCCESS) {
2363                         wpa_printf(MSG_DEBUG, "NDIS: Could not find active "
2364                                    "WZC - assuming it is not running.");
2365                         RegCloseKey(hk);
2366                         return -1;
2367                 }
2368
2369                 ret = RegOpenKeyEx(hk, keyname, 0, 0, &hk2);
2370                 if (ret != ERROR_SUCCESS) {
2371                         wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(active dev) "
2372                                    "failed: %d %d",
2373                                    (int) ret, (int) GetLastError());
2374                         continue;
2375                 }
2376
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());
2384                         RegCloseKey(hk2);
2385                         continue;
2386                 }
2387
2388                 if (_tcscmp(devname, WZC_DRIVER) == 0)
2389                         break;
2390
2391                 RegCloseKey(hk2);
2392         }
2393
2394         RegCloseKey(hk);
2395
2396         /* Found WZC - get handle to it. */
2397         len = sizeof(hnd);
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());
2403                 RegCloseKey(hk2);
2404                 return -1;
2405         }
2406
2407         RegCloseKey(hk2);
2408
2409         /* Deactivate WZC */
2410         if (!DeactivateDevice((HANDLE) hnd)) {
2411                 wpa_printf(MSG_DEBUG, "NDIS: DeactivateDevice failed: %d",
2412                            (int) GetLastError());
2413                 return -1;
2414         }
2415
2416         wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily");
2417         drv->wzc_disabled = 1;
2418         return wpa_driver_ndis_rebind_adapter(drv);
2419
2420 #else /* _WIN32_WCE */
2421
2422         HMODULE hm;
2423         DWORD (WINAPI *wzc_enum_interf)(LPWSTR pSrvAddr,
2424                                         PINTFS_KEY_TABLE pIntfs);
2425         DWORD (WINAPI *wzc_query_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2426                                          PINTF_ENTRY pIntf,
2427                                          LPDWORD pdwOutFlags);
2428         DWORD (WINAPI *wzc_set_interf)(LPWSTR pSrvAddr, DWORD dwInFlags,
2429                                        PINTF_ENTRY pIntf, LPDWORD pdwOutFlags);
2430         int ret = -1, j;
2431         DWORD res;
2432         INTFS_KEY_TABLE guids;
2433         INTF_ENTRY intf;
2434         char guid[128];
2435         WCHAR *pos;
2436         DWORD flags, i;
2437
2438         hm = LoadLibrary(TEXT("wzcsapi.dll"));
2439         if (hm == NULL) {
2440                 wpa_printf(MSG_DEBUG, "NDIS: Failed to load wzcsapi.dll (%u) "
2441                            "- WZC probably not running",
2442                            (unsigned int) GetLastError());
2443                 return -1;
2444         }
2445
2446 #ifdef _WIN32_WCE
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 */
2455
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 "
2460                            "in wzcsapi.dll");
2461                 goto fail;
2462         }
2463
2464         os_memset(&guids, 0, sizeof(guids));
2465         res = wzc_enum_interf(NULL, &guids);
2466         if (res != 0) {
2467                 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces failed: %d; "
2468                            "WZC service is apparently not running",
2469                            (int) res);
2470                 goto fail;
2471         }
2472
2473         wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces: %d interfaces",
2474                    (int) guids.dwNumIntfs);
2475
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;
2480                         if (*pos == 0)
2481                                 break;
2482                         pos++;
2483                 }
2484                 guid[sizeof(guid) - 1] = '\0';
2485                 wpa_printf(MSG_DEBUG, "NDIS: intfs %d GUID '%s'",
2486                            (int) i, guid);
2487                 if (os_strstr(drv->ifname, guid) == NULL)
2488                         continue;
2489
2490                 wpa_printf(MSG_DEBUG, "NDIS: Current interface found from "
2491                            "WZC");
2492                 break;
2493         }
2494
2495         if (i >= guids.dwNumIntfs) {
2496                 wpa_printf(MSG_DEBUG, "NDIS: Current interface not found from "
2497                            "WZC");
2498                 goto fail;
2499         }
2500
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;
2505         flags = 0;
2506         res = wzc_query_interf(NULL, INTFCTL_ENABLED, &intf, &flags);
2507         if (res != 0) {
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());
2513                 goto fail;
2514         }
2515
2516         wpa_printf(MSG_DEBUG, "NDIS: WZC interface flags 0x%x dwCtlFlags 0x%x",
2517                    (int) flags, (int) intf.dwCtlFlags);
2518
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));
2524                 goto fail;
2525         }
2526
2527         if (enable) {
2528                 if (!(intf.dwCtlFlags & INTFCTL_ENABLED)) {
2529                         wpa_printf(MSG_DEBUG, "NDIS: Enabling WZC for this "
2530                                    "interface");
2531                         intf.dwCtlFlags |= INTFCTL_ENABLED;
2532                         res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2533                                              &flags);
2534                         if (res != 0) {
2535                                 wpa_printf(MSG_DEBUG, "NDIS: Failed to enable "
2536                                            "WZC: %d (0x%x)",
2537                                            (int) res, (int) res);
2538                                 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u",
2539                                            (unsigned int) GetLastError());
2540                                 goto fail;
2541                         }
2542                         wpa_printf(MSG_DEBUG, "NDIS: Re-enabled WZC for this "
2543                                    "interface");
2544                         drv->wzc_disabled = 0;
2545                 }
2546         } else {
2547                 if (intf.dwCtlFlags & INTFCTL_ENABLED) {
2548                         wpa_printf(MSG_DEBUG, "NDIS: Disabling WZC for this "
2549                                    "interface");
2550                         intf.dwCtlFlags &= ~INTFCTL_ENABLED;
2551                         res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf,
2552                                              &flags);
2553                         if (res != 0) {
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());
2559                                 goto fail;
2560                         }
2561                         wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily "
2562                                    "for this interface");
2563                         drv->wzc_disabled = 1;
2564                 } else {
2565                         wpa_printf(MSG_DEBUG, "NDIS: WZC was not enabled for "
2566                                    "this interface");
2567                 }
2568         }
2569
2570         ret = 0;
2571
2572 fail:
2573         FreeLibrary(hm);
2574
2575         return ret;
2576 #endif /* _WIN32_WCE */
2577 }
2578
2579 #else /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2580
2581 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv,
2582                                    int enable)
2583 {
2584         return 0;
2585 }
2586
2587 #endif /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */
2588
2589
2590 #ifdef CONFIG_USE_NDISUIO
2591 /*
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.
2595  */
2596 static HANDLE driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE;
2597
2598 HANDLE driver_ndis_get_ndisuio_handle(void)
2599 {
2600         return driver_ndis_ndisuio_handle;
2601 }
2602 #endif /* CONFIG_USE_NDISUIO */
2603
2604
2605 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv)
2606 {
2607 #ifdef CONFIG_USE_NDISUIO
2608 #ifndef _WIN32_WCE
2609 #define NDISUIO_DEVICE_NAME TEXT("\\\\.\\\\Ndisuio")
2610         DWORD written;
2611 #endif /* _WIN32_WCE */
2612         drv->ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2613                                   GENERIC_READ | GENERIC_WRITE, 0, NULL,
2614                                   OPEN_EXISTING,
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());
2620                 return -1;
2621         }
2622         driver_ndis_ndisuio_handle = drv->ndisuio;
2623
2624 #ifndef _WIN32_WCE
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;
2631                 return -1;
2632         }
2633 #endif /* _WIN32_WCE */
2634
2635         return 0;
2636 #else /* CONFIG_USE_NDISUIO */
2637         return 0;
2638 #endif /* CONFIG_USE_NDISUIO */
2639 }
2640
2641
2642 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv)
2643 {
2644 #ifdef CONFIG_USE_NDISUIO
2645         DWORD written;
2646 #define MAX_NDIS_DEVICE_NAME_LEN 256
2647         WCHAR ifname[MAX_NDIS_DEVICE_NAME_LEN];
2648         size_t len, i, pos;
2649         const char *prefix = "\\DEVICE\\";
2650
2651 #ifdef _WIN32_WCE
2652         pos = 0;
2653 #else /* _WIN32_WCE */
2654         pos = 8;
2655 #endif /* _WIN32_WCE */
2656         len = pos + os_strlen(drv->ifname);
2657         if (len >= MAX_NDIS_DEVICE_NAME_LEN)
2658                 return -1;
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];
2663         ifname[i] = L'\0';
2664
2665         if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_OPEN_DEVICE,
2666                              ifname, len * sizeof(WCHAR), NULL, 0, &written,
2667                              NULL)) {
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;
2674                 return -1;
2675         }
2676
2677         wpa_printf(MSG_DEBUG, "NDIS: Opened NDISUIO device successfully");
2678
2679         return 0;
2680 #else /* CONFIG_USE_NDISUIO */
2681         char ifname[128];
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 "
2686                            "'%s'", ifname);
2687                 return -1;
2688         }
2689         return 0;
2690 #endif /* CONFIG_USE_NDISUIO */
2691 }
2692
2693
2694 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv)
2695 {
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 */
2701         if (drv->adapter)
2702                 PacketCloseAdapter(drv->adapter);
2703 #endif /* CONFIG_USE_NDISUIO */
2704 }
2705
2706
2707 static void * wpa_driver_ndis_init(void *ctx, const char *ifname)
2708 {
2709         struct wpa_driver_ndis_data *drv;
2710         u32 mode;
2711
2712         drv = os_zalloc(sizeof(*drv));
2713         if (drv == NULL)
2714                 return NULL;
2715         drv->ctx = ctx;
2716         /*
2717          * Compatibility code to strip possible prefix from the GUID. Previous
2718          * versions include \Device\NPF_ prefix for all names, but the internal
2719          * interface name is now only the GUI. Both Packet32 and NDISUIO
2720          * prefixes are supported.
2721          */
2722         if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0)
2723                 ifname += 12;
2724         else if (os_strncmp(ifname, "\\DEVICE\\", 8) == 0)
2725                 ifname += 8;
2726         os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
2727
2728         if (wpa_driver_ndis_adapter_init(drv) < 0) {
2729                 os_free(drv);
2730                 return NULL;
2731         }
2732
2733         if (wpa_driver_ndis_get_names(drv) < 0) {
2734                 wpa_driver_ndis_adapter_close(drv);
2735                 os_free(drv);
2736                 return NULL;
2737         }
2738
2739         wpa_driver_ndis_set_wzc(drv, 0);
2740
2741         if (wpa_driver_ndis_adapter_open(drv) < 0) {
2742                 wpa_driver_ndis_adapter_close(drv);
2743                 os_free(drv);
2744                 return NULL;
2745         }
2746
2747         if (ndis_get_oid(drv, OID_802_3_CURRENT_ADDRESS,
2748                          (char *) drv->own_addr, ETH_ALEN) < 0) {
2749                 wpa_printf(MSG_DEBUG, "NDIS: Get OID_802_3_CURRENT_ADDRESS "
2750                            "failed");
2751                 wpa_driver_ndis_adapter_close(drv);
2752                 os_free(drv);
2753                 return NULL;
2754         }
2755         wpa_driver_ndis_get_capability(drv);
2756
2757         /* Make sure that the driver does not have any obsolete PMKID entries.
2758          */
2759         wpa_driver_ndis_flush_pmkid(drv);
2760
2761         /*
2762          * Disconnect to make sure that driver re-associates if it was
2763          * connected.
2764          */
2765         wpa_driver_ndis_disconnect(drv);
2766
2767         eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout, drv, NULL);
2768
2769 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2770         drv->events = ndis_events_init(&drv->events_pipe, &drv->event_avail,
2771                                        drv->ifname, drv->adapter_desc);
2772         if (drv->events == NULL) {
2773                 wpa_driver_ndis_deinit(drv);
2774                 return NULL;
2775         }
2776         eloop_register_event(drv->event_avail, sizeof(drv->event_avail),
2777                              wpa_driver_ndis_event_pipe_cb, drv, NULL);
2778 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2779
2780 #ifdef _WIN32_WCE
2781         if (ndisuio_notification_init(drv) < 0) {
2782                 wpa_driver_ndis_deinit(drv);
2783                 return NULL;
2784         }
2785 #endif /* _WIN32_WCE */
2786
2787         /* Set mode here in case card was configured for ad-hoc mode
2788          * previously. */
2789         mode = Ndis802_11Infrastructure;
2790         if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE,
2791                          (char *) &mode, sizeof(mode)) < 0) {
2792                 wpa_printf(MSG_DEBUG, "NDIS: Failed to set "
2793                            "OID_802_11_INFRASTRUCTURE_MODE (%d)",
2794                            (int) mode);
2795                 /* Try to continue anyway */
2796
2797                 if (!drv->has_capability && drv->capa.enc == 0) {
2798                         wpa_printf(MSG_DEBUG, "NDIS: Driver did not provide "
2799                                    "any wireless capabilities - assume it is "
2800                                    "a wired interface");
2801                         drv->wired = 1;
2802                 }
2803         }
2804
2805         return drv;
2806 }
2807
2808
2809 static void wpa_driver_ndis_deinit(void *priv)
2810 {
2811         struct wpa_driver_ndis_data *drv = priv;
2812
2813 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED
2814         if (drv->events) {
2815                 eloop_unregister_event(drv->event_avail,
2816                                        sizeof(drv->event_avail));
2817                 ndis_events_deinit(drv->events);
2818         }
2819 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */
2820
2821 #ifdef _WIN32_WCE
2822         ndisuio_notification_deinit(drv);
2823 #endif /* _WIN32_WCE */
2824
2825         eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx);
2826         eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL);
2827         wpa_driver_ndis_flush_pmkid(drv);
2828         wpa_driver_ndis_disconnect(drv);
2829         if (wpa_driver_ndis_radio_off(drv) < 0) {
2830                 wpa_printf(MSG_DEBUG, "NDIS: failed to disassociate and turn "
2831                            "radio off");
2832         }
2833
2834         wpa_driver_ndis_adapter_close(drv);
2835
2836         if (drv->wzc_disabled)
2837                 wpa_driver_ndis_set_wzc(drv, 1);
2838
2839 #ifdef _WIN32_WCE
2840         os_free(drv->adapter_name);
2841 #endif /* _WIN32_WCE */
2842         os_free(drv->adapter_desc);
2843         os_free(drv);
2844 }
2845
2846
2847 static struct wpa_interface_info *
2848 wpa_driver_ndis_get_interfaces(void *global_priv)
2849 {
2850         struct wpa_interface_info *iface = NULL, *niface;
2851
2852 #ifdef CONFIG_USE_NDISUIO
2853         NDISUIO_QUERY_BINDING *b;
2854         size_t blen = sizeof(*b) + 1024;
2855         int i, error;
2856         DWORD written;
2857         char name[256], desc[256];
2858         WCHAR *pos;
2859         size_t j, len;
2860         HANDLE ndisuio;
2861
2862         ndisuio = CreateFile(NDISUIO_DEVICE_NAME,
2863                              GENERIC_READ | GENERIC_WRITE, 0, NULL,
2864                              OPEN_EXISTING,
2865                              FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
2866                              INVALID_HANDLE_VALUE);
2867         if (ndisuio == INVALID_HANDLE_VALUE) {
2868                 wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to "
2869                            "NDISUIO: %d", (int) GetLastError());
2870                 return NULL;
2871         }
2872
2873 #ifndef _WIN32_WCE
2874         if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0,
2875                              NULL, 0, &written, NULL)) {
2876                 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: "
2877                            "%d", (int) GetLastError());
2878                 CloseHandle(ndisuio);
2879                 return NULL;
2880         }
2881 #endif /* _WIN32_WCE */
2882
2883         b = os_malloc(blen);
2884         if (b == NULL) {
2885                 CloseHandle(ndisuio);
2886                 return NULL;
2887         }
2888
2889         for (i = 0; ; i++) {
2890                 os_memset(b, 0, blen);
2891                 b->BindingIndex = i;
2892                 if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
2893                                      b, sizeof(NDISUIO_QUERY_BINDING), b, blen,
2894                                      &written, NULL)) {
2895                         error = (int) GetLastError();
2896                         if (error == ERROR_NO_MORE_ITEMS)
2897                                 break;
2898                         wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING "
2899                                    "failed: %d", error);
2900                         break;
2901                 }
2902
2903                 pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
2904                 len = b->DeviceNameLength;
2905                 if (len >= sizeof(name))
2906                         len = sizeof(name) - 1;
2907                 for (j = 0; j < len; j++)
2908                         name[j] = (char) pos[j];
2909                 name[len] = '\0';
2910
2911                 pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
2912                 len = b->DeviceDescrLength;
2913                 if (len >= sizeof(desc))
2914                         len = sizeof(desc) - 1;
2915                 for (j = 0; j < len; j++)
2916                         desc[j] = (char) pos[j];
2917                 desc[len] = '\0';
2918
2919                 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc);
2920
2921                 niface = os_zalloc(sizeof(*niface));
2922                 if (niface == NULL)
2923                         break;
2924                 niface->drv_name = "ndis";
2925                 if (os_strncmp(name, "\\DEVICE\\", 8) == 0)
2926                         niface->ifname = os_strdup(name + 8);
2927                 else
2928                         niface->ifname = os_strdup(name);
2929                 if (niface->ifname == NULL) {
2930                         os_free(niface);
2931                         break;
2932                 }
2933                 niface->desc = os_strdup(desc);
2934                 niface->next = iface;
2935                 iface = niface;
2936         }
2937
2938         os_free(b);
2939         CloseHandle(ndisuio);
2940 #else /* CONFIG_USE_NDISUIO */
2941         PTSTR _names;
2942         char *names, *pos, *pos2;
2943         ULONG len;
2944         BOOLEAN res;
2945         char *name[MAX_ADAPTERS];
2946         char *desc[MAX_ADAPTERS];
2947         int num_name, num_desc, i;
2948
2949         wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s",
2950                    PacketGetVersion());
2951
2952         len = 8192;
2953         _names = os_zalloc(len);
2954         if (_names == NULL)
2955                 return NULL;
2956
2957         res = PacketGetAdapterNames(_names, &len);
2958         if (!res && len > 8192) {
2959                 os_free(_names);
2960                 _names = os_zalloc(len);
2961                 if (_names == NULL)
2962                         return NULL;
2963                 res = PacketGetAdapterNames(_names, &len);
2964         }
2965
2966         if (!res) {
2967                 wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list "
2968                            "(PacketGetAdapterNames)");
2969                 os_free(_names);
2970                 return NULL;
2971         }
2972
2973         names = (char *) _names;
2974         if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') {
2975                 wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in "
2976                            "UNICODE");
2977                 /* Convert to ASCII */
2978                 pos2 = pos = names;
2979                 while (pos2 < names + len) {
2980                         if (pos2[0] == '\0' && pos2[1] == '\0' &&
2981                             pos2[2] == '\0' && pos2[3] == '\0') {
2982                                 pos2 += 4;
2983                                 break;
2984                         }
2985                         *pos++ = pos2[0];
2986                         pos2 += 2;
2987                 }
2988                 os_memcpy(pos + 2, names, pos - names);
2989                 pos += 2;
2990         } else
2991                 pos = names;
2992
2993         num_name = 0;
2994         while (pos < names + len) {
2995                 name[num_name] = pos;
2996                 while (*pos && pos < names + len)
2997                         pos++;
2998                 if (pos + 1 >= names + len) {
2999                         os_free(names);
3000                         return NULL;
3001                 }
3002                 pos++;
3003                 num_name++;
3004                 if (num_name >= MAX_ADAPTERS) {
3005                         wpa_printf(MSG_DEBUG, "NDIS: Too many adapters");
3006                         os_free(names);
3007                         return NULL;
3008                 }
3009                 if (*pos == '\0') {
3010                         wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found",
3011                                    num_name);
3012                         pos++;
3013                         break;
3014                 }
3015         }
3016
3017         num_desc = 0;
3018         while (pos < names + len) {
3019                 desc[num_desc] = pos;
3020                 while (*pos && pos < names + len)
3021                         pos++;
3022                 if (pos + 1 >= names + len) {
3023                         os_free(names);
3024                         return NULL;
3025                 }
3026                 pos++;
3027                 num_desc++;
3028                 if (num_desc >= MAX_ADAPTERS) {
3029                         wpa_printf(MSG_DEBUG, "NDIS: Too many adapter "
3030                                    "descriptions");
3031                         os_free(names);
3032                         return NULL;
3033                 }
3034                 if (*pos == '\0') {
3035                         wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions "
3036                                    "found", num_name);
3037                         pos++;
3038                         break;
3039                 }
3040         }
3041
3042         /*
3043          * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter
3044          * descriptions. Fill in dummy descriptors to work around this.
3045          */
3046         while (num_desc < num_name)
3047                 desc[num_desc++] = "dummy description";
3048
3049         if (num_name != num_desc) {
3050                 wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and "
3051                            "description counts (%d != %d)",
3052                            num_name, num_desc);
3053                 os_free(names);
3054                 return NULL;
3055         }
3056
3057         for (i = 0; i < num_name; i++) {
3058                 niface = os_zalloc(sizeof(*niface));
3059                 if (niface == NULL)
3060                         break;
3061                 niface->drv_name = "ndis";
3062                 if (os_strncmp(name[i], "\\Device\\NPF_", 12) == 0)
3063                         niface->ifname = os_strdup(name[i] + 12);
3064                 else
3065                         niface->ifname = os_strdup(name[i]);
3066                 if (niface->ifname == NULL) {
3067                         os_free(niface);
3068                         break;
3069                 }
3070                 niface->desc = os_strdup(desc[i]);
3071                 niface->next = iface;
3072                 iface = niface;
3073         }
3074
3075 #endif /* CONFIG_USE_NDISUIO */
3076
3077         return iface;
3078 }
3079
3080
3081 const struct wpa_driver_ops wpa_driver_ndis_ops = {
3082         "ndis",
3083         "Windows NDIS driver",
3084         wpa_driver_ndis_get_bssid,
3085         wpa_driver_ndis_get_ssid,
3086         wpa_driver_ndis_set_wpa,
3087         wpa_driver_ndis_set_key,
3088         wpa_driver_ndis_init,
3089         wpa_driver_ndis_deinit,
3090         NULL /* set_param */,
3091         NULL /* set_countermeasures */,
3092         NULL /* set_drop_unencrypted */,
3093         wpa_driver_ndis_scan,
3094         NULL /* get_scan_results */,
3095         wpa_driver_ndis_deauthenticate,
3096         wpa_driver_ndis_disassociate,
3097         wpa_driver_ndis_associate,
3098         NULL /* set_auth_alg */,
3099         wpa_driver_ndis_add_pmkid,
3100         wpa_driver_ndis_remove_pmkid,
3101         wpa_driver_ndis_flush_pmkid,
3102         wpa_driver_ndis_get_capa,
3103         wpa_driver_ndis_poll,
3104         wpa_driver_ndis_get_ifname,
3105         wpa_driver_ndis_get_mac_addr,
3106         NULL /* send_eapol */,
3107         NULL /* set_operstate */,
3108         NULL /* mlme_setprotection */,
3109         NULL /* get_hw_feature_data */,
3110         NULL /* set_channel */,
3111         NULL /* set_ssid */,
3112         NULL /* set_bssid */,
3113         NULL /* send_mlme */,
3114         NULL /* mlme_add_sta */,
3115         NULL /* mlme_remove_sta */,
3116         NULL /* update_ft_ies */,
3117         NULL /* send_ft_action */,
3118         wpa_driver_ndis_get_scan_results,
3119         NULL /* set_probe_req_ie */,
3120         NULL /* set_mode */,
3121         NULL /* set_country */,
3122         NULL /* global_init */,
3123         NULL /* global_deinit */,
3124         NULL /* init2 */,
3125         wpa_driver_ndis_get_interfaces
3126 };