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