ap_list.c 11 KB

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