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