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) &&
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");
882 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
884 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
889 #endif /* CONFIG_P2P */
891 wpa_supplicant_optimize_freqs(wpa_s, ¶ms);
892 extra_ie = wpa_supplicant_extra_ies(wpa_s);
894 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
895 wpa_s->manual_scan_only_new) {
896 wpa_printf(MSG_DEBUG,
897 "Request driver to clear scan cache due to manual only_new=1 scan");
898 params.only_new_results = 1;
901 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
902 wpa_s->manual_scan_freqs) {
903 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
904 params.freqs = wpa_s->manual_scan_freqs;
905 wpa_s->manual_scan_freqs = NULL;
908 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
909 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
910 "generated frequency list");
911 params.freqs = wpa_s->next_scan_freqs;
913 os_free(wpa_s->next_scan_freqs);
914 wpa_s->next_scan_freqs = NULL;
915 wpa_setband_scan_freqs(wpa_s, ¶ms);
917 /* See if user specified frequencies. If so, scan only those. */
918 if (wpa_s->conf->freq_list && !params.freqs) {
919 wpa_dbg(wpa_s, MSG_DEBUG,
920 "Optimize scan based on conf->freq_list");
921 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
924 /* Use current associated channel? */
925 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
926 unsigned int num = wpa_s->num_multichan_concurrent;
928 params.freqs = os_calloc(num + 1, sizeof(int));
930 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
932 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
933 "current operating channels since "
934 "scan_cur_freq is enabled");
936 os_free(params.freqs);
942 params.filter_ssids = wpa_supplicant_build_filter_ssids(
943 wpa_s->conf, ¶ms.num_filter_ssids);
945 params.extra_ies = wpabuf_head(extra_ie);
946 params.extra_ies_len = wpabuf_len(extra_ie);
950 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
951 (wpa_s->show_group_started && wpa_s->go_params)) {
953 * The interface may not yet be in P2P mode, so we have to
954 * explicitly request P2P probe to disable CCK rates.
956 params.p2p_probe = 1;
958 #endif /* CONFIG_P2P */
960 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
961 params.mac_addr_rand = 1;
962 if (wpa_s->mac_addr_scan) {
963 params.mac_addr = wpa_s->mac_addr_scan;
964 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
968 scan_params = ¶ms;
973 * If the driver does not support multi-channel concurrency and a
974 * virtual interface that shares the same radio with the wpa_s interface
975 * is operating there may not be need to scan other channels apart from
976 * the current operating channel on the other virtual interface. Filter
977 * out other channels in case we are trying to find a connection for a
978 * station interface when we are not configured to prefer station
979 * connection and a concurrent operation is already in process.
981 if (wpa_s->scan_for_connection &&
982 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
983 !scan_params->freqs && !params.freqs &&
984 wpas_is_p2p_prioritized(wpa_s) &&
985 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
986 non_p2p_network_enabled(wpa_s)) {
987 unsigned int num = wpa_s->num_multichan_concurrent;
989 params.freqs = os_calloc(num + 1, sizeof(int));
991 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
992 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
993 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
995 os_free(params.freqs);
1000 #endif /* CONFIG_P2P */
1002 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
1004 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
1005 !wpa_s->manual_scan_freqs) {
1006 /* Restore manual_scan_freqs for the next attempt */
1007 wpa_s->manual_scan_freqs = params.freqs;
1008 params.freqs = NULL;
1011 wpabuf_free(extra_ie);
1012 os_free(params.freqs);
1013 os_free(params.filter_ssids);
1016 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
1017 if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
1018 wpa_supplicant_set_state(wpa_s,
1019 wpa_s->scan_prev_wpa_state);
1020 /* Restore scan_req since we will try to scan again */
1021 wpa_s->scan_req = wpa_s->last_scan_req;
1022 wpa_supplicant_req_scan(wpa_s, 1, 0);
1024 wpa_s->scan_for_connection = 0;
1025 #ifdef CONFIG_INTERWORKING
1026 wpa_s->interworking_fast_assoc_tried = 0;
1027 #endif /* CONFIG_INTERWORKING */
1032 void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1034 struct os_reltime remaining, new_int;
1037 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1042 if (cancelled && os_reltime_before(&remaining, &new_int)) {
1043 new_int.sec = remaining.sec;
1044 new_int.usec = remaining.usec;
1048 eloop_register_timeout(new_int.sec, new_int.usec,
1049 wpa_supplicant_scan, wpa_s, NULL);
1051 wpa_s->scan_interval = sec;
1056 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1057 * @wpa_s: Pointer to wpa_supplicant data
1058 * @sec: Number of seconds after which to scan
1059 * @usec: Number of microseconds after which to scan
1061 * This function is used to schedule a scan for neighboring access points after
1062 * the specified time.
1064 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1066 int res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1069 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
1071 } else if (res == 0) {
1072 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1075 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1077 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
1083 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1084 * @wpa_s: Pointer to wpa_supplicant data
1085 * @sec: Number of seconds after which to scan
1086 * @usec: Number of microseconds after which to scan
1087 * Returns: 0 on success or -1 otherwise
1089 * This function is used to schedule periodic scans for neighboring
1090 * access points after the specified time.
1092 int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1095 if (!wpa_s->sched_scan_supported)
1098 eloop_register_timeout(sec, usec,
1099 wpa_supplicant_delayed_sched_scan_timeout,
1107 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1108 * @wpa_s: Pointer to wpa_supplicant data
1109 * Returns: 0 is sched_scan was started or -1 otherwise
1111 * This function is used to schedule periodic scans for neighboring
1112 * access points repeating the scan continuously.
1114 int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1116 struct wpa_driver_scan_params params;
1117 struct wpa_driver_scan_params *scan_params;
1118 enum wpa_states prev_state;
1119 struct wpa_ssid *ssid = NULL;
1120 struct wpabuf *extra_ie = NULL;
1122 unsigned int max_sched_scan_ssids;
1126 if (!wpa_s->sched_scan_supported)
1129 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1130 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1132 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
1133 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
1136 if (wpa_s->sched_scanning) {
1137 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1142 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1143 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
1144 /* Use wildcard SSID to find this network */
1146 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1151 if (!wpas_network_disabled(wpa_s, ssid) &&
1152 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1154 * Normal scan is more reliable and faster for WPS
1155 * operations and since these are for short periods of
1156 * time, the benefit of trying to use sched_scan would
1159 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1160 "sched_scan for WPS");
1163 #endif /* CONFIG_WPS */
1168 if (wpa_s->normal_scans < 3 &&
1169 (need_ssids <= wpa_s->max_scan_ssids ||
1170 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1172 * When normal scan can speed up operations, use that for the
1173 * first operations before starting the sched_scan to allow
1174 * user space sleep more. We do this only if the normal scan
1175 * has functionality that is suitable for this or if the
1176 * sched_scan does not have better support for multiple SSIDs.
1178 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1179 "sched_scan for initial scans (normal_scans=%d)",
1180 wpa_s->normal_scans);
1184 os_memset(¶ms, 0, sizeof(params));
1186 /* If we can't allocate space for the filters, we just don't filter */
1187 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
1188 sizeof(struct wpa_driver_scan_filter));
1190 prev_state = wpa_s->wpa_state;
1191 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1192 wpa_s->wpa_state == WPA_INACTIVE)
1193 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1195 if (wpa_s->autoscan_params != NULL) {
1196 scan_params = wpa_s->autoscan_params;
1200 /* Find the starting point from which to continue scanning */
1201 ssid = wpa_s->conf->ssid;
1202 if (wpa_s->prev_sched_ssid) {
1204 if (ssid == wpa_s->prev_sched_ssid) {
1212 if (!ssid || !wpa_s->prev_sched_ssid) {
1213 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
1214 if (wpa_s->conf->sched_scan_interval)
1215 wpa_s->sched_scan_interval =
1216 wpa_s->conf->sched_scan_interval;
1217 if (wpa_s->sched_scan_interval == 0)
1218 wpa_s->sched_scan_interval = 10;
1219 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1220 wpa_s->first_sched_scan = 1;
1221 ssid = wpa_s->conf->ssid;
1222 wpa_s->prev_sched_ssid = ssid;
1226 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1231 if (wpas_network_disabled(wpa_s, ssid))
1234 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1235 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1236 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1237 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1238 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1239 ssid->ssid, ssid->ssid_len);
1240 params.filter_ssids[params.num_filter_ssids].ssid_len =
1242 params.num_filter_ssids++;
1243 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1245 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1246 "filter for sched_scan - drop filter");
1247 os_free(params.filter_ssids);
1248 params.filter_ssids = NULL;
1249 params.num_filter_ssids = 0;
1252 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1253 if (params.num_ssids == max_sched_scan_ssids)
1254 break; /* only room for broadcast SSID */
1255 wpa_dbg(wpa_s, MSG_DEBUG,
1256 "add to active scan ssid: %s",
1257 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1258 params.ssids[params.num_ssids].ssid =
1260 params.ssids[params.num_ssids].ssid_len =
1263 if (params.num_ssids >= max_sched_scan_ssids) {
1264 wpa_s->prev_sched_ssid = ssid;
1268 (wpas_network_disabled(wpa_s, ssid) ||
1275 wpa_s->prev_sched_ssid = ssid;
1279 if (params.num_filter_ssids == 0) {
1280 os_free(params.filter_ssids);
1281 params.filter_ssids = NULL;
1284 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1286 params.extra_ies = wpabuf_head(extra_ie);
1287 params.extra_ies_len = wpabuf_len(extra_ie);
1290 if (wpa_s->conf->filter_rssi)
1291 params.filter_rssi = wpa_s->conf->filter_rssi;
1293 /* See if user specified frequencies. If so, scan only those. */
1294 if (wpa_s->conf->freq_list && !params.freqs) {
1295 wpa_dbg(wpa_s, MSG_DEBUG,
1296 "Optimize scan based on conf->freq_list");
1297 int_array_concat(¶ms.freqs, wpa_s->conf->freq_list);
1300 scan_params = ¶ms;
1303 if (ssid || !wpa_s->first_sched_scan) {
1304 wpa_dbg(wpa_s, MSG_DEBUG,
1305 "Starting sched scan: interval %d timeout %d",
1306 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
1308 wpa_dbg(wpa_s, MSG_DEBUG,
1309 "Starting sched scan: interval %d (no timeout)",
1310 wpa_s->sched_scan_interval);
1313 wpa_setband_scan_freqs(wpa_s, scan_params);
1315 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1316 params.mac_addr_rand = 1;
1317 if (wpa_s->mac_addr_sched_scan) {
1318 params.mac_addr = wpa_s->mac_addr_sched_scan;
1319 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1324 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
1325 wpa_s->sched_scan_interval);
1326 wpabuf_free(extra_ie);
1327 os_free(params.filter_ssids);
1329 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1330 if (prev_state != wpa_s->wpa_state)
1331 wpa_supplicant_set_state(wpa_s, prev_state);
1335 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1336 if (ssid || !wpa_s->first_sched_scan) {
1337 wpa_s->sched_scan_timed_out = 0;
1338 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1339 wpa_supplicant_sched_scan_timeout,
1341 wpa_s->first_sched_scan = 0;
1342 wpa_s->sched_scan_timeout /= 2;
1343 wpa_s->sched_scan_interval *= 2;
1344 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1345 wpa_s->sched_scan_interval = 10;
1346 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1350 /* If there is no more ssids, start next time from the beginning */
1352 wpa_s->prev_sched_ssid = NULL;
1359 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1360 * @wpa_s: Pointer to wpa_supplicant data
1362 * This function is used to cancel a scan request scheduled with
1363 * wpa_supplicant_req_scan().
1365 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1367 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1368 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
1373 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1374 * @wpa_s: Pointer to wpa_supplicant data
1376 * This function is used to stop a delayed scheduled scan.
1378 void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1380 if (!wpa_s->sched_scan_supported)
1383 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1384 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1390 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1391 * @wpa_s: Pointer to wpa_supplicant data
1393 * This function is used to stop a periodic scheduled scan.
1395 void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1397 if (!wpa_s->sched_scanning)
1400 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1401 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1402 wpa_supplicant_stop_sched_scan(wpa_s);
1407 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1408 * @wpa_s: Pointer to wpa_supplicant data
1409 * @scanning: Whether scanning is currently in progress
1411 * This function is to generate scanning notifycations. It is called whenever
1412 * there may have been a change in scanning (scan started, completed, stopped).
1413 * wpas_notify_scanning() is called whenever the scanning state changed from the
1414 * previously notified state.
1416 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1419 if (wpa_s->scanning != scanning) {
1420 wpa_s->scanning = scanning;
1421 wpas_notify_scanning(wpa_s);
1426 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1432 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1433 for (i = 0; ie && i < ie[1]; i++) {
1434 if ((ie[i + 2] & 0x7f) > rate)
1435 rate = ie[i + 2] & 0x7f;
1438 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1439 for (i = 0; ie && i < ie[1]; i++) {
1440 if ((ie[i + 2] & 0x7f) > rate)
1441 rate = ie[i + 2] & 0x7f;
1449 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1450 * @res: Scan result entry
1451 * @ie: Information element identitifier (WLAN_EID_*)
1452 * Returns: Pointer to the information element (id field) or %NULL if not found
1454 * This function returns the first matching information element in the scan
1457 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1459 const u8 *end, *pos;
1461 pos = (const u8 *) (res + 1);
1462 end = pos + res->ie_len;
1464 while (pos + 1 < end) {
1465 if (pos + 2 + pos[1] > end)
1477 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1478 * @res: Scan result entry
1479 * @vendor_type: Vendor type (four octets starting the IE payload)
1480 * Returns: Pointer to the information element (id field) or %NULL if not found
1482 * This function returns the first matching information element in the scan
1485 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1488 const u8 *end, *pos;
1490 pos = (const u8 *) (res + 1);
1491 end = pos + res->ie_len;
1493 while (pos + 1 < end) {
1494 if (pos + 2 + pos[1] > end)
1496 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1497 vendor_type == WPA_GET_BE32(&pos[2]))
1507 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1508 * @res: Scan result entry
1509 * @vendor_type: Vendor type (four octets starting the IE payload)
1510 * Returns: Pointer to the information element (id field) or %NULL if not found
1512 * This function returns the first matching information element in the scan
1515 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1516 * from Beacon frames instead of either Beacon or Probe Response frames.
1518 const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1521 const u8 *end, *pos;
1523 if (res->beacon_ie_len == 0)
1526 pos = (const u8 *) (res + 1);
1528 end = pos + res->beacon_ie_len;
1530 while (pos + 1 < end) {
1531 if (pos + 2 + pos[1] > end)
1533 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1534 vendor_type == WPA_GET_BE32(&pos[2]))
1544 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1545 * @res: Scan result entry
1546 * @vendor_type: Vendor type (four octets starting the IE payload)
1547 * Returns: Pointer to the information element payload or %NULL if not found
1549 * This function returns concatenated payload of possibly fragmented vendor
1550 * specific information elements in the scan result. The caller is responsible
1551 * for freeing the returned buffer.
1553 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1557 const u8 *end, *pos;
1559 buf = wpabuf_alloc(res->ie_len);
1563 pos = (const u8 *) (res + 1);
1564 end = pos + res->ie_len;
1566 while (pos + 1 < end) {
1567 if (pos + 2 + pos[1] > end)
1569 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1570 vendor_type == WPA_GET_BE32(&pos[2]))
1571 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1575 if (wpabuf_len(buf) == 0) {
1585 * Channels with a great SNR can operate at full rate. What is a great SNR?
1586 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1587 * rule of thumb is that any SNR above 20 is good." This one
1588 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1589 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1590 * conservative value.
1592 #define GREAT_SNR 30
1594 /* Compare function for sorting scan results. Return >0 if @b is considered
1596 static int wpa_scan_result_compar(const void *a, const void *b)
1598 #define IS_5GHZ(n) (n > 4000)
1599 #define MIN(a,b) a < b ? a : b
1600 struct wpa_scan_res **_wa = (void *) a;
1601 struct wpa_scan_res **_wb = (void *) b;
1602 struct wpa_scan_res *wa = *_wa;
1603 struct wpa_scan_res *wb = *_wb;
1604 int wpa_a, wpa_b, maxrate_a, maxrate_b;
1607 /* WPA/WPA2 support preferred */
1608 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1609 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1610 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1611 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1613 if (wpa_b && !wpa_a)
1615 if (!wpa_b && wpa_a)
1618 /* privacy support preferred */
1619 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1620 (wb->caps & IEEE80211_CAP_PRIVACY))
1622 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1623 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1626 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1627 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1628 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1629 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1631 /* Not suitable information to calculate SNR, so use level */
1636 /* best/max rate preferred if SNR close enough */
1637 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
1638 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1639 maxrate_a = wpa_scan_get_max_rate(wa);
1640 maxrate_b = wpa_scan_get_max_rate(wb);
1641 if (maxrate_a != maxrate_b)
1642 return maxrate_b - maxrate_a;
1643 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1644 return IS_5GHZ(wa->freq) ? -1 : 1;
1647 /* use freq for channel preference */
1649 /* all things being equal, use SNR; if SNRs are
1650 * identical, use quality values since some drivers may only report
1651 * that value and leave the signal level zero */
1653 return wb->qual - wa->qual;
1654 return snr_b - snr_a;
1661 /* Compare function for sorting scan results when searching a WPS AP for
1662 * provisioning. Return >0 if @b is considered better. */
1663 static int wpa_scan_result_wps_compar(const void *a, const void *b)
1665 struct wpa_scan_res **_wa = (void *) a;
1666 struct wpa_scan_res **_wb = (void *) b;
1667 struct wpa_scan_res *wa = *_wa;
1668 struct wpa_scan_res *wb = *_wb;
1669 int uses_wps_a, uses_wps_b;
1670 struct wpabuf *wps_a, *wps_b;
1673 /* Optimization - check WPS IE existence before allocated memory and
1674 * doing full reassembly. */
1675 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1676 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1677 if (uses_wps_a && !uses_wps_b)
1679 if (!uses_wps_a && uses_wps_b)
1682 if (uses_wps_a && uses_wps_b) {
1683 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1684 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1685 res = wps_ap_priority_compar(wps_a, wps_b);
1693 * Do not use current AP security policy as a sorting criteria during
1694 * WPS provisioning step since the AP may get reconfigured at the
1695 * completion of provisioning.
1698 /* all things being equal, use signal level; if signal levels are
1699 * identical, use quality values since some drivers may only report
1700 * that value and leave the signal level zero */
1701 if (wb->level == wa->level)
1702 return wb->qual - wa->qual;
1703 return wb->level - wa->level;
1705 #endif /* CONFIG_WPS */
1708 static void dump_scan_res(struct wpa_scan_results *scan_res)
1710 #ifndef CONFIG_NO_STDOUT_DEBUG
1713 if (scan_res->res == NULL || scan_res->num == 0)
1716 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1718 for (i = 0; i < scan_res->num; i++) {
1719 struct wpa_scan_res *r = scan_res->res[i];
1721 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1722 == WPA_SCAN_LEVEL_DBM) {
1723 int snr = r->level - r->noise;
1724 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1725 "noise=%d level=%d snr=%d%s flags=0x%x "
1727 MAC2STR(r->bssid), r->freq, r->qual,
1728 r->noise, r->level, snr,
1729 snr >= GREAT_SNR ? "*" : "", r->flags,
1732 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
1733 "noise=%d level=%d flags=0x%x age=%u",
1734 MAC2STR(r->bssid), r->freq, r->qual,
1735 r->noise, r->level, r->flags, r->age);
1737 pos = (u8 *) (r + 1);
1739 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1741 if (r->beacon_ie_len)
1742 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1743 pos, r->beacon_ie_len);
1745 #endif /* CONFIG_NO_STDOUT_DEBUG */
1750 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1751 * @wpa_s: Pointer to wpa_supplicant data
1752 * @bssid: BSSID to check
1753 * Returns: 0 if the BSSID is filtered or 1 if not
1755 * This function is used to filter out specific BSSIDs from scan reslts mainly
1756 * for testing purposes (SET bssid_filter ctrl_iface command).
1758 int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1763 if (wpa_s->bssid_filter == NULL)
1766 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1767 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1776 static void filter_scan_res(struct wpa_supplicant *wpa_s,
1777 struct wpa_scan_results *res)
1781 if (wpa_s->bssid_filter == NULL)
1784 for (i = 0, j = 0; i < res->num; i++) {
1785 if (wpa_supplicant_filter_bssid_match(wpa_s,
1786 res->res[i]->bssid)) {
1787 res->res[j++] = res->res[i];
1789 os_free(res->res[i]);
1794 if (res->num != j) {
1795 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1796 (int) (res->num - j));
1803 * wpa_supplicant_get_scan_results - Get scan results
1804 * @wpa_s: Pointer to wpa_supplicant data
1805 * @info: Information about what was scanned or %NULL if not available
1806 * @new_scan: Whether a new scan was performed
1807 * Returns: Scan results, %NULL on failure
1809 * This function request the current scan results from the driver and updates
1810 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1811 * results with wpa_scan_results_free().
1813 struct wpa_scan_results *
1814 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1815 struct scan_info *info, int new_scan)
1817 struct wpa_scan_results *scan_res;
1819 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1821 scan_res = wpa_drv_get_scan_results2(wpa_s);
1822 if (scan_res == NULL) {
1823 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1826 if (scan_res->fetch_time.sec == 0) {
1828 * Make sure we have a valid timestamp if the driver wrapper
1829 * does not set this.
1831 os_get_reltime(&scan_res->fetch_time);
1833 filter_scan_res(wpa_s, scan_res);
1836 if (wpas_wps_searching(wpa_s)) {
1837 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1838 "provisioning rules");
1839 compar = wpa_scan_result_wps_compar;
1841 #endif /* CONFIG_WPS */
1843 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1845 dump_scan_res(scan_res);
1847 wpa_bss_update_start(wpa_s);
1848 for (i = 0; i < scan_res->num; i++)
1849 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1850 &scan_res->fetch_time);
1851 wpa_bss_update_end(wpa_s, info, new_scan);
1858 * wpa_supplicant_update_scan_results - Update scan results from the driver
1859 * @wpa_s: Pointer to wpa_supplicant data
1860 * Returns: 0 on success, -1 on failure
1862 * This function updates the BSS table within wpa_supplicant based on the
1863 * currently available scan results from the driver without requesting a new
1864 * scan. This is used in cases where the driver indicates an association
1865 * (including roaming within ESS) and wpa_supplicant does not yet have the
1866 * needed information to complete the connection (e.g., to perform validation
1867 * steps in 4-way handshake).
1869 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1871 struct wpa_scan_results *scan_res;
1872 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1873 if (scan_res == NULL)
1875 wpa_scan_results_free(scan_res);
1882 * scan_only_handler - Reports scan results
1884 void scan_only_handler(struct wpa_supplicant *wpa_s,
1885 struct wpa_scan_results *scan_res)
1887 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
1888 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1889 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1890 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1891 wpa_s->manual_scan_id);
1892 wpa_s->manual_scan_use_id = 0;
1894 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1896 wpas_notify_scan_results(wpa_s);
1897 wpas_notify_scan_done(wpa_s, 1);
1898 if (wpa_s->scan_work) {
1899 struct wpa_radio_work *work = wpa_s->scan_work;
1900 wpa_s->scan_work = NULL;
1901 radio_work_done(work);
1906 int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1908 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1912 struct wpa_driver_scan_params *
1913 wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
1915 struct wpa_driver_scan_params *params;
1919 params = os_zalloc(sizeof(*params));
1923 for (i = 0; i < src->num_ssids; i++) {
1924 if (src->ssids[i].ssid) {
1925 n = os_malloc(src->ssids[i].ssid_len);
1928 os_memcpy(n, src->ssids[i].ssid,
1929 src->ssids[i].ssid_len);
1930 params->ssids[i].ssid = n;
1931 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
1934 params->num_ssids = src->num_ssids;
1936 if (src->extra_ies) {
1937 n = os_malloc(src->extra_ies_len);
1940 os_memcpy(n, src->extra_ies, src->extra_ies_len);
1941 params->extra_ies = n;
1942 params->extra_ies_len = src->extra_ies_len;
1946 int len = int_array_len(src->freqs);
1947 params->freqs = os_malloc((len + 1) * sizeof(int));
1948 if (params->freqs == NULL)
1950 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
1953 if (src->filter_ssids) {
1954 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
1955 src->num_filter_ssids);
1956 if (params->filter_ssids == NULL)
1958 os_memcpy(params->filter_ssids, src->filter_ssids,
1959 sizeof(*params->filter_ssids) *
1960 src->num_filter_ssids);
1961 params->num_filter_ssids = src->num_filter_ssids;
1964 params->filter_rssi = src->filter_rssi;
1965 params->p2p_probe = src->p2p_probe;
1966 params->only_new_results = src->only_new_results;
1967 params->low_priority = src->low_priority;
1969 if (src->mac_addr_rand) {
1970 params->mac_addr_rand = src->mac_addr_rand;
1972 if (src->mac_addr && src->mac_addr_mask) {
1975 mac_addr = os_malloc(2 * ETH_ALEN);
1979 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
1980 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
1982 params->mac_addr = mac_addr;
1983 params->mac_addr_mask = mac_addr + ETH_ALEN;
1989 wpa_scan_free_params(params);
1994 void wpa_scan_free_params(struct wpa_driver_scan_params *params)
2001 for (i = 0; i < params->num_ssids; i++)
2002 os_free((u8 *) params->ssids[i].ssid);
2003 os_free((u8 *) params->extra_ies);
2004 os_free(params->freqs);
2005 os_free(params->filter_ssids);
2008 * Note: params->mac_addr_mask points to same memory allocation and
2009 * must not be freed separately.
2011 os_free((u8 *) params->mac_addr);
2017 int wpas_start_pno(struct wpa_supplicant *wpa_s)
2020 size_t i, num_ssid, num_match_ssid;
2021 struct wpa_ssid *ssid;
2022 struct wpa_driver_scan_params params;
2024 if (!wpa_s->sched_scan_supported)
2027 if (wpa_s->pno || wpa_s->pno_sched_pending)
2030 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2031 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2032 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2036 if (wpa_s->wpa_state == WPA_SCANNING) {
2037 wpa_supplicant_cancel_scan(wpa_s);
2038 if (wpa_s->sched_scanning) {
2039 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2040 "ongoing sched scan");
2041 wpa_supplicant_cancel_sched_scan(wpa_s);
2042 wpa_s->pno_sched_pending = 1;
2047 os_memset(¶ms, 0, sizeof(params));
2049 num_ssid = num_match_ssid = 0;
2050 ssid = wpa_s->conf->ssid;
2052 if (!wpas_network_disabled(wpa_s, ssid)) {
2054 if (ssid->scan_ssid)
2060 if (num_match_ssid == 0) {
2061 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2065 if (num_match_ssid > num_ssid) {
2066 params.num_ssids++; /* wildcard */
2070 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
2071 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2072 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
2073 num_ssid = WPAS_MAX_SCAN_SSIDS;
2076 if (num_match_ssid > wpa_s->max_match_sets) {
2077 num_match_ssid = wpa_s->max_match_sets;
2078 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
2080 params.filter_ssids = os_calloc(num_match_ssid,
2081 sizeof(struct wpa_driver_scan_filter));
2082 if (params.filter_ssids == NULL)
2085 ssid = wpa_s->conf->ssid;
2087 if (!wpas_network_disabled(wpa_s, ssid)) {
2088 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2089 params.ssids[params.num_ssids].ssid =
2091 params.ssids[params.num_ssids].ssid_len =
2095 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2097 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2098 params.num_filter_ssids++;
2100 if (i == num_match_ssid)
2106 if (wpa_s->conf->filter_rssi)
2107 params.filter_rssi = wpa_s->conf->filter_rssi;
2109 interval = wpa_s->conf->sched_scan_interval ?
2110 wpa_s->conf->sched_scan_interval : 10;
2112 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2113 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2114 params.freqs = wpa_s->manual_sched_scan_freqs;
2117 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2118 params.mac_addr_rand = 1;
2119 if (wpa_s->mac_addr_pno) {
2120 params.mac_addr = wpa_s->mac_addr_pno;
2121 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2125 ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms, interval);
2126 os_free(params.filter_ssids);
2130 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2135 int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2142 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2145 wpa_s->pno_sched_pending = 0;
2147 if (wpa_s->wpa_state == WPA_SCANNING)
2148 wpa_supplicant_req_scan(wpa_s, 0, 0);
2154 void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2157 type &= MAC_ADDR_RAND_ALL;
2158 wpa_s->mac_addr_rand_enable &= ~type;
2160 if (type & MAC_ADDR_RAND_SCAN) {
2161 os_free(wpa_s->mac_addr_scan);
2162 wpa_s->mac_addr_scan = NULL;
2165 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2166 os_free(wpa_s->mac_addr_sched_scan);
2167 wpa_s->mac_addr_sched_scan = NULL;
2170 if (type & MAC_ADDR_RAND_PNO) {
2171 os_free(wpa_s->mac_addr_pno);
2172 wpa_s->mac_addr_pno = NULL;
2177 int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2178 unsigned int type, const u8 *addr,
2183 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2186 tmp = os_malloc(2 * ETH_ALEN);
2189 os_memcpy(tmp, addr, ETH_ALEN);
2190 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2193 if (type == MAC_ADDR_RAND_SCAN) {
2194 wpa_s->mac_addr_scan = tmp;
2195 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2196 wpa_s->mac_addr_sched_scan = tmp;
2197 } else if (type == MAC_ADDR_RAND_PNO) {
2198 wpa_s->mac_addr_pno = tmp;
2200 wpa_printf(MSG_INFO,
2201 "scan: Invalid MAC randomization type=0x%x",
2207 wpa_s->mac_addr_rand_enable |= type;