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