blob: 048e6d01c77f4f075e96f85a788772af48b0574f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
14#include "config.h"
15#include "wpa_supplicant_i.h"
16#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "wps_supplicant.h"
18#include "p2p_supplicant.h"
19#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070020#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "notify.h"
22#include "bss.h"
23#include "scan.h"
24
25
26static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
27{
28 struct wpa_ssid *ssid;
29 union wpa_event_data data;
30
31 ssid = wpa_supplicant_get_ssid(wpa_s);
32 if (ssid == NULL)
33 return;
34
35 if (wpa_s->current_ssid == NULL) {
36 wpa_s->current_ssid = ssid;
37 if (wpa_s->current_ssid != NULL)
38 wpas_notify_network_changed(wpa_s);
39 }
40 wpa_supplicant_initiate_eapol(wpa_s);
41 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
42 "network - generating associated event");
43 os_memset(&data, 0, sizeof(data));
44 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
45}
46
47
48#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050 enum wps_request_type *req_type)
51{
52 struct wpa_ssid *ssid;
53 int wps = 0;
54
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080055 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
57 continue;
58
59 wps = 1;
60 *req_type = wpas_wps_get_req_type(ssid);
61 if (!ssid->eap.phase1)
62 continue;
63
64 if (os_strstr(ssid->eap.phase1, "pbc=1"))
65 return 2;
66 }
67
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080068#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080069 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
70 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080071 wpa_s->wps->dev.p2p = 1;
72 if (!wps) {
73 wps = 1;
74 *req_type = WPS_REQ_ENROLLEE_INFO;
75 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080076 }
77#endif /* CONFIG_P2P */
78
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 return wps;
80}
81#endif /* CONFIG_WPS */
82
83
Dmitry Shmidt04949592012-07-19 12:16:46 -070084int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085{
Dmitry Shmidt04949592012-07-19 12:16:46 -070086 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070087 int count = 0, disabled = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070089 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070091 else
92 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070093 ssid = ssid->next;
94 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070095 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
96 wpa_s->conf->auto_interworking)
97 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070098 if (count == 0 && disabled > 0) {
99 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
100 "networks)", disabled);
101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 return count;
103}
104
105
106static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
107 struct wpa_ssid *ssid)
108{
109 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700110 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 break;
112 ssid = ssid->next;
113 }
114
115 /* ap_scan=2 mode - try to associate with each SSID. */
116 if (ssid == NULL) {
117 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
118 "end of scan list - go back to beginning");
119 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
120 wpa_supplicant_req_scan(wpa_s, 0, 0);
121 return;
122 }
123 if (ssid->next) {
124 /* Continue from the next SSID on the next attempt. */
125 wpa_s->prev_scan_ssid = ssid;
126 } else {
127 /* Start from the beginning of the SSID list. */
128 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
129 }
130 wpa_supplicant_associate(wpa_s, NULL, ssid);
131}
132
133
134static int int_array_len(const int *a)
135{
136 int i;
137 for (i = 0; a && a[i]; i++)
138 ;
139 return i;
140}
141
142
143static void int_array_concat(int **res, const int *a)
144{
145 int reslen, alen, i;
146 int *n;
147
148 reslen = int_array_len(*res);
149 alen = int_array_len(a);
150
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700151 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 if (n == NULL) {
153 os_free(*res);
154 *res = NULL;
155 return;
156 }
157 for (i = 0; i <= alen; i++)
158 n[reslen + i] = a[i];
159 *res = n;
160}
161
162
163static int freq_cmp(const void *a, const void *b)
164{
165 int _a = *(int *) a;
166 int _b = *(int *) b;
167
168 if (_a == 0)
169 return 1;
170 if (_b == 0)
171 return -1;
172 return _a - _b;
173}
174
175
176static void int_array_sort_unique(int *a)
177{
178 int alen;
179 int i, j;
180
181 if (a == NULL)
182 return;
183
184 alen = int_array_len(a);
185 qsort(a, alen, sizeof(int), freq_cmp);
186
187 i = 0;
188 j = 1;
189 while (a[i] && a[j]) {
190 if (a[i] == a[j]) {
191 j++;
192 continue;
193 }
194 a[++i] = a[j++];
195 }
196 if (a[i])
197 i++;
198 a[i] = 0;
199}
200
201
202int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
203 struct wpa_driver_scan_params *params)
204{
205 int ret;
206
207 wpa_supplicant_notify_scanning(wpa_s, 1);
208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 if (ret) {
211 wpa_supplicant_notify_scanning(wpa_s, 0);
212 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800213 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 wpa_s->scan_runs++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800215 wpa_s->normal_scans++;
216 }
217
218 return ret;
219}
220
221
222static void
223wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
224{
225 struct wpa_supplicant *wpa_s = eloop_ctx;
226
227 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
228
229 if (wpa_supplicant_req_sched_scan(wpa_s))
230 wpa_supplicant_req_scan(wpa_s, 0, 0);
231}
232
233
234static void
235wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
236{
237 struct wpa_supplicant *wpa_s = eloop_ctx;
238
239 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
240
241 wpa_s->sched_scan_timed_out = 1;
242 wpa_supplicant_cancel_sched_scan(wpa_s);
243}
244
245
246static int
247wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
248 struct wpa_driver_scan_params *params,
249 int interval)
250{
251 int ret;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700252#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700254#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800255 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
256 if (ret)
257 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700258 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800259 wpa_s->sched_scanning = 1;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700260#ifdef ANDROID_P2P
261 wpa_supplicant_notify_scanning(wpa_s, 1);
262#endif
263 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800264
265 return ret;
266}
267
268
269static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
270{
271 int ret;
272
273 ret = wpa_drv_stop_sched_scan(wpa_s);
274 if (ret) {
275 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
276 /* TODO: what to do if stopping fails? */
277 return -1;
278 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279
280 return ret;
281}
282
283
284static struct wpa_driver_scan_filter *
285wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
286{
287 struct wpa_driver_scan_filter *ssids;
288 struct wpa_ssid *ssid;
289 size_t count;
290
291 *num_ssids = 0;
292 if (!conf->filter_ssids)
293 return NULL;
294
295 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
296 if (ssid->ssid && ssid->ssid_len)
297 count++;
298 }
299 if (count == 0)
300 return NULL;
301 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
302 if (ssids == NULL)
303 return NULL;
304
305 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
306 if (!ssid->ssid || !ssid->ssid_len)
307 continue;
308 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
309 ssids[*num_ssids].ssid_len = ssid->ssid_len;
310 (*num_ssids)++;
311 }
312
313 return ssids;
314}
315
316
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800317static void wpa_supplicant_optimize_freqs(
318 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
319{
320#ifdef CONFIG_P2P
321 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
322 wpa_s->go_params) {
323 /* Optimize provisioning state scan based on GO information */
324 if (wpa_s->p2p_in_provisioning < 5 &&
325 wpa_s->go_params->freq > 0) {
326 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
327 "preferred frequency %d MHz",
328 wpa_s->go_params->freq);
329 params->freqs = os_zalloc(2 * sizeof(int));
330 if (params->freqs)
331 params->freqs[0] = wpa_s->go_params->freq;
332 } else if (wpa_s->p2p_in_provisioning < 8 &&
333 wpa_s->go_params->freq_list[0]) {
334 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
335 "channels");
336 int_array_concat(&params->freqs,
337 wpa_s->go_params->freq_list);
338 if (params->freqs)
339 int_array_sort_unique(params->freqs);
340 }
341 wpa_s->p2p_in_provisioning++;
342 }
343#endif /* CONFIG_P2P */
344
345#ifdef CONFIG_WPS
346 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
347 /*
348 * Optimize post-provisioning scan based on channel used
349 * during provisioning.
350 */
351 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
352 "that was used during provisioning", wpa_s->wps_freq);
353 params->freqs = os_zalloc(2 * sizeof(int));
354 if (params->freqs)
355 params->freqs[0] = wpa_s->wps_freq;
356 wpa_s->after_wps--;
357 }
358
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800359 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
360 {
361 /* Optimize provisioning scan based on already known channel */
362 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
363 wpa_s->wps_freq);
364 params->freqs = os_zalloc(2 * sizeof(int));
365 if (params->freqs)
366 params->freqs[0] = wpa_s->wps_freq;
367 wpa_s->known_wps_freq = 0; /* only do this once */
368 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800369#endif /* CONFIG_WPS */
370}
371
372
373#ifdef CONFIG_INTERWORKING
374static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
375 struct wpabuf *buf)
376{
377 if (wpa_s->conf->interworking == 0)
378 return;
379
380 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
381 wpabuf_put_u8(buf, 4);
382 wpabuf_put_u8(buf, 0x00);
383 wpabuf_put_u8(buf, 0x00);
384 wpabuf_put_u8(buf, 0x00);
385 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
386
387 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
388 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
389 1 + ETH_ALEN);
390 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
391 /* No Venue Info */
392 if (!is_zero_ether_addr(wpa_s->conf->hessid))
393 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
394}
395#endif /* CONFIG_INTERWORKING */
396
397
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700398static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800399{
400 struct wpabuf *extra_ie = NULL;
401#ifdef CONFIG_WPS
402 int wps = 0;
403 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
404#endif /* CONFIG_WPS */
405
406#ifdef CONFIG_INTERWORKING
407 if (wpa_s->conf->interworking &&
408 wpabuf_resize(&extra_ie, 100) == 0)
409 wpas_add_interworking_elements(wpa_s, extra_ie);
410#endif /* CONFIG_INTERWORKING */
411
412#ifdef CONFIG_WPS
413 wps = wpas_wps_in_use(wpa_s, &req_type);
414
415 if (wps) {
416 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700417 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
418 DEV_PW_DEFAULT,
419 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800420 wpa_s->wps->uuid, req_type,
421 0, NULL);
422 if (wps_ie) {
423 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
424 wpabuf_put_buf(extra_ie, wps_ie);
425 wpabuf_free(wps_ie);
426 }
427 }
428
429#ifdef CONFIG_P2P
430 if (wps) {
431 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
432 if (wpabuf_resize(&extra_ie, ielen) == 0)
433 wpas_p2p_scan_ie(wpa_s, extra_ie);
434 }
435#endif /* CONFIG_P2P */
436
437#endif /* CONFIG_WPS */
438
439 return extra_ie;
440}
441
442
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800443#ifdef CONFIG_P2P
444
445/*
446 * Check whether there are any enabled networks or credentials that could be
447 * used for a non-P2P connection.
448 */
449static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
450{
451 struct wpa_ssid *ssid;
452
453 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
454 if (wpas_network_disabled(wpa_s, ssid))
455 continue;
456 if (!ssid->p2p_group)
457 return 1;
458 }
459
460 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
461 wpa_s->conf->auto_interworking)
462 return 1;
463
464 return 0;
465}
466
467
468/*
469 * Find the operating frequency of any other virtual interface that is using
470 * the same radio concurrently.
471 */
472static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
473{
474 const char *rn, *rn2;
475 struct wpa_supplicant *ifs;
476 u8 bssid[ETH_ALEN];
477
478 if (!wpa_s->driver->get_radio_name)
479 return -1;
480
481 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
482 if (rn == NULL || rn[0] == '\0')
483 return -1;
484
485 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
486 if (ifs == wpa_s || !ifs->driver->get_radio_name)
487 continue;
488
489 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
490 if (!rn2 || os_strcmp(rn, rn2) != 0)
491 continue;
492
493 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
494 continue;
495
496 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
497 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
498 return ifs->current_ssid->frequency;
499 if (wpa_drv_get_bssid(ifs, bssid) == 0)
500 return ifs->assoc_freq;
501 }
502
503 return 0;
504}
505
506#endif /* CONFIG_P2P */
507
508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
510{
511 struct wpa_supplicant *wpa_s = eloop_ctx;
512 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800513 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
514 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700515 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700517 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 size_t max_ssids;
519 enum wpa_states prev_state;
520
521 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
522 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
523 return;
524 }
525
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800526 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700527 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
529 return;
530 }
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700531#ifdef ANDROID
532 if (wpa_s->scanning) {
533 /* If we are already in scanning state, we shall ignore this new scan request*/
534 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
535 return;
536 }
537#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700538 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800539 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
541 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700542#ifdef CONFIG_P2P
543 wpa_s->sta_scan_pending = 0;
544#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 return;
546 }
547
548 if (wpa_s->conf->ap_scan != 0 &&
549 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
550 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
551 "overriding ap_scan configuration");
552 wpa_s->conf->ap_scan = 0;
553 wpas_notify_ap_scan_changed(wpa_s);
554 }
555
556 if (wpa_s->conf->ap_scan == 0) {
557 wpa_supplicant_gen_assoc_event(wpa_s);
558 return;
559 }
560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800561#ifdef CONFIG_P2P
562 if (wpas_p2p_in_progress(wpa_s)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700563 if (wpa_s->sta_scan_pending &&
564 wpas_p2p_in_progress(wpa_s) == 2 &&
Jouni Malinendc7b7132012-09-14 12:53:47 -0700565 wpa_s->global->p2p_cb_on_scan_complete) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700566 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
567 "mode scan during P2P search");
568 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800569 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
570 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700571 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800572 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700573 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800575 }
576#endif /* CONFIG_P2P */
577
578 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 max_ssids = 1;
580 else {
581 max_ssids = wpa_s->max_scan_ssids;
582 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
583 max_ssids = WPAS_MAX_SCAN_SSIDS;
584 }
585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800587 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588
589 os_memset(&params, 0, sizeof(params));
590
591 prev_state = wpa_s->wpa_state;
592 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
593 wpa_s->wpa_state == WPA_INACTIVE)
594 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
595
Dmitry Shmidt04949592012-07-19 12:16:46 -0700596 /*
597 * If autoscan has set its own scanning parameters
598 */
599 if (wpa_s->autoscan_params != NULL) {
600 scan_params = wpa_s->autoscan_params;
601 goto scan;
602 }
603
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800604 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700605 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
606 if (ssid == wpa_s->connect_without_scan)
607 break;
608 }
609 wpa_s->connect_without_scan = NULL;
610 if (ssid) {
611 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
612 "without scan step");
613 wpa_supplicant_associate(wpa_s, NULL, ssid);
614 return;
615 }
616 }
617
Dmitry Shmidt04949592012-07-19 12:16:46 -0700618#ifdef CONFIG_P2P
619 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
620 wpa_s->go_params) {
621 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
622 "P2P group formation");
623 params.ssids[0].ssid = wpa_s->go_params->ssid;
624 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
625 params.num_ssids = 1;
626 goto ssid_list_set;
627 }
628#endif /* CONFIG_P2P */
629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 /* Find the starting point from which to continue scanning */
631 ssid = wpa_s->conf->ssid;
632 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
633 while (ssid) {
634 if (ssid == wpa_s->prev_scan_ssid) {
635 ssid = ssid->next;
636 break;
637 }
638 ssid = ssid->next;
639 }
640 }
641
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800642 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700643 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800644 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 wpa_supplicant_assoc_try(wpa_s, ssid);
646 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700647#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648 } else if (wpa_s->conf->ap_scan == 2) {
649 /*
650 * User-initiated scan request in ap_scan == 2; scan with
651 * wildcard SSID.
652 */
653 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700654#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 } else {
656 struct wpa_ssid *start = ssid, *tssid;
657 int freqs_set = 0;
658 if (ssid == NULL && max_ssids > 1)
659 ssid = wpa_s->conf->ssid;
660 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700661 if (!wpas_network_disabled(wpa_s, ssid) &&
662 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
664 ssid->ssid, ssid->ssid_len);
665 params.ssids[params.num_ssids].ssid =
666 ssid->ssid;
667 params.ssids[params.num_ssids].ssid_len =
668 ssid->ssid_len;
669 params.num_ssids++;
670 if (params.num_ssids + 1 >= max_ssids)
671 break;
672 }
673 ssid = ssid->next;
674 if (ssid == start)
675 break;
676 if (ssid == NULL && max_ssids > 1 &&
677 start != wpa_s->conf->ssid)
678 ssid = wpa_s->conf->ssid;
679 }
680
681 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700682 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 continue;
684 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
685 int_array_concat(&params.freqs,
686 tssid->scan_freq);
687 } else {
688 os_free(params.freqs);
689 params.freqs = NULL;
690 }
691 freqs_set = 1;
692 }
693 int_array_sort_unique(params.freqs);
694 }
695
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800696 if (ssid && max_ssids == 1) {
697 /*
698 * If the driver is limited to 1 SSID at a time interleave
699 * wildcard SSID scans with specific SSID scans to avoid
700 * waiting a long time for a wildcard scan.
701 */
702 if (!wpa_s->prev_scan_wildcard) {
703 params.ssids[0].ssid = NULL;
704 params.ssids[0].ssid_len = 0;
705 wpa_s->prev_scan_wildcard = 1;
706 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
707 "wildcard SSID (Interleave with specific)");
708 } else {
709 wpa_s->prev_scan_ssid = ssid;
710 wpa_s->prev_scan_wildcard = 0;
711 wpa_dbg(wpa_s, MSG_DEBUG,
712 "Starting AP scan for specific SSID: %s",
713 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800715 } else if (ssid) {
716 /* max_ssids > 1 */
717
718 wpa_s->prev_scan_ssid = ssid;
719 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
720 "the scan request");
721 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 } else {
723 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
724 params.num_ssids++;
725 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
726 "SSID");
727 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700728#ifdef CONFIG_P2P
729ssid_list_set:
730#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700733 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734
Dmitry Shmidt04949592012-07-19 12:16:46 -0700735#ifdef CONFIG_HS20
736 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
737 wpas_hs20_add_indication(extra_ie);
738#endif /* CONFIG_HS20 */
739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
741 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
742 "generated frequency list");
743 params.freqs = wpa_s->next_scan_freqs;
744 } else
745 os_free(wpa_s->next_scan_freqs);
746 wpa_s->next_scan_freqs = NULL;
747
748 params.filter_ssids = wpa_supplicant_build_filter_ssids(
749 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800750 if (extra_ie) {
751 params.extra_ies = wpabuf_head(extra_ie);
752 params.extra_ies_len = wpabuf_len(extra_ie);
753 }
754
755#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700756 if (wpa_s->p2p_in_provisioning ||
757 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800758 /*
759 * The interface may not yet be in P2P mode, so we have to
760 * explicitly request P2P probe to disable CCK rates.
761 */
762 params.p2p_probe = 1;
763 }
764#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765
Dmitry Shmidt04949592012-07-19 12:16:46 -0700766 scan_params = &params;
767
768scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800769#ifdef CONFIG_P2P
770 /*
771 * If the driver does not support multi-channel concurrency and a
772 * virtual interface that shares the same radio with the wpa_s interface
773 * is operating there may not be need to scan other channels apart from
774 * the current operating channel on the other virtual interface. Filter
775 * out other channels in case we are trying to find a connection for a
776 * station interface when we are not configured to prefer station
777 * connection and a concurrent operation is already in process.
778 */
779 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
780 !scan_params->freqs && !params.freqs &&
781 wpas_is_p2p_prioritized(wpa_s) &&
782 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
783 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
784 non_p2p_network_enabled(wpa_s)) {
785 int freq = shared_vif_oper_freq(wpa_s);
786 if (freq > 0) {
787 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
788 "operating channel (%d MHz) since driver does "
789 "not support multi-channel concurrency", freq);
790 params.freqs = os_zalloc(sizeof(int) * 2);
791 if (params.freqs)
792 params.freqs[0] = freq;
793 scan_params->freqs = params.freqs;
794 }
795 }
796#endif /* CONFIG_P2P */
797
Dmitry Shmidt04949592012-07-19 12:16:46 -0700798 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801 os_free(params.freqs);
802 os_free(params.filter_ssids);
803
804 if (ret) {
805 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
806 if (prev_state != wpa_s->wpa_state)
807 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800808 /* Restore scan_req since we will try to scan again */
809 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800811 } else {
812 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 }
814}
815
816
817/**
818 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
819 * @wpa_s: Pointer to wpa_supplicant data
820 * @sec: Number of seconds after which to scan
821 * @usec: Number of microseconds after which to scan
822 *
823 * This function is used to schedule a scan for neighboring access points after
824 * the specified time.
825 */
826void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
827{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800828#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 /* If there's at least one network that should be specifically scanned
830 * then don't cancel the scan and reschedule. Some drivers do
831 * background scanning which generates frequent scan results, and that
832 * causes the specific SSID scan to get continually pushed back and
833 * never happen, which causes hidden APs to never get probe-scanned.
834 */
835 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
836 wpa_s->conf->ap_scan == 1) {
837 struct wpa_ssid *ssid = wpa_s->conf->ssid;
838
839 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700840 if (!wpas_network_disabled(wpa_s, ssid) &&
841 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 break;
843 ssid = ssid->next;
844 }
845 if (ssid) {
846 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
847 "ensure that specific SSID scans occur");
848 return;
849 }
850 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800851#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
853 sec, usec);
854 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
855 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
856}
857
858
859/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800860 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
861 * @wpa_s: Pointer to wpa_supplicant data
862 * @sec: Number of seconds after which to scan
863 * @usec: Number of microseconds after which to scan
864 *
865 * This function is used to schedule periodic scans for neighboring
866 * access points after the specified time.
867 */
868int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
869 int sec, int usec)
870{
871 if (!wpa_s->sched_scan_supported)
872 return -1;
873
874 eloop_register_timeout(sec, usec,
875 wpa_supplicant_delayed_sched_scan_timeout,
876 wpa_s, NULL);
877
878 return 0;
879}
880
881
882/**
883 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
884 * @wpa_s: Pointer to wpa_supplicant data
885 *
886 * This function is used to schedule periodic scans for neighboring
887 * access points repeating the scan continuously.
888 */
889int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
890{
891 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700892 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800893 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700894 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700895 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800896 int ret;
897 unsigned int max_sched_scan_ssids;
898 int wildcard = 0;
899 int need_ssids;
900
901 if (!wpa_s->sched_scan_supported)
902 return -1;
903
904 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
905 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
906 else
907 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700908 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800909 return -1;
910
911 if (wpa_s->sched_scanning) {
912 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
913 return 0;
914 }
915
916 need_ssids = 0;
917 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700918 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800919 /* Use wildcard SSID to find this network */
920 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700921 } else if (!wpas_network_disabled(wpa_s, ssid) &&
922 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800923 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700924
925#ifdef CONFIG_WPS
926 if (!wpas_network_disabled(wpa_s, ssid) &&
927 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
928 /*
929 * Normal scan is more reliable and faster for WPS
930 * operations and since these are for short periods of
931 * time, the benefit of trying to use sched_scan would
932 * be limited.
933 */
934 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
935 "sched_scan for WPS");
936 return -1;
937 }
938#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800939 }
940 if (wildcard)
941 need_ssids++;
942
943 if (wpa_s->normal_scans < 3 &&
944 (need_ssids <= wpa_s->max_scan_ssids ||
945 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
946 /*
947 * When normal scan can speed up operations, use that for the
948 * first operations before starting the sched_scan to allow
949 * user space sleep more. We do this only if the normal scan
950 * has functionality that is suitable for this or if the
951 * sched_scan does not have better support for multiple SSIDs.
952 */
953 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
954 "sched_scan for initial scans (normal_scans=%d)",
955 wpa_s->normal_scans);
956 return -1;
957 }
958
959 os_memset(&params, 0, sizeof(params));
960
961 /* If we can't allocate space for the filters, we just don't filter */
962 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
963 sizeof(struct wpa_driver_scan_filter));
964
965 prev_state = wpa_s->wpa_state;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700966#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800967 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
968 wpa_s->wpa_state == WPA_INACTIVE)
969 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700970#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800971
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972 if (wpa_s->autoscan_params != NULL) {
973 scan_params = wpa_s->autoscan_params;
974 goto scan;
975 }
976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977 /* Find the starting point from which to continue scanning */
978 ssid = wpa_s->conf->ssid;
979 if (wpa_s->prev_sched_ssid) {
980 while (ssid) {
981 if (ssid == wpa_s->prev_sched_ssid) {
982 ssid = ssid->next;
983 break;
984 }
985 ssid = ssid->next;
986 }
987 }
988
989 if (!ssid || !wpa_s->prev_sched_ssid) {
990 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
991
Dmitry Shmidt04949592012-07-19 12:16:46 -0700992 if (wpa_s->sched_scan_interval == 0)
993 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800994 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
995 wpa_s->first_sched_scan = 1;
996 ssid = wpa_s->conf->ssid;
997 wpa_s->prev_sched_ssid = ssid;
998 }
999
1000 if (wildcard) {
1001 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1002 params.num_ssids++;
1003 }
1004
1005 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001006 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001007 goto next;
1008
1009 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1010 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1011 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1012 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1013 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1014 ssid->ssid, ssid->ssid_len);
1015 params.filter_ssids[params.num_filter_ssids].ssid_len =
1016 ssid->ssid_len;
1017 params.num_filter_ssids++;
1018 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1019 {
1020 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1021 "filter for sched_scan - drop filter");
1022 os_free(params.filter_ssids);
1023 params.filter_ssids = NULL;
1024 params.num_filter_ssids = 0;
1025 }
1026
1027 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1028 if (params.num_ssids == max_sched_scan_ssids)
1029 break; /* only room for broadcast SSID */
1030 wpa_dbg(wpa_s, MSG_DEBUG,
1031 "add to active scan ssid: %s",
1032 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1033 params.ssids[params.num_ssids].ssid =
1034 ssid->ssid;
1035 params.ssids[params.num_ssids].ssid_len =
1036 ssid->ssid_len;
1037 params.num_ssids++;
1038 if (params.num_ssids >= max_sched_scan_ssids) {
1039 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001040 do {
1041 ssid = ssid->next;
1042 } while (ssid &&
1043 (wpas_network_disabled(wpa_s, ssid) ||
1044 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001045 break;
1046 }
1047 }
1048
1049 next:
1050 wpa_s->prev_sched_ssid = ssid;
1051 ssid = ssid->next;
1052 }
1053
1054 if (params.num_filter_ssids == 0) {
1055 os_free(params.filter_ssids);
1056 params.filter_ssids = NULL;
1057 }
1058
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001059 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1060 if (extra_ie) {
1061 params.extra_ies = wpabuf_head(extra_ie);
1062 params.extra_ies_len = wpabuf_len(extra_ie);
1063 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001064
Dmitry Shmidt04949592012-07-19 12:16:46 -07001065 scan_params = &params;
1066
1067scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001068 if (ssid || !wpa_s->first_sched_scan) {
1069 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001070 "Starting sched scan: interval %d timeout %d",
1071 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001072 } else {
1073 wpa_dbg(wpa_s, MSG_DEBUG,
1074 "Starting sched scan: interval %d (no timeout)",
1075 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001076 }
1077
Dmitry Shmidt04949592012-07-19 12:16:46 -07001078 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001079 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001080 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001081 os_free(params.filter_ssids);
1082 if (ret) {
1083 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1084 if (prev_state != wpa_s->wpa_state)
1085 wpa_supplicant_set_state(wpa_s, prev_state);
1086 return ret;
1087 }
1088
1089 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1090 if (ssid || !wpa_s->first_sched_scan) {
1091 wpa_s->sched_scan_timed_out = 0;
1092 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1093 wpa_supplicant_sched_scan_timeout,
1094 wpa_s, NULL);
1095 wpa_s->first_sched_scan = 0;
1096 wpa_s->sched_scan_timeout /= 2;
1097 wpa_s->sched_scan_interval *= 2;
1098 }
1099
1100 return 0;
1101}
1102
1103
1104/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1106 * @wpa_s: Pointer to wpa_supplicant data
1107 *
1108 * This function is used to cancel a scan request scheduled with
1109 * wpa_supplicant_req_scan().
1110 */
1111void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1112{
1113 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1114 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001115#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001116 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001117#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118}
1119
1120
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001121/**
1122 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1123 * @wpa_s: Pointer to wpa_supplicant data
1124 *
1125 * This function is used to stop a periodic scheduled scan.
1126 */
1127void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1128{
1129 if (!wpa_s->sched_scanning)
1130 return;
1131
1132 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1133 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1134 wpa_supplicant_stop_sched_scan(wpa_s);
1135}
1136
1137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1139 int scanning)
1140{
1141 if (wpa_s->scanning != scanning) {
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001142#ifdef ANDROID_P2P
1143 if(!wpa_s->sched_scanning)
1144 wpa_s->scanning = scanning;
1145#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 wpa_s->scanning = scanning;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001147#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 wpas_notify_scanning(wpa_s);
1149 }
1150}
1151
1152
1153static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1154{
1155 int rate = 0;
1156 const u8 *ie;
1157 int i;
1158
1159 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1160 for (i = 0; ie && i < ie[1]; i++) {
1161 if ((ie[i + 2] & 0x7f) > rate)
1162 rate = ie[i + 2] & 0x7f;
1163 }
1164
1165 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1166 for (i = 0; ie && i < ie[1]; i++) {
1167 if ((ie[i + 2] & 0x7f) > rate)
1168 rate = ie[i + 2] & 0x7f;
1169 }
1170
1171 return rate;
1172}
1173
1174
1175const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1176{
1177 const u8 *end, *pos;
1178
1179 pos = (const u8 *) (res + 1);
1180 end = pos + res->ie_len;
1181
1182 while (pos + 1 < end) {
1183 if (pos + 2 + pos[1] > end)
1184 break;
1185 if (pos[0] == ie)
1186 return pos;
1187 pos += 2 + pos[1];
1188 }
1189
1190 return NULL;
1191}
1192
1193
1194const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1195 u32 vendor_type)
1196{
1197 const u8 *end, *pos;
1198
1199 pos = (const u8 *) (res + 1);
1200 end = pos + res->ie_len;
1201
1202 while (pos + 1 < end) {
1203 if (pos + 2 + pos[1] > end)
1204 break;
1205 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1206 vendor_type == WPA_GET_BE32(&pos[2]))
1207 return pos;
1208 pos += 2 + pos[1];
1209 }
1210
1211 return NULL;
1212}
1213
1214
1215struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1216 u32 vendor_type)
1217{
1218 struct wpabuf *buf;
1219 const u8 *end, *pos;
1220
1221 buf = wpabuf_alloc(res->ie_len);
1222 if (buf == NULL)
1223 return NULL;
1224
1225 pos = (const u8 *) (res + 1);
1226 end = pos + res->ie_len;
1227
1228 while (pos + 1 < end) {
1229 if (pos + 2 + pos[1] > end)
1230 break;
1231 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1232 vendor_type == WPA_GET_BE32(&pos[2]))
1233 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1234 pos += 2 + pos[1];
1235 }
1236
1237 if (wpabuf_len(buf) == 0) {
1238 wpabuf_free(buf);
1239 buf = NULL;
1240 }
1241
1242 return buf;
1243}
1244
1245
1246struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1247 const struct wpa_scan_res *res, u32 vendor_type)
1248{
1249 struct wpabuf *buf;
1250 const u8 *end, *pos;
1251
1252 if (res->beacon_ie_len == 0)
1253 return NULL;
1254 buf = wpabuf_alloc(res->beacon_ie_len);
1255 if (buf == NULL)
1256 return NULL;
1257
1258 pos = (const u8 *) (res + 1);
1259 pos += res->ie_len;
1260 end = pos + res->beacon_ie_len;
1261
1262 while (pos + 1 < end) {
1263 if (pos + 2 + pos[1] > end)
1264 break;
1265 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1266 vendor_type == WPA_GET_BE32(&pos[2]))
1267 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1268 pos += 2 + pos[1];
1269 }
1270
1271 if (wpabuf_len(buf) == 0) {
1272 wpabuf_free(buf);
1273 buf = NULL;
1274 }
1275
1276 return buf;
1277}
1278
1279
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001280/*
1281 * Channels with a great SNR can operate at full rate. What is a great SNR?
1282 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1283 * rule of thumb is that any SNR above 20 is good." This one
1284 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1285 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1286 * conservative value.
1287 */
1288#define GREAT_SNR 30
1289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290/* Compare function for sorting scan results. Return >0 if @b is considered
1291 * better. */
1292static int wpa_scan_result_compar(const void *a, const void *b)
1293{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001294#define IS_5GHZ(n) (n > 4000)
1295#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 struct wpa_scan_res **_wa = (void *) a;
1297 struct wpa_scan_res **_wb = (void *) b;
1298 struct wpa_scan_res *wa = *_wa;
1299 struct wpa_scan_res *wb = *_wb;
1300 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001301 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302
1303 /* WPA/WPA2 support preferred */
1304 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1305 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1306 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1307 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1308
1309 if (wpa_b && !wpa_a)
1310 return 1;
1311 if (!wpa_b && wpa_a)
1312 return -1;
1313
1314 /* privacy support preferred */
1315 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1316 (wb->caps & IEEE80211_CAP_PRIVACY))
1317 return 1;
1318 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1319 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1320 return -1;
1321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1323 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1324 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1325 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1326 } else {
1327 /* Not suitable information to calculate SNR, so use level */
1328 snr_a = wa->level;
1329 snr_b = wb->level;
1330 }
1331
1332 /* best/max rate preferred if SNR close enough */
1333 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1335 maxrate_a = wpa_scan_get_max_rate(wa);
1336 maxrate_b = wpa_scan_get_max_rate(wb);
1337 if (maxrate_a != maxrate_b)
1338 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001339 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1340 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 }
1342
1343 /* use freq for channel preference */
1344
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001345 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 * identical, use quality values since some drivers may only report
1347 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001348 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001350 return snr_b - snr_a;
1351#undef MIN
1352#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353}
1354
1355
1356#ifdef CONFIG_WPS
1357/* Compare function for sorting scan results when searching a WPS AP for
1358 * provisioning. Return >0 if @b is considered better. */
1359static int wpa_scan_result_wps_compar(const void *a, const void *b)
1360{
1361 struct wpa_scan_res **_wa = (void *) a;
1362 struct wpa_scan_res **_wb = (void *) b;
1363 struct wpa_scan_res *wa = *_wa;
1364 struct wpa_scan_res *wb = *_wb;
1365 int uses_wps_a, uses_wps_b;
1366 struct wpabuf *wps_a, *wps_b;
1367 int res;
1368
1369 /* Optimization - check WPS IE existence before allocated memory and
1370 * doing full reassembly. */
1371 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1372 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1373 if (uses_wps_a && !uses_wps_b)
1374 return -1;
1375 if (!uses_wps_a && uses_wps_b)
1376 return 1;
1377
1378 if (uses_wps_a && uses_wps_b) {
1379 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1380 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1381 res = wps_ap_priority_compar(wps_a, wps_b);
1382 wpabuf_free(wps_a);
1383 wpabuf_free(wps_b);
1384 if (res)
1385 return res;
1386 }
1387
1388 /*
1389 * Do not use current AP security policy as a sorting criteria during
1390 * WPS provisioning step since the AP may get reconfigured at the
1391 * completion of provisioning.
1392 */
1393
1394 /* all things being equal, use signal level; if signal levels are
1395 * identical, use quality values since some drivers may only report
1396 * that value and leave the signal level zero */
1397 if (wb->level == wa->level)
1398 return wb->qual - wa->qual;
1399 return wb->level - wa->level;
1400}
1401#endif /* CONFIG_WPS */
1402
1403
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001404static void dump_scan_res(struct wpa_scan_results *scan_res)
1405{
1406#ifndef CONFIG_NO_STDOUT_DEBUG
1407 size_t i;
1408
1409 if (scan_res->res == NULL || scan_res->num == 0)
1410 return;
1411
1412 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1413
1414 for (i = 0; i < scan_res->num; i++) {
1415 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001416 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001417 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1418 == WPA_SCAN_LEVEL_DBM) {
1419 int snr = r->level - r->noise;
1420 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1421 "noise=%d level=%d snr=%d%s flags=0x%x",
1422 MAC2STR(r->bssid), r->freq, r->qual,
1423 r->noise, r->level, snr,
1424 snr >= GREAT_SNR ? "*" : "", r->flags);
1425 } else {
1426 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1427 "noise=%d level=%d flags=0x%x",
1428 MAC2STR(r->bssid), r->freq, r->qual,
1429 r->noise, r->level, r->flags);
1430 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001431 pos = (u8 *) (r + 1);
1432 if (r->ie_len)
1433 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1434 pos += r->ie_len;
1435 if (r->beacon_ie_len)
1436 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1437 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001438 }
1439#endif /* CONFIG_NO_STDOUT_DEBUG */
1440}
1441
1442
Dmitry Shmidt04949592012-07-19 12:16:46 -07001443int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1444 const u8 *bssid)
1445{
1446 size_t i;
1447
1448 if (wpa_s->bssid_filter == NULL)
1449 return 1;
1450
1451 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1452 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1453 ETH_ALEN) == 0)
1454 return 1;
1455 }
1456
1457 return 0;
1458}
1459
1460
1461static void filter_scan_res(struct wpa_supplicant *wpa_s,
1462 struct wpa_scan_results *res)
1463{
1464 size_t i, j;
1465
1466 if (wpa_s->bssid_filter == NULL)
1467 return;
1468
1469 for (i = 0, j = 0; i < res->num; i++) {
1470 if (wpa_supplicant_filter_bssid_match(wpa_s,
1471 res->res[i]->bssid)) {
1472 res->res[j++] = res->res[i];
1473 } else {
1474 os_free(res->res[i]);
1475 res->res[i] = NULL;
1476 }
1477 }
1478
1479 if (res->num != j) {
1480 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1481 (int) (res->num - j));
1482 res->num = j;
1483 }
1484}
1485
1486
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487/**
1488 * wpa_supplicant_get_scan_results - Get scan results
1489 * @wpa_s: Pointer to wpa_supplicant data
1490 * @info: Information about what was scanned or %NULL if not available
1491 * @new_scan: Whether a new scan was performed
1492 * Returns: Scan results, %NULL on failure
1493 *
1494 * This function request the current scan results from the driver and updates
1495 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1496 * results with wpa_scan_results_free().
1497 */
1498struct wpa_scan_results *
1499wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1500 struct scan_info *info, int new_scan)
1501{
1502 struct wpa_scan_results *scan_res;
1503 size_t i;
1504 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1505
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001506 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507 if (scan_res == NULL) {
1508 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1509 return NULL;
1510 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001511 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512
1513#ifdef CONFIG_WPS
1514 if (wpas_wps_in_progress(wpa_s)) {
1515 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1516 "provisioning rules");
1517 compar = wpa_scan_result_wps_compar;
1518 }
1519#endif /* CONFIG_WPS */
1520
1521 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1522 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001523 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524
1525 wpa_bss_update_start(wpa_s);
1526 for (i = 0; i < scan_res->num; i++)
1527 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1528 wpa_bss_update_end(wpa_s, info, new_scan);
1529
1530 return scan_res;
1531}
1532
1533
1534int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1535{
1536 struct wpa_scan_results *scan_res;
1537 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1538 if (scan_res == NULL)
1539 return -1;
1540 wpa_scan_results_free(scan_res);
1541
1542 return 0;
1543}