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