P2P: Fix WSC IE inclusion for P2P disabled case
[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_supplicant_assoc_try(wpa_s, ssid);
534                 return;
535         } else if (wpa_s->conf->ap_scan == 2) {
536                 /*
537                  * User-initiated scan request in ap_scan == 2; scan with
538                  * wildcard SSID.
539                  */
540                 ssid = NULL;
541         } else {
542                 struct wpa_ssid *start = ssid, *tssid;
543                 int freqs_set = 0;
544                 if (ssid == NULL && max_ssids > 1)
545                         ssid = wpa_s->conf->ssid;
546                 while (ssid) {
547                         if (!ssid->disabled && ssid->scan_ssid) {
548                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
549                                                   ssid->ssid, ssid->ssid_len);
550                                 params.ssids[params.num_ssids].ssid =
551                                         ssid->ssid;
552                                 params.ssids[params.num_ssids].ssid_len =
553                                         ssid->ssid_len;
554                                 params.num_ssids++;
555                                 if (params.num_ssids + 1 >= max_ssids)
556                                         break;
557                         }
558                         ssid = ssid->next;
559                         if (ssid == start)
560                                 break;
561                         if (ssid == NULL && max_ssids > 1 &&
562                             start != wpa_s->conf->ssid)
563                                 ssid = wpa_s->conf->ssid;
564                 }
565
566                 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
567                         if (tssid->disabled)
568                                 continue;
569                         if ((params.freqs || !freqs_set) && tssid->scan_freq) {
570                                 int_array_concat(&params.freqs,
571                                                  tssid->scan_freq);
572                         } else {
573                                 os_free(params.freqs);
574                                 params.freqs = NULL;
575                         }
576                         freqs_set = 1;
577                 }
578                 int_array_sort_unique(params.freqs);
579         }
580
581         if (ssid) {
582                 wpa_s->prev_scan_ssid = ssid;
583                 if (max_ssids > 1) {
584                         wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
585                                 "the scan request");
586                         params.num_ssids++;
587                 }
588                 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
589                         "SSID(s)");
590         } else {
591                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
592                 params.num_ssids++;
593                 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
594                         "SSID");
595         }
596
597         wpa_supplicant_optimize_freqs(wpa_s, &params);
598         extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
599
600         if (params.freqs == NULL && wpa_s->next_scan_freqs) {
601                 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
602                         "generated frequency list");
603                 params.freqs = wpa_s->next_scan_freqs;
604         } else
605                 os_free(wpa_s->next_scan_freqs);
606         wpa_s->next_scan_freqs = NULL;
607
608         params.filter_ssids = wpa_supplicant_build_filter_ssids(
609                 wpa_s->conf, &params.num_filter_ssids);
610         if (extra_ie) {
611                 params.extra_ies = wpabuf_head(extra_ie);
612                 params.extra_ies_len = wpabuf_len(extra_ie);
613         }
614
615 #ifdef CONFIG_P2P
616         if (wpa_s->p2p_in_provisioning) {
617                 /*
618                  * The interface may not yet be in P2P mode, so we have to
619                  * explicitly request P2P probe to disable CCK rates.
620                  */
621                 params.p2p_probe = 1;
622         }
623 #endif /* CONFIG_P2P */
624
625         ret = wpa_supplicant_trigger_scan(wpa_s, &params);
626
627         wpabuf_free(extra_ie);
628         os_free(params.freqs);
629         os_free(params.filter_ssids);
630
631         if (ret) {
632                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
633                 if (prev_state != wpa_s->wpa_state)
634                         wpa_supplicant_set_state(wpa_s, prev_state);
635                 wpa_supplicant_req_scan(wpa_s, 1, 0);
636         }
637 }
638
639
640 /**
641  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
642  * @wpa_s: Pointer to wpa_supplicant data
643  * @sec: Number of seconds after which to scan
644  * @usec: Number of microseconds after which to scan
645  *
646  * This function is used to schedule a scan for neighboring access points after
647  * the specified time.
648  */
649 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
650 {
651         /* If there's at least one network that should be specifically scanned
652          * then don't cancel the scan and reschedule.  Some drivers do
653          * background scanning which generates frequent scan results, and that
654          * causes the specific SSID scan to get continually pushed back and
655          * never happen, which causes hidden APs to never get probe-scanned.
656          */
657         if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
658             wpa_s->conf->ap_scan == 1) {
659                 struct wpa_ssid *ssid = wpa_s->conf->ssid;
660
661                 while (ssid) {
662                         if (!ssid->disabled && ssid->scan_ssid)
663                                 break;
664                         ssid = ssid->next;
665                 }
666                 if (ssid) {
667                         wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
668                                 "ensure that specific SSID scans occur");
669                         return;
670                 }
671         }
672
673         wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
674                 sec, usec);
675         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
676         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
677 }
678
679
680 /**
681  * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
682  * @wpa_s: Pointer to wpa_supplicant data
683  * @sec: Number of seconds after which to scan
684  * @usec: Number of microseconds after which to scan
685  *
686  * This function is used to schedule periodic scans for neighboring
687  * access points after the specified time.
688  */
689 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
690                                       int sec, int usec)
691 {
692         if (!wpa_s->sched_scan_supported)
693                 return -1;
694
695         eloop_register_timeout(sec, usec,
696                                wpa_supplicant_delayed_sched_scan_timeout,
697                                wpa_s, NULL);
698
699         return 0;
700 }
701
702
703 /**
704  * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
705  * @wpa_s: Pointer to wpa_supplicant data
706  *
707  * This function is used to schedule periodic scans for neighboring
708  * access points repeating the scan continuously.
709  */
710 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
711 {
712         struct wpa_driver_scan_params params;
713         enum wpa_states prev_state;
714         struct wpa_ssid *ssid;
715         struct wpabuf *wps_ie = NULL;
716         int ret;
717         unsigned int max_sched_scan_ssids;
718         int wildcard = 0;
719         int need_ssids;
720
721         if (!wpa_s->sched_scan_supported)
722                 return -1;
723
724         if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
725                 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
726         else
727                 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
728         if (max_sched_scan_ssids < 1)
729                 return -1;
730
731         if (wpa_s->sched_scanning) {
732                 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
733                 return 0;
734         }
735
736         need_ssids = 0;
737         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
738                 if (!ssid->disabled && !ssid->scan_ssid) {
739                         /* Use wildcard SSID to find this network */
740                         wildcard = 1;
741                 } else if (!ssid->disabled && ssid->ssid_len)
742                         need_ssids++;
743         }
744         if (wildcard)
745                 need_ssids++;
746
747         if (wpa_s->normal_scans < 3 &&
748             (need_ssids <= wpa_s->max_scan_ssids ||
749              wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
750                 /*
751                  * When normal scan can speed up operations, use that for the
752                  * first operations before starting the sched_scan to allow
753                  * user space sleep more. We do this only if the normal scan
754                  * has functionality that is suitable for this or if the
755                  * sched_scan does not have better support for multiple SSIDs.
756                  */
757                 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
758                         "sched_scan for initial scans (normal_scans=%d)",
759                         wpa_s->normal_scans);
760                 return -1;
761         }
762
763         os_memset(&params, 0, sizeof(params));
764
765         /* If we can't allocate space for the filters, we just don't filter */
766         params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
767                                         sizeof(struct wpa_driver_scan_filter));
768
769         prev_state = wpa_s->wpa_state;
770         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
771             wpa_s->wpa_state == WPA_INACTIVE)
772                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
773
774         /* Find the starting point from which to continue scanning */
775         ssid = wpa_s->conf->ssid;
776         if (wpa_s->prev_sched_ssid) {
777                 while (ssid) {
778                         if (ssid == wpa_s->prev_sched_ssid) {
779                                 ssid = ssid->next;
780                                 break;
781                         }
782                         ssid = ssid->next;
783                 }
784         }
785
786         if (!ssid || !wpa_s->prev_sched_ssid) {
787                 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
788
789                 wpa_s->sched_scan_interval = 10;
790                 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
791                 wpa_s->first_sched_scan = 1;
792                 ssid = wpa_s->conf->ssid;
793                 wpa_s->prev_sched_ssid = ssid;
794         }
795
796         if (wildcard) {
797                 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
798                 params.num_ssids++;
799         }
800
801         while (ssid) {
802                 if (ssid->disabled)
803                         goto next;
804
805                 if (params.num_filter_ssids < wpa_s->max_match_sets &&
806                     params.filter_ssids && ssid->ssid && ssid->ssid_len) {
807                         wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
808                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
809                         os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
810                                   ssid->ssid, ssid->ssid_len);
811                         params.filter_ssids[params.num_filter_ssids].ssid_len =
812                                 ssid->ssid_len;
813                         params.num_filter_ssids++;
814                 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
815                 {
816                         wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
817                                 "filter for sched_scan - drop filter");
818                         os_free(params.filter_ssids);
819                         params.filter_ssids = NULL;
820                         params.num_filter_ssids = 0;
821                 }
822
823                 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
824                         if (params.num_ssids == max_sched_scan_ssids)
825                                 break; /* only room for broadcast SSID */
826                         wpa_dbg(wpa_s, MSG_DEBUG,
827                                 "add to active scan ssid: %s",
828                                 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
829                         params.ssids[params.num_ssids].ssid =
830                                 ssid->ssid;
831                         params.ssids[params.num_ssids].ssid_len =
832                                 ssid->ssid_len;
833                         params.num_ssids++;
834                         if (params.num_ssids >= max_sched_scan_ssids) {
835                                 wpa_s->prev_sched_ssid = ssid;
836                                 break;
837                         }
838                 }
839
840         next:
841                 wpa_s->prev_sched_ssid = ssid;
842                 ssid = ssid->next;
843         }
844
845         if (params.num_filter_ssids == 0) {
846                 os_free(params.filter_ssids);
847                 params.filter_ssids = NULL;
848         }
849
850         if (wpa_s->wps)
851                 wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
852
853         if (ssid || !wpa_s->first_sched_scan) {
854                 wpa_dbg(wpa_s, MSG_DEBUG,
855                         "Starting sched scan: interval %d (no timeout)",
856                         wpa_s->sched_scan_interval);
857         } else {
858                 wpa_dbg(wpa_s, MSG_DEBUG,
859                         "Starting sched scan: interval %d timeout %d",
860                         wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
861         }
862
863         ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
864                                               wpa_s->sched_scan_interval);
865         wpabuf_free(wps_ie);
866         os_free(params.filter_ssids);
867         if (ret) {
868                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
869                 if (prev_state != wpa_s->wpa_state)
870                         wpa_supplicant_set_state(wpa_s, prev_state);
871                 return ret;
872         }
873
874         /* If we have more SSIDs to scan, add a timeout so we scan them too */
875         if (ssid || !wpa_s->first_sched_scan) {
876                 wpa_s->sched_scan_timed_out = 0;
877                 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
878                                        wpa_supplicant_sched_scan_timeout,
879                                        wpa_s, NULL);
880                 wpa_s->first_sched_scan = 0;
881                 wpa_s->sched_scan_timeout /= 2;
882                 wpa_s->sched_scan_interval *= 2;
883         }
884
885         return 0;
886 }
887
888
889 /**
890  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
891  * @wpa_s: Pointer to wpa_supplicant data
892  *
893  * This function is used to cancel a scan request scheduled with
894  * wpa_supplicant_req_scan().
895  */
896 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
897 {
898         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
899         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
900 }
901
902
903 /**
904  * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
905  * @wpa_s: Pointer to wpa_supplicant data
906  *
907  * This function is used to stop a periodic scheduled scan.
908  */
909 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
910 {
911         if (!wpa_s->sched_scanning)
912                 return;
913
914         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
915         eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
916         wpa_supplicant_stop_sched_scan(wpa_s);
917 }
918
919
920 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
921                                     int scanning)
922 {
923         if (wpa_s->scanning != scanning) {
924                 wpa_s->scanning = scanning;
925                 wpas_notify_scanning(wpa_s);
926         }
927 }
928
929
930 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
931 {
932         int rate = 0;
933         const u8 *ie;
934         int i;
935
936         ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
937         for (i = 0; ie && i < ie[1]; i++) {
938                 if ((ie[i + 2] & 0x7f) > rate)
939                         rate = ie[i + 2] & 0x7f;
940         }
941
942         ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
943         for (i = 0; ie && i < ie[1]; i++) {
944                 if ((ie[i + 2] & 0x7f) > rate)
945                         rate = ie[i + 2] & 0x7f;
946         }
947
948         return rate;
949 }
950
951
952 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
953 {
954         const u8 *end, *pos;
955
956         pos = (const u8 *) (res + 1);
957         end = pos + res->ie_len;
958
959         while (pos + 1 < end) {
960                 if (pos + 2 + pos[1] > end)
961                         break;
962                 if (pos[0] == ie)
963                         return pos;
964                 pos += 2 + pos[1];
965         }
966
967         return NULL;
968 }
969
970
971 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
972                                   u32 vendor_type)
973 {
974         const u8 *end, *pos;
975
976         pos = (const u8 *) (res + 1);
977         end = pos + res->ie_len;
978
979         while (pos + 1 < end) {
980                 if (pos + 2 + pos[1] > end)
981                         break;
982                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
983                     vendor_type == WPA_GET_BE32(&pos[2]))
984                         return pos;
985                 pos += 2 + pos[1];
986         }
987
988         return NULL;
989 }
990
991
992 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
993                                              u32 vendor_type)
994 {
995         struct wpabuf *buf;
996         const u8 *end, *pos;
997
998         buf = wpabuf_alloc(res->ie_len);
999         if (buf == NULL)
1000                 return NULL;
1001
1002         pos = (const u8 *) (res + 1);
1003         end = pos + res->ie_len;
1004
1005         while (pos + 1 < end) {
1006                 if (pos + 2 + pos[1] > end)
1007                         break;
1008                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1009                     vendor_type == WPA_GET_BE32(&pos[2]))
1010                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1011                 pos += 2 + pos[1];
1012         }
1013
1014         if (wpabuf_len(buf) == 0) {
1015                 wpabuf_free(buf);
1016                 buf = NULL;
1017         }
1018
1019         return buf;
1020 }
1021
1022
1023 struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1024         const struct wpa_scan_res *res, u32 vendor_type)
1025 {
1026         struct wpabuf *buf;
1027         const u8 *end, *pos;
1028
1029         if (res->beacon_ie_len == 0)
1030                 return NULL;
1031         buf = wpabuf_alloc(res->beacon_ie_len);
1032         if (buf == NULL)
1033                 return NULL;
1034
1035         pos = (const u8 *) (res + 1);
1036         pos += res->ie_len;
1037         end = pos + res->beacon_ie_len;
1038
1039         while (pos + 1 < end) {
1040                 if (pos + 2 + pos[1] > end)
1041                         break;
1042                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1043                     vendor_type == WPA_GET_BE32(&pos[2]))
1044                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1045                 pos += 2 + pos[1];
1046         }
1047
1048         if (wpabuf_len(buf) == 0) {
1049                 wpabuf_free(buf);
1050                 buf = NULL;
1051         }
1052
1053         return buf;
1054 }
1055
1056
1057 /*
1058  * Channels with a great SNR can operate at full rate. What is a great SNR?
1059  * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1060  * rule of thumb is that any SNR above 20 is good." This one
1061  * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1062  * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1063  * conservative value.
1064  */
1065 #define GREAT_SNR 30
1066
1067 /* Compare function for sorting scan results. Return >0 if @b is considered
1068  * better. */
1069 static int wpa_scan_result_compar(const void *a, const void *b)
1070 {
1071 #define IS_5GHZ(n) (n > 4000)
1072 #define MIN(a,b) a < b ? a : b
1073         struct wpa_scan_res **_wa = (void *) a;
1074         struct wpa_scan_res **_wb = (void *) b;
1075         struct wpa_scan_res *wa = *_wa;
1076         struct wpa_scan_res *wb = *_wb;
1077         int wpa_a, wpa_b, maxrate_a, maxrate_b;
1078         int snr_a, snr_b;
1079
1080         /* WPA/WPA2 support preferred */
1081         wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1082                 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1083         wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1084                 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1085
1086         if (wpa_b && !wpa_a)
1087                 return 1;
1088         if (!wpa_b && wpa_a)
1089                 return -1;
1090
1091         /* privacy support preferred */
1092         if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1093             (wb->caps & IEEE80211_CAP_PRIVACY))
1094                 return 1;
1095         if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1096             (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1097                 return -1;
1098
1099         if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1100             !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1101                 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1102                 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1103         } else {
1104                 /* Not suitable information to calculate SNR, so use level */
1105                 snr_a = wa->level;
1106                 snr_b = wb->level;
1107         }
1108
1109         /* best/max rate preferred if SNR close enough */
1110         if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1111             (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1112                 maxrate_a = wpa_scan_get_max_rate(wa);
1113                 maxrate_b = wpa_scan_get_max_rate(wb);
1114                 if (maxrate_a != maxrate_b)
1115                         return maxrate_b - maxrate_a;
1116                 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1117                         return IS_5GHZ(wa->freq) ? -1 : 1;
1118         }
1119
1120         /* use freq for channel preference */
1121
1122         /* all things being equal, use SNR; if SNRs are
1123          * identical, use quality values since some drivers may only report
1124          * that value and leave the signal level zero */
1125         if (snr_b == snr_a)
1126                 return wb->qual - wa->qual;
1127         return snr_b - snr_a;
1128 #undef MIN
1129 #undef IS_5GHZ
1130 }
1131
1132
1133 #ifdef CONFIG_WPS
1134 /* Compare function for sorting scan results when searching a WPS AP for
1135  * provisioning. Return >0 if @b is considered better. */
1136 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1137 {
1138         struct wpa_scan_res **_wa = (void *) a;
1139         struct wpa_scan_res **_wb = (void *) b;
1140         struct wpa_scan_res *wa = *_wa;
1141         struct wpa_scan_res *wb = *_wb;
1142         int uses_wps_a, uses_wps_b;
1143         struct wpabuf *wps_a, *wps_b;
1144         int res;
1145
1146         /* Optimization - check WPS IE existence before allocated memory and
1147          * doing full reassembly. */
1148         uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1149         uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1150         if (uses_wps_a && !uses_wps_b)
1151                 return -1;
1152         if (!uses_wps_a && uses_wps_b)
1153                 return 1;
1154
1155         if (uses_wps_a && uses_wps_b) {
1156                 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1157                 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1158                 res = wps_ap_priority_compar(wps_a, wps_b);
1159                 wpabuf_free(wps_a);
1160                 wpabuf_free(wps_b);
1161                 if (res)
1162                         return res;
1163         }
1164
1165         /*
1166          * Do not use current AP security policy as a sorting criteria during
1167          * WPS provisioning step since the AP may get reconfigured at the
1168          * completion of provisioning.
1169          */
1170
1171         /* all things being equal, use signal level; if signal levels are
1172          * identical, use quality values since some drivers may only report
1173          * that value and leave the signal level zero */
1174         if (wb->level == wa->level)
1175                 return wb->qual - wa->qual;
1176         return wb->level - wa->level;
1177 }
1178 #endif /* CONFIG_WPS */
1179
1180
1181 static void dump_scan_res(struct wpa_scan_results *scan_res)
1182 {
1183 #ifndef CONFIG_NO_STDOUT_DEBUG
1184         size_t i;
1185
1186         if (scan_res->res == NULL || scan_res->num == 0)
1187                 return;
1188
1189         wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1190
1191         for (i = 0; i < scan_res->num; i++) {
1192                 struct wpa_scan_res *r = scan_res->res[i];
1193                 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1194                     == WPA_SCAN_LEVEL_DBM) {
1195                         int snr = r->level - r->noise;
1196                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1197                                    "noise=%d level=%d snr=%d%s flags=0x%x",
1198                                    MAC2STR(r->bssid), r->freq, r->qual,
1199                                    r->noise, r->level, snr,
1200                                    snr >= GREAT_SNR ? "*" : "", r->flags);
1201                 } else {
1202                         wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1203                                    "noise=%d level=%d flags=0x%x",
1204                                    MAC2STR(r->bssid), r->freq, r->qual,
1205                                    r->noise, r->level, r->flags);
1206                 }
1207         }
1208 #endif /* CONFIG_NO_STDOUT_DEBUG */
1209 }
1210
1211
1212 /**
1213  * wpa_supplicant_get_scan_results - Get scan results
1214  * @wpa_s: Pointer to wpa_supplicant data
1215  * @info: Information about what was scanned or %NULL if not available
1216  * @new_scan: Whether a new scan was performed
1217  * Returns: Scan results, %NULL on failure
1218  *
1219  * This function request the current scan results from the driver and updates
1220  * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1221  * results with wpa_scan_results_free().
1222  */
1223 struct wpa_scan_results *
1224 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1225                                 struct scan_info *info, int new_scan)
1226 {
1227         struct wpa_scan_results *scan_res;
1228         size_t i;
1229         int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1230
1231         scan_res = wpa_drv_get_scan_results2(wpa_s);
1232         if (scan_res == NULL) {
1233                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1234                 return NULL;
1235         }
1236
1237 #ifdef CONFIG_WPS
1238         if (wpas_wps_in_progress(wpa_s)) {
1239                 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1240                         "provisioning rules");
1241                 compar = wpa_scan_result_wps_compar;
1242         }
1243 #endif /* CONFIG_WPS */
1244
1245         qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1246               compar);
1247         dump_scan_res(scan_res);
1248
1249         wpa_bss_update_start(wpa_s);
1250         for (i = 0; i < scan_res->num; i++)
1251                 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1252         wpa_bss_update_end(wpa_s, info, new_scan);
1253
1254         return scan_res;
1255 }
1256
1257
1258 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1259 {
1260         struct wpa_scan_results *scan_res;
1261         scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1262         if (scan_res == NULL)
1263                 return -1;
1264         wpa_scan_results_free(scan_res);
1265
1266         return 0;
1267 }