blob: 57f5747036e42262eef79f75aa3ab47e5e4d939d [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt3a787e62013-01-17 10:32:35 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "config.h"
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "wps_supplicant.h"
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "notify.h"
23#include "bss.h"
24#include "scan.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080025#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026
27
28static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29{
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
32
33 ssid = wpa_supplicant_get_ssid(wpa_s);
34 if (ssid == NULL)
35 return;
36
37 if (wpa_s->current_ssid == NULL) {
38 wpa_s->current_ssid = ssid;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070039 wpas_notify_network_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040 }
41 wpa_supplicant_initiate_eapol(wpa_s);
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
49#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 enum wps_request_type *req_type)
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
55
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
61 *req_type = wpas_wps_get_req_type(ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -070062 if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 return 2;
64 }
65
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080067 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
68 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080069 wpa_s->wps->dev.p2p = 1;
70 if (!wps) {
71 wps = 1;
72 *req_type = WPS_REQ_ENROLLEE_INFO;
73 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080074 }
75#endif /* CONFIG_P2P */
76
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 return wps;
78}
79#endif /* CONFIG_WPS */
80
81
Hai Shalomc3565922019-10-28 11:58:20 -070082static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
83 const u8 *mac_addr)
84{
85 u8 *tmp;
86
87 if (params->mac_addr) {
88 params->mac_addr_mask = NULL;
89 os_free(params->mac_addr);
90 params->mac_addr = NULL;
91 }
92
93 params->mac_addr_rand = 1;
94
95 if (!mac_addr)
96 return 0;
97
98 tmp = os_malloc(2 * ETH_ALEN);
99 if (!tmp)
100 return -1;
101
102 os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
103 params->mac_addr = tmp;
104 params->mac_addr_mask = tmp + ETH_ALEN;
105 return 0;
106}
107
108
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800109/**
110 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
111 * @wpa_s: Pointer to wpa_supplicant data
112 * Returns: 0 if no networks are enabled, >0 if networks are enabled
113 *
114 * This function is used to figure out whether any networks (or Interworking
115 * with enabled credentials and auto_interworking) are present in the current
116 * configuration.
117 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700118int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700121 int count = 0, disabled = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800122
123 if (wpa_s->p2p_mgmt)
124 return 0; /* no normal network profiles on p2p_mgmt interface */
125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700127 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700129 else
130 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 ssid = ssid->next;
132 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700133 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
134 wpa_s->conf->auto_interworking)
135 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700136 if (count == 0 && disabled > 0) {
137 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
138 "networks)", disabled);
139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 return count;
141}
142
143
144static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
145 struct wpa_ssid *ssid)
146{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700147 int min_temp_disabled = 0;
148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 while (ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700150 if (!wpas_network_disabled(wpa_s, ssid)) {
151 int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
152
153 if (temp_disabled <= 0)
154 break;
155
156 if (!min_temp_disabled ||
157 temp_disabled < min_temp_disabled)
158 min_temp_disabled = temp_disabled;
159 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 ssid = ssid->next;
161 }
162
163 /* ap_scan=2 mode - try to associate with each SSID. */
164 if (ssid == NULL) {
165 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
166 "end of scan list - go back to beginning");
167 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700168 wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 return;
170 }
171 if (ssid->next) {
172 /* Continue from the next SSID on the next attempt. */
173 wpa_s->prev_scan_ssid = ssid;
174 } else {
175 /* Start from the beginning of the SSID list. */
176 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
177 }
178 wpa_supplicant_associate(wpa_s, NULL, ssid);
179}
180
181
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800182static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800184 struct wpa_supplicant *wpa_s = work->wpa_s;
185 struct wpa_driver_scan_params *params = work->ctx;
186 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800188 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800189 if (!work->started) {
190 wpa_scan_free_params(params);
191 return;
192 }
193 wpa_supplicant_notify_scanning(wpa_s, 0);
194 wpas_notify_scan_done(wpa_s, 0);
195 wpa_s->scan_work = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 return;
197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198
Hai Shalomc3565922019-10-28 11:58:20 -0700199 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
200 wpa_s->wpa_state <= WPA_SCANNING)
201 wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
202
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700203 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
204 wpa_msg(wpa_s, MSG_INFO,
205 "Failed to assign random MAC address for a scan");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700206 wpa_scan_free_params(params);
207 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700208 radio_work_done(work);
209 return;
210 }
211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800212 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800214 if (wpa_s->clear_driver_scan_cache) {
215 wpa_printf(MSG_DEBUG,
216 "Request driver to clear scan cache due to local BSS flush");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800217 params->only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800218 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800219 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800220 /*
221 * Store the obtained vendor scan cookie (if any) in wpa_s context.
222 * The current design is to allow only one scan request on each
223 * interface, hence having this scan cookie stored in wpa_s context is
224 * fine for now.
225 *
226 * Revisit this logic if concurrent scan operations per interface
227 * is supported.
228 */
229 if (ret == 0)
230 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800231 wpa_scan_free_params(params);
232 work->ctx = NULL;
233 if (ret) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700234 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
235 !wpa_s->beacon_rep_data.token;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800236
237 if (wpa_s->disconnected)
238 retry = 0;
239
Hai Shalom899fcc72020-10-19 14:38:18 -0700240 /* do not retry if operation is not supported */
241 if (ret == -EOPNOTSUPP)
242 retry = 0;
243
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800244 wpa_supplicant_notify_scanning(wpa_s, 0);
245 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800246 if (wpa_s->wpa_state == WPA_SCANNING)
247 wpa_supplicant_set_state(wpa_s,
248 wpa_s->scan_prev_wpa_state);
249 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
250 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800252
253 if (retry) {
254 /* Restore scan_req since we will try to scan again */
255 wpa_s->scan_req = wpa_s->last_scan_req;
256 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700257 } else if (wpa_s->scan_res_handler) {
258 /* Clear the scan_res_handler */
259 wpa_s->scan_res_handler = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800260 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700261
262 if (wpa_s->beacon_rep_data.token)
263 wpas_rrm_refuse_request(wpa_s);
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800267
268 os_get_reltime(&wpa_s->scan_trigger_time);
269 wpa_s->scan_runs++;
270 wpa_s->normal_scans++;
271 wpa_s->own_scan_requested = 1;
272 wpa_s->clear_driver_scan_cache = 0;
273 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274}
275
276
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800277/**
278 * wpa_supplicant_trigger_scan - Request driver to start a scan
279 * @wpa_s: Pointer to wpa_supplicant data
280 * @params: Scan parameters
281 * Returns: 0 on success, -1 on failure
282 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
284 struct wpa_driver_scan_params *params)
285{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800286 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800288 if (wpa_s->scan_work) {
289 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
290 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 }
292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800293 ctx = wpa_scan_clone_params(params);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700294 if (!ctx ||
295 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800296 {
297 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700298 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800299 return -1;
300 }
301
302 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303}
304
305
306static void
307wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
308{
309 struct wpa_supplicant *wpa_s = eloop_ctx;
310
311 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
312
313 if (wpa_supplicant_req_sched_scan(wpa_s))
314 wpa_supplicant_req_scan(wpa_s, 0, 0);
315}
316
317
318static void
319wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
320{
321 struct wpa_supplicant *wpa_s = eloop_ctx;
322
323 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
324
325 wpa_s->sched_scan_timed_out = 1;
326 wpa_supplicant_cancel_sched_scan(wpa_s);
327}
328
329
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700330static int
331wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
332 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333{
334 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700335
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800336 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800337 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800338 if (ret)
339 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800340 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800341 wpa_s->sched_scanning = 1;
342
343 return ret;
344}
345
346
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700347static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800348{
349 int ret;
350
351 ret = wpa_drv_stop_sched_scan(wpa_s);
352 if (ret) {
353 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
354 /* TODO: what to do if stopping fails? */
355 return -1;
356 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
358 return ret;
359}
360
361
362static struct wpa_driver_scan_filter *
363wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
364{
365 struct wpa_driver_scan_filter *ssids;
366 struct wpa_ssid *ssid;
367 size_t count;
368
369 *num_ssids = 0;
370 if (!conf->filter_ssids)
371 return NULL;
372
373 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
374 if (ssid->ssid && ssid->ssid_len)
375 count++;
376 }
377 if (count == 0)
378 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 if (ssids == NULL)
381 return NULL;
382
383 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
384 if (!ssid->ssid || !ssid->ssid_len)
385 continue;
386 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
387 ssids[*num_ssids].ssid_len = ssid->ssid_len;
388 (*num_ssids)++;
389 }
390
391 return ssids;
392}
393
394
Hai Shaloma20dcd72022-02-04 13:43:00 -0800395#ifdef CONFIG_P2P
396static bool is_6ghz_supported(struct wpa_supplicant *wpa_s)
397{
398 struct hostapd_channel_data *chnl;
399 int i, j;
400
401 for (i = 0; i < wpa_s->hw.num_modes; i++) {
402 if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211A) {
403 chnl = wpa_s->hw.modes[i].channels;
404 for (j = 0; j < wpa_s->hw.modes[i].num_channels; j++) {
405 if (chnl[j].flag & HOSTAPD_CHAN_DISABLED)
406 continue;
407 if (is_6ghz_freq(chnl[j].freq))
408 return true;
409 }
410 }
411 }
412
413 return false;
414}
415#endif /* CONFIG_P2P */
416
417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418static void wpa_supplicant_optimize_freqs(
419 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
420{
421#ifdef CONFIG_P2P
422 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
423 wpa_s->go_params) {
424 /* Optimize provisioning state scan based on GO information */
425 if (wpa_s->p2p_in_provisioning < 5 &&
426 wpa_s->go_params->freq > 0) {
427 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
428 "preferred frequency %d MHz",
429 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800430 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800431 if (params->freqs)
432 params->freqs[0] = wpa_s->go_params->freq;
433 } else if (wpa_s->p2p_in_provisioning < 8 &&
434 wpa_s->go_params->freq_list[0]) {
435 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
436 "channels");
437 int_array_concat(&params->freqs,
438 wpa_s->go_params->freq_list);
439 if (params->freqs)
440 int_array_sort_unique(params->freqs);
441 }
442 wpa_s->p2p_in_provisioning++;
443 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700444
445 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
446 /*
447 * Optimize scan based on GO information during persistent
448 * group reinvocation
449 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700450 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700451 wpa_s->p2p_invite_go_freq > 0) {
452 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
453 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800454 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700455 if (params->freqs)
456 params->freqs[0] = wpa_s->p2p_invite_go_freq;
457 }
458 wpa_s->p2p_in_invitation++;
459 if (wpa_s->p2p_in_invitation > 20) {
460 /*
461 * This should not really happen since the variable is
462 * cleared on group removal, but if it does happen, make
463 * sure we do not get stuck in special invitation scan
464 * mode.
465 */
466 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
467 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +0000468 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt15907092014-03-25 10:42:57 -0700469 }
470 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800471#endif /* CONFIG_P2P */
472
473#ifdef CONFIG_WPS
474 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
475 /*
476 * Optimize post-provisioning scan based on channel used
477 * during provisioning.
478 */
479 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
480 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800481 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800482 if (params->freqs)
483 params->freqs[0] = wpa_s->wps_freq;
484 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800485 } else if (wpa_s->after_wps)
486 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800487
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800488 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
489 {
490 /* Optimize provisioning scan based on already known channel */
491 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
492 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800493 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800494 if (params->freqs)
495 params->freqs[0] = wpa_s->wps_freq;
496 wpa_s->known_wps_freq = 0; /* only do this once */
497 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800498#endif /* CONFIG_WPS */
499}
500
501
502#ifdef CONFIG_INTERWORKING
503static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
504 struct wpabuf *buf)
505{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800506 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
507 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
508 1 + ETH_ALEN);
509 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
510 /* No Venue Info */
511 if (!is_zero_ether_addr(wpa_s->conf->hessid))
512 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
513}
514#endif /* CONFIG_INTERWORKING */
515
516
Hai Shalomce48b4a2018-09-05 11:41:35 -0700517#ifdef CONFIG_MBO
518static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
519 struct wpabuf **ie)
520{
521 if (wpabuf_resize(ie, 5)) {
522 wpa_printf(MSG_DEBUG,
523 "Failed to allocate space for FILS Request Parameters element");
524 return;
525 }
526
527 /* FILS Request Parameters element */
528 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
529 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
530 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
531 /* Parameter control bitmap */
532 wpabuf_put_u8(*ie, 0);
533 /* Max Channel Time field - contains the value of MaxChannelTime
534 * parameter of the MLME-SCAN.request primitive represented in units of
535 * TUs, as an unsigned integer. A Max Channel Time field value of 255
536 * is used to indicate any duration of more than 254 TUs, or an
537 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
538 */
539 wpabuf_put_u8(*ie, 255);
540}
541#endif /* CONFIG_MBO */
542
543
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700544void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
545{
546 struct wpabuf *default_ies = NULL;
547 u8 ext_capab[18];
Hai Shalom60840252021-02-19 19:02:11 -0800548 int ext_capab_len, frame_id;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700549 enum wpa_driver_if_type type = WPA_IF_STATION;
550
551#ifdef CONFIG_P2P
552 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
553 type = WPA_IF_P2P_CLIENT;
554#endif /* CONFIG_P2P */
555
556 wpa_drv_get_ext_capa(wpa_s, type);
557
558 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
559 sizeof(ext_capab));
560 if (ext_capab_len > 0 &&
561 wpabuf_resize(&default_ies, ext_capab_len) == 0)
562 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
563
564#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -0700565 if (wpa_s->enable_oce & OCE_STA)
566 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700567 /* Send MBO and OCE capabilities */
568 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700569 wpas_mbo_scan_ie(wpa_s, default_ies);
570#endif /* CONFIG_MBO */
571
Hai Shalom60840252021-02-19 19:02:11 -0800572 if (type == WPA_IF_P2P_CLIENT)
573 frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
574 else
575 frame_id = VENDOR_ELEM_PROBE_REQ;
576
577 if (wpa_s->vendor_elem[frame_id]) {
578 size_t len;
579
580 len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
581 if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
582 wpabuf_put_buf(default_ies,
583 wpa_s->vendor_elem[frame_id]);
584 }
585
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700586 if (default_ies)
587 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
588 wpabuf_len(default_ies));
589 wpabuf_free(default_ies);
590}
591
592
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700593static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800594{
595 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700596 u8 ext_capab[18];
597 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800598#ifdef CONFIG_WPS
599 int wps = 0;
600 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
601#endif /* CONFIG_WPS */
602
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700603#ifdef CONFIG_P2P
604 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
605 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
606 else
607#endif /* CONFIG_P2P */
608 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
609
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700610 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
611 sizeof(ext_capab));
612 if (ext_capab_len > 0 &&
613 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
614 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
615
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616#ifdef CONFIG_INTERWORKING
617 if (wpa_s->conf->interworking &&
618 wpabuf_resize(&extra_ie, 100) == 0)
619 wpas_add_interworking_elements(wpa_s, extra_ie);
620#endif /* CONFIG_INTERWORKING */
621
Hai Shalomce48b4a2018-09-05 11:41:35 -0700622#ifdef CONFIG_MBO
623 if (wpa_s->enable_oce & OCE_STA)
624 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
625#endif /* CONFIG_MBO */
626
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800627#ifdef CONFIG_WPS
628 wps = wpas_wps_in_use(wpa_s, &req_type);
629
630 if (wps) {
631 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700632 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
633 DEV_PW_DEFAULT,
634 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800635 wpa_s->wps->uuid, req_type,
636 0, NULL);
637 if (wps_ie) {
638 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
639 wpabuf_put_buf(extra_ie, wps_ie);
640 wpabuf_free(wps_ie);
641 }
642 }
643
644#ifdef CONFIG_P2P
645 if (wps) {
646 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
647 if (wpabuf_resize(&extra_ie, ielen) == 0)
648 wpas_p2p_scan_ie(wpa_s, extra_ie);
649 }
650#endif /* CONFIG_P2P */
651
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800652 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
653
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800654#endif /* CONFIG_WPS */
655
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700656#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -0800657 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
658 wpas_hs20_add_indication(extra_ie, -1, 0);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700659#endif /* CONFIG_HS20 */
660
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800661#ifdef CONFIG_FST
662 if (wpa_s->fst_ies &&
663 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
664 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
665#endif /* CONFIG_FST */
666
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800667#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700668 /* Send MBO and OCE capabilities */
669 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800670 wpas_mbo_scan_ie(wpa_s, extra_ie);
671#endif /* CONFIG_MBO */
672
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700673 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
674 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
675
676 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
677 wpabuf_put_buf(extra_ie, buf);
678 }
679
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800680 return extra_ie;
681}
682
683
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800684#ifdef CONFIG_P2P
685
686/*
687 * Check whether there are any enabled networks or credentials that could be
688 * used for a non-P2P connection.
689 */
690static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
691{
692 struct wpa_ssid *ssid;
693
694 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
695 if (wpas_network_disabled(wpa_s, ssid))
696 continue;
697 if (!ssid->p2p_group)
698 return 1;
699 }
700
701 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
702 wpa_s->conf->auto_interworking)
703 return 1;
704
705 return 0;
706}
707
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700708#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800709
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800710
Hai Shalom60840252021-02-19 19:02:11 -0800711int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
712 enum hostapd_hw_mode band,
713 struct wpa_driver_scan_params *params, bool is_6ghz)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700714{
715 /* Include only supported channels for the specified band */
716 struct hostapd_hw_modes *mode;
Hai Shalom60840252021-02-19 19:02:11 -0800717 int num_chans = 0;
718 int *freqs, i;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700719
Hai Shalomfdcde762020-04-02 11:19:20 -0700720 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
Hai Shalom60840252021-02-19 19:02:11 -0800721 if (!mode)
722 return -1;
723
724 if (params->freqs) {
725 while (params->freqs[num_chans])
726 num_chans++;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700727 }
728
Hai Shalom60840252021-02-19 19:02:11 -0800729 freqs = os_realloc(params->freqs,
730 (num_chans + mode->num_channels + 1) * sizeof(int));
731 if (!freqs)
732 return -1;
733
734 params->freqs = freqs;
735 for (i = 0; i < mode->num_channels; i++) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700736 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
737 continue;
Hai Shalom60840252021-02-19 19:02:11 -0800738 params->freqs[num_chans++] = mode->channels[i].freq;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700739 }
Hai Shalom60840252021-02-19 19:02:11 -0800740 params->freqs[num_chans] = 0;
741
742 return 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700743}
744
745
746static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
747 struct wpa_driver_scan_params *params)
748{
749 if (wpa_s->hw.modes == NULL)
750 return; /* unknown what channels the driver supports */
751 if (params->freqs)
752 return; /* already using a limited channel set */
Hai Shalom60840252021-02-19 19:02:11 -0800753
754 if (wpa_s->setband_mask & WPA_SETBAND_5G)
755 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800756 false);
Hai Shalom60840252021-02-19 19:02:11 -0800757 if (wpa_s->setband_mask & WPA_SETBAND_2G)
758 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800759 false);
Hai Shalom60840252021-02-19 19:02:11 -0800760 if (wpa_s->setband_mask & WPA_SETBAND_6G)
761 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800762 true);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700763}
764
765
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800766static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
767 struct wpa_driver_scan_params *params,
768 size_t max_ssids, const u8 *ssid, size_t ssid_len)
769{
770 unsigned int j;
771
772 for (j = 0; j < params->num_ssids; j++) {
773 if (params->ssids[j].ssid_len == ssid_len &&
774 params->ssids[j].ssid &&
775 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
776 return; /* already in the list */
777 }
778
779 if (params->num_ssids + 1 > max_ssids) {
780 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
781 return;
782 }
783
784 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
785 wpa_ssid_txt(ssid, ssid_len));
786
787 params->ssids[params->num_ssids].ssid = ssid;
788 params->ssids[params->num_ssids].ssid_len = ssid_len;
789 params->num_ssids++;
790}
791
792
793static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
794 struct wpa_driver_scan_params *params,
795 struct wpa_ssid *ssid, size_t max_ssids)
796{
797#ifdef CONFIG_OWE
798 struct wpa_bss *bss;
799
800 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
801 return;
802
803 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
804 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
805
806 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
807 const u8 *owe, *pos, *end;
808 const u8 *owe_ssid;
809 size_t owe_ssid_len;
810
811 if (bss->ssid_len != ssid->ssid_len ||
812 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
813 continue;
814
815 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
816 if (!owe || owe[1] < 4)
817 continue;
818
819 pos = owe + 6;
820 end = owe + 2 + owe[1];
821
822 /* Must include BSSID and ssid_len */
823 if (end - pos < ETH_ALEN + 1)
824 return;
825
826 /* Skip BSSID */
827 pos += ETH_ALEN;
828 owe_ssid_len = *pos++;
829 owe_ssid = pos;
830
831 if ((size_t) (end - pos) < owe_ssid_len ||
832 owe_ssid_len > SSID_MAX_LEN)
833 return;
834
835 wpa_printf(MSG_DEBUG,
836 "OWE: scan_ssids: transition mode OWE ssid=%s",
837 wpa_ssid_txt(owe_ssid, owe_ssid_len));
838
839 wpa_add_scan_ssid(wpa_s, params, max_ssids,
840 owe_ssid, owe_ssid_len);
841 return;
842 }
843#endif /* CONFIG_OWE */
844}
845
846
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700847static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
848 struct wpa_driver_scan_params *params,
849 size_t max_ssids)
850{
851 unsigned int i;
852 struct wpa_ssid *ssid;
853
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700854 /*
855 * For devices with max_ssids greater than 1, leave the last slot empty
856 * for adding the wildcard scan entry.
857 */
858 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
859
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700860 for (i = 0; i < wpa_s->scan_id_count; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700861 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800862 if (!ssid)
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700863 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800864 if (ssid->scan_ssid)
865 wpa_add_scan_ssid(wpa_s, params, max_ssids,
866 ssid->ssid, ssid->ssid_len);
867 /*
868 * Also add the SSID of the OWE BSS, to allow discovery of
869 * transition mode APs more quickly.
870 */
871 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700872 }
873
874 wpa_s->scan_id_count = 0;
875}
876
877
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700878static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
879 struct wpa_driver_scan_params *params,
880 size_t max_ssids)
881{
882 unsigned int i;
883
884 if (wpa_s->ssids_from_scan_req == NULL ||
885 wpa_s->num_ssids_from_scan_req == 0)
886 return 0;
887
888 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
889 wpa_s->num_ssids_from_scan_req = max_ssids;
890 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
891 (unsigned int) max_ssids);
892 }
893
894 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
895 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
896 params->ssids[i].ssid_len =
897 wpa_s->ssids_from_scan_req[i].ssid_len;
898 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
899 params->ssids[i].ssid,
900 params->ssids[i].ssid_len);
901 }
902
903 params->num_ssids = wpa_s->num_ssids_from_scan_req;
904 wpa_s->num_ssids_from_scan_req = 0;
905 return 1;
906}
907
908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
910{
911 struct wpa_supplicant *wpa_s = eloop_ctx;
912 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800913 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700914 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700916 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800918 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919
Dmitry Shmidt29333592017-01-09 12:27:11 -0800920 wpa_s->ignore_post_flush_scan_res = 0;
921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
923 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
924 return;
925 }
926
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800927 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700928 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
930 return;
931 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800932
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700933 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800934 /*
935 * If we are already in scanning state, we shall reschedule the
936 * the incoming scan request.
937 */
938 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
939 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700940 return;
941 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800942
Dmitry Shmidt04949592012-07-19 12:16:46 -0700943 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800944 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
946 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
947 return;
948 }
949
950 if (wpa_s->conf->ap_scan != 0 &&
951 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
952 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
953 "overriding ap_scan configuration");
954 wpa_s->conf->ap_scan = 0;
955 wpas_notify_ap_scan_changed(wpa_s);
956 }
957
958 if (wpa_s->conf->ap_scan == 0) {
959 wpa_supplicant_gen_assoc_event(wpa_s);
960 return;
961 }
962
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800963 ssid = NULL;
964 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
965 wpa_s->connect_without_scan) {
966 connect_without_scan = 1;
967 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
968 if (ssid == wpa_s->connect_without_scan)
969 break;
970 }
971 }
972
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800973 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800974 if (p2p_in_prog && p2p_in_prog != 2 &&
975 (!ssid ||
976 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800977 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
978 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700979 return;
980 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700981
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800982 /*
983 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
984 * used for changing modes inside wpa_supplicant (roaming,
985 * auto-reconnect, etc). Discarding the scan might hurt these processes.
986 * The normal use case for PNO is to suspend the host immediately after
987 * starting PNO, so the periodic 100 ms attempts to run the scan do not
988 * normally happen in practice multiple times, i.e., this is simply
989 * restarting scanning once the host is woken up and PNO stopped.
990 */
991 if (wpa_s->pno || wpa_s->pno_sched_pending) {
992 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
993 wpa_supplicant_req_scan(wpa_s, 0, 100000);
994 return;
995 }
996
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800997 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 max_ssids = 1;
999 else {
1000 max_ssids = wpa_s->max_scan_ssids;
1001 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1002 max_ssids = WPAS_MAX_SCAN_SSIDS;
1003 }
1004
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001005 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001006 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001008 if (connect_without_scan) {
1009 wpa_s->connect_without_scan = NULL;
1010 if (ssid) {
1011 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1012 "without scan step");
1013 wpa_supplicant_associate(wpa_s, NULL, ssid);
1014 return;
1015 }
1016 }
1017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 os_memset(&params, 0, sizeof(params));
1019
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001020 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1022 wpa_s->wpa_state == WPA_INACTIVE)
1023 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1024
Dmitry Shmidt04949592012-07-19 12:16:46 -07001025 /*
1026 * If autoscan has set its own scanning parameters
1027 */
1028 if (wpa_s->autoscan_params != NULL) {
1029 scan_params = wpa_s->autoscan_params;
1030 goto scan;
1031 }
1032
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001033 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1034 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
1035 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1036 goto ssid_list_set;
1037 }
1038
Dmitry Shmidt04949592012-07-19 12:16:46 -07001039#ifdef CONFIG_P2P
1040 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001041 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001042 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1043 wpa_s->p2p_in_provisioning,
1044 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001045 params.ssids[0].ssid = wpa_s->go_params->ssid;
1046 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1047 params.num_ssids = 1;
1048 goto ssid_list_set;
1049 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07001050
1051 if (wpa_s->p2p_in_invitation) {
1052 if (wpa_s->current_ssid) {
1053 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1054 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1055 params.ssids[0].ssid_len =
1056 wpa_s->current_ssid->ssid_len;
1057 params.num_ssids = 1;
1058 } else {
1059 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1060 }
1061 goto ssid_list_set;
1062 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001063#endif /* CONFIG_P2P */
1064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 /* Find the starting point from which to continue scanning */
1066 ssid = wpa_s->conf->ssid;
1067 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1068 while (ssid) {
1069 if (ssid == wpa_s->prev_scan_ssid) {
1070 ssid = ssid->next;
1071 break;
1072 }
1073 ssid = ssid->next;
1074 }
1075 }
1076
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001077 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001078#ifdef CONFIG_AP
1079 !wpa_s->ap_iface &&
1080#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001081 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001082 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001083 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 wpa_supplicant_assoc_try(wpa_s, ssid);
1085 return;
1086 } else if (wpa_s->conf->ap_scan == 2) {
1087 /*
1088 * User-initiated scan request in ap_scan == 2; scan with
1089 * wildcard SSID.
1090 */
1091 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001092 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1093 /*
1094 * Perform single-channel single-SSID scan for
1095 * reassociate-to-same-BSS operation.
1096 */
1097 /* Setup SSID */
1098 ssid = wpa_s->current_ssid;
1099 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1100 ssid->ssid, ssid->ssid_len);
1101 params.ssids[0].ssid = ssid->ssid;
1102 params.ssids[0].ssid_len = ssid->ssid_len;
1103 params.num_ssids = 1;
1104
1105 /*
1106 * Allocate memory for frequency array, allocate one extra
1107 * slot for the zero-terminator.
1108 */
1109 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001110 if (params.freqs) {
1111 params.freqs[0] = wpa_s->assoc_freq;
1112 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001113 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07001114
1115 /*
1116 * Reset the reattach flag so that we fall back to full scan if
1117 * this scan fails.
1118 */
1119 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 } else {
1121 struct wpa_ssid *start = ssid, *tssid;
1122 int freqs_set = 0;
1123 if (ssid == NULL && max_ssids > 1)
1124 ssid = wpa_s->conf->ssid;
1125 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001126 if (!wpas_network_disabled(wpa_s, ssid) &&
1127 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1129 ssid->ssid, ssid->ssid_len);
1130 params.ssids[params.num_ssids].ssid =
1131 ssid->ssid;
1132 params.ssids[params.num_ssids].ssid_len =
1133 ssid->ssid_len;
1134 params.num_ssids++;
1135 if (params.num_ssids + 1 >= max_ssids)
1136 break;
1137 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001138
1139 if (!wpas_network_disabled(wpa_s, ssid)) {
1140 /*
1141 * Also add the SSID of the OWE BSS, to allow
1142 * discovery of transition mode APs more
1143 * quickly.
1144 */
1145 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1146 max_ssids);
1147 }
1148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 ssid = ssid->next;
1150 if (ssid == start)
1151 break;
1152 if (ssid == NULL && max_ssids > 1 &&
1153 start != wpa_s->conf->ssid)
1154 ssid = wpa_s->conf->ssid;
1155 }
1156
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001157 if (wpa_s->scan_id_count &&
1158 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1159 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1160
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001161 for (tssid = wpa_s->conf->ssid;
1162 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1163 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001164 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07001166 if (((params.freqs || !freqs_set) &&
1167 tssid->scan_freq) &&
1168 int_array_len(params.freqs) < 100) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 int_array_concat(&params.freqs,
1170 tssid->scan_freq);
1171 } else {
1172 os_free(params.freqs);
1173 params.freqs = NULL;
1174 }
1175 freqs_set = 1;
1176 }
1177 int_array_sort_unique(params.freqs);
1178 }
1179
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001180 if (ssid && max_ssids == 1) {
1181 /*
1182 * If the driver is limited to 1 SSID at a time interleave
1183 * wildcard SSID scans with specific SSID scans to avoid
1184 * waiting a long time for a wildcard scan.
1185 */
1186 if (!wpa_s->prev_scan_wildcard) {
1187 params.ssids[0].ssid = NULL;
1188 params.ssids[0].ssid_len = 0;
1189 wpa_s->prev_scan_wildcard = 1;
1190 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1191 "wildcard SSID (Interleave with specific)");
1192 } else {
1193 wpa_s->prev_scan_ssid = ssid;
1194 wpa_s->prev_scan_wildcard = 0;
1195 wpa_dbg(wpa_s, MSG_DEBUG,
1196 "Starting AP scan for specific SSID: %s",
1197 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001199 } else if (ssid) {
1200 /* max_ssids > 1 */
1201
1202 wpa_s->prev_scan_ssid = ssid;
1203 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1204 "the scan request");
1205 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001206 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1207 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1208 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001209 } else if (wpa_s->conf->passive_scan) {
1210 wpa_dbg(wpa_s, MSG_DEBUG,
1211 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 } else {
1213 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1214 params.num_ssids++;
1215 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1216 "SSID");
1217 }
1218
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001219ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001220 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001221 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001223 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001224 wpa_s->manual_scan_only_new) {
1225 wpa_printf(MSG_DEBUG,
1226 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001227 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001228 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001229
1230 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1231 wpa_s->manual_scan_freqs) {
1232 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1233 params.freqs = wpa_s->manual_scan_freqs;
1234 wpa_s->manual_scan_freqs = NULL;
1235 }
1236
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001237 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1238 wpa_dbg(wpa_s, MSG_DEBUG,
1239 "Limit select_network scan to specified channels");
1240 params.freqs = wpa_s->select_network_scan_freqs;
1241 wpa_s->select_network_scan_freqs = NULL;
1242 }
1243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1245 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1246 "generated frequency list");
1247 params.freqs = wpa_s->next_scan_freqs;
1248 } else
1249 os_free(wpa_s->next_scan_freqs);
1250 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001251 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001253 /* See if user specified frequencies. If so, scan only those. */
Hai Shalom60840252021-02-19 19:02:11 -08001254 if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1255 wpa_s->conf->initial_freq_list && !params.freqs) {
1256 wpa_dbg(wpa_s, MSG_DEBUG,
1257 "Optimize scan based on conf->initial_freq_list");
1258 int_array_concat(&params.freqs, wpa_s->conf->initial_freq_list);
1259 } else if (wpa_s->conf->freq_list && !params.freqs) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001260 wpa_dbg(wpa_s, MSG_DEBUG,
1261 "Optimize scan based on conf->freq_list");
1262 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1263 }
1264
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001265 /* Use current associated channel? */
1266 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001267 unsigned int num = wpa_s->num_multichan_concurrent;
1268
1269 params.freqs = os_calloc(num + 1, sizeof(int));
1270 if (params.freqs) {
1271 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1272 if (num > 0) {
1273 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1274 "current operating channels since "
1275 "scan_cur_freq is enabled");
1276 } else {
1277 os_free(params.freqs);
1278 params.freqs = NULL;
1279 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001280 }
1281 }
1282
Hai Shalomce48b4a2018-09-05 11:41:35 -07001283#ifdef CONFIG_MBO
1284 if (wpa_s->enable_oce & OCE_STA)
1285 params.oce_scan = 1;
1286#endif /* CONFIG_MBO */
1287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1289 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001290 if (extra_ie) {
1291 params.extra_ies = wpabuf_head(extra_ie);
1292 params.extra_ies_len = wpabuf_len(extra_ie);
1293 }
1294
1295#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001296 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001297 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001298 /*
1299 * The interface may not yet be in P2P mode, so we have to
1300 * explicitly request P2P probe to disable CCK rates.
1301 */
1302 params.p2p_probe = 1;
1303 }
1304#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001306 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001307 wpa_s->wpa_state <= WPA_SCANNING)
1308 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001309
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001310 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1311 struct wpa_bss *bss;
1312
1313 params.bssid = wpa_s->next_scan_bssid;
1314 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
Hai Shalomfdcde762020-04-02 11:19:20 -07001315 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1316 bss && bss->ssid_len && params.num_ssids == 1 &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001317 params.ssids[0].ssid_len == 0) {
1318 params.ssids[0].ssid = bss->ssid;
1319 params.ssids[0].ssid_len = bss->ssid_len;
1320 wpa_dbg(wpa_s, MSG_DEBUG,
1321 "Scan a previously specified BSSID " MACSTR
1322 " and SSID %s",
1323 MAC2STR(params.bssid),
1324 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1325 } else {
1326 wpa_dbg(wpa_s, MSG_DEBUG,
1327 "Scan a previously specified BSSID " MACSTR,
1328 MAC2STR(params.bssid));
1329 }
1330 }
1331
Sunil8cd6f4d2022-06-28 18:40:46 +00001332 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1333 wpa_s->manual_non_coloc_6ghz) {
1334 wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1335 params.non_coloc_6ghz = 1;
1336 }
1337
Dmitry Shmidt04949592012-07-19 12:16:46 -07001338 scan_params = &params;
1339
1340scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001341#ifdef CONFIG_P2P
1342 /*
1343 * If the driver does not support multi-channel concurrency and a
1344 * virtual interface that shares the same radio with the wpa_s interface
1345 * is operating there may not be need to scan other channels apart from
1346 * the current operating channel on the other virtual interface. Filter
1347 * out other channels in case we are trying to find a connection for a
1348 * station interface when we are not configured to prefer station
1349 * connection and a concurrent operation is already in process.
1350 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001351 if (wpa_s->scan_for_connection &&
1352 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001353 !scan_params->freqs && !params.freqs &&
1354 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001355 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1356 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001357 unsigned int num = wpa_s->num_multichan_concurrent;
1358
1359 params.freqs = os_calloc(num + 1, sizeof(int));
1360 if (params.freqs) {
1361 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1362 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1363 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1364 } else {
1365 os_free(params.freqs);
1366 params.freqs = NULL;
1367 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001368 }
1369 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001370
1371 if (!params.freqs &&
1372 (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning) &&
1373 !is_p2p_allow_6ghz(wpa_s->global->p2p) &&
1374 is_6ghz_supported(wpa_s)) {
Jimmy Chenafee24e2022-06-17 22:53:15 +08001375 /* Exclude 6 GHz channels from the full scan for P2P connection
Hai Shaloma20dcd72022-02-04 13:43:00 -08001376 * since the 6 GHz band is disabled for P2P uses. */
1377 wpa_printf(MSG_DEBUG,
1378 "P2P: 6 GHz disabled - update the scan frequency list");
Jimmy Chenafee24e2022-06-17 22:53:15 +08001379 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params, false);
1380 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params, false);
1381 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD, &params, false);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001382 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001383#endif /* CONFIG_P2P */
1384
Dmitry Shmidt04949592012-07-19 12:16:46 -07001385 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001387 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1388 !wpa_s->manual_scan_freqs) {
1389 /* Restore manual_scan_freqs for the next attempt */
1390 wpa_s->manual_scan_freqs = params.freqs;
1391 params.freqs = NULL;
1392 }
1393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001394 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 os_free(params.freqs);
1396 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001397 os_free(params.mac_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398
1399 if (ret) {
1400 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001401 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1402 wpa_supplicant_set_state(wpa_s,
1403 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001404 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001405 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001407 } else {
1408 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001409#ifdef CONFIG_INTERWORKING
1410 wpa_s->interworking_fast_assoc_tried = 0;
1411#endif /* CONFIG_INTERWORKING */
Hai Shalomfdcde762020-04-02 11:19:20 -07001412 wpa_s->next_scan_bssid_wildcard_ssid = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001413 if (params.bssid)
1414 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 }
1416}
1417
1418
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001419void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1420{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001421 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001422 int cancelled;
1423
1424 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1425 &remaining);
1426
1427 new_int.sec = sec;
1428 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001429 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001430 new_int.sec = remaining.sec;
1431 new_int.usec = remaining.usec;
1432 }
1433
Dmitry Shmidt051af732013-10-22 13:52:46 -07001434 if (cancelled) {
1435 eloop_register_timeout(new_int.sec, new_int.usec,
1436 wpa_supplicant_scan, wpa_s, NULL);
1437 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001438 wpa_s->scan_interval = sec;
1439}
1440
1441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442/**
1443 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1444 * @wpa_s: Pointer to wpa_supplicant data
1445 * @sec: Number of seconds after which to scan
1446 * @usec: Number of microseconds after which to scan
1447 *
1448 * This function is used to schedule a scan for neighboring access points after
1449 * the specified time.
1450 */
1451void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1452{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001453 int res;
1454
1455 if (wpa_s->p2p_mgmt) {
1456 wpa_dbg(wpa_s, MSG_DEBUG,
1457 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1458 sec, usec);
1459 return;
1460 }
1461
1462 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1463 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001464 if (res == 1) {
1465 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001466 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001467 } else if (res == 0) {
1468 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1469 sec, usec);
1470 } else {
1471 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1472 sec, usec);
1473 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475}
1476
1477
1478/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1480 * @wpa_s: Pointer to wpa_supplicant data
1481 * @sec: Number of seconds after which to scan
1482 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001483 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001484 *
1485 * This function is used to schedule periodic scans for neighboring
1486 * access points after the specified time.
1487 */
1488int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1489 int sec, int usec)
1490{
1491 if (!wpa_s->sched_scan_supported)
1492 return -1;
1493
1494 eloop_register_timeout(sec, usec,
1495 wpa_supplicant_delayed_sched_scan_timeout,
1496 wpa_s, NULL);
1497
1498 return 0;
1499}
1500
1501
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001502static void
1503wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1504 struct wpa_driver_scan_params *params)
1505{
1506 if (wpa_s->wpa_state != WPA_COMPLETED ||
1507 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1508 wpa_s->srp.relative_rssi_set == 0)
1509 return;
1510
1511 params->relative_rssi_set = 1;
1512 params->relative_rssi = wpa_s->srp.relative_rssi;
1513
1514 if (wpa_s->srp.relative_adjust_rssi == 0)
1515 return;
1516
1517 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1518 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1519}
1520
1521
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001522/**
1523 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1524 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001525 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001526 *
1527 * This function is used to schedule periodic scans for neighboring
1528 * access points repeating the scan continuously.
1529 */
1530int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1531{
1532 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001533 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001534 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001535 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001536 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001537 int ret;
1538 unsigned int max_sched_scan_ssids;
1539 int wildcard = 0;
1540 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001541 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001542
1543 if (!wpa_s->sched_scan_supported)
1544 return -1;
1545
1546 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1547 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1548 else
1549 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001550 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551 return -1;
1552
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001553 wpa_s->sched_scan_stop_req = 0;
1554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001555 if (wpa_s->sched_scanning) {
1556 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1557 return 0;
1558 }
1559
1560 need_ssids = 0;
1561 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001562 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001563 /* Use wildcard SSID to find this network */
1564 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001565 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1566 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001567 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001568
1569#ifdef CONFIG_WPS
1570 if (!wpas_network_disabled(wpa_s, ssid) &&
1571 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1572 /*
1573 * Normal scan is more reliable and faster for WPS
1574 * operations and since these are for short periods of
1575 * time, the benefit of trying to use sched_scan would
1576 * be limited.
1577 */
1578 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1579 "sched_scan for WPS");
1580 return -1;
1581 }
1582#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001583 }
1584 if (wildcard)
1585 need_ssids++;
1586
1587 if (wpa_s->normal_scans < 3 &&
1588 (need_ssids <= wpa_s->max_scan_ssids ||
1589 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1590 /*
1591 * When normal scan can speed up operations, use that for the
1592 * first operations before starting the sched_scan to allow
1593 * user space sleep more. We do this only if the normal scan
1594 * has functionality that is suitable for this or if the
1595 * sched_scan does not have better support for multiple SSIDs.
1596 */
1597 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1598 "sched_scan for initial scans (normal_scans=%d)",
1599 wpa_s->normal_scans);
1600 return -1;
1601 }
1602
1603 os_memset(&params, 0, sizeof(params));
1604
1605 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001606 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001607 sizeof(struct wpa_driver_scan_filter));
1608
1609 prev_state = wpa_s->wpa_state;
1610 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1611 wpa_s->wpa_state == WPA_INACTIVE)
1612 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1613
Dmitry Shmidt04949592012-07-19 12:16:46 -07001614 if (wpa_s->autoscan_params != NULL) {
1615 scan_params = wpa_s->autoscan_params;
1616 goto scan;
1617 }
1618
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001619 /* Find the starting point from which to continue scanning */
1620 ssid = wpa_s->conf->ssid;
1621 if (wpa_s->prev_sched_ssid) {
1622 while (ssid) {
1623 if (ssid == wpa_s->prev_sched_ssid) {
1624 ssid = ssid->next;
1625 break;
1626 }
1627 ssid = ssid->next;
1628 }
1629 }
1630
1631 if (!ssid || !wpa_s->prev_sched_ssid) {
1632 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001633 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1634 wpa_s->first_sched_scan = 1;
1635 ssid = wpa_s->conf->ssid;
1636 wpa_s->prev_sched_ssid = ssid;
1637 }
1638
1639 if (wildcard) {
1640 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1641 params.num_ssids++;
1642 }
1643
1644 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001645 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001646 goto next;
1647
1648 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1649 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1650 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1651 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1652 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1653 ssid->ssid, ssid->ssid_len);
1654 params.filter_ssids[params.num_filter_ssids].ssid_len =
1655 ssid->ssid_len;
1656 params.num_filter_ssids++;
1657 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1658 {
1659 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1660 "filter for sched_scan - drop filter");
1661 os_free(params.filter_ssids);
1662 params.filter_ssids = NULL;
1663 params.num_filter_ssids = 0;
1664 }
1665
1666 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1667 if (params.num_ssids == max_sched_scan_ssids)
1668 break; /* only room for broadcast SSID */
1669 wpa_dbg(wpa_s, MSG_DEBUG,
1670 "add to active scan ssid: %s",
1671 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1672 params.ssids[params.num_ssids].ssid =
1673 ssid->ssid;
1674 params.ssids[params.num_ssids].ssid_len =
1675 ssid->ssid_len;
1676 params.num_ssids++;
1677 if (params.num_ssids >= max_sched_scan_ssids) {
1678 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001679 do {
1680 ssid = ssid->next;
1681 } while (ssid &&
1682 (wpas_network_disabled(wpa_s, ssid) ||
1683 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001684 break;
1685 }
1686 }
1687
1688 next:
1689 wpa_s->prev_sched_ssid = ssid;
1690 ssid = ssid->next;
1691 }
1692
1693 if (params.num_filter_ssids == 0) {
1694 os_free(params.filter_ssids);
1695 params.filter_ssids = NULL;
1696 }
1697
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001698 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1699 if (extra_ie) {
1700 params.extra_ies = wpabuf_head(extra_ie);
1701 params.extra_ies_len = wpabuf_len(extra_ie);
1702 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001703
Dmitry Shmidt18463232014-01-24 12:29:41 -08001704 if (wpa_s->conf->filter_rssi)
1705 params.filter_rssi = wpa_s->conf->filter_rssi;
1706
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001707 /* See if user specified frequencies. If so, scan only those. */
1708 if (wpa_s->conf->freq_list && !params.freqs) {
1709 wpa_dbg(wpa_s, MSG_DEBUG,
1710 "Optimize scan based on conf->freq_list");
1711 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1712 }
1713
Hai Shalomce48b4a2018-09-05 11:41:35 -07001714#ifdef CONFIG_MBO
1715 if (wpa_s->enable_oce & OCE_STA)
1716 params.oce_scan = 1;
1717#endif /* CONFIG_MBO */
1718
Dmitry Shmidt04949592012-07-19 12:16:46 -07001719 scan_params = &params;
1720
1721scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001722 wpa_s->sched_scan_timed_out = 0;
1723
1724 /*
1725 * We cannot support multiple scan plans if the scan request includes
1726 * too many SSID's, so in this case use only the last scan plan and make
1727 * it run infinitely. It will be stopped by the timeout.
1728 */
1729 if (wpa_s->sched_scan_plans_num == 1 ||
1730 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1731 params.sched_scan_plans = wpa_s->sched_scan_plans;
1732 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1733 } else if (wpa_s->sched_scan_plans_num > 1) {
1734 wpa_dbg(wpa_s, MSG_DEBUG,
1735 "Too many SSIDs. Default to using single scheduled_scan plan");
1736 params.sched_scan_plans =
1737 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1738 1];
1739 params.sched_scan_plans_num = 1;
1740 } else {
1741 if (wpa_s->conf->sched_scan_interval)
1742 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1743 else
1744 scan_plan.interval = 10;
1745
1746 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1747 wpa_printf(MSG_WARNING,
1748 "Scan interval too long(%u), use the maximum allowed(%u)",
1749 scan_plan.interval,
1750 wpa_s->max_sched_scan_plan_interval);
1751 scan_plan.interval =
1752 wpa_s->max_sched_scan_plan_interval;
1753 }
1754
1755 scan_plan.iterations = 0;
1756 params.sched_scan_plans = &scan_plan;
1757 params.sched_scan_plans_num = 1;
1758 }
1759
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001760 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001762 if (ssid || !wpa_s->first_sched_scan) {
1763 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001764 "Starting sched scan after %u seconds: interval %u timeout %d",
1765 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001766 params.sched_scan_plans[0].interval,
1767 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001768 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001769 wpa_dbg(wpa_s, MSG_DEBUG,
1770 "Starting sched scan after %u seconds (no timeout)",
1771 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001772 }
1773
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001774 wpa_setband_scan_freqs(wpa_s, scan_params);
1775
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001776 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001777 wpa_s->wpa_state <= WPA_SCANNING)
1778 wpa_setup_mac_addr_rand_params(&params,
1779 wpa_s->mac_addr_sched_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001780
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001781 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1782
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001783 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001784 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001785 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001786 os_free(params.mac_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001787 if (ret) {
1788 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1789 if (prev_state != wpa_s->wpa_state)
1790 wpa_supplicant_set_state(wpa_s, prev_state);
1791 return ret;
1792 }
1793
1794 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1795 if (ssid || !wpa_s->first_sched_scan) {
1796 wpa_s->sched_scan_timed_out = 0;
1797 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1798 wpa_supplicant_sched_scan_timeout,
1799 wpa_s, NULL);
1800 wpa_s->first_sched_scan = 0;
1801 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001802 params.sched_scan_plans[0].interval *= 2;
1803 if ((unsigned int) wpa_s->sched_scan_timeout <
1804 params.sched_scan_plans[0].interval ||
1805 params.sched_scan_plans[0].interval >
1806 wpa_s->max_sched_scan_plan_interval) {
1807 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001808 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1809 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 }
1811
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001812 /* If there is no more ssids, start next time from the beginning */
1813 if (!ssid)
1814 wpa_s->prev_sched_ssid = NULL;
1815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001816 return 0;
1817}
1818
1819
1820/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1822 * @wpa_s: Pointer to wpa_supplicant data
1823 *
1824 * This function is used to cancel a scan request scheduled with
1825 * wpa_supplicant_req_scan().
1826 */
1827void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1828{
1829 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1830 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831}
1832
1833
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001834/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001835 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1836 * @wpa_s: Pointer to wpa_supplicant data
1837 *
1838 * This function is used to stop a delayed scheduled scan.
1839 */
1840void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1841{
1842 if (!wpa_s->sched_scan_supported)
1843 return;
1844
1845 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1846 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1847 wpa_s, NULL);
1848}
1849
1850
1851/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1853 * @wpa_s: Pointer to wpa_supplicant data
1854 *
1855 * This function is used to stop a periodic scheduled scan.
1856 */
1857void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1858{
1859 if (!wpa_s->sched_scanning)
1860 return;
1861
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001862 if (wpa_s->sched_scanning)
1863 wpa_s->sched_scan_stop_req = 1;
1864
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001865 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1866 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1867 wpa_supplicant_stop_sched_scan(wpa_s);
1868}
1869
1870
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001871/**
1872 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1873 * @wpa_s: Pointer to wpa_supplicant data
1874 * @scanning: Whether scanning is currently in progress
1875 *
1876 * This function is to generate scanning notifycations. It is called whenever
1877 * there may have been a change in scanning (scan started, completed, stopped).
1878 * wpas_notify_scanning() is called whenever the scanning state changed from the
1879 * previously notified state.
1880 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1882 int scanning)
1883{
1884 if (wpa_s->scanning != scanning) {
1885 wpa_s->scanning = scanning;
1886 wpas_notify_scanning(wpa_s);
1887 }
1888}
1889
1890
1891static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1892{
1893 int rate = 0;
1894 const u8 *ie;
1895 int i;
1896
1897 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1898 for (i = 0; ie && i < ie[1]; i++) {
1899 if ((ie[i + 2] & 0x7f) > rate)
1900 rate = ie[i + 2] & 0x7f;
1901 }
1902
1903 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1904 for (i = 0; ie && i < ie[1]; i++) {
1905 if ((ie[i + 2] & 0x7f) > rate)
1906 rate = ie[i + 2] & 0x7f;
1907 }
1908
1909 return rate;
1910}
1911
1912
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001913/**
1914 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1915 * @res: Scan result entry
1916 * @ie: Information element identitifier (WLAN_EID_*)
1917 * Returns: Pointer to the information element (id field) or %NULL if not found
1918 *
1919 * This function returns the first matching information element in the scan
1920 * result.
1921 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1923{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001924 size_t ie_len = res->ie_len;
1925
1926 /* Use the Beacon frame IEs if res->ie_len is not available */
1927 if (!ie_len)
1928 ie_len = res->beacon_ie_len;
1929
1930 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931}
1932
1933
Sunil Ravi89eba102022-09-13 21:04:37 -07001934const u8 * wpa_scan_get_ml_ie(const struct wpa_scan_res *res, u8 type)
1935{
1936 size_t ie_len = res->ie_len;
1937
1938 /* Use the Beacon frame IEs if res->ie_len is not available */
1939 if (!ie_len)
1940 ie_len = res->beacon_ie_len;
1941
1942 return get_ml_ie((const u8 *) (res + 1), ie_len, type);
1943}
1944
1945
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001946/**
1947 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1948 * @res: Scan result entry
1949 * @vendor_type: Vendor type (four octets starting the IE payload)
1950 * Returns: Pointer to the information element (id field) or %NULL if not found
1951 *
1952 * This function returns the first matching information element in the scan
1953 * result.
1954 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1956 u32 vendor_type)
1957{
Hai Shalom60840252021-02-19 19:02:11 -08001958 const u8 *ies;
1959 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960
Hai Shalom60840252021-02-19 19:02:11 -08001961 ies = (const u8 *) (res + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962
Hai Shalom60840252021-02-19 19:02:11 -08001963 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
1964 if (elem->datalen >= 4 &&
1965 vendor_type == WPA_GET_BE32(elem->data))
1966 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967 }
1968
1969 return NULL;
1970}
1971
1972
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001973/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001974 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1975 * @res: Scan result entry
1976 * @vendor_type: Vendor type (four octets starting the IE payload)
1977 * Returns: Pointer to the information element (id field) or %NULL if not found
1978 *
1979 * This function returns the first matching information element in the scan
1980 * result.
1981 *
1982 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1983 * from Beacon frames instead of either Beacon or Probe Response frames.
1984 */
1985const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1986 u32 vendor_type)
1987{
Hai Shalom60840252021-02-19 19:02:11 -08001988 const u8 *ies;
1989 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001990
1991 if (res->beacon_ie_len == 0)
1992 return NULL;
1993
Hai Shalom60840252021-02-19 19:02:11 -08001994 ies = (const u8 *) (res + 1);
1995 ies += res->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001996
Hai Shalom60840252021-02-19 19:02:11 -08001997 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1998 res->beacon_ie_len) {
1999 if (elem->datalen >= 4 &&
2000 vendor_type == WPA_GET_BE32(elem->data))
2001 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002002 }
2003
2004 return NULL;
2005}
2006
2007
2008/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002009 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2010 * @res: Scan result entry
2011 * @vendor_type: Vendor type (four octets starting the IE payload)
2012 * Returns: Pointer to the information element payload or %NULL if not found
2013 *
2014 * This function returns concatenated payload of possibly fragmented vendor
2015 * specific information elements in the scan result. The caller is responsible
2016 * for freeing the returned buffer.
2017 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2019 u32 vendor_type)
2020{
2021 struct wpabuf *buf;
2022 const u8 *end, *pos;
2023
2024 buf = wpabuf_alloc(res->ie_len);
2025 if (buf == NULL)
2026 return NULL;
2027
2028 pos = (const u8 *) (res + 1);
2029 end = pos + res->ie_len;
2030
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002031 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08002032 u8 ie, len;
2033
2034 ie = pos[0];
2035 len = pos[1];
2036 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 break;
Hai Shalom60840252021-02-19 19:02:11 -08002038 pos += 2;
2039 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2040 vendor_type == WPA_GET_BE32(pos))
2041 wpabuf_put_data(buf, pos + 4, len - 4);
2042 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 }
2044
2045 if (wpabuf_len(buf) == 0) {
2046 wpabuf_free(buf);
2047 buf = NULL;
2048 }
2049
2050 return buf;
2051}
2052
2053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054/* Compare function for sorting scan results. Return >0 if @b is considered
2055 * better. */
2056static int wpa_scan_result_compar(const void *a, const void *b)
2057{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002058#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 struct wpa_scan_res **_wa = (void *) a;
2060 struct wpa_scan_res **_wb = (void *) b;
2061 struct wpa_scan_res *wa = *_wa;
2062 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002063 int wpa_a, wpa_b;
2064 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065
2066 /* WPA/WPA2 support preferred */
2067 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2068 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2069 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2070 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2071
2072 if (wpa_b && !wpa_a)
2073 return 1;
2074 if (!wpa_b && wpa_a)
2075 return -1;
2076
2077 /* privacy support preferred */
2078 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2079 (wb->caps & IEEE80211_CAP_PRIVACY))
2080 return 1;
2081 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2082 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2083 return -1;
2084
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002085 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002086 snr_a_full = wa->snr;
2087 snr_a = MIN(wa->snr, GREAT_SNR);
2088 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002089 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002090 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002091 /* Level is not in dBm, so we can't calculate
2092 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002093 snr_a = snr_a_full = wa->level;
2094 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002095 }
2096
Hai Shaloma20dcd72022-02-04 13:43:00 -08002097 /* If SNR is close, decide by max rate or frequency band. For cases
2098 * involving the 6 GHz band, use the throughput estimate irrespective
2099 * of the SNR difference since the LPI/VLP rules may result in
2100 * significant differences in SNR for cases where the estimated
2101 * throughput can be considerably higher with the lower SNR. */
2102 if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2103 is_6ghz_freq(wa->freq) ||
2104 is_6ghz_freq(wb->freq))) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002105 if (wa->est_throughput != wb->est_throughput)
Hai Shalom021b0b52019-04-10 11:17:58 -07002106 return (int) wb->est_throughput -
2107 (int) wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002108 }
2109 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2110 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002111 if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2112 return is_6ghz_freq(wa->freq) ? -1 : 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002113 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2114 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 }
2116
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002117 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 * identical, use quality values since some drivers may only report
2119 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002120 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002122 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002123#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124}
2125
2126
2127#ifdef CONFIG_WPS
2128/* Compare function for sorting scan results when searching a WPS AP for
2129 * provisioning. Return >0 if @b is considered better. */
2130static int wpa_scan_result_wps_compar(const void *a, const void *b)
2131{
2132 struct wpa_scan_res **_wa = (void *) a;
2133 struct wpa_scan_res **_wb = (void *) b;
2134 struct wpa_scan_res *wa = *_wa;
2135 struct wpa_scan_res *wb = *_wb;
2136 int uses_wps_a, uses_wps_b;
2137 struct wpabuf *wps_a, *wps_b;
2138 int res;
2139
2140 /* Optimization - check WPS IE existence before allocated memory and
2141 * doing full reassembly. */
2142 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2143 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2144 if (uses_wps_a && !uses_wps_b)
2145 return -1;
2146 if (!uses_wps_a && uses_wps_b)
2147 return 1;
2148
2149 if (uses_wps_a && uses_wps_b) {
2150 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2151 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2152 res = wps_ap_priority_compar(wps_a, wps_b);
2153 wpabuf_free(wps_a);
2154 wpabuf_free(wps_b);
2155 if (res)
2156 return res;
2157 }
2158
2159 /*
2160 * Do not use current AP security policy as a sorting criteria during
2161 * WPS provisioning step since the AP may get reconfigured at the
2162 * completion of provisioning.
2163 */
2164
2165 /* all things being equal, use signal level; if signal levels are
2166 * identical, use quality values since some drivers may only report
2167 * that value and leave the signal level zero */
2168 if (wb->level == wa->level)
2169 return wb->qual - wa->qual;
2170 return wb->level - wa->level;
2171}
2172#endif /* CONFIG_WPS */
2173
2174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002175static void dump_scan_res(struct wpa_scan_results *scan_res)
2176{
2177#ifndef CONFIG_NO_STDOUT_DEBUG
2178 size_t i;
2179
2180 if (scan_res->res == NULL || scan_res->num == 0)
2181 return;
2182
2183 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2184
2185 for (i = 0; i < scan_res->num; i++) {
2186 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002187 u8 *pos;
Sunil Ravia04bd252022-05-02 22:54:18 -07002188 const u8 *ssid_ie, *ssid = NULL;
2189 size_t ssid_len = 0;
2190
2191 ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2192 if (ssid_ie) {
2193 ssid = ssid_ie + 2;
2194 ssid_len = ssid_ie[1];
2195 }
2196
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002197 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002198 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2199
Sunil Ravia04bd252022-05-02 22:54:18 -07002200 wpa_printf(MSG_EXCESSIVE, MACSTR
2201 " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2202 MAC2STR(r->bssid),
2203 wpa_ssid_txt(ssid, ssid_len),
2204 r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002205 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002206 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2207 r->flags,
2208 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002209 } else {
Sunil Ravia04bd252022-05-02 22:54:18 -07002210 wpa_printf(MSG_EXCESSIVE, MACSTR
2211 " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2212 MAC2STR(r->bssid),
2213 wpa_ssid_txt(ssid, ssid_len),
2214 r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002215 r->noise, r->level, r->flags, r->age,
2216 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002217 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002218 pos = (u8 *) (r + 1);
2219 if (r->ie_len)
2220 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2221 pos += r->ie_len;
2222 if (r->beacon_ie_len)
2223 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2224 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002225 }
2226#endif /* CONFIG_NO_STDOUT_DEBUG */
2227}
2228
2229
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002230/**
2231 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2232 * @wpa_s: Pointer to wpa_supplicant data
2233 * @bssid: BSSID to check
2234 * Returns: 0 if the BSSID is filtered or 1 if not
2235 *
2236 * This function is used to filter out specific BSSIDs from scan reslts mainly
2237 * for testing purposes (SET bssid_filter ctrl_iface command).
2238 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002239int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2240 const u8 *bssid)
2241{
2242 size_t i;
2243
2244 if (wpa_s->bssid_filter == NULL)
2245 return 1;
2246
2247 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2248 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2249 ETH_ALEN) == 0)
2250 return 1;
2251 }
2252
2253 return 0;
2254}
2255
2256
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002257void filter_scan_res(struct wpa_supplicant *wpa_s,
2258 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002259{
2260 size_t i, j;
2261
2262 if (wpa_s->bssid_filter == NULL)
2263 return;
2264
2265 for (i = 0, j = 0; i < res->num; i++) {
2266 if (wpa_supplicant_filter_bssid_match(wpa_s,
2267 res->res[i]->bssid)) {
2268 res->res[j++] = res->res[i];
2269 } else {
2270 os_free(res->res[i]);
2271 res->res[i] = NULL;
2272 }
2273 }
2274
2275 if (res->num != j) {
2276 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2277 (int) (res->num - j));
2278 res->num = j;
2279 }
2280}
2281
2282
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002283void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002284{
2285 if (res->flags & WPA_SCAN_NOISE_INVALID) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002286 res->noise = is_6ghz_freq(res->freq) ?
2287 DEFAULT_NOISE_FLOOR_6GHZ :
2288 (IS_5GHZ(res->freq) ?
2289 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002290 }
2291
2292 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2293 res->snr = res->level - res->noise;
2294 } else {
2295 /* Level is not in dBm, so we can't calculate
2296 * SNR. Just use raw level (units unknown). */
2297 res->snr = res->level;
2298 }
2299}
2300
2301
Hai Shalom899fcc72020-10-19 14:38:18 -07002302/* Minimum SNR required to achieve a certain bitrate. */
2303struct minsnr_bitrate_entry {
2304 int minsnr;
2305 unsigned int bitrate; /* in Mbps */
2306};
2307
2308/* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2309static const int vht_mcs = 8;
2310
2311static const struct minsnr_bitrate_entry vht20_table[] = {
2312 { 0, 0 },
2313 { 2, 6500 }, /* HT20 MCS0 */
2314 { 5, 13000 }, /* HT20 MCS1 */
2315 { 9, 19500 }, /* HT20 MCS2 */
2316 { 11, 26000 }, /* HT20 MCS3 */
2317 { 15, 39000 }, /* HT20 MCS4 */
2318 { 18, 52000 }, /* HT20 MCS5 */
2319 { 20, 58500 }, /* HT20 MCS6 */
2320 { 25, 65000 }, /* HT20 MCS7 */
2321 { 29, 78000 }, /* VHT20 MCS8 */
2322 { -1, 78000 } /* SNR > 29 */
2323};
2324
2325static const struct minsnr_bitrate_entry vht40_table[] = {
2326 { 0, 0 },
2327 { 5, 13500 }, /* HT40 MCS0 */
2328 { 8, 27000 }, /* HT40 MCS1 */
2329 { 12, 40500 }, /* HT40 MCS2 */
2330 { 14, 54000 }, /* HT40 MCS3 */
2331 { 18, 81000 }, /* HT40 MCS4 */
2332 { 21, 108000 }, /* HT40 MCS5 */
2333 { 23, 121500 }, /* HT40 MCS6 */
2334 { 28, 135000 }, /* HT40 MCS7 */
2335 { 32, 162000 }, /* VHT40 MCS8 */
2336 { 34, 180000 }, /* VHT40 MCS9 */
2337 { -1, 180000 } /* SNR > 34 */
2338};
2339
2340static const struct minsnr_bitrate_entry vht80_table[] = {
2341 { 0, 0 },
2342 { 8, 29300 }, /* VHT80 MCS0 */
2343 { 11, 58500 }, /* VHT80 MCS1 */
2344 { 15, 87800 }, /* VHT80 MCS2 */
2345 { 17, 117000 }, /* VHT80 MCS3 */
2346 { 21, 175500 }, /* VHT80 MCS4 */
2347 { 24, 234000 }, /* VHT80 MCS5 */
2348 { 26, 263300 }, /* VHT80 MCS6 */
2349 { 31, 292500 }, /* VHT80 MCS7 */
2350 { 35, 351000 }, /* VHT80 MCS8 */
2351 { 37, 390000 }, /* VHT80 MCS9 */
2352 { -1, 390000 } /* SNR > 37 */
2353};
2354
2355
Hai Shaloma20dcd72022-02-04 13:43:00 -08002356static const struct minsnr_bitrate_entry vht160_table[] = {
2357 { 0, 0 },
2358 { 11, 58500 }, /* VHT160 MCS0 */
2359 { 14, 117000 }, /* VHT160 MCS1 */
2360 { 18, 175500 }, /* VHT160 MCS2 */
2361 { 20, 234000 }, /* VHT160 MCS3 */
2362 { 24, 351000 }, /* VHT160 MCS4 */
2363 { 27, 468000 }, /* VHT160 MCS5 */
2364 { 29, 526500 }, /* VHT160 MCS6 */
2365 { 34, 585000 }, /* VHT160 MCS7 */
2366 { 38, 702000 }, /* VHT160 MCS8 */
2367 { 40, 780000 }, /* VHT160 MCS9 */
2368 { -1, 780000 } /* SNR > 37 */
2369};
2370
2371
2372static const struct minsnr_bitrate_entry he20_table[] = {
2373 { 0, 0 },
2374 { 2, 8600 }, /* HE20 MCS0 */
2375 { 5, 17200 }, /* HE20 MCS1 */
2376 { 9, 25800 }, /* HE20 MCS2 */
2377 { 11, 34400 }, /* HE20 MCS3 */
2378 { 15, 51600 }, /* HE20 MCS4 */
2379 { 18, 68800 }, /* HE20 MCS5 */
2380 { 20, 77400 }, /* HE20 MCS6 */
2381 { 25, 86000 }, /* HE20 MCS7 */
2382 { 29, 103200 }, /* HE20 MCS8 */
2383 { 31, 114700 }, /* HE20 MCS9 */
2384 { 34, 129000 }, /* HE20 MCS10 */
2385 { 36, 143400 }, /* HE20 MCS11 */
2386 { -1, 143400 } /* SNR > 29 */
2387};
2388
2389static const struct minsnr_bitrate_entry he40_table[] = {
2390 { 0, 0 },
2391 { 5, 17200 }, /* HE40 MCS0 */
2392 { 8, 34400 }, /* HE40 MCS1 */
2393 { 12, 51600 }, /* HE40 MCS2 */
2394 { 14, 68800 }, /* HE40 MCS3 */
2395 { 18, 103200 }, /* HE40 MCS4 */
2396 { 21, 137600 }, /* HE40 MCS5 */
2397 { 23, 154900 }, /* HE40 MCS6 */
2398 { 28, 172100 }, /* HE40 MCS7 */
2399 { 32, 206500 }, /* HE40 MCS8 */
2400 { 34, 229400 }, /* HE40 MCS9 */
2401 { 37, 258100 }, /* HE40 MCS10 */
2402 { 39, 286800 }, /* HE40 MCS11 */
2403 { -1, 286800 } /* SNR > 34 */
2404};
2405
2406static const struct minsnr_bitrate_entry he80_table[] = {
2407 { 0, 0 },
2408 { 8, 36000 }, /* HE80 MCS0 */
2409 { 11, 72100 }, /* HE80 MCS1 */
2410 { 15, 108100 }, /* HE80 MCS2 */
2411 { 17, 144100 }, /* HE80 MCS3 */
2412 { 21, 216200 }, /* HE80 MCS4 */
2413 { 24, 288200 }, /* HE80 MCS5 */
2414 { 26, 324300 }, /* HE80 MCS6 */
2415 { 31, 360300 }, /* HE80 MCS7 */
2416 { 35, 432400 }, /* HE80 MCS8 */
2417 { 37, 480400 }, /* HE80 MCS9 */
2418 { 40, 540400 }, /* HE80 MCS10 */
2419 { 42, 600500 }, /* HE80 MCS11 */
2420 { -1, 600500 } /* SNR > 37 */
2421};
2422
2423
2424static const struct minsnr_bitrate_entry he160_table[] = {
2425 { 0, 0 },
2426 { 11, 72100 }, /* HE160 MCS0 */
2427 { 14, 144100 }, /* HE160 MCS1 */
2428 { 18, 216200 }, /* HE160 MCS2 */
2429 { 20, 288200 }, /* HE160 MCS3 */
2430 { 24, 432400 }, /* HE160 MCS4 */
2431 { 27, 576500 }, /* HE160 MCS5 */
2432 { 29, 648500 }, /* HE160 MCS6 */
2433 { 34, 720600 }, /* HE160 MCS7 */
2434 { 38, 864700 }, /* HE160 MCS8 */
2435 { 40, 960800 }, /* HE160 MCS9 */
2436 { 43, 1080900 }, /* HE160 MCS10 */
2437 { 45, 1201000 }, /* HE160 MCS11 */
2438 { -1, 1201000 } /* SNR > 37 */
2439};
2440
2441
Hai Shalomfdcde762020-04-02 11:19:20 -07002442static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2443 int rate0, int rate1)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002444{
Hai Shalomfdcde762020-04-02 11:19:20 -07002445 return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002446}
2447
2448
Hai Shalom899fcc72020-10-19 14:38:18 -07002449static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2450 int snr, bool vht)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002451{
Hai Shalom899fcc72020-10-19 14:38:18 -07002452 const struct minsnr_bitrate_entry *prev, *entry = table;
2453
2454 while ((entry->minsnr != -1) &&
2455 (snr >= entry->minsnr) &&
2456 (vht || entry - table <= vht_mcs))
2457 entry++;
2458 if (entry == table)
2459 return entry->bitrate;
2460 prev = entry - 1;
2461 if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2462 return prev->bitrate;
2463 return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2464 entry->bitrate);
Hai Shalomfdcde762020-04-02 11:19:20 -07002465}
2466
2467
Hai Shalom899fcc72020-10-19 14:38:18 -07002468static unsigned int max_ht20_rate(int snr, bool vht)
Hai Shalomfdcde762020-04-02 11:19:20 -07002469{
Hai Shalom899fcc72020-10-19 14:38:18 -07002470 return max_rate(vht20_table, snr, vht);
2471}
2472
2473
2474static unsigned int max_ht40_rate(int snr, bool vht)
2475{
2476 return max_rate(vht40_table, snr, vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002477}
2478
2479
2480static unsigned int max_vht80_rate(int snr)
2481{
Hai Shalom899fcc72020-10-19 14:38:18 -07002482 return max_rate(vht80_table, snr, 1);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002483}
2484
Hai Shalomfdcde762020-04-02 11:19:20 -07002485
Hai Shaloma20dcd72022-02-04 13:43:00 -08002486static unsigned int max_vht160_rate(int snr)
2487{
2488 return max_rate(vht160_table, snr, 1);
2489}
2490
2491
2492static unsigned int max_he_rate(const struct minsnr_bitrate_entry table[],
2493 int snr)
2494{
2495 const struct minsnr_bitrate_entry *prev, *entry = table;
2496
2497 while (entry->minsnr != -1 && snr >= entry->minsnr)
2498 entry++;
2499 if (entry == table)
2500 return 0;
2501 prev = entry - 1;
2502 if (entry->minsnr == -1)
2503 return prev->bitrate;
2504 return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2505 prev->bitrate, entry->bitrate);
2506}
2507
2508
Hai Shalomfdcde762020-04-02 11:19:20 -07002509unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2510 const u8 *ies, size_t ies_len, int rate,
Hai Shaloma20dcd72022-02-04 13:43:00 -08002511 int snr, int freq)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002512{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002513 struct hostapd_hw_modes *hw_mode;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002514 unsigned int est, tmp;
Hai Shalomfdcde762020-04-02 11:19:20 -07002515 const u8 *ie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002516
2517 /* Limit based on estimated SNR */
2518 if (rate > 1 * 2 && snr < 1)
2519 rate = 1 * 2;
2520 else if (rate > 2 * 2 && snr < 4)
2521 rate = 2 * 2;
2522 else if (rate > 6 * 2 && snr < 5)
2523 rate = 6 * 2;
2524 else if (rate > 9 * 2 && snr < 6)
2525 rate = 9 * 2;
2526 else if (rate > 12 * 2 && snr < 7)
2527 rate = 12 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002528 else if (rate > 12 * 2 && snr < 8)
2529 rate = 14 * 2;
2530 else if (rate > 12 * 2 && snr < 9)
2531 rate = 16 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002532 else if (rate > 18 * 2 && snr < 10)
2533 rate = 18 * 2;
2534 else if (rate > 24 * 2 && snr < 11)
2535 rate = 24 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002536 else if (rate > 24 * 2 && snr < 12)
2537 rate = 27 * 2;
2538 else if (rate > 24 * 2 && snr < 13)
2539 rate = 30 * 2;
2540 else if (rate > 24 * 2 && snr < 14)
2541 rate = 33 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002542 else if (rate > 36 * 2 && snr < 15)
2543 rate = 36 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002544 else if (rate > 36 * 2 && snr < 16)
2545 rate = 39 * 2;
2546 else if (rate > 36 * 2 && snr < 17)
2547 rate = 42 * 2;
2548 else if (rate > 36 * 2 && snr < 18)
2549 rate = 45 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002550 else if (rate > 48 * 2 && snr < 19)
2551 rate = 48 * 2;
Hai Shalomfdcde762020-04-02 11:19:20 -07002552 else if (rate > 48 * 2 && snr < 20)
2553 rate = 51 * 2;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002554 else if (rate > 54 * 2 && snr < 21)
2555 rate = 54 * 2;
2556 est = rate * 500;
2557
Hai Shaloma20dcd72022-02-04 13:43:00 -08002558 hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2559 freq);
2560
2561 if (hw_mode && hw_mode->ht_capab) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002562 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002563 if (ie) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002564 tmp = max_ht20_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002565 if (tmp > est)
2566 est = tmp;
2567 }
2568 }
2569
Hai Shaloma20dcd72022-02-04 13:43:00 -08002570 if (hw_mode &&
2571 (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002572 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002573 if (ie && ie[1] >= 2 &&
2574 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002575 tmp = max_ht40_rate(snr, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002576 if (tmp > est)
2577 est = tmp;
2578 }
2579 }
2580
Hai Shaloma20dcd72022-02-04 13:43:00 -08002581 if (hw_mode && hw_mode->vht_capab) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002582 /* Use +1 to assume VHT is always faster than HT */
Hai Shalomfdcde762020-04-02 11:19:20 -07002583 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002584 if (ie) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08002585 bool vht80 = false, vht160 = false;
2586
Hai Shalom899fcc72020-10-19 14:38:18 -07002587 tmp = max_ht20_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002588 if (tmp > est)
2589 est = tmp;
2590
Hai Shalomfdcde762020-04-02 11:19:20 -07002591 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002592 if (ie && ie[1] >= 2 &&
2593 (ie[3] &
2594 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002595 tmp = max_ht40_rate(snr, true) + 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002596 if (tmp > est)
2597 est = tmp;
2598 }
2599
Hai Shaloma20dcd72022-02-04 13:43:00 -08002600 /* Determine VHT BSS bandwidth based on IEEE Std
2601 * 802.11-2020, Table 11-23 (VHT BSs bandwidth) */
Hai Shalomfdcde762020-04-02 11:19:20 -07002602 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002603 if (ie && ie[1] >= 3) {
2604 u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2605 u8 seg0 = ie[3];
2606 u8 seg1 = ie[4];
2607
2608 if (cw)
2609 vht80 = true;
2610 if (cw == 2 ||
2611 (cw == 3 &&
2612 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2613 vht160 = true;
2614 if (cw == 1 &&
2615 ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2616 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2617 vht160 = true;
2618 }
2619
2620 if (vht80) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002621 tmp = max_vht80_rate(snr) + 1;
2622 if (tmp > est)
2623 est = tmp;
2624 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08002625
2626 if (vht160 &&
2627 (hw_mode->vht_capab &
2628 (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2629 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
2630 tmp = max_vht160_rate(snr) + 1;
2631 if (tmp > est)
2632 est = tmp;
2633 }
2634 }
2635 }
2636
2637 if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
2638 /* Use +2 to assume HE is always faster than HT/VHT */
2639 struct ieee80211_he_capabilities *he;
2640 struct he_capabilities *own_he;
2641 u8 cw;
2642
2643 ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
2644 if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
2645 return est;
2646 he = (struct ieee80211_he_capabilities *) &ie[3];
2647 own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
2648
2649 tmp = max_he_rate(he20_table, snr) + 2;
2650 if (tmp > est)
2651 est = tmp;
2652
2653 cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
2654 own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
2655 if (cw &
2656 (IS_2P4GHZ(freq) ? HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
2657 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2658 tmp = max_he_rate(he40_table, snr) + 2;
2659 if (tmp > est)
2660 est = tmp;
2661 }
2662
2663 if (!IS_2P4GHZ(freq) &&
2664 (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
2665 tmp = max_he_rate(he80_table, snr) + 2;
2666 if (tmp > est)
2667 est = tmp;
2668 }
2669
2670 if (!IS_2P4GHZ(freq) &&
2671 (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2672 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G))) {
2673 tmp = max_he_rate(he160_table, snr) + 2;
2674 if (tmp > est)
2675 est = tmp;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002676 }
2677 }
2678
Hai Shalomfdcde762020-04-02 11:19:20 -07002679 return est;
2680}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002681
Hai Shalomfdcde762020-04-02 11:19:20 -07002682
2683void scan_est_throughput(struct wpa_supplicant *wpa_s,
2684 struct wpa_scan_res *res)
2685{
2686 int rate; /* max legacy rate in 500 kb/s units */
2687 int snr = res->snr;
2688 const u8 *ies = (const void *) (res + 1);
2689 size_t ie_len = res->ie_len;
2690
2691 if (res->est_throughput)
2692 return;
2693
2694 /* Get maximum legacy rate */
2695 rate = wpa_scan_get_max_rate(res);
2696
2697 if (!ie_len)
2698 ie_len = res->beacon_ie_len;
2699 res->est_throughput =
Hai Shaloma20dcd72022-02-04 13:43:00 -08002700 wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, res->freq);
Hai Shalomfdcde762020-04-02 11:19:20 -07002701
2702 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002703}
2704
2705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706/**
2707 * wpa_supplicant_get_scan_results - Get scan results
2708 * @wpa_s: Pointer to wpa_supplicant data
2709 * @info: Information about what was scanned or %NULL if not available
2710 * @new_scan: Whether a new scan was performed
2711 * Returns: Scan results, %NULL on failure
2712 *
2713 * This function request the current scan results from the driver and updates
2714 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2715 * results with wpa_scan_results_free().
2716 */
2717struct wpa_scan_results *
2718wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2719 struct scan_info *info, int new_scan)
2720{
2721 struct wpa_scan_results *scan_res;
2722 size_t i;
2723 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002725 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726 if (scan_res == NULL) {
2727 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2728 return NULL;
2729 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002730 if (scan_res->fetch_time.sec == 0) {
2731 /*
2732 * Make sure we have a valid timestamp if the driver wrapper
2733 * does not set this.
2734 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002735 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002736 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002737 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002739 for (i = 0; i < scan_res->num; i++) {
2740 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2741
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002742 scan_snr(scan_res_item);
2743 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002744 }
2745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002747 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2749 "provisioning rules");
2750 compar = wpa_scan_result_wps_compar;
2751 }
2752#endif /* CONFIG_WPS */
2753
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002754 if (scan_res->res) {
2755 qsort(scan_res->res, scan_res->num,
2756 sizeof(struct wpa_scan_res *), compar);
2757 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002758 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759
Dmitry Shmidt29333592017-01-09 12:27:11 -08002760 if (wpa_s->ignore_post_flush_scan_res) {
2761 /* FLUSH command aborted an ongoing scan and these are the
2762 * results from the aborted scan. Do not process the results to
2763 * maintain flushed state. */
2764 wpa_dbg(wpa_s, MSG_DEBUG,
2765 "Do not update BSS table based on pending post-FLUSH scan results");
2766 wpa_s->ignore_post_flush_scan_res = 0;
2767 return scan_res;
2768 }
2769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 wpa_bss_update_start(wpa_s);
2771 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002772 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2773 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 wpa_bss_update_end(wpa_s, info, new_scan);
2775
2776 return scan_res;
2777}
2778
2779
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002780/**
2781 * wpa_supplicant_update_scan_results - Update scan results from the driver
2782 * @wpa_s: Pointer to wpa_supplicant data
2783 * Returns: 0 on success, -1 on failure
2784 *
2785 * This function updates the BSS table within wpa_supplicant based on the
2786 * currently available scan results from the driver without requesting a new
2787 * scan. This is used in cases where the driver indicates an association
2788 * (including roaming within ESS) and wpa_supplicant does not yet have the
2789 * needed information to complete the connection (e.g., to perform validation
2790 * steps in 4-way handshake).
2791 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2793{
2794 struct wpa_scan_results *scan_res;
2795 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2796 if (scan_res == NULL)
2797 return -1;
2798 wpa_scan_results_free(scan_res);
2799
2800 return 0;
2801}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002802
2803
2804/**
2805 * scan_only_handler - Reports scan results
2806 */
2807void scan_only_handler(struct wpa_supplicant *wpa_s,
2808 struct wpa_scan_results *scan_res)
2809{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002810 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002811 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2812 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2813 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2814 wpa_s->manual_scan_id);
2815 wpa_s->manual_scan_use_id = 0;
2816 } else {
2817 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2818 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002819 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002820 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002821 if (wpa_s->scan_work) {
2822 struct wpa_radio_work *work = wpa_s->scan_work;
2823 wpa_s->scan_work = NULL;
2824 radio_work_done(work);
2825 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002826
2827 if (wpa_s->wpa_state == WPA_SCANNING)
2828 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002829}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002830
2831
2832int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2833{
2834 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2835}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002836
2837
2838struct wpa_driver_scan_params *
2839wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2840{
2841 struct wpa_driver_scan_params *params;
2842 size_t i;
2843 u8 *n;
2844
2845 params = os_zalloc(sizeof(*params));
2846 if (params == NULL)
2847 return NULL;
2848
2849 for (i = 0; i < src->num_ssids; i++) {
2850 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002851 n = os_memdup(src->ssids[i].ssid,
2852 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002853 if (n == NULL)
2854 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002855 params->ssids[i].ssid = n;
2856 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2857 }
2858 }
2859 params->num_ssids = src->num_ssids;
2860
2861 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002862 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002863 if (n == NULL)
2864 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002865 params->extra_ies = n;
2866 params->extra_ies_len = src->extra_ies_len;
2867 }
2868
2869 if (src->freqs) {
2870 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002871 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002872 if (params->freqs == NULL)
2873 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002874 }
2875
2876 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002877 params->filter_ssids = os_memdup(src->filter_ssids,
2878 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002879 src->num_filter_ssids);
2880 if (params->filter_ssids == NULL)
2881 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002882 params->num_filter_ssids = src->num_filter_ssids;
2883 }
2884
2885 params->filter_rssi = src->filter_rssi;
2886 params->p2p_probe = src->p2p_probe;
2887 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002888 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002889 params->duration = src->duration;
2890 params->duration_mandatory = src->duration_mandatory;
Hai Shalomce48b4a2018-09-05 11:41:35 -07002891 params->oce_scan = src->oce_scan;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002892
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002893 if (src->sched_scan_plans_num > 0) {
2894 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002895 os_memdup(src->sched_scan_plans,
2896 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002897 src->sched_scan_plans_num);
2898 if (!params->sched_scan_plans)
2899 goto failed;
2900
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002901 params->sched_scan_plans_num = src->sched_scan_plans_num;
2902 }
2903
Hai Shalomc3565922019-10-28 11:58:20 -07002904 if (src->mac_addr_rand &&
2905 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
2906 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002907
2908 if (src->bssid) {
2909 u8 *bssid;
2910
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002911 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002912 if (!bssid)
2913 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002914 params->bssid = bssid;
2915 }
2916
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002917 params->relative_rssi_set = src->relative_rssi_set;
2918 params->relative_rssi = src->relative_rssi;
2919 params->relative_adjust_band = src->relative_adjust_band;
2920 params->relative_adjust_rssi = src->relative_adjust_rssi;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002921 params->p2p_include_6ghz = src->p2p_include_6ghz;
Sunil8cd6f4d2022-06-28 18:40:46 +00002922 params->non_coloc_6ghz = src->non_coloc_6ghz;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002923 return params;
2924
2925failed:
2926 wpa_scan_free_params(params);
2927 return NULL;
2928}
2929
2930
2931void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2932{
2933 size_t i;
2934
2935 if (params == NULL)
2936 return;
2937
2938 for (i = 0; i < params->num_ssids; i++)
2939 os_free((u8 *) params->ssids[i].ssid);
2940 os_free((u8 *) params->extra_ies);
2941 os_free(params->freqs);
2942 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002943 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002944
2945 /*
2946 * Note: params->mac_addr_mask points to same memory allocation and
2947 * must not be freed separately.
2948 */
2949 os_free((u8 *) params->mac_addr);
2950
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002951 os_free((u8 *) params->bssid);
2952
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002953 os_free(params);
2954}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002955
2956
2957int wpas_start_pno(struct wpa_supplicant *wpa_s)
2958{
Hai Shalomfdcde762020-04-02 11:19:20 -07002959 int ret;
2960 size_t prio, i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002961 struct wpa_ssid *ssid;
2962 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002963 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002964 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002965
2966 if (!wpa_s->sched_scan_supported)
2967 return -1;
2968
Dmitry Shmidte4663042016-04-04 10:07:49 -07002969 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2970 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2971 else
2972 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2973 if (max_sched_scan_ssids < 1)
2974 return -1;
2975
Dmitry Shmidt98660862014-03-11 17:26:21 -07002976 if (wpa_s->pno || wpa_s->pno_sched_pending)
2977 return 0;
2978
2979 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002980 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002981 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2982 return -EAGAIN;
2983 }
2984
2985 if (wpa_s->wpa_state == WPA_SCANNING) {
2986 wpa_supplicant_cancel_scan(wpa_s);
2987 if (wpa_s->sched_scanning) {
2988 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2989 "ongoing sched scan");
2990 wpa_supplicant_cancel_sched_scan(wpa_s);
2991 wpa_s->pno_sched_pending = 1;
2992 return 0;
2993 }
2994 }
2995
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002996 if (wpa_s->sched_scan_stop_req) {
2997 wpa_printf(MSG_DEBUG,
2998 "Schedule PNO after previous sched scan has stopped");
2999 wpa_s->pno_sched_pending = 1;
3000 return 0;
3001 }
3002
Dmitry Shmidt98660862014-03-11 17:26:21 -07003003 os_memset(&params, 0, sizeof(params));
3004
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003005 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003006 ssid = wpa_s->conf->ssid;
3007 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003008 if (!wpas_network_disabled(wpa_s, ssid)) {
3009 num_match_ssid++;
3010 if (ssid->scan_ssid)
3011 num_ssid++;
3012 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003013 ssid = ssid->next;
3014 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003015
3016 if (num_match_ssid == 0) {
3017 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3018 return -1;
3019 }
3020
3021 if (num_match_ssid > num_ssid) {
3022 params.num_ssids++; /* wildcard */
3023 num_ssid++;
3024 }
3025
Dmitry Shmidte4663042016-04-04 10:07:49 -07003026 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07003027 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07003028 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3029 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003030 }
3031
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003032 if (num_match_ssid > wpa_s->max_match_sets) {
3033 num_match_ssid = wpa_s->max_match_sets;
3034 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003035 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003036 params.filter_ssids = os_calloc(num_match_ssid,
3037 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07003038 if (params.filter_ssids == NULL)
3039 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003040
Dmitry Shmidt98660862014-03-11 17:26:21 -07003041 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003042 prio = 0;
3043 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003044 while (ssid) {
3045 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003046 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3047 params.ssids[params.num_ssids].ssid =
3048 ssid->ssid;
3049 params.ssids[params.num_ssids].ssid_len =
3050 ssid->ssid_len;
3051 params.num_ssids++;
3052 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003053 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3054 ssid->ssid_len);
3055 params.filter_ssids[i].ssid_len = ssid->ssid_len;
3056 params.num_filter_ssids++;
3057 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003058 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07003059 break;
3060 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003061 if (ssid->pnext)
3062 ssid = ssid->pnext;
3063 else if (prio + 1 == wpa_s->conf->num_prio)
3064 break;
3065 else
3066 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07003067 }
3068
3069 if (wpa_s->conf->filter_rssi)
3070 params.filter_rssi = wpa_s->conf->filter_rssi;
3071
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003072 if (wpa_s->sched_scan_plans_num) {
3073 params.sched_scan_plans = wpa_s->sched_scan_plans;
3074 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3075 } else {
3076 /* Set one scan plan that will run infinitely */
3077 if (wpa_s->conf->sched_scan_interval)
3078 scan_plan.interval = wpa_s->conf->sched_scan_interval;
3079 else
3080 scan_plan.interval = 10;
3081
3082 scan_plan.iterations = 0;
3083 params.sched_scan_plans = &scan_plan;
3084 params.sched_scan_plans_num = 1;
3085 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003086
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003087 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3088
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003089 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3090 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3091 params.freqs = wpa_s->manual_sched_scan_freqs;
3092 }
3093
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003094 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
Hai Shalomc3565922019-10-28 11:58:20 -07003095 wpa_s->wpa_state <= WPA_SCANNING)
3096 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003097
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003098 wpa_scan_set_relative_rssi_params(wpa_s, &params);
3099
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003100 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003101 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07003102 os_free(params.mac_addr);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003103 if (ret == 0)
3104 wpa_s->pno = 1;
3105 else
3106 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3107 return ret;
3108}
3109
3110
3111int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3112{
3113 int ret = 0;
3114
3115 if (!wpa_s->pno)
3116 return 0;
3117
3118 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003119 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003120
3121 wpa_s->pno = 0;
3122 wpa_s->pno_sched_pending = 0;
3123
3124 if (wpa_s->wpa_state == WPA_SCANNING)
3125 wpa_supplicant_req_scan(wpa_s, 0, 0);
3126
3127 return ret;
3128}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003129
3130
3131void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3132 unsigned int type)
3133{
3134 type &= MAC_ADDR_RAND_ALL;
3135 wpa_s->mac_addr_rand_enable &= ~type;
3136
3137 if (type & MAC_ADDR_RAND_SCAN) {
3138 os_free(wpa_s->mac_addr_scan);
3139 wpa_s->mac_addr_scan = NULL;
3140 }
3141
3142 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3143 os_free(wpa_s->mac_addr_sched_scan);
3144 wpa_s->mac_addr_sched_scan = NULL;
3145 }
3146
3147 if (type & MAC_ADDR_RAND_PNO) {
3148 os_free(wpa_s->mac_addr_pno);
3149 wpa_s->mac_addr_pno = NULL;
3150 }
3151}
3152
3153
3154int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3155 unsigned int type, const u8 *addr,
3156 const u8 *mask)
3157{
3158 u8 *tmp = NULL;
3159
Hai Shalom74f70d42019-02-11 14:42:39 -08003160 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3161 wpa_printf(MSG_INFO,
3162 "scan: MAC randomization type %u != supported=%u",
3163 type, wpa_s->mac_addr_rand_supported);
3164 return -1;
3165 }
3166
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003167 wpas_mac_addr_rand_scan_clear(wpa_s, type);
3168
3169 if (addr) {
3170 tmp = os_malloc(2 * ETH_ALEN);
3171 if (!tmp)
3172 return -1;
3173 os_memcpy(tmp, addr, ETH_ALEN);
3174 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3175 }
3176
3177 if (type == MAC_ADDR_RAND_SCAN) {
3178 wpa_s->mac_addr_scan = tmp;
3179 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3180 wpa_s->mac_addr_sched_scan = tmp;
3181 } else if (type == MAC_ADDR_RAND_PNO) {
3182 wpa_s->mac_addr_pno = tmp;
3183 } else {
3184 wpa_printf(MSG_INFO,
3185 "scan: Invalid MAC randomization type=0x%x",
3186 type);
3187 os_free(tmp);
3188 return -1;
3189 }
3190
3191 wpa_s->mac_addr_rand_enable |= type;
3192 return 0;
3193}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003194
3195
Hai Shalomc3565922019-10-28 11:58:20 -07003196int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3197 unsigned int type, u8 *mask)
3198{
3199 const u8 *to_copy;
3200
3201 if ((wpa_s->mac_addr_rand_enable & type) != type)
3202 return -1;
3203
3204 if (type == MAC_ADDR_RAND_SCAN) {
3205 to_copy = wpa_s->mac_addr_scan;
3206 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3207 to_copy = wpa_s->mac_addr_sched_scan;
3208 } else if (type == MAC_ADDR_RAND_PNO) {
3209 to_copy = wpa_s->mac_addr_pno;
3210 } else {
3211 wpa_printf(MSG_DEBUG,
3212 "scan: Invalid MAC randomization type=0x%x",
3213 type);
3214 return -1;
3215 }
3216
3217 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3218 return 0;
3219}
3220
3221
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003222int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3223{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003224 struct wpa_radio_work *work;
3225 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003226
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003227 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3228 if (work->wpa_s != wpa_s || !work->started ||
3229 (os_strcmp(work->type, "scan") != 0 &&
3230 os_strcmp(work->type, "p2p-scan") != 0))
3231 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003232 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003233 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003234 }
3235
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003236 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3237 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003238}
3239
3240
3241int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3242{
3243 struct sched_scan_plan *scan_plans = NULL;
3244 const char *token, *context = NULL;
3245 unsigned int num = 0;
3246
3247 if (!cmd)
3248 return -1;
3249
3250 if (!cmd[0]) {
3251 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3252 os_free(wpa_s->sched_scan_plans);
3253 wpa_s->sched_scan_plans = NULL;
3254 wpa_s->sched_scan_plans_num = 0;
3255 return 0;
3256 }
3257
3258 while ((token = cstr_token(cmd, " ", &context))) {
3259 int ret;
3260 struct sched_scan_plan *scan_plan, *n;
3261
3262 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3263 if (!n)
3264 goto fail;
3265
3266 scan_plans = n;
3267 scan_plan = &scan_plans[num];
3268 num++;
3269
3270 ret = sscanf(token, "%u:%u", &scan_plan->interval,
3271 &scan_plan->iterations);
3272 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3273 wpa_printf(MSG_ERROR,
3274 "Invalid sched scan plan input: %s", token);
3275 goto fail;
3276 }
3277
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003278 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3279 wpa_printf(MSG_WARNING,
3280 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3281 num, scan_plan->interval,
3282 wpa_s->max_sched_scan_plan_interval);
3283 scan_plan->interval =
3284 wpa_s->max_sched_scan_plan_interval;
3285 }
3286
3287 if (ret == 1) {
3288 scan_plan->iterations = 0;
3289 break;
3290 }
3291
3292 if (!scan_plan->iterations) {
3293 wpa_printf(MSG_ERROR,
3294 "scan plan %u: Number of iterations cannot be zero",
3295 num);
3296 goto fail;
3297 }
3298
3299 if (scan_plan->iterations >
3300 wpa_s->max_sched_scan_plan_iterations) {
3301 wpa_printf(MSG_WARNING,
3302 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3303 num, scan_plan->iterations,
3304 wpa_s->max_sched_scan_plan_iterations);
3305 scan_plan->iterations =
3306 wpa_s->max_sched_scan_plan_iterations;
3307 }
3308
3309 wpa_printf(MSG_DEBUG,
3310 "scan plan %u: interval=%u iterations=%u",
3311 num, scan_plan->interval, scan_plan->iterations);
3312 }
3313
3314 if (!scan_plans) {
3315 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3316 goto fail;
3317 }
3318
3319 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3320 wpa_printf(MSG_ERROR,
3321 "All scan plans but the last must specify a number of iterations");
3322 goto fail;
3323 }
3324
3325 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3326 num, scan_plans[num - 1].interval);
3327
3328 if (num > wpa_s->max_sched_scan_plans) {
3329 wpa_printf(MSG_WARNING,
3330 "Too many scheduled scan plans (only %u supported)",
3331 wpa_s->max_sched_scan_plans);
3332 wpa_printf(MSG_WARNING,
3333 "Use only the first %u scan plans, and the last one (in infinite loop)",
3334 wpa_s->max_sched_scan_plans - 1);
3335 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3336 &scan_plans[num - 1], sizeof(*scan_plans));
3337 num = wpa_s->max_sched_scan_plans;
3338 }
3339
3340 os_free(wpa_s->sched_scan_plans);
3341 wpa_s->sched_scan_plans = scan_plans;
3342 wpa_s->sched_scan_plans_num = num;
3343
3344 return 0;
3345
3346fail:
3347 os_free(scan_plans);
3348 wpa_printf(MSG_ERROR, "invalid scan plans list");
3349 return -1;
3350}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003351
3352
3353/**
3354 * wpas_scan_reset_sched_scan - Reset sched_scan state
3355 * @wpa_s: Pointer to wpa_supplicant data
3356 *
3357 * This function is used to cancel a running scheduled scan and to reset an
3358 * internal scan state to continue with a regular scan on the following
3359 * wpa_supplicant_req_scan() calls.
3360 */
3361void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3362{
3363 wpa_s->normal_scans = 0;
3364 if (wpa_s->sched_scanning) {
3365 wpa_s->sched_scan_timed_out = 0;
3366 wpa_s->prev_sched_ssid = NULL;
3367 wpa_supplicant_cancel_sched_scan(wpa_s);
3368 }
3369}
3370
3371
3372void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3373{
3374 /* simulate timeout to restart the sched scan */
3375 wpa_s->sched_scan_timed_out = 1;
3376 wpa_s->prev_sched_ssid = NULL;
3377 wpa_supplicant_cancel_sched_scan(wpa_s);
3378}