blob: f2f72270456ccc91da681639e493d5932de2da7b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "utils/includes.h"
16
17#include "utils/common.h"
18#include "utils/eloop.h"
19#include "common/ieee802_11_defs.h"
20#include "config.h"
21#include "wpa_supplicant_i.h"
22#include "driver_i.h"
23#include "mlme.h"
24#include "wps_supplicant.h"
25#include "p2p_supplicant.h"
26#include "p2p/p2p.h"
27#include "notify.h"
28#include "bss.h"
29#include "scan.h"
30
31
32static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
33{
34 struct wpa_ssid *ssid;
35 union wpa_event_data data;
36
37 ssid = wpa_supplicant_get_ssid(wpa_s);
38 if (ssid == NULL)
39 return;
40
41 if (wpa_s->current_ssid == NULL) {
42 wpa_s->current_ssid = ssid;
43 if (wpa_s->current_ssid != NULL)
44 wpas_notify_network_changed(wpa_s);
45 }
46 wpa_supplicant_initiate_eapol(wpa_s);
47 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
48 "network - generating associated event");
49 os_memset(&data, 0, sizeof(data));
50 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
51}
52
53
54#ifdef CONFIG_WPS
55static int wpas_wps_in_use(struct wpa_config *conf,
56 enum wps_request_type *req_type)
57{
58 struct wpa_ssid *ssid;
59 int wps = 0;
60
61 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
62 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
63 continue;
64
65 wps = 1;
66 *req_type = wpas_wps_get_req_type(ssid);
67 if (!ssid->eap.phase1)
68 continue;
69
70 if (os_strstr(ssid->eap.phase1, "pbc=1"))
71 return 2;
72 }
73
74 return wps;
75}
76#endif /* CONFIG_WPS */
77
78
79int wpa_supplicant_enabled_networks(struct wpa_config *conf)
80{
81 struct wpa_ssid *ssid = conf->ssid;
82 int count = 0;
83 while (ssid) {
84 if (!ssid->disabled)
85 count++;
86 ssid = ssid->next;
87 }
88 return count;
89}
90
91
92static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
93 struct wpa_ssid *ssid)
94{
95 while (ssid) {
96 if (!ssid->disabled)
97 break;
98 ssid = ssid->next;
99 }
100
101 /* ap_scan=2 mode - try to associate with each SSID. */
102 if (ssid == NULL) {
103 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
104 "end of scan list - go back to beginning");
105 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
106 wpa_supplicant_req_scan(wpa_s, 0, 0);
107 return;
108 }
109 if (ssid->next) {
110 /* Continue from the next SSID on the next attempt. */
111 wpa_s->prev_scan_ssid = ssid;
112 } else {
113 /* Start from the beginning of the SSID list. */
114 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
115 }
116 wpa_supplicant_associate(wpa_s, NULL, ssid);
117}
118
119
120static int int_array_len(const int *a)
121{
122 int i;
123 for (i = 0; a && a[i]; i++)
124 ;
125 return i;
126}
127
128
129static void int_array_concat(int **res, const int *a)
130{
131 int reslen, alen, i;
132 int *n;
133
134 reslen = int_array_len(*res);
135 alen = int_array_len(a);
136
137 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
138 if (n == NULL) {
139 os_free(*res);
140 *res = NULL;
141 return;
142 }
143 for (i = 0; i <= alen; i++)
144 n[reslen + i] = a[i];
145 *res = n;
146}
147
148
149static int freq_cmp(const void *a, const void *b)
150{
151 int _a = *(int *) a;
152 int _b = *(int *) b;
153
154 if (_a == 0)
155 return 1;
156 if (_b == 0)
157 return -1;
158 return _a - _b;
159}
160
161
162static void int_array_sort_unique(int *a)
163{
164 int alen;
165 int i, j;
166
167 if (a == NULL)
168 return;
169
170 alen = int_array_len(a);
171 qsort(a, alen, sizeof(int), freq_cmp);
172
173 i = 0;
174 j = 1;
175 while (a[i] && a[j]) {
176 if (a[i] == a[j]) {
177 j++;
178 continue;
179 }
180 a[++i] = a[j++];
181 }
182 if (a[i])
183 i++;
184 a[i] = 0;
185}
186
187
188int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
189 struct wpa_driver_scan_params *params)
190{
191 int ret;
192
193 wpa_supplicant_notify_scanning(wpa_s, 1);
194
195 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
196 ret = ieee80211_sta_req_scan(wpa_s, params);
197 else
198 ret = wpa_drv_scan(wpa_s, params);
199
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
206 return ret;
207}
208
209
210static struct wpa_driver_scan_filter *
211wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
212{
213 struct wpa_driver_scan_filter *ssids;
214 struct wpa_ssid *ssid;
215 size_t count;
216
217 *num_ssids = 0;
218 if (!conf->filter_ssids)
219 return NULL;
220
221 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
222 if (ssid->ssid && ssid->ssid_len)
223 count++;
224 }
225 if (count == 0)
226 return NULL;
227 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
228 if (ssids == NULL)
229 return NULL;
230
231 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
232 if (!ssid->ssid || !ssid->ssid_len)
233 continue;
234 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
235 ssids[*num_ssids].ssid_len = ssid->ssid_len;
236 (*num_ssids)++;
237 }
238
239 return ssids;
240}
241
242
243static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
244{
245 struct wpa_supplicant *wpa_s = eloop_ctx;
246 struct wpa_ssid *ssid;
247 int scan_req = 0, ret;
248 struct wpabuf *wps_ie = NULL;
249#ifdef CONFIG_WPS
250 int wps = 0;
251 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
252#endif /* CONFIG_WPS */
253 struct wpa_driver_scan_params params;
254 size_t max_ssids;
255 enum wpa_states prev_state;
256
257 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
258 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
259 return;
260 }
261
262 if (wpa_s->disconnected && !wpa_s->scan_req) {
263 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
264 return;
265 }
266
267 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
268 !wpa_s->scan_req) {
269 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
270 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
271 return;
272 }
273
274 if (wpa_s->conf->ap_scan != 0 &&
275 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
276 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
277 "overriding ap_scan configuration");
278 wpa_s->conf->ap_scan = 0;
279 wpas_notify_ap_scan_changed(wpa_s);
280 }
281
282 if (wpa_s->conf->ap_scan == 0) {
283 wpa_supplicant_gen_assoc_event(wpa_s);
284 return;
285 }
286
287 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
288 wpa_s->conf->ap_scan == 2)
289 max_ssids = 1;
290 else {
291 max_ssids = wpa_s->max_scan_ssids;
292 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
293 max_ssids = WPAS_MAX_SCAN_SSIDS;
294 }
295
296#ifdef CONFIG_WPS
297 wps = wpas_wps_in_use(wpa_s->conf, &req_type);
298#endif /* CONFIG_WPS */
299
300 scan_req = wpa_s->scan_req;
301 wpa_s->scan_req = 0;
302
303 os_memset(&params, 0, sizeof(params));
304
305 prev_state = wpa_s->wpa_state;
306 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
307 wpa_s->wpa_state == WPA_INACTIVE)
308 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
309
Jouni Malinen75ecf522011-06-27 15:19:46 -0700310 if (scan_req != 2 && wpa_s->connect_without_scan) {
311 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
312 if (ssid == wpa_s->connect_without_scan)
313 break;
314 }
315 wpa_s->connect_without_scan = NULL;
316 if (ssid) {
317 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
318 "without scan step");
319 wpa_supplicant_associate(wpa_s, NULL, ssid);
320 return;
321 }
322 }
323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 /* Find the starting point from which to continue scanning */
325 ssid = wpa_s->conf->ssid;
326 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
327 while (ssid) {
328 if (ssid == wpa_s->prev_scan_ssid) {
329 ssid = ssid->next;
330 break;
331 }
332 ssid = ssid->next;
333 }
334 }
335
Jouni Malinen75ecf522011-06-27 15:19:46 -0700336 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
337 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 wpa_supplicant_assoc_try(wpa_s, ssid);
339 return;
340 } else if (wpa_s->conf->ap_scan == 2) {
341 /*
342 * User-initiated scan request in ap_scan == 2; scan with
343 * wildcard SSID.
344 */
345 ssid = NULL;
346 } else {
347 struct wpa_ssid *start = ssid, *tssid;
348 int freqs_set = 0;
349 if (ssid == NULL && max_ssids > 1)
350 ssid = wpa_s->conf->ssid;
351 while (ssid) {
352 if (!ssid->disabled && ssid->scan_ssid) {
353 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
354 ssid->ssid, ssid->ssid_len);
355 params.ssids[params.num_ssids].ssid =
356 ssid->ssid;
357 params.ssids[params.num_ssids].ssid_len =
358 ssid->ssid_len;
359 params.num_ssids++;
360 if (params.num_ssids + 1 >= max_ssids)
361 break;
362 }
363 ssid = ssid->next;
364 if (ssid == start)
365 break;
366 if (ssid == NULL && max_ssids > 1 &&
367 start != wpa_s->conf->ssid)
368 ssid = wpa_s->conf->ssid;
369 }
370
371 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
372 if (tssid->disabled)
373 continue;
374 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
375 int_array_concat(&params.freqs,
376 tssid->scan_freq);
377 } else {
378 os_free(params.freqs);
379 params.freqs = NULL;
380 }
381 freqs_set = 1;
382 }
383 int_array_sort_unique(params.freqs);
384 }
385
386 if (ssid) {
387 wpa_s->prev_scan_ssid = ssid;
388 if (max_ssids > 1) {
389 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
390 "the scan request");
391 params.num_ssids++;
392 }
393 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
394 "SSID(s)");
395 } else {
396 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
397 params.num_ssids++;
398 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
399 "SSID");
400 }
401
402#ifdef CONFIG_P2P
403 wpa_s->wps->dev.p2p = 1;
404 if (!wps) {
405 wps = 1;
406 req_type = WPS_REQ_ENROLLEE_INFO;
407 }
408
409 if (params.freqs == NULL && wpa_s->p2p_in_provisioning &&
410 wpa_s->go_params) {
411 /* Optimize provisioning state scan based on GO information */
412 if (wpa_s->p2p_in_provisioning < 5 &&
413 wpa_s->go_params->freq > 0) {
414 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
415 "preferred frequency %d MHz",
416 wpa_s->go_params->freq);
417 params.freqs = os_zalloc(2 * sizeof(int));
418 if (params.freqs)
419 params.freqs[0] = wpa_s->go_params->freq;
420 } else if (wpa_s->p2p_in_provisioning < 8 &&
421 wpa_s->go_params->freq_list[0]) {
422 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
423 "channels");
424 int_array_concat(&params.freqs,
425 wpa_s->go_params->freq_list);
426 if (params.freqs)
427 int_array_sort_unique(params.freqs);
428 }
429 wpa_s->p2p_in_provisioning++;
430 }
431#endif /* CONFIG_P2P */
432
433#ifdef CONFIG_WPS
434 if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
435 /*
436 * Optimize post-provisioning scan based on channel used
437 * during provisioning.
438 */
439 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
440 "that was used during provisioning", wpa_s->wps_freq);
441 params.freqs = os_zalloc(2 * sizeof(int));
442 if (params.freqs)
443 params.freqs[0] = wpa_s->wps_freq;
444 wpa_s->after_wps--;
445 }
446
447 if (wps) {
448 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
449 wpa_s->wps->uuid, req_type,
450 0, NULL);
451 if (wps_ie) {
452 params.extra_ies = wpabuf_head(wps_ie);
453 params.extra_ies_len = wpabuf_len(wps_ie);
454 }
455 }
456#endif /* CONFIG_WPS */
457
458#ifdef CONFIG_P2P
459 if (wps_ie) {
460 if (wpabuf_resize(&wps_ie, 100) == 0) {
461 wpas_p2p_scan_ie(wpa_s, wps_ie);
462 params.extra_ies = wpabuf_head(wps_ie);
463 params.extra_ies_len = wpabuf_len(wps_ie);
464 }
465 }
466#endif /* CONFIG_P2P */
467
468 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
469 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
470 "generated frequency list");
471 params.freqs = wpa_s->next_scan_freqs;
472 } else
473 os_free(wpa_s->next_scan_freqs);
474 wpa_s->next_scan_freqs = NULL;
475
476 params.filter_ssids = wpa_supplicant_build_filter_ssids(
477 wpa_s->conf, &params.num_filter_ssids);
478
479 ret = wpa_supplicant_trigger_scan(wpa_s, &params);
480
481 wpabuf_free(wps_ie);
482 os_free(params.freqs);
483 os_free(params.filter_ssids);
484
485 if (ret) {
486 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
487 if (prev_state != wpa_s->wpa_state)
488 wpa_supplicant_set_state(wpa_s, prev_state);
489 wpa_supplicant_req_scan(wpa_s, 1, 0);
490 }
491}
492
493
494/**
495 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
496 * @wpa_s: Pointer to wpa_supplicant data
497 * @sec: Number of seconds after which to scan
498 * @usec: Number of microseconds after which to scan
499 *
500 * This function is used to schedule a scan for neighboring access points after
501 * the specified time.
502 */
503void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
504{
505 /* If there's at least one network that should be specifically scanned
506 * then don't cancel the scan and reschedule. Some drivers do
507 * background scanning which generates frequent scan results, and that
508 * causes the specific SSID scan to get continually pushed back and
509 * never happen, which causes hidden APs to never get probe-scanned.
510 */
511 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
512 wpa_s->conf->ap_scan == 1) {
513 struct wpa_ssid *ssid = wpa_s->conf->ssid;
514
515 while (ssid) {
516 if (!ssid->disabled && ssid->scan_ssid)
517 break;
518 ssid = ssid->next;
519 }
520 if (ssid) {
521 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
522 "ensure that specific SSID scans occur");
523 return;
524 }
525 }
526
527 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
528 sec, usec);
529 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
530 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
531}
532
533
534/**
535 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
536 * @wpa_s: Pointer to wpa_supplicant data
537 *
538 * This function is used to cancel a scan request scheduled with
539 * wpa_supplicant_req_scan().
540 */
541void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
542{
543 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
544 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt20df8072011-04-08 15:35:17 -0700545 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546}
547
548
549void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
550 int scanning)
551{
552 if (wpa_s->scanning != scanning) {
553 wpa_s->scanning = scanning;
554 wpas_notify_scanning(wpa_s);
555 }
556}
557
558
559static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
560{
561 int rate = 0;
562 const u8 *ie;
563 int i;
564
565 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
566 for (i = 0; ie && i < ie[1]; i++) {
567 if ((ie[i + 2] & 0x7f) > rate)
568 rate = ie[i + 2] & 0x7f;
569 }
570
571 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
572 for (i = 0; ie && i < ie[1]; i++) {
573 if ((ie[i + 2] & 0x7f) > rate)
574 rate = ie[i + 2] & 0x7f;
575 }
576
577 return rate;
578}
579
580
581const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
582{
583 const u8 *end, *pos;
584
585 pos = (const u8 *) (res + 1);
586 end = pos + res->ie_len;
587
588 while (pos + 1 < end) {
589 if (pos + 2 + pos[1] > end)
590 break;
591 if (pos[0] == ie)
592 return pos;
593 pos += 2 + pos[1];
594 }
595
596 return NULL;
597}
598
599
600const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
601 u32 vendor_type)
602{
603 const u8 *end, *pos;
604
605 pos = (const u8 *) (res + 1);
606 end = pos + res->ie_len;
607
608 while (pos + 1 < end) {
609 if (pos + 2 + pos[1] > end)
610 break;
611 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
612 vendor_type == WPA_GET_BE32(&pos[2]))
613 return pos;
614 pos += 2 + pos[1];
615 }
616
617 return NULL;
618}
619
620
621struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
622 u32 vendor_type)
623{
624 struct wpabuf *buf;
625 const u8 *end, *pos;
626
627 buf = wpabuf_alloc(res->ie_len);
628 if (buf == NULL)
629 return NULL;
630
631 pos = (const u8 *) (res + 1);
632 end = pos + res->ie_len;
633
634 while (pos + 1 < end) {
635 if (pos + 2 + pos[1] > end)
636 break;
637 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
638 vendor_type == WPA_GET_BE32(&pos[2]))
639 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
640 pos += 2 + pos[1];
641 }
642
643 if (wpabuf_len(buf) == 0) {
644 wpabuf_free(buf);
645 buf = NULL;
646 }
647
648 return buf;
649}
650
651
652struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
653 const struct wpa_scan_res *res, u32 vendor_type)
654{
655 struct wpabuf *buf;
656 const u8 *end, *pos;
657
658 if (res->beacon_ie_len == 0)
659 return NULL;
660 buf = wpabuf_alloc(res->beacon_ie_len);
661 if (buf == NULL)
662 return NULL;
663
664 pos = (const u8 *) (res + 1);
665 pos += res->ie_len;
666 end = pos + res->beacon_ie_len;
667
668 while (pos + 1 < end) {
669 if (pos + 2 + pos[1] > end)
670 break;
671 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
672 vendor_type == WPA_GET_BE32(&pos[2]))
673 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
674 pos += 2 + pos[1];
675 }
676
677 if (wpabuf_len(buf) == 0) {
678 wpabuf_free(buf);
679 buf = NULL;
680 }
681
682 return buf;
683}
684
685
686/* Compare function for sorting scan results. Return >0 if @b is considered
687 * better. */
688static int wpa_scan_result_compar(const void *a, const void *b)
689{
690 struct wpa_scan_res **_wa = (void *) a;
691 struct wpa_scan_res **_wb = (void *) b;
692 struct wpa_scan_res *wa = *_wa;
693 struct wpa_scan_res *wb = *_wb;
694 int wpa_a, wpa_b, maxrate_a, maxrate_b;
695
696 /* WPA/WPA2 support preferred */
697 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
698 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
699 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
700 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
701
702 if (wpa_b && !wpa_a)
703 return 1;
704 if (!wpa_b && wpa_a)
705 return -1;
706
707 /* privacy support preferred */
708 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
709 (wb->caps & IEEE80211_CAP_PRIVACY))
710 return 1;
711 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
712 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
713 return -1;
714
715 /* best/max rate preferred if signal level close enough XXX */
716 if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
717 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
718 maxrate_a = wpa_scan_get_max_rate(wa);
719 maxrate_b = wpa_scan_get_max_rate(wb);
720 if (maxrate_a != maxrate_b)
721 return maxrate_b - maxrate_a;
722 }
723
724 /* use freq for channel preference */
725
726 /* all things being equal, use signal level; if signal levels are
727 * identical, use quality values since some drivers may only report
728 * that value and leave the signal level zero */
729 if (wb->level == wa->level)
730 return wb->qual - wa->qual;
731 return wb->level - wa->level;
732}
733
734
735#ifdef CONFIG_WPS
736/* Compare function for sorting scan results when searching a WPS AP for
737 * provisioning. Return >0 if @b is considered better. */
738static int wpa_scan_result_wps_compar(const void *a, const void *b)
739{
740 struct wpa_scan_res **_wa = (void *) a;
741 struct wpa_scan_res **_wb = (void *) b;
742 struct wpa_scan_res *wa = *_wa;
743 struct wpa_scan_res *wb = *_wb;
744 int uses_wps_a, uses_wps_b;
745 struct wpabuf *wps_a, *wps_b;
746 int res;
747
748 /* Optimization - check WPS IE existence before allocated memory and
749 * doing full reassembly. */
750 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
751 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
752 if (uses_wps_a && !uses_wps_b)
753 return -1;
754 if (!uses_wps_a && uses_wps_b)
755 return 1;
756
757 if (uses_wps_a && uses_wps_b) {
758 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
759 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
760 res = wps_ap_priority_compar(wps_a, wps_b);
761 wpabuf_free(wps_a);
762 wpabuf_free(wps_b);
763 if (res)
764 return res;
765 }
766
767 /*
768 * Do not use current AP security policy as a sorting criteria during
769 * WPS provisioning step since the AP may get reconfigured at the
770 * completion of provisioning.
771 */
772
773 /* all things being equal, use signal level; if signal levels are
774 * identical, use quality values since some drivers may only report
775 * that value and leave the signal level zero */
776 if (wb->level == wa->level)
777 return wb->qual - wa->qual;
778 return wb->level - wa->level;
779}
780#endif /* CONFIG_WPS */
781
782
783/**
784 * wpa_supplicant_get_scan_results - Get scan results
785 * @wpa_s: Pointer to wpa_supplicant data
786 * @info: Information about what was scanned or %NULL if not available
787 * @new_scan: Whether a new scan was performed
788 * Returns: Scan results, %NULL on failure
789 *
790 * This function request the current scan results from the driver and updates
791 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
792 * results with wpa_scan_results_free().
793 */
794struct wpa_scan_results *
795wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
796 struct scan_info *info, int new_scan)
797{
798 struct wpa_scan_results *scan_res;
799 size_t i;
800 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
801
802 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
803 scan_res = ieee80211_sta_get_scan_results(wpa_s);
804 else
805 scan_res = wpa_drv_get_scan_results2(wpa_s);
806 if (scan_res == NULL) {
807 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
808 return NULL;
809 }
810
811#ifdef CONFIG_WPS
812 if (wpas_wps_in_progress(wpa_s)) {
813 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
814 "provisioning rules");
815 compar = wpa_scan_result_wps_compar;
816 }
817#endif /* CONFIG_WPS */
818
819 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
820 compar);
821
822 wpa_bss_update_start(wpa_s);
823 for (i = 0; i < scan_res->num; i++)
824 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
825 wpa_bss_update_end(wpa_s, info, new_scan);
826
827 return scan_res;
828}
829
830
831int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
832{
833 struct wpa_scan_results *scan_res;
834 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
835 if (scan_res == NULL)
836 return -1;
837 wpa_scan_results_free(scan_res);
838
839 return 0;
840}
841
842
843void wpa_scan_results_free(struct wpa_scan_results *res)
844{
845 size_t i;
846
847 if (res == NULL)
848 return;
849
850 for (i = 0; i < res->num; i++)
851 os_free(res->res[i]);
852 os_free(res->res);
853 os_free(res);
854}