xref: /freebsd/sys/net80211/ieee80211_node.c (revision 5463c4a4)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
310ad9a77SSam Leffler  * Copyright (c) 2002-2009 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 
30b032f27cSSam Leffler #include "opt_wlan.h"
31b032f27cSSam Leffler 
321a1e1d21SSam Leffler #include <sys/param.h>
331a1e1d21SSam Leffler #include <sys/systm.h>
341a1e1d21SSam Leffler #include <sys/mbuf.h>
351a1e1d21SSam Leffler #include <sys/malloc.h>
361a1e1d21SSam Leffler #include <sys/kernel.h>
371a1e1d21SSam Leffler 
388a1b9b6aSSam Leffler #include <sys/socket.h>
391a1e1d21SSam Leffler 
401a1e1d21SSam Leffler #include <net/if.h>
411a1e1d21SSam Leffler #include <net/if_media.h>
421a1e1d21SSam Leffler #include <net/ethernet.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
45b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
46616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
47616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
48616190d0SSam Leffler #endif
4910ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
5010ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
5110ad9a77SSam Leffler #endif
52b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
531a1e1d21SSam Leffler 
541a1e1d21SSam Leffler #include <net/bpf.h>
551a1e1d21SSam Leffler 
567268fa64SSam Leffler /*
577268fa64SSam Leffler  * Association id's are managed with a bit vector.
587268fa64SSam Leffler  */
59b032f27cSSam Leffler #define	IEEE80211_AID_SET(_vap, b) \
60b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
61b032f27cSSam Leffler 		(1 << (IEEE80211_AID(b) % 32)))
62b032f27cSSam Leffler #define	IEEE80211_AID_CLR(_vap, b) \
63b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
64b032f27cSSam Leffler 		~(1 << (IEEE80211_AID(b) % 32)))
65b032f27cSSam Leffler #define	IEEE80211_AID_ISSET(_vap, b) \
66b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
677268fa64SSam Leffler 
68132142c5SDiomidis Spinellis #ifdef IEEE80211_DEBUG_REFCNT
69132142c5SDiomidis Spinellis #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
70132142c5SDiomidis Spinellis #else
71132142c5SDiomidis Spinellis #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
72132142c5SDiomidis Spinellis #endif
73132142c5SDiomidis Spinellis 
7468e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *);
7568e8e04eSSam Leffler 
7638c208f8SSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211vap *,
7738c208f8SSam Leffler 	const uint8_t [IEEE80211_ADDR_LEN]);
788a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
798a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
80b032f27cSSam Leffler static void node_age(struct ieee80211_node *);
8168e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *);
8268e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
83b032f27cSSam Leffler static void node_getmimoinfo(const struct ieee80211_node *,
84b032f27cSSam Leffler 	struct ieee80211_mimo_info *);
851a1e1d21SSam Leffler 
868a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
878a1b9b6aSSam Leffler 
888a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
89c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
9068e8e04eSSam Leffler 	int inact, int keymaxix);
91b032f27cSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *,
92b032f27cSSam Leffler 	struct ieee80211vap *);
938a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
94b105a069SSam Leffler static void ieee80211_erp_timeout(struct ieee80211com *);
951a1e1d21SSam Leffler 
9632346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
97b032f27cSSam Leffler MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
9837c150c4SSam Leffler 
991a1e1d21SSam Leffler void
1008a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
1011a1e1d21SSam Leffler {
102b032f27cSSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
103b032f27cSSam Leffler 		IEEE80211_INACT_INIT, ic->ic_max_keyix);
104b032f27cSSam Leffler 	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
105b032f27cSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
106b032f27cSSam Leffler 		ieee80211_node_timeout, ic);
1071a1e1d21SSam Leffler 
1088a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
1098a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
1108a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
111b032f27cSSam Leffler 	ic->ic_node_age = node_age;
112b032f27cSSam Leffler 	ic->ic_node_drain = node_age;		/* NB: same as age */
1138a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
11468e8e04eSSam Leffler 	ic->ic_node_getsignal = node_getsignal;
115b032f27cSSam Leffler 	ic->ic_node_getmimoinfo = node_getmimoinfo;
1168a1b9b6aSSam Leffler 
117b032f27cSSam Leffler 	/*
118b032f27cSSam Leffler 	 * Set flags to be propagated to all vap's;
119b032f27cSSam Leffler 	 * these define default behaviour/configuration.
120b032f27cSSam Leffler 	 */
121c066143cSSam Leffler 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
122c1225b52SSam Leffler }
123c1225b52SSam Leffler 
124c1225b52SSam Leffler void
1258a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1261a1e1d21SSam Leffler {
1271a1e1d21SSam Leffler 
128b032f27cSSam Leffler 	callout_drain(&ic->ic_inact);
129acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
130b032f27cSSam Leffler }
131b032f27cSSam Leffler 
132b032f27cSSam Leffler void
133b032f27cSSam Leffler ieee80211_node_vattach(struct ieee80211vap *vap)
134b032f27cSSam Leffler {
135b032f27cSSam Leffler 	/* NB: driver can override */
136b032f27cSSam Leffler 	vap->iv_max_aid = IEEE80211_AID_DEF;
137b032f27cSSam Leffler 
138b032f27cSSam Leffler 	/* default station inactivity timer setings */
139b032f27cSSam Leffler 	vap->iv_inact_init = IEEE80211_INACT_INIT;
140b032f27cSSam Leffler 	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
141b032f27cSSam Leffler 	vap->iv_inact_run = IEEE80211_INACT_RUN;
142b032f27cSSam Leffler 	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
143be1054edSSam Leffler 
144be1054edSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
145be1054edSSam Leffler 	    "%s: init %u auth %u run %u probe %u\n", __func__,
146be1054edSSam Leffler 	    vap->iv_inact_init, vap->iv_inact_auth,
147be1054edSSam Leffler 	    vap->iv_inact_run, vap->iv_inact_probe);
148b032f27cSSam Leffler }
149b032f27cSSam Leffler 
150b032f27cSSam Leffler void
151b032f27cSSam Leffler ieee80211_node_latevattach(struct ieee80211vap *vap)
152b032f27cSSam Leffler {
153b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
154b032f27cSSam Leffler 		/* XXX should we allow max aid to be zero? */
155b032f27cSSam Leffler 		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
156b032f27cSSam Leffler 			vap->iv_max_aid = IEEE80211_AID_MIN;
157b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
158b032f27cSSam Leffler 			    "WARNING: max aid too small, changed to %d\n",
159b032f27cSSam Leffler 			    vap->iv_max_aid);
160b032f27cSSam Leffler 		}
161e2126decSSam Leffler 		vap->iv_aid_bitmap = (uint32_t *) malloc(
162c5abbba3SDag-Erling Smørgrav 			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
163b032f27cSSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
164b032f27cSSam Leffler 		if (vap->iv_aid_bitmap == NULL) {
165b032f27cSSam Leffler 			/* XXX no way to recover */
166b032f27cSSam Leffler 			printf("%s: no memory for AID bitmap, max aid %d!\n",
167b032f27cSSam Leffler 			    __func__, vap->iv_max_aid);
168b032f27cSSam Leffler 			vap->iv_max_aid = 0;
169b032f27cSSam Leffler 		}
170b032f27cSSam Leffler 	}
171b032f27cSSam Leffler 
172b032f27cSSam Leffler 	ieee80211_reset_bss(vap);
173b032f27cSSam Leffler 
174b032f27cSSam Leffler 	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
175b032f27cSSam Leffler }
176b032f27cSSam Leffler 
177b032f27cSSam Leffler void
178b032f27cSSam Leffler ieee80211_node_vdetach(struct ieee80211vap *vap)
179b032f27cSSam Leffler {
180b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
181b032f27cSSam Leffler 
182b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
183b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
184b032f27cSSam Leffler 		ieee80211_free_node(vap->iv_bss);
185b032f27cSSam Leffler 		vap->iv_bss = NULL;
186b032f27cSSam Leffler 	}
187b032f27cSSam Leffler 	if (vap->iv_aid_bitmap != NULL) {
188e2126decSSam Leffler 		free(vap->iv_aid_bitmap, M_80211_NODE);
189b032f27cSSam Leffler 		vap->iv_aid_bitmap = NULL;
1908a1b9b6aSSam Leffler 	}
1918a1b9b6aSSam Leffler }
1928a1b9b6aSSam Leffler 
1938a1b9b6aSSam Leffler /*
1948a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
1958a1b9b6aSSam Leffler  */
1968a1b9b6aSSam Leffler 
1978a1b9b6aSSam Leffler void
198e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
1998a1b9b6aSSam Leffler {
200be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
201be1054edSSam Leffler 
2028a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
203be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_run;
204c066143cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
205be1054edSSam Leffler 
206be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
207be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
2088a1b9b6aSSam Leffler }
2098a1b9b6aSSam Leffler 
2108a1b9b6aSSam Leffler void
211e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2128a1b9b6aSSam Leffler {
213be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
214be1054edSSam Leffler 
2158a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
216be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_auth;
217c066143cSSam Leffler 	if (ni->ni_inact > ni->ni_inact_reload)
218c066143cSSam Leffler 		ni->ni_inact = ni->ni_inact_reload;
219be1054edSSam Leffler 
220be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
221be1054edSSam Leffler 	    "%s: inact_reload %u inact %u", __func__,
222be1054edSSam Leffler 	    ni->ni_inact_reload, ni->ni_inact);
2238a1b9b6aSSam Leffler }
2248a1b9b6aSSam Leffler 
2258a1b9b6aSSam Leffler /*
22601a03542SSam Leffler  * Fix tx parameters for a node according to ``association state''.
22701a03542SSam Leffler  */
22801a03542SSam Leffler static void
22901a03542SSam Leffler node_setuptxparms(struct ieee80211_node *ni)
23001a03542SSam Leffler {
23101a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
232f76cde95SSam Leffler 	enum ieee80211_phymode mode;
23301a03542SSam Leffler 
23401a03542SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT) {
23501a03542SSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
236f76cde95SSam Leffler 			mode = IEEE80211_MODE_11NA;
23701a03542SSam Leffler 		else
238f76cde95SSam Leffler 			mode = IEEE80211_MODE_11NG;
23901a03542SSam Leffler 	} else {				/* legacy rate handling */
240f76cde95SSam Leffler 		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
241f76cde95SSam Leffler 			mode = IEEE80211_MODE_STURBO_A;
2426a76ae21SSam Leffler 		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
2436a76ae21SSam Leffler 			mode = IEEE80211_MODE_HALF;
2446a76ae21SSam Leffler 		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
2456a76ae21SSam Leffler 			mode = IEEE80211_MODE_QUARTER;
246c5262b82SSam Leffler 		/* NB: 108A should be handled as 11a */
247f76cde95SSam Leffler 		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
248f76cde95SSam Leffler 			mode = IEEE80211_MODE_11A;
249c5262b82SSam Leffler 		else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
250c5262b82SSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_ERP))
251f76cde95SSam Leffler 			mode = IEEE80211_MODE_11G;
25201a03542SSam Leffler 		else
253f76cde95SSam Leffler 			mode = IEEE80211_MODE_11B;
25401a03542SSam Leffler 	}
255f76cde95SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
25601a03542SSam Leffler }
25701a03542SSam Leffler 
25801a03542SSam Leffler /*
2598a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2608a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
261b032f27cSSam Leffler  * XXX should be private but hostap needs it to deal with CSA
2628a1b9b6aSSam Leffler  */
263b032f27cSSam Leffler void
264b032f27cSSam Leffler ieee80211_node_set_chan(struct ieee80211_node *ni,
265b032f27cSSam Leffler 	struct ieee80211_channel *chan)
2668a1b9b6aSSam Leffler {
267b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
26801a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
26901a03542SSam Leffler 	enum ieee80211_phymode mode;
27068e8e04eSSam Leffler 
271b032f27cSSam Leffler 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
272b032f27cSSam Leffler 
2738a1b9b6aSSam Leffler 	ni->ni_chan = chan;
27401a03542SSam Leffler 	mode = ieee80211_chan2mode(chan);
27568e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(chan)) {
27668e8e04eSSam Leffler 		/*
27768e8e04eSSam Leffler 		 * XXX Gotta be careful here; the rate set returned by
27868e8e04eSSam Leffler 		 * ieee80211_get_suprates is actually any HT rate
27968e8e04eSSam Leffler 		 * set so blindly copying it will be bad.  We must
28068e8e04eSSam Leffler 		 * install the legacy rate est in ni_rates and the
28168e8e04eSSam Leffler 		 * HT rate set in ni_htrates.
28268e8e04eSSam Leffler 		 */
28368e8e04eSSam Leffler 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
28401a03542SSam Leffler 		/*
28501a03542SSam Leffler 		 * Setup bss tx parameters based on operating mode.  We
28601a03542SSam Leffler 		 * use legacy rates when operating in a mixed HT+non-HT bss
28701a03542SSam Leffler 		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
28801a03542SSam Leffler 		 */
28901a03542SSam Leffler 		if (mode == IEEE80211_MODE_11NA &&
29001a03542SSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
29101a03542SSam Leffler 			mode = IEEE80211_MODE_11A;
29201a03542SSam Leffler 		else if (mode == IEEE80211_MODE_11NG &&
29301a03542SSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
29401a03542SSam Leffler 			mode = IEEE80211_MODE_11G;
29501a03542SSam Leffler 		if (mode == IEEE80211_MODE_11G &&
29601a03542SSam Leffler 		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
29701a03542SSam Leffler 			mode = IEEE80211_MODE_11B;
29868e8e04eSSam Leffler 	}
29901a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
30041b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
3011a1e1d21SSam Leffler }
3021a1e1d21SSam Leffler 
303f9cd9174SSam Leffler static __inline void
304f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
305f9cd9174SSam Leffler {
306f9cd9174SSam Leffler 	/* propagate useful state */
307f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
308f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
309f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
310f9cd9174SSam Leffler 	/* XXX statistics? */
311b032f27cSSam Leffler 	/* XXX legacy WDS bssid? */
312f9cd9174SSam Leffler }
313f9cd9174SSam Leffler 
3141a1e1d21SSam Leffler void
315b032f27cSSam Leffler ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
3161a1e1d21SSam Leffler {
317b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3181a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3198a1b9b6aSSam Leffler 
320b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
321b032f27cSSam Leffler 		"%s: creating ibss on channel %u\n", __func__,
322b032f27cSSam Leffler 		ieee80211_chan2ieee(ic, chan));
3238a1b9b6aSSam Leffler 
324b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
325acc4f7f5SSam Leffler 	if (ni == NULL) {
326acc4f7f5SSam Leffler 		/* XXX recovery? */
3278a1b9b6aSSam Leffler 		return;
3288a1b9b6aSSam Leffler 	}
329b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
330b032f27cSSam Leffler 	ni->ni_esslen = vap->iv_des_ssid[0].len;
331b032f27cSSam Leffler 	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
332b032f27cSSam Leffler 	if (vap->iv_bss != NULL)
333b032f27cSSam Leffler 		copy_bss(ni, vap->iv_bss);
334d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
335b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
3361a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3371a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3381a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3391a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3401a1e1d21SSam Leffler 	}
341b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
342b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_SIBSS;
3438a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
344b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
345b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
346fe49f061SSam Leffler 		else {
347fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
348fe49f061SSam Leffler 			/* clear group bit, add local bit */
349fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
350fe49f061SSam Leffler 		}
351b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
352b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
353b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
35450d8b493SSam Leffler 		else
35510ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
35610ad9a77SSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
35710ad9a77SSam Leffler #endif
35850d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
3598a1b9b6aSSam Leffler 	}
3608a1b9b6aSSam Leffler 	/*
3618a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3628a1b9b6aSSam Leffler 	 */
363b032f27cSSam Leffler 	/* clear DFS CAC state on previous channel */
364b032f27cSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
365b032f27cSSam Leffler 	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
366b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
367b032f27cSSam Leffler 		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
36868e8e04eSSam Leffler 	ic->ic_bsschan = chan;
369b032f27cSSam Leffler 	ieee80211_node_set_chan(ni, chan);
37068e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(chan);
3718a1b9b6aSSam Leffler 	/*
372b032f27cSSam Leffler 	 * Do mode-specific setup.
3738a1b9b6aSSam Leffler 	 */
37468e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan)) {
37568e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
37668e8e04eSSam Leffler 			/*
377b032f27cSSam Leffler 			 * Use a mixed 11b/11g basic rate set.
37868e8e04eSSam Leffler 			 */
379b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
38068e8e04eSSam Leffler 			    IEEE80211_MODE_11G);
381b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PUREG) {
382b032f27cSSam Leffler 				/*
383b032f27cSSam Leffler 				 * Also mark OFDM rates basic so 11b
384b032f27cSSam Leffler 				 * stations do not join (WiFi compliance).
385b032f27cSSam Leffler 				 */
386b032f27cSSam Leffler 				ieee80211_addbasicrates(&ni->ni_rates,
387b032f27cSSam Leffler 				    IEEE80211_MODE_11A);
388b032f27cSSam Leffler 			}
38968e8e04eSSam Leffler 		} else if (IEEE80211_IS_CHAN_B(chan)) {
39068e8e04eSSam Leffler 			/*
39168e8e04eSSam Leffler 			 * Force pure 11b rate set.
39268e8e04eSSam Leffler 			 */
393b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
39468e8e04eSSam Leffler 				IEEE80211_MODE_11B);
39568e8e04eSSam Leffler 		}
3961a1e1d21SSam Leffler 	}
3971a1e1d21SSam Leffler 
39868e8e04eSSam Leffler 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
39968e8e04eSSam Leffler }
40068e8e04eSSam Leffler 
40168e8e04eSSam Leffler /*
40268e8e04eSSam Leffler  * Reset bss state on transition to the INIT state.
40368e8e04eSSam Leffler  * Clear any stations from the table (they have been
40468e8e04eSSam Leffler  * deauth'd) and reset the bss node (clears key, rate
40568e8e04eSSam Leffler  * etc. state).
40668e8e04eSSam Leffler  */
4078a1b9b6aSSam Leffler void
408b032f27cSSam Leffler ieee80211_reset_bss(struct ieee80211vap *vap)
409b4c5a90fSSam Leffler {
410b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4118a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4128a1b9b6aSSam Leffler 
413b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
414b032f27cSSam Leffler 	/* XXX multi-bss: wrong */
41568e8e04eSSam Leffler 	ieee80211_reset_erp(ic);
416acc4f7f5SSam Leffler 
417b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
4188a1b9b6aSSam Leffler 	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
419b032f27cSSam Leffler 	obss = vap->iv_bss;
420b032f27cSSam Leffler 	vap->iv_bss = ieee80211_ref_node(ni);
421f9cd9174SSam Leffler 	if (obss != NULL) {
422f9cd9174SSam Leffler 		copy_bss(ni, obss);
423d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
4248a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
425b032f27cSSam Leffler 	} else
426b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
427f9cd9174SSam Leffler }
4288a1b9b6aSSam Leffler 
4298a1b9b6aSSam Leffler static int
43068e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni,
43168e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
4328a1b9b6aSSam Leffler {
43368e8e04eSSam Leffler 	int i;
43468e8e04eSSam Leffler 
43568e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
43668e8e04eSSam Leffler 		if (ni->ni_esslen == ssids[i].len &&
43768e8e04eSSam Leffler 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
43868e8e04eSSam Leffler 			return 1;
43968e8e04eSSam Leffler 	}
44068e8e04eSSam Leffler 	return 0;
44168e8e04eSSam Leffler }
44268e8e04eSSam Leffler 
44368e8e04eSSam Leffler /*
44468e8e04eSSam Leffler  * Test a node for suitability/compatibility.
44568e8e04eSSam Leffler  */
44668e8e04eSSam Leffler static int
447b032f27cSSam Leffler check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
44868e8e04eSSam Leffler {
449b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
45068e8e04eSSam Leffler         uint8_t rate;
45168e8e04eSSam Leffler 
45268e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
45368e8e04eSSam Leffler 		return 0;
454b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
45568e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
45668e8e04eSSam Leffler 			return 0;
45768e8e04eSSam Leffler 	} else {
45868e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
45968e8e04eSSam Leffler 			return 0;
46068e8e04eSSam Leffler 	}
461b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
46268e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
46368e8e04eSSam Leffler 			return 0;
46468e8e04eSSam Leffler 	} else {
46568e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
46668e8e04eSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
46768e8e04eSSam Leffler 			return 0;
46868e8e04eSSam Leffler 	}
46968e8e04eSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
47068e8e04eSSam Leffler 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
47168e8e04eSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
47268e8e04eSSam Leffler 		return 0;
473b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
474b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
47568e8e04eSSam Leffler 		return 0;
476b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
477b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
47868e8e04eSSam Leffler 		return 0;
47968e8e04eSSam Leffler 	return 1;
48068e8e04eSSam Leffler }
48168e8e04eSSam Leffler 
48268e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
48368e8e04eSSam Leffler /*
48468e8e04eSSam Leffler  * Display node suitability/compatibility.
48568e8e04eSSam Leffler  */
48668e8e04eSSam Leffler static void
487b032f27cSSam Leffler check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
48868e8e04eSSam Leffler {
489b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
49068e8e04eSSam Leffler         uint8_t rate;
491b4c5a90fSSam Leffler         int fail;
492b4c5a90fSSam Leffler 
493b4c5a90fSSam Leffler 	fail = 0;
494b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
495b4c5a90fSSam Leffler 		fail |= 0x01;
496b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
497b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
498b4c5a90fSSam Leffler 			fail |= 0x02;
499b4c5a90fSSam Leffler 	} else {
500b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
501b4c5a90fSSam Leffler 			fail |= 0x02;
502b4c5a90fSSam Leffler 	}
503b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
504b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
505b4c5a90fSSam Leffler 			fail |= 0x04;
506b4c5a90fSSam Leffler 	} else {
507b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
508b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
509b4c5a90fSSam Leffler 			fail |= 0x04;
510b4c5a90fSSam Leffler 	}
51170e28b9aSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
51279edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
513b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
514b4c5a90fSSam Leffler 		fail |= 0x08;
515b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
516b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
517b4c5a90fSSam Leffler 		fail |= 0x10;
518b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
519b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
520b4c5a90fSSam Leffler 		fail |= 0x20;
52168e8e04eSSam Leffler 
52268e8e04eSSam Leffler 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
52368e8e04eSSam Leffler 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
52468e8e04eSSam Leffler 	printf(" %3d%c",
52568e8e04eSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
526b4c5a90fSSam Leffler 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
527b4c5a90fSSam Leffler 	    fail & 0x08 ? '!' : ' ');
528b4c5a90fSSam Leffler 	printf(" %4s%c",
529b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
530b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
531b4c5a90fSSam Leffler 	    "????",
532b4c5a90fSSam Leffler 	    fail & 0x02 ? '!' : ' ');
533b4c5a90fSSam Leffler 	printf(" %3s%c ",
53468e8e04eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
535b4c5a90fSSam Leffler 	    fail & 0x04 ? '!' : ' ');
536b4c5a90fSSam Leffler 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
537b4c5a90fSSam Leffler 	printf("%s\n", fail & 0x10 ? "!" : "");
538b4c5a90fSSam Leffler }
53968e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
5408a1b9b6aSSam Leffler 
541750d6d0cSSam Leffler /*
5428a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
5438a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
5448a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
5458a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
5468a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
5478a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
5488a1b9b6aSSam Leffler  *
5498a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
5508a1b9b6aSSam Leffler  *
5518a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
552750d6d0cSSam Leffler  */
5538a1b9b6aSSam Leffler int
554641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
5558a1b9b6aSSam Leffler {
556b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
557b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
558641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
559b032f27cSSam Leffler #endif
5608a1b9b6aSSam Leffler 
561b032f27cSSam Leffler 	if (ni == vap->iv_bss ||
562b032f27cSSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
5638a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
5648a1b9b6aSSam Leffler 		return 0;
5658a1b9b6aSSam Leffler 	}
566b032f27cSSam Leffler 	if (!check_bss(vap, ni)) {
56768e8e04eSSam Leffler 		/* capabilities mismatch */
568b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
5698a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
57068e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
571b032f27cSSam Leffler 		if (ieee80211_msg_assoc(vap))
572b032f27cSSam Leffler 			check_bss_debug(vap, ni);
57368e8e04eSSam Leffler #endif
574b032f27cSSam Leffler 		vap->iv_stats.is_ibss_capmismatch++;
5758a1b9b6aSSam Leffler 		return 0;
5768a1b9b6aSSam Leffler 	}
577b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
5788a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
5798a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
5808a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
5818a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
5828a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
5838a1b9b6aSSam Leffler 	);
58468e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
5858a1b9b6aSSam Leffler }
5868a1b9b6aSSam Leffler 
5878a1b9b6aSSam Leffler /*
588b032f27cSSam Leffler  * Calculate HT channel promotion flags for all vaps.
589b032f27cSSam Leffler  * This assumes ni_chan have been setup for each vap.
590b032f27cSSam Leffler  */
591b032f27cSSam Leffler static int
592b032f27cSSam Leffler gethtadjustflags(struct ieee80211com *ic)
593b032f27cSSam Leffler {
594b032f27cSSam Leffler 	struct ieee80211vap *vap;
595b032f27cSSam Leffler 	int flags;
596b032f27cSSam Leffler 
597b032f27cSSam Leffler 	flags = 0;
598b032f27cSSam Leffler 	/* XXX locking */
599b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
600b032f27cSSam Leffler 		if (vap->iv_state < IEEE80211_S_RUN)
601b032f27cSSam Leffler 			continue;
602b032f27cSSam Leffler 		switch (vap->iv_opmode) {
603b032f27cSSam Leffler 		case IEEE80211_M_WDS:
604b032f27cSSam Leffler 		case IEEE80211_M_STA:
605b032f27cSSam Leffler 		case IEEE80211_M_AHDEMO:
606b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
607b032f27cSSam Leffler 		case IEEE80211_M_IBSS:
608b032f27cSSam Leffler 			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
609b032f27cSSam Leffler 			break;
610b032f27cSSam Leffler 		default:
611b032f27cSSam Leffler 			break;
612b032f27cSSam Leffler 		}
613b032f27cSSam Leffler 	}
614b032f27cSSam Leffler 	return flags;
615b032f27cSSam Leffler }
616b032f27cSSam Leffler 
617b032f27cSSam Leffler /*
618b032f27cSSam Leffler  * Check if the current channel needs to change based on whether
6195d44f8c0SSam Leffler  * any vap's are using HT20/HT40.  This is used to sync the state
6205d44f8c0SSam Leffler  * of ic_curchan after a channel width change on a running vap.
6211b6167d2SSam Leffler  */
6221b6167d2SSam Leffler void
623b032f27cSSam Leffler ieee80211_sync_curchan(struct ieee80211com *ic)
6241b6167d2SSam Leffler {
625b032f27cSSam Leffler 	struct ieee80211_channel *c;
626b032f27cSSam Leffler 
627b032f27cSSam Leffler 	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
628b032f27cSSam Leffler 	if (c != ic->ic_curchan) {
629b032f27cSSam Leffler 		ic->ic_curchan = c;
630b032f27cSSam Leffler 		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
63126d39e2cSSam Leffler 		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
6325efea30fSAndrew Thompson 		IEEE80211_UNLOCK(ic);
633b032f27cSSam Leffler 		ic->ic_set_channel(ic);
6345463c4a4SSam Leffler 		ieee80211_radiotap_chan_change(ic);
6355efea30fSAndrew Thompson 		IEEE80211_LOCK(ic);
636b032f27cSSam Leffler 	}
637b032f27cSSam Leffler }
638b032f27cSSam Leffler 
639b032f27cSSam Leffler /*
6405efea30fSAndrew Thompson  * Setup the current channel.  The request channel may be
641b032f27cSSam Leffler  * promoted if other vap's are operating with HT20/HT40.
642b032f27cSSam Leffler  */
643b032f27cSSam Leffler void
6445efea30fSAndrew Thompson ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
645b032f27cSSam Leffler {
646b032f27cSSam Leffler 	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
647b032f27cSSam Leffler 		int flags = gethtadjustflags(ic);
648b032f27cSSam Leffler 		/*
649b032f27cSSam Leffler 		 * Check for channel promotion required to support the
650b032f27cSSam Leffler 		 * set of running vap's.  This assumes we are called
651b032f27cSSam Leffler 		 * after ni_chan is setup for each vap.
652b032f27cSSam Leffler 		 */
653b032f27cSSam Leffler 		/* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
654b032f27cSSam Leffler 		if (flags > ieee80211_htchanflags(c))
655b032f27cSSam Leffler 			c = ieee80211_ht_adjust_channel(ic, c, flags);
656b032f27cSSam Leffler 	}
657b032f27cSSam Leffler 	ic->ic_bsschan = ic->ic_curchan = c;
6581b6167d2SSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
65926d39e2cSSam Leffler 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
6605efea30fSAndrew Thompson }
6615efea30fSAndrew Thompson 
6625efea30fSAndrew Thompson /*
6635efea30fSAndrew Thompson  * Change the current channel.  The channel change is guaranteed to have
6645efea30fSAndrew Thompson  * happened before the next state change.
6655efea30fSAndrew Thompson  */
6665efea30fSAndrew Thompson void
6675efea30fSAndrew Thompson ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
6685efea30fSAndrew Thompson {
6695efea30fSAndrew Thompson 	ieee80211_setupcurchan(ic, c);
6705efea30fSAndrew Thompson 	ieee80211_runtask(ic, &ic->ic_chan_task);
6711b6167d2SSam Leffler }
6721b6167d2SSam Leffler 
6731b6167d2SSam Leffler /*
6748a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
6758a1b9b6aSSam Leffler  * be passed in with a held reference.
6768a1b9b6aSSam Leffler  */
67768e8e04eSSam Leffler static int
67868e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs)
6798a1b9b6aSSam Leffler {
680b032f27cSSam Leffler 	struct ieee80211vap *vap = selbs->ni_vap;
68168e8e04eSSam Leffler 	struct ieee80211com *ic = selbs->ni_ic;
6828a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
68368e8e04eSSam Leffler 	int canreassoc;
6848a1b9b6aSSam Leffler 
6858a1b9b6aSSam Leffler 	/*
6868a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
6878a1b9b6aSSam Leffler 	 */
688b032f27cSSam Leffler 	obss = vap->iv_bss;
68968e8e04eSSam Leffler 	/*
69068e8e04eSSam Leffler 	 * Check if old+new node have the same address in which
69168e8e04eSSam Leffler 	 * case we can reassociate when operating in sta mode.
69268e8e04eSSam Leffler 	 */
69368e8e04eSSam Leffler 	canreassoc = (obss != NULL &&
694b032f27cSSam Leffler 		vap->iv_state == IEEE80211_S_RUN &&
69568e8e04eSSam Leffler 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
696b032f27cSSam Leffler 	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
6971fd2349dSSam Leffler 	if (obss != NULL) {
6981fd2349dSSam Leffler 		copy_bss(selbs, obss);
69947a7b0faSSam Leffler 		ieee80211_node_decref(obss);	/* iv_bss reference */
70047a7b0faSSam Leffler 		ieee80211_free_node(obss);	/* station table reference */
701b032f27cSSam Leffler 		obss = NULL;		/* NB: guard against later use */
7021fd2349dSSam Leffler 	}
70379edaebfSSam Leffler 
70479edaebfSSam Leffler 	/*
70579edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
70679edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
70779edaebfSSam Leffler 	 */
708b032f27cSSam Leffler 	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
70970e28b9aSSam Leffler 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
71079edaebfSSam Leffler 
711b032f27cSSam Leffler 	ieee80211_setcurchan(ic, selbs->ni_chan);
7128a1b9b6aSSam Leffler 	/*
7138a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
7148a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
7158a1b9b6aSSam Leffler 	 * mode is locked.
7168a1b9b6aSSam Leffler 	 */
7178a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
718b032f27cSSam Leffler 	ieee80211_wme_initparams(vap);
719acc4f7f5SSam Leffler 
720b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
72168e8e04eSSam Leffler 		if (canreassoc) {
72268e8e04eSSam Leffler 			/* Reassociate */
723b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
72468e8e04eSSam Leffler 		} else {
72568e8e04eSSam Leffler 			/*
72668e8e04eSSam Leffler 			 * Act as if we received a DEAUTH frame in case we
72768e8e04eSSam Leffler 			 * are invoked from the RUN state.  This will cause
72868e8e04eSSam Leffler 			 * us to try to re-authenticate if we are operating
72968e8e04eSSam Leffler 			 * as a station.
73068e8e04eSSam Leffler 			 */
731b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_AUTH,
73268e8e04eSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH);
73368e8e04eSSam Leffler 		}
73468e8e04eSSam Leffler 	} else
735b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
7368a1b9b6aSSam Leffler 	return 1;
7378a1b9b6aSSam Leffler }
7388a1b9b6aSSam Leffler 
73968e8e04eSSam Leffler int
74010959256SSam Leffler ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
74168e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se)
74268e8e04eSSam Leffler {
743b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
74468e8e04eSSam Leffler 	struct ieee80211_node *ni;
74568e8e04eSSam Leffler 
746b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
74768e8e04eSSam Leffler 	if (ni == NULL) {
74868e8e04eSSam Leffler 		/* XXX msg */
74968e8e04eSSam Leffler 		return 0;
75068e8e04eSSam Leffler 	}
75168e8e04eSSam Leffler 	/*
75268e8e04eSSam Leffler 	 * Expand scan state into node's format.
75368e8e04eSSam Leffler 	 * XXX may not need all this stuff
75468e8e04eSSam Leffler 	 */
75568e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
75668e8e04eSSam Leffler 	ni->ni_esslen = se->se_ssid[1];
75768e8e04eSSam Leffler 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
75868e8e04eSSam Leffler 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
75968e8e04eSSam Leffler 	ni->ni_intval = se->se_intval;
76068e8e04eSSam Leffler 	ni->ni_capinfo = se->se_capinfo;
76110959256SSam Leffler 	ni->ni_chan = chan;
76268e8e04eSSam Leffler 	ni->ni_timoff = se->se_timoff;
76368e8e04eSSam Leffler 	ni->ni_fhdwell = se->se_fhdwell;
76468e8e04eSSam Leffler 	ni->ni_fhindex = se->se_fhindex;
76568e8e04eSSam Leffler 	ni->ni_erp = se->se_erp;
766b032f27cSSam Leffler 	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
76768e8e04eSSam Leffler 	ni->ni_noise = se->se_noise;
7686ca74c40SSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
7696ca74c40SSam Leffler 		/* NB: only infrastructure mode requires an associd */
7701b999d64SSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
7716ca74c40SSam Leffler 	}
77268e8e04eSSam Leffler 
773b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
774b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
775616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
776b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
777b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
778616190d0SSam Leffler #endif
779b032f27cSSam Leffler 		if (ni->ni_ies.htcap_ie != NULL)
780b032f27cSSam Leffler 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
781b032f27cSSam Leffler 		if (ni->ni_ies.htinfo_ie != NULL)
782b032f27cSSam Leffler 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
78310ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
78410ad9a77SSam Leffler 		if (ni->ni_ies.tdma_ie != NULL)
78510ad9a77SSam Leffler 			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
78610ad9a77SSam Leffler #endif
787b032f27cSSam Leffler 	}
788b032f27cSSam Leffler 
789b032f27cSSam Leffler 	vap->iv_dtim_period = se->se_dtimperiod;
790b032f27cSSam Leffler 	vap->iv_dtim_count = 0;
79168e8e04eSSam Leffler 
79268e8e04eSSam Leffler 	/* NB: must be after ni_chan is setup */
79368e8e04eSSam Leffler 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
79468e8e04eSSam Leffler 		IEEE80211_F_DOSORT);
795dfcd1f4dSSam Leffler 	if (ieee80211_iserp_rateset(&ni->ni_rates))
796dfcd1f4dSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
797dfcd1f4dSSam Leffler 	node_setuptxparms(ni);
79868e8e04eSSam Leffler 
79968e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
80068e8e04eSSam Leffler }
80168e8e04eSSam Leffler 
8028a1b9b6aSSam Leffler /*
8038a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
8048a1b9b6aSSam Leffler  * be passed in with a held reference.
8058a1b9b6aSSam Leffler  */
8068a1b9b6aSSam Leffler void
807b032f27cSSam Leffler ieee80211_sta_leave(struct ieee80211_node *ni)
8088a1b9b6aSSam Leffler {
809b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
810b032f27cSSam Leffler 
8118a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
812b032f27cSSam Leffler 	ieee80211_notify_node_leave(ni);
813b032f27cSSam Leffler }
814b032f27cSSam Leffler 
815b032f27cSSam Leffler /*
816b032f27cSSam Leffler  * Send a deauthenticate frame and drop the station.
817b032f27cSSam Leffler  */
818b032f27cSSam Leffler void
819b032f27cSSam Leffler ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
820b032f27cSSam Leffler {
821b032f27cSSam Leffler 	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
822b032f27cSSam Leffler 	ieee80211_ref_node(ni);
823b032f27cSSam Leffler 	if (ni->ni_associd != 0)
824b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
825b032f27cSSam Leffler 	ieee80211_node_leave(ni);
826b032f27cSSam Leffler 	ieee80211_free_node(ni);
8278a1b9b6aSSam Leffler }
8288a1b9b6aSSam Leffler 
8291a1e1d21SSam Leffler static struct ieee80211_node *
83038c208f8SSam Leffler node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
8311a1e1d21SSam Leffler {
832410ca74bSSam Leffler 	struct ieee80211_node *ni;
8338a1b9b6aSSam Leffler 
834e2126decSSam Leffler 	ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
835410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
836410ca74bSSam Leffler 	return ni;
8371a1e1d21SSam Leffler }
8381a1e1d21SSam Leffler 
8398a1b9b6aSSam Leffler /*
840b032f27cSSam Leffler  * Initialize an ie blob with the specified data.  If previous
841b032f27cSSam Leffler  * data exists re-use the data block.  As a side effect we clear
842b032f27cSSam Leffler  * all references to specific ie's; the caller is required to
843b032f27cSSam Leffler  * recalculate them.
844b032f27cSSam Leffler  */
845b032f27cSSam Leffler int
846b032f27cSSam Leffler ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
847b032f27cSSam Leffler {
848b032f27cSSam Leffler 	/* NB: assumes data+len are the last fields */
849b032f27cSSam Leffler 	memset(ies, 0, offsetof(struct ieee80211_ies, data));
850b032f27cSSam Leffler 	if (ies->data != NULL && ies->len != len) {
851b032f27cSSam Leffler 		/* data size changed */
852e2126decSSam Leffler 		free(ies->data, M_80211_NODE_IE);
853b032f27cSSam Leffler 		ies->data = NULL;
854b032f27cSSam Leffler 	}
855b032f27cSSam Leffler 	if (ies->data == NULL) {
856e2126decSSam Leffler 		ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
857b032f27cSSam Leffler 		if (ies->data == NULL) {
858b032f27cSSam Leffler 			ies->len = 0;
859b032f27cSSam Leffler 			/* NB: pointers have already been zero'd above */
860b032f27cSSam Leffler 			return 0;
861b032f27cSSam Leffler 		}
862b032f27cSSam Leffler 	}
863b032f27cSSam Leffler 	memcpy(ies->data, data, len);
864b032f27cSSam Leffler 	ies->len = len;
865b032f27cSSam Leffler 	return 1;
866b032f27cSSam Leffler }
867b032f27cSSam Leffler 
868b032f27cSSam Leffler /*
869b032f27cSSam Leffler  * Reclaim storage for an ie blob.
870b032f27cSSam Leffler  */
871b032f27cSSam Leffler void
872b032f27cSSam Leffler ieee80211_ies_cleanup(struct ieee80211_ies *ies)
873b032f27cSSam Leffler {
874b032f27cSSam Leffler 	if (ies->data != NULL)
875e2126decSSam Leffler 		free(ies->data, M_80211_NODE_IE);
876b032f27cSSam Leffler }
877b032f27cSSam Leffler 
878b032f27cSSam Leffler /*
879b032f27cSSam Leffler  * Expand an ie blob data contents and to fillin individual
880b032f27cSSam Leffler  * ie pointers.  The data blob is assumed to be well-formed;
881b032f27cSSam Leffler  * we don't do any validity checking of ie lengths.
882b032f27cSSam Leffler  */
883b032f27cSSam Leffler void
884b032f27cSSam Leffler ieee80211_ies_expand(struct ieee80211_ies *ies)
885b032f27cSSam Leffler {
886b032f27cSSam Leffler 	uint8_t *ie;
887b032f27cSSam Leffler 	int ielen;
888b032f27cSSam Leffler 
889b032f27cSSam Leffler 	ie = ies->data;
890b032f27cSSam Leffler 	ielen = ies->len;
891b032f27cSSam Leffler 	while (ielen > 0) {
892b032f27cSSam Leffler 		switch (ie[0]) {
893b032f27cSSam Leffler 		case IEEE80211_ELEMID_VENDOR:
894b032f27cSSam Leffler 			if (iswpaoui(ie))
895b032f27cSSam Leffler 				ies->wpa_ie = ie;
896b032f27cSSam Leffler 			else if (iswmeoui(ie))
897b032f27cSSam Leffler 				ies->wme_ie = ie;
898616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
899b032f27cSSam Leffler 			else if (isatherosoui(ie))
900b032f27cSSam Leffler 				ies->ath_ie = ie;
901616190d0SSam Leffler #endif
90210ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
90310ad9a77SSam Leffler 			else if (istdmaoui(ie))
90410ad9a77SSam Leffler 				ies->tdma_ie = ie;
90510ad9a77SSam Leffler #endif
906b032f27cSSam Leffler 			break;
907b032f27cSSam Leffler 		case IEEE80211_ELEMID_RSN:
908b032f27cSSam Leffler 			ies->rsn_ie = ie;
909b032f27cSSam Leffler 			break;
910b032f27cSSam Leffler 		case IEEE80211_ELEMID_HTCAP:
911b032f27cSSam Leffler 			ies->htcap_ie = ie;
912b032f27cSSam Leffler 			break;
913b032f27cSSam Leffler 		}
914b032f27cSSam Leffler 		ielen -= 2 + ie[1];
915b032f27cSSam Leffler 		ie += 2 + ie[1];
916b032f27cSSam Leffler 	}
917b032f27cSSam Leffler }
918b032f27cSSam Leffler 
919b032f27cSSam Leffler /*
9208a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
9218a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
9228a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
9238a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
9248a1b9b6aSSam Leffler  */
9251a1e1d21SSam Leffler static void
9268a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
9271a1e1d21SSam Leffler {
9288a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
929b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
93068e8e04eSSam Leffler 	int i;
9318a1b9b6aSSam Leffler 
9328a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
9338a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
934b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_STA)
935b032f27cSSam Leffler 			vap->iv_ps_sta--;
9368a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
937b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
938b032f27cSSam Leffler 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
9398a1b9b6aSSam Leffler 	}
940ebdda46cSSam Leffler 	/*
9411b6167d2SSam Leffler 	 * Cleanup any HT-related state.
9421b6167d2SSam Leffler 	 */
9431b6167d2SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT)
9441b6167d2SSam Leffler 		ieee80211_ht_node_cleanup(ni);
945339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
946339ccfb3SSam Leffler 	else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
947339ccfb3SSam Leffler 		ieee80211_ff_node_cleanup(ni);
948339ccfb3SSam Leffler #endif
9491b6167d2SSam Leffler 	/*
950ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
951ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
952ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
953ebdda46cSSam Leffler 	 * do it just in case.
9541b999d64SSam Leffler 	 * Likewise clear the ASSOCID flag as these flags are intended
9551b999d64SSam Leffler 	 * to be managed in tandem.
956ebdda46cSSam Leffler 	 */
9571b999d64SSam Leffler 	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
9588a1b9b6aSSam Leffler 
9598a1b9b6aSSam Leffler 	/*
9608a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
9618a1b9b6aSSam Leffler 	 */
96263092fceSSam Leffler 	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
963b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
9648a1b9b6aSSam Leffler 
9658a1b9b6aSSam Leffler 	ni->ni_associd = 0;
9668a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
967e2126decSSam Leffler 		free(ni->ni_challenge, M_80211_NODE);
9688a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
9698a1b9b6aSSam Leffler 	}
9708a1b9b6aSSam Leffler 	/*
9718a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
9728a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
9738a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
9748a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
9758a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
9768a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
9778a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
9788a1b9b6aSSam Leffler 	 * which will rebuild this state.
9798a1b9b6aSSam Leffler 	 *
9808a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
9818a1b9b6aSSam Leffler 	 */
9828a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
9838a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
9848a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
9858a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
9868a1b9b6aSSam Leffler 		}
987c1225b52SSam Leffler 	/*
988c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
989c1225b52SSam Leffler 	 */
990c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
9918a1b9b6aSSam Leffler #undef N
9928a1b9b6aSSam Leffler }
9938a1b9b6aSSam Leffler 
9948a1b9b6aSSam Leffler static void
9958a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
9968a1b9b6aSSam Leffler {
9978a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
9988a1b9b6aSSam Leffler 
9998a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
1000b032f27cSSam Leffler 	ieee80211_ies_cleanup(&ni->ni_ies);
100163092fceSSam Leffler 	ieee80211_psq_cleanup(&ni->ni_psq);
1002b032f27cSSam Leffler 	IEEE80211_NODE_WDSQ_DESTROY(ni);
1003e2126decSSam Leffler 	free(ni, M_80211_NODE);
10041a1e1d21SSam Leffler }
10051a1e1d21SSam Leffler 
1006b032f27cSSam Leffler static void
1007b032f27cSSam Leffler node_age(struct ieee80211_node *ni)
1008b032f27cSSam Leffler {
1009b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
10105d44f8c0SSam Leffler 
10115d44f8c0SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
10125d44f8c0SSam Leffler 
1013b032f27cSSam Leffler 	/*
1014b032f27cSSam Leffler 	 * Age frames on the power save queue.
1015b032f27cSSam Leffler 	 */
101663092fceSSam Leffler 	if (ieee80211_node_psq_age(ni) != 0 &&
101763092fceSSam Leffler 	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1018b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
1019b032f27cSSam Leffler 	/*
1020b032f27cSSam Leffler 	 * Age frames on the wds pending queue.
1021b032f27cSSam Leffler 	 */
1022b032f27cSSam Leffler 	if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
1023b032f27cSSam Leffler 		ieee80211_node_wdsq_age(ni);
1024b032f27cSSam Leffler 	/*
1025b032f27cSSam Leffler 	 * Age out HT resources (e.g. frames on the
1026b032f27cSSam Leffler 	 * A-MPDU reorder queues).
1027b032f27cSSam Leffler 	 */
1028b032f27cSSam Leffler 	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1029b032f27cSSam Leffler 		ieee80211_ht_node_age(ni);
1030b032f27cSSam Leffler }
1031b032f27cSSam Leffler 
103268e8e04eSSam Leffler static int8_t
10338a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
1034d1e61976SSam Leffler {
1035b032f27cSSam Leffler 	uint32_t avgrssi = ni->ni_avgrssi;
1036b032f27cSSam Leffler 	int32_t rssi;
1037b032f27cSSam Leffler 
1038b032f27cSSam Leffler 	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1039b032f27cSSam Leffler 		return 0;
1040b032f27cSSam Leffler 	rssi = IEEE80211_RSSI_GET(avgrssi);
1041b032f27cSSam Leffler 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1042d1e61976SSam Leffler }
1043d1e61976SSam Leffler 
10441a1e1d21SSam Leffler static void
104568e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
104668e8e04eSSam Leffler {
1047b032f27cSSam Leffler 	*rssi = node_getrssi(ni);
104868e8e04eSSam Leffler 	*noise = ni->ni_noise;
104968e8e04eSSam Leffler }
105068e8e04eSSam Leffler 
105168e8e04eSSam Leffler static void
1052b032f27cSSam Leffler node_getmimoinfo(const struct ieee80211_node *ni,
1053b032f27cSSam Leffler 	struct ieee80211_mimo_info *info)
1054b032f27cSSam Leffler {
1055b032f27cSSam Leffler 	/* XXX zero data? */
1056b032f27cSSam Leffler }
1057b032f27cSSam Leffler 
1058b032f27cSSam Leffler struct ieee80211_node *
1059b032f27cSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt,
1060b032f27cSSam Leffler 	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
10611a1e1d21SSam Leffler {
10628a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1063b032f27cSSam Leffler 	struct ieee80211_node *ni;
10641a1e1d21SSam Leffler 	int hash;
10651a1e1d21SSam Leffler 
106638c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
1067b032f27cSSam Leffler 	if (ni == NULL) {
1068b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
1069b032f27cSSam Leffler 		return NULL;
1070b032f27cSSam Leffler 	}
1071b032f27cSSam Leffler 
1072b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
107349a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
10748a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
10758a1b9b6aSSam Leffler 
10761a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
10771a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
10788a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
10798a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
10808a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
10818a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
108201a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1083b032f27cSSam Leffler 	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1084b032f27cSSam Leffler 	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
10852045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
10862045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
108768e8e04eSSam Leffler 	ni->ni_ath_defkeyix = 0x7fff;
108863092fceSSam Leffler 	ieee80211_psq_init(&ni->ni_psq, "unknown");
1089b032f27cSSam Leffler 	IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
10908a1b9b6aSSam Leffler 
10918a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
10928a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
10938a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
10948a1b9b6aSSam Leffler 	ni->ni_table = nt;
1095b032f27cSSam Leffler 	ni->ni_vap = vap;
10968a1b9b6aSSam Leffler 	ni->ni_ic = ic;
10978a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
10981a1e1d21SSam Leffler 
1099be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1100be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1101be1054edSSam Leffler 
11021a1e1d21SSam Leffler 	return ni;
11031a1e1d21SSam Leffler }
11041a1e1d21SSam Leffler 
110597c973adSSam Leffler /*
110697c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
110797c973adSSam Leffler  * to the specified station.  We craft only as much state as we
110897c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
110997c973adSSam Leffler  * once the send completes.
111097c973adSSam Leffler  */
111197c973adSSam Leffler struct ieee80211_node *
1112b032f27cSSam Leffler ieee80211_tmp_node(struct ieee80211vap *vap,
1113b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
111497c973adSSam Leffler {
1115b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
111697c973adSSam Leffler 	struct ieee80211_node *ni;
111797c973adSSam Leffler 
111838c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
111997c973adSSam Leffler 	if (ni != NULL) {
1120b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1121b9b5f07dSSam Leffler 
1122b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
112397c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
112497c973adSSam Leffler 
1125b032f27cSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
1126b032f27cSSam Leffler 		ni->ni_ic = ic;			/* NB: needed to set channel */
1127b032f27cSSam Leffler 		ni->ni_vap = vap;
1128b032f27cSSam Leffler 
112997c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1130b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
113197c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
113297c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
1133b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1134b032f27cSSam Leffler 		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
113597c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
1136b9b5f07dSSam Leffler 		ni->ni_txpower = bss->ni_txpower;
113797c973adSSam Leffler 		/* XXX optimize away */
113863092fceSSam Leffler 		ieee80211_psq_init(&ni->ni_psq, "unknown");
1139b032f27cSSam Leffler 		IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
114097c973adSSam Leffler 	} else {
114197c973adSSam Leffler 		/* XXX msg */
1142b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
114397c973adSSam Leffler 	}
114497c973adSSam Leffler 	return ni;
114597c973adSSam Leffler }
114697c973adSSam Leffler 
11471a1e1d21SSam Leffler struct ieee80211_node *
1148b032f27cSSam Leffler ieee80211_dup_bss(struct ieee80211vap *vap,
1149b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
11501a1e1d21SSam Leffler {
1151b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
11528a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
11538a1b9b6aSSam Leffler 
1154b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
11551a1e1d21SSam Leffler 	if (ni != NULL) {
1156b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1157694dca64SSam Leffler 		/*
1158b032f27cSSam Leffler 		 * Inherit from iv_bss.
1159694dca64SSam Leffler 		 */
1160b9b5f07dSSam Leffler 		copy_bss(ni, bss);
1161b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1162b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1163b032f27cSSam Leffler 	}
11641a1e1d21SSam Leffler 	return ni;
11651a1e1d21SSam Leffler }
11661a1e1d21SSam Leffler 
1167b032f27cSSam Leffler /*
1168b032f27cSSam Leffler  * Create a bss node for a legacy WDS vap.  The far end does
1169b032f27cSSam Leffler  * not associate so we just create create a new node and
1170b032f27cSSam Leffler  * simulate an association.  The caller is responsible for
1171b032f27cSSam Leffler  * installing the node as the bss node and handling any further
1172b032f27cSSam Leffler  * setup work like authorizing the port.
1173b032f27cSSam Leffler  */
1174b032f27cSSam Leffler struct ieee80211_node *
1175b032f27cSSam Leffler ieee80211_node_create_wds(struct ieee80211vap *vap,
1176b032f27cSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1177b032f27cSSam Leffler {
1178b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1179b032f27cSSam Leffler 	struct ieee80211_node *ni;
1180b032f27cSSam Leffler 
1181b032f27cSSam Leffler 	/* XXX check if node already in sta table? */
1182b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1183b032f27cSSam Leffler 	if (ni != NULL) {
1184b032f27cSSam Leffler 		ni->ni_wdsvap = vap;
1185b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1186b032f27cSSam Leffler 		/*
1187b032f27cSSam Leffler 		 * Inherit any manually configured settings.
1188b032f27cSSam Leffler 		 */
1189b9b5f07dSSam Leffler 		copy_bss(ni, vap->iv_bss);
1190b032f27cSSam Leffler 		ieee80211_node_set_chan(ni, chan);
1191b032f27cSSam Leffler 		/* NB: propagate ssid so available to WPA supplicant */
1192b032f27cSSam Leffler 		ni->ni_esslen = vap->iv_des_ssid[0].len;
1193b032f27cSSam Leffler 		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1194b032f27cSSam Leffler 		/* NB: no associd for peer */
1195b032f27cSSam Leffler 		/*
1196b032f27cSSam Leffler 		 * There are no management frames to use to
1197b032f27cSSam Leffler 		 * discover neighbor capabilities, so blindly
1198b032f27cSSam Leffler 		 * propagate the local configuration.
1199b032f27cSSam Leffler 		 */
1200b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WME)
1201b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
1202616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1203b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_FF)
1204b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_FF;
1205616190d0SSam Leffler #endif
1206b032f27cSSam Leffler 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1207b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1208b032f27cSSam Leffler 			/*
1209b032f27cSSam Leffler 			 * Device is HT-capable and HT is enabled for
1210b032f27cSSam Leffler 			 * the vap; setup HT operation.  On return
1211b032f27cSSam Leffler 			 * ni_chan will be adjusted to an HT channel.
1212b032f27cSSam Leffler 			 */
1213b032f27cSSam Leffler 			ieee80211_ht_wds_init(ni);
1214b032f27cSSam Leffler 		} else {
1215b032f27cSSam Leffler 			struct ieee80211_channel *c = ni->ni_chan;
1216b032f27cSSam Leffler 			/*
1217b032f27cSSam Leffler 			 * Force a legacy channel to be used.
1218b032f27cSSam Leffler 			 */
1219b032f27cSSam Leffler 			c = ieee80211_find_channel(ic,
1220b032f27cSSam Leffler 			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1221b032f27cSSam Leffler 			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1222b032f27cSSam Leffler 			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1223b032f27cSSam Leffler 			ni->ni_chan = c;
1224b032f27cSSam Leffler 		}
1225b032f27cSSam Leffler 	}
1226b032f27cSSam Leffler 	return ni;
1227b032f27cSSam Leffler }
1228b032f27cSSam Leffler 
1229b032f27cSSam Leffler struct ieee80211_node *
12308a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1231b032f27cSSam Leffler ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1232b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
12338a1b9b6aSSam Leffler #else
1234b032f27cSSam Leffler ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1235b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
12368a1b9b6aSSam Leffler #endif
12371a1e1d21SSam Leffler {
12381a1e1d21SSam Leffler 	struct ieee80211_node *ni;
12391a1e1d21SSam Leffler 	int hash;
12401a1e1d21SSam Leffler 
12418a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1242750d6d0cSSam Leffler 
12431a1e1d21SSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
12448a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
12451a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
12468a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
12478a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1248b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
124949a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
125049a15236SSam Leffler 			    func, line,
125149a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
12528a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
12538a1b9b6aSSam Leffler #endif
1254750d6d0cSSam Leffler 			return ni;
12551a1e1d21SSam Leffler 		}
12561a1e1d21SSam Leffler 	}
1257750d6d0cSSam Leffler 	return NULL;
1258750d6d0cSSam Leffler }
1259750d6d0cSSam Leffler 
1260750d6d0cSSam Leffler struct ieee80211_node *
12618a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
12628a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1263b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
12648a1b9b6aSSam Leffler #else
1265b032f27cSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt,
1266b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
12678a1b9b6aSSam Leffler #endif
1268750d6d0cSSam Leffler {
1269750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1270750d6d0cSSam Leffler 
12718a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1272b032f27cSSam Leffler 	ni = ieee80211_find_node_locked(nt, macaddr);
1273b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1274b032f27cSSam Leffler 	return ni;
1275b032f27cSSam Leffler }
1276b032f27cSSam Leffler 
1277b032f27cSSam Leffler struct ieee80211_node *
1278b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1279b032f27cSSam Leffler ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1280b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1281b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1282b032f27cSSam Leffler #else
1283b032f27cSSam Leffler ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1284b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1285b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1286b032f27cSSam Leffler #endif
1287b032f27cSSam Leffler {
1288b032f27cSSam Leffler 	struct ieee80211_node *ni;
1289b032f27cSSam Leffler 	int hash;
1290b032f27cSSam Leffler 
1291b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1292b032f27cSSam Leffler 
1293b032f27cSSam Leffler 	hash = IEEE80211_NODE_HASH(macaddr);
1294b032f27cSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1295b032f27cSSam Leffler 		if (ni->ni_vap == vap &&
1296b032f27cSSam Leffler 		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1297b032f27cSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
1298b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1299b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1300b032f27cSSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1301b032f27cSSam Leffler 			    func, line,
1302b032f27cSSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
1303b032f27cSSam Leffler 			    ieee80211_node_refcnt(ni));
1304b032f27cSSam Leffler #endif
1305b032f27cSSam Leffler 			return ni;
1306b032f27cSSam Leffler 		}
1307b032f27cSSam Leffler 	}
1308b032f27cSSam Leffler 	return NULL;
1309b032f27cSSam Leffler }
1310b032f27cSSam Leffler 
1311b032f27cSSam Leffler struct ieee80211_node *
1312b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1313b032f27cSSam Leffler ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1314b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1315b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1316b032f27cSSam Leffler #else
1317b032f27cSSam Leffler ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1318b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1319b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1320b032f27cSSam Leffler #endif
1321b032f27cSSam Leffler {
1322b032f27cSSam Leffler 	struct ieee80211_node *ni;
1323b032f27cSSam Leffler 
1324b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1325b032f27cSSam Leffler 	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
13268a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
13271a1e1d21SSam Leffler 	return ni;
13281a1e1d21SSam Leffler }
13291a1e1d21SSam Leffler 
13301a1e1d21SSam Leffler /*
13318a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
13328a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
13338a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
13348a1b9b6aSSam Leffler  * it's private state.
13358a1b9b6aSSam Leffler  */
13368a1b9b6aSSam Leffler struct ieee80211_node *
1337b032f27cSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
133868e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
13398a1b9b6aSSam Leffler {
13408a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
13418a1b9b6aSSam Leffler 
1342b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1343be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1344b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, macaddr);
13458a1b9b6aSSam Leffler 	if (ni != NULL) {
1346b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1347b032f27cSSam Leffler 
13488a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
1349b032f27cSSam Leffler 		ni->ni_rates = vap->iv_bss->ni_rates;
1350aa68c24fSSam Leffler 		if (ieee80211_iserp_rateset(&ni->ni_rates))
1351aa68c24fSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_ERP;
1352b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
13538e292e8eSSam Leffler 			/*
135468e8e04eSSam Leffler 			 * In adhoc demo mode there are no management
135568e8e04eSSam Leffler 			 * frames to use to discover neighbor capabilities,
135668e8e04eSSam Leffler 			 * so blindly propagate the local configuration
135768e8e04eSSam Leffler 			 * so we can do interesting things (e.g. use
135868e8e04eSSam Leffler 			 * WME to disable ACK's).
13598e292e8eSSam Leffler 			 */
1360b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_WME)
13618e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
1362616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1363b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_FF)
136468e8e04eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_FF;
1365616190d0SSam Leffler #endif
13668e292e8eSSam Leffler 		}
136701a03542SSam Leffler 		node_setuptxparms(ni);
1368b032f27cSSam Leffler 		if (ic->ic_newassoc != NULL)
1369b032f27cSSam Leffler 			ic->ic_newassoc(ni, 1);
137068e8e04eSSam Leffler 		/* XXX not right for 802.1x/WPA */
137168e8e04eSSam Leffler 		ieee80211_node_authorize(ni);
13728a1b9b6aSSam Leffler 	}
13738a1b9b6aSSam Leffler 	return ni;
13748a1b9b6aSSam Leffler }
13758a1b9b6aSSam Leffler 
1376be425a0fSSam Leffler void
1377be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
1378be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
1379be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
1380be425a0fSSam Leffler {
1381be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
1382be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1383be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1384be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1385be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
1386be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
1387be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
1388be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1389be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1390be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1391be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
1392b032f27cSSam Leffler 
1393b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1394b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
13959094ffdfSSam Leffler 		if (ni->ni_ies.wme_ie != NULL)
13969094ffdfSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
13979094ffdfSSam Leffler 		else
13989094ffdfSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1399616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1400b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
1401b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1402616190d0SSam Leffler #endif
1403b032f27cSSam Leffler 	}
1404be425a0fSSam Leffler 
1405be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
140679edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
140779edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
140879edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1409be425a0fSSam Leffler }
1410be425a0fSSam Leffler 
1411b5c99415SSam Leffler /*
1412b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1413b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1414b5c99415SSam Leffler  * benefit we we treat this like an association so the
1415b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1416b5c99415SSam Leffler  */
1417b5c99415SSam Leffler struct ieee80211_node *
1418b032f27cSSam Leffler ieee80211_add_neighbor(struct ieee80211vap *vap,
1419b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1420b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1421b5c99415SSam Leffler {
1422b5c99415SSam Leffler 	struct ieee80211_node *ni;
1423b5c99415SSam Leffler 
1424b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1425be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1426b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1427b5c99415SSam Leffler 	if (ni != NULL) {
1428b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1429b032f27cSSam Leffler 
1430be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1431aa68c24fSSam Leffler 		if (ieee80211_iserp_rateset(&ni->ni_rates))
1432aa68c24fSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_ERP;
143301a03542SSam Leffler 		node_setuptxparms(ni);
1434b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1435b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1436b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1437b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1438b5c99415SSam Leffler 	}
1439b5c99415SSam Leffler 	return ni;
1440b5c99415SSam Leffler }
1441b5c99415SSam Leffler 
1442a2cfa5b7SSam Leffler #define	IS_PROBEREQ(wh) \
1443a2cfa5b7SSam Leffler 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1444a2cfa5b7SSam Leffler 	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1445a2cfa5b7SSam Leffler #define	IS_BCAST_PROBEREQ(wh) \
1446a2cfa5b7SSam Leffler 	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1447a2cfa5b7SSam Leffler 	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1448a2cfa5b7SSam Leffler 
1449a2cfa5b7SSam Leffler static __inline struct ieee80211_node *
1450a2cfa5b7SSam Leffler _find_rxnode(struct ieee80211_node_table *nt,
1451a2cfa5b7SSam Leffler     const struct ieee80211_frame_min *wh)
1452a2cfa5b7SSam Leffler {
1453a2cfa5b7SSam Leffler 	if (IS_BCAST_PROBEREQ(wh))
1454a2cfa5b7SSam Leffler 		return NULL;		/* spam bcast probe req to all vap's */
1455a2cfa5b7SSam Leffler 	return ieee80211_find_node_locked(nt, wh->i_addr2);
1456a2cfa5b7SSam Leffler }
145768e8e04eSSam Leffler 
14588a1b9b6aSSam Leffler /*
14598a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
1460a2cfa5b7SSam Leffler  * (referenced) node up to the 802.11 layer for its use.  Note
1461a2cfa5b7SSam Leffler  * we can return NULL if the sender is not in the table.
14628a1b9b6aSSam Leffler  */
14638a1b9b6aSSam Leffler struct ieee80211_node *
14648a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
14658a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
14668a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
14678a1b9b6aSSam Leffler #else
14688a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
14698a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
14708a1b9b6aSSam Leffler #endif
14718a1b9b6aSSam Leffler {
14728a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
14738a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
14748a1b9b6aSSam Leffler 
147568e8e04eSSam Leffler 	nt = &ic->ic_sta;
14768a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1477a2cfa5b7SSam Leffler 	ni = _find_rxnode(nt, wh);
14788a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14798a1b9b6aSSam Leffler 
1480c1225b52SSam Leffler 	return ni;
1481c1225b52SSam Leffler }
1482c1225b52SSam Leffler 
1483c1225b52SSam Leffler /*
1484c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1485c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1486c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1487c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1488c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1489c1225b52SSam Leffler  * key assigned to it.
1490c1225b52SSam Leffler  */
1491c1225b52SSam Leffler struct ieee80211_node *
1492c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1493c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1494c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1495c1225b52SSam Leffler 	const char *func, int line)
1496c1225b52SSam Leffler #else
1497c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1498c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1499c1225b52SSam Leffler #endif
1500c1225b52SSam Leffler {
1501c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1502c1225b52SSam Leffler 	struct ieee80211_node *ni;
1503c1225b52SSam Leffler 
1504c1225b52SSam Leffler 	nt = &ic->ic_sta;
1505c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1506c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1507c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1508c1225b52SSam Leffler 	else
1509c1225b52SSam Leffler 		ni = NULL;
1510c1225b52SSam Leffler 	if (ni == NULL) {
1511a2cfa5b7SSam Leffler 		ni = _find_rxnode(nt, wh);
1512b032f27cSSam Leffler 		if (ni != NULL && nt->nt_keyixmap != NULL) {
1513c1225b52SSam Leffler 			/*
1514c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1515c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1516c1225b52SSam Leffler 			 */
1517c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1518c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1519c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1520c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1521b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1522b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
1523c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1524c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1525c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1526c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1527c1225b52SSam Leffler 			}
1528c1225b52SSam Leffler 		}
1529a2cfa5b7SSam Leffler 	} else {
1530a2cfa5b7SSam Leffler 		if (IS_BCAST_PROBEREQ(wh))
1531a2cfa5b7SSam Leffler 			ni = NULL;	/* spam bcast probe req to all vap's */
1532a2cfa5b7SSam Leffler 		else
1533c1225b52SSam Leffler 			ieee80211_ref_node(ni);
1534a2cfa5b7SSam Leffler 	}
1535c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1536c1225b52SSam Leffler 
1537c1225b52SSam Leffler 	return ni;
1538c1225b52SSam Leffler }
1539a2cfa5b7SSam Leffler #undef IS_BCAST_PROBEREQ
1540a2cfa5b7SSam Leffler #undef IS_PROBEREQ
15418a1b9b6aSSam Leffler 
15428a1b9b6aSSam Leffler /*
1543750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1544750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1545750d6d0cSSam Leffler  */
1546750d6d0cSSam Leffler struct ieee80211_node *
15478a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1548b032f27cSSam Leffler ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1549b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN],
15508a1b9b6aSSam Leffler 	const char *func, int line)
15518a1b9b6aSSam Leffler #else
1552b032f27cSSam Leffler ieee80211_find_txnode(struct ieee80211vap *vap,
1553b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
15548a1b9b6aSSam Leffler #endif
1555750d6d0cSSam Leffler {
1556b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1557750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1558750d6d0cSSam Leffler 
1559750d6d0cSSam Leffler 	/*
1560750d6d0cSSam Leffler 	 * The destination address should be in the node table
1561c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1562c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1563c1225b52SSam Leffler 	 * to the bss node.
1564750d6d0cSSam Leffler 	 */
1565750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
15668a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1567b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA ||
1568b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_WDS ||
1569b032f27cSSam Leffler 	    IEEE80211_IS_MULTICAST(macaddr))
1570b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
15711b999d64SSam Leffler 	else
1572b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, macaddr);
15738a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15748a1b9b6aSSam Leffler 
15758a1b9b6aSSam Leffler 	if (ni == NULL) {
1576b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1577b032f27cSSam Leffler 		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
157890d0d036SSam Leffler 			/*
157990d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
158090d0d036SSam Leffler 			 * Note that we need an additional reference for the
1581b032f27cSSam Leffler 			 * caller to be consistent with
1582b032f27cSSam Leffler 			 * ieee80211_find_node_locked.
158390d0d036SSam Leffler 			 */
1584b032f27cSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
158590d0d036SSam Leffler 			if (ni != NULL)
158690d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
158790d0d036SSam Leffler 		} else {
1588b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1589b032f27cSSam Leffler 			    "no node, discard frame (%s)", __func__);
1590b032f27cSSam Leffler 			vap->iv_stats.is_tx_nonode++;
1591750d6d0cSSam Leffler 		}
1592750d6d0cSSam Leffler 	}
1593750d6d0cSSam Leffler 	return ni;
1594750d6d0cSSam Leffler }
1595750d6d0cSSam Leffler 
15968a1b9b6aSSam Leffler static void
15978a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
15988a1b9b6aSSam Leffler {
15998a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
16008a1b9b6aSSam Leffler 
160137e9743aSSam Leffler 	/*
160237e9743aSSam Leffler 	 * NB: careful about referencing the vap as it may be
160337e9743aSSam Leffler 	 * gone if the last reference was held by a driver.
160437e9743aSSam Leffler 	 * We know the com will always be present so it's safe
160537e9743aSSam Leffler 	 * to use ni_ic below to reclaim resources.
160637e9743aSSam Leffler 	 */
160737e9743aSSam Leffler #if 0
1608b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
160949a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
161049a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
16118a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
161237e9743aSSam Leffler #endif
161337e9743aSSam Leffler 	if (ni->ni_associd != 0) {
161437e9743aSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
1615b032f27cSSam Leffler 		if (vap->iv_aid_bitmap != NULL)
1616b032f27cSSam Leffler 			IEEE80211_AID_CLR(vap, ni->ni_associd);
161737e9743aSSam Leffler 	}
16188a1b9b6aSSam Leffler 	if (nt != NULL) {
16198a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
16208a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
16218a1b9b6aSSam Leffler 	}
162237e9743aSSam Leffler 	ni->ni_ic->ic_node_free(ni);
16238a1b9b6aSSam Leffler }
16248a1b9b6aSSam Leffler 
16258a1b9b6aSSam Leffler void
16268a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
16278a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
16288a1b9b6aSSam Leffler #else
16298a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
16308a1b9b6aSSam Leffler #endif
16318a1b9b6aSSam Leffler {
16328a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
16338a1b9b6aSSam Leffler 
16348a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1635b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
163649a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
16378a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
16388a1b9b6aSSam Leffler #endif
1639c1225b52SSam Leffler 	if (nt != NULL) {
1640c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
16418a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
16428a1b9b6aSSam Leffler 			/*
1643c1225b52SSam Leffler 			 * Last reference, reclaim state.
16448a1b9b6aSSam Leffler 			 */
16458a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1646c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1647c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1648c1225b52SSam Leffler 			ieee80211_keyix keyix;
1649c1225b52SSam Leffler 			/*
1650c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1651c1225b52SSam Leffler 			 */
1652c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1653c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1654c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1655b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1656b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
1657c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1658c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1659c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1660c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
16618a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
16628a1b9b6aSSam Leffler 			}
16631a1e1d21SSam Leffler 		}
1664c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1665c1225b52SSam Leffler 	} else {
1666c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1667c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1668c1225b52SSam Leffler 	}
1669c1225b52SSam Leffler }
1670c1225b52SSam Leffler 
1671c1225b52SSam Leffler /*
1672c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1673c1225b52SSam Leffler  */
1674c1225b52SSam Leffler int
1675c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1676c1225b52SSam Leffler {
167737e9743aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
167837e9743aSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1679c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1680c1225b52SSam Leffler 	ieee80211_keyix keyix;
1681c1225b52SSam Leffler 	int isowned, status;
1682c1225b52SSam Leffler 
1683c1225b52SSam Leffler 	/*
1684c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1685c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1686c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1687c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1688c1225b52SSam Leffler 	 *
1689c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1690c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1691c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1692c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1693c1225b52SSam Leffler 	 * conditionally.
1694c1225b52SSam Leffler 	 */
1695c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1696c1225b52SSam Leffler 	if (!isowned)
1697c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
169837e9743aSSam Leffler 	nikey = NULL;
169937e9743aSSam Leffler 	status = 1;		/* NB: success */
1700ca92652aSSam Leffler 	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1701c1225b52SSam Leffler 		keyix = ni->ni_ucastkey.wk_rxkeyix;
170237e9743aSSam Leffler 		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1703c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1704c1225b52SSam Leffler 			nikey = nt->nt_keyixmap[keyix];
1705c1225b52SSam Leffler 			nt->nt_keyixmap[keyix] = NULL;;
170637e9743aSSam Leffler 		}
170737e9743aSSam Leffler 	}
1708c1225b52SSam Leffler 	if (!isowned)
1709b032f27cSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1710c1225b52SSam Leffler 
1711c1225b52SSam Leffler 	if (nikey != NULL) {
1712c1225b52SSam Leffler 		KASSERT(nikey == ni,
1713c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
171437e9743aSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1715c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1716c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1717c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1718c1225b52SSam Leffler 		ieee80211_free_node(ni);
1719c1225b52SSam Leffler 	}
1720c1225b52SSam Leffler 	return status;
1721c1225b52SSam Leffler }
17221a1e1d21SSam Leffler 
1723303ebc3cSSam Leffler /*
17248a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
17258a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
17268a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
17278a1b9b6aSSam Leffler  */
17288a1b9b6aSSam Leffler static void
17298a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
17308a1b9b6aSSam Leffler {
1731c1225b52SSam Leffler 	ieee80211_keyix keyix;
1732c1225b52SSam Leffler 
1733c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
17348a1b9b6aSSam Leffler 
1735b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
173649a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
173749a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
173849a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1739c1225b52SSam Leffler 	/*
1740c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1741c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1742c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1743c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1744c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1745c1225b52SSam Leffler 	 */
1746c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1747c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1748c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1749b032f27cSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
175073bac36fSSam Leffler 			"%s: %p<%s> clear key map entry %u\n",
175173bac36fSSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1752c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1753c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1754c1225b52SSam Leffler 	}
17558a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
17568a1b9b6aSSam Leffler 		/*
17578a1b9b6aSSam Leffler 		 * Other references are present, just remove the
17588a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
17598a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1760acc4f7f5SSam Leffler 		 * reclaimed.
17618a1b9b6aSSam Leffler 		 */
17628a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
17638a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
17648a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
17658a1b9b6aSSam Leffler 	} else
17668a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
17678a1b9b6aSSam Leffler }
17688a1b9b6aSSam Leffler 
1769b032f27cSSam Leffler /*
1770b032f27cSSam Leffler  * Node table support.
1771b032f27cSSam Leffler  */
1772b032f27cSSam Leffler 
1773b032f27cSSam Leffler static void
1774b032f27cSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
1775b032f27cSSam Leffler 	struct ieee80211_node_table *nt,
1776b032f27cSSam Leffler 	const char *name, int inact, int keyixmax)
1777b032f27cSSam Leffler {
1778b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1779b032f27cSSam Leffler 
1780b032f27cSSam Leffler 	nt->nt_ic = ic;
1781b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1782b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1783b032f27cSSam Leffler 	TAILQ_INIT(&nt->nt_node);
1784b032f27cSSam Leffler 	nt->nt_name = name;
1785b032f27cSSam Leffler 	nt->nt_scangen = 1;
1786b032f27cSSam Leffler 	nt->nt_inact_init = inact;
1787b032f27cSSam Leffler 	nt->nt_keyixmax = keyixmax;
1788b032f27cSSam Leffler 	if (nt->nt_keyixmax > 0) {
1789e2126decSSam Leffler 		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1790c5abbba3SDag-Erling Smørgrav 			keyixmax * sizeof(struct ieee80211_node *),
1791b032f27cSSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
1792b032f27cSSam Leffler 		if (nt->nt_keyixmap == NULL)
1793b032f27cSSam Leffler 			if_printf(ic->ic_ifp,
1794b032f27cSSam Leffler 			    "Cannot allocate key index map with %u entries\n",
1795b032f27cSSam Leffler 			    keyixmax);
1796b032f27cSSam Leffler 	} else
1797b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
1798b032f27cSSam Leffler }
1799b032f27cSSam Leffler 
1800b032f27cSSam Leffler static void
1801b032f27cSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1802b032f27cSSam Leffler 	struct ieee80211vap *match)
1803b032f27cSSam Leffler {
1804b032f27cSSam Leffler 	struct ieee80211_node *ni, *next;
1805b032f27cSSam Leffler 
1806b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1807b032f27cSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1808b032f27cSSam Leffler 		if (match != NULL && ni->ni_vap != match)
1809b032f27cSSam Leffler 			continue;
1810b032f27cSSam Leffler 		/* XXX can this happen?  if so need's work */
1811b032f27cSSam Leffler 		if (ni->ni_associd != 0) {
1812b032f27cSSam Leffler 			struct ieee80211vap *vap = ni->ni_vap;
1813b032f27cSSam Leffler 
1814b032f27cSSam Leffler 			if (vap->iv_auth->ia_node_leave != NULL)
1815b032f27cSSam Leffler 				vap->iv_auth->ia_node_leave(ni);
1816b032f27cSSam Leffler 			if (vap->iv_aid_bitmap != NULL)
1817b032f27cSSam Leffler 				IEEE80211_AID_CLR(vap, ni->ni_associd);
1818b032f27cSSam Leffler 		}
1819b032f27cSSam Leffler 		ni->ni_wdsvap = NULL;		/* clear reference */
18208a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
18218a1b9b6aSSam Leffler 	}
1822b032f27cSSam Leffler 	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1823b032f27cSSam Leffler 		/*
1824b032f27cSSam Leffler 		 * Make a separate pass to clear references to this vap
1825b032f27cSSam Leffler 		 * held by DWDS entries.  They will not be matched above
1826b032f27cSSam Leffler 		 * because ni_vap will point to the ap vap but we still
1827b032f27cSSam Leffler 		 * need to clear ni_wdsvap when the WDS vap is destroyed
1828b032f27cSSam Leffler 		 * and/or reset.
1829b032f27cSSam Leffler 		 */
1830b032f27cSSam Leffler 		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1831b032f27cSSam Leffler 			if (ni->ni_wdsvap == match)
1832b032f27cSSam Leffler 				ni->ni_wdsvap = NULL;
1833b032f27cSSam Leffler 	}
1834b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1835b032f27cSSam Leffler }
1836b032f27cSSam Leffler 
1837b032f27cSSam Leffler static void
1838b032f27cSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1839b032f27cSSam Leffler {
1840b032f27cSSam Leffler 	ieee80211_node_table_reset(nt, NULL);
1841b032f27cSSam Leffler 	if (nt->nt_keyixmap != NULL) {
1842b032f27cSSam Leffler #ifdef DIAGNOSTIC
1843b032f27cSSam Leffler 		/* XXX verify all entries are NULL */
1844b032f27cSSam Leffler 		int i;
1845b032f27cSSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
1846b032f27cSSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
1847b032f27cSSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
1848b032f27cSSam Leffler 					nt->nt_name, i);
1849b032f27cSSam Leffler #endif
1850e2126decSSam Leffler 		free(nt->nt_keyixmap, M_80211_NODE);
1851b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
1852b032f27cSSam Leffler 	}
1853b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1854b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
18558a1b9b6aSSam Leffler }
18568a1b9b6aSSam Leffler 
18578a1b9b6aSSam Leffler /*
18588a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
18598a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
18608a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
18618a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
18628a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
18638a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1864303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1865303ebc3cSSam Leffler  * process each node only once.
1866303ebc3cSSam Leffler  */
18678a1b9b6aSSam Leffler static void
1868b032f27cSSam Leffler ieee80211_timeout_stations(struct ieee80211com *ic)
18691a1e1d21SSam Leffler {
1870b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1871b032f27cSSam Leffler 	struct ieee80211vap *vap;
1872303ebc3cSSam Leffler 	struct ieee80211_node *ni;
1873b032f27cSSam Leffler 	int gen = 0;
18741a1e1d21SSam Leffler 
1875b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK(nt);
1876a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
1877303ebc3cSSam Leffler restart:
18788a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
18798a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1880303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1881303ebc3cSSam Leffler 			continue;
1882303ebc3cSSam Leffler 		ni->ni_scangen = gen;
18830a915fadSSam Leffler 		/*
1884ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1885ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1886ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1887ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1888ebdda46cSSam Leffler 		 */
1889b032f27cSSam Leffler 		vap = ni->ni_vap;
1890b032f27cSSam Leffler 		/*
1891b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
1892b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
1893b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
1894b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
1895b032f27cSSam Leffler 		 * it applies to more than timeout processing.
1896b032f27cSSam Leffler 		 */
1897b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
1898b032f27cSSam Leffler 			continue;
1899b032f27cSSam Leffler 		/* XXX can vap be NULL? */
1900b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1901b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
190244b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1903ebdda46cSSam Leffler 			continue;
1904ebdda46cSSam Leffler 		/*
19058a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
19068a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
1907b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_age
19080a915fadSSam Leffler 		 */
19098a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
19108a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
19118a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
19128a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
19138a1b9b6aSSam Leffler 		}
1914be1054edSSam Leffler 		if (ni->ni_inact > 0) {
1915c066143cSSam Leffler 			ni->ni_inact--;
1916be1054edSSam Leffler 			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1917be1054edSSam Leffler 			    "%s: inact %u inact_reload %u nrates %u",
1918be1054edSSam Leffler 			    __func__, ni->ni_inact, ni->ni_inact_reload,
1919be1054edSSam Leffler 			    ni->ni_rates.rs_nrates);
1920be1054edSSam Leffler 		}
1921ce647032SSam Leffler 		/*
1922ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1923ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1924b032f27cSSam Leffler 		 * XXX run ic_node_age
1925ce647032SSam Leffler 		 */
1926b032f27cSSam Leffler 		if (ni == vap->iv_bss)
1927ce647032SSam Leffler 			continue;
1928b032f27cSSam Leffler 		if (ni->ni_associd != 0 ||
1929b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1930b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
19318a1b9b6aSSam Leffler 			/*
1932b032f27cSSam Leffler 			 * Age/drain resources held by the station.
19338a1b9b6aSSam Leffler 			 */
1934b032f27cSSam Leffler 			ic->ic_node_age(ni);
19358a1b9b6aSSam Leffler 			/*
19368a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
19378a1b9b6aSSam Leffler 			 * send a null data frame which may not be
19388a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
19398a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
194068e8e04eSSam Leffler 			 *
194168e8e04eSSam Leffler 			 * XXX don't probe the station unless we've
194268e8e04eSSam Leffler 			 *     received a frame from them (and have
194368e8e04eSSam Leffler 			 *     some idea of the rates they are capable
194468e8e04eSSam Leffler 			 *     of); this will get fixed more properly
194568e8e04eSSam Leffler 			 *     soon with better handling of the rate set.
19468a1b9b6aSSam Leffler 			 */
1947b032f27cSSam Leffler 			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1948c066143cSSam Leffler 			    (0 < ni->ni_inact &&
1949b032f27cSSam Leffler 			     ni->ni_inact <= vap->iv_inact_probe) &&
195068e8e04eSSam Leffler 			    ni->ni_rates.rs_nrates != 0) {
1951b032f27cSSam Leffler 				IEEE80211_NOTE(vap,
1952f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1953f66d97f6SSam Leffler 				    ni, "%s",
1954f66d97f6SSam Leffler 				    "probe station due to inactivity");
195519ad2dd7SSam Leffler 				/*
195619ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
195719ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
195819ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
195919ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
196019ad2dd7SSam Leffler 				 * ref for us as needed.
196119ad2dd7SSam Leffler 				 */
196219ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
19638a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
1964f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
19658a1b9b6aSSam Leffler 				/* XXX stat? */
19668a1b9b6aSSam Leffler 				goto restart;
19678a1b9b6aSSam Leffler 			}
19688a1b9b6aSSam Leffler 		}
1969b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1970c066143cSSam Leffler 		    ni->ni_inact <= 0) {
1971b032f27cSSam Leffler 			IEEE80211_NOTE(vap,
1972f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1973f66d97f6SSam Leffler 			    "station timed out due to inactivity "
1974f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
19758a1b9b6aSSam Leffler 			/*
19768a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
19778a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
19788a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
19798a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
19808a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
19818a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
19828a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
19838a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
19848a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
19858a1b9b6aSSam Leffler 			 *
19868a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
198768e8e04eSSam Leffler 			 * in case the driver takes a lock, as this can result
198868e8e04eSSam Leffler 			 * in a LOR between the node lock and the driver lock.
19898a1b9b6aSSam Leffler 			 */
19905698ab1aSSam Leffler 			ieee80211_ref_node(ni);
19918a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
19928a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
1993b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
19941a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
19951a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
19968a1b9b6aSSam Leffler 			}
1997b032f27cSSam Leffler 			ieee80211_node_leave(ni);
19985698ab1aSSam Leffler 			ieee80211_free_node(ni);
1999b032f27cSSam Leffler 			vap->iv_stats.is_node_timeout++;
2000303ebc3cSSam Leffler 			goto restart;
2001303ebc3cSSam Leffler 		}
20021a1e1d21SSam Leffler 	}
20038a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
20048a1b9b6aSSam Leffler 
2005b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_UNLOCK(nt);
200668e8e04eSSam Leffler }
20078a1b9b6aSSam Leffler 
2008b032f27cSSam Leffler /*
2009b032f27cSSam Leffler  * Aggressively reclaim resources.  This should be used
2010b032f27cSSam Leffler  * only in a critical situation to reclaim mbuf resources.
2011b032f27cSSam Leffler  */
2012b032f27cSSam Leffler void
2013b032f27cSSam Leffler ieee80211_drain(struct ieee80211com *ic)
2014b032f27cSSam Leffler {
2015b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
2016b032f27cSSam Leffler 	struct ieee80211vap *vap;
2017b032f27cSSam Leffler 	struct ieee80211_node *ni;
2018b032f27cSSam Leffler 
2019b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
2020b032f27cSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2021b032f27cSSam Leffler 		/*
2022b032f27cSSam Leffler 		 * Ignore entries for which have yet to receive an
2023b032f27cSSam Leffler 		 * authentication frame.  These are transient and
2024b032f27cSSam Leffler 		 * will be reclaimed when the last reference to them
2025b032f27cSSam Leffler 		 * goes away (when frame xmits complete).
2026b032f27cSSam Leffler 		 */
2027b032f27cSSam Leffler 		vap = ni->ni_vap;
2028b032f27cSSam Leffler 		/*
2029b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
2030b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
2031b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
2032b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
2033b032f27cSSam Leffler 		 * it applies to more than timeout processing.
2034b032f27cSSam Leffler 		 */
2035b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
2036b032f27cSSam Leffler 			continue;
2037b032f27cSSam Leffler 		/* XXX can vap be NULL? */
2038b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2039b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
2040b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2041b032f27cSSam Leffler 			continue;
2042b032f27cSSam Leffler 		/*
2043b032f27cSSam Leffler 		 * Free fragments.
2044b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_drain
2045b032f27cSSam Leffler 		 */
2046b032f27cSSam Leffler 		if (ni->ni_rxfrag[0] != NULL) {
2047b032f27cSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
2048b032f27cSSam Leffler 			ni->ni_rxfrag[0] = NULL;
2049b032f27cSSam Leffler 		}
2050b032f27cSSam Leffler 		/*
2051b032f27cSSam Leffler 		 * Drain resources held by the station.
2052b032f27cSSam Leffler 		 */
2053b032f27cSSam Leffler 		ic->ic_node_drain(ni);
2054b032f27cSSam Leffler 	}
2055b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
2056b032f27cSSam Leffler }
2057b032f27cSSam Leffler 
2058b032f27cSSam Leffler /*
2059b032f27cSSam Leffler  * Per-ieee80211com inactivity timer callback.
2060b032f27cSSam Leffler  */
206168e8e04eSSam Leffler void
206268e8e04eSSam Leffler ieee80211_node_timeout(void *arg)
206368e8e04eSSam Leffler {
206468e8e04eSSam Leffler 	struct ieee80211com *ic = arg;
206568e8e04eSSam Leffler 
2066b032f27cSSam Leffler 	/*
2067b032f27cSSam Leffler 	 * Defer timeout processing if a channel switch is pending.
2068b032f27cSSam Leffler 	 * We typically need to be mute so not doing things that
2069b032f27cSSam Leffler 	 * might generate frames is good to handle in one place.
2070b032f27cSSam Leffler 	 * Supressing the station timeout processing may extend the
2071b032f27cSSam Leffler 	 * lifetime of inactive stations (by not decrementing their
2072b032f27cSSam Leffler 	 * idle counters) but this should be ok unless the CSA is
2073b032f27cSSam Leffler 	 * active for an unusually long time.
2074b032f27cSSam Leffler 	 */
2075b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
207668e8e04eSSam Leffler 		ieee80211_scan_timeout(ic);
2077b032f27cSSam Leffler 		ieee80211_timeout_stations(ic);
207868e8e04eSSam Leffler 
2079b105a069SSam Leffler 		IEEE80211_LOCK(ic);
2080b105a069SSam Leffler 		ieee80211_erp_timeout(ic);
20811b6167d2SSam Leffler 		ieee80211_ht_timeout(ic);
2082b105a069SSam Leffler 		IEEE80211_UNLOCK(ic);
2083b032f27cSSam Leffler 	}
208468e8e04eSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
208568e8e04eSSam Leffler 		ieee80211_node_timeout, ic);
20861a1e1d21SSam Leffler }
20871a1e1d21SSam Leffler 
20881a1e1d21SSam Leffler void
2089b032f27cSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2090b032f27cSSam Leffler 	ieee80211_iter_func *f, void *arg)
20911a1e1d21SSam Leffler {
20921a1e1d21SSam Leffler 	struct ieee80211_node *ni;
20938a1b9b6aSSam Leffler 	u_int gen;
20941a1e1d21SSam Leffler 
2095b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK(nt);
2096a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
20978a1b9b6aSSam Leffler restart:
20988a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
20998a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
21008a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
21018a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
21028a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
21038a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
21041a1e1d21SSam Leffler 			(*f)(arg, ni);
21058a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
21068a1b9b6aSSam Leffler 			goto restart;
21078a1b9b6aSSam Leffler 		}
21088a1b9b6aSSam Leffler 	}
21098a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
21108a1b9b6aSSam Leffler 
2111b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_UNLOCK(nt);
21128a1b9b6aSSam Leffler }
21138a1b9b6aSSam Leffler 
21148a1b9b6aSSam Leffler void
21158a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
21168a1b9b6aSSam Leffler {
21178a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
21188a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
21198a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
21208a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
21218a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
21228a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
21238a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2124801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2125801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2126801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
21278a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
21285463c4a4SSam Leffler 	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
21295463c4a4SSam Leffler 		node_getrssi(ni), ni->ni_noise,
213068e8e04eSSam Leffler 		ni->ni_intval, ni->ni_capinfo);
21318a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
21328a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
21338a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
21348a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2135be1054edSSam Leffler 	printf("\tinact %u inact_reload %u txrate %u\n",
2136be1054edSSam Leffler 		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
213768e8e04eSSam Leffler 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
213868e8e04eSSam Leffler 		ni->ni_htcap, ni->ni_htparam,
213968e8e04eSSam Leffler 		ni->ni_htctlchan, ni->ni_ht2ndchan);
214068e8e04eSSam Leffler 	printf("\thtopmode %x htstbc %x chw %u\n",
214168e8e04eSSam Leffler 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
21428a1b9b6aSSam Leffler }
21438a1b9b6aSSam Leffler 
21448a1b9b6aSSam Leffler void
21458a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
21468a1b9b6aSSam Leffler {
21478a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
21488a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
21498a1b9b6aSSam Leffler }
21508a1b9b6aSSam Leffler 
21512dc4d8dcSSam Leffler static void
21522dc4d8dcSSam Leffler ieee80211_notify_erp_locked(struct ieee80211com *ic)
2153b105a069SSam Leffler {
2154b032f27cSSam Leffler 	struct ieee80211vap *vap;
2155b032f27cSSam Leffler 
2156b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2157b032f27cSSam Leffler 
2158b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2159b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2160b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2161b105a069SSam Leffler }
2162b105a069SSam Leffler 
21632dc4d8dcSSam Leffler void
21642dc4d8dcSSam Leffler ieee80211_notify_erp(struct ieee80211com *ic)
21652dc4d8dcSSam Leffler {
21662dc4d8dcSSam Leffler 	IEEE80211_LOCK(ic);
21672dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
21682dc4d8dcSSam Leffler 	IEEE80211_UNLOCK(ic);
21692dc4d8dcSSam Leffler }
21702dc4d8dcSSam Leffler 
21718a1b9b6aSSam Leffler /*
21728a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
21738a1b9b6aSSam Leffler  */
21748a1b9b6aSSam Leffler static void
2175b032f27cSSam Leffler ieee80211_node_join_11g(struct ieee80211_node *ni)
21768a1b9b6aSSam Leffler {
2177b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
21788a1b9b6aSSam Leffler 
21791b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
21801b6167d2SSam Leffler 
21818a1b9b6aSSam Leffler 	/*
21828a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
21838a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
21848a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
21858a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
21868a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
21878a1b9b6aSSam Leffler 	 */
21888a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
21898a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
2190b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2191b105a069SSam Leffler 		    "station needs long slot time, count %d",
2192b105a069SSam Leffler 		    ic->ic_longslotsta);
21938a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
219468e8e04eSSam Leffler 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
219568e8e04eSSam Leffler 			/*
219668e8e04eSSam Leffler 			 * Don't force slot time when switched to turbo
219768e8e04eSSam Leffler 			 * mode as non-ERP stations won't be present; this
219868e8e04eSSam Leffler 			 * need only be done when on the normal G channel.
219968e8e04eSSam Leffler 			 */
22008a1b9b6aSSam Leffler 			ieee80211_set_shortslottime(ic, 0);
22018a1b9b6aSSam Leffler 		}
220268e8e04eSSam Leffler 	}
22038a1b9b6aSSam Leffler 	/*
22048a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
22058a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
22068a1b9b6aSSam Leffler 	 * if configured.
22078a1b9b6aSSam Leffler 	 */
2208b032f27cSSam Leffler 	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
22098a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
2210b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2211b105a069SSam Leffler 		    "station is !ERP, %d non-ERP stations associated",
2212b105a069SSam Leffler 		    ic->ic_nonerpsta);
22138a1b9b6aSSam Leffler 		/*
22148a1b9b6aSSam Leffler 		 * If station does not support short preamble
22158a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
22168a1b9b6aSSam Leffler 		 */
22178a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2218b032f27cSSam Leffler 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2219b105a069SSam Leffler 			    "%s", "station needs long preamble");
22208a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
22218a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
22228a1b9b6aSSam Leffler 		}
2223b105a069SSam Leffler 		/*
2224b032f27cSSam Leffler 		 * If protection is configured and this is the first
2225b032f27cSSam Leffler 		 * indication we should use protection, enable it.
2226b105a069SSam Leffler 		 */
2227b105a069SSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2228b105a069SSam Leffler 		    ic->ic_nonerpsta == 1 &&
2229b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2230b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2231b105a069SSam Leffler 			    "%s: enable use of protection\n", __func__);
2232b105a069SSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
22332dc4d8dcSSam Leffler 			ieee80211_notify_erp_locked(ic);
2234b105a069SSam Leffler 		}
22358a1b9b6aSSam Leffler 	} else
22368a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
22378a1b9b6aSSam Leffler }
22388a1b9b6aSSam Leffler 
22398a1b9b6aSSam Leffler void
2240b032f27cSSam Leffler ieee80211_node_join(struct ieee80211_node *ni, int resp)
22418a1b9b6aSSam Leffler {
2242b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2243b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
22448a1b9b6aSSam Leffler 	int newassoc;
22458a1b9b6aSSam Leffler 
22468a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
224768e8e04eSSam Leffler 		uint16_t aid;
22488a1b9b6aSSam Leffler 
2249b032f27cSSam Leffler 		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
22508a1b9b6aSSam Leffler 		/*
22518a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
22528a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
22538a1b9b6aSSam Leffler 		 */
2254b032f27cSSam Leffler 		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2255b032f27cSSam Leffler 			if (!IEEE80211_AID_ISSET(vap, aid))
22568a1b9b6aSSam Leffler 				break;
22578a1b9b6aSSam Leffler 		}
2258b032f27cSSam Leffler 		if (aid >= vap->iv_max_aid) {
225913f91245SSam Leffler 			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2260b032f27cSSam Leffler 			ieee80211_node_leave(ni);
22618a1b9b6aSSam Leffler 			return;
22628a1b9b6aSSam Leffler 		}
22638a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
22641b6167d2SSam Leffler 		ni->ni_jointime = time_uptime;
2265b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
2266b032f27cSSam Leffler 		IEEE80211_AID_SET(vap, ni->ni_associd);
2267b032f27cSSam Leffler 		vap->iv_sta_assoc++;
22688a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
22691b6167d2SSam Leffler 
22701b6167d2SSam Leffler 		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
22711b6167d2SSam Leffler 			ieee80211_ht_node_join(ni);
227268e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
227368e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2274b032f27cSSam Leffler 			ieee80211_node_join_11g(ni);
22751b6167d2SSam Leffler 		IEEE80211_UNLOCK(ic);
22761b6167d2SSam Leffler 
22771b6167d2SSam Leffler 		newassoc = 1;
22788a1b9b6aSSam Leffler 	} else
22798a1b9b6aSSam Leffler 		newassoc = 0;
22808a1b9b6aSSam Leffler 
2281b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
228244f7a6edSSam Leffler 	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2283b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
2284b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2285b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2286b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
228768e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
228868e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_HT ?
22890f52b1c4SSam Leffler 		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
22901b6167d2SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
22918c070d69SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
22928c070d69SSam Leffler 	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
229344f7a6edSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2294b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
229568e8e04eSSam Leffler 		", fast-frames" : "",
2296b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
229768e8e04eSSam Leffler 		", turbo" : ""
2298b8fcf546SSam Leffler 	);
22998a1b9b6aSSam Leffler 
230001a03542SSam Leffler 	node_setuptxparms(ni);
23018a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
2302736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
2303e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
2304b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
23058a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
2306b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_join != NULL)
2307b032f27cSSam Leffler 		vap->iv_auth->ia_node_join(ni);
2308b032f27cSSam Leffler 	ieee80211_notify_node_join(ni,
2309ce8977dfSSam Leffler 	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
23108a1b9b6aSSam Leffler }
23118a1b9b6aSSam Leffler 
2312b105a069SSam Leffler static void
2313b105a069SSam Leffler disable_protection(struct ieee80211com *ic)
2314b105a069SSam Leffler {
2315b105a069SSam Leffler 	KASSERT(ic->ic_nonerpsta == 0 &&
2316b105a069SSam Leffler 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2317b105a069SSam Leffler 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2318b105a069SSam Leffler 	   ic->ic_flags_ext));
2319b105a069SSam Leffler 
2320b105a069SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2321b105a069SSam Leffler 	/* XXX verify mode? */
2322b105a069SSam Leffler 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2323b105a069SSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2324b105a069SSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2325b105a069SSam Leffler 	}
23262dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
2327b105a069SSam Leffler }
2328b105a069SSam Leffler 
23298a1b9b6aSSam Leffler /*
23308a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
23318a1b9b6aSSam Leffler  */
23328a1b9b6aSSam Leffler static void
2333b032f27cSSam Leffler ieee80211_node_leave_11g(struct ieee80211_node *ni)
23348a1b9b6aSSam Leffler {
2335b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
23368a1b9b6aSSam Leffler 
23371b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
23381b6167d2SSam Leffler 
233968e8e04eSSam Leffler 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2340b032f27cSSam Leffler 	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2341b032f27cSSam Leffler 	      ic->ic_bsschan->ic_flags));
23428a1b9b6aSSam Leffler 
23438a1b9b6aSSam Leffler 	/*
23448a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
23458a1b9b6aSSam Leffler 	 */
23468a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
23478a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
23488a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
23498a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
2350b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2351b105a069SSam Leffler 		    "long slot time station leaves, count now %d",
2352b105a069SSam Leffler 		    ic->ic_longslotsta);
23538a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
23548a1b9b6aSSam Leffler 			/*
23558a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
23568a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
23578a1b9b6aSSam Leffler 			 */
23588a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
23598a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
2360b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
2361b032f27cSSam Leffler 				    IEEE80211_MSG_ASSOC,
23628a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
23638a1b9b6aSSam Leffler 				    __func__);
23648a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
23658a1b9b6aSSam Leffler 			}
23668a1b9b6aSSam Leffler 		}
23678a1b9b6aSSam Leffler 	}
23688a1b9b6aSSam Leffler 	/*
23698a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
23708a1b9b6aSSam Leffler 	 */
23718a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
23728a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
23738a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
23748a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
2375b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2376b105a069SSam Leffler 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2377b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2378b105a069SSam Leffler 			" (non-ERP sta present)" : "");
2379b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0 &&
2380b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2381b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
23828a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
2383b105a069SSam Leffler 			disable_protection(ic);
2384b105a069SSam Leffler 		}
2385b105a069SSam Leffler 	}
2386b105a069SSam Leffler }
2387b105a069SSam Leffler 
2388b105a069SSam Leffler /*
2389b105a069SSam Leffler  * Time out presence of an overlapping bss with non-ERP
2390b105a069SSam Leffler  * stations.  When operating in hostap mode we listen for
2391b105a069SSam Leffler  * beacons from other stations and if we identify a non-ERP
2392b105a069SSam Leffler  * station is present we enable protection.  To identify
2393b105a069SSam Leffler  * when all non-ERP stations are gone we time out this
2394b105a069SSam Leffler  * condition.
2395b105a069SSam Leffler  */
2396b105a069SSam Leffler static void
2397b105a069SSam Leffler ieee80211_erp_timeout(struct ieee80211com *ic)
2398b105a069SSam Leffler {
2399b105a069SSam Leffler 
2400b105a069SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2401b105a069SSam Leffler 
2402b105a069SSam Leffler 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2403b105a069SSam Leffler 	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2404b032f27cSSam Leffler #if 0
2405b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2406b032f27cSSam Leffler 		    "%s", "age out non-ERP sta present on channel");
2407b032f27cSSam Leffler #endif
2408b105a069SSam Leffler 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2409b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0)
2410b105a069SSam Leffler 			disable_protection(ic);
24118a1b9b6aSSam Leffler 	}
24128a1b9b6aSSam Leffler }
24138a1b9b6aSSam Leffler 
24148a1b9b6aSSam Leffler /*
24158a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
24168a1b9b6aSSam Leffler  * when operating as an ap.
24178a1b9b6aSSam Leffler  */
24188a1b9b6aSSam Leffler void
2419b032f27cSSam Leffler ieee80211_node_leave(struct ieee80211_node *ni)
24208a1b9b6aSSam Leffler {
2421b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2422b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
242344acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
24248a1b9b6aSSam Leffler 
2425b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2426b105a069SSam Leffler 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
24278a1b9b6aSSam Leffler 
2428b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2429b032f27cSSam Leffler 		("unexpected operating mode %u", vap->iv_opmode));
24308a1b9b6aSSam Leffler 	/*
24318a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
24328a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
24338a1b9b6aSSam Leffler 	 */
24348a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
24358a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
24368a1b9b6aSSam Leffler 		goto done;
24378a1b9b6aSSam Leffler 	/*
24388a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
24398a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
24408a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
24418a1b9b6aSSam Leffler 	 * associd to locate it's state block.
24428a1b9b6aSSam Leffler 	 */
2443b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_leave != NULL)
2444b032f27cSSam Leffler 		vap->iv_auth->ia_node_leave(ni);
24451b6167d2SSam Leffler 
24461b6167d2SSam Leffler 	IEEE80211_LOCK(ic);
2447b032f27cSSam Leffler 	IEEE80211_AID_CLR(vap, ni->ni_associd);
24488a1b9b6aSSam Leffler 	ni->ni_associd = 0;
2449b032f27cSSam Leffler 	vap->iv_sta_assoc--;
24508a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
24518a1b9b6aSSam Leffler 
24521b6167d2SSam Leffler 	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
24531b6167d2SSam Leffler 		ieee80211_ht_node_leave(ni);
245468e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
245568e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2456b032f27cSSam Leffler 		ieee80211_node_leave_11g(ni);
24571b6167d2SSam Leffler 	IEEE80211_UNLOCK(ic);
24588a1b9b6aSSam Leffler 	/*
24598a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
24608a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
24618a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
24628a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
24638a1b9b6aSSam Leffler 	 */
2464b032f27cSSam Leffler 	ieee80211_sta_leave(ni);
24658a1b9b6aSSam Leffler done:
246644acc00dSSam Leffler 	/*
246744acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
246844acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
246944acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
247044acc00dSSam Leffler 	 * for inactivity.
247144acc00dSSam Leffler 	 */
247244acc00dSSam Leffler 	if (nt != NULL) {
247344acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
247444acc00dSSam Leffler 		node_reclaim(nt, ni);
247544acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
247644acc00dSSam Leffler 	} else
24778a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
24788a1b9b6aSSam Leffler }
24798a1b9b6aSSam Leffler 
2480b032f27cSSam Leffler struct rssiinfo {
2481b032f27cSSam Leffler 	struct ieee80211vap *vap;
2482b032f27cSSam Leffler 	int	rssi_samples;
2483b032f27cSSam Leffler 	uint32_t rssi_total;
2484b032f27cSSam Leffler };
2485b032f27cSSam Leffler 
2486b032f27cSSam Leffler static void
2487b032f27cSSam Leffler get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2488b032f27cSSam Leffler {
2489b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2490b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2491b032f27cSSam Leffler 	int8_t rssi;
2492b032f27cSSam Leffler 
2493b032f27cSSam Leffler 	if (info->vap != vap)
2494b032f27cSSam Leffler 		return;
2495b032f27cSSam Leffler 	/* only associated stations */
2496b032f27cSSam Leffler 	if (ni->ni_associd == 0)
2497b032f27cSSam Leffler 		return;
2498b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
2499b032f27cSSam Leffler 	if (rssi != 0) {
2500b032f27cSSam Leffler 		info->rssi_samples++;
2501b032f27cSSam Leffler 		info->rssi_total += rssi;
2502b032f27cSSam Leffler 	}
2503b032f27cSSam Leffler }
2504b032f27cSSam Leffler 
2505b032f27cSSam Leffler static void
2506b032f27cSSam Leffler get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2507b032f27cSSam Leffler {
2508b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2509b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2510b032f27cSSam Leffler 	int8_t rssi;
2511b032f27cSSam Leffler 
2512b032f27cSSam Leffler 	if (info->vap != vap)
2513b032f27cSSam Leffler 		return;
2514b032f27cSSam Leffler 	/* only neighbors */
2515b032f27cSSam Leffler 	/* XXX check bssid */
2516b032f27cSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2517b032f27cSSam Leffler 		return;
2518b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
2519b032f27cSSam Leffler 	if (rssi != 0) {
2520b032f27cSSam Leffler 		info->rssi_samples++;
2521b032f27cSSam Leffler 		info->rssi_total += rssi;
2522b032f27cSSam Leffler 	}
2523b032f27cSSam Leffler }
2524b032f27cSSam Leffler 
252568e8e04eSSam Leffler int8_t
2526b032f27cSSam Leffler ieee80211_getrssi(struct ieee80211vap *vap)
25278a1b9b6aSSam Leffler {
25288a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
2529b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2530b032f27cSSam Leffler 	struct rssiinfo info;
25318a1b9b6aSSam Leffler 
2532b032f27cSSam Leffler 	info.rssi_total = 0;
2533b032f27cSSam Leffler 	info.rssi_samples = 0;
2534b032f27cSSam Leffler 	info.vap = vap;
2535b032f27cSSam Leffler 	switch (vap->iv_opmode) {
25368a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
25378a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2538b032f27cSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2539b032f27cSSam Leffler 		break;
25408a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2541b032f27cSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
25428a1b9b6aSSam Leffler 		break;
25438a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
25448a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
25458a1b9b6aSSam Leffler 	default:
2546b032f27cSSam Leffler 		if (vap->iv_bss != NULL)
2547b032f27cSSam Leffler 			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2548b032f27cSSam Leffler 		info.rssi_samples = 1;
25498a1b9b6aSSam Leffler 		break;
25508a1b9b6aSSam Leffler 	}
2551b032f27cSSam Leffler 	return info.rssi_total / NZ(info.rssi_samples);
25528a1b9b6aSSam Leffler #undef NZ
25538a1b9b6aSSam Leffler }
25548a1b9b6aSSam Leffler 
255568e8e04eSSam Leffler void
2556b032f27cSSam Leffler ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
25578a1b9b6aSSam Leffler {
25588a1b9b6aSSam Leffler 
2559b032f27cSSam Leffler 	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
256068e8e04eSSam Leffler 		return;
2561b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
256268e8e04eSSam Leffler 	/* for non-station mode return avg'd rssi accounting */
2563b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA)
2564b032f27cSSam Leffler 		*rssi = ieee80211_getrssi(vap);
25658a1b9b6aSSam Leffler }
2566