blob: 4d158a9c66daf2656b13ebaac114a8e85b766442 [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
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800240 wpa_supplicant_notify_scanning(wpa_s, 0);
241 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800242 if (wpa_s->wpa_state == WPA_SCANNING)
243 wpa_supplicant_set_state(wpa_s,
244 wpa_s->scan_prev_wpa_state);
245 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
246 ret, retry ? " retry=1" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800247 radio_work_done(work);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800248
249 if (retry) {
250 /* Restore scan_req since we will try to scan again */
251 wpa_s->scan_req = wpa_s->last_scan_req;
252 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700253 } else if (wpa_s->scan_res_handler) {
254 /* Clear the scan_res_handler */
255 wpa_s->scan_res_handler = NULL;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800256 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700257
258 if (wpa_s->beacon_rep_data.token)
259 wpas_rrm_refuse_request(wpa_s);
260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800263
264 os_get_reltime(&wpa_s->scan_trigger_time);
265 wpa_s->scan_runs++;
266 wpa_s->normal_scans++;
267 wpa_s->own_scan_requested = 1;
268 wpa_s->clear_driver_scan_cache = 0;
269 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270}
271
272
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800273/**
274 * wpa_supplicant_trigger_scan - Request driver to start a scan
275 * @wpa_s: Pointer to wpa_supplicant data
276 * @params: Scan parameters
277 * Returns: 0 on success, -1 on failure
278 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
280 struct wpa_driver_scan_params *params)
281{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800282 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800284 if (wpa_s->scan_work) {
285 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
286 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800287 }
288
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800289 ctx = wpa_scan_clone_params(params);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700290 if (!ctx ||
291 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800292 {
293 wpa_scan_free_params(ctx);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700294 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800295 return -1;
296 }
297
298 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800299}
300
301
302static void
303wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
304{
305 struct wpa_supplicant *wpa_s = eloop_ctx;
306
307 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
308
309 if (wpa_supplicant_req_sched_scan(wpa_s))
310 wpa_supplicant_req_scan(wpa_s, 0, 0);
311}
312
313
314static void
315wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
316{
317 struct wpa_supplicant *wpa_s = eloop_ctx;
318
319 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
320
321 wpa_s->sched_scan_timed_out = 1;
322 wpa_supplicant_cancel_sched_scan(wpa_s);
323}
324
325
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700326static int
327wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
328 struct wpa_driver_scan_params *params)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800329{
330 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800332 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800333 ret = wpa_drv_sched_scan(wpa_s, params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334 if (ret)
335 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800336 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800337 wpa_s->sched_scanning = 1;
338
339 return ret;
340}
341
342
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700343static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800344{
345 int ret;
346
347 ret = wpa_drv_stop_sched_scan(wpa_s);
348 if (ret) {
349 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
350 /* TODO: what to do if stopping fails? */
351 return -1;
352 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353
354 return ret;
355}
356
357
358static struct wpa_driver_scan_filter *
359wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
360{
361 struct wpa_driver_scan_filter *ssids;
362 struct wpa_ssid *ssid;
363 size_t count;
364
365 *num_ssids = 0;
366 if (!conf->filter_ssids)
367 return NULL;
368
369 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
370 if (ssid->ssid && ssid->ssid_len)
371 count++;
372 }
373 if (count == 0)
374 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800375 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 if (ssids == NULL)
377 return NULL;
378
379 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
380 if (!ssid->ssid || !ssid->ssid_len)
381 continue;
382 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
383 ssids[*num_ssids].ssid_len = ssid->ssid_len;
384 (*num_ssids)++;
385 }
386
387 return ssids;
388}
389
390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800391static void wpa_supplicant_optimize_freqs(
392 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
393{
394#ifdef CONFIG_P2P
395 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
396 wpa_s->go_params) {
397 /* Optimize provisioning state scan based on GO information */
398 if (wpa_s->p2p_in_provisioning < 5 &&
399 wpa_s->go_params->freq > 0) {
400 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
401 "preferred frequency %d MHz",
402 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800403 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800404 if (params->freqs)
405 params->freqs[0] = wpa_s->go_params->freq;
406 } else if (wpa_s->p2p_in_provisioning < 8 &&
407 wpa_s->go_params->freq_list[0]) {
408 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
409 "channels");
410 int_array_concat(&params->freqs,
411 wpa_s->go_params->freq_list);
412 if (params->freqs)
413 int_array_sort_unique(params->freqs);
414 }
415 wpa_s->p2p_in_provisioning++;
416 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700417
418 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
419 /*
420 * Optimize scan based on GO information during persistent
421 * group reinvocation
422 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700423 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700424 wpa_s->p2p_invite_go_freq > 0) {
425 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
426 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800427 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700428 if (params->freqs)
429 params->freqs[0] = wpa_s->p2p_invite_go_freq;
430 }
431 wpa_s->p2p_in_invitation++;
432 if (wpa_s->p2p_in_invitation > 20) {
433 /*
434 * This should not really happen since the variable is
435 * cleared on group removal, but if it does happen, make
436 * sure we do not get stuck in special invitation scan
437 * mode.
438 */
439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
440 wpa_s->p2p_in_invitation = 0;
441 }
442 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800443#endif /* CONFIG_P2P */
444
445#ifdef CONFIG_WPS
446 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
447 /*
448 * Optimize post-provisioning scan based on channel used
449 * during provisioning.
450 */
451 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
452 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800453 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454 if (params->freqs)
455 params->freqs[0] = wpa_s->wps_freq;
456 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800457 } else if (wpa_s->after_wps)
458 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800459
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800460 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
461 {
462 /* Optimize provisioning scan based on already known channel */
463 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
464 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800465 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800466 if (params->freqs)
467 params->freqs[0] = wpa_s->wps_freq;
468 wpa_s->known_wps_freq = 0; /* only do this once */
469 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800470#endif /* CONFIG_WPS */
471}
472
473
474#ifdef CONFIG_INTERWORKING
475static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
476 struct wpabuf *buf)
477{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800478 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
479 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
480 1 + ETH_ALEN);
481 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
482 /* No Venue Info */
483 if (!is_zero_ether_addr(wpa_s->conf->hessid))
484 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
485}
486#endif /* CONFIG_INTERWORKING */
487
488
Hai Shalomce48b4a2018-09-05 11:41:35 -0700489#ifdef CONFIG_MBO
490static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
491 struct wpabuf **ie)
492{
493 if (wpabuf_resize(ie, 5)) {
494 wpa_printf(MSG_DEBUG,
495 "Failed to allocate space for FILS Request Parameters element");
496 return;
497 }
498
499 /* FILS Request Parameters element */
500 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
501 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
502 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
503 /* Parameter control bitmap */
504 wpabuf_put_u8(*ie, 0);
505 /* Max Channel Time field - contains the value of MaxChannelTime
506 * parameter of the MLME-SCAN.request primitive represented in units of
507 * TUs, as an unsigned integer. A Max Channel Time field value of 255
508 * is used to indicate any duration of more than 254 TUs, or an
509 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
510 */
511 wpabuf_put_u8(*ie, 255);
512}
513#endif /* CONFIG_MBO */
514
515
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700516void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
517{
518 struct wpabuf *default_ies = NULL;
519 u8 ext_capab[18];
520 int ext_capab_len;
521 enum wpa_driver_if_type type = WPA_IF_STATION;
522
523#ifdef CONFIG_P2P
524 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
525 type = WPA_IF_P2P_CLIENT;
526#endif /* CONFIG_P2P */
527
528 wpa_drv_get_ext_capa(wpa_s, type);
529
530 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
531 sizeof(ext_capab));
532 if (ext_capab_len > 0 &&
533 wpabuf_resize(&default_ies, ext_capab_len) == 0)
534 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
535
536#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -0700537 if (wpa_s->enable_oce & OCE_STA)
538 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700539 /* Send MBO and OCE capabilities */
540 if (wpabuf_resize(&default_ies, 12) == 0)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700541 wpas_mbo_scan_ie(wpa_s, default_ies);
542#endif /* CONFIG_MBO */
543
544 if (default_ies)
545 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
546 wpabuf_len(default_ies));
547 wpabuf_free(default_ies);
548}
549
550
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700551static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552{
553 struct wpabuf *extra_ie = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700554 u8 ext_capab[18];
555 int ext_capab_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800556#ifdef CONFIG_WPS
557 int wps = 0;
558 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
559#endif /* CONFIG_WPS */
560
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700561#ifdef CONFIG_P2P
562 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
563 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
564 else
565#endif /* CONFIG_P2P */
566 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
567
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700568 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
569 sizeof(ext_capab));
570 if (ext_capab_len > 0 &&
571 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
572 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574#ifdef CONFIG_INTERWORKING
575 if (wpa_s->conf->interworking &&
576 wpabuf_resize(&extra_ie, 100) == 0)
577 wpas_add_interworking_elements(wpa_s, extra_ie);
578#endif /* CONFIG_INTERWORKING */
579
Hai Shalomce48b4a2018-09-05 11:41:35 -0700580#ifdef CONFIG_MBO
581 if (wpa_s->enable_oce & OCE_STA)
582 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
583#endif /* CONFIG_MBO */
584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585#ifdef CONFIG_WPS
586 wps = wpas_wps_in_use(wpa_s, &req_type);
587
588 if (wps) {
589 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700590 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
591 DEV_PW_DEFAULT,
592 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800593 wpa_s->wps->uuid, req_type,
594 0, NULL);
595 if (wps_ie) {
596 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
597 wpabuf_put_buf(extra_ie, wps_ie);
598 wpabuf_free(wps_ie);
599 }
600 }
601
602#ifdef CONFIG_P2P
603 if (wps) {
604 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
605 if (wpabuf_resize(&extra_ie, ielen) == 0)
606 wpas_p2p_scan_ie(wpa_s, extra_ie);
607 }
608#endif /* CONFIG_P2P */
609
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800610 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
611
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612#endif /* CONFIG_WPS */
613
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700614#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -0800615 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
616 wpas_hs20_add_indication(extra_ie, -1, 0);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700617#endif /* CONFIG_HS20 */
618
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800619#ifdef CONFIG_FST
620 if (wpa_s->fst_ies &&
621 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
622 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
623#endif /* CONFIG_FST */
624
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800625#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700626 /* Send MBO and OCE capabilities */
627 if (wpabuf_resize(&extra_ie, 12) == 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800628 wpas_mbo_scan_ie(wpa_s, extra_ie);
629#endif /* CONFIG_MBO */
630
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700631 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
632 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
633
634 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
635 wpabuf_put_buf(extra_ie, buf);
636 }
637
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800638 return extra_ie;
639}
640
641
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800642#ifdef CONFIG_P2P
643
644/*
645 * Check whether there are any enabled networks or credentials that could be
646 * used for a non-P2P connection.
647 */
648static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
649{
650 struct wpa_ssid *ssid;
651
652 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
653 if (wpas_network_disabled(wpa_s, ssid))
654 continue;
655 if (!ssid->p2p_group)
656 return 1;
657 }
658
659 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
660 wpa_s->conf->auto_interworking)
661 return 1;
662
663 return 0;
664}
665
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700666#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800667
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800668
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700669static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
670 enum hostapd_hw_mode band,
671 struct wpa_driver_scan_params *params)
672{
673 /* Include only supported channels for the specified band */
674 struct hostapd_hw_modes *mode;
675 int count, i;
676
677 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
678 if (mode == NULL) {
679 /* No channels supported in this band - use empty list */
680 params->freqs = os_zalloc(sizeof(int));
681 return;
682 }
683
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800684 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700685 if (params->freqs == NULL)
686 return;
687 for (count = 0, i = 0; i < mode->num_channels; i++) {
688 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
689 continue;
690 params->freqs[count++] = mode->channels[i].freq;
691 }
692}
693
694
695static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
696 struct wpa_driver_scan_params *params)
697{
698 if (wpa_s->hw.modes == NULL)
699 return; /* unknown what channels the driver supports */
700 if (params->freqs)
701 return; /* already using a limited channel set */
702 if (wpa_s->setband == WPA_SETBAND_5G)
703 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
704 params);
705 else if (wpa_s->setband == WPA_SETBAND_2G)
706 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
707 params);
708}
709
710
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800711static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
712 struct wpa_driver_scan_params *params,
713 size_t max_ssids, const u8 *ssid, size_t ssid_len)
714{
715 unsigned int j;
716
717 for (j = 0; j < params->num_ssids; j++) {
718 if (params->ssids[j].ssid_len == ssid_len &&
719 params->ssids[j].ssid &&
720 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
721 return; /* already in the list */
722 }
723
724 if (params->num_ssids + 1 > max_ssids) {
725 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
726 return;
727 }
728
729 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
730 wpa_ssid_txt(ssid, ssid_len));
731
732 params->ssids[params->num_ssids].ssid = ssid;
733 params->ssids[params->num_ssids].ssid_len = ssid_len;
734 params->num_ssids++;
735}
736
737
738static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
739 struct wpa_driver_scan_params *params,
740 struct wpa_ssid *ssid, size_t max_ssids)
741{
742#ifdef CONFIG_OWE
743 struct wpa_bss *bss;
744
745 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
746 return;
747
748 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
749 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
750
751 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
752 const u8 *owe, *pos, *end;
753 const u8 *owe_ssid;
754 size_t owe_ssid_len;
755
756 if (bss->ssid_len != ssid->ssid_len ||
757 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
758 continue;
759
760 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
761 if (!owe || owe[1] < 4)
762 continue;
763
764 pos = owe + 6;
765 end = owe + 2 + owe[1];
766
767 /* Must include BSSID and ssid_len */
768 if (end - pos < ETH_ALEN + 1)
769 return;
770
771 /* Skip BSSID */
772 pos += ETH_ALEN;
773 owe_ssid_len = *pos++;
774 owe_ssid = pos;
775
776 if ((size_t) (end - pos) < owe_ssid_len ||
777 owe_ssid_len > SSID_MAX_LEN)
778 return;
779
780 wpa_printf(MSG_DEBUG,
781 "OWE: scan_ssids: transition mode OWE ssid=%s",
782 wpa_ssid_txt(owe_ssid, owe_ssid_len));
783
784 wpa_add_scan_ssid(wpa_s, params, max_ssids,
785 owe_ssid, owe_ssid_len);
786 return;
787 }
788#endif /* CONFIG_OWE */
789}
790
791
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700792static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
793 struct wpa_driver_scan_params *params,
794 size_t max_ssids)
795{
796 unsigned int i;
797 struct wpa_ssid *ssid;
798
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700799 /*
800 * For devices with max_ssids greater than 1, leave the last slot empty
801 * for adding the wildcard scan entry.
802 */
803 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
804
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700805 for (i = 0; i < wpa_s->scan_id_count; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700806 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800807 if (!ssid)
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700808 continue;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800809 if (ssid->scan_ssid)
810 wpa_add_scan_ssid(wpa_s, params, max_ssids,
811 ssid->ssid, ssid->ssid_len);
812 /*
813 * Also add the SSID of the OWE BSS, to allow discovery of
814 * transition mode APs more quickly.
815 */
816 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700817 }
818
819 wpa_s->scan_id_count = 0;
820}
821
822
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700823static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
824 struct wpa_driver_scan_params *params,
825 size_t max_ssids)
826{
827 unsigned int i;
828
829 if (wpa_s->ssids_from_scan_req == NULL ||
830 wpa_s->num_ssids_from_scan_req == 0)
831 return 0;
832
833 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
834 wpa_s->num_ssids_from_scan_req = max_ssids;
835 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
836 (unsigned int) max_ssids);
837 }
838
839 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
840 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
841 params->ssids[i].ssid_len =
842 wpa_s->ssids_from_scan_req[i].ssid_len;
843 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
844 params->ssids[i].ssid,
845 params->ssids[i].ssid_len);
846 }
847
848 params->num_ssids = wpa_s->num_ssids_from_scan_req;
849 wpa_s->num_ssids_from_scan_req = 0;
850 return 1;
851}
852
853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
855{
856 struct wpa_supplicant *wpa_s = eloop_ctx;
857 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800858 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700859 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700861 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862 size_t max_ssids;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800863 int connect_without_scan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864
Dmitry Shmidt29333592017-01-09 12:27:11 -0800865 wpa_s->ignore_post_flush_scan_res = 0;
866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
868 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
869 return;
870 }
871
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800872 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700873 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
875 return;
876 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800877
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700878 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800879 /*
880 * If we are already in scanning state, we shall reschedule the
881 * the incoming scan request.
882 */
883 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
884 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700885 return;
886 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800887
Dmitry Shmidt04949592012-07-19 12:16:46 -0700888 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800889 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
891 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
892 return;
893 }
894
895 if (wpa_s->conf->ap_scan != 0 &&
896 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
897 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
898 "overriding ap_scan configuration");
899 wpa_s->conf->ap_scan = 0;
900 wpas_notify_ap_scan_changed(wpa_s);
901 }
902
903 if (wpa_s->conf->ap_scan == 0) {
904 wpa_supplicant_gen_assoc_event(wpa_s);
905 return;
906 }
907
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800908 ssid = NULL;
909 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
910 wpa_s->connect_without_scan) {
911 connect_without_scan = 1;
912 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
913 if (ssid == wpa_s->connect_without_scan)
914 break;
915 }
916 }
917
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800918 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800919 if (p2p_in_prog && p2p_in_prog != 2 &&
920 (!ssid ||
921 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800922 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
923 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700924 return;
925 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700926
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800927 /*
928 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
929 * used for changing modes inside wpa_supplicant (roaming,
930 * auto-reconnect, etc). Discarding the scan might hurt these processes.
931 * The normal use case for PNO is to suspend the host immediately after
932 * starting PNO, so the periodic 100 ms attempts to run the scan do not
933 * normally happen in practice multiple times, i.e., this is simply
934 * restarting scanning once the host is woken up and PNO stopped.
935 */
936 if (wpa_s->pno || wpa_s->pno_sched_pending) {
937 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
938 wpa_supplicant_req_scan(wpa_s, 0, 100000);
939 return;
940 }
941
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800942 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 max_ssids = 1;
944 else {
945 max_ssids = wpa_s->max_scan_ssids;
946 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
947 max_ssids = WPAS_MAX_SCAN_SSIDS;
948 }
949
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800950 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800951 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700952
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800953 if (connect_without_scan) {
954 wpa_s->connect_without_scan = NULL;
955 if (ssid) {
956 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
957 "without scan step");
958 wpa_supplicant_associate(wpa_s, NULL, ssid);
959 return;
960 }
961 }
962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 os_memset(&params, 0, sizeof(params));
964
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800965 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
967 wpa_s->wpa_state == WPA_INACTIVE)
968 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
969
Dmitry Shmidt04949592012-07-19 12:16:46 -0700970 /*
971 * If autoscan has set its own scanning parameters
972 */
973 if (wpa_s->autoscan_params != NULL) {
974 scan_params = wpa_s->autoscan_params;
975 goto scan;
976 }
977
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700978 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
979 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
980 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
981 goto ssid_list_set;
982 }
983
Dmitry Shmidt04949592012-07-19 12:16:46 -0700984#ifdef CONFIG_P2P
985 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800986 wpa_s->go_params && !wpa_s->conf->passive_scan) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800987 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
988 wpa_s->p2p_in_provisioning,
989 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700990 params.ssids[0].ssid = wpa_s->go_params->ssid;
991 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
992 params.num_ssids = 1;
993 goto ssid_list_set;
994 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700995
996 if (wpa_s->p2p_in_invitation) {
997 if (wpa_s->current_ssid) {
998 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
999 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1000 params.ssids[0].ssid_len =
1001 wpa_s->current_ssid->ssid_len;
1002 params.num_ssids = 1;
1003 } else {
1004 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1005 }
1006 goto ssid_list_set;
1007 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001008#endif /* CONFIG_P2P */
1009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 /* Find the starting point from which to continue scanning */
1011 ssid = wpa_s->conf->ssid;
1012 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1013 while (ssid) {
1014 if (ssid == wpa_s->prev_scan_ssid) {
1015 ssid = ssid->next;
1016 break;
1017 }
1018 ssid = ssid->next;
1019 }
1020 }
1021
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001022 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001023#ifdef CONFIG_AP
1024 !wpa_s->ap_iface &&
1025#endif /* CONFIG_AP */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001026 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001027 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001028 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 wpa_supplicant_assoc_try(wpa_s, ssid);
1030 return;
1031 } else if (wpa_s->conf->ap_scan == 2) {
1032 /*
1033 * User-initiated scan request in ap_scan == 2; scan with
1034 * wildcard SSID.
1035 */
1036 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001037 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1038 /*
1039 * Perform single-channel single-SSID scan for
1040 * reassociate-to-same-BSS operation.
1041 */
1042 /* Setup SSID */
1043 ssid = wpa_s->current_ssid;
1044 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1045 ssid->ssid, ssid->ssid_len);
1046 params.ssids[0].ssid = ssid->ssid;
1047 params.ssids[0].ssid_len = ssid->ssid_len;
1048 params.num_ssids = 1;
1049
1050 /*
1051 * Allocate memory for frequency array, allocate one extra
1052 * slot for the zero-terminator.
1053 */
1054 params.freqs = os_malloc(sizeof(int) * 2);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001055 if (params.freqs) {
1056 params.freqs[0] = wpa_s->assoc_freq;
1057 params.freqs[1] = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001058 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07001059
1060 /*
1061 * Reset the reattach flag so that we fall back to full scan if
1062 * this scan fails.
1063 */
1064 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 } else {
1066 struct wpa_ssid *start = ssid, *tssid;
1067 int freqs_set = 0;
1068 if (ssid == NULL && max_ssids > 1)
1069 ssid = wpa_s->conf->ssid;
1070 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001071 if (!wpas_network_disabled(wpa_s, ssid) &&
1072 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1074 ssid->ssid, ssid->ssid_len);
1075 params.ssids[params.num_ssids].ssid =
1076 ssid->ssid;
1077 params.ssids[params.num_ssids].ssid_len =
1078 ssid->ssid_len;
1079 params.num_ssids++;
1080 if (params.num_ssids + 1 >= max_ssids)
1081 break;
1082 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001083
1084 if (!wpas_network_disabled(wpa_s, ssid)) {
1085 /*
1086 * Also add the SSID of the OWE BSS, to allow
1087 * discovery of transition mode APs more
1088 * quickly.
1089 */
1090 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1091 max_ssids);
1092 }
1093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 ssid = ssid->next;
1095 if (ssid == start)
1096 break;
1097 if (ssid == NULL && max_ssids > 1 &&
1098 start != wpa_s->conf->ssid)
1099 ssid = wpa_s->conf->ssid;
1100 }
1101
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001102 if (wpa_s->scan_id_count &&
1103 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1104 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1105
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001106 for (tssid = wpa_s->conf->ssid;
1107 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1108 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001109 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 continue;
1111 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
1112 int_array_concat(&params.freqs,
1113 tssid->scan_freq);
1114 } else {
1115 os_free(params.freqs);
1116 params.freqs = NULL;
1117 }
1118 freqs_set = 1;
1119 }
1120 int_array_sort_unique(params.freqs);
1121 }
1122
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001123 if (ssid && max_ssids == 1) {
1124 /*
1125 * If the driver is limited to 1 SSID at a time interleave
1126 * wildcard SSID scans with specific SSID scans to avoid
1127 * waiting a long time for a wildcard scan.
1128 */
1129 if (!wpa_s->prev_scan_wildcard) {
1130 params.ssids[0].ssid = NULL;
1131 params.ssids[0].ssid_len = 0;
1132 wpa_s->prev_scan_wildcard = 1;
1133 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1134 "wildcard SSID (Interleave with specific)");
1135 } else {
1136 wpa_s->prev_scan_ssid = ssid;
1137 wpa_s->prev_scan_wildcard = 0;
1138 wpa_dbg(wpa_s, MSG_DEBUG,
1139 "Starting AP scan for specific SSID: %s",
1140 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001142 } else if (ssid) {
1143 /* max_ssids > 1 */
1144
1145 wpa_s->prev_scan_ssid = ssid;
1146 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1147 "the scan request");
1148 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001149 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1150 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1151 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001152 } else if (wpa_s->conf->passive_scan) {
1153 wpa_dbg(wpa_s, MSG_DEBUG,
1154 "Use passive scan based on configuration");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 } else {
1156 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1157 params.num_ssids++;
1158 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1159 "SSID");
1160 }
1161
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001162ssid_list_set:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001163 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001164 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001166 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001167 wpa_s->manual_scan_only_new) {
1168 wpa_printf(MSG_DEBUG,
1169 "Request driver to clear scan cache due to manual only_new=1 scan");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001170 params.only_new_results = 1;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001171 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001172
1173 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1174 wpa_s->manual_scan_freqs) {
1175 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1176 params.freqs = wpa_s->manual_scan_freqs;
1177 wpa_s->manual_scan_freqs = NULL;
1178 }
1179
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001180 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1181 wpa_dbg(wpa_s, MSG_DEBUG,
1182 "Limit select_network scan to specified channels");
1183 params.freqs = wpa_s->select_network_scan_freqs;
1184 wpa_s->select_network_scan_freqs = NULL;
1185 }
1186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1188 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1189 "generated frequency list");
1190 params.freqs = wpa_s->next_scan_freqs;
1191 } else
1192 os_free(wpa_s->next_scan_freqs);
1193 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001194 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001196 /* See if user specified frequencies. If so, scan only those. */
1197 if (wpa_s->conf->freq_list && !params.freqs) {
1198 wpa_dbg(wpa_s, MSG_DEBUG,
1199 "Optimize scan based on conf->freq_list");
1200 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1201 }
1202
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001203 /* Use current associated channel? */
1204 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001205 unsigned int num = wpa_s->num_multichan_concurrent;
1206
1207 params.freqs = os_calloc(num + 1, sizeof(int));
1208 if (params.freqs) {
1209 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1210 if (num > 0) {
1211 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1212 "current operating channels since "
1213 "scan_cur_freq is enabled");
1214 } else {
1215 os_free(params.freqs);
1216 params.freqs = NULL;
1217 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001218 }
1219 }
1220
Hai Shalomce48b4a2018-09-05 11:41:35 -07001221#ifdef CONFIG_MBO
1222 if (wpa_s->enable_oce & OCE_STA)
1223 params.oce_scan = 1;
1224#endif /* CONFIG_MBO */
1225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1227 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228 if (extra_ie) {
1229 params.extra_ies = wpabuf_head(extra_ie);
1230 params.extra_ies_len = wpabuf_len(extra_ie);
1231 }
1232
1233#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001234 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07001235 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001236 /*
1237 * The interface may not yet be in P2P mode, so we have to
1238 * explicitly request P2P probe to disable CCK rates.
1239 */
1240 params.p2p_probe = 1;
1241 }
1242#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001244 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001245 wpa_s->wpa_state <= WPA_SCANNING)
1246 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001247
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001248 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1249 struct wpa_bss *bss;
1250
1251 params.bssid = wpa_s->next_scan_bssid;
1252 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1253 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1254 params.ssids[0].ssid_len == 0) {
1255 params.ssids[0].ssid = bss->ssid;
1256 params.ssids[0].ssid_len = bss->ssid_len;
1257 wpa_dbg(wpa_s, MSG_DEBUG,
1258 "Scan a previously specified BSSID " MACSTR
1259 " and SSID %s",
1260 MAC2STR(params.bssid),
1261 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1262 } else {
1263 wpa_dbg(wpa_s, MSG_DEBUG,
1264 "Scan a previously specified BSSID " MACSTR,
1265 MAC2STR(params.bssid));
1266 }
1267 }
1268
Dmitry Shmidt04949592012-07-19 12:16:46 -07001269 scan_params = &params;
1270
1271scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001272#ifdef CONFIG_P2P
1273 /*
1274 * If the driver does not support multi-channel concurrency and a
1275 * virtual interface that shares the same radio with the wpa_s interface
1276 * is operating there may not be need to scan other channels apart from
1277 * the current operating channel on the other virtual interface. Filter
1278 * out other channels in case we are trying to find a connection for a
1279 * station interface when we are not configured to prefer station
1280 * connection and a concurrent operation is already in process.
1281 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001282 if (wpa_s->scan_for_connection &&
1283 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001284 !scan_params->freqs && !params.freqs &&
1285 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001286 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1287 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001288 unsigned int num = wpa_s->num_multichan_concurrent;
1289
1290 params.freqs = os_calloc(num + 1, sizeof(int));
1291 if (params.freqs) {
1292 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1293 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1294 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1295 } else {
1296 os_free(params.freqs);
1297 params.freqs = NULL;
1298 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001299 }
1300 }
1301#endif /* CONFIG_P2P */
1302
Dmitry Shmidt04949592012-07-19 12:16:46 -07001303 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001305 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1306 !wpa_s->manual_scan_freqs) {
1307 /* Restore manual_scan_freqs for the next attempt */
1308 wpa_s->manual_scan_freqs = params.freqs;
1309 params.freqs = NULL;
1310 }
1311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001312 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 os_free(params.freqs);
1314 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001315 os_free(params.mac_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316
1317 if (ret) {
1318 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001319 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1320 wpa_supplicant_set_state(wpa_s,
1321 wpa_s->scan_prev_wpa_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001322 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001323 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001325 } else {
1326 wpa_s->scan_for_connection = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001327#ifdef CONFIG_INTERWORKING
1328 wpa_s->interworking_fast_assoc_tried = 0;
1329#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001330 if (params.bssid)
1331 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 }
1333}
1334
1335
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001336void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1337{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001338 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001339 int cancelled;
1340
1341 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1342 &remaining);
1343
1344 new_int.sec = sec;
1345 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001346 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001347 new_int.sec = remaining.sec;
1348 new_int.usec = remaining.usec;
1349 }
1350
Dmitry Shmidt051af732013-10-22 13:52:46 -07001351 if (cancelled) {
1352 eloop_register_timeout(new_int.sec, new_int.usec,
1353 wpa_supplicant_scan, wpa_s, NULL);
1354 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001355 wpa_s->scan_interval = sec;
1356}
1357
1358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359/**
1360 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1361 * @wpa_s: Pointer to wpa_supplicant data
1362 * @sec: Number of seconds after which to scan
1363 * @usec: Number of microseconds after which to scan
1364 *
1365 * This function is used to schedule a scan for neighboring access points after
1366 * the specified time.
1367 */
1368void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1369{
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001370 int res;
1371
1372 if (wpa_s->p2p_mgmt) {
1373 wpa_dbg(wpa_s, MSG_DEBUG,
1374 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1375 sec, usec);
1376 return;
1377 }
1378
1379 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1380 NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001381 if (res == 1) {
1382 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001383 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001384 } else if (res == 0) {
1385 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1386 sec, usec);
1387 } else {
1388 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1389 sec, usec);
1390 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392}
1393
1394
1395/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001396 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1397 * @wpa_s: Pointer to wpa_supplicant data
1398 * @sec: Number of seconds after which to scan
1399 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001400 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001401 *
1402 * This function is used to schedule periodic scans for neighboring
1403 * access points after the specified time.
1404 */
1405int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1406 int sec, int usec)
1407{
1408 if (!wpa_s->sched_scan_supported)
1409 return -1;
1410
1411 eloop_register_timeout(sec, usec,
1412 wpa_supplicant_delayed_sched_scan_timeout,
1413 wpa_s, NULL);
1414
1415 return 0;
1416}
1417
1418
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001419static void
1420wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1421 struct wpa_driver_scan_params *params)
1422{
1423 if (wpa_s->wpa_state != WPA_COMPLETED ||
1424 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1425 wpa_s->srp.relative_rssi_set == 0)
1426 return;
1427
1428 params->relative_rssi_set = 1;
1429 params->relative_rssi = wpa_s->srp.relative_rssi;
1430
1431 if (wpa_s->srp.relative_adjust_rssi == 0)
1432 return;
1433
1434 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1435 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1436}
1437
1438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001439/**
1440 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1441 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001442 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001443 *
1444 * This function is used to schedule periodic scans for neighboring
1445 * access points repeating the scan continuously.
1446 */
1447int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1448{
1449 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001450 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001451 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001452 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001453 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001454 int ret;
1455 unsigned int max_sched_scan_ssids;
1456 int wildcard = 0;
1457 int need_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001458 struct sched_scan_plan scan_plan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001459
1460 if (!wpa_s->sched_scan_supported)
1461 return -1;
1462
1463 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1464 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1465 else
1466 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001467 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001468 return -1;
1469
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001470 wpa_s->sched_scan_stop_req = 0;
1471
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001472 if (wpa_s->sched_scanning) {
1473 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1474 return 0;
1475 }
1476
1477 need_ssids = 0;
1478 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001479 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001480 /* Use wildcard SSID to find this network */
1481 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001482 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1483 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001484 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001485
1486#ifdef CONFIG_WPS
1487 if (!wpas_network_disabled(wpa_s, ssid) &&
1488 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1489 /*
1490 * Normal scan is more reliable and faster for WPS
1491 * operations and since these are for short periods of
1492 * time, the benefit of trying to use sched_scan would
1493 * be limited.
1494 */
1495 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1496 "sched_scan for WPS");
1497 return -1;
1498 }
1499#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001500 }
1501 if (wildcard)
1502 need_ssids++;
1503
1504 if (wpa_s->normal_scans < 3 &&
1505 (need_ssids <= wpa_s->max_scan_ssids ||
1506 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1507 /*
1508 * When normal scan can speed up operations, use that for the
1509 * first operations before starting the sched_scan to allow
1510 * user space sleep more. We do this only if the normal scan
1511 * has functionality that is suitable for this or if the
1512 * sched_scan does not have better support for multiple SSIDs.
1513 */
1514 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1515 "sched_scan for initial scans (normal_scans=%d)",
1516 wpa_s->normal_scans);
1517 return -1;
1518 }
1519
1520 os_memset(&params, 0, sizeof(params));
1521
1522 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001523 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001524 sizeof(struct wpa_driver_scan_filter));
1525
1526 prev_state = wpa_s->wpa_state;
1527 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1528 wpa_s->wpa_state == WPA_INACTIVE)
1529 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1530
Dmitry Shmidt04949592012-07-19 12:16:46 -07001531 if (wpa_s->autoscan_params != NULL) {
1532 scan_params = wpa_s->autoscan_params;
1533 goto scan;
1534 }
1535
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001536 /* Find the starting point from which to continue scanning */
1537 ssid = wpa_s->conf->ssid;
1538 if (wpa_s->prev_sched_ssid) {
1539 while (ssid) {
1540 if (ssid == wpa_s->prev_sched_ssid) {
1541 ssid = ssid->next;
1542 break;
1543 }
1544 ssid = ssid->next;
1545 }
1546 }
1547
1548 if (!ssid || !wpa_s->prev_sched_ssid) {
1549 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001550 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1551 wpa_s->first_sched_scan = 1;
1552 ssid = wpa_s->conf->ssid;
1553 wpa_s->prev_sched_ssid = ssid;
1554 }
1555
1556 if (wildcard) {
1557 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1558 params.num_ssids++;
1559 }
1560
1561 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001562 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001563 goto next;
1564
1565 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1566 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1567 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1568 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1569 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1570 ssid->ssid, ssid->ssid_len);
1571 params.filter_ssids[params.num_filter_ssids].ssid_len =
1572 ssid->ssid_len;
1573 params.num_filter_ssids++;
1574 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1575 {
1576 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1577 "filter for sched_scan - drop filter");
1578 os_free(params.filter_ssids);
1579 params.filter_ssids = NULL;
1580 params.num_filter_ssids = 0;
1581 }
1582
1583 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1584 if (params.num_ssids == max_sched_scan_ssids)
1585 break; /* only room for broadcast SSID */
1586 wpa_dbg(wpa_s, MSG_DEBUG,
1587 "add to active scan ssid: %s",
1588 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1589 params.ssids[params.num_ssids].ssid =
1590 ssid->ssid;
1591 params.ssids[params.num_ssids].ssid_len =
1592 ssid->ssid_len;
1593 params.num_ssids++;
1594 if (params.num_ssids >= max_sched_scan_ssids) {
1595 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001596 do {
1597 ssid = ssid->next;
1598 } while (ssid &&
1599 (wpas_network_disabled(wpa_s, ssid) ||
1600 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001601 break;
1602 }
1603 }
1604
1605 next:
1606 wpa_s->prev_sched_ssid = ssid;
1607 ssid = ssid->next;
1608 }
1609
1610 if (params.num_filter_ssids == 0) {
1611 os_free(params.filter_ssids);
1612 params.filter_ssids = NULL;
1613 }
1614
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001615 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1616 if (extra_ie) {
1617 params.extra_ies = wpabuf_head(extra_ie);
1618 params.extra_ies_len = wpabuf_len(extra_ie);
1619 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001620
Dmitry Shmidt18463232014-01-24 12:29:41 -08001621 if (wpa_s->conf->filter_rssi)
1622 params.filter_rssi = wpa_s->conf->filter_rssi;
1623
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001624 /* See if user specified frequencies. If so, scan only those. */
1625 if (wpa_s->conf->freq_list && !params.freqs) {
1626 wpa_dbg(wpa_s, MSG_DEBUG,
1627 "Optimize scan based on conf->freq_list");
1628 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1629 }
1630
Hai Shalomce48b4a2018-09-05 11:41:35 -07001631#ifdef CONFIG_MBO
1632 if (wpa_s->enable_oce & OCE_STA)
1633 params.oce_scan = 1;
1634#endif /* CONFIG_MBO */
1635
Dmitry Shmidt04949592012-07-19 12:16:46 -07001636 scan_params = &params;
1637
1638scan:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001639 wpa_s->sched_scan_timed_out = 0;
1640
1641 /*
1642 * We cannot support multiple scan plans if the scan request includes
1643 * too many SSID's, so in this case use only the last scan plan and make
1644 * it run infinitely. It will be stopped by the timeout.
1645 */
1646 if (wpa_s->sched_scan_plans_num == 1 ||
1647 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1648 params.sched_scan_plans = wpa_s->sched_scan_plans;
1649 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1650 } else if (wpa_s->sched_scan_plans_num > 1) {
1651 wpa_dbg(wpa_s, MSG_DEBUG,
1652 "Too many SSIDs. Default to using single scheduled_scan plan");
1653 params.sched_scan_plans =
1654 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1655 1];
1656 params.sched_scan_plans_num = 1;
1657 } else {
1658 if (wpa_s->conf->sched_scan_interval)
1659 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1660 else
1661 scan_plan.interval = 10;
1662
1663 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1664 wpa_printf(MSG_WARNING,
1665 "Scan interval too long(%u), use the maximum allowed(%u)",
1666 scan_plan.interval,
1667 wpa_s->max_sched_scan_plan_interval);
1668 scan_plan.interval =
1669 wpa_s->max_sched_scan_plan_interval;
1670 }
1671
1672 scan_plan.iterations = 0;
1673 params.sched_scan_plans = &scan_plan;
1674 params.sched_scan_plans_num = 1;
1675 }
1676
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001677 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001679 if (ssid || !wpa_s->first_sched_scan) {
1680 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001681 "Starting sched scan after %u seconds: interval %u timeout %d",
1682 params.sched_scan_start_delay,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001683 params.sched_scan_plans[0].interval,
1684 wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001685 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001686 wpa_dbg(wpa_s, MSG_DEBUG,
1687 "Starting sched scan after %u seconds (no timeout)",
1688 params.sched_scan_start_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001689 }
1690
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001691 wpa_setband_scan_freqs(wpa_s, scan_params);
1692
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001693 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
Hai Shalomc3565922019-10-28 11:58:20 -07001694 wpa_s->wpa_state <= WPA_SCANNING)
1695 wpa_setup_mac_addr_rand_params(&params,
1696 wpa_s->mac_addr_sched_scan);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001697
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001698 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1699
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001700 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001701 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001702 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07001703 os_free(params.mac_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001704 if (ret) {
1705 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1706 if (prev_state != wpa_s->wpa_state)
1707 wpa_supplicant_set_state(wpa_s, prev_state);
1708 return ret;
1709 }
1710
1711 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1712 if (ssid || !wpa_s->first_sched_scan) {
1713 wpa_s->sched_scan_timed_out = 0;
1714 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1715 wpa_supplicant_sched_scan_timeout,
1716 wpa_s, NULL);
1717 wpa_s->first_sched_scan = 0;
1718 wpa_s->sched_scan_timeout /= 2;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001719 params.sched_scan_plans[0].interval *= 2;
1720 if ((unsigned int) wpa_s->sched_scan_timeout <
1721 params.sched_scan_plans[0].interval ||
1722 params.sched_scan_plans[0].interval >
1723 wpa_s->max_sched_scan_plan_interval) {
1724 params.sched_scan_plans[0].interval = 10;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001725 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1726 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001727 }
1728
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001729 /* If there is no more ssids, start next time from the beginning */
1730 if (!ssid)
1731 wpa_s->prev_sched_ssid = NULL;
1732
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001733 return 0;
1734}
1735
1736
1737/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1739 * @wpa_s: Pointer to wpa_supplicant data
1740 *
1741 * This function is used to cancel a scan request scheduled with
1742 * wpa_supplicant_req_scan().
1743 */
1744void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1745{
1746 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1747 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748}
1749
1750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001751/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001752 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1753 * @wpa_s: Pointer to wpa_supplicant data
1754 *
1755 * This function is used to stop a delayed scheduled scan.
1756 */
1757void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1758{
1759 if (!wpa_s->sched_scan_supported)
1760 return;
1761
1762 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1763 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1764 wpa_s, NULL);
1765}
1766
1767
1768/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001769 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1770 * @wpa_s: Pointer to wpa_supplicant data
1771 *
1772 * This function is used to stop a periodic scheduled scan.
1773 */
1774void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1775{
1776 if (!wpa_s->sched_scanning)
1777 return;
1778
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001779 if (wpa_s->sched_scanning)
1780 wpa_s->sched_scan_stop_req = 1;
1781
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001782 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1783 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1784 wpa_supplicant_stop_sched_scan(wpa_s);
1785}
1786
1787
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001788/**
1789 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1790 * @wpa_s: Pointer to wpa_supplicant data
1791 * @scanning: Whether scanning is currently in progress
1792 *
1793 * This function is to generate scanning notifycations. It is called whenever
1794 * there may have been a change in scanning (scan started, completed, stopped).
1795 * wpas_notify_scanning() is called whenever the scanning state changed from the
1796 * previously notified state.
1797 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1799 int scanning)
1800{
1801 if (wpa_s->scanning != scanning) {
1802 wpa_s->scanning = scanning;
1803 wpas_notify_scanning(wpa_s);
1804 }
1805}
1806
1807
1808static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1809{
1810 int rate = 0;
1811 const u8 *ie;
1812 int i;
1813
1814 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1815 for (i = 0; ie && i < ie[1]; i++) {
1816 if ((ie[i + 2] & 0x7f) > rate)
1817 rate = ie[i + 2] & 0x7f;
1818 }
1819
1820 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1821 for (i = 0; ie && i < ie[1]; i++) {
1822 if ((ie[i + 2] & 0x7f) > rate)
1823 rate = ie[i + 2] & 0x7f;
1824 }
1825
1826 return rate;
1827}
1828
1829
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001830/**
1831 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1832 * @res: Scan result entry
1833 * @ie: Information element identitifier (WLAN_EID_*)
1834 * Returns: Pointer to the information element (id field) or %NULL if not found
1835 *
1836 * This function returns the first matching information element in the scan
1837 * result.
1838 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1840{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001841 size_t ie_len = res->ie_len;
1842
1843 /* Use the Beacon frame IEs if res->ie_len is not available */
1844 if (!ie_len)
1845 ie_len = res->beacon_ie_len;
1846
1847 return get_ie((const u8 *) (res + 1), ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848}
1849
1850
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001851/**
1852 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1853 * @res: Scan result entry
1854 * @vendor_type: Vendor type (four octets starting the IE payload)
1855 * Returns: Pointer to the information element (id field) or %NULL if not found
1856 *
1857 * This function returns the first matching information element in the scan
1858 * result.
1859 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1861 u32 vendor_type)
1862{
1863 const u8 *end, *pos;
1864
1865 pos = (const u8 *) (res + 1);
1866 end = pos + res->ie_len;
1867
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001868 while (end - pos > 1) {
1869 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 break;
1871 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1872 vendor_type == WPA_GET_BE32(&pos[2]))
1873 return pos;
1874 pos += 2 + pos[1];
1875 }
1876
1877 return NULL;
1878}
1879
1880
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001881/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001882 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1883 * @res: Scan result entry
1884 * @vendor_type: Vendor type (four octets starting the IE payload)
1885 * Returns: Pointer to the information element (id field) or %NULL if not found
1886 *
1887 * This function returns the first matching information element in the scan
1888 * result.
1889 *
1890 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1891 * from Beacon frames instead of either Beacon or Probe Response frames.
1892 */
1893const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1894 u32 vendor_type)
1895{
1896 const u8 *end, *pos;
1897
1898 if (res->beacon_ie_len == 0)
1899 return NULL;
1900
1901 pos = (const u8 *) (res + 1);
1902 pos += res->ie_len;
1903 end = pos + res->beacon_ie_len;
1904
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001905 while (end - pos > 1) {
1906 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001907 break;
1908 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1909 vendor_type == WPA_GET_BE32(&pos[2]))
1910 return pos;
1911 pos += 2 + pos[1];
1912 }
1913
1914 return NULL;
1915}
1916
1917
1918/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001919 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1920 * @res: Scan result entry
1921 * @vendor_type: Vendor type (four octets starting the IE payload)
1922 * Returns: Pointer to the information element payload or %NULL if not found
1923 *
1924 * This function returns concatenated payload of possibly fragmented vendor
1925 * specific information elements in the scan result. The caller is responsible
1926 * for freeing the returned buffer.
1927 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1929 u32 vendor_type)
1930{
1931 struct wpabuf *buf;
1932 const u8 *end, *pos;
1933
1934 buf = wpabuf_alloc(res->ie_len);
1935 if (buf == NULL)
1936 return NULL;
1937
1938 pos = (const u8 *) (res + 1);
1939 end = pos + res->ie_len;
1940
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001941 while (end - pos > 1) {
1942 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943 break;
1944 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1945 vendor_type == WPA_GET_BE32(&pos[2]))
1946 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1947 pos += 2 + pos[1];
1948 }
1949
1950 if (wpabuf_len(buf) == 0) {
1951 wpabuf_free(buf);
1952 buf = NULL;
1953 }
1954
1955 return buf;
1956}
1957
1958
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001959/*
1960 * Channels with a great SNR can operate at full rate. What is a great SNR?
1961 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1962 * rule of thumb is that any SNR above 20 is good." This one
1963 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001964 * recommends 25 as a minimum SNR for 54 Mbps data rate. The estimates used in
1965 * scan_est_throughput() allow even smaller SNR values for the maximum rates
1966 * (21 for 54 Mbps, 22 for VHT80 MCS9, 24 for HT40 and HT20 MCS7). Use 25 as a
1967 * somewhat conservative value here.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001968 */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001969#define GREAT_SNR 25
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001971#define IS_5GHZ(n) (n > 4000)
1972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973/* Compare function for sorting scan results. Return >0 if @b is considered
1974 * better. */
1975static int wpa_scan_result_compar(const void *a, const void *b)
1976{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001977#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 struct wpa_scan_res **_wa = (void *) a;
1979 struct wpa_scan_res **_wb = (void *) b;
1980 struct wpa_scan_res *wa = *_wa;
1981 struct wpa_scan_res *wb = *_wb;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001982 int wpa_a, wpa_b;
1983 int snr_a, snr_b, snr_a_full, snr_b_full;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984
1985 /* WPA/WPA2 support preferred */
1986 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1987 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1988 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1989 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1990
1991 if (wpa_b && !wpa_a)
1992 return 1;
1993 if (!wpa_b && wpa_a)
1994 return -1;
1995
1996 /* privacy support preferred */
1997 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1998 (wb->caps & IEEE80211_CAP_PRIVACY))
1999 return 1;
2000 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2001 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2002 return -1;
2003
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002004 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002005 snr_a_full = wa->snr;
2006 snr_a = MIN(wa->snr, GREAT_SNR);
2007 snr_b_full = wb->snr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002008 snr_b = MIN(wb->snr, GREAT_SNR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002009 } else {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002010 /* Level is not in dBm, so we can't calculate
2011 * SNR. Just use raw level (units unknown). */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002012 snr_a = snr_a_full = wa->level;
2013 snr_b = snr_b_full = wb->level;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002014 }
2015
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002016 /* if SNR is close, decide by max rate or frequency band */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002017 if (snr_a && snr_b && abs(snr_b - snr_a) < 7) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002018 if (wa->est_throughput != wb->est_throughput)
Hai Shalom021b0b52019-04-10 11:17:58 -07002019 return (int) wb->est_throughput -
2020 (int) wa->est_throughput;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002021 }
2022 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2023 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002024 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2025 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026 }
2027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002028 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029 * identical, use quality values since some drivers may only report
2030 * that value and leave the signal level zero */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002031 if (snr_b_full == snr_a_full)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 return wb->qual - wa->qual;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002033 return snr_b_full - snr_a_full;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002034#undef MIN
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035}
2036
2037
2038#ifdef CONFIG_WPS
2039/* Compare function for sorting scan results when searching a WPS AP for
2040 * provisioning. Return >0 if @b is considered better. */
2041static int wpa_scan_result_wps_compar(const void *a, const void *b)
2042{
2043 struct wpa_scan_res **_wa = (void *) a;
2044 struct wpa_scan_res **_wb = (void *) b;
2045 struct wpa_scan_res *wa = *_wa;
2046 struct wpa_scan_res *wb = *_wb;
2047 int uses_wps_a, uses_wps_b;
2048 struct wpabuf *wps_a, *wps_b;
2049 int res;
2050
2051 /* Optimization - check WPS IE existence before allocated memory and
2052 * doing full reassembly. */
2053 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2054 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2055 if (uses_wps_a && !uses_wps_b)
2056 return -1;
2057 if (!uses_wps_a && uses_wps_b)
2058 return 1;
2059
2060 if (uses_wps_a && uses_wps_b) {
2061 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2062 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2063 res = wps_ap_priority_compar(wps_a, wps_b);
2064 wpabuf_free(wps_a);
2065 wpabuf_free(wps_b);
2066 if (res)
2067 return res;
2068 }
2069
2070 /*
2071 * Do not use current AP security policy as a sorting criteria during
2072 * WPS provisioning step since the AP may get reconfigured at the
2073 * completion of provisioning.
2074 */
2075
2076 /* all things being equal, use signal level; if signal levels are
2077 * identical, use quality values since some drivers may only report
2078 * that value and leave the signal level zero */
2079 if (wb->level == wa->level)
2080 return wb->qual - wa->qual;
2081 return wb->level - wa->level;
2082}
2083#endif /* CONFIG_WPS */
2084
2085
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002086static void dump_scan_res(struct wpa_scan_results *scan_res)
2087{
2088#ifndef CONFIG_NO_STDOUT_DEBUG
2089 size_t i;
2090
2091 if (scan_res->res == NULL || scan_res->num == 0)
2092 return;
2093
2094 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2095
2096 for (i = 0; i < scan_res->num; i++) {
2097 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002098 u8 *pos;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002099 if (r->flags & WPA_SCAN_LEVEL_DBM) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002100 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2101
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002103 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002104 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002105 r->noise, noise_valid ? "" : "~", r->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002106 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2107 r->flags,
2108 r->age, r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002109 } else {
2110 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002111 "noise=%d level=%d flags=0x%x age=%u est=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002113 r->noise, r->level, r->flags, r->age,
2114 r->est_throughput);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002115 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002116 pos = (u8 *) (r + 1);
2117 if (r->ie_len)
2118 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2119 pos += r->ie_len;
2120 if (r->beacon_ie_len)
2121 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2122 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002123 }
2124#endif /* CONFIG_NO_STDOUT_DEBUG */
2125}
2126
2127
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002128/**
2129 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2130 * @wpa_s: Pointer to wpa_supplicant data
2131 * @bssid: BSSID to check
2132 * Returns: 0 if the BSSID is filtered or 1 if not
2133 *
2134 * This function is used to filter out specific BSSIDs from scan reslts mainly
2135 * for testing purposes (SET bssid_filter ctrl_iface command).
2136 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002137int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2138 const u8 *bssid)
2139{
2140 size_t i;
2141
2142 if (wpa_s->bssid_filter == NULL)
2143 return 1;
2144
2145 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2146 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2147 ETH_ALEN) == 0)
2148 return 1;
2149 }
2150
2151 return 0;
2152}
2153
2154
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002155void filter_scan_res(struct wpa_supplicant *wpa_s,
2156 struct wpa_scan_results *res)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002157{
2158 size_t i, j;
2159
2160 if (wpa_s->bssid_filter == NULL)
2161 return;
2162
2163 for (i = 0, j = 0; i < res->num; i++) {
2164 if (wpa_supplicant_filter_bssid_match(wpa_s,
2165 res->res[i]->bssid)) {
2166 res->res[j++] = res->res[i];
2167 } else {
2168 os_free(res->res[i]);
2169 res->res[i] = NULL;
2170 }
2171 }
2172
2173 if (res->num != j) {
2174 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2175 (int) (res->num - j));
2176 res->num = j;
2177 }
2178}
2179
2180
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002181/*
2182 * Noise floor values to use when we have signal strength
Dmitry Shmidte4663042016-04-04 10:07:49 -07002183 * measurements, but no noise floor measurements. These values were
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002184 * measured in an office environment with many APs.
2185 */
2186#define DEFAULT_NOISE_FLOOR_2GHZ (-89)
2187#define DEFAULT_NOISE_FLOOR_5GHZ (-92)
2188
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002189void scan_snr(struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002190{
2191 if (res->flags & WPA_SCAN_NOISE_INVALID) {
2192 res->noise = IS_5GHZ(res->freq) ?
2193 DEFAULT_NOISE_FLOOR_5GHZ :
2194 DEFAULT_NOISE_FLOOR_2GHZ;
2195 }
2196
2197 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2198 res->snr = res->level - res->noise;
2199 } else {
2200 /* Level is not in dBm, so we can't calculate
2201 * SNR. Just use raw level (units unknown). */
2202 res->snr = res->level;
2203 }
2204}
2205
2206
2207static unsigned int max_ht20_rate(int snr)
2208{
2209 if (snr < 6)
2210 return 6500; /* HT20 MCS0 */
2211 if (snr < 8)
2212 return 13000; /* HT20 MCS1 */
2213 if (snr < 13)
2214 return 19500; /* HT20 MCS2 */
2215 if (snr < 17)
2216 return 26000; /* HT20 MCS3 */
2217 if (snr < 20)
2218 return 39000; /* HT20 MCS4 */
2219 if (snr < 23)
2220 return 52000; /* HT20 MCS5 */
2221 if (snr < 24)
2222 return 58500; /* HT20 MCS6 */
2223 return 65000; /* HT20 MCS7 */
2224}
2225
2226
2227static unsigned int max_ht40_rate(int snr)
2228{
2229 if (snr < 3)
2230 return 13500; /* HT40 MCS0 */
2231 if (snr < 6)
2232 return 27000; /* HT40 MCS1 */
2233 if (snr < 10)
2234 return 40500; /* HT40 MCS2 */
2235 if (snr < 15)
2236 return 54000; /* HT40 MCS3 */
2237 if (snr < 17)
2238 return 81000; /* HT40 MCS4 */
2239 if (snr < 22)
2240 return 108000; /* HT40 MCS5 */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002241 if (snr < 24)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002242 return 121500; /* HT40 MCS6 */
2243 return 135000; /* HT40 MCS7 */
2244}
2245
2246
2247static unsigned int max_vht80_rate(int snr)
2248{
2249 if (snr < 1)
2250 return 0;
2251 if (snr < 2)
2252 return 29300; /* VHT80 MCS0 */
2253 if (snr < 5)
2254 return 58500; /* VHT80 MCS1 */
2255 if (snr < 9)
2256 return 87800; /* VHT80 MCS2 */
2257 if (snr < 11)
2258 return 117000; /* VHT80 MCS3 */
2259 if (snr < 15)
2260 return 175500; /* VHT80 MCS4 */
2261 if (snr < 16)
2262 return 234000; /* VHT80 MCS5 */
2263 if (snr < 18)
2264 return 263300; /* VHT80 MCS6 */
2265 if (snr < 20)
2266 return 292500; /* VHT80 MCS7 */
2267 if (snr < 22)
2268 return 351000; /* VHT80 MCS8 */
2269 return 390000; /* VHT80 MCS9 */
2270}
2271
2272
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002273void scan_est_throughput(struct wpa_supplicant *wpa_s,
2274 struct wpa_scan_res *res)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002275{
2276 enum local_hw_capab capab = wpa_s->hw_capab;
2277 int rate; /* max legacy rate in 500 kb/s units */
2278 const u8 *ie;
2279 unsigned int est, tmp;
2280 int snr = res->snr;
2281
2282 if (res->est_throughput)
2283 return;
2284
2285 /* Get maximum legacy rate */
2286 rate = wpa_scan_get_max_rate(res);
2287
2288 /* Limit based on estimated SNR */
2289 if (rate > 1 * 2 && snr < 1)
2290 rate = 1 * 2;
2291 else if (rate > 2 * 2 && snr < 4)
2292 rate = 2 * 2;
2293 else if (rate > 6 * 2 && snr < 5)
2294 rate = 6 * 2;
2295 else if (rate > 9 * 2 && snr < 6)
2296 rate = 9 * 2;
2297 else if (rate > 12 * 2 && snr < 7)
2298 rate = 12 * 2;
2299 else if (rate > 18 * 2 && snr < 10)
2300 rate = 18 * 2;
2301 else if (rate > 24 * 2 && snr < 11)
2302 rate = 24 * 2;
2303 else if (rate > 36 * 2 && snr < 15)
2304 rate = 36 * 2;
2305 else if (rate > 48 * 2 && snr < 19)
2306 rate = 48 * 2;
2307 else if (rate > 54 * 2 && snr < 21)
2308 rate = 54 * 2;
2309 est = rate * 500;
2310
2311 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2312 ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP);
2313 if (ie) {
2314 tmp = max_ht20_rate(snr);
2315 if (tmp > est)
2316 est = tmp;
2317 }
2318 }
2319
2320 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
2321 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2322 if (ie && ie[1] >= 2 &&
2323 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2324 tmp = max_ht40_rate(snr);
2325 if (tmp > est)
2326 est = tmp;
2327 }
2328 }
2329
2330 if (capab == CAPAB_VHT) {
2331 /* Use +1 to assume VHT is always faster than HT */
2332 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP);
2333 if (ie) {
2334 tmp = max_ht20_rate(snr) + 1;
2335 if (tmp > est)
2336 est = tmp;
2337
2338 ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION);
2339 if (ie && ie[1] >= 2 &&
2340 (ie[3] &
2341 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2342 tmp = max_ht40_rate(snr) + 1;
2343 if (tmp > est)
2344 est = tmp;
2345 }
2346
2347 ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION);
2348 if (ie && ie[1] >= 1 &&
2349 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2350 tmp = max_vht80_rate(snr) + 1;
2351 if (tmp > est)
2352 est = tmp;
2353 }
2354 }
2355 }
2356
2357 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
2358
2359 res->est_throughput = est;
2360}
2361
2362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363/**
2364 * wpa_supplicant_get_scan_results - Get scan results
2365 * @wpa_s: Pointer to wpa_supplicant data
2366 * @info: Information about what was scanned or %NULL if not available
2367 * @new_scan: Whether a new scan was performed
2368 * Returns: Scan results, %NULL on failure
2369 *
2370 * This function request the current scan results from the driver and updates
2371 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2372 * results with wpa_scan_results_free().
2373 */
2374struct wpa_scan_results *
2375wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2376 struct scan_info *info, int new_scan)
2377{
2378 struct wpa_scan_results *scan_res;
2379 size_t i;
2380 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
2381
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002382 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 if (scan_res == NULL) {
2384 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
2385 return NULL;
2386 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002387 if (scan_res->fetch_time.sec == 0) {
2388 /*
2389 * Make sure we have a valid timestamp if the driver wrapper
2390 * does not set this.
2391 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002392 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002393 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002394 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002396 for (i = 0; i < scan_res->num; i++) {
2397 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2398
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002399 scan_snr(scan_res_item);
2400 scan_est_throughput(wpa_s, scan_res_item);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002401 }
2402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002404 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2406 "provisioning rules");
2407 compar = wpa_scan_result_wps_compar;
2408 }
2409#endif /* CONFIG_WPS */
2410
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002411 if (scan_res->res) {
2412 qsort(scan_res->res, scan_res->num,
2413 sizeof(struct wpa_scan_res *), compar);
2414 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002415 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416
Dmitry Shmidt29333592017-01-09 12:27:11 -08002417 if (wpa_s->ignore_post_flush_scan_res) {
2418 /* FLUSH command aborted an ongoing scan and these are the
2419 * results from the aborted scan. Do not process the results to
2420 * maintain flushed state. */
2421 wpa_dbg(wpa_s, MSG_DEBUG,
2422 "Do not update BSS table based on pending post-FLUSH scan results");
2423 wpa_s->ignore_post_flush_scan_res = 0;
2424 return scan_res;
2425 }
2426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 wpa_bss_update_start(wpa_s);
2428 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002429 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2430 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002431 wpa_bss_update_end(wpa_s, info, new_scan);
2432
2433 return scan_res;
2434}
2435
2436
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002437/**
2438 * wpa_supplicant_update_scan_results - Update scan results from the driver
2439 * @wpa_s: Pointer to wpa_supplicant data
2440 * Returns: 0 on success, -1 on failure
2441 *
2442 * This function updates the BSS table within wpa_supplicant based on the
2443 * currently available scan results from the driver without requesting a new
2444 * scan. This is used in cases where the driver indicates an association
2445 * (including roaming within ESS) and wpa_supplicant does not yet have the
2446 * needed information to complete the connection (e.g., to perform validation
2447 * steps in 4-way handshake).
2448 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2450{
2451 struct wpa_scan_results *scan_res;
2452 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2453 if (scan_res == NULL)
2454 return -1;
2455 wpa_scan_results_free(scan_res);
2456
2457 return 0;
2458}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002459
2460
2461/**
2462 * scan_only_handler - Reports scan results
2463 */
2464void scan_only_handler(struct wpa_supplicant *wpa_s,
2465 struct wpa_scan_results *scan_res)
2466{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002467 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002468 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2469 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2470 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2471 wpa_s->manual_scan_id);
2472 wpa_s->manual_scan_use_id = 0;
2473 } else {
2474 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2475 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002476 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002477 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002478 if (wpa_s->scan_work) {
2479 struct wpa_radio_work *work = wpa_s->scan_work;
2480 wpa_s->scan_work = NULL;
2481 radio_work_done(work);
2482 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002483
2484 if (wpa_s->wpa_state == WPA_SCANNING)
2485 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08002486}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002487
2488
2489int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2490{
2491 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2492}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002493
2494
2495struct wpa_driver_scan_params *
2496wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2497{
2498 struct wpa_driver_scan_params *params;
2499 size_t i;
2500 u8 *n;
2501
2502 params = os_zalloc(sizeof(*params));
2503 if (params == NULL)
2504 return NULL;
2505
2506 for (i = 0; i < src->num_ssids; i++) {
2507 if (src->ssids[i].ssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002508 n = os_memdup(src->ssids[i].ssid,
2509 src->ssids[i].ssid_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002510 if (n == NULL)
2511 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002512 params->ssids[i].ssid = n;
2513 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2514 }
2515 }
2516 params->num_ssids = src->num_ssids;
2517
2518 if (src->extra_ies) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002519 n = os_memdup(src->extra_ies, src->extra_ies_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002520 if (n == NULL)
2521 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002522 params->extra_ies = n;
2523 params->extra_ies_len = src->extra_ies_len;
2524 }
2525
2526 if (src->freqs) {
2527 int len = int_array_len(src->freqs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002528 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002529 if (params->freqs == NULL)
2530 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002531 }
2532
2533 if (src->filter_ssids) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002534 params->filter_ssids = os_memdup(src->filter_ssids,
2535 sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002536 src->num_filter_ssids);
2537 if (params->filter_ssids == NULL)
2538 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002539 params->num_filter_ssids = src->num_filter_ssids;
2540 }
2541
2542 params->filter_rssi = src->filter_rssi;
2543 params->p2p_probe = src->p2p_probe;
2544 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002545 params->low_priority = src->low_priority;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002546 params->duration = src->duration;
2547 params->duration_mandatory = src->duration_mandatory;
Hai Shalomce48b4a2018-09-05 11:41:35 -07002548 params->oce_scan = src->oce_scan;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002549
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002550 if (src->sched_scan_plans_num > 0) {
2551 params->sched_scan_plans =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002552 os_memdup(src->sched_scan_plans,
2553 sizeof(*src->sched_scan_plans) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002554 src->sched_scan_plans_num);
2555 if (!params->sched_scan_plans)
2556 goto failed;
2557
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002558 params->sched_scan_plans_num = src->sched_scan_plans_num;
2559 }
2560
Hai Shalomc3565922019-10-28 11:58:20 -07002561 if (src->mac_addr_rand &&
2562 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
2563 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002564
2565 if (src->bssid) {
2566 u8 *bssid;
2567
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002568 bssid = os_memdup(src->bssid, ETH_ALEN);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002569 if (!bssid)
2570 goto failed;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002571 params->bssid = bssid;
2572 }
2573
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002574 params->relative_rssi_set = src->relative_rssi_set;
2575 params->relative_rssi = src->relative_rssi;
2576 params->relative_adjust_band = src->relative_adjust_band;
2577 params->relative_adjust_rssi = src->relative_adjust_rssi;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002578 return params;
2579
2580failed:
2581 wpa_scan_free_params(params);
2582 return NULL;
2583}
2584
2585
2586void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2587{
2588 size_t i;
2589
2590 if (params == NULL)
2591 return;
2592
2593 for (i = 0; i < params->num_ssids; i++)
2594 os_free((u8 *) params->ssids[i].ssid);
2595 os_free((u8 *) params->extra_ies);
2596 os_free(params->freqs);
2597 os_free(params->filter_ssids);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002598 os_free(params->sched_scan_plans);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002599
2600 /*
2601 * Note: params->mac_addr_mask points to same memory allocation and
2602 * must not be freed separately.
2603 */
2604 os_free((u8 *) params->mac_addr);
2605
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002606 os_free((u8 *) params->bssid);
2607
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002608 os_free(params);
2609}
Dmitry Shmidt98660862014-03-11 17:26:21 -07002610
2611
2612int wpas_start_pno(struct wpa_supplicant *wpa_s)
2613{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002614 int ret, prio;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002615 size_t i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002616 struct wpa_ssid *ssid;
2617 struct wpa_driver_scan_params params;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002618 struct sched_scan_plan scan_plan;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002619 unsigned int max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002620
2621 if (!wpa_s->sched_scan_supported)
2622 return -1;
2623
Dmitry Shmidte4663042016-04-04 10:07:49 -07002624 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2625 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2626 else
2627 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2628 if (max_sched_scan_ssids < 1)
2629 return -1;
2630
Dmitry Shmidt98660862014-03-11 17:26:21 -07002631 if (wpa_s->pno || wpa_s->pno_sched_pending)
2632 return 0;
2633
2634 if ((wpa_s->wpa_state > WPA_SCANNING) &&
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002635 (wpa_s->wpa_state < WPA_COMPLETED)) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002636 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2637 return -EAGAIN;
2638 }
2639
2640 if (wpa_s->wpa_state == WPA_SCANNING) {
2641 wpa_supplicant_cancel_scan(wpa_s);
2642 if (wpa_s->sched_scanning) {
2643 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2644 "ongoing sched scan");
2645 wpa_supplicant_cancel_sched_scan(wpa_s);
2646 wpa_s->pno_sched_pending = 1;
2647 return 0;
2648 }
2649 }
2650
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002651 if (wpa_s->sched_scan_stop_req) {
2652 wpa_printf(MSG_DEBUG,
2653 "Schedule PNO after previous sched scan has stopped");
2654 wpa_s->pno_sched_pending = 1;
2655 return 0;
2656 }
2657
Dmitry Shmidt98660862014-03-11 17:26:21 -07002658 os_memset(&params, 0, sizeof(params));
2659
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002660 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002661 ssid = wpa_s->conf->ssid;
2662 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002663 if (!wpas_network_disabled(wpa_s, ssid)) {
2664 num_match_ssid++;
2665 if (ssid->scan_ssid)
2666 num_ssid++;
2667 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002668 ssid = ssid->next;
2669 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002670
2671 if (num_match_ssid == 0) {
2672 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2673 return -1;
2674 }
2675
2676 if (num_match_ssid > num_ssid) {
2677 params.num_ssids++; /* wildcard */
2678 num_ssid++;
2679 }
2680
Dmitry Shmidte4663042016-04-04 10:07:49 -07002681 if (num_ssid > max_sched_scan_ssids) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07002682 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
Dmitry Shmidte4663042016-04-04 10:07:49 -07002683 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2684 num_ssid = max_sched_scan_ssids;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002685 }
2686
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002687 if (num_match_ssid > wpa_s->max_match_sets) {
2688 num_match_ssid = wpa_s->max_match_sets;
2689 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002690 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002691 params.filter_ssids = os_calloc(num_match_ssid,
2692 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002693 if (params.filter_ssids == NULL)
2694 return -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002695
Dmitry Shmidt98660862014-03-11 17:26:21 -07002696 i = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002697 prio = 0;
2698 ssid = wpa_s->conf->pssid[prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002699 while (ssid) {
2700 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002701 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2702 params.ssids[params.num_ssids].ssid =
2703 ssid->ssid;
2704 params.ssids[params.num_ssids].ssid_len =
2705 ssid->ssid_len;
2706 params.num_ssids++;
2707 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002708 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2709 ssid->ssid_len);
2710 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2711 params.num_filter_ssids++;
2712 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002713 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002714 break;
2715 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002716 if (ssid->pnext)
2717 ssid = ssid->pnext;
2718 else if (prio + 1 == wpa_s->conf->num_prio)
2719 break;
2720 else
2721 ssid = wpa_s->conf->pssid[++prio];
Dmitry Shmidt98660862014-03-11 17:26:21 -07002722 }
2723
2724 if (wpa_s->conf->filter_rssi)
2725 params.filter_rssi = wpa_s->conf->filter_rssi;
2726
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002727 if (wpa_s->sched_scan_plans_num) {
2728 params.sched_scan_plans = wpa_s->sched_scan_plans;
2729 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2730 } else {
2731 /* Set one scan plan that will run infinitely */
2732 if (wpa_s->conf->sched_scan_interval)
2733 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2734 else
2735 scan_plan.interval = 10;
2736
2737 scan_plan.iterations = 0;
2738 params.sched_scan_plans = &scan_plan;
2739 params.sched_scan_plans_num = 1;
2740 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002741
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002742 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
2743
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002744 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2745 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2746 params.freqs = wpa_s->manual_sched_scan_freqs;
2747 }
2748
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002749 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
Hai Shalomc3565922019-10-28 11:58:20 -07002750 wpa_s->wpa_state <= WPA_SCANNING)
2751 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002752
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002753 wpa_scan_set_relative_rssi_params(wpa_s, &params);
2754
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002755 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002756 os_free(params.filter_ssids);
Hai Shalomc3565922019-10-28 11:58:20 -07002757 os_free(params.mac_addr);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002758 if (ret == 0)
2759 wpa_s->pno = 1;
2760 else
2761 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2762 return ret;
2763}
2764
2765
2766int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2767{
2768 int ret = 0;
2769
2770 if (!wpa_s->pno)
2771 return 0;
2772
2773 ret = wpa_supplicant_stop_sched_scan(wpa_s);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002774 wpa_s->sched_scan_stop_req = 1;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002775
2776 wpa_s->pno = 0;
2777 wpa_s->pno_sched_pending = 0;
2778
2779 if (wpa_s->wpa_state == WPA_SCANNING)
2780 wpa_supplicant_req_scan(wpa_s, 0, 0);
2781
2782 return ret;
2783}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002784
2785
2786void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2787 unsigned int type)
2788{
2789 type &= MAC_ADDR_RAND_ALL;
2790 wpa_s->mac_addr_rand_enable &= ~type;
2791
2792 if (type & MAC_ADDR_RAND_SCAN) {
2793 os_free(wpa_s->mac_addr_scan);
2794 wpa_s->mac_addr_scan = NULL;
2795 }
2796
2797 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2798 os_free(wpa_s->mac_addr_sched_scan);
2799 wpa_s->mac_addr_sched_scan = NULL;
2800 }
2801
2802 if (type & MAC_ADDR_RAND_PNO) {
2803 os_free(wpa_s->mac_addr_pno);
2804 wpa_s->mac_addr_pno = NULL;
2805 }
2806}
2807
2808
2809int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2810 unsigned int type, const u8 *addr,
2811 const u8 *mask)
2812{
2813 u8 *tmp = NULL;
2814
Hai Shalom74f70d42019-02-11 14:42:39 -08002815 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
2816 wpa_printf(MSG_INFO,
2817 "scan: MAC randomization type %u != supported=%u",
2818 type, wpa_s->mac_addr_rand_supported);
2819 return -1;
2820 }
2821
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002822 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2823
2824 if (addr) {
2825 tmp = os_malloc(2 * ETH_ALEN);
2826 if (!tmp)
2827 return -1;
2828 os_memcpy(tmp, addr, ETH_ALEN);
2829 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2830 }
2831
2832 if (type == MAC_ADDR_RAND_SCAN) {
2833 wpa_s->mac_addr_scan = tmp;
2834 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2835 wpa_s->mac_addr_sched_scan = tmp;
2836 } else if (type == MAC_ADDR_RAND_PNO) {
2837 wpa_s->mac_addr_pno = tmp;
2838 } else {
2839 wpa_printf(MSG_INFO,
2840 "scan: Invalid MAC randomization type=0x%x",
2841 type);
2842 os_free(tmp);
2843 return -1;
2844 }
2845
2846 wpa_s->mac_addr_rand_enable |= type;
2847 return 0;
2848}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002849
2850
Hai Shalomc3565922019-10-28 11:58:20 -07002851int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
2852 unsigned int type, u8 *mask)
2853{
2854 const u8 *to_copy;
2855
2856 if ((wpa_s->mac_addr_rand_enable & type) != type)
2857 return -1;
2858
2859 if (type == MAC_ADDR_RAND_SCAN) {
2860 to_copy = wpa_s->mac_addr_scan;
2861 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2862 to_copy = wpa_s->mac_addr_sched_scan;
2863 } else if (type == MAC_ADDR_RAND_PNO) {
2864 to_copy = wpa_s->mac_addr_pno;
2865 } else {
2866 wpa_printf(MSG_DEBUG,
2867 "scan: Invalid MAC randomization type=0x%x",
2868 type);
2869 return -1;
2870 }
2871
2872 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
2873 return 0;
2874}
2875
2876
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002877int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2878{
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002879 struct wpa_radio_work *work;
2880 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002881
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002882 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
2883 if (work->wpa_s != wpa_s || !work->started ||
2884 (os_strcmp(work->type, "scan") != 0 &&
2885 os_strcmp(work->type, "p2p-scan") != 0))
2886 continue;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002887 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002888 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002889 }
2890
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002891 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
2892 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002893}
2894
2895
2896int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2897{
2898 struct sched_scan_plan *scan_plans = NULL;
2899 const char *token, *context = NULL;
2900 unsigned int num = 0;
2901
2902 if (!cmd)
2903 return -1;
2904
2905 if (!cmd[0]) {
2906 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2907 os_free(wpa_s->sched_scan_plans);
2908 wpa_s->sched_scan_plans = NULL;
2909 wpa_s->sched_scan_plans_num = 0;
2910 return 0;
2911 }
2912
2913 while ((token = cstr_token(cmd, " ", &context))) {
2914 int ret;
2915 struct sched_scan_plan *scan_plan, *n;
2916
2917 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2918 if (!n)
2919 goto fail;
2920
2921 scan_plans = n;
2922 scan_plan = &scan_plans[num];
2923 num++;
2924
2925 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2926 &scan_plan->iterations);
2927 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2928 wpa_printf(MSG_ERROR,
2929 "Invalid sched scan plan input: %s", token);
2930 goto fail;
2931 }
2932
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002933 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2934 wpa_printf(MSG_WARNING,
2935 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2936 num, scan_plan->interval,
2937 wpa_s->max_sched_scan_plan_interval);
2938 scan_plan->interval =
2939 wpa_s->max_sched_scan_plan_interval;
2940 }
2941
2942 if (ret == 1) {
2943 scan_plan->iterations = 0;
2944 break;
2945 }
2946
2947 if (!scan_plan->iterations) {
2948 wpa_printf(MSG_ERROR,
2949 "scan plan %u: Number of iterations cannot be zero",
2950 num);
2951 goto fail;
2952 }
2953
2954 if (scan_plan->iterations >
2955 wpa_s->max_sched_scan_plan_iterations) {
2956 wpa_printf(MSG_WARNING,
2957 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2958 num, scan_plan->iterations,
2959 wpa_s->max_sched_scan_plan_iterations);
2960 scan_plan->iterations =
2961 wpa_s->max_sched_scan_plan_iterations;
2962 }
2963
2964 wpa_printf(MSG_DEBUG,
2965 "scan plan %u: interval=%u iterations=%u",
2966 num, scan_plan->interval, scan_plan->iterations);
2967 }
2968
2969 if (!scan_plans) {
2970 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2971 goto fail;
2972 }
2973
2974 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2975 wpa_printf(MSG_ERROR,
2976 "All scan plans but the last must specify a number of iterations");
2977 goto fail;
2978 }
2979
2980 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2981 num, scan_plans[num - 1].interval);
2982
2983 if (num > wpa_s->max_sched_scan_plans) {
2984 wpa_printf(MSG_WARNING,
2985 "Too many scheduled scan plans (only %u supported)",
2986 wpa_s->max_sched_scan_plans);
2987 wpa_printf(MSG_WARNING,
2988 "Use only the first %u scan plans, and the last one (in infinite loop)",
2989 wpa_s->max_sched_scan_plans - 1);
2990 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2991 &scan_plans[num - 1], sizeof(*scan_plans));
2992 num = wpa_s->max_sched_scan_plans;
2993 }
2994
2995 os_free(wpa_s->sched_scan_plans);
2996 wpa_s->sched_scan_plans = scan_plans;
2997 wpa_s->sched_scan_plans_num = num;
2998
2999 return 0;
3000
3001fail:
3002 os_free(scan_plans);
3003 wpa_printf(MSG_ERROR, "invalid scan plans list");
3004 return -1;
3005}
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003006
3007
3008/**
3009 * wpas_scan_reset_sched_scan - Reset sched_scan state
3010 * @wpa_s: Pointer to wpa_supplicant data
3011 *
3012 * This function is used to cancel a running scheduled scan and to reset an
3013 * internal scan state to continue with a regular scan on the following
3014 * wpa_supplicant_req_scan() calls.
3015 */
3016void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3017{
3018 wpa_s->normal_scans = 0;
3019 if (wpa_s->sched_scanning) {
3020 wpa_s->sched_scan_timed_out = 0;
3021 wpa_s->prev_sched_ssid = NULL;
3022 wpa_supplicant_cancel_sched_scan(wpa_s);
3023 }
3024}
3025
3026
3027void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3028{
3029 /* simulate timeout to restart the sched scan */
3030 wpa_s->sched_scan_timed_out = 1;
3031 wpa_s->prev_sched_ssid = NULL;
3032 wpa_supplicant_cancel_sched_scan(wpa_s);
3033}