ap_list.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #ifdef CONFIG_IEEE80211N
  48. static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
  49. struct ap_info *ap)
  50. {
  51. return !ap->ht_support;
  52. }
  53. #endif /* CONFIG_IEEE80211N */
  54. struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
  55. {
  56. struct ap_info *s;
  57. s = iface->ap_hash[STA_HASH(ap)];
  58. while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
  59. s = s->hnext;
  60. return s;
  61. }
  62. static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
  63. {
  64. if (iface->ap_list) {
  65. ap->prev = iface->ap_list->prev;
  66. iface->ap_list->prev = ap;
  67. } else
  68. ap->prev = ap;
  69. ap->next = iface->ap_list;
  70. iface->ap_list = ap;
  71. }
  72. static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
  73. {
  74. if (iface->ap_list == ap)
  75. iface->ap_list = ap->next;
  76. else
  77. ap->prev->next = ap->next;
  78. if (ap->next)
  79. ap->next->prev = ap->prev;
  80. else if (iface->ap_list)
  81. iface->ap_list->prev = ap->prev;
  82. }
  83. static void ap_ap_iter_list_add(struct hostapd_iface *iface,
  84. struct ap_info *ap)
  85. {
  86. if (iface->ap_iter_list) {
  87. ap->iter_prev = iface->ap_iter_list->iter_prev;
  88. iface->ap_iter_list->iter_prev = ap;
  89. } else
  90. ap->iter_prev = ap;
  91. ap->iter_next = iface->ap_iter_list;
  92. iface->ap_iter_list = ap;
  93. }
  94. static void ap_ap_iter_list_del(struct hostapd_iface *iface,
  95. struct ap_info *ap)
  96. {
  97. if (iface->ap_iter_list == ap)
  98. iface->ap_iter_list = ap->iter_next;
  99. else
  100. ap->iter_prev->iter_next = ap->iter_next;
  101. if (ap->iter_next)
  102. ap->iter_next->iter_prev = ap->iter_prev;
  103. else if (iface->ap_iter_list)
  104. iface->ap_iter_list->iter_prev = ap->iter_prev;
  105. }
  106. static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
  107. {
  108. ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
  109. iface->ap_hash[STA_HASH(ap->addr)] = ap;
  110. }
  111. static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
  112. {
  113. struct ap_info *s;
  114. s = iface->ap_hash[STA_HASH(ap->addr)];
  115. if (s == NULL) return;
  116. if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
  117. iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
  118. return;
  119. }
  120. while (s->hnext != NULL &&
  121. os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
  122. s = s->hnext;
  123. if (s->hnext != NULL)
  124. s->hnext = s->hnext->hnext;
  125. else
  126. printf("AP: could not remove AP " MACSTR " from hash table\n",
  127. MAC2STR(ap->addr));
  128. }
  129. static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
  130. {
  131. ap_ap_hash_del(iface, ap);
  132. ap_ap_list_del(iface, ap);
  133. ap_ap_iter_list_del(iface, ap);
  134. iface->num_ap--;
  135. os_free(ap);
  136. }
  137. static void hostapd_free_aps(struct hostapd_iface *iface)
  138. {
  139. struct ap_info *ap, *prev;
  140. ap = iface->ap_list;
  141. while (ap) {
  142. prev = ap;
  143. ap = ap->next;
  144. ap_free_ap(iface, prev);
  145. }
  146. iface->ap_list = NULL;
  147. }
  148. int ap_ap_for_each(struct hostapd_iface *iface,
  149. int (*func)(struct ap_info *s, void *data), void *data)
  150. {
  151. struct ap_info *s;
  152. int ret = 0;
  153. s = iface->ap_list;
  154. while (s) {
  155. ret = func(s, data);
  156. if (ret)
  157. break;
  158. s = s->next;
  159. }
  160. return ret;
  161. }
  162. static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
  163. {
  164. struct ap_info *ap;
  165. ap = os_zalloc(sizeof(struct ap_info));
  166. if (ap == NULL)
  167. return NULL;
  168. /* initialize AP info data */
  169. os_memcpy(ap->addr, addr, ETH_ALEN);
  170. ap_ap_list_add(iface, ap);
  171. iface->num_ap++;
  172. ap_ap_hash_add(iface, ap);
  173. ap_ap_iter_list_add(iface, ap);
  174. if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
  175. wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
  176. MACSTR " from AP table", MAC2STR(ap->prev->addr));
  177. ap_free_ap(iface, ap->prev);
  178. }
  179. return ap;
  180. }
  181. void ap_list_process_beacon(struct hostapd_iface *iface,
  182. struct ieee80211_mgmt *mgmt,
  183. struct ieee802_11_elems *elems,
  184. struct hostapd_frame_info *fi)
  185. {
  186. struct ap_info *ap;
  187. int new_ap = 0;
  188. size_t len;
  189. int set_beacon = 0;
  190. if (iface->conf->ap_table_max_size < 1)
  191. return;
  192. ap = ap_get_ap(iface, mgmt->bssid);
  193. if (!ap) {
  194. ap = ap_ap_add(iface, mgmt->bssid);
  195. if (!ap) {
  196. printf("Failed to allocate AP information entry\n");
  197. return;
  198. }
  199. new_ap = 1;
  200. }
  201. ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
  202. ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
  203. if (elems->ssid) {
  204. len = elems->ssid_len;
  205. if (len >= sizeof(ap->ssid))
  206. len = sizeof(ap->ssid) - 1;
  207. os_memcpy(ap->ssid, elems->ssid, len);
  208. ap->ssid[len] = '\0';
  209. ap->ssid_len = len;
  210. }
  211. os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
  212. len = 0;
  213. if (elems->supp_rates) {
  214. len = elems->supp_rates_len;
  215. if (len > WLAN_SUPP_RATES_MAX)
  216. len = WLAN_SUPP_RATES_MAX;
  217. os_memcpy(ap->supported_rates, elems->supp_rates, len);
  218. }
  219. if (elems->ext_supp_rates) {
  220. int len2;
  221. if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
  222. len2 = WLAN_SUPP_RATES_MAX - len;
  223. else
  224. len2 = elems->ext_supp_rates_len;
  225. os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
  226. len2);
  227. }
  228. ap->wpa = elems->wpa_ie != NULL;
  229. if (elems->erp_info && elems->erp_info_len == 1)
  230. ap->erp = elems->erp_info[0];
  231. else
  232. ap->erp = -1;
  233. if (elems->ds_params && elems->ds_params_len == 1)
  234. ap->channel = elems->ds_params[0];
  235. else if (fi)
  236. ap->channel = fi->channel;
  237. if (elems->ht_capabilities)
  238. ap->ht_support = 1;
  239. else
  240. ap->ht_support = 0;
  241. ap->num_beacons++;
  242. time(&ap->last_beacon);
  243. if (fi) {
  244. ap->ssi_signal = fi->ssi_signal;
  245. ap->datarate = fi->datarate;
  246. }
  247. if (!new_ap && ap != iface->ap_list) {
  248. /* move AP entry into the beginning of the list so that the
  249. * oldest entry is always in the end of the list */
  250. ap_ap_list_del(iface, ap);
  251. ap_ap_list_add(iface, ap);
  252. }
  253. if (!iface->olbc &&
  254. ap_list_beacon_olbc(iface, ap)) {
  255. iface->olbc = 1;
  256. wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
  257. "protection", MAC2STR(ap->addr));
  258. set_beacon++;
  259. }
  260. #ifdef CONFIG_IEEE80211N
  261. if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
  262. iface->olbc_ht = 1;
  263. hostapd_ht_operation_update(iface);
  264. wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
  265. " - enable protection", MAC2STR(ap->addr));
  266. set_beacon++;
  267. }
  268. #endif /* CONFIG_IEEE80211N */
  269. if (set_beacon)
  270. ieee802_11_set_beacons(iface);
  271. }
  272. static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
  273. {
  274. struct hostapd_iface *iface = eloop_ctx;
  275. time_t now;
  276. struct ap_info *ap;
  277. int set_beacon = 0;
  278. eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
  279. if (!iface->ap_list)
  280. return;
  281. time(&now);
  282. while (iface->ap_list) {
  283. ap = iface->ap_list->prev;
  284. if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
  285. now)
  286. break;
  287. ap_free_ap(iface, ap);
  288. }
  289. if (iface->olbc || iface->olbc_ht) {
  290. int olbc = 0;
  291. int olbc_ht = 0;
  292. ap = iface->ap_list;
  293. while (ap && (olbc == 0 || olbc_ht == 0)) {
  294. if (ap_list_beacon_olbc(iface, ap))
  295. olbc = 1;
  296. #ifdef CONFIG_IEEE80211N
  297. if (ap_list_beacon_olbc_ht(iface, ap))
  298. olbc_ht = 1;
  299. #endif /* CONFIG_IEEE80211N */
  300. ap = ap->next;
  301. }
  302. if (!olbc && iface->olbc) {
  303. wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
  304. iface->olbc = 0;
  305. set_beacon++;
  306. }
  307. #ifdef CONFIG_IEEE80211N
  308. if (!olbc_ht && iface->olbc_ht) {
  309. wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
  310. iface->olbc_ht = 0;
  311. hostapd_ht_operation_update(iface);
  312. set_beacon++;
  313. }
  314. #endif /* CONFIG_IEEE80211N */
  315. }
  316. if (set_beacon)
  317. ieee802_11_set_beacons(iface);
  318. }
  319. int ap_list_init(struct hostapd_iface *iface)
  320. {
  321. eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
  322. return 0;
  323. }
  324. void ap_list_deinit(struct hostapd_iface *iface)
  325. {
  326. eloop_cancel_timeout(ap_list_timer, iface, NULL);
  327. hostapd_free_aps(iface);
  328. }