ap_list.c 11 KB

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