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