xref: /freebsd/sys/net80211/ieee80211_node.c (revision ea3d5fd9)
11a1e1d21SSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
47535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
510ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
61a1e1d21SSam Leffler  * All rights reserved.
71a1e1d21SSam Leffler  *
81a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
91a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
101a1e1d21SSam Leffler  * are met:
111a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
137535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
147535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
157535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
161a1e1d21SSam Leffler  *
177535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271a1e1d21SSam Leffler  */
281a1e1d21SSam Leffler 
291a1e1d21SSam Leffler #include <sys/cdefs.h>
301a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
311a1e1d21SSam Leffler 
32b032f27cSSam Leffler #include "opt_wlan.h"
33b032f27cSSam Leffler 
341a1e1d21SSam Leffler #include <sys/param.h>
351a1e1d21SSam Leffler #include <sys/systm.h>
361a1e1d21SSam Leffler #include <sys/mbuf.h>
371a1e1d21SSam Leffler #include <sys/malloc.h>
381a1e1d21SSam Leffler #include <sys/kernel.h>
391a1e1d21SSam Leffler 
408a1b9b6aSSam Leffler #include <sys/socket.h>
411a1e1d21SSam Leffler 
421a1e1d21SSam Leffler #include <net/if.h>
4376039bc8SGleb Smirnoff #include <net/if_var.h>
441a1e1d21SSam Leffler #include <net/if_media.h>
451a1e1d21SSam Leffler #include <net/ethernet.h>
461a1e1d21SSam Leffler 
471a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
48b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
49616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
50616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
51616190d0SSam Leffler #endif
5210ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
5310ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
5410ad9a77SSam Leffler #endif
55b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
5659aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
57b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
5851172f62SAdrian Chadd #include <net80211/ieee80211_vht.h>
591a1e1d21SSam Leffler 
601a1e1d21SSam Leffler #include <net/bpf.h>
611a1e1d21SSam Leffler 
627268fa64SSam Leffler /*
6359aa14a9SRui Paulo  * IEEE80211_NODE_HASHSIZE must be a power of 2.
6459aa14a9SRui Paulo  */
6559aa14a9SRui Paulo CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
6659aa14a9SRui Paulo 
6759aa14a9SRui Paulo /*
687268fa64SSam Leffler  * Association id's are managed with a bit vector.
697268fa64SSam Leffler  */
70b032f27cSSam Leffler #define	IEEE80211_AID_SET(_vap, b) \
71b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
72b032f27cSSam Leffler 		(1 << (IEEE80211_AID(b) % 32)))
73b032f27cSSam Leffler #define	IEEE80211_AID_CLR(_vap, b) \
74b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
75b032f27cSSam Leffler 		~(1 << (IEEE80211_AID(b) % 32)))
76b032f27cSSam Leffler #define	IEEE80211_AID_ISSET(_vap, b) \
77b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
787268fa64SSam Leffler 
7968e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *);
8068e8e04eSSam Leffler 
8138c208f8SSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211vap *,
8238c208f8SSam Leffler 	const uint8_t [IEEE80211_ADDR_LEN]);
83ea3d5fd9SAdrian Chadd static int node_init(struct ieee80211_node *);
848a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
858a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
86b032f27cSSam Leffler static void node_age(struct ieee80211_node *);
8768e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *);
8868e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
89b032f27cSSam Leffler static void node_getmimoinfo(const struct ieee80211_node *,
90b032f27cSSam Leffler 	struct ieee80211_mimo_info *);
911a1e1d21SSam Leffler 
928a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
938a1b9b6aSSam Leffler 
948a055831SAdrian Chadd static void node_reclaim(struct ieee80211_node_table *nt,
958a055831SAdrian Chadd 	struct ieee80211_node *ni);
968a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
97c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
9868e8e04eSSam Leffler 	int inact, int keymaxix);
99b032f27cSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *,
100b032f27cSSam Leffler 	struct ieee80211vap *);
1018a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
102b105a069SSam Leffler static void ieee80211_erp_timeout(struct ieee80211com *);
1031a1e1d21SSam Leffler 
10432346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
105b032f27cSSam Leffler MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
10637c150c4SSam Leffler 
1071a1e1d21SSam Leffler void
1088a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
1091a1e1d21SSam Leffler {
1105b16c28cSSam Leffler 	/* XXX really want maxlen enforced per-sta */
1115b16c28cSSam Leffler 	ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
1125b16c28cSSam Leffler 	    "802.11 staging q");
113b032f27cSSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
114b032f27cSSam Leffler 		IEEE80211_INACT_INIT, ic->ic_max_keyix);
115fd90e2edSJung-uk Kim 	callout_init(&ic->ic_inact, 1);
116b032f27cSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
117b032f27cSSam Leffler 		ieee80211_node_timeout, ic);
1181a1e1d21SSam Leffler 
1198a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
120ea3d5fd9SAdrian Chadd 	ic->ic_node_init = node_init;
1218a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
1228a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
123b032f27cSSam Leffler 	ic->ic_node_age = node_age;
124b032f27cSSam Leffler 	ic->ic_node_drain = node_age;		/* NB: same as age */
1258a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
12668e8e04eSSam Leffler 	ic->ic_node_getsignal = node_getsignal;
127b032f27cSSam Leffler 	ic->ic_node_getmimoinfo = node_getmimoinfo;
1288a1b9b6aSSam Leffler 
129b032f27cSSam Leffler 	/*
130b032f27cSSam Leffler 	 * Set flags to be propagated to all vap's;
131b032f27cSSam Leffler 	 * these define default behaviour/configuration.
132b032f27cSSam Leffler 	 */
133c066143cSSam Leffler 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
134c1225b52SSam Leffler }
135c1225b52SSam Leffler 
136c1225b52SSam Leffler void
1378a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1381a1e1d21SSam Leffler {
1391a1e1d21SSam Leffler 
140b032f27cSSam Leffler 	callout_drain(&ic->ic_inact);
141acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
14258a7c4bfSKyle Evans 	ieee80211_ageq_drain(&ic->ic_stageq);
1435b16c28cSSam Leffler 	ieee80211_ageq_cleanup(&ic->ic_stageq);
144b032f27cSSam Leffler }
145b032f27cSSam Leffler 
146b032f27cSSam Leffler void
147b032f27cSSam Leffler ieee80211_node_vattach(struct ieee80211vap *vap)
148b032f27cSSam Leffler {
149b032f27cSSam Leffler 	/* NB: driver can override */
150b032f27cSSam Leffler 	vap->iv_max_aid = IEEE80211_AID_DEF;
151b032f27cSSam Leffler 
152b032f27cSSam Leffler 	/* default station inactivity timer setings */
153b032f27cSSam Leffler 	vap->iv_inact_init = IEEE80211_INACT_INIT;
154b032f27cSSam Leffler 	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
155b032f27cSSam Leffler 	vap->iv_inact_run = IEEE80211_INACT_RUN;
156b032f27cSSam Leffler 	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
157be1054edSSam Leffler 
158be1054edSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
159be1054edSSam Leffler 	    "%s: init %u auth %u run %u probe %u\n", __func__,
160be1054edSSam Leffler 	    vap->iv_inact_init, vap->iv_inact_auth,
161be1054edSSam Leffler 	    vap->iv_inact_run, vap->iv_inact_probe);
162b032f27cSSam Leffler }
163b032f27cSSam Leffler 
164b032f27cSSam Leffler void
165b032f27cSSam Leffler ieee80211_node_latevattach(struct ieee80211vap *vap)
166b032f27cSSam Leffler {
167b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
168b032f27cSSam Leffler 		/* XXX should we allow max aid to be zero? */
169b032f27cSSam Leffler 		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
170b032f27cSSam Leffler 			vap->iv_max_aid = IEEE80211_AID_MIN;
171b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
172b032f27cSSam Leffler 			    "WARNING: max aid too small, changed to %d\n",
173b032f27cSSam Leffler 			    vap->iv_max_aid);
174b032f27cSSam Leffler 		}
175b9b53389SAdrian Chadd 		vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
176c5abbba3SDag-Erling Smørgrav 			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
177b9b53389SAdrian Chadd 			M_80211_NODE,
178b9b53389SAdrian Chadd 			IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
179b032f27cSSam Leffler 		if (vap->iv_aid_bitmap == NULL) {
180b032f27cSSam Leffler 			/* XXX no way to recover */
181b032f27cSSam Leffler 			printf("%s: no memory for AID bitmap, max aid %d!\n",
182b032f27cSSam Leffler 			    __func__, vap->iv_max_aid);
183b032f27cSSam Leffler 			vap->iv_max_aid = 0;
184b032f27cSSam Leffler 		}
185b032f27cSSam Leffler 	}
186b032f27cSSam Leffler 
187b032f27cSSam Leffler 	ieee80211_reset_bss(vap);
188b032f27cSSam Leffler 
189b032f27cSSam Leffler 	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
190b032f27cSSam Leffler }
191b032f27cSSam Leffler 
192b032f27cSSam Leffler void
193b032f27cSSam Leffler ieee80211_node_vdetach(struct ieee80211vap *vap)
194b032f27cSSam Leffler {
195b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
196b032f27cSSam Leffler 
197b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
198b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
199b032f27cSSam Leffler 		ieee80211_free_node(vap->iv_bss);
200b032f27cSSam Leffler 		vap->iv_bss = NULL;
201b032f27cSSam Leffler 	}
202b032f27cSSam Leffler 	if (vap->iv_aid_bitmap != NULL) {
203b9b53389SAdrian Chadd 		IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
204b032f27cSSam Leffler 		vap->iv_aid_bitmap = NULL;
2058a1b9b6aSSam Leffler 	}
2068a1b9b6aSSam Leffler }
2078a1b9b6aSSam Leffler 
2088a1b9b6aSSam Leffler /*
2098a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
2108a1b9b6aSSam Leffler  */
2118a1b9b6aSSam Leffler 
2128a1b9b6aSSam Leffler void
213e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
2148a1b9b6aSSam Leffler {
215be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
216be1054edSSam Leffler 
2178a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
218be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_run;
219c066143cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
220be1054edSSam Leffler 
221be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
222be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
2238a1b9b6aSSam Leffler }
2248a1b9b6aSSam Leffler 
2258a1b9b6aSSam Leffler void
226e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2278a1b9b6aSSam Leffler {
228be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
229be1054edSSam Leffler 
2308a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
231be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_auth;
232c066143cSSam Leffler 	if (ni->ni_inact > ni->ni_inact_reload)
233c066143cSSam Leffler 		ni->ni_inact = ni->ni_inact_reload;
234be1054edSSam Leffler 
235be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
236be1054edSSam Leffler 	    "%s: inact_reload %u inact %u", __func__,
237be1054edSSam Leffler 	    ni->ni_inact_reload, ni->ni_inact);
2388a1b9b6aSSam Leffler }
2398a1b9b6aSSam Leffler 
2408a1b9b6aSSam Leffler /*
24101a03542SSam Leffler  * Fix tx parameters for a node according to ``association state''.
24201a03542SSam Leffler  */
243d77148fbSSam Leffler void
244d77148fbSSam Leffler ieee80211_node_setuptxparms(struct ieee80211_node *ni)
24501a03542SSam Leffler {
24601a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
247f76cde95SSam Leffler 	enum ieee80211_phymode mode;
24801a03542SSam Leffler 
249f6b98645SAndriy Voskoboinyk 	if (ni->ni_flags & IEEE80211_NODE_VHT) {
250f6b98645SAndriy Voskoboinyk 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
251f6b98645SAndriy Voskoboinyk 			mode = IEEE80211_MODE_VHT_5GHZ;
252f6b98645SAndriy Voskoboinyk 		else
253f6b98645SAndriy Voskoboinyk 			mode = IEEE80211_MODE_VHT_2GHZ;
254f6b98645SAndriy Voskoboinyk 	} else if (ni->ni_flags & IEEE80211_NODE_HT) {
25501a03542SSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
256f76cde95SSam Leffler 			mode = IEEE80211_MODE_11NA;
25701a03542SSam Leffler 		else
258f76cde95SSam Leffler 			mode = IEEE80211_MODE_11NG;
25901a03542SSam Leffler 	} else {				/* legacy rate handling */
260f76cde95SSam Leffler 		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
261f76cde95SSam Leffler 			mode = IEEE80211_MODE_STURBO_A;
2626a76ae21SSam Leffler 		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
2636a76ae21SSam Leffler 			mode = IEEE80211_MODE_HALF;
2646a76ae21SSam Leffler 		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
2656a76ae21SSam Leffler 			mode = IEEE80211_MODE_QUARTER;
266c5262b82SSam Leffler 		/* NB: 108A should be handled as 11a */
267f76cde95SSam Leffler 		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
268f76cde95SSam Leffler 			mode = IEEE80211_MODE_11A;
269c5262b82SSam Leffler 		else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
270c5262b82SSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_ERP))
271f76cde95SSam Leffler 			mode = IEEE80211_MODE_11G;
27201a03542SSam Leffler 		else
273f76cde95SSam Leffler 			mode = IEEE80211_MODE_11B;
27401a03542SSam Leffler 	}
275f76cde95SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
27601a03542SSam Leffler }
27701a03542SSam Leffler 
27801a03542SSam Leffler /*
2798a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2808a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
281b032f27cSSam Leffler  * XXX should be private but hostap needs it to deal with CSA
2828a1b9b6aSSam Leffler  */
283b032f27cSSam Leffler void
284b032f27cSSam Leffler ieee80211_node_set_chan(struct ieee80211_node *ni,
285b032f27cSSam Leffler 	struct ieee80211_channel *chan)
2868a1b9b6aSSam Leffler {
287b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
28801a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
28901a03542SSam Leffler 	enum ieee80211_phymode mode;
29068e8e04eSSam Leffler 
291b032f27cSSam Leffler 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
292b032f27cSSam Leffler 
2938a1b9b6aSSam Leffler 	ni->ni_chan = chan;
29401a03542SSam Leffler 	mode = ieee80211_chan2mode(chan);
29568e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(chan)) {
29668e8e04eSSam Leffler 		/*
297597029bfSBernhard Schmidt 		 * We must install the legacy rate est in ni_rates and the
29868e8e04eSSam Leffler 		 * HT rate set in ni_htrates.
29968e8e04eSSam Leffler 		 */
30068e8e04eSSam Leffler 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
30101a03542SSam Leffler 		/*
30201a03542SSam Leffler 		 * Setup bss tx parameters based on operating mode.  We
30301a03542SSam Leffler 		 * use legacy rates when operating in a mixed HT+non-HT bss
30401a03542SSam Leffler 		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
30501a03542SSam Leffler 		 */
30601a03542SSam Leffler 		if (mode == IEEE80211_MODE_11NA &&
3072bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
30801a03542SSam Leffler 			mode = IEEE80211_MODE_11A;
30901a03542SSam Leffler 		else if (mode == IEEE80211_MODE_11NG &&
3102bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
31101a03542SSam Leffler 			mode = IEEE80211_MODE_11G;
31201a03542SSam Leffler 		if (mode == IEEE80211_MODE_11G &&
31301a03542SSam Leffler 		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
31401a03542SSam Leffler 			mode = IEEE80211_MODE_11B;
31568e8e04eSSam Leffler 	}
31601a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
31741b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
3181a1e1d21SSam Leffler }
3191a1e1d21SSam Leffler 
320f9cd9174SSam Leffler static __inline void
321f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
322f9cd9174SSam Leffler {
323f9cd9174SSam Leffler 	/* propagate useful state */
324f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
325f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
326f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
327f9cd9174SSam Leffler 	/* XXX statistics? */
328b032f27cSSam Leffler 	/* XXX legacy WDS bssid? */
329f9cd9174SSam Leffler }
330f9cd9174SSam Leffler 
3311a1e1d21SSam Leffler void
332b032f27cSSam Leffler ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
3331a1e1d21SSam Leffler {
334b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3351a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3368a1b9b6aSSam Leffler 
337b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
338869897d2SAdrian Chadd 		"%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
33959aa14a9SRui Paulo 		ieee80211_opmode_name[vap->iv_opmode],
3402b8b8ae8SAdrian Chadd 		ieee80211_chan2ieee(ic, chan),
341869897d2SAdrian Chadd 		ieee80211_channel_type_char(chan),
342869897d2SAdrian Chadd 		chan->ic_flags);
3438a1b9b6aSSam Leffler 
344b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
345acc4f7f5SSam Leffler 	if (ni == NULL) {
346acc4f7f5SSam Leffler 		/* XXX recovery? */
3478a1b9b6aSSam Leffler 		return;
3488a1b9b6aSSam Leffler 	}
349b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
350b032f27cSSam Leffler 	ni->ni_esslen = vap->iv_des_ssid[0].len;
351b032f27cSSam Leffler 	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
352b032f27cSSam Leffler 	if (vap->iv_bss != NULL)
353b032f27cSSam Leffler 		copy_bss(ni, vap->iv_bss);
354d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
355b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
3561a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3571a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3581a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3591a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3601a1e1d21SSam Leffler 	}
361b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
3628a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
363b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
364b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
365fe49f061SSam Leffler 		else {
366fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
367fe49f061SSam Leffler 			/* clear group bit, add local bit */
368fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
369fe49f061SSam Leffler 		}
370b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
371b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
372b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
37350d8b493SSam Leffler 		else
37410ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
37510ad9a77SSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
37610ad9a77SSam Leffler #endif
37750d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
37859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
37959aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
38059aa14a9SRui Paulo 		ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
38159aa14a9SRui Paulo 		memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
38259aa14a9SRui Paulo #endif
3838a1b9b6aSSam Leffler 	}
3848a1b9b6aSSam Leffler 	/*
3858a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3868a1b9b6aSSam Leffler 	 */
387b032f27cSSam Leffler 	/* clear DFS CAC state on previous channel */
388b032f27cSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
389b032f27cSSam Leffler 	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
390b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
391b032f27cSSam Leffler 		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
39268e8e04eSSam Leffler 	ic->ic_bsschan = chan;
393b032f27cSSam Leffler 	ieee80211_node_set_chan(ni, chan);
39468e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(chan);
3958a1b9b6aSSam Leffler 	/*
396b032f27cSSam Leffler 	 * Do mode-specific setup.
3978a1b9b6aSSam Leffler 	 */
39868e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan)) {
39968e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
40068e8e04eSSam Leffler 			/*
401b032f27cSSam Leffler 			 * Use a mixed 11b/11g basic rate set.
40268e8e04eSSam Leffler 			 */
403b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
40468e8e04eSSam Leffler 			    IEEE80211_MODE_11G);
405b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PUREG) {
406b032f27cSSam Leffler 				/*
407b032f27cSSam Leffler 				 * Also mark OFDM rates basic so 11b
408b032f27cSSam Leffler 				 * stations do not join (WiFi compliance).
409b032f27cSSam Leffler 				 */
410b032f27cSSam Leffler 				ieee80211_addbasicrates(&ni->ni_rates,
411b032f27cSSam Leffler 				    IEEE80211_MODE_11A);
412b032f27cSSam Leffler 			}
41368e8e04eSSam Leffler 		} else if (IEEE80211_IS_CHAN_B(chan)) {
41468e8e04eSSam Leffler 			/*
41568e8e04eSSam Leffler 			 * Force pure 11b rate set.
41668e8e04eSSam Leffler 			 */
417b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
41868e8e04eSSam Leffler 				IEEE80211_MODE_11B);
41968e8e04eSSam Leffler 		}
4201a1e1d21SSam Leffler 	}
4211a1e1d21SSam Leffler 
422869897d2SAdrian Chadd 	/* XXX TODO: other bits and pieces - eg fast-frames? */
423869897d2SAdrian Chadd 
424869897d2SAdrian Chadd 	/* If we're an 11n channel then initialise the 11n bits */
42551172f62SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
42651172f62SAdrian Chadd 		/* XXX what else? */
42751172f62SAdrian Chadd 		ieee80211_ht_node_init(ni);
42851172f62SAdrian Chadd 		ieee80211_vht_node_init(ni);
42951172f62SAdrian Chadd 	} else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
430869897d2SAdrian Chadd 		/* XXX what else? */
431869897d2SAdrian Chadd 		ieee80211_ht_node_init(ni);
432869897d2SAdrian Chadd 	}
433869897d2SAdrian Chadd 
43468e8e04eSSam Leffler 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
43568e8e04eSSam Leffler }
43668e8e04eSSam Leffler 
43768e8e04eSSam Leffler /*
43868e8e04eSSam Leffler  * Reset bss state on transition to the INIT state.
43968e8e04eSSam Leffler  * Clear any stations from the table (they have been
44068e8e04eSSam Leffler  * deauth'd) and reset the bss node (clears key, rate
44168e8e04eSSam Leffler  * etc. state).
44268e8e04eSSam Leffler  */
4438a1b9b6aSSam Leffler void
444b032f27cSSam Leffler ieee80211_reset_bss(struct ieee80211vap *vap)
445b4c5a90fSSam Leffler {
446b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4478a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4488a1b9b6aSSam Leffler 
449b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
450b032f27cSSam Leffler 	/* XXX multi-bss: wrong */
451d20ff6e6SAdrian Chadd 	ieee80211_vap_reset_erp(vap);
452acc4f7f5SSam Leffler 
453b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
45448e1bda0SRui Paulo 	KASSERT(ni != NULL, ("unable to setup initial BSS node"));
455b032f27cSSam Leffler 	obss = vap->iv_bss;
456b032f27cSSam Leffler 	vap->iv_bss = ieee80211_ref_node(ni);
457f9cd9174SSam Leffler 	if (obss != NULL) {
458f9cd9174SSam Leffler 		copy_bss(ni, obss);
459d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
4608a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
461b032f27cSSam Leffler 	} else
462b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
463f9cd9174SSam Leffler }
4648a1b9b6aSSam Leffler 
4658a1b9b6aSSam Leffler static int
46668e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni,
46768e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
4688a1b9b6aSSam Leffler {
46968e8e04eSSam Leffler 	int i;
47068e8e04eSSam Leffler 
47168e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
47268e8e04eSSam Leffler 		if (ni->ni_esslen == ssids[i].len &&
47368e8e04eSSam Leffler 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
47468e8e04eSSam Leffler 			return 1;
47568e8e04eSSam Leffler 	}
47668e8e04eSSam Leffler 	return 0;
47768e8e04eSSam Leffler }
47868e8e04eSSam Leffler 
47968e8e04eSSam Leffler /*
48068e8e04eSSam Leffler  * Test a node for suitability/compatibility.
48168e8e04eSSam Leffler  */
48268e8e04eSSam Leffler static int
483b032f27cSSam Leffler check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
48468e8e04eSSam Leffler {
485b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
48668e8e04eSSam Leffler         uint8_t rate;
48768e8e04eSSam Leffler 
48868e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
48968e8e04eSSam Leffler 		return 0;
490b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
49168e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
49268e8e04eSSam Leffler 			return 0;
49368e8e04eSSam Leffler 	} else {
49468e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
49568e8e04eSSam Leffler 			return 0;
49668e8e04eSSam Leffler 	}
497b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
49868e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
49968e8e04eSSam Leffler 			return 0;
50068e8e04eSSam Leffler 	} else {
50168e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
50268e8e04eSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
50368e8e04eSSam Leffler 			return 0;
50468e8e04eSSam Leffler 	}
50568e8e04eSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
50668e8e04eSSam Leffler 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
50768e8e04eSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
50868e8e04eSSam Leffler 		return 0;
509b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
510b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
51168e8e04eSSam Leffler 		return 0;
512b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
513b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
51468e8e04eSSam Leffler 		return 0;
51568e8e04eSSam Leffler 	return 1;
51668e8e04eSSam Leffler }
51768e8e04eSSam Leffler 
51868e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
51968e8e04eSSam Leffler /*
52068e8e04eSSam Leffler  * Display node suitability/compatibility.
52168e8e04eSSam Leffler  */
52268e8e04eSSam Leffler static void
523b032f27cSSam Leffler check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
52468e8e04eSSam Leffler {
525b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
52668e8e04eSSam Leffler         uint8_t rate;
527b4c5a90fSSam Leffler         int fail;
528b4c5a90fSSam Leffler 
529b4c5a90fSSam Leffler 	fail = 0;
530b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
531b4c5a90fSSam Leffler 		fail |= 0x01;
532b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
533b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
534b4c5a90fSSam Leffler 			fail |= 0x02;
535b4c5a90fSSam Leffler 	} else {
536b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
537b4c5a90fSSam Leffler 			fail |= 0x02;
538b4c5a90fSSam Leffler 	}
539b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
540b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
541b4c5a90fSSam Leffler 			fail |= 0x04;
542b4c5a90fSSam Leffler 	} else {
543b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
544b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
545b4c5a90fSSam Leffler 			fail |= 0x04;
546b4c5a90fSSam Leffler 	}
54770e28b9aSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
54879edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
549b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
550b4c5a90fSSam Leffler 		fail |= 0x08;
551b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
552b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
553b4c5a90fSSam Leffler 		fail |= 0x10;
554b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
555b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
556b4c5a90fSSam Leffler 		fail |= 0x20;
55768e8e04eSSam Leffler 
55868e8e04eSSam Leffler 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
55968e8e04eSSam Leffler 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
56068e8e04eSSam Leffler 	printf(" %3d%c",
56168e8e04eSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
562b4c5a90fSSam Leffler 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
563b4c5a90fSSam Leffler 	    fail & 0x08 ? '!' : ' ');
564b4c5a90fSSam Leffler 	printf(" %4s%c",
565b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
566b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
567b4c5a90fSSam Leffler 	    "????",
568b4c5a90fSSam Leffler 	    fail & 0x02 ? '!' : ' ');
569b4c5a90fSSam Leffler 	printf(" %3s%c ",
57068e8e04eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
571b4c5a90fSSam Leffler 	    fail & 0x04 ? '!' : ' ');
572b4c5a90fSSam Leffler 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
573b4c5a90fSSam Leffler 	printf("%s\n", fail & 0x10 ? "!" : "");
574b4c5a90fSSam Leffler }
57568e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
5768a1b9b6aSSam Leffler 
577adad5b45SAdrian Chadd 
578adad5b45SAdrian Chadd int
579adad5b45SAdrian Chadd ieee80211_ibss_merge_check(struct ieee80211_node *ni)
580adad5b45SAdrian Chadd {
581adad5b45SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
582adad5b45SAdrian Chadd 
583adad5b45SAdrian Chadd 	if (ni == vap->iv_bss ||
584adad5b45SAdrian Chadd 	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
585adad5b45SAdrian Chadd 		/* unchanged, nothing to do */
586adad5b45SAdrian Chadd 		return 0;
587adad5b45SAdrian Chadd 	}
588adad5b45SAdrian Chadd 
589adad5b45SAdrian Chadd 	if (!check_bss(vap, ni)) {
590adad5b45SAdrian Chadd 		/* capabilities mismatch */
591adad5b45SAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
592adad5b45SAdrian Chadd 		    "%s: merge failed, capabilities mismatch\n", __func__);
593adad5b45SAdrian Chadd #ifdef IEEE80211_DEBUG
594adad5b45SAdrian Chadd 		if (ieee80211_msg_assoc(vap))
595adad5b45SAdrian Chadd 			check_bss_debug(vap, ni);
596adad5b45SAdrian Chadd #endif
597adad5b45SAdrian Chadd 		vap->iv_stats.is_ibss_capmismatch++;
598adad5b45SAdrian Chadd 		return 0;
599adad5b45SAdrian Chadd 	}
600adad5b45SAdrian Chadd 
601adad5b45SAdrian Chadd 	return 1;
602adad5b45SAdrian Chadd }
603adad5b45SAdrian Chadd 
604750d6d0cSSam Leffler /*
605172b009aSAdrian Chadd  * Check if the given node should populate the node table.
606172b009aSAdrian Chadd  *
607172b009aSAdrian Chadd  * We need to be in "see all beacons for all ssids" mode in order
608172b009aSAdrian Chadd  * to do IBSS merges, however this means we will populate nodes for
609172b009aSAdrian Chadd  * /all/ IBSS SSIDs, versus just the one we care about.
610172b009aSAdrian Chadd  *
611172b009aSAdrian Chadd  * So this check ensures the node can actually belong to our IBSS
612172b009aSAdrian Chadd  * configuration.  For now it simply checks the SSID.
613172b009aSAdrian Chadd  */
614172b009aSAdrian Chadd int
615172b009aSAdrian Chadd ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
616172b009aSAdrian Chadd     const struct ieee80211_scanparams *scan)
617172b009aSAdrian Chadd {
618172b009aSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
619172b009aSAdrian Chadd 	int i;
620172b009aSAdrian Chadd 
621172b009aSAdrian Chadd 	/*
622172b009aSAdrian Chadd 	 * If we have no SSID and no scan SSID, return OK.
623172b009aSAdrian Chadd 	 */
624172b009aSAdrian Chadd 	if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
625172b009aSAdrian Chadd 		goto ok;
626172b009aSAdrian Chadd 
627172b009aSAdrian Chadd 	/*
628172b009aSAdrian Chadd 	 * If we have one of (SSID, scan SSID) then return error.
629172b009aSAdrian Chadd 	 */
630172b009aSAdrian Chadd 	if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
631172b009aSAdrian Chadd 		goto mismatch;
632172b009aSAdrian Chadd 
633172b009aSAdrian Chadd 	/*
634172b009aSAdrian Chadd 	 * Double-check - we need scan SSID.
635172b009aSAdrian Chadd 	 */
636172b009aSAdrian Chadd 	if (scan->ssid == NULL)
637172b009aSAdrian Chadd 		goto mismatch;
638172b009aSAdrian Chadd 
639172b009aSAdrian Chadd 	/*
640172b009aSAdrian Chadd 	 * Check if the scan SSID matches the SSID list for the VAP.
641172b009aSAdrian Chadd 	 */
642172b009aSAdrian Chadd 	for (i = 0; i < vap->iv_des_nssid; i++) {
643172b009aSAdrian Chadd 
644172b009aSAdrian Chadd 		/* Sanity length check */
645172b009aSAdrian Chadd 		if (vap->iv_des_ssid[i].len != scan->ssid[1])
646172b009aSAdrian Chadd 			continue;
647172b009aSAdrian Chadd 
648172b009aSAdrian Chadd 		/* Note: SSID in the scan entry is the IE format */
649172b009aSAdrian Chadd 		if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
650172b009aSAdrian Chadd 		    vap->iv_des_ssid[i].len) == 0)
651172b009aSAdrian Chadd 			goto ok;
652172b009aSAdrian Chadd 	}
653172b009aSAdrian Chadd 
654172b009aSAdrian Chadd mismatch:
655172b009aSAdrian Chadd 	return (0);
656172b009aSAdrian Chadd ok:
657172b009aSAdrian Chadd 	return (1);
658172b009aSAdrian Chadd }
659172b009aSAdrian Chadd 
660172b009aSAdrian Chadd /*
6618a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
6628a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
6638a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
6648a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
6658a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
6668a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
6678a1b9b6aSSam Leffler  *
6688a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
6698a1b9b6aSSam Leffler  *
6708a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
671750d6d0cSSam Leffler  */
6728a1b9b6aSSam Leffler int
673641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
6748a1b9b6aSSam Leffler {
675b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
676c89e0d15SBjoern A. Zeeb 	struct ieee80211vap *vap = ni->ni_vap;
677641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
678b032f27cSSam Leffler #endif
6798a1b9b6aSSam Leffler 
680adad5b45SAdrian Chadd 	if (! ieee80211_ibss_merge_check(ni))
6818a1b9b6aSSam Leffler 		return 0;
682adad5b45SAdrian Chadd 
683b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
6848a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
6858a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
6868a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
687d20ff6e6SAdrian Chadd 		vap->iv_flags&IEEE80211_F_SHSLOT ? "short" : "long",
6888a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
6898a1b9b6aSSam Leffler 	);
69068e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
6918a1b9b6aSSam Leffler }
6928a1b9b6aSSam Leffler 
6938a1b9b6aSSam Leffler /*
694b032f27cSSam Leffler  * Calculate HT channel promotion flags for all vaps.
695b032f27cSSam Leffler  * This assumes ni_chan have been setup for each vap.
696b032f27cSSam Leffler  */
697b032f27cSSam Leffler static int
698b032f27cSSam Leffler gethtadjustflags(struct ieee80211com *ic)
699b032f27cSSam Leffler {
700b032f27cSSam Leffler 	struct ieee80211vap *vap;
701b032f27cSSam Leffler 	int flags;
702b032f27cSSam Leffler 
703b032f27cSSam Leffler 	flags = 0;
704b032f27cSSam Leffler 	/* XXX locking */
705b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
706b032f27cSSam Leffler 		if (vap->iv_state < IEEE80211_S_RUN)
707b032f27cSSam Leffler 			continue;
708b032f27cSSam Leffler 		switch (vap->iv_opmode) {
709b032f27cSSam Leffler 		case IEEE80211_M_WDS:
710b032f27cSSam Leffler 		case IEEE80211_M_STA:
711b032f27cSSam Leffler 		case IEEE80211_M_AHDEMO:
712b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
713b032f27cSSam Leffler 		case IEEE80211_M_IBSS:
71459aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
715b032f27cSSam Leffler 			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
716b032f27cSSam Leffler 			break;
717b032f27cSSam Leffler 		default:
718b032f27cSSam Leffler 			break;
719b032f27cSSam Leffler 		}
720b032f27cSSam Leffler 	}
721b032f27cSSam Leffler 	return flags;
722b032f27cSSam Leffler }
723b032f27cSSam Leffler 
724b032f27cSSam Leffler /*
72551172f62SAdrian Chadd  * Calculate VHT channel promotion flags for all vaps.
72651172f62SAdrian Chadd  * This assumes ni_chan have been setup for each vap.
72751172f62SAdrian Chadd  */
72851172f62SAdrian Chadd static int
72951172f62SAdrian Chadd getvhtadjustflags(struct ieee80211com *ic)
73051172f62SAdrian Chadd {
73151172f62SAdrian Chadd 	struct ieee80211vap *vap;
73251172f62SAdrian Chadd 	int flags;
73351172f62SAdrian Chadd 
73451172f62SAdrian Chadd 	flags = 0;
73551172f62SAdrian Chadd 	/* XXX locking */
73651172f62SAdrian Chadd 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
73751172f62SAdrian Chadd 		if (vap->iv_state < IEEE80211_S_RUN)
73851172f62SAdrian Chadd 			continue;
73951172f62SAdrian Chadd 		switch (vap->iv_opmode) {
74051172f62SAdrian Chadd 		case IEEE80211_M_WDS:
74151172f62SAdrian Chadd 		case IEEE80211_M_STA:
74251172f62SAdrian Chadd 		case IEEE80211_M_AHDEMO:
74351172f62SAdrian Chadd 		case IEEE80211_M_HOSTAP:
74451172f62SAdrian Chadd 		case IEEE80211_M_IBSS:
74551172f62SAdrian Chadd 		case IEEE80211_M_MBSS:
74651172f62SAdrian Chadd 			flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
74751172f62SAdrian Chadd 			break;
74851172f62SAdrian Chadd 		default:
74951172f62SAdrian Chadd 			break;
75051172f62SAdrian Chadd 		}
75151172f62SAdrian Chadd 	}
75251172f62SAdrian Chadd 	return flags;
75351172f62SAdrian Chadd }
75451172f62SAdrian Chadd 
75551172f62SAdrian Chadd /*
756b032f27cSSam Leffler  * Check if the current channel needs to change based on whether
7575d44f8c0SSam Leffler  * any vap's are using HT20/HT40.  This is used to sync the state
7585d44f8c0SSam Leffler  * of ic_curchan after a channel width change on a running vap.
75951172f62SAdrian Chadd  *
76051172f62SAdrian Chadd  * Same applies for VHT.
7611b6167d2SSam Leffler  */
7621b6167d2SSam Leffler void
763b032f27cSSam Leffler ieee80211_sync_curchan(struct ieee80211com *ic)
7641b6167d2SSam Leffler {
765b032f27cSSam Leffler 	struct ieee80211_channel *c;
766b032f27cSSam Leffler 
767b032f27cSSam Leffler 	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
76851172f62SAdrian Chadd 	c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
76951172f62SAdrian Chadd 
770b032f27cSSam Leffler 	if (c != ic->ic_curchan) {
771b032f27cSSam Leffler 		ic->ic_curchan = c;
772b032f27cSSam Leffler 		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
77326d39e2cSSam Leffler 		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
7745efea30fSAndrew Thompson 		IEEE80211_UNLOCK(ic);
775b032f27cSSam Leffler 		ic->ic_set_channel(ic);
7765463c4a4SSam Leffler 		ieee80211_radiotap_chan_change(ic);
7775efea30fSAndrew Thompson 		IEEE80211_LOCK(ic);
778b032f27cSSam Leffler 	}
779b032f27cSSam Leffler }
780b032f27cSSam Leffler 
781b032f27cSSam Leffler /*
7825efea30fSAndrew Thompson  * Setup the current channel.  The request channel may be
783b032f27cSSam Leffler  * promoted if other vap's are operating with HT20/HT40.
784b032f27cSSam Leffler  */
785b032f27cSSam Leffler void
7865efea30fSAndrew Thompson ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
787b032f27cSSam Leffler {
788b032f27cSSam Leffler 	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
789b032f27cSSam Leffler 		int flags = gethtadjustflags(ic);
790b032f27cSSam Leffler 		/*
791b032f27cSSam Leffler 		 * Check for channel promotion required to support the
792b032f27cSSam Leffler 		 * set of running vap's.  This assumes we are called
793b032f27cSSam Leffler 		 * after ni_chan is setup for each vap.
794b032f27cSSam Leffler 		 */
79551172f62SAdrian Chadd 		/* XXX VHT? */
7962bfc8a91SSam Leffler 		/* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
797b032f27cSSam Leffler 		if (flags > ieee80211_htchanflags(c))
798b032f27cSSam Leffler 			c = ieee80211_ht_adjust_channel(ic, c, flags);
799b032f27cSSam Leffler 	}
80051172f62SAdrian Chadd 
80151172f62SAdrian Chadd 	/*
80251172f62SAdrian Chadd 	 * VHT promotion - this will at least promote to VHT20/40
80351172f62SAdrian Chadd 	 * based on what HT has done; it may further promote the
80451172f62SAdrian Chadd 	 * channel to VHT80 or above.
80551172f62SAdrian Chadd 	 */
80651172f62SAdrian Chadd 	if (ic->ic_vhtcaps != 0) {
80751172f62SAdrian Chadd 		int flags = getvhtadjustflags(ic);
80851172f62SAdrian Chadd 		if (flags > ieee80211_vhtchanflags(c))
80951172f62SAdrian Chadd 			c = ieee80211_vht_adjust_channel(ic, c, flags);
81051172f62SAdrian Chadd 	}
81151172f62SAdrian Chadd 
812b032f27cSSam Leffler 	ic->ic_bsschan = ic->ic_curchan = c;
8131b6167d2SSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
81426d39e2cSSam Leffler 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
8155efea30fSAndrew Thompson }
8165efea30fSAndrew Thompson 
8175efea30fSAndrew Thompson /*
8185efea30fSAndrew Thompson  * Change the current channel.  The channel change is guaranteed to have
8195efea30fSAndrew Thompson  * happened before the next state change.
8205efea30fSAndrew Thompson  */
8215efea30fSAndrew Thompson void
8225efea30fSAndrew Thompson ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
8235efea30fSAndrew Thompson {
8245efea30fSAndrew Thompson 	ieee80211_setupcurchan(ic, c);
8255efea30fSAndrew Thompson 	ieee80211_runtask(ic, &ic->ic_chan_task);
8261b6167d2SSam Leffler }
8271b6167d2SSam Leffler 
828b94299c4SAdrian Chadd void
829b94299c4SAdrian Chadd ieee80211_update_chw(struct ieee80211com *ic)
830b94299c4SAdrian Chadd {
831b94299c4SAdrian Chadd 
832b94299c4SAdrian Chadd 	ieee80211_setupcurchan(ic, ic->ic_curchan);
833b94299c4SAdrian Chadd 	ieee80211_runtask(ic, &ic->ic_chw_task);
834b94299c4SAdrian Chadd }
835b94299c4SAdrian Chadd 
8361b6167d2SSam Leffler /*
8378a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
8388a1b9b6aSSam Leffler  * be passed in with a held reference.
8398a1b9b6aSSam Leffler  */
84068e8e04eSSam Leffler static int
84168e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs)
8428a1b9b6aSSam Leffler {
843b032f27cSSam Leffler 	struct ieee80211vap *vap = selbs->ni_vap;
84468e8e04eSSam Leffler 	struct ieee80211com *ic = selbs->ni_ic;
8458a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
84668e8e04eSSam Leffler 	int canreassoc;
8478a1b9b6aSSam Leffler 
8488a1b9b6aSSam Leffler 	/*
8498a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
8508a1b9b6aSSam Leffler 	 */
851b032f27cSSam Leffler 	obss = vap->iv_bss;
85268e8e04eSSam Leffler 	/*
85368e8e04eSSam Leffler 	 * Check if old+new node have the same address in which
85468e8e04eSSam Leffler 	 * case we can reassociate when operating in sta mode.
85568e8e04eSSam Leffler 	 */
85668e8e04eSSam Leffler 	canreassoc = (obss != NULL &&
857b032f27cSSam Leffler 		vap->iv_state == IEEE80211_S_RUN &&
85868e8e04eSSam Leffler 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
859b032f27cSSam Leffler 	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
8601fd2349dSSam Leffler 	if (obss != NULL) {
8618a055831SAdrian Chadd 		struct ieee80211_node_table *nt = obss->ni_table;
8628a055831SAdrian Chadd 
8631fd2349dSSam Leffler 		copy_bss(selbs, obss);
86447a7b0faSSam Leffler 		ieee80211_node_decref(obss);	/* iv_bss reference */
8658a055831SAdrian Chadd 
8668a055831SAdrian Chadd 		IEEE80211_NODE_LOCK(nt);
8678a055831SAdrian Chadd 		node_reclaim(nt, obss);		/* station table reference */
8688a055831SAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
8698a055831SAdrian Chadd 
870b032f27cSSam Leffler 		obss = NULL;		/* NB: guard against later use */
8711fd2349dSSam Leffler 	}
87279edaebfSSam Leffler 
87379edaebfSSam Leffler 	/*
87479edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
87579edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
87679edaebfSSam Leffler 	 */
877b032f27cSSam Leffler 	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
87870e28b9aSSam Leffler 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
87979edaebfSSam Leffler 
880b032f27cSSam Leffler 	ieee80211_setcurchan(ic, selbs->ni_chan);
8818a1b9b6aSSam Leffler 	/*
8828a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
8838a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
8848a1b9b6aSSam Leffler 	 * mode is locked.
8858a1b9b6aSSam Leffler 	 */
886d20ff6e6SAdrian Chadd 	ieee80211_vap_reset_erp(vap);
887b032f27cSSam Leffler 	ieee80211_wme_initparams(vap);
888acc4f7f5SSam Leffler 
889b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
89068e8e04eSSam Leffler 		if (canreassoc) {
89168e8e04eSSam Leffler 			/* Reassociate */
892b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
89368e8e04eSSam Leffler 		} else {
89468e8e04eSSam Leffler 			/*
89568e8e04eSSam Leffler 			 * Act as if we received a DEAUTH frame in case we
89668e8e04eSSam Leffler 			 * are invoked from the RUN state.  This will cause
89768e8e04eSSam Leffler 			 * us to try to re-authenticate if we are operating
89868e8e04eSSam Leffler 			 * as a station.
89968e8e04eSSam Leffler 			 */
900b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_AUTH,
90168e8e04eSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH);
90268e8e04eSSam Leffler 		}
90368e8e04eSSam Leffler 	} else
904b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
9058a1b9b6aSSam Leffler 	return 1;
9068a1b9b6aSSam Leffler }
9078a1b9b6aSSam Leffler 
90868e8e04eSSam Leffler int
90910959256SSam Leffler ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
91068e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se)
91168e8e04eSSam Leffler {
912b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
91368e8e04eSSam Leffler 	struct ieee80211_node *ni;
91451172f62SAdrian Chadd 	int do_ht = 0;
91568e8e04eSSam Leffler 
916b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
91768e8e04eSSam Leffler 	if (ni == NULL) {
91868e8e04eSSam Leffler 		/* XXX msg */
91968e8e04eSSam Leffler 		return 0;
92068e8e04eSSam Leffler 	}
921d71a1f7aSAdrian Chadd 
92268e8e04eSSam Leffler 	/*
92368e8e04eSSam Leffler 	 * Expand scan state into node's format.
92468e8e04eSSam Leffler 	 * XXX may not need all this stuff
92568e8e04eSSam Leffler 	 */
92668e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
92768e8e04eSSam Leffler 	ni->ni_esslen = se->se_ssid[1];
92868e8e04eSSam Leffler 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
92968e8e04eSSam Leffler 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
93068e8e04eSSam Leffler 	ni->ni_intval = se->se_intval;
93168e8e04eSSam Leffler 	ni->ni_capinfo = se->se_capinfo;
93210959256SSam Leffler 	ni->ni_chan = chan;
93368e8e04eSSam Leffler 	ni->ni_timoff = se->se_timoff;
93468e8e04eSSam Leffler 	ni->ni_fhdwell = se->se_fhdwell;
93568e8e04eSSam Leffler 	ni->ni_fhindex = se->se_fhindex;
93668e8e04eSSam Leffler 	ni->ni_erp = se->se_erp;
937b032f27cSSam Leffler 	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
93868e8e04eSSam Leffler 	ni->ni_noise = se->se_noise;
9396ca74c40SSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
9406ca74c40SSam Leffler 		/* NB: only infrastructure mode requires an associd */
9411b999d64SSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
9426ca74c40SSam Leffler 	}
94368e8e04eSSam Leffler 
944b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
945b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
946616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
947b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
948b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
949616190d0SSam Leffler #endif
950b032f27cSSam Leffler 		if (ni->ni_ies.htcap_ie != NULL)
951b032f27cSSam Leffler 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
952b032f27cSSam Leffler 		if (ni->ni_ies.htinfo_ie != NULL)
953b032f27cSSam Leffler 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
95459aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
95559aa14a9SRui Paulo 		if (ni->ni_ies.meshid_ie != NULL)
95659aa14a9SRui Paulo 			ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
95759aa14a9SRui Paulo #endif
95810ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
95910ad9a77SSam Leffler 		if (ni->ni_ies.tdma_ie != NULL)
96010ad9a77SSam Leffler 			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
96110ad9a77SSam Leffler #endif
96251172f62SAdrian Chadd 		if (ni->ni_ies.vhtcap_ie != NULL)
96351172f62SAdrian Chadd 			ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
96451172f62SAdrian Chadd 		if (ni->ni_ies.vhtopmode_ie != NULL)
96551172f62SAdrian Chadd 			ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
96674a54be9SAdrian Chadd 
96774a54be9SAdrian Chadd 		/* XXX parse BSSLOAD IE */
96851172f62SAdrian Chadd 		/* XXX parse TXPWRENV IE */
96974a54be9SAdrian Chadd 		/* XXX parse APCHANREP IE */
970b032f27cSSam Leffler 	}
971b032f27cSSam Leffler 
972b032f27cSSam Leffler 	vap->iv_dtim_period = se->se_dtimperiod;
973b032f27cSSam Leffler 	vap->iv_dtim_count = 0;
97468e8e04eSSam Leffler 
97568e8e04eSSam Leffler 	/* NB: must be after ni_chan is setup */
97668e8e04eSSam Leffler 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
97768e8e04eSSam Leffler 		IEEE80211_F_DOSORT);
978dfcd1f4dSSam Leffler 	if (ieee80211_iserp_rateset(&ni->ni_rates))
979dfcd1f4dSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
980d71a1f7aSAdrian Chadd 
981d71a1f7aSAdrian Chadd 	/*
982d71a1f7aSAdrian Chadd 	 * Setup HT state for this node if it's available, otherwise
983d71a1f7aSAdrian Chadd 	 * non-STA modes won't pick this state up.
984d71a1f7aSAdrian Chadd 	 *
985d71a1f7aSAdrian Chadd 	 * For IBSS and related modes that don't go through an
986d71a1f7aSAdrian Chadd 	 * association request/response, the only appropriate place
987d71a1f7aSAdrian Chadd 	 * to setup the HT state is here.
988d71a1f7aSAdrian Chadd 	 */
989d71a1f7aSAdrian Chadd 	if (ni->ni_ies.htinfo_ie != NULL &&
990d71a1f7aSAdrian Chadd 	    ni->ni_ies.htcap_ie != NULL &&
991d71a1f7aSAdrian Chadd 	    vap->iv_flags_ht & IEEE80211_FHT_HT) {
992d71a1f7aSAdrian Chadd 		ieee80211_ht_node_init(ni);
993d71a1f7aSAdrian Chadd 		ieee80211_ht_updateparams(ni,
994d71a1f7aSAdrian Chadd 		    ni->ni_ies.htcap_ie,
995d71a1f7aSAdrian Chadd 		    ni->ni_ies.htinfo_ie);
99651172f62SAdrian Chadd 		do_ht = 1;
99751172f62SAdrian Chadd 	}
99851172f62SAdrian Chadd 
99951172f62SAdrian Chadd 	/*
100051172f62SAdrian Chadd 	 * Setup VHT state for this node if it's available.
100151172f62SAdrian Chadd 	 * Same as the above.
100251172f62SAdrian Chadd 	 *
100351172f62SAdrian Chadd 	 * For now, don't allow 2GHz VHT operation.
100451172f62SAdrian Chadd 	 */
100551172f62SAdrian Chadd 	if (ni->ni_ies.vhtopmode_ie != NULL &&
100651172f62SAdrian Chadd 	    ni->ni_ies.vhtcap_ie != NULL &&
100751172f62SAdrian Chadd 	    vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
100851172f62SAdrian Chadd 		if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
100951172f62SAdrian Chadd 			printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
101051172f62SAdrian Chadd 			    __func__,
101151172f62SAdrian Chadd 			    ni->ni_macaddr,
101251172f62SAdrian Chadd 			    ":");
101351172f62SAdrian Chadd 		} else {
101451172f62SAdrian Chadd 			ieee80211_vht_node_init(ni);
101551172f62SAdrian Chadd 			ieee80211_vht_updateparams(ni,
101651172f62SAdrian Chadd 			    ni->ni_ies.vhtcap_ie,
101751172f62SAdrian Chadd 			    ni->ni_ies.vhtopmode_ie);
101851172f62SAdrian Chadd 			ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
101951172f62SAdrian Chadd 			    ni->ni_ies.vhtopmode_ie);
102051172f62SAdrian Chadd 			do_ht = 1;
102151172f62SAdrian Chadd 		}
102251172f62SAdrian Chadd 	}
102351172f62SAdrian Chadd 
102451172f62SAdrian Chadd 	/* Finally do the node channel change */
102551172f62SAdrian Chadd 	if (do_ht) {
102651172f62SAdrian Chadd 		ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
102751172f62SAdrian Chadd 		    ni->ni_ies.htinfo_ie);
1028d71a1f7aSAdrian Chadd 		ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
1029d71a1f7aSAdrian Chadd 		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1030d71a1f7aSAdrian Chadd 		ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
1031d71a1f7aSAdrian Chadd 	}
103251172f62SAdrian Chadd 
1033d71a1f7aSAdrian Chadd 	/* XXX else check for ath FF? */
1034d71a1f7aSAdrian Chadd 	/* XXX QoS? Difficult given that WME config is specific to a master */
1035d71a1f7aSAdrian Chadd 
1036d77148fbSSam Leffler 	ieee80211_node_setuptxparms(ni);
103749d2c137SBernhard Schmidt 	ieee80211_ratectl_node_init(ni);
103868e8e04eSSam Leffler 
103968e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
104068e8e04eSSam Leffler }
104168e8e04eSSam Leffler 
10428a1b9b6aSSam Leffler /*
10438a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
10448a1b9b6aSSam Leffler  * be passed in with a held reference.
10458a1b9b6aSSam Leffler  */
10468a1b9b6aSSam Leffler void
1047b032f27cSSam Leffler ieee80211_sta_leave(struct ieee80211_node *ni)
10488a1b9b6aSSam Leffler {
1049b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1050b032f27cSSam Leffler 
10518a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
1052b032f27cSSam Leffler 	ieee80211_notify_node_leave(ni);
1053b032f27cSSam Leffler }
1054b032f27cSSam Leffler 
1055b032f27cSSam Leffler /*
1056b032f27cSSam Leffler  * Send a deauthenticate frame and drop the station.
1057b032f27cSSam Leffler  */
1058b032f27cSSam Leffler void
1059b032f27cSSam Leffler ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
1060b032f27cSSam Leffler {
1061eca3b4fcSAdrian Chadd 	/* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
1062b032f27cSSam Leffler 	ieee80211_ref_node(ni);
1063b032f27cSSam Leffler 	if (ni->ni_associd != 0)
1064b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
1065b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1066b032f27cSSam Leffler 	ieee80211_free_node(ni);
10678a1b9b6aSSam Leffler }
10688a1b9b6aSSam Leffler 
10691a1e1d21SSam Leffler static struct ieee80211_node *
107038c208f8SSam Leffler node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
10711a1e1d21SSam Leffler {
1072410ca74bSSam Leffler 	struct ieee80211_node *ni;
10738a1b9b6aSSam Leffler 
1074b9b53389SAdrian Chadd 	ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
1075b9b53389SAdrian Chadd 		M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1076410ca74bSSam Leffler 	return ni;
10771a1e1d21SSam Leffler }
10781a1e1d21SSam Leffler 
1079ea3d5fd9SAdrian Chadd static int
1080ea3d5fd9SAdrian Chadd node_init(struct ieee80211_node *ni)
1081ea3d5fd9SAdrian Chadd {
1082ea3d5fd9SAdrian Chadd 	return 0;
1083ea3d5fd9SAdrian Chadd }
1084ea3d5fd9SAdrian Chadd 
10858a1b9b6aSSam Leffler /*
1086b032f27cSSam Leffler  * Initialize an ie blob with the specified data.  If previous
1087b032f27cSSam Leffler  * data exists re-use the data block.  As a side effect we clear
1088b032f27cSSam Leffler  * all references to specific ie's; the caller is required to
1089b032f27cSSam Leffler  * recalculate them.
1090b032f27cSSam Leffler  */
1091b032f27cSSam Leffler int
1092b032f27cSSam Leffler ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
1093b032f27cSSam Leffler {
1094b032f27cSSam Leffler 	/* NB: assumes data+len are the last fields */
1095b032f27cSSam Leffler 	memset(ies, 0, offsetof(struct ieee80211_ies, data));
1096b032f27cSSam Leffler 	if (ies->data != NULL && ies->len != len) {
1097b032f27cSSam Leffler 		/* data size changed */
1098b9b53389SAdrian Chadd 		IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1099b032f27cSSam Leffler 		ies->data = NULL;
1100b032f27cSSam Leffler 	}
1101b032f27cSSam Leffler 	if (ies->data == NULL) {
1102b9b53389SAdrian Chadd 		ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
1103b9b53389SAdrian Chadd 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1104b032f27cSSam Leffler 		if (ies->data == NULL) {
1105b032f27cSSam Leffler 			ies->len = 0;
1106b032f27cSSam Leffler 			/* NB: pointers have already been zero'd above */
1107b032f27cSSam Leffler 			return 0;
1108b032f27cSSam Leffler 		}
1109b032f27cSSam Leffler 	}
1110b032f27cSSam Leffler 	memcpy(ies->data, data, len);
1111b032f27cSSam Leffler 	ies->len = len;
1112b032f27cSSam Leffler 	return 1;
1113b032f27cSSam Leffler }
1114b032f27cSSam Leffler 
1115b032f27cSSam Leffler /*
1116b032f27cSSam Leffler  * Reclaim storage for an ie blob.
1117b032f27cSSam Leffler  */
1118b032f27cSSam Leffler void
1119b032f27cSSam Leffler ieee80211_ies_cleanup(struct ieee80211_ies *ies)
1120b032f27cSSam Leffler {
1121b032f27cSSam Leffler 	if (ies->data != NULL)
1122b9b53389SAdrian Chadd 		IEEE80211_FREE(ies->data, M_80211_NODE_IE);
1123b032f27cSSam Leffler }
1124b032f27cSSam Leffler 
1125b032f27cSSam Leffler /*
1126b032f27cSSam Leffler  * Expand an ie blob data contents and to fillin individual
1127b032f27cSSam Leffler  * ie pointers.  The data blob is assumed to be well-formed;
1128b032f27cSSam Leffler  * we don't do any validity checking of ie lengths.
1129b032f27cSSam Leffler  */
1130b032f27cSSam Leffler void
1131b032f27cSSam Leffler ieee80211_ies_expand(struct ieee80211_ies *ies)
1132b032f27cSSam Leffler {
1133b032f27cSSam Leffler 	uint8_t *ie;
1134b032f27cSSam Leffler 	int ielen;
1135b032f27cSSam Leffler 
1136b032f27cSSam Leffler 	ie = ies->data;
1137b032f27cSSam Leffler 	ielen = ies->len;
1138b032f27cSSam Leffler 	while (ielen > 0) {
1139b032f27cSSam Leffler 		switch (ie[0]) {
1140b032f27cSSam Leffler 		case IEEE80211_ELEMID_VENDOR:
1141b032f27cSSam Leffler 			if (iswpaoui(ie))
1142b032f27cSSam Leffler 				ies->wpa_ie = ie;
1143b032f27cSSam Leffler 			else if (iswmeoui(ie))
1144b032f27cSSam Leffler 				ies->wme_ie = ie;
1145616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1146b032f27cSSam Leffler 			else if (isatherosoui(ie))
1147b032f27cSSam Leffler 				ies->ath_ie = ie;
1148616190d0SSam Leffler #endif
114910ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
115010ad9a77SSam Leffler 			else if (istdmaoui(ie))
115110ad9a77SSam Leffler 				ies->tdma_ie = ie;
115210ad9a77SSam Leffler #endif
1153b032f27cSSam Leffler 			break;
1154b032f27cSSam Leffler 		case IEEE80211_ELEMID_RSN:
1155b032f27cSSam Leffler 			ies->rsn_ie = ie;
1156b032f27cSSam Leffler 			break;
1157b032f27cSSam Leffler 		case IEEE80211_ELEMID_HTCAP:
1158b032f27cSSam Leffler 			ies->htcap_ie = ie;
1159b032f27cSSam Leffler 			break;
1160d71a1f7aSAdrian Chadd 		case IEEE80211_ELEMID_HTINFO:
1161d71a1f7aSAdrian Chadd 			ies->htinfo_ie = ie;
1162d71a1f7aSAdrian Chadd 			break;
116359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
116459aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
116559aa14a9SRui Paulo 			ies->meshid_ie = ie;
116659aa14a9SRui Paulo 			break;
116759aa14a9SRui Paulo #endif
116874a54be9SAdrian Chadd 		case IEEE80211_ELEMID_VHT_CAP:
116974a54be9SAdrian Chadd 			ies->vhtcap_ie = ie;
117074a54be9SAdrian Chadd 			break;
117174a54be9SAdrian Chadd 		case IEEE80211_ELEMID_VHT_OPMODE:
117274a54be9SAdrian Chadd 			ies->vhtopmode_ie = ie;
117374a54be9SAdrian Chadd 			break;
117474a54be9SAdrian Chadd 		case IEEE80211_ELEMID_VHT_PWR_ENV:
117574a54be9SAdrian Chadd 			ies->vhtpwrenv_ie = ie;
117674a54be9SAdrian Chadd 			break;
117774a54be9SAdrian Chadd 		case IEEE80211_ELEMID_BSSLOAD:
117874a54be9SAdrian Chadd 			ies->bssload_ie = ie;
117974a54be9SAdrian Chadd 			break;
118074a54be9SAdrian Chadd 		case IEEE80211_ELEMID_APCHANREP:
118174a54be9SAdrian Chadd 			ies->apchanrep_ie = ie;
118274a54be9SAdrian Chadd 			break;
1183b032f27cSSam Leffler 		}
1184b032f27cSSam Leffler 		ielen -= 2 + ie[1];
1185b032f27cSSam Leffler 		ie += 2 + ie[1];
1186b032f27cSSam Leffler 	}
1187b032f27cSSam Leffler }
1188b032f27cSSam Leffler 
1189b032f27cSSam Leffler /*
11908a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
11918a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
11928a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
11938a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
11948a1b9b6aSSam Leffler  */
11951a1e1d21SSam Leffler static void
11968a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
11971a1e1d21SSam Leffler {
1198b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
11995b16c28cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
120068e8e04eSSam Leffler 	int i;
12018a1b9b6aSSam Leffler 
12028a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
12038a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1204b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_STA)
1205b032f27cSSam Leffler 			vap->iv_ps_sta--;
12068a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1207b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1208b032f27cSSam Leffler 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
12098a1b9b6aSSam Leffler 	}
1210ebdda46cSSam Leffler 	/*
121151172f62SAdrian Chadd 	 * Cleanup any VHT and HT-related state.
12121b6167d2SSam Leffler 	 */
121351172f62SAdrian Chadd 	if (ni->ni_flags & IEEE80211_NODE_VHT)
121451172f62SAdrian Chadd 		ieee80211_vht_node_cleanup(ni);
12151b6167d2SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT)
12161b6167d2SSam Leffler 		ieee80211_ht_node_cleanup(ni);
1217339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
12181c7b0c84SAdrian Chadd 	/* Always do FF node cleanup; for A-MSDU */
1219339ccfb3SSam Leffler 	ieee80211_ff_node_cleanup(ni);
1220339ccfb3SSam Leffler #endif
122159aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
122259aa14a9SRui Paulo 	/*
122359aa14a9SRui Paulo 	 * Cleanup any mesh-related state.
122459aa14a9SRui Paulo 	 */
122559aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS)
122659aa14a9SRui Paulo 		ieee80211_mesh_node_cleanup(ni);
122759aa14a9SRui Paulo #endif
12281b6167d2SSam Leffler 	/*
12295b16c28cSSam Leffler 	 * Clear any staging queue entries.
12305b16c28cSSam Leffler 	 */
12315b16c28cSSam Leffler 	ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
12325b16c28cSSam Leffler 
12335b16c28cSSam Leffler 	/*
1234ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
1235ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
1236ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
1237ebdda46cSSam Leffler 	 * do it just in case.
12381b999d64SSam Leffler 	 * Likewise clear the ASSOCID flag as these flags are intended
12391b999d64SSam Leffler 	 * to be managed in tandem.
1240ebdda46cSSam Leffler 	 */
12411b999d64SSam Leffler 	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
12428a1b9b6aSSam Leffler 
12438a1b9b6aSSam Leffler 	/*
12448a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
12458a1b9b6aSSam Leffler 	 */
124663092fceSSam Leffler 	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1247b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
12488a1b9b6aSSam Leffler 
12498a1b9b6aSSam Leffler 	ni->ni_associd = 0;
12508a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
1251b9b53389SAdrian Chadd 		IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
12528a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
12538a1b9b6aSSam Leffler 	}
12548a1b9b6aSSam Leffler 	/*
12558a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
12568a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
12578a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
12588a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
12598a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
12608a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
12618a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
12628a1b9b6aSSam Leffler 	 * which will rebuild this state.
12638a1b9b6aSSam Leffler 	 *
12648a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
12658a1b9b6aSSam Leffler 	 */
1266a3e08d6fSRui Paulo 	for (i = 0; i < nitems(ni->ni_rxfrag); i++)
12678a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
12688a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
12698a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
12708a1b9b6aSSam Leffler 		}
1271c1225b52SSam Leffler 	/*
1272c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
1273c1225b52SSam Leffler 	 */
1274c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
12758a1b9b6aSSam Leffler }
12768a1b9b6aSSam Leffler 
12778a1b9b6aSSam Leffler static void
12788a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
12798a1b9b6aSSam Leffler {
12808a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
12818a1b9b6aSSam Leffler 
1282b6108616SRui Paulo 	ieee80211_ratectl_node_deinit(ni);
12838a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
1284b032f27cSSam Leffler 	ieee80211_ies_cleanup(&ni->ni_ies);
128563092fceSSam Leffler 	ieee80211_psq_cleanup(&ni->ni_psq);
1286b9b53389SAdrian Chadd 	IEEE80211_FREE(ni, M_80211_NODE);
12871a1e1d21SSam Leffler }
12881a1e1d21SSam Leffler 
1289b032f27cSSam Leffler static void
1290b032f27cSSam Leffler node_age(struct ieee80211_node *ni)
1291b032f27cSSam Leffler {
1292b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
12935d44f8c0SSam Leffler 
1294b032f27cSSam Leffler 	/*
1295b032f27cSSam Leffler 	 * Age frames on the power save queue.
1296b032f27cSSam Leffler 	 */
129763092fceSSam Leffler 	if (ieee80211_node_psq_age(ni) != 0 &&
129863092fceSSam Leffler 	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1299b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
1300b032f27cSSam Leffler 	/*
1301b032f27cSSam Leffler 	 * Age out HT resources (e.g. frames on the
1302b032f27cSSam Leffler 	 * A-MPDU reorder queues).
1303b032f27cSSam Leffler 	 */
1304b032f27cSSam Leffler 	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1305b032f27cSSam Leffler 		ieee80211_ht_node_age(ni);
1306b032f27cSSam Leffler }
1307b032f27cSSam Leffler 
130868e8e04eSSam Leffler static int8_t
13098a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
1310d1e61976SSam Leffler {
1311b032f27cSSam Leffler 	uint32_t avgrssi = ni->ni_avgrssi;
1312b032f27cSSam Leffler 	int32_t rssi;
1313b032f27cSSam Leffler 
1314b032f27cSSam Leffler 	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1315b032f27cSSam Leffler 		return 0;
1316b032f27cSSam Leffler 	rssi = IEEE80211_RSSI_GET(avgrssi);
1317b032f27cSSam Leffler 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1318d1e61976SSam Leffler }
1319d1e61976SSam Leffler 
13201a1e1d21SSam Leffler static void
132168e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
132268e8e04eSSam Leffler {
1323b032f27cSSam Leffler 	*rssi = node_getrssi(ni);
132468e8e04eSSam Leffler 	*noise = ni->ni_noise;
132568e8e04eSSam Leffler }
132668e8e04eSSam Leffler 
132768e8e04eSSam Leffler static void
1328b032f27cSSam Leffler node_getmimoinfo(const struct ieee80211_node *ni,
1329b032f27cSSam Leffler 	struct ieee80211_mimo_info *info)
1330b032f27cSSam Leffler {
1331864ab114SAdrian Chadd 	int i;
1332864ab114SAdrian Chadd 	uint32_t avgrssi;
1333864ab114SAdrian Chadd 	int32_t rssi;
1334864ab114SAdrian Chadd 
1335864ab114SAdrian Chadd 	bzero(info, sizeof(*info));
1336864ab114SAdrian Chadd 
1337617f8b10SAdrian Chadd 	for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
1338617f8b10SAdrian Chadd 		/* Note: for now, just pri20 channel info */
1339864ab114SAdrian Chadd 		avgrssi = ni->ni_mimo_rssi_ctl[i];
1340864ab114SAdrian Chadd 		if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1341617f8b10SAdrian Chadd 			info->ch[i].rssi[0] = 0;
1342864ab114SAdrian Chadd 		} else {
1343864ab114SAdrian Chadd 			rssi = IEEE80211_RSSI_GET(avgrssi);
1344617f8b10SAdrian Chadd 			info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1345864ab114SAdrian Chadd 		}
1346617f8b10SAdrian Chadd 		info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
134788e428c6SAdrian Chadd 	}
1348864ab114SAdrian Chadd 
134988e428c6SAdrian Chadd 	/* XXX ext radios? */
1350864ab114SAdrian Chadd 
1351864ab114SAdrian Chadd 	/* XXX EVM? */
1352b032f27cSSam Leffler }
1353b032f27cSSam Leffler 
1354d2e877f0SAndriy Voskoboinyk static void
1355d2e877f0SAndriy Voskoboinyk ieee80211_add_node_nt(struct ieee80211_node_table *nt,
1356d2e877f0SAndriy Voskoboinyk     struct ieee80211_node *ni)
1357d2e877f0SAndriy Voskoboinyk {
1358d2e877f0SAndriy Voskoboinyk 	struct ieee80211com *ic = nt->nt_ic;
1359d2e877f0SAndriy Voskoboinyk 	int hash;
1360d2e877f0SAndriy Voskoboinyk 
1361d2e877f0SAndriy Voskoboinyk 	IEEE80211_NODE_LOCK_ASSERT(nt);
1362d2e877f0SAndriy Voskoboinyk 
1363d2e877f0SAndriy Voskoboinyk 	hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
1364d2e877f0SAndriy Voskoboinyk 	(void) ic;	/* XXX IEEE80211_NODE_HASH */
1365d2e877f0SAndriy Voskoboinyk 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1366d2e877f0SAndriy Voskoboinyk 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1367d2e877f0SAndriy Voskoboinyk 	nt->nt_count++;
1368d2e877f0SAndriy Voskoboinyk 	ni->ni_table = nt;
1369d2e877f0SAndriy Voskoboinyk }
1370d2e877f0SAndriy Voskoboinyk 
1371d2e877f0SAndriy Voskoboinyk static void
1372d2e877f0SAndriy Voskoboinyk ieee80211_del_node_nt(struct ieee80211_node_table *nt,
1373d2e877f0SAndriy Voskoboinyk     struct ieee80211_node *ni)
1374d2e877f0SAndriy Voskoboinyk {
1375d2e877f0SAndriy Voskoboinyk 
1376d2e877f0SAndriy Voskoboinyk 	IEEE80211_NODE_LOCK_ASSERT(nt);
1377d2e877f0SAndriy Voskoboinyk 
1378d2e877f0SAndriy Voskoboinyk 	TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1379d2e877f0SAndriy Voskoboinyk 	LIST_REMOVE(ni, ni_hash);
1380d2e877f0SAndriy Voskoboinyk 	nt->nt_count--;
1381d2e877f0SAndriy Voskoboinyk 	KASSERT(nt->nt_count >= 0,
1382d2e877f0SAndriy Voskoboinyk 	    ("nt_count is negative (%d)!\n", nt->nt_count));
1383d2e877f0SAndriy Voskoboinyk 	ni->ni_table = NULL;
1384d2e877f0SAndriy Voskoboinyk }
1385d2e877f0SAndriy Voskoboinyk 
1386b032f27cSSam Leffler struct ieee80211_node *
1387b032f27cSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt,
1388b032f27cSSam Leffler 	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
13891a1e1d21SSam Leffler {
13908a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1391b032f27cSSam Leffler 	struct ieee80211_node *ni;
13921a1e1d21SSam Leffler 
139338c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
1394b032f27cSSam Leffler 	if (ni == NULL) {
1395b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
1396b032f27cSSam Leffler 		return NULL;
1397b032f27cSSam Leffler 	}
1398b032f27cSSam Leffler 
1399b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
140049a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
14018a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
14028a1b9b6aSSam Leffler 
14031a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
14048a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
14058a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
14068a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
14078a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
140801a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1409b032f27cSSam Leffler 	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1410b032f27cSSam Leffler 	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
14112045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
14122045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
141368e8e04eSSam Leffler 	ni->ni_ath_defkeyix = 0x7fff;
141463092fceSSam Leffler 	ieee80211_psq_init(&ni->ni_psq, "unknown");
141559aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
141659aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS)
141759aa14a9SRui Paulo 		ieee80211_mesh_node_init(vap, ni);
141859aa14a9SRui Paulo #endif
14198a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1420d2e877f0SAndriy Voskoboinyk 	ieee80211_add_node_nt(nt, ni);
1421b032f27cSSam Leffler 	ni->ni_vap = vap;
14228a1b9b6aSSam Leffler 	ni->ni_ic = ic;
14238a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14241a1e1d21SSam Leffler 
1425ea3d5fd9SAdrian Chadd 	/* handle failure; free node state */
1426ea3d5fd9SAdrian Chadd 	if (ic->ic_node_init(ni) != 0) {
1427ea3d5fd9SAdrian Chadd 		vap->iv_stats.is_rx_nodealloc++;
1428ea3d5fd9SAdrian Chadd 		ieee80211_psq_cleanup(&ni->ni_psq);
1429ea3d5fd9SAdrian Chadd 		ieee80211_ratectl_node_deinit(ni);
1430ea3d5fd9SAdrian Chadd 		_ieee80211_free_node(ni);
1431ea3d5fd9SAdrian Chadd 		return NULL;
1432ea3d5fd9SAdrian Chadd 	}
1433ea3d5fd9SAdrian Chadd 
1434be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1435be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1436be1054edSSam Leffler 
14371a1e1d21SSam Leffler 	return ni;
14381a1e1d21SSam Leffler }
14391a1e1d21SSam Leffler 
144097c973adSSam Leffler /*
144197c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
144297c973adSSam Leffler  * to the specified station.  We craft only as much state as we
144397c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
144497c973adSSam Leffler  * once the send completes.
144597c973adSSam Leffler  */
144697c973adSSam Leffler struct ieee80211_node *
1447b032f27cSSam Leffler ieee80211_tmp_node(struct ieee80211vap *vap,
1448b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
144997c973adSSam Leffler {
1450b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
145197c973adSSam Leffler 	struct ieee80211_node *ni;
145297c973adSSam Leffler 
145338c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
145497c973adSSam Leffler 	if (ni != NULL) {
1455b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1456b9b5f07dSSam Leffler 
1457b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
145897c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
145997c973adSSam Leffler 
1460b032f27cSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
1461b032f27cSSam Leffler 		ni->ni_ic = ic;			/* NB: needed to set channel */
1462b032f27cSSam Leffler 		ni->ni_vap = vap;
1463b032f27cSSam Leffler 
146497c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1465b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
146697c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
146797c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
1468b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1469b032f27cSSam Leffler 		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
147097c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
1471b9b5f07dSSam Leffler 		ni->ni_txpower = bss->ni_txpower;
147297c973adSSam Leffler 		/* XXX optimize away */
147363092fceSSam Leffler 		ieee80211_psq_init(&ni->ni_psq, "unknown");
1474bd56e71bSBernhard Schmidt 
1475bd56e71bSBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
1476ea3d5fd9SAdrian Chadd 
1477ea3d5fd9SAdrian Chadd 		/* handle failure; free node state */
1478ea3d5fd9SAdrian Chadd 		if (ic->ic_node_init(ni) != 0) {
1479ea3d5fd9SAdrian Chadd 			vap->iv_stats.is_rx_nodealloc++;
1480ea3d5fd9SAdrian Chadd 			ieee80211_psq_cleanup(&ni->ni_psq);
1481ea3d5fd9SAdrian Chadd 			ieee80211_ratectl_node_deinit(ni);
1482ea3d5fd9SAdrian Chadd 			_ieee80211_free_node(ni);
1483ea3d5fd9SAdrian Chadd 			return NULL;
1484ea3d5fd9SAdrian Chadd 		}
1485ea3d5fd9SAdrian Chadd 
148697c973adSSam Leffler 	} else {
148797c973adSSam Leffler 		/* XXX msg */
1488b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
148997c973adSSam Leffler 	}
149097c973adSSam Leffler 	return ni;
149197c973adSSam Leffler }
149297c973adSSam Leffler 
14931a1e1d21SSam Leffler struct ieee80211_node *
1494b032f27cSSam Leffler ieee80211_dup_bss(struct ieee80211vap *vap,
1495b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
14961a1e1d21SSam Leffler {
1497b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
14988a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
14998a1b9b6aSSam Leffler 
1500b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
15011a1e1d21SSam Leffler 	if (ni != NULL) {
1502b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1503694dca64SSam Leffler 		/*
1504b032f27cSSam Leffler 		 * Inherit from iv_bss.
1505694dca64SSam Leffler 		 */
1506b9b5f07dSSam Leffler 		copy_bss(ni, bss);
1507b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1508b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1509b032f27cSSam Leffler 	}
15101a1e1d21SSam Leffler 	return ni;
15111a1e1d21SSam Leffler }
15121a1e1d21SSam Leffler 
1513b032f27cSSam Leffler /*
1514b032f27cSSam Leffler  * Create a bss node for a legacy WDS vap.  The far end does
1515b032f27cSSam Leffler  * not associate so we just create create a new node and
1516b032f27cSSam Leffler  * simulate an association.  The caller is responsible for
1517b032f27cSSam Leffler  * installing the node as the bss node and handling any further
1518b032f27cSSam Leffler  * setup work like authorizing the port.
1519b032f27cSSam Leffler  */
1520b032f27cSSam Leffler struct ieee80211_node *
1521b032f27cSSam Leffler ieee80211_node_create_wds(struct ieee80211vap *vap,
1522b032f27cSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1523b032f27cSSam Leffler {
1524b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1525b032f27cSSam Leffler 	struct ieee80211_node *ni;
1526b032f27cSSam Leffler 
1527b032f27cSSam Leffler 	/* XXX check if node already in sta table? */
1528b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1529b032f27cSSam Leffler 	if (ni != NULL) {
1530b032f27cSSam Leffler 		ni->ni_wdsvap = vap;
1531b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1532b032f27cSSam Leffler 		/*
1533b032f27cSSam Leffler 		 * Inherit any manually configured settings.
1534b032f27cSSam Leffler 		 */
1535b9b5f07dSSam Leffler 		copy_bss(ni, vap->iv_bss);
1536b032f27cSSam Leffler 		ieee80211_node_set_chan(ni, chan);
1537b032f27cSSam Leffler 		/* NB: propagate ssid so available to WPA supplicant */
1538b032f27cSSam Leffler 		ni->ni_esslen = vap->iv_des_ssid[0].len;
1539b032f27cSSam Leffler 		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1540b032f27cSSam Leffler 		/* NB: no associd for peer */
1541b032f27cSSam Leffler 		/*
1542b032f27cSSam Leffler 		 * There are no management frames to use to
1543b032f27cSSam Leffler 		 * discover neighbor capabilities, so blindly
1544b032f27cSSam Leffler 		 * propagate the local configuration.
1545b032f27cSSam Leffler 		 */
1546b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WME)
1547b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
1548616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1549b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_FF)
1550b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_FF;
1551616190d0SSam Leffler #endif
155251172f62SAdrian Chadd 		/* XXX VHT */
1553b032f27cSSam Leffler 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
15542bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1555b032f27cSSam Leffler 			/*
1556b032f27cSSam Leffler 			 * Device is HT-capable and HT is enabled for
1557b032f27cSSam Leffler 			 * the vap; setup HT operation.  On return
1558b032f27cSSam Leffler 			 * ni_chan will be adjusted to an HT channel.
1559b032f27cSSam Leffler 			 */
1560b032f27cSSam Leffler 			ieee80211_ht_wds_init(ni);
156151172f62SAdrian Chadd 			if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
156251172f62SAdrian Chadd 				printf("%s: TODO: vht_wds_init\n", __func__);
156351172f62SAdrian Chadd 			}
1564b032f27cSSam Leffler 		} else {
1565b032f27cSSam Leffler 			struct ieee80211_channel *c = ni->ni_chan;
1566b032f27cSSam Leffler 			/*
1567b032f27cSSam Leffler 			 * Force a legacy channel to be used.
1568b032f27cSSam Leffler 			 */
1569b032f27cSSam Leffler 			c = ieee80211_find_channel(ic,
1570b032f27cSSam Leffler 			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1571b032f27cSSam Leffler 			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1572b032f27cSSam Leffler 			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1573b032f27cSSam Leffler 			ni->ni_chan = c;
1574b032f27cSSam Leffler 		}
1575b032f27cSSam Leffler 	}
1576b032f27cSSam Leffler 	return ni;
1577b032f27cSSam Leffler }
1578b032f27cSSam Leffler 
1579b032f27cSSam Leffler struct ieee80211_node *
15808a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1581b032f27cSSam Leffler ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1582b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
15838a1b9b6aSSam Leffler #else
1584b032f27cSSam Leffler ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1585b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
15868a1b9b6aSSam Leffler #endif
15871a1e1d21SSam Leffler {
15881a1e1d21SSam Leffler 	struct ieee80211_node *ni;
15891a1e1d21SSam Leffler 	int hash;
15901a1e1d21SSam Leffler 
15918a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1592750d6d0cSSam Leffler 
159359aa14a9SRui Paulo 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
15948a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
15951a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
15968a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
15978a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1598b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
159949a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
160049a15236SSam Leffler 			    func, line,
160149a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
16028a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
16038a1b9b6aSSam Leffler #endif
1604750d6d0cSSam Leffler 			return ni;
16051a1e1d21SSam Leffler 		}
16061a1e1d21SSam Leffler 	}
1607750d6d0cSSam Leffler 	return NULL;
1608750d6d0cSSam Leffler }
1609750d6d0cSSam Leffler 
1610750d6d0cSSam Leffler struct ieee80211_node *
16118a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
16128a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1613b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
16148a1b9b6aSSam Leffler #else
1615b032f27cSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt,
1616b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
16178a1b9b6aSSam Leffler #endif
1618750d6d0cSSam Leffler {
1619750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1620750d6d0cSSam Leffler 
16218a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1622b032f27cSSam Leffler 	ni = ieee80211_find_node_locked(nt, macaddr);
1623b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1624b032f27cSSam Leffler 	return ni;
1625b032f27cSSam Leffler }
1626b032f27cSSam Leffler 
1627b032f27cSSam Leffler struct ieee80211_node *
1628b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1629b032f27cSSam Leffler ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1630b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1631b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1632b032f27cSSam Leffler #else
1633b032f27cSSam Leffler ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1634b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1635b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1636b032f27cSSam Leffler #endif
1637b032f27cSSam Leffler {
1638b032f27cSSam Leffler 	struct ieee80211_node *ni;
1639b032f27cSSam Leffler 	int hash;
1640b032f27cSSam Leffler 
1641b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1642b032f27cSSam Leffler 
164359aa14a9SRui Paulo 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1644b032f27cSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1645b032f27cSSam Leffler 		if (ni->ni_vap == vap &&
1646b032f27cSSam Leffler 		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1647b032f27cSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
1648b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1649b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1650b032f27cSSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1651b032f27cSSam Leffler 			    func, line,
1652b032f27cSSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
1653b032f27cSSam Leffler 			    ieee80211_node_refcnt(ni));
1654b032f27cSSam Leffler #endif
1655b032f27cSSam Leffler 			return ni;
1656b032f27cSSam Leffler 		}
1657b032f27cSSam Leffler 	}
1658b032f27cSSam Leffler 	return NULL;
1659b032f27cSSam Leffler }
1660b032f27cSSam Leffler 
1661b032f27cSSam Leffler struct ieee80211_node *
1662b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1663b032f27cSSam Leffler ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1664b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1665b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1666b032f27cSSam Leffler #else
1667b032f27cSSam Leffler ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1668b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1669b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1670b032f27cSSam Leffler #endif
1671b032f27cSSam Leffler {
1672b032f27cSSam Leffler 	struct ieee80211_node *ni;
1673b032f27cSSam Leffler 
1674b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1675b032f27cSSam Leffler 	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
16768a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
16771a1e1d21SSam Leffler 	return ni;
16781a1e1d21SSam Leffler }
16791a1e1d21SSam Leffler 
16801a1e1d21SSam Leffler /*
16818a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
16828a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
16838a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
16848a1b9b6aSSam Leffler  * it's private state.
16858a1b9b6aSSam Leffler  */
16868a1b9b6aSSam Leffler struct ieee80211_node *
1687b032f27cSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
168868e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
16898a1b9b6aSSam Leffler {
16908a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16918a1b9b6aSSam Leffler 
1692d71a1f7aSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1693be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1694b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, macaddr);
16958a1b9b6aSSam Leffler 	if (ni != NULL) {
1696b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1697b032f27cSSam Leffler 
16988a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
1699b032f27cSSam Leffler 		ni->ni_rates = vap->iv_bss->ni_rates;
1700aa68c24fSSam Leffler 		if (ieee80211_iserp_rateset(&ni->ni_rates))
1701aa68c24fSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_ERP;
1702b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
17038e292e8eSSam Leffler 			/*
170468e8e04eSSam Leffler 			 * In adhoc demo mode there are no management
170568e8e04eSSam Leffler 			 * frames to use to discover neighbor capabilities,
170668e8e04eSSam Leffler 			 * so blindly propagate the local configuration
170768e8e04eSSam Leffler 			 * so we can do interesting things (e.g. use
170868e8e04eSSam Leffler 			 * WME to disable ACK's).
17098e292e8eSSam Leffler 			 */
1710869897d2SAdrian Chadd 			/*
1711869897d2SAdrian Chadd 			 * XXX TODO: 11n?
1712869897d2SAdrian Chadd 			 */
1713b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_WME)
17148e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
1715616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1716b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_FF)
171768e8e04eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_FF;
1718616190d0SSam Leffler #endif
17198e292e8eSSam Leffler 		}
1720d77148fbSSam Leffler 		ieee80211_node_setuptxparms(ni);
172149d2c137SBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
1722869897d2SAdrian Chadd 
1723869897d2SAdrian Chadd 		/*
1724869897d2SAdrian Chadd 		 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
1725869897d2SAdrian Chadd 		 * not A-MPDU TX; not 11n rates, etc.  We'll cycle
1726869897d2SAdrian Chadd 		 * that after we hear that we can indeed do 11n
1727869897d2SAdrian Chadd 		 * (either by a beacon frame or by a probe response.)
1728869897d2SAdrian Chadd 		 */
1729869897d2SAdrian Chadd 
1730869897d2SAdrian Chadd 		/*
1731869897d2SAdrian Chadd 		 * This is the first time we see the node.
1732869897d2SAdrian Chadd 		 */
1733b032f27cSSam Leffler 		if (ic->ic_newassoc != NULL)
1734b032f27cSSam Leffler 			ic->ic_newassoc(ni, 1);
1735869897d2SAdrian Chadd 
1736869897d2SAdrian Chadd 		/*
1737869897d2SAdrian Chadd 		 * Kick off a probe request to the given node;
1738869897d2SAdrian Chadd 		 * we will then use the probe response to update
1739869897d2SAdrian Chadd 		 * 11n/etc configuration state.
1740869897d2SAdrian Chadd 		 *
1741869897d2SAdrian Chadd 		 * XXX TODO: this isn't guaranteed, and until we get
1742869897d2SAdrian Chadd 		 * a probe response, we won't be able to actually
1743869897d2SAdrian Chadd 		 * do anything 802.11n related to the node.
1744869897d2SAdrian Chadd 		 * So if this does indeed work, maybe we should hold
1745869897d2SAdrian Chadd 		 * off on sending responses until we get the probe
1746869897d2SAdrian Chadd 		 * response, or just default to some sensible subset
1747869897d2SAdrian Chadd 		 * of 802.11n behaviour (eg always allow aggregation
1748869897d2SAdrian Chadd 		 * negotiation TO us, but not FROM us, etc) so we
1749869897d2SAdrian Chadd 		 * aren't entirely busted.
1750869897d2SAdrian Chadd 		 */
1751869897d2SAdrian Chadd 		if (vap->iv_opmode == IEEE80211_M_IBSS) {
1752869897d2SAdrian Chadd 			ieee80211_send_probereq(ni, /* node */
1753869897d2SAdrian Chadd 				vap->iv_myaddr, /* SA */
1754869897d2SAdrian Chadd 				ni->ni_macaddr, /* DA */
1755869897d2SAdrian Chadd 				vap->iv_bss->ni_bssid, /* BSSID */
1756869897d2SAdrian Chadd 				vap->iv_bss->ni_essid,
1757869897d2SAdrian Chadd 				vap->iv_bss->ni_esslen); /* SSID */
1758869897d2SAdrian Chadd 		}
1759869897d2SAdrian Chadd 
176068e8e04eSSam Leffler 		/* XXX not right for 802.1x/WPA */
176168e8e04eSSam Leffler 		ieee80211_node_authorize(ni);
17628a1b9b6aSSam Leffler 	}
17638a1b9b6aSSam Leffler 	return ni;
17648a1b9b6aSSam Leffler }
17658a1b9b6aSSam Leffler 
1766be425a0fSSam Leffler void
1767be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
1768be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
1769be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
1770be425a0fSSam Leffler {
177151172f62SAdrian Chadd 	int do_ht_setup = 0, do_vht_setup = 0;
1772d71a1f7aSAdrian Chadd 
1773be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
1774be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1775be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1776be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1777be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
1778be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
1779be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
1780be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1781be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1782be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1783be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
178459aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
178559aa14a9SRui Paulo 	if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
178659aa14a9SRui Paulo 		ieee80211_mesh_init_neighbor(ni, wh, sp);
178759aa14a9SRui Paulo #endif
1788b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1789b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
17909094ffdfSSam Leffler 		if (ni->ni_ies.wme_ie != NULL)
17919094ffdfSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
17929094ffdfSSam Leffler 		else
17939094ffdfSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1794616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1795b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
1796b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1797616190d0SSam Leffler #endif
1798d71a1f7aSAdrian Chadd 		if (ni->ni_ies.htcap_ie != NULL)
1799d71a1f7aSAdrian Chadd 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1800d71a1f7aSAdrian Chadd 		if (ni->ni_ies.htinfo_ie != NULL)
1801d71a1f7aSAdrian Chadd 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1802d71a1f7aSAdrian Chadd 
180351172f62SAdrian Chadd 		if (ni->ni_ies.vhtcap_ie != NULL)
180451172f62SAdrian Chadd 			ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
180551172f62SAdrian Chadd 		if (ni->ni_ies.vhtopmode_ie != NULL)
180651172f62SAdrian Chadd 			ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
180751172f62SAdrian Chadd 
1808d71a1f7aSAdrian Chadd 		if ((ni->ni_ies.htcap_ie != NULL) &&
1809d71a1f7aSAdrian Chadd 		    (ni->ni_ies.htinfo_ie != NULL) &&
1810d71a1f7aSAdrian Chadd 		    (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1811d71a1f7aSAdrian Chadd 			do_ht_setup = 1;
1812d71a1f7aSAdrian Chadd 		}
181351172f62SAdrian Chadd 
181451172f62SAdrian Chadd 		if ((ni->ni_ies.vhtcap_ie != NULL) &&
181551172f62SAdrian Chadd 		    (ni->ni_ies.vhtopmode_ie != NULL) &&
181651172f62SAdrian Chadd 		    (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
181751172f62SAdrian Chadd 			do_vht_setup = 1;
181851172f62SAdrian Chadd 		}
181951172f62SAdrian Chadd 
1820b032f27cSSam Leffler 	}
1821be425a0fSSam Leffler 
1822be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
182379edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
182479edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
182579edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1826d71a1f7aSAdrian Chadd 
1827d71a1f7aSAdrian Chadd 	/*
1828d71a1f7aSAdrian Chadd 	 * If the neighbor is HT compatible, flip that on.
1829d71a1f7aSAdrian Chadd 	 */
1830d71a1f7aSAdrian Chadd 	if (do_ht_setup) {
1831d71a1f7aSAdrian Chadd 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1832d71a1f7aSAdrian Chadd 		    "%s: doing HT setup\n", __func__);
1833d71a1f7aSAdrian Chadd 		ieee80211_ht_node_init(ni);
1834d71a1f7aSAdrian Chadd 		ieee80211_ht_updateparams(ni,
1835d71a1f7aSAdrian Chadd 		    ni->ni_ies.htcap_ie,
1836d71a1f7aSAdrian Chadd 		    ni->ni_ies.htinfo_ie);
183751172f62SAdrian Chadd 
183851172f62SAdrian Chadd 		if (do_vht_setup) {
183951172f62SAdrian Chadd 			if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
184051172f62SAdrian Chadd 				printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
184151172f62SAdrian Chadd 				    __func__,
184251172f62SAdrian Chadd 				    ni->ni_macaddr,
184351172f62SAdrian Chadd 				    ":");
184451172f62SAdrian Chadd 			} else {
184551172f62SAdrian Chadd 				ieee80211_vht_node_init(ni);
184651172f62SAdrian Chadd 				ieee80211_vht_updateparams(ni,
184751172f62SAdrian Chadd 				    ni->ni_ies.vhtcap_ie,
184851172f62SAdrian Chadd 				    ni->ni_ies.vhtopmode_ie);
184951172f62SAdrian Chadd 				ieee80211_setup_vht_rates(ni,
185051172f62SAdrian Chadd 				    ni->ni_ies.vhtcap_ie,
185151172f62SAdrian Chadd 				    ni->ni_ies.vhtopmode_ie);
185251172f62SAdrian Chadd 			}
185351172f62SAdrian Chadd 		}
185451172f62SAdrian Chadd 
185551172f62SAdrian Chadd 		/*
185651172f62SAdrian Chadd 		 * Finally do the channel upgrade/change based
185751172f62SAdrian Chadd 		 * on the HT/VHT configuration.
185851172f62SAdrian Chadd 		 */
185951172f62SAdrian Chadd 		ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
186051172f62SAdrian Chadd 		    ni->ni_ies.htinfo_ie);
1861d71a1f7aSAdrian Chadd 		ieee80211_setup_htrates(ni,
1862d71a1f7aSAdrian Chadd 		    ni->ni_ies.htcap_ie,
1863d71a1f7aSAdrian Chadd 		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1864d71a1f7aSAdrian Chadd 		ieee80211_setup_basic_htrates(ni,
1865d71a1f7aSAdrian Chadd 		    ni->ni_ies.htinfo_ie);
186651172f62SAdrian Chadd 
1867d71a1f7aSAdrian Chadd 		ieee80211_node_setuptxparms(ni);
1868d71a1f7aSAdrian Chadd 		ieee80211_ratectl_node_init(ni);
1869869897d2SAdrian Chadd 
187051172f62SAdrian Chadd 		/* Reassociate; we're now 11n/11ac */
1871869897d2SAdrian Chadd 		/*
1872869897d2SAdrian Chadd 		 * XXX TODO: this is the wrong thing to do -
1873869897d2SAdrian Chadd 		 * we're calling it with isnew=1 so the ath(4)
1874869897d2SAdrian Chadd 		 * driver reinitialises the rate tables.
1875869897d2SAdrian Chadd 		 * This "mostly" works for ath(4), but it won't
1876869897d2SAdrian Chadd 		 * be right for firmware devices which allocate
1877869897d2SAdrian Chadd 		 * node states.
1878869897d2SAdrian Chadd 		 *
1879869897d2SAdrian Chadd 		 * So, do we just create a new node and delete
1880869897d2SAdrian Chadd 		 * the old one? Or?
1881869897d2SAdrian Chadd 		 */
1882869897d2SAdrian Chadd 		if (ni->ni_ic->ic_newassoc)
1883869897d2SAdrian Chadd 			ni->ni_ic->ic_newassoc(ni, 1);
1884d71a1f7aSAdrian Chadd 	}
1885be425a0fSSam Leffler }
1886be425a0fSSam Leffler 
1887b5c99415SSam Leffler /*
1888b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1889b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1890b5c99415SSam Leffler  * benefit we we treat this like an association so the
1891b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1892b5c99415SSam Leffler  */
1893b5c99415SSam Leffler struct ieee80211_node *
1894b032f27cSSam Leffler ieee80211_add_neighbor(struct ieee80211vap *vap,
1895b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1896b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1897b5c99415SSam Leffler {
1898b5c99415SSam Leffler 	struct ieee80211_node *ni;
1899b5c99415SSam Leffler 
1900d71a1f7aSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1901be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1902b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1903b5c99415SSam Leffler 	if (ni != NULL) {
1904b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1905b032f27cSSam Leffler 
1906be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1907aa68c24fSSam Leffler 		if (ieee80211_iserp_rateset(&ni->ni_rates))
1908aa68c24fSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_ERP;
1909d77148fbSSam Leffler 		ieee80211_node_setuptxparms(ni);
191049d2c137SBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
1911b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1912b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1913b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1914b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1915b5c99415SSam Leffler 	}
1916b5c99415SSam Leffler 	return ni;
1917b5c99415SSam Leffler }
1918b5c99415SSam Leffler 
1919a2cfa5b7SSam Leffler #define	IS_PROBEREQ(wh) \
1920a2cfa5b7SSam Leffler 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1921a2cfa5b7SSam Leffler 	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1922a2cfa5b7SSam Leffler #define	IS_BCAST_PROBEREQ(wh) \
1923a2cfa5b7SSam Leffler 	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1924a2cfa5b7SSam Leffler 	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1925a2cfa5b7SSam Leffler 
1926a2cfa5b7SSam Leffler static __inline struct ieee80211_node *
1927a2cfa5b7SSam Leffler _find_rxnode(struct ieee80211_node_table *nt,
1928a2cfa5b7SSam Leffler     const struct ieee80211_frame_min *wh)
1929a2cfa5b7SSam Leffler {
1930a2cfa5b7SSam Leffler 	if (IS_BCAST_PROBEREQ(wh))
1931a2cfa5b7SSam Leffler 		return NULL;		/* spam bcast probe req to all vap's */
1932a2cfa5b7SSam Leffler 	return ieee80211_find_node_locked(nt, wh->i_addr2);
1933a2cfa5b7SSam Leffler }
193468e8e04eSSam Leffler 
19358a1b9b6aSSam Leffler /*
19368a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
1937a2cfa5b7SSam Leffler  * (referenced) node up to the 802.11 layer for its use.  Note
1938a2cfa5b7SSam Leffler  * we can return NULL if the sender is not in the table.
19398a1b9b6aSSam Leffler  */
19408a1b9b6aSSam Leffler struct ieee80211_node *
19418a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
19428a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
19438a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
19448a1b9b6aSSam Leffler #else
19458a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
19468a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
19478a1b9b6aSSam Leffler #endif
19488a1b9b6aSSam Leffler {
19498a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
19508a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
19518a1b9b6aSSam Leffler 
195268e8e04eSSam Leffler 	nt = &ic->ic_sta;
19538a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1954a2cfa5b7SSam Leffler 	ni = _find_rxnode(nt, wh);
19558a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
19568a1b9b6aSSam Leffler 
1957c1225b52SSam Leffler 	return ni;
1958c1225b52SSam Leffler }
1959c1225b52SSam Leffler 
1960c1225b52SSam Leffler /*
1961c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1962c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1963c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1964c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1965c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1966c1225b52SSam Leffler  * key assigned to it.
1967c1225b52SSam Leffler  */
1968c1225b52SSam Leffler struct ieee80211_node *
1969c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1970c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1971c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1972c1225b52SSam Leffler 	const char *func, int line)
1973c1225b52SSam Leffler #else
1974c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1975c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1976c1225b52SSam Leffler #endif
1977c1225b52SSam Leffler {
1978c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1979c1225b52SSam Leffler 	struct ieee80211_node *ni;
1980c1225b52SSam Leffler 
1981c1225b52SSam Leffler 	nt = &ic->ic_sta;
1982c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1983c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1984c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1985c1225b52SSam Leffler 	else
1986c1225b52SSam Leffler 		ni = NULL;
1987c1225b52SSam Leffler 	if (ni == NULL) {
1988a2cfa5b7SSam Leffler 		ni = _find_rxnode(nt, wh);
1989b032f27cSSam Leffler 		if (ni != NULL && nt->nt_keyixmap != NULL) {
1990c1225b52SSam Leffler 			/*
1991c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1992c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1993c1225b52SSam Leffler 			 */
1994c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1995c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1996c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1997c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1998b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1999b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
2000c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
2001c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
2002c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
2003c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
2004c1225b52SSam Leffler 			}
2005c1225b52SSam Leffler 		}
2006a2cfa5b7SSam Leffler 	} else {
2007a2cfa5b7SSam Leffler 		if (IS_BCAST_PROBEREQ(wh))
2008a2cfa5b7SSam Leffler 			ni = NULL;	/* spam bcast probe req to all vap's */
2009a2cfa5b7SSam Leffler 		else
2010c1225b52SSam Leffler 			ieee80211_ref_node(ni);
2011a2cfa5b7SSam Leffler 	}
2012c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
2013c1225b52SSam Leffler 
2014c1225b52SSam Leffler 	return ni;
2015c1225b52SSam Leffler }
2016a2cfa5b7SSam Leffler #undef IS_BCAST_PROBEREQ
2017a2cfa5b7SSam Leffler #undef IS_PROBEREQ
20188a1b9b6aSSam Leffler 
20198a1b9b6aSSam Leffler /*
2020750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
2021750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
2022750d6d0cSSam Leffler  */
2023750d6d0cSSam Leffler struct ieee80211_node *
20248a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
2025b032f27cSSam Leffler ieee80211_find_txnode_debug(struct ieee80211vap *vap,
2026b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN],
20278a1b9b6aSSam Leffler 	const char *func, int line)
20288a1b9b6aSSam Leffler #else
2029b032f27cSSam Leffler ieee80211_find_txnode(struct ieee80211vap *vap,
2030b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
20318a1b9b6aSSam Leffler #endif
2032750d6d0cSSam Leffler {
2033b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
2034750d6d0cSSam Leffler 	struct ieee80211_node *ni;
2035750d6d0cSSam Leffler 
2036750d6d0cSSam Leffler 	/*
2037750d6d0cSSam Leffler 	 * The destination address should be in the node table
2038c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
2039c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
2040c1225b52SSam Leffler 	 * to the bss node.
2041750d6d0cSSam Leffler 	 */
2042750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
20438a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
2044b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA ||
2045b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_WDS ||
2046b032f27cSSam Leffler 	    IEEE80211_IS_MULTICAST(macaddr))
2047b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
20481b999d64SSam Leffler 	else
2049b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, macaddr);
20508a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
20518a1b9b6aSSam Leffler 
20528a1b9b6aSSam Leffler 	if (ni == NULL) {
2053b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS ||
2054b032f27cSSam Leffler 		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
205590d0d036SSam Leffler 			/*
205690d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
205790d0d036SSam Leffler 			 * Note that we need an additional reference for the
2058b032f27cSSam Leffler 			 * caller to be consistent with
2059b032f27cSSam Leffler 			 * ieee80211_find_node_locked.
206090d0d036SSam Leffler 			 */
2061869897d2SAdrian Chadd 			/*
2062869897d2SAdrian Chadd 			 * XXX TODO: this doesn't fake up 11n state; we need
2063869897d2SAdrian Chadd 			 * to find another way to get it upgraded.
2064869897d2SAdrian Chadd 			 */
2065b032f27cSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
206690d0d036SSam Leffler 			if (ni != NULL)
206790d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
206890d0d036SSam Leffler 		} else {
2069b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
2070b032f27cSSam Leffler 			    "no node, discard frame (%s)", __func__);
2071b032f27cSSam Leffler 			vap->iv_stats.is_tx_nonode++;
2072750d6d0cSSam Leffler 		}
2073750d6d0cSSam Leffler 	}
2074750d6d0cSSam Leffler 	return ni;
2075750d6d0cSSam Leffler }
2076750d6d0cSSam Leffler 
20778a1b9b6aSSam Leffler static void
20788a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
20798a1b9b6aSSam Leffler {
20808a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
20818a1b9b6aSSam Leffler 
208237e9743aSSam Leffler 	/*
208337e9743aSSam Leffler 	 * NB: careful about referencing the vap as it may be
208437e9743aSSam Leffler 	 * gone if the last reference was held by a driver.
208537e9743aSSam Leffler 	 * We know the com will always be present so it's safe
208637e9743aSSam Leffler 	 * to use ni_ic below to reclaim resources.
208737e9743aSSam Leffler 	 */
208837e9743aSSam Leffler #if 0
2089b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
209049a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
209149a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
20928a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
209337e9743aSSam Leffler #endif
209437e9743aSSam Leffler 	if (ni->ni_associd != 0) {
209537e9743aSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
2096b032f27cSSam Leffler 		if (vap->iv_aid_bitmap != NULL)
2097b032f27cSSam Leffler 			IEEE80211_AID_CLR(vap, ni->ni_associd);
209837e9743aSSam Leffler 	}
2099d2e877f0SAndriy Voskoboinyk 	if (nt != NULL)
2100d2e877f0SAndriy Voskoboinyk 		ieee80211_del_node_nt(nt, ni);
210137e9743aSSam Leffler 	ni->ni_ic->ic_node_free(ni);
21028a1b9b6aSSam Leffler }
21038a1b9b6aSSam Leffler 
2104eca3b4fcSAdrian Chadd /*
2105eca3b4fcSAdrian Chadd  * Clear any entry in the unicast key mapping table.
2106eca3b4fcSAdrian Chadd  */
2107eca3b4fcSAdrian Chadd static int
2108eca3b4fcSAdrian Chadd node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2109eca3b4fcSAdrian Chadd {
2110eca3b4fcSAdrian Chadd 	ieee80211_keyix keyix;
2111eca3b4fcSAdrian Chadd 
2112eca3b4fcSAdrian Chadd 	keyix = ni->ni_ucastkey.wk_rxkeyix;
2113eca3b4fcSAdrian Chadd 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
2114eca3b4fcSAdrian Chadd 	    nt->nt_keyixmap[keyix] == ni) {
2115eca3b4fcSAdrian Chadd 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2116eca3b4fcSAdrian Chadd 			"%s: %p<%s> clear key map entry %u\n",
2117eca3b4fcSAdrian Chadd 			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
2118eca3b4fcSAdrian Chadd 		nt->nt_keyixmap[keyix] = NULL;
2119eca3b4fcSAdrian Chadd 		ieee80211_node_decref(ni);
2120eca3b4fcSAdrian Chadd 		return 1;
2121eca3b4fcSAdrian Chadd 	}
2122eca3b4fcSAdrian Chadd 
2123eca3b4fcSAdrian Chadd 	return 0;
2124eca3b4fcSAdrian Chadd }
2125eca3b4fcSAdrian Chadd 
21268a1b9b6aSSam Leffler void
21278a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
21288a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
21298a1b9b6aSSam Leffler #else
21308a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
21318a1b9b6aSSam Leffler #endif
21328a1b9b6aSSam Leffler {
21338a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
21348a1b9b6aSSam Leffler 
21358a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
2136b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
213749a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
21388a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
21398a1b9b6aSSam Leffler #endif
2140c1225b52SSam Leffler 	if (nt != NULL) {
2141c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
21428a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
21438a1b9b6aSSam Leffler 			/*
2144c1225b52SSam Leffler 			 * Last reference, reclaim state.
21458a1b9b6aSSam Leffler 			 */
21468a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
2147eca3b4fcSAdrian Chadd 		} else if (ieee80211_node_refcnt(ni) == 1)
2148eca3b4fcSAdrian Chadd 			if (node_clear_keyixmap(nt, ni))
21498a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
2150c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
2151c1225b52SSam Leffler 	} else {
2152c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
2153c1225b52SSam Leffler 			_ieee80211_free_node(ni);
2154c1225b52SSam Leffler 	}
2155c1225b52SSam Leffler }
2156c1225b52SSam Leffler 
2157c1225b52SSam Leffler /*
2158c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
2159c1225b52SSam Leffler  */
2160c1225b52SSam Leffler int
2161c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
2162c1225b52SSam Leffler {
216337e9743aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
216437e9743aSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
2165c1225b52SSam Leffler 	struct ieee80211_node *nikey;
2166c1225b52SSam Leffler 	ieee80211_keyix keyix;
2167c1225b52SSam Leffler 	int isowned, status;
2168c1225b52SSam Leffler 
2169c1225b52SSam Leffler 	/*
2170c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
2171c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
2172c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
2173c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
2174c1225b52SSam Leffler 	 *
2175c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
2176c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
2177c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
2178c1225b52SSam Leffler 	 * way to separate out this path so we must do this
2179c1225b52SSam Leffler 	 * conditionally.
2180c1225b52SSam Leffler 	 */
2181c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
2182c1225b52SSam Leffler 	if (!isowned)
2183c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
218437e9743aSSam Leffler 	nikey = NULL;
218537e9743aSSam Leffler 	status = 1;		/* NB: success */
2186ca92652aSSam Leffler 	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
2187c1225b52SSam Leffler 		keyix = ni->ni_ucastkey.wk_rxkeyix;
218837e9743aSSam Leffler 		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
2189c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
2190c1225b52SSam Leffler 			nikey = nt->nt_keyixmap[keyix];
2191c2ede4b3SMartin Blapp 			nt->nt_keyixmap[keyix] = NULL;
219237e9743aSSam Leffler 		}
219337e9743aSSam Leffler 	}
2194c1225b52SSam Leffler 	if (!isowned)
2195b032f27cSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
2196c1225b52SSam Leffler 
2197c1225b52SSam Leffler 	if (nikey != NULL) {
2198c1225b52SSam Leffler 		KASSERT(nikey == ni,
2199c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
220037e9743aSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
2201c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
2202c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
2203c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
2204c1225b52SSam Leffler 		ieee80211_free_node(ni);
2205c1225b52SSam Leffler 	}
2206c1225b52SSam Leffler 	return status;
2207c1225b52SSam Leffler }
22081a1e1d21SSam Leffler 
2209303ebc3cSSam Leffler /*
22108a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
22118a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
22128a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
22138a1b9b6aSSam Leffler  */
22148a1b9b6aSSam Leffler static void
22158a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
22168a1b9b6aSSam Leffler {
2217c1225b52SSam Leffler 
2218c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
22198a1b9b6aSSam Leffler 
2220b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
222149a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
222249a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
222349a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
2224c1225b52SSam Leffler 	/*
2225c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
2226c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
2227c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
2228c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
2229c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
2230c1225b52SSam Leffler 	 */
2231eca3b4fcSAdrian Chadd 	(void)node_clear_keyixmap(nt, ni);
22328a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
22338a1b9b6aSSam Leffler 		/*
22348a1b9b6aSSam Leffler 		 * Other references are present, just remove the
22358a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
22368a1b9b6aSSam Leffler 		 * the references are dropped storage will be
2237acc4f7f5SSam Leffler 		 * reclaimed.
22388a1b9b6aSSam Leffler 		 */
2239d2e877f0SAndriy Voskoboinyk 		ieee80211_del_node_nt(nt, ni);
22408a1b9b6aSSam Leffler 	} else
22418a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
22428a1b9b6aSSam Leffler }
22438a1b9b6aSSam Leffler 
2244b032f27cSSam Leffler /*
2245b032f27cSSam Leffler  * Node table support.
2246b032f27cSSam Leffler  */
2247b032f27cSSam Leffler 
2248b032f27cSSam Leffler static void
2249b032f27cSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
2250b032f27cSSam Leffler 	struct ieee80211_node_table *nt,
2251b032f27cSSam Leffler 	const char *name, int inact, int keyixmax)
2252b032f27cSSam Leffler {
2253b032f27cSSam Leffler 
2254b032f27cSSam Leffler 	nt->nt_ic = ic;
2255c8f5794eSGleb Smirnoff 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
2256b032f27cSSam Leffler 	TAILQ_INIT(&nt->nt_node);
2257d2e877f0SAndriy Voskoboinyk 	nt->nt_count = 0;
2258b032f27cSSam Leffler 	nt->nt_name = name;
2259b032f27cSSam Leffler 	nt->nt_inact_init = inact;
2260b032f27cSSam Leffler 	nt->nt_keyixmax = keyixmax;
2261b032f27cSSam Leffler 	if (nt->nt_keyixmax > 0) {
2262b9b53389SAdrian Chadd 		nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
2263c5abbba3SDag-Erling Smørgrav 			keyixmax * sizeof(struct ieee80211_node *),
2264b9b53389SAdrian Chadd 			M_80211_NODE,
2265b9b53389SAdrian Chadd 			IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2266b032f27cSSam Leffler 		if (nt->nt_keyixmap == NULL)
2267c8f5794eSGleb Smirnoff 			ic_printf(ic,
2268b032f27cSSam Leffler 			    "Cannot allocate key index map with %u entries\n",
2269b032f27cSSam Leffler 			    keyixmax);
2270b032f27cSSam Leffler 	} else
2271b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
2272b032f27cSSam Leffler }
2273b032f27cSSam Leffler 
2274b032f27cSSam Leffler static void
2275b032f27cSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt,
2276b032f27cSSam Leffler 	struct ieee80211vap *match)
2277b032f27cSSam Leffler {
2278b032f27cSSam Leffler 	struct ieee80211_node *ni, *next;
2279b032f27cSSam Leffler 
2280b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
2281b032f27cSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
2282b032f27cSSam Leffler 		if (match != NULL && ni->ni_vap != match)
2283b032f27cSSam Leffler 			continue;
2284b032f27cSSam Leffler 		/* XXX can this happen?  if so need's work */
2285b032f27cSSam Leffler 		if (ni->ni_associd != 0) {
2286b032f27cSSam Leffler 			struct ieee80211vap *vap = ni->ni_vap;
2287b032f27cSSam Leffler 
2288b032f27cSSam Leffler 			if (vap->iv_auth->ia_node_leave != NULL)
2289b032f27cSSam Leffler 				vap->iv_auth->ia_node_leave(ni);
2290b032f27cSSam Leffler 			if (vap->iv_aid_bitmap != NULL)
2291b032f27cSSam Leffler 				IEEE80211_AID_CLR(vap, ni->ni_associd);
2292b032f27cSSam Leffler 		}
2293b032f27cSSam Leffler 		ni->ni_wdsvap = NULL;		/* clear reference */
22948a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
22958a1b9b6aSSam Leffler 	}
2296b032f27cSSam Leffler 	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
2297b032f27cSSam Leffler 		/*
2298b032f27cSSam Leffler 		 * Make a separate pass to clear references to this vap
2299b032f27cSSam Leffler 		 * held by DWDS entries.  They will not be matched above
2300b032f27cSSam Leffler 		 * because ni_vap will point to the ap vap but we still
2301b032f27cSSam Leffler 		 * need to clear ni_wdsvap when the WDS vap is destroyed
2302b032f27cSSam Leffler 		 * and/or reset.
2303b032f27cSSam Leffler 		 */
2304b032f27cSSam Leffler 		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
2305b032f27cSSam Leffler 			if (ni->ni_wdsvap == match)
2306b032f27cSSam Leffler 				ni->ni_wdsvap = NULL;
2307b032f27cSSam Leffler 	}
2308b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
2309b032f27cSSam Leffler }
2310b032f27cSSam Leffler 
2311b032f27cSSam Leffler static void
2312b032f27cSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2313b032f27cSSam Leffler {
2314b032f27cSSam Leffler 	ieee80211_node_table_reset(nt, NULL);
2315b032f27cSSam Leffler 	if (nt->nt_keyixmap != NULL) {
2316b032f27cSSam Leffler #ifdef DIAGNOSTIC
2317b032f27cSSam Leffler 		/* XXX verify all entries are NULL */
2318b032f27cSSam Leffler 		int i;
2319b032f27cSSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
2320b032f27cSSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
2321b032f27cSSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
2322b032f27cSSam Leffler 					nt->nt_name, i);
2323b032f27cSSam Leffler #endif
2324b9b53389SAdrian Chadd 		IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
2325b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
2326b032f27cSSam Leffler 	}
2327b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
23288a1b9b6aSSam Leffler }
23298a1b9b6aSSam Leffler 
23308a1b9b6aSSam Leffler static void
233103475bd0SAdrian Chadd timeout_stations(void *arg __unused, struct ieee80211_node *ni)
23321a1e1d21SSam Leffler {
233303475bd0SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
233403475bd0SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
23351a1e1d21SSam Leffler 
2336b032f27cSSam Leffler 	/*
2337b032f27cSSam Leffler 	 * Only process stations when in RUN state.  This
2338b032f27cSSam Leffler 	 * insures, for example, that we don't timeout an
2339b032f27cSSam Leffler 	 * inactive station during CAC.  Note that CSA state
2340b032f27cSSam Leffler 	 * is actually handled in ieee80211_node_timeout as
2341b032f27cSSam Leffler 	 * it applies to more than timeout processing.
2342b032f27cSSam Leffler 	 */
2343b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_RUN)
234403475bd0SAdrian Chadd 		return;
234503475bd0SAdrian Chadd 	/*
234603475bd0SAdrian Chadd 	 * Ignore entries for which have yet to receive an
234703475bd0SAdrian Chadd 	 * authentication frame.  These are transient and
234803475bd0SAdrian Chadd 	 * will be reclaimed when the last reference to them
234903475bd0SAdrian Chadd 	 * goes away (when frame xmits complete).
235003475bd0SAdrian Chadd 	 */
2351b032f27cSSam Leffler 	if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2352b032f27cSSam Leffler 	     vap->iv_opmode == IEEE80211_M_STA) &&
235344b666cdSSam Leffler 	    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
235403475bd0SAdrian Chadd 		return;
2355ebdda46cSSam Leffler 	/*
23568a1b9b6aSSam Leffler 	 * Free fragment if not needed anymore
23578a1b9b6aSSam Leffler 	 * (last fragment older than 1s).
2358b032f27cSSam Leffler 	 * XXX doesn't belong here, move to node_age
23590a915fadSSam Leffler 	 */
23608a1b9b6aSSam Leffler 	if (ni->ni_rxfrag[0] != NULL &&
23618a1b9b6aSSam Leffler 	    ticks > ni->ni_rxfragstamp + hz) {
23628a1b9b6aSSam Leffler 		m_freem(ni->ni_rxfrag[0]);
23638a1b9b6aSSam Leffler 		ni->ni_rxfrag[0] = NULL;
23648a1b9b6aSSam Leffler 	}
2365be1054edSSam Leffler 	if (ni->ni_inact > 0) {
2366c066143cSSam Leffler 		ni->ni_inact--;
2367be1054edSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2368be1054edSSam Leffler 		    "%s: inact %u inact_reload %u nrates %u",
2369be1054edSSam Leffler 		    __func__, ni->ni_inact, ni->ni_inact_reload,
2370be1054edSSam Leffler 		    ni->ni_rates.rs_nrates);
2371be1054edSSam Leffler 	}
2372ce647032SSam Leffler 	/*
2373ce647032SSam Leffler 	 * Special case ourself; we may be idle for extended periods
2374ce647032SSam Leffler 	 * of time and regardless reclaiming our state is wrong.
2375b032f27cSSam Leffler 	 * XXX run ic_node_age
2376ce647032SSam Leffler 	 */
237703475bd0SAdrian Chadd 	/* XXX before inact decrement? */
2378b032f27cSSam Leffler 	if (ni == vap->iv_bss)
237903475bd0SAdrian Chadd 		return;
2380b032f27cSSam Leffler 	if (ni->ni_associd != 0 ||
2381b032f27cSSam Leffler 	    (vap->iv_opmode == IEEE80211_M_IBSS ||
2382b032f27cSSam Leffler 	     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
23838a1b9b6aSSam Leffler 		/*
2384b032f27cSSam Leffler 		 * Age/drain resources held by the station.
23858a1b9b6aSSam Leffler 		 */
2386b032f27cSSam Leffler 		ic->ic_node_age(ni);
23878a1b9b6aSSam Leffler 		/*
23888a1b9b6aSSam Leffler 		 * Probe the station before time it out.  We
23898a1b9b6aSSam Leffler 		 * send a null data frame which may not be
23908a1b9b6aSSam Leffler 		 * universally supported by drivers (need it
23918a1b9b6aSSam Leffler 		 * for ps-poll support so it should be...).
239268e8e04eSSam Leffler 		 *
239368e8e04eSSam Leffler 		 * XXX don't probe the station unless we've
239468e8e04eSSam Leffler 		 *     received a frame from them (and have
239568e8e04eSSam Leffler 		 *     some idea of the rates they are capable
239668e8e04eSSam Leffler 		 *     of); this will get fixed more properly
239768e8e04eSSam Leffler 		 *     soon with better handling of the rate set.
23988a1b9b6aSSam Leffler 		 */
2399b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2400c066143cSSam Leffler 		    (0 < ni->ni_inact &&
2401b032f27cSSam Leffler 		     ni->ni_inact <= vap->iv_inact_probe) &&
240268e8e04eSSam Leffler 		    ni->ni_rates.rs_nrates != 0) {
2403b032f27cSSam Leffler 			IEEE80211_NOTE(vap,
2404f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2405f66d97f6SSam Leffler 			    ni, "%s",
2406f66d97f6SSam Leffler 			    "probe station due to inactivity");
240719ad2dd7SSam Leffler 			/*
240803475bd0SAdrian Chadd 			 * Grab a reference so the node cannot
240903475bd0SAdrian Chadd 			 * be reclaimed before we send the frame.
241003475bd0SAdrian Chadd 			 * ieee80211_send_nulldata understands
241103475bd0SAdrian Chadd 			 * we've done this and reclaims the
241219ad2dd7SSam Leffler 			 * ref for us as needed.
241319ad2dd7SSam Leffler 			 */
241403475bd0SAdrian Chadd 			/* XXX fix this (not required anymore). */
241519ad2dd7SSam Leffler 			ieee80211_ref_node(ni);
241603475bd0SAdrian Chadd 			/* XXX useless */
2417f62121ceSSam Leffler 			ieee80211_send_nulldata(ni);
24188a1b9b6aSSam Leffler 			/* XXX stat? */
241903475bd0SAdrian Chadd 			return;
24208a1b9b6aSSam Leffler 		}
24218a1b9b6aSSam Leffler 	}
2422b032f27cSSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2423c066143cSSam Leffler 	    ni->ni_inact <= 0) {
2424b032f27cSSam Leffler 		IEEE80211_NOTE(vap,
2425f66d97f6SSam Leffler 		    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2426f66d97f6SSam Leffler 		    "station timed out due to inactivity "
2427f66d97f6SSam Leffler 		    "(refcnt %u)", ieee80211_node_refcnt(ni));
24288a1b9b6aSSam Leffler 		/*
24298a1b9b6aSSam Leffler 		 * Send a deauthenticate frame and drop the station.
24308a1b9b6aSSam Leffler 		 * This is somewhat complicated due to reference counts
24318a1b9b6aSSam Leffler 		 * and locking.  At this point a station will typically
243203475bd0SAdrian Chadd 		 * have a reference count of 2.  ieee80211_node_leave
24338a1b9b6aSSam Leffler 		 * will do a "free" of the node which will drop the
24348a1b9b6aSSam Leffler 		 * reference count.  But in the meantime a reference
24358a1b9b6aSSam Leffler 		 * wil be held by the deauth frame.  The actual reclaim
24368a1b9b6aSSam Leffler 		 * of the node will happen either after the tx is
24378a1b9b6aSSam Leffler 		 * completed or by ieee80211_node_leave.
24388a1b9b6aSSam Leffler 		 */
24398a1b9b6aSSam Leffler 		if (ni->ni_associd != 0) {
2440b032f27cSSam Leffler 			IEEE80211_SEND_MGMT(ni,
24411a1e1d21SSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
24421a1e1d21SSam Leffler 			    IEEE80211_REASON_AUTH_EXPIRE);
24438a1b9b6aSSam Leffler 		}
2444b032f27cSSam Leffler 		ieee80211_node_leave(ni);
2445b032f27cSSam Leffler 		vap->iv_stats.is_node_timeout++;
2446303ebc3cSSam Leffler 	}
24471a1e1d21SSam Leffler }
24488a1b9b6aSSam Leffler 
244903475bd0SAdrian Chadd /*
245003475bd0SAdrian Chadd  * Timeout inactive stations and do related housekeeping.
245103475bd0SAdrian Chadd  */
245203475bd0SAdrian Chadd static void
245303475bd0SAdrian Chadd ieee80211_timeout_stations(struct ieee80211com *ic)
245403475bd0SAdrian Chadd {
245503475bd0SAdrian Chadd 	struct ieee80211_node_table *nt = &ic->ic_sta;
245603475bd0SAdrian Chadd 
245703475bd0SAdrian Chadd 	ieee80211_iterate_nodes(nt, timeout_stations, NULL);
245868e8e04eSSam Leffler }
24598a1b9b6aSSam Leffler 
2460b032f27cSSam Leffler /*
2461b032f27cSSam Leffler  * Aggressively reclaim resources.  This should be used
2462b032f27cSSam Leffler  * only in a critical situation to reclaim mbuf resources.
2463b032f27cSSam Leffler  */
2464b032f27cSSam Leffler void
2465b032f27cSSam Leffler ieee80211_drain(struct ieee80211com *ic)
2466b032f27cSSam Leffler {
2467b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
2468b032f27cSSam Leffler 	struct ieee80211vap *vap;
2469b032f27cSSam Leffler 	struct ieee80211_node *ni;
2470b032f27cSSam Leffler 
2471b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
2472b032f27cSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2473b032f27cSSam Leffler 		/*
2474b032f27cSSam Leffler 		 * Ignore entries for which have yet to receive an
2475b032f27cSSam Leffler 		 * authentication frame.  These are transient and
2476b032f27cSSam Leffler 		 * will be reclaimed when the last reference to them
2477b032f27cSSam Leffler 		 * goes away (when frame xmits complete).
2478b032f27cSSam Leffler 		 */
2479b032f27cSSam Leffler 		vap = ni->ni_vap;
2480b032f27cSSam Leffler 		/*
2481b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
2482b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
2483b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
2484b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
2485b032f27cSSam Leffler 		 * it applies to more than timeout processing.
2486b032f27cSSam Leffler 		 */
2487b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
2488b032f27cSSam Leffler 			continue;
2489b032f27cSSam Leffler 		/* XXX can vap be NULL? */
2490b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2491b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
2492b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2493b032f27cSSam Leffler 			continue;
2494b032f27cSSam Leffler 		/*
2495b032f27cSSam Leffler 		 * Free fragments.
2496b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_drain
2497b032f27cSSam Leffler 		 */
2498b032f27cSSam Leffler 		if (ni->ni_rxfrag[0] != NULL) {
2499b032f27cSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
2500b032f27cSSam Leffler 			ni->ni_rxfrag[0] = NULL;
2501b032f27cSSam Leffler 		}
2502b032f27cSSam Leffler 		/*
2503b032f27cSSam Leffler 		 * Drain resources held by the station.
2504b032f27cSSam Leffler 		 */
2505b032f27cSSam Leffler 		ic->ic_node_drain(ni);
2506b032f27cSSam Leffler 	}
2507b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
2508b032f27cSSam Leffler }
2509b032f27cSSam Leffler 
2510b032f27cSSam Leffler /*
2511b032f27cSSam Leffler  * Per-ieee80211com inactivity timer callback.
2512b032f27cSSam Leffler  */
251368e8e04eSSam Leffler void
251468e8e04eSSam Leffler ieee80211_node_timeout(void *arg)
251568e8e04eSSam Leffler {
251668e8e04eSSam Leffler 	struct ieee80211com *ic = arg;
251768e8e04eSSam Leffler 
2518b032f27cSSam Leffler 	/*
2519b032f27cSSam Leffler 	 * Defer timeout processing if a channel switch is pending.
2520b032f27cSSam Leffler 	 * We typically need to be mute so not doing things that
2521b032f27cSSam Leffler 	 * might generate frames is good to handle in one place.
2522a4641f4eSPedro F. Giffuni 	 * Suppressing the station timeout processing may extend the
2523b032f27cSSam Leffler 	 * lifetime of inactive stations (by not decrementing their
2524b032f27cSSam Leffler 	 * idle counters) but this should be ok unless the CSA is
2525b032f27cSSam Leffler 	 * active for an unusually long time.
2526b032f27cSSam Leffler 	 */
2527b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
252868e8e04eSSam Leffler 		ieee80211_scan_timeout(ic);
2529b032f27cSSam Leffler 		ieee80211_timeout_stations(ic);
25305b16c28cSSam Leffler 		ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
253168e8e04eSSam Leffler 
2532b105a069SSam Leffler 		IEEE80211_LOCK(ic);
2533b105a069SSam Leffler 		ieee80211_erp_timeout(ic);
25341b6167d2SSam Leffler 		ieee80211_ht_timeout(ic);
253551172f62SAdrian Chadd 		ieee80211_vht_timeout(ic);
2536b105a069SSam Leffler 		IEEE80211_UNLOCK(ic);
2537b032f27cSSam Leffler 	}
253868e8e04eSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
253968e8e04eSSam Leffler 		ieee80211_node_timeout, ic);
25401a1e1d21SSam Leffler }
25411a1e1d21SSam Leffler 
25427d684b4bSAdrian Chadd /*
2543d2e877f0SAndriy Voskoboinyk  * The same as ieee80211_iterate_nodes(), but for one vap only.
2544d2e877f0SAndriy Voskoboinyk  */
2545d2e877f0SAndriy Voskoboinyk int
2546d2e877f0SAndriy Voskoboinyk ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
2547d2e877f0SAndriy Voskoboinyk     struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
2548d2e877f0SAndriy Voskoboinyk {
2549d2e877f0SAndriy Voskoboinyk 	struct ieee80211_node **ni_arr;
2550d2e877f0SAndriy Voskoboinyk 	struct ieee80211_node *ni;
2551d2e877f0SAndriy Voskoboinyk 	size_t size;
2552d2e877f0SAndriy Voskoboinyk 	int count, i;
2553d2e877f0SAndriy Voskoboinyk 
2554d2e877f0SAndriy Voskoboinyk 	/*
2555d2e877f0SAndriy Voskoboinyk 	 * Iterate over the node table and save an array of ref'ed nodes.
25567d684b4bSAdrian Chadd 	 *
25577d684b4bSAdrian Chadd 	 * This is separated out from calling the actual node function so that
25587d684b4bSAdrian Chadd 	 * no LORs will occur.
25597d684b4bSAdrian Chadd 	 */
25607d684b4bSAdrian Chadd 	IEEE80211_NODE_LOCK(nt);
2561d2e877f0SAndriy Voskoboinyk 	count = nt->nt_count;
2562d2e877f0SAndriy Voskoboinyk 	size = count * sizeof(struct ieee80211_node *);
2563d2e877f0SAndriy Voskoboinyk 	ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
2564d2e877f0SAndriy Voskoboinyk 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2565d2e877f0SAndriy Voskoboinyk 	if (ni_arr == NULL) {
2566d2e877f0SAndriy Voskoboinyk 		IEEE80211_NODE_UNLOCK(nt);
2567d2e877f0SAndriy Voskoboinyk 		return (ENOMEM);
25687d684b4bSAdrian Chadd 	}
2569d2e877f0SAndriy Voskoboinyk 
2570d2e877f0SAndriy Voskoboinyk 	i = 0;
2571d2e877f0SAndriy Voskoboinyk 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2572d2e877f0SAndriy Voskoboinyk 		if (vap != NULL && ni->ni_vap != vap)
2573d2e877f0SAndriy Voskoboinyk 			continue;
2574d2e877f0SAndriy Voskoboinyk 		KASSERT(i < count,
2575d2e877f0SAndriy Voskoboinyk 		    ("node array overflow (vap %p, i %d, count %d)\n",
2576d2e877f0SAndriy Voskoboinyk 		    vap, i, count));
25777d684b4bSAdrian Chadd 		ni_arr[i] = ieee80211_ref_node(ni);
25787d684b4bSAdrian Chadd 		i++;
25797d684b4bSAdrian Chadd 	}
25807d684b4bSAdrian Chadd 	IEEE80211_NODE_UNLOCK(nt);
25817d684b4bSAdrian Chadd 
2582d2e877f0SAndriy Voskoboinyk 	for (i = 0; i < count; i++) {
25837d684b4bSAdrian Chadd 		if (ni_arr[i] == NULL)	/* end of the list */
25847d684b4bSAdrian Chadd 			break;
25857d684b4bSAdrian Chadd 		(*f)(arg, ni_arr[i]);
25867d684b4bSAdrian Chadd 		/* ieee80211_free_node() locks by itself */
25877d684b4bSAdrian Chadd 		ieee80211_free_node(ni_arr[i]);
25887d684b4bSAdrian Chadd 	}
25897d684b4bSAdrian Chadd 
2590b9b53389SAdrian Chadd 	IEEE80211_FREE(ni_arr, M_80211_NODE);
2591d2e877f0SAndriy Voskoboinyk 
2592d2e877f0SAndriy Voskoboinyk 	return (0);
2593d2e877f0SAndriy Voskoboinyk }
2594d2e877f0SAndriy Voskoboinyk 
2595d2e877f0SAndriy Voskoboinyk /*
2596d2e877f0SAndriy Voskoboinyk  * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2597d2e877f0SAndriy Voskoboinyk  * reference in the source.
2598d2e877f0SAndriy Voskoboinyk  */
2599d2e877f0SAndriy Voskoboinyk void
2600d2e877f0SAndriy Voskoboinyk ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2601d2e877f0SAndriy Voskoboinyk 	ieee80211_iter_func *f, void *arg)
2602d2e877f0SAndriy Voskoboinyk {
2603d2e877f0SAndriy Voskoboinyk 	/* XXX no way to pass error to the caller. */
2604d2e877f0SAndriy Voskoboinyk 	(void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
26058a1b9b6aSSam Leffler }
26068a1b9b6aSSam Leffler 
26078a1b9b6aSSam Leffler void
26088a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
26098a1b9b6aSSam Leffler {
26108a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
26118a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
261203475bd0SAdrian Chadd 	printf("\tauthmode %u flags 0x%x\n",
261303475bd0SAdrian Chadd 		ni->ni_authmode, ni->ni_flags);
26148a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
26158a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
26168a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2617801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2618801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2619801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
26208a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
26215463c4a4SSam Leffler 	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
26225463c4a4SSam Leffler 		node_getrssi(ni), ni->ni_noise,
262368e8e04eSSam Leffler 		ni->ni_intval, ni->ni_capinfo);
26248a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
26258a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
26268a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
26278a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2628be1054edSSam Leffler 	printf("\tinact %u inact_reload %u txrate %u\n",
2629be1054edSSam Leffler 		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
263068e8e04eSSam Leffler 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
263168e8e04eSSam Leffler 		ni->ni_htcap, ni->ni_htparam,
263268e8e04eSSam Leffler 		ni->ni_htctlchan, ni->ni_ht2ndchan);
263351172f62SAdrian Chadd 	printf("\thtopmode %x htstbc %x htchw %u\n",
263468e8e04eSSam Leffler 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
263551172f62SAdrian Chadd 	printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
263651172f62SAdrian Chadd 		ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
263751172f62SAdrian Chadd 		(int) ni->ni_vht_basicmcs);
263851172f62SAdrian Chadd 	/* XXX VHT state */
26398a1b9b6aSSam Leffler }
26408a1b9b6aSSam Leffler 
26418a1b9b6aSSam Leffler void
26428a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
26438a1b9b6aSSam Leffler {
26448a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
26458a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
26468a1b9b6aSSam Leffler }
26478a1b9b6aSSam Leffler 
26482dc4d8dcSSam Leffler static void
26492dc4d8dcSSam Leffler ieee80211_notify_erp_locked(struct ieee80211com *ic)
2650b105a069SSam Leffler {
2651b032f27cSSam Leffler 	struct ieee80211vap *vap;
2652b032f27cSSam Leffler 
2653b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2654b032f27cSSam Leffler 
2655b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2656b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2657b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2658b105a069SSam Leffler }
2659b105a069SSam Leffler 
26602dc4d8dcSSam Leffler void
26612dc4d8dcSSam Leffler ieee80211_notify_erp(struct ieee80211com *ic)
26622dc4d8dcSSam Leffler {
26632dc4d8dcSSam Leffler 	IEEE80211_LOCK(ic);
26642dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
26652dc4d8dcSSam Leffler 	IEEE80211_UNLOCK(ic);
26662dc4d8dcSSam Leffler }
26672dc4d8dcSSam Leffler 
26688a1b9b6aSSam Leffler /*
26698a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
26708a1b9b6aSSam Leffler  */
26718a1b9b6aSSam Leffler static void
2672b032f27cSSam Leffler ieee80211_node_join_11g(struct ieee80211_node *ni)
26738a1b9b6aSSam Leffler {
2674b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2675d20ff6e6SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
26768a1b9b6aSSam Leffler 
26771b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
26781b6167d2SSam Leffler 
26798a1b9b6aSSam Leffler 	/*
26808a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
26818a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
26828a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
26838a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
26848a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
26858a1b9b6aSSam Leffler 	 */
26868a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
26878a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
2688b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2689b105a069SSam Leffler 		    "station needs long slot time, count %d",
2690b105a069SSam Leffler 		    ic->ic_longslotsta);
269168e8e04eSSam Leffler 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
269268e8e04eSSam Leffler 			/*
269368e8e04eSSam Leffler 			 * Don't force slot time when switched to turbo
269468e8e04eSSam Leffler 			 * mode as non-ERP stations won't be present; this
269568e8e04eSSam Leffler 			 * need only be done when on the normal G channel.
269668e8e04eSSam Leffler 			 */
2697d20ff6e6SAdrian Chadd 			ieee80211_vap_set_shortslottime(vap, 0);
26988a1b9b6aSSam Leffler 		}
269968e8e04eSSam Leffler 	}
27008a1b9b6aSSam Leffler 	/*
27018a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
27028a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
27038a1b9b6aSSam Leffler 	 * if configured.
27048a1b9b6aSSam Leffler 	 */
2705b032f27cSSam Leffler 	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
27068a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
2707b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2708b105a069SSam Leffler 		    "station is !ERP, %d non-ERP stations associated",
2709b105a069SSam Leffler 		    ic->ic_nonerpsta);
27108a1b9b6aSSam Leffler 		/*
27118a1b9b6aSSam Leffler 		 * If station does not support short preamble
27128a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
27138a1b9b6aSSam Leffler 		 */
27148a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2715b032f27cSSam Leffler 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2716b105a069SSam Leffler 			    "%s", "station needs long preamble");
27178a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
27188a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
27198a1b9b6aSSam Leffler 		}
2720b105a069SSam Leffler 		/*
2721b032f27cSSam Leffler 		 * If protection is configured and this is the first
2722b032f27cSSam Leffler 		 * indication we should use protection, enable it.
2723b105a069SSam Leffler 		 */
2724b105a069SSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2725b105a069SSam Leffler 		    ic->ic_nonerpsta == 1 &&
2726b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2727b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2728b105a069SSam Leffler 			    "%s: enable use of protection\n", __func__);
2729b105a069SSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
27302dc4d8dcSSam Leffler 			ieee80211_notify_erp_locked(ic);
2731b105a069SSam Leffler 		}
27328a1b9b6aSSam Leffler 	} else
27338a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
27348a1b9b6aSSam Leffler }
27358a1b9b6aSSam Leffler 
27368a1b9b6aSSam Leffler void
2737b032f27cSSam Leffler ieee80211_node_join(struct ieee80211_node *ni, int resp)
27388a1b9b6aSSam Leffler {
2739b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2740b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
27418a1b9b6aSSam Leffler 	int newassoc;
27428a1b9b6aSSam Leffler 
27438a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
274468e8e04eSSam Leffler 		uint16_t aid;
27458a1b9b6aSSam Leffler 
2746b032f27cSSam Leffler 		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
27478a1b9b6aSSam Leffler 		/*
27488a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
27498a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
27508a1b9b6aSSam Leffler 		 */
2751b032f27cSSam Leffler 		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2752b032f27cSSam Leffler 			if (!IEEE80211_AID_ISSET(vap, aid))
27538a1b9b6aSSam Leffler 				break;
27548a1b9b6aSSam Leffler 		}
2755b032f27cSSam Leffler 		if (aid >= vap->iv_max_aid) {
275613f91245SSam Leffler 			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2757b032f27cSSam Leffler 			ieee80211_node_leave(ni);
27588a1b9b6aSSam Leffler 			return;
27598a1b9b6aSSam Leffler 		}
27608a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
27611b6167d2SSam Leffler 		ni->ni_jointime = time_uptime;
2762b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
2763b032f27cSSam Leffler 		IEEE80211_AID_SET(vap, ni->ni_associd);
2764b032f27cSSam Leffler 		vap->iv_sta_assoc++;
27658a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
27661b6167d2SSam Leffler 
27671b6167d2SSam Leffler 		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
27681b6167d2SSam Leffler 			ieee80211_ht_node_join(ni);
276951172f62SAdrian Chadd 		if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
277051172f62SAdrian Chadd 			ieee80211_vht_node_join(ni);
277168e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
277268e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2773b032f27cSSam Leffler 			ieee80211_node_join_11g(ni);
27741b6167d2SSam Leffler 		IEEE80211_UNLOCK(ic);
27751b6167d2SSam Leffler 
27761b6167d2SSam Leffler 		newassoc = 1;
27778a1b9b6aSSam Leffler 	} else
27788a1b9b6aSSam Leffler 		newassoc = 0;
27798a1b9b6aSSam Leffler 
278051172f62SAdrian Chadd 	/*
278151172f62SAdrian Chadd 	 * XXX VHT - should log VHT channel width, etc
278251172f62SAdrian Chadd 	 */
2783b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
278448f25cc3SAdrian Chadd 	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s%s",
2785b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
2786b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2787d20ff6e6SAdrian Chadd 	    vap->iv_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2788b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
278968e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
279051172f62SAdrian Chadd 	    /* XXX update for VHT string */
279168e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_HT ?
27920f52b1c4SSam Leffler 		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
27931b6167d2SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
279447bf877bSAdrian Chadd 	    ni->ni_flags & IEEE80211_NODE_AMSDU ? " (+AMSDU)" : "",
27958c070d69SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
27968c070d69SSam Leffler 	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
279744f7a6edSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2798b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
279968e8e04eSSam Leffler 		", fast-frames" : "",
2800b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
280168e8e04eSSam Leffler 		", turbo" : ""
2802b8fcf546SSam Leffler 	);
28038a1b9b6aSSam Leffler 
2804d77148fbSSam Leffler 	ieee80211_node_setuptxparms(ni);
280549d2c137SBernhard Schmidt 	ieee80211_ratectl_node_init(ni);
28068a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
2807736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
2808e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
2809b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
28108a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
2811b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_join != NULL)
2812b032f27cSSam Leffler 		vap->iv_auth->ia_node_join(ni);
2813b032f27cSSam Leffler 	ieee80211_notify_node_join(ni,
2814ce8977dfSSam Leffler 	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
28158a1b9b6aSSam Leffler }
28168a1b9b6aSSam Leffler 
2817b105a069SSam Leffler static void
2818b105a069SSam Leffler disable_protection(struct ieee80211com *ic)
2819b105a069SSam Leffler {
2820b105a069SSam Leffler 	KASSERT(ic->ic_nonerpsta == 0 &&
2821b105a069SSam Leffler 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2822b105a069SSam Leffler 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2823b105a069SSam Leffler 	   ic->ic_flags_ext));
2824b105a069SSam Leffler 
2825b105a069SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2826b105a069SSam Leffler 	/* XXX verify mode? */
2827b105a069SSam Leffler 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2828b105a069SSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2829b105a069SSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2830b105a069SSam Leffler 	}
28312dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
2832b105a069SSam Leffler }
2833b105a069SSam Leffler 
28348a1b9b6aSSam Leffler /*
28358a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
28368a1b9b6aSSam Leffler  */
28378a1b9b6aSSam Leffler static void
2838b032f27cSSam Leffler ieee80211_node_leave_11g(struct ieee80211_node *ni)
28398a1b9b6aSSam Leffler {
2840b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2841d20ff6e6SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
28428a1b9b6aSSam Leffler 
28431b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
28441b6167d2SSam Leffler 
284568e8e04eSSam Leffler 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2846b032f27cSSam Leffler 	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2847b032f27cSSam Leffler 	      ic->ic_bsschan->ic_flags));
28488a1b9b6aSSam Leffler 
28498a1b9b6aSSam Leffler 	/*
28508a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
28518a1b9b6aSSam Leffler 	 */
28528a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
28538a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
28548a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
28558a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
2856b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2857b105a069SSam Leffler 		    "long slot time station leaves, count now %d",
2858b105a069SSam Leffler 		    ic->ic_longslotsta);
28598a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
28608a1b9b6aSSam Leffler 			/*
28618a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
28628a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
28638a1b9b6aSSam Leffler 			 */
28648a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
28658a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
2866b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
2867b032f27cSSam Leffler 				    IEEE80211_MSG_ASSOC,
28688a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
28698a1b9b6aSSam Leffler 				    __func__);
2870d20ff6e6SAdrian Chadd 				ieee80211_vap_set_shortslottime(vap, 1);
28718a1b9b6aSSam Leffler 			}
28728a1b9b6aSSam Leffler 		}
28738a1b9b6aSSam Leffler 	}
28748a1b9b6aSSam Leffler 	/*
28758a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
28768a1b9b6aSSam Leffler 	 */
28778a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
28788a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
28798a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
28808a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
2881b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2882b105a069SSam Leffler 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2883b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2884b105a069SSam Leffler 			" (non-ERP sta present)" : "");
2885b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0 &&
2886b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2887b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
28888a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
2889b105a069SSam Leffler 			disable_protection(ic);
2890b105a069SSam Leffler 		}
2891b105a069SSam Leffler 	}
2892b105a069SSam Leffler }
2893b105a069SSam Leffler 
2894b105a069SSam Leffler /*
2895b105a069SSam Leffler  * Time out presence of an overlapping bss with non-ERP
2896b105a069SSam Leffler  * stations.  When operating in hostap mode we listen for
2897b105a069SSam Leffler  * beacons from other stations and if we identify a non-ERP
2898b105a069SSam Leffler  * station is present we enable protection.  To identify
2899b105a069SSam Leffler  * when all non-ERP stations are gone we time out this
2900b105a069SSam Leffler  * condition.
2901b105a069SSam Leffler  */
2902b105a069SSam Leffler static void
2903b105a069SSam Leffler ieee80211_erp_timeout(struct ieee80211com *ic)
2904b105a069SSam Leffler {
2905b105a069SSam Leffler 
2906b105a069SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2907b105a069SSam Leffler 
2908b105a069SSam Leffler 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2909b8e29e06SAdrian Chadd 	    ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2910b032f27cSSam Leffler #if 0
2911b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2912b032f27cSSam Leffler 		    "%s", "age out non-ERP sta present on channel");
2913b032f27cSSam Leffler #endif
2914b105a069SSam Leffler 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2915b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0)
2916b105a069SSam Leffler 			disable_protection(ic);
29178a1b9b6aSSam Leffler 	}
29188a1b9b6aSSam Leffler }
29198a1b9b6aSSam Leffler 
29208a1b9b6aSSam Leffler /*
29218a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
29228a1b9b6aSSam Leffler  * when operating as an ap.
29238a1b9b6aSSam Leffler  */
29248a1b9b6aSSam Leffler void
2925b032f27cSSam Leffler ieee80211_node_leave(struct ieee80211_node *ni)
29268a1b9b6aSSam Leffler {
2927b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2928b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
292944acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
29308a1b9b6aSSam Leffler 
2931b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2932b105a069SSam Leffler 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
29338a1b9b6aSSam Leffler 
2934b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2935b032f27cSSam Leffler 		("unexpected operating mode %u", vap->iv_opmode));
29368a1b9b6aSSam Leffler 	/*
29378a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
29388a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
29398a1b9b6aSSam Leffler 	 */
29408a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
29418a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
29428a1b9b6aSSam Leffler 		goto done;
29438a1b9b6aSSam Leffler 	/*
29448a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
29458a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
29468a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
29478a1b9b6aSSam Leffler 	 * associd to locate it's state block.
29488a1b9b6aSSam Leffler 	 */
2949b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_leave != NULL)
2950b032f27cSSam Leffler 		vap->iv_auth->ia_node_leave(ni);
29511b6167d2SSam Leffler 
29521b6167d2SSam Leffler 	IEEE80211_LOCK(ic);
2953b032f27cSSam Leffler 	IEEE80211_AID_CLR(vap, ni->ni_associd);
2954b032f27cSSam Leffler 	vap->iv_sta_assoc--;
29558a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
29568a1b9b6aSSam Leffler 
295751172f62SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
295851172f62SAdrian Chadd 		ieee80211_vht_node_leave(ni);
29591b6167d2SSam Leffler 	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
29601b6167d2SSam Leffler 		ieee80211_ht_node_leave(ni);
296168e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
296268e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2963b032f27cSSam Leffler 		ieee80211_node_leave_11g(ni);
29641b6167d2SSam Leffler 	IEEE80211_UNLOCK(ic);
29658a1b9b6aSSam Leffler 	/*
29668a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
29678a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
29688a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
29698a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
29708a1b9b6aSSam Leffler 	 */
2971b032f27cSSam Leffler 	ieee80211_sta_leave(ni);
29728a1b9b6aSSam Leffler done:
297344acc00dSSam Leffler 	/*
297444acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
297544acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
297644acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
297744acc00dSSam Leffler 	 * for inactivity.
297844acc00dSSam Leffler 	 */
297944acc00dSSam Leffler 	if (nt != NULL) {
298044acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
298144acc00dSSam Leffler 		node_reclaim(nt, ni);
298244acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
298344acc00dSSam Leffler 	} else
29848a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
29858a1b9b6aSSam Leffler }
29868a1b9b6aSSam Leffler 
2987b032f27cSSam Leffler struct rssiinfo {
2988b032f27cSSam Leffler 	int	rssi_samples;
2989b032f27cSSam Leffler 	uint32_t rssi_total;
2990b032f27cSSam Leffler };
2991b032f27cSSam Leffler 
2992b032f27cSSam Leffler static void
2993b032f27cSSam Leffler get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2994b032f27cSSam Leffler {
2995b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2996b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2997b032f27cSSam Leffler 	int8_t rssi;
2998b032f27cSSam Leffler 
2999b032f27cSSam Leffler 	/* only associated stations */
3000b032f27cSSam Leffler 	if (ni->ni_associd == 0)
3001b032f27cSSam Leffler 		return;
3002b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
3003b032f27cSSam Leffler 	if (rssi != 0) {
3004b032f27cSSam Leffler 		info->rssi_samples++;
3005b032f27cSSam Leffler 		info->rssi_total += rssi;
3006b032f27cSSam Leffler 	}
3007b032f27cSSam Leffler }
3008b032f27cSSam Leffler 
3009b032f27cSSam Leffler static void
3010b032f27cSSam Leffler get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
3011b032f27cSSam Leffler {
3012b032f27cSSam Leffler 	struct rssiinfo *info = arg;
3013b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3014b032f27cSSam Leffler 	int8_t rssi;
3015b032f27cSSam Leffler 
3016b032f27cSSam Leffler 	/* only neighbors */
3017b032f27cSSam Leffler 	/* XXX check bssid */
3018b032f27cSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
3019b032f27cSSam Leffler 		return;
3020b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
3021b032f27cSSam Leffler 	if (rssi != 0) {
3022b032f27cSSam Leffler 		info->rssi_samples++;
3023b032f27cSSam Leffler 		info->rssi_total += rssi;
3024b032f27cSSam Leffler 	}
3025b032f27cSSam Leffler }
3026b032f27cSSam Leffler 
302759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
302859aa14a9SRui Paulo static void
302959aa14a9SRui Paulo get_mesh_rssi(void *arg, struct ieee80211_node *ni)
303059aa14a9SRui Paulo {
303159aa14a9SRui Paulo 	struct rssiinfo *info = arg;
303259aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
303359aa14a9SRui Paulo 	int8_t rssi;
303459aa14a9SRui Paulo 
303559aa14a9SRui Paulo 	/* only neighbors that peered successfully */
303659aa14a9SRui Paulo 	if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
303759aa14a9SRui Paulo 		return;
303859aa14a9SRui Paulo 	rssi = vap->iv_ic->ic_node_getrssi(ni);
303959aa14a9SRui Paulo 	if (rssi != 0) {
304059aa14a9SRui Paulo 		info->rssi_samples++;
304159aa14a9SRui Paulo 		info->rssi_total += rssi;
304259aa14a9SRui Paulo 	}
304359aa14a9SRui Paulo }
304459aa14a9SRui Paulo #endif /* IEEE80211_SUPPORT_MESH */
304559aa14a9SRui Paulo 
304668e8e04eSSam Leffler int8_t
3047b032f27cSSam Leffler ieee80211_getrssi(struct ieee80211vap *vap)
30488a1b9b6aSSam Leffler {
30498a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
3050b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3051b032f27cSSam Leffler 	struct rssiinfo info;
30528a1b9b6aSSam Leffler 
3053b032f27cSSam Leffler 	info.rssi_total = 0;
3054b032f27cSSam Leffler 	info.rssi_samples = 0;
3055b032f27cSSam Leffler 	switch (vap->iv_opmode) {
30568a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
30578a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
30587db788c6SAndriy Voskoboinyk 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
30597db788c6SAndriy Voskoboinyk 		    &info);
3060b032f27cSSam Leffler 		break;
30618a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
30627db788c6SAndriy Voskoboinyk 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
30637db788c6SAndriy Voskoboinyk 		    &info);
30648a1b9b6aSSam Leffler 		break;
306559aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
306659aa14a9SRui Paulo 	case IEEE80211_M_MBSS:		/* average of all mesh neighbors */
30677db788c6SAndriy Voskoboinyk 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
30687db788c6SAndriy Voskoboinyk 		    &info);
306959aa14a9SRui Paulo 		break;
307059aa14a9SRui Paulo #endif
30718a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
30728a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
30738a1b9b6aSSam Leffler 	default:
3074b032f27cSSam Leffler 		if (vap->iv_bss != NULL)
3075b032f27cSSam Leffler 			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
3076b032f27cSSam Leffler 		info.rssi_samples = 1;
30778a1b9b6aSSam Leffler 		break;
30788a1b9b6aSSam Leffler 	}
3079b032f27cSSam Leffler 	return info.rssi_total / NZ(info.rssi_samples);
30808a1b9b6aSSam Leffler #undef NZ
30818a1b9b6aSSam Leffler }
30828a1b9b6aSSam Leffler 
308368e8e04eSSam Leffler void
3084b032f27cSSam Leffler ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
30858a1b9b6aSSam Leffler {
30868a1b9b6aSSam Leffler 
3087b032f27cSSam Leffler 	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
308868e8e04eSSam Leffler 		return;
3089b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
309068e8e04eSSam Leffler 	/* for non-station mode return avg'd rssi accounting */
3091b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA)
3092b032f27cSSam Leffler 		*rssi = ieee80211_getrssi(vap);
30938a1b9b6aSSam Leffler }
3094