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