bss.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. wpa_s->num_bss--;
  50. wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'",
  51. bss->id, MAC2STR(bss->bssid),
  52. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  53. wpas_notify_bss_removed(wpa_s, bss->bssid);
  54. os_free(bss);
  55. }
  56. static struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s,
  57. const u8 *bssid, const u8 *ssid,
  58. size_t ssid_len)
  59. {
  60. struct wpa_bss *bss;
  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)
  65. return bss;
  66. }
  67. return NULL;
  68. }
  69. static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
  70. {
  71. os_time_t usec;
  72. dst->flags = src->flags;
  73. os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
  74. dst->freq = src->freq;
  75. dst->beacon_int = src->beacon_int;
  76. dst->caps = src->caps;
  77. dst->qual = src->qual;
  78. dst->noise = src->noise;
  79. dst->level = src->level;
  80. dst->tsf = src->tsf;
  81. os_get_time(&dst->last_update);
  82. dst->last_update.sec -= src->age / 1000;
  83. usec = (src->age % 1000) * 1000;
  84. if (dst->last_update.usec < usec) {
  85. dst->last_update.sec--;
  86. dst->last_update.usec += 1000000;
  87. }
  88. dst->last_update.usec -= usec;
  89. }
  90. static void wpa_bss_add(struct wpa_supplicant *wpa_s,
  91. const u8 *ssid, size_t ssid_len,
  92. struct wpa_scan_res *res)
  93. {
  94. struct wpa_bss *bss;
  95. bss = os_zalloc(sizeof(*bss) + res->ie_len);
  96. if (bss == NULL)
  97. return;
  98. bss->id = wpa_s->bss_next_id++;
  99. bss->last_update_idx = wpa_s->bss_update_idx;
  100. wpa_bss_copy_res(bss, res);
  101. os_memcpy(bss->ssid, ssid, ssid_len);
  102. bss->ssid_len = ssid_len;
  103. bss->ie_len = res->ie_len;
  104. os_memcpy(bss + 1, res + 1, res->ie_len);
  105. dl_list_add_tail(&wpa_s->bss, &bss->list);
  106. wpa_s->num_bss++;
  107. wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'",
  108. bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  109. wpas_notify_bss_added(wpa_s, res->bssid);
  110. if (wpa_s->num_bss > WPA_BSS_MAX_COUNT) {
  111. /* Remove the oldest entry */
  112. wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
  113. struct wpa_bss, list));
  114. }
  115. }
  116. static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  117. struct wpa_scan_res *res)
  118. {
  119. bss->scan_miss_count = 0;
  120. bss->last_update_idx = wpa_s->bss_update_idx;
  121. wpa_bss_copy_res(bss, res);
  122. /* Move the entry to the end of the list */
  123. dl_list_del(&bss->list);
  124. if (bss->ie_len >= res->ie_len) {
  125. os_memcpy(bss + 1, res + 1, res->ie_len);
  126. bss->ie_len = res->ie_len;
  127. } else {
  128. struct wpa_bss *nbss;
  129. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len);
  130. if (nbss) {
  131. bss = nbss;
  132. os_memcpy(bss + 1, res + 1, res->ie_len);
  133. bss->ie_len = res->ie_len;
  134. }
  135. }
  136. dl_list_add_tail(&wpa_s->bss, &bss->list);
  137. }
  138. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  139. {
  140. wpa_s->bss_update_idx++;
  141. wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
  142. wpa_s->bss_update_idx);
  143. }
  144. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  145. struct wpa_scan_res *res)
  146. {
  147. const u8 *ssid;
  148. struct wpa_bss *bss;
  149. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  150. if (ssid == NULL) {
  151. wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
  152. MAC2STR(res->bssid));
  153. return;
  154. }
  155. if (ssid[1] > 32) {
  156. wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
  157. MACSTR, MAC2STR(res->bssid));
  158. return;
  159. }
  160. /* TODO: add option for ignoring BSSes we are not interested in
  161. * (to save memory) */
  162. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  163. if (bss == NULL)
  164. wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
  165. else
  166. wpa_bss_update(wpa_s, bss, res);
  167. }
  168. void wpa_bss_update_end(struct wpa_supplicant *wpa_s)
  169. {
  170. struct wpa_bss *bss, *n;
  171. /* TODO: expire only entries that were on the scanned frequencies/SSIDs
  172. * list; need to get info from driver about scanned frequencies and
  173. * SSIDs to be able to figure out which entries should be expired based
  174. * on this */
  175. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  176. if (bss->last_update_idx < wpa_s->bss_update_idx)
  177. bss->scan_miss_count++;
  178. if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
  179. wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
  180. "match in scan", bss->id);
  181. wpa_bss_remove(wpa_s, bss);
  182. }
  183. }
  184. }
  185. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  186. {
  187. struct wpa_supplicant *wpa_s = eloop_ctx;
  188. struct wpa_bss *bss, *n;
  189. struct os_time t;
  190. if (dl_list_empty(&wpa_s->bss))
  191. return;
  192. os_get_time(&t);
  193. t.sec -= WPA_BSS_EXPIRATION_AGE;
  194. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  195. if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
  196. os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0)
  197. continue; /* do not expire BSSes that are in use */
  198. if (os_time_before(&bss->last_update, &t)) {
  199. wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
  200. bss->id);
  201. wpa_bss_remove(wpa_s, bss);
  202. } else
  203. break;
  204. }
  205. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  206. wpa_bss_timeout, wpa_s, NULL);
  207. }
  208. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  209. {
  210. dl_list_init(&wpa_s->bss);
  211. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  212. wpa_bss_timeout, wpa_s, NULL);
  213. return 0;
  214. }
  215. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  216. {
  217. struct wpa_bss *bss, *n;
  218. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  219. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
  220. wpa_bss_remove(wpa_s, bss);
  221. }