ap_list.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
  88. {
  89. struct ap_info *s;
  90. s = iface->ap_hash[STA_HASH(ap)];
  91. while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
  92. s = s->hnext;
  93. return s;
  94. }
  95. static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
  96. {
  97. if (iface->ap_list) {
  98. ap->prev = iface->ap_list->prev;
  99. iface->ap_list->prev = ap;
  100. } else
  101. ap->prev = ap;
  102. ap->next = iface->ap_list;
  103. iface->ap_list = ap;
  104. }
  105. static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
  106. {
  107. if (iface->ap_list == ap)
  108. iface->ap_list = ap->next;
  109. else
  110. ap->prev->next = ap->next;
  111. if (ap->next)
  112. ap->next->prev = ap->prev;
  113. else if (iface->ap_list)
  114. iface->ap_list->prev = ap->prev;
  115. }
  116. static void ap_ap_iter_list_add(struct hostapd_iface *iface,
  117. struct ap_info *ap)
  118. {
  119. if (iface->ap_iter_list) {
  120. ap->iter_prev = iface->ap_iter_list->iter_prev;
  121. iface->ap_iter_list->iter_prev = ap;
  122. } else
  123. ap->iter_prev = ap;
  124. ap->iter_next = iface->ap_iter_list;
  125. iface->ap_iter_list = ap;
  126. }
  127. static void ap_ap_iter_list_del(struct hostapd_iface *iface,
  128. struct ap_info *ap)
  129. {
  130. if (iface->ap_iter_list == ap)
  131. iface->ap_iter_list = ap->iter_next;
  132. else
  133. ap->iter_prev->iter_next = ap->iter_next;
  134. if (ap->iter_next)
  135. ap->iter_next->iter_prev = ap->iter_prev;
  136. else if (iface->ap_iter_list)
  137. iface->ap_iter_list->iter_prev = ap->iter_prev;
  138. }
  139. static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
  140. {
  141. ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
  142. iface->ap_hash[STA_HASH(ap->addr)] = ap;
  143. }
  144. static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
  145. {
  146. struct ap_info *s;
  147. s = iface->ap_hash[STA_HASH(ap->addr)];
  148. if (s == NULL) return;
  149. if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
  150. iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
  151. return;
  152. }
  153. while (s->hnext != NULL &&
  154. os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
  155. s = s->hnext;
  156. if (s->hnext != NULL)
  157. s->hnext = s->hnext->hnext;
  158. else
  159. printf("AP: could not remove AP " MACSTR " from hash table\n",
  160. MAC2STR(ap->addr));
  161. }
  162. static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
  163. {
  164. ap_ap_hash_del(iface, ap);
  165. ap_ap_list_del(iface, ap);
  166. ap_ap_iter_list_del(iface, ap);
  167. iface->num_ap--;
  168. os_free(ap);
  169. }
  170. static void hostapd_free_aps(struct hostapd_iface *iface)
  171. {
  172. struct ap_info *ap, *prev;
  173. ap = iface->ap_list;
  174. while (ap) {
  175. prev = ap;
  176. ap = ap->next;
  177. ap_free_ap(iface, prev);
  178. }
  179. iface->ap_list = NULL;
  180. }
  181. int ap_ap_for_each(struct hostapd_iface *iface,
  182. int (*func)(struct ap_info *s, void *data), void *data)
  183. {
  184. struct ap_info *s;
  185. int ret = 0;
  186. s = iface->ap_list;
  187. while (s) {
  188. ret = func(s, data);
  189. if (ret)
  190. break;
  191. s = s->next;
  192. }
  193. return ret;
  194. }
  195. static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
  196. {
  197. struct ap_info *ap;
  198. ap = os_zalloc(sizeof(struct ap_info));
  199. if (ap == NULL)
  200. return NULL;
  201. /* initialize AP info data */
  202. os_memcpy(ap->addr, addr, ETH_ALEN);
  203. ap_ap_list_add(iface, ap);
  204. iface->num_ap++;
  205. ap_ap_hash_add(iface, ap);
  206. ap_ap_iter_list_add(iface, ap);
  207. if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
  208. wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
  209. MACSTR " from AP table", MAC2STR(ap->prev->addr));
  210. if (iface->conf->passive_scan_interval > 0)
  211. ap_list_expired_ap(iface, ap->prev);
  212. ap_free_ap(iface, ap->prev);
  213. }
  214. return ap;
  215. }
  216. void ap_list_process_beacon(struct hostapd_iface *iface,
  217. struct ieee80211_mgmt *mgmt,
  218. struct ieee802_11_elems *elems,
  219. struct hostapd_frame_info *fi)
  220. {
  221. struct ap_info *ap;
  222. int new_ap = 0;
  223. size_t len;
  224. if (iface->conf->ap_table_max_size < 1)
  225. return;
  226. ap = ap_get_ap(iface, mgmt->bssid);
  227. if (!ap) {
  228. ap = ap_ap_add(iface, mgmt->bssid);
  229. if (!ap) {
  230. printf("Failed to allocate AP information entry\n");
  231. return;
  232. }
  233. new_ap = 1;
  234. }
  235. ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
  236. ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
  237. if (elems->ssid) {
  238. len = elems->ssid_len;
  239. if (len >= sizeof(ap->ssid))
  240. len = sizeof(ap->ssid) - 1;
  241. os_memcpy(ap->ssid, elems->ssid, len);
  242. ap->ssid[len] = '\0';
  243. ap->ssid_len = len;
  244. }
  245. os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
  246. len = 0;
  247. if (elems->supp_rates) {
  248. len = elems->supp_rates_len;
  249. if (len > WLAN_SUPP_RATES_MAX)
  250. len = WLAN_SUPP_RATES_MAX;
  251. os_memcpy(ap->supported_rates, elems->supp_rates, len);
  252. }
  253. if (elems->ext_supp_rates) {
  254. int len2;
  255. if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
  256. len2 = WLAN_SUPP_RATES_MAX - len;
  257. else
  258. len2 = elems->ext_supp_rates_len;
  259. os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
  260. len2);
  261. }
  262. ap->wpa = elems->wpa_ie != NULL;
  263. if (elems->erp_info && elems->erp_info_len == 1)
  264. ap->erp = elems->erp_info[0];
  265. else
  266. ap->erp = -1;
  267. if (elems->ds_params && elems->ds_params_len == 1)
  268. ap->channel = elems->ds_params[0];
  269. else if (fi)
  270. ap->channel = fi->channel;
  271. ap->num_beacons++;
  272. time(&ap->last_beacon);
  273. if (fi) {
  274. ap->phytype = fi->phytype;
  275. ap->ssi_signal = fi->ssi_signal;
  276. ap->datarate = fi->datarate;
  277. }
  278. if (new_ap) {
  279. if (iface->conf->passive_scan_interval > 0)
  280. ap_list_new_ap(iface, ap);
  281. } else if (ap != iface->ap_list) {
  282. /* move AP entry into the beginning of the list so that the
  283. * oldest entry is always in the end of the list */
  284. ap_ap_list_del(iface, ap);
  285. ap_ap_list_add(iface, ap);
  286. }
  287. if (!iface->olbc &&
  288. ap_list_beacon_olbc(iface, ap)) {
  289. struct hostapd_data *hapd = iface->bss[0];
  290. iface->olbc = 1;
  291. wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
  292. "protection", MAC2STR(ap->addr));
  293. ieee802_11_set_beacons(hapd->iface);
  294. }
  295. }
  296. static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
  297. {
  298. struct hostapd_iface *iface = eloop_ctx;
  299. time_t now;
  300. struct ap_info *ap;
  301. eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
  302. if (!iface->ap_list)
  303. return;
  304. time(&now);
  305. /* FIX: it looks like jkm-Purina ended up in busy loop in this
  306. * function. Apparently, something can still cause a loop in the AP
  307. * list.. */
  308. while (iface->ap_list) {
  309. ap = iface->ap_list->prev;
  310. if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
  311. now)
  312. break;
  313. if (iface->conf->passive_scan_interval > 0)
  314. ap_list_expired_ap(iface, ap);
  315. ap_free_ap(iface, ap);
  316. }
  317. if (iface->olbc) {
  318. int olbc = 0;
  319. ap = iface->ap_list;
  320. while (ap) {
  321. if (ap_list_beacon_olbc(iface, ap)) {
  322. olbc = 1;
  323. break;
  324. }
  325. ap = ap->next;
  326. }
  327. if (!olbc) {
  328. struct hostapd_data *hapd = iface->bss[0];
  329. wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
  330. iface->olbc = 0;
  331. ieee802_11_set_beacons(hapd->iface);
  332. }
  333. }
  334. }
  335. int ap_list_init(struct hostapd_iface *iface)
  336. {
  337. eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
  338. return 0;
  339. }
  340. void ap_list_deinit(struct hostapd_iface *iface)
  341. {
  342. eloop_cancel_timeout(ap_list_timer, iface, NULL);
  343. hostapd_free_aps(iface);
  344. }
  345. int ap_list_reconfig(struct hostapd_iface *iface,
  346. struct hostapd_config *oldconf)
  347. {
  348. time_t now;
  349. struct ap_info *ap;
  350. if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
  351. iface->conf->ap_table_expiration_time ==
  352. oldconf->ap_table_expiration_time)
  353. return 0;
  354. time(&now);
  355. while (iface->ap_list) {
  356. ap = iface->ap_list->prev;
  357. if (iface->num_ap <= iface->conf->ap_table_max_size &&
  358. ap->last_beacon + iface->conf->ap_table_expiration_time >=
  359. now)
  360. break;
  361. if (iface->conf->passive_scan_interval > 0)
  362. ap_list_expired_ap(iface, iface->ap_list->prev);
  363. ap_free_ap(iface, iface->ap_list->prev);
  364. }
  365. return 0;
  366. }