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