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