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,
41 dl_list_del(&bss->list);
42 dl_list_del(&bss->list_id);
44 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
45 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
46 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
47 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
48 #ifdef CONFIG_INTERWORKING
49 wpabuf_free(bss->anqp_venue_name);
50 wpabuf_free(bss->anqp_network_auth_type);
51 wpabuf_free(bss->anqp_roaming_consortium);
52 wpabuf_free(bss->anqp_ip_addr_type_availability);
53 wpabuf_free(bss->anqp_nai_realm);
54 wpabuf_free(bss->anqp_3gpp);
55 wpabuf_free(bss->anqp_domain_name);
56 #endif /* CONFIG_INTERWORKING */
58 wpabuf_free(bss->hs20_operator_friendly_name);
59 wpabuf_free(bss->hs20_wan_metrics);
60 wpabuf_free(bss->hs20_connection_capability);
61 wpabuf_free(bss->hs20_operating_class);
62 #endif /* CONFIG_HS20 */
67 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
68 const u8 *ssid, size_t ssid_len)
71 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
73 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
74 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
75 bss->ssid_len == ssid_len &&
76 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
83 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
87 dst->flags = src->flags;
88 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
89 dst->freq = src->freq;
90 dst->beacon_int = src->beacon_int;
91 dst->caps = src->caps;
92 dst->qual = src->qual;
93 dst->noise = src->noise;
94 dst->level = src->level;
97 os_get_time(&dst->last_update);
98 dst->last_update.sec -= src->age / 1000;
99 usec = (src->age % 1000) * 1000;
100 if (dst->last_update.usec < usec) {
101 dst->last_update.sec--;
102 dst->last_update.usec += 1000000;
104 dst->last_update.usec -= usec;
108 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
110 struct wpa_ssid *ssid;
112 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
113 if (ssid->ssid == NULL || ssid->ssid_len == 0)
115 if (ssid->ssid_len == bss->ssid_len &&
116 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
124 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
126 return bss == wpa_s->current_bss ||
127 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
128 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
132 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
136 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
137 if (!wpa_bss_known(wpa_s, bss)) {
138 wpa_bss_remove(wpa_s, bss, __func__);
147 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
152 * Remove the oldest entry that does not match with any configured
155 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
159 * Remove the oldest entry that isn't currently in use.
161 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
162 if (!wpa_bss_in_use(wpa_s, bss)) {
163 wpa_bss_remove(wpa_s, bss, __func__);
172 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
173 const u8 *ssid, size_t ssid_len,
174 struct wpa_scan_res *res)
178 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
181 bss->id = wpa_s->bss_next_id++;
182 bss->last_update_idx = wpa_s->bss_update_idx;
183 wpa_bss_copy_res(bss, res);
184 os_memcpy(bss->ssid, ssid, ssid_len);
185 bss->ssid_len = ssid_len;
186 bss->ie_len = res->ie_len;
187 bss->beacon_ie_len = res->beacon_ie_len;
188 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
190 dl_list_add_tail(&wpa_s->bss, &bss->list);
191 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
193 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
195 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
196 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
197 if (wpa_s->num_bss > wpa_s->conf->bss_max_count &&
198 wpa_bss_remove_oldest(wpa_s) != 0) {
199 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
200 "because all BSSes are in use. We should normally "
201 "not get here!", (int) wpa_s->num_bss);
202 wpa_s->conf->bss_max_count = wpa_s->num_bss;
207 static int are_ies_equal(const struct wpa_bss *old,
208 const struct wpa_scan_res *new, u32 ie)
210 const u8 *old_ie, *new_ie;
211 struct wpabuf *old_ie_buff = NULL;
212 struct wpabuf *new_ie_buff = NULL;
213 int new_ie_len, old_ie_len, ret, is_multi;
216 case WPA_IE_VENDOR_TYPE:
217 old_ie = wpa_bss_get_vendor_ie(old, ie);
218 new_ie = wpa_scan_get_vendor_ie(new, ie);
221 case WPS_IE_VENDOR_TYPE:
222 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
223 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
227 case WLAN_EID_SUPP_RATES:
228 case WLAN_EID_EXT_SUPP_RATES:
229 old_ie = wpa_bss_get_ie(old, ie);
230 new_ie = wpa_scan_get_ie(new, ie);
234 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
239 /* in case of multiple IEs stored in buffer */
240 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
241 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
242 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
243 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
245 /* in case of single IE */
246 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
247 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
250 if (!old_ie || !new_ie)
251 ret = !old_ie && !new_ie;
253 ret = (old_ie_len == new_ie_len &&
254 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
256 wpabuf_free(old_ie_buff);
257 wpabuf_free(new_ie_buff);
263 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
264 const struct wpa_scan_res *new)
267 int caps_diff = old->caps ^ new->caps;
269 if (old->freq != new->freq)
270 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
272 if (old->level != new->level)
273 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
275 if (caps_diff & IEEE80211_CAP_PRIVACY)
276 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
278 if (caps_diff & IEEE80211_CAP_IBSS)
279 changes |= WPA_BSS_MODE_CHANGED_FLAG;
281 if (old->ie_len == new->ie_len &&
282 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
284 changes |= WPA_BSS_IES_CHANGED_FLAG;
286 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
287 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
289 if (!are_ies_equal(old, new, WLAN_EID_RSN))
290 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
292 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
293 changes |= WPA_BSS_WPS_CHANGED_FLAG;
295 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
296 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
297 changes |= WPA_BSS_RATES_CHANGED_FLAG;
303 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
304 const struct wpa_bss *bss)
306 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
307 wpas_notify_bss_freq_changed(wpa_s, bss->id);
309 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
310 wpas_notify_bss_signal_changed(wpa_s, bss->id);
312 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
313 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
315 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
316 wpas_notify_bss_mode_changed(wpa_s, bss->id);
318 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
319 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
321 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
322 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
324 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
325 wpas_notify_bss_wps_changed(wpa_s, bss->id);
327 if (changes & WPA_BSS_IES_CHANGED_FLAG)
328 wpas_notify_bss_ies_changed(wpa_s, bss->id);
330 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
331 wpas_notify_bss_rates_changed(wpa_s, bss->id);
335 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
336 struct wpa_scan_res *res)
340 changes = wpa_bss_compare_res(bss, res);
341 bss->scan_miss_count = 0;
342 bss->last_update_idx = wpa_s->bss_update_idx;
343 wpa_bss_copy_res(bss, res);
344 /* Move the entry to the end of the list */
345 dl_list_del(&bss->list);
346 if (bss->ie_len + bss->beacon_ie_len >=
347 res->ie_len + res->beacon_ie_len) {
348 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
349 bss->ie_len = res->ie_len;
350 bss->beacon_ie_len = res->beacon_ie_len;
352 struct wpa_bss *nbss;
353 struct dl_list *prev = bss->list_id.prev;
354 dl_list_del(&bss->list_id);
355 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
358 if (wpa_s->current_bss == bss)
359 wpa_s->current_bss = nbss;
361 os_memcpy(bss + 1, res + 1,
362 res->ie_len + res->beacon_ie_len);
363 bss->ie_len = res->ie_len;
364 bss->beacon_ie_len = res->beacon_ie_len;
366 dl_list_add(prev, &bss->list_id);
368 dl_list_add_tail(&wpa_s->bss, &bss->list);
370 notify_bss_changes(wpa_s, changes, bss);
374 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
376 wpa_s->bss_update_idx++;
377 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
378 wpa_s->bss_update_idx);
382 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
383 struct wpa_scan_res *res)
385 const u8 *ssid, *p2p;
388 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
390 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
391 MACSTR, MAC2STR(res->bssid));
395 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
396 MACSTR, MAC2STR(res->bssid));
400 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
403 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
405 * If it's a P2P specific interface, then don't update
406 * the scan result without a P2P IE.
408 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
409 " update for P2P interface", MAC2STR(res->bssid));
412 #endif /* CONFIG_P2P */
413 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
414 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
415 return; /* Skip P2P listen discovery results here */
417 /* TODO: add option for ignoring BSSes we are not interested in
418 * (to save memory) */
419 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
421 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
423 wpa_bss_update(wpa_s, bss, res);
427 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
428 const struct scan_info *info)
436 if (info->num_freqs) {
438 for (i = 0; i < info->num_freqs; i++) {
439 if (bss->freq == info->freqs[i]) {
448 if (info->num_ssids) {
450 for (i = 0; i < info->num_ssids; i++) {
451 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
452 if ((s->ssid == NULL || s->ssid_len == 0) ||
453 (s->ssid_len == bss->ssid_len &&
454 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
468 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
471 struct wpa_bss *bss, *n;
474 return; /* do not expire entries without new scan */
476 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
477 if (wpa_bss_in_use(wpa_s, bss))
479 if (!wpa_bss_included_in_scan(bss, info))
480 continue; /* expire only BSSes that were scanned */
481 if (bss->last_update_idx < wpa_s->bss_update_idx)
482 bss->scan_miss_count++;
483 if (bss->scan_miss_count >=
484 wpa_s->conf->bss_expiration_scan_count) {
485 wpa_bss_remove(wpa_s, bss, "no match in scan");
491 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
493 struct wpa_bss *bss, *n;
496 if (dl_list_empty(&wpa_s->bss))
502 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
503 if (wpa_bss_in_use(wpa_s, bss))
506 if (os_time_before(&bss->last_update, &t)) {
507 wpa_bss_remove(wpa_s, bss, __func__);
514 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
516 struct wpa_supplicant *wpa_s = eloop_ctx;
518 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
519 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
520 wpa_bss_timeout, wpa_s, NULL);
524 int wpa_bss_init(struct wpa_supplicant *wpa_s)
526 dl_list_init(&wpa_s->bss);
527 dl_list_init(&wpa_s->bss_id);
528 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
529 wpa_bss_timeout, wpa_s, NULL);
534 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
536 struct wpa_bss *bss, *n;
538 if (wpa_s->bss.next == NULL)
539 return; /* BSS table not yet initialized */
541 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
542 if (wpa_bss_in_use(wpa_s, bss))
544 wpa_bss_remove(wpa_s, bss, __func__);
549 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
551 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
552 wpa_bss_flush(wpa_s);
556 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
560 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
562 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
563 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
571 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
575 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
577 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
579 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
584 #endif /* CONFIG_P2P */
587 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
590 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
598 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
602 pos = (const u8 *) (bss + 1);
603 end = pos + bss->ie_len;
605 while (pos + 1 < end) {
606 if (pos + 2 + pos[1] > end)
617 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
621 pos = (const u8 *) (bss + 1);
622 end = pos + bss->ie_len;
624 while (pos + 1 < end) {
625 if (pos + 2 + pos[1] > end)
627 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
628 vendor_type == WPA_GET_BE32(&pos[2]))
637 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
643 buf = wpabuf_alloc(bss->ie_len);
647 pos = (const u8 *) (bss + 1);
648 end = pos + bss->ie_len;
650 while (pos + 1 < end) {
651 if (pos + 2 + pos[1] > end)
653 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
654 vendor_type == WPA_GET_BE32(&pos[2]))
655 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
659 if (wpabuf_len(buf) == 0) {
668 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
674 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
675 for (i = 0; ie && i < ie[1]; i++) {
676 if ((ie[i + 2] & 0x7f) > rate)
677 rate = ie[i + 2] & 0x7f;
680 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
681 for (i = 0; ie && i < ie[1]; i++) {
682 if ((ie[i + 2] & 0x7f) > rate)
683 rate = ie[i + 2] & 0x7f;
690 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
697 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
698 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
700 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
706 for (i = 0; ie && i < ie[1]; i++)
707 r[i] = ie[i + 2] & 0x7f;
709 for (j = 0; ie2 && j < ie2[1]; j++)
710 r[i + j] = ie2[j + 2] & 0x7f;