2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
21 #include "wpa_supplicant_i.h"
23 #include "wps_supplicant.h"
24 #include "p2p_supplicant.h"
31 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant
*wpa_s
)
33 struct wpa_ssid
*ssid
;
34 union wpa_event_data data
;
36 ssid
= wpa_supplicant_get_ssid(wpa_s
);
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
);
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
);
54 static int wpas_wps_in_use(struct wpa_supplicant
*wpa_s
,
55 enum wps_request_type
*req_type
)
57 struct wpa_ssid
*ssid
;
60 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
61 if (!(ssid
->key_mgmt
& WPA_KEY_MGMT_WPS
))
65 *req_type
= wpas_wps_get_req_type(ssid
);
66 if (!ssid
->eap
.phase1
)
69 if (os_strstr(ssid
->eap
.phase1
, "pbc=1"))
74 wpa_s
->wps
->dev
.p2p
= 1;
77 *req_type
= WPS_REQ_ENROLLEE_INFO
;
79 #endif /* CONFIG_P2P */
83 #endif /* CONFIG_WPS */
86 int wpa_supplicant_enabled_networks(struct wpa_config
*conf
)
88 struct wpa_ssid
*ssid
= conf
->ssid
;
99 static void wpa_supplicant_assoc_try(struct wpa_supplicant
*wpa_s
,
100 struct wpa_ssid
*ssid
)
108 /* ap_scan=2 mode - try to associate with each SSID. */
110 wpa_dbg(wpa_s
, MSG_DEBUG
, "wpa_supplicant_assoc_try: Reached "
111 "end of scan list - go back to beginning");
112 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
113 wpa_supplicant_req_scan(wpa_s
, 0, 0);
117 /* Continue from the next SSID on the next attempt. */
118 wpa_s
->prev_scan_ssid
= ssid
;
120 /* Start from the beginning of the SSID list. */
121 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
123 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
127 static int int_array_len(const int *a
)
130 for (i
= 0; a
&& a
[i
]; i
++)
136 static void int_array_concat(int **res
, const int *a
)
141 reslen
= int_array_len(*res
);
142 alen
= int_array_len(a
);
144 n
= os_realloc(*res
, (reslen
+ alen
+ 1) * sizeof(int));
150 for (i
= 0; i
<= alen
; i
++)
151 n
[reslen
+ i
] = a
[i
];
156 static int freq_cmp(const void *a
, const void *b
)
169 static void int_array_sort_unique(int *a
)
177 alen
= int_array_len(a
);
178 qsort(a
, alen
, sizeof(int), freq_cmp
);
182 while (a
[i
] && a
[j
]) {
195 int wpa_supplicant_trigger_scan(struct wpa_supplicant
*wpa_s
,
196 struct wpa_driver_scan_params
*params
)
200 wpa_supplicant_notify_scanning(wpa_s
, 1);
202 ret
= wpa_drv_scan(wpa_s
, params
);
204 wpa_supplicant_notify_scanning(wpa_s
, 0);
205 wpas_notify_scan_done(wpa_s
, 0);
214 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
216 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
218 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting delayed sched scan");
220 if (wpa_supplicant_req_sched_scan(wpa_s
))
221 wpa_supplicant_req_scan(wpa_s
, 0, 0);
226 wpa_supplicant_sched_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
228 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
230 wpa_dbg(wpa_s
, MSG_DEBUG
, "Sched scan timeout - stopping it");
232 wpa_s
->sched_scan_timed_out
= 1;
233 wpa_supplicant_cancel_sched_scan(wpa_s
);
238 wpa_supplicant_start_sched_scan(struct wpa_supplicant
*wpa_s
,
239 struct wpa_driver_scan_params
*params
,
244 wpa_supplicant_notify_scanning(wpa_s
, 1);
245 ret
= wpa_drv_sched_scan(wpa_s
, params
, interval
* 1000);
247 wpa_supplicant_notify_scanning(wpa_s
, 0);
249 wpa_s
->sched_scanning
= 1;
255 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant
*wpa_s
)
259 ret
= wpa_drv_stop_sched_scan(wpa_s
);
261 wpa_dbg(wpa_s
, MSG_DEBUG
, "stopping sched_scan failed!");
262 /* TODO: what to do if stopping fails? */
270 static struct wpa_driver_scan_filter
*
271 wpa_supplicant_build_filter_ssids(struct wpa_config
*conf
, size_t *num_ssids
)
273 struct wpa_driver_scan_filter
*ssids
;
274 struct wpa_ssid
*ssid
;
278 if (!conf
->filter_ssids
)
281 for (count
= 0, ssid
= conf
->ssid
; ssid
; ssid
= ssid
->next
) {
282 if (ssid
->ssid
&& ssid
->ssid_len
)
287 ssids
= os_zalloc(count
* sizeof(struct wpa_driver_scan_filter
));
291 for (ssid
= conf
->ssid
; ssid
; ssid
= ssid
->next
) {
292 if (!ssid
->ssid
|| !ssid
->ssid_len
)
294 os_memcpy(ssids
[*num_ssids
].ssid
, ssid
->ssid
, ssid
->ssid_len
);
295 ssids
[*num_ssids
].ssid_len
= ssid
->ssid_len
;
303 static void wpa_supplicant_optimize_freqs(
304 struct wpa_supplicant
*wpa_s
, struct wpa_driver_scan_params
*params
)
307 if (params
->freqs
== NULL
&& wpa_s
->p2p_in_provisioning
&&
309 /* Optimize provisioning state scan based on GO information */
310 if (wpa_s
->p2p_in_provisioning
< 5 &&
311 wpa_s
->go_params
->freq
> 0) {
312 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only GO "
313 "preferred frequency %d MHz",
314 wpa_s
->go_params
->freq
);
315 params
->freqs
= os_zalloc(2 * sizeof(int));
317 params
->freqs
[0] = wpa_s
->go_params
->freq
;
318 } else if (wpa_s
->p2p_in_provisioning
< 8 &&
319 wpa_s
->go_params
->freq_list
[0]) {
320 wpa_dbg(wpa_s
, MSG_DEBUG
, "P2P: Scan only common "
322 int_array_concat(¶ms
->freqs
,
323 wpa_s
->go_params
->freq_list
);
325 int_array_sort_unique(params
->freqs
);
327 wpa_s
->p2p_in_provisioning
++;
329 #endif /* CONFIG_P2P */
332 if (params
->freqs
== NULL
&& wpa_s
->after_wps
&& wpa_s
->wps_freq
) {
334 * Optimize post-provisioning scan based on channel used
335 * during provisioning.
337 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Scan only frequency %u MHz "
338 "that was used during provisioning", wpa_s
->wps_freq
);
339 params
->freqs
= os_zalloc(2 * sizeof(int));
341 params
->freqs
[0] = wpa_s
->wps_freq
;
345 #endif /* CONFIG_WPS */
349 #ifdef CONFIG_INTERWORKING
350 static void wpas_add_interworking_elements(struct wpa_supplicant
*wpa_s
,
353 if (wpa_s
->conf
->interworking
== 0)
356 wpabuf_put_u8(buf
, WLAN_EID_EXT_CAPAB
);
357 wpabuf_put_u8(buf
, 4);
358 wpabuf_put_u8(buf
, 0x00);
359 wpabuf_put_u8(buf
, 0x00);
360 wpabuf_put_u8(buf
, 0x00);
361 wpabuf_put_u8(buf
, 0x80); /* Bit 31 - Interworking */
363 wpabuf_put_u8(buf
, WLAN_EID_INTERWORKING
);
364 wpabuf_put_u8(buf
, is_zero_ether_addr(wpa_s
->conf
->hessid
) ? 1 :
366 wpabuf_put_u8(buf
, wpa_s
->conf
->access_network_type
);
368 if (!is_zero_ether_addr(wpa_s
->conf
->hessid
))
369 wpabuf_put_data(buf
, wpa_s
->conf
->hessid
, ETH_ALEN
);
371 #endif /* CONFIG_INTERWORKING */
374 static struct wpabuf
*
375 wpa_supplicant_extra_ies(struct wpa_supplicant
*wpa_s
,
376 struct wpa_driver_scan_params
*params
)
378 struct wpabuf
*extra_ie
= NULL
;
381 enum wps_request_type req_type
= WPS_REQ_ENROLLEE_INFO
;
382 #endif /* CONFIG_WPS */
384 #ifdef CONFIG_INTERWORKING
385 if (wpa_s
->conf
->interworking
&&
386 wpabuf_resize(&extra_ie
, 100) == 0)
387 wpas_add_interworking_elements(wpa_s
, extra_ie
);
388 #endif /* CONFIG_INTERWORKING */
391 wps
= wpas_wps_in_use(wpa_s
, &req_type
);
394 struct wpabuf
*wps_ie
;
395 wps_ie
= wps_build_probe_req_ie(wps
== 2, &wpa_s
->wps
->dev
,
396 wpa_s
->wps
->uuid
, req_type
,
399 if (wpabuf_resize(&extra_ie
, wpabuf_len(wps_ie
)) == 0)
400 wpabuf_put_buf(extra_ie
, wps_ie
);
407 size_t ielen
= p2p_scan_ie_buf_len(wpa_s
->global
->p2p
);
408 if (wpabuf_resize(&extra_ie
, ielen
) == 0)
409 wpas_p2p_scan_ie(wpa_s
, extra_ie
);
411 #endif /* CONFIG_P2P */
413 #endif /* CONFIG_WPS */
419 static void wpa_supplicant_scan(void *eloop_ctx
, void *timeout_ctx
)
421 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
422 struct wpa_ssid
*ssid
;
423 int scan_req
= 0, ret
;
424 struct wpabuf
*extra_ie
;
425 struct wpa_driver_scan_params params
;
427 enum wpa_states prev_state
;
429 if (wpa_s
->wpa_state
== WPA_INTERFACE_DISABLED
) {
430 wpa_dbg(wpa_s
, MSG_DEBUG
, "Skip scan - interface disabled");
434 if (wpa_s
->disconnected
&& !wpa_s
->scan_req
) {
435 wpa_supplicant_set_state(wpa_s
, WPA_DISCONNECTED
);
439 if (!wpa_supplicant_enabled_networks(wpa_s
->conf
) &&
441 wpa_dbg(wpa_s
, MSG_DEBUG
, "No enabled networks - do not scan");
442 wpa_supplicant_set_state(wpa_s
, WPA_INACTIVE
);
446 if (wpa_s
->conf
->ap_scan
!= 0 &&
447 (wpa_s
->drv_flags
& WPA_DRIVER_FLAGS_WIRED
)) {
448 wpa_dbg(wpa_s
, MSG_DEBUG
, "Using wired authentication - "
449 "overriding ap_scan configuration");
450 wpa_s
->conf
->ap_scan
= 0;
451 wpas_notify_ap_scan_changed(wpa_s
);
454 if (wpa_s
->conf
->ap_scan
== 0) {
455 wpa_supplicant_gen_assoc_event(wpa_s
);
460 if (wpas_p2p_in_progress(wpa_s
)) {
461 if (wpa_s
->wpa_state
== WPA_SCANNING
) {
462 wpa_dbg(wpa_s
, MSG_DEBUG
, "Delay station mode scan "
463 "while P2P operation is in progress");
464 wpa_supplicant_req_scan(wpa_s
, 5, 0);
466 wpa_dbg(wpa_s
, MSG_DEBUG
, "Do not request scan while "
467 "P2P operation is in progress");
471 #endif /* CONFIG_P2P */
473 if (wpa_s
->conf
->ap_scan
== 2)
476 max_ssids
= wpa_s
->max_scan_ssids
;
477 if (max_ssids
> WPAS_MAX_SCAN_SSIDS
)
478 max_ssids
= WPAS_MAX_SCAN_SSIDS
;
481 scan_req
= wpa_s
->scan_req
;
484 os_memset(¶ms
, 0, sizeof(params
));
486 prev_state
= wpa_s
->wpa_state
;
487 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
488 wpa_s
->wpa_state
== WPA_INACTIVE
)
489 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
491 if (scan_req
!= 2 && wpa_s
->connect_without_scan
) {
492 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
493 if (ssid
== wpa_s
->connect_without_scan
)
496 wpa_s
->connect_without_scan
= NULL
;
498 wpa_printf(MSG_DEBUG
, "Start a pre-selected network "
499 "without scan step");
500 wpa_supplicant_associate(wpa_s
, NULL
, ssid
);
505 /* Find the starting point from which to continue scanning */
506 ssid
= wpa_s
->conf
->ssid
;
507 if (wpa_s
->prev_scan_ssid
!= WILDCARD_SSID_SCAN
) {
509 if (ssid
== wpa_s
->prev_scan_ssid
) {
517 if (scan_req
!= 2 && wpa_s
->conf
->ap_scan
== 2) {
518 wpa_s
->connect_without_scan
= NULL
;
519 wpa_supplicant_assoc_try(wpa_s
, ssid
);
521 } else if (wpa_s
->conf
->ap_scan
== 2) {
523 * User-initiated scan request in ap_scan == 2; scan with
528 struct wpa_ssid
*start
= ssid
, *tssid
;
530 if (ssid
== NULL
&& max_ssids
> 1)
531 ssid
= wpa_s
->conf
->ssid
;
533 if (!ssid
->disabled
&& ssid
->scan_ssid
) {
534 wpa_hexdump_ascii(MSG_DEBUG
, "Scan SSID",
535 ssid
->ssid
, ssid
->ssid_len
);
536 params
.ssids
[params
.num_ssids
].ssid
=
538 params
.ssids
[params
.num_ssids
].ssid_len
=
541 if (params
.num_ssids
+ 1 >= max_ssids
)
547 if (ssid
== NULL
&& max_ssids
> 1 &&
548 start
!= wpa_s
->conf
->ssid
)
549 ssid
= wpa_s
->conf
->ssid
;
552 for (tssid
= wpa_s
->conf
->ssid
; tssid
; tssid
= tssid
->next
) {
555 if ((params
.freqs
|| !freqs_set
) && tssid
->scan_freq
) {
556 int_array_concat(¶ms
.freqs
,
559 os_free(params
.freqs
);
564 int_array_sort_unique(params
.freqs
);
568 wpa_s
->prev_scan_ssid
= ssid
;
570 wpa_dbg(wpa_s
, MSG_DEBUG
, "Include wildcard SSID in "
574 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for specific "
577 wpa_s
->prev_scan_ssid
= WILDCARD_SSID_SCAN
;
579 wpa_dbg(wpa_s
, MSG_DEBUG
, "Starting AP scan for wildcard "
583 wpa_supplicant_optimize_freqs(wpa_s
, ¶ms
);
584 extra_ie
= wpa_supplicant_extra_ies(wpa_s
, ¶ms
);
586 if (params
.freqs
== NULL
&& wpa_s
->next_scan_freqs
) {
587 wpa_dbg(wpa_s
, MSG_DEBUG
, "Optimize scan based on previously "
588 "generated frequency list");
589 params
.freqs
= wpa_s
->next_scan_freqs
;
591 os_free(wpa_s
->next_scan_freqs
);
592 wpa_s
->next_scan_freqs
= NULL
;
594 params
.filter_ssids
= wpa_supplicant_build_filter_ssids(
595 wpa_s
->conf
, ¶ms
.num_filter_ssids
);
597 params
.extra_ies
= wpabuf_head(extra_ie
);
598 params
.extra_ies_len
= wpabuf_len(extra_ie
);
602 if (wpa_s
->p2p_in_provisioning
) {
604 * The interface may not yet be in P2P mode, so we have to
605 * explicitly request P2P probe to disable CCK rates.
607 params
.p2p_probe
= 1;
609 #endif /* CONFIG_P2P */
611 ret
= wpa_supplicant_trigger_scan(wpa_s
, ¶ms
);
613 wpabuf_free(extra_ie
);
614 os_free(params
.freqs
);
615 os_free(params
.filter_ssids
);
618 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate AP scan");
619 if (prev_state
!= wpa_s
->wpa_state
)
620 wpa_supplicant_set_state(wpa_s
, prev_state
);
621 wpa_supplicant_req_scan(wpa_s
, 1, 0);
627 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
628 * @wpa_s: Pointer to wpa_supplicant data
629 * @sec: Number of seconds after which to scan
630 * @usec: Number of microseconds after which to scan
632 * This function is used to schedule a scan for neighboring access points after
633 * the specified time.
635 void wpa_supplicant_req_scan(struct wpa_supplicant
*wpa_s
, int sec
, int usec
)
637 /* If there's at least one network that should be specifically scanned
638 * then don't cancel the scan and reschedule. Some drivers do
639 * background scanning which generates frequent scan results, and that
640 * causes the specific SSID scan to get continually pushed back and
641 * never happen, which causes hidden APs to never get probe-scanned.
643 if (eloop_is_timeout_registered(wpa_supplicant_scan
, wpa_s
, NULL
) &&
644 wpa_s
->conf
->ap_scan
== 1) {
645 struct wpa_ssid
*ssid
= wpa_s
->conf
->ssid
;
648 if (!ssid
->disabled
&& ssid
->scan_ssid
)
653 wpa_dbg(wpa_s
, MSG_DEBUG
, "Not rescheduling scan to "
654 "ensure that specific SSID scans occur");
659 wpa_dbg(wpa_s
, MSG_DEBUG
, "Setting scan request: %d sec %d usec",
661 eloop_cancel_timeout(wpa_supplicant_scan
, wpa_s
, NULL
);
662 eloop_register_timeout(sec
, usec
, wpa_supplicant_scan
, wpa_s
, NULL
);
667 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
668 * @wpa_s: Pointer to wpa_supplicant data
669 * @sec: Number of seconds after which to scan
670 * @usec: Number of microseconds after which to scan
672 * This function is used to schedule periodic scans for neighboring
673 * access points after the specified time.
675 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant
*wpa_s
,
678 if (!wpa_s
->sched_scan_supported
)
681 eloop_register_timeout(sec
, usec
,
682 wpa_supplicant_delayed_sched_scan_timeout
,
690 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
691 * @wpa_s: Pointer to wpa_supplicant data
693 * This function is used to schedule periodic scans for neighboring
694 * access points repeating the scan continuously.
696 int wpa_supplicant_req_sched_scan(struct wpa_supplicant
*wpa_s
)
698 struct wpa_driver_scan_params params
;
699 enum wpa_states prev_state
;
700 struct wpa_ssid
*ssid
;
701 struct wpabuf
*wps_ie
= NULL
;
703 unsigned int max_sched_scan_ssids
;
705 if (!wpa_s
->sched_scan_supported
)
708 if (wpa_s
->max_sched_scan_ssids
> WPAS_MAX_SCAN_SSIDS
)
709 max_sched_scan_ssids
= WPAS_MAX_SCAN_SSIDS
;
711 max_sched_scan_ssids
= wpa_s
->max_sched_scan_ssids
;
713 if (wpa_s
->sched_scanning
)
716 os_memset(¶ms
, 0, sizeof(params
));
718 /* If we can't allocate space for the filters, we just don't filter */
719 params
.filter_ssids
= os_zalloc(wpa_s
->max_match_sets
*
720 sizeof(struct wpa_driver_scan_filter
));
722 prev_state
= wpa_s
->wpa_state
;
723 if (wpa_s
->wpa_state
== WPA_DISCONNECTED
||
724 wpa_s
->wpa_state
== WPA_INACTIVE
)
725 wpa_supplicant_set_state(wpa_s
, WPA_SCANNING
);
727 /* Find the starting point from which to continue scanning */
728 ssid
= wpa_s
->conf
->ssid
;
729 if (wpa_s
->prev_sched_ssid
) {
731 if (ssid
== wpa_s
->prev_sched_ssid
) {
739 if (!ssid
|| !wpa_s
->prev_sched_ssid
) {
740 wpa_dbg(wpa_s
, MSG_DEBUG
, "Beginning of SSID list");
742 wpa_s
->sched_scan_interval
= 2;
743 wpa_s
->sched_scan_timeout
= max_sched_scan_ssids
* 2;
744 wpa_s
->first_sched_scan
= 1;
745 ssid
= wpa_s
->conf
->ssid
;
746 wpa_s
->prev_sched_ssid
= ssid
;
750 if (ssid
->disabled
) {
751 wpa_s
->prev_sched_ssid
= ssid
;
756 if (params
.filter_ssids
&& ssid
->ssid
&& ssid
->ssid_len
) {
757 os_memcpy(params
.filter_ssids
[params
.num_filter_ssids
].ssid
,
758 ssid
->ssid
, ssid
->ssid_len
);
759 params
.filter_ssids
[params
.num_filter_ssids
].ssid_len
=
761 params
.num_filter_ssids
++;
764 if (ssid
->scan_ssid
) {
765 params
.ssids
[params
.num_ssids
].ssid
=
767 params
.ssids
[params
.num_ssids
].ssid_len
=
770 if (params
.num_ssids
>= max_sched_scan_ssids
) {
771 wpa_s
->prev_sched_ssid
= ssid
;
776 if (params
.num_filter_ssids
>= wpa_s
->max_match_sets
)
778 wpa_s
->prev_sched_ssid
= ssid
;
782 if (!params
.num_ssids
) {
783 os_free(params
.filter_ssids
);
788 wps_ie
= wpa_supplicant_extra_ies(wpa_s
, ¶ms
);
790 wpa_dbg(wpa_s
, MSG_DEBUG
,
791 "Starting sched scan: interval %d timeout %d",
792 wpa_s
->sched_scan_interval
, wpa_s
->sched_scan_timeout
);
794 ret
= wpa_supplicant_start_sched_scan(wpa_s
, ¶ms
,
795 wpa_s
->sched_scan_interval
);
797 os_free(params
.filter_ssids
);
799 wpa_msg(wpa_s
, MSG_WARNING
, "Failed to initiate sched scan");
800 if (prev_state
!= wpa_s
->wpa_state
)
801 wpa_supplicant_set_state(wpa_s
, prev_state
);
805 /* If we have more SSIDs to scan, add a timeout so we scan them too */
806 if (ssid
|| !wpa_s
->first_sched_scan
) {
807 wpa_s
->sched_scan_timed_out
= 0;
808 eloop_register_timeout(wpa_s
->sched_scan_timeout
, 0,
809 wpa_supplicant_sched_scan_timeout
,
811 wpa_s
->first_sched_scan
= 0;
812 wpa_s
->sched_scan_timeout
/= 2;
813 wpa_s
->sched_scan_interval
*= 2;
821 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
822 * @wpa_s: Pointer to wpa_supplicant data
824 * This function is used to cancel a scan request scheduled with
825 * wpa_supplicant_req_scan().
827 void wpa_supplicant_cancel_scan(struct wpa_supplicant
*wpa_s
)
829 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling scan request");
830 eloop_cancel_timeout(wpa_supplicant_scan
, wpa_s
, NULL
);
835 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
836 * @wpa_s: Pointer to wpa_supplicant data
838 * This function is used to stop a periodic scheduled scan.
840 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant
*wpa_s
)
842 if (!wpa_s
->sched_scanning
)
845 wpa_dbg(wpa_s
, MSG_DEBUG
, "Cancelling sched scan");
846 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout
, wpa_s
, NULL
);
847 wpa_supplicant_stop_sched_scan(wpa_s
);
851 void wpa_supplicant_notify_scanning(struct wpa_supplicant
*wpa_s
,
854 if (wpa_s
->scanning
!= scanning
) {
855 wpa_s
->scanning
= scanning
;
856 wpas_notify_scanning(wpa_s
);
861 static int wpa_scan_get_max_rate(const struct wpa_scan_res
*res
)
867 ie
= wpa_scan_get_ie(res
, WLAN_EID_SUPP_RATES
);
868 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
869 if ((ie
[i
+ 2] & 0x7f) > rate
)
870 rate
= ie
[i
+ 2] & 0x7f;
873 ie
= wpa_scan_get_ie(res
, WLAN_EID_EXT_SUPP_RATES
);
874 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
875 if ((ie
[i
+ 2] & 0x7f) > rate
)
876 rate
= ie
[i
+ 2] & 0x7f;
883 const u8
* wpa_scan_get_ie(const struct wpa_scan_res
*res
, u8 ie
)
887 pos
= (const u8
*) (res
+ 1);
888 end
= pos
+ res
->ie_len
;
890 while (pos
+ 1 < end
) {
891 if (pos
+ 2 + pos
[1] > end
)
902 const u8
* wpa_scan_get_vendor_ie(const struct wpa_scan_res
*res
,
907 pos
= (const u8
*) (res
+ 1);
908 end
= pos
+ res
->ie_len
;
910 while (pos
+ 1 < end
) {
911 if (pos
+ 2 + pos
[1] > end
)
913 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
914 vendor_type
== WPA_GET_BE32(&pos
[2]))
923 struct wpabuf
* wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res
*res
,
929 buf
= wpabuf_alloc(res
->ie_len
);
933 pos
= (const u8
*) (res
+ 1);
934 end
= pos
+ res
->ie_len
;
936 while (pos
+ 1 < end
) {
937 if (pos
+ 2 + pos
[1] > end
)
939 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
940 vendor_type
== WPA_GET_BE32(&pos
[2]))
941 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
945 if (wpabuf_len(buf
) == 0) {
954 struct wpabuf
* wpa_scan_get_vendor_ie_multi_beacon(
955 const struct wpa_scan_res
*res
, u32 vendor_type
)
960 if (res
->beacon_ie_len
== 0)
962 buf
= wpabuf_alloc(res
->beacon_ie_len
);
966 pos
= (const u8
*) (res
+ 1);
968 end
= pos
+ res
->beacon_ie_len
;
970 while (pos
+ 1 < end
) {
971 if (pos
+ 2 + pos
[1] > end
)
973 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
974 vendor_type
== WPA_GET_BE32(&pos
[2]))
975 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
979 if (wpabuf_len(buf
) == 0) {
988 /* Compare function for sorting scan results. Return >0 if @b is considered
990 static int wpa_scan_result_compar(const void *a
, const void *b
)
992 struct wpa_scan_res
**_wa
= (void *) a
;
993 struct wpa_scan_res
**_wb
= (void *) b
;
994 struct wpa_scan_res
*wa
= *_wa
;
995 struct wpa_scan_res
*wb
= *_wb
;
996 int wpa_a
, wpa_b
, maxrate_a
, maxrate_b
;
998 /* WPA/WPA2 support preferred */
999 wpa_a
= wpa_scan_get_vendor_ie(wa
, WPA_IE_VENDOR_TYPE
) != NULL
||
1000 wpa_scan_get_ie(wa
, WLAN_EID_RSN
) != NULL
;
1001 wpa_b
= wpa_scan_get_vendor_ie(wb
, WPA_IE_VENDOR_TYPE
) != NULL
||
1002 wpa_scan_get_ie(wb
, WLAN_EID_RSN
) != NULL
;
1004 if (wpa_b
&& !wpa_a
)
1006 if (!wpa_b
&& wpa_a
)
1009 /* privacy support preferred */
1010 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) == 0 &&
1011 (wb
->caps
& IEEE80211_CAP_PRIVACY
))
1013 if ((wa
->caps
& IEEE80211_CAP_PRIVACY
) &&
1014 (wb
->caps
& IEEE80211_CAP_PRIVACY
) == 0)
1017 /* best/max rate preferred if signal level close enough XXX */
1018 if ((wa
->level
&& wb
->level
&& abs(wb
->level
- wa
->level
) < 5) ||
1019 (wa
->qual
&& wb
->qual
&& abs(wb
->qual
- wa
->qual
) < 10)) {
1020 maxrate_a
= wpa_scan_get_max_rate(wa
);
1021 maxrate_b
= wpa_scan_get_max_rate(wb
);
1022 if (maxrate_a
!= maxrate_b
)
1023 return maxrate_b
- maxrate_a
;
1026 /* use freq for channel preference */
1028 /* all things being equal, use signal level; if signal levels are
1029 * identical, use quality values since some drivers may only report
1030 * that value and leave the signal level zero */
1031 if (wb
->level
== wa
->level
)
1032 return wb
->qual
- wa
->qual
;
1033 return wb
->level
- wa
->level
;
1038 /* Compare function for sorting scan results when searching a WPS AP for
1039 * provisioning. Return >0 if @b is considered better. */
1040 static int wpa_scan_result_wps_compar(const void *a
, const void *b
)
1042 struct wpa_scan_res
**_wa
= (void *) a
;
1043 struct wpa_scan_res
**_wb
= (void *) b
;
1044 struct wpa_scan_res
*wa
= *_wa
;
1045 struct wpa_scan_res
*wb
= *_wb
;
1046 int uses_wps_a
, uses_wps_b
;
1047 struct wpabuf
*wps_a
, *wps_b
;
1050 /* Optimization - check WPS IE existence before allocated memory and
1051 * doing full reassembly. */
1052 uses_wps_a
= wpa_scan_get_vendor_ie(wa
, WPS_IE_VENDOR_TYPE
) != NULL
;
1053 uses_wps_b
= wpa_scan_get_vendor_ie(wb
, WPS_IE_VENDOR_TYPE
) != NULL
;
1054 if (uses_wps_a
&& !uses_wps_b
)
1056 if (!uses_wps_a
&& uses_wps_b
)
1059 if (uses_wps_a
&& uses_wps_b
) {
1060 wps_a
= wpa_scan_get_vendor_ie_multi(wa
, WPS_IE_VENDOR_TYPE
);
1061 wps_b
= wpa_scan_get_vendor_ie_multi(wb
, WPS_IE_VENDOR_TYPE
);
1062 res
= wps_ap_priority_compar(wps_a
, wps_b
);
1070 * Do not use current AP security policy as a sorting criteria during
1071 * WPS provisioning step since the AP may get reconfigured at the
1072 * completion of provisioning.
1075 /* all things being equal, use signal level; if signal levels are
1076 * identical, use quality values since some drivers may only report
1077 * that value and leave the signal level zero */
1078 if (wb
->level
== wa
->level
)
1079 return wb
->qual
- wa
->qual
;
1080 return wb
->level
- wa
->level
;
1082 #endif /* CONFIG_WPS */
1086 * wpa_supplicant_get_scan_results - Get scan results
1087 * @wpa_s: Pointer to wpa_supplicant data
1088 * @info: Information about what was scanned or %NULL if not available
1089 * @new_scan: Whether a new scan was performed
1090 * Returns: Scan results, %NULL on failure
1092 * This function request the current scan results from the driver and updates
1093 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1094 * results with wpa_scan_results_free().
1096 struct wpa_scan_results
*
1097 wpa_supplicant_get_scan_results(struct wpa_supplicant
*wpa_s
,
1098 struct scan_info
*info
, int new_scan
)
1100 struct wpa_scan_results
*scan_res
;
1102 int (*compar
)(const void *, const void *) = wpa_scan_result_compar
;
1104 scan_res
= wpa_drv_get_scan_results2(wpa_s
);
1105 if (scan_res
== NULL
) {
1106 wpa_dbg(wpa_s
, MSG_DEBUG
, "Failed to get scan results");
1111 if (wpas_wps_in_progress(wpa_s
)) {
1112 wpa_dbg(wpa_s
, MSG_DEBUG
, "WPS: Order scan results with WPS "
1113 "provisioning rules");
1114 compar
= wpa_scan_result_wps_compar
;
1116 #endif /* CONFIG_WPS */
1118 qsort(scan_res
->res
, scan_res
->num
, sizeof(struct wpa_scan_res
*),
1121 wpa_bss_update_start(wpa_s
);
1122 for (i
= 0; i
< scan_res
->num
; i
++)
1123 wpa_bss_update_scan_res(wpa_s
, scan_res
->res
[i
]);
1124 wpa_bss_update_end(wpa_s
, info
, new_scan
);
1130 int wpa_supplicant_update_scan_results(struct wpa_supplicant
*wpa_s
)
1132 struct wpa_scan_results
*scan_res
;
1133 scan_res
= wpa_supplicant_get_scan_results(wpa_s
, NULL
, 0);
1134 if (scan_res
== NULL
)
1136 wpa_scan_results_free(scan_res
);