blob: 8cd0f1d2ecdb5216b1a7997654d1c2ebe1584b72 [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
491/*
492 * Find the operating frequency of any other virtual interface that is using
493 * the same radio concurrently.
494 */
495static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
496{
497 const char *rn, *rn2;
498 struct wpa_supplicant *ifs;
499 u8 bssid[ETH_ALEN];
500
501 if (!wpa_s->driver->get_radio_name)
502 return -1;
503
504 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
505 if (rn == NULL || rn[0] == '\0')
506 return -1;
507
508 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
509 if (ifs == wpa_s || !ifs->driver->get_radio_name)
510 continue;
511
512 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
513 if (!rn2 || os_strcmp(rn, rn2) != 0)
514 continue;
515
516 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
517 continue;
518
519 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
520 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
521 return ifs->current_ssid->frequency;
522 if (wpa_drv_get_bssid(ifs, bssid) == 0)
523 return ifs->assoc_freq;
524 }
525
526 return 0;
527}
528
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800529
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700530static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
531 u16 num_modes,
532 enum hostapd_hw_mode mode)
533{
534 u16 i;
535
536 for (i = 0; i < num_modes; i++) {
537 if (modes[i].mode == mode)
538 return &modes[i];
539 }
540
541 return NULL;
542}
543
544
545static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
546 enum hostapd_hw_mode band,
547 struct wpa_driver_scan_params *params)
548{
549 /* Include only supported channels for the specified band */
550 struct hostapd_hw_modes *mode;
551 int count, i;
552
553 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
554 if (mode == NULL) {
555 /* No channels supported in this band - use empty list */
556 params->freqs = os_zalloc(sizeof(int));
557 return;
558 }
559
560 params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
561 if (params->freqs == NULL)
562 return;
563 for (count = 0, i = 0; i < mode->num_channels; i++) {
564 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
565 continue;
566 params->freqs[count++] = mode->channels[i].freq;
567 }
568}
569
570
571static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
572 struct wpa_driver_scan_params *params)
573{
574 if (wpa_s->hw.modes == NULL)
575 return; /* unknown what channels the driver supports */
576 if (params->freqs)
577 return; /* already using a limited channel set */
578 if (wpa_s->setband == WPA_SETBAND_5G)
579 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
580 params);
581 else if (wpa_s->setband == WPA_SETBAND_2G)
582 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
583 params);
584}
585
586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
588{
589 struct wpa_supplicant *wpa_s = eloop_ctx;
590 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800591 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
592 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700593 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700595 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 size_t max_ssids;
597 enum wpa_states prev_state;
598
599 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
600 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700601 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602 return;
603 }
604
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800605 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700606 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700608 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609 return;
610 }
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700611#ifdef ANDROID
612 if (wpa_s->scanning) {
613 /* If we are already in scanning state, we shall ignore this new scan request*/
614 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
615 return;
616 }
617#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700618 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800619 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
621 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700622 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 return;
624 }
625
626 if (wpa_s->conf->ap_scan != 0 &&
627 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
628 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
629 "overriding ap_scan configuration");
630 wpa_s->conf->ap_scan = 0;
631 wpas_notify_ap_scan_changed(wpa_s);
632 }
633
634 if (wpa_s->conf->ap_scan == 0) {
635 wpa_supplicant_gen_assoc_event(wpa_s);
636 return;
637 }
638
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800639#ifdef CONFIG_P2P
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700640 if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700641 if (wpa_s->sta_scan_pending &&
642 wpas_p2p_in_progress(wpa_s) == 2 &&
Jouni Malinendc7b7132012-09-14 12:53:47 -0700643 wpa_s->global->p2p_cb_on_scan_complete) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700644 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
645 "mode scan during P2P search");
646 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800647 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
648 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700649 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800650 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700651 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800653 }
654#endif /* CONFIG_P2P */
655
656 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 max_ssids = 1;
658 else {
659 max_ssids = wpa_s->max_scan_ssids;
660 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
661 max_ssids = WPAS_MAX_SCAN_SSIDS;
662 }
663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800665 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666
667 os_memset(&params, 0, sizeof(params));
668
669 prev_state = wpa_s->wpa_state;
670 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
671 wpa_s->wpa_state == WPA_INACTIVE)
672 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
673
Dmitry Shmidt04949592012-07-19 12:16:46 -0700674 /*
675 * If autoscan has set its own scanning parameters
676 */
677 if (wpa_s->autoscan_params != NULL) {
678 scan_params = wpa_s->autoscan_params;
679 goto scan;
680 }
681
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800682 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700683 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
684 if (ssid == wpa_s->connect_without_scan)
685 break;
686 }
687 wpa_s->connect_without_scan = NULL;
688 if (ssid) {
689 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
690 "without scan step");
691 wpa_supplicant_associate(wpa_s, NULL, ssid);
692 return;
693 }
694 }
695
Dmitry Shmidt04949592012-07-19 12:16:46 -0700696#ifdef CONFIG_P2P
697 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
698 wpa_s->go_params) {
699 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
700 "P2P group formation");
701 params.ssids[0].ssid = wpa_s->go_params->ssid;
702 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
703 params.num_ssids = 1;
704 goto ssid_list_set;
705 }
706#endif /* CONFIG_P2P */
707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 /* Find the starting point from which to continue scanning */
709 ssid = wpa_s->conf->ssid;
710 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
711 while (ssid) {
712 if (ssid == wpa_s->prev_scan_ssid) {
713 ssid = ssid->next;
714 break;
715 }
716 ssid = ssid->next;
717 }
718 }
719
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800720 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700721 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800722 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 wpa_supplicant_assoc_try(wpa_s, ssid);
724 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700725#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 } else if (wpa_s->conf->ap_scan == 2) {
727 /*
728 * User-initiated scan request in ap_scan == 2; scan with
729 * wildcard SSID.
730 */
731 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700732#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 } else {
734 struct wpa_ssid *start = ssid, *tssid;
735 int freqs_set = 0;
736 if (ssid == NULL && max_ssids > 1)
737 ssid = wpa_s->conf->ssid;
738 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700739 if (!wpas_network_disabled(wpa_s, ssid) &&
740 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
742 ssid->ssid, ssid->ssid_len);
743 params.ssids[params.num_ssids].ssid =
744 ssid->ssid;
745 params.ssids[params.num_ssids].ssid_len =
746 ssid->ssid_len;
747 params.num_ssids++;
748 if (params.num_ssids + 1 >= max_ssids)
749 break;
750 }
751 ssid = ssid->next;
752 if (ssid == start)
753 break;
754 if (ssid == NULL && max_ssids > 1 &&
755 start != wpa_s->conf->ssid)
756 ssid = wpa_s->conf->ssid;
757 }
758
759 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700760 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 continue;
762 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
763 int_array_concat(&params.freqs,
764 tssid->scan_freq);
765 } else {
766 os_free(params.freqs);
767 params.freqs = NULL;
768 }
769 freqs_set = 1;
770 }
771 int_array_sort_unique(params.freqs);
772 }
773
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800774 if (ssid && max_ssids == 1) {
775 /*
776 * If the driver is limited to 1 SSID at a time interleave
777 * wildcard SSID scans with specific SSID scans to avoid
778 * waiting a long time for a wildcard scan.
779 */
780 if (!wpa_s->prev_scan_wildcard) {
781 params.ssids[0].ssid = NULL;
782 params.ssids[0].ssid_len = 0;
783 wpa_s->prev_scan_wildcard = 1;
784 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
785 "wildcard SSID (Interleave with specific)");
786 } else {
787 wpa_s->prev_scan_ssid = ssid;
788 wpa_s->prev_scan_wildcard = 0;
789 wpa_dbg(wpa_s, MSG_DEBUG,
790 "Starting AP scan for specific SSID: %s",
791 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800793 } else if (ssid) {
794 /* max_ssids > 1 */
795
796 wpa_s->prev_scan_ssid = ssid;
797 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
798 "the scan request");
799 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 } else {
801 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
802 params.num_ssids++;
803 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
804 "SSID");
805 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700806#ifdef CONFIG_P2P
807ssid_list_set:
808#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800810 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700811 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812
813 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
814 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
815 "generated frequency list");
816 params.freqs = wpa_s->next_scan_freqs;
817 } else
818 os_free(wpa_s->next_scan_freqs);
819 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700820 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700822 /* See if user specified frequencies. If so, scan only those. */
823 if (wpa_s->conf->freq_list && !params.freqs) {
824 wpa_dbg(wpa_s, MSG_DEBUG,
825 "Optimize scan based on conf->freq_list");
826 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
827 }
828
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700829 /* Use current associated channel? */
830 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
831 int freq = shared_vif_oper_freq(wpa_s);
832 if (freq > 0) {
833 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current "
834 "operating channel (%d MHz) since "
835 "scan_cur_freq is enabled", freq);
836 params.freqs = os_zalloc(sizeof(int) * 2);
837 if (params.freqs)
838 params.freqs[0] = freq;
839 }
840 }
841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 params.filter_ssids = wpa_supplicant_build_filter_ssids(
843 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800844 if (extra_ie) {
845 params.extra_ies = wpabuf_head(extra_ie);
846 params.extra_ies_len = wpabuf_len(extra_ie);
847 }
848
849#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700850 if (wpa_s->p2p_in_provisioning ||
851 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800852 /*
853 * The interface may not yet be in P2P mode, so we have to
854 * explicitly request P2P probe to disable CCK rates.
855 */
856 params.p2p_probe = 1;
857 }
858#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860 scan_params = &params;
861
862scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800863#ifdef CONFIG_P2P
864 /*
865 * If the driver does not support multi-channel concurrency and a
866 * virtual interface that shares the same radio with the wpa_s interface
867 * is operating there may not be need to scan other channels apart from
868 * the current operating channel on the other virtual interface. Filter
869 * out other channels in case we are trying to find a connection for a
870 * station interface when we are not configured to prefer station
871 * connection and a concurrent operation is already in process.
872 */
873 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
874 !scan_params->freqs && !params.freqs &&
875 wpas_is_p2p_prioritized(wpa_s) &&
876 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
877 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
878 non_p2p_network_enabled(wpa_s)) {
879 int freq = shared_vif_oper_freq(wpa_s);
880 if (freq > 0) {
881 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
882 "operating channel (%d MHz) since driver does "
883 "not support multi-channel concurrency", freq);
884 params.freqs = os_zalloc(sizeof(int) * 2);
885 if (params.freqs)
886 params.freqs[0] = freq;
887 scan_params->freqs = params.freqs;
888 }
889 }
890#endif /* CONFIG_P2P */
891
Dmitry Shmidt04949592012-07-19 12:16:46 -0700892 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800894 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 os_free(params.freqs);
896 os_free(params.filter_ssids);
897
898 if (ret) {
899 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
900 if (prev_state != wpa_s->wpa_state)
901 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800902 /* Restore scan_req since we will try to scan again */
903 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800905 } else {
906 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 }
908}
909
910
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800911void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
912{
913 struct os_time remaining, new_int;
914 int cancelled;
915
916 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
917 &remaining);
918
919 new_int.sec = sec;
920 new_int.usec = 0;
921 if (cancelled && os_time_before(&remaining, &new_int)) {
922 new_int.sec = remaining.sec;
923 new_int.usec = remaining.usec;
924 }
925
926 eloop_register_timeout(new_int.sec, new_int.usec, wpa_supplicant_scan,
927 wpa_s, NULL);
928 wpa_s->scan_interval = sec;
929}
930
931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932/**
933 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
934 * @wpa_s: Pointer to wpa_supplicant data
935 * @sec: Number of seconds after which to scan
936 * @usec: Number of microseconds after which to scan
937 *
938 * This function is used to schedule a scan for neighboring access points after
939 * the specified time.
940 */
941void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
942{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800943#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 /* If there's at least one network that should be specifically scanned
945 * then don't cancel the scan and reschedule. Some drivers do
946 * background scanning which generates frequent scan results, and that
947 * causes the specific SSID scan to get continually pushed back and
948 * never happen, which causes hidden APs to never get probe-scanned.
949 */
950 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
951 wpa_s->conf->ap_scan == 1) {
952 struct wpa_ssid *ssid = wpa_s->conf->ssid;
953
954 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700955 if (!wpas_network_disabled(wpa_s, ssid) &&
956 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 break;
958 ssid = ssid->next;
959 }
960 if (ssid) {
961 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
962 "ensure that specific SSID scans occur");
963 return;
964 }
965 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800966#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
968 sec, usec);
969 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
970 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
971}
972
973
974/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800975 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
976 * @wpa_s: Pointer to wpa_supplicant data
977 * @sec: Number of seconds after which to scan
978 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800979 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800980 *
981 * This function is used to schedule periodic scans for neighboring
982 * access points after the specified time.
983 */
984int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
985 int sec, int usec)
986{
987 if (!wpa_s->sched_scan_supported)
988 return -1;
989
990 eloop_register_timeout(sec, usec,
991 wpa_supplicant_delayed_sched_scan_timeout,
992 wpa_s, NULL);
993
994 return 0;
995}
996
997
998/**
999 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1000 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001001 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 *
1003 * This function is used to schedule periodic scans for neighboring
1004 * access points repeating the scan continuously.
1005 */
1006int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1007{
1008 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001009 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001010 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001012 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001013 int ret;
1014 unsigned int max_sched_scan_ssids;
1015 int wildcard = 0;
1016 int need_ssids;
1017
1018 if (!wpa_s->sched_scan_supported)
1019 return -1;
1020
1021 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1022 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1023 else
1024 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001025 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001026 return -1;
1027
1028 if (wpa_s->sched_scanning) {
1029 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1030 return 0;
1031 }
1032
1033 need_ssids = 0;
1034 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001035 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001036 /* Use wildcard SSID to find this network */
1037 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001038 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1039 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001040 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041
1042#ifdef CONFIG_WPS
1043 if (!wpas_network_disabled(wpa_s, ssid) &&
1044 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1045 /*
1046 * Normal scan is more reliable and faster for WPS
1047 * operations and since these are for short periods of
1048 * time, the benefit of trying to use sched_scan would
1049 * be limited.
1050 */
1051 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1052 "sched_scan for WPS");
1053 return -1;
1054 }
1055#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001056 }
1057 if (wildcard)
1058 need_ssids++;
1059
1060 if (wpa_s->normal_scans < 3 &&
1061 (need_ssids <= wpa_s->max_scan_ssids ||
1062 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1063 /*
1064 * When normal scan can speed up operations, use that for the
1065 * first operations before starting the sched_scan to allow
1066 * user space sleep more. We do this only if the normal scan
1067 * has functionality that is suitable for this or if the
1068 * sched_scan does not have better support for multiple SSIDs.
1069 */
1070 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1071 "sched_scan for initial scans (normal_scans=%d)",
1072 wpa_s->normal_scans);
1073 return -1;
1074 }
1075
1076 os_memset(&params, 0, sizeof(params));
1077
1078 /* If we can't allocate space for the filters, we just don't filter */
1079 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
1080 sizeof(struct wpa_driver_scan_filter));
1081
1082 prev_state = wpa_s->wpa_state;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001083#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1085 wpa_s->wpa_state == WPA_INACTIVE)
1086 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001087#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001088
Dmitry Shmidt04949592012-07-19 12:16:46 -07001089 if (wpa_s->autoscan_params != NULL) {
1090 scan_params = wpa_s->autoscan_params;
1091 goto scan;
1092 }
1093
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001094 /* Find the starting point from which to continue scanning */
1095 ssid = wpa_s->conf->ssid;
1096 if (wpa_s->prev_sched_ssid) {
1097 while (ssid) {
1098 if (ssid == wpa_s->prev_sched_ssid) {
1099 ssid = ssid->next;
1100 break;
1101 }
1102 ssid = ssid->next;
1103 }
1104 }
1105
1106 if (!ssid || !wpa_s->prev_sched_ssid) {
1107 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001108 if (wpa_s->conf->sched_scan_interval)
1109 wpa_s->sched_scan_interval =
1110 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001111 if (wpa_s->sched_scan_interval == 0)
1112 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001113 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1114 wpa_s->first_sched_scan = 1;
1115 ssid = wpa_s->conf->ssid;
1116 wpa_s->prev_sched_ssid = ssid;
1117 }
1118
1119 if (wildcard) {
1120 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1121 params.num_ssids++;
1122 }
1123
1124 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001125 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001126 goto next;
1127
1128 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1129 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1130 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1131 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1132 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1133 ssid->ssid, ssid->ssid_len);
1134 params.filter_ssids[params.num_filter_ssids].ssid_len =
1135 ssid->ssid_len;
1136 params.num_filter_ssids++;
1137 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1138 {
1139 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1140 "filter for sched_scan - drop filter");
1141 os_free(params.filter_ssids);
1142 params.filter_ssids = NULL;
1143 params.num_filter_ssids = 0;
1144 }
1145
1146 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1147 if (params.num_ssids == max_sched_scan_ssids)
1148 break; /* only room for broadcast SSID */
1149 wpa_dbg(wpa_s, MSG_DEBUG,
1150 "add to active scan ssid: %s",
1151 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1152 params.ssids[params.num_ssids].ssid =
1153 ssid->ssid;
1154 params.ssids[params.num_ssids].ssid_len =
1155 ssid->ssid_len;
1156 params.num_ssids++;
1157 if (params.num_ssids >= max_sched_scan_ssids) {
1158 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001159 do {
1160 ssid = ssid->next;
1161 } while (ssid &&
1162 (wpas_network_disabled(wpa_s, ssid) ||
1163 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001164 break;
1165 }
1166 }
1167
1168 next:
1169 wpa_s->prev_sched_ssid = ssid;
1170 ssid = ssid->next;
1171 }
1172
1173 if (params.num_filter_ssids == 0) {
1174 os_free(params.filter_ssids);
1175 params.filter_ssids = NULL;
1176 }
1177
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001178 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1179 if (extra_ie) {
1180 params.extra_ies = wpabuf_head(extra_ie);
1181 params.extra_ies_len = wpabuf_len(extra_ie);
1182 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001183
Dmitry Shmidt04949592012-07-19 12:16:46 -07001184 scan_params = &params;
1185
1186scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001187 if (ssid || !wpa_s->first_sched_scan) {
1188 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001189 "Starting sched scan: interval %d timeout %d",
1190 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001191 } else {
1192 wpa_dbg(wpa_s, MSG_DEBUG,
1193 "Starting sched scan: interval %d (no timeout)",
1194 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001195 }
1196
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001197 wpa_setband_scan_freqs(wpa_s, scan_params);
1198
Dmitry Shmidt04949592012-07-19 12:16:46 -07001199 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001200 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001201 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001202 os_free(params.filter_ssids);
1203 if (ret) {
1204 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1205 if (prev_state != wpa_s->wpa_state)
1206 wpa_supplicant_set_state(wpa_s, prev_state);
1207 return ret;
1208 }
1209
1210 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1211 if (ssid || !wpa_s->first_sched_scan) {
1212 wpa_s->sched_scan_timed_out = 0;
1213 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1214 wpa_supplicant_sched_scan_timeout,
1215 wpa_s, NULL);
1216 wpa_s->first_sched_scan = 0;
1217 wpa_s->sched_scan_timeout /= 2;
1218 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001219 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1220 wpa_s->sched_scan_interval = 10;
1221 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1222 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001223 }
1224
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001225 /* If there is no more ssids, start next time from the beginning */
1226 if (!ssid)
1227 wpa_s->prev_sched_ssid = NULL;
1228
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229 return 0;
1230}
1231
1232
1233/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1235 * @wpa_s: Pointer to wpa_supplicant data
1236 *
1237 * This function is used to cancel a scan request scheduled with
1238 * wpa_supplicant_req_scan().
1239 */
1240void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1241{
1242 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1243 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001244 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001245#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001246 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001247#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248}
1249
1250
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251/**
1252 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1253 * @wpa_s: Pointer to wpa_supplicant data
1254 *
1255 * This function is used to stop a periodic scheduled scan.
1256 */
1257void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1258{
1259 if (!wpa_s->sched_scanning)
1260 return;
1261
1262 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1263 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1264 wpa_supplicant_stop_sched_scan(wpa_s);
1265}
1266
1267
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001268/**
1269 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1270 * @wpa_s: Pointer to wpa_supplicant data
1271 * @scanning: Whether scanning is currently in progress
1272 *
1273 * This function is to generate scanning notifycations. It is called whenever
1274 * there may have been a change in scanning (scan started, completed, stopped).
1275 * wpas_notify_scanning() is called whenever the scanning state changed from the
1276 * previously notified state.
1277 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1279 int scanning)
1280{
1281 if (wpa_s->scanning != scanning) {
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001282#ifdef ANDROID_P2P
1283 if(!wpa_s->sched_scanning)
1284 wpa_s->scanning = scanning;
1285#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 wpa_s->scanning = scanning;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001287#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 wpas_notify_scanning(wpa_s);
1289 }
1290}
1291
1292
1293static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1294{
1295 int rate = 0;
1296 const u8 *ie;
1297 int i;
1298
1299 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1300 for (i = 0; ie && i < ie[1]; i++) {
1301 if ((ie[i + 2] & 0x7f) > rate)
1302 rate = ie[i + 2] & 0x7f;
1303 }
1304
1305 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1306 for (i = 0; ie && i < ie[1]; i++) {
1307 if ((ie[i + 2] & 0x7f) > rate)
1308 rate = ie[i + 2] & 0x7f;
1309 }
1310
1311 return rate;
1312}
1313
1314
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001315/**
1316 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1317 * @res: Scan result entry
1318 * @ie: Information element identitifier (WLAN_EID_*)
1319 * Returns: Pointer to the information element (id field) or %NULL if not found
1320 *
1321 * This function returns the first matching information element in the scan
1322 * result.
1323 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1325{
1326 const u8 *end, *pos;
1327
1328 pos = (const u8 *) (res + 1);
1329 end = pos + res->ie_len;
1330
1331 while (pos + 1 < end) {
1332 if (pos + 2 + pos[1] > end)
1333 break;
1334 if (pos[0] == ie)
1335 return pos;
1336 pos += 2 + pos[1];
1337 }
1338
1339 return NULL;
1340}
1341
1342
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001343/**
1344 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1345 * @res: Scan result entry
1346 * @vendor_type: Vendor type (four octets starting the IE payload)
1347 * Returns: Pointer to the information element (id field) or %NULL if not found
1348 *
1349 * This function returns the first matching information element in the scan
1350 * result.
1351 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1353 u32 vendor_type)
1354{
1355 const u8 *end, *pos;
1356
1357 pos = (const u8 *) (res + 1);
1358 end = pos + res->ie_len;
1359
1360 while (pos + 1 < end) {
1361 if (pos + 2 + pos[1] > end)
1362 break;
1363 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1364 vendor_type == WPA_GET_BE32(&pos[2]))
1365 return pos;
1366 pos += 2 + pos[1];
1367 }
1368
1369 return NULL;
1370}
1371
1372
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001373/**
1374 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1375 * @res: Scan result entry
1376 * @vendor_type: Vendor type (four octets starting the IE payload)
1377 * Returns: Pointer to the information element payload or %NULL if not found
1378 *
1379 * This function returns concatenated payload of possibly fragmented vendor
1380 * specific information elements in the scan result. The caller is responsible
1381 * for freeing the returned buffer.
1382 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1384 u32 vendor_type)
1385{
1386 struct wpabuf *buf;
1387 const u8 *end, *pos;
1388
1389 buf = wpabuf_alloc(res->ie_len);
1390 if (buf == NULL)
1391 return NULL;
1392
1393 pos = (const u8 *) (res + 1);
1394 end = pos + res->ie_len;
1395
1396 while (pos + 1 < end) {
1397 if (pos + 2 + pos[1] > end)
1398 break;
1399 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1400 vendor_type == WPA_GET_BE32(&pos[2]))
1401 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1402 pos += 2 + pos[1];
1403 }
1404
1405 if (wpabuf_len(buf) == 0) {
1406 wpabuf_free(buf);
1407 buf = NULL;
1408 }
1409
1410 return buf;
1411}
1412
1413
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001414/*
1415 * Channels with a great SNR can operate at full rate. What is a great SNR?
1416 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1417 * rule of thumb is that any SNR above 20 is good." This one
1418 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1419 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1420 * conservative value.
1421 */
1422#define GREAT_SNR 30
1423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424/* Compare function for sorting scan results. Return >0 if @b is considered
1425 * better. */
1426static int wpa_scan_result_compar(const void *a, const void *b)
1427{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001428#define IS_5GHZ(n) (n > 4000)
1429#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 struct wpa_scan_res **_wa = (void *) a;
1431 struct wpa_scan_res **_wb = (void *) b;
1432 struct wpa_scan_res *wa = *_wa;
1433 struct wpa_scan_res *wb = *_wb;
1434 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001435 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436
1437 /* WPA/WPA2 support preferred */
1438 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1439 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1440 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1441 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1442
1443 if (wpa_b && !wpa_a)
1444 return 1;
1445 if (!wpa_b && wpa_a)
1446 return -1;
1447
1448 /* privacy support preferred */
1449 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1450 (wb->caps & IEEE80211_CAP_PRIVACY))
1451 return 1;
1452 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1453 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1454 return -1;
1455
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1457 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1458 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1459 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1460 } else {
1461 /* Not suitable information to calculate SNR, so use level */
1462 snr_a = wa->level;
1463 snr_b = wb->level;
1464 }
1465
1466 /* best/max rate preferred if SNR close enough */
1467 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1469 maxrate_a = wpa_scan_get_max_rate(wa);
1470 maxrate_b = wpa_scan_get_max_rate(wb);
1471 if (maxrate_a != maxrate_b)
1472 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001473 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1474 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 }
1476
1477 /* use freq for channel preference */
1478
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480 * identical, use quality values since some drivers may only report
1481 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001482 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001484 return snr_b - snr_a;
1485#undef MIN
1486#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487}
1488
1489
1490#ifdef CONFIG_WPS
1491/* Compare function for sorting scan results when searching a WPS AP for
1492 * provisioning. Return >0 if @b is considered better. */
1493static int wpa_scan_result_wps_compar(const void *a, const void *b)
1494{
1495 struct wpa_scan_res **_wa = (void *) a;
1496 struct wpa_scan_res **_wb = (void *) b;
1497 struct wpa_scan_res *wa = *_wa;
1498 struct wpa_scan_res *wb = *_wb;
1499 int uses_wps_a, uses_wps_b;
1500 struct wpabuf *wps_a, *wps_b;
1501 int res;
1502
1503 /* Optimization - check WPS IE existence before allocated memory and
1504 * doing full reassembly. */
1505 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1506 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1507 if (uses_wps_a && !uses_wps_b)
1508 return -1;
1509 if (!uses_wps_a && uses_wps_b)
1510 return 1;
1511
1512 if (uses_wps_a && uses_wps_b) {
1513 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1514 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1515 res = wps_ap_priority_compar(wps_a, wps_b);
1516 wpabuf_free(wps_a);
1517 wpabuf_free(wps_b);
1518 if (res)
1519 return res;
1520 }
1521
1522 /*
1523 * Do not use current AP security policy as a sorting criteria during
1524 * WPS provisioning step since the AP may get reconfigured at the
1525 * completion of provisioning.
1526 */
1527
1528 /* all things being equal, use signal level; if signal levels are
1529 * identical, use quality values since some drivers may only report
1530 * that value and leave the signal level zero */
1531 if (wb->level == wa->level)
1532 return wb->qual - wa->qual;
1533 return wb->level - wa->level;
1534}
1535#endif /* CONFIG_WPS */
1536
1537
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001538static void dump_scan_res(struct wpa_scan_results *scan_res)
1539{
1540#ifndef CONFIG_NO_STDOUT_DEBUG
1541 size_t i;
1542
1543 if (scan_res->res == NULL || scan_res->num == 0)
1544 return;
1545
1546 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1547
1548 for (i = 0; i < scan_res->num; i++) {
1549 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001550 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1552 == WPA_SCAN_LEVEL_DBM) {
1553 int snr = r->level - r->noise;
1554 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001555 "noise=%d level=%d snr=%d%s flags=0x%x "
1556 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001557 MAC2STR(r->bssid), r->freq, r->qual,
1558 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001559 snr >= GREAT_SNR ? "*" : "", r->flags,
1560 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561 } else {
1562 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001563 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001564 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001565 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001566 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001567 pos = (u8 *) (r + 1);
1568 if (r->ie_len)
1569 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1570 pos += r->ie_len;
1571 if (r->beacon_ie_len)
1572 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1573 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001574 }
1575#endif /* CONFIG_NO_STDOUT_DEBUG */
1576}
1577
1578
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001579/**
1580 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1581 * @wpa_s: Pointer to wpa_supplicant data
1582 * @bssid: BSSID to check
1583 * Returns: 0 if the BSSID is filtered or 1 if not
1584 *
1585 * This function is used to filter out specific BSSIDs from scan reslts mainly
1586 * for testing purposes (SET bssid_filter ctrl_iface command).
1587 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001588int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1589 const u8 *bssid)
1590{
1591 size_t i;
1592
1593 if (wpa_s->bssid_filter == NULL)
1594 return 1;
1595
1596 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1597 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1598 ETH_ALEN) == 0)
1599 return 1;
1600 }
1601
1602 return 0;
1603}
1604
1605
1606static void filter_scan_res(struct wpa_supplicant *wpa_s,
1607 struct wpa_scan_results *res)
1608{
1609 size_t i, j;
1610
1611 if (wpa_s->bssid_filter == NULL)
1612 return;
1613
1614 for (i = 0, j = 0; i < res->num; i++) {
1615 if (wpa_supplicant_filter_bssid_match(wpa_s,
1616 res->res[i]->bssid)) {
1617 res->res[j++] = res->res[i];
1618 } else {
1619 os_free(res->res[i]);
1620 res->res[i] = NULL;
1621 }
1622 }
1623
1624 if (res->num != j) {
1625 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1626 (int) (res->num - j));
1627 res->num = j;
1628 }
1629}
1630
1631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632/**
1633 * wpa_supplicant_get_scan_results - Get scan results
1634 * @wpa_s: Pointer to wpa_supplicant data
1635 * @info: Information about what was scanned or %NULL if not available
1636 * @new_scan: Whether a new scan was performed
1637 * Returns: Scan results, %NULL on failure
1638 *
1639 * This function request the current scan results from the driver and updates
1640 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1641 * results with wpa_scan_results_free().
1642 */
1643struct wpa_scan_results *
1644wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1645 struct scan_info *info, int new_scan)
1646{
1647 struct wpa_scan_results *scan_res;
1648 size_t i;
1649 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1650
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001651 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652 if (scan_res == NULL) {
1653 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1654 return NULL;
1655 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001656 if (scan_res->fetch_time.sec == 0) {
1657 /*
1658 * Make sure we have a valid timestamp if the driver wrapper
1659 * does not set this.
1660 */
1661 os_get_time(&scan_res->fetch_time);
1662 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001663 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664
1665#ifdef CONFIG_WPS
1666 if (wpas_wps_in_progress(wpa_s)) {
1667 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1668 "provisioning rules");
1669 compar = wpa_scan_result_wps_compar;
1670 }
1671#endif /* CONFIG_WPS */
1672
1673 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1674 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001675 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676
1677 wpa_bss_update_start(wpa_s);
1678 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001679 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1680 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 wpa_bss_update_end(wpa_s, info, new_scan);
1682
1683 return scan_res;
1684}
1685
1686
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001687/**
1688 * wpa_supplicant_update_scan_results - Update scan results from the driver
1689 * @wpa_s: Pointer to wpa_supplicant data
1690 * Returns: 0 on success, -1 on failure
1691 *
1692 * This function updates the BSS table within wpa_supplicant based on the
1693 * currently available scan results from the driver without requesting a new
1694 * scan. This is used in cases where the driver indicates an association
1695 * (including roaming within ESS) and wpa_supplicant does not yet have the
1696 * needed information to complete the connection (e.g., to perform validation
1697 * steps in 4-way handshake).
1698 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1700{
1701 struct wpa_scan_results *scan_res;
1702 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1703 if (scan_res == NULL)
1704 return -1;
1705 wpa_scan_results_free(scan_res);
1706
1707 return 0;
1708}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001709
1710
1711/**
1712 * scan_only_handler - Reports scan results
1713 */
1714void scan_only_handler(struct wpa_supplicant *wpa_s,
1715 struct wpa_scan_results *scan_res)
1716{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001717 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001718 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1719 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001720 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001721}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001722
1723
1724int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1725{
1726 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1727}