blob: 6e6f05d423c33202df2266c977412d9712454439 [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
Sunil Ravi2a14cf12023-11-21 00:54:38 +000027static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s);
28
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029
30static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
31{
32 struct wpa_ssid *ssid;
33 union wpa_event_data data;
34
35 ssid = wpa_supplicant_get_ssid(wpa_s);
36 if (ssid == NULL)
37 return;
38
39 if (wpa_s->current_ssid == NULL) {
40 wpa_s->current_ssid = ssid;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070041 wpas_notify_network_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042 }
43 wpa_supplicant_initiate_eapol(wpa_s);
44 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
45 "network - generating associated event");
46 os_memset(&data, 0, sizeof(data));
47 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
48}
49
50
51#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053 enum wps_request_type *req_type)
54{
55 struct wpa_ssid *ssid;
56 int wps = 0;
57
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080058 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
60 continue;
61
62 wps = 1;
63 *req_type = wpas_wps_get_req_type(ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -070064 if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065 return 2;
66 }
67
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080068#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080069 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
70 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080071 wpa_s->wps->dev.p2p = 1;
72 if (!wps) {
73 wps = 1;
74 *req_type = WPS_REQ_ENROLLEE_INFO;
75 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080076 }
77#endif /* CONFIG_P2P */
78
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 return wps;
80}
81#endif /* CONFIG_WPS */
82
83
Hai Shalomc3565922019-10-28 11:58:20 -070084static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
85 const u8 *mac_addr)
86{
87 u8 *tmp;
88
89 if (params->mac_addr) {
90 params->mac_addr_mask = NULL;
91 os_free(params->mac_addr);
92 params->mac_addr = NULL;
93 }
94
95 params->mac_addr_rand = 1;
96
97 if (!mac_addr)
98 return 0;
99
100 tmp = os_malloc(2 * ETH_ALEN);
101 if (!tmp)
102 return -1;
103
104 os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
105 params->mac_addr = tmp;
106 params->mac_addr_mask = tmp + ETH_ALEN;
107 return 0;
108}
109
110
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800111/**
112 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
113 * @wpa_s: Pointer to wpa_supplicant data
114 * Returns: 0 if no networks are enabled, >0 if networks are enabled
115 *
116 * This function is used to figure out whether any networks (or Interworking
117 * with enabled credentials and auto_interworking) are present in the current
118 * configuration.
119 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700122 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700123 int count = 0, disabled = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800124
125 if (wpa_s->p2p_mgmt)
126 return 0; /* no normal network profiles on p2p_mgmt interface */
127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700129 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700131 else
132 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 ssid = ssid->next;
134 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700135 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
136 wpa_s->conf->auto_interworking)
137 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700138 if (count == 0 && disabled > 0) {
139 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
140 "networks)", disabled);
141 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 return count;
143}
144
145
146static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
147 struct wpa_ssid *ssid)
148{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700149 int min_temp_disabled = 0;
150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 while (ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700152 if (!wpas_network_disabled(wpa_s, ssid)) {
153 int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
154
155 if (temp_disabled <= 0)
156 break;
157
158 if (!min_temp_disabled ||
159 temp_disabled < min_temp_disabled)
160 min_temp_disabled = temp_disabled;
161 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 ssid = ssid->next;
163 }
164
165 /* ap_scan=2 mode - try to associate with each SSID. */
166 if (ssid == NULL) {
167 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
168 "end of scan list - go back to beginning");
169 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700170 wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171 return;
172 }
173 if (ssid->next) {
174 /* Continue from the next SSID on the next attempt. */
175 wpa_s->prev_scan_ssid = ssid;
176 } else {
177 /* Start from the beginning of the SSID list. */
178 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
179 }
180 wpa_supplicant_associate(wpa_s, NULL, ssid);
181}
182
183
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800184static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800186 struct wpa_supplicant *wpa_s = work->wpa_s;
187 struct wpa_driver_scan_params *params = work->ctx;
188 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800190 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800191 if (!work->started) {
192 wpa_scan_free_params(params);
193 return;
194 }
195 wpa_supplicant_notify_scanning(wpa_s, 0);
196 wpas_notify_scan_done(wpa_s, 0);
197 wpa_s->scan_work = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 return;
199 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200
Hai Shalomc3565922019-10-28 11:58:20 -0700201 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
202 wpa_s->wpa_state <= WPA_SCANNING)
203 wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
204
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700205 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
206 wpa_msg(wpa_s, MSG_INFO,
207 "Failed to assign random MAC address for a scan");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700208 wpa_scan_free_params(params);
209 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700210 radio_work_done(work);
211 return;
212 }
213
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800214 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800216 if (wpa_s->clear_driver_scan_cache) {
217 wpa_printf(MSG_DEBUG,
218 "Request driver to clear scan cache due to local BSS flush");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800219 params->only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800220 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800221 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800222 /*
223 * Store the obtained vendor scan cookie (if any) in wpa_s context.
224 * The current design is to allow only one scan request on each
225 * interface, hence having this scan cookie stored in wpa_s context is
226 * fine for now.
227 *
228 * Revisit this logic if concurrent scan operations per interface
229 * is supported.
230 */
231 if (ret == 0)
232 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800233 wpa_scan_free_params(params);
234 work->ctx = NULL;
235 if (ret) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700236 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
237 !wpa_s->beacon_rep_data.token;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800238
239 if (wpa_s->disconnected)
240 retry = 0;
241
Hai Shalom899fcc72020-10-19 14:38:18 -0700242 /* do not retry if operation is not supported */
243 if (ret == -EOPNOTSUPP)
244 retry = 0;
245
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800246 wpa_supplicant_notify_scanning(wpa_s, 0);
247 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800248 if (wpa_s->wpa_state == WPA_SCANNING)
249 wpa_supplicant_set_state(wpa_s,
250 wpa_s->scan_prev_wpa_state);
251 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
252 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800253 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800254
255 if (retry) {
256 /* Restore scan_req since we will try to scan again */
257 wpa_s->scan_req = wpa_s->last_scan_req;
258 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700259 } else if (wpa_s->scan_res_handler) {
260 /* Clear the scan_res_handler */
261 wpa_s->scan_res_handler = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800262 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700263
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000264#ifndef CONFIG_NO_RRM
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700265 if (wpa_s->beacon_rep_data.token)
266 wpas_rrm_refuse_request(wpa_s);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000267#endif /* CONFIG_NO_RRM */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800271
272 os_get_reltime(&wpa_s->scan_trigger_time);
273 wpa_s->scan_runs++;
274 wpa_s->normal_scans++;
275 wpa_s->own_scan_requested = 1;
276 wpa_s->clear_driver_scan_cache = 0;
277 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278}
279
280
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800281/**
282 * wpa_supplicant_trigger_scan - Request driver to start a scan
283 * @wpa_s: Pointer to wpa_supplicant data
284 * @params: Scan parameters
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000285 * @default_ies: Whether or not to use the default IEs in the Probe Request
286 * frames. Note that this will free any existing IEs set in @params, so this
287 * shouldn't be set if the IEs have already been set with
288 * wpa_supplicant_extra_ies(). Otherwise, wpabuf_free() will lead to a
289 * double-free.
290 * @next: Whether or not to perform this scan as the next radio work
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800291 * Returns: 0 on success, -1 on failure
292 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000294 struct wpa_driver_scan_params *params,
295 bool default_ies, bool next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800297 struct wpa_driver_scan_params *ctx;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000298 struct wpabuf *ies = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800300 if (wpa_s->scan_work) {
301 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
302 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303 }
304
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000305 if (default_ies) {
306 if (params->extra_ies_len) {
307 os_free((u8 *) params->extra_ies);
308 params->extra_ies = NULL;
309 params->extra_ies_len = 0;
310 }
311 ies = wpa_supplicant_extra_ies(wpa_s);
312 if (ies) {
313 params->extra_ies = wpabuf_head(ies);
314 params->extra_ies_len = wpabuf_len(ies);
315 }
316 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800317 ctx = wpa_scan_clone_params(params);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000318 if (ies) {
319 wpabuf_free(ies);
320 params->extra_ies = NULL;
321 params->extra_ies_len = 0;
322 }
323 wpa_s->last_scan_all_chan = !params->freqs;
324 wpa_s->last_scan_non_coloc_6ghz = params->non_coloc_6ghz;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700325 if (!ctx ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000326 radio_add_work(wpa_s, 0, "scan", next, wpas_trigger_scan_cb,
327 ctx) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800328 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700329 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800330 return -1;
331 }
332
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000333 wpa_s->wps_scan_done = false;
334
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800335 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800336}
337
338
339static void
340wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
341{
342 struct wpa_supplicant *wpa_s = eloop_ctx;
343
344 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
345
346 if (wpa_supplicant_req_sched_scan(wpa_s))
347 wpa_supplicant_req_scan(wpa_s, 0, 0);
348}
349
350
351static void
352wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
353{
354 struct wpa_supplicant *wpa_s = eloop_ctx;
355
356 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
357
358 wpa_s->sched_scan_timed_out = 1;
359 wpa_supplicant_cancel_sched_scan(wpa_s);
360}
361
362
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700363static int
364wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
365 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800366{
367 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800369 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800370 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800371 if (ret)
372 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800373 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374 wpa_s->sched_scanning = 1;
375
376 return ret;
377}
378
379
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700380static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381{
382 int ret;
383
384 ret = wpa_drv_stop_sched_scan(wpa_s);
385 if (ret) {
386 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
387 /* TODO: what to do if stopping fails? */
388 return -1;
389 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390
391 return ret;
392}
393
394
395static struct wpa_driver_scan_filter *
396wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
397{
398 struct wpa_driver_scan_filter *ssids;
399 struct wpa_ssid *ssid;
400 size_t count;
401
402 *num_ssids = 0;
403 if (!conf->filter_ssids)
404 return NULL;
405
406 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
407 if (ssid->ssid && ssid->ssid_len)
408 count++;
409 }
410 if (count == 0)
411 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800412 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 if (ssids == NULL)
414 return NULL;
415
416 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
417 if (!ssid->ssid || !ssid->ssid_len)
418 continue;
419 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
420 ssids[*num_ssids].ssid_len = ssid->ssid_len;
421 (*num_ssids)++;
422 }
423
424 return ssids;
425}
426
427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800428static void wpa_supplicant_optimize_freqs(
429 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
430{
431#ifdef CONFIG_P2P
432 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
433 wpa_s->go_params) {
434 /* Optimize provisioning state scan based on GO information */
435 if (wpa_s->p2p_in_provisioning < 5 &&
436 wpa_s->go_params->freq > 0) {
437 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
438 "preferred frequency %d MHz",
439 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800440 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800441 if (params->freqs)
442 params->freqs[0] = wpa_s->go_params->freq;
443 } else if (wpa_s->p2p_in_provisioning < 8 &&
444 wpa_s->go_params->freq_list[0]) {
445 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
446 "channels");
447 int_array_concat(&params->freqs,
448 wpa_s->go_params->freq_list);
449 if (params->freqs)
450 int_array_sort_unique(params->freqs);
451 }
452 wpa_s->p2p_in_provisioning++;
453 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700454
455 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
Sunil Raviaf8751c2023-03-29 11:35:17 -0700456 struct wpa_ssid *ssid = wpa_s->current_ssid;
457
Dmitry Shmidt15907092014-03-25 10:42:57 -0700458 /*
Matthew Wangdcf19452022-11-07 20:42:52 -0800459 * Perform a single-channel scan if the GO has already been
460 * discovered on another non-P2P interface. Note that a scan
Sunil Raviaf8751c2023-03-29 11:35:17 -0700461 * initiated by a P2P interface (e.g., the device interface)
Matthew Wangdcf19452022-11-07 20:42:52 -0800462 * should already have sufficient IEs and scan results will be
463 * fetched on interface creation in that case.
464 */
Sunil Raviaf8751c2023-03-29 11:35:17 -0700465 if (wpa_s->p2p_in_invitation == 1 && ssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -0800466 struct wpa_supplicant *ifs;
467 struct wpa_bss *bss = NULL;
Sunil Raviaf8751c2023-03-29 11:35:17 -0700468 const u8 *bssid = ssid->bssid_set ? ssid->bssid : NULL;
469
Matthew Wangdcf19452022-11-07 20:42:52 -0800470 dl_list_for_each(ifs, &wpa_s->radio->ifaces,
471 struct wpa_supplicant, radio_list) {
472 bss = wpa_bss_get(ifs, bssid, ssid->ssid,
473 ssid->ssid_len);
474 if (bss)
475 break;
476 }
477 if (bss && !disabled_freq(wpa_s, bss->freq)) {
478 params->freqs = os_calloc(2, sizeof(int));
Matthew Wangafc981e2023-03-17 21:30:12 +0000479 if (params->freqs) {
480 wpa_dbg(wpa_s, MSG_DEBUG,
481 "P2P: Scan only the known GO frequency %d MHz during invitation",
482 bss->freq);
Matthew Wangdcf19452022-11-07 20:42:52 -0800483 params->freqs[0] = bss->freq;
Matthew Wangafc981e2023-03-17 21:30:12 +0000484 }
Matthew Wangdcf19452022-11-07 20:42:52 -0800485 }
486 }
Sunil Raviaf8751c2023-03-29 11:35:17 -0700487
Matthew Wangdcf19452022-11-07 20:42:52 -0800488 /*
Dmitry Shmidt15907092014-03-25 10:42:57 -0700489 * Optimize scan based on GO information during persistent
490 * group reinvocation
491 */
Matthew Wangafc981e2023-03-17 21:30:12 +0000492 if (!params->freqs && wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700493 wpa_s->p2p_invite_go_freq > 0) {
Matthew Wang36173112022-11-07 21:48:44 -0800494 if (wpa_s->p2p_invite_go_freq == 2 ||
495 wpa_s->p2p_invite_go_freq == 5) {
Matthew Wang36173112022-11-07 21:48:44 -0800496 enum hostapd_hw_mode mode;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000497
498 wpa_dbg(wpa_s, MSG_DEBUG,
499 "P2P: Scan only GO preferred band %d GHz during invitation",
500 wpa_s->p2p_invite_go_freq);
501
502 if (!wpa_s->hw.modes)
Matthew Wang36173112022-11-07 21:48:44 -0800503 return;
504 mode = wpa_s->p2p_invite_go_freq == 5 ?
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000505 HOSTAPD_MODE_IEEE80211A :
506 HOSTAPD_MODE_IEEE80211G;
Matthew Wang36173112022-11-07 21:48:44 -0800507 if (wpa_s->p2p_in_invitation <= 2)
508 wpa_add_scan_freqs_list(wpa_s, mode,
509 params, false,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000510 false, true);
511 if (!params->freqs || params->freqs[0] == 0)
Matthew Wang36173112022-11-07 21:48:44 -0800512 wpa_add_scan_freqs_list(wpa_s, mode,
513 params, false,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000514 false, false);
Matthew Wang36173112022-11-07 21:48:44 -0800515 } else {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000516 wpa_dbg(wpa_s, MSG_DEBUG,
517 "P2P: Scan only GO preferred frequency %d MHz during invitation",
Matthew Wang36173112022-11-07 21:48:44 -0800518 wpa_s->p2p_invite_go_freq);
519 params->freqs = os_calloc(2, sizeof(int));
520 if (params->freqs)
521 params->freqs[0] =
522 wpa_s->p2p_invite_go_freq;
523 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700524 }
525 wpa_s->p2p_in_invitation++;
526 if (wpa_s->p2p_in_invitation > 20) {
527 /*
528 * This should not really happen since the variable is
529 * cleared on group removal, but if it does happen, make
530 * sure we do not get stuck in special invitation scan
531 * mode.
532 */
533 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
534 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +0000535 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -0700536 }
537 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800538#endif /* CONFIG_P2P */
539
540#ifdef CONFIG_WPS
541 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
542 /*
543 * Optimize post-provisioning scan based on channel used
544 * during provisioning.
545 */
546 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
547 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800548 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549 if (params->freqs)
550 params->freqs[0] = wpa_s->wps_freq;
551 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800552 } else if (wpa_s->after_wps)
553 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800554
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800555 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
556 {
557 /* Optimize provisioning scan based on already known channel */
558 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
559 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800560 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800561 if (params->freqs)
562 params->freqs[0] = wpa_s->wps_freq;
563 wpa_s->known_wps_freq = 0; /* only do this once */
564 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800565#endif /* CONFIG_WPS */
566}
567
568
569#ifdef CONFIG_INTERWORKING
570static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
571 struct wpabuf *buf)
572{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
574 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
575 1 + ETH_ALEN);
576 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
577 /* No Venue Info */
578 if (!is_zero_ether_addr(wpa_s->conf->hessid))
579 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
580}
581#endif /* CONFIG_INTERWORKING */
582
583
Hai Shalomce48b4a2018-09-05 11:41:35 -0700584#ifdef CONFIG_MBO
585static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
586 struct wpabuf **ie)
587{
588 if (wpabuf_resize(ie, 5)) {
589 wpa_printf(MSG_DEBUG,
590 "Failed to allocate space for FILS Request Parameters element");
591 return;
592 }
593
594 /* FILS Request Parameters element */
595 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
596 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
597 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
598 /* Parameter control bitmap */
599 wpabuf_put_u8(*ie, 0);
600 /* Max Channel Time field - contains the value of MaxChannelTime
601 * parameter of the MLME-SCAN.request primitive represented in units of
602 * TUs, as an unsigned integer. A Max Channel Time field value of 255
603 * is used to indicate any duration of more than 254 TUs, or an
604 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
605 */
606 wpabuf_put_u8(*ie, 255);
607}
608#endif /* CONFIG_MBO */
609
610
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700611void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
612{
613 struct wpabuf *default_ies = NULL;
614 u8 ext_capab[18];
Hai Shalom60840252021-02-19 19:02:11 -0800615 int ext_capab_len, frame_id;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700616 enum wpa_driver_if_type type = WPA_IF_STATION;
617
618#ifdef CONFIG_P2P
619 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
620 type = WPA_IF_P2P_CLIENT;
621#endif /* CONFIG_P2P */
622
623 wpa_drv_get_ext_capa(wpa_s, type);
624
625 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000626 sizeof(ext_capab), NULL);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700627 if (ext_capab_len > 0 &&
628 wpabuf_resize(&default_ies, ext_capab_len) == 0)
629 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
630
631#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -0700632 if (wpa_s->enable_oce & OCE_STA)
633 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700634 /* Send MBO and OCE capabilities */
635 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700636 wpas_mbo_scan_ie(wpa_s, default_ies);
637#endif /* CONFIG_MBO */
638
Hai Shalom60840252021-02-19 19:02:11 -0800639 if (type == WPA_IF_P2P_CLIENT)
640 frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
641 else
642 frame_id = VENDOR_ELEM_PROBE_REQ;
643
644 if (wpa_s->vendor_elem[frame_id]) {
645 size_t len;
646
647 len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
648 if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
649 wpabuf_put_buf(default_ies,
650 wpa_s->vendor_elem[frame_id]);
651 }
652
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700653 if (default_ies)
654 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
655 wpabuf_len(default_ies));
656 wpabuf_free(default_ies);
657}
658
659
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000660static struct wpabuf * wpa_supplicant_ml_probe_ie(int mld_id, u16 links)
661{
662 struct wpabuf *extra_ie;
663 u16 control = MULTI_LINK_CONTROL_TYPE_PROBE_REQ;
664 size_t len = 3 + 4 + 4 * MAX_NUM_MLD_LINKS;
665 u8 link_id;
666 u8 *len_pos;
667
668 if (mld_id >= 0) {
669 control |= EHT_ML_PRES_BM_PROBE_REQ_AP_MLD_ID;
670 len++;
671 }
672
673 extra_ie = wpabuf_alloc(len);
674 if (!extra_ie)
675 return NULL;
676
677 wpabuf_put_u8(extra_ie, WLAN_EID_EXTENSION);
678 len_pos = wpabuf_put(extra_ie, 1);
679 wpabuf_put_u8(extra_ie, WLAN_EID_EXT_MULTI_LINK);
680
681 wpabuf_put_le16(extra_ie, control);
682
683 /* common info length and MLD ID (if requested) */
684 if (mld_id >= 0) {
685 wpabuf_put_u8(extra_ie, 2);
686 wpabuf_put_u8(extra_ie, mld_id);
687
688 wpa_printf(MSG_DEBUG, "MLD: ML probe targeted at MLD ID %d",
689 mld_id);
690 } else {
691 wpabuf_put_u8(extra_ie, 1);
692
693 wpa_printf(MSG_DEBUG, "MLD: ML probe targeted at receiving AP");
694 }
695
696 if (!links)
697 wpa_printf(MSG_DEBUG, "MLD: Probing all links");
698 else
699 wpa_printf(MSG_DEBUG, "MLD: Probing links 0x%04x", links);
700
Sunil Ravi88611412024-06-28 17:34:56 +0000701 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
702 if (!(links & BIT(link_id)))
703 continue;
704
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000705 wpabuf_put_u8(extra_ie, EHT_ML_SUB_ELEM_PER_STA_PROFILE);
706
707 /* Subelement length includes only the control */
708 wpabuf_put_u8(extra_ie, 2);
709
710 control = link_id | EHT_PER_STA_CTRL_COMPLETE_PROFILE_MSK;
711
712 wpabuf_put_le16(extra_ie, control);
713 }
714
715 *len_pos = (u8 *) wpabuf_put(extra_ie, 0) - len_pos - 1;
716
717 return extra_ie;
718}
719
720
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700721static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800722{
723 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700724 u8 ext_capab[18];
725 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800726#ifdef CONFIG_WPS
727 int wps = 0;
728 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
729#endif /* CONFIG_WPS */
730
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000731 if (!is_zero_ether_addr(wpa_s->ml_probe_bssid)) {
732 extra_ie = wpa_supplicant_ml_probe_ie(wpa_s->ml_probe_mld_id,
733 wpa_s->ml_probe_links);
734
735 /* No other elements should be included in the probe request */
736 wpa_printf(MSG_DEBUG, "MLD: Scan including only ML element");
737 return extra_ie;
738 }
739
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700740#ifdef CONFIG_P2P
741 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
742 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
743 else
744#endif /* CONFIG_P2P */
745 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
746
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700747 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000748 sizeof(ext_capab), NULL);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700749 if (ext_capab_len > 0 &&
750 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
751 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800753#ifdef CONFIG_INTERWORKING
754 if (wpa_s->conf->interworking &&
755 wpabuf_resize(&extra_ie, 100) == 0)
756 wpas_add_interworking_elements(wpa_s, extra_ie);
757#endif /* CONFIG_INTERWORKING */
758
Hai Shalomce48b4a2018-09-05 11:41:35 -0700759#ifdef CONFIG_MBO
760 if (wpa_s->enable_oce & OCE_STA)
761 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
762#endif /* CONFIG_MBO */
763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764#ifdef CONFIG_WPS
765 wps = wpas_wps_in_use(wpa_s, &req_type);
766
767 if (wps) {
768 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700769 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
770 DEV_PW_DEFAULT,
771 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800772 wpa_s->wps->uuid, req_type,
773 0, NULL);
774 if (wps_ie) {
775 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
776 wpabuf_put_buf(extra_ie, wps_ie);
777 wpabuf_free(wps_ie);
778 }
779 }
780
781#ifdef CONFIG_P2P
782 if (wps) {
783 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
784 if (wpabuf_resize(&extra_ie, ielen) == 0)
785 wpas_p2p_scan_ie(wpa_s, extra_ie);
786 }
787#endif /* CONFIG_P2P */
788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800789 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800791#endif /* CONFIG_WPS */
792
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700793#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -0800794 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
795 wpas_hs20_add_indication(extra_ie, -1, 0);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700796#endif /* CONFIG_HS20 */
797
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800798#ifdef CONFIG_FST
799 if (wpa_s->fst_ies &&
800 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
801 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
802#endif /* CONFIG_FST */
803
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800804#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700805 /* Send MBO and OCE capabilities */
806 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800807 wpas_mbo_scan_ie(wpa_s, extra_ie);
808#endif /* CONFIG_MBO */
809
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700810 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
811 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
812
813 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
814 wpabuf_put_buf(extra_ie, buf);
815 }
816
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800817 return extra_ie;
818}
819
820
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800821#ifdef CONFIG_P2P
822
823/*
824 * Check whether there are any enabled networks or credentials that could be
825 * used for a non-P2P connection.
826 */
827static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
828{
829 struct wpa_ssid *ssid;
830
831 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
832 if (wpas_network_disabled(wpa_s, ssid))
833 continue;
834 if (!ssid->p2p_group)
835 return 1;
836 }
837
838 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
839 wpa_s->conf->auto_interworking)
840 return 1;
841
842 return 0;
843}
844
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700845#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800846
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800847
Hai Shalom60840252021-02-19 19:02:11 -0800848int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
849 enum hostapd_hw_mode band,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000850 struct wpa_driver_scan_params *params,
851 bool is_6ghz, bool only_6ghz_psc,
Matthew Wang36173112022-11-07 21:48:44 -0800852 bool exclude_radar)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700853{
854 /* Include only supported channels for the specified band */
855 struct hostapd_hw_modes *mode;
Hai Shalom60840252021-02-19 19:02:11 -0800856 int num_chans = 0;
857 int *freqs, i;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700858
Hai Shalomfdcde762020-04-02 11:19:20 -0700859 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000860 if (!mode || !mode->num_channels)
Hai Shalom60840252021-02-19 19:02:11 -0800861 return -1;
862
863 if (params->freqs) {
864 while (params->freqs[num_chans])
865 num_chans++;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700866 }
867
Hai Shalom60840252021-02-19 19:02:11 -0800868 freqs = os_realloc(params->freqs,
869 (num_chans + mode->num_channels + 1) * sizeof(int));
870 if (!freqs)
871 return -1;
872
873 params->freqs = freqs;
874 for (i = 0; i < mode->num_channels; i++) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700875 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
876 continue;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000877 if (exclude_radar &&
878 (mode->channels[i].flag & HOSTAPD_CHAN_RADAR))
Matthew Wang36173112022-11-07 21:48:44 -0800879 continue;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000880
881 if (is_6ghz && only_6ghz_psc &&
882 !is_6ghz_psc_frequency(mode->channels[i].freq))
883 continue;
884
Hai Shalom60840252021-02-19 19:02:11 -0800885 params->freqs[num_chans++] = mode->channels[i].freq;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700886 }
Hai Shalom60840252021-02-19 19:02:11 -0800887 params->freqs[num_chans] = 0;
888
889 return 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700890}
891
892
893static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
894 struct wpa_driver_scan_params *params)
895{
896 if (wpa_s->hw.modes == NULL)
897 return; /* unknown what channels the driver supports */
898 if (params->freqs)
899 return; /* already using a limited channel set */
Hai Shalom60840252021-02-19 19:02:11 -0800900
901 if (wpa_s->setband_mask & WPA_SETBAND_5G)
902 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000903 false, false, false);
Hai Shalom60840252021-02-19 19:02:11 -0800904 if (wpa_s->setband_mask & WPA_SETBAND_2G)
905 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000906 false, false, false);
Hai Shalom60840252021-02-19 19:02:11 -0800907 if (wpa_s->setband_mask & WPA_SETBAND_6G)
908 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000909 true, false, false);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700910}
911
912
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800913static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
914 struct wpa_driver_scan_params *params,
915 size_t max_ssids, const u8 *ssid, size_t ssid_len)
916{
917 unsigned int j;
918
919 for (j = 0; j < params->num_ssids; j++) {
920 if (params->ssids[j].ssid_len == ssid_len &&
921 params->ssids[j].ssid &&
922 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
923 return; /* already in the list */
924 }
925
926 if (params->num_ssids + 1 > max_ssids) {
927 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
928 return;
929 }
930
931 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
932 wpa_ssid_txt(ssid, ssid_len));
933
934 params->ssids[params->num_ssids].ssid = ssid;
935 params->ssids[params->num_ssids].ssid_len = ssid_len;
936 params->num_ssids++;
937}
938
939
940static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
941 struct wpa_driver_scan_params *params,
942 struct wpa_ssid *ssid, size_t max_ssids)
943{
944#ifdef CONFIG_OWE
945 struct wpa_bss *bss;
946
947 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
948 return;
949
950 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
951 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
952
953 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
954 const u8 *owe, *pos, *end;
955 const u8 *owe_ssid;
956 size_t owe_ssid_len;
957
958 if (bss->ssid_len != ssid->ssid_len ||
959 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
960 continue;
961
962 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
963 if (!owe || owe[1] < 4)
964 continue;
965
966 pos = owe + 6;
967 end = owe + 2 + owe[1];
968
969 /* Must include BSSID and ssid_len */
970 if (end - pos < ETH_ALEN + 1)
971 return;
972
973 /* Skip BSSID */
974 pos += ETH_ALEN;
975 owe_ssid_len = *pos++;
976 owe_ssid = pos;
977
978 if ((size_t) (end - pos) < owe_ssid_len ||
979 owe_ssid_len > SSID_MAX_LEN)
980 return;
981
982 wpa_printf(MSG_DEBUG,
983 "OWE: scan_ssids: transition mode OWE ssid=%s",
984 wpa_ssid_txt(owe_ssid, owe_ssid_len));
985
986 wpa_add_scan_ssid(wpa_s, params, max_ssids,
987 owe_ssid, owe_ssid_len);
988 return;
989 }
990#endif /* CONFIG_OWE */
991}
992
993
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700994static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
995 struct wpa_driver_scan_params *params,
996 size_t max_ssids)
997{
998 unsigned int i;
999 struct wpa_ssid *ssid;
1000
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001001 /*
1002 * For devices with max_ssids greater than 1, leave the last slot empty
1003 * for adding the wildcard scan entry.
1004 */
1005 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
1006
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001007 for (i = 0; i < wpa_s->scan_id_count; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001008 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001009 if (!ssid)
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001010 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001011 if (ssid->scan_ssid)
1012 wpa_add_scan_ssid(wpa_s, params, max_ssids,
1013 ssid->ssid, ssid->ssid_len);
1014 /*
1015 * Also add the SSID of the OWE BSS, to allow discovery of
1016 * transition mode APs more quickly.
1017 */
1018 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001019 }
1020
1021 wpa_s->scan_id_count = 0;
1022}
1023
1024
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001025static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
1026 struct wpa_driver_scan_params *params,
1027 size_t max_ssids)
1028{
1029 unsigned int i;
1030
1031 if (wpa_s->ssids_from_scan_req == NULL ||
1032 wpa_s->num_ssids_from_scan_req == 0)
1033 return 0;
1034
1035 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
1036 wpa_s->num_ssids_from_scan_req = max_ssids;
1037 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
1038 (unsigned int) max_ssids);
1039 }
1040
1041 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
1042 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
1043 params->ssids[i].ssid_len =
1044 wpa_s->ssids_from_scan_req[i].ssid_len;
1045 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
1046 params->ssids[i].ssid,
1047 params->ssids[i].ssid_len);
1048 }
1049
1050 params->num_ssids = wpa_s->num_ssids_from_scan_req;
1051 wpa_s->num_ssids_from_scan_req = 0;
1052 return 1;
1053}
1054
1055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
1057{
1058 struct wpa_supplicant *wpa_s = eloop_ctx;
1059 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001060 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001061 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001063 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001065 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066
Dmitry Shmidt29333592017-01-09 12:27:11 -08001067 wpa_s->ignore_post_flush_scan_res = 0;
1068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1070 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
1071 return;
1072 }
1073
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001074 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001075 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1077 return;
1078 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001079
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -07001080 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001081 /*
1082 * If we are already in scanning state, we shall reschedule the
1083 * the incoming scan request.
1084 */
1085 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
1086 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -07001087 return;
1088 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001089
Dmitry Shmidt04949592012-07-19 12:16:46 -07001090 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001091 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
1093 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1094 return;
1095 }
1096
1097 if (wpa_s->conf->ap_scan != 0 &&
1098 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
1099 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
1100 "overriding ap_scan configuration");
1101 wpa_s->conf->ap_scan = 0;
1102 wpas_notify_ap_scan_changed(wpa_s);
1103 }
1104
1105 if (wpa_s->conf->ap_scan == 0) {
1106 wpa_supplicant_gen_assoc_event(wpa_s);
1107 return;
1108 }
1109
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001110 ssid = NULL;
1111 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
1112 wpa_s->connect_without_scan) {
1113 connect_without_scan = 1;
1114 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1115 if (ssid == wpa_s->connect_without_scan)
1116 break;
1117 }
1118 }
1119
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001120 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001121 if (p2p_in_prog && p2p_in_prog != 2 &&
1122 (!ssid ||
1123 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001124 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
1125 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001126 return;
1127 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001128
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001129 /*
1130 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
1131 * used for changing modes inside wpa_supplicant (roaming,
1132 * auto-reconnect, etc). Discarding the scan might hurt these processes.
1133 * The normal use case for PNO is to suspend the host immediately after
1134 * starting PNO, so the periodic 100 ms attempts to run the scan do not
1135 * normally happen in practice multiple times, i.e., this is simply
1136 * restarting scanning once the host is woken up and PNO stopped.
1137 */
1138 if (wpa_s->pno || wpa_s->pno_sched_pending) {
1139 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
1140 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1141 return;
1142 }
1143
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001144 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 max_ssids = 1;
1146 else {
1147 max_ssids = wpa_s->max_scan_ssids;
1148 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1149 max_ssids = WPAS_MAX_SCAN_SSIDS;
1150 }
1151
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001152 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001153 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001155 if (connect_without_scan) {
1156 wpa_s->connect_without_scan = NULL;
1157 if (ssid) {
1158 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1159 "without scan step");
1160 wpa_supplicant_associate(wpa_s, NULL, ssid);
1161 return;
1162 }
1163 }
1164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 os_memset(&params, 0, sizeof(params));
1166
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001167 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1169 wpa_s->wpa_state == WPA_INACTIVE)
1170 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1171
Dmitry Shmidt04949592012-07-19 12:16:46 -07001172 /*
1173 * If autoscan has set its own scanning parameters
1174 */
1175 if (wpa_s->autoscan_params != NULL) {
1176 scan_params = wpa_s->autoscan_params;
1177 goto scan;
1178 }
1179
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001180 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1181 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1182 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1183 goto ssid_list_set;
1184 }
1185
Dmitry Shmidt04949592012-07-19 12:16:46 -07001186#ifdef CONFIG_P2P
1187 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001188 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001189 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1190 wpa_s->p2p_in_provisioning,
1191 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001192 params.ssids[0].ssid = wpa_s->go_params->ssid;
1193 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1194 params.num_ssids = 1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001195 params.bssid = wpa_s->go_params->peer_interface_addr;
1196 wpa_printf(MSG_DEBUG, "P2P: Use specific BSSID " MACSTR
1197 " (peer interface address) for scan",
1198 MAC2STR(params.bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001199 goto ssid_list_set;
1200 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07001201
1202 if (wpa_s->p2p_in_invitation) {
1203 if (wpa_s->current_ssid) {
1204 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1205 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1206 params.ssids[0].ssid_len =
1207 wpa_s->current_ssid->ssid_len;
1208 params.num_ssids = 1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001209 if (wpa_s->current_ssid->bssid_set) {
1210 params.bssid = wpa_s->current_ssid->bssid;
1211 wpa_printf(MSG_DEBUG, "P2P: Use specific BSSID "
1212 MACSTR " for scan",
1213 MAC2STR(params.bssid));
1214 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07001215 } else {
1216 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1217 }
1218 goto ssid_list_set;
1219 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001220#endif /* CONFIG_P2P */
1221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 /* Find the starting point from which to continue scanning */
1223 ssid = wpa_s->conf->ssid;
1224 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1225 while (ssid) {
1226 if (ssid == wpa_s->prev_scan_ssid) {
1227 ssid = ssid->next;
1228 break;
1229 }
1230 ssid = ssid->next;
1231 }
1232 }
1233
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001234 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001235#ifdef CONFIG_AP
1236 !wpa_s->ap_iface &&
1237#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001238 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001239 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001240 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 wpa_supplicant_assoc_try(wpa_s, ssid);
1242 return;
1243 } else if (wpa_s->conf->ap_scan == 2) {
1244 /*
1245 * User-initiated scan request in ap_scan == 2; scan with
1246 * wildcard SSID.
1247 */
1248 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001249 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1250 /*
1251 * Perform single-channel single-SSID scan for
1252 * reassociate-to-same-BSS operation.
1253 */
1254 /* Setup SSID */
1255 ssid = wpa_s->current_ssid;
1256 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1257 ssid->ssid, ssid->ssid_len);
1258 params.ssids[0].ssid = ssid->ssid;
1259 params.ssids[0].ssid_len = ssid->ssid_len;
1260 params.num_ssids = 1;
1261
1262 /*
1263 * Allocate memory for frequency array, allocate one extra
1264 * slot for the zero-terminator.
1265 */
1266 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001267 if (params.freqs) {
1268 params.freqs[0] = wpa_s->assoc_freq;
1269 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001270 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07001271
1272 /*
1273 * Reset the reattach flag so that we fall back to full scan if
1274 * this scan fails.
1275 */
1276 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277 } else {
1278 struct wpa_ssid *start = ssid, *tssid;
1279 int freqs_set = 0;
1280 if (ssid == NULL && max_ssids > 1)
1281 ssid = wpa_s->conf->ssid;
1282 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001283 if (!wpas_network_disabled(wpa_s, ssid) &&
1284 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1286 ssid->ssid, ssid->ssid_len);
1287 params.ssids[params.num_ssids].ssid =
1288 ssid->ssid;
1289 params.ssids[params.num_ssids].ssid_len =
1290 ssid->ssid_len;
1291 params.num_ssids++;
1292 if (params.num_ssids + 1 >= max_ssids)
1293 break;
1294 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001295
1296 if (!wpas_network_disabled(wpa_s, ssid)) {
1297 /*
1298 * Also add the SSID of the OWE BSS, to allow
1299 * discovery of transition mode APs more
1300 * quickly.
1301 */
1302 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1303 max_ssids);
1304 }
1305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 ssid = ssid->next;
1307 if (ssid == start)
1308 break;
1309 if (ssid == NULL && max_ssids > 1 &&
1310 start != wpa_s->conf->ssid)
1311 ssid = wpa_s->conf->ssid;
1312 }
1313
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001314 if (wpa_s->scan_id_count &&
1315 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1316 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1317
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001318 for (tssid = wpa_s->conf->ssid;
1319 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1320 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001321 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07001323 if (((params.freqs || !freqs_set) &&
1324 tssid->scan_freq) &&
1325 int_array_len(params.freqs) < 100) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 int_array_concat(&params.freqs,
1327 tssid->scan_freq);
1328 } else {
1329 os_free(params.freqs);
1330 params.freqs = NULL;
1331 }
1332 freqs_set = 1;
1333 }
1334 int_array_sort_unique(params.freqs);
1335 }
1336
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001337 if (ssid && max_ssids == 1) {
1338 /*
1339 * If the driver is limited to 1 SSID at a time interleave
1340 * wildcard SSID scans with specific SSID scans to avoid
1341 * waiting a long time for a wildcard scan.
1342 */
1343 if (!wpa_s->prev_scan_wildcard) {
1344 params.ssids[0].ssid = NULL;
1345 params.ssids[0].ssid_len = 0;
1346 wpa_s->prev_scan_wildcard = 1;
1347 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1348 "wildcard SSID (Interleave with specific)");
1349 } else {
1350 wpa_s->prev_scan_ssid = ssid;
1351 wpa_s->prev_scan_wildcard = 0;
1352 wpa_dbg(wpa_s, MSG_DEBUG,
1353 "Starting AP scan for specific SSID: %s",
1354 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001356 } else if (ssid) {
1357 /* max_ssids > 1 */
1358
1359 wpa_s->prev_scan_ssid = ssid;
1360 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1361 "the scan request");
1362 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001363 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1364 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1365 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001366 } else if (wpa_s->conf->passive_scan) {
1367 wpa_dbg(wpa_s, MSG_DEBUG,
1368 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 } else {
1370 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1371 params.num_ssids++;
1372 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1373 "SSID");
1374 }
1375
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001376ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001377 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001378 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001380 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001381 wpa_s->manual_scan_only_new) {
1382 wpa_printf(MSG_DEBUG,
1383 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001384 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001385 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001386
1387 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1388 wpa_s->manual_scan_freqs) {
1389 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1390 params.freqs = wpa_s->manual_scan_freqs;
1391 wpa_s->manual_scan_freqs = NULL;
1392 }
1393
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001394 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1395 wpa_dbg(wpa_s, MSG_DEBUG,
1396 "Limit select_network scan to specified channels");
1397 params.freqs = wpa_s->select_network_scan_freqs;
1398 wpa_s->select_network_scan_freqs = NULL;
1399 }
1400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1402 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1403 "generated frequency list");
1404 params.freqs = wpa_s->next_scan_freqs;
1405 } else
1406 os_free(wpa_s->next_scan_freqs);
1407 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001408 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001410 /* See if user specified frequencies. If so, scan only those. */
Hai Shalom60840252021-02-19 19:02:11 -08001411 if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1412 wpa_s->conf->initial_freq_list && !params.freqs) {
1413 wpa_dbg(wpa_s, MSG_DEBUG,
1414 "Optimize scan based on conf->initial_freq_list");
1415 int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1416 } else if (wpa_s->conf->freq_list && !params.freqs) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001417 wpa_dbg(wpa_s, MSG_DEBUG,
1418 "Optimize scan based on conf->freq_list");
1419 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1420 }
1421
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001422 /* Use current associated channel? */
1423 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001424 unsigned int num = wpa_s->num_multichan_concurrent;
1425
1426 params.freqs = os_calloc(num + 1, sizeof(int));
1427 if (params.freqs) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001428 num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1429 false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001430 if (num > 0) {
1431 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1432 "current operating channels since "
1433 "scan_cur_freq is enabled");
1434 } else {
1435 os_free(params.freqs);
1436 params.freqs = NULL;
1437 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001438 }
1439 }
1440
Hai Shalomce48b4a2018-09-05 11:41:35 -07001441#ifdef CONFIG_MBO
1442 if (wpa_s->enable_oce & OCE_STA)
1443 params.oce_scan = 1;
1444#endif /* CONFIG_MBO */
1445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1447 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001448 if (extra_ie) {
1449 params.extra_ies = wpabuf_head(extra_ie);
1450 params.extra_ies_len = wpabuf_len(extra_ie);
1451 }
1452
1453#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001454 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001455 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456 /*
1457 * The interface may not yet be in P2P mode, so we have to
1458 * explicitly request P2P probe to disable CCK rates.
1459 */
1460 params.p2p_probe = 1;
1461 }
1462#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001464 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001465 wpa_s->wpa_state <= WPA_SCANNING)
1466 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001467
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001468 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1469 struct wpa_bss *bss;
1470
1471 params.bssid = wpa_s->next_scan_bssid;
1472 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
Hai Shalomfdcde762020-04-02 11:19:20 -07001473 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1474 bss && bss->ssid_len && params.num_ssids == 1 &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001475 params.ssids[0].ssid_len == 0) {
1476 params.ssids[0].ssid = bss->ssid;
1477 params.ssids[0].ssid_len = bss->ssid_len;
1478 wpa_dbg(wpa_s, MSG_DEBUG,
1479 "Scan a previously specified BSSID " MACSTR
1480 " and SSID %s",
1481 MAC2STR(params.bssid),
1482 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1483 } else {
1484 wpa_dbg(wpa_s, MSG_DEBUG,
1485 "Scan a previously specified BSSID " MACSTR,
1486 MAC2STR(params.bssid));
1487 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001488 } else if (!is_zero_ether_addr(wpa_s->ml_probe_bssid)) {
1489 wpa_printf(MSG_DEBUG, "Scanning for ML probe request");
1490 params.bssid = wpa_s->ml_probe_bssid;
1491 params.min_probe_req_content = true;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001492 }
1493
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001494
Sunil8cd6f4d2022-06-28 18:40:46 +00001495 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1496 wpa_s->manual_non_coloc_6ghz) {
1497 wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1498 params.non_coloc_6ghz = 1;
1499 }
1500
Dmitry Shmidt04949592012-07-19 12:16:46 -07001501 scan_params = &params;
1502
1503scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001504#ifdef CONFIG_P2P
1505 /*
1506 * If the driver does not support multi-channel concurrency and a
1507 * virtual interface that shares the same radio with the wpa_s interface
1508 * is operating there may not be need to scan other channels apart from
1509 * the current operating channel on the other virtual interface. Filter
1510 * out other channels in case we are trying to find a connection for a
1511 * station interface when we are not configured to prefer station
1512 * connection and a concurrent operation is already in process.
1513 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001514 if (wpa_s->scan_for_connection &&
1515 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001516 !scan_params->freqs && !params.freqs &&
1517 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001518 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1519 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001520 unsigned int num = wpa_s->num_multichan_concurrent;
1521
1522 params.freqs = os_calloc(num + 1, sizeof(int));
1523 if (params.freqs) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001524 /*
1525 * Exclude the operating frequency of the current
1526 * interface since we're looking to transition off of
1527 * it.
1528 */
1529 num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1530 true);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001531 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1532 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1533 } else {
1534 os_free(params.freqs);
1535 params.freqs = NULL;
1536 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001537 }
1538 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001539
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001540 if (!params.freqs && wpas_is_6ghz_supported(wpa_s, true) &&
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001541 (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning))
1542 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001543#endif /* CONFIG_P2P */
1544
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001545 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params, false, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001547 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1548 !wpa_s->manual_scan_freqs) {
1549 /* Restore manual_scan_freqs for the next attempt */
1550 wpa_s->manual_scan_freqs = params.freqs;
1551 params.freqs = NULL;
1552 }
1553
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001554 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555 os_free(params.freqs);
1556 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001557 os_free(params.mac_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558
1559 if (ret) {
1560 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001561 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1562 wpa_supplicant_set_state(wpa_s,
1563 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001564 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001565 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001567 } else {
1568 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001569#ifdef CONFIG_INTERWORKING
1570 wpa_s->interworking_fast_assoc_tried = 0;
1571#endif /* CONFIG_INTERWORKING */
Hai Shalomfdcde762020-04-02 11:19:20 -07001572 wpa_s->next_scan_bssid_wildcard_ssid = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001573 if (params.bssid)
1574 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001576
1577 wpa_s->ml_probe_mld_id = -1;
1578 wpa_s->ml_probe_links = 0;
1579 os_memset(wpa_s->ml_probe_bssid, 0, sizeof(wpa_s->ml_probe_bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580}
1581
1582
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001583void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1584{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001585 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001586 int cancelled;
1587
1588 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1589 &remaining);
1590
1591 new_int.sec = sec;
1592 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001593 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001594 new_int.sec = remaining.sec;
1595 new_int.usec = remaining.usec;
1596 }
1597
Dmitry Shmidt051af732013-10-22 13:52:46 -07001598 if (cancelled) {
1599 eloop_register_timeout(new_int.sec, new_int.usec,
1600 wpa_supplicant_scan, wpa_s, NULL);
1601 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001602 wpa_s->scan_interval = sec;
1603}
1604
1605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606/**
1607 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1608 * @wpa_s: Pointer to wpa_supplicant data
1609 * @sec: Number of seconds after which to scan
1610 * @usec: Number of microseconds after which to scan
1611 *
1612 * This function is used to schedule a scan for neighboring access points after
1613 * the specified time.
1614 */
1615void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1616{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001617 int res;
1618
1619 if (wpa_s->p2p_mgmt) {
1620 wpa_dbg(wpa_s, MSG_DEBUG,
1621 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1622 sec, usec);
1623 return;
1624 }
1625
1626 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1627 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001628 if (res == 1) {
1629 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001630 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001631 } else if (res == 0) {
1632 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1633 sec, usec);
1634 } else {
1635 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1636 sec, usec);
1637 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639}
1640
1641
1642/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001643 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1644 * @wpa_s: Pointer to wpa_supplicant data
1645 * @sec: Number of seconds after which to scan
1646 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001647 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001648 *
1649 * This function is used to schedule periodic scans for neighboring
1650 * access points after the specified time.
1651 */
1652int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1653 int sec, int usec)
1654{
1655 if (!wpa_s->sched_scan_supported)
1656 return -1;
1657
1658 eloop_register_timeout(sec, usec,
1659 wpa_supplicant_delayed_sched_scan_timeout,
1660 wpa_s, NULL);
1661
1662 return 0;
1663}
1664
1665
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001666static void
1667wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1668 struct wpa_driver_scan_params *params)
1669{
1670 if (wpa_s->wpa_state != WPA_COMPLETED ||
1671 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1672 wpa_s->srp.relative_rssi_set == 0)
1673 return;
1674
1675 params->relative_rssi_set = 1;
1676 params->relative_rssi = wpa_s->srp.relative_rssi;
1677
1678 if (wpa_s->srp.relative_adjust_rssi == 0)
1679 return;
1680
1681 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1682 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1683}
1684
1685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001686/**
1687 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1688 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001689 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001690 *
1691 * This function is used to schedule periodic scans for neighboring
1692 * access points repeating the scan continuously.
1693 */
1694int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1695{
1696 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001697 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001698 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001699 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001700 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001701 int ret;
1702 unsigned int max_sched_scan_ssids;
1703 int wildcard = 0;
1704 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001705 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001706
1707 if (!wpa_s->sched_scan_supported)
1708 return -1;
1709
1710 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1711 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1712 else
1713 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001714 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715 return -1;
1716
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001717 wpa_s->sched_scan_stop_req = 0;
1718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001719 if (wpa_s->sched_scanning) {
1720 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1721 return 0;
1722 }
1723
1724 need_ssids = 0;
1725 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001726 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001727 /* Use wildcard SSID to find this network */
1728 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001729 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1730 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001731 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001732
1733#ifdef CONFIG_WPS
1734 if (!wpas_network_disabled(wpa_s, ssid) &&
1735 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1736 /*
1737 * Normal scan is more reliable and faster for WPS
1738 * operations and since these are for short periods of
1739 * time, the benefit of trying to use sched_scan would
1740 * be limited.
1741 */
1742 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1743 "sched_scan for WPS");
1744 return -1;
1745 }
1746#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001747 }
1748 if (wildcard)
1749 need_ssids++;
1750
1751 if (wpa_s->normal_scans < 3 &&
1752 (need_ssids <= wpa_s->max_scan_ssids ||
1753 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1754 /*
1755 * When normal scan can speed up operations, use that for the
1756 * first operations before starting the sched_scan to allow
1757 * user space sleep more. We do this only if the normal scan
1758 * has functionality that is suitable for this or if the
1759 * sched_scan does not have better support for multiple SSIDs.
1760 */
1761 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1762 "sched_scan for initial scans (normal_scans=%d)",
1763 wpa_s->normal_scans);
1764 return -1;
1765 }
1766
1767 os_memset(&params, 0, sizeof(params));
1768
1769 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001770 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001771 sizeof(struct wpa_driver_scan_filter));
1772
1773 prev_state = wpa_s->wpa_state;
1774 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1775 wpa_s->wpa_state == WPA_INACTIVE)
1776 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1777
Dmitry Shmidt04949592012-07-19 12:16:46 -07001778 if (wpa_s->autoscan_params != NULL) {
1779 scan_params = wpa_s->autoscan_params;
1780 goto scan;
1781 }
1782
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001783 /* Find the starting point from which to continue scanning */
1784 ssid = wpa_s->conf->ssid;
1785 if (wpa_s->prev_sched_ssid) {
1786 while (ssid) {
1787 if (ssid == wpa_s->prev_sched_ssid) {
1788 ssid = ssid->next;
1789 break;
1790 }
1791 ssid = ssid->next;
1792 }
1793 }
1794
1795 if (!ssid || !wpa_s->prev_sched_ssid) {
1796 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001797 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1798 wpa_s->first_sched_scan = 1;
1799 ssid = wpa_s->conf->ssid;
1800 wpa_s->prev_sched_ssid = ssid;
1801 }
1802
1803 if (wildcard) {
1804 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1805 params.num_ssids++;
1806 }
1807
1808 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001809 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 goto next;
1811
1812 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1813 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1814 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1815 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1816 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1817 ssid->ssid, ssid->ssid_len);
1818 params.filter_ssids[params.num_filter_ssids].ssid_len =
1819 ssid->ssid_len;
1820 params.num_filter_ssids++;
1821 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1822 {
1823 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1824 "filter for sched_scan - drop filter");
1825 os_free(params.filter_ssids);
1826 params.filter_ssids = NULL;
1827 params.num_filter_ssids = 0;
1828 }
1829
1830 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1831 if (params.num_ssids == max_sched_scan_ssids)
1832 break; /* only room for broadcast SSID */
1833 wpa_dbg(wpa_s, MSG_DEBUG,
1834 "add to active scan ssid: %s",
1835 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1836 params.ssids[params.num_ssids].ssid =
1837 ssid->ssid;
1838 params.ssids[params.num_ssids].ssid_len =
1839 ssid->ssid_len;
1840 params.num_ssids++;
1841 if (params.num_ssids >= max_sched_scan_ssids) {
1842 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001843 do {
1844 ssid = ssid->next;
1845 } while (ssid &&
1846 (wpas_network_disabled(wpa_s, ssid) ||
1847 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001848 break;
1849 }
1850 }
1851
1852 next:
1853 wpa_s->prev_sched_ssid = ssid;
1854 ssid = ssid->next;
1855 }
1856
1857 if (params.num_filter_ssids == 0) {
1858 os_free(params.filter_ssids);
1859 params.filter_ssids = NULL;
1860 }
1861
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001862 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1863 if (extra_ie) {
1864 params.extra_ies = wpabuf_head(extra_ie);
1865 params.extra_ies_len = wpabuf_len(extra_ie);
1866 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001867
Dmitry Shmidt18463232014-01-24 12:29:41 -08001868 if (wpa_s->conf->filter_rssi)
1869 params.filter_rssi = wpa_s->conf->filter_rssi;
1870
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001871 /* See if user specified frequencies. If so, scan only those. */
1872 if (wpa_s->conf->freq_list && !params.freqs) {
1873 wpa_dbg(wpa_s, MSG_DEBUG,
1874 "Optimize scan based on conf->freq_list");
1875 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1876 }
1877
Hai Shalomce48b4a2018-09-05 11:41:35 -07001878#ifdef CONFIG_MBO
1879 if (wpa_s->enable_oce & OCE_STA)
1880 params.oce_scan = 1;
1881#endif /* CONFIG_MBO */
1882
Dmitry Shmidt04949592012-07-19 12:16:46 -07001883 scan_params = &params;
1884
1885scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001886 wpa_s->sched_scan_timed_out = 0;
1887
1888 /*
1889 * We cannot support multiple scan plans if the scan request includes
1890 * too many SSID's, so in this case use only the last scan plan and make
1891 * it run infinitely. It will be stopped by the timeout.
1892 */
1893 if (wpa_s->sched_scan_plans_num == 1 ||
1894 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1895 params.sched_scan_plans = wpa_s->sched_scan_plans;
1896 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1897 } else if (wpa_s->sched_scan_plans_num > 1) {
1898 wpa_dbg(wpa_s, MSG_DEBUG,
1899 "Too many SSIDs. Default to using single scheduled_scan plan");
1900 params.sched_scan_plans =
1901 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1902 1];
1903 params.sched_scan_plans_num = 1;
1904 } else {
1905 if (wpa_s->conf->sched_scan_interval)
1906 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1907 else
1908 scan_plan.interval = 10;
1909
1910 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1911 wpa_printf(MSG_WARNING,
1912 "Scan interval too long(%u), use the maximum allowed(%u)",
1913 scan_plan.interval,
1914 wpa_s->max_sched_scan_plan_interval);
1915 scan_plan.interval =
1916 wpa_s->max_sched_scan_plan_interval;
1917 }
1918
1919 scan_plan.iterations = 0;
1920 params.sched_scan_plans = &scan_plan;
1921 params.sched_scan_plans_num = 1;
1922 }
1923
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001924 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1925
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001926 if (ssid || !wpa_s->first_sched_scan) {
1927 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001928 "Starting sched scan after %u seconds: interval %u timeout %d",
1929 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001930 params.sched_scan_plans[0].interval,
1931 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001932 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001933 wpa_dbg(wpa_s, MSG_DEBUG,
1934 "Starting sched scan after %u seconds (no timeout)",
1935 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001936 }
1937
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001938 wpa_setband_scan_freqs(wpa_s, scan_params);
1939
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001940 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001941 wpa_s->wpa_state <= WPA_SCANNING)
1942 wpa_setup_mac_addr_rand_params(&params,
1943 wpa_s->mac_addr_sched_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001944
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001945 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1946
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001947 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001948 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001949 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001950 os_free(params.mac_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001951 if (ret) {
1952 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1953 if (prev_state != wpa_s->wpa_state)
1954 wpa_supplicant_set_state(wpa_s, prev_state);
1955 return ret;
1956 }
1957
1958 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1959 if (ssid || !wpa_s->first_sched_scan) {
1960 wpa_s->sched_scan_timed_out = 0;
1961 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1962 wpa_supplicant_sched_scan_timeout,
1963 wpa_s, NULL);
1964 wpa_s->first_sched_scan = 0;
1965 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001966 params.sched_scan_plans[0].interval *= 2;
1967 if ((unsigned int) wpa_s->sched_scan_timeout <
1968 params.sched_scan_plans[0].interval ||
1969 params.sched_scan_plans[0].interval >
1970 wpa_s->max_sched_scan_plan_interval) {
1971 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001972 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1973 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001974 }
1975
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001976 /* If there is no more ssids, start next time from the beginning */
1977 if (!ssid)
1978 wpa_s->prev_sched_ssid = NULL;
1979
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001980 return 0;
1981}
1982
1983
1984/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1986 * @wpa_s: Pointer to wpa_supplicant data
1987 *
1988 * This function is used to cancel a scan request scheduled with
1989 * wpa_supplicant_req_scan().
1990 */
1991void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1992{
1993 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1994 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995}
1996
1997
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001999 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
2000 * @wpa_s: Pointer to wpa_supplicant data
2001 *
2002 * This function is used to stop a delayed scheduled scan.
2003 */
2004void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
2005{
2006 if (!wpa_s->sched_scan_supported)
2007 return;
2008
2009 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
2010 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
2011 wpa_s, NULL);
2012}
2013
2014
2015/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002016 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
2017 * @wpa_s: Pointer to wpa_supplicant data
2018 *
2019 * This function is used to stop a periodic scheduled scan.
2020 */
2021void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
2022{
2023 if (!wpa_s->sched_scanning)
2024 return;
2025
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002026 if (wpa_s->sched_scanning)
2027 wpa_s->sched_scan_stop_req = 1;
2028
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002029 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
2030 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
2031 wpa_supplicant_stop_sched_scan(wpa_s);
2032}
2033
2034
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002035/**
2036 * wpa_supplicant_notify_scanning - Indicate possible scan state change
2037 * @wpa_s: Pointer to wpa_supplicant data
2038 * @scanning: Whether scanning is currently in progress
2039 *
2040 * This function is to generate scanning notifycations. It is called whenever
2041 * there may have been a change in scanning (scan started, completed, stopped).
2042 * wpas_notify_scanning() is called whenever the scanning state changed from the
2043 * previously notified state.
2044 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
2046 int scanning)
2047{
2048 if (wpa_s->scanning != scanning) {
2049 wpa_s->scanning = scanning;
2050 wpas_notify_scanning(wpa_s);
2051 }
2052}
2053
2054
2055static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
2056{
2057 int rate = 0;
2058 const u8 *ie;
2059 int i;
2060
2061 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
2062 for (i = 0; ie && i < ie[1]; i++) {
2063 if ((ie[i + 2] & 0x7f) > rate)
2064 rate = ie[i + 2] & 0x7f;
2065 }
2066
2067 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
2068 for (i = 0; ie && i < ie[1]; i++) {
2069 if ((ie[i + 2] & 0x7f) > rate)
2070 rate = ie[i + 2] & 0x7f;
2071 }
2072
2073 return rate;
2074}
2075
2076
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002077/**
2078 * wpa_scan_get_ie - Fetch a specified information element from a scan result
2079 * @res: Scan result entry
2080 * @ie: Information element identitifier (WLAN_EID_*)
2081 * Returns: Pointer to the information element (id field) or %NULL if not found
2082 *
2083 * This function returns the first matching information element in the scan
2084 * result.
2085 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
2087{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002088 size_t ie_len = res->ie_len;
2089
2090 /* Use the Beacon frame IEs if res->ie_len is not available */
2091 if (!ie_len)
2092 ie_len = res->beacon_ie_len;
2093
2094 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095}
2096
2097
Sunil Ravi89eba102022-09-13 21:04:37 -07002098const u8 * wpa_scan_get_ml_ie(const struct wpa_scan_res *res, u8 type)
2099{
2100 size_t ie_len = res->ie_len;
2101
2102 /* Use the Beacon frame IEs if res->ie_len is not available */
2103 if (!ie_len)
2104 ie_len = res->beacon_ie_len;
2105
2106 return get_ml_ie((const u8 *) (res + 1), ie_len, type);
2107}
2108
2109
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002110/**
2111 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
2112 * @res: Scan result entry
2113 * @vendor_type: Vendor type (four octets starting the IE payload)
2114 * Returns: Pointer to the information element (id field) or %NULL if not found
2115 *
2116 * This function returns the first matching information element in the scan
2117 * result.
2118 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
2120 u32 vendor_type)
2121{
Hai Shalom60840252021-02-19 19:02:11 -08002122 const u8 *ies;
2123 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124
Hai Shalom60840252021-02-19 19:02:11 -08002125 ies = (const u8 *) (res + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126
Hai Shalom60840252021-02-19 19:02:11 -08002127 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
2128 if (elem->datalen >= 4 &&
2129 vendor_type == WPA_GET_BE32(elem->data))
2130 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002131 }
2132
2133 return NULL;
2134}
2135
2136
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002137/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07002138 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
2139 * @res: Scan result entry
2140 * @vendor_type: Vendor type (four octets starting the IE payload)
2141 * Returns: Pointer to the information element (id field) or %NULL if not found
2142 *
2143 * This function returns the first matching information element in the scan
2144 * result.
2145 *
2146 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
2147 * from Beacon frames instead of either Beacon or Probe Response frames.
2148 */
2149const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
2150 u32 vendor_type)
2151{
Hai Shalom60840252021-02-19 19:02:11 -08002152 const u8 *ies;
2153 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002154
2155 if (res->beacon_ie_len == 0)
2156 return NULL;
2157
Hai Shalom60840252021-02-19 19:02:11 -08002158 ies = (const u8 *) (res + 1);
2159 ies += res->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002160
Hai Shalom60840252021-02-19 19:02:11 -08002161 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
2162 res->beacon_ie_len) {
2163 if (elem->datalen >= 4 &&
2164 vendor_type == WPA_GET_BE32(elem->data))
2165 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002166 }
2167
2168 return NULL;
2169}
2170
2171
2172/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002173 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2174 * @res: Scan result entry
2175 * @vendor_type: Vendor type (four octets starting the IE payload)
2176 * Returns: Pointer to the information element payload or %NULL if not found
2177 *
2178 * This function returns concatenated payload of possibly fragmented vendor
2179 * specific information elements in the scan result. The caller is responsible
2180 * for freeing the returned buffer.
2181 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2183 u32 vendor_type)
2184{
2185 struct wpabuf *buf;
2186 const u8 *end, *pos;
2187
2188 buf = wpabuf_alloc(res->ie_len);
2189 if (buf == NULL)
2190 return NULL;
2191
2192 pos = (const u8 *) (res + 1);
2193 end = pos + res->ie_len;
2194
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002195 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08002196 u8 ie, len;
2197
2198 ie = pos[0];
2199 len = pos[1];
2200 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 break;
Hai Shalom60840252021-02-19 19:02:11 -08002202 pos += 2;
2203 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2204 vendor_type == WPA_GET_BE32(pos))
2205 wpabuf_put_data(buf, pos + 4, len - 4);
2206 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 }
2208
2209 if (wpabuf_len(buf) == 0) {
2210 wpabuf_free(buf);
2211 buf = NULL;
2212 }
2213
2214 return buf;
2215}
2216
2217
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002218static int wpas_channel_width_offset(enum chan_width cw)
2219{
2220 switch (cw) {
2221 case CHAN_WIDTH_40:
2222 return 1;
2223 case CHAN_WIDTH_80:
2224 return 2;
2225 case CHAN_WIDTH_80P80:
2226 case CHAN_WIDTH_160:
2227 return 3;
2228 case CHAN_WIDTH_320:
2229 return 4;
2230 default:
2231 return 0;
2232 }
2233}
2234
2235
2236/**
2237 * wpas_channel_width_tx_pwr - Calculate the max transmit power at the channel
2238 * width
2239 * @ies: Information elements
2240 * @ies_len: Length of elements
2241 * @cw: The channel width
2242 * Returns: The max transmit power at the channel width, TX_POWER_NO_CONSTRAINT
2243 * if it is not constrained.
2244 *
2245 * This function is only used to estimate the actual signal RSSI when associated
2246 * based on the beacon RSSI at the STA. Beacon frames are transmitted on 20 MHz
2247 * channels, while the Data frames usually use higher channel width. Therefore
2248 * their RSSIs may be different. Assuming there is a fixed gap between the TX
2249 * power limit of the STA defined by the Transmit Power Envelope element and the
2250 * TX power of the AP, the difference in the TX power of X MHz and Y MHz at the
2251 * STA equals to the difference at the AP, and the difference in the signal RSSI
2252 * at the STA. tx_pwr is a floating point number in the standard, but the error
2253 * of casting to int is trivial in comparing two BSSes.
2254 */
2255static int wpas_channel_width_tx_pwr(const u8 *ies, size_t ies_len,
2256 enum chan_width cw)
2257{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002258 int offset = wpas_channel_width_offset(cw);
2259 const struct element *elem;
2260 int max_tx_power = TX_POWER_NO_CONSTRAINT, tx_pwr = 0;
2261
2262 for_each_element_id(elem, WLAN_EID_TRANSMIT_POWER_ENVELOPE, ies,
2263 ies_len) {
2264 int max_tx_pwr_count;
2265 enum max_tx_pwr_interpretation tx_pwr_intrpn;
2266 enum reg_6g_client_type client_type;
2267
2268 if (elem->datalen < 1)
2269 continue;
2270
2271 /*
2272 * IEEE Std 802.11ax-2021, 9.4.2.161 (Transmit Power Envelope
2273 * element) defines Maximum Transmit Power Count (B0-B2),
2274 * Maximum Transmit Power Interpretation (B3-B5), and Maximum
2275 * Transmit Power Category (B6-B7).
2276 */
2277 max_tx_pwr_count = elem->data[0] & 0x07;
2278 tx_pwr_intrpn = (elem->data[0] >> 3) & 0x07;
2279 client_type = (elem->data[0] >> 6) & 0x03;
2280
2281 if (client_type != REG_DEFAULT_CLIENT)
2282 continue;
2283
2284 if (tx_pwr_intrpn == LOCAL_EIRP ||
2285 tx_pwr_intrpn == REGULATORY_CLIENT_EIRP) {
2286 int offs;
2287
2288 max_tx_pwr_count = MIN(max_tx_pwr_count, 3);
2289 offs = MIN(offset, max_tx_pwr_count) + 1;
2290 if (elem->datalen <= offs)
2291 continue;
2292 tx_pwr = (signed char) elem->data[offs];
2293 /*
2294 * Maximum Transmit Power subfield is encoded as an
2295 * 8-bit 2s complement signed integer in the range -64
2296 * dBm to 63 dBm with a 0.5 dB step. 63.5 dBm means no
2297 * local maximum transmit power constraint.
2298 */
2299 if (tx_pwr == 127)
2300 continue;
2301 tx_pwr /= 2;
2302 max_tx_power = MIN(max_tx_power, tx_pwr);
2303 } else if (tx_pwr_intrpn == LOCAL_EIRP_PSD ||
2304 tx_pwr_intrpn == REGULATORY_CLIENT_EIRP_PSD) {
2305 if (elem->datalen < 2)
2306 continue;
2307
2308 tx_pwr = (signed char) elem->data[1];
2309 /*
2310 * Maximum Transmit PSD subfield is encoded as an 8-bit
2311 * 2s complement signed integer. -128 indicates that the
2312 * corresponding 20 MHz channel cannot be used for
2313 * transmission. +127 indicates that no maximum PSD
2314 * limit is specified for the corresponding 20 MHz
2315 * channel.
2316 */
2317 if (tx_pwr == 127 || tx_pwr == -128)
2318 continue;
2319
2320 /*
2321 * The Maximum Transmit PSD subfield indicates the
2322 * maximum transmit PSD for the 20 MHz channel. Suppose
2323 * the PSD value is X dBm/MHz, the TX power of N MHz is
2324 * X + 10*log10(N) = X + 10*log10(20) + 10*log10(N/20) =
2325 * X + 13 + 3*log2(N/20)
2326 */
2327 tx_pwr = tx_pwr / 2 + 13 + offset * 3;
2328 max_tx_power = MIN(max_tx_power, tx_pwr);
2329 }
2330 }
2331
2332 return max_tx_power;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002333}
2334
2335
2336/**
2337 * Estimate the RSSI bump of channel width |cw| with respect to 20 MHz channel.
2338 * If the TX power has no constraint, it is unable to estimate the RSSI bump.
2339 */
2340int wpas_channel_width_rssi_bump(const u8 *ies, size_t ies_len,
2341 enum chan_width cw)
2342{
2343 int max_20mhz_tx_pwr = wpas_channel_width_tx_pwr(ies, ies_len,
2344 CHAN_WIDTH_20);
2345 int max_cw_tx_pwr = wpas_channel_width_tx_pwr(ies, ies_len, cw);
2346
2347 return (max_20mhz_tx_pwr == TX_POWER_NO_CONSTRAINT ||
2348 max_cw_tx_pwr == TX_POWER_NO_CONSTRAINT) ?
2349 0 : (max_cw_tx_pwr - max_20mhz_tx_pwr);
2350}
2351
2352
2353int wpas_adjust_snr_by_chanwidth(const u8 *ies, size_t ies_len,
2354 enum chan_width max_cw, int snr)
2355{
2356 int rssi_bump = wpas_channel_width_rssi_bump(ies, ies_len, max_cw);
2357 /*
2358 * The noise has uniform power spectral density (PSD) across the
2359 * frequency band, its power is proportional to the channel width.
2360 * Suppose the PSD of noise is X dBm/MHz, the noise power of N MHz is
2361 * X + 10*log10(N), and the noise power bump with respect to 20 MHz is
2362 * 10*log10(N) - 10*log10(20) = 10*log10(N/20) = 3*log2(N/20)
2363 */
2364 int noise_bump = 3 * wpas_channel_width_offset(max_cw);
2365
2366 return snr + rssi_bump - noise_bump;
2367}
2368
2369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370/* Compare function for sorting scan results. Return >0 if @b is considered
2371 * better. */
2372static int wpa_scan_result_compar(const void *a, const void *b)
2373{
2374 struct wpa_scan_res **_wa = (void *) a;
2375 struct wpa_scan_res **_wb = (void *) b;
2376 struct wpa_scan_res *wa = *_wa;
2377 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002378 int wpa_a, wpa_b;
2379 int snr_a, snr_b, snr_a_full, snr_b_full;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002380 size_t ies_len;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002381 const u8 *rsne_a, *rsne_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382
2383 /* WPA/WPA2 support preferred */
2384 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2385 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2386 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2387 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2388
2389 if (wpa_b && !wpa_a)
2390 return 1;
2391 if (!wpa_b && wpa_a)
2392 return -1;
2393
2394 /* privacy support preferred */
2395 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2396 (wb->caps & IEEE80211_CAP_PRIVACY))
2397 return 1;
2398 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2399 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2400 return -1;
2401
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002402 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002403 /*
2404 * The scan result estimates SNR over 20 MHz, while Data frames
2405 * usually use wider channel width. The TX power and noise power
2406 * are both affected by the channel width.
2407 */
2408 ies_len = wa->ie_len ? wa->ie_len : wa->beacon_ie_len;
2409 snr_a_full = wpas_adjust_snr_by_chanwidth((const u8 *) (wa + 1),
2410 ies_len, wa->max_cw,
2411 wa->snr);
2412 snr_a = MIN(snr_a_full, GREAT_SNR);
2413 ies_len = wb->ie_len ? wb->ie_len : wb->beacon_ie_len;
2414 snr_b_full = wpas_adjust_snr_by_chanwidth((const u8 *) (wb + 1),
2415 ies_len, wb->max_cw,
2416 wb->snr);
2417 snr_b = MIN(snr_b_full, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002418 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002419 /* Level is not in dBm, so we can't calculate
2420 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002421 snr_a = snr_a_full = wa->level;
2422 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002423 }
2424
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002425 /* If SNR of a SAE BSS is good or at least as high as the PSK BSS,
2426 * prefer SAE over PSK for mixed WPA3-Personal transition mode and
2427 * WPA2-Personal deployments */
2428 rsne_a = wpa_scan_get_ie(wa, WLAN_EID_RSN);
2429 rsne_b = wpa_scan_get_ie(wb, WLAN_EID_RSN);
2430 if (rsne_a && rsne_b) {
2431 struct wpa_ie_data data;
2432 bool psk_a = false, psk_b = false, sae_a = false, sae_b = false;
2433
2434 if (wpa_parse_wpa_ie_rsn(rsne_a, 2 + rsne_a[1], &data) == 0) {
2435 psk_a = wpa_key_mgmt_wpa_psk_no_sae(data.key_mgmt);
2436 sae_a = wpa_key_mgmt_sae(data.key_mgmt);
2437 }
2438 if (wpa_parse_wpa_ie_rsn(rsne_b, 2 + rsne_b[1], &data) == 0) {
2439 psk_b = wpa_key_mgmt_wpa_psk_no_sae(data.key_mgmt);
2440 sae_b = wpa_key_mgmt_sae(data.key_mgmt);
2441 }
2442
2443 if (sae_a && !sae_b && psk_b &&
2444 (snr_a >= GREAT_SNR || snr_a >= snr_b))
2445 return -1;
2446 if (sae_b && !sae_a && psk_a &&
2447 (snr_b >= GREAT_SNR || snr_b >= snr_a))
2448 return 1;
2449 }
2450
Hai Shaloma20dcd72022-02-04 13:43:00 -08002451 /* If SNR is close, decide by max rate or frequency band. For cases
2452 * involving the 6 GHz band, use the throughput estimate irrespective
2453 * of the SNR difference since the LPI/VLP rules may result in
2454 * significant differences in SNR for cases where the estimated
2455 * throughput can be considerably higher with the lower SNR. */
2456 if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2457 is_6ghz_freq(wa->freq) ||
2458 is_6ghz_freq(wb->freq))) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002459 if (wa->est_throughput != wb->est_throughput)
Hai Shalom021b0b52019-04-10 11:17:58 -07002460 return (int) wb->est_throughput -
2461 (int) wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002462 }
2463 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2464 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002465 if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2466 return is_6ghz_freq(wa->freq) ? -1 : 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002467 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2468 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 }
2470
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002471 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 * identical, use quality values since some drivers may only report
2473 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002474 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002476 return snr_b_full - snr_a_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477}
2478
2479
2480#ifdef CONFIG_WPS
2481/* Compare function for sorting scan results when searching a WPS AP for
2482 * provisioning. Return >0 if @b is considered better. */
2483static int wpa_scan_result_wps_compar(const void *a, const void *b)
2484{
2485 struct wpa_scan_res **_wa = (void *) a;
2486 struct wpa_scan_res **_wb = (void *) b;
2487 struct wpa_scan_res *wa = *_wa;
2488 struct wpa_scan_res *wb = *_wb;
2489 int uses_wps_a, uses_wps_b;
2490 struct wpabuf *wps_a, *wps_b;
2491 int res;
2492
2493 /* Optimization - check WPS IE existence before allocated memory and
2494 * doing full reassembly. */
2495 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2496 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2497 if (uses_wps_a && !uses_wps_b)
2498 return -1;
2499 if (!uses_wps_a && uses_wps_b)
2500 return 1;
2501
2502 if (uses_wps_a && uses_wps_b) {
2503 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2504 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2505 res = wps_ap_priority_compar(wps_a, wps_b);
2506 wpabuf_free(wps_a);
2507 wpabuf_free(wps_b);
2508 if (res)
2509 return res;
2510 }
2511
2512 /*
2513 * Do not use current AP security policy as a sorting criteria during
2514 * WPS provisioning step since the AP may get reconfigured at the
2515 * completion of provisioning.
2516 */
2517
2518 /* all things being equal, use signal level; if signal levels are
2519 * identical, use quality values since some drivers may only report
2520 * that value and leave the signal level zero */
2521 if (wb->level == wa->level)
2522 return wb->qual - wa->qual;
2523 return wb->level - wa->level;
2524}
2525#endif /* CONFIG_WPS */
2526
2527
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002528static void dump_scan_res(struct wpa_scan_results *scan_res)
2529{
2530#ifndef CONFIG_NO_STDOUT_DEBUG
2531 size_t i;
2532
2533 if (scan_res->res == NULL || scan_res->num == 0)
2534 return;
2535
2536 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2537
2538 for (i = 0; i < scan_res->num; i++) {
2539 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002540 u8 *pos;
Sunil Ravia04bd252022-05-02 22:54:18 -07002541 const u8 *ssid_ie, *ssid = NULL;
2542 size_t ssid_len = 0;
2543
2544 ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2545 if (ssid_ie) {
2546 ssid = ssid_ie + 2;
2547 ssid_len = ssid_ie[1];
2548 }
2549
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002550 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002551 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2552
Sunil Ravia04bd252022-05-02 22:54:18 -07002553 wpa_printf(MSG_EXCESSIVE, MACSTR
2554 " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2555 MAC2STR(r->bssid),
2556 wpa_ssid_txt(ssid, ssid_len),
2557 r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002558 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002559 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2560 r->flags,
2561 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002562 } else {
Sunil Ravia04bd252022-05-02 22:54:18 -07002563 wpa_printf(MSG_EXCESSIVE, MACSTR
2564 " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2565 MAC2STR(r->bssid),
2566 wpa_ssid_txt(ssid, ssid_len),
2567 r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002568 r->noise, r->level, r->flags, r->age,
2569 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002570 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002571 pos = (u8 *) (r + 1);
2572 if (r->ie_len)
2573 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2574 pos += r->ie_len;
2575 if (r->beacon_ie_len)
2576 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2577 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002578 }
2579#endif /* CONFIG_NO_STDOUT_DEBUG */
2580}
2581
2582
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002583/**
2584 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2585 * @wpa_s: Pointer to wpa_supplicant data
2586 * @bssid: BSSID to check
2587 * Returns: 0 if the BSSID is filtered or 1 if not
2588 *
2589 * This function is used to filter out specific BSSIDs from scan reslts mainly
2590 * for testing purposes (SET bssid_filter ctrl_iface command).
2591 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002592int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2593 const u8 *bssid)
2594{
2595 size_t i;
2596
2597 if (wpa_s->bssid_filter == NULL)
2598 return 1;
2599
2600 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002601 if (ether_addr_equal(wpa_s->bssid_filter + i * ETH_ALEN, bssid))
Dmitry Shmidt04949592012-07-19 12:16:46 -07002602 return 1;
2603 }
2604
2605 return 0;
2606}
2607
2608
Sunil Ravi88611412024-06-28 17:34:56 +00002609void filter_scan_res(struct wpa_supplicant *wpa_s,
2610 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002611{
2612 size_t i, j;
2613
2614 if (wpa_s->bssid_filter == NULL)
2615 return;
2616
2617 for (i = 0, j = 0; i < res->num; i++) {
2618 if (wpa_supplicant_filter_bssid_match(wpa_s,
2619 res->res[i]->bssid)) {
2620 res->res[j++] = res->res[i];
2621 } else {
2622 os_free(res->res[i]);
2623 res->res[i] = NULL;
2624 }
2625 }
2626
2627 if (res->num != j) {
2628 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2629 (int) (res->num - j));
2630 res->num = j;
2631 }
2632}
2633
2634
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002635void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002636{
2637 if (res->flags & WPA_SCAN_NOISE_INVALID) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002638 res->noise = is_6ghz_freq(res->freq) ?
2639 DEFAULT_NOISE_FLOOR_6GHZ :
2640 (IS_5GHZ(res->freq) ?
2641 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002642 }
2643
2644 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2645 res->snr = res->level - res->noise;
2646 } else {
2647 /* Level is not in dBm, so we can't calculate
2648 * SNR. Just use raw level (units unknown). */
2649 res->snr = res->level;
2650 }
2651}
2652
2653
Hai Shalom899fcc72020-10-19 14:38:18 -07002654/* Minimum SNR required to achieve a certain bitrate. */
2655struct minsnr_bitrate_entry {
2656 int minsnr;
2657 unsigned int bitrate; /* in Mbps */
2658};
2659
2660/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2661static const int vht_mcs = 8;
2662
2663static const struct minsnr_bitrate_entry vht20_table[] = {
2664 { 0, 0 },
2665 { 2, 6500 }, /* HT20 MCS0 */
2666 { 5, 13000 }, /* HT20 MCS1 */
2667 { 9, 19500 }, /* HT20 MCS2 */
2668 { 11, 26000 }, /* HT20 MCS3 */
2669 { 15, 39000 }, /* HT20 MCS4 */
2670 { 18, 52000 }, /* HT20 MCS5 */
2671 { 20, 58500 }, /* HT20 MCS6 */
2672 { 25, 65000 }, /* HT20 MCS7 */
2673 { 29, 78000 }, /* VHT20 MCS8 */
2674 { -1, 78000 } /* SNR > 29 */
2675};
2676
2677static const struct minsnr_bitrate_entry vht40_table[] = {
2678 { 0, 0 },
2679 { 5, 13500 }, /* HT40 MCS0 */
2680 { 8, 27000 }, /* HT40 MCS1 */
2681 { 12, 40500 }, /* HT40 MCS2 */
2682 { 14, 54000 }, /* HT40 MCS3 */
2683 { 18, 81000 }, /* HT40 MCS4 */
2684 { 21, 108000 }, /* HT40 MCS5 */
2685 { 23, 121500 }, /* HT40 MCS6 */
2686 { 28, 135000 }, /* HT40 MCS7 */
2687 { 32, 162000 }, /* VHT40 MCS8 */
2688 { 34, 180000 }, /* VHT40 MCS9 */
2689 { -1, 180000 } /* SNR > 34 */
2690};
2691
2692static const struct minsnr_bitrate_entry vht80_table[] = {
2693 { 0, 0 },
2694 { 8, 29300 }, /* VHT80 MCS0 */
2695 { 11, 58500 }, /* VHT80 MCS1 */
2696 { 15, 87800 }, /* VHT80 MCS2 */
2697 { 17, 117000 }, /* VHT80 MCS3 */
2698 { 21, 175500 }, /* VHT80 MCS4 */
2699 { 24, 234000 }, /* VHT80 MCS5 */
2700 { 26, 263300 }, /* VHT80 MCS6 */
2701 { 31, 292500 }, /* VHT80 MCS7 */
2702 { 35, 351000 }, /* VHT80 MCS8 */
2703 { 37, 390000 }, /* VHT80 MCS9 */
2704 { -1, 390000 } /* SNR > 37 */
2705};
2706
2707
Hai Shaloma20dcd72022-02-04 13:43:00 -08002708static const struct minsnr_bitrate_entry vht160_table[] = {
2709 { 0, 0 },
2710 { 11, 58500 }, /* VHT160 MCS0 */
2711 { 14, 117000 }, /* VHT160 MCS1 */
2712 { 18, 175500 }, /* VHT160 MCS2 */
2713 { 20, 234000 }, /* VHT160 MCS3 */
2714 { 24, 351000 }, /* VHT160 MCS4 */
2715 { 27, 468000 }, /* VHT160 MCS5 */
2716 { 29, 526500 }, /* VHT160 MCS6 */
2717 { 34, 585000 }, /* VHT160 MCS7 */
2718 { 38, 702000 }, /* VHT160 MCS8 */
2719 { 40, 780000 }, /* VHT160 MCS9 */
2720 { -1, 780000 } /* SNR > 37 */
2721};
2722
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002723/* EHT needs to be enabled in order to achieve MCS12 and MCS13 rates. */
2724#define EHT_MCS 12
Hai Shaloma20dcd72022-02-04 13:43:00 -08002725
2726static const struct minsnr_bitrate_entry he20_table[] = {
2727 { 0, 0 },
2728 { 2, 8600 }, /* HE20 MCS0 */
2729 { 5, 17200 }, /* HE20 MCS1 */
2730 { 9, 25800 }, /* HE20 MCS2 */
2731 { 11, 34400 }, /* HE20 MCS3 */
2732 { 15, 51600 }, /* HE20 MCS4 */
2733 { 18, 68800 }, /* HE20 MCS5 */
2734 { 20, 77400 }, /* HE20 MCS6 */
2735 { 25, 86000 }, /* HE20 MCS7 */
2736 { 29, 103200 }, /* HE20 MCS8 */
2737 { 31, 114700 }, /* HE20 MCS9 */
2738 { 34, 129000 }, /* HE20 MCS10 */
2739 { 36, 143400 }, /* HE20 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002740 { 39, 154900 }, /* EHT20 MCS12 */
2741 { 42, 172100 }, /* EHT20 MCS13 */
2742 { -1, 172100 } /* SNR > 42 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002743};
2744
2745static const struct minsnr_bitrate_entry he40_table[] = {
2746 { 0, 0 },
2747 { 5, 17200 }, /* HE40 MCS0 */
2748 { 8, 34400 }, /* HE40 MCS1 */
2749 { 12, 51600 }, /* HE40 MCS2 */
2750 { 14, 68800 }, /* HE40 MCS3 */
2751 { 18, 103200 }, /* HE40 MCS4 */
2752 { 21, 137600 }, /* HE40 MCS5 */
2753 { 23, 154900 }, /* HE40 MCS6 */
2754 { 28, 172100 }, /* HE40 MCS7 */
2755 { 32, 206500 }, /* HE40 MCS8 */
2756 { 34, 229400 }, /* HE40 MCS9 */
2757 { 37, 258100 }, /* HE40 MCS10 */
2758 { 39, 286800 }, /* HE40 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002759 { 42, 309500 }, /* EHT40 MCS12 */
2760 { 45, 344100 }, /* EHT40 MCS13 */
2761 { -1, 344100 } /* SNR > 45 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002762};
2763
2764static const struct minsnr_bitrate_entry he80_table[] = {
2765 { 0, 0 },
2766 { 8, 36000 }, /* HE80 MCS0 */
2767 { 11, 72100 }, /* HE80 MCS1 */
2768 { 15, 108100 }, /* HE80 MCS2 */
2769 { 17, 144100 }, /* HE80 MCS3 */
2770 { 21, 216200 }, /* HE80 MCS4 */
2771 { 24, 288200 }, /* HE80 MCS5 */
2772 { 26, 324300 }, /* HE80 MCS6 */
2773 { 31, 360300 }, /* HE80 MCS7 */
2774 { 35, 432400 }, /* HE80 MCS8 */
2775 { 37, 480400 }, /* HE80 MCS9 */
2776 { 40, 540400 }, /* HE80 MCS10 */
2777 { 42, 600500 }, /* HE80 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002778 { 45, 648500 }, /* EHT80 MCS12 */
2779 { 48, 720600 }, /* EHT80 MCS13 */
2780 { -1, 720600 } /* SNR > 48 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002781};
2782
2783
2784static const struct minsnr_bitrate_entry he160_table[] = {
2785 { 0, 0 },
2786 { 11, 72100 }, /* HE160 MCS0 */
2787 { 14, 144100 }, /* HE160 MCS1 */
2788 { 18, 216200 }, /* HE160 MCS2 */
2789 { 20, 288200 }, /* HE160 MCS3 */
2790 { 24, 432400 }, /* HE160 MCS4 */
2791 { 27, 576500 }, /* HE160 MCS5 */
2792 { 29, 648500 }, /* HE160 MCS6 */
2793 { 34, 720600 }, /* HE160 MCS7 */
2794 { 38, 864700 }, /* HE160 MCS8 */
2795 { 40, 960800 }, /* HE160 MCS9 */
2796 { 43, 1080900 }, /* HE160 MCS10 */
2797 { 45, 1201000 }, /* HE160 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002798 { 48, 1297100 }, /* EHT160 MCS12 */
2799 { 51, 1441200 }, /* EHT160 MCS13 */
2800 { -1, 1441200 } /* SNR > 51 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002801};
2802
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002803/* See IEEE P802.11be/D2.0, Table 36-86: EHT-MCSs for 4x996-tone RU, NSS,u = 1
2804 */
2805static const struct minsnr_bitrate_entry eht320_table[] = {
2806 { 0, 0 },
2807 { 14, 144100 }, /* EHT320 MCS0 */
2808 { 17, 288200 }, /* EHT320 MCS1 */
2809 { 21, 432400 }, /* EHT320 MCS2 */
2810 { 23, 576500 }, /* EHT320 MCS3 */
2811 { 27, 864700 }, /* EHT320 MCS4 */
2812 { 30, 1152900 }, /* EHT320 MCS5 */
2813 { 32, 1297100 }, /* EHT320 MCS6 */
2814 { 37, 1441200 }, /* EHT320 MCS7 */
2815 { 41, 1729400 }, /* EHT320 MCS8 */
2816 { 43, 1921500 }, /* EHT320 MCS9 */
2817 { 46, 2161800 }, /* EHT320 MCS10 */
2818 { 48, 2401900 }, /* EHT320 MCS11 */
2819 { 51, 2594100 }, /* EHT320 MCS12 */
2820 { 54, 2882400 }, /* EHT320 MCS13 */
2821 { -1, 2882400 } /* SNR > 54 */
2822};
Hai Shaloma20dcd72022-02-04 13:43:00 -08002823
Hai Shalomfdcde762020-04-02 11:19:20 -07002824static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2825 int rate0, int rate1)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002826{
Hai Shalomfdcde762020-04-02 11:19:20 -07002827 return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002828}
2829
2830
Hai Shalom899fcc72020-10-19 14:38:18 -07002831static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2832 int snr, bool vht)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002833{
Hai Shalom899fcc72020-10-19 14:38:18 -07002834 const struct minsnr_bitrate_entry *prev, *entry = table;
2835
2836 while ((entry->minsnr != -1) &&
2837 (snr >= entry->minsnr) &&
2838 (vht || entry - table <= vht_mcs))
2839 entry++;
2840 if (entry == table)
2841 return entry->bitrate;
2842 prev = entry - 1;
2843 if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2844 return prev->bitrate;
2845 return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2846 entry->bitrate);
Hai Shalomfdcde762020-04-02 11:19:20 -07002847}
2848
2849
Hai Shalom899fcc72020-10-19 14:38:18 -07002850static unsigned int max_ht20_rate(int snr, bool vht)
Hai Shalomfdcde762020-04-02 11:19:20 -07002851{
Hai Shalom899fcc72020-10-19 14:38:18 -07002852 return max_rate(vht20_table, snr, vht);
2853}
2854
2855
2856static unsigned int max_ht40_rate(int snr, bool vht)
2857{
2858 return max_rate(vht40_table, snr, vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002859}
2860
2861
2862static unsigned int max_vht80_rate(int snr)
2863{
Hai Shalom899fcc72020-10-19 14:38:18 -07002864 return max_rate(vht80_table, snr, 1);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002865}
2866
Hai Shalomfdcde762020-04-02 11:19:20 -07002867
Hai Shaloma20dcd72022-02-04 13:43:00 -08002868static unsigned int max_vht160_rate(int snr)
2869{
2870 return max_rate(vht160_table, snr, 1);
2871}
2872
2873
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002874static unsigned int max_he_eht_rate(const struct minsnr_bitrate_entry table[],
2875 int snr, bool eht)
Hai Shaloma20dcd72022-02-04 13:43:00 -08002876{
2877 const struct minsnr_bitrate_entry *prev, *entry = table;
2878
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002879 while (entry->minsnr != -1 && snr >= entry->minsnr &&
2880 (eht || entry - table <= EHT_MCS))
Hai Shaloma20dcd72022-02-04 13:43:00 -08002881 entry++;
2882 if (entry == table)
2883 return 0;
2884 prev = entry - 1;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002885 if (entry->minsnr == -1 || (!eht && entry - table > EHT_MCS))
Hai Shaloma20dcd72022-02-04 13:43:00 -08002886 return prev->bitrate;
2887 return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2888 prev->bitrate, entry->bitrate);
2889}
2890
2891
Hai Shalomfdcde762020-04-02 11:19:20 -07002892unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2893 const u8 *ies, size_t ies_len, int rate,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002894 int snr, int freq, enum chan_width *max_cw)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002895{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002896 struct hostapd_hw_modes *hw_mode;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002897 unsigned int est, tmp;
Hai Shalomfdcde762020-04-02 11:19:20 -07002898 const u8 *ie;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002899 /*
2900 * No need to apply a bump to the noise here because the
2901 * minsnr_bitrate_entry tables are based on MCS tables where this has
2902 * been taken into account.
2903 */
2904 int adjusted_snr;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002905 bool ht40 = false, vht80 = false, vht160 = false;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002906
2907 /* Limit based on estimated SNR */
2908 if (rate > 1 * 2 && snr < 1)
2909 rate = 1 * 2;
2910 else if (rate > 2 * 2 && snr < 4)
2911 rate = 2 * 2;
2912 else if (rate > 6 * 2 && snr < 5)
2913 rate = 6 * 2;
2914 else if (rate > 9 * 2 && snr < 6)
2915 rate = 9 * 2;
2916 else if (rate > 12 * 2 && snr < 7)
2917 rate = 12 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002918 else if (rate > 12 * 2 && snr < 8)
2919 rate = 14 * 2;
2920 else if (rate > 12 * 2 && snr < 9)
2921 rate = 16 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002922 else if (rate > 18 * 2 && snr < 10)
2923 rate = 18 * 2;
2924 else if (rate > 24 * 2 && snr < 11)
2925 rate = 24 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002926 else if (rate > 24 * 2 && snr < 12)
2927 rate = 27 * 2;
2928 else if (rate > 24 * 2 && snr < 13)
2929 rate = 30 * 2;
2930 else if (rate > 24 * 2 && snr < 14)
2931 rate = 33 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002932 else if (rate > 36 * 2 && snr < 15)
2933 rate = 36 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002934 else if (rate > 36 * 2 && snr < 16)
2935 rate = 39 * 2;
2936 else if (rate > 36 * 2 && snr < 17)
2937 rate = 42 * 2;
2938 else if (rate > 36 * 2 && snr < 18)
2939 rate = 45 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002940 else if (rate > 48 * 2 && snr < 19)
2941 rate = 48 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002942 else if (rate > 48 * 2 && snr < 20)
2943 rate = 51 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002944 else if (rate > 54 * 2 && snr < 21)
2945 rate = 54 * 2;
2946 est = rate * 500;
2947
Hai Shaloma20dcd72022-02-04 13:43:00 -08002948 hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2949 freq);
2950
2951 if (hw_mode && hw_mode->ht_capab) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002952 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002953 if (ie) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002954 *max_cw = CHAN_WIDTH_20;
Hai Shalom899fcc72020-10-19 14:38:18 -07002955 tmp = max_ht20_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002956 if (tmp > est)
2957 est = tmp;
2958 }
2959 }
2960
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002961 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
2962 if (ie && ie[1] >= 2 &&
2963 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK))
2964 ht40 = true;
2965
Hai Shaloma20dcd72022-02-04 13:43:00 -08002966 if (hw_mode &&
2967 (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002968 if (ht40) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002969 *max_cw = CHAN_WIDTH_40;
2970 adjusted_snr = snr +
2971 wpas_channel_width_rssi_bump(ies, ies_len,
2972 CHAN_WIDTH_40);
2973 tmp = max_ht40_rate(adjusted_snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002974 if (tmp > est)
2975 est = tmp;
2976 }
2977 }
2978
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002979 /* Determine VHT BSS bandwidth based on IEEE Std 802.11-2020,
2980 * Table 11-23 (VHT BSS bandwidth) */
2981 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
2982 if (ie && ie[1] >= 3) {
2983 u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2984 u8 seg0 = ie[3];
2985 u8 seg1 = ie[4];
2986
2987 if (cw)
2988 vht80 = true;
2989 if (cw == 2 ||
2990 (cw == 3 && (seg1 > 0 && abs(seg1 - seg0) == 16)))
2991 vht160 = true;
2992 if (cw == 1 &&
2993 ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2994 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2995 vht160 = true;
2996 }
2997
Hai Shaloma20dcd72022-02-04 13:43:00 -08002998 if (hw_mode && hw_mode->vht_capab) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002999 /* Use +1 to assume VHT is always faster than HT */
Hai Shalomfdcde762020-04-02 11:19:20 -07003000 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003001 if (ie) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003002 if (*max_cw == CHAN_WIDTH_UNKNOWN)
3003 *max_cw = CHAN_WIDTH_20;
Hai Shalom899fcc72020-10-19 14:38:18 -07003004 tmp = max_ht20_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003005 if (tmp > est)
3006 est = tmp;
3007
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003008 if (ht40) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003009 *max_cw = CHAN_WIDTH_40;
3010 adjusted_snr = snr +
3011 wpas_channel_width_rssi_bump(
3012 ies, ies_len, CHAN_WIDTH_40);
3013 tmp = max_ht40_rate(adjusted_snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003014 if (tmp > est)
3015 est = tmp;
3016 }
3017
Hai Shaloma20dcd72022-02-04 13:43:00 -08003018 if (vht80) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003019 *max_cw = CHAN_WIDTH_80;
3020 adjusted_snr = snr +
3021 wpas_channel_width_rssi_bump(
3022 ies, ies_len, CHAN_WIDTH_80);
3023 tmp = max_vht80_rate(adjusted_snr) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003024 if (tmp > est)
3025 est = tmp;
3026 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08003027
3028 if (vht160 &&
3029 (hw_mode->vht_capab &
3030 (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3031 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003032 *max_cw = CHAN_WIDTH_160;
3033 adjusted_snr = snr +
3034 wpas_channel_width_rssi_bump(
3035 ies, ies_len, CHAN_WIDTH_160);
3036 tmp = max_vht160_rate(adjusted_snr) + 1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003037 if (tmp > est)
3038 est = tmp;
3039 }
3040 }
3041 }
3042
3043 if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
3044 /* Use +2 to assume HE is always faster than HT/VHT */
3045 struct ieee80211_he_capabilities *he;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00003046 struct ieee80211_eht_capabilities *eht;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003047 struct he_capabilities *own_he;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00003048 u8 cw, boost = 2;
3049 const u8 *eht_ie;
3050 bool is_eht = false;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003051
3052 ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
3053 if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
3054 return est;
3055 he = (struct ieee80211_he_capabilities *) &ie[3];
3056 own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
3057
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00003058 /* Use +3 to assume EHT is always faster than HE */
3059 if (hw_mode->eht_capab[IEEE80211_MODE_INFRA].eht_supported) {
3060 eht_ie = get_ie_ext(ies, ies_len,
3061 WLAN_EID_EXT_EHT_CAPABILITIES);
3062 if (eht_ie &&
3063 (eht_ie[1] >= 1 + IEEE80211_EHT_CAPAB_MIN_LEN)) {
3064 is_eht = true;
3065 boost = 3;
3066 }
3067 }
3068
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003069 if (*max_cw == CHAN_WIDTH_UNKNOWN)
3070 *max_cw = CHAN_WIDTH_20;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00003071 tmp = max_he_eht_rate(he20_table, snr, is_eht) + boost;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003072 if (tmp > est)
3073 est = tmp;
3074
3075 cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
3076 own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003077 if ((cw &
3078 (IS_2P4GHZ(freq) ?
3079 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
3080 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) && ht40) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003081 if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3082 *max_cw < CHAN_WIDTH_40)
3083 *max_cw = CHAN_WIDTH_40;
3084 adjusted_snr = snr + wpas_channel_width_rssi_bump(
3085 ies, ies_len, CHAN_WIDTH_40);
3086 tmp = max_he_eht_rate(he40_table, adjusted_snr,
3087 is_eht) + boost;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003088 if (tmp > est)
3089 est = tmp;
3090 }
3091
3092 if (!IS_2P4GHZ(freq) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003093 (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G) &&
3094 (!IS_5GHZ(freq) || vht80)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003095 if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3096 *max_cw < CHAN_WIDTH_80)
3097 *max_cw = CHAN_WIDTH_80;
3098 adjusted_snr = snr + wpas_channel_width_rssi_bump(
3099 ies, ies_len, CHAN_WIDTH_80);
3100 tmp = max_he_eht_rate(he80_table, adjusted_snr,
3101 is_eht) + boost;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003102 if (tmp > est)
3103 est = tmp;
3104 }
3105
3106 if (!IS_2P4GHZ(freq) &&
3107 (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003108 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) &&
3109 (!IS_5GHZ(freq) || vht160)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003110 if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3111 *max_cw < CHAN_WIDTH_160)
3112 *max_cw = CHAN_WIDTH_160;
3113 adjusted_snr = snr + wpas_channel_width_rssi_bump(
3114 ies, ies_len, CHAN_WIDTH_160);
3115 tmp = max_he_eht_rate(he160_table, adjusted_snr,
3116 is_eht) + boost;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00003117 if (tmp > est)
3118 est = tmp;
3119 }
3120
3121 if (!is_eht)
3122 return est;
3123
3124 eht = (struct ieee80211_eht_capabilities *) &eht_ie[3];
3125
3126 if (is_6ghz_freq(freq) &&
3127 (eht->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
3128 EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003129 if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3130 *max_cw < CHAN_WIDTH_320)
3131 *max_cw = CHAN_WIDTH_320;
3132 adjusted_snr = snr + wpas_channel_width_rssi_bump(
3133 ies, ies_len, CHAN_WIDTH_320);
3134 tmp = max_he_eht_rate(eht320_table, adjusted_snr, true);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003135 if (tmp > est)
3136 est = tmp;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003137 }
3138 }
3139
Hai Shalomfdcde762020-04-02 11:19:20 -07003140 return est;
3141}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003142
Hai Shalomfdcde762020-04-02 11:19:20 -07003143
3144void scan_est_throughput(struct wpa_supplicant *wpa_s,
3145 struct wpa_scan_res *res)
3146{
3147 int rate; /* max legacy rate in 500 kb/s units */
3148 int snr = res->snr;
3149 const u8 *ies = (const void *) (res + 1);
3150 size_t ie_len = res->ie_len;
3151
3152 if (res->est_throughput)
3153 return;
3154
3155 /* Get maximum legacy rate */
3156 rate = wpa_scan_get_max_rate(res);
3157
3158 if (!ie_len)
3159 ie_len = res->beacon_ie_len;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003160 res->est_throughput = wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr,
3161 res->freq, &res->max_cw);
Hai Shalomfdcde762020-04-02 11:19:20 -07003162
3163 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003164}
3165
3166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167/**
3168 * wpa_supplicant_get_scan_results - Get scan results
3169 * @wpa_s: Pointer to wpa_supplicant data
3170 * @info: Information about what was scanned or %NULL if not available
3171 * @new_scan: Whether a new scan was performed
3172 * Returns: Scan results, %NULL on failure
3173 *
3174 * This function request the current scan results from the driver and updates
3175 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
3176 * results with wpa_scan_results_free().
3177 */
3178struct wpa_scan_results *
3179wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
Sunil Ravi88611412024-06-28 17:34:56 +00003180 struct scan_info *info, int new_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181{
3182 struct wpa_scan_results *scan_res;
3183 size_t i;
3184 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
3185
Sunil Ravi88611412024-06-28 17:34:56 +00003186 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 if (scan_res == NULL) {
3188 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
3189 return NULL;
3190 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003191 if (scan_res->fetch_time.sec == 0) {
3192 /*
3193 * Make sure we have a valid timestamp if the driver wrapper
3194 * does not set this.
3195 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003196 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003197 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003198 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003200 for (i = 0; i < scan_res->num; i++) {
3201 struct wpa_scan_res *scan_res_item = scan_res->res[i];
3202
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003203 scan_snr(scan_res_item);
3204 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003205 }
3206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003208 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003209 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
3210 "provisioning rules");
3211 compar = wpa_scan_result_wps_compar;
3212 }
3213#endif /* CONFIG_WPS */
3214
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003215 if (scan_res->res) {
3216 qsort(scan_res->res, scan_res->num,
3217 sizeof(struct wpa_scan_res *), compar);
3218 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003219 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220
Dmitry Shmidt29333592017-01-09 12:27:11 -08003221 if (wpa_s->ignore_post_flush_scan_res) {
3222 /* FLUSH command aborted an ongoing scan and these are the
3223 * results from the aborted scan. Do not process the results to
3224 * maintain flushed state. */
3225 wpa_dbg(wpa_s, MSG_DEBUG,
3226 "Do not update BSS table based on pending post-FLUSH scan results");
3227 wpa_s->ignore_post_flush_scan_res = 0;
3228 return scan_res;
3229 }
3230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231 wpa_bss_update_start(wpa_s);
3232 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003233 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
3234 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235 wpa_bss_update_end(wpa_s, info, new_scan);
3236
3237 return scan_res;
3238}
3239
3240
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003241/**
3242 * wpa_supplicant_update_scan_results - Update scan results from the driver
3243 * @wpa_s: Pointer to wpa_supplicant data
3244 * Returns: 0 on success, -1 on failure
3245 *
3246 * This function updates the BSS table within wpa_supplicant based on the
3247 * currently available scan results from the driver without requesting a new
3248 * scan. This is used in cases where the driver indicates an association
3249 * (including roaming within ESS) and wpa_supplicant does not yet have the
3250 * needed information to complete the connection (e.g., to perform validation
3251 * steps in 4-way handshake).
3252 */
Sunil Ravi88611412024-06-28 17:34:56 +00003253int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254{
3255 struct wpa_scan_results *scan_res;
Sunil Ravi88611412024-06-28 17:34:56 +00003256 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 if (scan_res == NULL)
3258 return -1;
3259 wpa_scan_results_free(scan_res);
3260
3261 return 0;
3262}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08003263
3264
3265/**
3266 * scan_only_handler - Reports scan results
3267 */
3268void scan_only_handler(struct wpa_supplicant *wpa_s,
3269 struct wpa_scan_results *scan_res)
3270{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003271 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003272 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3273 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
3274 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
3275 wpa_s->manual_scan_id);
3276 wpa_s->manual_scan_use_id = 0;
3277 } else {
3278 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
3279 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08003280 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003281 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003282 if (wpa_s->scan_work) {
3283 struct wpa_radio_work *work = wpa_s->scan_work;
3284 wpa_s->scan_work = NULL;
3285 radio_work_done(work);
3286 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003287
3288 if (wpa_s->wpa_state == WPA_SCANNING)
3289 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08003290}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07003291
3292
3293int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
3294{
3295 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
3296}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003297
3298
3299struct wpa_driver_scan_params *
3300wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
3301{
3302 struct wpa_driver_scan_params *params;
3303 size_t i;
3304 u8 *n;
3305
3306 params = os_zalloc(sizeof(*params));
3307 if (params == NULL)
3308 return NULL;
3309
3310 for (i = 0; i < src->num_ssids; i++) {
3311 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003312 n = os_memdup(src->ssids[i].ssid,
3313 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003314 if (n == NULL)
3315 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003316 params->ssids[i].ssid = n;
3317 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
3318 }
3319 }
3320 params->num_ssids = src->num_ssids;
3321
3322 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003323 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003324 if (n == NULL)
3325 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003326 params->extra_ies = n;
3327 params->extra_ies_len = src->extra_ies_len;
3328 }
3329
3330 if (src->freqs) {
3331 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003332 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003333 if (params->freqs == NULL)
3334 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003335 }
3336
3337 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003338 params->filter_ssids = os_memdup(src->filter_ssids,
3339 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003340 src->num_filter_ssids);
3341 if (params->filter_ssids == NULL)
3342 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003343 params->num_filter_ssids = src->num_filter_ssids;
3344 }
3345
3346 params->filter_rssi = src->filter_rssi;
3347 params->p2p_probe = src->p2p_probe;
3348 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003349 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003350 params->duration = src->duration;
3351 params->duration_mandatory = src->duration_mandatory;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003352 params->oce_scan = src->oce_scan;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003353
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003354 if (src->sched_scan_plans_num > 0) {
3355 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003356 os_memdup(src->sched_scan_plans,
3357 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003358 src->sched_scan_plans_num);
3359 if (!params->sched_scan_plans)
3360 goto failed;
3361
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003362 params->sched_scan_plans_num = src->sched_scan_plans_num;
3363 }
3364
Hai Shalomc3565922019-10-28 11:58:20 -07003365 if (src->mac_addr_rand &&
3366 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
3367 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003368
3369 if (src->bssid) {
3370 u8 *bssid;
3371
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003372 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003373 if (!bssid)
3374 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003375 params->bssid = bssid;
3376 }
3377
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003378 params->relative_rssi_set = src->relative_rssi_set;
3379 params->relative_rssi = src->relative_rssi;
3380 params->relative_adjust_band = src->relative_adjust_band;
3381 params->relative_adjust_rssi = src->relative_adjust_rssi;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003382 params->p2p_include_6ghz = src->p2p_include_6ghz;
Sunil8cd6f4d2022-06-28 18:40:46 +00003383 params->non_coloc_6ghz = src->non_coloc_6ghz;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003384 params->min_probe_req_content = src->min_probe_req_content;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003385 return params;
3386
3387failed:
3388 wpa_scan_free_params(params);
3389 return NULL;
3390}
3391
3392
3393void wpa_scan_free_params(struct wpa_driver_scan_params *params)
3394{
3395 size_t i;
3396
3397 if (params == NULL)
3398 return;
3399
3400 for (i = 0; i < params->num_ssids; i++)
3401 os_free((u8 *) params->ssids[i].ssid);
3402 os_free((u8 *) params->extra_ies);
3403 os_free(params->freqs);
3404 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003405 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003406
3407 /*
3408 * Note: params->mac_addr_mask points to same memory allocation and
3409 * must not be freed separately.
3410 */
3411 os_free((u8 *) params->mac_addr);
3412
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003413 os_free((u8 *) params->bssid);
3414
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003415 os_free(params);
3416}
Dmitry Shmidt98660862014-03-11 17:26:21 -07003417
3418
3419int wpas_start_pno(struct wpa_supplicant *wpa_s)
3420{
Hai Shalomfdcde762020-04-02 11:19:20 -07003421 int ret;
3422 size_t prio, i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003423 struct wpa_ssid *ssid;
3424 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003425 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07003426 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003427
3428 if (!wpa_s->sched_scan_supported)
3429 return -1;
3430
Dmitry Shmidte4663042016-04-04 10:07:49 -07003431 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
3432 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
3433 else
3434 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
3435 if (max_sched_scan_ssids < 1)
3436 return -1;
3437
Dmitry Shmidt98660862014-03-11 17:26:21 -07003438 if (wpa_s->pno || wpa_s->pno_sched_pending)
3439 return 0;
3440
3441 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003442 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003443 wpa_printf(MSG_ERROR, "PNO: In assoc process");
3444 return -EAGAIN;
3445 }
3446
3447 if (wpa_s->wpa_state == WPA_SCANNING) {
3448 wpa_supplicant_cancel_scan(wpa_s);
3449 if (wpa_s->sched_scanning) {
3450 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
3451 "ongoing sched scan");
3452 wpa_supplicant_cancel_sched_scan(wpa_s);
3453 wpa_s->pno_sched_pending = 1;
3454 return 0;
3455 }
3456 }
3457
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003458 if (wpa_s->sched_scan_stop_req) {
3459 wpa_printf(MSG_DEBUG,
3460 "Schedule PNO after previous sched scan has stopped");
3461 wpa_s->pno_sched_pending = 1;
3462 return 0;
3463 }
3464
Dmitry Shmidt98660862014-03-11 17:26:21 -07003465 os_memset(&params, 0, sizeof(params));
3466
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003467 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003468 ssid = wpa_s->conf->ssid;
3469 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003470 if (!wpas_network_disabled(wpa_s, ssid)) {
3471 num_match_ssid++;
3472 if (ssid->scan_ssid)
3473 num_ssid++;
3474 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003475 ssid = ssid->next;
3476 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003477
3478 if (num_match_ssid == 0) {
3479 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3480 return -1;
3481 }
3482
3483 if (num_match_ssid > num_ssid) {
3484 params.num_ssids++; /* wildcard */
3485 num_ssid++;
3486 }
3487
Dmitry Shmidte4663042016-04-04 10:07:49 -07003488 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003489 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07003490 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3491 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003492 }
3493
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003494 if (num_match_ssid > wpa_s->max_match_sets) {
3495 num_match_ssid = wpa_s->max_match_sets;
3496 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003497 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003498 params.filter_ssids = os_calloc(num_match_ssid,
3499 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07003500 if (params.filter_ssids == NULL)
3501 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003502
Dmitry Shmidt98660862014-03-11 17:26:21 -07003503 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003504 prio = 0;
3505 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003506 while (ssid) {
3507 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003508 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3509 params.ssids[params.num_ssids].ssid =
3510 ssid->ssid;
3511 params.ssids[params.num_ssids].ssid_len =
3512 ssid->ssid_len;
3513 params.num_ssids++;
3514 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003515 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3516 ssid->ssid_len);
3517 params.filter_ssids[i].ssid_len = ssid->ssid_len;
3518 params.num_filter_ssids++;
3519 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003520 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07003521 break;
3522 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003523 if (ssid->pnext)
3524 ssid = ssid->pnext;
3525 else if (prio + 1 == wpa_s->conf->num_prio)
3526 break;
3527 else
3528 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003529 }
3530
3531 if (wpa_s->conf->filter_rssi)
3532 params.filter_rssi = wpa_s->conf->filter_rssi;
3533
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003534 if (wpa_s->sched_scan_plans_num) {
3535 params.sched_scan_plans = wpa_s->sched_scan_plans;
3536 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3537 } else {
3538 /* Set one scan plan that will run infinitely */
3539 if (wpa_s->conf->sched_scan_interval)
3540 scan_plan.interval = wpa_s->conf->sched_scan_interval;
3541 else
3542 scan_plan.interval = 10;
3543
3544 scan_plan.iterations = 0;
3545 params.sched_scan_plans = &scan_plan;
3546 params.sched_scan_plans_num = 1;
3547 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003548
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003549 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3550
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003551 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3552 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3553 params.freqs = wpa_s->manual_sched_scan_freqs;
3554 }
3555
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003556 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
Hai Shalomc3565922019-10-28 11:58:20 -07003557 wpa_s->wpa_state <= WPA_SCANNING)
3558 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003559
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003560 wpa_scan_set_relative_rssi_params(wpa_s, &params);
3561
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003562 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003563 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07003564 os_free(params.mac_addr);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003565 if (ret == 0)
3566 wpa_s->pno = 1;
3567 else
3568 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3569 return ret;
3570}
3571
3572
3573int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3574{
3575 int ret = 0;
3576
3577 if (!wpa_s->pno)
3578 return 0;
3579
3580 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003581 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003582
3583 wpa_s->pno = 0;
3584 wpa_s->pno_sched_pending = 0;
3585
3586 if (wpa_s->wpa_state == WPA_SCANNING)
3587 wpa_supplicant_req_scan(wpa_s, 0, 0);
3588
3589 return ret;
3590}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003591
3592
3593void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3594 unsigned int type)
3595{
3596 type &= MAC_ADDR_RAND_ALL;
3597 wpa_s->mac_addr_rand_enable &= ~type;
3598
3599 if (type & MAC_ADDR_RAND_SCAN) {
3600 os_free(wpa_s->mac_addr_scan);
3601 wpa_s->mac_addr_scan = NULL;
3602 }
3603
3604 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3605 os_free(wpa_s->mac_addr_sched_scan);
3606 wpa_s->mac_addr_sched_scan = NULL;
3607 }
3608
3609 if (type & MAC_ADDR_RAND_PNO) {
3610 os_free(wpa_s->mac_addr_pno);
3611 wpa_s->mac_addr_pno = NULL;
3612 }
3613}
3614
3615
3616int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3617 unsigned int type, const u8 *addr,
3618 const u8 *mask)
3619{
3620 u8 *tmp = NULL;
3621
Hai Shalom74f70d42019-02-11 14:42:39 -08003622 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3623 wpa_printf(MSG_INFO,
3624 "scan: MAC randomization type %u != supported=%u",
3625 type, wpa_s->mac_addr_rand_supported);
3626 return -1;
3627 }
3628
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003629 wpas_mac_addr_rand_scan_clear(wpa_s, type);
3630
3631 if (addr) {
3632 tmp = os_malloc(2 * ETH_ALEN);
3633 if (!tmp)
3634 return -1;
3635 os_memcpy(tmp, addr, ETH_ALEN);
3636 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3637 }
3638
3639 if (type == MAC_ADDR_RAND_SCAN) {
3640 wpa_s->mac_addr_scan = tmp;
3641 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3642 wpa_s->mac_addr_sched_scan = tmp;
3643 } else if (type == MAC_ADDR_RAND_PNO) {
3644 wpa_s->mac_addr_pno = tmp;
3645 } else {
3646 wpa_printf(MSG_INFO,
3647 "scan: Invalid MAC randomization type=0x%x",
3648 type);
3649 os_free(tmp);
3650 return -1;
3651 }
3652
3653 wpa_s->mac_addr_rand_enable |= type;
3654 return 0;
3655}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003656
3657
Hai Shalomc3565922019-10-28 11:58:20 -07003658int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3659 unsigned int type, u8 *mask)
3660{
3661 const u8 *to_copy;
3662
3663 if ((wpa_s->mac_addr_rand_enable & type) != type)
3664 return -1;
3665
3666 if (type == MAC_ADDR_RAND_SCAN) {
3667 to_copy = wpa_s->mac_addr_scan;
3668 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3669 to_copy = wpa_s->mac_addr_sched_scan;
3670 } else if (type == MAC_ADDR_RAND_PNO) {
3671 to_copy = wpa_s->mac_addr_pno;
3672 } else {
3673 wpa_printf(MSG_DEBUG,
3674 "scan: Invalid MAC randomization type=0x%x",
3675 type);
3676 return -1;
3677 }
3678
3679 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3680 return 0;
3681}
3682
3683
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003684int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3685{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003686 struct wpa_radio_work *work;
3687 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003688
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003689 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3690 if (work->wpa_s != wpa_s || !work->started ||
3691 (os_strcmp(work->type, "scan") != 0 &&
3692 os_strcmp(work->type, "p2p-scan") != 0))
3693 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003694 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003695 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003696 }
3697
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003698 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3699 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003700}
3701
3702
3703int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3704{
3705 struct sched_scan_plan *scan_plans = NULL;
3706 const char *token, *context = NULL;
3707 unsigned int num = 0;
3708
3709 if (!cmd)
3710 return -1;
3711
3712 if (!cmd[0]) {
3713 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3714 os_free(wpa_s->sched_scan_plans);
3715 wpa_s->sched_scan_plans = NULL;
3716 wpa_s->sched_scan_plans_num = 0;
3717 return 0;
3718 }
3719
3720 while ((token = cstr_token(cmd, " ", &context))) {
3721 int ret;
3722 struct sched_scan_plan *scan_plan, *n;
3723
3724 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3725 if (!n)
3726 goto fail;
3727
3728 scan_plans = n;
3729 scan_plan = &scan_plans[num];
3730 num++;
3731
3732 ret = sscanf(token, "%u:%u", &scan_plan->interval,
3733 &scan_plan->iterations);
3734 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3735 wpa_printf(MSG_ERROR,
3736 "Invalid sched scan plan input: %s", token);
3737 goto fail;
3738 }
3739
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003740 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3741 wpa_printf(MSG_WARNING,
3742 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3743 num, scan_plan->interval,
3744 wpa_s->max_sched_scan_plan_interval);
3745 scan_plan->interval =
3746 wpa_s->max_sched_scan_plan_interval;
3747 }
3748
3749 if (ret == 1) {
3750 scan_plan->iterations = 0;
3751 break;
3752 }
3753
3754 if (!scan_plan->iterations) {
3755 wpa_printf(MSG_ERROR,
3756 "scan plan %u: Number of iterations cannot be zero",
3757 num);
3758 goto fail;
3759 }
3760
3761 if (scan_plan->iterations >
3762 wpa_s->max_sched_scan_plan_iterations) {
3763 wpa_printf(MSG_WARNING,
3764 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3765 num, scan_plan->iterations,
3766 wpa_s->max_sched_scan_plan_iterations);
3767 scan_plan->iterations =
3768 wpa_s->max_sched_scan_plan_iterations;
3769 }
3770
3771 wpa_printf(MSG_DEBUG,
3772 "scan plan %u: interval=%u iterations=%u",
3773 num, scan_plan->interval, scan_plan->iterations);
3774 }
3775
3776 if (!scan_plans) {
3777 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3778 goto fail;
3779 }
3780
3781 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3782 wpa_printf(MSG_ERROR,
3783 "All scan plans but the last must specify a number of iterations");
3784 goto fail;
3785 }
3786
3787 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3788 num, scan_plans[num - 1].interval);
3789
3790 if (num > wpa_s->max_sched_scan_plans) {
3791 wpa_printf(MSG_WARNING,
3792 "Too many scheduled scan plans (only %u supported)",
3793 wpa_s->max_sched_scan_plans);
3794 wpa_printf(MSG_WARNING,
3795 "Use only the first %u scan plans, and the last one (in infinite loop)",
3796 wpa_s->max_sched_scan_plans - 1);
3797 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3798 &scan_plans[num - 1], sizeof(*scan_plans));
3799 num = wpa_s->max_sched_scan_plans;
3800 }
3801
3802 os_free(wpa_s->sched_scan_plans);
3803 wpa_s->sched_scan_plans = scan_plans;
3804 wpa_s->sched_scan_plans_num = num;
3805
3806 return 0;
3807
3808fail:
3809 os_free(scan_plans);
3810 wpa_printf(MSG_ERROR, "invalid scan plans list");
3811 return -1;
3812}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003813
3814
3815/**
3816 * wpas_scan_reset_sched_scan - Reset sched_scan state
3817 * @wpa_s: Pointer to wpa_supplicant data
3818 *
3819 * This function is used to cancel a running scheduled scan and to reset an
3820 * internal scan state to continue with a regular scan on the following
3821 * wpa_supplicant_req_scan() calls.
3822 */
3823void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3824{
3825 wpa_s->normal_scans = 0;
3826 if (wpa_s->sched_scanning) {
3827 wpa_s->sched_scan_timed_out = 0;
3828 wpa_s->prev_sched_ssid = NULL;
3829 wpa_supplicant_cancel_sched_scan(wpa_s);
3830 }
3831}
3832
3833
3834void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3835{
3836 /* simulate timeout to restart the sched scan */
3837 wpa_s->sched_scan_timed_out = 1;
3838 wpa_s->prev_sched_ssid = NULL;
3839 wpa_supplicant_cancel_sched_scan(wpa_s);
3840}