2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
15 #include "wpa_supplicant_i.h"
17 #include "wps_supplicant.h"
18 #include "p2p_supplicant.h"
20 #include "hs20_supplicant.h"
26 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant
*wpa_s
)
28 struct wpa_ssid
*ssid
;
29 union wpa_event_data data
;
31 ssid
= wpa_supplicant_get_ssid(wpa_s
);
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
);
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
);
49 static int wpas_wps_in_use(struct wpa_supplicant
*wpa_s
,
50 enum wps_request_type
*req_type
)
52 struct wpa_ssid
*ssid
;
55 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
56 if (!(ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))
60 *req_type
= wpas_wps_get_req_type(ssid
);
61 if (!ssid
->eap
.phase1
)
64 if (os_strstr(ssid
->eap
.phase1
, "pbc=1"))
69 if (!wpa_s
->global
->p2p_disabled
&& wpa_s
->global
->p2p
) {
70 wpa_s
->wps
->dev
.p2p
= 1;
73 *req_type
= WPS_REQ_ENROLLEE_INFO
;
76 #endif /* CONFIG_P2P */
80 #endif /* CONFIG_WPS */
83 int wpa_supplicant_enabled_networks(struct wpa_supplicant
*wpa_s
)
85 struct wpa_ssid
*ssid
= wpa_s
->conf
->ssid
;
88 if (!wpas_network_disabled(wpa_s
, ssid
))
96 static void wpa_supplicant_assoc_try(struct wpa_supplicant
*wpa_s
,
97 struct wpa_ssid
*ssid
)
100 if (!wpas_network_disabled(wpa_s
, ssid
))
105 /* ap_scan=2 mode - try to associate with each SSID. */
107 wpa_dbg(wpa_s
, MSG_DEBUG
, "wpa_supplicant_assoc_try: Reached "
108 "end of scan list - go back to beginning");
109 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
110 wpa_supplicant_req_scan(wpa_s
, 0, 0);
114 /* Continue from the next SSID on the next attempt. */
115 wpa_s
->prev_scan_ssid
= ssid
;
117 /* Start from the beginning of the SSID list. */
118 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
120 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
124 static int int_array_len(const int *a
)
127 for (i
= 0; a
&& a
[i
]; i
++)
133 static void int_array_concat(int **res
, const int *a
)
138 reslen
= int_array_len(*res
);
139 alen
= int_array_len(a
);
141 n
= os_realloc_array(*res
, reslen
+ alen
+ 1, sizeof(int));
147 for (i
= 0; i
<= alen
; i
++)
148 n
[reslen
+ i
] = a
[i
];
153 static int freq_cmp(const void *a
, const void *b
)
166 static void int_array_sort_unique(int *a
)
174 alen
= int_array_len(a
);
175 qsort(a
, alen
, sizeof(int), freq_cmp
);
179 while (a
[i
] && a
[j
]) {
192 int wpa_supplicant_trigger_scan(struct wpa_supplicant
*wpa_s
,
193 struct wpa_driver_scan_params
*params
)
197 wpa_supplicant_notify_scanning(wpa_s
, 1);
199 ret
= wpa_drv_scan(wpa_s
, params
);
201 wpa_supplicant_notify_scanning(wpa_s
, 0);
202 wpas_notify_scan_done(wpa_s
, 0);
205 wpa_s
->normal_scans
++;
213 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
215 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
217 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting delayed sched scan");
219 if (wpa_supplicant_req_sched_scan(wpa_s
))
220 wpa_supplicant_req_scan(wpa_s
, 0, 0);
225 wpa_supplicant_sched_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
227 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
229 wpa_dbg(wpa_s
, MSG_DEBUG
, "Sched scan timeout - stopping it");
231 wpa_s
->sched_scan_timed_out
= 1;
232 wpa_supplicant_cancel_sched_scan(wpa_s
);
237 wpa_supplicant_start_sched_scan(struct wpa_supplicant
*wpa_s
,
238 struct wpa_driver_scan_params
*params
,
243 wpa_supplicant_notify_scanning(wpa_s
, 1);
244 ret
= wpa_drv_sched_scan(wpa_s
, params
, interval
* 1000);
246 wpa_supplicant_notify_scanning(wpa_s
, 0);
248 wpa_s
->sched_scanning
= 1;
254 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant
*wpa_s
)
258 ret
= wpa_drv_stop_sched_scan(wpa_s
);
260 wpa_dbg(wpa_s
, MSG_DEBUG
, "stopping sched_scan failed!");
261 /* TODO: what to do if stopping fails? */
269 static struct wpa_driver_scan_filter
*
270 wpa_supplicant_build_filter_ssids(struct wpa_config
*conf
, size_t *num_ssids
)
272 struct wpa_driver_scan_filter
*ssids
;
273 struct wpa_ssid
*ssid
;
277 if (!conf
->filter_ssids
)
280 for (count
= 0, ssid
= conf
->ssid
; ssid
; ssid
= ssid
->next
) {
281 if (ssid
->ssid
&& ssid
->ssid_len
)
286 ssids
= os_zalloc(count
* sizeof(struct wpa_driver_scan_filter
));
290 for (ssid
= conf
->ssid
; ssid
; ssid
= ssid
->next
) {
291 if (!ssid
->ssid
|| !ssid
->ssid_len
)
293 os_memcpy(ssids
[*num_ssids
].ssid
, ssid
->ssid
, ssid
->ssid_len
);
294 ssids
[*num_ssids
].ssid_len
= ssid
->ssid_len
;
302 static void wpa_supplicant_optimize_freqs(
303 struct wpa_supplicant
*wpa_s
, struct wpa_driver_scan_params
*params
)
306 if (params
->freqs
== NULL
&& wpa_s
->p2p_in_provisioning
&&
308 /* Optimize provisioning state scan based on GO information */
309 if (wpa_s
->p2p_in_provisioning
< 5 &&
310 wpa_s
->go_params
->freq
> 0) {
311 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only GO "
312 "preferred frequency %d MHz",
313 wpa_s
->go_params
->freq
);
314 params
->freqs
= os_zalloc(2 * sizeof(int));
316 params
->freqs
[0] = wpa_s
->go_params
->freq
;
317 } else if (wpa_s
->p2p_in_provisioning
< 8 &&
318 wpa_s
->go_params
->freq_list
[0]) {
319 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only common "
321 int_array_concat(¶ms
->freqs
,
322 wpa_s
->go_params
->freq_list
);
324 int_array_sort_unique(params
->freqs
);
326 wpa_s
->p2p_in_provisioning
++;
328 #endif /* CONFIG_P2P */
331 if (params
->freqs
== NULL
&& wpa_s
->after_wps
&& wpa_s
->wps_freq
) {
333 * Optimize post-provisioning scan based on channel used
334 * during provisioning.
336 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Scan only frequency %u MHz "
337 "that was used during provisioning", wpa_s
->wps_freq
);
338 params
->freqs
= os_zalloc(2 * sizeof(int));
340 params
->freqs
[0] = wpa_s
->wps_freq
;
344 if (params
->freqs
== NULL
&& wpa_s
->known_wps_freq
&& wpa_s
->wps_freq
)
346 /* Optimize provisioning scan based on already known channel */
347 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Scan only frequency %u MHz",
349 params
->freqs
= os_zalloc(2 * sizeof(int));
351 params
->freqs
[0] = wpa_s
->wps_freq
;
352 wpa_s
->known_wps_freq
= 0; /* only do this once */
354 #endif /* CONFIG_WPS */
358 #ifdef CONFIG_INTERWORKING
359 static void wpas_add_interworking_elements(struct wpa_supplicant
*wpa_s
,
362 if (wpa_s
->conf
->interworking
== 0)
365 wpabuf_put_u8(buf
, WLAN_EID_EXT_CAPAB
);
366 wpabuf_put_u8(buf
, 4);
367 wpabuf_put_u8(buf
, 0x00);
368 wpabuf_put_u8(buf
, 0x00);
369 wpabuf_put_u8(buf
, 0x00);
370 wpabuf_put_u8(buf
, 0x80); /* Bit 31 - Interworking */
372 wpabuf_put_u8(buf
, WLAN_EID_INTERWORKING
);
373 wpabuf_put_u8(buf
, is_zero_ether_addr(wpa_s
->conf
->hessid
) ? 1 :
375 wpabuf_put_u8(buf
, wpa_s
->conf
->access_network_type
);
377 if (!is_zero_ether_addr(wpa_s
->conf
->hessid
))
378 wpabuf_put_data(buf
, wpa_s
->conf
->hessid
, ETH_ALEN
);
380 #endif /* CONFIG_INTERWORKING */
383 static struct wpabuf
* wpa_supplicant_extra_ies(struct wpa_supplicant
*wpa_s
)
385 struct wpabuf
*extra_ie
= NULL
;
388 enum wps_request_type req_type
= WPS_REQ_ENROLLEE_INFO
;
389 #endif /* CONFIG_WPS */
391 #ifdef CONFIG_INTERWORKING
392 if (wpa_s
->conf
->interworking
&&
393 wpabuf_resize(&extra_ie
, 100) == 0)
394 wpas_add_interworking_elements(wpa_s
, extra_ie
);
395 #endif /* CONFIG_INTERWORKING */
398 wps
= wpas_wps_in_use(wpa_s
, &req_type
);
401 struct wpabuf
*wps_ie
;
402 wps_ie
= wps_build_probe_req_ie(wps
== 2 ? DEV_PW_PUSHBUTTON
:
405 wpa_s
->wps
->uuid
, req_type
,
408 if (wpabuf_resize(&extra_ie
, wpabuf_len(wps_ie
)) == 0)
409 wpabuf_put_buf(extra_ie
, wps_ie
);
416 size_t ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
417 if (wpabuf_resize(&extra_ie
, ielen
) == 0)
418 wpas_p2p_scan_ie(wpa_s
, extra_ie
);
420 #endif /* CONFIG_P2P */
422 #endif /* CONFIG_WPS */
428 static void wpa_supplicant_scan(void *eloop_ctx
, void *timeout_ctx
)
430 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
431 struct wpa_ssid
*ssid
;
432 int scan_req
= 0, ret
;
433 struct wpabuf
*extra_ie
= NULL
;
434 struct wpa_driver_scan_params params
;
435 struct wpa_driver_scan_params
*scan_params
;
437 enum wpa_states prev_state
;
439 if (wpa_s
->wpa_state
== WPA_INTERFACE_DISABLED
) {
440 wpa_dbg(wpa_s
, MSG_DEBUG
, "Skip scan - interface disabled");
444 if (wpa_s
->disconnected
&& !wpa_s
->scan_req
) {
445 wpa_supplicant_set_state(wpa_s
, WPA_DISCONNECTED
);
449 if (!wpa_supplicant_enabled_networks(wpa_s
) &&
451 wpa_dbg(wpa_s
, MSG_DEBUG
, "No enabled networks - do not scan");
452 wpa_supplicant_set_state(wpa_s
, WPA_INACTIVE
);
456 if (wpa_s
->conf
->ap_scan
!= 0 &&
457 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_WIRED
)) {
458 wpa_dbg(wpa_s
, MSG_DEBUG
, "Using wired authentication - "
459 "overriding ap_scan configuration");
460 wpa_s
->conf
->ap_scan
= 0;
461 wpas_notify_ap_scan_changed(wpa_s
);
464 if (wpa_s
->conf
->ap_scan
== 0) {
465 wpa_supplicant_gen_assoc_event(wpa_s
);
470 if (wpas_p2p_in_progress(wpa_s
)) {
471 if (wpa_s
->sta_scan_pending
&&
472 wpas_p2p_in_progress(wpa_s
) == 2 &&
473 wpa_s
->p2p_cb_on_scan_complete
) {
474 wpa_dbg(wpa_s
, MSG_DEBUG
, "Process pending station "
475 "mode scan during P2P search");
477 wpa_dbg(wpa_s
, MSG_DEBUG
, "Delay station mode scan "
478 "while P2P operation is in progress");
479 wpa_s
->sta_scan_pending
= 1;
480 wpa_supplicant_req_scan(wpa_s
, 5, 0);
484 #endif /* CONFIG_P2P */
486 if (wpa_s
->conf
->ap_scan
== 2)
489 max_ssids
= wpa_s
->max_scan_ssids
;
490 if (max_ssids
> WPAS_MAX_SCAN_SSIDS
)
491 max_ssids
= WPAS_MAX_SCAN_SSIDS
;
494 scan_req
= wpa_s
->scan_req
;
497 os_memset(¶ms
, 0, sizeof(params
));
499 prev_state
= wpa_s
->wpa_state
;
500 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
501 wpa_s
->wpa_state
== WPA_INACTIVE
)
502 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
505 * If autoscan has set its own scanning parameters
507 if (wpa_s
->autoscan_params
!= NULL
) {
508 scan_params
= wpa_s
->autoscan_params
;
512 if (scan_req
!= 2 && wpa_s
->connect_without_scan
) {
513 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
514 if (ssid
== wpa_s
->connect_without_scan
)
517 wpa_s
->connect_without_scan
= NULL
;
519 wpa_printf(MSG_DEBUG
, "Start a pre-selected network "
520 "without scan step");
521 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
527 if ((wpa_s
->p2p_in_provisioning
|| wpa_s
->show_group_started
) &&
529 wpa_printf(MSG_DEBUG
, "P2P: Use specific SSID for scan during "
530 "P2P group formation");
531 params
.ssids
[0].ssid
= wpa_s
->go_params
->ssid
;
532 params
.ssids
[0].ssid_len
= wpa_s
->go_params
->ssid_len
;
533 params
.num_ssids
= 1;
536 #endif /* CONFIG_P2P */
538 /* Find the starting point from which to continue scanning */
539 ssid
= wpa_s
->conf
->ssid
;
540 if (wpa_s
->prev_scan_ssid
!= WILDCARD_SSID_SCAN
) {
542 if (ssid
== wpa_s
->prev_scan_ssid
) {
550 if (scan_req
!= 2 && wpa_s
->conf
->ap_scan
== 2) {
551 wpa_s
->connect_without_scan
= NULL
;
552 wpa_s
->prev_scan_wildcard
= 0;
553 wpa_supplicant_assoc_try(wpa_s
, ssid
);
555 } else if (wpa_s
->conf
->ap_scan
== 2) {
557 * User-initiated scan request in ap_scan == 2; scan with
562 struct wpa_ssid
*start
= ssid
, *tssid
;
564 if (ssid
== NULL
&& max_ssids
> 1)
565 ssid
= wpa_s
->conf
->ssid
;
567 if (!wpas_network_disabled(wpa_s
, ssid
) &&
569 wpa_hexdump_ascii(MSG_DEBUG
, "Scan SSID",
570 ssid
->ssid
, ssid
->ssid_len
);
571 params
.ssids
[params
.num_ssids
].ssid
=
573 params
.ssids
[params
.num_ssids
].ssid_len
=
576 if (params
.num_ssids
+ 1 >= max_ssids
)
582 if (ssid
== NULL
&& max_ssids
> 1 &&
583 start
!= wpa_s
->conf
->ssid
)
584 ssid
= wpa_s
->conf
->ssid
;
587 for (tssid
= wpa_s
->conf
->ssid
; tssid
; tssid
= tssid
->next
) {
588 if (wpas_network_disabled(wpa_s
, tssid
))
590 if ((params
.freqs
|| !freqs_set
) && tssid
->scan_freq
) {
591 int_array_concat(¶ms
.freqs
,
594 os_free(params
.freqs
);
599 int_array_sort_unique(params
.freqs
);
602 if (ssid
&& max_ssids
== 1) {
604 * If the driver is limited to 1 SSID at a time interleave
605 * wildcard SSID scans with specific SSID scans to avoid
606 * waiting a long time for a wildcard scan.
608 if (!wpa_s
->prev_scan_wildcard
) {
609 params
.ssids
[0].ssid
= NULL
;
610 params
.ssids
[0].ssid_len
= 0;
611 wpa_s
->prev_scan_wildcard
= 1;
612 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for "
613 "wildcard SSID (Interleave with specific)");
615 wpa_s
->prev_scan_ssid
= ssid
;
616 wpa_s
->prev_scan_wildcard
= 0;
617 wpa_dbg(wpa_s
, MSG_DEBUG
,
618 "Starting AP scan for specific SSID: %s",
619 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
624 wpa_s
->prev_scan_ssid
= ssid
;
625 wpa_dbg(wpa_s
, MSG_DEBUG
, "Include wildcard SSID in "
629 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
631 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for wildcard "
636 #endif /* CONFIG_P2P */
638 wpa_supplicant_optimize_freqs(wpa_s
, ¶ms
);
639 extra_ie
= wpa_supplicant_extra_ies(wpa_s
);
642 if (wpa_s
->conf
->hs20
&& wpabuf_resize(&extra_ie
, 6) == 0)
643 wpas_hs20_add_indication(extra_ie
);
644 #endif /* CONFIG_HS20 */
646 if (params
.freqs
== NULL
&& wpa_s
->next_scan_freqs
) {
647 wpa_dbg(wpa_s
, MSG_DEBUG
, "Optimize scan based on previously "
648 "generated frequency list");
649 params
.freqs
= wpa_s
->next_scan_freqs
;
651 os_free(wpa_s
->next_scan_freqs
);
652 wpa_s
->next_scan_freqs
= NULL
;
654 params
.filter_ssids
= wpa_supplicant_build_filter_ssids(
655 wpa_s
->conf
, ¶ms
.num_filter_ssids
);
657 params
.extra_ies
= wpabuf_head(extra_ie
);
658 params
.extra_ies_len
= wpabuf_len(extra_ie
);
662 if (wpa_s
->p2p_in_provisioning
||
663 (wpa_s
->show_group_started
&& wpa_s
->go_params
)) {
665 * The interface may not yet be in P2P mode, so we have to
666 * explicitly request P2P probe to disable CCK rates.
668 params
.p2p_probe
= 1;
670 #endif /* CONFIG_P2P */
672 scan_params
= ¶ms
;
675 ret
= wpa_supplicant_trigger_scan(wpa_s
, scan_params
);
677 wpabuf_free(extra_ie
);
678 os_free(params
.freqs
);
679 os_free(params
.filter_ssids
);
682 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate AP scan");
683 if (prev_state
!= wpa_s
->wpa_state
)
684 wpa_supplicant_set_state(wpa_s
, prev_state
);
685 wpa_supplicant_req_scan(wpa_s
, 1, 0);
691 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
692 * @wpa_s: Pointer to wpa_supplicant data
693 * @sec: Number of seconds after which to scan
694 * @usec: Number of microseconds after which to scan
696 * This function is used to schedule a scan for neighboring access points after
697 * the specified time.
699 void wpa_supplicant_req_scan(struct wpa_supplicant
*wpa_s
, int sec
, int usec
)
701 /* If there's at least one network that should be specifically scanned
702 * then don't cancel the scan and reschedule. Some drivers do
703 * background scanning which generates frequent scan results, and that
704 * causes the specific SSID scan to get continually pushed back and
705 * never happen, which causes hidden APs to never get probe-scanned.
707 if (eloop_is_timeout_registered(wpa_supplicant_scan
, wpa_s
, NULL
) &&
708 wpa_s
->conf
->ap_scan
== 1) {
709 struct wpa_ssid
*ssid
= wpa_s
->conf
->ssid
;
712 if (!wpas_network_disabled(wpa_s
, ssid
) &&
718 wpa_dbg(wpa_s
, MSG_DEBUG
, "Not rescheduling scan to "
719 "ensure that specific SSID scans occur");
724 wpa_dbg(wpa_s
, MSG_DEBUG
, "Setting scan request: %d sec %d usec",
726 eloop_cancel_timeout(wpa_supplicant_scan
, wpa_s
, NULL
);
727 eloop_register_timeout(sec
, usec
, wpa_supplicant_scan
, wpa_s
, NULL
);
732 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
733 * @wpa_s: Pointer to wpa_supplicant data
734 * @sec: Number of seconds after which to scan
735 * @usec: Number of microseconds after which to scan
737 * This function is used to schedule periodic scans for neighboring
738 * access points after the specified time.
740 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant
*wpa_s
,
743 if (!wpa_s
->sched_scan_supported
)
746 eloop_register_timeout(sec
, usec
,
747 wpa_supplicant_delayed_sched_scan_timeout
,
755 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
756 * @wpa_s: Pointer to wpa_supplicant data
758 * This function is used to schedule periodic scans for neighboring
759 * access points repeating the scan continuously.
761 int wpa_supplicant_req_sched_scan(struct wpa_supplicant
*wpa_s
)
763 struct wpa_driver_scan_params params
;
764 struct wpa_driver_scan_params
*scan_params
;
765 enum wpa_states prev_state
;
766 struct wpa_ssid
*ssid
= NULL
;
767 struct wpabuf
*extra_ie
= NULL
;
769 unsigned int max_sched_scan_ssids
;
773 if (!wpa_s
->sched_scan_supported
)
776 if (wpa_s
->max_sched_scan_ssids
> WPAS_MAX_SCAN_SSIDS
)
777 max_sched_scan_ssids
= WPAS_MAX_SCAN_SSIDS
;
779 max_sched_scan_ssids
= wpa_s
->max_sched_scan_ssids
;
780 if (max_sched_scan_ssids
< 1 || wpa_s
->conf
->disable_scan_offload
)
783 if (wpa_s
->sched_scanning
) {
784 wpa_dbg(wpa_s
, MSG_DEBUG
, "Already sched scanning");
789 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
790 if (!wpas_network_disabled(wpa_s
, ssid
) && !ssid
->scan_ssid
) {
791 /* Use wildcard SSID to find this network */
793 } else if (!wpas_network_disabled(wpa_s
, ssid
) &&
798 if (!wpas_network_disabled(wpa_s
, ssid
) &&
799 ssid
->key_mgmt
== WPA_KEY_MGMT_WPS
) {
801 * Normal scan is more reliable and faster for WPS
802 * operations and since these are for short periods of
803 * time, the benefit of trying to use sched_scan would
806 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use normal scan instead of "
807 "sched_scan for WPS");
810 #endif /* CONFIG_WPS */
815 if (wpa_s
->normal_scans
< 3 &&
816 (need_ssids
<= wpa_s
->max_scan_ssids
||
817 wpa_s
->max_scan_ssids
>= (int) max_sched_scan_ssids
)) {
819 * When normal scan can speed up operations, use that for the
820 * first operations before starting the sched_scan to allow
821 * user space sleep more. We do this only if the normal scan
822 * has functionality that is suitable for this or if the
823 * sched_scan does not have better support for multiple SSIDs.
825 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use normal scan instead of "
826 "sched_scan for initial scans (normal_scans=%d)",
827 wpa_s
->normal_scans
);
831 os_memset(¶ms
, 0, sizeof(params
));
833 /* If we can't allocate space for the filters, we just don't filter */
834 params
.filter_ssids
= os_zalloc(wpa_s
->max_match_sets
*
835 sizeof(struct wpa_driver_scan_filter
));
837 prev_state
= wpa_s
->wpa_state
;
838 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
839 wpa_s
->wpa_state
== WPA_INACTIVE
)
840 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
842 if (wpa_s
->autoscan_params
!= NULL
) {
843 scan_params
= wpa_s
->autoscan_params
;
847 /* Find the starting point from which to continue scanning */
848 ssid
= wpa_s
->conf
->ssid
;
849 if (wpa_s
->prev_sched_ssid
) {
851 if (ssid
== wpa_s
->prev_sched_ssid
) {
859 if (!ssid
|| !wpa_s
->prev_sched_ssid
) {
860 wpa_dbg(wpa_s
, MSG_DEBUG
, "Beginning of SSID list");
862 if (wpa_s
->sched_scan_interval
== 0)
863 wpa_s
->sched_scan_interval
= 10;
864 wpa_s
->sched_scan_timeout
= max_sched_scan_ssids
* 2;
865 wpa_s
->first_sched_scan
= 1;
866 ssid
= wpa_s
->conf
->ssid
;
867 wpa_s
->prev_sched_ssid
= ssid
;
871 wpa_dbg(wpa_s
, MSG_DEBUG
, "Add wildcard SSID to sched_scan");
876 if (wpas_network_disabled(wpa_s
, ssid
))
879 if (params
.num_filter_ssids
< wpa_s
->max_match_sets
&&
880 params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
) {
881 wpa_dbg(wpa_s
, MSG_DEBUG
, "add to filter ssid: %s",
882 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
883 os_memcpy(params
.filter_ssids
[params
.num_filter_ssids
].ssid
,
884 ssid
->ssid
, ssid
->ssid_len
);
885 params
.filter_ssids
[params
.num_filter_ssids
].ssid_len
=
887 params
.num_filter_ssids
++;
888 } else if (params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
)
890 wpa_dbg(wpa_s
, MSG_DEBUG
, "Not enough room for SSID "
891 "filter for sched_scan - drop filter");
892 os_free(params
.filter_ssids
);
893 params
.filter_ssids
= NULL
;
894 params
.num_filter_ssids
= 0;
897 if (ssid
->scan_ssid
&& ssid
->ssid
&& ssid
->ssid_len
) {
898 if (params
.num_ssids
== max_sched_scan_ssids
)
899 break; /* only room for broadcast SSID */
900 wpa_dbg(wpa_s
, MSG_DEBUG
,
901 "add to active scan ssid: %s",
902 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
903 params
.ssids
[params
.num_ssids
].ssid
=
905 params
.ssids
[params
.num_ssids
].ssid_len
=
908 if (params
.num_ssids
>= max_sched_scan_ssids
) {
909 wpa_s
->prev_sched_ssid
= ssid
;
913 (wpas_network_disabled(wpa_s
, ssid
) ||
920 wpa_s
->prev_sched_ssid
= ssid
;
924 if (params
.num_filter_ssids
== 0) {
925 os_free(params
.filter_ssids
);
926 params
.filter_ssids
= NULL
;
929 extra_ie
= wpa_supplicant_extra_ies(wpa_s
);
931 params
.extra_ies
= wpabuf_head(extra_ie
);
932 params
.extra_ies_len
= wpabuf_len(extra_ie
);
935 scan_params
= ¶ms
;
938 if (ssid
|| !wpa_s
->first_sched_scan
) {
939 wpa_dbg(wpa_s
, MSG_DEBUG
,
940 "Starting sched scan: interval %d timeout %d",
941 wpa_s
->sched_scan_interval
, wpa_s
->sched_scan_timeout
);
943 wpa_dbg(wpa_s
, MSG_DEBUG
,
944 "Starting sched scan: interval %d (no timeout)",
945 wpa_s
->sched_scan_interval
);
948 ret
= wpa_supplicant_start_sched_scan(wpa_s
, scan_params
,
949 wpa_s
->sched_scan_interval
);
950 wpabuf_free(extra_ie
);
951 os_free(params
.filter_ssids
);
953 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate sched scan");
954 if (prev_state
!= wpa_s
->wpa_state
)
955 wpa_supplicant_set_state(wpa_s
, prev_state
);
959 /* If we have more SSIDs to scan, add a timeout so we scan them too */
960 if (ssid
|| !wpa_s
->first_sched_scan
) {
961 wpa_s
->sched_scan_timed_out
= 0;
962 eloop_register_timeout(wpa_s
->sched_scan_timeout
, 0,
963 wpa_supplicant_sched_scan_timeout
,
965 wpa_s
->first_sched_scan
= 0;
966 wpa_s
->sched_scan_timeout
/= 2;
967 wpa_s
->sched_scan_interval
*= 2;
975 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
976 * @wpa_s: Pointer to wpa_supplicant data
978 * This function is used to cancel a scan request scheduled with
979 * wpa_supplicant_req_scan().
981 void wpa_supplicant_cancel_scan(struct wpa_supplicant
*wpa_s
)
983 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling scan request");
984 eloop_cancel_timeout(wpa_supplicant_scan
, wpa_s
, NULL
);
989 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
990 * @wpa_s: Pointer to wpa_supplicant data
992 * This function is used to stop a periodic scheduled scan.
994 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant
*wpa_s
)
996 if (!wpa_s
->sched_scanning
)
999 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling sched scan");
1000 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout
, wpa_s
, NULL
);
1001 wpa_supplicant_stop_sched_scan(wpa_s
);
1005 void wpa_supplicant_notify_scanning(struct wpa_supplicant
*wpa_s
,
1008 if (wpa_s
->scanning
!= scanning
) {
1009 wpa_s
->scanning
= scanning
;
1010 wpas_notify_scanning(wpa_s
);
1015 static int wpa_scan_get_max_rate(const struct wpa_scan_res
*res
)
1021 ie
= wpa_scan_get_ie(res
, WLAN_EID_SUPP_RATES
);
1022 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1023 if ((ie
[i
+ 2] & 0x7f) > rate
)
1024 rate
= ie
[i
+ 2] & 0x7f;
1027 ie
= wpa_scan_get_ie(res
, WLAN_EID_EXT_SUPP_RATES
);
1028 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1029 if ((ie
[i
+ 2] & 0x7f) > rate
)
1030 rate
= ie
[i
+ 2] & 0x7f;
1037 const u8
* wpa_scan_get_ie(const struct wpa_scan_res
*res
, u8 ie
)
1039 const u8
*end
, *pos
;
1041 pos
= (const u8
*) (res
+ 1);
1042 end
= pos
+ res
->ie_len
;
1044 while (pos
+ 1 < end
) {
1045 if (pos
+ 2 + pos
[1] > end
)
1056 const u8
* wpa_scan_get_vendor_ie(const struct wpa_scan_res
*res
,
1059 const u8
*end
, *pos
;
1061 pos
= (const u8
*) (res
+ 1);
1062 end
= pos
+ res
->ie_len
;
1064 while (pos
+ 1 < end
) {
1065 if (pos
+ 2 + pos
[1] > end
)
1067 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1068 vendor_type
== WPA_GET_BE32(&pos
[2]))
1077 struct wpabuf
* wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res
*res
,
1081 const u8
*end
, *pos
;
1083 buf
= wpabuf_alloc(res
->ie_len
);
1087 pos
= (const u8
*) (res
+ 1);
1088 end
= pos
+ res
->ie_len
;
1090 while (pos
+ 1 < end
) {
1091 if (pos
+ 2 + pos
[1] > end
)
1093 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1094 vendor_type
== WPA_GET_BE32(&pos
[2]))
1095 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
1099 if (wpabuf_len(buf
) == 0) {
1108 struct wpabuf
* wpa_scan_get_vendor_ie_multi_beacon(
1109 const struct wpa_scan_res
*res
, u32 vendor_type
)
1112 const u8
*end
, *pos
;
1114 if (res
->beacon_ie_len
== 0)
1116 buf
= wpabuf_alloc(res
->beacon_ie_len
);
1120 pos
= (const u8
*) (res
+ 1);
1122 end
= pos
+ res
->beacon_ie_len
;
1124 while (pos
+ 1 < end
) {
1125 if (pos
+ 2 + pos
[1] > end
)
1127 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1128 vendor_type
== WPA_GET_BE32(&pos
[2]))
1129 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
1133 if (wpabuf_len(buf
) == 0) {
1143 * Channels with a great SNR can operate at full rate. What is a great SNR?
1144 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1145 * rule of thumb is that any SNR above 20 is good." This one
1146 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1147 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1148 * conservative value.
1150 #define GREAT_SNR 30
1152 /* Compare function for sorting scan results. Return >0 if @b is considered
1154 static int wpa_scan_result_compar(const void *a
, const void *b
)
1156 #define IS_5GHZ(n) (n > 4000)
1157 #define MIN(a,b) a < b ? a : b
1158 struct wpa_scan_res
**_wa
= (void *) a
;
1159 struct wpa_scan_res
**_wb
= (void *) b
;
1160 struct wpa_scan_res
*wa
= *_wa
;
1161 struct wpa_scan_res
*wb
= *_wb
;
1162 int wpa_a
, wpa_b
, maxrate_a
, maxrate_b
;
1165 /* WPA/WPA2 support preferred */
1166 wpa_a
= wpa_scan_get_vendor_ie(wa
, WPA_IE_VENDOR_TYPE
) != NULL
||
1167 wpa_scan_get_ie(wa
, WLAN_EID_RSN
) != NULL
;
1168 wpa_b
= wpa_scan_get_vendor_ie(wb
, WPA_IE_VENDOR_TYPE
) != NULL
||
1169 wpa_scan_get_ie(wb
, WLAN_EID_RSN
) != NULL
;
1171 if (wpa_b
&& !wpa_a
)
1173 if (!wpa_b
&& wpa_a
)
1176 /* privacy support preferred */
1177 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) == 0 &&
1178 (wb
->caps
& IEEE80211_CAP_PRIVACY
))
1180 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) &&
1181 (wb
->caps
& IEEE80211_CAP_PRIVACY
) == 0)
1184 if ((wa
->flags
& wb
->flags
& WPA_SCAN_LEVEL_DBM
) &&
1185 !((wa
->flags
| wb
->flags
) & WPA_SCAN_NOISE_INVALID
)) {
1186 snr_a
= MIN(wa
->level
- wa
->noise
, GREAT_SNR
);
1187 snr_b
= MIN(wb
->level
- wb
->noise
, GREAT_SNR
);
1189 /* Not suitable information to calculate SNR, so use level */
1194 /* best/max rate preferred if SNR close enough */
1195 if ((snr_a
&& snr_b
&& abs(snr_b
- snr_a
) < 5) ||
1196 (wa
->qual
&& wb
->qual
&& abs(wb
->qual
- wa
->qual
) < 10)) {
1197 maxrate_a
= wpa_scan_get_max_rate(wa
);
1198 maxrate_b
= wpa_scan_get_max_rate(wb
);
1199 if (maxrate_a
!= maxrate_b
)
1200 return maxrate_b
- maxrate_a
;
1201 if (IS_5GHZ(wa
->freq
) ^ IS_5GHZ(wb
->freq
))
1202 return IS_5GHZ(wa
->freq
) ? -1 : 1;
1205 /* use freq for channel preference */
1207 /* all things being equal, use SNR; if SNRs are
1208 * identical, use quality values since some drivers may only report
1209 * that value and leave the signal level zero */
1211 return wb
->qual
- wa
->qual
;
1212 return snr_b
- snr_a
;
1219 /* Compare function for sorting scan results when searching a WPS AP for
1220 * provisioning. Return >0 if @b is considered better. */
1221 static int wpa_scan_result_wps_compar(const void *a
, const void *b
)
1223 struct wpa_scan_res
**_wa
= (void *) a
;
1224 struct wpa_scan_res
**_wb
= (void *) b
;
1225 struct wpa_scan_res
*wa
= *_wa
;
1226 struct wpa_scan_res
*wb
= *_wb
;
1227 int uses_wps_a
, uses_wps_b
;
1228 struct wpabuf
*wps_a
, *wps_b
;
1231 /* Optimization - check WPS IE existence before allocated memory and
1232 * doing full reassembly. */
1233 uses_wps_a
= wpa_scan_get_vendor_ie(wa
, WPS_IE_VENDOR_TYPE
) != NULL
;
1234 uses_wps_b
= wpa_scan_get_vendor_ie(wb
, WPS_IE_VENDOR_TYPE
) != NULL
;
1235 if (uses_wps_a
&& !uses_wps_b
)
1237 if (!uses_wps_a
&& uses_wps_b
)
1240 if (uses_wps_a
&& uses_wps_b
) {
1241 wps_a
= wpa_scan_get_vendor_ie_multi(wa
, WPS_IE_VENDOR_TYPE
);
1242 wps_b
= wpa_scan_get_vendor_ie_multi(wb
, WPS_IE_VENDOR_TYPE
);
1243 res
= wps_ap_priority_compar(wps_a
, wps_b
);
1251 * Do not use current AP security policy as a sorting criteria during
1252 * WPS provisioning step since the AP may get reconfigured at the
1253 * completion of provisioning.
1256 /* all things being equal, use signal level; if signal levels are
1257 * identical, use quality values since some drivers may only report
1258 * that value and leave the signal level zero */
1259 if (wb
->level
== wa
->level
)
1260 return wb
->qual
- wa
->qual
;
1261 return wb
->level
- wa
->level
;
1263 #endif /* CONFIG_WPS */
1266 static void dump_scan_res(struct wpa_scan_results
*scan_res
)
1268 #ifndef CONFIG_NO_STDOUT_DEBUG
1271 if (scan_res
->res
== NULL
|| scan_res
->num
== 0)
1274 wpa_printf(MSG_EXCESSIVE
, "Sorted scan results");
1276 for (i
= 0; i
< scan_res
->num
; i
++) {
1277 struct wpa_scan_res
*r
= scan_res
->res
[i
];
1279 if ((r
->flags
& (WPA_SCAN_LEVEL_DBM
| WPA_SCAN_NOISE_INVALID
))
1280 == WPA_SCAN_LEVEL_DBM
) {
1281 int snr
= r
->level
- r
->noise
;
1282 wpa_printf(MSG_EXCESSIVE
, MACSTR
" freq=%d qual=%d "
1283 "noise=%d level=%d snr=%d%s flags=0x%x",
1284 MAC2STR(r
->bssid
), r
->freq
, r
->qual
,
1285 r
->noise
, r
->level
, snr
,
1286 snr
>= GREAT_SNR
? "*" : "", r
->flags
);
1288 wpa_printf(MSG_EXCESSIVE
, MACSTR
" freq=%d qual=%d "
1289 "noise=%d level=%d flags=0x%x",
1290 MAC2STR(r
->bssid
), r
->freq
, r
->qual
,
1291 r
->noise
, r
->level
, r
->flags
);
1293 pos
= (u8
*) (r
+ 1);
1295 wpa_hexdump(MSG_EXCESSIVE
, "IEs", pos
, r
->ie_len
);
1297 if (r
->beacon_ie_len
)
1298 wpa_hexdump(MSG_EXCESSIVE
, "Beacon IEs",
1299 pos
, r
->beacon_ie_len
);
1301 #endif /* CONFIG_NO_STDOUT_DEBUG */
1305 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant
*wpa_s
,
1310 if (wpa_s
->bssid_filter
== NULL
)
1313 for (i
= 0; i
< wpa_s
->bssid_filter_count
; i
++) {
1314 if (os_memcmp(wpa_s
->bssid_filter
+ i
* ETH_ALEN
, bssid
,
1323 static void filter_scan_res(struct wpa_supplicant
*wpa_s
,
1324 struct wpa_scan_results
*res
)
1328 if (wpa_s
->bssid_filter
== NULL
)
1331 for (i
= 0, j
= 0; i
< res
->num
; i
++) {
1332 if (wpa_supplicant_filter_bssid_match(wpa_s
,
1333 res
->res
[i
]->bssid
)) {
1334 res
->res
[j
++] = res
->res
[i
];
1336 os_free(res
->res
[i
]);
1341 if (res
->num
!= j
) {
1342 wpa_printf(MSG_DEBUG
, "Filtered out %d scan results",
1343 (int) (res
->num
- j
));
1350 * wpa_supplicant_get_scan_results - Get scan results
1351 * @wpa_s: Pointer to wpa_supplicant data
1352 * @info: Information about what was scanned or %NULL if not available
1353 * @new_scan: Whether a new scan was performed
1354 * Returns: Scan results, %NULL on failure
1356 * This function request the current scan results from the driver and updates
1357 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1358 * results with wpa_scan_results_free().
1360 struct wpa_scan_results
*
1361 wpa_supplicant_get_scan_results(struct wpa_supplicant
*wpa_s
,
1362 struct scan_info
*info
, int new_scan
)
1364 struct wpa_scan_results
*scan_res
;
1366 int (*compar
)(const void *, const void *) = wpa_scan_result_compar
;
1368 scan_res
= wpa_drv_get_scan_results2(wpa_s
);
1369 if (scan_res
== NULL
) {
1370 wpa_dbg(wpa_s
, MSG_DEBUG
, "Failed to get scan results");
1373 filter_scan_res(wpa_s
, scan_res
);
1376 if (wpas_wps_in_progress(wpa_s
)) {
1377 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Order scan results with WPS "
1378 "provisioning rules");
1379 compar
= wpa_scan_result_wps_compar
;
1381 #endif /* CONFIG_WPS */
1383 qsort(scan_res
->res
, scan_res
->num
, sizeof(struct wpa_scan_res
*),
1385 dump_scan_res(scan_res
);
1387 wpa_bss_update_start(wpa_s
);
1388 for (i
= 0; i
< scan_res
->num
; i
++)
1389 wpa_bss_update_scan_res(wpa_s
, scan_res
->res
[i
]);
1390 wpa_bss_update_end(wpa_s
, info
, new_scan
);
1396 int wpa_supplicant_update_scan_results(struct wpa_supplicant
*wpa_s
)
1398 struct wpa_scan_results
*scan_res
;
1399 scan_res
= wpa_supplicant_get_scan_results(wpa_s
, NULL
, 0);
1400 if (scan_res
== NULL
)
1402 wpa_scan_results_free(scan_res
);