P2P: Avoid multi-channel scans when they are not needed
[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 software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "config.h"
15 #include "wpa_supplicant_i.h"
16 #include "driver_i.h"
17 #include "wps_supplicant.h"
18 #include "p2p_supplicant.h"
19 #include "p2p/p2p.h"
20 #include "hs20_supplicant.h"
21 #include "notify.h"
22 #include "bss.h"
23 #include "scan.h"
24
25
26 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
27 {
28         struct wpa_ssid *ssid;
29         union wpa_event_data data;
30
31         ssid = wpa_supplicant_get_ssid(wpa_s);
32         if (ssid == NULL)
33                 return;
34
35         if (wpa_s->current_ssid == NULL) {
36                 wpa_s->current_ssid = ssid;
37                 if (wpa_s->current_ssid != NULL)
38                         wpas_notify_network_changed(wpa_s);
39         }
40         wpa_supplicant_initiate_eapol(wpa_s);
41         wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
42                 "network - generating associated event");
43         os_memset(&data, 0, sizeof(data));
44         wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
45 }
46
47
48 #ifdef CONFIG_WPS
49 static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
50                            enum wps_request_type *req_type)
51 {
52         struct wpa_ssid *ssid;
53         int wps = 0;
54
55         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
56                 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
57                         continue;
58
59                 wps = 1;
60                 *req_type = wpas_wps_get_req_type(ssid);
61                 if (!ssid->eap.phase1)
62                         continue;
63
64                 if (os_strstr(ssid->eap.phase1, "pbc=1"))
65                         return 2;
66         }
67
68 #ifdef CONFIG_P2P
69         if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
70             !wpa_s->conf->p2p_disabled) {
71                 wpa_s->wps->dev.p2p = 1;
72                 if (!wps) {
73                         wps = 1;
74                         *req_type = WPS_REQ_ENROLLEE_INFO;
75                 }
76         }
77 #endif /* CONFIG_P2P */
78
79         return wps;
80 }
81 #endif /* CONFIG_WPS */
82
83
84 int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
85 {
86         struct wpa_ssid *ssid = wpa_s->conf->ssid;
87         int count = 0, disabled = 0;
88         while (ssid) {
89                 if (!wpas_network_disabled(wpa_s, ssid))
90                         count++;
91                 else
92                         disabled++;
93                 ssid = ssid->next;
94         }
95         if (wpa_s->conf->cred && wpa_s->conf->interworking &&
96             wpa_s->conf->auto_interworking)
97                 count++;
98         if (count == 0 && disabled > 0) {
99                 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
100                         "networks)", disabled);
101         }
102         return count;
103 }
104
105
106 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
107                                      struct wpa_ssid *ssid)
108 {
109         while (ssid) {
110                 if (!wpas_network_disabled(wpa_s, ssid))
111                         break;
112                 ssid = ssid->next;
113         }
114
115         /* ap_scan=2 mode - try to associate with each SSID. */
116         if (ssid == NULL) {
117                 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
118                         "end of scan list - go back to beginning");
119                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
120                 wpa_supplicant_req_scan(wpa_s, 0, 0);
121                 return;
122         }
123         if (ssid->next) {
124                 /* Continue from the next SSID on the next attempt. */
125                 wpa_s->prev_scan_ssid = ssid;
126         } else {
127                 /* Start from the beginning of the SSID list. */
128                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
129         }
130         wpa_supplicant_associate(wpa_s, NULL, ssid);
131 }
132
133
134 static int int_array_len(const int *a)
135 {
136         int i;
137         for (i = 0; a && a[i]; i++)
138                 ;
139         return i;
140 }
141
142
143 static void int_array_concat(int **res, const int *a)
144 {
145         int reslen, alen, i;
146         int *n;
147
148         reslen = int_array_len(*res);
149         alen = int_array_len(a);
150
151         n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
152         if (n == NULL) {
153                 os_free(*res);
154                 *res = NULL;
155                 return;
156         }
157         for (i = 0; i <= alen; i++)
158                 n[reslen + i] = a[i];
159         *res = n;
160 }
161
162
163 static int freq_cmp(const void *a, const void *b)
164 {
165         int _a = *(int *) a;
166         int _b = *(int *) b;
167
168         if (_a == 0)
169                 return 1;
170         if (_b == 0)
171                 return -1;
172         return _a - _b;
173 }
174
175
176 static void int_array_sort_unique(int *a)
177 {
178         int alen;
179         int i, j;
180
181         if (a == NULL)
182                 return;
183
184         alen = int_array_len(a);
185         qsort(a, alen, sizeof(int), freq_cmp);
186
187         i = 0;
188         j = 1;
189         while (a[i] && a[j]) {
190                 if (a[i] == a[j]) {
191                         j++;
192                         continue;
193                 }
194                 a[++i] = a[j++];
195         }
196         if (a[i])
197                 i++;
198         a[i] = 0;
199 }
200
201
202 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
203                                 struct wpa_driver_scan_params *params)
204 {
205         int ret;
206
207         wpa_supplicant_notify_scanning(wpa_s, 1);
208
209         ret = wpa_drv_scan(wpa_s, params);
210         if (ret) {
211                 wpa_supplicant_notify_scanning(wpa_s, 0);
212                 wpas_notify_scan_done(wpa_s, 0);
213         } else {
214                 wpa_s->scan_runs++;
215                 wpa_s->normal_scans++;
216         }
217
218         return ret;
219 }
220
221
222 static void
223 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
224 {
225         struct wpa_supplicant *wpa_s = eloop_ctx;
226
227         wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
228
229         if (wpa_supplicant_req_sched_scan(wpa_s))
230                 wpa_supplicant_req_scan(wpa_s, 0, 0);
231 }
232
233
234 static void
235 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
236 {
237         struct wpa_supplicant *wpa_s = eloop_ctx;
238
239         wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
240
241         wpa_s->sched_scan_timed_out = 1;
242         wpa_supplicant_cancel_sched_scan(wpa_s);
243 }
244
245
246 static int
247 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
248                                 struct wpa_driver_scan_params *params,
249                                 int interval)
250 {
251         int ret;
252
253         wpa_supplicant_notify_scanning(wpa_s, 1);
254         ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
255         if (ret)
256                 wpa_supplicant_notify_scanning(wpa_s, 0);
257         else
258                 wpa_s->sched_scanning = 1;
259
260         return ret;
261 }
262
263
264 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
265 {
266         int ret;
267
268         ret = wpa_drv_stop_sched_scan(wpa_s);
269         if (ret) {
270                 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
271                 /* TODO: what to do if stopping fails? */
272                 return -1;
273         }
274
275         return ret;
276 }
277
278
279 static struct wpa_driver_scan_filter *
280 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
281 {
282         struct wpa_driver_scan_filter *ssids;
283         struct wpa_ssid *ssid;
284         size_t count;
285
286         *num_ssids = 0;
287         if (!conf->filter_ssids)
288                 return NULL;
289
290         for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
291                 if (ssid->ssid && ssid->ssid_len)
292                         count++;
293         }
294         if (count == 0)
295                 return NULL;
296         ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
297         if (ssids == NULL)
298                 return NULL;
299
300         for (ssid = conf->ssid; ssid; ssid = ssid->next) {
301                 if (!ssid->ssid || !ssid->ssid_len)
302                         continue;
303                 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
304                 ssids[*num_ssids].ssid_len = ssid->ssid_len;
305                 (*num_ssids)++;
306         }
307
308         return ssids;
309 }
310
311
312 static void wpa_supplicant_optimize_freqs(
313         struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
314 {
315 #ifdef CONFIG_P2P
316         if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
317             wpa_s->go_params) {
318                 /* Optimize provisioning state scan based on GO information */
319                 if (wpa_s->p2p_in_provisioning < 5 &&
320                     wpa_s->go_params->freq > 0) {
321                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
322                                 "preferred frequency %d MHz",
323                                 wpa_s->go_params->freq);
324                         params->freqs = os_zalloc(2 * sizeof(int));
325                         if (params->freqs)
326                                 params->freqs[0] = wpa_s->go_params->freq;
327                 } else if (wpa_s->p2p_in_provisioning < 8 &&
328                            wpa_s->go_params->freq_list[0]) {
329                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
330                                 "channels");
331                         int_array_concat(&params->freqs,
332                                          wpa_s->go_params->freq_list);
333                         if (params->freqs)
334                                 int_array_sort_unique(params->freqs);
335                 }
336                 wpa_s->p2p_in_provisioning++;
337         }
338 #endif /* CONFIG_P2P */
339
340 #ifdef CONFIG_WPS
341         if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
342                 /*
343                  * Optimize post-provisioning scan based on channel used
344                  * during provisioning.
345                  */
346                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
347                         "that was used during provisioning", wpa_s->wps_freq);
348                 params->freqs = os_zalloc(2 * sizeof(int));
349                 if (params->freqs)
350                         params->freqs[0] = wpa_s->wps_freq;
351                 wpa_s->after_wps--;
352         }
353
354         if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
355         {
356                 /* Optimize provisioning scan based on already known channel */
357                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
358                         wpa_s->wps_freq);
359                 params->freqs = os_zalloc(2 * sizeof(int));
360                 if (params->freqs)
361                         params->freqs[0] = wpa_s->wps_freq;
362                 wpa_s->known_wps_freq = 0; /* only do this once */
363         }
364 #endif /* CONFIG_WPS */
365 }
366
367
368 #ifdef CONFIG_INTERWORKING
369 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
370                                            struct wpabuf *buf)
371 {
372         if (wpa_s->conf->interworking == 0)
373                 return;
374
375         wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
376         wpabuf_put_u8(buf, 4);
377         wpabuf_put_u8(buf, 0x00);
378         wpabuf_put_u8(buf, 0x00);
379         wpabuf_put_u8(buf, 0x00);
380         wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
381
382         wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
383         wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
384                       1 + ETH_ALEN);
385         wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
386         /* No Venue Info */
387         if (!is_zero_ether_addr(wpa_s->conf->hessid))
388                 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
389 }
390 #endif /* CONFIG_INTERWORKING */
391
392
393 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
394 {
395         struct wpabuf *extra_ie = NULL;
396 #ifdef CONFIG_WPS
397         int wps = 0;
398         enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
399 #endif /* CONFIG_WPS */
400
401 #ifdef CONFIG_INTERWORKING
402         if (wpa_s->conf->interworking &&
403             wpabuf_resize(&extra_ie, 100) == 0)
404                 wpas_add_interworking_elements(wpa_s, extra_ie);
405 #endif /* CONFIG_INTERWORKING */
406
407 #ifdef CONFIG_WPS
408         wps = wpas_wps_in_use(wpa_s, &req_type);
409
410         if (wps) {
411                 struct wpabuf *wps_ie;
412                 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
413                                                 DEV_PW_DEFAULT,
414                                                 &wpa_s->wps->dev,
415                                                 wpa_s->wps->uuid, req_type,
416                                                 0, NULL);
417                 if (wps_ie) {
418                         if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
419                                 wpabuf_put_buf(extra_ie, wps_ie);
420                         wpabuf_free(wps_ie);
421                 }
422         }
423
424 #ifdef CONFIG_P2P
425         if (wps) {
426                 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
427                 if (wpabuf_resize(&extra_ie, ielen) == 0)
428                         wpas_p2p_scan_ie(wpa_s, extra_ie);
429         }
430 #endif /* CONFIG_P2P */
431
432 #endif /* CONFIG_WPS */
433
434         return extra_ie;
435 }
436
437
438 #ifdef CONFIG_P2P
439
440 /*
441  * Check whether there are any enabled networks or credentials that could be
442  * used for a non-P2P connection.
443  */
444 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
445 {
446         struct wpa_ssid *ssid;
447
448         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
449                 if (wpas_network_disabled(wpa_s, ssid))
450                         continue;
451                 if (!ssid->p2p_group)
452                         return 1;
453         }
454
455         if (wpa_s->conf->cred && wpa_s->conf->interworking &&
456             wpa_s->conf->auto_interworking)
457                 return 1;
458
459         return 0;
460 }
461
462
463 /*
464  * Find the operating frequency of any other virtual interface that is using
465  * the same radio concurrently.
466  */
467 static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
468 {
469         const char *rn, *rn2;
470         struct wpa_supplicant *ifs;
471         u8 bssid[ETH_ALEN];
472
473         if (!wpa_s->driver->get_radio_name)
474                 return -1;
475
476         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
477         if (rn == NULL || rn[0] == '\0')
478                 return -1;
479
480         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
481                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
482                         continue;
483
484                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
485                 if (!rn2 || os_strcmp(rn, rn2) != 0)
486                         continue;
487
488                 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
489                         continue;
490
491                 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
492                     ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
493                         return ifs->current_ssid->frequency;
494                 if (wpa_drv_get_bssid(ifs, bssid) == 0)
495                         return ifs->assoc_freq;
496         }
497
498         return 0;
499 }
500
501 #endif /* CONFIG_P2P */
502
503
504 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
505 {
506         struct wpa_supplicant *wpa_s = eloop_ctx;
507         struct wpa_ssid *ssid;
508         int scan_req = 0, ret;
509         struct wpabuf *extra_ie = NULL;
510         struct wpa_driver_scan_params params;
511         struct wpa_driver_scan_params *scan_params;
512         size_t max_ssids;
513         enum wpa_states prev_state;
514
515         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
516                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
517                 return;
518         }
519
520         if (wpa_s->disconnected && !wpa_s->scan_req) {
521                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
522                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
523                 return;
524         }
525
526         if (!wpa_supplicant_enabled_networks(wpa_s) &&
527             !wpa_s->scan_req) {
528                 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
529                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
530 #ifdef CONFIG_P2P
531                 wpa_s->sta_scan_pending = 0;
532 #endif /* CONFIG_P2P */
533                 return;
534         }
535
536         if (wpa_s->conf->ap_scan != 0 &&
537             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
538                 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
539                         "overriding ap_scan configuration");
540                 wpa_s->conf->ap_scan = 0;
541                 wpas_notify_ap_scan_changed(wpa_s);
542         }
543
544         if (wpa_s->conf->ap_scan == 0) {
545                 wpa_supplicant_gen_assoc_event(wpa_s);
546                 return;
547         }
548
549 #ifdef CONFIG_P2P
550         if (wpas_p2p_in_progress(wpa_s)) {
551                 if (wpa_s->sta_scan_pending &&
552                     wpas_p2p_in_progress(wpa_s) == 2 &&
553                     wpa_s->global->p2p_cb_on_scan_complete) {
554                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
555                                 "mode scan during P2P search");
556                 } else {
557                         wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
558                                 "while P2P operation is in progress");
559                         wpa_s->sta_scan_pending = 1;
560                         wpa_supplicant_req_scan(wpa_s, 5, 0);
561                         return;
562                 }
563         }
564 #endif /* CONFIG_P2P */
565
566         if (wpa_s->conf->ap_scan == 2)
567                 max_ssids = 1;
568         else {
569                 max_ssids = wpa_s->max_scan_ssids;
570                 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
571                         max_ssids = WPAS_MAX_SCAN_SSIDS;
572         }
573
574         scan_req = wpa_s->scan_req;
575         wpa_s->scan_req = 0;
576
577         os_memset(&params, 0, sizeof(params));
578
579         prev_state = wpa_s->wpa_state;
580         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
581             wpa_s->wpa_state == WPA_INACTIVE)
582                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
583
584         /*
585          * If autoscan has set its own scanning parameters
586          */
587         if (wpa_s->autoscan_params != NULL) {
588                 scan_params = wpa_s->autoscan_params;
589                 goto scan;
590         }
591
592         if (scan_req != 2 && wpa_s->connect_without_scan) {
593                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
594                         if (ssid == wpa_s->connect_without_scan)
595                                 break;
596                 }
597                 wpa_s->connect_without_scan = NULL;
598                 if (ssid) {
599                         wpa_printf(MSG_DEBUG, "Start a pre-selected network "
600                                    "without scan step");
601                         wpa_supplicant_associate(wpa_s, NULL, ssid);
602                         return;
603                 }
604         }
605
606 #ifdef CONFIG_P2P
607         if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
608             wpa_s->go_params) {
609                 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
610                            "P2P group formation");
611                 params.ssids[0].ssid = wpa_s->go_params->ssid;
612                 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
613                 params.num_ssids = 1;
614                 goto ssid_list_set;
615         }
616 #endif /* CONFIG_P2P */
617
618         /* Find the starting point from which to continue scanning */
619         ssid = wpa_s->conf->ssid;
620         if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
621                 while (ssid) {
622                         if (ssid == wpa_s->prev_scan_ssid) {
623                                 ssid = ssid->next;
624                                 break;
625                         }
626                         ssid = ssid->next;
627                 }
628         }
629
630         if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
631                 wpa_s->connect_without_scan = NULL;
632                 wpa_s->prev_scan_wildcard = 0;
633                 wpa_supplicant_assoc_try(wpa_s, ssid);
634                 return;
635         } else if (wpa_s->conf->ap_scan == 2) {
636                 /*
637                  * User-initiated scan request in ap_scan == 2; scan with
638                  * wildcard SSID.
639                  */
640                 ssid = NULL;
641         } else {
642                 struct wpa_ssid *start = ssid, *tssid;
643                 int freqs_set = 0;
644                 if (ssid == NULL && max_ssids > 1)
645                         ssid = wpa_s->conf->ssid;
646                 while (ssid) {
647                         if (!wpas_network_disabled(wpa_s, ssid) &&
648                             ssid->scan_ssid) {
649                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
650                                                   ssid->ssid, ssid->ssid_len);
651                                 params.ssids[params.num_ssids].ssid =
652                                         ssid->ssid;
653                                 params.ssids[params.num_ssids].ssid_len =
654                                         ssid->ssid_len;
655                                 params.num_ssids++;
656                                 if (params.num_ssids + 1 >= max_ssids)
657                                         break;
658                         }
659                         ssid = ssid->next;
660                         if (ssid == start)
661                                 break;
662                         if (ssid == NULL && max_ssids > 1 &&
663                             start != wpa_s->conf->ssid)
664                                 ssid = wpa_s->conf->ssid;
665                 }
666
667                 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
668                         if (wpas_network_disabled(wpa_s, tssid))
669                                 continue;
670                         if ((params.freqs || !freqs_set) && tssid->scan_freq) {
671                                 int_array_concat(&params.freqs,
672                                                  tssid->scan_freq);
673                         } else {
674                                 os_free(params.freqs);
675                                 params.freqs = NULL;
676                         }
677                         freqs_set = 1;
678                 }
679                 int_array_sort_unique(params.freqs);
680         }
681
682         if (ssid && max_ssids == 1) {
683                 /*
684                  * If the driver is limited to 1 SSID at a time interleave
685                  * wildcard SSID scans with specific SSID scans to avoid
686                  * waiting a long time for a wildcard scan.
687                  */
688                 if (!wpa_s->prev_scan_wildcard) {
689                         params.ssids[0].ssid = NULL;
690                         params.ssids[0].ssid_len = 0;
691                         wpa_s->prev_scan_wildcard = 1;
692                         wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
693                                 "wildcard SSID (Interleave with specific)");
694                 } else {
695                         wpa_s->prev_scan_ssid = ssid;
696                         wpa_s->prev_scan_wildcard = 0;
697                         wpa_dbg(wpa_s, MSG_DEBUG,
698                                 "Starting AP scan for specific SSID: %s",
699                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
700                 }
701         } else if (ssid) {
702                 /* max_ssids > 1 */
703
704                 wpa_s->prev_scan_ssid = ssid;
705                 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
706                         "the scan request");
707                 params.num_ssids++;
708         } else {
709                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
710                 params.num_ssids++;
711                 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
712                         "SSID");
713         }
714 #ifdef CONFIG_P2P
715 ssid_list_set:
716 #endif /* CONFIG_P2P */
717
718         wpa_supplicant_optimize_freqs(wpa_s, &params);
719         extra_ie = wpa_supplicant_extra_ies(wpa_s);
720
721 #ifdef CONFIG_HS20
722         if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
723                 wpas_hs20_add_indication(extra_ie);
724 #endif /* CONFIG_HS20 */
725
726         if (params.freqs == NULL && wpa_s->next_scan_freqs) {
727                 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
728                         "generated frequency list");
729                 params.freqs = wpa_s->next_scan_freqs;
730         } else
731                 os_free(wpa_s->next_scan_freqs);
732         wpa_s->next_scan_freqs = NULL;
733
734         params.filter_ssids = wpa_supplicant_build_filter_ssids(
735                 wpa_s->conf, &params.num_filter_ssids);
736         if (extra_ie) {
737                 params.extra_ies = wpabuf_head(extra_ie);
738                 params.extra_ies_len = wpabuf_len(extra_ie);
739         }
740
741 #ifdef CONFIG_P2P
742         if (wpa_s->p2p_in_provisioning ||
743             (wpa_s->show_group_started && wpa_s->go_params)) {
744                 /*
745                  * The interface may not yet be in P2P mode, so we have to
746                  * explicitly request P2P probe to disable CCK rates.
747                  */
748                 params.p2p_probe = 1;
749         }
750 #endif /* CONFIG_P2P */
751
752         scan_params = &params;
753
754 scan:
755 #ifdef CONFIG_P2P
756         /*
757          * If the driver does not support multi-channel concurrency and a
758          * virtual interface that shares the same radio with the wpa_s interface
759          * is operating there may not be need to scan other channels apart from
760          * the current operating channel on the other virtual interface. Filter
761          * out other channels in case we are trying to find a connection for a
762          * station interface when we are not configured to prefer station
763          * connection and a concurrent operation is already in process.
764          */
765         if (wpa_s->scan_for_connection && !scan_req &&
766             !scan_params->freqs && !params.freqs &&
767             wpas_is_p2p_prioritized(wpa_s) &&
768             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
769             wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
770             non_p2p_network_enabled(wpa_s)) {
771                 int freq = shared_vif_oper_freq(wpa_s);
772                 if (freq > 0) {
773                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
774                                 "operating channel (%d MHz) since driver does "
775                                 "not support multi-channel concurrency", freq);
776                         params.freqs = os_zalloc(sizeof(int) * 2);
777                         if (params.freqs)
778                                 params.freqs[0] = freq;
779                         scan_params->freqs = params.freqs;
780                 }
781         }
782 #endif /* CONFIG_P2P */
783
784         ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
785
786         wpabuf_free(extra_ie);
787         os_free(params.freqs);
788         os_free(params.filter_ssids);
789
790         if (ret) {
791                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
792                 if (prev_state != wpa_s->wpa_state)
793                         wpa_supplicant_set_state(wpa_s, prev_state);
794                 /* Restore scan_req since we will try to scan again */
795                 wpa_s->scan_req = scan_req;
796                 wpa_supplicant_req_scan(wpa_s, 1, 0);
797         } else {
798                 wpa_s->scan_for_connection = 0;
799         }
800 }
801
802
803 /**
804  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
805  * @wpa_s: Pointer to wpa_supplicant data
806  * @sec: Number of seconds after which to scan
807  * @usec: Number of microseconds after which to scan
808  *
809  * This function is used to schedule a scan for neighboring access points after
810  * the specified time.
811  */
812 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
813 {
814         /* If there's at least one network that should be specifically scanned
815          * then don't cancel the scan and reschedule.  Some drivers do
816          * background scanning which generates frequent scan results, and that
817          * causes the specific SSID scan to get continually pushed back and
818          * never happen, which causes hidden APs to never get probe-scanned.
819          */
820         if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
821             wpa_s->conf->ap_scan == 1) {
822                 struct wpa_ssid *ssid = wpa_s->conf->ssid;
823
824                 while (ssid) {
825                         if (!wpas_network_disabled(wpa_s, ssid) &&
826                             ssid->scan_ssid)
827                                 break;
828                         ssid = ssid->next;
829                 }
830                 if (ssid) {
831                         wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
832                                 "ensure that specific SSID scans occur");
833                         return;
834                 }
835         }
836
837         wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
838                 sec, usec);
839         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
840         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
841 }
842
843
844 /**
845  * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
846  * @wpa_s: Pointer to wpa_supplicant data
847  * @sec: Number of seconds after which to scan
848  * @usec: Number of microseconds after which to scan
849  *
850  * This function is used to schedule periodic scans for neighboring
851  * access points after the specified time.
852  */
853 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
854                                       int sec, int usec)
855 {
856         if (!wpa_s->sched_scan_supported)
857                 return -1;
858
859         eloop_register_timeout(sec, usec,
860                                wpa_supplicant_delayed_sched_scan_timeout,
861                                wpa_s, NULL);
862
863         return 0;
864 }
865
866
867 /**
868  * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
869  * @wpa_s: Pointer to wpa_supplicant data
870  *
871  * This function is used to schedule periodic scans for neighboring
872  * access points repeating the scan continuously.
873  */
874 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
875 {
876         struct wpa_driver_scan_params params;
877         struct wpa_driver_scan_params *scan_params;
878         enum wpa_states prev_state;
879         struct wpa_ssid *ssid = NULL;
880         struct wpabuf *extra_ie = NULL;
881         int ret;
882         unsigned int max_sched_scan_ssids;
883         int wildcard = 0;
884         int need_ssids;
885
886         if (!wpa_s->sched_scan_supported)
887                 return -1;
888
889         if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
890                 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
891         else
892                 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
893         if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
894                 return -1;
895
896         if (wpa_s->sched_scanning) {
897                 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
898                 return 0;
899         }
900
901         need_ssids = 0;
902         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
903                 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
904                         /* Use wildcard SSID to find this network */
905                         wildcard = 1;
906                 } else if (!wpas_network_disabled(wpa_s, ssid) &&
907                            ssid->ssid_len)
908                         need_ssids++;
909
910 #ifdef CONFIG_WPS
911                 if (!wpas_network_disabled(wpa_s, ssid) &&
912                     ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
913                         /*
914                          * Normal scan is more reliable and faster for WPS
915                          * operations and since these are for short periods of
916                          * time, the benefit of trying to use sched_scan would
917                          * be limited.
918                          */
919                         wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
920                                 "sched_scan for WPS");
921                         return -1;
922                 }
923 #endif /* CONFIG_WPS */
924         }
925         if (wildcard)
926                 need_ssids++;
927
928         if (wpa_s->normal_scans < 3 &&
929             (need_ssids <= wpa_s->max_scan_ssids ||
930              wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
931                 /*
932                  * When normal scan can speed up operations, use that for the
933                  * first operations before starting the sched_scan to allow
934                  * user space sleep more. We do this only if the normal scan
935                  * has functionality that is suitable for this or if the
936                  * sched_scan does not have better support for multiple SSIDs.
937                  */
938                 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
939                         "sched_scan for initial scans (normal_scans=%d)",
940                         wpa_s->normal_scans);
941                 return -1;
942         }
943
944         os_memset(&params, 0, sizeof(params));
945
946         /* If we can't allocate space for the filters, we just don't filter */
947         params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
948                                         sizeof(struct wpa_driver_scan_filter));
949
950         prev_state = wpa_s->wpa_state;
951         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
952             wpa_s->wpa_state == WPA_INACTIVE)
953                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
954
955         if (wpa_s->autoscan_params != NULL) {
956                 scan_params = wpa_s->autoscan_params;
957                 goto scan;
958         }
959
960         /* Find the starting point from which to continue scanning */
961         ssid = wpa_s->conf->ssid;
962         if (wpa_s->prev_sched_ssid) {
963                 while (ssid) {
964                         if (ssid == wpa_s->prev_sched_ssid) {
965                                 ssid = ssid->next;
966                                 break;
967                         }
968                         ssid = ssid->next;
969                 }
970         }
971
972         if (!ssid || !wpa_s->prev_sched_ssid) {
973                 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
974
975                 if (wpa_s->sched_scan_interval == 0)
976                         wpa_s->sched_scan_interval = 10;
977                 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
978                 wpa_s->first_sched_scan = 1;
979                 ssid = wpa_s->conf->ssid;
980                 wpa_s->prev_sched_ssid = ssid;
981         }
982
983         if (wildcard) {
984                 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
985                 params.num_ssids++;
986         }
987
988         while (ssid) {
989                 if (wpas_network_disabled(wpa_s, ssid))
990                         goto next;
991
992                 if (params.num_filter_ssids < wpa_s->max_match_sets &&
993                     params.filter_ssids && ssid->ssid && ssid->ssid_len) {
994                         wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
995                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
996                         os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
997                                   ssid->ssid, ssid->ssid_len);
998                         params.filter_ssids[params.num_filter_ssids].ssid_len =
999                                 ssid->ssid_len;
1000                         params.num_filter_ssids++;
1001                 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1002                 {
1003                         wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1004                                 "filter for sched_scan - drop filter");
1005                         os_free(params.filter_ssids);
1006                         params.filter_ssids = NULL;
1007                         params.num_filter_ssids = 0;
1008                 }
1009
1010                 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1011                         if (params.num_ssids == max_sched_scan_ssids)
1012                                 break; /* only room for broadcast SSID */
1013                         wpa_dbg(wpa_s, MSG_DEBUG,
1014                                 "add to active scan ssid: %s",
1015                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1016                         params.ssids[params.num_ssids].ssid =
1017                                 ssid->ssid;
1018                         params.ssids[params.num_ssids].ssid_len =
1019                                 ssid->ssid_len;
1020                         params.num_ssids++;
1021                         if (params.num_ssids >= max_sched_scan_ssids) {
1022                                 wpa_s->prev_sched_ssid = ssid;
1023                                 do {
1024                                         ssid = ssid->next;
1025                                 } while (ssid &&
1026                                          (wpas_network_disabled(wpa_s, ssid) ||
1027                                           !ssid->scan_ssid));
1028                                 break;
1029                         }
1030                 }
1031
1032         next:
1033                 wpa_s->prev_sched_ssid = ssid;
1034                 ssid = ssid->next;
1035         }
1036
1037         if (params.num_filter_ssids == 0) {
1038                 os_free(params.filter_ssids);
1039                 params.filter_ssids = NULL;
1040         }
1041
1042         extra_ie = wpa_supplicant_extra_ies(wpa_s);
1043         if (extra_ie) {
1044                 params.extra_ies = wpabuf_head(extra_ie);
1045                 params.extra_ies_len = wpabuf_len(extra_ie);
1046         }
1047
1048         scan_params = &params;
1049
1050 scan:
1051         if (ssid || !wpa_s->first_sched_scan) {
1052                 wpa_dbg(wpa_s, MSG_DEBUG,
1053                         "Starting sched scan: interval %d timeout %d",
1054                         wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1055         } else {
1056                 wpa_dbg(wpa_s, MSG_DEBUG,
1057                         "Starting sched scan: interval %d (no timeout)",
1058                         wpa_s->sched_scan_interval);
1059         }
1060
1061         ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1062                                               wpa_s->sched_scan_interval);
1063         wpabuf_free(extra_ie);
1064         os_free(params.filter_ssids);
1065         if (ret) {
1066                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1067                 if (prev_state != wpa_s->wpa_state)
1068                         wpa_supplicant_set_state(wpa_s, prev_state);
1069                 return ret;
1070         }
1071
1072         /* If we have more SSIDs to scan, add a timeout so we scan them too */
1073         if (ssid || !wpa_s->first_sched_scan) {
1074                 wpa_s->sched_scan_timed_out = 0;
1075                 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1076                                        wpa_supplicant_sched_scan_timeout,
1077                                        wpa_s, NULL);
1078                 wpa_s->first_sched_scan = 0;
1079                 wpa_s->sched_scan_timeout /= 2;
1080                 wpa_s->sched_scan_interval *= 2;
1081         }
1082
1083         return 0;
1084 }
1085
1086
1087 /**
1088  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1089  * @wpa_s: Pointer to wpa_supplicant data
1090  *
1091  * This function is used to cancel a scan request scheduled with
1092  * wpa_supplicant_req_scan().
1093  */
1094 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1095 {
1096         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1097         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1098 }
1099
1100
1101 /**
1102  * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1103  * @wpa_s: Pointer to wpa_supplicant data
1104  *
1105  * This function is used to stop a periodic scheduled scan.
1106  */
1107 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1108 {
1109         if (!wpa_s->sched_scanning)
1110                 return;
1111
1112         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1113         eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1114         wpa_supplicant_stop_sched_scan(wpa_s);
1115 }
1116
1117
1118 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1119                                     int scanning)
1120 {
1121         if (wpa_s->scanning != scanning) {
1122                 wpa_s->scanning = scanning;
1123                 wpas_notify_scanning(wpa_s);
1124         }
1125 }
1126
1127
1128 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1129 {
1130         int rate = 0;
1131         const u8 *ie;
1132         int i;
1133
1134         ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1135         for (i = 0; ie && i < ie[1]; i++) {
1136                 if ((ie[i + 2] & 0x7f) > rate)
1137                         rate = ie[i + 2] & 0x7f;
1138         }
1139
1140         ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1141         for (i = 0; ie && i < ie[1]; i++) {
1142                 if ((ie[i + 2] & 0x7f) > rate)
1143                         rate = ie[i + 2] & 0x7f;
1144         }
1145
1146         return rate;
1147 }
1148
1149
1150 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1151 {
1152         const u8 *end, *pos;
1153
1154         pos = (const u8 *) (res + 1);
1155         end = pos + res->ie_len;
1156
1157         while (pos + 1 < end) {
1158                 if (pos + 2 + pos[1] > end)
1159                         break;
1160                 if (pos[0] == ie)
1161                         return pos;
1162                 pos += 2 + pos[1];
1163         }
1164
1165         return NULL;
1166 }
1167
1168
1169 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1170                                   u32 vendor_type)
1171 {
1172         const u8 *end, *pos;
1173
1174         pos = (const u8 *) (res + 1);
1175         end = pos + res->ie_len;
1176
1177         while (pos + 1 < end) {
1178                 if (pos + 2 + pos[1] > end)
1179                         break;
1180                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1181                     vendor_type == WPA_GET_BE32(&pos[2]))
1182                         return pos;
1183                 pos += 2 + pos[1];
1184         }
1185
1186         return NULL;
1187 }
1188
1189
1190 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1191                                              u32 vendor_type)
1192 {
1193         struct wpabuf *buf;
1194         const u8 *end, *pos;
1195
1196         buf = wpabuf_alloc(res->ie_len);
1197         if (buf == NULL)
1198                 return NULL;
1199
1200         pos = (const u8 *) (res + 1);
1201         end = pos + res->ie_len;
1202
1203         while (pos + 1 < end) {
1204                 if (pos + 2 + pos[1] > end)
1205                         break;
1206                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1207                     vendor_type == WPA_GET_BE32(&pos[2]))
1208                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1209                 pos += 2 + pos[1];
1210         }
1211
1212         if (wpabuf_len(buf) == 0) {
1213                 wpabuf_free(buf);
1214                 buf = NULL;
1215         }
1216
1217         return buf;
1218 }
1219
1220
1221 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1222         const struct wpa_scan_res *res, u32 vendor_type)
1223 {
1224         struct wpabuf *buf;
1225         const u8 *end, *pos;
1226
1227         if (res->beacon_ie_len == 0)
1228                 return NULL;
1229         buf = wpabuf_alloc(res->beacon_ie_len);
1230         if (buf == NULL)
1231                 return NULL;
1232
1233         pos = (const u8 *) (res + 1);
1234         pos += res->ie_len;
1235         end = pos + res->beacon_ie_len;
1236
1237         while (pos + 1 < end) {
1238                 if (pos + 2 + pos[1] > end)
1239                         break;
1240                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1241                     vendor_type == WPA_GET_BE32(&pos[2]))
1242                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1243                 pos += 2 + pos[1];
1244         }
1245
1246         if (wpabuf_len(buf) == 0) {
1247                 wpabuf_free(buf);
1248                 buf = NULL;
1249         }
1250
1251         return buf;
1252 }
1253
1254
1255 /*
1256  * Channels with a great SNR can operate at full rate. What is a great SNR?
1257  * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1258  * rule of thumb is that any SNR above 20 is good." This one
1259  * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1260  * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1261  * conservative value.
1262  */
1263 #define GREAT_SNR 30
1264
1265 /* Compare function for sorting scan results. Return >0 if @b is considered
1266  * better. */
1267 static int wpa_scan_result_compar(const void *a, const void *b)
1268 {
1269 #define IS_5GHZ(n) (n > 4000)
1270 #define MIN(a,b) a < b ? a : b
1271         struct wpa_scan_res **_wa = (void *) a;
1272         struct wpa_scan_res **_wb = (void *) b;
1273         struct wpa_scan_res *wa = *_wa;
1274         struct wpa_scan_res *wb = *_wb;
1275         int wpa_a, wpa_b, maxrate_a, maxrate_b;
1276         int snr_a, snr_b;
1277
1278         /* WPA/WPA2 support preferred */
1279         wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1280                 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1281         wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1282                 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1283
1284         if (wpa_b && !wpa_a)
1285                 return 1;
1286         if (!wpa_b && wpa_a)
1287                 return -1;
1288
1289         /* privacy support preferred */
1290         if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1291             (wb->caps & IEEE80211_CAP_PRIVACY))
1292                 return 1;
1293         if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1294             (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1295                 return -1;
1296
1297         if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1298             !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1299                 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1300                 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1301         } else {
1302                 /* Not suitable information to calculate SNR, so use level */
1303                 snr_a = wa->level;
1304                 snr_b = wb->level;
1305         }
1306
1307         /* best/max rate preferred if SNR close enough */
1308         if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1309             (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1310                 maxrate_a = wpa_scan_get_max_rate(wa);
1311                 maxrate_b = wpa_scan_get_max_rate(wb);
1312                 if (maxrate_a != maxrate_b)
1313                         return maxrate_b - maxrate_a;
1314                 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1315                         return IS_5GHZ(wa->freq) ? -1 : 1;
1316         }
1317
1318         /* use freq for channel preference */
1319
1320         /* all things being equal, use SNR; if SNRs are
1321          * identical, use quality values since some drivers may only report
1322          * that value and leave the signal level zero */
1323         if (snr_b == snr_a)
1324                 return wb->qual - wa->qual;
1325         return snr_b - snr_a;
1326 #undef MIN
1327 #undef IS_5GHZ
1328 }
1329
1330
1331 #ifdef CONFIG_WPS
1332 /* Compare function for sorting scan results when searching a WPS AP for
1333  * provisioning. Return >0 if @b is considered better. */
1334 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1335 {
1336         struct wpa_scan_res **_wa = (void *) a;
1337         struct wpa_scan_res **_wb = (void *) b;
1338         struct wpa_scan_res *wa = *_wa;
1339         struct wpa_scan_res *wb = *_wb;
1340         int uses_wps_a, uses_wps_b;
1341         struct wpabuf *wps_a, *wps_b;
1342         int res;
1343
1344         /* Optimization - check WPS IE existence before allocated memory and
1345          * doing full reassembly. */
1346         uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1347         uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1348         if (uses_wps_a && !uses_wps_b)
1349                 return -1;
1350         if (!uses_wps_a && uses_wps_b)
1351                 return 1;
1352
1353         if (uses_wps_a && uses_wps_b) {
1354                 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1355                 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1356                 res = wps_ap_priority_compar(wps_a, wps_b);
1357                 wpabuf_free(wps_a);
1358                 wpabuf_free(wps_b);
1359                 if (res)
1360                         return res;
1361         }
1362
1363         /*
1364          * Do not use current AP security policy as a sorting criteria during
1365          * WPS provisioning step since the AP may get reconfigured at the
1366          * completion of provisioning.
1367          */
1368
1369         /* all things being equal, use signal level; if signal levels are
1370          * identical, use quality values since some drivers may only report
1371          * that value and leave the signal level zero */
1372         if (wb->level == wa->level)
1373                 return wb->qual - wa->qual;
1374         return wb->level - wa->level;
1375 }
1376 #endif /* CONFIG_WPS */
1377
1378
1379 static void dump_scan_res(struct wpa_scan_results *scan_res)
1380 {
1381 #ifndef CONFIG_NO_STDOUT_DEBUG
1382         size_t i;
1383
1384         if (scan_res->res == NULL || scan_res->num == 0)
1385                 return;
1386
1387         wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1388
1389         for (i = 0; i < scan_res->num; i++) {
1390                 struct wpa_scan_res *r = scan_res->res[i];
1391                 u8 *pos;
1392                 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1393                     == WPA_SCAN_LEVEL_DBM) {
1394                         int snr = r->level - r->noise;
1395                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1396                                    "noise=%d level=%d snr=%d%s flags=0x%x",
1397                                    MAC2STR(r->bssid), r->freq, r->qual,
1398                                    r->noise, r->level, snr,
1399                                    snr >= GREAT_SNR ? "*" : "", r->flags);
1400                 } else {
1401                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1402                                    "noise=%d level=%d flags=0x%x",
1403                                    MAC2STR(r->bssid), r->freq, r->qual,
1404                                    r->noise, r->level, r->flags);
1405                 }
1406                 pos = (u8 *) (r + 1);
1407                 if (r->ie_len)
1408                         wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1409                 pos += r->ie_len;
1410                 if (r->beacon_ie_len)
1411                         wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1412                                     pos, r->beacon_ie_len);
1413         }
1414 #endif /* CONFIG_NO_STDOUT_DEBUG */
1415 }
1416
1417
1418 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1419                                       const u8 *bssid)
1420 {
1421         size_t i;
1422
1423         if (wpa_s->bssid_filter == NULL)
1424                 return 1;
1425
1426         for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1427                 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1428                               ETH_ALEN) == 0)
1429                         return 1;
1430         }
1431
1432         return 0;
1433 }
1434
1435
1436 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1437                             struct wpa_scan_results *res)
1438 {
1439         size_t i, j;
1440
1441         if (wpa_s->bssid_filter == NULL)
1442                 return;
1443
1444         for (i = 0, j = 0; i < res->num; i++) {
1445                 if (wpa_supplicant_filter_bssid_match(wpa_s,
1446                                                       res->res[i]->bssid)) {
1447                         res->res[j++] = res->res[i];
1448                 } else {
1449                         os_free(res->res[i]);
1450                         res->res[i] = NULL;
1451                 }
1452         }
1453
1454         if (res->num != j) {
1455                 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1456                            (int) (res->num - j));
1457                 res->num = j;
1458         }
1459 }
1460
1461
1462 /**
1463  * wpa_supplicant_get_scan_results - Get scan results
1464  * @wpa_s: Pointer to wpa_supplicant data
1465  * @info: Information about what was scanned or %NULL if not available
1466  * @new_scan: Whether a new scan was performed
1467  * Returns: Scan results, %NULL on failure
1468  *
1469  * This function request the current scan results from the driver and updates
1470  * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1471  * results with wpa_scan_results_free().
1472  */
1473 struct wpa_scan_results *
1474 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1475                                 struct scan_info *info, int new_scan)
1476 {
1477         struct wpa_scan_results *scan_res;
1478         size_t i;
1479         int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1480
1481         scan_res = wpa_drv_get_scan_results2(wpa_s);
1482         if (scan_res == NULL) {
1483                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1484                 return NULL;
1485         }
1486         filter_scan_res(wpa_s, scan_res);
1487
1488 #ifdef CONFIG_WPS
1489         if (wpas_wps_in_progress(wpa_s)) {
1490                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1491                         "provisioning rules");
1492                 compar = wpa_scan_result_wps_compar;
1493         }
1494 #endif /* CONFIG_WPS */
1495
1496         qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1497               compar);
1498         dump_scan_res(scan_res);
1499
1500         wpa_bss_update_start(wpa_s);
1501         for (i = 0; i < scan_res->num; i++)
1502                 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1503         wpa_bss_update_end(wpa_s, info, new_scan);
1504
1505         return scan_res;
1506 }
1507
1508
1509 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1510 {
1511         struct wpa_scan_results *scan_res;
1512         scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1513         if (scan_res == NULL)
1514                 return -1;
1515         wpa_scan_results_free(scan_res);
1516
1517         return 0;
1518 }