bss.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * BSS table
  3. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  4. *
  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.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "drivers/driver.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "notify.h"
  21. #include "bss.h"
  22. #ifndef WPA_BSS_MAX_COUNT
  23. #define WPA_BSS_MAX_COUNT 200
  24. #endif /* WPA_BSS_MAX_COUNT */
  25. /**
  26. * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
  27. */
  28. #define WPA_BSS_EXPIRATION_PERIOD 10
  29. /**
  30. * WPA_BSS_EXPIRATION_AGE - BSS entry age after which it can be expired
  31. *
  32. * This value control the time in seconds after which a BSS entry gets removed
  33. * if it has not been updated or is not in use.
  34. */
  35. #define WPA_BSS_EXPIRATION_AGE 180
  36. /**
  37. * WPA_BSS_EXPIRATION_SCAN_COUNT - Expire BSS after number of scans
  38. *
  39. * If the BSS entry has not been seen in this many scans, it will be removed.
  40. * Value 1 means that the entry is removed after the first scan without the
  41. * BSSID being seen. Larger values can be used to avoid BSS entries
  42. * disappearing if they are not visible in every scan (e.g., low signal quality
  43. * or interference).
  44. */
  45. #define WPA_BSS_EXPIRATION_SCAN_COUNT 2
  46. static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  47. {
  48. dl_list_del(&bss->list);
  49. dl_list_del(&bss->list_id);
  50. wpa_s->num_bss--;
  51. wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'",
  52. bss->id, MAC2STR(bss->bssid),
  53. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  54. wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
  55. os_free(bss);
  56. }
  57. static struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s,
  58. const u8 *bssid, const u8 *ssid,
  59. size_t ssid_len)
  60. {
  61. struct wpa_bss *bss;
  62. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  63. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  64. bss->ssid_len == ssid_len &&
  65. os_memcmp(bss->ssid, ssid, ssid_len) == 0)
  66. return bss;
  67. }
  68. return NULL;
  69. }
  70. static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
  71. {
  72. os_time_t usec;
  73. dst->flags = src->flags;
  74. os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
  75. dst->freq = src->freq;
  76. dst->beacon_int = src->beacon_int;
  77. dst->caps = src->caps;
  78. dst->qual = src->qual;
  79. dst->noise = src->noise;
  80. dst->level = src->level;
  81. dst->tsf = src->tsf;
  82. os_get_time(&dst->last_update);
  83. dst->last_update.sec -= src->age / 1000;
  84. usec = (src->age % 1000) * 1000;
  85. if (dst->last_update.usec < usec) {
  86. dst->last_update.sec--;
  87. dst->last_update.usec += 1000000;
  88. }
  89. dst->last_update.usec -= usec;
  90. }
  91. static void wpa_bss_add(struct wpa_supplicant *wpa_s,
  92. const u8 *ssid, size_t ssid_len,
  93. struct wpa_scan_res *res)
  94. {
  95. struct wpa_bss *bss;
  96. bss = os_zalloc(sizeof(*bss) + res->ie_len);
  97. if (bss == NULL)
  98. return;
  99. bss->id = wpa_s->bss_next_id++;
  100. bss->last_update_idx = wpa_s->bss_update_idx;
  101. wpa_bss_copy_res(bss, res);
  102. os_memcpy(bss->ssid, ssid, ssid_len);
  103. bss->ssid_len = ssid_len;
  104. bss->ie_len = res->ie_len;
  105. os_memcpy(bss + 1, res + 1, res->ie_len);
  106. dl_list_add_tail(&wpa_s->bss, &bss->list);
  107. dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
  108. wpa_s->num_bss++;
  109. wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'",
  110. bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  111. wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
  112. if (wpa_s->num_bss > WPA_BSS_MAX_COUNT) {
  113. /* Remove the oldest entry */
  114. wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
  115. struct wpa_bss, list));
  116. }
  117. }
  118. static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  119. struct wpa_scan_res *res)
  120. {
  121. bss->scan_miss_count = 0;
  122. bss->last_update_idx = wpa_s->bss_update_idx;
  123. wpa_bss_copy_res(bss, res);
  124. /* Move the entry to the end of the list */
  125. dl_list_del(&bss->list);
  126. if (bss->ie_len >= res->ie_len) {
  127. os_memcpy(bss + 1, res + 1, res->ie_len);
  128. bss->ie_len = res->ie_len;
  129. } else {
  130. struct wpa_bss *nbss;
  131. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len);
  132. if (nbss) {
  133. bss = nbss;
  134. os_memcpy(bss + 1, res + 1, res->ie_len);
  135. bss->ie_len = res->ie_len;
  136. }
  137. }
  138. dl_list_add_tail(&wpa_s->bss, &bss->list);
  139. }
  140. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  141. {
  142. wpa_s->bss_update_idx++;
  143. wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
  144. wpa_s->bss_update_idx);
  145. }
  146. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  147. struct wpa_scan_res *res)
  148. {
  149. const u8 *ssid;
  150. struct wpa_bss *bss;
  151. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  152. if (ssid == NULL) {
  153. wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
  154. MAC2STR(res->bssid));
  155. return;
  156. }
  157. if (ssid[1] > 32) {
  158. wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
  159. MACSTR, MAC2STR(res->bssid));
  160. return;
  161. }
  162. /* TODO: add option for ignoring BSSes we are not interested in
  163. * (to save memory) */
  164. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  165. if (bss == NULL)
  166. wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
  167. else
  168. wpa_bss_update(wpa_s, bss, res);
  169. }
  170. void wpa_bss_update_end(struct wpa_supplicant *wpa_s)
  171. {
  172. struct wpa_bss *bss, *n;
  173. /* TODO: expire only entries that were on the scanned frequencies/SSIDs
  174. * list; need to get info from driver about scanned frequencies and
  175. * SSIDs to be able to figure out which entries should be expired based
  176. * on this */
  177. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  178. if (bss->last_update_idx < wpa_s->bss_update_idx)
  179. bss->scan_miss_count++;
  180. if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
  181. wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
  182. "match in scan", bss->id);
  183. wpa_bss_remove(wpa_s, bss);
  184. }
  185. }
  186. }
  187. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  188. {
  189. struct wpa_supplicant *wpa_s = eloop_ctx;
  190. struct wpa_bss *bss, *n;
  191. struct os_time t;
  192. if (dl_list_empty(&wpa_s->bss))
  193. return;
  194. os_get_time(&t);
  195. t.sec -= WPA_BSS_EXPIRATION_AGE;
  196. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  197. if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
  198. os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0)
  199. continue; /* do not expire BSSes that are in use */
  200. if (os_time_before(&bss->last_update, &t)) {
  201. wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
  202. bss->id);
  203. wpa_bss_remove(wpa_s, bss);
  204. } else
  205. break;
  206. }
  207. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  208. wpa_bss_timeout, wpa_s, NULL);
  209. }
  210. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  211. {
  212. dl_list_init(&wpa_s->bss);
  213. dl_list_init(&wpa_s->bss_id);
  214. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  215. wpa_bss_timeout, wpa_s, NULL);
  216. return 0;
  217. }
  218. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  219. {
  220. struct wpa_bss *bss, *n;
  221. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  222. if (wpa_s->bss.next == NULL)
  223. return; /* BSS table not yet initialized */
  224. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
  225. wpa_bss_remove(wpa_s, bss);
  226. }
  227. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  228. const u8 *bssid)
  229. {
  230. struct wpa_bss *bss;
  231. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  232. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  233. return bss;
  234. }
  235. return NULL;
  236. }
  237. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  238. {
  239. struct wpa_bss *bss;
  240. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  241. if (bss->id == id)
  242. return bss;
  243. }
  244. return NULL;
  245. }
  246. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  247. {
  248. const u8 *end, *pos;
  249. pos = (const u8 *) (bss + 1);
  250. end = pos + bss->ie_len;
  251. while (pos + 1 < end) {
  252. if (pos + 2 + pos[1] > end)
  253. break;
  254. if (pos[0] == ie)
  255. return pos;
  256. pos += 2 + pos[1];
  257. }
  258. return NULL;
  259. }
  260. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  261. {
  262. const u8 *end, *pos;
  263. pos = (const u8 *) (bss + 1);
  264. end = pos + bss->ie_len;
  265. while (pos + 1 < end) {
  266. if (pos + 2 + pos[1] > end)
  267. break;
  268. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  269. vendor_type == WPA_GET_BE32(&pos[2]))
  270. return pos;
  271. pos += 2 + pos[1];
  272. }
  273. return NULL;
  274. }
  275. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  276. u32 vendor_type)
  277. {
  278. struct wpabuf *buf;
  279. const u8 *end, *pos;
  280. buf = wpabuf_alloc(bss->ie_len);
  281. if (buf == NULL)
  282. return NULL;
  283. pos = (const u8 *) (bss + 1);
  284. end = pos + bss->ie_len;
  285. while (pos + 1 < end) {
  286. if (pos + 2 + pos[1] > end)
  287. break;
  288. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  289. vendor_type == WPA_GET_BE32(&pos[2]))
  290. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  291. pos += 2 + pos[1];
  292. }
  293. if (wpabuf_len(buf) == 0) {
  294. wpabuf_free(buf);
  295. buf = NULL;
  296. }
  297. return buf;
  298. }
  299. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  300. {
  301. int rate = 0;
  302. const u8 *ie;
  303. int i;
  304. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  305. for (i = 0; ie && i < ie[1]; i++) {
  306. if ((ie[i + 2] & 0x7f) > rate)
  307. rate = ie[i + 2] & 0x7f;
  308. }
  309. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  310. for (i = 0; ie && i < ie[1]; i++) {
  311. if ((ie[i + 2] & 0x7f) > rate)
  312. rate = ie[i + 2] & 0x7f;
  313. }
  314. return rate;
  315. }