1 /* 2 * WPA Supplicant - Windows/NDIS driver interface 3 * Copyright (c) 2004-2007, Jouni Malinen <j (at) w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #ifdef __CYGWIN__ 10 /* Avoid some header file conflicts by not including standard headers for 11 * cygwin builds when Packet32.h is included. */ 12 #include "build_config.h" 13 int close(int fd); 14 #else /* __CYGWIN__ */ 15 #include "includes.h" 16 #endif /* __CYGWIN__ */ 17 #ifdef CONFIG_USE_NDISUIO 18 #include <winsock2.h> 19 #else /* CONFIG_USE_NDISUIO */ 20 #include <Packet32.h> 21 #endif /* CONFIG_USE_NDISUIO */ 22 #ifdef __MINGW32_VERSION 23 #include <ddk/ntddndis.h> 24 #else /* __MINGW32_VERSION */ 25 #include <ntddndis.h> 26 #endif /* __MINGW32_VERSION */ 27 28 #ifdef _WIN32_WCE 29 #include <winioctl.h> 30 #include <nuiouser.h> 31 #include <devload.h> 32 #endif /* _WIN32_WCE */ 33 34 #include "common.h" 35 #include "driver.h" 36 #include "eloop.h" 37 #include "common/ieee802_11_defs.h" 38 #include "common/ieee802_11_common.h" 39 #include "driver_ndis.h" 40 41 int wpa_driver_register_event_cb(struct wpa_driver_ndis_data *drv); 42 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED 43 void wpa_driver_ndis_event_pipe_cb(void *eloop_data, void *user_data); 44 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */ 45 46 static void wpa_driver_ndis_deinit(void *priv); 47 static void wpa_driver_ndis_poll(void *drv); 48 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx); 49 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv); 50 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv); 51 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv); 52 53 54 static const u8 pae_group_addr[ETH_ALEN] = 55 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 }; 56 57 58 /* FIX: to be removed once this can be compiled with the complete NDIS 59 * header files */ 60 #ifndef OID_802_11_BSSID 61 #define OID_802_11_BSSID 0x0d010101 62 #define OID_802_11_SSID 0x0d010102 63 #define OID_802_11_INFRASTRUCTURE_MODE 0x0d010108 64 #define OID_802_11_ADD_WEP 0x0D010113 65 #define OID_802_11_REMOVE_WEP 0x0D010114 66 #define OID_802_11_DISASSOCIATE 0x0D010115 67 #define OID_802_11_BSSID_LIST 0x0d010217 68 #define OID_802_11_AUTHENTICATION_MODE 0x0d010118 69 #define OID_802_11_PRIVACY_FILTER 0x0d010119 70 #define OID_802_11_BSSID_LIST_SCAN 0x0d01011A 71 #define OID_802_11_WEP_STATUS 0x0d01011B 72 #define OID_802_11_ENCRYPTION_STATUS OID_802_11_WEP_STATUS 73 #define OID_802_11_ADD_KEY 0x0d01011D 74 #define OID_802_11_REMOVE_KEY 0x0d01011E 75 #define OID_802_11_ASSOCIATION_INFORMATION 0x0d01011F 76 #define OID_802_11_TEST 0x0d010120 77 #define OID_802_11_CAPABILITY 0x0d010122 78 #define OID_802_11_PMKID 0x0d010123 79 80 #define NDIS_802_11_LENGTH_SSID 32 81 #define NDIS_802_11_LENGTH_RATES 8 82 #define NDIS_802_11_LENGTH_RATES_EX 16 83 84 typedef UCHAR NDIS_802_11_MAC_ADDRESS[6]; 85 86 typedef struct NDIS_802_11_SSID { 87 ULONG SsidLength; 88 UCHAR Ssid[NDIS_802_11_LENGTH_SSID]; 89 } NDIS_802_11_SSID; 90 91 typedef LONG NDIS_802_11_RSSI; 92 93 typedef enum NDIS_802_11_NETWORK_TYPE { 94 Ndis802_11FH, 95 Ndis802_11DS, 96 Ndis802_11OFDM5, 97 Ndis802_11OFDM24, 98 Ndis802_11NetworkTypeMax 99 } NDIS_802_11_NETWORK_TYPE; 100 101 typedef struct NDIS_802_11_CONFIGURATION_FH { 102 ULONG Length; 103 ULONG HopPattern; 104 ULONG HopSet; 105 ULONG DwellTime; 106 } NDIS_802_11_CONFIGURATION_FH; 107 108 typedef struct NDIS_802_11_CONFIGURATION { 109 ULONG Length; 110 ULONG BeaconPeriod; 111 ULONG ATIMWindow; 112 ULONG DSConfig; 113 NDIS_802_11_CONFIGURATION_FH FHConfig; 114 } NDIS_802_11_CONFIGURATION; 115 116 typedef enum NDIS_802_11_NETWORK_INFRASTRUCTURE { 117 Ndis802_11IBSS, 118 Ndis802_11Infrastructure, 119 Ndis802_11AutoUnknown, 120 Ndis802_11InfrastructureMax 121 } NDIS_802_11_NETWORK_INFRASTRUCTURE; 122 123 typedef enum NDIS_802_11_AUTHENTICATION_MODE { 124 Ndis802_11AuthModeOpen, 125 Ndis802_11AuthModeShared, 126 Ndis802_11AuthModeAutoSwitch, 127 Ndis802_11AuthModeWPA, 128 Ndis802_11AuthModeWPAPSK, 129 Ndis802_11AuthModeWPANone, 130 Ndis802_11AuthModeWPA2, 131 Ndis802_11AuthModeWPA2PSK, 132 Ndis802_11AuthModeMax 133 } NDIS_802_11_AUTHENTICATION_MODE; 134 135 typedef enum NDIS_802_11_WEP_STATUS { 136 Ndis802_11WEPEnabled, 137 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled, 138 Ndis802_11WEPDisabled, 139 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled, 140 Ndis802_11WEPKeyAbsent, 141 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent, 142 Ndis802_11WEPNotSupported, 143 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported, 144 Ndis802_11Encryption2Enabled, 145 Ndis802_11Encryption2KeyAbsent, 146 Ndis802_11Encryption3Enabled, 147 Ndis802_11Encryption3KeyAbsent 148 } NDIS_802_11_WEP_STATUS, NDIS_802_11_ENCRYPTION_STATUS; 149 150 typedef enum NDIS_802_11_PRIVACY_FILTER { 151 Ndis802_11PrivFilterAcceptAll, 152 Ndis802_11PrivFilter8021xWEP 153 } NDIS_802_11_PRIVACY_FILTER; 154 155 typedef UCHAR NDIS_802_11_RATES[NDIS_802_11_LENGTH_RATES]; 156 typedef UCHAR NDIS_802_11_RATES_EX[NDIS_802_11_LENGTH_RATES_EX]; 157 158 typedef struct NDIS_WLAN_BSSID_EX { 159 ULONG Length; 160 NDIS_802_11_MAC_ADDRESS MacAddress; /* BSSID */ 161 UCHAR Reserved[2]; 162 NDIS_802_11_SSID Ssid; 163 ULONG Privacy; 164 NDIS_802_11_RSSI Rssi; 165 NDIS_802_11_NETWORK_TYPE NetworkTypeInUse; 166 NDIS_802_11_CONFIGURATION Configuration; 167 NDIS_802_11_NETWORK_INFRASTRUCTURE InfrastructureMode; 168 NDIS_802_11_RATES_EX SupportedRates; 169 ULONG IELength; 170 UCHAR IEs[1]; 171 } NDIS_WLAN_BSSID_EX; 172 173 typedef struct NDIS_802_11_BSSID_LIST_EX { 174 ULONG NumberOfItems; 175 NDIS_WLAN_BSSID_EX Bssid[1]; 176 } NDIS_802_11_BSSID_LIST_EX; 177 178 typedef struct NDIS_802_11_FIXED_IEs { 179 UCHAR Timestamp[8]; 180 USHORT BeaconInterval; 181 USHORT Capabilities; 182 } NDIS_802_11_FIXED_IEs; 183 184 typedef struct NDIS_802_11_WEP { 185 ULONG Length; 186 ULONG KeyIndex; 187 ULONG KeyLength; 188 UCHAR KeyMaterial[1]; 189 } NDIS_802_11_WEP; 190 191 typedef ULONG NDIS_802_11_KEY_INDEX; 192 typedef ULONGLONG NDIS_802_11_KEY_RSC; 193 194 typedef struct NDIS_802_11_KEY { 195 ULONG Length; 196 ULONG KeyIndex; 197 ULONG KeyLength; 198 NDIS_802_11_MAC_ADDRESS BSSID; 199 NDIS_802_11_KEY_RSC KeyRSC; 200 UCHAR KeyMaterial[1]; 201 } NDIS_802_11_KEY; 202 203 typedef struct NDIS_802_11_REMOVE_KEY { 204 ULONG Length; 205 ULONG KeyIndex; 206 NDIS_802_11_MAC_ADDRESS BSSID; 207 } NDIS_802_11_REMOVE_KEY; 208 209 typedef struct NDIS_802_11_AI_REQFI { 210 USHORT Capabilities; 211 USHORT ListenInterval; 212 NDIS_802_11_MAC_ADDRESS CurrentAPAddress; 213 } NDIS_802_11_AI_REQFI; 214 215 typedef struct NDIS_802_11_AI_RESFI { 216 USHORT Capabilities; 217 USHORT StatusCode; 218 USHORT AssociationId; 219 } NDIS_802_11_AI_RESFI; 220 221 typedef struct NDIS_802_11_ASSOCIATION_INFORMATION { 222 ULONG Length; 223 USHORT AvailableRequestFixedIEs; 224 NDIS_802_11_AI_REQFI RequestFixedIEs; 225 ULONG RequestIELength; 226 ULONG OffsetRequestIEs; 227 USHORT AvailableResponseFixedIEs; 228 NDIS_802_11_AI_RESFI ResponseFixedIEs; 229 ULONG ResponseIELength; 230 ULONG OffsetResponseIEs; 231 } NDIS_802_11_ASSOCIATION_INFORMATION; 232 233 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION { 234 NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported; 235 NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported; 236 } NDIS_802_11_AUTHENTICATION_ENCRYPTION; 237 238 typedef struct NDIS_802_11_CAPABILITY { 239 ULONG Length; 240 ULONG Version; 241 ULONG NoOfPMKIDs; 242 ULONG NoOfAuthEncryptPairsSupported; 243 NDIS_802_11_AUTHENTICATION_ENCRYPTION 244 AuthenticationEncryptionSupported[1]; 245 } NDIS_802_11_CAPABILITY; 246 247 typedef UCHAR NDIS_802_11_PMKID_VALUE[16]; 248 249 typedef struct BSSID_INFO { 250 NDIS_802_11_MAC_ADDRESS BSSID; 251 NDIS_802_11_PMKID_VALUE PMKID; 252 } BSSID_INFO; 253 254 typedef struct NDIS_802_11_PMKID { 255 ULONG Length; 256 ULONG BSSIDInfoCount; 257 BSSID_INFO BSSIDInfo[1]; 258 } NDIS_802_11_PMKID; 259 260 typedef enum NDIS_802_11_STATUS_TYPE { 261 Ndis802_11StatusType_Authentication, 262 Ndis802_11StatusType_PMKID_CandidateList = 2, 263 Ndis802_11StatusTypeMax 264 } NDIS_802_11_STATUS_TYPE; 265 266 typedef struct NDIS_802_11_STATUS_INDICATION { 267 NDIS_802_11_STATUS_TYPE StatusType; 268 } NDIS_802_11_STATUS_INDICATION; 269 270 typedef struct PMKID_CANDIDATE { 271 NDIS_802_11_MAC_ADDRESS BSSID; 272 ULONG Flags; 273 } PMKID_CANDIDATE; 274 275 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01 276 277 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST { 278 ULONG Version; 279 ULONG NumCandidates; 280 PMKID_CANDIDATE CandidateList[1]; 281 } NDIS_802_11_PMKID_CANDIDATE_LIST; 282 283 typedef struct NDIS_802_11_AUTHENTICATION_REQUEST { 284 ULONG Length; 285 NDIS_802_11_MAC_ADDRESS Bssid; 286 ULONG Flags; 287 } NDIS_802_11_AUTHENTICATION_REQUEST; 288 289 #define NDIS_802_11_AUTH_REQUEST_REAUTH 0x01 290 #define NDIS_802_11_AUTH_REQUEST_KEYUPDATE 0x02 291 #define NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR 0x06 292 #define NDIS_802_11_AUTH_REQUEST_GROUP_ERROR 0x0E 293 294 #endif /* OID_802_11_BSSID */ 295 296 297 #ifndef OID_802_11_PMKID 298 /* Platform SDK for XP did not include WPA2, so add needed definitions */ 299 300 #define OID_802_11_CAPABILITY 0x0d010122 301 #define OID_802_11_PMKID 0x0d010123 302 303 #define Ndis802_11AuthModeWPA2 6 304 #define Ndis802_11AuthModeWPA2PSK 7 305 306 #define Ndis802_11StatusType_PMKID_CandidateList 2 307 308 typedef struct NDIS_802_11_AUTHENTICATION_ENCRYPTION { 309 NDIS_802_11_AUTHENTICATION_MODE AuthModeSupported; 310 NDIS_802_11_ENCRYPTION_STATUS EncryptStatusSupported; 311 } NDIS_802_11_AUTHENTICATION_ENCRYPTION; 312 313 typedef struct NDIS_802_11_CAPABILITY { 314 ULONG Length; 315 ULONG Version; 316 ULONG NoOfPMKIDs; 317 ULONG NoOfAuthEncryptPairsSupported; 318 NDIS_802_11_AUTHENTICATION_ENCRYPTION 319 AuthenticationEncryptionSupported[1]; 320 } NDIS_802_11_CAPABILITY; 321 322 typedef UCHAR NDIS_802_11_PMKID_VALUE[16]; 323 324 typedef struct BSSID_INFO { 325 NDIS_802_11_MAC_ADDRESS BSSID; 326 NDIS_802_11_PMKID_VALUE PMKID; 327 } BSSID_INFO; 328 329 typedef struct NDIS_802_11_PMKID { 330 ULONG Length; 331 ULONG BSSIDInfoCount; 332 BSSID_INFO BSSIDInfo[1]; 333 } NDIS_802_11_PMKID; 334 335 typedef struct PMKID_CANDIDATE { 336 NDIS_802_11_MAC_ADDRESS BSSID; 337 ULONG Flags; 338 } PMKID_CANDIDATE; 339 340 #define NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED 0x01 341 342 typedef struct NDIS_802_11_PMKID_CANDIDATE_LIST { 343 ULONG Version; 344 ULONG NumCandidates; 345 PMKID_CANDIDATE CandidateList[1]; 346 } NDIS_802_11_PMKID_CANDIDATE_LIST; 347 348 #endif /* OID_802_11_CAPABILITY */ 349 350 351 #ifndef OID_DOT11_CURRENT_OPERATION_MODE 352 /* Native 802.11 OIDs */ 353 #define OID_DOT11_NDIS_START 0x0D010300 354 #define OID_DOT11_CURRENT_OPERATION_MODE (OID_DOT11_NDIS_START + 8) 355 #define OID_DOT11_SCAN_REQUEST (OID_DOT11_NDIS_START + 11) 356 357 typedef enum _DOT11_BSS_TYPE { 358 dot11_BSS_type_infrastructure = 1, 359 dot11_BSS_type_independent = 2, 360 dot11_BSS_type_any = 3 361 } DOT11_BSS_TYPE, * PDOT11_BSS_TYPE; 362 363 typedef UCHAR DOT11_MAC_ADDRESS[6]; 364 typedef DOT11_MAC_ADDRESS * PDOT11_MAC_ADDRESS; 365 366 typedef enum _DOT11_SCAN_TYPE { 367 dot11_scan_type_active = 1, 368 dot11_scan_type_passive = 2, 369 dot11_scan_type_auto = 3, 370 dot11_scan_type_forced = 0x80000000 371 } DOT11_SCAN_TYPE, * PDOT11_SCAN_TYPE; 372 373 typedef struct _DOT11_SCAN_REQUEST_V2 { 374 DOT11_BSS_TYPE dot11BSSType; 375 DOT11_MAC_ADDRESS dot11BSSID; 376 DOT11_SCAN_TYPE dot11ScanType; 377 BOOLEAN bRestrictedScan; 378 ULONG udot11SSIDsOffset; 379 ULONG uNumOfdot11SSIDs; 380 BOOLEAN bUseRequestIE; 381 ULONG uRequestIDsOffset; 382 ULONG uNumOfRequestIDs; 383 ULONG uPhyTypeInfosOffset; 384 ULONG uNumOfPhyTypeInfos; 385 ULONG uIEsOffset; 386 ULONG uIEsLength; 387 UCHAR ucBuffer[1]; 388 } DOT11_SCAN_REQUEST_V2, * PDOT11_SCAN_REQUEST_V2; 389 390 #endif /* OID_DOT11_CURRENT_OPERATION_MODE */ 391 392 #ifdef CONFIG_USE_NDISUIO 393 #ifndef _WIN32_WCE 394 #ifdef __MINGW32_VERSION 395 typedef ULONG NDIS_OID; 396 #endif /* __MINGW32_VERSION */ 397 /* from nuiouser.h */ 398 #define FSCTL_NDISUIO_BASE FILE_DEVICE_NETWORK 399 400 #define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \ 401 CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access) 402 403 #define IOCTL_NDISUIO_OPEN_DEVICE \ 404 _NDISUIO_CTL_CODE(0x200, METHOD_BUFFERED, \ 405 FILE_READ_ACCESS | FILE_WRITE_ACCESS) 406 407 #define IOCTL_NDISUIO_QUERY_OID_VALUE \ 408 _NDISUIO_CTL_CODE(0x201, METHOD_BUFFERED, \ 409 FILE_READ_ACCESS | FILE_WRITE_ACCESS) 410 411 #define IOCTL_NDISUIO_SET_OID_VALUE \ 412 _NDISUIO_CTL_CODE(0x205, METHOD_BUFFERED, \ 413 FILE_READ_ACCESS | FILE_WRITE_ACCESS) 414 415 #define IOCTL_NDISUIO_SET_ETHER_TYPE \ 416 _NDISUIO_CTL_CODE(0x202, METHOD_BUFFERED, \ 417 FILE_READ_ACCESS | FILE_WRITE_ACCESS) 418 419 #define IOCTL_NDISUIO_QUERY_BINDING \ 420 _NDISUIO_CTL_CODE(0x203, METHOD_BUFFERED, \ 421 FILE_READ_ACCESS | FILE_WRITE_ACCESS) 422 423 #define IOCTL_NDISUIO_BIND_WAIT \ 424 _NDISUIO_CTL_CODE(0x204, METHOD_BUFFERED, \ 425 FILE_READ_ACCESS | FILE_WRITE_ACCESS) 426 427 typedef struct _NDISUIO_QUERY_OID 428 { 429 NDIS_OID Oid; 430 UCHAR Data[sizeof(ULONG)]; 431 } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID; 432 433 typedef struct _NDISUIO_SET_OID 434 { 435 NDIS_OID Oid; 436 UCHAR Data[sizeof(ULONG)]; 437 } NDISUIO_SET_OID, *PNDISUIO_SET_OID; 438 439 typedef struct _NDISUIO_QUERY_BINDING 440 { 441 ULONG BindingIndex; 442 ULONG DeviceNameOffset; 443 ULONG DeviceNameLength; 444 ULONG DeviceDescrOffset; 445 ULONG DeviceDescrLength; 446 } NDISUIO_QUERY_BINDING, *PNDISUIO_QUERY_BINDING; 447 #endif /* _WIN32_WCE */ 448 #endif /* CONFIG_USE_NDISUIO */ 449 450 451 static int ndis_get_oid(struct wpa_driver_ndis_data *drv, unsigned int oid, 452 char *data, size_t len) 453 { 454 #ifdef CONFIG_USE_NDISUIO 455 NDISUIO_QUERY_OID *o; 456 size_t buflen = sizeof(*o) + len; 457 DWORD written; 458 int ret; 459 size_t hdrlen; 460 461 o = os_zalloc(buflen); 462 if (o == NULL) 463 return -1; 464 o->Oid = oid; 465 #ifdef _WIN32_WCE 466 o->ptcDeviceName = drv->adapter_name; 467 #endif /* _WIN32_WCE */ 468 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_OID_VALUE, 469 o, sizeof(NDISUIO_QUERY_OID), o, buflen, &written, 470 NULL)) { 471 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_QUERY_OID_VALUE " 472 "failed (oid=%08x): %d", oid, (int) GetLastError()); 473 os_free(o); 474 return -1; 475 } 476 hdrlen = sizeof(NDISUIO_QUERY_OID) - sizeof(o->Data); 477 if (written < hdrlen) { 478 wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d); " 479 "too short", oid, (unsigned int) written); 480 os_free(o); 481 return -1; 482 } 483 written -= hdrlen; 484 if (written > len) { 485 wpa_printf(MSG_DEBUG, "NDIS: query oid=%08x written (%d) > " 486 "len (%d)",oid, (unsigned int) written, len); 487 os_free(o); 488 return -1; 489 } 490 os_memcpy(data, o->Data, written); 491 ret = written; 492 os_free(o); 493 return ret; 494 #else /* CONFIG_USE_NDISUIO */ 495 char *buf; 496 PACKET_OID_DATA *o; 497 int ret; 498 499 buf = os_zalloc(sizeof(*o) + len); 500 if (buf == NULL) 501 return -1; 502 o = (PACKET_OID_DATA *) buf; 503 o->Oid = oid; 504 o->Length = len; 505 506 if (!PacketRequest(drv->adapter, FALSE, o)) { 507 wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed", 508 __func__, oid, len); 509 os_free(buf); 510 return -1; 511 } 512 if (o->Length > len) { 513 wpa_printf(MSG_DEBUG, "%s: oid=0x%x Length (%d) > len (%d)", 514 __func__, oid, (unsigned int) o->Length, len); 515 os_free(buf); 516 return -1; 517 } 518 os_memcpy(data, o->Data, o->Length); 519 ret = o->Length; 520 os_free(buf); 521 return ret; 522 #endif /* CONFIG_USE_NDISUIO */ 523 } 524 525 526 static int ndis_set_oid(struct wpa_driver_ndis_data *drv, unsigned int oid, 527 const char *data, size_t len) 528 { 529 #ifdef CONFIG_USE_NDISUIO 530 NDISUIO_SET_OID *o; 531 size_t buflen, reallen; 532 DWORD written; 533 char txt[50]; 534 535 os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid); 536 wpa_hexdump_key(MSG_MSGDUMP, txt, (const u8 *) data, len); 537 538 buflen = sizeof(*o) + len; 539 reallen = buflen - sizeof(o->Data); 540 o = os_zalloc(buflen); 541 if (o == NULL) 542 return -1; 543 o->Oid = oid; 544 #ifdef _WIN32_WCE 545 o->ptcDeviceName = drv->adapter_name; 546 #endif /* _WIN32_WCE */ 547 if (data) 548 os_memcpy(o->Data, data, len); 549 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_SET_OID_VALUE, 550 o, reallen, NULL, 0, &written, NULL)) { 551 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDISUIO_SET_OID_VALUE " 552 "(oid=%08x) failed: %d", oid, (int) GetLastError()); 553 os_free(o); 554 return -1; 555 } 556 os_free(o); 557 return 0; 558 #else /* CONFIG_USE_NDISUIO */ 559 char *buf; 560 PACKET_OID_DATA *o; 561 char txt[50]; 562 563 os_snprintf(txt, sizeof(txt), "NDIS: Set OID %08x", oid); 564 wpa_hexdump_key(MSG_MSGDUMP, txt, (const u8 *) data, len); 565 566 buf = os_zalloc(sizeof(*o) + len); 567 if (buf == NULL) 568 return -1; 569 o = (PACKET_OID_DATA *) buf; 570 o->Oid = oid; 571 o->Length = len; 572 if (data) 573 os_memcpy(o->Data, data, len); 574 575 if (!PacketRequest(drv->adapter, TRUE, o)) { 576 wpa_printf(MSG_DEBUG, "%s: oid=0x%x len (%d) failed", 577 __func__, oid, len); 578 os_free(buf); 579 return -1; 580 } 581 os_free(buf); 582 return 0; 583 #endif /* CONFIG_USE_NDISUIO */ 584 } 585 586 587 static int ndis_set_auth_mode(struct wpa_driver_ndis_data *drv, int mode) 588 { 589 u32 auth_mode = mode; 590 if (ndis_set_oid(drv, OID_802_11_AUTHENTICATION_MODE, 591 (char *) &auth_mode, sizeof(auth_mode)) < 0) { 592 wpa_printf(MSG_DEBUG, "NDIS: Failed to set " 593 "OID_802_11_AUTHENTICATION_MODE (%d)", 594 (int) auth_mode); 595 return -1; 596 } 597 return 0; 598 } 599 600 601 static int ndis_get_auth_mode(struct wpa_driver_ndis_data *drv) 602 { 603 u32 auth_mode; 604 int res; 605 res = ndis_get_oid(drv, OID_802_11_AUTHENTICATION_MODE, 606 (char *) &auth_mode, sizeof(auth_mode)); 607 if (res != sizeof(auth_mode)) { 608 wpa_printf(MSG_DEBUG, "NDIS: Failed to get " 609 "OID_802_11_AUTHENTICATION_MODE"); 610 return -1; 611 } 612 return auth_mode; 613 } 614 615 616 static int ndis_set_encr_status(struct wpa_driver_ndis_data *drv, int encr) 617 { 618 u32 encr_status = encr; 619 if (ndis_set_oid(drv, OID_802_11_ENCRYPTION_STATUS, 620 (char *) &encr_status, sizeof(encr_status)) < 0) { 621 wpa_printf(MSG_DEBUG, "NDIS: Failed to set " 622 "OID_802_11_ENCRYPTION_STATUS (%d)", encr); 623 return -1; 624 } 625 return 0; 626 } 627 628 629 static int ndis_get_encr_status(struct wpa_driver_ndis_data *drv) 630 { 631 u32 encr; 632 int res; 633 res = ndis_get_oid(drv, OID_802_11_ENCRYPTION_STATUS, 634 (char *) &encr, sizeof(encr)); 635 if (res != sizeof(encr)) { 636 wpa_printf(MSG_DEBUG, "NDIS: Failed to get " 637 "OID_802_11_ENCRYPTION_STATUS"); 638 return -1; 639 } 640 return encr; 641 } 642 643 644 static int wpa_driver_ndis_get_bssid(void *priv, u8 *bssid) 645 { 646 struct wpa_driver_ndis_data *drv = priv; 647 648 if (drv->wired) { 649 /* 650 * Report PAE group address as the "BSSID" for wired 651 * connection. 652 */ 653 os_memcpy(bssid, pae_group_addr, ETH_ALEN); 654 return 0; 655 } 656 657 return ndis_get_oid(drv, OID_802_11_BSSID, (char *) bssid, ETH_ALEN) < 658 0 ? -1 : 0; 659 } 660 661 662 static int wpa_driver_ndis_get_ssid(void *priv, u8 *ssid) 663 { 664 struct wpa_driver_ndis_data *drv = priv; 665 NDIS_802_11_SSID buf; 666 int res; 667 668 res = ndis_get_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf)); 669 if (res < 4) { 670 wpa_printf(MSG_DEBUG, "NDIS: Failed to get SSID"); 671 if (drv->wired) { 672 wpa_printf(MSG_DEBUG, "NDIS: Allow get_ssid failure " 673 "with a wired interface"); 674 return 0; 675 } 676 return -1; 677 } 678 os_memcpy(ssid, buf.Ssid, buf.SsidLength); 679 return buf.SsidLength; 680 } 681 682 683 static int wpa_driver_ndis_set_ssid(struct wpa_driver_ndis_data *drv, 684 const u8 *ssid, size_t ssid_len) 685 { 686 NDIS_802_11_SSID buf; 687 688 os_memset(&buf, 0, sizeof(buf)); 689 buf.SsidLength = ssid_len; 690 os_memcpy(buf.Ssid, ssid, ssid_len); 691 /* 692 * Make sure radio is marked enabled here so that scan request will not 693 * force SSID to be changed to a random one in order to enable radio at 694 * that point. 695 */ 696 drv->radio_enabled = 1; 697 return ndis_set_oid(drv, OID_802_11_SSID, (char *) &buf, sizeof(buf)); 698 } 699 700 701 /* Disconnect using OID_802_11_DISASSOCIATE. This will also turn the radio off. 702 */ 703 static int wpa_driver_ndis_radio_off(struct wpa_driver_ndis_data *drv) 704 { 705 drv->radio_enabled = 0; 706 return ndis_set_oid(drv, OID_802_11_DISASSOCIATE, " ", 4); 707 } 708 709 710 /* Disconnect by setting SSID to random (i.e., likely not used). */ 711 static int wpa_driver_ndis_disconnect(struct wpa_driver_ndis_data *drv) 712 { 713 char ssid[SSID_MAX_LEN]; 714 int i; 715 for (i = 0; i < SSID_MAX_LEN; i++) 716 ssid[i] = rand() & 0xff; 717 return wpa_driver_ndis_set_ssid(drv, (u8 *) ssid, SSID_MAX_LEN); 718 } 719 720 721 static int wpa_driver_ndis_deauthenticate(void *priv, const u8 *addr, 722 int reason_code) 723 { 724 struct wpa_driver_ndis_data *drv = priv; 725 return wpa_driver_ndis_disconnect(drv); 726 } 727 728 729 static void wpa_driver_ndis_scan_timeout(void *eloop_ctx, void *timeout_ctx) 730 { 731 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results"); 732 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL); 733 } 734 735 736 static int wpa_driver_ndis_scan_native80211( 737 struct wpa_driver_ndis_data *drv, 738 struct wpa_driver_scan_params *params) 739 { 740 DOT11_SCAN_REQUEST_V2 req; 741 int res; 742 743 os_memset(&req, 0, sizeof(req)); 744 req.dot11BSSType = dot11_BSS_type_any; 745 os_memset(req.dot11BSSID, 0xff, ETH_ALEN); 746 req.dot11ScanType = dot11_scan_type_auto; 747 res = ndis_set_oid(drv, OID_DOT11_SCAN_REQUEST, (char *) &req, 748 sizeof(req)); 749 eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx); 750 eloop_register_timeout(7, 0, wpa_driver_ndis_scan_timeout, drv, 751 drv->ctx); 752 return res; 753 } 754 755 756 static int wpa_driver_ndis_scan(void *priv, 757 struct wpa_driver_scan_params *params) 758 { 759 struct wpa_driver_ndis_data *drv = priv; 760 int res; 761 762 if (drv->native80211) 763 return wpa_driver_ndis_scan_native80211(drv, params); 764 765 if (!drv->radio_enabled) { 766 wpa_printf(MSG_DEBUG, "NDIS: turning radio on before the first" 767 " scan"); 768 if (wpa_driver_ndis_disconnect(drv) < 0) { 769 wpa_printf(MSG_DEBUG, "NDIS: failed to enable radio"); 770 } 771 drv->radio_enabled = 1; 772 } 773 774 res = ndis_set_oid(drv, OID_802_11_BSSID_LIST_SCAN, " ", 4); 775 eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx); 776 eloop_register_timeout(7, 0, wpa_driver_ndis_scan_timeout, drv, 777 drv->ctx); 778 return res; 779 } 780 781 782 static const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie) 783 { 784 return get_ie((const u8 *) (res + 1), res->ie_len, ie); 785 } 786 787 788 static struct wpa_scan_res * wpa_driver_ndis_add_scan_ssid( 789 struct wpa_scan_res *r, NDIS_802_11_SSID *ssid) 790 { 791 struct wpa_scan_res *nr; 792 u8 *pos; 793 794 if (wpa_scan_get_ie(r, WLAN_EID_SSID)) 795 return r; /* SSID IE already present */ 796 797 if (ssid->SsidLength == 0 || ssid->SsidLength > SSID_MAX_LEN) 798 return r; /* No valid SSID inside scan data */ 799 800 nr = os_realloc(r, sizeof(*r) + r->ie_len + 2 + ssid->SsidLength); 801 if (nr == NULL) 802 return r; 803 804 pos = ((u8 *) (nr + 1)) + nr->ie_len; 805 *pos++ = WLAN_EID_SSID; 806 *pos++ = ssid->SsidLength; 807 os_memcpy(pos, ssid->Ssid, ssid->SsidLength); 808 nr->ie_len += 2 + ssid->SsidLength; 809 810 return nr; 811 } 812 813 814 static struct wpa_scan_results * wpa_driver_ndis_get_scan_results(void *priv) 815 { 816 struct wpa_driver_ndis_data *drv = priv; 817 NDIS_802_11_BSSID_LIST_EX *b; 818 size_t blen, count, i; 819 int len; 820 char *pos; 821 struct wpa_scan_results *results; 822 struct wpa_scan_res *r; 823 824 blen = 65535; 825 b = os_zalloc(blen); 826 if (b == NULL) 827 return NULL; 828 len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen); 829 if (len < 0) { 830 wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results"); 831 os_free(b); 832 return NULL; 833 } 834 count = b->NumberOfItems; 835 836 results = os_zalloc(sizeof(*results)); 837 if (results == NULL) { 838 os_free(b); 839 return NULL; 840 } 841 results->res = os_calloc(count, sizeof(struct wpa_scan_res *)); 842 if (results->res == NULL) { 843 os_free(results); 844 os_free(b); 845 return NULL; 846 } 847 848 pos = (char *) &b->Bssid[0]; 849 for (i = 0; i < count; i++) { 850 NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos; 851 NDIS_802_11_FIXED_IEs *fixed; 852 853 if (bss->IELength < sizeof(NDIS_802_11_FIXED_IEs)) { 854 wpa_printf(MSG_DEBUG, "NDIS: too small IELength=%d", 855 (int) bss->IELength); 856 break; 857 } 858 if (((char *) bss->IEs) + bss->IELength > (char *) b + blen) { 859 /* 860 * Some NDIS drivers have been reported to include an 861 * entry with an invalid IELength in scan results and 862 * this has crashed wpa_supplicant, so validate the 863 * returned value before using it. 864 */ 865 wpa_printf(MSG_DEBUG, "NDIS: skipped invalid scan " 866 "result IE (BSSID=" MACSTR ") IELength=%d", 867 MAC2STR(bss->MacAddress), 868 (int) bss->IELength); 869 break; 870 } 871 872 r = os_zalloc(sizeof(*r) + bss->IELength - 873 sizeof(NDIS_802_11_FIXED_IEs)); 874 if (r == NULL) 875 break; 876 877 os_memcpy(r->bssid, bss->MacAddress, ETH_ALEN); 878 r->level = (int) bss->Rssi; 879 r->freq = bss->Configuration.DSConfig / 1000; 880 fixed = (NDIS_802_11_FIXED_IEs *) bss->IEs; 881 r->beacon_int = WPA_GET_LE16((u8 *) &fixed->BeaconInterval); 882 r->caps = WPA_GET_LE16((u8 *) &fixed->Capabilities); 883 r->tsf = WPA_GET_LE64(fixed->Timestamp); 884 os_memcpy(r + 1, bss->IEs + sizeof(NDIS_802_11_FIXED_IEs), 885 bss->IELength - sizeof(NDIS_802_11_FIXED_IEs)); 886 r->ie_len = bss->IELength - sizeof(NDIS_802_11_FIXED_IEs); 887 r = wpa_driver_ndis_add_scan_ssid(r, &bss->Ssid); 888 889 results->res[results->num++] = r; 890 891 pos += bss->Length; 892 if (pos > (char *) b + blen) 893 break; 894 } 895 896 os_free(b); 897 898 return results; 899 } 900 901 902 static int wpa_driver_ndis_remove_key(struct wpa_driver_ndis_data *drv, 903 int key_idx, const u8 *addr, 904 const u8 *bssid, int pairwise) 905 { 906 NDIS_802_11_REMOVE_KEY rkey; 907 NDIS_802_11_KEY_INDEX index; 908 int res, res2; 909 910 os_memset(&rkey, 0, sizeof(rkey)); 911 912 rkey.Length = sizeof(rkey); 913 rkey.KeyIndex = key_idx; 914 if (pairwise) 915 rkey.KeyIndex |= 1 << 30; 916 os_memcpy(rkey.BSSID, bssid, ETH_ALEN); 917 918 res = ndis_set_oid(drv, OID_802_11_REMOVE_KEY, (char *) &rkey, 919 sizeof(rkey)); 920 if (!pairwise) { 921 index = key_idx; 922 res2 = ndis_set_oid(drv, OID_802_11_REMOVE_WEP, 923 (char *) &index, sizeof(index)); 924 } else 925 res2 = 0; 926 927 if (res < 0 && res2 < 0) 928 return -1; 929 return 0; 930 } 931 932 933 static int wpa_driver_ndis_add_wep(struct wpa_driver_ndis_data *drv, 934 int pairwise, int key_idx, int set_tx, 935 const u8 *key, size_t key_len) 936 { 937 NDIS_802_11_WEP *wep; 938 size_t len; 939 int res; 940 941 len = 12 + key_len; 942 wep = os_zalloc(len); 943 if (wep == NULL) 944 return -1; 945 wep->Length = len; 946 wep->KeyIndex = key_idx; 947 if (set_tx) 948 wep->KeyIndex |= 1 << 31; 949 #if 0 /* Setting bit30 does not seem to work with some NDIS drivers */ 950 if (pairwise) 951 wep->KeyIndex |= 1 << 30; 952 #endif 953 wep->KeyLength = key_len; 954 os_memcpy(wep->KeyMaterial, key, key_len); 955 956 wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_WEP", 957 (u8 *) wep, len); 958 res = ndis_set_oid(drv, OID_802_11_ADD_WEP, (char *) wep, len); 959 960 os_free(wep); 961 962 return res; 963 } 964 965 966 static int wpa_driver_ndis_set_key(const char *ifname, void *priv, 967 enum wpa_alg alg, const u8 *addr, 968 int key_idx, int set_tx, 969 const u8 *seq, size_t seq_len, 970 const u8 *key, size_t key_len) 971 { 972 struct wpa_driver_ndis_data *drv = priv; 973 size_t len, i; 974 NDIS_802_11_KEY *nkey; 975 int res, pairwise; 976 u8 bssid[ETH_ALEN]; 977 978 if (addr == NULL || is_broadcast_ether_addr(addr)) { 979 /* Group Key */ 980 pairwise = 0; 981 if (wpa_driver_ndis_get_bssid(drv, bssid) < 0) 982 os_memset(bssid, 0xff, ETH_ALEN); 983 } else { 984 /* Pairwise Key */ 985 pairwise = 1; 986 os_memcpy(bssid, addr, ETH_ALEN); 987 } 988 989 if (alg == WPA_ALG_NONE || key_len == 0) { 990 return wpa_driver_ndis_remove_key(drv, key_idx, addr, bssid, 991 pairwise); 992 } 993 994 if (alg == WPA_ALG_WEP) { 995 return wpa_driver_ndis_add_wep(drv, pairwise, key_idx, set_tx, 996 key, key_len); 997 } 998 999 len = 12 + 6 + 6 + 8 + key_len; 1000 1001 nkey = os_zalloc(len); 1002 if (nkey == NULL) 1003 return -1; 1004 1005 nkey->Length = len; 1006 nkey->KeyIndex = key_idx; 1007 if (set_tx) 1008 nkey->KeyIndex |= 1 << 31; 1009 if (pairwise) 1010 nkey->KeyIndex |= 1 << 30; 1011 if (seq && seq_len) 1012 nkey->KeyIndex |= 1 << 29; 1013 nkey->KeyLength = key_len; 1014 os_memcpy(nkey->BSSID, bssid, ETH_ALEN); 1015 if (seq && seq_len) { 1016 for (i = 0; i < seq_len; i++) 1017 nkey->KeyRSC |= (ULONGLONG) seq[i] << (i * 8); 1018 } 1019 if (alg == WPA_ALG_TKIP && key_len == 32) { 1020 os_memcpy(nkey->KeyMaterial, key, 16); 1021 os_memcpy(nkey->KeyMaterial + 16, key + 24, 8); 1022 os_memcpy(nkey->KeyMaterial + 24, key + 16, 8); 1023 } else { 1024 os_memcpy(nkey->KeyMaterial, key, key_len); 1025 } 1026 1027 wpa_hexdump_key(MSG_MSGDUMP, "NDIS: OID_802_11_ADD_KEY", 1028 (u8 *) nkey, len); 1029 res = ndis_set_oid(drv, OID_802_11_ADD_KEY, (char *) nkey, len); 1030 os_free(nkey); 1031 1032 return res; 1033 } 1034 1035 1036 static int 1037 wpa_driver_ndis_associate(void *priv, 1038 struct wpa_driver_associate_params *params) 1039 { 1040 struct wpa_driver_ndis_data *drv = priv; 1041 u32 auth_mode, encr, priv_mode, mode; 1042 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 1043 1044 drv->mode = params->mode; 1045 1046 /* Note: Setting OID_802_11_INFRASTRUCTURE_MODE clears current keys, 1047 * so static WEP keys needs to be set again after this. */ 1048 if (params->mode == IEEE80211_MODE_IBSS) { 1049 mode = Ndis802_11IBSS; 1050 /* Need to make sure that BSSID polling is enabled for 1051 * IBSS mode. */ 1052 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL); 1053 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout, 1054 drv, NULL); 1055 } else 1056 mode = Ndis802_11Infrastructure; 1057 if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE, 1058 (char *) &mode, sizeof(mode)) < 0) { 1059 wpa_printf(MSG_DEBUG, "NDIS: Failed to set " 1060 "OID_802_11_INFRASTRUCTURE_MODE (%d)", 1061 (int) mode); 1062 /* Try to continue anyway */ 1063 } 1064 1065 if (params->key_mgmt_suite == WPA_KEY_MGMT_NONE || 1066 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA) { 1067 /* Re-set WEP keys if static WEP configuration is used. */ 1068 int i; 1069 for (i = 0; i < 4; i++) { 1070 if (!params->wep_key[i]) 1071 continue; 1072 wpa_printf(MSG_DEBUG, "NDIS: Re-setting static WEP " 1073 "key %d", i); 1074 wpa_driver_ndis_set_key(drv->ifname, drv, WPA_ALG_WEP, 1075 bcast, i, 1076 i == params->wep_tx_keyidx, 1077 NULL, 0, params->wep_key[i], 1078 params->wep_key_len[i]); 1079 } 1080 } 1081 1082 if (params->wpa_ie == NULL || params->wpa_ie_len == 0) { 1083 if (params->auth_alg & WPA_AUTH_ALG_SHARED) { 1084 if (params->auth_alg & WPA_AUTH_ALG_OPEN) 1085 auth_mode = Ndis802_11AuthModeAutoSwitch; 1086 else 1087 auth_mode = Ndis802_11AuthModeShared; 1088 } else 1089 auth_mode = Ndis802_11AuthModeOpen; 1090 priv_mode = Ndis802_11PrivFilterAcceptAll; 1091 } else if (params->wpa_ie[0] == WLAN_EID_RSN) { 1092 priv_mode = Ndis802_11PrivFilter8021xWEP; 1093 if (params->key_mgmt_suite == WPA_KEY_MGMT_PSK) 1094 auth_mode = Ndis802_11AuthModeWPA2PSK; 1095 else 1096 auth_mode = Ndis802_11AuthModeWPA2; 1097 #ifdef CONFIG_WPS 1098 } else if (params->key_mgmt_suite == WPA_KEY_MGMT_WPS) { 1099 auth_mode = Ndis802_11AuthModeOpen; 1100 priv_mode = Ndis802_11PrivFilterAcceptAll; 1101 if (params->wps == WPS_MODE_PRIVACY) { 1102 u8 dummy_key[5] = { 0x11, 0x22, 0x33, 0x44, 0x55 }; 1103 /* 1104 * Some NDIS drivers refuse to associate in open mode 1105 * configuration due to Privacy field mismatch, so use 1106 * a workaround to make the configuration look like 1107 * matching one for WPS provisioning. 1108 */ 1109 wpa_printf(MSG_DEBUG, "NDIS: Set dummy WEP key as a " 1110 "workaround to allow driver to associate " 1111 "for WPS"); 1112 wpa_driver_ndis_set_key(drv->ifname, drv, WPA_ALG_WEP, 1113 bcast, 0, 1, 1114 NULL, 0, dummy_key, 1115 sizeof(dummy_key)); 1116 } 1117 #endif /* CONFIG_WPS */ 1118 } else { 1119 priv_mode = Ndis802_11PrivFilter8021xWEP; 1120 if (params->key_mgmt_suite == WPA_KEY_MGMT_WPA_NONE) 1121 auth_mode = Ndis802_11AuthModeWPANone; 1122 else if (params->key_mgmt_suite == WPA_KEY_MGMT_PSK) 1123 auth_mode = Ndis802_11AuthModeWPAPSK; 1124 else 1125 auth_mode = Ndis802_11AuthModeWPA; 1126 } 1127 1128 switch (params->pairwise_suite) { 1129 case WPA_CIPHER_CCMP: 1130 encr = Ndis802_11Encryption3Enabled; 1131 break; 1132 case WPA_CIPHER_TKIP: 1133 encr = Ndis802_11Encryption2Enabled; 1134 break; 1135 case WPA_CIPHER_WEP40: 1136 case WPA_CIPHER_WEP104: 1137 encr = Ndis802_11Encryption1Enabled; 1138 break; 1139 case WPA_CIPHER_NONE: 1140 #ifdef CONFIG_WPS 1141 if (params->wps == WPS_MODE_PRIVACY) { 1142 encr = Ndis802_11Encryption1Enabled; 1143 break; 1144 } 1145 #endif /* CONFIG_WPS */ 1146 if (params->group_suite == WPA_CIPHER_CCMP) 1147 encr = Ndis802_11Encryption3Enabled; 1148 else if (params->group_suite == WPA_CIPHER_TKIP) 1149 encr = Ndis802_11Encryption2Enabled; 1150 else 1151 encr = Ndis802_11EncryptionDisabled; 1152 break; 1153 default: 1154 #ifdef CONFIG_WPS 1155 if (params->wps == WPS_MODE_PRIVACY) { 1156 encr = Ndis802_11Encryption1Enabled; 1157 break; 1158 } 1159 #endif /* CONFIG_WPS */ 1160 encr = Ndis802_11EncryptionDisabled; 1161 break; 1162 }; 1163 1164 if (ndis_set_oid(drv, OID_802_11_PRIVACY_FILTER, 1165 (char *) &priv_mode, sizeof(priv_mode)) < 0) { 1166 wpa_printf(MSG_DEBUG, "NDIS: Failed to set " 1167 "OID_802_11_PRIVACY_FILTER (%d)", 1168 (int) priv_mode); 1169 /* Try to continue anyway */ 1170 } 1171 1172 ndis_set_auth_mode(drv, auth_mode); 1173 ndis_set_encr_status(drv, encr); 1174 1175 if (params->bssid) { 1176 ndis_set_oid(drv, OID_802_11_BSSID, (char *) params->bssid, 1177 ETH_ALEN); 1178 drv->oid_bssid_set = 1; 1179 } else if (drv->oid_bssid_set) { 1180 ndis_set_oid(drv, OID_802_11_BSSID, "\xff\xff\xff\xff\xff\xff", 1181 ETH_ALEN); 1182 drv->oid_bssid_set = 0; 1183 } 1184 1185 return wpa_driver_ndis_set_ssid(drv, params->ssid, params->ssid_len); 1186 } 1187 1188 1189 static int wpa_driver_ndis_set_pmkid(struct wpa_driver_ndis_data *drv) 1190 { 1191 int len, count, i, ret; 1192 struct ndis_pmkid_entry *entry; 1193 NDIS_802_11_PMKID *p; 1194 1195 count = 0; 1196 entry = drv->pmkid; 1197 while (entry) { 1198 count++; 1199 if (count >= drv->no_of_pmkid) 1200 break; 1201 entry = entry->next; 1202 } 1203 len = 8 + count * sizeof(BSSID_INFO); 1204 p = os_zalloc(len); 1205 if (p == NULL) 1206 return -1; 1207 1208 p->Length = len; 1209 p->BSSIDInfoCount = count; 1210 entry = drv->pmkid; 1211 for (i = 0; i < count; i++) { 1212 os_memcpy(&p->BSSIDInfo[i].BSSID, entry->bssid, ETH_ALEN); 1213 os_memcpy(&p->BSSIDInfo[i].PMKID, entry->pmkid, 16); 1214 entry = entry->next; 1215 } 1216 wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID", (u8 *) p, len); 1217 ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) p, len); 1218 os_free(p); 1219 return ret; 1220 } 1221 1222 1223 static int wpa_driver_ndis_add_pmkid(void *priv, const u8 *bssid, 1224 const u8 *pmkid) 1225 { 1226 struct wpa_driver_ndis_data *drv = priv; 1227 struct ndis_pmkid_entry *entry, *prev; 1228 1229 if (drv->no_of_pmkid == 0) 1230 return 0; 1231 1232 prev = NULL; 1233 entry = drv->pmkid; 1234 while (entry) { 1235 if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0) 1236 break; 1237 prev = entry; 1238 entry = entry->next; 1239 } 1240 1241 if (entry) { 1242 /* Replace existing entry for this BSSID and move it into the 1243 * beginning of the list. */ 1244 os_memcpy(entry->pmkid, pmkid, 16); 1245 if (prev) { 1246 prev->next = entry->next; 1247 entry->next = drv->pmkid; 1248 drv->pmkid = entry; 1249 } 1250 } else { 1251 entry = os_malloc(sizeof(*entry)); 1252 if (entry) { 1253 os_memcpy(entry->bssid, bssid, ETH_ALEN); 1254 os_memcpy(entry->pmkid, pmkid, 16); 1255 entry->next = drv->pmkid; 1256 drv->pmkid = entry; 1257 } 1258 } 1259 1260 return wpa_driver_ndis_set_pmkid(drv); 1261 } 1262 1263 1264 static int wpa_driver_ndis_remove_pmkid(void *priv, const u8 *bssid, 1265 const u8 *pmkid) 1266 { 1267 struct wpa_driver_ndis_data *drv = priv; 1268 struct ndis_pmkid_entry *entry, *prev; 1269 1270 if (drv->no_of_pmkid == 0) 1271 return 0; 1272 1273 entry = drv->pmkid; 1274 prev = NULL; 1275 while (entry) { 1276 if (os_memcmp(entry->bssid, bssid, ETH_ALEN) == 0 && 1277 os_memcmp(entry->pmkid, pmkid, 16) == 0) { 1278 if (prev) 1279 prev->next = entry->next; 1280 else 1281 drv->pmkid = entry->next; 1282 os_free(entry); 1283 break; 1284 } 1285 prev = entry; 1286 entry = entry->next; 1287 } 1288 return wpa_driver_ndis_set_pmkid(drv); 1289 } 1290 1291 1292 static int wpa_driver_ndis_flush_pmkid(void *priv) 1293 { 1294 struct wpa_driver_ndis_data *drv = priv; 1295 NDIS_802_11_PMKID p; 1296 struct ndis_pmkid_entry *pmkid, *prev; 1297 int prev_authmode, ret; 1298 1299 if (drv->no_of_pmkid == 0) 1300 return 0; 1301 1302 pmkid = drv->pmkid; 1303 drv->pmkid = NULL; 1304 while (pmkid) { 1305 prev = pmkid; 1306 pmkid = pmkid->next; 1307 os_free(prev); 1308 } 1309 1310 /* 1311 * Some drivers may refuse OID_802_11_PMKID if authMode is not set to 1312 * WPA2, so change authMode temporarily, if needed. 1313 */ 1314 prev_authmode = ndis_get_auth_mode(drv); 1315 if (prev_authmode != Ndis802_11AuthModeWPA2) 1316 ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA2); 1317 1318 os_memset(&p, 0, sizeof(p)); 1319 p.Length = 8; 1320 p.BSSIDInfoCount = 0; 1321 wpa_hexdump(MSG_MSGDUMP, "NDIS: OID_802_11_PMKID (flush)", 1322 (u8 *) &p, 8); 1323 ret = ndis_set_oid(drv, OID_802_11_PMKID, (char *) &p, 8); 1324 1325 if (prev_authmode != Ndis802_11AuthModeWPA2) 1326 ndis_set_auth_mode(drv, prev_authmode); 1327 1328 return ret; 1329 } 1330 1331 1332 static int wpa_driver_ndis_get_associnfo(struct wpa_driver_ndis_data *drv) 1333 { 1334 char buf[512], *pos; 1335 NDIS_802_11_ASSOCIATION_INFORMATION *ai; 1336 int len; 1337 union wpa_event_data data; 1338 NDIS_802_11_BSSID_LIST_EX *b; 1339 size_t blen, i; 1340 1341 len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION, buf, 1342 sizeof(buf)); 1343 if (len < 0) { 1344 wpa_printf(MSG_DEBUG, "NDIS: failed to get association " 1345 "information"); 1346 return -1; 1347 } 1348 if (len > sizeof(buf)) { 1349 /* Some drivers seem to be producing incorrect length for this 1350 * data. Limit the length to the current buffer size to avoid 1351 * crashing in hexdump. The data seems to be otherwise valid, 1352 * so better try to use it. */ 1353 wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association " 1354 "information length %d", len); 1355 len = ndis_get_oid(drv, OID_802_11_ASSOCIATION_INFORMATION, 1356 buf, sizeof(buf)); 1357 if (len < -1) { 1358 wpa_printf(MSG_DEBUG, "NDIS: re-reading association " 1359 "information failed"); 1360 return -1; 1361 } 1362 if (len > sizeof(buf)) { 1363 wpa_printf(MSG_DEBUG, "NDIS: ignored bogus association" 1364 " information length %d (re-read)", len); 1365 len = sizeof(buf); 1366 } 1367 } 1368 wpa_hexdump(MSG_MSGDUMP, "NDIS: association information", 1369 (u8 *) buf, len); 1370 if (len < sizeof(*ai)) { 1371 wpa_printf(MSG_DEBUG, "NDIS: too short association " 1372 "information"); 1373 return -1; 1374 } 1375 ai = (NDIS_802_11_ASSOCIATION_INFORMATION *) buf; 1376 wpa_printf(MSG_DEBUG, "NDIS: ReqFixed=0x%x RespFixed=0x%x off_req=%d " 1377 "off_resp=%d len_req=%d len_resp=%d", 1378 ai->AvailableRequestFixedIEs, ai->AvailableResponseFixedIEs, 1379 (int) ai->OffsetRequestIEs, (int) ai->OffsetResponseIEs, 1380 (int) ai->RequestIELength, (int) ai->ResponseIELength); 1381 1382 if (ai->OffsetRequestIEs + ai->RequestIELength > (unsigned) len || 1383 ai->OffsetResponseIEs + ai->ResponseIELength > (unsigned) len) { 1384 wpa_printf(MSG_DEBUG, "NDIS: association information - " 1385 "IE overflow"); 1386 return -1; 1387 } 1388 1389 wpa_hexdump(MSG_MSGDUMP, "NDIS: Request IEs", 1390 (u8 *) buf + ai->OffsetRequestIEs, ai->RequestIELength); 1391 wpa_hexdump(MSG_MSGDUMP, "NDIS: Response IEs", 1392 (u8 *) buf + ai->OffsetResponseIEs, ai->ResponseIELength); 1393 1394 os_memset(&data, 0, sizeof(data)); 1395 data.assoc_info.req_ies = (u8 *) buf + ai->OffsetRequestIEs; 1396 data.assoc_info.req_ies_len = ai->RequestIELength; 1397 data.assoc_info.resp_ies = (u8 *) buf + ai->OffsetResponseIEs; 1398 data.assoc_info.resp_ies_len = ai->ResponseIELength; 1399 1400 blen = 65535; 1401 b = os_zalloc(blen); 1402 if (b == NULL) 1403 goto skip_scan_results; 1404 len = ndis_get_oid(drv, OID_802_11_BSSID_LIST, (char *) b, blen); 1405 if (len < 0) { 1406 wpa_printf(MSG_DEBUG, "NDIS: failed to get scan results"); 1407 os_free(b); 1408 b = NULL; 1409 goto skip_scan_results; 1410 } 1411 wpa_printf(MSG_DEBUG, "NDIS: %d BSSID items to process for AssocInfo", 1412 (unsigned int) b->NumberOfItems); 1413 1414 pos = (char *) &b->Bssid[0]; 1415 for (i = 0; i < b->NumberOfItems; i++) { 1416 NDIS_WLAN_BSSID_EX *bss = (NDIS_WLAN_BSSID_EX *) pos; 1417 if (os_memcmp(drv->bssid, bss->MacAddress, ETH_ALEN) == 0 && 1418 bss->IELength > sizeof(NDIS_802_11_FIXED_IEs)) { 1419 data.assoc_info.beacon_ies = 1420 ((u8 *) bss->IEs) + 1421 sizeof(NDIS_802_11_FIXED_IEs); 1422 data.assoc_info.beacon_ies_len = 1423 bss->IELength - sizeof(NDIS_802_11_FIXED_IEs); 1424 wpa_hexdump(MSG_MSGDUMP, "NDIS: Beacon IEs", 1425 data.assoc_info.beacon_ies, 1426 data.assoc_info.beacon_ies_len); 1427 break; 1428 } 1429 pos += bss->Length; 1430 if (pos > (char *) b + blen) 1431 break; 1432 } 1433 1434 skip_scan_results: 1435 wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data); 1436 1437 os_free(b); 1438 1439 return 0; 1440 } 1441 1442 1443 static void wpa_driver_ndis_poll_timeout(void *eloop_ctx, void *timeout_ctx) 1444 { 1445 struct wpa_driver_ndis_data *drv = eloop_ctx; 1446 u8 bssid[ETH_ALEN]; 1447 int poll; 1448 1449 if (drv->wired) 1450 return; 1451 1452 if (wpa_driver_ndis_get_bssid(drv, bssid)) { 1453 /* Disconnected */ 1454 if (!is_zero_ether_addr(drv->bssid)) { 1455 os_memset(drv->bssid, 0, ETH_ALEN); 1456 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL); 1457 } 1458 } else { 1459 /* Connected */ 1460 if (os_memcmp(drv->bssid, bssid, ETH_ALEN) != 0) { 1461 os_memcpy(drv->bssid, bssid, ETH_ALEN); 1462 wpa_driver_ndis_get_associnfo(drv); 1463 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL); 1464 } 1465 } 1466 1467 /* When using integrated NDIS event receiver, we can skip BSSID 1468 * polling when using infrastructure network. However, when using 1469 * IBSS mode, many driver do not seem to generate connection event, 1470 * so we need to enable BSSID polling to figure out when IBSS network 1471 * has been formed. 1472 */ 1473 poll = drv->mode == IEEE80211_MODE_IBSS; 1474 #ifndef CONFIG_NDIS_EVENTS_INTEGRATED 1475 #ifndef _WIN32_WCE 1476 poll = 1; 1477 #endif /* _WIN32_WCE */ 1478 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */ 1479 1480 if (poll) { 1481 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout, 1482 drv, NULL); 1483 } 1484 } 1485 1486 1487 static void wpa_driver_ndis_poll(void *priv) 1488 { 1489 struct wpa_driver_ndis_data *drv = priv; 1490 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL); 1491 wpa_driver_ndis_poll_timeout(drv, NULL); 1492 } 1493 1494 1495 /* Called when driver generates Media Connect Event by calling 1496 * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_CONNECT */ 1497 void wpa_driver_ndis_event_connect(struct wpa_driver_ndis_data *drv) 1498 { 1499 wpa_printf(MSG_DEBUG, "NDIS: Media Connect Event"); 1500 if (wpa_driver_ndis_get_bssid(drv, drv->bssid) == 0) { 1501 wpa_driver_ndis_get_associnfo(drv); 1502 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL); 1503 } 1504 } 1505 1506 1507 /* Called when driver generates Media Disconnect Event by calling 1508 * NdisMIndicateStatus() with NDIS_STATUS_MEDIA_DISCONNECT */ 1509 void wpa_driver_ndis_event_disconnect(struct wpa_driver_ndis_data *drv) 1510 { 1511 wpa_printf(MSG_DEBUG, "NDIS: Media Disconnect Event"); 1512 os_memset(drv->bssid, 0, ETH_ALEN); 1513 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL); 1514 } 1515 1516 1517 static void wpa_driver_ndis_event_auth(struct wpa_driver_ndis_data *drv, 1518 const u8 *data, size_t data_len) 1519 { 1520 NDIS_802_11_AUTHENTICATION_REQUEST *req; 1521 int pairwise = 0, group = 0; 1522 union wpa_event_data event; 1523 1524 if (data_len < sizeof(*req)) { 1525 wpa_printf(MSG_DEBUG, "NDIS: Too short Authentication Request " 1526 "Event (len=%d)", data_len); 1527 return; 1528 } 1529 req = (NDIS_802_11_AUTHENTICATION_REQUEST *) data; 1530 1531 wpa_printf(MSG_DEBUG, "NDIS: Authentication Request Event: " 1532 "Bssid " MACSTR " Flags 0x%x", 1533 MAC2STR(req->Bssid), (int) req->Flags); 1534 1535 if ((req->Flags & NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR) == 1536 NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR) 1537 pairwise = 1; 1538 else if ((req->Flags & NDIS_802_11_AUTH_REQUEST_GROUP_ERROR) == 1539 NDIS_802_11_AUTH_REQUEST_GROUP_ERROR) 1540 group = 1; 1541 1542 if (pairwise || group) { 1543 os_memset(&event, 0, sizeof(event)); 1544 event.michael_mic_failure.unicast = pairwise; 1545 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, 1546 &event); 1547 } 1548 } 1549 1550 1551 static void wpa_driver_ndis_event_pmkid(struct wpa_driver_ndis_data *drv, 1552 const u8 *data, size_t data_len) 1553 { 1554 NDIS_802_11_PMKID_CANDIDATE_LIST *pmkid; 1555 size_t i; 1556 union wpa_event_data event; 1557 1558 if (data_len < 8) { 1559 wpa_printf(MSG_DEBUG, "NDIS: Too short PMKID Candidate List " 1560 "Event (len=%d)", data_len); 1561 return; 1562 } 1563 pmkid = (NDIS_802_11_PMKID_CANDIDATE_LIST *) data; 1564 wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List Event - Version %d " 1565 "NumCandidates %d", 1566 (int) pmkid->Version, (int) pmkid->NumCandidates); 1567 1568 if (pmkid->Version != 1) { 1569 wpa_printf(MSG_DEBUG, "NDIS: Unsupported PMKID Candidate List " 1570 "Version %d", (int) pmkid->Version); 1571 return; 1572 } 1573 1574 if (data_len < 8 + pmkid->NumCandidates * sizeof(PMKID_CANDIDATE)) { 1575 wpa_printf(MSG_DEBUG, "NDIS: PMKID Candidate List underflow"); 1576 return; 1577 } 1578 1579 os_memset(&event, 0, sizeof(event)); 1580 for (i = 0; i < pmkid->NumCandidates; i++) { 1581 PMKID_CANDIDATE *p = &pmkid->CandidateList[i]; 1582 wpa_printf(MSG_DEBUG, "NDIS: %d: " MACSTR " Flags 0x%x", 1583 i, MAC2STR(p->BSSID), (int) p->Flags); 1584 os_memcpy(event.pmkid_candidate.bssid, p->BSSID, ETH_ALEN); 1585 event.pmkid_candidate.index = i; 1586 event.pmkid_candidate.preauth = 1587 p->Flags & NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED; 1588 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, 1589 &event); 1590 } 1591 } 1592 1593 1594 /* Called when driver calls NdisMIndicateStatus() with 1595 * NDIS_STATUS_MEDIA_SPECIFIC_INDICATION */ 1596 void wpa_driver_ndis_event_media_specific(struct wpa_driver_ndis_data *drv, 1597 const u8 *data, size_t data_len) 1598 { 1599 NDIS_802_11_STATUS_INDICATION *status; 1600 1601 if (data == NULL || data_len < sizeof(*status)) 1602 return; 1603 1604 wpa_hexdump(MSG_DEBUG, "NDIS: Media Specific Indication", 1605 data, data_len); 1606 1607 status = (NDIS_802_11_STATUS_INDICATION *) data; 1608 data += sizeof(status); 1609 data_len -= sizeof(status); 1610 1611 switch (status->StatusType) { 1612 case Ndis802_11StatusType_Authentication: 1613 wpa_driver_ndis_event_auth(drv, data, data_len); 1614 break; 1615 case Ndis802_11StatusType_PMKID_CandidateList: 1616 wpa_driver_ndis_event_pmkid(drv, data, data_len); 1617 break; 1618 default: 1619 wpa_printf(MSG_DEBUG, "NDIS: Unknown StatusType %d", 1620 (int) status->StatusType); 1621 break; 1622 } 1623 } 1624 1625 1626 /* Called when an adapter is added */ 1627 void wpa_driver_ndis_event_adapter_arrival(struct wpa_driver_ndis_data *drv) 1628 { 1629 union wpa_event_data event; 1630 int i; 1631 1632 wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Arrival"); 1633 1634 for (i = 0; i < 30; i++) { 1635 /* Re-open Packet32/NDISUIO connection */ 1636 wpa_driver_ndis_adapter_close(drv); 1637 if (wpa_driver_ndis_adapter_init(drv) < 0 || 1638 wpa_driver_ndis_adapter_open(drv) < 0) { 1639 wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialization " 1640 "(%d) failed", i); 1641 os_sleep(1, 0); 1642 } else { 1643 wpa_printf(MSG_DEBUG, "NDIS: Driver re-initialized"); 1644 break; 1645 } 1646 } 1647 1648 os_memset(&event, 0, sizeof(event)); 1649 os_strlcpy(event.interface_status.ifname, drv->ifname, 1650 sizeof(event.interface_status.ifname)); 1651 event.interface_status.ievent = EVENT_INTERFACE_ADDED; 1652 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event); 1653 } 1654 1655 1656 /* Called when an adapter is removed */ 1657 void wpa_driver_ndis_event_adapter_removal(struct wpa_driver_ndis_data *drv) 1658 { 1659 union wpa_event_data event; 1660 1661 wpa_printf(MSG_DEBUG, "NDIS: Notify Adapter Removal"); 1662 os_memset(&event, 0, sizeof(event)); 1663 os_strlcpy(event.interface_status.ifname, drv->ifname, 1664 sizeof(event.interface_status.ifname)); 1665 event.interface_status.ievent = EVENT_INTERFACE_REMOVED; 1666 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event); 1667 } 1668 1669 1670 static void 1671 wpa_driver_ndis_get_wpa_capability(struct wpa_driver_ndis_data *drv) 1672 { 1673 wpa_printf(MSG_DEBUG, "NDIS: verifying driver WPA capability"); 1674 1675 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPA) == 0 && 1676 ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPA) { 1677 wpa_printf(MSG_DEBUG, "NDIS: WPA key management supported"); 1678 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA; 1679 } 1680 1681 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeWPAPSK) == 0 && 1682 ndis_get_auth_mode(drv) == Ndis802_11AuthModeWPAPSK) { 1683 wpa_printf(MSG_DEBUG, "NDIS: WPA-PSK key management " 1684 "supported"); 1685 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK; 1686 } 1687 1688 if (ndis_set_encr_status(drv, Ndis802_11Encryption3Enabled) == 0 && 1689 ndis_get_encr_status(drv) == Ndis802_11Encryption3KeyAbsent) { 1690 wpa_printf(MSG_DEBUG, "NDIS: CCMP encryption supported"); 1691 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP; 1692 } 1693 1694 if (ndis_set_encr_status(drv, Ndis802_11Encryption2Enabled) == 0 && 1695 ndis_get_encr_status(drv) == Ndis802_11Encryption2KeyAbsent) { 1696 wpa_printf(MSG_DEBUG, "NDIS: TKIP encryption supported"); 1697 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP; 1698 } 1699 1700 if (ndis_set_encr_status(drv, Ndis802_11Encryption1Enabled) == 0 && 1701 ndis_get_encr_status(drv) == Ndis802_11Encryption1KeyAbsent) { 1702 wpa_printf(MSG_DEBUG, "NDIS: WEP encryption supported"); 1703 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 | 1704 WPA_DRIVER_CAPA_ENC_WEP104; 1705 } 1706 1707 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeShared) == 0 && 1708 ndis_get_auth_mode(drv) == Ndis802_11AuthModeShared) { 1709 drv->capa.auth |= WPA_DRIVER_AUTH_SHARED; 1710 } 1711 1712 if (ndis_set_auth_mode(drv, Ndis802_11AuthModeOpen) == 0 && 1713 ndis_get_auth_mode(drv) == Ndis802_11AuthModeOpen) { 1714 drv->capa.auth |= WPA_DRIVER_AUTH_OPEN; 1715 } 1716 1717 ndis_set_encr_status(drv, Ndis802_11EncryptionDisabled); 1718 1719 /* Could also verify OID_802_11_ADD_KEY error reporting and 1720 * support for OID_802_11_ASSOCIATION_INFORMATION. */ 1721 1722 if (drv->capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA && 1723 drv->capa.enc & (WPA_DRIVER_CAPA_ENC_TKIP | 1724 WPA_DRIVER_CAPA_ENC_CCMP)) { 1725 wpa_printf(MSG_DEBUG, "NDIS: driver supports WPA"); 1726 drv->has_capability = 1; 1727 } else { 1728 wpa_printf(MSG_DEBUG, "NDIS: no WPA support found"); 1729 } 1730 1731 wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x " 1732 "enc 0x%x auth 0x%x", 1733 drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth); 1734 } 1735 1736 1737 static void wpa_driver_ndis_get_capability(struct wpa_driver_ndis_data *drv) 1738 { 1739 char buf[512]; 1740 int len; 1741 size_t i; 1742 NDIS_802_11_CAPABILITY *c; 1743 1744 drv->capa.flags = WPA_DRIVER_FLAGS_DRIVER_IE; 1745 1746 len = ndis_get_oid(drv, OID_802_11_CAPABILITY, buf, sizeof(buf)); 1747 if (len < 0) { 1748 wpa_driver_ndis_get_wpa_capability(drv); 1749 return; 1750 } 1751 1752 wpa_hexdump(MSG_MSGDUMP, "OID_802_11_CAPABILITY", (u8 *) buf, len); 1753 c = (NDIS_802_11_CAPABILITY *) buf; 1754 if (len < sizeof(*c) || c->Version != 2) { 1755 wpa_printf(MSG_DEBUG, "NDIS: unsupported " 1756 "OID_802_11_CAPABILITY data"); 1757 return; 1758 } 1759 wpa_printf(MSG_DEBUG, "NDIS: Driver supports OID_802_11_CAPABILITY - " 1760 "NoOfPMKIDs %d NoOfAuthEncrPairs %d", 1761 (int) c->NoOfPMKIDs, 1762 (int) c->NoOfAuthEncryptPairsSupported); 1763 drv->has_capability = 1; 1764 drv->no_of_pmkid = c->NoOfPMKIDs; 1765 for (i = 0; i < c->NoOfAuthEncryptPairsSupported; i++) { 1766 NDIS_802_11_AUTHENTICATION_ENCRYPTION *ae; 1767 ae = &c->AuthenticationEncryptionSupported[i]; 1768 if ((char *) (ae + 1) > buf + len) { 1769 wpa_printf(MSG_DEBUG, "NDIS: auth/encr pair list " 1770 "overflow"); 1771 break; 1772 } 1773 wpa_printf(MSG_MSGDUMP, "NDIS: %d - auth %d encr %d", 1774 i, (int) ae->AuthModeSupported, 1775 (int) ae->EncryptStatusSupported); 1776 switch (ae->AuthModeSupported) { 1777 case Ndis802_11AuthModeOpen: 1778 drv->capa.auth |= WPA_DRIVER_AUTH_OPEN; 1779 break; 1780 case Ndis802_11AuthModeShared: 1781 drv->capa.auth |= WPA_DRIVER_AUTH_SHARED; 1782 break; 1783 case Ndis802_11AuthModeWPA: 1784 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA; 1785 break; 1786 case Ndis802_11AuthModeWPAPSK: 1787 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK; 1788 break; 1789 case Ndis802_11AuthModeWPA2: 1790 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2; 1791 break; 1792 case Ndis802_11AuthModeWPA2PSK: 1793 drv->capa.key_mgmt |= 1794 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; 1795 break; 1796 case Ndis802_11AuthModeWPANone: 1797 drv->capa.key_mgmt |= 1798 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE; 1799 break; 1800 default: 1801 break; 1802 } 1803 switch (ae->EncryptStatusSupported) { 1804 case Ndis802_11Encryption1Enabled: 1805 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40; 1806 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP104; 1807 break; 1808 case Ndis802_11Encryption2Enabled: 1809 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP; 1810 break; 1811 case Ndis802_11Encryption3Enabled: 1812 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP; 1813 break; 1814 default: 1815 break; 1816 } 1817 } 1818 1819 wpa_printf(MSG_DEBUG, "NDIS: driver capabilities: key_mgmt 0x%x " 1820 "enc 0x%x auth 0x%x", 1821 drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth); 1822 } 1823 1824 1825 static int wpa_driver_ndis_get_capa(void *priv, struct wpa_driver_capa *capa) 1826 { 1827 struct wpa_driver_ndis_data *drv = priv; 1828 if (!drv->has_capability) 1829 return -1; 1830 os_memcpy(capa, &drv->capa, sizeof(*capa)); 1831 return 0; 1832 } 1833 1834 1835 static const char * wpa_driver_ndis_get_ifname(void *priv) 1836 { 1837 struct wpa_driver_ndis_data *drv = priv; 1838 return drv->ifname; 1839 } 1840 1841 1842 static const u8 * wpa_driver_ndis_get_mac_addr(void *priv) 1843 { 1844 struct wpa_driver_ndis_data *drv = priv; 1845 return drv->own_addr; 1846 } 1847 1848 1849 #ifdef _WIN32_WCE 1850 1851 #define NDISUIO_MSG_SIZE (sizeof(NDISUIO_DEVICE_NOTIFICATION) + 512) 1852 1853 static void ndisuio_notification_receive(void *eloop_data, void *user_ctx) 1854 { 1855 struct wpa_driver_ndis_data *drv = eloop_data; 1856 NDISUIO_DEVICE_NOTIFICATION *hdr; 1857 u8 buf[NDISUIO_MSG_SIZE]; 1858 DWORD len, flags; 1859 1860 if (!ReadMsgQueue(drv->event_queue, buf, NDISUIO_MSG_SIZE, &len, 0, 1861 &flags)) { 1862 wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: " 1863 "ReadMsgQueue failed: %d", (int) GetLastError()); 1864 return; 1865 } 1866 1867 if (len < sizeof(NDISUIO_DEVICE_NOTIFICATION)) { 1868 wpa_printf(MSG_DEBUG, "ndisuio_notification_receive: " 1869 "Too short message (len=%d)", (int) len); 1870 return; 1871 } 1872 1873 hdr = (NDISUIO_DEVICE_NOTIFICATION *) buf; 1874 wpa_printf(MSG_DEBUG, "NDIS: Notification received: len=%d type=0x%x", 1875 (int) len, hdr->dwNotificationType); 1876 1877 switch (hdr->dwNotificationType) { 1878 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL 1879 case NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL: 1880 wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_ARRIVAL"); 1881 wpa_driver_ndis_event_adapter_arrival(drv); 1882 break; 1883 #endif 1884 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL 1885 case NDISUIO_NOTIFICATION_ADAPTER_REMOVAL: 1886 wpa_printf(MSG_DEBUG, "NDIS: ADAPTER_REMOVAL"); 1887 wpa_driver_ndis_event_adapter_removal(drv); 1888 break; 1889 #endif 1890 case NDISUIO_NOTIFICATION_MEDIA_CONNECT: 1891 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_CONNECT"); 1892 SetEvent(drv->connected_event); 1893 wpa_driver_ndis_event_connect(drv); 1894 break; 1895 case NDISUIO_NOTIFICATION_MEDIA_DISCONNECT: 1896 ResetEvent(drv->connected_event); 1897 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_DISCONNECT"); 1898 wpa_driver_ndis_event_disconnect(drv); 1899 break; 1900 case NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION: 1901 wpa_printf(MSG_DEBUG, "NDIS: MEDIA_SPECIFIC_NOTIFICATION"); 1902 #if _WIN32_WCE == 420 || _WIN32_WCE == 0x420 1903 wpa_driver_ndis_event_media_specific( 1904 drv, hdr->pvStatusBuffer, hdr->uiStatusBufferSize); 1905 #else 1906 wpa_driver_ndis_event_media_specific( 1907 drv, ((const u8 *) hdr) + hdr->uiOffsetToStatusBuffer, 1908 (size_t) hdr->uiStatusBufferSize); 1909 #endif 1910 break; 1911 default: 1912 wpa_printf(MSG_DEBUG, "NDIS: Unknown notification type 0x%x", 1913 hdr->dwNotificationType); 1914 break; 1915 } 1916 } 1917 1918 1919 static void ndisuio_notification_deinit(struct wpa_driver_ndis_data *drv) 1920 { 1921 NDISUIO_REQUEST_NOTIFICATION req; 1922 1923 memset(&req, 0, sizeof(req)); 1924 req.hMsgQueue = drv->event_queue; 1925 req.dwNotificationTypes = 0; 1926 1927 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION, 1928 &req, sizeof(req), NULL, 0, NULL, NULL)) { 1929 wpa_printf(MSG_INFO, "ndisuio_notification_deinit: " 1930 "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d", 1931 (int) GetLastError()); 1932 } 1933 1934 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_CANCEL_NOTIFICATION, 1935 NULL, 0, NULL, 0, NULL, NULL)) { 1936 wpa_printf(MSG_INFO, "ndisuio_notification_deinit: " 1937 "IOCTL_NDISUIO_CANCEL_NOTIFICATION failed: %d", 1938 (int) GetLastError()); 1939 } 1940 1941 if (drv->event_queue) { 1942 eloop_unregister_event(drv->event_queue, 1943 sizeof(drv->event_queue)); 1944 CloseHandle(drv->event_queue); 1945 drv->event_queue = NULL; 1946 } 1947 1948 if (drv->connected_event) { 1949 CloseHandle(drv->connected_event); 1950 drv->connected_event = NULL; 1951 } 1952 } 1953 1954 1955 static int ndisuio_notification_init(struct wpa_driver_ndis_data *drv) 1956 { 1957 MSGQUEUEOPTIONS opt; 1958 NDISUIO_REQUEST_NOTIFICATION req; 1959 1960 drv->connected_event = 1961 CreateEvent(NULL, TRUE, FALSE, TEXT("WpaSupplicantConnected")); 1962 if (drv->connected_event == NULL) { 1963 wpa_printf(MSG_INFO, "ndisuio_notification_init: " 1964 "CreateEvent failed: %d", 1965 (int) GetLastError()); 1966 return -1; 1967 } 1968 1969 memset(&opt, 0, sizeof(opt)); 1970 opt.dwSize = sizeof(opt); 1971 opt.dwMaxMessages = 5; 1972 opt.cbMaxMessage = NDISUIO_MSG_SIZE; 1973 opt.bReadAccess = TRUE; 1974 1975 drv->event_queue = CreateMsgQueue(NULL, &opt); 1976 if (drv->event_queue == NULL) { 1977 wpa_printf(MSG_INFO, "ndisuio_notification_init: " 1978 "CreateMsgQueue failed: %d", 1979 (int) GetLastError()); 1980 ndisuio_notification_deinit(drv); 1981 return -1; 1982 } 1983 1984 memset(&req, 0, sizeof(req)); 1985 req.hMsgQueue = drv->event_queue; 1986 req.dwNotificationTypes = 1987 #ifdef NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL 1988 NDISUIO_NOTIFICATION_ADAPTER_ARRIVAL | 1989 #endif 1990 #ifdef NDISUIO_NOTIFICATION_ADAPTER_REMOVAL 1991 NDISUIO_NOTIFICATION_ADAPTER_REMOVAL | 1992 #endif 1993 NDISUIO_NOTIFICATION_MEDIA_CONNECT | 1994 NDISUIO_NOTIFICATION_MEDIA_DISCONNECT | 1995 NDISUIO_NOTIFICATION_MEDIA_SPECIFIC_NOTIFICATION; 1996 1997 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_REQUEST_NOTIFICATION, 1998 &req, sizeof(req), NULL, 0, NULL, NULL)) { 1999 wpa_printf(MSG_INFO, "ndisuio_notification_init: " 2000 "IOCTL_NDISUIO_REQUEST_NOTIFICATION failed: %d", 2001 (int) GetLastError()); 2002 ndisuio_notification_deinit(drv); 2003 return -1; 2004 } 2005 2006 eloop_register_event(drv->event_queue, sizeof(drv->event_queue), 2007 ndisuio_notification_receive, drv, NULL); 2008 2009 return 0; 2010 } 2011 #endif /* _WIN32_WCE */ 2012 2013 2014 static int wpa_driver_ndis_get_names(struct wpa_driver_ndis_data *drv) 2015 { 2016 #ifdef CONFIG_USE_NDISUIO 2017 NDISUIO_QUERY_BINDING *b; 2018 size_t blen = sizeof(*b) + 1024; 2019 int i, error, found = 0; 2020 DWORD written; 2021 char name[256], desc[256], *dpos; 2022 WCHAR *pos; 2023 size_t j, len, dlen; 2024 2025 b = os_malloc(blen); 2026 if (b == NULL) 2027 return -1; 2028 2029 for (i = 0; ; i++) { 2030 os_memset(b, 0, blen); 2031 b->BindingIndex = i; 2032 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_QUERY_BINDING, 2033 b, sizeof(NDISUIO_QUERY_BINDING), b, blen, 2034 &written, NULL)) { 2035 error = (int) GetLastError(); 2036 if (error == ERROR_NO_MORE_ITEMS) 2037 break; 2038 wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING " 2039 "failed: %d", error); 2040 break; 2041 } 2042 2043 pos = (WCHAR *) ((char *) b + b->DeviceNameOffset); 2044 len = b->DeviceNameLength; 2045 if (len >= sizeof(name)) 2046 len = sizeof(name) - 1; 2047 for (j = 0; j < len; j++) 2048 name[j] = (char) pos[j]; 2049 name[len] = '\0'; 2050 2051 pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset); 2052 len = b->DeviceDescrLength; 2053 if (len >= sizeof(desc)) 2054 len = sizeof(desc) - 1; 2055 for (j = 0; j < len; j++) 2056 desc[j] = (char) pos[j]; 2057 desc[len] = '\0'; 2058 2059 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc); 2060 2061 if (os_strstr(name, drv->ifname)) { 2062 wpa_printf(MSG_DEBUG, "NDIS: Interface name match"); 2063 found = 1; 2064 break; 2065 } 2066 2067 if (os_strncmp(desc, drv->ifname, os_strlen(drv->ifname)) == 0) 2068 { 2069 wpa_printf(MSG_DEBUG, "NDIS: Interface description " 2070 "match"); 2071 found = 1; 2072 break; 2073 } 2074 } 2075 2076 if (!found) { 2077 wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'", 2078 drv->ifname); 2079 os_free(b); 2080 return -1; 2081 } 2082 2083 os_strlcpy(drv->ifname, 2084 os_strncmp(name, "\\DEVICE\\", 8) == 0 ? name + 8 : name, 2085 sizeof(drv->ifname)); 2086 #ifdef _WIN32_WCE 2087 drv->adapter_name = wpa_strdup_tchar(drv->ifname); 2088 if (drv->adapter_name == NULL) { 2089 wpa_printf(MSG_ERROR, "NDIS: Failed to allocate memory for " 2090 "adapter name"); 2091 os_free(b); 2092 return -1; 2093 } 2094 #endif /* _WIN32_WCE */ 2095 2096 dpos = os_strstr(desc, " - "); 2097 if (dpos) 2098 dlen = dpos - desc; 2099 else 2100 dlen = os_strlen(desc); 2101 drv->adapter_desc = dup_binstr(desc, dlen); 2102 os_free(b); 2103 if (drv->adapter_desc == NULL) 2104 return -1; 2105 2106 wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'", 2107 drv->adapter_desc); 2108 2109 return 0; 2110 #else /* CONFIG_USE_NDISUIO */ 2111 PTSTR _names; 2112 char *names, *pos, *pos2; 2113 ULONG len; 2114 BOOLEAN res; 2115 #define MAX_ADAPTERS 32 2116 char *name[MAX_ADAPTERS]; 2117 char *desc[MAX_ADAPTERS]; 2118 int num_name, num_desc, i, found_name, found_desc; 2119 size_t dlen; 2120 2121 wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s", 2122 PacketGetVersion()); 2123 2124 len = 8192; 2125 _names = os_zalloc(len); 2126 if (_names == NULL) 2127 return -1; 2128 2129 res = PacketGetAdapterNames(_names, &len); 2130 if (!res && len > 8192) { 2131 os_free(_names); 2132 _names = os_zalloc(len); 2133 if (_names == NULL) 2134 return -1; 2135 res = PacketGetAdapterNames(_names, &len); 2136 } 2137 2138 if (!res) { 2139 wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list " 2140 "(PacketGetAdapterNames)"); 2141 os_free(_names); 2142 return -1; 2143 } 2144 2145 names = (char *) _names; 2146 if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') { 2147 wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in " 2148 "UNICODE"); 2149 /* Convert to ASCII */ 2150 pos2 = pos = names; 2151 while (pos2 < names + len) { 2152 if (pos2[0] == '\0' && pos2[1] == '\0' && 2153 pos2[2] == '\0' && pos2[3] == '\0') { 2154 pos2 += 4; 2155 break; 2156 } 2157 *pos++ = pos2[0]; 2158 pos2 += 2; 2159 } 2160 os_memcpy(pos + 2, names, pos - names); 2161 pos += 2; 2162 } else 2163 pos = names; 2164 2165 num_name = 0; 2166 while (pos < names + len) { 2167 name[num_name] = pos; 2168 while (*pos && pos < names + len) 2169 pos++; 2170 if (pos + 1 >= names + len) { 2171 os_free(names); 2172 return -1; 2173 } 2174 pos++; 2175 num_name++; 2176 if (num_name >= MAX_ADAPTERS) { 2177 wpa_printf(MSG_DEBUG, "NDIS: Too many adapters"); 2178 os_free(names); 2179 return -1; 2180 } 2181 if (*pos == '\0') { 2182 wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found", 2183 num_name); 2184 pos++; 2185 break; 2186 } 2187 } 2188 2189 num_desc = 0; 2190 while (pos < names + len) { 2191 desc[num_desc] = pos; 2192 while (*pos && pos < names + len) 2193 pos++; 2194 if (pos + 1 >= names + len) { 2195 os_free(names); 2196 return -1; 2197 } 2198 pos++; 2199 num_desc++; 2200 if (num_desc >= MAX_ADAPTERS) { 2201 wpa_printf(MSG_DEBUG, "NDIS: Too many adapter " 2202 "descriptions"); 2203 os_free(names); 2204 return -1; 2205 } 2206 if (*pos == '\0') { 2207 wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions " 2208 "found", num_name); 2209 pos++; 2210 break; 2211 } 2212 } 2213 2214 /* 2215 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter 2216 * descriptions. Fill in dummy descriptors to work around this. 2217 */ 2218 while (num_desc < num_name) 2219 desc[num_desc++] = "dummy description"; 2220 2221 if (num_name != num_desc) { 2222 wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and " 2223 "description counts (%d != %d)", 2224 num_name, num_desc); 2225 os_free(names); 2226 return -1; 2227 } 2228 2229 found_name = found_desc = -1; 2230 for (i = 0; i < num_name; i++) { 2231 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", 2232 i, name[i], desc[i]); 2233 if (found_name == -1 && os_strstr(name[i], drv->ifname)) 2234 found_name = i; 2235 if (found_desc == -1 && 2236 os_strncmp(desc[i], drv->ifname, os_strlen(drv->ifname)) == 2237 0) 2238 found_desc = i; 2239 } 2240 2241 if (found_name < 0 && found_desc >= 0) { 2242 wpa_printf(MSG_DEBUG, "NDIS: Matched interface '%s' based on " 2243 "description '%s'", 2244 name[found_desc], desc[found_desc]); 2245 found_name = found_desc; 2246 os_strlcpy(drv->ifname, 2247 os_strncmp(name[found_desc], "\\Device\\NPF_", 12) 2248 == 0 ? name[found_desc] + 12 : name[found_desc], 2249 sizeof(drv->ifname)); 2250 } 2251 2252 if (found_name < 0) { 2253 wpa_printf(MSG_DEBUG, "NDIS: Could not find interface '%s'", 2254 drv->ifname); 2255 os_free(names); 2256 return -1; 2257 } 2258 2259 i = found_name; 2260 pos = os_strrchr(desc[i], '('); 2261 if (pos) { 2262 dlen = pos - desc[i]; 2263 pos--; 2264 if (pos > desc[i] && *pos == ' ') 2265 dlen--; 2266 } else { 2267 dlen = os_strlen(desc[i]); 2268 } 2269 drv->adapter_desc = dup_binstr(desc[i], dlen); 2270 os_free(names); 2271 if (drv->adapter_desc == NULL) 2272 return -1; 2273 2274 wpa_printf(MSG_DEBUG, "NDIS: Adapter description prefix '%s'", 2275 drv->adapter_desc); 2276 2277 return 0; 2278 #endif /* CONFIG_USE_NDISUIO */ 2279 } 2280 2281 2282 #if defined(CONFIG_NATIVE_WINDOWS) || defined(__CYGWIN__) 2283 #ifndef _WIN32_WCE 2284 /* 2285 * These structures are undocumented for WinXP; only WinCE version is 2286 * documented. These would be included wzcsapi.h if it were available. Some 2287 * changes here have been needed to make the structures match with WinXP SP2. 2288 * It is unclear whether these work with any other version. 2289 */ 2290 2291 typedef struct { 2292 LPWSTR wszGuid; 2293 } INTF_KEY_ENTRY, *PINTF_KEY_ENTRY; 2294 2295 typedef struct { 2296 DWORD dwNumIntfs; 2297 PINTF_KEY_ENTRY pIntfs; 2298 } INTFS_KEY_TABLE, *PINTFS_KEY_TABLE; 2299 2300 typedef struct { 2301 DWORD dwDataLen; 2302 LPBYTE pData; 2303 } RAW_DATA, *PRAW_DATA; 2304 2305 typedef struct { 2306 LPWSTR wszGuid; 2307 LPWSTR wszDescr; 2308 ULONG ulMediaState; 2309 ULONG ulMediaType; 2310 ULONG ulPhysicalMediaType; 2311 INT nInfraMode; 2312 INT nAuthMode; 2313 INT nWepStatus; 2314 #ifndef _WIN32_WCE 2315 u8 pad[2]; /* why is this needed? */ 2316 #endif /* _WIN32_WCE */ 2317 DWORD dwCtlFlags; 2318 DWORD dwCapabilities; /* something added for WinXP SP2(?) */ 2319 RAW_DATA rdSSID; 2320 RAW_DATA rdBSSID; 2321 RAW_DATA rdBSSIDList; 2322 RAW_DATA rdStSSIDList; 2323 RAW_DATA rdCtrlData; 2324 #ifdef UNDER_CE 2325 BOOL bInitialized; 2326 #endif 2327 DWORD nWPAMCastCipher; 2328 /* add some extra buffer for later additions since this interface is 2329 * far from stable */ 2330 u8 later_additions[100]; 2331 } INTF_ENTRY, *PINTF_ENTRY; 2332 2333 #define INTF_ALL 0xffffffff 2334 #define INTF_ALL_FLAGS 0x0000ffff 2335 #define INTF_CTLFLAGS 0x00000010 2336 #define INTFCTL_ENABLED 0x8000 2337 #endif /* _WIN32_WCE */ 2338 2339 2340 #ifdef _WIN32_WCE 2341 static int wpa_driver_ndis_rebind_adapter(struct wpa_driver_ndis_data *drv) 2342 { 2343 HANDLE ndis; 2344 TCHAR multi[100]; 2345 int len; 2346 2347 len = _tcslen(drv->adapter_name); 2348 if (len > 80) 2349 return -1; 2350 2351 ndis = CreateFile(DD_NDIS_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, 2352 0, NULL, OPEN_EXISTING, 0, NULL); 2353 if (ndis == INVALID_HANDLE_VALUE) { 2354 wpa_printf(MSG_DEBUG, "NDIS: Failed to open file to NDIS " 2355 "device: %d", (int) GetLastError()); 2356 return -1; 2357 } 2358 2359 len++; 2360 memcpy(multi, drv->adapter_name, len * sizeof(TCHAR)); 2361 memcpy(&multi[len], TEXT("NDISUIO\0"), 9 * sizeof(TCHAR)); 2362 len += 9; 2363 2364 if (!DeviceIoControl(ndis, IOCTL_NDIS_REBIND_ADAPTER, 2365 multi, len * sizeof(TCHAR), NULL, 0, NULL, NULL)) 2366 { 2367 wpa_printf(MSG_DEBUG, "NDIS: IOCTL_NDIS_REBIND_ADAPTER " 2368 "failed: 0x%x", (int) GetLastError()); 2369 wpa_hexdump_ascii(MSG_DEBUG, "NDIS: rebind multi_sz", 2370 (u8 *) multi, len * sizeof(TCHAR)); 2371 CloseHandle(ndis); 2372 return -1; 2373 } 2374 2375 CloseHandle(ndis); 2376 2377 wpa_printf(MSG_DEBUG, "NDIS: Requested NDIS rebind of NDISUIO " 2378 "protocol"); 2379 2380 return 0; 2381 } 2382 #endif /* _WIN32_WCE */ 2383 2384 2385 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv, 2386 int enable) 2387 { 2388 #ifdef _WIN32_WCE 2389 HKEY hk, hk2; 2390 LONG ret; 2391 DWORD i, hnd, len; 2392 TCHAR keyname[256], devname[256]; 2393 2394 #define WZC_DRIVER TEXT("Drivers\\BuiltIn\\ZeroConfig") 2395 2396 if (enable) { 2397 HANDLE h; 2398 h = ActivateDeviceEx(WZC_DRIVER, NULL, 0, NULL); 2399 if (h == INVALID_HANDLE_VALUE || h == 0) { 2400 wpa_printf(MSG_DEBUG, "NDIS: Failed to re-enable WZC " 2401 "- ActivateDeviceEx failed: %d", 2402 (int) GetLastError()); 2403 return -1; 2404 } 2405 2406 wpa_printf(MSG_DEBUG, "NDIS: WZC re-enabled"); 2407 return wpa_driver_ndis_rebind_adapter(drv); 2408 } 2409 2410 /* 2411 * Unfortunately, just disabling the WZC for an interface is not enough 2412 * to free NDISUIO for us, so need to disable and unload WZC completely 2413 * for now when using WinCE with NDISUIO. In addition, must request 2414 * NDISUIO protocol to be rebound to the adapter in order to free the 2415 * NDISUIO binding that WZC hold before us. 2416 */ 2417 2418 /* Enumerate HKLM\Drivers\Active\* to find a handle to WZC. */ 2419 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, DEVLOAD_ACTIVE_KEY, 0, 0, &hk); 2420 if (ret != ERROR_SUCCESS) { 2421 wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(DEVLOAD_ACTIVE_KEY) " 2422 "failed: %d %d", (int) ret, (int) GetLastError()); 2423 return -1; 2424 } 2425 2426 for (i = 0; ; i++) { 2427 len = sizeof(keyname); 2428 ret = RegEnumKeyEx(hk, i, keyname, &len, NULL, NULL, NULL, 2429 NULL); 2430 if (ret != ERROR_SUCCESS) { 2431 wpa_printf(MSG_DEBUG, "NDIS: Could not find active " 2432 "WZC - assuming it is not running."); 2433 RegCloseKey(hk); 2434 return -1; 2435 } 2436 2437 ret = RegOpenKeyEx(hk, keyname, 0, 0, &hk2); 2438 if (ret != ERROR_SUCCESS) { 2439 wpa_printf(MSG_DEBUG, "NDIS: RegOpenKeyEx(active dev) " 2440 "failed: %d %d", 2441 (int) ret, (int) GetLastError()); 2442 continue; 2443 } 2444 2445 len = sizeof(devname); 2446 ret = RegQueryValueEx(hk2, DEVLOAD_DEVKEY_VALNAME, NULL, NULL, 2447 (LPBYTE) devname, &len); 2448 if (ret != ERROR_SUCCESS) { 2449 wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx(" 2450 "DEVKEY_VALNAME) failed: %d %d", 2451 (int) ret, (int) GetLastError()); 2452 RegCloseKey(hk2); 2453 continue; 2454 } 2455 2456 if (_tcscmp(devname, WZC_DRIVER) == 0) 2457 break; 2458 2459 RegCloseKey(hk2); 2460 } 2461 2462 RegCloseKey(hk); 2463 2464 /* Found WZC - get handle to it. */ 2465 len = sizeof(hnd); 2466 ret = RegQueryValueEx(hk2, DEVLOAD_HANDLE_VALNAME, NULL, NULL, 2467 (PUCHAR) &hnd, &len); 2468 if (ret != ERROR_SUCCESS) { 2469 wpa_printf(MSG_DEBUG, "NDIS: RegQueryValueEx(HANDLE_VALNAME) " 2470 "failed: %d %d", (int) ret, (int) GetLastError()); 2471 RegCloseKey(hk2); 2472 return -1; 2473 } 2474 2475 RegCloseKey(hk2); 2476 2477 /* Deactivate WZC */ 2478 if (!DeactivateDevice((HANDLE) hnd)) { 2479 wpa_printf(MSG_DEBUG, "NDIS: DeactivateDevice failed: %d", 2480 (int) GetLastError()); 2481 return -1; 2482 } 2483 2484 wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily"); 2485 drv->wzc_disabled = 1; 2486 return wpa_driver_ndis_rebind_adapter(drv); 2487 2488 #else /* _WIN32_WCE */ 2489 2490 HMODULE hm; 2491 DWORD (WINAPI *wzc_enum_interf)(LPWSTR pSrvAddr, 2492 PINTFS_KEY_TABLE pIntfs); 2493 DWORD (WINAPI *wzc_query_interf)(LPWSTR pSrvAddr, DWORD dwInFlags, 2494 PINTF_ENTRY pIntf, 2495 LPDWORD pdwOutFlags); 2496 DWORD (WINAPI *wzc_set_interf)(LPWSTR pSrvAddr, DWORD dwInFlags, 2497 PINTF_ENTRY pIntf, LPDWORD pdwOutFlags); 2498 int ret = -1, j; 2499 DWORD res; 2500 INTFS_KEY_TABLE guids; 2501 INTF_ENTRY intf; 2502 char guid[128]; 2503 WCHAR *pos; 2504 DWORD flags, i; 2505 2506 hm = LoadLibrary(TEXT("wzcsapi.dll")); 2507 if (hm == NULL) { 2508 wpa_printf(MSG_DEBUG, "NDIS: Failed to load wzcsapi.dll (%u) " 2509 "- WZC probably not running", 2510 (unsigned int) GetLastError()); 2511 return -1; 2512 } 2513 2514 #ifdef _WIN32_WCE 2515 wzc_enum_interf = (void *) GetProcAddressA(hm, "WZCEnumInterfaces"); 2516 wzc_query_interf = (void *) GetProcAddressA(hm, "WZCQueryInterface"); 2517 wzc_set_interf = (void *) GetProcAddressA(hm, "WZCSetInterface"); 2518 #else /* _WIN32_WCE */ 2519 wzc_enum_interf = (void *) GetProcAddress(hm, "WZCEnumInterfaces"); 2520 wzc_query_interf = (void *) GetProcAddress(hm, "WZCQueryInterface"); 2521 wzc_set_interf = (void *) GetProcAddress(hm, "WZCSetInterface"); 2522 #endif /* _WIN32_WCE */ 2523 2524 if (wzc_enum_interf == NULL || wzc_query_interf == NULL || 2525 wzc_set_interf == NULL) { 2526 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces, " 2527 "WZCQueryInterface, or WZCSetInterface not found " 2528 "in wzcsapi.dll"); 2529 goto fail; 2530 } 2531 2532 os_memset(&guids, 0, sizeof(guids)); 2533 res = wzc_enum_interf(NULL, &guids); 2534 if (res != 0) { 2535 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces failed: %d; " 2536 "WZC service is apparently not running", 2537 (int) res); 2538 goto fail; 2539 } 2540 2541 wpa_printf(MSG_DEBUG, "NDIS: WZCEnumInterfaces: %d interfaces", 2542 (int) guids.dwNumIntfs); 2543 2544 for (i = 0; i < guids.dwNumIntfs; i++) { 2545 pos = guids.pIntfs[i].wszGuid; 2546 for (j = 0; j < sizeof(guid); j++) { 2547 guid[j] = (char) *pos; 2548 if (*pos == 0) 2549 break; 2550 pos++; 2551 } 2552 guid[sizeof(guid) - 1] = '\0'; 2553 wpa_printf(MSG_DEBUG, "NDIS: intfs %d GUID '%s'", 2554 (int) i, guid); 2555 if (os_strstr(drv->ifname, guid) == NULL) 2556 continue; 2557 2558 wpa_printf(MSG_DEBUG, "NDIS: Current interface found from " 2559 "WZC"); 2560 break; 2561 } 2562 2563 if (i >= guids.dwNumIntfs) { 2564 wpa_printf(MSG_DEBUG, "NDIS: Current interface not found from " 2565 "WZC"); 2566 goto fail; 2567 } 2568 2569 os_memset(&intf, 0, sizeof(intf)); 2570 intf.wszGuid = guids.pIntfs[i].wszGuid; 2571 /* Set flags to verify that the structure has not changed. */ 2572 intf.dwCtlFlags = -1; 2573 flags = 0; 2574 res = wzc_query_interf(NULL, INTFCTL_ENABLED, &intf, &flags); 2575 if (res != 0) { 2576 wpa_printf(MSG_DEBUG, "NDIS: Could not query flags for the " 2577 "WZC interface: %d (0x%x)", 2578 (int) res, (int) res); 2579 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u", 2580 (unsigned int) GetLastError()); 2581 goto fail; 2582 } 2583 2584 wpa_printf(MSG_DEBUG, "NDIS: WZC interface flags 0x%x dwCtlFlags 0x%x", 2585 (int) flags, (int) intf.dwCtlFlags); 2586 2587 if (intf.dwCtlFlags == -1) { 2588 wpa_printf(MSG_DEBUG, "NDIS: Looks like wzcsapi has changed " 2589 "again - could not disable WZC"); 2590 wpa_hexdump(MSG_MSGDUMP, "NDIS: intf", 2591 (u8 *) &intf, sizeof(intf)); 2592 goto fail; 2593 } 2594 2595 if (enable) { 2596 if (!(intf.dwCtlFlags & INTFCTL_ENABLED)) { 2597 wpa_printf(MSG_DEBUG, "NDIS: Enabling WZC for this " 2598 "interface"); 2599 intf.dwCtlFlags |= INTFCTL_ENABLED; 2600 res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf, 2601 &flags); 2602 if (res != 0) { 2603 wpa_printf(MSG_DEBUG, "NDIS: Failed to enable " 2604 "WZC: %d (0x%x)", 2605 (int) res, (int) res); 2606 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u", 2607 (unsigned int) GetLastError()); 2608 goto fail; 2609 } 2610 wpa_printf(MSG_DEBUG, "NDIS: Re-enabled WZC for this " 2611 "interface"); 2612 drv->wzc_disabled = 0; 2613 } 2614 } else { 2615 if (intf.dwCtlFlags & INTFCTL_ENABLED) { 2616 wpa_printf(MSG_DEBUG, "NDIS: Disabling WZC for this " 2617 "interface"); 2618 intf.dwCtlFlags &= ~INTFCTL_ENABLED; 2619 res = wzc_set_interf(NULL, INTFCTL_ENABLED, &intf, 2620 &flags); 2621 if (res != 0) { 2622 wpa_printf(MSG_DEBUG, "NDIS: Failed to " 2623 "disable WZC: %d (0x%x)", 2624 (int) res, (int) res); 2625 wpa_printf(MSG_DEBUG, "NDIS: GetLastError: %u", 2626 (unsigned int) GetLastError()); 2627 goto fail; 2628 } 2629 wpa_printf(MSG_DEBUG, "NDIS: Disabled WZC temporarily " 2630 "for this interface"); 2631 drv->wzc_disabled = 1; 2632 } else { 2633 wpa_printf(MSG_DEBUG, "NDIS: WZC was not enabled for " 2634 "this interface"); 2635 } 2636 } 2637 2638 ret = 0; 2639 2640 fail: 2641 FreeLibrary(hm); 2642 2643 return ret; 2644 #endif /* _WIN32_WCE */ 2645 } 2646 2647 #else /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */ 2648 2649 static int wpa_driver_ndis_set_wzc(struct wpa_driver_ndis_data *drv, 2650 int enable) 2651 { 2652 return 0; 2653 } 2654 2655 #endif /* CONFIG_NATIVE_WINDOWS || __CYGWIN__ */ 2656 2657 2658 #ifdef CONFIG_USE_NDISUIO 2659 /* 2660 * l2_packet_ndis.c is sharing the same handle to NDISUIO, so we must be able 2661 * to export this handle. This is somewhat ugly, but there is no better 2662 * mechanism available to pass data from driver interface to l2_packet wrapper. 2663 */ 2664 static HANDLE driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE; 2665 2666 HANDLE driver_ndis_get_ndisuio_handle(void) 2667 { 2668 return driver_ndis_ndisuio_handle; 2669 } 2670 #endif /* CONFIG_USE_NDISUIO */ 2671 2672 2673 static int wpa_driver_ndis_adapter_init(struct wpa_driver_ndis_data *drv) 2674 { 2675 #ifdef CONFIG_USE_NDISUIO 2676 #ifndef _WIN32_WCE 2677 #define NDISUIO_DEVICE_NAME TEXT("\\\\.\\\\Ndisuio") 2678 DWORD written; 2679 #endif /* _WIN32_WCE */ 2680 drv->ndisuio = CreateFile(NDISUIO_DEVICE_NAME, 2681 GENERIC_READ | GENERIC_WRITE, 0, NULL, 2682 OPEN_EXISTING, 2683 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 2684 INVALID_HANDLE_VALUE); 2685 if (drv->ndisuio == INVALID_HANDLE_VALUE) { 2686 wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to " 2687 "NDISUIO: %d", (int) GetLastError()); 2688 return -1; 2689 } 2690 driver_ndis_ndisuio_handle = drv->ndisuio; 2691 2692 #ifndef _WIN32_WCE 2693 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0, 2694 NULL, 0, &written, NULL)) { 2695 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: " 2696 "%d", (int) GetLastError()); 2697 CloseHandle(drv->ndisuio); 2698 drv->ndisuio = INVALID_HANDLE_VALUE; 2699 return -1; 2700 } 2701 #endif /* _WIN32_WCE */ 2702 2703 return 0; 2704 #else /* CONFIG_USE_NDISUIO */ 2705 return 0; 2706 #endif /* CONFIG_USE_NDISUIO */ 2707 } 2708 2709 2710 static int wpa_driver_ndis_adapter_open(struct wpa_driver_ndis_data *drv) 2711 { 2712 #ifdef CONFIG_USE_NDISUIO 2713 DWORD written; 2714 #define MAX_NDIS_DEVICE_NAME_LEN 256 2715 WCHAR ifname[MAX_NDIS_DEVICE_NAME_LEN]; 2716 size_t len, i, pos; 2717 const char *prefix = "\\DEVICE\\"; 2718 2719 #ifdef _WIN32_WCE 2720 pos = 0; 2721 #else /* _WIN32_WCE */ 2722 pos = 8; 2723 #endif /* _WIN32_WCE */ 2724 len = pos + os_strlen(drv->ifname); 2725 if (len >= MAX_NDIS_DEVICE_NAME_LEN) 2726 return -1; 2727 for (i = 0; i < pos; i++) 2728 ifname[i] = (WCHAR) prefix[i]; 2729 for (i = pos; i < len; i++) 2730 ifname[i] = (WCHAR) drv->ifname[i - pos]; 2731 ifname[i] = L'\0'; 2732 2733 if (!DeviceIoControl(drv->ndisuio, IOCTL_NDISUIO_OPEN_DEVICE, 2734 ifname, len * sizeof(WCHAR), NULL, 0, &written, 2735 NULL)) { 2736 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_OPEN_DEVICE " 2737 "failed: %d", (int) GetLastError()); 2738 wpa_hexdump_ascii(MSG_DEBUG, "NDIS: ifname", 2739 (const u8 *) ifname, len * sizeof(WCHAR)); 2740 CloseHandle(drv->ndisuio); 2741 drv->ndisuio = INVALID_HANDLE_VALUE; 2742 return -1; 2743 } 2744 2745 wpa_printf(MSG_DEBUG, "NDIS: Opened NDISUIO device successfully"); 2746 2747 return 0; 2748 #else /* CONFIG_USE_NDISUIO */ 2749 char ifname[128]; 2750 os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", drv->ifname); 2751 drv->adapter = PacketOpenAdapter(ifname); 2752 if (drv->adapter == NULL) { 2753 wpa_printf(MSG_DEBUG, "NDIS: PacketOpenAdapter failed for " 2754 "'%s'", ifname); 2755 return -1; 2756 } 2757 return 0; 2758 #endif /* CONFIG_USE_NDISUIO */ 2759 } 2760 2761 2762 static void wpa_driver_ndis_adapter_close(struct wpa_driver_ndis_data *drv) 2763 { 2764 #ifdef CONFIG_USE_NDISUIO 2765 driver_ndis_ndisuio_handle = INVALID_HANDLE_VALUE; 2766 if (drv->ndisuio != INVALID_HANDLE_VALUE) 2767 CloseHandle(drv->ndisuio); 2768 #else /* CONFIG_USE_NDISUIO */ 2769 if (drv->adapter) 2770 PacketCloseAdapter(drv->adapter); 2771 #endif /* CONFIG_USE_NDISUIO */ 2772 } 2773 2774 2775 static int ndis_add_multicast(struct wpa_driver_ndis_data *drv) 2776 { 2777 if (ndis_set_oid(drv, OID_802_3_MULTICAST_LIST, 2778 (const char *) pae_group_addr, ETH_ALEN) < 0) { 2779 wpa_printf(MSG_DEBUG, "NDIS: Failed to add PAE group address " 2780 "to the multicast list"); 2781 return -1; 2782 } 2783 2784 return 0; 2785 } 2786 2787 2788 static void * wpa_driver_ndis_init(void *ctx, const char *ifname) 2789 { 2790 struct wpa_driver_ndis_data *drv; 2791 u32 mode; 2792 2793 drv = os_zalloc(sizeof(*drv)); 2794 if (drv == NULL) 2795 return NULL; 2796 drv->ctx = ctx; 2797 /* 2798 * Compatibility code to strip possible prefix from the GUID. Previous 2799 * versions include \Device\NPF_ prefix for all names, but the internal 2800 * interface name is now only the GUI. Both Packet32 and NDISUIO 2801 * prefixes are supported. 2802 */ 2803 if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0) 2804 ifname += 12; 2805 else if (os_strncmp(ifname, "\\DEVICE\\", 8) == 0) 2806 ifname += 8; 2807 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)); 2808 2809 if (wpa_driver_ndis_adapter_init(drv) < 0) { 2810 os_free(drv); 2811 return NULL; 2812 } 2813 2814 if (wpa_driver_ndis_get_names(drv) < 0) { 2815 wpa_driver_ndis_adapter_close(drv); 2816 os_free(drv); 2817 return NULL; 2818 } 2819 2820 wpa_driver_ndis_set_wzc(drv, 0); 2821 2822 if (wpa_driver_ndis_adapter_open(drv) < 0) { 2823 wpa_driver_ndis_adapter_close(drv); 2824 os_free(drv); 2825 return NULL; 2826 } 2827 2828 if (ndis_get_oid(drv, OID_802_3_CURRENT_ADDRESS, 2829 (char *) drv->own_addr, ETH_ALEN) < 0) { 2830 wpa_printf(MSG_DEBUG, "NDIS: Get OID_802_3_CURRENT_ADDRESS " 2831 "failed"); 2832 wpa_driver_ndis_adapter_close(drv); 2833 os_free(drv); 2834 return NULL; 2835 } 2836 wpa_driver_ndis_get_capability(drv); 2837 2838 /* Make sure that the driver does not have any obsolete PMKID entries. 2839 */ 2840 wpa_driver_ndis_flush_pmkid(drv); 2841 2842 /* 2843 * Disconnect to make sure that driver re-associates if it was 2844 * connected. 2845 */ 2846 wpa_driver_ndis_disconnect(drv); 2847 2848 eloop_register_timeout(1, 0, wpa_driver_ndis_poll_timeout, drv, NULL); 2849 2850 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED 2851 drv->events = ndis_events_init(&drv->events_pipe, &drv->event_avail, 2852 drv->ifname, drv->adapter_desc); 2853 if (drv->events == NULL) { 2854 wpa_driver_ndis_deinit(drv); 2855 return NULL; 2856 } 2857 eloop_register_event(drv->event_avail, sizeof(drv->event_avail), 2858 wpa_driver_ndis_event_pipe_cb, drv, NULL); 2859 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */ 2860 2861 #ifdef _WIN32_WCE 2862 if (ndisuio_notification_init(drv) < 0) { 2863 wpa_driver_ndis_deinit(drv); 2864 return NULL; 2865 } 2866 #endif /* _WIN32_WCE */ 2867 2868 /* Set mode here in case card was configured for ad-hoc mode 2869 * previously. */ 2870 mode = Ndis802_11Infrastructure; 2871 if (ndis_set_oid(drv, OID_802_11_INFRASTRUCTURE_MODE, 2872 (char *) &mode, sizeof(mode)) < 0) { 2873 char buf[8]; 2874 int res; 2875 wpa_printf(MSG_DEBUG, "NDIS: Failed to set " 2876 "OID_802_11_INFRASTRUCTURE_MODE (%d)", 2877 (int) mode); 2878 /* Try to continue anyway */ 2879 2880 res = ndis_get_oid(drv, OID_DOT11_CURRENT_OPERATION_MODE, buf, 2881 sizeof(buf)); 2882 if (res > 0) { 2883 wpa_printf(MSG_INFO, "NDIS: The driver seems to use " 2884 "Native 802.11 OIDs. These are not yet " 2885 "fully supported."); 2886 drv->native80211 = 1; 2887 } else if (!drv->has_capability || drv->capa.enc == 0) { 2888 /* 2889 * Note: This will also happen with NDIS 6 drivers with 2890 * Vista. 2891 */ 2892 wpa_printf(MSG_DEBUG, "NDIS: Driver did not provide " 2893 "any wireless capabilities - assume it is " 2894 "a wired interface"); 2895 drv->wired = 1; 2896 drv->capa.flags |= WPA_DRIVER_FLAGS_WIRED; 2897 drv->has_capability = 1; 2898 ndis_add_multicast(drv); 2899 } 2900 } 2901 2902 return drv; 2903 } 2904 2905 2906 static void wpa_driver_ndis_deinit(void *priv) 2907 { 2908 struct wpa_driver_ndis_data *drv = priv; 2909 2910 #ifdef CONFIG_NDIS_EVENTS_INTEGRATED 2911 if (drv->events) { 2912 eloop_unregister_event(drv->event_avail, 2913 sizeof(drv->event_avail)); 2914 ndis_events_deinit(drv->events); 2915 } 2916 #endif /* CONFIG_NDIS_EVENTS_INTEGRATED */ 2917 2918 #ifdef _WIN32_WCE 2919 ndisuio_notification_deinit(drv); 2920 #endif /* _WIN32_WCE */ 2921 2922 eloop_cancel_timeout(wpa_driver_ndis_scan_timeout, drv, drv->ctx); 2923 eloop_cancel_timeout(wpa_driver_ndis_poll_timeout, drv, NULL); 2924 wpa_driver_ndis_flush_pmkid(drv); 2925 wpa_driver_ndis_disconnect(drv); 2926 if (wpa_driver_ndis_radio_off(drv) < 0) { 2927 wpa_printf(MSG_DEBUG, "NDIS: failed to disassociate and turn " 2928 "radio off"); 2929 } 2930 2931 wpa_driver_ndis_adapter_close(drv); 2932 2933 if (drv->wzc_disabled) 2934 wpa_driver_ndis_set_wzc(drv, 1); 2935 2936 #ifdef _WIN32_WCE 2937 os_free(drv->adapter_name); 2938 #endif /* _WIN32_WCE */ 2939 os_free(drv->adapter_desc); 2940 os_free(drv); 2941 } 2942 2943 2944 static struct wpa_interface_info * 2945 wpa_driver_ndis_get_interfaces(void *global_priv) 2946 { 2947 struct wpa_interface_info *iface = NULL, *niface; 2948 2949 #ifdef CONFIG_USE_NDISUIO 2950 NDISUIO_QUERY_BINDING *b; 2951 size_t blen = sizeof(*b) + 1024; 2952 int i, error; 2953 DWORD written; 2954 char name[256], desc[256]; 2955 WCHAR *pos; 2956 size_t j, len; 2957 HANDLE ndisuio; 2958 2959 ndisuio = CreateFile(NDISUIO_DEVICE_NAME, 2960 GENERIC_READ | GENERIC_WRITE, 0, NULL, 2961 OPEN_EXISTING, 2962 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 2963 INVALID_HANDLE_VALUE); 2964 if (ndisuio == INVALID_HANDLE_VALUE) { 2965 wpa_printf(MSG_ERROR, "NDIS: Failed to open connection to " 2966 "NDISUIO: %d", (int) GetLastError()); 2967 return NULL; 2968 } 2969 2970 #ifndef _WIN32_WCE 2971 if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_BIND_WAIT, NULL, 0, 2972 NULL, 0, &written, NULL)) { 2973 wpa_printf(MSG_ERROR, "NDIS: IOCTL_NDISUIO_BIND_WAIT failed: " 2974 "%d", (int) GetLastError()); 2975 CloseHandle(ndisuio); 2976 return NULL; 2977 } 2978 #endif /* _WIN32_WCE */ 2979 2980 b = os_malloc(blen); 2981 if (b == NULL) { 2982 CloseHandle(ndisuio); 2983 return NULL; 2984 } 2985 2986 for (i = 0; ; i++) { 2987 os_memset(b, 0, blen); 2988 b->BindingIndex = i; 2989 if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_QUERY_BINDING, 2990 b, sizeof(NDISUIO_QUERY_BINDING), b, blen, 2991 &written, NULL)) { 2992 error = (int) GetLastError(); 2993 if (error == ERROR_NO_MORE_ITEMS) 2994 break; 2995 wpa_printf(MSG_DEBUG, "IOCTL_NDISUIO_QUERY_BINDING " 2996 "failed: %d", error); 2997 break; 2998 } 2999 3000 pos = (WCHAR *) ((char *) b + b->DeviceNameOffset); 3001 len = b->DeviceNameLength; 3002 if (len >= sizeof(name)) 3003 len = sizeof(name) - 1; 3004 for (j = 0; j < len; j++) 3005 name[j] = (char) pos[j]; 3006 name[len] = '\0'; 3007 3008 pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset); 3009 len = b->DeviceDescrLength; 3010 if (len >= sizeof(desc)) 3011 len = sizeof(desc) - 1; 3012 for (j = 0; j < len; j++) 3013 desc[j] = (char) pos[j]; 3014 desc[len] = '\0'; 3015 3016 wpa_printf(MSG_DEBUG, "NDIS: %d - %s - %s", i, name, desc); 3017 3018 niface = os_zalloc(sizeof(*niface)); 3019 if (niface == NULL) 3020 break; 3021 niface->drv_name = "ndis"; 3022 if (os_strncmp(name, "\\DEVICE\\", 8) == 0) 3023 niface->ifname = os_strdup(name + 8); 3024 else 3025 niface->ifname = os_strdup(name); 3026 if (niface->ifname == NULL) { 3027 os_free(niface); 3028 break; 3029 } 3030 niface->desc = os_strdup(desc); 3031 niface->next = iface; 3032 iface = niface; 3033 } 3034 3035 os_free(b); 3036 CloseHandle(ndisuio); 3037 #else /* CONFIG_USE_NDISUIO */ 3038 PTSTR _names; 3039 char *names, *pos, *pos2; 3040 ULONG len; 3041 BOOLEAN res; 3042 char *name[MAX_ADAPTERS]; 3043 char *desc[MAX_ADAPTERS]; 3044 int num_name, num_desc, i; 3045 3046 wpa_printf(MSG_DEBUG, "NDIS: Packet.dll version: %s", 3047 PacketGetVersion()); 3048 3049 len = 8192; 3050 _names = os_zalloc(len); 3051 if (_names == NULL) 3052 return NULL; 3053 3054 res = PacketGetAdapterNames(_names, &len); 3055 if (!res && len > 8192) { 3056 os_free(_names); 3057 _names = os_zalloc(len); 3058 if (_names == NULL) 3059 return NULL; 3060 res = PacketGetAdapterNames(_names, &len); 3061 } 3062 3063 if (!res) { 3064 wpa_printf(MSG_ERROR, "NDIS: Failed to get adapter list " 3065 "(PacketGetAdapterNames)"); 3066 os_free(_names); 3067 return NULL; 3068 } 3069 3070 names = (char *) _names; 3071 if (names[0] && names[1] == '\0' && names[2] && names[3] == '\0') { 3072 wpa_printf(MSG_DEBUG, "NDIS: Looks like adapter names are in " 3073 "UNICODE"); 3074 /* Convert to ASCII */ 3075 pos2 = pos = names; 3076 while (pos2 < names + len) { 3077 if (pos2[0] == '\0' && pos2[1] == '\0' && 3078 pos2[2] == '\0' && pos2[3] == '\0') { 3079 pos2 += 4; 3080 break; 3081 } 3082 *pos++ = pos2[0]; 3083 pos2 += 2; 3084 } 3085 os_memcpy(pos + 2, names, pos - names); 3086 pos += 2; 3087 } else 3088 pos = names; 3089 3090 num_name = 0; 3091 while (pos < names + len) { 3092 name[num_name] = pos; 3093 while (*pos && pos < names + len) 3094 pos++; 3095 if (pos + 1 >= names + len) { 3096 os_free(names); 3097 return NULL; 3098 } 3099 pos++; 3100 num_name++; 3101 if (num_name >= MAX_ADAPTERS) { 3102 wpa_printf(MSG_DEBUG, "NDIS: Too many adapters"); 3103 os_free(names); 3104 return NULL; 3105 } 3106 if (*pos == '\0') { 3107 wpa_printf(MSG_DEBUG, "NDIS: %d adapter names found", 3108 num_name); 3109 pos++; 3110 break; 3111 } 3112 } 3113 3114 num_desc = 0; 3115 while (pos < names + len) { 3116 desc[num_desc] = pos; 3117 while (*pos && pos < names + len) 3118 pos++; 3119 if (pos + 1 >= names + len) { 3120 os_free(names); 3121 return NULL; 3122 } 3123 pos++; 3124 num_desc++; 3125 if (num_desc >= MAX_ADAPTERS) { 3126 wpa_printf(MSG_DEBUG, "NDIS: Too many adapter " 3127 "descriptions"); 3128 os_free(names); 3129 return NULL; 3130 } 3131 if (*pos == '\0') { 3132 wpa_printf(MSG_DEBUG, "NDIS: %d adapter descriptions " 3133 "found", num_name); 3134 pos++; 3135 break; 3136 } 3137 } 3138 3139 /* 3140 * Windows 98 with Packet.dll 3.0 alpha3 does not include adapter 3141 * descriptions. Fill in dummy descriptors to work around this. 3142 */ 3143 while (num_desc < num_name) 3144 desc[num_desc++] = "dummy description"; 3145 3146 if (num_name != num_desc) { 3147 wpa_printf(MSG_DEBUG, "NDIS: mismatch in adapter name and " 3148 "description counts (%d != %d)", 3149 num_name, num_desc); 3150 os_free(names); 3151 return NULL; 3152 } 3153 3154 for (i = 0; i < num_name; i++) { 3155 niface = os_zalloc(sizeof(*niface)); 3156 if (niface == NULL) 3157 break; 3158 niface->drv_name = "ndis"; 3159 if (os_strncmp(name[i], "\\Device\\NPF_", 12) == 0) 3160 niface->ifname = os_strdup(name[i] + 12); 3161 else 3162 niface->ifname = os_strdup(name[i]); 3163 if (niface->ifname == NULL) { 3164 os_free(niface); 3165 break; 3166 } 3167 niface->desc = os_strdup(desc[i]); 3168 niface->next = iface; 3169 iface = niface; 3170 } 3171 3172 #endif /* CONFIG_USE_NDISUIO */ 3173 3174 return iface; 3175 } 3176 3177 3178 static const char *ndis_drv_name = "ndis"; 3179 static const char *ndis_drv_desc = "Windows NDIS driver"; 3180 3181 struct wpa_driver_ops wpa_driver_ndis_ops; 3182 3183 void driver_ndis_init_ops(void) 3184 { 3185 os_memset(&wpa_driver_ndis_ops, 0, sizeof(wpa_driver_ndis_ops)); 3186 wpa_driver_ndis_ops.name = ndis_drv_name; 3187 wpa_driver_ndis_ops.desc = ndis_drv_desc; 3188 wpa_driver_ndis_ops.get_bssid = wpa_driver_ndis_get_bssid; 3189 wpa_driver_ndis_ops.get_ssid = wpa_driver_ndis_get_ssid; 3190 wpa_driver_ndis_ops.set_key = wpa_driver_ndis_set_key; 3191 wpa_driver_ndis_ops.init = wpa_driver_ndis_init; 3192 wpa_driver_ndis_ops.deinit = wpa_driver_ndis_deinit; 3193 wpa_driver_ndis_ops.deauthenticate = wpa_driver_ndis_deauthenticate; 3194 wpa_driver_ndis_ops.associate = wpa_driver_ndis_associate; 3195 wpa_driver_ndis_ops.add_pmkid = wpa_driver_ndis_add_pmkid; 3196 wpa_driver_ndis_ops.remove_pmkid = wpa_driver_ndis_remove_pmkid; 3197 wpa_driver_ndis_ops.flush_pmkid = wpa_driver_ndis_flush_pmkid; 3198 wpa_driver_ndis_ops.get_capa = wpa_driver_ndis_get_capa; 3199 wpa_driver_ndis_ops.poll = wpa_driver_ndis_poll; 3200 wpa_driver_ndis_ops.get_ifname = wpa_driver_ndis_get_ifname; 3201 wpa_driver_ndis_ops.get_mac_addr = wpa_driver_ndis_get_mac_addr; 3202 wpa_driver_ndis_ops.get_scan_results2 = 3203 wpa_driver_ndis_get_scan_results; 3204 wpa_driver_ndis_ops.get_interfaces = wpa_driver_ndis_get_interfaces; 3205 wpa_driver_ndis_ops.scan2 = wpa_driver_ndis_scan; 3206 } 3207