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"
26 #ifndef WPA_BSS_MAX_COUNT
27 #define WPA_BSS_MAX_COUNT 200
28 #endif /* WPA_BSS_MAX_COUNT */
31 * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
33 #define WPA_BSS_EXPIRATION_PERIOD 10
36 * WPA_BSS_EXPIRATION_AGE - BSS entry age after which it can be expired
38 * This value control the time in seconds after which a BSS entry gets removed
39 * if it has not been updated or is not in use.
41 #define WPA_BSS_EXPIRATION_AGE 180
44 * WPA_BSS_EXPIRATION_SCAN_COUNT - Expire BSS after number of scans
46 * If the BSS entry has not been seen in this many scans, it will be removed.
47 * Value 1 means that the entry is removed after the first scan without the
48 * BSSID being seen. Larger values can be used to avoid BSS entries
49 * disappearing if they are not visible in every scan (e.g., low signal quality
52 #define WPA_BSS_EXPIRATION_SCAN_COUNT 2
55 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
57 dl_list_del(&bss->list);
58 dl_list_del(&bss->list_id);
60 wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'",
61 bss->id, MAC2STR(bss->bssid),
62 wpa_ssid_txt(bss->ssid, bss->ssid_len));
63 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
68 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
69 const u8 *ssid, size_t ssid_len)
72 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
73 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
74 bss->ssid_len == ssid_len &&
75 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
82 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
86 dst->flags = src->flags;
87 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
88 dst->freq = src->freq;
89 dst->beacon_int = src->beacon_int;
90 dst->caps = src->caps;
91 dst->qual = src->qual;
92 dst->noise = src->noise;
93 dst->level = src->level;
96 os_get_time(&dst->last_update);
97 dst->last_update.sec -= src->age / 1000;
98 usec = (src->age % 1000) * 1000;
99 if (dst->last_update.usec < usec) {
100 dst->last_update.sec--;
101 dst->last_update.usec += 1000000;
103 dst->last_update.usec -= usec;
107 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
108 const u8 *ssid, size_t ssid_len,
109 struct wpa_scan_res *res)
113 bss = os_zalloc(sizeof(*bss) + res->ie_len);
116 bss->id = wpa_s->bss_next_id++;
117 bss->last_update_idx = wpa_s->bss_update_idx;
118 wpa_bss_copy_res(bss, res);
119 os_memcpy(bss->ssid, ssid, ssid_len);
120 bss->ssid_len = ssid_len;
121 bss->ie_len = res->ie_len;
122 os_memcpy(bss + 1, res + 1, res->ie_len);
124 dl_list_add_tail(&wpa_s->bss, &bss->list);
125 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
127 wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'",
128 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
129 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
130 if (wpa_s->num_bss > WPA_BSS_MAX_COUNT) {
131 /* Remove the oldest entry */
132 wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
133 struct wpa_bss, list));
138 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
139 struct wpa_scan_res *res)
141 bss->scan_miss_count = 0;
142 bss->last_update_idx = wpa_s->bss_update_idx;
143 wpa_bss_copy_res(bss, res);
144 /* Move the entry to the end of the list */
145 dl_list_del(&bss->list);
146 if (bss->ie_len >= res->ie_len) {
147 os_memcpy(bss + 1, res + 1, res->ie_len);
148 bss->ie_len = res->ie_len;
150 struct wpa_bss *nbss;
151 struct dl_list *prev = bss->list_id.prev;
152 dl_list_del(&bss->list_id);
153 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len);
156 os_memcpy(bss + 1, res + 1, res->ie_len);
157 bss->ie_len = res->ie_len;
159 dl_list_add(prev, &bss->list_id);
161 dl_list_add_tail(&wpa_s->bss, &bss->list);
165 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
167 wpa_s->bss_update_idx++;
168 wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
169 wpa_s->bss_update_idx);
173 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
174 struct wpa_scan_res *res)
179 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
181 wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
182 MAC2STR(res->bssid));
186 wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
187 MACSTR, MAC2STR(res->bssid));
191 /* TODO: add option for ignoring BSSes we are not interested in
192 * (to save memory) */
193 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
195 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
197 wpa_bss_update(wpa_s, bss, res);
201 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
202 const struct scan_info *info)
210 if (info->num_freqs) {
212 for (i = 0; i < info->num_freqs; i++) {
213 if (bss->freq == info->freqs[i]) {
222 if (info->num_ssids) {
224 for (i = 0; i < info->num_ssids; i++) {
225 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
226 if ((s->ssid == NULL || s->ssid_len == 0) ||
227 (s->ssid_len == bss->ssid_len &&
228 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
242 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
245 struct wpa_bss *bss, *n;
248 return; /* do not expire entries without new scan */
250 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
251 if (!wpa_bss_included_in_scan(bss, info))
252 continue; /* expire only BSSes that were scanned */
253 if (bss->last_update_idx < wpa_s->bss_update_idx)
254 bss->scan_miss_count++;
255 if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
256 wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
257 "match in scan", bss->id);
258 wpa_bss_remove(wpa_s, bss);
264 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
266 struct wpa_supplicant *wpa_s = eloop_ctx;
267 struct wpa_bss *bss, *n;
270 if (dl_list_empty(&wpa_s->bss))
274 t.sec -= WPA_BSS_EXPIRATION_AGE;
276 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
277 if (bss == wpa_s->current_bss ||
278 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
279 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0)
280 continue; /* do not expire BSSes that are in use */
282 if (os_time_before(&bss->last_update, &t)) {
283 wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
285 wpa_bss_remove(wpa_s, bss);
289 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
290 wpa_bss_timeout, wpa_s, NULL);
294 int wpa_bss_init(struct wpa_supplicant *wpa_s)
296 dl_list_init(&wpa_s->bss);
297 dl_list_init(&wpa_s->bss_id);
298 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
299 wpa_bss_timeout, wpa_s, NULL);
304 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
306 struct wpa_bss *bss, *n;
307 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
308 if (wpa_s->bss.next == NULL)
309 return; /* BSS table not yet initialized */
310 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
311 wpa_bss_remove(wpa_s, bss);
315 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
319 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
320 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
327 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
330 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
338 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
342 pos = (const u8 *) (bss + 1);
343 end = pos + bss->ie_len;
345 while (pos + 1 < end) {
346 if (pos + 2 + pos[1] > end)
357 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
361 pos = (const u8 *) (bss + 1);
362 end = pos + bss->ie_len;
364 while (pos + 1 < end) {
365 if (pos + 2 + pos[1] > end)
367 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
368 vendor_type == WPA_GET_BE32(&pos[2]))
377 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
383 buf = wpabuf_alloc(bss->ie_len);
387 pos = (const u8 *) (bss + 1);
388 end = pos + bss->ie_len;
390 while (pos + 1 < end) {
391 if (pos + 2 + pos[1] > end)
393 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
394 vendor_type == WPA_GET_BE32(&pos[2]))
395 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
399 if (wpabuf_len(buf) == 0) {
408 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
414 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
415 for (i = 0; ie && i < ie[1]; i++) {
416 if ((ie[i + 2] & 0x7f) > rate)
417 rate = ie[i + 2] & 0x7f;
420 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
421 for (i = 0; ie && i < ie[1]; i++) {
422 if ((ie[i + 2] & 0x7f) > rate)
423 rate = ie[i + 2] & 0x7f;