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