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"
29 * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
31 #define WPA_BSS_EXPIRATION_PERIOD 10
33 #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
34 #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
35 #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
36 #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
37 #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
38 #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
39 #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
40 #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
41 #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
44 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
46 dl_list_del(&bss->list);
47 dl_list_del(&bss->list_id);
49 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
50 " SSID '%s'", bss->id, MAC2STR(bss->bssid),
51 wpa_ssid_txt(bss->ssid, bss->ssid_len));
52 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
53 #ifdef CONFIG_INTERWORKING
54 wpabuf_free(bss->anqp_venue_name);
55 wpabuf_free(bss->anqp_network_auth_type);
56 wpabuf_free(bss->anqp_roaming_consortium);
57 wpabuf_free(bss->anqp_ip_addr_type_availability);
58 wpabuf_free(bss->anqp_nai_realm);
59 wpabuf_free(bss->anqp_3gpp);
60 wpabuf_free(bss->anqp_domain_name);
61 #endif /* CONFIG_INTERWORKING */
66 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
67 const u8 *ssid, size_t ssid_len)
70 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
71 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
72 bss->ssid_len == ssid_len &&
73 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
80 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
84 dst->flags = src->flags;
85 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
86 dst->freq = src->freq;
87 dst->beacon_int = src->beacon_int;
88 dst->caps = src->caps;
89 dst->qual = src->qual;
90 dst->noise = src->noise;
91 dst->level = src->level;
94 os_get_time(&dst->last_update);
95 dst->last_update.sec -= src->age / 1000;
96 usec = (src->age % 1000) * 1000;
97 if (dst->last_update.usec < usec) {
98 dst->last_update.sec--;
99 dst->last_update.usec += 1000000;
101 dst->last_update.usec -= usec;
105 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
107 struct wpa_ssid *ssid;
109 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
110 if (ssid->ssid == NULL || ssid->ssid_len == 0)
112 if (ssid->ssid_len == bss->ssid_len &&
113 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
121 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
125 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
126 if (!wpa_bss_known(wpa_s, bss)) {
127 wpa_bss_remove(wpa_s, bss);
136 static void wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
139 * Remove the oldest entry that does not match with any configured
142 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
146 * Remove the oldest entry since no better candidate for removal was
149 wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
150 struct wpa_bss, list));
154 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
155 const u8 *ssid, size_t ssid_len,
156 struct wpa_scan_res *res)
160 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
163 bss->id = wpa_s->bss_next_id++;
164 bss->last_update_idx = wpa_s->bss_update_idx;
165 wpa_bss_copy_res(bss, res);
166 os_memcpy(bss->ssid, ssid, ssid_len);
167 bss->ssid_len = ssid_len;
168 bss->ie_len = res->ie_len;
169 bss->beacon_ie_len = res->beacon_ie_len;
170 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
172 dl_list_add_tail(&wpa_s->bss, &bss->list);
173 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
175 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
177 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
178 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
179 if (wpa_s->num_bss > wpa_s->conf->bss_max_count)
180 wpa_bss_remove_oldest(wpa_s);
184 static int are_ies_equal(const struct wpa_bss *old,
185 const struct wpa_scan_res *new, u32 ie)
187 const u8 *old_ie, *new_ie;
188 struct wpabuf *old_ie_buff = NULL;
189 struct wpabuf *new_ie_buff = NULL;
190 int new_ie_len, old_ie_len, ret, is_multi;
193 case WPA_IE_VENDOR_TYPE:
194 old_ie = wpa_bss_get_vendor_ie(old, ie);
195 new_ie = wpa_scan_get_vendor_ie(new, ie);
198 case WPS_IE_VENDOR_TYPE:
199 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
200 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
204 case WLAN_EID_SUPP_RATES:
205 case WLAN_EID_EXT_SUPP_RATES:
206 old_ie = wpa_bss_get_ie(old, ie);
207 new_ie = wpa_scan_get_ie(new, ie);
211 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
216 /* in case of multiple IEs stored in buffer */
217 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
218 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
219 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
220 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
222 /* in case of single IE */
223 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
224 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
227 if (!old_ie || !new_ie)
228 ret = !old_ie && !new_ie;
230 ret = (old_ie_len == new_ie_len &&
231 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
233 wpabuf_free(old_ie_buff);
234 wpabuf_free(new_ie_buff);
240 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
241 const struct wpa_scan_res *new)
244 int caps_diff = old->caps ^ new->caps;
246 if (old->freq != new->freq)
247 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
249 if (old->level != new->level)
250 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
252 if (caps_diff & IEEE80211_CAP_PRIVACY)
253 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
255 if (caps_diff & IEEE80211_CAP_IBSS)
256 changes |= WPA_BSS_MODE_CHANGED_FLAG;
258 if (old->ie_len == new->ie_len &&
259 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
261 changes |= WPA_BSS_IES_CHANGED_FLAG;
263 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
264 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
266 if (!are_ies_equal(old, new, WLAN_EID_RSN))
267 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
269 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
270 changes |= WPA_BSS_WPS_CHANGED_FLAG;
272 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
273 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
274 changes |= WPA_BSS_RATES_CHANGED_FLAG;
280 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
281 const struct wpa_bss *bss)
283 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
284 wpas_notify_bss_freq_changed(wpa_s, bss->id);
286 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
287 wpas_notify_bss_signal_changed(wpa_s, bss->id);
289 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
290 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
292 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
293 wpas_notify_bss_mode_changed(wpa_s, bss->id);
295 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
296 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
298 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
299 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
301 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
302 wpas_notify_bss_wps_changed(wpa_s, bss->id);
304 if (changes & WPA_BSS_IES_CHANGED_FLAG)
305 wpas_notify_bss_ies_changed(wpa_s, bss->id);
307 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
308 wpas_notify_bss_rates_changed(wpa_s, bss->id);
312 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
313 struct wpa_scan_res *res)
317 changes = wpa_bss_compare_res(bss, res);
318 bss->scan_miss_count = 0;
319 bss->last_update_idx = wpa_s->bss_update_idx;
320 wpa_bss_copy_res(bss, res);
321 /* Move the entry to the end of the list */
322 dl_list_del(&bss->list);
323 if (bss->ie_len + bss->beacon_ie_len >=
324 res->ie_len + res->beacon_ie_len) {
325 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
326 bss->ie_len = res->ie_len;
327 bss->beacon_ie_len = res->beacon_ie_len;
329 struct wpa_bss *nbss;
330 struct dl_list *prev = bss->list_id.prev;
331 dl_list_del(&bss->list_id);
332 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
336 os_memcpy(bss + 1, res + 1,
337 res->ie_len + res->beacon_ie_len);
338 bss->ie_len = res->ie_len;
339 bss->beacon_ie_len = res->beacon_ie_len;
341 dl_list_add(prev, &bss->list_id);
343 dl_list_add_tail(&wpa_s->bss, &bss->list);
345 notify_bss_changes(wpa_s, changes, bss);
349 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
351 return bss == wpa_s->current_bss ||
352 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
353 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
357 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
359 wpa_s->bss_update_idx++;
360 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
361 wpa_s->bss_update_idx);
365 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
366 struct wpa_scan_res *res)
368 const u8 *ssid, *p2p;
371 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
373 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
374 MACSTR, MAC2STR(res->bssid));
378 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
379 MACSTR, MAC2STR(res->bssid));
383 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
384 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
385 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
386 return; /* Skip P2P listen discovery results here */
388 /* TODO: add option for ignoring BSSes we are not interested in
389 * (to save memory) */
390 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
392 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
394 wpa_bss_update(wpa_s, bss, res);
398 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
399 const struct scan_info *info)
407 if (info->num_freqs) {
409 for (i = 0; i < info->num_freqs; i++) {
410 if (bss->freq == info->freqs[i]) {
419 if (info->num_ssids) {
421 for (i = 0; i < info->num_ssids; i++) {
422 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
423 if ((s->ssid == NULL || s->ssid_len == 0) ||
424 (s->ssid_len == bss->ssid_len &&
425 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
439 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
442 struct wpa_bss *bss, *n;
445 return; /* do not expire entries without new scan */
447 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
448 if (wpa_bss_in_use(wpa_s, bss))
450 if (!wpa_bss_included_in_scan(bss, info))
451 continue; /* expire only BSSes that were scanned */
452 if (bss->last_update_idx < wpa_s->bss_update_idx)
453 bss->scan_miss_count++;
454 if (bss->scan_miss_count >=
455 wpa_s->conf->bss_expiration_scan_count) {
456 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
457 "no match in scan", bss->id);
458 wpa_bss_remove(wpa_s, bss);
464 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
466 struct wpa_bss *bss, *n;
469 if (dl_list_empty(&wpa_s->bss))
475 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
476 if (wpa_bss_in_use(wpa_s, bss))
479 if (os_time_before(&bss->last_update, &t)) {
480 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
482 wpa_bss_remove(wpa_s, bss);
489 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
491 struct wpa_supplicant *wpa_s = eloop_ctx;
493 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
494 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
495 wpa_bss_timeout, wpa_s, NULL);
499 int wpa_bss_init(struct wpa_supplicant *wpa_s)
501 dl_list_init(&wpa_s->bss);
502 dl_list_init(&wpa_s->bss_id);
503 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
504 wpa_bss_timeout, wpa_s, NULL);
509 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
511 struct wpa_bss *bss, *n;
513 if (wpa_s->bss.next == NULL)
514 return; /* BSS table not yet initialized */
516 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
517 if (wpa_bss_in_use(wpa_s, bss))
519 wpa_bss_remove(wpa_s, bss);
524 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
526 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
527 wpa_bss_flush(wpa_s);
531 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
535 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
536 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
544 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
548 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
550 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
552 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
557 #endif /* CONFIG_P2P */
560 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
563 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
571 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
575 pos = (const u8 *) (bss + 1);
576 end = pos + bss->ie_len;
578 while (pos + 1 < end) {
579 if (pos + 2 + pos[1] > end)
590 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
594 pos = (const u8 *) (bss + 1);
595 end = pos + bss->ie_len;
597 while (pos + 1 < end) {
598 if (pos + 2 + pos[1] > end)
600 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
601 vendor_type == WPA_GET_BE32(&pos[2]))
610 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
616 buf = wpabuf_alloc(bss->ie_len);
620 pos = (const u8 *) (bss + 1);
621 end = pos + bss->ie_len;
623 while (pos + 1 < end) {
624 if (pos + 2 + pos[1] > end)
626 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
627 vendor_type == WPA_GET_BE32(&pos[2]))
628 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
632 if (wpabuf_len(buf) == 0) {
641 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
647 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
648 for (i = 0; ie && i < ie[1]; i++) {
649 if ((ie[i + 2] & 0x7f) > rate)
650 rate = ie[i + 2] & 0x7f;
653 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
654 for (i = 0; ie && i < ie[1]; i++) {
655 if ((ie[i + 2] & 0x7f) > rate)
656 rate = ie[i + 2] & 0x7f;
663 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
670 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
671 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
673 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
679 for (i = 0; ie && i < ie[1]; i++)
680 r[i] = ie[i + 2] & 0x7f;
682 for (j = 0; ie2 && j < ie2[1]; j++)
683 r[i + j] = ie2[j + 2] & 0x7f;