blob: b9e4162c8259925c25b1eb996e68e6372528bef0 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2003-2019, 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
Hai Shalomc3565922019-10-28 11:58:20 -070082static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
83 const u8 *mac_addr)
84{
85 u8 *tmp;
86
87 if (params->mac_addr) {
88 params->mac_addr_mask = NULL;
89 os_free(params->mac_addr);
90 params->mac_addr = NULL;
91 }
92
93 params->mac_addr_rand = 1;
94
95 if (!mac_addr)
96 return 0;
97
98 tmp = os_malloc(2 * ETH_ALEN);
99 if (!tmp)
100 return -1;
101
102 os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
103 params->mac_addr = tmp;
104 params->mac_addr_mask = tmp + ETH_ALEN;
105 return 0;
106}
107
108
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800109/**
110 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
111 * @wpa_s: Pointer to wpa_supplicant data
112 * Returns: 0 if no networks are enabled, >0 if networks are enabled
113 *
114 * This function is used to figure out whether any networks (or Interworking
115 * with enabled credentials and auto_interworking) are present in the current
116 * configuration.
117 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700118int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700121 int count = 0, disabled = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800122
123 if (wpa_s->p2p_mgmt)
124 return 0; /* no normal network profiles on p2p_mgmt interface */
125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700127 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700129 else
130 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 ssid = ssid->next;
132 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700133 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
134 wpa_s->conf->auto_interworking)
135 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700136 if (count == 0 && disabled > 0) {
137 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
138 "networks)", disabled);
139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 return count;
141}
142
143
144static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
145 struct wpa_ssid *ssid)
146{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700147 int min_temp_disabled = 0;
148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 while (ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700150 if (!wpas_network_disabled(wpa_s, ssid)) {
151 int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
152
153 if (temp_disabled <= 0)
154 break;
155
156 if (!min_temp_disabled ||
157 temp_disabled < min_temp_disabled)
158 min_temp_disabled = temp_disabled;
159 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 ssid = ssid->next;
161 }
162
163 /* ap_scan=2 mode - try to associate with each SSID. */
164 if (ssid == NULL) {
165 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
166 "end of scan list - go back to beginning");
167 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700168 wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 return;
170 }
171 if (ssid->next) {
172 /* Continue from the next SSID on the next attempt. */
173 wpa_s->prev_scan_ssid = ssid;
174 } else {
175 /* Start from the beginning of the SSID list. */
176 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
177 }
178 wpa_supplicant_associate(wpa_s, NULL, ssid);
179}
180
181
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800182static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800184 struct wpa_supplicant *wpa_s = work->wpa_s;
185 struct wpa_driver_scan_params *params = work->ctx;
186 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800188 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800189 if (!work->started) {
190 wpa_scan_free_params(params);
191 return;
192 }
193 wpa_supplicant_notify_scanning(wpa_s, 0);
194 wpas_notify_scan_done(wpa_s, 0);
195 wpa_s->scan_work = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 return;
197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198
Hai Shalomc3565922019-10-28 11:58:20 -0700199 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
200 wpa_s->wpa_state <= WPA_SCANNING)
201 wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
202
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700203 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
204 wpa_msg(wpa_s, MSG_INFO,
205 "Failed to assign random MAC address for a scan");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700206 wpa_scan_free_params(params);
207 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700208 radio_work_done(work);
209 return;
210 }
211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800212 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800214 if (wpa_s->clear_driver_scan_cache) {
215 wpa_printf(MSG_DEBUG,
216 "Request driver to clear scan cache due to local BSS flush");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800217 params->only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800218 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800219 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800220 /*
221 * Store the obtained vendor scan cookie (if any) in wpa_s context.
222 * The current design is to allow only one scan request on each
223 * interface, hence having this scan cookie stored in wpa_s context is
224 * fine for now.
225 *
226 * Revisit this logic if concurrent scan operations per interface
227 * is supported.
228 */
229 if (ret == 0)
230 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800231 wpa_scan_free_params(params);
232 work->ctx = NULL;
233 if (ret) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700234 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
235 !wpa_s->beacon_rep_data.token;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800236
237 if (wpa_s->disconnected)
238 retry = 0;
239
Hai Shalom899fcc72020-10-19 14:38:18 -0700240 /* do not retry if operation is not supported */
241 if (ret == -EOPNOTSUPP)
242 retry = 0;
243
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800244 wpa_supplicant_notify_scanning(wpa_s, 0);
245 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800246 if (wpa_s->wpa_state == WPA_SCANNING)
247 wpa_supplicant_set_state(wpa_s,
248 wpa_s->scan_prev_wpa_state);
249 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
250 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800252
253 if (retry) {
254 /* Restore scan_req since we will try to scan again */
255 wpa_s->scan_req = wpa_s->last_scan_req;
256 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700257 } else if (wpa_s->scan_res_handler) {
258 /* Clear the scan_res_handler */
259 wpa_s->scan_res_handler = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800260 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700261
262 if (wpa_s->beacon_rep_data.token)
263 wpas_rrm_refuse_request(wpa_s);
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800267
268 os_get_reltime(&wpa_s->scan_trigger_time);
269 wpa_s->scan_runs++;
270 wpa_s->normal_scans++;
271 wpa_s->own_scan_requested = 1;
272 wpa_s->clear_driver_scan_cache = 0;
273 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274}
275
276
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800277/**
278 * wpa_supplicant_trigger_scan - Request driver to start a scan
279 * @wpa_s: Pointer to wpa_supplicant data
280 * @params: Scan parameters
281 * Returns: 0 on success, -1 on failure
282 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
284 struct wpa_driver_scan_params *params)
285{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800286 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800288 if (wpa_s->scan_work) {
289 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
290 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 }
292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800293 ctx = wpa_scan_clone_params(params);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700294 if (!ctx ||
295 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800296 {
297 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700298 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800299 return -1;
300 }
301
302 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303}
304
305
306static void
307wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
308{
309 struct wpa_supplicant *wpa_s = eloop_ctx;
310
311 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
312
313 if (wpa_supplicant_req_sched_scan(wpa_s))
314 wpa_supplicant_req_scan(wpa_s, 0, 0);
315}
316
317
318static void
319wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
320{
321 struct wpa_supplicant *wpa_s = eloop_ctx;
322
323 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
324
325 wpa_s->sched_scan_timed_out = 1;
326 wpa_supplicant_cancel_sched_scan(wpa_s);
327}
328
329
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700330static int
331wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
332 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333{
334 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700335
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800336 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800337 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800338 if (ret)
339 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800340 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800341 wpa_s->sched_scanning = 1;
342
343 return ret;
344}
345
346
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700347static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800348{
349 int ret;
350
351 ret = wpa_drv_stop_sched_scan(wpa_s);
352 if (ret) {
353 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
354 /* TODO: what to do if stopping fails? */
355 return -1;
356 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
358 return ret;
359}
360
361
362static struct wpa_driver_scan_filter *
363wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
364{
365 struct wpa_driver_scan_filter *ssids;
366 struct wpa_ssid *ssid;
367 size_t count;
368
369 *num_ssids = 0;
370 if (!conf->filter_ssids)
371 return NULL;
372
373 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
374 if (ssid->ssid && ssid->ssid_len)
375 count++;
376 }
377 if (count == 0)
378 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 if (ssids == NULL)
381 return NULL;
382
383 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
384 if (!ssid->ssid || !ssid->ssid_len)
385 continue;
386 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
387 ssids[*num_ssids].ssid_len = ssid->ssid_len;
388 (*num_ssids)++;
389 }
390
391 return ssids;
392}
393
394
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800395static void wpa_supplicant_optimize_freqs(
396 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
397{
398#ifdef CONFIG_P2P
399 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
400 wpa_s->go_params) {
401 /* Optimize provisioning state scan based on GO information */
402 if (wpa_s->p2p_in_provisioning < 5 &&
403 wpa_s->go_params->freq > 0) {
404 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
405 "preferred frequency %d MHz",
406 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800407 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800408 if (params->freqs)
409 params->freqs[0] = wpa_s->go_params->freq;
410 } else if (wpa_s->p2p_in_provisioning < 8 &&
411 wpa_s->go_params->freq_list[0]) {
412 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
413 "channels");
414 int_array_concat(&params->freqs,
415 wpa_s->go_params->freq_list);
416 if (params->freqs)
417 int_array_sort_unique(params->freqs);
418 }
419 wpa_s->p2p_in_provisioning++;
420 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700421
422 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
423 /*
424 * Optimize scan based on GO information during persistent
425 * group reinvocation
426 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700427 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700428 wpa_s->p2p_invite_go_freq > 0) {
429 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
430 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800431 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700432 if (params->freqs)
433 params->freqs[0] = wpa_s->p2p_invite_go_freq;
434 }
435 wpa_s->p2p_in_invitation++;
436 if (wpa_s->p2p_in_invitation > 20) {
437 /*
438 * This should not really happen since the variable is
439 * cleared on group removal, but if it does happen, make
440 * sure we do not get stuck in special invitation scan
441 * mode.
442 */
443 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
444 wpa_s->p2p_in_invitation = 0;
445 }
446 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447#endif /* CONFIG_P2P */
448
449#ifdef CONFIG_WPS
450 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
451 /*
452 * Optimize post-provisioning scan based on channel used
453 * during provisioning.
454 */
455 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
456 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800457 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800458 if (params->freqs)
459 params->freqs[0] = wpa_s->wps_freq;
460 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800461 } else if (wpa_s->after_wps)
462 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800463
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800464 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
465 {
466 /* Optimize provisioning scan based on already known channel */
467 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
468 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800469 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800470 if (params->freqs)
471 params->freqs[0] = wpa_s->wps_freq;
472 wpa_s->known_wps_freq = 0; /* only do this once */
473 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800474#endif /* CONFIG_WPS */
475}
476
477
478#ifdef CONFIG_INTERWORKING
479static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
480 struct wpabuf *buf)
481{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800482 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
483 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
484 1 + ETH_ALEN);
485 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
486 /* No Venue Info */
487 if (!is_zero_ether_addr(wpa_s->conf->hessid))
488 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
489}
490#endif /* CONFIG_INTERWORKING */
491
492
Hai Shalomce48b4a2018-09-05 11:41:35 -0700493#ifdef CONFIG_MBO
494static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
495 struct wpabuf **ie)
496{
497 if (wpabuf_resize(ie, 5)) {
498 wpa_printf(MSG_DEBUG,
499 "Failed to allocate space for FILS Request Parameters element");
500 return;
501 }
502
503 /* FILS Request Parameters element */
504 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
505 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
506 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
507 /* Parameter control bitmap */
508 wpabuf_put_u8(*ie, 0);
509 /* Max Channel Time field - contains the value of MaxChannelTime
510 * parameter of the MLME-SCAN.request primitive represented in units of
511 * TUs, as an unsigned integer. A Max Channel Time field value of 255
512 * is used to indicate any duration of more than 254 TUs, or an
513 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
514 */
515 wpabuf_put_u8(*ie, 255);
516}
517#endif /* CONFIG_MBO */
518
519
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700520void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
521{
522 struct wpabuf *default_ies = NULL;
523 u8 ext_capab[18];
524 int ext_capab_len;
525 enum wpa_driver_if_type type = WPA_IF_STATION;
526
527#ifdef CONFIG_P2P
528 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
529 type = WPA_IF_P2P_CLIENT;
530#endif /* CONFIG_P2P */
531
532 wpa_drv_get_ext_capa(wpa_s, type);
533
534 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
535 sizeof(ext_capab));
536 if (ext_capab_len > 0 &&
537 wpabuf_resize(&default_ies, ext_capab_len) == 0)
538 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
539
540#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -0700541 if (wpa_s->enable_oce & OCE_STA)
542 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700543 /* Send MBO and OCE capabilities */
544 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700545 wpas_mbo_scan_ie(wpa_s, default_ies);
546#endif /* CONFIG_MBO */
547
548 if (default_ies)
549 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
550 wpabuf_len(default_ies));
551 wpabuf_free(default_ies);
552}
553
554
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700555static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800556{
557 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700558 u8 ext_capab[18];
559 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800560#ifdef CONFIG_WPS
561 int wps = 0;
562 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
563#endif /* CONFIG_WPS */
564
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700565#ifdef CONFIG_P2P
566 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
567 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
568 else
569#endif /* CONFIG_P2P */
570 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
571
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700572 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
573 sizeof(ext_capab));
574 if (ext_capab_len > 0 &&
575 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
576 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800578#ifdef CONFIG_INTERWORKING
579 if (wpa_s->conf->interworking &&
580 wpabuf_resize(&extra_ie, 100) == 0)
581 wpas_add_interworking_elements(wpa_s, extra_ie);
582#endif /* CONFIG_INTERWORKING */
583
Hai Shalomce48b4a2018-09-05 11:41:35 -0700584#ifdef CONFIG_MBO
585 if (wpa_s->enable_oce & OCE_STA)
586 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
587#endif /* CONFIG_MBO */
588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800589#ifdef CONFIG_WPS
590 wps = wpas_wps_in_use(wpa_s, &req_type);
591
592 if (wps) {
593 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700594 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
595 DEV_PW_DEFAULT,
596 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800597 wpa_s->wps->uuid, req_type,
598 0, NULL);
599 if (wps_ie) {
600 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
601 wpabuf_put_buf(extra_ie, wps_ie);
602 wpabuf_free(wps_ie);
603 }
604 }
605
606#ifdef CONFIG_P2P
607 if (wps) {
608 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
609 if (wpabuf_resize(&extra_ie, ielen) == 0)
610 wpas_p2p_scan_ie(wpa_s, extra_ie);
611 }
612#endif /* CONFIG_P2P */
613
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800614 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
615
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616#endif /* CONFIG_WPS */
617
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700618#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -0800619 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
620 wpas_hs20_add_indication(extra_ie, -1, 0);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700621#endif /* CONFIG_HS20 */
622
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800623#ifdef CONFIG_FST
624 if (wpa_s->fst_ies &&
625 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
626 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
627#endif /* CONFIG_FST */
628
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800629#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700630 /* Send MBO and OCE capabilities */
631 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800632 wpas_mbo_scan_ie(wpa_s, extra_ie);
633#endif /* CONFIG_MBO */
634
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700635 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
636 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
637
638 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
639 wpabuf_put_buf(extra_ie, buf);
640 }
641
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800642 return extra_ie;
643}
644
645
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800646#ifdef CONFIG_P2P
647
648/*
649 * Check whether there are any enabled networks or credentials that could be
650 * used for a non-P2P connection.
651 */
652static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
653{
654 struct wpa_ssid *ssid;
655
656 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
657 if (wpas_network_disabled(wpa_s, ssid))
658 continue;
659 if (!ssid->p2p_group)
660 return 1;
661 }
662
663 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
664 wpa_s->conf->auto_interworking)
665 return 1;
666
667 return 0;
668}
669
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700670#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800671
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800672
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700673static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
674 enum hostapd_hw_mode band,
Hai Shalomfdcde762020-04-02 11:19:20 -0700675 struct wpa_driver_scan_params *params,
676 int is_6ghz)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700677{
678 /* Include only supported channels for the specified band */
679 struct hostapd_hw_modes *mode;
680 int count, i;
681
Hai Shalomfdcde762020-04-02 11:19:20 -0700682 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700683 if (mode == NULL) {
684 /* No channels supported in this band - use empty list */
685 params->freqs = os_zalloc(sizeof(int));
686 return;
687 }
688
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800689 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700690 if (params->freqs == NULL)
691 return;
692 for (count = 0, i = 0; i < mode->num_channels; i++) {
693 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
694 continue;
695 params->freqs[count++] = mode->channels[i].freq;
696 }
697}
698
699
700static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
701 struct wpa_driver_scan_params *params)
702{
703 if (wpa_s->hw.modes == NULL)
704 return; /* unknown what channels the driver supports */
705 if (params->freqs)
706 return; /* already using a limited channel set */
707 if (wpa_s->setband == WPA_SETBAND_5G)
708 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
Hai Shalomfdcde762020-04-02 11:19:20 -0700709 params, 0);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700710 else if (wpa_s->setband == WPA_SETBAND_2G)
711 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
Hai Shalomfdcde762020-04-02 11:19:20 -0700712 params, 0);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700713}
714
715
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800716static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
717 struct wpa_driver_scan_params *params,
718 size_t max_ssids, const u8 *ssid, size_t ssid_len)
719{
720 unsigned int j;
721
722 for (j = 0; j < params->num_ssids; j++) {
723 if (params->ssids[j].ssid_len == ssid_len &&
724 params->ssids[j].ssid &&
725 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
726 return; /* already in the list */
727 }
728
729 if (params->num_ssids + 1 > max_ssids) {
730 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
731 return;
732 }
733
734 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
735 wpa_ssid_txt(ssid, ssid_len));
736
737 params->ssids[params->num_ssids].ssid = ssid;
738 params->ssids[params->num_ssids].ssid_len = ssid_len;
739 params->num_ssids++;
740}
741
742
743static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
744 struct wpa_driver_scan_params *params,
745 struct wpa_ssid *ssid, size_t max_ssids)
746{
747#ifdef CONFIG_OWE
748 struct wpa_bss *bss;
749
750 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
751 return;
752
753 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
754 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
755
756 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
757 const u8 *owe, *pos, *end;
758 const u8 *owe_ssid;
759 size_t owe_ssid_len;
760
761 if (bss->ssid_len != ssid->ssid_len ||
762 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
763 continue;
764
765 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
766 if (!owe || owe[1] < 4)
767 continue;
768
769 pos = owe + 6;
770 end = owe + 2 + owe[1];
771
772 /* Must include BSSID and ssid_len */
773 if (end - pos < ETH_ALEN + 1)
774 return;
775
776 /* Skip BSSID */
777 pos += ETH_ALEN;
778 owe_ssid_len = *pos++;
779 owe_ssid = pos;
780
781 if ((size_t) (end - pos) < owe_ssid_len ||
782 owe_ssid_len > SSID_MAX_LEN)
783 return;
784
785 wpa_printf(MSG_DEBUG,
786 "OWE: scan_ssids: transition mode OWE ssid=%s",
787 wpa_ssid_txt(owe_ssid, owe_ssid_len));
788
789 wpa_add_scan_ssid(wpa_s, params, max_ssids,
790 owe_ssid, owe_ssid_len);
791 return;
792 }
793#endif /* CONFIG_OWE */
794}
795
796
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700797static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
798 struct wpa_driver_scan_params *params,
799 size_t max_ssids)
800{
801 unsigned int i;
802 struct wpa_ssid *ssid;
803
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700804 /*
805 * For devices with max_ssids greater than 1, leave the last slot empty
806 * for adding the wildcard scan entry.
807 */
808 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
809
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700810 for (i = 0; i < wpa_s->scan_id_count; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700811 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800812 if (!ssid)
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700813 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800814 if (ssid->scan_ssid)
815 wpa_add_scan_ssid(wpa_s, params, max_ssids,
816 ssid->ssid, ssid->ssid_len);
817 /*
818 * Also add the SSID of the OWE BSS, to allow discovery of
819 * transition mode APs more quickly.
820 */
821 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700822 }
823
824 wpa_s->scan_id_count = 0;
825}
826
827
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700828static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
829 struct wpa_driver_scan_params *params,
830 size_t max_ssids)
831{
832 unsigned int i;
833
834 if (wpa_s->ssids_from_scan_req == NULL ||
835 wpa_s->num_ssids_from_scan_req == 0)
836 return 0;
837
838 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
839 wpa_s->num_ssids_from_scan_req = max_ssids;
840 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
841 (unsigned int) max_ssids);
842 }
843
844 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
845 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
846 params->ssids[i].ssid_len =
847 wpa_s->ssids_from_scan_req[i].ssid_len;
848 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
849 params->ssids[i].ssid,
850 params->ssids[i].ssid_len);
851 }
852
853 params->num_ssids = wpa_s->num_ssids_from_scan_req;
854 wpa_s->num_ssids_from_scan_req = 0;
855 return 1;
856}
857
858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
860{
861 struct wpa_supplicant *wpa_s = eloop_ctx;
862 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800863 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700864 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700866 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800868 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869
Dmitry Shmidt29333592017-01-09 12:27:11 -0800870 wpa_s->ignore_post_flush_scan_res = 0;
871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
873 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
874 return;
875 }
876
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800877 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700878 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
880 return;
881 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800882
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700883 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800884 /*
885 * If we are already in scanning state, we shall reschedule the
886 * the incoming scan request.
887 */
888 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
889 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700890 return;
891 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800892
Dmitry Shmidt04949592012-07-19 12:16:46 -0700893 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800894 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
896 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
897 return;
898 }
899
900 if (wpa_s->conf->ap_scan != 0 &&
901 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
902 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
903 "overriding ap_scan configuration");
904 wpa_s->conf->ap_scan = 0;
905 wpas_notify_ap_scan_changed(wpa_s);
906 }
907
908 if (wpa_s->conf->ap_scan == 0) {
909 wpa_supplicant_gen_assoc_event(wpa_s);
910 return;
911 }
912
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800913 ssid = NULL;
914 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
915 wpa_s->connect_without_scan) {
916 connect_without_scan = 1;
917 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
918 if (ssid == wpa_s->connect_without_scan)
919 break;
920 }
921 }
922
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800923 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800924 if (p2p_in_prog && p2p_in_prog != 2 &&
925 (!ssid ||
926 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800927 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
928 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700929 return;
930 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700931
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800932 /*
933 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
934 * used for changing modes inside wpa_supplicant (roaming,
935 * auto-reconnect, etc). Discarding the scan might hurt these processes.
936 * The normal use case for PNO is to suspend the host immediately after
937 * starting PNO, so the periodic 100 ms attempts to run the scan do not
938 * normally happen in practice multiple times, i.e., this is simply
939 * restarting scanning once the host is woken up and PNO stopped.
940 */
941 if (wpa_s->pno || wpa_s->pno_sched_pending) {
942 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
943 wpa_supplicant_req_scan(wpa_s, 0, 100000);
944 return;
945 }
946
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800947 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 max_ssids = 1;
949 else {
950 max_ssids = wpa_s->max_scan_ssids;
951 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
952 max_ssids = WPAS_MAX_SCAN_SSIDS;
953 }
954
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800955 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800956 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800958 if (connect_without_scan) {
959 wpa_s->connect_without_scan = NULL;
960 if (ssid) {
961 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
962 "without scan step");
963 wpa_supplicant_associate(wpa_s, NULL, ssid);
964 return;
965 }
966 }
967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 os_memset(&params, 0, sizeof(params));
969
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800970 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
972 wpa_s->wpa_state == WPA_INACTIVE)
973 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
974
Dmitry Shmidt04949592012-07-19 12:16:46 -0700975 /*
976 * If autoscan has set its own scanning parameters
977 */
978 if (wpa_s->autoscan_params != NULL) {
979 scan_params = wpa_s->autoscan_params;
980 goto scan;
981 }
982
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700983 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
984 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
985 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
986 goto ssid_list_set;
987 }
988
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989#ifdef CONFIG_P2P
990 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800991 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800992 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
993 wpa_s->p2p_in_provisioning,
994 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700995 params.ssids[0].ssid = wpa_s->go_params->ssid;
996 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
997 params.num_ssids = 1;
998 goto ssid_list_set;
999 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07001000
1001 if (wpa_s->p2p_in_invitation) {
1002 if (wpa_s->current_ssid) {
1003 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1004 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1005 params.ssids[0].ssid_len =
1006 wpa_s->current_ssid->ssid_len;
1007 params.num_ssids = 1;
1008 } else {
1009 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1010 }
1011 goto ssid_list_set;
1012 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013#endif /* CONFIG_P2P */
1014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 /* Find the starting point from which to continue scanning */
1016 ssid = wpa_s->conf->ssid;
1017 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1018 while (ssid) {
1019 if (ssid == wpa_s->prev_scan_ssid) {
1020 ssid = ssid->next;
1021 break;
1022 }
1023 ssid = ssid->next;
1024 }
1025 }
1026
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001027 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001028#ifdef CONFIG_AP
1029 !wpa_s->ap_iface &&
1030#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001031 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001032 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001033 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 wpa_supplicant_assoc_try(wpa_s, ssid);
1035 return;
1036 } else if (wpa_s->conf->ap_scan == 2) {
1037 /*
1038 * User-initiated scan request in ap_scan == 2; scan with
1039 * wildcard SSID.
1040 */
1041 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001042 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1043 /*
1044 * Perform single-channel single-SSID scan for
1045 * reassociate-to-same-BSS operation.
1046 */
1047 /* Setup SSID */
1048 ssid = wpa_s->current_ssid;
1049 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1050 ssid->ssid, ssid->ssid_len);
1051 params.ssids[0].ssid = ssid->ssid;
1052 params.ssids[0].ssid_len = ssid->ssid_len;
1053 params.num_ssids = 1;
1054
1055 /*
1056 * Allocate memory for frequency array, allocate one extra
1057 * slot for the zero-terminator.
1058 */
1059 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001060 if (params.freqs) {
1061 params.freqs[0] = wpa_s->assoc_freq;
1062 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001063 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07001064
1065 /*
1066 * Reset the reattach flag so that we fall back to full scan if
1067 * this scan fails.
1068 */
1069 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 } else {
1071 struct wpa_ssid *start = ssid, *tssid;
1072 int freqs_set = 0;
1073 if (ssid == NULL && max_ssids > 1)
1074 ssid = wpa_s->conf->ssid;
1075 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001076 if (!wpas_network_disabled(wpa_s, ssid) &&
1077 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1079 ssid->ssid, ssid->ssid_len);
1080 params.ssids[params.num_ssids].ssid =
1081 ssid->ssid;
1082 params.ssids[params.num_ssids].ssid_len =
1083 ssid->ssid_len;
1084 params.num_ssids++;
1085 if (params.num_ssids + 1 >= max_ssids)
1086 break;
1087 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001088
1089 if (!wpas_network_disabled(wpa_s, ssid)) {
1090 /*
1091 * Also add the SSID of the OWE BSS, to allow
1092 * discovery of transition mode APs more
1093 * quickly.
1094 */
1095 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1096 max_ssids);
1097 }
1098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 ssid = ssid->next;
1100 if (ssid == start)
1101 break;
1102 if (ssid == NULL && max_ssids > 1 &&
1103 start != wpa_s->conf->ssid)
1104 ssid = wpa_s->conf->ssid;
1105 }
1106
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001107 if (wpa_s->scan_id_count &&
1108 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1109 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1110
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001111 for (tssid = wpa_s->conf->ssid;
1112 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1113 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001114 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07001116 if (((params.freqs || !freqs_set) &&
1117 tssid->scan_freq) &&
1118 int_array_len(params.freqs) < 100) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119 int_array_concat(&params.freqs,
1120 tssid->scan_freq);
1121 } else {
1122 os_free(params.freqs);
1123 params.freqs = NULL;
1124 }
1125 freqs_set = 1;
1126 }
1127 int_array_sort_unique(params.freqs);
1128 }
1129
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001130 if (ssid && max_ssids == 1) {
1131 /*
1132 * If the driver is limited to 1 SSID at a time interleave
1133 * wildcard SSID scans with specific SSID scans to avoid
1134 * waiting a long time for a wildcard scan.
1135 */
1136 if (!wpa_s->prev_scan_wildcard) {
1137 params.ssids[0].ssid = NULL;
1138 params.ssids[0].ssid_len = 0;
1139 wpa_s->prev_scan_wildcard = 1;
1140 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1141 "wildcard SSID (Interleave with specific)");
1142 } else {
1143 wpa_s->prev_scan_ssid = ssid;
1144 wpa_s->prev_scan_wildcard = 0;
1145 wpa_dbg(wpa_s, MSG_DEBUG,
1146 "Starting AP scan for specific SSID: %s",
1147 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001149 } else if (ssid) {
1150 /* max_ssids > 1 */
1151
1152 wpa_s->prev_scan_ssid = ssid;
1153 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1154 "the scan request");
1155 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001156 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1157 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1158 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001159 } else if (wpa_s->conf->passive_scan) {
1160 wpa_dbg(wpa_s, MSG_DEBUG,
1161 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 } else {
1163 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1164 params.num_ssids++;
1165 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1166 "SSID");
1167 }
1168
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001169ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001170 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001171 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001173 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001174 wpa_s->manual_scan_only_new) {
1175 wpa_printf(MSG_DEBUG,
1176 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001177 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001178 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001179
1180 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1181 wpa_s->manual_scan_freqs) {
1182 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1183 params.freqs = wpa_s->manual_scan_freqs;
1184 wpa_s->manual_scan_freqs = NULL;
1185 }
1186
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001187 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1188 wpa_dbg(wpa_s, MSG_DEBUG,
1189 "Limit select_network scan to specified channels");
1190 params.freqs = wpa_s->select_network_scan_freqs;
1191 wpa_s->select_network_scan_freqs = NULL;
1192 }
1193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1195 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1196 "generated frequency list");
1197 params.freqs = wpa_s->next_scan_freqs;
1198 } else
1199 os_free(wpa_s->next_scan_freqs);
1200 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001201 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001203 /* See if user specified frequencies. If so, scan only those. */
1204 if (wpa_s->conf->freq_list && !params.freqs) {
1205 wpa_dbg(wpa_s, MSG_DEBUG,
1206 "Optimize scan based on conf->freq_list");
1207 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1208 }
1209
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001210 /* Use current associated channel? */
1211 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001212 unsigned int num = wpa_s->num_multichan_concurrent;
1213
1214 params.freqs = os_calloc(num + 1, sizeof(int));
1215 if (params.freqs) {
1216 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1217 if (num > 0) {
1218 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1219 "current operating channels since "
1220 "scan_cur_freq is enabled");
1221 } else {
1222 os_free(params.freqs);
1223 params.freqs = NULL;
1224 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001225 }
1226 }
1227
Hai Shalomce48b4a2018-09-05 11:41:35 -07001228#ifdef CONFIG_MBO
1229 if (wpa_s->enable_oce & OCE_STA)
1230 params.oce_scan = 1;
1231#endif /* CONFIG_MBO */
1232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1234 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001235 if (extra_ie) {
1236 params.extra_ies = wpabuf_head(extra_ie);
1237 params.extra_ies_len = wpabuf_len(extra_ie);
1238 }
1239
1240#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001241 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001242 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001243 /*
1244 * The interface may not yet be in P2P mode, so we have to
1245 * explicitly request P2P probe to disable CCK rates.
1246 */
1247 params.p2p_probe = 1;
1248 }
1249#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001251 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001252 wpa_s->wpa_state <= WPA_SCANNING)
1253 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001254
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001255 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1256 struct wpa_bss *bss;
1257
1258 params.bssid = wpa_s->next_scan_bssid;
1259 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
Hai Shalomfdcde762020-04-02 11:19:20 -07001260 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1261 bss && bss->ssid_len && params.num_ssids == 1 &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001262 params.ssids[0].ssid_len == 0) {
1263 params.ssids[0].ssid = bss->ssid;
1264 params.ssids[0].ssid_len = bss->ssid_len;
1265 wpa_dbg(wpa_s, MSG_DEBUG,
1266 "Scan a previously specified BSSID " MACSTR
1267 " and SSID %s",
1268 MAC2STR(params.bssid),
1269 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1270 } else {
1271 wpa_dbg(wpa_s, MSG_DEBUG,
1272 "Scan a previously specified BSSID " MACSTR,
1273 MAC2STR(params.bssid));
1274 }
1275 }
1276
Dmitry Shmidt04949592012-07-19 12:16:46 -07001277 scan_params = &params;
1278
1279scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001280#ifdef CONFIG_P2P
1281 /*
1282 * If the driver does not support multi-channel concurrency and a
1283 * virtual interface that shares the same radio with the wpa_s interface
1284 * is operating there may not be need to scan other channels apart from
1285 * the current operating channel on the other virtual interface. Filter
1286 * out other channels in case we are trying to find a connection for a
1287 * station interface when we are not configured to prefer station
1288 * connection and a concurrent operation is already in process.
1289 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001290 if (wpa_s->scan_for_connection &&
1291 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001292 !scan_params->freqs && !params.freqs &&
1293 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001294 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1295 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001296 unsigned int num = wpa_s->num_multichan_concurrent;
1297
1298 params.freqs = os_calloc(num + 1, sizeof(int));
1299 if (params.freqs) {
1300 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1301 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1302 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1303 } else {
1304 os_free(params.freqs);
1305 params.freqs = NULL;
1306 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001307 }
1308 }
1309#endif /* CONFIG_P2P */
1310
Dmitry Shmidt04949592012-07-19 12:16:46 -07001311 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001313 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1314 !wpa_s->manual_scan_freqs) {
1315 /* Restore manual_scan_freqs for the next attempt */
1316 wpa_s->manual_scan_freqs = params.freqs;
1317 params.freqs = NULL;
1318 }
1319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001320 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 os_free(params.freqs);
1322 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001323 os_free(params.mac_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324
1325 if (ret) {
1326 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001327 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1328 wpa_supplicant_set_state(wpa_s,
1329 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001330 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001331 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001333 } else {
1334 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001335#ifdef CONFIG_INTERWORKING
1336 wpa_s->interworking_fast_assoc_tried = 0;
1337#endif /* CONFIG_INTERWORKING */
Hai Shalomfdcde762020-04-02 11:19:20 -07001338 wpa_s->next_scan_bssid_wildcard_ssid = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001339 if (params.bssid)
1340 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 }
1342}
1343
1344
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001345void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1346{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001347 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001348 int cancelled;
1349
1350 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1351 &remaining);
1352
1353 new_int.sec = sec;
1354 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001355 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001356 new_int.sec = remaining.sec;
1357 new_int.usec = remaining.usec;
1358 }
1359
Dmitry Shmidt051af732013-10-22 13:52:46 -07001360 if (cancelled) {
1361 eloop_register_timeout(new_int.sec, new_int.usec,
1362 wpa_supplicant_scan, wpa_s, NULL);
1363 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001364 wpa_s->scan_interval = sec;
1365}
1366
1367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368/**
1369 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1370 * @wpa_s: Pointer to wpa_supplicant data
1371 * @sec: Number of seconds after which to scan
1372 * @usec: Number of microseconds after which to scan
1373 *
1374 * This function is used to schedule a scan for neighboring access points after
1375 * the specified time.
1376 */
1377void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1378{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001379 int res;
1380
1381 if (wpa_s->p2p_mgmt) {
1382 wpa_dbg(wpa_s, MSG_DEBUG,
1383 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1384 sec, usec);
1385 return;
1386 }
1387
1388 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1389 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001390 if (res == 1) {
1391 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001392 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001393 } else if (res == 0) {
1394 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1395 sec, usec);
1396 } else {
1397 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1398 sec, usec);
1399 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401}
1402
1403
1404/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001405 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1406 * @wpa_s: Pointer to wpa_supplicant data
1407 * @sec: Number of seconds after which to scan
1408 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001409 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001410 *
1411 * This function is used to schedule periodic scans for neighboring
1412 * access points after the specified time.
1413 */
1414int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1415 int sec, int usec)
1416{
1417 if (!wpa_s->sched_scan_supported)
1418 return -1;
1419
1420 eloop_register_timeout(sec, usec,
1421 wpa_supplicant_delayed_sched_scan_timeout,
1422 wpa_s, NULL);
1423
1424 return 0;
1425}
1426
1427
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001428static void
1429wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1430 struct wpa_driver_scan_params *params)
1431{
1432 if (wpa_s->wpa_state != WPA_COMPLETED ||
1433 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1434 wpa_s->srp.relative_rssi_set == 0)
1435 return;
1436
1437 params->relative_rssi_set = 1;
1438 params->relative_rssi = wpa_s->srp.relative_rssi;
1439
1440 if (wpa_s->srp.relative_adjust_rssi == 0)
1441 return;
1442
1443 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1444 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1445}
1446
1447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001448/**
1449 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1450 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001451 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001452 *
1453 * This function is used to schedule periodic scans for neighboring
1454 * access points repeating the scan continuously.
1455 */
1456int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1457{
1458 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001459 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001460 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001461 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001462 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001463 int ret;
1464 unsigned int max_sched_scan_ssids;
1465 int wildcard = 0;
1466 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001467 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001468
1469 if (!wpa_s->sched_scan_supported)
1470 return -1;
1471
1472 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1473 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1474 else
1475 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001476 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477 return -1;
1478
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001479 wpa_s->sched_scan_stop_req = 0;
1480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001481 if (wpa_s->sched_scanning) {
1482 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1483 return 0;
1484 }
1485
1486 need_ssids = 0;
1487 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001488 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001489 /* Use wildcard SSID to find this network */
1490 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001491 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1492 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001493 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001494
1495#ifdef CONFIG_WPS
1496 if (!wpas_network_disabled(wpa_s, ssid) &&
1497 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1498 /*
1499 * Normal scan is more reliable and faster for WPS
1500 * operations and since these are for short periods of
1501 * time, the benefit of trying to use sched_scan would
1502 * be limited.
1503 */
1504 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1505 "sched_scan for WPS");
1506 return -1;
1507 }
1508#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001509 }
1510 if (wildcard)
1511 need_ssids++;
1512
1513 if (wpa_s->normal_scans < 3 &&
1514 (need_ssids <= wpa_s->max_scan_ssids ||
1515 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1516 /*
1517 * When normal scan can speed up operations, use that for the
1518 * first operations before starting the sched_scan to allow
1519 * user space sleep more. We do this only if the normal scan
1520 * has functionality that is suitable for this or if the
1521 * sched_scan does not have better support for multiple SSIDs.
1522 */
1523 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1524 "sched_scan for initial scans (normal_scans=%d)",
1525 wpa_s->normal_scans);
1526 return -1;
1527 }
1528
1529 os_memset(&params, 0, sizeof(params));
1530
1531 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001532 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001533 sizeof(struct wpa_driver_scan_filter));
1534
1535 prev_state = wpa_s->wpa_state;
1536 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1537 wpa_s->wpa_state == WPA_INACTIVE)
1538 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1539
Dmitry Shmidt04949592012-07-19 12:16:46 -07001540 if (wpa_s->autoscan_params != NULL) {
1541 scan_params = wpa_s->autoscan_params;
1542 goto scan;
1543 }
1544
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001545 /* Find the starting point from which to continue scanning */
1546 ssid = wpa_s->conf->ssid;
1547 if (wpa_s->prev_sched_ssid) {
1548 while (ssid) {
1549 if (ssid == wpa_s->prev_sched_ssid) {
1550 ssid = ssid->next;
1551 break;
1552 }
1553 ssid = ssid->next;
1554 }
1555 }
1556
1557 if (!ssid || !wpa_s->prev_sched_ssid) {
1558 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001559 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1560 wpa_s->first_sched_scan = 1;
1561 ssid = wpa_s->conf->ssid;
1562 wpa_s->prev_sched_ssid = ssid;
1563 }
1564
1565 if (wildcard) {
1566 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1567 params.num_ssids++;
1568 }
1569
1570 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001571 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001572 goto next;
1573
1574 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1575 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1576 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1577 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1578 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1579 ssid->ssid, ssid->ssid_len);
1580 params.filter_ssids[params.num_filter_ssids].ssid_len =
1581 ssid->ssid_len;
1582 params.num_filter_ssids++;
1583 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1584 {
1585 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1586 "filter for sched_scan - drop filter");
1587 os_free(params.filter_ssids);
1588 params.filter_ssids = NULL;
1589 params.num_filter_ssids = 0;
1590 }
1591
1592 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1593 if (params.num_ssids == max_sched_scan_ssids)
1594 break; /* only room for broadcast SSID */
1595 wpa_dbg(wpa_s, MSG_DEBUG,
1596 "add to active scan ssid: %s",
1597 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1598 params.ssids[params.num_ssids].ssid =
1599 ssid->ssid;
1600 params.ssids[params.num_ssids].ssid_len =
1601 ssid->ssid_len;
1602 params.num_ssids++;
1603 if (params.num_ssids >= max_sched_scan_ssids) {
1604 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001605 do {
1606 ssid = ssid->next;
1607 } while (ssid &&
1608 (wpas_network_disabled(wpa_s, ssid) ||
1609 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001610 break;
1611 }
1612 }
1613
1614 next:
1615 wpa_s->prev_sched_ssid = ssid;
1616 ssid = ssid->next;
1617 }
1618
1619 if (params.num_filter_ssids == 0) {
1620 os_free(params.filter_ssids);
1621 params.filter_ssids = NULL;
1622 }
1623
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001624 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1625 if (extra_ie) {
1626 params.extra_ies = wpabuf_head(extra_ie);
1627 params.extra_ies_len = wpabuf_len(extra_ie);
1628 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001629
Dmitry Shmidt18463232014-01-24 12:29:41 -08001630 if (wpa_s->conf->filter_rssi)
1631 params.filter_rssi = wpa_s->conf->filter_rssi;
1632
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001633 /* See if user specified frequencies. If so, scan only those. */
1634 if (wpa_s->conf->freq_list && !params.freqs) {
1635 wpa_dbg(wpa_s, MSG_DEBUG,
1636 "Optimize scan based on conf->freq_list");
1637 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1638 }
1639
Hai Shalomce48b4a2018-09-05 11:41:35 -07001640#ifdef CONFIG_MBO
1641 if (wpa_s->enable_oce & OCE_STA)
1642 params.oce_scan = 1;
1643#endif /* CONFIG_MBO */
1644
Dmitry Shmidt04949592012-07-19 12:16:46 -07001645 scan_params = &params;
1646
1647scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001648 wpa_s->sched_scan_timed_out = 0;
1649
1650 /*
1651 * We cannot support multiple scan plans if the scan request includes
1652 * too many SSID's, so in this case use only the last scan plan and make
1653 * it run infinitely. It will be stopped by the timeout.
1654 */
1655 if (wpa_s->sched_scan_plans_num == 1 ||
1656 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1657 params.sched_scan_plans = wpa_s->sched_scan_plans;
1658 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1659 } else if (wpa_s->sched_scan_plans_num > 1) {
1660 wpa_dbg(wpa_s, MSG_DEBUG,
1661 "Too many SSIDs. Default to using single scheduled_scan plan");
1662 params.sched_scan_plans =
1663 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1664 1];
1665 params.sched_scan_plans_num = 1;
1666 } else {
1667 if (wpa_s->conf->sched_scan_interval)
1668 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1669 else
1670 scan_plan.interval = 10;
1671
1672 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1673 wpa_printf(MSG_WARNING,
1674 "Scan interval too long(%u), use the maximum allowed(%u)",
1675 scan_plan.interval,
1676 wpa_s->max_sched_scan_plan_interval);
1677 scan_plan.interval =
1678 wpa_s->max_sched_scan_plan_interval;
1679 }
1680
1681 scan_plan.iterations = 0;
1682 params.sched_scan_plans = &scan_plan;
1683 params.sched_scan_plans_num = 1;
1684 }
1685
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001686 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1687
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001688 if (ssid || !wpa_s->first_sched_scan) {
1689 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001690 "Starting sched scan after %u seconds: interval %u timeout %d",
1691 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001692 params.sched_scan_plans[0].interval,
1693 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001694 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001695 wpa_dbg(wpa_s, MSG_DEBUG,
1696 "Starting sched scan after %u seconds (no timeout)",
1697 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001698 }
1699
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001700 wpa_setband_scan_freqs(wpa_s, scan_params);
1701
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001702 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001703 wpa_s->wpa_state <= WPA_SCANNING)
1704 wpa_setup_mac_addr_rand_params(&params,
1705 wpa_s->mac_addr_sched_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001706
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001707 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1708
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001709 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001710 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001712 os_free(params.mac_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713 if (ret) {
1714 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1715 if (prev_state != wpa_s->wpa_state)
1716 wpa_supplicant_set_state(wpa_s, prev_state);
1717 return ret;
1718 }
1719
1720 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1721 if (ssid || !wpa_s->first_sched_scan) {
1722 wpa_s->sched_scan_timed_out = 0;
1723 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1724 wpa_supplicant_sched_scan_timeout,
1725 wpa_s, NULL);
1726 wpa_s->first_sched_scan = 0;
1727 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001728 params.sched_scan_plans[0].interval *= 2;
1729 if ((unsigned int) wpa_s->sched_scan_timeout <
1730 params.sched_scan_plans[0].interval ||
1731 params.sched_scan_plans[0].interval >
1732 wpa_s->max_sched_scan_plan_interval) {
1733 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001734 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1735 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001736 }
1737
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001738 /* If there is no more ssids, start next time from the beginning */
1739 if (!ssid)
1740 wpa_s->prev_sched_ssid = NULL;
1741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001742 return 0;
1743}
1744
1745
1746/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1748 * @wpa_s: Pointer to wpa_supplicant data
1749 *
1750 * This function is used to cancel a scan request scheduled with
1751 * wpa_supplicant_req_scan().
1752 */
1753void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1754{
1755 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1756 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757}
1758
1759
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001760/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001761 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1762 * @wpa_s: Pointer to wpa_supplicant data
1763 *
1764 * This function is used to stop a delayed scheduled scan.
1765 */
1766void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1767{
1768 if (!wpa_s->sched_scan_supported)
1769 return;
1770
1771 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1772 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1773 wpa_s, NULL);
1774}
1775
1776
1777/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1779 * @wpa_s: Pointer to wpa_supplicant data
1780 *
1781 * This function is used to stop a periodic scheduled scan.
1782 */
1783void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1784{
1785 if (!wpa_s->sched_scanning)
1786 return;
1787
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001788 if (wpa_s->sched_scanning)
1789 wpa_s->sched_scan_stop_req = 1;
1790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001791 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1792 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1793 wpa_supplicant_stop_sched_scan(wpa_s);
1794}
1795
1796
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001797/**
1798 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1799 * @wpa_s: Pointer to wpa_supplicant data
1800 * @scanning: Whether scanning is currently in progress
1801 *
1802 * This function is to generate scanning notifycations. It is called whenever
1803 * there may have been a change in scanning (scan started, completed, stopped).
1804 * wpas_notify_scanning() is called whenever the scanning state changed from the
1805 * previously notified state.
1806 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1808 int scanning)
1809{
1810 if (wpa_s->scanning != scanning) {
1811 wpa_s->scanning = scanning;
1812 wpas_notify_scanning(wpa_s);
1813 }
1814}
1815
1816
1817static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1818{
1819 int rate = 0;
1820 const u8 *ie;
1821 int i;
1822
1823 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1824 for (i = 0; ie && i < ie[1]; i++) {
1825 if ((ie[i + 2] & 0x7f) > rate)
1826 rate = ie[i + 2] & 0x7f;
1827 }
1828
1829 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1830 for (i = 0; ie && i < ie[1]; i++) {
1831 if ((ie[i + 2] & 0x7f) > rate)
1832 rate = ie[i + 2] & 0x7f;
1833 }
1834
1835 return rate;
1836}
1837
1838
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001839/**
1840 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1841 * @res: Scan result entry
1842 * @ie: Information element identitifier (WLAN_EID_*)
1843 * Returns: Pointer to the information element (id field) or %NULL if not found
1844 *
1845 * This function returns the first matching information element in the scan
1846 * result.
1847 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1849{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001850 size_t ie_len = res->ie_len;
1851
1852 /* Use the Beacon frame IEs if res->ie_len is not available */
1853 if (!ie_len)
1854 ie_len = res->beacon_ie_len;
1855
1856 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857}
1858
1859
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001860/**
1861 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1862 * @res: Scan result entry
1863 * @vendor_type: Vendor type (four octets starting the IE payload)
1864 * Returns: Pointer to the information element (id field) or %NULL if not found
1865 *
1866 * This function returns the first matching information element in the scan
1867 * result.
1868 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1870 u32 vendor_type)
1871{
1872 const u8 *end, *pos;
1873
1874 pos = (const u8 *) (res + 1);
1875 end = pos + res->ie_len;
1876
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001877 while (end - pos > 1) {
1878 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 break;
1880 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1881 vendor_type == WPA_GET_BE32(&pos[2]))
1882 return pos;
1883 pos += 2 + pos[1];
1884 }
1885
1886 return NULL;
1887}
1888
1889
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001890/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001891 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1892 * @res: Scan result entry
1893 * @vendor_type: Vendor type (four octets starting the IE payload)
1894 * Returns: Pointer to the information element (id field) or %NULL if not found
1895 *
1896 * This function returns the first matching information element in the scan
1897 * result.
1898 *
1899 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1900 * from Beacon frames instead of either Beacon or Probe Response frames.
1901 */
1902const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1903 u32 vendor_type)
1904{
1905 const u8 *end, *pos;
1906
1907 if (res->beacon_ie_len == 0)
1908 return NULL;
1909
1910 pos = (const u8 *) (res + 1);
1911 pos += res->ie_len;
1912 end = pos + res->beacon_ie_len;
1913
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001914 while (end - pos > 1) {
1915 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001916 break;
1917 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1918 vendor_type == WPA_GET_BE32(&pos[2]))
1919 return pos;
1920 pos += 2 + pos[1];
1921 }
1922
1923 return NULL;
1924}
1925
1926
1927/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001928 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1929 * @res: Scan result entry
1930 * @vendor_type: Vendor type (four octets starting the IE payload)
1931 * Returns: Pointer to the information element payload or %NULL if not found
1932 *
1933 * This function returns concatenated payload of possibly fragmented vendor
1934 * specific information elements in the scan result. The caller is responsible
1935 * for freeing the returned buffer.
1936 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1938 u32 vendor_type)
1939{
1940 struct wpabuf *buf;
1941 const u8 *end, *pos;
1942
1943 buf = wpabuf_alloc(res->ie_len);
1944 if (buf == NULL)
1945 return NULL;
1946
1947 pos = (const u8 *) (res + 1);
1948 end = pos + res->ie_len;
1949
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001950 while (end - pos > 1) {
1951 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 break;
1953 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1954 vendor_type == WPA_GET_BE32(&pos[2]))
1955 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1956 pos += 2 + pos[1];
1957 }
1958
1959 if (wpabuf_len(buf) == 0) {
1960 wpabuf_free(buf);
1961 buf = NULL;
1962 }
1963
1964 return buf;
1965}
1966
1967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001968/* Compare function for sorting scan results. Return >0 if @b is considered
1969 * better. */
1970static int wpa_scan_result_compar(const void *a, const void *b)
1971{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001972#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973 struct wpa_scan_res **_wa = (void *) a;
1974 struct wpa_scan_res **_wb = (void *) b;
1975 struct wpa_scan_res *wa = *_wa;
1976 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001977 int wpa_a, wpa_b;
1978 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979
1980 /* WPA/WPA2 support preferred */
1981 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1982 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1983 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1984 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1985
1986 if (wpa_b && !wpa_a)
1987 return 1;
1988 if (!wpa_b && wpa_a)
1989 return -1;
1990
1991 /* privacy support preferred */
1992 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1993 (wb->caps & IEEE80211_CAP_PRIVACY))
1994 return 1;
1995 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1996 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1997 return -1;
1998
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001999 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002000 snr_a_full = wa->snr;
2001 snr_a = MIN(wa->snr, GREAT_SNR);
2002 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002003 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002004 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002005 /* Level is not in dBm, so we can't calculate
2006 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002007 snr_a = snr_a_full = wa->level;
2008 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002009 }
2010
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002011 /* if SNR is close, decide by max rate or frequency band */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002012 if (snr_a && snr_b && abs(snr_b - snr_a) < 7) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002013 if (wa->est_throughput != wb->est_throughput)
Hai Shalom021b0b52019-04-10 11:17:58 -07002014 return (int) wb->est_throughput -
2015 (int) wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002016 }
2017 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2018 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002019 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2020 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 }
2022
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 * identical, use quality values since some drivers may only report
2025 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002026 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002028 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002029#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030}
2031
2032
2033#ifdef CONFIG_WPS
2034/* Compare function for sorting scan results when searching a WPS AP for
2035 * provisioning. Return >0 if @b is considered better. */
2036static int wpa_scan_result_wps_compar(const void *a, const void *b)
2037{
2038 struct wpa_scan_res **_wa = (void *) a;
2039 struct wpa_scan_res **_wb = (void *) b;
2040 struct wpa_scan_res *wa = *_wa;
2041 struct wpa_scan_res *wb = *_wb;
2042 int uses_wps_a, uses_wps_b;
2043 struct wpabuf *wps_a, *wps_b;
2044 int res;
2045
2046 /* Optimization - check WPS IE existence before allocated memory and
2047 * doing full reassembly. */
2048 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2049 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2050 if (uses_wps_a && !uses_wps_b)
2051 return -1;
2052 if (!uses_wps_a && uses_wps_b)
2053 return 1;
2054
2055 if (uses_wps_a && uses_wps_b) {
2056 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2057 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2058 res = wps_ap_priority_compar(wps_a, wps_b);
2059 wpabuf_free(wps_a);
2060 wpabuf_free(wps_b);
2061 if (res)
2062 return res;
2063 }
2064
2065 /*
2066 * Do not use current AP security policy as a sorting criteria during
2067 * WPS provisioning step since the AP may get reconfigured at the
2068 * completion of provisioning.
2069 */
2070
2071 /* all things being equal, use signal level; if signal levels are
2072 * identical, use quality values since some drivers may only report
2073 * that value and leave the signal level zero */
2074 if (wb->level == wa->level)
2075 return wb->qual - wa->qual;
2076 return wb->level - wa->level;
2077}
2078#endif /* CONFIG_WPS */
2079
2080
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002081static void dump_scan_res(struct wpa_scan_results *scan_res)
2082{
2083#ifndef CONFIG_NO_STDOUT_DEBUG
2084 size_t i;
2085
2086 if (scan_res->res == NULL || scan_res->num == 0)
2087 return;
2088
2089 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2090
2091 for (i = 0; i < scan_res->num; i++) {
2092 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002093 u8 *pos;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002094 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002095 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2096
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002097 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002098 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002099 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002100 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002101 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2102 r->flags,
2103 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002104 } else {
2105 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002106 "noise=%d level=%d flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002107 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002108 r->noise, r->level, r->flags, r->age,
2109 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002111 pos = (u8 *) (r + 1);
2112 if (r->ie_len)
2113 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2114 pos += r->ie_len;
2115 if (r->beacon_ie_len)
2116 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2117 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118 }
2119#endif /* CONFIG_NO_STDOUT_DEBUG */
2120}
2121
2122
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002123/**
2124 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2125 * @wpa_s: Pointer to wpa_supplicant data
2126 * @bssid: BSSID to check
2127 * Returns: 0 if the BSSID is filtered or 1 if not
2128 *
2129 * This function is used to filter out specific BSSIDs from scan reslts mainly
2130 * for testing purposes (SET bssid_filter ctrl_iface command).
2131 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002132int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2133 const u8 *bssid)
2134{
2135 size_t i;
2136
2137 if (wpa_s->bssid_filter == NULL)
2138 return 1;
2139
2140 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2141 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2142 ETH_ALEN) == 0)
2143 return 1;
2144 }
2145
2146 return 0;
2147}
2148
2149
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002150void filter_scan_res(struct wpa_supplicant *wpa_s,
2151 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002152{
2153 size_t i, j;
2154
2155 if (wpa_s->bssid_filter == NULL)
2156 return;
2157
2158 for (i = 0, j = 0; i < res->num; i++) {
2159 if (wpa_supplicant_filter_bssid_match(wpa_s,
2160 res->res[i]->bssid)) {
2161 res->res[j++] = res->res[i];
2162 } else {
2163 os_free(res->res[i]);
2164 res->res[i] = NULL;
2165 }
2166 }
2167
2168 if (res->num != j) {
2169 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2170 (int) (res->num - j));
2171 res->num = j;
2172 }
2173}
2174
2175
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002176void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002177{
2178 if (res->flags & WPA_SCAN_NOISE_INVALID) {
2179 res->noise = IS_5GHZ(res->freq) ?
2180 DEFAULT_NOISE_FLOOR_5GHZ :
2181 DEFAULT_NOISE_FLOOR_2GHZ;
2182 }
2183
2184 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2185 res->snr = res->level - res->noise;
2186 } else {
2187 /* Level is not in dBm, so we can't calculate
2188 * SNR. Just use raw level (units unknown). */
2189 res->snr = res->level;
2190 }
2191}
2192
2193
Hai Shalom899fcc72020-10-19 14:38:18 -07002194/* Minimum SNR required to achieve a certain bitrate. */
2195struct minsnr_bitrate_entry {
2196 int minsnr;
2197 unsigned int bitrate; /* in Mbps */
2198};
2199
2200/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2201static const int vht_mcs = 8;
2202
2203static const struct minsnr_bitrate_entry vht20_table[] = {
2204 { 0, 0 },
2205 { 2, 6500 }, /* HT20 MCS0 */
2206 { 5, 13000 }, /* HT20 MCS1 */
2207 { 9, 19500 }, /* HT20 MCS2 */
2208 { 11, 26000 }, /* HT20 MCS3 */
2209 { 15, 39000 }, /* HT20 MCS4 */
2210 { 18, 52000 }, /* HT20 MCS5 */
2211 { 20, 58500 }, /* HT20 MCS6 */
2212 { 25, 65000 }, /* HT20 MCS7 */
2213 { 29, 78000 }, /* VHT20 MCS8 */
2214 { -1, 78000 } /* SNR > 29 */
2215};
2216
2217static const struct minsnr_bitrate_entry vht40_table[] = {
2218 { 0, 0 },
2219 { 5, 13500 }, /* HT40 MCS0 */
2220 { 8, 27000 }, /* HT40 MCS1 */
2221 { 12, 40500 }, /* HT40 MCS2 */
2222 { 14, 54000 }, /* HT40 MCS3 */
2223 { 18, 81000 }, /* HT40 MCS4 */
2224 { 21, 108000 }, /* HT40 MCS5 */
2225 { 23, 121500 }, /* HT40 MCS6 */
2226 { 28, 135000 }, /* HT40 MCS7 */
2227 { 32, 162000 }, /* VHT40 MCS8 */
2228 { 34, 180000 }, /* VHT40 MCS9 */
2229 { -1, 180000 } /* SNR > 34 */
2230};
2231
2232static const struct minsnr_bitrate_entry vht80_table[] = {
2233 { 0, 0 },
2234 { 8, 29300 }, /* VHT80 MCS0 */
2235 { 11, 58500 }, /* VHT80 MCS1 */
2236 { 15, 87800 }, /* VHT80 MCS2 */
2237 { 17, 117000 }, /* VHT80 MCS3 */
2238 { 21, 175500 }, /* VHT80 MCS4 */
2239 { 24, 234000 }, /* VHT80 MCS5 */
2240 { 26, 263300 }, /* VHT80 MCS6 */
2241 { 31, 292500 }, /* VHT80 MCS7 */
2242 { 35, 351000 }, /* VHT80 MCS8 */
2243 { 37, 390000 }, /* VHT80 MCS9 */
2244 { -1, 390000 } /* SNR > 37 */
2245};
2246
2247
Hai Shalomfdcde762020-04-02 11:19:20 -07002248static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2249 int rate0, int rate1)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002250{
Hai Shalomfdcde762020-04-02 11:19:20 -07002251 return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002252}
2253
2254
Hai Shalom899fcc72020-10-19 14:38:18 -07002255static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2256 int snr, bool vht)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002257{
Hai Shalom899fcc72020-10-19 14:38:18 -07002258 const struct minsnr_bitrate_entry *prev, *entry = table;
2259
2260 while ((entry->minsnr != -1) &&
2261 (snr >= entry->minsnr) &&
2262 (vht || entry - table <= vht_mcs))
2263 entry++;
2264 if (entry == table)
2265 return entry->bitrate;
2266 prev = entry - 1;
2267 if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2268 return prev->bitrate;
2269 return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2270 entry->bitrate);
Hai Shalomfdcde762020-04-02 11:19:20 -07002271}
2272
2273
Hai Shalom899fcc72020-10-19 14:38:18 -07002274static unsigned int max_ht20_rate(int snr, bool vht)
Hai Shalomfdcde762020-04-02 11:19:20 -07002275{
Hai Shalom899fcc72020-10-19 14:38:18 -07002276 return max_rate(vht20_table, snr, vht);
2277}
2278
2279
2280static unsigned int max_ht40_rate(int snr, bool vht)
2281{
2282 return max_rate(vht40_table, snr, vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002283}
2284
2285
2286static unsigned int max_vht80_rate(int snr)
2287{
Hai Shalom899fcc72020-10-19 14:38:18 -07002288 return max_rate(vht80_table, snr, 1);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002289}
2290
Hai Shalomfdcde762020-04-02 11:19:20 -07002291
2292unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2293 const u8 *ies, size_t ies_len, int rate,
2294 int snr)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002295{
2296 enum local_hw_capab capab = wpa_s->hw_capab;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002297 unsigned int est, tmp;
Hai Shalomfdcde762020-04-02 11:19:20 -07002298 const u8 *ie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002299
2300 /* Limit based on estimated SNR */
2301 if (rate > 1 * 2 && snr < 1)
2302 rate = 1 * 2;
2303 else if (rate > 2 * 2 && snr < 4)
2304 rate = 2 * 2;
2305 else if (rate > 6 * 2 && snr < 5)
2306 rate = 6 * 2;
2307 else if (rate > 9 * 2 && snr < 6)
2308 rate = 9 * 2;
2309 else if (rate > 12 * 2 && snr < 7)
2310 rate = 12 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002311 else if (rate > 12 * 2 && snr < 8)
2312 rate = 14 * 2;
2313 else if (rate > 12 * 2 && snr < 9)
2314 rate = 16 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002315 else if (rate > 18 * 2 && snr < 10)
2316 rate = 18 * 2;
2317 else if (rate > 24 * 2 && snr < 11)
2318 rate = 24 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002319 else if (rate > 24 * 2 && snr < 12)
2320 rate = 27 * 2;
2321 else if (rate > 24 * 2 && snr < 13)
2322 rate = 30 * 2;
2323 else if (rate > 24 * 2 && snr < 14)
2324 rate = 33 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002325 else if (rate > 36 * 2 && snr < 15)
2326 rate = 36 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002327 else if (rate > 36 * 2 && snr < 16)
2328 rate = 39 * 2;
2329 else if (rate > 36 * 2 && snr < 17)
2330 rate = 42 * 2;
2331 else if (rate > 36 * 2 && snr < 18)
2332 rate = 45 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002333 else if (rate > 48 * 2 && snr < 19)
2334 rate = 48 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002335 else if (rate > 48 * 2 && snr < 20)
2336 rate = 51 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002337 else if (rate > 54 * 2 && snr < 21)
2338 rate = 54 * 2;
2339 est = rate * 500;
2340
2341 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002342 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002343 if (ie) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002344 tmp = max_ht20_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002345 if (tmp > est)
2346 est = tmp;
2347 }
2348 }
2349
2350 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002351 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002352 if (ie && ie[1] >= 2 &&
2353 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002354 tmp = max_ht40_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002355 if (tmp > est)
2356 est = tmp;
2357 }
2358 }
2359
2360 if (capab == CAPAB_VHT) {
2361 /* Use +1 to assume VHT is always faster than HT */
Hai Shalomfdcde762020-04-02 11:19:20 -07002362 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002363 if (ie) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002364 tmp = max_ht20_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002365 if (tmp > est)
2366 est = tmp;
2367
Hai Shalomfdcde762020-04-02 11:19:20 -07002368 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002369 if (ie && ie[1] >= 2 &&
2370 (ie[3] &
2371 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002372 tmp = max_ht40_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002373 if (tmp > est)
2374 est = tmp;
2375 }
2376
Hai Shalomfdcde762020-04-02 11:19:20 -07002377 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002378 if (ie && ie[1] >= 1 &&
2379 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2380 tmp = max_vht80_rate(snr) + 1;
2381 if (tmp > est)
2382 est = tmp;
2383 }
2384 }
2385 }
2386
Hai Shalomfdcde762020-04-02 11:19:20 -07002387 return est;
2388}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002389
Hai Shalomfdcde762020-04-02 11:19:20 -07002390
2391void scan_est_throughput(struct wpa_supplicant *wpa_s,
2392 struct wpa_scan_res *res)
2393{
2394 int rate; /* max legacy rate in 500 kb/s units */
2395 int snr = res->snr;
2396 const u8 *ies = (const void *) (res + 1);
2397 size_t ie_len = res->ie_len;
2398
2399 if (res->est_throughput)
2400 return;
2401
2402 /* Get maximum legacy rate */
2403 rate = wpa_scan_get_max_rate(res);
2404
2405 if (!ie_len)
2406 ie_len = res->beacon_ie_len;
2407 res->est_throughput =
2408 wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr);
2409
2410 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002411}
2412
2413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414/**
2415 * wpa_supplicant_get_scan_results - Get scan results
2416 * @wpa_s: Pointer to wpa_supplicant data
2417 * @info: Information about what was scanned or %NULL if not available
2418 * @new_scan: Whether a new scan was performed
2419 * Returns: Scan results, %NULL on failure
2420 *
2421 * This function request the current scan results from the driver and updates
2422 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2423 * results with wpa_scan_results_free().
2424 */
2425struct wpa_scan_results *
2426wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2427 struct scan_info *info, int new_scan)
2428{
2429 struct wpa_scan_results *scan_res;
2430 size_t i;
2431 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2432
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002433 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 if (scan_res == NULL) {
2435 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2436 return NULL;
2437 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002438 if (scan_res->fetch_time.sec == 0) {
2439 /*
2440 * Make sure we have a valid timestamp if the driver wrapper
2441 * does not set this.
2442 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002443 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002444 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002445 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002447 for (i = 0; i < scan_res->num; i++) {
2448 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2449
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002450 scan_snr(scan_res_item);
2451 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002452 }
2453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002455 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2457 "provisioning rules");
2458 compar = wpa_scan_result_wps_compar;
2459 }
2460#endif /* CONFIG_WPS */
2461
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002462 if (scan_res->res) {
2463 qsort(scan_res->res, scan_res->num,
2464 sizeof(struct wpa_scan_res *), compar);
2465 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002466 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467
Dmitry Shmidt29333592017-01-09 12:27:11 -08002468 if (wpa_s->ignore_post_flush_scan_res) {
2469 /* FLUSH command aborted an ongoing scan and these are the
2470 * results from the aborted scan. Do not process the results to
2471 * maintain flushed state. */
2472 wpa_dbg(wpa_s, MSG_DEBUG,
2473 "Do not update BSS table based on pending post-FLUSH scan results");
2474 wpa_s->ignore_post_flush_scan_res = 0;
2475 return scan_res;
2476 }
2477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478 wpa_bss_update_start(wpa_s);
2479 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002480 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2481 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 wpa_bss_update_end(wpa_s, info, new_scan);
2483
2484 return scan_res;
2485}
2486
2487
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002488/**
2489 * wpa_supplicant_update_scan_results - Update scan results from the driver
2490 * @wpa_s: Pointer to wpa_supplicant data
2491 * Returns: 0 on success, -1 on failure
2492 *
2493 * This function updates the BSS table within wpa_supplicant based on the
2494 * currently available scan results from the driver without requesting a new
2495 * scan. This is used in cases where the driver indicates an association
2496 * (including roaming within ESS) and wpa_supplicant does not yet have the
2497 * needed information to complete the connection (e.g., to perform validation
2498 * steps in 4-way handshake).
2499 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2501{
2502 struct wpa_scan_results *scan_res;
2503 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2504 if (scan_res == NULL)
2505 return -1;
2506 wpa_scan_results_free(scan_res);
2507
2508 return 0;
2509}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002510
2511
2512/**
2513 * scan_only_handler - Reports scan results
2514 */
2515void scan_only_handler(struct wpa_supplicant *wpa_s,
2516 struct wpa_scan_results *scan_res)
2517{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002518 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002519 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2520 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2521 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2522 wpa_s->manual_scan_id);
2523 wpa_s->manual_scan_use_id = 0;
2524 } else {
2525 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2526 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002527 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002528 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002529 if (wpa_s->scan_work) {
2530 struct wpa_radio_work *work = wpa_s->scan_work;
2531 wpa_s->scan_work = NULL;
2532 radio_work_done(work);
2533 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002534
2535 if (wpa_s->wpa_state == WPA_SCANNING)
2536 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002537}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002538
2539
2540int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2541{
2542 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2543}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002544
2545
2546struct wpa_driver_scan_params *
2547wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2548{
2549 struct wpa_driver_scan_params *params;
2550 size_t i;
2551 u8 *n;
2552
2553 params = os_zalloc(sizeof(*params));
2554 if (params == NULL)
2555 return NULL;
2556
2557 for (i = 0; i < src->num_ssids; i++) {
2558 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002559 n = os_memdup(src->ssids[i].ssid,
2560 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002561 if (n == NULL)
2562 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002563 params->ssids[i].ssid = n;
2564 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2565 }
2566 }
2567 params->num_ssids = src->num_ssids;
2568
2569 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002570 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002571 if (n == NULL)
2572 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002573 params->extra_ies = n;
2574 params->extra_ies_len = src->extra_ies_len;
2575 }
2576
2577 if (src->freqs) {
2578 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002579 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002580 if (params->freqs == NULL)
2581 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002582 }
2583
2584 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002585 params->filter_ssids = os_memdup(src->filter_ssids,
2586 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002587 src->num_filter_ssids);
2588 if (params->filter_ssids == NULL)
2589 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002590 params->num_filter_ssids = src->num_filter_ssids;
2591 }
2592
2593 params->filter_rssi = src->filter_rssi;
2594 params->p2p_probe = src->p2p_probe;
2595 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002596 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002597 params->duration = src->duration;
2598 params->duration_mandatory = src->duration_mandatory;
Hai Shalomce48b4a2018-09-05 11:41:35 -07002599 params->oce_scan = src->oce_scan;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002600
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002601 if (src->sched_scan_plans_num > 0) {
2602 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002603 os_memdup(src->sched_scan_plans,
2604 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002605 src->sched_scan_plans_num);
2606 if (!params->sched_scan_plans)
2607 goto failed;
2608
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002609 params->sched_scan_plans_num = src->sched_scan_plans_num;
2610 }
2611
Hai Shalomc3565922019-10-28 11:58:20 -07002612 if (src->mac_addr_rand &&
2613 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
2614 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002615
2616 if (src->bssid) {
2617 u8 *bssid;
2618
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002619 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002620 if (!bssid)
2621 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002622 params->bssid = bssid;
2623 }
2624
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002625 params->relative_rssi_set = src->relative_rssi_set;
2626 params->relative_rssi = src->relative_rssi;
2627 params->relative_adjust_band = src->relative_adjust_band;
2628 params->relative_adjust_rssi = src->relative_adjust_rssi;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002629 return params;
2630
2631failed:
2632 wpa_scan_free_params(params);
2633 return NULL;
2634}
2635
2636
2637void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2638{
2639 size_t i;
2640
2641 if (params == NULL)
2642 return;
2643
2644 for (i = 0; i < params->num_ssids; i++)
2645 os_free((u8 *) params->ssids[i].ssid);
2646 os_free((u8 *) params->extra_ies);
2647 os_free(params->freqs);
2648 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002649 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002650
2651 /*
2652 * Note: params->mac_addr_mask points to same memory allocation and
2653 * must not be freed separately.
2654 */
2655 os_free((u8 *) params->mac_addr);
2656
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002657 os_free((u8 *) params->bssid);
2658
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002659 os_free(params);
2660}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002661
2662
2663int wpas_start_pno(struct wpa_supplicant *wpa_s)
2664{
Hai Shalomfdcde762020-04-02 11:19:20 -07002665 int ret;
2666 size_t prio, i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002667 struct wpa_ssid *ssid;
2668 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002669 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002670 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002671
2672 if (!wpa_s->sched_scan_supported)
2673 return -1;
2674
Dmitry Shmidte4663042016-04-04 10:07:49 -07002675 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2676 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2677 else
2678 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2679 if (max_sched_scan_ssids < 1)
2680 return -1;
2681
Dmitry Shmidt98660862014-03-11 17:26:21 -07002682 if (wpa_s->pno || wpa_s->pno_sched_pending)
2683 return 0;
2684
2685 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002686 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002687 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2688 return -EAGAIN;
2689 }
2690
2691 if (wpa_s->wpa_state == WPA_SCANNING) {
2692 wpa_supplicant_cancel_scan(wpa_s);
2693 if (wpa_s->sched_scanning) {
2694 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2695 "ongoing sched scan");
2696 wpa_supplicant_cancel_sched_scan(wpa_s);
2697 wpa_s->pno_sched_pending = 1;
2698 return 0;
2699 }
2700 }
2701
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002702 if (wpa_s->sched_scan_stop_req) {
2703 wpa_printf(MSG_DEBUG,
2704 "Schedule PNO after previous sched scan has stopped");
2705 wpa_s->pno_sched_pending = 1;
2706 return 0;
2707 }
2708
Dmitry Shmidt98660862014-03-11 17:26:21 -07002709 os_memset(&params, 0, sizeof(params));
2710
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002711 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002712 ssid = wpa_s->conf->ssid;
2713 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002714 if (!wpas_network_disabled(wpa_s, ssid)) {
2715 num_match_ssid++;
2716 if (ssid->scan_ssid)
2717 num_ssid++;
2718 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002719 ssid = ssid->next;
2720 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002721
2722 if (num_match_ssid == 0) {
2723 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2724 return -1;
2725 }
2726
2727 if (num_match_ssid > num_ssid) {
2728 params.num_ssids++; /* wildcard */
2729 num_ssid++;
2730 }
2731
Dmitry Shmidte4663042016-04-04 10:07:49 -07002732 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002733 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07002734 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2735 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002736 }
2737
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002738 if (num_match_ssid > wpa_s->max_match_sets) {
2739 num_match_ssid = wpa_s->max_match_sets;
2740 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002741 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002742 params.filter_ssids = os_calloc(num_match_ssid,
2743 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002744 if (params.filter_ssids == NULL)
2745 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002746
Dmitry Shmidt98660862014-03-11 17:26:21 -07002747 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002748 prio = 0;
2749 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002750 while (ssid) {
2751 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002752 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2753 params.ssids[params.num_ssids].ssid =
2754 ssid->ssid;
2755 params.ssids[params.num_ssids].ssid_len =
2756 ssid->ssid_len;
2757 params.num_ssids++;
2758 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002759 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2760 ssid->ssid_len);
2761 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2762 params.num_filter_ssids++;
2763 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002764 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002765 break;
2766 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002767 if (ssid->pnext)
2768 ssid = ssid->pnext;
2769 else if (prio + 1 == wpa_s->conf->num_prio)
2770 break;
2771 else
2772 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002773 }
2774
2775 if (wpa_s->conf->filter_rssi)
2776 params.filter_rssi = wpa_s->conf->filter_rssi;
2777
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002778 if (wpa_s->sched_scan_plans_num) {
2779 params.sched_scan_plans = wpa_s->sched_scan_plans;
2780 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2781 } else {
2782 /* Set one scan plan that will run infinitely */
2783 if (wpa_s->conf->sched_scan_interval)
2784 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2785 else
2786 scan_plan.interval = 10;
2787
2788 scan_plan.iterations = 0;
2789 params.sched_scan_plans = &scan_plan;
2790 params.sched_scan_plans_num = 1;
2791 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002792
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002793 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
2794
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002795 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2796 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2797 params.freqs = wpa_s->manual_sched_scan_freqs;
2798 }
2799
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002800 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
Hai Shalomc3565922019-10-28 11:58:20 -07002801 wpa_s->wpa_state <= WPA_SCANNING)
2802 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002803
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002804 wpa_scan_set_relative_rssi_params(wpa_s, &params);
2805
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002806 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002807 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07002808 os_free(params.mac_addr);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002809 if (ret == 0)
2810 wpa_s->pno = 1;
2811 else
2812 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2813 return ret;
2814}
2815
2816
2817int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2818{
2819 int ret = 0;
2820
2821 if (!wpa_s->pno)
2822 return 0;
2823
2824 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002825 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002826
2827 wpa_s->pno = 0;
2828 wpa_s->pno_sched_pending = 0;
2829
2830 if (wpa_s->wpa_state == WPA_SCANNING)
2831 wpa_supplicant_req_scan(wpa_s, 0, 0);
2832
2833 return ret;
2834}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002835
2836
2837void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2838 unsigned int type)
2839{
2840 type &= MAC_ADDR_RAND_ALL;
2841 wpa_s->mac_addr_rand_enable &= ~type;
2842
2843 if (type & MAC_ADDR_RAND_SCAN) {
2844 os_free(wpa_s->mac_addr_scan);
2845 wpa_s->mac_addr_scan = NULL;
2846 }
2847
2848 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2849 os_free(wpa_s->mac_addr_sched_scan);
2850 wpa_s->mac_addr_sched_scan = NULL;
2851 }
2852
2853 if (type & MAC_ADDR_RAND_PNO) {
2854 os_free(wpa_s->mac_addr_pno);
2855 wpa_s->mac_addr_pno = NULL;
2856 }
2857}
2858
2859
2860int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2861 unsigned int type, const u8 *addr,
2862 const u8 *mask)
2863{
2864 u8 *tmp = NULL;
2865
Hai Shalom74f70d42019-02-11 14:42:39 -08002866 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
2867 wpa_printf(MSG_INFO,
2868 "scan: MAC randomization type %u != supported=%u",
2869 type, wpa_s->mac_addr_rand_supported);
2870 return -1;
2871 }
2872
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002873 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2874
2875 if (addr) {
2876 tmp = os_malloc(2 * ETH_ALEN);
2877 if (!tmp)
2878 return -1;
2879 os_memcpy(tmp, addr, ETH_ALEN);
2880 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2881 }
2882
2883 if (type == MAC_ADDR_RAND_SCAN) {
2884 wpa_s->mac_addr_scan = tmp;
2885 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2886 wpa_s->mac_addr_sched_scan = tmp;
2887 } else if (type == MAC_ADDR_RAND_PNO) {
2888 wpa_s->mac_addr_pno = tmp;
2889 } else {
2890 wpa_printf(MSG_INFO,
2891 "scan: Invalid MAC randomization type=0x%x",
2892 type);
2893 os_free(tmp);
2894 return -1;
2895 }
2896
2897 wpa_s->mac_addr_rand_enable |= type;
2898 return 0;
2899}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002900
2901
Hai Shalomc3565922019-10-28 11:58:20 -07002902int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
2903 unsigned int type, u8 *mask)
2904{
2905 const u8 *to_copy;
2906
2907 if ((wpa_s->mac_addr_rand_enable & type) != type)
2908 return -1;
2909
2910 if (type == MAC_ADDR_RAND_SCAN) {
2911 to_copy = wpa_s->mac_addr_scan;
2912 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2913 to_copy = wpa_s->mac_addr_sched_scan;
2914 } else if (type == MAC_ADDR_RAND_PNO) {
2915 to_copy = wpa_s->mac_addr_pno;
2916 } else {
2917 wpa_printf(MSG_DEBUG,
2918 "scan: Invalid MAC randomization type=0x%x",
2919 type);
2920 return -1;
2921 }
2922
2923 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
2924 return 0;
2925}
2926
2927
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002928int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2929{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002930 struct wpa_radio_work *work;
2931 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002932
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002933 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
2934 if (work->wpa_s != wpa_s || !work->started ||
2935 (os_strcmp(work->type, "scan") != 0 &&
2936 os_strcmp(work->type, "p2p-scan") != 0))
2937 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002938 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002939 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002940 }
2941
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002942 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
2943 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002944}
2945
2946
2947int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2948{
2949 struct sched_scan_plan *scan_plans = NULL;
2950 const char *token, *context = NULL;
2951 unsigned int num = 0;
2952
2953 if (!cmd)
2954 return -1;
2955
2956 if (!cmd[0]) {
2957 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2958 os_free(wpa_s->sched_scan_plans);
2959 wpa_s->sched_scan_plans = NULL;
2960 wpa_s->sched_scan_plans_num = 0;
2961 return 0;
2962 }
2963
2964 while ((token = cstr_token(cmd, " ", &context))) {
2965 int ret;
2966 struct sched_scan_plan *scan_plan, *n;
2967
2968 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2969 if (!n)
2970 goto fail;
2971
2972 scan_plans = n;
2973 scan_plan = &scan_plans[num];
2974 num++;
2975
2976 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2977 &scan_plan->iterations);
2978 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2979 wpa_printf(MSG_ERROR,
2980 "Invalid sched scan plan input: %s", token);
2981 goto fail;
2982 }
2983
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002984 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2985 wpa_printf(MSG_WARNING,
2986 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2987 num, scan_plan->interval,
2988 wpa_s->max_sched_scan_plan_interval);
2989 scan_plan->interval =
2990 wpa_s->max_sched_scan_plan_interval;
2991 }
2992
2993 if (ret == 1) {
2994 scan_plan->iterations = 0;
2995 break;
2996 }
2997
2998 if (!scan_plan->iterations) {
2999 wpa_printf(MSG_ERROR,
3000 "scan plan %u: Number of iterations cannot be zero",
3001 num);
3002 goto fail;
3003 }
3004
3005 if (scan_plan->iterations >
3006 wpa_s->max_sched_scan_plan_iterations) {
3007 wpa_printf(MSG_WARNING,
3008 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3009 num, scan_plan->iterations,
3010 wpa_s->max_sched_scan_plan_iterations);
3011 scan_plan->iterations =
3012 wpa_s->max_sched_scan_plan_iterations;
3013 }
3014
3015 wpa_printf(MSG_DEBUG,
3016 "scan plan %u: interval=%u iterations=%u",
3017 num, scan_plan->interval, scan_plan->iterations);
3018 }
3019
3020 if (!scan_plans) {
3021 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3022 goto fail;
3023 }
3024
3025 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3026 wpa_printf(MSG_ERROR,
3027 "All scan plans but the last must specify a number of iterations");
3028 goto fail;
3029 }
3030
3031 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3032 num, scan_plans[num - 1].interval);
3033
3034 if (num > wpa_s->max_sched_scan_plans) {
3035 wpa_printf(MSG_WARNING,
3036 "Too many scheduled scan plans (only %u supported)",
3037 wpa_s->max_sched_scan_plans);
3038 wpa_printf(MSG_WARNING,
3039 "Use only the first %u scan plans, and the last one (in infinite loop)",
3040 wpa_s->max_sched_scan_plans - 1);
3041 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3042 &scan_plans[num - 1], sizeof(*scan_plans));
3043 num = wpa_s->max_sched_scan_plans;
3044 }
3045
3046 os_free(wpa_s->sched_scan_plans);
3047 wpa_s->sched_scan_plans = scan_plans;
3048 wpa_s->sched_scan_plans_num = num;
3049
3050 return 0;
3051
3052fail:
3053 os_free(scan_plans);
3054 wpa_printf(MSG_ERROR, "invalid scan plans list");
3055 return -1;
3056}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003057
3058
3059/**
3060 * wpas_scan_reset_sched_scan - Reset sched_scan state
3061 * @wpa_s: Pointer to wpa_supplicant data
3062 *
3063 * This function is used to cancel a running scheduled scan and to reset an
3064 * internal scan state to continue with a regular scan on the following
3065 * wpa_supplicant_req_scan() calls.
3066 */
3067void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3068{
3069 wpa_s->normal_scans = 0;
3070 if (wpa_s->sched_scanning) {
3071 wpa_s->sched_scan_timed_out = 0;
3072 wpa_s->prev_sched_ssid = NULL;
3073 wpa_supplicant_cancel_sched_scan(wpa_s);
3074 }
3075}
3076
3077
3078void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3079{
3080 /* simulate timeout to restart the sched scan */
3081 wpa_s->sched_scan_timed_out = 1;
3082 wpa_s->prev_sched_ssid = NULL;
3083 wpa_supplicant_cancel_sched_scan(wpa_s);
3084}