blob: bfcb62b2ae418021c815c32548c7e2775e1e0b5f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt3a787e62013-01-17 10:32:35 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "config.h"
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "wps_supplicant.h"
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "notify.h"
23#include "bss.h"
24#include "scan.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080025#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026
27
28static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29{
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
32
33 ssid = wpa_supplicant_get_ssid(wpa_s);
34 if (ssid == NULL)
35 return;
36
37 if (wpa_s->current_ssid == NULL) {
38 wpa_s->current_ssid = ssid;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070039 wpas_notify_network_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040 }
41 wpa_supplicant_initiate_eapol(wpa_s);
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
49#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 enum wps_request_type *req_type)
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
55
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
61 *req_type = wpas_wps_get_req_type(ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -070062 if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 return 2;
64 }
65
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080067 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
68 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080069 wpa_s->wps->dev.p2p = 1;
70 if (!wps) {
71 wps = 1;
72 *req_type = WPS_REQ_ENROLLEE_INFO;
73 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080074 }
75#endif /* CONFIG_P2P */
76
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 return wps;
78}
79#endif /* CONFIG_WPS */
80
81
Hai Shalomc3565922019-10-28 11:58:20 -070082static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
83 const u8 *mac_addr)
84{
85 u8 *tmp;
86
87 if (params->mac_addr) {
88 params->mac_addr_mask = NULL;
89 os_free(params->mac_addr);
90 params->mac_addr = NULL;
91 }
92
93 params->mac_addr_rand = 1;
94
95 if (!mac_addr)
96 return 0;
97
98 tmp = os_malloc(2 * ETH_ALEN);
99 if (!tmp)
100 return -1;
101
102 os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
103 params->mac_addr = tmp;
104 params->mac_addr_mask = tmp + ETH_ALEN;
105 return 0;
106}
107
108
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800109/**
110 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
111 * @wpa_s: Pointer to wpa_supplicant data
112 * Returns: 0 if no networks are enabled, >0 if networks are enabled
113 *
114 * This function is used to figure out whether any networks (or Interworking
115 * with enabled credentials and auto_interworking) are present in the current
116 * configuration.
117 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700118int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700121 int count = 0, disabled = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800122
123 if (wpa_s->p2p_mgmt)
124 return 0; /* no normal network profiles on p2p_mgmt interface */
125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700127 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700129 else
130 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 ssid = ssid->next;
132 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700133 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
134 wpa_s->conf->auto_interworking)
135 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700136 if (count == 0 && disabled > 0) {
137 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
138 "networks)", disabled);
139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 return count;
141}
142
143
144static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
145 struct wpa_ssid *ssid)
146{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700147 int min_temp_disabled = 0;
148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 while (ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700150 if (!wpas_network_disabled(wpa_s, ssid)) {
151 int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
152
153 if (temp_disabled <= 0)
154 break;
155
156 if (!min_temp_disabled ||
157 temp_disabled < min_temp_disabled)
158 min_temp_disabled = temp_disabled;
159 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 ssid = ssid->next;
161 }
162
163 /* ap_scan=2 mode - try to associate with each SSID. */
164 if (ssid == NULL) {
165 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
166 "end of scan list - go back to beginning");
167 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700168 wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 return;
170 }
171 if (ssid->next) {
172 /* Continue from the next SSID on the next attempt. */
173 wpa_s->prev_scan_ssid = ssid;
174 } else {
175 /* Start from the beginning of the SSID list. */
176 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
177 }
178 wpa_supplicant_associate(wpa_s, NULL, ssid);
179}
180
181
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800182static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800184 struct wpa_supplicant *wpa_s = work->wpa_s;
185 struct wpa_driver_scan_params *params = work->ctx;
186 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800188 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800189 if (!work->started) {
190 wpa_scan_free_params(params);
191 return;
192 }
193 wpa_supplicant_notify_scanning(wpa_s, 0);
194 wpas_notify_scan_done(wpa_s, 0);
195 wpa_s->scan_work = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 return;
197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198
Hai Shalomc3565922019-10-28 11:58:20 -0700199 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
200 wpa_s->wpa_state <= WPA_SCANNING)
201 wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
202
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700203 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
204 wpa_msg(wpa_s, MSG_INFO,
205 "Failed to assign random MAC address for a scan");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700206 wpa_scan_free_params(params);
207 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700208 radio_work_done(work);
209 return;
210 }
211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800212 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800214 if (wpa_s->clear_driver_scan_cache) {
215 wpa_printf(MSG_DEBUG,
216 "Request driver to clear scan cache due to local BSS flush");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800217 params->only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800218 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800219 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800220 /*
221 * Store the obtained vendor scan cookie (if any) in wpa_s context.
222 * The current design is to allow only one scan request on each
223 * interface, hence having this scan cookie stored in wpa_s context is
224 * fine for now.
225 *
226 * Revisit this logic if concurrent scan operations per interface
227 * is supported.
228 */
229 if (ret == 0)
230 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800231 wpa_scan_free_params(params);
232 work->ctx = NULL;
233 if (ret) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700234 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
235 !wpa_s->beacon_rep_data.token;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800236
237 if (wpa_s->disconnected)
238 retry = 0;
239
Hai Shalom899fcc72020-10-19 14:38:18 -0700240 /* do not retry if operation is not supported */
241 if (ret == -EOPNOTSUPP)
242 retry = 0;
243
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800244 wpa_supplicant_notify_scanning(wpa_s, 0);
245 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800246 if (wpa_s->wpa_state == WPA_SCANNING)
247 wpa_supplicant_set_state(wpa_s,
248 wpa_s->scan_prev_wpa_state);
249 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
250 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800252
253 if (retry) {
254 /* Restore scan_req since we will try to scan again */
255 wpa_s->scan_req = wpa_s->last_scan_req;
256 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700257 } else if (wpa_s->scan_res_handler) {
258 /* Clear the scan_res_handler */
259 wpa_s->scan_res_handler = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800260 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700261
262 if (wpa_s->beacon_rep_data.token)
263 wpas_rrm_refuse_request(wpa_s);
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800267
268 os_get_reltime(&wpa_s->scan_trigger_time);
269 wpa_s->scan_runs++;
270 wpa_s->normal_scans++;
271 wpa_s->own_scan_requested = 1;
272 wpa_s->clear_driver_scan_cache = 0;
273 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274}
275
276
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800277/**
278 * wpa_supplicant_trigger_scan - Request driver to start a scan
279 * @wpa_s: Pointer to wpa_supplicant data
280 * @params: Scan parameters
281 * Returns: 0 on success, -1 on failure
282 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
284 struct wpa_driver_scan_params *params)
285{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800286 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800288 if (wpa_s->scan_work) {
289 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
290 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 }
292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800293 ctx = wpa_scan_clone_params(params);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700294 if (!ctx ||
295 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800296 {
297 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700298 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800299 return -1;
300 }
301
302 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303}
304
305
306static void
307wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
308{
309 struct wpa_supplicant *wpa_s = eloop_ctx;
310
311 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
312
313 if (wpa_supplicant_req_sched_scan(wpa_s))
314 wpa_supplicant_req_scan(wpa_s, 0, 0);
315}
316
317
318static void
319wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
320{
321 struct wpa_supplicant *wpa_s = eloop_ctx;
322
323 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
324
325 wpa_s->sched_scan_timed_out = 1;
326 wpa_supplicant_cancel_sched_scan(wpa_s);
327}
328
329
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700330static int
331wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
332 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333{
334 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700335
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800336 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800337 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800338 if (ret)
339 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800340 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800341 wpa_s->sched_scanning = 1;
342
343 return ret;
344}
345
346
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700347static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800348{
349 int ret;
350
351 ret = wpa_drv_stop_sched_scan(wpa_s);
352 if (ret) {
353 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
354 /* TODO: what to do if stopping fails? */
355 return -1;
356 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
358 return ret;
359}
360
361
362static struct wpa_driver_scan_filter *
363wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
364{
365 struct wpa_driver_scan_filter *ssids;
366 struct wpa_ssid *ssid;
367 size_t count;
368
369 *num_ssids = 0;
370 if (!conf->filter_ssids)
371 return NULL;
372
373 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
374 if (ssid->ssid && ssid->ssid_len)
375 count++;
376 }
377 if (count == 0)
378 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 if (ssids == NULL)
381 return NULL;
382
383 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
384 if (!ssid->ssid || !ssid->ssid_len)
385 continue;
386 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
387 ssids[*num_ssids].ssid_len = ssid->ssid_len;
388 (*num_ssids)++;
389 }
390
391 return ssids;
392}
393
394
Hai Shaloma20dcd72022-02-04 13:43:00 -0800395#ifdef CONFIG_P2P
396static bool is_6ghz_supported(struct wpa_supplicant *wpa_s)
397{
398 struct hostapd_channel_data *chnl;
399 int i, j;
400
401 for (i = 0; i < wpa_s->hw.num_modes; i++) {
402 if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211A) {
403 chnl = wpa_s->hw.modes[i].channels;
404 for (j = 0; j < wpa_s->hw.modes[i].num_channels; j++) {
405 if (chnl[j].flag & HOSTAPD_CHAN_DISABLED)
406 continue;
407 if (is_6ghz_freq(chnl[j].freq))
408 return true;
409 }
410 }
411 }
412
413 return false;
414}
415#endif /* CONFIG_P2P */
416
417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418static void wpa_supplicant_optimize_freqs(
419 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
420{
421#ifdef CONFIG_P2P
422 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
423 wpa_s->go_params) {
424 /* Optimize provisioning state scan based on GO information */
425 if (wpa_s->p2p_in_provisioning < 5 &&
426 wpa_s->go_params->freq > 0) {
427 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
428 "preferred frequency %d MHz",
429 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800430 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800431 if (params->freqs)
432 params->freqs[0] = wpa_s->go_params->freq;
433 } else if (wpa_s->p2p_in_provisioning < 8 &&
434 wpa_s->go_params->freq_list[0]) {
435 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
436 "channels");
437 int_array_concat(&params->freqs,
438 wpa_s->go_params->freq_list);
439 if (params->freqs)
440 int_array_sort_unique(params->freqs);
441 }
442 wpa_s->p2p_in_provisioning++;
443 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700444
445 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
446 /*
Matthew Wangdcf19452022-11-07 20:42:52 -0800447 * Perform a single-channel scan if the GO has already been
448 * discovered on another non-P2P interface. Note that a scan
449 * initiated by a P2P interface (e.g. the device interface)
450 * should already have sufficient IEs and scan results will be
451 * fetched on interface creation in that case.
452 */
453 if (wpa_s->p2p_in_invitation == 1 && wpa_s->current_ssid) {
454 struct wpa_supplicant *ifs;
455 struct wpa_bss *bss = NULL;
456 struct wpa_ssid *ssid = wpa_s->current_ssid;
457 u8 *bssid = ssid->bssid_set ? ssid->bssid : NULL;
458 dl_list_for_each(ifs, &wpa_s->radio->ifaces,
459 struct wpa_supplicant, radio_list) {
460 bss = wpa_bss_get(ifs, bssid, ssid->ssid,
461 ssid->ssid_len);
462 if (bss)
463 break;
464 }
465 if (bss && !disabled_freq(wpa_s, bss->freq)) {
466 params->freqs = os_calloc(2, sizeof(int));
467 if (params->freqs)
468 params->freqs[0] = bss->freq;
469 }
470 }
471 /*
Dmitry Shmidt15907092014-03-25 10:42:57 -0700472 * Optimize scan based on GO information during persistent
473 * group reinvocation
474 */
Matthew Wangdcf19452022-11-07 20:42:52 -0800475 if (params->freqs == NULL && wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700476 wpa_s->p2p_invite_go_freq > 0) {
477 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
478 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800479 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700480 if (params->freqs)
481 params->freqs[0] = wpa_s->p2p_invite_go_freq;
482 }
483 wpa_s->p2p_in_invitation++;
484 if (wpa_s->p2p_in_invitation > 20) {
485 /*
486 * This should not really happen since the variable is
487 * cleared on group removal, but if it does happen, make
488 * sure we do not get stuck in special invitation scan
489 * mode.
490 */
491 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
492 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +0000493 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -0700494 }
495 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800496#endif /* CONFIG_P2P */
497
498#ifdef CONFIG_WPS
499 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
500 /*
501 * Optimize post-provisioning scan based on channel used
502 * during provisioning.
503 */
504 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
505 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800506 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800507 if (params->freqs)
508 params->freqs[0] = wpa_s->wps_freq;
509 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800510 } else if (wpa_s->after_wps)
511 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800512
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800513 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
514 {
515 /* Optimize provisioning scan based on already known channel */
516 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
517 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800518 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800519 if (params->freqs)
520 params->freqs[0] = wpa_s->wps_freq;
521 wpa_s->known_wps_freq = 0; /* only do this once */
522 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800523#endif /* CONFIG_WPS */
524}
525
526
527#ifdef CONFIG_INTERWORKING
528static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
529 struct wpabuf *buf)
530{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800531 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
532 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
533 1 + ETH_ALEN);
534 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
535 /* No Venue Info */
536 if (!is_zero_ether_addr(wpa_s->conf->hessid))
537 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
538}
539#endif /* CONFIG_INTERWORKING */
540
541
Hai Shalomce48b4a2018-09-05 11:41:35 -0700542#ifdef CONFIG_MBO
543static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
544 struct wpabuf **ie)
545{
546 if (wpabuf_resize(ie, 5)) {
547 wpa_printf(MSG_DEBUG,
548 "Failed to allocate space for FILS Request Parameters element");
549 return;
550 }
551
552 /* FILS Request Parameters element */
553 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
554 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
555 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
556 /* Parameter control bitmap */
557 wpabuf_put_u8(*ie, 0);
558 /* Max Channel Time field - contains the value of MaxChannelTime
559 * parameter of the MLME-SCAN.request primitive represented in units of
560 * TUs, as an unsigned integer. A Max Channel Time field value of 255
561 * is used to indicate any duration of more than 254 TUs, or an
562 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
563 */
564 wpabuf_put_u8(*ie, 255);
565}
566#endif /* CONFIG_MBO */
567
568
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700569void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
570{
571 struct wpabuf *default_ies = NULL;
572 u8 ext_capab[18];
Hai Shalom60840252021-02-19 19:02:11 -0800573 int ext_capab_len, frame_id;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700574 enum wpa_driver_if_type type = WPA_IF_STATION;
575
576#ifdef CONFIG_P2P
577 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
578 type = WPA_IF_P2P_CLIENT;
579#endif /* CONFIG_P2P */
580
581 wpa_drv_get_ext_capa(wpa_s, type);
582
583 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
584 sizeof(ext_capab));
585 if (ext_capab_len > 0 &&
586 wpabuf_resize(&default_ies, ext_capab_len) == 0)
587 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
588
589#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -0700590 if (wpa_s->enable_oce & OCE_STA)
591 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700592 /* Send MBO and OCE capabilities */
593 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700594 wpas_mbo_scan_ie(wpa_s, default_ies);
595#endif /* CONFIG_MBO */
596
Hai Shalom60840252021-02-19 19:02:11 -0800597 if (type == WPA_IF_P2P_CLIENT)
598 frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
599 else
600 frame_id = VENDOR_ELEM_PROBE_REQ;
601
602 if (wpa_s->vendor_elem[frame_id]) {
603 size_t len;
604
605 len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
606 if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
607 wpabuf_put_buf(default_ies,
608 wpa_s->vendor_elem[frame_id]);
609 }
610
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700611 if (default_ies)
612 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
613 wpabuf_len(default_ies));
614 wpabuf_free(default_ies);
615}
616
617
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700618static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800619{
620 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700621 u8 ext_capab[18];
622 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800623#ifdef CONFIG_WPS
624 int wps = 0;
625 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
626#endif /* CONFIG_WPS */
627
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700628#ifdef CONFIG_P2P
629 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
630 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
631 else
632#endif /* CONFIG_P2P */
633 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
634
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700635 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
636 sizeof(ext_capab));
637 if (ext_capab_len > 0 &&
638 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
639 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
640
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800641#ifdef CONFIG_INTERWORKING
642 if (wpa_s->conf->interworking &&
643 wpabuf_resize(&extra_ie, 100) == 0)
644 wpas_add_interworking_elements(wpa_s, extra_ie);
645#endif /* CONFIG_INTERWORKING */
646
Hai Shalomce48b4a2018-09-05 11:41:35 -0700647#ifdef CONFIG_MBO
648 if (wpa_s->enable_oce & OCE_STA)
649 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
650#endif /* CONFIG_MBO */
651
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652#ifdef CONFIG_WPS
653 wps = wpas_wps_in_use(wpa_s, &req_type);
654
655 if (wps) {
656 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700657 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
658 DEV_PW_DEFAULT,
659 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800660 wpa_s->wps->uuid, req_type,
661 0, NULL);
662 if (wps_ie) {
663 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
664 wpabuf_put_buf(extra_ie, wps_ie);
665 wpabuf_free(wps_ie);
666 }
667 }
668
669#ifdef CONFIG_P2P
670 if (wps) {
671 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
672 if (wpabuf_resize(&extra_ie, ielen) == 0)
673 wpas_p2p_scan_ie(wpa_s, extra_ie);
674 }
675#endif /* CONFIG_P2P */
676
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800677 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800679#endif /* CONFIG_WPS */
680
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700681#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -0800682 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
683 wpas_hs20_add_indication(extra_ie, -1, 0);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700684#endif /* CONFIG_HS20 */
685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686#ifdef CONFIG_FST
687 if (wpa_s->fst_ies &&
688 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
689 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
690#endif /* CONFIG_FST */
691
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800692#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700693 /* Send MBO and OCE capabilities */
694 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800695 wpas_mbo_scan_ie(wpa_s, extra_ie);
696#endif /* CONFIG_MBO */
697
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700698 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
699 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
700
701 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
702 wpabuf_put_buf(extra_ie, buf);
703 }
704
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800705 return extra_ie;
706}
707
708
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800709#ifdef CONFIG_P2P
710
711/*
712 * Check whether there are any enabled networks or credentials that could be
713 * used for a non-P2P connection.
714 */
715static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
716{
717 struct wpa_ssid *ssid;
718
719 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
720 if (wpas_network_disabled(wpa_s, ssid))
721 continue;
722 if (!ssid->p2p_group)
723 return 1;
724 }
725
726 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
727 wpa_s->conf->auto_interworking)
728 return 1;
729
730 return 0;
731}
732
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700733#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800734
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800735
Hai Shalom60840252021-02-19 19:02:11 -0800736int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
737 enum hostapd_hw_mode band,
738 struct wpa_driver_scan_params *params, bool is_6ghz)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700739{
740 /* Include only supported channels for the specified band */
741 struct hostapd_hw_modes *mode;
Hai Shalom60840252021-02-19 19:02:11 -0800742 int num_chans = 0;
743 int *freqs, i;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700744
Hai Shalomfdcde762020-04-02 11:19:20 -0700745 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
Hai Shalom60840252021-02-19 19:02:11 -0800746 if (!mode)
747 return -1;
748
749 if (params->freqs) {
750 while (params->freqs[num_chans])
751 num_chans++;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700752 }
753
Hai Shalom60840252021-02-19 19:02:11 -0800754 freqs = os_realloc(params->freqs,
755 (num_chans + mode->num_channels + 1) * sizeof(int));
756 if (!freqs)
757 return -1;
758
759 params->freqs = freqs;
760 for (i = 0; i < mode->num_channels; i++) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700761 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
762 continue;
Hai Shalom60840252021-02-19 19:02:11 -0800763 params->freqs[num_chans++] = mode->channels[i].freq;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700764 }
Hai Shalom60840252021-02-19 19:02:11 -0800765 params->freqs[num_chans] = 0;
766
767 return 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700768}
769
770
771static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
772 struct wpa_driver_scan_params *params)
773{
774 if (wpa_s->hw.modes == NULL)
775 return; /* unknown what channels the driver supports */
776 if (params->freqs)
777 return; /* already using a limited channel set */
Hai Shalom60840252021-02-19 19:02:11 -0800778
779 if (wpa_s->setband_mask & WPA_SETBAND_5G)
780 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800781 false);
Hai Shalom60840252021-02-19 19:02:11 -0800782 if (wpa_s->setband_mask & WPA_SETBAND_2G)
783 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800784 false);
Hai Shalom60840252021-02-19 19:02:11 -0800785 if (wpa_s->setband_mask & WPA_SETBAND_6G)
786 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800787 true);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700788}
789
790
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800791static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
792 struct wpa_driver_scan_params *params,
793 size_t max_ssids, const u8 *ssid, size_t ssid_len)
794{
795 unsigned int j;
796
797 for (j = 0; j < params->num_ssids; j++) {
798 if (params->ssids[j].ssid_len == ssid_len &&
799 params->ssids[j].ssid &&
800 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
801 return; /* already in the list */
802 }
803
804 if (params->num_ssids + 1 > max_ssids) {
805 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
806 return;
807 }
808
809 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
810 wpa_ssid_txt(ssid, ssid_len));
811
812 params->ssids[params->num_ssids].ssid = ssid;
813 params->ssids[params->num_ssids].ssid_len = ssid_len;
814 params->num_ssids++;
815}
816
817
818static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
819 struct wpa_driver_scan_params *params,
820 struct wpa_ssid *ssid, size_t max_ssids)
821{
822#ifdef CONFIG_OWE
823 struct wpa_bss *bss;
824
825 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
826 return;
827
828 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
829 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
830
831 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
832 const u8 *owe, *pos, *end;
833 const u8 *owe_ssid;
834 size_t owe_ssid_len;
835
836 if (bss->ssid_len != ssid->ssid_len ||
837 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
838 continue;
839
840 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
841 if (!owe || owe[1] < 4)
842 continue;
843
844 pos = owe + 6;
845 end = owe + 2 + owe[1];
846
847 /* Must include BSSID and ssid_len */
848 if (end - pos < ETH_ALEN + 1)
849 return;
850
851 /* Skip BSSID */
852 pos += ETH_ALEN;
853 owe_ssid_len = *pos++;
854 owe_ssid = pos;
855
856 if ((size_t) (end - pos) < owe_ssid_len ||
857 owe_ssid_len > SSID_MAX_LEN)
858 return;
859
860 wpa_printf(MSG_DEBUG,
861 "OWE: scan_ssids: transition mode OWE ssid=%s",
862 wpa_ssid_txt(owe_ssid, owe_ssid_len));
863
864 wpa_add_scan_ssid(wpa_s, params, max_ssids,
865 owe_ssid, owe_ssid_len);
866 return;
867 }
868#endif /* CONFIG_OWE */
869}
870
871
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700872static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
873 struct wpa_driver_scan_params *params,
874 size_t max_ssids)
875{
876 unsigned int i;
877 struct wpa_ssid *ssid;
878
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700879 /*
880 * For devices with max_ssids greater than 1, leave the last slot empty
881 * for adding the wildcard scan entry.
882 */
883 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
884
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700885 for (i = 0; i < wpa_s->scan_id_count; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700886 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800887 if (!ssid)
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700888 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800889 if (ssid->scan_ssid)
890 wpa_add_scan_ssid(wpa_s, params, max_ssids,
891 ssid->ssid, ssid->ssid_len);
892 /*
893 * Also add the SSID of the OWE BSS, to allow discovery of
894 * transition mode APs more quickly.
895 */
896 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700897 }
898
899 wpa_s->scan_id_count = 0;
900}
901
902
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700903static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
904 struct wpa_driver_scan_params *params,
905 size_t max_ssids)
906{
907 unsigned int i;
908
909 if (wpa_s->ssids_from_scan_req == NULL ||
910 wpa_s->num_ssids_from_scan_req == 0)
911 return 0;
912
913 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
914 wpa_s->num_ssids_from_scan_req = max_ssids;
915 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
916 (unsigned int) max_ssids);
917 }
918
919 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
920 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
921 params->ssids[i].ssid_len =
922 wpa_s->ssids_from_scan_req[i].ssid_len;
923 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
924 params->ssids[i].ssid,
925 params->ssids[i].ssid_len);
926 }
927
928 params->num_ssids = wpa_s->num_ssids_from_scan_req;
929 wpa_s->num_ssids_from_scan_req = 0;
930 return 1;
931}
932
933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
935{
936 struct wpa_supplicant *wpa_s = eloop_ctx;
937 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800938 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700939 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700941 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800943 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944
Dmitry Shmidt29333592017-01-09 12:27:11 -0800945 wpa_s->ignore_post_flush_scan_res = 0;
946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
948 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
949 return;
950 }
951
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800952 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700953 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
955 return;
956 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800957
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700958 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800959 /*
960 * If we are already in scanning state, we shall reschedule the
961 * the incoming scan request.
962 */
963 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
964 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700965 return;
966 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800967
Dmitry Shmidt04949592012-07-19 12:16:46 -0700968 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800969 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
971 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
972 return;
973 }
974
975 if (wpa_s->conf->ap_scan != 0 &&
976 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
977 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
978 "overriding ap_scan configuration");
979 wpa_s->conf->ap_scan = 0;
980 wpas_notify_ap_scan_changed(wpa_s);
981 }
982
983 if (wpa_s->conf->ap_scan == 0) {
984 wpa_supplicant_gen_assoc_event(wpa_s);
985 return;
986 }
987
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800988 ssid = NULL;
989 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
990 wpa_s->connect_without_scan) {
991 connect_without_scan = 1;
992 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
993 if (ssid == wpa_s->connect_without_scan)
994 break;
995 }
996 }
997
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800998 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800999 if (p2p_in_prog && p2p_in_prog != 2 &&
1000 (!ssid ||
1001 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001002 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
1003 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001004 return;
1005 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001006
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001007 /*
1008 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
1009 * used for changing modes inside wpa_supplicant (roaming,
1010 * auto-reconnect, etc). Discarding the scan might hurt these processes.
1011 * The normal use case for PNO is to suspend the host immediately after
1012 * starting PNO, so the periodic 100 ms attempts to run the scan do not
1013 * normally happen in practice multiple times, i.e., this is simply
1014 * restarting scanning once the host is woken up and PNO stopped.
1015 */
1016 if (wpa_s->pno || wpa_s->pno_sched_pending) {
1017 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
1018 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1019 return;
1020 }
1021
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001022 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 max_ssids = 1;
1024 else {
1025 max_ssids = wpa_s->max_scan_ssids;
1026 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1027 max_ssids = WPAS_MAX_SCAN_SSIDS;
1028 }
1029
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001030 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001031 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001033 if (connect_without_scan) {
1034 wpa_s->connect_without_scan = NULL;
1035 if (ssid) {
1036 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1037 "without scan step");
1038 wpa_supplicant_associate(wpa_s, NULL, ssid);
1039 return;
1040 }
1041 }
1042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 os_memset(&params, 0, sizeof(params));
1044
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001045 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1047 wpa_s->wpa_state == WPA_INACTIVE)
1048 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1049
Dmitry Shmidt04949592012-07-19 12:16:46 -07001050 /*
1051 * If autoscan has set its own scanning parameters
1052 */
1053 if (wpa_s->autoscan_params != NULL) {
1054 scan_params = wpa_s->autoscan_params;
1055 goto scan;
1056 }
1057
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001058 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1059 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1060 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1061 goto ssid_list_set;
1062 }
1063
Dmitry Shmidt04949592012-07-19 12:16:46 -07001064#ifdef CONFIG_P2P
1065 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001066 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001067 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1068 wpa_s->p2p_in_provisioning,
1069 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001070 params.ssids[0].ssid = wpa_s->go_params->ssid;
1071 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1072 params.num_ssids = 1;
1073 goto ssid_list_set;
1074 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07001075
1076 if (wpa_s->p2p_in_invitation) {
1077 if (wpa_s->current_ssid) {
1078 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1079 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1080 params.ssids[0].ssid_len =
1081 wpa_s->current_ssid->ssid_len;
1082 params.num_ssids = 1;
1083 } else {
1084 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1085 }
1086 goto ssid_list_set;
1087 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001088#endif /* CONFIG_P2P */
1089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 /* Find the starting point from which to continue scanning */
1091 ssid = wpa_s->conf->ssid;
1092 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1093 while (ssid) {
1094 if (ssid == wpa_s->prev_scan_ssid) {
1095 ssid = ssid->next;
1096 break;
1097 }
1098 ssid = ssid->next;
1099 }
1100 }
1101
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001102 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001103#ifdef CONFIG_AP
1104 !wpa_s->ap_iface &&
1105#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001106 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001107 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001108 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 wpa_supplicant_assoc_try(wpa_s, ssid);
1110 return;
1111 } else if (wpa_s->conf->ap_scan == 2) {
1112 /*
1113 * User-initiated scan request in ap_scan == 2; scan with
1114 * wildcard SSID.
1115 */
1116 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001117 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1118 /*
1119 * Perform single-channel single-SSID scan for
1120 * reassociate-to-same-BSS operation.
1121 */
1122 /* Setup SSID */
1123 ssid = wpa_s->current_ssid;
1124 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1125 ssid->ssid, ssid->ssid_len);
1126 params.ssids[0].ssid = ssid->ssid;
1127 params.ssids[0].ssid_len = ssid->ssid_len;
1128 params.num_ssids = 1;
1129
1130 /*
1131 * Allocate memory for frequency array, allocate one extra
1132 * slot for the zero-terminator.
1133 */
1134 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001135 if (params.freqs) {
1136 params.freqs[0] = wpa_s->assoc_freq;
1137 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001138 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07001139
1140 /*
1141 * Reset the reattach flag so that we fall back to full scan if
1142 * this scan fails.
1143 */
1144 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 } else {
1146 struct wpa_ssid *start = ssid, *tssid;
1147 int freqs_set = 0;
1148 if (ssid == NULL && max_ssids > 1)
1149 ssid = wpa_s->conf->ssid;
1150 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001151 if (!wpas_network_disabled(wpa_s, ssid) &&
1152 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1154 ssid->ssid, ssid->ssid_len);
1155 params.ssids[params.num_ssids].ssid =
1156 ssid->ssid;
1157 params.ssids[params.num_ssids].ssid_len =
1158 ssid->ssid_len;
1159 params.num_ssids++;
1160 if (params.num_ssids + 1 >= max_ssids)
1161 break;
1162 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001163
1164 if (!wpas_network_disabled(wpa_s, ssid)) {
1165 /*
1166 * Also add the SSID of the OWE BSS, to allow
1167 * discovery of transition mode APs more
1168 * quickly.
1169 */
1170 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1171 max_ssids);
1172 }
1173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 ssid = ssid->next;
1175 if (ssid == start)
1176 break;
1177 if (ssid == NULL && max_ssids > 1 &&
1178 start != wpa_s->conf->ssid)
1179 ssid = wpa_s->conf->ssid;
1180 }
1181
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001182 if (wpa_s->scan_id_count &&
1183 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1184 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1185
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001186 for (tssid = wpa_s->conf->ssid;
1187 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1188 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001189 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07001191 if (((params.freqs || !freqs_set) &&
1192 tssid->scan_freq) &&
1193 int_array_len(params.freqs) < 100) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 int_array_concat(&params.freqs,
1195 tssid->scan_freq);
1196 } else {
1197 os_free(params.freqs);
1198 params.freqs = NULL;
1199 }
1200 freqs_set = 1;
1201 }
1202 int_array_sort_unique(params.freqs);
1203 }
1204
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001205 if (ssid && max_ssids == 1) {
1206 /*
1207 * If the driver is limited to 1 SSID at a time interleave
1208 * wildcard SSID scans with specific SSID scans to avoid
1209 * waiting a long time for a wildcard scan.
1210 */
1211 if (!wpa_s->prev_scan_wildcard) {
1212 params.ssids[0].ssid = NULL;
1213 params.ssids[0].ssid_len = 0;
1214 wpa_s->prev_scan_wildcard = 1;
1215 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1216 "wildcard SSID (Interleave with specific)");
1217 } else {
1218 wpa_s->prev_scan_ssid = ssid;
1219 wpa_s->prev_scan_wildcard = 0;
1220 wpa_dbg(wpa_s, MSG_DEBUG,
1221 "Starting AP scan for specific SSID: %s",
1222 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001224 } else if (ssid) {
1225 /* max_ssids > 1 */
1226
1227 wpa_s->prev_scan_ssid = ssid;
1228 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1229 "the scan request");
1230 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001231 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1232 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1233 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001234 } else if (wpa_s->conf->passive_scan) {
1235 wpa_dbg(wpa_s, MSG_DEBUG,
1236 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 } else {
1238 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1239 params.num_ssids++;
1240 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1241 "SSID");
1242 }
1243
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001244ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001246 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001248 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001249 wpa_s->manual_scan_only_new) {
1250 wpa_printf(MSG_DEBUG,
1251 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001252 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001253 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001254
1255 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1256 wpa_s->manual_scan_freqs) {
1257 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1258 params.freqs = wpa_s->manual_scan_freqs;
1259 wpa_s->manual_scan_freqs = NULL;
1260 }
1261
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001262 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1263 wpa_dbg(wpa_s, MSG_DEBUG,
1264 "Limit select_network scan to specified channels");
1265 params.freqs = wpa_s->select_network_scan_freqs;
1266 wpa_s->select_network_scan_freqs = NULL;
1267 }
1268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1270 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1271 "generated frequency list");
1272 params.freqs = wpa_s->next_scan_freqs;
1273 } else
1274 os_free(wpa_s->next_scan_freqs);
1275 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001276 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001278 /* See if user specified frequencies. If so, scan only those. */
Hai Shalom60840252021-02-19 19:02:11 -08001279 if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1280 wpa_s->conf->initial_freq_list && !params.freqs) {
1281 wpa_dbg(wpa_s, MSG_DEBUG,
1282 "Optimize scan based on conf->initial_freq_list");
1283 int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1284 } else if (wpa_s->conf->freq_list && !params.freqs) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001285 wpa_dbg(wpa_s, MSG_DEBUG,
1286 "Optimize scan based on conf->freq_list");
1287 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1288 }
1289
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001290 /* Use current associated channel? */
1291 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001292 unsigned int num = wpa_s->num_multichan_concurrent;
1293
1294 params.freqs = os_calloc(num + 1, sizeof(int));
1295 if (params.freqs) {
1296 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1297 if (num > 0) {
1298 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1299 "current operating channels since "
1300 "scan_cur_freq is enabled");
1301 } else {
1302 os_free(params.freqs);
1303 params.freqs = NULL;
1304 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001305 }
1306 }
1307
Hai Shalomce48b4a2018-09-05 11:41:35 -07001308#ifdef CONFIG_MBO
1309 if (wpa_s->enable_oce & OCE_STA)
1310 params.oce_scan = 1;
1311#endif /* CONFIG_MBO */
1312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1314 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001315 if (extra_ie) {
1316 params.extra_ies = wpabuf_head(extra_ie);
1317 params.extra_ies_len = wpabuf_len(extra_ie);
1318 }
1319
1320#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001321 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001322 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001323 /*
1324 * The interface may not yet be in P2P mode, so we have to
1325 * explicitly request P2P probe to disable CCK rates.
1326 */
1327 params.p2p_probe = 1;
1328 }
1329#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001331 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001332 wpa_s->wpa_state <= WPA_SCANNING)
1333 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001334
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001335 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1336 struct wpa_bss *bss;
1337
1338 params.bssid = wpa_s->next_scan_bssid;
1339 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
Hai Shalomfdcde762020-04-02 11:19:20 -07001340 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1341 bss && bss->ssid_len && params.num_ssids == 1 &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001342 params.ssids[0].ssid_len == 0) {
1343 params.ssids[0].ssid = bss->ssid;
1344 params.ssids[0].ssid_len = bss->ssid_len;
1345 wpa_dbg(wpa_s, MSG_DEBUG,
1346 "Scan a previously specified BSSID " MACSTR
1347 " and SSID %s",
1348 MAC2STR(params.bssid),
1349 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1350 } else {
1351 wpa_dbg(wpa_s, MSG_DEBUG,
1352 "Scan a previously specified BSSID " MACSTR,
1353 MAC2STR(params.bssid));
1354 }
1355 }
1356
Sunil8cd6f4d2022-06-28 18:40:46 +00001357 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1358 wpa_s->manual_non_coloc_6ghz) {
1359 wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1360 params.non_coloc_6ghz = 1;
1361 }
1362
Dmitry Shmidt04949592012-07-19 12:16:46 -07001363 scan_params = &params;
1364
1365scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001366#ifdef CONFIG_P2P
1367 /*
1368 * If the driver does not support multi-channel concurrency and a
1369 * virtual interface that shares the same radio with the wpa_s interface
1370 * is operating there may not be need to scan other channels apart from
1371 * the current operating channel on the other virtual interface. Filter
1372 * out other channels in case we are trying to find a connection for a
1373 * station interface when we are not configured to prefer station
1374 * connection and a concurrent operation is already in process.
1375 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001376 if (wpa_s->scan_for_connection &&
1377 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001378 !scan_params->freqs && !params.freqs &&
1379 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001380 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1381 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001382 unsigned int num = wpa_s->num_multichan_concurrent;
1383
1384 params.freqs = os_calloc(num + 1, sizeof(int));
1385 if (params.freqs) {
1386 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1387 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1388 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1389 } else {
1390 os_free(params.freqs);
1391 params.freqs = NULL;
1392 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001393 }
1394 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001395
1396 if (!params.freqs &&
1397 (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning) &&
1398 !is_p2p_allow_6ghz(wpa_s->global->p2p) &&
1399 is_6ghz_supported(wpa_s)) {
Jimmy Chenafee24e2022-06-17 22:53:15 +08001400 /* Exclude 6 GHz channels from the full scan for P2P connection
Hai Shaloma20dcd72022-02-04 13:43:00 -08001401 * since the 6 GHz band is disabled for P2P uses. */
1402 wpa_printf(MSG_DEBUG,
1403 "P2P: 6 GHz disabled - update the scan frequency list");
Jimmy Chenafee24e2022-06-17 22:53:15 +08001404 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params, false);
1405 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params, false);
1406 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD, &params, false);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001407 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001408#endif /* CONFIG_P2P */
1409
Dmitry Shmidt04949592012-07-19 12:16:46 -07001410 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001412 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1413 !wpa_s->manual_scan_freqs) {
1414 /* Restore manual_scan_freqs for the next attempt */
1415 wpa_s->manual_scan_freqs = params.freqs;
1416 params.freqs = NULL;
1417 }
1418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001419 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 os_free(params.freqs);
1421 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001422 os_free(params.mac_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423
1424 if (ret) {
1425 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001426 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1427 wpa_supplicant_set_state(wpa_s,
1428 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001429 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001430 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001432 } else {
1433 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001434#ifdef CONFIG_INTERWORKING
1435 wpa_s->interworking_fast_assoc_tried = 0;
1436#endif /* CONFIG_INTERWORKING */
Hai Shalomfdcde762020-04-02 11:19:20 -07001437 wpa_s->next_scan_bssid_wildcard_ssid = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001438 if (params.bssid)
1439 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 }
1441}
1442
1443
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001444void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1445{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001446 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001447 int cancelled;
1448
1449 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1450 &remaining);
1451
1452 new_int.sec = sec;
1453 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001454 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001455 new_int.sec = remaining.sec;
1456 new_int.usec = remaining.usec;
1457 }
1458
Dmitry Shmidt051af732013-10-22 13:52:46 -07001459 if (cancelled) {
1460 eloop_register_timeout(new_int.sec, new_int.usec,
1461 wpa_supplicant_scan, wpa_s, NULL);
1462 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001463 wpa_s->scan_interval = sec;
1464}
1465
1466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467/**
1468 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1469 * @wpa_s: Pointer to wpa_supplicant data
1470 * @sec: Number of seconds after which to scan
1471 * @usec: Number of microseconds after which to scan
1472 *
1473 * This function is used to schedule a scan for neighboring access points after
1474 * the specified time.
1475 */
1476void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1477{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001478 int res;
1479
1480 if (wpa_s->p2p_mgmt) {
1481 wpa_dbg(wpa_s, MSG_DEBUG,
1482 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1483 sec, usec);
1484 return;
1485 }
1486
1487 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1488 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001489 if (res == 1) {
1490 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001491 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001492 } else if (res == 0) {
1493 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1494 sec, usec);
1495 } else {
1496 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1497 sec, usec);
1498 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500}
1501
1502
1503/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1505 * @wpa_s: Pointer to wpa_supplicant data
1506 * @sec: Number of seconds after which to scan
1507 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001508 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001509 *
1510 * This function is used to schedule periodic scans for neighboring
1511 * access points after the specified time.
1512 */
1513int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1514 int sec, int usec)
1515{
1516 if (!wpa_s->sched_scan_supported)
1517 return -1;
1518
1519 eloop_register_timeout(sec, usec,
1520 wpa_supplicant_delayed_sched_scan_timeout,
1521 wpa_s, NULL);
1522
1523 return 0;
1524}
1525
1526
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001527static void
1528wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1529 struct wpa_driver_scan_params *params)
1530{
1531 if (wpa_s->wpa_state != WPA_COMPLETED ||
1532 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1533 wpa_s->srp.relative_rssi_set == 0)
1534 return;
1535
1536 params->relative_rssi_set = 1;
1537 params->relative_rssi = wpa_s->srp.relative_rssi;
1538
1539 if (wpa_s->srp.relative_adjust_rssi == 0)
1540 return;
1541
1542 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1543 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1544}
1545
1546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001547/**
1548 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1549 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001550 * Returns: 0 is sched_scan was started 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 repeating the scan continuously.
1554 */
1555int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1556{
1557 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001558 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001559 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001560 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001561 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001562 int ret;
1563 unsigned int max_sched_scan_ssids;
1564 int wildcard = 0;
1565 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001566 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001567
1568 if (!wpa_s->sched_scan_supported)
1569 return -1;
1570
1571 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1572 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1573 else
1574 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001575 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001576 return -1;
1577
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001578 wpa_s->sched_scan_stop_req = 0;
1579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001580 if (wpa_s->sched_scanning) {
1581 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1582 return 0;
1583 }
1584
1585 need_ssids = 0;
1586 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001587 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001588 /* Use wildcard SSID to find this network */
1589 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001590 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1591 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001592 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001593
1594#ifdef CONFIG_WPS
1595 if (!wpas_network_disabled(wpa_s, ssid) &&
1596 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1597 /*
1598 * Normal scan is more reliable and faster for WPS
1599 * operations and since these are for short periods of
1600 * time, the benefit of trying to use sched_scan would
1601 * be limited.
1602 */
1603 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1604 "sched_scan for WPS");
1605 return -1;
1606 }
1607#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608 }
1609 if (wildcard)
1610 need_ssids++;
1611
1612 if (wpa_s->normal_scans < 3 &&
1613 (need_ssids <= wpa_s->max_scan_ssids ||
1614 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1615 /*
1616 * When normal scan can speed up operations, use that for the
1617 * first operations before starting the sched_scan to allow
1618 * user space sleep more. We do this only if the normal scan
1619 * has functionality that is suitable for this or if the
1620 * sched_scan does not have better support for multiple SSIDs.
1621 */
1622 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1623 "sched_scan for initial scans (normal_scans=%d)",
1624 wpa_s->normal_scans);
1625 return -1;
1626 }
1627
1628 os_memset(&params, 0, sizeof(params));
1629
1630 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001631 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001632 sizeof(struct wpa_driver_scan_filter));
1633
1634 prev_state = wpa_s->wpa_state;
1635 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1636 wpa_s->wpa_state == WPA_INACTIVE)
1637 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1638
Dmitry Shmidt04949592012-07-19 12:16:46 -07001639 if (wpa_s->autoscan_params != NULL) {
1640 scan_params = wpa_s->autoscan_params;
1641 goto scan;
1642 }
1643
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001644 /* Find the starting point from which to continue scanning */
1645 ssid = wpa_s->conf->ssid;
1646 if (wpa_s->prev_sched_ssid) {
1647 while (ssid) {
1648 if (ssid == wpa_s->prev_sched_ssid) {
1649 ssid = ssid->next;
1650 break;
1651 }
1652 ssid = ssid->next;
1653 }
1654 }
1655
1656 if (!ssid || !wpa_s->prev_sched_ssid) {
1657 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001658 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1659 wpa_s->first_sched_scan = 1;
1660 ssid = wpa_s->conf->ssid;
1661 wpa_s->prev_sched_ssid = ssid;
1662 }
1663
1664 if (wildcard) {
1665 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1666 params.num_ssids++;
1667 }
1668
1669 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001670 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001671 goto next;
1672
1673 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1674 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1675 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1676 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1677 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1678 ssid->ssid, ssid->ssid_len);
1679 params.filter_ssids[params.num_filter_ssids].ssid_len =
1680 ssid->ssid_len;
1681 params.num_filter_ssids++;
1682 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1683 {
1684 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1685 "filter for sched_scan - drop filter");
1686 os_free(params.filter_ssids);
1687 params.filter_ssids = NULL;
1688 params.num_filter_ssids = 0;
1689 }
1690
1691 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1692 if (params.num_ssids == max_sched_scan_ssids)
1693 break; /* only room for broadcast SSID */
1694 wpa_dbg(wpa_s, MSG_DEBUG,
1695 "add to active scan ssid: %s",
1696 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1697 params.ssids[params.num_ssids].ssid =
1698 ssid->ssid;
1699 params.ssids[params.num_ssids].ssid_len =
1700 ssid->ssid_len;
1701 params.num_ssids++;
1702 if (params.num_ssids >= max_sched_scan_ssids) {
1703 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001704 do {
1705 ssid = ssid->next;
1706 } while (ssid &&
1707 (wpas_network_disabled(wpa_s, ssid) ||
1708 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001709 break;
1710 }
1711 }
1712
1713 next:
1714 wpa_s->prev_sched_ssid = ssid;
1715 ssid = ssid->next;
1716 }
1717
1718 if (params.num_filter_ssids == 0) {
1719 os_free(params.filter_ssids);
1720 params.filter_ssids = NULL;
1721 }
1722
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001723 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1724 if (extra_ie) {
1725 params.extra_ies = wpabuf_head(extra_ie);
1726 params.extra_ies_len = wpabuf_len(extra_ie);
1727 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001728
Dmitry Shmidt18463232014-01-24 12:29:41 -08001729 if (wpa_s->conf->filter_rssi)
1730 params.filter_rssi = wpa_s->conf->filter_rssi;
1731
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001732 /* See if user specified frequencies. If so, scan only those. */
1733 if (wpa_s->conf->freq_list && !params.freqs) {
1734 wpa_dbg(wpa_s, MSG_DEBUG,
1735 "Optimize scan based on conf->freq_list");
1736 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1737 }
1738
Hai Shalomce48b4a2018-09-05 11:41:35 -07001739#ifdef CONFIG_MBO
1740 if (wpa_s->enable_oce & OCE_STA)
1741 params.oce_scan = 1;
1742#endif /* CONFIG_MBO */
1743
Dmitry Shmidt04949592012-07-19 12:16:46 -07001744 scan_params = &params;
1745
1746scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001747 wpa_s->sched_scan_timed_out = 0;
1748
1749 /*
1750 * We cannot support multiple scan plans if the scan request includes
1751 * too many SSID's, so in this case use only the last scan plan and make
1752 * it run infinitely. It will be stopped by the timeout.
1753 */
1754 if (wpa_s->sched_scan_plans_num == 1 ||
1755 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1756 params.sched_scan_plans = wpa_s->sched_scan_plans;
1757 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1758 } else if (wpa_s->sched_scan_plans_num > 1) {
1759 wpa_dbg(wpa_s, MSG_DEBUG,
1760 "Too many SSIDs. Default to using single scheduled_scan plan");
1761 params.sched_scan_plans =
1762 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1763 1];
1764 params.sched_scan_plans_num = 1;
1765 } else {
1766 if (wpa_s->conf->sched_scan_interval)
1767 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1768 else
1769 scan_plan.interval = 10;
1770
1771 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1772 wpa_printf(MSG_WARNING,
1773 "Scan interval too long(%u), use the maximum allowed(%u)",
1774 scan_plan.interval,
1775 wpa_s->max_sched_scan_plan_interval);
1776 scan_plan.interval =
1777 wpa_s->max_sched_scan_plan_interval;
1778 }
1779
1780 scan_plan.iterations = 0;
1781 params.sched_scan_plans = &scan_plan;
1782 params.sched_scan_plans_num = 1;
1783 }
1784
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001785 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1786
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001787 if (ssid || !wpa_s->first_sched_scan) {
1788 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001789 "Starting sched scan after %u seconds: interval %u timeout %d",
1790 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001791 params.sched_scan_plans[0].interval,
1792 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001793 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001794 wpa_dbg(wpa_s, MSG_DEBUG,
1795 "Starting sched scan after %u seconds (no timeout)",
1796 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001797 }
1798
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001799 wpa_setband_scan_freqs(wpa_s, scan_params);
1800
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001801 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001802 wpa_s->wpa_state <= WPA_SCANNING)
1803 wpa_setup_mac_addr_rand_params(&params,
1804 wpa_s->mac_addr_sched_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001805
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001806 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1807
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001808 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001809 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001811 os_free(params.mac_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001812 if (ret) {
1813 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1814 if (prev_state != wpa_s->wpa_state)
1815 wpa_supplicant_set_state(wpa_s, prev_state);
1816 return ret;
1817 }
1818
1819 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1820 if (ssid || !wpa_s->first_sched_scan) {
1821 wpa_s->sched_scan_timed_out = 0;
1822 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1823 wpa_supplicant_sched_scan_timeout,
1824 wpa_s, NULL);
1825 wpa_s->first_sched_scan = 0;
1826 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001827 params.sched_scan_plans[0].interval *= 2;
1828 if ((unsigned int) wpa_s->sched_scan_timeout <
1829 params.sched_scan_plans[0].interval ||
1830 params.sched_scan_plans[0].interval >
1831 wpa_s->max_sched_scan_plan_interval) {
1832 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001833 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1834 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001835 }
1836
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001837 /* If there is no more ssids, start next time from the beginning */
1838 if (!ssid)
1839 wpa_s->prev_sched_ssid = NULL;
1840
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001841 return 0;
1842}
1843
1844
1845/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1847 * @wpa_s: Pointer to wpa_supplicant data
1848 *
1849 * This function is used to cancel a scan request scheduled with
1850 * wpa_supplicant_req_scan().
1851 */
1852void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1853{
1854 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1855 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856}
1857
1858
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001859/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001860 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1861 * @wpa_s: Pointer to wpa_supplicant data
1862 *
1863 * This function is used to stop a delayed scheduled scan.
1864 */
1865void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1866{
1867 if (!wpa_s->sched_scan_supported)
1868 return;
1869
1870 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1871 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1872 wpa_s, NULL);
1873}
1874
1875
1876/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1878 * @wpa_s: Pointer to wpa_supplicant data
1879 *
1880 * This function is used to stop a periodic scheduled scan.
1881 */
1882void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1883{
1884 if (!wpa_s->sched_scanning)
1885 return;
1886
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001887 if (wpa_s->sched_scanning)
1888 wpa_s->sched_scan_stop_req = 1;
1889
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001890 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1891 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1892 wpa_supplicant_stop_sched_scan(wpa_s);
1893}
1894
1895
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001896/**
1897 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1898 * @wpa_s: Pointer to wpa_supplicant data
1899 * @scanning: Whether scanning is currently in progress
1900 *
1901 * This function is to generate scanning notifycations. It is called whenever
1902 * there may have been a change in scanning (scan started, completed, stopped).
1903 * wpas_notify_scanning() is called whenever the scanning state changed from the
1904 * previously notified state.
1905 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1907 int scanning)
1908{
1909 if (wpa_s->scanning != scanning) {
1910 wpa_s->scanning = scanning;
1911 wpas_notify_scanning(wpa_s);
1912 }
1913}
1914
1915
1916static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1917{
1918 int rate = 0;
1919 const u8 *ie;
1920 int i;
1921
1922 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1923 for (i = 0; ie && i < ie[1]; i++) {
1924 if ((ie[i + 2] & 0x7f) > rate)
1925 rate = ie[i + 2] & 0x7f;
1926 }
1927
1928 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1929 for (i = 0; ie && i < ie[1]; i++) {
1930 if ((ie[i + 2] & 0x7f) > rate)
1931 rate = ie[i + 2] & 0x7f;
1932 }
1933
1934 return rate;
1935}
1936
1937
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001938/**
1939 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1940 * @res: Scan result entry
1941 * @ie: Information element identitifier (WLAN_EID_*)
1942 * Returns: Pointer to the information element (id field) or %NULL if not found
1943 *
1944 * This function returns the first matching information element in the scan
1945 * result.
1946 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1948{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001949 size_t ie_len = res->ie_len;
1950
1951 /* Use the Beacon frame IEs if res->ie_len is not available */
1952 if (!ie_len)
1953 ie_len = res->beacon_ie_len;
1954
1955 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956}
1957
1958
Sunil Ravi89eba102022-09-13 21:04:37 -07001959const u8 * wpa_scan_get_ml_ie(const struct wpa_scan_res *res, u8 type)
1960{
1961 size_t ie_len = res->ie_len;
1962
1963 /* Use the Beacon frame IEs if res->ie_len is not available */
1964 if (!ie_len)
1965 ie_len = res->beacon_ie_len;
1966
1967 return get_ml_ie((const u8 *) (res + 1), ie_len, type);
1968}
1969
1970
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001971/**
1972 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1973 * @res: Scan result entry
1974 * @vendor_type: Vendor type (four octets starting the IE payload)
1975 * Returns: Pointer to the information element (id field) or %NULL if not found
1976 *
1977 * This function returns the first matching information element in the scan
1978 * result.
1979 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1981 u32 vendor_type)
1982{
Hai Shalom60840252021-02-19 19:02:11 -08001983 const u8 *ies;
1984 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985
Hai Shalom60840252021-02-19 19:02:11 -08001986 ies = (const u8 *) (res + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987
Hai Shalom60840252021-02-19 19:02:11 -08001988 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
1989 if (elem->datalen >= 4 &&
1990 vendor_type == WPA_GET_BE32(elem->data))
1991 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 }
1993
1994 return NULL;
1995}
1996
1997
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001998/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001999 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
2000 * @res: Scan result entry
2001 * @vendor_type: Vendor type (four octets starting the IE payload)
2002 * Returns: Pointer to the information element (id field) or %NULL if not found
2003 *
2004 * This function returns the first matching information element in the scan
2005 * result.
2006 *
2007 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
2008 * from Beacon frames instead of either Beacon or Probe Response frames.
2009 */
2010const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
2011 u32 vendor_type)
2012{
Hai Shalom60840252021-02-19 19:02:11 -08002013 const u8 *ies;
2014 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002015
2016 if (res->beacon_ie_len == 0)
2017 return NULL;
2018
Hai Shalom60840252021-02-19 19:02:11 -08002019 ies = (const u8 *) (res + 1);
2020 ies += res->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002021
Hai Shalom60840252021-02-19 19:02:11 -08002022 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
2023 res->beacon_ie_len) {
2024 if (elem->datalen >= 4 &&
2025 vendor_type == WPA_GET_BE32(elem->data))
2026 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002027 }
2028
2029 return NULL;
2030}
2031
2032
2033/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002034 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2035 * @res: Scan result entry
2036 * @vendor_type: Vendor type (four octets starting the IE payload)
2037 * Returns: Pointer to the information element payload or %NULL if not found
2038 *
2039 * This function returns concatenated payload of possibly fragmented vendor
2040 * specific information elements in the scan result. The caller is responsible
2041 * for freeing the returned buffer.
2042 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2044 u32 vendor_type)
2045{
2046 struct wpabuf *buf;
2047 const u8 *end, *pos;
2048
2049 buf = wpabuf_alloc(res->ie_len);
2050 if (buf == NULL)
2051 return NULL;
2052
2053 pos = (const u8 *) (res + 1);
2054 end = pos + res->ie_len;
2055
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002056 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08002057 u8 ie, len;
2058
2059 ie = pos[0];
2060 len = pos[1];
2061 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 break;
Hai Shalom60840252021-02-19 19:02:11 -08002063 pos += 2;
2064 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2065 vendor_type == WPA_GET_BE32(pos))
2066 wpabuf_put_data(buf, pos + 4, len - 4);
2067 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 }
2069
2070 if (wpabuf_len(buf) == 0) {
2071 wpabuf_free(buf);
2072 buf = NULL;
2073 }
2074
2075 return buf;
2076}
2077
2078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079/* Compare function for sorting scan results. Return >0 if @b is considered
2080 * better. */
2081static int wpa_scan_result_compar(const void *a, const void *b)
2082{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002083#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 struct wpa_scan_res **_wa = (void *) a;
2085 struct wpa_scan_res **_wb = (void *) b;
2086 struct wpa_scan_res *wa = *_wa;
2087 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002088 int wpa_a, wpa_b;
2089 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090
2091 /* WPA/WPA2 support preferred */
2092 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2093 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2094 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2095 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2096
2097 if (wpa_b && !wpa_a)
2098 return 1;
2099 if (!wpa_b && wpa_a)
2100 return -1;
2101
2102 /* privacy support preferred */
2103 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2104 (wb->caps & IEEE80211_CAP_PRIVACY))
2105 return 1;
2106 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2107 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2108 return -1;
2109
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002110 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002111 snr_a_full = wa->snr;
2112 snr_a = MIN(wa->snr, GREAT_SNR);
2113 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002114 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002115 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002116 /* Level is not in dBm, so we can't calculate
2117 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002118 snr_a = snr_a_full = wa->level;
2119 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002120 }
2121
Hai Shaloma20dcd72022-02-04 13:43:00 -08002122 /* If SNR is close, decide by max rate or frequency band. For cases
2123 * involving the 6 GHz band, use the throughput estimate irrespective
2124 * of the SNR difference since the LPI/VLP rules may result in
2125 * significant differences in SNR for cases where the estimated
2126 * throughput can be considerably higher with the lower SNR. */
2127 if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2128 is_6ghz_freq(wa->freq) ||
2129 is_6ghz_freq(wb->freq))) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002130 if (wa->est_throughput != wb->est_throughput)
Hai Shalom021b0b52019-04-10 11:17:58 -07002131 return (int) wb->est_throughput -
2132 (int) wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002133 }
2134 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2135 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002136 if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2137 return is_6ghz_freq(wa->freq) ? -1 : 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002138 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2139 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 }
2141
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002142 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 * identical, use quality values since some drivers may only report
2144 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002145 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002147 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002148#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149}
2150
2151
2152#ifdef CONFIG_WPS
2153/* Compare function for sorting scan results when searching a WPS AP for
2154 * provisioning. Return >0 if @b is considered better. */
2155static int wpa_scan_result_wps_compar(const void *a, const void *b)
2156{
2157 struct wpa_scan_res **_wa = (void *) a;
2158 struct wpa_scan_res **_wb = (void *) b;
2159 struct wpa_scan_res *wa = *_wa;
2160 struct wpa_scan_res *wb = *_wb;
2161 int uses_wps_a, uses_wps_b;
2162 struct wpabuf *wps_a, *wps_b;
2163 int res;
2164
2165 /* Optimization - check WPS IE existence before allocated memory and
2166 * doing full reassembly. */
2167 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2168 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2169 if (uses_wps_a && !uses_wps_b)
2170 return -1;
2171 if (!uses_wps_a && uses_wps_b)
2172 return 1;
2173
2174 if (uses_wps_a && uses_wps_b) {
2175 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2176 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2177 res = wps_ap_priority_compar(wps_a, wps_b);
2178 wpabuf_free(wps_a);
2179 wpabuf_free(wps_b);
2180 if (res)
2181 return res;
2182 }
2183
2184 /*
2185 * Do not use current AP security policy as a sorting criteria during
2186 * WPS provisioning step since the AP may get reconfigured at the
2187 * completion of provisioning.
2188 */
2189
2190 /* all things being equal, use signal level; if signal levels are
2191 * identical, use quality values since some drivers may only report
2192 * that value and leave the signal level zero */
2193 if (wb->level == wa->level)
2194 return wb->qual - wa->qual;
2195 return wb->level - wa->level;
2196}
2197#endif /* CONFIG_WPS */
2198
2199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002200static void dump_scan_res(struct wpa_scan_results *scan_res)
2201{
2202#ifndef CONFIG_NO_STDOUT_DEBUG
2203 size_t i;
2204
2205 if (scan_res->res == NULL || scan_res->num == 0)
2206 return;
2207
2208 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2209
2210 for (i = 0; i < scan_res->num; i++) {
2211 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002212 u8 *pos;
Sunil Ravia04bd252022-05-02 22:54:18 -07002213 const u8 *ssid_ie, *ssid = NULL;
2214 size_t ssid_len = 0;
2215
2216 ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2217 if (ssid_ie) {
2218 ssid = ssid_ie + 2;
2219 ssid_len = ssid_ie[1];
2220 }
2221
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002222 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002223 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2224
Sunil Ravia04bd252022-05-02 22:54:18 -07002225 wpa_printf(MSG_EXCESSIVE, MACSTR
2226 " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2227 MAC2STR(r->bssid),
2228 wpa_ssid_txt(ssid, ssid_len),
2229 r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002230 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002231 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2232 r->flags,
2233 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002234 } else {
Sunil Ravia04bd252022-05-02 22:54:18 -07002235 wpa_printf(MSG_EXCESSIVE, MACSTR
2236 " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2237 MAC2STR(r->bssid),
2238 wpa_ssid_txt(ssid, ssid_len),
2239 r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002240 r->noise, r->level, r->flags, r->age,
2241 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002242 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002243 pos = (u8 *) (r + 1);
2244 if (r->ie_len)
2245 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2246 pos += r->ie_len;
2247 if (r->beacon_ie_len)
2248 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2249 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002250 }
2251#endif /* CONFIG_NO_STDOUT_DEBUG */
2252}
2253
2254
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002255/**
2256 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2257 * @wpa_s: Pointer to wpa_supplicant data
2258 * @bssid: BSSID to check
2259 * Returns: 0 if the BSSID is filtered or 1 if not
2260 *
2261 * This function is used to filter out specific BSSIDs from scan reslts mainly
2262 * for testing purposes (SET bssid_filter ctrl_iface command).
2263 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002264int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2265 const u8 *bssid)
2266{
2267 size_t i;
2268
2269 if (wpa_s->bssid_filter == NULL)
2270 return 1;
2271
2272 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2273 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2274 ETH_ALEN) == 0)
2275 return 1;
2276 }
2277
2278 return 0;
2279}
2280
2281
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002282void filter_scan_res(struct wpa_supplicant *wpa_s,
2283 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002284{
2285 size_t i, j;
2286
2287 if (wpa_s->bssid_filter == NULL)
2288 return;
2289
2290 for (i = 0, j = 0; i < res->num; i++) {
2291 if (wpa_supplicant_filter_bssid_match(wpa_s,
2292 res->res[i]->bssid)) {
2293 res->res[j++] = res->res[i];
2294 } else {
2295 os_free(res->res[i]);
2296 res->res[i] = NULL;
2297 }
2298 }
2299
2300 if (res->num != j) {
2301 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2302 (int) (res->num - j));
2303 res->num = j;
2304 }
2305}
2306
2307
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002308void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002309{
2310 if (res->flags & WPA_SCAN_NOISE_INVALID) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002311 res->noise = is_6ghz_freq(res->freq) ?
2312 DEFAULT_NOISE_FLOOR_6GHZ :
2313 (IS_5GHZ(res->freq) ?
2314 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002315 }
2316
2317 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2318 res->snr = res->level - res->noise;
2319 } else {
2320 /* Level is not in dBm, so we can't calculate
2321 * SNR. Just use raw level (units unknown). */
2322 res->snr = res->level;
2323 }
2324}
2325
2326
Hai Shalom899fcc72020-10-19 14:38:18 -07002327/* Minimum SNR required to achieve a certain bitrate. */
2328struct minsnr_bitrate_entry {
2329 int minsnr;
2330 unsigned int bitrate; /* in Mbps */
2331};
2332
2333/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2334static const int vht_mcs = 8;
2335
2336static const struct minsnr_bitrate_entry vht20_table[] = {
2337 { 0, 0 },
2338 { 2, 6500 }, /* HT20 MCS0 */
2339 { 5, 13000 }, /* HT20 MCS1 */
2340 { 9, 19500 }, /* HT20 MCS2 */
2341 { 11, 26000 }, /* HT20 MCS3 */
2342 { 15, 39000 }, /* HT20 MCS4 */
2343 { 18, 52000 }, /* HT20 MCS5 */
2344 { 20, 58500 }, /* HT20 MCS6 */
2345 { 25, 65000 }, /* HT20 MCS7 */
2346 { 29, 78000 }, /* VHT20 MCS8 */
2347 { -1, 78000 } /* SNR > 29 */
2348};
2349
2350static const struct minsnr_bitrate_entry vht40_table[] = {
2351 { 0, 0 },
2352 { 5, 13500 }, /* HT40 MCS0 */
2353 { 8, 27000 }, /* HT40 MCS1 */
2354 { 12, 40500 }, /* HT40 MCS2 */
2355 { 14, 54000 }, /* HT40 MCS3 */
2356 { 18, 81000 }, /* HT40 MCS4 */
2357 { 21, 108000 }, /* HT40 MCS5 */
2358 { 23, 121500 }, /* HT40 MCS6 */
2359 { 28, 135000 }, /* HT40 MCS7 */
2360 { 32, 162000 }, /* VHT40 MCS8 */
2361 { 34, 180000 }, /* VHT40 MCS9 */
2362 { -1, 180000 } /* SNR > 34 */
2363};
2364
2365static const struct minsnr_bitrate_entry vht80_table[] = {
2366 { 0, 0 },
2367 { 8, 29300 }, /* VHT80 MCS0 */
2368 { 11, 58500 }, /* VHT80 MCS1 */
2369 { 15, 87800 }, /* VHT80 MCS2 */
2370 { 17, 117000 }, /* VHT80 MCS3 */
2371 { 21, 175500 }, /* VHT80 MCS4 */
2372 { 24, 234000 }, /* VHT80 MCS5 */
2373 { 26, 263300 }, /* VHT80 MCS6 */
2374 { 31, 292500 }, /* VHT80 MCS7 */
2375 { 35, 351000 }, /* VHT80 MCS8 */
2376 { 37, 390000 }, /* VHT80 MCS9 */
2377 { -1, 390000 } /* SNR > 37 */
2378};
2379
2380
Hai Shaloma20dcd72022-02-04 13:43:00 -08002381static const struct minsnr_bitrate_entry vht160_table[] = {
2382 { 0, 0 },
2383 { 11, 58500 }, /* VHT160 MCS0 */
2384 { 14, 117000 }, /* VHT160 MCS1 */
2385 { 18, 175500 }, /* VHT160 MCS2 */
2386 { 20, 234000 }, /* VHT160 MCS3 */
2387 { 24, 351000 }, /* VHT160 MCS4 */
2388 { 27, 468000 }, /* VHT160 MCS5 */
2389 { 29, 526500 }, /* VHT160 MCS6 */
2390 { 34, 585000 }, /* VHT160 MCS7 */
2391 { 38, 702000 }, /* VHT160 MCS8 */
2392 { 40, 780000 }, /* VHT160 MCS9 */
2393 { -1, 780000 } /* SNR > 37 */
2394};
2395
2396
2397static const struct minsnr_bitrate_entry he20_table[] = {
2398 { 0, 0 },
2399 { 2, 8600 }, /* HE20 MCS0 */
2400 { 5, 17200 }, /* HE20 MCS1 */
2401 { 9, 25800 }, /* HE20 MCS2 */
2402 { 11, 34400 }, /* HE20 MCS3 */
2403 { 15, 51600 }, /* HE20 MCS4 */
2404 { 18, 68800 }, /* HE20 MCS5 */
2405 { 20, 77400 }, /* HE20 MCS6 */
2406 { 25, 86000 }, /* HE20 MCS7 */
2407 { 29, 103200 }, /* HE20 MCS8 */
2408 { 31, 114700 }, /* HE20 MCS9 */
2409 { 34, 129000 }, /* HE20 MCS10 */
2410 { 36, 143400 }, /* HE20 MCS11 */
2411 { -1, 143400 } /* SNR > 29 */
2412};
2413
2414static const struct minsnr_bitrate_entry he40_table[] = {
2415 { 0, 0 },
2416 { 5, 17200 }, /* HE40 MCS0 */
2417 { 8, 34400 }, /* HE40 MCS1 */
2418 { 12, 51600 }, /* HE40 MCS2 */
2419 { 14, 68800 }, /* HE40 MCS3 */
2420 { 18, 103200 }, /* HE40 MCS4 */
2421 { 21, 137600 }, /* HE40 MCS5 */
2422 { 23, 154900 }, /* HE40 MCS6 */
2423 { 28, 172100 }, /* HE40 MCS7 */
2424 { 32, 206500 }, /* HE40 MCS8 */
2425 { 34, 229400 }, /* HE40 MCS9 */
2426 { 37, 258100 }, /* HE40 MCS10 */
2427 { 39, 286800 }, /* HE40 MCS11 */
2428 { -1, 286800 } /* SNR > 34 */
2429};
2430
2431static const struct minsnr_bitrate_entry he80_table[] = {
2432 { 0, 0 },
2433 { 8, 36000 }, /* HE80 MCS0 */
2434 { 11, 72100 }, /* HE80 MCS1 */
2435 { 15, 108100 }, /* HE80 MCS2 */
2436 { 17, 144100 }, /* HE80 MCS3 */
2437 { 21, 216200 }, /* HE80 MCS4 */
2438 { 24, 288200 }, /* HE80 MCS5 */
2439 { 26, 324300 }, /* HE80 MCS6 */
2440 { 31, 360300 }, /* HE80 MCS7 */
2441 { 35, 432400 }, /* HE80 MCS8 */
2442 { 37, 480400 }, /* HE80 MCS9 */
2443 { 40, 540400 }, /* HE80 MCS10 */
2444 { 42, 600500 }, /* HE80 MCS11 */
2445 { -1, 600500 } /* SNR > 37 */
2446};
2447
2448
2449static const struct minsnr_bitrate_entry he160_table[] = {
2450 { 0, 0 },
2451 { 11, 72100 }, /* HE160 MCS0 */
2452 { 14, 144100 }, /* HE160 MCS1 */
2453 { 18, 216200 }, /* HE160 MCS2 */
2454 { 20, 288200 }, /* HE160 MCS3 */
2455 { 24, 432400 }, /* HE160 MCS4 */
2456 { 27, 576500 }, /* HE160 MCS5 */
2457 { 29, 648500 }, /* HE160 MCS6 */
2458 { 34, 720600 }, /* HE160 MCS7 */
2459 { 38, 864700 }, /* HE160 MCS8 */
2460 { 40, 960800 }, /* HE160 MCS9 */
2461 { 43, 1080900 }, /* HE160 MCS10 */
2462 { 45, 1201000 }, /* HE160 MCS11 */
2463 { -1, 1201000 } /* SNR > 37 */
2464};
2465
2466
Hai Shalomfdcde762020-04-02 11:19:20 -07002467static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2468 int rate0, int rate1)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002469{
Hai Shalomfdcde762020-04-02 11:19:20 -07002470 return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002471}
2472
2473
Hai Shalom899fcc72020-10-19 14:38:18 -07002474static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2475 int snr, bool vht)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002476{
Hai Shalom899fcc72020-10-19 14:38:18 -07002477 const struct minsnr_bitrate_entry *prev, *entry = table;
2478
2479 while ((entry->minsnr != -1) &&
2480 (snr >= entry->minsnr) &&
2481 (vht || entry - table <= vht_mcs))
2482 entry++;
2483 if (entry == table)
2484 return entry->bitrate;
2485 prev = entry - 1;
2486 if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2487 return prev->bitrate;
2488 return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2489 entry->bitrate);
Hai Shalomfdcde762020-04-02 11:19:20 -07002490}
2491
2492
Hai Shalom899fcc72020-10-19 14:38:18 -07002493static unsigned int max_ht20_rate(int snr, bool vht)
Hai Shalomfdcde762020-04-02 11:19:20 -07002494{
Hai Shalom899fcc72020-10-19 14:38:18 -07002495 return max_rate(vht20_table, snr, vht);
2496}
2497
2498
2499static unsigned int max_ht40_rate(int snr, bool vht)
2500{
2501 return max_rate(vht40_table, snr, vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002502}
2503
2504
2505static unsigned int max_vht80_rate(int snr)
2506{
Hai Shalom899fcc72020-10-19 14:38:18 -07002507 return max_rate(vht80_table, snr, 1);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002508}
2509
Hai Shalomfdcde762020-04-02 11:19:20 -07002510
Hai Shaloma20dcd72022-02-04 13:43:00 -08002511static unsigned int max_vht160_rate(int snr)
2512{
2513 return max_rate(vht160_table, snr, 1);
2514}
2515
2516
2517static unsigned int max_he_rate(const struct minsnr_bitrate_entry table[],
2518 int snr)
2519{
2520 const struct minsnr_bitrate_entry *prev, *entry = table;
2521
2522 while (entry->minsnr != -1 && snr >= entry->minsnr)
2523 entry++;
2524 if (entry == table)
2525 return 0;
2526 prev = entry - 1;
2527 if (entry->minsnr == -1)
2528 return prev->bitrate;
2529 return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2530 prev->bitrate, entry->bitrate);
2531}
2532
2533
Hai Shalomfdcde762020-04-02 11:19:20 -07002534unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2535 const u8 *ies, size_t ies_len, int rate,
Hai Shaloma20dcd72022-02-04 13:43:00 -08002536 int snr, int freq)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002537{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002538 struct hostapd_hw_modes *hw_mode;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002539 unsigned int est, tmp;
Hai Shalomfdcde762020-04-02 11:19:20 -07002540 const u8 *ie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002541
2542 /* Limit based on estimated SNR */
2543 if (rate > 1 * 2 && snr < 1)
2544 rate = 1 * 2;
2545 else if (rate > 2 * 2 && snr < 4)
2546 rate = 2 * 2;
2547 else if (rate > 6 * 2 && snr < 5)
2548 rate = 6 * 2;
2549 else if (rate > 9 * 2 && snr < 6)
2550 rate = 9 * 2;
2551 else if (rate > 12 * 2 && snr < 7)
2552 rate = 12 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002553 else if (rate > 12 * 2 && snr < 8)
2554 rate = 14 * 2;
2555 else if (rate > 12 * 2 && snr < 9)
2556 rate = 16 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002557 else if (rate > 18 * 2 && snr < 10)
2558 rate = 18 * 2;
2559 else if (rate > 24 * 2 && snr < 11)
2560 rate = 24 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002561 else if (rate > 24 * 2 && snr < 12)
2562 rate = 27 * 2;
2563 else if (rate > 24 * 2 && snr < 13)
2564 rate = 30 * 2;
2565 else if (rate > 24 * 2 && snr < 14)
2566 rate = 33 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002567 else if (rate > 36 * 2 && snr < 15)
2568 rate = 36 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002569 else if (rate > 36 * 2 && snr < 16)
2570 rate = 39 * 2;
2571 else if (rate > 36 * 2 && snr < 17)
2572 rate = 42 * 2;
2573 else if (rate > 36 * 2 && snr < 18)
2574 rate = 45 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002575 else if (rate > 48 * 2 && snr < 19)
2576 rate = 48 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002577 else if (rate > 48 * 2 && snr < 20)
2578 rate = 51 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002579 else if (rate > 54 * 2 && snr < 21)
2580 rate = 54 * 2;
2581 est = rate * 500;
2582
Hai Shaloma20dcd72022-02-04 13:43:00 -08002583 hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2584 freq);
2585
2586 if (hw_mode && hw_mode->ht_capab) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002587 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002588 if (ie) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002589 tmp = max_ht20_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002590 if (tmp > est)
2591 est = tmp;
2592 }
2593 }
2594
Hai Shaloma20dcd72022-02-04 13:43:00 -08002595 if (hw_mode &&
2596 (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002597 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002598 if (ie && ie[1] >= 2 &&
2599 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002600 tmp = max_ht40_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002601 if (tmp > est)
2602 est = tmp;
2603 }
2604 }
2605
Hai Shaloma20dcd72022-02-04 13:43:00 -08002606 if (hw_mode && hw_mode->vht_capab) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002607 /* Use +1 to assume VHT is always faster than HT */
Hai Shalomfdcde762020-04-02 11:19:20 -07002608 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002609 if (ie) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002610 bool vht80 = false, vht160 = false;
2611
Hai Shalom899fcc72020-10-19 14:38:18 -07002612 tmp = max_ht20_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002613 if (tmp > est)
2614 est = tmp;
2615
Hai Shalomfdcde762020-04-02 11:19:20 -07002616 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002617 if (ie && ie[1] >= 2 &&
2618 (ie[3] &
2619 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002620 tmp = max_ht40_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002621 if (tmp > est)
2622 est = tmp;
2623 }
2624
Hai Shaloma20dcd72022-02-04 13:43:00 -08002625 /* Determine VHT BSS bandwidth based on IEEE Std
2626 * 802.11-2020, Table 11-23 (VHT BSs bandwidth) */
Hai Shalomfdcde762020-04-02 11:19:20 -07002627 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002628 if (ie && ie[1] >= 3) {
2629 u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2630 u8 seg0 = ie[3];
2631 u8 seg1 = ie[4];
2632
2633 if (cw)
2634 vht80 = true;
2635 if (cw == 2 ||
2636 (cw == 3 &&
2637 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2638 vht160 = true;
2639 if (cw == 1 &&
2640 ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2641 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2642 vht160 = true;
2643 }
2644
2645 if (vht80) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002646 tmp = max_vht80_rate(snr) + 1;
2647 if (tmp > est)
2648 est = tmp;
2649 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08002650
2651 if (vht160 &&
2652 (hw_mode->vht_capab &
2653 (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2654 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
2655 tmp = max_vht160_rate(snr) + 1;
2656 if (tmp > est)
2657 est = tmp;
2658 }
2659 }
2660 }
2661
2662 if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
2663 /* Use +2 to assume HE is always faster than HT/VHT */
2664 struct ieee80211_he_capabilities *he;
2665 struct he_capabilities *own_he;
2666 u8 cw;
2667
2668 ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
2669 if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
2670 return est;
2671 he = (struct ieee80211_he_capabilities *) &ie[3];
2672 own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
2673
2674 tmp = max_he_rate(he20_table, snr) + 2;
2675 if (tmp > est)
2676 est = tmp;
2677
2678 cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
2679 own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
2680 if (cw &
2681 (IS_2P4GHZ(freq) ? HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
2682 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2683 tmp = max_he_rate(he40_table, snr) + 2;
2684 if (tmp > est)
2685 est = tmp;
2686 }
2687
2688 if (!IS_2P4GHZ(freq) &&
2689 (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2690 tmp = max_he_rate(he80_table, snr) + 2;
2691 if (tmp > est)
2692 est = tmp;
2693 }
2694
2695 if (!IS_2P4GHZ(freq) &&
2696 (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2697 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G))) {
2698 tmp = max_he_rate(he160_table, snr) + 2;
2699 if (tmp > est)
2700 est = tmp;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002701 }
2702 }
2703
Hai Shalomfdcde762020-04-02 11:19:20 -07002704 return est;
2705}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002706
Hai Shalomfdcde762020-04-02 11:19:20 -07002707
2708void scan_est_throughput(struct wpa_supplicant *wpa_s,
2709 struct wpa_scan_res *res)
2710{
2711 int rate; /* max legacy rate in 500 kb/s units */
2712 int snr = res->snr;
2713 const u8 *ies = (const void *) (res + 1);
2714 size_t ie_len = res->ie_len;
2715
2716 if (res->est_throughput)
2717 return;
2718
2719 /* Get maximum legacy rate */
2720 rate = wpa_scan_get_max_rate(res);
2721
2722 if (!ie_len)
2723 ie_len = res->beacon_ie_len;
2724 res->est_throughput =
Hai Shaloma20dcd72022-02-04 13:43:00 -08002725 wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, res->freq);
Hai Shalomfdcde762020-04-02 11:19:20 -07002726
2727 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002728}
2729
2730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731/**
2732 * wpa_supplicant_get_scan_results - Get scan results
2733 * @wpa_s: Pointer to wpa_supplicant data
2734 * @info: Information about what was scanned or %NULL if not available
2735 * @new_scan: Whether a new scan was performed
2736 * Returns: Scan results, %NULL on failure
2737 *
2738 * This function request the current scan results from the driver and updates
2739 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2740 * results with wpa_scan_results_free().
2741 */
2742struct wpa_scan_results *
2743wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2744 struct scan_info *info, int new_scan)
2745{
2746 struct wpa_scan_results *scan_res;
2747 size_t i;
2748 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2749
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002750 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 if (scan_res == NULL) {
2752 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2753 return NULL;
2754 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002755 if (scan_res->fetch_time.sec == 0) {
2756 /*
2757 * Make sure we have a valid timestamp if the driver wrapper
2758 * does not set this.
2759 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002760 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002761 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002762 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002764 for (i = 0; i < scan_res->num; i++) {
2765 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2766
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002767 scan_snr(scan_res_item);
2768 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002769 }
2770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002772 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2774 "provisioning rules");
2775 compar = wpa_scan_result_wps_compar;
2776 }
2777#endif /* CONFIG_WPS */
2778
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002779 if (scan_res->res) {
2780 qsort(scan_res->res, scan_res->num,
2781 sizeof(struct wpa_scan_res *), compar);
2782 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002783 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784
Dmitry Shmidt29333592017-01-09 12:27:11 -08002785 if (wpa_s->ignore_post_flush_scan_res) {
2786 /* FLUSH command aborted an ongoing scan and these are the
2787 * results from the aborted scan. Do not process the results to
2788 * maintain flushed state. */
2789 wpa_dbg(wpa_s, MSG_DEBUG,
2790 "Do not update BSS table based on pending post-FLUSH scan results");
2791 wpa_s->ignore_post_flush_scan_res = 0;
2792 return scan_res;
2793 }
2794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 wpa_bss_update_start(wpa_s);
2796 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002797 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2798 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799 wpa_bss_update_end(wpa_s, info, new_scan);
2800
2801 return scan_res;
2802}
2803
2804
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002805/**
2806 * wpa_supplicant_update_scan_results - Update scan results from the driver
2807 * @wpa_s: Pointer to wpa_supplicant data
2808 * Returns: 0 on success, -1 on failure
2809 *
2810 * This function updates the BSS table within wpa_supplicant based on the
2811 * currently available scan results from the driver without requesting a new
2812 * scan. This is used in cases where the driver indicates an association
2813 * (including roaming within ESS) and wpa_supplicant does not yet have the
2814 * needed information to complete the connection (e.g., to perform validation
2815 * steps in 4-way handshake).
2816 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2818{
2819 struct wpa_scan_results *scan_res;
2820 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2821 if (scan_res == NULL)
2822 return -1;
2823 wpa_scan_results_free(scan_res);
2824
2825 return 0;
2826}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002827
2828
2829/**
2830 * scan_only_handler - Reports scan results
2831 */
2832void scan_only_handler(struct wpa_supplicant *wpa_s,
2833 struct wpa_scan_results *scan_res)
2834{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002835 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002836 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2837 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2838 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2839 wpa_s->manual_scan_id);
2840 wpa_s->manual_scan_use_id = 0;
2841 } else {
2842 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2843 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002844 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002845 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002846 if (wpa_s->scan_work) {
2847 struct wpa_radio_work *work = wpa_s->scan_work;
2848 wpa_s->scan_work = NULL;
2849 radio_work_done(work);
2850 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002851
2852 if (wpa_s->wpa_state == WPA_SCANNING)
2853 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002854}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002855
2856
2857int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2858{
2859 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2860}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002861
2862
2863struct wpa_driver_scan_params *
2864wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2865{
2866 struct wpa_driver_scan_params *params;
2867 size_t i;
2868 u8 *n;
2869
2870 params = os_zalloc(sizeof(*params));
2871 if (params == NULL)
2872 return NULL;
2873
2874 for (i = 0; i < src->num_ssids; i++) {
2875 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002876 n = os_memdup(src->ssids[i].ssid,
2877 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002878 if (n == NULL)
2879 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002880 params->ssids[i].ssid = n;
2881 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2882 }
2883 }
2884 params->num_ssids = src->num_ssids;
2885
2886 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002887 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002888 if (n == NULL)
2889 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002890 params->extra_ies = n;
2891 params->extra_ies_len = src->extra_ies_len;
2892 }
2893
2894 if (src->freqs) {
2895 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002896 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002897 if (params->freqs == NULL)
2898 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002899 }
2900
2901 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002902 params->filter_ssids = os_memdup(src->filter_ssids,
2903 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002904 src->num_filter_ssids);
2905 if (params->filter_ssids == NULL)
2906 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002907 params->num_filter_ssids = src->num_filter_ssids;
2908 }
2909
2910 params->filter_rssi = src->filter_rssi;
2911 params->p2p_probe = src->p2p_probe;
2912 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002913 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002914 params->duration = src->duration;
2915 params->duration_mandatory = src->duration_mandatory;
Hai Shalomce48b4a2018-09-05 11:41:35 -07002916 params->oce_scan = src->oce_scan;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002917
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002918 if (src->sched_scan_plans_num > 0) {
2919 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002920 os_memdup(src->sched_scan_plans,
2921 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002922 src->sched_scan_plans_num);
2923 if (!params->sched_scan_plans)
2924 goto failed;
2925
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002926 params->sched_scan_plans_num = src->sched_scan_plans_num;
2927 }
2928
Hai Shalomc3565922019-10-28 11:58:20 -07002929 if (src->mac_addr_rand &&
2930 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
2931 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002932
2933 if (src->bssid) {
2934 u8 *bssid;
2935
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002936 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002937 if (!bssid)
2938 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002939 params->bssid = bssid;
2940 }
2941
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002942 params->relative_rssi_set = src->relative_rssi_set;
2943 params->relative_rssi = src->relative_rssi;
2944 params->relative_adjust_band = src->relative_adjust_band;
2945 params->relative_adjust_rssi = src->relative_adjust_rssi;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002946 params->p2p_include_6ghz = src->p2p_include_6ghz;
Sunil8cd6f4d2022-06-28 18:40:46 +00002947 params->non_coloc_6ghz = src->non_coloc_6ghz;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002948 return params;
2949
2950failed:
2951 wpa_scan_free_params(params);
2952 return NULL;
2953}
2954
2955
2956void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2957{
2958 size_t i;
2959
2960 if (params == NULL)
2961 return;
2962
2963 for (i = 0; i < params->num_ssids; i++)
2964 os_free((u8 *) params->ssids[i].ssid);
2965 os_free((u8 *) params->extra_ies);
2966 os_free(params->freqs);
2967 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002968 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002969
2970 /*
2971 * Note: params->mac_addr_mask points to same memory allocation and
2972 * must not be freed separately.
2973 */
2974 os_free((u8 *) params->mac_addr);
2975
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002976 os_free((u8 *) params->bssid);
2977
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002978 os_free(params);
2979}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002980
2981
2982int wpas_start_pno(struct wpa_supplicant *wpa_s)
2983{
Hai Shalomfdcde762020-04-02 11:19:20 -07002984 int ret;
2985 size_t prio, i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002986 struct wpa_ssid *ssid;
2987 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002988 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002989 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002990
2991 if (!wpa_s->sched_scan_supported)
2992 return -1;
2993
Dmitry Shmidte4663042016-04-04 10:07:49 -07002994 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2995 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2996 else
2997 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2998 if (max_sched_scan_ssids < 1)
2999 return -1;
3000
Dmitry Shmidt98660862014-03-11 17:26:21 -07003001 if (wpa_s->pno || wpa_s->pno_sched_pending)
3002 return 0;
3003
3004 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003005 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003006 wpa_printf(MSG_ERROR, "PNO: In assoc process");
3007 return -EAGAIN;
3008 }
3009
3010 if (wpa_s->wpa_state == WPA_SCANNING) {
3011 wpa_supplicant_cancel_scan(wpa_s);
3012 if (wpa_s->sched_scanning) {
3013 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
3014 "ongoing sched scan");
3015 wpa_supplicant_cancel_sched_scan(wpa_s);
3016 wpa_s->pno_sched_pending = 1;
3017 return 0;
3018 }
3019 }
3020
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003021 if (wpa_s->sched_scan_stop_req) {
3022 wpa_printf(MSG_DEBUG,
3023 "Schedule PNO after previous sched scan has stopped");
3024 wpa_s->pno_sched_pending = 1;
3025 return 0;
3026 }
3027
Dmitry Shmidt98660862014-03-11 17:26:21 -07003028 os_memset(&params, 0, sizeof(params));
3029
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003030 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003031 ssid = wpa_s->conf->ssid;
3032 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003033 if (!wpas_network_disabled(wpa_s, ssid)) {
3034 num_match_ssid++;
3035 if (ssid->scan_ssid)
3036 num_ssid++;
3037 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003038 ssid = ssid->next;
3039 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003040
3041 if (num_match_ssid == 0) {
3042 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3043 return -1;
3044 }
3045
3046 if (num_match_ssid > num_ssid) {
3047 params.num_ssids++; /* wildcard */
3048 num_ssid++;
3049 }
3050
Dmitry Shmidte4663042016-04-04 10:07:49 -07003051 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003052 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07003053 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3054 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003055 }
3056
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003057 if (num_match_ssid > wpa_s->max_match_sets) {
3058 num_match_ssid = wpa_s->max_match_sets;
3059 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003060 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003061 params.filter_ssids = os_calloc(num_match_ssid,
3062 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07003063 if (params.filter_ssids == NULL)
3064 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003065
Dmitry Shmidt98660862014-03-11 17:26:21 -07003066 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003067 prio = 0;
3068 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003069 while (ssid) {
3070 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003071 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3072 params.ssids[params.num_ssids].ssid =
3073 ssid->ssid;
3074 params.ssids[params.num_ssids].ssid_len =
3075 ssid->ssid_len;
3076 params.num_ssids++;
3077 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003078 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3079 ssid->ssid_len);
3080 params.filter_ssids[i].ssid_len = ssid->ssid_len;
3081 params.num_filter_ssids++;
3082 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003083 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07003084 break;
3085 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003086 if (ssid->pnext)
3087 ssid = ssid->pnext;
3088 else if (prio + 1 == wpa_s->conf->num_prio)
3089 break;
3090 else
3091 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003092 }
3093
3094 if (wpa_s->conf->filter_rssi)
3095 params.filter_rssi = wpa_s->conf->filter_rssi;
3096
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003097 if (wpa_s->sched_scan_plans_num) {
3098 params.sched_scan_plans = wpa_s->sched_scan_plans;
3099 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3100 } else {
3101 /* Set one scan plan that will run infinitely */
3102 if (wpa_s->conf->sched_scan_interval)
3103 scan_plan.interval = wpa_s->conf->sched_scan_interval;
3104 else
3105 scan_plan.interval = 10;
3106
3107 scan_plan.iterations = 0;
3108 params.sched_scan_plans = &scan_plan;
3109 params.sched_scan_plans_num = 1;
3110 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003111
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003112 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3113
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003114 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3115 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3116 params.freqs = wpa_s->manual_sched_scan_freqs;
3117 }
3118
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003119 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
Hai Shalomc3565922019-10-28 11:58:20 -07003120 wpa_s->wpa_state <= WPA_SCANNING)
3121 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003122
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003123 wpa_scan_set_relative_rssi_params(wpa_s, &params);
3124
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003125 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003126 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07003127 os_free(params.mac_addr);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003128 if (ret == 0)
3129 wpa_s->pno = 1;
3130 else
3131 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3132 return ret;
3133}
3134
3135
3136int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3137{
3138 int ret = 0;
3139
3140 if (!wpa_s->pno)
3141 return 0;
3142
3143 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003144 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003145
3146 wpa_s->pno = 0;
3147 wpa_s->pno_sched_pending = 0;
3148
3149 if (wpa_s->wpa_state == WPA_SCANNING)
3150 wpa_supplicant_req_scan(wpa_s, 0, 0);
3151
3152 return ret;
3153}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003154
3155
3156void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3157 unsigned int type)
3158{
3159 type &= MAC_ADDR_RAND_ALL;
3160 wpa_s->mac_addr_rand_enable &= ~type;
3161
3162 if (type & MAC_ADDR_RAND_SCAN) {
3163 os_free(wpa_s->mac_addr_scan);
3164 wpa_s->mac_addr_scan = NULL;
3165 }
3166
3167 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3168 os_free(wpa_s->mac_addr_sched_scan);
3169 wpa_s->mac_addr_sched_scan = NULL;
3170 }
3171
3172 if (type & MAC_ADDR_RAND_PNO) {
3173 os_free(wpa_s->mac_addr_pno);
3174 wpa_s->mac_addr_pno = NULL;
3175 }
3176}
3177
3178
3179int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3180 unsigned int type, const u8 *addr,
3181 const u8 *mask)
3182{
3183 u8 *tmp = NULL;
3184
Hai Shalom74f70d42019-02-11 14:42:39 -08003185 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3186 wpa_printf(MSG_INFO,
3187 "scan: MAC randomization type %u != supported=%u",
3188 type, wpa_s->mac_addr_rand_supported);
3189 return -1;
3190 }
3191
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003192 wpas_mac_addr_rand_scan_clear(wpa_s, type);
3193
3194 if (addr) {
3195 tmp = os_malloc(2 * ETH_ALEN);
3196 if (!tmp)
3197 return -1;
3198 os_memcpy(tmp, addr, ETH_ALEN);
3199 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3200 }
3201
3202 if (type == MAC_ADDR_RAND_SCAN) {
3203 wpa_s->mac_addr_scan = tmp;
3204 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3205 wpa_s->mac_addr_sched_scan = tmp;
3206 } else if (type == MAC_ADDR_RAND_PNO) {
3207 wpa_s->mac_addr_pno = tmp;
3208 } else {
3209 wpa_printf(MSG_INFO,
3210 "scan: Invalid MAC randomization type=0x%x",
3211 type);
3212 os_free(tmp);
3213 return -1;
3214 }
3215
3216 wpa_s->mac_addr_rand_enable |= type;
3217 return 0;
3218}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003219
3220
Hai Shalomc3565922019-10-28 11:58:20 -07003221int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3222 unsigned int type, u8 *mask)
3223{
3224 const u8 *to_copy;
3225
3226 if ((wpa_s->mac_addr_rand_enable & type) != type)
3227 return -1;
3228
3229 if (type == MAC_ADDR_RAND_SCAN) {
3230 to_copy = wpa_s->mac_addr_scan;
3231 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3232 to_copy = wpa_s->mac_addr_sched_scan;
3233 } else if (type == MAC_ADDR_RAND_PNO) {
3234 to_copy = wpa_s->mac_addr_pno;
3235 } else {
3236 wpa_printf(MSG_DEBUG,
3237 "scan: Invalid MAC randomization type=0x%x",
3238 type);
3239 return -1;
3240 }
3241
3242 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3243 return 0;
3244}
3245
3246
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003247int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3248{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003249 struct wpa_radio_work *work;
3250 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003251
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003252 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3253 if (work->wpa_s != wpa_s || !work->started ||
3254 (os_strcmp(work->type, "scan") != 0 &&
3255 os_strcmp(work->type, "p2p-scan") != 0))
3256 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003257 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003258 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003259 }
3260
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003261 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3262 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003263}
3264
3265
3266int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3267{
3268 struct sched_scan_plan *scan_plans = NULL;
3269 const char *token, *context = NULL;
3270 unsigned int num = 0;
3271
3272 if (!cmd)
3273 return -1;
3274
3275 if (!cmd[0]) {
3276 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3277 os_free(wpa_s->sched_scan_plans);
3278 wpa_s->sched_scan_plans = NULL;
3279 wpa_s->sched_scan_plans_num = 0;
3280 return 0;
3281 }
3282
3283 while ((token = cstr_token(cmd, " ", &context))) {
3284 int ret;
3285 struct sched_scan_plan *scan_plan, *n;
3286
3287 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3288 if (!n)
3289 goto fail;
3290
3291 scan_plans = n;
3292 scan_plan = &scan_plans[num];
3293 num++;
3294
3295 ret = sscanf(token, "%u:%u", &scan_plan->interval,
3296 &scan_plan->iterations);
3297 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3298 wpa_printf(MSG_ERROR,
3299 "Invalid sched scan plan input: %s", token);
3300 goto fail;
3301 }
3302
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003303 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3304 wpa_printf(MSG_WARNING,
3305 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3306 num, scan_plan->interval,
3307 wpa_s->max_sched_scan_plan_interval);
3308 scan_plan->interval =
3309 wpa_s->max_sched_scan_plan_interval;
3310 }
3311
3312 if (ret == 1) {
3313 scan_plan->iterations = 0;
3314 break;
3315 }
3316
3317 if (!scan_plan->iterations) {
3318 wpa_printf(MSG_ERROR,
3319 "scan plan %u: Number of iterations cannot be zero",
3320 num);
3321 goto fail;
3322 }
3323
3324 if (scan_plan->iterations >
3325 wpa_s->max_sched_scan_plan_iterations) {
3326 wpa_printf(MSG_WARNING,
3327 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3328 num, scan_plan->iterations,
3329 wpa_s->max_sched_scan_plan_iterations);
3330 scan_plan->iterations =
3331 wpa_s->max_sched_scan_plan_iterations;
3332 }
3333
3334 wpa_printf(MSG_DEBUG,
3335 "scan plan %u: interval=%u iterations=%u",
3336 num, scan_plan->interval, scan_plan->iterations);
3337 }
3338
3339 if (!scan_plans) {
3340 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3341 goto fail;
3342 }
3343
3344 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3345 wpa_printf(MSG_ERROR,
3346 "All scan plans but the last must specify a number of iterations");
3347 goto fail;
3348 }
3349
3350 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3351 num, scan_plans[num - 1].interval);
3352
3353 if (num > wpa_s->max_sched_scan_plans) {
3354 wpa_printf(MSG_WARNING,
3355 "Too many scheduled scan plans (only %u supported)",
3356 wpa_s->max_sched_scan_plans);
3357 wpa_printf(MSG_WARNING,
3358 "Use only the first %u scan plans, and the last one (in infinite loop)",
3359 wpa_s->max_sched_scan_plans - 1);
3360 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3361 &scan_plans[num - 1], sizeof(*scan_plans));
3362 num = wpa_s->max_sched_scan_plans;
3363 }
3364
3365 os_free(wpa_s->sched_scan_plans);
3366 wpa_s->sched_scan_plans = scan_plans;
3367 wpa_s->sched_scan_plans_num = num;
3368
3369 return 0;
3370
3371fail:
3372 os_free(scan_plans);
3373 wpa_printf(MSG_ERROR, "invalid scan plans list");
3374 return -1;
3375}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003376
3377
3378/**
3379 * wpas_scan_reset_sched_scan - Reset sched_scan state
3380 * @wpa_s: Pointer to wpa_supplicant data
3381 *
3382 * This function is used to cancel a running scheduled scan and to reset an
3383 * internal scan state to continue with a regular scan on the following
3384 * wpa_supplicant_req_scan() calls.
3385 */
3386void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3387{
3388 wpa_s->normal_scans = 0;
3389 if (wpa_s->sched_scanning) {
3390 wpa_s->sched_scan_timed_out = 0;
3391 wpa_s->prev_sched_ssid = NULL;
3392 wpa_supplicant_cancel_sched_scan(wpa_s);
3393 }
3394}
3395
3396
3397void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3398{
3399 /* simulate timeout to restart the sched scan */
3400 wpa_s->sched_scan_timed_out = 1;
3401 wpa_s->prev_sched_ssid = NULL;
3402 wpa_supplicant_cancel_sched_scan(wpa_s);
3403}