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