ap_list.c 11 KB

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