libm: Import fdim{,f,l} functions from FreeBSD.
[dragonfly.git] / contrib / hostapd-0.5.8 / ap_list.c
blobf2d322125fc85703280b627ca4fdf5006ee0b568
1 /*
2 * hostapd / AP table
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
12 * license.
14 * See README and COPYING for more details.
17 #include "includes.h"
19 #include "hostapd.h"
20 #include "ieee802_11.h"
21 #include "eloop.h"
22 #include "ap_list.h"
23 #include "hw_features.h"
24 #include "beacon.h"
27 struct ieee80211_frame_info {
28 u32 version;
29 u32 length;
30 u64 mactime;
31 u64 hosttime;
32 u32 phytype;
33 u32 channel;
34 u32 datarate;
35 u32 antenna;
36 u32 priority;
37 u32 ssi_type;
38 u32 ssi_signal;
39 u32 ssi_noise;
40 u32 preamble;
41 u32 encoding;
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. */
47 u32 msg_type;
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)
90 int i;
92 if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
93 ap->phytype != ieee80211_phytype_pbcc_dot11_g ||
94 iface->conf->channel != ap->channel)
95 return 0;
97 if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
98 return 1;
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)
103 return 0;
106 return 1;
110 struct ap_info * ap_get_ap(struct hostapd_iface *iface, u8 *ap)
112 struct ap_info *s;
114 s = iface->ap_hash[STA_HASH(ap)];
115 while (s != NULL && memcmp(s->addr, ap, ETH_ALEN) != 0)
116 s = s->hnext;
117 return s;
121 static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
123 if (iface->ap_list) {
124 ap->prev = iface->ap_list->prev;
125 iface->ap_list->prev = ap;
126 } else
127 ap->prev = ap;
128 ap->next = iface->ap_list;
129 iface->ap_list = ap;
133 static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
135 if (iface->ap_list == ap)
136 iface->ap_list = ap->next;
137 else
138 ap->prev->next = ap->next;
140 if (ap->next)
141 ap->next->prev = ap->prev;
142 else if (iface->ap_list)
143 iface->ap_list->prev = ap->prev;
147 static void ap_ap_iter_list_add(struct hostapd_iface *iface,
148 struct ap_info *ap)
150 if (iface->ap_iter_list) {
151 ap->iter_prev = iface->ap_iter_list->iter_prev;
152 iface->ap_iter_list->iter_prev = ap;
153 } else
154 ap->iter_prev = ap;
155 ap->iter_next = iface->ap_iter_list;
156 iface->ap_iter_list = ap;
160 static void ap_ap_iter_list_del(struct hostapd_iface *iface,
161 struct ap_info *ap)
163 if (iface->ap_iter_list == ap)
164 iface->ap_iter_list = ap->iter_next;
165 else
166 ap->iter_prev->iter_next = ap->iter_next;
168 if (ap->iter_next)
169 ap->iter_next->iter_prev = ap->iter_prev;
170 else if (iface->ap_iter_list)
171 iface->ap_iter_list->iter_prev = ap->iter_prev;
175 static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
177 ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
178 iface->ap_hash[STA_HASH(ap->addr)] = ap;
182 static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
184 struct ap_info *s;
186 s = iface->ap_hash[STA_HASH(ap->addr)];
187 if (s == NULL) return;
188 if (memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
189 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
190 return;
193 while (s->hnext != NULL &&
194 memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
195 s = s->hnext;
196 if (s->hnext != NULL)
197 s->hnext = s->hnext->hnext;
198 else
199 printf("AP: could not remove AP " MACSTR " from hash table\n",
200 MAC2STR(ap->addr));
204 static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
206 ap_ap_hash_del(iface, ap);
207 ap_ap_list_del(iface, ap);
208 ap_ap_iter_list_del(iface, ap);
210 iface->num_ap--;
211 free(ap);
215 static void hostapd_free_aps(struct hostapd_iface *iface)
217 struct ap_info *ap, *prev;
219 ap = iface->ap_list;
221 while (ap) {
222 prev = ap;
223 ap = ap->next;
224 ap_free_ap(iface, prev);
227 iface->ap_list = NULL;
231 int ap_ap_for_each(struct hostapd_iface *iface,
232 int (*func)(struct ap_info *s, void *data), void *data)
234 struct ap_info *s;
235 int ret = 0;
237 s = iface->ap_list;
239 while (s) {
240 ret = func(s, data);
241 if (ret)
242 break;
243 s = s->next;
246 return ret;
250 static struct ap_info * ap_ap_add(struct hostapd_iface *iface, u8 *addr)
252 struct ap_info *ap;
254 ap = wpa_zalloc(sizeof(struct ap_info));
255 if (ap == NULL)
256 return NULL;
258 /* initialize AP info data */
259 memcpy(ap->addr, addr, ETH_ALEN);
260 ap_ap_list_add(iface, ap);
261 iface->num_ap++;
262 ap_ap_hash_add(iface, ap);
263 ap_ap_iter_list_add(iface, ap);
265 if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
266 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
267 MACSTR " from AP table", MAC2STR(ap->prev->addr));
268 if (iface->conf->passive_scan_interval > 0)
269 ap_list_expired_ap(iface, ap->prev);
270 ap_free_ap(iface, ap->prev);
273 return ap;
277 void ap_list_process_beacon(struct hostapd_iface *iface,
278 struct ieee80211_mgmt *mgmt,
279 struct ieee802_11_elems *elems,
280 struct hostapd_frame_info *fi)
282 struct ap_info *ap;
283 int new_ap = 0;
284 size_t len;
286 if (iface->conf->ap_table_max_size < 1)
287 return;
289 ap = ap_get_ap(iface, mgmt->bssid);
290 if (!ap) {
291 ap = ap_ap_add(iface, mgmt->bssid);
292 if (!ap) {
293 printf("Failed to allocate AP information entry\n");
294 return;
296 new_ap = 1;
299 ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
300 ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
302 if (elems->ssid) {
303 len = elems->ssid_len;
304 if (len >= sizeof(ap->ssid))
305 len = sizeof(ap->ssid) - 1;
306 memcpy(ap->ssid, elems->ssid, len);
307 ap->ssid[len] = '\0';
308 ap->ssid_len = len;
311 memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
312 len = 0;
313 if (elems->supp_rates) {
314 len = elems->supp_rates_len;
315 if (len > WLAN_SUPP_RATES_MAX)
316 len = WLAN_SUPP_RATES_MAX;
317 memcpy(ap->supported_rates, elems->supp_rates, len);
319 if (elems->ext_supp_rates) {
320 int len2;
321 if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
322 len2 = WLAN_SUPP_RATES_MAX - len;
323 else
324 len2 = elems->ext_supp_rates_len;
325 memcpy(ap->supported_rates + len, elems->ext_supp_rates, len2);
328 ap->wpa = elems->wpa_ie != NULL;
330 if (elems->erp_info && elems->erp_info_len == 1)
331 ap->erp = elems->erp_info[0];
332 else
333 ap->erp = -1;
335 if (elems->ds_params && elems->ds_params_len == 1)
336 ap->channel = elems->ds_params[0];
337 else if (fi)
338 ap->channel = fi->channel;
340 ap->num_beacons++;
341 time(&ap->last_beacon);
342 if (fi) {
343 ap->phytype = fi->phytype;
344 ap->ssi_signal = fi->ssi_signal;
345 ap->datarate = fi->datarate;
348 if (new_ap) {
349 if (iface->conf->passive_scan_interval > 0)
350 ap_list_new_ap(iface, ap);
351 } else if (ap != iface->ap_list) {
352 /* move AP entry into the beginning of the list so that the
353 * oldest entry is always in the end of the list */
354 ap_ap_list_del(iface, ap);
355 ap_ap_list_add(iface, ap);
358 if (!iface->olbc &&
359 ap_list_beacon_olbc(iface, ap)) {
360 struct hostapd_data *hapd = iface->bss[0];
361 iface->olbc = 1;
362 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
363 "OLBC AP detected: " MACSTR " - enable "
364 "protection\n", MAC2STR(ap->addr));
365 ieee802_11_set_beacons(hapd->iface);
370 static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
372 struct hostapd_iface *iface = eloop_ctx;
373 time_t now;
374 struct ap_info *ap;
376 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
378 if (!iface->ap_list)
379 return;
381 time(&now);
383 /* FIX: it looks like jkm-Purina ended up in busy loop in this
384 * function. Apparently, something can still cause a loop in the AP
385 * list.. */
387 while (iface->ap_list) {
388 ap = iface->ap_list->prev;
389 if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
390 now)
391 break;
393 if (iface->conf->passive_scan_interval > 0)
394 ap_list_expired_ap(iface, ap);
395 ap_free_ap(iface, ap);
398 if (iface->olbc) {
399 int olbc = 0;
400 ap = iface->ap_list;
401 while (ap) {
402 if (ap_list_beacon_olbc(iface, ap)) {
403 olbc = 1;
404 break;
406 ap = ap->next;
408 if (!olbc) {
409 struct hostapd_data *hapd = iface->bss[0];
410 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
411 "OLBC not detected anymore\n");
412 iface->olbc = 0;
413 ieee802_11_set_beacons(hapd->iface);
419 int ap_list_init(struct hostapd_iface *iface)
421 eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
422 return 0;
426 void ap_list_deinit(struct hostapd_iface *iface)
428 eloop_cancel_timeout(ap_list_timer, iface, NULL);
429 hostapd_free_aps(iface);
433 int ap_list_reconfig(struct hostapd_iface *iface,
434 struct hostapd_config *oldconf)
436 time_t now;
437 struct ap_info *ap;
439 if (iface->conf->ap_table_max_size == oldconf->ap_table_max_size &&
440 iface->conf->ap_table_expiration_time ==
441 oldconf->ap_table_expiration_time)
442 return 0;
444 time(&now);
446 while (iface->ap_list) {
447 ap = iface->ap_list->prev;
448 if (iface->num_ap <= iface->conf->ap_table_max_size &&
449 ap->last_beacon + iface->conf->ap_table_expiration_time >=
450 now)
451 break;
453 if (iface->conf->passive_scan_interval > 0)
454 ap_list_expired_ap(iface, iface->ap_list->prev);
455 ap_free_ap(iface, iface->ap_list->prev);
458 return 0;