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_config
*conf
)
85 struct wpa_ssid
*ssid
= conf
->ssid
;
88 if (!wpas_network_disabled(ssid
))
96 static void wpa_supplicant_assoc_try(struct wpa_supplicant
*wpa_s
,
97 struct wpa_ssid
*ssid
)
100 if (!wpas_network_disabled(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(*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
*
384 wpa_supplicant_extra_ies(struct wpa_supplicant
*wpa_s
,
385 struct wpa_driver_scan_params
*params
)
387 struct wpabuf
*extra_ie
= NULL
;
390 enum wps_request_type req_type
= WPS_REQ_ENROLLEE_INFO
;
391 #endif /* CONFIG_WPS */
393 #ifdef CONFIG_INTERWORKING
394 if (wpa_s
->conf
->interworking
&&
395 wpabuf_resize(&extra_ie
, 100) == 0)
396 wpas_add_interworking_elements(wpa_s
, extra_ie
);
397 #endif /* CONFIG_INTERWORKING */
400 wps
= wpas_wps_in_use(wpa_s
, &req_type
);
403 struct wpabuf
*wps_ie
;
404 wps_ie
= wps_build_probe_req_ie(wps
== 2 ? DEV_PW_PUSHBUTTON
:
407 wpa_s
->wps
->uuid
, req_type
,
410 if (wpabuf_resize(&extra_ie
, wpabuf_len(wps_ie
)) == 0)
411 wpabuf_put_buf(extra_ie
, wps_ie
);
418 size_t ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
419 if (wpabuf_resize(&extra_ie
, ielen
) == 0)
420 wpas_p2p_scan_ie(wpa_s
, extra_ie
);
422 #endif /* CONFIG_P2P */
424 #endif /* CONFIG_WPS */
430 static void wpa_supplicant_scan(void *eloop_ctx
, void *timeout_ctx
)
432 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
433 struct wpa_ssid
*ssid
;
434 int scan_req
= 0, ret
;
435 struct wpabuf
*extra_ie
;
436 struct wpa_driver_scan_params params
;
438 enum wpa_states prev_state
;
440 if (wpa_s
->wpa_state
== WPA_INTERFACE_DISABLED
) {
441 wpa_dbg(wpa_s
, MSG_DEBUG
, "Skip scan - interface disabled");
445 if (wpa_s
->disconnected
&& !wpa_s
->scan_req
) {
446 wpa_supplicant_set_state(wpa_s
, WPA_DISCONNECTED
);
450 if (!wpa_supplicant_enabled_networks(wpa_s
->conf
) &&
452 wpa_dbg(wpa_s
, MSG_DEBUG
, "No enabled networks - do not scan");
453 wpa_supplicant_set_state(wpa_s
, WPA_INACTIVE
);
457 if (wpa_s
->conf
->ap_scan
!= 0 &&
458 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_WIRED
)) {
459 wpa_dbg(wpa_s
, MSG_DEBUG
, "Using wired authentication - "
460 "overriding ap_scan configuration");
461 wpa_s
->conf
->ap_scan
= 0;
462 wpas_notify_ap_scan_changed(wpa_s
);
465 if (wpa_s
->conf
->ap_scan
== 0) {
466 wpa_supplicant_gen_assoc_event(wpa_s
);
471 if (wpas_p2p_in_progress(wpa_s
)) {
472 if (wpa_s
->wpa_state
== WPA_SCANNING
) {
473 wpa_dbg(wpa_s
, MSG_DEBUG
, "Delay station mode scan "
474 "while P2P operation is in progress");
475 wpa_supplicant_req_scan(wpa_s
, 5, 0);
477 wpa_dbg(wpa_s
, MSG_DEBUG
, "Do not request scan while "
478 "P2P operation is in progress");
482 #endif /* CONFIG_P2P */
484 if (wpa_s
->conf
->ap_scan
== 2)
487 max_ssids
= wpa_s
->max_scan_ssids
;
488 if (max_ssids
> WPAS_MAX_SCAN_SSIDS
)
489 max_ssids
= WPAS_MAX_SCAN_SSIDS
;
492 scan_req
= wpa_s
->scan_req
;
495 os_memset(¶ms
, 0, sizeof(params
));
497 prev_state
= wpa_s
->wpa_state
;
498 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
499 wpa_s
->wpa_state
== WPA_INACTIVE
)
500 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
502 if (scan_req
!= 2 && wpa_s
->connect_without_scan
) {
503 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
504 if (ssid
== wpa_s
->connect_without_scan
)
507 wpa_s
->connect_without_scan
= NULL
;
509 wpa_printf(MSG_DEBUG
, "Start a pre-selected network "
510 "without scan step");
511 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
517 if ((wpa_s
->p2p_in_provisioning
|| wpa_s
->show_group_started
) &&
519 wpa_printf(MSG_DEBUG
, "P2P: Use specific SSID for scan during "
520 "P2P group formation");
521 params
.ssids
[0].ssid
= wpa_s
->go_params
->ssid
;
522 params
.ssids
[0].ssid_len
= wpa_s
->go_params
->ssid_len
;
523 params
.num_ssids
= 1;
526 #endif /* CONFIG_P2P */
528 /* Find the starting point from which to continue scanning */
529 ssid
= wpa_s
->conf
->ssid
;
530 if (wpa_s
->prev_scan_ssid
!= WILDCARD_SSID_SCAN
) {
532 if (ssid
== wpa_s
->prev_scan_ssid
) {
540 if (scan_req
!= 2 && wpa_s
->conf
->ap_scan
== 2) {
541 wpa_s
->connect_without_scan
= NULL
;
542 wpa_s
->prev_scan_wildcard
= 0;
543 wpa_supplicant_assoc_try(wpa_s
, ssid
);
545 } else if (wpa_s
->conf
->ap_scan
== 2) {
547 * User-initiated scan request in ap_scan == 2; scan with
552 struct wpa_ssid
*start
= ssid
, *tssid
;
554 if (ssid
== NULL
&& max_ssids
> 1)
555 ssid
= wpa_s
->conf
->ssid
;
557 if (!wpas_network_disabled(ssid
) && ssid
->scan_ssid
) {
558 wpa_hexdump_ascii(MSG_DEBUG
, "Scan SSID",
559 ssid
->ssid
, ssid
->ssid_len
);
560 params
.ssids
[params
.num_ssids
].ssid
=
562 params
.ssids
[params
.num_ssids
].ssid_len
=
565 if (params
.num_ssids
+ 1 >= max_ssids
)
571 if (ssid
== NULL
&& max_ssids
> 1 &&
572 start
!= wpa_s
->conf
->ssid
)
573 ssid
= wpa_s
->conf
->ssid
;
576 for (tssid
= wpa_s
->conf
->ssid
; tssid
; tssid
= tssid
->next
) {
577 if (wpas_network_disabled(tssid
))
579 if ((params
.freqs
|| !freqs_set
) && tssid
->scan_freq
) {
580 int_array_concat(¶ms
.freqs
,
583 os_free(params
.freqs
);
588 int_array_sort_unique(params
.freqs
);
591 if (ssid
&& max_ssids
== 1) {
593 * If the driver is limited to 1 SSID at a time interleave
594 * wildcard SSID scans with specific SSID scans to avoid
595 * waiting a long time for a wildcard scan.
597 if (!wpa_s
->prev_scan_wildcard
) {
598 params
.ssids
[0].ssid
= NULL
;
599 params
.ssids
[0].ssid_len
= 0;
600 wpa_s
->prev_scan_wildcard
= 1;
601 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for "
602 "wildcard SSID (Interleave with specific)");
604 wpa_s
->prev_scan_ssid
= ssid
;
605 wpa_s
->prev_scan_wildcard
= 0;
606 wpa_dbg(wpa_s
, MSG_DEBUG
,
607 "Starting AP scan for specific SSID: %s",
608 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
613 wpa_s
->prev_scan_ssid
= ssid
;
614 wpa_dbg(wpa_s
, MSG_DEBUG
, "Include wildcard SSID in "
618 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
620 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for wildcard "
625 #endif /* CONFIG_P2P */
627 wpa_supplicant_optimize_freqs(wpa_s
, ¶ms
);
628 extra_ie
= wpa_supplicant_extra_ies(wpa_s
, ¶ms
);
631 if (wpa_s
->conf
->hs20
&& wpabuf_resize(&extra_ie
, 6) == 0)
632 wpas_hs20_add_indication(extra_ie
);
633 #endif /* CONFIG_HS20 */
635 if (params
.freqs
== NULL
&& wpa_s
->next_scan_freqs
) {
636 wpa_dbg(wpa_s
, MSG_DEBUG
, "Optimize scan based on previously "
637 "generated frequency list");
638 params
.freqs
= wpa_s
->next_scan_freqs
;
640 os_free(wpa_s
->next_scan_freqs
);
641 wpa_s
->next_scan_freqs
= NULL
;
643 params
.filter_ssids
= wpa_supplicant_build_filter_ssids(
644 wpa_s
->conf
, ¶ms
.num_filter_ssids
);
646 params
.extra_ies
= wpabuf_head(extra_ie
);
647 params
.extra_ies_len
= wpabuf_len(extra_ie
);
651 if (wpa_s
->p2p_in_provisioning
||
652 (wpa_s
->show_group_started
&& wpa_s
->go_params
)) {
654 * The interface may not yet be in P2P mode, so we have to
655 * explicitly request P2P probe to disable CCK rates.
657 params
.p2p_probe
= 1;
659 #endif /* CONFIG_P2P */
661 ret
= wpa_supplicant_trigger_scan(wpa_s
, ¶ms
);
663 wpabuf_free(extra_ie
);
664 os_free(params
.freqs
);
665 os_free(params
.filter_ssids
);
668 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate AP scan");
669 if (prev_state
!= wpa_s
->wpa_state
)
670 wpa_supplicant_set_state(wpa_s
, prev_state
);
671 wpa_supplicant_req_scan(wpa_s
, 1, 0);
677 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
678 * @wpa_s: Pointer to wpa_supplicant data
679 * @sec: Number of seconds after which to scan
680 * @usec: Number of microseconds after which to scan
682 * This function is used to schedule a scan for neighboring access points after
683 * the specified time.
685 void wpa_supplicant_req_scan(struct wpa_supplicant
*wpa_s
, int sec
, int usec
)
687 /* If there's at least one network that should be specifically scanned
688 * then don't cancel the scan and reschedule. Some drivers do
689 * background scanning which generates frequent scan results, and that
690 * causes the specific SSID scan to get continually pushed back and
691 * never happen, which causes hidden APs to never get probe-scanned.
693 if (eloop_is_timeout_registered(wpa_supplicant_scan
, wpa_s
, NULL
) &&
694 wpa_s
->conf
->ap_scan
== 1) {
695 struct wpa_ssid
*ssid
= wpa_s
->conf
->ssid
;
698 if (!wpas_network_disabled(ssid
) && ssid
->scan_ssid
)
703 wpa_dbg(wpa_s
, MSG_DEBUG
, "Not rescheduling scan to "
704 "ensure that specific SSID scans occur");
709 wpa_dbg(wpa_s
, MSG_DEBUG
, "Setting scan request: %d sec %d usec",
711 eloop_cancel_timeout(wpa_supplicant_scan
, wpa_s
, NULL
);
712 eloop_register_timeout(sec
, usec
, wpa_supplicant_scan
, wpa_s
, NULL
);
717 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
718 * @wpa_s: Pointer to wpa_supplicant data
719 * @sec: Number of seconds after which to scan
720 * @usec: Number of microseconds after which to scan
722 * This function is used to schedule periodic scans for neighboring
723 * access points after the specified time.
725 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant
*wpa_s
,
728 if (!wpa_s
->sched_scan_supported
)
731 eloop_register_timeout(sec
, usec
,
732 wpa_supplicant_delayed_sched_scan_timeout
,
740 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
741 * @wpa_s: Pointer to wpa_supplicant data
743 * This function is used to schedule periodic scans for neighboring
744 * access points repeating the scan continuously.
746 int wpa_supplicant_req_sched_scan(struct wpa_supplicant
*wpa_s
)
748 struct wpa_driver_scan_params params
;
749 enum wpa_states prev_state
;
750 struct wpa_ssid
*ssid
;
751 struct wpabuf
*wps_ie
= NULL
;
753 unsigned int max_sched_scan_ssids
;
757 if (!wpa_s
->sched_scan_supported
)
760 if (wpa_s
->max_sched_scan_ssids
> WPAS_MAX_SCAN_SSIDS
)
761 max_sched_scan_ssids
= WPAS_MAX_SCAN_SSIDS
;
763 max_sched_scan_ssids
= wpa_s
->max_sched_scan_ssids
;
764 if (max_sched_scan_ssids
< 1 || wpa_s
->conf
->disable_scan_offload
)
767 if (wpa_s
->sched_scanning
) {
768 wpa_dbg(wpa_s
, MSG_DEBUG
, "Already sched scanning");
773 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
774 if (!wpas_network_disabled(ssid
) && !ssid
->scan_ssid
) {
775 /* Use wildcard SSID to find this network */
777 } else if (!wpas_network_disabled(ssid
) && ssid
->ssid_len
)
781 if (!wpas_network_disabled(ssid
) &&
782 ssid
->key_mgmt
== WPA_KEY_MGMT_WPS
) {
784 * Normal scan is more reliable and faster for WPS
785 * operations and since these are for short periods of
786 * time, the benefit of trying to use sched_scan would
789 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use normal scan instead of "
790 "sched_scan for WPS");
793 #endif /* CONFIG_WPS */
798 if (wpa_s
->normal_scans
< 3 &&
799 (need_ssids
<= wpa_s
->max_scan_ssids
||
800 wpa_s
->max_scan_ssids
>= (int) max_sched_scan_ssids
)) {
802 * When normal scan can speed up operations, use that for the
803 * first operations before starting the sched_scan to allow
804 * user space sleep more. We do this only if the normal scan
805 * has functionality that is suitable for this or if the
806 * sched_scan does not have better support for multiple SSIDs.
808 wpa_dbg(wpa_s
, MSG_DEBUG
, "Use normal scan instead of "
809 "sched_scan for initial scans (normal_scans=%d)",
810 wpa_s
->normal_scans
);
814 os_memset(¶ms
, 0, sizeof(params
));
816 /* If we can't allocate space for the filters, we just don't filter */
817 params
.filter_ssids
= os_zalloc(wpa_s
->max_match_sets
*
818 sizeof(struct wpa_driver_scan_filter
));
820 prev_state
= wpa_s
->wpa_state
;
821 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
822 wpa_s
->wpa_state
== WPA_INACTIVE
)
823 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
825 /* Find the starting point from which to continue scanning */
826 ssid
= wpa_s
->conf
->ssid
;
827 if (wpa_s
->prev_sched_ssid
) {
829 if (ssid
== wpa_s
->prev_sched_ssid
) {
837 if (!ssid
|| !wpa_s
->prev_sched_ssid
) {
838 wpa_dbg(wpa_s
, MSG_DEBUG
, "Beginning of SSID list");
840 wpa_s
->sched_scan_interval
= 10;
841 wpa_s
->sched_scan_timeout
= max_sched_scan_ssids
* 2;
842 wpa_s
->first_sched_scan
= 1;
843 ssid
= wpa_s
->conf
->ssid
;
844 wpa_s
->prev_sched_ssid
= ssid
;
848 wpa_dbg(wpa_s
, MSG_DEBUG
, "Add wildcard SSID to sched_scan");
853 if (wpas_network_disabled(ssid
))
856 if (params
.num_filter_ssids
< wpa_s
->max_match_sets
&&
857 params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
) {
858 wpa_dbg(wpa_s
, MSG_DEBUG
, "add to filter ssid: %s",
859 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
860 os_memcpy(params
.filter_ssids
[params
.num_filter_ssids
].ssid
,
861 ssid
->ssid
, ssid
->ssid_len
);
862 params
.filter_ssids
[params
.num_filter_ssids
].ssid_len
=
864 params
.num_filter_ssids
++;
865 } else if (params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
)
867 wpa_dbg(wpa_s
, MSG_DEBUG
, "Not enough room for SSID "
868 "filter for sched_scan - drop filter");
869 os_free(params
.filter_ssids
);
870 params
.filter_ssids
= NULL
;
871 params
.num_filter_ssids
= 0;
874 if (ssid
->scan_ssid
&& ssid
->ssid
&& ssid
->ssid_len
) {
875 if (params
.num_ssids
== max_sched_scan_ssids
)
876 break; /* only room for broadcast SSID */
877 wpa_dbg(wpa_s
, MSG_DEBUG
,
878 "add to active scan ssid: %s",
879 wpa_ssid_txt(ssid
->ssid
, ssid
->ssid_len
));
880 params
.ssids
[params
.num_ssids
].ssid
=
882 params
.ssids
[params
.num_ssids
].ssid_len
=
885 if (params
.num_ssids
>= max_sched_scan_ssids
) {
886 wpa_s
->prev_sched_ssid
= ssid
;
890 (wpas_network_disabled(ssid
) ||
897 wpa_s
->prev_sched_ssid
= ssid
;
901 if (params
.num_filter_ssids
== 0) {
902 os_free(params
.filter_ssids
);
903 params
.filter_ssids
= NULL
;
907 wps_ie
= wpa_supplicant_extra_ies(wpa_s
, ¶ms
);
909 if (ssid
|| !wpa_s
->first_sched_scan
) {
910 wpa_dbg(wpa_s
, MSG_DEBUG
,
911 "Starting sched scan: interval %d timeout %d",
912 wpa_s
->sched_scan_interval
, wpa_s
->sched_scan_timeout
);
914 wpa_dbg(wpa_s
, MSG_DEBUG
,
915 "Starting sched scan: interval %d (no timeout)",
916 wpa_s
->sched_scan_interval
);
919 ret
= wpa_supplicant_start_sched_scan(wpa_s
, ¶ms
,
920 wpa_s
->sched_scan_interval
);
922 os_free(params
.filter_ssids
);
924 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate sched scan");
925 if (prev_state
!= wpa_s
->wpa_state
)
926 wpa_supplicant_set_state(wpa_s
, prev_state
);
930 /* If we have more SSIDs to scan, add a timeout so we scan them too */
931 if (ssid
|| !wpa_s
->first_sched_scan
) {
932 wpa_s
->sched_scan_timed_out
= 0;
933 eloop_register_timeout(wpa_s
->sched_scan_timeout
, 0,
934 wpa_supplicant_sched_scan_timeout
,
936 wpa_s
->first_sched_scan
= 0;
937 wpa_s
->sched_scan_timeout
/= 2;
938 wpa_s
->sched_scan_interval
*= 2;
946 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
947 * @wpa_s: Pointer to wpa_supplicant data
949 * This function is used to cancel a scan request scheduled with
950 * wpa_supplicant_req_scan().
952 void wpa_supplicant_cancel_scan(struct wpa_supplicant
*wpa_s
)
954 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling scan request");
955 eloop_cancel_timeout(wpa_supplicant_scan
, wpa_s
, NULL
);
960 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
961 * @wpa_s: Pointer to wpa_supplicant data
963 * This function is used to stop a periodic scheduled scan.
965 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant
*wpa_s
)
967 if (!wpa_s
->sched_scanning
)
970 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling sched scan");
971 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout
, wpa_s
, NULL
);
972 wpa_supplicant_stop_sched_scan(wpa_s
);
976 void wpa_supplicant_notify_scanning(struct wpa_supplicant
*wpa_s
,
979 if (wpa_s
->scanning
!= scanning
) {
980 wpa_s
->scanning
= scanning
;
981 wpas_notify_scanning(wpa_s
);
986 static int wpa_scan_get_max_rate(const struct wpa_scan_res
*res
)
992 ie
= wpa_scan_get_ie(res
, WLAN_EID_SUPP_RATES
);
993 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
994 if ((ie
[i
+ 2] & 0x7f) > rate
)
995 rate
= ie
[i
+ 2] & 0x7f;
998 ie
= wpa_scan_get_ie(res
, WLAN_EID_EXT_SUPP_RATES
);
999 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1000 if ((ie
[i
+ 2] & 0x7f) > rate
)
1001 rate
= ie
[i
+ 2] & 0x7f;
1008 const u8
* wpa_scan_get_ie(const struct wpa_scan_res
*res
, u8 ie
)
1010 const u8
*end
, *pos
;
1012 pos
= (const u8
*) (res
+ 1);
1013 end
= pos
+ res
->ie_len
;
1015 while (pos
+ 1 < end
) {
1016 if (pos
+ 2 + pos
[1] > end
)
1027 const u8
* wpa_scan_get_vendor_ie(const struct wpa_scan_res
*res
,
1030 const u8
*end
, *pos
;
1032 pos
= (const u8
*) (res
+ 1);
1033 end
= pos
+ res
->ie_len
;
1035 while (pos
+ 1 < end
) {
1036 if (pos
+ 2 + pos
[1] > end
)
1038 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1039 vendor_type
== WPA_GET_BE32(&pos
[2]))
1048 struct wpabuf
* wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res
*res
,
1052 const u8
*end
, *pos
;
1054 buf
= wpabuf_alloc(res
->ie_len
);
1058 pos
= (const u8
*) (res
+ 1);
1059 end
= pos
+ res
->ie_len
;
1061 while (pos
+ 1 < end
) {
1062 if (pos
+ 2 + pos
[1] > end
)
1064 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1065 vendor_type
== WPA_GET_BE32(&pos
[2]))
1066 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
1070 if (wpabuf_len(buf
) == 0) {
1079 struct wpabuf
* wpa_scan_get_vendor_ie_multi_beacon(
1080 const struct wpa_scan_res
*res
, u32 vendor_type
)
1083 const u8
*end
, *pos
;
1085 if (res
->beacon_ie_len
== 0)
1087 buf
= wpabuf_alloc(res
->beacon_ie_len
);
1091 pos
= (const u8
*) (res
+ 1);
1093 end
= pos
+ res
->beacon_ie_len
;
1095 while (pos
+ 1 < end
) {
1096 if (pos
+ 2 + pos
[1] > end
)
1098 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1099 vendor_type
== WPA_GET_BE32(&pos
[2]))
1100 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
1104 if (wpabuf_len(buf
) == 0) {
1114 * Channels with a great SNR can operate at full rate. What is a great SNR?
1115 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1116 * rule of thumb is that any SNR above 20 is good." This one
1117 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1118 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1119 * conservative value.
1121 #define GREAT_SNR 30
1123 /* Compare function for sorting scan results. Return >0 if @b is considered
1125 static int wpa_scan_result_compar(const void *a
, const void *b
)
1127 #define IS_5GHZ(n) (n > 4000)
1128 #define MIN(a,b) a < b ? a : b
1129 struct wpa_scan_res
**_wa
= (void *) a
;
1130 struct wpa_scan_res
**_wb
= (void *) b
;
1131 struct wpa_scan_res
*wa
= *_wa
;
1132 struct wpa_scan_res
*wb
= *_wb
;
1133 int wpa_a
, wpa_b
, maxrate_a
, maxrate_b
;
1136 /* WPA/WPA2 support preferred */
1137 wpa_a
= wpa_scan_get_vendor_ie(wa
, WPA_IE_VENDOR_TYPE
) != NULL
||
1138 wpa_scan_get_ie(wa
, WLAN_EID_RSN
) != NULL
;
1139 wpa_b
= wpa_scan_get_vendor_ie(wb
, WPA_IE_VENDOR_TYPE
) != NULL
||
1140 wpa_scan_get_ie(wb
, WLAN_EID_RSN
) != NULL
;
1142 if (wpa_b
&& !wpa_a
)
1144 if (!wpa_b
&& wpa_a
)
1147 /* privacy support preferred */
1148 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) == 0 &&
1149 (wb
->caps
& IEEE80211_CAP_PRIVACY
))
1151 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) &&
1152 (wb
->caps
& IEEE80211_CAP_PRIVACY
) == 0)
1155 if ((wa
->flags
& wb
->flags
& WPA_SCAN_LEVEL_DBM
) &&
1156 !((wa
->flags
| wb
->flags
) & WPA_SCAN_NOISE_INVALID
)) {
1157 snr_a
= MIN(wa
->level
- wa
->noise
, GREAT_SNR
);
1158 snr_b
= MIN(wb
->level
- wb
->noise
, GREAT_SNR
);
1160 /* Not suitable information to calculate SNR, so use level */
1165 /* best/max rate preferred if SNR close enough */
1166 if ((snr_a
&& snr_b
&& abs(snr_b
- snr_a
) < 5) ||
1167 (wa
->qual
&& wb
->qual
&& abs(wb
->qual
- wa
->qual
) < 10)) {
1168 maxrate_a
= wpa_scan_get_max_rate(wa
);
1169 maxrate_b
= wpa_scan_get_max_rate(wb
);
1170 if (maxrate_a
!= maxrate_b
)
1171 return maxrate_b
- maxrate_a
;
1172 if (IS_5GHZ(wa
->freq
) ^ IS_5GHZ(wb
->freq
))
1173 return IS_5GHZ(wa
->freq
) ? -1 : 1;
1176 /* use freq for channel preference */
1178 /* all things being equal, use SNR; if SNRs are
1179 * identical, use quality values since some drivers may only report
1180 * that value and leave the signal level zero */
1182 return wb
->qual
- wa
->qual
;
1183 return snr_b
- snr_a
;
1190 /* Compare function for sorting scan results when searching a WPS AP for
1191 * provisioning. Return >0 if @b is considered better. */
1192 static int wpa_scan_result_wps_compar(const void *a
, const void *b
)
1194 struct wpa_scan_res
**_wa
= (void *) a
;
1195 struct wpa_scan_res
**_wb
= (void *) b
;
1196 struct wpa_scan_res
*wa
= *_wa
;
1197 struct wpa_scan_res
*wb
= *_wb
;
1198 int uses_wps_a
, uses_wps_b
;
1199 struct wpabuf
*wps_a
, *wps_b
;
1202 /* Optimization - check WPS IE existence before allocated memory and
1203 * doing full reassembly. */
1204 uses_wps_a
= wpa_scan_get_vendor_ie(wa
, WPS_IE_VENDOR_TYPE
) != NULL
;
1205 uses_wps_b
= wpa_scan_get_vendor_ie(wb
, WPS_IE_VENDOR_TYPE
) != NULL
;
1206 if (uses_wps_a
&& !uses_wps_b
)
1208 if (!uses_wps_a
&& uses_wps_b
)
1211 if (uses_wps_a
&& uses_wps_b
) {
1212 wps_a
= wpa_scan_get_vendor_ie_multi(wa
, WPS_IE_VENDOR_TYPE
);
1213 wps_b
= wpa_scan_get_vendor_ie_multi(wb
, WPS_IE_VENDOR_TYPE
);
1214 res
= wps_ap_priority_compar(wps_a
, wps_b
);
1222 * Do not use current AP security policy as a sorting criteria during
1223 * WPS provisioning step since the AP may get reconfigured at the
1224 * completion of provisioning.
1227 /* all things being equal, use signal level; if signal levels are
1228 * identical, use quality values since some drivers may only report
1229 * that value and leave the signal level zero */
1230 if (wb
->level
== wa
->level
)
1231 return wb
->qual
- wa
->qual
;
1232 return wb
->level
- wa
->level
;
1234 #endif /* CONFIG_WPS */
1237 static void dump_scan_res(struct wpa_scan_results
*scan_res
)
1239 #ifndef CONFIG_NO_STDOUT_DEBUG
1242 if (scan_res
->res
== NULL
|| scan_res
->num
== 0)
1245 wpa_printf(MSG_EXCESSIVE
, "Sorted scan results");
1247 for (i
= 0; i
< scan_res
->num
; i
++) {
1248 struct wpa_scan_res
*r
= scan_res
->res
[i
];
1249 if ((r
->flags
& (WPA_SCAN_LEVEL_DBM
| WPA_SCAN_NOISE_INVALID
))
1250 == WPA_SCAN_LEVEL_DBM
) {
1251 int snr
= r
->level
- r
->noise
;
1252 wpa_printf(MSG_EXCESSIVE
, MACSTR
" freq=%d qual=%d "
1253 "noise=%d level=%d snr=%d%s flags=0x%x",
1254 MAC2STR(r
->bssid
), r
->freq
, r
->qual
,
1255 r
->noise
, r
->level
, snr
,
1256 snr
>= GREAT_SNR
? "*" : "", r
->flags
);
1258 wpa_printf(MSG_EXCESSIVE
, MACSTR
" freq=%d qual=%d "
1259 "noise=%d level=%d flags=0x%x",
1260 MAC2STR(r
->bssid
), r
->freq
, r
->qual
,
1261 r
->noise
, r
->level
, r
->flags
);
1264 #endif /* CONFIG_NO_STDOUT_DEBUG */
1268 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant
*wpa_s
,
1273 if (wpa_s
->bssid_filter
== NULL
)
1276 for (i
= 0; i
< wpa_s
->bssid_filter_count
; i
++) {
1277 if (os_memcmp(wpa_s
->bssid_filter
+ i
* ETH_ALEN
, bssid
,
1286 static void filter_scan_res(struct wpa_supplicant
*wpa_s
,
1287 struct wpa_scan_results
*res
)
1291 if (wpa_s
->bssid_filter
== NULL
)
1294 for (i
= 0, j
= 0; i
< res
->num
; i
++) {
1295 if (wpa_supplicant_filter_bssid_match(wpa_s
,
1296 res
->res
[i
]->bssid
)) {
1297 res
->res
[j
++] = res
->res
[i
];
1299 os_free(res
->res
[i
]);
1304 if (res
->num
!= j
) {
1305 wpa_printf(MSG_DEBUG
, "Filtered out %d scan results",
1306 (int) (res
->num
- j
));
1313 * wpa_supplicant_get_scan_results - Get scan results
1314 * @wpa_s: Pointer to wpa_supplicant data
1315 * @info: Information about what was scanned or %NULL if not available
1316 * @new_scan: Whether a new scan was performed
1317 * Returns: Scan results, %NULL on failure
1319 * This function request the current scan results from the driver and updates
1320 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1321 * results with wpa_scan_results_free().
1323 struct wpa_scan_results
*
1324 wpa_supplicant_get_scan_results(struct wpa_supplicant
*wpa_s
,
1325 struct scan_info
*info
, int new_scan
)
1327 struct wpa_scan_results
*scan_res
;
1329 int (*compar
)(const void *, const void *) = wpa_scan_result_compar
;
1331 scan_res
= wpa_drv_get_scan_results2(wpa_s
);
1332 if (scan_res
== NULL
) {
1333 wpa_dbg(wpa_s
, MSG_DEBUG
, "Failed to get scan results");
1336 filter_scan_res(wpa_s
, scan_res
);
1339 if (wpas_wps_in_progress(wpa_s
)) {
1340 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Order scan results with WPS "
1341 "provisioning rules");
1342 compar
= wpa_scan_result_wps_compar
;
1344 #endif /* CONFIG_WPS */
1346 qsort(scan_res
->res
, scan_res
->num
, sizeof(struct wpa_scan_res
*),
1348 dump_scan_res(scan_res
);
1350 wpa_bss_update_start(wpa_s
);
1351 for (i
= 0; i
< scan_res
->num
; i
++)
1352 wpa_bss_update_scan_res(wpa_s
, scan_res
->res
[i
]);
1353 wpa_bss_update_end(wpa_s
, info
, new_scan
);
1359 int wpa_supplicant_update_scan_results(struct wpa_supplicant
*wpa_s
)
1361 struct wpa_scan_results
*scan_res
;
1362 scan_res
= wpa_supplicant_get_scan_results(wpa_s
, NULL
, 0);
1363 if (scan_res
== NULL
)
1365 wpa_scan_results_free(scan_res
);