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);
57 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
58 const u8 *ssid, size_t ssid_len)
61 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
62 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
63 bss->ssid_len == ssid_len &&
64 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
71 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
75 dst->flags = src->flags;
76 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
77 dst->freq = src->freq;
78 dst->beacon_int = src->beacon_int;
79 dst->caps = src->caps;
80 dst->qual = src->qual;
81 dst->noise = src->noise;
82 dst->level = src->level;
85 os_get_time(&dst->last_update);
86 dst->last_update.sec -= src->age / 1000;
87 usec = (src->age % 1000) * 1000;
88 if (dst->last_update.usec < usec) {
89 dst->last_update.sec--;
90 dst->last_update.usec += 1000000;
92 dst->last_update.usec -= usec;
96 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
98 struct wpa_ssid *ssid;
100 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
101 if (ssid->ssid == NULL || ssid->ssid_len == 0)
103 if (ssid->ssid_len == bss->ssid_len &&
104 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
112 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
116 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
117 if (!wpa_bss_known(wpa_s, bss)) {
118 wpa_bss_remove(wpa_s, bss);
127 static void wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
130 * Remove the oldest entry that does not match with any configured
133 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
137 * Remove the oldest entry since no better candidate for removal was
140 wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
141 struct wpa_bss, list));
145 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
146 const u8 *ssid, size_t ssid_len,
147 struct wpa_scan_res *res)
151 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
154 bss->id = wpa_s->bss_next_id++;
155 bss->last_update_idx = wpa_s->bss_update_idx;
156 wpa_bss_copy_res(bss, res);
157 os_memcpy(bss->ssid, ssid, ssid_len);
158 bss->ssid_len = ssid_len;
159 bss->ie_len = res->ie_len;
160 bss->beacon_ie_len = res->beacon_ie_len;
161 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
163 dl_list_add_tail(&wpa_s->bss, &bss->list);
164 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
166 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
168 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
169 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
170 if (wpa_s->num_bss > wpa_s->conf->bss_max_count)
171 wpa_bss_remove_oldest(wpa_s);
175 static int are_ies_equal(const struct wpa_bss *old,
176 const struct wpa_scan_res *new, u32 ie)
178 const u8 *old_ie, *new_ie;
179 struct wpabuf *old_ie_buff = NULL;
180 struct wpabuf *new_ie_buff = NULL;
181 int new_ie_len, old_ie_len, ret, is_multi;
184 case WPA_IE_VENDOR_TYPE:
185 old_ie = wpa_bss_get_vendor_ie(old, ie);
186 new_ie = wpa_scan_get_vendor_ie(new, ie);
189 case WPS_IE_VENDOR_TYPE:
190 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
191 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
195 case WLAN_EID_SUPP_RATES:
196 case WLAN_EID_EXT_SUPP_RATES:
197 old_ie = wpa_bss_get_ie(old, ie);
198 new_ie = wpa_scan_get_ie(new, ie);
202 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
207 /* in case of multiple IEs stored in buffer */
208 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
209 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
210 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
211 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
213 /* in case of single IE */
214 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
215 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
218 if (!old_ie || !new_ie)
219 ret = !old_ie && !new_ie;
221 ret = (old_ie_len == new_ie_len &&
222 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
224 wpabuf_free(old_ie_buff);
225 wpabuf_free(new_ie_buff);
231 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
232 const struct wpa_scan_res *new)
235 int caps_diff = old->caps ^ new->caps;
237 if (old->freq != new->freq)
238 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
240 if (old->level != new->level)
241 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
243 if (caps_diff & IEEE80211_CAP_PRIVACY)
244 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
246 if (caps_diff & IEEE80211_CAP_IBSS)
247 changes |= WPA_BSS_MODE_CHANGED_FLAG;
249 if (old->ie_len == new->ie_len &&
250 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
252 changes |= WPA_BSS_IES_CHANGED_FLAG;
254 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
255 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
257 if (!are_ies_equal(old, new, WLAN_EID_RSN))
258 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
260 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
261 changes |= WPA_BSS_WPS_CHANGED_FLAG;
263 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
264 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
265 changes |= WPA_BSS_RATES_CHANGED_FLAG;
271 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
272 const struct wpa_bss *bss)
274 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
275 wpas_notify_bss_freq_changed(wpa_s, bss->id);
277 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
278 wpas_notify_bss_signal_changed(wpa_s, bss->id);
280 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
281 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
283 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
284 wpas_notify_bss_mode_changed(wpa_s, bss->id);
286 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
287 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
289 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
290 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
292 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
293 wpas_notify_bss_wps_changed(wpa_s, bss->id);
295 if (changes & WPA_BSS_IES_CHANGED_FLAG)
296 wpas_notify_bss_ies_changed(wpa_s, bss->id);
298 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
299 wpas_notify_bss_rates_changed(wpa_s, bss->id);
303 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
304 struct wpa_scan_res *res)
308 changes = wpa_bss_compare_res(bss, res);
309 bss->scan_miss_count = 0;
310 bss->last_update_idx = wpa_s->bss_update_idx;
311 wpa_bss_copy_res(bss, res);
312 /* Move the entry to the end of the list */
313 dl_list_del(&bss->list);
314 if (bss->ie_len + bss->beacon_ie_len >=
315 res->ie_len + res->beacon_ie_len) {
316 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
317 bss->ie_len = res->ie_len;
318 bss->beacon_ie_len = res->beacon_ie_len;
320 struct wpa_bss *nbss;
321 struct dl_list *prev = bss->list_id.prev;
322 dl_list_del(&bss->list_id);
323 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
327 os_memcpy(bss + 1, res + 1,
328 res->ie_len + res->beacon_ie_len);
329 bss->ie_len = res->ie_len;
330 bss->beacon_ie_len = res->beacon_ie_len;
332 dl_list_add(prev, &bss->list_id);
334 dl_list_add_tail(&wpa_s->bss, &bss->list);
336 notify_bss_changes(wpa_s, changes, bss);
340 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
342 return bss == wpa_s->current_bss ||
343 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
344 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
348 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
350 wpa_s->bss_update_idx++;
351 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
352 wpa_s->bss_update_idx);
356 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
357 struct wpa_scan_res *res)
359 const u8 *ssid, *p2p;
362 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
364 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
365 MACSTR, MAC2STR(res->bssid));
369 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
370 MACSTR, MAC2STR(res->bssid));
374 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
375 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
376 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
377 return; /* Skip P2P listen discovery results here */
379 /* TODO: add option for ignoring BSSes we are not interested in
380 * (to save memory) */
381 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
383 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
385 wpa_bss_update(wpa_s, bss, res);
389 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
390 const struct scan_info *info)
398 if (info->num_freqs) {
400 for (i = 0; i < info->num_freqs; i++) {
401 if (bss->freq == info->freqs[i]) {
410 if (info->num_ssids) {
412 for (i = 0; i < info->num_ssids; i++) {
413 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
414 if ((s->ssid == NULL || s->ssid_len == 0) ||
415 (s->ssid_len == bss->ssid_len &&
416 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
430 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
433 struct wpa_bss *bss, *n;
436 return; /* do not expire entries without new scan */
438 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
439 if (wpa_bss_in_use(wpa_s, bss))
441 if (!wpa_bss_included_in_scan(bss, info))
442 continue; /* expire only BSSes that were scanned */
443 if (bss->last_update_idx < wpa_s->bss_update_idx)
444 bss->scan_miss_count++;
445 if (bss->scan_miss_count >=
446 wpa_s->conf->bss_expiration_scan_count) {
447 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
448 "no match in scan", bss->id);
449 wpa_bss_remove(wpa_s, bss);
455 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
457 struct wpa_bss *bss, *n;
460 if (dl_list_empty(&wpa_s->bss))
466 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
467 if (wpa_bss_in_use(wpa_s, bss))
470 if (os_time_before(&bss->last_update, &t)) {
471 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
473 wpa_bss_remove(wpa_s, bss);
480 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
482 struct wpa_supplicant *wpa_s = eloop_ctx;
484 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
485 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
486 wpa_bss_timeout, wpa_s, NULL);
490 int wpa_bss_init(struct wpa_supplicant *wpa_s)
492 dl_list_init(&wpa_s->bss);
493 dl_list_init(&wpa_s->bss_id);
494 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
495 wpa_bss_timeout, wpa_s, NULL);
500 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
502 struct wpa_bss *bss, *n;
504 if (wpa_s->bss.next == NULL)
505 return; /* BSS table not yet initialized */
507 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
508 if (wpa_bss_in_use(wpa_s, bss))
510 wpa_bss_remove(wpa_s, bss);
515 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
517 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
518 wpa_bss_flush(wpa_s);
522 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
526 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
527 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
534 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
537 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
545 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
549 pos = (const u8 *) (bss + 1);
550 end = pos + bss->ie_len;
552 while (pos + 1 < end) {
553 if (pos + 2 + pos[1] > end)
564 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
568 pos = (const u8 *) (bss + 1);
569 end = pos + bss->ie_len;
571 while (pos + 1 < end) {
572 if (pos + 2 + pos[1] > end)
574 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
575 vendor_type == WPA_GET_BE32(&pos[2]))
584 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
590 buf = wpabuf_alloc(bss->ie_len);
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]))
602 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
606 if (wpabuf_len(buf) == 0) {
615 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
621 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
622 for (i = 0; ie && i < ie[1]; i++) {
623 if ((ie[i + 2] & 0x7f) > rate)
624 rate = ie[i + 2] & 0x7f;
627 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
628 for (i = 0; ie && i < ie[1]; i++) {
629 if ((ie[i + 2] & 0x7f) > rate)
630 rate = ie[i + 2] & 0x7f;
637 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
644 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
645 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
647 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
653 for (i = 0; ie && i < ie[1]; i++)
654 r[i] = ie[i + 2] & 0x7f;
656 for (j = 0; ie2 && j < ie2[1]; j++)
657 r[i + j] = ie2[j + 2] & 0x7f;