blob: 272e633f9a472fbf96be138cd9730ff734d654ce [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);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800179 /*
180 * Store the obtained vendor scan cookie (if any) in wpa_s context.
181 * The current design is to allow only one scan request on each
182 * interface, hence having this scan cookie stored in wpa_s context is
183 * fine for now.
184 *
185 * Revisit this logic if concurrent scan operations per interface
186 * is supported.
187 */
188 if (ret == 0)
189 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800190 wpa_scan_free_params(params);
191 work->ctx = NULL;
192 if (ret) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800193 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
194
195 if (wpa_s->disconnected)
196 retry = 0;
197
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800198 wpa_supplicant_notify_scanning(wpa_s, 0);
199 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800200 if (wpa_s->wpa_state == WPA_SCANNING)
201 wpa_supplicant_set_state(wpa_s,
202 wpa_s->scan_prev_wpa_state);
203 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
204 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800205 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800206
207 if (retry) {
208 /* Restore scan_req since we will try to scan again */
209 wpa_s->scan_req = wpa_s->last_scan_req;
210 wpa_supplicant_req_scan(wpa_s, 1, 0);
211 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800214
215 os_get_reltime(&wpa_s->scan_trigger_time);
216 wpa_s->scan_runs++;
217 wpa_s->normal_scans++;
218 wpa_s->own_scan_requested = 1;
219 wpa_s->clear_driver_scan_cache = 0;
220 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221}
222
223
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800224/**
225 * wpa_supplicant_trigger_scan - Request driver to start a scan
226 * @wpa_s: Pointer to wpa_supplicant data
227 * @params: Scan parameters
228 * Returns: 0 on success, -1 on failure
229 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
231 struct wpa_driver_scan_params *params)
232{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800233 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800235 if (wpa_s->scan_work) {
236 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
237 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238 }
239
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800240 ctx = wpa_scan_clone_params(params);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700241 if (!ctx ||
242 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800243 {
244 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700245 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800246 return -1;
247 }
248
249 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800250}
251
252
253static void
254wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
255{
256 struct wpa_supplicant *wpa_s = eloop_ctx;
257
258 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
259
260 if (wpa_supplicant_req_sched_scan(wpa_s))
261 wpa_supplicant_req_scan(wpa_s, 0, 0);
262}
263
264
265static void
266wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
267{
268 struct wpa_supplicant *wpa_s = eloop_ctx;
269
270 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
271
272 wpa_s->sched_scan_timed_out = 1;
273 wpa_supplicant_cancel_sched_scan(wpa_s);
274}
275
276
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700277static int
278wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
279 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800280{
281 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800283 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800284 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800285 if (ret)
286 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800287 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800288 wpa_s->sched_scanning = 1;
289
290 return ret;
291}
292
293
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700294static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800295{
296 int ret;
297
298 ret = wpa_drv_stop_sched_scan(wpa_s);
299 if (ret) {
300 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
301 /* TODO: what to do if stopping fails? */
302 return -1;
303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304
305 return ret;
306}
307
308
309static struct wpa_driver_scan_filter *
310wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
311{
312 struct wpa_driver_scan_filter *ssids;
313 struct wpa_ssid *ssid;
314 size_t count;
315
316 *num_ssids = 0;
317 if (!conf->filter_ssids)
318 return NULL;
319
320 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
321 if (ssid->ssid && ssid->ssid_len)
322 count++;
323 }
324 if (count == 0)
325 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800326 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 if (ssids == NULL)
328 return NULL;
329
330 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
331 if (!ssid->ssid || !ssid->ssid_len)
332 continue;
333 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
334 ssids[*num_ssids].ssid_len = ssid->ssid_len;
335 (*num_ssids)++;
336 }
337
338 return ssids;
339}
340
341
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800342static void wpa_supplicant_optimize_freqs(
343 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
344{
345#ifdef CONFIG_P2P
346 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
347 wpa_s->go_params) {
348 /* Optimize provisioning state scan based on GO information */
349 if (wpa_s->p2p_in_provisioning < 5 &&
350 wpa_s->go_params->freq > 0) {
351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
352 "preferred frequency %d MHz",
353 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800354 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800355 if (params->freqs)
356 params->freqs[0] = wpa_s->go_params->freq;
357 } else if (wpa_s->p2p_in_provisioning < 8 &&
358 wpa_s->go_params->freq_list[0]) {
359 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
360 "channels");
361 int_array_concat(&params->freqs,
362 wpa_s->go_params->freq_list);
363 if (params->freqs)
364 int_array_sort_unique(params->freqs);
365 }
366 wpa_s->p2p_in_provisioning++;
367 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700368
369 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
370 /*
371 * Optimize scan based on GO information during persistent
372 * group reinvocation
373 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700374 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700375 wpa_s->p2p_invite_go_freq > 0) {
376 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
377 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800378 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700379 if (params->freqs)
380 params->freqs[0] = wpa_s->p2p_invite_go_freq;
381 }
382 wpa_s->p2p_in_invitation++;
383 if (wpa_s->p2p_in_invitation > 20) {
384 /*
385 * This should not really happen since the variable is
386 * cleared on group removal, but if it does happen, make
387 * sure we do not get stuck in special invitation scan
388 * mode.
389 */
390 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
391 wpa_s->p2p_in_invitation = 0;
392 }
393 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800394#endif /* CONFIG_P2P */
395
396#ifdef CONFIG_WPS
397 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
398 /*
399 * Optimize post-provisioning scan based on channel used
400 * during provisioning.
401 */
402 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
403 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800404 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800405 if (params->freqs)
406 params->freqs[0] = wpa_s->wps_freq;
407 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800408 } else if (wpa_s->after_wps)
409 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800410
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800411 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
412 {
413 /* Optimize provisioning scan based on already known channel */
414 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
415 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800416 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800417 if (params->freqs)
418 params->freqs[0] = wpa_s->wps_freq;
419 wpa_s->known_wps_freq = 0; /* only do this once */
420 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800421#endif /* CONFIG_WPS */
422}
423
424
425#ifdef CONFIG_INTERWORKING
426static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
427 struct wpabuf *buf)
428{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800429 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
430 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
431 1 + ETH_ALEN);
432 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
433 /* No Venue Info */
434 if (!is_zero_ether_addr(wpa_s->conf->hessid))
435 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
436}
437#endif /* CONFIG_INTERWORKING */
438
439
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700440void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
441{
442 struct wpabuf *default_ies = NULL;
443 u8 ext_capab[18];
444 int ext_capab_len;
445 enum wpa_driver_if_type type = WPA_IF_STATION;
446
447#ifdef CONFIG_P2P
448 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
449 type = WPA_IF_P2P_CLIENT;
450#endif /* CONFIG_P2P */
451
452 wpa_drv_get_ext_capa(wpa_s, type);
453
454 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
455 sizeof(ext_capab));
456 if (ext_capab_len > 0 &&
457 wpabuf_resize(&default_ies, ext_capab_len) == 0)
458 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
459
460#ifdef CONFIG_MBO
461 /* Send cellular capabilities for potential MBO STAs */
462 if (wpabuf_resize(&default_ies, 9) == 0)
463 wpas_mbo_scan_ie(wpa_s, default_ies);
464#endif /* CONFIG_MBO */
465
466 if (default_ies)
467 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
468 wpabuf_len(default_ies));
469 wpabuf_free(default_ies);
470}
471
472
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700473static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800474{
475 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700476 u8 ext_capab[18];
477 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800478#ifdef CONFIG_WPS
479 int wps = 0;
480 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
481#endif /* CONFIG_WPS */
482
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700483#ifdef CONFIG_P2P
484 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
485 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
486 else
487#endif /* CONFIG_P2P */
488 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
489
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700490 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
491 sizeof(ext_capab));
492 if (ext_capab_len > 0 &&
493 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
494 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
495
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800496#ifdef CONFIG_INTERWORKING
497 if (wpa_s->conf->interworking &&
498 wpabuf_resize(&extra_ie, 100) == 0)
499 wpas_add_interworking_elements(wpa_s, extra_ie);
500#endif /* CONFIG_INTERWORKING */
501
502#ifdef CONFIG_WPS
503 wps = wpas_wps_in_use(wpa_s, &req_type);
504
505 if (wps) {
506 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700507 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
508 DEV_PW_DEFAULT,
509 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800510 wpa_s->wps->uuid, req_type,
511 0, NULL);
512 if (wps_ie) {
513 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
514 wpabuf_put_buf(extra_ie, wps_ie);
515 wpabuf_free(wps_ie);
516 }
517 }
518
519#ifdef CONFIG_P2P
520 if (wps) {
521 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
522 if (wpabuf_resize(&extra_ie, ielen) == 0)
523 wpas_p2p_scan_ie(wpa_s, extra_ie);
524 }
525#endif /* CONFIG_P2P */
526
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800527 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
528
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800529#endif /* CONFIG_WPS */
530
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700531#ifdef CONFIG_HS20
532 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800533 wpas_hs20_add_indication(extra_ie, -1);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700534#endif /* CONFIG_HS20 */
535
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800536#ifdef CONFIG_FST
537 if (wpa_s->fst_ies &&
538 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
539 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
540#endif /* CONFIG_FST */
541
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800542#ifdef CONFIG_MBO
543 /* Send cellular capabilities for potential MBO STAs */
544 if (wpabuf_resize(&extra_ie, 9) == 0)
545 wpas_mbo_scan_ie(wpa_s, extra_ie);
546#endif /* CONFIG_MBO */
547
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700548 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
549 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
550
551 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
552 wpabuf_put_buf(extra_ie, buf);
553 }
554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800555 return extra_ie;
556}
557
558
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800559#ifdef CONFIG_P2P
560
561/*
562 * Check whether there are any enabled networks or credentials that could be
563 * used for a non-P2P connection.
564 */
565static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
566{
567 struct wpa_ssid *ssid;
568
569 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
570 if (wpas_network_disabled(wpa_s, ssid))
571 continue;
572 if (!ssid->p2p_group)
573 return 1;
574 }
575
576 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
577 wpa_s->conf->auto_interworking)
578 return 1;
579
580 return 0;
581}
582
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700583#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800584
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800585
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700586static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
587 enum hostapd_hw_mode band,
588 struct wpa_driver_scan_params *params)
589{
590 /* Include only supported channels for the specified band */
591 struct hostapd_hw_modes *mode;
592 int count, i;
593
594 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
595 if (mode == NULL) {
596 /* No channels supported in this band - use empty list */
597 params->freqs = os_zalloc(sizeof(int));
598 return;
599 }
600
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800601 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700602 if (params->freqs == NULL)
603 return;
604 for (count = 0, i = 0; i < mode->num_channels; i++) {
605 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
606 continue;
607 params->freqs[count++] = mode->channels[i].freq;
608 }
609}
610
611
612static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
613 struct wpa_driver_scan_params *params)
614{
615 if (wpa_s->hw.modes == NULL)
616 return; /* unknown what channels the driver supports */
617 if (params->freqs)
618 return; /* already using a limited channel set */
619 if (wpa_s->setband == WPA_SETBAND_5G)
620 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
621 params);
622 else if (wpa_s->setband == WPA_SETBAND_2G)
623 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
624 params);
625}
626
627
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700628static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
629 struct wpa_driver_scan_params *params,
630 size_t max_ssids)
631{
632 unsigned int i;
633 struct wpa_ssid *ssid;
634
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700635 /*
636 * For devices with max_ssids greater than 1, leave the last slot empty
637 * for adding the wildcard scan entry.
638 */
639 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
640
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700641 for (i = 0; i < wpa_s->scan_id_count; i++) {
642 unsigned int j;
643
644 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
645 if (!ssid || !ssid->scan_ssid)
646 continue;
647
648 for (j = 0; j < params->num_ssids; j++) {
649 if (params->ssids[j].ssid_len == ssid->ssid_len &&
650 params->ssids[j].ssid &&
651 os_memcmp(params->ssids[j].ssid, ssid->ssid,
652 ssid->ssid_len) == 0)
653 break;
654 }
655 if (j < params->num_ssids)
656 continue; /* already in the list */
657
658 if (params->num_ssids + 1 > max_ssids) {
659 wpa_printf(MSG_DEBUG,
660 "Over max scan SSIDs for manual request");
661 break;
662 }
663
664 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
665 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
666 params->ssids[params->num_ssids].ssid = ssid->ssid;
667 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
668 params->num_ssids++;
669 }
670
671 wpa_s->scan_id_count = 0;
672}
673
674
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700675static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
676 struct wpa_driver_scan_params *params,
677 size_t max_ssids)
678{
679 unsigned int i;
680
681 if (wpa_s->ssids_from_scan_req == NULL ||
682 wpa_s->num_ssids_from_scan_req == 0)
683 return 0;
684
685 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
686 wpa_s->num_ssids_from_scan_req = max_ssids;
687 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
688 (unsigned int) max_ssids);
689 }
690
691 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
692 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
693 params->ssids[i].ssid_len =
694 wpa_s->ssids_from_scan_req[i].ssid_len;
695 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
696 params->ssids[i].ssid,
697 params->ssids[i].ssid_len);
698 }
699
700 params->num_ssids = wpa_s->num_ssids_from_scan_req;
701 wpa_s->num_ssids_from_scan_req = 0;
702 return 1;
703}
704
705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
707{
708 struct wpa_supplicant *wpa_s = eloop_ctx;
709 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800710 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700711 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700713 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800715 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716
717 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
718 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
719 return;
720 }
721
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800722 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700723 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
725 return;
726 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800727
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700728 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800729 /*
730 * If we are already in scanning state, we shall reschedule the
731 * the incoming scan request.
732 */
733 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
734 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700735 return;
736 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800737
Dmitry Shmidt04949592012-07-19 12:16:46 -0700738 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800739 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
741 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
742 return;
743 }
744
745 if (wpa_s->conf->ap_scan != 0 &&
746 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
747 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
748 "overriding ap_scan configuration");
749 wpa_s->conf->ap_scan = 0;
750 wpas_notify_ap_scan_changed(wpa_s);
751 }
752
753 if (wpa_s->conf->ap_scan == 0) {
754 wpa_supplicant_gen_assoc_event(wpa_s);
755 return;
756 }
757
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800758 ssid = NULL;
759 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
760 wpa_s->connect_without_scan) {
761 connect_without_scan = 1;
762 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
763 if (ssid == wpa_s->connect_without_scan)
764 break;
765 }
766 }
767
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800768 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800769 if (p2p_in_prog && p2p_in_prog != 2 &&
770 (!ssid ||
771 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800772 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
773 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700774 return;
775 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700776
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800777 /*
778 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
779 * used for changing modes inside wpa_supplicant (roaming,
780 * auto-reconnect, etc). Discarding the scan might hurt these processes.
781 * The normal use case for PNO is to suspend the host immediately after
782 * starting PNO, so the periodic 100 ms attempts to run the scan do not
783 * normally happen in practice multiple times, i.e., this is simply
784 * restarting scanning once the host is woken up and PNO stopped.
785 */
786 if (wpa_s->pno || wpa_s->pno_sched_pending) {
787 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
788 wpa_supplicant_req_scan(wpa_s, 0, 100000);
789 return;
790 }
791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 max_ssids = 1;
794 else {
795 max_ssids = wpa_s->max_scan_ssids;
796 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
797 max_ssids = WPAS_MAX_SCAN_SSIDS;
798 }
799
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800800 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800801 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800803 if (connect_without_scan) {
804 wpa_s->connect_without_scan = NULL;
805 if (ssid) {
806 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
807 "without scan step");
808 wpa_supplicant_associate(wpa_s, NULL, ssid);
809 return;
810 }
811 }
812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 os_memset(&params, 0, sizeof(params));
814
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800815 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
817 wpa_s->wpa_state == WPA_INACTIVE)
818 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
819
Dmitry Shmidt04949592012-07-19 12:16:46 -0700820 /*
821 * If autoscan has set its own scanning parameters
822 */
823 if (wpa_s->autoscan_params != NULL) {
824 scan_params = wpa_s->autoscan_params;
825 goto scan;
826 }
827
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700828 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
829 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
830 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
831 goto ssid_list_set;
832 }
833
Dmitry Shmidt04949592012-07-19 12:16:46 -0700834#ifdef CONFIG_P2P
835 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800836 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800837 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
838 wpa_s->p2p_in_provisioning,
839 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700840 params.ssids[0].ssid = wpa_s->go_params->ssid;
841 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
842 params.num_ssids = 1;
843 goto ssid_list_set;
844 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700845
846 if (wpa_s->p2p_in_invitation) {
847 if (wpa_s->current_ssid) {
848 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
849 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
850 params.ssids[0].ssid_len =
851 wpa_s->current_ssid->ssid_len;
852 params.num_ssids = 1;
853 } else {
854 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
855 }
856 goto ssid_list_set;
857 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700858#endif /* CONFIG_P2P */
859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 /* Find the starting point from which to continue scanning */
861 ssid = wpa_s->conf->ssid;
862 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
863 while (ssid) {
864 if (ssid == wpa_s->prev_scan_ssid) {
865 ssid = ssid->next;
866 break;
867 }
868 ssid = ssid->next;
869 }
870 }
871
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800872 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800873#ifdef CONFIG_AP
874 !wpa_s->ap_iface &&
875#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800876 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700877 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800878 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 wpa_supplicant_assoc_try(wpa_s, ssid);
880 return;
881 } else if (wpa_s->conf->ap_scan == 2) {
882 /*
883 * User-initiated scan request in ap_scan == 2; scan with
884 * wildcard SSID.
885 */
886 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700887 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
888 /*
889 * Perform single-channel single-SSID scan for
890 * reassociate-to-same-BSS operation.
891 */
892 /* Setup SSID */
893 ssid = wpa_s->current_ssid;
894 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
895 ssid->ssid, ssid->ssid_len);
896 params.ssids[0].ssid = ssid->ssid;
897 params.ssids[0].ssid_len = ssid->ssid_len;
898 params.num_ssids = 1;
899
900 /*
901 * Allocate memory for frequency array, allocate one extra
902 * slot for the zero-terminator.
903 */
904 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700905 if (params.freqs) {
906 params.freqs[0] = wpa_s->assoc_freq;
907 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700908 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700909
910 /*
911 * Reset the reattach flag so that we fall back to full scan if
912 * this scan fails.
913 */
914 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 } else {
916 struct wpa_ssid *start = ssid, *tssid;
917 int freqs_set = 0;
918 if (ssid == NULL && max_ssids > 1)
919 ssid = wpa_s->conf->ssid;
920 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700921 if (!wpas_network_disabled(wpa_s, ssid) &&
922 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
924 ssid->ssid, ssid->ssid_len);
925 params.ssids[params.num_ssids].ssid =
926 ssid->ssid;
927 params.ssids[params.num_ssids].ssid_len =
928 ssid->ssid_len;
929 params.num_ssids++;
930 if (params.num_ssids + 1 >= max_ssids)
931 break;
932 }
933 ssid = ssid->next;
934 if (ssid == start)
935 break;
936 if (ssid == NULL && max_ssids > 1 &&
937 start != wpa_s->conf->ssid)
938 ssid = wpa_s->conf->ssid;
939 }
940
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700941 if (wpa_s->scan_id_count &&
942 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
943 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
944
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800945 for (tssid = wpa_s->conf->ssid;
946 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
947 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700948 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 continue;
950 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
951 int_array_concat(&params.freqs,
952 tssid->scan_freq);
953 } else {
954 os_free(params.freqs);
955 params.freqs = NULL;
956 }
957 freqs_set = 1;
958 }
959 int_array_sort_unique(params.freqs);
960 }
961
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800962 if (ssid && max_ssids == 1) {
963 /*
964 * If the driver is limited to 1 SSID at a time interleave
965 * wildcard SSID scans with specific SSID scans to avoid
966 * waiting a long time for a wildcard scan.
967 */
968 if (!wpa_s->prev_scan_wildcard) {
969 params.ssids[0].ssid = NULL;
970 params.ssids[0].ssid_len = 0;
971 wpa_s->prev_scan_wildcard = 1;
972 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
973 "wildcard SSID (Interleave with specific)");
974 } else {
975 wpa_s->prev_scan_ssid = ssid;
976 wpa_s->prev_scan_wildcard = 0;
977 wpa_dbg(wpa_s, MSG_DEBUG,
978 "Starting AP scan for specific SSID: %s",
979 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800981 } else if (ssid) {
982 /* max_ssids > 1 */
983
984 wpa_s->prev_scan_ssid = ssid;
985 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
986 "the scan request");
987 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800988 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
989 wpa_s->manual_scan_passive && params.num_ssids == 0) {
990 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800991 } else if (wpa_s->conf->passive_scan) {
992 wpa_dbg(wpa_s, MSG_DEBUG,
993 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 } else {
995 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
996 params.num_ssids++;
997 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
998 "SSID");
999 }
1000
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001001ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001003 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001005 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001006 wpa_s->manual_scan_only_new) {
1007 wpa_printf(MSG_DEBUG,
1008 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001009 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001010 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001011
1012 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1013 wpa_s->manual_scan_freqs) {
1014 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1015 params.freqs = wpa_s->manual_scan_freqs;
1016 wpa_s->manual_scan_freqs = NULL;
1017 }
1018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1020 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1021 "generated frequency list");
1022 params.freqs = wpa_s->next_scan_freqs;
1023 } else
1024 os_free(wpa_s->next_scan_freqs);
1025 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001026 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001028 /* See if user specified frequencies. If so, scan only those. */
1029 if (wpa_s->conf->freq_list && !params.freqs) {
1030 wpa_dbg(wpa_s, MSG_DEBUG,
1031 "Optimize scan based on conf->freq_list");
1032 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1033 }
1034
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001035 /* Use current associated channel? */
1036 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001037 unsigned int num = wpa_s->num_multichan_concurrent;
1038
1039 params.freqs = os_calloc(num + 1, sizeof(int));
1040 if (params.freqs) {
1041 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1042 if (num > 0) {
1043 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1044 "current operating channels since "
1045 "scan_cur_freq is enabled");
1046 } else {
1047 os_free(params.freqs);
1048 params.freqs = NULL;
1049 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001050 }
1051 }
1052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1054 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001055 if (extra_ie) {
1056 params.extra_ies = wpabuf_head(extra_ie);
1057 params.extra_ies_len = wpabuf_len(extra_ie);
1058 }
1059
1060#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001061 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001062 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001063 /*
1064 * The interface may not yet be in P2P mode, so we have to
1065 * explicitly request P2P probe to disable CCK rates.
1066 */
1067 params.p2p_probe = 1;
1068 }
1069#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001071 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
1072 wpa_s->wpa_state <= WPA_SCANNING) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001073 params.mac_addr_rand = 1;
1074 if (wpa_s->mac_addr_scan) {
1075 params.mac_addr = wpa_s->mac_addr_scan;
1076 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1077 }
1078 }
1079
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001080 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1081 struct wpa_bss *bss;
1082
1083 params.bssid = wpa_s->next_scan_bssid;
1084 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1085 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1086 params.ssids[0].ssid_len == 0) {
1087 params.ssids[0].ssid = bss->ssid;
1088 params.ssids[0].ssid_len = bss->ssid_len;
1089 wpa_dbg(wpa_s, MSG_DEBUG,
1090 "Scan a previously specified BSSID " MACSTR
1091 " and SSID %s",
1092 MAC2STR(params.bssid),
1093 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1094 } else {
1095 wpa_dbg(wpa_s, MSG_DEBUG,
1096 "Scan a previously specified BSSID " MACSTR,
1097 MAC2STR(params.bssid));
1098 }
1099 }
1100
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101 scan_params = &params;
1102
1103scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001104#ifdef CONFIG_P2P
1105 /*
1106 * If the driver does not support multi-channel concurrency and a
1107 * virtual interface that shares the same radio with the wpa_s interface
1108 * is operating there may not be need to scan other channels apart from
1109 * the current operating channel on the other virtual interface. Filter
1110 * out other channels in case we are trying to find a connection for a
1111 * station interface when we are not configured to prefer station
1112 * connection and a concurrent operation is already in process.
1113 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001114 if (wpa_s->scan_for_connection &&
1115 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001116 !scan_params->freqs && !params.freqs &&
1117 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001118 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1119 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001120 unsigned int num = wpa_s->num_multichan_concurrent;
1121
1122 params.freqs = os_calloc(num + 1, sizeof(int));
1123 if (params.freqs) {
1124 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1125 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1126 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1127 } else {
1128 os_free(params.freqs);
1129 params.freqs = NULL;
1130 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001131 }
1132 }
1133#endif /* CONFIG_P2P */
1134
Dmitry Shmidt04949592012-07-19 12:16:46 -07001135 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001137 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1138 !wpa_s->manual_scan_freqs) {
1139 /* Restore manual_scan_freqs for the next attempt */
1140 wpa_s->manual_scan_freqs = params.freqs;
1141 params.freqs = NULL;
1142 }
1143
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001144 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 os_free(params.freqs);
1146 os_free(params.filter_ssids);
1147
1148 if (ret) {
1149 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001150 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1151 wpa_supplicant_set_state(wpa_s,
1152 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001153 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001154 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001156 } else {
1157 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001158#ifdef CONFIG_INTERWORKING
1159 wpa_s->interworking_fast_assoc_tried = 0;
1160#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001161 if (params.bssid)
1162 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 }
1164}
1165
1166
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001167void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1168{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001169 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001170 int cancelled;
1171
1172 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1173 &remaining);
1174
1175 new_int.sec = sec;
1176 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001177 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001178 new_int.sec = remaining.sec;
1179 new_int.usec = remaining.usec;
1180 }
1181
Dmitry Shmidt051af732013-10-22 13:52:46 -07001182 if (cancelled) {
1183 eloop_register_timeout(new_int.sec, new_int.usec,
1184 wpa_supplicant_scan, wpa_s, NULL);
1185 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001186 wpa_s->scan_interval = sec;
1187}
1188
1189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190/**
1191 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1192 * @wpa_s: Pointer to wpa_supplicant data
1193 * @sec: Number of seconds after which to scan
1194 * @usec: Number of microseconds after which to scan
1195 *
1196 * This function is used to schedule a scan for neighboring access points after
1197 * the specified time.
1198 */
1199void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1200{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001201 int res;
1202
1203 if (wpa_s->p2p_mgmt) {
1204 wpa_dbg(wpa_s, MSG_DEBUG,
1205 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1206 sec, usec);
1207 return;
1208 }
1209
1210 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1211 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001212 if (res == 1) {
1213 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001214 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001215 } else if (res == 0) {
1216 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1217 sec, usec);
1218 } else {
1219 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1220 sec, usec);
1221 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223}
1224
1225
1226/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001227 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1228 * @wpa_s: Pointer to wpa_supplicant data
1229 * @sec: Number of seconds after which to scan
1230 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001231 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232 *
1233 * This function is used to schedule periodic scans for neighboring
1234 * access points after the specified time.
1235 */
1236int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1237 int sec, int usec)
1238{
1239 if (!wpa_s->sched_scan_supported)
1240 return -1;
1241
1242 eloop_register_timeout(sec, usec,
1243 wpa_supplicant_delayed_sched_scan_timeout,
1244 wpa_s, NULL);
1245
1246 return 0;
1247}
1248
1249
1250/**
1251 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1252 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001253 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254 *
1255 * This function is used to schedule periodic scans for neighboring
1256 * access points repeating the scan continuously.
1257 */
1258int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1259{
1260 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001261 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001262 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001263 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001264 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001265 int ret;
1266 unsigned int max_sched_scan_ssids;
1267 int wildcard = 0;
1268 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001269 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001270
1271 if (!wpa_s->sched_scan_supported)
1272 return -1;
1273
1274 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1275 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1276 else
1277 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001278 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001279 return -1;
1280
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001281 wpa_s->sched_scan_stop_req = 0;
1282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001283 if (wpa_s->sched_scanning) {
1284 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1285 return 0;
1286 }
1287
1288 need_ssids = 0;
1289 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001290 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 /* Use wildcard SSID to find this network */
1292 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001293 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1294 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001295 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001296
1297#ifdef CONFIG_WPS
1298 if (!wpas_network_disabled(wpa_s, ssid) &&
1299 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1300 /*
1301 * Normal scan is more reliable and faster for WPS
1302 * operations and since these are for short periods of
1303 * time, the benefit of trying to use sched_scan would
1304 * be limited.
1305 */
1306 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1307 "sched_scan for WPS");
1308 return -1;
1309 }
1310#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001311 }
1312 if (wildcard)
1313 need_ssids++;
1314
1315 if (wpa_s->normal_scans < 3 &&
1316 (need_ssids <= wpa_s->max_scan_ssids ||
1317 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1318 /*
1319 * When normal scan can speed up operations, use that for the
1320 * first operations before starting the sched_scan to allow
1321 * user space sleep more. We do this only if the normal scan
1322 * has functionality that is suitable for this or if the
1323 * sched_scan does not have better support for multiple SSIDs.
1324 */
1325 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1326 "sched_scan for initial scans (normal_scans=%d)",
1327 wpa_s->normal_scans);
1328 return -1;
1329 }
1330
1331 os_memset(&params, 0, sizeof(params));
1332
1333 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001334 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001335 sizeof(struct wpa_driver_scan_filter));
1336
1337 prev_state = wpa_s->wpa_state;
1338 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1339 wpa_s->wpa_state == WPA_INACTIVE)
1340 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1341
Dmitry Shmidt04949592012-07-19 12:16:46 -07001342 if (wpa_s->autoscan_params != NULL) {
1343 scan_params = wpa_s->autoscan_params;
1344 goto scan;
1345 }
1346
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001347 /* Find the starting point from which to continue scanning */
1348 ssid = wpa_s->conf->ssid;
1349 if (wpa_s->prev_sched_ssid) {
1350 while (ssid) {
1351 if (ssid == wpa_s->prev_sched_ssid) {
1352 ssid = ssid->next;
1353 break;
1354 }
1355 ssid = ssid->next;
1356 }
1357 }
1358
1359 if (!ssid || !wpa_s->prev_sched_ssid) {
1360 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001361 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1362 wpa_s->first_sched_scan = 1;
1363 ssid = wpa_s->conf->ssid;
1364 wpa_s->prev_sched_ssid = ssid;
1365 }
1366
1367 if (wildcard) {
1368 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1369 params.num_ssids++;
1370 }
1371
1372 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001373 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001374 goto next;
1375
1376 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1377 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1378 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1379 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1380 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1381 ssid->ssid, ssid->ssid_len);
1382 params.filter_ssids[params.num_filter_ssids].ssid_len =
1383 ssid->ssid_len;
1384 params.num_filter_ssids++;
1385 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1386 {
1387 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1388 "filter for sched_scan - drop filter");
1389 os_free(params.filter_ssids);
1390 params.filter_ssids = NULL;
1391 params.num_filter_ssids = 0;
1392 }
1393
1394 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1395 if (params.num_ssids == max_sched_scan_ssids)
1396 break; /* only room for broadcast SSID */
1397 wpa_dbg(wpa_s, MSG_DEBUG,
1398 "add to active scan ssid: %s",
1399 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1400 params.ssids[params.num_ssids].ssid =
1401 ssid->ssid;
1402 params.ssids[params.num_ssids].ssid_len =
1403 ssid->ssid_len;
1404 params.num_ssids++;
1405 if (params.num_ssids >= max_sched_scan_ssids) {
1406 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001407 do {
1408 ssid = ssid->next;
1409 } while (ssid &&
1410 (wpas_network_disabled(wpa_s, ssid) ||
1411 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001412 break;
1413 }
1414 }
1415
1416 next:
1417 wpa_s->prev_sched_ssid = ssid;
1418 ssid = ssid->next;
1419 }
1420
1421 if (params.num_filter_ssids == 0) {
1422 os_free(params.filter_ssids);
1423 params.filter_ssids = NULL;
1424 }
1425
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001426 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1427 if (extra_ie) {
1428 params.extra_ies = wpabuf_head(extra_ie);
1429 params.extra_ies_len = wpabuf_len(extra_ie);
1430 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001431
Dmitry Shmidt18463232014-01-24 12:29:41 -08001432 if (wpa_s->conf->filter_rssi)
1433 params.filter_rssi = wpa_s->conf->filter_rssi;
1434
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001435 /* See if user specified frequencies. If so, scan only those. */
1436 if (wpa_s->conf->freq_list && !params.freqs) {
1437 wpa_dbg(wpa_s, MSG_DEBUG,
1438 "Optimize scan based on conf->freq_list");
1439 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1440 }
1441
Dmitry Shmidt04949592012-07-19 12:16:46 -07001442 scan_params = &params;
1443
1444scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001445 wpa_s->sched_scan_timed_out = 0;
1446
1447 /*
1448 * We cannot support multiple scan plans if the scan request includes
1449 * too many SSID's, so in this case use only the last scan plan and make
1450 * it run infinitely. It will be stopped by the timeout.
1451 */
1452 if (wpa_s->sched_scan_plans_num == 1 ||
1453 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1454 params.sched_scan_plans = wpa_s->sched_scan_plans;
1455 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1456 } else if (wpa_s->sched_scan_plans_num > 1) {
1457 wpa_dbg(wpa_s, MSG_DEBUG,
1458 "Too many SSIDs. Default to using single scheduled_scan plan");
1459 params.sched_scan_plans =
1460 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1461 1];
1462 params.sched_scan_plans_num = 1;
1463 } else {
1464 if (wpa_s->conf->sched_scan_interval)
1465 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1466 else
1467 scan_plan.interval = 10;
1468
1469 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1470 wpa_printf(MSG_WARNING,
1471 "Scan interval too long(%u), use the maximum allowed(%u)",
1472 scan_plan.interval,
1473 wpa_s->max_sched_scan_plan_interval);
1474 scan_plan.interval =
1475 wpa_s->max_sched_scan_plan_interval;
1476 }
1477
1478 scan_plan.iterations = 0;
1479 params.sched_scan_plans = &scan_plan;
1480 params.sched_scan_plans_num = 1;
1481 }
1482
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001483 if (ssid || !wpa_s->first_sched_scan) {
1484 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001485 "Starting sched scan: interval %u timeout %d",
1486 params.sched_scan_plans[0].interval,
1487 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001488 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001489 wpa_dbg(wpa_s, MSG_DEBUG, "Starting sched scan (no timeout)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001490 }
1491
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001492 wpa_setband_scan_freqs(wpa_s, scan_params);
1493
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001494 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
1495 wpa_s->wpa_state <= WPA_SCANNING) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001496 params.mac_addr_rand = 1;
1497 if (wpa_s->mac_addr_sched_scan) {
1498 params.mac_addr = wpa_s->mac_addr_sched_scan;
1499 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1500 ETH_ALEN;
1501 }
1502 }
1503
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001504 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001505 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001506 os_free(params.filter_ssids);
1507 if (ret) {
1508 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1509 if (prev_state != wpa_s->wpa_state)
1510 wpa_supplicant_set_state(wpa_s, prev_state);
1511 return ret;
1512 }
1513
1514 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1515 if (ssid || !wpa_s->first_sched_scan) {
1516 wpa_s->sched_scan_timed_out = 0;
1517 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1518 wpa_supplicant_sched_scan_timeout,
1519 wpa_s, NULL);
1520 wpa_s->first_sched_scan = 0;
1521 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001522 params.sched_scan_plans[0].interval *= 2;
1523 if ((unsigned int) wpa_s->sched_scan_timeout <
1524 params.sched_scan_plans[0].interval ||
1525 params.sched_scan_plans[0].interval >
1526 wpa_s->max_sched_scan_plan_interval) {
1527 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001528 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1529 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001530 }
1531
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001532 /* If there is no more ssids, start next time from the beginning */
1533 if (!ssid)
1534 wpa_s->prev_sched_ssid = NULL;
1535
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001536 return 0;
1537}
1538
1539
1540/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1542 * @wpa_s: Pointer to wpa_supplicant data
1543 *
1544 * This function is used to cancel a scan request scheduled with
1545 * wpa_supplicant_req_scan().
1546 */
1547void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1548{
1549 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1550 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551}
1552
1553
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001554/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001555 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1556 * @wpa_s: Pointer to wpa_supplicant data
1557 *
1558 * This function is used to stop a delayed scheduled scan.
1559 */
1560void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1561{
1562 if (!wpa_s->sched_scan_supported)
1563 return;
1564
1565 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1566 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1567 wpa_s, NULL);
1568}
1569
1570
1571/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001572 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1573 * @wpa_s: Pointer to wpa_supplicant data
1574 *
1575 * This function is used to stop a periodic scheduled scan.
1576 */
1577void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1578{
1579 if (!wpa_s->sched_scanning)
1580 return;
1581
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001582 if (wpa_s->sched_scanning)
1583 wpa_s->sched_scan_stop_req = 1;
1584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001585 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1586 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1587 wpa_supplicant_stop_sched_scan(wpa_s);
1588}
1589
1590
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001591/**
1592 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1593 * @wpa_s: Pointer to wpa_supplicant data
1594 * @scanning: Whether scanning is currently in progress
1595 *
1596 * This function is to generate scanning notifycations. It is called whenever
1597 * there may have been a change in scanning (scan started, completed, stopped).
1598 * wpas_notify_scanning() is called whenever the scanning state changed from the
1599 * previously notified state.
1600 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1602 int scanning)
1603{
1604 if (wpa_s->scanning != scanning) {
1605 wpa_s->scanning = scanning;
1606 wpas_notify_scanning(wpa_s);
1607 }
1608}
1609
1610
1611static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1612{
1613 int rate = 0;
1614 const u8 *ie;
1615 int i;
1616
1617 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1618 for (i = 0; ie && i < ie[1]; i++) {
1619 if ((ie[i + 2] & 0x7f) > rate)
1620 rate = ie[i + 2] & 0x7f;
1621 }
1622
1623 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1624 for (i = 0; ie && i < ie[1]; i++) {
1625 if ((ie[i + 2] & 0x7f) > rate)
1626 rate = ie[i + 2] & 0x7f;
1627 }
1628
1629 return rate;
1630}
1631
1632
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001633/**
1634 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1635 * @res: Scan result entry
1636 * @ie: Information element identitifier (WLAN_EID_*)
1637 * Returns: Pointer to the information element (id field) or %NULL if not found
1638 *
1639 * This function returns the first matching information element in the scan
1640 * result.
1641 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1643{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001644 size_t ie_len = res->ie_len;
1645
1646 /* Use the Beacon frame IEs if res->ie_len is not available */
1647 if (!ie_len)
1648 ie_len = res->beacon_ie_len;
1649
1650 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651}
1652
1653
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001654/**
1655 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1656 * @res: Scan result entry
1657 * @vendor_type: Vendor type (four octets starting the IE payload)
1658 * Returns: Pointer to the information element (id field) or %NULL if not found
1659 *
1660 * This function returns the first matching information element in the scan
1661 * result.
1662 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1664 u32 vendor_type)
1665{
1666 const u8 *end, *pos;
1667
1668 pos = (const u8 *) (res + 1);
1669 end = pos + res->ie_len;
1670
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001671 while (end - pos > 1) {
1672 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673 break;
1674 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1675 vendor_type == WPA_GET_BE32(&pos[2]))
1676 return pos;
1677 pos += 2 + pos[1];
1678 }
1679
1680 return NULL;
1681}
1682
1683
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001684/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001685 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1686 * @res: Scan result entry
1687 * @vendor_type: Vendor type (four octets starting the IE payload)
1688 * Returns: Pointer to the information element (id field) or %NULL if not found
1689 *
1690 * This function returns the first matching information element in the scan
1691 * result.
1692 *
1693 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1694 * from Beacon frames instead of either Beacon or Probe Response frames.
1695 */
1696const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1697 u32 vendor_type)
1698{
1699 const u8 *end, *pos;
1700
1701 if (res->beacon_ie_len == 0)
1702 return NULL;
1703
1704 pos = (const u8 *) (res + 1);
1705 pos += res->ie_len;
1706 end = pos + res->beacon_ie_len;
1707
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001708 while (end - pos > 1) {
1709 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001710 break;
1711 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1712 vendor_type == WPA_GET_BE32(&pos[2]))
1713 return pos;
1714 pos += 2 + pos[1];
1715 }
1716
1717 return NULL;
1718}
1719
1720
1721/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001722 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1723 * @res: Scan result entry
1724 * @vendor_type: Vendor type (four octets starting the IE payload)
1725 * Returns: Pointer to the information element payload or %NULL if not found
1726 *
1727 * This function returns concatenated payload of possibly fragmented vendor
1728 * specific information elements in the scan result. The caller is responsible
1729 * for freeing the returned buffer.
1730 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001731struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1732 u32 vendor_type)
1733{
1734 struct wpabuf *buf;
1735 const u8 *end, *pos;
1736
1737 buf = wpabuf_alloc(res->ie_len);
1738 if (buf == NULL)
1739 return NULL;
1740
1741 pos = (const u8 *) (res + 1);
1742 end = pos + res->ie_len;
1743
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001744 while (end - pos > 1) {
1745 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 break;
1747 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1748 vendor_type == WPA_GET_BE32(&pos[2]))
1749 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1750 pos += 2 + pos[1];
1751 }
1752
1753 if (wpabuf_len(buf) == 0) {
1754 wpabuf_free(buf);
1755 buf = NULL;
1756 }
1757
1758 return buf;
1759}
1760
1761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001762/*
1763 * Channels with a great SNR can operate at full rate. What is a great SNR?
1764 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1765 * rule of thumb is that any SNR above 20 is good." This one
1766 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1767 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1768 * conservative value.
1769 */
1770#define GREAT_SNR 30
1771
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001772#define IS_5GHZ(n) (n > 4000)
1773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774/* Compare function for sorting scan results. Return >0 if @b is considered
1775 * better. */
1776static int wpa_scan_result_compar(const void *a, const void *b)
1777{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779 struct wpa_scan_res **_wa = (void *) a;
1780 struct wpa_scan_res **_wb = (void *) b;
1781 struct wpa_scan_res *wa = *_wa;
1782 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001783 int wpa_a, wpa_b;
1784 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785
1786 /* WPA/WPA2 support preferred */
1787 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1788 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1789 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1790 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1791
1792 if (wpa_b && !wpa_a)
1793 return 1;
1794 if (!wpa_b && wpa_a)
1795 return -1;
1796
1797 /* privacy support preferred */
1798 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1799 (wb->caps & IEEE80211_CAP_PRIVACY))
1800 return 1;
1801 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1802 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1803 return -1;
1804
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001805 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001806 snr_a_full = wa->snr;
1807 snr_a = MIN(wa->snr, GREAT_SNR);
1808 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001809 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001811 /* Level is not in dBm, so we can't calculate
1812 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001813 snr_a = snr_a_full = wa->level;
1814 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001815 }
1816
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001817 /* if SNR is close, decide by max rate or frequency band */
1818 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001820 if (wa->est_throughput != wb->est_throughput)
1821 return wb->est_throughput - wa->est_throughput;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001822 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1823 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824 }
1825
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001826 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 * identical, use quality values since some drivers may only report
1828 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001829 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001831 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833}
1834
1835
1836#ifdef CONFIG_WPS
1837/* Compare function for sorting scan results when searching a WPS AP for
1838 * provisioning. Return >0 if @b is considered better. */
1839static int wpa_scan_result_wps_compar(const void *a, const void *b)
1840{
1841 struct wpa_scan_res **_wa = (void *) a;
1842 struct wpa_scan_res **_wb = (void *) b;
1843 struct wpa_scan_res *wa = *_wa;
1844 struct wpa_scan_res *wb = *_wb;
1845 int uses_wps_a, uses_wps_b;
1846 struct wpabuf *wps_a, *wps_b;
1847 int res;
1848
1849 /* Optimization - check WPS IE existence before allocated memory and
1850 * doing full reassembly. */
1851 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1852 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1853 if (uses_wps_a && !uses_wps_b)
1854 return -1;
1855 if (!uses_wps_a && uses_wps_b)
1856 return 1;
1857
1858 if (uses_wps_a && uses_wps_b) {
1859 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1860 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1861 res = wps_ap_priority_compar(wps_a, wps_b);
1862 wpabuf_free(wps_a);
1863 wpabuf_free(wps_b);
1864 if (res)
1865 return res;
1866 }
1867
1868 /*
1869 * Do not use current AP security policy as a sorting criteria during
1870 * WPS provisioning step since the AP may get reconfigured at the
1871 * completion of provisioning.
1872 */
1873
1874 /* all things being equal, use signal level; if signal levels are
1875 * identical, use quality values since some drivers may only report
1876 * that value and leave the signal level zero */
1877 if (wb->level == wa->level)
1878 return wb->qual - wa->qual;
1879 return wb->level - wa->level;
1880}
1881#endif /* CONFIG_WPS */
1882
1883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884static void dump_scan_res(struct wpa_scan_results *scan_res)
1885{
1886#ifndef CONFIG_NO_STDOUT_DEBUG
1887 size_t i;
1888
1889 if (scan_res->res == NULL || scan_res->num == 0)
1890 return;
1891
1892 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1893
1894 for (i = 0; i < scan_res->num; i++) {
1895 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001896 u8 *pos;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001897 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001898 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1899
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001900 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001901 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001902 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001903 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001904 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1905 r->flags,
1906 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907 } else {
1908 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001909 "noise=%d level=%d flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001910 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001911 r->noise, r->level, r->flags, r->age,
1912 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001913 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001914 pos = (u8 *) (r + 1);
1915 if (r->ie_len)
1916 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1917 pos += r->ie_len;
1918 if (r->beacon_ie_len)
1919 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1920 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001921 }
1922#endif /* CONFIG_NO_STDOUT_DEBUG */
1923}
1924
1925
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001926/**
1927 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1928 * @wpa_s: Pointer to wpa_supplicant data
1929 * @bssid: BSSID to check
1930 * Returns: 0 if the BSSID is filtered or 1 if not
1931 *
1932 * This function is used to filter out specific BSSIDs from scan reslts mainly
1933 * for testing purposes (SET bssid_filter ctrl_iface command).
1934 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001935int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1936 const u8 *bssid)
1937{
1938 size_t i;
1939
1940 if (wpa_s->bssid_filter == NULL)
1941 return 1;
1942
1943 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1944 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1945 ETH_ALEN) == 0)
1946 return 1;
1947 }
1948
1949 return 0;
1950}
1951
1952
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001953void filter_scan_res(struct wpa_supplicant *wpa_s,
1954 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001955{
1956 size_t i, j;
1957
1958 if (wpa_s->bssid_filter == NULL)
1959 return;
1960
1961 for (i = 0, j = 0; i < res->num; i++) {
1962 if (wpa_supplicant_filter_bssid_match(wpa_s,
1963 res->res[i]->bssid)) {
1964 res->res[j++] = res->res[i];
1965 } else {
1966 os_free(res->res[i]);
1967 res->res[i] = NULL;
1968 }
1969 }
1970
1971 if (res->num != j) {
1972 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1973 (int) (res->num - j));
1974 res->num = j;
1975 }
1976}
1977
1978
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001979/*
1980 * Noise floor values to use when we have signal strength
Dmitry Shmidte4663042016-04-04 10:07:49 -07001981 * measurements, but no noise floor measurements. These values were
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001982 * measured in an office environment with many APs.
1983 */
1984#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
1985#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
1986
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001987void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001988{
1989 if (res->flags & WPA_SCAN_NOISE_INVALID) {
1990 res->noise = IS_5GHZ(res->freq) ?
1991 DEFAULT_NOISE_FLOOR_5GHZ :
1992 DEFAULT_NOISE_FLOOR_2GHZ;
1993 }
1994
1995 if (res->flags & WPA_SCAN_LEVEL_DBM) {
1996 res->snr = res->level - res->noise;
1997 } else {
1998 /* Level is not in dBm, so we can't calculate
1999 * SNR. Just use raw level (units unknown). */
2000 res->snr = res->level;
2001 }
2002}
2003
2004
2005static unsigned int max_ht20_rate(int snr)
2006{
2007 if (snr < 6)
2008 return 6500; /* HT20 MCS0 */
2009 if (snr < 8)
2010 return 13000; /* HT20 MCS1 */
2011 if (snr < 13)
2012 return 19500; /* HT20 MCS2 */
2013 if (snr < 17)
2014 return 26000; /* HT20 MCS3 */
2015 if (snr < 20)
2016 return 39000; /* HT20 MCS4 */
2017 if (snr < 23)
2018 return 52000; /* HT20 MCS5 */
2019 if (snr < 24)
2020 return 58500; /* HT20 MCS6 */
2021 return 65000; /* HT20 MCS7 */
2022}
2023
2024
2025static unsigned int max_ht40_rate(int snr)
2026{
2027 if (snr < 3)
2028 return 13500; /* HT40 MCS0 */
2029 if (snr < 6)
2030 return 27000; /* HT40 MCS1 */
2031 if (snr < 10)
2032 return 40500; /* HT40 MCS2 */
2033 if (snr < 15)
2034 return 54000; /* HT40 MCS3 */
2035 if (snr < 17)
2036 return 81000; /* HT40 MCS4 */
2037 if (snr < 22)
2038 return 108000; /* HT40 MCS5 */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002039 if (snr < 24)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002040 return 121500; /* HT40 MCS6 */
2041 return 135000; /* HT40 MCS7 */
2042}
2043
2044
2045static unsigned int max_vht80_rate(int snr)
2046{
2047 if (snr < 1)
2048 return 0;
2049 if (snr < 2)
2050 return 29300; /* VHT80 MCS0 */
2051 if (snr < 5)
2052 return 58500; /* VHT80 MCS1 */
2053 if (snr < 9)
2054 return 87800; /* VHT80 MCS2 */
2055 if (snr < 11)
2056 return 117000; /* VHT80 MCS3 */
2057 if (snr < 15)
2058 return 175500; /* VHT80 MCS4 */
2059 if (snr < 16)
2060 return 234000; /* VHT80 MCS5 */
2061 if (snr < 18)
2062 return 263300; /* VHT80 MCS6 */
2063 if (snr < 20)
2064 return 292500; /* VHT80 MCS7 */
2065 if (snr < 22)
2066 return 351000; /* VHT80 MCS8 */
2067 return 390000; /* VHT80 MCS9 */
2068}
2069
2070
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002071void scan_est_throughput(struct wpa_supplicant *wpa_s,
2072 struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002073{
2074 enum local_hw_capab capab = wpa_s->hw_capab;
2075 int rate; /* max legacy rate in 500 kb/s units */
2076 const u8 *ie;
2077 unsigned int est, tmp;
2078 int snr = res->snr;
2079
2080 if (res->est_throughput)
2081 return;
2082
2083 /* Get maximum legacy rate */
2084 rate = wpa_scan_get_max_rate(res);
2085
2086 /* Limit based on estimated SNR */
2087 if (rate > 1 * 2 && snr < 1)
2088 rate = 1 * 2;
2089 else if (rate > 2 * 2 && snr < 4)
2090 rate = 2 * 2;
2091 else if (rate > 6 * 2 && snr < 5)
2092 rate = 6 * 2;
2093 else if (rate > 9 * 2 && snr < 6)
2094 rate = 9 * 2;
2095 else if (rate > 12 * 2 && snr < 7)
2096 rate = 12 * 2;
2097 else if (rate > 18 * 2 && snr < 10)
2098 rate = 18 * 2;
2099 else if (rate > 24 * 2 && snr < 11)
2100 rate = 24 * 2;
2101 else if (rate > 36 * 2 && snr < 15)
2102 rate = 36 * 2;
2103 else if (rate > 48 * 2 && snr < 19)
2104 rate = 48 * 2;
2105 else if (rate > 54 * 2 && snr < 21)
2106 rate = 54 * 2;
2107 est = rate * 500;
2108
2109 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2110 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2111 if (ie) {
2112 tmp = max_ht20_rate(snr);
2113 if (tmp > est)
2114 est = tmp;
2115 }
2116 }
2117
2118 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2119 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2120 if (ie && ie[1] >= 2 &&
2121 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2122 tmp = max_ht40_rate(snr);
2123 if (tmp > est)
2124 est = tmp;
2125 }
2126 }
2127
2128 if (capab == CAPAB_VHT) {
2129 /* Use +1 to assume VHT is always faster than HT */
2130 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2131 if (ie) {
2132 tmp = max_ht20_rate(snr) + 1;
2133 if (tmp > est)
2134 est = tmp;
2135
2136 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2137 if (ie && ie[1] >= 2 &&
2138 (ie[3] &
2139 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2140 tmp = max_ht40_rate(snr) + 1;
2141 if (tmp > est)
2142 est = tmp;
2143 }
2144
2145 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2146 if (ie && ie[1] >= 1 &&
2147 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2148 tmp = max_vht80_rate(snr) + 1;
2149 if (tmp > est)
2150 est = tmp;
2151 }
2152 }
2153 }
2154
2155 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2156
2157 res->est_throughput = est;
2158}
2159
2160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161/**
2162 * wpa_supplicant_get_scan_results - Get scan results
2163 * @wpa_s: Pointer to wpa_supplicant data
2164 * @info: Information about what was scanned or %NULL if not available
2165 * @new_scan: Whether a new scan was performed
2166 * Returns: Scan results, %NULL on failure
2167 *
2168 * This function request the current scan results from the driver and updates
2169 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2170 * results with wpa_scan_results_free().
2171 */
2172struct wpa_scan_results *
2173wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2174 struct scan_info *info, int new_scan)
2175{
2176 struct wpa_scan_results *scan_res;
2177 size_t i;
2178 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002180 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 if (scan_res == NULL) {
2182 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2183 return NULL;
2184 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002185 if (scan_res->fetch_time.sec == 0) {
2186 /*
2187 * Make sure we have a valid timestamp if the driver wrapper
2188 * does not set this.
2189 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002190 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002191 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002192 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002194 for (i = 0; i < scan_res->num; i++) {
2195 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2196
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002197 scan_snr(scan_res_item);
2198 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002199 }
2200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002202 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2204 "provisioning rules");
2205 compar = wpa_scan_result_wps_compar;
2206 }
2207#endif /* CONFIG_WPS */
2208
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002209 if (scan_res->res) {
2210 qsort(scan_res->res, scan_res->num,
2211 sizeof(struct wpa_scan_res *), compar);
2212 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002213 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214
2215 wpa_bss_update_start(wpa_s);
2216 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002217 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2218 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 wpa_bss_update_end(wpa_s, info, new_scan);
2220
2221 return scan_res;
2222}
2223
2224
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002225/**
2226 * wpa_supplicant_update_scan_results - Update scan results from the driver
2227 * @wpa_s: Pointer to wpa_supplicant data
2228 * Returns: 0 on success, -1 on failure
2229 *
2230 * This function updates the BSS table within wpa_supplicant based on the
2231 * currently available scan results from the driver without requesting a new
2232 * scan. This is used in cases where the driver indicates an association
2233 * (including roaming within ESS) and wpa_supplicant does not yet have the
2234 * needed information to complete the connection (e.g., to perform validation
2235 * steps in 4-way handshake).
2236 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2238{
2239 struct wpa_scan_results *scan_res;
2240 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2241 if (scan_res == NULL)
2242 return -1;
2243 wpa_scan_results_free(scan_res);
2244
2245 return 0;
2246}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002247
2248
2249/**
2250 * scan_only_handler - Reports scan results
2251 */
2252void scan_only_handler(struct wpa_supplicant *wpa_s,
2253 struct wpa_scan_results *scan_res)
2254{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002255 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002256 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2257 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2258 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2259 wpa_s->manual_scan_id);
2260 wpa_s->manual_scan_use_id = 0;
2261 } else {
2262 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2263 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002264 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002265 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002266 if (wpa_s->scan_work) {
2267 struct wpa_radio_work *work = wpa_s->scan_work;
2268 wpa_s->scan_work = NULL;
2269 radio_work_done(work);
2270 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002271
2272 if (wpa_s->wpa_state == WPA_SCANNING)
2273 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002274}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002275
2276
2277int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2278{
2279 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2280}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002281
2282
2283struct wpa_driver_scan_params *
2284wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2285{
2286 struct wpa_driver_scan_params *params;
2287 size_t i;
2288 u8 *n;
2289
2290 params = os_zalloc(sizeof(*params));
2291 if (params == NULL)
2292 return NULL;
2293
2294 for (i = 0; i < src->num_ssids; i++) {
2295 if (src->ssids[i].ssid) {
2296 n = os_malloc(src->ssids[i].ssid_len);
2297 if (n == NULL)
2298 goto failed;
2299 os_memcpy(n, src->ssids[i].ssid,
2300 src->ssids[i].ssid_len);
2301 params->ssids[i].ssid = n;
2302 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2303 }
2304 }
2305 params->num_ssids = src->num_ssids;
2306
2307 if (src->extra_ies) {
2308 n = os_malloc(src->extra_ies_len);
2309 if (n == NULL)
2310 goto failed;
2311 os_memcpy(n, src->extra_ies, src->extra_ies_len);
2312 params->extra_ies = n;
2313 params->extra_ies_len = src->extra_ies_len;
2314 }
2315
2316 if (src->freqs) {
2317 int len = int_array_len(src->freqs);
2318 params->freqs = os_malloc((len + 1) * sizeof(int));
2319 if (params->freqs == NULL)
2320 goto failed;
2321 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
2322 }
2323
2324 if (src->filter_ssids) {
Dmitry Shmidt97672262014-02-03 13:02:54 -08002325 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002326 src->num_filter_ssids);
2327 if (params->filter_ssids == NULL)
2328 goto failed;
2329 os_memcpy(params->filter_ssids, src->filter_ssids,
Dmitry Shmidt97672262014-02-03 13:02:54 -08002330 sizeof(*params->filter_ssids) *
2331 src->num_filter_ssids);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002332 params->num_filter_ssids = src->num_filter_ssids;
2333 }
2334
2335 params->filter_rssi = src->filter_rssi;
2336 params->p2p_probe = src->p2p_probe;
2337 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002338 params->low_priority = src->low_priority;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002339
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002340 if (src->sched_scan_plans_num > 0) {
2341 params->sched_scan_plans =
2342 os_malloc(sizeof(*src->sched_scan_plans) *
2343 src->sched_scan_plans_num);
2344 if (!params->sched_scan_plans)
2345 goto failed;
2346
2347 os_memcpy(params->sched_scan_plans, src->sched_scan_plans,
2348 sizeof(*src->sched_scan_plans) *
2349 src->sched_scan_plans_num);
2350 params->sched_scan_plans_num = src->sched_scan_plans_num;
2351 }
2352
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002353 if (src->mac_addr_rand) {
2354 params->mac_addr_rand = src->mac_addr_rand;
2355
2356 if (src->mac_addr && src->mac_addr_mask) {
2357 u8 *mac_addr;
2358
2359 mac_addr = os_malloc(2 * ETH_ALEN);
2360 if (!mac_addr)
2361 goto failed;
2362
2363 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2364 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2365 ETH_ALEN);
2366 params->mac_addr = mac_addr;
2367 params->mac_addr_mask = mac_addr + ETH_ALEN;
2368 }
2369 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002370
2371 if (src->bssid) {
2372 u8 *bssid;
2373
2374 bssid = os_malloc(ETH_ALEN);
2375 if (!bssid)
2376 goto failed;
2377 os_memcpy(bssid, src->bssid, ETH_ALEN);
2378 params->bssid = bssid;
2379 }
2380
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002381 return params;
2382
2383failed:
2384 wpa_scan_free_params(params);
2385 return NULL;
2386}
2387
2388
2389void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2390{
2391 size_t i;
2392
2393 if (params == NULL)
2394 return;
2395
2396 for (i = 0; i < params->num_ssids; i++)
2397 os_free((u8 *) params->ssids[i].ssid);
2398 os_free((u8 *) params->extra_ies);
2399 os_free(params->freqs);
2400 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002401 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002402
2403 /*
2404 * Note: params->mac_addr_mask points to same memory allocation and
2405 * must not be freed separately.
2406 */
2407 os_free((u8 *) params->mac_addr);
2408
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002409 os_free((u8 *) params->bssid);
2410
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002411 os_free(params);
2412}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002413
2414
2415int wpas_start_pno(struct wpa_supplicant *wpa_s)
2416{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002417 int ret, prio;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002418 size_t i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002419 struct wpa_ssid *ssid;
2420 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002421 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002422 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002423
2424 if (!wpa_s->sched_scan_supported)
2425 return -1;
2426
Dmitry Shmidte4663042016-04-04 10:07:49 -07002427 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2428 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2429 else
2430 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2431 if (max_sched_scan_ssids < 1)
2432 return -1;
2433
Dmitry Shmidt98660862014-03-11 17:26:21 -07002434 if (wpa_s->pno || wpa_s->pno_sched_pending)
2435 return 0;
2436
2437 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2438 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2439 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2440 return -EAGAIN;
2441 }
2442
2443 if (wpa_s->wpa_state == WPA_SCANNING) {
2444 wpa_supplicant_cancel_scan(wpa_s);
2445 if (wpa_s->sched_scanning) {
2446 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2447 "ongoing sched scan");
2448 wpa_supplicant_cancel_sched_scan(wpa_s);
2449 wpa_s->pno_sched_pending = 1;
2450 return 0;
2451 }
2452 }
2453
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002454 if (wpa_s->sched_scan_stop_req) {
2455 wpa_printf(MSG_DEBUG,
2456 "Schedule PNO after previous sched scan has stopped");
2457 wpa_s->pno_sched_pending = 1;
2458 return 0;
2459 }
2460
Dmitry Shmidt98660862014-03-11 17:26:21 -07002461 os_memset(&params, 0, sizeof(params));
2462
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002463 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002464 ssid = wpa_s->conf->ssid;
2465 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002466 if (!wpas_network_disabled(wpa_s, ssid)) {
2467 num_match_ssid++;
2468 if (ssid->scan_ssid)
2469 num_ssid++;
2470 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002471 ssid = ssid->next;
2472 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002473
2474 if (num_match_ssid == 0) {
2475 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2476 return -1;
2477 }
2478
2479 if (num_match_ssid > num_ssid) {
2480 params.num_ssids++; /* wildcard */
2481 num_ssid++;
2482 }
2483
Dmitry Shmidte4663042016-04-04 10:07:49 -07002484 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002485 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07002486 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2487 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002488 }
2489
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002490 if (num_match_ssid > wpa_s->max_match_sets) {
2491 num_match_ssid = wpa_s->max_match_sets;
2492 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002493 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002494 params.filter_ssids = os_calloc(num_match_ssid,
2495 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002496 if (params.filter_ssids == NULL)
2497 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002498
Dmitry Shmidt98660862014-03-11 17:26:21 -07002499 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002500 prio = 0;
2501 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002502 while (ssid) {
2503 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002504 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2505 params.ssids[params.num_ssids].ssid =
2506 ssid->ssid;
2507 params.ssids[params.num_ssids].ssid_len =
2508 ssid->ssid_len;
2509 params.num_ssids++;
2510 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002511 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2512 ssid->ssid_len);
2513 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2514 params.num_filter_ssids++;
2515 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002516 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002517 break;
2518 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002519 if (ssid->pnext)
2520 ssid = ssid->pnext;
2521 else if (prio + 1 == wpa_s->conf->num_prio)
2522 break;
2523 else
2524 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002525 }
2526
2527 if (wpa_s->conf->filter_rssi)
2528 params.filter_rssi = wpa_s->conf->filter_rssi;
2529
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002530 if (wpa_s->sched_scan_plans_num) {
2531 params.sched_scan_plans = wpa_s->sched_scan_plans;
2532 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2533 } else {
2534 /* Set one scan plan that will run infinitely */
2535 if (wpa_s->conf->sched_scan_interval)
2536 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2537 else
2538 scan_plan.interval = 10;
2539
2540 scan_plan.iterations = 0;
2541 params.sched_scan_plans = &scan_plan;
2542 params.sched_scan_plans_num = 1;
2543 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002544
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002545 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2546 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2547 params.freqs = wpa_s->manual_sched_scan_freqs;
2548 }
2549
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002550 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
2551 wpa_s->wpa_state <= WPA_SCANNING) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002552 params.mac_addr_rand = 1;
2553 if (wpa_s->mac_addr_pno) {
2554 params.mac_addr = wpa_s->mac_addr_pno;
2555 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2556 }
2557 }
2558
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002559 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002560 os_free(params.filter_ssids);
2561 if (ret == 0)
2562 wpa_s->pno = 1;
2563 else
2564 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2565 return ret;
2566}
2567
2568
2569int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2570{
2571 int ret = 0;
2572
2573 if (!wpa_s->pno)
2574 return 0;
2575
2576 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002577 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002578
2579 wpa_s->pno = 0;
2580 wpa_s->pno_sched_pending = 0;
2581
2582 if (wpa_s->wpa_state == WPA_SCANNING)
2583 wpa_supplicant_req_scan(wpa_s, 0, 0);
2584
2585 return ret;
2586}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002587
2588
2589void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2590 unsigned int type)
2591{
2592 type &= MAC_ADDR_RAND_ALL;
2593 wpa_s->mac_addr_rand_enable &= ~type;
2594
2595 if (type & MAC_ADDR_RAND_SCAN) {
2596 os_free(wpa_s->mac_addr_scan);
2597 wpa_s->mac_addr_scan = NULL;
2598 }
2599
2600 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2601 os_free(wpa_s->mac_addr_sched_scan);
2602 wpa_s->mac_addr_sched_scan = NULL;
2603 }
2604
2605 if (type & MAC_ADDR_RAND_PNO) {
2606 os_free(wpa_s->mac_addr_pno);
2607 wpa_s->mac_addr_pno = NULL;
2608 }
2609}
2610
2611
2612int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2613 unsigned int type, const u8 *addr,
2614 const u8 *mask)
2615{
2616 u8 *tmp = NULL;
2617
2618 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2619
2620 if (addr) {
2621 tmp = os_malloc(2 * ETH_ALEN);
2622 if (!tmp)
2623 return -1;
2624 os_memcpy(tmp, addr, ETH_ALEN);
2625 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2626 }
2627
2628 if (type == MAC_ADDR_RAND_SCAN) {
2629 wpa_s->mac_addr_scan = tmp;
2630 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2631 wpa_s->mac_addr_sched_scan = tmp;
2632 } else if (type == MAC_ADDR_RAND_PNO) {
2633 wpa_s->mac_addr_pno = tmp;
2634 } else {
2635 wpa_printf(MSG_INFO,
2636 "scan: Invalid MAC randomization type=0x%x",
2637 type);
2638 os_free(tmp);
2639 return -1;
2640 }
2641
2642 wpa_s->mac_addr_rand_enable |= type;
2643 return 0;
2644}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002645
2646
2647int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2648{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002649 struct wpa_radio_work *work;
2650 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002651
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002652 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
2653 if (work->wpa_s != wpa_s || !work->started ||
2654 (os_strcmp(work->type, "scan") != 0 &&
2655 os_strcmp(work->type, "p2p-scan") != 0))
2656 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002657 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002658 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002659 }
2660
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002661 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
2662 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002663}
2664
2665
2666int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2667{
2668 struct sched_scan_plan *scan_plans = NULL;
2669 const char *token, *context = NULL;
2670 unsigned int num = 0;
2671
2672 if (!cmd)
2673 return -1;
2674
2675 if (!cmd[0]) {
2676 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2677 os_free(wpa_s->sched_scan_plans);
2678 wpa_s->sched_scan_plans = NULL;
2679 wpa_s->sched_scan_plans_num = 0;
2680 return 0;
2681 }
2682
2683 while ((token = cstr_token(cmd, " ", &context))) {
2684 int ret;
2685 struct sched_scan_plan *scan_plan, *n;
2686
2687 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2688 if (!n)
2689 goto fail;
2690
2691 scan_plans = n;
2692 scan_plan = &scan_plans[num];
2693 num++;
2694
2695 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2696 &scan_plan->iterations);
2697 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2698 wpa_printf(MSG_ERROR,
2699 "Invalid sched scan plan input: %s", token);
2700 goto fail;
2701 }
2702
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002703 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2704 wpa_printf(MSG_WARNING,
2705 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2706 num, scan_plan->interval,
2707 wpa_s->max_sched_scan_plan_interval);
2708 scan_plan->interval =
2709 wpa_s->max_sched_scan_plan_interval;
2710 }
2711
2712 if (ret == 1) {
2713 scan_plan->iterations = 0;
2714 break;
2715 }
2716
2717 if (!scan_plan->iterations) {
2718 wpa_printf(MSG_ERROR,
2719 "scan plan %u: Number of iterations cannot be zero",
2720 num);
2721 goto fail;
2722 }
2723
2724 if (scan_plan->iterations >
2725 wpa_s->max_sched_scan_plan_iterations) {
2726 wpa_printf(MSG_WARNING,
2727 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2728 num, scan_plan->iterations,
2729 wpa_s->max_sched_scan_plan_iterations);
2730 scan_plan->iterations =
2731 wpa_s->max_sched_scan_plan_iterations;
2732 }
2733
2734 wpa_printf(MSG_DEBUG,
2735 "scan plan %u: interval=%u iterations=%u",
2736 num, scan_plan->interval, scan_plan->iterations);
2737 }
2738
2739 if (!scan_plans) {
2740 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2741 goto fail;
2742 }
2743
2744 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2745 wpa_printf(MSG_ERROR,
2746 "All scan plans but the last must specify a number of iterations");
2747 goto fail;
2748 }
2749
2750 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2751 num, scan_plans[num - 1].interval);
2752
2753 if (num > wpa_s->max_sched_scan_plans) {
2754 wpa_printf(MSG_WARNING,
2755 "Too many scheduled scan plans (only %u supported)",
2756 wpa_s->max_sched_scan_plans);
2757 wpa_printf(MSG_WARNING,
2758 "Use only the first %u scan plans, and the last one (in infinite loop)",
2759 wpa_s->max_sched_scan_plans - 1);
2760 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2761 &scan_plans[num - 1], sizeof(*scan_plans));
2762 num = wpa_s->max_sched_scan_plans;
2763 }
2764
2765 os_free(wpa_s->sched_scan_plans);
2766 wpa_s->sched_scan_plans = scan_plans;
2767 wpa_s->sched_scan_plans_num = num;
2768
2769 return 0;
2770
2771fail:
2772 os_free(scan_plans);
2773 wpa_printf(MSG_ERROR, "invalid scan plans list");
2774 return -1;
2775}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002776
2777
2778/**
2779 * wpas_scan_reset_sched_scan - Reset sched_scan state
2780 * @wpa_s: Pointer to wpa_supplicant data
2781 *
2782 * This function is used to cancel a running scheduled scan and to reset an
2783 * internal scan state to continue with a regular scan on the following
2784 * wpa_supplicant_req_scan() calls.
2785 */
2786void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
2787{
2788 wpa_s->normal_scans = 0;
2789 if (wpa_s->sched_scanning) {
2790 wpa_s->sched_scan_timed_out = 0;
2791 wpa_s->prev_sched_ssid = NULL;
2792 wpa_supplicant_cancel_sched_scan(wpa_s);
2793 }
2794}
2795
2796
2797void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
2798{
2799 /* simulate timeout to restart the sched scan */
2800 wpa_s->sched_scan_timed_out = 1;
2801 wpa_s->prev_sched_ssid = NULL;
2802 wpa_supplicant_cancel_sched_scan(wpa_s);
2803}