]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/scan.c
tests: Current Operating Class value from STA
[thirdparty/hostap.git] / wpa_supplicant / scan.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Scanning
ec2e7c4c 3 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9ba9fa07 9#include "utils/includes.h"
6fc6879b 10
9ba9fa07
JM
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
66fe0f70 14#include "common/wpa_ctrl.h"
6fc6879b
JM
15#include "config.h"
16#include "wpa_supplicant_i.h"
2d5b792d 17#include "driver_i.h"
b01c18a8 18#include "wps_supplicant.h"
0e65037c
JM
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
c923b8a5 21#include "hs20_supplicant.h"
8bac466b 22#include "notify.h"
9ba9fa07
JM
23#include "bss.h"
24#include "scan.h"
fbca4c89 25#include "mesh.h"
6fc6879b
JM
26
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
8bac466b 37 if (wpa_s->current_ssid == NULL) {
6fc6879b 38 wpa_s->current_ssid = ssid;
e1ae2876 39 wpas_notify_network_changed(wpa_s);
8bac466b 40 }
6fc6879b 41 wpa_supplicant_initiate_eapol(wpa_s);
f049052b
BG
42 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
43 "network - generating associated event");
6fc6879b
JM
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46}
47
48
ad08c363 49#ifdef CONFIG_WPS
5f738a21 50static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
f90c86d4 51 enum wps_request_type *req_type)
ad08c363
JM
52{
53 struct wpa_ssid *ssid;
54 int wps = 0;
ad08c363 55
5f738a21 56 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
ad08c363
JM
57 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
58 continue;
59
60 wps = 1;
b01c18a8 61 *req_type = wpas_wps_get_req_type(ssid);
88802405 62 if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
ad08c363
JM
63 return 2;
64 }
65
5f738a21 66#ifdef CONFIG_P2P
7a808c7e
JM
67 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
68 !wpa_s->conf->p2p_disabled) {
b21ff9cb
JM
69 wpa_s->wps->dev.p2p = 1;
70 if (!wps) {
71 wps = 1;
72 *req_type = WPS_REQ_ENROLLEE_INFO;
73 }
5f738a21
LC
74 }
75#endif /* CONFIG_P2P */
76
ad08c363
JM
77 return wps;
78}
79#endif /* CONFIG_WPS */
80
e76baaac 81
bb66d467
EC
82static 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
2c09af30
JM
109/**
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 */
349493bd 118int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
e76baaac 119{
349493bd 120 struct wpa_ssid *ssid = wpa_s->conf->ssid;
d4534bde 121 int count = 0, disabled = 0;
44b9ea5b
JM
122
123 if (wpa_s->p2p_mgmt)
124 return 0; /* no normal network profiles on p2p_mgmt interface */
125
e76baaac 126 while (ssid) {
349493bd 127 if (!wpas_network_disabled(wpa_s, ssid))
5471c343 128 count++;
d4534bde
JM
129 else
130 disabled++;
e76baaac
JM
131 ssid = ssid->next;
132 }
4d5bda5f
JM
133 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
134 wpa_s->conf->auto_interworking)
135 count++;
d4534bde
JM
136 if (count == 0 && disabled > 0) {
137 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
138 "networks)", disabled);
139 }
5471c343 140 return count;
e76baaac
JM
141}
142
143
144static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
145 struct wpa_ssid *ssid)
146{
3d5f0e91
ST
147 int min_temp_disabled = 0;
148
e76baaac 149 while (ssid) {
3d5f0e91
ST
150 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 }
e76baaac
JM
160 ssid = ssid->next;
161 }
162
163 /* ap_scan=2 mode - try to associate with each SSID. */
164 if (ssid == NULL) {
f049052b
BG
165 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
166 "end of scan list - go back to beginning");
ba2a573c 167 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
3d5f0e91 168 wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
e76baaac
JM
169 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. */
ba2a573c 176 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac
JM
177 }
178 wpa_supplicant_associate(wpa_s, NULL, ssid);
179}
180
181
d12a51b5 182static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
60b94c98 183{
d12a51b5
JM
184 struct wpa_supplicant *wpa_s = work->wpa_s;
185 struct wpa_driver_scan_params *params = work->ctx;
60b94c98
JM
186 int ret;
187
d12a51b5 188 if (deinit) {
b3253ebb
AO
189 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;
d12a51b5
JM
196 return;
197 }
198
9fde1460
EC
199 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
c267753b
JM
203 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");
6dc3206e
JM
206 wpa_scan_free_params(params);
207 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
c267753b
JM
208 radio_work_done(work);
209 return;
210 }
211
60b94c98
JM
212 wpa_supplicant_notify_scanning(wpa_s, 1);
213
2dbe63ad
JM
214 if (wpa_s->clear_driver_scan_cache) {
215 wpa_printf(MSG_DEBUG,
216 "Request driver to clear scan cache due to local BSS flush");
949938aa 217 params->only_new_results = 1;
2dbe63ad 218 }
17fbb751 219 ret = wpa_drv_scan(wpa_s, params);
bf88401d
SD
220 /*
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;
d12a51b5
JM
231 wpa_scan_free_params(params);
232 work->ctx = NULL;
60b94c98 233 if (ret) {
3756acfd
AS
234 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
235 !wpa_s->beacon_rep_data.token;
2d9c99e3
JM
236
237 if (wpa_s->disconnected)
238 retry = 0;
239
60b94c98
JM
240 wpa_supplicant_notify_scanning(wpa_s, 0);
241 wpas_notify_scan_done(wpa_s, 0);
2d9c99e3
JM
242 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" : "");
d12a51b5 247 radio_work_done(work);
2d9c99e3
JM
248
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);
57d3c591
HW
253 } else if (wpa_s->scan_res_handler) {
254 /* Clear the scan_res_handler */
255 wpa_s->scan_res_handler = NULL;
2d9c99e3 256 }
3756acfd
AS
257
258 if (wpa_s->beacon_rep_data.token)
259 wpas_rrm_refuse_request(wpa_s);
260
d12a51b5 261 return;
0b7a25c0 262 }
60b94c98 263
d12a51b5
JM
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;
270}
271
272
273/**
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 */
279int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
280 struct wpa_driver_scan_params *params)
281{
282 struct wpa_driver_scan_params *ctx;
283
284 if (wpa_s->scan_work) {
285 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
286 return -1;
287 }
288
289 ctx = wpa_scan_clone_params(params);
f37d8a4d
JM
290 if (!ctx ||
291 radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
d12a51b5
JM
292 {
293 wpa_scan_free_params(ctx);
6dc3206e 294 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
d12a51b5
JM
295 return -1;
296 }
297
298 return 0;
60b94c98
JM
299}
300
301
6a90053c
LC
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
cbdf3507
LC
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
0f346657
JM
326static int
327wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
328 struct wpa_driver_scan_params *params)
cbdf3507
LC
329{
330 int ret;
331
cbdf3507 332 wpa_supplicant_notify_scanning(wpa_s, 1);
09ea4309 333 ret = wpa_drv_sched_scan(wpa_s, params);
cbdf3507
LC
334 if (ret)
335 wpa_supplicant_notify_scanning(wpa_s, 0);
336 else
337 wpa_s->sched_scanning = 1;
338
339 return ret;
340}
341
342
0f346657 343static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
cbdf3507
LC
344{
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 }
353
354 return ret;
355}
356
357
3812464c
JM
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;
faebdeaa 375 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
3812464c
JM
376 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
5f738a21
LC
391static 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);
faebdeaa 403 params->freqs = os_calloc(2, sizeof(int));
5f738a21
LC
404 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 }
41d5ce9e
RR
417
418 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
419 /*
420 * Optimize scan based on GO information during persistent
421 * group reinvocation
422 */
28fa4eb2 423 if (wpa_s->p2p_in_invitation < 5 &&
41d5ce9e
RR
424 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);
faebdeaa 427 params->freqs = os_calloc(2, sizeof(int));
41d5ce9e
RR
428 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 }
5f738a21
LC
443#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);
faebdeaa 453 params->freqs = os_calloc(2, sizeof(int));
5f738a21
LC
454 if (params->freqs)
455 params->freqs[0] = wpa_s->wps_freq;
456 wpa_s->after_wps--;
662b40b1
JM
457 } else if (wpa_s->after_wps)
458 wpa_s->after_wps--;
5f738a21 459
1e7fb4f1
JM
460 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);
faebdeaa 465 params->freqs = os_calloc(2, sizeof(int));
1e7fb4f1
JM
466 if (params->freqs)
467 params->freqs[0] = wpa_s->wps_freq;
468 wpa_s->known_wps_freq = 0; /* only do this once */
469 }
5f738a21
LC
470#endif /* CONFIG_WPS */
471}
472
473
46ee0427
JM
474#ifdef CONFIG_INTERWORKING
475static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
476 struct wpabuf *buf)
477{
46ee0427
JM
478 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);
11540c0b 481 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
46ee0427
JM
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
2ac43334
RZ
489#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
cc9985d1 516void 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
2ac43334
RZ
537 if (wpa_s->enable_oce & OCE_STA)
538 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
332aadb8
AP
539 /* Send MBO and OCE capabilities */
540 if (wpabuf_resize(&default_ies, 12) == 0)
cc9985d1 541 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
a13e07ec 551static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
6fc6879b 552{
46ee0427 553 struct wpabuf *extra_ie = NULL;
ad443097
IP
554 u8 ext_capab[18];
555 int ext_capab_len;
b01c18a8 556#ifdef CONFIG_WPS
509a3972 557 int wps = 0;
f90c86d4 558 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
46ee0427
JM
559#endif /* CONFIG_WPS */
560
cc9a2575
KV
561#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
ad443097
IP
568 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
46ee0427
JM
574#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 */
5f738a21 579
2ac43334
RZ
580#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
46ee0427 585#ifdef CONFIG_WPS
5f738a21
LC
586 wps = wpas_wps_in_use(wpa_s, &req_type);
587
588 if (wps) {
46ee0427 589 struct wpabuf *wps_ie;
360182ed
JM
590 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
591 DEV_PW_DEFAULT,
592 &wpa_s->wps->dev,
5f738a21
LC
593 wpa_s->wps->uuid, req_type,
594 0, NULL);
595 if (wps_ie) {
46ee0427
JM
596 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
597 wpabuf_put_buf(extra_ie, wps_ie);
598 wpabuf_free(wps_ie);
5f738a21
LC
599 }
600 }
601
602#ifdef CONFIG_P2P
46ee0427 603 if (wps) {
5f738a21 604 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
46ee0427
JM
605 if (wpabuf_resize(&extra_ie, ielen) == 0)
606 wpas_p2p_scan_ie(wpa_s, extra_ie);
5f738a21
LC
607 }
608#endif /* CONFIG_P2P */
609
fbca4c89
JA
610 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
611
b01c18a8 612#endif /* CONFIG_WPS */
5f738a21 613
0f105f9e 614#ifdef CONFIG_HS20
9ce2015a 615 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
ec2cf403 616 wpas_hs20_add_indication(extra_ie, -1, 0);
0f105f9e
JM
617#endif /* CONFIG_HS20 */
618
b36a3a65
AN
619#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
c5d193d7 625#ifdef CONFIG_MBO
332aadb8
AP
626 /* Send MBO and OCE capabilities */
627 if (wpabuf_resize(&extra_ie, 12) == 0)
c5d193d7
DS
628 wpas_mbo_scan_ie(wpa_s, extra_ie);
629#endif /* CONFIG_MBO */
630
61c10118
JM
631 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
46ee0427 638 return extra_ie;
5f738a21
LC
639}
640
641
5cc70322
JM
642#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
6124e858 666#endif /* CONFIG_P2P */
5cc70322 667
5cc70322 668
faf9a858
JM
669static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
670 enum hostapd_hw_mode band,
d0e116f6
VK
671 struct wpa_driver_scan_params *params,
672 int is_6ghz)
faf9a858
JM
673{
674 /* Include only supported channels for the specified band */
675 struct hostapd_hw_modes *mode;
676 int count, i;
677
d0e116f6 678 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
faf9a858
JM
679 if (mode == NULL) {
680 /* No channels supported in this band - use empty list */
681 params->freqs = os_zalloc(sizeof(int));
682 return;
683 }
684
faebdeaa 685 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
faf9a858
JM
686 if (params->freqs == NULL)
687 return;
688 for (count = 0, i = 0; i < mode->num_channels; i++) {
689 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
690 continue;
691 params->freqs[count++] = mode->channels[i].freq;
692 }
693}
694
695
696static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
697 struct wpa_driver_scan_params *params)
698{
699 if (wpa_s->hw.modes == NULL)
700 return; /* unknown what channels the driver supports */
701 if (params->freqs)
702 return; /* already using a limited channel set */
703 if (wpa_s->setband == WPA_SETBAND_5G)
704 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
d0e116f6 705 params, 0);
faf9a858
JM
706 else if (wpa_s->setband == WPA_SETBAND_2G)
707 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
d0e116f6 708 params, 0);
faf9a858
JM
709}
710
711
c04562e6
IP
712static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
713 struct wpa_driver_scan_params *params,
714 size_t max_ssids, const u8 *ssid, size_t ssid_len)
715{
716 unsigned int j;
717
718 for (j = 0; j < params->num_ssids; j++) {
719 if (params->ssids[j].ssid_len == ssid_len &&
720 params->ssids[j].ssid &&
721 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
722 return; /* already in the list */
723 }
724
725 if (params->num_ssids + 1 > max_ssids) {
726 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
727 return;
728 }
729
730 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
731 wpa_ssid_txt(ssid, ssid_len));
732
733 params->ssids[params->num_ssids].ssid = ssid;
734 params->ssids[params->num_ssids].ssid_len = ssid_len;
735 params->num_ssids++;
736}
737
738
739static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
740 struct wpa_driver_scan_params *params,
741 struct wpa_ssid *ssid, size_t max_ssids)
742{
743#ifdef CONFIG_OWE
744 struct wpa_bss *bss;
745
746 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
747 return;
748
749 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
750 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
751
752 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
753 const u8 *owe, *pos, *end;
754 const u8 *owe_ssid;
755 size_t owe_ssid_len;
756
757 if (bss->ssid_len != ssid->ssid_len ||
758 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
759 continue;
760
761 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
762 if (!owe || owe[1] < 4)
763 continue;
764
765 pos = owe + 6;
766 end = owe + 2 + owe[1];
767
768 /* Must include BSSID and ssid_len */
769 if (end - pos < ETH_ALEN + 1)
770 return;
771
772 /* Skip BSSID */
773 pos += ETH_ALEN;
774 owe_ssid_len = *pos++;
775 owe_ssid = pos;
776
777 if ((size_t) (end - pos) < owe_ssid_len ||
778 owe_ssid_len > SSID_MAX_LEN)
779 return;
780
781 wpa_printf(MSG_DEBUG,
782 "OWE: scan_ssids: transition mode OWE ssid=%s",
783 wpa_ssid_txt(owe_ssid, owe_ssid_len));
784
785 wpa_add_scan_ssid(wpa_s, params, max_ssids,
786 owe_ssid, owe_ssid_len);
787 return;
788 }
789#endif /* CONFIG_OWE */
790}
791
792
6891f0e6
LJ
793static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
794 struct wpa_driver_scan_params *params,
795 size_t max_ssids)
796{
797 unsigned int i;
798 struct wpa_ssid *ssid;
799
ef60f643
RP
800 /*
801 * For devices with max_ssids greater than 1, leave the last slot empty
802 * for adding the wildcard scan entry.
803 */
804 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
805
6891f0e6 806 for (i = 0; i < wpa_s->scan_id_count; i++) {
6891f0e6 807 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
c04562e6 808 if (!ssid)
6891f0e6 809 continue;
c04562e6
IP
810 if (ssid->scan_ssid)
811 wpa_add_scan_ssid(wpa_s, params, max_ssids,
812 ssid->ssid, ssid->ssid_len);
813 /*
814 * Also add the SSID of the OWE BSS, to allow discovery of
815 * transition mode APs more quickly.
816 */
817 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
6891f0e6
LJ
818 }
819
820 wpa_s->scan_id_count = 0;
821}
822
823
a80651d0
KV
824static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
825 struct wpa_driver_scan_params *params,
826 size_t max_ssids)
827{
828 unsigned int i;
829
830 if (wpa_s->ssids_from_scan_req == NULL ||
831 wpa_s->num_ssids_from_scan_req == 0)
832 return 0;
833
834 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
835 wpa_s->num_ssids_from_scan_req = max_ssids;
836 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
837 (unsigned int) max_ssids);
838 }
839
840 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
841 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
842 params->ssids[i].ssid_len =
843 wpa_s->ssids_from_scan_req[i].ssid_len;
844 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
845 params->ssids[i].ssid,
846 params->ssids[i].ssid_len);
847 }
848
849 params->num_ssids = wpa_s->num_ssids_from_scan_req;
850 wpa_s->num_ssids_from_scan_req = 0;
851 return 1;
852}
853
854
5f738a21
LC
855static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
856{
857 struct wpa_supplicant *wpa_s = eloop_ctx;
858 struct wpa_ssid *ssid;
49e3eea8 859 int ret, p2p_in_prog;
b9b12d02 860 struct wpabuf *extra_ie = NULL;
e76baaac 861 struct wpa_driver_scan_params params;
7c865c68 862 struct wpa_driver_scan_params *scan_params;
e76baaac 863 size_t max_ssids;
3f9ebc43 864 int connect_without_scan = 0;
6fc6879b 865
6e374bd4
JM
866 wpa_s->ignore_post_flush_scan_res = 0;
867
8401a6b0 868 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
f049052b 869 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
8401a6b0
JM
870 return;
871 }
872
4115303b 873 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
ac06fb12 874 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
3180d7a2 875 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6fc6879b 876 return;
3180d7a2 877 }
6fc6879b 878
f86d282f
JJ
879 if (wpa_s->scanning) {
880 /*
881 * If we are already in scanning state, we shall reschedule the
882 * the incoming scan request.
883 */
884 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
885 wpa_supplicant_req_scan(wpa_s, 1, 0);
886 return;
887 }
888
349493bd 889 if (!wpa_supplicant_enabled_networks(wpa_s) &&
4115303b 890 wpa_s->scan_req == NORMAL_SCAN_REQ) {
f049052b 891 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
6fc6879b
JM
892 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
893 return;
894 }
6fc6879b 895
c2a04078
JM
896 if (wpa_s->conf->ap_scan != 0 &&
897 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
f049052b
BG
898 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
899 "overriding ap_scan configuration");
6fc6879b 900 wpa_s->conf->ap_scan = 0;
8bac466b 901 wpas_notify_ap_scan_changed(wpa_s);
6fc6879b
JM
902 }
903
904 if (wpa_s->conf->ap_scan == 0) {
905 wpa_supplicant_gen_assoc_event(wpa_s);
906 return;
907 }
908
3f9ebc43 909 ssid = NULL;
a51c40aa 910 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
3f9ebc43
JM
911 wpa_s->connect_without_scan) {
912 connect_without_scan = 1;
913 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
914 if (ssid == wpa_s->connect_without_scan)
915 break;
916 }
917 }
918
49e3eea8 919 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
3f9ebc43
JM
920 if (p2p_in_prog && p2p_in_prog != 2 &&
921 (!ssid ||
922 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
1b5d4714
JM
923 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
924 wpa_supplicant_req_scan(wpa_s, 5, 0);
925 return;
303f60d3 926 }
303f60d3 927
57f93d68
AN
928 /*
929 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
930 * used for changing modes inside wpa_supplicant (roaming,
931 * auto-reconnect, etc). Discarding the scan might hurt these processes.
932 * The normal use case for PNO is to suspend the host immediately after
933 * starting PNO, so the periodic 100 ms attempts to run the scan do not
934 * normally happen in practice multiple times, i.e., this is simply
935 * restarting scanning once the host is woken up and PNO stopped.
936 */
937 if (wpa_s->pno || wpa_s->pno_sched_pending) {
938 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
939 wpa_supplicant_req_scan(wpa_s, 0, 100000);
940 return;
941 }
942
17fbb751 943 if (wpa_s->conf->ap_scan == 2)
e76baaac
JM
944 max_ssids = 1;
945 else {
946 max_ssids = wpa_s->max_scan_ssids;
947 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
948 max_ssids = WPAS_MAX_SCAN_SSIDS;
949 }
950
12455031 951 wpa_s->last_scan_req = wpa_s->scan_req;
4115303b 952 wpa_s->scan_req = NORMAL_SCAN_REQ;
e76baaac 953
3f9ebc43
JM
954 if (connect_without_scan) {
955 wpa_s->connect_without_scan = NULL;
956 if (ssid) {
957 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
958 "without scan step");
959 wpa_supplicant_associate(wpa_s, NULL, ssid);
960 return;
961 }
962 }
963
e76baaac
JM
964 os_memset(&params, 0, sizeof(params));
965
2d9c99e3 966 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
6fc6879b
JM
967 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
968 wpa_s->wpa_state == WPA_INACTIVE)
969 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
970
7c865c68
TB
971 /*
972 * If autoscan has set its own scanning parameters
973 */
974 if (wpa_s->autoscan_params != NULL) {
975 scan_params = wpa_s->autoscan_params;
976 goto scan;
977 }
978
a80651d0
KV
979 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
980 wpa_set_ssids_from_scan_req(wpa_s, &params, max_ssids)) {
981 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
982 goto ssid_list_set;
983 }
984
a21c05db 985#ifdef CONFIG_P2P
85ea132a 986 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
c35e35ed 987 wpa_s->go_params && !wpa_s->conf->passive_scan) {
9100b660
JM
988 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
989 wpa_s->p2p_in_provisioning,
990 wpa_s->show_group_started);
a21c05db
JM
991 params.ssids[0].ssid = wpa_s->go_params->ssid;
992 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
993 params.num_ssids = 1;
994 goto ssid_list_set;
995 }
41d5ce9e
RR
996
997 if (wpa_s->p2p_in_invitation) {
998 if (wpa_s->current_ssid) {
999 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1000 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1001 params.ssids[0].ssid_len =
1002 wpa_s->current_ssid->ssid_len;
1003 params.num_ssids = 1;
1004 } else {
1005 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1006 }
1007 goto ssid_list_set;
1008 }
a21c05db
JM
1009#endif /* CONFIG_P2P */
1010
e76baaac 1011 /* Find the starting point from which to continue scanning */
6fc6879b 1012 ssid = wpa_s->conf->ssid;
ba2a573c 1013 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
6fc6879b
JM
1014 while (ssid) {
1015 if (ssid == wpa_s->prev_scan_ssid) {
1016 ssid = ssid->next;
1017 break;
1018 }
1019 ssid = ssid->next;
1020 }
1021 }
6fc6879b 1022
12455031 1023 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
0a34b62b
JM
1024#ifdef CONFIG_AP
1025 !wpa_s->ap_iface &&
1026#endif /* CONFIG_AP */
12455031 1027 wpa_s->conf->ap_scan == 2) {
7dcdcfd6 1028 wpa_s->connect_without_scan = NULL;
b3aa456b 1029 wpa_s->prev_scan_wildcard = 0;
e76baaac
JM
1030 wpa_supplicant_assoc_try(wpa_s, ssid);
1031 return;
1032 } else if (wpa_s->conf->ap_scan == 2) {
6fc6879b 1033 /*
ba2a573c
JM
1034 * User-initiated scan request in ap_scan == 2; scan with
1035 * wildcard SSID.
6fc6879b 1036 */
e76baaac 1037 ssid = NULL;
0f44ec8e
PQ
1038 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1039 /*
1040 * Perform single-channel single-SSID scan for
1041 * reassociate-to-same-BSS operation.
1042 */
1043 /* Setup SSID */
1044 ssid = wpa_s->current_ssid;
1045 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1046 ssid->ssid, ssid->ssid_len);
1047 params.ssids[0].ssid = ssid->ssid;
1048 params.ssids[0].ssid_len = ssid->ssid_len;
1049 params.num_ssids = 1;
1050
1051 /*
1052 * Allocate memory for frequency array, allocate one extra
1053 * slot for the zero-terminator.
1054 */
1055 params.freqs = os_malloc(sizeof(int) * 2);
8e909fa9
JM
1056 if (params.freqs) {
1057 params.freqs[0] = wpa_s->assoc_freq;
1058 params.freqs[1] = 0;
0f44ec8e 1059 }
0f44ec8e
PQ
1060
1061 /*
1062 * Reset the reattach flag so that we fall back to full scan if
1063 * this scan fails.
1064 */
1065 wpa_s->reattach = 0;
e76baaac 1066 } else {
5be45e2e 1067 struct wpa_ssid *start = ssid, *tssid;
d3a98225 1068 int freqs_set = 0;
e76baaac
JM
1069 if (ssid == NULL && max_ssids > 1)
1070 ssid = wpa_s->conf->ssid;
1071 while (ssid) {
349493bd
JM
1072 if (!wpas_network_disabled(wpa_s, ssid) &&
1073 ssid->scan_ssid) {
e76baaac
JM
1074 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1075 ssid->ssid, ssid->ssid_len);
1076 params.ssids[params.num_ssids].ssid =
1077 ssid->ssid;
1078 params.ssids[params.num_ssids].ssid_len =
1079 ssid->ssid_len;
1080 params.num_ssids++;
1081 if (params.num_ssids + 1 >= max_ssids)
1082 break;
1083 }
c04562e6
IP
1084
1085 if (!wpas_network_disabled(wpa_s, ssid)) {
1086 /*
1087 * Also add the SSID of the OWE BSS, to allow
1088 * discovery of transition mode APs more
1089 * quickly.
1090 */
1091 wpa_add_owe_scan_ssid(wpa_s, &params, ssid,
1092 max_ssids);
1093 }
1094
e76baaac
JM
1095 ssid = ssid->next;
1096 if (ssid == start)
1097 break;
1098 if (ssid == NULL && max_ssids > 1 &&
1099 start != wpa_s->conf->ssid)
1100 ssid = wpa_s->conf->ssid;
6fc6879b 1101 }
d3a98225 1102
6891f0e6
LJ
1103 if (wpa_s->scan_id_count &&
1104 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1105 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
1106
0855e2e1
JM
1107 for (tssid = wpa_s->conf->ssid;
1108 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1109 tssid = tssid->next) {
349493bd 1110 if (wpas_network_disabled(wpa_s, tssid))
d3a98225 1111 continue;
5be45e2e 1112 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
d3a98225 1113 int_array_concat(&params.freqs,
5be45e2e 1114 tssid->scan_freq);
d3a98225
JM
1115 } else {
1116 os_free(params.freqs);
1117 params.freqs = NULL;
1118 }
1119 freqs_set = 1;
1120 }
1121 int_array_sort_unique(params.freqs);
6fc6879b
JM
1122 }
1123
b3aa456b
ES
1124 if (ssid && max_ssids == 1) {
1125 /*
1126 * If the driver is limited to 1 SSID at a time interleave
1127 * wildcard SSID scans with specific SSID scans to avoid
1128 * waiting a long time for a wildcard scan.
1129 */
1130 if (!wpa_s->prev_scan_wildcard) {
1131 params.ssids[0].ssid = NULL;
1132 params.ssids[0].ssid_len = 0;
1133 wpa_s->prev_scan_wildcard = 1;
1134 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1135 "wildcard SSID (Interleave with specific)");
1136 } else {
1137 wpa_s->prev_scan_ssid = ssid;
1138 wpa_s->prev_scan_wildcard = 0;
1139 wpa_dbg(wpa_s, MSG_DEBUG,
1140 "Starting AP scan for specific SSID: %s",
1141 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
e76baaac 1142 }
b3aa456b
ES
1143 } else if (ssid) {
1144 /* max_ssids > 1 */
1145
1146 wpa_s->prev_scan_ssid = ssid;
1147 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1148 "the scan request");
1149 params.num_ssids++;
88c2d488
JM
1150 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1151 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1152 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
c35e35ed
JM
1153 } else if (wpa_s->conf->passive_scan) {
1154 wpa_dbg(wpa_s, MSG_DEBUG,
1155 "Use passive scan based on configuration");
e76baaac 1156 } else {
ba2a573c 1157 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
e76baaac 1158 params.num_ssids++;
f049052b
BG
1159 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1160 "SSID");
6fc6879b
JM
1161 }
1162
a80651d0 1163ssid_list_set:
5f738a21 1164 wpa_supplicant_optimize_freqs(wpa_s, &params);
a13e07ec 1165 extra_ie = wpa_supplicant_extra_ies(wpa_s);
0e65037c 1166
949938aa 1167 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2dbe63ad
JM
1168 wpa_s->manual_scan_only_new) {
1169 wpa_printf(MSG_DEBUG,
1170 "Request driver to clear scan cache due to manual only_new=1 scan");
949938aa 1171 params.only_new_results = 1;
2dbe63ad 1172 }
949938aa 1173
fee52342
JM
1174 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1175 wpa_s->manual_scan_freqs) {
1176 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1177 params.freqs = wpa_s->manual_scan_freqs;
1178 wpa_s->manual_scan_freqs = NULL;
1179 }
1180
88a44755
JM
1181 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1182 wpa_dbg(wpa_s, MSG_DEBUG,
1183 "Limit select_network scan to specified channels");
1184 params.freqs = wpa_s->select_network_scan_freqs;
1185 wpa_s->select_network_scan_freqs = NULL;
1186 }
1187
f47d639d 1188 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
f049052b
BG
1189 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1190 "generated frequency list");
f47d639d
JM
1191 params.freqs = wpa_s->next_scan_freqs;
1192 } else
1193 os_free(wpa_s->next_scan_freqs);
1194 wpa_s->next_scan_freqs = NULL;
faf9a858 1195 wpa_setband_scan_freqs(wpa_s, &params);
f47d639d 1196
f5ffc348
BG
1197 /* See if user specified frequencies. If so, scan only those. */
1198 if (wpa_s->conf->freq_list && !params.freqs) {
1199 wpa_dbg(wpa_s, MSG_DEBUG,
1200 "Optimize scan based on conf->freq_list");
1201 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1202 }
1203
6124e858
BG
1204 /* Use current associated channel? */
1205 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
53c5dfc2
IP
1206 unsigned int num = wpa_s->num_multichan_concurrent;
1207
1208 params.freqs = os_calloc(num + 1, sizeof(int));
1209 if (params.freqs) {
1210 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1211 if (num > 0) {
1212 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1213 "current operating channels since "
1214 "scan_cur_freq is enabled");
1215 } else {
1216 os_free(params.freqs);
1217 params.freqs = NULL;
1218 }
6124e858
BG
1219 }
1220 }
1221
2ac43334
RZ
1222#ifdef CONFIG_MBO
1223 if (wpa_s->enable_oce & OCE_STA)
1224 params.oce_scan = 1;
1225#endif /* CONFIG_MBO */
1226
3812464c
JM
1227 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1228 wpa_s->conf, &params.num_filter_ssids);
46ee0427
JM
1229 if (extra_ie) {
1230 params.extra_ies = wpabuf_head(extra_ie);
1231 params.extra_ies_len = wpabuf_len(extra_ie);
1232 }
3812464c 1233
d1dd48e3 1234#ifdef CONFIG_P2P
8235f89f 1235 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
85ea132a 1236 (wpa_s->show_group_started && wpa_s->go_params)) {
d1dd48e3
JM
1237 /*
1238 * The interface may not yet be in P2P mode, so we have to
1239 * explicitly request P2P probe to disable CCK rates.
1240 */
1241 params.p2p_probe = 1;
1242 }
1243#endif /* CONFIG_P2P */
1244
346b333d 1245 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
bb66d467
EC
1246 wpa_s->wpa_state <= WPA_SCANNING)
1247 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_scan);
56c76fa5 1248
0645492e
JM
1249 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1250 struct wpa_bss *bss;
1251
1252 params.bssid = wpa_s->next_scan_bssid;
1253 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
5fdacce4
VJ
1254 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1255 bss && bss->ssid_len && params.num_ssids == 1 &&
0645492e
JM
1256 params.ssids[0].ssid_len == 0) {
1257 params.ssids[0].ssid = bss->ssid;
1258 params.ssids[0].ssid_len = bss->ssid_len;
1259 wpa_dbg(wpa_s, MSG_DEBUG,
1260 "Scan a previously specified BSSID " MACSTR
1261 " and SSID %s",
1262 MAC2STR(params.bssid),
1263 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1264 } else {
1265 wpa_dbg(wpa_s, MSG_DEBUG,
1266 "Scan a previously specified BSSID " MACSTR,
1267 MAC2STR(params.bssid));
1268 }
1269 }
1270
7c865c68
TB
1271 scan_params = &params;
1272
1273scan:
5cc70322
JM
1274#ifdef CONFIG_P2P
1275 /*
1276 * If the driver does not support multi-channel concurrency and a
1277 * virtual interface that shares the same radio with the wpa_s interface
1278 * is operating there may not be need to scan other channels apart from
1279 * the current operating channel on the other virtual interface. Filter
1280 * out other channels in case we are trying to find a connection for a
1281 * station interface when we are not configured to prefer station
1282 * connection and a concurrent operation is already in process.
1283 */
12455031
LP
1284 if (wpa_s->scan_for_connection &&
1285 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
5cc70322
JM
1286 !scan_params->freqs && !params.freqs &&
1287 wpas_is_p2p_prioritized(wpa_s) &&
5cc70322
JM
1288 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1289 non_p2p_network_enabled(wpa_s)) {
53c5dfc2
IP
1290 unsigned int num = wpa_s->num_multichan_concurrent;
1291
1292 params.freqs = os_calloc(num + 1, sizeof(int));
1293 if (params.freqs) {
1294 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1295 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1296 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1297 } else {
1298 os_free(params.freqs);
1299 params.freqs = NULL;
1300 }
5cc70322
JM
1301 }
1302 }
1303#endif /* CONFIG_P2P */
1304
7c865c68 1305 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
6fc6879b 1306
fee52342
JM
1307 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1308 !wpa_s->manual_scan_freqs) {
1309 /* Restore manual_scan_freqs for the next attempt */
1310 wpa_s->manual_scan_freqs = params.freqs;
1311 params.freqs = NULL;
1312 }
1313
46ee0427 1314 wpabuf_free(extra_ie);
d3a98225 1315 os_free(params.freqs);
3812464c 1316 os_free(params.filter_ssids);
bb66d467 1317 os_free(params.mac_addr);
ad08c363 1318
6fc6879b 1319 if (ret) {
f049052b 1320 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
2d9c99e3
JM
1321 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1322 wpa_supplicant_set_state(wpa_s,
1323 wpa_s->scan_prev_wpa_state);
23270cd8 1324 /* Restore scan_req since we will try to scan again */
12455031 1325 wpa_s->scan_req = wpa_s->last_scan_req;
1c4c9c50 1326 wpa_supplicant_req_scan(wpa_s, 1, 0);
5cc70322
JM
1327 } else {
1328 wpa_s->scan_for_connection = 0;
a8826b18
JM
1329#ifdef CONFIG_INTERWORKING
1330 wpa_s->interworking_fast_assoc_tried = 0;
1331#endif /* CONFIG_INTERWORKING */
5fdacce4 1332 wpa_s->next_scan_bssid_wildcard_ssid = 0;
0645492e
JM
1333 if (params.bssid)
1334 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
d902a9c1 1335 }
6fc6879b
JM
1336}
1337
1338
9e737f08
PF
1339void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1340{
594516b4 1341 struct os_reltime remaining, new_int;
9e737f08
PF
1342 int cancelled;
1343
1344 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1345 &remaining);
1346
1347 new_int.sec = sec;
1348 new_int.usec = 0;
594516b4 1349 if (cancelled && os_reltime_before(&remaining, &new_int)) {
9e737f08
PF
1350 new_int.sec = remaining.sec;
1351 new_int.usec = remaining.usec;
1352 }
1353
c6f5dec9
PF
1354 if (cancelled) {
1355 eloop_register_timeout(new_int.sec, new_int.usec,
1356 wpa_supplicant_scan, wpa_s, NULL);
1357 }
9e737f08
PF
1358 wpa_s->scan_interval = sec;
1359}
1360
1361
6fc6879b
JM
1362/**
1363 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1364 * @wpa_s: Pointer to wpa_supplicant data
1365 * @sec: Number of seconds after which to scan
1366 * @usec: Number of microseconds after which to scan
1367 *
1368 * This function is used to schedule a scan for neighboring access points after
1369 * the specified time.
1370 */
1371void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1372{
44b9ea5b
JM
1373 int res;
1374
1375 if (wpa_s->p2p_mgmt) {
1376 wpa_dbg(wpa_s, MSG_DEBUG,
1377 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1378 sec, usec);
1379 return;
1380 }
1381
1382 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1383 NULL);
a09ffd5f
JM
1384 if (res == 1) {
1385 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
e2f5a988 1386 sec, usec);
a09ffd5f
JM
1387 } else if (res == 0) {
1388 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1389 sec, usec);
1390 } else {
1391 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1392 sec, usec);
1393 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
e2f5a988 1394 }
6fc6879b
JM
1395}
1396
1397
6a90053c
LC
1398/**
1399 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1400 * @wpa_s: Pointer to wpa_supplicant data
1401 * @sec: Number of seconds after which to scan
1402 * @usec: Number of microseconds after which to scan
2c09af30 1403 * Returns: 0 on success or -1 otherwise
6a90053c
LC
1404 *
1405 * This function is used to schedule periodic scans for neighboring
1406 * access points after the specified time.
1407 */
1408int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1409 int sec, int usec)
1410{
1411 if (!wpa_s->sched_scan_supported)
1412 return -1;
1413
1414 eloop_register_timeout(sec, usec,
1415 wpa_supplicant_delayed_sched_scan_timeout,
1416 wpa_s, NULL);
1417
1418 return 0;
1419}
1420
1421
57c3a605 1422static void
1423wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1424 struct wpa_driver_scan_params *params)
1425{
1426 if (wpa_s->wpa_state != WPA_COMPLETED ||
1427 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1428 wpa_s->srp.relative_rssi_set == 0)
1429 return;
1430
1431 params->relative_rssi_set = 1;
1432 params->relative_rssi = wpa_s->srp.relative_rssi;
1433
1434 if (wpa_s->srp.relative_adjust_rssi == 0)
1435 return;
1436
1437 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1438 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1439}
1440
1441
cbdf3507
LC
1442/**
1443 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1444 * @wpa_s: Pointer to wpa_supplicant data
2c09af30 1445 * Returns: 0 is sched_scan was started or -1 otherwise
cbdf3507
LC
1446 *
1447 * This function is used to schedule periodic scans for neighboring
1448 * access points repeating the scan continuously.
1449 */
1450int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1451{
1452 struct wpa_driver_scan_params params;
7c865c68 1453 struct wpa_driver_scan_params *scan_params;
cbdf3507 1454 enum wpa_states prev_state;
7c865c68 1455 struct wpa_ssid *ssid = NULL;
a13e07ec 1456 struct wpabuf *extra_ie = NULL;
cbdf3507 1457 int ret;
cbdf3507 1458 unsigned int max_sched_scan_ssids;
76a5249e 1459 int wildcard = 0;
0b7a25c0 1460 int need_ssids;
32c02261 1461 struct sched_scan_plan scan_plan;
cbdf3507
LC
1462
1463 if (!wpa_s->sched_scan_supported)
1464 return -1;
1465
1466 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1467 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1468 else
1469 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
54ddd743 1470 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
76a5249e 1471 return -1;
cbdf3507 1472
14f34a73
AS
1473 wpa_s->sched_scan_stop_req = 0;
1474
1966e3d1
ES
1475 if (wpa_s->sched_scanning) {
1476 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
cbdf3507 1477 return 0;
1966e3d1 1478 }
cbdf3507 1479
0b7a25c0
JM
1480 need_ssids = 0;
1481 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 1482 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
0b7a25c0
JM
1483 /* Use wildcard SSID to find this network */
1484 wildcard = 1;
349493bd
JM
1485 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1486 ssid->ssid_len)
0b7a25c0 1487 need_ssids++;
aa283ddd
JM
1488
1489#ifdef CONFIG_WPS
349493bd 1490 if (!wpas_network_disabled(wpa_s, ssid) &&
fea7c3a0 1491 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
aa283ddd
JM
1492 /*
1493 * Normal scan is more reliable and faster for WPS
1494 * operations and since these are for short periods of
1495 * time, the benefit of trying to use sched_scan would
1496 * be limited.
1497 */
1498 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1499 "sched_scan for WPS");
1500 return -1;
1501 }
1502#endif /* CONFIG_WPS */
0b7a25c0
JM
1503 }
1504 if (wildcard)
1505 need_ssids++;
1506
1507 if (wpa_s->normal_scans < 3 &&
1508 (need_ssids <= wpa_s->max_scan_ssids ||
1509 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1510 /*
1511 * When normal scan can speed up operations, use that for the
1512 * first operations before starting the sched_scan to allow
1513 * user space sleep more. We do this only if the normal scan
1514 * has functionality that is suitable for this or if the
1515 * sched_scan does not have better support for multiple SSIDs.
1516 */
1517 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1518 "sched_scan for initial scans (normal_scans=%d)",
1519 wpa_s->normal_scans);
1520 return -1;
1521 }
1522
cbdf3507
LC
1523 os_memset(&params, 0, sizeof(params));
1524
b59e6f26 1525 /* If we can't allocate space for the filters, we just don't filter */
faebdeaa 1526 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
b59e6f26
LC
1527 sizeof(struct wpa_driver_scan_filter));
1528
cbdf3507
LC
1529 prev_state = wpa_s->wpa_state;
1530 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1531 wpa_s->wpa_state == WPA_INACTIVE)
1532 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1533
7c865c68
TB
1534 if (wpa_s->autoscan_params != NULL) {
1535 scan_params = wpa_s->autoscan_params;
1536 goto scan;
1537 }
1538
cbdf3507
LC
1539 /* Find the starting point from which to continue scanning */
1540 ssid = wpa_s->conf->ssid;
1541 if (wpa_s->prev_sched_ssid) {
1542 while (ssid) {
1543 if (ssid == wpa_s->prev_sched_ssid) {
1544 ssid = ssid->next;
1545 break;
1546 }
1547 ssid = ssid->next;
1548 }
1549 }
1550
1551 if (!ssid || !wpa_s->prev_sched_ssid) {
1552 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
cbdf3507
LC
1553 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1554 wpa_s->first_sched_scan = 1;
1555 ssid = wpa_s->conf->ssid;
1556 wpa_s->prev_sched_ssid = ssid;
1557 }
1558
76a5249e
JM
1559 if (wildcard) {
1560 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1561 params.num_ssids++;
1562 }
1563
cbdf3507 1564 while (ssid) {
349493bd 1565 if (wpas_network_disabled(wpa_s, ssid))
5edddf41 1566 goto next;
cbdf3507 1567
fcd16847
JM
1568 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1569 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1966e3d1
ES
1570 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1571 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
b59e6f26
LC
1572 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1573 ssid->ssid, ssid->ssid_len);
1574 params.filter_ssids[params.num_filter_ssids].ssid_len =
1575 ssid->ssid_len;
1576 params.num_filter_ssids++;
86b47aaf
JM
1577 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1578 {
1579 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1580 "filter for sched_scan - drop filter");
1581 os_free(params.filter_ssids);
1582 params.filter_ssids = NULL;
1583 params.num_filter_ssids = 0;
b59e6f26
LC
1584 }
1585
3f56f3a4 1586 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
76a5249e
JM
1587 if (params.num_ssids == max_sched_scan_ssids)
1588 break; /* only room for broadcast SSID */
3f56f3a4
JM
1589 wpa_dbg(wpa_s, MSG_DEBUG,
1590 "add to active scan ssid: %s",
1966e3d1 1591 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
cbdf3507
LC
1592 params.ssids[params.num_ssids].ssid =
1593 ssid->ssid;
1594 params.ssids[params.num_ssids].ssid_len =
1595 ssid->ssid_len;
1596 params.num_ssids++;
b59e6f26 1597 if (params.num_ssids >= max_sched_scan_ssids) {
cbdf3507 1598 wpa_s->prev_sched_ssid = ssid;
b55aca46
JM
1599 do {
1600 ssid = ssid->next;
1601 } while (ssid &&
349493bd 1602 (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 1603 !ssid->scan_ssid));
cbdf3507
LC
1604 break;
1605 }
1606 }
b59e6f26 1607
5edddf41 1608 next:
cbdf3507
LC
1609 wpa_s->prev_sched_ssid = ssid;
1610 ssid = ssid->next;
1611 }
1612
7c6a266c
JM
1613 if (params.num_filter_ssids == 0) {
1614 os_free(params.filter_ssids);
1615 params.filter_ssids = NULL;
1616 }
1617
a13e07ec
ES
1618 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1619 if (extra_ie) {
1620 params.extra_ies = wpabuf_head(extra_ie);
1621 params.extra_ies_len = wpabuf_len(extra_ie);
1622 }
cbdf3507 1623
cf70d298
RM
1624 if (wpa_s->conf->filter_rssi)
1625 params.filter_rssi = wpa_s->conf->filter_rssi;
1626
1cc0d6a0
BP
1627 /* See if user specified frequencies. If so, scan only those. */
1628 if (wpa_s->conf->freq_list && !params.freqs) {
1629 wpa_dbg(wpa_s, MSG_DEBUG,
1630 "Optimize scan based on conf->freq_list");
1631 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1632 }
1633
2ac43334
RZ
1634#ifdef CONFIG_MBO
1635 if (wpa_s->enable_oce & OCE_STA)
1636 params.oce_scan = 1;
1637#endif /* CONFIG_MBO */
1638
7c865c68
TB
1639 scan_params = &params;
1640
1641scan:
32c02261
AS
1642 wpa_s->sched_scan_timed_out = 0;
1643
1644 /*
1645 * We cannot support multiple scan plans if the scan request includes
1646 * too many SSID's, so in this case use only the last scan plan and make
1647 * it run infinitely. It will be stopped by the timeout.
1648 */
1649 if (wpa_s->sched_scan_plans_num == 1 ||
1650 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1651 params.sched_scan_plans = wpa_s->sched_scan_plans;
1652 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1653 } else if (wpa_s->sched_scan_plans_num > 1) {
a8cb5a88 1654 wpa_dbg(wpa_s, MSG_DEBUG,
32c02261
AS
1655 "Too many SSIDs. Default to using single scheduled_scan plan");
1656 params.sched_scan_plans =
1657 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1658 1];
1659 params.sched_scan_plans_num = 1;
7d21a223 1660 } else {
32c02261
AS
1661 if (wpa_s->conf->sched_scan_interval)
1662 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1663 else
1664 scan_plan.interval = 10;
1665
1666 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1667 wpa_printf(MSG_WARNING,
1668 "Scan interval too long(%u), use the maximum allowed(%u)",
1669 scan_plan.interval,
1670 wpa_s->max_sched_scan_plan_interval);
1671 scan_plan.interval =
1672 wpa_s->max_sched_scan_plan_interval;
1673 }
1674
1675 scan_plan.iterations = 0;
1676 params.sched_scan_plans = &scan_plan;
1677 params.sched_scan_plans_num = 1;
1678 }
1679
d0330d57
PK
1680 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1681
32c02261 1682 if (ssid || !wpa_s->first_sched_scan) {
7d21a223 1683 wpa_dbg(wpa_s, MSG_DEBUG,
d0330d57
PK
1684 "Starting sched scan after %u seconds: interval %u timeout %d",
1685 params.sched_scan_start_delay,
32c02261
AS
1686 params.sched_scan_plans[0].interval,
1687 wpa_s->sched_scan_timeout);
1688 } else {
d0330d57
PK
1689 wpa_dbg(wpa_s, MSG_DEBUG,
1690 "Starting sched scan after %u seconds (no timeout)",
1691 params.sched_scan_start_delay);
a8cb5a88 1692 }
cbdf3507 1693
faf9a858
JM
1694 wpa_setband_scan_freqs(wpa_s, scan_params);
1695
346b333d 1696 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
bb66d467
EC
1697 wpa_s->wpa_state <= WPA_SCANNING)
1698 wpa_setup_mac_addr_rand_params(&params,
1699 wpa_s->mac_addr_sched_scan);
56c76fa5 1700
57c3a605 1701 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1702
32c02261 1703 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
a13e07ec 1704 wpabuf_free(extra_ie);
b59e6f26 1705 os_free(params.filter_ssids);
bb66d467 1706 os_free(params.mac_addr);
cbdf3507
LC
1707 if (ret) {
1708 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1709 if (prev_state != wpa_s->wpa_state)
1710 wpa_supplicant_set_state(wpa_s, prev_state);
1711 return ret;
1712 }
1713
1714 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1715 if (ssid || !wpa_s->first_sched_scan) {
1716 wpa_s->sched_scan_timed_out = 0;
1717 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1718 wpa_supplicant_sched_scan_timeout,
1719 wpa_s, NULL);
1720 wpa_s->first_sched_scan = 0;
1721 wpa_s->sched_scan_timeout /= 2;
32c02261
AS
1722 params.sched_scan_plans[0].interval *= 2;
1723 if ((unsigned int) wpa_s->sched_scan_timeout <
1724 params.sched_scan_plans[0].interval ||
1725 params.sched_scan_plans[0].interval >
1726 wpa_s->max_sched_scan_plan_interval) {
1727 params.sched_scan_plans[0].interval = 10;
a09fc1cc
DS
1728 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1729 }
cbdf3507
LC
1730 }
1731
7ed52f67
DS
1732 /* If there is no more ssids, start next time from the beginning */
1733 if (!ssid)
1734 wpa_s->prev_sched_ssid = NULL;
1735
cbdf3507
LC
1736 return 0;
1737}
1738
1739
6fc6879b
JM
1740/**
1741 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1742 * @wpa_s: Pointer to wpa_supplicant data
1743 *
1744 * This function is used to cancel a scan request scheduled with
1745 * wpa_supplicant_req_scan().
1746 */
1747void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1748{
f049052b 1749 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
6fc6879b
JM
1750 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1751}
cb8564b1
DW
1752
1753
831770bf
CZ
1754/**
1755 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1756 * @wpa_s: Pointer to wpa_supplicant data
1757 *
1758 * This function is used to stop a delayed scheduled scan.
1759 */
1760void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1761{
1762 if (!wpa_s->sched_scan_supported)
1763 return;
1764
1765 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1766 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1767 wpa_s, NULL);
1768}
1769
1770
cbdf3507
LC
1771/**
1772 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1773 * @wpa_s: Pointer to wpa_supplicant data
1774 *
1775 * This function is used to stop a periodic scheduled scan.
1776 */
1777void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1778{
1779 if (!wpa_s->sched_scanning)
1780 return;
1781
14f34a73
AS
1782 if (wpa_s->sched_scanning)
1783 wpa_s->sched_scan_stop_req = 1;
1784
cbdf3507
LC
1785 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1786 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1787 wpa_supplicant_stop_sched_scan(wpa_s);
1788}
1789
1790
2c09af30
JM
1791/**
1792 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1793 * @wpa_s: Pointer to wpa_supplicant data
1794 * @scanning: Whether scanning is currently in progress
1795 *
1796 * This function is to generate scanning notifycations. It is called whenever
1797 * there may have been a change in scanning (scan started, completed, stopped).
1798 * wpas_notify_scanning() is called whenever the scanning state changed from the
1799 * previously notified state.
1800 */
cb8564b1
DW
1801void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1802 int scanning)
1803{
1804 if (wpa_s->scanning != scanning) {
1805 wpa_s->scanning = scanning;
8bac466b 1806 wpas_notify_scanning(wpa_s);
cb8564b1
DW
1807 }
1808}
1809
9ba9fa07
JM
1810
1811static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1812{
1813 int rate = 0;
1814 const u8 *ie;
1815 int i;
1816
1817 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1818 for (i = 0; ie && i < ie[1]; i++) {
1819 if ((ie[i + 2] & 0x7f) > rate)
1820 rate = ie[i + 2] & 0x7f;
1821 }
1822
1823 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1824 for (i = 0; ie && i < ie[1]; i++) {
1825 if ((ie[i + 2] & 0x7f) > rate)
1826 rate = ie[i + 2] & 0x7f;
1827 }
1828
1829 return rate;
1830}
1831
1832
2c09af30
JM
1833/**
1834 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1835 * @res: Scan result entry
1836 * @ie: Information element identitifier (WLAN_EID_*)
1837 * Returns: Pointer to the information element (id field) or %NULL if not found
1838 *
1839 * This function returns the first matching information element in the scan
1840 * result.
1841 */
d1f9c410
JM
1842const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1843{
d044d2f7
EP
1844 size_t ie_len = res->ie_len;
1845
1846 /* Use the Beacon frame IEs if res->ie_len is not available */
1847 if (!ie_len)
1848 ie_len = res->beacon_ie_len;
1849
1850 return get_ie((const u8 *) (res + 1), ie_len, ie);
d1f9c410
JM
1851}
1852
1853
2c09af30
JM
1854/**
1855 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1856 * @res: Scan result entry
1857 * @vendor_type: Vendor type (four octets starting the IE payload)
1858 * Returns: Pointer to the information element (id field) or %NULL if not found
1859 *
1860 * This function returns the first matching information element in the scan
1861 * result.
1862 */
9ba9fa07
JM
1863const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1864 u32 vendor_type)
1865{
1866 const u8 *end, *pos;
1867
1868 pos = (const u8 *) (res + 1);
1869 end = pos + res->ie_len;
1870
904e977b
JM
1871 while (end - pos > 1) {
1872 if (2 + pos[1] > end - pos)
9ba9fa07
JM
1873 break;
1874 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1875 vendor_type == WPA_GET_BE32(&pos[2]))
1876 return pos;
1877 pos += 2 + pos[1];
1878 }
1879
1880 return NULL;
1881}
1882
1883
aaeb9c98
JM
1884/**
1885 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1886 * @res: Scan result entry
1887 * @vendor_type: Vendor type (four octets starting the IE payload)
1888 * Returns: Pointer to the information element (id field) or %NULL if not found
1889 *
1890 * This function returns the first matching information element in the scan
1891 * result.
1892 *
1893 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1894 * from Beacon frames instead of either Beacon or Probe Response frames.
1895 */
1896const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1897 u32 vendor_type)
1898{
1899 const u8 *end, *pos;
1900
1901 if (res->beacon_ie_len == 0)
1902 return NULL;
1903
1904 pos = (const u8 *) (res + 1);
1905 pos += res->ie_len;
1906 end = pos + res->beacon_ie_len;
1907
904e977b
JM
1908 while (end - pos > 1) {
1909 if (2 + pos[1] > end - pos)
aaeb9c98
JM
1910 break;
1911 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1912 vendor_type == WPA_GET_BE32(&pos[2]))
1913 return pos;
1914 pos += 2 + pos[1];
1915 }
1916
1917 return NULL;
1918}
1919
1920
2c09af30
JM
1921/**
1922 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1923 * @res: Scan result entry
1924 * @vendor_type: Vendor type (four octets starting the IE payload)
1925 * Returns: Pointer to the information element payload or %NULL if not found
1926 *
1927 * This function returns concatenated payload of possibly fragmented vendor
1928 * specific information elements in the scan result. The caller is responsible
1929 * for freeing the returned buffer.
1930 */
9ba9fa07
JM
1931struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1932 u32 vendor_type)
1933{
1934 struct wpabuf *buf;
1935 const u8 *end, *pos;
1936
1937 buf = wpabuf_alloc(res->ie_len);
1938 if (buf == NULL)
1939 return NULL;
1940
1941 pos = (const u8 *) (res + 1);
1942 end = pos + res->ie_len;
1943
904e977b
JM
1944 while (end - pos > 1) {
1945 if (2 + pos[1] > end - pos)
54f489be
JM
1946 break;
1947 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1948 vendor_type == WPA_GET_BE32(&pos[2]))
1949 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1950 pos += 2 + pos[1];
1951 }
1952
1953 if (wpabuf_len(buf) == 0) {
1954 wpabuf_free(buf);
1955 buf = NULL;
1956 }
1957
1958 return buf;
1959}
1960
1961
9ba9fa07
JM
1962/* Compare function for sorting scan results. Return >0 if @b is considered
1963 * better. */
1964static int wpa_scan_result_compar(const void *a, const void *b)
1965{
577db0ae 1966#define MIN(a,b) a < b ? a : b
9ba9fa07
JM
1967 struct wpa_scan_res **_wa = (void *) a;
1968 struct wpa_scan_res **_wb = (void *) b;
1969 struct wpa_scan_res *wa = *_wa;
1970 struct wpa_scan_res *wb = *_wb;
a1b790eb
JM
1971 int wpa_a, wpa_b;
1972 int snr_a, snr_b, snr_a_full, snr_b_full;
9ba9fa07
JM
1973
1974 /* WPA/WPA2 support preferred */
1975 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1976 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1977 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1978 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1979
1980 if (wpa_b && !wpa_a)
1981 return 1;
1982 if (!wpa_b && wpa_a)
1983 return -1;
1984
1985 /* privacy support preferred */
1986 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1987 (wb->caps & IEEE80211_CAP_PRIVACY))
1988 return 1;
1989 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1990 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1991 return -1;
1992
f0d0a5d2 1993 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
a1b790eb
JM
1994 snr_a_full = wa->snr;
1995 snr_a = MIN(wa->snr, GREAT_SNR);
1996 snr_b_full = wb->snr;
aa517ae2 1997 snr_b = MIN(wb->snr, GREAT_SNR);
577db0ae 1998 } else {
f0d0a5d2
MA
1999 /* Level is not in dBm, so we can't calculate
2000 * SNR. Just use raw level (units unknown). */
a1b790eb
JM
2001 snr_a = snr_a_full = wa->level;
2002 snr_b = snr_b_full = wb->level;
577db0ae
GM
2003 }
2004
f0d0a5d2 2005 /* if SNR is close, decide by max rate or frequency band */
b4d56efb 2006 if (snr_a && snr_b && abs(snr_b - snr_a) < 7) {
a1b790eb 2007 if (wa->est_throughput != wb->est_throughput)
ec2e7c4c
JM
2008 return (int) wb->est_throughput -
2009 (int) wa->est_throughput;
b4d56efb
JM
2010 }
2011 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2012 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
577db0ae
GM
2013 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2014 return IS_5GHZ(wa->freq) ? -1 : 1;
9ba9fa07
JM
2015 }
2016
577db0ae 2017 /* all things being equal, use SNR; if SNRs are
9ba9fa07
JM
2018 * identical, use quality values since some drivers may only report
2019 * that value and leave the signal level zero */
a1b790eb 2020 if (snr_b_full == snr_a_full)
9ba9fa07 2021 return wb->qual - wa->qual;
a1b790eb 2022 return snr_b_full - snr_a_full;
577db0ae 2023#undef MIN
9ba9fa07
JM
2024}
2025
2026
41e650ae
JM
2027#ifdef CONFIG_WPS
2028/* Compare function for sorting scan results when searching a WPS AP for
2029 * provisioning. Return >0 if @b is considered better. */
2030static int wpa_scan_result_wps_compar(const void *a, const void *b)
2031{
2032 struct wpa_scan_res **_wa = (void *) a;
2033 struct wpa_scan_res **_wb = (void *) b;
2034 struct wpa_scan_res *wa = *_wa;
2035 struct wpa_scan_res *wb = *_wb;
2036 int uses_wps_a, uses_wps_b;
2037 struct wpabuf *wps_a, *wps_b;
2038 int res;
2039
2040 /* Optimization - check WPS IE existence before allocated memory and
2041 * doing full reassembly. */
2042 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2043 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2044 if (uses_wps_a && !uses_wps_b)
2045 return -1;
2046 if (!uses_wps_a && uses_wps_b)
2047 return 1;
2048
2049 if (uses_wps_a && uses_wps_b) {
2050 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2051 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2052 res = wps_ap_priority_compar(wps_a, wps_b);
2053 wpabuf_free(wps_a);
2054 wpabuf_free(wps_b);
2055 if (res)
2056 return res;
2057 }
2058
2059 /*
2060 * Do not use current AP security policy as a sorting criteria during
2061 * WPS provisioning step since the AP may get reconfigured at the
2062 * completion of provisioning.
2063 */
2064
2065 /* all things being equal, use signal level; if signal levels are
2066 * identical, use quality values since some drivers may only report
2067 * that value and leave the signal level zero */
2068 if (wb->level == wa->level)
2069 return wb->qual - wa->qual;
2070 return wb->level - wa->level;
2071}
2072#endif /* CONFIG_WPS */
2073
2074
aa820e02
JM
2075static void dump_scan_res(struct wpa_scan_results *scan_res)
2076{
76202aed 2077#ifndef CONFIG_NO_STDOUT_DEBUG
aa820e02
JM
2078 size_t i;
2079
2080 if (scan_res->res == NULL || scan_res->num == 0)
2081 return;
2082
2083 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2084
2085 for (i = 0; i < scan_res->num; i++) {
2086 struct wpa_scan_res *r = scan_res->res[i];
1cbe86e2 2087 u8 *pos;
f0d0a5d2 2088 if (r->flags & WPA_SCAN_LEVEL_DBM) {
f0d0a5d2
MA
2089 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2090
aa820e02 2091 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
a1b790eb 2092 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
aa820e02 2093 MAC2STR(r->bssid), r->freq, r->qual,
f0d0a5d2 2094 r->noise, noise_valid ? "" : "~", r->level,
a1b790eb
JM
2095 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2096 r->flags,
2097 r->age, r->est_throughput);
aa820e02
JM
2098 } else {
2099 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
a1b790eb 2100 "noise=%d level=%d flags=0x%x age=%u est=%u",
aa820e02 2101 MAC2STR(r->bssid), r->freq, r->qual,
a1b790eb
JM
2102 r->noise, r->level, r->flags, r->age,
2103 r->est_throughput);
aa820e02 2104 }
1cbe86e2
JM
2105 pos = (u8 *) (r + 1);
2106 if (r->ie_len)
2107 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2108 pos += r->ie_len;
2109 if (r->beacon_ie_len)
2110 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2111 pos, r->beacon_ie_len);
aa820e02 2112 }
76202aed 2113#endif /* CONFIG_NO_STDOUT_DEBUG */
aa820e02
JM
2114}
2115
2116
2c09af30
JM
2117/**
2118 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2119 * @wpa_s: Pointer to wpa_supplicant data
2120 * @bssid: BSSID to check
2121 * Returns: 0 if the BSSID is filtered or 1 if not
2122 *
2123 * This function is used to filter out specific BSSIDs from scan reslts mainly
2124 * for testing purposes (SET bssid_filter ctrl_iface command).
2125 */
d445a5cd
JM
2126int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2127 const u8 *bssid)
2128{
2129 size_t i;
2130
2131 if (wpa_s->bssid_filter == NULL)
2132 return 1;
2133
2134 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2135 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2136 ETH_ALEN) == 0)
2137 return 1;
2138 }
2139
2140 return 0;
2141}
2142
2143
2f195639
KV
2144void filter_scan_res(struct wpa_supplicant *wpa_s,
2145 struct wpa_scan_results *res)
d445a5cd
JM
2146{
2147 size_t i, j;
2148
2149 if (wpa_s->bssid_filter == NULL)
2150 return;
2151
2152 for (i = 0, j = 0; i < res->num; i++) {
2153 if (wpa_supplicant_filter_bssid_match(wpa_s,
2154 res->res[i]->bssid)) {
2155 res->res[j++] = res->res[i];
2156 } else {
2157 os_free(res->res[i]);
2158 res->res[i] = NULL;
2159 }
2160 }
2161
2162 if (res->num != j) {
2163 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2164 (int) (res->num - j));
2165 res->num = j;
2166 }
2167}
2168
2169
2f195639 2170void scan_snr(struct wpa_scan_res *res)
a1b790eb
JM
2171{
2172 if (res->flags & WPA_SCAN_NOISE_INVALID) {
2173 res->noise = IS_5GHZ(res->freq) ?
2174 DEFAULT_NOISE_FLOOR_5GHZ :
2175 DEFAULT_NOISE_FLOOR_2GHZ;
2176 }
2177
2178 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2179 res->snr = res->level - res->noise;
2180 } else {
2181 /* Level is not in dBm, so we can't calculate
2182 * SNR. Just use raw level (units unknown). */
2183 res->snr = res->level;
2184 }
2185}
2186
2187
2188static unsigned int max_ht20_rate(int snr)
2189{
2190 if (snr < 6)
2191 return 6500; /* HT20 MCS0 */
2192 if (snr < 8)
2193 return 13000; /* HT20 MCS1 */
2194 if (snr < 13)
2195 return 19500; /* HT20 MCS2 */
2196 if (snr < 17)
2197 return 26000; /* HT20 MCS3 */
2198 if (snr < 20)
2199 return 39000; /* HT20 MCS4 */
2200 if (snr < 23)
2201 return 52000; /* HT20 MCS5 */
2202 if (snr < 24)
2203 return 58500; /* HT20 MCS6 */
2204 return 65000; /* HT20 MCS7 */
2205}
2206
2207
2208static unsigned int max_ht40_rate(int snr)
2209{
2210 if (snr < 3)
2211 return 13500; /* HT40 MCS0 */
2212 if (snr < 6)
2213 return 27000; /* HT40 MCS1 */
2214 if (snr < 10)
2215 return 40500; /* HT40 MCS2 */
2216 if (snr < 15)
2217 return 54000; /* HT40 MCS3 */
2218 if (snr < 17)
2219 return 81000; /* HT40 MCS4 */
2220 if (snr < 22)
2221 return 108000; /* HT40 MCS5 */
8b2b718d 2222 if (snr < 24)
a1b790eb
JM
2223 return 121500; /* HT40 MCS6 */
2224 return 135000; /* HT40 MCS7 */
2225}
2226
2227
2228static unsigned int max_vht80_rate(int snr)
2229{
2230 if (snr < 1)
2231 return 0;
2232 if (snr < 2)
2233 return 29300; /* VHT80 MCS0 */
2234 if (snr < 5)
2235 return 58500; /* VHT80 MCS1 */
2236 if (snr < 9)
2237 return 87800; /* VHT80 MCS2 */
2238 if (snr < 11)
2239 return 117000; /* VHT80 MCS3 */
2240 if (snr < 15)
2241 return 175500; /* VHT80 MCS4 */
2242 if (snr < 16)
2243 return 234000; /* VHT80 MCS5 */
2244 if (snr < 18)
2245 return 263300; /* VHT80 MCS6 */
2246 if (snr < 20)
2247 return 292500; /* VHT80 MCS7 */
2248 if (snr < 22)
2249 return 351000; /* VHT80 MCS8 */
2250 return 390000; /* VHT80 MCS9 */
2251}
2252
2253
ad06ac0b
EG
2254unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2255 const u8 *ies, size_t ies_len, int rate,
2256 int snr)
a1b790eb
JM
2257{
2258 enum local_hw_capab capab = wpa_s->hw_capab;
a1b790eb 2259 unsigned int est, tmp;
ad06ac0b 2260 const u8 *ie;
a1b790eb
JM
2261
2262 /* Limit based on estimated SNR */
2263 if (rate > 1 * 2 && snr < 1)
2264 rate = 1 * 2;
2265 else if (rate > 2 * 2 && snr < 4)
2266 rate = 2 * 2;
2267 else if (rate > 6 * 2 && snr < 5)
2268 rate = 6 * 2;
2269 else if (rate > 9 * 2 && snr < 6)
2270 rate = 9 * 2;
2271 else if (rate > 12 * 2 && snr < 7)
2272 rate = 12 * 2;
2273 else if (rate > 18 * 2 && snr < 10)
2274 rate = 18 * 2;
2275 else if (rate > 24 * 2 && snr < 11)
2276 rate = 24 * 2;
2277 else if (rate > 36 * 2 && snr < 15)
2278 rate = 36 * 2;
2279 else if (rate > 48 * 2 && snr < 19)
2280 rate = 48 * 2;
2281 else if (rate > 54 * 2 && snr < 21)
2282 rate = 54 * 2;
2283 est = rate * 500;
2284
2285 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
ad06ac0b 2286 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
a1b790eb
JM
2287 if (ie) {
2288 tmp = max_ht20_rate(snr);
2289 if (tmp > est)
2290 est = tmp;
2291 }
2292 }
2293
2294 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
ad06ac0b 2295 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
a1b790eb
JM
2296 if (ie && ie[1] >= 2 &&
2297 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2298 tmp = max_ht40_rate(snr);
2299 if (tmp > est)
2300 est = tmp;
2301 }
2302 }
2303
2304 if (capab == CAPAB_VHT) {
2305 /* Use +1 to assume VHT is always faster than HT */
ad06ac0b 2306 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
a1b790eb
JM
2307 if (ie) {
2308 tmp = max_ht20_rate(snr) + 1;
2309 if (tmp > est)
2310 est = tmp;
2311
ad06ac0b 2312 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
a1b790eb
JM
2313 if (ie && ie[1] >= 2 &&
2314 (ie[3] &
2315 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2316 tmp = max_ht40_rate(snr) + 1;
2317 if (tmp > est)
2318 est = tmp;
2319 }
2320
ad06ac0b 2321 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
a1b790eb
JM
2322 if (ie && ie[1] >= 1 &&
2323 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2324 tmp = max_vht80_rate(snr) + 1;
2325 if (tmp > est)
2326 est = tmp;
2327 }
2328 }
2329 }
2330
ad06ac0b
EG
2331 return est;
2332}
2333
2334
2335void scan_est_throughput(struct wpa_supplicant *wpa_s,
2336 struct wpa_scan_res *res)
2337{
2338 int rate; /* max legacy rate in 500 kb/s units */
2339 int snr = res->snr;
2340 const u8 *ies = (const void *) (res + 1);
2341 size_t ie_len = res->ie_len;
2342
2343 if (res->est_throughput)
2344 return;
2345
2346 /* Get maximum legacy rate */
2347 rate = wpa_scan_get_max_rate(res);
a1b790eb 2348
ad06ac0b
EG
2349 if (!ie_len)
2350 ie_len = res->beacon_ie_len;
2351 res->est_throughput =
fee28410 2352 wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr);
ad06ac0b
EG
2353
2354 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
a1b790eb
JM
2355}
2356
2357
9ba9fa07
JM
2358/**
2359 * wpa_supplicant_get_scan_results - Get scan results
2360 * @wpa_s: Pointer to wpa_supplicant data
2361 * @info: Information about what was scanned or %NULL if not available
2362 * @new_scan: Whether a new scan was performed
2363 * Returns: Scan results, %NULL on failure
2364 *
2365 * This function request the current scan results from the driver and updates
2366 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2367 * results with wpa_scan_results_free().
2368 */
2369struct wpa_scan_results *
2370wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2371 struct scan_info *info, int new_scan)
2372{
2373 struct wpa_scan_results *scan_res;
2374 size_t i;
41e650ae 2375 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
9ba9fa07 2376
17fbb751 2377 scan_res = wpa_drv_get_scan_results2(wpa_s);
9ba9fa07 2378 if (scan_res == NULL) {
f049052b 2379 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
9ba9fa07
JM
2380 return NULL;
2381 }
c5f10e80
JM
2382 if (scan_res->fetch_time.sec == 0) {
2383 /*
2384 * Make sure we have a valid timestamp if the driver wrapper
2385 * does not set this.
2386 */
acb69cec 2387 os_get_reltime(&scan_res->fetch_time);
c5f10e80 2388 }
d445a5cd 2389 filter_scan_res(wpa_s, scan_res);
9ba9fa07 2390
f0d0a5d2
MA
2391 for (i = 0; i < scan_res->num; i++) {
2392 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2393
a1b790eb
JM
2394 scan_snr(scan_res_item);
2395 scan_est_throughput(wpa_s, scan_res_item);
f0d0a5d2
MA
2396 }
2397
41e650ae 2398#ifdef CONFIG_WPS
3187fd90 2399 if (wpas_wps_searching(wpa_s)) {
f049052b
BG
2400 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2401 "provisioning rules");
41e650ae
JM
2402 compar = wpa_scan_result_wps_compar;
2403 }
2404#endif /* CONFIG_WPS */
2405
33111c91
JC
2406 if (scan_res->res) {
2407 qsort(scan_res->res, scan_res->num,
2408 sizeof(struct wpa_scan_res *), compar);
2409 }
aa820e02 2410 dump_scan_res(scan_res);
9ba9fa07 2411
6e374bd4
JM
2412 if (wpa_s->ignore_post_flush_scan_res) {
2413 /* FLUSH command aborted an ongoing scan and these are the
2414 * results from the aborted scan. Do not process the results to
2415 * maintain flushed state. */
2416 wpa_dbg(wpa_s, MSG_DEBUG,
2417 "Do not update BSS table based on pending post-FLUSH scan results");
2418 wpa_s->ignore_post_flush_scan_res = 0;
2419 return scan_res;
2420 }
2421
9ba9fa07
JM
2422 wpa_bss_update_start(wpa_s);
2423 for (i = 0; i < scan_res->num; i++)
c5f10e80
JM
2424 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2425 &scan_res->fetch_time);
9ba9fa07
JM
2426 wpa_bss_update_end(wpa_s, info, new_scan);
2427
2428 return scan_res;
2429}
2430
2431
2c09af30
JM
2432/**
2433 * wpa_supplicant_update_scan_results - Update scan results from the driver
2434 * @wpa_s: Pointer to wpa_supplicant data
2435 * Returns: 0 on success, -1 on failure
2436 *
2437 * This function updates the BSS table within wpa_supplicant based on the
2438 * currently available scan results from the driver without requesting a new
2439 * scan. This is used in cases where the driver indicates an association
2440 * (including roaming within ESS) and wpa_supplicant does not yet have the
2441 * needed information to complete the connection (e.g., to perform validation
2442 * steps in 4-way handshake).
2443 */
9ba9fa07
JM
2444int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2445{
2446 struct wpa_scan_results *scan_res;
2447 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2448 if (scan_res == NULL)
2449 return -1;
2450 wpa_scan_results_free(scan_res);
2451
2452 return 0;
2453}
66fe0f70
DS
2454
2455
2456/**
2457 * scan_only_handler - Reports scan results
2458 */
2459void scan_only_handler(struct wpa_supplicant *wpa_s,
2460 struct wpa_scan_results *scan_res)
2461{
2462 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
d81c73be
JM
2463 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2464 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2465 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2466 wpa_s->manual_scan_id);
2467 wpa_s->manual_scan_use_id = 0;
2468 } else {
2469 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2470 }
66fe0f70
DS
2471 wpas_notify_scan_results(wpa_s);
2472 wpas_notify_scan_done(wpa_s, 1);
d12a51b5
JM
2473 if (wpa_s->scan_work) {
2474 struct wpa_radio_work *work = wpa_s->scan_work;
2475 wpa_s->scan_work = NULL;
2476 radio_work_done(work);
2477 }
ea6030c7
JM
2478
2479 if (wpa_s->wpa_state == WPA_SCANNING)
2480 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
66fe0f70 2481}
407be00b
JM
2482
2483
2484int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2485{
2486 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2487}
dd43aaa5
JM
2488
2489
2490struct wpa_driver_scan_params *
2491wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2492{
2493 struct wpa_driver_scan_params *params;
2494 size_t i;
2495 u8 *n;
2496
2497 params = os_zalloc(sizeof(*params));
2498 if (params == NULL)
2499 return NULL;
2500
2501 for (i = 0; i < src->num_ssids; i++) {
2502 if (src->ssids[i].ssid) {
a1f11e34
JB
2503 n = os_memdup(src->ssids[i].ssid,
2504 src->ssids[i].ssid_len);
dd43aaa5
JM
2505 if (n == NULL)
2506 goto failed;
dd43aaa5
JM
2507 params->ssids[i].ssid = n;
2508 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2509 }
2510 }
2511 params->num_ssids = src->num_ssids;
2512
2513 if (src->extra_ies) {
a1f11e34 2514 n = os_memdup(src->extra_ies, src->extra_ies_len);
dd43aaa5
JM
2515 if (n == NULL)
2516 goto failed;
dd43aaa5
JM
2517 params->extra_ies = n;
2518 params->extra_ies_len = src->extra_ies_len;
2519 }
2520
2521 if (src->freqs) {
2522 int len = int_array_len(src->freqs);
a1f11e34 2523 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
dd43aaa5
JM
2524 if (params->freqs == NULL)
2525 goto failed;
dd43aaa5
JM
2526 }
2527
2528 if (src->filter_ssids) {
a1f11e34
JB
2529 params->filter_ssids = os_memdup(src->filter_ssids,
2530 sizeof(*params->filter_ssids) *
dd43aaa5
JM
2531 src->num_filter_ssids);
2532 if (params->filter_ssids == NULL)
2533 goto failed;
dd43aaa5
JM
2534 params->num_filter_ssids = src->num_filter_ssids;
2535 }
2536
2537 params->filter_rssi = src->filter_rssi;
2538 params->p2p_probe = src->p2p_probe;
2539 params->only_new_results = src->only_new_results;
57a8f8af 2540 params->low_priority = src->low_priority;
c16b9f8d
AS
2541 params->duration = src->duration;
2542 params->duration_mandatory = src->duration_mandatory;
938dd97a 2543 params->oce_scan = src->oce_scan;
dd43aaa5 2544
09ea4309
AS
2545 if (src->sched_scan_plans_num > 0) {
2546 params->sched_scan_plans =
a1f11e34
JB
2547 os_memdup(src->sched_scan_plans,
2548 sizeof(*src->sched_scan_plans) *
09ea4309
AS
2549 src->sched_scan_plans_num);
2550 if (!params->sched_scan_plans)
2551 goto failed;
2552
09ea4309
AS
2553 params->sched_scan_plans_num = src->sched_scan_plans_num;
2554 }
2555
bb66d467
EC
2556 if (src->mac_addr_rand &&
2557 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
2558 goto failed;
eb20cea5
JM
2559
2560 if (src->bssid) {
2561 u8 *bssid;
2562
a1f11e34 2563 bssid = os_memdup(src->bssid, ETH_ALEN);
eb20cea5
JM
2564 if (!bssid)
2565 goto failed;
eb20cea5
JM
2566 params->bssid = bssid;
2567 }
2568
57c3a605 2569 params->relative_rssi_set = src->relative_rssi_set;
2570 params->relative_rssi = src->relative_rssi;
2571 params->relative_adjust_band = src->relative_adjust_band;
2572 params->relative_adjust_rssi = src->relative_adjust_rssi;
dd43aaa5
JM
2573 return params;
2574
2575failed:
2576 wpa_scan_free_params(params);
2577 return NULL;
2578}
2579
2580
2581void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2582{
2583 size_t i;
2584
2585 if (params == NULL)
2586 return;
2587
2588 for (i = 0; i < params->num_ssids; i++)
2589 os_free((u8 *) params->ssids[i].ssid);
2590 os_free((u8 *) params->extra_ies);
2591 os_free(params->freqs);
2592 os_free(params->filter_ssids);
09ea4309 2593 os_free(params->sched_scan_plans);
56c76fa5
IP
2594
2595 /*
2596 * Note: params->mac_addr_mask points to same memory allocation and
2597 * must not be freed separately.
2598 */
2599 os_free((u8 *) params->mac_addr);
2600
eb20cea5
JM
2601 os_free((u8 *) params->bssid);
2602
dd43aaa5
JM
2603 os_free(params);
2604}
737e7a08
AB
2605
2606
2607int wpas_start_pno(struct wpa_supplicant *wpa_s)
2608{
32c02261 2609 int ret, prio;
4ed34922 2610 size_t i, num_ssid, num_match_ssid;
737e7a08
AB
2611 struct wpa_ssid *ssid;
2612 struct wpa_driver_scan_params params;
32c02261 2613 struct sched_scan_plan scan_plan;
f89c32e6 2614 unsigned int max_sched_scan_ssids;
737e7a08 2615
5e3ddf4d
AB
2616 if (!wpa_s->sched_scan_supported)
2617 return -1;
2618
f89c32e6
DS
2619 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2620 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2621 else
2622 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2623 if (max_sched_scan_ssids < 1)
2624 return -1;
2625
737e7a08
AB
2626 if (wpa_s->pno || wpa_s->pno_sched_pending)
2627 return 0;
2628
2629 if ((wpa_s->wpa_state > WPA_SCANNING) &&
b4fd1f0e 2630 (wpa_s->wpa_state < WPA_COMPLETED)) {
737e7a08
AB
2631 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2632 return -EAGAIN;
2633 }
2634
2635 if (wpa_s->wpa_state == WPA_SCANNING) {
2636 wpa_supplicant_cancel_scan(wpa_s);
2637 if (wpa_s->sched_scanning) {
2638 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2639 "ongoing sched scan");
2640 wpa_supplicant_cancel_sched_scan(wpa_s);
2641 wpa_s->pno_sched_pending = 1;
2642 return 0;
2643 }
2644 }
2645
5ac8f862
AS
2646 if (wpa_s->sched_scan_stop_req) {
2647 wpa_printf(MSG_DEBUG,
2648 "Schedule PNO after previous sched scan has stopped");
2649 wpa_s->pno_sched_pending = 1;
2650 return 0;
2651 }
2652
737e7a08
AB
2653 os_memset(&params, 0, sizeof(params));
2654
4ed34922 2655 num_ssid = num_match_ssid = 0;
737e7a08
AB
2656 ssid = wpa_s->conf->ssid;
2657 while (ssid) {
4ed34922
DS
2658 if (!wpas_network_disabled(wpa_s, ssid)) {
2659 num_match_ssid++;
2660 if (ssid->scan_ssid)
2661 num_ssid++;
2662 }
737e7a08
AB
2663 ssid = ssid->next;
2664 }
4ed34922
DS
2665
2666 if (num_match_ssid == 0) {
2667 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2668 return -1;
2669 }
2670
2671 if (num_match_ssid > num_ssid) {
2672 params.num_ssids++; /* wildcard */
2673 num_ssid++;
2674 }
2675
f89c32e6 2676 if (num_ssid > max_sched_scan_ssids) {
737e7a08 2677 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
f89c32e6
DS
2678 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2679 num_ssid = max_sched_scan_ssids;
737e7a08
AB
2680 }
2681
4ed34922
DS
2682 if (num_match_ssid > wpa_s->max_match_sets) {
2683 num_match_ssid = wpa_s->max_match_sets;
2684 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
737e7a08 2685 }
4ed34922
DS
2686 params.filter_ssids = os_calloc(num_match_ssid,
2687 sizeof(struct wpa_driver_scan_filter));
737e7a08
AB
2688 if (params.filter_ssids == NULL)
2689 return -1;
6f5e1b0b 2690
737e7a08 2691 i = 0;
6f5e1b0b
DS
2692 prio = 0;
2693 ssid = wpa_s->conf->pssid[prio];
737e7a08
AB
2694 while (ssid) {
2695 if (!wpas_network_disabled(wpa_s, ssid)) {
4ed34922
DS
2696 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2697 params.ssids[params.num_ssids].ssid =
2698 ssid->ssid;
2699 params.ssids[params.num_ssids].ssid_len =
2700 ssid->ssid_len;
2701 params.num_ssids++;
2702 }
737e7a08
AB
2703 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2704 ssid->ssid_len);
2705 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2706 params.num_filter_ssids++;
2707 i++;
4ed34922 2708 if (i == num_match_ssid)
737e7a08
AB
2709 break;
2710 }
6f5e1b0b
DS
2711 if (ssid->pnext)
2712 ssid = ssid->pnext;
2713 else if (prio + 1 == wpa_s->conf->num_prio)
2714 break;
2715 else
2716 ssid = wpa_s->conf->pssid[++prio];
737e7a08
AB
2717 }
2718
2719 if (wpa_s->conf->filter_rssi)
2720 params.filter_rssi = wpa_s->conf->filter_rssi;
2721
32c02261
AS
2722 if (wpa_s->sched_scan_plans_num) {
2723 params.sched_scan_plans = wpa_s->sched_scan_plans;
2724 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2725 } else {
2726 /* Set one scan plan that will run infinitely */
2727 if (wpa_s->conf->sched_scan_interval)
2728 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2729 else
2730 scan_plan.interval = 10;
2731
2732 scan_plan.iterations = 0;
2733 params.sched_scan_plans = &scan_plan;
2734 params.sched_scan_plans_num = 1;
2735 }
737e7a08 2736
d0330d57
PK
2737 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
2738
d3c9c35f
DS
2739 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2740 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2741 params.freqs = wpa_s->manual_sched_scan_freqs;
2742 }
2743
346b333d 2744 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
bb66d467
EC
2745 wpa_s->wpa_state <= WPA_SCANNING)
2746 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
56c76fa5 2747
57c3a605 2748 wpa_scan_set_relative_rssi_params(wpa_s, &params);
2749
32c02261 2750 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
737e7a08 2751 os_free(params.filter_ssids);
bb66d467 2752 os_free(params.mac_addr);
737e7a08
AB
2753 if (ret == 0)
2754 wpa_s->pno = 1;
5e3ddf4d
AB
2755 else
2756 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
737e7a08
AB
2757 return ret;
2758}
2759
2760
2761int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2762{
2763 int ret = 0;
2764
5e3ddf4d
AB
2765 if (!wpa_s->pno)
2766 return 0;
2767
2768 ret = wpa_supplicant_stop_sched_scan(wpa_s);
14f34a73 2769 wpa_s->sched_scan_stop_req = 1;
737e7a08 2770
5e3ddf4d 2771 wpa_s->pno = 0;
737e7a08
AB
2772 wpa_s->pno_sched_pending = 0;
2773
2774 if (wpa_s->wpa_state == WPA_SCANNING)
2775 wpa_supplicant_req_scan(wpa_s, 0, 0);
2776
2777 return ret;
2778}
56c76fa5
IP
2779
2780
2781void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2782 unsigned int type)
2783{
2784 type &= MAC_ADDR_RAND_ALL;
2785 wpa_s->mac_addr_rand_enable &= ~type;
2786
2787 if (type & MAC_ADDR_RAND_SCAN) {
2788 os_free(wpa_s->mac_addr_scan);
2789 wpa_s->mac_addr_scan = NULL;
2790 }
2791
2792 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2793 os_free(wpa_s->mac_addr_sched_scan);
2794 wpa_s->mac_addr_sched_scan = NULL;
2795 }
2796
2797 if (type & MAC_ADDR_RAND_PNO) {
2798 os_free(wpa_s->mac_addr_pno);
2799 wpa_s->mac_addr_pno = NULL;
2800 }
2801}
2802
2803
2804int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2805 unsigned int type, const u8 *addr,
2806 const u8 *mask)
2807{
2808 u8 *tmp = NULL;
2809
1e591df0
LD
2810 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
2811 wpa_printf(MSG_INFO,
2812 "scan: MAC randomization type %u != supported=%u",
2813 type, wpa_s->mac_addr_rand_supported);
2814 return -1;
2815 }
2816
56c76fa5
IP
2817 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2818
2819 if (addr) {
2820 tmp = os_malloc(2 * ETH_ALEN);
2821 if (!tmp)
2822 return -1;
2823 os_memcpy(tmp, addr, ETH_ALEN);
2824 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2825 }
2826
2827 if (type == MAC_ADDR_RAND_SCAN) {
2828 wpa_s->mac_addr_scan = tmp;
2829 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2830 wpa_s->mac_addr_sched_scan = tmp;
2831 } else if (type == MAC_ADDR_RAND_PNO) {
2832 wpa_s->mac_addr_pno = tmp;
2833 } else {
2834 wpa_printf(MSG_INFO,
2835 "scan: Invalid MAC randomization type=0x%x",
2836 type);
2837 os_free(tmp);
2838 return -1;
2839 }
2840
2841 wpa_s->mac_addr_rand_enable |= type;
2842 return 0;
2843}
4ead7cfd
KV
2844
2845
10f8351d
EC
2846int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
2847 unsigned int type, u8 *mask)
2848{
2849 const u8 *to_copy;
2850
2851 if ((wpa_s->mac_addr_rand_enable & type) != type)
2852 return -1;
2853
2854 if (type == MAC_ADDR_RAND_SCAN) {
2855 to_copy = wpa_s->mac_addr_scan;
2856 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2857 to_copy = wpa_s->mac_addr_sched_scan;
2858 } else if (type == MAC_ADDR_RAND_PNO) {
2859 to_copy = wpa_s->mac_addr_pno;
2860 } else {
2861 wpa_printf(MSG_DEBUG,
2862 "scan: Invalid MAC randomization type=0x%x",
2863 type);
2864 return -1;
2865 }
2866
2867 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
2868 return 0;
2869}
2870
2871
4ead7cfd
KV
2872int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2873{
bf88401d
SD
2874 struct wpa_radio_work *work;
2875 struct wpa_radio *radio = wpa_s->radio;
a8057310 2876
bf88401d
SD
2877 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
2878 if (work->wpa_s != wpa_s || !work->started ||
2879 (os_strcmp(work->type, "scan") != 0 &&
2880 os_strcmp(work->type, "p2p-scan") != 0))
2881 continue;
4ead7cfd 2882 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
bf88401d 2883 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
4ead7cfd
KV
2884 }
2885
bf88401d
SD
2886 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
2887 return -1;
4ead7cfd 2888}
32c02261
AS
2889
2890
2891int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2892{
2893 struct sched_scan_plan *scan_plans = NULL;
2894 const char *token, *context = NULL;
2895 unsigned int num = 0;
2896
2897 if (!cmd)
2898 return -1;
2899
bea48f77
JM
2900 if (!cmd[0]) {
2901 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2902 os_free(wpa_s->sched_scan_plans);
2903 wpa_s->sched_scan_plans = NULL;
2904 wpa_s->sched_scan_plans_num = 0;
2905 return 0;
2906 }
2907
32c02261
AS
2908 while ((token = cstr_token(cmd, " ", &context))) {
2909 int ret;
2910 struct sched_scan_plan *scan_plan, *n;
2911
2912 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2913 if (!n)
2914 goto fail;
2915
2916 scan_plans = n;
2917 scan_plan = &scan_plans[num];
2918 num++;
2919
2920 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2921 &scan_plan->iterations);
2922 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2923 wpa_printf(MSG_ERROR,
2924 "Invalid sched scan plan input: %s", token);
2925 goto fail;
2926 }
2927
32c02261
AS
2928 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2929 wpa_printf(MSG_WARNING,
2930 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2931 num, scan_plan->interval,
2932 wpa_s->max_sched_scan_plan_interval);
2933 scan_plan->interval =
2934 wpa_s->max_sched_scan_plan_interval;
2935 }
2936
2937 if (ret == 1) {
2938 scan_plan->iterations = 0;
2939 break;
2940 }
2941
2942 if (!scan_plan->iterations) {
2943 wpa_printf(MSG_ERROR,
2944 "scan plan %u: Number of iterations cannot be zero",
2945 num);
2946 goto fail;
2947 }
2948
2949 if (scan_plan->iterations >
2950 wpa_s->max_sched_scan_plan_iterations) {
2951 wpa_printf(MSG_WARNING,
2952 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2953 num, scan_plan->iterations,
2954 wpa_s->max_sched_scan_plan_iterations);
2955 scan_plan->iterations =
2956 wpa_s->max_sched_scan_plan_iterations;
2957 }
2958
2959 wpa_printf(MSG_DEBUG,
2960 "scan plan %u: interval=%u iterations=%u",
2961 num, scan_plan->interval, scan_plan->iterations);
2962 }
2963
2964 if (!scan_plans) {
2965 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2966 goto fail;
2967 }
2968
2969 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2970 wpa_printf(MSG_ERROR,
2971 "All scan plans but the last must specify a number of iterations");
2972 goto fail;
2973 }
2974
2975 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2976 num, scan_plans[num - 1].interval);
2977
2978 if (num > wpa_s->max_sched_scan_plans) {
2979 wpa_printf(MSG_WARNING,
2980 "Too many scheduled scan plans (only %u supported)",
2981 wpa_s->max_sched_scan_plans);
2982 wpa_printf(MSG_WARNING,
2983 "Use only the first %u scan plans, and the last one (in infinite loop)",
2984 wpa_s->max_sched_scan_plans - 1);
2985 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2986 &scan_plans[num - 1], sizeof(*scan_plans));
2987 num = wpa_s->max_sched_scan_plans;
2988 }
2989
2990 os_free(wpa_s->sched_scan_plans);
2991 wpa_s->sched_scan_plans = scan_plans;
2992 wpa_s->sched_scan_plans_num = num;
2993
2994 return 0;
2995
2996fail:
2997 os_free(scan_plans);
2998 wpa_printf(MSG_ERROR, "invalid scan plans list");
2999 return -1;
3000}
be7ebd89
MS
3001
3002
3003/**
3004 * wpas_scan_reset_sched_scan - Reset sched_scan state
3005 * @wpa_s: Pointer to wpa_supplicant data
3006 *
3007 * This function is used to cancel a running scheduled scan and to reset an
3008 * internal scan state to continue with a regular scan on the following
3009 * wpa_supplicant_req_scan() calls.
3010 */
3011void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3012{
3013 wpa_s->normal_scans = 0;
3014 if (wpa_s->sched_scanning) {
3015 wpa_s->sched_scan_timed_out = 0;
3016 wpa_s->prev_sched_ssid = NULL;
3017 wpa_supplicant_cancel_sched_scan(wpa_s);
3018 }
3019}
5bb7327a
JM
3020
3021
3022void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3023{
3024 /* simulate timeout to restart the sched scan */
3025 wpa_s->sched_scan_timed_out = 1;
3026 wpa_s->prev_sched_ssid = NULL;
3027 wpa_supplicant_cancel_sched_scan(wpa_s);
3028}