ap_list.c 11 KB

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