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