blob: c2a5cfd0887999821c8fffa40cd867349d2893b0 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2003-2014, 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"
Dmitry Shmidt3a787e62013-01-17 10:32:35 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "config.h"
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "wps_supplicant.h"
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "notify.h"
23#include "bss.h"
24#include "scan.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080025#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026
27
28static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29{
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
32
33 ssid = wpa_supplicant_get_ssid(wpa_s);
34 if (ssid == NULL)
35 return;
36
37 if (wpa_s->current_ssid == NULL) {
38 wpa_s->current_ssid = ssid;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070039 wpas_notify_network_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040 }
41 wpa_supplicant_initiate_eapol(wpa_s);
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
49#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 enum wps_request_type *req_type)
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
55
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
61 *req_type = wpas_wps_get_req_type(ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -070062 if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 return 2;
64 }
65
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080067 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
68 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080069 wpa_s->wps->dev.p2p = 1;
70 if (!wps) {
71 wps = 1;
72 *req_type = WPS_REQ_ENROLLEE_INFO;
73 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080074 }
75#endif /* CONFIG_P2P */
76
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 return wps;
78}
79#endif /* CONFIG_WPS */
80
81
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080082/**
83 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
84 * @wpa_s: Pointer to wpa_supplicant data
85 * Returns: 0 if no networks are enabled, >0 if networks are enabled
86 *
87 * This function is used to figure out whether any networks (or Interworking
88 * with enabled credentials and auto_interworking) are present in the current
89 * configuration.
90 */
Dmitry Shmidt04949592012-07-19 12:16:46 -070091int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092{
Dmitry Shmidt04949592012-07-19 12:16:46 -070093 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070094 int count = 0, disabled = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080095
96 if (wpa_s->p2p_mgmt)
97 return 0; /* no normal network profiles on p2p_mgmt interface */
98
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700100 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700102 else
103 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 ssid = ssid->next;
105 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700106 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
107 wpa_s->conf->auto_interworking)
108 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700109 if (count == 0 && disabled > 0) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
111 "networks)", disabled);
112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 return count;
114}
115
116
117static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
118 struct wpa_ssid *ssid)
119{
120 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700121 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 break;
123 ssid = ssid->next;
124 }
125
126 /* ap_scan=2 mode - try to associate with each SSID. */
127 if (ssid == NULL) {
128 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
129 "end of scan list - go back to beginning");
130 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
131 wpa_supplicant_req_scan(wpa_s, 0, 0);
132 return;
133 }
134 if (ssid->next) {
135 /* Continue from the next SSID on the next attempt. */
136 wpa_s->prev_scan_ssid = ssid;
137 } else {
138 /* Start from the beginning of the SSID list. */
139 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
140 }
141 wpa_supplicant_associate(wpa_s, NULL, ssid);
142}
143
144
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800145static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800147 struct wpa_supplicant *wpa_s = work->wpa_s;
148 struct wpa_driver_scan_params *params = work->ctx;
149 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800151 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800152 if (!work->started) {
153 wpa_scan_free_params(params);
154 return;
155 }
156 wpa_supplicant_notify_scanning(wpa_s, 0);
157 wpas_notify_scan_done(wpa_s, 0);
158 wpa_s->scan_work = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 return;
160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700162 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
163 wpa_msg(wpa_s, MSG_INFO,
164 "Failed to assign random MAC address for a scan");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700165 wpa_scan_free_params(params);
166 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700167 radio_work_done(work);
168 return;
169 }
170
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800171 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800173 if (wpa_s->clear_driver_scan_cache) {
174 wpa_printf(MSG_DEBUG,
175 "Request driver to clear scan cache due to local BSS flush");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800176 params->only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800177 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800178 ret = wpa_drv_scan(wpa_s, params);
179 wpa_scan_free_params(params);
180 work->ctx = NULL;
181 if (ret) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800182 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
183
184 if (wpa_s->disconnected)
185 retry = 0;
186
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800187 wpa_supplicant_notify_scanning(wpa_s, 0);
188 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800189 if (wpa_s->wpa_state == WPA_SCANNING)
190 wpa_supplicant_set_state(wpa_s,
191 wpa_s->scan_prev_wpa_state);
192 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
193 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800194 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800195
196 if (retry) {
197 /* Restore scan_req since we will try to scan again */
198 wpa_s->scan_req = wpa_s->last_scan_req;
199 wpa_supplicant_req_scan(wpa_s, 1, 0);
200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800203
204 os_get_reltime(&wpa_s->scan_trigger_time);
205 wpa_s->scan_runs++;
206 wpa_s->normal_scans++;
207 wpa_s->own_scan_requested = 1;
208 wpa_s->clear_driver_scan_cache = 0;
209 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210}
211
212
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800213/**
214 * wpa_supplicant_trigger_scan - Request driver to start a scan
215 * @wpa_s: Pointer to wpa_supplicant data
216 * @params: Scan parameters
217 * Returns: 0 on success, -1 on failure
218 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
220 struct wpa_driver_scan_params *params)
221{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800222 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800224 if (wpa_s->scan_work) {
225 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
226 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800227 }
228
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800229 ctx = wpa_scan_clone_params(params);
230 if (ctx == NULL)
231 return -1;
232
233 if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
234 {
235 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700236 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800237 return -1;
238 }
239
240 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241}
242
243
244static void
245wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
246{
247 struct wpa_supplicant *wpa_s = eloop_ctx;
248
249 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
250
251 if (wpa_supplicant_req_sched_scan(wpa_s))
252 wpa_supplicant_req_scan(wpa_s, 0, 0);
253}
254
255
256static void
257wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
258{
259 struct wpa_supplicant *wpa_s = eloop_ctx;
260
261 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
262
263 wpa_s->sched_scan_timed_out = 1;
264 wpa_supplicant_cancel_sched_scan(wpa_s);
265}
266
267
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700268static int
269wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
270 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800271{
272 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700273
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800274 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800275 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800276 if (ret)
277 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800278 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279 wpa_s->sched_scanning = 1;
280
281 return ret;
282}
283
284
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700285static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800286{
287 int ret;
288
289 ret = wpa_drv_stop_sched_scan(wpa_s);
290 if (ret) {
291 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
292 /* TODO: what to do if stopping fails? */
293 return -1;
294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295
296 return ret;
297}
298
299
300static struct wpa_driver_scan_filter *
301wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
302{
303 struct wpa_driver_scan_filter *ssids;
304 struct wpa_ssid *ssid;
305 size_t count;
306
307 *num_ssids = 0;
308 if (!conf->filter_ssids)
309 return NULL;
310
311 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
312 if (ssid->ssid && ssid->ssid_len)
313 count++;
314 }
315 if (count == 0)
316 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800317 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 if (ssids == NULL)
319 return NULL;
320
321 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
322 if (!ssid->ssid || !ssid->ssid_len)
323 continue;
324 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
325 ssids[*num_ssids].ssid_len = ssid->ssid_len;
326 (*num_ssids)++;
327 }
328
329 return ssids;
330}
331
332
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333static void wpa_supplicant_optimize_freqs(
334 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
335{
336#ifdef CONFIG_P2P
337 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
338 wpa_s->go_params) {
339 /* Optimize provisioning state scan based on GO information */
340 if (wpa_s->p2p_in_provisioning < 5 &&
341 wpa_s->go_params->freq > 0) {
342 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
343 "preferred frequency %d MHz",
344 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800345 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800346 if (params->freqs)
347 params->freqs[0] = wpa_s->go_params->freq;
348 } else if (wpa_s->p2p_in_provisioning < 8 &&
349 wpa_s->go_params->freq_list[0]) {
350 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
351 "channels");
352 int_array_concat(&params->freqs,
353 wpa_s->go_params->freq_list);
354 if (params->freqs)
355 int_array_sort_unique(params->freqs);
356 }
357 wpa_s->p2p_in_provisioning++;
358 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700359
360 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
361 /*
362 * Optimize scan based on GO information during persistent
363 * group reinvocation
364 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700365 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700366 wpa_s->p2p_invite_go_freq > 0) {
367 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
368 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800369 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700370 if (params->freqs)
371 params->freqs[0] = wpa_s->p2p_invite_go_freq;
372 }
373 wpa_s->p2p_in_invitation++;
374 if (wpa_s->p2p_in_invitation > 20) {
375 /*
376 * This should not really happen since the variable is
377 * cleared on group removal, but if it does happen, make
378 * sure we do not get stuck in special invitation scan
379 * mode.
380 */
381 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
382 wpa_s->p2p_in_invitation = 0;
383 }
384 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800385#endif /* CONFIG_P2P */
386
387#ifdef CONFIG_WPS
388 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
389 /*
390 * Optimize post-provisioning scan based on channel used
391 * during provisioning.
392 */
393 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
394 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800395 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800396 if (params->freqs)
397 params->freqs[0] = wpa_s->wps_freq;
398 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800399 } else if (wpa_s->after_wps)
400 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800401
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800402 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
403 {
404 /* Optimize provisioning scan based on already known channel */
405 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
406 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800407 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800408 if (params->freqs)
409 params->freqs[0] = wpa_s->wps_freq;
410 wpa_s->known_wps_freq = 0; /* only do this once */
411 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800412#endif /* CONFIG_WPS */
413}
414
415
416#ifdef CONFIG_INTERWORKING
417static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
418 struct wpabuf *buf)
419{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800420 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
421 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
422 1 + ETH_ALEN);
423 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
424 /* No Venue Info */
425 if (!is_zero_ether_addr(wpa_s->conf->hessid))
426 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
427}
428#endif /* CONFIG_INTERWORKING */
429
430
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700431static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800432{
433 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700434 u8 ext_capab[18];
435 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800436#ifdef CONFIG_WPS
437 int wps = 0;
438 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
439#endif /* CONFIG_WPS */
440
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700441 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
442 sizeof(ext_capab));
443 if (ext_capab_len > 0 &&
444 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
445 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447#ifdef CONFIG_INTERWORKING
448 if (wpa_s->conf->interworking &&
449 wpabuf_resize(&extra_ie, 100) == 0)
450 wpas_add_interworking_elements(wpa_s, extra_ie);
451#endif /* CONFIG_INTERWORKING */
452
453#ifdef CONFIG_WPS
454 wps = wpas_wps_in_use(wpa_s, &req_type);
455
456 if (wps) {
457 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700458 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
459 DEV_PW_DEFAULT,
460 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800461 wpa_s->wps->uuid, req_type,
462 0, NULL);
463 if (wps_ie) {
464 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
465 wpabuf_put_buf(extra_ie, wps_ie);
466 wpabuf_free(wps_ie);
467 }
468 }
469
470#ifdef CONFIG_P2P
471 if (wps) {
472 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
473 if (wpabuf_resize(&extra_ie, ielen) == 0)
474 wpas_p2p_scan_ie(wpa_s, extra_ie);
475 }
476#endif /* CONFIG_P2P */
477
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800478 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
479
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800480#endif /* CONFIG_WPS */
481
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700482#ifdef CONFIG_HS20
483 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800484 wpas_hs20_add_indication(extra_ie, -1);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700485#endif /* CONFIG_HS20 */
486
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800487#ifdef CONFIG_FST
488 if (wpa_s->fst_ies &&
489 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
490 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
491#endif /* CONFIG_FST */
492
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800493#ifdef CONFIG_MBO
494 /* Send cellular capabilities for potential MBO STAs */
495 if (wpabuf_resize(&extra_ie, 9) == 0)
496 wpas_mbo_scan_ie(wpa_s, extra_ie);
497#endif /* CONFIG_MBO */
498
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700499 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
500 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
501
502 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
503 wpabuf_put_buf(extra_ie, buf);
504 }
505
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800506 return extra_ie;
507}
508
509
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800510#ifdef CONFIG_P2P
511
512/*
513 * Check whether there are any enabled networks or credentials that could be
514 * used for a non-P2P connection.
515 */
516static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
517{
518 struct wpa_ssid *ssid;
519
520 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
521 if (wpas_network_disabled(wpa_s, ssid))
522 continue;
523 if (!ssid->p2p_group)
524 return 1;
525 }
526
527 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
528 wpa_s->conf->auto_interworking)
529 return 1;
530
531 return 0;
532}
533
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700534#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800535
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800536
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700537static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
538 enum hostapd_hw_mode band,
539 struct wpa_driver_scan_params *params)
540{
541 /* Include only supported channels for the specified band */
542 struct hostapd_hw_modes *mode;
543 int count, i;
544
545 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
546 if (mode == NULL) {
547 /* No channels supported in this band - use empty list */
548 params->freqs = os_zalloc(sizeof(int));
549 return;
550 }
551
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800552 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700553 if (params->freqs == NULL)
554 return;
555 for (count = 0, i = 0; i < mode->num_channels; i++) {
556 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
557 continue;
558 params->freqs[count++] = mode->channels[i].freq;
559 }
560}
561
562
563static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
564 struct wpa_driver_scan_params *params)
565{
566 if (wpa_s->hw.modes == NULL)
567 return; /* unknown what channels the driver supports */
568 if (params->freqs)
569 return; /* already using a limited channel set */
570 if (wpa_s->setband == WPA_SETBAND_5G)
571 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
572 params);
573 else if (wpa_s->setband == WPA_SETBAND_2G)
574 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
575 params);
576}
577
578
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700579static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
580 struct wpa_driver_scan_params *params,
581 size_t max_ssids)
582{
583 unsigned int i;
584 struct wpa_ssid *ssid;
Roshan Piuse382b882016-06-29 15:37:57 -0700585 /*
586 * For devices with |max_ssids| greater than 1, leave the last slot empty
587 * for adding the wildcard scan entry.
588 */
589 max_ssids = (max_ssids == 1) ? max_ssids : max_ssids - 1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700590
591 for (i = 0; i < wpa_s->scan_id_count; i++) {
592 unsigned int j;
593
594 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
595 if (!ssid || !ssid->scan_ssid)
596 continue;
597
598 for (j = 0; j < params->num_ssids; j++) {
599 if (params->ssids[j].ssid_len == ssid->ssid_len &&
600 params->ssids[j].ssid &&
601 os_memcmp(params->ssids[j].ssid, ssid->ssid,
602 ssid->ssid_len) == 0)
603 break;
604 }
605 if (j < params->num_ssids)
606 continue; /* already in the list */
607
608 if (params->num_ssids + 1 > max_ssids) {
609 wpa_printf(MSG_DEBUG,
610 "Over max scan SSIDs for manual request");
611 break;
612 }
613
614 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
615 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
616 params->ssids[params->num_ssids].ssid = ssid->ssid;
617 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
618 params->num_ssids++;
619 }
620
621 wpa_s->scan_id_count = 0;
622}
623
624
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700625static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
626 struct wpa_driver_scan_params *params,
627 size_t max_ssids)
628{
629 unsigned int i;
630
631 if (wpa_s->ssids_from_scan_req == NULL ||
632 wpa_s->num_ssids_from_scan_req == 0)
633 return 0;
634
635 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
636 wpa_s->num_ssids_from_scan_req = max_ssids;
637 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
638 (unsigned int) max_ssids);
639 }
640
641 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
642 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
643 params->ssids[i].ssid_len =
644 wpa_s->ssids_from_scan_req[i].ssid_len;
645 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
646 params->ssids[i].ssid,
647 params->ssids[i].ssid_len);
648 }
649
650 params->num_ssids = wpa_s->num_ssids_from_scan_req;
651 wpa_s->num_ssids_from_scan_req = 0;
652 return 1;
653}
654
655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
657{
658 struct wpa_supplicant *wpa_s = eloop_ctx;
659 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800660 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700661 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700663 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800665 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666
Dmitry Shmidt18463232014-01-24 12:29:41 -0800667 if (wpa_s->pno || wpa_s->pno_sched_pending) {
668 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
669 return;
670 }
671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
673 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
674 return;
675 }
676
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800677 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700678 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
680 return;
681 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800682
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700683 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800684 /*
685 * If we are already in scanning state, we shall reschedule the
686 * the incoming scan request.
687 */
688 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
689 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700690 return;
691 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800692
Dmitry Shmidt04949592012-07-19 12:16:46 -0700693 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800694 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
696 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
697 return;
698 }
699
700 if (wpa_s->conf->ap_scan != 0 &&
701 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
702 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
703 "overriding ap_scan configuration");
704 wpa_s->conf->ap_scan = 0;
705 wpas_notify_ap_scan_changed(wpa_s);
706 }
707
708 if (wpa_s->conf->ap_scan == 0) {
709 wpa_supplicant_gen_assoc_event(wpa_s);
710 return;
711 }
712
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800713 ssid = NULL;
714 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
715 wpa_s->connect_without_scan) {
716 connect_without_scan = 1;
717 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
718 if (ssid == wpa_s->connect_without_scan)
719 break;
720 }
721 }
722
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800723 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800724 if (p2p_in_prog && p2p_in_prog != 2 &&
725 (!ssid ||
726 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800727 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
728 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700729 return;
730 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 max_ssids = 1;
734 else {
735 max_ssids = wpa_s->max_scan_ssids;
736 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
737 max_ssids = WPAS_MAX_SCAN_SSIDS;
738 }
739
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800740 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800741 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800743 if (connect_without_scan) {
744 wpa_s->connect_without_scan = NULL;
745 if (ssid) {
746 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
747 "without scan step");
748 wpa_supplicant_associate(wpa_s, NULL, ssid);
749 return;
750 }
751 }
752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 os_memset(&params, 0, sizeof(params));
754
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800755 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
757 wpa_s->wpa_state == WPA_INACTIVE)
758 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
759
Dmitry Shmidt04949592012-07-19 12:16:46 -0700760 /*
761 * If autoscan has set its own scanning parameters
762 */
763 if (wpa_s->autoscan_params != NULL) {
764 scan_params = wpa_s->autoscan_params;
765 goto scan;
766 }
767
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700768 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
769 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
770 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
771 goto ssid_list_set;
772 }
773
Dmitry Shmidt04949592012-07-19 12:16:46 -0700774#ifdef CONFIG_P2P
775 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800776 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800777 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
778 wpa_s->p2p_in_provisioning,
779 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700780 params.ssids[0].ssid = wpa_s->go_params->ssid;
781 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
782 params.num_ssids = 1;
783 goto ssid_list_set;
784 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700785
786 if (wpa_s->p2p_in_invitation) {
787 if (wpa_s->current_ssid) {
788 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
789 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
790 params.ssids[0].ssid_len =
791 wpa_s->current_ssid->ssid_len;
792 params.num_ssids = 1;
793 } else {
794 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
795 }
796 goto ssid_list_set;
797 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700798#endif /* CONFIG_P2P */
799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 /* Find the starting point from which to continue scanning */
801 ssid = wpa_s->conf->ssid;
802 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
803 while (ssid) {
804 if (ssid == wpa_s->prev_scan_ssid) {
805 ssid = ssid->next;
806 break;
807 }
808 ssid = ssid->next;
809 }
810 }
811
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800812 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800813#ifdef CONFIG_AP
814 !wpa_s->ap_iface &&
815#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800816 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700817 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800818 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 wpa_supplicant_assoc_try(wpa_s, ssid);
820 return;
821 } else if (wpa_s->conf->ap_scan == 2) {
822 /*
823 * User-initiated scan request in ap_scan == 2; scan with
824 * wildcard SSID.
825 */
826 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700827 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
828 /*
829 * Perform single-channel single-SSID scan for
830 * reassociate-to-same-BSS operation.
831 */
832 /* Setup SSID */
833 ssid = wpa_s->current_ssid;
834 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
835 ssid->ssid, ssid->ssid_len);
836 params.ssids[0].ssid = ssid->ssid;
837 params.ssids[0].ssid_len = ssid->ssid_len;
838 params.num_ssids = 1;
839
840 /*
841 * Allocate memory for frequency array, allocate one extra
842 * slot for the zero-terminator.
843 */
844 params.freqs = os_malloc(sizeof(int) * 2);
845 if (params.freqs == NULL) {
846 wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
847 return;
848 }
849 params.freqs[0] = wpa_s->assoc_freq;
850 params.freqs[1] = 0;
851
852 /*
853 * Reset the reattach flag so that we fall back to full scan if
854 * this scan fails.
855 */
856 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 } else {
858 struct wpa_ssid *start = ssid, *tssid;
859 int freqs_set = 0;
860 if (ssid == NULL && max_ssids > 1)
861 ssid = wpa_s->conf->ssid;
862 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700863 if (!wpas_network_disabled(wpa_s, ssid) &&
864 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
866 ssid->ssid, ssid->ssid_len);
867 params.ssids[params.num_ssids].ssid =
868 ssid->ssid;
869 params.ssids[params.num_ssids].ssid_len =
870 ssid->ssid_len;
871 params.num_ssids++;
872 if (params.num_ssids + 1 >= max_ssids)
873 break;
874 }
875 ssid = ssid->next;
876 if (ssid == start)
877 break;
878 if (ssid == NULL && max_ssids > 1 &&
879 start != wpa_s->conf->ssid)
880 ssid = wpa_s->conf->ssid;
881 }
882
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700883 if (wpa_s->scan_id_count &&
884 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
885 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
886
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800887 for (tssid = wpa_s->conf->ssid;
888 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
889 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700890 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 continue;
892 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
893 int_array_concat(&params.freqs,
894 tssid->scan_freq);
895 } else {
896 os_free(params.freqs);
897 params.freqs = NULL;
898 }
899 freqs_set = 1;
900 }
901 int_array_sort_unique(params.freqs);
902 }
903
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800904 if (ssid && max_ssids == 1) {
905 /*
906 * If the driver is limited to 1 SSID at a time interleave
907 * wildcard SSID scans with specific SSID scans to avoid
908 * waiting a long time for a wildcard scan.
909 */
910 if (!wpa_s->prev_scan_wildcard) {
911 params.ssids[0].ssid = NULL;
912 params.ssids[0].ssid_len = 0;
913 wpa_s->prev_scan_wildcard = 1;
914 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
915 "wildcard SSID (Interleave with specific)");
916 } else {
917 wpa_s->prev_scan_ssid = ssid;
918 wpa_s->prev_scan_wildcard = 0;
919 wpa_dbg(wpa_s, MSG_DEBUG,
920 "Starting AP scan for specific SSID: %s",
921 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800923 } else if (ssid) {
924 /* max_ssids > 1 */
925
926 wpa_s->prev_scan_ssid = ssid;
927 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
928 "the scan request");
929 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800930 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
931 wpa_s->manual_scan_passive && params.num_ssids == 0) {
932 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800933 } else if (wpa_s->conf->passive_scan) {
934 wpa_dbg(wpa_s, MSG_DEBUG,
935 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 } else {
937 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
938 params.num_ssids++;
939 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
940 "SSID");
941 }
942
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700943ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800944 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700945 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800947 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800948 wpa_s->manual_scan_only_new) {
949 wpa_printf(MSG_DEBUG,
950 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800951 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800952 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800953
954 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
955 wpa_s->manual_scan_freqs) {
956 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
957 params.freqs = wpa_s->manual_scan_freqs;
958 wpa_s->manual_scan_freqs = NULL;
959 }
960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
962 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
963 "generated frequency list");
964 params.freqs = wpa_s->next_scan_freqs;
965 } else
966 os_free(wpa_s->next_scan_freqs);
967 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700968 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700970 /* See if user specified frequencies. If so, scan only those. */
971 if (wpa_s->conf->freq_list && !params.freqs) {
972 wpa_dbg(wpa_s, MSG_DEBUG,
973 "Optimize scan based on conf->freq_list");
974 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
975 }
976
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700977 /* Use current associated channel? */
978 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700979 unsigned int num = wpa_s->num_multichan_concurrent;
980
981 params.freqs = os_calloc(num + 1, sizeof(int));
982 if (params.freqs) {
983 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
984 if (num > 0) {
985 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
986 "current operating channels since "
987 "scan_cur_freq is enabled");
988 } else {
989 os_free(params.freqs);
990 params.freqs = NULL;
991 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700992 }
993 }
994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 params.filter_ssids = wpa_supplicant_build_filter_ssids(
996 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800997 if (extra_ie) {
998 params.extra_ies = wpabuf_head(extra_ie);
999 params.extra_ies_len = wpabuf_len(extra_ie);
1000 }
1001
1002#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001003 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001004 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001005 /*
1006 * The interface may not yet be in P2P mode, so we have to
1007 * explicitly request P2P probe to disable CCK rates.
1008 */
1009 params.p2p_probe = 1;
1010 }
1011#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001013 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
1014 params.mac_addr_rand = 1;
1015 if (wpa_s->mac_addr_scan) {
1016 params.mac_addr = wpa_s->mac_addr_scan;
1017 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1018 }
1019 }
1020
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001021 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1022 struct wpa_bss *bss;
1023
1024 params.bssid = wpa_s->next_scan_bssid;
1025 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1026 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1027 params.ssids[0].ssid_len == 0) {
1028 params.ssids[0].ssid = bss->ssid;
1029 params.ssids[0].ssid_len = bss->ssid_len;
1030 wpa_dbg(wpa_s, MSG_DEBUG,
1031 "Scan a previously specified BSSID " MACSTR
1032 " and SSID %s",
1033 MAC2STR(params.bssid),
1034 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1035 } else {
1036 wpa_dbg(wpa_s, MSG_DEBUG,
1037 "Scan a previously specified BSSID " MACSTR,
1038 MAC2STR(params.bssid));
1039 }
1040 }
1041
Dmitry Shmidt04949592012-07-19 12:16:46 -07001042 scan_params = &params;
1043
1044scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001045#ifdef CONFIG_P2P
1046 /*
1047 * If the driver does not support multi-channel concurrency and a
1048 * virtual interface that shares the same radio with the wpa_s interface
1049 * is operating there may not be need to scan other channels apart from
1050 * the current operating channel on the other virtual interface. Filter
1051 * out other channels in case we are trying to find a connection for a
1052 * station interface when we are not configured to prefer station
1053 * connection and a concurrent operation is already in process.
1054 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001055 if (wpa_s->scan_for_connection &&
1056 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001057 !scan_params->freqs && !params.freqs &&
1058 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001059 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1060 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001061 unsigned int num = wpa_s->num_multichan_concurrent;
1062
1063 params.freqs = os_calloc(num + 1, sizeof(int));
1064 if (params.freqs) {
1065 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1066 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1067 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1068 } else {
1069 os_free(params.freqs);
1070 params.freqs = NULL;
1071 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001072 }
1073 }
1074#endif /* CONFIG_P2P */
1075
Dmitry Shmidt04949592012-07-19 12:16:46 -07001076 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001078 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1079 !wpa_s->manual_scan_freqs) {
1080 /* Restore manual_scan_freqs for the next attempt */
1081 wpa_s->manual_scan_freqs = params.freqs;
1082 params.freqs = NULL;
1083 }
1084
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001085 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 os_free(params.freqs);
1087 os_free(params.filter_ssids);
1088
1089 if (ret) {
1090 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001091 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1092 wpa_supplicant_set_state(wpa_s,
1093 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001094 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001095 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001097 } else {
1098 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001099#ifdef CONFIG_INTERWORKING
1100 wpa_s->interworking_fast_assoc_tried = 0;
1101#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001102 if (params.bssid)
1103 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 }
1105}
1106
1107
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001108void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1109{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001110 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001111 int cancelled;
1112
1113 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1114 &remaining);
1115
1116 new_int.sec = sec;
1117 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001118 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001119 new_int.sec = remaining.sec;
1120 new_int.usec = remaining.usec;
1121 }
1122
Dmitry Shmidt051af732013-10-22 13:52:46 -07001123 if (cancelled) {
1124 eloop_register_timeout(new_int.sec, new_int.usec,
1125 wpa_supplicant_scan, wpa_s, NULL);
1126 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001127 wpa_s->scan_interval = sec;
1128}
1129
1130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131/**
1132 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1133 * @wpa_s: Pointer to wpa_supplicant data
1134 * @sec: Number of seconds after which to scan
1135 * @usec: Number of microseconds after which to scan
1136 *
1137 * This function is used to schedule a scan for neighboring access points after
1138 * the specified time.
1139 */
1140void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1141{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001142 int res;
1143
1144 if (wpa_s->p2p_mgmt) {
1145 wpa_dbg(wpa_s, MSG_DEBUG,
1146 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1147 sec, usec);
1148 return;
1149 }
1150
1151 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1152 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001153 if (res == 1) {
1154 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001155 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001156 } else if (res == 0) {
1157 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1158 sec, usec);
1159 } else {
1160 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1161 sec, usec);
1162 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164}
1165
1166
1167/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001168 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1169 * @wpa_s: Pointer to wpa_supplicant data
1170 * @sec: Number of seconds after which to scan
1171 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001172 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001173 *
1174 * This function is used to schedule periodic scans for neighboring
1175 * access points after the specified time.
1176 */
1177int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1178 int sec, int usec)
1179{
1180 if (!wpa_s->sched_scan_supported)
1181 return -1;
1182
1183 eloop_register_timeout(sec, usec,
1184 wpa_supplicant_delayed_sched_scan_timeout,
1185 wpa_s, NULL);
1186
1187 return 0;
1188}
1189
1190
1191/**
1192 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1193 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001194 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001195 *
1196 * This function is used to schedule periodic scans for neighboring
1197 * access points repeating the scan continuously.
1198 */
1199int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1200{
1201 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001202 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001203 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001204 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001205 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001206 int ret;
1207 unsigned int max_sched_scan_ssids;
1208 int wildcard = 0;
1209 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001210 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001211
1212 if (!wpa_s->sched_scan_supported)
1213 return -1;
1214
1215 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1216 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1217 else
1218 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001219 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001220 return -1;
1221
1222 if (wpa_s->sched_scanning) {
1223 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1224 return 0;
1225 }
1226
1227 need_ssids = 0;
1228 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001229 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001230 /* Use wildcard SSID to find this network */
1231 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001232 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1233 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001234 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001235
1236#ifdef CONFIG_WPS
1237 if (!wpas_network_disabled(wpa_s, ssid) &&
1238 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1239 /*
1240 * Normal scan is more reliable and faster for WPS
1241 * operations and since these are for short periods of
1242 * time, the benefit of trying to use sched_scan would
1243 * be limited.
1244 */
1245 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1246 "sched_scan for WPS");
1247 return -1;
1248 }
1249#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 }
1251 if (wildcard)
1252 need_ssids++;
1253
1254 if (wpa_s->normal_scans < 3 &&
1255 (need_ssids <= wpa_s->max_scan_ssids ||
1256 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1257 /*
1258 * When normal scan can speed up operations, use that for the
1259 * first operations before starting the sched_scan to allow
1260 * user space sleep more. We do this only if the normal scan
1261 * has functionality that is suitable for this or if the
1262 * sched_scan does not have better support for multiple SSIDs.
1263 */
1264 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1265 "sched_scan for initial scans (normal_scans=%d)",
1266 wpa_s->normal_scans);
1267 return -1;
1268 }
1269
1270 os_memset(&params, 0, sizeof(params));
1271
1272 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001273 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001274 sizeof(struct wpa_driver_scan_filter));
1275
1276 prev_state = wpa_s->wpa_state;
1277 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1278 wpa_s->wpa_state == WPA_INACTIVE)
1279 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1280
Dmitry Shmidt04949592012-07-19 12:16:46 -07001281 if (wpa_s->autoscan_params != NULL) {
1282 scan_params = wpa_s->autoscan_params;
1283 goto scan;
1284 }
1285
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001286 /* Find the starting point from which to continue scanning */
1287 ssid = wpa_s->conf->ssid;
1288 if (wpa_s->prev_sched_ssid) {
1289 while (ssid) {
1290 if (ssid == wpa_s->prev_sched_ssid) {
1291 ssid = ssid->next;
1292 break;
1293 }
1294 ssid = ssid->next;
1295 }
1296 }
1297
1298 if (!ssid || !wpa_s->prev_sched_ssid) {
1299 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001300 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1301 wpa_s->first_sched_scan = 1;
1302 ssid = wpa_s->conf->ssid;
1303 wpa_s->prev_sched_ssid = ssid;
1304 }
1305
1306 if (wildcard) {
1307 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1308 params.num_ssids++;
1309 }
1310
1311 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001312 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001313 goto next;
1314
1315 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1316 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1317 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1318 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1319 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1320 ssid->ssid, ssid->ssid_len);
1321 params.filter_ssids[params.num_filter_ssids].ssid_len =
1322 ssid->ssid_len;
1323 params.num_filter_ssids++;
1324 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1325 {
1326 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1327 "filter for sched_scan - drop filter");
1328 os_free(params.filter_ssids);
1329 params.filter_ssids = NULL;
1330 params.num_filter_ssids = 0;
1331 }
1332
1333 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1334 if (params.num_ssids == max_sched_scan_ssids)
1335 break; /* only room for broadcast SSID */
1336 wpa_dbg(wpa_s, MSG_DEBUG,
1337 "add to active scan ssid: %s",
1338 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1339 params.ssids[params.num_ssids].ssid =
1340 ssid->ssid;
1341 params.ssids[params.num_ssids].ssid_len =
1342 ssid->ssid_len;
1343 params.num_ssids++;
1344 if (params.num_ssids >= max_sched_scan_ssids) {
1345 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001346 do {
1347 ssid = ssid->next;
1348 } while (ssid &&
1349 (wpas_network_disabled(wpa_s, ssid) ||
1350 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001351 break;
1352 }
1353 }
1354
1355 next:
1356 wpa_s->prev_sched_ssid = ssid;
1357 ssid = ssid->next;
1358 }
1359
1360 if (params.num_filter_ssids == 0) {
1361 os_free(params.filter_ssids);
1362 params.filter_ssids = NULL;
1363 }
1364
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001365 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1366 if (extra_ie) {
1367 params.extra_ies = wpabuf_head(extra_ie);
1368 params.extra_ies_len = wpabuf_len(extra_ie);
1369 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001370
Dmitry Shmidt18463232014-01-24 12:29:41 -08001371 if (wpa_s->conf->filter_rssi)
1372 params.filter_rssi = wpa_s->conf->filter_rssi;
1373
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001374 /* See if user specified frequencies. If so, scan only those. */
1375 if (wpa_s->conf->freq_list && !params.freqs) {
1376 wpa_dbg(wpa_s, MSG_DEBUG,
1377 "Optimize scan based on conf->freq_list");
1378 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1379 }
1380
Dmitry Shmidt04949592012-07-19 12:16:46 -07001381 scan_params = &params;
1382
1383scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001384 wpa_s->sched_scan_timed_out = 0;
1385
1386 /*
1387 * We cannot support multiple scan plans if the scan request includes
1388 * too many SSID's, so in this case use only the last scan plan and make
1389 * it run infinitely. It will be stopped by the timeout.
1390 */
1391 if (wpa_s->sched_scan_plans_num == 1 ||
1392 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1393 params.sched_scan_plans = wpa_s->sched_scan_plans;
1394 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1395 } else if (wpa_s->sched_scan_plans_num > 1) {
1396 wpa_dbg(wpa_s, MSG_DEBUG,
1397 "Too many SSIDs. Default to using single scheduled_scan plan");
1398 params.sched_scan_plans =
1399 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1400 1];
1401 params.sched_scan_plans_num = 1;
1402 } else {
1403 if (wpa_s->conf->sched_scan_interval)
1404 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1405 else
1406 scan_plan.interval = 10;
1407
1408 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1409 wpa_printf(MSG_WARNING,
1410 "Scan interval too long(%u), use the maximum allowed(%u)",
1411 scan_plan.interval,
1412 wpa_s->max_sched_scan_plan_interval);
1413 scan_plan.interval =
1414 wpa_s->max_sched_scan_plan_interval;
1415 }
1416
1417 scan_plan.iterations = 0;
1418 params.sched_scan_plans = &scan_plan;
1419 params.sched_scan_plans_num = 1;
1420 }
1421
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001422 if (ssid || !wpa_s->first_sched_scan) {
1423 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001424 "Starting sched scan: interval %u timeout %d",
1425 params.sched_scan_plans[0].interval,
1426 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001427 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001428 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001429 }
1430
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001431 wpa_setband_scan_freqs(wpa_s, scan_params);
1432
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001433 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1434 params.mac_addr_rand = 1;
1435 if (wpa_s->mac_addr_sched_scan) {
1436 params.mac_addr = wpa_s->mac_addr_sched_scan;
1437 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1438 ETH_ALEN;
1439 }
1440 }
1441
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001442 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001443 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001444 os_free(params.filter_ssids);
1445 if (ret) {
1446 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1447 if (prev_state != wpa_s->wpa_state)
1448 wpa_supplicant_set_state(wpa_s, prev_state);
1449 return ret;
1450 }
1451
1452 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1453 if (ssid || !wpa_s->first_sched_scan) {
1454 wpa_s->sched_scan_timed_out = 0;
1455 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1456 wpa_supplicant_sched_scan_timeout,
1457 wpa_s, NULL);
1458 wpa_s->first_sched_scan = 0;
1459 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001460 params.sched_scan_plans[0].interval *= 2;
1461 if ((unsigned int) wpa_s->sched_scan_timeout <
1462 params.sched_scan_plans[0].interval ||
1463 params.sched_scan_plans[0].interval >
1464 wpa_s->max_sched_scan_plan_interval) {
1465 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001466 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1467 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001468 }
1469
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001470 /* If there is no more ssids, start next time from the beginning */
1471 if (!ssid)
1472 wpa_s->prev_sched_ssid = NULL;
1473
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001474 return 0;
1475}
1476
1477
1478/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1480 * @wpa_s: Pointer to wpa_supplicant data
1481 *
1482 * This function is used to cancel a scan request scheduled with
1483 * wpa_supplicant_req_scan().
1484 */
1485void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1486{
1487 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1488 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489}
1490
1491
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001493 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1494 * @wpa_s: Pointer to wpa_supplicant data
1495 *
1496 * This function is used to stop a delayed scheduled scan.
1497 */
1498void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1499{
1500 if (!wpa_s->sched_scan_supported)
1501 return;
1502
1503 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1504 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1505 wpa_s, NULL);
1506}
1507
1508
1509/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001510 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1511 * @wpa_s: Pointer to wpa_supplicant data
1512 *
1513 * This function is used to stop a periodic scheduled scan.
1514 */
1515void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1516{
1517 if (!wpa_s->sched_scanning)
1518 return;
1519
1520 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1521 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1522 wpa_supplicant_stop_sched_scan(wpa_s);
1523}
1524
1525
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001526/**
1527 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1528 * @wpa_s: Pointer to wpa_supplicant data
1529 * @scanning: Whether scanning is currently in progress
1530 *
1531 * This function is to generate scanning notifycations. It is called whenever
1532 * there may have been a change in scanning (scan started, completed, stopped).
1533 * wpas_notify_scanning() is called whenever the scanning state changed from the
1534 * previously notified state.
1535 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1537 int scanning)
1538{
1539 if (wpa_s->scanning != scanning) {
1540 wpa_s->scanning = scanning;
1541 wpas_notify_scanning(wpa_s);
1542 }
1543}
1544
1545
1546static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1547{
1548 int rate = 0;
1549 const u8 *ie;
1550 int i;
1551
1552 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1553 for (i = 0; ie && i < ie[1]; i++) {
1554 if ((ie[i + 2] & 0x7f) > rate)
1555 rate = ie[i + 2] & 0x7f;
1556 }
1557
1558 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1559 for (i = 0; ie && i < ie[1]; i++) {
1560 if ((ie[i + 2] & 0x7f) > rate)
1561 rate = ie[i + 2] & 0x7f;
1562 }
1563
1564 return rate;
1565}
1566
1567
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001568/**
1569 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1570 * @res: Scan result entry
1571 * @ie: Information element identitifier (WLAN_EID_*)
1572 * Returns: Pointer to the information element (id field) or %NULL if not found
1573 *
1574 * This function returns the first matching information element in the scan
1575 * result.
1576 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1578{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001579 return get_ie((const u8 *) (res + 1), res->ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580}
1581
1582
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001583/**
1584 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1585 * @res: Scan result entry
1586 * @vendor_type: Vendor type (four octets starting the IE payload)
1587 * Returns: Pointer to the information element (id field) or %NULL if not found
1588 *
1589 * This function returns the first matching information element in the scan
1590 * result.
1591 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1593 u32 vendor_type)
1594{
1595 const u8 *end, *pos;
1596
1597 pos = (const u8 *) (res + 1);
1598 end = pos + res->ie_len;
1599
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001600 while (end - pos > 1) {
1601 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 break;
1603 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1604 vendor_type == WPA_GET_BE32(&pos[2]))
1605 return pos;
1606 pos += 2 + pos[1];
1607 }
1608
1609 return NULL;
1610}
1611
1612
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001613/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001614 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1615 * @res: Scan result entry
1616 * @vendor_type: Vendor type (four octets starting the IE payload)
1617 * Returns: Pointer to the information element (id field) or %NULL if not found
1618 *
1619 * This function returns the first matching information element in the scan
1620 * result.
1621 *
1622 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1623 * from Beacon frames instead of either Beacon or Probe Response frames.
1624 */
1625const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1626 u32 vendor_type)
1627{
1628 const u8 *end, *pos;
1629
1630 if (res->beacon_ie_len == 0)
1631 return NULL;
1632
1633 pos = (const u8 *) (res + 1);
1634 pos += res->ie_len;
1635 end = pos + res->beacon_ie_len;
1636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001637 while (end - pos > 1) {
1638 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001639 break;
1640 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1641 vendor_type == WPA_GET_BE32(&pos[2]))
1642 return pos;
1643 pos += 2 + pos[1];
1644 }
1645
1646 return NULL;
1647}
1648
1649
1650/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001651 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1652 * @res: Scan result entry
1653 * @vendor_type: Vendor type (four octets starting the IE payload)
1654 * Returns: Pointer to the information element payload or %NULL if not found
1655 *
1656 * This function returns concatenated payload of possibly fragmented vendor
1657 * specific information elements in the scan result. The caller is responsible
1658 * for freeing the returned buffer.
1659 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1661 u32 vendor_type)
1662{
1663 struct wpabuf *buf;
1664 const u8 *end, *pos;
1665
1666 buf = wpabuf_alloc(res->ie_len);
1667 if (buf == NULL)
1668 return NULL;
1669
1670 pos = (const u8 *) (res + 1);
1671 end = pos + res->ie_len;
1672
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001673 while (end - pos > 1) {
1674 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 break;
1676 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1677 vendor_type == WPA_GET_BE32(&pos[2]))
1678 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1679 pos += 2 + pos[1];
1680 }
1681
1682 if (wpabuf_len(buf) == 0) {
1683 wpabuf_free(buf);
1684 buf = NULL;
1685 }
1686
1687 return buf;
1688}
1689
1690
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001691/*
1692 * Channels with a great SNR can operate at full rate. What is a great SNR?
1693 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1694 * rule of thumb is that any SNR above 20 is good." This one
1695 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1696 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1697 * conservative value.
1698 */
1699#define GREAT_SNR 30
1700
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001701#define IS_5GHZ(n) (n > 4000)
1702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703/* Compare function for sorting scan results. Return >0 if @b is considered
1704 * better. */
1705static int wpa_scan_result_compar(const void *a, const void *b)
1706{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001707#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 struct wpa_scan_res **_wa = (void *) a;
1709 struct wpa_scan_res **_wb = (void *) b;
1710 struct wpa_scan_res *wa = *_wa;
1711 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001712 int wpa_a, wpa_b;
1713 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714
1715 /* WPA/WPA2 support preferred */
1716 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1717 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1718 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1719 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1720
1721 if (wpa_b && !wpa_a)
1722 return 1;
1723 if (!wpa_b && wpa_a)
1724 return -1;
1725
1726 /* privacy support preferred */
1727 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1728 (wb->caps & IEEE80211_CAP_PRIVACY))
1729 return 1;
1730 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1731 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1732 return -1;
1733
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001734 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001735 snr_a_full = wa->snr;
1736 snr_a = MIN(wa->snr, GREAT_SNR);
1737 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001738 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001739 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001740 /* Level is not in dBm, so we can't calculate
1741 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001742 snr_a = snr_a_full = wa->level;
1743 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001744 }
1745
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001746 /* if SNR is close, decide by max rate or frequency band */
1747 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001749 if (wa->est_throughput != wb->est_throughput)
1750 return wb->est_throughput - wa->est_throughput;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001751 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1752 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 }
1754
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001755 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 * identical, use quality values since some drivers may only report
1757 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001758 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001760 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001761#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762}
1763
1764
1765#ifdef CONFIG_WPS
1766/* Compare function for sorting scan results when searching a WPS AP for
1767 * provisioning. Return >0 if @b is considered better. */
1768static int wpa_scan_result_wps_compar(const void *a, const void *b)
1769{
1770 struct wpa_scan_res **_wa = (void *) a;
1771 struct wpa_scan_res **_wb = (void *) b;
1772 struct wpa_scan_res *wa = *_wa;
1773 struct wpa_scan_res *wb = *_wb;
1774 int uses_wps_a, uses_wps_b;
1775 struct wpabuf *wps_a, *wps_b;
1776 int res;
1777
1778 /* Optimization - check WPS IE existence before allocated memory and
1779 * doing full reassembly. */
1780 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1781 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1782 if (uses_wps_a && !uses_wps_b)
1783 return -1;
1784 if (!uses_wps_a && uses_wps_b)
1785 return 1;
1786
1787 if (uses_wps_a && uses_wps_b) {
1788 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1789 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1790 res = wps_ap_priority_compar(wps_a, wps_b);
1791 wpabuf_free(wps_a);
1792 wpabuf_free(wps_b);
1793 if (res)
1794 return res;
1795 }
1796
1797 /*
1798 * Do not use current AP security policy as a sorting criteria during
1799 * WPS provisioning step since the AP may get reconfigured at the
1800 * completion of provisioning.
1801 */
1802
1803 /* all things being equal, use signal level; if signal levels are
1804 * identical, use quality values since some drivers may only report
1805 * that value and leave the signal level zero */
1806 if (wb->level == wa->level)
1807 return wb->qual - wa->qual;
1808 return wb->level - wa->level;
1809}
1810#endif /* CONFIG_WPS */
1811
1812
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001813static void dump_scan_res(struct wpa_scan_results *scan_res)
1814{
1815#ifndef CONFIG_NO_STDOUT_DEBUG
1816 size_t i;
1817
1818 if (scan_res->res == NULL || scan_res->num == 0)
1819 return;
1820
1821 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1822
1823 for (i = 0; i < scan_res->num; i++) {
1824 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001825 u8 *pos;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001826 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001827 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1828
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001829 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001830 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001831 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001832 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001833 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1834 r->flags,
1835 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001836 } else {
1837 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001838 "noise=%d level=%d flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001840 r->noise, r->level, r->flags, r->age,
1841 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001842 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001843 pos = (u8 *) (r + 1);
1844 if (r->ie_len)
1845 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1846 pos += r->ie_len;
1847 if (r->beacon_ie_len)
1848 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1849 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 }
1851#endif /* CONFIG_NO_STDOUT_DEBUG */
1852}
1853
1854
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001855/**
1856 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1857 * @wpa_s: Pointer to wpa_supplicant data
1858 * @bssid: BSSID to check
1859 * Returns: 0 if the BSSID is filtered or 1 if not
1860 *
1861 * This function is used to filter out specific BSSIDs from scan reslts mainly
1862 * for testing purposes (SET bssid_filter ctrl_iface command).
1863 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001864int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1865 const u8 *bssid)
1866{
1867 size_t i;
1868
1869 if (wpa_s->bssid_filter == NULL)
1870 return 1;
1871
1872 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1873 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1874 ETH_ALEN) == 0)
1875 return 1;
1876 }
1877
1878 return 0;
1879}
1880
1881
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001882void filter_scan_res(struct wpa_supplicant *wpa_s,
1883 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001884{
1885 size_t i, j;
1886
1887 if (wpa_s->bssid_filter == NULL)
1888 return;
1889
1890 for (i = 0, j = 0; i < res->num; i++) {
1891 if (wpa_supplicant_filter_bssid_match(wpa_s,
1892 res->res[i]->bssid)) {
1893 res->res[j++] = res->res[i];
1894 } else {
1895 os_free(res->res[i]);
1896 res->res[i] = NULL;
1897 }
1898 }
1899
1900 if (res->num != j) {
1901 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1902 (int) (res->num - j));
1903 res->num = j;
1904 }
1905}
1906
1907
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001908/*
1909 * Noise floor values to use when we have signal strength
Dmitry Shmidte4663042016-04-04 10:07:49 -07001910 * measurements, but no noise floor measurements. These values were
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001911 * measured in an office environment with many APs.
1912 */
1913#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1914#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1915
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001916void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001917{
1918 if (res->flags & WPA_SCAN_NOISE_INVALID) {
1919 res->noise = IS_5GHZ(res->freq) ?
1920 DEFAULT_NOISE_FLOOR_5GHZ :
1921 DEFAULT_NOISE_FLOOR_2GHZ;
1922 }
1923
1924 if (res->flags & WPA_SCAN_LEVEL_DBM) {
1925 res->snr = res->level - res->noise;
1926 } else {
1927 /* Level is not in dBm, so we can't calculate
1928 * SNR. Just use raw level (units unknown). */
1929 res->snr = res->level;
1930 }
1931}
1932
1933
1934static unsigned int max_ht20_rate(int snr)
1935{
1936 if (snr < 6)
1937 return 6500; /* HT20 MCS0 */
1938 if (snr < 8)
1939 return 13000; /* HT20 MCS1 */
1940 if (snr < 13)
1941 return 19500; /* HT20 MCS2 */
1942 if (snr < 17)
1943 return 26000; /* HT20 MCS3 */
1944 if (snr < 20)
1945 return 39000; /* HT20 MCS4 */
1946 if (snr < 23)
1947 return 52000; /* HT20 MCS5 */
1948 if (snr < 24)
1949 return 58500; /* HT20 MCS6 */
1950 return 65000; /* HT20 MCS7 */
1951}
1952
1953
1954static unsigned int max_ht40_rate(int snr)
1955{
1956 if (snr < 3)
1957 return 13500; /* HT40 MCS0 */
1958 if (snr < 6)
1959 return 27000; /* HT40 MCS1 */
1960 if (snr < 10)
1961 return 40500; /* HT40 MCS2 */
1962 if (snr < 15)
1963 return 54000; /* HT40 MCS3 */
1964 if (snr < 17)
1965 return 81000; /* HT40 MCS4 */
1966 if (snr < 22)
1967 return 108000; /* HT40 MCS5 */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001968 if (snr < 24)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001969 return 121500; /* HT40 MCS6 */
1970 return 135000; /* HT40 MCS7 */
1971}
1972
1973
1974static unsigned int max_vht80_rate(int snr)
1975{
1976 if (snr < 1)
1977 return 0;
1978 if (snr < 2)
1979 return 29300; /* VHT80 MCS0 */
1980 if (snr < 5)
1981 return 58500; /* VHT80 MCS1 */
1982 if (snr < 9)
1983 return 87800; /* VHT80 MCS2 */
1984 if (snr < 11)
1985 return 117000; /* VHT80 MCS3 */
1986 if (snr < 15)
1987 return 175500; /* VHT80 MCS4 */
1988 if (snr < 16)
1989 return 234000; /* VHT80 MCS5 */
1990 if (snr < 18)
1991 return 263300; /* VHT80 MCS6 */
1992 if (snr < 20)
1993 return 292500; /* VHT80 MCS7 */
1994 if (snr < 22)
1995 return 351000; /* VHT80 MCS8 */
1996 return 390000; /* VHT80 MCS9 */
1997}
1998
1999
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002000void scan_est_throughput(struct wpa_supplicant *wpa_s,
2001 struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002002{
2003 enum local_hw_capab capab = wpa_s->hw_capab;
2004 int rate; /* max legacy rate in 500 kb/s units */
2005 const u8 *ie;
2006 unsigned int est, tmp;
2007 int snr = res->snr;
2008
2009 if (res->est_throughput)
2010 return;
2011
2012 /* Get maximum legacy rate */
2013 rate = wpa_scan_get_max_rate(res);
2014
2015 /* Limit based on estimated SNR */
2016 if (rate > 1 * 2 && snr < 1)
2017 rate = 1 * 2;
2018 else if (rate > 2 * 2 && snr < 4)
2019 rate = 2 * 2;
2020 else if (rate > 6 * 2 && snr < 5)
2021 rate = 6 * 2;
2022 else if (rate > 9 * 2 && snr < 6)
2023 rate = 9 * 2;
2024 else if (rate > 12 * 2 && snr < 7)
2025 rate = 12 * 2;
2026 else if (rate > 18 * 2 && snr < 10)
2027 rate = 18 * 2;
2028 else if (rate > 24 * 2 && snr < 11)
2029 rate = 24 * 2;
2030 else if (rate > 36 * 2 && snr < 15)
2031 rate = 36 * 2;
2032 else if (rate > 48 * 2 && snr < 19)
2033 rate = 48 * 2;
2034 else if (rate > 54 * 2 && snr < 21)
2035 rate = 54 * 2;
2036 est = rate * 500;
2037
2038 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2039 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2040 if (ie) {
2041 tmp = max_ht20_rate(snr);
2042 if (tmp > est)
2043 est = tmp;
2044 }
2045 }
2046
2047 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2048 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2049 if (ie && ie[1] >= 2 &&
2050 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2051 tmp = max_ht40_rate(snr);
2052 if (tmp > est)
2053 est = tmp;
2054 }
2055 }
2056
2057 if (capab == CAPAB_VHT) {
2058 /* Use +1 to assume VHT is always faster than HT */
2059 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2060 if (ie) {
2061 tmp = max_ht20_rate(snr) + 1;
2062 if (tmp > est)
2063 est = tmp;
2064
2065 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2066 if (ie && ie[1] >= 2 &&
2067 (ie[3] &
2068 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2069 tmp = max_ht40_rate(snr) + 1;
2070 if (tmp > est)
2071 est = tmp;
2072 }
2073
2074 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2075 if (ie && ie[1] >= 1 &&
2076 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2077 tmp = max_vht80_rate(snr) + 1;
2078 if (tmp > est)
2079 est = tmp;
2080 }
2081 }
2082 }
2083
2084 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2085
2086 res->est_throughput = est;
2087}
2088
2089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090/**
2091 * wpa_supplicant_get_scan_results - Get scan results
2092 * @wpa_s: Pointer to wpa_supplicant data
2093 * @info: Information about what was scanned or %NULL if not available
2094 * @new_scan: Whether a new scan was performed
2095 * Returns: Scan results, %NULL on failure
2096 *
2097 * This function request the current scan results from the driver and updates
2098 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2099 * results with wpa_scan_results_free().
2100 */
2101struct wpa_scan_results *
2102wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2103 struct scan_info *info, int new_scan)
2104{
2105 struct wpa_scan_results *scan_res;
2106 size_t i;
2107 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2108
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002109 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 if (scan_res == NULL) {
2111 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2112 return NULL;
2113 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002114 if (scan_res->fetch_time.sec == 0) {
2115 /*
2116 * Make sure we have a valid timestamp if the driver wrapper
2117 * does not set this.
2118 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002119 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002120 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002121 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002123 for (i = 0; i < scan_res->num; i++) {
2124 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2125
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002126 scan_snr(scan_res_item);
2127 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002128 }
2129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002131 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2133 "provisioning rules");
2134 compar = wpa_scan_result_wps_compar;
2135 }
2136#endif /* CONFIG_WPS */
2137
2138 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
2139 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002140 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141
2142 wpa_bss_update_start(wpa_s);
2143 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002144 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2145 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 wpa_bss_update_end(wpa_s, info, new_scan);
2147
2148 return scan_res;
2149}
2150
2151
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002152/**
2153 * wpa_supplicant_update_scan_results - Update scan results from the driver
2154 * @wpa_s: Pointer to wpa_supplicant data
2155 * Returns: 0 on success, -1 on failure
2156 *
2157 * This function updates the BSS table within wpa_supplicant based on the
2158 * currently available scan results from the driver without requesting a new
2159 * scan. This is used in cases where the driver indicates an association
2160 * (including roaming within ESS) and wpa_supplicant does not yet have the
2161 * needed information to complete the connection (e.g., to perform validation
2162 * steps in 4-way handshake).
2163 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2165{
2166 struct wpa_scan_results *scan_res;
2167 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2168 if (scan_res == NULL)
2169 return -1;
2170 wpa_scan_results_free(scan_res);
2171
2172 return 0;
2173}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002174
2175
2176/**
2177 * scan_only_handler - Reports scan results
2178 */
2179void scan_only_handler(struct wpa_supplicant *wpa_s,
2180 struct wpa_scan_results *scan_res)
2181{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002182 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002183 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2184 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2185 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2186 wpa_s->manual_scan_id);
2187 wpa_s->manual_scan_use_id = 0;
2188 } else {
2189 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2190 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002191 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002192 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002193 if (wpa_s->scan_work) {
2194 struct wpa_radio_work *work = wpa_s->scan_work;
2195 wpa_s->scan_work = NULL;
2196 radio_work_done(work);
2197 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002198
2199 if (wpa_s->wpa_state == WPA_SCANNING)
2200 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002201}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002202
2203
2204int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2205{
2206 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2207}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002208
2209
2210struct wpa_driver_scan_params *
2211wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2212{
2213 struct wpa_driver_scan_params *params;
2214 size_t i;
2215 u8 *n;
2216
2217 params = os_zalloc(sizeof(*params));
2218 if (params == NULL)
2219 return NULL;
2220
2221 for (i = 0; i < src->num_ssids; i++) {
2222 if (src->ssids[i].ssid) {
2223 n = os_malloc(src->ssids[i].ssid_len);
2224 if (n == NULL)
2225 goto failed;
2226 os_memcpy(n, src->ssids[i].ssid,
2227 src->ssids[i].ssid_len);
2228 params->ssids[i].ssid = n;
2229 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2230 }
2231 }
2232 params->num_ssids = src->num_ssids;
2233
2234 if (src->extra_ies) {
2235 n = os_malloc(src->extra_ies_len);
2236 if (n == NULL)
2237 goto failed;
2238 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2239 params->extra_ies = n;
2240 params->extra_ies_len = src->extra_ies_len;
2241 }
2242
2243 if (src->freqs) {
2244 int len = int_array_len(src->freqs);
2245 params->freqs = os_malloc((len + 1) * sizeof(int));
2246 if (params->freqs == NULL)
2247 goto failed;
2248 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2249 }
2250
2251 if (src->filter_ssids) {
Dmitry Shmidt97672262014-02-03 13:02:54 -08002252 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002253 src->num_filter_ssids);
2254 if (params->filter_ssids == NULL)
2255 goto failed;
2256 os_memcpy(params->filter_ssids, src->filter_ssids,
Dmitry Shmidt97672262014-02-03 13:02:54 -08002257 sizeof(*params->filter_ssids) *
2258 src->num_filter_ssids);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002259 params->num_filter_ssids = src->num_filter_ssids;
2260 }
2261
2262 params->filter_rssi = src->filter_rssi;
2263 params->p2p_probe = src->p2p_probe;
2264 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002265 params->low_priority = src->low_priority;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002266
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002267 if (src->sched_scan_plans_num > 0) {
2268 params->sched_scan_plans =
2269 os_malloc(sizeof(*src->sched_scan_plans) *
2270 src->sched_scan_plans_num);
2271 if (!params->sched_scan_plans)
2272 goto failed;
2273
2274 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2275 sizeof(*src->sched_scan_plans) *
2276 src->sched_scan_plans_num);
2277 params->sched_scan_plans_num = src->sched_scan_plans_num;
2278 }
2279
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002280 if (src->mac_addr_rand) {
2281 params->mac_addr_rand = src->mac_addr_rand;
2282
2283 if (src->mac_addr && src->mac_addr_mask) {
2284 u8 *mac_addr;
2285
2286 mac_addr = os_malloc(2 * ETH_ALEN);
2287 if (!mac_addr)
2288 goto failed;
2289
2290 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2291 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2292 ETH_ALEN);
2293 params->mac_addr = mac_addr;
2294 params->mac_addr_mask = mac_addr + ETH_ALEN;
2295 }
2296 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002297
2298 if (src->bssid) {
2299 u8 *bssid;
2300
2301 bssid = os_malloc(ETH_ALEN);
2302 if (!bssid)
2303 goto failed;
2304 os_memcpy(bssid, src->bssid, ETH_ALEN);
2305 params->bssid = bssid;
2306 }
2307
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002308 return params;
2309
2310failed:
2311 wpa_scan_free_params(params);
2312 return NULL;
2313}
2314
2315
2316void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2317{
2318 size_t i;
2319
2320 if (params == NULL)
2321 return;
2322
2323 for (i = 0; i < params->num_ssids; i++)
2324 os_free((u8 *) params->ssids[i].ssid);
2325 os_free((u8 *) params->extra_ies);
2326 os_free(params->freqs);
2327 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002328 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002329
2330 /*
2331 * Note: params->mac_addr_mask points to same memory allocation and
2332 * must not be freed separately.
2333 */
2334 os_free((u8 *) params->mac_addr);
2335
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002336 os_free((u8 *) params->bssid);
2337
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002338 os_free(params);
2339}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002340
2341
2342int wpas_start_pno(struct wpa_supplicant *wpa_s)
2343{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002344 int ret, prio;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002345 size_t i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002346 struct wpa_ssid *ssid;
2347 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002348 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002349 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002350
2351 if (!wpa_s->sched_scan_supported)
2352 return -1;
2353
Dmitry Shmidte4663042016-04-04 10:07:49 -07002354 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2355 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2356 else
2357 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2358 if (max_sched_scan_ssids < 1)
2359 return -1;
2360
Dmitry Shmidt98660862014-03-11 17:26:21 -07002361 if (wpa_s->pno || wpa_s->pno_sched_pending)
2362 return 0;
2363
2364 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2365 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2366 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2367 return -EAGAIN;
2368 }
2369
2370 if (wpa_s->wpa_state == WPA_SCANNING) {
2371 wpa_supplicant_cancel_scan(wpa_s);
2372 if (wpa_s->sched_scanning) {
2373 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2374 "ongoing sched scan");
2375 wpa_supplicant_cancel_sched_scan(wpa_s);
2376 wpa_s->pno_sched_pending = 1;
2377 return 0;
2378 }
2379 }
2380
2381 os_memset(&params, 0, sizeof(params));
2382
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002383 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002384 ssid = wpa_s->conf->ssid;
2385 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002386 if (!wpas_network_disabled(wpa_s, ssid)) {
2387 num_match_ssid++;
2388 if (ssid->scan_ssid)
2389 num_ssid++;
2390 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002391 ssid = ssid->next;
2392 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002393
2394 if (num_match_ssid == 0) {
2395 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2396 return -1;
2397 }
2398
2399 if (num_match_ssid > num_ssid) {
2400 params.num_ssids++; /* wildcard */
2401 num_ssid++;
2402 }
2403
Dmitry Shmidte4663042016-04-04 10:07:49 -07002404 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002405 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07002406 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2407 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002408 }
2409
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002410 if (num_match_ssid > wpa_s->max_match_sets) {
2411 num_match_ssid = wpa_s->max_match_sets;
2412 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002413 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002414 params.filter_ssids = os_calloc(num_match_ssid,
2415 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002416 if (params.filter_ssids == NULL)
2417 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002418
Dmitry Shmidt98660862014-03-11 17:26:21 -07002419 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002420 prio = 0;
2421 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002422 while (ssid) {
2423 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002424 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2425 params.ssids[params.num_ssids].ssid =
2426 ssid->ssid;
2427 params.ssids[params.num_ssids].ssid_len =
2428 ssid->ssid_len;
2429 params.num_ssids++;
2430 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002431 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2432 ssid->ssid_len);
2433 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2434 params.num_filter_ssids++;
2435 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002436 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002437 break;
2438 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002439 if (ssid->pnext)
2440 ssid = ssid->pnext;
2441 else if (prio + 1 == wpa_s->conf->num_prio)
2442 break;
2443 else
2444 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002445 }
2446
2447 if (wpa_s->conf->filter_rssi)
2448 params.filter_rssi = wpa_s->conf->filter_rssi;
2449
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002450 if (wpa_s->sched_scan_plans_num) {
2451 params.sched_scan_plans = wpa_s->sched_scan_plans;
2452 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2453 } else {
2454 /* Set one scan plan that will run infinitely */
2455 if (wpa_s->conf->sched_scan_interval)
2456 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2457 else
2458 scan_plan.interval = 10;
2459
2460 scan_plan.iterations = 0;
2461 params.sched_scan_plans = &scan_plan;
2462 params.sched_scan_plans_num = 1;
2463 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002464
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002465 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2466 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2467 params.freqs = wpa_s->manual_sched_scan_freqs;
2468 }
2469
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002470 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2471 params.mac_addr_rand = 1;
2472 if (wpa_s->mac_addr_pno) {
2473 params.mac_addr = wpa_s->mac_addr_pno;
2474 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2475 }
2476 }
2477
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002478 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002479 os_free(params.filter_ssids);
2480 if (ret == 0)
2481 wpa_s->pno = 1;
2482 else
2483 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2484 return ret;
2485}
2486
2487
2488int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2489{
2490 int ret = 0;
2491
2492 if (!wpa_s->pno)
2493 return 0;
2494
2495 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2496
2497 wpa_s->pno = 0;
2498 wpa_s->pno_sched_pending = 0;
2499
2500 if (wpa_s->wpa_state == WPA_SCANNING)
2501 wpa_supplicant_req_scan(wpa_s, 0, 0);
2502
2503 return ret;
2504}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002505
2506
2507void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2508 unsigned int type)
2509{
2510 type &= MAC_ADDR_RAND_ALL;
2511 wpa_s->mac_addr_rand_enable &= ~type;
2512
2513 if (type & MAC_ADDR_RAND_SCAN) {
2514 os_free(wpa_s->mac_addr_scan);
2515 wpa_s->mac_addr_scan = NULL;
2516 }
2517
2518 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2519 os_free(wpa_s->mac_addr_sched_scan);
2520 wpa_s->mac_addr_sched_scan = NULL;
2521 }
2522
2523 if (type & MAC_ADDR_RAND_PNO) {
2524 os_free(wpa_s->mac_addr_pno);
2525 wpa_s->mac_addr_pno = NULL;
2526 }
2527}
2528
2529
2530int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2531 unsigned int type, const u8 *addr,
2532 const u8 *mask)
2533{
2534 u8 *tmp = NULL;
2535
2536 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2537
2538 if (addr) {
2539 tmp = os_malloc(2 * ETH_ALEN);
2540 if (!tmp)
2541 return -1;
2542 os_memcpy(tmp, addr, ETH_ALEN);
2543 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2544 }
2545
2546 if (type == MAC_ADDR_RAND_SCAN) {
2547 wpa_s->mac_addr_scan = tmp;
2548 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2549 wpa_s->mac_addr_sched_scan = tmp;
2550 } else if (type == MAC_ADDR_RAND_PNO) {
2551 wpa_s->mac_addr_pno = tmp;
2552 } else {
2553 wpa_printf(MSG_INFO,
2554 "scan: Invalid MAC randomization type=0x%x",
2555 type);
2556 os_free(tmp);
2557 return -1;
2558 }
2559
2560 wpa_s->mac_addr_rand_enable |= type;
2561 return 0;
2562}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002563
2564
2565int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2566{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002567 int scan_work = !!wpa_s->scan_work;
2568
2569#ifdef CONFIG_P2P
2570 scan_work |= !!wpa_s->p2p_scan_work;
2571#endif /* CONFIG_P2P */
2572
2573 if (scan_work && wpa_s->own_scan_running) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002574 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
2575 return wpa_drv_abort_scan(wpa_s);
2576 }
2577
2578 return 0;
2579}
2580
2581
2582int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2583{
2584 struct sched_scan_plan *scan_plans = NULL;
2585 const char *token, *context = NULL;
2586 unsigned int num = 0;
2587
2588 if (!cmd)
2589 return -1;
2590
2591 if (!cmd[0]) {
2592 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2593 os_free(wpa_s->sched_scan_plans);
2594 wpa_s->sched_scan_plans = NULL;
2595 wpa_s->sched_scan_plans_num = 0;
2596 return 0;
2597 }
2598
2599 while ((token = cstr_token(cmd, " ", &context))) {
2600 int ret;
2601 struct sched_scan_plan *scan_plan, *n;
2602
2603 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2604 if (!n)
2605 goto fail;
2606
2607 scan_plans = n;
2608 scan_plan = &scan_plans[num];
2609 num++;
2610
2611 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2612 &scan_plan->iterations);
2613 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2614 wpa_printf(MSG_ERROR,
2615 "Invalid sched scan plan input: %s", token);
2616 goto fail;
2617 }
2618
2619 if (!scan_plan->interval) {
2620 wpa_printf(MSG_ERROR,
2621 "scan plan %u: Interval cannot be zero",
2622 num);
2623 goto fail;
2624 }
2625
2626 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2627 wpa_printf(MSG_WARNING,
2628 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2629 num, scan_plan->interval,
2630 wpa_s->max_sched_scan_plan_interval);
2631 scan_plan->interval =
2632 wpa_s->max_sched_scan_plan_interval;
2633 }
2634
2635 if (ret == 1) {
2636 scan_plan->iterations = 0;
2637 break;
2638 }
2639
2640 if (!scan_plan->iterations) {
2641 wpa_printf(MSG_ERROR,
2642 "scan plan %u: Number of iterations cannot be zero",
2643 num);
2644 goto fail;
2645 }
2646
2647 if (scan_plan->iterations >
2648 wpa_s->max_sched_scan_plan_iterations) {
2649 wpa_printf(MSG_WARNING,
2650 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2651 num, scan_plan->iterations,
2652 wpa_s->max_sched_scan_plan_iterations);
2653 scan_plan->iterations =
2654 wpa_s->max_sched_scan_plan_iterations;
2655 }
2656
2657 wpa_printf(MSG_DEBUG,
2658 "scan plan %u: interval=%u iterations=%u",
2659 num, scan_plan->interval, scan_plan->iterations);
2660 }
2661
2662 if (!scan_plans) {
2663 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2664 goto fail;
2665 }
2666
2667 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2668 wpa_printf(MSG_ERROR,
2669 "All scan plans but the last must specify a number of iterations");
2670 goto fail;
2671 }
2672
2673 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2674 num, scan_plans[num - 1].interval);
2675
2676 if (num > wpa_s->max_sched_scan_plans) {
2677 wpa_printf(MSG_WARNING,
2678 "Too many scheduled scan plans (only %u supported)",
2679 wpa_s->max_sched_scan_plans);
2680 wpa_printf(MSG_WARNING,
2681 "Use only the first %u scan plans, and the last one (in infinite loop)",
2682 wpa_s->max_sched_scan_plans - 1);
2683 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2684 &scan_plans[num - 1], sizeof(*scan_plans));
2685 num = wpa_s->max_sched_scan_plans;
2686 }
2687
2688 os_free(wpa_s->sched_scan_plans);
2689 wpa_s->sched_scan_plans = scan_plans;
2690 wpa_s->sched_scan_plans_num = num;
2691
2692 return 0;
2693
2694fail:
2695 os_free(scan_plans);
2696 wpa_printf(MSG_ERROR, "invalid scan plans list");
2697 return -1;
2698}