3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "drivers/driver.h"
15 #include "wpa_supplicant_i.h"
23 * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
25 #define WPA_BSS_EXPIRATION_PERIOD 10
27 #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
28 #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
29 #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
30 #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
31 #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
32 #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
33 #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
34 #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
35 #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
38 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
40 dl_list_del(&bss->list);
41 dl_list_del(&bss->list_id);
43 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
44 " SSID '%s'", bss->id, MAC2STR(bss->bssid),
45 wpa_ssid_txt(bss->ssid, bss->ssid_len));
46 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
47 #ifdef CONFIG_INTERWORKING
48 wpabuf_free(bss->anqp_venue_name);
49 wpabuf_free(bss->anqp_network_auth_type);
50 wpabuf_free(bss->anqp_roaming_consortium);
51 wpabuf_free(bss->anqp_ip_addr_type_availability);
52 wpabuf_free(bss->anqp_nai_realm);
53 wpabuf_free(bss->anqp_3gpp);
54 wpabuf_free(bss->anqp_domain_name);
55 #endif /* CONFIG_INTERWORKING */
60 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
61 const u8 *ssid, size_t ssid_len)
64 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
66 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
67 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
68 bss->ssid_len == ssid_len &&
69 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
76 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
80 dst->flags = src->flags;
81 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
82 dst->freq = src->freq;
83 dst->beacon_int = src->beacon_int;
84 dst->caps = src->caps;
85 dst->qual = src->qual;
86 dst->noise = src->noise;
87 dst->level = src->level;
90 os_get_time(&dst->last_update);
91 dst->last_update.sec -= src->age / 1000;
92 usec = (src->age % 1000) * 1000;
93 if (dst->last_update.usec < usec) {
94 dst->last_update.sec--;
95 dst->last_update.usec += 1000000;
97 dst->last_update.usec -= usec;
101 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
103 struct wpa_ssid *ssid;
105 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
106 if (ssid->ssid == NULL || ssid->ssid_len == 0)
108 if (ssid->ssid_len == bss->ssid_len &&
109 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
117 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
121 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
122 if (!wpa_bss_known(wpa_s, bss)) {
123 wpa_bss_remove(wpa_s, bss);
132 static void wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
135 * Remove the oldest entry that does not match with any configured
138 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
142 * Remove the oldest entry since no better candidate for removal was
145 wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
146 struct wpa_bss, list));
150 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
151 const u8 *ssid, size_t ssid_len,
152 struct wpa_scan_res *res)
156 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
159 bss->id = wpa_s->bss_next_id++;
160 bss->last_update_idx = wpa_s->bss_update_idx;
161 wpa_bss_copy_res(bss, res);
162 os_memcpy(bss->ssid, ssid, ssid_len);
163 bss->ssid_len = ssid_len;
164 bss->ie_len = res->ie_len;
165 bss->beacon_ie_len = res->beacon_ie_len;
166 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
168 dl_list_add_tail(&wpa_s->bss, &bss->list);
169 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
171 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
173 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
174 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
175 if (wpa_s->num_bss > wpa_s->conf->bss_max_count)
176 wpa_bss_remove_oldest(wpa_s);
180 static int are_ies_equal(const struct wpa_bss *old,
181 const struct wpa_scan_res *new, u32 ie)
183 const u8 *old_ie, *new_ie;
184 struct wpabuf *old_ie_buff = NULL;
185 struct wpabuf *new_ie_buff = NULL;
186 int new_ie_len, old_ie_len, ret, is_multi;
189 case WPA_IE_VENDOR_TYPE:
190 old_ie = wpa_bss_get_vendor_ie(old, ie);
191 new_ie = wpa_scan_get_vendor_ie(new, ie);
194 case WPS_IE_VENDOR_TYPE:
195 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
196 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
200 case WLAN_EID_SUPP_RATES:
201 case WLAN_EID_EXT_SUPP_RATES:
202 old_ie = wpa_bss_get_ie(old, ie);
203 new_ie = wpa_scan_get_ie(new, ie);
207 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
212 /* in case of multiple IEs stored in buffer */
213 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
214 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
215 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
216 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
218 /* in case of single IE */
219 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
220 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
223 if (!old_ie || !new_ie)
224 ret = !old_ie && !new_ie;
226 ret = (old_ie_len == new_ie_len &&
227 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
229 wpabuf_free(old_ie_buff);
230 wpabuf_free(new_ie_buff);
236 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
237 const struct wpa_scan_res *new)
240 int caps_diff = old->caps ^ new->caps;
242 if (old->freq != new->freq)
243 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
245 if (old->level != new->level)
246 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
248 if (caps_diff & IEEE80211_CAP_PRIVACY)
249 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
251 if (caps_diff & IEEE80211_CAP_IBSS)
252 changes |= WPA_BSS_MODE_CHANGED_FLAG;
254 if (old->ie_len == new->ie_len &&
255 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
257 changes |= WPA_BSS_IES_CHANGED_FLAG;
259 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
260 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
262 if (!are_ies_equal(old, new, WLAN_EID_RSN))
263 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
265 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
266 changes |= WPA_BSS_WPS_CHANGED_FLAG;
268 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
269 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
270 changes |= WPA_BSS_RATES_CHANGED_FLAG;
276 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
277 const struct wpa_bss *bss)
279 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
280 wpas_notify_bss_freq_changed(wpa_s, bss->id);
282 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
283 wpas_notify_bss_signal_changed(wpa_s, bss->id);
285 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
286 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
288 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
289 wpas_notify_bss_mode_changed(wpa_s, bss->id);
291 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
292 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
294 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
295 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
297 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
298 wpas_notify_bss_wps_changed(wpa_s, bss->id);
300 if (changes & WPA_BSS_IES_CHANGED_FLAG)
301 wpas_notify_bss_ies_changed(wpa_s, bss->id);
303 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
304 wpas_notify_bss_rates_changed(wpa_s, bss->id);
308 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
309 struct wpa_scan_res *res)
313 changes = wpa_bss_compare_res(bss, res);
314 bss->scan_miss_count = 0;
315 bss->last_update_idx = wpa_s->bss_update_idx;
316 wpa_bss_copy_res(bss, res);
317 /* Move the entry to the end of the list */
318 dl_list_del(&bss->list);
319 if (bss->ie_len + bss->beacon_ie_len >=
320 res->ie_len + res->beacon_ie_len) {
321 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
322 bss->ie_len = res->ie_len;
323 bss->beacon_ie_len = res->beacon_ie_len;
325 struct wpa_bss *nbss;
326 struct dl_list *prev = bss->list_id.prev;
327 dl_list_del(&bss->list_id);
328 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
332 os_memcpy(bss + 1, res + 1,
333 res->ie_len + res->beacon_ie_len);
334 bss->ie_len = res->ie_len;
335 bss->beacon_ie_len = res->beacon_ie_len;
337 dl_list_add(prev, &bss->list_id);
339 dl_list_add_tail(&wpa_s->bss, &bss->list);
341 notify_bss_changes(wpa_s, changes, bss);
345 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
347 return bss == wpa_s->current_bss ||
348 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
349 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
353 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
355 wpa_s->bss_update_idx++;
356 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
357 wpa_s->bss_update_idx);
361 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
362 struct wpa_scan_res *res)
364 const u8 *ssid, *p2p;
367 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
369 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
370 MACSTR, MAC2STR(res->bssid));
374 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
375 MACSTR, MAC2STR(res->bssid));
379 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
380 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
381 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
382 return; /* Skip P2P listen discovery results here */
384 /* TODO: add option for ignoring BSSes we are not interested in
385 * (to save memory) */
386 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
388 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
390 wpa_bss_update(wpa_s, bss, res);
394 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
395 const struct scan_info *info)
403 if (info->num_freqs) {
405 for (i = 0; i < info->num_freqs; i++) {
406 if (bss->freq == info->freqs[i]) {
415 if (info->num_ssids) {
417 for (i = 0; i < info->num_ssids; i++) {
418 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
419 if ((s->ssid == NULL || s->ssid_len == 0) ||
420 (s->ssid_len == bss->ssid_len &&
421 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
435 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
438 struct wpa_bss *bss, *n;
441 return; /* do not expire entries without new scan */
443 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
444 if (wpa_bss_in_use(wpa_s, bss))
446 if (!wpa_bss_included_in_scan(bss, info))
447 continue; /* expire only BSSes that were scanned */
448 if (bss->last_update_idx < wpa_s->bss_update_idx)
449 bss->scan_miss_count++;
450 if (bss->scan_miss_count >=
451 wpa_s->conf->bss_expiration_scan_count) {
452 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
453 "no match in scan", bss->id);
454 wpa_bss_remove(wpa_s, bss);
460 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
462 struct wpa_bss *bss, *n;
465 if (dl_list_empty(&wpa_s->bss))
471 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
472 if (wpa_bss_in_use(wpa_s, bss))
475 if (os_time_before(&bss->last_update, &t)) {
476 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
478 wpa_bss_remove(wpa_s, bss);
485 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
487 struct wpa_supplicant *wpa_s = eloop_ctx;
489 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
490 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
491 wpa_bss_timeout, wpa_s, NULL);
495 int wpa_bss_init(struct wpa_supplicant *wpa_s)
497 dl_list_init(&wpa_s->bss);
498 dl_list_init(&wpa_s->bss_id);
499 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
500 wpa_bss_timeout, wpa_s, NULL);
505 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
507 struct wpa_bss *bss, *n;
509 if (wpa_s->bss.next == NULL)
510 return; /* BSS table not yet initialized */
512 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
513 if (wpa_bss_in_use(wpa_s, bss))
515 wpa_bss_remove(wpa_s, bss);
520 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
522 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
523 wpa_bss_flush(wpa_s);
527 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
531 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
533 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
534 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
542 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
546 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
548 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
550 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
555 #endif /* CONFIG_P2P */
558 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
561 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
569 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
573 pos = (const u8 *) (bss + 1);
574 end = pos + bss->ie_len;
576 while (pos + 1 < end) {
577 if (pos + 2 + pos[1] > end)
588 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
592 pos = (const u8 *) (bss + 1);
593 end = pos + bss->ie_len;
595 while (pos + 1 < end) {
596 if (pos + 2 + pos[1] > end)
598 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
599 vendor_type == WPA_GET_BE32(&pos[2]))
608 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
614 buf = wpabuf_alloc(bss->ie_len);
618 pos = (const u8 *) (bss + 1);
619 end = pos + bss->ie_len;
621 while (pos + 1 < end) {
622 if (pos + 2 + pos[1] > end)
624 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
625 vendor_type == WPA_GET_BE32(&pos[2]))
626 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
630 if (wpabuf_len(buf) == 0) {
639 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
645 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
646 for (i = 0; ie && i < ie[1]; i++) {
647 if ((ie[i + 2] & 0x7f) > rate)
648 rate = ie[i + 2] & 0x7f;
651 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
652 for (i = 0; ie && i < ie[1]; i++) {
653 if ((ie[i + 2] & 0x7f) > rate)
654 rate = ie[i + 2] & 0x7f;
661 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
668 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
669 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
671 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
677 for (i = 0; ie && i < ie[1]; i++)
678 r[i] = ie[i + 2] & 0x7f;
680 for (j = 0; ie2 && j < ie2[1]; j++)
681 r[i + j] = ie2[j + 2] & 0x7f;