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