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