blob: c59b8baba839833451030067d740ffaeac6fee80 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
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 Shmidta54fa5f2013-01-15 13:53:35 -080084/**
85 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
86 * @wpa_s: Pointer to wpa_supplicant data
87 * Returns: 0 if no networks are enabled, >0 if networks are enabled
88 *
89 * This function is used to figure out whether any networks (or Interworking
90 * with enabled credentials and auto_interworking) are present in the current
91 * configuration.
92 */
Dmitry Shmidt04949592012-07-19 12:16:46 -070093int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094{
Dmitry Shmidt04949592012-07-19 12:16:46 -070095 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070096 int count = 0, disabled = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070098 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700100 else
101 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 ssid = ssid->next;
103 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700104 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
105 wpa_s->conf->auto_interworking)
106 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700107 if (count == 0 && disabled > 0) {
108 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
109 "networks)", disabled);
110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 return count;
112}
113
114
115static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
116 struct wpa_ssid *ssid)
117{
118 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700119 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 break;
121 ssid = ssid->next;
122 }
123
124 /* ap_scan=2 mode - try to associate with each SSID. */
125 if (ssid == NULL) {
126 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
127 "end of scan list - go back to beginning");
128 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
129 wpa_supplicant_req_scan(wpa_s, 0, 0);
130 return;
131 }
132 if (ssid->next) {
133 /* Continue from the next SSID on the next attempt. */
134 wpa_s->prev_scan_ssid = ssid;
135 } else {
136 /* Start from the beginning of the SSID list. */
137 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
138 }
139 wpa_supplicant_associate(wpa_s, NULL, ssid);
140}
141
142
143static int int_array_len(const int *a)
144{
145 int i;
146 for (i = 0; a && a[i]; i++)
147 ;
148 return i;
149}
150
151
152static void int_array_concat(int **res, const int *a)
153{
154 int reslen, alen, i;
155 int *n;
156
157 reslen = int_array_len(*res);
158 alen = int_array_len(a);
159
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700160 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 if (n == NULL) {
162 os_free(*res);
163 *res = NULL;
164 return;
165 }
166 for (i = 0; i <= alen; i++)
167 n[reslen + i] = a[i];
168 *res = n;
169}
170
171
172static int freq_cmp(const void *a, const void *b)
173{
174 int _a = *(int *) a;
175 int _b = *(int *) b;
176
177 if (_a == 0)
178 return 1;
179 if (_b == 0)
180 return -1;
181 return _a - _b;
182}
183
184
185static void int_array_sort_unique(int *a)
186{
187 int alen;
188 int i, j;
189
190 if (a == NULL)
191 return;
192
193 alen = int_array_len(a);
194 qsort(a, alen, sizeof(int), freq_cmp);
195
196 i = 0;
197 j = 1;
198 while (a[i] && a[j]) {
199 if (a[i] == a[j]) {
200 j++;
201 continue;
202 }
203 a[++i] = a[j++];
204 }
205 if (a[i])
206 i++;
207 a[i] = 0;
208}
209
210
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800211/**
212 * wpa_supplicant_trigger_scan - Request driver to start a scan
213 * @wpa_s: Pointer to wpa_supplicant data
214 * @params: Scan parameters
215 * Returns: 0 on success, -1 on failure
216 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
218 struct wpa_driver_scan_params *params)
219{
220 int ret;
221
222 wpa_supplicant_notify_scanning(wpa_s, 1);
223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800224 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 if (ret) {
226 wpa_supplicant_notify_scanning(wpa_s, 0);
227 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800228 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229 wpa_s->scan_runs++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 wpa_s->normal_scans++;
231 }
232
233 return ret;
234}
235
236
237static void
238wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
239{
240 struct wpa_supplicant *wpa_s = eloop_ctx;
241
242 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
243
244 if (wpa_supplicant_req_sched_scan(wpa_s))
245 wpa_supplicant_req_scan(wpa_s, 0, 0);
246}
247
248
249static void
250wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
251{
252 struct wpa_supplicant *wpa_s = eloop_ctx;
253
254 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
255
256 wpa_s->sched_scan_timed_out = 1;
257 wpa_supplicant_cancel_sched_scan(wpa_s);
258}
259
260
261static int
262wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
263 struct wpa_driver_scan_params *params,
264 int interval)
265{
266 int ret;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700267#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800268 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700269#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800270 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
271 if (ret)
272 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700273 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800274 wpa_s->sched_scanning = 1;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700275#ifdef ANDROID_P2P
276 wpa_supplicant_notify_scanning(wpa_s, 1);
277#endif
278 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279
280 return ret;
281}
282
283
284static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
285{
286 int ret;
287
288 ret = wpa_drv_stop_sched_scan(wpa_s);
289 if (ret) {
290 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
291 /* TODO: what to do if stopping fails? */
292 return -1;
293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294
295 return ret;
296}
297
298
299static struct wpa_driver_scan_filter *
300wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
301{
302 struct wpa_driver_scan_filter *ssids;
303 struct wpa_ssid *ssid;
304 size_t count;
305
306 *num_ssids = 0;
307 if (!conf->filter_ssids)
308 return NULL;
309
310 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
311 if (ssid->ssid && ssid->ssid_len)
312 count++;
313 }
314 if (count == 0)
315 return NULL;
316 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
317 if (ssids == NULL)
318 return NULL;
319
320 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
321 if (!ssid->ssid || !ssid->ssid_len)
322 continue;
323 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
324 ssids[*num_ssids].ssid_len = ssid->ssid_len;
325 (*num_ssids)++;
326 }
327
328 return ssids;
329}
330
331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800332static void wpa_supplicant_optimize_freqs(
333 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
334{
335#ifdef CONFIG_P2P
336 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
337 wpa_s->go_params) {
338 /* Optimize provisioning state scan based on GO information */
339 if (wpa_s->p2p_in_provisioning < 5 &&
340 wpa_s->go_params->freq > 0) {
341 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
342 "preferred frequency %d MHz",
343 wpa_s->go_params->freq);
344 params->freqs = os_zalloc(2 * sizeof(int));
345 if (params->freqs)
346 params->freqs[0] = wpa_s->go_params->freq;
347 } else if (wpa_s->p2p_in_provisioning < 8 &&
348 wpa_s->go_params->freq_list[0]) {
349 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
350 "channels");
351 int_array_concat(&params->freqs,
352 wpa_s->go_params->freq_list);
353 if (params->freqs)
354 int_array_sort_unique(params->freqs);
355 }
356 wpa_s->p2p_in_provisioning++;
357 }
358#endif /* CONFIG_P2P */
359
360#ifdef CONFIG_WPS
361 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
362 /*
363 * Optimize post-provisioning scan based on channel used
364 * during provisioning.
365 */
366 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
367 "that was used during provisioning", wpa_s->wps_freq);
368 params->freqs = os_zalloc(2 * sizeof(int));
369 if (params->freqs)
370 params->freqs[0] = wpa_s->wps_freq;
371 wpa_s->after_wps--;
372 }
373
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800374 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
375 {
376 /* Optimize provisioning scan based on already known channel */
377 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
378 wpa_s->wps_freq);
379 params->freqs = os_zalloc(2 * sizeof(int));
380 if (params->freqs)
381 params->freqs[0] = wpa_s->wps_freq;
382 wpa_s->known_wps_freq = 0; /* only do this once */
383 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800384#endif /* CONFIG_WPS */
385}
386
387
388#ifdef CONFIG_INTERWORKING
389static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
390 struct wpabuf *buf)
391{
392 if (wpa_s->conf->interworking == 0)
393 return;
394
395 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
396 wpabuf_put_u8(buf, 4);
397 wpabuf_put_u8(buf, 0x00);
398 wpabuf_put_u8(buf, 0x00);
399 wpabuf_put_u8(buf, 0x00);
400 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
401
402 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
403 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
404 1 + ETH_ALEN);
405 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
406 /* No Venue Info */
407 if (!is_zero_ether_addr(wpa_s->conf->hessid))
408 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
409}
410#endif /* CONFIG_INTERWORKING */
411
412
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700413static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800414{
415 struct wpabuf *extra_ie = NULL;
416#ifdef CONFIG_WPS
417 int wps = 0;
418 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
419#endif /* CONFIG_WPS */
420
421#ifdef CONFIG_INTERWORKING
422 if (wpa_s->conf->interworking &&
423 wpabuf_resize(&extra_ie, 100) == 0)
424 wpas_add_interworking_elements(wpa_s, extra_ie);
425#endif /* CONFIG_INTERWORKING */
426
427#ifdef CONFIG_WPS
428 wps = wpas_wps_in_use(wpa_s, &req_type);
429
430 if (wps) {
431 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700432 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
433 DEV_PW_DEFAULT,
434 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800435 wpa_s->wps->uuid, req_type,
436 0, NULL);
437 if (wps_ie) {
438 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
439 wpabuf_put_buf(extra_ie, wps_ie);
440 wpabuf_free(wps_ie);
441 }
442 }
443
444#ifdef CONFIG_P2P
445 if (wps) {
446 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
447 if (wpabuf_resize(&extra_ie, ielen) == 0)
448 wpas_p2p_scan_ie(wpa_s, extra_ie);
449 }
450#endif /* CONFIG_P2P */
451
452#endif /* CONFIG_WPS */
453
454 return extra_ie;
455}
456
457
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800458#ifdef CONFIG_P2P
459
460/*
461 * Check whether there are any enabled networks or credentials that could be
462 * used for a non-P2P connection.
463 */
464static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
465{
466 struct wpa_ssid *ssid;
467
468 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
469 if (wpas_network_disabled(wpa_s, ssid))
470 continue;
471 if (!ssid->p2p_group)
472 return 1;
473 }
474
475 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
476 wpa_s->conf->auto_interworking)
477 return 1;
478
479 return 0;
480}
481
482
483/*
484 * Find the operating frequency of any other virtual interface that is using
485 * the same radio concurrently.
486 */
487static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
488{
489 const char *rn, *rn2;
490 struct wpa_supplicant *ifs;
491 u8 bssid[ETH_ALEN];
492
493 if (!wpa_s->driver->get_radio_name)
494 return -1;
495
496 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
497 if (rn == NULL || rn[0] == '\0')
498 return -1;
499
500 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
501 if (ifs == wpa_s || !ifs->driver->get_radio_name)
502 continue;
503
504 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
505 if (!rn2 || os_strcmp(rn, rn2) != 0)
506 continue;
507
508 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
509 continue;
510
511 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
512 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
513 return ifs->current_ssid->frequency;
514 if (wpa_drv_get_bssid(ifs, bssid) == 0)
515 return ifs->assoc_freq;
516 }
517
518 return 0;
519}
520
521#endif /* CONFIG_P2P */
522
523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
525{
526 struct wpa_supplicant *wpa_s = eloop_ctx;
527 struct wpa_ssid *ssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800528 enum scan_req_type scan_req = NORMAL_SCAN_REQ;
529 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700530 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700532 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 size_t max_ssids;
534 enum wpa_states prev_state;
535
536 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
537 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
538 return;
539 }
540
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800541 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700542 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
544 return;
545 }
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700546#ifdef ANDROID
547 if (wpa_s->scanning) {
548 /* If we are already in scanning state, we shall ignore this new scan request*/
549 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning");
550 return;
551 }
552#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700553 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800554 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
556 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700557#ifdef CONFIG_P2P
558 wpa_s->sta_scan_pending = 0;
559#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 return;
561 }
562
563 if (wpa_s->conf->ap_scan != 0 &&
564 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
565 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
566 "overriding ap_scan configuration");
567 wpa_s->conf->ap_scan = 0;
568 wpas_notify_ap_scan_changed(wpa_s);
569 }
570
571 if (wpa_s->conf->ap_scan == 0) {
572 wpa_supplicant_gen_assoc_event(wpa_s);
573 return;
574 }
575
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800576#ifdef CONFIG_P2P
577 if (wpas_p2p_in_progress(wpa_s)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700578 if (wpa_s->sta_scan_pending &&
579 wpas_p2p_in_progress(wpa_s) == 2 &&
Jouni Malinendc7b7132012-09-14 12:53:47 -0700580 wpa_s->global->p2p_cb_on_scan_complete) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700581 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
582 "mode scan during P2P search");
583 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800584 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
585 "while P2P operation is in progress");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700586 wpa_s->sta_scan_pending = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800587 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700588 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800589 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590 }
591#endif /* CONFIG_P2P */
592
593 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594 max_ssids = 1;
595 else {
596 max_ssids = wpa_s->max_scan_ssids;
597 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
598 max_ssids = WPAS_MAX_SCAN_SSIDS;
599 }
600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800602 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603
604 os_memset(&params, 0, sizeof(params));
605
606 prev_state = wpa_s->wpa_state;
607 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
608 wpa_s->wpa_state == WPA_INACTIVE)
609 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
610
Dmitry Shmidt04949592012-07-19 12:16:46 -0700611 /*
612 * If autoscan has set its own scanning parameters
613 */
614 if (wpa_s->autoscan_params != NULL) {
615 scan_params = wpa_s->autoscan_params;
616 goto scan;
617 }
618
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800619 if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700620 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
621 if (ssid == wpa_s->connect_without_scan)
622 break;
623 }
624 wpa_s->connect_without_scan = NULL;
625 if (ssid) {
626 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
627 "without scan step");
628 wpa_supplicant_associate(wpa_s, NULL, ssid);
629 return;
630 }
631 }
632
Dmitry Shmidt04949592012-07-19 12:16:46 -0700633#ifdef CONFIG_P2P
634 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
635 wpa_s->go_params) {
636 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
637 "P2P group formation");
638 params.ssids[0].ssid = wpa_s->go_params->ssid;
639 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
640 params.num_ssids = 1;
641 goto ssid_list_set;
642 }
643#endif /* CONFIG_P2P */
644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 /* Find the starting point from which to continue scanning */
646 ssid = wpa_s->conf->ssid;
647 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
648 while (ssid) {
649 if (ssid == wpa_s->prev_scan_ssid) {
650 ssid = ssid->next;
651 break;
652 }
653 ssid = ssid->next;
654 }
655 }
656
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800657 if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700658 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800659 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660 wpa_supplicant_assoc_try(wpa_s, ssid);
661 return;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700662#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 } else if (wpa_s->conf->ap_scan == 2) {
664 /*
665 * User-initiated scan request in ap_scan == 2; scan with
666 * wildcard SSID.
667 */
668 ssid = NULL;
Dmitry Shmidte25ba152011-08-22 15:04:04 -0700669#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 } else {
671 struct wpa_ssid *start = ssid, *tssid;
672 int freqs_set = 0;
673 if (ssid == NULL && max_ssids > 1)
674 ssid = wpa_s->conf->ssid;
675 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700676 if (!wpas_network_disabled(wpa_s, ssid) &&
677 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
679 ssid->ssid, ssid->ssid_len);
680 params.ssids[params.num_ssids].ssid =
681 ssid->ssid;
682 params.ssids[params.num_ssids].ssid_len =
683 ssid->ssid_len;
684 params.num_ssids++;
685 if (params.num_ssids + 1 >= max_ssids)
686 break;
687 }
688 ssid = ssid->next;
689 if (ssid == start)
690 break;
691 if (ssid == NULL && max_ssids > 1 &&
692 start != wpa_s->conf->ssid)
693 ssid = wpa_s->conf->ssid;
694 }
695
696 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700697 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 continue;
699 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
700 int_array_concat(&params.freqs,
701 tssid->scan_freq);
702 } else {
703 os_free(params.freqs);
704 params.freqs = NULL;
705 }
706 freqs_set = 1;
707 }
708 int_array_sort_unique(params.freqs);
709 }
710
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800711 if (ssid && max_ssids == 1) {
712 /*
713 * If the driver is limited to 1 SSID at a time interleave
714 * wildcard SSID scans with specific SSID scans to avoid
715 * waiting a long time for a wildcard scan.
716 */
717 if (!wpa_s->prev_scan_wildcard) {
718 params.ssids[0].ssid = NULL;
719 params.ssids[0].ssid_len = 0;
720 wpa_s->prev_scan_wildcard = 1;
721 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
722 "wildcard SSID (Interleave with specific)");
723 } else {
724 wpa_s->prev_scan_ssid = ssid;
725 wpa_s->prev_scan_wildcard = 0;
726 wpa_dbg(wpa_s, MSG_DEBUG,
727 "Starting AP scan for specific SSID: %s",
728 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800730 } else if (ssid) {
731 /* max_ssids > 1 */
732
733 wpa_s->prev_scan_ssid = ssid;
734 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
735 "the scan request");
736 params.num_ssids++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 } else {
738 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
739 params.num_ssids++;
740 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
741 "SSID");
742 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700743#ifdef CONFIG_P2P
744ssid_list_set:
745#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700748 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749
Dmitry Shmidt04949592012-07-19 12:16:46 -0700750#ifdef CONFIG_HS20
751 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
752 wpas_hs20_add_indication(extra_ie);
753#endif /* CONFIG_HS20 */
754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
756 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
757 "generated frequency list");
758 params.freqs = wpa_s->next_scan_freqs;
759 } else
760 os_free(wpa_s->next_scan_freqs);
761 wpa_s->next_scan_freqs = NULL;
762
763 params.filter_ssids = wpa_supplicant_build_filter_ssids(
764 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800765 if (extra_ie) {
766 params.extra_ies = wpabuf_head(extra_ie);
767 params.extra_ies_len = wpabuf_len(extra_ie);
768 }
769
770#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700771 if (wpa_s->p2p_in_provisioning ||
772 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800773 /*
774 * The interface may not yet be in P2P mode, so we have to
775 * explicitly request P2P probe to disable CCK rates.
776 */
777 params.p2p_probe = 1;
778 }
779#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780
Dmitry Shmidt04949592012-07-19 12:16:46 -0700781 scan_params = &params;
782
783scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800784#ifdef CONFIG_P2P
785 /*
786 * If the driver does not support multi-channel concurrency and a
787 * virtual interface that shares the same radio with the wpa_s interface
788 * is operating there may not be need to scan other channels apart from
789 * the current operating channel on the other virtual interface. Filter
790 * out other channels in case we are trying to find a connection for a
791 * station interface when we are not configured to prefer station
792 * connection and a concurrent operation is already in process.
793 */
794 if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
795 !scan_params->freqs && !params.freqs &&
796 wpas_is_p2p_prioritized(wpa_s) &&
797 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
798 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
799 non_p2p_network_enabled(wpa_s)) {
800 int freq = shared_vif_oper_freq(wpa_s);
801 if (freq > 0) {
802 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
803 "operating channel (%d MHz) since driver does "
804 "not support multi-channel concurrency", freq);
805 params.freqs = os_zalloc(sizeof(int) * 2);
806 if (params.freqs)
807 params.freqs[0] = freq;
808 scan_params->freqs = params.freqs;
809 }
810 }
811#endif /* CONFIG_P2P */
812
Dmitry Shmidt04949592012-07-19 12:16:46 -0700813 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800815 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 os_free(params.freqs);
817 os_free(params.filter_ssids);
818
819 if (ret) {
820 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
821 if (prev_state != wpa_s->wpa_state)
822 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800823 /* Restore scan_req since we will try to scan again */
824 wpa_s->scan_req = scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800826 } else {
827 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 }
829}
830
831
832/**
833 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
834 * @wpa_s: Pointer to wpa_supplicant data
835 * @sec: Number of seconds after which to scan
836 * @usec: Number of microseconds after which to scan
837 *
838 * This function is used to schedule a scan for neighboring access points after
839 * the specified time.
840 */
841void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
842{
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800843#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 /* If there's at least one network that should be specifically scanned
845 * then don't cancel the scan and reschedule. Some drivers do
846 * background scanning which generates frequent scan results, and that
847 * causes the specific SSID scan to get continually pushed back and
848 * never happen, which causes hidden APs to never get probe-scanned.
849 */
850 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
851 wpa_s->conf->ap_scan == 1) {
852 struct wpa_ssid *ssid = wpa_s->conf->ssid;
853
854 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700855 if (!wpas_network_disabled(wpa_s, ssid) &&
856 ssid->scan_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 break;
858 ssid = ssid->next;
859 }
860 if (ssid) {
861 wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
862 "ensure that specific SSID scans occur");
863 return;
864 }
865 }
Dmitry Shmidt81e97002011-11-14 15:53:31 -0800866#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
868 sec, usec);
869 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
870 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
871}
872
873
874/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800875 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
876 * @wpa_s: Pointer to wpa_supplicant data
877 * @sec: Number of seconds after which to scan
878 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800879 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800880 *
881 * This function is used to schedule periodic scans for neighboring
882 * access points after the specified time.
883 */
884int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
885 int sec, int usec)
886{
887 if (!wpa_s->sched_scan_supported)
888 return -1;
889
890 eloop_register_timeout(sec, usec,
891 wpa_supplicant_delayed_sched_scan_timeout,
892 wpa_s, NULL);
893
894 return 0;
895}
896
897
898/**
899 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
900 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800901 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 *
903 * This function is used to schedule periodic scans for neighboring
904 * access points repeating the scan continuously.
905 */
906int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
907{
908 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700909 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800910 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700911 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700912 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800913 int ret;
914 unsigned int max_sched_scan_ssids;
915 int wildcard = 0;
916 int need_ssids;
917
918 if (!wpa_s->sched_scan_supported)
919 return -1;
920
921 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
922 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
923 else
924 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700925 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800926 return -1;
927
928 if (wpa_s->sched_scanning) {
929 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
930 return 0;
931 }
932
933 need_ssids = 0;
934 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700935 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800936 /* Use wildcard SSID to find this network */
937 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700938 } else if (!wpas_network_disabled(wpa_s, ssid) &&
939 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700941
942#ifdef CONFIG_WPS
943 if (!wpas_network_disabled(wpa_s, ssid) &&
944 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
945 /*
946 * Normal scan is more reliable and faster for WPS
947 * operations and since these are for short periods of
948 * time, the benefit of trying to use sched_scan would
949 * be limited.
950 */
951 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
952 "sched_scan for WPS");
953 return -1;
954 }
955#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800956 }
957 if (wildcard)
958 need_ssids++;
959
960 if (wpa_s->normal_scans < 3 &&
961 (need_ssids <= wpa_s->max_scan_ssids ||
962 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
963 /*
964 * When normal scan can speed up operations, use that for the
965 * first operations before starting the sched_scan to allow
966 * user space sleep more. We do this only if the normal scan
967 * has functionality that is suitable for this or if the
968 * sched_scan does not have better support for multiple SSIDs.
969 */
970 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
971 "sched_scan for initial scans (normal_scans=%d)",
972 wpa_s->normal_scans);
973 return -1;
974 }
975
976 os_memset(&params, 0, sizeof(params));
977
978 /* If we can't allocate space for the filters, we just don't filter */
979 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
980 sizeof(struct wpa_driver_scan_filter));
981
982 prev_state = wpa_s->wpa_state;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700983#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800984 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
985 wpa_s->wpa_state == WPA_INACTIVE)
986 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700987#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989 if (wpa_s->autoscan_params != NULL) {
990 scan_params = wpa_s->autoscan_params;
991 goto scan;
992 }
993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800994 /* Find the starting point from which to continue scanning */
995 ssid = wpa_s->conf->ssid;
996 if (wpa_s->prev_sched_ssid) {
997 while (ssid) {
998 if (ssid == wpa_s->prev_sched_ssid) {
999 ssid = ssid->next;
1000 break;
1001 }
1002 ssid = ssid->next;
1003 }
1004 }
1005
1006 if (!ssid || !wpa_s->prev_sched_ssid) {
1007 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1008
Dmitry Shmidt04949592012-07-19 12:16:46 -07001009 if (wpa_s->sched_scan_interval == 0)
1010 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001011 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1012 wpa_s->first_sched_scan = 1;
1013 ssid = wpa_s->conf->ssid;
1014 wpa_s->prev_sched_ssid = ssid;
1015 }
1016
1017 if (wildcard) {
1018 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1019 params.num_ssids++;
1020 }
1021
1022 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001023 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024 goto next;
1025
1026 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1027 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1028 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1029 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1030 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1031 ssid->ssid, ssid->ssid_len);
1032 params.filter_ssids[params.num_filter_ssids].ssid_len =
1033 ssid->ssid_len;
1034 params.num_filter_ssids++;
1035 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1036 {
1037 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1038 "filter for sched_scan - drop filter");
1039 os_free(params.filter_ssids);
1040 params.filter_ssids = NULL;
1041 params.num_filter_ssids = 0;
1042 }
1043
1044 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1045 if (params.num_ssids == max_sched_scan_ssids)
1046 break; /* only room for broadcast SSID */
1047 wpa_dbg(wpa_s, MSG_DEBUG,
1048 "add to active scan ssid: %s",
1049 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1050 params.ssids[params.num_ssids].ssid =
1051 ssid->ssid;
1052 params.ssids[params.num_ssids].ssid_len =
1053 ssid->ssid_len;
1054 params.num_ssids++;
1055 if (params.num_ssids >= max_sched_scan_ssids) {
1056 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001057 do {
1058 ssid = ssid->next;
1059 } while (ssid &&
1060 (wpas_network_disabled(wpa_s, ssid) ||
1061 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001062 break;
1063 }
1064 }
1065
1066 next:
1067 wpa_s->prev_sched_ssid = ssid;
1068 ssid = ssid->next;
1069 }
1070
1071 if (params.num_filter_ssids == 0) {
1072 os_free(params.filter_ssids);
1073 params.filter_ssids = NULL;
1074 }
1075
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001076 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1077 if (extra_ie) {
1078 params.extra_ies = wpabuf_head(extra_ie);
1079 params.extra_ies_len = wpabuf_len(extra_ie);
1080 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001081
Dmitry Shmidt04949592012-07-19 12:16:46 -07001082 scan_params = &params;
1083
1084scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001085 if (ssid || !wpa_s->first_sched_scan) {
1086 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001087 "Starting sched scan: interval %d timeout %d",
1088 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001089 } else {
1090 wpa_dbg(wpa_s, MSG_DEBUG,
1091 "Starting sched scan: interval %d (no timeout)",
1092 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001093 }
1094
Dmitry Shmidt04949592012-07-19 12:16:46 -07001095 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001096 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001097 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001098 os_free(params.filter_ssids);
1099 if (ret) {
1100 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1101 if (prev_state != wpa_s->wpa_state)
1102 wpa_supplicant_set_state(wpa_s, prev_state);
1103 return ret;
1104 }
1105
1106 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1107 if (ssid || !wpa_s->first_sched_scan) {
1108 wpa_s->sched_scan_timed_out = 0;
1109 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1110 wpa_supplicant_sched_scan_timeout,
1111 wpa_s, NULL);
1112 wpa_s->first_sched_scan = 0;
1113 wpa_s->sched_scan_timeout /= 2;
1114 wpa_s->sched_scan_interval *= 2;
1115 }
1116
1117 return 0;
1118}
1119
1120
1121/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1123 * @wpa_s: Pointer to wpa_supplicant data
1124 *
1125 * This function is used to cancel a scan request scheduled with
1126 * wpa_supplicant_req_scan().
1127 */
1128void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1129{
1130 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1131 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001132#ifdef ANDROID
Dmitry Shmidt20df8072011-04-08 15:35:17 -07001133 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001134#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135}
1136
1137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001138/**
1139 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1140 * @wpa_s: Pointer to wpa_supplicant data
1141 *
1142 * This function is used to stop a periodic scheduled scan.
1143 */
1144void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1145{
1146 if (!wpa_s->sched_scanning)
1147 return;
1148
1149 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1150 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1151 wpa_supplicant_stop_sched_scan(wpa_s);
1152}
1153
1154
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001155/**
1156 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1157 * @wpa_s: Pointer to wpa_supplicant data
1158 * @scanning: Whether scanning is currently in progress
1159 *
1160 * This function is to generate scanning notifycations. It is called whenever
1161 * there may have been a change in scanning (scan started, completed, stopped).
1162 * wpas_notify_scanning() is called whenever the scanning state changed from the
1163 * previously notified state.
1164 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1166 int scanning)
1167{
1168 if (wpa_s->scanning != scanning) {
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001169#ifdef ANDROID_P2P
1170 if(!wpa_s->sched_scanning)
1171 wpa_s->scanning = scanning;
1172#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 wpa_s->scanning = scanning;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07001174#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 wpas_notify_scanning(wpa_s);
1176 }
1177}
1178
1179
1180static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1181{
1182 int rate = 0;
1183 const u8 *ie;
1184 int i;
1185
1186 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1187 for (i = 0; ie && i < ie[1]; i++) {
1188 if ((ie[i + 2] & 0x7f) > rate)
1189 rate = ie[i + 2] & 0x7f;
1190 }
1191
1192 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1193 for (i = 0; ie && i < ie[1]; i++) {
1194 if ((ie[i + 2] & 0x7f) > rate)
1195 rate = ie[i + 2] & 0x7f;
1196 }
1197
1198 return rate;
1199}
1200
1201
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001202/**
1203 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1204 * @res: Scan result entry
1205 * @ie: Information element identitifier (WLAN_EID_*)
1206 * Returns: Pointer to the information element (id field) or %NULL if not found
1207 *
1208 * This function returns the first matching information element in the scan
1209 * result.
1210 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1212{
1213 const u8 *end, *pos;
1214
1215 pos = (const u8 *) (res + 1);
1216 end = pos + res->ie_len;
1217
1218 while (pos + 1 < end) {
1219 if (pos + 2 + pos[1] > end)
1220 break;
1221 if (pos[0] == ie)
1222 return pos;
1223 pos += 2 + pos[1];
1224 }
1225
1226 return NULL;
1227}
1228
1229
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001230/**
1231 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1232 * @res: Scan result entry
1233 * @vendor_type: Vendor type (four octets starting the IE payload)
1234 * Returns: Pointer to the information element (id field) or %NULL if not found
1235 *
1236 * This function returns the first matching information element in the scan
1237 * result.
1238 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1240 u32 vendor_type)
1241{
1242 const u8 *end, *pos;
1243
1244 pos = (const u8 *) (res + 1);
1245 end = pos + res->ie_len;
1246
1247 while (pos + 1 < end) {
1248 if (pos + 2 + pos[1] > end)
1249 break;
1250 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1251 vendor_type == WPA_GET_BE32(&pos[2]))
1252 return pos;
1253 pos += 2 + pos[1];
1254 }
1255
1256 return NULL;
1257}
1258
1259
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001260/**
1261 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1262 * @res: Scan result entry
1263 * @vendor_type: Vendor type (four octets starting the IE payload)
1264 * Returns: Pointer to the information element payload or %NULL if not found
1265 *
1266 * This function returns concatenated payload of possibly fragmented vendor
1267 * specific information elements in the scan result. The caller is responsible
1268 * for freeing the returned buffer.
1269 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1271 u32 vendor_type)
1272{
1273 struct wpabuf *buf;
1274 const u8 *end, *pos;
1275
1276 buf = wpabuf_alloc(res->ie_len);
1277 if (buf == NULL)
1278 return NULL;
1279
1280 pos = (const u8 *) (res + 1);
1281 end = pos + res->ie_len;
1282
1283 while (pos + 1 < end) {
1284 if (pos + 2 + pos[1] > end)
1285 break;
1286 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1287 vendor_type == WPA_GET_BE32(&pos[2]))
1288 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1289 pos += 2 + pos[1];
1290 }
1291
1292 if (wpabuf_len(buf) == 0) {
1293 wpabuf_free(buf);
1294 buf = NULL;
1295 }
1296
1297 return buf;
1298}
1299
1300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001301/*
1302 * Channels with a great SNR can operate at full rate. What is a great SNR?
1303 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1304 * rule of thumb is that any SNR above 20 is good." This one
1305 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1306 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1307 * conservative value.
1308 */
1309#define GREAT_SNR 30
1310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311/* Compare function for sorting scan results. Return >0 if @b is considered
1312 * better. */
1313static int wpa_scan_result_compar(const void *a, const void *b)
1314{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001315#define IS_5GHZ(n) (n > 4000)
1316#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 struct wpa_scan_res **_wa = (void *) a;
1318 struct wpa_scan_res **_wb = (void *) b;
1319 struct wpa_scan_res *wa = *_wa;
1320 struct wpa_scan_res *wb = *_wb;
1321 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323
1324 /* WPA/WPA2 support preferred */
1325 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1326 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1327 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1328 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1329
1330 if (wpa_b && !wpa_a)
1331 return 1;
1332 if (!wpa_b && wpa_a)
1333 return -1;
1334
1335 /* privacy support preferred */
1336 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1337 (wb->caps & IEEE80211_CAP_PRIVACY))
1338 return 1;
1339 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1340 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1341 return -1;
1342
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001343 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1344 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1345 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1346 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1347 } else {
1348 /* Not suitable information to calculate SNR, so use level */
1349 snr_a = wa->level;
1350 snr_b = wb->level;
1351 }
1352
1353 /* best/max rate preferred if SNR close enough */
1354 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1356 maxrate_a = wpa_scan_get_max_rate(wa);
1357 maxrate_b = wpa_scan_get_max_rate(wb);
1358 if (maxrate_a != maxrate_b)
1359 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001360 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1361 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 }
1363
1364 /* use freq for channel preference */
1365
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001366 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 * identical, use quality values since some drivers may only report
1368 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001369 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001371 return snr_b - snr_a;
1372#undef MIN
1373#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374}
1375
1376
1377#ifdef CONFIG_WPS
1378/* Compare function for sorting scan results when searching a WPS AP for
1379 * provisioning. Return >0 if @b is considered better. */
1380static int wpa_scan_result_wps_compar(const void *a, const void *b)
1381{
1382 struct wpa_scan_res **_wa = (void *) a;
1383 struct wpa_scan_res **_wb = (void *) b;
1384 struct wpa_scan_res *wa = *_wa;
1385 struct wpa_scan_res *wb = *_wb;
1386 int uses_wps_a, uses_wps_b;
1387 struct wpabuf *wps_a, *wps_b;
1388 int res;
1389
1390 /* Optimization - check WPS IE existence before allocated memory and
1391 * doing full reassembly. */
1392 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1393 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1394 if (uses_wps_a && !uses_wps_b)
1395 return -1;
1396 if (!uses_wps_a && uses_wps_b)
1397 return 1;
1398
1399 if (uses_wps_a && uses_wps_b) {
1400 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1401 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1402 res = wps_ap_priority_compar(wps_a, wps_b);
1403 wpabuf_free(wps_a);
1404 wpabuf_free(wps_b);
1405 if (res)
1406 return res;
1407 }
1408
1409 /*
1410 * Do not use current AP security policy as a sorting criteria during
1411 * WPS provisioning step since the AP may get reconfigured at the
1412 * completion of provisioning.
1413 */
1414
1415 /* all things being equal, use signal level; if signal levels are
1416 * identical, use quality values since some drivers may only report
1417 * that value and leave the signal level zero */
1418 if (wb->level == wa->level)
1419 return wb->qual - wa->qual;
1420 return wb->level - wa->level;
1421}
1422#endif /* CONFIG_WPS */
1423
1424
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001425static void dump_scan_res(struct wpa_scan_results *scan_res)
1426{
1427#ifndef CONFIG_NO_STDOUT_DEBUG
1428 size_t i;
1429
1430 if (scan_res->res == NULL || scan_res->num == 0)
1431 return;
1432
1433 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1434
1435 for (i = 0; i < scan_res->num; i++) {
1436 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001437 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001438 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1439 == WPA_SCAN_LEVEL_DBM) {
1440 int snr = r->level - r->noise;
1441 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1442 "noise=%d level=%d snr=%d%s flags=0x%x",
1443 MAC2STR(r->bssid), r->freq, r->qual,
1444 r->noise, r->level, snr,
1445 snr >= GREAT_SNR ? "*" : "", r->flags);
1446 } else {
1447 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1448 "noise=%d level=%d flags=0x%x",
1449 MAC2STR(r->bssid), r->freq, r->qual,
1450 r->noise, r->level, r->flags);
1451 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001452 pos = (u8 *) (r + 1);
1453 if (r->ie_len)
1454 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1455 pos += r->ie_len;
1456 if (r->beacon_ie_len)
1457 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1458 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001459 }
1460#endif /* CONFIG_NO_STDOUT_DEBUG */
1461}
1462
1463
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001464/**
1465 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1466 * @wpa_s: Pointer to wpa_supplicant data
1467 * @bssid: BSSID to check
1468 * Returns: 0 if the BSSID is filtered or 1 if not
1469 *
1470 * This function is used to filter out specific BSSIDs from scan reslts mainly
1471 * for testing purposes (SET bssid_filter ctrl_iface command).
1472 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001473int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1474 const u8 *bssid)
1475{
1476 size_t i;
1477
1478 if (wpa_s->bssid_filter == NULL)
1479 return 1;
1480
1481 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1482 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1483 ETH_ALEN) == 0)
1484 return 1;
1485 }
1486
1487 return 0;
1488}
1489
1490
1491static void filter_scan_res(struct wpa_supplicant *wpa_s,
1492 struct wpa_scan_results *res)
1493{
1494 size_t i, j;
1495
1496 if (wpa_s->bssid_filter == NULL)
1497 return;
1498
1499 for (i = 0, j = 0; i < res->num; i++) {
1500 if (wpa_supplicant_filter_bssid_match(wpa_s,
1501 res->res[i]->bssid)) {
1502 res->res[j++] = res->res[i];
1503 } else {
1504 os_free(res->res[i]);
1505 res->res[i] = NULL;
1506 }
1507 }
1508
1509 if (res->num != j) {
1510 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1511 (int) (res->num - j));
1512 res->num = j;
1513 }
1514}
1515
1516
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001517/**
1518 * wpa_supplicant_get_scan_results - Get scan results
1519 * @wpa_s: Pointer to wpa_supplicant data
1520 * @info: Information about what was scanned or %NULL if not available
1521 * @new_scan: Whether a new scan was performed
1522 * Returns: Scan results, %NULL on failure
1523 *
1524 * This function request the current scan results from the driver and updates
1525 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1526 * results with wpa_scan_results_free().
1527 */
1528struct wpa_scan_results *
1529wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1530 struct scan_info *info, int new_scan)
1531{
1532 struct wpa_scan_results *scan_res;
1533 size_t i;
1534 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1535
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001536 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 if (scan_res == NULL) {
1538 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1539 return NULL;
1540 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001541 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542
1543#ifdef CONFIG_WPS
1544 if (wpas_wps_in_progress(wpa_s)) {
1545 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1546 "provisioning rules");
1547 compar = wpa_scan_result_wps_compar;
1548 }
1549#endif /* CONFIG_WPS */
1550
1551 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1552 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001553 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554
1555 wpa_bss_update_start(wpa_s);
1556 for (i = 0; i < scan_res->num; i++)
1557 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
1558 wpa_bss_update_end(wpa_s, info, new_scan);
1559
1560 return scan_res;
1561}
1562
1563
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001564/**
1565 * wpa_supplicant_update_scan_results - Update scan results from the driver
1566 * @wpa_s: Pointer to wpa_supplicant data
1567 * Returns: 0 on success, -1 on failure
1568 *
1569 * This function updates the BSS table within wpa_supplicant based on the
1570 * currently available scan results from the driver without requesting a new
1571 * scan. This is used in cases where the driver indicates an association
1572 * (including roaming within ESS) and wpa_supplicant does not yet have the
1573 * needed information to complete the connection (e.g., to perform validation
1574 * steps in 4-way handshake).
1575 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1577{
1578 struct wpa_scan_results *scan_res;
1579 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1580 if (scan_res == NULL)
1581 return -1;
1582 wpa_scan_results_free(scan_res);
1583
1584 return 0;
1585}