ap_list.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 "hostapd.h"
  19. #include "ieee802_11.h"
  20. #include "eloop.h"
  21. #include "ap_list.h"
  22. #include "hw_features.h"
  23. #include "beacon.h"
  24. struct ieee80211_frame_info {
  25. u32 version;
  26. u32 length;
  27. u64 mactime;
  28. u64 hosttime;
  29. u32 phytype;
  30. u32 channel;
  31. u32 datarate;
  32. u32 antenna;
  33. u32 priority;
  34. u32 ssi_type;
  35. u32 ssi_signal;
  36. u32 ssi_noise;
  37. u32 preamble;
  38. u32 encoding;
  39. /* Note: this structure is otherwise identical to capture format used
  40. * in linux-wlan-ng, but this additional field is used to provide meta
  41. * data about the frame to hostapd. This was the easiest method for
  42. * providing this information, but this might change in the future. */
  43. u32 msg_type;
  44. } __attribute__ ((packed));
  45. enum ieee80211_phytype {
  46. ieee80211_phytype_fhss_dot11_97 = 1,
  47. ieee80211_phytype_dsss_dot11_97 = 2,
  48. ieee80211_phytype_irbaseband = 3,
  49. ieee80211_phytype_dsss_dot11_b = 4,
  50. ieee80211_phytype_pbcc_dot11_b = 5,
  51. ieee80211_phytype_ofdm_dot11_g = 6,
  52. ieee80211_phytype_pbcc_dot11_g = 7,
  53. ieee80211_phytype_ofdm_dot11_a = 8,
  54. ieee80211_phytype_dsss_dot11_turbog = 255,
  55. ieee80211_phytype_dsss_dot11_turbo = 256,
  56. };
  57. /* AP list is a double linked list with head->prev pointing to the end of the
  58. * list and tail->next = NULL. Entries are moved to the head of the list
  59. * whenever a beacon has been received from the AP in question. The tail entry
  60. * in this link will thus be the least recently used entry. */
  61. static void ap_list_new_ap(struct hostapd_iface *iface, struct ap_info *ap)
  62. {
  63. wpa_printf(MSG_DEBUG, "New AP detected: " MACSTR, MAC2STR(ap->addr));
  64. /* TODO: could send a notification message to an external program that
  65. * would then determine whether a rogue AP has been detected */
  66. }
  67. static void ap_list_expired_ap(struct hostapd_iface *iface, struct ap_info *ap)
  68. {
  69. wpa_printf(MSG_DEBUG, "AP info expired: " MACSTR, MAC2STR(ap->addr));
  70. /* TODO: could send a notification message to an external program */
  71. }
  72. static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
  73. {
  74. int i;
  75. if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
  76. ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
  77. iface->conf->channel != ap->channel)
  78. return 0;
  79. if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
  80. return 1;
  81. for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
  82. int rate = (ap->supported_rates[i] & 0x7f) * 5;
  83. if (rate == 60 || rate == 90 || rate > 110)
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. #ifdef CONFIG_IEEE80211N
  89. static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
  90. struct ap_info *ap)
  91. {
  92. return !ap->ht_support;
  93. }
  94. #endif /* CONFIG_IEEE80211N */
  95. struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
  96. {
  97. struct ap_info *s;
  98. s = iface->ap_hash[STA_HASH(ap)];
  99. while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
  100. s = s->hnext;
  101. return s;
  102. }
  103. static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
  104. {
  105. if (iface->ap_list) {
  106. ap->prev = iface->ap_list->prev;
  107. iface->ap_list->prev = ap;
  108. } else
  109. ap->prev = ap;
  110. ap->next = iface->ap_list;
  111. iface->ap_list = ap;
  112. }
  113. static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
  114. {
  115. if (iface->ap_list == ap)
  116. iface->ap_list = ap->next;
  117. else
  118. ap->prev->next = ap->next;
  119. if (ap->next)
  120. ap->next->prev = ap->prev;
  121. else if (iface->ap_list)
  122. iface->ap_list->prev = ap->prev;
  123. }
  124. static void ap_ap_iter_list_add(struct hostapd_iface *iface,
  125. struct ap_info *ap)
  126. {
  127. if (iface->ap_iter_list) {
  128. ap->iter_prev = iface->ap_iter_list->iter_prev;
  129. iface->ap_iter_list->iter_prev = ap;
  130. } else
  131. ap->iter_prev = ap;
  132. ap->iter_next = iface->ap_iter_list;
  133. iface->ap_iter_list = ap;
  134. }
  135. static void ap_ap_iter_list_del(struct hostapd_iface *iface,
  136. struct ap_info *ap)
  137. {
  138. if (iface->ap_iter_list == ap)
  139. iface->ap_iter_list = ap->iter_next;
  140. else
  141. ap->iter_prev->iter_next = ap->iter_next;
  142. if (ap->iter_next)
  143. ap->iter_next->iter_prev = ap->iter_prev;
  144. else if (iface->ap_iter_list)
  145. iface->ap_iter_list->iter_prev = ap->iter_prev;
  146. }
  147. static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
  148. {
  149. ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
  150. iface->ap_hash[STA_HASH(ap->addr)] = ap;
  151. }
  152. static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
  153. {
  154. struct ap_info *s;
  155. s = iface->ap_hash[STA_HASH(ap->addr)];
  156. if (s == NULL) return;
  157. if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
  158. iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
  159. return;
  160. }
  161. while (s->hnext != NULL &&
  162. os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
  163. s = s->hnext;
  164. if (s->hnext != NULL)
  165. s->hnext = s->hnext->hnext;
  166. else
  167. printf("AP: could not remove AP " MACSTR " from hash table\n",
  168. MAC2STR(ap->addr));
  169. }
  170. static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
  171. {
  172. ap_ap_hash_del(iface, ap);
  173. ap_ap_list_del(iface, ap);
  174. ap_ap_iter_list_del(iface, ap);
  175. iface->num_ap--;
  176. os_free(ap);
  177. }
  178. static void hostapd_free_aps(struct hostapd_iface *iface)
  179. {
  180. struct ap_info *ap, *prev;
  181. ap = iface->ap_list;
  182. while (ap) {
  183. prev = ap;
  184. ap = ap->next;
  185. ap_free_ap(iface, prev);
  186. }
  187. iface->ap_list = NULL;
  188. }
  189. int ap_ap_for_each(struct hostapd_iface *iface,
  190. int (*func)(struct ap_info *s, void *data), void *data)
  191. {
  192. struct ap_info *s;
  193. int ret = 0;
  194. s = iface->ap_list;
  195. while (s) {
  196. ret = func(s, data);
  197. if (ret)
  198. break;
  199. s = s->next;
  200. }
  201. return ret;
  202. }
  203. static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
  204. {
  205. struct ap_info *ap;
  206. ap = os_zalloc(sizeof(struct ap_info));
  207. if (ap == NULL)
  208. return NULL;
  209. /* initialize AP info data */
  210. os_memcpy(ap->addr, addr, ETH_ALEN);
  211. ap_ap_list_add(iface, ap);
  212. iface->num_ap++;
  213. ap_ap_hash_add(iface, ap);
  214. ap_ap_iter_list_add(iface, ap);
  215. if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
  216. wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
  217. MACSTR " from AP table", MAC2STR(ap->prev->addr));
  218. if (iface->conf->passive_scan_interval > 0)
  219. ap_list_expired_ap(iface, ap->prev);
  220. ap_free_ap(iface, ap->prev);
  221. }
  222. return ap;
  223. }
  224. void ap_list_process_beacon(struct hostapd_iface *iface,
  225. struct ieee80211_mgmt *mgmt,
  226. struct ieee802_11_elems *elems,
  227. struct hostapd_frame_info *fi)
  228. {
  229. struct ap_info *ap;
  230. int new_ap = 0;
  231. size_t len;
  232. int set_beacon = 0;
  233. if (iface->conf->ap_table_max_size < 1)
  234. return;
  235. ap = ap_get_ap(iface, mgmt->bssid);
  236. if (!ap) {
  237. ap = ap_ap_add(iface, mgmt->bssid);
  238. if (!ap) {
  239. printf("Failed to allocate AP information entry\n");
  240. return;
  241. }
  242. new_ap = 1;
  243. }
  244. ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
  245. ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
  246. if (elems->ssid) {
  247. len = elems->ssid_len;
  248. if (len >= sizeof(ap->ssid))
  249. len = sizeof(ap->ssid) - 1;
  250. os_memcpy(ap->ssid, elems->ssid, len);
  251. ap->ssid[len] = '\0';
  252. ap->ssid_len = len;
  253. }
  254. os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
  255. len = 0;
  256. if (elems->supp_rates) {
  257. len = elems->supp_rates_len;
  258. if (len > WLAN_SUPP_RATES_MAX)
  259. len = WLAN_SUPP_RATES_MAX;
  260. os_memcpy(ap->supported_rates, elems->supp_rates, len);
  261. }
  262. if (elems->ext_supp_rates) {
  263. int len2;
  264. if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
  265. len2 = WLAN_SUPP_RATES_MAX - len;
  266. else
  267. len2 = elems->ext_supp_rates_len;
  268. os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
  269. len2);
  270. }
  271. ap->wpa = elems->wpa_ie != NULL;
  272. if (elems->erp_info && elems->erp_info_len == 1)
  273. ap->erp = elems->erp_info[0];
  274. else
  275. ap->erp = -1;
  276. if (elems->ds_params && elems->ds_params_len == 1)
  277. ap->channel = elems->ds_params[0];
  278. else if (fi)
  279. ap->channel = fi->channel;
  280. if (elems->ht_capabilities)
  281. ap->ht_support = 1;
  282. else
  283. ap->ht_support = 0;
  284. ap->num_beacons++;
  285. time(&ap->last_beacon);
  286. if (fi) {
  287. ap->phytype = fi->phytype;
  288. ap->ssi_signal = fi->ssi_signal;
  289. ap->datarate = fi->datarate;
  290. }
  291. if (new_ap) {
  292. if (iface->conf->passive_scan_interval > 0)
  293. ap_list_new_ap(iface, ap);
  294. } else if (ap != iface->ap_list) {
  295. /* move AP entry into the beginning of the list so that the
  296. * oldest entry is always in the end of the list */
  297. ap_ap_list_del(iface, ap);
  298. ap_ap_list_add(iface, ap);
  299. }
  300. if (!iface->olbc &&
  301. ap_list_beacon_olbc(iface, ap)) {
  302. iface->olbc = 1;
  303. wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
  304. "protection", MAC2STR(ap->addr));
  305. set_beacon++;
  306. }
  307. #ifdef CONFIG_IEEE80211N
  308. if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
  309. iface->olbc_ht = 1;
  310. hostapd_ht_operation_update(iface);
  311. wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
  312. " - enable protection", MAC2STR(ap->addr));
  313. set_beacon++;
  314. }
  315. #endif /* CONFIG_IEEE80211N */
  316. if (set_beacon)
  317. ieee802_11_set_beacons(iface);
  318. }
  319. static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
  320. {
  321. struct hostapd_iface *iface = eloop_ctx;
  322. time_t now;
  323. struct ap_info *ap;
  324. int set_beacon = 0;
  325. eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
  326. if (!iface->ap_list)
  327. return;
  328. time(&now);
  329. /* FIX: it looks like jkm-Purina ended up in busy loop in this
  330. * function. Apparently, something can still cause a loop in the AP
  331. * list.. */
  332. while (iface->ap_list) {
  333. ap = iface->ap_list->prev;
  334. if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
  335. now)
  336. break;
  337. if (iface->conf->passive_scan_interval > 0)
  338. ap_list_expired_ap(iface, ap);
  339. ap_free_ap(iface, ap);
  340. }
  341. if (iface->olbc || iface->olbc_ht) {
  342. int olbc = 0;
  343. int olbc_ht = 0;
  344. ap = iface->ap_list;
  345. while (ap && (olbc == 0 || olbc_ht == 0)) {
  346. if (ap_list_beacon_olbc(iface, ap))
  347. olbc = 1;
  348. #ifdef CONFIG_IEEE80211N
  349. if (ap_list_beacon_olbc_ht(iface, ap))
  350. olbc_ht = 1;
  351. #endif /* CONFIG_IEEE80211N */
  352. ap = ap->next;
  353. }
  354. if (!olbc && iface->olbc) {
  355. wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
  356. iface->olbc = 0;
  357. set_beacon++;
  358. }
  359. #ifdef CONFIG_IEEE80211N
  360. if (!olbc_ht && iface->olbc_ht) {
  361. wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
  362. iface->olbc_ht = 0;
  363. hostapd_ht_operation_update(iface);
  364. set_beacon++;
  365. }
  366. #endif /* CONFIG_IEEE80211N */
  367. }
  368. if (set_beacon)
  369. ieee802_11_set_beacons(iface);
  370. }
  371. int ap_list_init(struct hostapd_iface *iface)
  372. {
  373. eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
  374. return 0;
  375. }
  376. void ap_list_deinit(struct hostapd_iface *iface)
  377. {
  378. eloop_cancel_timeout(ap_list_timer, iface, NULL);
  379. hostapd_free_aps(iface);
  380. }
  381. int ap_list_reconfig(struct hostapd_iface *iface,
  382. struct hostapd_config *oldconf)
  383. {
  384. time_t now;
  385. struct ap_info *ap;
  386. if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
  387. iface->conf->ap_table_expiration_time ==
  388. oldconf->ap_table_expiration_time)
  389. return 0;
  390. time(&now);
  391. while (iface->ap_list) {
  392. ap = iface->ap_list->prev;
  393. if (iface->num_ap <= iface->conf->ap_table_max_size &&
  394. ap->last_beacon + iface->conf->ap_table_expiration_time >=
  395. now)
  396. break;
  397. if (iface->conf->passive_scan_interval > 0)
  398. ap_list_expired_ap(iface, iface->ap_list->prev);
  399. ap_free_ap(iface, iface->ap_list->prev);
  400. }
  401. return 0;
  402. }