Share a single Linux ioctl helper fo setting interface up/down
[hostap-gosc2009.git] / wpa_supplicant / scan.c
blob56f062f56b42b01787858d71d039109ce1baf0cd
1 /*
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
10 * license.
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"
20 #include "config.h"
21 #include "wpa_supplicant_i.h"
22 #include "driver_i.h"
23 #include "mlme.h"
24 #include "wps_supplicant.h"
25 #include "notify.h"
26 #include "bss.h"
27 #include "scan.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);
36 if (ssid == NULL)
37 return;
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);
52 #ifdef CONFIG_WPS
53 static int wpas_wps_in_use(struct wpa_config *conf,
54 enum wps_request_type *req_type)
56 struct wpa_ssid *ssid;
57 int wps = 0;
59 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
60 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
61 continue;
63 wps = 1;
64 *req_type = wpas_wps_get_req_type(ssid);
65 if (!ssid->eap.phase1)
66 continue;
68 if (os_strstr(ssid->eap.phase1, "pbc=1"))
69 return 2;
72 return wps;
74 #endif /* CONFIG_WPS */
77 int wpa_supplicant_enabled_networks(struct wpa_config *conf)
79 struct wpa_ssid *ssid = conf->ssid;
80 while (ssid) {
81 if (!ssid->disabled)
82 return 1;
83 ssid = ssid->next;
85 return 0;
89 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
90 struct wpa_ssid *ssid)
92 while (ssid) {
93 if (!ssid->disabled)
94 break;
95 ssid = ssid->next;
98 /* ap_scan=2 mode - try to associate with each SSID. */
99 if (ssid == NULL) {
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);
104 return;
106 if (ssid->next) {
107 /* Continue from the next SSID on the next attempt. */
108 wpa_s->prev_scan_ssid = ssid;
109 } else {
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)
119 int i;
120 for (i = 0; a && a[i]; i++)
122 return i;
126 static void int_array_concat(int **res, const int *a)
128 int reslen, alen, i;
129 int *n;
131 reslen = int_array_len(*res);
132 alen = int_array_len(a);
134 n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
135 if (n == NULL) {
136 os_free(*res);
137 *res = NULL;
138 return;
140 for (i = 0; i <= alen; i++)
141 n[reslen + i] = a[i];
142 *res = n;
146 static int freq_cmp(const void *a, const void *b)
148 int _a = *(int *) a;
149 int _b = *(int *) b;
151 if (_a == 0)
152 return 1;
153 if (_b == 0)
154 return -1;
155 return _a - _b;
159 static void int_array_sort_unique(int *a)
161 int alen;
162 int i, j;
164 if (a == NULL)
165 return;
167 alen = int_array_len(a);
168 qsort(a, alen, sizeof(int), freq_cmp);
170 i = 0;
171 j = 1;
172 while (a[i] && a[j]) {
173 if (a[i] == a[j]) {
174 j++;
175 continue;
177 a[++i] = a[j++];
179 if (a[i])
180 i++;
181 a[i] = 0;
185 int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
186 struct wpa_driver_scan_params *params)
188 int ret;
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);
194 else
195 ret = wpa_drv_scan(wpa_s, params);
197 if (ret) {
198 wpa_supplicant_notify_scanning(wpa_s, 0);
199 wpas_notify_scan_done(wpa_s, 0);
200 } else
201 wpa_s->scan_runs++;
203 return ret;
207 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
209 struct wpa_supplicant *wpa_s = eloop_ctx;
210 struct wpa_ssid *ssid;
211 int scan_req = 0, ret;
212 struct wpabuf *wps_ie = NULL;
213 int wps = 0;
214 #ifdef CONFIG_WPS
215 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
216 #endif /* CONFIG_WPS */
217 struct wpa_driver_scan_params params;
218 size_t max_ssids;
220 if (wpa_s->disconnected && !wpa_s->scan_req) {
221 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
222 return;
225 if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
226 !wpa_s->scan_req) {
227 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
228 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
229 return;
232 if (wpa_s->conf->ap_scan != 0 &&
233 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
234 wpa_printf(MSG_DEBUG, "Using wired authentication - "
235 "overriding ap_scan configuration");
236 wpa_s->conf->ap_scan = 0;
237 wpas_notify_ap_scan_changed(wpa_s);
240 if (wpa_s->conf->ap_scan == 0) {
241 wpa_supplicant_gen_assoc_event(wpa_s);
242 return;
245 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
246 wpa_s->conf->ap_scan == 2)
247 max_ssids = 1;
248 else {
249 max_ssids = wpa_s->max_scan_ssids;
250 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
251 max_ssids = WPAS_MAX_SCAN_SSIDS;
254 #ifdef CONFIG_WPS
255 wps = wpas_wps_in_use(wpa_s->conf, &req_type);
256 #endif /* CONFIG_WPS */
258 if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
259 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
260 wps != 2) {
261 wpa_s->scan_res_tried++;
262 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
263 "first without requesting a new scan to speed up "
264 "initial association");
265 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
266 return;
269 scan_req = wpa_s->scan_req;
270 wpa_s->scan_req = 0;
272 os_memset(&params, 0, sizeof(params));
274 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
275 wpa_s->wpa_state == WPA_INACTIVE)
276 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
278 /* Find the starting point from which to continue scanning */
279 ssid = wpa_s->conf->ssid;
280 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
281 while (ssid) {
282 if (ssid == wpa_s->prev_scan_ssid) {
283 ssid = ssid->next;
284 break;
286 ssid = ssid->next;
290 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
291 wpa_supplicant_assoc_try(wpa_s, ssid);
292 return;
293 } else if (wpa_s->conf->ap_scan == 2) {
295 * User-initiated scan request in ap_scan == 2; scan with
296 * wildcard SSID.
298 ssid = NULL;
299 } else {
300 struct wpa_ssid *start = ssid, *tssid;
301 int freqs_set = 0;
302 if (ssid == NULL && max_ssids > 1)
303 ssid = wpa_s->conf->ssid;
304 while (ssid) {
305 if (!ssid->disabled && ssid->scan_ssid) {
306 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
307 ssid->ssid, ssid->ssid_len);
308 params.ssids[params.num_ssids].ssid =
309 ssid->ssid;
310 params.ssids[params.num_ssids].ssid_len =
311 ssid->ssid_len;
312 params.num_ssids++;
313 if (params.num_ssids + 1 >= max_ssids)
314 break;
316 ssid = ssid->next;
317 if (ssid == start)
318 break;
319 if (ssid == NULL && max_ssids > 1 &&
320 start != wpa_s->conf->ssid)
321 ssid = wpa_s->conf->ssid;
324 for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
325 if (tssid->disabled)
326 continue;
327 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
328 int_array_concat(&params.freqs,
329 tssid->scan_freq);
330 } else {
331 os_free(params.freqs);
332 params.freqs = NULL;
334 freqs_set = 1;
336 int_array_sort_unique(params.freqs);
339 if (ssid) {
340 wpa_s->prev_scan_ssid = ssid;
341 if (max_ssids > 1) {
342 wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
343 "scan request");
344 params.num_ssids++;
346 wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
347 } else {
348 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
349 params.num_ssids++;
350 wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
353 #ifdef CONFIG_WPS
354 if (wps) {
355 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
356 wpa_s->wps->uuid, req_type);
357 if (wps_ie) {
358 params.extra_ies = wpabuf_head(wps_ie);
359 params.extra_ies_len = wpabuf_len(wps_ie);
362 #endif /* CONFIG_WPS */
364 ret = wpa_supplicant_trigger_scan(wpa_s, &params);
366 wpabuf_free(wps_ie);
367 os_free(params.freqs);
369 if (ret) {
370 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
371 wpa_supplicant_req_scan(wpa_s, 10, 0);
372 } else
373 wpa_s->scan_runs++;
378 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
379 * @wpa_s: Pointer to wpa_supplicant data
380 * @sec: Number of seconds after which to scan
381 * @usec: Number of microseconds after which to scan
383 * This function is used to schedule a scan for neighboring access points after
384 * the specified time.
386 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
388 /* If there's at least one network that should be specifically scanned
389 * then don't cancel the scan and reschedule. Some drivers do
390 * background scanning which generates frequent scan results, and that
391 * causes the specific SSID scan to get continually pushed back and
392 * never happen, which causes hidden APs to never get probe-scanned.
394 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
395 wpa_s->conf->ap_scan == 1) {
396 struct wpa_ssid *ssid = wpa_s->conf->ssid;
398 while (ssid) {
399 if (!ssid->disabled && ssid->scan_ssid)
400 break;
401 ssid = ssid->next;
403 if (ssid) {
404 wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
405 "ensure that specific SSID scans occur");
406 return;
410 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
411 sec, usec);
412 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
413 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
418 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
419 * @wpa_s: Pointer to wpa_supplicant data
421 * This function is used to cancel a scan request scheduled with
422 * wpa_supplicant_req_scan().
424 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
426 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
427 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
431 void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
432 int scanning)
434 if (wpa_s->scanning != scanning) {
435 wpa_s->scanning = scanning;
436 wpas_notify_scanning(wpa_s);
441 static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
443 int rate = 0;
444 const u8 *ie;
445 int i;
447 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
448 for (i = 0; ie && i < ie[1]; i++) {
449 if ((ie[i + 2] & 0x7f) > rate)
450 rate = ie[i + 2] & 0x7f;
453 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
454 for (i = 0; ie && i < ie[1]; i++) {
455 if ((ie[i + 2] & 0x7f) > rate)
456 rate = ie[i + 2] & 0x7f;
459 return rate;
463 const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
465 const u8 *end, *pos;
467 pos = (const u8 *) (res + 1);
468 end = pos + res->ie_len;
470 while (pos + 1 < end) {
471 if (pos + 2 + pos[1] > end)
472 break;
473 if (pos[0] == ie)
474 return pos;
475 pos += 2 + pos[1];
478 return NULL;
482 const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
483 u32 vendor_type)
485 const u8 *end, *pos;
487 pos = (const u8 *) (res + 1);
488 end = pos + res->ie_len;
490 while (pos + 1 < end) {
491 if (pos + 2 + pos[1] > end)
492 break;
493 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
494 vendor_type == WPA_GET_BE32(&pos[2]))
495 return pos;
496 pos += 2 + pos[1];
499 return NULL;
503 struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
504 u32 vendor_type)
506 struct wpabuf *buf;
507 const u8 *end, *pos;
509 buf = wpabuf_alloc(res->ie_len);
510 if (buf == NULL)
511 return NULL;
513 pos = (const u8 *) (res + 1);
514 end = pos + res->ie_len;
516 while (pos + 1 < end) {
517 if (pos + 2 + pos[1] > end)
518 break;
519 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
520 vendor_type == WPA_GET_BE32(&pos[2]))
521 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
522 pos += 2 + pos[1];
525 if (wpabuf_len(buf) == 0) {
526 wpabuf_free(buf);
527 buf = NULL;
530 return buf;
534 /* Compare function for sorting scan results. Return >0 if @b is considered
535 * better. */
536 static int wpa_scan_result_compar(const void *a, const void *b)
538 struct wpa_scan_res **_wa = (void *) a;
539 struct wpa_scan_res **_wb = (void *) b;
540 struct wpa_scan_res *wa = *_wa;
541 struct wpa_scan_res *wb = *_wb;
542 int wpa_a, wpa_b, maxrate_a, maxrate_b;
544 /* WPA/WPA2 support preferred */
545 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
546 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
547 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
548 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
550 if (wpa_b && !wpa_a)
551 return 1;
552 if (!wpa_b && wpa_a)
553 return -1;
555 /* privacy support preferred */
556 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
557 (wb->caps & IEEE80211_CAP_PRIVACY))
558 return 1;
559 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
560 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
561 return -1;
563 /* best/max rate preferred if signal level close enough XXX */
564 if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
565 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
566 maxrate_a = wpa_scan_get_max_rate(wa);
567 maxrate_b = wpa_scan_get_max_rate(wb);
568 if (maxrate_a != maxrate_b)
569 return maxrate_b - maxrate_a;
572 /* use freq for channel preference */
574 /* all things being equal, use signal level; if signal levels are
575 * identical, use quality values since some drivers may only report
576 * that value and leave the signal level zero */
577 if (wb->level == wa->level)
578 return wb->qual - wa->qual;
579 return wb->level - wa->level;
584 * wpa_supplicant_get_scan_results - Get scan results
585 * @wpa_s: Pointer to wpa_supplicant data
586 * @info: Information about what was scanned or %NULL if not available
587 * @new_scan: Whether a new scan was performed
588 * Returns: Scan results, %NULL on failure
590 * This function request the current scan results from the driver and updates
591 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
592 * results with wpa_scan_results_free().
594 struct wpa_scan_results *
595 wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
596 struct scan_info *info, int new_scan)
598 struct wpa_scan_results *scan_res;
599 size_t i;
601 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
602 scan_res = ieee80211_sta_get_scan_results(wpa_s);
603 else
604 scan_res = wpa_drv_get_scan_results2(wpa_s);
605 if (scan_res == NULL) {
606 wpa_printf(MSG_DEBUG, "Failed to get scan results");
607 return NULL;
610 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
611 wpa_scan_result_compar);
613 wpa_bss_update_start(wpa_s);
614 for (i = 0; i < scan_res->num; i++)
615 wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
616 wpa_bss_update_end(wpa_s, info, new_scan);
618 return scan_res;
622 int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
624 struct wpa_scan_results *scan_res;
625 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
626 if (scan_res == NULL)
627 return -1;
628 wpa_scan_results_free(scan_res);
630 return 0;
634 void wpa_scan_results_free(struct wpa_scan_results *res)
636 size_t i;
638 if (res == NULL)
639 return;
641 for (i = 0; i < res->num; i++)
642 os_free(res->res[i]);
643 os_free(res->res);
644 os_free(res);