2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "wpa_supplicant_i.h"
23 #include "wps_supplicant.h"
26 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
28 struct wpa_ssid *ssid;
29 union wpa_event_data data;
31 ssid = wpa_supplicant_get_ssid(wpa_s);
35 if (wpa_s->current_ssid == NULL)
36 wpa_s->current_ssid = ssid;
37 wpa_supplicant_initiate_eapol(wpa_s);
38 wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
39 "generating associated event");
40 os_memset(&data, 0, sizeof(data));
41 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
46 static int wpas_wps_in_use(struct wpa_config *conf,
47 enum wps_request_type *req_type)
49 struct wpa_ssid *ssid;
52 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
53 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
57 *req_type = wpas_wps_get_req_type(ssid);
58 if (!ssid->eap.phase1)
61 if (os_strstr(ssid->eap.phase1, "pbc=1"))
67 #endif /* CONFIG_WPS */
70 static int wpa_supplicant_enabled_networks(struct wpa_config *conf)
72 struct wpa_ssid *ssid = conf->ssid;
82 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
83 struct wpa_ssid *ssid)
91 /* ap_scan=2 mode - try to associate with each SSID. */
93 wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
94 "end of scan list - go back to beginning");
95 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
96 wpa_supplicant_req_scan(wpa_s, 0, 0);
100 /* Continue from the next SSID on the next attempt. */
101 wpa_s->prev_scan_ssid = ssid;
103 /* Start from the beginning of the SSID list. */
104 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
106 wpa_supplicant_associate(wpa_s, NULL, ssid);
110 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
112 struct wpa_supplicant *wpa_s = eloop_ctx;
113 struct wpa_ssid *ssid;
114 int scan_req = 0, ret;
115 struct wpabuf *wps_ie = NULL;
118 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
119 #endif /* CONFIG_WPS */
120 struct wpa_driver_scan_params params;
123 if (wpa_s->disconnected && !wpa_s->scan_req)
126 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
128 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
129 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
133 if (wpa_s->conf->ap_scan != 0 &&
134 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
135 wpa_printf(MSG_DEBUG, "Using wired authentication - "
136 "overriding ap_scan configuration");
137 wpa_s->conf->ap_scan = 0;
140 if (wpa_s->conf->ap_scan == 0) {
141 wpa_supplicant_gen_assoc_event(wpa_s);
145 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
146 wpa_s->conf->ap_scan == 2)
149 max_ssids = wpa_s->max_scan_ssids;
150 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
151 max_ssids = WPAS_MAX_SCAN_SSIDS;
155 wps = wpas_wps_in_use(wpa_s->conf, &req_type);
156 #endif /* CONFIG_WPS */
158 if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
159 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
161 wpa_s->scan_res_tried++;
162 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
163 "first without requesting a new scan to speed up "
164 "initial association");
165 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
169 scan_req = wpa_s->scan_req;
172 os_memset(¶ms, 0, sizeof(params));
174 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
175 wpa_s->wpa_state == WPA_INACTIVE)
176 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
178 /* Find the starting point from which to continue scanning */
179 ssid = wpa_s->conf->ssid;
180 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
182 if (ssid == wpa_s->prev_scan_ssid) {
190 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
191 wpa_supplicant_assoc_try(wpa_s, ssid);
193 } else if (wpa_s->conf->ap_scan == 2) {
195 * User-initiated scan request in ap_scan == 2; scan with
200 struct wpa_ssid *start = ssid;
201 if (ssid == NULL && max_ssids > 1)
202 ssid = wpa_s->conf->ssid;
204 if (!ssid->disabled && ssid->scan_ssid) {
205 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
206 ssid->ssid, ssid->ssid_len);
207 params.ssids[params.num_ssids].ssid =
209 params.ssids[params.num_ssids].ssid_len =
212 if (params.num_ssids + 1 >= max_ssids)
218 if (ssid == NULL && max_ssids > 1 &&
219 start != wpa_s->conf->ssid)
220 ssid = wpa_s->conf->ssid;
225 wpa_s->prev_scan_ssid = ssid;
227 wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
231 wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
233 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
235 wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
240 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
241 wpa_s->wps->uuid, req_type);
243 params.extra_ies = wpabuf_head(wps_ie);
244 params.extra_ies_len = wpabuf_len(wps_ie);
247 #endif /* CONFIG_WPS */
249 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
250 ieee80211_sta_set_probe_req_ie(wpa_s, params.extra_ies,
251 params.extra_ies_len);
252 ret = ieee80211_sta_req_scan(wpa_s, params.ssids[0].ssid,
253 params.ssids[0].ssid_len);
255 wpa_drv_set_probe_req_ie(wpa_s, params.extra_ies,
256 params.extra_ies_len);
257 ret = wpa_drv_scan(wpa_s, ¶ms);
263 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
264 wpa_supplicant_req_scan(wpa_s, 10, 0);
271 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
272 * @wpa_s: Pointer to wpa_supplicant data
273 * @sec: Number of seconds after which to scan
274 * @usec: Number of microseconds after which to scan
276 * This function is used to schedule a scan for neighboring access points after
277 * the specified time.
279 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
281 /* If there's at least one network that should be specifically scanned
282 * then don't cancel the scan and reschedule. Some drivers do
283 * background scanning which generates frequent scan results, and that
284 * causes the specific SSID scan to get continually pushed back and
285 * never happen, which causes hidden APs to never get probe-scanned.
287 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
288 wpa_s->conf->ap_scan == 1) {
289 struct wpa_ssid *ssid = wpa_s->conf->ssid;
292 if (!ssid->disabled && ssid->scan_ssid)
297 wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
298 "ensure that specific SSID scans occur");
303 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
305 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
306 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
311 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
312 * @wpa_s: Pointer to wpa_supplicant data
314 * This function is used to cancel a scan request scheduled with
315 * wpa_supplicant_req_scan().
317 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
319 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
320 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);