]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/scan.c
nl80211: Mark Beacon event debug excessive
[thirdparty/hostap.git] / wpa_supplicant / scan.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Scanning
9ba9fa07 3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
6fc6879b
JM
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
9ba9fa07 15#include "utils/includes.h"
6fc6879b 16
9ba9fa07
JM
17#include "utils/common.h"
18#include "utils/eloop.h"
19#include "common/ieee802_11_defs.h"
6fc6879b
JM
20#include "config.h"
21#include "wpa_supplicant_i.h"
2d5b792d 22#include "driver_i.h"
b01c18a8 23#include "wps_supplicant.h"
0e65037c
JM
24#include "p2p_supplicant.h"
25#include "p2p/p2p.h"
8bac466b 26#include "notify.h"
9ba9fa07
JM
27#include "bss.h"
28#include "scan.h"
6fc6879b
JM
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
8bac466b 40 if (wpa_s->current_ssid == NULL) {
6fc6879b 41 wpa_s->current_ssid = ssid;
8bac466b
JM
42 if (wpa_s->current_ssid != NULL)
43 wpas_notify_network_changed(wpa_s);
44 }
6fc6879b 45 wpa_supplicant_initiate_eapol(wpa_s);
f049052b
BG
46 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
47 "network - generating associated event");
6fc6879b
JM
48 os_memset(&data, 0, sizeof(data));
49 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
50}
51
52
ad08c363 53#ifdef CONFIG_WPS
5f738a21 54static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
f90c86d4 55 enum wps_request_type *req_type)
ad08c363
JM
56{
57 struct wpa_ssid *ssid;
58 int wps = 0;
ad08c363 59
5f738a21 60 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
ad08c363
JM
61 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
62 continue;
63
64 wps = 1;
b01c18a8 65 *req_type = wpas_wps_get_req_type(ssid);
ad08c363
JM
66 if (!ssid->eap.phase1)
67 continue;
68
ad08c363
JM
69 if (os_strstr(ssid->eap.phase1, "pbc=1"))
70 return 2;
71 }
72
5f738a21
LC
73#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
ad08c363
JM
81 return wps;
82}
83#endif /* CONFIG_WPS */
84
e76baaac 85
4f34d51a 86int wpa_supplicant_enabled_networks(struct wpa_config *conf)
e76baaac
JM
87{
88 struct wpa_ssid *ssid = conf->ssid;
5471c343 89 int count = 0;
e76baaac
JM
90 while (ssid) {
91 if (!ssid->disabled)
5471c343 92 count++;
e76baaac
JM
93 ssid = ssid->next;
94 }
5471c343 95 return count;
e76baaac
JM
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) {
f049052b
BG
110 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
111 "end of scan list - go back to beginning");
ba2a573c 112 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
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. */
ba2a573c 121 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
122 }
123 wpa_supplicant_associate(wpa_s, NULL, ssid);
124}
125
126
d3a98225
JM
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;
e6c0ebff 148 return;
d3a98225
JM
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
60b94c98
JM
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
17fbb751 202 ret = wpa_drv_scan(wpa_s, params);
60b94c98
JM
203 if (ret) {
204 wpa_supplicant_notify_scanning(wpa_s, 0);
205 wpas_notify_scan_done(wpa_s, 0);
0b7a25c0 206 } else {
60b94c98 207 wpa_s->scan_runs++;
0b7a25c0
JM
208 wpa_s->normal_scans++;
209 }
60b94c98
JM
210
211 return ret;
212}
213
214
6a90053c
LC
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
cbdf3507
LC
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
cbdf3507
LC
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 }
267
268 return ret;
269}
270
271
3812464c
JM
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
5f738a21
LC
305static 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
46ee0427
JM
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);
11540c0b 368 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
46ee0427
JM
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
5f738a21
LC
376static struct wpabuf *
377wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
378 struct wpa_driver_scan_params *params)
6fc6879b 379{
46ee0427 380 struct wpabuf *extra_ie = NULL;
b01c18a8 381#ifdef CONFIG_WPS
509a3972 382 int wps = 0;
f90c86d4 383 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
46ee0427
JM
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 */
5f738a21 391
46ee0427 392#ifdef CONFIG_WPS
5f738a21
LC
393 wps = wpas_wps_in_use(wpa_s, &req_type);
394
395 if (wps) {
46ee0427 396 struct wpabuf *wps_ie;
5f738a21
LC
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) {
46ee0427
JM
401 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
402 wpabuf_put_buf(extra_ie, wps_ie);
403 wpabuf_free(wps_ie);
5f738a21
LC
404 }
405 }
406
407#ifdef CONFIG_P2P
46ee0427 408 if (wps) {
5f738a21 409 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
46ee0427
JM
410 if (wpabuf_resize(&extra_ie, ielen) == 0)
411 wpas_p2p_scan_ie(wpa_s, extra_ie);
5f738a21
LC
412 }
413#endif /* CONFIG_P2P */
414
b01c18a8 415#endif /* CONFIG_WPS */
5f738a21 416
46ee0427 417 return extra_ie;
5f738a21
LC
418}
419
420
421static 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;
46ee0427 426 struct wpabuf *extra_ie;
e76baaac
JM
427 struct wpa_driver_scan_params params;
428 size_t max_ssids;
207ef3fb 429 enum wpa_states prev_state;
6fc6879b 430
8401a6b0 431 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
f049052b 432 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
8401a6b0
JM
433 return;
434 }
435
3180d7a2
SO
436 if (wpa_s->disconnected && !wpa_s->scan_req) {
437 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6fc6879b 438 return;
3180d7a2 439 }
6fc6879b 440
e76baaac
JM
441 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
442 !wpa_s->scan_req) {
f049052b 443 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
6fc6879b
JM
444 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
445 return;
446 }
6fc6879b 447
c2a04078
JM
448 if (wpa_s->conf->ap_scan != 0 &&
449 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
f049052b
BG
450 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
451 "overriding ap_scan configuration");
6fc6879b 452 wpa_s->conf->ap_scan = 0;
8bac466b 453 wpas_notify_ap_scan_changed(wpa_s);
6fc6879b
JM
454 }
455
456 if (wpa_s->conf->ap_scan == 0) {
457 wpa_supplicant_gen_assoc_event(wpa_s);
458 return;
459 }
460
303f60d3
JM
461#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
17fbb751 475 if (wpa_s->conf->ap_scan == 2)
e76baaac
JM
476 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
e76baaac
JM
483 scan_req = wpa_s->scan_req;
484 wpa_s->scan_req = 0;
485
486 os_memset(&params, 0, sizeof(params));
487
207ef3fb 488 prev_state = wpa_s->wpa_state;
6fc6879b
JM
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
7dcdcfd6
JM
493 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
e76baaac 507 /* Find the starting point from which to continue scanning */
6fc6879b 508 ssid = wpa_s->conf->ssid;
ba2a573c 509 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
6fc6879b
JM
510 while (ssid) {
511 if (ssid == wpa_s->prev_scan_ssid) {
512 ssid = ssid->next;
513 break;
514 }
515 ssid = ssid->next;
516 }
517 }
6fc6879b 518
7dcdcfd6
JM
519 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
520 wpa_s->connect_without_scan = NULL;
e76baaac
JM
521 wpa_supplicant_assoc_try(wpa_s, ssid);
522 return;
523 } else if (wpa_s->conf->ap_scan == 2) {
6fc6879b 524 /*
ba2a573c
JM
525 * User-initiated scan request in ap_scan == 2; scan with
526 * wildcard SSID.
6fc6879b 527 */
e76baaac
JM
528 ssid = NULL;
529 } else {
5be45e2e 530 struct wpa_ssid *start = ssid, *tssid;
d3a98225 531 int freqs_set = 0;
e76baaac
JM
532 if (ssid == NULL && max_ssids > 1)
533 ssid = wpa_s->conf->ssid;
534 while (ssid) {
535 if (!ssid->disabled && ssid->scan_ssid) {
536 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
537 ssid->ssid, ssid->ssid_len);
538 params.ssids[params.num_ssids].ssid =
539 ssid->ssid;
540 params.ssids[params.num_ssids].ssid_len =
541 ssid->ssid_len;
542 params.num_ssids++;
543 if (params.num_ssids + 1 >= max_ssids)
544 break;
545 }
546 ssid = ssid->next;
547 if (ssid == start)
548 break;
549 if (ssid == NULL && max_ssids > 1 &&
550 start != wpa_s->conf->ssid)
551 ssid = wpa_s->conf->ssid;
6fc6879b 552 }
d3a98225 553
5be45e2e
JM
554 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
555 if (tssid->disabled)
d3a98225 556 continue;
5be45e2e 557 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
d3a98225 558 int_array_concat(&params.freqs,
5be45e2e 559 tssid->scan_freq);
d3a98225
JM
560 } else {
561 os_free(params.freqs);
562 params.freqs = NULL;
563 }
564 freqs_set = 1;
565 }
566 int_array_sort_unique(params.freqs);
6fc6879b
JM
567 }
568
6fc6879b 569 if (ssid) {
6fc6879b 570 wpa_s->prev_scan_ssid = ssid;
e76baaac 571 if (max_ssids > 1) {
f049052b
BG
572 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
573 "the scan request");
ba2a573c 574 params.num_ssids++;
e76baaac 575 }
f049052b
BG
576 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
577 "SSID(s)");
e76baaac 578 } else {
ba2a573c 579 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac 580 params.num_ssids++;
f049052b
BG
581 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
582 "SSID");
6fc6879b
JM
583 }
584
5f738a21 585 wpa_supplicant_optimize_freqs(wpa_s, &params);
46ee0427 586 extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
0e65037c 587
f47d639d 588 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
f049052b
BG
589 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
590 "generated frequency list");
f47d639d
JM
591 params.freqs = wpa_s->next_scan_freqs;
592 } else
593 os_free(wpa_s->next_scan_freqs);
594 wpa_s->next_scan_freqs = NULL;
595
3812464c
JM
596 params.filter_ssids = wpa_supplicant_build_filter_ssids(
597 wpa_s->conf, &params.num_filter_ssids);
46ee0427
JM
598 if (extra_ie) {
599 params.extra_ies = wpabuf_head(extra_ie);
600 params.extra_ies_len = wpabuf_len(extra_ie);
601 }
3812464c 602
d1dd48e3
JM
603#ifdef CONFIG_P2P
604 if (wpa_s->p2p_in_provisioning) {
605 /*
606 * The interface may not yet be in P2P mode, so we have to
607 * explicitly request P2P probe to disable CCK rates.
608 */
609 params.p2p_probe = 1;
610 }
611#endif /* CONFIG_P2P */
612
60b94c98 613 ret = wpa_supplicant_trigger_scan(wpa_s, &params);
6fc6879b 614
46ee0427 615 wpabuf_free(extra_ie);
d3a98225 616 os_free(params.freqs);
3812464c 617 os_free(params.filter_ssids);
ad08c363 618
6fc6879b 619 if (ret) {
f049052b 620 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
207ef3fb
JM
621 if (prev_state != wpa_s->wpa_state)
622 wpa_supplicant_set_state(wpa_s, prev_state);
1c4c9c50 623 wpa_supplicant_req_scan(wpa_s, 1, 0);
d902a9c1 624 }
6fc6879b
JM
625}
626
627
628/**
629 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
630 * @wpa_s: Pointer to wpa_supplicant data
631 * @sec: Number of seconds after which to scan
632 * @usec: Number of microseconds after which to scan
633 *
634 * This function is used to schedule a scan for neighboring access points after
635 * the specified time.
636 */
637void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
638{
7e148849
DW
639 /* If there's at least one network that should be specifically scanned
640 * then don't cancel the scan and reschedule. Some drivers do
641 * background scanning which generates frequent scan results, and that
642 * causes the specific SSID scan to get continually pushed back and
643 * never happen, which causes hidden APs to never get probe-scanned.
644 */
645 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
646 wpa_s->conf->ap_scan == 1) {
647 struct wpa_ssid *ssid = wpa_s->conf->ssid;
648
649 while (ssid) {
650 if (!ssid->disabled && ssid->scan_ssid)
651 break;
652 ssid = ssid->next;
653 }
654 if (ssid) {
f049052b 655 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
7e148849
DW
656 "ensure that specific SSID scans occur");
657 return;
658 }
659 }
660
f049052b 661 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
6fc6879b
JM
662 sec, usec);
663 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
664 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
665}
666
667
6a90053c
LC
668/**
669 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
670 * @wpa_s: Pointer to wpa_supplicant data
671 * @sec: Number of seconds after which to scan
672 * @usec: Number of microseconds after which to scan
673 *
674 * This function is used to schedule periodic scans for neighboring
675 * access points after the specified time.
676 */
677int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
678 int sec, int usec)
679{
680 if (!wpa_s->sched_scan_supported)
681 return -1;
682
683 eloop_register_timeout(sec, usec,
684 wpa_supplicant_delayed_sched_scan_timeout,
685 wpa_s, NULL);
686
687 return 0;
688}
689
690
cbdf3507
LC
691/**
692 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
693 * @wpa_s: Pointer to wpa_supplicant data
694 *
695 * This function is used to schedule periodic scans for neighboring
696 * access points repeating the scan continuously.
697 */
698int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
699{
700 struct wpa_driver_scan_params params;
701 enum wpa_states prev_state;
702 struct wpa_ssid *ssid;
703 struct wpabuf *wps_ie = NULL;
704 int ret;
cbdf3507 705 unsigned int max_sched_scan_ssids;
76a5249e 706 int wildcard = 0;
0b7a25c0 707 int need_ssids;
cbdf3507
LC
708
709 if (!wpa_s->sched_scan_supported)
710 return -1;
711
712 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
713 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
714 else
715 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
76a5249e
JM
716 if (max_sched_scan_ssids < 1)
717 return -1;
cbdf3507 718
1966e3d1
ES
719 if (wpa_s->sched_scanning) {
720 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
cbdf3507 721 return 0;
1966e3d1 722 }
cbdf3507 723
0b7a25c0
JM
724 need_ssids = 0;
725 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
726 if (!ssid->disabled && !ssid->scan_ssid) {
727 /* Use wildcard SSID to find this network */
728 wildcard = 1;
729 } else if (!ssid->disabled && ssid->ssid_len)
730 need_ssids++;
731 }
732 if (wildcard)
733 need_ssids++;
734
735 if (wpa_s->normal_scans < 3 &&
736 (need_ssids <= wpa_s->max_scan_ssids ||
737 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
738 /*
739 * When normal scan can speed up operations, use that for the
740 * first operations before starting the sched_scan to allow
741 * user space sleep more. We do this only if the normal scan
742 * has functionality that is suitable for this or if the
743 * sched_scan does not have better support for multiple SSIDs.
744 */
745 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
746 "sched_scan for initial scans (normal_scans=%d)",
747 wpa_s->normal_scans);
748 return -1;
749 }
750
cbdf3507
LC
751 os_memset(&params, 0, sizeof(params));
752
b59e6f26
LC
753 /* If we can't allocate space for the filters, we just don't filter */
754 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
755 sizeof(struct wpa_driver_scan_filter));
756
cbdf3507
LC
757 prev_state = wpa_s->wpa_state;
758 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
759 wpa_s->wpa_state == WPA_INACTIVE)
760 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
761
762 /* Find the starting point from which to continue scanning */
763 ssid = wpa_s->conf->ssid;
764 if (wpa_s->prev_sched_ssid) {
765 while (ssid) {
766 if (ssid == wpa_s->prev_sched_ssid) {
767 ssid = ssid->next;
768 break;
769 }
770 ssid = ssid->next;
771 }
772 }
773
774 if (!ssid || !wpa_s->prev_sched_ssid) {
775 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
776
ea5bae47 777 wpa_s->sched_scan_interval = 10;
cbdf3507
LC
778 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
779 wpa_s->first_sched_scan = 1;
780 ssid = wpa_s->conf->ssid;
781 wpa_s->prev_sched_ssid = ssid;
782 }
783
76a5249e
JM
784 if (wildcard) {
785 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
786 params.num_ssids++;
787 }
788
cbdf3507 789 while (ssid) {
5edddf41
JM
790 if (ssid->disabled)
791 goto next;
cbdf3507 792
fcd16847
JM
793 if (params.num_filter_ssids < wpa_s->max_match_sets &&
794 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1966e3d1
ES
795 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
796 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
b59e6f26
LC
797 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
798 ssid->ssid, ssid->ssid_len);
799 params.filter_ssids[params.num_filter_ssids].ssid_len =
800 ssid->ssid_len;
801 params.num_filter_ssids++;
86b47aaf
JM
802 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
803 {
804 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
805 "filter for sched_scan - drop filter");
806 os_free(params.filter_ssids);
807 params.filter_ssids = NULL;
808 params.num_filter_ssids = 0;
b59e6f26
LC
809 }
810
3f56f3a4 811 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
76a5249e
JM
812 if (params.num_ssids == max_sched_scan_ssids)
813 break; /* only room for broadcast SSID */
3f56f3a4
JM
814 wpa_dbg(wpa_s, MSG_DEBUG,
815 "add to active scan ssid: %s",
1966e3d1 816 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
cbdf3507
LC
817 params.ssids[params.num_ssids].ssid =
818 ssid->ssid;
819 params.ssids[params.num_ssids].ssid_len =
820 ssid->ssid_len;
821 params.num_ssids++;
b59e6f26 822 if (params.num_ssids >= max_sched_scan_ssids) {
cbdf3507
LC
823 wpa_s->prev_sched_ssid = ssid;
824 break;
825 }
826 }
b59e6f26 827
5edddf41 828 next:
cbdf3507
LC
829 wpa_s->prev_sched_ssid = ssid;
830 ssid = ssid->next;
831 }
832
7c6a266c
JM
833 if (params.num_filter_ssids == 0) {
834 os_free(params.filter_ssids);
835 params.filter_ssids = NULL;
836 }
837
cbdf3507
LC
838 if (wpa_s->wps)
839 wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
840
a8cb5a88
JM
841 if (ssid || !wpa_s->first_sched_scan) {
842 wpa_dbg(wpa_s, MSG_DEBUG,
843 "Starting sched scan: interval %d (no timeout)",
844 wpa_s->sched_scan_interval);
845 } else {
846 wpa_dbg(wpa_s, MSG_DEBUG,
847 "Starting sched scan: interval %d timeout %d",
848 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
849 }
cbdf3507
LC
850
851 ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
852 wpa_s->sched_scan_interval);
853 wpabuf_free(wps_ie);
b59e6f26 854 os_free(params.filter_ssids);
cbdf3507
LC
855 if (ret) {
856 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
857 if (prev_state != wpa_s->wpa_state)
858 wpa_supplicant_set_state(wpa_s, prev_state);
859 return ret;
860 }
861
862 /* If we have more SSIDs to scan, add a timeout so we scan them too */
863 if (ssid || !wpa_s->first_sched_scan) {
864 wpa_s->sched_scan_timed_out = 0;
865 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
866 wpa_supplicant_sched_scan_timeout,
867 wpa_s, NULL);
868 wpa_s->first_sched_scan = 0;
869 wpa_s->sched_scan_timeout /= 2;
870 wpa_s->sched_scan_interval *= 2;
871 }
872
873 return 0;
874}
875
876
6fc6879b
JM
877/**
878 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
879 * @wpa_s: Pointer to wpa_supplicant data
880 *
881 * This function is used to cancel a scan request scheduled with
882 * wpa_supplicant_req_scan().
883 */
884void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
885{
f049052b 886 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
6fc6879b
JM
887 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
888}
cb8564b1
DW
889
890
cbdf3507
LC
891/**
892 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
893 * @wpa_s: Pointer to wpa_supplicant data
894 *
895 * This function is used to stop a periodic scheduled scan.
896 */
897void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
898{
899 if (!wpa_s->sched_scanning)
900 return;
901
902 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
903 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
904 wpa_supplicant_stop_sched_scan(wpa_s);
905}
906
907
cb8564b1
DW
908void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
909 int scanning)
910{
911 if (wpa_s->scanning != scanning) {
912 wpa_s->scanning = scanning;
8bac466b 913 wpas_notify_scanning(wpa_s);
cb8564b1
DW
914 }
915}
916
9ba9fa07
JM
917
918static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
919{
920 int rate = 0;
921 const u8 *ie;
922 int i;
923
924 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
925 for (i = 0; ie && i < ie[1]; i++) {
926 if ((ie[i + 2] & 0x7f) > rate)
927 rate = ie[i + 2] & 0x7f;
928 }
929
930 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
931 for (i = 0; ie && i < ie[1]; i++) {
932 if ((ie[i + 2] & 0x7f) > rate)
933 rate = ie[i + 2] & 0x7f;
934 }
935
936 return rate;
937}
938
939
d1f9c410
JM
940const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
941{
942 const u8 *end, *pos;
943
944 pos = (const u8 *) (res + 1);
945 end = pos + res->ie_len;
946
947 while (pos + 1 < end) {
948 if (pos + 2 + pos[1] > end)
949 break;
950 if (pos[0] == ie)
951 return pos;
952 pos += 2 + pos[1];
953 }
954
955 return NULL;
956}
957
958
9ba9fa07
JM
959const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
960 u32 vendor_type)
961{
962 const u8 *end, *pos;
963
964 pos = (const u8 *) (res + 1);
965 end = pos + res->ie_len;
966
967 while (pos + 1 < end) {
968 if (pos + 2 + pos[1] > end)
969 break;
970 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
971 vendor_type == WPA_GET_BE32(&pos[2]))
972 return pos;
973 pos += 2 + pos[1];
974 }
975
976 return NULL;
977}
978
979
980struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
981 u32 vendor_type)
982{
983 struct wpabuf *buf;
984 const u8 *end, *pos;
985
986 buf = wpabuf_alloc(res->ie_len);
987 if (buf == NULL)
988 return NULL;
989
990 pos = (const u8 *) (res + 1);
991 end = pos + res->ie_len;
992
54f489be
JM
993 while (pos + 1 < end) {
994 if (pos + 2 + pos[1] > end)
995 break;
996 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
997 vendor_type == WPA_GET_BE32(&pos[2]))
998 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
999 pos += 2 + pos[1];
1000 }
1001
1002 if (wpabuf_len(buf) == 0) {
1003 wpabuf_free(buf);
1004 buf = NULL;
1005 }
1006
1007 return buf;
1008}
1009
1010
1011struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1012 const struct wpa_scan_res *res, u32 vendor_type)
1013{
1014 struct wpabuf *buf;
1015 const u8 *end, *pos;
1016
1017 if (res->beacon_ie_len == 0)
1018 return NULL;
1019 buf = wpabuf_alloc(res->beacon_ie_len);
1020 if (buf == NULL)
1021 return NULL;
1022
1023 pos = (const u8 *) (res + 1);
1024 pos += res->ie_len;
1025 end = pos + res->beacon_ie_len;
1026
9ba9fa07
JM
1027 while (pos + 1 < end) {
1028 if (pos + 2 + pos[1] > end)
1029 break;
1030 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1031 vendor_type == WPA_GET_BE32(&pos[2]))
1032 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1033 pos += 2 + pos[1];
1034 }
1035
1036 if (wpabuf_len(buf) == 0) {
1037 wpabuf_free(buf);
1038 buf = NULL;
1039 }
1040
1041 return buf;
1042}
1043
1044
577db0ae
GM
1045/*
1046 * Channels with a great SNR can operate at full rate. What is a great SNR?
1047 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1048 * rule of thumb is that any SNR above 20 is good." This one
1049 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1050 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1051 * conservative value.
1052 */
1053#define GREAT_SNR 30
1054
9ba9fa07
JM
1055/* Compare function for sorting scan results. Return >0 if @b is considered
1056 * better. */
1057static int wpa_scan_result_compar(const void *a, const void *b)
1058{
577db0ae
GM
1059#define IS_5GHZ(n) (n > 4000)
1060#define MIN(a,b) a < b ? a : b
9ba9fa07
JM
1061 struct wpa_scan_res **_wa = (void *) a;
1062 struct wpa_scan_res **_wb = (void *) b;
1063 struct wpa_scan_res *wa = *_wa;
1064 struct wpa_scan_res *wb = *_wb;
1065 int wpa_a, wpa_b, maxrate_a, maxrate_b;
577db0ae 1066 int snr_a, snr_b;
9ba9fa07
JM
1067
1068 /* WPA/WPA2 support preferred */
1069 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1070 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1071 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1072 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1073
1074 if (wpa_b && !wpa_a)
1075 return 1;
1076 if (!wpa_b && wpa_a)
1077 return -1;
1078
1079 /* privacy support preferred */
1080 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1081 (wb->caps & IEEE80211_CAP_PRIVACY))
1082 return 1;
1083 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1084 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1085 return -1;
1086
577db0ae
GM
1087 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1088 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1089 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1090 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1091 } else {
1092 /* Not suitable information to calculate SNR, so use level */
1093 snr_a = wa->level;
1094 snr_b = wb->level;
1095 }
1096
577db0ae
GM
1097 /* best/max rate preferred if SNR close enough */
1098 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
9ba9fa07
JM
1099 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1100 maxrate_a = wpa_scan_get_max_rate(wa);
1101 maxrate_b = wpa_scan_get_max_rate(wb);
1102 if (maxrate_a != maxrate_b)
1103 return maxrate_b - maxrate_a;
577db0ae
GM
1104 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1105 return IS_5GHZ(wa->freq) ? -1 : 1;
9ba9fa07
JM
1106 }
1107
1108 /* use freq for channel preference */
1109
577db0ae 1110 /* all things being equal, use SNR; if SNRs are
9ba9fa07
JM
1111 * identical, use quality values since some drivers may only report
1112 * that value and leave the signal level zero */
577db0ae 1113 if (snr_b == snr_a)
9ba9fa07 1114 return wb->qual - wa->qual;
577db0ae
GM
1115 return snr_b - snr_a;
1116#undef MIN
1117#undef IS_5GHZ
9ba9fa07
JM
1118}
1119
1120
41e650ae
JM
1121#ifdef CONFIG_WPS
1122/* Compare function for sorting scan results when searching a WPS AP for
1123 * provisioning. Return >0 if @b is considered better. */
1124static int wpa_scan_result_wps_compar(const void *a, const void *b)
1125{
1126 struct wpa_scan_res **_wa = (void *) a;
1127 struct wpa_scan_res **_wb = (void *) b;
1128 struct wpa_scan_res *wa = *_wa;
1129 struct wpa_scan_res *wb = *_wb;
1130 int uses_wps_a, uses_wps_b;
1131 struct wpabuf *wps_a, *wps_b;
1132 int res;
1133
1134 /* Optimization - check WPS IE existence before allocated memory and
1135 * doing full reassembly. */
1136 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1137 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1138 if (uses_wps_a && !uses_wps_b)
1139 return -1;
1140 if (!uses_wps_a && uses_wps_b)
1141 return 1;
1142
1143 if (uses_wps_a && uses_wps_b) {
1144 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1145 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1146 res = wps_ap_priority_compar(wps_a, wps_b);
1147 wpabuf_free(wps_a);
1148 wpabuf_free(wps_b);
1149 if (res)
1150 return res;
1151 }
1152
1153 /*
1154 * Do not use current AP security policy as a sorting criteria during
1155 * WPS provisioning step since the AP may get reconfigured at the
1156 * completion of provisioning.
1157 */
1158
1159 /* all things being equal, use signal level; if signal levels are
1160 * identical, use quality values since some drivers may only report
1161 * that value and leave the signal level zero */
1162 if (wb->level == wa->level)
1163 return wb->qual - wa->qual;
1164 return wb->level - wa->level;
1165}
1166#endif /* CONFIG_WPS */
1167
1168
aa820e02
JM
1169static void dump_scan_res(struct wpa_scan_results *scan_res)
1170{
76202aed 1171#ifndef CONFIG_NO_STDOUT_DEBUG
aa820e02
JM
1172 size_t i;
1173
1174 if (scan_res->res == NULL || scan_res->num == 0)
1175 return;
1176
1177 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1178
1179 for (i = 0; i < scan_res->num; i++) {
1180 struct wpa_scan_res *r = scan_res->res[i];
1181 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1182 == WPA_SCAN_LEVEL_DBM) {
1183 int snr = r->level - r->noise;
1184 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1185 "noise=%d level=%d snr=%d%s flags=0x%x",
1186 MAC2STR(r->bssid), r->freq, r->qual,
1187 r->noise, r->level, snr,
1188 snr >= GREAT_SNR ? "*" : "", r->flags);
1189 } else {
1190 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1191 "noise=%d level=%d flags=0x%x",
1192 MAC2STR(r->bssid), r->freq, r->qual,
1193 r->noise, r->level, r->flags);
1194 }
1195 }
76202aed 1196#endif /* CONFIG_NO_STDOUT_DEBUG */
aa820e02
JM
1197}
1198
1199
9ba9fa07
JM
1200/**
1201 * wpa_supplicant_get_scan_results - Get scan results
1202 * @wpa_s: Pointer to wpa_supplicant data
1203 * @info: Information about what was scanned or %NULL if not available
1204 * @new_scan: Whether a new scan was performed
1205 * Returns: Scan results, %NULL on failure
1206 *
1207 * This function request the current scan results from the driver and updates
1208 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1209 * results with wpa_scan_results_free().
1210 */
1211struct wpa_scan_results *
1212wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1213 struct scan_info *info, int new_scan)
1214{
1215 struct wpa_scan_results *scan_res;
1216 size_t i;
41e650ae 1217 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
9ba9fa07 1218
17fbb751 1219 scan_res = wpa_drv_get_scan_results2(wpa_s);
9ba9fa07 1220 if (scan_res == NULL) {
f049052b 1221 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
9ba9fa07
JM
1222 return NULL;
1223 }
1224
41e650ae
JM
1225#ifdef CONFIG_WPS
1226 if (wpas_wps_in_progress(wpa_s)) {
f049052b
BG
1227 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1228 "provisioning rules");
41e650ae
JM
1229 compar = wpa_scan_result_wps_compar;
1230 }
1231#endif /* CONFIG_WPS */
1232
9ba9fa07 1233 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
41e650ae 1234 compar);
aa820e02 1235 dump_scan_res(scan_res);
9ba9fa07
JM
1236
1237 wpa_bss_update_start(wpa_s);
1238 for (i = 0; i < scan_res->num; i++)
1239 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1240 wpa_bss_update_end(wpa_s, info, new_scan);
1241
1242 return scan_res;
1243}
1244
1245
1246int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1247{
1248 struct wpa_scan_results *scan_res;
1249 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1250 if (scan_res == NULL)
1251 return -1;
1252 wpa_scan_results_free(scan_res);
1253
1254 return 0;
1255}