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
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.
12 * Alternatively, this software may be distributed under the terms of BSD
15 * See README and COPYING for more details.
21 #include "ieee802_11.h"
24 #include "hw_features.h"
29 struct ieee80211_frame_info {
45 /* Note: this structure is otherwise identical to capture format used
46 * in linux-wlan-ng, but this additional field is used to provide meta
47 * data about the frame to hostapd. This was the easiest method for
48 * providing this information, but this might change in the future. */
50 } __attribute__ ((packed));
53 enum ieee80211_phytype {
54 ieee80211_phytype_fhss_dot11_97 = 1,
55 ieee80211_phytype_dsss_dot11_97 = 2,
56 ieee80211_phytype_irbaseband = 3,
57 ieee80211_phytype_dsss_dot11_b = 4,
58 ieee80211_phytype_pbcc_dot11_b = 5,
59 ieee80211_phytype_ofdm_dot11_g = 6,
60 ieee80211_phytype_pbcc_dot11_g = 7,
61 ieee80211_phytype_ofdm_dot11_a = 8,
62 ieee80211_phytype_dsss_dot11_turbog = 255,
63 ieee80211_phytype_dsss_dot11_turbo = 256,
67 /* AP list is a double linked list with head->prev pointing to the end of the
68 * list and tail->next = NULL. Entries are moved to the head of the list
69 * whenever a beacon has been received from the AP in question. The tail entry
70 * in this link will thus be the least recently used entry. */
73 static void ap_list_new_ap(struct hostapd_iface *iface, struct ap_info *ap)
75 wpa_printf(MSG_DEBUG, "New AP detected: " MACSTR, MAC2STR(ap->addr));
77 /* TODO: could send a notification message to an external program that
78 * would then determine whether a rogue AP has been detected */
82 static void ap_list_expired_ap(struct hostapd_iface *iface, struct ap_info *ap)
84 wpa_printf(MSG_DEBUG, "AP info expired: " MACSTR, MAC2STR(ap->addr));
86 /* TODO: could send a notification message to an external program */
90 static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
94 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
95 ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
96 iface->conf->channel != ap->channel)
99 if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
102 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
103 int rate = (ap->supported_rates[i] & 0x7f) * 5;
104 if (rate == 60 || rate == 90 || rate > 110)
112 #ifdef CONFIG_IEEE80211N
113 static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
116 return !ap->ht_support;
118 #endif /* CONFIG_IEEE80211N */
121 struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
125 s = iface->ap_hash[STA_HASH(ap)];
126 while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
132 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
134 if (iface->ap_list) {
135 ap->prev = iface->ap_list->prev;
136 iface->ap_list->prev = ap;
139 ap->next = iface->ap_list;
144 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
146 if (iface->ap_list == ap)
147 iface->ap_list = ap->next;
149 ap->prev->next = ap->next;
152 ap->next->prev = ap->prev;
153 else if (iface->ap_list)
154 iface->ap_list->prev = ap->prev;
158 static void ap_ap_iter_list_add(struct hostapd_iface *iface,
161 if (iface->ap_iter_list) {
162 ap->iter_prev = iface->ap_iter_list->iter_prev;
163 iface->ap_iter_list->iter_prev = ap;
166 ap->iter_next = iface->ap_iter_list;
167 iface->ap_iter_list = ap;
171 static void ap_ap_iter_list_del(struct hostapd_iface *iface,
174 if (iface->ap_iter_list == ap)
175 iface->ap_iter_list = ap->iter_next;
177 ap->iter_prev->iter_next = ap->iter_next;
180 ap->iter_next->iter_prev = ap->iter_prev;
181 else if (iface->ap_iter_list)
182 iface->ap_iter_list->iter_prev = ap->iter_prev;
186 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
188 ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
189 iface->ap_hash[STA_HASH(ap->addr)] = ap;
193 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
197 s = iface->ap_hash[STA_HASH(ap->addr)];
198 if (s == NULL) return;
199 if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
200 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
204 while (s->hnext != NULL &&
205 os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
207 if (s->hnext != NULL)
208 s->hnext = s->hnext->hnext;
210 printf("AP: could not remove AP " MACSTR " from hash table\n",
215 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
217 ap_ap_hash_del(iface, ap);
218 ap_ap_list_del(iface, ap);
219 ap_ap_iter_list_del(iface, ap);
226 static void hostapd_free_aps(struct hostapd_iface *iface)
228 struct ap_info *ap, *prev;
235 ap_free_ap(iface, prev);
238 iface->ap_list = NULL;
242 int ap_ap_for_each(struct hostapd_iface *iface,
243 int (*func)(struct ap_info *s, void *data), void *data)
261 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
265 ap = os_zalloc(sizeof(struct ap_info));
269 /* initialize AP info data */
270 os_memcpy(ap->addr, addr, ETH_ALEN);
271 ap_ap_list_add(iface, ap);
273 ap_ap_hash_add(iface, ap);
274 ap_ap_iter_list_add(iface, ap);
276 if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
277 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
278 MACSTR " from AP table", MAC2STR(ap->prev->addr));
279 if (iface->conf->passive_scan_interval > 0)
280 ap_list_expired_ap(iface, ap->prev);
281 ap_free_ap(iface, ap->prev);
288 void ap_list_process_beacon(struct hostapd_iface *iface,
289 struct ieee80211_mgmt *mgmt,
290 struct ieee802_11_elems *elems,
291 struct hostapd_frame_info *fi)
298 if (iface->conf->ap_table_max_size < 1)
301 ap = ap_get_ap(iface, mgmt->bssid);
303 ap = ap_ap_add(iface, mgmt->bssid);
305 printf("Failed to allocate AP information entry\n");
311 ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
312 ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
315 len = elems->ssid_len;
316 if (len >= sizeof(ap->ssid))
317 len = sizeof(ap->ssid) - 1;
318 os_memcpy(ap->ssid, elems->ssid, len);
319 ap->ssid[len] = '\0';
323 os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
325 if (elems->supp_rates) {
326 len = elems->supp_rates_len;
327 if (len > WLAN_SUPP_RATES_MAX)
328 len = WLAN_SUPP_RATES_MAX;
329 os_memcpy(ap->supported_rates, elems->supp_rates, len);
331 if (elems->ext_supp_rates) {
333 if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
334 len2 = WLAN_SUPP_RATES_MAX - len;
336 len2 = elems->ext_supp_rates_len;
337 os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
341 ap->wpa = elems->wpa_ie != NULL;
343 if (elems->erp_info && elems->erp_info_len == 1)
344 ap->erp = elems->erp_info[0];
348 if (elems->ds_params && elems->ds_params_len == 1)
349 ap->channel = elems->ds_params[0];
351 ap->channel = fi->channel;
353 if (elems->ht_capabilities)
359 time(&ap->last_beacon);
361 ap->phytype = fi->phytype;
362 ap->ssi_signal = fi->ssi_signal;
363 ap->datarate = fi->datarate;
367 if (iface->conf->passive_scan_interval > 0)
368 ap_list_new_ap(iface, ap);
369 } else if (ap != iface->ap_list) {
370 /* move AP entry into the beginning of the list so that the
371 * oldest entry is always in the end of the list */
372 ap_ap_list_del(iface, ap);
373 ap_ap_list_add(iface, ap);
377 ap_list_beacon_olbc(iface, ap)) {
379 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
380 "protection", MAC2STR(ap->addr));
384 #ifdef CONFIG_IEEE80211N
385 if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
387 hostapd_ht_operation_update(iface);
388 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
389 " - enable protection", MAC2STR(ap->addr));
392 #endif /* CONFIG_IEEE80211N */
395 ieee802_11_set_beacons(iface);
399 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
401 struct hostapd_iface *iface = eloop_ctx;
406 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
413 /* FIX: it looks like jkm-Purina ended up in busy loop in this
414 * function. Apparently, something can still cause a loop in the AP
417 while (iface->ap_list) {
418 ap = iface->ap_list->prev;
419 if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
423 if (iface->conf->passive_scan_interval > 0)
424 ap_list_expired_ap(iface, ap);
425 ap_free_ap(iface, ap);
428 if (iface->olbc || iface->olbc_ht) {
433 while (ap && (olbc == 0 || olbc_ht == 0)) {
434 if (ap_list_beacon_olbc(iface, ap))
436 #ifdef CONFIG_IEEE80211N
437 if (ap_list_beacon_olbc_ht(iface, ap))
439 #endif /* CONFIG_IEEE80211N */
442 if (!olbc && iface->olbc) {
443 wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
447 #ifdef CONFIG_IEEE80211N
448 if (!olbc_ht && iface->olbc_ht) {
449 wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
451 hostapd_ht_operation_update(iface);
454 #endif /* CONFIG_IEEE80211N */
458 ieee802_11_set_beacons(iface);
462 int ap_list_init(struct hostapd_iface *iface)
464 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
469 void ap_list_deinit(struct hostapd_iface *iface)
471 eloop_cancel_timeout(ap_list_timer, iface, NULL);
472 hostapd_free_aps(iface);
476 int ap_list_reconfig(struct hostapd_iface *iface,
477 struct hostapd_config *oldconf)
482 if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
483 iface->conf->ap_table_expiration_time ==
484 oldconf->ap_table_expiration_time)
489 while (iface->ap_list) {
490 ap = iface->ap_list->prev;
491 if (iface->num_ap <= iface->conf->ap_table_max_size &&
492 ap->last_beacon + iface->conf->ap_table_expiration_time >=
496 if (iface->conf->passive_scan_interval > 0)
497 ap_list_expired_ap(iface, iface->ap_list->prev);
498 ap_free_ap(iface, iface->ap_list->prev);