ap_list.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "hostapd.h"
  16. #include "ap_config.h"
  17. #include "ieee802_11.h"
  18. #include "sta_info.h"
  19. #include "beacon.h"
  20. #include "ap_list.h"
  21. /* AP list is a double linked list with head->prev pointing to the end of the
  22. * list and tail->next = NULL. Entries are moved to the head of the list
  23. * whenever a beacon has been received from the AP in question. The tail entry
  24. * in this link will thus be the least recently used entry. */
  25. static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
  26. {
  27. int i;
  28. if (iface->current_mode == NULL ||
  29. 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. static 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_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
  71. {
  72. ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
  73. iface->ap_hash[STA_HASH(ap->addr)] = ap;
  74. }
  75. static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
  76. {
  77. struct ap_info *s;
  78. s = iface->ap_hash[STA_HASH(ap->addr)];
  79. if (s == NULL) return;
  80. if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
  81. iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
  82. return;
  83. }
  84. while (s->hnext != NULL &&
  85. os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
  86. s = s->hnext;
  87. if (s->hnext != NULL)
  88. s->hnext = s->hnext->hnext;
  89. else
  90. wpa_printf(MSG_INFO, "AP: could not remove AP " MACSTR
  91. " from hash table", MAC2STR(ap->addr));
  92. }
  93. static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
  94. {
  95. ap_ap_hash_del(iface, ap);
  96. ap_ap_list_del(iface, ap);
  97. iface->num_ap--;
  98. os_free(ap);
  99. }
  100. static void hostapd_free_aps(struct hostapd_iface *iface)
  101. {
  102. struct ap_info *ap, *prev;
  103. ap = iface->ap_list;
  104. while (ap) {
  105. prev = ap;
  106. ap = ap->next;
  107. ap_free_ap(iface, prev);
  108. }
  109. iface->ap_list = NULL;
  110. }
  111. static struct ap_info * ap_ap_add(struct hostapd_iface *iface, const u8 *addr)
  112. {
  113. struct ap_info *ap;
  114. ap = os_zalloc(sizeof(struct ap_info));
  115. if (ap == NULL)
  116. return NULL;
  117. /* initialize AP info data */
  118. os_memcpy(ap->addr, addr, ETH_ALEN);
  119. ap_ap_list_add(iface, ap);
  120. iface->num_ap++;
  121. ap_ap_hash_add(iface, ap);
  122. if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
  123. wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
  124. MACSTR " from AP table", MAC2STR(ap->prev->addr));
  125. ap_free_ap(iface, ap->prev);
  126. }
  127. return ap;
  128. }
  129. void ap_list_process_beacon(struct hostapd_iface *iface,
  130. const struct ieee80211_mgmt *mgmt,
  131. struct ieee802_11_elems *elems,
  132. struct hostapd_frame_info *fi)
  133. {
  134. struct ap_info *ap;
  135. int new_ap = 0;
  136. int set_beacon = 0;
  137. if (iface->conf->ap_table_max_size < 1)
  138. return;
  139. ap = ap_get_ap(iface, mgmt->bssid);
  140. if (!ap) {
  141. ap = ap_ap_add(iface, mgmt->bssid);
  142. if (!ap) {
  143. wpa_printf(MSG_INFO,
  144. "Failed to allocate AP information entry");
  145. return;
  146. }
  147. new_ap = 1;
  148. }
  149. merge_byte_arrays(ap->supported_rates, WLAN_SUPP_RATES_MAX,
  150. elems->supp_rates, elems->supp_rates_len,
  151. elems->ext_supp_rates, elems->ext_supp_rates_len);
  152. if (elems->erp_info)
  153. ap->erp = elems->erp_info[0];
  154. else
  155. ap->erp = -1;
  156. if (elems->ds_params)
  157. ap->channel = elems->ds_params[0];
  158. else if (elems->ht_operation)
  159. ap->channel = elems->ht_operation[0];
  160. else if (fi)
  161. ap->channel = fi->channel;
  162. if (elems->ht_capabilities)
  163. ap->ht_support = 1;
  164. else
  165. ap->ht_support = 0;
  166. os_get_reltime(&ap->last_beacon);
  167. if (!new_ap && ap != iface->ap_list) {
  168. /* move AP entry into the beginning of the list so that the
  169. * oldest entry is always in the end of the list */
  170. ap_ap_list_del(iface, ap);
  171. ap_ap_list_add(iface, ap);
  172. }
  173. if (!iface->olbc &&
  174. ap_list_beacon_olbc(iface, ap)) {
  175. iface->olbc = 1;
  176. wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR
  177. " (channel %d) - enable protection",
  178. MAC2STR(ap->addr), ap->channel);
  179. set_beacon++;
  180. }
  181. #ifdef CONFIG_IEEE80211N
  182. if (!iface->olbc_ht && !ap->ht_support &&
  183. (ap->channel == 0 ||
  184. ap->channel == iface->conf->channel ||
  185. ap->channel == iface->conf->channel +
  186. iface->conf->secondary_channel * 4)) {
  187. iface->olbc_ht = 1;
  188. hostapd_ht_operation_update(iface);
  189. wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
  190. " (channel %d) - enable protection",
  191. MAC2STR(ap->addr), ap->channel);
  192. set_beacon++;
  193. }
  194. #endif /* CONFIG_IEEE80211N */
  195. if (set_beacon)
  196. ieee802_11_update_beacons(iface);
  197. }
  198. void ap_list_timer(struct hostapd_iface *iface)
  199. {
  200. struct os_reltime now;
  201. struct ap_info *ap;
  202. int set_beacon = 0;
  203. if (!iface->ap_list)
  204. return;
  205. os_get_reltime(&now);
  206. while (iface->ap_list) {
  207. ap = iface->ap_list->prev;
  208. if (!os_reltime_expired(&now, &ap->last_beacon,
  209. iface->conf->ap_table_expiration_time))
  210. break;
  211. ap_free_ap(iface, ap);
  212. }
  213. if (iface->olbc || iface->olbc_ht) {
  214. int olbc = 0;
  215. int olbc_ht = 0;
  216. ap = iface->ap_list;
  217. while (ap && (olbc == 0 || olbc_ht == 0)) {
  218. if (ap_list_beacon_olbc(iface, ap))
  219. olbc = 1;
  220. if (!ap->ht_support)
  221. olbc_ht = 1;
  222. ap = ap->next;
  223. }
  224. if (!olbc && iface->olbc) {
  225. wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
  226. iface->olbc = 0;
  227. set_beacon++;
  228. }
  229. #ifdef CONFIG_IEEE80211N
  230. if (!olbc_ht && iface->olbc_ht) {
  231. wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
  232. iface->olbc_ht = 0;
  233. hostapd_ht_operation_update(iface);
  234. set_beacon++;
  235. }
  236. #endif /* CONFIG_IEEE80211N */
  237. }
  238. if (set_beacon)
  239. ieee802_11_update_beacons(iface);
  240. }
  241. int ap_list_init(struct hostapd_iface *iface)
  242. {
  243. return 0;
  244. }
  245. void ap_list_deinit(struct hostapd_iface *iface)
  246. {
  247. hostapd_free_aps(iface);
  248. }