2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
16 #include "wpa_supplicant_i.h"
18 #include "wps_supplicant.h"
19 #include "p2p_supplicant.h"
21 #include "hs20_supplicant.h"
28 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
33 ssid = wpa_supplicant_get_ssid(wpa_s);
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);
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);
51 static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
52 enum wps_request_type *req_type)
54 struct wpa_ssid *ssid;
57 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
58 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
62 *req_type = wpas_wps_get_req_type(ssid);
63 if (!ssid->eap.phase1)
66 if (os_strstr(ssid->eap.phase1, "pbc=1"))
71 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
72 !wpa_s->conf->p2p_disabled) {
73 wpa_s->wps->dev.p2p = 1;
76 *req_type = WPS_REQ_ENROLLEE_INFO;
79 #endif /* CONFIG_P2P */
83 #endif /* CONFIG_WPS */
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
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
95 int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
97 struct wpa_ssid *ssid = wpa_s->conf->ssid;
98 int count = 0, disabled = 0;
100 if (!wpas_network_disabled(wpa_s, ssid))
106 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
107 wpa_s->conf->auto_interworking)
109 if (count == 0 && disabled > 0) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
111 "networks)", disabled);
117 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
118 struct wpa_ssid *ssid)
121 if (!wpas_network_disabled(wpa_s, ssid))
126 /* ap_scan=2 mode - try to associate with each SSID. */
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);
135 /* Continue from the next SSID on the next attempt. */
136 wpa_s->prev_scan_ssid = ssid;
138 /* Start from the beginning of the SSID list. */
139 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
141 wpa_supplicant_associate(wpa_s, NULL, ssid);
145 static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
147 struct wpa_supplicant *wpa_s = work->wpa_s;
148 struct wpa_driver_scan_params *params = work->ctx;
152 if (!work->started) {
153 wpa_scan_free_params(params);
156 wpa_supplicant_notify_scanning(wpa_s, 0);
157 wpas_notify_scan_done(wpa_s, 0);
158 wpa_s->scan_work = NULL;
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);
169 wpa_supplicant_notify_scanning(wpa_s, 1);
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;
176 ret = wpa_drv_scan(wpa_s, params);
177 wpa_scan_free_params(params);
180 int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
182 if (wpa_s->disconnected)
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);
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);
202 os_get_reltime(&wpa_s->scan_trigger_time);
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;
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
217 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
218 struct wpa_driver_scan_params *params)
220 struct wpa_driver_scan_params *ctx;
222 if (wpa_s->scan_work) {
223 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
227 ctx = wpa_scan_clone_params(params);
231 if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
233 wpa_scan_free_params(ctx);
242 wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
244 struct wpa_supplicant *wpa_s = eloop_ctx;
246 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
248 if (wpa_supplicant_req_sched_scan(wpa_s))
249 wpa_supplicant_req_scan(wpa_s, 0, 0);
254 wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
256 struct wpa_supplicant *wpa_s = eloop_ctx;
258 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
260 wpa_s->sched_scan_timed_out = 1;
261 wpa_supplicant_cancel_sched_scan(wpa_s);
265 int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
266 struct wpa_driver_scan_params *params,
271 wpa_supplicant_notify_scanning(wpa_s, 1);
272 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
274 wpa_supplicant_notify_scanning(wpa_s, 0);
276 wpa_s->sched_scanning = 1;
282 int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
286 ret = wpa_drv_stop_sched_scan(wpa_s);
288 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
289 /* TODO: what to do if stopping fails? */
297 static struct wpa_driver_scan_filter *
298 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
300 struct wpa_driver_scan_filter *ssids;
301 struct wpa_ssid *ssid;
305 if (!conf->filter_ssids)
308 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
309 if (ssid->ssid && ssid->ssid_len)
314 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
318 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
319 if (!ssid->ssid || !ssid->ssid_len)
321 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
322 ssids[*num_ssids].ssid_len = ssid->ssid_len;
330 static void wpa_supplicant_optimize_freqs(
331 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
334 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
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));
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 "
349 int_array_concat(¶ms->freqs,
350 wpa_s->go_params->freq_list);
352 int_array_sort_unique(params->freqs);
354 wpa_s->p2p_in_provisioning++;
357 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
359 * Optimize scan based on GO information during persistent
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));
368 params->freqs[0] = wpa_s->p2p_invite_go_freq;
370 wpa_s->p2p_in_invitation++;
371 if (wpa_s->p2p_in_invitation > 20) {
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
378 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
379 wpa_s->p2p_in_invitation = 0;
382 #endif /* CONFIG_P2P */
385 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
387 * Optimize post-provisioning scan based on channel used
388 * during provisioning.
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));
394 params->freqs[0] = wpa_s->wps_freq;
396 } else if (wpa_s->after_wps)
399 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
401 /* Optimize provisioning scan based on already known channel */
402 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
404 params->freqs = os_calloc(2, sizeof(int));
406 params->freqs[0] = wpa_s->wps_freq;
407 wpa_s->known_wps_freq = 0; /* only do this once */
409 #endif /* CONFIG_WPS */
413 #ifdef CONFIG_INTERWORKING
414 static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
417 if (wpa_s->conf->interworking == 0)
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);
428 wpabuf_put_u8(buf, 0x40); /* Bit 46 - WNM-Notification */
429 #else /* CONFIG_HS20 */
430 wpabuf_put_u8(buf, 0x00);
431 #endif /* CONFIG_HS20 */
433 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
434 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
436 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
438 if (!is_zero_ether_addr(wpa_s->conf->hessid))
439 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
441 #endif /* CONFIG_INTERWORKING */
444 static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
446 struct wpabuf *extra_ie = NULL;
449 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
450 #endif /* CONFIG_WPS */
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 */
459 wps = wpas_wps_in_use(wpa_s, &req_type);
462 struct wpabuf *wps_ie;
463 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
466 wpa_s->wps->uuid, req_type,
469 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
470 wpabuf_put_buf(extra_ie, wps_ie);
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);
481 #endif /* CONFIG_P2P */
483 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
485 #endif /* CONFIG_WPS */
488 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
489 wpas_hs20_add_indication(extra_ie, -1);
490 #endif /* CONFIG_HS20 */
499 * Check whether there are any enabled networks or credentials that could be
500 * used for a non-P2P connection.
502 static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
504 struct wpa_ssid *ssid;
506 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
507 if (wpas_network_disabled(wpa_s, ssid))
509 if (!ssid->p2p_group)
513 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
514 wpa_s->conf->auto_interworking)
520 #endif /* CONFIG_P2P */
523 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
525 enum hostapd_hw_mode mode)
529 for (i = 0; i < num_modes; i++) {
530 if (modes[i].mode == mode)
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)
542 /* Include only supported channels for the specified band */
543 struct hostapd_hw_modes *mode;
546 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
548 /* No channels supported in this band - use empty list */
549 params->freqs = os_zalloc(sizeof(int));
553 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
554 if (params->freqs == NULL)
556 for (count = 0, i = 0; i < mode->num_channels; i++) {
557 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
559 params->freqs[count++] = mode->channels[i].freq;
564 static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
565 struct wpa_driver_scan_params *params)
567 if (wpa_s->hw.modes == NULL)
568 return; /* unknown what channels the driver supports */
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,
574 else if (wpa_s->setband == WPA_SETBAND_2G)
575 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
580 static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
581 struct wpa_driver_scan_params *params,
585 struct wpa_ssid *ssid;
587 for (i = 0; i < wpa_s->scan_id_count; i++) {
590 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
591 if (!ssid || !ssid->scan_ssid)
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)
601 if (j < params->num_ssids)
602 continue; /* already in the list */
604 if (params->num_ssids + 1 > max_ssids) {
605 wpa_printf(MSG_DEBUG,
606 "Over max scan SSIDs for manual request");
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;
617 wpa_s->scan_id_count = 0;
621 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
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;
631 if (wpa_s->pno || wpa_s->pno_sched_pending) {
632 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
636 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
637 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
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);
647 if (wpa_s->scanning) {
649 * If we are already in scanning state, we shall reschedule the
650 * the incoming scan request.
652 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
653 wpa_supplicant_req_scan(wpa_s, 1, 0);
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);
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);
672 if (wpa_s->conf->ap_scan == 0) {
673 wpa_supplicant_gen_assoc_event(wpa_s);
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);
684 if (wpa_s->conf->ap_scan == 2)
687 max_ssids = wpa_s->max_scan_ssids;
688 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
689 max_ssids = WPAS_MAX_SCAN_SSIDS;
692 wpa_s->last_scan_req = wpa_s->scan_req;
693 wpa_s->scan_req = NORMAL_SCAN_REQ;
695 os_memset(¶ms, 0, sizeof(params));
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);
703 * If autoscan has set its own scanning parameters
705 if (wpa_s->autoscan_params != NULL) {
706 scan_params = wpa_s->autoscan_params;
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)
716 wpa_s->connect_without_scan = NULL;
718 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
719 "without scan step");
720 wpa_supplicant_associate(wpa_s, NULL, ssid);
726 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
727 wpa_s->go_params && !wpa_s->conf->passive_scan) {
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;
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;
745 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
749 #endif /* CONFIG_P2P */
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) {
755 if (ssid == wpa_s->prev_scan_ssid) {
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);
769 } else if (wpa_s->conf->ap_scan == 2) {
771 * User-initiated scan request in ap_scan == 2; scan with
775 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
777 * Perform single-channel single-SSID scan for
778 * reassociate-to-same-BSS operation.
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;
789 * Allocate memory for frequency array, allocate one extra
790 * slot for the zero-terminator.
792 params.freqs = os_malloc(sizeof(int) * 2);
793 if (params.freqs == NULL) {
794 wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
797 params.freqs[0] = wpa_s->assoc_freq;
801 * Reset the reattach flag so that we fall back to full scan if
806 struct wpa_ssid *start = ssid, *tssid;
808 if (ssid == NULL && max_ssids > 1)
809 ssid = wpa_s->conf->ssid;
811 if (!wpas_network_disabled(wpa_s, ssid) &&
813 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
814 ssid->ssid, ssid->ssid_len);
815 params.ssids[params.num_ssids].ssid =
817 params.ssids[params.num_ssids].ssid_len =
820 if (params.num_ssids + 1 >= max_ssids)
826 if (ssid == NULL && max_ssids > 1 &&
827 start != wpa_s->conf->ssid)
828 ssid = wpa_s->conf->ssid;
831 if (wpa_s->scan_id_count &&
832 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
833 wpa_set_scan_ssids(wpa_s, ¶ms, max_ssids);
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))
840 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
841 int_array_concat(¶ms.freqs,
844 os_free(params.freqs);
849 int_array_sort_unique(params.freqs);
852 if (ssid && max_ssids == 1) {
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.
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)");
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));
874 wpa_s->prev_scan_ssid = ssid;
875 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
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 if (wpa_s->conf->passive_scan) {
882 wpa_dbg(wpa_s, MSG_DEBUG,
883 "Use passive scan based on configuration");
885 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
887 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
892 #endif /* CONFIG_P2P */
894 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
895 extra_ie = wpa_supplicant_extra_ies(wpa_s);
897 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
898 wpa_s->manual_scan_only_new) {
899 wpa_printf(MSG_DEBUG,
900 "Request driver to clear scan cache due to manual only_new=1 scan");
901 params.only_new_results = 1;
904 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
905 wpa_s->manual_scan_freqs) {
906 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
907 params.freqs = wpa_s->manual_scan_freqs;
908 wpa_s->manual_scan_freqs = NULL;
911 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
912 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
913 "generated frequency list");
914 params.freqs = wpa_s->next_scan_freqs;
916 os_free(wpa_s->next_scan_freqs);
917 wpa_s->next_scan_freqs = NULL;
918 wpa_setband_scan_freqs(wpa_s, ¶ms);
920 /* See if user specified frequencies. If so, scan only those. */
921 if (wpa_s->conf->freq_list && !params.freqs) {
922 wpa_dbg(wpa_s, MSG_DEBUG,
923 "Optimize scan based on conf->freq_list");
924 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
927 /* Use current associated channel? */
928 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
929 unsigned int num = wpa_s->num_multichan_concurrent;
931 params.freqs = os_calloc(num + 1, sizeof(int));
933 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
935 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
936 "current operating channels since "
937 "scan_cur_freq is enabled");
939 os_free(params.freqs);
945 params.filter_ssids = wpa_supplicant_build_filter_ssids(
946 wpa_s->conf, ¶ms.num_filter_ssids);
948 params.extra_ies = wpabuf_head(extra_ie);
949 params.extra_ies_len = wpabuf_len(extra_ie);
953 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
954 (wpa_s->show_group_started && wpa_s->go_params)) {
956 * The interface may not yet be in P2P mode, so we have to
957 * explicitly request P2P probe to disable CCK rates.
959 params.p2p_probe = 1;
961 #endif /* CONFIG_P2P */
963 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
964 params.mac_addr_rand = 1;
965 if (wpa_s->mac_addr_scan) {
966 params.mac_addr = wpa_s->mac_addr_scan;
967 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
971 scan_params = ¶ms;
976 * If the driver does not support multi-channel concurrency and a
977 * virtual interface that shares the same radio with the wpa_s interface
978 * is operating there may not be need to scan other channels apart from
979 * the current operating channel on the other virtual interface. Filter
980 * out other channels in case we are trying to find a connection for a
981 * station interface when we are not configured to prefer station
982 * connection and a concurrent operation is already in process.
984 if (wpa_s->scan_for_connection &&
985 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
986 !scan_params->freqs && !params.freqs &&
987 wpas_is_p2p_prioritized(wpa_s) &&
988 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
989 non_p2p_network_enabled(wpa_s)) {
990 unsigned int num = wpa_s->num_multichan_concurrent;
992 params.freqs = os_calloc(num + 1, sizeof(int));
994 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
995 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
996 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
998 os_free(params.freqs);
1003 #endif /* CONFIG_P2P */
1005 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
1007 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1008 !wpa_s->manual_scan_freqs) {
1009 /* Restore manual_scan_freqs for the next attempt */
1010 wpa_s->manual_scan_freqs = params.freqs;
1011 params.freqs = NULL;
1014 wpabuf_free(extra_ie);
1015 os_free(params.freqs);
1016 os_free(params.filter_ssids);
1019 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1020 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1021 wpa_supplicant_set_state(wpa_s,
1022 wpa_s->scan_prev_wpa_state);
1023 /* Restore scan_req since we will try to scan again */
1024 wpa_s->scan_req = wpa_s->last_scan_req;
1025 wpa_supplicant_req_scan(wpa_s, 1, 0);
1027 wpa_s->scan_for_connection = 0;
1028 #ifdef CONFIG_INTERWORKING
1029 wpa_s->interworking_fast_assoc_tried = 0;
1030 #endif /* CONFIG_INTERWORKING */
1035 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1037 struct os_reltime remaining, new_int;
1040 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1045 if (cancelled && os_reltime_before(&remaining, &new_int)) {
1046 new_int.sec = remaining.sec;
1047 new_int.usec = remaining.usec;
1051 eloop_register_timeout(new_int.sec, new_int.usec,
1052 wpa_supplicant_scan, wpa_s, NULL);
1054 wpa_s->scan_interval = sec;
1059 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1060 * @wpa_s: Pointer to wpa_supplicant data
1061 * @sec: Number of seconds after which to scan
1062 * @usec: Number of microseconds after which to scan
1064 * This function is used to schedule a scan for neighboring access points after
1065 * the specified time.
1067 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1069 int res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1072 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1074 } else if (res == 0) {
1075 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1078 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1080 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1086 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1087 * @wpa_s: Pointer to wpa_supplicant data
1088 * @sec: Number of seconds after which to scan
1089 * @usec: Number of microseconds after which to scan
1090 * Returns: 0 on success or -1 otherwise
1092 * This function is used to schedule periodic scans for neighboring
1093 * access points after the specified time.
1095 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1098 if (!wpa_s->sched_scan_supported)
1101 eloop_register_timeout(sec, usec,
1102 wpa_supplicant_delayed_sched_scan_timeout,
1110 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1111 * @wpa_s: Pointer to wpa_supplicant data
1112 * Returns: 0 is sched_scan was started or -1 otherwise
1114 * This function is used to schedule periodic scans for neighboring
1115 * access points repeating the scan continuously.
1117 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1119 struct wpa_driver_scan_params params;
1120 struct wpa_driver_scan_params *scan_params;
1121 enum wpa_states prev_state;
1122 struct wpa_ssid *ssid = NULL;
1123 struct wpabuf *extra_ie = NULL;
1125 unsigned int max_sched_scan_ssids;
1129 if (!wpa_s->sched_scan_supported)
1132 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1133 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1135 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1136 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1139 if (wpa_s->sched_scanning) {
1140 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1145 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1146 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1147 /* Use wildcard SSID to find this network */
1149 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1154 if (!wpas_network_disabled(wpa_s, ssid) &&
1155 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1157 * Normal scan is more reliable and faster for WPS
1158 * operations and since these are for short periods of
1159 * time, the benefit of trying to use sched_scan would
1162 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1163 "sched_scan for WPS");
1166 #endif /* CONFIG_WPS */
1171 if (wpa_s->normal_scans < 3 &&
1172 (need_ssids <= wpa_s->max_scan_ssids ||
1173 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1175 * When normal scan can speed up operations, use that for the
1176 * first operations before starting the sched_scan to allow
1177 * user space sleep more. We do this only if the normal scan
1178 * has functionality that is suitable for this or if the
1179 * sched_scan does not have better support for multiple SSIDs.
1181 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1182 "sched_scan for initial scans (normal_scans=%d)",
1183 wpa_s->normal_scans);
1187 os_memset(¶ms, 0, sizeof(params));
1189 /* If we can't allocate space for the filters, we just don't filter */
1190 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1191 sizeof(struct wpa_driver_scan_filter));
1193 prev_state = wpa_s->wpa_state;
1194 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1195 wpa_s->wpa_state == WPA_INACTIVE)
1196 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1198 if (wpa_s->autoscan_params != NULL) {
1199 scan_params = wpa_s->autoscan_params;
1203 /* Find the starting point from which to continue scanning */
1204 ssid = wpa_s->conf->ssid;
1205 if (wpa_s->prev_sched_ssid) {
1207 if (ssid == wpa_s->prev_sched_ssid) {
1215 if (!ssid || !wpa_s->prev_sched_ssid) {
1216 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1217 if (wpa_s->conf->sched_scan_interval)
1218 wpa_s->sched_scan_interval =
1219 wpa_s->conf->sched_scan_interval;
1220 if (wpa_s->sched_scan_interval == 0)
1221 wpa_s->sched_scan_interval = 10;
1222 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1223 wpa_s->first_sched_scan = 1;
1224 ssid = wpa_s->conf->ssid;
1225 wpa_s->prev_sched_ssid = ssid;
1229 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1234 if (wpas_network_disabled(wpa_s, ssid))
1237 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1238 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1239 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1240 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1241 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1242 ssid->ssid, ssid->ssid_len);
1243 params.filter_ssids[params.num_filter_ssids].ssid_len =
1245 params.num_filter_ssids++;
1246 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1248 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1249 "filter for sched_scan - drop filter");
1250 os_free(params.filter_ssids);
1251 params.filter_ssids = NULL;
1252 params.num_filter_ssids = 0;
1255 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1256 if (params.num_ssids == max_sched_scan_ssids)
1257 break; /* only room for broadcast SSID */
1258 wpa_dbg(wpa_s, MSG_DEBUG,
1259 "add to active scan ssid: %s",
1260 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1261 params.ssids[params.num_ssids].ssid =
1263 params.ssids[params.num_ssids].ssid_len =
1266 if (params.num_ssids >= max_sched_scan_ssids) {
1267 wpa_s->prev_sched_ssid = ssid;
1271 (wpas_network_disabled(wpa_s, ssid) ||
1278 wpa_s->prev_sched_ssid = ssid;
1282 if (params.num_filter_ssids == 0) {
1283 os_free(params.filter_ssids);
1284 params.filter_ssids = NULL;
1287 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1289 params.extra_ies = wpabuf_head(extra_ie);
1290 params.extra_ies_len = wpabuf_len(extra_ie);
1293 if (wpa_s->conf->filter_rssi)
1294 params.filter_rssi = wpa_s->conf->filter_rssi;
1296 /* See if user specified frequencies. If so, scan only those. */
1297 if (wpa_s->conf->freq_list && !params.freqs) {
1298 wpa_dbg(wpa_s, MSG_DEBUG,
1299 "Optimize scan based on conf->freq_list");
1300 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
1303 scan_params = ¶ms;
1306 if (ssid || !wpa_s->first_sched_scan) {
1307 wpa_dbg(wpa_s, MSG_DEBUG,
1308 "Starting sched scan: interval %d timeout %d",
1309 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1311 wpa_dbg(wpa_s, MSG_DEBUG,
1312 "Starting sched scan: interval %d (no timeout)",
1313 wpa_s->sched_scan_interval);
1316 wpa_setband_scan_freqs(wpa_s, scan_params);
1318 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1319 params.mac_addr_rand = 1;
1320 if (wpa_s->mac_addr_sched_scan) {
1321 params.mac_addr = wpa_s->mac_addr_sched_scan;
1322 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1327 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1328 wpa_s->sched_scan_interval);
1329 wpabuf_free(extra_ie);
1330 os_free(params.filter_ssids);
1332 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1333 if (prev_state != wpa_s->wpa_state)
1334 wpa_supplicant_set_state(wpa_s, prev_state);
1338 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1339 if (ssid || !wpa_s->first_sched_scan) {
1340 wpa_s->sched_scan_timed_out = 0;
1341 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1342 wpa_supplicant_sched_scan_timeout,
1344 wpa_s->first_sched_scan = 0;
1345 wpa_s->sched_scan_timeout /= 2;
1346 wpa_s->sched_scan_interval *= 2;
1347 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1348 wpa_s->sched_scan_interval = 10;
1349 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1353 /* If there is no more ssids, start next time from the beginning */
1355 wpa_s->prev_sched_ssid = NULL;
1362 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1363 * @wpa_s: Pointer to wpa_supplicant data
1365 * This function is used to cancel a scan request scheduled with
1366 * wpa_supplicant_req_scan().
1368 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1370 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1371 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1376 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1377 * @wpa_s: Pointer to wpa_supplicant data
1379 * This function is used to stop a delayed scheduled scan.
1381 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1383 if (!wpa_s->sched_scan_supported)
1386 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1387 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1393 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1394 * @wpa_s: Pointer to wpa_supplicant data
1396 * This function is used to stop a periodic scheduled scan.
1398 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1400 if (!wpa_s->sched_scanning)
1403 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1404 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1405 wpa_supplicant_stop_sched_scan(wpa_s);
1410 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1411 * @wpa_s: Pointer to wpa_supplicant data
1412 * @scanning: Whether scanning is currently in progress
1414 * This function is to generate scanning notifycations. It is called whenever
1415 * there may have been a change in scanning (scan started, completed, stopped).
1416 * wpas_notify_scanning() is called whenever the scanning state changed from the
1417 * previously notified state.
1419 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1422 if (wpa_s->scanning != scanning) {
1423 wpa_s->scanning = scanning;
1424 wpas_notify_scanning(wpa_s);
1429 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1435 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1436 for (i = 0; ie && i < ie[1]; i++) {
1437 if ((ie[i + 2] & 0x7f) > rate)
1438 rate = ie[i + 2] & 0x7f;
1441 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1442 for (i = 0; ie && i < ie[1]; i++) {
1443 if ((ie[i + 2] & 0x7f) > rate)
1444 rate = ie[i + 2] & 0x7f;
1452 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1453 * @res: Scan result entry
1454 * @ie: Information element identitifier (WLAN_EID_*)
1455 * Returns: Pointer to the information element (id field) or %NULL if not found
1457 * This function returns the first matching information element in the scan
1460 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1462 const u8 *end, *pos;
1464 pos = (const u8 *) (res + 1);
1465 end = pos + res->ie_len;
1467 while (pos + 1 < end) {
1468 if (pos + 2 + pos[1] > end)
1480 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1481 * @res: Scan result entry
1482 * @vendor_type: Vendor type (four octets starting the IE payload)
1483 * Returns: Pointer to the information element (id field) or %NULL if not found
1485 * This function returns the first matching information element in the scan
1488 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1491 const u8 *end, *pos;
1493 pos = (const u8 *) (res + 1);
1494 end = pos + res->ie_len;
1496 while (pos + 1 < end) {
1497 if (pos + 2 + pos[1] > end)
1499 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1500 vendor_type == WPA_GET_BE32(&pos[2]))
1510 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1511 * @res: Scan result entry
1512 * @vendor_type: Vendor type (four octets starting the IE payload)
1513 * Returns: Pointer to the information element (id field) or %NULL if not found
1515 * This function returns the first matching information element in the scan
1518 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1519 * from Beacon frames instead of either Beacon or Probe Response frames.
1521 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1524 const u8 *end, *pos;
1526 if (res->beacon_ie_len == 0)
1529 pos = (const u8 *) (res + 1);
1531 end = pos + res->beacon_ie_len;
1533 while (pos + 1 < end) {
1534 if (pos + 2 + pos[1] > end)
1536 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1537 vendor_type == WPA_GET_BE32(&pos[2]))
1547 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1548 * @res: Scan result entry
1549 * @vendor_type: Vendor type (four octets starting the IE payload)
1550 * Returns: Pointer to the information element payload or %NULL if not found
1552 * This function returns concatenated payload of possibly fragmented vendor
1553 * specific information elements in the scan result. The caller is responsible
1554 * for freeing the returned buffer.
1556 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1560 const u8 *end, *pos;
1562 buf = wpabuf_alloc(res->ie_len);
1566 pos = (const u8 *) (res + 1);
1567 end = pos + res->ie_len;
1569 while (pos + 1 < end) {
1570 if (pos + 2 + pos[1] > end)
1572 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1573 vendor_type == WPA_GET_BE32(&pos[2]))
1574 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1578 if (wpabuf_len(buf) == 0) {
1588 * Channels with a great SNR can operate at full rate. What is a great SNR?
1589 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1590 * rule of thumb is that any SNR above 20 is good." This one
1591 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1592 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1593 * conservative value.
1595 #define GREAT_SNR 30
1597 /* Compare function for sorting scan results. Return >0 if @b is considered
1599 static int wpa_scan_result_compar(const void *a, const void *b)
1601 #define IS_5GHZ(n) (n > 4000)
1602 #define MIN(a,b) a < b ? a : b
1603 struct wpa_scan_res **_wa = (void *) a;
1604 struct wpa_scan_res **_wb = (void *) b;
1605 struct wpa_scan_res *wa = *_wa;
1606 struct wpa_scan_res *wb = *_wb;
1607 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1610 /* WPA/WPA2 support preferred */
1611 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1612 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1613 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1614 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1616 if (wpa_b && !wpa_a)
1618 if (!wpa_b && wpa_a)
1621 /* privacy support preferred */
1622 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1623 (wb->caps & IEEE80211_CAP_PRIVACY))
1625 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1626 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1629 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1630 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1631 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1632 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1634 /* Not suitable information to calculate SNR, so use level */
1639 /* best/max rate preferred if SNR close enough */
1640 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1641 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1642 maxrate_a = wpa_scan_get_max_rate(wa);
1643 maxrate_b = wpa_scan_get_max_rate(wb);
1644 if (maxrate_a != maxrate_b)
1645 return maxrate_b - maxrate_a;
1646 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1647 return IS_5GHZ(wa->freq) ? -1 : 1;
1650 /* use freq for channel preference */
1652 /* all things being equal, use SNR; if SNRs are
1653 * identical, use quality values since some drivers may only report
1654 * that value and leave the signal level zero */
1656 return wb->qual - wa->qual;
1657 return snr_b - snr_a;
1664 /* Compare function for sorting scan results when searching a WPS AP for
1665 * provisioning. Return >0 if @b is considered better. */
1666 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1668 struct wpa_scan_res **_wa = (void *) a;
1669 struct wpa_scan_res **_wb = (void *) b;
1670 struct wpa_scan_res *wa = *_wa;
1671 struct wpa_scan_res *wb = *_wb;
1672 int uses_wps_a, uses_wps_b;
1673 struct wpabuf *wps_a, *wps_b;
1676 /* Optimization - check WPS IE existence before allocated memory and
1677 * doing full reassembly. */
1678 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1679 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1680 if (uses_wps_a && !uses_wps_b)
1682 if (!uses_wps_a && uses_wps_b)
1685 if (uses_wps_a && uses_wps_b) {
1686 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1687 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1688 res = wps_ap_priority_compar(wps_a, wps_b);
1696 * Do not use current AP security policy as a sorting criteria during
1697 * WPS provisioning step since the AP may get reconfigured at the
1698 * completion of provisioning.
1701 /* all things being equal, use signal level; if signal levels are
1702 * identical, use quality values since some drivers may only report
1703 * that value and leave the signal level zero */
1704 if (wb->level == wa->level)
1705 return wb->qual - wa->qual;
1706 return wb->level - wa->level;
1708 #endif /* CONFIG_WPS */
1711 static void dump_scan_res(struct wpa_scan_results *scan_res)
1713 #ifndef CONFIG_NO_STDOUT_DEBUG
1716 if (scan_res->res == NULL || scan_res->num == 0)
1719 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1721 for (i = 0; i < scan_res->num; i++) {
1722 struct wpa_scan_res *r = scan_res->res[i];
1724 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1725 == WPA_SCAN_LEVEL_DBM) {
1726 int snr = r->level - r->noise;
1727 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1728 "noise=%d level=%d snr=%d%s flags=0x%x "
1730 MAC2STR(r->bssid), r->freq, r->qual,
1731 r->noise, r->level, snr,
1732 snr >= GREAT_SNR ? "*" : "", r->flags,
1735 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1736 "noise=%d level=%d flags=0x%x age=%u",
1737 MAC2STR(r->bssid), r->freq, r->qual,
1738 r->noise, r->level, r->flags, r->age);
1740 pos = (u8 *) (r + 1);
1742 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1744 if (r->beacon_ie_len)
1745 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1746 pos, r->beacon_ie_len);
1748 #endif /* CONFIG_NO_STDOUT_DEBUG */
1753 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1754 * @wpa_s: Pointer to wpa_supplicant data
1755 * @bssid: BSSID to check
1756 * Returns: 0 if the BSSID is filtered or 1 if not
1758 * This function is used to filter out specific BSSIDs from scan reslts mainly
1759 * for testing purposes (SET bssid_filter ctrl_iface command).
1761 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1766 if (wpa_s->bssid_filter == NULL)
1769 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1770 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1779 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1780 struct wpa_scan_results *res)
1784 if (wpa_s->bssid_filter == NULL)
1787 for (i = 0, j = 0; i < res->num; i++) {
1788 if (wpa_supplicant_filter_bssid_match(wpa_s,
1789 res->res[i]->bssid)) {
1790 res->res[j++] = res->res[i];
1792 os_free(res->res[i]);
1797 if (res->num != j) {
1798 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1799 (int) (res->num - j));
1806 * wpa_supplicant_get_scan_results - Get scan results
1807 * @wpa_s: Pointer to wpa_supplicant data
1808 * @info: Information about what was scanned or %NULL if not available
1809 * @new_scan: Whether a new scan was performed
1810 * Returns: Scan results, %NULL on failure
1812 * This function request the current scan results from the driver and updates
1813 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1814 * results with wpa_scan_results_free().
1816 struct wpa_scan_results *
1817 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1818 struct scan_info *info, int new_scan)
1820 struct wpa_scan_results *scan_res;
1822 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1824 scan_res = wpa_drv_get_scan_results2(wpa_s);
1825 if (scan_res == NULL) {
1826 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1829 if (scan_res->fetch_time.sec == 0) {
1831 * Make sure we have a valid timestamp if the driver wrapper
1832 * does not set this.
1834 os_get_reltime(&scan_res->fetch_time);
1836 filter_scan_res(wpa_s, scan_res);
1839 if (wpas_wps_searching(wpa_s)) {
1840 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1841 "provisioning rules");
1842 compar = wpa_scan_result_wps_compar;
1844 #endif /* CONFIG_WPS */
1846 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1848 dump_scan_res(scan_res);
1850 wpa_bss_update_start(wpa_s);
1851 for (i = 0; i < scan_res->num; i++)
1852 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1853 &scan_res->fetch_time);
1854 wpa_bss_update_end(wpa_s, info, new_scan);
1861 * wpa_supplicant_update_scan_results - Update scan results from the driver
1862 * @wpa_s: Pointer to wpa_supplicant data
1863 * Returns: 0 on success, -1 on failure
1865 * This function updates the BSS table within wpa_supplicant based on the
1866 * currently available scan results from the driver without requesting a new
1867 * scan. This is used in cases where the driver indicates an association
1868 * (including roaming within ESS) and wpa_supplicant does not yet have the
1869 * needed information to complete the connection (e.g., to perform validation
1870 * steps in 4-way handshake).
1872 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1874 struct wpa_scan_results *scan_res;
1875 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1876 if (scan_res == NULL)
1878 wpa_scan_results_free(scan_res);
1885 * scan_only_handler - Reports scan results
1887 void scan_only_handler(struct wpa_supplicant *wpa_s,
1888 struct wpa_scan_results *scan_res)
1890 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
1891 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1892 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1893 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1894 wpa_s->manual_scan_id);
1895 wpa_s->manual_scan_use_id = 0;
1897 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1899 wpas_notify_scan_results(wpa_s);
1900 wpas_notify_scan_done(wpa_s, 1);
1901 if (wpa_s->scan_work) {
1902 struct wpa_radio_work *work = wpa_s->scan_work;
1903 wpa_s->scan_work = NULL;
1904 radio_work_done(work);
1909 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1911 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1915 struct wpa_driver_scan_params *
1916 wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
1918 struct wpa_driver_scan_params *params;
1922 params = os_zalloc(sizeof(*params));
1926 for (i = 0; i < src->num_ssids; i++) {
1927 if (src->ssids[i].ssid) {
1928 n = os_malloc(src->ssids[i].ssid_len);
1931 os_memcpy(n, src->ssids[i].ssid,
1932 src->ssids[i].ssid_len);
1933 params->ssids[i].ssid = n;
1934 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
1937 params->num_ssids = src->num_ssids;
1939 if (src->extra_ies) {
1940 n = os_malloc(src->extra_ies_len);
1943 os_memcpy(n, src->extra_ies, src->extra_ies_len);
1944 params->extra_ies = n;
1945 params->extra_ies_len = src->extra_ies_len;
1949 int len = int_array_len(src->freqs);
1950 params->freqs = os_malloc((len + 1) * sizeof(int));
1951 if (params->freqs == NULL)
1953 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
1956 if (src->filter_ssids) {
1957 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
1958 src->num_filter_ssids);
1959 if (params->filter_ssids == NULL)
1961 os_memcpy(params->filter_ssids, src->filter_ssids,
1962 sizeof(*params->filter_ssids) *
1963 src->num_filter_ssids);
1964 params->num_filter_ssids = src->num_filter_ssids;
1967 params->filter_rssi = src->filter_rssi;
1968 params->p2p_probe = src->p2p_probe;
1969 params->only_new_results = src->only_new_results;
1970 params->low_priority = src->low_priority;
1972 if (src->mac_addr_rand) {
1973 params->mac_addr_rand = src->mac_addr_rand;
1975 if (src->mac_addr && src->mac_addr_mask) {
1978 mac_addr = os_malloc(2 * ETH_ALEN);
1982 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
1983 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
1985 params->mac_addr = mac_addr;
1986 params->mac_addr_mask = mac_addr + ETH_ALEN;
1992 wpa_scan_free_params(params);
1997 void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2004 for (i = 0; i < params->num_ssids; i++)
2005 os_free((u8 *) params->ssids[i].ssid);
2006 os_free((u8 *) params->extra_ies);
2007 os_free(params->freqs);
2008 os_free(params->filter_ssids);
2011 * Note: params->mac_addr_mask points to same memory allocation and
2012 * must not be freed separately.
2014 os_free((u8 *) params->mac_addr);
2020 int wpas_start_pno(struct wpa_supplicant *wpa_s)
2023 size_t i, num_ssid, num_match_ssid;
2024 struct wpa_ssid *ssid;
2025 struct wpa_driver_scan_params params;
2027 if (!wpa_s->sched_scan_supported)
2030 if (wpa_s->pno || wpa_s->pno_sched_pending)
2033 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2034 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2035 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2039 if (wpa_s->wpa_state == WPA_SCANNING) {
2040 wpa_supplicant_cancel_scan(wpa_s);
2041 if (wpa_s->sched_scanning) {
2042 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2043 "ongoing sched scan");
2044 wpa_supplicant_cancel_sched_scan(wpa_s);
2045 wpa_s->pno_sched_pending = 1;
2050 os_memset(¶ms, 0, sizeof(params));
2052 num_ssid = num_match_ssid = 0;
2053 ssid = wpa_s->conf->ssid;
2055 if (!wpas_network_disabled(wpa_s, ssid)) {
2057 if (ssid->scan_ssid)
2063 if (num_match_ssid == 0) {
2064 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2068 if (num_match_ssid > num_ssid) {
2069 params.num_ssids++; /* wildcard */
2073 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
2074 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2075 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
2076 num_ssid = WPAS_MAX_SCAN_SSIDS;
2079 if (num_match_ssid > wpa_s->max_match_sets) {
2080 num_match_ssid = wpa_s->max_match_sets;
2081 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
2083 params.filter_ssids = os_calloc(num_match_ssid,
2084 sizeof(struct wpa_driver_scan_filter));
2085 if (params.filter_ssids == NULL)
2088 ssid = wpa_s->conf->ssid;
2090 if (!wpas_network_disabled(wpa_s, ssid)) {
2091 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2092 params.ssids[params.num_ssids].ssid =
2094 params.ssids[params.num_ssids].ssid_len =
2098 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2100 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2101 params.num_filter_ssids++;
2103 if (i == num_match_ssid)
2109 if (wpa_s->conf->filter_rssi)
2110 params.filter_rssi = wpa_s->conf->filter_rssi;
2112 interval = wpa_s->conf->sched_scan_interval ?
2113 wpa_s->conf->sched_scan_interval : 10;
2115 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2116 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2117 params.freqs = wpa_s->manual_sched_scan_freqs;
2120 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2121 params.mac_addr_rand = 1;
2122 if (wpa_s->mac_addr_pno) {
2123 params.mac_addr = wpa_s->mac_addr_pno;
2124 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2128 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms, interval);
2129 os_free(params.filter_ssids);
2133 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2138 int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2145 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2148 wpa_s->pno_sched_pending = 0;
2150 if (wpa_s->wpa_state == WPA_SCANNING)
2151 wpa_supplicant_req_scan(wpa_s, 0, 0);
2157 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2160 type &= MAC_ADDR_RAND_ALL;
2161 wpa_s->mac_addr_rand_enable &= ~type;
2163 if (type & MAC_ADDR_RAND_SCAN) {
2164 os_free(wpa_s->mac_addr_scan);
2165 wpa_s->mac_addr_scan = NULL;
2168 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2169 os_free(wpa_s->mac_addr_sched_scan);
2170 wpa_s->mac_addr_sched_scan = NULL;
2173 if (type & MAC_ADDR_RAND_PNO) {
2174 os_free(wpa_s->mac_addr_pno);
2175 wpa_s->mac_addr_pno = NULL;
2180 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2181 unsigned int type, const u8 *addr,
2186 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2189 tmp = os_malloc(2 * ETH_ALEN);
2192 os_memcpy(tmp, addr, ETH_ALEN);
2193 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2196 if (type == MAC_ADDR_RAND_SCAN) {
2197 wpa_s->mac_addr_scan = tmp;
2198 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2199 wpa_s->mac_addr_sched_scan = tmp;
2200 } else if (type == MAC_ADDR_RAND_PNO) {
2201 wpa_s->mac_addr_pno = tmp;
2203 wpa_printf(MSG_INFO,
2204 "scan: Invalid MAC randomization type=0x%x",
2210 wpa_s->mac_addr_rand_enable |= type;