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