blob: f9e927a628d4f84cce7bfdeac4f26a83691f1f3c [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
Hai Shaloma20dcd72022-02-04 13:43:00 -0800395#ifdef CONFIG_P2P
396static bool is_6ghz_supported(struct wpa_supplicant *wpa_s)
397{
398 struct hostapd_channel_data *chnl;
399 int i, j;
400
401 for (i = 0; i < wpa_s->hw.num_modes; i++) {
402 if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211A) {
403 chnl = wpa_s->hw.modes[i].channels;
404 for (j = 0; j < wpa_s->hw.modes[i].num_channels; j++) {
405 if (chnl[j].flag & HOSTAPD_CHAN_DISABLED)
406 continue;
407 if (is_6ghz_freq(chnl[j].freq))
408 return true;
409 }
410 }
411 }
412
413 return false;
414}
415#endif /* CONFIG_P2P */
416
417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418static void wpa_supplicant_optimize_freqs(
419 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
420{
421#ifdef CONFIG_P2P
422 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
423 wpa_s->go_params) {
424 /* Optimize provisioning state scan based on GO information */
425 if (wpa_s->p2p_in_provisioning < 5 &&
426 wpa_s->go_params->freq > 0) {
427 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
428 "preferred frequency %d MHz",
429 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800430 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800431 if (params->freqs)
432 params->freqs[0] = wpa_s->go_params->freq;
433 } else if (wpa_s->p2p_in_provisioning < 8 &&
434 wpa_s->go_params->freq_list[0]) {
435 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
436 "channels");
437 int_array_concat(&params->freqs,
438 wpa_s->go_params->freq_list);
439 if (params->freqs)
440 int_array_sort_unique(params->freqs);
441 }
442 wpa_s->p2p_in_provisioning++;
443 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700444
445 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
446 /*
447 * Optimize scan based on GO information during persistent
448 * group reinvocation
449 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700450 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700451 wpa_s->p2p_invite_go_freq > 0) {
452 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
453 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800454 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700455 if (params->freqs)
456 params->freqs[0] = wpa_s->p2p_invite_go_freq;
457 }
458 wpa_s->p2p_in_invitation++;
459 if (wpa_s->p2p_in_invitation > 20) {
460 /*
461 * This should not really happen since the variable is
462 * cleared on group removal, but if it does happen, make
463 * sure we do not get stuck in special invitation scan
464 * mode.
465 */
466 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
467 wpa_s->p2p_in_invitation = 0;
468 }
469 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800470#endif /* CONFIG_P2P */
471
472#ifdef CONFIG_WPS
473 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
474 /*
475 * Optimize post-provisioning scan based on channel used
476 * during provisioning.
477 */
478 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
479 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800480 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800481 if (params->freqs)
482 params->freqs[0] = wpa_s->wps_freq;
483 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800484 } else if (wpa_s->after_wps)
485 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800486
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800487 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
488 {
489 /* Optimize provisioning scan based on already known channel */
490 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
491 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800492 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800493 if (params->freqs)
494 params->freqs[0] = wpa_s->wps_freq;
495 wpa_s->known_wps_freq = 0; /* only do this once */
496 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800497#endif /* CONFIG_WPS */
498}
499
500
501#ifdef CONFIG_INTERWORKING
502static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
503 struct wpabuf *buf)
504{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800505 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
506 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
507 1 + ETH_ALEN);
508 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
509 /* No Venue Info */
510 if (!is_zero_ether_addr(wpa_s->conf->hessid))
511 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
512}
513#endif /* CONFIG_INTERWORKING */
514
515
Hai Shalomce48b4a2018-09-05 11:41:35 -0700516#ifdef CONFIG_MBO
517static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
518 struct wpabuf **ie)
519{
520 if (wpabuf_resize(ie, 5)) {
521 wpa_printf(MSG_DEBUG,
522 "Failed to allocate space for FILS Request Parameters element");
523 return;
524 }
525
526 /* FILS Request Parameters element */
527 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
528 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
529 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
530 /* Parameter control bitmap */
531 wpabuf_put_u8(*ie, 0);
532 /* Max Channel Time field - contains the value of MaxChannelTime
533 * parameter of the MLME-SCAN.request primitive represented in units of
534 * TUs, as an unsigned integer. A Max Channel Time field value of 255
535 * is used to indicate any duration of more than 254 TUs, or an
536 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
537 */
538 wpabuf_put_u8(*ie, 255);
539}
540#endif /* CONFIG_MBO */
541
542
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700543void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
544{
545 struct wpabuf *default_ies = NULL;
546 u8 ext_capab[18];
Hai Shalom60840252021-02-19 19:02:11 -0800547 int ext_capab_len, frame_id;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700548 enum wpa_driver_if_type type = WPA_IF_STATION;
549
550#ifdef CONFIG_P2P
551 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
552 type = WPA_IF_P2P_CLIENT;
553#endif /* CONFIG_P2P */
554
555 wpa_drv_get_ext_capa(wpa_s, type);
556
557 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
558 sizeof(ext_capab));
559 if (ext_capab_len > 0 &&
560 wpabuf_resize(&default_ies, ext_capab_len) == 0)
561 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
562
563#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -0700564 if (wpa_s->enable_oce & OCE_STA)
565 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700566 /* Send MBO and OCE capabilities */
567 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700568 wpas_mbo_scan_ie(wpa_s, default_ies);
569#endif /* CONFIG_MBO */
570
Hai Shalom60840252021-02-19 19:02:11 -0800571 if (type == WPA_IF_P2P_CLIENT)
572 frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
573 else
574 frame_id = VENDOR_ELEM_PROBE_REQ;
575
576 if (wpa_s->vendor_elem[frame_id]) {
577 size_t len;
578
579 len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
580 if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
581 wpabuf_put_buf(default_ies,
582 wpa_s->vendor_elem[frame_id]);
583 }
584
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700585 if (default_ies)
586 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
587 wpabuf_len(default_ies));
588 wpabuf_free(default_ies);
589}
590
591
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700592static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800593{
594 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700595 u8 ext_capab[18];
596 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800597#ifdef CONFIG_WPS
598 int wps = 0;
599 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
600#endif /* CONFIG_WPS */
601
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700602#ifdef CONFIG_P2P
603 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
604 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
605 else
606#endif /* CONFIG_P2P */
607 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
608
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700609 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
610 sizeof(ext_capab));
611 if (ext_capab_len > 0 &&
612 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
613 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
614
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800615#ifdef CONFIG_INTERWORKING
616 if (wpa_s->conf->interworking &&
617 wpabuf_resize(&extra_ie, 100) == 0)
618 wpas_add_interworking_elements(wpa_s, extra_ie);
619#endif /* CONFIG_INTERWORKING */
620
Hai Shalomce48b4a2018-09-05 11:41:35 -0700621#ifdef CONFIG_MBO
622 if (wpa_s->enable_oce & OCE_STA)
623 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
624#endif /* CONFIG_MBO */
625
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800626#ifdef CONFIG_WPS
627 wps = wpas_wps_in_use(wpa_s, &req_type);
628
629 if (wps) {
630 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700631 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
632 DEV_PW_DEFAULT,
633 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800634 wpa_s->wps->uuid, req_type,
635 0, NULL);
636 if (wps_ie) {
637 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
638 wpabuf_put_buf(extra_ie, wps_ie);
639 wpabuf_free(wps_ie);
640 }
641 }
642
643#ifdef CONFIG_P2P
644 if (wps) {
645 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
646 if (wpabuf_resize(&extra_ie, ielen) == 0)
647 wpas_p2p_scan_ie(wpa_s, extra_ie);
648 }
649#endif /* CONFIG_P2P */
650
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800651 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800653#endif /* CONFIG_WPS */
654
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700655#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -0800656 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
657 wpas_hs20_add_indication(extra_ie, -1, 0);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700658#endif /* CONFIG_HS20 */
659
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800660#ifdef CONFIG_FST
661 if (wpa_s->fst_ies &&
662 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
663 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
664#endif /* CONFIG_FST */
665
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800666#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700667 /* Send MBO and OCE capabilities */
668 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800669 wpas_mbo_scan_ie(wpa_s, extra_ie);
670#endif /* CONFIG_MBO */
671
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700672 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
673 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
674
675 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
676 wpabuf_put_buf(extra_ie, buf);
677 }
678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800679 return extra_ie;
680}
681
682
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800683#ifdef CONFIG_P2P
684
685/*
686 * Check whether there are any enabled networks or credentials that could be
687 * used for a non-P2P connection.
688 */
689static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
690{
691 struct wpa_ssid *ssid;
692
693 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
694 if (wpas_network_disabled(wpa_s, ssid))
695 continue;
696 if (!ssid->p2p_group)
697 return 1;
698 }
699
700 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
701 wpa_s->conf->auto_interworking)
702 return 1;
703
704 return 0;
705}
706
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700707#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800708
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800709
Hai Shalom60840252021-02-19 19:02:11 -0800710int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
711 enum hostapd_hw_mode band,
712 struct wpa_driver_scan_params *params, bool is_6ghz)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700713{
714 /* Include only supported channels for the specified band */
715 struct hostapd_hw_modes *mode;
Hai Shalom60840252021-02-19 19:02:11 -0800716 int num_chans = 0;
717 int *freqs, i;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700718
Hai Shalomfdcde762020-04-02 11:19:20 -0700719 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
Hai Shalom60840252021-02-19 19:02:11 -0800720 if (!mode)
721 return -1;
722
723 if (params->freqs) {
724 while (params->freqs[num_chans])
725 num_chans++;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700726 }
727
Hai Shalom60840252021-02-19 19:02:11 -0800728 freqs = os_realloc(params->freqs,
729 (num_chans + mode->num_channels + 1) * sizeof(int));
730 if (!freqs)
731 return -1;
732
733 params->freqs = freqs;
734 for (i = 0; i < mode->num_channels; i++) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700735 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
736 continue;
Hai Shalom60840252021-02-19 19:02:11 -0800737 params->freqs[num_chans++] = mode->channels[i].freq;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700738 }
Hai Shalom60840252021-02-19 19:02:11 -0800739 params->freqs[num_chans] = 0;
740
741 return 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700742}
743
744
745static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
746 struct wpa_driver_scan_params *params)
747{
748 if (wpa_s->hw.modes == NULL)
749 return; /* unknown what channels the driver supports */
750 if (params->freqs)
751 return; /* already using a limited channel set */
Hai Shalom60840252021-02-19 19:02:11 -0800752
753 if (wpa_s->setband_mask & WPA_SETBAND_5G)
754 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800755 false);
Hai Shalom60840252021-02-19 19:02:11 -0800756 if (wpa_s->setband_mask & WPA_SETBAND_2G)
757 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800758 false);
Hai Shalom60840252021-02-19 19:02:11 -0800759 if (wpa_s->setband_mask & WPA_SETBAND_6G)
760 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800761 true);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700762}
763
764
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800765static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
766 struct wpa_driver_scan_params *params,
767 size_t max_ssids, const u8 *ssid, size_t ssid_len)
768{
769 unsigned int j;
770
771 for (j = 0; j < params->num_ssids; j++) {
772 if (params->ssids[j].ssid_len == ssid_len &&
773 params->ssids[j].ssid &&
774 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
775 return; /* already in the list */
776 }
777
778 if (params->num_ssids + 1 > max_ssids) {
779 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
780 return;
781 }
782
783 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
784 wpa_ssid_txt(ssid, ssid_len));
785
786 params->ssids[params->num_ssids].ssid = ssid;
787 params->ssids[params->num_ssids].ssid_len = ssid_len;
788 params->num_ssids++;
789}
790
791
792static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
793 struct wpa_driver_scan_params *params,
794 struct wpa_ssid *ssid, size_t max_ssids)
795{
796#ifdef CONFIG_OWE
797 struct wpa_bss *bss;
798
799 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
800 return;
801
802 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
803 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
804
805 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
806 const u8 *owe, *pos, *end;
807 const u8 *owe_ssid;
808 size_t owe_ssid_len;
809
810 if (bss->ssid_len != ssid->ssid_len ||
811 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
812 continue;
813
814 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
815 if (!owe || owe[1] < 4)
816 continue;
817
818 pos = owe + 6;
819 end = owe + 2 + owe[1];
820
821 /* Must include BSSID and ssid_len */
822 if (end - pos < ETH_ALEN + 1)
823 return;
824
825 /* Skip BSSID */
826 pos += ETH_ALEN;
827 owe_ssid_len = *pos++;
828 owe_ssid = pos;
829
830 if ((size_t) (end - pos) < owe_ssid_len ||
831 owe_ssid_len > SSID_MAX_LEN)
832 return;
833
834 wpa_printf(MSG_DEBUG,
835 "OWE: scan_ssids: transition mode OWE ssid=%s",
836 wpa_ssid_txt(owe_ssid, owe_ssid_len));
837
838 wpa_add_scan_ssid(wpa_s, params, max_ssids,
839 owe_ssid, owe_ssid_len);
840 return;
841 }
842#endif /* CONFIG_OWE */
843}
844
845
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700846static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
847 struct wpa_driver_scan_params *params,
848 size_t max_ssids)
849{
850 unsigned int i;
851 struct wpa_ssid *ssid;
852
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700853 /*
854 * For devices with max_ssids greater than 1, leave the last slot empty
855 * for adding the wildcard scan entry.
856 */
857 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
858
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700859 for (i = 0; i < wpa_s->scan_id_count; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700860 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800861 if (!ssid)
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700862 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800863 if (ssid->scan_ssid)
864 wpa_add_scan_ssid(wpa_s, params, max_ssids,
865 ssid->ssid, ssid->ssid_len);
866 /*
867 * Also add the SSID of the OWE BSS, to allow discovery of
868 * transition mode APs more quickly.
869 */
870 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700871 }
872
873 wpa_s->scan_id_count = 0;
874}
875
876
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700877static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
878 struct wpa_driver_scan_params *params,
879 size_t max_ssids)
880{
881 unsigned int i;
882
883 if (wpa_s->ssids_from_scan_req == NULL ||
884 wpa_s->num_ssids_from_scan_req == 0)
885 return 0;
886
887 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
888 wpa_s->num_ssids_from_scan_req = max_ssids;
889 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
890 (unsigned int) max_ssids);
891 }
892
893 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
894 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
895 params->ssids[i].ssid_len =
896 wpa_s->ssids_from_scan_req[i].ssid_len;
897 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
898 params->ssids[i].ssid,
899 params->ssids[i].ssid_len);
900 }
901
902 params->num_ssids = wpa_s->num_ssids_from_scan_req;
903 wpa_s->num_ssids_from_scan_req = 0;
904 return 1;
905}
906
907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
909{
910 struct wpa_supplicant *wpa_s = eloop_ctx;
911 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800912 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700913 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700915 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800917 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918
Dmitry Shmidt29333592017-01-09 12:27:11 -0800919 wpa_s->ignore_post_flush_scan_res = 0;
920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
922 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
923 return;
924 }
925
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800926 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700927 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
929 return;
930 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800931
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700932 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800933 /*
934 * If we are already in scanning state, we shall reschedule the
935 * the incoming scan request.
936 */
937 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
938 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700939 return;
940 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800941
Dmitry Shmidt04949592012-07-19 12:16:46 -0700942 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800943 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
945 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
946 return;
947 }
948
949 if (wpa_s->conf->ap_scan != 0 &&
950 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
951 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
952 "overriding ap_scan configuration");
953 wpa_s->conf->ap_scan = 0;
954 wpas_notify_ap_scan_changed(wpa_s);
955 }
956
957 if (wpa_s->conf->ap_scan == 0) {
958 wpa_supplicant_gen_assoc_event(wpa_s);
959 return;
960 }
961
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800962 ssid = NULL;
963 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
964 wpa_s->connect_without_scan) {
965 connect_without_scan = 1;
966 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
967 if (ssid == wpa_s->connect_without_scan)
968 break;
969 }
970 }
971
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800972 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800973 if (p2p_in_prog && p2p_in_prog != 2 &&
974 (!ssid ||
975 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800976 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
977 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700978 return;
979 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700980
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800981 /*
982 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
983 * used for changing modes inside wpa_supplicant (roaming,
984 * auto-reconnect, etc). Discarding the scan might hurt these processes.
985 * The normal use case for PNO is to suspend the host immediately after
986 * starting PNO, so the periodic 100 ms attempts to run the scan do not
987 * normally happen in practice multiple times, i.e., this is simply
988 * restarting scanning once the host is woken up and PNO stopped.
989 */
990 if (wpa_s->pno || wpa_s->pno_sched_pending) {
991 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
992 wpa_supplicant_req_scan(wpa_s, 0, 100000);
993 return;
994 }
995
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800996 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 max_ssids = 1;
998 else {
999 max_ssids = wpa_s->max_scan_ssids;
1000 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1001 max_ssids = WPAS_MAX_SCAN_SSIDS;
1002 }
1003
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001004 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001005 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001007 if (connect_without_scan) {
1008 wpa_s->connect_without_scan = NULL;
1009 if (ssid) {
1010 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1011 "without scan step");
1012 wpa_supplicant_associate(wpa_s, NULL, ssid);
1013 return;
1014 }
1015 }
1016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 os_memset(&params, 0, sizeof(params));
1018
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001019 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1021 wpa_s->wpa_state == WPA_INACTIVE)
1022 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1023
Dmitry Shmidt04949592012-07-19 12:16:46 -07001024 /*
1025 * If autoscan has set its own scanning parameters
1026 */
1027 if (wpa_s->autoscan_params != NULL) {
1028 scan_params = wpa_s->autoscan_params;
1029 goto scan;
1030 }
1031
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001032 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1033 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1034 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1035 goto ssid_list_set;
1036 }
1037
Dmitry Shmidt04949592012-07-19 12:16:46 -07001038#ifdef CONFIG_P2P
1039 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001040 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001041 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1042 wpa_s->p2p_in_provisioning,
1043 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001044 params.ssids[0].ssid = wpa_s->go_params->ssid;
1045 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1046 params.num_ssids = 1;
1047 goto ssid_list_set;
1048 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07001049
1050 if (wpa_s->p2p_in_invitation) {
1051 if (wpa_s->current_ssid) {
1052 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1053 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1054 params.ssids[0].ssid_len =
1055 wpa_s->current_ssid->ssid_len;
1056 params.num_ssids = 1;
1057 } else {
1058 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1059 }
1060 goto ssid_list_set;
1061 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001062#endif /* CONFIG_P2P */
1063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 /* Find the starting point from which to continue scanning */
1065 ssid = wpa_s->conf->ssid;
1066 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1067 while (ssid) {
1068 if (ssid == wpa_s->prev_scan_ssid) {
1069 ssid = ssid->next;
1070 break;
1071 }
1072 ssid = ssid->next;
1073 }
1074 }
1075
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001076 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001077#ifdef CONFIG_AP
1078 !wpa_s->ap_iface &&
1079#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001080 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001081 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001082 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 wpa_supplicant_assoc_try(wpa_s, ssid);
1084 return;
1085 } else if (wpa_s->conf->ap_scan == 2) {
1086 /*
1087 * User-initiated scan request in ap_scan == 2; scan with
1088 * wildcard SSID.
1089 */
1090 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001091 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1092 /*
1093 * Perform single-channel single-SSID scan for
1094 * reassociate-to-same-BSS operation.
1095 */
1096 /* Setup SSID */
1097 ssid = wpa_s->current_ssid;
1098 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1099 ssid->ssid, ssid->ssid_len);
1100 params.ssids[0].ssid = ssid->ssid;
1101 params.ssids[0].ssid_len = ssid->ssid_len;
1102 params.num_ssids = 1;
1103
1104 /*
1105 * Allocate memory for frequency array, allocate one extra
1106 * slot for the zero-terminator.
1107 */
1108 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001109 if (params.freqs) {
1110 params.freqs[0] = wpa_s->assoc_freq;
1111 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001112 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07001113
1114 /*
1115 * Reset the reattach flag so that we fall back to full scan if
1116 * this scan fails.
1117 */
1118 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119 } else {
1120 struct wpa_ssid *start = ssid, *tssid;
1121 int freqs_set = 0;
1122 if (ssid == NULL && max_ssids > 1)
1123 ssid = wpa_s->conf->ssid;
1124 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001125 if (!wpas_network_disabled(wpa_s, ssid) &&
1126 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1128 ssid->ssid, ssid->ssid_len);
1129 params.ssids[params.num_ssids].ssid =
1130 ssid->ssid;
1131 params.ssids[params.num_ssids].ssid_len =
1132 ssid->ssid_len;
1133 params.num_ssids++;
1134 if (params.num_ssids + 1 >= max_ssids)
1135 break;
1136 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001137
1138 if (!wpas_network_disabled(wpa_s, ssid)) {
1139 /*
1140 * Also add the SSID of the OWE BSS, to allow
1141 * discovery of transition mode APs more
1142 * quickly.
1143 */
1144 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1145 max_ssids);
1146 }
1147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 ssid = ssid->next;
1149 if (ssid == start)
1150 break;
1151 if (ssid == NULL && max_ssids > 1 &&
1152 start != wpa_s->conf->ssid)
1153 ssid = wpa_s->conf->ssid;
1154 }
1155
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001156 if (wpa_s->scan_id_count &&
1157 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1158 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1159
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001160 for (tssid = wpa_s->conf->ssid;
1161 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1162 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001163 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07001165 if (((params.freqs || !freqs_set) &&
1166 tssid->scan_freq) &&
1167 int_array_len(params.freqs) < 100) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 int_array_concat(&params.freqs,
1169 tssid->scan_freq);
1170 } else {
1171 os_free(params.freqs);
1172 params.freqs = NULL;
1173 }
1174 freqs_set = 1;
1175 }
1176 int_array_sort_unique(params.freqs);
1177 }
1178
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001179 if (ssid && max_ssids == 1) {
1180 /*
1181 * If the driver is limited to 1 SSID at a time interleave
1182 * wildcard SSID scans with specific SSID scans to avoid
1183 * waiting a long time for a wildcard scan.
1184 */
1185 if (!wpa_s->prev_scan_wildcard) {
1186 params.ssids[0].ssid = NULL;
1187 params.ssids[0].ssid_len = 0;
1188 wpa_s->prev_scan_wildcard = 1;
1189 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1190 "wildcard SSID (Interleave with specific)");
1191 } else {
1192 wpa_s->prev_scan_ssid = ssid;
1193 wpa_s->prev_scan_wildcard = 0;
1194 wpa_dbg(wpa_s, MSG_DEBUG,
1195 "Starting AP scan for specific SSID: %s",
1196 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001198 } else if (ssid) {
1199 /* max_ssids > 1 */
1200
1201 wpa_s->prev_scan_ssid = ssid;
1202 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1203 "the scan request");
1204 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001205 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1206 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1207 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001208 } else if (wpa_s->conf->passive_scan) {
1209 wpa_dbg(wpa_s, MSG_DEBUG,
1210 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 } else {
1212 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1213 params.num_ssids++;
1214 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1215 "SSID");
1216 }
1217
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001218ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001219 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001220 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001222 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001223 wpa_s->manual_scan_only_new) {
1224 wpa_printf(MSG_DEBUG,
1225 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001226 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001227 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001228
1229 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1230 wpa_s->manual_scan_freqs) {
1231 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1232 params.freqs = wpa_s->manual_scan_freqs;
1233 wpa_s->manual_scan_freqs = NULL;
1234 }
1235
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001236 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1237 wpa_dbg(wpa_s, MSG_DEBUG,
1238 "Limit select_network scan to specified channels");
1239 params.freqs = wpa_s->select_network_scan_freqs;
1240 wpa_s->select_network_scan_freqs = NULL;
1241 }
1242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1244 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1245 "generated frequency list");
1246 params.freqs = wpa_s->next_scan_freqs;
1247 } else
1248 os_free(wpa_s->next_scan_freqs);
1249 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001250 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001252 /* See if user specified frequencies. If so, scan only those. */
Hai Shalom60840252021-02-19 19:02:11 -08001253 if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1254 wpa_s->conf->initial_freq_list && !params.freqs) {
1255 wpa_dbg(wpa_s, MSG_DEBUG,
1256 "Optimize scan based on conf->initial_freq_list");
1257 int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1258 } else if (wpa_s->conf->freq_list && !params.freqs) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001259 wpa_dbg(wpa_s, MSG_DEBUG,
1260 "Optimize scan based on conf->freq_list");
1261 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1262 }
1263
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001264 /* Use current associated channel? */
1265 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001266 unsigned int num = wpa_s->num_multichan_concurrent;
1267
1268 params.freqs = os_calloc(num + 1, sizeof(int));
1269 if (params.freqs) {
1270 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1271 if (num > 0) {
1272 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1273 "current operating channels since "
1274 "scan_cur_freq is enabled");
1275 } else {
1276 os_free(params.freqs);
1277 params.freqs = NULL;
1278 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001279 }
1280 }
1281
Hai Shalomce48b4a2018-09-05 11:41:35 -07001282#ifdef CONFIG_MBO
1283 if (wpa_s->enable_oce & OCE_STA)
1284 params.oce_scan = 1;
1285#endif /* CONFIG_MBO */
1286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1288 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001289 if (extra_ie) {
1290 params.extra_ies = wpabuf_head(extra_ie);
1291 params.extra_ies_len = wpabuf_len(extra_ie);
1292 }
1293
1294#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001295 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001296 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001297 /*
1298 * The interface may not yet be in P2P mode, so we have to
1299 * explicitly request P2P probe to disable CCK rates.
1300 */
1301 params.p2p_probe = 1;
1302 }
1303#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001305 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001306 wpa_s->wpa_state <= WPA_SCANNING)
1307 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001308
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001309 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1310 struct wpa_bss *bss;
1311
1312 params.bssid = wpa_s->next_scan_bssid;
1313 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
Hai Shalomfdcde762020-04-02 11:19:20 -07001314 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1315 bss && bss->ssid_len && params.num_ssids == 1 &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001316 params.ssids[0].ssid_len == 0) {
1317 params.ssids[0].ssid = bss->ssid;
1318 params.ssids[0].ssid_len = bss->ssid_len;
1319 wpa_dbg(wpa_s, MSG_DEBUG,
1320 "Scan a previously specified BSSID " MACSTR
1321 " and SSID %s",
1322 MAC2STR(params.bssid),
1323 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1324 } else {
1325 wpa_dbg(wpa_s, MSG_DEBUG,
1326 "Scan a previously specified BSSID " MACSTR,
1327 MAC2STR(params.bssid));
1328 }
1329 }
1330
Sunil8cd6f4d2022-06-28 18:40:46 +00001331 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1332 wpa_s->manual_non_coloc_6ghz) {
1333 wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1334 params.non_coloc_6ghz = 1;
1335 }
1336
Dmitry Shmidt04949592012-07-19 12:16:46 -07001337 scan_params = &params;
1338
1339scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001340#ifdef CONFIG_P2P
1341 /*
1342 * If the driver does not support multi-channel concurrency and a
1343 * virtual interface that shares the same radio with the wpa_s interface
1344 * is operating there may not be need to scan other channels apart from
1345 * the current operating channel on the other virtual interface. Filter
1346 * out other channels in case we are trying to find a connection for a
1347 * station interface when we are not configured to prefer station
1348 * connection and a concurrent operation is already in process.
1349 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001350 if (wpa_s->scan_for_connection &&
1351 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001352 !scan_params->freqs && !params.freqs &&
1353 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001354 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1355 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001356 unsigned int num = wpa_s->num_multichan_concurrent;
1357
1358 params.freqs = os_calloc(num + 1, sizeof(int));
1359 if (params.freqs) {
1360 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1361 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1362 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1363 } else {
1364 os_free(params.freqs);
1365 params.freqs = NULL;
1366 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001367 }
1368 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001369
1370 if (!params.freqs &&
1371 (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning) &&
1372 !is_p2p_allow_6ghz(wpa_s->global->p2p) &&
1373 is_6ghz_supported(wpa_s)) {
1374 int i;
1375
Jimmy Chenafee24e2022-06-17 22:53:15 +08001376 /* Exclude 6 GHz channels from the full scan for P2P connection
Hai Shaloma20dcd72022-02-04 13:43:00 -08001377 * since the 6 GHz band is disabled for P2P uses. */
1378 wpa_printf(MSG_DEBUG,
1379 "P2P: 6 GHz disabled - update the scan frequency list");
Jimmy Chenafee24e2022-06-17 22:53:15 +08001380 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params, false);
1381 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params, false);
1382 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD, &params, false);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001383 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001384#endif /* CONFIG_P2P */
1385
Dmitry Shmidt04949592012-07-19 12:16:46 -07001386 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001388 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1389 !wpa_s->manual_scan_freqs) {
1390 /* Restore manual_scan_freqs for the next attempt */
1391 wpa_s->manual_scan_freqs = params.freqs;
1392 params.freqs = NULL;
1393 }
1394
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001395 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 os_free(params.freqs);
1397 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001398 os_free(params.mac_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399
1400 if (ret) {
1401 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001402 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1403 wpa_supplicant_set_state(wpa_s,
1404 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001405 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001406 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001407 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001408 } else {
1409 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001410#ifdef CONFIG_INTERWORKING
1411 wpa_s->interworking_fast_assoc_tried = 0;
1412#endif /* CONFIG_INTERWORKING */
Hai Shalomfdcde762020-04-02 11:19:20 -07001413 wpa_s->next_scan_bssid_wildcard_ssid = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001414 if (params.bssid)
1415 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 }
1417}
1418
1419
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001420void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1421{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001422 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001423 int cancelled;
1424
1425 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1426 &remaining);
1427
1428 new_int.sec = sec;
1429 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001430 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001431 new_int.sec = remaining.sec;
1432 new_int.usec = remaining.usec;
1433 }
1434
Dmitry Shmidt051af732013-10-22 13:52:46 -07001435 if (cancelled) {
1436 eloop_register_timeout(new_int.sec, new_int.usec,
1437 wpa_supplicant_scan, wpa_s, NULL);
1438 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001439 wpa_s->scan_interval = sec;
1440}
1441
1442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443/**
1444 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1445 * @wpa_s: Pointer to wpa_supplicant data
1446 * @sec: Number of seconds after which to scan
1447 * @usec: Number of microseconds after which to scan
1448 *
1449 * This function is used to schedule a scan for neighboring access points after
1450 * the specified time.
1451 */
1452void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1453{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001454 int res;
1455
1456 if (wpa_s->p2p_mgmt) {
1457 wpa_dbg(wpa_s, MSG_DEBUG,
1458 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1459 sec, usec);
1460 return;
1461 }
1462
1463 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1464 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001465 if (res == 1) {
1466 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001467 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001468 } else if (res == 0) {
1469 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1470 sec, usec);
1471 } else {
1472 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1473 sec, usec);
1474 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476}
1477
1478
1479/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001480 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1481 * @wpa_s: Pointer to wpa_supplicant data
1482 * @sec: Number of seconds after which to scan
1483 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001484 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001485 *
1486 * This function is used to schedule periodic scans for neighboring
1487 * access points after the specified time.
1488 */
1489int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1490 int sec, int usec)
1491{
1492 if (!wpa_s->sched_scan_supported)
1493 return -1;
1494
1495 eloop_register_timeout(sec, usec,
1496 wpa_supplicant_delayed_sched_scan_timeout,
1497 wpa_s, NULL);
1498
1499 return 0;
1500}
1501
1502
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001503static void
1504wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1505 struct wpa_driver_scan_params *params)
1506{
1507 if (wpa_s->wpa_state != WPA_COMPLETED ||
1508 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1509 wpa_s->srp.relative_rssi_set == 0)
1510 return;
1511
1512 params->relative_rssi_set = 1;
1513 params->relative_rssi = wpa_s->srp.relative_rssi;
1514
1515 if (wpa_s->srp.relative_adjust_rssi == 0)
1516 return;
1517
1518 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1519 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1520}
1521
1522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001523/**
1524 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1525 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001526 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001527 *
1528 * This function is used to schedule periodic scans for neighboring
1529 * access points repeating the scan continuously.
1530 */
1531int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1532{
1533 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001534 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001535 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001536 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001537 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001538 int ret;
1539 unsigned int max_sched_scan_ssids;
1540 int wildcard = 0;
1541 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001542 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001543
1544 if (!wpa_s->sched_scan_supported)
1545 return -1;
1546
1547 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1548 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1549 else
1550 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001551 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001552 return -1;
1553
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001554 wpa_s->sched_scan_stop_req = 0;
1555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001556 if (wpa_s->sched_scanning) {
1557 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1558 return 0;
1559 }
1560
1561 need_ssids = 0;
1562 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001563 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001564 /* Use wildcard SSID to find this network */
1565 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001566 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1567 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001568 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001569
1570#ifdef CONFIG_WPS
1571 if (!wpas_network_disabled(wpa_s, ssid) &&
1572 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1573 /*
1574 * Normal scan is more reliable and faster for WPS
1575 * operations and since these are for short periods of
1576 * time, the benefit of trying to use sched_scan would
1577 * be limited.
1578 */
1579 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1580 "sched_scan for WPS");
1581 return -1;
1582 }
1583#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 }
1585 if (wildcard)
1586 need_ssids++;
1587
1588 if (wpa_s->normal_scans < 3 &&
1589 (need_ssids <= wpa_s->max_scan_ssids ||
1590 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1591 /*
1592 * When normal scan can speed up operations, use that for the
1593 * first operations before starting the sched_scan to allow
1594 * user space sleep more. We do this only if the normal scan
1595 * has functionality that is suitable for this or if the
1596 * sched_scan does not have better support for multiple SSIDs.
1597 */
1598 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1599 "sched_scan for initial scans (normal_scans=%d)",
1600 wpa_s->normal_scans);
1601 return -1;
1602 }
1603
1604 os_memset(&params, 0, sizeof(params));
1605
1606 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001607 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608 sizeof(struct wpa_driver_scan_filter));
1609
1610 prev_state = wpa_s->wpa_state;
1611 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1612 wpa_s->wpa_state == WPA_INACTIVE)
1613 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1614
Dmitry Shmidt04949592012-07-19 12:16:46 -07001615 if (wpa_s->autoscan_params != NULL) {
1616 scan_params = wpa_s->autoscan_params;
1617 goto scan;
1618 }
1619
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001620 /* Find the starting point from which to continue scanning */
1621 ssid = wpa_s->conf->ssid;
1622 if (wpa_s->prev_sched_ssid) {
1623 while (ssid) {
1624 if (ssid == wpa_s->prev_sched_ssid) {
1625 ssid = ssid->next;
1626 break;
1627 }
1628 ssid = ssid->next;
1629 }
1630 }
1631
1632 if (!ssid || !wpa_s->prev_sched_ssid) {
1633 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001634 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1635 wpa_s->first_sched_scan = 1;
1636 ssid = wpa_s->conf->ssid;
1637 wpa_s->prev_sched_ssid = ssid;
1638 }
1639
1640 if (wildcard) {
1641 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1642 params.num_ssids++;
1643 }
1644
1645 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001646 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001647 goto next;
1648
1649 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1650 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1651 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1652 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1653 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1654 ssid->ssid, ssid->ssid_len);
1655 params.filter_ssids[params.num_filter_ssids].ssid_len =
1656 ssid->ssid_len;
1657 params.num_filter_ssids++;
1658 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1659 {
1660 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1661 "filter for sched_scan - drop filter");
1662 os_free(params.filter_ssids);
1663 params.filter_ssids = NULL;
1664 params.num_filter_ssids = 0;
1665 }
1666
1667 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1668 if (params.num_ssids == max_sched_scan_ssids)
1669 break; /* only room for broadcast SSID */
1670 wpa_dbg(wpa_s, MSG_DEBUG,
1671 "add to active scan ssid: %s",
1672 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1673 params.ssids[params.num_ssids].ssid =
1674 ssid->ssid;
1675 params.ssids[params.num_ssids].ssid_len =
1676 ssid->ssid_len;
1677 params.num_ssids++;
1678 if (params.num_ssids >= max_sched_scan_ssids) {
1679 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001680 do {
1681 ssid = ssid->next;
1682 } while (ssid &&
1683 (wpas_network_disabled(wpa_s, ssid) ||
1684 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001685 break;
1686 }
1687 }
1688
1689 next:
1690 wpa_s->prev_sched_ssid = ssid;
1691 ssid = ssid->next;
1692 }
1693
1694 if (params.num_filter_ssids == 0) {
1695 os_free(params.filter_ssids);
1696 params.filter_ssids = NULL;
1697 }
1698
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001699 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1700 if (extra_ie) {
1701 params.extra_ies = wpabuf_head(extra_ie);
1702 params.extra_ies_len = wpabuf_len(extra_ie);
1703 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001704
Dmitry Shmidt18463232014-01-24 12:29:41 -08001705 if (wpa_s->conf->filter_rssi)
1706 params.filter_rssi = wpa_s->conf->filter_rssi;
1707
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001708 /* See if user specified frequencies. If so, scan only those. */
1709 if (wpa_s->conf->freq_list && !params.freqs) {
1710 wpa_dbg(wpa_s, MSG_DEBUG,
1711 "Optimize scan based on conf->freq_list");
1712 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1713 }
1714
Hai Shalomce48b4a2018-09-05 11:41:35 -07001715#ifdef CONFIG_MBO
1716 if (wpa_s->enable_oce & OCE_STA)
1717 params.oce_scan = 1;
1718#endif /* CONFIG_MBO */
1719
Dmitry Shmidt04949592012-07-19 12:16:46 -07001720 scan_params = &params;
1721
1722scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001723 wpa_s->sched_scan_timed_out = 0;
1724
1725 /*
1726 * We cannot support multiple scan plans if the scan request includes
1727 * too many SSID's, so in this case use only the last scan plan and make
1728 * it run infinitely. It will be stopped by the timeout.
1729 */
1730 if (wpa_s->sched_scan_plans_num == 1 ||
1731 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1732 params.sched_scan_plans = wpa_s->sched_scan_plans;
1733 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1734 } else if (wpa_s->sched_scan_plans_num > 1) {
1735 wpa_dbg(wpa_s, MSG_DEBUG,
1736 "Too many SSIDs. Default to using single scheduled_scan plan");
1737 params.sched_scan_plans =
1738 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1739 1];
1740 params.sched_scan_plans_num = 1;
1741 } else {
1742 if (wpa_s->conf->sched_scan_interval)
1743 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1744 else
1745 scan_plan.interval = 10;
1746
1747 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1748 wpa_printf(MSG_WARNING,
1749 "Scan interval too long(%u), use the maximum allowed(%u)",
1750 scan_plan.interval,
1751 wpa_s->max_sched_scan_plan_interval);
1752 scan_plan.interval =
1753 wpa_s->max_sched_scan_plan_interval;
1754 }
1755
1756 scan_plan.iterations = 0;
1757 params.sched_scan_plans = &scan_plan;
1758 params.sched_scan_plans_num = 1;
1759 }
1760
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001761 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1762
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001763 if (ssid || !wpa_s->first_sched_scan) {
1764 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001765 "Starting sched scan after %u seconds: interval %u timeout %d",
1766 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001767 params.sched_scan_plans[0].interval,
1768 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001769 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001770 wpa_dbg(wpa_s, MSG_DEBUG,
1771 "Starting sched scan after %u seconds (no timeout)",
1772 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001773 }
1774
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001775 wpa_setband_scan_freqs(wpa_s, scan_params);
1776
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001777 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001778 wpa_s->wpa_state <= WPA_SCANNING)
1779 wpa_setup_mac_addr_rand_params(&params,
1780 wpa_s->mac_addr_sched_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001781
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001782 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1783
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001784 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001785 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001786 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001787 os_free(params.mac_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001788 if (ret) {
1789 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1790 if (prev_state != wpa_s->wpa_state)
1791 wpa_supplicant_set_state(wpa_s, prev_state);
1792 return ret;
1793 }
1794
1795 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1796 if (ssid || !wpa_s->first_sched_scan) {
1797 wpa_s->sched_scan_timed_out = 0;
1798 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1799 wpa_supplicant_sched_scan_timeout,
1800 wpa_s, NULL);
1801 wpa_s->first_sched_scan = 0;
1802 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001803 params.sched_scan_plans[0].interval *= 2;
1804 if ((unsigned int) wpa_s->sched_scan_timeout <
1805 params.sched_scan_plans[0].interval ||
1806 params.sched_scan_plans[0].interval >
1807 wpa_s->max_sched_scan_plan_interval) {
1808 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001809 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1810 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001811 }
1812
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001813 /* If there is no more ssids, start next time from the beginning */
1814 if (!ssid)
1815 wpa_s->prev_sched_ssid = NULL;
1816
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817 return 0;
1818}
1819
1820
1821/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1823 * @wpa_s: Pointer to wpa_supplicant data
1824 *
1825 * This function is used to cancel a scan request scheduled with
1826 * wpa_supplicant_req_scan().
1827 */
1828void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1829{
1830 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1831 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832}
1833
1834
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001835/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001836 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1837 * @wpa_s: Pointer to wpa_supplicant data
1838 *
1839 * This function is used to stop a delayed scheduled scan.
1840 */
1841void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1842{
1843 if (!wpa_s->sched_scan_supported)
1844 return;
1845
1846 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1847 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1848 wpa_s, NULL);
1849}
1850
1851
1852/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001853 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1854 * @wpa_s: Pointer to wpa_supplicant data
1855 *
1856 * This function is used to stop a periodic scheduled scan.
1857 */
1858void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1859{
1860 if (!wpa_s->sched_scanning)
1861 return;
1862
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001863 if (wpa_s->sched_scanning)
1864 wpa_s->sched_scan_stop_req = 1;
1865
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1867 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1868 wpa_supplicant_stop_sched_scan(wpa_s);
1869}
1870
1871
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001872/**
1873 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1874 * @wpa_s: Pointer to wpa_supplicant data
1875 * @scanning: Whether scanning is currently in progress
1876 *
1877 * This function is to generate scanning notifycations. It is called whenever
1878 * there may have been a change in scanning (scan started, completed, stopped).
1879 * wpas_notify_scanning() is called whenever the scanning state changed from the
1880 * previously notified state.
1881 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1883 int scanning)
1884{
1885 if (wpa_s->scanning != scanning) {
1886 wpa_s->scanning = scanning;
1887 wpas_notify_scanning(wpa_s);
1888 }
1889}
1890
1891
1892static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1893{
1894 int rate = 0;
1895 const u8 *ie;
1896 int i;
1897
1898 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1899 for (i = 0; ie && i < ie[1]; i++) {
1900 if ((ie[i + 2] & 0x7f) > rate)
1901 rate = ie[i + 2] & 0x7f;
1902 }
1903
1904 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1905 for (i = 0; ie && i < ie[1]; i++) {
1906 if ((ie[i + 2] & 0x7f) > rate)
1907 rate = ie[i + 2] & 0x7f;
1908 }
1909
1910 return rate;
1911}
1912
1913
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001914/**
1915 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1916 * @res: Scan result entry
1917 * @ie: Information element identitifier (WLAN_EID_*)
1918 * Returns: Pointer to the information element (id field) or %NULL if not found
1919 *
1920 * This function returns the first matching information element in the scan
1921 * result.
1922 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1924{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001925 size_t ie_len = res->ie_len;
1926
1927 /* Use the Beacon frame IEs if res->ie_len is not available */
1928 if (!ie_len)
1929 ie_len = res->beacon_ie_len;
1930
1931 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932}
1933
1934
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001935/**
1936 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1937 * @res: Scan result entry
1938 * @vendor_type: Vendor type (four octets starting the IE payload)
1939 * Returns: Pointer to the information element (id field) or %NULL if not found
1940 *
1941 * This function returns the first matching information element in the scan
1942 * result.
1943 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1945 u32 vendor_type)
1946{
Hai Shalom60840252021-02-19 19:02:11 -08001947 const u8 *ies;
1948 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949
Hai Shalom60840252021-02-19 19:02:11 -08001950 ies = (const u8 *) (res + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951
Hai Shalom60840252021-02-19 19:02:11 -08001952 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
1953 if (elem->datalen >= 4 &&
1954 vendor_type == WPA_GET_BE32(elem->data))
1955 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 }
1957
1958 return NULL;
1959}
1960
1961
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001962/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001963 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1964 * @res: Scan result entry
1965 * @vendor_type: Vendor type (four octets starting the IE payload)
1966 * Returns: Pointer to the information element (id field) or %NULL if not found
1967 *
1968 * This function returns the first matching information element in the scan
1969 * result.
1970 *
1971 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1972 * from Beacon frames instead of either Beacon or Probe Response frames.
1973 */
1974const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1975 u32 vendor_type)
1976{
Hai Shalom60840252021-02-19 19:02:11 -08001977 const u8 *ies;
1978 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001979
1980 if (res->beacon_ie_len == 0)
1981 return NULL;
1982
Hai Shalom60840252021-02-19 19:02:11 -08001983 ies = (const u8 *) (res + 1);
1984 ies += res->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001985
Hai Shalom60840252021-02-19 19:02:11 -08001986 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1987 res->beacon_ie_len) {
1988 if (elem->datalen >= 4 &&
1989 vendor_type == WPA_GET_BE32(elem->data))
1990 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001991 }
1992
1993 return NULL;
1994}
1995
1996
1997/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001998 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1999 * @res: Scan result entry
2000 * @vendor_type: Vendor type (four octets starting the IE payload)
2001 * Returns: Pointer to the information element payload or %NULL if not found
2002 *
2003 * This function returns concatenated payload of possibly fragmented vendor
2004 * specific information elements in the scan result. The caller is responsible
2005 * for freeing the returned buffer.
2006 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002007struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2008 u32 vendor_type)
2009{
2010 struct wpabuf *buf;
2011 const u8 *end, *pos;
2012
2013 buf = wpabuf_alloc(res->ie_len);
2014 if (buf == NULL)
2015 return NULL;
2016
2017 pos = (const u8 *) (res + 1);
2018 end = pos + res->ie_len;
2019
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002020 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08002021 u8 ie, len;
2022
2023 ie = pos[0];
2024 len = pos[1];
2025 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026 break;
Hai Shalom60840252021-02-19 19:02:11 -08002027 pos += 2;
2028 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2029 vendor_type == WPA_GET_BE32(pos))
2030 wpabuf_put_data(buf, pos + 4, len - 4);
2031 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 }
2033
2034 if (wpabuf_len(buf) == 0) {
2035 wpabuf_free(buf);
2036 buf = NULL;
2037 }
2038
2039 return buf;
2040}
2041
2042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043/* Compare function for sorting scan results. Return >0 if @b is considered
2044 * better. */
2045static int wpa_scan_result_compar(const void *a, const void *b)
2046{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002047#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 struct wpa_scan_res **_wa = (void *) a;
2049 struct wpa_scan_res **_wb = (void *) b;
2050 struct wpa_scan_res *wa = *_wa;
2051 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002052 int wpa_a, wpa_b;
2053 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054
2055 /* WPA/WPA2 support preferred */
2056 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2057 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2058 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2059 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2060
2061 if (wpa_b && !wpa_a)
2062 return 1;
2063 if (!wpa_b && wpa_a)
2064 return -1;
2065
2066 /* privacy support preferred */
2067 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2068 (wb->caps & IEEE80211_CAP_PRIVACY))
2069 return 1;
2070 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2071 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2072 return -1;
2073
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002074 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002075 snr_a_full = wa->snr;
2076 snr_a = MIN(wa->snr, GREAT_SNR);
2077 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002078 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002079 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002080 /* Level is not in dBm, so we can't calculate
2081 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002082 snr_a = snr_a_full = wa->level;
2083 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002084 }
2085
Hai Shaloma20dcd72022-02-04 13:43:00 -08002086 /* If SNR is close, decide by max rate or frequency band. For cases
2087 * involving the 6 GHz band, use the throughput estimate irrespective
2088 * of the SNR difference since the LPI/VLP rules may result in
2089 * significant differences in SNR for cases where the estimated
2090 * throughput can be considerably higher with the lower SNR. */
2091 if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2092 is_6ghz_freq(wa->freq) ||
2093 is_6ghz_freq(wb->freq))) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002094 if (wa->est_throughput != wb->est_throughput)
Hai Shalom021b0b52019-04-10 11:17:58 -07002095 return (int) wb->est_throughput -
2096 (int) wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002097 }
2098 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2099 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002100 if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2101 return is_6ghz_freq(wa->freq) ? -1 : 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2103 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 }
2105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 * identical, use quality values since some drivers may only report
2108 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002109 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002111 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113}
2114
2115
2116#ifdef CONFIG_WPS
2117/* Compare function for sorting scan results when searching a WPS AP for
2118 * provisioning. Return >0 if @b is considered better. */
2119static int wpa_scan_result_wps_compar(const void *a, const void *b)
2120{
2121 struct wpa_scan_res **_wa = (void *) a;
2122 struct wpa_scan_res **_wb = (void *) b;
2123 struct wpa_scan_res *wa = *_wa;
2124 struct wpa_scan_res *wb = *_wb;
2125 int uses_wps_a, uses_wps_b;
2126 struct wpabuf *wps_a, *wps_b;
2127 int res;
2128
2129 /* Optimization - check WPS IE existence before allocated memory and
2130 * doing full reassembly. */
2131 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2132 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2133 if (uses_wps_a && !uses_wps_b)
2134 return -1;
2135 if (!uses_wps_a && uses_wps_b)
2136 return 1;
2137
2138 if (uses_wps_a && uses_wps_b) {
2139 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2140 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2141 res = wps_ap_priority_compar(wps_a, wps_b);
2142 wpabuf_free(wps_a);
2143 wpabuf_free(wps_b);
2144 if (res)
2145 return res;
2146 }
2147
2148 /*
2149 * Do not use current AP security policy as a sorting criteria during
2150 * WPS provisioning step since the AP may get reconfigured at the
2151 * completion of provisioning.
2152 */
2153
2154 /* all things being equal, use signal level; if signal levels are
2155 * identical, use quality values since some drivers may only report
2156 * that value and leave the signal level zero */
2157 if (wb->level == wa->level)
2158 return wb->qual - wa->qual;
2159 return wb->level - wa->level;
2160}
2161#endif /* CONFIG_WPS */
2162
2163
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002164static void dump_scan_res(struct wpa_scan_results *scan_res)
2165{
2166#ifndef CONFIG_NO_STDOUT_DEBUG
2167 size_t i;
2168
2169 if (scan_res->res == NULL || scan_res->num == 0)
2170 return;
2171
2172 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2173
2174 for (i = 0; i < scan_res->num; i++) {
2175 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002176 u8 *pos;
Sunil Ravia04bd252022-05-02 22:54:18 -07002177 const u8 *ssid_ie, *ssid = NULL;
2178 size_t ssid_len = 0;
2179
2180 ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2181 if (ssid_ie) {
2182 ssid = ssid_ie + 2;
2183 ssid_len = ssid_ie[1];
2184 }
2185
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002186 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002187 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2188
Sunil Ravia04bd252022-05-02 22:54:18 -07002189 wpa_printf(MSG_EXCESSIVE, MACSTR
2190 " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2191 MAC2STR(r->bssid),
2192 wpa_ssid_txt(ssid, ssid_len),
2193 r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002194 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002195 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2196 r->flags,
2197 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002198 } else {
Sunil Ravia04bd252022-05-02 22:54:18 -07002199 wpa_printf(MSG_EXCESSIVE, MACSTR
2200 " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2201 MAC2STR(r->bssid),
2202 wpa_ssid_txt(ssid, ssid_len),
2203 r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002204 r->noise, r->level, r->flags, r->age,
2205 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002206 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002207 pos = (u8 *) (r + 1);
2208 if (r->ie_len)
2209 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2210 pos += r->ie_len;
2211 if (r->beacon_ie_len)
2212 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2213 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002214 }
2215#endif /* CONFIG_NO_STDOUT_DEBUG */
2216}
2217
2218
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002219/**
2220 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2221 * @wpa_s: Pointer to wpa_supplicant data
2222 * @bssid: BSSID to check
2223 * Returns: 0 if the BSSID is filtered or 1 if not
2224 *
2225 * This function is used to filter out specific BSSIDs from scan reslts mainly
2226 * for testing purposes (SET bssid_filter ctrl_iface command).
2227 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002228int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2229 const u8 *bssid)
2230{
2231 size_t i;
2232
2233 if (wpa_s->bssid_filter == NULL)
2234 return 1;
2235
2236 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2237 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2238 ETH_ALEN) == 0)
2239 return 1;
2240 }
2241
2242 return 0;
2243}
2244
2245
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002246void filter_scan_res(struct wpa_supplicant *wpa_s,
2247 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002248{
2249 size_t i, j;
2250
2251 if (wpa_s->bssid_filter == NULL)
2252 return;
2253
2254 for (i = 0, j = 0; i < res->num; i++) {
2255 if (wpa_supplicant_filter_bssid_match(wpa_s,
2256 res->res[i]->bssid)) {
2257 res->res[j++] = res->res[i];
2258 } else {
2259 os_free(res->res[i]);
2260 res->res[i] = NULL;
2261 }
2262 }
2263
2264 if (res->num != j) {
2265 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2266 (int) (res->num - j));
2267 res->num = j;
2268 }
2269}
2270
2271
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002272void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002273{
2274 if (res->flags & WPA_SCAN_NOISE_INVALID) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002275 res->noise = is_6ghz_freq(res->freq) ?
2276 DEFAULT_NOISE_FLOOR_6GHZ :
2277 (IS_5GHZ(res->freq) ?
2278 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002279 }
2280
2281 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2282 res->snr = res->level - res->noise;
2283 } else {
2284 /* Level is not in dBm, so we can't calculate
2285 * SNR. Just use raw level (units unknown). */
2286 res->snr = res->level;
2287 }
2288}
2289
2290
Hai Shalom899fcc72020-10-19 14:38:18 -07002291/* Minimum SNR required to achieve a certain bitrate. */
2292struct minsnr_bitrate_entry {
2293 int minsnr;
2294 unsigned int bitrate; /* in Mbps */
2295};
2296
2297/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2298static const int vht_mcs = 8;
2299
2300static const struct minsnr_bitrate_entry vht20_table[] = {
2301 { 0, 0 },
2302 { 2, 6500 }, /* HT20 MCS0 */
2303 { 5, 13000 }, /* HT20 MCS1 */
2304 { 9, 19500 }, /* HT20 MCS2 */
2305 { 11, 26000 }, /* HT20 MCS3 */
2306 { 15, 39000 }, /* HT20 MCS4 */
2307 { 18, 52000 }, /* HT20 MCS5 */
2308 { 20, 58500 }, /* HT20 MCS6 */
2309 { 25, 65000 }, /* HT20 MCS7 */
2310 { 29, 78000 }, /* VHT20 MCS8 */
2311 { -1, 78000 } /* SNR > 29 */
2312};
2313
2314static const struct minsnr_bitrate_entry vht40_table[] = {
2315 { 0, 0 },
2316 { 5, 13500 }, /* HT40 MCS0 */
2317 { 8, 27000 }, /* HT40 MCS1 */
2318 { 12, 40500 }, /* HT40 MCS2 */
2319 { 14, 54000 }, /* HT40 MCS3 */
2320 { 18, 81000 }, /* HT40 MCS4 */
2321 { 21, 108000 }, /* HT40 MCS5 */
2322 { 23, 121500 }, /* HT40 MCS6 */
2323 { 28, 135000 }, /* HT40 MCS7 */
2324 { 32, 162000 }, /* VHT40 MCS8 */
2325 { 34, 180000 }, /* VHT40 MCS9 */
2326 { -1, 180000 } /* SNR > 34 */
2327};
2328
2329static const struct minsnr_bitrate_entry vht80_table[] = {
2330 { 0, 0 },
2331 { 8, 29300 }, /* VHT80 MCS0 */
2332 { 11, 58500 }, /* VHT80 MCS1 */
2333 { 15, 87800 }, /* VHT80 MCS2 */
2334 { 17, 117000 }, /* VHT80 MCS3 */
2335 { 21, 175500 }, /* VHT80 MCS4 */
2336 { 24, 234000 }, /* VHT80 MCS5 */
2337 { 26, 263300 }, /* VHT80 MCS6 */
2338 { 31, 292500 }, /* VHT80 MCS7 */
2339 { 35, 351000 }, /* VHT80 MCS8 */
2340 { 37, 390000 }, /* VHT80 MCS9 */
2341 { -1, 390000 } /* SNR > 37 */
2342};
2343
2344
Hai Shaloma20dcd72022-02-04 13:43:00 -08002345static const struct minsnr_bitrate_entry vht160_table[] = {
2346 { 0, 0 },
2347 { 11, 58500 }, /* VHT160 MCS0 */
2348 { 14, 117000 }, /* VHT160 MCS1 */
2349 { 18, 175500 }, /* VHT160 MCS2 */
2350 { 20, 234000 }, /* VHT160 MCS3 */
2351 { 24, 351000 }, /* VHT160 MCS4 */
2352 { 27, 468000 }, /* VHT160 MCS5 */
2353 { 29, 526500 }, /* VHT160 MCS6 */
2354 { 34, 585000 }, /* VHT160 MCS7 */
2355 { 38, 702000 }, /* VHT160 MCS8 */
2356 { 40, 780000 }, /* VHT160 MCS9 */
2357 { -1, 780000 } /* SNR > 37 */
2358};
2359
2360
2361static const struct minsnr_bitrate_entry he20_table[] = {
2362 { 0, 0 },
2363 { 2, 8600 }, /* HE20 MCS0 */
2364 { 5, 17200 }, /* HE20 MCS1 */
2365 { 9, 25800 }, /* HE20 MCS2 */
2366 { 11, 34400 }, /* HE20 MCS3 */
2367 { 15, 51600 }, /* HE20 MCS4 */
2368 { 18, 68800 }, /* HE20 MCS5 */
2369 { 20, 77400 }, /* HE20 MCS6 */
2370 { 25, 86000 }, /* HE20 MCS7 */
2371 { 29, 103200 }, /* HE20 MCS8 */
2372 { 31, 114700 }, /* HE20 MCS9 */
2373 { 34, 129000 }, /* HE20 MCS10 */
2374 { 36, 143400 }, /* HE20 MCS11 */
2375 { -1, 143400 } /* SNR > 29 */
2376};
2377
2378static const struct minsnr_bitrate_entry he40_table[] = {
2379 { 0, 0 },
2380 { 5, 17200 }, /* HE40 MCS0 */
2381 { 8, 34400 }, /* HE40 MCS1 */
2382 { 12, 51600 }, /* HE40 MCS2 */
2383 { 14, 68800 }, /* HE40 MCS3 */
2384 { 18, 103200 }, /* HE40 MCS4 */
2385 { 21, 137600 }, /* HE40 MCS5 */
2386 { 23, 154900 }, /* HE40 MCS6 */
2387 { 28, 172100 }, /* HE40 MCS7 */
2388 { 32, 206500 }, /* HE40 MCS8 */
2389 { 34, 229400 }, /* HE40 MCS9 */
2390 { 37, 258100 }, /* HE40 MCS10 */
2391 { 39, 286800 }, /* HE40 MCS11 */
2392 { -1, 286800 } /* SNR > 34 */
2393};
2394
2395static const struct minsnr_bitrate_entry he80_table[] = {
2396 { 0, 0 },
2397 { 8, 36000 }, /* HE80 MCS0 */
2398 { 11, 72100 }, /* HE80 MCS1 */
2399 { 15, 108100 }, /* HE80 MCS2 */
2400 { 17, 144100 }, /* HE80 MCS3 */
2401 { 21, 216200 }, /* HE80 MCS4 */
2402 { 24, 288200 }, /* HE80 MCS5 */
2403 { 26, 324300 }, /* HE80 MCS6 */
2404 { 31, 360300 }, /* HE80 MCS7 */
2405 { 35, 432400 }, /* HE80 MCS8 */
2406 { 37, 480400 }, /* HE80 MCS9 */
2407 { 40, 540400 }, /* HE80 MCS10 */
2408 { 42, 600500 }, /* HE80 MCS11 */
2409 { -1, 600500 } /* SNR > 37 */
2410};
2411
2412
2413static const struct minsnr_bitrate_entry he160_table[] = {
2414 { 0, 0 },
2415 { 11, 72100 }, /* HE160 MCS0 */
2416 { 14, 144100 }, /* HE160 MCS1 */
2417 { 18, 216200 }, /* HE160 MCS2 */
2418 { 20, 288200 }, /* HE160 MCS3 */
2419 { 24, 432400 }, /* HE160 MCS4 */
2420 { 27, 576500 }, /* HE160 MCS5 */
2421 { 29, 648500 }, /* HE160 MCS6 */
2422 { 34, 720600 }, /* HE160 MCS7 */
2423 { 38, 864700 }, /* HE160 MCS8 */
2424 { 40, 960800 }, /* HE160 MCS9 */
2425 { 43, 1080900 }, /* HE160 MCS10 */
2426 { 45, 1201000 }, /* HE160 MCS11 */
2427 { -1, 1201000 } /* SNR > 37 */
2428};
2429
2430
Hai Shalomfdcde762020-04-02 11:19:20 -07002431static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2432 int rate0, int rate1)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002433{
Hai Shalomfdcde762020-04-02 11:19:20 -07002434 return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002435}
2436
2437
Hai Shalom899fcc72020-10-19 14:38:18 -07002438static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2439 int snr, bool vht)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002440{
Hai Shalom899fcc72020-10-19 14:38:18 -07002441 const struct minsnr_bitrate_entry *prev, *entry = table;
2442
2443 while ((entry->minsnr != -1) &&
2444 (snr >= entry->minsnr) &&
2445 (vht || entry - table <= vht_mcs))
2446 entry++;
2447 if (entry == table)
2448 return entry->bitrate;
2449 prev = entry - 1;
2450 if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2451 return prev->bitrate;
2452 return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2453 entry->bitrate);
Hai Shalomfdcde762020-04-02 11:19:20 -07002454}
2455
2456
Hai Shalom899fcc72020-10-19 14:38:18 -07002457static unsigned int max_ht20_rate(int snr, bool vht)
Hai Shalomfdcde762020-04-02 11:19:20 -07002458{
Hai Shalom899fcc72020-10-19 14:38:18 -07002459 return max_rate(vht20_table, snr, vht);
2460}
2461
2462
2463static unsigned int max_ht40_rate(int snr, bool vht)
2464{
2465 return max_rate(vht40_table, snr, vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002466}
2467
2468
2469static unsigned int max_vht80_rate(int snr)
2470{
Hai Shalom899fcc72020-10-19 14:38:18 -07002471 return max_rate(vht80_table, snr, 1);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002472}
2473
Hai Shalomfdcde762020-04-02 11:19:20 -07002474
Hai Shaloma20dcd72022-02-04 13:43:00 -08002475static unsigned int max_vht160_rate(int snr)
2476{
2477 return max_rate(vht160_table, snr, 1);
2478}
2479
2480
2481static unsigned int max_he_rate(const struct minsnr_bitrate_entry table[],
2482 int snr)
2483{
2484 const struct minsnr_bitrate_entry *prev, *entry = table;
2485
2486 while (entry->minsnr != -1 && snr >= entry->minsnr)
2487 entry++;
2488 if (entry == table)
2489 return 0;
2490 prev = entry - 1;
2491 if (entry->minsnr == -1)
2492 return prev->bitrate;
2493 return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2494 prev->bitrate, entry->bitrate);
2495}
2496
2497
Hai Shalomfdcde762020-04-02 11:19:20 -07002498unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2499 const u8 *ies, size_t ies_len, int rate,
Hai Shaloma20dcd72022-02-04 13:43:00 -08002500 int snr, int freq)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002501{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002502 struct hostapd_hw_modes *hw_mode;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002503 unsigned int est, tmp;
Hai Shalomfdcde762020-04-02 11:19:20 -07002504 const u8 *ie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002505
2506 /* Limit based on estimated SNR */
2507 if (rate > 1 * 2 && snr < 1)
2508 rate = 1 * 2;
2509 else if (rate > 2 * 2 && snr < 4)
2510 rate = 2 * 2;
2511 else if (rate > 6 * 2 && snr < 5)
2512 rate = 6 * 2;
2513 else if (rate > 9 * 2 && snr < 6)
2514 rate = 9 * 2;
2515 else if (rate > 12 * 2 && snr < 7)
2516 rate = 12 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002517 else if (rate > 12 * 2 && snr < 8)
2518 rate = 14 * 2;
2519 else if (rate > 12 * 2 && snr < 9)
2520 rate = 16 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002521 else if (rate > 18 * 2 && snr < 10)
2522 rate = 18 * 2;
2523 else if (rate > 24 * 2 && snr < 11)
2524 rate = 24 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002525 else if (rate > 24 * 2 && snr < 12)
2526 rate = 27 * 2;
2527 else if (rate > 24 * 2 && snr < 13)
2528 rate = 30 * 2;
2529 else if (rate > 24 * 2 && snr < 14)
2530 rate = 33 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002531 else if (rate > 36 * 2 && snr < 15)
2532 rate = 36 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002533 else if (rate > 36 * 2 && snr < 16)
2534 rate = 39 * 2;
2535 else if (rate > 36 * 2 && snr < 17)
2536 rate = 42 * 2;
2537 else if (rate > 36 * 2 && snr < 18)
2538 rate = 45 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002539 else if (rate > 48 * 2 && snr < 19)
2540 rate = 48 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002541 else if (rate > 48 * 2 && snr < 20)
2542 rate = 51 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002543 else if (rate > 54 * 2 && snr < 21)
2544 rate = 54 * 2;
2545 est = rate * 500;
2546
Hai Shaloma20dcd72022-02-04 13:43:00 -08002547 hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2548 freq);
2549
2550 if (hw_mode && hw_mode->ht_capab) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002551 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002552 if (ie) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002553 tmp = max_ht20_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002554 if (tmp > est)
2555 est = tmp;
2556 }
2557 }
2558
Hai Shaloma20dcd72022-02-04 13:43:00 -08002559 if (hw_mode &&
2560 (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002561 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002562 if (ie && ie[1] >= 2 &&
2563 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002564 tmp = max_ht40_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002565 if (tmp > est)
2566 est = tmp;
2567 }
2568 }
2569
Hai Shaloma20dcd72022-02-04 13:43:00 -08002570 if (hw_mode && hw_mode->vht_capab) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002571 /* Use +1 to assume VHT is always faster than HT */
Hai Shalomfdcde762020-04-02 11:19:20 -07002572 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002573 if (ie) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002574 bool vht80 = false, vht160 = false;
2575
Hai Shalom899fcc72020-10-19 14:38:18 -07002576 tmp = max_ht20_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002577 if (tmp > est)
2578 est = tmp;
2579
Hai Shalomfdcde762020-04-02 11:19:20 -07002580 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002581 if (ie && ie[1] >= 2 &&
2582 (ie[3] &
2583 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002584 tmp = max_ht40_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002585 if (tmp > est)
2586 est = tmp;
2587 }
2588
Hai Shaloma20dcd72022-02-04 13:43:00 -08002589 /* Determine VHT BSS bandwidth based on IEEE Std
2590 * 802.11-2020, Table 11-23 (VHT BSs bandwidth) */
Hai Shalomfdcde762020-04-02 11:19:20 -07002591 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002592 if (ie && ie[1] >= 3) {
2593 u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2594 u8 seg0 = ie[3];
2595 u8 seg1 = ie[4];
2596
2597 if (cw)
2598 vht80 = true;
2599 if (cw == 2 ||
2600 (cw == 3 &&
2601 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2602 vht160 = true;
2603 if (cw == 1 &&
2604 ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2605 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2606 vht160 = true;
2607 }
2608
2609 if (vht80) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002610 tmp = max_vht80_rate(snr) + 1;
2611 if (tmp > est)
2612 est = tmp;
2613 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08002614
2615 if (vht160 &&
2616 (hw_mode->vht_capab &
2617 (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2618 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
2619 tmp = max_vht160_rate(snr) + 1;
2620 if (tmp > est)
2621 est = tmp;
2622 }
2623 }
2624 }
2625
2626 if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
2627 /* Use +2 to assume HE is always faster than HT/VHT */
2628 struct ieee80211_he_capabilities *he;
2629 struct he_capabilities *own_he;
2630 u8 cw;
2631
2632 ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
2633 if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
2634 return est;
2635 he = (struct ieee80211_he_capabilities *) &ie[3];
2636 own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
2637
2638 tmp = max_he_rate(he20_table, snr) + 2;
2639 if (tmp > est)
2640 est = tmp;
2641
2642 cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
2643 own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
2644 if (cw &
2645 (IS_2P4GHZ(freq) ? HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
2646 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2647 tmp = max_he_rate(he40_table, snr) + 2;
2648 if (tmp > est)
2649 est = tmp;
2650 }
2651
2652 if (!IS_2P4GHZ(freq) &&
2653 (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2654 tmp = max_he_rate(he80_table, snr) + 2;
2655 if (tmp > est)
2656 est = tmp;
2657 }
2658
2659 if (!IS_2P4GHZ(freq) &&
2660 (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2661 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G))) {
2662 tmp = max_he_rate(he160_table, snr) + 2;
2663 if (tmp > est)
2664 est = tmp;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002665 }
2666 }
2667
Hai Shalomfdcde762020-04-02 11:19:20 -07002668 return est;
2669}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002670
Hai Shalomfdcde762020-04-02 11:19:20 -07002671
2672void scan_est_throughput(struct wpa_supplicant *wpa_s,
2673 struct wpa_scan_res *res)
2674{
2675 int rate; /* max legacy rate in 500 kb/s units */
2676 int snr = res->snr;
2677 const u8 *ies = (const void *) (res + 1);
2678 size_t ie_len = res->ie_len;
2679
2680 if (res->est_throughput)
2681 return;
2682
2683 /* Get maximum legacy rate */
2684 rate = wpa_scan_get_max_rate(res);
2685
2686 if (!ie_len)
2687 ie_len = res->beacon_ie_len;
2688 res->est_throughput =
Hai Shaloma20dcd72022-02-04 13:43:00 -08002689 wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, res->freq);
Hai Shalomfdcde762020-04-02 11:19:20 -07002690
2691 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002692}
2693
2694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695/**
2696 * wpa_supplicant_get_scan_results - Get scan results
2697 * @wpa_s: Pointer to wpa_supplicant data
2698 * @info: Information about what was scanned or %NULL if not available
2699 * @new_scan: Whether a new scan was performed
2700 * Returns: Scan results, %NULL on failure
2701 *
2702 * This function request the current scan results from the driver and updates
2703 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2704 * results with wpa_scan_results_free().
2705 */
2706struct wpa_scan_results *
2707wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2708 struct scan_info *info, int new_scan)
2709{
2710 struct wpa_scan_results *scan_res;
2711 size_t i;
2712 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002714 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 if (scan_res == NULL) {
2716 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2717 return NULL;
2718 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002719 if (scan_res->fetch_time.sec == 0) {
2720 /*
2721 * Make sure we have a valid timestamp if the driver wrapper
2722 * does not set this.
2723 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002724 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002725 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002726 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002728 for (i = 0; i < scan_res->num; i++) {
2729 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2730
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002731 scan_snr(scan_res_item);
2732 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002733 }
2734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002736 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2738 "provisioning rules");
2739 compar = wpa_scan_result_wps_compar;
2740 }
2741#endif /* CONFIG_WPS */
2742
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002743 if (scan_res->res) {
2744 qsort(scan_res->res, scan_res->num,
2745 sizeof(struct wpa_scan_res *), compar);
2746 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002747 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748
Dmitry Shmidt29333592017-01-09 12:27:11 -08002749 if (wpa_s->ignore_post_flush_scan_res) {
2750 /* FLUSH command aborted an ongoing scan and these are the
2751 * results from the aborted scan. Do not process the results to
2752 * maintain flushed state. */
2753 wpa_dbg(wpa_s, MSG_DEBUG,
2754 "Do not update BSS table based on pending post-FLUSH scan results");
2755 wpa_s->ignore_post_flush_scan_res = 0;
2756 return scan_res;
2757 }
2758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759 wpa_bss_update_start(wpa_s);
2760 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002761 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2762 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 wpa_bss_update_end(wpa_s, info, new_scan);
2764
2765 return scan_res;
2766}
2767
2768
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002769/**
2770 * wpa_supplicant_update_scan_results - Update scan results from the driver
2771 * @wpa_s: Pointer to wpa_supplicant data
2772 * Returns: 0 on success, -1 on failure
2773 *
2774 * This function updates the BSS table within wpa_supplicant based on the
2775 * currently available scan results from the driver without requesting a new
2776 * scan. This is used in cases where the driver indicates an association
2777 * (including roaming within ESS) and wpa_supplicant does not yet have the
2778 * needed information to complete the connection (e.g., to perform validation
2779 * steps in 4-way handshake).
2780 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2782{
2783 struct wpa_scan_results *scan_res;
2784 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2785 if (scan_res == NULL)
2786 return -1;
2787 wpa_scan_results_free(scan_res);
2788
2789 return 0;
2790}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002791
2792
2793/**
2794 * scan_only_handler - Reports scan results
2795 */
2796void scan_only_handler(struct wpa_supplicant *wpa_s,
2797 struct wpa_scan_results *scan_res)
2798{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002799 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002800 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2801 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2802 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2803 wpa_s->manual_scan_id);
2804 wpa_s->manual_scan_use_id = 0;
2805 } else {
2806 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2807 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002808 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002809 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002810 if (wpa_s->scan_work) {
2811 struct wpa_radio_work *work = wpa_s->scan_work;
2812 wpa_s->scan_work = NULL;
2813 radio_work_done(work);
2814 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002815
2816 if (wpa_s->wpa_state == WPA_SCANNING)
2817 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002818}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002819
2820
2821int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2822{
2823 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2824}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002825
2826
2827struct wpa_driver_scan_params *
2828wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2829{
2830 struct wpa_driver_scan_params *params;
2831 size_t i;
2832 u8 *n;
2833
2834 params = os_zalloc(sizeof(*params));
2835 if (params == NULL)
2836 return NULL;
2837
2838 for (i = 0; i < src->num_ssids; i++) {
2839 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002840 n = os_memdup(src->ssids[i].ssid,
2841 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002842 if (n == NULL)
2843 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002844 params->ssids[i].ssid = n;
2845 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2846 }
2847 }
2848 params->num_ssids = src->num_ssids;
2849
2850 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002851 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002852 if (n == NULL)
2853 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002854 params->extra_ies = n;
2855 params->extra_ies_len = src->extra_ies_len;
2856 }
2857
2858 if (src->freqs) {
2859 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002860 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002861 if (params->freqs == NULL)
2862 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002863 }
2864
2865 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002866 params->filter_ssids = os_memdup(src->filter_ssids,
2867 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002868 src->num_filter_ssids);
2869 if (params->filter_ssids == NULL)
2870 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002871 params->num_filter_ssids = src->num_filter_ssids;
2872 }
2873
2874 params->filter_rssi = src->filter_rssi;
2875 params->p2p_probe = src->p2p_probe;
2876 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002877 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002878 params->duration = src->duration;
2879 params->duration_mandatory = src->duration_mandatory;
Hai Shalomce48b4a2018-09-05 11:41:35 -07002880 params->oce_scan = src->oce_scan;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002881
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002882 if (src->sched_scan_plans_num > 0) {
2883 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002884 os_memdup(src->sched_scan_plans,
2885 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002886 src->sched_scan_plans_num);
2887 if (!params->sched_scan_plans)
2888 goto failed;
2889
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002890 params->sched_scan_plans_num = src->sched_scan_plans_num;
2891 }
2892
Hai Shalomc3565922019-10-28 11:58:20 -07002893 if (src->mac_addr_rand &&
2894 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
2895 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002896
2897 if (src->bssid) {
2898 u8 *bssid;
2899
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002900 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002901 if (!bssid)
2902 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002903 params->bssid = bssid;
2904 }
2905
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002906 params->relative_rssi_set = src->relative_rssi_set;
2907 params->relative_rssi = src->relative_rssi;
2908 params->relative_adjust_band = src->relative_adjust_band;
2909 params->relative_adjust_rssi = src->relative_adjust_rssi;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002910 params->p2p_include_6ghz = src->p2p_include_6ghz;
Sunil8cd6f4d2022-06-28 18:40:46 +00002911 params->non_coloc_6ghz = src->non_coloc_6ghz;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002912 return params;
2913
2914failed:
2915 wpa_scan_free_params(params);
2916 return NULL;
2917}
2918
2919
2920void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2921{
2922 size_t i;
2923
2924 if (params == NULL)
2925 return;
2926
2927 for (i = 0; i < params->num_ssids; i++)
2928 os_free((u8 *) params->ssids[i].ssid);
2929 os_free((u8 *) params->extra_ies);
2930 os_free(params->freqs);
2931 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002932 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002933
2934 /*
2935 * Note: params->mac_addr_mask points to same memory allocation and
2936 * must not be freed separately.
2937 */
2938 os_free((u8 *) params->mac_addr);
2939
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002940 os_free((u8 *) params->bssid);
2941
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002942 os_free(params);
2943}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002944
2945
2946int wpas_start_pno(struct wpa_supplicant *wpa_s)
2947{
Hai Shalomfdcde762020-04-02 11:19:20 -07002948 int ret;
2949 size_t prio, i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002950 struct wpa_ssid *ssid;
2951 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002952 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002953 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002954
2955 if (!wpa_s->sched_scan_supported)
2956 return -1;
2957
Dmitry Shmidte4663042016-04-04 10:07:49 -07002958 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2959 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2960 else
2961 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2962 if (max_sched_scan_ssids < 1)
2963 return -1;
2964
Dmitry Shmidt98660862014-03-11 17:26:21 -07002965 if (wpa_s->pno || wpa_s->pno_sched_pending)
2966 return 0;
2967
2968 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002969 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002970 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2971 return -EAGAIN;
2972 }
2973
2974 if (wpa_s->wpa_state == WPA_SCANNING) {
2975 wpa_supplicant_cancel_scan(wpa_s);
2976 if (wpa_s->sched_scanning) {
2977 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2978 "ongoing sched scan");
2979 wpa_supplicant_cancel_sched_scan(wpa_s);
2980 wpa_s->pno_sched_pending = 1;
2981 return 0;
2982 }
2983 }
2984
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002985 if (wpa_s->sched_scan_stop_req) {
2986 wpa_printf(MSG_DEBUG,
2987 "Schedule PNO after previous sched scan has stopped");
2988 wpa_s->pno_sched_pending = 1;
2989 return 0;
2990 }
2991
Dmitry Shmidt98660862014-03-11 17:26:21 -07002992 os_memset(&params, 0, sizeof(params));
2993
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002994 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002995 ssid = wpa_s->conf->ssid;
2996 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002997 if (!wpas_network_disabled(wpa_s, ssid)) {
2998 num_match_ssid++;
2999 if (ssid->scan_ssid)
3000 num_ssid++;
3001 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003002 ssid = ssid->next;
3003 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003004
3005 if (num_match_ssid == 0) {
3006 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3007 return -1;
3008 }
3009
3010 if (num_match_ssid > num_ssid) {
3011 params.num_ssids++; /* wildcard */
3012 num_ssid++;
3013 }
3014
Dmitry Shmidte4663042016-04-04 10:07:49 -07003015 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003016 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07003017 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3018 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003019 }
3020
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003021 if (num_match_ssid > wpa_s->max_match_sets) {
3022 num_match_ssid = wpa_s->max_match_sets;
3023 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003024 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003025 params.filter_ssids = os_calloc(num_match_ssid,
3026 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07003027 if (params.filter_ssids == NULL)
3028 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003029
Dmitry Shmidt98660862014-03-11 17:26:21 -07003030 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003031 prio = 0;
3032 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003033 while (ssid) {
3034 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003035 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3036 params.ssids[params.num_ssids].ssid =
3037 ssid->ssid;
3038 params.ssids[params.num_ssids].ssid_len =
3039 ssid->ssid_len;
3040 params.num_ssids++;
3041 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003042 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3043 ssid->ssid_len);
3044 params.filter_ssids[i].ssid_len = ssid->ssid_len;
3045 params.num_filter_ssids++;
3046 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003047 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07003048 break;
3049 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003050 if (ssid->pnext)
3051 ssid = ssid->pnext;
3052 else if (prio + 1 == wpa_s->conf->num_prio)
3053 break;
3054 else
3055 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003056 }
3057
3058 if (wpa_s->conf->filter_rssi)
3059 params.filter_rssi = wpa_s->conf->filter_rssi;
3060
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003061 if (wpa_s->sched_scan_plans_num) {
3062 params.sched_scan_plans = wpa_s->sched_scan_plans;
3063 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3064 } else {
3065 /* Set one scan plan that will run infinitely */
3066 if (wpa_s->conf->sched_scan_interval)
3067 scan_plan.interval = wpa_s->conf->sched_scan_interval;
3068 else
3069 scan_plan.interval = 10;
3070
3071 scan_plan.iterations = 0;
3072 params.sched_scan_plans = &scan_plan;
3073 params.sched_scan_plans_num = 1;
3074 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003075
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003076 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3077
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003078 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3079 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3080 params.freqs = wpa_s->manual_sched_scan_freqs;
3081 }
3082
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003083 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
Hai Shalomc3565922019-10-28 11:58:20 -07003084 wpa_s->wpa_state <= WPA_SCANNING)
3085 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003086
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003087 wpa_scan_set_relative_rssi_params(wpa_s, &params);
3088
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003089 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003090 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07003091 os_free(params.mac_addr);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003092 if (ret == 0)
3093 wpa_s->pno = 1;
3094 else
3095 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3096 return ret;
3097}
3098
3099
3100int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3101{
3102 int ret = 0;
3103
3104 if (!wpa_s->pno)
3105 return 0;
3106
3107 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003108 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003109
3110 wpa_s->pno = 0;
3111 wpa_s->pno_sched_pending = 0;
3112
3113 if (wpa_s->wpa_state == WPA_SCANNING)
3114 wpa_supplicant_req_scan(wpa_s, 0, 0);
3115
3116 return ret;
3117}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003118
3119
3120void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3121 unsigned int type)
3122{
3123 type &= MAC_ADDR_RAND_ALL;
3124 wpa_s->mac_addr_rand_enable &= ~type;
3125
3126 if (type & MAC_ADDR_RAND_SCAN) {
3127 os_free(wpa_s->mac_addr_scan);
3128 wpa_s->mac_addr_scan = NULL;
3129 }
3130
3131 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3132 os_free(wpa_s->mac_addr_sched_scan);
3133 wpa_s->mac_addr_sched_scan = NULL;
3134 }
3135
3136 if (type & MAC_ADDR_RAND_PNO) {
3137 os_free(wpa_s->mac_addr_pno);
3138 wpa_s->mac_addr_pno = NULL;
3139 }
3140}
3141
3142
3143int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3144 unsigned int type, const u8 *addr,
3145 const u8 *mask)
3146{
3147 u8 *tmp = NULL;
3148
Hai Shalom74f70d42019-02-11 14:42:39 -08003149 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3150 wpa_printf(MSG_INFO,
3151 "scan: MAC randomization type %u != supported=%u",
3152 type, wpa_s->mac_addr_rand_supported);
3153 return -1;
3154 }
3155
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003156 wpas_mac_addr_rand_scan_clear(wpa_s, type);
3157
3158 if (addr) {
3159 tmp = os_malloc(2 * ETH_ALEN);
3160 if (!tmp)
3161 return -1;
3162 os_memcpy(tmp, addr, ETH_ALEN);
3163 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3164 }
3165
3166 if (type == MAC_ADDR_RAND_SCAN) {
3167 wpa_s->mac_addr_scan = tmp;
3168 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3169 wpa_s->mac_addr_sched_scan = tmp;
3170 } else if (type == MAC_ADDR_RAND_PNO) {
3171 wpa_s->mac_addr_pno = tmp;
3172 } else {
3173 wpa_printf(MSG_INFO,
3174 "scan: Invalid MAC randomization type=0x%x",
3175 type);
3176 os_free(tmp);
3177 return -1;
3178 }
3179
3180 wpa_s->mac_addr_rand_enable |= type;
3181 return 0;
3182}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003183
3184
Hai Shalomc3565922019-10-28 11:58:20 -07003185int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3186 unsigned int type, u8 *mask)
3187{
3188 const u8 *to_copy;
3189
3190 if ((wpa_s->mac_addr_rand_enable & type) != type)
3191 return -1;
3192
3193 if (type == MAC_ADDR_RAND_SCAN) {
3194 to_copy = wpa_s->mac_addr_scan;
3195 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3196 to_copy = wpa_s->mac_addr_sched_scan;
3197 } else if (type == MAC_ADDR_RAND_PNO) {
3198 to_copy = wpa_s->mac_addr_pno;
3199 } else {
3200 wpa_printf(MSG_DEBUG,
3201 "scan: Invalid MAC randomization type=0x%x",
3202 type);
3203 return -1;
3204 }
3205
3206 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3207 return 0;
3208}
3209
3210
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003211int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3212{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003213 struct wpa_radio_work *work;
3214 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003215
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003216 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3217 if (work->wpa_s != wpa_s || !work->started ||
3218 (os_strcmp(work->type, "scan") != 0 &&
3219 os_strcmp(work->type, "p2p-scan") != 0))
3220 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003221 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003222 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003223 }
3224
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003225 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3226 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003227}
3228
3229
3230int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3231{
3232 struct sched_scan_plan *scan_plans = NULL;
3233 const char *token, *context = NULL;
3234 unsigned int num = 0;
3235
3236 if (!cmd)
3237 return -1;
3238
3239 if (!cmd[0]) {
3240 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3241 os_free(wpa_s->sched_scan_plans);
3242 wpa_s->sched_scan_plans = NULL;
3243 wpa_s->sched_scan_plans_num = 0;
3244 return 0;
3245 }
3246
3247 while ((token = cstr_token(cmd, " ", &context))) {
3248 int ret;
3249 struct sched_scan_plan *scan_plan, *n;
3250
3251 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3252 if (!n)
3253 goto fail;
3254
3255 scan_plans = n;
3256 scan_plan = &scan_plans[num];
3257 num++;
3258
3259 ret = sscanf(token, "%u:%u", &scan_plan->interval,
3260 &scan_plan->iterations);
3261 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3262 wpa_printf(MSG_ERROR,
3263 "Invalid sched scan plan input: %s", token);
3264 goto fail;
3265 }
3266
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003267 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3268 wpa_printf(MSG_WARNING,
3269 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3270 num, scan_plan->interval,
3271 wpa_s->max_sched_scan_plan_interval);
3272 scan_plan->interval =
3273 wpa_s->max_sched_scan_plan_interval;
3274 }
3275
3276 if (ret == 1) {
3277 scan_plan->iterations = 0;
3278 break;
3279 }
3280
3281 if (!scan_plan->iterations) {
3282 wpa_printf(MSG_ERROR,
3283 "scan plan %u: Number of iterations cannot be zero",
3284 num);
3285 goto fail;
3286 }
3287
3288 if (scan_plan->iterations >
3289 wpa_s->max_sched_scan_plan_iterations) {
3290 wpa_printf(MSG_WARNING,
3291 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3292 num, scan_plan->iterations,
3293 wpa_s->max_sched_scan_plan_iterations);
3294 scan_plan->iterations =
3295 wpa_s->max_sched_scan_plan_iterations;
3296 }
3297
3298 wpa_printf(MSG_DEBUG,
3299 "scan plan %u: interval=%u iterations=%u",
3300 num, scan_plan->interval, scan_plan->iterations);
3301 }
3302
3303 if (!scan_plans) {
3304 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3305 goto fail;
3306 }
3307
3308 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3309 wpa_printf(MSG_ERROR,
3310 "All scan plans but the last must specify a number of iterations");
3311 goto fail;
3312 }
3313
3314 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3315 num, scan_plans[num - 1].interval);
3316
3317 if (num > wpa_s->max_sched_scan_plans) {
3318 wpa_printf(MSG_WARNING,
3319 "Too many scheduled scan plans (only %u supported)",
3320 wpa_s->max_sched_scan_plans);
3321 wpa_printf(MSG_WARNING,
3322 "Use only the first %u scan plans, and the last one (in infinite loop)",
3323 wpa_s->max_sched_scan_plans - 1);
3324 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3325 &scan_plans[num - 1], sizeof(*scan_plans));
3326 num = wpa_s->max_sched_scan_plans;
3327 }
3328
3329 os_free(wpa_s->sched_scan_plans);
3330 wpa_s->sched_scan_plans = scan_plans;
3331 wpa_s->sched_scan_plans_num = num;
3332
3333 return 0;
3334
3335fail:
3336 os_free(scan_plans);
3337 wpa_printf(MSG_ERROR, "invalid scan plans list");
3338 return -1;
3339}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003340
3341
3342/**
3343 * wpas_scan_reset_sched_scan - Reset sched_scan state
3344 * @wpa_s: Pointer to wpa_supplicant data
3345 *
3346 * This function is used to cancel a running scheduled scan and to reset an
3347 * internal scan state to continue with a regular scan on the following
3348 * wpa_supplicant_req_scan() calls.
3349 */
3350void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3351{
3352 wpa_s->normal_scans = 0;
3353 if (wpa_s->sched_scanning) {
3354 wpa_s->sched_scan_timed_out = 0;
3355 wpa_s->prev_sched_ssid = NULL;
3356 wpa_supplicant_cancel_sched_scan(wpa_s);
3357 }
3358}
3359
3360
3361void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3362{
3363 /* simulate timeout to restart the sched scan */
3364 wpa_s->sched_scan_timed_out = 1;
3365 wpa_s->prev_sched_ssid = NULL;
3366 wpa_supplicant_cancel_sched_scan(wpa_s);
3367}