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