scan.c revision 9bce59c7fef20e34a05f04d1e33a4076083dca0c
1/* 2 * WPA Supplicant - Scanning 3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "utils/includes.h" 10 11#include "utils/common.h" 12#include "utils/eloop.h" 13#include "common/ieee802_11_defs.h" 14#include "config.h" 15#include "wpa_supplicant_i.h" 16#include "driver_i.h" 17#include "wps_supplicant.h" 18#include "p2p_supplicant.h" 19#include "p2p/p2p.h" 20#include "hs20_supplicant.h" 21#include "notify.h" 22#include "bss.h" 23#include "scan.h" 24 25 26static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s) 27{ 28 struct wpa_ssid *ssid; 29 union wpa_event_data data; 30 31 ssid = wpa_supplicant_get_ssid(wpa_s); 32 if (ssid == NULL) 33 return; 34 35 if (wpa_s->current_ssid == NULL) { 36 wpa_s->current_ssid = ssid; 37 if (wpa_s->current_ssid != NULL) 38 wpas_notify_network_changed(wpa_s); 39 } 40 wpa_supplicant_initiate_eapol(wpa_s); 41 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured " 42 "network - generating associated event"); 43 os_memset(&data, 0, sizeof(data)); 44 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data); 45} 46 47 48#ifdef CONFIG_WPS 49static int wpas_wps_in_use(struct wpa_supplicant *wpa_s, 50 enum wps_request_type *req_type) 51{ 52 struct wpa_ssid *ssid; 53 int wps = 0; 54 55 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { 56 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS)) 57 continue; 58 59 wps = 1; 60 *req_type = wpas_wps_get_req_type(ssid); 61 if (!ssid->eap.phase1) 62 continue; 63 64 if (os_strstr(ssid->eap.phase1, "pbc=1")) 65 return 2; 66 } 67 68#ifdef CONFIG_P2P 69 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p) { 70 wpa_s->wps->dev.p2p = 1; 71 if (!wps) { 72 wps = 1; 73 *req_type = WPS_REQ_ENROLLEE_INFO; 74 } 75 } 76#endif /* CONFIG_P2P */ 77 78 return wps; 79} 80#endif /* CONFIG_WPS */ 81 82 83int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s) 84{ 85 struct wpa_ssid *ssid = wpa_s->conf->ssid; 86 int count = 0; 87 while (ssid) { 88 if (!wpas_network_disabled(wpa_s, ssid)) 89 count++; 90 ssid = ssid->next; 91 } 92 if (wpa_s->conf->cred && wpa_s->conf->interworking && 93 wpa_s->conf->auto_interworking) 94 count++; 95 return count; 96} 97 98 99static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s, 100 struct wpa_ssid *ssid) 101{ 102 while (ssid) { 103 if (!wpas_network_disabled(wpa_s, ssid)) 104 break; 105 ssid = ssid->next; 106 } 107 108 /* ap_scan=2 mode - try to associate with each SSID. */ 109 if (ssid == NULL) { 110 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached " 111 "end of scan list - go back to beginning"); 112 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; 113 wpa_supplicant_req_scan(wpa_s, 0, 0); 114 return; 115 } 116 if (ssid->next) { 117 /* Continue from the next SSID on the next attempt. */ 118 wpa_s->prev_scan_ssid = ssid; 119 } else { 120 /* Start from the beginning of the SSID list. */ 121 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; 122 } 123 wpa_supplicant_associate(wpa_s, NULL, ssid); 124} 125 126 127static int int_array_len(const int *a) 128{ 129 int i; 130 for (i = 0; a && a[i]; i++) 131 ; 132 return i; 133} 134 135 136static void int_array_concat(int **res, const int *a) 137{ 138 int reslen, alen, i; 139 int *n; 140 141 reslen = int_array_len(*res); 142 alen = int_array_len(a); 143 144 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int)); 145 if (n == NULL) { 146 os_free(*res); 147 *res = NULL; 148 return; 149 } 150 for (i = 0; i <= alen; i++) 151 n[reslen + i] = a[i]; 152 *res = n; 153} 154 155 156static int freq_cmp(const void *a, const void *b) 157{ 158 int _a = *(int *) a; 159 int _b = *(int *) b; 160 161 if (_a == 0) 162 return 1; 163 if (_b == 0) 164 return -1; 165 return _a - _b; 166} 167 168 169static void int_array_sort_unique(int *a) 170{ 171 int alen; 172 int i, j; 173 174 if (a == NULL) 175 return; 176 177 alen = int_array_len(a); 178 qsort(a, alen, sizeof(int), freq_cmp); 179 180 i = 0; 181 j = 1; 182 while (a[i] && a[j]) { 183 if (a[i] == a[j]) { 184 j++; 185 continue; 186 } 187 a[++i] = a[j++]; 188 } 189 if (a[i]) 190 i++; 191 a[i] = 0; 192} 193 194 195int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s, 196 struct wpa_driver_scan_params *params) 197{ 198 int ret; 199 200 wpa_supplicant_notify_scanning(wpa_s, 1); 201 202 ret = wpa_drv_scan(wpa_s, params); 203 if (ret) { 204 wpa_supplicant_notify_scanning(wpa_s, 0); 205 wpas_notify_scan_done(wpa_s, 0); 206 } else { 207 wpa_s->scan_runs++; 208 wpa_s->normal_scans++; 209 } 210 211 return ret; 212} 213 214 215static void 216wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx) 217{ 218 struct wpa_supplicant *wpa_s = eloop_ctx; 219 220 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan"); 221 222 if (wpa_supplicant_req_sched_scan(wpa_s)) 223 wpa_supplicant_req_scan(wpa_s, 0, 0); 224} 225 226 227static void 228wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx) 229{ 230 struct wpa_supplicant *wpa_s = eloop_ctx; 231 232 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it"); 233 234 wpa_s->sched_scan_timed_out = 1; 235 wpa_supplicant_cancel_sched_scan(wpa_s); 236} 237 238 239static int 240wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s, 241 struct wpa_driver_scan_params *params, 242 int interval) 243{ 244 int ret; 245#ifndef ANDROID_P2P 246 wpa_supplicant_notify_scanning(wpa_s, 1); 247#endif 248 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000); 249 if (ret) 250 wpa_supplicant_notify_scanning(wpa_s, 0); 251 else { 252 wpa_s->sched_scanning = 1; 253#ifdef ANDROID_P2P 254 wpa_supplicant_notify_scanning(wpa_s, 1); 255#endif 256 } 257 258 return ret; 259} 260 261 262static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s) 263{ 264 int ret; 265 266 ret = wpa_drv_stop_sched_scan(wpa_s); 267 if (ret) { 268 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!"); 269 /* TODO: what to do if stopping fails? */ 270 return -1; 271 } 272 273 return ret; 274} 275 276 277static struct wpa_driver_scan_filter * 278wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids) 279{ 280 struct wpa_driver_scan_filter *ssids; 281 struct wpa_ssid *ssid; 282 size_t count; 283 284 *num_ssids = 0; 285 if (!conf->filter_ssids) 286 return NULL; 287 288 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) { 289 if (ssid->ssid && ssid->ssid_len) 290 count++; 291 } 292 if (count == 0) 293 return NULL; 294 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter)); 295 if (ssids == NULL) 296 return NULL; 297 298 for (ssid = conf->ssid; ssid; ssid = ssid->next) { 299 if (!ssid->ssid || !ssid->ssid_len) 300 continue; 301 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len); 302 ssids[*num_ssids].ssid_len = ssid->ssid_len; 303 (*num_ssids)++; 304 } 305 306 return ssids; 307} 308 309 310static void wpa_supplicant_optimize_freqs( 311 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params) 312{ 313#ifdef CONFIG_P2P 314 if (params->freqs == NULL && wpa_s->p2p_in_provisioning && 315 wpa_s->go_params) { 316 /* Optimize provisioning state scan based on GO information */ 317 if (wpa_s->p2p_in_provisioning < 5 && 318 wpa_s->go_params->freq > 0) { 319 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO " 320 "preferred frequency %d MHz", 321 wpa_s->go_params->freq); 322 params->freqs = os_zalloc(2 * sizeof(int)); 323 if (params->freqs) 324 params->freqs[0] = wpa_s->go_params->freq; 325 } else if (wpa_s->p2p_in_provisioning < 8 && 326 wpa_s->go_params->freq_list[0]) { 327 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common " 328 "channels"); 329 int_array_concat(¶ms->freqs, 330 wpa_s->go_params->freq_list); 331 if (params->freqs) 332 int_array_sort_unique(params->freqs); 333 } 334 wpa_s->p2p_in_provisioning++; 335 } 336#endif /* CONFIG_P2P */ 337 338#ifdef CONFIG_WPS 339 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) { 340 /* 341 * Optimize post-provisioning scan based on channel used 342 * during provisioning. 343 */ 344 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz " 345 "that was used during provisioning", wpa_s->wps_freq); 346 params->freqs = os_zalloc(2 * sizeof(int)); 347 if (params->freqs) 348 params->freqs[0] = wpa_s->wps_freq; 349 wpa_s->after_wps--; 350 } 351 352 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq) 353 { 354 /* Optimize provisioning scan based on already known channel */ 355 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz", 356 wpa_s->wps_freq); 357 params->freqs = os_zalloc(2 * sizeof(int)); 358 if (params->freqs) 359 params->freqs[0] = wpa_s->wps_freq; 360 wpa_s->known_wps_freq = 0; /* only do this once */ 361 } 362#endif /* CONFIG_WPS */ 363} 364 365 366#ifdef CONFIG_INTERWORKING 367static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s, 368 struct wpabuf *buf) 369{ 370 if (wpa_s->conf->interworking == 0) 371 return; 372 373 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB); 374 wpabuf_put_u8(buf, 4); 375 wpabuf_put_u8(buf, 0x00); 376 wpabuf_put_u8(buf, 0x00); 377 wpabuf_put_u8(buf, 0x00); 378 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */ 379 380 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING); 381 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 : 382 1 + ETH_ALEN); 383 wpabuf_put_u8(buf, wpa_s->conf->access_network_type); 384 /* No Venue Info */ 385 if (!is_zero_ether_addr(wpa_s->conf->hessid)) 386 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN); 387} 388#endif /* CONFIG_INTERWORKING */ 389 390 391static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s) 392{ 393 struct wpabuf *extra_ie = NULL; 394#ifdef CONFIG_WPS 395 int wps = 0; 396 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO; 397#endif /* CONFIG_WPS */ 398 399#ifdef CONFIG_INTERWORKING 400 if (wpa_s->conf->interworking && 401 wpabuf_resize(&extra_ie, 100) == 0) 402 wpas_add_interworking_elements(wpa_s, extra_ie); 403#endif /* CONFIG_INTERWORKING */ 404 405#ifdef CONFIG_WPS 406 wps = wpas_wps_in_use(wpa_s, &req_type); 407 408 if (wps) { 409 struct wpabuf *wps_ie; 410 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON : 411 DEV_PW_DEFAULT, 412 &wpa_s->wps->dev, 413 wpa_s->wps->uuid, req_type, 414 0, NULL); 415 if (wps_ie) { 416 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0) 417 wpabuf_put_buf(extra_ie, wps_ie); 418 wpabuf_free(wps_ie); 419 } 420 } 421 422#ifdef CONFIG_P2P 423 if (wps) { 424 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); 425 if (wpabuf_resize(&extra_ie, ielen) == 0) 426 wpas_p2p_scan_ie(wpa_s, extra_ie); 427 } 428#endif /* CONFIG_P2P */ 429 430#endif /* CONFIG_WPS */ 431 432 return extra_ie; 433} 434 435 436static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx) 437{ 438 struct wpa_supplicant *wpa_s = eloop_ctx; 439 struct wpa_ssid *ssid; 440 int scan_req = 0, ret; 441 struct wpabuf *extra_ie = NULL; 442 struct wpa_driver_scan_params params; 443 struct wpa_driver_scan_params *scan_params; 444 size_t max_ssids; 445 enum wpa_states prev_state; 446 447 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) { 448 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled"); 449 return; 450 } 451 452 if (wpa_s->disconnected && !wpa_s->scan_req) { 453 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED); 454 return; 455 } 456 457 if (!wpa_supplicant_enabled_networks(wpa_s) && 458 !wpa_s->scan_req) { 459 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan"); 460 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE); 461 return; 462 } 463 464 if (wpa_s->conf->ap_scan != 0 && 465 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) { 466 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - " 467 "overriding ap_scan configuration"); 468 wpa_s->conf->ap_scan = 0; 469 wpas_notify_ap_scan_changed(wpa_s); 470 } 471 472 if (wpa_s->conf->ap_scan == 0) { 473 wpa_supplicant_gen_assoc_event(wpa_s); 474 return; 475 } 476 477#ifdef CONFIG_P2P 478 if (wpas_p2p_in_progress(wpa_s)) { 479 if (wpa_s->sta_scan_pending && 480 wpas_p2p_in_progress(wpa_s) == 2 && 481 wpa_s->p2p_cb_on_scan_complete) { 482 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station " 483 "mode scan during P2P search"); 484 } else { 485 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan " 486 "while P2P operation is in progress"); 487 wpa_s->sta_scan_pending = 1; 488 wpa_supplicant_req_scan(wpa_s, 5, 0); 489 return; 490 } 491 } 492#endif /* CONFIG_P2P */ 493 494 if (wpa_s->conf->ap_scan == 2) 495 max_ssids = 1; 496 else { 497 max_ssids = wpa_s->max_scan_ssids; 498 if (max_ssids > WPAS_MAX_SCAN_SSIDS) 499 max_ssids = WPAS_MAX_SCAN_SSIDS; 500 } 501 502 scan_req = wpa_s->scan_req; 503 wpa_s->scan_req = 0; 504 505 os_memset(¶ms, 0, sizeof(params)); 506 507 prev_state = wpa_s->wpa_state; 508 if (wpa_s->wpa_state == WPA_DISCONNECTED || 509 wpa_s->wpa_state == WPA_INACTIVE) 510 wpa_supplicant_set_state(wpa_s, WPA_SCANNING); 511 512 /* 513 * If autoscan has set its own scanning parameters 514 */ 515 if (wpa_s->autoscan_params != NULL) { 516 scan_params = wpa_s->autoscan_params; 517 goto scan; 518 } 519 520 if (scan_req != 2 && wpa_s->connect_without_scan) { 521 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { 522 if (ssid == wpa_s->connect_without_scan) 523 break; 524 } 525 wpa_s->connect_without_scan = NULL; 526 if (ssid) { 527 wpa_printf(MSG_DEBUG, "Start a pre-selected network " 528 "without scan step"); 529 wpa_supplicant_associate(wpa_s, NULL, ssid); 530 return; 531 } 532 } 533 534#ifdef CONFIG_P2P 535 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) && 536 wpa_s->go_params) { 537 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during " 538 "P2P group formation"); 539 params.ssids[0].ssid = wpa_s->go_params->ssid; 540 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len; 541 params.num_ssids = 1; 542 goto ssid_list_set; 543 } 544#endif /* CONFIG_P2P */ 545 546 /* Find the starting point from which to continue scanning */ 547 ssid = wpa_s->conf->ssid; 548 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) { 549 while (ssid) { 550 if (ssid == wpa_s->prev_scan_ssid) { 551 ssid = ssid->next; 552 break; 553 } 554 ssid = ssid->next; 555 } 556 } 557 558 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) { 559 wpa_s->connect_without_scan = NULL; 560 wpa_s->prev_scan_wildcard = 0; 561 wpa_supplicant_assoc_try(wpa_s, ssid); 562 return; 563#ifndef ANDROID 564 } else if (wpa_s->conf->ap_scan == 2) { 565 /* 566 * User-initiated scan request in ap_scan == 2; scan with 567 * wildcard SSID. 568 */ 569 ssid = NULL; 570#endif 571 } else { 572 struct wpa_ssid *start = ssid, *tssid; 573 int freqs_set = 0; 574 if (ssid == NULL && max_ssids > 1) 575 ssid = wpa_s->conf->ssid; 576 while (ssid) { 577 if (!wpas_network_disabled(wpa_s, ssid) && 578 ssid->scan_ssid) { 579 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID", 580 ssid->ssid, ssid->ssid_len); 581 params.ssids[params.num_ssids].ssid = 582 ssid->ssid; 583 params.ssids[params.num_ssids].ssid_len = 584 ssid->ssid_len; 585 params.num_ssids++; 586 if (params.num_ssids + 1 >= max_ssids) 587 break; 588 } 589 ssid = ssid->next; 590 if (ssid == start) 591 break; 592 if (ssid == NULL && max_ssids > 1 && 593 start != wpa_s->conf->ssid) 594 ssid = wpa_s->conf->ssid; 595 } 596 597 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) { 598 if (wpas_network_disabled(wpa_s, tssid)) 599 continue; 600 if ((params.freqs || !freqs_set) && tssid->scan_freq) { 601 int_array_concat(¶ms.freqs, 602 tssid->scan_freq); 603 } else { 604 os_free(params.freqs); 605 params.freqs = NULL; 606 } 607 freqs_set = 1; 608 } 609 int_array_sort_unique(params.freqs); 610 } 611 612 if (ssid && max_ssids == 1) { 613 /* 614 * If the driver is limited to 1 SSID at a time interleave 615 * wildcard SSID scans with specific SSID scans to avoid 616 * waiting a long time for a wildcard scan. 617 */ 618 if (!wpa_s->prev_scan_wildcard) { 619 params.ssids[0].ssid = NULL; 620 params.ssids[0].ssid_len = 0; 621 wpa_s->prev_scan_wildcard = 1; 622 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for " 623 "wildcard SSID (Interleave with specific)"); 624 } else { 625 wpa_s->prev_scan_ssid = ssid; 626 wpa_s->prev_scan_wildcard = 0; 627 wpa_dbg(wpa_s, MSG_DEBUG, 628 "Starting AP scan for specific SSID: %s", 629 wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); 630 } 631 } else if (ssid) { 632 /* max_ssids > 1 */ 633 634 wpa_s->prev_scan_ssid = ssid; 635 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in " 636 "the scan request"); 637 params.num_ssids++; 638 } else { 639 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; 640 params.num_ssids++; 641 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard " 642 "SSID"); 643 } 644#ifdef CONFIG_P2P 645ssid_list_set: 646#endif /* CONFIG_P2P */ 647 648 wpa_supplicant_optimize_freqs(wpa_s, ¶ms); 649 extra_ie = wpa_supplicant_extra_ies(wpa_s); 650 651#ifdef CONFIG_HS20 652 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0) 653 wpas_hs20_add_indication(extra_ie); 654#endif /* CONFIG_HS20 */ 655 656 if (params.freqs == NULL && wpa_s->next_scan_freqs) { 657 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously " 658 "generated frequency list"); 659 params.freqs = wpa_s->next_scan_freqs; 660 } else 661 os_free(wpa_s->next_scan_freqs); 662 wpa_s->next_scan_freqs = NULL; 663 664 params.filter_ssids = wpa_supplicant_build_filter_ssids( 665 wpa_s->conf, ¶ms.num_filter_ssids); 666 if (extra_ie) { 667 params.extra_ies = wpabuf_head(extra_ie); 668 params.extra_ies_len = wpabuf_len(extra_ie); 669 } 670 671#ifdef CONFIG_P2P 672 if (wpa_s->p2p_in_provisioning || 673 (wpa_s->show_group_started && wpa_s->go_params)) { 674 /* 675 * The interface may not yet be in P2P mode, so we have to 676 * explicitly request P2P probe to disable CCK rates. 677 */ 678 params.p2p_probe = 1; 679 } 680#endif /* CONFIG_P2P */ 681 682 scan_params = ¶ms; 683 684scan: 685 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params); 686 687 wpabuf_free(extra_ie); 688 os_free(params.freqs); 689 os_free(params.filter_ssids); 690 691 if (ret) { 692 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan"); 693#ifdef ANDROID_P2P 694 /* Restore back the wpa_s->scan_req if we failed the scan because of any reason */ 695 wpa_msg(wpa_s, MSG_DEBUG, "Restoring back the wpa_s->scan_req " 696 "to the original value %d", scan_req); 697 wpa_s->scan_req = scan_req; 698#endif 699 if (prev_state != wpa_s->wpa_state) 700 wpa_supplicant_set_state(wpa_s, prev_state); 701 wpa_supplicant_req_scan(wpa_s, 1, 0); 702 } 703} 704 705 706/** 707 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points 708 * @wpa_s: Pointer to wpa_supplicant data 709 * @sec: Number of seconds after which to scan 710 * @usec: Number of microseconds after which to scan 711 * 712 * This function is used to schedule a scan for neighboring access points after 713 * the specified time. 714 */ 715void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec) 716{ 717#ifndef ANDROID 718 /* If there's at least one network that should be specifically scanned 719 * then don't cancel the scan and reschedule. Some drivers do 720 * background scanning which generates frequent scan results, and that 721 * causes the specific SSID scan to get continually pushed back and 722 * never happen, which causes hidden APs to never get probe-scanned. 723 */ 724 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) && 725 wpa_s->conf->ap_scan == 1) { 726 struct wpa_ssid *ssid = wpa_s->conf->ssid; 727 728 while (ssid) { 729 if (!wpas_network_disabled(wpa_s, ssid) && 730 ssid->scan_ssid) 731 break; 732 ssid = ssid->next; 733 } 734 if (ssid) { 735 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to " 736 "ensure that specific SSID scans occur"); 737 return; 738 } 739 } 740#endif 741 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec", 742 sec, usec); 743 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL); 744 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL); 745} 746 747 748/** 749 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan 750 * @wpa_s: Pointer to wpa_supplicant data 751 * @sec: Number of seconds after which to scan 752 * @usec: Number of microseconds after which to scan 753 * 754 * This function is used to schedule periodic scans for neighboring 755 * access points after the specified time. 756 */ 757int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s, 758 int sec, int usec) 759{ 760 if (!wpa_s->sched_scan_supported) 761 return -1; 762 763 eloop_register_timeout(sec, usec, 764 wpa_supplicant_delayed_sched_scan_timeout, 765 wpa_s, NULL); 766 767 return 0; 768} 769 770 771/** 772 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan 773 * @wpa_s: Pointer to wpa_supplicant data 774 * 775 * This function is used to schedule periodic scans for neighboring 776 * access points repeating the scan continuously. 777 */ 778int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s) 779{ 780 struct wpa_driver_scan_params params; 781 struct wpa_driver_scan_params *scan_params; 782 enum wpa_states prev_state; 783 struct wpa_ssid *ssid = NULL; 784 struct wpabuf *extra_ie = NULL; 785 int ret; 786 unsigned int max_sched_scan_ssids; 787 int wildcard = 0; 788 int need_ssids; 789 790 if (!wpa_s->sched_scan_supported) 791 return -1; 792 793 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS) 794 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS; 795 else 796 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids; 797 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload) 798 return -1; 799 800 if (wpa_s->sched_scanning) { 801 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning"); 802 return 0; 803 } 804 805 need_ssids = 0; 806 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { 807 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) { 808 /* Use wildcard SSID to find this network */ 809 wildcard = 1; 810 } else if (!wpas_network_disabled(wpa_s, ssid) && 811 ssid->ssid_len) 812 need_ssids++; 813 814#ifdef CONFIG_WPS 815 if (!wpas_network_disabled(wpa_s, ssid) && 816 ssid->key_mgmt == WPA_KEY_MGMT_WPS) { 817 /* 818 * Normal scan is more reliable and faster for WPS 819 * operations and since these are for short periods of 820 * time, the benefit of trying to use sched_scan would 821 * be limited. 822 */ 823 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of " 824 "sched_scan for WPS"); 825 return -1; 826 } 827#endif /* CONFIG_WPS */ 828 } 829 if (wildcard) 830 need_ssids++; 831 832 if (wpa_s->normal_scans < 3 && 833 (need_ssids <= wpa_s->max_scan_ssids || 834 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) { 835 /* 836 * When normal scan can speed up operations, use that for the 837 * first operations before starting the sched_scan to allow 838 * user space sleep more. We do this only if the normal scan 839 * has functionality that is suitable for this or if the 840 * sched_scan does not have better support for multiple SSIDs. 841 */ 842 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of " 843 "sched_scan for initial scans (normal_scans=%d)", 844 wpa_s->normal_scans); 845 return -1; 846 } 847 848 os_memset(¶ms, 0, sizeof(params)); 849 850 /* If we can't allocate space for the filters, we just don't filter */ 851 params.filter_ssids = os_zalloc(wpa_s->max_match_sets * 852 sizeof(struct wpa_driver_scan_filter)); 853 854 prev_state = wpa_s->wpa_state; 855#ifndef ANDROID_P2P 856 if (wpa_s->wpa_state == WPA_DISCONNECTED || 857 wpa_s->wpa_state == WPA_INACTIVE) 858 wpa_supplicant_set_state(wpa_s, WPA_SCANNING); 859#endif 860 861 if (wpa_s->autoscan_params != NULL) { 862 scan_params = wpa_s->autoscan_params; 863 goto scan; 864 } 865 866 /* Find the starting point from which to continue scanning */ 867 ssid = wpa_s->conf->ssid; 868 if (wpa_s->prev_sched_ssid) { 869 while (ssid) { 870 if (ssid == wpa_s->prev_sched_ssid) { 871 ssid = ssid->next; 872 break; 873 } 874 ssid = ssid->next; 875 } 876 } 877 878 if (!ssid || !wpa_s->prev_sched_ssid) { 879 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list"); 880 881 if (wpa_s->sched_scan_interval == 0) 882 wpa_s->sched_scan_interval = 10; 883 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2; 884 wpa_s->first_sched_scan = 1; 885 ssid = wpa_s->conf->ssid; 886 wpa_s->prev_sched_ssid = ssid; 887 } 888 889 if (wildcard) { 890 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan"); 891 params.num_ssids++; 892 } 893 894 while (ssid) { 895 if (wpas_network_disabled(wpa_s, ssid)) 896 goto next; 897 898 if (params.num_filter_ssids < wpa_s->max_match_sets && 899 params.filter_ssids && ssid->ssid && ssid->ssid_len) { 900 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s", 901 wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); 902 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid, 903 ssid->ssid, ssid->ssid_len); 904 params.filter_ssids[params.num_filter_ssids].ssid_len = 905 ssid->ssid_len; 906 params.num_filter_ssids++; 907 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len) 908 { 909 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID " 910 "filter for sched_scan - drop filter"); 911 os_free(params.filter_ssids); 912 params.filter_ssids = NULL; 913 params.num_filter_ssids = 0; 914 } 915 916 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) { 917 if (params.num_ssids == max_sched_scan_ssids) 918 break; /* only room for broadcast SSID */ 919 wpa_dbg(wpa_s, MSG_DEBUG, 920 "add to active scan ssid: %s", 921 wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); 922 params.ssids[params.num_ssids].ssid = 923 ssid->ssid; 924 params.ssids[params.num_ssids].ssid_len = 925 ssid->ssid_len; 926 params.num_ssids++; 927 if (params.num_ssids >= max_sched_scan_ssids) { 928 wpa_s->prev_sched_ssid = ssid; 929 do { 930 ssid = ssid->next; 931 } while (ssid && 932 (wpas_network_disabled(wpa_s, ssid) || 933 !ssid->scan_ssid)); 934 break; 935 } 936 } 937 938 next: 939 wpa_s->prev_sched_ssid = ssid; 940 ssid = ssid->next; 941 } 942 943 if (params.num_filter_ssids == 0) { 944 os_free(params.filter_ssids); 945 params.filter_ssids = NULL; 946 } 947 948 extra_ie = wpa_supplicant_extra_ies(wpa_s); 949 if (extra_ie) { 950 params.extra_ies = wpabuf_head(extra_ie); 951 params.extra_ies_len = wpabuf_len(extra_ie); 952 } 953 954 scan_params = ¶ms; 955 956scan: 957 if (ssid || !wpa_s->first_sched_scan) { 958 wpa_dbg(wpa_s, MSG_DEBUG, 959 "Starting sched scan: interval %d timeout %d", 960 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout); 961 } else { 962 wpa_dbg(wpa_s, MSG_DEBUG, 963 "Starting sched scan: interval %d (no timeout)", 964 wpa_s->sched_scan_interval); 965 } 966 967 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params, 968 wpa_s->sched_scan_interval); 969 wpabuf_free(extra_ie); 970 os_free(params.filter_ssids); 971 if (ret) { 972 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan"); 973 if (prev_state != wpa_s->wpa_state) 974 wpa_supplicant_set_state(wpa_s, prev_state); 975 return ret; 976 } 977 978 /* If we have more SSIDs to scan, add a timeout so we scan them too */ 979 if (ssid || !wpa_s->first_sched_scan) { 980 wpa_s->sched_scan_timed_out = 0; 981 eloop_register_timeout(wpa_s->sched_scan_timeout, 0, 982 wpa_supplicant_sched_scan_timeout, 983 wpa_s, NULL); 984 wpa_s->first_sched_scan = 0; 985 wpa_s->sched_scan_timeout /= 2; 986 wpa_s->sched_scan_interval *= 2; 987 } 988 989 return 0; 990} 991 992 993/** 994 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request 995 * @wpa_s: Pointer to wpa_supplicant data 996 * 997 * This function is used to cancel a scan request scheduled with 998 * wpa_supplicant_req_scan(). 999 */ 1000void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s) 1001{ 1002 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request"); 1003 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL); 1004#ifdef ANDROID 1005 wpa_supplicant_notify_scanning(wpa_s, 0); 1006#endif 1007} 1008 1009 1010/** 1011 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans 1012 * @wpa_s: Pointer to wpa_supplicant data 1013 * 1014 * This function is used to stop a periodic scheduled scan. 1015 */ 1016void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s) 1017{ 1018 if (!wpa_s->sched_scanning) 1019 return; 1020 1021 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan"); 1022 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL); 1023 wpa_supplicant_stop_sched_scan(wpa_s); 1024} 1025 1026 1027void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s, 1028 int scanning) 1029{ 1030 if (wpa_s->scanning != scanning) { 1031#ifdef ANDROID_P2P 1032 if(!wpa_s->sched_scanning) 1033 wpa_s->scanning = scanning; 1034#else 1035 wpa_s->scanning = scanning; 1036#endif 1037 wpas_notify_scanning(wpa_s); 1038 } 1039} 1040 1041 1042static int wpa_scan_get_max_rate(const struct wpa_scan_res *res) 1043{ 1044 int rate = 0; 1045 const u8 *ie; 1046 int i; 1047 1048 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES); 1049 for (i = 0; ie && i < ie[1]; i++) { 1050 if ((ie[i + 2] & 0x7f) > rate) 1051 rate = ie[i + 2] & 0x7f; 1052 } 1053 1054 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES); 1055 for (i = 0; ie && i < ie[1]; i++) { 1056 if ((ie[i + 2] & 0x7f) > rate) 1057 rate = ie[i + 2] & 0x7f; 1058 } 1059 1060 return rate; 1061} 1062 1063 1064const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie) 1065{ 1066 const u8 *end, *pos; 1067 1068 pos = (const u8 *) (res + 1); 1069 end = pos + res->ie_len; 1070 1071 while (pos + 1 < end) { 1072 if (pos + 2 + pos[1] > end) 1073 break; 1074 if (pos[0] == ie) 1075 return pos; 1076 pos += 2 + pos[1]; 1077 } 1078 1079 return NULL; 1080} 1081 1082 1083const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res, 1084 u32 vendor_type) 1085{ 1086 const u8 *end, *pos; 1087 1088 pos = (const u8 *) (res + 1); 1089 end = pos + res->ie_len; 1090 1091 while (pos + 1 < end) { 1092 if (pos + 2 + pos[1] > end) 1093 break; 1094 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && 1095 vendor_type == WPA_GET_BE32(&pos[2])) 1096 return pos; 1097 pos += 2 + pos[1]; 1098 } 1099 1100 return NULL; 1101} 1102 1103 1104struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res, 1105 u32 vendor_type) 1106{ 1107 struct wpabuf *buf; 1108 const u8 *end, *pos; 1109 1110 buf = wpabuf_alloc(res->ie_len); 1111 if (buf == NULL) 1112 return NULL; 1113 1114 pos = (const u8 *) (res + 1); 1115 end = pos + res->ie_len; 1116 1117 while (pos + 1 < end) { 1118 if (pos + 2 + pos[1] > end) 1119 break; 1120 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && 1121 vendor_type == WPA_GET_BE32(&pos[2])) 1122 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); 1123 pos += 2 + pos[1]; 1124 } 1125 1126 if (wpabuf_len(buf) == 0) { 1127 wpabuf_free(buf); 1128 buf = NULL; 1129 } 1130 1131 return buf; 1132} 1133 1134 1135struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon( 1136 const struct wpa_scan_res *res, u32 vendor_type) 1137{ 1138 struct wpabuf *buf; 1139 const u8 *end, *pos; 1140 1141 if (res->beacon_ie_len == 0) 1142 return NULL; 1143 buf = wpabuf_alloc(res->beacon_ie_len); 1144 if (buf == NULL) 1145 return NULL; 1146 1147 pos = (const u8 *) (res + 1); 1148 pos += res->ie_len; 1149 end = pos + res->beacon_ie_len; 1150 1151 while (pos + 1 < end) { 1152 if (pos + 2 + pos[1] > end) 1153 break; 1154 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && 1155 vendor_type == WPA_GET_BE32(&pos[2])) 1156 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); 1157 pos += 2 + pos[1]; 1158 } 1159 1160 if (wpabuf_len(buf) == 0) { 1161 wpabuf_free(buf); 1162 buf = NULL; 1163 } 1164 1165 return buf; 1166} 1167 1168 1169/* 1170 * Channels with a great SNR can operate at full rate. What is a great SNR? 1171 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general 1172 * rule of thumb is that any SNR above 20 is good." This one 1173 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23 1174 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a 1175 * conservative value. 1176 */ 1177#define GREAT_SNR 30 1178 1179/* Compare function for sorting scan results. Return >0 if @b is considered 1180 * better. */ 1181static int wpa_scan_result_compar(const void *a, const void *b) 1182{ 1183#define IS_5GHZ(n) (n > 4000) 1184#define MIN(a,b) a < b ? a : b 1185 struct wpa_scan_res **_wa = (void *) a; 1186 struct wpa_scan_res **_wb = (void *) b; 1187 struct wpa_scan_res *wa = *_wa; 1188 struct wpa_scan_res *wb = *_wb; 1189 int wpa_a, wpa_b, maxrate_a, maxrate_b; 1190 int snr_a, snr_b; 1191 1192 /* WPA/WPA2 support preferred */ 1193 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL || 1194 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL; 1195 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL || 1196 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL; 1197 1198 if (wpa_b && !wpa_a) 1199 return 1; 1200 if (!wpa_b && wpa_a) 1201 return -1; 1202 1203 /* privacy support preferred */ 1204 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 && 1205 (wb->caps & IEEE80211_CAP_PRIVACY)) 1206 return 1; 1207 if ((wa->caps & IEEE80211_CAP_PRIVACY) && 1208 (wb->caps & IEEE80211_CAP_PRIVACY) == 0) 1209 return -1; 1210 1211 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) && 1212 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) { 1213 snr_a = MIN(wa->level - wa->noise, GREAT_SNR); 1214 snr_b = MIN(wb->level - wb->noise, GREAT_SNR); 1215 } else { 1216 /* Not suitable information to calculate SNR, so use level */ 1217 snr_a = wa->level; 1218 snr_b = wb->level; 1219 } 1220 1221 /* best/max rate preferred if SNR close enough */ 1222 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) || 1223 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) { 1224 maxrate_a = wpa_scan_get_max_rate(wa); 1225 maxrate_b = wpa_scan_get_max_rate(wb); 1226 if (maxrate_a != maxrate_b) 1227 return maxrate_b - maxrate_a; 1228 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq)) 1229 return IS_5GHZ(wa->freq) ? -1 : 1; 1230 } 1231 1232 /* use freq for channel preference */ 1233 1234 /* all things being equal, use SNR; if SNRs are 1235 * identical, use quality values since some drivers may only report 1236 * that value and leave the signal level zero */ 1237 if (snr_b == snr_a) 1238 return wb->qual - wa->qual; 1239 return snr_b - snr_a; 1240#undef MIN 1241#undef IS_5GHZ 1242} 1243 1244 1245#ifdef CONFIG_WPS 1246/* Compare function for sorting scan results when searching a WPS AP for 1247 * provisioning. Return >0 if @b is considered better. */ 1248static int wpa_scan_result_wps_compar(const void *a, const void *b) 1249{ 1250 struct wpa_scan_res **_wa = (void *) a; 1251 struct wpa_scan_res **_wb = (void *) b; 1252 struct wpa_scan_res *wa = *_wa; 1253 struct wpa_scan_res *wb = *_wb; 1254 int uses_wps_a, uses_wps_b; 1255 struct wpabuf *wps_a, *wps_b; 1256 int res; 1257 1258 /* Optimization - check WPS IE existence before allocated memory and 1259 * doing full reassembly. */ 1260 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL; 1261 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL; 1262 if (uses_wps_a && !uses_wps_b) 1263 return -1; 1264 if (!uses_wps_a && uses_wps_b) 1265 return 1; 1266 1267 if (uses_wps_a && uses_wps_b) { 1268 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE); 1269 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE); 1270 res = wps_ap_priority_compar(wps_a, wps_b); 1271 wpabuf_free(wps_a); 1272 wpabuf_free(wps_b); 1273 if (res) 1274 return res; 1275 } 1276 1277 /* 1278 * Do not use current AP security policy as a sorting criteria during 1279 * WPS provisioning step since the AP may get reconfigured at the 1280 * completion of provisioning. 1281 */ 1282 1283 /* all things being equal, use signal level; if signal levels are 1284 * identical, use quality values since some drivers may only report 1285 * that value and leave the signal level zero */ 1286 if (wb->level == wa->level) 1287 return wb->qual - wa->qual; 1288 return wb->level - wa->level; 1289} 1290#endif /* CONFIG_WPS */ 1291 1292 1293static void dump_scan_res(struct wpa_scan_results *scan_res) 1294{ 1295#ifndef CONFIG_NO_STDOUT_DEBUG 1296 size_t i; 1297 1298 if (scan_res->res == NULL || scan_res->num == 0) 1299 return; 1300 1301 wpa_printf(MSG_EXCESSIVE, "Sorted scan results"); 1302 1303 for (i = 0; i < scan_res->num; i++) { 1304 struct wpa_scan_res *r = scan_res->res[i]; 1305 u8 *pos; 1306 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID)) 1307 == WPA_SCAN_LEVEL_DBM) { 1308 int snr = r->level - r->noise; 1309 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d " 1310 "noise=%d level=%d snr=%d%s flags=0x%x", 1311 MAC2STR(r->bssid), r->freq, r->qual, 1312 r->noise, r->level, snr, 1313 snr >= GREAT_SNR ? "*" : "", r->flags); 1314 } else { 1315 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d " 1316 "noise=%d level=%d flags=0x%x", 1317 MAC2STR(r->bssid), r->freq, r->qual, 1318 r->noise, r->level, r->flags); 1319 } 1320 pos = (u8 *) (r + 1); 1321 if (r->ie_len) 1322 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len); 1323 pos += r->ie_len; 1324 if (r->beacon_ie_len) 1325 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs", 1326 pos, r->beacon_ie_len); 1327 } 1328#endif /* CONFIG_NO_STDOUT_DEBUG */ 1329} 1330 1331 1332int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s, 1333 const u8 *bssid) 1334{ 1335 size_t i; 1336 1337 if (wpa_s->bssid_filter == NULL) 1338 return 1; 1339 1340 for (i = 0; i < wpa_s->bssid_filter_count; i++) { 1341 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid, 1342 ETH_ALEN) == 0) 1343 return 1; 1344 } 1345 1346 return 0; 1347} 1348 1349 1350static void filter_scan_res(struct wpa_supplicant *wpa_s, 1351 struct wpa_scan_results *res) 1352{ 1353 size_t i, j; 1354 1355 if (wpa_s->bssid_filter == NULL) 1356 return; 1357 1358 for (i = 0, j = 0; i < res->num; i++) { 1359 if (wpa_supplicant_filter_bssid_match(wpa_s, 1360 res->res[i]->bssid)) { 1361 res->res[j++] = res->res[i]; 1362 } else { 1363 os_free(res->res[i]); 1364 res->res[i] = NULL; 1365 } 1366 } 1367 1368 if (res->num != j) { 1369 wpa_printf(MSG_DEBUG, "Filtered out %d scan results", 1370 (int) (res->num - j)); 1371 res->num = j; 1372 } 1373} 1374 1375 1376/** 1377 * wpa_supplicant_get_scan_results - Get scan results 1378 * @wpa_s: Pointer to wpa_supplicant data 1379 * @info: Information about what was scanned or %NULL if not available 1380 * @new_scan: Whether a new scan was performed 1381 * Returns: Scan results, %NULL on failure 1382 * 1383 * This function request the current scan results from the driver and updates 1384 * the local BSS list wpa_s->bss. The caller is responsible for freeing the 1385 * results with wpa_scan_results_free(). 1386 */ 1387struct wpa_scan_results * 1388wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s, 1389 struct scan_info *info, int new_scan) 1390{ 1391 struct wpa_scan_results *scan_res; 1392 size_t i; 1393 int (*compar)(const void *, const void *) = wpa_scan_result_compar; 1394 1395 scan_res = wpa_drv_get_scan_results2(wpa_s); 1396 if (scan_res == NULL) { 1397 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results"); 1398 return NULL; 1399 } 1400 filter_scan_res(wpa_s, scan_res); 1401 1402#ifdef CONFIG_WPS 1403 if (wpas_wps_in_progress(wpa_s)) { 1404 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS " 1405 "provisioning rules"); 1406 compar = wpa_scan_result_wps_compar; 1407 } 1408#endif /* CONFIG_WPS */ 1409 1410 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *), 1411 compar); 1412 dump_scan_res(scan_res); 1413 1414 wpa_bss_update_start(wpa_s); 1415 for (i = 0; i < scan_res->num; i++) 1416 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]); 1417 wpa_bss_update_end(wpa_s, info, new_scan); 1418 1419 return scan_res; 1420} 1421 1422 1423int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s) 1424{ 1425 struct wpa_scan_results *scan_res; 1426 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0); 1427 if (scan_res == NULL) 1428 return -1; 1429 wpa_scan_results_free(scan_res); 1430 1431 return 0; 1432} 1433