3 * Copyright 2002-2003, Jouni Malinen <j@w1.fi>
4 * Copyright 2003-2004, Instant802 Networks, Inc.
5 * Copyright 2006, Devicescape Software, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
20 #include "ieee802_11.h"
23 #include "hw_features.h"
27 struct ieee80211_frame_info {
43 /* Note: this structure is otherwise identical to capture format used
44 * in linux-wlan-ng, but this additional field is used to provide meta
45 * data about the frame to hostapd. This was the easiest method for
46 * providing this information, but this might change in the future. */
48 } __attribute__ ((packed));
51 enum ieee80211_phytype {
52 ieee80211_phytype_fhss_dot11_97 = 1,
53 ieee80211_phytype_dsss_dot11_97 = 2,
54 ieee80211_phytype_irbaseband = 3,
55 ieee80211_phytype_dsss_dot11_b = 4,
56 ieee80211_phytype_pbcc_dot11_b = 5,
57 ieee80211_phytype_ofdm_dot11_g = 6,
58 ieee80211_phytype_pbcc_dot11_g = 7,
59 ieee80211_phytype_ofdm_dot11_a = 8,
60 ieee80211_phytype_dsss_dot11_turbog = 255,
61 ieee80211_phytype_dsss_dot11_turbo = 256,
65 /* AP list is a double linked list with head->prev pointing to the end of the
66 * list and tail->next = NULL. Entries are moved to the head of the list
67 * whenever a beacon has been received from the AP in question. The tail entry
68 * in this link will thus be the least recently used entry. */
71 static void ap_list_new_ap(struct hostapd_iface *iface, struct ap_info *ap)
73 wpa_printf(MSG_DEBUG, "New AP detected: " MACSTR, MAC2STR(ap->addr));
75 /* TODO: could send a notification message to an external program that
76 * would then determine whether a rogue AP has been detected */
80 static void ap_list_expired_ap(struct hostapd_iface *iface, struct ap_info *ap)
82 wpa_printf(MSG_DEBUG, "AP info expired: " MACSTR, MAC2STR(ap->addr));
84 /* TODO: could send a notification message to an external program */
88 static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
92 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
93 ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
94 iface->conf->channel != ap->channel)
97 if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
100 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
101 int rate = (ap->supported_rates[i] & 0x7f) * 5;
102 if (rate == 60 || rate == 90 || rate > 110)
110 #ifdef CONFIG_IEEE80211N
111 static int ap_list_beacon_olbc_ht(struct hostapd_iface *iface,
114 return !ap->ht_support;
116 #endif /* CONFIG_IEEE80211N */
119 struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
123 s = iface->ap_hash[STA_HASH(ap)];
124 while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
130 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
132 if (iface->ap_list) {
133 ap->prev = iface->ap_list->prev;
134 iface->ap_list->prev = ap;
137 ap->next = iface->ap_list;
142 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
144 if (iface->ap_list == ap)
145 iface->ap_list = ap->next;
147 ap->prev->next = ap->next;
150 ap->next->prev = ap->prev;
151 else if (iface->ap_list)
152 iface->ap_list->prev = ap->prev;
156 static void ap_ap_iter_list_add(struct hostapd_iface *iface,
159 if (iface->ap_iter_list) {
160 ap->iter_prev = iface->ap_iter_list->iter_prev;
161 iface->ap_iter_list->iter_prev = ap;
164 ap->iter_next = iface->ap_iter_list;
165 iface->ap_iter_list = ap;
169 static void ap_ap_iter_list_del(struct hostapd_iface *iface,
172 if (iface->ap_iter_list == ap)
173 iface->ap_iter_list = ap->iter_next;
175 ap->iter_prev->iter_next = ap->iter_next;
178 ap->iter_next->iter_prev = ap->iter_prev;
179 else if (iface->ap_iter_list)
180 iface->ap_iter_list->iter_prev = ap->iter_prev;
184 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
186 ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
187 iface->ap_hash[STA_HASH(ap->addr)] = ap;
191 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
195 s = iface->ap_hash[STA_HASH(ap->addr)];
196 if (s == NULL) return;
197 if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
198 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
202 while (s->hnext != NULL &&
203 os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
205 if (s->hnext != NULL)
206 s->hnext = s->hnext->hnext;
208 printf("AP: could not remove AP " MACSTR " from hash table\n",
213 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
215 ap_ap_hash_del(iface, ap);
216 ap_ap_list_del(iface, ap);
217 ap_ap_iter_list_del(iface, ap);
224 static void hostapd_free_aps(struct hostapd_iface *iface)
226 struct ap_info *ap, *prev;
233 ap_free_ap(iface, prev);
236 iface->ap_list = NULL;
240 int ap_ap_for_each(struct hostapd_iface *iface,
241 int (*func)(struct ap_info *s, void *data), void *data)
259 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
263 ap = os_zalloc(sizeof(struct ap_info));
267 /* initialize AP info data */
268 os_memcpy(ap->addr, addr, ETH_ALEN);
269 ap_ap_list_add(iface, ap);
271 ap_ap_hash_add(iface, ap);
272 ap_ap_iter_list_add(iface, ap);
274 if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
275 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
276 MACSTR " from AP table", MAC2STR(ap->prev->addr));
277 if (iface->conf->passive_scan_interval > 0)
278 ap_list_expired_ap(iface, ap->prev);
279 ap_free_ap(iface, ap->prev);
286 void ap_list_process_beacon(struct hostapd_iface *iface,
287 struct ieee80211_mgmt *mgmt,
288 struct ieee802_11_elems *elems,
289 struct hostapd_frame_info *fi)
296 if (iface->conf->ap_table_max_size < 1)
299 ap = ap_get_ap(iface, mgmt->bssid);
301 ap = ap_ap_add(iface, mgmt->bssid);
303 printf("Failed to allocate AP information entry\n");
309 ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
310 ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
313 len = elems->ssid_len;
314 if (len >= sizeof(ap->ssid))
315 len = sizeof(ap->ssid) - 1;
316 os_memcpy(ap->ssid, elems->ssid, len);
317 ap->ssid[len] = '\0';
321 os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
323 if (elems->supp_rates) {
324 len = elems->supp_rates_len;
325 if (len > WLAN_SUPP_RATES_MAX)
326 len = WLAN_SUPP_RATES_MAX;
327 os_memcpy(ap->supported_rates, elems->supp_rates, len);
329 if (elems->ext_supp_rates) {
331 if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
332 len2 = WLAN_SUPP_RATES_MAX - len;
334 len2 = elems->ext_supp_rates_len;
335 os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
339 ap->wpa = elems->wpa_ie != NULL;
341 if (elems->erp_info && elems->erp_info_len == 1)
342 ap->erp = elems->erp_info[0];
346 if (elems->ds_params && elems->ds_params_len == 1)
347 ap->channel = elems->ds_params[0];
349 ap->channel = fi->channel;
351 if (elems->ht_capabilities)
357 time(&ap->last_beacon);
359 ap->phytype = fi->phytype;
360 ap->ssi_signal = fi->ssi_signal;
361 ap->datarate = fi->datarate;
365 if (iface->conf->passive_scan_interval > 0)
366 ap_list_new_ap(iface, ap);
367 } else if (ap != iface->ap_list) {
368 /* move AP entry into the beginning of the list so that the
369 * oldest entry is always in the end of the list */
370 ap_ap_list_del(iface, ap);
371 ap_ap_list_add(iface, ap);
375 ap_list_beacon_olbc(iface, ap)) {
377 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
378 "protection", MAC2STR(ap->addr));
382 #ifdef CONFIG_IEEE80211N
383 if (!iface->olbc_ht && ap_list_beacon_olbc_ht(iface, ap)) {
385 hostapd_ht_operation_update(iface);
386 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
387 " - enable protection", MAC2STR(ap->addr));
390 #endif /* CONFIG_IEEE80211N */
393 ieee802_11_set_beacons(iface);
397 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
399 struct hostapd_iface *iface = eloop_ctx;
404 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
411 /* FIX: it looks like jkm-Purina ended up in busy loop in this
412 * function. Apparently, something can still cause a loop in the AP
415 while (iface->ap_list) {
416 ap = iface->ap_list->prev;
417 if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
421 if (iface->conf->passive_scan_interval > 0)
422 ap_list_expired_ap(iface, ap);
423 ap_free_ap(iface, ap);
426 if (iface->olbc || iface->olbc_ht) {
431 while (ap && (olbc == 0 || olbc_ht == 0)) {
432 if (ap_list_beacon_olbc(iface, ap))
434 #ifdef CONFIG_IEEE80211N
435 if (ap_list_beacon_olbc_ht(iface, ap))
437 #endif /* CONFIG_IEEE80211N */
440 if (!olbc && iface->olbc) {
441 wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
445 #ifdef CONFIG_IEEE80211N
446 if (!olbc_ht && iface->olbc_ht) {
447 wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
449 hostapd_ht_operation_update(iface);
452 #endif /* CONFIG_IEEE80211N */
456 ieee802_11_set_beacons(iface);
460 int ap_list_init(struct hostapd_iface *iface)
462 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
467 void ap_list_deinit(struct hostapd_iface *iface)
469 eloop_cancel_timeout(ap_list_timer, iface, NULL);
470 hostapd_free_aps(iface);
474 int ap_list_reconfig(struct hostapd_iface *iface,
475 struct hostapd_config *oldconf)
480 if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
481 iface->conf->ap_table_expiration_time ==
482 oldconf->ap_table_expiration_time)
487 while (iface->ap_list) {
488 ap = iface->ap_list->prev;
489 if (iface->num_ap <= iface->conf->ap_table_max_size &&
490 ap->last_beacon + iface->conf->ap_table_expiration_time >=
494 if (iface->conf->passive_scan_interval > 0)
495 ap_list_expired_ap(iface, iface->ap_list->prev);
496 ap_free_ap(iface, iface->ap_list->prev);