]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/scan.c
Interworking: Avoid busy loop in scan result mismatch corner cases
[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 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 radio_work_done(work);
166 return;
167 }
168
169 wpa_supplicant_notify_scanning(wpa_s, 1);
170
171 if (wpa_s->clear_driver_scan_cache)
172 params->only_new_results = 1;
173 ret = wpa_drv_scan(wpa_s, params);
174 wpa_scan_free_params(params);
175 work->ctx = NULL;
176 if (ret) {
177 wpa_supplicant_notify_scanning(wpa_s, 0);
178 wpas_notify_scan_done(wpa_s, 0);
179 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d",
180 ret);
181 radio_work_done(work);
182 return;
183 }
184
185 os_get_reltime(&wpa_s->scan_trigger_time);
186 wpa_s->scan_runs++;
187 wpa_s->normal_scans++;
188 wpa_s->own_scan_requested = 1;
189 wpa_s->clear_driver_scan_cache = 0;
190 wpa_s->scan_work = work;
191 }
192
193
194 /**
195 * wpa_supplicant_trigger_scan - Request driver to start a scan
196 * @wpa_s: Pointer to wpa_supplicant data
197 * @params: Scan parameters
198 * Returns: 0 on success, -1 on failure
199 */
200 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
201 struct wpa_driver_scan_params *params)
202 {
203 struct wpa_driver_scan_params *ctx;
204
205 if (wpa_s->scan_work) {
206 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
207 return -1;
208 }
209
210 ctx = wpa_scan_clone_params(params);
211 if (ctx == NULL)
212 return -1;
213
214 if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
215 {
216 wpa_scan_free_params(ctx);
217 return -1;
218 }
219
220 return 0;
221 }
222
223
224 static void
225 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
226 {
227 struct wpa_supplicant *wpa_s = eloop_ctx;
228
229 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
230
231 if (wpa_supplicant_req_sched_scan(wpa_s))
232 wpa_supplicant_req_scan(wpa_s, 0, 0);
233 }
234
235
236 static void
237 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
238 {
239 struct wpa_supplicant *wpa_s = eloop_ctx;
240
241 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
242
243 wpa_s->sched_scan_timed_out = 1;
244 wpa_supplicant_cancel_sched_scan(wpa_s);
245 }
246
247
248 int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
249 struct wpa_driver_scan_params *params,
250 int interval)
251 {
252 int ret;
253
254 wpa_supplicant_notify_scanning(wpa_s, 1);
255 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
256 if (ret)
257 wpa_supplicant_notify_scanning(wpa_s, 0);
258 else
259 wpa_s->sched_scanning = 1;
260
261 return ret;
262 }
263
264
265 int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
266 {
267 int ret;
268
269 ret = wpa_drv_stop_sched_scan(wpa_s);
270 if (ret) {
271 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
272 /* TODO: what to do if stopping fails? */
273 return -1;
274 }
275
276 return ret;
277 }
278
279
280 static struct wpa_driver_scan_filter *
281 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
282 {
283 struct wpa_driver_scan_filter *ssids;
284 struct wpa_ssid *ssid;
285 size_t count;
286
287 *num_ssids = 0;
288 if (!conf->filter_ssids)
289 return NULL;
290
291 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
292 if (ssid->ssid && ssid->ssid_len)
293 count++;
294 }
295 if (count == 0)
296 return NULL;
297 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
298 if (ssids == NULL)
299 return NULL;
300
301 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
302 if (!ssid->ssid || !ssid->ssid_len)
303 continue;
304 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
305 ssids[*num_ssids].ssid_len = ssid->ssid_len;
306 (*num_ssids)++;
307 }
308
309 return ssids;
310 }
311
312
313 static void wpa_supplicant_optimize_freqs(
314 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
315 {
316 #ifdef CONFIG_P2P
317 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
318 wpa_s->go_params) {
319 /* Optimize provisioning state scan based on GO information */
320 if (wpa_s->p2p_in_provisioning < 5 &&
321 wpa_s->go_params->freq > 0) {
322 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
323 "preferred frequency %d MHz",
324 wpa_s->go_params->freq);
325 params->freqs = os_calloc(2, sizeof(int));
326 if (params->freqs)
327 params->freqs[0] = wpa_s->go_params->freq;
328 } else if (wpa_s->p2p_in_provisioning < 8 &&
329 wpa_s->go_params->freq_list[0]) {
330 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
331 "channels");
332 int_array_concat(&params->freqs,
333 wpa_s->go_params->freq_list);
334 if (params->freqs)
335 int_array_sort_unique(params->freqs);
336 }
337 wpa_s->p2p_in_provisioning++;
338 }
339
340 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
341 /*
342 * Optimize scan based on GO information during persistent
343 * group reinvocation
344 */
345 if (wpa_s->p2p_in_invitation < 5 &&
346 wpa_s->p2p_invite_go_freq > 0) {
347 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
348 wpa_s->p2p_invite_go_freq);
349 params->freqs = os_calloc(2, sizeof(int));
350 if (params->freqs)
351 params->freqs[0] = wpa_s->p2p_invite_go_freq;
352 }
353 wpa_s->p2p_in_invitation++;
354 if (wpa_s->p2p_in_invitation > 20) {
355 /*
356 * This should not really happen since the variable is
357 * cleared on group removal, but if it does happen, make
358 * sure we do not get stuck in special invitation scan
359 * mode.
360 */
361 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
362 wpa_s->p2p_in_invitation = 0;
363 }
364 }
365 #endif /* CONFIG_P2P */
366
367 #ifdef CONFIG_WPS
368 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
369 /*
370 * Optimize post-provisioning scan based on channel used
371 * during provisioning.
372 */
373 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
374 "that was used during provisioning", wpa_s->wps_freq);
375 params->freqs = os_calloc(2, sizeof(int));
376 if (params->freqs)
377 params->freqs[0] = wpa_s->wps_freq;
378 wpa_s->after_wps--;
379 } else if (wpa_s->after_wps)
380 wpa_s->after_wps--;
381
382 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
383 {
384 /* Optimize provisioning scan based on already known channel */
385 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
386 wpa_s->wps_freq);
387 params->freqs = os_calloc(2, sizeof(int));
388 if (params->freqs)
389 params->freqs[0] = wpa_s->wps_freq;
390 wpa_s->known_wps_freq = 0; /* only do this once */
391 }
392 #endif /* CONFIG_WPS */
393 }
394
395
396 #ifdef CONFIG_INTERWORKING
397 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
398 struct wpabuf *buf)
399 {
400 if (wpa_s->conf->interworking == 0)
401 return;
402
403 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
404 wpabuf_put_u8(buf, 6);
405 wpabuf_put_u8(buf, 0x00);
406 wpabuf_put_u8(buf, 0x00);
407 wpabuf_put_u8(buf, 0x00);
408 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
409 wpabuf_put_u8(buf, 0x00);
410 #ifdef CONFIG_HS20
411 wpabuf_put_u8(buf, 0x40); /* Bit 46 - WNM-Notification */
412 #else /* CONFIG_HS20 */
413 wpabuf_put_u8(buf, 0x00);
414 #endif /* CONFIG_HS20 */
415
416 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
417 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
418 1 + ETH_ALEN);
419 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
420 /* No Venue Info */
421 if (!is_zero_ether_addr(wpa_s->conf->hessid))
422 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
423 }
424 #endif /* CONFIG_INTERWORKING */
425
426
427 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
428 {
429 struct wpabuf *extra_ie = NULL;
430 #ifdef CONFIG_WPS
431 int wps = 0;
432 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
433 #endif /* CONFIG_WPS */
434
435 #ifdef CONFIG_INTERWORKING
436 if (wpa_s->conf->interworking &&
437 wpabuf_resize(&extra_ie, 100) == 0)
438 wpas_add_interworking_elements(wpa_s, extra_ie);
439 #endif /* CONFIG_INTERWORKING */
440
441 #ifdef CONFIG_WPS
442 wps = wpas_wps_in_use(wpa_s, &req_type);
443
444 if (wps) {
445 struct wpabuf *wps_ie;
446 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
447 DEV_PW_DEFAULT,
448 &wpa_s->wps->dev,
449 wpa_s->wps->uuid, req_type,
450 0, NULL);
451 if (wps_ie) {
452 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
453 wpabuf_put_buf(extra_ie, wps_ie);
454 wpabuf_free(wps_ie);
455 }
456 }
457
458 #ifdef CONFIG_P2P
459 if (wps) {
460 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
461 if (wpabuf_resize(&extra_ie, ielen) == 0)
462 wpas_p2p_scan_ie(wpa_s, extra_ie);
463 }
464 #endif /* CONFIG_P2P */
465
466 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
467
468 #endif /* CONFIG_WPS */
469
470 #ifdef CONFIG_HS20
471 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
472 wpas_hs20_add_indication(extra_ie, -1);
473 #endif /* CONFIG_HS20 */
474
475 return extra_ie;
476 }
477
478
479 #ifdef CONFIG_P2P
480
481 /*
482 * Check whether there are any enabled networks or credentials that could be
483 * used for a non-P2P connection.
484 */
485 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
486 {
487 struct wpa_ssid *ssid;
488
489 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
490 if (wpas_network_disabled(wpa_s, ssid))
491 continue;
492 if (!ssid->p2p_group)
493 return 1;
494 }
495
496 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
497 wpa_s->conf->auto_interworking)
498 return 1;
499
500 return 0;
501 }
502
503 #endif /* CONFIG_P2P */
504
505
506 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
507 u16 num_modes,
508 enum hostapd_hw_mode mode)
509 {
510 u16 i;
511
512 for (i = 0; i < num_modes; i++) {
513 if (modes[i].mode == mode)
514 return &modes[i];
515 }
516
517 return NULL;
518 }
519
520
521 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
522 enum hostapd_hw_mode band,
523 struct wpa_driver_scan_params *params)
524 {
525 /* Include only supported channels for the specified band */
526 struct hostapd_hw_modes *mode;
527 int count, i;
528
529 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
530 if (mode == NULL) {
531 /* No channels supported in this band - use empty list */
532 params->freqs = os_zalloc(sizeof(int));
533 return;
534 }
535
536 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
537 if (params->freqs == NULL)
538 return;
539 for (count = 0, i = 0; i < mode->num_channels; i++) {
540 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
541 continue;
542 params->freqs[count++] = mode->channels[i].freq;
543 }
544 }
545
546
547 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
548 struct wpa_driver_scan_params *params)
549 {
550 if (wpa_s->hw.modes == NULL)
551 return; /* unknown what channels the driver supports */
552 if (params->freqs)
553 return; /* already using a limited channel set */
554 if (wpa_s->setband == WPA_SETBAND_5G)
555 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
556 params);
557 else if (wpa_s->setband == WPA_SETBAND_2G)
558 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
559 params);
560 }
561
562
563 static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
564 struct wpa_driver_scan_params *params,
565 size_t max_ssids)
566 {
567 unsigned int i;
568 struct wpa_ssid *ssid;
569
570 for (i = 0; i < wpa_s->scan_id_count; i++) {
571 unsigned int j;
572
573 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
574 if (!ssid || !ssid->scan_ssid)
575 continue;
576
577 for (j = 0; j < params->num_ssids; j++) {
578 if (params->ssids[j].ssid_len == ssid->ssid_len &&
579 params->ssids[j].ssid &&
580 os_memcmp(params->ssids[j].ssid, ssid->ssid,
581 ssid->ssid_len) == 0)
582 break;
583 }
584 if (j < params->num_ssids)
585 continue; /* already in the list */
586
587 if (params->num_ssids + 1 > max_ssids) {
588 wpa_printf(MSG_DEBUG,
589 "Over max scan SSIDs for manual request");
590 break;
591 }
592
593 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
594 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
595 params->ssids[params->num_ssids].ssid = ssid->ssid;
596 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
597 params->num_ssids++;
598 }
599
600 wpa_s->scan_id_count = 0;
601 }
602
603
604 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
605 {
606 struct wpa_supplicant *wpa_s = eloop_ctx;
607 struct wpa_ssid *ssid;
608 int ret, p2p_in_prog;
609 struct wpabuf *extra_ie = NULL;
610 struct wpa_driver_scan_params params;
611 struct wpa_driver_scan_params *scan_params;
612 size_t max_ssids;
613 enum wpa_states prev_state;
614
615 if (wpa_s->pno || wpa_s->pno_sched_pending) {
616 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
617 return;
618 }
619
620 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
621 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
622 return;
623 }
624
625 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
626 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
627 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
628 return;
629 }
630
631 if (wpa_s->scanning) {
632 /*
633 * If we are already in scanning state, we shall reschedule the
634 * the incoming scan request.
635 */
636 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
637 wpa_supplicant_req_scan(wpa_s, 1, 0);
638 return;
639 }
640
641 if (!wpa_supplicant_enabled_networks(wpa_s) &&
642 wpa_s->scan_req == NORMAL_SCAN_REQ) {
643 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
644 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
645 return;
646 }
647
648 if (wpa_s->conf->ap_scan != 0 &&
649 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
650 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
651 "overriding ap_scan configuration");
652 wpa_s->conf->ap_scan = 0;
653 wpas_notify_ap_scan_changed(wpa_s);
654 }
655
656 if (wpa_s->conf->ap_scan == 0) {
657 wpa_supplicant_gen_assoc_event(wpa_s);
658 return;
659 }
660
661 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
662 if (p2p_in_prog && p2p_in_prog != 2) {
663 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
664 wpa_supplicant_req_scan(wpa_s, 5, 0);
665 return;
666 }
667
668 if (wpa_s->conf->ap_scan == 2)
669 max_ssids = 1;
670 else {
671 max_ssids = wpa_s->max_scan_ssids;
672 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
673 max_ssids = WPAS_MAX_SCAN_SSIDS;
674 }
675
676 wpa_s->last_scan_req = wpa_s->scan_req;
677 wpa_s->scan_req = NORMAL_SCAN_REQ;
678
679 os_memset(&params, 0, sizeof(params));
680
681 prev_state = wpa_s->wpa_state;
682 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
683 wpa_s->wpa_state == WPA_INACTIVE)
684 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
685
686 /*
687 * If autoscan has set its own scanning parameters
688 */
689 if (wpa_s->autoscan_params != NULL) {
690 scan_params = wpa_s->autoscan_params;
691 goto scan;
692 }
693
694 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
695 wpa_s->connect_without_scan) {
696 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
697 if (ssid == wpa_s->connect_without_scan)
698 break;
699 }
700 wpa_s->connect_without_scan = NULL;
701 if (ssid) {
702 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
703 "without scan step");
704 wpa_supplicant_associate(wpa_s, NULL, ssid);
705 return;
706 }
707 }
708
709 #ifdef CONFIG_P2P
710 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
711 wpa_s->go_params) {
712 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
713 wpa_s->p2p_in_provisioning,
714 wpa_s->show_group_started);
715 params.ssids[0].ssid = wpa_s->go_params->ssid;
716 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
717 params.num_ssids = 1;
718 goto ssid_list_set;
719 }
720
721 if (wpa_s->p2p_in_invitation) {
722 if (wpa_s->current_ssid) {
723 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
724 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
725 params.ssids[0].ssid_len =
726 wpa_s->current_ssid->ssid_len;
727 params.num_ssids = 1;
728 } else {
729 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
730 }
731 goto ssid_list_set;
732 }
733 #endif /* CONFIG_P2P */
734
735 /* Find the starting point from which to continue scanning */
736 ssid = wpa_s->conf->ssid;
737 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
738 while (ssid) {
739 if (ssid == wpa_s->prev_scan_ssid) {
740 ssid = ssid->next;
741 break;
742 }
743 ssid = ssid->next;
744 }
745 }
746
747 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
748 wpa_s->conf->ap_scan == 2) {
749 wpa_s->connect_without_scan = NULL;
750 wpa_s->prev_scan_wildcard = 0;
751 wpa_supplicant_assoc_try(wpa_s, ssid);
752 return;
753 } else if (wpa_s->conf->ap_scan == 2) {
754 /*
755 * User-initiated scan request in ap_scan == 2; scan with
756 * wildcard SSID.
757 */
758 ssid = NULL;
759 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
760 /*
761 * Perform single-channel single-SSID scan for
762 * reassociate-to-same-BSS operation.
763 */
764 /* Setup SSID */
765 ssid = wpa_s->current_ssid;
766 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
767 ssid->ssid, ssid->ssid_len);
768 params.ssids[0].ssid = ssid->ssid;
769 params.ssids[0].ssid_len = ssid->ssid_len;
770 params.num_ssids = 1;
771
772 /*
773 * Allocate memory for frequency array, allocate one extra
774 * slot for the zero-terminator.
775 */
776 params.freqs = os_malloc(sizeof(int) * 2);
777 if (params.freqs == NULL) {
778 wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
779 return;
780 }
781 params.freqs[0] = wpa_s->assoc_freq;
782 params.freqs[1] = 0;
783
784 /*
785 * Reset the reattach flag so that we fall back to full scan if
786 * this scan fails.
787 */
788 wpa_s->reattach = 0;
789 } else {
790 struct wpa_ssid *start = ssid, *tssid;
791 int freqs_set = 0;
792 if (ssid == NULL && max_ssids > 1)
793 ssid = wpa_s->conf->ssid;
794 while (ssid) {
795 if (!wpas_network_disabled(wpa_s, ssid) &&
796 ssid->scan_ssid) {
797 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
798 ssid->ssid, ssid->ssid_len);
799 params.ssids[params.num_ssids].ssid =
800 ssid->ssid;
801 params.ssids[params.num_ssids].ssid_len =
802 ssid->ssid_len;
803 params.num_ssids++;
804 if (params.num_ssids + 1 >= max_ssids)
805 break;
806 }
807 ssid = ssid->next;
808 if (ssid == start)
809 break;
810 if (ssid == NULL && max_ssids > 1 &&
811 start != wpa_s->conf->ssid)
812 ssid = wpa_s->conf->ssid;
813 }
814
815 if (wpa_s->scan_id_count &&
816 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
817 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
818
819 for (tssid = wpa_s->conf->ssid;
820 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
821 tssid = tssid->next) {
822 if (wpas_network_disabled(wpa_s, tssid))
823 continue;
824 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
825 int_array_concat(&params.freqs,
826 tssid->scan_freq);
827 } else {
828 os_free(params.freqs);
829 params.freqs = NULL;
830 }
831 freqs_set = 1;
832 }
833 int_array_sort_unique(params.freqs);
834 }
835
836 if (ssid && max_ssids == 1) {
837 /*
838 * If the driver is limited to 1 SSID at a time interleave
839 * wildcard SSID scans with specific SSID scans to avoid
840 * waiting a long time for a wildcard scan.
841 */
842 if (!wpa_s->prev_scan_wildcard) {
843 params.ssids[0].ssid = NULL;
844 params.ssids[0].ssid_len = 0;
845 wpa_s->prev_scan_wildcard = 1;
846 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
847 "wildcard SSID (Interleave with specific)");
848 } else {
849 wpa_s->prev_scan_ssid = ssid;
850 wpa_s->prev_scan_wildcard = 0;
851 wpa_dbg(wpa_s, MSG_DEBUG,
852 "Starting AP scan for specific SSID: %s",
853 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
854 }
855 } else if (ssid) {
856 /* max_ssids > 1 */
857
858 wpa_s->prev_scan_ssid = ssid;
859 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
860 "the scan request");
861 params.num_ssids++;
862 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
863 wpa_s->manual_scan_passive && params.num_ssids == 0) {
864 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
865 } else {
866 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
867 params.num_ssids++;
868 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
869 "SSID");
870 }
871 #ifdef CONFIG_P2P
872 ssid_list_set:
873 #endif /* CONFIG_P2P */
874
875 wpa_supplicant_optimize_freqs(wpa_s, &params);
876 extra_ie = wpa_supplicant_extra_ies(wpa_s);
877
878 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
879 wpa_s->manual_scan_only_new)
880 params.only_new_results = 1;
881
882 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
883 wpa_s->manual_scan_freqs) {
884 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
885 params.freqs = wpa_s->manual_scan_freqs;
886 wpa_s->manual_scan_freqs = NULL;
887 }
888
889 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
890 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
891 "generated frequency list");
892 params.freqs = wpa_s->next_scan_freqs;
893 } else
894 os_free(wpa_s->next_scan_freqs);
895 wpa_s->next_scan_freqs = NULL;
896 wpa_setband_scan_freqs(wpa_s, &params);
897
898 /* See if user specified frequencies. If so, scan only those. */
899 if (wpa_s->conf->freq_list && !params.freqs) {
900 wpa_dbg(wpa_s, MSG_DEBUG,
901 "Optimize scan based on conf->freq_list");
902 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
903 }
904
905 /* Use current associated channel? */
906 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
907 unsigned int num = wpa_s->num_multichan_concurrent;
908
909 params.freqs = os_calloc(num + 1, sizeof(int));
910 if (params.freqs) {
911 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
912 if (num > 0) {
913 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
914 "current operating channels since "
915 "scan_cur_freq is enabled");
916 } else {
917 os_free(params.freqs);
918 params.freqs = NULL;
919 }
920 }
921 }
922
923 params.filter_ssids = wpa_supplicant_build_filter_ssids(
924 wpa_s->conf, &params.num_filter_ssids);
925 if (extra_ie) {
926 params.extra_ies = wpabuf_head(extra_ie);
927 params.extra_ies_len = wpabuf_len(extra_ie);
928 }
929
930 #ifdef CONFIG_P2P
931 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
932 (wpa_s->show_group_started && wpa_s->go_params)) {
933 /*
934 * The interface may not yet be in P2P mode, so we have to
935 * explicitly request P2P probe to disable CCK rates.
936 */
937 params.p2p_probe = 1;
938 }
939 #endif /* CONFIG_P2P */
940
941 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
942 params.mac_addr_rand = 1;
943 if (wpa_s->mac_addr_scan) {
944 params.mac_addr = wpa_s->mac_addr_scan;
945 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
946 }
947 }
948
949 scan_params = &params;
950
951 scan:
952 #ifdef CONFIG_P2P
953 /*
954 * If the driver does not support multi-channel concurrency and a
955 * virtual interface that shares the same radio with the wpa_s interface
956 * is operating there may not be need to scan other channels apart from
957 * the current operating channel on the other virtual interface. Filter
958 * out other channels in case we are trying to find a connection for a
959 * station interface when we are not configured to prefer station
960 * connection and a concurrent operation is already in process.
961 */
962 if (wpa_s->scan_for_connection &&
963 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
964 !scan_params->freqs && !params.freqs &&
965 wpas_is_p2p_prioritized(wpa_s) &&
966 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
967 non_p2p_network_enabled(wpa_s)) {
968 unsigned int num = wpa_s->num_multichan_concurrent;
969
970 params.freqs = os_calloc(num + 1, sizeof(int));
971 if (params.freqs) {
972 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
973 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
974 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
975 } else {
976 os_free(params.freqs);
977 params.freqs = NULL;
978 }
979 }
980 }
981 #endif /* CONFIG_P2P */
982
983 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
984
985 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
986 !wpa_s->manual_scan_freqs) {
987 /* Restore manual_scan_freqs for the next attempt */
988 wpa_s->manual_scan_freqs = params.freqs;
989 params.freqs = NULL;
990 }
991
992 wpabuf_free(extra_ie);
993 os_free(params.freqs);
994 os_free(params.filter_ssids);
995
996 if (ret) {
997 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
998 if (prev_state != wpa_s->wpa_state)
999 wpa_supplicant_set_state(wpa_s, prev_state);
1000 /* Restore scan_req since we will try to scan again */
1001 wpa_s->scan_req = wpa_s->last_scan_req;
1002 wpa_supplicant_req_scan(wpa_s, 1, 0);
1003 } else {
1004 wpa_s->scan_for_connection = 0;
1005 #ifdef CONFIG_INTERWORKING
1006 wpa_s->interworking_fast_assoc_tried = 0;
1007 #endif /* CONFIG_INTERWORKING */
1008 }
1009 }
1010
1011
1012 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1013 {
1014 struct os_reltime remaining, new_int;
1015 int cancelled;
1016
1017 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1018 &remaining);
1019
1020 new_int.sec = sec;
1021 new_int.usec = 0;
1022 if (cancelled && os_reltime_before(&remaining, &new_int)) {
1023 new_int.sec = remaining.sec;
1024 new_int.usec = remaining.usec;
1025 }
1026
1027 if (cancelled) {
1028 eloop_register_timeout(new_int.sec, new_int.usec,
1029 wpa_supplicant_scan, wpa_s, NULL);
1030 }
1031 wpa_s->scan_interval = sec;
1032 }
1033
1034
1035 /**
1036 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1037 * @wpa_s: Pointer to wpa_supplicant data
1038 * @sec: Number of seconds after which to scan
1039 * @usec: Number of microseconds after which to scan
1040 *
1041 * This function is used to schedule a scan for neighboring access points after
1042 * the specified time.
1043 */
1044 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1045 {
1046 int res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1047 NULL);
1048 if (res == 1) {
1049 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1050 sec, usec);
1051 } else if (res == 0) {
1052 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1053 sec, usec);
1054 } else {
1055 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1056 sec, usec);
1057 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1058 }
1059 }
1060
1061
1062 /**
1063 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1064 * @wpa_s: Pointer to wpa_supplicant data
1065 * @sec: Number of seconds after which to scan
1066 * @usec: Number of microseconds after which to scan
1067 * Returns: 0 on success or -1 otherwise
1068 *
1069 * This function is used to schedule periodic scans for neighboring
1070 * access points after the specified time.
1071 */
1072 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1073 int sec, int usec)
1074 {
1075 if (!wpa_s->sched_scan_supported)
1076 return -1;
1077
1078 eloop_register_timeout(sec, usec,
1079 wpa_supplicant_delayed_sched_scan_timeout,
1080 wpa_s, NULL);
1081
1082 return 0;
1083 }
1084
1085
1086 /**
1087 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1088 * @wpa_s: Pointer to wpa_supplicant data
1089 * Returns: 0 is sched_scan was started or -1 otherwise
1090 *
1091 * This function is used to schedule periodic scans for neighboring
1092 * access points repeating the scan continuously.
1093 */
1094 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1095 {
1096 struct wpa_driver_scan_params params;
1097 struct wpa_driver_scan_params *scan_params;
1098 enum wpa_states prev_state;
1099 struct wpa_ssid *ssid = NULL;
1100 struct wpabuf *extra_ie = NULL;
1101 int ret;
1102 unsigned int max_sched_scan_ssids;
1103 int wildcard = 0;
1104 int need_ssids;
1105
1106 if (!wpa_s->sched_scan_supported)
1107 return -1;
1108
1109 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1110 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1111 else
1112 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1113 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1114 return -1;
1115
1116 if (wpa_s->sched_scanning) {
1117 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1118 return 0;
1119 }
1120
1121 need_ssids = 0;
1122 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1123 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1124 /* Use wildcard SSID to find this network */
1125 wildcard = 1;
1126 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1127 ssid->ssid_len)
1128 need_ssids++;
1129
1130 #ifdef CONFIG_WPS
1131 if (!wpas_network_disabled(wpa_s, ssid) &&
1132 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1133 /*
1134 * Normal scan is more reliable and faster for WPS
1135 * operations and since these are for short periods of
1136 * time, the benefit of trying to use sched_scan would
1137 * be limited.
1138 */
1139 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1140 "sched_scan for WPS");
1141 return -1;
1142 }
1143 #endif /* CONFIG_WPS */
1144 }
1145 if (wildcard)
1146 need_ssids++;
1147
1148 if (wpa_s->normal_scans < 3 &&
1149 (need_ssids <= wpa_s->max_scan_ssids ||
1150 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1151 /*
1152 * When normal scan can speed up operations, use that for the
1153 * first operations before starting the sched_scan to allow
1154 * user space sleep more. We do this only if the normal scan
1155 * has functionality that is suitable for this or if the
1156 * sched_scan does not have better support for multiple SSIDs.
1157 */
1158 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1159 "sched_scan for initial scans (normal_scans=%d)",
1160 wpa_s->normal_scans);
1161 return -1;
1162 }
1163
1164 os_memset(&params, 0, sizeof(params));
1165
1166 /* If we can't allocate space for the filters, we just don't filter */
1167 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1168 sizeof(struct wpa_driver_scan_filter));
1169
1170 prev_state = wpa_s->wpa_state;
1171 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1172 wpa_s->wpa_state == WPA_INACTIVE)
1173 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1174
1175 if (wpa_s->autoscan_params != NULL) {
1176 scan_params = wpa_s->autoscan_params;
1177 goto scan;
1178 }
1179
1180 /* Find the starting point from which to continue scanning */
1181 ssid = wpa_s->conf->ssid;
1182 if (wpa_s->prev_sched_ssid) {
1183 while (ssid) {
1184 if (ssid == wpa_s->prev_sched_ssid) {
1185 ssid = ssid->next;
1186 break;
1187 }
1188 ssid = ssid->next;
1189 }
1190 }
1191
1192 if (!ssid || !wpa_s->prev_sched_ssid) {
1193 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1194 if (wpa_s->conf->sched_scan_interval)
1195 wpa_s->sched_scan_interval =
1196 wpa_s->conf->sched_scan_interval;
1197 if (wpa_s->sched_scan_interval == 0)
1198 wpa_s->sched_scan_interval = 10;
1199 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1200 wpa_s->first_sched_scan = 1;
1201 ssid = wpa_s->conf->ssid;
1202 wpa_s->prev_sched_ssid = ssid;
1203 }
1204
1205 if (wildcard) {
1206 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1207 params.num_ssids++;
1208 }
1209
1210 while (ssid) {
1211 if (wpas_network_disabled(wpa_s, ssid))
1212 goto next;
1213
1214 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1215 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1216 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1217 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1218 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1219 ssid->ssid, ssid->ssid_len);
1220 params.filter_ssids[params.num_filter_ssids].ssid_len =
1221 ssid->ssid_len;
1222 params.num_filter_ssids++;
1223 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1224 {
1225 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1226 "filter for sched_scan - drop filter");
1227 os_free(params.filter_ssids);
1228 params.filter_ssids = NULL;
1229 params.num_filter_ssids = 0;
1230 }
1231
1232 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1233 if (params.num_ssids == max_sched_scan_ssids)
1234 break; /* only room for broadcast SSID */
1235 wpa_dbg(wpa_s, MSG_DEBUG,
1236 "add to active scan ssid: %s",
1237 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1238 params.ssids[params.num_ssids].ssid =
1239 ssid->ssid;
1240 params.ssids[params.num_ssids].ssid_len =
1241 ssid->ssid_len;
1242 params.num_ssids++;
1243 if (params.num_ssids >= max_sched_scan_ssids) {
1244 wpa_s->prev_sched_ssid = ssid;
1245 do {
1246 ssid = ssid->next;
1247 } while (ssid &&
1248 (wpas_network_disabled(wpa_s, ssid) ||
1249 !ssid->scan_ssid));
1250 break;
1251 }
1252 }
1253
1254 next:
1255 wpa_s->prev_sched_ssid = ssid;
1256 ssid = ssid->next;
1257 }
1258
1259 if (params.num_filter_ssids == 0) {
1260 os_free(params.filter_ssids);
1261 params.filter_ssids = NULL;
1262 }
1263
1264 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1265 if (extra_ie) {
1266 params.extra_ies = wpabuf_head(extra_ie);
1267 params.extra_ies_len = wpabuf_len(extra_ie);
1268 }
1269
1270 if (wpa_s->conf->filter_rssi)
1271 params.filter_rssi = wpa_s->conf->filter_rssi;
1272
1273 /* See if user specified frequencies. If so, scan only those. */
1274 if (wpa_s->conf->freq_list && !params.freqs) {
1275 wpa_dbg(wpa_s, MSG_DEBUG,
1276 "Optimize scan based on conf->freq_list");
1277 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1278 }
1279
1280 scan_params = &params;
1281
1282 scan:
1283 if (ssid || !wpa_s->first_sched_scan) {
1284 wpa_dbg(wpa_s, MSG_DEBUG,
1285 "Starting sched scan: interval %d timeout %d",
1286 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1287 } else {
1288 wpa_dbg(wpa_s, MSG_DEBUG,
1289 "Starting sched scan: interval %d (no timeout)",
1290 wpa_s->sched_scan_interval);
1291 }
1292
1293 wpa_setband_scan_freqs(wpa_s, scan_params);
1294
1295 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1296 params.mac_addr_rand = 1;
1297 if (wpa_s->mac_addr_sched_scan) {
1298 params.mac_addr = wpa_s->mac_addr_sched_scan;
1299 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1300 ETH_ALEN;
1301 }
1302 }
1303
1304 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1305 wpa_s->sched_scan_interval);
1306 wpabuf_free(extra_ie);
1307 os_free(params.filter_ssids);
1308 if (ret) {
1309 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1310 if (prev_state != wpa_s->wpa_state)
1311 wpa_supplicant_set_state(wpa_s, prev_state);
1312 return ret;
1313 }
1314
1315 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1316 if (ssid || !wpa_s->first_sched_scan) {
1317 wpa_s->sched_scan_timed_out = 0;
1318 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1319 wpa_supplicant_sched_scan_timeout,
1320 wpa_s, NULL);
1321 wpa_s->first_sched_scan = 0;
1322 wpa_s->sched_scan_timeout /= 2;
1323 wpa_s->sched_scan_interval *= 2;
1324 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1325 wpa_s->sched_scan_interval = 10;
1326 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1327 }
1328 }
1329
1330 /* If there is no more ssids, start next time from the beginning */
1331 if (!ssid)
1332 wpa_s->prev_sched_ssid = NULL;
1333
1334 return 0;
1335 }
1336
1337
1338 /**
1339 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1340 * @wpa_s: Pointer to wpa_supplicant data
1341 *
1342 * This function is used to cancel a scan request scheduled with
1343 * wpa_supplicant_req_scan().
1344 */
1345 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1346 {
1347 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1348 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1349 }
1350
1351
1352 /**
1353 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1354 * @wpa_s: Pointer to wpa_supplicant data
1355 *
1356 * This function is used to stop a delayed scheduled scan.
1357 */
1358 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1359 {
1360 if (!wpa_s->sched_scan_supported)
1361 return;
1362
1363 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1364 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1365 wpa_s, NULL);
1366 }
1367
1368
1369 /**
1370 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1371 * @wpa_s: Pointer to wpa_supplicant data
1372 *
1373 * This function is used to stop a periodic scheduled scan.
1374 */
1375 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1376 {
1377 if (!wpa_s->sched_scanning)
1378 return;
1379
1380 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1381 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1382 wpa_supplicant_stop_sched_scan(wpa_s);
1383 }
1384
1385
1386 /**
1387 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1388 * @wpa_s: Pointer to wpa_supplicant data
1389 * @scanning: Whether scanning is currently in progress
1390 *
1391 * This function is to generate scanning notifycations. It is called whenever
1392 * there may have been a change in scanning (scan started, completed, stopped).
1393 * wpas_notify_scanning() is called whenever the scanning state changed from the
1394 * previously notified state.
1395 */
1396 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1397 int scanning)
1398 {
1399 if (wpa_s->scanning != scanning) {
1400 wpa_s->scanning = scanning;
1401 wpas_notify_scanning(wpa_s);
1402 }
1403 }
1404
1405
1406 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1407 {
1408 int rate = 0;
1409 const u8 *ie;
1410 int i;
1411
1412 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1413 for (i = 0; ie && i < ie[1]; i++) {
1414 if ((ie[i + 2] & 0x7f) > rate)
1415 rate = ie[i + 2] & 0x7f;
1416 }
1417
1418 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1419 for (i = 0; ie && i < ie[1]; i++) {
1420 if ((ie[i + 2] & 0x7f) > rate)
1421 rate = ie[i + 2] & 0x7f;
1422 }
1423
1424 return rate;
1425 }
1426
1427
1428 /**
1429 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1430 * @res: Scan result entry
1431 * @ie: Information element identitifier (WLAN_EID_*)
1432 * Returns: Pointer to the information element (id field) or %NULL if not found
1433 *
1434 * This function returns the first matching information element in the scan
1435 * result.
1436 */
1437 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1438 {
1439 const u8 *end, *pos;
1440
1441 pos = (const u8 *) (res + 1);
1442 end = pos + res->ie_len;
1443
1444 while (pos + 1 < end) {
1445 if (pos + 2 + pos[1] > end)
1446 break;
1447 if (pos[0] == ie)
1448 return pos;
1449 pos += 2 + pos[1];
1450 }
1451
1452 return NULL;
1453 }
1454
1455
1456 /**
1457 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1458 * @res: Scan result entry
1459 * @vendor_type: Vendor type (four octets starting the IE payload)
1460 * Returns: Pointer to the information element (id field) or %NULL if not found
1461 *
1462 * This function returns the first matching information element in the scan
1463 * result.
1464 */
1465 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1466 u32 vendor_type)
1467 {
1468 const u8 *end, *pos;
1469
1470 pos = (const u8 *) (res + 1);
1471 end = pos + res->ie_len;
1472
1473 while (pos + 1 < end) {
1474 if (pos + 2 + pos[1] > end)
1475 break;
1476 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1477 vendor_type == WPA_GET_BE32(&pos[2]))
1478 return pos;
1479 pos += 2 + pos[1];
1480 }
1481
1482 return NULL;
1483 }
1484
1485
1486 /**
1487 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1488 * @res: Scan result entry
1489 * @vendor_type: Vendor type (four octets starting the IE payload)
1490 * Returns: Pointer to the information element (id field) or %NULL if not found
1491 *
1492 * This function returns the first matching information element in the scan
1493 * result.
1494 *
1495 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1496 * from Beacon frames instead of either Beacon or Probe Response frames.
1497 */
1498 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1499 u32 vendor_type)
1500 {
1501 const u8 *end, *pos;
1502
1503 if (res->beacon_ie_len == 0)
1504 return NULL;
1505
1506 pos = (const u8 *) (res + 1);
1507 pos += res->ie_len;
1508 end = pos + res->beacon_ie_len;
1509
1510 while (pos + 1 < end) {
1511 if (pos + 2 + pos[1] > end)
1512 break;
1513 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1514 vendor_type == WPA_GET_BE32(&pos[2]))
1515 return pos;
1516 pos += 2 + pos[1];
1517 }
1518
1519 return NULL;
1520 }
1521
1522
1523 /**
1524 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1525 * @res: Scan result entry
1526 * @vendor_type: Vendor type (four octets starting the IE payload)
1527 * Returns: Pointer to the information element payload or %NULL if not found
1528 *
1529 * This function returns concatenated payload of possibly fragmented vendor
1530 * specific information elements in the scan result. The caller is responsible
1531 * for freeing the returned buffer.
1532 */
1533 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1534 u32 vendor_type)
1535 {
1536 struct wpabuf *buf;
1537 const u8 *end, *pos;
1538
1539 buf = wpabuf_alloc(res->ie_len);
1540 if (buf == NULL)
1541 return NULL;
1542
1543 pos = (const u8 *) (res + 1);
1544 end = pos + res->ie_len;
1545
1546 while (pos + 1 < end) {
1547 if (pos + 2 + pos[1] > end)
1548 break;
1549 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1550 vendor_type == WPA_GET_BE32(&pos[2]))
1551 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1552 pos += 2 + pos[1];
1553 }
1554
1555 if (wpabuf_len(buf) == 0) {
1556 wpabuf_free(buf);
1557 buf = NULL;
1558 }
1559
1560 return buf;
1561 }
1562
1563
1564 /*
1565 * Channels with a great SNR can operate at full rate. What is a great SNR?
1566 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1567 * rule of thumb is that any SNR above 20 is good." This one
1568 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1569 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1570 * conservative value.
1571 */
1572 #define GREAT_SNR 30
1573
1574 /* Compare function for sorting scan results. Return >0 if @b is considered
1575 * better. */
1576 static int wpa_scan_result_compar(const void *a, const void *b)
1577 {
1578 #define IS_5GHZ(n) (n > 4000)
1579 #define MIN(a,b) a < b ? a : b
1580 struct wpa_scan_res **_wa = (void *) a;
1581 struct wpa_scan_res **_wb = (void *) b;
1582 struct wpa_scan_res *wa = *_wa;
1583 struct wpa_scan_res *wb = *_wb;
1584 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1585 int snr_a, snr_b;
1586
1587 /* WPA/WPA2 support preferred */
1588 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1589 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1590 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1591 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1592
1593 if (wpa_b && !wpa_a)
1594 return 1;
1595 if (!wpa_b && wpa_a)
1596 return -1;
1597
1598 /* privacy support preferred */
1599 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1600 (wb->caps & IEEE80211_CAP_PRIVACY))
1601 return 1;
1602 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1603 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1604 return -1;
1605
1606 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1607 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1608 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1609 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1610 } else {
1611 /* Not suitable information to calculate SNR, so use level */
1612 snr_a = wa->level;
1613 snr_b = wb->level;
1614 }
1615
1616 /* best/max rate preferred if SNR close enough */
1617 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1618 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1619 maxrate_a = wpa_scan_get_max_rate(wa);
1620 maxrate_b = wpa_scan_get_max_rate(wb);
1621 if (maxrate_a != maxrate_b)
1622 return maxrate_b - maxrate_a;
1623 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1624 return IS_5GHZ(wa->freq) ? -1 : 1;
1625 }
1626
1627 /* use freq for channel preference */
1628
1629 /* all things being equal, use SNR; if SNRs are
1630 * identical, use quality values since some drivers may only report
1631 * that value and leave the signal level zero */
1632 if (snr_b == snr_a)
1633 return wb->qual - wa->qual;
1634 return snr_b - snr_a;
1635 #undef MIN
1636 #undef IS_5GHZ
1637 }
1638
1639
1640 #ifdef CONFIG_WPS
1641 /* Compare function for sorting scan results when searching a WPS AP for
1642 * provisioning. Return >0 if @b is considered better. */
1643 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1644 {
1645 struct wpa_scan_res **_wa = (void *) a;
1646 struct wpa_scan_res **_wb = (void *) b;
1647 struct wpa_scan_res *wa = *_wa;
1648 struct wpa_scan_res *wb = *_wb;
1649 int uses_wps_a, uses_wps_b;
1650 struct wpabuf *wps_a, *wps_b;
1651 int res;
1652
1653 /* Optimization - check WPS IE existence before allocated memory and
1654 * doing full reassembly. */
1655 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1656 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1657 if (uses_wps_a && !uses_wps_b)
1658 return -1;
1659 if (!uses_wps_a && uses_wps_b)
1660 return 1;
1661
1662 if (uses_wps_a && uses_wps_b) {
1663 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1664 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1665 res = wps_ap_priority_compar(wps_a, wps_b);
1666 wpabuf_free(wps_a);
1667 wpabuf_free(wps_b);
1668 if (res)
1669 return res;
1670 }
1671
1672 /*
1673 * Do not use current AP security policy as a sorting criteria during
1674 * WPS provisioning step since the AP may get reconfigured at the
1675 * completion of provisioning.
1676 */
1677
1678 /* all things being equal, use signal level; if signal levels are
1679 * identical, use quality values since some drivers may only report
1680 * that value and leave the signal level zero */
1681 if (wb->level == wa->level)
1682 return wb->qual - wa->qual;
1683 return wb->level - wa->level;
1684 }
1685 #endif /* CONFIG_WPS */
1686
1687
1688 static void dump_scan_res(struct wpa_scan_results *scan_res)
1689 {
1690 #ifndef CONFIG_NO_STDOUT_DEBUG
1691 size_t i;
1692
1693 if (scan_res->res == NULL || scan_res->num == 0)
1694 return;
1695
1696 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1697
1698 for (i = 0; i < scan_res->num; i++) {
1699 struct wpa_scan_res *r = scan_res->res[i];
1700 u8 *pos;
1701 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1702 == WPA_SCAN_LEVEL_DBM) {
1703 int snr = r->level - r->noise;
1704 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1705 "noise=%d level=%d snr=%d%s flags=0x%x "
1706 "age=%u",
1707 MAC2STR(r->bssid), r->freq, r->qual,
1708 r->noise, r->level, snr,
1709 snr >= GREAT_SNR ? "*" : "", r->flags,
1710 r->age);
1711 } else {
1712 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1713 "noise=%d level=%d flags=0x%x age=%u",
1714 MAC2STR(r->bssid), r->freq, r->qual,
1715 r->noise, r->level, r->flags, r->age);
1716 }
1717 pos = (u8 *) (r + 1);
1718 if (r->ie_len)
1719 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1720 pos += r->ie_len;
1721 if (r->beacon_ie_len)
1722 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1723 pos, r->beacon_ie_len);
1724 }
1725 #endif /* CONFIG_NO_STDOUT_DEBUG */
1726 }
1727
1728
1729 /**
1730 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1731 * @wpa_s: Pointer to wpa_supplicant data
1732 * @bssid: BSSID to check
1733 * Returns: 0 if the BSSID is filtered or 1 if not
1734 *
1735 * This function is used to filter out specific BSSIDs from scan reslts mainly
1736 * for testing purposes (SET bssid_filter ctrl_iface command).
1737 */
1738 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1739 const u8 *bssid)
1740 {
1741 size_t i;
1742
1743 if (wpa_s->bssid_filter == NULL)
1744 return 1;
1745
1746 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1747 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1748 ETH_ALEN) == 0)
1749 return 1;
1750 }
1751
1752 return 0;
1753 }
1754
1755
1756 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1757 struct wpa_scan_results *res)
1758 {
1759 size_t i, j;
1760
1761 if (wpa_s->bssid_filter == NULL)
1762 return;
1763
1764 for (i = 0, j = 0; i < res->num; i++) {
1765 if (wpa_supplicant_filter_bssid_match(wpa_s,
1766 res->res[i]->bssid)) {
1767 res->res[j++] = res->res[i];
1768 } else {
1769 os_free(res->res[i]);
1770 res->res[i] = NULL;
1771 }
1772 }
1773
1774 if (res->num != j) {
1775 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1776 (int) (res->num - j));
1777 res->num = j;
1778 }
1779 }
1780
1781
1782 /**
1783 * wpa_supplicant_get_scan_results - Get scan results
1784 * @wpa_s: Pointer to wpa_supplicant data
1785 * @info: Information about what was scanned or %NULL if not available
1786 * @new_scan: Whether a new scan was performed
1787 * Returns: Scan results, %NULL on failure
1788 *
1789 * This function request the current scan results from the driver and updates
1790 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1791 * results with wpa_scan_results_free().
1792 */
1793 struct wpa_scan_results *
1794 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1795 struct scan_info *info, int new_scan)
1796 {
1797 struct wpa_scan_results *scan_res;
1798 size_t i;
1799 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1800
1801 scan_res = wpa_drv_get_scan_results2(wpa_s);
1802 if (scan_res == NULL) {
1803 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1804 return NULL;
1805 }
1806 if (scan_res->fetch_time.sec == 0) {
1807 /*
1808 * Make sure we have a valid timestamp if the driver wrapper
1809 * does not set this.
1810 */
1811 os_get_reltime(&scan_res->fetch_time);
1812 }
1813 filter_scan_res(wpa_s, scan_res);
1814
1815 #ifdef CONFIG_WPS
1816 if (wpas_wps_searching(wpa_s)) {
1817 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1818 "provisioning rules");
1819 compar = wpa_scan_result_wps_compar;
1820 }
1821 #endif /* CONFIG_WPS */
1822
1823 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1824 compar);
1825 dump_scan_res(scan_res);
1826
1827 wpa_bss_update_start(wpa_s);
1828 for (i = 0; i < scan_res->num; i++)
1829 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1830 &scan_res->fetch_time);
1831 wpa_bss_update_end(wpa_s, info, new_scan);
1832
1833 return scan_res;
1834 }
1835
1836
1837 /**
1838 * wpa_supplicant_update_scan_results - Update scan results from the driver
1839 * @wpa_s: Pointer to wpa_supplicant data
1840 * Returns: 0 on success, -1 on failure
1841 *
1842 * This function updates the BSS table within wpa_supplicant based on the
1843 * currently available scan results from the driver without requesting a new
1844 * scan. This is used in cases where the driver indicates an association
1845 * (including roaming within ESS) and wpa_supplicant does not yet have the
1846 * needed information to complete the connection (e.g., to perform validation
1847 * steps in 4-way handshake).
1848 */
1849 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1850 {
1851 struct wpa_scan_results *scan_res;
1852 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1853 if (scan_res == NULL)
1854 return -1;
1855 wpa_scan_results_free(scan_res);
1856
1857 return 0;
1858 }
1859
1860
1861 /**
1862 * scan_only_handler - Reports scan results
1863 */
1864 void scan_only_handler(struct wpa_supplicant *wpa_s,
1865 struct wpa_scan_results *scan_res)
1866 {
1867 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
1868 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1869 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1870 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1871 wpa_s->manual_scan_id);
1872 wpa_s->manual_scan_use_id = 0;
1873 } else {
1874 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1875 }
1876 wpas_notify_scan_results(wpa_s);
1877 wpas_notify_scan_done(wpa_s, 1);
1878 if (wpa_s->scan_work) {
1879 struct wpa_radio_work *work = wpa_s->scan_work;
1880 wpa_s->scan_work = NULL;
1881 radio_work_done(work);
1882 }
1883 }
1884
1885
1886 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1887 {
1888 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1889 }
1890
1891
1892 struct wpa_driver_scan_params *
1893 wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
1894 {
1895 struct wpa_driver_scan_params *params;
1896 size_t i;
1897 u8 *n;
1898
1899 params = os_zalloc(sizeof(*params));
1900 if (params == NULL)
1901 return NULL;
1902
1903 for (i = 0; i < src->num_ssids; i++) {
1904 if (src->ssids[i].ssid) {
1905 n = os_malloc(src->ssids[i].ssid_len);
1906 if (n == NULL)
1907 goto failed;
1908 os_memcpy(n, src->ssids[i].ssid,
1909 src->ssids[i].ssid_len);
1910 params->ssids[i].ssid = n;
1911 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
1912 }
1913 }
1914 params->num_ssids = src->num_ssids;
1915
1916 if (src->extra_ies) {
1917 n = os_malloc(src->extra_ies_len);
1918 if (n == NULL)
1919 goto failed;
1920 os_memcpy(n, src->extra_ies, src->extra_ies_len);
1921 params->extra_ies = n;
1922 params->extra_ies_len = src->extra_ies_len;
1923 }
1924
1925 if (src->freqs) {
1926 int len = int_array_len(src->freqs);
1927 params->freqs = os_malloc((len + 1) * sizeof(int));
1928 if (params->freqs == NULL)
1929 goto failed;
1930 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
1931 }
1932
1933 if (src->filter_ssids) {
1934 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
1935 src->num_filter_ssids);
1936 if (params->filter_ssids == NULL)
1937 goto failed;
1938 os_memcpy(params->filter_ssids, src->filter_ssids,
1939 sizeof(*params->filter_ssids) *
1940 src->num_filter_ssids);
1941 params->num_filter_ssids = src->num_filter_ssids;
1942 }
1943
1944 params->filter_rssi = src->filter_rssi;
1945 params->p2p_probe = src->p2p_probe;
1946 params->only_new_results = src->only_new_results;
1947 params->low_priority = src->low_priority;
1948
1949 if (src->mac_addr_rand) {
1950 params->mac_addr_rand = src->mac_addr_rand;
1951
1952 if (src->mac_addr && src->mac_addr_mask) {
1953 u8 *mac_addr;
1954
1955 mac_addr = os_malloc(2 * ETH_ALEN);
1956 if (!mac_addr)
1957 goto failed;
1958
1959 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
1960 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
1961 ETH_ALEN);
1962 params->mac_addr = mac_addr;
1963 params->mac_addr_mask = mac_addr + ETH_ALEN;
1964 }
1965 }
1966 return params;
1967
1968 failed:
1969 wpa_scan_free_params(params);
1970 return NULL;
1971 }
1972
1973
1974 void wpa_scan_free_params(struct wpa_driver_scan_params *params)
1975 {
1976 size_t i;
1977
1978 if (params == NULL)
1979 return;
1980
1981 for (i = 0; i < params->num_ssids; i++)
1982 os_free((u8 *) params->ssids[i].ssid);
1983 os_free((u8 *) params->extra_ies);
1984 os_free(params->freqs);
1985 os_free(params->filter_ssids);
1986
1987 /*
1988 * Note: params->mac_addr_mask points to same memory allocation and
1989 * must not be freed separately.
1990 */
1991 os_free((u8 *) params->mac_addr);
1992
1993 os_free(params);
1994 }
1995
1996
1997 int wpas_start_pno(struct wpa_supplicant *wpa_s)
1998 {
1999 int ret, interval;
2000 size_t i, num_ssid, num_match_ssid;
2001 struct wpa_ssid *ssid;
2002 struct wpa_driver_scan_params params;
2003
2004 if (!wpa_s->sched_scan_supported)
2005 return -1;
2006
2007 if (wpa_s->pno || wpa_s->pno_sched_pending)
2008 return 0;
2009
2010 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2011 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2012 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2013 return -EAGAIN;
2014 }
2015
2016 if (wpa_s->wpa_state == WPA_SCANNING) {
2017 wpa_supplicant_cancel_scan(wpa_s);
2018 if (wpa_s->sched_scanning) {
2019 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2020 "ongoing sched scan");
2021 wpa_supplicant_cancel_sched_scan(wpa_s);
2022 wpa_s->pno_sched_pending = 1;
2023 return 0;
2024 }
2025 }
2026
2027 os_memset(&params, 0, sizeof(params));
2028
2029 num_ssid = num_match_ssid = 0;
2030 ssid = wpa_s->conf->ssid;
2031 while (ssid) {
2032 if (!wpas_network_disabled(wpa_s, ssid)) {
2033 num_match_ssid++;
2034 if (ssid->scan_ssid)
2035 num_ssid++;
2036 }
2037 ssid = ssid->next;
2038 }
2039
2040 if (num_match_ssid == 0) {
2041 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2042 return -1;
2043 }
2044
2045 if (num_match_ssid > num_ssid) {
2046 params.num_ssids++; /* wildcard */
2047 num_ssid++;
2048 }
2049
2050 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
2051 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2052 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
2053 num_ssid = WPAS_MAX_SCAN_SSIDS;
2054 }
2055
2056 if (num_match_ssid > wpa_s->max_match_sets) {
2057 num_match_ssid = wpa_s->max_match_sets;
2058 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
2059 }
2060 params.filter_ssids = os_calloc(num_match_ssid,
2061 sizeof(struct wpa_driver_scan_filter));
2062 if (params.filter_ssids == NULL)
2063 return -1;
2064 i = 0;
2065 ssid = wpa_s->conf->ssid;
2066 while (ssid) {
2067 if (!wpas_network_disabled(wpa_s, ssid)) {
2068 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2069 params.ssids[params.num_ssids].ssid =
2070 ssid->ssid;
2071 params.ssids[params.num_ssids].ssid_len =
2072 ssid->ssid_len;
2073 params.num_ssids++;
2074 }
2075 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2076 ssid->ssid_len);
2077 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2078 params.num_filter_ssids++;
2079 i++;
2080 if (i == num_match_ssid)
2081 break;
2082 }
2083 ssid = ssid->next;
2084 }
2085
2086 if (wpa_s->conf->filter_rssi)
2087 params.filter_rssi = wpa_s->conf->filter_rssi;
2088
2089 interval = wpa_s->conf->sched_scan_interval ?
2090 wpa_s->conf->sched_scan_interval : 10;
2091
2092 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2093 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2094 params.freqs = wpa_s->manual_sched_scan_freqs;
2095 }
2096
2097 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2098 params.mac_addr_rand = 1;
2099 if (wpa_s->mac_addr_pno) {
2100 params.mac_addr = wpa_s->mac_addr_pno;
2101 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2102 }
2103 }
2104
2105 ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
2106 os_free(params.filter_ssids);
2107 if (ret == 0)
2108 wpa_s->pno = 1;
2109 else
2110 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2111 return ret;
2112 }
2113
2114
2115 int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2116 {
2117 int ret = 0;
2118
2119 if (!wpa_s->pno)
2120 return 0;
2121
2122 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2123
2124 wpa_s->pno = 0;
2125 wpa_s->pno_sched_pending = 0;
2126
2127 if (wpa_s->wpa_state == WPA_SCANNING)
2128 wpa_supplicant_req_scan(wpa_s, 0, 0);
2129
2130 return ret;
2131 }
2132
2133
2134 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2135 unsigned int type)
2136 {
2137 type &= MAC_ADDR_RAND_ALL;
2138 wpa_s->mac_addr_rand_enable &= ~type;
2139
2140 if (type & MAC_ADDR_RAND_SCAN) {
2141 os_free(wpa_s->mac_addr_scan);
2142 wpa_s->mac_addr_scan = NULL;
2143 }
2144
2145 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2146 os_free(wpa_s->mac_addr_sched_scan);
2147 wpa_s->mac_addr_sched_scan = NULL;
2148 }
2149
2150 if (type & MAC_ADDR_RAND_PNO) {
2151 os_free(wpa_s->mac_addr_pno);
2152 wpa_s->mac_addr_pno = NULL;
2153 }
2154 }
2155
2156
2157 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2158 unsigned int type, const u8 *addr,
2159 const u8 *mask)
2160 {
2161 u8 *tmp = NULL;
2162
2163 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2164
2165 if (addr) {
2166 tmp = os_malloc(2 * ETH_ALEN);
2167 if (!tmp)
2168 return -1;
2169 os_memcpy(tmp, addr, ETH_ALEN);
2170 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2171 }
2172
2173 if (type == MAC_ADDR_RAND_SCAN) {
2174 wpa_s->mac_addr_scan = tmp;
2175 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2176 wpa_s->mac_addr_sched_scan = tmp;
2177 } else if (type == MAC_ADDR_RAND_PNO) {
2178 wpa_s->mac_addr_pno = tmp;
2179 } else {
2180 wpa_printf(MSG_INFO,
2181 "scan: Invalid MAC randomization type=0x%x",
2182 type);
2183 os_free(tmp);
2184 return -1;
2185 }
2186
2187 wpa_s->mac_addr_rand_enable |= type;
2188 return 0;
2189 }