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