1/*
2 * hostapd / AP table
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2006, Devicescape Software, Inc.
6 *
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
9 */
10
11#include "utils/includes.h"
12
13#include "utils/common.h"
14#include "utils/eloop.h"
15#include "common/ieee802_11_defs.h"
16#include "common/ieee802_11_common.h"
17#include "drivers/driver.h"
18#include "hostapd.h"
19#include "ap_config.h"
20#include "ieee802_11.h"
21#include "sta_info.h"
22#include "beacon.h"
23#include "ap_list.h"
24
25
26/* AP list is a double linked list with head->prev pointing to the end of the
27 * list and tail->next = NULL. Entries are moved to the head of the list
28 * whenever a beacon has been received from the AP in question. The tail entry
29 * in this link will thus be the least recently used entry. */
30
31
32static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
33{
34	int i;
35
36	if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
37	    iface->conf->channel != ap->channel)
38		return 0;
39
40	if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
41		return 1;
42
43	for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
44		int rate = (ap->supported_rates[i] & 0x7f) * 5;
45		if (rate == 60 || rate == 90 || rate > 110)
46			return 0;
47	}
48
49	return 1;
50}
51
52
53struct ap_info * ap_get_ap(struct hostapd_iface *iface, const u8 *ap)
54{
55	struct ap_info *s;
56
57	s = iface->ap_hash[STA_HASH(ap)];
58	while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
59		s = s->hnext;
60	return s;
61}
62
63
64static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
65{
66	if (iface->ap_list) {
67		ap->prev = iface->ap_list->prev;
68		iface->ap_list->prev = ap;
69	} else
70		ap->prev = ap;
71	ap->next = iface->ap_list;
72	iface->ap_list = ap;
73}
74
75
76static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
77{
78	if (iface->ap_list == ap)
79		iface->ap_list = ap->next;
80	else
81		ap->prev->next = ap->next;
82
83	if (ap->next)
84		ap->next->prev = ap->prev;
85	else if (iface->ap_list)
86		iface->ap_list->prev = ap->prev;
87}
88
89
90static void ap_ap_iter_list_add(struct hostapd_iface *iface,
91				struct ap_info *ap)
92{
93	if (iface->ap_iter_list) {
94		ap->iter_prev = iface->ap_iter_list->iter_prev;
95		iface->ap_iter_list->iter_prev = ap;
96	} else
97		ap->iter_prev = ap;
98	ap->iter_next = iface->ap_iter_list;
99	iface->ap_iter_list = ap;
100}
101
102
103static void ap_ap_iter_list_del(struct hostapd_iface *iface,
104				struct ap_info *ap)
105{
106	if (iface->ap_iter_list == ap)
107		iface->ap_iter_list = ap->iter_next;
108	else
109		ap->iter_prev->iter_next = ap->iter_next;
110
111	if (ap->iter_next)
112		ap->iter_next->iter_prev = ap->iter_prev;
113	else if (iface->ap_iter_list)
114		iface->ap_iter_list->iter_prev = ap->iter_prev;
115}
116
117
118static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
119{
120	ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
121	iface->ap_hash[STA_HASH(ap->addr)] = ap;
122}
123
124
125static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
126{
127	struct ap_info *s;
128
129	s = iface->ap_hash[STA_HASH(ap->addr)];
130	if (s == NULL) return;
131	if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
132		iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
133		return;
134	}
135
136	while (s->hnext != NULL &&
137	       os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
138		s = s->hnext;
139	if (s->hnext != NULL)
140		s->hnext = s->hnext->hnext;
141	else
142		printf("AP: could not remove AP " MACSTR " from hash table\n",
143		       MAC2STR(ap->addr));
144}
145
146
147static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
148{
149	ap_ap_hash_del(iface, ap);
150	ap_ap_list_del(iface, ap);
151	ap_ap_iter_list_del(iface, ap);
152
153	iface->num_ap--;
154	os_free(ap);
155}
156
157
158static void hostapd_free_aps(struct hostapd_iface *iface)
159{
160	struct ap_info *ap, *prev;
161
162	ap = iface->ap_list;
163
164	while (ap) {
165		prev = ap;
166		ap = ap->next;
167		ap_free_ap(iface, prev);
168	}
169
170	iface->ap_list = NULL;
171}
172
173
174int ap_ap_for_each(struct hostapd_iface *iface,
175		   int (*func)(struct ap_info *s, void *data), void *data)
176{
177	struct ap_info *s;
178	int ret = 0;
179
180	s = iface->ap_list;
181
182	while (s) {
183		ret = func(s, data);
184		if (ret)
185			break;
186		s = s->next;
187	}
188
189	return ret;
190}
191
192
193static struct ap_info * ap_ap_add(struct hostapd_iface *iface, const u8 *addr)
194{
195	struct ap_info *ap;
196
197	ap = os_zalloc(sizeof(struct ap_info));
198	if (ap == NULL)
199		return NULL;
200
201	/* initialize AP info data */
202	os_memcpy(ap->addr, addr, ETH_ALEN);
203	ap_ap_list_add(iface, ap);
204	iface->num_ap++;
205	ap_ap_hash_add(iface, ap);
206	ap_ap_iter_list_add(iface, ap);
207
208	if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
209		wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
210			   MACSTR " from AP table", MAC2STR(ap->prev->addr));
211		ap_free_ap(iface, ap->prev);
212	}
213
214	return ap;
215}
216
217
218void ap_list_process_beacon(struct hostapd_iface *iface,
219			    const struct ieee80211_mgmt *mgmt,
220			    struct ieee802_11_elems *elems,
221			    struct hostapd_frame_info *fi)
222{
223	struct ap_info *ap;
224	struct os_time now;
225	int new_ap = 0;
226	size_t len;
227	int set_beacon = 0;
228
229	if (iface->conf->ap_table_max_size < 1)
230		return;
231
232	ap = ap_get_ap(iface, mgmt->bssid);
233	if (!ap) {
234		ap = ap_ap_add(iface, mgmt->bssid);
235		if (!ap) {
236			printf("Failed to allocate AP information entry\n");
237			return;
238		}
239		new_ap = 1;
240	}
241
242	ap->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
243	ap->capability = le_to_host16(mgmt->u.beacon.capab_info);
244
245	if (elems->ssid) {
246		len = elems->ssid_len;
247		if (len >= sizeof(ap->ssid))
248			len = sizeof(ap->ssid) - 1;
249		os_memcpy(ap->ssid, elems->ssid, len);
250		ap->ssid[len] = '\0';
251		ap->ssid_len = len;
252	}
253
254	os_memset(ap->supported_rates, 0, WLAN_SUPP_RATES_MAX);
255	len = 0;
256	if (elems->supp_rates) {
257		len = elems->supp_rates_len;
258		if (len > WLAN_SUPP_RATES_MAX)
259			len = WLAN_SUPP_RATES_MAX;
260		os_memcpy(ap->supported_rates, elems->supp_rates, len);
261	}
262	if (elems->ext_supp_rates) {
263		int len2;
264		if (len + elems->ext_supp_rates_len > WLAN_SUPP_RATES_MAX)
265			len2 = WLAN_SUPP_RATES_MAX - len;
266		else
267			len2 = elems->ext_supp_rates_len;
268		os_memcpy(ap->supported_rates + len, elems->ext_supp_rates,
269			  len2);
270	}
271
272	ap->wpa = elems->wpa_ie != NULL;
273
274	if (elems->erp_info && elems->erp_info_len == 1)
275		ap->erp = elems->erp_info[0];
276	else
277		ap->erp = -1;
278
279	if (elems->ds_params && elems->ds_params_len == 1)
280		ap->channel = elems->ds_params[0];
281	else if (fi)
282		ap->channel = fi->channel;
283
284	if (elems->ht_capabilities)
285		ap->ht_support = 1;
286	else
287		ap->ht_support = 0;
288
289	ap->num_beacons++;
290	os_get_time(&now);
291	ap->last_beacon = now.sec;
292	if (fi) {
293		ap->ssi_signal = fi->ssi_signal;
294		ap->datarate = fi->datarate;
295	}
296
297	if (!new_ap && ap != iface->ap_list) {
298		/* move AP entry into the beginning of the list so that the
299		 * oldest entry is always in the end of the list */
300		ap_ap_list_del(iface, ap);
301		ap_ap_list_add(iface, ap);
302	}
303
304	if (!iface->olbc &&
305	    ap_list_beacon_olbc(iface, ap)) {
306		iface->olbc = 1;
307		wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR " - enable "
308			   "protection", MAC2STR(ap->addr));
309		set_beacon++;
310	}
311
312#ifdef CONFIG_IEEE80211N
313	if (!iface->olbc_ht && !ap->ht_support) {
314		iface->olbc_ht = 1;
315		hostapd_ht_operation_update(iface);
316		wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
317			   " - enable protection", MAC2STR(ap->addr));
318		set_beacon++;
319	}
320#endif /* CONFIG_IEEE80211N */
321
322	if (set_beacon)
323		ieee802_11_set_beacons(iface);
324}
325
326
327static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
328{
329	struct hostapd_iface *iface = eloop_ctx;
330	struct os_time now;
331	struct ap_info *ap;
332	int set_beacon = 0;
333
334	eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
335
336	if (!iface->ap_list)
337		return;
338
339	os_get_time(&now);
340
341	while (iface->ap_list) {
342		ap = iface->ap_list->prev;
343		if (ap->last_beacon + iface->conf->ap_table_expiration_time >=
344		    now.sec)
345			break;
346
347		ap_free_ap(iface, ap);
348	}
349
350	if (iface->olbc || iface->olbc_ht) {
351		int olbc = 0;
352		int olbc_ht = 0;
353
354		ap = iface->ap_list;
355		while (ap && (olbc == 0 || olbc_ht == 0)) {
356			if (ap_list_beacon_olbc(iface, ap))
357				olbc = 1;
358			if (!ap->ht_support)
359				olbc_ht = 1;
360			ap = ap->next;
361		}
362		if (!olbc && iface->olbc) {
363			wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
364			iface->olbc = 0;
365			set_beacon++;
366		}
367#ifdef CONFIG_IEEE80211N
368		if (!olbc_ht && iface->olbc_ht) {
369			wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
370			iface->olbc_ht = 0;
371			hostapd_ht_operation_update(iface);
372			set_beacon++;
373		}
374#endif /* CONFIG_IEEE80211N */
375	}
376
377	if (set_beacon)
378		ieee802_11_set_beacons(iface);
379}
380
381
382int ap_list_init(struct hostapd_iface *iface)
383{
384	eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
385	return 0;
386}
387
388
389void ap_list_deinit(struct hostapd_iface *iface)
390{
391	eloop_cancel_timeout(ap_list_timer, iface, NULL);
392	hostapd_free_aps(iface);
393}
394