blob: 6f219980c565859c93937905b28ac608beb32d85 [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;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700692#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 } else if (wpa_s->conf->ap_scan == 2) {
694 /*
695 * User-initiated scan request in ap_scan == 2; scan with
696 * wildcard SSID.
697 */
698 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700699#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 } else {
701 struct wpa_ssid *start = ssid, *tssid;
702 int freqs_set = 0;
703 if (ssid == NULL && max_ssids > 1)
704 ssid = wpa_s->conf->ssid;
705 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706 if (!wpas_network_disabled(wpa_s, ssid) &&
707 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
709 ssid->ssid, ssid->ssid_len);
710 params.ssids[params.num_ssids].ssid =
711 ssid->ssid;
712 params.ssids[params.num_ssids].ssid_len =
713 ssid->ssid_len;
714 params.num_ssids++;
715 if (params.num_ssids + 1 >= max_ssids)
716 break;
717 }
718 ssid = ssid->next;
719 if (ssid == start)
720 break;
721 if (ssid == NULL && max_ssids > 1 &&
722 start != wpa_s->conf->ssid)
723 ssid = wpa_s->conf->ssid;
724 }
725
726 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700727 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 continue;
729 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
730 int_array_concat(&params.freqs,
731 tssid->scan_freq);
732 } else {
733 os_free(params.freqs);
734 params.freqs = NULL;
735 }
736 freqs_set = 1;
737 }
738 int_array_sort_unique(params.freqs);
739 }
740
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800741 if (ssid && max_ssids == 1) {
742 /*
743 * If the driver is limited to 1 SSID at a time interleave
744 * wildcard SSID scans with specific SSID scans to avoid
745 * waiting a long time for a wildcard scan.
746 */
747 if (!wpa_s->prev_scan_wildcard) {
748 params.ssids[0].ssid = NULL;
749 params.ssids[0].ssid_len = 0;
750 wpa_s->prev_scan_wildcard = 1;
751 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
752 "wildcard SSID (Interleave with specific)");
753 } else {
754 wpa_s->prev_scan_ssid = ssid;
755 wpa_s->prev_scan_wildcard = 0;
756 wpa_dbg(wpa_s, MSG_DEBUG,
757 "Starting AP scan for specific SSID: %s",
758 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800760 } else if (ssid) {
761 /* max_ssids > 1 */
762
763 wpa_s->prev_scan_ssid = ssid;
764 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
765 "the scan request");
766 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 } else {
768 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
769 params.num_ssids++;
770 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
771 "SSID");
772 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700773#ifdef CONFIG_P2P
774ssid_list_set:
775#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700778 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779
780 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
781 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
782 "generated frequency list");
783 params.freqs = wpa_s->next_scan_freqs;
784 } else
785 os_free(wpa_s->next_scan_freqs);
786 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700787 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700789 /* See if user specified frequencies. If so, scan only those. */
790 if (wpa_s->conf->freq_list && !params.freqs) {
791 wpa_dbg(wpa_s, MSG_DEBUG,
792 "Optimize scan based on conf->freq_list");
793 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
794 }
795
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700796 /* Use current associated channel? */
797 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700798 unsigned int num = wpa_s->num_multichan_concurrent;
799
800 params.freqs = os_calloc(num + 1, sizeof(int));
801 if (params.freqs) {
802 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
803 if (num > 0) {
804 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
805 "current operating channels since "
806 "scan_cur_freq is enabled");
807 } else {
808 os_free(params.freqs);
809 params.freqs = NULL;
810 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700811 }
812 }
813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 params.filter_ssids = wpa_supplicant_build_filter_ssids(
815 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816 if (extra_ie) {
817 params.extra_ies = wpabuf_head(extra_ie);
818 params.extra_ies_len = wpabuf_len(extra_ie);
819 }
820
821#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700822 if (wpa_s->p2p_in_provisioning ||
823 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800824 /*
825 * The interface may not yet be in P2P mode, so we have to
826 * explicitly request P2P probe to disable CCK rates.
827 */
828 params.p2p_probe = 1;
829 }
830#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831
Dmitry Shmidt04949592012-07-19 12:16:46 -0700832 scan_params = &params;
833
834scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800835#ifdef CONFIG_P2P
836 /*
837 * If the driver does not support multi-channel concurrency and a
838 * virtual interface that shares the same radio with the wpa_s interface
839 * is operating there may not be need to scan other channels apart from
840 * the current operating channel on the other virtual interface. Filter
841 * out other channels in case we are trying to find a connection for a
842 * station interface when we are not configured to prefer station
843 * connection and a concurrent operation is already in process.
844 */
845 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
846 !scan_params->freqs && !params.freqs &&
847 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800848 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
849 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700850 unsigned int num = wpa_s->num_multichan_concurrent;
851
852 params.freqs = os_calloc(num + 1, sizeof(int));
853 if (params.freqs) {
854 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
855 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
856 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
857 } else {
858 os_free(params.freqs);
859 params.freqs = NULL;
860 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800861 }
862 }
863#endif /* CONFIG_P2P */
864
Dmitry Shmidt04949592012-07-19 12:16:46 -0700865 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 os_free(params.freqs);
869 os_free(params.filter_ssids);
870
871 if (ret) {
872 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
873 if (prev_state != wpa_s->wpa_state)
874 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800875 /* Restore scan_req since we will try to scan again */
876 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800878 } else {
879 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 }
881}
882
883
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800884void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
885{
886 struct os_time remaining, new_int;
887 int cancelled;
888
889 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
890 &remaining);
891
892 new_int.sec = sec;
893 new_int.usec = 0;
894 if (cancelled && os_time_before(&remaining, &new_int)) {
895 new_int.sec = remaining.sec;
896 new_int.usec = remaining.usec;
897 }
898
Dmitry Shmidt051af732013-10-22 13:52:46 -0700899 if (cancelled) {
900 eloop_register_timeout(new_int.sec, new_int.usec,
901 wpa_supplicant_scan, wpa_s, NULL);
902 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800903 wpa_s->scan_interval = sec;
904}
905
906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907/**
908 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
909 * @wpa_s: Pointer to wpa_supplicant data
910 * @sec: Number of seconds after which to scan
911 * @usec: Number of microseconds after which to scan
912 *
913 * This function is used to schedule a scan for neighboring access points after
914 * the specified time.
915 */
916void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
917{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800918#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 /* If there's at least one network that should be specifically scanned
920 * then don't cancel the scan and reschedule. Some drivers do
921 * background scanning which generates frequent scan results, and that
922 * causes the specific SSID scan to get continually pushed back and
923 * never happen, which causes hidden APs to never get probe-scanned.
924 */
925 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
926 wpa_s->conf->ap_scan == 1) {
927 struct wpa_ssid *ssid = wpa_s->conf->ssid;
928
929 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700930 if (!wpas_network_disabled(wpa_s, ssid) &&
931 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 break;
933 ssid = ssid->next;
934 }
935 if (ssid) {
936 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
937 "ensure that specific SSID scans occur");
938 return;
939 }
940 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800941#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
943 sec, usec);
944 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
945 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
946}
947
948
949/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800950 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
951 * @wpa_s: Pointer to wpa_supplicant data
952 * @sec: Number of seconds after which to scan
953 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800954 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800955 *
956 * This function is used to schedule periodic scans for neighboring
957 * access points after the specified time.
958 */
959int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
960 int sec, int usec)
961{
962 if (!wpa_s->sched_scan_supported)
963 return -1;
964
965 eloop_register_timeout(sec, usec,
966 wpa_supplicant_delayed_sched_scan_timeout,
967 wpa_s, NULL);
968
969 return 0;
970}
971
972
973/**
974 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
975 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800976 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977 *
978 * This function is used to schedule periodic scans for neighboring
979 * access points repeating the scan continuously.
980 */
981int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
982{
983 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700984 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800985 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700986 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700987 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988 int ret;
989 unsigned int max_sched_scan_ssids;
990 int wildcard = 0;
991 int need_ssids;
992
993 if (!wpa_s->sched_scan_supported)
994 return -1;
995
996 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
997 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
998 else
999 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001000 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001001 return -1;
1002
1003 if (wpa_s->sched_scanning) {
1004 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1005 return 0;
1006 }
1007
1008 need_ssids = 0;
1009 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001011 /* Use wildcard SSID to find this network */
1012 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1014 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001015 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001016
1017#ifdef CONFIG_WPS
1018 if (!wpas_network_disabled(wpa_s, ssid) &&
1019 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1020 /*
1021 * Normal scan is more reliable and faster for WPS
1022 * operations and since these are for short periods of
1023 * time, the benefit of trying to use sched_scan would
1024 * be limited.
1025 */
1026 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1027 "sched_scan for WPS");
1028 return -1;
1029 }
1030#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001031 }
1032 if (wildcard)
1033 need_ssids++;
1034
1035 if (wpa_s->normal_scans < 3 &&
1036 (need_ssids <= wpa_s->max_scan_ssids ||
1037 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1038 /*
1039 * When normal scan can speed up operations, use that for the
1040 * first operations before starting the sched_scan to allow
1041 * user space sleep more. We do this only if the normal scan
1042 * has functionality that is suitable for this or if the
1043 * sched_scan does not have better support for multiple SSIDs.
1044 */
1045 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1046 "sched_scan for initial scans (normal_scans=%d)",
1047 wpa_s->normal_scans);
1048 return -1;
1049 }
1050
1051 os_memset(&params, 0, sizeof(params));
1052
1053 /* If we can't allocate space for the filters, we just don't filter */
1054 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1055 sizeof(struct wpa_driver_scan_filter));
1056
1057 prev_state = wpa_s->wpa_state;
1058 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1059 wpa_s->wpa_state == WPA_INACTIVE)
1060 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1061
Dmitry Shmidt04949592012-07-19 12:16:46 -07001062 if (wpa_s->autoscan_params != NULL) {
1063 scan_params = wpa_s->autoscan_params;
1064 goto scan;
1065 }
1066
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001067 /* Find the starting point from which to continue scanning */
1068 ssid = wpa_s->conf->ssid;
1069 if (wpa_s->prev_sched_ssid) {
1070 while (ssid) {
1071 if (ssid == wpa_s->prev_sched_ssid) {
1072 ssid = ssid->next;
1073 break;
1074 }
1075 ssid = ssid->next;
1076 }
1077 }
1078
1079 if (!ssid || !wpa_s->prev_sched_ssid) {
1080 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001081 if (wpa_s->conf->sched_scan_interval)
1082 wpa_s->sched_scan_interval =
1083 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001084 if (wpa_s->sched_scan_interval == 0)
1085 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1087 wpa_s->first_sched_scan = 1;
1088 ssid = wpa_s->conf->ssid;
1089 wpa_s->prev_sched_ssid = ssid;
1090 }
1091
1092 if (wildcard) {
1093 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1094 params.num_ssids++;
1095 }
1096
1097 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001098 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001099 goto next;
1100
1101 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1102 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1103 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1104 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1105 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1106 ssid->ssid, ssid->ssid_len);
1107 params.filter_ssids[params.num_filter_ssids].ssid_len =
1108 ssid->ssid_len;
1109 params.num_filter_ssids++;
1110 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1111 {
1112 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1113 "filter for sched_scan - drop filter");
1114 os_free(params.filter_ssids);
1115 params.filter_ssids = NULL;
1116 params.num_filter_ssids = 0;
1117 }
1118
1119 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1120 if (params.num_ssids == max_sched_scan_ssids)
1121 break; /* only room for broadcast SSID */
1122 wpa_dbg(wpa_s, MSG_DEBUG,
1123 "add to active scan ssid: %s",
1124 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1125 params.ssids[params.num_ssids].ssid =
1126 ssid->ssid;
1127 params.ssids[params.num_ssids].ssid_len =
1128 ssid->ssid_len;
1129 params.num_ssids++;
1130 if (params.num_ssids >= max_sched_scan_ssids) {
1131 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001132 do {
1133 ssid = ssid->next;
1134 } while (ssid &&
1135 (wpas_network_disabled(wpa_s, ssid) ||
1136 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001137 break;
1138 }
1139 }
1140
1141 next:
1142 wpa_s->prev_sched_ssid = ssid;
1143 ssid = ssid->next;
1144 }
1145
1146 if (params.num_filter_ssids == 0) {
1147 os_free(params.filter_ssids);
1148 params.filter_ssids = NULL;
1149 }
1150
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001151 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1152 if (extra_ie) {
1153 params.extra_ies = wpabuf_head(extra_ie);
1154 params.extra_ies_len = wpabuf_len(extra_ie);
1155 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001156
Dmitry Shmidt04949592012-07-19 12:16:46 -07001157 scan_params = &params;
1158
1159scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001160 if (ssid || !wpa_s->first_sched_scan) {
1161 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001162 "Starting sched scan: interval %d timeout %d",
1163 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001164 } else {
1165 wpa_dbg(wpa_s, MSG_DEBUG,
1166 "Starting sched scan: interval %d (no timeout)",
1167 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001168 }
1169
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001170 wpa_setband_scan_freqs(wpa_s, scan_params);
1171
Dmitry Shmidt04949592012-07-19 12:16:46 -07001172 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001173 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001174 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001175 os_free(params.filter_ssids);
1176 if (ret) {
1177 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1178 if (prev_state != wpa_s->wpa_state)
1179 wpa_supplicant_set_state(wpa_s, prev_state);
1180 return ret;
1181 }
1182
1183 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1184 if (ssid || !wpa_s->first_sched_scan) {
1185 wpa_s->sched_scan_timed_out = 0;
1186 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1187 wpa_supplicant_sched_scan_timeout,
1188 wpa_s, NULL);
1189 wpa_s->first_sched_scan = 0;
1190 wpa_s->sched_scan_timeout /= 2;
1191 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001192 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1193 wpa_s->sched_scan_interval = 10;
1194 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1195 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001196 }
1197
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001198 /* If there is no more ssids, start next time from the beginning */
1199 if (!ssid)
1200 wpa_s->prev_sched_ssid = NULL;
1201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001202 return 0;
1203}
1204
1205
1206/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1208 * @wpa_s: Pointer to wpa_supplicant data
1209 *
1210 * This function is used to cancel a scan request scheduled with
1211 * wpa_supplicant_req_scan().
1212 */
1213void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1214{
1215 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1216 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001217 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001218#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001219 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001220#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221}
1222
1223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001224/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001225 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1226 * @wpa_s: Pointer to wpa_supplicant data
1227 *
1228 * This function is used to stop a delayed scheduled scan.
1229 */
1230void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1231{
1232 if (!wpa_s->sched_scan_supported)
1233 return;
1234
1235 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1236 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1237 wpa_s, NULL);
1238}
1239
1240
1241/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001242 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1243 * @wpa_s: Pointer to wpa_supplicant data
1244 *
1245 * This function is used to stop a periodic scheduled scan.
1246 */
1247void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1248{
1249 if (!wpa_s->sched_scanning)
1250 return;
1251
1252 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1253 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1254 wpa_supplicant_stop_sched_scan(wpa_s);
1255}
1256
1257
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001258/**
1259 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1260 * @wpa_s: Pointer to wpa_supplicant data
1261 * @scanning: Whether scanning is currently in progress
1262 *
1263 * This function is to generate scanning notifycations. It is called whenever
1264 * there may have been a change in scanning (scan started, completed, stopped).
1265 * wpas_notify_scanning() is called whenever the scanning state changed from the
1266 * previously notified state.
1267 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1269 int scanning)
1270{
1271 if (wpa_s->scanning != scanning) {
1272 wpa_s->scanning = scanning;
1273 wpas_notify_scanning(wpa_s);
1274 }
1275}
1276
1277
1278static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1279{
1280 int rate = 0;
1281 const u8 *ie;
1282 int i;
1283
1284 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1285 for (i = 0; ie && i < ie[1]; i++) {
1286 if ((ie[i + 2] & 0x7f) > rate)
1287 rate = ie[i + 2] & 0x7f;
1288 }
1289
1290 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1291 for (i = 0; ie && i < ie[1]; i++) {
1292 if ((ie[i + 2] & 0x7f) > rate)
1293 rate = ie[i + 2] & 0x7f;
1294 }
1295
1296 return rate;
1297}
1298
1299
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001300/**
1301 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1302 * @res: Scan result entry
1303 * @ie: Information element identitifier (WLAN_EID_*)
1304 * Returns: Pointer to the information element (id field) or %NULL if not found
1305 *
1306 * This function returns the first matching information element in the scan
1307 * result.
1308 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1310{
1311 const u8 *end, *pos;
1312
1313 pos = (const u8 *) (res + 1);
1314 end = pos + res->ie_len;
1315
1316 while (pos + 1 < end) {
1317 if (pos + 2 + pos[1] > end)
1318 break;
1319 if (pos[0] == ie)
1320 return pos;
1321 pos += 2 + pos[1];
1322 }
1323
1324 return NULL;
1325}
1326
1327
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001328/**
1329 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1330 * @res: Scan result entry
1331 * @vendor_type: Vendor type (four octets starting the IE payload)
1332 * Returns: Pointer to the information element (id field) or %NULL if not found
1333 *
1334 * This function returns the first matching information element in the scan
1335 * result.
1336 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1338 u32 vendor_type)
1339{
1340 const u8 *end, *pos;
1341
1342 pos = (const u8 *) (res + 1);
1343 end = pos + res->ie_len;
1344
1345 while (pos + 1 < end) {
1346 if (pos + 2 + pos[1] > end)
1347 break;
1348 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1349 vendor_type == WPA_GET_BE32(&pos[2]))
1350 return pos;
1351 pos += 2 + pos[1];
1352 }
1353
1354 return NULL;
1355}
1356
1357
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001358/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001359 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1360 * @res: Scan result entry
1361 * @vendor_type: Vendor type (four octets starting the IE payload)
1362 * Returns: Pointer to the information element (id field) or %NULL if not found
1363 *
1364 * This function returns the first matching information element in the scan
1365 * result.
1366 *
1367 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1368 * from Beacon frames instead of either Beacon or Probe Response frames.
1369 */
1370const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1371 u32 vendor_type)
1372{
1373 const u8 *end, *pos;
1374
1375 if (res->beacon_ie_len == 0)
1376 return NULL;
1377
1378 pos = (const u8 *) (res + 1);
1379 pos += res->ie_len;
1380 end = pos + res->beacon_ie_len;
1381
1382 while (pos + 1 < end) {
1383 if (pos + 2 + pos[1] > end)
1384 break;
1385 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1386 vendor_type == WPA_GET_BE32(&pos[2]))
1387 return pos;
1388 pos += 2 + pos[1];
1389 }
1390
1391 return NULL;
1392}
1393
1394
1395/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001396 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1397 * @res: Scan result entry
1398 * @vendor_type: Vendor type (four octets starting the IE payload)
1399 * Returns: Pointer to the information element payload or %NULL if not found
1400 *
1401 * This function returns concatenated payload of possibly fragmented vendor
1402 * specific information elements in the scan result. The caller is responsible
1403 * for freeing the returned buffer.
1404 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1406 u32 vendor_type)
1407{
1408 struct wpabuf *buf;
1409 const u8 *end, *pos;
1410
1411 buf = wpabuf_alloc(res->ie_len);
1412 if (buf == NULL)
1413 return NULL;
1414
1415 pos = (const u8 *) (res + 1);
1416 end = pos + res->ie_len;
1417
1418 while (pos + 1 < end) {
1419 if (pos + 2 + pos[1] > end)
1420 break;
1421 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1422 vendor_type == WPA_GET_BE32(&pos[2]))
1423 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1424 pos += 2 + pos[1];
1425 }
1426
1427 if (wpabuf_len(buf) == 0) {
1428 wpabuf_free(buf);
1429 buf = NULL;
1430 }
1431
1432 return buf;
1433}
1434
1435
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001436/*
1437 * Channels with a great SNR can operate at full rate. What is a great SNR?
1438 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1439 * rule of thumb is that any SNR above 20 is good." This one
1440 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1441 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1442 * conservative value.
1443 */
1444#define GREAT_SNR 30
1445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446/* Compare function for sorting scan results. Return >0 if @b is considered
1447 * better. */
1448static int wpa_scan_result_compar(const void *a, const void *b)
1449{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001450#define IS_5GHZ(n) (n > 4000)
1451#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 struct wpa_scan_res **_wa = (void *) a;
1453 struct wpa_scan_res **_wb = (void *) b;
1454 struct wpa_scan_res *wa = *_wa;
1455 struct wpa_scan_res *wb = *_wb;
1456 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001457 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458
1459 /* WPA/WPA2 support preferred */
1460 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1461 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1462 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1463 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1464
1465 if (wpa_b && !wpa_a)
1466 return 1;
1467 if (!wpa_b && wpa_a)
1468 return -1;
1469
1470 /* privacy support preferred */
1471 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1472 (wb->caps & IEEE80211_CAP_PRIVACY))
1473 return 1;
1474 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1475 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1476 return -1;
1477
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001478 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1479 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1480 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1481 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1482 } else {
1483 /* Not suitable information to calculate SNR, so use level */
1484 snr_a = wa->level;
1485 snr_b = wb->level;
1486 }
1487
1488 /* best/max rate preferred if SNR close enough */
1489 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1491 maxrate_a = wpa_scan_get_max_rate(wa);
1492 maxrate_b = wpa_scan_get_max_rate(wb);
1493 if (maxrate_a != maxrate_b)
1494 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001495 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1496 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497 }
1498
1499 /* use freq for channel preference */
1500
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001501 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 * identical, use quality values since some drivers may only report
1503 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001506 return snr_b - snr_a;
1507#undef MIN
1508#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509}
1510
1511
1512#ifdef CONFIG_WPS
1513/* Compare function for sorting scan results when searching a WPS AP for
1514 * provisioning. Return >0 if @b is considered better. */
1515static int wpa_scan_result_wps_compar(const void *a, const void *b)
1516{
1517 struct wpa_scan_res **_wa = (void *) a;
1518 struct wpa_scan_res **_wb = (void *) b;
1519 struct wpa_scan_res *wa = *_wa;
1520 struct wpa_scan_res *wb = *_wb;
1521 int uses_wps_a, uses_wps_b;
1522 struct wpabuf *wps_a, *wps_b;
1523 int res;
1524
1525 /* Optimization - check WPS IE existence before allocated memory and
1526 * doing full reassembly. */
1527 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1528 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1529 if (uses_wps_a && !uses_wps_b)
1530 return -1;
1531 if (!uses_wps_a && uses_wps_b)
1532 return 1;
1533
1534 if (uses_wps_a && uses_wps_b) {
1535 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1536 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1537 res = wps_ap_priority_compar(wps_a, wps_b);
1538 wpabuf_free(wps_a);
1539 wpabuf_free(wps_b);
1540 if (res)
1541 return res;
1542 }
1543
1544 /*
1545 * Do not use current AP security policy as a sorting criteria during
1546 * WPS provisioning step since the AP may get reconfigured at the
1547 * completion of provisioning.
1548 */
1549
1550 /* all things being equal, use signal level; if signal levels are
1551 * identical, use quality values since some drivers may only report
1552 * that value and leave the signal level zero */
1553 if (wb->level == wa->level)
1554 return wb->qual - wa->qual;
1555 return wb->level - wa->level;
1556}
1557#endif /* CONFIG_WPS */
1558
1559
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001560static void dump_scan_res(struct wpa_scan_results *scan_res)
1561{
1562#ifndef CONFIG_NO_STDOUT_DEBUG
1563 size_t i;
1564
1565 if (scan_res->res == NULL || scan_res->num == 0)
1566 return;
1567
1568 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1569
1570 for (i = 0; i < scan_res->num; i++) {
1571 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001572 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001573 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1574 == WPA_SCAN_LEVEL_DBM) {
1575 int snr = r->level - r->noise;
1576 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001577 "noise=%d level=%d snr=%d%s flags=0x%x "
1578 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001579 MAC2STR(r->bssid), r->freq, r->qual,
1580 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001581 snr >= GREAT_SNR ? "*" : "", r->flags,
1582 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001583 } else {
1584 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001585 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001586 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001587 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001588 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001589 pos = (u8 *) (r + 1);
1590 if (r->ie_len)
1591 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1592 pos += r->ie_len;
1593 if (r->beacon_ie_len)
1594 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1595 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001596 }
1597#endif /* CONFIG_NO_STDOUT_DEBUG */
1598}
1599
1600
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001601/**
1602 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1603 * @wpa_s: Pointer to wpa_supplicant data
1604 * @bssid: BSSID to check
1605 * Returns: 0 if the BSSID is filtered or 1 if not
1606 *
1607 * This function is used to filter out specific BSSIDs from scan reslts mainly
1608 * for testing purposes (SET bssid_filter ctrl_iface command).
1609 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001610int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1611 const u8 *bssid)
1612{
1613 size_t i;
1614
1615 if (wpa_s->bssid_filter == NULL)
1616 return 1;
1617
1618 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1619 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1620 ETH_ALEN) == 0)
1621 return 1;
1622 }
1623
1624 return 0;
1625}
1626
1627
1628static void filter_scan_res(struct wpa_supplicant *wpa_s,
1629 struct wpa_scan_results *res)
1630{
1631 size_t i, j;
1632
1633 if (wpa_s->bssid_filter == NULL)
1634 return;
1635
1636 for (i = 0, j = 0; i < res->num; i++) {
1637 if (wpa_supplicant_filter_bssid_match(wpa_s,
1638 res->res[i]->bssid)) {
1639 res->res[j++] = res->res[i];
1640 } else {
1641 os_free(res->res[i]);
1642 res->res[i] = NULL;
1643 }
1644 }
1645
1646 if (res->num != j) {
1647 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1648 (int) (res->num - j));
1649 res->num = j;
1650 }
1651}
1652
1653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654/**
1655 * wpa_supplicant_get_scan_results - Get scan results
1656 * @wpa_s: Pointer to wpa_supplicant data
1657 * @info: Information about what was scanned or %NULL if not available
1658 * @new_scan: Whether a new scan was performed
1659 * Returns: Scan results, %NULL on failure
1660 *
1661 * This function request the current scan results from the driver and updates
1662 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1663 * results with wpa_scan_results_free().
1664 */
1665struct wpa_scan_results *
1666wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1667 struct scan_info *info, int new_scan)
1668{
1669 struct wpa_scan_results *scan_res;
1670 size_t i;
1671 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1672
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001673 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 if (scan_res == NULL) {
1675 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1676 return NULL;
1677 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001678 if (scan_res->fetch_time.sec == 0) {
1679 /*
1680 * Make sure we have a valid timestamp if the driver wrapper
1681 * does not set this.
1682 */
1683 os_get_time(&scan_res->fetch_time);
1684 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001685 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686
1687#ifdef CONFIG_WPS
1688 if (wpas_wps_in_progress(wpa_s)) {
1689 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1690 "provisioning rules");
1691 compar = wpa_scan_result_wps_compar;
1692 }
1693#endif /* CONFIG_WPS */
1694
1695 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1696 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001697 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698
1699 wpa_bss_update_start(wpa_s);
1700 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001701 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1702 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 wpa_bss_update_end(wpa_s, info, new_scan);
1704
1705 return scan_res;
1706}
1707
1708
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001709/**
1710 * wpa_supplicant_update_scan_results - Update scan results from the driver
1711 * @wpa_s: Pointer to wpa_supplicant data
1712 * Returns: 0 on success, -1 on failure
1713 *
1714 * This function updates the BSS table within wpa_supplicant based on the
1715 * currently available scan results from the driver without requesting a new
1716 * scan. This is used in cases where the driver indicates an association
1717 * (including roaming within ESS) and wpa_supplicant does not yet have the
1718 * needed information to complete the connection (e.g., to perform validation
1719 * steps in 4-way handshake).
1720 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1722{
1723 struct wpa_scan_results *scan_res;
1724 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1725 if (scan_res == NULL)
1726 return -1;
1727 wpa_scan_results_free(scan_res);
1728
1729 return 0;
1730}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001731
1732
1733/**
1734 * scan_only_handler - Reports scan results
1735 */
1736void scan_only_handler(struct wpa_supplicant *wpa_s,
1737 struct wpa_scan_results *scan_res)
1738{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001739 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001740 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1741 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001742 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001743}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001744
1745
1746int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1747{
1748 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1749}