xref: /freebsd/contrib/wpa/src/ap/ap_list.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * hostapd / AP table
3e28a4053SRui Paulo  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo  * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5e28a4053SRui Paulo  * Copyright (c) 2006, Devicescape Software, Inc.
6e28a4053SRui Paulo  *
7f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
8f05cddf9SRui Paulo  * See README for more details.
9e28a4053SRui Paulo  */
10e28a4053SRui Paulo 
11e28a4053SRui Paulo #include "utils/includes.h"
12e28a4053SRui Paulo 
13e28a4053SRui Paulo #include "utils/common.h"
14e28a4053SRui Paulo #include "utils/eloop.h"
15e28a4053SRui Paulo #include "common/ieee802_11_defs.h"
16e28a4053SRui Paulo #include "common/ieee802_11_common.h"
17e28a4053SRui Paulo #include "hostapd.h"
18e28a4053SRui Paulo #include "ap_config.h"
19e28a4053SRui Paulo #include "ieee802_11.h"
20e28a4053SRui Paulo #include "sta_info.h"
21e28a4053SRui Paulo #include "beacon.h"
22e28a4053SRui Paulo #include "ap_list.h"
23e28a4053SRui Paulo 
24e28a4053SRui Paulo 
25e28a4053SRui Paulo /* AP list is a double linked list with head->prev pointing to the end of the
26e28a4053SRui Paulo  * list and tail->next = NULL. Entries are moved to the head of the list
27e28a4053SRui Paulo  * whenever a beacon has been received from the AP in question. The tail entry
28e28a4053SRui Paulo  * in this link will thus be the least recently used entry. */
29e28a4053SRui Paulo 
30e28a4053SRui Paulo 
ap_list_beacon_olbc(struct hostapd_iface * iface,struct ap_info * ap)31e28a4053SRui Paulo static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
32e28a4053SRui Paulo {
33e28a4053SRui Paulo 	int i;
34e28a4053SRui Paulo 
355b9c547cSRui Paulo 	if (iface->current_mode == NULL ||
365b9c547cSRui Paulo 	    iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
37e28a4053SRui Paulo 	    iface->conf->channel != ap->channel)
38e28a4053SRui Paulo 		return 0;
39e28a4053SRui Paulo 
40e28a4053SRui Paulo 	if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
41e28a4053SRui Paulo 		return 1;
42e28a4053SRui Paulo 
43e28a4053SRui Paulo 	for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
44e28a4053SRui Paulo 		int rate = (ap->supported_rates[i] & 0x7f) * 5;
45e28a4053SRui Paulo 		if (rate == 60 || rate == 90 || rate > 110)
46e28a4053SRui Paulo 			return 0;
47e28a4053SRui Paulo 	}
48e28a4053SRui Paulo 
49e28a4053SRui Paulo 	return 1;
50e28a4053SRui Paulo }
51e28a4053SRui Paulo 
52e28a4053SRui Paulo 
ap_get_ap(struct hostapd_iface * iface,const u8 * ap)535b9c547cSRui Paulo static struct ap_info * ap_get_ap(struct hostapd_iface *iface, const u8 *ap)
54e28a4053SRui Paulo {
55e28a4053SRui Paulo 	struct ap_info *s;
56e28a4053SRui Paulo 
57e28a4053SRui Paulo 	s = iface->ap_hash[STA_HASH(ap)];
58*a90b9d01SCy Schubert 	while (s != NULL && !ether_addr_equal(s->addr, ap))
59e28a4053SRui Paulo 		s = s->hnext;
60e28a4053SRui Paulo 	return s;
61e28a4053SRui Paulo }
62e28a4053SRui Paulo 
63e28a4053SRui Paulo 
ap_ap_list_add(struct hostapd_iface * iface,struct ap_info * ap)64e28a4053SRui Paulo static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
65e28a4053SRui Paulo {
66e28a4053SRui Paulo 	if (iface->ap_list) {
67e28a4053SRui Paulo 		ap->prev = iface->ap_list->prev;
68e28a4053SRui Paulo 		iface->ap_list->prev = ap;
69e28a4053SRui Paulo 	} else
70e28a4053SRui Paulo 		ap->prev = ap;
71e28a4053SRui Paulo 	ap->next = iface->ap_list;
72e28a4053SRui Paulo 	iface->ap_list = ap;
73e28a4053SRui Paulo }
74e28a4053SRui Paulo 
75e28a4053SRui Paulo 
ap_ap_list_del(struct hostapd_iface * iface,struct ap_info * ap)76e28a4053SRui Paulo static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
77e28a4053SRui Paulo {
78e28a4053SRui Paulo 	if (iface->ap_list == ap)
79e28a4053SRui Paulo 		iface->ap_list = ap->next;
80e28a4053SRui Paulo 	else
81e28a4053SRui Paulo 		ap->prev->next = ap->next;
82e28a4053SRui Paulo 
83e28a4053SRui Paulo 	if (ap->next)
84e28a4053SRui Paulo 		ap->next->prev = ap->prev;
85e28a4053SRui Paulo 	else if (iface->ap_list)
86e28a4053SRui Paulo 		iface->ap_list->prev = ap->prev;
87e28a4053SRui Paulo }
88e28a4053SRui Paulo 
89e28a4053SRui Paulo 
ap_ap_hash_add(struct hostapd_iface * iface,struct ap_info * ap)90e28a4053SRui Paulo static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
91e28a4053SRui Paulo {
92e28a4053SRui Paulo 	ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
93e28a4053SRui Paulo 	iface->ap_hash[STA_HASH(ap->addr)] = ap;
94e28a4053SRui Paulo }
95e28a4053SRui Paulo 
96e28a4053SRui Paulo 
ap_ap_hash_del(struct hostapd_iface * iface,struct ap_info * ap)97e28a4053SRui Paulo static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
98e28a4053SRui Paulo {
99e28a4053SRui Paulo 	struct ap_info *s;
100e28a4053SRui Paulo 
101e28a4053SRui Paulo 	s = iface->ap_hash[STA_HASH(ap->addr)];
102e28a4053SRui Paulo 	if (s == NULL) return;
103*a90b9d01SCy Schubert 	if (ether_addr_equal(s->addr, ap->addr)) {
104e28a4053SRui Paulo 		iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
105e28a4053SRui Paulo 		return;
106e28a4053SRui Paulo 	}
107e28a4053SRui Paulo 
108e28a4053SRui Paulo 	while (s->hnext != NULL &&
109*a90b9d01SCy Schubert 	       !ether_addr_equal(s->hnext->addr, ap->addr))
110e28a4053SRui Paulo 		s = s->hnext;
111e28a4053SRui Paulo 	if (s->hnext != NULL)
112e28a4053SRui Paulo 		s->hnext = s->hnext->hnext;
113e28a4053SRui Paulo 	else
1145b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "AP: could not remove AP " MACSTR
1155b9c547cSRui Paulo 			   " from hash table",  MAC2STR(ap->addr));
116e28a4053SRui Paulo }
117e28a4053SRui Paulo 
118e28a4053SRui Paulo 
ap_free_ap(struct hostapd_iface * iface,struct ap_info * ap)119e28a4053SRui Paulo static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
120e28a4053SRui Paulo {
121e28a4053SRui Paulo 	ap_ap_hash_del(iface, ap);
122e28a4053SRui Paulo 	ap_ap_list_del(iface, ap);
123e28a4053SRui Paulo 
124e28a4053SRui Paulo 	iface->num_ap--;
125e28a4053SRui Paulo 	os_free(ap);
126e28a4053SRui Paulo }
127e28a4053SRui Paulo 
128e28a4053SRui Paulo 
hostapd_free_aps(struct hostapd_iface * iface)129e28a4053SRui Paulo static void hostapd_free_aps(struct hostapd_iface *iface)
130e28a4053SRui Paulo {
131e28a4053SRui Paulo 	struct ap_info *ap, *prev;
132e28a4053SRui Paulo 
133e28a4053SRui Paulo 	ap = iface->ap_list;
134e28a4053SRui Paulo 
135e28a4053SRui Paulo 	while (ap) {
136e28a4053SRui Paulo 		prev = ap;
137e28a4053SRui Paulo 		ap = ap->next;
138e28a4053SRui Paulo 		ap_free_ap(iface, prev);
139e28a4053SRui Paulo 	}
140e28a4053SRui Paulo 
141e28a4053SRui Paulo 	iface->ap_list = NULL;
142e28a4053SRui Paulo }
143e28a4053SRui Paulo 
144e28a4053SRui Paulo 
ap_ap_add(struct hostapd_iface * iface,const u8 * addr)145e28a4053SRui Paulo static struct ap_info * ap_ap_add(struct hostapd_iface *iface, const u8 *addr)
146e28a4053SRui Paulo {
147e28a4053SRui Paulo 	struct ap_info *ap;
148e28a4053SRui Paulo 
149e28a4053SRui Paulo 	ap = os_zalloc(sizeof(struct ap_info));
150e28a4053SRui Paulo 	if (ap == NULL)
151e28a4053SRui Paulo 		return NULL;
152e28a4053SRui Paulo 
153e28a4053SRui Paulo 	/* initialize AP info data */
154e28a4053SRui Paulo 	os_memcpy(ap->addr, addr, ETH_ALEN);
155e28a4053SRui Paulo 	ap_ap_list_add(iface, ap);
156e28a4053SRui Paulo 	iface->num_ap++;
157e28a4053SRui Paulo 	ap_ap_hash_add(iface, ap);
158e28a4053SRui Paulo 
159e28a4053SRui Paulo 	if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
160e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
161e28a4053SRui Paulo 			   MACSTR " from AP table", MAC2STR(ap->prev->addr));
162e28a4053SRui Paulo 		ap_free_ap(iface, ap->prev);
163e28a4053SRui Paulo 	}
164e28a4053SRui Paulo 
165e28a4053SRui Paulo 	return ap;
166e28a4053SRui Paulo }
167e28a4053SRui Paulo 
168e28a4053SRui Paulo 
ap_list_process_beacon(struct hostapd_iface * iface,const struct ieee80211_mgmt * mgmt,struct ieee802_11_elems * elems,struct hostapd_frame_info * fi)169e28a4053SRui Paulo void ap_list_process_beacon(struct hostapd_iface *iface,
170e28a4053SRui Paulo 			    const struct ieee80211_mgmt *mgmt,
171e28a4053SRui Paulo 			    struct ieee802_11_elems *elems,
172e28a4053SRui Paulo 			    struct hostapd_frame_info *fi)
173e28a4053SRui Paulo {
174e28a4053SRui Paulo 	struct ap_info *ap;
175e28a4053SRui Paulo 	int new_ap = 0;
176e28a4053SRui Paulo 	int set_beacon = 0;
177e28a4053SRui Paulo 
178e28a4053SRui Paulo 	if (iface->conf->ap_table_max_size < 1)
179e28a4053SRui Paulo 		return;
180e28a4053SRui Paulo 
181e28a4053SRui Paulo 	ap = ap_get_ap(iface, mgmt->bssid);
182e28a4053SRui Paulo 	if (!ap) {
183e28a4053SRui Paulo 		ap = ap_ap_add(iface, mgmt->bssid);
184e28a4053SRui Paulo 		if (!ap) {
1855b9c547cSRui Paulo 			wpa_printf(MSG_INFO,
1865b9c547cSRui Paulo 				   "Failed to allocate AP information entry");
187e28a4053SRui Paulo 			return;
188e28a4053SRui Paulo 		}
189e28a4053SRui Paulo 		new_ap = 1;
190e28a4053SRui Paulo 	}
191e28a4053SRui Paulo 
192f05cddf9SRui Paulo 	merge_byte_arrays(ap->supported_rates, WLAN_SUPP_RATES_MAX,
193f05cddf9SRui Paulo 			  elems->supp_rates, elems->supp_rates_len,
194f05cddf9SRui Paulo 			  elems->ext_supp_rates, elems->ext_supp_rates_len);
195e28a4053SRui Paulo 
196325151a3SRui Paulo 	if (elems->erp_info)
197e28a4053SRui Paulo 		ap->erp = elems->erp_info[0];
198e28a4053SRui Paulo 	else
199e28a4053SRui Paulo 		ap->erp = -1;
200e28a4053SRui Paulo 
201325151a3SRui Paulo 	if (elems->ds_params)
202e28a4053SRui Paulo 		ap->channel = elems->ds_params[0];
203325151a3SRui Paulo 	else if (elems->ht_operation)
2045b9c547cSRui Paulo 		ap->channel = elems->ht_operation[0];
205e28a4053SRui Paulo 	else if (fi)
206e28a4053SRui Paulo 		ap->channel = fi->channel;
207e28a4053SRui Paulo 
208e28a4053SRui Paulo 	if (elems->ht_capabilities)
209e28a4053SRui Paulo 		ap->ht_support = 1;
210e28a4053SRui Paulo 	else
211e28a4053SRui Paulo 		ap->ht_support = 0;
212e28a4053SRui Paulo 
2135b9c547cSRui Paulo 	os_get_reltime(&ap->last_beacon);
214e28a4053SRui Paulo 
215e28a4053SRui Paulo 	if (!new_ap && ap != iface->ap_list) {
216e28a4053SRui Paulo 		/* move AP entry into the beginning of the list so that the
217e28a4053SRui Paulo 		 * oldest entry is always in the end of the list */
218e28a4053SRui Paulo 		ap_ap_list_del(iface, ap);
219e28a4053SRui Paulo 		ap_ap_list_add(iface, ap);
220e28a4053SRui Paulo 	}
221e28a4053SRui Paulo 
222e28a4053SRui Paulo 	if (!iface->olbc &&
223e28a4053SRui Paulo 	    ap_list_beacon_olbc(iface, ap)) {
224e28a4053SRui Paulo 		iface->olbc = 1;
2255b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR
2265b9c547cSRui Paulo 			   " (channel %d) - enable protection",
2275b9c547cSRui Paulo 			   MAC2STR(ap->addr), ap->channel);
228e28a4053SRui Paulo 		set_beacon++;
229e28a4053SRui Paulo 	}
230e28a4053SRui Paulo 
2315b9c547cSRui Paulo 	if (!iface->olbc_ht && !ap->ht_support &&
2325b9c547cSRui Paulo 	    (ap->channel == 0 ||
2335b9c547cSRui Paulo 	     ap->channel == iface->conf->channel ||
2345b9c547cSRui Paulo 	     ap->channel == iface->conf->channel +
2355b9c547cSRui Paulo 	     iface->conf->secondary_channel * 4)) {
236e28a4053SRui Paulo 		iface->olbc_ht = 1;
237e28a4053SRui Paulo 		hostapd_ht_operation_update(iface);
238e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
2395b9c547cSRui Paulo 			   " (channel %d) - enable protection",
2405b9c547cSRui Paulo 			   MAC2STR(ap->addr), ap->channel);
241e28a4053SRui Paulo 		set_beacon++;
242e28a4053SRui Paulo 	}
243e28a4053SRui Paulo 
244e28a4053SRui Paulo 	if (set_beacon)
245f05cddf9SRui Paulo 		ieee802_11_update_beacons(iface);
246e28a4053SRui Paulo }
247e28a4053SRui Paulo 
248e28a4053SRui Paulo 
ap_list_timer(struct hostapd_iface * iface)249325151a3SRui Paulo void ap_list_timer(struct hostapd_iface *iface)
250e28a4053SRui Paulo {
2515b9c547cSRui Paulo 	struct os_reltime now;
252e28a4053SRui Paulo 	struct ap_info *ap;
253e28a4053SRui Paulo 	int set_beacon = 0;
254e28a4053SRui Paulo 
255e28a4053SRui Paulo 	if (!iface->ap_list)
256e28a4053SRui Paulo 		return;
257e28a4053SRui Paulo 
2585b9c547cSRui Paulo 	os_get_reltime(&now);
259e28a4053SRui Paulo 
260e28a4053SRui Paulo 	while (iface->ap_list) {
261e28a4053SRui Paulo 		ap = iface->ap_list->prev;
2625b9c547cSRui Paulo 		if (!os_reltime_expired(&now, &ap->last_beacon,
2635b9c547cSRui Paulo 					iface->conf->ap_table_expiration_time))
264e28a4053SRui Paulo 			break;
265e28a4053SRui Paulo 
266e28a4053SRui Paulo 		ap_free_ap(iface, ap);
267e28a4053SRui Paulo 	}
268e28a4053SRui Paulo 
269e28a4053SRui Paulo 	if (iface->olbc || iface->olbc_ht) {
270e28a4053SRui Paulo 		int olbc = 0;
271e28a4053SRui Paulo 		int olbc_ht = 0;
272e28a4053SRui Paulo 
273e28a4053SRui Paulo 		ap = iface->ap_list;
274e28a4053SRui Paulo 		while (ap && (olbc == 0 || olbc_ht == 0)) {
275e28a4053SRui Paulo 			if (ap_list_beacon_olbc(iface, ap))
276e28a4053SRui Paulo 				olbc = 1;
277e28a4053SRui Paulo 			if (!ap->ht_support)
278e28a4053SRui Paulo 				olbc_ht = 1;
279e28a4053SRui Paulo 			ap = ap->next;
280e28a4053SRui Paulo 		}
281e28a4053SRui Paulo 		if (!olbc && iface->olbc) {
282e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
283e28a4053SRui Paulo 			iface->olbc = 0;
284e28a4053SRui Paulo 			set_beacon++;
285e28a4053SRui Paulo 		}
286e28a4053SRui Paulo 		if (!olbc_ht && iface->olbc_ht) {
287e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
288e28a4053SRui Paulo 			iface->olbc_ht = 0;
289e28a4053SRui Paulo 			hostapd_ht_operation_update(iface);
290e28a4053SRui Paulo 			set_beacon++;
291e28a4053SRui Paulo 		}
292e28a4053SRui Paulo 	}
293e28a4053SRui Paulo 
294e28a4053SRui Paulo 	if (set_beacon)
295f05cddf9SRui Paulo 		ieee802_11_update_beacons(iface);
296e28a4053SRui Paulo }
297e28a4053SRui Paulo 
298e28a4053SRui Paulo 
ap_list_init(struct hostapd_iface * iface)299e28a4053SRui Paulo int ap_list_init(struct hostapd_iface *iface)
300e28a4053SRui Paulo {
301e28a4053SRui Paulo 	return 0;
302e28a4053SRui Paulo }
303e28a4053SRui Paulo 
304e28a4053SRui Paulo 
ap_list_deinit(struct hostapd_iface * iface)305e28a4053SRui Paulo void ap_list_deinit(struct hostapd_iface *iface)
306e28a4053SRui Paulo {
307e28a4053SRui Paulo 	hostapd_free_aps(iface);
308e28a4053SRui Paulo }
309