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