OSDN Git Service

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