blob: 88d7e6e65e989f5bbc0cfd5bc5e0f8b3524a7efb [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt3a787e62013-01-17 10:32:35 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "config.h"
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "wps_supplicant.h"
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "notify.h"
23#include "bss.h"
24#include "scan.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080025#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026
27
28static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29{
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
32
33 ssid = wpa_supplicant_get_ssid(wpa_s);
34 if (ssid == NULL)
35 return;
36
37 if (wpa_s->current_ssid == NULL) {
38 wpa_s->current_ssid = ssid;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070039 wpas_notify_network_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040 }
41 wpa_supplicant_initiate_eapol(wpa_s);
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
49#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 enum wps_request_type *req_type)
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
55
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
61 *req_type = wpas_wps_get_req_type(ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -070062 if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 return 2;
64 }
65
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080067 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
68 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080069 wpa_s->wps->dev.p2p = 1;
70 if (!wps) {
71 wps = 1;
72 *req_type = WPS_REQ_ENROLLEE_INFO;
73 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080074 }
75#endif /* CONFIG_P2P */
76
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 return wps;
78}
79#endif /* CONFIG_WPS */
80
81
Hai Shalomc3565922019-10-28 11:58:20 -070082static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
83 const u8 *mac_addr)
84{
85 u8 *tmp;
86
87 if (params->mac_addr) {
88 params->mac_addr_mask = NULL;
89 os_free(params->mac_addr);
90 params->mac_addr = NULL;
91 }
92
93 params->mac_addr_rand = 1;
94
95 if (!mac_addr)
96 return 0;
97
98 tmp = os_malloc(2 * ETH_ALEN);
99 if (!tmp)
100 return -1;
101
102 os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
103 params->mac_addr = tmp;
104 params->mac_addr_mask = tmp + ETH_ALEN;
105 return 0;
106}
107
108
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800109/**
110 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
111 * @wpa_s: Pointer to wpa_supplicant data
112 * Returns: 0 if no networks are enabled, >0 if networks are enabled
113 *
114 * This function is used to figure out whether any networks (or Interworking
115 * with enabled credentials and auto_interworking) are present in the current
116 * configuration.
117 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700118int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700121 int count = 0, disabled = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800122
123 if (wpa_s->p2p_mgmt)
124 return 0; /* no normal network profiles on p2p_mgmt interface */
125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700127 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700129 else
130 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 ssid = ssid->next;
132 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700133 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
134 wpa_s->conf->auto_interworking)
135 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700136 if (count == 0 && disabled > 0) {
137 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
138 "networks)", disabled);
139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 return count;
141}
142
143
144static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
145 struct wpa_ssid *ssid)
146{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700147 int min_temp_disabled = 0;
148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 while (ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700150 if (!wpas_network_disabled(wpa_s, ssid)) {
151 int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
152
153 if (temp_disabled <= 0)
154 break;
155
156 if (!min_temp_disabled ||
157 temp_disabled < min_temp_disabled)
158 min_temp_disabled = temp_disabled;
159 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 ssid = ssid->next;
161 }
162
163 /* ap_scan=2 mode - try to associate with each SSID. */
164 if (ssid == NULL) {
165 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
166 "end of scan list - go back to beginning");
167 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700168 wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 return;
170 }
171 if (ssid->next) {
172 /* Continue from the next SSID on the next attempt. */
173 wpa_s->prev_scan_ssid = ssid;
174 } else {
175 /* Start from the beginning of the SSID list. */
176 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
177 }
178 wpa_supplicant_associate(wpa_s, NULL, ssid);
179}
180
181
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800182static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800184 struct wpa_supplicant *wpa_s = work->wpa_s;
185 struct wpa_driver_scan_params *params = work->ctx;
186 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800188 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800189 if (!work->started) {
190 wpa_scan_free_params(params);
191 return;
192 }
193 wpa_supplicant_notify_scanning(wpa_s, 0);
194 wpas_notify_scan_done(wpa_s, 0);
195 wpa_s->scan_work = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 return;
197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198
Hai Shalomc3565922019-10-28 11:58:20 -0700199 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
200 wpa_s->wpa_state <= WPA_SCANNING)
201 wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
202
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700203 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
204 wpa_msg(wpa_s, MSG_INFO,
205 "Failed to assign random MAC address for a scan");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700206 wpa_scan_free_params(params);
207 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700208 radio_work_done(work);
209 return;
210 }
211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800212 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800214 if (wpa_s->clear_driver_scan_cache) {
215 wpa_printf(MSG_DEBUG,
216 "Request driver to clear scan cache due to local BSS flush");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800217 params->only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800218 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800219 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800220 /*
221 * Store the obtained vendor scan cookie (if any) in wpa_s context.
222 * The current design is to allow only one scan request on each
223 * interface, hence having this scan cookie stored in wpa_s context is
224 * fine for now.
225 *
226 * Revisit this logic if concurrent scan operations per interface
227 * is supported.
228 */
229 if (ret == 0)
230 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800231 wpa_scan_free_params(params);
232 work->ctx = NULL;
233 if (ret) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700234 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
235 !wpa_s->beacon_rep_data.token;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800236
237 if (wpa_s->disconnected)
238 retry = 0;
239
Hai Shalom899fcc72020-10-19 14:38:18 -0700240 /* do not retry if operation is not supported */
241 if (ret == -EOPNOTSUPP)
242 retry = 0;
243
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800244 wpa_supplicant_notify_scanning(wpa_s, 0);
245 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800246 if (wpa_s->wpa_state == WPA_SCANNING)
247 wpa_supplicant_set_state(wpa_s,
248 wpa_s->scan_prev_wpa_state);
249 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
250 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800252
253 if (retry) {
254 /* Restore scan_req since we will try to scan again */
255 wpa_s->scan_req = wpa_s->last_scan_req;
256 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700257 } else if (wpa_s->scan_res_handler) {
258 /* Clear the scan_res_handler */
259 wpa_s->scan_res_handler = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800260 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700261
262 if (wpa_s->beacon_rep_data.token)
263 wpas_rrm_refuse_request(wpa_s);
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800267
268 os_get_reltime(&wpa_s->scan_trigger_time);
269 wpa_s->scan_runs++;
270 wpa_s->normal_scans++;
271 wpa_s->own_scan_requested = 1;
272 wpa_s->clear_driver_scan_cache = 0;
273 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274}
275
276
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800277/**
278 * wpa_supplicant_trigger_scan - Request driver to start a scan
279 * @wpa_s: Pointer to wpa_supplicant data
280 * @params: Scan parameters
281 * Returns: 0 on success, -1 on failure
282 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
284 struct wpa_driver_scan_params *params)
285{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800286 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800288 if (wpa_s->scan_work) {
289 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
290 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 }
292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800293 ctx = wpa_scan_clone_params(params);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700294 if (!ctx ||
295 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800296 {
297 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700298 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800299 return -1;
300 }
301
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000302 wpa_s->wps_scan_done = false;
303
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800304 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800305}
306
307
308static void
309wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
310{
311 struct wpa_supplicant *wpa_s = eloop_ctx;
312
313 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
314
315 if (wpa_supplicant_req_sched_scan(wpa_s))
316 wpa_supplicant_req_scan(wpa_s, 0, 0);
317}
318
319
320static void
321wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
322{
323 struct wpa_supplicant *wpa_s = eloop_ctx;
324
325 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
326
327 wpa_s->sched_scan_timed_out = 1;
328 wpa_supplicant_cancel_sched_scan(wpa_s);
329}
330
331
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700332static int
333wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
334 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800335{
336 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700337
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800338 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800339 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800340 if (ret)
341 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800342 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800343 wpa_s->sched_scanning = 1;
344
345 return ret;
346}
347
348
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700349static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800350{
351 int ret;
352
353 ret = wpa_drv_stop_sched_scan(wpa_s);
354 if (ret) {
355 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
356 /* TODO: what to do if stopping fails? */
357 return -1;
358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359
360 return ret;
361}
362
363
364static struct wpa_driver_scan_filter *
365wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
366{
367 struct wpa_driver_scan_filter *ssids;
368 struct wpa_ssid *ssid;
369 size_t count;
370
371 *num_ssids = 0;
372 if (!conf->filter_ssids)
373 return NULL;
374
375 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
376 if (ssid->ssid && ssid->ssid_len)
377 count++;
378 }
379 if (count == 0)
380 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800381 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 if (ssids == NULL)
383 return NULL;
384
385 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
386 if (!ssid->ssid || !ssid->ssid_len)
387 continue;
388 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
389 ssids[*num_ssids].ssid_len = ssid->ssid_len;
390 (*num_ssids)++;
391 }
392
393 return ssids;
394}
395
396
Hai Shaloma20dcd72022-02-04 13:43:00 -0800397#ifdef CONFIG_P2P
398static bool is_6ghz_supported(struct wpa_supplicant *wpa_s)
399{
400 struct hostapd_channel_data *chnl;
401 int i, j;
402
403 for (i = 0; i < wpa_s->hw.num_modes; i++) {
404 if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211A) {
405 chnl = wpa_s->hw.modes[i].channels;
406 for (j = 0; j < wpa_s->hw.modes[i].num_channels; j++) {
407 if (chnl[j].flag & HOSTAPD_CHAN_DISABLED)
408 continue;
409 if (is_6ghz_freq(chnl[j].freq))
410 return true;
411 }
412 }
413 }
414
415 return false;
416}
417#endif /* CONFIG_P2P */
418
419
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800420static void wpa_supplicant_optimize_freqs(
421 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
422{
423#ifdef CONFIG_P2P
424 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
425 wpa_s->go_params) {
426 /* Optimize provisioning state scan based on GO information */
427 if (wpa_s->p2p_in_provisioning < 5 &&
428 wpa_s->go_params->freq > 0) {
429 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
430 "preferred frequency %d MHz",
431 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800432 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800433 if (params->freqs)
434 params->freqs[0] = wpa_s->go_params->freq;
435 } else if (wpa_s->p2p_in_provisioning < 8 &&
436 wpa_s->go_params->freq_list[0]) {
437 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
438 "channels");
439 int_array_concat(&params->freqs,
440 wpa_s->go_params->freq_list);
441 if (params->freqs)
442 int_array_sort_unique(params->freqs);
443 }
444 wpa_s->p2p_in_provisioning++;
445 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700446
447 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
448 /*
Matthew Wangdcf19452022-11-07 20:42:52 -0800449 * Perform a single-channel scan if the GO has already been
450 * discovered on another non-P2P interface. Note that a scan
451 * initiated by a P2P interface (e.g. the device interface)
452 * should already have sufficient IEs and scan results will be
453 * fetched on interface creation in that case.
454 */
455 if (wpa_s->p2p_in_invitation == 1 && wpa_s->current_ssid) {
456 struct wpa_supplicant *ifs;
457 struct wpa_bss *bss = NULL;
458 struct wpa_ssid *ssid = wpa_s->current_ssid;
459 u8 *bssid = ssid->bssid_set ? ssid->bssid : NULL;
460 dl_list_for_each(ifs, &wpa_s->radio->ifaces,
461 struct wpa_supplicant, radio_list) {
462 bss = wpa_bss_get(ifs, bssid, ssid->ssid,
463 ssid->ssid_len);
464 if (bss)
465 break;
466 }
467 if (bss && !disabled_freq(wpa_s, bss->freq)) {
468 params->freqs = os_calloc(2, sizeof(int));
469 if (params->freqs)
470 params->freqs[0] = bss->freq;
471 }
472 }
473 /*
Dmitry Shmidt15907092014-03-25 10:42:57 -0700474 * Optimize scan based on GO information during persistent
475 * group reinvocation
476 */
Matthew Wangdcf19452022-11-07 20:42:52 -0800477 if (params->freqs == NULL && wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700478 wpa_s->p2p_invite_go_freq > 0) {
Matthew Wang36173112022-11-07 21:48:44 -0800479 if (wpa_s->p2p_invite_go_freq == 2 ||
480 wpa_s->p2p_invite_go_freq == 5) {
Matthew Wang36173112022-11-07 21:48:44 -0800481 enum hostapd_hw_mode mode;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000482
483 wpa_dbg(wpa_s, MSG_DEBUG,
484 "P2P: Scan only GO preferred band %d GHz during invitation",
485 wpa_s->p2p_invite_go_freq);
486
487 if (!wpa_s->hw.modes)
Matthew Wang36173112022-11-07 21:48:44 -0800488 return;
489 mode = wpa_s->p2p_invite_go_freq == 5 ?
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000490 HOSTAPD_MODE_IEEE80211A :
491 HOSTAPD_MODE_IEEE80211G;
Matthew Wang36173112022-11-07 21:48:44 -0800492 if (wpa_s->p2p_in_invitation <= 2)
493 wpa_add_scan_freqs_list(wpa_s, mode,
494 params, false,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000495 false, true);
496 if (!params->freqs || params->freqs[0] == 0)
Matthew Wang36173112022-11-07 21:48:44 -0800497 wpa_add_scan_freqs_list(wpa_s, mode,
498 params, false,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000499 false, false);
Matthew Wang36173112022-11-07 21:48:44 -0800500 } else {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000501 wpa_dbg(wpa_s, MSG_DEBUG,
502 "P2P: Scan only GO preferred frequency %d MHz during invitation",
Matthew Wang36173112022-11-07 21:48:44 -0800503 wpa_s->p2p_invite_go_freq);
504 params->freqs = os_calloc(2, sizeof(int));
505 if (params->freqs)
506 params->freqs[0] =
507 wpa_s->p2p_invite_go_freq;
508 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700509 }
510 wpa_s->p2p_in_invitation++;
511 if (wpa_s->p2p_in_invitation > 20) {
512 /*
513 * This should not really happen since the variable is
514 * cleared on group removal, but if it does happen, make
515 * sure we do not get stuck in special invitation scan
516 * mode.
517 */
518 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
519 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +0000520 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -0700521 }
522 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800523#endif /* CONFIG_P2P */
524
525#ifdef CONFIG_WPS
526 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
527 /*
528 * Optimize post-provisioning scan based on channel used
529 * during provisioning.
530 */
531 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
532 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800533 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800534 if (params->freqs)
535 params->freqs[0] = wpa_s->wps_freq;
536 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800537 } else if (wpa_s->after_wps)
538 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800540 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
541 {
542 /* Optimize provisioning scan based on already known channel */
543 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
544 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800545 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800546 if (params->freqs)
547 params->freqs[0] = wpa_s->wps_freq;
548 wpa_s->known_wps_freq = 0; /* only do this once */
549 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800550#endif /* CONFIG_WPS */
551}
552
553
554#ifdef CONFIG_INTERWORKING
555static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
556 struct wpabuf *buf)
557{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800558 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
559 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
560 1 + ETH_ALEN);
561 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
562 /* No Venue Info */
563 if (!is_zero_ether_addr(wpa_s->conf->hessid))
564 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
565}
566#endif /* CONFIG_INTERWORKING */
567
568
Hai Shalomce48b4a2018-09-05 11:41:35 -0700569#ifdef CONFIG_MBO
570static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
571 struct wpabuf **ie)
572{
573 if (wpabuf_resize(ie, 5)) {
574 wpa_printf(MSG_DEBUG,
575 "Failed to allocate space for FILS Request Parameters element");
576 return;
577 }
578
579 /* FILS Request Parameters element */
580 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
581 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
582 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
583 /* Parameter control bitmap */
584 wpabuf_put_u8(*ie, 0);
585 /* Max Channel Time field - contains the value of MaxChannelTime
586 * parameter of the MLME-SCAN.request primitive represented in units of
587 * TUs, as an unsigned integer. A Max Channel Time field value of 255
588 * is used to indicate any duration of more than 254 TUs, or an
589 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
590 */
591 wpabuf_put_u8(*ie, 255);
592}
593#endif /* CONFIG_MBO */
594
595
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700596void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
597{
598 struct wpabuf *default_ies = NULL;
599 u8 ext_capab[18];
Hai Shalom60840252021-02-19 19:02:11 -0800600 int ext_capab_len, frame_id;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700601 enum wpa_driver_if_type type = WPA_IF_STATION;
602
603#ifdef CONFIG_P2P
604 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
605 type = WPA_IF_P2P_CLIENT;
606#endif /* CONFIG_P2P */
607
608 wpa_drv_get_ext_capa(wpa_s, type);
609
610 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
611 sizeof(ext_capab));
612 if (ext_capab_len > 0 &&
613 wpabuf_resize(&default_ies, ext_capab_len) == 0)
614 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
615
616#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -0700617 if (wpa_s->enable_oce & OCE_STA)
618 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700619 /* Send MBO and OCE capabilities */
620 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700621 wpas_mbo_scan_ie(wpa_s, default_ies);
622#endif /* CONFIG_MBO */
623
Hai Shalom60840252021-02-19 19:02:11 -0800624 if (type == WPA_IF_P2P_CLIENT)
625 frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
626 else
627 frame_id = VENDOR_ELEM_PROBE_REQ;
628
629 if (wpa_s->vendor_elem[frame_id]) {
630 size_t len;
631
632 len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
633 if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
634 wpabuf_put_buf(default_ies,
635 wpa_s->vendor_elem[frame_id]);
636 }
637
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700638 if (default_ies)
639 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
640 wpabuf_len(default_ies));
641 wpabuf_free(default_ies);
642}
643
644
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700645static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800646{
647 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700648 u8 ext_capab[18];
649 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800650#ifdef CONFIG_WPS
651 int wps = 0;
652 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
653#endif /* CONFIG_WPS */
654
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700655#ifdef CONFIG_P2P
656 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
657 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
658 else
659#endif /* CONFIG_P2P */
660 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
661
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700662 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
663 sizeof(ext_capab));
664 if (ext_capab_len > 0 &&
665 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
666 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
667
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800668#ifdef CONFIG_INTERWORKING
669 if (wpa_s->conf->interworking &&
670 wpabuf_resize(&extra_ie, 100) == 0)
671 wpas_add_interworking_elements(wpa_s, extra_ie);
672#endif /* CONFIG_INTERWORKING */
673
Hai Shalomce48b4a2018-09-05 11:41:35 -0700674#ifdef CONFIG_MBO
675 if (wpa_s->enable_oce & OCE_STA)
676 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
677#endif /* CONFIG_MBO */
678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800679#ifdef CONFIG_WPS
680 wps = wpas_wps_in_use(wpa_s, &req_type);
681
682 if (wps) {
683 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700684 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
685 DEV_PW_DEFAULT,
686 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800687 wpa_s->wps->uuid, req_type,
688 0, NULL);
689 if (wps_ie) {
690 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
691 wpabuf_put_buf(extra_ie, wps_ie);
692 wpabuf_free(wps_ie);
693 }
694 }
695
696#ifdef CONFIG_P2P
697 if (wps) {
698 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
699 if (wpabuf_resize(&extra_ie, ielen) == 0)
700 wpas_p2p_scan_ie(wpa_s, extra_ie);
701 }
702#endif /* CONFIG_P2P */
703
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800704 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
705
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800706#endif /* CONFIG_WPS */
707
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700708#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -0800709 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
710 wpas_hs20_add_indication(extra_ie, -1, 0);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700711#endif /* CONFIG_HS20 */
712
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800713#ifdef CONFIG_FST
714 if (wpa_s->fst_ies &&
715 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
716 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
717#endif /* CONFIG_FST */
718
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800719#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700720 /* Send MBO and OCE capabilities */
721 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800722 wpas_mbo_scan_ie(wpa_s, extra_ie);
723#endif /* CONFIG_MBO */
724
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700725 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
726 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
727
728 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
729 wpabuf_put_buf(extra_ie, buf);
730 }
731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732 return extra_ie;
733}
734
735
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800736#ifdef CONFIG_P2P
737
738/*
739 * Check whether there are any enabled networks or credentials that could be
740 * used for a non-P2P connection.
741 */
742static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
743{
744 struct wpa_ssid *ssid;
745
746 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
747 if (wpas_network_disabled(wpa_s, ssid))
748 continue;
749 if (!ssid->p2p_group)
750 return 1;
751 }
752
753 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
754 wpa_s->conf->auto_interworking)
755 return 1;
756
757 return 0;
758}
759
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700760#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800761
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800762
Hai Shalom60840252021-02-19 19:02:11 -0800763int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
764 enum hostapd_hw_mode band,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000765 struct wpa_driver_scan_params *params,
766 bool is_6ghz, bool only_6ghz_psc,
Matthew Wang36173112022-11-07 21:48:44 -0800767 bool exclude_radar)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700768{
769 /* Include only supported channels for the specified band */
770 struct hostapd_hw_modes *mode;
Hai Shalom60840252021-02-19 19:02:11 -0800771 int num_chans = 0;
772 int *freqs, i;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700773
Hai Shalomfdcde762020-04-02 11:19:20 -0700774 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000775 if (!mode || !mode->num_channels)
Hai Shalom60840252021-02-19 19:02:11 -0800776 return -1;
777
778 if (params->freqs) {
779 while (params->freqs[num_chans])
780 num_chans++;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700781 }
782
Hai Shalom60840252021-02-19 19:02:11 -0800783 freqs = os_realloc(params->freqs,
784 (num_chans + mode->num_channels + 1) * sizeof(int));
785 if (!freqs)
786 return -1;
787
788 params->freqs = freqs;
789 for (i = 0; i < mode->num_channels; i++) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700790 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
791 continue;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000792 if (exclude_radar &&
793 (mode->channels[i].flag & HOSTAPD_CHAN_RADAR))
Matthew Wang36173112022-11-07 21:48:44 -0800794 continue;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000795
796 if (is_6ghz && only_6ghz_psc &&
797 !is_6ghz_psc_frequency(mode->channels[i].freq))
798 continue;
799
Hai Shalom60840252021-02-19 19:02:11 -0800800 params->freqs[num_chans++] = mode->channels[i].freq;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700801 }
Hai Shalom60840252021-02-19 19:02:11 -0800802 params->freqs[num_chans] = 0;
803
804 return 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700805}
806
807
808static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
809 struct wpa_driver_scan_params *params)
810{
811 if (wpa_s->hw.modes == NULL)
812 return; /* unknown what channels the driver supports */
813 if (params->freqs)
814 return; /* already using a limited channel set */
Hai Shalom60840252021-02-19 19:02:11 -0800815
816 if (wpa_s->setband_mask & WPA_SETBAND_5G)
817 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000818 false, false, false);
Hai Shalom60840252021-02-19 19:02:11 -0800819 if (wpa_s->setband_mask & WPA_SETBAND_2G)
820 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000821 false, false, false);
Hai Shalom60840252021-02-19 19:02:11 -0800822 if (wpa_s->setband_mask & WPA_SETBAND_6G)
823 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000824 true, false, false);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700825}
826
827
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800828static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
829 struct wpa_driver_scan_params *params,
830 size_t max_ssids, const u8 *ssid, size_t ssid_len)
831{
832 unsigned int j;
833
834 for (j = 0; j < params->num_ssids; j++) {
835 if (params->ssids[j].ssid_len == ssid_len &&
836 params->ssids[j].ssid &&
837 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
838 return; /* already in the list */
839 }
840
841 if (params->num_ssids + 1 > max_ssids) {
842 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
843 return;
844 }
845
846 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
847 wpa_ssid_txt(ssid, ssid_len));
848
849 params->ssids[params->num_ssids].ssid = ssid;
850 params->ssids[params->num_ssids].ssid_len = ssid_len;
851 params->num_ssids++;
852}
853
854
855static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
856 struct wpa_driver_scan_params *params,
857 struct wpa_ssid *ssid, size_t max_ssids)
858{
859#ifdef CONFIG_OWE
860 struct wpa_bss *bss;
861
862 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
863 return;
864
865 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
866 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
867
868 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
869 const u8 *owe, *pos, *end;
870 const u8 *owe_ssid;
871 size_t owe_ssid_len;
872
873 if (bss->ssid_len != ssid->ssid_len ||
874 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
875 continue;
876
877 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
878 if (!owe || owe[1] < 4)
879 continue;
880
881 pos = owe + 6;
882 end = owe + 2 + owe[1];
883
884 /* Must include BSSID and ssid_len */
885 if (end - pos < ETH_ALEN + 1)
886 return;
887
888 /* Skip BSSID */
889 pos += ETH_ALEN;
890 owe_ssid_len = *pos++;
891 owe_ssid = pos;
892
893 if ((size_t) (end - pos) < owe_ssid_len ||
894 owe_ssid_len > SSID_MAX_LEN)
895 return;
896
897 wpa_printf(MSG_DEBUG,
898 "OWE: scan_ssids: transition mode OWE ssid=%s",
899 wpa_ssid_txt(owe_ssid, owe_ssid_len));
900
901 wpa_add_scan_ssid(wpa_s, params, max_ssids,
902 owe_ssid, owe_ssid_len);
903 return;
904 }
905#endif /* CONFIG_OWE */
906}
907
908
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700909static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
910 struct wpa_driver_scan_params *params,
911 size_t max_ssids)
912{
913 unsigned int i;
914 struct wpa_ssid *ssid;
915
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700916 /*
917 * For devices with max_ssids greater than 1, leave the last slot empty
918 * for adding the wildcard scan entry.
919 */
920 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
921
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700922 for (i = 0; i < wpa_s->scan_id_count; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700923 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800924 if (!ssid)
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700925 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800926 if (ssid->scan_ssid)
927 wpa_add_scan_ssid(wpa_s, params, max_ssids,
928 ssid->ssid, ssid->ssid_len);
929 /*
930 * Also add the SSID of the OWE BSS, to allow discovery of
931 * transition mode APs more quickly.
932 */
933 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700934 }
935
936 wpa_s->scan_id_count = 0;
937}
938
939
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700940static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
941 struct wpa_driver_scan_params *params,
942 size_t max_ssids)
943{
944 unsigned int i;
945
946 if (wpa_s->ssids_from_scan_req == NULL ||
947 wpa_s->num_ssids_from_scan_req == 0)
948 return 0;
949
950 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
951 wpa_s->num_ssids_from_scan_req = max_ssids;
952 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
953 (unsigned int) max_ssids);
954 }
955
956 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
957 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
958 params->ssids[i].ssid_len =
959 wpa_s->ssids_from_scan_req[i].ssid_len;
960 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
961 params->ssids[i].ssid,
962 params->ssids[i].ssid_len);
963 }
964
965 params->num_ssids = wpa_s->num_ssids_from_scan_req;
966 wpa_s->num_ssids_from_scan_req = 0;
967 return 1;
968}
969
970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
972{
973 struct wpa_supplicant *wpa_s = eloop_ctx;
974 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800975 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700976 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700978 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800980 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981
Dmitry Shmidt29333592017-01-09 12:27:11 -0800982 wpa_s->ignore_post_flush_scan_res = 0;
983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
985 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
986 return;
987 }
988
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800989 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700990 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
992 return;
993 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800994
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700995 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800996 /*
997 * If we are already in scanning state, we shall reschedule the
998 * the incoming scan request.
999 */
1000 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
1001 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -07001002 return;
1003 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001004
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001006 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
1008 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1009 return;
1010 }
1011
1012 if (wpa_s->conf->ap_scan != 0 &&
1013 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
1014 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
1015 "overriding ap_scan configuration");
1016 wpa_s->conf->ap_scan = 0;
1017 wpas_notify_ap_scan_changed(wpa_s);
1018 }
1019
1020 if (wpa_s->conf->ap_scan == 0) {
1021 wpa_supplicant_gen_assoc_event(wpa_s);
1022 return;
1023 }
1024
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001025 ssid = NULL;
1026 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
1027 wpa_s->connect_without_scan) {
1028 connect_without_scan = 1;
1029 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1030 if (ssid == wpa_s->connect_without_scan)
1031 break;
1032 }
1033 }
1034
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001035 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001036 if (p2p_in_prog && p2p_in_prog != 2 &&
1037 (!ssid ||
1038 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001039 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
1040 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001041 return;
1042 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001043
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001044 /*
1045 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
1046 * used for changing modes inside wpa_supplicant (roaming,
1047 * auto-reconnect, etc). Discarding the scan might hurt these processes.
1048 * The normal use case for PNO is to suspend the host immediately after
1049 * starting PNO, so the periodic 100 ms attempts to run the scan do not
1050 * normally happen in practice multiple times, i.e., this is simply
1051 * restarting scanning once the host is woken up and PNO stopped.
1052 */
1053 if (wpa_s->pno || wpa_s->pno_sched_pending) {
1054 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
1055 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1056 return;
1057 }
1058
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001059 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 max_ssids = 1;
1061 else {
1062 max_ssids = wpa_s->max_scan_ssids;
1063 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1064 max_ssids = WPAS_MAX_SCAN_SSIDS;
1065 }
1066
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001067 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001068 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001070 if (connect_without_scan) {
1071 wpa_s->connect_without_scan = NULL;
1072 if (ssid) {
1073 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1074 "without scan step");
1075 wpa_supplicant_associate(wpa_s, NULL, ssid);
1076 return;
1077 }
1078 }
1079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 os_memset(&params, 0, sizeof(params));
1081
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001082 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1084 wpa_s->wpa_state == WPA_INACTIVE)
1085 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1086
Dmitry Shmidt04949592012-07-19 12:16:46 -07001087 /*
1088 * If autoscan has set its own scanning parameters
1089 */
1090 if (wpa_s->autoscan_params != NULL) {
1091 scan_params = wpa_s->autoscan_params;
1092 goto scan;
1093 }
1094
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001095 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1096 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1097 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1098 goto ssid_list_set;
1099 }
1100
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101#ifdef CONFIG_P2P
1102 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001103 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001104 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1105 wpa_s->p2p_in_provisioning,
1106 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001107 params.ssids[0].ssid = wpa_s->go_params->ssid;
1108 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1109 params.num_ssids = 1;
1110 goto ssid_list_set;
1111 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07001112
1113 if (wpa_s->p2p_in_invitation) {
1114 if (wpa_s->current_ssid) {
1115 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1116 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1117 params.ssids[0].ssid_len =
1118 wpa_s->current_ssid->ssid_len;
1119 params.num_ssids = 1;
1120 } else {
1121 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1122 }
1123 goto ssid_list_set;
1124 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001125#endif /* CONFIG_P2P */
1126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 /* Find the starting point from which to continue scanning */
1128 ssid = wpa_s->conf->ssid;
1129 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1130 while (ssid) {
1131 if (ssid == wpa_s->prev_scan_ssid) {
1132 ssid = ssid->next;
1133 break;
1134 }
1135 ssid = ssid->next;
1136 }
1137 }
1138
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001139 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001140#ifdef CONFIG_AP
1141 !wpa_s->ap_iface &&
1142#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001143 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001144 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001145 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 wpa_supplicant_assoc_try(wpa_s, ssid);
1147 return;
1148 } else if (wpa_s->conf->ap_scan == 2) {
1149 /*
1150 * User-initiated scan request in ap_scan == 2; scan with
1151 * wildcard SSID.
1152 */
1153 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001154 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1155 /*
1156 * Perform single-channel single-SSID scan for
1157 * reassociate-to-same-BSS operation.
1158 */
1159 /* Setup SSID */
1160 ssid = wpa_s->current_ssid;
1161 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1162 ssid->ssid, ssid->ssid_len);
1163 params.ssids[0].ssid = ssid->ssid;
1164 params.ssids[0].ssid_len = ssid->ssid_len;
1165 params.num_ssids = 1;
1166
1167 /*
1168 * Allocate memory for frequency array, allocate one extra
1169 * slot for the zero-terminator.
1170 */
1171 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001172 if (params.freqs) {
1173 params.freqs[0] = wpa_s->assoc_freq;
1174 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001175 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07001176
1177 /*
1178 * Reset the reattach flag so that we fall back to full scan if
1179 * this scan fails.
1180 */
1181 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 } else {
1183 struct wpa_ssid *start = ssid, *tssid;
1184 int freqs_set = 0;
1185 if (ssid == NULL && max_ssids > 1)
1186 ssid = wpa_s->conf->ssid;
1187 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001188 if (!wpas_network_disabled(wpa_s, ssid) &&
1189 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1191 ssid->ssid, ssid->ssid_len);
1192 params.ssids[params.num_ssids].ssid =
1193 ssid->ssid;
1194 params.ssids[params.num_ssids].ssid_len =
1195 ssid->ssid_len;
1196 params.num_ssids++;
1197 if (params.num_ssids + 1 >= max_ssids)
1198 break;
1199 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001200
1201 if (!wpas_network_disabled(wpa_s, ssid)) {
1202 /*
1203 * Also add the SSID of the OWE BSS, to allow
1204 * discovery of transition mode APs more
1205 * quickly.
1206 */
1207 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1208 max_ssids);
1209 }
1210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 ssid = ssid->next;
1212 if (ssid == start)
1213 break;
1214 if (ssid == NULL && max_ssids > 1 &&
1215 start != wpa_s->conf->ssid)
1216 ssid = wpa_s->conf->ssid;
1217 }
1218
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001219 if (wpa_s->scan_id_count &&
1220 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1221 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1222
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001223 for (tssid = wpa_s->conf->ssid;
1224 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1225 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001226 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07001228 if (((params.freqs || !freqs_set) &&
1229 tssid->scan_freq) &&
1230 int_array_len(params.freqs) < 100) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 int_array_concat(&params.freqs,
1232 tssid->scan_freq);
1233 } else {
1234 os_free(params.freqs);
1235 params.freqs = NULL;
1236 }
1237 freqs_set = 1;
1238 }
1239 int_array_sort_unique(params.freqs);
1240 }
1241
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001242 if (ssid && max_ssids == 1) {
1243 /*
1244 * If the driver is limited to 1 SSID at a time interleave
1245 * wildcard SSID scans with specific SSID scans to avoid
1246 * waiting a long time for a wildcard scan.
1247 */
1248 if (!wpa_s->prev_scan_wildcard) {
1249 params.ssids[0].ssid = NULL;
1250 params.ssids[0].ssid_len = 0;
1251 wpa_s->prev_scan_wildcard = 1;
1252 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1253 "wildcard SSID (Interleave with specific)");
1254 } else {
1255 wpa_s->prev_scan_ssid = ssid;
1256 wpa_s->prev_scan_wildcard = 0;
1257 wpa_dbg(wpa_s, MSG_DEBUG,
1258 "Starting AP scan for specific SSID: %s",
1259 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001261 } else if (ssid) {
1262 /* max_ssids > 1 */
1263
1264 wpa_s->prev_scan_ssid = ssid;
1265 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1266 "the scan request");
1267 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001268 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1269 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1270 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001271 } else if (wpa_s->conf->passive_scan) {
1272 wpa_dbg(wpa_s, MSG_DEBUG,
1273 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 } else {
1275 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1276 params.num_ssids++;
1277 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1278 "SSID");
1279 }
1280
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001281ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001282 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001283 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001285 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001286 wpa_s->manual_scan_only_new) {
1287 wpa_printf(MSG_DEBUG,
1288 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001289 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001290 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001291
1292 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1293 wpa_s->manual_scan_freqs) {
1294 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1295 params.freqs = wpa_s->manual_scan_freqs;
1296 wpa_s->manual_scan_freqs = NULL;
1297 }
1298
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001299 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1300 wpa_dbg(wpa_s, MSG_DEBUG,
1301 "Limit select_network scan to specified channels");
1302 params.freqs = wpa_s->select_network_scan_freqs;
1303 wpa_s->select_network_scan_freqs = NULL;
1304 }
1305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1307 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1308 "generated frequency list");
1309 params.freqs = wpa_s->next_scan_freqs;
1310 } else
1311 os_free(wpa_s->next_scan_freqs);
1312 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001313 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001315 /* See if user specified frequencies. If so, scan only those. */
Hai Shalom60840252021-02-19 19:02:11 -08001316 if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1317 wpa_s->conf->initial_freq_list && !params.freqs) {
1318 wpa_dbg(wpa_s, MSG_DEBUG,
1319 "Optimize scan based on conf->initial_freq_list");
1320 int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1321 } else if (wpa_s->conf->freq_list && !params.freqs) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001322 wpa_dbg(wpa_s, MSG_DEBUG,
1323 "Optimize scan based on conf->freq_list");
1324 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1325 }
1326
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001327 /* Use current associated channel? */
1328 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001329 unsigned int num = wpa_s->num_multichan_concurrent;
1330
1331 params.freqs = os_calloc(num + 1, sizeof(int));
1332 if (params.freqs) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001333 num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1334 false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001335 if (num > 0) {
1336 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1337 "current operating channels since "
1338 "scan_cur_freq is enabled");
1339 } else {
1340 os_free(params.freqs);
1341 params.freqs = NULL;
1342 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001343 }
1344 }
1345
Hai Shalomce48b4a2018-09-05 11:41:35 -07001346#ifdef CONFIG_MBO
1347 if (wpa_s->enable_oce & OCE_STA)
1348 params.oce_scan = 1;
1349#endif /* CONFIG_MBO */
1350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1352 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001353 if (extra_ie) {
1354 params.extra_ies = wpabuf_head(extra_ie);
1355 params.extra_ies_len = wpabuf_len(extra_ie);
1356 }
1357
1358#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001359 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001360 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001361 /*
1362 * The interface may not yet be in P2P mode, so we have to
1363 * explicitly request P2P probe to disable CCK rates.
1364 */
1365 params.p2p_probe = 1;
1366 }
1367#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001369 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001370 wpa_s->wpa_state <= WPA_SCANNING)
1371 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001372
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001373 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1374 struct wpa_bss *bss;
1375
1376 params.bssid = wpa_s->next_scan_bssid;
1377 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
Hai Shalomfdcde762020-04-02 11:19:20 -07001378 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1379 bss && bss->ssid_len && params.num_ssids == 1 &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001380 params.ssids[0].ssid_len == 0) {
1381 params.ssids[0].ssid = bss->ssid;
1382 params.ssids[0].ssid_len = bss->ssid_len;
1383 wpa_dbg(wpa_s, MSG_DEBUG,
1384 "Scan a previously specified BSSID " MACSTR
1385 " and SSID %s",
1386 MAC2STR(params.bssid),
1387 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1388 } else {
1389 wpa_dbg(wpa_s, MSG_DEBUG,
1390 "Scan a previously specified BSSID " MACSTR,
1391 MAC2STR(params.bssid));
1392 }
1393 }
1394
Sunil8cd6f4d2022-06-28 18:40:46 +00001395 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1396 wpa_s->manual_non_coloc_6ghz) {
1397 wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1398 params.non_coloc_6ghz = 1;
1399 }
1400
Dmitry Shmidt04949592012-07-19 12:16:46 -07001401 scan_params = &params;
1402
1403scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001404#ifdef CONFIG_P2P
1405 /*
1406 * If the driver does not support multi-channel concurrency and a
1407 * virtual interface that shares the same radio with the wpa_s interface
1408 * is operating there may not be need to scan other channels apart from
1409 * the current operating channel on the other virtual interface. Filter
1410 * out other channels in case we are trying to find a connection for a
1411 * station interface when we are not configured to prefer station
1412 * connection and a concurrent operation is already in process.
1413 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001414 if (wpa_s->scan_for_connection &&
1415 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001416 !scan_params->freqs && !params.freqs &&
1417 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001418 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1419 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001420 unsigned int num = wpa_s->num_multichan_concurrent;
1421
1422 params.freqs = os_calloc(num + 1, sizeof(int));
1423 if (params.freqs) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001424 /*
1425 * Exclude the operating frequency of the current
1426 * interface since we're looking to transition off of
1427 * it.
1428 */
1429 num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1430 true);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001431 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1432 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1433 } else {
1434 os_free(params.freqs);
1435 params.freqs = NULL;
1436 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001437 }
1438 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001439
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001440 if (!params.freqs && is_6ghz_supported(wpa_s) &&
1441 (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning))
1442 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001443#endif /* CONFIG_P2P */
1444
Dmitry Shmidt04949592012-07-19 12:16:46 -07001445 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001447 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1448 !wpa_s->manual_scan_freqs) {
1449 /* Restore manual_scan_freqs for the next attempt */
1450 wpa_s->manual_scan_freqs = params.freqs;
1451 params.freqs = NULL;
1452 }
1453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001454 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 os_free(params.freqs);
1456 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001457 os_free(params.mac_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458
1459 if (ret) {
1460 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001461 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1462 wpa_supplicant_set_state(wpa_s,
1463 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001464 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001465 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001467 } else {
1468 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001469#ifdef CONFIG_INTERWORKING
1470 wpa_s->interworking_fast_assoc_tried = 0;
1471#endif /* CONFIG_INTERWORKING */
Hai Shalomfdcde762020-04-02 11:19:20 -07001472 wpa_s->next_scan_bssid_wildcard_ssid = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001473 if (params.bssid)
1474 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 }
1476}
1477
1478
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001479void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1480{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001481 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001482 int cancelled;
1483
1484 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1485 &remaining);
1486
1487 new_int.sec = sec;
1488 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001489 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001490 new_int.sec = remaining.sec;
1491 new_int.usec = remaining.usec;
1492 }
1493
Dmitry Shmidt051af732013-10-22 13:52:46 -07001494 if (cancelled) {
1495 eloop_register_timeout(new_int.sec, new_int.usec,
1496 wpa_supplicant_scan, wpa_s, NULL);
1497 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001498 wpa_s->scan_interval = sec;
1499}
1500
1501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502/**
1503 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1504 * @wpa_s: Pointer to wpa_supplicant data
1505 * @sec: Number of seconds after which to scan
1506 * @usec: Number of microseconds after which to scan
1507 *
1508 * This function is used to schedule a scan for neighboring access points after
1509 * the specified time.
1510 */
1511void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1512{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001513 int res;
1514
1515 if (wpa_s->p2p_mgmt) {
1516 wpa_dbg(wpa_s, MSG_DEBUG,
1517 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1518 sec, usec);
1519 return;
1520 }
1521
1522 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1523 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524 if (res == 1) {
1525 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001526 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001527 } else if (res == 0) {
1528 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1529 sec, usec);
1530 } else {
1531 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1532 sec, usec);
1533 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535}
1536
1537
1538/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001539 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1540 * @wpa_s: Pointer to wpa_supplicant data
1541 * @sec: Number of seconds after which to scan
1542 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001543 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001544 *
1545 * This function is used to schedule periodic scans for neighboring
1546 * access points after the specified time.
1547 */
1548int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1549 int sec, int usec)
1550{
1551 if (!wpa_s->sched_scan_supported)
1552 return -1;
1553
1554 eloop_register_timeout(sec, usec,
1555 wpa_supplicant_delayed_sched_scan_timeout,
1556 wpa_s, NULL);
1557
1558 return 0;
1559}
1560
1561
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001562static void
1563wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1564 struct wpa_driver_scan_params *params)
1565{
1566 if (wpa_s->wpa_state != WPA_COMPLETED ||
1567 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1568 wpa_s->srp.relative_rssi_set == 0)
1569 return;
1570
1571 params->relative_rssi_set = 1;
1572 params->relative_rssi = wpa_s->srp.relative_rssi;
1573
1574 if (wpa_s->srp.relative_adjust_rssi == 0)
1575 return;
1576
1577 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1578 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1579}
1580
1581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001582/**
1583 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1584 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001585 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001586 *
1587 * This function is used to schedule periodic scans for neighboring
1588 * access points repeating the scan continuously.
1589 */
1590int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1591{
1592 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001593 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001594 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001595 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001596 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001597 int ret;
1598 unsigned int max_sched_scan_ssids;
1599 int wildcard = 0;
1600 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001601 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001602
1603 if (!wpa_s->sched_scan_supported)
1604 return -1;
1605
1606 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1607 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1608 else
1609 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001610 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001611 return -1;
1612
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001613 wpa_s->sched_scan_stop_req = 0;
1614
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001615 if (wpa_s->sched_scanning) {
1616 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1617 return 0;
1618 }
1619
1620 need_ssids = 0;
1621 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001622 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001623 /* Use wildcard SSID to find this network */
1624 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001625 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1626 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001627 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001628
1629#ifdef CONFIG_WPS
1630 if (!wpas_network_disabled(wpa_s, ssid) &&
1631 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1632 /*
1633 * Normal scan is more reliable and faster for WPS
1634 * operations and since these are for short periods of
1635 * time, the benefit of trying to use sched_scan would
1636 * be limited.
1637 */
1638 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1639 "sched_scan for WPS");
1640 return -1;
1641 }
1642#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001643 }
1644 if (wildcard)
1645 need_ssids++;
1646
1647 if (wpa_s->normal_scans < 3 &&
1648 (need_ssids <= wpa_s->max_scan_ssids ||
1649 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1650 /*
1651 * When normal scan can speed up operations, use that for the
1652 * first operations before starting the sched_scan to allow
1653 * user space sleep more. We do this only if the normal scan
1654 * has functionality that is suitable for this or if the
1655 * sched_scan does not have better support for multiple SSIDs.
1656 */
1657 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1658 "sched_scan for initial scans (normal_scans=%d)",
1659 wpa_s->normal_scans);
1660 return -1;
1661 }
1662
1663 os_memset(&params, 0, sizeof(params));
1664
1665 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001666 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001667 sizeof(struct wpa_driver_scan_filter));
1668
1669 prev_state = wpa_s->wpa_state;
1670 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1671 wpa_s->wpa_state == WPA_INACTIVE)
1672 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1673
Dmitry Shmidt04949592012-07-19 12:16:46 -07001674 if (wpa_s->autoscan_params != NULL) {
1675 scan_params = wpa_s->autoscan_params;
1676 goto scan;
1677 }
1678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001679 /* Find the starting point from which to continue scanning */
1680 ssid = wpa_s->conf->ssid;
1681 if (wpa_s->prev_sched_ssid) {
1682 while (ssid) {
1683 if (ssid == wpa_s->prev_sched_ssid) {
1684 ssid = ssid->next;
1685 break;
1686 }
1687 ssid = ssid->next;
1688 }
1689 }
1690
1691 if (!ssid || !wpa_s->prev_sched_ssid) {
1692 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001693 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1694 wpa_s->first_sched_scan = 1;
1695 ssid = wpa_s->conf->ssid;
1696 wpa_s->prev_sched_ssid = ssid;
1697 }
1698
1699 if (wildcard) {
1700 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1701 params.num_ssids++;
1702 }
1703
1704 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001705 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001706 goto next;
1707
1708 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1709 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1710 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1711 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1712 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1713 ssid->ssid, ssid->ssid_len);
1714 params.filter_ssids[params.num_filter_ssids].ssid_len =
1715 ssid->ssid_len;
1716 params.num_filter_ssids++;
1717 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1718 {
1719 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1720 "filter for sched_scan - drop filter");
1721 os_free(params.filter_ssids);
1722 params.filter_ssids = NULL;
1723 params.num_filter_ssids = 0;
1724 }
1725
1726 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1727 if (params.num_ssids == max_sched_scan_ssids)
1728 break; /* only room for broadcast SSID */
1729 wpa_dbg(wpa_s, MSG_DEBUG,
1730 "add to active scan ssid: %s",
1731 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1732 params.ssids[params.num_ssids].ssid =
1733 ssid->ssid;
1734 params.ssids[params.num_ssids].ssid_len =
1735 ssid->ssid_len;
1736 params.num_ssids++;
1737 if (params.num_ssids >= max_sched_scan_ssids) {
1738 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001739 do {
1740 ssid = ssid->next;
1741 } while (ssid &&
1742 (wpas_network_disabled(wpa_s, ssid) ||
1743 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001744 break;
1745 }
1746 }
1747
1748 next:
1749 wpa_s->prev_sched_ssid = ssid;
1750 ssid = ssid->next;
1751 }
1752
1753 if (params.num_filter_ssids == 0) {
1754 os_free(params.filter_ssids);
1755 params.filter_ssids = NULL;
1756 }
1757
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001758 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1759 if (extra_ie) {
1760 params.extra_ies = wpabuf_head(extra_ie);
1761 params.extra_ies_len = wpabuf_len(extra_ie);
1762 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001763
Dmitry Shmidt18463232014-01-24 12:29:41 -08001764 if (wpa_s->conf->filter_rssi)
1765 params.filter_rssi = wpa_s->conf->filter_rssi;
1766
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001767 /* See if user specified frequencies. If so, scan only those. */
1768 if (wpa_s->conf->freq_list && !params.freqs) {
1769 wpa_dbg(wpa_s, MSG_DEBUG,
1770 "Optimize scan based on conf->freq_list");
1771 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1772 }
1773
Hai Shalomce48b4a2018-09-05 11:41:35 -07001774#ifdef CONFIG_MBO
1775 if (wpa_s->enable_oce & OCE_STA)
1776 params.oce_scan = 1;
1777#endif /* CONFIG_MBO */
1778
Dmitry Shmidt04949592012-07-19 12:16:46 -07001779 scan_params = &params;
1780
1781scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001782 wpa_s->sched_scan_timed_out = 0;
1783
1784 /*
1785 * We cannot support multiple scan plans if the scan request includes
1786 * too many SSID's, so in this case use only the last scan plan and make
1787 * it run infinitely. It will be stopped by the timeout.
1788 */
1789 if (wpa_s->sched_scan_plans_num == 1 ||
1790 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1791 params.sched_scan_plans = wpa_s->sched_scan_plans;
1792 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1793 } else if (wpa_s->sched_scan_plans_num > 1) {
1794 wpa_dbg(wpa_s, MSG_DEBUG,
1795 "Too many SSIDs. Default to using single scheduled_scan plan");
1796 params.sched_scan_plans =
1797 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1798 1];
1799 params.sched_scan_plans_num = 1;
1800 } else {
1801 if (wpa_s->conf->sched_scan_interval)
1802 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1803 else
1804 scan_plan.interval = 10;
1805
1806 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1807 wpa_printf(MSG_WARNING,
1808 "Scan interval too long(%u), use the maximum allowed(%u)",
1809 scan_plan.interval,
1810 wpa_s->max_sched_scan_plan_interval);
1811 scan_plan.interval =
1812 wpa_s->max_sched_scan_plan_interval;
1813 }
1814
1815 scan_plan.iterations = 0;
1816 params.sched_scan_plans = &scan_plan;
1817 params.sched_scan_plans_num = 1;
1818 }
1819
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001820 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1821
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001822 if (ssid || !wpa_s->first_sched_scan) {
1823 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001824 "Starting sched scan after %u seconds: interval %u timeout %d",
1825 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001826 params.sched_scan_plans[0].interval,
1827 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001828 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001829 wpa_dbg(wpa_s, MSG_DEBUG,
1830 "Starting sched scan after %u seconds (no timeout)",
1831 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832 }
1833
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001834 wpa_setband_scan_freqs(wpa_s, scan_params);
1835
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001836 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001837 wpa_s->wpa_state <= WPA_SCANNING)
1838 wpa_setup_mac_addr_rand_params(&params,
1839 wpa_s->mac_addr_sched_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001840
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001841 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1842
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001843 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001844 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001845 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001846 os_free(params.mac_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001847 if (ret) {
1848 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1849 if (prev_state != wpa_s->wpa_state)
1850 wpa_supplicant_set_state(wpa_s, prev_state);
1851 return ret;
1852 }
1853
1854 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1855 if (ssid || !wpa_s->first_sched_scan) {
1856 wpa_s->sched_scan_timed_out = 0;
1857 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1858 wpa_supplicant_sched_scan_timeout,
1859 wpa_s, NULL);
1860 wpa_s->first_sched_scan = 0;
1861 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001862 params.sched_scan_plans[0].interval *= 2;
1863 if ((unsigned int) wpa_s->sched_scan_timeout <
1864 params.sched_scan_plans[0].interval ||
1865 params.sched_scan_plans[0].interval >
1866 wpa_s->max_sched_scan_plan_interval) {
1867 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001868 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1869 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001870 }
1871
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001872 /* If there is no more ssids, start next time from the beginning */
1873 if (!ssid)
1874 wpa_s->prev_sched_ssid = NULL;
1875
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001876 return 0;
1877}
1878
1879
1880/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1882 * @wpa_s: Pointer to wpa_supplicant data
1883 *
1884 * This function is used to cancel a scan request scheduled with
1885 * wpa_supplicant_req_scan().
1886 */
1887void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1888{
1889 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1890 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891}
1892
1893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001894/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001895 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1896 * @wpa_s: Pointer to wpa_supplicant data
1897 *
1898 * This function is used to stop a delayed scheduled scan.
1899 */
1900void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1901{
1902 if (!wpa_s->sched_scan_supported)
1903 return;
1904
1905 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1906 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1907 wpa_s, NULL);
1908}
1909
1910
1911/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001912 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1913 * @wpa_s: Pointer to wpa_supplicant data
1914 *
1915 * This function is used to stop a periodic scheduled scan.
1916 */
1917void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1918{
1919 if (!wpa_s->sched_scanning)
1920 return;
1921
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001922 if (wpa_s->sched_scanning)
1923 wpa_s->sched_scan_stop_req = 1;
1924
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001925 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1926 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1927 wpa_supplicant_stop_sched_scan(wpa_s);
1928}
1929
1930
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001931/**
1932 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1933 * @wpa_s: Pointer to wpa_supplicant data
1934 * @scanning: Whether scanning is currently in progress
1935 *
1936 * This function is to generate scanning notifycations. It is called whenever
1937 * there may have been a change in scanning (scan started, completed, stopped).
1938 * wpas_notify_scanning() is called whenever the scanning state changed from the
1939 * previously notified state.
1940 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1942 int scanning)
1943{
1944 if (wpa_s->scanning != scanning) {
1945 wpa_s->scanning = scanning;
1946 wpas_notify_scanning(wpa_s);
1947 }
1948}
1949
1950
1951static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1952{
1953 int rate = 0;
1954 const u8 *ie;
1955 int i;
1956
1957 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1958 for (i = 0; ie && i < ie[1]; i++) {
1959 if ((ie[i + 2] & 0x7f) > rate)
1960 rate = ie[i + 2] & 0x7f;
1961 }
1962
1963 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1964 for (i = 0; ie && i < ie[1]; i++) {
1965 if ((ie[i + 2] & 0x7f) > rate)
1966 rate = ie[i + 2] & 0x7f;
1967 }
1968
1969 return rate;
1970}
1971
1972
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001973/**
1974 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1975 * @res: Scan result entry
1976 * @ie: Information element identitifier (WLAN_EID_*)
1977 * Returns: Pointer to the information element (id field) or %NULL if not found
1978 *
1979 * This function returns the first matching information element in the scan
1980 * result.
1981 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1983{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001984 size_t ie_len = res->ie_len;
1985
1986 /* Use the Beacon frame IEs if res->ie_len is not available */
1987 if (!ie_len)
1988 ie_len = res->beacon_ie_len;
1989
1990 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991}
1992
1993
Sunil Ravi89eba102022-09-13 21:04:37 -07001994const u8 * wpa_scan_get_ml_ie(const struct wpa_scan_res *res, u8 type)
1995{
1996 size_t ie_len = res->ie_len;
1997
1998 /* Use the Beacon frame IEs if res->ie_len is not available */
1999 if (!ie_len)
2000 ie_len = res->beacon_ie_len;
2001
2002 return get_ml_ie((const u8 *) (res + 1), ie_len, type);
2003}
2004
2005
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002006/**
2007 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
2008 * @res: Scan result entry
2009 * @vendor_type: Vendor type (four octets starting the IE payload)
2010 * Returns: Pointer to the information element (id field) or %NULL if not found
2011 *
2012 * This function returns the first matching information element in the scan
2013 * result.
2014 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
2016 u32 vendor_type)
2017{
Hai Shalom60840252021-02-19 19:02:11 -08002018 const u8 *ies;
2019 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020
Hai Shalom60840252021-02-19 19:02:11 -08002021 ies = (const u8 *) (res + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022
Hai Shalom60840252021-02-19 19:02:11 -08002023 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
2024 if (elem->datalen >= 4 &&
2025 vendor_type == WPA_GET_BE32(elem->data))
2026 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 }
2028
2029 return NULL;
2030}
2031
2032
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002033/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07002034 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
2035 * @res: Scan result entry
2036 * @vendor_type: Vendor type (four octets starting the IE payload)
2037 * Returns: Pointer to the information element (id field) or %NULL if not found
2038 *
2039 * This function returns the first matching information element in the scan
2040 * result.
2041 *
2042 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
2043 * from Beacon frames instead of either Beacon or Probe Response frames.
2044 */
2045const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
2046 u32 vendor_type)
2047{
Hai Shalom60840252021-02-19 19:02:11 -08002048 const u8 *ies;
2049 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002050
2051 if (res->beacon_ie_len == 0)
2052 return NULL;
2053
Hai Shalom60840252021-02-19 19:02:11 -08002054 ies = (const u8 *) (res + 1);
2055 ies += res->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002056
Hai Shalom60840252021-02-19 19:02:11 -08002057 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
2058 res->beacon_ie_len) {
2059 if (elem->datalen >= 4 &&
2060 vendor_type == WPA_GET_BE32(elem->data))
2061 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002062 }
2063
2064 return NULL;
2065}
2066
2067
2068/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002069 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2070 * @res: Scan result entry
2071 * @vendor_type: Vendor type (four octets starting the IE payload)
2072 * Returns: Pointer to the information element payload or %NULL if not found
2073 *
2074 * This function returns concatenated payload of possibly fragmented vendor
2075 * specific information elements in the scan result. The caller is responsible
2076 * for freeing the returned buffer.
2077 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2079 u32 vendor_type)
2080{
2081 struct wpabuf *buf;
2082 const u8 *end, *pos;
2083
2084 buf = wpabuf_alloc(res->ie_len);
2085 if (buf == NULL)
2086 return NULL;
2087
2088 pos = (const u8 *) (res + 1);
2089 end = pos + res->ie_len;
2090
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002091 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08002092 u8 ie, len;
2093
2094 ie = pos[0];
2095 len = pos[1];
2096 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 break;
Hai Shalom60840252021-02-19 19:02:11 -08002098 pos += 2;
2099 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2100 vendor_type == WPA_GET_BE32(pos))
2101 wpabuf_put_data(buf, pos + 4, len - 4);
2102 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 }
2104
2105 if (wpabuf_len(buf) == 0) {
2106 wpabuf_free(buf);
2107 buf = NULL;
2108 }
2109
2110 return buf;
2111}
2112
2113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114/* Compare function for sorting scan results. Return >0 if @b is considered
2115 * better. */
2116static int wpa_scan_result_compar(const void *a, const void *b)
2117{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 struct wpa_scan_res **_wa = (void *) a;
2120 struct wpa_scan_res **_wb = (void *) b;
2121 struct wpa_scan_res *wa = *_wa;
2122 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002123 int wpa_a, wpa_b;
2124 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
2126 /* WPA/WPA2 support preferred */
2127 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2128 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2129 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2130 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2131
2132 if (wpa_b && !wpa_a)
2133 return 1;
2134 if (!wpa_b && wpa_a)
2135 return -1;
2136
2137 /* privacy support preferred */
2138 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2139 (wb->caps & IEEE80211_CAP_PRIVACY))
2140 return 1;
2141 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2142 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2143 return -1;
2144
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002145 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002146 snr_a_full = wa->snr;
2147 snr_a = MIN(wa->snr, GREAT_SNR);
2148 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002149 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002150 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002151 /* Level is not in dBm, so we can't calculate
2152 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002153 snr_a = snr_a_full = wa->level;
2154 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002155 }
2156
Hai Shaloma20dcd72022-02-04 13:43:00 -08002157 /* If SNR is close, decide by max rate or frequency band. For cases
2158 * involving the 6 GHz band, use the throughput estimate irrespective
2159 * of the SNR difference since the LPI/VLP rules may result in
2160 * significant differences in SNR for cases where the estimated
2161 * throughput can be considerably higher with the lower SNR. */
2162 if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2163 is_6ghz_freq(wa->freq) ||
2164 is_6ghz_freq(wb->freq))) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002165 if (wa->est_throughput != wb->est_throughput)
Hai Shalom021b0b52019-04-10 11:17:58 -07002166 return (int) wb->est_throughput -
2167 (int) wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002168 }
2169 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2170 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002171 if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2172 return is_6ghz_freq(wa->freq) ? -1 : 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002173 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2174 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175 }
2176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002177 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 * identical, use quality values since some drivers may only report
2179 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002180 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002182 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002183#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184}
2185
2186
2187#ifdef CONFIG_WPS
2188/* Compare function for sorting scan results when searching a WPS AP for
2189 * provisioning. Return >0 if @b is considered better. */
2190static int wpa_scan_result_wps_compar(const void *a, const void *b)
2191{
2192 struct wpa_scan_res **_wa = (void *) a;
2193 struct wpa_scan_res **_wb = (void *) b;
2194 struct wpa_scan_res *wa = *_wa;
2195 struct wpa_scan_res *wb = *_wb;
2196 int uses_wps_a, uses_wps_b;
2197 struct wpabuf *wps_a, *wps_b;
2198 int res;
2199
2200 /* Optimization - check WPS IE existence before allocated memory and
2201 * doing full reassembly. */
2202 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2203 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2204 if (uses_wps_a && !uses_wps_b)
2205 return -1;
2206 if (!uses_wps_a && uses_wps_b)
2207 return 1;
2208
2209 if (uses_wps_a && uses_wps_b) {
2210 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2211 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2212 res = wps_ap_priority_compar(wps_a, wps_b);
2213 wpabuf_free(wps_a);
2214 wpabuf_free(wps_b);
2215 if (res)
2216 return res;
2217 }
2218
2219 /*
2220 * Do not use current AP security policy as a sorting criteria during
2221 * WPS provisioning step since the AP may get reconfigured at the
2222 * completion of provisioning.
2223 */
2224
2225 /* all things being equal, use signal level; if signal levels are
2226 * identical, use quality values since some drivers may only report
2227 * that value and leave the signal level zero */
2228 if (wb->level == wa->level)
2229 return wb->qual - wa->qual;
2230 return wb->level - wa->level;
2231}
2232#endif /* CONFIG_WPS */
2233
2234
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002235static void dump_scan_res(struct wpa_scan_results *scan_res)
2236{
2237#ifndef CONFIG_NO_STDOUT_DEBUG
2238 size_t i;
2239
2240 if (scan_res->res == NULL || scan_res->num == 0)
2241 return;
2242
2243 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2244
2245 for (i = 0; i < scan_res->num; i++) {
2246 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002247 u8 *pos;
Sunil Ravia04bd252022-05-02 22:54:18 -07002248 const u8 *ssid_ie, *ssid = NULL;
2249 size_t ssid_len = 0;
2250
2251 ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2252 if (ssid_ie) {
2253 ssid = ssid_ie + 2;
2254 ssid_len = ssid_ie[1];
2255 }
2256
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002257 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002258 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2259
Sunil Ravia04bd252022-05-02 22:54:18 -07002260 wpa_printf(MSG_EXCESSIVE, MACSTR
2261 " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2262 MAC2STR(r->bssid),
2263 wpa_ssid_txt(ssid, ssid_len),
2264 r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002265 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002266 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2267 r->flags,
2268 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002269 } else {
Sunil Ravia04bd252022-05-02 22:54:18 -07002270 wpa_printf(MSG_EXCESSIVE, MACSTR
2271 " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2272 MAC2STR(r->bssid),
2273 wpa_ssid_txt(ssid, ssid_len),
2274 r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002275 r->noise, r->level, r->flags, r->age,
2276 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002277 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002278 pos = (u8 *) (r + 1);
2279 if (r->ie_len)
2280 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2281 pos += r->ie_len;
2282 if (r->beacon_ie_len)
2283 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2284 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002285 }
2286#endif /* CONFIG_NO_STDOUT_DEBUG */
2287}
2288
2289
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002290/**
2291 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2292 * @wpa_s: Pointer to wpa_supplicant data
2293 * @bssid: BSSID to check
2294 * Returns: 0 if the BSSID is filtered or 1 if not
2295 *
2296 * This function is used to filter out specific BSSIDs from scan reslts mainly
2297 * for testing purposes (SET bssid_filter ctrl_iface command).
2298 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002299int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2300 const u8 *bssid)
2301{
2302 size_t i;
2303
2304 if (wpa_s->bssid_filter == NULL)
2305 return 1;
2306
2307 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2308 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2309 ETH_ALEN) == 0)
2310 return 1;
2311 }
2312
2313 return 0;
2314}
2315
2316
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002317void filter_scan_res(struct wpa_supplicant *wpa_s,
2318 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002319{
2320 size_t i, j;
2321
2322 if (wpa_s->bssid_filter == NULL)
2323 return;
2324
2325 for (i = 0, j = 0; i < res->num; i++) {
2326 if (wpa_supplicant_filter_bssid_match(wpa_s,
2327 res->res[i]->bssid)) {
2328 res->res[j++] = res->res[i];
2329 } else {
2330 os_free(res->res[i]);
2331 res->res[i] = NULL;
2332 }
2333 }
2334
2335 if (res->num != j) {
2336 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2337 (int) (res->num - j));
2338 res->num = j;
2339 }
2340}
2341
2342
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002343void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002344{
2345 if (res->flags & WPA_SCAN_NOISE_INVALID) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002346 res->noise = is_6ghz_freq(res->freq) ?
2347 DEFAULT_NOISE_FLOOR_6GHZ :
2348 (IS_5GHZ(res->freq) ?
2349 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002350 }
2351
2352 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2353 res->snr = res->level - res->noise;
2354 } else {
2355 /* Level is not in dBm, so we can't calculate
2356 * SNR. Just use raw level (units unknown). */
2357 res->snr = res->level;
2358 }
2359}
2360
2361
Hai Shalom899fcc72020-10-19 14:38:18 -07002362/* Minimum SNR required to achieve a certain bitrate. */
2363struct minsnr_bitrate_entry {
2364 int minsnr;
2365 unsigned int bitrate; /* in Mbps */
2366};
2367
2368/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2369static const int vht_mcs = 8;
2370
2371static const struct minsnr_bitrate_entry vht20_table[] = {
2372 { 0, 0 },
2373 { 2, 6500 }, /* HT20 MCS0 */
2374 { 5, 13000 }, /* HT20 MCS1 */
2375 { 9, 19500 }, /* HT20 MCS2 */
2376 { 11, 26000 }, /* HT20 MCS3 */
2377 { 15, 39000 }, /* HT20 MCS4 */
2378 { 18, 52000 }, /* HT20 MCS5 */
2379 { 20, 58500 }, /* HT20 MCS6 */
2380 { 25, 65000 }, /* HT20 MCS7 */
2381 { 29, 78000 }, /* VHT20 MCS8 */
2382 { -1, 78000 } /* SNR > 29 */
2383};
2384
2385static const struct minsnr_bitrate_entry vht40_table[] = {
2386 { 0, 0 },
2387 { 5, 13500 }, /* HT40 MCS0 */
2388 { 8, 27000 }, /* HT40 MCS1 */
2389 { 12, 40500 }, /* HT40 MCS2 */
2390 { 14, 54000 }, /* HT40 MCS3 */
2391 { 18, 81000 }, /* HT40 MCS4 */
2392 { 21, 108000 }, /* HT40 MCS5 */
2393 { 23, 121500 }, /* HT40 MCS6 */
2394 { 28, 135000 }, /* HT40 MCS7 */
2395 { 32, 162000 }, /* VHT40 MCS8 */
2396 { 34, 180000 }, /* VHT40 MCS9 */
2397 { -1, 180000 } /* SNR > 34 */
2398};
2399
2400static const struct minsnr_bitrate_entry vht80_table[] = {
2401 { 0, 0 },
2402 { 8, 29300 }, /* VHT80 MCS0 */
2403 { 11, 58500 }, /* VHT80 MCS1 */
2404 { 15, 87800 }, /* VHT80 MCS2 */
2405 { 17, 117000 }, /* VHT80 MCS3 */
2406 { 21, 175500 }, /* VHT80 MCS4 */
2407 { 24, 234000 }, /* VHT80 MCS5 */
2408 { 26, 263300 }, /* VHT80 MCS6 */
2409 { 31, 292500 }, /* VHT80 MCS7 */
2410 { 35, 351000 }, /* VHT80 MCS8 */
2411 { 37, 390000 }, /* VHT80 MCS9 */
2412 { -1, 390000 } /* SNR > 37 */
2413};
2414
2415
Hai Shaloma20dcd72022-02-04 13:43:00 -08002416static const struct minsnr_bitrate_entry vht160_table[] = {
2417 { 0, 0 },
2418 { 11, 58500 }, /* VHT160 MCS0 */
2419 { 14, 117000 }, /* VHT160 MCS1 */
2420 { 18, 175500 }, /* VHT160 MCS2 */
2421 { 20, 234000 }, /* VHT160 MCS3 */
2422 { 24, 351000 }, /* VHT160 MCS4 */
2423 { 27, 468000 }, /* VHT160 MCS5 */
2424 { 29, 526500 }, /* VHT160 MCS6 */
2425 { 34, 585000 }, /* VHT160 MCS7 */
2426 { 38, 702000 }, /* VHT160 MCS8 */
2427 { 40, 780000 }, /* VHT160 MCS9 */
2428 { -1, 780000 } /* SNR > 37 */
2429};
2430
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002431/* EHT needs to be enabled in order to achieve MCS12 and MCS13 rates. */
2432#define EHT_MCS 12
Hai Shaloma20dcd72022-02-04 13:43:00 -08002433
2434static const struct minsnr_bitrate_entry he20_table[] = {
2435 { 0, 0 },
2436 { 2, 8600 }, /* HE20 MCS0 */
2437 { 5, 17200 }, /* HE20 MCS1 */
2438 { 9, 25800 }, /* HE20 MCS2 */
2439 { 11, 34400 }, /* HE20 MCS3 */
2440 { 15, 51600 }, /* HE20 MCS4 */
2441 { 18, 68800 }, /* HE20 MCS5 */
2442 { 20, 77400 }, /* HE20 MCS6 */
2443 { 25, 86000 }, /* HE20 MCS7 */
2444 { 29, 103200 }, /* HE20 MCS8 */
2445 { 31, 114700 }, /* HE20 MCS9 */
2446 { 34, 129000 }, /* HE20 MCS10 */
2447 { 36, 143400 }, /* HE20 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002448 { 39, 154900 }, /* EHT20 MCS12 */
2449 { 42, 172100 }, /* EHT20 MCS13 */
2450 { -1, 172100 } /* SNR > 42 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002451};
2452
2453static const struct minsnr_bitrate_entry he40_table[] = {
2454 { 0, 0 },
2455 { 5, 17200 }, /* HE40 MCS0 */
2456 { 8, 34400 }, /* HE40 MCS1 */
2457 { 12, 51600 }, /* HE40 MCS2 */
2458 { 14, 68800 }, /* HE40 MCS3 */
2459 { 18, 103200 }, /* HE40 MCS4 */
2460 { 21, 137600 }, /* HE40 MCS5 */
2461 { 23, 154900 }, /* HE40 MCS6 */
2462 { 28, 172100 }, /* HE40 MCS7 */
2463 { 32, 206500 }, /* HE40 MCS8 */
2464 { 34, 229400 }, /* HE40 MCS9 */
2465 { 37, 258100 }, /* HE40 MCS10 */
2466 { 39, 286800 }, /* HE40 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002467 { 42, 309500 }, /* EHT40 MCS12 */
2468 { 45, 344100 }, /* EHT40 MCS13 */
2469 { -1, 344100 } /* SNR > 45 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002470};
2471
2472static const struct minsnr_bitrate_entry he80_table[] = {
2473 { 0, 0 },
2474 { 8, 36000 }, /* HE80 MCS0 */
2475 { 11, 72100 }, /* HE80 MCS1 */
2476 { 15, 108100 }, /* HE80 MCS2 */
2477 { 17, 144100 }, /* HE80 MCS3 */
2478 { 21, 216200 }, /* HE80 MCS4 */
2479 { 24, 288200 }, /* HE80 MCS5 */
2480 { 26, 324300 }, /* HE80 MCS6 */
2481 { 31, 360300 }, /* HE80 MCS7 */
2482 { 35, 432400 }, /* HE80 MCS8 */
2483 { 37, 480400 }, /* HE80 MCS9 */
2484 { 40, 540400 }, /* HE80 MCS10 */
2485 { 42, 600500 }, /* HE80 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002486 { 45, 648500 }, /* EHT80 MCS12 */
2487 { 48, 720600 }, /* EHT80 MCS13 */
2488 { -1, 720600 } /* SNR > 48 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002489};
2490
2491
2492static const struct minsnr_bitrate_entry he160_table[] = {
2493 { 0, 0 },
2494 { 11, 72100 }, /* HE160 MCS0 */
2495 { 14, 144100 }, /* HE160 MCS1 */
2496 { 18, 216200 }, /* HE160 MCS2 */
2497 { 20, 288200 }, /* HE160 MCS3 */
2498 { 24, 432400 }, /* HE160 MCS4 */
2499 { 27, 576500 }, /* HE160 MCS5 */
2500 { 29, 648500 }, /* HE160 MCS6 */
2501 { 34, 720600 }, /* HE160 MCS7 */
2502 { 38, 864700 }, /* HE160 MCS8 */
2503 { 40, 960800 }, /* HE160 MCS9 */
2504 { 43, 1080900 }, /* HE160 MCS10 */
2505 { 45, 1201000 }, /* HE160 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002506 { 48, 1297100 }, /* EHT160 MCS12 */
2507 { 51, 1441200 }, /* EHT160 MCS13 */
2508 { -1, 1441200 } /* SNR > 51 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002509};
2510
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002511/* See IEEE P802.11be/D2.0, Table 36-86: EHT-MCSs for 4x996-tone RU, NSS,u = 1
2512 */
2513static const struct minsnr_bitrate_entry eht320_table[] = {
2514 { 0, 0 },
2515 { 14, 144100 }, /* EHT320 MCS0 */
2516 { 17, 288200 }, /* EHT320 MCS1 */
2517 { 21, 432400 }, /* EHT320 MCS2 */
2518 { 23, 576500 }, /* EHT320 MCS3 */
2519 { 27, 864700 }, /* EHT320 MCS4 */
2520 { 30, 1152900 }, /* EHT320 MCS5 */
2521 { 32, 1297100 }, /* EHT320 MCS6 */
2522 { 37, 1441200 }, /* EHT320 MCS7 */
2523 { 41, 1729400 }, /* EHT320 MCS8 */
2524 { 43, 1921500 }, /* EHT320 MCS9 */
2525 { 46, 2161800 }, /* EHT320 MCS10 */
2526 { 48, 2401900 }, /* EHT320 MCS11 */
2527 { 51, 2594100 }, /* EHT320 MCS12 */
2528 { 54, 2882400 }, /* EHT320 MCS13 */
2529 { -1, 2882400 } /* SNR > 54 */
2530};
Hai Shaloma20dcd72022-02-04 13:43:00 -08002531
Hai Shalomfdcde762020-04-02 11:19:20 -07002532static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2533 int rate0, int rate1)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002534{
Hai Shalomfdcde762020-04-02 11:19:20 -07002535 return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002536}
2537
2538
Hai Shalom899fcc72020-10-19 14:38:18 -07002539static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2540 int snr, bool vht)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002541{
Hai Shalom899fcc72020-10-19 14:38:18 -07002542 const struct minsnr_bitrate_entry *prev, *entry = table;
2543
2544 while ((entry->minsnr != -1) &&
2545 (snr >= entry->minsnr) &&
2546 (vht || entry - table <= vht_mcs))
2547 entry++;
2548 if (entry == table)
2549 return entry->bitrate;
2550 prev = entry - 1;
2551 if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2552 return prev->bitrate;
2553 return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2554 entry->bitrate);
Hai Shalomfdcde762020-04-02 11:19:20 -07002555}
2556
2557
Hai Shalom899fcc72020-10-19 14:38:18 -07002558static unsigned int max_ht20_rate(int snr, bool vht)
Hai Shalomfdcde762020-04-02 11:19:20 -07002559{
Hai Shalom899fcc72020-10-19 14:38:18 -07002560 return max_rate(vht20_table, snr, vht);
2561}
2562
2563
2564static unsigned int max_ht40_rate(int snr, bool vht)
2565{
2566 return max_rate(vht40_table, snr, vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002567}
2568
2569
2570static unsigned int max_vht80_rate(int snr)
2571{
Hai Shalom899fcc72020-10-19 14:38:18 -07002572 return max_rate(vht80_table, snr, 1);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002573}
2574
Hai Shalomfdcde762020-04-02 11:19:20 -07002575
Hai Shaloma20dcd72022-02-04 13:43:00 -08002576static unsigned int max_vht160_rate(int snr)
2577{
2578 return max_rate(vht160_table, snr, 1);
2579}
2580
2581
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002582static unsigned int max_he_eht_rate(const struct minsnr_bitrate_entry table[],
2583 int snr, bool eht)
Hai Shaloma20dcd72022-02-04 13:43:00 -08002584{
2585 const struct minsnr_bitrate_entry *prev, *entry = table;
2586
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002587 while (entry->minsnr != -1 && snr >= entry->minsnr &&
2588 (eht || entry - table <= EHT_MCS))
Hai Shaloma20dcd72022-02-04 13:43:00 -08002589 entry++;
2590 if (entry == table)
2591 return 0;
2592 prev = entry - 1;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002593 if (entry->minsnr == -1 || (!eht && entry - table > EHT_MCS))
Hai Shaloma20dcd72022-02-04 13:43:00 -08002594 return prev->bitrate;
2595 return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2596 prev->bitrate, entry->bitrate);
2597}
2598
2599
Hai Shalomfdcde762020-04-02 11:19:20 -07002600unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2601 const u8 *ies, size_t ies_len, int rate,
Hai Shaloma20dcd72022-02-04 13:43:00 -08002602 int snr, int freq)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002603{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002604 struct hostapd_hw_modes *hw_mode;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002605 unsigned int est, tmp;
Hai Shalomfdcde762020-04-02 11:19:20 -07002606 const u8 *ie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002607
2608 /* Limit based on estimated SNR */
2609 if (rate > 1 * 2 && snr < 1)
2610 rate = 1 * 2;
2611 else if (rate > 2 * 2 && snr < 4)
2612 rate = 2 * 2;
2613 else if (rate > 6 * 2 && snr < 5)
2614 rate = 6 * 2;
2615 else if (rate > 9 * 2 && snr < 6)
2616 rate = 9 * 2;
2617 else if (rate > 12 * 2 && snr < 7)
2618 rate = 12 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002619 else if (rate > 12 * 2 && snr < 8)
2620 rate = 14 * 2;
2621 else if (rate > 12 * 2 && snr < 9)
2622 rate = 16 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002623 else if (rate > 18 * 2 && snr < 10)
2624 rate = 18 * 2;
2625 else if (rate > 24 * 2 && snr < 11)
2626 rate = 24 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002627 else if (rate > 24 * 2 && snr < 12)
2628 rate = 27 * 2;
2629 else if (rate > 24 * 2 && snr < 13)
2630 rate = 30 * 2;
2631 else if (rate > 24 * 2 && snr < 14)
2632 rate = 33 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002633 else if (rate > 36 * 2 && snr < 15)
2634 rate = 36 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002635 else if (rate > 36 * 2 && snr < 16)
2636 rate = 39 * 2;
2637 else if (rate > 36 * 2 && snr < 17)
2638 rate = 42 * 2;
2639 else if (rate > 36 * 2 && snr < 18)
2640 rate = 45 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002641 else if (rate > 48 * 2 && snr < 19)
2642 rate = 48 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002643 else if (rate > 48 * 2 && snr < 20)
2644 rate = 51 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002645 else if (rate > 54 * 2 && snr < 21)
2646 rate = 54 * 2;
2647 est = rate * 500;
2648
Hai Shaloma20dcd72022-02-04 13:43:00 -08002649 hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2650 freq);
2651
2652 if (hw_mode && hw_mode->ht_capab) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002653 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002654 if (ie) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002655 tmp = max_ht20_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002656 if (tmp > est)
2657 est = tmp;
2658 }
2659 }
2660
Hai Shaloma20dcd72022-02-04 13:43:00 -08002661 if (hw_mode &&
2662 (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002663 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002664 if (ie && ie[1] >= 2 &&
2665 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002666 tmp = max_ht40_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002667 if (tmp > est)
2668 est = tmp;
2669 }
2670 }
2671
Hai Shaloma20dcd72022-02-04 13:43:00 -08002672 if (hw_mode && hw_mode->vht_capab) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002673 /* Use +1 to assume VHT is always faster than HT */
Hai Shalomfdcde762020-04-02 11:19:20 -07002674 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002675 if (ie) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002676 bool vht80 = false, vht160 = false;
2677
Hai Shalom899fcc72020-10-19 14:38:18 -07002678 tmp = max_ht20_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002679 if (tmp > est)
2680 est = tmp;
2681
Hai Shalomfdcde762020-04-02 11:19:20 -07002682 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002683 if (ie && ie[1] >= 2 &&
2684 (ie[3] &
2685 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002686 tmp = max_ht40_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002687 if (tmp > est)
2688 est = tmp;
2689 }
2690
Hai Shaloma20dcd72022-02-04 13:43:00 -08002691 /* Determine VHT BSS bandwidth based on IEEE Std
2692 * 802.11-2020, Table 11-23 (VHT BSs bandwidth) */
Hai Shalomfdcde762020-04-02 11:19:20 -07002693 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002694 if (ie && ie[1] >= 3) {
2695 u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2696 u8 seg0 = ie[3];
2697 u8 seg1 = ie[4];
2698
2699 if (cw)
2700 vht80 = true;
2701 if (cw == 2 ||
2702 (cw == 3 &&
2703 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2704 vht160 = true;
2705 if (cw == 1 &&
2706 ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2707 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2708 vht160 = true;
2709 }
2710
2711 if (vht80) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002712 tmp = max_vht80_rate(snr) + 1;
2713 if (tmp > est)
2714 est = tmp;
2715 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08002716
2717 if (vht160 &&
2718 (hw_mode->vht_capab &
2719 (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2720 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
2721 tmp = max_vht160_rate(snr) + 1;
2722 if (tmp > est)
2723 est = tmp;
2724 }
2725 }
2726 }
2727
2728 if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
2729 /* Use +2 to assume HE is always faster than HT/VHT */
2730 struct ieee80211_he_capabilities *he;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002731 struct ieee80211_eht_capabilities *eht;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002732 struct he_capabilities *own_he;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002733 u8 cw, boost = 2;
2734 const u8 *eht_ie;
2735 bool is_eht = false;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002736
2737 ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
2738 if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
2739 return est;
2740 he = (struct ieee80211_he_capabilities *) &ie[3];
2741 own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
2742
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002743 /* Use +3 to assume EHT is always faster than HE */
2744 if (hw_mode->eht_capab[IEEE80211_MODE_INFRA].eht_supported) {
2745 eht_ie = get_ie_ext(ies, ies_len,
2746 WLAN_EID_EXT_EHT_CAPABILITIES);
2747 if (eht_ie &&
2748 (eht_ie[1] >= 1 + IEEE80211_EHT_CAPAB_MIN_LEN)) {
2749 is_eht = true;
2750 boost = 3;
2751 }
2752 }
2753
2754 tmp = max_he_eht_rate(he20_table, snr, is_eht) + boost;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002755 if (tmp > est)
2756 est = tmp;
2757
2758 cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
2759 own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
2760 if (cw &
2761 (IS_2P4GHZ(freq) ? HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
2762 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002763 tmp = max_he_eht_rate(he40_table, snr, is_eht) + boost;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002764 if (tmp > est)
2765 est = tmp;
2766 }
2767
2768 if (!IS_2P4GHZ(freq) &&
2769 (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002770 tmp = max_he_eht_rate(he80_table, snr, is_eht) + boost;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002771 if (tmp > est)
2772 est = tmp;
2773 }
2774
2775 if (!IS_2P4GHZ(freq) &&
2776 (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2777 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G))) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002778 tmp = max_he_eht_rate(he160_table, snr, is_eht) + boost;
2779 if (tmp > est)
2780 est = tmp;
2781 }
2782
2783 if (!is_eht)
2784 return est;
2785
2786 eht = (struct ieee80211_eht_capabilities *) &eht_ie[3];
2787
2788 if (is_6ghz_freq(freq) &&
2789 (eht->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
2790 EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
2791 tmp = max_he_eht_rate(eht320_table, snr, true);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002792 if (tmp > est)
2793 est = tmp;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002794 }
2795 }
2796
Hai Shalomfdcde762020-04-02 11:19:20 -07002797 return est;
2798}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002799
Hai Shalomfdcde762020-04-02 11:19:20 -07002800
2801void scan_est_throughput(struct wpa_supplicant *wpa_s,
2802 struct wpa_scan_res *res)
2803{
2804 int rate; /* max legacy rate in 500 kb/s units */
2805 int snr = res->snr;
2806 const u8 *ies = (const void *) (res + 1);
2807 size_t ie_len = res->ie_len;
2808
2809 if (res->est_throughput)
2810 return;
2811
2812 /* Get maximum legacy rate */
2813 rate = wpa_scan_get_max_rate(res);
2814
2815 if (!ie_len)
2816 ie_len = res->beacon_ie_len;
2817 res->est_throughput =
Hai Shaloma20dcd72022-02-04 13:43:00 -08002818 wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, res->freq);
Hai Shalomfdcde762020-04-02 11:19:20 -07002819
2820 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002821}
2822
2823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824/**
2825 * wpa_supplicant_get_scan_results - Get scan results
2826 * @wpa_s: Pointer to wpa_supplicant data
2827 * @info: Information about what was scanned or %NULL if not available
2828 * @new_scan: Whether a new scan was performed
2829 * Returns: Scan results, %NULL on failure
2830 *
2831 * This function request the current scan results from the driver and updates
2832 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2833 * results with wpa_scan_results_free().
2834 */
2835struct wpa_scan_results *
2836wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2837 struct scan_info *info, int new_scan)
2838{
2839 struct wpa_scan_results *scan_res;
2840 size_t i;
2841 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2842
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002843 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844 if (scan_res == NULL) {
2845 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2846 return NULL;
2847 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002848 if (scan_res->fetch_time.sec == 0) {
2849 /*
2850 * Make sure we have a valid timestamp if the driver wrapper
2851 * does not set this.
2852 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002853 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002854 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002855 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002857 for (i = 0; i < scan_res->num; i++) {
2858 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2859
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002860 scan_snr(scan_res_item);
2861 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002862 }
2863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002865 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2867 "provisioning rules");
2868 compar = wpa_scan_result_wps_compar;
2869 }
2870#endif /* CONFIG_WPS */
2871
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002872 if (scan_res->res) {
2873 qsort(scan_res->res, scan_res->num,
2874 sizeof(struct wpa_scan_res *), compar);
2875 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002876 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877
Dmitry Shmidt29333592017-01-09 12:27:11 -08002878 if (wpa_s->ignore_post_flush_scan_res) {
2879 /* FLUSH command aborted an ongoing scan and these are the
2880 * results from the aborted scan. Do not process the results to
2881 * maintain flushed state. */
2882 wpa_dbg(wpa_s, MSG_DEBUG,
2883 "Do not update BSS table based on pending post-FLUSH scan results");
2884 wpa_s->ignore_post_flush_scan_res = 0;
2885 return scan_res;
2886 }
2887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 wpa_bss_update_start(wpa_s);
2889 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002890 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2891 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892 wpa_bss_update_end(wpa_s, info, new_scan);
2893
2894 return scan_res;
2895}
2896
2897
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002898/**
2899 * wpa_supplicant_update_scan_results - Update scan results from the driver
2900 * @wpa_s: Pointer to wpa_supplicant data
2901 * Returns: 0 on success, -1 on failure
2902 *
2903 * This function updates the BSS table within wpa_supplicant based on the
2904 * currently available scan results from the driver without requesting a new
2905 * scan. This is used in cases where the driver indicates an association
2906 * (including roaming within ESS) and wpa_supplicant does not yet have the
2907 * needed information to complete the connection (e.g., to perform validation
2908 * steps in 4-way handshake).
2909 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2911{
2912 struct wpa_scan_results *scan_res;
2913 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2914 if (scan_res == NULL)
2915 return -1;
2916 wpa_scan_results_free(scan_res);
2917
2918 return 0;
2919}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002920
2921
2922/**
2923 * scan_only_handler - Reports scan results
2924 */
2925void scan_only_handler(struct wpa_supplicant *wpa_s,
2926 struct wpa_scan_results *scan_res)
2927{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002928 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002929 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2930 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2931 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2932 wpa_s->manual_scan_id);
2933 wpa_s->manual_scan_use_id = 0;
2934 } else {
2935 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2936 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002937 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002938 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002939 if (wpa_s->scan_work) {
2940 struct wpa_radio_work *work = wpa_s->scan_work;
2941 wpa_s->scan_work = NULL;
2942 radio_work_done(work);
2943 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002944
2945 if (wpa_s->wpa_state == WPA_SCANNING)
2946 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002947}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002948
2949
2950int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2951{
2952 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2953}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002954
2955
2956struct wpa_driver_scan_params *
2957wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2958{
2959 struct wpa_driver_scan_params *params;
2960 size_t i;
2961 u8 *n;
2962
2963 params = os_zalloc(sizeof(*params));
2964 if (params == NULL)
2965 return NULL;
2966
2967 for (i = 0; i < src->num_ssids; i++) {
2968 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002969 n = os_memdup(src->ssids[i].ssid,
2970 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002971 if (n == NULL)
2972 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002973 params->ssids[i].ssid = n;
2974 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2975 }
2976 }
2977 params->num_ssids = src->num_ssids;
2978
2979 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002980 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002981 if (n == NULL)
2982 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002983 params->extra_ies = n;
2984 params->extra_ies_len = src->extra_ies_len;
2985 }
2986
2987 if (src->freqs) {
2988 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002989 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002990 if (params->freqs == NULL)
2991 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002992 }
2993
2994 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002995 params->filter_ssids = os_memdup(src->filter_ssids,
2996 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002997 src->num_filter_ssids);
2998 if (params->filter_ssids == NULL)
2999 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003000 params->num_filter_ssids = src->num_filter_ssids;
3001 }
3002
3003 params->filter_rssi = src->filter_rssi;
3004 params->p2p_probe = src->p2p_probe;
3005 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003006 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003007 params->duration = src->duration;
3008 params->duration_mandatory = src->duration_mandatory;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003009 params->oce_scan = src->oce_scan;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003010
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003011 if (src->sched_scan_plans_num > 0) {
3012 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003013 os_memdup(src->sched_scan_plans,
3014 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003015 src->sched_scan_plans_num);
3016 if (!params->sched_scan_plans)
3017 goto failed;
3018
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003019 params->sched_scan_plans_num = src->sched_scan_plans_num;
3020 }
3021
Hai Shalomc3565922019-10-28 11:58:20 -07003022 if (src->mac_addr_rand &&
3023 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
3024 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003025
3026 if (src->bssid) {
3027 u8 *bssid;
3028
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003029 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003030 if (!bssid)
3031 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003032 params->bssid = bssid;
3033 }
3034
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003035 params->relative_rssi_set = src->relative_rssi_set;
3036 params->relative_rssi = src->relative_rssi;
3037 params->relative_adjust_band = src->relative_adjust_band;
3038 params->relative_adjust_rssi = src->relative_adjust_rssi;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003039 params->p2p_include_6ghz = src->p2p_include_6ghz;
Sunil8cd6f4d2022-06-28 18:40:46 +00003040 params->non_coloc_6ghz = src->non_coloc_6ghz;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003041 return params;
3042
3043failed:
3044 wpa_scan_free_params(params);
3045 return NULL;
3046}
3047
3048
3049void wpa_scan_free_params(struct wpa_driver_scan_params *params)
3050{
3051 size_t i;
3052
3053 if (params == NULL)
3054 return;
3055
3056 for (i = 0; i < params->num_ssids; i++)
3057 os_free((u8 *) params->ssids[i].ssid);
3058 os_free((u8 *) params->extra_ies);
3059 os_free(params->freqs);
3060 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003061 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003062
3063 /*
3064 * Note: params->mac_addr_mask points to same memory allocation and
3065 * must not be freed separately.
3066 */
3067 os_free((u8 *) params->mac_addr);
3068
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003069 os_free((u8 *) params->bssid);
3070
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003071 os_free(params);
3072}
Dmitry Shmidt98660862014-03-11 17:26:21 -07003073
3074
3075int wpas_start_pno(struct wpa_supplicant *wpa_s)
3076{
Hai Shalomfdcde762020-04-02 11:19:20 -07003077 int ret;
3078 size_t prio, i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003079 struct wpa_ssid *ssid;
3080 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003081 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07003082 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003083
3084 if (!wpa_s->sched_scan_supported)
3085 return -1;
3086
Dmitry Shmidte4663042016-04-04 10:07:49 -07003087 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
3088 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
3089 else
3090 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
3091 if (max_sched_scan_ssids < 1)
3092 return -1;
3093
Dmitry Shmidt98660862014-03-11 17:26:21 -07003094 if (wpa_s->pno || wpa_s->pno_sched_pending)
3095 return 0;
3096
3097 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003098 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003099 wpa_printf(MSG_ERROR, "PNO: In assoc process");
3100 return -EAGAIN;
3101 }
3102
3103 if (wpa_s->wpa_state == WPA_SCANNING) {
3104 wpa_supplicant_cancel_scan(wpa_s);
3105 if (wpa_s->sched_scanning) {
3106 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
3107 "ongoing sched scan");
3108 wpa_supplicant_cancel_sched_scan(wpa_s);
3109 wpa_s->pno_sched_pending = 1;
3110 return 0;
3111 }
3112 }
3113
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003114 if (wpa_s->sched_scan_stop_req) {
3115 wpa_printf(MSG_DEBUG,
3116 "Schedule PNO after previous sched scan has stopped");
3117 wpa_s->pno_sched_pending = 1;
3118 return 0;
3119 }
3120
Dmitry Shmidt98660862014-03-11 17:26:21 -07003121 os_memset(&params, 0, sizeof(params));
3122
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003123 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003124 ssid = wpa_s->conf->ssid;
3125 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003126 if (!wpas_network_disabled(wpa_s, ssid)) {
3127 num_match_ssid++;
3128 if (ssid->scan_ssid)
3129 num_ssid++;
3130 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003131 ssid = ssid->next;
3132 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003133
3134 if (num_match_ssid == 0) {
3135 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3136 return -1;
3137 }
3138
3139 if (num_match_ssid > num_ssid) {
3140 params.num_ssids++; /* wildcard */
3141 num_ssid++;
3142 }
3143
Dmitry Shmidte4663042016-04-04 10:07:49 -07003144 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003145 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07003146 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3147 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003148 }
3149
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003150 if (num_match_ssid > wpa_s->max_match_sets) {
3151 num_match_ssid = wpa_s->max_match_sets;
3152 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003153 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003154 params.filter_ssids = os_calloc(num_match_ssid,
3155 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07003156 if (params.filter_ssids == NULL)
3157 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003158
Dmitry Shmidt98660862014-03-11 17:26:21 -07003159 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003160 prio = 0;
3161 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003162 while (ssid) {
3163 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003164 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3165 params.ssids[params.num_ssids].ssid =
3166 ssid->ssid;
3167 params.ssids[params.num_ssids].ssid_len =
3168 ssid->ssid_len;
3169 params.num_ssids++;
3170 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003171 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3172 ssid->ssid_len);
3173 params.filter_ssids[i].ssid_len = ssid->ssid_len;
3174 params.num_filter_ssids++;
3175 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003176 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07003177 break;
3178 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003179 if (ssid->pnext)
3180 ssid = ssid->pnext;
3181 else if (prio + 1 == wpa_s->conf->num_prio)
3182 break;
3183 else
3184 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003185 }
3186
3187 if (wpa_s->conf->filter_rssi)
3188 params.filter_rssi = wpa_s->conf->filter_rssi;
3189
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003190 if (wpa_s->sched_scan_plans_num) {
3191 params.sched_scan_plans = wpa_s->sched_scan_plans;
3192 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3193 } else {
3194 /* Set one scan plan that will run infinitely */
3195 if (wpa_s->conf->sched_scan_interval)
3196 scan_plan.interval = wpa_s->conf->sched_scan_interval;
3197 else
3198 scan_plan.interval = 10;
3199
3200 scan_plan.iterations = 0;
3201 params.sched_scan_plans = &scan_plan;
3202 params.sched_scan_plans_num = 1;
3203 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003204
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003205 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3206
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003207 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3208 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3209 params.freqs = wpa_s->manual_sched_scan_freqs;
3210 }
3211
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003212 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
Hai Shalomc3565922019-10-28 11:58:20 -07003213 wpa_s->wpa_state <= WPA_SCANNING)
3214 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003215
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003216 wpa_scan_set_relative_rssi_params(wpa_s, &params);
3217
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003218 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003219 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07003220 os_free(params.mac_addr);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003221 if (ret == 0)
3222 wpa_s->pno = 1;
3223 else
3224 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3225 return ret;
3226}
3227
3228
3229int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3230{
3231 int ret = 0;
3232
3233 if (!wpa_s->pno)
3234 return 0;
3235
3236 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003237 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003238
3239 wpa_s->pno = 0;
3240 wpa_s->pno_sched_pending = 0;
3241
3242 if (wpa_s->wpa_state == WPA_SCANNING)
3243 wpa_supplicant_req_scan(wpa_s, 0, 0);
3244
3245 return ret;
3246}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003247
3248
3249void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3250 unsigned int type)
3251{
3252 type &= MAC_ADDR_RAND_ALL;
3253 wpa_s->mac_addr_rand_enable &= ~type;
3254
3255 if (type & MAC_ADDR_RAND_SCAN) {
3256 os_free(wpa_s->mac_addr_scan);
3257 wpa_s->mac_addr_scan = NULL;
3258 }
3259
3260 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3261 os_free(wpa_s->mac_addr_sched_scan);
3262 wpa_s->mac_addr_sched_scan = NULL;
3263 }
3264
3265 if (type & MAC_ADDR_RAND_PNO) {
3266 os_free(wpa_s->mac_addr_pno);
3267 wpa_s->mac_addr_pno = NULL;
3268 }
3269}
3270
3271
3272int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3273 unsigned int type, const u8 *addr,
3274 const u8 *mask)
3275{
3276 u8 *tmp = NULL;
3277
Hai Shalom74f70d42019-02-11 14:42:39 -08003278 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3279 wpa_printf(MSG_INFO,
3280 "scan: MAC randomization type %u != supported=%u",
3281 type, wpa_s->mac_addr_rand_supported);
3282 return -1;
3283 }
3284
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003285 wpas_mac_addr_rand_scan_clear(wpa_s, type);
3286
3287 if (addr) {
3288 tmp = os_malloc(2 * ETH_ALEN);
3289 if (!tmp)
3290 return -1;
3291 os_memcpy(tmp, addr, ETH_ALEN);
3292 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3293 }
3294
3295 if (type == MAC_ADDR_RAND_SCAN) {
3296 wpa_s->mac_addr_scan = tmp;
3297 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3298 wpa_s->mac_addr_sched_scan = tmp;
3299 } else if (type == MAC_ADDR_RAND_PNO) {
3300 wpa_s->mac_addr_pno = tmp;
3301 } else {
3302 wpa_printf(MSG_INFO,
3303 "scan: Invalid MAC randomization type=0x%x",
3304 type);
3305 os_free(tmp);
3306 return -1;
3307 }
3308
3309 wpa_s->mac_addr_rand_enable |= type;
3310 return 0;
3311}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003312
3313
Hai Shalomc3565922019-10-28 11:58:20 -07003314int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3315 unsigned int type, u8 *mask)
3316{
3317 const u8 *to_copy;
3318
3319 if ((wpa_s->mac_addr_rand_enable & type) != type)
3320 return -1;
3321
3322 if (type == MAC_ADDR_RAND_SCAN) {
3323 to_copy = wpa_s->mac_addr_scan;
3324 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3325 to_copy = wpa_s->mac_addr_sched_scan;
3326 } else if (type == MAC_ADDR_RAND_PNO) {
3327 to_copy = wpa_s->mac_addr_pno;
3328 } else {
3329 wpa_printf(MSG_DEBUG,
3330 "scan: Invalid MAC randomization type=0x%x",
3331 type);
3332 return -1;
3333 }
3334
3335 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3336 return 0;
3337}
3338
3339
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003340int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3341{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003342 struct wpa_radio_work *work;
3343 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003344
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003345 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3346 if (work->wpa_s != wpa_s || !work->started ||
3347 (os_strcmp(work->type, "scan") != 0 &&
3348 os_strcmp(work->type, "p2p-scan") != 0))
3349 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003350 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003351 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003352 }
3353
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003354 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3355 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003356}
3357
3358
3359int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3360{
3361 struct sched_scan_plan *scan_plans = NULL;
3362 const char *token, *context = NULL;
3363 unsigned int num = 0;
3364
3365 if (!cmd)
3366 return -1;
3367
3368 if (!cmd[0]) {
3369 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3370 os_free(wpa_s->sched_scan_plans);
3371 wpa_s->sched_scan_plans = NULL;
3372 wpa_s->sched_scan_plans_num = 0;
3373 return 0;
3374 }
3375
3376 while ((token = cstr_token(cmd, " ", &context))) {
3377 int ret;
3378 struct sched_scan_plan *scan_plan, *n;
3379
3380 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3381 if (!n)
3382 goto fail;
3383
3384 scan_plans = n;
3385 scan_plan = &scan_plans[num];
3386 num++;
3387
3388 ret = sscanf(token, "%u:%u", &scan_plan->interval,
3389 &scan_plan->iterations);
3390 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3391 wpa_printf(MSG_ERROR,
3392 "Invalid sched scan plan input: %s", token);
3393 goto fail;
3394 }
3395
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003396 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3397 wpa_printf(MSG_WARNING,
3398 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3399 num, scan_plan->interval,
3400 wpa_s->max_sched_scan_plan_interval);
3401 scan_plan->interval =
3402 wpa_s->max_sched_scan_plan_interval;
3403 }
3404
3405 if (ret == 1) {
3406 scan_plan->iterations = 0;
3407 break;
3408 }
3409
3410 if (!scan_plan->iterations) {
3411 wpa_printf(MSG_ERROR,
3412 "scan plan %u: Number of iterations cannot be zero",
3413 num);
3414 goto fail;
3415 }
3416
3417 if (scan_plan->iterations >
3418 wpa_s->max_sched_scan_plan_iterations) {
3419 wpa_printf(MSG_WARNING,
3420 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3421 num, scan_plan->iterations,
3422 wpa_s->max_sched_scan_plan_iterations);
3423 scan_plan->iterations =
3424 wpa_s->max_sched_scan_plan_iterations;
3425 }
3426
3427 wpa_printf(MSG_DEBUG,
3428 "scan plan %u: interval=%u iterations=%u",
3429 num, scan_plan->interval, scan_plan->iterations);
3430 }
3431
3432 if (!scan_plans) {
3433 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3434 goto fail;
3435 }
3436
3437 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3438 wpa_printf(MSG_ERROR,
3439 "All scan plans but the last must specify a number of iterations");
3440 goto fail;
3441 }
3442
3443 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3444 num, scan_plans[num - 1].interval);
3445
3446 if (num > wpa_s->max_sched_scan_plans) {
3447 wpa_printf(MSG_WARNING,
3448 "Too many scheduled scan plans (only %u supported)",
3449 wpa_s->max_sched_scan_plans);
3450 wpa_printf(MSG_WARNING,
3451 "Use only the first %u scan plans, and the last one (in infinite loop)",
3452 wpa_s->max_sched_scan_plans - 1);
3453 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3454 &scan_plans[num - 1], sizeof(*scan_plans));
3455 num = wpa_s->max_sched_scan_plans;
3456 }
3457
3458 os_free(wpa_s->sched_scan_plans);
3459 wpa_s->sched_scan_plans = scan_plans;
3460 wpa_s->sched_scan_plans_num = num;
3461
3462 return 0;
3463
3464fail:
3465 os_free(scan_plans);
3466 wpa_printf(MSG_ERROR, "invalid scan plans list");
3467 return -1;
3468}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003469
3470
3471/**
3472 * wpas_scan_reset_sched_scan - Reset sched_scan state
3473 * @wpa_s: Pointer to wpa_supplicant data
3474 *
3475 * This function is used to cancel a running scheduled scan and to reset an
3476 * internal scan state to continue with a regular scan on the following
3477 * wpa_supplicant_req_scan() calls.
3478 */
3479void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3480{
3481 wpa_s->normal_scans = 0;
3482 if (wpa_s->sched_scanning) {
3483 wpa_s->sched_scan_timed_out = 0;
3484 wpa_s->prev_sched_ssid = NULL;
3485 wpa_supplicant_cancel_sched_scan(wpa_s);
3486 }
3487}
3488
3489
3490void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3491{
3492 /* simulate timeout to restart the sched scan */
3493 wpa_s->sched_scan_timed_out = 1;
3494 wpa_s->prev_sched_ssid = NULL;
3495 wpa_supplicant_cancel_sched_scan(wpa_s);
3496}