ap_list.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * hostapd / AP table
  3. * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2003-2004, Instant802 Networks, Inc.
  5. * Copyright (c) 2006, Devicescape Software, Inc.
  6. *
  7. * This software may be distributed under the terms of the BSD license.
  8. * See README for more details.
  9. */
  10. #include "utils/includes.h"
  11. #include "utils/common.h"
  12. #include "utils/eloop.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "drivers/driver.h"
  16. #include "hostapd.h"
  17. #include "ap_config.h"
  18. #include "ieee802_11.h"
  19. #include "sta_info.h"
  20. #include "beacon.h"
  21. #include "ap_list.h"
  22. /* AP list is a double linked list with head->prev pointing to the end of the
  23. * list and tail->next = NULL. Entries are moved to the head of the list
  24. * whenever a beacon has been received from the AP in question. The tail entry
  25. * in this link will thus be the least recently used entry. */
  26. static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
  27. {
  28. int i;
  29. if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
  30. iface->conf->channel != ap->channel)
  31. return 0;
  32. if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
  33. return 1;
  34. for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
  35. int rate = (ap->supported_rates[i] & 0x7f) * 5;
  36. if (rate == 60 || rate == 90 || rate > 110)
  37. return 0;
  38. }
  39. return 1;
  40. }
  41. struct ap_info * ap_get_ap(struct hostapd_iface *iface, const u8 *ap)
  42. {
  43. struct ap_info *s;
  44. s = iface->ap_hash[STA_HASH(ap)];
  45. while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
  46. s = s->hnext;
  47. return s;
  48. }
  49. static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
  50. {
  51. if (iface->ap_list) {
  52. ap->prev = iface->ap_list->prev;
  53. iface->ap_list->prev = ap;
  54. } else
  55. ap->prev = ap;
  56. ap->next = iface->ap_list;
  57. iface->ap_list = ap;
  58. }
  59. static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
  60. {
  61. if (iface->ap_list == ap)
  62. iface->ap_list = ap->next;
  63. else
  64. ap->prev->next = ap->next;
  65. if (ap->next)
  66. ap->next->prev = ap->prev;
  67. else if (iface->ap_list)
  68. iface->ap_list->prev = ap->prev;
  69. }
  70. static void ap_ap_iter_list_add(struct hostapd_iface *iface,
  71. struct ap_info *ap)
  72. {
  73. if (iface->ap_iter_list) {
  74. ap->iter_prev = iface->ap_iter_list->iter_prev;
  75. iface->ap_iter_list->iter_prev = ap;
  76. } else
  77. ap->iter_prev = ap;
  78. ap->iter_next = iface->ap_iter_list;
  79. iface->ap_iter_list = ap;
  80. }
  81. static void ap_ap_iter_list_del(struct hostapd_iface *iface,
  82. struct ap_info *ap)
  83. {
  84. if (iface->ap_iter_list == ap)
  85. iface->ap_iter_list = ap->iter_next;
  86. else
  87. ap->iter_prev->iter_next = ap->iter_next;
  88. if (ap->iter_next)
  89. ap->iter_next->iter_prev = ap->iter_prev;
  90. else if (iface->ap_iter_list)
  91. iface->ap_iter_list->iter_prev = ap->iter_prev;
  92. }
  93. static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
  94. {
  95. ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
  96. iface->ap_hash[STA_HASH(ap->addr)] = ap;
  97. }
  98. static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
  99. {
  100. struct ap_info *s;
  101. s = iface->ap_hash[STA_HASH(ap->addr)];
  102. if (s == NULL) return;
  103. if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
  104. iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
  105. return;
  106. }
  107. while (s->hnext != NULL &&
  108. os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
  109. s = s->hnext;
  110. if (s->hnext != NULL)
  111. s->hnext = s->hnext->hnext;
  112. else
  113. printf("AP: could not remove AP " MACSTR " from hash table\n",
  114. MAC2STR(ap->addr));
  115. }
  116. static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
  117. {
  118. ap_ap_hash_del(iface, ap);
  119. ap_ap_list_del(iface, ap);
  120. ap_ap_iter_list_del(iface, ap);
  121. iface->num_ap--;
  122. os_free(ap);
  123. }
  124. static void hostapd_free_aps(struct hostapd_iface *iface)
  125. {
  126. struct ap_info *ap, *prev;
  127. ap = iface->ap_list;
  128. while (ap) {
  129. prev = ap;
  130. ap = ap->next;
  131. ap_free_ap(iface, prev);
  132. }
  133. iface->ap_list = NULL;
  134. }
  135. int ap_ap_for_each(struct hostapd_iface *iface,
  136. int (*func)(struct ap_info *s, void *data), void *data)
  137. {
  138. struct ap_info *s;
  139. int ret = 0;
  140. s = iface->ap_list;
  141. while (s) {
  142. ret = func(s, data);
  143. if (ret)
  144. break;
  145. s = s->next;
  146. }
  147. return ret;
  148. }
  149. static struct ap_info * ap_ap_add(struct hostapd_iface *iface, const u8 *addr)
  150. {
  151. struct ap_info *ap;
  152. ap = os_zalloc(sizeof(struct ap_info));
  153. if (ap == NULL)
  154. return NULL;
  155. /* initialize AP info data */
  156. os_memcpy(ap->addr, addr, ETH_ALEN);
  157. ap_ap_list_add(iface, ap);
  158. iface->num_ap++;
  159. ap_ap_hash_add(iface, ap);
  160. ap_ap_iter_list_add(iface, ap);
  161. if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
  162. wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
  163. MACSTR " from AP table", MAC2STR(ap->prev->addr));
  164. ap_free_ap(iface, ap->prev);
  165. }
  166. return ap;
  167. }
  168. void ap_list_process_beacon(struct hostapd_iface *iface,
  169. const struct ieee80211_mgmt *mgmt,
  170. struct ieee802_11_elems *elems,
  171. struct hostapd_frame_info *fi)
  172. {
  173. struct ap_info *ap;
  174. struct os_time now;
  175. int new_ap = 0;
  176. size_t len;
  177. int set_beacon = 0;
  178. if (iface->conf->ap_table_max_size < 1)
  179. return;
  180. ap = ap_get_ap(iface, mgmt->bssid);
  181. if (!ap) {
  182. ap = ap_ap_add(iface, mgmt->bssid);
  183. if (!ap) {
  184. printf("Failed to allocate AP information entry\n");
  185. return;
  186. }
  187. new_ap = 1;
  188. }
  189. ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
  190. ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
  191. if (elems->ssid) {
  192. len = elems->ssid_len;
  193. if (len >= sizeof(ap->ssid))
  194. len = sizeof(ap->ssid) - 1;
  195. os_memcpy(ap->ssid, elems->ssid, len);
  196. ap->ssid[len] = '\0';
  197. ap->ssid_len = len;
  198. }
  199. merge_byte_arrays(ap->supported_rates, WLAN_SUPP_RATES_MAX,
  200. elems->supp_rates, elems->supp_rates_len,
  201. elems->ext_supp_rates, elems->ext_supp_rates_len);
  202. ap->wpa = elems->wpa_ie != NULL;
  203. if (elems->erp_info && elems->erp_info_len == 1)
  204. ap->erp = elems->erp_info[0];
  205. else
  206. ap->erp = -1;
  207. if (elems->ds_params && elems->ds_params_len == 1)
  208. ap->channel = elems->ds_params[0];
  209. else if (fi)
  210. ap->channel = fi->channel;
  211. if (elems->ht_capabilities)
  212. ap->ht_support = 1;
  213. else
  214. ap->ht_support = 0;
  215. ap->num_beacons++;
  216. os_get_time(&now);
  217. ap->last_beacon = now.sec;
  218. if (fi)
  219. ap->datarate = fi->datarate;
  220. if (!new_ap && ap != iface->ap_list) {
  221. /* move AP entry into the beginning of the list so that the
  222. * oldest entry is always in the end of the list */
  223. ap_ap_list_del(iface, ap);
  224. ap_ap_list_add(iface, ap);
  225. }
  226. if (!iface->olbc &&
  227. ap_list_beacon_olbc(iface, ap)) {
  228. iface->olbc = 1;
  229. wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
  230. "protection", MAC2STR(ap->addr));
  231. set_beacon++;
  232. }
  233. #ifdef CONFIG_IEEE80211N
  234. if (!iface->olbc_ht && !ap->ht_support) {
  235. iface->olbc_ht = 1;
  236. hostapd_ht_operation_update(iface);
  237. wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
  238. " - enable protection", MAC2STR(ap->addr));
  239. set_beacon++;
  240. }
  241. #endif /* CONFIG_IEEE80211N */
  242. if (set_beacon)
  243. ieee802_11_update_beacons(iface);
  244. }
  245. static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
  246. {
  247. struct hostapd_iface *iface = eloop_ctx;
  248. struct os_time now;
  249. struct ap_info *ap;
  250. int set_beacon = 0;
  251. eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
  252. if (!iface->ap_list)
  253. return;
  254. os_get_time(&now);
  255. while (iface->ap_list) {
  256. ap = iface->ap_list->prev;
  257. if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
  258. now.sec)
  259. break;
  260. ap_free_ap(iface, ap);
  261. }
  262. if (iface->olbc || iface->olbc_ht) {
  263. int olbc = 0;
  264. int olbc_ht = 0;
  265. ap = iface->ap_list;
  266. while (ap && (olbc == 0 || olbc_ht == 0)) {
  267. if (ap_list_beacon_olbc(iface, ap))
  268. olbc = 1;
  269. if (!ap->ht_support)
  270. olbc_ht = 1;
  271. ap = ap->next;
  272. }
  273. if (!olbc && iface->olbc) {
  274. wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
  275. iface->olbc = 0;
  276. set_beacon++;
  277. }
  278. #ifdef CONFIG_IEEE80211N
  279. if (!olbc_ht && iface->olbc_ht) {
  280. wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
  281. iface->olbc_ht = 0;
  282. hostapd_ht_operation_update(iface);
  283. set_beacon++;
  284. }
  285. #endif /* CONFIG_IEEE80211N */
  286. }
  287. if (set_beacon)
  288. ieee802_11_update_beacons(iface);
  289. }
  290. int ap_list_init(struct hostapd_iface *iface)
  291. {
  292. eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
  293. return 0;
  294. }
  295. void ap_list_deinit(struct hostapd_iface *iface)
  296. {
  297. eloop_cancel_timeout(ap_list_timer, iface, NULL);
  298. hostapd_free_aps(iface);
  299. }