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