xref: /freebsd/sys/net80211/ieee80211_node.c (revision b105a069080d9362f2ff71a22e2a1702b7e23d42)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3ae8b7333SSam Leffler  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler #include <sys/param.h>
311a1e1d21SSam Leffler #include <sys/systm.h>
321a1e1d21SSam Leffler #include <sys/mbuf.h>
331a1e1d21SSam Leffler #include <sys/malloc.h>
341a1e1d21SSam Leffler #include <sys/kernel.h>
351a1e1d21SSam Leffler 
368a1b9b6aSSam Leffler #include <sys/socket.h>
371a1e1d21SSam Leffler 
381a1e1d21SSam Leffler #include <net/if.h>
391a1e1d21SSam Leffler #include <net/if_media.h>
401a1e1d21SSam Leffler #include <net/ethernet.h>
411a1e1d21SSam Leffler 
421a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net/bpf.h>
451a1e1d21SSam Leffler 
467268fa64SSam Leffler /*
477268fa64SSam Leffler  * Association id's are managed with a bit vector.
487268fa64SSam Leffler  */
497268fa64SSam Leffler #define	IEEE80211_AID_SET(b, w) \
507268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
517268fa64SSam Leffler #define	IEEE80211_AID_CLR(b, w) \
527268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
537268fa64SSam Leffler #define	IEEE80211_AID_ISSET(b, w) \
547268fa64SSam Leffler 	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
557268fa64SSam Leffler 
56132142c5SDiomidis Spinellis #ifdef IEEE80211_DEBUG_REFCNT
57132142c5SDiomidis Spinellis #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
58132142c5SDiomidis Spinellis #else
59132142c5SDiomidis Spinellis #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
60132142c5SDiomidis Spinellis #endif
61132142c5SDiomidis Spinellis 
6268e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *);
6368e8e04eSSam Leffler 
648a1b9b6aSSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
658a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
668a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
6768e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *);
6868e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
691a1e1d21SSam Leffler 
708a1b9b6aSSam Leffler static void ieee80211_setup_node(struct ieee80211_node_table *,
7168e8e04eSSam Leffler 		struct ieee80211_node *, const uint8_t *);
728a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
738a1b9b6aSSam Leffler 
748a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
75c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
7668e8e04eSSam Leffler 	int inact, int keymaxix);
7768e8e04eSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *);
788a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
79b105a069SSam Leffler static void ieee80211_erp_timeout(struct ieee80211com *);
801a1e1d21SSam Leffler 
8132346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
8237c150c4SSam Leffler 
831a1e1d21SSam Leffler void
848a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
851a1e1d21SSam Leffler {
861a1e1d21SSam Leffler 
878a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
888a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
898a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
908a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
9168e8e04eSSam Leffler 	ic->ic_node_getsignal = node_getsignal;
928a1b9b6aSSam Leffler 
938a1b9b6aSSam Leffler 	/* default station inactivity timer setings */
948a1b9b6aSSam Leffler 	ic->ic_inact_init = IEEE80211_INACT_INIT;
958a1b9b6aSSam Leffler 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
968a1b9b6aSSam Leffler 	ic->ic_inact_run = IEEE80211_INACT_RUN;
978a1b9b6aSSam Leffler 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
988a1b9b6aSSam Leffler 
9968e8e04eSSam Leffler 	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
10068e8e04eSSam Leffler 
101c1225b52SSam Leffler 	/* NB: driver should override */
1028a1b9b6aSSam Leffler 	ic->ic_max_aid = IEEE80211_AID_DEF;
103c066143cSSam Leffler 
104c066143cSSam Leffler 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
105c1225b52SSam Leffler }
106c1225b52SSam Leffler 
107c1225b52SSam Leffler void
108c1225b52SSam Leffler ieee80211_node_lateattach(struct ieee80211com *ic)
109c1225b52SSam Leffler {
110c1225b52SSam Leffler 	struct ieee80211_rsnparms *rsn;
111c1225b52SSam Leffler 
112c1225b52SSam Leffler 	if (ic->ic_max_aid > IEEE80211_AID_MAX)
1138a1b9b6aSSam Leffler 		ic->ic_max_aid = IEEE80211_AID_MAX;
11468e8e04eSSam Leffler 	MALLOC(ic->ic_aid_bitmap, uint32_t *,
11568e8e04eSSam Leffler 		howmany(ic->ic_max_aid, 32) * sizeof(uint32_t),
11668e8e04eSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
1178a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap == NULL) {
1188a1b9b6aSSam Leffler 		/* XXX no way to recover */
1198a1b9b6aSSam Leffler 		printf("%s: no memory for AID bitmap!\n", __func__);
1208a1b9b6aSSam Leffler 		ic->ic_max_aid = 0;
1218a1b9b6aSSam Leffler 	}
1228a1b9b6aSSam Leffler 
123c1225b52SSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
12468e8e04eSSam Leffler 		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix);
1252692bb26SSam Leffler 
126c1225b52SSam Leffler 	ieee80211_reset_bss(ic);
1278a1b9b6aSSam Leffler 	/*
1288a1b9b6aSSam Leffler 	 * Setup "global settings" in the bss node so that
1298a1b9b6aSSam Leffler 	 * each new station automatically inherits them.
1308a1b9b6aSSam Leffler 	 */
131c1225b52SSam Leffler 	rsn = &ic->ic_bss->ni_rsn;
1328a1b9b6aSSam Leffler 	/* WEP, TKIP, and AES-CCM are always supported */
1338a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
1348a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
1358a1b9b6aSSam Leffler 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
1368a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_AES)
1378a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
1388a1b9b6aSSam Leffler 	if (ic->ic_caps & IEEE80211_C_CKIP)
1398a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
1408a1b9b6aSSam Leffler 	/*
1418a1b9b6aSSam Leffler 	 * Default unicast cipher to WEP for 802.1x use.  If
1428a1b9b6aSSam Leffler 	 * WPA is enabled the management code will set these
1438a1b9b6aSSam Leffler 	 * values to reflect.
1448a1b9b6aSSam Leffler 	 */
1458a1b9b6aSSam Leffler 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
1468a1b9b6aSSam Leffler 	rsn->rsn_ucastkeylen = 104 / NBBY;
1478a1b9b6aSSam Leffler 	/*
1488a1b9b6aSSam Leffler 	 * WPA says the multicast cipher is the lowest unicast
1498a1b9b6aSSam Leffler 	 * cipher supported.  But we skip WEP which would
1508a1b9b6aSSam Leffler 	 * otherwise be used based on this criteria.
1518a1b9b6aSSam Leffler 	 */
1528a1b9b6aSSam Leffler 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
1538a1b9b6aSSam Leffler 	rsn->rsn_mcastkeylen = 128 / NBBY;
1548a1b9b6aSSam Leffler 
1558a1b9b6aSSam Leffler 	/*
1568a1b9b6aSSam Leffler 	 * We support both WPA-PSK and 802.1x; the one used
1578a1b9b6aSSam Leffler 	 * is determined by the authentication mode and the
1588a1b9b6aSSam Leffler 	 * setting of the PSK state.
1598a1b9b6aSSam Leffler 	 */
1608a1b9b6aSSam Leffler 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
1618a1b9b6aSSam Leffler 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1628a1b9b6aSSam Leffler 
163c1225b52SSam Leffler 	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
1641a1e1d21SSam Leffler }
1651a1e1d21SSam Leffler 
1661a1e1d21SSam Leffler void
1678a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1681a1e1d21SSam Leffler {
1691a1e1d21SSam Leffler 
1708a1b9b6aSSam Leffler 	if (ic->ic_bss != NULL) {
1718a1b9b6aSSam Leffler 		ieee80211_free_node(ic->ic_bss);
1728a1b9b6aSSam Leffler 		ic->ic_bss = NULL;
1738a1b9b6aSSam Leffler 	}
174acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1758a1b9b6aSSam Leffler 	if (ic->ic_aid_bitmap != NULL) {
17668e8e04eSSam Leffler 		FREE(ic->ic_aid_bitmap, M_80211_NODE);
1778a1b9b6aSSam Leffler 		ic->ic_aid_bitmap = NULL;
1788a1b9b6aSSam Leffler 	}
1798a1b9b6aSSam Leffler }
1808a1b9b6aSSam Leffler 
1818a1b9b6aSSam Leffler /*
1828a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1838a1b9b6aSSam Leffler  */
1848a1b9b6aSSam Leffler 
1858a1b9b6aSSam Leffler void
186e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
1878a1b9b6aSSam Leffler {
188e4918ecdSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
189e4918ecdSSam Leffler 
1908a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
1912045f699SSam Leffler 	ni->ni_inact_reload = ic->ic_inact_run;
192c066143cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
1938a1b9b6aSSam Leffler }
1948a1b9b6aSSam Leffler 
1958a1b9b6aSSam Leffler void
196e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
1978a1b9b6aSSam Leffler {
1987595008bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1997595008bSSam Leffler 
2008a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
201c066143cSSam Leffler 	ni->ni_inact_reload = ic->ic_inact_auth;
202c066143cSSam Leffler 	if (ni->ni_inact > ni->ni_inact_reload)
203c066143cSSam Leffler 		ni->ni_inact = ni->ni_inact_reload;
2048a1b9b6aSSam Leffler }
2058a1b9b6aSSam Leffler 
2068a1b9b6aSSam Leffler /*
2078a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2088a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
2098a1b9b6aSSam Leffler  */
2101b49e120SSam Leffler static void
21168e8e04eSSam Leffler ieee80211_node_set_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
2128a1b9b6aSSam Leffler {
21368e8e04eSSam Leffler 	struct ieee80211_channel *chan = ic->ic_bsschan;
21468e8e04eSSam Leffler 
21568e8e04eSSam Leffler #if 0
21668e8e04eSSam Leffler 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("bss channel not setup"));
21768e8e04eSSam Leffler #else
2181b49e120SSam Leffler 	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
2191b49e120SSam Leffler 		chan = ic->ic_curchan;
22068e8e04eSSam Leffler #endif
2218a1b9b6aSSam Leffler 	ni->ni_chan = chan;
22268e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(chan)) {
22368e8e04eSSam Leffler 		/*
22468e8e04eSSam Leffler 		 * XXX Gotta be careful here; the rate set returned by
22568e8e04eSSam Leffler 		 * ieee80211_get_suprates is actually any HT rate
22668e8e04eSSam Leffler 		 * set so blindly copying it will be bad.  We must
22768e8e04eSSam Leffler 		 * install the legacy rate est in ni_rates and the
22868e8e04eSSam Leffler 		 * HT rate set in ni_htrates.
22968e8e04eSSam Leffler 		 */
23068e8e04eSSam Leffler 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
23168e8e04eSSam Leffler 	}
23241b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
2331a1e1d21SSam Leffler }
2341a1e1d21SSam Leffler 
2351a1e1d21SSam Leffler /*
236097131ffSSam Leffler  * Probe the curent channel, if allowed, while scanning.
237097131ffSSam Leffler  * If the channel is not marked passive-only then send
238097131ffSSam Leffler  * a probe request immediately.  Otherwise mark state and
239097131ffSSam Leffler  * listen for beacons on the channel; if we receive something
240097131ffSSam Leffler  * then we'll transmit a probe request.
241097131ffSSam Leffler  */
242097131ffSSam Leffler void
243097131ffSSam Leffler ieee80211_probe_curchan(struct ieee80211com *ic, int force)
244097131ffSSam Leffler {
245097131ffSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
246097131ffSSam Leffler 
247097131ffSSam Leffler 	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
248097131ffSSam Leffler 		/*
249097131ffSSam Leffler 		 * XXX send both broadcast+directed probe request
250097131ffSSam Leffler 		 */
251097131ffSSam Leffler 		ieee80211_send_probereq(ic->ic_bss,
252097131ffSSam Leffler 			ic->ic_myaddr, ifp->if_broadcastaddr,
253097131ffSSam Leffler 			ifp->if_broadcastaddr,
25468e8e04eSSam Leffler 			ic->ic_des_ssid[0].ssid, ic->ic_des_ssid[0].len,
255097131ffSSam Leffler 			ic->ic_opt_ie, ic->ic_opt_ie_len);
256097131ffSSam Leffler 	} else
257097131ffSSam Leffler 		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
258097131ffSSam Leffler }
259097131ffSSam Leffler 
260f9cd9174SSam Leffler static __inline void
261f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
262f9cd9174SSam Leffler {
263f9cd9174SSam Leffler 	/* propagate useful state */
264f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
265f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
266f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
267f9cd9174SSam Leffler 	nbss->ni_rsn = obss->ni_rsn;
268f9cd9174SSam Leffler 	/* XXX statistics? */
269f9cd9174SSam Leffler }
270f9cd9174SSam Leffler 
2711a1e1d21SSam Leffler void
2721a1e1d21SSam Leffler ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
2731a1e1d21SSam Leffler {
274acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt;
2751a1e1d21SSam Leffler 	struct ieee80211_node *ni;
2768a1b9b6aSSam Leffler 
2778a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
2788a1b9b6aSSam Leffler 		"%s: creating ibss\n", __func__);
2798a1b9b6aSSam Leffler 
2808a1b9b6aSSam Leffler 	/*
2818a1b9b6aSSam Leffler 	 * Create the station/neighbor table.  Note that for adhoc
2828a1b9b6aSSam Leffler 	 * mode we make the initial inactivity timer longer since
2838a1b9b6aSSam Leffler 	 * we create nodes only through discovery and they typically
2848a1b9b6aSSam Leffler 	 * are long-lived associations.
2858a1b9b6aSSam Leffler 	 */
286acc4f7f5SSam Leffler 	nt = &ic->ic_sta;
287acc4f7f5SSam Leffler 	IEEE80211_NODE_LOCK(nt);
288acc4f7f5SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
289acc4f7f5SSam Leffler 		nt->nt_name = "station";
290acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_init;
291acc4f7f5SSam Leffler 	} else {
292acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
293acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
294acc4f7f5SSam Leffler 	}
295acc4f7f5SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
296acc4f7f5SSam Leffler 
297c1225b52SSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
298acc4f7f5SSam Leffler 	if (ni == NULL) {
299acc4f7f5SSam Leffler 		/* XXX recovery? */
3008a1b9b6aSSam Leffler 		return;
3018a1b9b6aSSam Leffler 	}
3021a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
30368e8e04eSSam Leffler 	ni->ni_esslen = ic->ic_des_ssid[0].len;
30468e8e04eSSam Leffler 	memcpy(ni->ni_essid, ic->ic_des_ssid[0].ssid, ni->ni_esslen);
30568e8e04eSSam Leffler 	if (ic->ic_bss != NULL)
306f9cd9174SSam Leffler 		copy_bss(ni, ic->ic_bss);
307d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
3088a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
3091a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3101a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3111a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3121a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3131a1e1d21SSam Leffler 	}
3148a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
3158a1b9b6aSSam Leffler 		ic->ic_flags |= IEEE80211_F_SIBSS;
3168a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
31748b0a5beSSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
31848b0a5beSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
319fe49f061SSam Leffler 		else {
320fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
321fe49f061SSam Leffler 			/* clear group bit, add local bit */
322fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
323fe49f061SSam Leffler 		}
32450d8b493SSam Leffler 	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
32550d8b493SSam Leffler 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
32650d8b493SSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
32750d8b493SSam Leffler 		else
32850d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
3298a1b9b6aSSam Leffler 	}
3308a1b9b6aSSam Leffler 	/*
3318a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3328a1b9b6aSSam Leffler 	 */
33368e8e04eSSam Leffler 	ic->ic_bsschan = chan;
33468e8e04eSSam Leffler 	ieee80211_node_set_chan(ic, ni);
33568e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(chan);
3368a1b9b6aSSam Leffler 	/*
3378a1b9b6aSSam Leffler 	 * Do mode-specific rate setup.
3388a1b9b6aSSam Leffler 	 */
33968e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan)) {
34068e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
34168e8e04eSSam Leffler 			/*
34268e8e04eSSam Leffler 			 * Use a mixed 11b/11g rate set.
34368e8e04eSSam Leffler 			 */
34468e8e04eSSam Leffler 			ieee80211_set11gbasicrates(&ni->ni_rates,
34568e8e04eSSam Leffler 				IEEE80211_MODE_11G);
34668e8e04eSSam Leffler 		} else if (IEEE80211_IS_CHAN_B(chan)) {
34768e8e04eSSam Leffler 			/*
34868e8e04eSSam Leffler 			 * Force pure 11b rate set.
34968e8e04eSSam Leffler 			 */
35068e8e04eSSam Leffler 			ieee80211_set11gbasicrates(&ni->ni_rates,
35168e8e04eSSam Leffler 				IEEE80211_MODE_11B);
35268e8e04eSSam Leffler 		}
3531a1e1d21SSam Leffler 	}
3541a1e1d21SSam Leffler 
35568e8e04eSSam Leffler 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
35668e8e04eSSam Leffler }
35768e8e04eSSam Leffler 
35868e8e04eSSam Leffler /*
35968e8e04eSSam Leffler  * Reset bss state on transition to the INIT state.
36068e8e04eSSam Leffler  * Clear any stations from the table (they have been
36168e8e04eSSam Leffler  * deauth'd) and reset the bss node (clears key, rate
36268e8e04eSSam Leffler  * etc. state).
36368e8e04eSSam Leffler  */
3648a1b9b6aSSam Leffler void
3658a1b9b6aSSam Leffler ieee80211_reset_bss(struct ieee80211com *ic)
366b4c5a90fSSam Leffler {
3678a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
3688a1b9b6aSSam Leffler 
36968e8e04eSSam Leffler 	callout_drain(&ic->ic_inact);
370acc4f7f5SSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta);
37168e8e04eSSam Leffler 	ieee80211_reset_erp(ic);
372acc4f7f5SSam Leffler 
37368e8e04eSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
3748a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
3758a1b9b6aSSam Leffler 	obss = ic->ic_bss;
3768a1b9b6aSSam Leffler 	ic->ic_bss = ieee80211_ref_node(ni);
377f9cd9174SSam Leffler 	if (obss != NULL) {
378f9cd9174SSam Leffler 		copy_bss(ni, obss);
379d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
3808a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
3818a1b9b6aSSam Leffler 	}
382f9cd9174SSam Leffler }
3838a1b9b6aSSam Leffler 
3848a1b9b6aSSam Leffler static int
38568e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni,
38668e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
3878a1b9b6aSSam Leffler {
38868e8e04eSSam Leffler 	int i;
38968e8e04eSSam Leffler 
39068e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
39168e8e04eSSam Leffler 		if (ni->ni_esslen == ssids[i].len &&
39268e8e04eSSam Leffler 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
39368e8e04eSSam Leffler 			return 1;
39468e8e04eSSam Leffler 	}
39568e8e04eSSam Leffler 	return 0;
39668e8e04eSSam Leffler }
39768e8e04eSSam Leffler 
39868e8e04eSSam Leffler /*
39968e8e04eSSam Leffler  * Test a node for suitability/compatibility.
40068e8e04eSSam Leffler  */
40168e8e04eSSam Leffler static int
40268e8e04eSSam Leffler check_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
40368e8e04eSSam Leffler {
40468e8e04eSSam Leffler         uint8_t rate;
40568e8e04eSSam Leffler 
40668e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
40768e8e04eSSam Leffler 		return 0;
40868e8e04eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
40968e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
41068e8e04eSSam Leffler 			return 0;
41168e8e04eSSam Leffler 	} else {
41268e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
41368e8e04eSSam Leffler 			return 0;
41468e8e04eSSam Leffler 	}
41568e8e04eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
41668e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
41768e8e04eSSam Leffler 			return 0;
41868e8e04eSSam Leffler 	} else {
41968e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
42068e8e04eSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
42168e8e04eSSam Leffler 			return 0;
42268e8e04eSSam Leffler 	}
42368e8e04eSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
42468e8e04eSSam Leffler 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
42568e8e04eSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
42668e8e04eSSam Leffler 		return 0;
42768e8e04eSSam Leffler 	if (ic->ic_des_nssid != 0 &&
42868e8e04eSSam Leffler 	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
42968e8e04eSSam Leffler 		return 0;
43068e8e04eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
43168e8e04eSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
43268e8e04eSSam Leffler 		return 0;
43368e8e04eSSam Leffler 	return 1;
43468e8e04eSSam Leffler }
43568e8e04eSSam Leffler 
43668e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
43768e8e04eSSam Leffler /*
43868e8e04eSSam Leffler  * Display node suitability/compatibility.
43968e8e04eSSam Leffler  */
44068e8e04eSSam Leffler static void
44168e8e04eSSam Leffler check_bss_debug(struct ieee80211com *ic, struct ieee80211_node *ni)
44268e8e04eSSam Leffler {
44368e8e04eSSam Leffler         uint8_t rate;
444b4c5a90fSSam Leffler         int fail;
445b4c5a90fSSam Leffler 
446b4c5a90fSSam Leffler 	fail = 0;
447b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
448b4c5a90fSSam Leffler 		fail |= 0x01;
449b4c5a90fSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
450b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
451b4c5a90fSSam Leffler 			fail |= 0x02;
452b4c5a90fSSam Leffler 	} else {
453b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
454b4c5a90fSSam Leffler 			fail |= 0x02;
455b4c5a90fSSam Leffler 	}
4568a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
457b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
458b4c5a90fSSam Leffler 			fail |= 0x04;
459b4c5a90fSSam Leffler 	} else {
460b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
461b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
462b4c5a90fSSam Leffler 			fail |= 0x04;
463b4c5a90fSSam Leffler 	}
46470e28b9aSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
46579edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
466b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
467b4c5a90fSSam Leffler 		fail |= 0x08;
46868e8e04eSSam Leffler 	if (ic->ic_des_nssid != 0 &&
46968e8e04eSSam Leffler 	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
470b4c5a90fSSam Leffler 		fail |= 0x10;
471b4c5a90fSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
472b4c5a90fSSam Leffler 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
473b4c5a90fSSam Leffler 		fail |= 0x20;
47468e8e04eSSam Leffler 
47568e8e04eSSam Leffler 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
47668e8e04eSSam Leffler 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
47768e8e04eSSam Leffler 	printf(" %3d%c",
47868e8e04eSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
479b4c5a90fSSam Leffler 	printf(" %+4d", ni->ni_rssi);
480b4c5a90fSSam Leffler 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
481b4c5a90fSSam Leffler 	    fail & 0x08 ? '!' : ' ');
482b4c5a90fSSam Leffler 	printf(" %4s%c",
483b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
484b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
485b4c5a90fSSam Leffler 	    "????",
486b4c5a90fSSam Leffler 	    fail & 0x02 ? '!' : ' ');
487b4c5a90fSSam Leffler 	printf(" %3s%c ",
48868e8e04eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
489b4c5a90fSSam Leffler 	    fail & 0x04 ? '!' : ' ');
490b4c5a90fSSam Leffler 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
491b4c5a90fSSam Leffler 	printf("%s\n", fail & 0x10 ? "!" : "");
492b4c5a90fSSam Leffler }
49368e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
4948a1b9b6aSSam Leffler 
495750d6d0cSSam Leffler /*
4968a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
4978a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
4988a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
4998a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
5008a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
5018a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
5028a1b9b6aSSam Leffler  *
5038a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
5048a1b9b6aSSam Leffler  *
5058a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
506750d6d0cSSam Leffler  */
5078a1b9b6aSSam Leffler int
508641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
5098a1b9b6aSSam Leffler {
510641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5118a1b9b6aSSam Leffler 
51296acc1b6SSam Leffler 	if (ni == ic->ic_bss ||
51396acc1b6SSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
5148a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
5158a1b9b6aSSam Leffler 		return 0;
5168a1b9b6aSSam Leffler 	}
51768e8e04eSSam Leffler 	if (!check_bss(ic, ni)) {
51868e8e04eSSam Leffler 		/* capabilities mismatch */
5198a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
5208a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
52168e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
52268e8e04eSSam Leffler 		if (ieee80211_msg_assoc(ic))
52368e8e04eSSam Leffler 			check_bss_debug(ic, ni);
52468e8e04eSSam Leffler #endif
5258a1b9b6aSSam Leffler 		ic->ic_stats.is_ibss_capmismatch++;
5268a1b9b6aSSam Leffler 		return 0;
5278a1b9b6aSSam Leffler 	}
5288a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
5298a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
5308a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
5318a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
5328a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
5338a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
5348a1b9b6aSSam Leffler 	);
53568e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
5368a1b9b6aSSam Leffler }
5378a1b9b6aSSam Leffler 
5388a1b9b6aSSam Leffler /*
5398a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
5408a1b9b6aSSam Leffler  * be passed in with a held reference.
5418a1b9b6aSSam Leffler  */
54268e8e04eSSam Leffler static int
54368e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs)
5448a1b9b6aSSam Leffler {
54568e8e04eSSam Leffler 	struct ieee80211com *ic = selbs->ni_ic;
5468a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
54768e8e04eSSam Leffler 	int canreassoc;
5488a1b9b6aSSam Leffler 
5498a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
550acc4f7f5SSam Leffler 		struct ieee80211_node_table *nt;
5518a1b9b6aSSam Leffler 		/*
552acc4f7f5SSam Leffler 		 * Fillin the neighbor table; it will already
5533d073929SSam Leffler 		 * exist if we are simply switching mastership.
554acc4f7f5SSam Leffler 		 * XXX ic_sta always setup so this is unnecessary?
5558a1b9b6aSSam Leffler 		 */
556acc4f7f5SSam Leffler 		nt = &ic->ic_sta;
557acc4f7f5SSam Leffler 		IEEE80211_NODE_LOCK(nt);
558acc4f7f5SSam Leffler 		nt->nt_name = "neighbor";
559acc4f7f5SSam Leffler 		nt->nt_inact_init = ic->ic_inact_run;
560acc4f7f5SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
5613d073929SSam Leffler 	}
5621a1e1d21SSam Leffler 
5638a1b9b6aSSam Leffler 	/*
5648a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
5658a1b9b6aSSam Leffler 	 */
5668a1b9b6aSSam Leffler 	obss = ic->ic_bss;
56768e8e04eSSam Leffler 	/*
56868e8e04eSSam Leffler 	 * Check if old+new node have the same address in which
56968e8e04eSSam Leffler 	 * case we can reassociate when operating in sta mode.
57068e8e04eSSam Leffler 	 */
57168e8e04eSSam Leffler 	canreassoc = (obss != NULL &&
57268e8e04eSSam Leffler 		ic->ic_state == IEEE80211_S_RUN &&
57368e8e04eSSam Leffler 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
574acc4f7f5SSam Leffler 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
5751fd2349dSSam Leffler 	if (obss != NULL) {
5761fd2349dSSam Leffler 		copy_bss(selbs, obss);
5778a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
5781fd2349dSSam Leffler 	}
57979edaebfSSam Leffler 
58079edaebfSSam Leffler 	/*
58179edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
58279edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
58379edaebfSSam Leffler 	 */
58470e28b9aSSam Leffler 	ieee80211_fix_rate(ic->ic_bss, &ic->ic_bss->ni_rates,
58570e28b9aSSam Leffler 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
58679edaebfSSam Leffler 
58768e8e04eSSam Leffler 	ic->ic_bsschan = selbs->ni_chan;
58868e8e04eSSam Leffler 	ic->ic_curchan = ic->ic_bsschan;
58968e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
59068e8e04eSSam Leffler 	ic->ic_set_channel(ic);
5918a1b9b6aSSam Leffler 	/*
5928a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
5938a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
5948a1b9b6aSSam Leffler 	 * mode is locked.
5958a1b9b6aSSam Leffler 	 */
5968a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
5978a1b9b6aSSam Leffler 	ieee80211_wme_initparams(ic);
598acc4f7f5SSam Leffler 
59968e8e04eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
60068e8e04eSSam Leffler 		if (canreassoc) {
60168e8e04eSSam Leffler 			/* Reassociate */
60268e8e04eSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, 1);
60368e8e04eSSam Leffler 		} else {
60468e8e04eSSam Leffler 			/*
60568e8e04eSSam Leffler 			 * Act as if we received a DEAUTH frame in case we
60668e8e04eSSam Leffler 			 * are invoked from the RUN state.  This will cause
60768e8e04eSSam Leffler 			 * us to try to re-authenticate if we are operating
60868e8e04eSSam Leffler 			 * as a station.
60968e8e04eSSam Leffler 			 */
61068e8e04eSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
61168e8e04eSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH);
61268e8e04eSSam Leffler 		}
61368e8e04eSSam Leffler 	} else
614acc4f7f5SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
6158a1b9b6aSSam Leffler 	return 1;
6168a1b9b6aSSam Leffler }
6178a1b9b6aSSam Leffler 
61868e8e04eSSam Leffler int
61968e8e04eSSam Leffler ieee80211_sta_join(struct ieee80211com *ic,
62068e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se)
62168e8e04eSSam Leffler {
62268e8e04eSSam Leffler 	struct ieee80211_node *ni;
62368e8e04eSSam Leffler 
62468e8e04eSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, se->se_macaddr);
62568e8e04eSSam Leffler 	if (ni == NULL) {
62668e8e04eSSam Leffler 		/* XXX msg */
62768e8e04eSSam Leffler 		return 0;
62868e8e04eSSam Leffler 	}
62968e8e04eSSam Leffler 	/*
63068e8e04eSSam Leffler 	 * Expand scan state into node's format.
63168e8e04eSSam Leffler 	 * XXX may not need all this stuff
63268e8e04eSSam Leffler 	 */
63368e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
63468e8e04eSSam Leffler 	ni->ni_esslen = se->se_ssid[1];
63568e8e04eSSam Leffler 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
63668e8e04eSSam Leffler 	ni->ni_rstamp = se->se_rstamp;
63768e8e04eSSam Leffler 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
63868e8e04eSSam Leffler 	ni->ni_intval = se->se_intval;
63968e8e04eSSam Leffler 	ni->ni_capinfo = se->se_capinfo;
64068e8e04eSSam Leffler 	/* XXX shift to 11n channel if htinfo present */
64168e8e04eSSam Leffler 	ni->ni_chan = se->se_chan;
64268e8e04eSSam Leffler 	ni->ni_timoff = se->se_timoff;
64368e8e04eSSam Leffler 	ni->ni_fhdwell = se->se_fhdwell;
64468e8e04eSSam Leffler 	ni->ni_fhindex = se->se_fhindex;
64568e8e04eSSam Leffler 	ni->ni_erp = se->se_erp;
64668e8e04eSSam Leffler 	ni->ni_rssi = se->se_rssi;
64768e8e04eSSam Leffler 	ni->ni_noise = se->se_noise;
64868e8e04eSSam Leffler 	if (se->se_htcap_ie != NULL)
64968e8e04eSSam Leffler 		ieee80211_ht_node_init(ni, se->se_htcap_ie);
65068e8e04eSSam Leffler 	if (se->se_htinfo_ie != NULL)
65168e8e04eSSam Leffler 		ieee80211_parse_htinfo(ni, se->se_htinfo_ie);
65268e8e04eSSam Leffler 	if (se->se_wpa_ie != NULL)
65368e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_wpa_ie, se->se_wpa_ie);
65468e8e04eSSam Leffler 	if (se->se_rsn_ie != NULL)
65568e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_rsn_ie, se->se_rsn_ie);
65668e8e04eSSam Leffler 	if (se->se_wme_ie != NULL)
65768e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_wme_ie, se->se_wme_ie);
65868e8e04eSSam Leffler 	if (se->se_ath_ie != NULL)
65968e8e04eSSam Leffler 		ieee80211_saveath(ni, se->se_ath_ie);
66068e8e04eSSam Leffler 
66168e8e04eSSam Leffler 	ic->ic_dtim_period = se->se_dtimperiod;
66268e8e04eSSam Leffler 	ic->ic_dtim_count = 0;
66368e8e04eSSam Leffler 
66468e8e04eSSam Leffler 	/* NB: must be after ni_chan is setup */
66568e8e04eSSam Leffler 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
66668e8e04eSSam Leffler 		IEEE80211_F_DOSORT);
66768e8e04eSSam Leffler 	if (se->se_htcap_ie != NULL)
66868e8e04eSSam Leffler 		ieee80211_setup_htrates(ni, se->se_htcap_ie, IEEE80211_F_JOIN);
66968e8e04eSSam Leffler 	if (se->se_htinfo_ie != NULL)
67068e8e04eSSam Leffler 		ieee80211_setup_basic_htrates(ni, se->se_htinfo_ie);
67168e8e04eSSam Leffler 
67268e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
67368e8e04eSSam Leffler }
67468e8e04eSSam Leffler 
6758a1b9b6aSSam Leffler /*
6768a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
6778a1b9b6aSSam Leffler  * be passed in with a held reference.
6788a1b9b6aSSam Leffler  */
6798a1b9b6aSSam Leffler void
6808a1b9b6aSSam Leffler ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
6818a1b9b6aSSam Leffler {
6828a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
6838a1b9b6aSSam Leffler 	ieee80211_notify_node_leave(ic, ni);
6848a1b9b6aSSam Leffler }
6858a1b9b6aSSam Leffler 
6861a1e1d21SSam Leffler static struct ieee80211_node *
6878a1b9b6aSSam Leffler node_alloc(struct ieee80211_node_table *nt)
6881a1e1d21SSam Leffler {
689410ca74bSSam Leffler 	struct ieee80211_node *ni;
6908a1b9b6aSSam Leffler 
691410ca74bSSam Leffler 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
692410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
693410ca74bSSam Leffler 	return ni;
6941a1e1d21SSam Leffler }
6951a1e1d21SSam Leffler 
6968a1b9b6aSSam Leffler /*
6978a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
6988a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
6998a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
7008a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
7018a1b9b6aSSam Leffler  */
7021a1e1d21SSam Leffler static void
7038a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
7041a1e1d21SSam Leffler {
7058a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
7068a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
70768e8e04eSSam Leffler 	int i;
7088a1b9b6aSSam Leffler 
7098a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
7108a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
7118a1b9b6aSSam Leffler 		ic->ic_ps_sta--;
7128a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
7138a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
7148a1b9b6aSSam Leffler 		    "[%s] power save mode off, %u sta's in ps mode\n",
7158a1b9b6aSSam Leffler 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
7168a1b9b6aSSam Leffler 	}
717ebdda46cSSam Leffler 	/*
718ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
719ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
720ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
721ebdda46cSSam Leffler 	 * do it just in case.
722ebdda46cSSam Leffler 	 */
723ebdda46cSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AREF;
7248a1b9b6aSSam Leffler 
7258a1b9b6aSSam Leffler 	/*
7268a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
7278a1b9b6aSSam Leffler 	 */
72868e8e04eSSam Leffler 	if (ieee80211_node_saveq_drain(ni) != 0 && ic->ic_set_tim != NULL)
729edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 0);
7308a1b9b6aSSam Leffler 
7318a1b9b6aSSam Leffler 	ni->ni_associd = 0;
7328a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
73368e8e04eSSam Leffler 		FREE(ni->ni_challenge, M_80211_NODE);
7348a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
7358a1b9b6aSSam Leffler 	}
7368a1b9b6aSSam Leffler 	/*
7378a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
7388a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
7398a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
7408a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
7418a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
7428a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
7438a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
7448a1b9b6aSSam Leffler 	 * which will rebuild this state.
7458a1b9b6aSSam Leffler 	 *
7468a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
7478a1b9b6aSSam Leffler 	 */
7488a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
7498a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
7508a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
7518a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
7528a1b9b6aSSam Leffler 		}
753c1225b52SSam Leffler 	/*
754c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
755c1225b52SSam Leffler 	 */
756c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
7578a1b9b6aSSam Leffler #undef N
7588a1b9b6aSSam Leffler }
7598a1b9b6aSSam Leffler 
7608a1b9b6aSSam Leffler static void
7618a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
7628a1b9b6aSSam Leffler {
7638a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
7648a1b9b6aSSam Leffler 
7658a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
7668a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
76768e8e04eSSam Leffler 		FREE(ni->ni_wpa_ie, M_80211_NODE);
76868e8e04eSSam Leffler 	if (ni->ni_rsn_ie != NULL)
76968e8e04eSSam Leffler 		FREE(ni->ni_rsn_ie, M_80211_NODE);
7708a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
77168e8e04eSSam Leffler 		FREE(ni->ni_wme_ie, M_80211_NODE);
77268e8e04eSSam Leffler 	if (ni->ni_ath_ie != NULL)
77368e8e04eSSam Leffler 		FREE(ni->ni_ath_ie, M_80211_NODE);
7748a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
775410ca74bSSam Leffler 	FREE(ni, M_80211_NODE);
7761a1e1d21SSam Leffler }
7771a1e1d21SSam Leffler 
77868e8e04eSSam Leffler static int8_t
7798a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
780d1e61976SSam Leffler {
781d1e61976SSam Leffler 	return ni->ni_rssi;
782d1e61976SSam Leffler }
783d1e61976SSam Leffler 
7841a1e1d21SSam Leffler static void
78568e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
78668e8e04eSSam Leffler {
78768e8e04eSSam Leffler 	*rssi = ni->ni_rssi;
78868e8e04eSSam Leffler 	*noise = ni->ni_noise;
78968e8e04eSSam Leffler }
79068e8e04eSSam Leffler 
79168e8e04eSSam Leffler static void
7928a1b9b6aSSam Leffler ieee80211_setup_node(struct ieee80211_node_table *nt,
79368e8e04eSSam Leffler 	struct ieee80211_node *ni, const uint8_t *macaddr)
7941a1e1d21SSam Leffler {
7958a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
7961a1e1d21SSam Leffler 	int hash;
7971a1e1d21SSam Leffler 
7988a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
79949a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
8008a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
8018a1b9b6aSSam Leffler 
8021a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
8031a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
8048a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
8058a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
8068a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
8078a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
8088a1b9b6aSSam Leffler 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
8092045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
8102045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
81168e8e04eSSam Leffler 	ni->ni_ath_defkeyix = 0x7fff;
8128a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
8138a1b9b6aSSam Leffler 
8148a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
8158a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
8168a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
8178a1b9b6aSSam Leffler 	ni->ni_table = nt;
8188a1b9b6aSSam Leffler 	ni->ni_ic = ic;
8198a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
8201a1e1d21SSam Leffler }
8211a1e1d21SSam Leffler 
8221a1e1d21SSam Leffler struct ieee80211_node *
82368e8e04eSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
8241a1e1d21SSam Leffler {
8258a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
8268a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8278a1b9b6aSSam Leffler 
8288a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
8291a1e1d21SSam Leffler 	if (ni != NULL)
8308a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
831c64bfa0fSSam Leffler 	else
832c64bfa0fSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
8331a1e1d21SSam Leffler 	return ni;
8341a1e1d21SSam Leffler }
8351a1e1d21SSam Leffler 
83697c973adSSam Leffler /*
83797c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
83897c973adSSam Leffler  * to the specified station.  We craft only as much state as we
83997c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
84097c973adSSam Leffler  * once the send completes.
84197c973adSSam Leffler  */
84297c973adSSam Leffler struct ieee80211_node *
84368e8e04eSSam Leffler ieee80211_tmp_node(struct ieee80211com *ic, const uint8_t *macaddr)
84497c973adSSam Leffler {
84597c973adSSam Leffler 	struct ieee80211_node *ni;
84697c973adSSam Leffler 
84797c973adSSam Leffler 	ni = ic->ic_node_alloc(&ic->ic_sta);
84897c973adSSam Leffler 	if (ni != NULL) {
84997c973adSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
85097c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
85197c973adSSam Leffler 
85297c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
85397c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
85497c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
85597c973adSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
85697c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
85768e8e04eSSam Leffler 		ieee80211_node_set_chan(ic, ni);
85897c973adSSam Leffler 		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
85997c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
86097c973adSSam Leffler 		/* XXX optimize away */
86197c973adSSam Leffler 		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
86297c973adSSam Leffler 
86397c973adSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
86497c973adSSam Leffler 		ni->ni_ic = ic;
86597c973adSSam Leffler 	} else {
86697c973adSSam Leffler 		/* XXX msg */
86797c973adSSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
86897c973adSSam Leffler 	}
86997c973adSSam Leffler 	return ni;
87097c973adSSam Leffler }
87197c973adSSam Leffler 
8721a1e1d21SSam Leffler struct ieee80211_node *
87368e8e04eSSam Leffler ieee80211_dup_bss(struct ieee80211_node_table *nt, const uint8_t *macaddr)
8741a1e1d21SSam Leffler {
8758a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
8768a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8778a1b9b6aSSam Leffler 
8788a1b9b6aSSam Leffler 	ni = ic->ic_node_alloc(nt);
8791a1e1d21SSam Leffler 	if (ni != NULL) {
8808a1b9b6aSSam Leffler 		ieee80211_setup_node(nt, ni, macaddr);
881694dca64SSam Leffler 		/*
882694dca64SSam Leffler 		 * Inherit from ic_bss.
883694dca64SSam Leffler 		 */
8848a1b9b6aSSam Leffler 		ni->ni_authmode = ic->ic_bss->ni_authmode;
8858a1b9b6aSSam Leffler 		ni->ni_txpower = ic->ic_bss->ni_txpower;
8868a1b9b6aSSam Leffler 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
887694dca64SSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
88868e8e04eSSam Leffler 		ieee80211_node_set_chan(ic, ni);
8898a1b9b6aSSam Leffler 		ni->ni_rsn = ic->ic_bss->ni_rsn;
890694dca64SSam Leffler 	} else
891694dca64SSam Leffler 		ic->ic_stats.is_rx_nodealloc++;
8921a1e1d21SSam Leffler 	return ni;
8931a1e1d21SSam Leffler }
8941a1e1d21SSam Leffler 
895750d6d0cSSam Leffler static struct ieee80211_node *
8968a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
8978a1b9b6aSSam Leffler _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
89868e8e04eSSam Leffler 	const uint8_t *macaddr, const char *func, int line)
8998a1b9b6aSSam Leffler #else
9008a1b9b6aSSam Leffler _ieee80211_find_node(struct ieee80211_node_table *nt,
90168e8e04eSSam Leffler 	const uint8_t *macaddr)
9028a1b9b6aSSam Leffler #endif
9031a1e1d21SSam Leffler {
9041a1e1d21SSam Leffler 	struct ieee80211_node *ni;
9051a1e1d21SSam Leffler 	int hash;
9061a1e1d21SSam Leffler 
9078a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
908750d6d0cSSam Leffler 
9091a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
9108a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
9111a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
9128a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
9138a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9148a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
91549a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
91649a15236SSam Leffler 			    func, line,
91749a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
9188a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
9198a1b9b6aSSam Leffler #endif
920750d6d0cSSam Leffler 			return ni;
9211a1e1d21SSam Leffler 		}
9221a1e1d21SSam Leffler 	}
923750d6d0cSSam Leffler 	return NULL;
924750d6d0cSSam Leffler }
9258a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9268a1b9b6aSSam Leffler #define	_ieee80211_find_node(nt, mac) \
9278a1b9b6aSSam Leffler 	_ieee80211_find_node_debug(nt, mac, func, line)
9288a1b9b6aSSam Leffler #endif
929750d6d0cSSam Leffler 
930750d6d0cSSam Leffler struct ieee80211_node *
9318a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
9328a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
93368e8e04eSSam Leffler 	const uint8_t *macaddr, const char *func, int line)
9348a1b9b6aSSam Leffler #else
93568e8e04eSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
9368a1b9b6aSSam Leffler #endif
937750d6d0cSSam Leffler {
938750d6d0cSSam Leffler 	struct ieee80211_node *ni;
939750d6d0cSSam Leffler 
9408a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
9418a1b9b6aSSam Leffler 	ni = _ieee80211_find_node(nt, macaddr);
9428a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
9431a1e1d21SSam Leffler 	return ni;
9441a1e1d21SSam Leffler }
9451a1e1d21SSam Leffler 
9461a1e1d21SSam Leffler /*
9478a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
9488a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
9498a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
9508a1b9b6aSSam Leffler  * it's private state.
9518a1b9b6aSSam Leffler  */
9528a1b9b6aSSam Leffler struct ieee80211_node *
9538a1b9b6aSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
95468e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
9558a1b9b6aSSam Leffler {
9568a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
9578a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9588a1b9b6aSSam Leffler 
959be425a0fSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
960be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
9618a1b9b6aSSam Leffler 	ni = ieee80211_dup_bss(nt, macaddr);
9628a1b9b6aSSam Leffler 	if (ni != NULL) {
9638a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
9648a1b9b6aSSam Leffler 		ni->ni_rates = ic->ic_bss->ni_rates;
965736b3dc3SSam Leffler 		if (ic->ic_newassoc != NULL)
966e9962332SSam Leffler 			ic->ic_newassoc(ni, 1);
9678e292e8eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
9688e292e8eSSam Leffler 			/*
96968e8e04eSSam Leffler 			 * In adhoc demo mode there are no management
97068e8e04eSSam Leffler 			 * frames to use to discover neighbor capabilities,
97168e8e04eSSam Leffler 			 * so blindly propagate the local configuration
97268e8e04eSSam Leffler 			 * so we can do interesting things (e.g. use
97368e8e04eSSam Leffler 			 * WME to disable ACK's).
9748e292e8eSSam Leffler 			 */
9758e292e8eSSam Leffler 			if (ic->ic_flags & IEEE80211_F_WME)
9768e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
97768e8e04eSSam Leffler 			if (ic->ic_flags & IEEE80211_F_FF)
97868e8e04eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_FF;
9798e292e8eSSam Leffler 		}
98068e8e04eSSam Leffler 		/* XXX not right for 802.1x/WPA */
98168e8e04eSSam Leffler 		ieee80211_node_authorize(ni);
9828a1b9b6aSSam Leffler 	}
9838a1b9b6aSSam Leffler 	return ni;
9848a1b9b6aSSam Leffler }
9858a1b9b6aSSam Leffler 
986be425a0fSSam Leffler void
987be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
988be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
989be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
990be425a0fSSam Leffler {
991be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
992be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
993be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
994be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
995be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
996be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
997be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
998be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
999be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1000be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1001be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
1002be425a0fSSam Leffler 	if (sp->wme != NULL)
1003be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1004be425a0fSSam Leffler 	if (sp->wpa != NULL)
1005be425a0fSSam Leffler 		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
100668e8e04eSSam Leffler 	if (sp->rsn != NULL)
100768e8e04eSSam Leffler 		ieee80211_saveie(&ni->ni_rsn_ie, sp->rsn);
100868e8e04eSSam Leffler 	if (sp->ath != NULL)
100968e8e04eSSam Leffler 		ieee80211_saveath(ni, sp->ath);
1010be425a0fSSam Leffler 
1011be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
101279edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
101379edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
101479edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1015be425a0fSSam Leffler }
1016be425a0fSSam Leffler 
1017b5c99415SSam Leffler /*
1018b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1019b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1020b5c99415SSam Leffler  * benefit we we treat this like an association so the
1021b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1022b5c99415SSam Leffler  */
1023b5c99415SSam Leffler struct ieee80211_node *
1024b5c99415SSam Leffler ieee80211_add_neighbor(struct ieee80211com *ic,
1025b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1026b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1027b5c99415SSam Leffler {
1028b5c99415SSam Leffler 	struct ieee80211_node *ni;
1029b5c99415SSam Leffler 
1030be425a0fSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1031be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1032b5c99415SSam Leffler 	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1033b5c99415SSam Leffler 	if (ni != NULL) {
1034be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1035b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1036b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1037b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1038b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1039b5c99415SSam Leffler 	}
1040b5c99415SSam Leffler 	return ni;
1041b5c99415SSam Leffler }
1042b5c99415SSam Leffler 
1043c1225b52SSam Leffler #define	IS_CTL(wh) \
1044c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1045c1225b52SSam Leffler #define	IS_PSPOLL(wh) \
1046c1225b52SSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
104768e8e04eSSam Leffler #define	IS_BAR(wh) \
104868e8e04eSSam Leffler 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
104968e8e04eSSam Leffler 
10508a1b9b6aSSam Leffler /*
10518a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
10528a1b9b6aSSam Leffler  * (referenced) node up to the 802.11 layer for its use.  We
10538a1b9b6aSSam Leffler  * are required to pass some node so we fall back to ic_bss
10548a1b9b6aSSam Leffler  * when this frame is from an unknown sender.  The 802.11 layer
10558a1b9b6aSSam Leffler  * knows this means the sender wasn't in the node table and
10568a1b9b6aSSam Leffler  * acts accordingly.
10578a1b9b6aSSam Leffler  */
10588a1b9b6aSSam Leffler struct ieee80211_node *
10598a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
10608a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
10618a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
10628a1b9b6aSSam Leffler #else
10638a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
10648a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
10658a1b9b6aSSam Leffler #endif
10668a1b9b6aSSam Leffler {
10678a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
10688a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10698a1b9b6aSSam Leffler 
10708a1b9b6aSSam Leffler 	/* XXX check ic_bss first in station mode */
10718a1b9b6aSSam Leffler 	/* XXX 4-address frames? */
107268e8e04eSSam Leffler 	nt = &ic->ic_sta;
10738a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
107468e8e04eSSam Leffler 	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
10758a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr1);
10768a1b9b6aSSam Leffler 	else
10778a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, wh->i_addr2);
1078c1225b52SSam Leffler 	if (ni == NULL)
1079c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
10808a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10818a1b9b6aSSam Leffler 
1082c1225b52SSam Leffler 	return ni;
1083c1225b52SSam Leffler }
1084c1225b52SSam Leffler 
1085c1225b52SSam Leffler /*
1086c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1087c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1088c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1089c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1090c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1091c1225b52SSam Leffler  * key assigned to it.
1092c1225b52SSam Leffler  */
1093c1225b52SSam Leffler struct ieee80211_node *
1094c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1095c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1096c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1097c1225b52SSam Leffler 	const char *func, int line)
1098c1225b52SSam Leffler #else
1099c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1100c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1101c1225b52SSam Leffler #endif
1102c1225b52SSam Leffler {
1103c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1104c1225b52SSam Leffler 	struct ieee80211_node *ni;
1105c1225b52SSam Leffler 
1106c1225b52SSam Leffler 	nt = &ic->ic_sta;
1107c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1108c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1109c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1110c1225b52SSam Leffler 	else
1111c1225b52SSam Leffler 		ni = NULL;
1112c1225b52SSam Leffler 	if (ni == NULL) {
111368e8e04eSSam Leffler 		if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1114c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr1);
1115c1225b52SSam Leffler 		else
1116c1225b52SSam Leffler 			ni = _ieee80211_find_node(nt, wh->i_addr2);
1117c1225b52SSam Leffler 		if (ni == NULL)
1118c1225b52SSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1119c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL) {
1120c1225b52SSam Leffler 			/*
1121c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1122c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1123c1225b52SSam Leffler 			 */
1124c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1125c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1126c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1127c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1128c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1129c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1130c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1131c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1132c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1133c1225b52SSam Leffler 			}
1134c1225b52SSam Leffler 		}
113568e8e04eSSam Leffler 	} else
1136c1225b52SSam Leffler 		ieee80211_ref_node(ni);
1137c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1138c1225b52SSam Leffler 
1139c1225b52SSam Leffler 	return ni;
1140c1225b52SSam Leffler }
114168e8e04eSSam Leffler #undef IS_BAR
11428a1b9b6aSSam Leffler #undef IS_PSPOLL
11438a1b9b6aSSam Leffler #undef IS_CTL
11448a1b9b6aSSam Leffler 
11458a1b9b6aSSam Leffler /*
1146750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1147750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1148750d6d0cSSam Leffler  */
1149750d6d0cSSam Leffler struct ieee80211_node *
11508a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
115168e8e04eSSam Leffler ieee80211_find_txnode_debug(struct ieee80211com *ic, const uint8_t *macaddr,
11528a1b9b6aSSam Leffler 	const char *func, int line)
11538a1b9b6aSSam Leffler #else
115468e8e04eSSam Leffler ieee80211_find_txnode(struct ieee80211com *ic, const uint8_t *macaddr)
11558a1b9b6aSSam Leffler #endif
1156750d6d0cSSam Leffler {
1157acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1158750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1159750d6d0cSSam Leffler 
1160750d6d0cSSam Leffler 	/*
1161750d6d0cSSam Leffler 	 * The destination address should be in the node table
1162c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1163c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1164c1225b52SSam Leffler 	 * to the bss node.
1165750d6d0cSSam Leffler 	 */
1166750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
11678a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1168c1225b52SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1169c1225b52SSam Leffler 		ni = ieee80211_ref_node(ic->ic_bss);
1170ee25b8dfSSam Leffler 	else {
11718a1b9b6aSSam Leffler 		ni = _ieee80211_find_node(nt, macaddr);
1172ee25b8dfSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1173ee25b8dfSSam Leffler 		    (ni != NULL && ni->ni_associd == 0)) {
1174ee25b8dfSSam Leffler 			/*
1175ee25b8dfSSam Leffler 			 * Station is not associated; don't permit the
1176ee25b8dfSSam Leffler 			 * data frame to be sent by returning NULL.  This
1177ee25b8dfSSam Leffler 			 * is kinda a kludge but the least intrusive way
1178ee25b8dfSSam Leffler 			 * to add this check into all drivers.
1179ee25b8dfSSam Leffler 			 */
1180ee25b8dfSSam Leffler 			ieee80211_unref_node(&ni);	/* NB: null's ni */
1181ee25b8dfSSam Leffler 		}
1182ee25b8dfSSam Leffler 	}
11838a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11848a1b9b6aSSam Leffler 
11858a1b9b6aSSam Leffler 	if (ni == NULL) {
11868a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
118790d0d036SSam Leffler 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
118890d0d036SSam Leffler 			/*
118990d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
119090d0d036SSam Leffler 			 * Note that we need an additional reference for the
119190d0d036SSam Leffler 			 * caller to be consistent with _ieee80211_find_node.
119290d0d036SSam Leffler 			 */
11938a1b9b6aSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
119490d0d036SSam Leffler 			if (ni != NULL)
119590d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
119690d0d036SSam Leffler 		} else {
11978a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
11988a1b9b6aSSam Leffler 				"[%s] no node, discard frame (%s)\n",
11998a1b9b6aSSam Leffler 				ether_sprintf(macaddr), __func__);
12008a1b9b6aSSam Leffler 			ic->ic_stats.is_tx_nonode++;
1201750d6d0cSSam Leffler 		}
1202750d6d0cSSam Leffler 	}
1203750d6d0cSSam Leffler 	return ni;
1204750d6d0cSSam Leffler }
1205750d6d0cSSam Leffler 
1206750d6d0cSSam Leffler /*
12078a1b9b6aSSam Leffler  * Like find but search based on the ssid too.
12088a1b9b6aSSam Leffler  */
12098a1b9b6aSSam Leffler struct ieee80211_node *
12108a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12118a1b9b6aSSam Leffler ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
121268e8e04eSSam Leffler 	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid,
12138a1b9b6aSSam Leffler 	const char *func, int line)
12148a1b9b6aSSam Leffler #else
12158a1b9b6aSSam Leffler ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
121668e8e04eSSam Leffler 	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid)
12178a1b9b6aSSam Leffler #endif
12181a1e1d21SSam Leffler {
1219f02a0bd2SSam Leffler #define	MATCH_SSID(ni, ssid, ssidlen) \
1220f02a0bd2SSam Leffler 	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
122168e8e04eSSam Leffler 	static const uint8_t zeromac[IEEE80211_ADDR_LEN];
12221a1e1d21SSam Leffler 	struct ieee80211_node *ni;
12238a1b9b6aSSam Leffler 	int hash;
12241a1e1d21SSam Leffler 
12258a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1226f02a0bd2SSam Leffler 	/*
1227f02a0bd2SSam Leffler 	 * A mac address that is all zero means match only the ssid;
1228f02a0bd2SSam Leffler 	 * otherwise we must match both.
1229f02a0bd2SSam Leffler 	 */
1230f02a0bd2SSam Leffler 	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1231f02a0bd2SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1232f02a0bd2SSam Leffler 			if (MATCH_SSID(ni, ssid, ssidlen))
1233f02a0bd2SSam Leffler 				break;
1234f02a0bd2SSam Leffler 		}
1235f02a0bd2SSam Leffler 	} else {
1236f02a0bd2SSam Leffler 		hash = IEEE80211_NODE_HASH(macaddr);
12378a1b9b6aSSam Leffler 		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
12388a1b9b6aSSam Leffler 			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1239f02a0bd2SSam Leffler 			    MATCH_SSID(ni, ssid, ssidlen))
1240f02a0bd2SSam Leffler 				break;
1241f02a0bd2SSam Leffler 		}
1242f02a0bd2SSam Leffler 	}
1243f02a0bd2SSam Leffler 	if (ni != NULL) {
12448a1b9b6aSSam Leffler 		ieee80211_ref_node(ni);	/* mark referenced */
124568e8e04eSSam Leffler 		IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1246132142c5SDiomidis Spinellis 		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
12478a1b9b6aSSam Leffler 		     ieee80211_node_refcnt(ni));
12488a1b9b6aSSam Leffler 	}
12498a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
12508a1b9b6aSSam Leffler 	return ni;
1251f02a0bd2SSam Leffler #undef MATCH_SSID
12528a1b9b6aSSam Leffler }
12538a1b9b6aSSam Leffler 
12548a1b9b6aSSam Leffler static void
12558a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
12568a1b9b6aSSam Leffler {
12578a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
12588a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12598a1b9b6aSSam Leffler 
12608a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
126149a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
126249a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
12638a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
12648a1b9b6aSSam Leffler 
12658a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
12668a1b9b6aSSam Leffler 	if (nt != NULL) {
12678a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
12688a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
12698a1b9b6aSSam Leffler 	}
12708a1b9b6aSSam Leffler 	ic->ic_node_free(ni);
12718a1b9b6aSSam Leffler }
12728a1b9b6aSSam Leffler 
12738a1b9b6aSSam Leffler void
12748a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12758a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
12768a1b9b6aSSam Leffler #else
12778a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
12788a1b9b6aSSam Leffler #endif
12798a1b9b6aSSam Leffler {
12808a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
12818a1b9b6aSSam Leffler 
12828a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
128329d368a7SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
128449a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
12858a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
12868a1b9b6aSSam Leffler #endif
1287c1225b52SSam Leffler 	if (nt != NULL) {
1288c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
12898a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
12908a1b9b6aSSam Leffler 			/*
1291c1225b52SSam Leffler 			 * Last reference, reclaim state.
12928a1b9b6aSSam Leffler 			 */
12938a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1294c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1295c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1296c1225b52SSam Leffler 			ieee80211_keyix keyix;
1297c1225b52SSam Leffler 			/*
1298c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1299c1225b52SSam Leffler 			 */
1300c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1301c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1302c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1303c1225b52SSam Leffler 				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1304c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1305c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1306c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1307c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
13088a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
13098a1b9b6aSSam Leffler 			}
13101a1e1d21SSam Leffler 		}
1311c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1312c1225b52SSam Leffler 	} else {
1313c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1314c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1315c1225b52SSam Leffler 	}
1316c1225b52SSam Leffler }
1317c1225b52SSam Leffler 
1318c1225b52SSam Leffler /*
1319c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1320c1225b52SSam Leffler  */
1321c1225b52SSam Leffler int
1322c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1323c1225b52SSam Leffler {
1324c1225b52SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1325c1225b52SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1326c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1327c1225b52SSam Leffler 	ieee80211_keyix keyix;
1328c1225b52SSam Leffler 	int isowned, status;
1329c1225b52SSam Leffler 
1330c1225b52SSam Leffler 	/*
1331c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1332c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1333c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1334c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1335c1225b52SSam Leffler 	 *
1336c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1337c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1338c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1339c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1340c1225b52SSam Leffler 	 * conditionally.
1341c1225b52SSam Leffler 	 */
1342c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1343c1225b52SSam Leffler 	if (!isowned)
1344c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
1345c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1346c1225b52SSam Leffler 	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1347c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1348c1225b52SSam Leffler 		nikey = nt->nt_keyixmap[keyix];
1349c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;;
1350c1225b52SSam Leffler 	} else
1351c1225b52SSam Leffler 		nikey = NULL;
1352c1225b52SSam Leffler 	if (!isowned)
1353c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1354c1225b52SSam Leffler 
1355c1225b52SSam Leffler 	if (nikey != NULL) {
1356c1225b52SSam Leffler 		KASSERT(nikey == ni,
1357c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
1358c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1359c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1360c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1361c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1362c1225b52SSam Leffler 		ieee80211_free_node(ni);
1363c1225b52SSam Leffler 	}
1364c1225b52SSam Leffler 	return status;
1365c1225b52SSam Leffler }
13661a1e1d21SSam Leffler 
1367303ebc3cSSam Leffler /*
13688a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
13698a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
13708a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
13718a1b9b6aSSam Leffler  */
13728a1b9b6aSSam Leffler static void
13738a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
13748a1b9b6aSSam Leffler {
1375c1225b52SSam Leffler 	ieee80211_keyix keyix;
1376c1225b52SSam Leffler 
1377c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
13788a1b9b6aSSam Leffler 
137949a15236SSam Leffler 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
138049a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
138149a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
138249a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1383c1225b52SSam Leffler 	/*
1384c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1385c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1386c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1387c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1388c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1389c1225b52SSam Leffler 	 */
1390c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1391c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1392c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1393c1225b52SSam Leffler 		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1394c1225b52SSam Leffler 			"%s: %p<%s> clear key map entry\n",
1395c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr));
1396c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1397c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1398c1225b52SSam Leffler 	}
13998a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
14008a1b9b6aSSam Leffler 		/*
14018a1b9b6aSSam Leffler 		 * Other references are present, just remove the
14028a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
14038a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1404acc4f7f5SSam Leffler 		 * reclaimed.
14058a1b9b6aSSam Leffler 		 */
14068a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
14078a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
14088a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
14098a1b9b6aSSam Leffler 	} else
14108a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
14118a1b9b6aSSam Leffler }
14128a1b9b6aSSam Leffler 
14138a1b9b6aSSam Leffler static void
14148a1b9b6aSSam Leffler ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
14158a1b9b6aSSam Leffler {
14168a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
14178a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
14188a1b9b6aSSam Leffler 
14198a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
14208a1b9b6aSSam Leffler 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
14218a1b9b6aSSam Leffler 
14228a1b9b6aSSam Leffler 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
14238a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
14248a1b9b6aSSam Leffler 			if (ic->ic_auth->ia_node_leave != NULL)
14258a1b9b6aSSam Leffler 				ic->ic_auth->ia_node_leave(ic, ni);
14268a1b9b6aSSam Leffler 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
14278a1b9b6aSSam Leffler 		}
14288a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
14298a1b9b6aSSam Leffler 	}
14308a1b9b6aSSam Leffler }
14318a1b9b6aSSam Leffler 
14328a1b9b6aSSam Leffler /*
14338a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
14348a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
14358a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
14368a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
14378a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
14388a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1439303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1440303ebc3cSSam Leffler  * process each node only once.
1441303ebc3cSSam Leffler  */
14428a1b9b6aSSam Leffler static void
14438a1b9b6aSSam Leffler ieee80211_timeout_stations(struct ieee80211_node_table *nt)
14441a1e1d21SSam Leffler {
14458a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1446303ebc3cSSam Leffler 	struct ieee80211_node *ni;
14478a1b9b6aSSam Leffler 	u_int gen;
1448f66d97f6SSam Leffler 	int isadhoc;
14491a1e1d21SSam Leffler 
1450f66d97f6SSam Leffler 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1451f66d97f6SSam Leffler 		   ic->ic_opmode == IEEE80211_M_AHDEMO);
14528a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
1453a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
1454303ebc3cSSam Leffler restart:
14558a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
14568a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1457303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1458303ebc3cSSam Leffler 			continue;
1459303ebc3cSSam Leffler 		ni->ni_scangen = gen;
14600a915fadSSam Leffler 		/*
1461ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1462ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1463ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1464ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1465ebdda46cSSam Leffler 		 */
146668e8e04eSSam Leffler 		if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
146768e8e04eSSam Leffler 		     ic->ic_opmode == IEEE80211_M_STA) &&
146844b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1469ebdda46cSSam Leffler 			continue;
1470ebdda46cSSam Leffler 		/*
14718a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
14728a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
14738a1b9b6aSSam Leffler 		 * XXX doesn't belong here
14740a915fadSSam Leffler 		 */
14758a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
14768a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
14778a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
14788a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
14798a1b9b6aSSam Leffler 		}
1480c066143cSSam Leffler 		if (ni->ni_inact > 0)
1481c066143cSSam Leffler 			ni->ni_inact--;
1482ce647032SSam Leffler 		/*
1483ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1484ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1485ce647032SSam Leffler 		 */
1486ce647032SSam Leffler 		if (ni == ic->ic_bss)
1487ce647032SSam Leffler 			continue;
1488f66d97f6SSam Leffler 		if (ni->ni_associd != 0 || isadhoc) {
14898a1b9b6aSSam Leffler 			/*
149068e8e04eSSam Leffler 			 * Age frames on the power save queue.
14918a1b9b6aSSam Leffler 			 */
149268e8e04eSSam Leffler 			if (ieee80211_node_saveq_age(ni) != 0 &&
149368e8e04eSSam Leffler 			    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
149468e8e04eSSam Leffler 			    ic->ic_set_tim != NULL)
1495edfa57d0SSam Leffler 				ic->ic_set_tim(ni, 0);
14968a1b9b6aSSam Leffler 			/*
14978a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
14988a1b9b6aSSam Leffler 			 * send a null data frame which may not be
14998a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
15008a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
150168e8e04eSSam Leffler 			 *
150268e8e04eSSam Leffler 			 * XXX don't probe the station unless we've
150368e8e04eSSam Leffler 			 *     received a frame from them (and have
150468e8e04eSSam Leffler 			 *     some idea of the rates they are capable
150568e8e04eSSam Leffler 			 *     of); this will get fixed more properly
150668e8e04eSSam Leffler 			 *     soon with better handling of the rate set.
15078a1b9b6aSSam Leffler 			 */
1508c066143cSSam Leffler 			if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1509c066143cSSam Leffler 			    (0 < ni->ni_inact &&
1510c066143cSSam Leffler 			     ni->ni_inact <= ic->ic_inact_probe) &&
151168e8e04eSSam Leffler 			    ni->ni_rates.rs_nrates != 0) {
1512f66d97f6SSam Leffler 				IEEE80211_NOTE(ic,
1513f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1514f66d97f6SSam Leffler 				    ni, "%s",
1515f66d97f6SSam Leffler 				    "probe station due to inactivity");
151619ad2dd7SSam Leffler 				/*
151719ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
151819ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
151919ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
152019ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
152119ad2dd7SSam Leffler 				 * ref for us as needed.
152219ad2dd7SSam Leffler 				 */
152319ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
15248a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1525f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
15268a1b9b6aSSam Leffler 				/* XXX stat? */
15278a1b9b6aSSam Leffler 				goto restart;
15288a1b9b6aSSam Leffler 			}
15298a1b9b6aSSam Leffler 		}
1530c066143cSSam Leffler 		if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1531c066143cSSam Leffler 		    ni->ni_inact <= 0) {
1532f66d97f6SSam Leffler 			IEEE80211_NOTE(ic,
1533f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1534f66d97f6SSam Leffler 			    "station timed out due to inactivity "
1535f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
15368a1b9b6aSSam Leffler 			/*
15378a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
15388a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
15398a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
15408a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
15418a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
15428a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
15438a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
15448a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
15458a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
15468a1b9b6aSSam Leffler 			 *
15478a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
154868e8e04eSSam Leffler 			 * in case the driver takes a lock, as this can result
154968e8e04eSSam Leffler 			 * in a LOR between the node lock and the driver lock.
15508a1b9b6aSSam Leffler 			 */
15518a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
15528a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
15531a1e1d21SSam Leffler 				IEEE80211_SEND_MGMT(ic, ni,
15541a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
15551a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
15568a1b9b6aSSam Leffler 			}
15578a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
15581be50176SSam Leffler 			ic->ic_stats.is_node_timeout++;
1559303ebc3cSSam Leffler 			goto restart;
1560303ebc3cSSam Leffler 		}
15611a1e1d21SSam Leffler 	}
15628a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15638a1b9b6aSSam Leffler 
15648a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
156568e8e04eSSam Leffler }
15668a1b9b6aSSam Leffler 
156768e8e04eSSam Leffler void
156868e8e04eSSam Leffler ieee80211_node_timeout(void *arg)
156968e8e04eSSam Leffler {
157068e8e04eSSam Leffler 	struct ieee80211com *ic = arg;
157168e8e04eSSam Leffler 
157268e8e04eSSam Leffler 	ieee80211_scan_timeout(ic);
157368e8e04eSSam Leffler 	ieee80211_timeout_stations(&ic->ic_sta);
157468e8e04eSSam Leffler 
1575b105a069SSam Leffler 	IEEE80211_LOCK(ic);
1576b105a069SSam Leffler 	ieee80211_erp_timeout(ic);
1577b105a069SSam Leffler 	IEEE80211_UNLOCK(ic);
1578b105a069SSam Leffler 
157968e8e04eSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
158068e8e04eSSam Leffler 		ieee80211_node_timeout, ic);
15811a1e1d21SSam Leffler }
15821a1e1d21SSam Leffler 
15831a1e1d21SSam Leffler void
15848a1b9b6aSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
15851a1e1d21SSam Leffler {
15861a1e1d21SSam Leffler 	struct ieee80211_node *ni;
15878a1b9b6aSSam Leffler 	u_int gen;
15881a1e1d21SSam Leffler 
15898a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK(nt);
1590a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
15918a1b9b6aSSam Leffler restart:
15928a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
15938a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
15948a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
15958a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
15968a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
15978a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
15981a1e1d21SSam Leffler 			(*f)(arg, ni);
15998a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
16008a1b9b6aSSam Leffler 			goto restart;
16018a1b9b6aSSam Leffler 		}
16028a1b9b6aSSam Leffler 	}
16038a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
16048a1b9b6aSSam Leffler 
16058a1b9b6aSSam Leffler 	IEEE80211_SCAN_UNLOCK(nt);
16068a1b9b6aSSam Leffler }
16078a1b9b6aSSam Leffler 
16088a1b9b6aSSam Leffler void
16098a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
16108a1b9b6aSSam Leffler {
16118a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
16128a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
16138a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
16148a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
16158a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
16168a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
16178a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1618801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
1619801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
1620801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
16218a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
162268e8e04eSSam Leffler 	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
162368e8e04eSSam Leffler 		ni->ni_rstamp, ni->ni_rssi, ni->ni_noise,
162468e8e04eSSam Leffler 		ni->ni_intval, ni->ni_capinfo);
16258a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
16268a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
16278a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
16288a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
16298a1b9b6aSSam Leffler 	printf("\tfails %u inact %u txrate %u\n",
16308a1b9b6aSSam Leffler 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
163168e8e04eSSam Leffler 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
163268e8e04eSSam Leffler 		ni->ni_htcap, ni->ni_htparam,
163368e8e04eSSam Leffler 		ni->ni_htctlchan, ni->ni_ht2ndchan);
163468e8e04eSSam Leffler 	printf("\thtopmode %x htstbc %x chw %u\n",
163568e8e04eSSam Leffler 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
16368a1b9b6aSSam Leffler }
16378a1b9b6aSSam Leffler 
16388a1b9b6aSSam Leffler void
16398a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
16408a1b9b6aSSam Leffler {
16418a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
16428a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
16438a1b9b6aSSam Leffler }
16448a1b9b6aSSam Leffler 
1645b105a069SSam Leffler void
1646b105a069SSam Leffler ieee80211_notify_erp(struct ieee80211com *ic)
1647b105a069SSam Leffler {
1648b105a069SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1649b105a069SSam Leffler 		ieee80211_beacon_notify(ic, IEEE80211_BEACON_ERP);
1650b105a069SSam Leffler }
1651b105a069SSam Leffler 
16528a1b9b6aSSam Leffler /*
16538a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
16548a1b9b6aSSam Leffler  */
16558a1b9b6aSSam Leffler static void
16568a1b9b6aSSam Leffler ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
16578a1b9b6aSSam Leffler {
16588a1b9b6aSSam Leffler 
16598a1b9b6aSSam Leffler 	/*
16608a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
16618a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
16628a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
16638a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
16648a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
16658a1b9b6aSSam Leffler 	 */
16668a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
16678a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
1668b105a069SSam Leffler 		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1669b105a069SSam Leffler 		    "station needs long slot time, count %d",
1670b105a069SSam Leffler 		    ic->ic_longslotsta);
16718a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
167268e8e04eSSam Leffler 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
167368e8e04eSSam Leffler 			/*
167468e8e04eSSam Leffler 			 * Don't force slot time when switched to turbo
167568e8e04eSSam Leffler 			 * mode as non-ERP stations won't be present; this
167668e8e04eSSam Leffler 			 * need only be done when on the normal G channel.
167768e8e04eSSam Leffler 			 */
16788a1b9b6aSSam Leffler 			ieee80211_set_shortslottime(ic, 0);
16798a1b9b6aSSam Leffler 		}
168068e8e04eSSam Leffler 	}
16818a1b9b6aSSam Leffler 	/*
16828a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
16838a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
16848a1b9b6aSSam Leffler 	 * if configured.
16858a1b9b6aSSam Leffler 	 */
16868a1b9b6aSSam Leffler 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
16878a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
1688b105a069SSam Leffler 		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1689b105a069SSam Leffler 		    "station is !ERP, %d non-ERP stations associated",
1690b105a069SSam Leffler 		    ic->ic_nonerpsta);
16918a1b9b6aSSam Leffler 		/*
16928a1b9b6aSSam Leffler 		 * If station does not support short preamble
16938a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
16948a1b9b6aSSam Leffler 		 */
16958a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1696b105a069SSam Leffler 			IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1697b105a069SSam Leffler 			    "%s", "station needs long preamble");
16988a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
16998a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
17008a1b9b6aSSam Leffler 		}
1701b105a069SSam Leffler 		/*
1702b105a069SSam Leffler 		 * If protection is configured, enable it.
1703b105a069SSam Leffler 		 */
1704b105a069SSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1705b105a069SSam Leffler 		    ic->ic_nonerpsta == 1 &&
1706b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
1707b105a069SSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1708b105a069SSam Leffler 			    "%s: enable use of protection\n", __func__);
1709b105a069SSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
1710b105a069SSam Leffler 			ieee80211_notify_erp(ic);
1711b105a069SSam Leffler 		}
17128a1b9b6aSSam Leffler 	} else
17138a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
17148a1b9b6aSSam Leffler }
17158a1b9b6aSSam Leffler 
17168a1b9b6aSSam Leffler void
17178a1b9b6aSSam Leffler ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
17188a1b9b6aSSam Leffler {
17198a1b9b6aSSam Leffler 	int newassoc;
17208a1b9b6aSSam Leffler 
17218a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
172268e8e04eSSam Leffler 		uint16_t aid;
17238a1b9b6aSSam Leffler 
17248a1b9b6aSSam Leffler 		/*
17258a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
17268a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
17278a1b9b6aSSam Leffler 		 */
17288a1b9b6aSSam Leffler 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
17298a1b9b6aSSam Leffler 			if (!IEEE80211_AID_ISSET(aid,
17308a1b9b6aSSam Leffler 			    ic->ic_aid_bitmap))
17318a1b9b6aSSam Leffler 				break;
17328a1b9b6aSSam Leffler 		}
17338a1b9b6aSSam Leffler 		if (aid >= ic->ic_max_aid) {
17348a1b9b6aSSam Leffler 			IEEE80211_SEND_MGMT(ic, ni, resp,
17358a1b9b6aSSam Leffler 			    IEEE80211_REASON_ASSOC_TOOMANY);
17368a1b9b6aSSam Leffler 			ieee80211_node_leave(ic, ni);
17378a1b9b6aSSam Leffler 			return;
17388a1b9b6aSSam Leffler 		}
17398a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
17408a1b9b6aSSam Leffler 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
17418a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
17428a1b9b6aSSam Leffler 		newassoc = 1;
174368e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
174468e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
17458a1b9b6aSSam Leffler 			ieee80211_node_join_11g(ic, ni);
17468a1b9b6aSSam Leffler 	} else
17478a1b9b6aSSam Leffler 		newassoc = 0;
17488a1b9b6aSSam Leffler 
1749b105a069SSam Leffler 	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1750b105a069SSam Leffler 	    "station %sassociated at aid %d: %s preamble, %s slot time%s%s%s%s%s",
1751b105a069SSam Leffler 	    newassoc ? "" : "re",
1752b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
1753b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1754b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1755b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
175668e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
175768e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_HT ?
175868e8e04eSSam Leffler 		(ni->ni_chw == 20 ? ", HT20" : ", HT40") : "",
175968e8e04eSSam Leffler 	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_FF) ?
176068e8e04eSSam Leffler 		", fast-frames" : "",
176168e8e04eSSam Leffler 	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_TURBOP) ?
176268e8e04eSSam Leffler 		", turbo" : ""
1763b8fcf546SSam Leffler 	);
17648a1b9b6aSSam Leffler 
17658a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
1766736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
1767e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
17688a1b9b6aSSam Leffler 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
17698a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
17708a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_join != NULL)
17718a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_join(ic, ni);
17728a1b9b6aSSam Leffler 	ieee80211_notify_node_join(ic, ni, newassoc);
17738a1b9b6aSSam Leffler }
17748a1b9b6aSSam Leffler 
1775b105a069SSam Leffler static void
1776b105a069SSam Leffler disable_protection(struct ieee80211com *ic)
1777b105a069SSam Leffler {
1778b105a069SSam Leffler 	KASSERT(ic->ic_nonerpsta == 0 &&
1779b105a069SSam Leffler 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
1780b105a069SSam Leffler 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
1781b105a069SSam Leffler 	   ic->ic_flags_ext));
1782b105a069SSam Leffler 
1783b105a069SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
1784b105a069SSam Leffler 	/* XXX verify mode? */
1785b105a069SSam Leffler 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1786b105a069SSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1787b105a069SSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1788b105a069SSam Leffler 	}
1789b105a069SSam Leffler 	ieee80211_notify_erp(ic);
1790b105a069SSam Leffler }
1791b105a069SSam Leffler 
17928a1b9b6aSSam Leffler /*
17938a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
17948a1b9b6aSSam Leffler  */
17958a1b9b6aSSam Leffler static void
17968a1b9b6aSSam Leffler ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
17978a1b9b6aSSam Leffler {
17988a1b9b6aSSam Leffler 
179968e8e04eSSam Leffler 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
180068e8e04eSSam Leffler 	     ("not in 11g, bss %u:0x%x, curmode %u", ic->ic_bsschan->ic_freq,
180168e8e04eSSam Leffler 	      ic->ic_bsschan->ic_flags, ic->ic_curmode));
18028a1b9b6aSSam Leffler 
18038a1b9b6aSSam Leffler 	/*
18048a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
18058a1b9b6aSSam Leffler 	 */
18068a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
18078a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
18088a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
18098a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
1810b105a069SSam Leffler 		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1811b105a069SSam Leffler 		    "long slot time station leaves, count now %d",
1812b105a069SSam Leffler 		    ic->ic_longslotsta);
18138a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
18148a1b9b6aSSam Leffler 			/*
18158a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
18168a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
18178a1b9b6aSSam Leffler 			 */
18188a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
18198a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
18208a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18218a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
18228a1b9b6aSSam Leffler 				    __func__);
18238a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
18248a1b9b6aSSam Leffler 			}
18258a1b9b6aSSam Leffler 		}
18268a1b9b6aSSam Leffler 	}
18278a1b9b6aSSam Leffler 	/*
18288a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
18298a1b9b6aSSam Leffler 	 */
18308a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
18318a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
18328a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
18338a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
1834b105a069SSam Leffler 		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1835b105a069SSam Leffler 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
1836b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
1837b105a069SSam Leffler 			" (non-ERP sta present)" : "");
1838b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0 &&
1839b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
18408a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
18418a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
1842b105a069SSam Leffler 			disable_protection(ic);
1843b105a069SSam Leffler 		}
1844b105a069SSam Leffler 	}
1845b105a069SSam Leffler }
1846b105a069SSam Leffler 
1847b105a069SSam Leffler /*
1848b105a069SSam Leffler  * Time out presence of an overlapping bss with non-ERP
1849b105a069SSam Leffler  * stations.  When operating in hostap mode we listen for
1850b105a069SSam Leffler  * beacons from other stations and if we identify a non-ERP
1851b105a069SSam Leffler  * station is present we enable protection.  To identify
1852b105a069SSam Leffler  * when all non-ERP stations are gone we time out this
1853b105a069SSam Leffler  * condition.
1854b105a069SSam Leffler  */
1855b105a069SSam Leffler static void
1856b105a069SSam Leffler ieee80211_erp_timeout(struct ieee80211com *ic)
1857b105a069SSam Leffler {
1858b105a069SSam Leffler 
1859b105a069SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
1860b105a069SSam Leffler 
1861b105a069SSam Leffler 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
1862b105a069SSam Leffler 	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
18638a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1864b105a069SSam Leffler 		    "%s\n", "age out non-ERP sta present on channel");
1865b105a069SSam Leffler 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
1866b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0)
1867b105a069SSam Leffler 			disable_protection(ic);
18688a1b9b6aSSam Leffler 	}
18698a1b9b6aSSam Leffler }
18708a1b9b6aSSam Leffler 
18718a1b9b6aSSam Leffler /*
18728a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
18738a1b9b6aSSam Leffler  * when operating as an ap.
18748a1b9b6aSSam Leffler  */
18758a1b9b6aSSam Leffler void
18768a1b9b6aSSam Leffler ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
18778a1b9b6aSSam Leffler {
187844acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
18798a1b9b6aSSam Leffler 
1880b105a069SSam Leffler 	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1881b105a069SSam Leffler 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
18828a1b9b6aSSam Leffler 
188368e8e04eSSam Leffler 	KASSERT(ic->ic_opmode != IEEE80211_M_STA,
18848a1b9b6aSSam Leffler 		("unexpected operating mode %u", ic->ic_opmode));
18858a1b9b6aSSam Leffler 	/*
18868a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
18878a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
18888a1b9b6aSSam Leffler 	 */
18898a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
18908a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
18918a1b9b6aSSam Leffler 		goto done;
18928a1b9b6aSSam Leffler 	/*
18938a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
18948a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
18958a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
18968a1b9b6aSSam Leffler 	 * associd to locate it's state block.
18978a1b9b6aSSam Leffler 	 */
18988a1b9b6aSSam Leffler 	if (ic->ic_auth->ia_node_leave != NULL)
18998a1b9b6aSSam Leffler 		ic->ic_auth->ia_node_leave(ic, ni);
19008a1b9b6aSSam Leffler 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
19018a1b9b6aSSam Leffler 	ni->ni_associd = 0;
19028a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
19038a1b9b6aSSam Leffler 
190468e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
190568e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
19068a1b9b6aSSam Leffler 		ieee80211_node_leave_11g(ic, ni);
19078a1b9b6aSSam Leffler 	/*
19088a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
19098a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
19108a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
19118a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
19128a1b9b6aSSam Leffler 	 */
19138a1b9b6aSSam Leffler 	ieee80211_sta_leave(ic, ni);
19148a1b9b6aSSam Leffler done:
191544acc00dSSam Leffler 	/*
191644acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
191744acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
191844acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
191944acc00dSSam Leffler 	 * for inactivity.
192044acc00dSSam Leffler 	 */
192144acc00dSSam Leffler 	if (nt != NULL) {
192244acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
192344acc00dSSam Leffler 		node_reclaim(nt, ni);
192444acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
192544acc00dSSam Leffler 	} else
19268a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
19278a1b9b6aSSam Leffler }
19288a1b9b6aSSam Leffler 
192968e8e04eSSam Leffler int8_t
19308a1b9b6aSSam Leffler ieee80211_getrssi(struct ieee80211com *ic)
19318a1b9b6aSSam Leffler {
19328a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
1933acc4f7f5SSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
193468e8e04eSSam Leffler 	int rssi_samples;
193568e8e04eSSam Leffler 	int32_t rssi_total;
19368a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
19378a1b9b6aSSam Leffler 
19388a1b9b6aSSam Leffler 	rssi_total = 0;
19398a1b9b6aSSam Leffler 	rssi_samples = 0;
19408a1b9b6aSSam Leffler 	switch (ic->ic_opmode) {
19418a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
19428a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
19438a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
19448a1b9b6aSSam Leffler 		/* XXX locking */
1945acc4f7f5SSam Leffler 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
194668e8e04eSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
194768e8e04eSSam Leffler 			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS)) {
194868e8e04eSSam Leffler 				int8_t rssi = ic->ic_node_getrssi(ni);
194968e8e04eSSam Leffler 				if (rssi != 0) {
19508a1b9b6aSSam Leffler 					rssi_samples++;
195168e8e04eSSam Leffler 					rssi_total += rssi;
195268e8e04eSSam Leffler 				}
19538a1b9b6aSSam Leffler 			}
19548a1b9b6aSSam Leffler 		break;
19558a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
19568a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
19578a1b9b6aSSam Leffler 	default:
19588a1b9b6aSSam Leffler 		if (ic->ic_bss != NULL)
19598a1b9b6aSSam Leffler 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
19608a1b9b6aSSam Leffler 		rssi_samples = 1;
19618a1b9b6aSSam Leffler 		break;
19628a1b9b6aSSam Leffler 	}
19638a1b9b6aSSam Leffler 	return rssi_total / NZ(rssi_samples);
19648a1b9b6aSSam Leffler #undef NZ
19658a1b9b6aSSam Leffler }
19668a1b9b6aSSam Leffler 
196768e8e04eSSam Leffler void
196868e8e04eSSam Leffler ieee80211_getsignal(struct ieee80211com *ic, int8_t *rssi, int8_t *noise)
19698a1b9b6aSSam Leffler {
19708a1b9b6aSSam Leffler 
197168e8e04eSSam Leffler 	if (ic->ic_bss == NULL)		/* NB: shouldn't happen */
197268e8e04eSSam Leffler 		return;
197368e8e04eSSam Leffler 	ic->ic_node_getsignal(ic->ic_bss, rssi, noise);
197468e8e04eSSam Leffler 	/* for non-station mode return avg'd rssi accounting */
197568e8e04eSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
197668e8e04eSSam Leffler 		*rssi = ieee80211_getrssi(ic);
19778a1b9b6aSSam Leffler }
19788a1b9b6aSSam Leffler 
19798a1b9b6aSSam Leffler /*
19808a1b9b6aSSam Leffler  * Node table support.
19818a1b9b6aSSam Leffler  */
19828a1b9b6aSSam Leffler 
19838a1b9b6aSSam Leffler static void
19848a1b9b6aSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
19858a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt,
198668e8e04eSSam Leffler 	const char *name, int inact, int keyixmax)
19878a1b9b6aSSam Leffler {
19888a1b9b6aSSam Leffler 
19898a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
19908a1b9b6aSSam Leffler 		"%s %s table, inact %u\n", __func__, name, inact);
19918a1b9b6aSSam Leffler 
19928a1b9b6aSSam Leffler 	nt->nt_ic = ic;
19938a1b9b6aSSam Leffler 	/* XXX need unit */
19948a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
19958a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
19968a1b9b6aSSam Leffler 	TAILQ_INIT(&nt->nt_node);
19978a1b9b6aSSam Leffler 	nt->nt_name = name;
19988a1b9b6aSSam Leffler 	nt->nt_scangen = 1;
19998a1b9b6aSSam Leffler 	nt->nt_inact_init = inact;
2000c1225b52SSam Leffler 	nt->nt_keyixmax = keyixmax;
2001c1225b52SSam Leffler 	if (nt->nt_keyixmax > 0) {
2002c1225b52SSam Leffler 		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2003c1225b52SSam Leffler 			keyixmax * sizeof(struct ieee80211_node *),
2004c1225b52SSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
2005c1225b52SSam Leffler 		if (nt->nt_keyixmap == NULL)
2006c1225b52SSam Leffler 			if_printf(ic->ic_ifp,
2007c1225b52SSam Leffler 			    "Cannot allocate key index map with %u entries\n",
2008c1225b52SSam Leffler 			    keyixmax);
2009c1225b52SSam Leffler 	} else
2010c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
20118a1b9b6aSSam Leffler }
20128a1b9b6aSSam Leffler 
201368e8e04eSSam Leffler static void
20148a1b9b6aSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt)
20158a1b9b6aSSam Leffler {
20168a1b9b6aSSam Leffler 
20178a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
20188a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
20198a1b9b6aSSam Leffler 
20208a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
20218a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
20228a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
20238a1b9b6aSSam Leffler }
20248a1b9b6aSSam Leffler 
20258a1b9b6aSSam Leffler static void
20268a1b9b6aSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
20278a1b9b6aSSam Leffler {
20288a1b9b6aSSam Leffler 
20298a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
20308a1b9b6aSSam Leffler 		"%s %s table\n", __func__, nt->nt_name);
20318a1b9b6aSSam Leffler 
2032c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
20338a1b9b6aSSam Leffler 	ieee80211_free_allnodes_locked(nt);
2034c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL) {
2035c1225b52SSam Leffler 		/* XXX verify all entries are NULL */
2036c1225b52SSam Leffler 		int i;
2037c1225b52SSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
2038c1225b52SSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
2039c1225b52SSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
2040c1225b52SSam Leffler 					nt->nt_name, i);
2041c1225b52SSam Leffler 		FREE(nt->nt_keyixmap, M_80211_NODE);
2042c1225b52SSam Leffler 		nt->nt_keyixmap = NULL;
2043c1225b52SSam Leffler 	}
20448a1b9b6aSSam Leffler 	IEEE80211_SCAN_LOCK_DESTROY(nt);
20458a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
20468a1b9b6aSSam Leffler }
2047