blob: d8b313975d210bc23c4d717b0c3512ff354ed9d5 [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 Shmidtc5ec7f52012-03-06 16:33:24 -080069 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p) {
70 wpa_s->wps->dev.p2p = 1;
71 if (!wps) {
72 wps = 1;
73 *req_type = WPS_REQ_ENROLLEE_INFO;
74 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080075 }
76#endif /* CONFIG_P2P */
77
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078 return wps;
79}
80#endif /* CONFIG_WPS */
81
82
Dmitry Shmidt04949592012-07-19 12:16:46 -070083int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084{
Dmitry Shmidt04949592012-07-19 12:16:46 -070085 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 int count = 0;
87 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070088 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089 count++;
90 ssid = ssid->next;
91 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070092 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
93 wpa_s->conf->auto_interworking)
94 count++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 return count;
96}
97
98
99static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
100 struct wpa_ssid *ssid)
101{
102 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700103 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 break;
105 ssid = ssid->next;
106 }
107
108 /* ap_scan=2 mode - try to associate with each SSID. */
109 if (ssid == NULL) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
111 "end of scan list - go back to beginning");
112 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
113 wpa_supplicant_req_scan(wpa_s, 0, 0);
114 return;
115 }
116 if (ssid->next) {
117 /* Continue from the next SSID on the next attempt. */
118 wpa_s->prev_scan_ssid = ssid;
119 } else {
120 /* Start from the beginning of the SSID list. */
121 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
122 }
123 wpa_supplicant_associate(wpa_s, NULL, ssid);
124}
125
126
127static int int_array_len(const int *a)
128{
129 int i;
130 for (i = 0; a && a[i]; i++)
131 ;
132 return i;
133}
134
135
136static void int_array_concat(int **res, const int *a)
137{
138 int reslen, alen, i;
139 int *n;
140
141 reslen = int_array_len(*res);
142 alen = int_array_len(a);
143
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700144 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145 if (n == NULL) {
146 os_free(*res);
147 *res = NULL;
148 return;
149 }
150 for (i = 0; i <= alen; i++)
151 n[reslen + i] = a[i];
152 *res = n;
153}
154
155
156static int freq_cmp(const void *a, const void *b)
157{
158 int _a = *(int *) a;
159 int _b = *(int *) b;
160
161 if (_a == 0)
162 return 1;
163 if (_b == 0)
164 return -1;
165 return _a - _b;
166}
167
168
169static void int_array_sort_unique(int *a)
170{
171 int alen;
172 int i, j;
173
174 if (a == NULL)
175 return;
176
177 alen = int_array_len(a);
178 qsort(a, alen, sizeof(int), freq_cmp);
179
180 i = 0;
181 j = 1;
182 while (a[i] && a[j]) {
183 if (a[i] == a[j]) {
184 j++;
185 continue;
186 }
187 a[++i] = a[j++];
188 }
189 if (a[i])
190 i++;
191 a[i] = 0;
192}
193
194
195int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
196 struct wpa_driver_scan_params *params)
197{
198 int ret;
199
200 wpa_supplicant_notify_scanning(wpa_s, 1);
201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800202 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 if (ret) {
204 wpa_supplicant_notify_scanning(wpa_s, 0);
205 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800206 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 wpa_s->scan_runs++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208 wpa_s->normal_scans++;
209 }
210
211 return ret;
212}
213
214
215static void
216wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
217{
218 struct wpa_supplicant *wpa_s = eloop_ctx;
219
220 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
221
222 if (wpa_supplicant_req_sched_scan(wpa_s))
223 wpa_supplicant_req_scan(wpa_s, 0, 0);
224}
225
226
227static void
228wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
229{
230 struct wpa_supplicant *wpa_s = eloop_ctx;
231
232 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
233
234 wpa_s->sched_scan_timed_out = 1;
235 wpa_supplicant_cancel_sched_scan(wpa_s);
236}
237
238
239static int
240wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
241 struct wpa_driver_scan_params *params,
242 int interval)
243{
244 int ret;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700245#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800246 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700247#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800248 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
249 if (ret)
250 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700251 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800252 wpa_s->sched_scanning = 1;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700253#ifdef ANDROID_P2P
254 wpa_supplicant_notify_scanning(wpa_s, 1);
255#endif
256 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800257
258 return ret;
259}
260
261
262static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
263{
264 int ret;
265
266 ret = wpa_drv_stop_sched_scan(wpa_s);
267 if (ret) {
268 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
269 /* TODO: what to do if stopping fails? */
270 return -1;
271 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272
273 return ret;
274}
275
276
277static struct wpa_driver_scan_filter *
278wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
279{
280 struct wpa_driver_scan_filter *ssids;
281 struct wpa_ssid *ssid;
282 size_t count;
283
284 *num_ssids = 0;
285 if (!conf->filter_ssids)
286 return NULL;
287
288 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
289 if (ssid->ssid && ssid->ssid_len)
290 count++;
291 }
292 if (count == 0)
293 return NULL;
294 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
295 if (ssids == NULL)
296 return NULL;
297
298 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
299 if (!ssid->ssid || !ssid->ssid_len)
300 continue;
301 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
302 ssids[*num_ssids].ssid_len = ssid->ssid_len;
303 (*num_ssids)++;
304 }
305
306 return ssids;
307}
308
309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800310static void wpa_supplicant_optimize_freqs(
311 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
312{
313#ifdef CONFIG_P2P
314 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
315 wpa_s->go_params) {
316 /* Optimize provisioning state scan based on GO information */
317 if (wpa_s->p2p_in_provisioning < 5 &&
318 wpa_s->go_params->freq > 0) {
319 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
320 "preferred frequency %d MHz",
321 wpa_s->go_params->freq);
322 params->freqs = os_zalloc(2 * sizeof(int));
323 if (params->freqs)
324 params->freqs[0] = wpa_s->go_params->freq;
325 } else if (wpa_s->p2p_in_provisioning < 8 &&
326 wpa_s->go_params->freq_list[0]) {
327 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
328 "channels");
329 int_array_concat(&params->freqs,
330 wpa_s->go_params->freq_list);
331 if (params->freqs)
332 int_array_sort_unique(params->freqs);
333 }
334 wpa_s->p2p_in_provisioning++;
335 }
336#endif /* CONFIG_P2P */
337
338#ifdef CONFIG_WPS
339 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
340 /*
341 * Optimize post-provisioning scan based on channel used
342 * during provisioning.
343 */
344 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
345 "that was used during provisioning", wpa_s->wps_freq);
346 params->freqs = os_zalloc(2 * sizeof(int));
347 if (params->freqs)
348 params->freqs[0] = wpa_s->wps_freq;
349 wpa_s->after_wps--;
350 }
351
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800352 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
353 {
354 /* Optimize provisioning scan based on already known channel */
355 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
356 wpa_s->wps_freq);
357 params->freqs = os_zalloc(2 * sizeof(int));
358 if (params->freqs)
359 params->freqs[0] = wpa_s->wps_freq;
360 wpa_s->known_wps_freq = 0; /* only do this once */
361 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800362#endif /* CONFIG_WPS */
363}
364
365
366#ifdef CONFIG_INTERWORKING
367static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
368 struct wpabuf *buf)
369{
370 if (wpa_s->conf->interworking == 0)
371 return;
372
373 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
374 wpabuf_put_u8(buf, 4);
375 wpabuf_put_u8(buf, 0x00);
376 wpabuf_put_u8(buf, 0x00);
377 wpabuf_put_u8(buf, 0x00);
378 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
379
380 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
381 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
382 1 + ETH_ALEN);
383 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
384 /* No Venue Info */
385 if (!is_zero_ether_addr(wpa_s->conf->hessid))
386 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
387}
388#endif /* CONFIG_INTERWORKING */
389
390
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700391static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800392{
393 struct wpabuf *extra_ie = NULL;
394#ifdef CONFIG_WPS
395 int wps = 0;
396 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
397#endif /* CONFIG_WPS */
398
399#ifdef CONFIG_INTERWORKING
400 if (wpa_s->conf->interworking &&
401 wpabuf_resize(&extra_ie, 100) == 0)
402 wpas_add_interworking_elements(wpa_s, extra_ie);
403#endif /* CONFIG_INTERWORKING */
404
405#ifdef CONFIG_WPS
406 wps = wpas_wps_in_use(wpa_s, &req_type);
407
408 if (wps) {
409 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700410 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
411 DEV_PW_DEFAULT,
412 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800413 wpa_s->wps->uuid, req_type,
414 0, NULL);
415 if (wps_ie) {
416 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
417 wpabuf_put_buf(extra_ie, wps_ie);
418 wpabuf_free(wps_ie);
419 }
420 }
421
422#ifdef CONFIG_P2P
423 if (wps) {
424 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
425 if (wpabuf_resize(&extra_ie, ielen) == 0)
426 wpas_p2p_scan_ie(wpa_s, extra_ie);
427 }
428#endif /* CONFIG_P2P */
429
430#endif /* CONFIG_WPS */
431
432 return extra_ie;
433}
434
435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
437{
438 struct wpa_supplicant *wpa_s = eloop_ctx;
439 struct wpa_ssid *ssid;
440 int scan_req = 0, ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700441 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700443 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 size_t max_ssids;
445 enum wpa_states prev_state;
446
447 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
448 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
449 return;
450 }
451
452 if (wpa_s->disconnected && !wpa_s->scan_req) {
453 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
454 return;
455 }
456
Dmitry Shmidt04949592012-07-19 12:16:46 -0700457 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 !wpa_s->scan_req) {
459 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
460 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
461 return;
462 }
463
464 if (wpa_s->conf->ap_scan != 0 &&
465 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
466 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
467 "overriding ap_scan configuration");
468 wpa_s->conf->ap_scan = 0;
469 wpas_notify_ap_scan_changed(wpa_s);
470 }
471
472 if (wpa_s->conf->ap_scan == 0) {
473 wpa_supplicant_gen_assoc_event(wpa_s);
474 return;
475 }
476
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800477#ifdef CONFIG_P2P
478 if (wpas_p2p_in_progress(wpa_s)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700479 if (wpa_s->sta_scan_pending &&
480 wpas_p2p_in_progress(wpa_s) == 2 &&
481 wpa_s->p2p_cb_on_scan_complete) {
482 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
483 "mode scan during P2P search");
484 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800485 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
486 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700487 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800488 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700489 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800490 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800491 }
492#endif /* CONFIG_P2P */
493
494 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 max_ssids = 1;
496 else {
497 max_ssids = wpa_s->max_scan_ssids;
498 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
499 max_ssids = WPAS_MAX_SCAN_SSIDS;
500 }
501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 scan_req = wpa_s->scan_req;
503 wpa_s->scan_req = 0;
504
505 os_memset(&params, 0, sizeof(params));
506
507 prev_state = wpa_s->wpa_state;
508 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
509 wpa_s->wpa_state == WPA_INACTIVE)
510 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
511
Dmitry Shmidt04949592012-07-19 12:16:46 -0700512 /*
513 * If autoscan has set its own scanning parameters
514 */
515 if (wpa_s->autoscan_params != NULL) {
516 scan_params = wpa_s->autoscan_params;
517 goto scan;
518 }
519
Jouni Malinen75ecf522011-06-27 15:19:46 -0700520 if (scan_req != 2 && wpa_s->connect_without_scan) {
521 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
522 if (ssid == wpa_s->connect_without_scan)
523 break;
524 }
525 wpa_s->connect_without_scan = NULL;
526 if (ssid) {
527 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
528 "without scan step");
529 wpa_supplicant_associate(wpa_s, NULL, ssid);
530 return;
531 }
532 }
533
Dmitry Shmidt04949592012-07-19 12:16:46 -0700534#ifdef CONFIG_P2P
535 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
536 wpa_s->go_params) {
537 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
538 "P2P group formation");
539 params.ssids[0].ssid = wpa_s->go_params->ssid;
540 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
541 params.num_ssids = 1;
542 goto ssid_list_set;
543 }
544#endif /* CONFIG_P2P */
545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 /* Find the starting point from which to continue scanning */
547 ssid = wpa_s->conf->ssid;
548 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
549 while (ssid) {
550 if (ssid == wpa_s->prev_scan_ssid) {
551 ssid = ssid->next;
552 break;
553 }
554 ssid = ssid->next;
555 }
556 }
557
Jouni Malinen75ecf522011-06-27 15:19:46 -0700558 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
559 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800560 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 wpa_supplicant_assoc_try(wpa_s, ssid);
562 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700563#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 } else if (wpa_s->conf->ap_scan == 2) {
565 /*
566 * User-initiated scan request in ap_scan == 2; scan with
567 * wildcard SSID.
568 */
569 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700570#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571 } else {
572 struct wpa_ssid *start = ssid, *tssid;
573 int freqs_set = 0;
574 if (ssid == NULL && max_ssids > 1)
575 ssid = wpa_s->conf->ssid;
576 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700577 if (!wpas_network_disabled(wpa_s, ssid) &&
578 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
580 ssid->ssid, ssid->ssid_len);
581 params.ssids[params.num_ssids].ssid =
582 ssid->ssid;
583 params.ssids[params.num_ssids].ssid_len =
584 ssid->ssid_len;
585 params.num_ssids++;
586 if (params.num_ssids + 1 >= max_ssids)
587 break;
588 }
589 ssid = ssid->next;
590 if (ssid == start)
591 break;
592 if (ssid == NULL && max_ssids > 1 &&
593 start != wpa_s->conf->ssid)
594 ssid = wpa_s->conf->ssid;
595 }
596
597 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700598 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 continue;
600 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
601 int_array_concat(&params.freqs,
602 tssid->scan_freq);
603 } else {
604 os_free(params.freqs);
605 params.freqs = NULL;
606 }
607 freqs_set = 1;
608 }
609 int_array_sort_unique(params.freqs);
610 }
611
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800612 if (ssid && max_ssids == 1) {
613 /*
614 * If the driver is limited to 1 SSID at a time interleave
615 * wildcard SSID scans with specific SSID scans to avoid
616 * waiting a long time for a wildcard scan.
617 */
618 if (!wpa_s->prev_scan_wildcard) {
619 params.ssids[0].ssid = NULL;
620 params.ssids[0].ssid_len = 0;
621 wpa_s->prev_scan_wildcard = 1;
622 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
623 "wildcard SSID (Interleave with specific)");
624 } else {
625 wpa_s->prev_scan_ssid = ssid;
626 wpa_s->prev_scan_wildcard = 0;
627 wpa_dbg(wpa_s, MSG_DEBUG,
628 "Starting AP scan for specific SSID: %s",
629 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800631 } else if (ssid) {
632 /* max_ssids > 1 */
633
634 wpa_s->prev_scan_ssid = ssid;
635 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
636 "the scan request");
637 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638 } else {
639 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
640 params.num_ssids++;
641 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
642 "SSID");
643 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700644#ifdef CONFIG_P2P
645ssid_list_set:
646#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700647
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800648 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700649 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650
Dmitry Shmidt04949592012-07-19 12:16:46 -0700651#ifdef CONFIG_HS20
652 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
653 wpas_hs20_add_indication(extra_ie);
654#endif /* CONFIG_HS20 */
655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
657 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
658 "generated frequency list");
659 params.freqs = wpa_s->next_scan_freqs;
660 } else
661 os_free(wpa_s->next_scan_freqs);
662 wpa_s->next_scan_freqs = NULL;
663
664 params.filter_ssids = wpa_supplicant_build_filter_ssids(
665 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800666 if (extra_ie) {
667 params.extra_ies = wpabuf_head(extra_ie);
668 params.extra_ies_len = wpabuf_len(extra_ie);
669 }
670
671#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700672 if (wpa_s->p2p_in_provisioning ||
673 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800674 /*
675 * The interface may not yet be in P2P mode, so we have to
676 * explicitly request P2P probe to disable CCK rates.
677 */
678 params.p2p_probe = 1;
679 }
680#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681
Dmitry Shmidt04949592012-07-19 12:16:46 -0700682 scan_params = &params;
683
684scan:
685 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800687 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688 os_free(params.freqs);
689 os_free(params.filter_ssids);
690
691 if (ret) {
692 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -0700693#ifdef ANDROID_P2P
694 /* Restore back the wpa_s->scan_req if we failed the scan becoz of any reason */
695 wpa_msg(wpa_s, MSG_DEBUG, "Restoring back the wpa_s->scan_req "
696 "to the original value %d", scan_req);
697 wpa_s->scan_req = scan_req;
698#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 if (prev_state != wpa_s->wpa_state)
700 wpa_supplicant_set_state(wpa_s, prev_state);
701 wpa_supplicant_req_scan(wpa_s, 1, 0);
702 }
703}
704
705
706/**
707 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
708 * @wpa_s: Pointer to wpa_supplicant data
709 * @sec: Number of seconds after which to scan
710 * @usec: Number of microseconds after which to scan
711 *
712 * This function is used to schedule a scan for neighboring access points after
713 * the specified time.
714 */
715void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
716{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800717#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 /* If there's at least one network that should be specifically scanned
719 * then don't cancel the scan and reschedule. Some drivers do
720 * background scanning which generates frequent scan results, and that
721 * causes the specific SSID scan to get continually pushed back and
722 * never happen, which causes hidden APs to never get probe-scanned.
723 */
724 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
725 wpa_s->conf->ap_scan == 1) {
726 struct wpa_ssid *ssid = wpa_s->conf->ssid;
727
728 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700729 if (!wpas_network_disabled(wpa_s, ssid) &&
730 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 break;
732 ssid = ssid->next;
733 }
734 if (ssid) {
735 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
736 "ensure that specific SSID scans occur");
737 return;
738 }
739 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800740#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
742 sec, usec);
743 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
744 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
745}
746
747
748/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800749 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
750 * @wpa_s: Pointer to wpa_supplicant data
751 * @sec: Number of seconds after which to scan
752 * @usec: Number of microseconds after which to scan
753 *
754 * This function is used to schedule periodic scans for neighboring
755 * access points after the specified time.
756 */
757int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
758 int sec, int usec)
759{
760 if (!wpa_s->sched_scan_supported)
761 return -1;
762
763 eloop_register_timeout(sec, usec,
764 wpa_supplicant_delayed_sched_scan_timeout,
765 wpa_s, NULL);
766
767 return 0;
768}
769
770
771/**
772 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
773 * @wpa_s: Pointer to wpa_supplicant data
774 *
775 * This function is used to schedule periodic scans for neighboring
776 * access points repeating the scan continuously.
777 */
778int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
779{
780 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700781 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700783 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700784 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800785 int ret;
786 unsigned int max_sched_scan_ssids;
787 int wildcard = 0;
788 int need_ssids;
789
790 if (!wpa_s->sched_scan_supported)
791 return -1;
792
793 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
794 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
795 else
796 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700797 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798 return -1;
799
800 if (wpa_s->sched_scanning) {
801 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
802 return 0;
803 }
804
805 need_ssids = 0;
806 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700807 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800808 /* Use wildcard SSID to find this network */
809 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700810 } else if (!wpas_network_disabled(wpa_s, ssid) &&
811 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700813
814#ifdef CONFIG_WPS
815 if (!wpas_network_disabled(wpa_s, ssid) &&
816 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
817 /*
818 * Normal scan is more reliable and faster for WPS
819 * operations and since these are for short periods of
820 * time, the benefit of trying to use sched_scan would
821 * be limited.
822 */
823 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
824 "sched_scan for WPS");
825 return -1;
826 }
827#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800828 }
829 if (wildcard)
830 need_ssids++;
831
832 if (wpa_s->normal_scans < 3 &&
833 (need_ssids <= wpa_s->max_scan_ssids ||
834 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
835 /*
836 * When normal scan can speed up operations, use that for the
837 * first operations before starting the sched_scan to allow
838 * user space sleep more. We do this only if the normal scan
839 * has functionality that is suitable for this or if the
840 * sched_scan does not have better support for multiple SSIDs.
841 */
842 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
843 "sched_scan for initial scans (normal_scans=%d)",
844 wpa_s->normal_scans);
845 return -1;
846 }
847
848 os_memset(&params, 0, sizeof(params));
849
850 /* If we can't allocate space for the filters, we just don't filter */
851 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
852 sizeof(struct wpa_driver_scan_filter));
853
854 prev_state = wpa_s->wpa_state;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700855#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
857 wpa_s->wpa_state == WPA_INACTIVE)
858 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700859#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800860
Dmitry Shmidt04949592012-07-19 12:16:46 -0700861 if (wpa_s->autoscan_params != NULL) {
862 scan_params = wpa_s->autoscan_params;
863 goto scan;
864 }
865
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800866 /* Find the starting point from which to continue scanning */
867 ssid = wpa_s->conf->ssid;
868 if (wpa_s->prev_sched_ssid) {
869 while (ssid) {
870 if (ssid == wpa_s->prev_sched_ssid) {
871 ssid = ssid->next;
872 break;
873 }
874 ssid = ssid->next;
875 }
876 }
877
878 if (!ssid || !wpa_s->prev_sched_ssid) {
879 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
880
Dmitry Shmidt04949592012-07-19 12:16:46 -0700881 if (wpa_s->sched_scan_interval == 0)
882 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800883 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
884 wpa_s->first_sched_scan = 1;
885 ssid = wpa_s->conf->ssid;
886 wpa_s->prev_sched_ssid = ssid;
887 }
888
889 if (wildcard) {
890 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
891 params.num_ssids++;
892 }
893
894 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700895 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800896 goto next;
897
898 if (params.num_filter_ssids < wpa_s->max_match_sets &&
899 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
900 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
901 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
902 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
903 ssid->ssid, ssid->ssid_len);
904 params.filter_ssids[params.num_filter_ssids].ssid_len =
905 ssid->ssid_len;
906 params.num_filter_ssids++;
907 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
908 {
909 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
910 "filter for sched_scan - drop filter");
911 os_free(params.filter_ssids);
912 params.filter_ssids = NULL;
913 params.num_filter_ssids = 0;
914 }
915
916 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
917 if (params.num_ssids == max_sched_scan_ssids)
918 break; /* only room for broadcast SSID */
919 wpa_dbg(wpa_s, MSG_DEBUG,
920 "add to active scan ssid: %s",
921 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
922 params.ssids[params.num_ssids].ssid =
923 ssid->ssid;
924 params.ssids[params.num_ssids].ssid_len =
925 ssid->ssid_len;
926 params.num_ssids++;
927 if (params.num_ssids >= max_sched_scan_ssids) {
928 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700929 do {
930 ssid = ssid->next;
931 } while (ssid &&
932 (wpas_network_disabled(wpa_s, ssid) ||
933 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800934 break;
935 }
936 }
937
938 next:
939 wpa_s->prev_sched_ssid = ssid;
940 ssid = ssid->next;
941 }
942
943 if (params.num_filter_ssids == 0) {
944 os_free(params.filter_ssids);
945 params.filter_ssids = NULL;
946 }
947
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700948 extra_ie = wpa_supplicant_extra_ies(wpa_s);
949 if (extra_ie) {
950 params.extra_ies = wpabuf_head(extra_ie);
951 params.extra_ies_len = wpabuf_len(extra_ie);
952 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800953
Dmitry Shmidt04949592012-07-19 12:16:46 -0700954 scan_params = &params;
955
956scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800957 if (ssid || !wpa_s->first_sched_scan) {
958 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800959 "Starting sched scan: interval %d timeout %d",
960 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700961 } else {
962 wpa_dbg(wpa_s, MSG_DEBUG,
963 "Starting sched scan: interval %d (no timeout)",
964 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800965 }
966
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800968 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700969 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800970 os_free(params.filter_ssids);
971 if (ret) {
972 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
973 if (prev_state != wpa_s->wpa_state)
974 wpa_supplicant_set_state(wpa_s, prev_state);
975 return ret;
976 }
977
978 /* If we have more SSIDs to scan, add a timeout so we scan them too */
979 if (ssid || !wpa_s->first_sched_scan) {
980 wpa_s->sched_scan_timed_out = 0;
981 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
982 wpa_supplicant_sched_scan_timeout,
983 wpa_s, NULL);
984 wpa_s->first_sched_scan = 0;
985 wpa_s->sched_scan_timeout /= 2;
986 wpa_s->sched_scan_interval *= 2;
987 }
988
989 return 0;
990}
991
992
993/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
995 * @wpa_s: Pointer to wpa_supplicant data
996 *
997 * This function is used to cancel a scan request scheduled with
998 * wpa_supplicant_req_scan().
999 */
1000void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1001{
1002 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1003 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001004#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001005 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001006#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007}
1008
1009
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001010/**
1011 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1012 * @wpa_s: Pointer to wpa_supplicant data
1013 *
1014 * This function is used to stop a periodic scheduled scan.
1015 */
1016void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1017{
1018 if (!wpa_s->sched_scanning)
1019 return;
1020
1021 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1022 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1023 wpa_supplicant_stop_sched_scan(wpa_s);
1024}
1025
1026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1028 int scanning)
1029{
1030 if (wpa_s->scanning != scanning) {
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001031#ifdef ANDROID_P2P
1032 if(!wpa_s->sched_scanning)
1033 wpa_s->scanning = scanning;
1034#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 wpa_s->scanning = scanning;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001036#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 wpas_notify_scanning(wpa_s);
1038 }
1039}
1040
1041
1042static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1043{
1044 int rate = 0;
1045 const u8 *ie;
1046 int i;
1047
1048 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1049 for (i = 0; ie && i < ie[1]; i++) {
1050 if ((ie[i + 2] & 0x7f) > rate)
1051 rate = ie[i + 2] & 0x7f;
1052 }
1053
1054 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1055 for (i = 0; ie && i < ie[1]; i++) {
1056 if ((ie[i + 2] & 0x7f) > rate)
1057 rate = ie[i + 2] & 0x7f;
1058 }
1059
1060 return rate;
1061}
1062
1063
1064const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1065{
1066 const u8 *end, *pos;
1067
1068 pos = (const u8 *) (res + 1);
1069 end = pos + res->ie_len;
1070
1071 while (pos + 1 < end) {
1072 if (pos + 2 + pos[1] > end)
1073 break;
1074 if (pos[0] == ie)
1075 return pos;
1076 pos += 2 + pos[1];
1077 }
1078
1079 return NULL;
1080}
1081
1082
1083const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1084 u32 vendor_type)
1085{
1086 const u8 *end, *pos;
1087
1088 pos = (const u8 *) (res + 1);
1089 end = pos + res->ie_len;
1090
1091 while (pos + 1 < end) {
1092 if (pos + 2 + pos[1] > end)
1093 break;
1094 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1095 vendor_type == WPA_GET_BE32(&pos[2]))
1096 return pos;
1097 pos += 2 + pos[1];
1098 }
1099
1100 return NULL;
1101}
1102
1103
1104struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1105 u32 vendor_type)
1106{
1107 struct wpabuf *buf;
1108 const u8 *end, *pos;
1109
1110 buf = wpabuf_alloc(res->ie_len);
1111 if (buf == NULL)
1112 return NULL;
1113
1114 pos = (const u8 *) (res + 1);
1115 end = pos + res->ie_len;
1116
1117 while (pos + 1 < end) {
1118 if (pos + 2 + pos[1] > end)
1119 break;
1120 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1121 vendor_type == WPA_GET_BE32(&pos[2]))
1122 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1123 pos += 2 + pos[1];
1124 }
1125
1126 if (wpabuf_len(buf) == 0) {
1127 wpabuf_free(buf);
1128 buf = NULL;
1129 }
1130
1131 return buf;
1132}
1133
1134
1135struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
1136 const struct wpa_scan_res *res, u32 vendor_type)
1137{
1138 struct wpabuf *buf;
1139 const u8 *end, *pos;
1140
1141 if (res->beacon_ie_len == 0)
1142 return NULL;
1143 buf = wpabuf_alloc(res->beacon_ie_len);
1144 if (buf == NULL)
1145 return NULL;
1146
1147 pos = (const u8 *) (res + 1);
1148 pos += res->ie_len;
1149 end = pos + res->beacon_ie_len;
1150
1151 while (pos + 1 < end) {
1152 if (pos + 2 + pos[1] > end)
1153 break;
1154 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1155 vendor_type == WPA_GET_BE32(&pos[2]))
1156 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1157 pos += 2 + pos[1];
1158 }
1159
1160 if (wpabuf_len(buf) == 0) {
1161 wpabuf_free(buf);
1162 buf = NULL;
1163 }
1164
1165 return buf;
1166}
1167
1168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001169/*
1170 * Channels with a great SNR can operate at full rate. What is a great SNR?
1171 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1172 * rule of thumb is that any SNR above 20 is good." This one
1173 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1174 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1175 * conservative value.
1176 */
1177#define GREAT_SNR 30
1178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179/* Compare function for sorting scan results. Return >0 if @b is considered
1180 * better. */
1181static int wpa_scan_result_compar(const void *a, const void *b)
1182{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001183#define IS_5GHZ(n) (n > 4000)
1184#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 struct wpa_scan_res **_wa = (void *) a;
1186 struct wpa_scan_res **_wb = (void *) b;
1187 struct wpa_scan_res *wa = *_wa;
1188 struct wpa_scan_res *wb = *_wb;
1189 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191
1192 /* WPA/WPA2 support preferred */
1193 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1194 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1195 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1196 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1197
1198 if (wpa_b && !wpa_a)
1199 return 1;
1200 if (!wpa_b && wpa_a)
1201 return -1;
1202
1203 /* privacy support preferred */
1204 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1205 (wb->caps & IEEE80211_CAP_PRIVACY))
1206 return 1;
1207 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1208 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1209 return -1;
1210
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001211 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1212 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1213 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1214 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1215 } else {
1216 /* Not suitable information to calculate SNR, so use level */
1217 snr_a = wa->level;
1218 snr_b = wb->level;
1219 }
1220
1221 /* best/max rate preferred if SNR close enough */
1222 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1224 maxrate_a = wpa_scan_get_max_rate(wa);
1225 maxrate_b = wpa_scan_get_max_rate(wb);
1226 if (maxrate_a != maxrate_b)
1227 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1229 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 }
1231
1232 /* use freq for channel preference */
1233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001234 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 * identical, use quality values since some drivers may only report
1236 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001239 return snr_b - snr_a;
1240#undef MIN
1241#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242}
1243
1244
1245#ifdef CONFIG_WPS
1246/* Compare function for sorting scan results when searching a WPS AP for
1247 * provisioning. Return >0 if @b is considered better. */
1248static int wpa_scan_result_wps_compar(const void *a, const void *b)
1249{
1250 struct wpa_scan_res **_wa = (void *) a;
1251 struct wpa_scan_res **_wb = (void *) b;
1252 struct wpa_scan_res *wa = *_wa;
1253 struct wpa_scan_res *wb = *_wb;
1254 int uses_wps_a, uses_wps_b;
1255 struct wpabuf *wps_a, *wps_b;
1256 int res;
1257
1258 /* Optimization - check WPS IE existence before allocated memory and
1259 * doing full reassembly. */
1260 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1261 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1262 if (uses_wps_a && !uses_wps_b)
1263 return -1;
1264 if (!uses_wps_a && uses_wps_b)
1265 return 1;
1266
1267 if (uses_wps_a && uses_wps_b) {
1268 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1269 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1270 res = wps_ap_priority_compar(wps_a, wps_b);
1271 wpabuf_free(wps_a);
1272 wpabuf_free(wps_b);
1273 if (res)
1274 return res;
1275 }
1276
1277 /*
1278 * Do not use current AP security policy as a sorting criteria during
1279 * WPS provisioning step since the AP may get reconfigured at the
1280 * completion of provisioning.
1281 */
1282
1283 /* all things being equal, use signal level; if signal levels are
1284 * identical, use quality values since some drivers may only report
1285 * that value and leave the signal level zero */
1286 if (wb->level == wa->level)
1287 return wb->qual - wa->qual;
1288 return wb->level - wa->level;
1289}
1290#endif /* CONFIG_WPS */
1291
1292
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001293static void dump_scan_res(struct wpa_scan_results *scan_res)
1294{
1295#ifndef CONFIG_NO_STDOUT_DEBUG
1296 size_t i;
1297
1298 if (scan_res->res == NULL || scan_res->num == 0)
1299 return;
1300
1301 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1302
1303 for (i = 0; i < scan_res->num; i++) {
1304 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001305 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001306 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1307 == WPA_SCAN_LEVEL_DBM) {
1308 int snr = r->level - r->noise;
1309 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1310 "noise=%d level=%d snr=%d%s flags=0x%x",
1311 MAC2STR(r->bssid), r->freq, r->qual,
1312 r->noise, r->level, snr,
1313 snr >= GREAT_SNR ? "*" : "", r->flags);
1314 } else {
1315 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1316 "noise=%d level=%d flags=0x%x",
1317 MAC2STR(r->bssid), r->freq, r->qual,
1318 r->noise, r->level, r->flags);
1319 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001320 pos = (u8 *) (r + 1);
1321 if (r->ie_len)
1322 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1323 pos += r->ie_len;
1324 if (r->beacon_ie_len)
1325 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1326 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001327 }
1328#endif /* CONFIG_NO_STDOUT_DEBUG */
1329}
1330
1331
Dmitry Shmidt04949592012-07-19 12:16:46 -07001332int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1333 const u8 *bssid)
1334{
1335 size_t i;
1336
1337 if (wpa_s->bssid_filter == NULL)
1338 return 1;
1339
1340 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1341 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1342 ETH_ALEN) == 0)
1343 return 1;
1344 }
1345
1346 return 0;
1347}
1348
1349
1350static void filter_scan_res(struct wpa_supplicant *wpa_s,
1351 struct wpa_scan_results *res)
1352{
1353 size_t i, j;
1354
1355 if (wpa_s->bssid_filter == NULL)
1356 return;
1357
1358 for (i = 0, j = 0; i < res->num; i++) {
1359 if (wpa_supplicant_filter_bssid_match(wpa_s,
1360 res->res[i]->bssid)) {
1361 res->res[j++] = res->res[i];
1362 } else {
1363 os_free(res->res[i]);
1364 res->res[i] = NULL;
1365 }
1366 }
1367
1368 if (res->num != j) {
1369 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1370 (int) (res->num - j));
1371 res->num = j;
1372 }
1373}
1374
1375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376/**
1377 * wpa_supplicant_get_scan_results - Get scan results
1378 * @wpa_s: Pointer to wpa_supplicant data
1379 * @info: Information about what was scanned or %NULL if not available
1380 * @new_scan: Whether a new scan was performed
1381 * Returns: Scan results, %NULL on failure
1382 *
1383 * This function request the current scan results from the driver and updates
1384 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1385 * results with wpa_scan_results_free().
1386 */
1387struct wpa_scan_results *
1388wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1389 struct scan_info *info, int new_scan)
1390{
1391 struct wpa_scan_results *scan_res;
1392 size_t i;
1393 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1394
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001395 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 if (scan_res == NULL) {
1397 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1398 return NULL;
1399 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001400 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401
1402#ifdef CONFIG_WPS
1403 if (wpas_wps_in_progress(wpa_s)) {
1404 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1405 "provisioning rules");
1406 compar = wpa_scan_result_wps_compar;
1407 }
1408#endif /* CONFIG_WPS */
1409
1410 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1411 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001412 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413
1414 wpa_bss_update_start(wpa_s);
1415 for (i = 0; i < scan_res->num; i++)
1416 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1417 wpa_bss_update_end(wpa_s, info, new_scan);
1418
1419 return scan_res;
1420}
1421
1422
1423int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1424{
1425 struct wpa_scan_results *scan_res;
1426 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1427 if (scan_res == NULL)
1428 return -1;
1429 wpa_scan_results_free(scan_res);
1430
1431 return 0;
1432}