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);
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 os_memcpy(bss + 1, res + 1, res->ie_len);
134 dl_list_add_tail(&wpa_s->bss, &bss->list);
135 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
137 wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'",
138 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
139 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
140 if (wpa_s->num_bss > WPA_BSS_MAX_COUNT) {
141 /* Remove the oldest entry */
142 wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
143 struct wpa_bss, list));
148 static int are_ies_equal(const struct wpa_bss *old,
149 const struct wpa_scan_res *new, u32 ie)
151 const u8 *old_ie, *new_ie;
152 struct wpabuf *old_ie_buff = NULL;
153 struct wpabuf *new_ie_buff = NULL;
154 int new_ie_len, old_ie_len, ret, is_multi;
157 case WPA_IE_VENDOR_TYPE:
158 old_ie = wpa_bss_get_vendor_ie(old, ie);
159 new_ie = wpa_scan_get_vendor_ie(new, ie);
162 case WPS_IE_VENDOR_TYPE:
163 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
164 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
168 case WLAN_EID_SUPP_RATES:
169 case WLAN_EID_EXT_SUPP_RATES:
170 old_ie = wpa_bss_get_ie(old, ie);
171 new_ie = wpa_scan_get_ie(new, ie);
175 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
180 /* in case of multiple IEs stored in buffer */
181 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
182 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
183 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
184 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
186 /* in case of single IE */
187 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
188 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
191 ret = (old_ie_len == new_ie_len &&
192 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
194 wpabuf_free(old_ie_buff);
195 wpabuf_free(new_ie_buff);
201 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
202 const struct wpa_scan_res *new)
205 int caps_diff = old->caps ^ new->caps;
207 if (old->freq != new->freq)
208 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
210 if (old->level != new->level)
211 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
213 if (caps_diff & IEEE80211_CAP_PRIVACY)
214 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
216 if (caps_diff & IEEE80211_CAP_IBSS)
217 changes |= WPA_BSS_MODE_CHANGED_FLAG;
219 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
220 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
222 if (!are_ies_equal(old, new, WLAN_EID_RSN))
223 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
225 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
226 changes |= WPA_BSS_WPS_CHANGED_FLAG;
228 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
229 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
230 changes |= WPA_BSS_RATES_CHANGED_FLAG;
236 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
237 const struct wpa_bss *bss)
239 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
240 wpas_notify_bss_freq_changed(wpa_s, bss->id);
242 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
243 wpas_notify_bss_signal_changed(wpa_s, bss->id);
245 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
246 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
248 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
249 wpas_notify_bss_mode_changed(wpa_s, bss->id);
251 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
252 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
254 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
255 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
257 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
258 wpas_notify_bss_wps_changed(wpa_s, bss->id);
260 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
261 wpas_notify_bss_rates_changed(wpa_s, bss->id);
265 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
266 struct wpa_scan_res *res)
270 changes = wpa_bss_compare_res(bss, res);
271 bss->scan_miss_count = 0;
272 bss->last_update_idx = wpa_s->bss_update_idx;
273 wpa_bss_copy_res(bss, res);
274 /* Move the entry to the end of the list */
275 dl_list_del(&bss->list);
276 if (bss->ie_len >= res->ie_len) {
277 os_memcpy(bss + 1, res + 1, res->ie_len);
278 bss->ie_len = res->ie_len;
280 struct wpa_bss *nbss;
281 struct dl_list *prev = bss->list_id.prev;
282 dl_list_del(&bss->list_id);
283 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len);
286 os_memcpy(bss + 1, res + 1, res->ie_len);
287 bss->ie_len = res->ie_len;
289 dl_list_add(prev, &bss->list_id);
291 dl_list_add_tail(&wpa_s->bss, &bss->list);
293 notify_bss_changes(wpa_s, changes, bss);
297 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
299 return bss == wpa_s->current_bss ||
300 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
301 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
305 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
307 wpa_s->bss_update_idx++;
308 wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
309 wpa_s->bss_update_idx);
313 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
314 struct wpa_scan_res *res)
319 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
321 wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
322 MAC2STR(res->bssid));
326 wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
327 MACSTR, MAC2STR(res->bssid));
331 /* TODO: add option for ignoring BSSes we are not interested in
332 * (to save memory) */
333 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
335 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
337 wpa_bss_update(wpa_s, bss, res);
341 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
342 const struct scan_info *info)
350 if (info->num_freqs) {
352 for (i = 0; i < info->num_freqs; i++) {
353 if (bss->freq == info->freqs[i]) {
362 if (info->num_ssids) {
364 for (i = 0; i < info->num_ssids; i++) {
365 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
366 if ((s->ssid == NULL || s->ssid_len == 0) ||
367 (s->ssid_len == bss->ssid_len &&
368 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
382 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
385 struct wpa_bss *bss, *n;
388 return; /* do not expire entries without new scan */
390 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
391 if (wpa_bss_in_use(wpa_s, bss))
393 if (!wpa_bss_included_in_scan(bss, info))
394 continue; /* expire only BSSes that were scanned */
395 if (bss->last_update_idx < wpa_s->bss_update_idx)
396 bss->scan_miss_count++;
397 if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
398 wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
399 "match in scan", bss->id);
400 wpa_bss_remove(wpa_s, bss);
406 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
408 struct wpa_supplicant *wpa_s = eloop_ctx;
409 struct wpa_bss *bss, *n;
412 if (dl_list_empty(&wpa_s->bss))
416 t.sec -= WPA_BSS_EXPIRATION_AGE;
418 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
419 if (wpa_bss_in_use(wpa_s, bss))
422 if (os_time_before(&bss->last_update, &t)) {
423 wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
425 wpa_bss_remove(wpa_s, bss);
429 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
430 wpa_bss_timeout, wpa_s, NULL);
434 int wpa_bss_init(struct wpa_supplicant *wpa_s)
436 dl_list_init(&wpa_s->bss);
437 dl_list_init(&wpa_s->bss_id);
438 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
439 wpa_bss_timeout, wpa_s, NULL);
444 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
446 struct wpa_bss *bss, *n;
447 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
448 if (wpa_s->bss.next == NULL)
449 return; /* BSS table not yet initialized */
450 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
451 wpa_bss_remove(wpa_s, bss);
455 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
459 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
460 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
467 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
470 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
478 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
482 pos = (const u8 *) (bss + 1);
483 end = pos + bss->ie_len;
485 while (pos + 1 < end) {
486 if (pos + 2 + pos[1] > end)
497 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
501 pos = (const u8 *) (bss + 1);
502 end = pos + bss->ie_len;
504 while (pos + 1 < end) {
505 if (pos + 2 + pos[1] > end)
507 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
508 vendor_type == WPA_GET_BE32(&pos[2]))
517 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
523 buf = wpabuf_alloc(bss->ie_len);
527 pos = (const u8 *) (bss + 1);
528 end = pos + bss->ie_len;
530 while (pos + 1 < end) {
531 if (pos + 2 + pos[1] > end)
533 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
534 vendor_type == WPA_GET_BE32(&pos[2]))
535 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
539 if (wpabuf_len(buf) == 0) {
548 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
554 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
555 for (i = 0; ie && i < ie[1]; i++) {
556 if ((ie[i + 2] & 0x7f) > rate)
557 rate = ie[i + 2] & 0x7f;
560 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
561 for (i = 0; ie && i < ie[1]; i++) {
562 if ((ie[i + 2] & 0x7f) > rate)
563 rate = ie[i + 2] & 0x7f;
570 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
577 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
578 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
580 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
586 for (i = 0; ie && i < ie[1]; i++)
587 r[i] = ie[i + 2] & 0x7f;
589 for (j = 0; ie2 && j < ie2[1]; j++)
590 r[i + j] = ie2[j + 2] & 0x7f;