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.
22 #include "ieee802_11.h"
26 #include "hw_features.h"
28 #include "drivers/driver.h"
31 struct ieee80211_frame_info {
47 /* Note: this structure is otherwise identical to capture format used
48 * in linux-wlan-ng, but this additional field is used to provide meta
49 * data about the frame to hostapd. This was the easiest method for
50 * providing this information, but this might change in the future. */
52 } __attribute__ ((packed));
55 enum ieee80211_phytype {
56 ieee80211_phytype_fhss_dot11_97 = 1,
57 ieee80211_phytype_dsss_dot11_97 = 2,
58 ieee80211_phytype_irbaseband = 3,
59 ieee80211_phytype_dsss_dot11_b = 4,
60 ieee80211_phytype_pbcc_dot11_b = 5,
61 ieee80211_phytype_ofdm_dot11_g = 6,
62 ieee80211_phytype_pbcc_dot11_g = 7,
63 ieee80211_phytype_ofdm_dot11_a = 8,
64 ieee80211_phytype_dsss_dot11_turbog = 255,
65 ieee80211_phytype_dsss_dot11_turbo = 256,
69 /* AP list is a double linked list with head->prev pointing to the end of the
70 * list and tail->next = NULL. Entries are moved to the head of the list
71 * whenever a beacon has been received from the AP in question. The tail entry
72 * in this link will thus be the least recently used entry. */
75 static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
79 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
80 ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
81 iface->conf->channel != ap->channel)
84 if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
87 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
88 int rate = (ap->supported_rates[i] & 0x7f) * 5;
89 if (rate == 60 || rate == 90 || rate > 110)
97 #ifdef CONFIG_IEEE80211N
98 static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
101 return !ap->ht_support;
103 #endif /* CONFIG_IEEE80211N */
106 struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
110 s = iface->ap_hash[STA_HASH(ap)];
111 while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
117 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
119 if (iface->ap_list) {
120 ap->prev = iface->ap_list->prev;
121 iface->ap_list->prev = ap;
124 ap->next = iface->ap_list;
129 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
131 if (iface->ap_list == ap)
132 iface->ap_list = ap->next;
134 ap->prev->next = ap->next;
137 ap->next->prev = ap->prev;
138 else if (iface->ap_list)
139 iface->ap_list->prev = ap->prev;
143 static void ap_ap_iter_list_add(struct hostapd_iface *iface,
146 if (iface->ap_iter_list) {
147 ap->iter_prev = iface->ap_iter_list->iter_prev;
148 iface->ap_iter_list->iter_prev = ap;
151 ap->iter_next = iface->ap_iter_list;
152 iface->ap_iter_list = ap;
156 static void ap_ap_iter_list_del(struct hostapd_iface *iface,
159 if (iface->ap_iter_list == ap)
160 iface->ap_iter_list = ap->iter_next;
162 ap->iter_prev->iter_next = ap->iter_next;
165 ap->iter_next->iter_prev = ap->iter_prev;
166 else if (iface->ap_iter_list)
167 iface->ap_iter_list->iter_prev = ap->iter_prev;
171 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
173 ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
174 iface->ap_hash[STA_HASH(ap->addr)] = ap;
178 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
182 s = iface->ap_hash[STA_HASH(ap->addr)];
183 if (s == NULL) return;
184 if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
185 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
189 while (s->hnext != NULL &&
190 os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
192 if (s->hnext != NULL)
193 s->hnext = s->hnext->hnext;
195 printf("AP: could not remove AP " MACSTR " from hash table\n",
200 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
202 ap_ap_hash_del(iface, ap);
203 ap_ap_list_del(iface, ap);
204 ap_ap_iter_list_del(iface, ap);
211 static void hostapd_free_aps(struct hostapd_iface *iface)
213 struct ap_info *ap, *prev;
220 ap_free_ap(iface, prev);
223 iface->ap_list = NULL;
227 int ap_ap_for_each(struct hostapd_iface *iface,
228 int (*func)(struct ap_info *s, void *data), void *data)
246 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
250 ap = os_zalloc(sizeof(struct ap_info));
254 /* initialize AP info data */
255 os_memcpy(ap->addr, addr, ETH_ALEN);
256 ap_ap_list_add(iface, ap);
258 ap_ap_hash_add(iface, ap);
259 ap_ap_iter_list_add(iface, ap);
261 if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
262 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
263 MACSTR " from AP table", MAC2STR(ap->prev->addr));
264 ap_free_ap(iface, ap->prev);
271 void ap_list_process_beacon(struct hostapd_iface *iface,
272 struct ieee80211_mgmt *mgmt,
273 struct ieee802_11_elems *elems,
274 struct hostapd_frame_info *fi)
281 if (iface->conf->ap_table_max_size < 1)
284 ap = ap_get_ap(iface, mgmt->bssid);
286 ap = ap_ap_add(iface, mgmt->bssid);
288 printf("Failed to allocate AP information entry\n");
294 ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
295 ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
298 len = elems->ssid_len;
299 if (len >= sizeof(ap->ssid))
300 len = sizeof(ap->ssid) - 1;
301 os_memcpy(ap->ssid, elems->ssid, len);
302 ap->ssid[len] = '\0';
306 os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
308 if (elems->supp_rates) {
309 len = elems->supp_rates_len;
310 if (len > WLAN_SUPP_RATES_MAX)
311 len = WLAN_SUPP_RATES_MAX;
312 os_memcpy(ap->supported_rates, elems->supp_rates, len);
314 if (elems->ext_supp_rates) {
316 if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
317 len2 = WLAN_SUPP_RATES_MAX - len;
319 len2 = elems->ext_supp_rates_len;
320 os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
324 ap->wpa = elems->wpa_ie != NULL;
326 if (elems->erp_info && elems->erp_info_len == 1)
327 ap->erp = elems->erp_info[0];
331 if (elems->ds_params && elems->ds_params_len == 1)
332 ap->channel = elems->ds_params[0];
334 ap->channel = fi->channel;
336 if (elems->ht_capabilities)
342 time(&ap->last_beacon);
344 ap->phytype = fi->phytype;
345 ap->ssi_signal = fi->ssi_signal;
346 ap->datarate = fi->datarate;
349 if (!new_ap && ap != iface->ap_list) {
350 /* move AP entry into the beginning of the list so that the
351 * oldest entry is always in the end of the list */
352 ap_ap_list_del(iface, ap);
353 ap_ap_list_add(iface, ap);
357 ap_list_beacon_olbc(iface, ap)) {
359 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
360 "protection", MAC2STR(ap->addr));
364 #ifdef CONFIG_IEEE80211N
365 if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
367 hostapd_ht_operation_update(iface);
368 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
369 " - enable protection", MAC2STR(ap->addr));
372 #endif /* CONFIG_IEEE80211N */
375 ieee802_11_set_beacons(iface);
379 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
381 struct hostapd_iface *iface = eloop_ctx;
386 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
393 while (iface->ap_list) {
394 ap = iface->ap_list->prev;
395 if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
399 ap_free_ap(iface, ap);
402 if (iface->olbc || iface->olbc_ht) {
407 while (ap && (olbc == 0 || olbc_ht == 0)) {
408 if (ap_list_beacon_olbc(iface, ap))
410 #ifdef CONFIG_IEEE80211N
411 if (ap_list_beacon_olbc_ht(iface, ap))
413 #endif /* CONFIG_IEEE80211N */
416 if (!olbc && iface->olbc) {
417 wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
421 #ifdef CONFIG_IEEE80211N
422 if (!olbc_ht && iface->olbc_ht) {
423 wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
425 hostapd_ht_operation_update(iface);
428 #endif /* CONFIG_IEEE80211N */
432 ieee802_11_set_beacons(iface);
436 int ap_list_init(struct hostapd_iface *iface)
438 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
443 void ap_list_deinit(struct hostapd_iface *iface)
445 eloop_cancel_timeout(ap_list_timer, iface, NULL);
446 hostapd_free_aps(iface);
450 int ap_list_reconfig(struct hostapd_iface *iface,
451 struct hostapd_config *oldconf)
456 if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
457 iface->conf->ap_table_expiration_time ==
458 oldconf->ap_table_expiration_time)
463 while (iface->ap_list) {
464 ap = iface->ap_list->prev;
465 if (iface->num_ap <= iface->conf->ap_table_max_size &&
466 ap->last_beacon + iface->conf->ap_table_expiration_time >=
470 ap_free_ap(iface, iface->ap_list->prev);