blob: 02c11b06c53089c7e640633d67b80a33fdd0deb6 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt3a787e62013-01-17 10:32:35 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "config.h"
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "wps_supplicant.h"
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "notify.h"
23#include "bss.h"
24#include "scan.h"
25
26
27static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
28{
29 struct wpa_ssid *ssid;
30 union wpa_event_data data;
31
32 ssid = wpa_supplicant_get_ssid(wpa_s);
33 if (ssid == NULL)
34 return;
35
36 if (wpa_s->current_ssid == NULL) {
37 wpa_s->current_ssid = ssid;
38 if (wpa_s->current_ssid != NULL)
39 wpas_notify_network_changed(wpa_s);
40 }
41 wpa_supplicant_initiate_eapol(wpa_s);
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
49#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 enum wps_request_type *req_type)
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
55
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
61 *req_type = wpas_wps_get_req_type(ssid);
62 if (!ssid->eap.phase1)
63 continue;
64
65 if (os_strstr(ssid->eap.phase1, "pbc=1"))
66 return 2;
67 }
68
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080069#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080070 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
71 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080072 wpa_s->wps->dev.p2p = 1;
73 if (!wps) {
74 wps = 1;
75 *req_type = WPS_REQ_ENROLLEE_INFO;
76 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080077 }
78#endif /* CONFIG_P2P */
79
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 return wps;
81}
82#endif /* CONFIG_WPS */
83
84
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080085/**
86 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
87 * @wpa_s: Pointer to wpa_supplicant data
88 * Returns: 0 if no networks are enabled, >0 if networks are enabled
89 *
90 * This function is used to figure out whether any networks (or Interworking
91 * with enabled credentials and auto_interworking) are present in the current
92 * configuration.
93 */
Dmitry Shmidt04949592012-07-19 12:16:46 -070094int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095{
Dmitry Shmidt04949592012-07-19 12:16:46 -070096 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070097 int count = 0, disabled = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070099 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700101 else
102 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103 ssid = ssid->next;
104 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700105 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
106 wpa_s->conf->auto_interworking)
107 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700108 if (count == 0 && disabled > 0) {
109 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
110 "networks)", disabled);
111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 return count;
113}
114
115
116static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
117 struct wpa_ssid *ssid)
118{
119 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 break;
122 ssid = ssid->next;
123 }
124
125 /* ap_scan=2 mode - try to associate with each SSID. */
126 if (ssid == NULL) {
127 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
128 "end of scan list - go back to beginning");
129 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
130 wpa_supplicant_req_scan(wpa_s, 0, 0);
131 return;
132 }
133 if (ssid->next) {
134 /* Continue from the next SSID on the next attempt. */
135 wpa_s->prev_scan_ssid = ssid;
136 } else {
137 /* Start from the beginning of the SSID list. */
138 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
139 }
140 wpa_supplicant_associate(wpa_s, NULL, ssid);
141}
142
143
144static int int_array_len(const int *a)
145{
146 int i;
147 for (i = 0; a && a[i]; i++)
148 ;
149 return i;
150}
151
152
153static void int_array_concat(int **res, const int *a)
154{
155 int reslen, alen, i;
156 int *n;
157
158 reslen = int_array_len(*res);
159 alen = int_array_len(a);
160
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700161 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 if (n == NULL) {
163 os_free(*res);
164 *res = NULL;
165 return;
166 }
167 for (i = 0; i <= alen; i++)
168 n[reslen + i] = a[i];
169 *res = n;
170}
171
172
173static int freq_cmp(const void *a, const void *b)
174{
175 int _a = *(int *) a;
176 int _b = *(int *) b;
177
178 if (_a == 0)
179 return 1;
180 if (_b == 0)
181 return -1;
182 return _a - _b;
183}
184
185
186static void int_array_sort_unique(int *a)
187{
188 int alen;
189 int i, j;
190
191 if (a == NULL)
192 return;
193
194 alen = int_array_len(a);
195 qsort(a, alen, sizeof(int), freq_cmp);
196
197 i = 0;
198 j = 1;
199 while (a[i] && a[j]) {
200 if (a[i] == a[j]) {
201 j++;
202 continue;
203 }
204 a[++i] = a[j++];
205 }
206 if (a[i])
207 i++;
208 a[i] = 0;
209}
210
211
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800212/**
213 * wpa_supplicant_trigger_scan - Request driver to start a scan
214 * @wpa_s: Pointer to wpa_supplicant data
215 * @params: Scan parameters
216 * Returns: 0 on success, -1 on failure
217 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
219 struct wpa_driver_scan_params *params)
220{
221 int ret;
222
223 wpa_supplicant_notify_scanning(wpa_s, 1);
224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800225 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 if (ret) {
227 wpa_supplicant_notify_scanning(wpa_s, 0);
228 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800229 } else {
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700230 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 wpa_s->scan_runs++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800232 wpa_s->normal_scans++;
233 }
234
235 return ret;
236}
237
238
239static void
240wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
241{
242 struct wpa_supplicant *wpa_s = eloop_ctx;
243
244 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
245
246 if (wpa_supplicant_req_sched_scan(wpa_s))
247 wpa_supplicant_req_scan(wpa_s, 0, 0);
248}
249
250
251static void
252wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
253{
254 struct wpa_supplicant *wpa_s = eloop_ctx;
255
256 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
257
258 wpa_s->sched_scan_timed_out = 1;
259 wpa_supplicant_cancel_sched_scan(wpa_s);
260}
261
262
263static int
264wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
265 struct wpa_driver_scan_params *params,
266 int interval)
267{
268 int ret;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700269#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800270 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700271#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800272 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
273 if (ret)
274 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700275 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800276 wpa_s->sched_scanning = 1;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700277#ifdef ANDROID_P2P
278 wpa_supplicant_notify_scanning(wpa_s, 1);
279#endif
280 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800281
282 return ret;
283}
284
285
286static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
287{
288 int ret;
289
290 ret = wpa_drv_stop_sched_scan(wpa_s);
291 if (ret) {
292 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
293 /* TODO: what to do if stopping fails? */
294 return -1;
295 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296
297 return ret;
298}
299
300
301static struct wpa_driver_scan_filter *
302wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
303{
304 struct wpa_driver_scan_filter *ssids;
305 struct wpa_ssid *ssid;
306 size_t count;
307
308 *num_ssids = 0;
309 if (!conf->filter_ssids)
310 return NULL;
311
312 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
313 if (ssid->ssid && ssid->ssid_len)
314 count++;
315 }
316 if (count == 0)
317 return NULL;
318 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
319 if (ssids == NULL)
320 return NULL;
321
322 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
323 if (!ssid->ssid || !ssid->ssid_len)
324 continue;
325 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
326 ssids[*num_ssids].ssid_len = ssid->ssid_len;
327 (*num_ssids)++;
328 }
329
330 return ssids;
331}
332
333
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334static void wpa_supplicant_optimize_freqs(
335 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
336{
337#ifdef CONFIG_P2P
338 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
339 wpa_s->go_params) {
340 /* Optimize provisioning state scan based on GO information */
341 if (wpa_s->p2p_in_provisioning < 5 &&
342 wpa_s->go_params->freq > 0) {
343 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
344 "preferred frequency %d MHz",
345 wpa_s->go_params->freq);
346 params->freqs = os_zalloc(2 * sizeof(int));
347 if (params->freqs)
348 params->freqs[0] = wpa_s->go_params->freq;
349 } else if (wpa_s->p2p_in_provisioning < 8 &&
350 wpa_s->go_params->freq_list[0]) {
351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
352 "channels");
353 int_array_concat(&params->freqs,
354 wpa_s->go_params->freq_list);
355 if (params->freqs)
356 int_array_sort_unique(params->freqs);
357 }
358 wpa_s->p2p_in_provisioning++;
359 }
360#endif /* CONFIG_P2P */
361
362#ifdef CONFIG_WPS
363 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
364 /*
365 * Optimize post-provisioning scan based on channel used
366 * during provisioning.
367 */
368 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
369 "that was used during provisioning", wpa_s->wps_freq);
370 params->freqs = os_zalloc(2 * sizeof(int));
371 if (params->freqs)
372 params->freqs[0] = wpa_s->wps_freq;
373 wpa_s->after_wps--;
374 }
375
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800376 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
377 {
378 /* Optimize provisioning scan based on already known channel */
379 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
380 wpa_s->wps_freq);
381 params->freqs = os_zalloc(2 * sizeof(int));
382 if (params->freqs)
383 params->freqs[0] = wpa_s->wps_freq;
384 wpa_s->known_wps_freq = 0; /* only do this once */
385 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800386#endif /* CONFIG_WPS */
387}
388
389
390#ifdef CONFIG_INTERWORKING
391static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
392 struct wpabuf *buf)
393{
394 if (wpa_s->conf->interworking == 0)
395 return;
396
397 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
398 wpabuf_put_u8(buf, 4);
399 wpabuf_put_u8(buf, 0x00);
400 wpabuf_put_u8(buf, 0x00);
401 wpabuf_put_u8(buf, 0x00);
402 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
403
404 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
405 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
406 1 + ETH_ALEN);
407 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
408 /* No Venue Info */
409 if (!is_zero_ether_addr(wpa_s->conf->hessid))
410 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
411}
412#endif /* CONFIG_INTERWORKING */
413
414
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700415static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800416{
417 struct wpabuf *extra_ie = NULL;
418#ifdef CONFIG_WPS
419 int wps = 0;
420 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
421#endif /* CONFIG_WPS */
422
423#ifdef CONFIG_INTERWORKING
424 if (wpa_s->conf->interworking &&
425 wpabuf_resize(&extra_ie, 100) == 0)
426 wpas_add_interworking_elements(wpa_s, extra_ie);
427#endif /* CONFIG_INTERWORKING */
428
429#ifdef CONFIG_WPS
430 wps = wpas_wps_in_use(wpa_s, &req_type);
431
432 if (wps) {
433 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700434 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
435 DEV_PW_DEFAULT,
436 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800437 wpa_s->wps->uuid, req_type,
438 0, NULL);
439 if (wps_ie) {
440 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
441 wpabuf_put_buf(extra_ie, wps_ie);
442 wpabuf_free(wps_ie);
443 }
444 }
445
446#ifdef CONFIG_P2P
447 if (wps) {
448 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
449 if (wpabuf_resize(&extra_ie, ielen) == 0)
450 wpas_p2p_scan_ie(wpa_s, extra_ie);
451 }
452#endif /* CONFIG_P2P */
453
454#endif /* CONFIG_WPS */
455
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700456#ifdef CONFIG_HS20
457 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
458 wpas_hs20_add_indication(extra_ie);
459#endif /* CONFIG_HS20 */
460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800461 return extra_ie;
462}
463
464
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800465#ifdef CONFIG_P2P
466
467/*
468 * Check whether there are any enabled networks or credentials that could be
469 * used for a non-P2P connection.
470 */
471static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
472{
473 struct wpa_ssid *ssid;
474
475 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
476 if (wpas_network_disabled(wpa_s, ssid))
477 continue;
478 if (!ssid->p2p_group)
479 return 1;
480 }
481
482 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
483 wpa_s->conf->auto_interworking)
484 return 1;
485
486 return 0;
487}
488
489
490/*
491 * Find the operating frequency of any other virtual interface that is using
492 * the same radio concurrently.
493 */
494static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
495{
496 const char *rn, *rn2;
497 struct wpa_supplicant *ifs;
498 u8 bssid[ETH_ALEN];
499
500 if (!wpa_s->driver->get_radio_name)
501 return -1;
502
503 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
504 if (rn == NULL || rn[0] == '\0')
505 return -1;
506
507 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
508 if (ifs == wpa_s || !ifs->driver->get_radio_name)
509 continue;
510
511 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
512 if (!rn2 || os_strcmp(rn, rn2) != 0)
513 continue;
514
515 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
516 continue;
517
518 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
519 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
520 return ifs->current_ssid->frequency;
521 if (wpa_drv_get_bssid(ifs, bssid) == 0)
522 return ifs->assoc_freq;
523 }
524
525 return 0;
526}
527
528#endif /* CONFIG_P2P */
529
530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
532{
533 struct wpa_supplicant *wpa_s = eloop_ctx;
534 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800535 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
536 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700537 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700539 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 size_t max_ssids;
541 enum wpa_states prev_state;
542
543 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
544 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700545 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 return;
547 }
548
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800549 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700550 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700552 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 return;
554 }
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700555#ifdef ANDROID
556 if (wpa_s->scanning) {
557 /* If we are already in scanning state, we shall ignore this new scan request*/
558 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
559 return;
560 }
561#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700562 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800563 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
565 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700566 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 return;
568 }
569
570 if (wpa_s->conf->ap_scan != 0 &&
571 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
572 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
573 "overriding ap_scan configuration");
574 wpa_s->conf->ap_scan = 0;
575 wpas_notify_ap_scan_changed(wpa_s);
576 }
577
578 if (wpa_s->conf->ap_scan == 0) {
579 wpa_supplicant_gen_assoc_event(wpa_s);
580 return;
581 }
582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583#ifdef CONFIG_P2P
584 if (wpas_p2p_in_progress(wpa_s)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700585 if (wpa_s->sta_scan_pending &&
586 wpas_p2p_in_progress(wpa_s) == 2 &&
Jouni Malinendc7b7132012-09-14 12:53:47 -0700587 wpa_s->global->p2p_cb_on_scan_complete) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700588 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
589 "mode scan during P2P search");
590 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800591 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
592 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700593 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800594 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700595 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800596 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800597 }
598#endif /* CONFIG_P2P */
599
600 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 max_ssids = 1;
602 else {
603 max_ssids = wpa_s->max_scan_ssids;
604 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
605 max_ssids = WPAS_MAX_SCAN_SSIDS;
606 }
607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800609 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610
611 os_memset(&params, 0, sizeof(params));
612
613 prev_state = wpa_s->wpa_state;
614 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
615 wpa_s->wpa_state == WPA_INACTIVE)
616 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
617
Dmitry Shmidt04949592012-07-19 12:16:46 -0700618 /*
619 * If autoscan has set its own scanning parameters
620 */
621 if (wpa_s->autoscan_params != NULL) {
622 scan_params = wpa_s->autoscan_params;
623 goto scan;
624 }
625
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800626 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700627 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
628 if (ssid == wpa_s->connect_without_scan)
629 break;
630 }
631 wpa_s->connect_without_scan = NULL;
632 if (ssid) {
633 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
634 "without scan step");
635 wpa_supplicant_associate(wpa_s, NULL, ssid);
636 return;
637 }
638 }
639
Dmitry Shmidt04949592012-07-19 12:16:46 -0700640#ifdef CONFIG_P2P
641 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
642 wpa_s->go_params) {
643 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
644 "P2P group formation");
645 params.ssids[0].ssid = wpa_s->go_params->ssid;
646 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
647 params.num_ssids = 1;
648 goto ssid_list_set;
649 }
650#endif /* CONFIG_P2P */
651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652 /* Find the starting point from which to continue scanning */
653 ssid = wpa_s->conf->ssid;
654 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
655 while (ssid) {
656 if (ssid == wpa_s->prev_scan_ssid) {
657 ssid = ssid->next;
658 break;
659 }
660 ssid = ssid->next;
661 }
662 }
663
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800664 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700665 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800666 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 wpa_supplicant_assoc_try(wpa_s, ssid);
668 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700669#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 } else if (wpa_s->conf->ap_scan == 2) {
671 /*
672 * User-initiated scan request in ap_scan == 2; scan with
673 * wildcard SSID.
674 */
675 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700676#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677 } else {
678 struct wpa_ssid *start = ssid, *tssid;
679 int freqs_set = 0;
680 if (ssid == NULL && max_ssids > 1)
681 ssid = wpa_s->conf->ssid;
682 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700683 if (!wpas_network_disabled(wpa_s, ssid) &&
684 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
686 ssid->ssid, ssid->ssid_len);
687 params.ssids[params.num_ssids].ssid =
688 ssid->ssid;
689 params.ssids[params.num_ssids].ssid_len =
690 ssid->ssid_len;
691 params.num_ssids++;
692 if (params.num_ssids + 1 >= max_ssids)
693 break;
694 }
695 ssid = ssid->next;
696 if (ssid == start)
697 break;
698 if (ssid == NULL && max_ssids > 1 &&
699 start != wpa_s->conf->ssid)
700 ssid = wpa_s->conf->ssid;
701 }
702
703 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700704 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705 continue;
706 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
707 int_array_concat(&params.freqs,
708 tssid->scan_freq);
709 } else {
710 os_free(params.freqs);
711 params.freqs = NULL;
712 }
713 freqs_set = 1;
714 }
715 int_array_sort_unique(params.freqs);
716 }
717
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800718 if (ssid && max_ssids == 1) {
719 /*
720 * If the driver is limited to 1 SSID at a time interleave
721 * wildcard SSID scans with specific SSID scans to avoid
722 * waiting a long time for a wildcard scan.
723 */
724 if (!wpa_s->prev_scan_wildcard) {
725 params.ssids[0].ssid = NULL;
726 params.ssids[0].ssid_len = 0;
727 wpa_s->prev_scan_wildcard = 1;
728 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
729 "wildcard SSID (Interleave with specific)");
730 } else {
731 wpa_s->prev_scan_ssid = ssid;
732 wpa_s->prev_scan_wildcard = 0;
733 wpa_dbg(wpa_s, MSG_DEBUG,
734 "Starting AP scan for specific SSID: %s",
735 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800737 } else if (ssid) {
738 /* max_ssids > 1 */
739
740 wpa_s->prev_scan_ssid = ssid;
741 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
742 "the scan request");
743 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 } else {
745 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
746 params.num_ssids++;
747 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
748 "SSID");
749 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700750#ifdef CONFIG_P2P
751ssid_list_set:
752#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800754 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700755 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756
757 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
758 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
759 "generated frequency list");
760 params.freqs = wpa_s->next_scan_freqs;
761 } else
762 os_free(wpa_s->next_scan_freqs);
763 wpa_s->next_scan_freqs = NULL;
764
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700765 /* See if user specified frequencies. If so, scan only those. */
766 if (wpa_s->conf->freq_list && !params.freqs) {
767 wpa_dbg(wpa_s, MSG_DEBUG,
768 "Optimize scan based on conf->freq_list");
769 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
770 }
771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772 params.filter_ssids = wpa_supplicant_build_filter_ssids(
773 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774 if (extra_ie) {
775 params.extra_ies = wpabuf_head(extra_ie);
776 params.extra_ies_len = wpabuf_len(extra_ie);
777 }
778
779#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700780 if (wpa_s->p2p_in_provisioning ||
781 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 /*
783 * The interface may not yet be in P2P mode, so we have to
784 * explicitly request P2P probe to disable CCK rates.
785 */
786 params.p2p_probe = 1;
787 }
788#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789
Dmitry Shmidt04949592012-07-19 12:16:46 -0700790 scan_params = &params;
791
792scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800793#ifdef CONFIG_P2P
794 /*
795 * If the driver does not support multi-channel concurrency and a
796 * virtual interface that shares the same radio with the wpa_s interface
797 * is operating there may not be need to scan other channels apart from
798 * the current operating channel on the other virtual interface. Filter
799 * out other channels in case we are trying to find a connection for a
800 * station interface when we are not configured to prefer station
801 * connection and a concurrent operation is already in process.
802 */
803 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
804 !scan_params->freqs && !params.freqs &&
805 wpas_is_p2p_prioritized(wpa_s) &&
806 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
807 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
808 non_p2p_network_enabled(wpa_s)) {
809 int freq = shared_vif_oper_freq(wpa_s);
810 if (freq > 0) {
811 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
812 "operating channel (%d MHz) since driver does "
813 "not support multi-channel concurrency", freq);
814 params.freqs = os_zalloc(sizeof(int) * 2);
815 if (params.freqs)
816 params.freqs[0] = freq;
817 scan_params->freqs = params.freqs;
818 }
819 }
820#endif /* CONFIG_P2P */
821
Dmitry Shmidt04949592012-07-19 12:16:46 -0700822 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800824 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 os_free(params.freqs);
826 os_free(params.filter_ssids);
827
828 if (ret) {
829 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
830 if (prev_state != wpa_s->wpa_state)
831 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800832 /* Restore scan_req since we will try to scan again */
833 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800835 } else {
836 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 }
838}
839
840
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800841void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
842{
843 struct os_time remaining, new_int;
844 int cancelled;
845
846 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
847 &remaining);
848
849 new_int.sec = sec;
850 new_int.usec = 0;
851 if (cancelled && os_time_before(&remaining, &new_int)) {
852 new_int.sec = remaining.sec;
853 new_int.usec = remaining.usec;
854 }
855
856 eloop_register_timeout(new_int.sec, new_int.usec, wpa_supplicant_scan,
857 wpa_s, NULL);
858 wpa_s->scan_interval = sec;
859}
860
861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862/**
863 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
864 * @wpa_s: Pointer to wpa_supplicant data
865 * @sec: Number of seconds after which to scan
866 * @usec: Number of microseconds after which to scan
867 *
868 * This function is used to schedule a scan for neighboring access points after
869 * the specified time.
870 */
871void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
872{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800873#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 /* If there's at least one network that should be specifically scanned
875 * then don't cancel the scan and reschedule. Some drivers do
876 * background scanning which generates frequent scan results, and that
877 * causes the specific SSID scan to get continually pushed back and
878 * never happen, which causes hidden APs to never get probe-scanned.
879 */
880 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
881 wpa_s->conf->ap_scan == 1) {
882 struct wpa_ssid *ssid = wpa_s->conf->ssid;
883
884 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700885 if (!wpas_network_disabled(wpa_s, ssid) &&
886 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 break;
888 ssid = ssid->next;
889 }
890 if (ssid) {
891 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
892 "ensure that specific SSID scans occur");
893 return;
894 }
895 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800896#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
898 sec, usec);
899 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
900 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
901}
902
903
904/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
906 * @wpa_s: Pointer to wpa_supplicant data
907 * @sec: Number of seconds after which to scan
908 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800909 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800910 *
911 * This function is used to schedule periodic scans for neighboring
912 * access points after the specified time.
913 */
914int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
915 int sec, int usec)
916{
917 if (!wpa_s->sched_scan_supported)
918 return -1;
919
920 eloop_register_timeout(sec, usec,
921 wpa_supplicant_delayed_sched_scan_timeout,
922 wpa_s, NULL);
923
924 return 0;
925}
926
927
928/**
929 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
930 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800931 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800932 *
933 * This function is used to schedule periodic scans for neighboring
934 * access points repeating the scan continuously.
935 */
936int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
937{
938 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700939 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700941 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700942 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800943 int ret;
944 unsigned int max_sched_scan_ssids;
945 int wildcard = 0;
946 int need_ssids;
947
948 if (!wpa_s->sched_scan_supported)
949 return -1;
950
951 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
952 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
953 else
954 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700955 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800956 return -1;
957
958 if (wpa_s->sched_scanning) {
959 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
960 return 0;
961 }
962
963 need_ssids = 0;
964 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700965 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800966 /* Use wildcard SSID to find this network */
967 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700968 } else if (!wpas_network_disabled(wpa_s, ssid) &&
969 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800970 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700971
972#ifdef CONFIG_WPS
973 if (!wpas_network_disabled(wpa_s, ssid) &&
974 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
975 /*
976 * Normal scan is more reliable and faster for WPS
977 * operations and since these are for short periods of
978 * time, the benefit of trying to use sched_scan would
979 * be limited.
980 */
981 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
982 "sched_scan for WPS");
983 return -1;
984 }
985#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800986 }
987 if (wildcard)
988 need_ssids++;
989
990 if (wpa_s->normal_scans < 3 &&
991 (need_ssids <= wpa_s->max_scan_ssids ||
992 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
993 /*
994 * When normal scan can speed up operations, use that for the
995 * first operations before starting the sched_scan to allow
996 * user space sleep more. We do this only if the normal scan
997 * has functionality that is suitable for this or if the
998 * sched_scan does not have better support for multiple SSIDs.
999 */
1000 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1001 "sched_scan for initial scans (normal_scans=%d)",
1002 wpa_s->normal_scans);
1003 return -1;
1004 }
1005
1006 os_memset(&params, 0, sizeof(params));
1007
1008 /* If we can't allocate space for the filters, we just don't filter */
1009 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1010 sizeof(struct wpa_driver_scan_filter));
1011
1012 prev_state = wpa_s->wpa_state;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001013#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001014 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1015 wpa_s->wpa_state == WPA_INACTIVE)
1016 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001017#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001018
Dmitry Shmidt04949592012-07-19 12:16:46 -07001019 if (wpa_s->autoscan_params != NULL) {
1020 scan_params = wpa_s->autoscan_params;
1021 goto scan;
1022 }
1023
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024 /* Find the starting point from which to continue scanning */
1025 ssid = wpa_s->conf->ssid;
1026 if (wpa_s->prev_sched_ssid) {
1027 while (ssid) {
1028 if (ssid == wpa_s->prev_sched_ssid) {
1029 ssid = ssid->next;
1030 break;
1031 }
1032 ssid = ssid->next;
1033 }
1034 }
1035
1036 if (!ssid || !wpa_s->prev_sched_ssid) {
1037 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001038 if (wpa_s->conf->sched_scan_interval)
1039 wpa_s->sched_scan_interval =
1040 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041 if (wpa_s->sched_scan_interval == 0)
1042 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001043 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1044 wpa_s->first_sched_scan = 1;
1045 ssid = wpa_s->conf->ssid;
1046 wpa_s->prev_sched_ssid = ssid;
1047 }
1048
1049 if (wildcard) {
1050 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1051 params.num_ssids++;
1052 }
1053
1054 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001055 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001056 goto next;
1057
1058 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1059 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1060 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1061 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1062 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1063 ssid->ssid, ssid->ssid_len);
1064 params.filter_ssids[params.num_filter_ssids].ssid_len =
1065 ssid->ssid_len;
1066 params.num_filter_ssids++;
1067 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1068 {
1069 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1070 "filter for sched_scan - drop filter");
1071 os_free(params.filter_ssids);
1072 params.filter_ssids = NULL;
1073 params.num_filter_ssids = 0;
1074 }
1075
1076 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1077 if (params.num_ssids == max_sched_scan_ssids)
1078 break; /* only room for broadcast SSID */
1079 wpa_dbg(wpa_s, MSG_DEBUG,
1080 "add to active scan ssid: %s",
1081 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1082 params.ssids[params.num_ssids].ssid =
1083 ssid->ssid;
1084 params.ssids[params.num_ssids].ssid_len =
1085 ssid->ssid_len;
1086 params.num_ssids++;
1087 if (params.num_ssids >= max_sched_scan_ssids) {
1088 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001089 do {
1090 ssid = ssid->next;
1091 } while (ssid &&
1092 (wpas_network_disabled(wpa_s, ssid) ||
1093 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001094 break;
1095 }
1096 }
1097
1098 next:
1099 wpa_s->prev_sched_ssid = ssid;
1100 ssid = ssid->next;
1101 }
1102
1103 if (params.num_filter_ssids == 0) {
1104 os_free(params.filter_ssids);
1105 params.filter_ssids = NULL;
1106 }
1107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001108 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1109 if (extra_ie) {
1110 params.extra_ies = wpabuf_head(extra_ie);
1111 params.extra_ies_len = wpabuf_len(extra_ie);
1112 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001113
Dmitry Shmidt04949592012-07-19 12:16:46 -07001114 scan_params = &params;
1115
1116scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001117 if (ssid || !wpa_s->first_sched_scan) {
1118 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001119 "Starting sched scan: interval %d timeout %d",
1120 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001121 } else {
1122 wpa_dbg(wpa_s, MSG_DEBUG,
1123 "Starting sched scan: interval %d (no timeout)",
1124 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001125 }
1126
Dmitry Shmidt04949592012-07-19 12:16:46 -07001127 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001128 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001129 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001130 os_free(params.filter_ssids);
1131 if (ret) {
1132 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1133 if (prev_state != wpa_s->wpa_state)
1134 wpa_supplicant_set_state(wpa_s, prev_state);
1135 return ret;
1136 }
1137
1138 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1139 if (ssid || !wpa_s->first_sched_scan) {
1140 wpa_s->sched_scan_timed_out = 0;
1141 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1142 wpa_supplicant_sched_scan_timeout,
1143 wpa_s, NULL);
1144 wpa_s->first_sched_scan = 0;
1145 wpa_s->sched_scan_timeout /= 2;
1146 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001147 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1148 wpa_s->sched_scan_interval = 10;
1149 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1150 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001151 }
1152
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001153 /* If there is no more ssids, start next time from the beginning */
1154 if (!ssid)
1155 wpa_s->prev_sched_ssid = NULL;
1156
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001157 return 0;
1158}
1159
1160
1161/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1163 * @wpa_s: Pointer to wpa_supplicant data
1164 *
1165 * This function is used to cancel a scan request scheduled with
1166 * wpa_supplicant_req_scan().
1167 */
1168void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1169{
1170 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1171 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001172 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001173#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001174 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001175#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176}
1177
1178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001179/**
1180 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1181 * @wpa_s: Pointer to wpa_supplicant data
1182 *
1183 * This function is used to stop a periodic scheduled scan.
1184 */
1185void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1186{
1187 if (!wpa_s->sched_scanning)
1188 return;
1189
1190 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1191 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1192 wpa_supplicant_stop_sched_scan(wpa_s);
1193}
1194
1195
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001196/**
1197 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1198 * @wpa_s: Pointer to wpa_supplicant data
1199 * @scanning: Whether scanning is currently in progress
1200 *
1201 * This function is to generate scanning notifycations. It is called whenever
1202 * there may have been a change in scanning (scan started, completed, stopped).
1203 * wpas_notify_scanning() is called whenever the scanning state changed from the
1204 * previously notified state.
1205 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1207 int scanning)
1208{
1209 if (wpa_s->scanning != scanning) {
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001210#ifdef ANDROID_P2P
1211 if(!wpa_s->sched_scanning)
1212 wpa_s->scanning = scanning;
1213#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214 wpa_s->scanning = scanning;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001215#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 wpas_notify_scanning(wpa_s);
1217 }
1218}
1219
1220
1221static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1222{
1223 int rate = 0;
1224 const u8 *ie;
1225 int i;
1226
1227 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1228 for (i = 0; ie && i < ie[1]; i++) {
1229 if ((ie[i + 2] & 0x7f) > rate)
1230 rate = ie[i + 2] & 0x7f;
1231 }
1232
1233 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1234 for (i = 0; ie && i < ie[1]; i++) {
1235 if ((ie[i + 2] & 0x7f) > rate)
1236 rate = ie[i + 2] & 0x7f;
1237 }
1238
1239 return rate;
1240}
1241
1242
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001243/**
1244 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1245 * @res: Scan result entry
1246 * @ie: Information element identitifier (WLAN_EID_*)
1247 * Returns: Pointer to the information element (id field) or %NULL if not found
1248 *
1249 * This function returns the first matching information element in the scan
1250 * result.
1251 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1253{
1254 const u8 *end, *pos;
1255
1256 pos = (const u8 *) (res + 1);
1257 end = pos + res->ie_len;
1258
1259 while (pos + 1 < end) {
1260 if (pos + 2 + pos[1] > end)
1261 break;
1262 if (pos[0] == ie)
1263 return pos;
1264 pos += 2 + pos[1];
1265 }
1266
1267 return NULL;
1268}
1269
1270
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001271/**
1272 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1273 * @res: Scan result entry
1274 * @vendor_type: Vendor type (four octets starting the IE payload)
1275 * Returns: Pointer to the information element (id field) or %NULL if not found
1276 *
1277 * This function returns the first matching information element in the scan
1278 * result.
1279 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1281 u32 vendor_type)
1282{
1283 const u8 *end, *pos;
1284
1285 pos = (const u8 *) (res + 1);
1286 end = pos + res->ie_len;
1287
1288 while (pos + 1 < end) {
1289 if (pos + 2 + pos[1] > end)
1290 break;
1291 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1292 vendor_type == WPA_GET_BE32(&pos[2]))
1293 return pos;
1294 pos += 2 + pos[1];
1295 }
1296
1297 return NULL;
1298}
1299
1300
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001301/**
1302 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1303 * @res: Scan result entry
1304 * @vendor_type: Vendor type (four octets starting the IE payload)
1305 * Returns: Pointer to the information element payload or %NULL if not found
1306 *
1307 * This function returns concatenated payload of possibly fragmented vendor
1308 * specific information elements in the scan result. The caller is responsible
1309 * for freeing the returned buffer.
1310 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1312 u32 vendor_type)
1313{
1314 struct wpabuf *buf;
1315 const u8 *end, *pos;
1316
1317 buf = wpabuf_alloc(res->ie_len);
1318 if (buf == NULL)
1319 return NULL;
1320
1321 pos = (const u8 *) (res + 1);
1322 end = pos + res->ie_len;
1323
1324 while (pos + 1 < end) {
1325 if (pos + 2 + pos[1] > end)
1326 break;
1327 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1328 vendor_type == WPA_GET_BE32(&pos[2]))
1329 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1330 pos += 2 + pos[1];
1331 }
1332
1333 if (wpabuf_len(buf) == 0) {
1334 wpabuf_free(buf);
1335 buf = NULL;
1336 }
1337
1338 return buf;
1339}
1340
1341
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001342/*
1343 * Channels with a great SNR can operate at full rate. What is a great SNR?
1344 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1345 * rule of thumb is that any SNR above 20 is good." This one
1346 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1347 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1348 * conservative value.
1349 */
1350#define GREAT_SNR 30
1351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352/* Compare function for sorting scan results. Return >0 if @b is considered
1353 * better. */
1354static int wpa_scan_result_compar(const void *a, const void *b)
1355{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001356#define IS_5GHZ(n) (n > 4000)
1357#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 struct wpa_scan_res **_wa = (void *) a;
1359 struct wpa_scan_res **_wb = (void *) b;
1360 struct wpa_scan_res *wa = *_wa;
1361 struct wpa_scan_res *wb = *_wb;
1362 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001363 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364
1365 /* WPA/WPA2 support preferred */
1366 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1367 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1368 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1369 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1370
1371 if (wpa_b && !wpa_a)
1372 return 1;
1373 if (!wpa_b && wpa_a)
1374 return -1;
1375
1376 /* privacy support preferred */
1377 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1378 (wb->caps & IEEE80211_CAP_PRIVACY))
1379 return 1;
1380 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1381 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1382 return -1;
1383
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001384 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1385 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1386 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1387 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1388 } else {
1389 /* Not suitable information to calculate SNR, so use level */
1390 snr_a = wa->level;
1391 snr_b = wb->level;
1392 }
1393
1394 /* best/max rate preferred if SNR close enough */
1395 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1397 maxrate_a = wpa_scan_get_max_rate(wa);
1398 maxrate_b = wpa_scan_get_max_rate(wb);
1399 if (maxrate_a != maxrate_b)
1400 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001401 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1402 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 }
1404
1405 /* use freq for channel preference */
1406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001407 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 * identical, use quality values since some drivers may only report
1409 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001410 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001412 return snr_b - snr_a;
1413#undef MIN
1414#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415}
1416
1417
1418#ifdef CONFIG_WPS
1419/* Compare function for sorting scan results when searching a WPS AP for
1420 * provisioning. Return >0 if @b is considered better. */
1421static int wpa_scan_result_wps_compar(const void *a, const void *b)
1422{
1423 struct wpa_scan_res **_wa = (void *) a;
1424 struct wpa_scan_res **_wb = (void *) b;
1425 struct wpa_scan_res *wa = *_wa;
1426 struct wpa_scan_res *wb = *_wb;
1427 int uses_wps_a, uses_wps_b;
1428 struct wpabuf *wps_a, *wps_b;
1429 int res;
1430
1431 /* Optimization - check WPS IE existence before allocated memory and
1432 * doing full reassembly. */
1433 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1434 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1435 if (uses_wps_a && !uses_wps_b)
1436 return -1;
1437 if (!uses_wps_a && uses_wps_b)
1438 return 1;
1439
1440 if (uses_wps_a && uses_wps_b) {
1441 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1442 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1443 res = wps_ap_priority_compar(wps_a, wps_b);
1444 wpabuf_free(wps_a);
1445 wpabuf_free(wps_b);
1446 if (res)
1447 return res;
1448 }
1449
1450 /*
1451 * Do not use current AP security policy as a sorting criteria during
1452 * WPS provisioning step since the AP may get reconfigured at the
1453 * completion of provisioning.
1454 */
1455
1456 /* all things being equal, use signal level; if signal levels are
1457 * identical, use quality values since some drivers may only report
1458 * that value and leave the signal level zero */
1459 if (wb->level == wa->level)
1460 return wb->qual - wa->qual;
1461 return wb->level - wa->level;
1462}
1463#endif /* CONFIG_WPS */
1464
1465
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001466static void dump_scan_res(struct wpa_scan_results *scan_res)
1467{
1468#ifndef CONFIG_NO_STDOUT_DEBUG
1469 size_t i;
1470
1471 if (scan_res->res == NULL || scan_res->num == 0)
1472 return;
1473
1474 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1475
1476 for (i = 0; i < scan_res->num; i++) {
1477 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001478 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1480 == WPA_SCAN_LEVEL_DBM) {
1481 int snr = r->level - r->noise;
1482 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001483 "noise=%d level=%d snr=%d%s flags=0x%x "
1484 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001485 MAC2STR(r->bssid), r->freq, r->qual,
1486 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001487 snr >= GREAT_SNR ? "*" : "", r->flags,
1488 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001489 } else {
1490 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001491 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001493 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001494 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001495 pos = (u8 *) (r + 1);
1496 if (r->ie_len)
1497 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1498 pos += r->ie_len;
1499 if (r->beacon_ie_len)
1500 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1501 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001502 }
1503#endif /* CONFIG_NO_STDOUT_DEBUG */
1504}
1505
1506
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001507/**
1508 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1509 * @wpa_s: Pointer to wpa_supplicant data
1510 * @bssid: BSSID to check
1511 * Returns: 0 if the BSSID is filtered or 1 if not
1512 *
1513 * This function is used to filter out specific BSSIDs from scan reslts mainly
1514 * for testing purposes (SET bssid_filter ctrl_iface command).
1515 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001516int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1517 const u8 *bssid)
1518{
1519 size_t i;
1520
1521 if (wpa_s->bssid_filter == NULL)
1522 return 1;
1523
1524 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1525 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1526 ETH_ALEN) == 0)
1527 return 1;
1528 }
1529
1530 return 0;
1531}
1532
1533
1534static void filter_scan_res(struct wpa_supplicant *wpa_s,
1535 struct wpa_scan_results *res)
1536{
1537 size_t i, j;
1538
1539 if (wpa_s->bssid_filter == NULL)
1540 return;
1541
1542 for (i = 0, j = 0; i < res->num; i++) {
1543 if (wpa_supplicant_filter_bssid_match(wpa_s,
1544 res->res[i]->bssid)) {
1545 res->res[j++] = res->res[i];
1546 } else {
1547 os_free(res->res[i]);
1548 res->res[i] = NULL;
1549 }
1550 }
1551
1552 if (res->num != j) {
1553 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1554 (int) (res->num - j));
1555 res->num = j;
1556 }
1557}
1558
1559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560/**
1561 * wpa_supplicant_get_scan_results - Get scan results
1562 * @wpa_s: Pointer to wpa_supplicant data
1563 * @info: Information about what was scanned or %NULL if not available
1564 * @new_scan: Whether a new scan was performed
1565 * Returns: Scan results, %NULL on failure
1566 *
1567 * This function request the current scan results from the driver and updates
1568 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1569 * results with wpa_scan_results_free().
1570 */
1571struct wpa_scan_results *
1572wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1573 struct scan_info *info, int new_scan)
1574{
1575 struct wpa_scan_results *scan_res;
1576 size_t i;
1577 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1578
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001579 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 if (scan_res == NULL) {
1581 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1582 return NULL;
1583 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001584 if (scan_res->fetch_time.sec == 0) {
1585 /*
1586 * Make sure we have a valid timestamp if the driver wrapper
1587 * does not set this.
1588 */
1589 os_get_time(&scan_res->fetch_time);
1590 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001591 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592
1593#ifdef CONFIG_WPS
1594 if (wpas_wps_in_progress(wpa_s)) {
1595 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1596 "provisioning rules");
1597 compar = wpa_scan_result_wps_compar;
1598 }
1599#endif /* CONFIG_WPS */
1600
1601 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1602 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001603 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604
1605 wpa_bss_update_start(wpa_s);
1606 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001607 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1608 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 wpa_bss_update_end(wpa_s, info, new_scan);
1610
1611 return scan_res;
1612}
1613
1614
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001615/**
1616 * wpa_supplicant_update_scan_results - Update scan results from the driver
1617 * @wpa_s: Pointer to wpa_supplicant data
1618 * Returns: 0 on success, -1 on failure
1619 *
1620 * This function updates the BSS table within wpa_supplicant based on the
1621 * currently available scan results from the driver without requesting a new
1622 * scan. This is used in cases where the driver indicates an association
1623 * (including roaming within ESS) and wpa_supplicant does not yet have the
1624 * needed information to complete the connection (e.g., to perform validation
1625 * steps in 4-way handshake).
1626 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1628{
1629 struct wpa_scan_results *scan_res;
1630 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1631 if (scan_res == NULL)
1632 return -1;
1633 wpa_scan_results_free(scan_res);
1634
1635 return 0;
1636}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001637
1638
1639/**
1640 * scan_only_handler - Reports scan results
1641 */
1642void scan_only_handler(struct wpa_supplicant *wpa_s,
1643 struct wpa_scan_results *scan_res)
1644{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001645 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001646 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1647 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001648 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001649}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001650
1651
1652int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1653{
1654 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1655}