2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2010, 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.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
21 #include "wpa_supplicant_i.h"
24 #include "wps_supplicant.h"
30 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
32 struct wpa_ssid *ssid;
33 union wpa_event_data data;
35 ssid = wpa_supplicant_get_ssid(wpa_s);
39 if (wpa_s->current_ssid == NULL) {
40 wpa_s->current_ssid = ssid;
41 if (wpa_s->current_ssid != NULL)
42 wpas_notify_network_changed(wpa_s);
44 wpa_supplicant_initiate_eapol(wpa_s);
45 wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
46 "generating associated event");
47 os_memset(&data, 0, sizeof(data));
48 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
53 static int wpas_wps_in_use(struct wpa_config *conf,
54 enum wps_request_type *req_type)
56 struct wpa_ssid *ssid;
59 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
60 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
64 *req_type = wpas_wps_get_req_type(ssid);
65 if (!ssid->eap.phase1)
68 if (os_strstr(ssid->eap.phase1, "pbc=1"))
74 #endif /* CONFIG_WPS */
77 int wpa_supplicant_enabled_networks(struct wpa_config *conf)
79 struct wpa_ssid *ssid = conf->ssid;
89 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
90 struct wpa_ssid *ssid)
98 /* ap_scan=2 mode - try to associate with each SSID. */
100 wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
101 "end of scan list - go back to beginning");
102 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
103 wpa_supplicant_req_scan(wpa_s, 0, 0);
107 /* Continue from the next SSID on the next attempt. */
108 wpa_s->prev_scan_ssid = ssid;
110 /* Start from the beginning of the SSID list. */
111 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
113 wpa_supplicant_associate(wpa_s, NULL, ssid);
117 static int int_array_len(const int *a)
120 for (i = 0; a && a[i]; i++)
126 static void int_array_concat(int **res, const int *a)
131 reslen = int_array_len(*res);
132 alen = int_array_len(a);
134 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
140 for (i = 0; i <= alen; i++)
141 n[reslen + i] = a[i];
146 static int freq_cmp(const void *a, const void *b)
159 static void int_array_sort_unique(int *a)
167 alen = int_array_len(a);
168 qsort(a, alen, sizeof(int), freq_cmp);
172 while (a[i] && a[j]) {
185 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
186 struct wpa_driver_scan_params *params)
190 wpa_supplicant_notify_scanning(wpa_s, 1);
192 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
193 ret = ieee80211_sta_req_scan(wpa_s, params);
195 ret = wpa_drv_scan(wpa_s, params);
198 wpa_supplicant_notify_scanning(wpa_s, 0);
199 wpas_notify_scan_done(wpa_s, 0);
207 static struct wpa_driver_scan_filter *
208 wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
210 struct wpa_driver_scan_filter *ssids;
211 struct wpa_ssid *ssid;
215 if (!conf->filter_ssids)
218 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
219 if (ssid->ssid && ssid->ssid_len)
224 ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
228 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
229 if (!ssid->ssid || !ssid->ssid_len)
231 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
232 ssids[*num_ssids].ssid_len = ssid->ssid_len;
240 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
242 struct wpa_supplicant *wpa_s = eloop_ctx;
243 struct wpa_ssid *ssid;
244 int scan_req = 0, ret;
245 struct wpabuf *wps_ie = NULL;
248 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
249 #endif /* CONFIG_WPS */
250 struct wpa_driver_scan_params params;
252 enum wpa_states prev_state;
254 if (wpa_s->disconnected && !wpa_s->scan_req) {
255 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
257 * Oct, 26th. 2011. TIZEN
258 * If scan after auth timeout is failed, wpa_s->associate_num should be initialized.
260 wpa_s->associate_num = 2;
264 wpa_printf(MSG_DEBUG,"Current scan req is [%d]",wpa_s->scan_req);
265 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
267 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
268 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
270 * Oct, 26th. 2011. TIZEN
271 * If scan after auth timeout is failed, wpa_s->associate_num should be initialized.
273 wpa_s->associate_num = 2;
277 if (wpa_s->conf->ap_scan != 0 &&
278 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
279 wpa_printf(MSG_DEBUG, "Using wired authentication - "
280 "overriding ap_scan configuration");
281 wpa_s->conf->ap_scan = 0;
282 wpas_notify_ap_scan_changed(wpa_s);
285 if (wpa_s->conf->ap_scan == 0) {
286 wpa_supplicant_gen_assoc_event(wpa_s);
290 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
291 wpa_s->conf->ap_scan == 2)
294 max_ssids = wpa_s->max_scan_ssids;
295 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
296 max_ssids = WPAS_MAX_SCAN_SSIDS;
300 wps = wpas_wps_in_use(wpa_s->conf, &req_type);
301 #endif /* CONFIG_WPS */
303 scan_req = wpa_s->scan_req;
306 os_memset(¶ms, 0, sizeof(params));
308 prev_state = wpa_s->wpa_state;
309 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
310 wpa_s->wpa_state == WPA_INACTIVE)
311 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
313 /* Find the starting point from which to continue scanning */
314 ssid = wpa_s->conf->ssid;
317 wpa_printf(MSG_DEBUG, "==============================");
318 wpa_printf(MSG_DEBUG, "Check 1 wildcard ssid [%s]",ssid->ssid);
319 wpa_printf(MSG_DEBUG, "==============================");
321 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
323 if (ssid == wpa_s->prev_scan_ssid) {
330 wpa_printf(MSG_DEBUG, "==============================");
331 wpa_printf(MSG_DEBUG, "Check 2 wildcard ssid [%s]",ssid->ssid);
332 wpa_printf(MSG_DEBUG, "==============================");
337 if (scan_req != 2 && (wpa_s->conf->ap_scan == 2 ||
338 wpa_s->connect_without_scan)) {
339 wpa_s->connect_without_scan = 0;
340 wpa_supplicant_assoc_try(wpa_s, ssid);
342 } else if (wpa_s->conf->ap_scan == 2) {
344 * User-initiated scan request in ap_scan == 2; scan with
348 wpa_printf(MSG_DEBUG, "==============================");
349 wpa_printf(MSG_DEBUG, "Check 3 wildcard ssid is NULL");
350 wpa_printf(MSG_DEBUG, "==============================");
352 struct wpa_ssid *start = ssid, *tssid;
354 if (ssid == NULL && max_ssids > 1)
356 ssid = wpa_s->conf->ssid;
359 wpa_printf(MSG_DEBUG, "==============================");
360 wpa_printf(MSG_DEBUG, "Check 4 wildcard ssid [%s]",ssid->ssid);
361 wpa_printf(MSG_DEBUG, "==============================");
365 if (!ssid->disabled && ssid->scan_ssid) {
366 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
367 ssid->ssid, ssid->ssid_len);
368 params.ssids[params.num_ssids].ssid =
370 params.ssids[params.num_ssids].ssid_len =
373 if (params.num_ssids + 1 >= max_ssids)
379 if (ssid == NULL && max_ssids > 1 &&
380 start != wpa_s->conf->ssid)
381 ssid = wpa_s->conf->ssid;
385 wpa_printf(MSG_DEBUG, "==============================");
386 wpa_printf(MSG_DEBUG, "Check 5 wildcard ssid [%s]",ssid->ssid);
387 wpa_printf(MSG_DEBUG, "==============================");
389 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
392 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
393 int_array_concat(¶ms.freqs,
396 os_free(params.freqs);
401 int_array_sort_unique(params.freqs);
405 wpa_printf(MSG_DEBUG, "==============================");
406 wpa_printf(MSG_DEBUG, "Check 6 wildcard ssid [%s]",ssid->ssid);
407 wpa_printf(MSG_DEBUG, "==============================");
410 wpa_s->prev_scan_ssid = ssid;
412 wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
416 wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
418 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
420 wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
424 if (params.freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
426 * Optimize post-provisioning scan based on channel used
427 * during provisioning.
429 wpa_printf(MSG_DEBUG, "WPS: Scan only frequency %u MHz that "
430 "was used during provisioning", wpa_s->wps_freq);
431 params.freqs = os_zalloc(2 * sizeof(int));
433 params.freqs[0] = wpa_s->wps_freq;
438 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
439 wpa_s->wps->uuid, req_type);
441 params.extra_ies = wpabuf_head(wps_ie);
442 params.extra_ies_len = wpabuf_len(wps_ie);
445 #endif /* CONFIG_WPS */
447 params.filter_ssids = wpa_supplicant_build_filter_ssids(
448 wpa_s->conf, ¶ms.num_filter_ssids);
450 ret = wpa_supplicant_trigger_scan(wpa_s, ¶ms);
453 os_free(params.freqs);
454 os_free(params.filter_ssids);
457 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
458 if (prev_state != wpa_s->wpa_state)
459 wpa_supplicant_set_state(wpa_s, prev_state);
460 wpa_supplicant_req_scan(wpa_s, 1, 0);
466 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
467 * @wpa_s: Pointer to wpa_supplicant data
468 * @sec: Number of seconds after which to scan
469 * @usec: Number of microseconds after which to scan
471 * This function is used to schedule a scan for neighboring access points after
472 * the specified time.
474 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
476 /* If there's at least one network that should be specifically scanned
477 * then don't cancel the scan and reschedule. Some drivers do
478 * background scanning which generates frequent scan results, and that
479 * causes the specific SSID scan to get continually pushed back and
480 * never happen, which causes hidden APs to never get probe-scanned.
482 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
483 wpa_s->conf->ap_scan == 1) {
484 struct wpa_ssid *ssid = wpa_s->conf->ssid;
487 if (!ssid->disabled && ssid->scan_ssid)
492 wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
493 "ensure that specific SSID scans occur");
498 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
500 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
501 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
506 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
507 * @wpa_s: Pointer to wpa_supplicant data
509 * This function is used to cancel a scan request scheduled with
510 * wpa_supplicant_req_scan().
512 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
514 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
515 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
519 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
522 if (wpa_s->scanning != scanning) {
523 wpa_s->scanning = scanning;
524 wpas_notify_scanning(wpa_s);
529 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
535 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
536 for (i = 0; ie && i < ie[1]; i++) {
537 if ((ie[i + 2] & 0x7f) > rate)
538 rate = ie[i + 2] & 0x7f;
541 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
542 for (i = 0; ie && i < ie[1]; i++) {
543 if ((ie[i + 2] & 0x7f) > rate)
544 rate = ie[i + 2] & 0x7f;
551 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
555 pos = (const u8 *) (res + 1);
556 end = pos + res->ie_len;
558 while (pos + 1 < end) {
559 if (pos + 2 + pos[1] > end)
570 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
575 pos = (const u8 *) (res + 1);
576 end = pos + res->ie_len;
578 while (pos + 1 < end) {
579 if (pos + 2 + pos[1] > end)
581 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
582 vendor_type == WPA_GET_BE32(&pos[2]))
591 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
597 buf = wpabuf_alloc(res->ie_len);
601 pos = (const u8 *) (res + 1);
602 end = pos + res->ie_len;
604 while (pos + 1 < end) {
605 if (pos + 2 + pos[1] > end)
607 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
608 vendor_type == WPA_GET_BE32(&pos[2]))
609 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
613 if (wpabuf_len(buf) == 0) {
622 /* Compare function for sorting scan results. Return >0 if @b is considered
624 static int wpa_scan_result_compar(const void *a, const void *b)
626 struct wpa_scan_res **_wa = (void *) a;
627 struct wpa_scan_res **_wb = (void *) b;
628 struct wpa_scan_res *wa = *_wa;
629 struct wpa_scan_res *wb = *_wb;
630 int wpa_a, wpa_b, maxrate_a, maxrate_b;
632 /* WPA/WPA2 support preferred */
633 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
634 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
635 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
636 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
643 /* privacy support preferred */
644 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
645 (wb->caps & IEEE80211_CAP_PRIVACY))
647 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
648 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
651 /* best/max rate preferred if signal level close enough XXX */
652 if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
653 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
654 maxrate_a = wpa_scan_get_max_rate(wa);
655 maxrate_b = wpa_scan_get_max_rate(wb);
656 if (maxrate_a != maxrate_b)
657 return maxrate_b - maxrate_a;
660 /* use freq for channel preference */
662 /* all things being equal, use signal level; if signal levels are
663 * identical, use quality values since some drivers may only report
664 * that value and leave the signal level zero */
665 if (wb->level == wa->level)
666 return wb->qual - wa->qual;
667 return wb->level - wa->level;
672 * wpa_supplicant_get_scan_results - Get scan results
673 * @wpa_s: Pointer to wpa_supplicant data
674 * @info: Information about what was scanned or %NULL if not available
675 * @new_scan: Whether a new scan was performed
676 * Returns: Scan results, %NULL on failure
678 * This function request the current scan results from the driver and updates
679 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
680 * results with wpa_scan_results_free().
682 struct wpa_scan_results *
683 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
684 struct scan_info *info, int new_scan)
686 struct wpa_scan_results *scan_res;
689 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
690 scan_res = ieee80211_sta_get_scan_results(wpa_s);
692 scan_res = wpa_drv_get_scan_results2(wpa_s);
693 if (scan_res == NULL) {
694 wpa_printf(MSG_DEBUG, "Failed to get scan results");
698 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
699 wpa_scan_result_compar);
700 wpa_printf(MSG_DEBUG,"current scan num is [%d]", scan_res->num);
701 wpa_bss_update_start(wpa_s);
702 for (i = 0; i < scan_res->num; i++)
705 * September, 30th 2011. TIZEN
707 * TIZEN doesn't want to get scan results
708 * which are under signal level -85dbm.
709 * Therefore, scan results are filtered by signal level under -85dbm
711 if( scan_res->res[i]->level < 170)
713 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
715 wpa_bss_update_end(wpa_s, info, new_scan);
721 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
723 struct wpa_scan_results *scan_res;
724 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
725 if (scan_res == NULL)
727 wpa_scan_results_free(scan_res);
733 void wpa_scan_results_free(struct wpa_scan_results *res)
740 for (i = 0; i < res->num; i++)
741 os_free(res->res[i]);