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