blob: 8be68a55d8cc35647fb64df46af89c350e3e9dfc [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{
916 /* If there's at least one network that should be specifically scanned
917 * then don't cancel the scan and reschedule. Some drivers do
918 * background scanning which generates frequent scan results, and that
919 * causes the specific SSID scan to get continually pushed back and
920 * never happen, which causes hidden APs to never get probe-scanned.
921 */
922 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
923 wpa_s->conf->ap_scan == 1) {
924 struct wpa_ssid *ssid = wpa_s->conf->ssid;
925
926 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 if (!wpas_network_disabled(wpa_s, ssid) &&
928 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 break;
930 ssid = ssid->next;
931 }
932 if (ssid) {
933 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
934 "ensure that specific SSID scans occur");
935 return;
936 }
937 }
Dmitry Shmidt394564b2013-11-11 11:19:02 -0800938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
940 sec, usec);
941 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
942 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
943}
944
945
946/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800947 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
948 * @wpa_s: Pointer to wpa_supplicant data
949 * @sec: Number of seconds after which to scan
950 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800951 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800952 *
953 * This function is used to schedule periodic scans for neighboring
954 * access points after the specified time.
955 */
956int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
957 int sec, int usec)
958{
959 if (!wpa_s->sched_scan_supported)
960 return -1;
961
962 eloop_register_timeout(sec, usec,
963 wpa_supplicant_delayed_sched_scan_timeout,
964 wpa_s, NULL);
965
966 return 0;
967}
968
969
970/**
971 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
972 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800973 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800974 *
975 * This function is used to schedule periodic scans for neighboring
976 * access points repeating the scan continuously.
977 */
978int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
979{
980 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700981 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800982 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700984 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800985 int ret;
986 unsigned int max_sched_scan_ssids;
987 int wildcard = 0;
988 int need_ssids;
989
990 if (!wpa_s->sched_scan_supported)
991 return -1;
992
993 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
994 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
995 else
996 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700997 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800998 return -1;
999
1000 if (wpa_s->sched_scanning) {
1001 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1002 return 0;
1003 }
1004
1005 need_ssids = 0;
1006 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001007 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001008 /* Use wildcard SSID to find this network */
1009 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1011 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001012 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013
1014#ifdef CONFIG_WPS
1015 if (!wpas_network_disabled(wpa_s, ssid) &&
1016 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1017 /*
1018 * Normal scan is more reliable and faster for WPS
1019 * operations and since these are for short periods of
1020 * time, the benefit of trying to use sched_scan would
1021 * be limited.
1022 */
1023 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1024 "sched_scan for WPS");
1025 return -1;
1026 }
1027#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001028 }
1029 if (wildcard)
1030 need_ssids++;
1031
1032 if (wpa_s->normal_scans < 3 &&
1033 (need_ssids <= wpa_s->max_scan_ssids ||
1034 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1035 /*
1036 * When normal scan can speed up operations, use that for the
1037 * first operations before starting the sched_scan to allow
1038 * user space sleep more. We do this only if the normal scan
1039 * has functionality that is suitable for this or if the
1040 * sched_scan does not have better support for multiple SSIDs.
1041 */
1042 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1043 "sched_scan for initial scans (normal_scans=%d)",
1044 wpa_s->normal_scans);
1045 return -1;
1046 }
1047
1048 os_memset(&params, 0, sizeof(params));
1049
1050 /* If we can't allocate space for the filters, we just don't filter */
1051 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1052 sizeof(struct wpa_driver_scan_filter));
1053
1054 prev_state = wpa_s->wpa_state;
1055 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1056 wpa_s->wpa_state == WPA_INACTIVE)
1057 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1058
Dmitry Shmidt04949592012-07-19 12:16:46 -07001059 if (wpa_s->autoscan_params != NULL) {
1060 scan_params = wpa_s->autoscan_params;
1061 goto scan;
1062 }
1063
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001064 /* Find the starting point from which to continue scanning */
1065 ssid = wpa_s->conf->ssid;
1066 if (wpa_s->prev_sched_ssid) {
1067 while (ssid) {
1068 if (ssid == wpa_s->prev_sched_ssid) {
1069 ssid = ssid->next;
1070 break;
1071 }
1072 ssid = ssid->next;
1073 }
1074 }
1075
1076 if (!ssid || !wpa_s->prev_sched_ssid) {
1077 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001078 if (wpa_s->conf->sched_scan_interval)
1079 wpa_s->sched_scan_interval =
1080 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001081 if (wpa_s->sched_scan_interval == 0)
1082 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001083 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1084 wpa_s->first_sched_scan = 1;
1085 ssid = wpa_s->conf->ssid;
1086 wpa_s->prev_sched_ssid = ssid;
1087 }
1088
1089 if (wildcard) {
1090 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1091 params.num_ssids++;
1092 }
1093
1094 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001095 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001096 goto next;
1097
1098 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1099 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1100 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1101 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1102 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1103 ssid->ssid, ssid->ssid_len);
1104 params.filter_ssids[params.num_filter_ssids].ssid_len =
1105 ssid->ssid_len;
1106 params.num_filter_ssids++;
1107 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1108 {
1109 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1110 "filter for sched_scan - drop filter");
1111 os_free(params.filter_ssids);
1112 params.filter_ssids = NULL;
1113 params.num_filter_ssids = 0;
1114 }
1115
1116 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1117 if (params.num_ssids == max_sched_scan_ssids)
1118 break; /* only room for broadcast SSID */
1119 wpa_dbg(wpa_s, MSG_DEBUG,
1120 "add to active scan ssid: %s",
1121 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1122 params.ssids[params.num_ssids].ssid =
1123 ssid->ssid;
1124 params.ssids[params.num_ssids].ssid_len =
1125 ssid->ssid_len;
1126 params.num_ssids++;
1127 if (params.num_ssids >= max_sched_scan_ssids) {
1128 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001129 do {
1130 ssid = ssid->next;
1131 } while (ssid &&
1132 (wpas_network_disabled(wpa_s, ssid) ||
1133 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001134 break;
1135 }
1136 }
1137
1138 next:
1139 wpa_s->prev_sched_ssid = ssid;
1140 ssid = ssid->next;
1141 }
1142
1143 if (params.num_filter_ssids == 0) {
1144 os_free(params.filter_ssids);
1145 params.filter_ssids = NULL;
1146 }
1147
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001148 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1149 if (extra_ie) {
1150 params.extra_ies = wpabuf_head(extra_ie);
1151 params.extra_ies_len = wpabuf_len(extra_ie);
1152 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001153
Dmitry Shmidt04949592012-07-19 12:16:46 -07001154 scan_params = &params;
1155
1156scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001157 if (ssid || !wpa_s->first_sched_scan) {
1158 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001159 "Starting sched scan: interval %d timeout %d",
1160 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001161 } else {
1162 wpa_dbg(wpa_s, MSG_DEBUG,
1163 "Starting sched scan: interval %d (no timeout)",
1164 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001165 }
1166
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001167 wpa_setband_scan_freqs(wpa_s, scan_params);
1168
Dmitry Shmidt04949592012-07-19 12:16:46 -07001169 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001170 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001171 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001172 os_free(params.filter_ssids);
1173 if (ret) {
1174 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1175 if (prev_state != wpa_s->wpa_state)
1176 wpa_supplicant_set_state(wpa_s, prev_state);
1177 return ret;
1178 }
1179
1180 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1181 if (ssid || !wpa_s->first_sched_scan) {
1182 wpa_s->sched_scan_timed_out = 0;
1183 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1184 wpa_supplicant_sched_scan_timeout,
1185 wpa_s, NULL);
1186 wpa_s->first_sched_scan = 0;
1187 wpa_s->sched_scan_timeout /= 2;
1188 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001189 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1190 wpa_s->sched_scan_interval = 10;
1191 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1192 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001193 }
1194
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001195 /* If there is no more ssids, start next time from the beginning */
1196 if (!ssid)
1197 wpa_s->prev_sched_ssid = NULL;
1198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001199 return 0;
1200}
1201
1202
1203/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1205 * @wpa_s: Pointer to wpa_supplicant data
1206 *
1207 * This function is used to cancel a scan request scheduled with
1208 * wpa_supplicant_req_scan().
1209 */
1210void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1211{
1212 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1213 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001214 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001215#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001216 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001217#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218}
1219
1220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001222 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1223 * @wpa_s: Pointer to wpa_supplicant data
1224 *
1225 * This function is used to stop a delayed scheduled scan.
1226 */
1227void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1228{
1229 if (!wpa_s->sched_scan_supported)
1230 return;
1231
1232 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1233 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1234 wpa_s, NULL);
1235}
1236
1237
1238/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001239 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1240 * @wpa_s: Pointer to wpa_supplicant data
1241 *
1242 * This function is used to stop a periodic scheduled scan.
1243 */
1244void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1245{
1246 if (!wpa_s->sched_scanning)
1247 return;
1248
1249 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1250 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1251 wpa_supplicant_stop_sched_scan(wpa_s);
1252}
1253
1254
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001255/**
1256 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1257 * @wpa_s: Pointer to wpa_supplicant data
1258 * @scanning: Whether scanning is currently in progress
1259 *
1260 * This function is to generate scanning notifycations. It is called whenever
1261 * there may have been a change in scanning (scan started, completed, stopped).
1262 * wpas_notify_scanning() is called whenever the scanning state changed from the
1263 * previously notified state.
1264 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1266 int scanning)
1267{
1268 if (wpa_s->scanning != scanning) {
1269 wpa_s->scanning = scanning;
1270 wpas_notify_scanning(wpa_s);
1271 }
1272}
1273
1274
1275static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1276{
1277 int rate = 0;
1278 const u8 *ie;
1279 int i;
1280
1281 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1282 for (i = 0; ie && i < ie[1]; i++) {
1283 if ((ie[i + 2] & 0x7f) > rate)
1284 rate = ie[i + 2] & 0x7f;
1285 }
1286
1287 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1288 for (i = 0; ie && i < ie[1]; i++) {
1289 if ((ie[i + 2] & 0x7f) > rate)
1290 rate = ie[i + 2] & 0x7f;
1291 }
1292
1293 return rate;
1294}
1295
1296
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001297/**
1298 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1299 * @res: Scan result entry
1300 * @ie: Information element identitifier (WLAN_EID_*)
1301 * Returns: Pointer to the information element (id field) or %NULL if not found
1302 *
1303 * This function returns the first matching information element in the scan
1304 * result.
1305 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1307{
1308 const u8 *end, *pos;
1309
1310 pos = (const u8 *) (res + 1);
1311 end = pos + res->ie_len;
1312
1313 while (pos + 1 < end) {
1314 if (pos + 2 + pos[1] > end)
1315 break;
1316 if (pos[0] == ie)
1317 return pos;
1318 pos += 2 + pos[1];
1319 }
1320
1321 return NULL;
1322}
1323
1324
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001325/**
1326 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1327 * @res: Scan result entry
1328 * @vendor_type: Vendor type (four octets starting the IE payload)
1329 * Returns: Pointer to the information element (id field) or %NULL if not found
1330 *
1331 * This function returns the first matching information element in the scan
1332 * result.
1333 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1335 u32 vendor_type)
1336{
1337 const u8 *end, *pos;
1338
1339 pos = (const u8 *) (res + 1);
1340 end = pos + res->ie_len;
1341
1342 while (pos + 1 < end) {
1343 if (pos + 2 + pos[1] > end)
1344 break;
1345 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1346 vendor_type == WPA_GET_BE32(&pos[2]))
1347 return pos;
1348 pos += 2 + pos[1];
1349 }
1350
1351 return NULL;
1352}
1353
1354
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001355/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001356 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1357 * @res: Scan result entry
1358 * @vendor_type: Vendor type (four octets starting the IE payload)
1359 * Returns: Pointer to the information element (id field) or %NULL if not found
1360 *
1361 * This function returns the first matching information element in the scan
1362 * result.
1363 *
1364 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1365 * from Beacon frames instead of either Beacon or Probe Response frames.
1366 */
1367const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1368 u32 vendor_type)
1369{
1370 const u8 *end, *pos;
1371
1372 if (res->beacon_ie_len == 0)
1373 return NULL;
1374
1375 pos = (const u8 *) (res + 1);
1376 pos += res->ie_len;
1377 end = pos + res->beacon_ie_len;
1378
1379 while (pos + 1 < end) {
1380 if (pos + 2 + pos[1] > end)
1381 break;
1382 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1383 vendor_type == WPA_GET_BE32(&pos[2]))
1384 return pos;
1385 pos += 2 + pos[1];
1386 }
1387
1388 return NULL;
1389}
1390
1391
1392/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001393 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1394 * @res: Scan result entry
1395 * @vendor_type: Vendor type (four octets starting the IE payload)
1396 * Returns: Pointer to the information element payload or %NULL if not found
1397 *
1398 * This function returns concatenated payload of possibly fragmented vendor
1399 * specific information elements in the scan result. The caller is responsible
1400 * for freeing the returned buffer.
1401 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1403 u32 vendor_type)
1404{
1405 struct wpabuf *buf;
1406 const u8 *end, *pos;
1407
1408 buf = wpabuf_alloc(res->ie_len);
1409 if (buf == NULL)
1410 return NULL;
1411
1412 pos = (const u8 *) (res + 1);
1413 end = pos + res->ie_len;
1414
1415 while (pos + 1 < end) {
1416 if (pos + 2 + pos[1] > end)
1417 break;
1418 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1419 vendor_type == WPA_GET_BE32(&pos[2]))
1420 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1421 pos += 2 + pos[1];
1422 }
1423
1424 if (wpabuf_len(buf) == 0) {
1425 wpabuf_free(buf);
1426 buf = NULL;
1427 }
1428
1429 return buf;
1430}
1431
1432
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001433/*
1434 * Channels with a great SNR can operate at full rate. What is a great SNR?
1435 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1436 * rule of thumb is that any SNR above 20 is good." This one
1437 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1438 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1439 * conservative value.
1440 */
1441#define GREAT_SNR 30
1442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443/* Compare function for sorting scan results. Return >0 if @b is considered
1444 * better. */
1445static int wpa_scan_result_compar(const void *a, const void *b)
1446{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001447#define IS_5GHZ(n) (n > 4000)
1448#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 struct wpa_scan_res **_wa = (void *) a;
1450 struct wpa_scan_res **_wb = (void *) b;
1451 struct wpa_scan_res *wa = *_wa;
1452 struct wpa_scan_res *wb = *_wb;
1453 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001454 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455
1456 /* WPA/WPA2 support preferred */
1457 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1458 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1459 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1460 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1461
1462 if (wpa_b && !wpa_a)
1463 return 1;
1464 if (!wpa_b && wpa_a)
1465 return -1;
1466
1467 /* privacy support preferred */
1468 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1469 (wb->caps & IEEE80211_CAP_PRIVACY))
1470 return 1;
1471 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1472 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1473 return -1;
1474
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001475 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1476 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1477 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1478 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1479 } else {
1480 /* Not suitable information to calculate SNR, so use level */
1481 snr_a = wa->level;
1482 snr_b = wb->level;
1483 }
1484
1485 /* best/max rate preferred if SNR close enough */
1486 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1488 maxrate_a = wpa_scan_get_max_rate(wa);
1489 maxrate_b = wpa_scan_get_max_rate(wb);
1490 if (maxrate_a != maxrate_b)
1491 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1493 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 }
1495
1496 /* use freq for channel preference */
1497
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001498 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499 * identical, use quality values since some drivers may only report
1500 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001501 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001503 return snr_b - snr_a;
1504#undef MIN
1505#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506}
1507
1508
1509#ifdef CONFIG_WPS
1510/* Compare function for sorting scan results when searching a WPS AP for
1511 * provisioning. Return >0 if @b is considered better. */
1512static int wpa_scan_result_wps_compar(const void *a, const void *b)
1513{
1514 struct wpa_scan_res **_wa = (void *) a;
1515 struct wpa_scan_res **_wb = (void *) b;
1516 struct wpa_scan_res *wa = *_wa;
1517 struct wpa_scan_res *wb = *_wb;
1518 int uses_wps_a, uses_wps_b;
1519 struct wpabuf *wps_a, *wps_b;
1520 int res;
1521
1522 /* Optimization - check WPS IE existence before allocated memory and
1523 * doing full reassembly. */
1524 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1525 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1526 if (uses_wps_a && !uses_wps_b)
1527 return -1;
1528 if (!uses_wps_a && uses_wps_b)
1529 return 1;
1530
1531 if (uses_wps_a && uses_wps_b) {
1532 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1533 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1534 res = wps_ap_priority_compar(wps_a, wps_b);
1535 wpabuf_free(wps_a);
1536 wpabuf_free(wps_b);
1537 if (res)
1538 return res;
1539 }
1540
1541 /*
1542 * Do not use current AP security policy as a sorting criteria during
1543 * WPS provisioning step since the AP may get reconfigured at the
1544 * completion of provisioning.
1545 */
1546
1547 /* all things being equal, use signal level; if signal levels are
1548 * identical, use quality values since some drivers may only report
1549 * that value and leave the signal level zero */
1550 if (wb->level == wa->level)
1551 return wb->qual - wa->qual;
1552 return wb->level - wa->level;
1553}
1554#endif /* CONFIG_WPS */
1555
1556
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001557static void dump_scan_res(struct wpa_scan_results *scan_res)
1558{
1559#ifndef CONFIG_NO_STDOUT_DEBUG
1560 size_t i;
1561
1562 if (scan_res->res == NULL || scan_res->num == 0)
1563 return;
1564
1565 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1566
1567 for (i = 0; i < scan_res->num; i++) {
1568 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001569 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001570 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1571 == WPA_SCAN_LEVEL_DBM) {
1572 int snr = r->level - r->noise;
1573 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001574 "noise=%d level=%d snr=%d%s flags=0x%x "
1575 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001576 MAC2STR(r->bssid), r->freq, r->qual,
1577 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001578 snr >= GREAT_SNR ? "*" : "", r->flags,
1579 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001580 } else {
1581 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001582 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001583 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001584 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001585 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001586 pos = (u8 *) (r + 1);
1587 if (r->ie_len)
1588 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1589 pos += r->ie_len;
1590 if (r->beacon_ie_len)
1591 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1592 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 }
1594#endif /* CONFIG_NO_STDOUT_DEBUG */
1595}
1596
1597
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001598/**
1599 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1600 * @wpa_s: Pointer to wpa_supplicant data
1601 * @bssid: BSSID to check
1602 * Returns: 0 if the BSSID is filtered or 1 if not
1603 *
1604 * This function is used to filter out specific BSSIDs from scan reslts mainly
1605 * for testing purposes (SET bssid_filter ctrl_iface command).
1606 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001607int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1608 const u8 *bssid)
1609{
1610 size_t i;
1611
1612 if (wpa_s->bssid_filter == NULL)
1613 return 1;
1614
1615 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1616 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1617 ETH_ALEN) == 0)
1618 return 1;
1619 }
1620
1621 return 0;
1622}
1623
1624
1625static void filter_scan_res(struct wpa_supplicant *wpa_s,
1626 struct wpa_scan_results *res)
1627{
1628 size_t i, j;
1629
1630 if (wpa_s->bssid_filter == NULL)
1631 return;
1632
1633 for (i = 0, j = 0; i < res->num; i++) {
1634 if (wpa_supplicant_filter_bssid_match(wpa_s,
1635 res->res[i]->bssid)) {
1636 res->res[j++] = res->res[i];
1637 } else {
1638 os_free(res->res[i]);
1639 res->res[i] = NULL;
1640 }
1641 }
1642
1643 if (res->num != j) {
1644 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1645 (int) (res->num - j));
1646 res->num = j;
1647 }
1648}
1649
1650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651/**
1652 * wpa_supplicant_get_scan_results - Get scan results
1653 * @wpa_s: Pointer to wpa_supplicant data
1654 * @info: Information about what was scanned or %NULL if not available
1655 * @new_scan: Whether a new scan was performed
1656 * Returns: Scan results, %NULL on failure
1657 *
1658 * This function request the current scan results from the driver and updates
1659 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1660 * results with wpa_scan_results_free().
1661 */
1662struct wpa_scan_results *
1663wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1664 struct scan_info *info, int new_scan)
1665{
1666 struct wpa_scan_results *scan_res;
1667 size_t i;
1668 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001670 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 if (scan_res == NULL) {
1672 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1673 return NULL;
1674 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001675 if (scan_res->fetch_time.sec == 0) {
1676 /*
1677 * Make sure we have a valid timestamp if the driver wrapper
1678 * does not set this.
1679 */
1680 os_get_time(&scan_res->fetch_time);
1681 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001682 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683
1684#ifdef CONFIG_WPS
1685 if (wpas_wps_in_progress(wpa_s)) {
1686 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1687 "provisioning rules");
1688 compar = wpa_scan_result_wps_compar;
1689 }
1690#endif /* CONFIG_WPS */
1691
1692 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1693 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001694 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695
1696 wpa_bss_update_start(wpa_s);
1697 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001698 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1699 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700 wpa_bss_update_end(wpa_s, info, new_scan);
1701
1702 return scan_res;
1703}
1704
1705
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001706/**
1707 * wpa_supplicant_update_scan_results - Update scan results from the driver
1708 * @wpa_s: Pointer to wpa_supplicant data
1709 * Returns: 0 on success, -1 on failure
1710 *
1711 * This function updates the BSS table within wpa_supplicant based on the
1712 * currently available scan results from the driver without requesting a new
1713 * scan. This is used in cases where the driver indicates an association
1714 * (including roaming within ESS) and wpa_supplicant does not yet have the
1715 * needed information to complete the connection (e.g., to perform validation
1716 * steps in 4-way handshake).
1717 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1719{
1720 struct wpa_scan_results *scan_res;
1721 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1722 if (scan_res == NULL)
1723 return -1;
1724 wpa_scan_results_free(scan_res);
1725
1726 return 0;
1727}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001728
1729
1730/**
1731 * scan_only_handler - Reports scan results
1732 */
1733void scan_only_handler(struct wpa_supplicant *wpa_s,
1734 struct wpa_scan_results *scan_res)
1735{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001736 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001737 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1738 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001739 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001740}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001741
1742
1743int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1744{
1745 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1746}