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