blob: 1d7fabfcaf6b71d2ed7f7792bd96c29bd448cb1c [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 Shmidt687922c2012-03-26 14:02:32 -0700275 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800276 wpa_s->sched_scanning = 1;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700277 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278
279 return ret;
280}
281
282
283static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
284{
285 int ret;
286
287 ret = wpa_drv_stop_sched_scan(wpa_s);
288 if (ret) {
289 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
290 /* TODO: what to do if stopping fails? */
291 return -1;
292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293
294 return ret;
295}
296
297
298static struct wpa_driver_scan_filter *
299wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
300{
301 struct wpa_driver_scan_filter *ssids;
302 struct wpa_ssid *ssid;
303 size_t count;
304
305 *num_ssids = 0;
306 if (!conf->filter_ssids)
307 return NULL;
308
309 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
310 if (ssid->ssid && ssid->ssid_len)
311 count++;
312 }
313 if (count == 0)
314 return NULL;
315 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
316 if (ssids == NULL)
317 return NULL;
318
319 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
320 if (!ssid->ssid || !ssid->ssid_len)
321 continue;
322 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
323 ssids[*num_ssids].ssid_len = ssid->ssid_len;
324 (*num_ssids)++;
325 }
326
327 return ssids;
328}
329
330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800331static void wpa_supplicant_optimize_freqs(
332 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
333{
334#ifdef CONFIG_P2P
335 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
336 wpa_s->go_params) {
337 /* Optimize provisioning state scan based on GO information */
338 if (wpa_s->p2p_in_provisioning < 5 &&
339 wpa_s->go_params->freq > 0) {
340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
341 "preferred frequency %d MHz",
342 wpa_s->go_params->freq);
343 params->freqs = os_zalloc(2 * sizeof(int));
344 if (params->freqs)
345 params->freqs[0] = wpa_s->go_params->freq;
346 } else if (wpa_s->p2p_in_provisioning < 8 &&
347 wpa_s->go_params->freq_list[0]) {
348 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
349 "channels");
350 int_array_concat(&params->freqs,
351 wpa_s->go_params->freq_list);
352 if (params->freqs)
353 int_array_sort_unique(params->freqs);
354 }
355 wpa_s->p2p_in_provisioning++;
356 }
357#endif /* CONFIG_P2P */
358
359#ifdef CONFIG_WPS
360 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
361 /*
362 * Optimize post-provisioning scan based on channel used
363 * during provisioning.
364 */
365 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
366 "that was used during provisioning", wpa_s->wps_freq);
367 params->freqs = os_zalloc(2 * sizeof(int));
368 if (params->freqs)
369 params->freqs[0] = wpa_s->wps_freq;
370 wpa_s->after_wps--;
371 }
372
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800373 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
374 {
375 /* Optimize provisioning scan based on already known channel */
376 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
377 wpa_s->wps_freq);
378 params->freqs = os_zalloc(2 * sizeof(int));
379 if (params->freqs)
380 params->freqs[0] = wpa_s->wps_freq;
381 wpa_s->known_wps_freq = 0; /* only do this once */
382 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383#endif /* CONFIG_WPS */
384}
385
386
387#ifdef CONFIG_INTERWORKING
388static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
389 struct wpabuf *buf)
390{
391 if (wpa_s->conf->interworking == 0)
392 return;
393
394 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
395 wpabuf_put_u8(buf, 4);
396 wpabuf_put_u8(buf, 0x00);
397 wpabuf_put_u8(buf, 0x00);
398 wpabuf_put_u8(buf, 0x00);
399 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
400
401 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
402 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
403 1 + ETH_ALEN);
404 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
405 /* No Venue Info */
406 if (!is_zero_ether_addr(wpa_s->conf->hessid))
407 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
408}
409#endif /* CONFIG_INTERWORKING */
410
411
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700412static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800413{
414 struct wpabuf *extra_ie = NULL;
415#ifdef CONFIG_WPS
416 int wps = 0;
417 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
418#endif /* CONFIG_WPS */
419
420#ifdef CONFIG_INTERWORKING
421 if (wpa_s->conf->interworking &&
422 wpabuf_resize(&extra_ie, 100) == 0)
423 wpas_add_interworking_elements(wpa_s, extra_ie);
424#endif /* CONFIG_INTERWORKING */
425
426#ifdef CONFIG_WPS
427 wps = wpas_wps_in_use(wpa_s, &req_type);
428
429 if (wps) {
430 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700431 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
432 DEV_PW_DEFAULT,
433 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434 wpa_s->wps->uuid, req_type,
435 0, NULL);
436 if (wps_ie) {
437 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
438 wpabuf_put_buf(extra_ie, wps_ie);
439 wpabuf_free(wps_ie);
440 }
441 }
442
443#ifdef CONFIG_P2P
444 if (wps) {
445 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
446 if (wpabuf_resize(&extra_ie, ielen) == 0)
447 wpas_p2p_scan_ie(wpa_s, extra_ie);
448 }
449#endif /* CONFIG_P2P */
450
451#endif /* CONFIG_WPS */
452
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700453#ifdef CONFIG_HS20
454 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
455 wpas_hs20_add_indication(extra_ie);
456#endif /* CONFIG_HS20 */
457
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800458 return extra_ie;
459}
460
461
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800462#ifdef CONFIG_P2P
463
464/*
465 * Check whether there are any enabled networks or credentials that could be
466 * used for a non-P2P connection.
467 */
468static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
469{
470 struct wpa_ssid *ssid;
471
472 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
473 if (wpas_network_disabled(wpa_s, ssid))
474 continue;
475 if (!ssid->p2p_group)
476 return 1;
477 }
478
479 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
480 wpa_s->conf->auto_interworking)
481 return 1;
482
483 return 0;
484}
485
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700486#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800487
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800488
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700489static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
490 u16 num_modes,
491 enum hostapd_hw_mode mode)
492{
493 u16 i;
494
495 for (i = 0; i < num_modes; i++) {
496 if (modes[i].mode == mode)
497 return &modes[i];
498 }
499
500 return NULL;
501}
502
503
504static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
505 enum hostapd_hw_mode band,
506 struct wpa_driver_scan_params *params)
507{
508 /* Include only supported channels for the specified band */
509 struct hostapd_hw_modes *mode;
510 int count, i;
511
512 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
513 if (mode == NULL) {
514 /* No channels supported in this band - use empty list */
515 params->freqs = os_zalloc(sizeof(int));
516 return;
517 }
518
519 params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
520 if (params->freqs == NULL)
521 return;
522 for (count = 0, i = 0; i < mode->num_channels; i++) {
523 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
524 continue;
525 params->freqs[count++] = mode->channels[i].freq;
526 }
527}
528
529
530static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
531 struct wpa_driver_scan_params *params)
532{
533 if (wpa_s->hw.modes == NULL)
534 return; /* unknown what channels the driver supports */
535 if (params->freqs)
536 return; /* already using a limited channel set */
537 if (wpa_s->setband == WPA_SETBAND_5G)
538 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
539 params);
540 else if (wpa_s->setband == WPA_SETBAND_2G)
541 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
542 params);
543}
544
545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
547{
548 struct wpa_supplicant *wpa_s = eloop_ctx;
549 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800550 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
551 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700552 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700554 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 size_t max_ssids;
556 enum wpa_states prev_state;
557
558 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
559 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700560 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 return;
562 }
563
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800564 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700565 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700567 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568 return;
569 }
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700570#ifdef ANDROID
571 if (wpa_s->scanning) {
572 /* If we are already in scanning state, we shall ignore this new scan request*/
573 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
574 return;
575 }
576#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700577 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800578 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
580 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700581 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 return;
583 }
584
585 if (wpa_s->conf->ap_scan != 0 &&
586 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
587 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
588 "overriding ap_scan configuration");
589 wpa_s->conf->ap_scan = 0;
590 wpas_notify_ap_scan_changed(wpa_s);
591 }
592
593 if (wpa_s->conf->ap_scan == 0) {
594 wpa_supplicant_gen_assoc_event(wpa_s);
595 return;
596 }
597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800598#ifdef CONFIG_P2P
Dmitry Shmidt051af732013-10-22 13:52:46 -0700599 if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s, 0)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700600 if (wpa_s->sta_scan_pending &&
601 wpas_p2p_in_progress(wpa_s) == 2 &&
Jouni Malinendc7b7132012-09-14 12:53:47 -0700602 wpa_s->global->p2p_cb_on_scan_complete) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700603 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
604 "mode scan during P2P search");
605 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800606 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
607 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700608 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800609 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700610 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612 }
613#endif /* CONFIG_P2P */
614
Dmitry Shmidt051af732013-10-22 13:52:46 -0700615#ifdef CONFIG_GAS
616 if (gas_query_in_progress(wpa_s->gas)) {
617 wpa_dbg(wpa_s, MSG_DEBUG, "Delay scan while GAS query is in progress");
618 wpa_supplicant_req_scan(wpa_s, 1, 0);
619 return;
620 }
621#endif /* CONFIG_GAS */
622
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800623 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624 max_ssids = 1;
625 else {
626 max_ssids = wpa_s->max_scan_ssids;
627 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
628 max_ssids = WPAS_MAX_SCAN_SSIDS;
629 }
630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800632 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633
634 os_memset(&params, 0, sizeof(params));
635
636 prev_state = wpa_s->wpa_state;
637 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
638 wpa_s->wpa_state == WPA_INACTIVE)
639 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
640
Dmitry Shmidt04949592012-07-19 12:16:46 -0700641 /*
642 * If autoscan has set its own scanning parameters
643 */
644 if (wpa_s->autoscan_params != NULL) {
645 scan_params = wpa_s->autoscan_params;
646 goto scan;
647 }
648
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800649 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700650 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
651 if (ssid == wpa_s->connect_without_scan)
652 break;
653 }
654 wpa_s->connect_without_scan = NULL;
655 if (ssid) {
656 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
657 "without scan step");
658 wpa_supplicant_associate(wpa_s, NULL, ssid);
659 return;
660 }
661 }
662
Dmitry Shmidt04949592012-07-19 12:16:46 -0700663#ifdef CONFIG_P2P
664 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
665 wpa_s->go_params) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800666 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
667 wpa_s->p2p_in_provisioning,
668 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700669 params.ssids[0].ssid = wpa_s->go_params->ssid;
670 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
671 params.num_ssids = 1;
672 goto ssid_list_set;
673 }
674#endif /* CONFIG_P2P */
675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 /* Find the starting point from which to continue scanning */
677 ssid = wpa_s->conf->ssid;
678 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
679 while (ssid) {
680 if (ssid == wpa_s->prev_scan_ssid) {
681 ssid = ssid->next;
682 break;
683 }
684 ssid = ssid->next;
685 }
686 }
687
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800688 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700689 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800690 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 wpa_supplicant_assoc_try(wpa_s, ssid);
692 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700693#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694 } else if (wpa_s->conf->ap_scan == 2) {
695 /*
696 * User-initiated scan request in ap_scan == 2; scan with
697 * wildcard SSID.
698 */
699 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700700#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 } else {
702 struct wpa_ssid *start = ssid, *tssid;
703 int freqs_set = 0;
704 if (ssid == NULL && max_ssids > 1)
705 ssid = wpa_s->conf->ssid;
706 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700707 if (!wpas_network_disabled(wpa_s, ssid) &&
708 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
710 ssid->ssid, ssid->ssid_len);
711 params.ssids[params.num_ssids].ssid =
712 ssid->ssid;
713 params.ssids[params.num_ssids].ssid_len =
714 ssid->ssid_len;
715 params.num_ssids++;
716 if (params.num_ssids + 1 >= max_ssids)
717 break;
718 }
719 ssid = ssid->next;
720 if (ssid == start)
721 break;
722 if (ssid == NULL && max_ssids > 1 &&
723 start != wpa_s->conf->ssid)
724 ssid = wpa_s->conf->ssid;
725 }
726
727 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700728 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 continue;
730 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
731 int_array_concat(&params.freqs,
732 tssid->scan_freq);
733 } else {
734 os_free(params.freqs);
735 params.freqs = NULL;
736 }
737 freqs_set = 1;
738 }
739 int_array_sort_unique(params.freqs);
740 }
741
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800742 if (ssid && max_ssids == 1) {
743 /*
744 * If the driver is limited to 1 SSID at a time interleave
745 * wildcard SSID scans with specific SSID scans to avoid
746 * waiting a long time for a wildcard scan.
747 */
748 if (!wpa_s->prev_scan_wildcard) {
749 params.ssids[0].ssid = NULL;
750 params.ssids[0].ssid_len = 0;
751 wpa_s->prev_scan_wildcard = 1;
752 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
753 "wildcard SSID (Interleave with specific)");
754 } else {
755 wpa_s->prev_scan_ssid = ssid;
756 wpa_s->prev_scan_wildcard = 0;
757 wpa_dbg(wpa_s, MSG_DEBUG,
758 "Starting AP scan for specific SSID: %s",
759 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800761 } else if (ssid) {
762 /* max_ssids > 1 */
763
764 wpa_s->prev_scan_ssid = ssid;
765 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
766 "the scan request");
767 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 } else {
769 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
770 params.num_ssids++;
771 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
772 "SSID");
773 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700774#ifdef CONFIG_P2P
775ssid_list_set:
776#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800778 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700779 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780
781 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
782 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
783 "generated frequency list");
784 params.freqs = wpa_s->next_scan_freqs;
785 } else
786 os_free(wpa_s->next_scan_freqs);
787 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700788 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700790 /* See if user specified frequencies. If so, scan only those. */
791 if (wpa_s->conf->freq_list && !params.freqs) {
792 wpa_dbg(wpa_s, MSG_DEBUG,
793 "Optimize scan based on conf->freq_list");
794 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
795 }
796
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700797 /* Use current associated channel? */
798 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700799 unsigned int num = wpa_s->num_multichan_concurrent;
800
801 params.freqs = os_calloc(num + 1, sizeof(int));
802 if (params.freqs) {
803 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
804 if (num > 0) {
805 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
806 "current operating channels since "
807 "scan_cur_freq is enabled");
808 } else {
809 os_free(params.freqs);
810 params.freqs = NULL;
811 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700812 }
813 }
814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 params.filter_ssids = wpa_supplicant_build_filter_ssids(
816 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800817 if (extra_ie) {
818 params.extra_ies = wpabuf_head(extra_ie);
819 params.extra_ies_len = wpabuf_len(extra_ie);
820 }
821
822#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700823 if (wpa_s->p2p_in_provisioning ||
824 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800825 /*
826 * The interface may not yet be in P2P mode, so we have to
827 * explicitly request P2P probe to disable CCK rates.
828 */
829 params.p2p_probe = 1;
830 }
831#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832
Dmitry Shmidt04949592012-07-19 12:16:46 -0700833 scan_params = &params;
834
835scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800836#ifdef CONFIG_P2P
837 /*
838 * If the driver does not support multi-channel concurrency and a
839 * virtual interface that shares the same radio with the wpa_s interface
840 * is operating there may not be need to scan other channels apart from
841 * the current operating channel on the other virtual interface. Filter
842 * out other channels in case we are trying to find a connection for a
843 * station interface when we are not configured to prefer station
844 * connection and a concurrent operation is already in process.
845 */
846 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
847 !scan_params->freqs && !params.freqs &&
848 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800849 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
850 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700851 unsigned int num = wpa_s->num_multichan_concurrent;
852
853 params.freqs = os_calloc(num + 1, sizeof(int));
854 if (params.freqs) {
855 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
856 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
857 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
858 } else {
859 os_free(params.freqs);
860 params.freqs = NULL;
861 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800862 }
863 }
864#endif /* CONFIG_P2P */
865
Dmitry Shmidt04949592012-07-19 12:16:46 -0700866 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800868 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 os_free(params.freqs);
870 os_free(params.filter_ssids);
871
872 if (ret) {
873 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
874 if (prev_state != wpa_s->wpa_state)
875 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800876 /* Restore scan_req since we will try to scan again */
877 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800879 } else {
880 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 }
882}
883
884
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800885void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
886{
887 struct os_time remaining, new_int;
888 int cancelled;
889
890 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
891 &remaining);
892
893 new_int.sec = sec;
894 new_int.usec = 0;
895 if (cancelled && os_time_before(&remaining, &new_int)) {
896 new_int.sec = remaining.sec;
897 new_int.usec = remaining.usec;
898 }
899
Dmitry Shmidt051af732013-10-22 13:52:46 -0700900 if (cancelled) {
901 eloop_register_timeout(new_int.sec, new_int.usec,
902 wpa_supplicant_scan, wpa_s, NULL);
903 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800904 wpa_s->scan_interval = sec;
905}
906
907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908/**
909 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
910 * @wpa_s: Pointer to wpa_supplicant data
911 * @sec: Number of seconds after which to scan
912 * @usec: Number of microseconds after which to scan
913 *
914 * This function is used to schedule a scan for neighboring access points after
915 * the specified time.
916 */
917void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
918{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800919#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 /* If there's at least one network that should be specifically scanned
921 * then don't cancel the scan and reschedule. Some drivers do
922 * background scanning which generates frequent scan results, and that
923 * causes the specific SSID scan to get continually pushed back and
924 * never happen, which causes hidden APs to never get probe-scanned.
925 */
926 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
927 wpa_s->conf->ap_scan == 1) {
928 struct wpa_ssid *ssid = wpa_s->conf->ssid;
929
930 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700931 if (!wpas_network_disabled(wpa_s, ssid) &&
932 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 break;
934 ssid = ssid->next;
935 }
936 if (ssid) {
937 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
938 "ensure that specific SSID scans occur");
939 return;
940 }
941 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800942#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
944 sec, usec);
945 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
946 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
947}
948
949
950/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800951 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
952 * @wpa_s: Pointer to wpa_supplicant data
953 * @sec: Number of seconds after which to scan
954 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800955 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800956 *
957 * This function is used to schedule periodic scans for neighboring
958 * access points after the specified time.
959 */
960int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
961 int sec, int usec)
962{
963 if (!wpa_s->sched_scan_supported)
964 return -1;
965
966 eloop_register_timeout(sec, usec,
967 wpa_supplicant_delayed_sched_scan_timeout,
968 wpa_s, NULL);
969
970 return 0;
971}
972
973
974/**
975 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
976 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800977 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978 *
979 * This function is used to schedule periodic scans for neighboring
980 * access points repeating the scan continuously.
981 */
982int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
983{
984 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700985 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800986 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700987 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700988 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 int ret;
990 unsigned int max_sched_scan_ssids;
991 int wildcard = 0;
992 int need_ssids;
993
994 if (!wpa_s->sched_scan_supported)
995 return -1;
996
997 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
998 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
999 else
1000 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001001 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 return -1;
1003
1004 if (wpa_s->sched_scanning) {
1005 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1006 return 0;
1007 }
1008
1009 need_ssids = 0;
1010 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001012 /* Use wildcard SSID to find this network */
1013 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001014 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1015 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001016 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001017
1018#ifdef CONFIG_WPS
1019 if (!wpas_network_disabled(wpa_s, ssid) &&
1020 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1021 /*
1022 * Normal scan is more reliable and faster for WPS
1023 * operations and since these are for short periods of
1024 * time, the benefit of trying to use sched_scan would
1025 * be limited.
1026 */
1027 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1028 "sched_scan for WPS");
1029 return -1;
1030 }
1031#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001032 }
1033 if (wildcard)
1034 need_ssids++;
1035
1036 if (wpa_s->normal_scans < 3 &&
1037 (need_ssids <= wpa_s->max_scan_ssids ||
1038 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1039 /*
1040 * When normal scan can speed up operations, use that for the
1041 * first operations before starting the sched_scan to allow
1042 * user space sleep more. We do this only if the normal scan
1043 * has functionality that is suitable for this or if the
1044 * sched_scan does not have better support for multiple SSIDs.
1045 */
1046 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1047 "sched_scan for initial scans (normal_scans=%d)",
1048 wpa_s->normal_scans);
1049 return -1;
1050 }
1051
1052 os_memset(&params, 0, sizeof(params));
1053
1054 /* If we can't allocate space for the filters, we just don't filter */
1055 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1056 sizeof(struct wpa_driver_scan_filter));
1057
1058 prev_state = wpa_s->wpa_state;
1059 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1060 wpa_s->wpa_state == WPA_INACTIVE)
1061 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1062
Dmitry Shmidt04949592012-07-19 12:16:46 -07001063 if (wpa_s->autoscan_params != NULL) {
1064 scan_params = wpa_s->autoscan_params;
1065 goto scan;
1066 }
1067
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001068 /* Find the starting point from which to continue scanning */
1069 ssid = wpa_s->conf->ssid;
1070 if (wpa_s->prev_sched_ssid) {
1071 while (ssid) {
1072 if (ssid == wpa_s->prev_sched_ssid) {
1073 ssid = ssid->next;
1074 break;
1075 }
1076 ssid = ssid->next;
1077 }
1078 }
1079
1080 if (!ssid || !wpa_s->prev_sched_ssid) {
1081 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001082 if (wpa_s->conf->sched_scan_interval)
1083 wpa_s->sched_scan_interval =
1084 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001085 if (wpa_s->sched_scan_interval == 0)
1086 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001087 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1088 wpa_s->first_sched_scan = 1;
1089 ssid = wpa_s->conf->ssid;
1090 wpa_s->prev_sched_ssid = ssid;
1091 }
1092
1093 if (wildcard) {
1094 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1095 params.num_ssids++;
1096 }
1097
1098 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001099 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001100 goto next;
1101
1102 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1103 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1104 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1105 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1106 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1107 ssid->ssid, ssid->ssid_len);
1108 params.filter_ssids[params.num_filter_ssids].ssid_len =
1109 ssid->ssid_len;
1110 params.num_filter_ssids++;
1111 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1112 {
1113 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1114 "filter for sched_scan - drop filter");
1115 os_free(params.filter_ssids);
1116 params.filter_ssids = NULL;
1117 params.num_filter_ssids = 0;
1118 }
1119
1120 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1121 if (params.num_ssids == max_sched_scan_ssids)
1122 break; /* only room for broadcast SSID */
1123 wpa_dbg(wpa_s, MSG_DEBUG,
1124 "add to active scan ssid: %s",
1125 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1126 params.ssids[params.num_ssids].ssid =
1127 ssid->ssid;
1128 params.ssids[params.num_ssids].ssid_len =
1129 ssid->ssid_len;
1130 params.num_ssids++;
1131 if (params.num_ssids >= max_sched_scan_ssids) {
1132 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001133 do {
1134 ssid = ssid->next;
1135 } while (ssid &&
1136 (wpas_network_disabled(wpa_s, ssid) ||
1137 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001138 break;
1139 }
1140 }
1141
1142 next:
1143 wpa_s->prev_sched_ssid = ssid;
1144 ssid = ssid->next;
1145 }
1146
1147 if (params.num_filter_ssids == 0) {
1148 os_free(params.filter_ssids);
1149 params.filter_ssids = NULL;
1150 }
1151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001152 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1153 if (extra_ie) {
1154 params.extra_ies = wpabuf_head(extra_ie);
1155 params.extra_ies_len = wpabuf_len(extra_ie);
1156 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001157
Dmitry Shmidt04949592012-07-19 12:16:46 -07001158 scan_params = &params;
1159
1160scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001161 if (ssid || !wpa_s->first_sched_scan) {
1162 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001163 "Starting sched scan: interval %d timeout %d",
1164 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001165 } else {
1166 wpa_dbg(wpa_s, MSG_DEBUG,
1167 "Starting sched scan: interval %d (no timeout)",
1168 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001169 }
1170
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001171 wpa_setband_scan_freqs(wpa_s, scan_params);
1172
Dmitry Shmidt04949592012-07-19 12:16:46 -07001173 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001174 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001175 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001176 os_free(params.filter_ssids);
1177 if (ret) {
1178 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1179 if (prev_state != wpa_s->wpa_state)
1180 wpa_supplicant_set_state(wpa_s, prev_state);
1181 return ret;
1182 }
1183
1184 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1185 if (ssid || !wpa_s->first_sched_scan) {
1186 wpa_s->sched_scan_timed_out = 0;
1187 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1188 wpa_supplicant_sched_scan_timeout,
1189 wpa_s, NULL);
1190 wpa_s->first_sched_scan = 0;
1191 wpa_s->sched_scan_timeout /= 2;
1192 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001193 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1194 wpa_s->sched_scan_interval = 10;
1195 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1196 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197 }
1198
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001199 /* If there is no more ssids, start next time from the beginning */
1200 if (!ssid)
1201 wpa_s->prev_sched_ssid = NULL;
1202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001203 return 0;
1204}
1205
1206
1207/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1209 * @wpa_s: Pointer to wpa_supplicant data
1210 *
1211 * This function is used to cancel a scan request scheduled with
1212 * wpa_supplicant_req_scan().
1213 */
1214void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1215{
1216 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1217 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001218 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001219#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001220 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001221#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222}
1223
1224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001225/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001226 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1227 * @wpa_s: Pointer to wpa_supplicant data
1228 *
1229 * This function is used to stop a delayed scheduled scan.
1230 */
1231void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1232{
1233 if (!wpa_s->sched_scan_supported)
1234 return;
1235
1236 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1237 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1238 wpa_s, NULL);
1239}
1240
1241
1242/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001243 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1244 * @wpa_s: Pointer to wpa_supplicant data
1245 *
1246 * This function is used to stop a periodic scheduled scan.
1247 */
1248void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1249{
1250 if (!wpa_s->sched_scanning)
1251 return;
1252
1253 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1254 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1255 wpa_supplicant_stop_sched_scan(wpa_s);
1256}
1257
1258
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001259/**
1260 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1261 * @wpa_s: Pointer to wpa_supplicant data
1262 * @scanning: Whether scanning is currently in progress
1263 *
1264 * This function is to generate scanning notifycations. It is called whenever
1265 * there may have been a change in scanning (scan started, completed, stopped).
1266 * wpas_notify_scanning() is called whenever the scanning state changed from the
1267 * previously notified state.
1268 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1270 int scanning)
1271{
1272 if (wpa_s->scanning != scanning) {
1273 wpa_s->scanning = scanning;
1274 wpas_notify_scanning(wpa_s);
1275 }
1276}
1277
1278
1279static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1280{
1281 int rate = 0;
1282 const u8 *ie;
1283 int i;
1284
1285 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1286 for (i = 0; ie && i < ie[1]; i++) {
1287 if ((ie[i + 2] & 0x7f) > rate)
1288 rate = ie[i + 2] & 0x7f;
1289 }
1290
1291 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1292 for (i = 0; ie && i < ie[1]; i++) {
1293 if ((ie[i + 2] & 0x7f) > rate)
1294 rate = ie[i + 2] & 0x7f;
1295 }
1296
1297 return rate;
1298}
1299
1300
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001301/**
1302 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1303 * @res: Scan result entry
1304 * @ie: Information element identitifier (WLAN_EID_*)
1305 * Returns: Pointer to the information element (id field) or %NULL if not found
1306 *
1307 * This function returns the first matching information element in the scan
1308 * result.
1309 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1311{
1312 const u8 *end, *pos;
1313
1314 pos = (const u8 *) (res + 1);
1315 end = pos + res->ie_len;
1316
1317 while (pos + 1 < end) {
1318 if (pos + 2 + pos[1] > end)
1319 break;
1320 if (pos[0] == ie)
1321 return pos;
1322 pos += 2 + pos[1];
1323 }
1324
1325 return NULL;
1326}
1327
1328
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001329/**
1330 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1331 * @res: Scan result entry
1332 * @vendor_type: Vendor type (four octets starting the IE payload)
1333 * Returns: Pointer to the information element (id field) or %NULL if not found
1334 *
1335 * This function returns the first matching information element in the scan
1336 * result.
1337 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1339 u32 vendor_type)
1340{
1341 const u8 *end, *pos;
1342
1343 pos = (const u8 *) (res + 1);
1344 end = pos + res->ie_len;
1345
1346 while (pos + 1 < end) {
1347 if (pos + 2 + pos[1] > end)
1348 break;
1349 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1350 vendor_type == WPA_GET_BE32(&pos[2]))
1351 return pos;
1352 pos += 2 + pos[1];
1353 }
1354
1355 return NULL;
1356}
1357
1358
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001359/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001360 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1361 * @res: Scan result entry
1362 * @vendor_type: Vendor type (four octets starting the IE payload)
1363 * Returns: Pointer to the information element (id field) or %NULL if not found
1364 *
1365 * This function returns the first matching information element in the scan
1366 * result.
1367 *
1368 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1369 * from Beacon frames instead of either Beacon or Probe Response frames.
1370 */
1371const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1372 u32 vendor_type)
1373{
1374 const u8 *end, *pos;
1375
1376 if (res->beacon_ie_len == 0)
1377 return NULL;
1378
1379 pos = (const u8 *) (res + 1);
1380 pos += res->ie_len;
1381 end = pos + res->beacon_ie_len;
1382
1383 while (pos + 1 < end) {
1384 if (pos + 2 + pos[1] > end)
1385 break;
1386 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1387 vendor_type == WPA_GET_BE32(&pos[2]))
1388 return pos;
1389 pos += 2 + pos[1];
1390 }
1391
1392 return NULL;
1393}
1394
1395
1396/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001397 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1398 * @res: Scan result entry
1399 * @vendor_type: Vendor type (four octets starting the IE payload)
1400 * Returns: Pointer to the information element payload or %NULL if not found
1401 *
1402 * This function returns concatenated payload of possibly fragmented vendor
1403 * specific information elements in the scan result. The caller is responsible
1404 * for freeing the returned buffer.
1405 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1407 u32 vendor_type)
1408{
1409 struct wpabuf *buf;
1410 const u8 *end, *pos;
1411
1412 buf = wpabuf_alloc(res->ie_len);
1413 if (buf == NULL)
1414 return NULL;
1415
1416 pos = (const u8 *) (res + 1);
1417 end = pos + res->ie_len;
1418
1419 while (pos + 1 < end) {
1420 if (pos + 2 + pos[1] > end)
1421 break;
1422 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1423 vendor_type == WPA_GET_BE32(&pos[2]))
1424 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1425 pos += 2 + pos[1];
1426 }
1427
1428 if (wpabuf_len(buf) == 0) {
1429 wpabuf_free(buf);
1430 buf = NULL;
1431 }
1432
1433 return buf;
1434}
1435
1436
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001437/*
1438 * Channels with a great SNR can operate at full rate. What is a great SNR?
1439 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1440 * rule of thumb is that any SNR above 20 is good." This one
1441 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1442 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1443 * conservative value.
1444 */
1445#define GREAT_SNR 30
1446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447/* Compare function for sorting scan results. Return >0 if @b is considered
1448 * better. */
1449static int wpa_scan_result_compar(const void *a, const void *b)
1450{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001451#define IS_5GHZ(n) (n > 4000)
1452#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 struct wpa_scan_res **_wa = (void *) a;
1454 struct wpa_scan_res **_wb = (void *) b;
1455 struct wpa_scan_res *wa = *_wa;
1456 struct wpa_scan_res *wb = *_wb;
1457 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001458 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459
1460 /* WPA/WPA2 support preferred */
1461 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1462 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1463 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1464 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1465
1466 if (wpa_b && !wpa_a)
1467 return 1;
1468 if (!wpa_b && wpa_a)
1469 return -1;
1470
1471 /* privacy support preferred */
1472 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1473 (wb->caps & IEEE80211_CAP_PRIVACY))
1474 return 1;
1475 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1476 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1477 return -1;
1478
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1480 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1481 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1482 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1483 } else {
1484 /* Not suitable information to calculate SNR, so use level */
1485 snr_a = wa->level;
1486 snr_b = wb->level;
1487 }
1488
1489 /* best/max rate preferred if SNR close enough */
1490 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1492 maxrate_a = wpa_scan_get_max_rate(wa);
1493 maxrate_b = wpa_scan_get_max_rate(wb);
1494 if (maxrate_a != maxrate_b)
1495 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001496 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1497 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 }
1499
1500 /* use freq for channel preference */
1501
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001502 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 * identical, use quality values since some drivers may only report
1504 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001505 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001507 return snr_b - snr_a;
1508#undef MIN
1509#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510}
1511
1512
1513#ifdef CONFIG_WPS
1514/* Compare function for sorting scan results when searching a WPS AP for
1515 * provisioning. Return >0 if @b is considered better. */
1516static int wpa_scan_result_wps_compar(const void *a, const void *b)
1517{
1518 struct wpa_scan_res **_wa = (void *) a;
1519 struct wpa_scan_res **_wb = (void *) b;
1520 struct wpa_scan_res *wa = *_wa;
1521 struct wpa_scan_res *wb = *_wb;
1522 int uses_wps_a, uses_wps_b;
1523 struct wpabuf *wps_a, *wps_b;
1524 int res;
1525
1526 /* Optimization - check WPS IE existence before allocated memory and
1527 * doing full reassembly. */
1528 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1529 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1530 if (uses_wps_a && !uses_wps_b)
1531 return -1;
1532 if (!uses_wps_a && uses_wps_b)
1533 return 1;
1534
1535 if (uses_wps_a && uses_wps_b) {
1536 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1537 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1538 res = wps_ap_priority_compar(wps_a, wps_b);
1539 wpabuf_free(wps_a);
1540 wpabuf_free(wps_b);
1541 if (res)
1542 return res;
1543 }
1544
1545 /*
1546 * Do not use current AP security policy as a sorting criteria during
1547 * WPS provisioning step since the AP may get reconfigured at the
1548 * completion of provisioning.
1549 */
1550
1551 /* all things being equal, use signal level; if signal levels are
1552 * identical, use quality values since some drivers may only report
1553 * that value and leave the signal level zero */
1554 if (wb->level == wa->level)
1555 return wb->qual - wa->qual;
1556 return wb->level - wa->level;
1557}
1558#endif /* CONFIG_WPS */
1559
1560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561static void dump_scan_res(struct wpa_scan_results *scan_res)
1562{
1563#ifndef CONFIG_NO_STDOUT_DEBUG
1564 size_t i;
1565
1566 if (scan_res->res == NULL || scan_res->num == 0)
1567 return;
1568
1569 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1570
1571 for (i = 0; i < scan_res->num; i++) {
1572 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001573 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001574 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1575 == WPA_SCAN_LEVEL_DBM) {
1576 int snr = r->level - r->noise;
1577 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001578 "noise=%d level=%d snr=%d%s flags=0x%x "
1579 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001580 MAC2STR(r->bssid), r->freq, r->qual,
1581 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001582 snr >= GREAT_SNR ? "*" : "", r->flags,
1583 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 } else {
1585 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001586 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001587 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001588 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001589 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001590 pos = (u8 *) (r + 1);
1591 if (r->ie_len)
1592 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1593 pos += r->ie_len;
1594 if (r->beacon_ie_len)
1595 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1596 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001597 }
1598#endif /* CONFIG_NO_STDOUT_DEBUG */
1599}
1600
1601
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001602/**
1603 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1604 * @wpa_s: Pointer to wpa_supplicant data
1605 * @bssid: BSSID to check
1606 * Returns: 0 if the BSSID is filtered or 1 if not
1607 *
1608 * This function is used to filter out specific BSSIDs from scan reslts mainly
1609 * for testing purposes (SET bssid_filter ctrl_iface command).
1610 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001611int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1612 const u8 *bssid)
1613{
1614 size_t i;
1615
1616 if (wpa_s->bssid_filter == NULL)
1617 return 1;
1618
1619 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1620 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1621 ETH_ALEN) == 0)
1622 return 1;
1623 }
1624
1625 return 0;
1626}
1627
1628
1629static void filter_scan_res(struct wpa_supplicant *wpa_s,
1630 struct wpa_scan_results *res)
1631{
1632 size_t i, j;
1633
1634 if (wpa_s->bssid_filter == NULL)
1635 return;
1636
1637 for (i = 0, j = 0; i < res->num; i++) {
1638 if (wpa_supplicant_filter_bssid_match(wpa_s,
1639 res->res[i]->bssid)) {
1640 res->res[j++] = res->res[i];
1641 } else {
1642 os_free(res->res[i]);
1643 res->res[i] = NULL;
1644 }
1645 }
1646
1647 if (res->num != j) {
1648 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1649 (int) (res->num - j));
1650 res->num = j;
1651 }
1652}
1653
1654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655/**
1656 * wpa_supplicant_get_scan_results - Get scan results
1657 * @wpa_s: Pointer to wpa_supplicant data
1658 * @info: Information about what was scanned or %NULL if not available
1659 * @new_scan: Whether a new scan was performed
1660 * Returns: Scan results, %NULL on failure
1661 *
1662 * This function request the current scan results from the driver and updates
1663 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1664 * results with wpa_scan_results_free().
1665 */
1666struct wpa_scan_results *
1667wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1668 struct scan_info *info, int new_scan)
1669{
1670 struct wpa_scan_results *scan_res;
1671 size_t i;
1672 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1673
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001674 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 if (scan_res == NULL) {
1676 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1677 return NULL;
1678 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001679 if (scan_res->fetch_time.sec == 0) {
1680 /*
1681 * Make sure we have a valid timestamp if the driver wrapper
1682 * does not set this.
1683 */
1684 os_get_time(&scan_res->fetch_time);
1685 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001686 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687
1688#ifdef CONFIG_WPS
1689 if (wpas_wps_in_progress(wpa_s)) {
1690 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1691 "provisioning rules");
1692 compar = wpa_scan_result_wps_compar;
1693 }
1694#endif /* CONFIG_WPS */
1695
1696 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1697 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001698 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699
1700 wpa_bss_update_start(wpa_s);
1701 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001702 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1703 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 wpa_bss_update_end(wpa_s, info, new_scan);
1705
1706 return scan_res;
1707}
1708
1709
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001710/**
1711 * wpa_supplicant_update_scan_results - Update scan results from the driver
1712 * @wpa_s: Pointer to wpa_supplicant data
1713 * Returns: 0 on success, -1 on failure
1714 *
1715 * This function updates the BSS table within wpa_supplicant based on the
1716 * currently available scan results from the driver without requesting a new
1717 * scan. This is used in cases where the driver indicates an association
1718 * (including roaming within ESS) and wpa_supplicant does not yet have the
1719 * needed information to complete the connection (e.g., to perform validation
1720 * steps in 4-way handshake).
1721 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1723{
1724 struct wpa_scan_results *scan_res;
1725 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1726 if (scan_res == NULL)
1727 return -1;
1728 wpa_scan_results_free(scan_res);
1729
1730 return 0;
1731}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001732
1733
1734/**
1735 * scan_only_handler - Reports scan results
1736 */
1737void scan_only_handler(struct wpa_supplicant *wpa_s,
1738 struct wpa_scan_results *scan_res)
1739{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001740 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001741 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1742 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001743 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001744}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001745
1746
1747int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1748{
1749 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1750}