]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/scan.c
Track whether scan was started by us or an external program
[thirdparty/hostap.git] / wpa_supplicant / scan.c
1 /*
2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2012, 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 "gas_query.h"
25 #include "scan.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 /**
146 * wpa_supplicant_trigger_scan - Request driver to start a scan
147 * @wpa_s: Pointer to wpa_supplicant data
148 * @params: Scan parameters
149 * Returns: 0 on success, -1 on failure
150 */
151 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
152 struct wpa_driver_scan_params *params)
153 {
154 int ret;
155
156 wpa_supplicant_notify_scanning(wpa_s, 1);
157
158 ret = wpa_drv_scan(wpa_s, params);
159 if (ret) {
160 wpa_supplicant_notify_scanning(wpa_s, 0);
161 wpas_notify_scan_done(wpa_s, 0);
162 } else {
163 os_get_reltime(&wpa_s->scan_trigger_time);
164 wpa_s->scan_runs++;
165 wpa_s->normal_scans++;
166 wpa_s->own_scan_requested = 1;
167 }
168
169 return ret;
170 }
171
172
173 static void
174 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
175 {
176 struct wpa_supplicant *wpa_s = eloop_ctx;
177
178 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
179
180 if (wpa_supplicant_req_sched_scan(wpa_s))
181 wpa_supplicant_req_scan(wpa_s, 0, 0);
182 }
183
184
185 static void
186 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
187 {
188 struct wpa_supplicant *wpa_s = eloop_ctx;
189
190 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
191
192 wpa_s->sched_scan_timed_out = 1;
193 wpa_supplicant_cancel_sched_scan(wpa_s);
194 }
195
196
197 int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
198 struct wpa_driver_scan_params *params,
199 int interval)
200 {
201 int ret;
202
203 wpa_supplicant_notify_scanning(wpa_s, 1);
204 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
205 if (ret)
206 wpa_supplicant_notify_scanning(wpa_s, 0);
207 else
208 wpa_s->sched_scanning = 1;
209
210 return ret;
211 }
212
213
214 int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
215 {
216 int ret;
217
218 ret = wpa_drv_stop_sched_scan(wpa_s);
219 if (ret) {
220 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
221 /* TODO: what to do if stopping fails? */
222 return -1;
223 }
224
225 return ret;
226 }
227
228
229 static struct wpa_driver_scan_filter *
230 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
231 {
232 struct wpa_driver_scan_filter *ssids;
233 struct wpa_ssid *ssid;
234 size_t count;
235
236 *num_ssids = 0;
237 if (!conf->filter_ssids)
238 return NULL;
239
240 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
241 if (ssid->ssid && ssid->ssid_len)
242 count++;
243 }
244 if (count == 0)
245 return NULL;
246 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
247 if (ssids == NULL)
248 return NULL;
249
250 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
251 if (!ssid->ssid || !ssid->ssid_len)
252 continue;
253 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
254 ssids[*num_ssids].ssid_len = ssid->ssid_len;
255 (*num_ssids)++;
256 }
257
258 return ssids;
259 }
260
261
262 static void wpa_supplicant_optimize_freqs(
263 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
264 {
265 #ifdef CONFIG_P2P
266 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
267 wpa_s->go_params) {
268 /* Optimize provisioning state scan based on GO information */
269 if (wpa_s->p2p_in_provisioning < 5 &&
270 wpa_s->go_params->freq > 0) {
271 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
272 "preferred frequency %d MHz",
273 wpa_s->go_params->freq);
274 params->freqs = os_zalloc(2 * sizeof(int));
275 if (params->freqs)
276 params->freqs[0] = wpa_s->go_params->freq;
277 } else if (wpa_s->p2p_in_provisioning < 8 &&
278 wpa_s->go_params->freq_list[0]) {
279 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
280 "channels");
281 int_array_concat(&params->freqs,
282 wpa_s->go_params->freq_list);
283 if (params->freqs)
284 int_array_sort_unique(params->freqs);
285 }
286 wpa_s->p2p_in_provisioning++;
287 }
288 #endif /* CONFIG_P2P */
289
290 #ifdef CONFIG_WPS
291 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
292 /*
293 * Optimize post-provisioning scan based on channel used
294 * during provisioning.
295 */
296 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
297 "that was used during provisioning", wpa_s->wps_freq);
298 params->freqs = os_zalloc(2 * sizeof(int));
299 if (params->freqs)
300 params->freqs[0] = wpa_s->wps_freq;
301 wpa_s->after_wps--;
302 }
303
304 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
305 {
306 /* Optimize provisioning scan based on already known channel */
307 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
308 wpa_s->wps_freq);
309 params->freqs = os_zalloc(2 * sizeof(int));
310 if (params->freqs)
311 params->freqs[0] = wpa_s->wps_freq;
312 wpa_s->known_wps_freq = 0; /* only do this once */
313 }
314 #endif /* CONFIG_WPS */
315 }
316
317
318 #ifdef CONFIG_INTERWORKING
319 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
320 struct wpabuf *buf)
321 {
322 if (wpa_s->conf->interworking == 0)
323 return;
324
325 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
326 wpabuf_put_u8(buf, 4);
327 wpabuf_put_u8(buf, 0x00);
328 wpabuf_put_u8(buf, 0x00);
329 wpabuf_put_u8(buf, 0x00);
330 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
331
332 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
333 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
334 1 + ETH_ALEN);
335 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
336 /* No Venue Info */
337 if (!is_zero_ether_addr(wpa_s->conf->hessid))
338 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
339 }
340 #endif /* CONFIG_INTERWORKING */
341
342
343 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
344 {
345 struct wpabuf *extra_ie = NULL;
346 #ifdef CONFIG_WPS
347 int wps = 0;
348 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
349 #endif /* CONFIG_WPS */
350
351 #ifdef CONFIG_INTERWORKING
352 if (wpa_s->conf->interworking &&
353 wpabuf_resize(&extra_ie, 100) == 0)
354 wpas_add_interworking_elements(wpa_s, extra_ie);
355 #endif /* CONFIG_INTERWORKING */
356
357 #ifdef CONFIG_WPS
358 wps = wpas_wps_in_use(wpa_s, &req_type);
359
360 if (wps) {
361 struct wpabuf *wps_ie;
362 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
363 DEV_PW_DEFAULT,
364 &wpa_s->wps->dev,
365 wpa_s->wps->uuid, req_type,
366 0, NULL);
367 if (wps_ie) {
368 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
369 wpabuf_put_buf(extra_ie, wps_ie);
370 wpabuf_free(wps_ie);
371 }
372 }
373
374 #ifdef CONFIG_P2P
375 if (wps) {
376 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
377 if (wpabuf_resize(&extra_ie, ielen) == 0)
378 wpas_p2p_scan_ie(wpa_s, extra_ie);
379 }
380 #endif /* CONFIG_P2P */
381
382 #endif /* CONFIG_WPS */
383
384 #ifdef CONFIG_HS20
385 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
386 wpas_hs20_add_indication(extra_ie);
387 #endif /* CONFIG_HS20 */
388
389 return extra_ie;
390 }
391
392
393 #ifdef CONFIG_P2P
394
395 /*
396 * Check whether there are any enabled networks or credentials that could be
397 * used for a non-P2P connection.
398 */
399 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
400 {
401 struct wpa_ssid *ssid;
402
403 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
404 if (wpas_network_disabled(wpa_s, ssid))
405 continue;
406 if (!ssid->p2p_group)
407 return 1;
408 }
409
410 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
411 wpa_s->conf->auto_interworking)
412 return 1;
413
414 return 0;
415 }
416
417 #endif /* CONFIG_P2P */
418
419
420 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
421 u16 num_modes,
422 enum hostapd_hw_mode mode)
423 {
424 u16 i;
425
426 for (i = 0; i < num_modes; i++) {
427 if (modes[i].mode == mode)
428 return &modes[i];
429 }
430
431 return NULL;
432 }
433
434
435 static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
436 enum hostapd_hw_mode band,
437 struct wpa_driver_scan_params *params)
438 {
439 /* Include only supported channels for the specified band */
440 struct hostapd_hw_modes *mode;
441 int count, i;
442
443 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
444 if (mode == NULL) {
445 /* No channels supported in this band - use empty list */
446 params->freqs = os_zalloc(sizeof(int));
447 return;
448 }
449
450 params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
451 if (params->freqs == NULL)
452 return;
453 for (count = 0, i = 0; i < mode->num_channels; i++) {
454 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
455 continue;
456 params->freqs[count++] = mode->channels[i].freq;
457 }
458 }
459
460
461 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
462 struct wpa_driver_scan_params *params)
463 {
464 if (wpa_s->hw.modes == NULL)
465 return; /* unknown what channels the driver supports */
466 if (params->freqs)
467 return; /* already using a limited channel set */
468 if (wpa_s->setband == WPA_SETBAND_5G)
469 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
470 params);
471 else if (wpa_s->setband == WPA_SETBAND_2G)
472 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
473 params);
474 }
475
476
477 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
478 {
479 struct wpa_supplicant *wpa_s = eloop_ctx;
480 struct wpa_ssid *ssid;
481 int ret;
482 struct wpabuf *extra_ie = NULL;
483 struct wpa_driver_scan_params params;
484 struct wpa_driver_scan_params *scan_params;
485 size_t max_ssids;
486 enum wpa_states prev_state;
487
488 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
489 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
490 wpas_p2p_continue_after_scan(wpa_s);
491 return;
492 }
493
494 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
495 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
496 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
497 wpas_p2p_continue_after_scan(wpa_s);
498 return;
499 }
500
501 if (wpa_s->scanning) {
502 /*
503 * If we are already in scanning state, we shall reschedule the
504 * the incoming scan request.
505 */
506 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
507 wpa_supplicant_req_scan(wpa_s, 1, 0);
508 return;
509 }
510
511 if (!wpa_supplicant_enabled_networks(wpa_s) &&
512 wpa_s->scan_req == NORMAL_SCAN_REQ) {
513 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
514 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
515 wpas_p2p_continue_after_scan(wpa_s);
516 return;
517 }
518
519 if (wpa_s->conf->ap_scan != 0 &&
520 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
521 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
522 "overriding ap_scan configuration");
523 wpa_s->conf->ap_scan = 0;
524 wpas_notify_ap_scan_changed(wpa_s);
525 }
526
527 if (wpa_s->conf->ap_scan == 0) {
528 wpa_supplicant_gen_assoc_event(wpa_s);
529 return;
530 }
531
532 #ifdef CONFIG_P2P
533 if (wpas_p2p_in_progress(wpa_s) || wpas_wpa_is_in_progress(wpa_s, 0)) {
534 if (wpa_s->sta_scan_pending &&
535 wpas_p2p_in_progress(wpa_s) == 2 &&
536 wpa_s->global->p2p_cb_on_scan_complete) {
537 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
538 "mode scan during P2P search");
539 } else {
540 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
541 "while P2P operation is in progress");
542 wpa_s->sta_scan_pending = 1;
543 wpa_supplicant_req_scan(wpa_s, 5, 0);
544 return;
545 }
546 }
547 #endif /* CONFIG_P2P */
548
549 #ifdef CONFIG_GAS
550 if (gas_query_in_progress(wpa_s->gas)) {
551 wpa_dbg(wpa_s, MSG_DEBUG, "Delay scan while GAS query is in progress");
552 wpa_supplicant_req_scan(wpa_s, 1, 0);
553 return;
554 }
555 #endif /* CONFIG_GAS */
556
557 if (wpa_s->conf->ap_scan == 2)
558 max_ssids = 1;
559 else {
560 max_ssids = wpa_s->max_scan_ssids;
561 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
562 max_ssids = WPAS_MAX_SCAN_SSIDS;
563 }
564
565 wpa_s->last_scan_req = wpa_s->scan_req;
566 wpa_s->scan_req = NORMAL_SCAN_REQ;
567
568 os_memset(&params, 0, sizeof(params));
569
570 prev_state = wpa_s->wpa_state;
571 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
572 wpa_s->wpa_state == WPA_INACTIVE)
573 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
574
575 /*
576 * If autoscan has set its own scanning parameters
577 */
578 if (wpa_s->autoscan_params != NULL) {
579 scan_params = wpa_s->autoscan_params;
580 goto scan;
581 }
582
583 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
584 wpa_s->connect_without_scan) {
585 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
586 if (ssid == wpa_s->connect_without_scan)
587 break;
588 }
589 wpa_s->connect_without_scan = NULL;
590 if (ssid) {
591 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
592 "without scan step");
593 wpa_supplicant_associate(wpa_s, NULL, ssid);
594 return;
595 }
596 }
597
598 #ifdef CONFIG_P2P
599 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
600 wpa_s->go_params) {
601 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
602 wpa_s->p2p_in_provisioning,
603 wpa_s->show_group_started);
604 params.ssids[0].ssid = wpa_s->go_params->ssid;
605 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
606 params.num_ssids = 1;
607 goto ssid_list_set;
608 }
609 #endif /* CONFIG_P2P */
610
611 /* Find the starting point from which to continue scanning */
612 ssid = wpa_s->conf->ssid;
613 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
614 while (ssid) {
615 if (ssid == wpa_s->prev_scan_ssid) {
616 ssid = ssid->next;
617 break;
618 }
619 ssid = ssid->next;
620 }
621 }
622
623 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
624 wpa_s->conf->ap_scan == 2) {
625 wpa_s->connect_without_scan = NULL;
626 wpa_s->prev_scan_wildcard = 0;
627 wpa_supplicant_assoc_try(wpa_s, ssid);
628 return;
629 } else if (wpa_s->conf->ap_scan == 2) {
630 /*
631 * User-initiated scan request in ap_scan == 2; scan with
632 * wildcard SSID.
633 */
634 ssid = NULL;
635 } else {
636 struct wpa_ssid *start = ssid, *tssid;
637 int freqs_set = 0;
638 if (ssid == NULL && max_ssids > 1)
639 ssid = wpa_s->conf->ssid;
640 while (ssid) {
641 if (!wpas_network_disabled(wpa_s, ssid) &&
642 ssid->scan_ssid) {
643 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
644 ssid->ssid, ssid->ssid_len);
645 params.ssids[params.num_ssids].ssid =
646 ssid->ssid;
647 params.ssids[params.num_ssids].ssid_len =
648 ssid->ssid_len;
649 params.num_ssids++;
650 if (params.num_ssids + 1 >= max_ssids)
651 break;
652 }
653 ssid = ssid->next;
654 if (ssid == start)
655 break;
656 if (ssid == NULL && max_ssids > 1 &&
657 start != wpa_s->conf->ssid)
658 ssid = wpa_s->conf->ssid;
659 }
660
661 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
662 if (wpas_network_disabled(wpa_s, tssid))
663 continue;
664 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
665 int_array_concat(&params.freqs,
666 tssid->scan_freq);
667 } else {
668 os_free(params.freqs);
669 params.freqs = NULL;
670 }
671 freqs_set = 1;
672 }
673 int_array_sort_unique(params.freqs);
674 }
675
676 if (ssid && max_ssids == 1) {
677 /*
678 * If the driver is limited to 1 SSID at a time interleave
679 * wildcard SSID scans with specific SSID scans to avoid
680 * waiting a long time for a wildcard scan.
681 */
682 if (!wpa_s->prev_scan_wildcard) {
683 params.ssids[0].ssid = NULL;
684 params.ssids[0].ssid_len = 0;
685 wpa_s->prev_scan_wildcard = 1;
686 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
687 "wildcard SSID (Interleave with specific)");
688 } else {
689 wpa_s->prev_scan_ssid = ssid;
690 wpa_s->prev_scan_wildcard = 0;
691 wpa_dbg(wpa_s, MSG_DEBUG,
692 "Starting AP scan for specific SSID: %s",
693 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
694 }
695 } else if (ssid) {
696 /* max_ssids > 1 */
697
698 wpa_s->prev_scan_ssid = ssid;
699 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
700 "the scan request");
701 params.num_ssids++;
702 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
703 wpa_s->manual_scan_passive && params.num_ssids == 0) {
704 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
705 } else {
706 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
707 params.num_ssids++;
708 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
709 "SSID");
710 }
711 #ifdef CONFIG_P2P
712 ssid_list_set:
713 #endif /* CONFIG_P2P */
714
715 wpa_supplicant_optimize_freqs(wpa_s, &params);
716 extra_ie = wpa_supplicant_extra_ies(wpa_s);
717
718 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
719 wpa_s->manual_scan_freqs) {
720 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
721 params.freqs = wpa_s->manual_scan_freqs;
722 wpa_s->manual_scan_freqs = NULL;
723 }
724
725 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
726 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
727 "generated frequency list");
728 params.freqs = wpa_s->next_scan_freqs;
729 } else
730 os_free(wpa_s->next_scan_freqs);
731 wpa_s->next_scan_freqs = NULL;
732 wpa_setband_scan_freqs(wpa_s, &params);
733
734 /* See if user specified frequencies. If so, scan only those. */
735 if (wpa_s->conf->freq_list && !params.freqs) {
736 wpa_dbg(wpa_s, MSG_DEBUG,
737 "Optimize scan based on conf->freq_list");
738 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
739 }
740
741 /* Use current associated channel? */
742 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
743 unsigned int num = wpa_s->num_multichan_concurrent;
744
745 params.freqs = os_calloc(num + 1, sizeof(int));
746 if (params.freqs) {
747 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
748 if (num > 0) {
749 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
750 "current operating channels since "
751 "scan_cur_freq is enabled");
752 } else {
753 os_free(params.freqs);
754 params.freqs = NULL;
755 }
756 }
757 }
758
759 params.filter_ssids = wpa_supplicant_build_filter_ssids(
760 wpa_s->conf, &params.num_filter_ssids);
761 if (extra_ie) {
762 params.extra_ies = wpabuf_head(extra_ie);
763 params.extra_ies_len = wpabuf_len(extra_ie);
764 }
765
766 #ifdef CONFIG_P2P
767 if (wpa_s->p2p_in_provisioning ||
768 (wpa_s->show_group_started && wpa_s->go_params)) {
769 /*
770 * The interface may not yet be in P2P mode, so we have to
771 * explicitly request P2P probe to disable CCK rates.
772 */
773 params.p2p_probe = 1;
774 }
775 #endif /* CONFIG_P2P */
776
777 scan_params = &params;
778
779 scan:
780 #ifdef CONFIG_P2P
781 /*
782 * If the driver does not support multi-channel concurrency and a
783 * virtual interface that shares the same radio with the wpa_s interface
784 * is operating there may not be need to scan other channels apart from
785 * the current operating channel on the other virtual interface. Filter
786 * out other channels in case we are trying to find a connection for a
787 * station interface when we are not configured to prefer station
788 * connection and a concurrent operation is already in process.
789 */
790 if (wpa_s->scan_for_connection &&
791 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
792 !scan_params->freqs && !params.freqs &&
793 wpas_is_p2p_prioritized(wpa_s) &&
794 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
795 non_p2p_network_enabled(wpa_s)) {
796 unsigned int num = wpa_s->num_multichan_concurrent;
797
798 params.freqs = os_calloc(num + 1, sizeof(int));
799 if (params.freqs) {
800 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
801 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
802 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
803 } else {
804 os_free(params.freqs);
805 params.freqs = NULL;
806 }
807 }
808 }
809 #endif /* CONFIG_P2P */
810
811 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
812
813 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
814 !wpa_s->manual_scan_freqs) {
815 /* Restore manual_scan_freqs for the next attempt */
816 wpa_s->manual_scan_freqs = params.freqs;
817 params.freqs = NULL;
818 }
819
820 wpabuf_free(extra_ie);
821 os_free(params.freqs);
822 os_free(params.filter_ssids);
823
824 if (ret) {
825 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
826 if (prev_state != wpa_s->wpa_state)
827 wpa_supplicant_set_state(wpa_s, prev_state);
828 /* Restore scan_req since we will try to scan again */
829 wpa_s->scan_req = wpa_s->last_scan_req;
830 wpa_supplicant_req_scan(wpa_s, 1, 0);
831 } else {
832 wpa_s->scan_for_connection = 0;
833 }
834 }
835
836
837 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
838 {
839 struct os_reltime remaining, new_int;
840 int cancelled;
841
842 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
843 &remaining);
844
845 new_int.sec = sec;
846 new_int.usec = 0;
847 if (cancelled && os_reltime_before(&remaining, &new_int)) {
848 new_int.sec = remaining.sec;
849 new_int.usec = remaining.usec;
850 }
851
852 if (cancelled) {
853 eloop_register_timeout(new_int.sec, new_int.usec,
854 wpa_supplicant_scan, wpa_s, NULL);
855 }
856 wpa_s->scan_interval = sec;
857 }
858
859
860 /**
861 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
862 * @wpa_s: Pointer to wpa_supplicant data
863 * @sec: Number of seconds after which to scan
864 * @usec: Number of microseconds after which to scan
865 *
866 * This function is used to schedule a scan for neighboring access points after
867 * the specified time.
868 */
869 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
870 {
871 if (eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL))
872 {
873 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d sec %d usec",
874 sec, usec);
875 return;
876 }
877
878 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
879 sec, usec);
880 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
881 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
882 }
883
884
885 /**
886 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
887 * @wpa_s: Pointer to wpa_supplicant data
888 * @sec: Number of seconds after which to scan
889 * @usec: Number of microseconds after which to scan
890 * Returns: 0 on success or -1 otherwise
891 *
892 * This function is used to schedule periodic scans for neighboring
893 * access points after the specified time.
894 */
895 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
896 int sec, int usec)
897 {
898 if (!wpa_s->sched_scan_supported)
899 return -1;
900
901 eloop_register_timeout(sec, usec,
902 wpa_supplicant_delayed_sched_scan_timeout,
903 wpa_s, NULL);
904
905 return 0;
906 }
907
908
909 /**
910 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
911 * @wpa_s: Pointer to wpa_supplicant data
912 * Returns: 0 is sched_scan was started or -1 otherwise
913 *
914 * This function is used to schedule periodic scans for neighboring
915 * access points repeating the scan continuously.
916 */
917 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
918 {
919 struct wpa_driver_scan_params params;
920 struct wpa_driver_scan_params *scan_params;
921 enum wpa_states prev_state;
922 struct wpa_ssid *ssid = NULL;
923 struct wpabuf *extra_ie = NULL;
924 int ret;
925 unsigned int max_sched_scan_ssids;
926 int wildcard = 0;
927 int need_ssids;
928
929 if (!wpa_s->sched_scan_supported)
930 return -1;
931
932 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
933 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
934 else
935 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
936 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
937 return -1;
938
939 if (wpa_s->sched_scanning) {
940 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
941 return 0;
942 }
943
944 need_ssids = 0;
945 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
946 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
947 /* Use wildcard SSID to find this network */
948 wildcard = 1;
949 } else if (!wpas_network_disabled(wpa_s, ssid) &&
950 ssid->ssid_len)
951 need_ssids++;
952
953 #ifdef CONFIG_WPS
954 if (!wpas_network_disabled(wpa_s, ssid) &&
955 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
956 /*
957 * Normal scan is more reliable and faster for WPS
958 * operations and since these are for short periods of
959 * time, the benefit of trying to use sched_scan would
960 * be limited.
961 */
962 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
963 "sched_scan for WPS");
964 return -1;
965 }
966 #endif /* CONFIG_WPS */
967 }
968 if (wildcard)
969 need_ssids++;
970
971 if (wpa_s->normal_scans < 3 &&
972 (need_ssids <= wpa_s->max_scan_ssids ||
973 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
974 /*
975 * When normal scan can speed up operations, use that for the
976 * first operations before starting the sched_scan to allow
977 * user space sleep more. We do this only if the normal scan
978 * has functionality that is suitable for this or if the
979 * sched_scan does not have better support for multiple SSIDs.
980 */
981 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
982 "sched_scan for initial scans (normal_scans=%d)",
983 wpa_s->normal_scans);
984 return -1;
985 }
986
987 os_memset(&params, 0, sizeof(params));
988
989 /* If we can't allocate space for the filters, we just don't filter */
990 params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
991 sizeof(struct wpa_driver_scan_filter));
992
993 prev_state = wpa_s->wpa_state;
994 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
995 wpa_s->wpa_state == WPA_INACTIVE)
996 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
997
998 if (wpa_s->autoscan_params != NULL) {
999 scan_params = wpa_s->autoscan_params;
1000 goto scan;
1001 }
1002
1003 /* Find the starting point from which to continue scanning */
1004 ssid = wpa_s->conf->ssid;
1005 if (wpa_s->prev_sched_ssid) {
1006 while (ssid) {
1007 if (ssid == wpa_s->prev_sched_ssid) {
1008 ssid = ssid->next;
1009 break;
1010 }
1011 ssid = ssid->next;
1012 }
1013 }
1014
1015 if (!ssid || !wpa_s->prev_sched_ssid) {
1016 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1017 if (wpa_s->conf->sched_scan_interval)
1018 wpa_s->sched_scan_interval =
1019 wpa_s->conf->sched_scan_interval;
1020 if (wpa_s->sched_scan_interval == 0)
1021 wpa_s->sched_scan_interval = 10;
1022 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1023 wpa_s->first_sched_scan = 1;
1024 ssid = wpa_s->conf->ssid;
1025 wpa_s->prev_sched_ssid = ssid;
1026 }
1027
1028 if (wildcard) {
1029 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1030 params.num_ssids++;
1031 }
1032
1033 while (ssid) {
1034 if (wpas_network_disabled(wpa_s, ssid))
1035 goto next;
1036
1037 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1038 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1039 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1040 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1041 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1042 ssid->ssid, ssid->ssid_len);
1043 params.filter_ssids[params.num_filter_ssids].ssid_len =
1044 ssid->ssid_len;
1045 params.num_filter_ssids++;
1046 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1047 {
1048 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1049 "filter for sched_scan - drop filter");
1050 os_free(params.filter_ssids);
1051 params.filter_ssids = NULL;
1052 params.num_filter_ssids = 0;
1053 }
1054
1055 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1056 if (params.num_ssids == max_sched_scan_ssids)
1057 break; /* only room for broadcast SSID */
1058 wpa_dbg(wpa_s, MSG_DEBUG,
1059 "add to active scan ssid: %s",
1060 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1061 params.ssids[params.num_ssids].ssid =
1062 ssid->ssid;
1063 params.ssids[params.num_ssids].ssid_len =
1064 ssid->ssid_len;
1065 params.num_ssids++;
1066 if (params.num_ssids >= max_sched_scan_ssids) {
1067 wpa_s->prev_sched_ssid = ssid;
1068 do {
1069 ssid = ssid->next;
1070 } while (ssid &&
1071 (wpas_network_disabled(wpa_s, ssid) ||
1072 !ssid->scan_ssid));
1073 break;
1074 }
1075 }
1076
1077 next:
1078 wpa_s->prev_sched_ssid = ssid;
1079 ssid = ssid->next;
1080 }
1081
1082 if (params.num_filter_ssids == 0) {
1083 os_free(params.filter_ssids);
1084 params.filter_ssids = NULL;
1085 }
1086
1087 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1088 if (extra_ie) {
1089 params.extra_ies = wpabuf_head(extra_ie);
1090 params.extra_ies_len = wpabuf_len(extra_ie);
1091 }
1092
1093 scan_params = &params;
1094
1095 scan:
1096 if (ssid || !wpa_s->first_sched_scan) {
1097 wpa_dbg(wpa_s, MSG_DEBUG,
1098 "Starting sched scan: interval %d timeout %d",
1099 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1100 } else {
1101 wpa_dbg(wpa_s, MSG_DEBUG,
1102 "Starting sched scan: interval %d (no timeout)",
1103 wpa_s->sched_scan_interval);
1104 }
1105
1106 wpa_setband_scan_freqs(wpa_s, scan_params);
1107
1108 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1109 wpa_s->sched_scan_interval);
1110 wpabuf_free(extra_ie);
1111 os_free(params.filter_ssids);
1112 if (ret) {
1113 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1114 if (prev_state != wpa_s->wpa_state)
1115 wpa_supplicant_set_state(wpa_s, prev_state);
1116 return ret;
1117 }
1118
1119 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1120 if (ssid || !wpa_s->first_sched_scan) {
1121 wpa_s->sched_scan_timed_out = 0;
1122 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1123 wpa_supplicant_sched_scan_timeout,
1124 wpa_s, NULL);
1125 wpa_s->first_sched_scan = 0;
1126 wpa_s->sched_scan_timeout /= 2;
1127 wpa_s->sched_scan_interval *= 2;
1128 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1129 wpa_s->sched_scan_interval = 10;
1130 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1131 }
1132 }
1133
1134 /* If there is no more ssids, start next time from the beginning */
1135 if (!ssid)
1136 wpa_s->prev_sched_ssid = NULL;
1137
1138 return 0;
1139 }
1140
1141
1142 /**
1143 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1144 * @wpa_s: Pointer to wpa_supplicant data
1145 *
1146 * This function is used to cancel a scan request scheduled with
1147 * wpa_supplicant_req_scan().
1148 */
1149 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1150 {
1151 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1152 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1153 wpas_p2p_continue_after_scan(wpa_s);
1154 }
1155
1156
1157 /**
1158 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1159 * @wpa_s: Pointer to wpa_supplicant data
1160 *
1161 * This function is used to stop a delayed scheduled scan.
1162 */
1163 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1164 {
1165 if (!wpa_s->sched_scan_supported)
1166 return;
1167
1168 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1169 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1170 wpa_s, NULL);
1171 }
1172
1173
1174 /**
1175 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1176 * @wpa_s: Pointer to wpa_supplicant data
1177 *
1178 * This function is used to stop a periodic scheduled scan.
1179 */
1180 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1181 {
1182 if (!wpa_s->sched_scanning)
1183 return;
1184
1185 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1186 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1187 wpa_supplicant_stop_sched_scan(wpa_s);
1188 }
1189
1190
1191 /**
1192 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1193 * @wpa_s: Pointer to wpa_supplicant data
1194 * @scanning: Whether scanning is currently in progress
1195 *
1196 * This function is to generate scanning notifycations. It is called whenever
1197 * there may have been a change in scanning (scan started, completed, stopped).
1198 * wpas_notify_scanning() is called whenever the scanning state changed from the
1199 * previously notified state.
1200 */
1201 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1202 int scanning)
1203 {
1204 if (wpa_s->scanning != scanning) {
1205 wpa_s->scanning = scanning;
1206 wpas_notify_scanning(wpa_s);
1207 }
1208 }
1209
1210
1211 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1212 {
1213 int rate = 0;
1214 const u8 *ie;
1215 int i;
1216
1217 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1218 for (i = 0; ie && i < ie[1]; i++) {
1219 if ((ie[i + 2] & 0x7f) > rate)
1220 rate = ie[i + 2] & 0x7f;
1221 }
1222
1223 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1224 for (i = 0; ie && i < ie[1]; i++) {
1225 if ((ie[i + 2] & 0x7f) > rate)
1226 rate = ie[i + 2] & 0x7f;
1227 }
1228
1229 return rate;
1230 }
1231
1232
1233 /**
1234 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1235 * @res: Scan result entry
1236 * @ie: Information element identitifier (WLAN_EID_*)
1237 * Returns: Pointer to the information element (id field) or %NULL if not found
1238 *
1239 * This function returns the first matching information element in the scan
1240 * result.
1241 */
1242 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1243 {
1244 const u8 *end, *pos;
1245
1246 pos = (const u8 *) (res + 1);
1247 end = pos + res->ie_len;
1248
1249 while (pos + 1 < end) {
1250 if (pos + 2 + pos[1] > end)
1251 break;
1252 if (pos[0] == ie)
1253 return pos;
1254 pos += 2 + pos[1];
1255 }
1256
1257 return NULL;
1258 }
1259
1260
1261 /**
1262 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1263 * @res: Scan result entry
1264 * @vendor_type: Vendor type (four octets starting the IE payload)
1265 * Returns: Pointer to the information element (id field) or %NULL if not found
1266 *
1267 * This function returns the first matching information element in the scan
1268 * result.
1269 */
1270 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1271 u32 vendor_type)
1272 {
1273 const u8 *end, *pos;
1274
1275 pos = (const u8 *) (res + 1);
1276 end = pos + res->ie_len;
1277
1278 while (pos + 1 < end) {
1279 if (pos + 2 + pos[1] > end)
1280 break;
1281 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1282 vendor_type == WPA_GET_BE32(&pos[2]))
1283 return pos;
1284 pos += 2 + pos[1];
1285 }
1286
1287 return NULL;
1288 }
1289
1290
1291 /**
1292 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1293 * @res: Scan result entry
1294 * @vendor_type: Vendor type (four octets starting the IE payload)
1295 * Returns: Pointer to the information element (id field) or %NULL if not found
1296 *
1297 * This function returns the first matching information element in the scan
1298 * result.
1299 *
1300 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1301 * from Beacon frames instead of either Beacon or Probe Response frames.
1302 */
1303 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1304 u32 vendor_type)
1305 {
1306 const u8 *end, *pos;
1307
1308 if (res->beacon_ie_len == 0)
1309 return NULL;
1310
1311 pos = (const u8 *) (res + 1);
1312 pos += res->ie_len;
1313 end = pos + res->beacon_ie_len;
1314
1315 while (pos + 1 < end) {
1316 if (pos + 2 + pos[1] > end)
1317 break;
1318 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1319 vendor_type == WPA_GET_BE32(&pos[2]))
1320 return pos;
1321 pos += 2 + pos[1];
1322 }
1323
1324 return NULL;
1325 }
1326
1327
1328 /**
1329 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1330 * @res: Scan result entry
1331 * @vendor_type: Vendor type (four octets starting the IE payload)
1332 * Returns: Pointer to the information element payload or %NULL if not found
1333 *
1334 * This function returns concatenated payload of possibly fragmented vendor
1335 * specific information elements in the scan result. The caller is responsible
1336 * for freeing the returned buffer.
1337 */
1338 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1339 u32 vendor_type)
1340 {
1341 struct wpabuf *buf;
1342 const u8 *end, *pos;
1343
1344 buf = wpabuf_alloc(res->ie_len);
1345 if (buf == NULL)
1346 return NULL;
1347
1348 pos = (const u8 *) (res + 1);
1349 end = pos + res->ie_len;
1350
1351 while (pos + 1 < end) {
1352 if (pos + 2 + pos[1] > end)
1353 break;
1354 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1355 vendor_type == WPA_GET_BE32(&pos[2]))
1356 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1357 pos += 2 + pos[1];
1358 }
1359
1360 if (wpabuf_len(buf) == 0) {
1361 wpabuf_free(buf);
1362 buf = NULL;
1363 }
1364
1365 return buf;
1366 }
1367
1368
1369 /*
1370 * Channels with a great SNR can operate at full rate. What is a great SNR?
1371 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1372 * rule of thumb is that any SNR above 20 is good." This one
1373 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1374 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1375 * conservative value.
1376 */
1377 #define GREAT_SNR 30
1378
1379 /* Compare function for sorting scan results. Return >0 if @b is considered
1380 * better. */
1381 static int wpa_scan_result_compar(const void *a, const void *b)
1382 {
1383 #define IS_5GHZ(n) (n > 4000)
1384 #define MIN(a,b) a < b ? a : b
1385 struct wpa_scan_res **_wa = (void *) a;
1386 struct wpa_scan_res **_wb = (void *) b;
1387 struct wpa_scan_res *wa = *_wa;
1388 struct wpa_scan_res *wb = *_wb;
1389 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1390 int snr_a, snr_b;
1391
1392 /* WPA/WPA2 support preferred */
1393 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1394 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1395 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1396 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1397
1398 if (wpa_b && !wpa_a)
1399 return 1;
1400 if (!wpa_b && wpa_a)
1401 return -1;
1402
1403 /* privacy support preferred */
1404 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1405 (wb->caps & IEEE80211_CAP_PRIVACY))
1406 return 1;
1407 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1408 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1409 return -1;
1410
1411 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1412 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1413 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1414 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1415 } else {
1416 /* Not suitable information to calculate SNR, so use level */
1417 snr_a = wa->level;
1418 snr_b = wb->level;
1419 }
1420
1421 /* best/max rate preferred if SNR close enough */
1422 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1423 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1424 maxrate_a = wpa_scan_get_max_rate(wa);
1425 maxrate_b = wpa_scan_get_max_rate(wb);
1426 if (maxrate_a != maxrate_b)
1427 return maxrate_b - maxrate_a;
1428 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1429 return IS_5GHZ(wa->freq) ? -1 : 1;
1430 }
1431
1432 /* use freq for channel preference */
1433
1434 /* all things being equal, use SNR; if SNRs are
1435 * identical, use quality values since some drivers may only report
1436 * that value and leave the signal level zero */
1437 if (snr_b == snr_a)
1438 return wb->qual - wa->qual;
1439 return snr_b - snr_a;
1440 #undef MIN
1441 #undef IS_5GHZ
1442 }
1443
1444
1445 #ifdef CONFIG_WPS
1446 /* Compare function for sorting scan results when searching a WPS AP for
1447 * provisioning. Return >0 if @b is considered better. */
1448 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1449 {
1450 struct wpa_scan_res **_wa = (void *) a;
1451 struct wpa_scan_res **_wb = (void *) b;
1452 struct wpa_scan_res *wa = *_wa;
1453 struct wpa_scan_res *wb = *_wb;
1454 int uses_wps_a, uses_wps_b;
1455 struct wpabuf *wps_a, *wps_b;
1456 int res;
1457
1458 /* Optimization - check WPS IE existence before allocated memory and
1459 * doing full reassembly. */
1460 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1461 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1462 if (uses_wps_a && !uses_wps_b)
1463 return -1;
1464 if (!uses_wps_a && uses_wps_b)
1465 return 1;
1466
1467 if (uses_wps_a && uses_wps_b) {
1468 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1469 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1470 res = wps_ap_priority_compar(wps_a, wps_b);
1471 wpabuf_free(wps_a);
1472 wpabuf_free(wps_b);
1473 if (res)
1474 return res;
1475 }
1476
1477 /*
1478 * Do not use current AP security policy as a sorting criteria during
1479 * WPS provisioning step since the AP may get reconfigured at the
1480 * completion of provisioning.
1481 */
1482
1483 /* all things being equal, use signal level; if signal levels are
1484 * identical, use quality values since some drivers may only report
1485 * that value and leave the signal level zero */
1486 if (wb->level == wa->level)
1487 return wb->qual - wa->qual;
1488 return wb->level - wa->level;
1489 }
1490 #endif /* CONFIG_WPS */
1491
1492
1493 static void dump_scan_res(struct wpa_scan_results *scan_res)
1494 {
1495 #ifndef CONFIG_NO_STDOUT_DEBUG
1496 size_t i;
1497
1498 if (scan_res->res == NULL || scan_res->num == 0)
1499 return;
1500
1501 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1502
1503 for (i = 0; i < scan_res->num; i++) {
1504 struct wpa_scan_res *r = scan_res->res[i];
1505 u8 *pos;
1506 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1507 == WPA_SCAN_LEVEL_DBM) {
1508 int snr = r->level - r->noise;
1509 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1510 "noise=%d level=%d snr=%d%s flags=0x%x "
1511 "age=%u",
1512 MAC2STR(r->bssid), r->freq, r->qual,
1513 r->noise, r->level, snr,
1514 snr >= GREAT_SNR ? "*" : "", r->flags,
1515 r->age);
1516 } else {
1517 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1518 "noise=%d level=%d flags=0x%x age=%u",
1519 MAC2STR(r->bssid), r->freq, r->qual,
1520 r->noise, r->level, r->flags, r->age);
1521 }
1522 pos = (u8 *) (r + 1);
1523 if (r->ie_len)
1524 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1525 pos += r->ie_len;
1526 if (r->beacon_ie_len)
1527 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1528 pos, r->beacon_ie_len);
1529 }
1530 #endif /* CONFIG_NO_STDOUT_DEBUG */
1531 }
1532
1533
1534 /**
1535 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1536 * @wpa_s: Pointer to wpa_supplicant data
1537 * @bssid: BSSID to check
1538 * Returns: 0 if the BSSID is filtered or 1 if not
1539 *
1540 * This function is used to filter out specific BSSIDs from scan reslts mainly
1541 * for testing purposes (SET bssid_filter ctrl_iface command).
1542 */
1543 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1544 const u8 *bssid)
1545 {
1546 size_t i;
1547
1548 if (wpa_s->bssid_filter == NULL)
1549 return 1;
1550
1551 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1552 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1553 ETH_ALEN) == 0)
1554 return 1;
1555 }
1556
1557 return 0;
1558 }
1559
1560
1561 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1562 struct wpa_scan_results *res)
1563 {
1564 size_t i, j;
1565
1566 if (wpa_s->bssid_filter == NULL)
1567 return;
1568
1569 for (i = 0, j = 0; i < res->num; i++) {
1570 if (wpa_supplicant_filter_bssid_match(wpa_s,
1571 res->res[i]->bssid)) {
1572 res->res[j++] = res->res[i];
1573 } else {
1574 os_free(res->res[i]);
1575 res->res[i] = NULL;
1576 }
1577 }
1578
1579 if (res->num != j) {
1580 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1581 (int) (res->num - j));
1582 res->num = j;
1583 }
1584 }
1585
1586
1587 /**
1588 * wpa_supplicant_get_scan_results - Get scan results
1589 * @wpa_s: Pointer to wpa_supplicant data
1590 * @info: Information about what was scanned or %NULL if not available
1591 * @new_scan: Whether a new scan was performed
1592 * Returns: Scan results, %NULL on failure
1593 *
1594 * This function request the current scan results from the driver and updates
1595 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1596 * results with wpa_scan_results_free().
1597 */
1598 struct wpa_scan_results *
1599 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1600 struct scan_info *info, int new_scan)
1601 {
1602 struct wpa_scan_results *scan_res;
1603 size_t i;
1604 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1605
1606 scan_res = wpa_drv_get_scan_results2(wpa_s);
1607 if (scan_res == NULL) {
1608 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1609 return NULL;
1610 }
1611 if (scan_res->fetch_time.sec == 0) {
1612 /*
1613 * Make sure we have a valid timestamp if the driver wrapper
1614 * does not set this.
1615 */
1616 os_get_reltime(&scan_res->fetch_time);
1617 }
1618 filter_scan_res(wpa_s, scan_res);
1619
1620 #ifdef CONFIG_WPS
1621 if (wpas_wps_in_progress(wpa_s)) {
1622 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1623 "provisioning rules");
1624 compar = wpa_scan_result_wps_compar;
1625 }
1626 #endif /* CONFIG_WPS */
1627
1628 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1629 compar);
1630 dump_scan_res(scan_res);
1631
1632 wpa_bss_update_start(wpa_s);
1633 for (i = 0; i < scan_res->num; i++)
1634 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1635 &scan_res->fetch_time);
1636 wpa_bss_update_end(wpa_s, info, new_scan);
1637
1638 return scan_res;
1639 }
1640
1641
1642 /**
1643 * wpa_supplicant_update_scan_results - Update scan results from the driver
1644 * @wpa_s: Pointer to wpa_supplicant data
1645 * Returns: 0 on success, -1 on failure
1646 *
1647 * This function updates the BSS table within wpa_supplicant based on the
1648 * currently available scan results from the driver without requesting a new
1649 * scan. This is used in cases where the driver indicates an association
1650 * (including roaming within ESS) and wpa_supplicant does not yet have the
1651 * needed information to complete the connection (e.g., to perform validation
1652 * steps in 4-way handshake).
1653 */
1654 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1655 {
1656 struct wpa_scan_results *scan_res;
1657 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1658 if (scan_res == NULL)
1659 return -1;
1660 wpa_scan_results_free(scan_res);
1661
1662 return 0;
1663 }
1664
1665
1666 /**
1667 * scan_only_handler - Reports scan results
1668 */
1669 void scan_only_handler(struct wpa_supplicant *wpa_s,
1670 struct wpa_scan_results *scan_res)
1671 {
1672 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
1673 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1674 wpas_notify_scan_results(wpa_s);
1675 wpas_notify_scan_done(wpa_s, 1);
1676 }
1677
1678
1679 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1680 {
1681 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1682 }