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"
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 void ap_list_new_ap(struct hostapd_iface *iface, struct ap_info *ap)
77 wpa_printf(MSG_DEBUG, "New AP detected: " MACSTR, MAC2STR(ap->addr));
79 /* TODO: could send a notification message to an external program that
80 * would then determine whether a rogue AP has been detected */
84 static void ap_list_expired_ap(struct hostapd_iface *iface, struct ap_info *ap)
86 wpa_printf(MSG_DEBUG, "AP info expired: " MACSTR, MAC2STR(ap->addr));
88 /* TODO: could send a notification message to an external program */
92 static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
96 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
97 ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
98 iface->conf->channel != ap->channel)
101 if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
104 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
105 int rate = (ap->supported_rates[i] & 0x7f) * 5;
106 if (rate == 60 || rate == 90 || rate > 110)
114 #ifdef CONFIG_IEEE80211N
115 static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
118 return !ap->ht_support;
120 #endif /* CONFIG_IEEE80211N */
123 struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
127 s = iface->ap_hash[STA_HASH(ap)];
128 while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
134 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
136 if (iface->ap_list) {
137 ap->prev = iface->ap_list->prev;
138 iface->ap_list->prev = ap;
141 ap->next = iface->ap_list;
146 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
148 if (iface->ap_list == ap)
149 iface->ap_list = ap->next;
151 ap->prev->next = ap->next;
154 ap->next->prev = ap->prev;
155 else if (iface->ap_list)
156 iface->ap_list->prev = ap->prev;
160 static void ap_ap_iter_list_add(struct hostapd_iface *iface,
163 if (iface->ap_iter_list) {
164 ap->iter_prev = iface->ap_iter_list->iter_prev;
165 iface->ap_iter_list->iter_prev = ap;
168 ap->iter_next = iface->ap_iter_list;
169 iface->ap_iter_list = ap;
173 static void ap_ap_iter_list_del(struct hostapd_iface *iface,
176 if (iface->ap_iter_list == ap)
177 iface->ap_iter_list = ap->iter_next;
179 ap->iter_prev->iter_next = ap->iter_next;
182 ap->iter_next->iter_prev = ap->iter_prev;
183 else if (iface->ap_iter_list)
184 iface->ap_iter_list->iter_prev = ap->iter_prev;
188 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
190 ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
191 iface->ap_hash[STA_HASH(ap->addr)] = ap;
195 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
199 s = iface->ap_hash[STA_HASH(ap->addr)];
200 if (s == NULL) return;
201 if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
202 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
206 while (s->hnext != NULL &&
207 os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
209 if (s->hnext != NULL)
210 s->hnext = s->hnext->hnext;
212 printf("AP: could not remove AP " MACSTR " from hash table\n",
217 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
219 ap_ap_hash_del(iface, ap);
220 ap_ap_list_del(iface, ap);
221 ap_ap_iter_list_del(iface, ap);
228 static void hostapd_free_aps(struct hostapd_iface *iface)
230 struct ap_info *ap, *prev;
237 ap_free_ap(iface, prev);
240 iface->ap_list = NULL;
244 int ap_ap_for_each(struct hostapd_iface *iface,
245 int (*func)(struct ap_info *s, void *data), void *data)
263 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
267 ap = os_zalloc(sizeof(struct ap_info));
271 /* initialize AP info data */
272 os_memcpy(ap->addr, addr, ETH_ALEN);
273 ap_ap_list_add(iface, ap);
275 ap_ap_hash_add(iface, ap);
276 ap_ap_iter_list_add(iface, ap);
278 if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
279 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
280 MACSTR " from AP table", MAC2STR(ap->prev->addr));
281 if (iface->conf->passive_scan_interval > 0)
282 ap_list_expired_ap(iface, ap->prev);
283 ap_free_ap(iface, ap->prev);
290 void ap_list_process_beacon(struct hostapd_iface *iface,
291 struct ieee80211_mgmt *mgmt,
292 struct ieee802_11_elems *elems,
293 struct hostapd_frame_info *fi)
300 if (iface->conf->ap_table_max_size < 1)
303 ap = ap_get_ap(iface, mgmt->bssid);
305 ap = ap_ap_add(iface, mgmt->bssid);
307 printf("Failed to allocate AP information entry\n");
313 ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
314 ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
317 len = elems->ssid_len;
318 if (len >= sizeof(ap->ssid))
319 len = sizeof(ap->ssid) - 1;
320 os_memcpy(ap->ssid, elems->ssid, len);
321 ap->ssid[len] = '\0';
325 os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
327 if (elems->supp_rates) {
328 len = elems->supp_rates_len;
329 if (len > WLAN_SUPP_RATES_MAX)
330 len = WLAN_SUPP_RATES_MAX;
331 os_memcpy(ap->supported_rates, elems->supp_rates, len);
333 if (elems->ext_supp_rates) {
335 if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
336 len2 = WLAN_SUPP_RATES_MAX - len;
338 len2 = elems->ext_supp_rates_len;
339 os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
343 ap->wpa = elems->wpa_ie != NULL;
345 if (elems->erp_info && elems->erp_info_len == 1)
346 ap->erp = elems->erp_info[0];
350 if (elems->ds_params && elems->ds_params_len == 1)
351 ap->channel = elems->ds_params[0];
353 ap->channel = fi->channel;
355 if (elems->ht_capabilities)
361 time(&ap->last_beacon);
363 ap->phytype = fi->phytype;
364 ap->ssi_signal = fi->ssi_signal;
365 ap->datarate = fi->datarate;
369 if (iface->conf->passive_scan_interval > 0)
370 ap_list_new_ap(iface, ap);
371 } else if (ap != iface->ap_list) {
372 /* move AP entry into the beginning of the list so that the
373 * oldest entry is always in the end of the list */
374 ap_ap_list_del(iface, ap);
375 ap_ap_list_add(iface, ap);
379 ap_list_beacon_olbc(iface, ap)) {
381 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
382 "protection", MAC2STR(ap->addr));
386 #ifdef CONFIG_IEEE80211N
387 if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
389 hostapd_ht_operation_update(iface);
390 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
391 " - enable protection", MAC2STR(ap->addr));
394 #endif /* CONFIG_IEEE80211N */
397 ieee802_11_set_beacons(iface);
401 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
403 struct hostapd_iface *iface = eloop_ctx;
408 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
415 /* FIX: it looks like jkm-Purina ended up in busy loop in this
416 * function. Apparently, something can still cause a loop in the AP
419 while (iface->ap_list) {
420 ap = iface->ap_list->prev;
421 if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
425 if (iface->conf->passive_scan_interval > 0)
426 ap_list_expired_ap(iface, ap);
427 ap_free_ap(iface, ap);
430 if (iface->olbc || iface->olbc_ht) {
435 while (ap && (olbc == 0 || olbc_ht == 0)) {
436 if (ap_list_beacon_olbc(iface, ap))
438 #ifdef CONFIG_IEEE80211N
439 if (ap_list_beacon_olbc_ht(iface, ap))
441 #endif /* CONFIG_IEEE80211N */
444 if (!olbc && iface->olbc) {
445 wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
449 #ifdef CONFIG_IEEE80211N
450 if (!olbc_ht && iface->olbc_ht) {
451 wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
453 hostapd_ht_operation_update(iface);
456 #endif /* CONFIG_IEEE80211N */
460 ieee802_11_set_beacons(iface);
464 int ap_list_init(struct hostapd_iface *iface)
466 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
471 void ap_list_deinit(struct hostapd_iface *iface)
473 eloop_cancel_timeout(ap_list_timer, iface, NULL);
474 hostapd_free_aps(iface);
478 int ap_list_reconfig(struct hostapd_iface *iface,
479 struct hostapd_config *oldconf)
484 if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
485 iface->conf->ap_table_expiration_time ==
486 oldconf->ap_table_expiration_time)
491 while (iface->ap_list) {
492 ap = iface->ap_list->prev;
493 if (iface->num_ap <= iface->conf->ap_table_max_size &&
494 ap->last_beacon + iface->conf->ap_table_expiration_time >=
498 if (iface->conf->passive_scan_interval > 0)
499 ap_list_expired_ap(iface, iface->ap_list->prev);
500 ap_free_ap(iface, iface->ap_list->prev);