blob: c3984a40c94c0d25294d6e91234a13edd6500a9b [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) {
Sunil Raviaf8751c2023-03-29 11:35:17 -0700448 struct wpa_ssid *ssid = wpa_s->current_ssid;
449
Dmitry Shmidt15907092014-03-25 10:42:57 -0700450 /*
Matthew Wangdcf19452022-11-07 20:42:52 -0800451 * Perform a single-channel scan if the GO has already been
452 * discovered on another non-P2P interface. Note that a scan
Sunil Raviaf8751c2023-03-29 11:35:17 -0700453 * initiated by a P2P interface (e.g., the device interface)
Matthew Wangdcf19452022-11-07 20:42:52 -0800454 * should already have sufficient IEs and scan results will be
455 * fetched on interface creation in that case.
456 */
Sunil Raviaf8751c2023-03-29 11:35:17 -0700457 if (wpa_s->p2p_in_invitation == 1 && ssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -0800458 struct wpa_supplicant *ifs;
459 struct wpa_bss *bss = NULL;
Sunil Raviaf8751c2023-03-29 11:35:17 -0700460 const u8 *bssid = ssid->bssid_set ? ssid->bssid : NULL;
461
Matthew Wangdcf19452022-11-07 20:42:52 -0800462 dl_list_for_each(ifs, &wpa_s->radio->ifaces,
463 struct wpa_supplicant, radio_list) {
464 bss = wpa_bss_get(ifs, bssid, ssid->ssid,
465 ssid->ssid_len);
466 if (bss)
467 break;
468 }
469 if (bss && !disabled_freq(wpa_s, bss->freq)) {
470 params->freqs = os_calloc(2, sizeof(int));
Matthew Wangafc981e2023-03-17 21:30:12 +0000471 if (params->freqs) {
472 wpa_dbg(wpa_s, MSG_DEBUG,
473 "P2P: Scan only the known GO frequency %d MHz during invitation",
474 bss->freq);
Matthew Wangdcf19452022-11-07 20:42:52 -0800475 params->freqs[0] = bss->freq;
Matthew Wangafc981e2023-03-17 21:30:12 +0000476 }
Matthew Wangdcf19452022-11-07 20:42:52 -0800477 }
478 }
Sunil Raviaf8751c2023-03-29 11:35:17 -0700479
Matthew Wangdcf19452022-11-07 20:42:52 -0800480 /*
Dmitry Shmidt15907092014-03-25 10:42:57 -0700481 * Optimize scan based on GO information during persistent
482 * group reinvocation
483 */
Matthew Wangafc981e2023-03-17 21:30:12 +0000484 if (!params->freqs && wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700485 wpa_s->p2p_invite_go_freq > 0) {
Matthew Wang36173112022-11-07 21:48:44 -0800486 if (wpa_s->p2p_invite_go_freq == 2 ||
487 wpa_s->p2p_invite_go_freq == 5) {
Matthew Wang36173112022-11-07 21:48:44 -0800488 enum hostapd_hw_mode mode;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000489
490 wpa_dbg(wpa_s, MSG_DEBUG,
491 "P2P: Scan only GO preferred band %d GHz during invitation",
492 wpa_s->p2p_invite_go_freq);
493
494 if (!wpa_s->hw.modes)
Matthew Wang36173112022-11-07 21:48:44 -0800495 return;
496 mode = wpa_s->p2p_invite_go_freq == 5 ?
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000497 HOSTAPD_MODE_IEEE80211A :
498 HOSTAPD_MODE_IEEE80211G;
Matthew Wang36173112022-11-07 21:48:44 -0800499 if (wpa_s->p2p_in_invitation <= 2)
500 wpa_add_scan_freqs_list(wpa_s, mode,
501 params, false,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000502 false, true);
503 if (!params->freqs || params->freqs[0] == 0)
Matthew Wang36173112022-11-07 21:48:44 -0800504 wpa_add_scan_freqs_list(wpa_s, mode,
505 params, false,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000506 false, false);
Matthew Wang36173112022-11-07 21:48:44 -0800507 } else {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000508 wpa_dbg(wpa_s, MSG_DEBUG,
509 "P2P: Scan only GO preferred frequency %d MHz during invitation",
Matthew Wang36173112022-11-07 21:48:44 -0800510 wpa_s->p2p_invite_go_freq);
511 params->freqs = os_calloc(2, sizeof(int));
512 if (params->freqs)
513 params->freqs[0] =
514 wpa_s->p2p_invite_go_freq;
515 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700516 }
517 wpa_s->p2p_in_invitation++;
518 if (wpa_s->p2p_in_invitation > 20) {
519 /*
520 * This should not really happen since the variable is
521 * cleared on group removal, but if it does happen, make
522 * sure we do not get stuck in special invitation scan
523 * mode.
524 */
525 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
526 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +0000527 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -0700528 }
529 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800530#endif /* CONFIG_P2P */
531
532#ifdef CONFIG_WPS
533 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
534 /*
535 * Optimize post-provisioning scan based on channel used
536 * during provisioning.
537 */
538 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
539 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800540 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800541 if (params->freqs)
542 params->freqs[0] = wpa_s->wps_freq;
543 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800544 } else if (wpa_s->after_wps)
545 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800546
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800547 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
548 {
549 /* Optimize provisioning scan based on already known channel */
550 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
551 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800552 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800553 if (params->freqs)
554 params->freqs[0] = wpa_s->wps_freq;
555 wpa_s->known_wps_freq = 0; /* only do this once */
556 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800557#endif /* CONFIG_WPS */
558}
559
560
561#ifdef CONFIG_INTERWORKING
562static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
563 struct wpabuf *buf)
564{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800565 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
566 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
567 1 + ETH_ALEN);
568 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
569 /* No Venue Info */
570 if (!is_zero_ether_addr(wpa_s->conf->hessid))
571 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
572}
573#endif /* CONFIG_INTERWORKING */
574
575
Hai Shalomce48b4a2018-09-05 11:41:35 -0700576#ifdef CONFIG_MBO
577static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
578 struct wpabuf **ie)
579{
580 if (wpabuf_resize(ie, 5)) {
581 wpa_printf(MSG_DEBUG,
582 "Failed to allocate space for FILS Request Parameters element");
583 return;
584 }
585
586 /* FILS Request Parameters element */
587 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
588 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
589 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
590 /* Parameter control bitmap */
591 wpabuf_put_u8(*ie, 0);
592 /* Max Channel Time field - contains the value of MaxChannelTime
593 * parameter of the MLME-SCAN.request primitive represented in units of
594 * TUs, as an unsigned integer. A Max Channel Time field value of 255
595 * is used to indicate any duration of more than 254 TUs, or an
596 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
597 */
598 wpabuf_put_u8(*ie, 255);
599}
600#endif /* CONFIG_MBO */
601
602
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700603void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
604{
605 struct wpabuf *default_ies = NULL;
606 u8 ext_capab[18];
Hai Shalom60840252021-02-19 19:02:11 -0800607 int ext_capab_len, frame_id;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700608 enum wpa_driver_if_type type = WPA_IF_STATION;
609
610#ifdef CONFIG_P2P
611 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
612 type = WPA_IF_P2P_CLIENT;
613#endif /* CONFIG_P2P */
614
615 wpa_drv_get_ext_capa(wpa_s, type);
616
617 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
618 sizeof(ext_capab));
619 if (ext_capab_len > 0 &&
620 wpabuf_resize(&default_ies, ext_capab_len) == 0)
621 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
622
623#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -0700624 if (wpa_s->enable_oce & OCE_STA)
625 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700626 /* Send MBO and OCE capabilities */
627 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700628 wpas_mbo_scan_ie(wpa_s, default_ies);
629#endif /* CONFIG_MBO */
630
Hai Shalom60840252021-02-19 19:02:11 -0800631 if (type == WPA_IF_P2P_CLIENT)
632 frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
633 else
634 frame_id = VENDOR_ELEM_PROBE_REQ;
635
636 if (wpa_s->vendor_elem[frame_id]) {
637 size_t len;
638
639 len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
640 if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
641 wpabuf_put_buf(default_ies,
642 wpa_s->vendor_elem[frame_id]);
643 }
644
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700645 if (default_ies)
646 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
647 wpabuf_len(default_ies));
648 wpabuf_free(default_ies);
649}
650
651
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700652static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800653{
654 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700655 u8 ext_capab[18];
656 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800657#ifdef CONFIG_WPS
658 int wps = 0;
659 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
660#endif /* CONFIG_WPS */
661
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700662#ifdef CONFIG_P2P
663 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
664 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
665 else
666#endif /* CONFIG_P2P */
667 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
668
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700669 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
670 sizeof(ext_capab));
671 if (ext_capab_len > 0 &&
672 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
673 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800675#ifdef CONFIG_INTERWORKING
676 if (wpa_s->conf->interworking &&
677 wpabuf_resize(&extra_ie, 100) == 0)
678 wpas_add_interworking_elements(wpa_s, extra_ie);
679#endif /* CONFIG_INTERWORKING */
680
Hai Shalomce48b4a2018-09-05 11:41:35 -0700681#ifdef CONFIG_MBO
682 if (wpa_s->enable_oce & OCE_STA)
683 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
684#endif /* CONFIG_MBO */
685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800686#ifdef CONFIG_WPS
687 wps = wpas_wps_in_use(wpa_s, &req_type);
688
689 if (wps) {
690 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700691 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
692 DEV_PW_DEFAULT,
693 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800694 wpa_s->wps->uuid, req_type,
695 0, NULL);
696 if (wps_ie) {
697 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
698 wpabuf_put_buf(extra_ie, wps_ie);
699 wpabuf_free(wps_ie);
700 }
701 }
702
703#ifdef CONFIG_P2P
704 if (wps) {
705 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
706 if (wpabuf_resize(&extra_ie, ielen) == 0)
707 wpas_p2p_scan_ie(wpa_s, extra_ie);
708 }
709#endif /* CONFIG_P2P */
710
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800711 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
712
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800713#endif /* CONFIG_WPS */
714
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700715#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -0800716 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
717 wpas_hs20_add_indication(extra_ie, -1, 0);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700718#endif /* CONFIG_HS20 */
719
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800720#ifdef CONFIG_FST
721 if (wpa_s->fst_ies &&
722 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
723 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
724#endif /* CONFIG_FST */
725
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800726#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700727 /* Send MBO and OCE capabilities */
728 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800729 wpas_mbo_scan_ie(wpa_s, extra_ie);
730#endif /* CONFIG_MBO */
731
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700732 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
733 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
734
735 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
736 wpabuf_put_buf(extra_ie, buf);
737 }
738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800739 return extra_ie;
740}
741
742
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800743#ifdef CONFIG_P2P
744
745/*
746 * Check whether there are any enabled networks or credentials that could be
747 * used for a non-P2P connection.
748 */
749static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
750{
751 struct wpa_ssid *ssid;
752
753 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
754 if (wpas_network_disabled(wpa_s, ssid))
755 continue;
756 if (!ssid->p2p_group)
757 return 1;
758 }
759
760 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
761 wpa_s->conf->auto_interworking)
762 return 1;
763
764 return 0;
765}
766
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700767#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800768
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800769
Hai Shalom60840252021-02-19 19:02:11 -0800770int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
771 enum hostapd_hw_mode band,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000772 struct wpa_driver_scan_params *params,
773 bool is_6ghz, bool only_6ghz_psc,
Matthew Wang36173112022-11-07 21:48:44 -0800774 bool exclude_radar)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700775{
776 /* Include only supported channels for the specified band */
777 struct hostapd_hw_modes *mode;
Hai Shalom60840252021-02-19 19:02:11 -0800778 int num_chans = 0;
779 int *freqs, i;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700780
Hai Shalomfdcde762020-04-02 11:19:20 -0700781 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000782 if (!mode || !mode->num_channels)
Hai Shalom60840252021-02-19 19:02:11 -0800783 return -1;
784
785 if (params->freqs) {
786 while (params->freqs[num_chans])
787 num_chans++;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700788 }
789
Hai Shalom60840252021-02-19 19:02:11 -0800790 freqs = os_realloc(params->freqs,
791 (num_chans + mode->num_channels + 1) * sizeof(int));
792 if (!freqs)
793 return -1;
794
795 params->freqs = freqs;
796 for (i = 0; i < mode->num_channels; i++) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700797 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
798 continue;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000799 if (exclude_radar &&
800 (mode->channels[i].flag & HOSTAPD_CHAN_RADAR))
Matthew Wang36173112022-11-07 21:48:44 -0800801 continue;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000802
803 if (is_6ghz && only_6ghz_psc &&
804 !is_6ghz_psc_frequency(mode->channels[i].freq))
805 continue;
806
Hai Shalom60840252021-02-19 19:02:11 -0800807 params->freqs[num_chans++] = mode->channels[i].freq;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700808 }
Hai Shalom60840252021-02-19 19:02:11 -0800809 params->freqs[num_chans] = 0;
810
811 return 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700812}
813
814
815static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
816 struct wpa_driver_scan_params *params)
817{
818 if (wpa_s->hw.modes == NULL)
819 return; /* unknown what channels the driver supports */
820 if (params->freqs)
821 return; /* already using a limited channel set */
Hai Shalom60840252021-02-19 19:02:11 -0800822
823 if (wpa_s->setband_mask & WPA_SETBAND_5G)
824 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000825 false, false, false);
Hai Shalom60840252021-02-19 19:02:11 -0800826 if (wpa_s->setband_mask & WPA_SETBAND_2G)
827 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000828 false, false, false);
Hai Shalom60840252021-02-19 19:02:11 -0800829 if (wpa_s->setband_mask & WPA_SETBAND_6G)
830 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000831 true, false, false);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700832}
833
834
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800835static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
836 struct wpa_driver_scan_params *params,
837 size_t max_ssids, const u8 *ssid, size_t ssid_len)
838{
839 unsigned int j;
840
841 for (j = 0; j < params->num_ssids; j++) {
842 if (params->ssids[j].ssid_len == ssid_len &&
843 params->ssids[j].ssid &&
844 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
845 return; /* already in the list */
846 }
847
848 if (params->num_ssids + 1 > max_ssids) {
849 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
850 return;
851 }
852
853 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
854 wpa_ssid_txt(ssid, ssid_len));
855
856 params->ssids[params->num_ssids].ssid = ssid;
857 params->ssids[params->num_ssids].ssid_len = ssid_len;
858 params->num_ssids++;
859}
860
861
862static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
863 struct wpa_driver_scan_params *params,
864 struct wpa_ssid *ssid, size_t max_ssids)
865{
866#ifdef CONFIG_OWE
867 struct wpa_bss *bss;
868
869 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
870 return;
871
872 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
873 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
874
875 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
876 const u8 *owe, *pos, *end;
877 const u8 *owe_ssid;
878 size_t owe_ssid_len;
879
880 if (bss->ssid_len != ssid->ssid_len ||
881 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
882 continue;
883
884 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
885 if (!owe || owe[1] < 4)
886 continue;
887
888 pos = owe + 6;
889 end = owe + 2 + owe[1];
890
891 /* Must include BSSID and ssid_len */
892 if (end - pos < ETH_ALEN + 1)
893 return;
894
895 /* Skip BSSID */
896 pos += ETH_ALEN;
897 owe_ssid_len = *pos++;
898 owe_ssid = pos;
899
900 if ((size_t) (end - pos) < owe_ssid_len ||
901 owe_ssid_len > SSID_MAX_LEN)
902 return;
903
904 wpa_printf(MSG_DEBUG,
905 "OWE: scan_ssids: transition mode OWE ssid=%s",
906 wpa_ssid_txt(owe_ssid, owe_ssid_len));
907
908 wpa_add_scan_ssid(wpa_s, params, max_ssids,
909 owe_ssid, owe_ssid_len);
910 return;
911 }
912#endif /* CONFIG_OWE */
913}
914
915
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700916static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
917 struct wpa_driver_scan_params *params,
918 size_t max_ssids)
919{
920 unsigned int i;
921 struct wpa_ssid *ssid;
922
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700923 /*
924 * For devices with max_ssids greater than 1, leave the last slot empty
925 * for adding the wildcard scan entry.
926 */
927 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
928
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700929 for (i = 0; i < wpa_s->scan_id_count; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700930 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800931 if (!ssid)
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700932 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800933 if (ssid->scan_ssid)
934 wpa_add_scan_ssid(wpa_s, params, max_ssids,
935 ssid->ssid, ssid->ssid_len);
936 /*
937 * Also add the SSID of the OWE BSS, to allow discovery of
938 * transition mode APs more quickly.
939 */
940 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700941 }
942
943 wpa_s->scan_id_count = 0;
944}
945
946
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700947static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
948 struct wpa_driver_scan_params *params,
949 size_t max_ssids)
950{
951 unsigned int i;
952
953 if (wpa_s->ssids_from_scan_req == NULL ||
954 wpa_s->num_ssids_from_scan_req == 0)
955 return 0;
956
957 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
958 wpa_s->num_ssids_from_scan_req = max_ssids;
959 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
960 (unsigned int) max_ssids);
961 }
962
963 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
964 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
965 params->ssids[i].ssid_len =
966 wpa_s->ssids_from_scan_req[i].ssid_len;
967 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
968 params->ssids[i].ssid,
969 params->ssids[i].ssid_len);
970 }
971
972 params->num_ssids = wpa_s->num_ssids_from_scan_req;
973 wpa_s->num_ssids_from_scan_req = 0;
974 return 1;
975}
976
977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
979{
980 struct wpa_supplicant *wpa_s = eloop_ctx;
981 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800982 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700985 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800987 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988
Dmitry Shmidt29333592017-01-09 12:27:11 -0800989 wpa_s->ignore_post_flush_scan_res = 0;
990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
992 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
993 return;
994 }
995
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800996 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700997 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
999 return;
1000 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001001
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -07001002 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001003 /*
1004 * If we are already in scanning state, we shall reschedule the
1005 * the incoming scan request.
1006 */
1007 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
1008 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -07001009 return;
1010 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001011
Dmitry Shmidt04949592012-07-19 12:16:46 -07001012 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001013 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
1015 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1016 return;
1017 }
1018
1019 if (wpa_s->conf->ap_scan != 0 &&
1020 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
1021 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
1022 "overriding ap_scan configuration");
1023 wpa_s->conf->ap_scan = 0;
1024 wpas_notify_ap_scan_changed(wpa_s);
1025 }
1026
1027 if (wpa_s->conf->ap_scan == 0) {
1028 wpa_supplicant_gen_assoc_event(wpa_s);
1029 return;
1030 }
1031
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001032 ssid = NULL;
1033 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
1034 wpa_s->connect_without_scan) {
1035 connect_without_scan = 1;
1036 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1037 if (ssid == wpa_s->connect_without_scan)
1038 break;
1039 }
1040 }
1041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001042 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001043 if (p2p_in_prog && p2p_in_prog != 2 &&
1044 (!ssid ||
1045 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001046 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
1047 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001048 return;
1049 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001050
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001051 /*
1052 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
1053 * used for changing modes inside wpa_supplicant (roaming,
1054 * auto-reconnect, etc). Discarding the scan might hurt these processes.
1055 * The normal use case for PNO is to suspend the host immediately after
1056 * starting PNO, so the periodic 100 ms attempts to run the scan do not
1057 * normally happen in practice multiple times, i.e., this is simply
1058 * restarting scanning once the host is woken up and PNO stopped.
1059 */
1060 if (wpa_s->pno || wpa_s->pno_sched_pending) {
1061 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
1062 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1063 return;
1064 }
1065
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001066 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067 max_ssids = 1;
1068 else {
1069 max_ssids = wpa_s->max_scan_ssids;
1070 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1071 max_ssids = WPAS_MAX_SCAN_SSIDS;
1072 }
1073
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001074 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001075 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001077 if (connect_without_scan) {
1078 wpa_s->connect_without_scan = NULL;
1079 if (ssid) {
1080 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1081 "without scan step");
1082 wpa_supplicant_associate(wpa_s, NULL, ssid);
1083 return;
1084 }
1085 }
1086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 os_memset(&params, 0, sizeof(params));
1088
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001089 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1091 wpa_s->wpa_state == WPA_INACTIVE)
1092 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1093
Dmitry Shmidt04949592012-07-19 12:16:46 -07001094 /*
1095 * If autoscan has set its own scanning parameters
1096 */
1097 if (wpa_s->autoscan_params != NULL) {
1098 scan_params = wpa_s->autoscan_params;
1099 goto scan;
1100 }
1101
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001102 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1103 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1104 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1105 goto ssid_list_set;
1106 }
1107
Dmitry Shmidt04949592012-07-19 12:16:46 -07001108#ifdef CONFIG_P2P
1109 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001110 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001111 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1112 wpa_s->p2p_in_provisioning,
1113 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001114 params.ssids[0].ssid = wpa_s->go_params->ssid;
1115 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1116 params.num_ssids = 1;
1117 goto ssid_list_set;
1118 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07001119
1120 if (wpa_s->p2p_in_invitation) {
1121 if (wpa_s->current_ssid) {
1122 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1123 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1124 params.ssids[0].ssid_len =
1125 wpa_s->current_ssid->ssid_len;
1126 params.num_ssids = 1;
1127 } else {
1128 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1129 }
1130 goto ssid_list_set;
1131 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001132#endif /* CONFIG_P2P */
1133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 /* Find the starting point from which to continue scanning */
1135 ssid = wpa_s->conf->ssid;
1136 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1137 while (ssid) {
1138 if (ssid == wpa_s->prev_scan_ssid) {
1139 ssid = ssid->next;
1140 break;
1141 }
1142 ssid = ssid->next;
1143 }
1144 }
1145
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001146 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001147#ifdef CONFIG_AP
1148 !wpa_s->ap_iface &&
1149#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001150 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001151 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001152 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 wpa_supplicant_assoc_try(wpa_s, ssid);
1154 return;
1155 } else if (wpa_s->conf->ap_scan == 2) {
1156 /*
1157 * User-initiated scan request in ap_scan == 2; scan with
1158 * wildcard SSID.
1159 */
1160 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001161 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1162 /*
1163 * Perform single-channel single-SSID scan for
1164 * reassociate-to-same-BSS operation.
1165 */
1166 /* Setup SSID */
1167 ssid = wpa_s->current_ssid;
1168 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1169 ssid->ssid, ssid->ssid_len);
1170 params.ssids[0].ssid = ssid->ssid;
1171 params.ssids[0].ssid_len = ssid->ssid_len;
1172 params.num_ssids = 1;
1173
1174 /*
1175 * Allocate memory for frequency array, allocate one extra
1176 * slot for the zero-terminator.
1177 */
1178 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001179 if (params.freqs) {
1180 params.freqs[0] = wpa_s->assoc_freq;
1181 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001182 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07001183
1184 /*
1185 * Reset the reattach flag so that we fall back to full scan if
1186 * this scan fails.
1187 */
1188 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 } else {
1190 struct wpa_ssid *start = ssid, *tssid;
1191 int freqs_set = 0;
1192 if (ssid == NULL && max_ssids > 1)
1193 ssid = wpa_s->conf->ssid;
1194 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001195 if (!wpas_network_disabled(wpa_s, ssid) &&
1196 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1198 ssid->ssid, ssid->ssid_len);
1199 params.ssids[params.num_ssids].ssid =
1200 ssid->ssid;
1201 params.ssids[params.num_ssids].ssid_len =
1202 ssid->ssid_len;
1203 params.num_ssids++;
1204 if (params.num_ssids + 1 >= max_ssids)
1205 break;
1206 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001207
1208 if (!wpas_network_disabled(wpa_s, ssid)) {
1209 /*
1210 * Also add the SSID of the OWE BSS, to allow
1211 * discovery of transition mode APs more
1212 * quickly.
1213 */
1214 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1215 max_ssids);
1216 }
1217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 ssid = ssid->next;
1219 if (ssid == start)
1220 break;
1221 if (ssid == NULL && max_ssids > 1 &&
1222 start != wpa_s->conf->ssid)
1223 ssid = wpa_s->conf->ssid;
1224 }
1225
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001226 if (wpa_s->scan_id_count &&
1227 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1228 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1229
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001230 for (tssid = wpa_s->conf->ssid;
1231 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1232 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001233 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07001235 if (((params.freqs || !freqs_set) &&
1236 tssid->scan_freq) &&
1237 int_array_len(params.freqs) < 100) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 int_array_concat(&params.freqs,
1239 tssid->scan_freq);
1240 } else {
1241 os_free(params.freqs);
1242 params.freqs = NULL;
1243 }
1244 freqs_set = 1;
1245 }
1246 int_array_sort_unique(params.freqs);
1247 }
1248
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001249 if (ssid && max_ssids == 1) {
1250 /*
1251 * If the driver is limited to 1 SSID at a time interleave
1252 * wildcard SSID scans with specific SSID scans to avoid
1253 * waiting a long time for a wildcard scan.
1254 */
1255 if (!wpa_s->prev_scan_wildcard) {
1256 params.ssids[0].ssid = NULL;
1257 params.ssids[0].ssid_len = 0;
1258 wpa_s->prev_scan_wildcard = 1;
1259 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1260 "wildcard SSID (Interleave with specific)");
1261 } else {
1262 wpa_s->prev_scan_ssid = ssid;
1263 wpa_s->prev_scan_wildcard = 0;
1264 wpa_dbg(wpa_s, MSG_DEBUG,
1265 "Starting AP scan for specific SSID: %s",
1266 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001268 } else if (ssid) {
1269 /* max_ssids > 1 */
1270
1271 wpa_s->prev_scan_ssid = ssid;
1272 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1273 "the scan request");
1274 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001275 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1276 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1277 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001278 } else if (wpa_s->conf->passive_scan) {
1279 wpa_dbg(wpa_s, MSG_DEBUG,
1280 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 } else {
1282 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1283 params.num_ssids++;
1284 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1285 "SSID");
1286 }
1287
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001288ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001289 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001290 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001292 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001293 wpa_s->manual_scan_only_new) {
1294 wpa_printf(MSG_DEBUG,
1295 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001296 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001297 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001298
1299 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1300 wpa_s->manual_scan_freqs) {
1301 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1302 params.freqs = wpa_s->manual_scan_freqs;
1303 wpa_s->manual_scan_freqs = NULL;
1304 }
1305
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001306 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1307 wpa_dbg(wpa_s, MSG_DEBUG,
1308 "Limit select_network scan to specified channels");
1309 params.freqs = wpa_s->select_network_scan_freqs;
1310 wpa_s->select_network_scan_freqs = NULL;
1311 }
1312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1314 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1315 "generated frequency list");
1316 params.freqs = wpa_s->next_scan_freqs;
1317 } else
1318 os_free(wpa_s->next_scan_freqs);
1319 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001320 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001322 /* See if user specified frequencies. If so, scan only those. */
Hai Shalom60840252021-02-19 19:02:11 -08001323 if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1324 wpa_s->conf->initial_freq_list && !params.freqs) {
1325 wpa_dbg(wpa_s, MSG_DEBUG,
1326 "Optimize scan based on conf->initial_freq_list");
1327 int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1328 } else if (wpa_s->conf->freq_list && !params.freqs) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001329 wpa_dbg(wpa_s, MSG_DEBUG,
1330 "Optimize scan based on conf->freq_list");
1331 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1332 }
1333
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001334 /* Use current associated channel? */
1335 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001336 unsigned int num = wpa_s->num_multichan_concurrent;
1337
1338 params.freqs = os_calloc(num + 1, sizeof(int));
1339 if (params.freqs) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001340 num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1341 false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001342 if (num > 0) {
1343 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1344 "current operating channels since "
1345 "scan_cur_freq is enabled");
1346 } else {
1347 os_free(params.freqs);
1348 params.freqs = NULL;
1349 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001350 }
1351 }
1352
Hai Shalomce48b4a2018-09-05 11:41:35 -07001353#ifdef CONFIG_MBO
1354 if (wpa_s->enable_oce & OCE_STA)
1355 params.oce_scan = 1;
1356#endif /* CONFIG_MBO */
1357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1359 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001360 if (extra_ie) {
1361 params.extra_ies = wpabuf_head(extra_ie);
1362 params.extra_ies_len = wpabuf_len(extra_ie);
1363 }
1364
1365#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001366 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001367 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001368 /*
1369 * The interface may not yet be in P2P mode, so we have to
1370 * explicitly request P2P probe to disable CCK rates.
1371 */
1372 params.p2p_probe = 1;
1373 }
1374#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001376 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001377 wpa_s->wpa_state <= WPA_SCANNING)
1378 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001379
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001380 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1381 struct wpa_bss *bss;
1382
1383 params.bssid = wpa_s->next_scan_bssid;
1384 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
Hai Shalomfdcde762020-04-02 11:19:20 -07001385 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1386 bss && bss->ssid_len && params.num_ssids == 1 &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001387 params.ssids[0].ssid_len == 0) {
1388 params.ssids[0].ssid = bss->ssid;
1389 params.ssids[0].ssid_len = bss->ssid_len;
1390 wpa_dbg(wpa_s, MSG_DEBUG,
1391 "Scan a previously specified BSSID " MACSTR
1392 " and SSID %s",
1393 MAC2STR(params.bssid),
1394 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1395 } else {
1396 wpa_dbg(wpa_s, MSG_DEBUG,
1397 "Scan a previously specified BSSID " MACSTR,
1398 MAC2STR(params.bssid));
1399 }
1400 }
1401
Sunil8cd6f4d2022-06-28 18:40:46 +00001402 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1403 wpa_s->manual_non_coloc_6ghz) {
1404 wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1405 params.non_coloc_6ghz = 1;
1406 }
1407
Dmitry Shmidt04949592012-07-19 12:16:46 -07001408 scan_params = &params;
1409
1410scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001411#ifdef CONFIG_P2P
1412 /*
1413 * If the driver does not support multi-channel concurrency and a
1414 * virtual interface that shares the same radio with the wpa_s interface
1415 * is operating there may not be need to scan other channels apart from
1416 * the current operating channel on the other virtual interface. Filter
1417 * out other channels in case we are trying to find a connection for a
1418 * station interface when we are not configured to prefer station
1419 * connection and a concurrent operation is already in process.
1420 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001421 if (wpa_s->scan_for_connection &&
1422 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001423 !scan_params->freqs && !params.freqs &&
1424 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001425 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1426 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001427 unsigned int num = wpa_s->num_multichan_concurrent;
1428
1429 params.freqs = os_calloc(num + 1, sizeof(int));
1430 if (params.freqs) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001431 /*
1432 * Exclude the operating frequency of the current
1433 * interface since we're looking to transition off of
1434 * it.
1435 */
1436 num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1437 true);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001438 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1439 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1440 } else {
1441 os_free(params.freqs);
1442 params.freqs = NULL;
1443 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001444 }
1445 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001446
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001447 if (!params.freqs && is_6ghz_supported(wpa_s) &&
1448 (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning))
1449 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001450#endif /* CONFIG_P2P */
1451
Dmitry Shmidt04949592012-07-19 12:16:46 -07001452 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001454 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1455 !wpa_s->manual_scan_freqs) {
1456 /* Restore manual_scan_freqs for the next attempt */
1457 wpa_s->manual_scan_freqs = params.freqs;
1458 params.freqs = NULL;
1459 }
1460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001461 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 os_free(params.freqs);
1463 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001464 os_free(params.mac_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465
1466 if (ret) {
1467 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001468 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1469 wpa_supplicant_set_state(wpa_s,
1470 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001471 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001472 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001474 } else {
1475 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001476#ifdef CONFIG_INTERWORKING
1477 wpa_s->interworking_fast_assoc_tried = 0;
1478#endif /* CONFIG_INTERWORKING */
Hai Shalomfdcde762020-04-02 11:19:20 -07001479 wpa_s->next_scan_bssid_wildcard_ssid = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001480 if (params.bssid)
1481 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 }
1483}
1484
1485
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001486void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1487{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001488 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001489 int cancelled;
1490
1491 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1492 &remaining);
1493
1494 new_int.sec = sec;
1495 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001496 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001497 new_int.sec = remaining.sec;
1498 new_int.usec = remaining.usec;
1499 }
1500
Dmitry Shmidt051af732013-10-22 13:52:46 -07001501 if (cancelled) {
1502 eloop_register_timeout(new_int.sec, new_int.usec,
1503 wpa_supplicant_scan, wpa_s, NULL);
1504 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001505 wpa_s->scan_interval = sec;
1506}
1507
1508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509/**
1510 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1511 * @wpa_s: Pointer to wpa_supplicant data
1512 * @sec: Number of seconds after which to scan
1513 * @usec: Number of microseconds after which to scan
1514 *
1515 * This function is used to schedule a scan for neighboring access points after
1516 * the specified time.
1517 */
1518void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1519{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001520 int res;
1521
1522 if (wpa_s->p2p_mgmt) {
1523 wpa_dbg(wpa_s, MSG_DEBUG,
1524 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1525 sec, usec);
1526 return;
1527 }
1528
1529 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1530 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001531 if (res == 1) {
1532 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001533 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001534 } else if (res == 0) {
1535 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1536 sec, usec);
1537 } else {
1538 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1539 sec, usec);
1540 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542}
1543
1544
1545/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001546 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1547 * @wpa_s: Pointer to wpa_supplicant data
1548 * @sec: Number of seconds after which to scan
1549 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001550 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551 *
1552 * This function is used to schedule periodic scans for neighboring
1553 * access points after the specified time.
1554 */
1555int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1556 int sec, int usec)
1557{
1558 if (!wpa_s->sched_scan_supported)
1559 return -1;
1560
1561 eloop_register_timeout(sec, usec,
1562 wpa_supplicant_delayed_sched_scan_timeout,
1563 wpa_s, NULL);
1564
1565 return 0;
1566}
1567
1568
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001569static void
1570wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1571 struct wpa_driver_scan_params *params)
1572{
1573 if (wpa_s->wpa_state != WPA_COMPLETED ||
1574 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1575 wpa_s->srp.relative_rssi_set == 0)
1576 return;
1577
1578 params->relative_rssi_set = 1;
1579 params->relative_rssi = wpa_s->srp.relative_rssi;
1580
1581 if (wpa_s->srp.relative_adjust_rssi == 0)
1582 return;
1583
1584 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1585 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1586}
1587
1588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001589/**
1590 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1591 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001592 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 *
1594 * This function is used to schedule periodic scans for neighboring
1595 * access points repeating the scan continuously.
1596 */
1597int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1598{
1599 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001600 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001601 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001602 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001603 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 int ret;
1605 unsigned int max_sched_scan_ssids;
1606 int wildcard = 0;
1607 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001608 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001609
1610 if (!wpa_s->sched_scan_supported)
1611 return -1;
1612
1613 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1614 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1615 else
1616 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001617 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001618 return -1;
1619
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001620 wpa_s->sched_scan_stop_req = 0;
1621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001622 if (wpa_s->sched_scanning) {
1623 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1624 return 0;
1625 }
1626
1627 need_ssids = 0;
1628 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001629 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001630 /* Use wildcard SSID to find this network */
1631 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001632 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1633 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001634 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001635
1636#ifdef CONFIG_WPS
1637 if (!wpas_network_disabled(wpa_s, ssid) &&
1638 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1639 /*
1640 * Normal scan is more reliable and faster for WPS
1641 * operations and since these are for short periods of
1642 * time, the benefit of trying to use sched_scan would
1643 * be limited.
1644 */
1645 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1646 "sched_scan for WPS");
1647 return -1;
1648 }
1649#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001650 }
1651 if (wildcard)
1652 need_ssids++;
1653
1654 if (wpa_s->normal_scans < 3 &&
1655 (need_ssids <= wpa_s->max_scan_ssids ||
1656 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1657 /*
1658 * When normal scan can speed up operations, use that for the
1659 * first operations before starting the sched_scan to allow
1660 * user space sleep more. We do this only if the normal scan
1661 * has functionality that is suitable for this or if the
1662 * sched_scan does not have better support for multiple SSIDs.
1663 */
1664 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1665 "sched_scan for initial scans (normal_scans=%d)",
1666 wpa_s->normal_scans);
1667 return -1;
1668 }
1669
1670 os_memset(&params, 0, sizeof(params));
1671
1672 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001673 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001674 sizeof(struct wpa_driver_scan_filter));
1675
1676 prev_state = wpa_s->wpa_state;
1677 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1678 wpa_s->wpa_state == WPA_INACTIVE)
1679 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1680
Dmitry Shmidt04949592012-07-19 12:16:46 -07001681 if (wpa_s->autoscan_params != NULL) {
1682 scan_params = wpa_s->autoscan_params;
1683 goto scan;
1684 }
1685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001686 /* Find the starting point from which to continue scanning */
1687 ssid = wpa_s->conf->ssid;
1688 if (wpa_s->prev_sched_ssid) {
1689 while (ssid) {
1690 if (ssid == wpa_s->prev_sched_ssid) {
1691 ssid = ssid->next;
1692 break;
1693 }
1694 ssid = ssid->next;
1695 }
1696 }
1697
1698 if (!ssid || !wpa_s->prev_sched_ssid) {
1699 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001700 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1701 wpa_s->first_sched_scan = 1;
1702 ssid = wpa_s->conf->ssid;
1703 wpa_s->prev_sched_ssid = ssid;
1704 }
1705
1706 if (wildcard) {
1707 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1708 params.num_ssids++;
1709 }
1710
1711 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001712 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713 goto next;
1714
1715 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1716 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1717 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1718 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1719 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1720 ssid->ssid, ssid->ssid_len);
1721 params.filter_ssids[params.num_filter_ssids].ssid_len =
1722 ssid->ssid_len;
1723 params.num_filter_ssids++;
1724 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1725 {
1726 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1727 "filter for sched_scan - drop filter");
1728 os_free(params.filter_ssids);
1729 params.filter_ssids = NULL;
1730 params.num_filter_ssids = 0;
1731 }
1732
1733 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1734 if (params.num_ssids == max_sched_scan_ssids)
1735 break; /* only room for broadcast SSID */
1736 wpa_dbg(wpa_s, MSG_DEBUG,
1737 "add to active scan ssid: %s",
1738 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1739 params.ssids[params.num_ssids].ssid =
1740 ssid->ssid;
1741 params.ssids[params.num_ssids].ssid_len =
1742 ssid->ssid_len;
1743 params.num_ssids++;
1744 if (params.num_ssids >= max_sched_scan_ssids) {
1745 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001746 do {
1747 ssid = ssid->next;
1748 } while (ssid &&
1749 (wpas_network_disabled(wpa_s, ssid) ||
1750 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001751 break;
1752 }
1753 }
1754
1755 next:
1756 wpa_s->prev_sched_ssid = ssid;
1757 ssid = ssid->next;
1758 }
1759
1760 if (params.num_filter_ssids == 0) {
1761 os_free(params.filter_ssids);
1762 params.filter_ssids = NULL;
1763 }
1764
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001765 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1766 if (extra_ie) {
1767 params.extra_ies = wpabuf_head(extra_ie);
1768 params.extra_ies_len = wpabuf_len(extra_ie);
1769 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001770
Dmitry Shmidt18463232014-01-24 12:29:41 -08001771 if (wpa_s->conf->filter_rssi)
1772 params.filter_rssi = wpa_s->conf->filter_rssi;
1773
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001774 /* See if user specified frequencies. If so, scan only those. */
1775 if (wpa_s->conf->freq_list && !params.freqs) {
1776 wpa_dbg(wpa_s, MSG_DEBUG,
1777 "Optimize scan based on conf->freq_list");
1778 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1779 }
1780
Hai Shalomce48b4a2018-09-05 11:41:35 -07001781#ifdef CONFIG_MBO
1782 if (wpa_s->enable_oce & OCE_STA)
1783 params.oce_scan = 1;
1784#endif /* CONFIG_MBO */
1785
Dmitry Shmidt04949592012-07-19 12:16:46 -07001786 scan_params = &params;
1787
1788scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001789 wpa_s->sched_scan_timed_out = 0;
1790
1791 /*
1792 * We cannot support multiple scan plans if the scan request includes
1793 * too many SSID's, so in this case use only the last scan plan and make
1794 * it run infinitely. It will be stopped by the timeout.
1795 */
1796 if (wpa_s->sched_scan_plans_num == 1 ||
1797 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1798 params.sched_scan_plans = wpa_s->sched_scan_plans;
1799 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1800 } else if (wpa_s->sched_scan_plans_num > 1) {
1801 wpa_dbg(wpa_s, MSG_DEBUG,
1802 "Too many SSIDs. Default to using single scheduled_scan plan");
1803 params.sched_scan_plans =
1804 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1805 1];
1806 params.sched_scan_plans_num = 1;
1807 } else {
1808 if (wpa_s->conf->sched_scan_interval)
1809 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1810 else
1811 scan_plan.interval = 10;
1812
1813 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1814 wpa_printf(MSG_WARNING,
1815 "Scan interval too long(%u), use the maximum allowed(%u)",
1816 scan_plan.interval,
1817 wpa_s->max_sched_scan_plan_interval);
1818 scan_plan.interval =
1819 wpa_s->max_sched_scan_plan_interval;
1820 }
1821
1822 scan_plan.iterations = 0;
1823 params.sched_scan_plans = &scan_plan;
1824 params.sched_scan_plans_num = 1;
1825 }
1826
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001827 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1828
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001829 if (ssid || !wpa_s->first_sched_scan) {
1830 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001831 "Starting sched scan after %u seconds: interval %u timeout %d",
1832 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001833 params.sched_scan_plans[0].interval,
1834 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001835 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001836 wpa_dbg(wpa_s, MSG_DEBUG,
1837 "Starting sched scan after %u seconds (no timeout)",
1838 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839 }
1840
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001841 wpa_setband_scan_freqs(wpa_s, scan_params);
1842
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001843 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001844 wpa_s->wpa_state <= WPA_SCANNING)
1845 wpa_setup_mac_addr_rand_params(&params,
1846 wpa_s->mac_addr_sched_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001847
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001848 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1849
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001850 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001851 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001853 os_free(params.mac_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001854 if (ret) {
1855 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1856 if (prev_state != wpa_s->wpa_state)
1857 wpa_supplicant_set_state(wpa_s, prev_state);
1858 return ret;
1859 }
1860
1861 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1862 if (ssid || !wpa_s->first_sched_scan) {
1863 wpa_s->sched_scan_timed_out = 0;
1864 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1865 wpa_supplicant_sched_scan_timeout,
1866 wpa_s, NULL);
1867 wpa_s->first_sched_scan = 0;
1868 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001869 params.sched_scan_plans[0].interval *= 2;
1870 if ((unsigned int) wpa_s->sched_scan_timeout <
1871 params.sched_scan_plans[0].interval ||
1872 params.sched_scan_plans[0].interval >
1873 wpa_s->max_sched_scan_plan_interval) {
1874 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001875 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1876 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877 }
1878
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001879 /* If there is no more ssids, start next time from the beginning */
1880 if (!ssid)
1881 wpa_s->prev_sched_ssid = NULL;
1882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883 return 0;
1884}
1885
1886
1887/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1889 * @wpa_s: Pointer to wpa_supplicant data
1890 *
1891 * This function is used to cancel a scan request scheduled with
1892 * wpa_supplicant_req_scan().
1893 */
1894void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1895{
1896 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1897 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898}
1899
1900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001901/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001902 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1903 * @wpa_s: Pointer to wpa_supplicant data
1904 *
1905 * This function is used to stop a delayed scheduled scan.
1906 */
1907void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1908{
1909 if (!wpa_s->sched_scan_supported)
1910 return;
1911
1912 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1913 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1914 wpa_s, NULL);
1915}
1916
1917
1918/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001919 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1920 * @wpa_s: Pointer to wpa_supplicant data
1921 *
1922 * This function is used to stop a periodic scheduled scan.
1923 */
1924void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1925{
1926 if (!wpa_s->sched_scanning)
1927 return;
1928
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001929 if (wpa_s->sched_scanning)
1930 wpa_s->sched_scan_stop_req = 1;
1931
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001932 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1933 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1934 wpa_supplicant_stop_sched_scan(wpa_s);
1935}
1936
1937
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001938/**
1939 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1940 * @wpa_s: Pointer to wpa_supplicant data
1941 * @scanning: Whether scanning is currently in progress
1942 *
1943 * This function is to generate scanning notifycations. It is called whenever
1944 * there may have been a change in scanning (scan started, completed, stopped).
1945 * wpas_notify_scanning() is called whenever the scanning state changed from the
1946 * previously notified state.
1947 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1949 int scanning)
1950{
1951 if (wpa_s->scanning != scanning) {
1952 wpa_s->scanning = scanning;
1953 wpas_notify_scanning(wpa_s);
1954 }
1955}
1956
1957
1958static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1959{
1960 int rate = 0;
1961 const u8 *ie;
1962 int i;
1963
1964 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1965 for (i = 0; ie && i < ie[1]; i++) {
1966 if ((ie[i + 2] & 0x7f) > rate)
1967 rate = ie[i + 2] & 0x7f;
1968 }
1969
1970 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1971 for (i = 0; ie && i < ie[1]; i++) {
1972 if ((ie[i + 2] & 0x7f) > rate)
1973 rate = ie[i + 2] & 0x7f;
1974 }
1975
1976 return rate;
1977}
1978
1979
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001980/**
1981 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1982 * @res: Scan result entry
1983 * @ie: Information element identitifier (WLAN_EID_*)
1984 * Returns: Pointer to the information element (id field) or %NULL if not found
1985 *
1986 * This function returns the first matching information element in the scan
1987 * result.
1988 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1990{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001991 size_t ie_len = res->ie_len;
1992
1993 /* Use the Beacon frame IEs if res->ie_len is not available */
1994 if (!ie_len)
1995 ie_len = res->beacon_ie_len;
1996
1997 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998}
1999
2000
Sunil Ravi89eba102022-09-13 21:04:37 -07002001const u8 * wpa_scan_get_ml_ie(const struct wpa_scan_res *res, u8 type)
2002{
2003 size_t ie_len = res->ie_len;
2004
2005 /* Use the Beacon frame IEs if res->ie_len is not available */
2006 if (!ie_len)
2007 ie_len = res->beacon_ie_len;
2008
2009 return get_ml_ie((const u8 *) (res + 1), ie_len, type);
2010}
2011
2012
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002013/**
2014 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
2015 * @res: Scan result entry
2016 * @vendor_type: Vendor type (four octets starting the IE payload)
2017 * Returns: Pointer to the information element (id field) or %NULL if not found
2018 *
2019 * This function returns the first matching information element in the scan
2020 * result.
2021 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
2023 u32 vendor_type)
2024{
Hai Shalom60840252021-02-19 19:02:11 -08002025 const u8 *ies;
2026 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027
Hai Shalom60840252021-02-19 19:02:11 -08002028 ies = (const u8 *) (res + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029
Hai Shalom60840252021-02-19 19:02:11 -08002030 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
2031 if (elem->datalen >= 4 &&
2032 vendor_type == WPA_GET_BE32(elem->data))
2033 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034 }
2035
2036 return NULL;
2037}
2038
2039
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002040/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07002041 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
2042 * @res: Scan result entry
2043 * @vendor_type: Vendor type (four octets starting the IE payload)
2044 * Returns: Pointer to the information element (id field) or %NULL if not found
2045 *
2046 * This function returns the first matching information element in the scan
2047 * result.
2048 *
2049 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
2050 * from Beacon frames instead of either Beacon or Probe Response frames.
2051 */
2052const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
2053 u32 vendor_type)
2054{
Hai Shalom60840252021-02-19 19:02:11 -08002055 const u8 *ies;
2056 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002057
2058 if (res->beacon_ie_len == 0)
2059 return NULL;
2060
Hai Shalom60840252021-02-19 19:02:11 -08002061 ies = (const u8 *) (res + 1);
2062 ies += res->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002063
Hai Shalom60840252021-02-19 19:02:11 -08002064 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
2065 res->beacon_ie_len) {
2066 if (elem->datalen >= 4 &&
2067 vendor_type == WPA_GET_BE32(elem->data))
2068 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002069 }
2070
2071 return NULL;
2072}
2073
2074
2075/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002076 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2077 * @res: Scan result entry
2078 * @vendor_type: Vendor type (four octets starting the IE payload)
2079 * Returns: Pointer to the information element payload or %NULL if not found
2080 *
2081 * This function returns concatenated payload of possibly fragmented vendor
2082 * specific information elements in the scan result. The caller is responsible
2083 * for freeing the returned buffer.
2084 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2086 u32 vendor_type)
2087{
2088 struct wpabuf *buf;
2089 const u8 *end, *pos;
2090
2091 buf = wpabuf_alloc(res->ie_len);
2092 if (buf == NULL)
2093 return NULL;
2094
2095 pos = (const u8 *) (res + 1);
2096 end = pos + res->ie_len;
2097
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002098 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08002099 u8 ie, len;
2100
2101 ie = pos[0];
2102 len = pos[1];
2103 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 break;
Hai Shalom60840252021-02-19 19:02:11 -08002105 pos += 2;
2106 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2107 vendor_type == WPA_GET_BE32(pos))
2108 wpabuf_put_data(buf, pos + 4, len - 4);
2109 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 }
2111
2112 if (wpabuf_len(buf) == 0) {
2113 wpabuf_free(buf);
2114 buf = NULL;
2115 }
2116
2117 return buf;
2118}
2119
2120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121/* Compare function for sorting scan results. Return >0 if @b is considered
2122 * better. */
2123static int wpa_scan_result_compar(const void *a, const void *b)
2124{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002125#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 struct wpa_scan_res **_wa = (void *) a;
2127 struct wpa_scan_res **_wb = (void *) b;
2128 struct wpa_scan_res *wa = *_wa;
2129 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002130 int wpa_a, wpa_b;
2131 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132
2133 /* WPA/WPA2 support preferred */
2134 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2135 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2136 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2137 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2138
2139 if (wpa_b && !wpa_a)
2140 return 1;
2141 if (!wpa_b && wpa_a)
2142 return -1;
2143
2144 /* privacy support preferred */
2145 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2146 (wb->caps & IEEE80211_CAP_PRIVACY))
2147 return 1;
2148 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2149 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2150 return -1;
2151
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002152 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002153 snr_a_full = wa->snr;
2154 snr_a = MIN(wa->snr, GREAT_SNR);
2155 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002156 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002157 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002158 /* Level is not in dBm, so we can't calculate
2159 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002160 snr_a = snr_a_full = wa->level;
2161 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002162 }
2163
Hai Shaloma20dcd72022-02-04 13:43:00 -08002164 /* If SNR is close, decide by max rate or frequency band. For cases
2165 * involving the 6 GHz band, use the throughput estimate irrespective
2166 * of the SNR difference since the LPI/VLP rules may result in
2167 * significant differences in SNR for cases where the estimated
2168 * throughput can be considerably higher with the lower SNR. */
2169 if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2170 is_6ghz_freq(wa->freq) ||
2171 is_6ghz_freq(wb->freq))) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002172 if (wa->est_throughput != wb->est_throughput)
Hai Shalom021b0b52019-04-10 11:17:58 -07002173 return (int) wb->est_throughput -
2174 (int) wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002175 }
2176 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2177 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002178 if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2179 return is_6ghz_freq(wa->freq) ? -1 : 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002180 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2181 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 }
2183
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002184 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 * identical, use quality values since some drivers may only report
2186 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002187 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002189 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002190#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191}
2192
2193
2194#ifdef CONFIG_WPS
2195/* Compare function for sorting scan results when searching a WPS AP for
2196 * provisioning. Return >0 if @b is considered better. */
2197static int wpa_scan_result_wps_compar(const void *a, const void *b)
2198{
2199 struct wpa_scan_res **_wa = (void *) a;
2200 struct wpa_scan_res **_wb = (void *) b;
2201 struct wpa_scan_res *wa = *_wa;
2202 struct wpa_scan_res *wb = *_wb;
2203 int uses_wps_a, uses_wps_b;
2204 struct wpabuf *wps_a, *wps_b;
2205 int res;
2206
2207 /* Optimization - check WPS IE existence before allocated memory and
2208 * doing full reassembly. */
2209 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2210 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2211 if (uses_wps_a && !uses_wps_b)
2212 return -1;
2213 if (!uses_wps_a && uses_wps_b)
2214 return 1;
2215
2216 if (uses_wps_a && uses_wps_b) {
2217 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2218 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2219 res = wps_ap_priority_compar(wps_a, wps_b);
2220 wpabuf_free(wps_a);
2221 wpabuf_free(wps_b);
2222 if (res)
2223 return res;
2224 }
2225
2226 /*
2227 * Do not use current AP security policy as a sorting criteria during
2228 * WPS provisioning step since the AP may get reconfigured at the
2229 * completion of provisioning.
2230 */
2231
2232 /* all things being equal, use signal level; if signal levels are
2233 * identical, use quality values since some drivers may only report
2234 * that value and leave the signal level zero */
2235 if (wb->level == wa->level)
2236 return wb->qual - wa->qual;
2237 return wb->level - wa->level;
2238}
2239#endif /* CONFIG_WPS */
2240
2241
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002242static void dump_scan_res(struct wpa_scan_results *scan_res)
2243{
2244#ifndef CONFIG_NO_STDOUT_DEBUG
2245 size_t i;
2246
2247 if (scan_res->res == NULL || scan_res->num == 0)
2248 return;
2249
2250 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2251
2252 for (i = 0; i < scan_res->num; i++) {
2253 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002254 u8 *pos;
Sunil Ravia04bd252022-05-02 22:54:18 -07002255 const u8 *ssid_ie, *ssid = NULL;
2256 size_t ssid_len = 0;
2257
2258 ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2259 if (ssid_ie) {
2260 ssid = ssid_ie + 2;
2261 ssid_len = ssid_ie[1];
2262 }
2263
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002264 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002265 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2266
Sunil Ravia04bd252022-05-02 22:54:18 -07002267 wpa_printf(MSG_EXCESSIVE, MACSTR
2268 " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2269 MAC2STR(r->bssid),
2270 wpa_ssid_txt(ssid, ssid_len),
2271 r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002272 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002273 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2274 r->flags,
2275 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002276 } else {
Sunil Ravia04bd252022-05-02 22:54:18 -07002277 wpa_printf(MSG_EXCESSIVE, MACSTR
2278 " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2279 MAC2STR(r->bssid),
2280 wpa_ssid_txt(ssid, ssid_len),
2281 r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002282 r->noise, r->level, r->flags, r->age,
2283 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002284 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002285 pos = (u8 *) (r + 1);
2286 if (r->ie_len)
2287 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2288 pos += r->ie_len;
2289 if (r->beacon_ie_len)
2290 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2291 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002292 }
2293#endif /* CONFIG_NO_STDOUT_DEBUG */
2294}
2295
2296
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002297/**
2298 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2299 * @wpa_s: Pointer to wpa_supplicant data
2300 * @bssid: BSSID to check
2301 * Returns: 0 if the BSSID is filtered or 1 if not
2302 *
2303 * This function is used to filter out specific BSSIDs from scan reslts mainly
2304 * for testing purposes (SET bssid_filter ctrl_iface command).
2305 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002306int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2307 const u8 *bssid)
2308{
2309 size_t i;
2310
2311 if (wpa_s->bssid_filter == NULL)
2312 return 1;
2313
2314 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2315 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2316 ETH_ALEN) == 0)
2317 return 1;
2318 }
2319
2320 return 0;
2321}
2322
2323
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002324void filter_scan_res(struct wpa_supplicant *wpa_s,
2325 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002326{
2327 size_t i, j;
2328
2329 if (wpa_s->bssid_filter == NULL)
2330 return;
2331
2332 for (i = 0, j = 0; i < res->num; i++) {
2333 if (wpa_supplicant_filter_bssid_match(wpa_s,
2334 res->res[i]->bssid)) {
2335 res->res[j++] = res->res[i];
2336 } else {
2337 os_free(res->res[i]);
2338 res->res[i] = NULL;
2339 }
2340 }
2341
2342 if (res->num != j) {
2343 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2344 (int) (res->num - j));
2345 res->num = j;
2346 }
2347}
2348
2349
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002350void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002351{
2352 if (res->flags & WPA_SCAN_NOISE_INVALID) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002353 res->noise = is_6ghz_freq(res->freq) ?
2354 DEFAULT_NOISE_FLOOR_6GHZ :
2355 (IS_5GHZ(res->freq) ?
2356 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002357 }
2358
2359 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2360 res->snr = res->level - res->noise;
2361 } else {
2362 /* Level is not in dBm, so we can't calculate
2363 * SNR. Just use raw level (units unknown). */
2364 res->snr = res->level;
2365 }
2366}
2367
2368
Hai Shalom899fcc72020-10-19 14:38:18 -07002369/* Minimum SNR required to achieve a certain bitrate. */
2370struct minsnr_bitrate_entry {
2371 int minsnr;
2372 unsigned int bitrate; /* in Mbps */
2373};
2374
2375/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2376static const int vht_mcs = 8;
2377
2378static const struct minsnr_bitrate_entry vht20_table[] = {
2379 { 0, 0 },
2380 { 2, 6500 }, /* HT20 MCS0 */
2381 { 5, 13000 }, /* HT20 MCS1 */
2382 { 9, 19500 }, /* HT20 MCS2 */
2383 { 11, 26000 }, /* HT20 MCS3 */
2384 { 15, 39000 }, /* HT20 MCS4 */
2385 { 18, 52000 }, /* HT20 MCS5 */
2386 { 20, 58500 }, /* HT20 MCS6 */
2387 { 25, 65000 }, /* HT20 MCS7 */
2388 { 29, 78000 }, /* VHT20 MCS8 */
2389 { -1, 78000 } /* SNR > 29 */
2390};
2391
2392static const struct minsnr_bitrate_entry vht40_table[] = {
2393 { 0, 0 },
2394 { 5, 13500 }, /* HT40 MCS0 */
2395 { 8, 27000 }, /* HT40 MCS1 */
2396 { 12, 40500 }, /* HT40 MCS2 */
2397 { 14, 54000 }, /* HT40 MCS3 */
2398 { 18, 81000 }, /* HT40 MCS4 */
2399 { 21, 108000 }, /* HT40 MCS5 */
2400 { 23, 121500 }, /* HT40 MCS6 */
2401 { 28, 135000 }, /* HT40 MCS7 */
2402 { 32, 162000 }, /* VHT40 MCS8 */
2403 { 34, 180000 }, /* VHT40 MCS9 */
2404 { -1, 180000 } /* SNR > 34 */
2405};
2406
2407static const struct minsnr_bitrate_entry vht80_table[] = {
2408 { 0, 0 },
2409 { 8, 29300 }, /* VHT80 MCS0 */
2410 { 11, 58500 }, /* VHT80 MCS1 */
2411 { 15, 87800 }, /* VHT80 MCS2 */
2412 { 17, 117000 }, /* VHT80 MCS3 */
2413 { 21, 175500 }, /* VHT80 MCS4 */
2414 { 24, 234000 }, /* VHT80 MCS5 */
2415 { 26, 263300 }, /* VHT80 MCS6 */
2416 { 31, 292500 }, /* VHT80 MCS7 */
2417 { 35, 351000 }, /* VHT80 MCS8 */
2418 { 37, 390000 }, /* VHT80 MCS9 */
2419 { -1, 390000 } /* SNR > 37 */
2420};
2421
2422
Hai Shaloma20dcd72022-02-04 13:43:00 -08002423static const struct minsnr_bitrate_entry vht160_table[] = {
2424 { 0, 0 },
2425 { 11, 58500 }, /* VHT160 MCS0 */
2426 { 14, 117000 }, /* VHT160 MCS1 */
2427 { 18, 175500 }, /* VHT160 MCS2 */
2428 { 20, 234000 }, /* VHT160 MCS3 */
2429 { 24, 351000 }, /* VHT160 MCS4 */
2430 { 27, 468000 }, /* VHT160 MCS5 */
2431 { 29, 526500 }, /* VHT160 MCS6 */
2432 { 34, 585000 }, /* VHT160 MCS7 */
2433 { 38, 702000 }, /* VHT160 MCS8 */
2434 { 40, 780000 }, /* VHT160 MCS9 */
2435 { -1, 780000 } /* SNR > 37 */
2436};
2437
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002438/* EHT needs to be enabled in order to achieve MCS12 and MCS13 rates. */
2439#define EHT_MCS 12
Hai Shaloma20dcd72022-02-04 13:43:00 -08002440
2441static const struct minsnr_bitrate_entry he20_table[] = {
2442 { 0, 0 },
2443 { 2, 8600 }, /* HE20 MCS0 */
2444 { 5, 17200 }, /* HE20 MCS1 */
2445 { 9, 25800 }, /* HE20 MCS2 */
2446 { 11, 34400 }, /* HE20 MCS3 */
2447 { 15, 51600 }, /* HE20 MCS4 */
2448 { 18, 68800 }, /* HE20 MCS5 */
2449 { 20, 77400 }, /* HE20 MCS6 */
2450 { 25, 86000 }, /* HE20 MCS7 */
2451 { 29, 103200 }, /* HE20 MCS8 */
2452 { 31, 114700 }, /* HE20 MCS9 */
2453 { 34, 129000 }, /* HE20 MCS10 */
2454 { 36, 143400 }, /* HE20 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002455 { 39, 154900 }, /* EHT20 MCS12 */
2456 { 42, 172100 }, /* EHT20 MCS13 */
2457 { -1, 172100 } /* SNR > 42 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002458};
2459
2460static const struct minsnr_bitrate_entry he40_table[] = {
2461 { 0, 0 },
2462 { 5, 17200 }, /* HE40 MCS0 */
2463 { 8, 34400 }, /* HE40 MCS1 */
2464 { 12, 51600 }, /* HE40 MCS2 */
2465 { 14, 68800 }, /* HE40 MCS3 */
2466 { 18, 103200 }, /* HE40 MCS4 */
2467 { 21, 137600 }, /* HE40 MCS5 */
2468 { 23, 154900 }, /* HE40 MCS6 */
2469 { 28, 172100 }, /* HE40 MCS7 */
2470 { 32, 206500 }, /* HE40 MCS8 */
2471 { 34, 229400 }, /* HE40 MCS9 */
2472 { 37, 258100 }, /* HE40 MCS10 */
2473 { 39, 286800 }, /* HE40 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002474 { 42, 309500 }, /* EHT40 MCS12 */
2475 { 45, 344100 }, /* EHT40 MCS13 */
2476 { -1, 344100 } /* SNR > 45 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002477};
2478
2479static const struct minsnr_bitrate_entry he80_table[] = {
2480 { 0, 0 },
2481 { 8, 36000 }, /* HE80 MCS0 */
2482 { 11, 72100 }, /* HE80 MCS1 */
2483 { 15, 108100 }, /* HE80 MCS2 */
2484 { 17, 144100 }, /* HE80 MCS3 */
2485 { 21, 216200 }, /* HE80 MCS4 */
2486 { 24, 288200 }, /* HE80 MCS5 */
2487 { 26, 324300 }, /* HE80 MCS6 */
2488 { 31, 360300 }, /* HE80 MCS7 */
2489 { 35, 432400 }, /* HE80 MCS8 */
2490 { 37, 480400 }, /* HE80 MCS9 */
2491 { 40, 540400 }, /* HE80 MCS10 */
2492 { 42, 600500 }, /* HE80 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002493 { 45, 648500 }, /* EHT80 MCS12 */
2494 { 48, 720600 }, /* EHT80 MCS13 */
2495 { -1, 720600 } /* SNR > 48 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002496};
2497
2498
2499static const struct minsnr_bitrate_entry he160_table[] = {
2500 { 0, 0 },
2501 { 11, 72100 }, /* HE160 MCS0 */
2502 { 14, 144100 }, /* HE160 MCS1 */
2503 { 18, 216200 }, /* HE160 MCS2 */
2504 { 20, 288200 }, /* HE160 MCS3 */
2505 { 24, 432400 }, /* HE160 MCS4 */
2506 { 27, 576500 }, /* HE160 MCS5 */
2507 { 29, 648500 }, /* HE160 MCS6 */
2508 { 34, 720600 }, /* HE160 MCS7 */
2509 { 38, 864700 }, /* HE160 MCS8 */
2510 { 40, 960800 }, /* HE160 MCS9 */
2511 { 43, 1080900 }, /* HE160 MCS10 */
2512 { 45, 1201000 }, /* HE160 MCS11 */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002513 { 48, 1297100 }, /* EHT160 MCS12 */
2514 { 51, 1441200 }, /* EHT160 MCS13 */
2515 { -1, 1441200 } /* SNR > 51 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002516};
2517
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002518/* See IEEE P802.11be/D2.0, Table 36-86: EHT-MCSs for 4x996-tone RU, NSS,u = 1
2519 */
2520static const struct minsnr_bitrate_entry eht320_table[] = {
2521 { 0, 0 },
2522 { 14, 144100 }, /* EHT320 MCS0 */
2523 { 17, 288200 }, /* EHT320 MCS1 */
2524 { 21, 432400 }, /* EHT320 MCS2 */
2525 { 23, 576500 }, /* EHT320 MCS3 */
2526 { 27, 864700 }, /* EHT320 MCS4 */
2527 { 30, 1152900 }, /* EHT320 MCS5 */
2528 { 32, 1297100 }, /* EHT320 MCS6 */
2529 { 37, 1441200 }, /* EHT320 MCS7 */
2530 { 41, 1729400 }, /* EHT320 MCS8 */
2531 { 43, 1921500 }, /* EHT320 MCS9 */
2532 { 46, 2161800 }, /* EHT320 MCS10 */
2533 { 48, 2401900 }, /* EHT320 MCS11 */
2534 { 51, 2594100 }, /* EHT320 MCS12 */
2535 { 54, 2882400 }, /* EHT320 MCS13 */
2536 { -1, 2882400 } /* SNR > 54 */
2537};
Hai Shaloma20dcd72022-02-04 13:43:00 -08002538
Hai Shalomfdcde762020-04-02 11:19:20 -07002539static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2540 int rate0, int rate1)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002541{
Hai Shalomfdcde762020-04-02 11:19:20 -07002542 return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002543}
2544
2545
Hai Shalom899fcc72020-10-19 14:38:18 -07002546static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2547 int snr, bool vht)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002548{
Hai Shalom899fcc72020-10-19 14:38:18 -07002549 const struct minsnr_bitrate_entry *prev, *entry = table;
2550
2551 while ((entry->minsnr != -1) &&
2552 (snr >= entry->minsnr) &&
2553 (vht || entry - table <= vht_mcs))
2554 entry++;
2555 if (entry == table)
2556 return entry->bitrate;
2557 prev = entry - 1;
2558 if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2559 return prev->bitrate;
2560 return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2561 entry->bitrate);
Hai Shalomfdcde762020-04-02 11:19:20 -07002562}
2563
2564
Hai Shalom899fcc72020-10-19 14:38:18 -07002565static unsigned int max_ht20_rate(int snr, bool vht)
Hai Shalomfdcde762020-04-02 11:19:20 -07002566{
Hai Shalom899fcc72020-10-19 14:38:18 -07002567 return max_rate(vht20_table, snr, vht);
2568}
2569
2570
2571static unsigned int max_ht40_rate(int snr, bool vht)
2572{
2573 return max_rate(vht40_table, snr, vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002574}
2575
2576
2577static unsigned int max_vht80_rate(int snr)
2578{
Hai Shalom899fcc72020-10-19 14:38:18 -07002579 return max_rate(vht80_table, snr, 1);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002580}
2581
Hai Shalomfdcde762020-04-02 11:19:20 -07002582
Hai Shaloma20dcd72022-02-04 13:43:00 -08002583static unsigned int max_vht160_rate(int snr)
2584{
2585 return max_rate(vht160_table, snr, 1);
2586}
2587
2588
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002589static unsigned int max_he_eht_rate(const struct minsnr_bitrate_entry table[],
2590 int snr, bool eht)
Hai Shaloma20dcd72022-02-04 13:43:00 -08002591{
2592 const struct minsnr_bitrate_entry *prev, *entry = table;
2593
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002594 while (entry->minsnr != -1 && snr >= entry->minsnr &&
2595 (eht || entry - table <= EHT_MCS))
Hai Shaloma20dcd72022-02-04 13:43:00 -08002596 entry++;
2597 if (entry == table)
2598 return 0;
2599 prev = entry - 1;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002600 if (entry->minsnr == -1 || (!eht && entry - table > EHT_MCS))
Hai Shaloma20dcd72022-02-04 13:43:00 -08002601 return prev->bitrate;
2602 return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2603 prev->bitrate, entry->bitrate);
2604}
2605
2606
Hai Shalomfdcde762020-04-02 11:19:20 -07002607unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2608 const u8 *ies, size_t ies_len, int rate,
Hai Shaloma20dcd72022-02-04 13:43:00 -08002609 int snr, int freq)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002610{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002611 struct hostapd_hw_modes *hw_mode;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002612 unsigned int est, tmp;
Hai Shalomfdcde762020-04-02 11:19:20 -07002613 const u8 *ie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002614
2615 /* Limit based on estimated SNR */
2616 if (rate > 1 * 2 && snr < 1)
2617 rate = 1 * 2;
2618 else if (rate > 2 * 2 && snr < 4)
2619 rate = 2 * 2;
2620 else if (rate > 6 * 2 && snr < 5)
2621 rate = 6 * 2;
2622 else if (rate > 9 * 2 && snr < 6)
2623 rate = 9 * 2;
2624 else if (rate > 12 * 2 && snr < 7)
2625 rate = 12 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002626 else if (rate > 12 * 2 && snr < 8)
2627 rate = 14 * 2;
2628 else if (rate > 12 * 2 && snr < 9)
2629 rate = 16 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002630 else if (rate > 18 * 2 && snr < 10)
2631 rate = 18 * 2;
2632 else if (rate > 24 * 2 && snr < 11)
2633 rate = 24 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002634 else if (rate > 24 * 2 && snr < 12)
2635 rate = 27 * 2;
2636 else if (rate > 24 * 2 && snr < 13)
2637 rate = 30 * 2;
2638 else if (rate > 24 * 2 && snr < 14)
2639 rate = 33 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002640 else if (rate > 36 * 2 && snr < 15)
2641 rate = 36 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002642 else if (rate > 36 * 2 && snr < 16)
2643 rate = 39 * 2;
2644 else if (rate > 36 * 2 && snr < 17)
2645 rate = 42 * 2;
2646 else if (rate > 36 * 2 && snr < 18)
2647 rate = 45 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002648 else if (rate > 48 * 2 && snr < 19)
2649 rate = 48 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002650 else if (rate > 48 * 2 && snr < 20)
2651 rate = 51 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002652 else if (rate > 54 * 2 && snr < 21)
2653 rate = 54 * 2;
2654 est = rate * 500;
2655
Hai Shaloma20dcd72022-02-04 13:43:00 -08002656 hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2657 freq);
2658
2659 if (hw_mode && hw_mode->ht_capab) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002660 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002661 if (ie) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002662 tmp = max_ht20_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002663 if (tmp > est)
2664 est = tmp;
2665 }
2666 }
2667
Hai Shaloma20dcd72022-02-04 13:43:00 -08002668 if (hw_mode &&
2669 (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002670 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002671 if (ie && ie[1] >= 2 &&
2672 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002673 tmp = max_ht40_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002674 if (tmp > est)
2675 est = tmp;
2676 }
2677 }
2678
Hai Shaloma20dcd72022-02-04 13:43:00 -08002679 if (hw_mode && hw_mode->vht_capab) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002680 /* Use +1 to assume VHT is always faster than HT */
Hai Shalomfdcde762020-04-02 11:19:20 -07002681 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002682 if (ie) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002683 bool vht80 = false, vht160 = false;
2684
Hai Shalom899fcc72020-10-19 14:38:18 -07002685 tmp = max_ht20_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002686 if (tmp > est)
2687 est = tmp;
2688
Hai Shalomfdcde762020-04-02 11:19:20 -07002689 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002690 if (ie && ie[1] >= 2 &&
2691 (ie[3] &
2692 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002693 tmp = max_ht40_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002694 if (tmp > est)
2695 est = tmp;
2696 }
2697
Hai Shaloma20dcd72022-02-04 13:43:00 -08002698 /* Determine VHT BSS bandwidth based on IEEE Std
2699 * 802.11-2020, Table 11-23 (VHT BSs bandwidth) */
Hai Shalomfdcde762020-04-02 11:19:20 -07002700 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002701 if (ie && ie[1] >= 3) {
2702 u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2703 u8 seg0 = ie[3];
2704 u8 seg1 = ie[4];
2705
2706 if (cw)
2707 vht80 = true;
2708 if (cw == 2 ||
2709 (cw == 3 &&
2710 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2711 vht160 = true;
2712 if (cw == 1 &&
2713 ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2714 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2715 vht160 = true;
2716 }
2717
2718 if (vht80) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002719 tmp = max_vht80_rate(snr) + 1;
2720 if (tmp > est)
2721 est = tmp;
2722 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08002723
2724 if (vht160 &&
2725 (hw_mode->vht_capab &
2726 (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2727 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
2728 tmp = max_vht160_rate(snr) + 1;
2729 if (tmp > est)
2730 est = tmp;
2731 }
2732 }
2733 }
2734
2735 if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
2736 /* Use +2 to assume HE is always faster than HT/VHT */
2737 struct ieee80211_he_capabilities *he;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002738 struct ieee80211_eht_capabilities *eht;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002739 struct he_capabilities *own_he;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002740 u8 cw, boost = 2;
2741 const u8 *eht_ie;
2742 bool is_eht = false;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002743
2744 ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
2745 if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
2746 return est;
2747 he = (struct ieee80211_he_capabilities *) &ie[3];
2748 own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
2749
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002750 /* Use +3 to assume EHT is always faster than HE */
2751 if (hw_mode->eht_capab[IEEE80211_MODE_INFRA].eht_supported) {
2752 eht_ie = get_ie_ext(ies, ies_len,
2753 WLAN_EID_EXT_EHT_CAPABILITIES);
2754 if (eht_ie &&
2755 (eht_ie[1] >= 1 + IEEE80211_EHT_CAPAB_MIN_LEN)) {
2756 is_eht = true;
2757 boost = 3;
2758 }
2759 }
2760
2761 tmp = max_he_eht_rate(he20_table, snr, is_eht) + boost;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002762 if (tmp > est)
2763 est = tmp;
2764
2765 cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
2766 own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
2767 if (cw &
2768 (IS_2P4GHZ(freq) ? HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
2769 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002770 tmp = max_he_eht_rate(he40_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_40MHZ_80MHZ_IN_5G)) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002777 tmp = max_he_eht_rate(he80_table, snr, is_eht) + boost;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002778 if (tmp > est)
2779 est = tmp;
2780 }
2781
2782 if (!IS_2P4GHZ(freq) &&
2783 (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2784 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G))) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00002785 tmp = max_he_eht_rate(he160_table, snr, is_eht) + boost;
2786 if (tmp > est)
2787 est = tmp;
2788 }
2789
2790 if (!is_eht)
2791 return est;
2792
2793 eht = (struct ieee80211_eht_capabilities *) &eht_ie[3];
2794
2795 if (is_6ghz_freq(freq) &&
2796 (eht->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
2797 EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
2798 tmp = max_he_eht_rate(eht320_table, snr, true);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002799 if (tmp > est)
2800 est = tmp;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002801 }
2802 }
2803
Hai Shalomfdcde762020-04-02 11:19:20 -07002804 return est;
2805}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002806
Hai Shalomfdcde762020-04-02 11:19:20 -07002807
2808void scan_est_throughput(struct wpa_supplicant *wpa_s,
2809 struct wpa_scan_res *res)
2810{
2811 int rate; /* max legacy rate in 500 kb/s units */
2812 int snr = res->snr;
2813 const u8 *ies = (const void *) (res + 1);
2814 size_t ie_len = res->ie_len;
2815
2816 if (res->est_throughput)
2817 return;
2818
2819 /* Get maximum legacy rate */
2820 rate = wpa_scan_get_max_rate(res);
2821
2822 if (!ie_len)
2823 ie_len = res->beacon_ie_len;
2824 res->est_throughput =
Hai Shaloma20dcd72022-02-04 13:43:00 -08002825 wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, res->freq);
Hai Shalomfdcde762020-04-02 11:19:20 -07002826
2827 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002828}
2829
2830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831/**
2832 * wpa_supplicant_get_scan_results - Get scan results
2833 * @wpa_s: Pointer to wpa_supplicant data
2834 * @info: Information about what was scanned or %NULL if not available
2835 * @new_scan: Whether a new scan was performed
2836 * Returns: Scan results, %NULL on failure
2837 *
2838 * This function request the current scan results from the driver and updates
2839 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2840 * results with wpa_scan_results_free().
2841 */
2842struct wpa_scan_results *
2843wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2844 struct scan_info *info, int new_scan)
2845{
2846 struct wpa_scan_results *scan_res;
2847 size_t i;
2848 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2849
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002850 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 if (scan_res == NULL) {
2852 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2853 return NULL;
2854 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002855 if (scan_res->fetch_time.sec == 0) {
2856 /*
2857 * Make sure we have a valid timestamp if the driver wrapper
2858 * does not set this.
2859 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002860 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002861 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002862 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002864 for (i = 0; i < scan_res->num; i++) {
2865 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2866
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002867 scan_snr(scan_res_item);
2868 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002869 }
2870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002872 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2874 "provisioning rules");
2875 compar = wpa_scan_result_wps_compar;
2876 }
2877#endif /* CONFIG_WPS */
2878
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002879 if (scan_res->res) {
2880 qsort(scan_res->res, scan_res->num,
2881 sizeof(struct wpa_scan_res *), compar);
2882 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884
Dmitry Shmidt29333592017-01-09 12:27:11 -08002885 if (wpa_s->ignore_post_flush_scan_res) {
2886 /* FLUSH command aborted an ongoing scan and these are the
2887 * results from the aborted scan. Do not process the results to
2888 * maintain flushed state. */
2889 wpa_dbg(wpa_s, MSG_DEBUG,
2890 "Do not update BSS table based on pending post-FLUSH scan results");
2891 wpa_s->ignore_post_flush_scan_res = 0;
2892 return scan_res;
2893 }
2894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 wpa_bss_update_start(wpa_s);
2896 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002897 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2898 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 wpa_bss_update_end(wpa_s, info, new_scan);
2900
2901 return scan_res;
2902}
2903
2904
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002905/**
2906 * wpa_supplicant_update_scan_results - Update scan results from the driver
2907 * @wpa_s: Pointer to wpa_supplicant data
2908 * Returns: 0 on success, -1 on failure
2909 *
2910 * This function updates the BSS table within wpa_supplicant based on the
2911 * currently available scan results from the driver without requesting a new
2912 * scan. This is used in cases where the driver indicates an association
2913 * (including roaming within ESS) and wpa_supplicant does not yet have the
2914 * needed information to complete the connection (e.g., to perform validation
2915 * steps in 4-way handshake).
2916 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2918{
2919 struct wpa_scan_results *scan_res;
2920 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2921 if (scan_res == NULL)
2922 return -1;
2923 wpa_scan_results_free(scan_res);
2924
2925 return 0;
2926}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002927
2928
2929/**
2930 * scan_only_handler - Reports scan results
2931 */
2932void scan_only_handler(struct wpa_supplicant *wpa_s,
2933 struct wpa_scan_results *scan_res)
2934{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002935 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002936 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2937 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2938 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2939 wpa_s->manual_scan_id);
2940 wpa_s->manual_scan_use_id = 0;
2941 } else {
2942 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2943 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002944 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002945 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002946 if (wpa_s->scan_work) {
2947 struct wpa_radio_work *work = wpa_s->scan_work;
2948 wpa_s->scan_work = NULL;
2949 radio_work_done(work);
2950 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002951
2952 if (wpa_s->wpa_state == WPA_SCANNING)
2953 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002954}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002955
2956
2957int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2958{
2959 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2960}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002961
2962
2963struct wpa_driver_scan_params *
2964wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2965{
2966 struct wpa_driver_scan_params *params;
2967 size_t i;
2968 u8 *n;
2969
2970 params = os_zalloc(sizeof(*params));
2971 if (params == NULL)
2972 return NULL;
2973
2974 for (i = 0; i < src->num_ssids; i++) {
2975 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002976 n = os_memdup(src->ssids[i].ssid,
2977 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002978 if (n == NULL)
2979 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002980 params->ssids[i].ssid = n;
2981 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2982 }
2983 }
2984 params->num_ssids = src->num_ssids;
2985
2986 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002987 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002988 if (n == NULL)
2989 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002990 params->extra_ies = n;
2991 params->extra_ies_len = src->extra_ies_len;
2992 }
2993
2994 if (src->freqs) {
2995 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002996 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002997 if (params->freqs == NULL)
2998 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002999 }
3000
3001 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003002 params->filter_ssids = os_memdup(src->filter_ssids,
3003 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003004 src->num_filter_ssids);
3005 if (params->filter_ssids == NULL)
3006 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003007 params->num_filter_ssids = src->num_filter_ssids;
3008 }
3009
3010 params->filter_rssi = src->filter_rssi;
3011 params->p2p_probe = src->p2p_probe;
3012 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003013 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003014 params->duration = src->duration;
3015 params->duration_mandatory = src->duration_mandatory;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003016 params->oce_scan = src->oce_scan;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003017
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003018 if (src->sched_scan_plans_num > 0) {
3019 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003020 os_memdup(src->sched_scan_plans,
3021 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003022 src->sched_scan_plans_num);
3023 if (!params->sched_scan_plans)
3024 goto failed;
3025
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003026 params->sched_scan_plans_num = src->sched_scan_plans_num;
3027 }
3028
Hai Shalomc3565922019-10-28 11:58:20 -07003029 if (src->mac_addr_rand &&
3030 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
3031 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003032
3033 if (src->bssid) {
3034 u8 *bssid;
3035
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003036 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003037 if (!bssid)
3038 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003039 params->bssid = bssid;
3040 }
3041
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003042 params->relative_rssi_set = src->relative_rssi_set;
3043 params->relative_rssi = src->relative_rssi;
3044 params->relative_adjust_band = src->relative_adjust_band;
3045 params->relative_adjust_rssi = src->relative_adjust_rssi;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003046 params->p2p_include_6ghz = src->p2p_include_6ghz;
Sunil8cd6f4d2022-06-28 18:40:46 +00003047 params->non_coloc_6ghz = src->non_coloc_6ghz;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003048 return params;
3049
3050failed:
3051 wpa_scan_free_params(params);
3052 return NULL;
3053}
3054
3055
3056void wpa_scan_free_params(struct wpa_driver_scan_params *params)
3057{
3058 size_t i;
3059
3060 if (params == NULL)
3061 return;
3062
3063 for (i = 0; i < params->num_ssids; i++)
3064 os_free((u8 *) params->ssids[i].ssid);
3065 os_free((u8 *) params->extra_ies);
3066 os_free(params->freqs);
3067 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003068 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003069
3070 /*
3071 * Note: params->mac_addr_mask points to same memory allocation and
3072 * must not be freed separately.
3073 */
3074 os_free((u8 *) params->mac_addr);
3075
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003076 os_free((u8 *) params->bssid);
3077
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003078 os_free(params);
3079}
Dmitry Shmidt98660862014-03-11 17:26:21 -07003080
3081
3082int wpas_start_pno(struct wpa_supplicant *wpa_s)
3083{
Hai Shalomfdcde762020-04-02 11:19:20 -07003084 int ret;
3085 size_t prio, i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003086 struct wpa_ssid *ssid;
3087 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003088 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07003089 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003090
3091 if (!wpa_s->sched_scan_supported)
3092 return -1;
3093
Dmitry Shmidte4663042016-04-04 10:07:49 -07003094 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
3095 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
3096 else
3097 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
3098 if (max_sched_scan_ssids < 1)
3099 return -1;
3100
Dmitry Shmidt98660862014-03-11 17:26:21 -07003101 if (wpa_s->pno || wpa_s->pno_sched_pending)
3102 return 0;
3103
3104 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003105 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003106 wpa_printf(MSG_ERROR, "PNO: In assoc process");
3107 return -EAGAIN;
3108 }
3109
3110 if (wpa_s->wpa_state == WPA_SCANNING) {
3111 wpa_supplicant_cancel_scan(wpa_s);
3112 if (wpa_s->sched_scanning) {
3113 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
3114 "ongoing sched scan");
3115 wpa_supplicant_cancel_sched_scan(wpa_s);
3116 wpa_s->pno_sched_pending = 1;
3117 return 0;
3118 }
3119 }
3120
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003121 if (wpa_s->sched_scan_stop_req) {
3122 wpa_printf(MSG_DEBUG,
3123 "Schedule PNO after previous sched scan has stopped");
3124 wpa_s->pno_sched_pending = 1;
3125 return 0;
3126 }
3127
Dmitry Shmidt98660862014-03-11 17:26:21 -07003128 os_memset(&params, 0, sizeof(params));
3129
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003130 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003131 ssid = wpa_s->conf->ssid;
3132 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003133 if (!wpas_network_disabled(wpa_s, ssid)) {
3134 num_match_ssid++;
3135 if (ssid->scan_ssid)
3136 num_ssid++;
3137 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003138 ssid = ssid->next;
3139 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003140
3141 if (num_match_ssid == 0) {
3142 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3143 return -1;
3144 }
3145
3146 if (num_match_ssid > num_ssid) {
3147 params.num_ssids++; /* wildcard */
3148 num_ssid++;
3149 }
3150
Dmitry Shmidte4663042016-04-04 10:07:49 -07003151 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003152 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07003153 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3154 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003155 }
3156
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003157 if (num_match_ssid > wpa_s->max_match_sets) {
3158 num_match_ssid = wpa_s->max_match_sets;
3159 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003160 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003161 params.filter_ssids = os_calloc(num_match_ssid,
3162 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07003163 if (params.filter_ssids == NULL)
3164 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003165
Dmitry Shmidt98660862014-03-11 17:26:21 -07003166 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003167 prio = 0;
3168 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003169 while (ssid) {
3170 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003171 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3172 params.ssids[params.num_ssids].ssid =
3173 ssid->ssid;
3174 params.ssids[params.num_ssids].ssid_len =
3175 ssid->ssid_len;
3176 params.num_ssids++;
3177 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003178 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3179 ssid->ssid_len);
3180 params.filter_ssids[i].ssid_len = ssid->ssid_len;
3181 params.num_filter_ssids++;
3182 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003183 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07003184 break;
3185 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003186 if (ssid->pnext)
3187 ssid = ssid->pnext;
3188 else if (prio + 1 == wpa_s->conf->num_prio)
3189 break;
3190 else
3191 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003192 }
3193
3194 if (wpa_s->conf->filter_rssi)
3195 params.filter_rssi = wpa_s->conf->filter_rssi;
3196
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003197 if (wpa_s->sched_scan_plans_num) {
3198 params.sched_scan_plans = wpa_s->sched_scan_plans;
3199 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3200 } else {
3201 /* Set one scan plan that will run infinitely */
3202 if (wpa_s->conf->sched_scan_interval)
3203 scan_plan.interval = wpa_s->conf->sched_scan_interval;
3204 else
3205 scan_plan.interval = 10;
3206
3207 scan_plan.iterations = 0;
3208 params.sched_scan_plans = &scan_plan;
3209 params.sched_scan_plans_num = 1;
3210 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003211
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003212 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3213
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003214 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3215 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3216 params.freqs = wpa_s->manual_sched_scan_freqs;
3217 }
3218
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003219 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
Hai Shalomc3565922019-10-28 11:58:20 -07003220 wpa_s->wpa_state <= WPA_SCANNING)
3221 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003222
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003223 wpa_scan_set_relative_rssi_params(wpa_s, &params);
3224
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003225 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003226 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07003227 os_free(params.mac_addr);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003228 if (ret == 0)
3229 wpa_s->pno = 1;
3230 else
3231 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3232 return ret;
3233}
3234
3235
3236int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3237{
3238 int ret = 0;
3239
3240 if (!wpa_s->pno)
3241 return 0;
3242
3243 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003244 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003245
3246 wpa_s->pno = 0;
3247 wpa_s->pno_sched_pending = 0;
3248
3249 if (wpa_s->wpa_state == WPA_SCANNING)
3250 wpa_supplicant_req_scan(wpa_s, 0, 0);
3251
3252 return ret;
3253}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003254
3255
3256void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3257 unsigned int type)
3258{
3259 type &= MAC_ADDR_RAND_ALL;
3260 wpa_s->mac_addr_rand_enable &= ~type;
3261
3262 if (type & MAC_ADDR_RAND_SCAN) {
3263 os_free(wpa_s->mac_addr_scan);
3264 wpa_s->mac_addr_scan = NULL;
3265 }
3266
3267 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3268 os_free(wpa_s->mac_addr_sched_scan);
3269 wpa_s->mac_addr_sched_scan = NULL;
3270 }
3271
3272 if (type & MAC_ADDR_RAND_PNO) {
3273 os_free(wpa_s->mac_addr_pno);
3274 wpa_s->mac_addr_pno = NULL;
3275 }
3276}
3277
3278
3279int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3280 unsigned int type, const u8 *addr,
3281 const u8 *mask)
3282{
3283 u8 *tmp = NULL;
3284
Hai Shalom74f70d42019-02-11 14:42:39 -08003285 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3286 wpa_printf(MSG_INFO,
3287 "scan: MAC randomization type %u != supported=%u",
3288 type, wpa_s->mac_addr_rand_supported);
3289 return -1;
3290 }
3291
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003292 wpas_mac_addr_rand_scan_clear(wpa_s, type);
3293
3294 if (addr) {
3295 tmp = os_malloc(2 * ETH_ALEN);
3296 if (!tmp)
3297 return -1;
3298 os_memcpy(tmp, addr, ETH_ALEN);
3299 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3300 }
3301
3302 if (type == MAC_ADDR_RAND_SCAN) {
3303 wpa_s->mac_addr_scan = tmp;
3304 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3305 wpa_s->mac_addr_sched_scan = tmp;
3306 } else if (type == MAC_ADDR_RAND_PNO) {
3307 wpa_s->mac_addr_pno = tmp;
3308 } else {
3309 wpa_printf(MSG_INFO,
3310 "scan: Invalid MAC randomization type=0x%x",
3311 type);
3312 os_free(tmp);
3313 return -1;
3314 }
3315
3316 wpa_s->mac_addr_rand_enable |= type;
3317 return 0;
3318}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003319
3320
Hai Shalomc3565922019-10-28 11:58:20 -07003321int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3322 unsigned int type, u8 *mask)
3323{
3324 const u8 *to_copy;
3325
3326 if ((wpa_s->mac_addr_rand_enable & type) != type)
3327 return -1;
3328
3329 if (type == MAC_ADDR_RAND_SCAN) {
3330 to_copy = wpa_s->mac_addr_scan;
3331 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3332 to_copy = wpa_s->mac_addr_sched_scan;
3333 } else if (type == MAC_ADDR_RAND_PNO) {
3334 to_copy = wpa_s->mac_addr_pno;
3335 } else {
3336 wpa_printf(MSG_DEBUG,
3337 "scan: Invalid MAC randomization type=0x%x",
3338 type);
3339 return -1;
3340 }
3341
3342 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3343 return 0;
3344}
3345
3346
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003347int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3348{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003349 struct wpa_radio_work *work;
3350 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003351
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003352 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3353 if (work->wpa_s != wpa_s || !work->started ||
3354 (os_strcmp(work->type, "scan") != 0 &&
3355 os_strcmp(work->type, "p2p-scan") != 0))
3356 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003357 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003358 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003359 }
3360
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003361 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3362 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003363}
3364
3365
3366int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3367{
3368 struct sched_scan_plan *scan_plans = NULL;
3369 const char *token, *context = NULL;
3370 unsigned int num = 0;
3371
3372 if (!cmd)
3373 return -1;
3374
3375 if (!cmd[0]) {
3376 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3377 os_free(wpa_s->sched_scan_plans);
3378 wpa_s->sched_scan_plans = NULL;
3379 wpa_s->sched_scan_plans_num = 0;
3380 return 0;
3381 }
3382
3383 while ((token = cstr_token(cmd, " ", &context))) {
3384 int ret;
3385 struct sched_scan_plan *scan_plan, *n;
3386
3387 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3388 if (!n)
3389 goto fail;
3390
3391 scan_plans = n;
3392 scan_plan = &scan_plans[num];
3393 num++;
3394
3395 ret = sscanf(token, "%u:%u", &scan_plan->interval,
3396 &scan_plan->iterations);
3397 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3398 wpa_printf(MSG_ERROR,
3399 "Invalid sched scan plan input: %s", token);
3400 goto fail;
3401 }
3402
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003403 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3404 wpa_printf(MSG_WARNING,
3405 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3406 num, scan_plan->interval,
3407 wpa_s->max_sched_scan_plan_interval);
3408 scan_plan->interval =
3409 wpa_s->max_sched_scan_plan_interval;
3410 }
3411
3412 if (ret == 1) {
3413 scan_plan->iterations = 0;
3414 break;
3415 }
3416
3417 if (!scan_plan->iterations) {
3418 wpa_printf(MSG_ERROR,
3419 "scan plan %u: Number of iterations cannot be zero",
3420 num);
3421 goto fail;
3422 }
3423
3424 if (scan_plan->iterations >
3425 wpa_s->max_sched_scan_plan_iterations) {
3426 wpa_printf(MSG_WARNING,
3427 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3428 num, scan_plan->iterations,
3429 wpa_s->max_sched_scan_plan_iterations);
3430 scan_plan->iterations =
3431 wpa_s->max_sched_scan_plan_iterations;
3432 }
3433
3434 wpa_printf(MSG_DEBUG,
3435 "scan plan %u: interval=%u iterations=%u",
3436 num, scan_plan->interval, scan_plan->iterations);
3437 }
3438
3439 if (!scan_plans) {
3440 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3441 goto fail;
3442 }
3443
3444 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3445 wpa_printf(MSG_ERROR,
3446 "All scan plans but the last must specify a number of iterations");
3447 goto fail;
3448 }
3449
3450 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3451 num, scan_plans[num - 1].interval);
3452
3453 if (num > wpa_s->max_sched_scan_plans) {
3454 wpa_printf(MSG_WARNING,
3455 "Too many scheduled scan plans (only %u supported)",
3456 wpa_s->max_sched_scan_plans);
3457 wpa_printf(MSG_WARNING,
3458 "Use only the first %u scan plans, and the last one (in infinite loop)",
3459 wpa_s->max_sched_scan_plans - 1);
3460 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3461 &scan_plans[num - 1], sizeof(*scan_plans));
3462 num = wpa_s->max_sched_scan_plans;
3463 }
3464
3465 os_free(wpa_s->sched_scan_plans);
3466 wpa_s->sched_scan_plans = scan_plans;
3467 wpa_s->sched_scan_plans_num = num;
3468
3469 return 0;
3470
3471fail:
3472 os_free(scan_plans);
3473 wpa_printf(MSG_ERROR, "invalid scan plans list");
3474 return -1;
3475}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003476
3477
3478/**
3479 * wpas_scan_reset_sched_scan - Reset sched_scan state
3480 * @wpa_s: Pointer to wpa_supplicant data
3481 *
3482 * This function is used to cancel a running scheduled scan and to reset an
3483 * internal scan state to continue with a regular scan on the following
3484 * wpa_supplicant_req_scan() calls.
3485 */
3486void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3487{
3488 wpa_s->normal_scans = 0;
3489 if (wpa_s->sched_scanning) {
3490 wpa_s->sched_scan_timed_out = 0;
3491 wpa_s->prev_sched_ssid = NULL;
3492 wpa_supplicant_cancel_sched_scan(wpa_s);
3493 }
3494}
3495
3496
3497void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3498{
3499 /* simulate timeout to restart the sched scan */
3500 wpa_s->sched_scan_timed_out = 1;
3501 wpa_s->prev_sched_ssid = NULL;
3502 wpa_supplicant_cancel_sched_scan(wpa_s);
3503}