ap_list.c 8.6 KB

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