blob: 104b258984a3e9a843dd63667d0a4cb3733928b0 [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{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700120 int min_temp_disabled = 0;
121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 while (ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700123 if (!wpas_network_disabled(wpa_s, ssid)) {
124 int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
125
126 if (temp_disabled <= 0)
127 break;
128
129 if (!min_temp_disabled ||
130 temp_disabled < min_temp_disabled)
131 min_temp_disabled = temp_disabled;
132 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 ssid = ssid->next;
134 }
135
136 /* ap_scan=2 mode - try to associate with each SSID. */
137 if (ssid == NULL) {
138 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
139 "end of scan list - go back to beginning");
140 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700141 wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 return;
143 }
144 if (ssid->next) {
145 /* Continue from the next SSID on the next attempt. */
146 wpa_s->prev_scan_ssid = ssid;
147 } else {
148 /* Start from the beginning of the SSID list. */
149 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
150 }
151 wpa_supplicant_associate(wpa_s, NULL, ssid);
152}
153
154
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800155static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800157 struct wpa_supplicant *wpa_s = work->wpa_s;
158 struct wpa_driver_scan_params *params = work->ctx;
159 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800161 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800162 if (!work->started) {
163 wpa_scan_free_params(params);
164 return;
165 }
166 wpa_supplicant_notify_scanning(wpa_s, 0);
167 wpas_notify_scan_done(wpa_s, 0);
168 wpa_s->scan_work = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 return;
170 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700172 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
173 wpa_msg(wpa_s, MSG_INFO,
174 "Failed to assign random MAC address for a scan");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700175 wpa_scan_free_params(params);
176 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700177 radio_work_done(work);
178 return;
179 }
180
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800181 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800183 if (wpa_s->clear_driver_scan_cache) {
184 wpa_printf(MSG_DEBUG,
185 "Request driver to clear scan cache due to local BSS flush");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800186 params->only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800187 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800188 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800189 /*
190 * Store the obtained vendor scan cookie (if any) in wpa_s context.
191 * The current design is to allow only one scan request on each
192 * interface, hence having this scan cookie stored in wpa_s context is
193 * fine for now.
194 *
195 * Revisit this logic if concurrent scan operations per interface
196 * is supported.
197 */
198 if (ret == 0)
199 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800200 wpa_scan_free_params(params);
201 work->ctx = NULL;
202 if (ret) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700203 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
204 !wpa_s->beacon_rep_data.token;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800205
206 if (wpa_s->disconnected)
207 retry = 0;
208
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800209 wpa_supplicant_notify_scanning(wpa_s, 0);
210 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800211 if (wpa_s->wpa_state == WPA_SCANNING)
212 wpa_supplicant_set_state(wpa_s,
213 wpa_s->scan_prev_wpa_state);
214 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
215 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800216 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800217
218 if (retry) {
219 /* Restore scan_req since we will try to scan again */
220 wpa_s->scan_req = wpa_s->last_scan_req;
221 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700222 } else if (wpa_s->scan_res_handler) {
223 /* Clear the scan_res_handler */
224 wpa_s->scan_res_handler = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800225 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700226
227 if (wpa_s->beacon_rep_data.token)
228 wpas_rrm_refuse_request(wpa_s);
229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800232
233 os_get_reltime(&wpa_s->scan_trigger_time);
234 wpa_s->scan_runs++;
235 wpa_s->normal_scans++;
236 wpa_s->own_scan_requested = 1;
237 wpa_s->clear_driver_scan_cache = 0;
238 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239}
240
241
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800242/**
243 * wpa_supplicant_trigger_scan - Request driver to start a scan
244 * @wpa_s: Pointer to wpa_supplicant data
245 * @params: Scan parameters
246 * Returns: 0 on success, -1 on failure
247 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
249 struct wpa_driver_scan_params *params)
250{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800253 if (wpa_s->scan_work) {
254 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
255 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800256 }
257
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800258 ctx = wpa_scan_clone_params(params);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700259 if (!ctx ||
260 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800261 {
262 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700263 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 return -1;
265 }
266
267 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800268}
269
270
271static void
272wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
273{
274 struct wpa_supplicant *wpa_s = eloop_ctx;
275
276 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
277
278 if (wpa_supplicant_req_sched_scan(wpa_s))
279 wpa_supplicant_req_scan(wpa_s, 0, 0);
280}
281
282
283static void
284wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
285{
286 struct wpa_supplicant *wpa_s = eloop_ctx;
287
288 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
289
290 wpa_s->sched_scan_timed_out = 1;
291 wpa_supplicant_cancel_sched_scan(wpa_s);
292}
293
294
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700295static int
296wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
297 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800298{
299 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800301 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800302 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303 if (ret)
304 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800305 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 wpa_s->sched_scanning = 1;
307
308 return ret;
309}
310
311
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700312static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313{
314 int ret;
315
316 ret = wpa_drv_stop_sched_scan(wpa_s);
317 if (ret) {
318 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
319 /* TODO: what to do if stopping fails? */
320 return -1;
321 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322
323 return ret;
324}
325
326
327static struct wpa_driver_scan_filter *
328wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
329{
330 struct wpa_driver_scan_filter *ssids;
331 struct wpa_ssid *ssid;
332 size_t count;
333
334 *num_ssids = 0;
335 if (!conf->filter_ssids)
336 return NULL;
337
338 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
339 if (ssid->ssid && ssid->ssid_len)
340 count++;
341 }
342 if (count == 0)
343 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800344 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 if (ssids == NULL)
346 return NULL;
347
348 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
349 if (!ssid->ssid || !ssid->ssid_len)
350 continue;
351 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
352 ssids[*num_ssids].ssid_len = ssid->ssid_len;
353 (*num_ssids)++;
354 }
355
356 return ssids;
357}
358
359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360static void wpa_supplicant_optimize_freqs(
361 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
362{
363#ifdef CONFIG_P2P
364 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
365 wpa_s->go_params) {
366 /* Optimize provisioning state scan based on GO information */
367 if (wpa_s->p2p_in_provisioning < 5 &&
368 wpa_s->go_params->freq > 0) {
369 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
370 "preferred frequency %d MHz",
371 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800372 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800373 if (params->freqs)
374 params->freqs[0] = wpa_s->go_params->freq;
375 } else if (wpa_s->p2p_in_provisioning < 8 &&
376 wpa_s->go_params->freq_list[0]) {
377 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
378 "channels");
379 int_array_concat(&params->freqs,
380 wpa_s->go_params->freq_list);
381 if (params->freqs)
382 int_array_sort_unique(params->freqs);
383 }
384 wpa_s->p2p_in_provisioning++;
385 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700386
387 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
388 /*
389 * Optimize scan based on GO information during persistent
390 * group reinvocation
391 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700392 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700393 wpa_s->p2p_invite_go_freq > 0) {
394 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
395 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800396 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700397 if (params->freqs)
398 params->freqs[0] = wpa_s->p2p_invite_go_freq;
399 }
400 wpa_s->p2p_in_invitation++;
401 if (wpa_s->p2p_in_invitation > 20) {
402 /*
403 * This should not really happen since the variable is
404 * cleared on group removal, but if it does happen, make
405 * sure we do not get stuck in special invitation scan
406 * mode.
407 */
408 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
409 wpa_s->p2p_in_invitation = 0;
410 }
411 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800412#endif /* CONFIG_P2P */
413
414#ifdef CONFIG_WPS
415 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
416 /*
417 * Optimize post-provisioning scan based on channel used
418 * during provisioning.
419 */
420 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
421 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800422 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800423 if (params->freqs)
424 params->freqs[0] = wpa_s->wps_freq;
425 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800426 } else if (wpa_s->after_wps)
427 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800428
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800429 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
430 {
431 /* Optimize provisioning scan based on already known channel */
432 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
433 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800434 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800435 if (params->freqs)
436 params->freqs[0] = wpa_s->wps_freq;
437 wpa_s->known_wps_freq = 0; /* only do this once */
438 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800439#endif /* CONFIG_WPS */
440}
441
442
443#ifdef CONFIG_INTERWORKING
444static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
445 struct wpabuf *buf)
446{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
448 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
449 1 + ETH_ALEN);
450 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
451 /* No Venue Info */
452 if (!is_zero_ether_addr(wpa_s->conf->hessid))
453 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
454}
455#endif /* CONFIG_INTERWORKING */
456
457
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700458void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
459{
460 struct wpabuf *default_ies = NULL;
461 u8 ext_capab[18];
462 int ext_capab_len;
463 enum wpa_driver_if_type type = WPA_IF_STATION;
464
465#ifdef CONFIG_P2P
466 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
467 type = WPA_IF_P2P_CLIENT;
468#endif /* CONFIG_P2P */
469
470 wpa_drv_get_ext_capa(wpa_s, type);
471
472 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
473 sizeof(ext_capab));
474 if (ext_capab_len > 0 &&
475 wpabuf_resize(&default_ies, ext_capab_len) == 0)
476 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
477
478#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700479 /* Send MBO and OCE capabilities */
480 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700481 wpas_mbo_scan_ie(wpa_s, default_ies);
482#endif /* CONFIG_MBO */
483
484 if (default_ies)
485 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
486 wpabuf_len(default_ies));
487 wpabuf_free(default_ies);
488}
489
490
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700491static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800492{
493 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700494 u8 ext_capab[18];
495 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800496#ifdef CONFIG_WPS
497 int wps = 0;
498 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
499#endif /* CONFIG_WPS */
500
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700501#ifdef CONFIG_P2P
502 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
503 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
504 else
505#endif /* CONFIG_P2P */
506 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
507
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700508 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
509 sizeof(ext_capab));
510 if (ext_capab_len > 0 &&
511 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
512 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800514#ifdef CONFIG_INTERWORKING
515 if (wpa_s->conf->interworking &&
516 wpabuf_resize(&extra_ie, 100) == 0)
517 wpas_add_interworking_elements(wpa_s, extra_ie);
518#endif /* CONFIG_INTERWORKING */
519
520#ifdef CONFIG_WPS
521 wps = wpas_wps_in_use(wpa_s, &req_type);
522
523 if (wps) {
524 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700525 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
526 DEV_PW_DEFAULT,
527 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800528 wpa_s->wps->uuid, req_type,
529 0, NULL);
530 if (wps_ie) {
531 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
532 wpabuf_put_buf(extra_ie, wps_ie);
533 wpabuf_free(wps_ie);
534 }
535 }
536
537#ifdef CONFIG_P2P
538 if (wps) {
539 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
540 if (wpabuf_resize(&extra_ie, ielen) == 0)
541 wpas_p2p_scan_ie(wpa_s, extra_ie);
542 }
543#endif /* CONFIG_P2P */
544
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800545 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547#endif /* CONFIG_WPS */
548
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700549#ifdef CONFIG_HS20
550 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800551 wpas_hs20_add_indication(extra_ie, -1);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700552#endif /* CONFIG_HS20 */
553
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800554#ifdef CONFIG_FST
555 if (wpa_s->fst_ies &&
556 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
557 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
558#endif /* CONFIG_FST */
559
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800560#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700561 /* Send MBO and OCE capabilities */
562 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800563 wpas_mbo_scan_ie(wpa_s, extra_ie);
564#endif /* CONFIG_MBO */
565
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700566 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
567 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
568
569 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
570 wpabuf_put_buf(extra_ie, buf);
571 }
572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573 return extra_ie;
574}
575
576
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800577#ifdef CONFIG_P2P
578
579/*
580 * Check whether there are any enabled networks or credentials that could be
581 * used for a non-P2P connection.
582 */
583static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
584{
585 struct wpa_ssid *ssid;
586
587 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
588 if (wpas_network_disabled(wpa_s, ssid))
589 continue;
590 if (!ssid->p2p_group)
591 return 1;
592 }
593
594 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
595 wpa_s->conf->auto_interworking)
596 return 1;
597
598 return 0;
599}
600
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700601#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800602
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800603
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700604static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
605 enum hostapd_hw_mode band,
606 struct wpa_driver_scan_params *params)
607{
608 /* Include only supported channels for the specified band */
609 struct hostapd_hw_modes *mode;
610 int count, i;
611
612 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
613 if (mode == NULL) {
614 /* No channels supported in this band - use empty list */
615 params->freqs = os_zalloc(sizeof(int));
616 return;
617 }
618
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800619 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700620 if (params->freqs == NULL)
621 return;
622 for (count = 0, i = 0; i < mode->num_channels; i++) {
623 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
624 continue;
625 params->freqs[count++] = mode->channels[i].freq;
626 }
627}
628
629
630static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
631 struct wpa_driver_scan_params *params)
632{
633 if (wpa_s->hw.modes == NULL)
634 return; /* unknown what channels the driver supports */
635 if (params->freqs)
636 return; /* already using a limited channel set */
637 if (wpa_s->setband == WPA_SETBAND_5G)
638 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
639 params);
640 else if (wpa_s->setband == WPA_SETBAND_2G)
641 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
642 params);
643}
644
645
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700646static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
647 struct wpa_driver_scan_params *params,
648 size_t max_ssids)
649{
650 unsigned int i;
651 struct wpa_ssid *ssid;
652
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700653 /*
654 * For devices with max_ssids greater than 1, leave the last slot empty
655 * for adding the wildcard scan entry.
656 */
657 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
658
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700659 for (i = 0; i < wpa_s->scan_id_count; i++) {
660 unsigned int j;
661
662 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
663 if (!ssid || !ssid->scan_ssid)
664 continue;
665
666 for (j = 0; j < params->num_ssids; j++) {
667 if (params->ssids[j].ssid_len == ssid->ssid_len &&
668 params->ssids[j].ssid &&
669 os_memcmp(params->ssids[j].ssid, ssid->ssid,
670 ssid->ssid_len) == 0)
671 break;
672 }
673 if (j < params->num_ssids)
674 continue; /* already in the list */
675
676 if (params->num_ssids + 1 > max_ssids) {
677 wpa_printf(MSG_DEBUG,
678 "Over max scan SSIDs for manual request");
679 break;
680 }
681
682 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
683 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
684 params->ssids[params->num_ssids].ssid = ssid->ssid;
685 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
686 params->num_ssids++;
687 }
688
689 wpa_s->scan_id_count = 0;
690}
691
692
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700693static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
694 struct wpa_driver_scan_params *params,
695 size_t max_ssids)
696{
697 unsigned int i;
698
699 if (wpa_s->ssids_from_scan_req == NULL ||
700 wpa_s->num_ssids_from_scan_req == 0)
701 return 0;
702
703 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
704 wpa_s->num_ssids_from_scan_req = max_ssids;
705 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
706 (unsigned int) max_ssids);
707 }
708
709 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
710 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
711 params->ssids[i].ssid_len =
712 wpa_s->ssids_from_scan_req[i].ssid_len;
713 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
714 params->ssids[i].ssid,
715 params->ssids[i].ssid_len);
716 }
717
718 params->num_ssids = wpa_s->num_ssids_from_scan_req;
719 wpa_s->num_ssids_from_scan_req = 0;
720 return 1;
721}
722
723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
725{
726 struct wpa_supplicant *wpa_s = eloop_ctx;
727 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800728 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700729 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700731 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800733 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734
Dmitry Shmidt29333592017-01-09 12:27:11 -0800735 wpa_s->ignore_post_flush_scan_res = 0;
736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
738 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
739 return;
740 }
741
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800742 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700743 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
745 return;
746 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800747
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700748 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800749 /*
750 * If we are already in scanning state, we shall reschedule the
751 * the incoming scan request.
752 */
753 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
754 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700755 return;
756 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800757
Dmitry Shmidt04949592012-07-19 12:16:46 -0700758 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800759 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
761 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
762 return;
763 }
764
765 if (wpa_s->conf->ap_scan != 0 &&
766 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
767 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
768 "overriding ap_scan configuration");
769 wpa_s->conf->ap_scan = 0;
770 wpas_notify_ap_scan_changed(wpa_s);
771 }
772
773 if (wpa_s->conf->ap_scan == 0) {
774 wpa_supplicant_gen_assoc_event(wpa_s);
775 return;
776 }
777
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800778 ssid = NULL;
779 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
780 wpa_s->connect_without_scan) {
781 connect_without_scan = 1;
782 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
783 if (ssid == wpa_s->connect_without_scan)
784 break;
785 }
786 }
787
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800788 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800789 if (p2p_in_prog && p2p_in_prog != 2 &&
790 (!ssid ||
791 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800792 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
793 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700794 return;
795 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700796
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800797 /*
798 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
799 * used for changing modes inside wpa_supplicant (roaming,
800 * auto-reconnect, etc). Discarding the scan might hurt these processes.
801 * The normal use case for PNO is to suspend the host immediately after
802 * starting PNO, so the periodic 100 ms attempts to run the scan do not
803 * normally happen in practice multiple times, i.e., this is simply
804 * restarting scanning once the host is woken up and PNO stopped.
805 */
806 if (wpa_s->pno || wpa_s->pno_sched_pending) {
807 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
808 wpa_supplicant_req_scan(wpa_s, 0, 100000);
809 return;
810 }
811
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 max_ssids = 1;
814 else {
815 max_ssids = wpa_s->max_scan_ssids;
816 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
817 max_ssids = WPAS_MAX_SCAN_SSIDS;
818 }
819
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800820 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800821 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800823 if (connect_without_scan) {
824 wpa_s->connect_without_scan = NULL;
825 if (ssid) {
826 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
827 "without scan step");
828 wpa_supplicant_associate(wpa_s, NULL, ssid);
829 return;
830 }
831 }
832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 os_memset(&params, 0, sizeof(params));
834
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800835 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
837 wpa_s->wpa_state == WPA_INACTIVE)
838 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
839
Dmitry Shmidt04949592012-07-19 12:16:46 -0700840 /*
841 * If autoscan has set its own scanning parameters
842 */
843 if (wpa_s->autoscan_params != NULL) {
844 scan_params = wpa_s->autoscan_params;
845 goto scan;
846 }
847
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700848 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
849 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
850 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
851 goto ssid_list_set;
852 }
853
Dmitry Shmidt04949592012-07-19 12:16:46 -0700854#ifdef CONFIG_P2P
855 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800856 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800857 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
858 wpa_s->p2p_in_provisioning,
859 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860 params.ssids[0].ssid = wpa_s->go_params->ssid;
861 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
862 params.num_ssids = 1;
863 goto ssid_list_set;
864 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700865
866 if (wpa_s->p2p_in_invitation) {
867 if (wpa_s->current_ssid) {
868 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
869 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
870 params.ssids[0].ssid_len =
871 wpa_s->current_ssid->ssid_len;
872 params.num_ssids = 1;
873 } else {
874 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
875 }
876 goto ssid_list_set;
877 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700878#endif /* CONFIG_P2P */
879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 /* Find the starting point from which to continue scanning */
881 ssid = wpa_s->conf->ssid;
882 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
883 while (ssid) {
884 if (ssid == wpa_s->prev_scan_ssid) {
885 ssid = ssid->next;
886 break;
887 }
888 ssid = ssid->next;
889 }
890 }
891
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800892 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800893#ifdef CONFIG_AP
894 !wpa_s->ap_iface &&
895#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800896 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700897 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800898 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 wpa_supplicant_assoc_try(wpa_s, ssid);
900 return;
901 } else if (wpa_s->conf->ap_scan == 2) {
902 /*
903 * User-initiated scan request in ap_scan == 2; scan with
904 * wildcard SSID.
905 */
906 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700907 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
908 /*
909 * Perform single-channel single-SSID scan for
910 * reassociate-to-same-BSS operation.
911 */
912 /* Setup SSID */
913 ssid = wpa_s->current_ssid;
914 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
915 ssid->ssid, ssid->ssid_len);
916 params.ssids[0].ssid = ssid->ssid;
917 params.ssids[0].ssid_len = ssid->ssid_len;
918 params.num_ssids = 1;
919
920 /*
921 * Allocate memory for frequency array, allocate one extra
922 * slot for the zero-terminator.
923 */
924 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700925 if (params.freqs) {
926 params.freqs[0] = wpa_s->assoc_freq;
927 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700928 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700929
930 /*
931 * Reset the reattach flag so that we fall back to full scan if
932 * this scan fails.
933 */
934 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 } else {
936 struct wpa_ssid *start = ssid, *tssid;
937 int freqs_set = 0;
938 if (ssid == NULL && max_ssids > 1)
939 ssid = wpa_s->conf->ssid;
940 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700941 if (!wpas_network_disabled(wpa_s, ssid) &&
942 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
944 ssid->ssid, ssid->ssid_len);
945 params.ssids[params.num_ssids].ssid =
946 ssid->ssid;
947 params.ssids[params.num_ssids].ssid_len =
948 ssid->ssid_len;
949 params.num_ssids++;
950 if (params.num_ssids + 1 >= max_ssids)
951 break;
952 }
953 ssid = ssid->next;
954 if (ssid == start)
955 break;
956 if (ssid == NULL && max_ssids > 1 &&
957 start != wpa_s->conf->ssid)
958 ssid = wpa_s->conf->ssid;
959 }
960
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700961 if (wpa_s->scan_id_count &&
962 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
963 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
964
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800965 for (tssid = wpa_s->conf->ssid;
966 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
967 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700968 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 continue;
970 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
971 int_array_concat(&params.freqs,
972 tssid->scan_freq);
973 } else {
974 os_free(params.freqs);
975 params.freqs = NULL;
976 }
977 freqs_set = 1;
978 }
979 int_array_sort_unique(params.freqs);
980 }
981
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800982 if (ssid && max_ssids == 1) {
983 /*
984 * If the driver is limited to 1 SSID at a time interleave
985 * wildcard SSID scans with specific SSID scans to avoid
986 * waiting a long time for a wildcard scan.
987 */
988 if (!wpa_s->prev_scan_wildcard) {
989 params.ssids[0].ssid = NULL;
990 params.ssids[0].ssid_len = 0;
991 wpa_s->prev_scan_wildcard = 1;
992 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
993 "wildcard SSID (Interleave with specific)");
994 } else {
995 wpa_s->prev_scan_ssid = ssid;
996 wpa_s->prev_scan_wildcard = 0;
997 wpa_dbg(wpa_s, MSG_DEBUG,
998 "Starting AP scan for specific SSID: %s",
999 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001001 } else if (ssid) {
1002 /* max_ssids > 1 */
1003
1004 wpa_s->prev_scan_ssid = ssid;
1005 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1006 "the scan request");
1007 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001008 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1009 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1010 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001011 } else if (wpa_s->conf->passive_scan) {
1012 wpa_dbg(wpa_s, MSG_DEBUG,
1013 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 } else {
1015 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1016 params.num_ssids++;
1017 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1018 "SSID");
1019 }
1020
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001021ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001022 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001023 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001025 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001026 wpa_s->manual_scan_only_new) {
1027 wpa_printf(MSG_DEBUG,
1028 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001029 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001030 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001031
1032 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1033 wpa_s->manual_scan_freqs) {
1034 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1035 params.freqs = wpa_s->manual_scan_freqs;
1036 wpa_s->manual_scan_freqs = NULL;
1037 }
1038
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001039 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1040 wpa_dbg(wpa_s, MSG_DEBUG,
1041 "Limit select_network scan to specified channels");
1042 params.freqs = wpa_s->select_network_scan_freqs;
1043 wpa_s->select_network_scan_freqs = NULL;
1044 }
1045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1047 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1048 "generated frequency list");
1049 params.freqs = wpa_s->next_scan_freqs;
1050 } else
1051 os_free(wpa_s->next_scan_freqs);
1052 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001053 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001055 /* See if user specified frequencies. If so, scan only those. */
1056 if (wpa_s->conf->freq_list && !params.freqs) {
1057 wpa_dbg(wpa_s, MSG_DEBUG,
1058 "Optimize scan based on conf->freq_list");
1059 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1060 }
1061
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001062 /* Use current associated channel? */
1063 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001064 unsigned int num = wpa_s->num_multichan_concurrent;
1065
1066 params.freqs = os_calloc(num + 1, sizeof(int));
1067 if (params.freqs) {
1068 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1069 if (num > 0) {
1070 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1071 "current operating channels since "
1072 "scan_cur_freq is enabled");
1073 } else {
1074 os_free(params.freqs);
1075 params.freqs = NULL;
1076 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001077 }
1078 }
1079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1081 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082 if (extra_ie) {
1083 params.extra_ies = wpabuf_head(extra_ie);
1084 params.extra_ies_len = wpabuf_len(extra_ie);
1085 }
1086
1087#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001088 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001089 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001090 /*
1091 * The interface may not yet be in P2P mode, so we have to
1092 * explicitly request P2P probe to disable CCK rates.
1093 */
1094 params.p2p_probe = 1;
1095 }
1096#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001098 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
1099 wpa_s->wpa_state <= WPA_SCANNING) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001100 params.mac_addr_rand = 1;
1101 if (wpa_s->mac_addr_scan) {
1102 params.mac_addr = wpa_s->mac_addr_scan;
1103 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
1104 }
1105 }
1106
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001107 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1108 struct wpa_bss *bss;
1109
1110 params.bssid = wpa_s->next_scan_bssid;
1111 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1112 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1113 params.ssids[0].ssid_len == 0) {
1114 params.ssids[0].ssid = bss->ssid;
1115 params.ssids[0].ssid_len = bss->ssid_len;
1116 wpa_dbg(wpa_s, MSG_DEBUG,
1117 "Scan a previously specified BSSID " MACSTR
1118 " and SSID %s",
1119 MAC2STR(params.bssid),
1120 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1121 } else {
1122 wpa_dbg(wpa_s, MSG_DEBUG,
1123 "Scan a previously specified BSSID " MACSTR,
1124 MAC2STR(params.bssid));
1125 }
1126 }
1127
Dmitry Shmidt04949592012-07-19 12:16:46 -07001128 scan_params = &params;
1129
1130scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001131#ifdef CONFIG_P2P
1132 /*
1133 * If the driver does not support multi-channel concurrency and a
1134 * virtual interface that shares the same radio with the wpa_s interface
1135 * is operating there may not be need to scan other channels apart from
1136 * the current operating channel on the other virtual interface. Filter
1137 * out other channels in case we are trying to find a connection for a
1138 * station interface when we are not configured to prefer station
1139 * connection and a concurrent operation is already in process.
1140 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001141 if (wpa_s->scan_for_connection &&
1142 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001143 !scan_params->freqs && !params.freqs &&
1144 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001145 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1146 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001147 unsigned int num = wpa_s->num_multichan_concurrent;
1148
1149 params.freqs = os_calloc(num + 1, sizeof(int));
1150 if (params.freqs) {
1151 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1152 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1153 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1154 } else {
1155 os_free(params.freqs);
1156 params.freqs = NULL;
1157 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001158 }
1159 }
1160#endif /* CONFIG_P2P */
1161
Dmitry Shmidt04949592012-07-19 12:16:46 -07001162 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001164 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1165 !wpa_s->manual_scan_freqs) {
1166 /* Restore manual_scan_freqs for the next attempt */
1167 wpa_s->manual_scan_freqs = params.freqs;
1168 params.freqs = NULL;
1169 }
1170
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001171 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 os_free(params.freqs);
1173 os_free(params.filter_ssids);
1174
1175 if (ret) {
1176 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001177 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1178 wpa_supplicant_set_state(wpa_s,
1179 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001180 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001181 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001183 } else {
1184 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001185#ifdef CONFIG_INTERWORKING
1186 wpa_s->interworking_fast_assoc_tried = 0;
1187#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001188 if (params.bssid)
1189 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 }
1191}
1192
1193
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001194void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1195{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001196 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001197 int cancelled;
1198
1199 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1200 &remaining);
1201
1202 new_int.sec = sec;
1203 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001204 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001205 new_int.sec = remaining.sec;
1206 new_int.usec = remaining.usec;
1207 }
1208
Dmitry Shmidt051af732013-10-22 13:52:46 -07001209 if (cancelled) {
1210 eloop_register_timeout(new_int.sec, new_int.usec,
1211 wpa_supplicant_scan, wpa_s, NULL);
1212 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001213 wpa_s->scan_interval = sec;
1214}
1215
1216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217/**
1218 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1219 * @wpa_s: Pointer to wpa_supplicant data
1220 * @sec: Number of seconds after which to scan
1221 * @usec: Number of microseconds after which to scan
1222 *
1223 * This function is used to schedule a scan for neighboring access points after
1224 * the specified time.
1225 */
1226void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1227{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001228 int res;
1229
1230 if (wpa_s->p2p_mgmt) {
1231 wpa_dbg(wpa_s, MSG_DEBUG,
1232 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1233 sec, usec);
1234 return;
1235 }
1236
1237 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1238 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001239 if (res == 1) {
1240 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001241 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001242 } else if (res == 0) {
1243 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1244 sec, usec);
1245 } else {
1246 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1247 sec, usec);
1248 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250}
1251
1252
1253/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1255 * @wpa_s: Pointer to wpa_supplicant data
1256 * @sec: Number of seconds after which to scan
1257 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001258 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001259 *
1260 * This function is used to schedule periodic scans for neighboring
1261 * access points after the specified time.
1262 */
1263int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1264 int sec, int usec)
1265{
1266 if (!wpa_s->sched_scan_supported)
1267 return -1;
1268
1269 eloop_register_timeout(sec, usec,
1270 wpa_supplicant_delayed_sched_scan_timeout,
1271 wpa_s, NULL);
1272
1273 return 0;
1274}
1275
1276
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001277static void
1278wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1279 struct wpa_driver_scan_params *params)
1280{
1281 if (wpa_s->wpa_state != WPA_COMPLETED ||
1282 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1283 wpa_s->srp.relative_rssi_set == 0)
1284 return;
1285
1286 params->relative_rssi_set = 1;
1287 params->relative_rssi = wpa_s->srp.relative_rssi;
1288
1289 if (wpa_s->srp.relative_adjust_rssi == 0)
1290 return;
1291
1292 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1293 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1294}
1295
1296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001297/**
1298 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1299 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001300 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001301 *
1302 * This function is used to schedule periodic scans for neighboring
1303 * access points repeating the scan continuously.
1304 */
1305int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1306{
1307 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001308 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001309 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001310 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001311 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001312 int ret;
1313 unsigned int max_sched_scan_ssids;
1314 int wildcard = 0;
1315 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001316 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001317
1318 if (!wpa_s->sched_scan_supported)
1319 return -1;
1320
1321 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1322 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1323 else
1324 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001325 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001326 return -1;
1327
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001328 wpa_s->sched_scan_stop_req = 0;
1329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001330 if (wpa_s->sched_scanning) {
1331 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1332 return 0;
1333 }
1334
1335 need_ssids = 0;
1336 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001337 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001338 /* Use wildcard SSID to find this network */
1339 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001340 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1341 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001342 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001343
1344#ifdef CONFIG_WPS
1345 if (!wpas_network_disabled(wpa_s, ssid) &&
1346 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1347 /*
1348 * Normal scan is more reliable and faster for WPS
1349 * operations and since these are for short periods of
1350 * time, the benefit of trying to use sched_scan would
1351 * be limited.
1352 */
1353 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1354 "sched_scan for WPS");
1355 return -1;
1356 }
1357#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001358 }
1359 if (wildcard)
1360 need_ssids++;
1361
1362 if (wpa_s->normal_scans < 3 &&
1363 (need_ssids <= wpa_s->max_scan_ssids ||
1364 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1365 /*
1366 * When normal scan can speed up operations, use that for the
1367 * first operations before starting the sched_scan to allow
1368 * user space sleep more. We do this only if the normal scan
1369 * has functionality that is suitable for this or if the
1370 * sched_scan does not have better support for multiple SSIDs.
1371 */
1372 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1373 "sched_scan for initial scans (normal_scans=%d)",
1374 wpa_s->normal_scans);
1375 return -1;
1376 }
1377
1378 os_memset(&params, 0, sizeof(params));
1379
1380 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001381 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001382 sizeof(struct wpa_driver_scan_filter));
1383
1384 prev_state = wpa_s->wpa_state;
1385 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1386 wpa_s->wpa_state == WPA_INACTIVE)
1387 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1388
Dmitry Shmidt04949592012-07-19 12:16:46 -07001389 if (wpa_s->autoscan_params != NULL) {
1390 scan_params = wpa_s->autoscan_params;
1391 goto scan;
1392 }
1393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001394 /* Find the starting point from which to continue scanning */
1395 ssid = wpa_s->conf->ssid;
1396 if (wpa_s->prev_sched_ssid) {
1397 while (ssid) {
1398 if (ssid == wpa_s->prev_sched_ssid) {
1399 ssid = ssid->next;
1400 break;
1401 }
1402 ssid = ssid->next;
1403 }
1404 }
1405
1406 if (!ssid || !wpa_s->prev_sched_ssid) {
1407 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001408 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1409 wpa_s->first_sched_scan = 1;
1410 ssid = wpa_s->conf->ssid;
1411 wpa_s->prev_sched_ssid = ssid;
1412 }
1413
1414 if (wildcard) {
1415 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1416 params.num_ssids++;
1417 }
1418
1419 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001420 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001421 goto next;
1422
1423 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1424 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1425 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1426 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1427 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1428 ssid->ssid, ssid->ssid_len);
1429 params.filter_ssids[params.num_filter_ssids].ssid_len =
1430 ssid->ssid_len;
1431 params.num_filter_ssids++;
1432 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1433 {
1434 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1435 "filter for sched_scan - drop filter");
1436 os_free(params.filter_ssids);
1437 params.filter_ssids = NULL;
1438 params.num_filter_ssids = 0;
1439 }
1440
1441 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1442 if (params.num_ssids == max_sched_scan_ssids)
1443 break; /* only room for broadcast SSID */
1444 wpa_dbg(wpa_s, MSG_DEBUG,
1445 "add to active scan ssid: %s",
1446 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1447 params.ssids[params.num_ssids].ssid =
1448 ssid->ssid;
1449 params.ssids[params.num_ssids].ssid_len =
1450 ssid->ssid_len;
1451 params.num_ssids++;
1452 if (params.num_ssids >= max_sched_scan_ssids) {
1453 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001454 do {
1455 ssid = ssid->next;
1456 } while (ssid &&
1457 (wpas_network_disabled(wpa_s, ssid) ||
1458 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001459 break;
1460 }
1461 }
1462
1463 next:
1464 wpa_s->prev_sched_ssid = ssid;
1465 ssid = ssid->next;
1466 }
1467
1468 if (params.num_filter_ssids == 0) {
1469 os_free(params.filter_ssids);
1470 params.filter_ssids = NULL;
1471 }
1472
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001473 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1474 if (extra_ie) {
1475 params.extra_ies = wpabuf_head(extra_ie);
1476 params.extra_ies_len = wpabuf_len(extra_ie);
1477 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001478
Dmitry Shmidt18463232014-01-24 12:29:41 -08001479 if (wpa_s->conf->filter_rssi)
1480 params.filter_rssi = wpa_s->conf->filter_rssi;
1481
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001482 /* See if user specified frequencies. If so, scan only those. */
1483 if (wpa_s->conf->freq_list && !params.freqs) {
1484 wpa_dbg(wpa_s, MSG_DEBUG,
1485 "Optimize scan based on conf->freq_list");
1486 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1487 }
1488
Dmitry Shmidt04949592012-07-19 12:16:46 -07001489 scan_params = &params;
1490
1491scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001492 wpa_s->sched_scan_timed_out = 0;
1493
1494 /*
1495 * We cannot support multiple scan plans if the scan request includes
1496 * too many SSID's, so in this case use only the last scan plan and make
1497 * it run infinitely. It will be stopped by the timeout.
1498 */
1499 if (wpa_s->sched_scan_plans_num == 1 ||
1500 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1501 params.sched_scan_plans = wpa_s->sched_scan_plans;
1502 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1503 } else if (wpa_s->sched_scan_plans_num > 1) {
1504 wpa_dbg(wpa_s, MSG_DEBUG,
1505 "Too many SSIDs. Default to using single scheduled_scan plan");
1506 params.sched_scan_plans =
1507 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1508 1];
1509 params.sched_scan_plans_num = 1;
1510 } else {
1511 if (wpa_s->conf->sched_scan_interval)
1512 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1513 else
1514 scan_plan.interval = 10;
1515
1516 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1517 wpa_printf(MSG_WARNING,
1518 "Scan interval too long(%u), use the maximum allowed(%u)",
1519 scan_plan.interval,
1520 wpa_s->max_sched_scan_plan_interval);
1521 scan_plan.interval =
1522 wpa_s->max_sched_scan_plan_interval;
1523 }
1524
1525 scan_plan.iterations = 0;
1526 params.sched_scan_plans = &scan_plan;
1527 params.sched_scan_plans_num = 1;
1528 }
1529
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001530 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1531
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001532 if (ssid || !wpa_s->first_sched_scan) {
1533 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001534 "Starting sched scan after %u seconds: interval %u timeout %d",
1535 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001536 params.sched_scan_plans[0].interval,
1537 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001538 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001539 wpa_dbg(wpa_s, MSG_DEBUG,
1540 "Starting sched scan after %u seconds (no timeout)",
1541 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001542 }
1543
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001544 wpa_setband_scan_freqs(wpa_s, scan_params);
1545
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001546 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
1547 wpa_s->wpa_state <= WPA_SCANNING) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001548 params.mac_addr_rand = 1;
1549 if (wpa_s->mac_addr_sched_scan) {
1550 params.mac_addr = wpa_s->mac_addr_sched_scan;
1551 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1552 ETH_ALEN;
1553 }
1554 }
1555
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001556 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1557
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001558 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001559 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001560 os_free(params.filter_ssids);
1561 if (ret) {
1562 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1563 if (prev_state != wpa_s->wpa_state)
1564 wpa_supplicant_set_state(wpa_s, prev_state);
1565 return ret;
1566 }
1567
1568 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1569 if (ssid || !wpa_s->first_sched_scan) {
1570 wpa_s->sched_scan_timed_out = 0;
1571 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1572 wpa_supplicant_sched_scan_timeout,
1573 wpa_s, NULL);
1574 wpa_s->first_sched_scan = 0;
1575 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001576 params.sched_scan_plans[0].interval *= 2;
1577 if ((unsigned int) wpa_s->sched_scan_timeout <
1578 params.sched_scan_plans[0].interval ||
1579 params.sched_scan_plans[0].interval >
1580 wpa_s->max_sched_scan_plan_interval) {
1581 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001582 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1583 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 }
1585
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001586 /* If there is no more ssids, start next time from the beginning */
1587 if (!ssid)
1588 wpa_s->prev_sched_ssid = NULL;
1589
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001590 return 0;
1591}
1592
1593
1594/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1596 * @wpa_s: Pointer to wpa_supplicant data
1597 *
1598 * This function is used to cancel a scan request scheduled with
1599 * wpa_supplicant_req_scan().
1600 */
1601void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1602{
1603 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1604 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605}
1606
1607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001609 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1610 * @wpa_s: Pointer to wpa_supplicant data
1611 *
1612 * This function is used to stop a delayed scheduled scan.
1613 */
1614void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1615{
1616 if (!wpa_s->sched_scan_supported)
1617 return;
1618
1619 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1620 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1621 wpa_s, NULL);
1622}
1623
1624
1625/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001626 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1627 * @wpa_s: Pointer to wpa_supplicant data
1628 *
1629 * This function is used to stop a periodic scheduled scan.
1630 */
1631void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1632{
1633 if (!wpa_s->sched_scanning)
1634 return;
1635
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001636 if (wpa_s->sched_scanning)
1637 wpa_s->sched_scan_stop_req = 1;
1638
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001639 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1640 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1641 wpa_supplicant_stop_sched_scan(wpa_s);
1642}
1643
1644
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001645/**
1646 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1647 * @wpa_s: Pointer to wpa_supplicant data
1648 * @scanning: Whether scanning is currently in progress
1649 *
1650 * This function is to generate scanning notifycations. It is called whenever
1651 * there may have been a change in scanning (scan started, completed, stopped).
1652 * wpas_notify_scanning() is called whenever the scanning state changed from the
1653 * previously notified state.
1654 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1656 int scanning)
1657{
1658 if (wpa_s->scanning != scanning) {
1659 wpa_s->scanning = scanning;
1660 wpas_notify_scanning(wpa_s);
1661 }
1662}
1663
1664
1665static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1666{
1667 int rate = 0;
1668 const u8 *ie;
1669 int i;
1670
1671 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1672 for (i = 0; ie && i < ie[1]; i++) {
1673 if ((ie[i + 2] & 0x7f) > rate)
1674 rate = ie[i + 2] & 0x7f;
1675 }
1676
1677 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1678 for (i = 0; ie && i < ie[1]; i++) {
1679 if ((ie[i + 2] & 0x7f) > rate)
1680 rate = ie[i + 2] & 0x7f;
1681 }
1682
1683 return rate;
1684}
1685
1686
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001687/**
1688 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1689 * @res: Scan result entry
1690 * @ie: Information element identitifier (WLAN_EID_*)
1691 * Returns: Pointer to the information element (id field) or %NULL if not found
1692 *
1693 * This function returns the first matching information element in the scan
1694 * result.
1695 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1697{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001698 size_t ie_len = res->ie_len;
1699
1700 /* Use the Beacon frame IEs if res->ie_len is not available */
1701 if (!ie_len)
1702 ie_len = res->beacon_ie_len;
1703
1704 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705}
1706
1707
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001708/**
1709 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1710 * @res: Scan result entry
1711 * @vendor_type: Vendor type (four octets starting the IE payload)
1712 * Returns: Pointer to the information element (id field) or %NULL if not found
1713 *
1714 * This function returns the first matching information element in the scan
1715 * result.
1716 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1718 u32 vendor_type)
1719{
1720 const u8 *end, *pos;
1721
1722 pos = (const u8 *) (res + 1);
1723 end = pos + res->ie_len;
1724
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001725 while (end - pos > 1) {
1726 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 break;
1728 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1729 vendor_type == WPA_GET_BE32(&pos[2]))
1730 return pos;
1731 pos += 2 + pos[1];
1732 }
1733
1734 return NULL;
1735}
1736
1737
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001738/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001739 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1740 * @res: Scan result entry
1741 * @vendor_type: Vendor type (four octets starting the IE payload)
1742 * Returns: Pointer to the information element (id field) or %NULL if not found
1743 *
1744 * This function returns the first matching information element in the scan
1745 * result.
1746 *
1747 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1748 * from Beacon frames instead of either Beacon or Probe Response frames.
1749 */
1750const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1751 u32 vendor_type)
1752{
1753 const u8 *end, *pos;
1754
1755 if (res->beacon_ie_len == 0)
1756 return NULL;
1757
1758 pos = (const u8 *) (res + 1);
1759 pos += res->ie_len;
1760 end = pos + res->beacon_ie_len;
1761
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001762 while (end - pos > 1) {
1763 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001764 break;
1765 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1766 vendor_type == WPA_GET_BE32(&pos[2]))
1767 return pos;
1768 pos += 2 + pos[1];
1769 }
1770
1771 return NULL;
1772}
1773
1774
1775/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001776 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1777 * @res: Scan result entry
1778 * @vendor_type: Vendor type (four octets starting the IE payload)
1779 * Returns: Pointer to the information element payload or %NULL if not found
1780 *
1781 * This function returns concatenated payload of possibly fragmented vendor
1782 * specific information elements in the scan result. The caller is responsible
1783 * for freeing the returned buffer.
1784 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1786 u32 vendor_type)
1787{
1788 struct wpabuf *buf;
1789 const u8 *end, *pos;
1790
1791 buf = wpabuf_alloc(res->ie_len);
1792 if (buf == NULL)
1793 return NULL;
1794
1795 pos = (const u8 *) (res + 1);
1796 end = pos + res->ie_len;
1797
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001798 while (end - pos > 1) {
1799 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 break;
1801 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1802 vendor_type == WPA_GET_BE32(&pos[2]))
1803 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1804 pos += 2 + pos[1];
1805 }
1806
1807 if (wpabuf_len(buf) == 0) {
1808 wpabuf_free(buf);
1809 buf = NULL;
1810 }
1811
1812 return buf;
1813}
1814
1815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001816/*
1817 * Channels with a great SNR can operate at full rate. What is a great SNR?
1818 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1819 * rule of thumb is that any SNR above 20 is good." This one
1820 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001821 * recommends 25 as a minimum SNR for 54 Mbps data rate. The estimates used in
1822 * scan_est_throughput() allow even smaller SNR values for the maximum rates
1823 * (21 for 54 Mbps, 22 for VHT80 MCS9, 24 for HT40 and HT20 MCS7). Use 25 as a
1824 * somewhat conservative value here.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001825 */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001826#define GREAT_SNR 25
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001827
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001828#define IS_5GHZ(n) (n > 4000)
1829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830/* Compare function for sorting scan results. Return >0 if @b is considered
1831 * better. */
1832static int wpa_scan_result_compar(const void *a, const void *b)
1833{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001834#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 struct wpa_scan_res **_wa = (void *) a;
1836 struct wpa_scan_res **_wb = (void *) b;
1837 struct wpa_scan_res *wa = *_wa;
1838 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001839 int wpa_a, wpa_b;
1840 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841
1842 /* WPA/WPA2 support preferred */
1843 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1844 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1845 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1846 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1847
1848 if (wpa_b && !wpa_a)
1849 return 1;
1850 if (!wpa_b && wpa_a)
1851 return -1;
1852
1853 /* privacy support preferred */
1854 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1855 (wb->caps & IEEE80211_CAP_PRIVACY))
1856 return 1;
1857 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1858 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1859 return -1;
1860
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001861 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001862 snr_a_full = wa->snr;
1863 snr_a = MIN(wa->snr, GREAT_SNR);
1864 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001865 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001867 /* Level is not in dBm, so we can't calculate
1868 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001869 snr_a = snr_a_full = wa->level;
1870 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001871 }
1872
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001873 /* if SNR is close, decide by max rate or frequency band */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001874 if (snr_a && snr_b && abs(snr_b - snr_a) < 7) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001875 if (wa->est_throughput != wb->est_throughput)
1876 return wb->est_throughput - wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001877 }
1878 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1879 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001880 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1881 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 }
1883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001884 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 * identical, use quality values since some drivers may only report
1886 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001887 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001889 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001890#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891}
1892
1893
1894#ifdef CONFIG_WPS
1895/* Compare function for sorting scan results when searching a WPS AP for
1896 * provisioning. Return >0 if @b is considered better. */
1897static int wpa_scan_result_wps_compar(const void *a, const void *b)
1898{
1899 struct wpa_scan_res **_wa = (void *) a;
1900 struct wpa_scan_res **_wb = (void *) b;
1901 struct wpa_scan_res *wa = *_wa;
1902 struct wpa_scan_res *wb = *_wb;
1903 int uses_wps_a, uses_wps_b;
1904 struct wpabuf *wps_a, *wps_b;
1905 int res;
1906
1907 /* Optimization - check WPS IE existence before allocated memory and
1908 * doing full reassembly. */
1909 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1910 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1911 if (uses_wps_a && !uses_wps_b)
1912 return -1;
1913 if (!uses_wps_a && uses_wps_b)
1914 return 1;
1915
1916 if (uses_wps_a && uses_wps_b) {
1917 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1918 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1919 res = wps_ap_priority_compar(wps_a, wps_b);
1920 wpabuf_free(wps_a);
1921 wpabuf_free(wps_b);
1922 if (res)
1923 return res;
1924 }
1925
1926 /*
1927 * Do not use current AP security policy as a sorting criteria during
1928 * WPS provisioning step since the AP may get reconfigured at the
1929 * completion of provisioning.
1930 */
1931
1932 /* all things being equal, use signal level; if signal levels are
1933 * identical, use quality values since some drivers may only report
1934 * that value and leave the signal level zero */
1935 if (wb->level == wa->level)
1936 return wb->qual - wa->qual;
1937 return wb->level - wa->level;
1938}
1939#endif /* CONFIG_WPS */
1940
1941
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001942static void dump_scan_res(struct wpa_scan_results *scan_res)
1943{
1944#ifndef CONFIG_NO_STDOUT_DEBUG
1945 size_t i;
1946
1947 if (scan_res->res == NULL || scan_res->num == 0)
1948 return;
1949
1950 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1951
1952 for (i = 0; i < scan_res->num; i++) {
1953 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001954 u8 *pos;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001955 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001956 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
1957
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001958 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001959 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001960 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001961 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001962 r->snr, r->snr >= GREAT_SNR ? "*" : "",
1963 r->flags,
1964 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001965 } else {
1966 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001967 "noise=%d level=%d flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001968 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001969 r->noise, r->level, r->flags, r->age,
1970 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001971 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001972 pos = (u8 *) (r + 1);
1973 if (r->ie_len)
1974 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1975 pos += r->ie_len;
1976 if (r->beacon_ie_len)
1977 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1978 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001979 }
1980#endif /* CONFIG_NO_STDOUT_DEBUG */
1981}
1982
1983
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001984/**
1985 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1986 * @wpa_s: Pointer to wpa_supplicant data
1987 * @bssid: BSSID to check
1988 * Returns: 0 if the BSSID is filtered or 1 if not
1989 *
1990 * This function is used to filter out specific BSSIDs from scan reslts mainly
1991 * for testing purposes (SET bssid_filter ctrl_iface command).
1992 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001993int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1994 const u8 *bssid)
1995{
1996 size_t i;
1997
1998 if (wpa_s->bssid_filter == NULL)
1999 return 1;
2000
2001 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2002 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2003 ETH_ALEN) == 0)
2004 return 1;
2005 }
2006
2007 return 0;
2008}
2009
2010
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002011void filter_scan_res(struct wpa_supplicant *wpa_s,
2012 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002013{
2014 size_t i, j;
2015
2016 if (wpa_s->bssid_filter == NULL)
2017 return;
2018
2019 for (i = 0, j = 0; i < res->num; i++) {
2020 if (wpa_supplicant_filter_bssid_match(wpa_s,
2021 res->res[i]->bssid)) {
2022 res->res[j++] = res->res[i];
2023 } else {
2024 os_free(res->res[i]);
2025 res->res[i] = NULL;
2026 }
2027 }
2028
2029 if (res->num != j) {
2030 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2031 (int) (res->num - j));
2032 res->num = j;
2033 }
2034}
2035
2036
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002037/*
2038 * Noise floor values to use when we have signal strength
Dmitry Shmidte4663042016-04-04 10:07:49 -07002039 * measurements, but no noise floor measurements. These values were
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002040 * measured in an office environment with many APs.
2041 */
2042#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
2043#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
2044
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002045void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002046{
2047 if (res->flags & WPA_SCAN_NOISE_INVALID) {
2048 res->noise = IS_5GHZ(res->freq) ?
2049 DEFAULT_NOISE_FLOOR_5GHZ :
2050 DEFAULT_NOISE_FLOOR_2GHZ;
2051 }
2052
2053 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2054 res->snr = res->level - res->noise;
2055 } else {
2056 /* Level is not in dBm, so we can't calculate
2057 * SNR. Just use raw level (units unknown). */
2058 res->snr = res->level;
2059 }
2060}
2061
2062
2063static unsigned int max_ht20_rate(int snr)
2064{
2065 if (snr < 6)
2066 return 6500; /* HT20 MCS0 */
2067 if (snr < 8)
2068 return 13000; /* HT20 MCS1 */
2069 if (snr < 13)
2070 return 19500; /* HT20 MCS2 */
2071 if (snr < 17)
2072 return 26000; /* HT20 MCS3 */
2073 if (snr < 20)
2074 return 39000; /* HT20 MCS4 */
2075 if (snr < 23)
2076 return 52000; /* HT20 MCS5 */
2077 if (snr < 24)
2078 return 58500; /* HT20 MCS6 */
2079 return 65000; /* HT20 MCS7 */
2080}
2081
2082
2083static unsigned int max_ht40_rate(int snr)
2084{
2085 if (snr < 3)
2086 return 13500; /* HT40 MCS0 */
2087 if (snr < 6)
2088 return 27000; /* HT40 MCS1 */
2089 if (snr < 10)
2090 return 40500; /* HT40 MCS2 */
2091 if (snr < 15)
2092 return 54000; /* HT40 MCS3 */
2093 if (snr < 17)
2094 return 81000; /* HT40 MCS4 */
2095 if (snr < 22)
2096 return 108000; /* HT40 MCS5 */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002097 if (snr < 24)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002098 return 121500; /* HT40 MCS6 */
2099 return 135000; /* HT40 MCS7 */
2100}
2101
2102
2103static unsigned int max_vht80_rate(int snr)
2104{
2105 if (snr < 1)
2106 return 0;
2107 if (snr < 2)
2108 return 29300; /* VHT80 MCS0 */
2109 if (snr < 5)
2110 return 58500; /* VHT80 MCS1 */
2111 if (snr < 9)
2112 return 87800; /* VHT80 MCS2 */
2113 if (snr < 11)
2114 return 117000; /* VHT80 MCS3 */
2115 if (snr < 15)
2116 return 175500; /* VHT80 MCS4 */
2117 if (snr < 16)
2118 return 234000; /* VHT80 MCS5 */
2119 if (snr < 18)
2120 return 263300; /* VHT80 MCS6 */
2121 if (snr < 20)
2122 return 292500; /* VHT80 MCS7 */
2123 if (snr < 22)
2124 return 351000; /* VHT80 MCS8 */
2125 return 390000; /* VHT80 MCS9 */
2126}
2127
2128
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002129void scan_est_throughput(struct wpa_supplicant *wpa_s,
2130 struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002131{
2132 enum local_hw_capab capab = wpa_s->hw_capab;
2133 int rate; /* max legacy rate in 500 kb/s units */
2134 const u8 *ie;
2135 unsigned int est, tmp;
2136 int snr = res->snr;
2137
2138 if (res->est_throughput)
2139 return;
2140
2141 /* Get maximum legacy rate */
2142 rate = wpa_scan_get_max_rate(res);
2143
2144 /* Limit based on estimated SNR */
2145 if (rate > 1 * 2 && snr < 1)
2146 rate = 1 * 2;
2147 else if (rate > 2 * 2 && snr < 4)
2148 rate = 2 * 2;
2149 else if (rate > 6 * 2 && snr < 5)
2150 rate = 6 * 2;
2151 else if (rate > 9 * 2 && snr < 6)
2152 rate = 9 * 2;
2153 else if (rate > 12 * 2 && snr < 7)
2154 rate = 12 * 2;
2155 else if (rate > 18 * 2 && snr < 10)
2156 rate = 18 * 2;
2157 else if (rate > 24 * 2 && snr < 11)
2158 rate = 24 * 2;
2159 else if (rate > 36 * 2 && snr < 15)
2160 rate = 36 * 2;
2161 else if (rate > 48 * 2 && snr < 19)
2162 rate = 48 * 2;
2163 else if (rate > 54 * 2 && snr < 21)
2164 rate = 54 * 2;
2165 est = rate * 500;
2166
2167 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2168 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2169 if (ie) {
2170 tmp = max_ht20_rate(snr);
2171 if (tmp > est)
2172 est = tmp;
2173 }
2174 }
2175
2176 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2177 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2178 if (ie && ie[1] >= 2 &&
2179 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2180 tmp = max_ht40_rate(snr);
2181 if (tmp > est)
2182 est = tmp;
2183 }
2184 }
2185
2186 if (capab == CAPAB_VHT) {
2187 /* Use +1 to assume VHT is always faster than HT */
2188 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2189 if (ie) {
2190 tmp = max_ht20_rate(snr) + 1;
2191 if (tmp > est)
2192 est = tmp;
2193
2194 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2195 if (ie && ie[1] >= 2 &&
2196 (ie[3] &
2197 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2198 tmp = max_ht40_rate(snr) + 1;
2199 if (tmp > est)
2200 est = tmp;
2201 }
2202
2203 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2204 if (ie && ie[1] >= 1 &&
2205 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2206 tmp = max_vht80_rate(snr) + 1;
2207 if (tmp > est)
2208 est = tmp;
2209 }
2210 }
2211 }
2212
2213 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2214
2215 res->est_throughput = est;
2216}
2217
2218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219/**
2220 * wpa_supplicant_get_scan_results - Get scan results
2221 * @wpa_s: Pointer to wpa_supplicant data
2222 * @info: Information about what was scanned or %NULL if not available
2223 * @new_scan: Whether a new scan was performed
2224 * Returns: Scan results, %NULL on failure
2225 *
2226 * This function request the current scan results from the driver and updates
2227 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2228 * results with wpa_scan_results_free().
2229 */
2230struct wpa_scan_results *
2231wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2232 struct scan_info *info, int new_scan)
2233{
2234 struct wpa_scan_results *scan_res;
2235 size_t i;
2236 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2237
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002238 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 if (scan_res == NULL) {
2240 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2241 return NULL;
2242 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002243 if (scan_res->fetch_time.sec == 0) {
2244 /*
2245 * Make sure we have a valid timestamp if the driver wrapper
2246 * does not set this.
2247 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002248 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002249 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002250 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002252 for (i = 0; i < scan_res->num; i++) {
2253 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2254
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002255 scan_snr(scan_res_item);
2256 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002257 }
2258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002260 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2262 "provisioning rules");
2263 compar = wpa_scan_result_wps_compar;
2264 }
2265#endif /* CONFIG_WPS */
2266
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002267 if (scan_res->res) {
2268 qsort(scan_res->res, scan_res->num,
2269 sizeof(struct wpa_scan_res *), compar);
2270 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002271 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272
Dmitry Shmidt29333592017-01-09 12:27:11 -08002273 if (wpa_s->ignore_post_flush_scan_res) {
2274 /* FLUSH command aborted an ongoing scan and these are the
2275 * results from the aborted scan. Do not process the results to
2276 * maintain flushed state. */
2277 wpa_dbg(wpa_s, MSG_DEBUG,
2278 "Do not update BSS table based on pending post-FLUSH scan results");
2279 wpa_s->ignore_post_flush_scan_res = 0;
2280 return scan_res;
2281 }
2282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 wpa_bss_update_start(wpa_s);
2284 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002285 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2286 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287 wpa_bss_update_end(wpa_s, info, new_scan);
2288
2289 return scan_res;
2290}
2291
2292
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002293/**
2294 * wpa_supplicant_update_scan_results - Update scan results from the driver
2295 * @wpa_s: Pointer to wpa_supplicant data
2296 * Returns: 0 on success, -1 on failure
2297 *
2298 * This function updates the BSS table within wpa_supplicant based on the
2299 * currently available scan results from the driver without requesting a new
2300 * scan. This is used in cases where the driver indicates an association
2301 * (including roaming within ESS) and wpa_supplicant does not yet have the
2302 * needed information to complete the connection (e.g., to perform validation
2303 * steps in 4-way handshake).
2304 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2306{
2307 struct wpa_scan_results *scan_res;
2308 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2309 if (scan_res == NULL)
2310 return -1;
2311 wpa_scan_results_free(scan_res);
2312
2313 return 0;
2314}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002315
2316
2317/**
2318 * scan_only_handler - Reports scan results
2319 */
2320void scan_only_handler(struct wpa_supplicant *wpa_s,
2321 struct wpa_scan_results *scan_res)
2322{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002323 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002324 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2325 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2326 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2327 wpa_s->manual_scan_id);
2328 wpa_s->manual_scan_use_id = 0;
2329 } else {
2330 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2331 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002332 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002333 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002334 if (wpa_s->scan_work) {
2335 struct wpa_radio_work *work = wpa_s->scan_work;
2336 wpa_s->scan_work = NULL;
2337 radio_work_done(work);
2338 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002339
2340 if (wpa_s->wpa_state == WPA_SCANNING)
2341 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002342}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002343
2344
2345int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2346{
2347 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2348}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002349
2350
2351struct wpa_driver_scan_params *
2352wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2353{
2354 struct wpa_driver_scan_params *params;
2355 size_t i;
2356 u8 *n;
2357
2358 params = os_zalloc(sizeof(*params));
2359 if (params == NULL)
2360 return NULL;
2361
2362 for (i = 0; i < src->num_ssids; i++) {
2363 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002364 n = os_memdup(src->ssids[i].ssid,
2365 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002366 if (n == NULL)
2367 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002368 params->ssids[i].ssid = n;
2369 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2370 }
2371 }
2372 params->num_ssids = src->num_ssids;
2373
2374 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002375 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002376 if (n == NULL)
2377 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002378 params->extra_ies = n;
2379 params->extra_ies_len = src->extra_ies_len;
2380 }
2381
2382 if (src->freqs) {
2383 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002384 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002385 if (params->freqs == NULL)
2386 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002387 }
2388
2389 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002390 params->filter_ssids = os_memdup(src->filter_ssids,
2391 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002392 src->num_filter_ssids);
2393 if (params->filter_ssids == NULL)
2394 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002395 params->num_filter_ssids = src->num_filter_ssids;
2396 }
2397
2398 params->filter_rssi = src->filter_rssi;
2399 params->p2p_probe = src->p2p_probe;
2400 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002401 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002402 params->duration = src->duration;
2403 params->duration_mandatory = src->duration_mandatory;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002404
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002405 if (src->sched_scan_plans_num > 0) {
2406 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002407 os_memdup(src->sched_scan_plans,
2408 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002409 src->sched_scan_plans_num);
2410 if (!params->sched_scan_plans)
2411 goto failed;
2412
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002413 params->sched_scan_plans_num = src->sched_scan_plans_num;
2414 }
2415
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002416 if (src->mac_addr_rand) {
2417 params->mac_addr_rand = src->mac_addr_rand;
2418
2419 if (src->mac_addr && src->mac_addr_mask) {
2420 u8 *mac_addr;
2421
2422 mac_addr = os_malloc(2 * ETH_ALEN);
2423 if (!mac_addr)
2424 goto failed;
2425
2426 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
2427 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
2428 ETH_ALEN);
2429 params->mac_addr = mac_addr;
2430 params->mac_addr_mask = mac_addr + ETH_ALEN;
2431 }
2432 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002433
2434 if (src->bssid) {
2435 u8 *bssid;
2436
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002437 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002438 if (!bssid)
2439 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002440 params->bssid = bssid;
2441 }
2442
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002443 params->relative_rssi_set = src->relative_rssi_set;
2444 params->relative_rssi = src->relative_rssi;
2445 params->relative_adjust_band = src->relative_adjust_band;
2446 params->relative_adjust_rssi = src->relative_adjust_rssi;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002447 return params;
2448
2449failed:
2450 wpa_scan_free_params(params);
2451 return NULL;
2452}
2453
2454
2455void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2456{
2457 size_t i;
2458
2459 if (params == NULL)
2460 return;
2461
2462 for (i = 0; i < params->num_ssids; i++)
2463 os_free((u8 *) params->ssids[i].ssid);
2464 os_free((u8 *) params->extra_ies);
2465 os_free(params->freqs);
2466 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002467 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002468
2469 /*
2470 * Note: params->mac_addr_mask points to same memory allocation and
2471 * must not be freed separately.
2472 */
2473 os_free((u8 *) params->mac_addr);
2474
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002475 os_free((u8 *) params->bssid);
2476
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002477 os_free(params);
2478}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002479
2480
2481int wpas_start_pno(struct wpa_supplicant *wpa_s)
2482{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002483 int ret, prio;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002484 size_t i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002485 struct wpa_ssid *ssid;
2486 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002487 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002488 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002489
2490 if (!wpa_s->sched_scan_supported)
2491 return -1;
2492
Dmitry Shmidte4663042016-04-04 10:07:49 -07002493 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2494 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2495 else
2496 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2497 if (max_sched_scan_ssids < 1)
2498 return -1;
2499
Dmitry Shmidt98660862014-03-11 17:26:21 -07002500 if (wpa_s->pno || wpa_s->pno_sched_pending)
2501 return 0;
2502
2503 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002504 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002505 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2506 return -EAGAIN;
2507 }
2508
2509 if (wpa_s->wpa_state == WPA_SCANNING) {
2510 wpa_supplicant_cancel_scan(wpa_s);
2511 if (wpa_s->sched_scanning) {
2512 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2513 "ongoing sched scan");
2514 wpa_supplicant_cancel_sched_scan(wpa_s);
2515 wpa_s->pno_sched_pending = 1;
2516 return 0;
2517 }
2518 }
2519
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002520 if (wpa_s->sched_scan_stop_req) {
2521 wpa_printf(MSG_DEBUG,
2522 "Schedule PNO after previous sched scan has stopped");
2523 wpa_s->pno_sched_pending = 1;
2524 return 0;
2525 }
2526
Dmitry Shmidt98660862014-03-11 17:26:21 -07002527 os_memset(&params, 0, sizeof(params));
2528
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002529 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002530 ssid = wpa_s->conf->ssid;
2531 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002532 if (!wpas_network_disabled(wpa_s, ssid)) {
2533 num_match_ssid++;
2534 if (ssid->scan_ssid)
2535 num_ssid++;
2536 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002537 ssid = ssid->next;
2538 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002539
2540 if (num_match_ssid == 0) {
2541 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2542 return -1;
2543 }
2544
2545 if (num_match_ssid > num_ssid) {
2546 params.num_ssids++; /* wildcard */
2547 num_ssid++;
2548 }
2549
Dmitry Shmidte4663042016-04-04 10:07:49 -07002550 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002551 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07002552 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2553 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002554 }
2555
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002556 if (num_match_ssid > wpa_s->max_match_sets) {
2557 num_match_ssid = wpa_s->max_match_sets;
2558 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002559 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002560 params.filter_ssids = os_calloc(num_match_ssid,
2561 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002562 if (params.filter_ssids == NULL)
2563 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002564
Dmitry Shmidt98660862014-03-11 17:26:21 -07002565 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002566 prio = 0;
2567 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002568 while (ssid) {
2569 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002570 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2571 params.ssids[params.num_ssids].ssid =
2572 ssid->ssid;
2573 params.ssids[params.num_ssids].ssid_len =
2574 ssid->ssid_len;
2575 params.num_ssids++;
2576 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002577 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2578 ssid->ssid_len);
2579 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2580 params.num_filter_ssids++;
2581 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002582 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002583 break;
2584 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002585 if (ssid->pnext)
2586 ssid = ssid->pnext;
2587 else if (prio + 1 == wpa_s->conf->num_prio)
2588 break;
2589 else
2590 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002591 }
2592
2593 if (wpa_s->conf->filter_rssi)
2594 params.filter_rssi = wpa_s->conf->filter_rssi;
2595
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002596 if (wpa_s->sched_scan_plans_num) {
2597 params.sched_scan_plans = wpa_s->sched_scan_plans;
2598 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2599 } else {
2600 /* Set one scan plan that will run infinitely */
2601 if (wpa_s->conf->sched_scan_interval)
2602 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2603 else
2604 scan_plan.interval = 10;
2605
2606 scan_plan.iterations = 0;
2607 params.sched_scan_plans = &scan_plan;
2608 params.sched_scan_plans_num = 1;
2609 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002610
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002611 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
2612
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002613 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2614 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2615 params.freqs = wpa_s->manual_sched_scan_freqs;
2616 }
2617
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002618 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
2619 wpa_s->wpa_state <= WPA_SCANNING) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002620 params.mac_addr_rand = 1;
2621 if (wpa_s->mac_addr_pno) {
2622 params.mac_addr = wpa_s->mac_addr_pno;
2623 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2624 }
2625 }
2626
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002627 wpa_scan_set_relative_rssi_params(wpa_s, &params);
2628
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002629 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002630 os_free(params.filter_ssids);
2631 if (ret == 0)
2632 wpa_s->pno = 1;
2633 else
2634 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2635 return ret;
2636}
2637
2638
2639int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2640{
2641 int ret = 0;
2642
2643 if (!wpa_s->pno)
2644 return 0;
2645
2646 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002647 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002648
2649 wpa_s->pno = 0;
2650 wpa_s->pno_sched_pending = 0;
2651
2652 if (wpa_s->wpa_state == WPA_SCANNING)
2653 wpa_supplicant_req_scan(wpa_s, 0, 0);
2654
2655 return ret;
2656}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002657
2658
2659void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2660 unsigned int type)
2661{
2662 type &= MAC_ADDR_RAND_ALL;
2663 wpa_s->mac_addr_rand_enable &= ~type;
2664
2665 if (type & MAC_ADDR_RAND_SCAN) {
2666 os_free(wpa_s->mac_addr_scan);
2667 wpa_s->mac_addr_scan = NULL;
2668 }
2669
2670 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2671 os_free(wpa_s->mac_addr_sched_scan);
2672 wpa_s->mac_addr_sched_scan = NULL;
2673 }
2674
2675 if (type & MAC_ADDR_RAND_PNO) {
2676 os_free(wpa_s->mac_addr_pno);
2677 wpa_s->mac_addr_pno = NULL;
2678 }
2679}
2680
2681
2682int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2683 unsigned int type, const u8 *addr,
2684 const u8 *mask)
2685{
2686 u8 *tmp = NULL;
2687
2688 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2689
2690 if (addr) {
2691 tmp = os_malloc(2 * ETH_ALEN);
2692 if (!tmp)
2693 return -1;
2694 os_memcpy(tmp, addr, ETH_ALEN);
2695 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2696 }
2697
2698 if (type == MAC_ADDR_RAND_SCAN) {
2699 wpa_s->mac_addr_scan = tmp;
2700 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2701 wpa_s->mac_addr_sched_scan = tmp;
2702 } else if (type == MAC_ADDR_RAND_PNO) {
2703 wpa_s->mac_addr_pno = tmp;
2704 } else {
2705 wpa_printf(MSG_INFO,
2706 "scan: Invalid MAC randomization type=0x%x",
2707 type);
2708 os_free(tmp);
2709 return -1;
2710 }
2711
2712 wpa_s->mac_addr_rand_enable |= type;
2713 return 0;
2714}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002715
2716
2717int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2718{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002719 struct wpa_radio_work *work;
2720 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002721
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002722 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
2723 if (work->wpa_s != wpa_s || !work->started ||
2724 (os_strcmp(work->type, "scan") != 0 &&
2725 os_strcmp(work->type, "p2p-scan") != 0))
2726 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002727 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002728 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002729 }
2730
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002731 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
2732 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002733}
2734
2735
2736int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2737{
2738 struct sched_scan_plan *scan_plans = NULL;
2739 const char *token, *context = NULL;
2740 unsigned int num = 0;
2741
2742 if (!cmd)
2743 return -1;
2744
2745 if (!cmd[0]) {
2746 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2747 os_free(wpa_s->sched_scan_plans);
2748 wpa_s->sched_scan_plans = NULL;
2749 wpa_s->sched_scan_plans_num = 0;
2750 return 0;
2751 }
2752
2753 while ((token = cstr_token(cmd, " ", &context))) {
2754 int ret;
2755 struct sched_scan_plan *scan_plan, *n;
2756
2757 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2758 if (!n)
2759 goto fail;
2760
2761 scan_plans = n;
2762 scan_plan = &scan_plans[num];
2763 num++;
2764
2765 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2766 &scan_plan->iterations);
2767 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2768 wpa_printf(MSG_ERROR,
2769 "Invalid sched scan plan input: %s", token);
2770 goto fail;
2771 }
2772
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002773 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2774 wpa_printf(MSG_WARNING,
2775 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2776 num, scan_plan->interval,
2777 wpa_s->max_sched_scan_plan_interval);
2778 scan_plan->interval =
2779 wpa_s->max_sched_scan_plan_interval;
2780 }
2781
2782 if (ret == 1) {
2783 scan_plan->iterations = 0;
2784 break;
2785 }
2786
2787 if (!scan_plan->iterations) {
2788 wpa_printf(MSG_ERROR,
2789 "scan plan %u: Number of iterations cannot be zero",
2790 num);
2791 goto fail;
2792 }
2793
2794 if (scan_plan->iterations >
2795 wpa_s->max_sched_scan_plan_iterations) {
2796 wpa_printf(MSG_WARNING,
2797 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2798 num, scan_plan->iterations,
2799 wpa_s->max_sched_scan_plan_iterations);
2800 scan_plan->iterations =
2801 wpa_s->max_sched_scan_plan_iterations;
2802 }
2803
2804 wpa_printf(MSG_DEBUG,
2805 "scan plan %u: interval=%u iterations=%u",
2806 num, scan_plan->interval, scan_plan->iterations);
2807 }
2808
2809 if (!scan_plans) {
2810 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2811 goto fail;
2812 }
2813
2814 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2815 wpa_printf(MSG_ERROR,
2816 "All scan plans but the last must specify a number of iterations");
2817 goto fail;
2818 }
2819
2820 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2821 num, scan_plans[num - 1].interval);
2822
2823 if (num > wpa_s->max_sched_scan_plans) {
2824 wpa_printf(MSG_WARNING,
2825 "Too many scheduled scan plans (only %u supported)",
2826 wpa_s->max_sched_scan_plans);
2827 wpa_printf(MSG_WARNING,
2828 "Use only the first %u scan plans, and the last one (in infinite loop)",
2829 wpa_s->max_sched_scan_plans - 1);
2830 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2831 &scan_plans[num - 1], sizeof(*scan_plans));
2832 num = wpa_s->max_sched_scan_plans;
2833 }
2834
2835 os_free(wpa_s->sched_scan_plans);
2836 wpa_s->sched_scan_plans = scan_plans;
2837 wpa_s->sched_scan_plans_num = num;
2838
2839 return 0;
2840
2841fail:
2842 os_free(scan_plans);
2843 wpa_printf(MSG_ERROR, "invalid scan plans list");
2844 return -1;
2845}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002846
2847
2848/**
2849 * wpas_scan_reset_sched_scan - Reset sched_scan state
2850 * @wpa_s: Pointer to wpa_supplicant data
2851 *
2852 * This function is used to cancel a running scheduled scan and to reset an
2853 * internal scan state to continue with a regular scan on the following
2854 * wpa_supplicant_req_scan() calls.
2855 */
2856void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
2857{
2858 wpa_s->normal_scans = 0;
2859 if (wpa_s->sched_scanning) {
2860 wpa_s->sched_scan_timed_out = 0;
2861 wpa_s->prev_sched_ssid = NULL;
2862 wpa_supplicant_cancel_sched_scan(wpa_s);
2863 }
2864}
2865
2866
2867void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
2868{
2869 /* simulate timeout to restart the sched scan */
2870 wpa_s->sched_scan_timed_out = 1;
2871 wpa_s->prev_sched_ssid = NULL;
2872 wpa_supplicant_cancel_sched_scan(wpa_s);
2873}