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.
23 #include "ieee802_11.h"
27 #include "hw_features.h"
29 #include "drivers/driver.h"
32 struct ieee80211_frame_info {
48 /* Note: this structure is otherwise identical to capture format used
49 * in linux-wlan-ng, but this additional field is used to provide meta
50 * data about the frame to hostapd. This was the easiest method for
51 * providing this information, but this might change in the future. */
53 } __attribute__ ((packed));
56 enum ieee80211_phytype {
57 ieee80211_phytype_fhss_dot11_97 = 1,
58 ieee80211_phytype_dsss_dot11_97 = 2,
59 ieee80211_phytype_irbaseband = 3,
60 ieee80211_phytype_dsss_dot11_b = 4,
61 ieee80211_phytype_pbcc_dot11_b = 5,
62 ieee80211_phytype_ofdm_dot11_g = 6,
63 ieee80211_phytype_pbcc_dot11_g = 7,
64 ieee80211_phytype_ofdm_dot11_a = 8,
65 ieee80211_phytype_dsss_dot11_turbog = 255,
66 ieee80211_phytype_dsss_dot11_turbo = 256,
70 /* AP list is a double linked list with head->prev pointing to the end of the
71 * list and tail->next = NULL. Entries are moved to the head of the list
72 * whenever a beacon has been received from the AP in question. The tail entry
73 * in this link will thus be the least recently used entry. */
76 static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
80 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
81 ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
82 iface->conf->channel != ap->channel)
85 if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
88 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
89 int rate = (ap->supported_rates[i] & 0x7f) * 5;
90 if (rate == 60 || rate == 90 || rate > 110)
98 #ifdef CONFIG_IEEE80211N
99 static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
102 return !ap->ht_support;
104 #endif /* CONFIG_IEEE80211N */
107 struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
111 s = iface->ap_hash[STA_HASH(ap)];
112 while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
118 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
120 if (iface->ap_list) {
121 ap->prev = iface->ap_list->prev;
122 iface->ap_list->prev = ap;
125 ap->next = iface->ap_list;
130 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
132 if (iface->ap_list == ap)
133 iface->ap_list = ap->next;
135 ap->prev->next = ap->next;
138 ap->next->prev = ap->prev;
139 else if (iface->ap_list)
140 iface->ap_list->prev = ap->prev;
144 static void ap_ap_iter_list_add(struct hostapd_iface *iface,
147 if (iface->ap_iter_list) {
148 ap->iter_prev = iface->ap_iter_list->iter_prev;
149 iface->ap_iter_list->iter_prev = ap;
152 ap->iter_next = iface->ap_iter_list;
153 iface->ap_iter_list = ap;
157 static void ap_ap_iter_list_del(struct hostapd_iface *iface,
160 if (iface->ap_iter_list == ap)
161 iface->ap_iter_list = ap->iter_next;
163 ap->iter_prev->iter_next = ap->iter_next;
166 ap->iter_next->iter_prev = ap->iter_prev;
167 else if (iface->ap_iter_list)
168 iface->ap_iter_list->iter_prev = ap->iter_prev;
172 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
174 ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
175 iface->ap_hash[STA_HASH(ap->addr)] = ap;
179 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
183 s = iface->ap_hash[STA_HASH(ap->addr)];
184 if (s == NULL) return;
185 if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
186 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
190 while (s->hnext != NULL &&
191 os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
193 if (s->hnext != NULL)
194 s->hnext = s->hnext->hnext;
196 printf("AP: could not remove AP " MACSTR " from hash table\n",
201 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
203 ap_ap_hash_del(iface, ap);
204 ap_ap_list_del(iface, ap);
205 ap_ap_iter_list_del(iface, ap);
212 static void hostapd_free_aps(struct hostapd_iface *iface)
214 struct ap_info *ap, *prev;
221 ap_free_ap(iface, prev);
224 iface->ap_list = NULL;
228 int ap_ap_for_each(struct hostapd_iface *iface,
229 int (*func)(struct ap_info *s, void *data), void *data)
247 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
251 ap = os_zalloc(sizeof(struct ap_info));
255 /* initialize AP info data */
256 os_memcpy(ap->addr, addr, ETH_ALEN);
257 ap_ap_list_add(iface, ap);
259 ap_ap_hash_add(iface, ap);
260 ap_ap_iter_list_add(iface, ap);
262 if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
263 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
264 MACSTR " from AP table", MAC2STR(ap->prev->addr));
265 ap_free_ap(iface, ap->prev);
272 void ap_list_process_beacon(struct hostapd_iface *iface,
273 struct ieee80211_mgmt *mgmt,
274 struct ieee802_11_elems *elems,
275 struct hostapd_frame_info *fi)
282 if (iface->conf->ap_table_max_size < 1)
285 ap = ap_get_ap(iface, mgmt->bssid);
287 ap = ap_ap_add(iface, mgmt->bssid);
289 printf("Failed to allocate AP information entry\n");
295 ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
296 ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
299 len = elems->ssid_len;
300 if (len >= sizeof(ap->ssid))
301 len = sizeof(ap->ssid) - 1;
302 os_memcpy(ap->ssid, elems->ssid, len);
303 ap->ssid[len] = '\0';
307 os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
309 if (elems->supp_rates) {
310 len = elems->supp_rates_len;
311 if (len > WLAN_SUPP_RATES_MAX)
312 len = WLAN_SUPP_RATES_MAX;
313 os_memcpy(ap->supported_rates, elems->supp_rates, len);
315 if (elems->ext_supp_rates) {
317 if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
318 len2 = WLAN_SUPP_RATES_MAX - len;
320 len2 = elems->ext_supp_rates_len;
321 os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
325 ap->wpa = elems->wpa_ie != NULL;
327 if (elems->erp_info && elems->erp_info_len == 1)
328 ap->erp = elems->erp_info[0];
332 if (elems->ds_params && elems->ds_params_len == 1)
333 ap->channel = elems->ds_params[0];
335 ap->channel = fi->channel;
337 if (elems->ht_capabilities)
343 time(&ap->last_beacon);
345 ap->phytype = fi->phytype;
346 ap->ssi_signal = fi->ssi_signal;
347 ap->datarate = fi->datarate;
350 if (!new_ap && ap != iface->ap_list) {
351 /* move AP entry into the beginning of the list so that the
352 * oldest entry is always in the end of the list */
353 ap_ap_list_del(iface, ap);
354 ap_ap_list_add(iface, ap);
358 ap_list_beacon_olbc(iface, ap)) {
360 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
361 "protection", MAC2STR(ap->addr));
365 #ifdef CONFIG_IEEE80211N
366 if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
368 hostapd_ht_operation_update(iface);
369 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
370 " - enable protection", MAC2STR(ap->addr));
373 #endif /* CONFIG_IEEE80211N */
376 ieee802_11_set_beacons(iface);
380 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
382 struct hostapd_iface *iface = eloop_ctx;
387 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
394 while (iface->ap_list) {
395 ap = iface->ap_list->prev;
396 if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
400 ap_free_ap(iface, ap);
403 if (iface->olbc || iface->olbc_ht) {
408 while (ap && (olbc == 0 || olbc_ht == 0)) {
409 if (ap_list_beacon_olbc(iface, ap))
411 #ifdef CONFIG_IEEE80211N
412 if (ap_list_beacon_olbc_ht(iface, ap))
414 #endif /* CONFIG_IEEE80211N */
417 if (!olbc && iface->olbc) {
418 wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
422 #ifdef CONFIG_IEEE80211N
423 if (!olbc_ht && iface->olbc_ht) {
424 wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
426 hostapd_ht_operation_update(iface);
429 #endif /* CONFIG_IEEE80211N */
433 ieee802_11_set_beacons(iface);
437 int ap_list_init(struct hostapd_iface *iface)
439 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
444 void ap_list_deinit(struct hostapd_iface *iface)
446 eloop_cancel_timeout(ap_list_timer, iface, NULL);
447 hostapd_free_aps(iface);