blob: 04148a85e418d50c86a873d02b00bb162cd5fad8 [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
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700489#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800490
491/*
492 * Find the operating frequency of any other virtual interface that is using
493 * the same radio concurrently.
494 */
495static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
496{
497 const char *rn, *rn2;
498 struct wpa_supplicant *ifs;
499 u8 bssid[ETH_ALEN];
500
501 if (!wpa_s->driver->get_radio_name)
502 return -1;
503
504 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
505 if (rn == NULL || rn[0] == '\0')
506 return -1;
507
508 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
509 if (ifs == wpa_s || !ifs->driver->get_radio_name)
510 continue;
511
512 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
513 if (!rn2 || os_strcmp(rn, rn2) != 0)
514 continue;
515
516 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
517 continue;
518
519 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
520 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
521 return ifs->current_ssid->frequency;
522 if (wpa_drv_get_bssid(ifs, bssid) == 0)
523 return ifs->assoc_freq;
524 }
525
526 return 0;
527}
528
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
531{
532 struct wpa_supplicant *wpa_s = eloop_ctx;
533 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800534 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
535 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700536 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700538 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 size_t max_ssids;
540 enum wpa_states prev_state;
541
542 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
543 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700544 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 return;
546 }
547
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800548 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700549 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700551 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 return;
553 }
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700554#ifdef ANDROID
555 if (wpa_s->scanning) {
556 /* If we are already in scanning state, we shall ignore this new scan request*/
557 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
558 return;
559 }
560#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700561 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800562 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
564 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700565 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566 return;
567 }
568
569 if (wpa_s->conf->ap_scan != 0 &&
570 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
571 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
572 "overriding ap_scan configuration");
573 wpa_s->conf->ap_scan = 0;
574 wpas_notify_ap_scan_changed(wpa_s);
575 }
576
577 if (wpa_s->conf->ap_scan == 0) {
578 wpa_supplicant_gen_assoc_event(wpa_s);
579 return;
580 }
581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582#ifdef CONFIG_P2P
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700583 if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700584 if (wpa_s->sta_scan_pending &&
585 wpas_p2p_in_progress(wpa_s) == 2 &&
Jouni Malinendc7b7132012-09-14 12:53:47 -0700586 wpa_s->global->p2p_cb_on_scan_complete) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700587 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
588 "mode scan during P2P search");
589 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
591 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700592 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800593 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700594 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800595 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800596 }
597#endif /* CONFIG_P2P */
598
599 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 max_ssids = 1;
601 else {
602 max_ssids = wpa_s->max_scan_ssids;
603 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
604 max_ssids = WPAS_MAX_SCAN_SSIDS;
605 }
606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607 scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800608 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609
610 os_memset(&params, 0, sizeof(params));
611
612 prev_state = wpa_s->wpa_state;
613 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
614 wpa_s->wpa_state == WPA_INACTIVE)
615 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
616
Dmitry Shmidt04949592012-07-19 12:16:46 -0700617 /*
618 * If autoscan has set its own scanning parameters
619 */
620 if (wpa_s->autoscan_params != NULL) {
621 scan_params = wpa_s->autoscan_params;
622 goto scan;
623 }
624
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800625 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700626 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
627 if (ssid == wpa_s->connect_without_scan)
628 break;
629 }
630 wpa_s->connect_without_scan = NULL;
631 if (ssid) {
632 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
633 "without scan step");
634 wpa_supplicant_associate(wpa_s, NULL, ssid);
635 return;
636 }
637 }
638
Dmitry Shmidt04949592012-07-19 12:16:46 -0700639#ifdef CONFIG_P2P
640 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
641 wpa_s->go_params) {
642 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
643 "P2P group formation");
644 params.ssids[0].ssid = wpa_s->go_params->ssid;
645 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
646 params.num_ssids = 1;
647 goto ssid_list_set;
648 }
649#endif /* CONFIG_P2P */
650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651 /* Find the starting point from which to continue scanning */
652 ssid = wpa_s->conf->ssid;
653 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
654 while (ssid) {
655 if (ssid == wpa_s->prev_scan_ssid) {
656 ssid = ssid->next;
657 break;
658 }
659 ssid = ssid->next;
660 }
661 }
662
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800663 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700664 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800665 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666 wpa_supplicant_assoc_try(wpa_s, ssid);
667 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700668#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 } else if (wpa_s->conf->ap_scan == 2) {
670 /*
671 * User-initiated scan request in ap_scan == 2; scan with
672 * wildcard SSID.
673 */
674 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700675#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 } else {
677 struct wpa_ssid *start = ssid, *tssid;
678 int freqs_set = 0;
679 if (ssid == NULL && max_ssids > 1)
680 ssid = wpa_s->conf->ssid;
681 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700682 if (!wpas_network_disabled(wpa_s, ssid) &&
683 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
685 ssid->ssid, ssid->ssid_len);
686 params.ssids[params.num_ssids].ssid =
687 ssid->ssid;
688 params.ssids[params.num_ssids].ssid_len =
689 ssid->ssid_len;
690 params.num_ssids++;
691 if (params.num_ssids + 1 >= max_ssids)
692 break;
693 }
694 ssid = ssid->next;
695 if (ssid == start)
696 break;
697 if (ssid == NULL && max_ssids > 1 &&
698 start != wpa_s->conf->ssid)
699 ssid = wpa_s->conf->ssid;
700 }
701
702 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700703 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 continue;
705 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
706 int_array_concat(&params.freqs,
707 tssid->scan_freq);
708 } else {
709 os_free(params.freqs);
710 params.freqs = NULL;
711 }
712 freqs_set = 1;
713 }
714 int_array_sort_unique(params.freqs);
715 }
716
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800717 if (ssid && max_ssids == 1) {
718 /*
719 * If the driver is limited to 1 SSID at a time interleave
720 * wildcard SSID scans with specific SSID scans to avoid
721 * waiting a long time for a wildcard scan.
722 */
723 if (!wpa_s->prev_scan_wildcard) {
724 params.ssids[0].ssid = NULL;
725 params.ssids[0].ssid_len = 0;
726 wpa_s->prev_scan_wildcard = 1;
727 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
728 "wildcard SSID (Interleave with specific)");
729 } else {
730 wpa_s->prev_scan_ssid = ssid;
731 wpa_s->prev_scan_wildcard = 0;
732 wpa_dbg(wpa_s, MSG_DEBUG,
733 "Starting AP scan for specific SSID: %s",
734 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800736 } else if (ssid) {
737 /* max_ssids > 1 */
738
739 wpa_s->prev_scan_ssid = ssid;
740 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
741 "the scan request");
742 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 } else {
744 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
745 params.num_ssids++;
746 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
747 "SSID");
748 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700749#ifdef CONFIG_P2P
750ssid_list_set:
751#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800753 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700754 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755
756 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
757 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
758 "generated frequency list");
759 params.freqs = wpa_s->next_scan_freqs;
760 } else
761 os_free(wpa_s->next_scan_freqs);
762 wpa_s->next_scan_freqs = NULL;
763
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700764 /* See if user specified frequencies. If so, scan only those. */
765 if (wpa_s->conf->freq_list && !params.freqs) {
766 wpa_dbg(wpa_s, MSG_DEBUG,
767 "Optimize scan based on conf->freq_list");
768 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
769 }
770
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700771 /* Use current associated channel? */
772 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
773 int freq = shared_vif_oper_freq(wpa_s);
774 if (freq > 0) {
775 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current "
776 "operating channel (%d MHz) since "
777 "scan_cur_freq is enabled", freq);
778 params.freqs = os_zalloc(sizeof(int) * 2);
779 if (params.freqs)
780 params.freqs[0] = freq;
781 }
782 }
783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 params.filter_ssids = wpa_supplicant_build_filter_ssids(
785 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800786 if (extra_ie) {
787 params.extra_ies = wpabuf_head(extra_ie);
788 params.extra_ies_len = wpabuf_len(extra_ie);
789 }
790
791#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700792 if (wpa_s->p2p_in_provisioning ||
793 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800794 /*
795 * The interface may not yet be in P2P mode, so we have to
796 * explicitly request P2P probe to disable CCK rates.
797 */
798 params.p2p_probe = 1;
799 }
800#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
Dmitry Shmidt04949592012-07-19 12:16:46 -0700802 scan_params = &params;
803
804scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800805#ifdef CONFIG_P2P
806 /*
807 * If the driver does not support multi-channel concurrency and a
808 * virtual interface that shares the same radio with the wpa_s interface
809 * is operating there may not be need to scan other channels apart from
810 * the current operating channel on the other virtual interface. Filter
811 * out other channels in case we are trying to find a connection for a
812 * station interface when we are not configured to prefer station
813 * connection and a concurrent operation is already in process.
814 */
815 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
816 !scan_params->freqs && !params.freqs &&
817 wpas_is_p2p_prioritized(wpa_s) &&
818 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
819 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
820 non_p2p_network_enabled(wpa_s)) {
821 int freq = shared_vif_oper_freq(wpa_s);
822 if (freq > 0) {
823 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
824 "operating channel (%d MHz) since driver does "
825 "not support multi-channel concurrency", freq);
826 params.freqs = os_zalloc(sizeof(int) * 2);
827 if (params.freqs)
828 params.freqs[0] = freq;
829 scan_params->freqs = params.freqs;
830 }
831 }
832#endif /* CONFIG_P2P */
833
Dmitry Shmidt04949592012-07-19 12:16:46 -0700834 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 os_free(params.freqs);
838 os_free(params.filter_ssids);
839
840 if (ret) {
841 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
842 if (prev_state != wpa_s->wpa_state)
843 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800844 /* Restore scan_req since we will try to scan again */
845 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800847 } else {
848 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 }
850}
851
852
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800853void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
854{
855 struct os_time remaining, new_int;
856 int cancelled;
857
858 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
859 &remaining);
860
861 new_int.sec = sec;
862 new_int.usec = 0;
863 if (cancelled && os_time_before(&remaining, &new_int)) {
864 new_int.sec = remaining.sec;
865 new_int.usec = remaining.usec;
866 }
867
868 eloop_register_timeout(new_int.sec, new_int.usec, wpa_supplicant_scan,
869 wpa_s, NULL);
870 wpa_s->scan_interval = sec;
871}
872
873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874/**
875 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
876 * @wpa_s: Pointer to wpa_supplicant data
877 * @sec: Number of seconds after which to scan
878 * @usec: Number of microseconds after which to scan
879 *
880 * This function is used to schedule a scan for neighboring access points after
881 * the specified time.
882 */
883void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
884{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800885#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 /* If there's at least one network that should be specifically scanned
887 * then don't cancel the scan and reschedule. Some drivers do
888 * background scanning which generates frequent scan results, and that
889 * causes the specific SSID scan to get continually pushed back and
890 * never happen, which causes hidden APs to never get probe-scanned.
891 */
892 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
893 wpa_s->conf->ap_scan == 1) {
894 struct wpa_ssid *ssid = wpa_s->conf->ssid;
895
896 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700897 if (!wpas_network_disabled(wpa_s, ssid) &&
898 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 break;
900 ssid = ssid->next;
901 }
902 if (ssid) {
903 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
904 "ensure that specific SSID scans occur");
905 return;
906 }
907 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800908#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
910 sec, usec);
911 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
912 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
913}
914
915
916/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800917 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
918 * @wpa_s: Pointer to wpa_supplicant data
919 * @sec: Number of seconds after which to scan
920 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800921 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922 *
923 * This function is used to schedule periodic scans for neighboring
924 * access points after the specified time.
925 */
926int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
927 int sec, int usec)
928{
929 if (!wpa_s->sched_scan_supported)
930 return -1;
931
932 eloop_register_timeout(sec, usec,
933 wpa_supplicant_delayed_sched_scan_timeout,
934 wpa_s, NULL);
935
936 return 0;
937}
938
939
940/**
941 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
942 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800943 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800944 *
945 * This function is used to schedule periodic scans for neighboring
946 * access points repeating the scan continuously.
947 */
948int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
949{
950 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700951 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800952 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700953 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700954 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800955 int ret;
956 unsigned int max_sched_scan_ssids;
957 int wildcard = 0;
958 int need_ssids;
959
960 if (!wpa_s->sched_scan_supported)
961 return -1;
962
963 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
964 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
965 else
966 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800968 return -1;
969
970 if (wpa_s->sched_scanning) {
971 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
972 return 0;
973 }
974
975 need_ssids = 0;
976 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700977 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978 /* Use wildcard SSID to find this network */
979 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980 } else if (!wpas_network_disabled(wpa_s, ssid) &&
981 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800982 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983
984#ifdef CONFIG_WPS
985 if (!wpas_network_disabled(wpa_s, ssid) &&
986 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
987 /*
988 * Normal scan is more reliable and faster for WPS
989 * operations and since these are for short periods of
990 * time, the benefit of trying to use sched_scan would
991 * be limited.
992 */
993 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
994 "sched_scan for WPS");
995 return -1;
996 }
997#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800998 }
999 if (wildcard)
1000 need_ssids++;
1001
1002 if (wpa_s->normal_scans < 3 &&
1003 (need_ssids <= wpa_s->max_scan_ssids ||
1004 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1005 /*
1006 * When normal scan can speed up operations, use that for the
1007 * first operations before starting the sched_scan to allow
1008 * user space sleep more. We do this only if the normal scan
1009 * has functionality that is suitable for this or if the
1010 * sched_scan does not have better support for multiple SSIDs.
1011 */
1012 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1013 "sched_scan for initial scans (normal_scans=%d)",
1014 wpa_s->normal_scans);
1015 return -1;
1016 }
1017
1018 os_memset(&params, 0, sizeof(params));
1019
1020 /* If we can't allocate space for the filters, we just don't filter */
1021 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1022 sizeof(struct wpa_driver_scan_filter));
1023
1024 prev_state = wpa_s->wpa_state;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001025#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001026 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1027 wpa_s->wpa_state == WPA_INACTIVE)
1028 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001029#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001030
Dmitry Shmidt04949592012-07-19 12:16:46 -07001031 if (wpa_s->autoscan_params != NULL) {
1032 scan_params = wpa_s->autoscan_params;
1033 goto scan;
1034 }
1035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001036 /* Find the starting point from which to continue scanning */
1037 ssid = wpa_s->conf->ssid;
1038 if (wpa_s->prev_sched_ssid) {
1039 while (ssid) {
1040 if (ssid == wpa_s->prev_sched_ssid) {
1041 ssid = ssid->next;
1042 break;
1043 }
1044 ssid = ssid->next;
1045 }
1046 }
1047
1048 if (!ssid || !wpa_s->prev_sched_ssid) {
1049 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001050 if (wpa_s->conf->sched_scan_interval)
1051 wpa_s->sched_scan_interval =
1052 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001053 if (wpa_s->sched_scan_interval == 0)
1054 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001055 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1056 wpa_s->first_sched_scan = 1;
1057 ssid = wpa_s->conf->ssid;
1058 wpa_s->prev_sched_ssid = ssid;
1059 }
1060
1061 if (wildcard) {
1062 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1063 params.num_ssids++;
1064 }
1065
1066 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001067 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001068 goto next;
1069
1070 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1071 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1072 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1073 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1074 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1075 ssid->ssid, ssid->ssid_len);
1076 params.filter_ssids[params.num_filter_ssids].ssid_len =
1077 ssid->ssid_len;
1078 params.num_filter_ssids++;
1079 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1080 {
1081 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1082 "filter for sched_scan - drop filter");
1083 os_free(params.filter_ssids);
1084 params.filter_ssids = NULL;
1085 params.num_filter_ssids = 0;
1086 }
1087
1088 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1089 if (params.num_ssids == max_sched_scan_ssids)
1090 break; /* only room for broadcast SSID */
1091 wpa_dbg(wpa_s, MSG_DEBUG,
1092 "add to active scan ssid: %s",
1093 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1094 params.ssids[params.num_ssids].ssid =
1095 ssid->ssid;
1096 params.ssids[params.num_ssids].ssid_len =
1097 ssid->ssid_len;
1098 params.num_ssids++;
1099 if (params.num_ssids >= max_sched_scan_ssids) {
1100 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101 do {
1102 ssid = ssid->next;
1103 } while (ssid &&
1104 (wpas_network_disabled(wpa_s, ssid) ||
1105 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001106 break;
1107 }
1108 }
1109
1110 next:
1111 wpa_s->prev_sched_ssid = ssid;
1112 ssid = ssid->next;
1113 }
1114
1115 if (params.num_filter_ssids == 0) {
1116 os_free(params.filter_ssids);
1117 params.filter_ssids = NULL;
1118 }
1119
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001120 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1121 if (extra_ie) {
1122 params.extra_ies = wpabuf_head(extra_ie);
1123 params.extra_ies_len = wpabuf_len(extra_ie);
1124 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001125
Dmitry Shmidt04949592012-07-19 12:16:46 -07001126 scan_params = &params;
1127
1128scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001129 if (ssid || !wpa_s->first_sched_scan) {
1130 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001131 "Starting sched scan: interval %d timeout %d",
1132 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001133 } else {
1134 wpa_dbg(wpa_s, MSG_DEBUG,
1135 "Starting sched scan: interval %d (no timeout)",
1136 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001137 }
1138
Dmitry Shmidt04949592012-07-19 12:16:46 -07001139 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001140 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001141 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001142 os_free(params.filter_ssids);
1143 if (ret) {
1144 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1145 if (prev_state != wpa_s->wpa_state)
1146 wpa_supplicant_set_state(wpa_s, prev_state);
1147 return ret;
1148 }
1149
1150 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1151 if (ssid || !wpa_s->first_sched_scan) {
1152 wpa_s->sched_scan_timed_out = 0;
1153 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1154 wpa_supplicant_sched_scan_timeout,
1155 wpa_s, NULL);
1156 wpa_s->first_sched_scan = 0;
1157 wpa_s->sched_scan_timeout /= 2;
1158 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001159 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1160 wpa_s->sched_scan_interval = 10;
1161 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1162 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001163 }
1164
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001165 /* If there is no more ssids, start next time from the beginning */
1166 if (!ssid)
1167 wpa_s->prev_sched_ssid = NULL;
1168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001169 return 0;
1170}
1171
1172
1173/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1175 * @wpa_s: Pointer to wpa_supplicant data
1176 *
1177 * This function is used to cancel a scan request scheduled with
1178 * wpa_supplicant_req_scan().
1179 */
1180void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1181{
1182 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1183 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001184 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001185#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001186 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001187#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188}
1189
1190
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001191/**
1192 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1193 * @wpa_s: Pointer to wpa_supplicant data
1194 *
1195 * This function is used to stop a periodic scheduled scan.
1196 */
1197void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1198{
1199 if (!wpa_s->sched_scanning)
1200 return;
1201
1202 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1203 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1204 wpa_supplicant_stop_sched_scan(wpa_s);
1205}
1206
1207
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001208/**
1209 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1210 * @wpa_s: Pointer to wpa_supplicant data
1211 * @scanning: Whether scanning is currently in progress
1212 *
1213 * This function is to generate scanning notifycations. It is called whenever
1214 * there may have been a change in scanning (scan started, completed, stopped).
1215 * wpas_notify_scanning() is called whenever the scanning state changed from the
1216 * previously notified state.
1217 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1219 int scanning)
1220{
1221 if (wpa_s->scanning != scanning) {
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001222#ifdef ANDROID_P2P
1223 if(!wpa_s->sched_scanning)
1224 wpa_s->scanning = scanning;
1225#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 wpa_s->scanning = scanning;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001227#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228 wpas_notify_scanning(wpa_s);
1229 }
1230}
1231
1232
1233static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1234{
1235 int rate = 0;
1236 const u8 *ie;
1237 int i;
1238
1239 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1240 for (i = 0; ie && i < ie[1]; i++) {
1241 if ((ie[i + 2] & 0x7f) > rate)
1242 rate = ie[i + 2] & 0x7f;
1243 }
1244
1245 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1246 for (i = 0; ie && i < ie[1]; i++) {
1247 if ((ie[i + 2] & 0x7f) > rate)
1248 rate = ie[i + 2] & 0x7f;
1249 }
1250
1251 return rate;
1252}
1253
1254
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001255/**
1256 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1257 * @res: Scan result entry
1258 * @ie: Information element identitifier (WLAN_EID_*)
1259 * Returns: Pointer to the information element (id field) or %NULL if not found
1260 *
1261 * This function returns the first matching information element in the scan
1262 * result.
1263 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1265{
1266 const u8 *end, *pos;
1267
1268 pos = (const u8 *) (res + 1);
1269 end = pos + res->ie_len;
1270
1271 while (pos + 1 < end) {
1272 if (pos + 2 + pos[1] > end)
1273 break;
1274 if (pos[0] == ie)
1275 return pos;
1276 pos += 2 + pos[1];
1277 }
1278
1279 return NULL;
1280}
1281
1282
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001283/**
1284 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1285 * @res: Scan result entry
1286 * @vendor_type: Vendor type (four octets starting the IE payload)
1287 * Returns: Pointer to the information element (id field) or %NULL if not found
1288 *
1289 * This function returns the first matching information element in the scan
1290 * result.
1291 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1293 u32 vendor_type)
1294{
1295 const u8 *end, *pos;
1296
1297 pos = (const u8 *) (res + 1);
1298 end = pos + res->ie_len;
1299
1300 while (pos + 1 < end) {
1301 if (pos + 2 + pos[1] > end)
1302 break;
1303 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1304 vendor_type == WPA_GET_BE32(&pos[2]))
1305 return pos;
1306 pos += 2 + pos[1];
1307 }
1308
1309 return NULL;
1310}
1311
1312
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001313/**
1314 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1315 * @res: Scan result entry
1316 * @vendor_type: Vendor type (four octets starting the IE payload)
1317 * Returns: Pointer to the information element payload or %NULL if not found
1318 *
1319 * This function returns concatenated payload of possibly fragmented vendor
1320 * specific information elements in the scan result. The caller is responsible
1321 * for freeing the returned buffer.
1322 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1324 u32 vendor_type)
1325{
1326 struct wpabuf *buf;
1327 const u8 *end, *pos;
1328
1329 buf = wpabuf_alloc(res->ie_len);
1330 if (buf == NULL)
1331 return NULL;
1332
1333 pos = (const u8 *) (res + 1);
1334 end = pos + res->ie_len;
1335
1336 while (pos + 1 < end) {
1337 if (pos + 2 + pos[1] > end)
1338 break;
1339 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1340 vendor_type == WPA_GET_BE32(&pos[2]))
1341 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1342 pos += 2 + pos[1];
1343 }
1344
1345 if (wpabuf_len(buf) == 0) {
1346 wpabuf_free(buf);
1347 buf = NULL;
1348 }
1349
1350 return buf;
1351}
1352
1353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001354/*
1355 * Channels with a great SNR can operate at full rate. What is a great SNR?
1356 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1357 * rule of thumb is that any SNR above 20 is good." This one
1358 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1359 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1360 * conservative value.
1361 */
1362#define GREAT_SNR 30
1363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364/* Compare function for sorting scan results. Return >0 if @b is considered
1365 * better. */
1366static int wpa_scan_result_compar(const void *a, const void *b)
1367{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001368#define IS_5GHZ(n) (n > 4000)
1369#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 struct wpa_scan_res **_wa = (void *) a;
1371 struct wpa_scan_res **_wb = (void *) b;
1372 struct wpa_scan_res *wa = *_wa;
1373 struct wpa_scan_res *wb = *_wb;
1374 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001375 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376
1377 /* WPA/WPA2 support preferred */
1378 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1379 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1380 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1381 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1382
1383 if (wpa_b && !wpa_a)
1384 return 1;
1385 if (!wpa_b && wpa_a)
1386 return -1;
1387
1388 /* privacy support preferred */
1389 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1390 (wb->caps & IEEE80211_CAP_PRIVACY))
1391 return 1;
1392 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1393 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1394 return -1;
1395
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001396 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1397 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1398 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1399 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1400 } else {
1401 /* Not suitable information to calculate SNR, so use level */
1402 snr_a = wa->level;
1403 snr_b = wb->level;
1404 }
1405
1406 /* best/max rate preferred if SNR close enough */
1407 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1409 maxrate_a = wpa_scan_get_max_rate(wa);
1410 maxrate_b = wpa_scan_get_max_rate(wb);
1411 if (maxrate_a != maxrate_b)
1412 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001413 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1414 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 }
1416
1417 /* use freq for channel preference */
1418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001419 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 * identical, use quality values since some drivers may only report
1421 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001422 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001424 return snr_b - snr_a;
1425#undef MIN
1426#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427}
1428
1429
1430#ifdef CONFIG_WPS
1431/* Compare function for sorting scan results when searching a WPS AP for
1432 * provisioning. Return >0 if @b is considered better. */
1433static int wpa_scan_result_wps_compar(const void *a, const void *b)
1434{
1435 struct wpa_scan_res **_wa = (void *) a;
1436 struct wpa_scan_res **_wb = (void *) b;
1437 struct wpa_scan_res *wa = *_wa;
1438 struct wpa_scan_res *wb = *_wb;
1439 int uses_wps_a, uses_wps_b;
1440 struct wpabuf *wps_a, *wps_b;
1441 int res;
1442
1443 /* Optimization - check WPS IE existence before allocated memory and
1444 * doing full reassembly. */
1445 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1446 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1447 if (uses_wps_a && !uses_wps_b)
1448 return -1;
1449 if (!uses_wps_a && uses_wps_b)
1450 return 1;
1451
1452 if (uses_wps_a && uses_wps_b) {
1453 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1454 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1455 res = wps_ap_priority_compar(wps_a, wps_b);
1456 wpabuf_free(wps_a);
1457 wpabuf_free(wps_b);
1458 if (res)
1459 return res;
1460 }
1461
1462 /*
1463 * Do not use current AP security policy as a sorting criteria during
1464 * WPS provisioning step since the AP may get reconfigured at the
1465 * completion of provisioning.
1466 */
1467
1468 /* all things being equal, use signal level; if signal levels are
1469 * identical, use quality values since some drivers may only report
1470 * that value and leave the signal level zero */
1471 if (wb->level == wa->level)
1472 return wb->qual - wa->qual;
1473 return wb->level - wa->level;
1474}
1475#endif /* CONFIG_WPS */
1476
1477
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001478static void dump_scan_res(struct wpa_scan_results *scan_res)
1479{
1480#ifndef CONFIG_NO_STDOUT_DEBUG
1481 size_t i;
1482
1483 if (scan_res->res == NULL || scan_res->num == 0)
1484 return;
1485
1486 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1487
1488 for (i = 0; i < scan_res->num; i++) {
1489 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001490 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001491 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1492 == WPA_SCAN_LEVEL_DBM) {
1493 int snr = r->level - r->noise;
1494 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001495 "noise=%d level=%d snr=%d%s flags=0x%x "
1496 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001497 MAC2STR(r->bssid), r->freq, r->qual,
1498 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001499 snr >= GREAT_SNR ? "*" : "", r->flags,
1500 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001501 } else {
1502 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001503 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001505 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001506 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001507 pos = (u8 *) (r + 1);
1508 if (r->ie_len)
1509 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1510 pos += r->ie_len;
1511 if (r->beacon_ie_len)
1512 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1513 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001514 }
1515#endif /* CONFIG_NO_STDOUT_DEBUG */
1516}
1517
1518
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001519/**
1520 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1521 * @wpa_s: Pointer to wpa_supplicant data
1522 * @bssid: BSSID to check
1523 * Returns: 0 if the BSSID is filtered or 1 if not
1524 *
1525 * This function is used to filter out specific BSSIDs from scan reslts mainly
1526 * for testing purposes (SET bssid_filter ctrl_iface command).
1527 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001528int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1529 const u8 *bssid)
1530{
1531 size_t i;
1532
1533 if (wpa_s->bssid_filter == NULL)
1534 return 1;
1535
1536 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1537 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1538 ETH_ALEN) == 0)
1539 return 1;
1540 }
1541
1542 return 0;
1543}
1544
1545
1546static void filter_scan_res(struct wpa_supplicant *wpa_s,
1547 struct wpa_scan_results *res)
1548{
1549 size_t i, j;
1550
1551 if (wpa_s->bssid_filter == NULL)
1552 return;
1553
1554 for (i = 0, j = 0; i < res->num; i++) {
1555 if (wpa_supplicant_filter_bssid_match(wpa_s,
1556 res->res[i]->bssid)) {
1557 res->res[j++] = res->res[i];
1558 } else {
1559 os_free(res->res[i]);
1560 res->res[i] = NULL;
1561 }
1562 }
1563
1564 if (res->num != j) {
1565 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1566 (int) (res->num - j));
1567 res->num = j;
1568 }
1569}
1570
1571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572/**
1573 * wpa_supplicant_get_scan_results - Get scan results
1574 * @wpa_s: Pointer to wpa_supplicant data
1575 * @info: Information about what was scanned or %NULL if not available
1576 * @new_scan: Whether a new scan was performed
1577 * Returns: Scan results, %NULL on failure
1578 *
1579 * This function request the current scan results from the driver and updates
1580 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1581 * results with wpa_scan_results_free().
1582 */
1583struct wpa_scan_results *
1584wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1585 struct scan_info *info, int new_scan)
1586{
1587 struct wpa_scan_results *scan_res;
1588 size_t i;
1589 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1590
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001591 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 if (scan_res == NULL) {
1593 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1594 return NULL;
1595 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001596 if (scan_res->fetch_time.sec == 0) {
1597 /*
1598 * Make sure we have a valid timestamp if the driver wrapper
1599 * does not set this.
1600 */
1601 os_get_time(&scan_res->fetch_time);
1602 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001603 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604
1605#ifdef CONFIG_WPS
1606 if (wpas_wps_in_progress(wpa_s)) {
1607 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1608 "provisioning rules");
1609 compar = wpa_scan_result_wps_compar;
1610 }
1611#endif /* CONFIG_WPS */
1612
1613 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1614 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001615 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616
1617 wpa_bss_update_start(wpa_s);
1618 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001619 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1620 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621 wpa_bss_update_end(wpa_s, info, new_scan);
1622
1623 return scan_res;
1624}
1625
1626
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001627/**
1628 * wpa_supplicant_update_scan_results - Update scan results from the driver
1629 * @wpa_s: Pointer to wpa_supplicant data
1630 * Returns: 0 on success, -1 on failure
1631 *
1632 * This function updates the BSS table within wpa_supplicant based on the
1633 * currently available scan results from the driver without requesting a new
1634 * scan. This is used in cases where the driver indicates an association
1635 * (including roaming within ESS) and wpa_supplicant does not yet have the
1636 * needed information to complete the connection (e.g., to perform validation
1637 * steps in 4-way handshake).
1638 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1640{
1641 struct wpa_scan_results *scan_res;
1642 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1643 if (scan_res == NULL)
1644 return -1;
1645 wpa_scan_results_free(scan_res);
1646
1647 return 0;
1648}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001649
1650
1651/**
1652 * scan_only_handler - Reports scan results
1653 */
1654void scan_only_handler(struct wpa_supplicant *wpa_s,
1655 struct wpa_scan_results *scan_res)
1656{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001657 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001658 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1659 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001660 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001661}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001662
1663
1664int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1665{
1666 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1667}