123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- /*
- * BSS table
- * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
- */
- #include "utils/includes.h"
- #include "utils/common.h"
- #include "utils/eloop.h"
- #include "common/ieee802_11_defs.h"
- #include "drivers/driver.h"
- #include "wpa_supplicant_i.h"
- #include "notify.h"
- #include "bss.h"
- #ifndef WPA_BSS_MAX_COUNT
- #define WPA_BSS_MAX_COUNT 200
- #endif /* WPA_BSS_MAX_COUNT */
- /**
- * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
- */
- #define WPA_BSS_EXPIRATION_PERIOD 10
- /**
- * WPA_BSS_EXPIRATION_AGE - BSS entry age after which it can be expired
- *
- * This value control the time in seconds after which a BSS entry gets removed
- * if it has not been updated or is not in use.
- */
- #define WPA_BSS_EXPIRATION_AGE 180
- /**
- * WPA_BSS_EXPIRATION_SCAN_COUNT - Expire BSS after number of scans
- *
- * If the BSS entry has not been seen in this many scans, it will be removed.
- * Value 1 means that the entry is removed after the first scan without the
- * BSSID being seen. Larger values can be used to avoid BSS entries
- * disappearing if they are not visible in every scan (e.g., low signal quality
- * or interference).
- */
- #define WPA_BSS_EXPIRATION_SCAN_COUNT 2
- static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
- {
- dl_list_del(&bss->list);
- dl_list_del(&bss->list_id);
- wpa_s->num_bss--;
- wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'",
- bss->id, MAC2STR(bss->bssid),
- wpa_ssid_txt(bss->ssid, bss->ssid_len));
- wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
- os_free(bss);
- }
- static struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s,
- const u8 *bssid, const u8 *ssid,
- size_t ssid_len)
- {
- struct wpa_bss *bss;
- dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
- if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
- bss->ssid_len == ssid_len &&
- os_memcmp(bss->ssid, ssid, ssid_len) == 0)
- return bss;
- }
- return NULL;
- }
- static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
- {
- os_time_t usec;
- dst->flags = src->flags;
- os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
- dst->freq = src->freq;
- dst->beacon_int = src->beacon_int;
- dst->caps = src->caps;
- dst->qual = src->qual;
- dst->noise = src->noise;
- dst->level = src->level;
- dst->tsf = src->tsf;
- os_get_time(&dst->last_update);
- dst->last_update.sec -= src->age / 1000;
- usec = (src->age % 1000) * 1000;
- if (dst->last_update.usec < usec) {
- dst->last_update.sec--;
- dst->last_update.usec += 1000000;
- }
- dst->last_update.usec -= usec;
- }
- static void wpa_bss_add(struct wpa_supplicant *wpa_s,
- const u8 *ssid, size_t ssid_len,
- struct wpa_scan_res *res)
- {
- struct wpa_bss *bss;
- bss = os_zalloc(sizeof(*bss) + res->ie_len);
- if (bss == NULL)
- return;
- bss->id = wpa_s->bss_next_id++;
- bss->last_update_idx = wpa_s->bss_update_idx;
- wpa_bss_copy_res(bss, res);
- os_memcpy(bss->ssid, ssid, ssid_len);
- bss->ssid_len = ssid_len;
- bss->ie_len = res->ie_len;
- os_memcpy(bss + 1, res + 1, res->ie_len);
- dl_list_add_tail(&wpa_s->bss, &bss->list);
- dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
- wpa_s->num_bss++;
- wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'",
- bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
- wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
- if (wpa_s->num_bss > WPA_BSS_MAX_COUNT) {
- /* Remove the oldest entry */
- wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
- struct wpa_bss, list));
- }
- }
- static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
- struct wpa_scan_res *res)
- {
- bss->scan_miss_count = 0;
- bss->last_update_idx = wpa_s->bss_update_idx;
- wpa_bss_copy_res(bss, res);
- /* Move the entry to the end of the list */
- dl_list_del(&bss->list);
- if (bss->ie_len >= res->ie_len) {
- os_memcpy(bss + 1, res + 1, res->ie_len);
- bss->ie_len = res->ie_len;
- } else {
- struct wpa_bss *nbss;
- nbss = os_realloc(bss, sizeof(*bss) + res->ie_len);
- if (nbss) {
- bss = nbss;
- os_memcpy(bss + 1, res + 1, res->ie_len);
- bss->ie_len = res->ie_len;
- }
- }
- dl_list_add_tail(&wpa_s->bss, &bss->list);
- }
- void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
- {
- wpa_s->bss_update_idx++;
- wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
- wpa_s->bss_update_idx);
- }
- void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
- struct wpa_scan_res *res)
- {
- const u8 *ssid;
- struct wpa_bss *bss;
- ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
- if (ssid == NULL) {
- wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
- MAC2STR(res->bssid));
- return;
- }
- if (ssid[1] > 32) {
- wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
- MACSTR, MAC2STR(res->bssid));
- return;
- }
- /* TODO: add option for ignoring BSSes we are not interested in
- * (to save memory) */
- bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
- if (bss == NULL)
- wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
- else
- wpa_bss_update(wpa_s, bss, res);
- }
- void wpa_bss_update_end(struct wpa_supplicant *wpa_s)
- {
- struct wpa_bss *bss, *n;
- /* TODO: expire only entries that were on the scanned frequencies/SSIDs
- * list; need to get info from driver about scanned frequencies and
- * SSIDs to be able to figure out which entries should be expired based
- * on this */
- dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
- if (bss->last_update_idx < wpa_s->bss_update_idx)
- bss->scan_miss_count++;
- if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
- wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
- "match in scan", bss->id);
- wpa_bss_remove(wpa_s, bss);
- }
- }
- }
- static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
- {
- struct wpa_supplicant *wpa_s = eloop_ctx;
- struct wpa_bss *bss, *n;
- struct os_time t;
- if (dl_list_empty(&wpa_s->bss))
- return;
- os_get_time(&t);
- t.sec -= WPA_BSS_EXPIRATION_AGE;
- dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
- if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
- os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0)
- continue; /* do not expire BSSes that are in use */
- if (os_time_before(&bss->last_update, &t)) {
- wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
- bss->id);
- wpa_bss_remove(wpa_s, bss);
- } else
- break;
- }
- eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
- wpa_bss_timeout, wpa_s, NULL);
- }
- int wpa_bss_init(struct wpa_supplicant *wpa_s)
- {
- dl_list_init(&wpa_s->bss);
- dl_list_init(&wpa_s->bss_id);
- eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
- wpa_bss_timeout, wpa_s, NULL);
- return 0;
- }
- void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
- {
- struct wpa_bss *bss, *n;
- eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
- if (wpa_s->bss.next == NULL)
- return; /* BSS table not yet initialized */
- dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
- wpa_bss_remove(wpa_s, bss);
- }
- struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
- const u8 *bssid)
- {
- struct wpa_bss *bss;
- dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
- if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
- return bss;
- }
- return NULL;
- }
- struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
- {
- struct wpa_bss *bss;
- dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
- if (bss->id == id)
- return bss;
- }
- return NULL;
- }
- const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
- {
- const u8 *end, *pos;
- pos = (const u8 *) (bss + 1);
- end = pos + bss->ie_len;
- while (pos + 1 < end) {
- if (pos + 2 + pos[1] > end)
- break;
- if (pos[0] == ie)
- return pos;
- pos += 2 + pos[1];
- }
- return NULL;
- }
- const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
- {
- const u8 *end, *pos;
- pos = (const u8 *) (bss + 1);
- end = pos + bss->ie_len;
- while (pos + 1 < end) {
- if (pos + 2 + pos[1] > end)
- break;
- if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
- vendor_type == WPA_GET_BE32(&pos[2]))
- return pos;
- pos += 2 + pos[1];
- }
- return NULL;
- }
- struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
- u32 vendor_type)
- {
- struct wpabuf *buf;
- const u8 *end, *pos;
- buf = wpabuf_alloc(bss->ie_len);
- if (buf == NULL)
- return NULL;
- pos = (const u8 *) (bss + 1);
- end = pos + bss->ie_len;
- while (pos + 1 < end) {
- if (pos + 2 + pos[1] > end)
- break;
- if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
- vendor_type == WPA_GET_BE32(&pos[2]))
- wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
- pos += 2 + pos[1];
- }
- if (wpabuf_len(buf) == 0) {
- wpabuf_free(buf);
- buf = NULL;
- }
- return buf;
- }
- int wpa_bss_get_max_rate(const struct wpa_bss *bss)
- {
- int rate = 0;
- const u8 *ie;
- int i;
- ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
- for (i = 0; ie && i < ie[1]; i++) {
- if ((ie[i + 2] & 0x7f) > rate)
- rate = ie[i + 2] & 0x7f;
- }
- ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
- for (i = 0; ie && i < ie[1]; i++) {
- if ((ie[i + 2] & 0x7f) > rate)
- rate = ie[i + 2] & 0x7f;
- }
- return rate;
- }
|