blob: 9dda477a1cdf8e14b782f820ba0f74400adaca17 [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"
Dmitry Shmidt051af732013-10-22 13:52:46 -070024#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "scan.h"
26
27
28static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29{
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
32
33 ssid = wpa_supplicant_get_ssid(wpa_s);
34 if (ssid == NULL)
35 return;
36
37 if (wpa_s->current_ssid == NULL) {
38 wpa_s->current_ssid = ssid;
39 if (wpa_s->current_ssid != NULL)
40 wpas_notify_network_changed(wpa_s);
41 }
42 wpa_supplicant_initiate_eapol(wpa_s);
43 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
44 "network - generating associated event");
45 os_memset(&data, 0, sizeof(data));
46 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
47}
48
49
50#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052 enum wps_request_type *req_type)
53{
54 struct wpa_ssid *ssid;
55 int wps = 0;
56
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080057 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
59 continue;
60
61 wps = 1;
62 *req_type = wpas_wps_get_req_type(ssid);
63 if (!ssid->eap.phase1)
64 continue;
65
66 if (os_strstr(ssid->eap.phase1, "pbc=1"))
67 return 2;
68 }
69
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080070#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080071 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
72 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080073 wpa_s->wps->dev.p2p = 1;
74 if (!wps) {
75 wps = 1;
76 *req_type = WPS_REQ_ENROLLEE_INFO;
77 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080078 }
79#endif /* CONFIG_P2P */
80
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081 return wps;
82}
83#endif /* CONFIG_WPS */
84
85
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080086/**
87 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
88 * @wpa_s: Pointer to wpa_supplicant data
89 * Returns: 0 if no networks are enabled, >0 if networks are enabled
90 *
91 * This function is used to figure out whether any networks (or Interworking
92 * with enabled credentials and auto_interworking) are present in the current
93 * configuration.
94 */
Dmitry Shmidt04949592012-07-19 12:16:46 -070095int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096{
Dmitry Shmidt04949592012-07-19 12:16:46 -070097 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070098 int count = 0, disabled = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700100 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700102 else
103 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 ssid = ssid->next;
105 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700106 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
107 wpa_s->conf->auto_interworking)
108 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700109 if (count == 0 && disabled > 0) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
111 "networks)", disabled);
112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 return count;
114}
115
116
117static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
118 struct wpa_ssid *ssid)
119{
120 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700121 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 break;
123 ssid = ssid->next;
124 }
125
126 /* ap_scan=2 mode - try to associate with each SSID. */
127 if (ssid == NULL) {
128 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
129 "end of scan list - go back to beginning");
130 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
131 wpa_supplicant_req_scan(wpa_s, 0, 0);
132 return;
133 }
134 if (ssid->next) {
135 /* Continue from the next SSID on the next attempt. */
136 wpa_s->prev_scan_ssid = ssid;
137 } else {
138 /* Start from the beginning of the SSID list. */
139 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
140 }
141 wpa_supplicant_associate(wpa_s, NULL, ssid);
142}
143
144
145static int int_array_len(const int *a)
146{
147 int i;
148 for (i = 0; a && a[i]; i++)
149 ;
150 return i;
151}
152
153
154static void int_array_concat(int **res, const int *a)
155{
156 int reslen, alen, i;
157 int *n;
158
159 reslen = int_array_len(*res);
160 alen = int_array_len(a);
161
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700162 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 if (n == NULL) {
164 os_free(*res);
165 *res = NULL;
166 return;
167 }
168 for (i = 0; i <= alen; i++)
169 n[reslen + i] = a[i];
170 *res = n;
171}
172
173
174static int freq_cmp(const void *a, const void *b)
175{
176 int _a = *(int *) a;
177 int _b = *(int *) b;
178
179 if (_a == 0)
180 return 1;
181 if (_b == 0)
182 return -1;
183 return _a - _b;
184}
185
186
187static void int_array_sort_unique(int *a)
188{
189 int alen;
190 int i, j;
191
192 if (a == NULL)
193 return;
194
195 alen = int_array_len(a);
196 qsort(a, alen, sizeof(int), freq_cmp);
197
198 i = 0;
199 j = 1;
200 while (a[i] && a[j]) {
201 if (a[i] == a[j]) {
202 j++;
203 continue;
204 }
205 a[++i] = a[j++];
206 }
207 if (a[i])
208 i++;
209 a[i] = 0;
210}
211
212
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800213/**
214 * wpa_supplicant_trigger_scan - Request driver to start a scan
215 * @wpa_s: Pointer to wpa_supplicant data
216 * @params: Scan parameters
217 * Returns: 0 on success, -1 on failure
218 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
220 struct wpa_driver_scan_params *params)
221{
222 int ret;
223
224 wpa_supplicant_notify_scanning(wpa_s, 1);
225
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800226 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 if (ret) {
228 wpa_supplicant_notify_scanning(wpa_s, 0);
229 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 } else {
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700231 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 wpa_s->scan_runs++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233 wpa_s->normal_scans++;
234 }
235
236 return ret;
237}
238
239
240static void
241wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
242{
243 struct wpa_supplicant *wpa_s = eloop_ctx;
244
245 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
246
247 if (wpa_supplicant_req_sched_scan(wpa_s))
248 wpa_supplicant_req_scan(wpa_s, 0, 0);
249}
250
251
252static void
253wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
254{
255 struct wpa_supplicant *wpa_s = eloop_ctx;
256
257 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
258
259 wpa_s->sched_scan_timed_out = 1;
260 wpa_supplicant_cancel_sched_scan(wpa_s);
261}
262
263
264static int
265wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
266 struct wpa_driver_scan_params *params,
267 int interval)
268{
269 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800271 wpa_supplicant_notify_scanning(wpa_s, 1);
272 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
273 if (ret)
274 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800275 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800276 wpa_s->sched_scanning = 1;
277
278 return ret;
279}
280
281
282static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
283{
284 int ret;
285
286 ret = wpa_drv_stop_sched_scan(wpa_s);
287 if (ret) {
288 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
289 /* TODO: what to do if stopping fails? */
290 return -1;
291 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292
293 return ret;
294}
295
296
297static struct wpa_driver_scan_filter *
298wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
299{
300 struct wpa_driver_scan_filter *ssids;
301 struct wpa_ssid *ssid;
302 size_t count;
303
304 *num_ssids = 0;
305 if (!conf->filter_ssids)
306 return NULL;
307
308 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
309 if (ssid->ssid && ssid->ssid_len)
310 count++;
311 }
312 if (count == 0)
313 return NULL;
314 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
315 if (ssids == NULL)
316 return NULL;
317
318 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
319 if (!ssid->ssid || !ssid->ssid_len)
320 continue;
321 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
322 ssids[*num_ssids].ssid_len = ssid->ssid_len;
323 (*num_ssids)++;
324 }
325
326 return ssids;
327}
328
329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330static void wpa_supplicant_optimize_freqs(
331 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
332{
333#ifdef CONFIG_P2P
334 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
335 wpa_s->go_params) {
336 /* Optimize provisioning state scan based on GO information */
337 if (wpa_s->p2p_in_provisioning < 5 &&
338 wpa_s->go_params->freq > 0) {
339 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
340 "preferred frequency %d MHz",
341 wpa_s->go_params->freq);
342 params->freqs = os_zalloc(2 * sizeof(int));
343 if (params->freqs)
344 params->freqs[0] = wpa_s->go_params->freq;
345 } else if (wpa_s->p2p_in_provisioning < 8 &&
346 wpa_s->go_params->freq_list[0]) {
347 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
348 "channels");
349 int_array_concat(&params->freqs,
350 wpa_s->go_params->freq_list);
351 if (params->freqs)
352 int_array_sort_unique(params->freqs);
353 }
354 wpa_s->p2p_in_provisioning++;
355 }
356#endif /* CONFIG_P2P */
357
358#ifdef CONFIG_WPS
359 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
360 /*
361 * Optimize post-provisioning scan based on channel used
362 * during provisioning.
363 */
364 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
365 "that was used during provisioning", wpa_s->wps_freq);
366 params->freqs = os_zalloc(2 * sizeof(int));
367 if (params->freqs)
368 params->freqs[0] = wpa_s->wps_freq;
369 wpa_s->after_wps--;
370 }
371
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800372 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
373 {
374 /* Optimize provisioning scan based on already known channel */
375 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
376 wpa_s->wps_freq);
377 params->freqs = os_zalloc(2 * sizeof(int));
378 if (params->freqs)
379 params->freqs[0] = wpa_s->wps_freq;
380 wpa_s->known_wps_freq = 0; /* only do this once */
381 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800382#endif /* CONFIG_WPS */
383}
384
385
386#ifdef CONFIG_INTERWORKING
387static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
388 struct wpabuf *buf)
389{
390 if (wpa_s->conf->interworking == 0)
391 return;
392
393 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
394 wpabuf_put_u8(buf, 4);
395 wpabuf_put_u8(buf, 0x00);
396 wpabuf_put_u8(buf, 0x00);
397 wpabuf_put_u8(buf, 0x00);
398 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
399
400 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
401 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
402 1 + ETH_ALEN);
403 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
404 /* No Venue Info */
405 if (!is_zero_ether_addr(wpa_s->conf->hessid))
406 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
407}
408#endif /* CONFIG_INTERWORKING */
409
410
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700411static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800412{
413 struct wpabuf *extra_ie = NULL;
414#ifdef CONFIG_WPS
415 int wps = 0;
416 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
417#endif /* CONFIG_WPS */
418
419#ifdef CONFIG_INTERWORKING
420 if (wpa_s->conf->interworking &&
421 wpabuf_resize(&extra_ie, 100) == 0)
422 wpas_add_interworking_elements(wpa_s, extra_ie);
423#endif /* CONFIG_INTERWORKING */
424
425#ifdef CONFIG_WPS
426 wps = wpas_wps_in_use(wpa_s, &req_type);
427
428 if (wps) {
429 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700430 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
431 DEV_PW_DEFAULT,
432 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800433 wpa_s->wps->uuid, req_type,
434 0, NULL);
435 if (wps_ie) {
436 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
437 wpabuf_put_buf(extra_ie, wps_ie);
438 wpabuf_free(wps_ie);
439 }
440 }
441
442#ifdef CONFIG_P2P
443 if (wps) {
444 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
445 if (wpabuf_resize(&extra_ie, ielen) == 0)
446 wpas_p2p_scan_ie(wpa_s, extra_ie);
447 }
448#endif /* CONFIG_P2P */
449
450#endif /* CONFIG_WPS */
451
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700452#ifdef CONFIG_HS20
453 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
454 wpas_hs20_add_indication(extra_ie);
455#endif /* CONFIG_HS20 */
456
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800457 return extra_ie;
458}
459
460
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800461#ifdef CONFIG_P2P
462
463/*
464 * Check whether there are any enabled networks or credentials that could be
465 * used for a non-P2P connection.
466 */
467static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
468{
469 struct wpa_ssid *ssid;
470
471 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
472 if (wpas_network_disabled(wpa_s, ssid))
473 continue;
474 if (!ssid->p2p_group)
475 return 1;
476 }
477
478 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
479 wpa_s->conf->auto_interworking)
480 return 1;
481
482 return 0;
483}
484
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700485#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800486
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800487
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700488static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
489 u16 num_modes,
490 enum hostapd_hw_mode mode)
491{
492 u16 i;
493
494 for (i = 0; i < num_modes; i++) {
495 if (modes[i].mode == mode)
496 return &modes[i];
497 }
498
499 return NULL;
500}
501
502
503static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
504 enum hostapd_hw_mode band,
505 struct wpa_driver_scan_params *params)
506{
507 /* Include only supported channels for the specified band */
508 struct hostapd_hw_modes *mode;
509 int count, i;
510
511 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
512 if (mode == NULL) {
513 /* No channels supported in this band - use empty list */
514 params->freqs = os_zalloc(sizeof(int));
515 return;
516 }
517
518 params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
519 if (params->freqs == NULL)
520 return;
521 for (count = 0, i = 0; i < mode->num_channels; i++) {
522 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
523 continue;
524 params->freqs[count++] = mode->channels[i].freq;
525 }
526}
527
528
529static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
530 struct wpa_driver_scan_params *params)
531{
532 if (wpa_s->hw.modes == NULL)
533 return; /* unknown what channels the driver supports */
534 if (params->freqs)
535 return; /* already using a limited channel set */
536 if (wpa_s->setband == WPA_SETBAND_5G)
537 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
538 params);
539 else if (wpa_s->setband == WPA_SETBAND_2G)
540 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
541 params);
542}
543
544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
546{
547 struct wpa_supplicant *wpa_s = eloop_ctx;
548 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800549 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
550 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700551 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700553 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 size_t max_ssids;
555 enum wpa_states prev_state;
556
557 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
558 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700559 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 return;
561 }
562
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800563 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700564 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
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 }
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700569#ifdef ANDROID
570 if (wpa_s->scanning) {
571 /* If we are already in scanning state, we shall ignore this new scan request*/
572 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
573 return;
574 }
575#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700576 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800577 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
579 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700580 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 return;
582 }
583
584 if (wpa_s->conf->ap_scan != 0 &&
585 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
586 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
587 "overriding ap_scan configuration");
588 wpa_s->conf->ap_scan = 0;
589 wpas_notify_ap_scan_changed(wpa_s);
590 }
591
592 if (wpa_s->conf->ap_scan == 0) {
593 wpa_supplicant_gen_assoc_event(wpa_s);
594 return;
595 }
596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800597#ifdef CONFIG_P2P
Dmitry Shmidt051af732013-10-22 13:52:46 -0700598 if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s, 0)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700599 if (wpa_s->sta_scan_pending &&
600 wpas_p2p_in_progress(wpa_s) == 2 &&
Jouni Malinendc7b7132012-09-14 12:53:47 -0700601 wpa_s->global->p2p_cb_on_scan_complete) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700602 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
603 "mode scan during P2P search");
604 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
606 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700607 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800608 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700609 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800610 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611 }
612#endif /* CONFIG_P2P */
613
Dmitry Shmidt051af732013-10-22 13:52:46 -0700614#ifdef CONFIG_GAS
615 if (gas_query_in_progress(wpa_s->gas)) {
616 wpa_dbg(wpa_s, MSG_DEBUG, "Delay scan while GAS query is in progress");
617 wpa_supplicant_req_scan(wpa_s, 1, 0);
618 return;
619 }
620#endif /* CONFIG_GAS */
621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800622 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 max_ssids = 1;
624 else {
625 max_ssids = wpa_s->max_scan_ssids;
626 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
627 max_ssids = WPAS_MAX_SCAN_SSIDS;
628 }
629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800631 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632
633 os_memset(&params, 0, sizeof(params));
634
635 prev_state = wpa_s->wpa_state;
636 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
637 wpa_s->wpa_state == WPA_INACTIVE)
638 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
639
Dmitry Shmidt04949592012-07-19 12:16:46 -0700640 /*
641 * If autoscan has set its own scanning parameters
642 */
643 if (wpa_s->autoscan_params != NULL) {
644 scan_params = wpa_s->autoscan_params;
645 goto scan;
646 }
647
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800648 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700649 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
650 if (ssid == wpa_s->connect_without_scan)
651 break;
652 }
653 wpa_s->connect_without_scan = NULL;
654 if (ssid) {
655 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
656 "without scan step");
657 wpa_supplicant_associate(wpa_s, NULL, ssid);
658 return;
659 }
660 }
661
Dmitry Shmidt04949592012-07-19 12:16:46 -0700662#ifdef CONFIG_P2P
663 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
664 wpa_s->go_params) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800665 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
666 wpa_s->p2p_in_provisioning,
667 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700668 params.ssids[0].ssid = wpa_s->go_params->ssid;
669 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
670 params.num_ssids = 1;
671 goto ssid_list_set;
672 }
673#endif /* CONFIG_P2P */
674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 /* Find the starting point from which to continue scanning */
676 ssid = wpa_s->conf->ssid;
677 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
678 while (ssid) {
679 if (ssid == wpa_s->prev_scan_ssid) {
680 ssid = ssid->next;
681 break;
682 }
683 ssid = ssid->next;
684 }
685 }
686
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800687 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700688 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800689 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 wpa_supplicant_assoc_try(wpa_s, ssid);
691 return;
692 } else if (wpa_s->conf->ap_scan == 2) {
693 /*
694 * User-initiated scan request in ap_scan == 2; scan with
695 * wildcard SSID.
696 */
697 ssid = NULL;
698 } else {
699 struct wpa_ssid *start = ssid, *tssid;
700 int freqs_set = 0;
701 if (ssid == NULL && max_ssids > 1)
702 ssid = wpa_s->conf->ssid;
703 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700704 if (!wpas_network_disabled(wpa_s, ssid) &&
705 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
707 ssid->ssid, ssid->ssid_len);
708 params.ssids[params.num_ssids].ssid =
709 ssid->ssid;
710 params.ssids[params.num_ssids].ssid_len =
711 ssid->ssid_len;
712 params.num_ssids++;
713 if (params.num_ssids + 1 >= max_ssids)
714 break;
715 }
716 ssid = ssid->next;
717 if (ssid == start)
718 break;
719 if (ssid == NULL && max_ssids > 1 &&
720 start != wpa_s->conf->ssid)
721 ssid = wpa_s->conf->ssid;
722 }
723
724 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700725 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 continue;
727 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
728 int_array_concat(&params.freqs,
729 tssid->scan_freq);
730 } else {
731 os_free(params.freqs);
732 params.freqs = NULL;
733 }
734 freqs_set = 1;
735 }
736 int_array_sort_unique(params.freqs);
737 }
738
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800739 if (ssid && max_ssids == 1) {
740 /*
741 * If the driver is limited to 1 SSID at a time interleave
742 * wildcard SSID scans with specific SSID scans to avoid
743 * waiting a long time for a wildcard scan.
744 */
745 if (!wpa_s->prev_scan_wildcard) {
746 params.ssids[0].ssid = NULL;
747 params.ssids[0].ssid_len = 0;
748 wpa_s->prev_scan_wildcard = 1;
749 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
750 "wildcard SSID (Interleave with specific)");
751 } else {
752 wpa_s->prev_scan_ssid = ssid;
753 wpa_s->prev_scan_wildcard = 0;
754 wpa_dbg(wpa_s, MSG_DEBUG,
755 "Starting AP scan for specific SSID: %s",
756 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800758 } else if (ssid) {
759 /* max_ssids > 1 */
760
761 wpa_s->prev_scan_ssid = ssid;
762 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
763 "the scan request");
764 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765 } else {
766 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
767 params.num_ssids++;
768 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
769 "SSID");
770 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700771#ifdef CONFIG_P2P
772ssid_list_set:
773#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800775 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700776 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777
778 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
779 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
780 "generated frequency list");
781 params.freqs = wpa_s->next_scan_freqs;
782 } else
783 os_free(wpa_s->next_scan_freqs);
784 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700785 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700787 /* See if user specified frequencies. If so, scan only those. */
788 if (wpa_s->conf->freq_list && !params.freqs) {
789 wpa_dbg(wpa_s, MSG_DEBUG,
790 "Optimize scan based on conf->freq_list");
791 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
792 }
793
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700794 /* Use current associated channel? */
795 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700796 unsigned int num = wpa_s->num_multichan_concurrent;
797
798 params.freqs = os_calloc(num + 1, sizeof(int));
799 if (params.freqs) {
800 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
801 if (num > 0) {
802 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
803 "current operating channels since "
804 "scan_cur_freq is enabled");
805 } else {
806 os_free(params.freqs);
807 params.freqs = NULL;
808 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700809 }
810 }
811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 params.filter_ssids = wpa_supplicant_build_filter_ssids(
813 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800814 if (extra_ie) {
815 params.extra_ies = wpabuf_head(extra_ie);
816 params.extra_ies_len = wpabuf_len(extra_ie);
817 }
818
819#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700820 if (wpa_s->p2p_in_provisioning ||
821 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800822 /*
823 * The interface may not yet be in P2P mode, so we have to
824 * explicitly request P2P probe to disable CCK rates.
825 */
826 params.p2p_probe = 1;
827 }
828#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829
Dmitry Shmidt04949592012-07-19 12:16:46 -0700830 scan_params = &params;
831
832scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800833#ifdef CONFIG_P2P
834 /*
835 * If the driver does not support multi-channel concurrency and a
836 * virtual interface that shares the same radio with the wpa_s interface
837 * is operating there may not be need to scan other channels apart from
838 * the current operating channel on the other virtual interface. Filter
839 * out other channels in case we are trying to find a connection for a
840 * station interface when we are not configured to prefer station
841 * connection and a concurrent operation is already in process.
842 */
843 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
844 !scan_params->freqs && !params.freqs &&
845 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800846 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
847 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700848 unsigned int num = wpa_s->num_multichan_concurrent;
849
850 params.freqs = os_calloc(num + 1, sizeof(int));
851 if (params.freqs) {
852 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
853 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
854 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
855 } else {
856 os_free(params.freqs);
857 params.freqs = NULL;
858 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800859 }
860 }
861#endif /* CONFIG_P2P */
862
Dmitry Shmidt04949592012-07-19 12:16:46 -0700863 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800865 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 os_free(params.freqs);
867 os_free(params.filter_ssids);
868
869 if (ret) {
870 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
871 if (prev_state != wpa_s->wpa_state)
872 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800873 /* Restore scan_req since we will try to scan again */
874 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800876 } else {
877 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 }
879}
880
881
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800882void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
883{
884 struct os_time remaining, new_int;
885 int cancelled;
886
887 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
888 &remaining);
889
890 new_int.sec = sec;
891 new_int.usec = 0;
892 if (cancelled && os_time_before(&remaining, &new_int)) {
893 new_int.sec = remaining.sec;
894 new_int.usec = remaining.usec;
895 }
896
Dmitry Shmidt051af732013-10-22 13:52:46 -0700897 if (cancelled) {
898 eloop_register_timeout(new_int.sec, new_int.usec,
899 wpa_supplicant_scan, wpa_s, NULL);
900 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800901 wpa_s->scan_interval = sec;
902}
903
904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905/**
906 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
907 * @wpa_s: Pointer to wpa_supplicant data
908 * @sec: Number of seconds after which to scan
909 * @usec: Number of microseconds after which to scan
910 *
911 * This function is used to schedule a scan for neighboring access points after
912 * the specified time.
913 */
914void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
915{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800916#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 /* If there's at least one network that should be specifically scanned
918 * then don't cancel the scan and reschedule. Some drivers do
919 * background scanning which generates frequent scan results, and that
920 * causes the specific SSID scan to get continually pushed back and
921 * never happen, which causes hidden APs to never get probe-scanned.
922 */
923 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
924 wpa_s->conf->ap_scan == 1) {
925 struct wpa_ssid *ssid = wpa_s->conf->ssid;
926
927 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700928 if (!wpas_network_disabled(wpa_s, ssid) &&
929 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 break;
931 ssid = ssid->next;
932 }
933 if (ssid) {
934 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
935 "ensure that specific SSID scans occur");
936 return;
937 }
938 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800939#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
941 sec, usec);
942 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
943 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
944}
945
946
947/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
949 * @wpa_s: Pointer to wpa_supplicant data
950 * @sec: Number of seconds after which to scan
951 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800952 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800953 *
954 * This function is used to schedule periodic scans for neighboring
955 * access points after the specified time.
956 */
957int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
958 int sec, int usec)
959{
960 if (!wpa_s->sched_scan_supported)
961 return -1;
962
963 eloop_register_timeout(sec, usec,
964 wpa_supplicant_delayed_sched_scan_timeout,
965 wpa_s, NULL);
966
967 return 0;
968}
969
970
971/**
972 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
973 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800974 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800975 *
976 * This function is used to schedule periodic scans for neighboring
977 * access points repeating the scan continuously.
978 */
979int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
980{
981 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700982 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800983 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700984 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700985 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800986 int ret;
987 unsigned int max_sched_scan_ssids;
988 int wildcard = 0;
989 int need_ssids;
990
991 if (!wpa_s->sched_scan_supported)
992 return -1;
993
994 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
995 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
996 else
997 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700998 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800999 return -1;
1000
1001 if (wpa_s->sched_scanning) {
1002 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1003 return 0;
1004 }
1005
1006 need_ssids = 0;
1007 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001008 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001009 /* Use wildcard SSID to find this network */
1010 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1012 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001013 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001014
1015#ifdef CONFIG_WPS
1016 if (!wpas_network_disabled(wpa_s, ssid) &&
1017 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1018 /*
1019 * Normal scan is more reliable and faster for WPS
1020 * operations and since these are for short periods of
1021 * time, the benefit of trying to use sched_scan would
1022 * be limited.
1023 */
1024 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1025 "sched_scan for WPS");
1026 return -1;
1027 }
1028#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001029 }
1030 if (wildcard)
1031 need_ssids++;
1032
1033 if (wpa_s->normal_scans < 3 &&
1034 (need_ssids <= wpa_s->max_scan_ssids ||
1035 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1036 /*
1037 * When normal scan can speed up operations, use that for the
1038 * first operations before starting the sched_scan to allow
1039 * user space sleep more. We do this only if the normal scan
1040 * has functionality that is suitable for this or if the
1041 * sched_scan does not have better support for multiple SSIDs.
1042 */
1043 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1044 "sched_scan for initial scans (normal_scans=%d)",
1045 wpa_s->normal_scans);
1046 return -1;
1047 }
1048
1049 os_memset(&params, 0, sizeof(params));
1050
1051 /* If we can't allocate space for the filters, we just don't filter */
1052 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1053 sizeof(struct wpa_driver_scan_filter));
1054
1055 prev_state = wpa_s->wpa_state;
1056 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1057 wpa_s->wpa_state == WPA_INACTIVE)
1058 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1059
Dmitry Shmidt04949592012-07-19 12:16:46 -07001060 if (wpa_s->autoscan_params != NULL) {
1061 scan_params = wpa_s->autoscan_params;
1062 goto scan;
1063 }
1064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001065 /* Find the starting point from which to continue scanning */
1066 ssid = wpa_s->conf->ssid;
1067 if (wpa_s->prev_sched_ssid) {
1068 while (ssid) {
1069 if (ssid == wpa_s->prev_sched_ssid) {
1070 ssid = ssid->next;
1071 break;
1072 }
1073 ssid = ssid->next;
1074 }
1075 }
1076
1077 if (!ssid || !wpa_s->prev_sched_ssid) {
1078 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001079 if (wpa_s->conf->sched_scan_interval)
1080 wpa_s->sched_scan_interval =
1081 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001082 if (wpa_s->sched_scan_interval == 0)
1083 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1085 wpa_s->first_sched_scan = 1;
1086 ssid = wpa_s->conf->ssid;
1087 wpa_s->prev_sched_ssid = ssid;
1088 }
1089
1090 if (wildcard) {
1091 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1092 params.num_ssids++;
1093 }
1094
1095 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001096 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001097 goto next;
1098
1099 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1100 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1101 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1102 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1103 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1104 ssid->ssid, ssid->ssid_len);
1105 params.filter_ssids[params.num_filter_ssids].ssid_len =
1106 ssid->ssid_len;
1107 params.num_filter_ssids++;
1108 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1109 {
1110 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1111 "filter for sched_scan - drop filter");
1112 os_free(params.filter_ssids);
1113 params.filter_ssids = NULL;
1114 params.num_filter_ssids = 0;
1115 }
1116
1117 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1118 if (params.num_ssids == max_sched_scan_ssids)
1119 break; /* only room for broadcast SSID */
1120 wpa_dbg(wpa_s, MSG_DEBUG,
1121 "add to active scan ssid: %s",
1122 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1123 params.ssids[params.num_ssids].ssid =
1124 ssid->ssid;
1125 params.ssids[params.num_ssids].ssid_len =
1126 ssid->ssid_len;
1127 params.num_ssids++;
1128 if (params.num_ssids >= max_sched_scan_ssids) {
1129 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001130 do {
1131 ssid = ssid->next;
1132 } while (ssid &&
1133 (wpas_network_disabled(wpa_s, ssid) ||
1134 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001135 break;
1136 }
1137 }
1138
1139 next:
1140 wpa_s->prev_sched_ssid = ssid;
1141 ssid = ssid->next;
1142 }
1143
1144 if (params.num_filter_ssids == 0) {
1145 os_free(params.filter_ssids);
1146 params.filter_ssids = NULL;
1147 }
1148
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001149 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1150 if (extra_ie) {
1151 params.extra_ies = wpabuf_head(extra_ie);
1152 params.extra_ies_len = wpabuf_len(extra_ie);
1153 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001154
Dmitry Shmidt04949592012-07-19 12:16:46 -07001155 scan_params = &params;
1156
1157scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001158 if (ssid || !wpa_s->first_sched_scan) {
1159 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001160 "Starting sched scan: interval %d timeout %d",
1161 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001162 } else {
1163 wpa_dbg(wpa_s, MSG_DEBUG,
1164 "Starting sched scan: interval %d (no timeout)",
1165 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001166 }
1167
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001168 wpa_setband_scan_freqs(wpa_s, scan_params);
1169
Dmitry Shmidt04949592012-07-19 12:16:46 -07001170 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001171 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001172 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001173 os_free(params.filter_ssids);
1174 if (ret) {
1175 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1176 if (prev_state != wpa_s->wpa_state)
1177 wpa_supplicant_set_state(wpa_s, prev_state);
1178 return ret;
1179 }
1180
1181 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1182 if (ssid || !wpa_s->first_sched_scan) {
1183 wpa_s->sched_scan_timed_out = 0;
1184 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1185 wpa_supplicant_sched_scan_timeout,
1186 wpa_s, NULL);
1187 wpa_s->first_sched_scan = 0;
1188 wpa_s->sched_scan_timeout /= 2;
1189 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001190 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1191 wpa_s->sched_scan_interval = 10;
1192 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1193 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001194 }
1195
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001196 /* If there is no more ssids, start next time from the beginning */
1197 if (!ssid)
1198 wpa_s->prev_sched_ssid = NULL;
1199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001200 return 0;
1201}
1202
1203
1204/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1206 * @wpa_s: Pointer to wpa_supplicant data
1207 *
1208 * This function is used to cancel a scan request scheduled with
1209 * wpa_supplicant_req_scan().
1210 */
1211void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1212{
1213 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1214 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001215 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001216#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001217 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001218#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219}
1220
1221
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001222/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001223 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1224 * @wpa_s: Pointer to wpa_supplicant data
1225 *
1226 * This function is used to stop a delayed scheduled scan.
1227 */
1228void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1229{
1230 if (!wpa_s->sched_scan_supported)
1231 return;
1232
1233 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1234 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1235 wpa_s, NULL);
1236}
1237
1238
1239/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001240 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1241 * @wpa_s: Pointer to wpa_supplicant data
1242 *
1243 * This function is used to stop a periodic scheduled scan.
1244 */
1245void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1246{
1247 if (!wpa_s->sched_scanning)
1248 return;
1249
1250 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1251 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1252 wpa_supplicant_stop_sched_scan(wpa_s);
1253}
1254
1255
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001256/**
1257 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1258 * @wpa_s: Pointer to wpa_supplicant data
1259 * @scanning: Whether scanning is currently in progress
1260 *
1261 * This function is to generate scanning notifycations. It is called whenever
1262 * there may have been a change in scanning (scan started, completed, stopped).
1263 * wpas_notify_scanning() is called whenever the scanning state changed from the
1264 * previously notified state.
1265 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1267 int scanning)
1268{
1269 if (wpa_s->scanning != scanning) {
1270 wpa_s->scanning = scanning;
1271 wpas_notify_scanning(wpa_s);
1272 }
1273}
1274
1275
1276static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1277{
1278 int rate = 0;
1279 const u8 *ie;
1280 int i;
1281
1282 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1283 for (i = 0; ie && i < ie[1]; i++) {
1284 if ((ie[i + 2] & 0x7f) > rate)
1285 rate = ie[i + 2] & 0x7f;
1286 }
1287
1288 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1289 for (i = 0; ie && i < ie[1]; i++) {
1290 if ((ie[i + 2] & 0x7f) > rate)
1291 rate = ie[i + 2] & 0x7f;
1292 }
1293
1294 return rate;
1295}
1296
1297
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001298/**
1299 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1300 * @res: Scan result entry
1301 * @ie: Information element identitifier (WLAN_EID_*)
1302 * Returns: Pointer to the information element (id field) or %NULL if not found
1303 *
1304 * This function returns the first matching information element in the scan
1305 * result.
1306 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1308{
1309 const u8 *end, *pos;
1310
1311 pos = (const u8 *) (res + 1);
1312 end = pos + res->ie_len;
1313
1314 while (pos + 1 < end) {
1315 if (pos + 2 + pos[1] > end)
1316 break;
1317 if (pos[0] == ie)
1318 return pos;
1319 pos += 2 + pos[1];
1320 }
1321
1322 return NULL;
1323}
1324
1325
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001326/**
1327 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1328 * @res: Scan result entry
1329 * @vendor_type: Vendor type (four octets starting the IE payload)
1330 * Returns: Pointer to the information element (id field) or %NULL if not found
1331 *
1332 * This function returns the first matching information element in the scan
1333 * result.
1334 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1336 u32 vendor_type)
1337{
1338 const u8 *end, *pos;
1339
1340 pos = (const u8 *) (res + 1);
1341 end = pos + res->ie_len;
1342
1343 while (pos + 1 < end) {
1344 if (pos + 2 + pos[1] > end)
1345 break;
1346 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1347 vendor_type == WPA_GET_BE32(&pos[2]))
1348 return pos;
1349 pos += 2 + pos[1];
1350 }
1351
1352 return NULL;
1353}
1354
1355
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001356/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001357 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1358 * @res: Scan result entry
1359 * @vendor_type: Vendor type (four octets starting the IE payload)
1360 * Returns: Pointer to the information element (id field) or %NULL if not found
1361 *
1362 * This function returns the first matching information element in the scan
1363 * result.
1364 *
1365 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1366 * from Beacon frames instead of either Beacon or Probe Response frames.
1367 */
1368const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1369 u32 vendor_type)
1370{
1371 const u8 *end, *pos;
1372
1373 if (res->beacon_ie_len == 0)
1374 return NULL;
1375
1376 pos = (const u8 *) (res + 1);
1377 pos += res->ie_len;
1378 end = pos + res->beacon_ie_len;
1379
1380 while (pos + 1 < end) {
1381 if (pos + 2 + pos[1] > end)
1382 break;
1383 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1384 vendor_type == WPA_GET_BE32(&pos[2]))
1385 return pos;
1386 pos += 2 + pos[1];
1387 }
1388
1389 return NULL;
1390}
1391
1392
1393/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001394 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1395 * @res: Scan result entry
1396 * @vendor_type: Vendor type (four octets starting the IE payload)
1397 * Returns: Pointer to the information element payload or %NULL if not found
1398 *
1399 * This function returns concatenated payload of possibly fragmented vendor
1400 * specific information elements in the scan result. The caller is responsible
1401 * for freeing the returned buffer.
1402 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1404 u32 vendor_type)
1405{
1406 struct wpabuf *buf;
1407 const u8 *end, *pos;
1408
1409 buf = wpabuf_alloc(res->ie_len);
1410 if (buf == NULL)
1411 return NULL;
1412
1413 pos = (const u8 *) (res + 1);
1414 end = pos + res->ie_len;
1415
1416 while (pos + 1 < end) {
1417 if (pos + 2 + pos[1] > end)
1418 break;
1419 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1420 vendor_type == WPA_GET_BE32(&pos[2]))
1421 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1422 pos += 2 + pos[1];
1423 }
1424
1425 if (wpabuf_len(buf) == 0) {
1426 wpabuf_free(buf);
1427 buf = NULL;
1428 }
1429
1430 return buf;
1431}
1432
1433
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001434/*
1435 * Channels with a great SNR can operate at full rate. What is a great SNR?
1436 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1437 * rule of thumb is that any SNR above 20 is good." This one
1438 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1439 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1440 * conservative value.
1441 */
1442#define GREAT_SNR 30
1443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444/* Compare function for sorting scan results. Return >0 if @b is considered
1445 * better. */
1446static int wpa_scan_result_compar(const void *a, const void *b)
1447{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001448#define IS_5GHZ(n) (n > 4000)
1449#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 struct wpa_scan_res **_wa = (void *) a;
1451 struct wpa_scan_res **_wb = (void *) b;
1452 struct wpa_scan_res *wa = *_wa;
1453 struct wpa_scan_res *wb = *_wb;
1454 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001455 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456
1457 /* WPA/WPA2 support preferred */
1458 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1459 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1460 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1461 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1462
1463 if (wpa_b && !wpa_a)
1464 return 1;
1465 if (!wpa_b && wpa_a)
1466 return -1;
1467
1468 /* privacy support preferred */
1469 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1470 (wb->caps & IEEE80211_CAP_PRIVACY))
1471 return 1;
1472 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1473 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1474 return -1;
1475
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001476 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1477 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1478 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1479 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1480 } else {
1481 /* Not suitable information to calculate SNR, so use level */
1482 snr_a = wa->level;
1483 snr_b = wb->level;
1484 }
1485
1486 /* best/max rate preferred if SNR close enough */
1487 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1489 maxrate_a = wpa_scan_get_max_rate(wa);
1490 maxrate_b = wpa_scan_get_max_rate(wb);
1491 if (maxrate_a != maxrate_b)
1492 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001493 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1494 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 }
1496
1497 /* use freq for channel preference */
1498
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001499 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500 * identical, use quality values since some drivers may only report
1501 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001502 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504 return snr_b - snr_a;
1505#undef MIN
1506#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507}
1508
1509
1510#ifdef CONFIG_WPS
1511/* Compare function for sorting scan results when searching a WPS AP for
1512 * provisioning. Return >0 if @b is considered better. */
1513static int wpa_scan_result_wps_compar(const void *a, const void *b)
1514{
1515 struct wpa_scan_res **_wa = (void *) a;
1516 struct wpa_scan_res **_wb = (void *) b;
1517 struct wpa_scan_res *wa = *_wa;
1518 struct wpa_scan_res *wb = *_wb;
1519 int uses_wps_a, uses_wps_b;
1520 struct wpabuf *wps_a, *wps_b;
1521 int res;
1522
1523 /* Optimization - check WPS IE existence before allocated memory and
1524 * doing full reassembly. */
1525 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1526 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1527 if (uses_wps_a && !uses_wps_b)
1528 return -1;
1529 if (!uses_wps_a && uses_wps_b)
1530 return 1;
1531
1532 if (uses_wps_a && uses_wps_b) {
1533 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1534 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1535 res = wps_ap_priority_compar(wps_a, wps_b);
1536 wpabuf_free(wps_a);
1537 wpabuf_free(wps_b);
1538 if (res)
1539 return res;
1540 }
1541
1542 /*
1543 * Do not use current AP security policy as a sorting criteria during
1544 * WPS provisioning step since the AP may get reconfigured at the
1545 * completion of provisioning.
1546 */
1547
1548 /* all things being equal, use signal level; if signal levels are
1549 * identical, use quality values since some drivers may only report
1550 * that value and leave the signal level zero */
1551 if (wb->level == wa->level)
1552 return wb->qual - wa->qual;
1553 return wb->level - wa->level;
1554}
1555#endif /* CONFIG_WPS */
1556
1557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001558static void dump_scan_res(struct wpa_scan_results *scan_res)
1559{
1560#ifndef CONFIG_NO_STDOUT_DEBUG
1561 size_t i;
1562
1563 if (scan_res->res == NULL || scan_res->num == 0)
1564 return;
1565
1566 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1567
1568 for (i = 0; i < scan_res->num; i++) {
1569 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001570 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001571 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1572 == WPA_SCAN_LEVEL_DBM) {
1573 int snr = r->level - r->noise;
1574 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001575 "noise=%d level=%d snr=%d%s flags=0x%x "
1576 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001577 MAC2STR(r->bssid), r->freq, r->qual,
1578 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001579 snr >= GREAT_SNR ? "*" : "", r->flags,
1580 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581 } else {
1582 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001583 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001585 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001586 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001587 pos = (u8 *) (r + 1);
1588 if (r->ie_len)
1589 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1590 pos += r->ie_len;
1591 if (r->beacon_ie_len)
1592 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1593 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001594 }
1595#endif /* CONFIG_NO_STDOUT_DEBUG */
1596}
1597
1598
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001599/**
1600 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1601 * @wpa_s: Pointer to wpa_supplicant data
1602 * @bssid: BSSID to check
1603 * Returns: 0 if the BSSID is filtered or 1 if not
1604 *
1605 * This function is used to filter out specific BSSIDs from scan reslts mainly
1606 * for testing purposes (SET bssid_filter ctrl_iface command).
1607 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001608int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1609 const u8 *bssid)
1610{
1611 size_t i;
1612
1613 if (wpa_s->bssid_filter == NULL)
1614 return 1;
1615
1616 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1617 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1618 ETH_ALEN) == 0)
1619 return 1;
1620 }
1621
1622 return 0;
1623}
1624
1625
1626static void filter_scan_res(struct wpa_supplicant *wpa_s,
1627 struct wpa_scan_results *res)
1628{
1629 size_t i, j;
1630
1631 if (wpa_s->bssid_filter == NULL)
1632 return;
1633
1634 for (i = 0, j = 0; i < res->num; i++) {
1635 if (wpa_supplicant_filter_bssid_match(wpa_s,
1636 res->res[i]->bssid)) {
1637 res->res[j++] = res->res[i];
1638 } else {
1639 os_free(res->res[i]);
1640 res->res[i] = NULL;
1641 }
1642 }
1643
1644 if (res->num != j) {
1645 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1646 (int) (res->num - j));
1647 res->num = j;
1648 }
1649}
1650
1651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652/**
1653 * wpa_supplicant_get_scan_results - Get scan results
1654 * @wpa_s: Pointer to wpa_supplicant data
1655 * @info: Information about what was scanned or %NULL if not available
1656 * @new_scan: Whether a new scan was performed
1657 * Returns: Scan results, %NULL on failure
1658 *
1659 * This function request the current scan results from the driver and updates
1660 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1661 * results with wpa_scan_results_free().
1662 */
1663struct wpa_scan_results *
1664wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1665 struct scan_info *info, int new_scan)
1666{
1667 struct wpa_scan_results *scan_res;
1668 size_t i;
1669 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1670
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001671 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 if (scan_res == NULL) {
1673 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1674 return NULL;
1675 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001676 if (scan_res->fetch_time.sec == 0) {
1677 /*
1678 * Make sure we have a valid timestamp if the driver wrapper
1679 * does not set this.
1680 */
1681 os_get_time(&scan_res->fetch_time);
1682 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001683 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684
1685#ifdef CONFIG_WPS
1686 if (wpas_wps_in_progress(wpa_s)) {
1687 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1688 "provisioning rules");
1689 compar = wpa_scan_result_wps_compar;
1690 }
1691#endif /* CONFIG_WPS */
1692
1693 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1694 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001695 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696
1697 wpa_bss_update_start(wpa_s);
1698 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001699 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1700 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 wpa_bss_update_end(wpa_s, info, new_scan);
1702
1703 return scan_res;
1704}
1705
1706
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001707/**
1708 * wpa_supplicant_update_scan_results - Update scan results from the driver
1709 * @wpa_s: Pointer to wpa_supplicant data
1710 * Returns: 0 on success, -1 on failure
1711 *
1712 * This function updates the BSS table within wpa_supplicant based on the
1713 * currently available scan results from the driver without requesting a new
1714 * scan. This is used in cases where the driver indicates an association
1715 * (including roaming within ESS) and wpa_supplicant does not yet have the
1716 * needed information to complete the connection (e.g., to perform validation
1717 * steps in 4-way handshake).
1718 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1720{
1721 struct wpa_scan_results *scan_res;
1722 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1723 if (scan_res == NULL)
1724 return -1;
1725 wpa_scan_results_free(scan_res);
1726
1727 return 0;
1728}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001729
1730
1731/**
1732 * scan_only_handler - Reports scan results
1733 */
1734void scan_only_handler(struct wpa_supplicant *wpa_s,
1735 struct wpa_scan_results *scan_res)
1736{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001737 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001738 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1739 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001740 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001741}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001742
1743
1744int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1745{
1746 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1747}