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