blob: e453cfc2f56f09cf11280188ca5a8ab5f87c052c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "utils/includes.h"
16
17#include "utils/common.h"
18#include "utils/eloop.h"
19#include "common/ieee802_11_defs.h"
20#include "config.h"
21#include "wpa_supplicant_i.h"
22#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "wps_supplicant.h"
24#include "p2p_supplicant.h"
25#include "p2p/p2p.h"
26#include "notify.h"
27#include "bss.h"
28#include "scan.h"
29
30
31static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
32{
33 struct wpa_ssid *ssid;
34 union wpa_event_data data;
35
36 ssid = wpa_supplicant_get_ssid(wpa_s);
37 if (ssid == NULL)
38 return;
39
40 if (wpa_s->current_ssid == NULL) {
41 wpa_s->current_ssid = ssid;
42 if (wpa_s->current_ssid != NULL)
43 wpas_notify_network_changed(wpa_s);
44 }
45 wpa_supplicant_initiate_eapol(wpa_s);
46 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
47 "network - generating associated event");
48 os_memset(&data, 0, sizeof(data));
49 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
50}
51
52
53#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055 enum wps_request_type *req_type)
56{
57 struct wpa_ssid *ssid;
58 int wps = 0;
59
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080060 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070061 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
62 continue;
63
64 wps = 1;
65 *req_type = wpas_wps_get_req_type(ssid);
66 if (!ssid->eap.phase1)
67 continue;
68
69 if (os_strstr(ssid->eap.phase1, "pbc=1"))
70 return 2;
71 }
72
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080073#ifdef CONFIG_P2P
74 wpa_s->wps->dev.p2p = 1;
75 if (!wps) {
76 wps = 1;
77 *req_type = WPS_REQ_ENROLLEE_INFO;
78 }
79#endif /* CONFIG_P2P */
80
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081 return wps;
82}
83#endif /* CONFIG_WPS */
84
85
86int wpa_supplicant_enabled_networks(struct wpa_config *conf)
87{
88 struct wpa_ssid *ssid = conf->ssid;
89 int count = 0;
90 while (ssid) {
91 if (!ssid->disabled)
92 count++;
93 ssid = ssid->next;
94 }
95 return count;
96}
97
98
99static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
100 struct wpa_ssid *ssid)
101{
102 while (ssid) {
103 if (!ssid->disabled)
104 break;
105 ssid = ssid->next;
106 }
107
108 /* ap_scan=2 mode - try to associate with each SSID. */
109 if (ssid == NULL) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
111 "end of scan list - go back to beginning");
112 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
113 wpa_supplicant_req_scan(wpa_s, 0, 0);
114 return;
115 }
116 if (ssid->next) {
117 /* Continue from the next SSID on the next attempt. */
118 wpa_s->prev_scan_ssid = ssid;
119 } else {
120 /* Start from the beginning of the SSID list. */
121 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
122 }
123 wpa_supplicant_associate(wpa_s, NULL, ssid);
124}
125
126
127static int int_array_len(const int *a)
128{
129 int i;
130 for (i = 0; a && a[i]; i++)
131 ;
132 return i;
133}
134
135
136static void int_array_concat(int **res, const int *a)
137{
138 int reslen, alen, i;
139 int *n;
140
141 reslen = int_array_len(*res);
142 alen = int_array_len(a);
143
144 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
145 if (n == NULL) {
146 os_free(*res);
147 *res = NULL;
148 return;
149 }
150 for (i = 0; i <= alen; i++)
151 n[reslen + i] = a[i];
152 *res = n;
153}
154
155
156static int freq_cmp(const void *a, const void *b)
157{
158 int _a = *(int *) a;
159 int _b = *(int *) b;
160
161 if (_a == 0)
162 return 1;
163 if (_b == 0)
164 return -1;
165 return _a - _b;
166}
167
168
169static void int_array_sort_unique(int *a)
170{
171 int alen;
172 int i, j;
173
174 if (a == NULL)
175 return;
176
177 alen = int_array_len(a);
178 qsort(a, alen, sizeof(int), freq_cmp);
179
180 i = 0;
181 j = 1;
182 while (a[i] && a[j]) {
183 if (a[i] == a[j]) {
184 j++;
185 continue;
186 }
187 a[++i] = a[j++];
188 }
189 if (a[i])
190 i++;
191 a[i] = 0;
192}
193
194
195int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
196 struct wpa_driver_scan_params *params)
197{
198 int ret;
199
200 wpa_supplicant_notify_scanning(wpa_s, 1);
201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800202 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 if (ret) {
204 wpa_supplicant_notify_scanning(wpa_s, 0);
205 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800206 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 wpa_s->scan_runs++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208 wpa_s->normal_scans++;
209 }
210
211 return ret;
212}
213
214
215static void
216wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
217{
218 struct wpa_supplicant *wpa_s = eloop_ctx;
219
220 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
221
222 if (wpa_supplicant_req_sched_scan(wpa_s))
223 wpa_supplicant_req_scan(wpa_s, 0, 0);
224}
225
226
227static void
228wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
229{
230 struct wpa_supplicant *wpa_s = eloop_ctx;
231
232 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
233
234 wpa_s->sched_scan_timed_out = 1;
235 wpa_supplicant_cancel_sched_scan(wpa_s);
236}
237
238
239static int
240wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
241 struct wpa_driver_scan_params *params,
242 int interval)
243{
244 int ret;
245
246 wpa_supplicant_notify_scanning(wpa_s, 1);
247 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
248 if (ret)
249 wpa_supplicant_notify_scanning(wpa_s, 0);
250 else
251 wpa_s->sched_scanning = 1;
252
253 return ret;
254}
255
256
257static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
258{
259 int ret;
260
261 ret = wpa_drv_stop_sched_scan(wpa_s);
262 if (ret) {
263 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
264 /* TODO: what to do if stopping fails? */
265 return -1;
266 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267
268 return ret;
269}
270
271
272static struct wpa_driver_scan_filter *
273wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
274{
275 struct wpa_driver_scan_filter *ssids;
276 struct wpa_ssid *ssid;
277 size_t count;
278
279 *num_ssids = 0;
280 if (!conf->filter_ssids)
281 return NULL;
282
283 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
284 if (ssid->ssid && ssid->ssid_len)
285 count++;
286 }
287 if (count == 0)
288 return NULL;
289 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
290 if (ssids == NULL)
291 return NULL;
292
293 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
294 if (!ssid->ssid || !ssid->ssid_len)
295 continue;
296 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
297 ssids[*num_ssids].ssid_len = ssid->ssid_len;
298 (*num_ssids)++;
299 }
300
301 return ssids;
302}
303
304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800305static void wpa_supplicant_optimize_freqs(
306 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
307{
308#ifdef CONFIG_P2P
309 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
310 wpa_s->go_params) {
311 /* Optimize provisioning state scan based on GO information */
312 if (wpa_s->p2p_in_provisioning < 5 &&
313 wpa_s->go_params->freq > 0) {
314 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
315 "preferred frequency %d MHz",
316 wpa_s->go_params->freq);
317 params->freqs = os_zalloc(2 * sizeof(int));
318 if (params->freqs)
319 params->freqs[0] = wpa_s->go_params->freq;
320 } else if (wpa_s->p2p_in_provisioning < 8 &&
321 wpa_s->go_params->freq_list[0]) {
322 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
323 "channels");
324 int_array_concat(&params->freqs,
325 wpa_s->go_params->freq_list);
326 if (params->freqs)
327 int_array_sort_unique(params->freqs);
328 }
329 wpa_s->p2p_in_provisioning++;
330 }
331#endif /* CONFIG_P2P */
332
333#ifdef CONFIG_WPS
334 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
335 /*
336 * Optimize post-provisioning scan based on channel used
337 * during provisioning.
338 */
339 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
340 "that was used during provisioning", wpa_s->wps_freq);
341 params->freqs = os_zalloc(2 * sizeof(int));
342 if (params->freqs)
343 params->freqs[0] = wpa_s->wps_freq;
344 wpa_s->after_wps--;
345 }
346
347#endif /* CONFIG_WPS */
348}
349
350
351#ifdef CONFIG_INTERWORKING
352static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
353 struct wpabuf *buf)
354{
355 if (wpa_s->conf->interworking == 0)
356 return;
357
358 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
359 wpabuf_put_u8(buf, 4);
360 wpabuf_put_u8(buf, 0x00);
361 wpabuf_put_u8(buf, 0x00);
362 wpabuf_put_u8(buf, 0x00);
363 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
364
365 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
366 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
367 1 + ETH_ALEN);
368 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
369 /* No Venue Info */
370 if (!is_zero_ether_addr(wpa_s->conf->hessid))
371 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
372}
373#endif /* CONFIG_INTERWORKING */
374
375
376static struct wpabuf *
377wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
378 struct wpa_driver_scan_params *params)
379{
380 struct wpabuf *extra_ie = NULL;
381#ifdef CONFIG_WPS
382 int wps = 0;
383 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
384#endif /* CONFIG_WPS */
385
386#ifdef CONFIG_INTERWORKING
387 if (wpa_s->conf->interworking &&
388 wpabuf_resize(&extra_ie, 100) == 0)
389 wpas_add_interworking_elements(wpa_s, extra_ie);
390#endif /* CONFIG_INTERWORKING */
391
392#ifdef CONFIG_WPS
393 wps = wpas_wps_in_use(wpa_s, &req_type);
394
395 if (wps) {
396 struct wpabuf *wps_ie;
397 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
398 wpa_s->wps->uuid, req_type,
399 0, NULL);
400 if (wps_ie) {
401 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
402 wpabuf_put_buf(extra_ie, wps_ie);
403 wpabuf_free(wps_ie);
404 }
405 }
406
407#ifdef CONFIG_P2P
408 if (wps) {
409 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
410 if (wpabuf_resize(&extra_ie, ielen) == 0)
411 wpas_p2p_scan_ie(wpa_s, extra_ie);
412 }
413#endif /* CONFIG_P2P */
414
415#endif /* CONFIG_WPS */
416
417 return extra_ie;
418}
419
420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
422{
423 struct wpa_supplicant *wpa_s = eloop_ctx;
424 struct wpa_ssid *ssid;
425 int scan_req = 0, ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800426 struct wpabuf *extra_ie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 struct wpa_driver_scan_params params;
428 size_t max_ssids;
429 enum wpa_states prev_state;
430
431 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
432 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
433 return;
434 }
435
436 if (wpa_s->disconnected && !wpa_s->scan_req) {
437 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
438 return;
439 }
440
441 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
442 !wpa_s->scan_req) {
443 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
444 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
445 return;
446 }
447
448 if (wpa_s->conf->ap_scan != 0 &&
449 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
450 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
451 "overriding ap_scan configuration");
452 wpa_s->conf->ap_scan = 0;
453 wpas_notify_ap_scan_changed(wpa_s);
454 }
455
456 if (wpa_s->conf->ap_scan == 0) {
457 wpa_supplicant_gen_assoc_event(wpa_s);
458 return;
459 }
460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800461#ifdef CONFIG_P2P
462 if (wpas_p2p_in_progress(wpa_s)) {
463 if (wpa_s->wpa_state == WPA_SCANNING) {
464 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
465 "while P2P operation is in progress");
466 wpa_supplicant_req_scan(wpa_s, 5, 0);
467 } else {
468 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
469 "P2P operation is in progress");
470 }
471 return;
472 }
473#endif /* CONFIG_P2P */
474
475 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 max_ssids = 1;
477 else {
478 max_ssids = wpa_s->max_scan_ssids;
479 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
480 max_ssids = WPAS_MAX_SCAN_SSIDS;
481 }
482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 scan_req = wpa_s->scan_req;
484 wpa_s->scan_req = 0;
485
486 os_memset(&params, 0, sizeof(params));
487
488 prev_state = wpa_s->wpa_state;
489 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
490 wpa_s->wpa_state == WPA_INACTIVE)
491 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
492
Jouni Malinen75ecf522011-06-27 15:19:46 -0700493 if (scan_req != 2 && wpa_s->connect_without_scan) {
494 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
495 if (ssid == wpa_s->connect_without_scan)
496 break;
497 }
498 wpa_s->connect_without_scan = NULL;
499 if (ssid) {
500 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
501 "without scan step");
502 wpa_supplicant_associate(wpa_s, NULL, ssid);
503 return;
504 }
505 }
506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 /* Find the starting point from which to continue scanning */
508 ssid = wpa_s->conf->ssid;
509 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
510 while (ssid) {
511 if (ssid == wpa_s->prev_scan_ssid) {
512 ssid = ssid->next;
513 break;
514 }
515 ssid = ssid->next;
516 }
517 }
518
Jouni Malinen75ecf522011-06-27 15:19:46 -0700519 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
520 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521 wpa_supplicant_assoc_try(wpa_s, ssid);
522 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700523#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 } else if (wpa_s->conf->ap_scan == 2) {
525 /*
526 * User-initiated scan request in ap_scan == 2; scan with
527 * wildcard SSID.
528 */
529 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700530#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 } else {
532 struct wpa_ssid *start = ssid, *tssid;
533 int freqs_set = 0;
534 if (ssid == NULL && max_ssids > 1)
535 ssid = wpa_s->conf->ssid;
536 while (ssid) {
537 if (!ssid->disabled && ssid->scan_ssid) {
538 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
539 ssid->ssid, ssid->ssid_len);
540 params.ssids[params.num_ssids].ssid =
541 ssid->ssid;
542 params.ssids[params.num_ssids].ssid_len =
543 ssid->ssid_len;
544 params.num_ssids++;
545 if (params.num_ssids + 1 >= max_ssids)
546 break;
547 }
548 ssid = ssid->next;
549 if (ssid == start)
550 break;
551 if (ssid == NULL && max_ssids > 1 &&
552 start != wpa_s->conf->ssid)
553 ssid = wpa_s->conf->ssid;
554 }
555
556 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
557 if (tssid->disabled)
558 continue;
559 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
560 int_array_concat(&params.freqs,
561 tssid->scan_freq);
562 } else {
563 os_free(params.freqs);
564 params.freqs = NULL;
565 }
566 freqs_set = 1;
567 }
568 int_array_sort_unique(params.freqs);
569 }
570
571 if (ssid) {
572 wpa_s->prev_scan_ssid = ssid;
573 if (max_ssids > 1) {
574 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
575 "the scan request");
576 params.num_ssids++;
577 }
578 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
579 "SSID(s)");
580 } else {
581 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
582 params.num_ssids++;
583 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
584 "SSID");
585 }
586
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800587 wpa_supplicant_optimize_freqs(wpa_s, &params);
588 extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589
590 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
591 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
592 "generated frequency list");
593 params.freqs = wpa_s->next_scan_freqs;
594 } else
595 os_free(wpa_s->next_scan_freqs);
596 wpa_s->next_scan_freqs = NULL;
597
598 params.filter_ssids = wpa_supplicant_build_filter_ssids(
599 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800600 if (extra_ie) {
601 params.extra_ies = wpabuf_head(extra_ie);
602 params.extra_ies_len = wpabuf_len(extra_ie);
603 }
604
605#ifdef CONFIG_P2P
606 if (wpa_s->p2p_in_provisioning) {
607 /*
608 * The interface may not yet be in P2P mode, so we have to
609 * explicitly request P2P probe to disable CCK rates.
610 */
611 params.p2p_probe = 1;
612 }
613#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614
615 ret = wpa_supplicant_trigger_scan(wpa_s, &params);
616
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800617 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 os_free(params.freqs);
619 os_free(params.filter_ssids);
620
621 if (ret) {
622 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
623 if (prev_state != wpa_s->wpa_state)
624 wpa_supplicant_set_state(wpa_s, prev_state);
625 wpa_supplicant_req_scan(wpa_s, 1, 0);
626 }
627}
628
629
630/**
631 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
632 * @wpa_s: Pointer to wpa_supplicant data
633 * @sec: Number of seconds after which to scan
634 * @usec: Number of microseconds after which to scan
635 *
636 * This function is used to schedule a scan for neighboring access points after
637 * the specified time.
638 */
639void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
640{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800641#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642 /* If there's at least one network that should be specifically scanned
643 * then don't cancel the scan and reschedule. Some drivers do
644 * background scanning which generates frequent scan results, and that
645 * causes the specific SSID scan to get continually pushed back and
646 * never happen, which causes hidden APs to never get probe-scanned.
647 */
648 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
649 wpa_s->conf->ap_scan == 1) {
650 struct wpa_ssid *ssid = wpa_s->conf->ssid;
651
652 while (ssid) {
653 if (!ssid->disabled && ssid->scan_ssid)
654 break;
655 ssid = ssid->next;
656 }
657 if (ssid) {
658 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
659 "ensure that specific SSID scans occur");
660 return;
661 }
662 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800663#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
665 sec, usec);
666 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
667 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
668}
669
670
671/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800672 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
673 * @wpa_s: Pointer to wpa_supplicant data
674 * @sec: Number of seconds after which to scan
675 * @usec: Number of microseconds after which to scan
676 *
677 * This function is used to schedule periodic scans for neighboring
678 * access points after the specified time.
679 */
680int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
681 int sec, int usec)
682{
683 if (!wpa_s->sched_scan_supported)
684 return -1;
685
686 eloop_register_timeout(sec, usec,
687 wpa_supplicant_delayed_sched_scan_timeout,
688 wpa_s, NULL);
689
690 return 0;
691}
692
693
694/**
695 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
696 * @wpa_s: Pointer to wpa_supplicant data
697 *
698 * This function is used to schedule periodic scans for neighboring
699 * access points repeating the scan continuously.
700 */
701int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
702{
703 struct wpa_driver_scan_params params;
704 enum wpa_states prev_state;
705 struct wpa_ssid *ssid;
706 struct wpabuf *wps_ie = NULL;
707 int ret;
708 unsigned int max_sched_scan_ssids;
709 int wildcard = 0;
710 int need_ssids;
711
712 if (!wpa_s->sched_scan_supported)
713 return -1;
714
715 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
716 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
717 else
718 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
719 if (max_sched_scan_ssids < 1)
720 return -1;
721
722 if (wpa_s->sched_scanning) {
723 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
724 return 0;
725 }
726
727 need_ssids = 0;
728 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
729 if (!ssid->disabled && !ssid->scan_ssid) {
730 /* Use wildcard SSID to find this network */
731 wildcard = 1;
732 } else if (!ssid->disabled && ssid->ssid_len)
733 need_ssids++;
734 }
735 if (wildcard)
736 need_ssids++;
737
738 if (wpa_s->normal_scans < 3 &&
739 (need_ssids <= wpa_s->max_scan_ssids ||
740 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
741 /*
742 * When normal scan can speed up operations, use that for the
743 * first operations before starting the sched_scan to allow
744 * user space sleep more. We do this only if the normal scan
745 * has functionality that is suitable for this or if the
746 * sched_scan does not have better support for multiple SSIDs.
747 */
748 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
749 "sched_scan for initial scans (normal_scans=%d)",
750 wpa_s->normal_scans);
751 return -1;
752 }
753
754 os_memset(&params, 0, sizeof(params));
755
756 /* If we can't allocate space for the filters, we just don't filter */
757 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
758 sizeof(struct wpa_driver_scan_filter));
759
760 prev_state = wpa_s->wpa_state;
761 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
762 wpa_s->wpa_state == WPA_INACTIVE)
763 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
764
765 /* Find the starting point from which to continue scanning */
766 ssid = wpa_s->conf->ssid;
767 if (wpa_s->prev_sched_ssid) {
768 while (ssid) {
769 if (ssid == wpa_s->prev_sched_ssid) {
770 ssid = ssid->next;
771 break;
772 }
773 ssid = ssid->next;
774 }
775 }
776
777 if (!ssid || !wpa_s->prev_sched_ssid) {
778 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
779
780 wpa_s->sched_scan_interval = 10;
781 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
782 wpa_s->first_sched_scan = 1;
783 ssid = wpa_s->conf->ssid;
784 wpa_s->prev_sched_ssid = ssid;
785 }
786
787 if (wildcard) {
788 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
789 params.num_ssids++;
790 }
791
792 while (ssid) {
793 if (ssid->disabled)
794 goto next;
795
796 if (params.num_filter_ssids < wpa_s->max_match_sets &&
797 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
798 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
799 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
800 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
801 ssid->ssid, ssid->ssid_len);
802 params.filter_ssids[params.num_filter_ssids].ssid_len =
803 ssid->ssid_len;
804 params.num_filter_ssids++;
805 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
806 {
807 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
808 "filter for sched_scan - drop filter");
809 os_free(params.filter_ssids);
810 params.filter_ssids = NULL;
811 params.num_filter_ssids = 0;
812 }
813
814 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
815 if (params.num_ssids == max_sched_scan_ssids)
816 break; /* only room for broadcast SSID */
817 wpa_dbg(wpa_s, MSG_DEBUG,
818 "add to active scan ssid: %s",
819 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
820 params.ssids[params.num_ssids].ssid =
821 ssid->ssid;
822 params.ssids[params.num_ssids].ssid_len =
823 ssid->ssid_len;
824 params.num_ssids++;
825 if (params.num_ssids >= max_sched_scan_ssids) {
826 wpa_s->prev_sched_ssid = ssid;
827 break;
828 }
829 }
830
831 next:
832 wpa_s->prev_sched_ssid = ssid;
833 ssid = ssid->next;
834 }
835
836 if (params.num_filter_ssids == 0) {
837 os_free(params.filter_ssids);
838 params.filter_ssids = NULL;
839 }
840
841 if (wpa_s->wps)
842 wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
843
844 if (ssid || !wpa_s->first_sched_scan) {
845 wpa_dbg(wpa_s, MSG_DEBUG,
846 "Starting sched scan: interval %d (no timeout)",
847 wpa_s->sched_scan_interval);
848 } else {
849 wpa_dbg(wpa_s, MSG_DEBUG,
850 "Starting sched scan: interval %d timeout %d",
851 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
852 }
853
854 ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
855 wpa_s->sched_scan_interval);
856 wpabuf_free(wps_ie);
857 os_free(params.filter_ssids);
858 if (ret) {
859 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
860 if (prev_state != wpa_s->wpa_state)
861 wpa_supplicant_set_state(wpa_s, prev_state);
862 return ret;
863 }
864
865 /* If we have more SSIDs to scan, add a timeout so we scan them too */
866 if (ssid || !wpa_s->first_sched_scan) {
867 wpa_s->sched_scan_timed_out = 0;
868 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
869 wpa_supplicant_sched_scan_timeout,
870 wpa_s, NULL);
871 wpa_s->first_sched_scan = 0;
872 wpa_s->sched_scan_timeout /= 2;
873 wpa_s->sched_scan_interval *= 2;
874 }
875
876 return 0;
877}
878
879
880/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
882 * @wpa_s: Pointer to wpa_supplicant data
883 *
884 * This function is used to cancel a scan request scheduled with
885 * wpa_supplicant_req_scan().
886 */
887void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
888{
889 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
890 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt20df8072011-04-08 15:35:17 -0700891 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892}
893
894
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800895/**
896 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
897 * @wpa_s: Pointer to wpa_supplicant data
898 *
899 * This function is used to stop a periodic scheduled scan.
900 */
901void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
902{
903 if (!wpa_s->sched_scanning)
904 return;
905
906 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
907 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
908 wpa_supplicant_stop_sched_scan(wpa_s);
909}
910
911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
913 int scanning)
914{
915 if (wpa_s->scanning != scanning) {
916 wpa_s->scanning = scanning;
917 wpas_notify_scanning(wpa_s);
918 }
919}
920
921
922static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
923{
924 int rate = 0;
925 const u8 *ie;
926 int i;
927
928 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
929 for (i = 0; ie && i < ie[1]; i++) {
930 if ((ie[i + 2] & 0x7f) > rate)
931 rate = ie[i + 2] & 0x7f;
932 }
933
934 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
935 for (i = 0; ie && i < ie[1]; i++) {
936 if ((ie[i + 2] & 0x7f) > rate)
937 rate = ie[i + 2] & 0x7f;
938 }
939
940 return rate;
941}
942
943
944const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
945{
946 const u8 *end, *pos;
947
948 pos = (const u8 *) (res + 1);
949 end = pos + res->ie_len;
950
951 while (pos + 1 < end) {
952 if (pos + 2 + pos[1] > end)
953 break;
954 if (pos[0] == ie)
955 return pos;
956 pos += 2 + pos[1];
957 }
958
959 return NULL;
960}
961
962
963const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
964 u32 vendor_type)
965{
966 const u8 *end, *pos;
967
968 pos = (const u8 *) (res + 1);
969 end = pos + res->ie_len;
970
971 while (pos + 1 < end) {
972 if (pos + 2 + pos[1] > end)
973 break;
974 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
975 vendor_type == WPA_GET_BE32(&pos[2]))
976 return pos;
977 pos += 2 + pos[1];
978 }
979
980 return NULL;
981}
982
983
984struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
985 u32 vendor_type)
986{
987 struct wpabuf *buf;
988 const u8 *end, *pos;
989
990 buf = wpabuf_alloc(res->ie_len);
991 if (buf == NULL)
992 return NULL;
993
994 pos = (const u8 *) (res + 1);
995 end = pos + res->ie_len;
996
997 while (pos + 1 < end) {
998 if (pos + 2 + pos[1] > end)
999 break;
1000 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1001 vendor_type == WPA_GET_BE32(&pos[2]))
1002 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1003 pos += 2 + pos[1];
1004 }
1005
1006 if (wpabuf_len(buf) == 0) {
1007 wpabuf_free(buf);
1008 buf = NULL;
1009 }
1010
1011 return buf;
1012}
1013
1014
1015struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1016 const struct wpa_scan_res *res, u32 vendor_type)
1017{
1018 struct wpabuf *buf;
1019 const u8 *end, *pos;
1020
1021 if (res->beacon_ie_len == 0)
1022 return NULL;
1023 buf = wpabuf_alloc(res->beacon_ie_len);
1024 if (buf == NULL)
1025 return NULL;
1026
1027 pos = (const u8 *) (res + 1);
1028 pos += res->ie_len;
1029 end = pos + res->beacon_ie_len;
1030
1031 while (pos + 1 < end) {
1032 if (pos + 2 + pos[1] > end)
1033 break;
1034 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1035 vendor_type == WPA_GET_BE32(&pos[2]))
1036 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1037 pos += 2 + pos[1];
1038 }
1039
1040 if (wpabuf_len(buf) == 0) {
1041 wpabuf_free(buf);
1042 buf = NULL;
1043 }
1044
1045 return buf;
1046}
1047
1048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049/*
1050 * Channels with a great SNR can operate at full rate. What is a great SNR?
1051 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1052 * rule of thumb is that any SNR above 20 is good." This one
1053 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1054 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1055 * conservative value.
1056 */
1057#define GREAT_SNR 30
1058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059/* Compare function for sorting scan results. Return >0 if @b is considered
1060 * better. */
1061static int wpa_scan_result_compar(const void *a, const void *b)
1062{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001063#define IS_5GHZ(n) (n > 4000)
1064#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 struct wpa_scan_res **_wa = (void *) a;
1066 struct wpa_scan_res **_wb = (void *) b;
1067 struct wpa_scan_res *wa = *_wa;
1068 struct wpa_scan_res *wb = *_wb;
1069 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001070 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071
1072 /* WPA/WPA2 support preferred */
1073 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1074 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1075 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1076 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1077
1078 if (wpa_b && !wpa_a)
1079 return 1;
1080 if (!wpa_b && wpa_a)
1081 return -1;
1082
1083 /* privacy support preferred */
1084 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1085 (wb->caps & IEEE80211_CAP_PRIVACY))
1086 return 1;
1087 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1088 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1089 return -1;
1090
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001091 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1092 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1093 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1094 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1095 } else {
1096 /* Not suitable information to calculate SNR, so use level */
1097 snr_a = wa->level;
1098 snr_b = wb->level;
1099 }
1100
1101 /* best/max rate preferred if SNR close enough */
1102 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1104 maxrate_a = wpa_scan_get_max_rate(wa);
1105 maxrate_b = wpa_scan_get_max_rate(wb);
1106 if (maxrate_a != maxrate_b)
1107 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001108 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1109 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 }
1111
1112 /* use freq for channel preference */
1113
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001114 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 * identical, use quality values since some drivers may only report
1116 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001117 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001119 return snr_b - snr_a;
1120#undef MIN
1121#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122}
1123
1124
1125#ifdef CONFIG_WPS
1126/* Compare function for sorting scan results when searching a WPS AP for
1127 * provisioning. Return >0 if @b is considered better. */
1128static int wpa_scan_result_wps_compar(const void *a, const void *b)
1129{
1130 struct wpa_scan_res **_wa = (void *) a;
1131 struct wpa_scan_res **_wb = (void *) b;
1132 struct wpa_scan_res *wa = *_wa;
1133 struct wpa_scan_res *wb = *_wb;
1134 int uses_wps_a, uses_wps_b;
1135 struct wpabuf *wps_a, *wps_b;
1136 int res;
1137
1138 /* Optimization - check WPS IE existence before allocated memory and
1139 * doing full reassembly. */
1140 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1141 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1142 if (uses_wps_a && !uses_wps_b)
1143 return -1;
1144 if (!uses_wps_a && uses_wps_b)
1145 return 1;
1146
1147 if (uses_wps_a && uses_wps_b) {
1148 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1149 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1150 res = wps_ap_priority_compar(wps_a, wps_b);
1151 wpabuf_free(wps_a);
1152 wpabuf_free(wps_b);
1153 if (res)
1154 return res;
1155 }
1156
1157 /*
1158 * Do not use current AP security policy as a sorting criteria during
1159 * WPS provisioning step since the AP may get reconfigured at the
1160 * completion of provisioning.
1161 */
1162
1163 /* all things being equal, use signal level; if signal levels are
1164 * identical, use quality values since some drivers may only report
1165 * that value and leave the signal level zero */
1166 if (wb->level == wa->level)
1167 return wb->qual - wa->qual;
1168 return wb->level - wa->level;
1169}
1170#endif /* CONFIG_WPS */
1171
1172
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001173static void dump_scan_res(struct wpa_scan_results *scan_res)
1174{
1175#ifndef CONFIG_NO_STDOUT_DEBUG
1176 size_t i;
1177
1178 if (scan_res->res == NULL || scan_res->num == 0)
1179 return;
1180
1181 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1182
1183 for (i = 0; i < scan_res->num; i++) {
1184 struct wpa_scan_res *r = scan_res->res[i];
1185 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1186 == WPA_SCAN_LEVEL_DBM) {
1187 int snr = r->level - r->noise;
1188 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1189 "noise=%d level=%d snr=%d%s flags=0x%x",
1190 MAC2STR(r->bssid), r->freq, r->qual,
1191 r->noise, r->level, snr,
1192 snr >= GREAT_SNR ? "*" : "", r->flags);
1193 } else {
1194 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1195 "noise=%d level=%d flags=0x%x",
1196 MAC2STR(r->bssid), r->freq, r->qual,
1197 r->noise, r->level, r->flags);
1198 }
1199 }
1200#endif /* CONFIG_NO_STDOUT_DEBUG */
1201}
1202
1203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204/**
1205 * wpa_supplicant_get_scan_results - Get scan results
1206 * @wpa_s: Pointer to wpa_supplicant data
1207 * @info: Information about what was scanned or %NULL if not available
1208 * @new_scan: Whether a new scan was performed
1209 * Returns: Scan results, %NULL on failure
1210 *
1211 * This function request the current scan results from the driver and updates
1212 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1213 * results with wpa_scan_results_free().
1214 */
1215struct wpa_scan_results *
1216wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1217 struct scan_info *info, int new_scan)
1218{
1219 struct wpa_scan_results *scan_res;
1220 size_t i;
1221 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1222
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001223 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 if (scan_res == NULL) {
1225 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1226 return NULL;
1227 }
1228
1229#ifdef CONFIG_WPS
1230 if (wpas_wps_in_progress(wpa_s)) {
1231 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1232 "provisioning rules");
1233 compar = wpa_scan_result_wps_compar;
1234 }
1235#endif /* CONFIG_WPS */
1236
1237 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1238 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001239 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240
1241 wpa_bss_update_start(wpa_s);
1242 for (i = 0; i < scan_res->num; i++)
1243 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1244 wpa_bss_update_end(wpa_s, info, new_scan);
1245
1246 return scan_res;
1247}
1248
1249
1250int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1251{
1252 struct wpa_scan_results *scan_res;
1253 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1254 if (scan_res == NULL)
1255 return -1;
1256 wpa_scan_results_free(scan_res);
1257
1258 return 0;
1259}