Blacklist BSS on first failure if only a single network is enabled
[mech_eap.git] / wpa_supplicant / scan.c
1 /*
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
32 static 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_printf(MSG_DEBUG, "Already associated with a configured network - "
48                    "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
55 static 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
79 int 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
92 static 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_printf(MSG_DEBUG, "wpa_supplicant_scan: 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
120 static 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
129 static 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
149 static 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
162 static 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
188 int 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
210 static struct wpa_driver_scan_filter *
211 wpa_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
243 static 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_printf(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_printf(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_printf(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
310         /* Find the starting point from which to continue scanning */
311         ssid = wpa_s->conf->ssid;
312         if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
313                 while (ssid) {
314                         if (ssid == wpa_s->prev_scan_ssid) {
315                                 ssid = ssid->next;
316                                 break;
317                         }
318                         ssid = ssid->next;
319                 }
320         }
321
322         if (scan_req != 2 && (wpa_s->conf->ap_scan == 2 ||
323                               wpa_s->connect_without_scan)) {
324                 wpa_s->connect_without_scan = 0;
325                 wpa_supplicant_assoc_try(wpa_s, ssid);
326                 return;
327         } else if (wpa_s->conf->ap_scan == 2) {
328                 /*
329                  * User-initiated scan request in ap_scan == 2; scan with
330                  * wildcard SSID.
331                  */
332                 ssid = NULL;
333         } else {
334                 struct wpa_ssid *start = ssid, *tssid;
335                 int freqs_set = 0;
336                 if (ssid == NULL && max_ssids > 1)
337                         ssid = wpa_s->conf->ssid;
338                 while (ssid) {
339                         if (!ssid->disabled && ssid->scan_ssid) {
340                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
341                                                   ssid->ssid, ssid->ssid_len);
342                                 params.ssids[params.num_ssids].ssid =
343                                         ssid->ssid;
344                                 params.ssids[params.num_ssids].ssid_len =
345                                         ssid->ssid_len;
346                                 params.num_ssids++;
347                                 if (params.num_ssids + 1 >= max_ssids)
348                                         break;
349                         }
350                         ssid = ssid->next;
351                         if (ssid == start)
352                                 break;
353                         if (ssid == NULL && max_ssids > 1 &&
354                             start != wpa_s->conf->ssid)
355                                 ssid = wpa_s->conf->ssid;
356                 }
357
358                 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
359                         if (tssid->disabled)
360                                 continue;
361                         if ((params.freqs || !freqs_set) && tssid->scan_freq) {
362                                 int_array_concat(&params.freqs,
363                                                  tssid->scan_freq);
364                         } else {
365                                 os_free(params.freqs);
366                                 params.freqs = NULL;
367                         }
368                         freqs_set = 1;
369                 }
370                 int_array_sort_unique(params.freqs);
371         }
372
373         if (ssid) {
374                 wpa_s->prev_scan_ssid = ssid;
375                 if (max_ssids > 1) {
376                         wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
377                                    "scan request");
378                         params.num_ssids++;
379                 }
380                 wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
381         } else {
382                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
383                 params.num_ssids++;
384                 wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
385         }
386
387 #ifdef CONFIG_P2P
388         wpa_s->wps->dev.p2p = 1;
389         if (!wps) {
390                 wps = 1;
391                 req_type = WPS_REQ_ENROLLEE_INFO;
392         }
393
394         if (params.freqs == NULL && wpa_s->p2p_in_provisioning &&
395             wpa_s->go_params) {
396                 /* Optimize provisioning state scan based on GO information */
397                 if (wpa_s->p2p_in_provisioning < 5 &&
398                     wpa_s->go_params->freq > 0) {
399                         wpa_printf(MSG_DEBUG, "P2P: Scan only GO preferred "
400                                    "frequency %d MHz",
401                                    wpa_s->go_params->freq);
402                         params.freqs = os_zalloc(2 * sizeof(int));
403                         if (params.freqs)
404                                 params.freqs[0] = wpa_s->go_params->freq;
405                 } else if (wpa_s->p2p_in_provisioning < 8 &&
406                            wpa_s->go_params->freq_list[0]) {
407                         wpa_printf(MSG_DEBUG, "P2P: Scan only common "
408                                    "channels");
409                         int_array_concat(&params.freqs,
410                                          wpa_s->go_params->freq_list);
411                         if (params.freqs)
412                                 int_array_sort_unique(params.freqs);
413                 }
414                 wpa_s->p2p_in_provisioning++;
415         }
416 #endif /* CONFIG_P2P */
417
418 #ifdef CONFIG_WPS
419         if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
420                 /*
421                  * Optimize post-provisioning scan based on channel used
422                  * during provisioning.
423                  */
424                 wpa_printf(MSG_DEBUG, "WPS: Scan only frequency %u MHz that "
425                            "was used during provisioning", wpa_s->wps_freq);
426                 params.freqs = os_zalloc(2 * sizeof(int));
427                 if (params.freqs)
428                         params.freqs[0] = wpa_s->wps_freq;
429                 wpa_s->after_wps--;
430         }
431
432         if (wps) {
433                 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
434                                                 wpa_s->wps->uuid, req_type);
435                 if (wps_ie) {
436                         params.extra_ies = wpabuf_head(wps_ie);
437                         params.extra_ies_len = wpabuf_len(wps_ie);
438                 }
439         }
440 #endif /* CONFIG_WPS */
441
442 #ifdef CONFIG_P2P
443         if (wps_ie) {
444                 if (wpabuf_resize(&wps_ie, 100) == 0) {
445                         wpas_p2p_scan_ie(wpa_s, wps_ie);
446                         params.extra_ies = wpabuf_head(wps_ie);
447                         params.extra_ies_len = wpabuf_len(wps_ie);
448                 }
449         }
450 #endif /* CONFIG_P2P */
451
452         if (params.freqs == NULL && wpa_s->next_scan_freqs) {
453                 wpa_printf(MSG_DEBUG, "Optimize scan based on previously "
454                            "generated frequency list");
455                 params.freqs = wpa_s->next_scan_freqs;
456         } else
457                 os_free(wpa_s->next_scan_freqs);
458         wpa_s->next_scan_freqs = NULL;
459
460         params.filter_ssids = wpa_supplicant_build_filter_ssids(
461                 wpa_s->conf, &params.num_filter_ssids);
462
463         ret = wpa_supplicant_trigger_scan(wpa_s, &params);
464
465         wpabuf_free(wps_ie);
466         os_free(params.freqs);
467         os_free(params.filter_ssids);
468
469         if (ret) {
470                 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
471                 if (prev_state != wpa_s->wpa_state)
472                         wpa_supplicant_set_state(wpa_s, prev_state);
473                 wpa_supplicant_req_scan(wpa_s, 1, 0);
474         }
475 }
476
477
478 /**
479  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
480  * @wpa_s: Pointer to wpa_supplicant data
481  * @sec: Number of seconds after which to scan
482  * @usec: Number of microseconds after which to scan
483  *
484  * This function is used to schedule a scan for neighboring access points after
485  * the specified time.
486  */
487 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
488 {
489         /* If there's at least one network that should be specifically scanned
490          * then don't cancel the scan and reschedule.  Some drivers do
491          * background scanning which generates frequent scan results, and that
492          * causes the specific SSID scan to get continually pushed back and
493          * never happen, which causes hidden APs to never get probe-scanned.
494          */
495         if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
496             wpa_s->conf->ap_scan == 1) {
497                 struct wpa_ssid *ssid = wpa_s->conf->ssid;
498
499                 while (ssid) {
500                         if (!ssid->disabled && ssid->scan_ssid)
501                                 break;
502                         ssid = ssid->next;
503                 }
504                 if (ssid) {
505                         wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
506                                 "ensure that specific SSID scans occur");
507                         return;
508                 }
509         }
510
511         wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
512                 sec, usec);
513         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
514         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
515 }
516
517
518 /**
519  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
520  * @wpa_s: Pointer to wpa_supplicant data
521  *
522  * This function is used to cancel a scan request scheduled with
523  * wpa_supplicant_req_scan().
524  */
525 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
526 {
527         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
528         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
529 }
530
531
532 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
533                                     int scanning)
534 {
535         if (wpa_s->scanning != scanning) {
536                 wpa_s->scanning = scanning;
537                 wpas_notify_scanning(wpa_s);
538         }
539 }
540
541
542 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
543 {
544         int rate = 0;
545         const u8 *ie;
546         int i;
547
548         ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
549         for (i = 0; ie && i < ie[1]; i++) {
550                 if ((ie[i + 2] & 0x7f) > rate)
551                         rate = ie[i + 2] & 0x7f;
552         }
553
554         ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
555         for (i = 0; ie && i < ie[1]; i++) {
556                 if ((ie[i + 2] & 0x7f) > rate)
557                         rate = ie[i + 2] & 0x7f;
558         }
559
560         return rate;
561 }
562
563
564 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
565 {
566         const u8 *end, *pos;
567
568         pos = (const u8 *) (res + 1);
569         end = pos + res->ie_len;
570
571         while (pos + 1 < end) {
572                 if (pos + 2 + pos[1] > end)
573                         break;
574                 if (pos[0] == ie)
575                         return pos;
576                 pos += 2 + pos[1];
577         }
578
579         return NULL;
580 }
581
582
583 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
584                                   u32 vendor_type)
585 {
586         const u8 *end, *pos;
587
588         pos = (const u8 *) (res + 1);
589         end = pos + res->ie_len;
590
591         while (pos + 1 < end) {
592                 if (pos + 2 + pos[1] > end)
593                         break;
594                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
595                     vendor_type == WPA_GET_BE32(&pos[2]))
596                         return pos;
597                 pos += 2 + pos[1];
598         }
599
600         return NULL;
601 }
602
603
604 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
605                                              u32 vendor_type)
606 {
607         struct wpabuf *buf;
608         const u8 *end, *pos;
609
610         buf = wpabuf_alloc(res->ie_len);
611         if (buf == NULL)
612                 return NULL;
613
614         pos = (const u8 *) (res + 1);
615         end = pos + res->ie_len;
616
617         while (pos + 1 < end) {
618                 if (pos + 2 + pos[1] > end)
619                         break;
620                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
621                     vendor_type == WPA_GET_BE32(&pos[2]))
622                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
623                 pos += 2 + pos[1];
624         }
625
626         if (wpabuf_len(buf) == 0) {
627                 wpabuf_free(buf);
628                 buf = NULL;
629         }
630
631         return buf;
632 }
633
634
635 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
636         const struct wpa_scan_res *res, u32 vendor_type)
637 {
638         struct wpabuf *buf;
639         const u8 *end, *pos;
640
641         if (res->beacon_ie_len == 0)
642                 return NULL;
643         buf = wpabuf_alloc(res->beacon_ie_len);
644         if (buf == NULL)
645                 return NULL;
646
647         pos = (const u8 *) (res + 1);
648         pos += res->ie_len;
649         end = pos + res->beacon_ie_len;
650
651         while (pos + 1 < end) {
652                 if (pos + 2 + pos[1] > end)
653                         break;
654                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
655                     vendor_type == WPA_GET_BE32(&pos[2]))
656                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
657                 pos += 2 + pos[1];
658         }
659
660         if (wpabuf_len(buf) == 0) {
661                 wpabuf_free(buf);
662                 buf = NULL;
663         }
664
665         return buf;
666 }
667
668
669 /* Compare function for sorting scan results. Return >0 if @b is considered
670  * better. */
671 static int wpa_scan_result_compar(const void *a, const void *b)
672 {
673         struct wpa_scan_res **_wa = (void *) a;
674         struct wpa_scan_res **_wb = (void *) b;
675         struct wpa_scan_res *wa = *_wa;
676         struct wpa_scan_res *wb = *_wb;
677         int wpa_a, wpa_b, maxrate_a, maxrate_b;
678
679         /* WPA/WPA2 support preferred */
680         wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
681                 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
682         wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
683                 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
684
685         if (wpa_b && !wpa_a)
686                 return 1;
687         if (!wpa_b && wpa_a)
688                 return -1;
689
690         /* privacy support preferred */
691         if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
692             (wb->caps & IEEE80211_CAP_PRIVACY))
693                 return 1;
694         if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
695             (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
696                 return -1;
697
698         /* best/max rate preferred if signal level close enough XXX */
699         if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
700             (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
701                 maxrate_a = wpa_scan_get_max_rate(wa);
702                 maxrate_b = wpa_scan_get_max_rate(wb);
703                 if (maxrate_a != maxrate_b)
704                         return maxrate_b - maxrate_a;
705         }
706
707         /* use freq for channel preference */
708
709         /* all things being equal, use signal level; if signal levels are
710          * identical, use quality values since some drivers may only report
711          * that value and leave the signal level zero */
712         if (wb->level == wa->level)
713                 return wb->qual - wa->qual;
714         return wb->level - wa->level;
715 }
716
717
718 #ifdef CONFIG_WPS
719 /* Compare function for sorting scan results when searching a WPS AP for
720  * provisioning. Return >0 if @b is considered better. */
721 static int wpa_scan_result_wps_compar(const void *a, const void *b)
722 {
723         struct wpa_scan_res **_wa = (void *) a;
724         struct wpa_scan_res **_wb = (void *) b;
725         struct wpa_scan_res *wa = *_wa;
726         struct wpa_scan_res *wb = *_wb;
727         int uses_wps_a, uses_wps_b;
728         struct wpabuf *wps_a, *wps_b;
729         int res;
730
731         /* Optimization - check WPS IE existence before allocated memory and
732          * doing full reassembly. */
733         uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
734         uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
735         if (uses_wps_a && !uses_wps_b)
736                 return -1;
737         if (!uses_wps_a && uses_wps_b)
738                 return 1;
739
740         if (uses_wps_a && uses_wps_b) {
741                 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
742                 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
743                 res = wps_ap_priority_compar(wps_a, wps_b);
744                 wpabuf_free(wps_a);
745                 wpabuf_free(wps_b);
746                 if (res)
747                         return res;
748         }
749
750         /*
751          * Do not use current AP security policy as a sorting criteria during
752          * WPS provisioning step since the AP may get reconfigured at the
753          * completion of provisioning.
754          */
755
756         /* all things being equal, use signal level; if signal levels are
757          * identical, use quality values since some drivers may only report
758          * that value and leave the signal level zero */
759         if (wb->level == wa->level)
760                 return wb->qual - wa->qual;
761         return wb->level - wa->level;
762 }
763 #endif /* CONFIG_WPS */
764
765
766 /**
767  * wpa_supplicant_get_scan_results - Get scan results
768  * @wpa_s: Pointer to wpa_supplicant data
769  * @info: Information about what was scanned or %NULL if not available
770  * @new_scan: Whether a new scan was performed
771  * Returns: Scan results, %NULL on failure
772  *
773  * This function request the current scan results from the driver and updates
774  * the local BSS list wpa_s->bss. The caller is responsible for freeing the
775  * results with wpa_scan_results_free().
776  */
777 struct wpa_scan_results *
778 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
779                                 struct scan_info *info, int new_scan)
780 {
781         struct wpa_scan_results *scan_res;
782         size_t i;
783         int (*compar)(const void *, const void *) = wpa_scan_result_compar;
784
785         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
786                 scan_res = ieee80211_sta_get_scan_results(wpa_s);
787         else
788                 scan_res = wpa_drv_get_scan_results2(wpa_s);
789         if (scan_res == NULL) {
790                 wpa_printf(MSG_DEBUG, "Failed to get scan results");
791                 return NULL;
792         }
793
794 #ifdef CONFIG_WPS
795         if (wpas_wps_in_progress(wpa_s)) {
796                 wpa_printf(MSG_DEBUG, "WPS: Order scan results with WPS "
797                            "provisioning rules");
798                 compar = wpa_scan_result_wps_compar;
799         }
800 #endif /* CONFIG_WPS */
801
802         qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
803               compar);
804
805         wpa_bss_update_start(wpa_s);
806         for (i = 0; i < scan_res->num; i++)
807                 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
808         wpa_bss_update_end(wpa_s, info, new_scan);
809
810         return scan_res;
811 }
812
813
814 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
815 {
816         struct wpa_scan_results *scan_res;
817         scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
818         if (scan_res == NULL)
819                 return -1;
820         wpa_scan_results_free(scan_res);
821
822         return 0;
823 }
824
825
826 void wpa_scan_results_free(struct wpa_scan_results *res)
827 {
828         size_t i;
829
830         if (res == NULL)
831                 return;
832
833         for (i = 0; i < res->num; i++)
834                 os_free(res->res[i]);
835         os_free(res->res);
836         os_free(res);
837 }