]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/scan.c
Fix adding extra IEs in sched scan
[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_supplicant *wpa_s)
84 {
85 struct wpa_ssid *ssid = wpa_s->conf->ssid;
86 int count = 0;
87 while (ssid) {
88 if (!wpas_network_disabled(wpa_s, 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(wpa_s, 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_array(*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 * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
384 {
385 struct wpabuf *extra_ie = NULL;
386 #ifdef CONFIG_WPS
387 int wps = 0;
388 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
389 #endif /* CONFIG_WPS */
390
391 #ifdef CONFIG_INTERWORKING
392 if (wpa_s->conf->interworking &&
393 wpabuf_resize(&extra_ie, 100) == 0)
394 wpas_add_interworking_elements(wpa_s, extra_ie);
395 #endif /* CONFIG_INTERWORKING */
396
397 #ifdef CONFIG_WPS
398 wps = wpas_wps_in_use(wpa_s, &req_type);
399
400 if (wps) {
401 struct wpabuf *wps_ie;
402 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
403 DEV_PW_DEFAULT,
404 &wpa_s->wps->dev,
405 wpa_s->wps->uuid, req_type,
406 0, NULL);
407 if (wps_ie) {
408 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
409 wpabuf_put_buf(extra_ie, wps_ie);
410 wpabuf_free(wps_ie);
411 }
412 }
413
414 #ifdef CONFIG_P2P
415 if (wps) {
416 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
417 if (wpabuf_resize(&extra_ie, ielen) == 0)
418 wpas_p2p_scan_ie(wpa_s, extra_ie);
419 }
420 #endif /* CONFIG_P2P */
421
422 #endif /* CONFIG_WPS */
423
424 return extra_ie;
425 }
426
427
428 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
429 {
430 struct wpa_supplicant *wpa_s = eloop_ctx;
431 struct wpa_ssid *ssid;
432 int scan_req = 0, ret;
433 struct wpabuf *extra_ie = NULL;
434 struct wpa_driver_scan_params params;
435 struct wpa_driver_scan_params *scan_params;
436 size_t max_ssids;
437 enum wpa_states prev_state;
438
439 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
440 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
441 return;
442 }
443
444 if (wpa_s->disconnected && !wpa_s->scan_req) {
445 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
446 return;
447 }
448
449 if (!wpa_supplicant_enabled_networks(wpa_s) &&
450 !wpa_s->scan_req) {
451 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
452 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
453 return;
454 }
455
456 if (wpa_s->conf->ap_scan != 0 &&
457 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
458 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
459 "overriding ap_scan configuration");
460 wpa_s->conf->ap_scan = 0;
461 wpas_notify_ap_scan_changed(wpa_s);
462 }
463
464 if (wpa_s->conf->ap_scan == 0) {
465 wpa_supplicant_gen_assoc_event(wpa_s);
466 return;
467 }
468
469 #ifdef CONFIG_P2P
470 if (wpas_p2p_in_progress(wpa_s)) {
471 if (wpa_s->wpa_state == WPA_SCANNING) {
472 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
473 "while P2P operation is in progress");
474 wpa_supplicant_req_scan(wpa_s, 5, 0);
475 } else {
476 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
477 "P2P operation is in progress");
478 }
479 return;
480 }
481 #endif /* CONFIG_P2P */
482
483 if (wpa_s->conf->ap_scan == 2)
484 max_ssids = 1;
485 else {
486 max_ssids = wpa_s->max_scan_ssids;
487 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
488 max_ssids = WPAS_MAX_SCAN_SSIDS;
489 }
490
491 scan_req = wpa_s->scan_req;
492 wpa_s->scan_req = 0;
493
494 os_memset(&params, 0, sizeof(params));
495
496 prev_state = wpa_s->wpa_state;
497 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
498 wpa_s->wpa_state == WPA_INACTIVE)
499 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
500
501 /*
502 * If autoscan has set its own scanning parameters
503 */
504 if (wpa_s->autoscan_params != NULL) {
505 scan_params = wpa_s->autoscan_params;
506 goto scan;
507 }
508
509 if (scan_req != 2 && wpa_s->connect_without_scan) {
510 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
511 if (ssid == wpa_s->connect_without_scan)
512 break;
513 }
514 wpa_s->connect_without_scan = NULL;
515 if (ssid) {
516 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
517 "without scan step");
518 wpa_supplicant_associate(wpa_s, NULL, ssid);
519 return;
520 }
521 }
522
523 #ifdef CONFIG_P2P
524 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
525 wpa_s->go_params) {
526 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
527 "P2P group formation");
528 params.ssids[0].ssid = wpa_s->go_params->ssid;
529 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
530 params.num_ssids = 1;
531 goto ssid_list_set;
532 }
533 #endif /* CONFIG_P2P */
534
535 /* Find the starting point from which to continue scanning */
536 ssid = wpa_s->conf->ssid;
537 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
538 while (ssid) {
539 if (ssid == wpa_s->prev_scan_ssid) {
540 ssid = ssid->next;
541 break;
542 }
543 ssid = ssid->next;
544 }
545 }
546
547 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
548 wpa_s->connect_without_scan = NULL;
549 wpa_s->prev_scan_wildcard = 0;
550 wpa_supplicant_assoc_try(wpa_s, ssid);
551 return;
552 } else if (wpa_s->conf->ap_scan == 2) {
553 /*
554 * User-initiated scan request in ap_scan == 2; scan with
555 * wildcard SSID.
556 */
557 ssid = NULL;
558 } else {
559 struct wpa_ssid *start = ssid, *tssid;
560 int freqs_set = 0;
561 if (ssid == NULL && max_ssids > 1)
562 ssid = wpa_s->conf->ssid;
563 while (ssid) {
564 if (!wpas_network_disabled(wpa_s, ssid) &&
565 ssid->scan_ssid) {
566 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
567 ssid->ssid, ssid->ssid_len);
568 params.ssids[params.num_ssids].ssid =
569 ssid->ssid;
570 params.ssids[params.num_ssids].ssid_len =
571 ssid->ssid_len;
572 params.num_ssids++;
573 if (params.num_ssids + 1 >= max_ssids)
574 break;
575 }
576 ssid = ssid->next;
577 if (ssid == start)
578 break;
579 if (ssid == NULL && max_ssids > 1 &&
580 start != wpa_s->conf->ssid)
581 ssid = wpa_s->conf->ssid;
582 }
583
584 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
585 if (wpas_network_disabled(wpa_s, tssid))
586 continue;
587 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
588 int_array_concat(&params.freqs,
589 tssid->scan_freq);
590 } else {
591 os_free(params.freqs);
592 params.freqs = NULL;
593 }
594 freqs_set = 1;
595 }
596 int_array_sort_unique(params.freqs);
597 }
598
599 if (ssid && max_ssids == 1) {
600 /*
601 * If the driver is limited to 1 SSID at a time interleave
602 * wildcard SSID scans with specific SSID scans to avoid
603 * waiting a long time for a wildcard scan.
604 */
605 if (!wpa_s->prev_scan_wildcard) {
606 params.ssids[0].ssid = NULL;
607 params.ssids[0].ssid_len = 0;
608 wpa_s->prev_scan_wildcard = 1;
609 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
610 "wildcard SSID (Interleave with specific)");
611 } else {
612 wpa_s->prev_scan_ssid = ssid;
613 wpa_s->prev_scan_wildcard = 0;
614 wpa_dbg(wpa_s, MSG_DEBUG,
615 "Starting AP scan for specific SSID: %s",
616 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
617 }
618 } else if (ssid) {
619 /* max_ssids > 1 */
620
621 wpa_s->prev_scan_ssid = ssid;
622 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
623 "the scan request");
624 params.num_ssids++;
625 } else {
626 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
627 params.num_ssids++;
628 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
629 "SSID");
630 }
631 #ifdef CONFIG_P2P
632 ssid_list_set:
633 #endif /* CONFIG_P2P */
634
635 wpa_supplicant_optimize_freqs(wpa_s, &params);
636 extra_ie = wpa_supplicant_extra_ies(wpa_s);
637
638 #ifdef CONFIG_HS20
639 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
640 wpas_hs20_add_indication(extra_ie);
641 #endif /* CONFIG_HS20 */
642
643 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
644 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
645 "generated frequency list");
646 params.freqs = wpa_s->next_scan_freqs;
647 } else
648 os_free(wpa_s->next_scan_freqs);
649 wpa_s->next_scan_freqs = NULL;
650
651 params.filter_ssids = wpa_supplicant_build_filter_ssids(
652 wpa_s->conf, &params.num_filter_ssids);
653 if (extra_ie) {
654 params.extra_ies = wpabuf_head(extra_ie);
655 params.extra_ies_len = wpabuf_len(extra_ie);
656 }
657
658 #ifdef CONFIG_P2P
659 if (wpa_s->p2p_in_provisioning ||
660 (wpa_s->show_group_started && wpa_s->go_params)) {
661 /*
662 * The interface may not yet be in P2P mode, so we have to
663 * explicitly request P2P probe to disable CCK rates.
664 */
665 params.p2p_probe = 1;
666 }
667 #endif /* CONFIG_P2P */
668
669 scan_params = &params;
670
671 scan:
672 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
673
674 wpabuf_free(extra_ie);
675 os_free(params.freqs);
676 os_free(params.filter_ssids);
677
678 if (ret) {
679 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
680 if (prev_state != wpa_s->wpa_state)
681 wpa_supplicant_set_state(wpa_s, prev_state);
682 wpa_supplicant_req_scan(wpa_s, 1, 0);
683 }
684 }
685
686
687 /**
688 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
689 * @wpa_s: Pointer to wpa_supplicant data
690 * @sec: Number of seconds after which to scan
691 * @usec: Number of microseconds after which to scan
692 *
693 * This function is used to schedule a scan for neighboring access points after
694 * the specified time.
695 */
696 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
697 {
698 /* If there's at least one network that should be specifically scanned
699 * then don't cancel the scan and reschedule. Some drivers do
700 * background scanning which generates frequent scan results, and that
701 * causes the specific SSID scan to get continually pushed back and
702 * never happen, which causes hidden APs to never get probe-scanned.
703 */
704 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
705 wpa_s->conf->ap_scan == 1) {
706 struct wpa_ssid *ssid = wpa_s->conf->ssid;
707
708 while (ssid) {
709 if (!wpas_network_disabled(wpa_s, ssid) &&
710 ssid->scan_ssid)
711 break;
712 ssid = ssid->next;
713 }
714 if (ssid) {
715 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
716 "ensure that specific SSID scans occur");
717 return;
718 }
719 }
720
721 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
722 sec, usec);
723 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
724 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
725 }
726
727
728 /**
729 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
730 * @wpa_s: Pointer to wpa_supplicant data
731 * @sec: Number of seconds after which to scan
732 * @usec: Number of microseconds after which to scan
733 *
734 * This function is used to schedule periodic scans for neighboring
735 * access points after the specified time.
736 */
737 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
738 int sec, int usec)
739 {
740 if (!wpa_s->sched_scan_supported)
741 return -1;
742
743 eloop_register_timeout(sec, usec,
744 wpa_supplicant_delayed_sched_scan_timeout,
745 wpa_s, NULL);
746
747 return 0;
748 }
749
750
751 /**
752 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
753 * @wpa_s: Pointer to wpa_supplicant data
754 *
755 * This function is used to schedule periodic scans for neighboring
756 * access points repeating the scan continuously.
757 */
758 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
759 {
760 struct wpa_driver_scan_params params;
761 struct wpa_driver_scan_params *scan_params;
762 enum wpa_states prev_state;
763 struct wpa_ssid *ssid = NULL;
764 struct wpabuf *extra_ie = NULL;
765 int ret;
766 unsigned int max_sched_scan_ssids;
767 int wildcard = 0;
768 int need_ssids;
769
770 if (!wpa_s->sched_scan_supported)
771 return -1;
772
773 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
774 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
775 else
776 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
777 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
778 return -1;
779
780 if (wpa_s->sched_scanning) {
781 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
782 return 0;
783 }
784
785 need_ssids = 0;
786 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
787 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
788 /* Use wildcard SSID to find this network */
789 wildcard = 1;
790 } else if (!wpas_network_disabled(wpa_s, ssid) &&
791 ssid->ssid_len)
792 need_ssids++;
793
794 #ifdef CONFIG_WPS
795 if (!wpas_network_disabled(wpa_s, ssid) &&
796 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
797 /*
798 * Normal scan is more reliable and faster for WPS
799 * operations and since these are for short periods of
800 * time, the benefit of trying to use sched_scan would
801 * be limited.
802 */
803 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
804 "sched_scan for WPS");
805 return -1;
806 }
807 #endif /* CONFIG_WPS */
808 }
809 if (wildcard)
810 need_ssids++;
811
812 if (wpa_s->normal_scans < 3 &&
813 (need_ssids <= wpa_s->max_scan_ssids ||
814 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
815 /*
816 * When normal scan can speed up operations, use that for the
817 * first operations before starting the sched_scan to allow
818 * user space sleep more. We do this only if the normal scan
819 * has functionality that is suitable for this or if the
820 * sched_scan does not have better support for multiple SSIDs.
821 */
822 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
823 "sched_scan for initial scans (normal_scans=%d)",
824 wpa_s->normal_scans);
825 return -1;
826 }
827
828 os_memset(&params, 0, sizeof(params));
829
830 /* If we can't allocate space for the filters, we just don't filter */
831 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
832 sizeof(struct wpa_driver_scan_filter));
833
834 prev_state = wpa_s->wpa_state;
835 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
836 wpa_s->wpa_state == WPA_INACTIVE)
837 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
838
839 if (wpa_s->autoscan_params != NULL) {
840 scan_params = wpa_s->autoscan_params;
841 goto scan;
842 }
843
844 /* Find the starting point from which to continue scanning */
845 ssid = wpa_s->conf->ssid;
846 if (wpa_s->prev_sched_ssid) {
847 while (ssid) {
848 if (ssid == wpa_s->prev_sched_ssid) {
849 ssid = ssid->next;
850 break;
851 }
852 ssid = ssid->next;
853 }
854 }
855
856 if (!ssid || !wpa_s->prev_sched_ssid) {
857 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
858
859 if (wpa_s->sched_scan_interval == 0)
860 wpa_s->sched_scan_interval = 10;
861 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
862 wpa_s->first_sched_scan = 1;
863 ssid = wpa_s->conf->ssid;
864 wpa_s->prev_sched_ssid = ssid;
865 }
866
867 if (wildcard) {
868 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
869 params.num_ssids++;
870 }
871
872 while (ssid) {
873 if (wpas_network_disabled(wpa_s, ssid))
874 goto next;
875
876 if (params.num_filter_ssids < wpa_s->max_match_sets &&
877 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
878 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
879 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
880 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
881 ssid->ssid, ssid->ssid_len);
882 params.filter_ssids[params.num_filter_ssids].ssid_len =
883 ssid->ssid_len;
884 params.num_filter_ssids++;
885 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
886 {
887 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
888 "filter for sched_scan - drop filter");
889 os_free(params.filter_ssids);
890 params.filter_ssids = NULL;
891 params.num_filter_ssids = 0;
892 }
893
894 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
895 if (params.num_ssids == max_sched_scan_ssids)
896 break; /* only room for broadcast SSID */
897 wpa_dbg(wpa_s, MSG_DEBUG,
898 "add to active scan ssid: %s",
899 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
900 params.ssids[params.num_ssids].ssid =
901 ssid->ssid;
902 params.ssids[params.num_ssids].ssid_len =
903 ssid->ssid_len;
904 params.num_ssids++;
905 if (params.num_ssids >= max_sched_scan_ssids) {
906 wpa_s->prev_sched_ssid = ssid;
907 do {
908 ssid = ssid->next;
909 } while (ssid &&
910 (wpas_network_disabled(wpa_s, ssid) ||
911 !ssid->scan_ssid));
912 break;
913 }
914 }
915
916 next:
917 wpa_s->prev_sched_ssid = ssid;
918 ssid = ssid->next;
919 }
920
921 if (params.num_filter_ssids == 0) {
922 os_free(params.filter_ssids);
923 params.filter_ssids = NULL;
924 }
925
926 extra_ie = wpa_supplicant_extra_ies(wpa_s);
927 if (extra_ie) {
928 params.extra_ies = wpabuf_head(extra_ie);
929 params.extra_ies_len = wpabuf_len(extra_ie);
930 }
931
932 scan_params = &params;
933
934 scan:
935 if (ssid || !wpa_s->first_sched_scan) {
936 wpa_dbg(wpa_s, MSG_DEBUG,
937 "Starting sched scan: interval %d timeout %d",
938 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
939 } else {
940 wpa_dbg(wpa_s, MSG_DEBUG,
941 "Starting sched scan: interval %d (no timeout)",
942 wpa_s->sched_scan_interval);
943 }
944
945 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
946 wpa_s->sched_scan_interval);
947 wpabuf_free(extra_ie);
948 os_free(params.filter_ssids);
949 if (ret) {
950 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
951 if (prev_state != wpa_s->wpa_state)
952 wpa_supplicant_set_state(wpa_s, prev_state);
953 return ret;
954 }
955
956 /* If we have more SSIDs to scan, add a timeout so we scan them too */
957 if (ssid || !wpa_s->first_sched_scan) {
958 wpa_s->sched_scan_timed_out = 0;
959 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
960 wpa_supplicant_sched_scan_timeout,
961 wpa_s, NULL);
962 wpa_s->first_sched_scan = 0;
963 wpa_s->sched_scan_timeout /= 2;
964 wpa_s->sched_scan_interval *= 2;
965 }
966
967 return 0;
968 }
969
970
971 /**
972 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
973 * @wpa_s: Pointer to wpa_supplicant data
974 *
975 * This function is used to cancel a scan request scheduled with
976 * wpa_supplicant_req_scan().
977 */
978 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
979 {
980 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
981 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
982 }
983
984
985 /**
986 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
987 * @wpa_s: Pointer to wpa_supplicant data
988 *
989 * This function is used to stop a periodic scheduled scan.
990 */
991 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
992 {
993 if (!wpa_s->sched_scanning)
994 return;
995
996 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
997 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
998 wpa_supplicant_stop_sched_scan(wpa_s);
999 }
1000
1001
1002 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1003 int scanning)
1004 {
1005 if (wpa_s->scanning != scanning) {
1006 wpa_s->scanning = scanning;
1007 wpas_notify_scanning(wpa_s);
1008 }
1009 }
1010
1011
1012 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1013 {
1014 int rate = 0;
1015 const u8 *ie;
1016 int i;
1017
1018 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1019 for (i = 0; ie && i < ie[1]; i++) {
1020 if ((ie[i + 2] & 0x7f) > rate)
1021 rate = ie[i + 2] & 0x7f;
1022 }
1023
1024 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1025 for (i = 0; ie && i < ie[1]; i++) {
1026 if ((ie[i + 2] & 0x7f) > rate)
1027 rate = ie[i + 2] & 0x7f;
1028 }
1029
1030 return rate;
1031 }
1032
1033
1034 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1035 {
1036 const u8 *end, *pos;
1037
1038 pos = (const u8 *) (res + 1);
1039 end = pos + res->ie_len;
1040
1041 while (pos + 1 < end) {
1042 if (pos + 2 + pos[1] > end)
1043 break;
1044 if (pos[0] == ie)
1045 return pos;
1046 pos += 2 + pos[1];
1047 }
1048
1049 return NULL;
1050 }
1051
1052
1053 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1054 u32 vendor_type)
1055 {
1056 const u8 *end, *pos;
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 return pos;
1067 pos += 2 + pos[1];
1068 }
1069
1070 return NULL;
1071 }
1072
1073
1074 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1075 u32 vendor_type)
1076 {
1077 struct wpabuf *buf;
1078 const u8 *end, *pos;
1079
1080 buf = wpabuf_alloc(res->ie_len);
1081 if (buf == NULL)
1082 return NULL;
1083
1084 pos = (const u8 *) (res + 1);
1085 end = pos + res->ie_len;
1086
1087 while (pos + 1 < end) {
1088 if (pos + 2 + pos[1] > end)
1089 break;
1090 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1091 vendor_type == WPA_GET_BE32(&pos[2]))
1092 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1093 pos += 2 + pos[1];
1094 }
1095
1096 if (wpabuf_len(buf) == 0) {
1097 wpabuf_free(buf);
1098 buf = NULL;
1099 }
1100
1101 return buf;
1102 }
1103
1104
1105 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1106 const struct wpa_scan_res *res, u32 vendor_type)
1107 {
1108 struct wpabuf *buf;
1109 const u8 *end, *pos;
1110
1111 if (res->beacon_ie_len == 0)
1112 return NULL;
1113 buf = wpabuf_alloc(res->beacon_ie_len);
1114 if (buf == NULL)
1115 return NULL;
1116
1117 pos = (const u8 *) (res + 1);
1118 pos += res->ie_len;
1119 end = pos + res->beacon_ie_len;
1120
1121 while (pos + 1 < end) {
1122 if (pos + 2 + pos[1] > end)
1123 break;
1124 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1125 vendor_type == WPA_GET_BE32(&pos[2]))
1126 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1127 pos += 2 + pos[1];
1128 }
1129
1130 if (wpabuf_len(buf) == 0) {
1131 wpabuf_free(buf);
1132 buf = NULL;
1133 }
1134
1135 return buf;
1136 }
1137
1138
1139 /*
1140 * Channels with a great SNR can operate at full rate. What is a great SNR?
1141 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1142 * rule of thumb is that any SNR above 20 is good." This one
1143 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1144 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1145 * conservative value.
1146 */
1147 #define GREAT_SNR 30
1148
1149 /* Compare function for sorting scan results. Return >0 if @b is considered
1150 * better. */
1151 static int wpa_scan_result_compar(const void *a, const void *b)
1152 {
1153 #define IS_5GHZ(n) (n > 4000)
1154 #define MIN(a,b) a < b ? a : b
1155 struct wpa_scan_res **_wa = (void *) a;
1156 struct wpa_scan_res **_wb = (void *) b;
1157 struct wpa_scan_res *wa = *_wa;
1158 struct wpa_scan_res *wb = *_wb;
1159 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1160 int snr_a, snr_b;
1161
1162 /* WPA/WPA2 support preferred */
1163 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1164 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1165 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1166 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1167
1168 if (wpa_b && !wpa_a)
1169 return 1;
1170 if (!wpa_b && wpa_a)
1171 return -1;
1172
1173 /* privacy support preferred */
1174 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1175 (wb->caps & IEEE80211_CAP_PRIVACY))
1176 return 1;
1177 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1178 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1179 return -1;
1180
1181 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1182 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1183 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1184 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1185 } else {
1186 /* Not suitable information to calculate SNR, so use level */
1187 snr_a = wa->level;
1188 snr_b = wb->level;
1189 }
1190
1191 /* best/max rate preferred if SNR close enough */
1192 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1193 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1194 maxrate_a = wpa_scan_get_max_rate(wa);
1195 maxrate_b = wpa_scan_get_max_rate(wb);
1196 if (maxrate_a != maxrate_b)
1197 return maxrate_b - maxrate_a;
1198 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1199 return IS_5GHZ(wa->freq) ? -1 : 1;
1200 }
1201
1202 /* use freq for channel preference */
1203
1204 /* all things being equal, use SNR; if SNRs are
1205 * identical, use quality values since some drivers may only report
1206 * that value and leave the signal level zero */
1207 if (snr_b == snr_a)
1208 return wb->qual - wa->qual;
1209 return snr_b - snr_a;
1210 #undef MIN
1211 #undef IS_5GHZ
1212 }
1213
1214
1215 #ifdef CONFIG_WPS
1216 /* Compare function for sorting scan results when searching a WPS AP for
1217 * provisioning. Return >0 if @b is considered better. */
1218 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1219 {
1220 struct wpa_scan_res **_wa = (void *) a;
1221 struct wpa_scan_res **_wb = (void *) b;
1222 struct wpa_scan_res *wa = *_wa;
1223 struct wpa_scan_res *wb = *_wb;
1224 int uses_wps_a, uses_wps_b;
1225 struct wpabuf *wps_a, *wps_b;
1226 int res;
1227
1228 /* Optimization - check WPS IE existence before allocated memory and
1229 * doing full reassembly. */
1230 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1231 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1232 if (uses_wps_a && !uses_wps_b)
1233 return -1;
1234 if (!uses_wps_a && uses_wps_b)
1235 return 1;
1236
1237 if (uses_wps_a && uses_wps_b) {
1238 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1239 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1240 res = wps_ap_priority_compar(wps_a, wps_b);
1241 wpabuf_free(wps_a);
1242 wpabuf_free(wps_b);
1243 if (res)
1244 return res;
1245 }
1246
1247 /*
1248 * Do not use current AP security policy as a sorting criteria during
1249 * WPS provisioning step since the AP may get reconfigured at the
1250 * completion of provisioning.
1251 */
1252
1253 /* all things being equal, use signal level; if signal levels are
1254 * identical, use quality values since some drivers may only report
1255 * that value and leave the signal level zero */
1256 if (wb->level == wa->level)
1257 return wb->qual - wa->qual;
1258 return wb->level - wa->level;
1259 }
1260 #endif /* CONFIG_WPS */
1261
1262
1263 static void dump_scan_res(struct wpa_scan_results *scan_res)
1264 {
1265 #ifndef CONFIG_NO_STDOUT_DEBUG
1266 size_t i;
1267
1268 if (scan_res->res == NULL || scan_res->num == 0)
1269 return;
1270
1271 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1272
1273 for (i = 0; i < scan_res->num; i++) {
1274 struct wpa_scan_res *r = scan_res->res[i];
1275 u8 *pos;
1276 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1277 == WPA_SCAN_LEVEL_DBM) {
1278 int snr = r->level - r->noise;
1279 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1280 "noise=%d level=%d snr=%d%s flags=0x%x",
1281 MAC2STR(r->bssid), r->freq, r->qual,
1282 r->noise, r->level, snr,
1283 snr >= GREAT_SNR ? "*" : "", r->flags);
1284 } else {
1285 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1286 "noise=%d level=%d flags=0x%x",
1287 MAC2STR(r->bssid), r->freq, r->qual,
1288 r->noise, r->level, r->flags);
1289 }
1290 pos = (u8 *) (r + 1);
1291 if (r->ie_len)
1292 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1293 pos += r->ie_len;
1294 if (r->beacon_ie_len)
1295 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1296 pos, r->beacon_ie_len);
1297 }
1298 #endif /* CONFIG_NO_STDOUT_DEBUG */
1299 }
1300
1301
1302 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1303 const u8 *bssid)
1304 {
1305 size_t i;
1306
1307 if (wpa_s->bssid_filter == NULL)
1308 return 1;
1309
1310 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1311 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1312 ETH_ALEN) == 0)
1313 return 1;
1314 }
1315
1316 return 0;
1317 }
1318
1319
1320 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1321 struct wpa_scan_results *res)
1322 {
1323 size_t i, j;
1324
1325 if (wpa_s->bssid_filter == NULL)
1326 return;
1327
1328 for (i = 0, j = 0; i < res->num; i++) {
1329 if (wpa_supplicant_filter_bssid_match(wpa_s,
1330 res->res[i]->bssid)) {
1331 res->res[j++] = res->res[i];
1332 } else {
1333 os_free(res->res[i]);
1334 res->res[i] = NULL;
1335 }
1336 }
1337
1338 if (res->num != j) {
1339 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1340 (int) (res->num - j));
1341 res->num = j;
1342 }
1343 }
1344
1345
1346 /**
1347 * wpa_supplicant_get_scan_results - Get scan results
1348 * @wpa_s: Pointer to wpa_supplicant data
1349 * @info: Information about what was scanned or %NULL if not available
1350 * @new_scan: Whether a new scan was performed
1351 * Returns: Scan results, %NULL on failure
1352 *
1353 * This function request the current scan results from the driver and updates
1354 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1355 * results with wpa_scan_results_free().
1356 */
1357 struct wpa_scan_results *
1358 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1359 struct scan_info *info, int new_scan)
1360 {
1361 struct wpa_scan_results *scan_res;
1362 size_t i;
1363 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1364
1365 scan_res = wpa_drv_get_scan_results2(wpa_s);
1366 if (scan_res == NULL) {
1367 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1368 return NULL;
1369 }
1370 filter_scan_res(wpa_s, scan_res);
1371
1372 #ifdef CONFIG_WPS
1373 if (wpas_wps_in_progress(wpa_s)) {
1374 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1375 "provisioning rules");
1376 compar = wpa_scan_result_wps_compar;
1377 }
1378 #endif /* CONFIG_WPS */
1379
1380 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1381 compar);
1382 dump_scan_res(scan_res);
1383
1384 wpa_bss_update_start(wpa_s);
1385 for (i = 0; i < scan_res->num; i++)
1386 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1387 wpa_bss_update_end(wpa_s, info, new_scan);
1388
1389 return scan_res;
1390 }
1391
1392
1393 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1394 {
1395 struct wpa_scan_results *scan_res;
1396 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1397 if (scan_res == NULL)
1398 return -1;
1399 wpa_scan_results_free(scan_res);
1400
1401 return 0;
1402 }