bss.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * BSS table
  3. * Copyright (c) 2009-2010, 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 "scan.h"
  22. #include "bss.h"
  23. #ifndef WPA_BSS_MAX_COUNT
  24. #define WPA_BSS_MAX_COUNT 200
  25. #endif /* WPA_BSS_MAX_COUNT */
  26. /**
  27. * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
  28. */
  29. #define WPA_BSS_EXPIRATION_PERIOD 10
  30. /**
  31. * WPA_BSS_EXPIRATION_AGE - BSS entry age after which it can be expired
  32. *
  33. * This value control the time in seconds after which a BSS entry gets removed
  34. * if it has not been updated or is not in use.
  35. */
  36. #define WPA_BSS_EXPIRATION_AGE 180
  37. /**
  38. * WPA_BSS_EXPIRATION_SCAN_COUNT - Expire BSS after number of scans
  39. *
  40. * If the BSS entry has not been seen in this many scans, it will be removed.
  41. * Value 1 means that the entry is removed after the first scan without the
  42. * BSSID being seen. Larger values can be used to avoid BSS entries
  43. * disappearing if they are not visible in every scan (e.g., low signal quality
  44. * or interference).
  45. */
  46. #define WPA_BSS_EXPIRATION_SCAN_COUNT 2
  47. static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  48. {
  49. dl_list_del(&bss->list);
  50. dl_list_del(&bss->list_id);
  51. wpa_s->num_bss--;
  52. wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'",
  53. bss->id, MAC2STR(bss->bssid),
  54. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  55. wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
  56. os_free(bss);
  57. }
  58. struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
  59. const u8 *ssid, 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. struct dl_list *prev = bss->list_id.prev;
  132. dl_list_del(&bss->list_id);
  133. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len);
  134. if (nbss) {
  135. bss = nbss;
  136. os_memcpy(bss + 1, res + 1, res->ie_len);
  137. bss->ie_len = res->ie_len;
  138. }
  139. dl_list_add(prev, &bss->list_id);
  140. }
  141. dl_list_add_tail(&wpa_s->bss, &bss->list);
  142. }
  143. static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  144. {
  145. return bss == wpa_s->current_bss ||
  146. os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
  147. os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
  148. }
  149. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  150. {
  151. wpa_s->bss_update_idx++;
  152. wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
  153. wpa_s->bss_update_idx);
  154. }
  155. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  156. struct wpa_scan_res *res)
  157. {
  158. const u8 *ssid;
  159. struct wpa_bss *bss;
  160. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  161. if (ssid == NULL) {
  162. wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
  163. MAC2STR(res->bssid));
  164. return;
  165. }
  166. if (ssid[1] > 32) {
  167. wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
  168. MACSTR, MAC2STR(res->bssid));
  169. return;
  170. }
  171. /* TODO: add option for ignoring BSSes we are not interested in
  172. * (to save memory) */
  173. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  174. if (bss == NULL)
  175. wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
  176. else
  177. wpa_bss_update(wpa_s, bss, res);
  178. }
  179. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  180. const struct scan_info *info)
  181. {
  182. int found;
  183. size_t i;
  184. if (info == NULL)
  185. return 1;
  186. if (info->num_freqs) {
  187. found = 0;
  188. for (i = 0; i < info->num_freqs; i++) {
  189. if (bss->freq == info->freqs[i]) {
  190. found = 1;
  191. break;
  192. }
  193. }
  194. if (!found)
  195. return 0;
  196. }
  197. if (info->num_ssids) {
  198. found = 0;
  199. for (i = 0; i < info->num_ssids; i++) {
  200. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  201. if ((s->ssid == NULL || s->ssid_len == 0) ||
  202. (s->ssid_len == bss->ssid_len &&
  203. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  204. 0)) {
  205. found = 1;
  206. break;
  207. }
  208. }
  209. if (!found)
  210. return 0;
  211. }
  212. return 1;
  213. }
  214. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  215. int new_scan)
  216. {
  217. struct wpa_bss *bss, *n;
  218. if (!new_scan)
  219. return; /* do not expire entries without new scan */
  220. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  221. if (wpa_bss_in_use(wpa_s, bss))
  222. continue;
  223. if (!wpa_bss_included_in_scan(bss, info))
  224. continue; /* expire only BSSes that were scanned */
  225. if (bss->last_update_idx < wpa_s->bss_update_idx)
  226. bss->scan_miss_count++;
  227. if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
  228. wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
  229. "match in scan", bss->id);
  230. wpa_bss_remove(wpa_s, bss);
  231. }
  232. }
  233. }
  234. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  235. {
  236. struct wpa_supplicant *wpa_s = eloop_ctx;
  237. struct wpa_bss *bss, *n;
  238. struct os_time t;
  239. if (dl_list_empty(&wpa_s->bss))
  240. return;
  241. os_get_time(&t);
  242. t.sec -= WPA_BSS_EXPIRATION_AGE;
  243. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  244. if (wpa_bss_in_use(wpa_s, bss))
  245. continue;
  246. if (os_time_before(&bss->last_update, &t)) {
  247. wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
  248. bss->id);
  249. wpa_bss_remove(wpa_s, bss);
  250. } else
  251. break;
  252. }
  253. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  254. wpa_bss_timeout, wpa_s, NULL);
  255. }
  256. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  257. {
  258. dl_list_init(&wpa_s->bss);
  259. dl_list_init(&wpa_s->bss_id);
  260. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  261. wpa_bss_timeout, wpa_s, NULL);
  262. return 0;
  263. }
  264. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  265. {
  266. struct wpa_bss *bss, *n;
  267. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  268. if (wpa_s->bss.next == NULL)
  269. return; /* BSS table not yet initialized */
  270. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
  271. wpa_bss_remove(wpa_s, bss);
  272. }
  273. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  274. const u8 *bssid)
  275. {
  276. struct wpa_bss *bss;
  277. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  278. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  279. return bss;
  280. }
  281. return NULL;
  282. }
  283. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  284. {
  285. struct wpa_bss *bss;
  286. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  287. if (bss->id == id)
  288. return bss;
  289. }
  290. return NULL;
  291. }
  292. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  293. {
  294. const u8 *end, *pos;
  295. pos = (const u8 *) (bss + 1);
  296. end = pos + bss->ie_len;
  297. while (pos + 1 < end) {
  298. if (pos + 2 + pos[1] > end)
  299. break;
  300. if (pos[0] == ie)
  301. return pos;
  302. pos += 2 + pos[1];
  303. }
  304. return NULL;
  305. }
  306. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  307. {
  308. const u8 *end, *pos;
  309. pos = (const u8 *) (bss + 1);
  310. end = pos + bss->ie_len;
  311. while (pos + 1 < end) {
  312. if (pos + 2 + pos[1] > end)
  313. break;
  314. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  315. vendor_type == WPA_GET_BE32(&pos[2]))
  316. return pos;
  317. pos += 2 + pos[1];
  318. }
  319. return NULL;
  320. }
  321. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  322. u32 vendor_type)
  323. {
  324. struct wpabuf *buf;
  325. const u8 *end, *pos;
  326. buf = wpabuf_alloc(bss->ie_len);
  327. if (buf == NULL)
  328. return NULL;
  329. pos = (const u8 *) (bss + 1);
  330. end = pos + bss->ie_len;
  331. while (pos + 1 < end) {
  332. if (pos + 2 + pos[1] > end)
  333. break;
  334. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  335. vendor_type == WPA_GET_BE32(&pos[2]))
  336. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  337. pos += 2 + pos[1];
  338. }
  339. if (wpabuf_len(buf) == 0) {
  340. wpabuf_free(buf);
  341. buf = NULL;
  342. }
  343. return buf;
  344. }
  345. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  346. {
  347. int rate = 0;
  348. const u8 *ie;
  349. int i;
  350. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  351. for (i = 0; ie && i < ie[1]; i++) {
  352. if ((ie[i + 2] & 0x7f) > rate)
  353. rate = ie[i + 2] & 0x7f;
  354. }
  355. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  356. for (i = 0; ie && i < ie[1]; i++) {
  357. if ((ie[i + 2] & 0x7f) > rate)
  358. rate = ie[i + 2] & 0x7f;
  359. }
  360. return rate;
  361. }
  362. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  363. {
  364. const u8 *ie, *ie2;
  365. int i, j, len;
  366. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  367. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  368. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  369. *rates = os_malloc(len);
  370. if (!rates)
  371. return -1;
  372. for (i = 0; ie && i < ie[1]; i++)
  373. (*rates)[i] = ie[i + 2] & 0x7f;
  374. for (j = 0; ie2 && j < ie2[1]; j++)
  375. (*rates)[i + j] = ie2[j + 2] & 0x7f;
  376. return len;
  377. }