3 * Copyright (c) 2009-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"
20 #include "drivers/driver.h"
21 #include "wpa_supplicant_i.h"
27 #ifndef WPA_BSS_MAX_COUNT
28 #define WPA_BSS_MAX_COUNT 200
29 #endif /* WPA_BSS_MAX_COUNT */
32 * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
34 #define WPA_BSS_EXPIRATION_PERIOD 10
37 * WPA_BSS_EXPIRATION_AGE - BSS entry age after which it can be expired
39 * This value control the time in seconds after which a BSS entry gets removed
40 * if it has not been updated or is not in use.
42 #define WPA_BSS_EXPIRATION_AGE 180
45 * WPA_BSS_EXPIRATION_SCAN_COUNT - Expire BSS after number of scans
47 * If the BSS entry has not been seen in this many scans, it will be removed.
48 * Value 1 means that the entry is removed after the first scan without the
49 * BSSID being seen. Larger values can be used to avoid BSS entries
50 * disappearing if they are not visible in every scan (e.g., low signal quality
53 #define WPA_BSS_EXPIRATION_SCAN_COUNT 2
55 #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
56 #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
57 #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
58 #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
59 #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
60 #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
61 #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
62 #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
65 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
67 dl_list_del(&bss->list);
68 dl_list_del(&bss->list_id);
70 wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'",
71 bss->id, MAC2STR(bss->bssid),
72 wpa_ssid_txt(bss->ssid, bss->ssid_len));
73 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
78 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
79 const u8 *ssid, size_t ssid_len)
82 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
83 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
84 bss->ssid_len == ssid_len &&
85 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
92 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
96 dst->flags = src->flags;
97 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
98 dst->freq = src->freq;
99 dst->beacon_int = src->beacon_int;
100 dst->caps = src->caps;
101 dst->qual = src->qual;
102 dst->noise = src->noise;
103 dst->level = src->level;
106 os_get_time(&dst->last_update);
107 dst->last_update.sec -= src->age / 1000;
108 usec = (src->age % 1000) * 1000;
109 if (dst->last_update.usec < usec) {
110 dst->last_update.sec--;
111 dst->last_update.usec += 1000000;
113 dst->last_update.usec -= usec;
117 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
118 const u8 *ssid, size_t ssid_len,
119 struct wpa_scan_res *res)
123 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
126 bss->id = wpa_s->bss_next_id++;
127 bss->last_update_idx = wpa_s->bss_update_idx;
128 wpa_bss_copy_res(bss, res);
129 os_memcpy(bss->ssid, ssid, ssid_len);
130 bss->ssid_len = ssid_len;
131 bss->ie_len = res->ie_len;
132 bss->beacon_ie_len = res->beacon_ie_len;
133 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
135 dl_list_add_tail(&wpa_s->bss, &bss->list);
136 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
138 wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'",
139 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
140 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
141 if (wpa_s->num_bss > WPA_BSS_MAX_COUNT) {
142 /* Remove the oldest entry */
143 wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
144 struct wpa_bss, list));
149 static int are_ies_equal(const struct wpa_bss *old,
150 const struct wpa_scan_res *new, u32 ie)
152 const u8 *old_ie, *new_ie;
153 struct wpabuf *old_ie_buff = NULL;
154 struct wpabuf *new_ie_buff = NULL;
155 int new_ie_len, old_ie_len, ret, is_multi;
158 case WPA_IE_VENDOR_TYPE:
159 old_ie = wpa_bss_get_vendor_ie(old, ie);
160 new_ie = wpa_scan_get_vendor_ie(new, ie);
163 case WPS_IE_VENDOR_TYPE:
164 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
165 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
169 case WLAN_EID_SUPP_RATES:
170 case WLAN_EID_EXT_SUPP_RATES:
171 old_ie = wpa_bss_get_ie(old, ie);
172 new_ie = wpa_scan_get_ie(new, ie);
176 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
181 /* in case of multiple IEs stored in buffer */
182 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
183 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
184 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
185 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
187 /* in case of single IE */
188 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
189 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
192 ret = (old_ie_len == new_ie_len &&
193 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
195 wpabuf_free(old_ie_buff);
196 wpabuf_free(new_ie_buff);
202 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
203 const struct wpa_scan_res *new)
206 int caps_diff = old->caps ^ new->caps;
208 if (old->freq != new->freq)
209 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
211 if (old->level != new->level)
212 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
214 if (caps_diff & IEEE80211_CAP_PRIVACY)
215 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
217 if (caps_diff & IEEE80211_CAP_IBSS)
218 changes |= WPA_BSS_MODE_CHANGED_FLAG;
220 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
221 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
223 if (!are_ies_equal(old, new, WLAN_EID_RSN))
224 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
226 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
227 changes |= WPA_BSS_WPS_CHANGED_FLAG;
229 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
230 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
231 changes |= WPA_BSS_RATES_CHANGED_FLAG;
237 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
238 const struct wpa_bss *bss)
240 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
241 wpas_notify_bss_freq_changed(wpa_s, bss->id);
243 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
244 wpas_notify_bss_signal_changed(wpa_s, bss->id);
246 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
247 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
249 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
250 wpas_notify_bss_mode_changed(wpa_s, bss->id);
252 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
253 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
255 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
256 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
258 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
259 wpas_notify_bss_wps_changed(wpa_s, bss->id);
261 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
262 wpas_notify_bss_rates_changed(wpa_s, bss->id);
266 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
267 struct wpa_scan_res *res)
271 changes = wpa_bss_compare_res(bss, res);
272 bss->scan_miss_count = 0;
273 bss->last_update_idx = wpa_s->bss_update_idx;
274 wpa_bss_copy_res(bss, res);
275 /* Move the entry to the end of the list */
276 dl_list_del(&bss->list);
277 if (bss->ie_len + bss->beacon_ie_len >=
278 res->ie_len + res->beacon_ie_len) {
279 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
280 bss->ie_len = res->ie_len;
281 bss->beacon_ie_len = res->beacon_ie_len;
283 struct wpa_bss *nbss;
284 struct dl_list *prev = bss->list_id.prev;
285 dl_list_del(&bss->list_id);
286 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
290 os_memcpy(bss + 1, res + 1,
291 res->ie_len + res->beacon_ie_len);
292 bss->ie_len = res->ie_len;
293 bss->beacon_ie_len = res->beacon_ie_len;
295 dl_list_add(prev, &bss->list_id);
297 dl_list_add_tail(&wpa_s->bss, &bss->list);
299 notify_bss_changes(wpa_s, changes, bss);
303 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
305 return bss == wpa_s->current_bss ||
306 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
307 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
311 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
313 wpa_s->bss_update_idx++;
314 wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
315 wpa_s->bss_update_idx);
319 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
320 struct wpa_scan_res *res)
325 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
327 wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
328 MAC2STR(res->bssid));
332 wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
333 MACSTR, MAC2STR(res->bssid));
337 /* TODO: add option for ignoring BSSes we are not interested in
338 * (to save memory) */
339 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
341 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
343 wpa_bss_update(wpa_s, bss, res);
347 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
348 const struct scan_info *info)
356 if (info->num_freqs) {
358 for (i = 0; i < info->num_freqs; i++) {
359 if (bss->freq == info->freqs[i]) {
368 if (info->num_ssids) {
370 for (i = 0; i < info->num_ssids; i++) {
371 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
372 if ((s->ssid == NULL || s->ssid_len == 0) ||
373 (s->ssid_len == bss->ssid_len &&
374 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
388 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
391 struct wpa_bss *bss, *n;
394 return; /* do not expire entries without new scan */
396 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
397 if (wpa_bss_in_use(wpa_s, bss))
399 if (!wpa_bss_included_in_scan(bss, info))
400 continue; /* expire only BSSes that were scanned */
401 if (bss->last_update_idx < wpa_s->bss_update_idx)
402 bss->scan_miss_count++;
403 if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
404 wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
405 "match in scan", bss->id);
406 wpa_bss_remove(wpa_s, bss);
412 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
414 struct wpa_supplicant *wpa_s = eloop_ctx;
415 struct wpa_bss *bss, *n;
418 if (dl_list_empty(&wpa_s->bss))
422 t.sec -= WPA_BSS_EXPIRATION_AGE;
424 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
425 if (wpa_bss_in_use(wpa_s, bss))
428 if (os_time_before(&bss->last_update, &t)) {
429 wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
431 wpa_bss_remove(wpa_s, bss);
435 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
436 wpa_bss_timeout, wpa_s, NULL);
440 int wpa_bss_init(struct wpa_supplicant *wpa_s)
442 dl_list_init(&wpa_s->bss);
443 dl_list_init(&wpa_s->bss_id);
444 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
445 wpa_bss_timeout, wpa_s, NULL);
450 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
452 struct wpa_bss *bss, *n;
453 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
454 if (wpa_s->bss.next == NULL)
455 return; /* BSS table not yet initialized */
456 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
457 wpa_bss_remove(wpa_s, bss);
461 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
465 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
466 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
473 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
476 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
484 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
488 pos = (const u8 *) (bss + 1);
489 end = pos + bss->ie_len;
491 while (pos + 1 < end) {
492 if (pos + 2 + pos[1] > end)
503 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
507 pos = (const u8 *) (bss + 1);
508 end = pos + bss->ie_len;
510 while (pos + 1 < end) {
511 if (pos + 2 + pos[1] > end)
513 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
514 vendor_type == WPA_GET_BE32(&pos[2]))
523 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
529 buf = wpabuf_alloc(bss->ie_len);
533 pos = (const u8 *) (bss + 1);
534 end = pos + bss->ie_len;
536 while (pos + 1 < end) {
537 if (pos + 2 + pos[1] > end)
539 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
540 vendor_type == WPA_GET_BE32(&pos[2]))
541 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
545 if (wpabuf_len(buf) == 0) {
554 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
560 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
561 for (i = 0; ie && i < ie[1]; i++) {
562 if ((ie[i + 2] & 0x7f) > rate)
563 rate = ie[i + 2] & 0x7f;
566 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
567 for (i = 0; ie && i < ie[1]; i++) {
568 if ((ie[i + 2] & 0x7f) > rate)
569 rate = ie[i + 2] & 0x7f;
576 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
583 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
584 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
586 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
592 for (i = 0; ie && i < ie[1]; i++)
593 r[i] = ie[i + 2] & 0x7f;
595 for (j = 0; ie2 && j < ie2[1]; j++)
596 r[i + j] = ie2[j + 2] & 0x7f;