Document wpa_s->scan_req
[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         enum scan_req_type scan_req = NORMAL_SCAN_REQ;
509         int ret;
510         struct wpabuf *extra_ie = NULL;
511         struct wpa_driver_scan_params params;
512         struct wpa_driver_scan_params *scan_params;
513         size_t max_ssids;
514         enum wpa_states prev_state;
515
516         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
517                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
518                 return;
519         }
520
521         if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
522                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
523                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
524                 return;
525         }
526
527         if (!wpa_supplicant_enabled_networks(wpa_s) &&
528             wpa_s->scan_req == NORMAL_SCAN_REQ) {
529                 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
530                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
531 #ifdef CONFIG_P2P
532                 wpa_s->sta_scan_pending = 0;
533 #endif /* CONFIG_P2P */
534                 return;
535         }
536
537         if (wpa_s->conf->ap_scan != 0 &&
538             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
539                 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
540                         "overriding ap_scan configuration");
541                 wpa_s->conf->ap_scan = 0;
542                 wpas_notify_ap_scan_changed(wpa_s);
543         }
544
545         if (wpa_s->conf->ap_scan == 0) {
546                 wpa_supplicant_gen_assoc_event(wpa_s);
547                 return;
548         }
549
550 #ifdef CONFIG_P2P
551         if (wpas_p2p_in_progress(wpa_s)) {
552                 if (wpa_s->sta_scan_pending &&
553                     wpas_p2p_in_progress(wpa_s) == 2 &&
554                     wpa_s->global->p2p_cb_on_scan_complete) {
555                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
556                                 "mode scan during P2P search");
557                 } else {
558                         wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
559                                 "while P2P operation is in progress");
560                         wpa_s->sta_scan_pending = 1;
561                         wpa_supplicant_req_scan(wpa_s, 5, 0);
562                         return;
563                 }
564         }
565 #endif /* CONFIG_P2P */
566
567         if (wpa_s->conf->ap_scan == 2)
568                 max_ssids = 1;
569         else {
570                 max_ssids = wpa_s->max_scan_ssids;
571                 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
572                         max_ssids = WPAS_MAX_SCAN_SSIDS;
573         }
574
575         scan_req = wpa_s->scan_req;
576         wpa_s->scan_req = NORMAL_SCAN_REQ;
577
578         os_memset(&params, 0, sizeof(params));
579
580         prev_state = wpa_s->wpa_state;
581         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
582             wpa_s->wpa_state == WPA_INACTIVE)
583                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
584
585         /*
586          * If autoscan has set its own scanning parameters
587          */
588         if (wpa_s->autoscan_params != NULL) {
589                 scan_params = wpa_s->autoscan_params;
590                 goto scan;
591         }
592
593         if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
594                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
595                         if (ssid == wpa_s->connect_without_scan)
596                                 break;
597                 }
598                 wpa_s->connect_without_scan = NULL;
599                 if (ssid) {
600                         wpa_printf(MSG_DEBUG, "Start a pre-selected network "
601                                    "without scan step");
602                         wpa_supplicant_associate(wpa_s, NULL, ssid);
603                         return;
604                 }
605         }
606
607 #ifdef CONFIG_P2P
608         if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
609             wpa_s->go_params) {
610                 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
611                            "P2P group formation");
612                 params.ssids[0].ssid = wpa_s->go_params->ssid;
613                 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
614                 params.num_ssids = 1;
615                 goto ssid_list_set;
616         }
617 #endif /* CONFIG_P2P */
618
619         /* Find the starting point from which to continue scanning */
620         ssid = wpa_s->conf->ssid;
621         if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
622                 while (ssid) {
623                         if (ssid == wpa_s->prev_scan_ssid) {
624                                 ssid = ssid->next;
625                                 break;
626                         }
627                         ssid = ssid->next;
628                 }
629         }
630
631         if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
632                 wpa_s->connect_without_scan = NULL;
633                 wpa_s->prev_scan_wildcard = 0;
634                 wpa_supplicant_assoc_try(wpa_s, ssid);
635                 return;
636         } else if (wpa_s->conf->ap_scan == 2) {
637                 /*
638                  * User-initiated scan request in ap_scan == 2; scan with
639                  * wildcard SSID.
640                  */
641                 ssid = NULL;
642         } else {
643                 struct wpa_ssid *start = ssid, *tssid;
644                 int freqs_set = 0;
645                 if (ssid == NULL && max_ssids > 1)
646                         ssid = wpa_s->conf->ssid;
647                 while (ssid) {
648                         if (!wpas_network_disabled(wpa_s, ssid) &&
649                             ssid->scan_ssid) {
650                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
651                                                   ssid->ssid, ssid->ssid_len);
652                                 params.ssids[params.num_ssids].ssid =
653                                         ssid->ssid;
654                                 params.ssids[params.num_ssids].ssid_len =
655                                         ssid->ssid_len;
656                                 params.num_ssids++;
657                                 if (params.num_ssids + 1 >= max_ssids)
658                                         break;
659                         }
660                         ssid = ssid->next;
661                         if (ssid == start)
662                                 break;
663                         if (ssid == NULL && max_ssids > 1 &&
664                             start != wpa_s->conf->ssid)
665                                 ssid = wpa_s->conf->ssid;
666                 }
667
668                 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
669                         if (wpas_network_disabled(wpa_s, tssid))
670                                 continue;
671                         if ((params.freqs || !freqs_set) && tssid->scan_freq) {
672                                 int_array_concat(&params.freqs,
673                                                  tssid->scan_freq);
674                         } else {
675                                 os_free(params.freqs);
676                                 params.freqs = NULL;
677                         }
678                         freqs_set = 1;
679                 }
680                 int_array_sort_unique(params.freqs);
681         }
682
683         if (ssid && max_ssids == 1) {
684                 /*
685                  * If the driver is limited to 1 SSID at a time interleave
686                  * wildcard SSID scans with specific SSID scans to avoid
687                  * waiting a long time for a wildcard scan.
688                  */
689                 if (!wpa_s->prev_scan_wildcard) {
690                         params.ssids[0].ssid = NULL;
691                         params.ssids[0].ssid_len = 0;
692                         wpa_s->prev_scan_wildcard = 1;
693                         wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
694                                 "wildcard SSID (Interleave with specific)");
695                 } else {
696                         wpa_s->prev_scan_ssid = ssid;
697                         wpa_s->prev_scan_wildcard = 0;
698                         wpa_dbg(wpa_s, MSG_DEBUG,
699                                 "Starting AP scan for specific SSID: %s",
700                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
701                 }
702         } else if (ssid) {
703                 /* max_ssids > 1 */
704
705                 wpa_s->prev_scan_ssid = ssid;
706                 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
707                         "the scan request");
708                 params.num_ssids++;
709         } else {
710                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
711                 params.num_ssids++;
712                 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
713                         "SSID");
714         }
715 #ifdef CONFIG_P2P
716 ssid_list_set:
717 #endif /* CONFIG_P2P */
718
719         wpa_supplicant_optimize_freqs(wpa_s, &params);
720         extra_ie = wpa_supplicant_extra_ies(wpa_s);
721
722 #ifdef CONFIG_HS20
723         if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
724                 wpas_hs20_add_indication(extra_ie);
725 #endif /* CONFIG_HS20 */
726
727         if (params.freqs == NULL && wpa_s->next_scan_freqs) {
728                 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
729                         "generated frequency list");
730                 params.freqs = wpa_s->next_scan_freqs;
731         } else
732                 os_free(wpa_s->next_scan_freqs);
733         wpa_s->next_scan_freqs = NULL;
734
735         params.filter_ssids = wpa_supplicant_build_filter_ssids(
736                 wpa_s->conf, &params.num_filter_ssids);
737         if (extra_ie) {
738                 params.extra_ies = wpabuf_head(extra_ie);
739                 params.extra_ies_len = wpabuf_len(extra_ie);
740         }
741
742 #ifdef CONFIG_P2P
743         if (wpa_s->p2p_in_provisioning ||
744             (wpa_s->show_group_started && wpa_s->go_params)) {
745                 /*
746                  * The interface may not yet be in P2P mode, so we have to
747                  * explicitly request P2P probe to disable CCK rates.
748                  */
749                 params.p2p_probe = 1;
750         }
751 #endif /* CONFIG_P2P */
752
753         scan_params = &params;
754
755 scan:
756 #ifdef CONFIG_P2P
757         /*
758          * If the driver does not support multi-channel concurrency and a
759          * virtual interface that shares the same radio with the wpa_s interface
760          * is operating there may not be need to scan other channels apart from
761          * the current operating channel on the other virtual interface. Filter
762          * out other channels in case we are trying to find a connection for a
763          * station interface when we are not configured to prefer station
764          * connection and a concurrent operation is already in process.
765          */
766         if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
767             !scan_params->freqs && !params.freqs &&
768             wpas_is_p2p_prioritized(wpa_s) &&
769             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
770             wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
771             non_p2p_network_enabled(wpa_s)) {
772                 int freq = shared_vif_oper_freq(wpa_s);
773                 if (freq > 0) {
774                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
775                                 "operating channel (%d MHz) since driver does "
776                                 "not support multi-channel concurrency", freq);
777                         params.freqs = os_zalloc(sizeof(int) * 2);
778                         if (params.freqs)
779                                 params.freqs[0] = freq;
780                         scan_params->freqs = params.freqs;
781                 }
782         }
783 #endif /* CONFIG_P2P */
784
785         ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
786
787         wpabuf_free(extra_ie);
788         os_free(params.freqs);
789         os_free(params.filter_ssids);
790
791         if (ret) {
792                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
793                 if (prev_state != wpa_s->wpa_state)
794                         wpa_supplicant_set_state(wpa_s, prev_state);
795                 /* Restore scan_req since we will try to scan again */
796                 wpa_s->scan_req = scan_req;
797                 wpa_supplicant_req_scan(wpa_s, 1, 0);
798         } else {
799                 wpa_s->scan_for_connection = 0;
800         }
801 }
802
803
804 /**
805  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
806  * @wpa_s: Pointer to wpa_supplicant data
807  * @sec: Number of seconds after which to scan
808  * @usec: Number of microseconds after which to scan
809  *
810  * This function is used to schedule a scan for neighboring access points after
811  * the specified time.
812  */
813 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
814 {
815         /* If there's at least one network that should be specifically scanned
816          * then don't cancel the scan and reschedule.  Some drivers do
817          * background scanning which generates frequent scan results, and that
818          * causes the specific SSID scan to get continually pushed back and
819          * never happen, which causes hidden APs to never get probe-scanned.
820          */
821         if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
822             wpa_s->conf->ap_scan == 1) {
823                 struct wpa_ssid *ssid = wpa_s->conf->ssid;
824
825                 while (ssid) {
826                         if (!wpas_network_disabled(wpa_s, ssid) &&
827                             ssid->scan_ssid)
828                                 break;
829                         ssid = ssid->next;
830                 }
831                 if (ssid) {
832                         wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
833                                 "ensure that specific SSID scans occur");
834                         return;
835                 }
836         }
837
838         wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
839                 sec, usec);
840         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
841         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
842 }
843
844
845 /**
846  * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
847  * @wpa_s: Pointer to wpa_supplicant data
848  * @sec: Number of seconds after which to scan
849  * @usec: Number of microseconds after which to scan
850  *
851  * This function is used to schedule periodic scans for neighboring
852  * access points after the specified time.
853  */
854 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
855                                       int sec, int usec)
856 {
857         if (!wpa_s->sched_scan_supported)
858                 return -1;
859
860         eloop_register_timeout(sec, usec,
861                                wpa_supplicant_delayed_sched_scan_timeout,
862                                wpa_s, NULL);
863
864         return 0;
865 }
866
867
868 /**
869  * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
870  * @wpa_s: Pointer to wpa_supplicant data
871  *
872  * This function is used to schedule periodic scans for neighboring
873  * access points repeating the scan continuously.
874  */
875 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
876 {
877         struct wpa_driver_scan_params params;
878         struct wpa_driver_scan_params *scan_params;
879         enum wpa_states prev_state;
880         struct wpa_ssid *ssid = NULL;
881         struct wpabuf *extra_ie = NULL;
882         int ret;
883         unsigned int max_sched_scan_ssids;
884         int wildcard = 0;
885         int need_ssids;
886
887         if (!wpa_s->sched_scan_supported)
888                 return -1;
889
890         if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
891                 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
892         else
893                 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
894         if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
895                 return -1;
896
897         if (wpa_s->sched_scanning) {
898                 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
899                 return 0;
900         }
901
902         need_ssids = 0;
903         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
904                 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
905                         /* Use wildcard SSID to find this network */
906                         wildcard = 1;
907                 } else if (!wpas_network_disabled(wpa_s, ssid) &&
908                            ssid->ssid_len)
909                         need_ssids++;
910
911 #ifdef CONFIG_WPS
912                 if (!wpas_network_disabled(wpa_s, ssid) &&
913                     ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
914                         /*
915                          * Normal scan is more reliable and faster for WPS
916                          * operations and since these are for short periods of
917                          * time, the benefit of trying to use sched_scan would
918                          * be limited.
919                          */
920                         wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
921                                 "sched_scan for WPS");
922                         return -1;
923                 }
924 #endif /* CONFIG_WPS */
925         }
926         if (wildcard)
927                 need_ssids++;
928
929         if (wpa_s->normal_scans < 3 &&
930             (need_ssids <= wpa_s->max_scan_ssids ||
931              wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
932                 /*
933                  * When normal scan can speed up operations, use that for the
934                  * first operations before starting the sched_scan to allow
935                  * user space sleep more. We do this only if the normal scan
936                  * has functionality that is suitable for this or if the
937                  * sched_scan does not have better support for multiple SSIDs.
938                  */
939                 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
940                         "sched_scan for initial scans (normal_scans=%d)",
941                         wpa_s->normal_scans);
942                 return -1;
943         }
944
945         os_memset(&params, 0, sizeof(params));
946
947         /* If we can't allocate space for the filters, we just don't filter */
948         params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
949                                         sizeof(struct wpa_driver_scan_filter));
950
951         prev_state = wpa_s->wpa_state;
952         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
953             wpa_s->wpa_state == WPA_INACTIVE)
954                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
955
956         if (wpa_s->autoscan_params != NULL) {
957                 scan_params = wpa_s->autoscan_params;
958                 goto scan;
959         }
960
961         /* Find the starting point from which to continue scanning */
962         ssid = wpa_s->conf->ssid;
963         if (wpa_s->prev_sched_ssid) {
964                 while (ssid) {
965                         if (ssid == wpa_s->prev_sched_ssid) {
966                                 ssid = ssid->next;
967                                 break;
968                         }
969                         ssid = ssid->next;
970                 }
971         }
972
973         if (!ssid || !wpa_s->prev_sched_ssid) {
974                 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
975
976                 if (wpa_s->sched_scan_interval == 0)
977                         wpa_s->sched_scan_interval = 10;
978                 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
979                 wpa_s->first_sched_scan = 1;
980                 ssid = wpa_s->conf->ssid;
981                 wpa_s->prev_sched_ssid = ssid;
982         }
983
984         if (wildcard) {
985                 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
986                 params.num_ssids++;
987         }
988
989         while (ssid) {
990                 if (wpas_network_disabled(wpa_s, ssid))
991                         goto next;
992
993                 if (params.num_filter_ssids < wpa_s->max_match_sets &&
994                     params.filter_ssids && ssid->ssid && ssid->ssid_len) {
995                         wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
996                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
997                         os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
998                                   ssid->ssid, ssid->ssid_len);
999                         params.filter_ssids[params.num_filter_ssids].ssid_len =
1000                                 ssid->ssid_len;
1001                         params.num_filter_ssids++;
1002                 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1003                 {
1004                         wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1005                                 "filter for sched_scan - drop filter");
1006                         os_free(params.filter_ssids);
1007                         params.filter_ssids = NULL;
1008                         params.num_filter_ssids = 0;
1009                 }
1010
1011                 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1012                         if (params.num_ssids == max_sched_scan_ssids)
1013                                 break; /* only room for broadcast SSID */
1014                         wpa_dbg(wpa_s, MSG_DEBUG,
1015                                 "add to active scan ssid: %s",
1016                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1017                         params.ssids[params.num_ssids].ssid =
1018                                 ssid->ssid;
1019                         params.ssids[params.num_ssids].ssid_len =
1020                                 ssid->ssid_len;
1021                         params.num_ssids++;
1022                         if (params.num_ssids >= max_sched_scan_ssids) {
1023                                 wpa_s->prev_sched_ssid = ssid;
1024                                 do {
1025                                         ssid = ssid->next;
1026                                 } while (ssid &&
1027                                          (wpas_network_disabled(wpa_s, ssid) ||
1028                                           !ssid->scan_ssid));
1029                                 break;
1030                         }
1031                 }
1032
1033         next:
1034                 wpa_s->prev_sched_ssid = ssid;
1035                 ssid = ssid->next;
1036         }
1037
1038         if (params.num_filter_ssids == 0) {
1039                 os_free(params.filter_ssids);
1040                 params.filter_ssids = NULL;
1041         }
1042
1043         extra_ie = wpa_supplicant_extra_ies(wpa_s);
1044         if (extra_ie) {
1045                 params.extra_ies = wpabuf_head(extra_ie);
1046                 params.extra_ies_len = wpabuf_len(extra_ie);
1047         }
1048
1049         scan_params = &params;
1050
1051 scan:
1052         if (ssid || !wpa_s->first_sched_scan) {
1053                 wpa_dbg(wpa_s, MSG_DEBUG,
1054                         "Starting sched scan: interval %d timeout %d",
1055                         wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1056         } else {
1057                 wpa_dbg(wpa_s, MSG_DEBUG,
1058                         "Starting sched scan: interval %d (no timeout)",
1059                         wpa_s->sched_scan_interval);
1060         }
1061
1062         ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1063                                               wpa_s->sched_scan_interval);
1064         wpabuf_free(extra_ie);
1065         os_free(params.filter_ssids);
1066         if (ret) {
1067                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1068                 if (prev_state != wpa_s->wpa_state)
1069                         wpa_supplicant_set_state(wpa_s, prev_state);
1070                 return ret;
1071         }
1072
1073         /* If we have more SSIDs to scan, add a timeout so we scan them too */
1074         if (ssid || !wpa_s->first_sched_scan) {
1075                 wpa_s->sched_scan_timed_out = 0;
1076                 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1077                                        wpa_supplicant_sched_scan_timeout,
1078                                        wpa_s, NULL);
1079                 wpa_s->first_sched_scan = 0;
1080                 wpa_s->sched_scan_timeout /= 2;
1081                 wpa_s->sched_scan_interval *= 2;
1082         }
1083
1084         return 0;
1085 }
1086
1087
1088 /**
1089  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1090  * @wpa_s: Pointer to wpa_supplicant data
1091  *
1092  * This function is used to cancel a scan request scheduled with
1093  * wpa_supplicant_req_scan().
1094  */
1095 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1096 {
1097         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1098         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1099 }
1100
1101
1102 /**
1103  * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1104  * @wpa_s: Pointer to wpa_supplicant data
1105  *
1106  * This function is used to stop a periodic scheduled scan.
1107  */
1108 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1109 {
1110         if (!wpa_s->sched_scanning)
1111                 return;
1112
1113         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1114         eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1115         wpa_supplicant_stop_sched_scan(wpa_s);
1116 }
1117
1118
1119 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1120                                     int scanning)
1121 {
1122         if (wpa_s->scanning != scanning) {
1123                 wpa_s->scanning = scanning;
1124                 wpas_notify_scanning(wpa_s);
1125         }
1126 }
1127
1128
1129 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1130 {
1131         int rate = 0;
1132         const u8 *ie;
1133         int i;
1134
1135         ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1136         for (i = 0; ie && i < ie[1]; i++) {
1137                 if ((ie[i + 2] & 0x7f) > rate)
1138                         rate = ie[i + 2] & 0x7f;
1139         }
1140
1141         ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1142         for (i = 0; ie && i < ie[1]; i++) {
1143                 if ((ie[i + 2] & 0x7f) > rate)
1144                         rate = ie[i + 2] & 0x7f;
1145         }
1146
1147         return rate;
1148 }
1149
1150
1151 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1152 {
1153         const u8 *end, *pos;
1154
1155         pos = (const u8 *) (res + 1);
1156         end = pos + res->ie_len;
1157
1158         while (pos + 1 < end) {
1159                 if (pos + 2 + pos[1] > end)
1160                         break;
1161                 if (pos[0] == ie)
1162                         return pos;
1163                 pos += 2 + pos[1];
1164         }
1165
1166         return NULL;
1167 }
1168
1169
1170 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1171                                   u32 vendor_type)
1172 {
1173         const u8 *end, *pos;
1174
1175         pos = (const u8 *) (res + 1);
1176         end = pos + res->ie_len;
1177
1178         while (pos + 1 < end) {
1179                 if (pos + 2 + pos[1] > end)
1180                         break;
1181                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1182                     vendor_type == WPA_GET_BE32(&pos[2]))
1183                         return pos;
1184                 pos += 2 + pos[1];
1185         }
1186
1187         return NULL;
1188 }
1189
1190
1191 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1192                                              u32 vendor_type)
1193 {
1194         struct wpabuf *buf;
1195         const u8 *end, *pos;
1196
1197         buf = wpabuf_alloc(res->ie_len);
1198         if (buf == NULL)
1199                 return NULL;
1200
1201         pos = (const u8 *) (res + 1);
1202         end = pos + res->ie_len;
1203
1204         while (pos + 1 < end) {
1205                 if (pos + 2 + pos[1] > end)
1206                         break;
1207                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1208                     vendor_type == WPA_GET_BE32(&pos[2]))
1209                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1210                 pos += 2 + pos[1];
1211         }
1212
1213         if (wpabuf_len(buf) == 0) {
1214                 wpabuf_free(buf);
1215                 buf = NULL;
1216         }
1217
1218         return buf;
1219 }
1220
1221
1222 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1223         const struct wpa_scan_res *res, u32 vendor_type)
1224 {
1225         struct wpabuf *buf;
1226         const u8 *end, *pos;
1227
1228         if (res->beacon_ie_len == 0)
1229                 return NULL;
1230         buf = wpabuf_alloc(res->beacon_ie_len);
1231         if (buf == NULL)
1232                 return NULL;
1233
1234         pos = (const u8 *) (res + 1);
1235         pos += res->ie_len;
1236         end = pos + res->beacon_ie_len;
1237
1238         while (pos + 1 < end) {
1239                 if (pos + 2 + pos[1] > end)
1240                         break;
1241                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1242                     vendor_type == WPA_GET_BE32(&pos[2]))
1243                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1244                 pos += 2 + pos[1];
1245         }
1246
1247         if (wpabuf_len(buf) == 0) {
1248                 wpabuf_free(buf);
1249                 buf = NULL;
1250         }
1251
1252         return buf;
1253 }
1254
1255
1256 /*
1257  * Channels with a great SNR can operate at full rate. What is a great SNR?
1258  * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1259  * rule of thumb is that any SNR above 20 is good." This one
1260  * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1261  * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1262  * conservative value.
1263  */
1264 #define GREAT_SNR 30
1265
1266 /* Compare function for sorting scan results. Return >0 if @b is considered
1267  * better. */
1268 static int wpa_scan_result_compar(const void *a, const void *b)
1269 {
1270 #define IS_5GHZ(n) (n > 4000)
1271 #define MIN(a,b) a < b ? a : b
1272         struct wpa_scan_res **_wa = (void *) a;
1273         struct wpa_scan_res **_wb = (void *) b;
1274         struct wpa_scan_res *wa = *_wa;
1275         struct wpa_scan_res *wb = *_wb;
1276         int wpa_a, wpa_b, maxrate_a, maxrate_b;
1277         int snr_a, snr_b;
1278
1279         /* WPA/WPA2 support preferred */
1280         wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1281                 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1282         wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1283                 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1284
1285         if (wpa_b && !wpa_a)
1286                 return 1;
1287         if (!wpa_b && wpa_a)
1288                 return -1;
1289
1290         /* privacy support preferred */
1291         if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1292             (wb->caps & IEEE80211_CAP_PRIVACY))
1293                 return 1;
1294         if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1295             (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1296                 return -1;
1297
1298         if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1299             !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1300                 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1301                 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1302         } else {
1303                 /* Not suitable information to calculate SNR, so use level */
1304                 snr_a = wa->level;
1305                 snr_b = wb->level;
1306         }
1307
1308         /* best/max rate preferred if SNR close enough */
1309         if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1310             (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1311                 maxrate_a = wpa_scan_get_max_rate(wa);
1312                 maxrate_b = wpa_scan_get_max_rate(wb);
1313                 if (maxrate_a != maxrate_b)
1314                         return maxrate_b - maxrate_a;
1315                 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1316                         return IS_5GHZ(wa->freq) ? -1 : 1;
1317         }
1318
1319         /* use freq for channel preference */
1320
1321         /* all things being equal, use SNR; if SNRs are
1322          * identical, use quality values since some drivers may only report
1323          * that value and leave the signal level zero */
1324         if (snr_b == snr_a)
1325                 return wb->qual - wa->qual;
1326         return snr_b - snr_a;
1327 #undef MIN
1328 #undef IS_5GHZ
1329 }
1330
1331
1332 #ifdef CONFIG_WPS
1333 /* Compare function for sorting scan results when searching a WPS AP for
1334  * provisioning. Return >0 if @b is considered better. */
1335 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1336 {
1337         struct wpa_scan_res **_wa = (void *) a;
1338         struct wpa_scan_res **_wb = (void *) b;
1339         struct wpa_scan_res *wa = *_wa;
1340         struct wpa_scan_res *wb = *_wb;
1341         int uses_wps_a, uses_wps_b;
1342         struct wpabuf *wps_a, *wps_b;
1343         int res;
1344
1345         /* Optimization - check WPS IE existence before allocated memory and
1346          * doing full reassembly. */
1347         uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1348         uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1349         if (uses_wps_a && !uses_wps_b)
1350                 return -1;
1351         if (!uses_wps_a && uses_wps_b)
1352                 return 1;
1353
1354         if (uses_wps_a && uses_wps_b) {
1355                 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1356                 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1357                 res = wps_ap_priority_compar(wps_a, wps_b);
1358                 wpabuf_free(wps_a);
1359                 wpabuf_free(wps_b);
1360                 if (res)
1361                         return res;
1362         }
1363
1364         /*
1365          * Do not use current AP security policy as a sorting criteria during
1366          * WPS provisioning step since the AP may get reconfigured at the
1367          * completion of provisioning.
1368          */
1369
1370         /* all things being equal, use signal level; if signal levels are
1371          * identical, use quality values since some drivers may only report
1372          * that value and leave the signal level zero */
1373         if (wb->level == wa->level)
1374                 return wb->qual - wa->qual;
1375         return wb->level - wa->level;
1376 }
1377 #endif /* CONFIG_WPS */
1378
1379
1380 static void dump_scan_res(struct wpa_scan_results *scan_res)
1381 {
1382 #ifndef CONFIG_NO_STDOUT_DEBUG
1383         size_t i;
1384
1385         if (scan_res->res == NULL || scan_res->num == 0)
1386                 return;
1387
1388         wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1389
1390         for (i = 0; i < scan_res->num; i++) {
1391                 struct wpa_scan_res *r = scan_res->res[i];
1392                 u8 *pos;
1393                 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1394                     == WPA_SCAN_LEVEL_DBM) {
1395                         int snr = r->level - r->noise;
1396                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1397                                    "noise=%d level=%d snr=%d%s flags=0x%x",
1398                                    MAC2STR(r->bssid), r->freq, r->qual,
1399                                    r->noise, r->level, snr,
1400                                    snr >= GREAT_SNR ? "*" : "", r->flags);
1401                 } else {
1402                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1403                                    "noise=%d level=%d flags=0x%x",
1404                                    MAC2STR(r->bssid), r->freq, r->qual,
1405                                    r->noise, r->level, r->flags);
1406                 }
1407                 pos = (u8 *) (r + 1);
1408                 if (r->ie_len)
1409                         wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1410                 pos += r->ie_len;
1411                 if (r->beacon_ie_len)
1412                         wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1413                                     pos, r->beacon_ie_len);
1414         }
1415 #endif /* CONFIG_NO_STDOUT_DEBUG */
1416 }
1417
1418
1419 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1420                                       const u8 *bssid)
1421 {
1422         size_t i;
1423
1424         if (wpa_s->bssid_filter == NULL)
1425                 return 1;
1426
1427         for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1428                 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1429                               ETH_ALEN) == 0)
1430                         return 1;
1431         }
1432
1433         return 0;
1434 }
1435
1436
1437 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1438                             struct wpa_scan_results *res)
1439 {
1440         size_t i, j;
1441
1442         if (wpa_s->bssid_filter == NULL)
1443                 return;
1444
1445         for (i = 0, j = 0; i < res->num; i++) {
1446                 if (wpa_supplicant_filter_bssid_match(wpa_s,
1447                                                       res->res[i]->bssid)) {
1448                         res->res[j++] = res->res[i];
1449                 } else {
1450                         os_free(res->res[i]);
1451                         res->res[i] = NULL;
1452                 }
1453         }
1454
1455         if (res->num != j) {
1456                 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1457                            (int) (res->num - j));
1458                 res->num = j;
1459         }
1460 }
1461
1462
1463 /**
1464  * wpa_supplicant_get_scan_results - Get scan results
1465  * @wpa_s: Pointer to wpa_supplicant data
1466  * @info: Information about what was scanned or %NULL if not available
1467  * @new_scan: Whether a new scan was performed
1468  * Returns: Scan results, %NULL on failure
1469  *
1470  * This function request the current scan results from the driver and updates
1471  * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1472  * results with wpa_scan_results_free().
1473  */
1474 struct wpa_scan_results *
1475 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1476                                 struct scan_info *info, int new_scan)
1477 {
1478         struct wpa_scan_results *scan_res;
1479         size_t i;
1480         int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1481
1482         scan_res = wpa_drv_get_scan_results2(wpa_s);
1483         if (scan_res == NULL) {
1484                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1485                 return NULL;
1486         }
1487         filter_scan_res(wpa_s, scan_res);
1488
1489 #ifdef CONFIG_WPS
1490         if (wpas_wps_in_progress(wpa_s)) {
1491                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1492                         "provisioning rules");
1493                 compar = wpa_scan_result_wps_compar;
1494         }
1495 #endif /* CONFIG_WPS */
1496
1497         qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1498               compar);
1499         dump_scan_res(scan_res);
1500
1501         wpa_bss_update_start(wpa_s);
1502         for (i = 0; i < scan_res->num; i++)
1503                 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1504         wpa_bss_update_end(wpa_s, info, new_scan);
1505
1506         return scan_res;
1507 }
1508
1509
1510 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1511 {
1512         struct wpa_scan_results *scan_res;
1513         scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1514         if (scan_res == NULL)
1515                 return -1;
1516         wpa_scan_results_free(scan_res);
1517
1518         return 0;
1519 }