1 /*
2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "config.h"
16 #include "wpa_supplicant_i.h"
17 #include "driver_i.h"
18 #include "wps_supplicant.h"
19 #include "p2p_supplicant.h"
20 #include "p2p/p2p.h"
21 #include "hs20_supplicant.h"
22 #include "notify.h"
23 #include "bss.h"
24 #include "scan.h"
25 #include "mesh.h"
26
27 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s);
28
29
wpa_supplicant_gen_assoc_event(struct wpa_supplicant * wpa_s)30 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
31 {
32 struct wpa_ssid *ssid;
33 union wpa_event_data data;
34
35 ssid = wpa_supplicant_get_ssid(wpa_s);
36 if (ssid == NULL)
37 return;
38
39 if (wpa_s->current_ssid == NULL) {
40 wpa_s->current_ssid = ssid;
41 wpas_notify_network_changed(wpa_s);
42 }
43 wpa_supplicant_initiate_eapol(wpa_s);
44 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
45 "network - generating associated event");
46 os_memset(&data, 0, sizeof(data));
47 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
48 }
49
50
51 #ifdef CONFIG_WPS
wpas_wps_in_use(struct wpa_supplicant * wpa_s,enum wps_request_type * req_type)52 static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
53 enum wps_request_type *req_type)
54 {
55 struct wpa_ssid *ssid;
56 int wps = 0;
57
58 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
59 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
60 continue;
61
62 wps = 1;
63 *req_type = wpas_wps_get_req_type(ssid);
64 if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1"))
65 return 2;
66 }
67
68 #ifdef CONFIG_P2P
69 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
70 !wpa_s->conf->p2p_disabled) {
71 wpa_s->wps->dev.p2p = 1;
72 if (!wps) {
73 wps = 1;
74 *req_type = WPS_REQ_ENROLLEE_INFO;
75 }
76 }
77 #endif /* CONFIG_P2P */
78
79 return wps;
80 }
81 #endif /* CONFIG_WPS */
82
83
wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params * params,const u8 * mac_addr)84 static int wpa_setup_mac_addr_rand_params(struct wpa_driver_scan_params *params,
85 const u8 *mac_addr)
86 {
87 u8 *tmp;
88
89 if (params->mac_addr) {
90 params->mac_addr_mask = NULL;
91 os_free(params->mac_addr);
92 params->mac_addr = NULL;
93 }
94
95 params->mac_addr_rand = 1;
96
97 if (!mac_addr)
98 return 0;
99
100 tmp = os_malloc(2 * ETH_ALEN);
101 if (!tmp)
102 return -1;
103
104 os_memcpy(tmp, mac_addr, 2 * ETH_ALEN);
105 params->mac_addr = tmp;
106 params->mac_addr_mask = tmp + ETH_ALEN;
107 return 0;
108 }
109
110
111 /**
112 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
113 * @wpa_s: Pointer to wpa_supplicant data
114 * Returns: 0 if no networks are enabled, >0 if networks are enabled
115 *
116 * This function is used to figure out whether any networks (or Interworking
117 * with enabled credentials and auto_interworking) are present in the current
118 * configuration.
119 */
wpa_supplicant_enabled_networks(struct wpa_supplicant * wpa_s)120 int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
121 {
122 struct wpa_ssid *ssid = wpa_s->conf->ssid;
123 int count = 0, disabled = 0;
124
125 if (wpa_s->p2p_mgmt)
126 return 0; /* no normal network profiles on p2p_mgmt interface */
127
128 while (ssid) {
129 if (!wpas_network_disabled(wpa_s, ssid))
130 count++;
131 else
132 disabled++;
133 ssid = ssid->next;
134 }
135 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
136 wpa_s->conf->auto_interworking)
137 count++;
138 if (count == 0 && disabled > 0) {
139 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
140 "networks)", disabled);
141 }
142 return count;
143 }
144
145
wpa_supplicant_assoc_try(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)146 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
147 struct wpa_ssid *ssid)
148 {
149 int min_temp_disabled = 0;
150
151 while (ssid) {
152 if (!wpas_network_disabled(wpa_s, ssid)) {
153 int temp_disabled = wpas_temp_disabled(wpa_s, ssid);
154
155 if (temp_disabled <= 0)
156 break;
157
158 if (!min_temp_disabled ||
159 temp_disabled < min_temp_disabled)
160 min_temp_disabled = temp_disabled;
161 }
162 ssid = ssid->next;
163 }
164
165 /* ap_scan=2 mode - try to associate with each SSID. */
166 if (ssid == NULL) {
167 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
168 "end of scan list - go back to beginning");
169 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
170 wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0);
171 return;
172 }
173 if (ssid->next) {
174 /* Continue from the next SSID on the next attempt. */
175 wpa_s->prev_scan_ssid = ssid;
176 } else {
177 /* Start from the beginning of the SSID list. */
178 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
179 }
180 wpa_supplicant_associate(wpa_s, NULL, ssid);
181 }
182
183
wpas_trigger_scan_cb(struct wpa_radio_work * work,int deinit)184 static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
185 {
186 struct wpa_supplicant *wpa_s = work->wpa_s;
187 struct wpa_driver_scan_params *params = work->ctx;
188 int ret;
189
190 if (deinit) {
191 if (!work->started) {
192 wpa_scan_free_params(params);
193 return;
194 }
195 wpa_supplicant_notify_scanning(wpa_s, 0);
196 wpas_notify_scan_done(wpa_s, 0);
197 wpa_s->scan_work = NULL;
198 return;
199 }
200
201 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
202 wpa_s->wpa_state <= WPA_SCANNING)
203 wpa_setup_mac_addr_rand_params(params, wpa_s->mac_addr_scan);
204
205 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
206 wpa_msg(wpa_s, MSG_INFO,
207 "Failed to assign random MAC address for a scan");
208 wpa_scan_free_params(params);
209 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
210 radio_work_done(work);
211 return;
212 }
213
214 wpa_supplicant_notify_scanning(wpa_s, 1);
215
216 if (wpa_s->clear_driver_scan_cache) {
217 wpa_printf(MSG_DEBUG,
218 "Request driver to clear scan cache due to local BSS flush");
219 params->only_new_results = 1;
220 }
221 ret = wpa_drv_scan(wpa_s, params);
222 /*
223 * Store the obtained vendor scan cookie (if any) in wpa_s context.
224 * The current design is to allow only one scan request on each
225 * interface, hence having this scan cookie stored in wpa_s context is
226 * fine for now.
227 *
228 * Revisit this logic if concurrent scan operations per interface
229 * is supported.
230 */
231 if (ret == 0)
232 wpa_s->curr_scan_cookie = params->scan_cookie;
233 wpa_scan_free_params(params);
234 work->ctx = NULL;
235 if (ret) {
236 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
237 !wpa_s->beacon_rep_data.token;
238
239 if (wpa_s->disconnected)
240 retry = 0;
241
242 /* do not retry if operation is not supported */
243 if (ret == -EOPNOTSUPP)
244 retry = 0;
245
246 wpa_supplicant_notify_scanning(wpa_s, 0);
247 wpas_notify_scan_done(wpa_s, 0);
248 if (wpa_s->wpa_state == WPA_SCANNING)
249 wpa_supplicant_set_state(wpa_s,
250 wpa_s->scan_prev_wpa_state);
251 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
252 ret, retry ? " retry=1" : "");
253 radio_work_done(work);
254
255 if (retry) {
256 /* Restore scan_req since we will try to scan again */
257 wpa_s->scan_req = wpa_s->last_scan_req;
258 wpa_supplicant_req_scan(wpa_s, 1, 0);
259 } else if (wpa_s->scan_res_handler) {
260 /* Clear the scan_res_handler */
261 wpa_s->scan_res_handler = NULL;
262 }
263
264 if (wpa_s->beacon_rep_data.token)
265 wpas_rrm_refuse_request(wpa_s);
266
267 return;
268 }
269
270 os_get_reltime(&wpa_s->scan_trigger_time);
271 wpa_s->scan_runs++;
272 wpa_s->normal_scans++;
273 wpa_s->own_scan_requested = 1;
274 wpa_s->clear_driver_scan_cache = 0;
275 wpa_s->scan_work = work;
276 }
277
278
279 /**
280 * wpa_supplicant_trigger_scan - Request driver to start a scan
281 * @wpa_s: Pointer to wpa_supplicant data
282 * @params: Scan parameters
283 * @default_ies: Whether or not to use the default IEs in the Probe Request
284 * frames. Note that this will free any existing IEs set in @params, so this
285 * shouldn't be set if the IEs have already been set with
286 * wpa_supplicant_extra_ies(). Otherwise, wpabuf_free() will lead to a
287 * double-free.
288 * @next: Whether or not to perform this scan as the next radio work
289 * Returns: 0 on success, -1 on failure
290 */
wpa_supplicant_trigger_scan(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,bool default_ies,bool next)291 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
292 struct wpa_driver_scan_params *params,
293 bool default_ies, bool next)
294 {
295 struct wpa_driver_scan_params *ctx;
296 struct wpabuf *ies = NULL;
297
298 if (wpa_s->scan_work) {
299 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
300 return -1;
301 }
302
303 if (default_ies) {
304 if (params->extra_ies_len) {
305 os_free((u8 *) params->extra_ies);
306 params->extra_ies = NULL;
307 params->extra_ies_len = 0;
308 }
309 ies = wpa_supplicant_extra_ies(wpa_s);
310 if (ies) {
311 params->extra_ies = wpabuf_head(ies);
312 params->extra_ies_len = wpabuf_len(ies);
313 }
314 }
315 ctx = wpa_scan_clone_params(params);
316 if (ies) {
317 wpabuf_free(ies);
318 params->extra_ies = NULL;
319 params->extra_ies_len = 0;
320 }
321 wpa_s->last_scan_all_chan = !params->freqs;
322 wpa_s->last_scan_non_coloc_6ghz = params->non_coloc_6ghz;
323 if (!ctx ||
324 radio_add_work(wpa_s, 0, "scan", next, wpas_trigger_scan_cb,
325 ctx) < 0) {
326 wpa_scan_free_params(ctx);
327 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1");
328 return -1;
329 }
330
331 wpa_s->wps_scan_done = false;
332
333 return 0;
334 }
335
336
337 static void
wpa_supplicant_delayed_sched_scan_timeout(void * eloop_ctx,void * timeout_ctx)338 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
339 {
340 struct wpa_supplicant *wpa_s = eloop_ctx;
341
342 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
343
344 if (wpa_supplicant_req_sched_scan(wpa_s))
345 wpa_supplicant_req_scan(wpa_s, 0, 0);
346 }
347
348
349 static void
wpa_supplicant_sched_scan_timeout(void * eloop_ctx,void * timeout_ctx)350 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
351 {
352 struct wpa_supplicant *wpa_s = eloop_ctx;
353
354 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
355
356 wpa_s->sched_scan_timed_out = 1;
357 wpa_supplicant_cancel_sched_scan(wpa_s);
358 }
359
360
361 static int
wpa_supplicant_start_sched_scan(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)362 wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
363 struct wpa_driver_scan_params *params)
364 {
365 int ret;
366
367 wpa_supplicant_notify_scanning(wpa_s, 1);
368 ret = wpa_drv_sched_scan(wpa_s, params);
369 if (ret)
370 wpa_supplicant_notify_scanning(wpa_s, 0);
371 else
372 wpa_s->sched_scanning = 1;
373
374 return ret;
375 }
376
377
wpa_supplicant_stop_sched_scan(struct wpa_supplicant * wpa_s)378 static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
379 {
380 int ret;
381
382 ret = wpa_drv_stop_sched_scan(wpa_s);
383 if (ret) {
384 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
385 /* TODO: what to do if stopping fails? */
386 return -1;
387 }
388
389 return ret;
390 }
391
392
393 static struct wpa_driver_scan_filter *
wpa_supplicant_build_filter_ssids(struct wpa_config * conf,size_t * num_ssids)394 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
395 {
396 struct wpa_driver_scan_filter *ssids;
397 struct wpa_ssid *ssid;
398 size_t count;
399
400 *num_ssids = 0;
401 if (!conf->filter_ssids)
402 return NULL;
403
404 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
405 if (ssid->ssid && ssid->ssid_len)
406 count++;
407 }
408 if (count == 0)
409 return NULL;
410 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
411 if (ssids == NULL)
412 return NULL;
413
414 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
415 if (!ssid->ssid || !ssid->ssid_len)
416 continue;
417 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
418 ssids[*num_ssids].ssid_len = ssid->ssid_len;
419 (*num_ssids)++;
420 }
421
422 return ssids;
423 }
424
425
wpa_supplicant_optimize_freqs(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)426 static void wpa_supplicant_optimize_freqs(
427 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
428 {
429 #ifdef CONFIG_P2P
430 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
431 wpa_s->go_params) {
432 /* Optimize provisioning state scan based on GO information */
433 if (wpa_s->p2p_in_provisioning < 5 &&
434 wpa_s->go_params->freq > 0) {
435 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
436 "preferred frequency %d MHz",
437 wpa_s->go_params->freq);
438 params->freqs = os_calloc(2, sizeof(int));
439 if (params->freqs)
440 params->freqs[0] = wpa_s->go_params->freq;
441 } else if (wpa_s->p2p_in_provisioning < 8 &&
442 wpa_s->go_params->freq_list[0]) {
443 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
444 "channels");
445 int_array_concat(¶ms->freqs,
446 wpa_s->go_params->freq_list);
447 if (params->freqs)
448 int_array_sort_unique(params->freqs);
449 }
450 wpa_s->p2p_in_provisioning++;
451 }
452
453 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
454 struct wpa_ssid *ssid = wpa_s->current_ssid;
455
456 /*
457 * Perform a single-channel scan if the GO has already been
458 * discovered on another non-P2P interface. Note that a scan
459 * initiated by a P2P interface (e.g., the device interface)
460 * should already have sufficient IEs and scan results will be
461 * fetched on interface creation in that case.
462 */
463 if (wpa_s->p2p_in_invitation == 1 && ssid) {
464 struct wpa_supplicant *ifs;
465 struct wpa_bss *bss = NULL;
466 const u8 *bssid = ssid->bssid_set ? ssid->bssid : NULL;
467
468 dl_list_for_each(ifs, &wpa_s->radio->ifaces,
469 struct wpa_supplicant, radio_list) {
470 bss = wpa_bss_get(ifs, bssid, ssid->ssid,
471 ssid->ssid_len);
472 if (bss)
473 break;
474 }
475 if (bss && !disabled_freq(wpa_s, bss->freq)) {
476 params->freqs = os_calloc(2, sizeof(int));
477 if (params->freqs) {
478 wpa_dbg(wpa_s, MSG_DEBUG,
479 "P2P: Scan only the known GO frequency %d MHz during invitation",
480 bss->freq);
481 params->freqs[0] = bss->freq;
482 }
483 }
484 }
485
486 /*
487 * Optimize scan based on GO information during persistent
488 * group reinvocation
489 */
490 if (!params->freqs && wpa_s->p2p_in_invitation < 5 &&
491 wpa_s->p2p_invite_go_freq > 0) {
492 if (wpa_s->p2p_invite_go_freq == 2 ||
493 wpa_s->p2p_invite_go_freq == 5) {
494 enum hostapd_hw_mode mode;
495
496 wpa_dbg(wpa_s, MSG_DEBUG,
497 "P2P: Scan only GO preferred band %d GHz during invitation",
498 wpa_s->p2p_invite_go_freq);
499
500 if (!wpa_s->hw.modes)
501 return;
502 mode = wpa_s->p2p_invite_go_freq == 5 ?
503 HOSTAPD_MODE_IEEE80211A :
504 HOSTAPD_MODE_IEEE80211G;
505 if (wpa_s->p2p_in_invitation <= 2)
506 wpa_add_scan_freqs_list(wpa_s, mode,
507 params, false,
508 false, true);
509 if (!params->freqs || params->freqs[0] == 0)
510 wpa_add_scan_freqs_list(wpa_s, mode,
511 params, false,
512 false, false);
513 } else {
514 wpa_dbg(wpa_s, MSG_DEBUG,
515 "P2P: Scan only GO preferred frequency %d MHz during invitation",
516 wpa_s->p2p_invite_go_freq);
517 params->freqs = os_calloc(2, sizeof(int));
518 if (params->freqs)
519 params->freqs[0] =
520 wpa_s->p2p_invite_go_freq;
521 }
522 }
523 wpa_s->p2p_in_invitation++;
524 if (wpa_s->p2p_in_invitation > 20) {
525 /*
526 * This should not really happen since the variable is
527 * cleared on group removal, but if it does happen, make
528 * sure we do not get stuck in special invitation scan
529 * mode.
530 */
531 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
532 wpa_s->p2p_in_invitation = 0;
533 wpa_s->p2p_retry_limit = 0;
534 }
535 }
536 #endif /* CONFIG_P2P */
537
538 #ifdef CONFIG_WPS
539 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
540 /*
541 * Optimize post-provisioning scan based on channel used
542 * during provisioning.
543 */
544 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
545 "that was used during provisioning", wpa_s->wps_freq);
546 params->freqs = os_calloc(2, sizeof(int));
547 if (params->freqs)
548 params->freqs[0] = wpa_s->wps_freq;
549 wpa_s->after_wps--;
550 } else if (wpa_s->after_wps)
551 wpa_s->after_wps--;
552
553 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
554 {
555 /* Optimize provisioning scan based on already known channel */
556 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
557 wpa_s->wps_freq);
558 params->freqs = os_calloc(2, sizeof(int));
559 if (params->freqs)
560 params->freqs[0] = wpa_s->wps_freq;
561 wpa_s->known_wps_freq = 0; /* only do this once */
562 }
563 #endif /* CONFIG_WPS */
564 }
565
566
567 #ifdef CONFIG_INTERWORKING
wpas_add_interworking_elements(struct wpa_supplicant * wpa_s,struct wpabuf * buf)568 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
569 struct wpabuf *buf)
570 {
571 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
572 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
573 1 + ETH_ALEN);
574 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
575 /* No Venue Info */
576 if (!is_zero_ether_addr(wpa_s->conf->hessid))
577 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
578 }
579 #endif /* CONFIG_INTERWORKING */
580
581
582 #ifdef CONFIG_MBO
wpas_fils_req_param_add_max_channel(struct wpa_supplicant * wpa_s,struct wpabuf ** ie)583 static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s,
584 struct wpabuf **ie)
585 {
586 if (wpabuf_resize(ie, 5)) {
587 wpa_printf(MSG_DEBUG,
588 "Failed to allocate space for FILS Request Parameters element");
589 return;
590 }
591
592 /* FILS Request Parameters element */
593 wpabuf_put_u8(*ie, WLAN_EID_EXTENSION);
594 wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */
595 wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS);
596 /* Parameter control bitmap */
597 wpabuf_put_u8(*ie, 0);
598 /* Max Channel Time field - contains the value of MaxChannelTime
599 * parameter of the MLME-SCAN.request primitive represented in units of
600 * TUs, as an unsigned integer. A Max Channel Time field value of 255
601 * is used to indicate any duration of more than 254 TUs, or an
602 * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178)
603 */
604 wpabuf_put_u8(*ie, 255);
605 }
606 #endif /* CONFIG_MBO */
607
608
wpa_supplicant_set_default_scan_ies(struct wpa_supplicant * wpa_s)609 void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s)
610 {
611 struct wpabuf *default_ies = NULL;
612 u8 ext_capab[18];
613 int ext_capab_len, frame_id;
614 enum wpa_driver_if_type type = WPA_IF_STATION;
615
616 #ifdef CONFIG_P2P
617 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
618 type = WPA_IF_P2P_CLIENT;
619 #endif /* CONFIG_P2P */
620
621 wpa_drv_get_ext_capa(wpa_s, type);
622
623 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
624 sizeof(ext_capab), NULL);
625 if (ext_capab_len > 0 &&
626 wpabuf_resize(&default_ies, ext_capab_len) == 0)
627 wpabuf_put_data(default_ies, ext_capab, ext_capab_len);
628
629 #ifdef CONFIG_MBO
630 if (wpa_s->enable_oce & OCE_STA)
631 wpas_fils_req_param_add_max_channel(wpa_s, &default_ies);
632 /* Send MBO and OCE capabilities */
633 if (wpabuf_resize(&default_ies, 12) == 0)
634 wpas_mbo_scan_ie(wpa_s, default_ies);
635 #endif /* CONFIG_MBO */
636
637 if (type == WPA_IF_P2P_CLIENT)
638 frame_id = VENDOR_ELEM_PROBE_REQ_P2P;
639 else
640 frame_id = VENDOR_ELEM_PROBE_REQ;
641
642 if (wpa_s->vendor_elem[frame_id]) {
643 size_t len;
644
645 len = wpabuf_len(wpa_s->vendor_elem[frame_id]);
646 if (len > 0 && wpabuf_resize(&default_ies, len) == 0)
647 wpabuf_put_buf(default_ies,
648 wpa_s->vendor_elem[frame_id]);
649 }
650
651 if (default_ies)
652 wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies),
653 wpabuf_len(default_ies));
654 wpabuf_free(default_ies);
655 }
656
657
wpa_supplicant_ml_probe_ie(int mld_id,u16 links)658 static struct wpabuf * wpa_supplicant_ml_probe_ie(int mld_id, u16 links)
659 {
660 struct wpabuf *extra_ie;
661 u16 control = MULTI_LINK_CONTROL_TYPE_PROBE_REQ;
662 size_t len = 3 + 4 + 4 * MAX_NUM_MLD_LINKS;
663 u8 link_id;
664 u8 *len_pos;
665
666 if (mld_id >= 0) {
667 control |= EHT_ML_PRES_BM_PROBE_REQ_AP_MLD_ID;
668 len++;
669 }
670
671 extra_ie = wpabuf_alloc(len);
672 if (!extra_ie)
673 return NULL;
674
675 wpabuf_put_u8(extra_ie, WLAN_EID_EXTENSION);
676 len_pos = wpabuf_put(extra_ie, 1);
677 wpabuf_put_u8(extra_ie, WLAN_EID_EXT_MULTI_LINK);
678
679 wpabuf_put_le16(extra_ie, control);
680
681 /* common info length and MLD ID (if requested) */
682 if (mld_id >= 0) {
683 wpabuf_put_u8(extra_ie, 2);
684 wpabuf_put_u8(extra_ie, mld_id);
685
686 wpa_printf(MSG_DEBUG, "MLD: ML probe targeted at MLD ID %d",
687 mld_id);
688 } else {
689 wpabuf_put_u8(extra_ie, 1);
690
691 wpa_printf(MSG_DEBUG, "MLD: ML probe targeted at receiving AP");
692 }
693
694 if (!links)
695 wpa_printf(MSG_DEBUG, "MLD: Probing all links");
696 else
697 wpa_printf(MSG_DEBUG, "MLD: Probing links 0x%04x", links);
698
699 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
700 if (!(links & BIT(link_id)))
701 continue;
702
703 wpabuf_put_u8(extra_ie, EHT_ML_SUB_ELEM_PER_STA_PROFILE);
704
705 /* Subelement length includes only the control */
706 wpabuf_put_u8(extra_ie, 2);
707
708 control = link_id | EHT_PER_STA_CTRL_COMPLETE_PROFILE_MSK;
709
710 wpabuf_put_le16(extra_ie, control);
711 }
712
713 *len_pos = (u8 *) wpabuf_put(extra_ie, 0) - len_pos - 1;
714
715 return extra_ie;
716 }
717
718
wpa_supplicant_extra_ies(struct wpa_supplicant * wpa_s)719 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
720 {
721 struct wpabuf *extra_ie = NULL;
722 u8 ext_capab[18];
723 int ext_capab_len;
724 #ifdef CONFIG_WPS
725 int wps = 0;
726 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
727 #endif /* CONFIG_WPS */
728
729 if (!is_zero_ether_addr(wpa_s->ml_probe_bssid)) {
730 extra_ie = wpa_supplicant_ml_probe_ie(wpa_s->ml_probe_mld_id,
731 wpa_s->ml_probe_links);
732
733 /* No other elements should be included in the probe request */
734 wpa_printf(MSG_DEBUG, "MLD: Scan including only ML element");
735 return extra_ie;
736 }
737
738 #ifdef CONFIG_P2P
739 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT)
740 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
741 else
742 #endif /* CONFIG_P2P */
743 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
744
745 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
746 sizeof(ext_capab), NULL);
747 if (ext_capab_len > 0 &&
748 wpabuf_resize(&extra_ie, ext_capab_len) == 0)
749 wpabuf_put_data(extra_ie, ext_capab, ext_capab_len);
750
751 #ifdef CONFIG_INTERWORKING
752 if (wpa_s->conf->interworking &&
753 wpabuf_resize(&extra_ie, 100) == 0)
754 wpas_add_interworking_elements(wpa_s, extra_ie);
755 #endif /* CONFIG_INTERWORKING */
756
757 #ifdef CONFIG_MBO
758 if (wpa_s->enable_oce & OCE_STA)
759 wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie);
760 #endif /* CONFIG_MBO */
761
762 #ifdef CONFIG_WPS
763 wps = wpas_wps_in_use(wpa_s, &req_type);
764
765 if (wps) {
766 struct wpabuf *wps_ie;
767 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
768 DEV_PW_DEFAULT,
769 &wpa_s->wps->dev,
770 wpa_s->wps->uuid, req_type,
771 0, NULL);
772 if (wps_ie) {
773 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
774 wpabuf_put_buf(extra_ie, wps_ie);
775 wpabuf_free(wps_ie);
776 }
777 }
778
779 #ifdef CONFIG_P2P
780 if (wps) {
781 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
782 if (wpabuf_resize(&extra_ie, ielen) == 0)
783 wpas_p2p_scan_ie(wpa_s, extra_ie);
784 }
785 #endif /* CONFIG_P2P */
786
787 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
788
789 #endif /* CONFIG_WPS */
790
791 #ifdef CONFIG_HS20
792 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 9) == 0)
793 wpas_hs20_add_indication(extra_ie, -1, 0);
794 #endif /* CONFIG_HS20 */
795
796 #ifdef CONFIG_FST
797 if (wpa_s->fst_ies &&
798 wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0)
799 wpabuf_put_buf(extra_ie, wpa_s->fst_ies);
800 #endif /* CONFIG_FST */
801
802 #ifdef CONFIG_MBO
803 /* Send MBO and OCE capabilities */
804 if (wpabuf_resize(&extra_ie, 12) == 0)
805 wpas_mbo_scan_ie(wpa_s, extra_ie);
806 #endif /* CONFIG_MBO */
807
808 if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) {
809 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ];
810
811 if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0)
812 wpabuf_put_buf(extra_ie, buf);
813 }
814
815 return extra_ie;
816 }
817
818
819 #ifdef CONFIG_P2P
820
821 /*
822 * Check whether there are any enabled networks or credentials that could be
823 * used for a non-P2P connection.
824 */
non_p2p_network_enabled(struct wpa_supplicant * wpa_s)825 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
826 {
827 struct wpa_ssid *ssid;
828
829 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
830 if (wpas_network_disabled(wpa_s, ssid))
831 continue;
832 if (!ssid->p2p_group)
833 return 1;
834 }
835
836 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
837 wpa_s->conf->auto_interworking)
838 return 1;
839
840 return 0;
841 }
842
843 #endif /* CONFIG_P2P */
844
845
wpa_add_scan_freqs_list(struct wpa_supplicant * wpa_s,enum hostapd_hw_mode band,struct wpa_driver_scan_params * params,bool is_6ghz,bool only_6ghz_psc,bool exclude_radar)846 int wpa_add_scan_freqs_list(struct wpa_supplicant *wpa_s,
847 enum hostapd_hw_mode band,
848 struct wpa_driver_scan_params *params,
849 bool is_6ghz, bool only_6ghz_psc,
850 bool exclude_radar)
851 {
852 /* Include only supported channels for the specified band */
853 struct hostapd_hw_modes *mode;
854 int num_chans = 0;
855 int *freqs, i;
856
857 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, is_6ghz);
858 if (!mode || !mode->num_channels)
859 return -1;
860
861 if (params->freqs) {
862 while (params->freqs[num_chans])
863 num_chans++;
864 }
865
866 freqs = os_realloc(params->freqs,
867 (num_chans + mode->num_channels + 1) * sizeof(int));
868 if (!freqs)
869 return -1;
870
871 params->freqs = freqs;
872 for (i = 0; i < mode->num_channels; i++) {
873 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
874 continue;
875 if (exclude_radar &&
876 (mode->channels[i].flag & HOSTAPD_CHAN_RADAR))
877 continue;
878
879 if (is_6ghz && only_6ghz_psc &&
880 !is_6ghz_psc_frequency(mode->channels[i].freq))
881 continue;
882
883 params->freqs[num_chans++] = mode->channels[i].freq;
884 }
885 params->freqs[num_chans] = 0;
886
887 return 0;
888 }
889
890
wpa_setband_scan_freqs(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)891 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
892 struct wpa_driver_scan_params *params)
893 {
894 if (wpa_s->hw.modes == NULL)
895 return; /* unknown what channels the driver supports */
896 if (params->freqs)
897 return; /* already using a limited channel set */
898
899 if (wpa_s->setband_mask & WPA_SETBAND_5G)
900 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
901 false, false, false);
902 if (wpa_s->setband_mask & WPA_SETBAND_2G)
903 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
904 false, false, false);
905 if (wpa_s->setband_mask & WPA_SETBAND_6G)
906 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
907 true, false, false);
908 }
909
910
wpa_add_scan_ssid(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,size_t max_ssids,const u8 * ssid,size_t ssid_len)911 static void wpa_add_scan_ssid(struct wpa_supplicant *wpa_s,
912 struct wpa_driver_scan_params *params,
913 size_t max_ssids, const u8 *ssid, size_t ssid_len)
914 {
915 unsigned int j;
916
917 for (j = 0; j < params->num_ssids; j++) {
918 if (params->ssids[j].ssid_len == ssid_len &&
919 params->ssids[j].ssid &&
920 os_memcmp(params->ssids[j].ssid, ssid, ssid_len) == 0)
921 return; /* already in the list */
922 }
923
924 if (params->num_ssids + 1 > max_ssids) {
925 wpa_printf(MSG_DEBUG, "Over max scan SSIDs for manual request");
926 return;
927 }
928
929 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
930 wpa_ssid_txt(ssid, ssid_len));
931
932 params->ssids[params->num_ssids].ssid = ssid;
933 params->ssids[params->num_ssids].ssid_len = ssid_len;
934 params->num_ssids++;
935 }
936
937
wpa_add_owe_scan_ssid(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,struct wpa_ssid * ssid,size_t max_ssids)938 static void wpa_add_owe_scan_ssid(struct wpa_supplicant *wpa_s,
939 struct wpa_driver_scan_params *params,
940 struct wpa_ssid *ssid, size_t max_ssids)
941 {
942 #ifdef CONFIG_OWE
943 struct wpa_bss *bss;
944
945 if (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE))
946 return;
947
948 wpa_printf(MSG_DEBUG, "OWE: Look for transition mode AP. ssid=%s",
949 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
950
951 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
952 const u8 *owe, *pos, *end;
953 const u8 *owe_ssid;
954 size_t owe_ssid_len;
955
956 if (bss->ssid_len != ssid->ssid_len ||
957 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) != 0)
958 continue;
959
960 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
961 if (!owe || owe[1] < 4)
962 continue;
963
964 pos = owe + 6;
965 end = owe + 2 + owe[1];
966
967 /* Must include BSSID and ssid_len */
968 if (end - pos < ETH_ALEN + 1)
969 return;
970
971 /* Skip BSSID */
972 pos += ETH_ALEN;
973 owe_ssid_len = *pos++;
974 owe_ssid = pos;
975
976 if ((size_t) (end - pos) < owe_ssid_len ||
977 owe_ssid_len > SSID_MAX_LEN)
978 return;
979
980 wpa_printf(MSG_DEBUG,
981 "OWE: scan_ssids: transition mode OWE ssid=%s",
982 wpa_ssid_txt(owe_ssid, owe_ssid_len));
983
984 wpa_add_scan_ssid(wpa_s, params, max_ssids,
985 owe_ssid, owe_ssid_len);
986 return;
987 }
988 #endif /* CONFIG_OWE */
989 }
990
991
wpa_set_scan_ssids(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,size_t max_ssids)992 static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
993 struct wpa_driver_scan_params *params,
994 size_t max_ssids)
995 {
996 unsigned int i;
997 struct wpa_ssid *ssid;
998
999 /*
1000 * For devices with max_ssids greater than 1, leave the last slot empty
1001 * for adding the wildcard scan entry.
1002 */
1003 max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids;
1004
1005 for (i = 0; i < wpa_s->scan_id_count; i++) {
1006 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
1007 if (!ssid)
1008 continue;
1009 if (ssid->scan_ssid)
1010 wpa_add_scan_ssid(wpa_s, params, max_ssids,
1011 ssid->ssid, ssid->ssid_len);
1012 /*
1013 * Also add the SSID of the OWE BSS, to allow discovery of
1014 * transition mode APs more quickly.
1015 */
1016 wpa_add_owe_scan_ssid(wpa_s, params, ssid, max_ssids);
1017 }
1018
1019 wpa_s->scan_id_count = 0;
1020 }
1021
1022
wpa_set_ssids_from_scan_req(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,size_t max_ssids)1023 static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s,
1024 struct wpa_driver_scan_params *params,
1025 size_t max_ssids)
1026 {
1027 unsigned int i;
1028
1029 if (wpa_s->ssids_from_scan_req == NULL ||
1030 wpa_s->num_ssids_from_scan_req == 0)
1031 return 0;
1032
1033 if (wpa_s->num_ssids_from_scan_req > max_ssids) {
1034 wpa_s->num_ssids_from_scan_req = max_ssids;
1035 wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u",
1036 (unsigned int) max_ssids);
1037 }
1038
1039 for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) {
1040 params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid;
1041 params->ssids[i].ssid_len =
1042 wpa_s->ssids_from_scan_req[i].ssid_len;
1043 wpa_hexdump_ascii(MSG_DEBUG, "specific SSID",
1044 params->ssids[i].ssid,
1045 params->ssids[i].ssid_len);
1046 }
1047
1048 params->num_ssids = wpa_s->num_ssids_from_scan_req;
1049 wpa_s->num_ssids_from_scan_req = 0;
1050 return 1;
1051 }
1052
1053
wpa_supplicant_scan(void * eloop_ctx,void * timeout_ctx)1054 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
1055 {
1056 struct wpa_supplicant *wpa_s = eloop_ctx;
1057 struct wpa_ssid *ssid;
1058 int ret, p2p_in_prog;
1059 struct wpabuf *extra_ie = NULL;
1060 struct wpa_driver_scan_params params;
1061 struct wpa_driver_scan_params *scan_params;
1062 size_t max_ssids;
1063 int connect_without_scan = 0;
1064
1065 wpa_s->ignore_post_flush_scan_res = 0;
1066
1067 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1068 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
1069 return;
1070 }
1071
1072 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
1073 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
1074 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1075 return;
1076 }
1077
1078 if (wpa_s->scanning) {
1079 /*
1080 * If we are already in scanning state, we shall reschedule the
1081 * the incoming scan request.
1082 */
1083 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
1084 wpa_supplicant_req_scan(wpa_s, 1, 0);
1085 return;
1086 }
1087
1088 if (!wpa_supplicant_enabled_networks(wpa_s) &&
1089 wpa_s->scan_req == NORMAL_SCAN_REQ) {
1090 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
1091 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1092 return;
1093 }
1094
1095 if (wpa_s->conf->ap_scan != 0 &&
1096 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
1097 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
1098 "overriding ap_scan configuration");
1099 wpa_s->conf->ap_scan = 0;
1100 wpas_notify_ap_scan_changed(wpa_s);
1101 }
1102
1103 if (wpa_s->conf->ap_scan == 0) {
1104 wpa_supplicant_gen_assoc_event(wpa_s);
1105 return;
1106 }
1107
1108 ssid = NULL;
1109 if (wpa_s->scan_req != MANUAL_SCAN_REQ &&
1110 wpa_s->connect_without_scan) {
1111 connect_without_scan = 1;
1112 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1113 if (ssid == wpa_s->connect_without_scan)
1114 break;
1115 }
1116 }
1117
1118 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
1119 if (p2p_in_prog && p2p_in_prog != 2 &&
1120 (!ssid ||
1121 (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) {
1122 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
1123 wpa_supplicant_req_scan(wpa_s, 5, 0);
1124 return;
1125 }
1126
1127 /*
1128 * Don't cancel the scan based on ongoing PNO; defer it. Some scans are
1129 * used for changing modes inside wpa_supplicant (roaming,
1130 * auto-reconnect, etc). Discarding the scan might hurt these processes.
1131 * The normal use case for PNO is to suspend the host immediately after
1132 * starting PNO, so the periodic 100 ms attempts to run the scan do not
1133 * normally happen in practice multiple times, i.e., this is simply
1134 * restarting scanning once the host is woken up and PNO stopped.
1135 */
1136 if (wpa_s->pno || wpa_s->pno_sched_pending) {
1137 wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress");
1138 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1139 return;
1140 }
1141
1142 if (wpa_s->conf->ap_scan == 2)
1143 max_ssids = 1;
1144 else {
1145 max_ssids = wpa_s->max_scan_ssids;
1146 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
1147 max_ssids = WPAS_MAX_SCAN_SSIDS;
1148 }
1149
1150 wpa_s->last_scan_req = wpa_s->scan_req;
1151 wpa_s->scan_req = NORMAL_SCAN_REQ;
1152
1153 if (connect_without_scan) {
1154 wpa_s->connect_without_scan = NULL;
1155 if (ssid) {
1156 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
1157 "without scan step");
1158 wpa_supplicant_associate(wpa_s, NULL, ssid);
1159 return;
1160 }
1161 }
1162
1163 os_memset(¶ms, 0, sizeof(params));
1164
1165 wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
1166 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1167 wpa_s->wpa_state == WPA_INACTIVE)
1168 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1169
1170 /*
1171 * If autoscan has set its own scanning parameters
1172 */
1173 if (wpa_s->autoscan_params != NULL) {
1174 scan_params = wpa_s->autoscan_params;
1175 goto scan;
1176 }
1177
1178 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1179 wpa_set_ssids_from_scan_req(wpa_s, ¶ms, max_ssids)) {
1180 wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command");
1181 goto ssid_list_set;
1182 }
1183
1184 #ifdef CONFIG_P2P
1185 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
1186 wpa_s->go_params && !wpa_s->conf->passive_scan) {
1187 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
1188 wpa_s->p2p_in_provisioning,
1189 wpa_s->show_group_started);
1190 params.ssids[0].ssid = wpa_s->go_params->ssid;
1191 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
1192 params.num_ssids = 1;
1193 params.bssid = wpa_s->go_params->peer_interface_addr;
1194 wpa_printf(MSG_DEBUG, "P2P: Use specific BSSID " MACSTR
1195 " (peer interface address) for scan",
1196 MAC2STR(params.bssid));
1197 goto ssid_list_set;
1198 }
1199
1200 if (wpa_s->p2p_in_invitation) {
1201 if (wpa_s->current_ssid) {
1202 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
1203 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
1204 params.ssids[0].ssid_len =
1205 wpa_s->current_ssid->ssid_len;
1206 params.num_ssids = 1;
1207 if (wpa_s->current_ssid->bssid_set) {
1208 params.bssid = wpa_s->current_ssid->bssid;
1209 wpa_printf(MSG_DEBUG, "P2P: Use specific BSSID "
1210 MACSTR " for scan",
1211 MAC2STR(params.bssid));
1212 }
1213 } else {
1214 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
1215 }
1216 goto ssid_list_set;
1217 }
1218 #endif /* CONFIG_P2P */
1219
1220 /* Find the starting point from which to continue scanning */
1221 ssid = wpa_s->conf->ssid;
1222 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
1223 while (ssid) {
1224 if (ssid == wpa_s->prev_scan_ssid) {
1225 ssid = ssid->next;
1226 break;
1227 }
1228 ssid = ssid->next;
1229 }
1230 }
1231
1232 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
1233 #ifdef CONFIG_AP
1234 !wpa_s->ap_iface &&
1235 #endif /* CONFIG_AP */
1236 wpa_s->conf->ap_scan == 2) {
1237 wpa_s->connect_without_scan = NULL;
1238 wpa_s->prev_scan_wildcard = 0;
1239 wpa_supplicant_assoc_try(wpa_s, ssid);
1240 return;
1241 } else if (wpa_s->conf->ap_scan == 2) {
1242 /*
1243 * User-initiated scan request in ap_scan == 2; scan with
1244 * wildcard SSID.
1245 */
1246 ssid = NULL;
1247 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
1248 /*
1249 * Perform single-channel single-SSID scan for
1250 * reassociate-to-same-BSS operation.
1251 */
1252 /* Setup SSID */
1253 ssid = wpa_s->current_ssid;
1254 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1255 ssid->ssid, ssid->ssid_len);
1256 params.ssids[0].ssid = ssid->ssid;
1257 params.ssids[0].ssid_len = ssid->ssid_len;
1258 params.num_ssids = 1;
1259
1260 /*
1261 * Allocate memory for frequency array, allocate one extra
1262 * slot for the zero-terminator.
1263 */
1264 params.freqs = os_malloc(sizeof(int) * 2);
1265 if (params.freqs) {
1266 params.freqs[0] = wpa_s->assoc_freq;
1267 params.freqs[1] = 0;
1268 }
1269
1270 /*
1271 * Reset the reattach flag so that we fall back to full scan if
1272 * this scan fails.
1273 */
1274 wpa_s->reattach = 0;
1275 } else {
1276 struct wpa_ssid *start = ssid, *tssid;
1277 int freqs_set = 0;
1278 if (ssid == NULL && max_ssids > 1)
1279 ssid = wpa_s->conf->ssid;
1280 while (ssid) {
1281 if (!wpas_network_disabled(wpa_s, ssid) &&
1282 ssid->scan_ssid) {
1283 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1284 ssid->ssid, ssid->ssid_len);
1285 params.ssids[params.num_ssids].ssid =
1286 ssid->ssid;
1287 params.ssids[params.num_ssids].ssid_len =
1288 ssid->ssid_len;
1289 params.num_ssids++;
1290 if (params.num_ssids + 1 >= max_ssids)
1291 break;
1292 }
1293
1294 if (!wpas_network_disabled(wpa_s, ssid)) {
1295 /*
1296 * Also add the SSID of the OWE BSS, to allow
1297 * discovery of transition mode APs more
1298 * quickly.
1299 */
1300 wpa_add_owe_scan_ssid(wpa_s, ¶ms, ssid,
1301 max_ssids);
1302 }
1303
1304 ssid = ssid->next;
1305 if (ssid == start)
1306 break;
1307 if (ssid == NULL && max_ssids > 1 &&
1308 start != wpa_s->conf->ssid)
1309 ssid = wpa_s->conf->ssid;
1310 }
1311
1312 if (wpa_s->scan_id_count &&
1313 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
1314 wpa_set_scan_ssids(wpa_s, ¶ms, max_ssids);
1315
1316 for (tssid = wpa_s->conf->ssid;
1317 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
1318 tssid = tssid->next) {
1319 if (wpas_network_disabled(wpa_s, tssid))
1320 continue;
1321 if (((params.freqs || !freqs_set) &&
1322 tssid->scan_freq) &&
1323 int_array_len(params.freqs) < 100) {
1324 int_array_concat(¶ms.freqs,
1325 tssid->scan_freq);
1326 } else {
1327 os_free(params.freqs);
1328 params.freqs = NULL;
1329 }
1330 freqs_set = 1;
1331 }
1332 int_array_sort_unique(params.freqs);
1333 }
1334
1335 if (ssid && max_ssids == 1) {
1336 /*
1337 * If the driver is limited to 1 SSID at a time interleave
1338 * wildcard SSID scans with specific SSID scans to avoid
1339 * waiting a long time for a wildcard scan.
1340 */
1341 if (!wpa_s->prev_scan_wildcard) {
1342 params.ssids[0].ssid = NULL;
1343 params.ssids[0].ssid_len = 0;
1344 wpa_s->prev_scan_wildcard = 1;
1345 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
1346 "wildcard SSID (Interleave with specific)");
1347 } else {
1348 wpa_s->prev_scan_ssid = ssid;
1349 wpa_s->prev_scan_wildcard = 0;
1350 wpa_dbg(wpa_s, MSG_DEBUG,
1351 "Starting AP scan for specific SSID: %s",
1352 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1353 }
1354 } else if (ssid) {
1355 /* max_ssids > 1 */
1356
1357 wpa_s->prev_scan_ssid = ssid;
1358 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
1359 "the scan request");
1360 params.num_ssids++;
1361 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1362 wpa_s->manual_scan_passive && params.num_ssids == 0) {
1363 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
1364 } else if (wpa_s->conf->passive_scan) {
1365 wpa_dbg(wpa_s, MSG_DEBUG,
1366 "Use passive scan based on configuration");
1367 } else {
1368 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1369 params.num_ssids++;
1370 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
1371 "SSID");
1372 }
1373
1374 ssid_list_set:
1375 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
1376 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1377
1378 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1379 wpa_s->manual_scan_only_new) {
1380 wpa_printf(MSG_DEBUG,
1381 "Request driver to clear scan cache due to manual only_new=1 scan");
1382 params.only_new_results = 1;
1383 }
1384
1385 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
1386 wpa_s->manual_scan_freqs) {
1387 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
1388 params.freqs = wpa_s->manual_scan_freqs;
1389 wpa_s->manual_scan_freqs = NULL;
1390 }
1391
1392 if (params.freqs == NULL && wpa_s->select_network_scan_freqs) {
1393 wpa_dbg(wpa_s, MSG_DEBUG,
1394 "Limit select_network scan to specified channels");
1395 params.freqs = wpa_s->select_network_scan_freqs;
1396 wpa_s->select_network_scan_freqs = NULL;
1397 }
1398
1399 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
1400 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
1401 "generated frequency list");
1402 params.freqs = wpa_s->next_scan_freqs;
1403 } else
1404 os_free(wpa_s->next_scan_freqs);
1405 wpa_s->next_scan_freqs = NULL;
1406 wpa_setband_scan_freqs(wpa_s, ¶ms);
1407
1408 /* See if user specified frequencies. If so, scan only those. */
1409 if (wpa_s->last_scan_req == INITIAL_SCAN_REQ &&
1410 wpa_s->conf->initial_freq_list && !params.freqs) {
1411 wpa_dbg(wpa_s, MSG_DEBUG,
1412 "Optimize scan based on conf->initial_freq_list");
1413 int_array_concat(¶ms.freqs, wpa_s->conf->initial_freq_list);
1414 } else if (wpa_s->conf->freq_list && !params.freqs) {
1415 wpa_dbg(wpa_s, MSG_DEBUG,
1416 "Optimize scan based on conf->freq_list");
1417 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
1418 }
1419
1420 /* Use current associated channel? */
1421 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
1422 unsigned int num = wpa_s->num_multichan_concurrent;
1423
1424 params.freqs = os_calloc(num + 1, sizeof(int));
1425 if (params.freqs) {
1426 num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1427 false);
1428 if (num > 0) {
1429 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
1430 "current operating channels since "
1431 "scan_cur_freq is enabled");
1432 } else {
1433 os_free(params.freqs);
1434 params.freqs = NULL;
1435 }
1436 }
1437 }
1438
1439 #ifdef CONFIG_MBO
1440 if (wpa_s->enable_oce & OCE_STA)
1441 params.oce_scan = 1;
1442 #endif /* CONFIG_MBO */
1443
1444 params.filter_ssids = wpa_supplicant_build_filter_ssids(
1445 wpa_s->conf, ¶ms.num_filter_ssids);
1446 if (extra_ie) {
1447 params.extra_ies = wpabuf_head(extra_ie);
1448 params.extra_ies_len = wpabuf_len(extra_ie);
1449 }
1450
1451 #ifdef CONFIG_P2P
1452 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
1453 (wpa_s->show_group_started && wpa_s->go_params)) {
1454 /*
1455 * The interface may not yet be in P2P mode, so we have to
1456 * explicitly request P2P probe to disable CCK rates.
1457 */
1458 params.p2p_probe = 1;
1459 }
1460 #endif /* CONFIG_P2P */
1461
1462 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) &&
1463 wpa_s->wpa_state <= WPA_SCANNING)
1464 wpa_setup_mac_addr_rand_params(¶ms, wpa_s->mac_addr_scan);
1465
1466 if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) {
1467 struct wpa_bss *bss;
1468
1469 params.bssid = wpa_s->next_scan_bssid;
1470 bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid);
1471 if (!wpa_s->next_scan_bssid_wildcard_ssid &&
1472 bss && bss->ssid_len && params.num_ssids == 1 &&
1473 params.ssids[0].ssid_len == 0) {
1474 params.ssids[0].ssid = bss->ssid;
1475 params.ssids[0].ssid_len = bss->ssid_len;
1476 wpa_dbg(wpa_s, MSG_DEBUG,
1477 "Scan a previously specified BSSID " MACSTR
1478 " and SSID %s",
1479 MAC2STR(params.bssid),
1480 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1481 } else {
1482 wpa_dbg(wpa_s, MSG_DEBUG,
1483 "Scan a previously specified BSSID " MACSTR,
1484 MAC2STR(params.bssid));
1485 }
1486 } else if (!is_zero_ether_addr(wpa_s->ml_probe_bssid)) {
1487 wpa_printf(MSG_DEBUG, "Scanning for ML probe request");
1488 params.bssid = wpa_s->ml_probe_bssid;
1489 params.min_probe_req_content = true;
1490 }
1491
1492
1493 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1494 wpa_s->manual_non_coloc_6ghz) {
1495 wpa_dbg(wpa_s, MSG_DEBUG, "Collocated 6 GHz logic is disabled");
1496 params.non_coloc_6ghz = 1;
1497 }
1498
1499 scan_params = ¶ms;
1500
1501 scan:
1502 #ifdef CONFIG_P2P
1503 /*
1504 * If the driver does not support multi-channel concurrency and a
1505 * virtual interface that shares the same radio with the wpa_s interface
1506 * is operating there may not be need to scan other channels apart from
1507 * the current operating channel on the other virtual interface. Filter
1508 * out other channels in case we are trying to find a connection for a
1509 * station interface when we are not configured to prefer station
1510 * connection and a concurrent operation is already in process.
1511 */
1512 if (wpa_s->scan_for_connection &&
1513 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
1514 !scan_params->freqs && !params.freqs &&
1515 wpas_is_p2p_prioritized(wpa_s) &&
1516 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
1517 non_p2p_network_enabled(wpa_s)) {
1518 unsigned int num = wpa_s->num_multichan_concurrent;
1519
1520 params.freqs = os_calloc(num + 1, sizeof(int));
1521 if (params.freqs) {
1522 /*
1523 * Exclude the operating frequency of the current
1524 * interface since we're looking to transition off of
1525 * it.
1526 */
1527 num = get_shared_radio_freqs(wpa_s, params.freqs, num,
1528 true);
1529 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
1530 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
1531 } else {
1532 os_free(params.freqs);
1533 params.freqs = NULL;
1534 }
1535 }
1536 }
1537
1538 if (!params.freqs && wpas_is_6ghz_supported(wpa_s, true) &&
1539 (wpa_s->p2p_in_invitation || wpa_s->p2p_in_provisioning))
1540 wpas_p2p_scan_freqs(wpa_s, ¶ms, true);
1541 #endif /* CONFIG_P2P */
1542
1543 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params, false, false);
1544
1545 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1546 !wpa_s->manual_scan_freqs) {
1547 /* Restore manual_scan_freqs for the next attempt */
1548 wpa_s->manual_scan_freqs = params.freqs;
1549 params.freqs = NULL;
1550 }
1551
1552 wpabuf_free(extra_ie);
1553 os_free(params.freqs);
1554 os_free(params.filter_ssids);
1555 os_free(params.mac_addr);
1556
1557 if (ret) {
1558 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1559 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1560 wpa_supplicant_set_state(wpa_s,
1561 wpa_s->scan_prev_wpa_state);
1562 /* Restore scan_req since we will try to scan again */
1563 wpa_s->scan_req = wpa_s->last_scan_req;
1564 wpa_supplicant_req_scan(wpa_s, 1, 0);
1565 } else {
1566 wpa_s->scan_for_connection = 0;
1567 #ifdef CONFIG_INTERWORKING
1568 wpa_s->interworking_fast_assoc_tried = 0;
1569 #endif /* CONFIG_INTERWORKING */
1570 wpa_s->next_scan_bssid_wildcard_ssid = 0;
1571 if (params.bssid)
1572 os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
1573 }
1574
1575 wpa_s->ml_probe_mld_id = -1;
1576 wpa_s->ml_probe_links = 0;
1577 os_memset(wpa_s->ml_probe_bssid, 0, sizeof(wpa_s->ml_probe_bssid));
1578 }
1579
1580
wpa_supplicant_update_scan_int(struct wpa_supplicant * wpa_s,int sec)1581 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1582 {
1583 struct os_reltime remaining, new_int;
1584 int cancelled;
1585
1586 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1587 &remaining);
1588
1589 new_int.sec = sec;
1590 new_int.usec = 0;
1591 if (cancelled && os_reltime_before(&remaining, &new_int)) {
1592 new_int.sec = remaining.sec;
1593 new_int.usec = remaining.usec;
1594 }
1595
1596 if (cancelled) {
1597 eloop_register_timeout(new_int.sec, new_int.usec,
1598 wpa_supplicant_scan, wpa_s, NULL);
1599 }
1600 wpa_s->scan_interval = sec;
1601 }
1602
1603
1604 /**
1605 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1606 * @wpa_s: Pointer to wpa_supplicant data
1607 * @sec: Number of seconds after which to scan
1608 * @usec: Number of microseconds after which to scan
1609 *
1610 * This function is used to schedule a scan for neighboring access points after
1611 * the specified time.
1612 */
wpa_supplicant_req_scan(struct wpa_supplicant * wpa_s,int sec,int usec)1613 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1614 {
1615 int res;
1616
1617 if (wpa_s->p2p_mgmt) {
1618 wpa_dbg(wpa_s, MSG_DEBUG,
1619 "Ignore scan request (%d.%06d sec) on p2p_mgmt interface",
1620 sec, usec);
1621 return;
1622 }
1623
1624 res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1625 NULL);
1626 if (res == 1) {
1627 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1628 sec, usec);
1629 } else if (res == 0) {
1630 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1631 sec, usec);
1632 } else {
1633 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1634 sec, usec);
1635 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1636 }
1637 }
1638
1639
1640 /**
1641 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1642 * @wpa_s: Pointer to wpa_supplicant data
1643 * @sec: Number of seconds after which to scan
1644 * @usec: Number of microseconds after which to scan
1645 * Returns: 0 on success or -1 otherwise
1646 *
1647 * This function is used to schedule periodic scans for neighboring
1648 * access points after the specified time.
1649 */
wpa_supplicant_delayed_sched_scan(struct wpa_supplicant * wpa_s,int sec,int usec)1650 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1651 int sec, int usec)
1652 {
1653 if (!wpa_s->sched_scan_supported)
1654 return -1;
1655
1656 eloop_register_timeout(sec, usec,
1657 wpa_supplicant_delayed_sched_scan_timeout,
1658 wpa_s, NULL);
1659
1660 return 0;
1661 }
1662
1663
1664 static void
wpa_scan_set_relative_rssi_params(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params)1665 wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s,
1666 struct wpa_driver_scan_params *params)
1667 {
1668 if (wpa_s->wpa_state != WPA_COMPLETED ||
1669 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) ||
1670 wpa_s->srp.relative_rssi_set == 0)
1671 return;
1672
1673 params->relative_rssi_set = 1;
1674 params->relative_rssi = wpa_s->srp.relative_rssi;
1675
1676 if (wpa_s->srp.relative_adjust_rssi == 0)
1677 return;
1678
1679 params->relative_adjust_band = wpa_s->srp.relative_adjust_band;
1680 params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi;
1681 }
1682
1683
1684 /**
1685 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1686 * @wpa_s: Pointer to wpa_supplicant data
1687 * Returns: 0 is sched_scan was started or -1 otherwise
1688 *
1689 * This function is used to schedule periodic scans for neighboring
1690 * access points repeating the scan continuously.
1691 */
wpa_supplicant_req_sched_scan(struct wpa_supplicant * wpa_s)1692 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1693 {
1694 struct wpa_driver_scan_params params;
1695 struct wpa_driver_scan_params *scan_params;
1696 enum wpa_states prev_state;
1697 struct wpa_ssid *ssid = NULL;
1698 struct wpabuf *extra_ie = NULL;
1699 int ret;
1700 unsigned int max_sched_scan_ssids;
1701 int wildcard = 0;
1702 int need_ssids;
1703 struct sched_scan_plan scan_plan;
1704
1705 if (!wpa_s->sched_scan_supported)
1706 return -1;
1707
1708 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1709 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1710 else
1711 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1712 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1713 return -1;
1714
1715 wpa_s->sched_scan_stop_req = 0;
1716
1717 if (wpa_s->sched_scanning) {
1718 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1719 return 0;
1720 }
1721
1722 need_ssids = 0;
1723 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1724 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1725 /* Use wildcard SSID to find this network */
1726 wildcard = 1;
1727 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1728 ssid->ssid_len)
1729 need_ssids++;
1730
1731 #ifdef CONFIG_WPS
1732 if (!wpas_network_disabled(wpa_s, ssid) &&
1733 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1734 /*
1735 * Normal scan is more reliable and faster for WPS
1736 * operations and since these are for short periods of
1737 * time, the benefit of trying to use sched_scan would
1738 * be limited.
1739 */
1740 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1741 "sched_scan for WPS");
1742 return -1;
1743 }
1744 #endif /* CONFIG_WPS */
1745 }
1746 if (wildcard)
1747 need_ssids++;
1748
1749 if (wpa_s->normal_scans < 3 &&
1750 (need_ssids <= wpa_s->max_scan_ssids ||
1751 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1752 /*
1753 * When normal scan can speed up operations, use that for the
1754 * first operations before starting the sched_scan to allow
1755 * user space sleep more. We do this only if the normal scan
1756 * has functionality that is suitable for this or if the
1757 * sched_scan does not have better support for multiple SSIDs.
1758 */
1759 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1760 "sched_scan for initial scans (normal_scans=%d)",
1761 wpa_s->normal_scans);
1762 return -1;
1763 }
1764
1765 os_memset(¶ms, 0, sizeof(params));
1766
1767 /* If we can't allocate space for the filters, we just don't filter */
1768 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1769 sizeof(struct wpa_driver_scan_filter));
1770
1771 prev_state = wpa_s->wpa_state;
1772 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1773 wpa_s->wpa_state == WPA_INACTIVE)
1774 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1775
1776 if (wpa_s->autoscan_params != NULL) {
1777 scan_params = wpa_s->autoscan_params;
1778 goto scan;
1779 }
1780
1781 /* Find the starting point from which to continue scanning */
1782 ssid = wpa_s->conf->ssid;
1783 if (wpa_s->prev_sched_ssid) {
1784 while (ssid) {
1785 if (ssid == wpa_s->prev_sched_ssid) {
1786 ssid = ssid->next;
1787 break;
1788 }
1789 ssid = ssid->next;
1790 }
1791 }
1792
1793 if (!ssid || !wpa_s->prev_sched_ssid) {
1794 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1795 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1796 wpa_s->first_sched_scan = 1;
1797 ssid = wpa_s->conf->ssid;
1798 wpa_s->prev_sched_ssid = ssid;
1799 }
1800
1801 if (wildcard) {
1802 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1803 params.num_ssids++;
1804 }
1805
1806 while (ssid) {
1807 if (wpas_network_disabled(wpa_s, ssid))
1808 goto next;
1809
1810 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1811 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1812 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1813 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1814 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1815 ssid->ssid, ssid->ssid_len);
1816 params.filter_ssids[params.num_filter_ssids].ssid_len =
1817 ssid->ssid_len;
1818 params.num_filter_ssids++;
1819 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1820 {
1821 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1822 "filter for sched_scan - drop filter");
1823 os_free(params.filter_ssids);
1824 params.filter_ssids = NULL;
1825 params.num_filter_ssids = 0;
1826 }
1827
1828 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1829 if (params.num_ssids == max_sched_scan_ssids)
1830 break; /* only room for broadcast SSID */
1831 wpa_dbg(wpa_s, MSG_DEBUG,
1832 "add to active scan ssid: %s",
1833 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1834 params.ssids[params.num_ssids].ssid =
1835 ssid->ssid;
1836 params.ssids[params.num_ssids].ssid_len =
1837 ssid->ssid_len;
1838 params.num_ssids++;
1839 if (params.num_ssids >= max_sched_scan_ssids) {
1840 wpa_s->prev_sched_ssid = ssid;
1841 do {
1842 ssid = ssid->next;
1843 } while (ssid &&
1844 (wpas_network_disabled(wpa_s, ssid) ||
1845 !ssid->scan_ssid));
1846 break;
1847 }
1848 }
1849
1850 next:
1851 wpa_s->prev_sched_ssid = ssid;
1852 ssid = ssid->next;
1853 }
1854
1855 if (params.num_filter_ssids == 0) {
1856 os_free(params.filter_ssids);
1857 params.filter_ssids = NULL;
1858 }
1859
1860 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1861 if (extra_ie) {
1862 params.extra_ies = wpabuf_head(extra_ie);
1863 params.extra_ies_len = wpabuf_len(extra_ie);
1864 }
1865
1866 if (wpa_s->conf->filter_rssi)
1867 params.filter_rssi = wpa_s->conf->filter_rssi;
1868
1869 /* See if user specified frequencies. If so, scan only those. */
1870 if (wpa_s->conf->freq_list && !params.freqs) {
1871 wpa_dbg(wpa_s, MSG_DEBUG,
1872 "Optimize scan based on conf->freq_list");
1873 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
1874 }
1875
1876 #ifdef CONFIG_MBO
1877 if (wpa_s->enable_oce & OCE_STA)
1878 params.oce_scan = 1;
1879 #endif /* CONFIG_MBO */
1880
1881 scan_params = ¶ms;
1882
1883 scan:
1884 wpa_s->sched_scan_timed_out = 0;
1885
1886 /*
1887 * We cannot support multiple scan plans if the scan request includes
1888 * too many SSID's, so in this case use only the last scan plan and make
1889 * it run infinitely. It will be stopped by the timeout.
1890 */
1891 if (wpa_s->sched_scan_plans_num == 1 ||
1892 (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) {
1893 params.sched_scan_plans = wpa_s->sched_scan_plans;
1894 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
1895 } else if (wpa_s->sched_scan_plans_num > 1) {
1896 wpa_dbg(wpa_s, MSG_DEBUG,
1897 "Too many SSIDs. Default to using single scheduled_scan plan");
1898 params.sched_scan_plans =
1899 &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num -
1900 1];
1901 params.sched_scan_plans_num = 1;
1902 } else {
1903 if (wpa_s->conf->sched_scan_interval)
1904 scan_plan.interval = wpa_s->conf->sched_scan_interval;
1905 else
1906 scan_plan.interval = 10;
1907
1908 if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) {
1909 wpa_printf(MSG_WARNING,
1910 "Scan interval too long(%u), use the maximum allowed(%u)",
1911 scan_plan.interval,
1912 wpa_s->max_sched_scan_plan_interval);
1913 scan_plan.interval =
1914 wpa_s->max_sched_scan_plan_interval;
1915 }
1916
1917 scan_plan.iterations = 0;
1918 params.sched_scan_plans = &scan_plan;
1919 params.sched_scan_plans_num = 1;
1920 }
1921
1922 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
1923
1924 if (ssid || !wpa_s->first_sched_scan) {
1925 wpa_dbg(wpa_s, MSG_DEBUG,
1926 "Starting sched scan after %u seconds: interval %u timeout %d",
1927 params.sched_scan_start_delay,
1928 params.sched_scan_plans[0].interval,
1929 wpa_s->sched_scan_timeout);
1930 } else {
1931 wpa_dbg(wpa_s, MSG_DEBUG,
1932 "Starting sched scan after %u seconds (no timeout)",
1933 params.sched_scan_start_delay);
1934 }
1935
1936 wpa_setband_scan_freqs(wpa_s, scan_params);
1937
1938 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) &&
1939 wpa_s->wpa_state <= WPA_SCANNING)
1940 wpa_setup_mac_addr_rand_params(¶ms,
1941 wpa_s->mac_addr_sched_scan);
1942
1943 wpa_scan_set_relative_rssi_params(wpa_s, scan_params);
1944
1945 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params);
1946 wpabuf_free(extra_ie);
1947 os_free(params.filter_ssids);
1948 os_free(params.mac_addr);
1949 if (ret) {
1950 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1951 if (prev_state != wpa_s->wpa_state)
1952 wpa_supplicant_set_state(wpa_s, prev_state);
1953 return ret;
1954 }
1955
1956 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1957 if (ssid || !wpa_s->first_sched_scan) {
1958 wpa_s->sched_scan_timed_out = 0;
1959 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1960 wpa_supplicant_sched_scan_timeout,
1961 wpa_s, NULL);
1962 wpa_s->first_sched_scan = 0;
1963 wpa_s->sched_scan_timeout /= 2;
1964 params.sched_scan_plans[0].interval *= 2;
1965 if ((unsigned int) wpa_s->sched_scan_timeout <
1966 params.sched_scan_plans[0].interval ||
1967 params.sched_scan_plans[0].interval >
1968 wpa_s->max_sched_scan_plan_interval) {
1969 params.sched_scan_plans[0].interval = 10;
1970 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1971 }
1972 }
1973
1974 /* If there is no more ssids, start next time from the beginning */
1975 if (!ssid)
1976 wpa_s->prev_sched_ssid = NULL;
1977
1978 return 0;
1979 }
1980
1981
1982 /**
1983 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1984 * @wpa_s: Pointer to wpa_supplicant data
1985 *
1986 * This function is used to cancel a scan request scheduled with
1987 * wpa_supplicant_req_scan().
1988 */
wpa_supplicant_cancel_scan(struct wpa_supplicant * wpa_s)1989 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1990 {
1991 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1992 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1993 }
1994
1995
1996 /**
1997 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1998 * @wpa_s: Pointer to wpa_supplicant data
1999 *
2000 * This function is used to stop a delayed scheduled scan.
2001 */
wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant * wpa_s)2002 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
2003 {
2004 if (!wpa_s->sched_scan_supported)
2005 return;
2006
2007 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
2008 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
2009 wpa_s, NULL);
2010 }
2011
2012
2013 /**
2014 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
2015 * @wpa_s: Pointer to wpa_supplicant data
2016 *
2017 * This function is used to stop a periodic scheduled scan.
2018 */
wpa_supplicant_cancel_sched_scan(struct wpa_supplicant * wpa_s)2019 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
2020 {
2021 if (!wpa_s->sched_scanning)
2022 return;
2023
2024 if (wpa_s->sched_scanning)
2025 wpa_s->sched_scan_stop_req = 1;
2026
2027 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
2028 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
2029 wpa_supplicant_stop_sched_scan(wpa_s);
2030 }
2031
2032
2033 /**
2034 * wpa_supplicant_notify_scanning - Indicate possible scan state change
2035 * @wpa_s: Pointer to wpa_supplicant data
2036 * @scanning: Whether scanning is currently in progress
2037 *
2038 * This function is to generate scanning notifycations. It is called whenever
2039 * there may have been a change in scanning (scan started, completed, stopped).
2040 * wpas_notify_scanning() is called whenever the scanning state changed from the
2041 * previously notified state.
2042 */
wpa_supplicant_notify_scanning(struct wpa_supplicant * wpa_s,int scanning)2043 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
2044 int scanning)
2045 {
2046 if (wpa_s->scanning != scanning) {
2047 wpa_s->scanning = scanning;
2048 wpas_notify_scanning(wpa_s);
2049 }
2050 }
2051
2052
wpa_scan_get_max_rate(const struct wpa_scan_res * res)2053 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
2054 {
2055 int rate = 0;
2056 const u8 *ie;
2057 int i;
2058
2059 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
2060 for (i = 0; ie && i < ie[1]; i++) {
2061 if ((ie[i + 2] & 0x7f) > rate)
2062 rate = ie[i + 2] & 0x7f;
2063 }
2064
2065 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
2066 for (i = 0; ie && i < ie[1]; i++) {
2067 if ((ie[i + 2] & 0x7f) > rate)
2068 rate = ie[i + 2] & 0x7f;
2069 }
2070
2071 return rate;
2072 }
2073
2074
2075 /**
2076 * wpa_scan_get_ie - Fetch a specified information element from a scan result
2077 * @res: Scan result entry
2078 * @ie: Information element identitifier (WLAN_EID_*)
2079 * Returns: Pointer to the information element (id field) or %NULL if not found
2080 *
2081 * This function returns the first matching information element in the scan
2082 * result.
2083 */
wpa_scan_get_ie(const struct wpa_scan_res * res,u8 ie)2084 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
2085 {
2086 size_t ie_len = res->ie_len;
2087
2088 /* Use the Beacon frame IEs if res->ie_len is not available */
2089 if (!ie_len)
2090 ie_len = res->beacon_ie_len;
2091
2092 return get_ie((const u8 *) (res + 1), ie_len, ie);
2093 }
2094
2095
wpa_scan_get_ml_ie(const struct wpa_scan_res * res,u8 type)2096 const u8 * wpa_scan_get_ml_ie(const struct wpa_scan_res *res, u8 type)
2097 {
2098 size_t ie_len = res->ie_len;
2099
2100 /* Use the Beacon frame IEs if res->ie_len is not available */
2101 if (!ie_len)
2102 ie_len = res->beacon_ie_len;
2103
2104 return get_ml_ie((const u8 *) (res + 1), ie_len, type);
2105 }
2106
2107
2108 /**
2109 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
2110 * @res: Scan result entry
2111 * @vendor_type: Vendor type (four octets starting the IE payload)
2112 * Returns: Pointer to the information element (id field) or %NULL if not found
2113 *
2114 * This function returns the first matching information element in the scan
2115 * result.
2116 */
wpa_scan_get_vendor_ie(const struct wpa_scan_res * res,u32 vendor_type)2117 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
2118 u32 vendor_type)
2119 {
2120 const u8 *ies;
2121 const struct element *elem;
2122
2123 ies = (const u8 *) (res + 1);
2124
2125 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, res->ie_len) {
2126 if (elem->datalen >= 4 &&
2127 vendor_type == WPA_GET_BE32(elem->data))
2128 return &elem->id;
2129 }
2130
2131 return NULL;
2132 }
2133
2134
2135 /**
2136 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
2137 * @res: Scan result entry
2138 * @vendor_type: Vendor type (four octets starting the IE payload)
2139 * Returns: Pointer to the information element (id field) or %NULL if not found
2140 *
2141 * This function returns the first matching information element in the scan
2142 * result.
2143 *
2144 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
2145 * from Beacon frames instead of either Beacon or Probe Response frames.
2146 */
wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res * res,u32 vendor_type)2147 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
2148 u32 vendor_type)
2149 {
2150 const u8 *ies;
2151 const struct element *elem;
2152
2153 if (res->beacon_ie_len == 0)
2154 return NULL;
2155
2156 ies = (const u8 *) (res + 1);
2157 ies += res->ie_len;
2158
2159 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
2160 res->beacon_ie_len) {
2161 if (elem->datalen >= 4 &&
2162 vendor_type == WPA_GET_BE32(elem->data))
2163 return &elem->id;
2164 }
2165
2166 return NULL;
2167 }
2168
2169
2170 /**
2171 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
2172 * @res: Scan result entry
2173 * @vendor_type: Vendor type (four octets starting the IE payload)
2174 * Returns: Pointer to the information element payload or %NULL if not found
2175 *
2176 * This function returns concatenated payload of possibly fragmented vendor
2177 * specific information elements in the scan result. The caller is responsible
2178 * for freeing the returned buffer.
2179 */
wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res * res,u32 vendor_type)2180 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
2181 u32 vendor_type)
2182 {
2183 struct wpabuf *buf;
2184 const u8 *end, *pos;
2185
2186 buf = wpabuf_alloc(res->ie_len);
2187 if (buf == NULL)
2188 return NULL;
2189
2190 pos = (const u8 *) (res + 1);
2191 end = pos + res->ie_len;
2192
2193 while (end - pos > 1) {
2194 u8 ie, len;
2195
2196 ie = pos[0];
2197 len = pos[1];
2198 if (len > end - pos - 2)
2199 break;
2200 pos += 2;
2201 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
2202 vendor_type == WPA_GET_BE32(pos))
2203 wpabuf_put_data(buf, pos + 4, len - 4);
2204 pos += len;
2205 }
2206
2207 if (wpabuf_len(buf) == 0) {
2208 wpabuf_free(buf);
2209 buf = NULL;
2210 }
2211
2212 return buf;
2213 }
2214
2215
wpas_channel_width_offset(enum chan_width cw)2216 static int wpas_channel_width_offset(enum chan_width cw)
2217 {
2218 switch (cw) {
2219 case CHAN_WIDTH_40:
2220 return 1;
2221 case CHAN_WIDTH_80:
2222 return 2;
2223 case CHAN_WIDTH_80P80:
2224 case CHAN_WIDTH_160:
2225 return 3;
2226 case CHAN_WIDTH_320:
2227 return 4;
2228 default:
2229 return 0;
2230 }
2231 }
2232
2233
2234 /**
2235 * wpas_channel_width_tx_pwr - Calculate the max transmit power at the channel
2236 * width
2237 * @ies: Information elements
2238 * @ies_len: Length of elements
2239 * @cw: The channel width
2240 * Returns: The max transmit power at the channel width, TX_POWER_NO_CONSTRAINT
2241 * if it is not constrained.
2242 *
2243 * This function is only used to estimate the actual signal RSSI when associated
2244 * based on the beacon RSSI at the STA. Beacon frames are transmitted on 20 MHz
2245 * channels, while the Data frames usually use higher channel width. Therefore
2246 * their RSSIs may be different. Assuming there is a fixed gap between the TX
2247 * power limit of the STA defined by the Transmit Power Envelope element and the
2248 * TX power of the AP, the difference in the TX power of X MHz and Y MHz at the
2249 * STA equals to the difference at the AP, and the difference in the signal RSSI
2250 * at the STA. tx_pwr is a floating point number in the standard, but the error
2251 * of casting to int is trivial in comparing two BSSes.
2252 */
wpas_channel_width_tx_pwr(const u8 * ies,size_t ies_len,enum chan_width cw)2253 static int wpas_channel_width_tx_pwr(const u8 *ies, size_t ies_len,
2254 enum chan_width cw)
2255 {
2256 #define MIN(a, b) (a < b ? a : b)
2257 int offset = wpas_channel_width_offset(cw);
2258 const struct element *elem;
2259 int max_tx_power = TX_POWER_NO_CONSTRAINT, tx_pwr = 0;
2260
2261 for_each_element_id(elem, WLAN_EID_TRANSMIT_POWER_ENVELOPE, ies,
2262 ies_len) {
2263 int max_tx_pwr_count;
2264 enum max_tx_pwr_interpretation tx_pwr_intrpn;
2265 enum reg_6g_client_type client_type;
2266
2267 if (elem->datalen < 1)
2268 continue;
2269
2270 /*
2271 * IEEE Std 802.11ax-2021, 9.4.2.161 (Transmit Power Envelope
2272 * element) defines Maximum Transmit Power Count (B0-B2),
2273 * Maximum Transmit Power Interpretation (B3-B5), and Maximum
2274 * Transmit Power Category (B6-B7).
2275 */
2276 max_tx_pwr_count = elem->data[0] & 0x07;
2277 tx_pwr_intrpn = (elem->data[0] >> 3) & 0x07;
2278 client_type = (elem->data[0] >> 6) & 0x03;
2279
2280 if (client_type != REG_DEFAULT_CLIENT)
2281 continue;
2282
2283 if (tx_pwr_intrpn == LOCAL_EIRP ||
2284 tx_pwr_intrpn == REGULATORY_CLIENT_EIRP) {
2285 int offs;
2286
2287 max_tx_pwr_count = MIN(max_tx_pwr_count, 3);
2288 offs = MIN(offset, max_tx_pwr_count) + 1;
2289 if (elem->datalen <= offs)
2290 continue;
2291 tx_pwr = (signed char) elem->data[offs];
2292 /*
2293 * Maximum Transmit Power subfield is encoded as an
2294 * 8-bit 2s complement signed integer in the range -64
2295 * dBm to 63 dBm with a 0.5 dB step. 63.5 dBm means no
2296 * local maximum transmit power constraint.
2297 */
2298 if (tx_pwr == 127)
2299 continue;
2300 tx_pwr /= 2;
2301 max_tx_power = MIN(max_tx_power, tx_pwr);
2302 } else if (tx_pwr_intrpn == LOCAL_EIRP_PSD ||
2303 tx_pwr_intrpn == REGULATORY_CLIENT_EIRP_PSD) {
2304 if (elem->datalen < 2)
2305 continue;
2306
2307 tx_pwr = (signed char) elem->data[1];
2308 /*
2309 * Maximum Transmit PSD subfield is encoded as an 8-bit
2310 * 2s complement signed integer. -128 indicates that the
2311 * corresponding 20 MHz channel cannot be used for
2312 * transmission. +127 indicates that no maximum PSD
2313 * limit is specified for the corresponding 20 MHz
2314 * channel.
2315 */
2316 if (tx_pwr == 127 || tx_pwr == -128)
2317 continue;
2318
2319 /*
2320 * The Maximum Transmit PSD subfield indicates the
2321 * maximum transmit PSD for the 20 MHz channel. Suppose
2322 * the PSD value is X dBm/MHz, the TX power of N MHz is
2323 * X + 10*log10(N) = X + 10*log10(20) + 10*log10(N/20) =
2324 * X + 13 + 3*log2(N/20)
2325 */
2326 tx_pwr = tx_pwr / 2 + 13 + offset * 3;
2327 max_tx_power = MIN(max_tx_power, tx_pwr);
2328 }
2329 }
2330
2331 return max_tx_power;
2332 #undef MIN
2333 }
2334
2335
2336 /**
2337 * Estimate the RSSI bump of channel width |cw| with respect to 20 MHz channel.
2338 * If the TX power has no constraint, it is unable to estimate the RSSI bump.
2339 */
wpas_channel_width_rssi_bump(const u8 * ies,size_t ies_len,enum chan_width cw)2340 int wpas_channel_width_rssi_bump(const u8 *ies, size_t ies_len,
2341 enum chan_width cw)
2342 {
2343 int max_20mhz_tx_pwr = wpas_channel_width_tx_pwr(ies, ies_len,
2344 CHAN_WIDTH_20);
2345 int max_cw_tx_pwr = wpas_channel_width_tx_pwr(ies, ies_len, cw);
2346
2347 return (max_20mhz_tx_pwr == TX_POWER_NO_CONSTRAINT ||
2348 max_cw_tx_pwr == TX_POWER_NO_CONSTRAINT) ?
2349 0 : (max_cw_tx_pwr - max_20mhz_tx_pwr);
2350 }
2351
2352
wpas_adjust_snr_by_chanwidth(const u8 * ies,size_t ies_len,enum chan_width max_cw,int snr)2353 int wpas_adjust_snr_by_chanwidth(const u8 *ies, size_t ies_len,
2354 enum chan_width max_cw, int snr)
2355 {
2356 int rssi_bump = wpas_channel_width_rssi_bump(ies, ies_len, max_cw);
2357 /*
2358 * The noise has uniform power spectral density (PSD) across the
2359 * frequency band, its power is proportional to the channel width.
2360 * Suppose the PSD of noise is X dBm/MHz, the noise power of N MHz is
2361 * X + 10*log10(N), and the noise power bump with respect to 20 MHz is
2362 * 10*log10(N) - 10*log10(20) = 10*log10(N/20) = 3*log2(N/20)
2363 */
2364 int noise_bump = 3 * wpas_channel_width_offset(max_cw);
2365
2366 return snr + rssi_bump - noise_bump;
2367 }
2368
2369
2370 /* Compare function for sorting scan results. Return >0 if @b is considered
2371 * better. */
wpa_scan_result_compar(const void * a,const void * b)2372 static int wpa_scan_result_compar(const void *a, const void *b)
2373 {
2374 #define MIN(a,b) a < b ? a : b
2375 struct wpa_scan_res **_wa = (void *) a;
2376 struct wpa_scan_res **_wb = (void *) b;
2377 struct wpa_scan_res *wa = *_wa;
2378 struct wpa_scan_res *wb = *_wb;
2379 int wpa_a, wpa_b;
2380 int snr_a, snr_b, snr_a_full, snr_b_full;
2381 size_t ies_len;
2382
2383 /* WPA/WPA2 support preferred */
2384 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
2385 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
2386 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
2387 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
2388
2389 if (wpa_b && !wpa_a)
2390 return 1;
2391 if (!wpa_b && wpa_a)
2392 return -1;
2393
2394 /* privacy support preferred */
2395 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
2396 (wb->caps & IEEE80211_CAP_PRIVACY))
2397 return 1;
2398 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
2399 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
2400 return -1;
2401
2402 if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
2403 /*
2404 * The scan result estimates SNR over 20 MHz, while Data frames
2405 * usually use wider channel width. The TX power and noise power
2406 * are both affected by the channel width.
2407 */
2408 ies_len = wa->ie_len ? wa->ie_len : wa->beacon_ie_len;
2409 snr_a_full = wpas_adjust_snr_by_chanwidth((const u8 *) (wa + 1),
2410 ies_len, wa->max_cw,
2411 wa->snr);
2412 snr_a = MIN(snr_a_full, GREAT_SNR);
2413 ies_len = wb->ie_len ? wb->ie_len : wb->beacon_ie_len;
2414 snr_b_full = wpas_adjust_snr_by_chanwidth((const u8 *) (wb + 1),
2415 ies_len, wb->max_cw,
2416 wb->snr);
2417 snr_b = MIN(snr_b_full, GREAT_SNR);
2418 } else {
2419 /* Level is not in dBm, so we can't calculate
2420 * SNR. Just use raw level (units unknown). */
2421 snr_a = snr_a_full = wa->level;
2422 snr_b = snr_b_full = wb->level;
2423 }
2424
2425 /* If SNR is close, decide by max rate or frequency band. For cases
2426 * involving the 6 GHz band, use the throughput estimate irrespective
2427 * of the SNR difference since the LPI/VLP rules may result in
2428 * significant differences in SNR for cases where the estimated
2429 * throughput can be considerably higher with the lower SNR. */
2430 if (snr_a && snr_b && (abs(snr_b - snr_a) < 7 ||
2431 is_6ghz_freq(wa->freq) ||
2432 is_6ghz_freq(wb->freq))) {
2433 if (wa->est_throughput != wb->est_throughput)
2434 return (int) wb->est_throughput -
2435 (int) wa->est_throughput;
2436 }
2437 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
2438 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
2439 if (is_6ghz_freq(wa->freq) ^ is_6ghz_freq(wb->freq))
2440 return is_6ghz_freq(wa->freq) ? -1 : 1;
2441 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
2442 return IS_5GHZ(wa->freq) ? -1 : 1;
2443 }
2444
2445 /* all things being equal, use SNR; if SNRs are
2446 * identical, use quality values since some drivers may only report
2447 * that value and leave the signal level zero */
2448 if (snr_b_full == snr_a_full)
2449 return wb->qual - wa->qual;
2450 return snr_b_full - snr_a_full;
2451 #undef MIN
2452 }
2453
2454
2455 #ifdef CONFIG_WPS
2456 /* Compare function for sorting scan results when searching a WPS AP for
2457 * provisioning. Return >0 if @b is considered better. */
wpa_scan_result_wps_compar(const void * a,const void * b)2458 static int wpa_scan_result_wps_compar(const void *a, const void *b)
2459 {
2460 struct wpa_scan_res **_wa = (void *) a;
2461 struct wpa_scan_res **_wb = (void *) b;
2462 struct wpa_scan_res *wa = *_wa;
2463 struct wpa_scan_res *wb = *_wb;
2464 int uses_wps_a, uses_wps_b;
2465 struct wpabuf *wps_a, *wps_b;
2466 int res;
2467
2468 /* Optimization - check WPS IE existence before allocated memory and
2469 * doing full reassembly. */
2470 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
2471 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
2472 if (uses_wps_a && !uses_wps_b)
2473 return -1;
2474 if (!uses_wps_a && uses_wps_b)
2475 return 1;
2476
2477 if (uses_wps_a && uses_wps_b) {
2478 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
2479 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
2480 res = wps_ap_priority_compar(wps_a, wps_b);
2481 wpabuf_free(wps_a);
2482 wpabuf_free(wps_b);
2483 if (res)
2484 return res;
2485 }
2486
2487 /*
2488 * Do not use current AP security policy as a sorting criteria during
2489 * WPS provisioning step since the AP may get reconfigured at the
2490 * completion of provisioning.
2491 */
2492
2493 /* all things being equal, use signal level; if signal levels are
2494 * identical, use quality values since some drivers may only report
2495 * that value and leave the signal level zero */
2496 if (wb->level == wa->level)
2497 return wb->qual - wa->qual;
2498 return wb->level - wa->level;
2499 }
2500 #endif /* CONFIG_WPS */
2501
2502
dump_scan_res(struct wpa_scan_results * scan_res)2503 static void dump_scan_res(struct wpa_scan_results *scan_res)
2504 {
2505 #ifndef CONFIG_NO_STDOUT_DEBUG
2506 size_t i;
2507
2508 if (scan_res->res == NULL || scan_res->num == 0)
2509 return;
2510
2511 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
2512
2513 for (i = 0; i < scan_res->num; i++) {
2514 struct wpa_scan_res *r = scan_res->res[i];
2515 u8 *pos;
2516 const u8 *ssid_ie, *ssid = NULL;
2517 size_t ssid_len = 0;
2518
2519 ssid_ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
2520 if (ssid_ie) {
2521 ssid = ssid_ie + 2;
2522 ssid_len = ssid_ie[1];
2523 }
2524
2525 if (r->flags & WPA_SCAN_LEVEL_DBM) {
2526 int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
2527
2528 wpa_printf(MSG_EXCESSIVE, MACSTR
2529 " ssid=%s freq=%d qual=%d noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u",
2530 MAC2STR(r->bssid),
2531 wpa_ssid_txt(ssid, ssid_len),
2532 r->freq, r->qual,
2533 r->noise, noise_valid ? "" : "~", r->level,
2534 r->snr, r->snr >= GREAT_SNR ? "*" : "",
2535 r->flags,
2536 r->age, r->est_throughput);
2537 } else {
2538 wpa_printf(MSG_EXCESSIVE, MACSTR
2539 " ssid=%s freq=%d qual=%d noise=%d level=%d flags=0x%x age=%u est=%u",
2540 MAC2STR(r->bssid),
2541 wpa_ssid_txt(ssid, ssid_len),
2542 r->freq, r->qual,
2543 r->noise, r->level, r->flags, r->age,
2544 r->est_throughput);
2545 }
2546 pos = (u8 *) (r + 1);
2547 if (r->ie_len)
2548 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
2549 pos += r->ie_len;
2550 if (r->beacon_ie_len)
2551 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
2552 pos, r->beacon_ie_len);
2553 }
2554 #endif /* CONFIG_NO_STDOUT_DEBUG */
2555 }
2556
2557
2558 /**
2559 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
2560 * @wpa_s: Pointer to wpa_supplicant data
2561 * @bssid: BSSID to check
2562 * Returns: 0 if the BSSID is filtered or 1 if not
2563 *
2564 * This function is used to filter out specific BSSIDs from scan reslts mainly
2565 * for testing purposes (SET bssid_filter ctrl_iface command).
2566 */
wpa_supplicant_filter_bssid_match(struct wpa_supplicant * wpa_s,const u8 * bssid)2567 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
2568 const u8 *bssid)
2569 {
2570 size_t i;
2571
2572 if (wpa_s->bssid_filter == NULL)
2573 return 1;
2574
2575 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
2576 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
2577 ETH_ALEN) == 0)
2578 return 1;
2579 }
2580
2581 return 0;
2582 }
2583
2584
filter_scan_res(struct wpa_supplicant * wpa_s,struct wpa_scan_results * res)2585 void filter_scan_res(struct wpa_supplicant *wpa_s,
2586 struct wpa_scan_results *res)
2587 {
2588 size_t i, j;
2589
2590 if (wpa_s->bssid_filter == NULL)
2591 return;
2592
2593 for (i = 0, j = 0; i < res->num; i++) {
2594 if (wpa_supplicant_filter_bssid_match(wpa_s,
2595 res->res[i]->bssid)) {
2596 res->res[j++] = res->res[i];
2597 } else {
2598 os_free(res->res[i]);
2599 res->res[i] = NULL;
2600 }
2601 }
2602
2603 if (res->num != j) {
2604 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
2605 (int) (res->num - j));
2606 res->num = j;
2607 }
2608 }
2609
2610
scan_snr(struct wpa_scan_res * res)2611 void scan_snr(struct wpa_scan_res *res)
2612 {
2613 if (res->flags & WPA_SCAN_NOISE_INVALID) {
2614 res->noise = is_6ghz_freq(res->freq) ?
2615 DEFAULT_NOISE_FLOOR_6GHZ :
2616 (IS_5GHZ(res->freq) ?
2617 DEFAULT_NOISE_FLOOR_5GHZ : DEFAULT_NOISE_FLOOR_2GHZ);
2618 }
2619
2620 if (res->flags & WPA_SCAN_LEVEL_DBM) {
2621 res->snr = res->level - res->noise;
2622 } else {
2623 /* Level is not in dBm, so we can't calculate
2624 * SNR. Just use raw level (units unknown). */
2625 res->snr = res->level;
2626 }
2627 }
2628
2629
2630 /* Minimum SNR required to achieve a certain bitrate. */
2631 struct minsnr_bitrate_entry {
2632 int minsnr;
2633 unsigned int bitrate; /* in Mbps */
2634 };
2635
2636 /* VHT needs to be enabled in order to achieve MCS8 and MCS9 rates. */
2637 static const int vht_mcs = 8;
2638
2639 static const struct minsnr_bitrate_entry vht20_table[] = {
2640 { 0, 0 },
2641 { 2, 6500 }, /* HT20 MCS0 */
2642 { 5, 13000 }, /* HT20 MCS1 */
2643 { 9, 19500 }, /* HT20 MCS2 */
2644 { 11, 26000 }, /* HT20 MCS3 */
2645 { 15, 39000 }, /* HT20 MCS4 */
2646 { 18, 52000 }, /* HT20 MCS5 */
2647 { 20, 58500 }, /* HT20 MCS6 */
2648 { 25, 65000 }, /* HT20 MCS7 */
2649 { 29, 78000 }, /* VHT20 MCS8 */
2650 { -1, 78000 } /* SNR > 29 */
2651 };
2652
2653 static const struct minsnr_bitrate_entry vht40_table[] = {
2654 { 0, 0 },
2655 { 5, 13500 }, /* HT40 MCS0 */
2656 { 8, 27000 }, /* HT40 MCS1 */
2657 { 12, 40500 }, /* HT40 MCS2 */
2658 { 14, 54000 }, /* HT40 MCS3 */
2659 { 18, 81000 }, /* HT40 MCS4 */
2660 { 21, 108000 }, /* HT40 MCS5 */
2661 { 23, 121500 }, /* HT40 MCS6 */
2662 { 28, 135000 }, /* HT40 MCS7 */
2663 { 32, 162000 }, /* VHT40 MCS8 */
2664 { 34, 180000 }, /* VHT40 MCS9 */
2665 { -1, 180000 } /* SNR > 34 */
2666 };
2667
2668 static const struct minsnr_bitrate_entry vht80_table[] = {
2669 { 0, 0 },
2670 { 8, 29300 }, /* VHT80 MCS0 */
2671 { 11, 58500 }, /* VHT80 MCS1 */
2672 { 15, 87800 }, /* VHT80 MCS2 */
2673 { 17, 117000 }, /* VHT80 MCS3 */
2674 { 21, 175500 }, /* VHT80 MCS4 */
2675 { 24, 234000 }, /* VHT80 MCS5 */
2676 { 26, 263300 }, /* VHT80 MCS6 */
2677 { 31, 292500 }, /* VHT80 MCS7 */
2678 { 35, 351000 }, /* VHT80 MCS8 */
2679 { 37, 390000 }, /* VHT80 MCS9 */
2680 { -1, 390000 } /* SNR > 37 */
2681 };
2682
2683
2684 static const struct minsnr_bitrate_entry vht160_table[] = {
2685 { 0, 0 },
2686 { 11, 58500 }, /* VHT160 MCS0 */
2687 { 14, 117000 }, /* VHT160 MCS1 */
2688 { 18, 175500 }, /* VHT160 MCS2 */
2689 { 20, 234000 }, /* VHT160 MCS3 */
2690 { 24, 351000 }, /* VHT160 MCS4 */
2691 { 27, 468000 }, /* VHT160 MCS5 */
2692 { 29, 526500 }, /* VHT160 MCS6 */
2693 { 34, 585000 }, /* VHT160 MCS7 */
2694 { 38, 702000 }, /* VHT160 MCS8 */
2695 { 40, 780000 }, /* VHT160 MCS9 */
2696 { -1, 780000 } /* SNR > 37 */
2697 };
2698
2699 /* EHT needs to be enabled in order to achieve MCS12 and MCS13 rates. */
2700 #define EHT_MCS 12
2701
2702 static const struct minsnr_bitrate_entry he20_table[] = {
2703 { 0, 0 },
2704 { 2, 8600 }, /* HE20 MCS0 */
2705 { 5, 17200 }, /* HE20 MCS1 */
2706 { 9, 25800 }, /* HE20 MCS2 */
2707 { 11, 34400 }, /* HE20 MCS3 */
2708 { 15, 51600 }, /* HE20 MCS4 */
2709 { 18, 68800 }, /* HE20 MCS5 */
2710 { 20, 77400 }, /* HE20 MCS6 */
2711 { 25, 86000 }, /* HE20 MCS7 */
2712 { 29, 103200 }, /* HE20 MCS8 */
2713 { 31, 114700 }, /* HE20 MCS9 */
2714 { 34, 129000 }, /* HE20 MCS10 */
2715 { 36, 143400 }, /* HE20 MCS11 */
2716 { 39, 154900 }, /* EHT20 MCS12 */
2717 { 42, 172100 }, /* EHT20 MCS13 */
2718 { -1, 172100 } /* SNR > 42 */
2719 };
2720
2721 static const struct minsnr_bitrate_entry he40_table[] = {
2722 { 0, 0 },
2723 { 5, 17200 }, /* HE40 MCS0 */
2724 { 8, 34400 }, /* HE40 MCS1 */
2725 { 12, 51600 }, /* HE40 MCS2 */
2726 { 14, 68800 }, /* HE40 MCS3 */
2727 { 18, 103200 }, /* HE40 MCS4 */
2728 { 21, 137600 }, /* HE40 MCS5 */
2729 { 23, 154900 }, /* HE40 MCS6 */
2730 { 28, 172100 }, /* HE40 MCS7 */
2731 { 32, 206500 }, /* HE40 MCS8 */
2732 { 34, 229400 }, /* HE40 MCS9 */
2733 { 37, 258100 }, /* HE40 MCS10 */
2734 { 39, 286800 }, /* HE40 MCS11 */
2735 { 42, 309500 }, /* EHT40 MCS12 */
2736 { 45, 344100 }, /* EHT40 MCS13 */
2737 { -1, 344100 } /* SNR > 45 */
2738 };
2739
2740 static const struct minsnr_bitrate_entry he80_table[] = {
2741 { 0, 0 },
2742 { 8, 36000 }, /* HE80 MCS0 */
2743 { 11, 72100 }, /* HE80 MCS1 */
2744 { 15, 108100 }, /* HE80 MCS2 */
2745 { 17, 144100 }, /* HE80 MCS3 */
2746 { 21, 216200 }, /* HE80 MCS4 */
2747 { 24, 288200 }, /* HE80 MCS5 */
2748 { 26, 324300 }, /* HE80 MCS6 */
2749 { 31, 360300 }, /* HE80 MCS7 */
2750 { 35, 432400 }, /* HE80 MCS8 */
2751 { 37, 480400 }, /* HE80 MCS9 */
2752 { 40, 540400 }, /* HE80 MCS10 */
2753 { 42, 600500 }, /* HE80 MCS11 */
2754 { 45, 648500 }, /* EHT80 MCS12 */
2755 { 48, 720600 }, /* EHT80 MCS13 */
2756 { -1, 720600 } /* SNR > 48 */
2757 };
2758
2759
2760 static const struct minsnr_bitrate_entry he160_table[] = {
2761 { 0, 0 },
2762 { 11, 72100 }, /* HE160 MCS0 */
2763 { 14, 144100 }, /* HE160 MCS1 */
2764 { 18, 216200 }, /* HE160 MCS2 */
2765 { 20, 288200 }, /* HE160 MCS3 */
2766 { 24, 432400 }, /* HE160 MCS4 */
2767 { 27, 576500 }, /* HE160 MCS5 */
2768 { 29, 648500 }, /* HE160 MCS6 */
2769 { 34, 720600 }, /* HE160 MCS7 */
2770 { 38, 864700 }, /* HE160 MCS8 */
2771 { 40, 960800 }, /* HE160 MCS9 */
2772 { 43, 1080900 }, /* HE160 MCS10 */
2773 { 45, 1201000 }, /* HE160 MCS11 */
2774 { 48, 1297100 }, /* EHT160 MCS12 */
2775 { 51, 1441200 }, /* EHT160 MCS13 */
2776 { -1, 1441200 } /* SNR > 51 */
2777 };
2778
2779 /* See IEEE P802.11be/D2.0, Table 36-86: EHT-MCSs for 4x996-tone RU, NSS,u = 1
2780 */
2781 static const struct minsnr_bitrate_entry eht320_table[] = {
2782 { 0, 0 },
2783 { 14, 144100 }, /* EHT320 MCS0 */
2784 { 17, 288200 }, /* EHT320 MCS1 */
2785 { 21, 432400 }, /* EHT320 MCS2 */
2786 { 23, 576500 }, /* EHT320 MCS3 */
2787 { 27, 864700 }, /* EHT320 MCS4 */
2788 { 30, 1152900 }, /* EHT320 MCS5 */
2789 { 32, 1297100 }, /* EHT320 MCS6 */
2790 { 37, 1441200 }, /* EHT320 MCS7 */
2791 { 41, 1729400 }, /* EHT320 MCS8 */
2792 { 43, 1921500 }, /* EHT320 MCS9 */
2793 { 46, 2161800 }, /* EHT320 MCS10 */
2794 { 48, 2401900 }, /* EHT320 MCS11 */
2795 { 51, 2594100 }, /* EHT320 MCS12 */
2796 { 54, 2882400 }, /* EHT320 MCS13 */
2797 { -1, 2882400 } /* SNR > 54 */
2798 };
2799
interpolate_rate(int snr,int snr0,int snr1,int rate0,int rate1)2800 static unsigned int interpolate_rate(int snr, int snr0, int snr1,
2801 int rate0, int rate1)
2802 {
2803 return rate0 + (snr - snr0) * (rate1 - rate0) / (snr1 - snr0);
2804 }
2805
2806
max_rate(const struct minsnr_bitrate_entry table[],int snr,bool vht)2807 static unsigned int max_rate(const struct minsnr_bitrate_entry table[],
2808 int snr, bool vht)
2809 {
2810 const struct minsnr_bitrate_entry *prev, *entry = table;
2811
2812 while ((entry->minsnr != -1) &&
2813 (snr >= entry->minsnr) &&
2814 (vht || entry - table <= vht_mcs))
2815 entry++;
2816 if (entry == table)
2817 return entry->bitrate;
2818 prev = entry - 1;
2819 if (entry->minsnr == -1 || (!vht && entry - table > vht_mcs))
2820 return prev->bitrate;
2821 return interpolate_rate(snr, prev->minsnr, entry->minsnr, prev->bitrate,
2822 entry->bitrate);
2823 }
2824
2825
max_ht20_rate(int snr,bool vht)2826 static unsigned int max_ht20_rate(int snr, bool vht)
2827 {
2828 return max_rate(vht20_table, snr, vht);
2829 }
2830
2831
max_ht40_rate(int snr,bool vht)2832 static unsigned int max_ht40_rate(int snr, bool vht)
2833 {
2834 return max_rate(vht40_table, snr, vht);
2835 }
2836
2837
max_vht80_rate(int snr)2838 static unsigned int max_vht80_rate(int snr)
2839 {
2840 return max_rate(vht80_table, snr, 1);
2841 }
2842
2843
max_vht160_rate(int snr)2844 static unsigned int max_vht160_rate(int snr)
2845 {
2846 return max_rate(vht160_table, snr, 1);
2847 }
2848
2849
max_he_eht_rate(const struct minsnr_bitrate_entry table[],int snr,bool eht)2850 static unsigned int max_he_eht_rate(const struct minsnr_bitrate_entry table[],
2851 int snr, bool eht)
2852 {
2853 const struct minsnr_bitrate_entry *prev, *entry = table;
2854
2855 while (entry->minsnr != -1 && snr >= entry->minsnr &&
2856 (eht || entry - table <= EHT_MCS))
2857 entry++;
2858 if (entry == table)
2859 return 0;
2860 prev = entry - 1;
2861 if (entry->minsnr == -1 || (!eht && entry - table > EHT_MCS))
2862 return prev->bitrate;
2863 return interpolate_rate(snr, prev->minsnr, entry->minsnr,
2864 prev->bitrate, entry->bitrate);
2865 }
2866
2867
wpas_get_est_tpt(const struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len,int rate,int snr,int freq,enum chan_width * max_cw)2868 unsigned int wpas_get_est_tpt(const struct wpa_supplicant *wpa_s,
2869 const u8 *ies, size_t ies_len, int rate,
2870 int snr, int freq, enum chan_width *max_cw)
2871 {
2872 struct hostapd_hw_modes *hw_mode;
2873 unsigned int est, tmp;
2874 const u8 *ie;
2875 /*
2876 * No need to apply a bump to the noise here because the
2877 * minsnr_bitrate_entry tables are based on MCS tables where this has
2878 * been taken into account.
2879 */
2880 int adjusted_snr;
2881
2882 /* Limit based on estimated SNR */
2883 if (rate > 1 * 2 && snr < 1)
2884 rate = 1 * 2;
2885 else if (rate > 2 * 2 && snr < 4)
2886 rate = 2 * 2;
2887 else if (rate > 6 * 2 && snr < 5)
2888 rate = 6 * 2;
2889 else if (rate > 9 * 2 && snr < 6)
2890 rate = 9 * 2;
2891 else if (rate > 12 * 2 && snr < 7)
2892 rate = 12 * 2;
2893 else if (rate > 12 * 2 && snr < 8)
2894 rate = 14 * 2;
2895 else if (rate > 12 * 2 && snr < 9)
2896 rate = 16 * 2;
2897 else if (rate > 18 * 2 && snr < 10)
2898 rate = 18 * 2;
2899 else if (rate > 24 * 2 && snr < 11)
2900 rate = 24 * 2;
2901 else if (rate > 24 * 2 && snr < 12)
2902 rate = 27 * 2;
2903 else if (rate > 24 * 2 && snr < 13)
2904 rate = 30 * 2;
2905 else if (rate > 24 * 2 && snr < 14)
2906 rate = 33 * 2;
2907 else if (rate > 36 * 2 && snr < 15)
2908 rate = 36 * 2;
2909 else if (rate > 36 * 2 && snr < 16)
2910 rate = 39 * 2;
2911 else if (rate > 36 * 2 && snr < 17)
2912 rate = 42 * 2;
2913 else if (rate > 36 * 2 && snr < 18)
2914 rate = 45 * 2;
2915 else if (rate > 48 * 2 && snr < 19)
2916 rate = 48 * 2;
2917 else if (rate > 48 * 2 && snr < 20)
2918 rate = 51 * 2;
2919 else if (rate > 54 * 2 && snr < 21)
2920 rate = 54 * 2;
2921 est = rate * 500;
2922
2923 hw_mode = get_mode_with_freq(wpa_s->hw.modes, wpa_s->hw.num_modes,
2924 freq);
2925
2926 if (hw_mode && hw_mode->ht_capab) {
2927 ie = get_ie(ies, ies_len, WLAN_EID_HT_CAP);
2928 if (ie) {
2929 *max_cw = CHAN_WIDTH_20;
2930 tmp = max_ht20_rate(snr, false);
2931 if (tmp > est)
2932 est = tmp;
2933 }
2934 }
2935
2936 if (hw_mode &&
2937 (hw_mode->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
2938 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
2939 if (ie && ie[1] >= 2 &&
2940 (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2941 *max_cw = CHAN_WIDTH_40;
2942 adjusted_snr = snr +
2943 wpas_channel_width_rssi_bump(ies, ies_len,
2944 CHAN_WIDTH_40);
2945 tmp = max_ht40_rate(adjusted_snr, false);
2946 if (tmp > est)
2947 est = tmp;
2948 }
2949 }
2950
2951 if (hw_mode && hw_mode->vht_capab) {
2952 /* Use +1 to assume VHT is always faster than HT */
2953 ie = get_ie(ies, ies_len, WLAN_EID_VHT_CAP);
2954 if (ie) {
2955 bool vht80 = false, vht160 = false;
2956
2957 if (*max_cw == CHAN_WIDTH_UNKNOWN)
2958 *max_cw = CHAN_WIDTH_20;
2959 tmp = max_ht20_rate(snr, true) + 1;
2960 if (tmp > est)
2961 est = tmp;
2962
2963 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
2964 if (ie && ie[1] >= 2 &&
2965 (ie[3] &
2966 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) {
2967 *max_cw = CHAN_WIDTH_40;
2968 adjusted_snr = snr +
2969 wpas_channel_width_rssi_bump(
2970 ies, ies_len, CHAN_WIDTH_40);
2971 tmp = max_ht40_rate(adjusted_snr, true) + 1;
2972 if (tmp > est)
2973 est = tmp;
2974 }
2975
2976 /* Determine VHT BSS bandwidth based on IEEE Std
2977 * 802.11-2020, Table 11-23 (VHT BSs bandwidth) */
2978 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
2979 if (ie && ie[1] >= 3) {
2980 u8 cw = ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK;
2981 u8 seg0 = ie[3];
2982 u8 seg1 = ie[4];
2983
2984 if (cw)
2985 vht80 = true;
2986 if (cw == 2 ||
2987 (cw == 3 &&
2988 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2989 vht160 = true;
2990 if (cw == 1 &&
2991 ((seg1 > 0 && abs(seg1 - seg0) == 8) ||
2992 (seg1 > 0 && abs(seg1 - seg0) == 16)))
2993 vht160 = true;
2994 }
2995
2996 if (vht80) {
2997 *max_cw = CHAN_WIDTH_80;
2998 adjusted_snr = snr +
2999 wpas_channel_width_rssi_bump(
3000 ies, ies_len, CHAN_WIDTH_80);
3001 tmp = max_vht80_rate(adjusted_snr) + 1;
3002 if (tmp > est)
3003 est = tmp;
3004 }
3005
3006 if (vht160 &&
3007 (hw_mode->vht_capab &
3008 (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3009 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
3010 *max_cw = CHAN_WIDTH_160;
3011 adjusted_snr = snr +
3012 wpas_channel_width_rssi_bump(
3013 ies, ies_len, CHAN_WIDTH_160);
3014 tmp = max_vht160_rate(adjusted_snr) + 1;
3015 if (tmp > est)
3016 est = tmp;
3017 }
3018 }
3019 }
3020
3021 if (hw_mode && hw_mode->he_capab[IEEE80211_MODE_INFRA].he_supported) {
3022 /* Use +2 to assume HE is always faster than HT/VHT */
3023 struct ieee80211_he_capabilities *he;
3024 struct ieee80211_eht_capabilities *eht;
3025 struct he_capabilities *own_he;
3026 u8 cw, boost = 2;
3027 const u8 *eht_ie;
3028 bool is_eht = false;
3029
3030 ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_HE_CAPABILITIES);
3031 if (!ie || (ie[1] < 1 + IEEE80211_HE_CAPAB_MIN_LEN))
3032 return est;
3033 he = (struct ieee80211_he_capabilities *) &ie[3];
3034 own_he = &hw_mode->he_capab[IEEE80211_MODE_INFRA];
3035
3036 /* Use +3 to assume EHT is always faster than HE */
3037 if (hw_mode->eht_capab[IEEE80211_MODE_INFRA].eht_supported) {
3038 eht_ie = get_ie_ext(ies, ies_len,
3039 WLAN_EID_EXT_EHT_CAPABILITIES);
3040 if (eht_ie &&
3041 (eht_ie[1] >= 1 + IEEE80211_EHT_CAPAB_MIN_LEN)) {
3042 is_eht = true;
3043 boost = 3;
3044 }
3045 }
3046
3047 if (*max_cw == CHAN_WIDTH_UNKNOWN)
3048 *max_cw = CHAN_WIDTH_20;
3049 tmp = max_he_eht_rate(he20_table, snr, is_eht) + boost;
3050 if (tmp > est)
3051 est = tmp;
3052
3053 cw = he->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
3054 own_he->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
3055 if (cw &
3056 (IS_2P4GHZ(freq) ? HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G :
3057 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
3058 if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3059 *max_cw < CHAN_WIDTH_40)
3060 *max_cw = CHAN_WIDTH_40;
3061 adjusted_snr = snr + wpas_channel_width_rssi_bump(
3062 ies, ies_len, CHAN_WIDTH_40);
3063 tmp = max_he_eht_rate(he40_table, adjusted_snr,
3064 is_eht) + boost;
3065 if (tmp > est)
3066 est = tmp;
3067 }
3068
3069 if (!IS_2P4GHZ(freq) &&
3070 (cw & HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
3071 if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3072 *max_cw < CHAN_WIDTH_80)
3073 *max_cw = CHAN_WIDTH_80;
3074 adjusted_snr = snr + wpas_channel_width_rssi_bump(
3075 ies, ies_len, CHAN_WIDTH_80);
3076 tmp = max_he_eht_rate(he80_table, adjusted_snr,
3077 is_eht) + boost;
3078 if (tmp > est)
3079 est = tmp;
3080 }
3081
3082 if (!IS_2P4GHZ(freq) &&
3083 (cw & (HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3084 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G))) {
3085 if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3086 *max_cw < CHAN_WIDTH_160)
3087 *max_cw = CHAN_WIDTH_160;
3088 adjusted_snr = snr + wpas_channel_width_rssi_bump(
3089 ies, ies_len, CHAN_WIDTH_160);
3090 tmp = max_he_eht_rate(he160_table, adjusted_snr,
3091 is_eht) + boost;
3092 if (tmp > est)
3093 est = tmp;
3094 }
3095
3096 if (!is_eht)
3097 return est;
3098
3099 eht = (struct ieee80211_eht_capabilities *) &eht_ie[3];
3100
3101 if (is_6ghz_freq(freq) &&
3102 (eht->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
3103 EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
3104 if (*max_cw == CHAN_WIDTH_UNKNOWN ||
3105 *max_cw < CHAN_WIDTH_320)
3106 *max_cw = CHAN_WIDTH_320;
3107 adjusted_snr = snr + wpas_channel_width_rssi_bump(
3108 ies, ies_len, CHAN_WIDTH_320);
3109 tmp = max_he_eht_rate(eht320_table, adjusted_snr, true);
3110 if (tmp > est)
3111 est = tmp;
3112 }
3113 }
3114
3115 return est;
3116 }
3117
3118
scan_est_throughput(struct wpa_supplicant * wpa_s,struct wpa_scan_res * res)3119 void scan_est_throughput(struct wpa_supplicant *wpa_s,
3120 struct wpa_scan_res *res)
3121 {
3122 int rate; /* max legacy rate in 500 kb/s units */
3123 int snr = res->snr;
3124 const u8 *ies = (const void *) (res + 1);
3125 size_t ie_len = res->ie_len;
3126
3127 if (res->est_throughput)
3128 return;
3129
3130 /* Get maximum legacy rate */
3131 rate = wpa_scan_get_max_rate(res);
3132
3133 if (!ie_len)
3134 ie_len = res->beacon_ie_len;
3135 res->est_throughput = wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr,
3136 res->freq, &res->max_cw);
3137
3138 /* TODO: channel utilization and AP load (e.g., from AP Beacon) */
3139 }
3140
3141
3142 /**
3143 * wpa_supplicant_get_scan_results - Get scan results
3144 * @wpa_s: Pointer to wpa_supplicant data
3145 * @info: Information about what was scanned or %NULL if not available
3146 * @new_scan: Whether a new scan was performed
3147 * Returns: Scan results, %NULL on failure
3148 *
3149 * This function request the current scan results from the driver and updates
3150 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
3151 * results with wpa_scan_results_free().
3152 */
3153 struct wpa_scan_results *
wpa_supplicant_get_scan_results(struct wpa_supplicant * wpa_s,struct scan_info * info,int new_scan)3154 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
3155 struct scan_info *info, int new_scan)
3156 {
3157 struct wpa_scan_results *scan_res;
3158 size_t i;
3159 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
3160
3161 scan_res = wpa_drv_get_scan_results2(wpa_s);
3162 if (scan_res == NULL) {
3163 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
3164 return NULL;
3165 }
3166 if (scan_res->fetch_time.sec == 0) {
3167 /*
3168 * Make sure we have a valid timestamp if the driver wrapper
3169 * does not set this.
3170 */
3171 os_get_reltime(&scan_res->fetch_time);
3172 }
3173 filter_scan_res(wpa_s, scan_res);
3174
3175 for (i = 0; i < scan_res->num; i++) {
3176 struct wpa_scan_res *scan_res_item = scan_res->res[i];
3177
3178 scan_snr(scan_res_item);
3179 scan_est_throughput(wpa_s, scan_res_item);
3180 }
3181
3182 #ifdef CONFIG_WPS
3183 if (wpas_wps_searching(wpa_s)) {
3184 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
3185 "provisioning rules");
3186 compar = wpa_scan_result_wps_compar;
3187 }
3188 #endif /* CONFIG_WPS */
3189
3190 if (scan_res->res) {
3191 qsort(scan_res->res, scan_res->num,
3192 sizeof(struct wpa_scan_res *), compar);
3193 }
3194 dump_scan_res(scan_res);
3195
3196 if (wpa_s->ignore_post_flush_scan_res) {
3197 /* FLUSH command aborted an ongoing scan and these are the
3198 * results from the aborted scan. Do not process the results to
3199 * maintain flushed state. */
3200 wpa_dbg(wpa_s, MSG_DEBUG,
3201 "Do not update BSS table based on pending post-FLUSH scan results");
3202 wpa_s->ignore_post_flush_scan_res = 0;
3203 return scan_res;
3204 }
3205
3206 wpa_bss_update_start(wpa_s);
3207 for (i = 0; i < scan_res->num; i++)
3208 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
3209 &scan_res->fetch_time);
3210 wpa_bss_update_end(wpa_s, info, new_scan);
3211
3212 return scan_res;
3213 }
3214
3215
3216 /**
3217 * wpa_supplicant_update_scan_results - Update scan results from the driver
3218 * @wpa_s: Pointer to wpa_supplicant data
3219 * Returns: 0 on success, -1 on failure
3220 *
3221 * This function updates the BSS table within wpa_supplicant based on the
3222 * currently available scan results from the driver without requesting a new
3223 * scan. This is used in cases where the driver indicates an association
3224 * (including roaming within ESS) and wpa_supplicant does not yet have the
3225 * needed information to complete the connection (e.g., to perform validation
3226 * steps in 4-way handshake).
3227 */
wpa_supplicant_update_scan_results(struct wpa_supplicant * wpa_s)3228 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
3229 {
3230 struct wpa_scan_results *scan_res;
3231 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
3232 if (scan_res == NULL)
3233 return -1;
3234 wpa_scan_results_free(scan_res);
3235
3236 return 0;
3237 }
3238
3239
3240 /**
3241 * scan_only_handler - Reports scan results
3242 */
scan_only_handler(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)3243 void scan_only_handler(struct wpa_supplicant *wpa_s,
3244 struct wpa_scan_results *scan_res)
3245 {
3246 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
3247 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3248 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
3249 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
3250 wpa_s->manual_scan_id);
3251 wpa_s->manual_scan_use_id = 0;
3252 } else {
3253 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
3254 }
3255 wpas_notify_scan_results(wpa_s);
3256 wpas_notify_scan_done(wpa_s, 1);
3257 if (wpa_s->scan_work) {
3258 struct wpa_radio_work *work = wpa_s->scan_work;
3259 wpa_s->scan_work = NULL;
3260 radio_work_done(work);
3261 }
3262
3263 if (wpa_s->wpa_state == WPA_SCANNING)
3264 wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state);
3265 }
3266
3267
wpas_scan_scheduled(struct wpa_supplicant * wpa_s)3268 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
3269 {
3270 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
3271 }
3272
3273
3274 struct wpa_driver_scan_params *
wpa_scan_clone_params(const struct wpa_driver_scan_params * src)3275 wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
3276 {
3277 struct wpa_driver_scan_params *params;
3278 size_t i;
3279 u8 *n;
3280
3281 params = os_zalloc(sizeof(*params));
3282 if (params == NULL)
3283 return NULL;
3284
3285 for (i = 0; i < src->num_ssids; i++) {
3286 if (src->ssids[i].ssid) {
3287 n = os_memdup(src->ssids[i].ssid,
3288 src->ssids[i].ssid_len);
3289 if (n == NULL)
3290 goto failed;
3291 params->ssids[i].ssid = n;
3292 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
3293 }
3294 }
3295 params->num_ssids = src->num_ssids;
3296
3297 if (src->extra_ies) {
3298 n = os_memdup(src->extra_ies, src->extra_ies_len);
3299 if (n == NULL)
3300 goto failed;
3301 params->extra_ies = n;
3302 params->extra_ies_len = src->extra_ies_len;
3303 }
3304
3305 if (src->freqs) {
3306 int len = int_array_len(src->freqs);
3307 params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int));
3308 if (params->freqs == NULL)
3309 goto failed;
3310 }
3311
3312 if (src->filter_ssids) {
3313 params->filter_ssids = os_memdup(src->filter_ssids,
3314 sizeof(*params->filter_ssids) *
3315 src->num_filter_ssids);
3316 if (params->filter_ssids == NULL)
3317 goto failed;
3318 params->num_filter_ssids = src->num_filter_ssids;
3319 }
3320
3321 params->filter_rssi = src->filter_rssi;
3322 params->p2p_probe = src->p2p_probe;
3323 params->only_new_results = src->only_new_results;
3324 params->low_priority = src->low_priority;
3325 params->duration = src->duration;
3326 params->duration_mandatory = src->duration_mandatory;
3327 params->oce_scan = src->oce_scan;
3328
3329 if (src->sched_scan_plans_num > 0) {
3330 params->sched_scan_plans =
3331 os_memdup(src->sched_scan_plans,
3332 sizeof(*src->sched_scan_plans) *
3333 src->sched_scan_plans_num);
3334 if (!params->sched_scan_plans)
3335 goto failed;
3336
3337 params->sched_scan_plans_num = src->sched_scan_plans_num;
3338 }
3339
3340 if (src->mac_addr_rand &&
3341 wpa_setup_mac_addr_rand_params(params, src->mac_addr))
3342 goto failed;
3343
3344 if (src->bssid) {
3345 u8 *bssid;
3346
3347 bssid = os_memdup(src->bssid, ETH_ALEN);
3348 if (!bssid)
3349 goto failed;
3350 params->bssid = bssid;
3351 }
3352
3353 params->relative_rssi_set = src->relative_rssi_set;
3354 params->relative_rssi = src->relative_rssi;
3355 params->relative_adjust_band = src->relative_adjust_band;
3356 params->relative_adjust_rssi = src->relative_adjust_rssi;
3357 params->p2p_include_6ghz = src->p2p_include_6ghz;
3358 params->non_coloc_6ghz = src->non_coloc_6ghz;
3359 params->min_probe_req_content = src->min_probe_req_content;
3360 return params;
3361
3362 failed:
3363 wpa_scan_free_params(params);
3364 return NULL;
3365 }
3366
3367
wpa_scan_free_params(struct wpa_driver_scan_params * params)3368 void wpa_scan_free_params(struct wpa_driver_scan_params *params)
3369 {
3370 size_t i;
3371
3372 if (params == NULL)
3373 return;
3374
3375 for (i = 0; i < params->num_ssids; i++)
3376 os_free((u8 *) params->ssids[i].ssid);
3377 os_free((u8 *) params->extra_ies);
3378 os_free(params->freqs);
3379 os_free(params->filter_ssids);
3380 os_free(params->sched_scan_plans);
3381
3382 /*
3383 * Note: params->mac_addr_mask points to same memory allocation and
3384 * must not be freed separately.
3385 */
3386 os_free((u8 *) params->mac_addr);
3387
3388 os_free((u8 *) params->bssid);
3389
3390 os_free(params);
3391 }
3392
3393
wpas_start_pno(struct wpa_supplicant * wpa_s)3394 int wpas_start_pno(struct wpa_supplicant *wpa_s)
3395 {
3396 int ret;
3397 size_t prio, i, num_ssid, num_match_ssid;
3398 struct wpa_ssid *ssid;
3399 struct wpa_driver_scan_params params;
3400 struct sched_scan_plan scan_plan;
3401 unsigned int max_sched_scan_ssids;
3402
3403 if (!wpa_s->sched_scan_supported)
3404 return -1;
3405
3406 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
3407 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
3408 else
3409 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
3410 if (max_sched_scan_ssids < 1)
3411 return -1;
3412
3413 if (wpa_s->pno || wpa_s->pno_sched_pending)
3414 return 0;
3415
3416 if ((wpa_s->wpa_state > WPA_SCANNING) &&
3417 (wpa_s->wpa_state < WPA_COMPLETED)) {
3418 wpa_printf(MSG_ERROR, "PNO: In assoc process");
3419 return -EAGAIN;
3420 }
3421
3422 if (wpa_s->wpa_state == WPA_SCANNING) {
3423 wpa_supplicant_cancel_scan(wpa_s);
3424 if (wpa_s->sched_scanning) {
3425 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
3426 "ongoing sched scan");
3427 wpa_supplicant_cancel_sched_scan(wpa_s);
3428 wpa_s->pno_sched_pending = 1;
3429 return 0;
3430 }
3431 }
3432
3433 if (wpa_s->sched_scan_stop_req) {
3434 wpa_printf(MSG_DEBUG,
3435 "Schedule PNO after previous sched scan has stopped");
3436 wpa_s->pno_sched_pending = 1;
3437 return 0;
3438 }
3439
3440 os_memset(¶ms, 0, sizeof(params));
3441
3442 num_ssid = num_match_ssid = 0;
3443 ssid = wpa_s->conf->ssid;
3444 while (ssid) {
3445 if (!wpas_network_disabled(wpa_s, ssid)) {
3446 num_match_ssid++;
3447 if (ssid->scan_ssid)
3448 num_ssid++;
3449 }
3450 ssid = ssid->next;
3451 }
3452
3453 if (num_match_ssid == 0) {
3454 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
3455 return -1;
3456 }
3457
3458 if (num_match_ssid > num_ssid) {
3459 params.num_ssids++; /* wildcard */
3460 num_ssid++;
3461 }
3462
3463 if (num_ssid > max_sched_scan_ssids) {
3464 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
3465 "%u", max_sched_scan_ssids, (unsigned int) num_ssid);
3466 num_ssid = max_sched_scan_ssids;
3467 }
3468
3469 if (num_match_ssid > wpa_s->max_match_sets) {
3470 num_match_ssid = wpa_s->max_match_sets;
3471 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
3472 }
3473 params.filter_ssids = os_calloc(num_match_ssid,
3474 sizeof(struct wpa_driver_scan_filter));
3475 if (params.filter_ssids == NULL)
3476 return -1;
3477
3478 i = 0;
3479 prio = 0;
3480 ssid = wpa_s->conf->pssid[prio];
3481 while (ssid) {
3482 if (!wpas_network_disabled(wpa_s, ssid)) {
3483 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
3484 params.ssids[params.num_ssids].ssid =
3485 ssid->ssid;
3486 params.ssids[params.num_ssids].ssid_len =
3487 ssid->ssid_len;
3488 params.num_ssids++;
3489 }
3490 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
3491 ssid->ssid_len);
3492 params.filter_ssids[i].ssid_len = ssid->ssid_len;
3493 params.num_filter_ssids++;
3494 i++;
3495 if (i == num_match_ssid)
3496 break;
3497 }
3498 if (ssid->pnext)
3499 ssid = ssid->pnext;
3500 else if (prio + 1 == wpa_s->conf->num_prio)
3501 break;
3502 else
3503 ssid = wpa_s->conf->pssid[++prio];
3504 }
3505
3506 if (wpa_s->conf->filter_rssi)
3507 params.filter_rssi = wpa_s->conf->filter_rssi;
3508
3509 if (wpa_s->sched_scan_plans_num) {
3510 params.sched_scan_plans = wpa_s->sched_scan_plans;
3511 params.sched_scan_plans_num = wpa_s->sched_scan_plans_num;
3512 } else {
3513 /* Set one scan plan that will run infinitely */
3514 if (wpa_s->conf->sched_scan_interval)
3515 scan_plan.interval = wpa_s->conf->sched_scan_interval;
3516 else
3517 scan_plan.interval = 10;
3518
3519 scan_plan.iterations = 0;
3520 params.sched_scan_plans = &scan_plan;
3521 params.sched_scan_plans_num = 1;
3522 }
3523
3524 params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay;
3525
3526 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
3527 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
3528 params.freqs = wpa_s->manual_sched_scan_freqs;
3529 }
3530
3531 if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) &&
3532 wpa_s->wpa_state <= WPA_SCANNING)
3533 wpa_setup_mac_addr_rand_params(¶ms, wpa_s->mac_addr_pno);
3534
3535 wpa_scan_set_relative_rssi_params(wpa_s, ¶ms);
3536
3537 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms);
3538 os_free(params.filter_ssids);
3539 os_free(params.mac_addr);
3540 if (ret == 0)
3541 wpa_s->pno = 1;
3542 else
3543 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3544 return ret;
3545 }
3546
3547
wpas_stop_pno(struct wpa_supplicant * wpa_s)3548 int wpas_stop_pno(struct wpa_supplicant *wpa_s)
3549 {
3550 int ret = 0;
3551
3552 if (!wpa_s->pno)
3553 return 0;
3554
3555 ret = wpa_supplicant_stop_sched_scan(wpa_s);
3556 wpa_s->sched_scan_stop_req = 1;
3557
3558 wpa_s->pno = 0;
3559 wpa_s->pno_sched_pending = 0;
3560
3561 if (wpa_s->wpa_state == WPA_SCANNING)
3562 wpa_supplicant_req_scan(wpa_s, 0, 0);
3563
3564 return ret;
3565 }
3566
3567
wpas_mac_addr_rand_scan_clear(struct wpa_supplicant * wpa_s,unsigned int type)3568 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
3569 unsigned int type)
3570 {
3571 type &= MAC_ADDR_RAND_ALL;
3572 wpa_s->mac_addr_rand_enable &= ~type;
3573
3574 if (type & MAC_ADDR_RAND_SCAN) {
3575 os_free(wpa_s->mac_addr_scan);
3576 wpa_s->mac_addr_scan = NULL;
3577 }
3578
3579 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
3580 os_free(wpa_s->mac_addr_sched_scan);
3581 wpa_s->mac_addr_sched_scan = NULL;
3582 }
3583
3584 if (type & MAC_ADDR_RAND_PNO) {
3585 os_free(wpa_s->mac_addr_pno);
3586 wpa_s->mac_addr_pno = NULL;
3587 }
3588 }
3589
3590
wpas_mac_addr_rand_scan_set(struct wpa_supplicant * wpa_s,unsigned int type,const u8 * addr,const u8 * mask)3591 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
3592 unsigned int type, const u8 *addr,
3593 const u8 *mask)
3594 {
3595 u8 *tmp = NULL;
3596
3597 if ((wpa_s->mac_addr_rand_supported & type) != type ) {
3598 wpa_printf(MSG_INFO,
3599 "scan: MAC randomization type %u != supported=%u",
3600 type, wpa_s->mac_addr_rand_supported);
3601 return -1;
3602 }
3603
3604 wpas_mac_addr_rand_scan_clear(wpa_s, type);
3605
3606 if (addr) {
3607 tmp = os_malloc(2 * ETH_ALEN);
3608 if (!tmp)
3609 return -1;
3610 os_memcpy(tmp, addr, ETH_ALEN);
3611 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
3612 }
3613
3614 if (type == MAC_ADDR_RAND_SCAN) {
3615 wpa_s->mac_addr_scan = tmp;
3616 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3617 wpa_s->mac_addr_sched_scan = tmp;
3618 } else if (type == MAC_ADDR_RAND_PNO) {
3619 wpa_s->mac_addr_pno = tmp;
3620 } else {
3621 wpa_printf(MSG_INFO,
3622 "scan: Invalid MAC randomization type=0x%x",
3623 type);
3624 os_free(tmp);
3625 return -1;
3626 }
3627
3628 wpa_s->mac_addr_rand_enable |= type;
3629 return 0;
3630 }
3631
3632
wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant * wpa_s,unsigned int type,u8 * mask)3633 int wpas_mac_addr_rand_scan_get_mask(struct wpa_supplicant *wpa_s,
3634 unsigned int type, u8 *mask)
3635 {
3636 const u8 *to_copy;
3637
3638 if ((wpa_s->mac_addr_rand_enable & type) != type)
3639 return -1;
3640
3641 if (type == MAC_ADDR_RAND_SCAN) {
3642 to_copy = wpa_s->mac_addr_scan;
3643 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
3644 to_copy = wpa_s->mac_addr_sched_scan;
3645 } else if (type == MAC_ADDR_RAND_PNO) {
3646 to_copy = wpa_s->mac_addr_pno;
3647 } else {
3648 wpa_printf(MSG_DEBUG,
3649 "scan: Invalid MAC randomization type=0x%x",
3650 type);
3651 return -1;
3652 }
3653
3654 os_memcpy(mask, to_copy + ETH_ALEN, ETH_ALEN);
3655 return 0;
3656 }
3657
3658
wpas_abort_ongoing_scan(struct wpa_supplicant * wpa_s)3659 int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s)
3660 {
3661 struct wpa_radio_work *work;
3662 struct wpa_radio *radio = wpa_s->radio;
3663
3664 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3665 if (work->wpa_s != wpa_s || !work->started ||
3666 (os_strcmp(work->type, "scan") != 0 &&
3667 os_strcmp(work->type, "p2p-scan") != 0))
3668 continue;
3669 wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan");
3670 return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie);
3671 }
3672
3673 wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort");
3674 return -1;
3675 }
3676
3677
wpas_sched_scan_plans_set(struct wpa_supplicant * wpa_s,const char * cmd)3678 int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd)
3679 {
3680 struct sched_scan_plan *scan_plans = NULL;
3681 const char *token, *context = NULL;
3682 unsigned int num = 0;
3683
3684 if (!cmd)
3685 return -1;
3686
3687 if (!cmd[0]) {
3688 wpa_printf(MSG_DEBUG, "Clear sched scan plans");
3689 os_free(wpa_s->sched_scan_plans);
3690 wpa_s->sched_scan_plans = NULL;
3691 wpa_s->sched_scan_plans_num = 0;
3692 return 0;
3693 }
3694
3695 while ((token = cstr_token(cmd, " ", &context))) {
3696 int ret;
3697 struct sched_scan_plan *scan_plan, *n;
3698
3699 n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans));
3700 if (!n)
3701 goto fail;
3702
3703 scan_plans = n;
3704 scan_plan = &scan_plans[num];
3705 num++;
3706
3707 ret = sscanf(token, "%u:%u", &scan_plan->interval,
3708 &scan_plan->iterations);
3709 if (ret <= 0 || ret > 2 || !scan_plan->interval) {
3710 wpa_printf(MSG_ERROR,
3711 "Invalid sched scan plan input: %s", token);
3712 goto fail;
3713 }
3714
3715 if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) {
3716 wpa_printf(MSG_WARNING,
3717 "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)",
3718 num, scan_plan->interval,
3719 wpa_s->max_sched_scan_plan_interval);
3720 scan_plan->interval =
3721 wpa_s->max_sched_scan_plan_interval;
3722 }
3723
3724 if (ret == 1) {
3725 scan_plan->iterations = 0;
3726 break;
3727 }
3728
3729 if (!scan_plan->iterations) {
3730 wpa_printf(MSG_ERROR,
3731 "scan plan %u: Number of iterations cannot be zero",
3732 num);
3733 goto fail;
3734 }
3735
3736 if (scan_plan->iterations >
3737 wpa_s->max_sched_scan_plan_iterations) {
3738 wpa_printf(MSG_WARNING,
3739 "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)",
3740 num, scan_plan->iterations,
3741 wpa_s->max_sched_scan_plan_iterations);
3742 scan_plan->iterations =
3743 wpa_s->max_sched_scan_plan_iterations;
3744 }
3745
3746 wpa_printf(MSG_DEBUG,
3747 "scan plan %u: interval=%u iterations=%u",
3748 num, scan_plan->interval, scan_plan->iterations);
3749 }
3750
3751 if (!scan_plans) {
3752 wpa_printf(MSG_ERROR, "Invalid scan plans entry");
3753 goto fail;
3754 }
3755
3756 if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) {
3757 wpa_printf(MSG_ERROR,
3758 "All scan plans but the last must specify a number of iterations");
3759 goto fail;
3760 }
3761
3762 wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u",
3763 num, scan_plans[num - 1].interval);
3764
3765 if (num > wpa_s->max_sched_scan_plans) {
3766 wpa_printf(MSG_WARNING,
3767 "Too many scheduled scan plans (only %u supported)",
3768 wpa_s->max_sched_scan_plans);
3769 wpa_printf(MSG_WARNING,
3770 "Use only the first %u scan plans, and the last one (in infinite loop)",
3771 wpa_s->max_sched_scan_plans - 1);
3772 os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1],
3773 &scan_plans[num - 1], sizeof(*scan_plans));
3774 num = wpa_s->max_sched_scan_plans;
3775 }
3776
3777 os_free(wpa_s->sched_scan_plans);
3778 wpa_s->sched_scan_plans = scan_plans;
3779 wpa_s->sched_scan_plans_num = num;
3780
3781 return 0;
3782
3783 fail:
3784 os_free(scan_plans);
3785 wpa_printf(MSG_ERROR, "invalid scan plans list");
3786 return -1;
3787 }
3788
3789
3790 /**
3791 * wpas_scan_reset_sched_scan - Reset sched_scan state
3792 * @wpa_s: Pointer to wpa_supplicant data
3793 *
3794 * This function is used to cancel a running scheduled scan and to reset an
3795 * internal scan state to continue with a regular scan on the following
3796 * wpa_supplicant_req_scan() calls.
3797 */
wpas_scan_reset_sched_scan(struct wpa_supplicant * wpa_s)3798 void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s)
3799 {
3800 wpa_s->normal_scans = 0;
3801 if (wpa_s->sched_scanning) {
3802 wpa_s->sched_scan_timed_out = 0;
3803 wpa_s->prev_sched_ssid = NULL;
3804 wpa_supplicant_cancel_sched_scan(wpa_s);
3805 }
3806 }
3807
3808
wpas_scan_restart_sched_scan(struct wpa_supplicant * wpa_s)3809 void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s)
3810 {
3811 /* simulate timeout to restart the sched scan */
3812 wpa_s->sched_scan_timed_out = 1;
3813 wpa_s->prev_sched_ssid = NULL;
3814 wpa_supplicant_cancel_sched_scan(wpa_s);
3815 }
3816