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