]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/scan.c
Add attribute for dwell time in QCA vendor scan
[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);
1254 if (bss && bss->ssid_len && params.num_ssids == 1 &&
1255 params.ssids[0].ssid_len == 0) {
1256 params.ssids[0].ssid = bss->ssid;
1257 params.ssids[0].ssid_len = bss->ssid_len;
1258 wpa_dbg(wpa_s, MSG_DEBUG,
1259 "Scan a previously specified BSSID " MACSTR
1260 " and SSID %s",
1261 MAC2STR(params.bssid),
1262 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1263 } else {
1264 wpa_dbg(wpa_s, MSG_DEBUG,
1265 "Scan a previously specified BSSID " MACSTR,
1266 MAC2STR(params.bssid));
1267 }
1268 }
1269
7c865c68
TB
1270 scan_params = &params;
1271
1272scan:
5cc70322
JM
1273#ifdef CONFIG_P2P
1274 /*
1275 * If the driver does not support multi-channel concurrency and a
1276 * virtual interface that shares the same radio with the wpa_s interface
1277 * is operating there may not be need to scan other channels apart from
1278 * the current operating channel on the other virtual interface. Filter
1279 * out other channels in case we are trying to find a connection for a
1280 * station interface when we are not configured to prefer station
1281 * connection and a concurrent operation is already in process.
1282 */
12455031
LP
1283 if (wpa_s->scan_for_connection &&
1284 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
5cc70322
JM
1285 !scan_params->freqs && !params.freqs &&
1286 wpas_is_p2p_prioritized(wpa_s) &&
5cc70322
JM
1287 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1288 non_p2p_network_enabled(wpa_s)) {
53c5dfc2
IP
1289 unsigned int num = wpa_s->num_multichan_concurrent;
1290
1291 params.freqs = os_calloc(num + 1, sizeof(int));
1292 if (params.freqs) {
1293 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
1294 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1295 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1296 } else {
1297 os_free(params.freqs);
1298 params.freqs = NULL;
1299 }
5cc70322
JM
1300 }
1301 }
1302#endif /* CONFIG_P2P */
1303
7c865c68 1304 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
6fc6879b 1305
fee52342
JM
1306 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1307 !wpa_s->manual_scan_freqs) {
1308 /* Restore manual_scan_freqs for the next attempt */
1309 wpa_s->manual_scan_freqs = params.freqs;
1310 params.freqs = NULL;
1311 }
1312
46ee0427 1313 wpabuf_free(extra_ie);
d3a98225 1314 os_free(params.freqs);
3812464c 1315 os_free(params.filter_ssids);
bb66d467 1316 os_free(params.mac_addr);
ad08c363 1317
6fc6879b 1318 if (ret) {
f049052b 1319 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
2d9c99e3
JM
1320 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1321 wpa_supplicant_set_state(wpa_s,
1322 wpa_s->scan_prev_wpa_state);
23270cd8 1323 /* Restore scan_req since we will try to scan again */
12455031 1324 wpa_s->scan_req = wpa_s->last_scan_req;
1c4c9c50 1325 wpa_supplicant_req_scan(wpa_s, 1, 0);
5cc70322
JM
1326 } else {
1327 wpa_s->scan_for_connection = 0;
a8826b18
JM
1328#ifdef CONFIG_INTERWORKING
1329 wpa_s->interworking_fast_assoc_tried = 0;
1330#endif /* CONFIG_INTERWORKING */
0645492e
JM
1331 if (params.bssid)
1332 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
d902a9c1 1333 }
6fc6879b
JM
1334}
1335
1336
9e737f08
PF
1337void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1338{
594516b4 1339 struct os_reltime remaining, new_int;
9e737f08
PF
1340 int cancelled;
1341
1342 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1343 &remaining);
1344
1345 new_int.sec = sec;
1346 new_int.usec = 0;
594516b4 1347 if (cancelled && os_reltime_before(&remaining, &new_int)) {
9e737f08
PF
1348 new_int.sec = remaining.sec;
1349 new_int.usec = remaining.usec;
1350 }
1351
c6f5dec9
PF
1352 if (cancelled) {
1353 eloop_register_timeout(new_int.sec, new_int.usec,
1354 wpa_supplicant_scan, wpa_s, NULL);
1355 }
9e737f08
PF
1356 wpa_s->scan_interval = sec;
1357}
1358
1359
6fc6879b
JM
1360/**
1361 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1362 * @wpa_s: Pointer to wpa_supplicant data
1363 * @sec: Number of seconds after which to scan
1364 * @usec: Number of microseconds after which to scan
1365 *
1366 * This function is used to schedule a scan for neighboring access points after
1367 * the specified time.
1368 */
1369void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1370{
44b9ea5b
JM
1371 int res;
1372
1373 if (wpa_s->p2p_mgmt) {
1374 wpa_dbg(wpa_s, MSG_DEBUG,
1375 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1376 sec, usec);
1377 return;
1378 }
1379
1380 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1381 NULL);
a09ffd5f
JM
1382 if (res == 1) {
1383 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
e2f5a988 1384 sec, usec);
a09ffd5f
JM
1385 } else if (res == 0) {
1386 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1387 sec, usec);
1388 } else {
1389 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1390 sec, usec);
1391 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
e2f5a988 1392 }
6fc6879b
JM
1393}
1394
1395
6a90053c
LC
1396/**
1397 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1398 * @wpa_s: Pointer to wpa_supplicant data
1399 * @sec: Number of seconds after which to scan
1400 * @usec: Number of microseconds after which to scan
2c09af30 1401 * Returns: 0 on success or -1 otherwise
6a90053c
LC
1402 *
1403 * This function is used to schedule periodic scans for neighboring
1404 * access points after the specified time.
1405 */
1406int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1407 int sec, int usec)
1408{
1409 if (!wpa_s->sched_scan_supported)
1410 return -1;
1411
1412 eloop_register_timeout(sec, usec,
1413 wpa_supplicant_delayed_sched_scan_timeout,
1414 wpa_s, NULL);
1415
1416 return 0;
1417}
1418
1419
57c3a605 1420static void
1421wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1422 struct wpa_driver_scan_params *params)
1423{
1424 if (wpa_s->wpa_state != WPA_COMPLETED ||
1425 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1426 wpa_s->srp.relative_rssi_set == 0)
1427 return;
1428
1429 params->relative_rssi_set = 1;
1430 params->relative_rssi = wpa_s->srp.relative_rssi;
1431
1432 if (wpa_s->srp.relative_adjust_rssi == 0)
1433 return;
1434
1435 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1436 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1437}
1438
1439
cbdf3507
LC
1440/**
1441 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1442 * @wpa_s: Pointer to wpa_supplicant data
2c09af30 1443 * Returns: 0 is sched_scan was started or -1 otherwise
cbdf3507
LC
1444 *
1445 * This function is used to schedule periodic scans for neighboring
1446 * access points repeating the scan continuously.
1447 */
1448int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1449{
1450 struct wpa_driver_scan_params params;
7c865c68 1451 struct wpa_driver_scan_params *scan_params;
cbdf3507 1452 enum wpa_states prev_state;
7c865c68 1453 struct wpa_ssid *ssid = NULL;
a13e07ec 1454 struct wpabuf *extra_ie = NULL;
cbdf3507 1455 int ret;
cbdf3507 1456 unsigned int max_sched_scan_ssids;
76a5249e 1457 int wildcard = 0;
0b7a25c0 1458 int need_ssids;
32c02261 1459 struct sched_scan_plan scan_plan;
cbdf3507
LC
1460
1461 if (!wpa_s->sched_scan_supported)
1462 return -1;
1463
1464 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1465 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1466 else
1467 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
54ddd743 1468 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
76a5249e 1469 return -1;
cbdf3507 1470
14f34a73
AS
1471 wpa_s->sched_scan_stop_req = 0;
1472
1966e3d1
ES
1473 if (wpa_s->sched_scanning) {
1474 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
cbdf3507 1475 return 0;
1966e3d1 1476 }
cbdf3507 1477
0b7a25c0
JM
1478 need_ssids = 0;
1479 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
349493bd 1480 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
0b7a25c0
JM
1481 /* Use wildcard SSID to find this network */
1482 wildcard = 1;
349493bd
JM
1483 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1484 ssid->ssid_len)
0b7a25c0 1485 need_ssids++;
aa283ddd
JM
1486
1487#ifdef CONFIG_WPS
349493bd 1488 if (!wpas_network_disabled(wpa_s, ssid) &&
fea7c3a0 1489 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
aa283ddd
JM
1490 /*
1491 * Normal scan is more reliable and faster for WPS
1492 * operations and since these are for short periods of
1493 * time, the benefit of trying to use sched_scan would
1494 * be limited.
1495 */
1496 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1497 "sched_scan for WPS");
1498 return -1;
1499 }
1500#endif /* CONFIG_WPS */
0b7a25c0
JM
1501 }
1502 if (wildcard)
1503 need_ssids++;
1504
1505 if (wpa_s->normal_scans < 3 &&
1506 (need_ssids <= wpa_s->max_scan_ssids ||
1507 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1508 /*
1509 * When normal scan can speed up operations, use that for the
1510 * first operations before starting the sched_scan to allow
1511 * user space sleep more. We do this only if the normal scan
1512 * has functionality that is suitable for this or if the
1513 * sched_scan does not have better support for multiple SSIDs.
1514 */
1515 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1516 "sched_scan for initial scans (normal_scans=%d)",
1517 wpa_s->normal_scans);
1518 return -1;
1519 }
1520
cbdf3507
LC
1521 os_memset(&params, 0, sizeof(params));
1522
b59e6f26 1523 /* If we can't allocate space for the filters, we just don't filter */
faebdeaa 1524 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
b59e6f26
LC
1525 sizeof(struct wpa_driver_scan_filter));
1526
cbdf3507
LC
1527 prev_state = wpa_s->wpa_state;
1528 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1529 wpa_s->wpa_state == WPA_INACTIVE)
1530 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1531
7c865c68
TB
1532 if (wpa_s->autoscan_params != NULL) {
1533 scan_params = wpa_s->autoscan_params;
1534 goto scan;
1535 }
1536
cbdf3507
LC
1537 /* Find the starting point from which to continue scanning */
1538 ssid = wpa_s->conf->ssid;
1539 if (wpa_s->prev_sched_ssid) {
1540 while (ssid) {
1541 if (ssid == wpa_s->prev_sched_ssid) {
1542 ssid = ssid->next;
1543 break;
1544 }
1545 ssid = ssid->next;
1546 }
1547 }
1548
1549 if (!ssid || !wpa_s->prev_sched_ssid) {
1550 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
cbdf3507
LC
1551 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1552 wpa_s->first_sched_scan = 1;
1553 ssid = wpa_s->conf->ssid;
1554 wpa_s->prev_sched_ssid = ssid;
1555 }
1556
76a5249e
JM
1557 if (wildcard) {
1558 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1559 params.num_ssids++;
1560 }
1561
cbdf3507 1562 while (ssid) {
349493bd 1563 if (wpas_network_disabled(wpa_s, ssid))
5edddf41 1564 goto next;
cbdf3507 1565
fcd16847
JM
1566 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1567 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1966e3d1
ES
1568 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1569 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
b59e6f26
LC
1570 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1571 ssid->ssid, ssid->ssid_len);
1572 params.filter_ssids[params.num_filter_ssids].ssid_len =
1573 ssid->ssid_len;
1574 params.num_filter_ssids++;
86b47aaf
JM
1575 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1576 {
1577 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1578 "filter for sched_scan - drop filter");
1579 os_free(params.filter_ssids);
1580 params.filter_ssids = NULL;
1581 params.num_filter_ssids = 0;
b59e6f26
LC
1582 }
1583
3f56f3a4 1584 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
76a5249e
JM
1585 if (params.num_ssids == max_sched_scan_ssids)
1586 break; /* only room for broadcast SSID */
3f56f3a4
JM
1587 wpa_dbg(wpa_s, MSG_DEBUG,
1588 "add to active scan ssid: %s",
1966e3d1 1589 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
cbdf3507
LC
1590 params.ssids[params.num_ssids].ssid =
1591 ssid->ssid;
1592 params.ssids[params.num_ssids].ssid_len =
1593 ssid->ssid_len;
1594 params.num_ssids++;
b59e6f26 1595 if (params.num_ssids >= max_sched_scan_ssids) {
cbdf3507 1596 wpa_s->prev_sched_ssid = ssid;
b55aca46
JM
1597 do {
1598 ssid = ssid->next;
1599 } while (ssid &&
349493bd 1600 (wpas_network_disabled(wpa_s, ssid) ||
fea7c3a0 1601 !ssid->scan_ssid));
cbdf3507
LC
1602 break;
1603 }
1604 }
b59e6f26 1605
5edddf41 1606 next:
cbdf3507
LC
1607 wpa_s->prev_sched_ssid = ssid;
1608 ssid = ssid->next;
1609 }
1610
7c6a266c
JM
1611 if (params.num_filter_ssids == 0) {
1612 os_free(params.filter_ssids);
1613 params.filter_ssids = NULL;
1614 }
1615
a13e07ec
ES
1616 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1617 if (extra_ie) {
1618 params.extra_ies = wpabuf_head(extra_ie);
1619 params.extra_ies_len = wpabuf_len(extra_ie);
1620 }
cbdf3507 1621
cf70d298
RM
1622 if (wpa_s->conf->filter_rssi)
1623 params.filter_rssi = wpa_s->conf->filter_rssi;
1624
1cc0d6a0
BP
1625 /* See if user specified frequencies. If so, scan only those. */
1626 if (wpa_s->conf->freq_list && !params.freqs) {
1627 wpa_dbg(wpa_s, MSG_DEBUG,
1628 "Optimize scan based on conf->freq_list");
1629 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1630 }
1631
2ac43334
RZ
1632#ifdef CONFIG_MBO
1633 if (wpa_s->enable_oce & OCE_STA)
1634 params.oce_scan = 1;
1635#endif /* CONFIG_MBO */
1636
7c865c68
TB
1637 scan_params = &params;
1638
1639scan:
32c02261
AS
1640 wpa_s->sched_scan_timed_out = 0;
1641
1642 /*
1643 * We cannot support multiple scan plans if the scan request includes
1644 * too many SSID's, so in this case use only the last scan plan and make
1645 * it run infinitely. It will be stopped by the timeout.
1646 */
1647 if (wpa_s->sched_scan_plans_num == 1 ||
1648 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1649 params.sched_scan_plans = wpa_s->sched_scan_plans;
1650 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1651 } else if (wpa_s->sched_scan_plans_num > 1) {
a8cb5a88 1652 wpa_dbg(wpa_s, MSG_DEBUG,
32c02261
AS
1653 "Too many SSIDs. Default to using single scheduled_scan plan");
1654 params.sched_scan_plans =
1655 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1656 1];
1657 params.sched_scan_plans_num = 1;
7d21a223 1658 } else {
32c02261
AS
1659 if (wpa_s->conf->sched_scan_interval)
1660 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1661 else
1662 scan_plan.interval = 10;
1663
1664 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1665 wpa_printf(MSG_WARNING,
1666 "Scan interval too long(%u), use the maximum allowed(%u)",
1667 scan_plan.interval,
1668 wpa_s->max_sched_scan_plan_interval);
1669 scan_plan.interval =
1670 wpa_s->max_sched_scan_plan_interval;
1671 }
1672
1673 scan_plan.iterations = 0;
1674 params.sched_scan_plans = &scan_plan;
1675 params.sched_scan_plans_num = 1;
1676 }
1677
d0330d57
PK
1678 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1679
32c02261 1680 if (ssid || !wpa_s->first_sched_scan) {
7d21a223 1681 wpa_dbg(wpa_s, MSG_DEBUG,
d0330d57
PK
1682 "Starting sched scan after %u seconds: interval %u timeout %d",
1683 params.sched_scan_start_delay,
32c02261
AS
1684 params.sched_scan_plans[0].interval,
1685 wpa_s->sched_scan_timeout);
1686 } else {
d0330d57
PK
1687 wpa_dbg(wpa_s, MSG_DEBUG,
1688 "Starting sched scan after %u seconds (no timeout)",
1689 params.sched_scan_start_delay);
a8cb5a88 1690 }
cbdf3507 1691
faf9a858
JM
1692 wpa_setband_scan_freqs(wpa_s, scan_params);
1693
346b333d 1694 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
bb66d467
EC
1695 wpa_s->wpa_state <= WPA_SCANNING)
1696 wpa_setup_mac_addr_rand_params(&params,
1697 wpa_s->mac_addr_sched_scan);
56c76fa5 1698
57c3a605 1699 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1700
32c02261 1701 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
a13e07ec 1702 wpabuf_free(extra_ie);
b59e6f26 1703 os_free(params.filter_ssids);
bb66d467 1704 os_free(params.mac_addr);
cbdf3507
LC
1705 if (ret) {
1706 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1707 if (prev_state != wpa_s->wpa_state)
1708 wpa_supplicant_set_state(wpa_s, prev_state);
1709 return ret;
1710 }
1711
1712 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1713 if (ssid || !wpa_s->first_sched_scan) {
1714 wpa_s->sched_scan_timed_out = 0;
1715 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1716 wpa_supplicant_sched_scan_timeout,
1717 wpa_s, NULL);
1718 wpa_s->first_sched_scan = 0;
1719 wpa_s->sched_scan_timeout /= 2;
32c02261
AS
1720 params.sched_scan_plans[0].interval *= 2;
1721 if ((unsigned int) wpa_s->sched_scan_timeout <
1722 params.sched_scan_plans[0].interval ||
1723 params.sched_scan_plans[0].interval >
1724 wpa_s->max_sched_scan_plan_interval) {
1725 params.sched_scan_plans[0].interval = 10;
a09fc1cc
DS
1726 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1727 }
cbdf3507
LC
1728 }
1729
7ed52f67
DS
1730 /* If there is no more ssids, start next time from the beginning */
1731 if (!ssid)
1732 wpa_s->prev_sched_ssid = NULL;
1733
cbdf3507
LC
1734 return 0;
1735}
1736
1737
6fc6879b
JM
1738/**
1739 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1740 * @wpa_s: Pointer to wpa_supplicant data
1741 *
1742 * This function is used to cancel a scan request scheduled with
1743 * wpa_supplicant_req_scan().
1744 */
1745void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1746{
f049052b 1747 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
6fc6879b
JM
1748 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1749}
cb8564b1
DW
1750
1751
831770bf
CZ
1752/**
1753 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1754 * @wpa_s: Pointer to wpa_supplicant data
1755 *
1756 * This function is used to stop a delayed scheduled scan.
1757 */
1758void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1759{
1760 if (!wpa_s->sched_scan_supported)
1761 return;
1762
1763 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1764 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1765 wpa_s, NULL);
1766}
1767
1768
cbdf3507
LC
1769/**
1770 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1771 * @wpa_s: Pointer to wpa_supplicant data
1772 *
1773 * This function is used to stop a periodic scheduled scan.
1774 */
1775void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1776{
1777 if (!wpa_s->sched_scanning)
1778 return;
1779
14f34a73
AS
1780 if (wpa_s->sched_scanning)
1781 wpa_s->sched_scan_stop_req = 1;
1782
cbdf3507
LC
1783 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1784 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1785 wpa_supplicant_stop_sched_scan(wpa_s);
1786}
1787
1788
2c09af30
JM
1789/**
1790 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1791 * @wpa_s: Pointer to wpa_supplicant data
1792 * @scanning: Whether scanning is currently in progress
1793 *
1794 * This function is to generate scanning notifycations. It is called whenever
1795 * there may have been a change in scanning (scan started, completed, stopped).
1796 * wpas_notify_scanning() is called whenever the scanning state changed from the
1797 * previously notified state.
1798 */
cb8564b1
DW
1799void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1800 int scanning)
1801{
1802 if (wpa_s->scanning != scanning) {
1803 wpa_s->scanning = scanning;
8bac466b 1804 wpas_notify_scanning(wpa_s);
cb8564b1
DW
1805 }
1806}
1807
9ba9fa07
JM
1808
1809static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1810{
1811 int rate = 0;
1812 const u8 *ie;
1813 int i;
1814
1815 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1816 for (i = 0; ie && i < ie[1]; i++) {
1817 if ((ie[i + 2] & 0x7f) > rate)
1818 rate = ie[i + 2] & 0x7f;
1819 }
1820
1821 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1822 for (i = 0; ie && i < ie[1]; i++) {
1823 if ((ie[i + 2] & 0x7f) > rate)
1824 rate = ie[i + 2] & 0x7f;
1825 }
1826
1827 return rate;
1828}
1829
1830
2c09af30
JM
1831/**
1832 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1833 * @res: Scan result entry
1834 * @ie: Information element identitifier (WLAN_EID_*)
1835 * Returns: Pointer to the information element (id field) or %NULL if not found
1836 *
1837 * This function returns the first matching information element in the scan
1838 * result.
1839 */
d1f9c410
JM
1840const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1841{
d044d2f7
EP
1842 size_t ie_len = res->ie_len;
1843
1844 /* Use the Beacon frame IEs if res->ie_len is not available */
1845 if (!ie_len)
1846 ie_len = res->beacon_ie_len;
1847
1848 return get_ie((const u8 *) (res + 1), ie_len, ie);
d1f9c410
JM
1849}
1850
1851
2c09af30
JM
1852/**
1853 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1854 * @res: Scan result entry
1855 * @vendor_type: Vendor type (four octets starting the IE payload)
1856 * Returns: Pointer to the information element (id field) or %NULL if not found
1857 *
1858 * This function returns the first matching information element in the scan
1859 * result.
1860 */
9ba9fa07
JM
1861const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1862 u32 vendor_type)
1863{
1864 const u8 *end, *pos;
1865
1866 pos = (const u8 *) (res + 1);
1867 end = pos + res->ie_len;
1868
904e977b
JM
1869 while (end - pos > 1) {
1870 if (2 + pos[1] > end - pos)
9ba9fa07
JM
1871 break;
1872 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1873 vendor_type == WPA_GET_BE32(&pos[2]))
1874 return pos;
1875 pos += 2 + pos[1];
1876 }
1877
1878 return NULL;
1879}
1880
1881
aaeb9c98
JM
1882/**
1883 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1884 * @res: Scan result entry
1885 * @vendor_type: Vendor type (four octets starting the IE payload)
1886 * Returns: Pointer to the information element (id field) or %NULL if not found
1887 *
1888 * This function returns the first matching information element in the scan
1889 * result.
1890 *
1891 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1892 * from Beacon frames instead of either Beacon or Probe Response frames.
1893 */
1894const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1895 u32 vendor_type)
1896{
1897 const u8 *end, *pos;
1898
1899 if (res->beacon_ie_len == 0)
1900 return NULL;
1901
1902 pos = (const u8 *) (res + 1);
1903 pos += res->ie_len;
1904 end = pos + res->beacon_ie_len;
1905
904e977b
JM
1906 while (end - pos > 1) {
1907 if (2 + pos[1] > end - pos)
aaeb9c98
JM
1908 break;
1909 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1910 vendor_type == WPA_GET_BE32(&pos[2]))
1911 return pos;
1912 pos += 2 + pos[1];
1913 }
1914
1915 return NULL;
1916}
1917
1918
2c09af30
JM
1919/**
1920 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1921 * @res: Scan result entry
1922 * @vendor_type: Vendor type (four octets starting the IE payload)
1923 * Returns: Pointer to the information element payload or %NULL if not found
1924 *
1925 * This function returns concatenated payload of possibly fragmented vendor
1926 * specific information elements in the scan result. The caller is responsible
1927 * for freeing the returned buffer.
1928 */
9ba9fa07
JM
1929struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1930 u32 vendor_type)
1931{
1932 struct wpabuf *buf;
1933 const u8 *end, *pos;
1934
1935 buf = wpabuf_alloc(res->ie_len);
1936 if (buf == NULL)
1937 return NULL;
1938
1939 pos = (const u8 *) (res + 1);
1940 end = pos + res->ie_len;
1941
904e977b
JM
1942 while (end - pos > 1) {
1943 if (2 + pos[1] > end - pos)
54f489be
JM
1944 break;
1945 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1946 vendor_type == WPA_GET_BE32(&pos[2]))
1947 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1948 pos += 2 + pos[1];
1949 }
1950
1951 if (wpabuf_len(buf) == 0) {
1952 wpabuf_free(buf);
1953 buf = NULL;
1954 }
1955
1956 return buf;
1957}
1958
1959
9ba9fa07
JM
1960/* Compare function for sorting scan results. Return >0 if @b is considered
1961 * better. */
1962static int wpa_scan_result_compar(const void *a, const void *b)
1963{
577db0ae 1964#define MIN(a,b) a < b ? a : b
9ba9fa07
JM
1965 struct wpa_scan_res **_wa = (void *) a;
1966 struct wpa_scan_res **_wb = (void *) b;
1967 struct wpa_scan_res *wa = *_wa;
1968 struct wpa_scan_res *wb = *_wb;
a1b790eb
JM
1969 int wpa_a, wpa_b;
1970 int snr_a, snr_b, snr_a_full, snr_b_full;
9ba9fa07
JM
1971
1972 /* WPA/WPA2 support preferred */
1973 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1974 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1975 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1976 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1977
1978 if (wpa_b && !wpa_a)
1979 return 1;
1980 if (!wpa_b && wpa_a)
1981 return -1;
1982
1983 /* privacy support preferred */
1984 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1985 (wb->caps & IEEE80211_CAP_PRIVACY))
1986 return 1;
1987 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1988 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1989 return -1;
1990
f0d0a5d2 1991 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
a1b790eb
JM
1992 snr_a_full = wa->snr;
1993 snr_a = MIN(wa->snr, GREAT_SNR);
1994 snr_b_full = wb->snr;
aa517ae2 1995 snr_b = MIN(wb->snr, GREAT_SNR);
577db0ae 1996 } else {
f0d0a5d2
MA
1997 /* Level is not in dBm, so we can't calculate
1998 * SNR. Just use raw level (units unknown). */
a1b790eb
JM
1999 snr_a = snr_a_full = wa->level;
2000 snr_b = snr_b_full = wb->level;
577db0ae
GM
2001 }
2002
f0d0a5d2 2003 /* if SNR is close, decide by max rate or frequency band */
b4d56efb 2004 if (snr_a && snr_b && abs(snr_b - snr_a) < 7) {
a1b790eb 2005 if (wa->est_throughput != wb->est_throughput)
ec2e7c4c
JM
2006 return (int) wb->est_throughput -
2007 (int) wa->est_throughput;
b4d56efb
JM
2008 }
2009 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2010 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
577db0ae
GM
2011 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2012 return IS_5GHZ(wa->freq) ? -1 : 1;
9ba9fa07
JM
2013 }
2014
577db0ae 2015 /* all things being equal, use SNR; if SNRs are
9ba9fa07
JM
2016 * identical, use quality values since some drivers may only report
2017 * that value and leave the signal level zero */
a1b790eb 2018 if (snr_b_full == snr_a_full)
9ba9fa07 2019 return wb->qual - wa->qual;
a1b790eb 2020 return snr_b_full - snr_a_full;
577db0ae 2021#undef MIN
9ba9fa07
JM
2022}
2023
2024
41e650ae
JM
2025#ifdef CONFIG_WPS
2026/* Compare function for sorting scan results when searching a WPS AP for
2027 * provisioning. Return >0 if @b is considered better. */
2028static int wpa_scan_result_wps_compar(const void *a, const void *b)
2029{
2030 struct wpa_scan_res **_wa = (void *) a;
2031 struct wpa_scan_res **_wb = (void *) b;
2032 struct wpa_scan_res *wa = *_wa;
2033 struct wpa_scan_res *wb = *_wb;
2034 int uses_wps_a, uses_wps_b;
2035 struct wpabuf *wps_a, *wps_b;
2036 int res;
2037
2038 /* Optimization - check WPS IE existence before allocated memory and
2039 * doing full reassembly. */
2040 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2041 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2042 if (uses_wps_a && !uses_wps_b)
2043 return -1;
2044 if (!uses_wps_a && uses_wps_b)
2045 return 1;
2046
2047 if (uses_wps_a && uses_wps_b) {
2048 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2049 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2050 res = wps_ap_priority_compar(wps_a, wps_b);
2051 wpabuf_free(wps_a);
2052 wpabuf_free(wps_b);
2053 if (res)
2054 return res;
2055 }
2056
2057 /*
2058 * Do not use current AP security policy as a sorting criteria during
2059 * WPS provisioning step since the AP may get reconfigured at the
2060 * completion of provisioning.
2061 */
2062
2063 /* all things being equal, use signal level; if signal levels are
2064 * identical, use quality values since some drivers may only report
2065 * that value and leave the signal level zero */
2066 if (wb->level == wa->level)
2067 return wb->qual - wa->qual;
2068 return wb->level - wa->level;
2069}
2070#endif /* CONFIG_WPS */
2071
2072
aa820e02
JM
2073static void dump_scan_res(struct wpa_scan_results *scan_res)
2074{
76202aed 2075#ifndef CONFIG_NO_STDOUT_DEBUG
aa820e02
JM
2076 size_t i;
2077
2078 if (scan_res->res == NULL || scan_res->num == 0)
2079 return;
2080
2081 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2082
2083 for (i = 0; i < scan_res->num; i++) {
2084 struct wpa_scan_res *r = scan_res->res[i];
1cbe86e2 2085 u8 *pos;
f0d0a5d2 2086 if (r->flags & WPA_SCAN_LEVEL_DBM) {
f0d0a5d2
MA
2087 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2088
aa820e02 2089 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
a1b790eb 2090 "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
aa820e02 2091 MAC2STR(r->bssid), r->freq, r->qual,
f0d0a5d2 2092 r->noise, noise_valid ? "" : "~", r->level,
a1b790eb
JM
2093 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2094 r->flags,
2095 r->age, r->est_throughput);
aa820e02
JM
2096 } else {
2097 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
a1b790eb 2098 "noise=%d level=%d flags=0x%x age=%u est=%u",
aa820e02 2099 MAC2STR(r->bssid), r->freq, r->qual,
a1b790eb
JM
2100 r->noise, r->level, r->flags, r->age,
2101 r->est_throughput);
aa820e02 2102 }
1cbe86e2
JM
2103 pos = (u8 *) (r + 1);
2104 if (r->ie_len)
2105 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2106 pos += r->ie_len;
2107 if (r->beacon_ie_len)
2108 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2109 pos, r->beacon_ie_len);
aa820e02 2110 }
76202aed 2111#endif /* CONFIG_NO_STDOUT_DEBUG */
aa820e02
JM
2112}
2113
2114
2c09af30
JM
2115/**
2116 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2117 * @wpa_s: Pointer to wpa_supplicant data
2118 * @bssid: BSSID to check
2119 * Returns: 0 if the BSSID is filtered or 1 if not
2120 *
2121 * This function is used to filter out specific BSSIDs from scan reslts mainly
2122 * for testing purposes (SET bssid_filter ctrl_iface command).
2123 */
d445a5cd
JM
2124int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2125 const u8 *bssid)
2126{
2127 size_t i;
2128
2129 if (wpa_s->bssid_filter == NULL)
2130 return 1;
2131
2132 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2133 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2134 ETH_ALEN) == 0)
2135 return 1;
2136 }
2137
2138 return 0;
2139}
2140
2141
2f195639
KV
2142void filter_scan_res(struct wpa_supplicant *wpa_s,
2143 struct wpa_scan_results *res)
d445a5cd
JM
2144{
2145 size_t i, j;
2146
2147 if (wpa_s->bssid_filter == NULL)
2148 return;
2149
2150 for (i = 0, j = 0; i < res->num; i++) {
2151 if (wpa_supplicant_filter_bssid_match(wpa_s,
2152 res->res[i]->bssid)) {
2153 res->res[j++] = res->res[i];
2154 } else {
2155 os_free(res->res[i]);
2156 res->res[i] = NULL;
2157 }
2158 }
2159
2160 if (res->num != j) {
2161 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2162 (int) (res->num - j));
2163 res->num = j;
2164 }
2165}
2166
2167
2f195639 2168void scan_snr(struct wpa_scan_res *res)
a1b790eb
JM
2169{
2170 if (res->flags & WPA_SCAN_NOISE_INVALID) {
2171 res->noise = IS_5GHZ(res->freq) ?
2172 DEFAULT_NOISE_FLOOR_5GHZ :
2173 DEFAULT_NOISE_FLOOR_2GHZ;
2174 }
2175
2176 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2177 res->snr = res->level - res->noise;
2178 } else {
2179 /* Level is not in dBm, so we can't calculate
2180 * SNR. Just use raw level (units unknown). */
2181 res->snr = res->level;
2182 }
2183}
2184
2185
2186static unsigned int max_ht20_rate(int snr)
2187{
2188 if (snr < 6)
2189 return 6500; /* HT20 MCS0 */
2190 if (snr < 8)
2191 return 13000; /* HT20 MCS1 */
2192 if (snr < 13)
2193 return 19500; /* HT20 MCS2 */
2194 if (snr < 17)
2195 return 26000; /* HT20 MCS3 */
2196 if (snr < 20)
2197 return 39000; /* HT20 MCS4 */
2198 if (snr < 23)
2199 return 52000; /* HT20 MCS5 */
2200 if (snr < 24)
2201 return 58500; /* HT20 MCS6 */
2202 return 65000; /* HT20 MCS7 */
2203}
2204
2205
2206static unsigned int max_ht40_rate(int snr)
2207{
2208 if (snr < 3)
2209 return 13500; /* HT40 MCS0 */
2210 if (snr < 6)
2211 return 27000; /* HT40 MCS1 */
2212 if (snr < 10)
2213 return 40500; /* HT40 MCS2 */
2214 if (snr < 15)
2215 return 54000; /* HT40 MCS3 */
2216 if (snr < 17)
2217 return 81000; /* HT40 MCS4 */
2218 if (snr < 22)
2219 return 108000; /* HT40 MCS5 */
8b2b718d 2220 if (snr < 24)
a1b790eb
JM
2221 return 121500; /* HT40 MCS6 */
2222 return 135000; /* HT40 MCS7 */
2223}
2224
2225
2226static unsigned int max_vht80_rate(int snr)
2227{
2228 if (snr < 1)
2229 return 0;
2230 if (snr < 2)
2231 return 29300; /* VHT80 MCS0 */
2232 if (snr < 5)
2233 return 58500; /* VHT80 MCS1 */
2234 if (snr < 9)
2235 return 87800; /* VHT80 MCS2 */
2236 if (snr < 11)
2237 return 117000; /* VHT80 MCS3 */
2238 if (snr < 15)
2239 return 175500; /* VHT80 MCS4 */
2240 if (snr < 16)
2241 return 234000; /* VHT80 MCS5 */
2242 if (snr < 18)
2243 return 263300; /* VHT80 MCS6 */
2244 if (snr < 20)
2245 return 292500; /* VHT80 MCS7 */
2246 if (snr < 22)
2247 return 351000; /* VHT80 MCS8 */
2248 return 390000; /* VHT80 MCS9 */
2249}
2250
2251
ad06ac0b
EG
2252unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2253 const u8 *ies, size_t ies_len, int rate,
2254 int snr)
a1b790eb
JM
2255{
2256 enum local_hw_capab capab = wpa_s->hw_capab;
a1b790eb 2257 unsigned int est, tmp;
ad06ac0b 2258 const u8 *ie;
a1b790eb
JM
2259
2260 /* Limit based on estimated SNR */
2261 if (rate > 1 * 2 && snr < 1)
2262 rate = 1 * 2;
2263 else if (rate > 2 * 2 && snr < 4)
2264 rate = 2 * 2;
2265 else if (rate > 6 * 2 && snr < 5)
2266 rate = 6 * 2;
2267 else if (rate > 9 * 2 && snr < 6)
2268 rate = 9 * 2;
2269 else if (rate > 12 * 2 && snr < 7)
2270 rate = 12 * 2;
2271 else if (rate > 18 * 2 && snr < 10)
2272 rate = 18 * 2;
2273 else if (rate > 24 * 2 && snr < 11)
2274 rate = 24 * 2;
2275 else if (rate > 36 * 2 && snr < 15)
2276 rate = 36 * 2;
2277 else if (rate > 48 * 2 && snr < 19)
2278 rate = 48 * 2;
2279 else if (rate > 54 * 2 && snr < 21)
2280 rate = 54 * 2;
2281 est = rate * 500;
2282
2283 if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) {
ad06ac0b 2284 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
a1b790eb
JM
2285 if (ie) {
2286 tmp = max_ht20_rate(snr);
2287 if (tmp > est)
2288 est = tmp;
2289 }
2290 }
2291
2292 if (capab == CAPAB_HT40 || capab == CAPAB_VHT) {
ad06ac0b 2293 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
a1b790eb
JM
2294 if (ie && ie[1] >= 2 &&
2295 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2296 tmp = max_ht40_rate(snr);
2297 if (tmp > est)
2298 est = tmp;
2299 }
2300 }
2301
2302 if (capab == CAPAB_VHT) {
2303 /* Use +1 to assume VHT is always faster than HT */
ad06ac0b 2304 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
a1b790eb
JM
2305 if (ie) {
2306 tmp = max_ht20_rate(snr) + 1;
2307 if (tmp > est)
2308 est = tmp;
2309
ad06ac0b 2310 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
a1b790eb
JM
2311 if (ie && ie[1] >= 2 &&
2312 (ie[3] &
2313 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2314 tmp = max_ht40_rate(snr) + 1;
2315 if (tmp > est)
2316 est = tmp;
2317 }
2318
ad06ac0b 2319 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
a1b790eb
JM
2320 if (ie && ie[1] >= 1 &&
2321 (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) {
2322 tmp = max_vht80_rate(snr) + 1;
2323 if (tmp > est)
2324 est = tmp;
2325 }
2326 }
2327 }
2328
ad06ac0b
EG
2329 return est;
2330}
2331
2332
2333void scan_est_throughput(struct wpa_supplicant *wpa_s,
2334 struct wpa_scan_res *res)
2335{
2336 int rate; /* max legacy rate in 500 kb/s units */
2337 int snr = res->snr;
2338 const u8 *ies = (const void *) (res + 1);
2339 size_t ie_len = res->ie_len;
2340
2341 if (res->est_throughput)
2342 return;
2343
2344 /* Get maximum legacy rate */
2345 rate = wpa_scan_get_max_rate(res);
a1b790eb 2346
ad06ac0b
EG
2347 if (!ie_len)
2348 ie_len = res->beacon_ie_len;
2349 res->est_throughput =
fee28410 2350 wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr);
ad06ac0b
EG
2351
2352 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
a1b790eb
JM
2353}
2354
2355
9ba9fa07
JM
2356/**
2357 * wpa_supplicant_get_scan_results - Get scan results
2358 * @wpa_s: Pointer to wpa_supplicant data
2359 * @info: Information about what was scanned or %NULL if not available
2360 * @new_scan: Whether a new scan was performed
2361 * Returns: Scan results, %NULL on failure
2362 *
2363 * This function request the current scan results from the driver and updates
2364 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
2365 * results with wpa_scan_results_free().
2366 */
2367struct wpa_scan_results *
2368wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
2369 struct scan_info *info, int new_scan)
2370{
2371 struct wpa_scan_results *scan_res;
2372 size_t i;
41e650ae 2373 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
9ba9fa07 2374
17fbb751 2375 scan_res = wpa_drv_get_scan_results2(wpa_s);
9ba9fa07 2376 if (scan_res == NULL) {
f049052b 2377 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
9ba9fa07
JM
2378 return NULL;
2379 }
c5f10e80
JM
2380 if (scan_res->fetch_time.sec == 0) {
2381 /*
2382 * Make sure we have a valid timestamp if the driver wrapper
2383 * does not set this.
2384 */
acb69cec 2385 os_get_reltime(&scan_res->fetch_time);
c5f10e80 2386 }
d445a5cd 2387 filter_scan_res(wpa_s, scan_res);
9ba9fa07 2388
f0d0a5d2
MA
2389 for (i = 0; i < scan_res->num; i++) {
2390 struct wpa_scan_res *scan_res_item = scan_res->res[i];
2391
a1b790eb
JM
2392 scan_snr(scan_res_item);
2393 scan_est_throughput(wpa_s, scan_res_item);
f0d0a5d2
MA
2394 }
2395
41e650ae 2396#ifdef CONFIG_WPS
3187fd90 2397 if (wpas_wps_searching(wpa_s)) {
f049052b
BG
2398 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
2399 "provisioning rules");
41e650ae
JM
2400 compar = wpa_scan_result_wps_compar;
2401 }
2402#endif /* CONFIG_WPS */
2403
33111c91
JC
2404 if (scan_res->res) {
2405 qsort(scan_res->res, scan_res->num,
2406 sizeof(struct wpa_scan_res *), compar);
2407 }
aa820e02 2408 dump_scan_res(scan_res);
9ba9fa07 2409
6e374bd4
JM
2410 if (wpa_s->ignore_post_flush_scan_res) {
2411 /* FLUSH command aborted an ongoing scan and these are the
2412 * results from the aborted scan. Do not process the results to
2413 * maintain flushed state. */
2414 wpa_dbg(wpa_s, MSG_DEBUG,
2415 "Do not update BSS table based on pending post-FLUSH scan results");
2416 wpa_s->ignore_post_flush_scan_res = 0;
2417 return scan_res;
2418 }
2419
9ba9fa07
JM
2420 wpa_bss_update_start(wpa_s);
2421 for (i = 0; i < scan_res->num; i++)
c5f10e80
JM
2422 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
2423 &scan_res->fetch_time);
9ba9fa07
JM
2424 wpa_bss_update_end(wpa_s, info, new_scan);
2425
2426 return scan_res;
2427}
2428
2429
2c09af30
JM
2430/**
2431 * wpa_supplicant_update_scan_results - Update scan results from the driver
2432 * @wpa_s: Pointer to wpa_supplicant data
2433 * Returns: 0 on success, -1 on failure
2434 *
2435 * This function updates the BSS table within wpa_supplicant based on the
2436 * currently available scan results from the driver without requesting a new
2437 * scan. This is used in cases where the driver indicates an association
2438 * (including roaming within ESS) and wpa_supplicant does not yet have the
2439 * needed information to complete the connection (e.g., to perform validation
2440 * steps in 4-way handshake).
2441 */
9ba9fa07
JM
2442int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
2443{
2444 struct wpa_scan_results *scan_res;
2445 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2446 if (scan_res == NULL)
2447 return -1;
2448 wpa_scan_results_free(scan_res);
2449
2450 return 0;
2451}
66fe0f70
DS
2452
2453
2454/**
2455 * scan_only_handler - Reports scan results
2456 */
2457void scan_only_handler(struct wpa_supplicant *wpa_s,
2458 struct wpa_scan_results *scan_res)
2459{
2460 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
d81c73be
JM
2461 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2462 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
2463 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2464 wpa_s->manual_scan_id);
2465 wpa_s->manual_scan_use_id = 0;
2466 } else {
2467 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2468 }
66fe0f70
DS
2469 wpas_notify_scan_results(wpa_s);
2470 wpas_notify_scan_done(wpa_s, 1);
d12a51b5
JM
2471 if (wpa_s->scan_work) {
2472 struct wpa_radio_work *work = wpa_s->scan_work;
2473 wpa_s->scan_work = NULL;
2474 radio_work_done(work);
2475 }
ea6030c7
JM
2476
2477 if (wpa_s->wpa_state == WPA_SCANNING)
2478 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
66fe0f70 2479}
407be00b
JM
2480
2481
2482int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
2483{
2484 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
2485}
dd43aaa5
JM
2486
2487
2488struct wpa_driver_scan_params *
2489wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
2490{
2491 struct wpa_driver_scan_params *params;
2492 size_t i;
2493 u8 *n;
2494
2495 params = os_zalloc(sizeof(*params));
2496 if (params == NULL)
2497 return NULL;
2498
2499 for (i = 0; i < src->num_ssids; i++) {
2500 if (src->ssids[i].ssid) {
a1f11e34
JB
2501 n = os_memdup(src->ssids[i].ssid,
2502 src->ssids[i].ssid_len);
dd43aaa5
JM
2503 if (n == NULL)
2504 goto failed;
dd43aaa5
JM
2505 params->ssids[i].ssid = n;
2506 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
2507 }
2508 }
2509 params->num_ssids = src->num_ssids;
2510
2511 if (src->extra_ies) {
a1f11e34 2512 n = os_memdup(src->extra_ies, src->extra_ies_len);
dd43aaa5
JM
2513 if (n == NULL)
2514 goto failed;
dd43aaa5
JM
2515 params->extra_ies = n;
2516 params->extra_ies_len = src->extra_ies_len;
2517 }
2518
2519 if (src->freqs) {
2520 int len = int_array_len(src->freqs);
a1f11e34 2521 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
dd43aaa5
JM
2522 if (params->freqs == NULL)
2523 goto failed;
dd43aaa5
JM
2524 }
2525
2526 if (src->filter_ssids) {
a1f11e34
JB
2527 params->filter_ssids = os_memdup(src->filter_ssids,
2528 sizeof(*params->filter_ssids) *
dd43aaa5
JM
2529 src->num_filter_ssids);
2530 if (params->filter_ssids == NULL)
2531 goto failed;
dd43aaa5
JM
2532 params->num_filter_ssids = src->num_filter_ssids;
2533 }
2534
2535 params->filter_rssi = src->filter_rssi;
2536 params->p2p_probe = src->p2p_probe;
2537 params->only_new_results = src->only_new_results;
57a8f8af 2538 params->low_priority = src->low_priority;
c16b9f8d
AS
2539 params->duration = src->duration;
2540 params->duration_mandatory = src->duration_mandatory;
938dd97a 2541 params->oce_scan = src->oce_scan;
dd43aaa5 2542
09ea4309
AS
2543 if (src->sched_scan_plans_num > 0) {
2544 params->sched_scan_plans =
a1f11e34
JB
2545 os_memdup(src->sched_scan_plans,
2546 sizeof(*src->sched_scan_plans) *
09ea4309
AS
2547 src->sched_scan_plans_num);
2548 if (!params->sched_scan_plans)
2549 goto failed;
2550
09ea4309
AS
2551 params->sched_scan_plans_num = src->sched_scan_plans_num;
2552 }
2553
bb66d467
EC
2554 if (src->mac_addr_rand &&
2555 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
2556 goto failed;
eb20cea5
JM
2557
2558 if (src->bssid) {
2559 u8 *bssid;
2560
a1f11e34 2561 bssid = os_memdup(src->bssid, ETH_ALEN);
eb20cea5
JM
2562 if (!bssid)
2563 goto failed;
eb20cea5
JM
2564 params->bssid = bssid;
2565 }
2566
57c3a605 2567 params->relative_rssi_set = src->relative_rssi_set;
2568 params->relative_rssi = src->relative_rssi;
2569 params->relative_adjust_band = src->relative_adjust_band;
2570 params->relative_adjust_rssi = src->relative_adjust_rssi;
dd43aaa5
JM
2571 return params;
2572
2573failed:
2574 wpa_scan_free_params(params);
2575 return NULL;
2576}
2577
2578
2579void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2580{
2581 size_t i;
2582
2583 if (params == NULL)
2584 return;
2585
2586 for (i = 0; i < params->num_ssids; i++)
2587 os_free((u8 *) params->ssids[i].ssid);
2588 os_free((u8 *) params->extra_ies);
2589 os_free(params->freqs);
2590 os_free(params->filter_ssids);
09ea4309 2591 os_free(params->sched_scan_plans);
56c76fa5
IP
2592
2593 /*
2594 * Note: params->mac_addr_mask points to same memory allocation and
2595 * must not be freed separately.
2596 */
2597 os_free((u8 *) params->mac_addr);
2598
eb20cea5
JM
2599 os_free((u8 *) params->bssid);
2600
dd43aaa5
JM
2601 os_free(params);
2602}
737e7a08
AB
2603
2604
2605int wpas_start_pno(struct wpa_supplicant *wpa_s)
2606{
32c02261 2607 int ret, prio;
4ed34922 2608 size_t i, num_ssid, num_match_ssid;
737e7a08
AB
2609 struct wpa_ssid *ssid;
2610 struct wpa_driver_scan_params params;
32c02261 2611 struct sched_scan_plan scan_plan;
f89c32e6 2612 unsigned int max_sched_scan_ssids;
737e7a08 2613
5e3ddf4d
AB
2614 if (!wpa_s->sched_scan_supported)
2615 return -1;
2616
f89c32e6
DS
2617 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
2618 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
2619 else
2620 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
2621 if (max_sched_scan_ssids < 1)
2622 return -1;
2623
737e7a08
AB
2624 if (wpa_s->pno || wpa_s->pno_sched_pending)
2625 return 0;
2626
2627 if ((wpa_s->wpa_state > WPA_SCANNING) &&
b4fd1f0e 2628 (wpa_s->wpa_state < WPA_COMPLETED)) {
737e7a08
AB
2629 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2630 return -EAGAIN;
2631 }
2632
2633 if (wpa_s->wpa_state == WPA_SCANNING) {
2634 wpa_supplicant_cancel_scan(wpa_s);
2635 if (wpa_s->sched_scanning) {
2636 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2637 "ongoing sched scan");
2638 wpa_supplicant_cancel_sched_scan(wpa_s);
2639 wpa_s->pno_sched_pending = 1;
2640 return 0;
2641 }
2642 }
2643
5ac8f862
AS
2644 if (wpa_s->sched_scan_stop_req) {
2645 wpa_printf(MSG_DEBUG,
2646 "Schedule PNO after previous sched scan has stopped");
2647 wpa_s->pno_sched_pending = 1;
2648 return 0;
2649 }
2650
737e7a08
AB
2651 os_memset(&params, 0, sizeof(params));
2652
4ed34922 2653 num_ssid = num_match_ssid = 0;
737e7a08
AB
2654 ssid = wpa_s->conf->ssid;
2655 while (ssid) {
4ed34922
DS
2656 if (!wpas_network_disabled(wpa_s, ssid)) {
2657 num_match_ssid++;
2658 if (ssid->scan_ssid)
2659 num_ssid++;
2660 }
737e7a08
AB
2661 ssid = ssid->next;
2662 }
4ed34922
DS
2663
2664 if (num_match_ssid == 0) {
2665 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2666 return -1;
2667 }
2668
2669 if (num_match_ssid > num_ssid) {
2670 params.num_ssids++; /* wildcard */
2671 num_ssid++;
2672 }
2673
f89c32e6 2674 if (num_ssid > max_sched_scan_ssids) {
737e7a08 2675 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
f89c32e6
DS
2676 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
2677 num_ssid = max_sched_scan_ssids;
737e7a08
AB
2678 }
2679
4ed34922
DS
2680 if (num_match_ssid > wpa_s->max_match_sets) {
2681 num_match_ssid = wpa_s->max_match_sets;
2682 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
737e7a08 2683 }
4ed34922
DS
2684 params.filter_ssids = os_calloc(num_match_ssid,
2685 sizeof(struct wpa_driver_scan_filter));
737e7a08
AB
2686 if (params.filter_ssids == NULL)
2687 return -1;
6f5e1b0b 2688
737e7a08 2689 i = 0;
6f5e1b0b
DS
2690 prio = 0;
2691 ssid = wpa_s->conf->pssid[prio];
737e7a08
AB
2692 while (ssid) {
2693 if (!wpas_network_disabled(wpa_s, ssid)) {
4ed34922
DS
2694 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2695 params.ssids[params.num_ssids].ssid =
2696 ssid->ssid;
2697 params.ssids[params.num_ssids].ssid_len =
2698 ssid->ssid_len;
2699 params.num_ssids++;
2700 }
737e7a08
AB
2701 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2702 ssid->ssid_len);
2703 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2704 params.num_filter_ssids++;
2705 i++;
4ed34922 2706 if (i == num_match_ssid)
737e7a08
AB
2707 break;
2708 }
6f5e1b0b
DS
2709 if (ssid->pnext)
2710 ssid = ssid->pnext;
2711 else if (prio + 1 == wpa_s->conf->num_prio)
2712 break;
2713 else
2714 ssid = wpa_s->conf->pssid[++prio];
737e7a08
AB
2715 }
2716
2717 if (wpa_s->conf->filter_rssi)
2718 params.filter_rssi = wpa_s->conf->filter_rssi;
2719
32c02261
AS
2720 if (wpa_s->sched_scan_plans_num) {
2721 params.sched_scan_plans = wpa_s->sched_scan_plans;
2722 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
2723 } else {
2724 /* Set one scan plan that will run infinitely */
2725 if (wpa_s->conf->sched_scan_interval)
2726 scan_plan.interval = wpa_s->conf->sched_scan_interval;
2727 else
2728 scan_plan.interval = 10;
2729
2730 scan_plan.iterations = 0;
2731 params.sched_scan_plans = &scan_plan;
2732 params.sched_scan_plans_num = 1;
2733 }
737e7a08 2734
d0330d57
PK
2735 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
2736
d3c9c35f
DS
2737 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2738 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2739 params.freqs = wpa_s->manual_sched_scan_freqs;
2740 }
2741
346b333d 2742 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
bb66d467
EC
2743 wpa_s->wpa_state <= WPA_SCANNING)
2744 wpa_setup_mac_addr_rand_params(&params, wpa_s->mac_addr_pno);
56c76fa5 2745
57c3a605 2746 wpa_scan_set_relative_rssi_params(wpa_s, &params);
2747
32c02261 2748 ret = wpa_supplicant_start_sched_scan(wpa_s, &params);
737e7a08 2749 os_free(params.filter_ssids);
bb66d467 2750 os_free(params.mac_addr);
737e7a08
AB
2751 if (ret == 0)
2752 wpa_s->pno = 1;
5e3ddf4d
AB
2753 else
2754 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
737e7a08
AB
2755 return ret;
2756}
2757
2758
2759int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2760{
2761 int ret = 0;
2762
5e3ddf4d
AB
2763 if (!wpa_s->pno)
2764 return 0;
2765
2766 ret = wpa_supplicant_stop_sched_scan(wpa_s);
14f34a73 2767 wpa_s->sched_scan_stop_req = 1;
737e7a08 2768
5e3ddf4d 2769 wpa_s->pno = 0;
737e7a08
AB
2770 wpa_s->pno_sched_pending = 0;
2771
2772 if (wpa_s->wpa_state == WPA_SCANNING)
2773 wpa_supplicant_req_scan(wpa_s, 0, 0);
2774
2775 return ret;
2776}
56c76fa5
IP
2777
2778
2779void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2780 unsigned int type)
2781{
2782 type &= MAC_ADDR_RAND_ALL;
2783 wpa_s->mac_addr_rand_enable &= ~type;
2784
2785 if (type & MAC_ADDR_RAND_SCAN) {
2786 os_free(wpa_s->mac_addr_scan);
2787 wpa_s->mac_addr_scan = NULL;
2788 }
2789
2790 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2791 os_free(wpa_s->mac_addr_sched_scan);
2792 wpa_s->mac_addr_sched_scan = NULL;
2793 }
2794
2795 if (type & MAC_ADDR_RAND_PNO) {
2796 os_free(wpa_s->mac_addr_pno);
2797 wpa_s->mac_addr_pno = NULL;
2798 }
2799}
2800
2801
2802int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2803 unsigned int type, const u8 *addr,
2804 const u8 *mask)
2805{
2806 u8 *tmp = NULL;
2807
1e591df0
LD
2808 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
2809 wpa_printf(MSG_INFO,
2810 "scan: MAC randomization type %u != supported=%u",
2811 type, wpa_s->mac_addr_rand_supported);
2812 return -1;
2813 }
2814
56c76fa5
IP
2815 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2816
2817 if (addr) {
2818 tmp = os_malloc(2 * ETH_ALEN);
2819 if (!tmp)
2820 return -1;
2821 os_memcpy(tmp, addr, ETH_ALEN);
2822 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2823 }
2824
2825 if (type == MAC_ADDR_RAND_SCAN) {
2826 wpa_s->mac_addr_scan = tmp;
2827 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2828 wpa_s->mac_addr_sched_scan = tmp;
2829 } else if (type == MAC_ADDR_RAND_PNO) {
2830 wpa_s->mac_addr_pno = tmp;
2831 } else {
2832 wpa_printf(MSG_INFO,
2833 "scan: Invalid MAC randomization type=0x%x",
2834 type);
2835 os_free(tmp);
2836 return -1;
2837 }
2838
2839 wpa_s->mac_addr_rand_enable |= type;
2840 return 0;
2841}
4ead7cfd
KV
2842
2843
10f8351d
EC
2844int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
2845 unsigned int type, u8 *mask)
2846{
2847 const u8 *to_copy;
2848
2849 if ((wpa_s->mac_addr_rand_enable & type) != type)
2850 return -1;
2851
2852 if (type == MAC_ADDR_RAND_SCAN) {
2853 to_copy = wpa_s->mac_addr_scan;
2854 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2855 to_copy = wpa_s->mac_addr_sched_scan;
2856 } else if (type == MAC_ADDR_RAND_PNO) {
2857 to_copy = wpa_s->mac_addr_pno;
2858 } else {
2859 wpa_printf(MSG_DEBUG,
2860 "scan: Invalid MAC randomization type=0x%x",
2861 type);
2862 return -1;
2863 }
2864
2865 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
2866 return 0;
2867}
2868
2869
4ead7cfd
KV
2870int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
2871{
bf88401d
SD
2872 struct wpa_radio_work *work;
2873 struct wpa_radio *radio = wpa_s->radio;
a8057310 2874
bf88401d
SD
2875 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
2876 if (work->wpa_s != wpa_s || !work->started ||
2877 (os_strcmp(work->type, "scan") != 0 &&
2878 os_strcmp(work->type, "p2p-scan") != 0))
2879 continue;
4ead7cfd 2880 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
bf88401d 2881 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
4ead7cfd
KV
2882 }
2883
bf88401d
SD
2884 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
2885 return -1;
4ead7cfd 2886}
32c02261
AS
2887
2888
2889int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
2890{
2891 struct sched_scan_plan *scan_plans = NULL;
2892 const char *token, *context = NULL;
2893 unsigned int num = 0;
2894
2895 if (!cmd)
2896 return -1;
2897
bea48f77
JM
2898 if (!cmd[0]) {
2899 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
2900 os_free(wpa_s->sched_scan_plans);
2901 wpa_s->sched_scan_plans = NULL;
2902 wpa_s->sched_scan_plans_num = 0;
2903 return 0;
2904 }
2905
32c02261
AS
2906 while ((token = cstr_token(cmd, " ", &context))) {
2907 int ret;
2908 struct sched_scan_plan *scan_plan, *n;
2909
2910 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
2911 if (!n)
2912 goto fail;
2913
2914 scan_plans = n;
2915 scan_plan = &scan_plans[num];
2916 num++;
2917
2918 ret = sscanf(token, "%u:%u", &scan_plan->interval,
2919 &scan_plan->iterations);
2920 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
2921 wpa_printf(MSG_ERROR,
2922 "Invalid sched scan plan input: %s", token);
2923 goto fail;
2924 }
2925
32c02261
AS
2926 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
2927 wpa_printf(MSG_WARNING,
2928 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
2929 num, scan_plan->interval,
2930 wpa_s->max_sched_scan_plan_interval);
2931 scan_plan->interval =
2932 wpa_s->max_sched_scan_plan_interval;
2933 }
2934
2935 if (ret == 1) {
2936 scan_plan->iterations = 0;
2937 break;
2938 }
2939
2940 if (!scan_plan->iterations) {
2941 wpa_printf(MSG_ERROR,
2942 "scan plan %u: Number of iterations cannot be zero",
2943 num);
2944 goto fail;
2945 }
2946
2947 if (scan_plan->iterations >
2948 wpa_s->max_sched_scan_plan_iterations) {
2949 wpa_printf(MSG_WARNING,
2950 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
2951 num, scan_plan->iterations,
2952 wpa_s->max_sched_scan_plan_iterations);
2953 scan_plan->iterations =
2954 wpa_s->max_sched_scan_plan_iterations;
2955 }
2956
2957 wpa_printf(MSG_DEBUG,
2958 "scan plan %u: interval=%u iterations=%u",
2959 num, scan_plan->interval, scan_plan->iterations);
2960 }
2961
2962 if (!scan_plans) {
2963 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
2964 goto fail;
2965 }
2966
2967 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
2968 wpa_printf(MSG_ERROR,
2969 "All scan plans but the last must specify a number of iterations");
2970 goto fail;
2971 }
2972
2973 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
2974 num, scan_plans[num - 1].interval);
2975
2976 if (num > wpa_s->max_sched_scan_plans) {
2977 wpa_printf(MSG_WARNING,
2978 "Too many scheduled scan plans (only %u supported)",
2979 wpa_s->max_sched_scan_plans);
2980 wpa_printf(MSG_WARNING,
2981 "Use only the first %u scan plans, and the last one (in infinite loop)",
2982 wpa_s->max_sched_scan_plans - 1);
2983 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
2984 &scan_plans[num - 1], sizeof(*scan_plans));
2985 num = wpa_s->max_sched_scan_plans;
2986 }
2987
2988 os_free(wpa_s->sched_scan_plans);
2989 wpa_s->sched_scan_plans = scan_plans;
2990 wpa_s->sched_scan_plans_num = num;
2991
2992 return 0;
2993
2994fail:
2995 os_free(scan_plans);
2996 wpa_printf(MSG_ERROR, "invalid scan plans list");
2997 return -1;
2998}
be7ebd89
MS
2999
3000
3001/**
3002 * wpas_scan_reset_sched_scan - Reset sched_scan state
3003 * @wpa_s: Pointer to wpa_supplicant data
3004 *
3005 * This function is used to cancel a running scheduled scan and to reset an
3006 * internal scan state to continue with a regular scan on the following
3007 * wpa_supplicant_req_scan() calls.
3008 */
3009void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3010{
3011 wpa_s->normal_scans = 0;
3012 if (wpa_s->sched_scanning) {
3013 wpa_s->sched_scan_timed_out = 0;
3014 wpa_s->prev_sched_ssid = NULL;
3015 wpa_supplicant_cancel_sched_scan(wpa_s);
3016 }
3017}
5bb7327a
JM
3018
3019
3020void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3021{
3022 /* simulate timeout to restart the sched scan */
3023 wpa_s->sched_scan_timed_out = 1;
3024 wpa_s->prev_sched_ssid = NULL;
3025 wpa_supplicant_cancel_sched_scan(wpa_s);
3026}