3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2006, Devicescape Software, Inc.
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
11 #include "utils/includes.h"
13 #include "utils/common.h"
14 #include "utils/eloop.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
18 #include "ap_config.h"
19 #include "ieee802_11.h"
25 /* AP list is a double linked list with head->prev pointing to the end of the
26 * list and tail->next = NULL. Entries are moved to the head of the list
27 * whenever a beacon has been received from the AP in question. The tail entry
28 * in this link will thus be the least recently used entry. */
31 static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
35 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
36 iface->conf->channel != ap->channel)
39 if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
42 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
43 int rate = (ap->supported_rates[i] & 0x7f) * 5;
44 if (rate == 60 || rate == 90 || rate > 110)
52 static struct ap_info * ap_get_ap(struct hostapd_iface *iface, const u8 *ap)
56 s = iface->ap_hash[STA_HASH(ap)];
57 while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
63 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
66 ap->prev = iface->ap_list->prev;
67 iface->ap_list->prev = ap;
70 ap->next = iface->ap_list;
75 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
77 if (iface->ap_list == ap)
78 iface->ap_list = ap->next;
80 ap->prev->next = ap->next;
83 ap->next->prev = ap->prev;
84 else if (iface->ap_list)
85 iface->ap_list->prev = ap->prev;
89 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
91 ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
92 iface->ap_hash[STA_HASH(ap->addr)] = ap;
96 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
100 s = iface->ap_hash[STA_HASH(ap->addr)];
101 if (s == NULL) return;
102 if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
103 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
107 while (s->hnext != NULL &&
108 os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
110 if (s->hnext != NULL)
111 s->hnext = s->hnext->hnext;
113 printf("AP: could not remove AP " MACSTR " from hash table\n",
118 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
120 ap_ap_hash_del(iface, ap);
121 ap_ap_list_del(iface, ap);
128 static void hostapd_free_aps(struct hostapd_iface *iface)
130 struct ap_info *ap, *prev;
137 ap_free_ap(iface, prev);
140 iface->ap_list = NULL;
144 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, const u8 *addr)
148 ap = os_zalloc(sizeof(struct ap_info));
152 /* initialize AP info data */
153 os_memcpy(ap->addr, addr, ETH_ALEN);
154 ap_ap_list_add(iface, ap);
156 ap_ap_hash_add(iface, ap);
158 if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
159 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
160 MACSTR " from AP table", MAC2STR(ap->prev->addr));
161 ap_free_ap(iface, ap->prev);
168 void ap_list_process_beacon(struct hostapd_iface *iface,
169 const struct ieee80211_mgmt *mgmt,
170 struct ieee802_11_elems *elems,
171 struct hostapd_frame_info *fi)
177 if (iface->conf->ap_table_max_size < 1)
180 ap = ap_get_ap(iface, mgmt->bssid);
182 ap = ap_ap_add(iface, mgmt->bssid);
184 printf("Failed to allocate AP information entry\n");
190 merge_byte_arrays(ap->supported_rates, WLAN_SUPP_RATES_MAX,
191 elems->supp_rates, elems->supp_rates_len,
192 elems->ext_supp_rates, elems->ext_supp_rates_len);
194 if (elems->erp_info && elems->erp_info_len == 1)
195 ap->erp = elems->erp_info[0];
199 if (elems->ds_params && elems->ds_params_len == 1)
200 ap->channel = elems->ds_params[0];
201 else if (elems->ht_operation && elems->ht_operation_len >= 1)
202 ap->channel = elems->ht_operation[0];
204 ap->channel = fi->channel;
206 if (elems->ht_capabilities)
211 os_get_reltime(&ap->last_beacon);
213 if (!new_ap && ap != iface->ap_list) {
214 /* move AP entry into the beginning of the list so that the
215 * oldest entry is always in the end of the list */
216 ap_ap_list_del(iface, ap);
217 ap_ap_list_add(iface, ap);
221 ap_list_beacon_olbc(iface, ap)) {
223 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR
224 " (channel %d) - enable protection",
225 MAC2STR(ap->addr), ap->channel);
229 #ifdef CONFIG_IEEE80211N
230 if (!iface->olbc_ht && !ap->ht_support &&
232 ap->channel == iface->conf->channel ||
233 ap->channel == iface->conf->channel +
234 iface->conf->secondary_channel * 4)) {
236 hostapd_ht_operation_update(iface);
237 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
238 " (channel %d) - enable protection",
239 MAC2STR(ap->addr), ap->channel);
242 #endif /* CONFIG_IEEE80211N */
245 ieee802_11_update_beacons(iface);
249 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
251 struct hostapd_iface *iface = eloop_ctx;
252 struct os_reltime now;
256 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
261 os_get_reltime(&now);
263 while (iface->ap_list) {
264 ap = iface->ap_list->prev;
265 if (!os_reltime_expired(&now, &ap->last_beacon,
266 iface->conf->ap_table_expiration_time))
269 ap_free_ap(iface, ap);
272 if (iface->olbc || iface->olbc_ht) {
277 while (ap && (olbc == 0 || olbc_ht == 0)) {
278 if (ap_list_beacon_olbc(iface, ap))
284 if (!olbc && iface->olbc) {
285 wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
289 #ifdef CONFIG_IEEE80211N
290 if (!olbc_ht && iface->olbc_ht) {
291 wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
293 hostapd_ht_operation_update(iface);
296 #endif /* CONFIG_IEEE80211N */
300 ieee802_11_update_beacons(iface);
304 int ap_list_init(struct hostapd_iface *iface)
306 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
311 void ap_list_deinit(struct hostapd_iface *iface)
313 eloop_cancel_timeout(ap_list_timer, iface, NULL);
314 hostapd_free_aps(iface);