blob: 9b6cd4662f6f406ce2bf66c0d54ccbb702a21828 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt3a787e62013-01-17 10:32:35 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "config.h"
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "wps_supplicant.h"
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "notify.h"
23#include "bss.h"
24#include "scan.h"
25
26
27static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
28{
29 struct wpa_ssid *ssid;
30 union wpa_event_data data;
31
32 ssid = wpa_supplicant_get_ssid(wpa_s);
33 if (ssid == NULL)
34 return;
35
36 if (wpa_s->current_ssid == NULL) {
37 wpa_s->current_ssid = ssid;
38 if (wpa_s->current_ssid != NULL)
39 wpas_notify_network_changed(wpa_s);
40 }
41 wpa_supplicant_initiate_eapol(wpa_s);
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
49#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 enum wps_request_type *req_type)
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
55
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
61 *req_type = wpas_wps_get_req_type(ssid);
62 if (!ssid->eap.phase1)
63 continue;
64
65 if (os_strstr(ssid->eap.phase1, "pbc=1"))
66 return 2;
67 }
68
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080069#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080070 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
71 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080072 wpa_s->wps->dev.p2p = 1;
73 if (!wps) {
74 wps = 1;
75 *req_type = WPS_REQ_ENROLLEE_INFO;
76 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080077 }
78#endif /* CONFIG_P2P */
79
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 return wps;
81}
82#endif /* CONFIG_WPS */
83
84
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080085/**
86 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
87 * @wpa_s: Pointer to wpa_supplicant data
88 * Returns: 0 if no networks are enabled, >0 if networks are enabled
89 *
90 * This function is used to figure out whether any networks (or Interworking
91 * with enabled credentials and auto_interworking) are present in the current
92 * configuration.
93 */
Dmitry Shmidt04949592012-07-19 12:16:46 -070094int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095{
Dmitry Shmidt04949592012-07-19 12:16:46 -070096 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070097 int count = 0, disabled = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070099 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700101 else
102 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103 ssid = ssid->next;
104 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700105 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
106 wpa_s->conf->auto_interworking)
107 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700108 if (count == 0 && disabled > 0) {
109 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
110 "networks)", disabled);
111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 return count;
113}
114
115
116static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
117 struct wpa_ssid *ssid)
118{
119 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 break;
122 ssid = ssid->next;
123 }
124
125 /* ap_scan=2 mode - try to associate with each SSID. */
126 if (ssid == NULL) {
127 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
128 "end of scan list - go back to beginning");
129 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
130 wpa_supplicant_req_scan(wpa_s, 0, 0);
131 return;
132 }
133 if (ssid->next) {
134 /* Continue from the next SSID on the next attempt. */
135 wpa_s->prev_scan_ssid = ssid;
136 } else {
137 /* Start from the beginning of the SSID list. */
138 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
139 }
140 wpa_supplicant_associate(wpa_s, NULL, ssid);
141}
142
143
144static int int_array_len(const int *a)
145{
146 int i;
147 for (i = 0; a && a[i]; i++)
148 ;
149 return i;
150}
151
152
153static void int_array_concat(int **res, const int *a)
154{
155 int reslen, alen, i;
156 int *n;
157
158 reslen = int_array_len(*res);
159 alen = int_array_len(a);
160
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700161 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 if (n == NULL) {
163 os_free(*res);
164 *res = NULL;
165 return;
166 }
167 for (i = 0; i <= alen; i++)
168 n[reslen + i] = a[i];
169 *res = n;
170}
171
172
173static int freq_cmp(const void *a, const void *b)
174{
175 int _a = *(int *) a;
176 int _b = *(int *) b;
177
178 if (_a == 0)
179 return 1;
180 if (_b == 0)
181 return -1;
182 return _a - _b;
183}
184
185
186static void int_array_sort_unique(int *a)
187{
188 int alen;
189 int i, j;
190
191 if (a == NULL)
192 return;
193
194 alen = int_array_len(a);
195 qsort(a, alen, sizeof(int), freq_cmp);
196
197 i = 0;
198 j = 1;
199 while (a[i] && a[j]) {
200 if (a[i] == a[j]) {
201 j++;
202 continue;
203 }
204 a[++i] = a[j++];
205 }
206 if (a[i])
207 i++;
208 a[i] = 0;
209}
210
211
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800212/**
213 * wpa_supplicant_trigger_scan - Request driver to start a scan
214 * @wpa_s: Pointer to wpa_supplicant data
215 * @params: Scan parameters
216 * Returns: 0 on success, -1 on failure
217 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
219 struct wpa_driver_scan_params *params)
220{
221 int ret;
222
223 wpa_supplicant_notify_scanning(wpa_s, 1);
224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800225 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 if (ret) {
227 wpa_supplicant_notify_scanning(wpa_s, 0);
228 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800229 } else {
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700230 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 wpa_s->scan_runs++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800232 wpa_s->normal_scans++;
233 }
234
235 return ret;
236}
237
238
239static void
240wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
241{
242 struct wpa_supplicant *wpa_s = eloop_ctx;
243
244 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
245
246 if (wpa_supplicant_req_sched_scan(wpa_s))
247 wpa_supplicant_req_scan(wpa_s, 0, 0);
248}
249
250
251static void
252wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
253{
254 struct wpa_supplicant *wpa_s = eloop_ctx;
255
256 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
257
258 wpa_s->sched_scan_timed_out = 1;
259 wpa_supplicant_cancel_sched_scan(wpa_s);
260}
261
262
263static int
264wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
265 struct wpa_driver_scan_params *params,
266 int interval)
267{
268 int ret;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700269#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800270 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700271#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800272 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
273 if (ret)
274 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700275 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800276 wpa_s->sched_scanning = 1;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700277#ifdef ANDROID_P2P
278 wpa_supplicant_notify_scanning(wpa_s, 1);
279#endif
280 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800281
282 return ret;
283}
284
285
286static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
287{
288 int ret;
289
290 ret = wpa_drv_stop_sched_scan(wpa_s);
291 if (ret) {
292 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
293 /* TODO: what to do if stopping fails? */
294 return -1;
295 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296
297 return ret;
298}
299
300
301static struct wpa_driver_scan_filter *
302wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
303{
304 struct wpa_driver_scan_filter *ssids;
305 struct wpa_ssid *ssid;
306 size_t count;
307
308 *num_ssids = 0;
309 if (!conf->filter_ssids)
310 return NULL;
311
312 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
313 if (ssid->ssid && ssid->ssid_len)
314 count++;
315 }
316 if (count == 0)
317 return NULL;
318 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
319 if (ssids == NULL)
320 return NULL;
321
322 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
323 if (!ssid->ssid || !ssid->ssid_len)
324 continue;
325 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
326 ssids[*num_ssids].ssid_len = ssid->ssid_len;
327 (*num_ssids)++;
328 }
329
330 return ssids;
331}
332
333
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334static void wpa_supplicant_optimize_freqs(
335 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
336{
337#ifdef CONFIG_P2P
338 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
339 wpa_s->go_params) {
340 /* Optimize provisioning state scan based on GO information */
341 if (wpa_s->p2p_in_provisioning < 5 &&
342 wpa_s->go_params->freq > 0) {
343 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
344 "preferred frequency %d MHz",
345 wpa_s->go_params->freq);
346 params->freqs = os_zalloc(2 * sizeof(int));
347 if (params->freqs)
348 params->freqs[0] = wpa_s->go_params->freq;
349 } else if (wpa_s->p2p_in_provisioning < 8 &&
350 wpa_s->go_params->freq_list[0]) {
351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
352 "channels");
353 int_array_concat(&params->freqs,
354 wpa_s->go_params->freq_list);
355 if (params->freqs)
356 int_array_sort_unique(params->freqs);
357 }
358 wpa_s->p2p_in_provisioning++;
359 }
360#endif /* CONFIG_P2P */
361
362#ifdef CONFIG_WPS
363 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
364 /*
365 * Optimize post-provisioning scan based on channel used
366 * during provisioning.
367 */
368 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
369 "that was used during provisioning", wpa_s->wps_freq);
370 params->freqs = os_zalloc(2 * sizeof(int));
371 if (params->freqs)
372 params->freqs[0] = wpa_s->wps_freq;
373 wpa_s->after_wps--;
374 }
375
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800376 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
377 {
378 /* Optimize provisioning scan based on already known channel */
379 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
380 wpa_s->wps_freq);
381 params->freqs = os_zalloc(2 * sizeof(int));
382 if (params->freqs)
383 params->freqs[0] = wpa_s->wps_freq;
384 wpa_s->known_wps_freq = 0; /* only do this once */
385 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800386#endif /* CONFIG_WPS */
387}
388
389
390#ifdef CONFIG_INTERWORKING
391static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
392 struct wpabuf *buf)
393{
394 if (wpa_s->conf->interworking == 0)
395 return;
396
397 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
398 wpabuf_put_u8(buf, 4);
399 wpabuf_put_u8(buf, 0x00);
400 wpabuf_put_u8(buf, 0x00);
401 wpabuf_put_u8(buf, 0x00);
402 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
403
404 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
405 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
406 1 + ETH_ALEN);
407 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
408 /* No Venue Info */
409 if (!is_zero_ether_addr(wpa_s->conf->hessid))
410 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
411}
412#endif /* CONFIG_INTERWORKING */
413
414
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700415static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800416{
417 struct wpabuf *extra_ie = NULL;
418#ifdef CONFIG_WPS
419 int wps = 0;
420 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
421#endif /* CONFIG_WPS */
422
423#ifdef CONFIG_INTERWORKING
424 if (wpa_s->conf->interworking &&
425 wpabuf_resize(&extra_ie, 100) == 0)
426 wpas_add_interworking_elements(wpa_s, extra_ie);
427#endif /* CONFIG_INTERWORKING */
428
429#ifdef CONFIG_WPS
430 wps = wpas_wps_in_use(wpa_s, &req_type);
431
432 if (wps) {
433 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700434 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
435 DEV_PW_DEFAULT,
436 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800437 wpa_s->wps->uuid, req_type,
438 0, NULL);
439 if (wps_ie) {
440 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
441 wpabuf_put_buf(extra_ie, wps_ie);
442 wpabuf_free(wps_ie);
443 }
444 }
445
446#ifdef CONFIG_P2P
447 if (wps) {
448 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
449 if (wpabuf_resize(&extra_ie, ielen) == 0)
450 wpas_p2p_scan_ie(wpa_s, extra_ie);
451 }
452#endif /* CONFIG_P2P */
453
454#endif /* CONFIG_WPS */
455
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700456#ifdef CONFIG_HS20
457 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
458 wpas_hs20_add_indication(extra_ie);
459#endif /* CONFIG_HS20 */
460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800461 return extra_ie;
462}
463
464
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800465#ifdef CONFIG_P2P
466
467/*
468 * Check whether there are any enabled networks or credentials that could be
469 * used for a non-P2P connection.
470 */
471static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
472{
473 struct wpa_ssid *ssid;
474
475 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
476 if (wpas_network_disabled(wpa_s, ssid))
477 continue;
478 if (!ssid->p2p_group)
479 return 1;
480 }
481
482 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
483 wpa_s->conf->auto_interworking)
484 return 1;
485
486 return 0;
487}
488
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700489#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800490
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800491
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700492static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
493 u16 num_modes,
494 enum hostapd_hw_mode mode)
495{
496 u16 i;
497
498 for (i = 0; i < num_modes; i++) {
499 if (modes[i].mode == mode)
500 return &modes[i];
501 }
502
503 return NULL;
504}
505
506
507static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
508 enum hostapd_hw_mode band,
509 struct wpa_driver_scan_params *params)
510{
511 /* Include only supported channels for the specified band */
512 struct hostapd_hw_modes *mode;
513 int count, i;
514
515 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
516 if (mode == NULL) {
517 /* No channels supported in this band - use empty list */
518 params->freqs = os_zalloc(sizeof(int));
519 return;
520 }
521
522 params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
523 if (params->freqs == NULL)
524 return;
525 for (count = 0, i = 0; i < mode->num_channels; i++) {
526 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
527 continue;
528 params->freqs[count++] = mode->channels[i].freq;
529 }
530}
531
532
533static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
534 struct wpa_driver_scan_params *params)
535{
536 if (wpa_s->hw.modes == NULL)
537 return; /* unknown what channels the driver supports */
538 if (params->freqs)
539 return; /* already using a limited channel set */
540 if (wpa_s->setband == WPA_SETBAND_5G)
541 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
542 params);
543 else if (wpa_s->setband == WPA_SETBAND_2G)
544 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
545 params);
546}
547
548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
550{
551 struct wpa_supplicant *wpa_s = eloop_ctx;
552 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800553 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
554 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700555 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700557 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700558 size_t max_ssids;
559 enum wpa_states prev_state;
560
561 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
562 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700563 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 return;
565 }
566
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800567 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700568 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700570 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571 return;
572 }
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700573#ifdef ANDROID
574 if (wpa_s->scanning) {
575 /* If we are already in scanning state, we shall ignore this new scan request*/
576 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
577 return;
578 }
579#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700580 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800581 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
583 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700584 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 return;
586 }
587
588 if (wpa_s->conf->ap_scan != 0 &&
589 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
590 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
591 "overriding ap_scan configuration");
592 wpa_s->conf->ap_scan = 0;
593 wpas_notify_ap_scan_changed(wpa_s);
594 }
595
596 if (wpa_s->conf->ap_scan == 0) {
597 wpa_supplicant_gen_assoc_event(wpa_s);
598 return;
599 }
600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800601#ifdef CONFIG_P2P
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700602 if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700603 if (wpa_s->sta_scan_pending &&
604 wpas_p2p_in_progress(wpa_s) == 2 &&
Jouni Malinendc7b7132012-09-14 12:53:47 -0700605 wpa_s->global->p2p_cb_on_scan_complete) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700606 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
607 "mode scan during P2P search");
608 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800609 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
610 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700611 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700613 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800615 }
616#endif /* CONFIG_P2P */
617
618 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619 max_ssids = 1;
620 else {
621 max_ssids = wpa_s->max_scan_ssids;
622 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
623 max_ssids = WPAS_MAX_SCAN_SSIDS;
624 }
625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800627 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628
629 os_memset(&params, 0, sizeof(params));
630
631 prev_state = wpa_s->wpa_state;
632 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
633 wpa_s->wpa_state == WPA_INACTIVE)
634 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
635
Dmitry Shmidt04949592012-07-19 12:16:46 -0700636 /*
637 * If autoscan has set its own scanning parameters
638 */
639 if (wpa_s->autoscan_params != NULL) {
640 scan_params = wpa_s->autoscan_params;
641 goto scan;
642 }
643
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800644 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700645 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
646 if (ssid == wpa_s->connect_without_scan)
647 break;
648 }
649 wpa_s->connect_without_scan = NULL;
650 if (ssid) {
651 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
652 "without scan step");
653 wpa_supplicant_associate(wpa_s, NULL, ssid);
654 return;
655 }
656 }
657
Dmitry Shmidt04949592012-07-19 12:16:46 -0700658#ifdef CONFIG_P2P
659 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
660 wpa_s->go_params) {
661 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
662 "P2P group formation");
663 params.ssids[0].ssid = wpa_s->go_params->ssid;
664 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
665 params.num_ssids = 1;
666 goto ssid_list_set;
667 }
668#endif /* CONFIG_P2P */
669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 /* Find the starting point from which to continue scanning */
671 ssid = wpa_s->conf->ssid;
672 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
673 while (ssid) {
674 if (ssid == wpa_s->prev_scan_ssid) {
675 ssid = ssid->next;
676 break;
677 }
678 ssid = ssid->next;
679 }
680 }
681
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800682 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700683 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800684 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 wpa_supplicant_assoc_try(wpa_s, ssid);
686 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700687#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688 } else if (wpa_s->conf->ap_scan == 2) {
689 /*
690 * User-initiated scan request in ap_scan == 2; scan with
691 * wildcard SSID.
692 */
693 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700694#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 } else {
696 struct wpa_ssid *start = ssid, *tssid;
697 int freqs_set = 0;
698 if (ssid == NULL && max_ssids > 1)
699 ssid = wpa_s->conf->ssid;
700 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700701 if (!wpas_network_disabled(wpa_s, ssid) &&
702 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
704 ssid->ssid, ssid->ssid_len);
705 params.ssids[params.num_ssids].ssid =
706 ssid->ssid;
707 params.ssids[params.num_ssids].ssid_len =
708 ssid->ssid_len;
709 params.num_ssids++;
710 if (params.num_ssids + 1 >= max_ssids)
711 break;
712 }
713 ssid = ssid->next;
714 if (ssid == start)
715 break;
716 if (ssid == NULL && max_ssids > 1 &&
717 start != wpa_s->conf->ssid)
718 ssid = wpa_s->conf->ssid;
719 }
720
721 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700722 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 continue;
724 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
725 int_array_concat(&params.freqs,
726 tssid->scan_freq);
727 } else {
728 os_free(params.freqs);
729 params.freqs = NULL;
730 }
731 freqs_set = 1;
732 }
733 int_array_sort_unique(params.freqs);
734 }
735
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800736 if (ssid && max_ssids == 1) {
737 /*
738 * If the driver is limited to 1 SSID at a time interleave
739 * wildcard SSID scans with specific SSID scans to avoid
740 * waiting a long time for a wildcard scan.
741 */
742 if (!wpa_s->prev_scan_wildcard) {
743 params.ssids[0].ssid = NULL;
744 params.ssids[0].ssid_len = 0;
745 wpa_s->prev_scan_wildcard = 1;
746 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
747 "wildcard SSID (Interleave with specific)");
748 } else {
749 wpa_s->prev_scan_ssid = ssid;
750 wpa_s->prev_scan_wildcard = 0;
751 wpa_dbg(wpa_s, MSG_DEBUG,
752 "Starting AP scan for specific SSID: %s",
753 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800755 } else if (ssid) {
756 /* max_ssids > 1 */
757
758 wpa_s->prev_scan_ssid = ssid;
759 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
760 "the scan request");
761 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 } else {
763 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
764 params.num_ssids++;
765 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
766 "SSID");
767 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700768#ifdef CONFIG_P2P
769ssid_list_set:
770#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800772 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700773 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774
775 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
776 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
777 "generated frequency list");
778 params.freqs = wpa_s->next_scan_freqs;
779 } else
780 os_free(wpa_s->next_scan_freqs);
781 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700782 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700784 /* See if user specified frequencies. If so, scan only those. */
785 if (wpa_s->conf->freq_list && !params.freqs) {
786 wpa_dbg(wpa_s, MSG_DEBUG,
787 "Optimize scan based on conf->freq_list");
788 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
789 }
790
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700791 /* Use current associated channel? */
792 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700793 unsigned int num = wpa_s->num_multichan_concurrent;
794
795 params.freqs = os_calloc(num + 1, sizeof(int));
796 if (params.freqs) {
797 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
798 if (num > 0) {
799 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
800 "current operating channels since "
801 "scan_cur_freq is enabled");
802 } else {
803 os_free(params.freqs);
804 params.freqs = NULL;
805 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700806 }
807 }
808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 params.filter_ssids = wpa_supplicant_build_filter_ssids(
810 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800811 if (extra_ie) {
812 params.extra_ies = wpabuf_head(extra_ie);
813 params.extra_ies_len = wpabuf_len(extra_ie);
814 }
815
816#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700817 if (wpa_s->p2p_in_provisioning ||
818 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800819 /*
820 * The interface may not yet be in P2P mode, so we have to
821 * explicitly request P2P probe to disable CCK rates.
822 */
823 params.p2p_probe = 1;
824 }
825#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826
Dmitry Shmidt04949592012-07-19 12:16:46 -0700827 scan_params = &params;
828
829scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800830#ifdef CONFIG_P2P
831 /*
832 * If the driver does not support multi-channel concurrency and a
833 * virtual interface that shares the same radio with the wpa_s interface
834 * is operating there may not be need to scan other channels apart from
835 * the current operating channel on the other virtual interface. Filter
836 * out other channels in case we are trying to find a connection for a
837 * station interface when we are not configured to prefer station
838 * connection and a concurrent operation is already in process.
839 */
840 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
841 !scan_params->freqs && !params.freqs &&
842 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800843 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
844 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700845 unsigned int num = wpa_s->num_multichan_concurrent;
846
847 params.freqs = os_calloc(num + 1, sizeof(int));
848 if (params.freqs) {
849 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
850 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
851 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
852 } else {
853 os_free(params.freqs);
854 params.freqs = NULL;
855 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800856 }
857 }
858#endif /* CONFIG_P2P */
859
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 os_free(params.freqs);
864 os_free(params.filter_ssids);
865
866 if (ret) {
867 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
868 if (prev_state != wpa_s->wpa_state)
869 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800870 /* Restore scan_req since we will try to scan again */
871 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800873 } else {
874 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 }
876}
877
878
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800879void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
880{
881 struct os_time remaining, new_int;
882 int cancelled;
883
884 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
885 &remaining);
886
887 new_int.sec = sec;
888 new_int.usec = 0;
889 if (cancelled && os_time_before(&remaining, &new_int)) {
890 new_int.sec = remaining.sec;
891 new_int.usec = remaining.usec;
892 }
893
894 eloop_register_timeout(new_int.sec, new_int.usec, wpa_supplicant_scan,
895 wpa_s, NULL);
896 wpa_s->scan_interval = sec;
897}
898
899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900/**
901 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
902 * @wpa_s: Pointer to wpa_supplicant data
903 * @sec: Number of seconds after which to scan
904 * @usec: Number of microseconds after which to scan
905 *
906 * This function is used to schedule a scan for neighboring access points after
907 * the specified time.
908 */
909void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
910{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800911#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 /* If there's at least one network that should be specifically scanned
913 * then don't cancel the scan and reschedule. Some drivers do
914 * background scanning which generates frequent scan results, and that
915 * causes the specific SSID scan to get continually pushed back and
916 * never happen, which causes hidden APs to never get probe-scanned.
917 */
918 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
919 wpa_s->conf->ap_scan == 1) {
920 struct wpa_ssid *ssid = wpa_s->conf->ssid;
921
922 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700923 if (!wpas_network_disabled(wpa_s, ssid) &&
924 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 break;
926 ssid = ssid->next;
927 }
928 if (ssid) {
929 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
930 "ensure that specific SSID scans occur");
931 return;
932 }
933 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800934#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
936 sec, usec);
937 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
938 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
939}
940
941
942/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800943 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
944 * @wpa_s: Pointer to wpa_supplicant data
945 * @sec: Number of seconds after which to scan
946 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800947 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 *
949 * This function is used to schedule periodic scans for neighboring
950 * access points after the specified time.
951 */
952int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
953 int sec, int usec)
954{
955 if (!wpa_s->sched_scan_supported)
956 return -1;
957
958 eloop_register_timeout(sec, usec,
959 wpa_supplicant_delayed_sched_scan_timeout,
960 wpa_s, NULL);
961
962 return 0;
963}
964
965
966/**
967 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
968 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800969 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800970 *
971 * This function is used to schedule periodic scans for neighboring
972 * access points repeating the scan continuously.
973 */
974int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
975{
976 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700977 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700979 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800981 int ret;
982 unsigned int max_sched_scan_ssids;
983 int wildcard = 0;
984 int need_ssids;
985
986 if (!wpa_s->sched_scan_supported)
987 return -1;
988
989 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
990 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
991 else
992 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700993 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800994 return -1;
995
996 if (wpa_s->sched_scanning) {
997 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
998 return 0;
999 }
1000
1001 need_ssids = 0;
1002 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001003 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001004 /* Use wildcard SSID to find this network */
1005 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001006 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1007 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001008 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001009
1010#ifdef CONFIG_WPS
1011 if (!wpas_network_disabled(wpa_s, ssid) &&
1012 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1013 /*
1014 * Normal scan is more reliable and faster for WPS
1015 * operations and since these are for short periods of
1016 * time, the benefit of trying to use sched_scan would
1017 * be limited.
1018 */
1019 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1020 "sched_scan for WPS");
1021 return -1;
1022 }
1023#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024 }
1025 if (wildcard)
1026 need_ssids++;
1027
1028 if (wpa_s->normal_scans < 3 &&
1029 (need_ssids <= wpa_s->max_scan_ssids ||
1030 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1031 /*
1032 * When normal scan can speed up operations, use that for the
1033 * first operations before starting the sched_scan to allow
1034 * user space sleep more. We do this only if the normal scan
1035 * has functionality that is suitable for this or if the
1036 * sched_scan does not have better support for multiple SSIDs.
1037 */
1038 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1039 "sched_scan for initial scans (normal_scans=%d)",
1040 wpa_s->normal_scans);
1041 return -1;
1042 }
1043
1044 os_memset(&params, 0, sizeof(params));
1045
1046 /* If we can't allocate space for the filters, we just don't filter */
1047 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1048 sizeof(struct wpa_driver_scan_filter));
1049
1050 prev_state = wpa_s->wpa_state;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001051#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001052 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1053 wpa_s->wpa_state == WPA_INACTIVE)
1054 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001055#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001056
Dmitry Shmidt04949592012-07-19 12:16:46 -07001057 if (wpa_s->autoscan_params != NULL) {
1058 scan_params = wpa_s->autoscan_params;
1059 goto scan;
1060 }
1061
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001062 /* Find the starting point from which to continue scanning */
1063 ssid = wpa_s->conf->ssid;
1064 if (wpa_s->prev_sched_ssid) {
1065 while (ssid) {
1066 if (ssid == wpa_s->prev_sched_ssid) {
1067 ssid = ssid->next;
1068 break;
1069 }
1070 ssid = ssid->next;
1071 }
1072 }
1073
1074 if (!ssid || !wpa_s->prev_sched_ssid) {
1075 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001076 if (wpa_s->conf->sched_scan_interval)
1077 wpa_s->sched_scan_interval =
1078 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001079 if (wpa_s->sched_scan_interval == 0)
1080 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001081 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1082 wpa_s->first_sched_scan = 1;
1083 ssid = wpa_s->conf->ssid;
1084 wpa_s->prev_sched_ssid = ssid;
1085 }
1086
1087 if (wildcard) {
1088 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1089 params.num_ssids++;
1090 }
1091
1092 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001093 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001094 goto next;
1095
1096 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1097 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1098 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1099 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1100 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1101 ssid->ssid, ssid->ssid_len);
1102 params.filter_ssids[params.num_filter_ssids].ssid_len =
1103 ssid->ssid_len;
1104 params.num_filter_ssids++;
1105 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1106 {
1107 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1108 "filter for sched_scan - drop filter");
1109 os_free(params.filter_ssids);
1110 params.filter_ssids = NULL;
1111 params.num_filter_ssids = 0;
1112 }
1113
1114 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1115 if (params.num_ssids == max_sched_scan_ssids)
1116 break; /* only room for broadcast SSID */
1117 wpa_dbg(wpa_s, MSG_DEBUG,
1118 "add to active scan ssid: %s",
1119 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1120 params.ssids[params.num_ssids].ssid =
1121 ssid->ssid;
1122 params.ssids[params.num_ssids].ssid_len =
1123 ssid->ssid_len;
1124 params.num_ssids++;
1125 if (params.num_ssids >= max_sched_scan_ssids) {
1126 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001127 do {
1128 ssid = ssid->next;
1129 } while (ssid &&
1130 (wpas_network_disabled(wpa_s, ssid) ||
1131 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001132 break;
1133 }
1134 }
1135
1136 next:
1137 wpa_s->prev_sched_ssid = ssid;
1138 ssid = ssid->next;
1139 }
1140
1141 if (params.num_filter_ssids == 0) {
1142 os_free(params.filter_ssids);
1143 params.filter_ssids = NULL;
1144 }
1145
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001146 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1147 if (extra_ie) {
1148 params.extra_ies = wpabuf_head(extra_ie);
1149 params.extra_ies_len = wpabuf_len(extra_ie);
1150 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001151
Dmitry Shmidt04949592012-07-19 12:16:46 -07001152 scan_params = &params;
1153
1154scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001155 if (ssid || !wpa_s->first_sched_scan) {
1156 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001157 "Starting sched scan: interval %d timeout %d",
1158 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001159 } else {
1160 wpa_dbg(wpa_s, MSG_DEBUG,
1161 "Starting sched scan: interval %d (no timeout)",
1162 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001163 }
1164
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001165 wpa_setband_scan_freqs(wpa_s, scan_params);
1166
Dmitry Shmidt04949592012-07-19 12:16:46 -07001167 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001168 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001169 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001170 os_free(params.filter_ssids);
1171 if (ret) {
1172 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1173 if (prev_state != wpa_s->wpa_state)
1174 wpa_supplicant_set_state(wpa_s, prev_state);
1175 return ret;
1176 }
1177
1178 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1179 if (ssid || !wpa_s->first_sched_scan) {
1180 wpa_s->sched_scan_timed_out = 0;
1181 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1182 wpa_supplicant_sched_scan_timeout,
1183 wpa_s, NULL);
1184 wpa_s->first_sched_scan = 0;
1185 wpa_s->sched_scan_timeout /= 2;
1186 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001187 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1188 wpa_s->sched_scan_interval = 10;
1189 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1190 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001191 }
1192
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001193 /* If there is no more ssids, start next time from the beginning */
1194 if (!ssid)
1195 wpa_s->prev_sched_ssid = NULL;
1196
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197 return 0;
1198}
1199
1200
1201/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1203 * @wpa_s: Pointer to wpa_supplicant data
1204 *
1205 * This function is used to cancel a scan request scheduled with
1206 * wpa_supplicant_req_scan().
1207 */
1208void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1209{
1210 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1211 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001212 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001213#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001214 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001215#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216}
1217
1218
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001219/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001220 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1221 * @wpa_s: Pointer to wpa_supplicant data
1222 *
1223 * This function is used to stop a delayed scheduled scan.
1224 */
1225void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1226{
1227 if (!wpa_s->sched_scan_supported)
1228 return;
1229
1230 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1231 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1232 wpa_s, NULL);
1233}
1234
1235
1236/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1238 * @wpa_s: Pointer to wpa_supplicant data
1239 *
1240 * This function is used to stop a periodic scheduled scan.
1241 */
1242void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1243{
1244 if (!wpa_s->sched_scanning)
1245 return;
1246
1247 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1248 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1249 wpa_supplicant_stop_sched_scan(wpa_s);
1250}
1251
1252
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001253/**
1254 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1255 * @wpa_s: Pointer to wpa_supplicant data
1256 * @scanning: Whether scanning is currently in progress
1257 *
1258 * This function is to generate scanning notifycations. It is called whenever
1259 * there may have been a change in scanning (scan started, completed, stopped).
1260 * wpas_notify_scanning() is called whenever the scanning state changed from the
1261 * previously notified state.
1262 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1264 int scanning)
1265{
1266 if (wpa_s->scanning != scanning) {
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001267#ifdef ANDROID_P2P
1268 if(!wpa_s->sched_scanning)
1269 wpa_s->scanning = scanning;
1270#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 wpa_s->scanning = scanning;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001272#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 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/**
1359 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data 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 payload or %NULL if not found
1363 *
1364 * This function returns concatenated payload of possibly fragmented vendor
1365 * specific information elements in the scan result. The caller is responsible
1366 * for freeing the returned buffer.
1367 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1369 u32 vendor_type)
1370{
1371 struct wpabuf *buf;
1372 const u8 *end, *pos;
1373
1374 buf = wpabuf_alloc(res->ie_len);
1375 if (buf == NULL)
1376 return NULL;
1377
1378 pos = (const u8 *) (res + 1);
1379 end = pos + res->ie_len;
1380
1381 while (pos + 1 < end) {
1382 if (pos + 2 + pos[1] > end)
1383 break;
1384 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1385 vendor_type == WPA_GET_BE32(&pos[2]))
1386 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1387 pos += 2 + pos[1];
1388 }
1389
1390 if (wpabuf_len(buf) == 0) {
1391 wpabuf_free(buf);
1392 buf = NULL;
1393 }
1394
1395 return buf;
1396}
1397
1398
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001399/*
1400 * Channels with a great SNR can operate at full rate. What is a great SNR?
1401 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1402 * rule of thumb is that any SNR above 20 is good." This one
1403 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1404 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1405 * conservative value.
1406 */
1407#define GREAT_SNR 30
1408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409/* Compare function for sorting scan results. Return >0 if @b is considered
1410 * better. */
1411static int wpa_scan_result_compar(const void *a, const void *b)
1412{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001413#define IS_5GHZ(n) (n > 4000)
1414#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 struct wpa_scan_res **_wa = (void *) a;
1416 struct wpa_scan_res **_wb = (void *) b;
1417 struct wpa_scan_res *wa = *_wa;
1418 struct wpa_scan_res *wb = *_wb;
1419 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001420 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421
1422 /* WPA/WPA2 support preferred */
1423 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1424 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1425 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1426 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1427
1428 if (wpa_b && !wpa_a)
1429 return 1;
1430 if (!wpa_b && wpa_a)
1431 return -1;
1432
1433 /* privacy support preferred */
1434 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1435 (wb->caps & IEEE80211_CAP_PRIVACY))
1436 return 1;
1437 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1438 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1439 return -1;
1440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001441 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1442 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1443 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1444 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1445 } else {
1446 /* Not suitable information to calculate SNR, so use level */
1447 snr_a = wa->level;
1448 snr_b = wb->level;
1449 }
1450
1451 /* best/max rate preferred if SNR close enough */
1452 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1454 maxrate_a = wpa_scan_get_max_rate(wa);
1455 maxrate_b = wpa_scan_get_max_rate(wb);
1456 if (maxrate_a != maxrate_b)
1457 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001458 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1459 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 }
1461
1462 /* use freq for channel preference */
1463
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001464 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 * identical, use quality values since some drivers may only report
1466 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001467 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001469 return snr_b - snr_a;
1470#undef MIN
1471#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472}
1473
1474
1475#ifdef CONFIG_WPS
1476/* Compare function for sorting scan results when searching a WPS AP for
1477 * provisioning. Return >0 if @b is considered better. */
1478static int wpa_scan_result_wps_compar(const void *a, const void *b)
1479{
1480 struct wpa_scan_res **_wa = (void *) a;
1481 struct wpa_scan_res **_wb = (void *) b;
1482 struct wpa_scan_res *wa = *_wa;
1483 struct wpa_scan_res *wb = *_wb;
1484 int uses_wps_a, uses_wps_b;
1485 struct wpabuf *wps_a, *wps_b;
1486 int res;
1487
1488 /* Optimization - check WPS IE existence before allocated memory and
1489 * doing full reassembly. */
1490 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1491 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1492 if (uses_wps_a && !uses_wps_b)
1493 return -1;
1494 if (!uses_wps_a && uses_wps_b)
1495 return 1;
1496
1497 if (uses_wps_a && uses_wps_b) {
1498 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1499 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1500 res = wps_ap_priority_compar(wps_a, wps_b);
1501 wpabuf_free(wps_a);
1502 wpabuf_free(wps_b);
1503 if (res)
1504 return res;
1505 }
1506
1507 /*
1508 * Do not use current AP security policy as a sorting criteria during
1509 * WPS provisioning step since the AP may get reconfigured at the
1510 * completion of provisioning.
1511 */
1512
1513 /* all things being equal, use signal level; if signal levels are
1514 * identical, use quality values since some drivers may only report
1515 * that value and leave the signal level zero */
1516 if (wb->level == wa->level)
1517 return wb->qual - wa->qual;
1518 return wb->level - wa->level;
1519}
1520#endif /* CONFIG_WPS */
1521
1522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001523static void dump_scan_res(struct wpa_scan_results *scan_res)
1524{
1525#ifndef CONFIG_NO_STDOUT_DEBUG
1526 size_t i;
1527
1528 if (scan_res->res == NULL || scan_res->num == 0)
1529 return;
1530
1531 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1532
1533 for (i = 0; i < scan_res->num; i++) {
1534 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001535 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001536 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1537 == WPA_SCAN_LEVEL_DBM) {
1538 int snr = r->level - r->noise;
1539 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001540 "noise=%d level=%d snr=%d%s flags=0x%x "
1541 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001542 MAC2STR(r->bssid), r->freq, r->qual,
1543 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001544 snr >= GREAT_SNR ? "*" : "", r->flags,
1545 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001546 } else {
1547 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001548 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001550 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001552 pos = (u8 *) (r + 1);
1553 if (r->ie_len)
1554 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1555 pos += r->ie_len;
1556 if (r->beacon_ie_len)
1557 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1558 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001559 }
1560#endif /* CONFIG_NO_STDOUT_DEBUG */
1561}
1562
1563
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001564/**
1565 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1566 * @wpa_s: Pointer to wpa_supplicant data
1567 * @bssid: BSSID to check
1568 * Returns: 0 if the BSSID is filtered or 1 if not
1569 *
1570 * This function is used to filter out specific BSSIDs from scan reslts mainly
1571 * for testing purposes (SET bssid_filter ctrl_iface command).
1572 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001573int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1574 const u8 *bssid)
1575{
1576 size_t i;
1577
1578 if (wpa_s->bssid_filter == NULL)
1579 return 1;
1580
1581 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1582 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1583 ETH_ALEN) == 0)
1584 return 1;
1585 }
1586
1587 return 0;
1588}
1589
1590
1591static void filter_scan_res(struct wpa_supplicant *wpa_s,
1592 struct wpa_scan_results *res)
1593{
1594 size_t i, j;
1595
1596 if (wpa_s->bssid_filter == NULL)
1597 return;
1598
1599 for (i = 0, j = 0; i < res->num; i++) {
1600 if (wpa_supplicant_filter_bssid_match(wpa_s,
1601 res->res[i]->bssid)) {
1602 res->res[j++] = res->res[i];
1603 } else {
1604 os_free(res->res[i]);
1605 res->res[i] = NULL;
1606 }
1607 }
1608
1609 if (res->num != j) {
1610 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1611 (int) (res->num - j));
1612 res->num = j;
1613 }
1614}
1615
1616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617/**
1618 * wpa_supplicant_get_scan_results - Get scan results
1619 * @wpa_s: Pointer to wpa_supplicant data
1620 * @info: Information about what was scanned or %NULL if not available
1621 * @new_scan: Whether a new scan was performed
1622 * Returns: Scan results, %NULL on failure
1623 *
1624 * This function request the current scan results from the driver and updates
1625 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1626 * results with wpa_scan_results_free().
1627 */
1628struct wpa_scan_results *
1629wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1630 struct scan_info *info, int new_scan)
1631{
1632 struct wpa_scan_results *scan_res;
1633 size_t i;
1634 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001636 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 if (scan_res == NULL) {
1638 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1639 return NULL;
1640 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001641 if (scan_res->fetch_time.sec == 0) {
1642 /*
1643 * Make sure we have a valid timestamp if the driver wrapper
1644 * does not set this.
1645 */
1646 os_get_time(&scan_res->fetch_time);
1647 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001648 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649
1650#ifdef CONFIG_WPS
1651 if (wpas_wps_in_progress(wpa_s)) {
1652 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1653 "provisioning rules");
1654 compar = wpa_scan_result_wps_compar;
1655 }
1656#endif /* CONFIG_WPS */
1657
1658 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1659 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001660 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661
1662 wpa_bss_update_start(wpa_s);
1663 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001664 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1665 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 wpa_bss_update_end(wpa_s, info, new_scan);
1667
1668 return scan_res;
1669}
1670
1671
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001672/**
1673 * wpa_supplicant_update_scan_results - Update scan results from the driver
1674 * @wpa_s: Pointer to wpa_supplicant data
1675 * Returns: 0 on success, -1 on failure
1676 *
1677 * This function updates the BSS table within wpa_supplicant based on the
1678 * currently available scan results from the driver without requesting a new
1679 * scan. This is used in cases where the driver indicates an association
1680 * (including roaming within ESS) and wpa_supplicant does not yet have the
1681 * needed information to complete the connection (e.g., to perform validation
1682 * steps in 4-way handshake).
1683 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1685{
1686 struct wpa_scan_results *scan_res;
1687 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1688 if (scan_res == NULL)
1689 return -1;
1690 wpa_scan_results_free(scan_res);
1691
1692 return 0;
1693}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001694
1695
1696/**
1697 * scan_only_handler - Reports scan results
1698 */
1699void scan_only_handler(struct wpa_supplicant *wpa_s,
1700 struct wpa_scan_results *scan_res)
1701{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001702 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001703 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1704 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001705 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001706}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001707
1708
1709int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1710{
1711 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1712}