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"
27 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29 struct wpa_ssid *ssid;
30 union wpa_event_data data;
32 ssid = wpa_supplicant_get_ssid(wpa_s);
36 if (wpa_s->current_ssid == NULL) {
37 wpa_s->current_ssid = ssid;
38 if (wpa_s->current_ssid != NULL)
39 wpas_notify_network_changed(wpa_s);
41 wpa_supplicant_initiate_eapol(wpa_s);
42 wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
43 "generating associated event");
44 os_memset(&data, 0, sizeof(data));
45 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
50 static int wpas_wps_in_use(struct wpa_config *conf,
51 enum wps_request_type *req_type)
53 struct wpa_ssid *ssid;
56 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
57 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
61 *req_type = wpas_wps_get_req_type(ssid);
62 if (!ssid->eap.phase1)
65 if (os_strstr(ssid->eap.phase1, "pbc=1"))
71 #endif /* CONFIG_WPS */
74 static int wpa_supplicant_enabled_networks(struct wpa_config *conf)
76 struct wpa_ssid *ssid = conf->ssid;
86 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
87 struct wpa_ssid *ssid)
95 /* ap_scan=2 mode - try to associate with each SSID. */
97 wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
98 "end of scan list - go back to beginning");
99 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
100 wpa_supplicant_req_scan(wpa_s, 0, 0);
104 /* Continue from the next SSID on the next attempt. */
105 wpa_s->prev_scan_ssid = ssid;
107 /* Start from the beginning of the SSID list. */
108 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
110 wpa_supplicant_associate(wpa_s, NULL, ssid);
114 static int int_array_len(const int *a)
117 for (i = 0; a && a[i]; i++)
123 static void int_array_concat(int **res, const int *a)
128 reslen = int_array_len(*res);
129 alen = int_array_len(a);
131 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
136 for (i = 0; i <= alen; i++)
137 n[reslen + i] = a[i];
142 static int freq_cmp(const void *a, const void *b)
155 static void int_array_sort_unique(int *a)
163 alen = int_array_len(a);
164 qsort(a, alen, sizeof(int), freq_cmp);
168 while (a[i] && a[j]) {
181 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
183 struct wpa_supplicant *wpa_s = eloop_ctx;
184 struct wpa_ssid *ssid;
185 int scan_req = 0, ret;
186 struct wpabuf *wps_ie = NULL;
189 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
190 #endif /* CONFIG_WPS */
191 struct wpa_driver_scan_params params;
194 if (wpa_s->disconnected && !wpa_s->scan_req)
197 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
199 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
200 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
204 if (wpa_s->conf->ap_scan != 0 &&
205 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
206 wpa_printf(MSG_DEBUG, "Using wired authentication - "
207 "overriding ap_scan configuration");
208 wpa_s->conf->ap_scan = 0;
209 wpas_notify_ap_scan_changed(wpa_s);
212 if (wpa_s->conf->ap_scan == 0) {
213 wpa_supplicant_gen_assoc_event(wpa_s);
217 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
218 wpa_s->conf->ap_scan == 2)
221 max_ssids = wpa_s->max_scan_ssids;
222 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
223 max_ssids = WPAS_MAX_SCAN_SSIDS;
227 wps = wpas_wps_in_use(wpa_s->conf, &req_type);
228 #endif /* CONFIG_WPS */
230 if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
231 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
233 wpa_s->scan_res_tried++;
234 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
235 "first without requesting a new scan to speed up "
236 "initial association");
237 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
241 scan_req = wpa_s->scan_req;
244 os_memset(¶ms, 0, sizeof(params));
246 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
247 wpa_s->wpa_state == WPA_INACTIVE)
248 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
250 /* Find the starting point from which to continue scanning */
251 ssid = wpa_s->conf->ssid;
252 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
254 if (ssid == wpa_s->prev_scan_ssid) {
262 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
263 wpa_supplicant_assoc_try(wpa_s, ssid);
265 } else if (wpa_s->conf->ap_scan == 2) {
267 * User-initiated scan request in ap_scan == 2; scan with
272 struct wpa_ssid *start = ssid;
274 if (ssid == NULL && max_ssids > 1)
275 ssid = wpa_s->conf->ssid;
277 if (!ssid->disabled && ssid->scan_ssid) {
278 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
279 ssid->ssid, ssid->ssid_len);
280 params.ssids[params.num_ssids].ssid =
282 params.ssids[params.num_ssids].ssid_len =
285 if (params.num_ssids + 1 >= max_ssids)
291 if (ssid == NULL && max_ssids > 1 &&
292 start != wpa_s->conf->ssid)
293 ssid = wpa_s->conf->ssid;
296 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
299 if ((params.freqs || !freqs_set) && ssid->scan_freq) {
300 int_array_concat(¶ms.freqs,
303 os_free(params.freqs);
308 int_array_sort_unique(params.freqs);
312 wpa_s->prev_scan_ssid = ssid;
314 wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
318 wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
320 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
322 wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
327 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
328 wpa_s->wps->uuid, req_type);
330 params.extra_ies = wpabuf_head(wps_ie);
331 params.extra_ies_len = wpabuf_len(wps_ie);
334 #endif /* CONFIG_WPS */
336 wpa_supplicant_notify_scanning(wpa_s, 1);
338 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
339 ieee80211_sta_set_probe_req_ie(wpa_s, params.extra_ies,
340 params.extra_ies_len);
341 ret = ieee80211_sta_req_scan(wpa_s, params.ssids[0].ssid,
342 params.ssids[0].ssid_len);
344 wpa_drv_set_probe_req_ie(wpa_s, params.extra_ies,
345 params.extra_ies_len);
346 ret = wpa_drv_scan(wpa_s, ¶ms);
350 os_free(params.freqs);
353 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
354 wpa_supplicant_notify_scanning(wpa_s, 0);
355 wpas_notify_scan_done(wpa_s, 0);
356 wpa_supplicant_req_scan(wpa_s, 10, 0);
363 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
364 * @wpa_s: Pointer to wpa_supplicant data
365 * @sec: Number of seconds after which to scan
366 * @usec: Number of microseconds after which to scan
368 * This function is used to schedule a scan for neighboring access points after
369 * the specified time.
371 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
373 /* If there's at least one network that should be specifically scanned
374 * then don't cancel the scan and reschedule. Some drivers do
375 * background scanning which generates frequent scan results, and that
376 * causes the specific SSID scan to get continually pushed back and
377 * never happen, which causes hidden APs to never get probe-scanned.
379 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
380 wpa_s->conf->ap_scan == 1) {
381 struct wpa_ssid *ssid = wpa_s->conf->ssid;
384 if (!ssid->disabled && ssid->scan_ssid)
389 wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
390 "ensure that specific SSID scans occur");
395 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
397 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
398 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
403 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
404 * @wpa_s: Pointer to wpa_supplicant data
406 * This function is used to cancel a scan request scheduled with
407 * wpa_supplicant_req_scan().
409 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
411 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
412 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
416 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
419 if (wpa_s->scanning != scanning) {
420 wpa_s->scanning = scanning;
421 wpas_notify_scanning(wpa_s);