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