xref: /freebsd/sys/net80211/ieee80211_node.c (revision 8a055831)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
310ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
30b032f27cSSam Leffler #include "opt_wlan.h"
31b032f27cSSam Leffler 
321a1e1d21SSam Leffler #include <sys/param.h>
331a1e1d21SSam Leffler #include <sys/systm.h>
341a1e1d21SSam Leffler #include <sys/mbuf.h>
351a1e1d21SSam Leffler #include <sys/malloc.h>
361a1e1d21SSam Leffler #include <sys/kernel.h>
371a1e1d21SSam Leffler 
388a1b9b6aSSam Leffler #include <sys/socket.h>
391a1e1d21SSam Leffler 
401a1e1d21SSam Leffler #include <net/if.h>
4176039bc8SGleb Smirnoff #include <net/if_var.h>
421a1e1d21SSam Leffler #include <net/if_media.h>
431a1e1d21SSam Leffler #include <net/ethernet.h>
441a1e1d21SSam Leffler 
451a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
46b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
47616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
48616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
49616190d0SSam Leffler #endif
5010ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
5110ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
5210ad9a77SSam Leffler #endif
53b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
5459aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
55b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
561a1e1d21SSam Leffler 
571a1e1d21SSam Leffler #include <net/bpf.h>
581a1e1d21SSam Leffler 
597268fa64SSam Leffler /*
6059aa14a9SRui Paulo  * IEEE80211_NODE_HASHSIZE must be a power of 2.
6159aa14a9SRui Paulo  */
6259aa14a9SRui Paulo CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
6359aa14a9SRui Paulo 
6459aa14a9SRui Paulo /*
657268fa64SSam Leffler  * Association id's are managed with a bit vector.
667268fa64SSam Leffler  */
67b032f27cSSam Leffler #define	IEEE80211_AID_SET(_vap, b) \
68b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
69b032f27cSSam Leffler 		(1 << (IEEE80211_AID(b) % 32)))
70b032f27cSSam Leffler #define	IEEE80211_AID_CLR(_vap, b) \
71b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
72b032f27cSSam Leffler 		~(1 << (IEEE80211_AID(b) % 32)))
73b032f27cSSam Leffler #define	IEEE80211_AID_ISSET(_vap, b) \
74b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
757268fa64SSam Leffler 
76132142c5SDiomidis Spinellis #ifdef IEEE80211_DEBUG_REFCNT
77132142c5SDiomidis Spinellis #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
78132142c5SDiomidis Spinellis #else
79132142c5SDiomidis Spinellis #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
80132142c5SDiomidis Spinellis #endif
81132142c5SDiomidis Spinellis 
8268e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *);
8368e8e04eSSam Leffler 
8438c208f8SSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211vap *,
8538c208f8SSam Leffler 	const uint8_t [IEEE80211_ADDR_LEN]);
868a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
878a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
88b032f27cSSam Leffler static void node_age(struct ieee80211_node *);
8968e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *);
9068e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
91b032f27cSSam Leffler static void node_getmimoinfo(const struct ieee80211_node *,
92b032f27cSSam Leffler 	struct ieee80211_mimo_info *);
931a1e1d21SSam Leffler 
948a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
958a1b9b6aSSam Leffler 
968a055831SAdrian Chadd static void node_reclaim(struct ieee80211_node_table *nt,
978a055831SAdrian Chadd 	struct ieee80211_node *ni);
988a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
99c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
10068e8e04eSSam Leffler 	int inact, int keymaxix);
101b032f27cSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *,
102b032f27cSSam Leffler 	struct ieee80211vap *);
1038a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
104b105a069SSam Leffler static void ieee80211_erp_timeout(struct ieee80211com *);
1051a1e1d21SSam Leffler 
10632346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
107b032f27cSSam Leffler MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
10837c150c4SSam Leffler 
1091a1e1d21SSam Leffler void
1108a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
1111a1e1d21SSam Leffler {
1125b16c28cSSam Leffler 	/* XXX really want maxlen enforced per-sta */
1135b16c28cSSam Leffler 	ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
1145b16c28cSSam Leffler 	    "802.11 staging q");
115b032f27cSSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
116b032f27cSSam Leffler 		IEEE80211_INACT_INIT, ic->ic_max_keyix);
117b032f27cSSam Leffler 	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
118b032f27cSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
119b032f27cSSam Leffler 		ieee80211_node_timeout, ic);
1201a1e1d21SSam Leffler 
1218a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
1228a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
1238a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
124b032f27cSSam Leffler 	ic->ic_node_age = node_age;
125b032f27cSSam Leffler 	ic->ic_node_drain = node_age;		/* NB: same as age */
1268a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
12768e8e04eSSam Leffler 	ic->ic_node_getsignal = node_getsignal;
128b032f27cSSam Leffler 	ic->ic_node_getmimoinfo = node_getmimoinfo;
1298a1b9b6aSSam Leffler 
130b032f27cSSam Leffler 	/*
131b032f27cSSam Leffler 	 * Set flags to be propagated to all vap's;
132b032f27cSSam Leffler 	 * these define default behaviour/configuration.
133b032f27cSSam Leffler 	 */
134c066143cSSam Leffler 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
135c1225b52SSam Leffler }
136c1225b52SSam Leffler 
137c1225b52SSam Leffler void
1388a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1391a1e1d21SSam Leffler {
1401a1e1d21SSam Leffler 
141b032f27cSSam Leffler 	callout_drain(&ic->ic_inact);
142acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
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 		}
175e2126decSSam Leffler 		vap->iv_aid_bitmap = (uint32_t *) malloc(
176c5abbba3SDag-Erling Smørgrav 			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
177b032f27cSSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
178b032f27cSSam Leffler 		if (vap->iv_aid_bitmap == NULL) {
179b032f27cSSam Leffler 			/* XXX no way to recover */
180b032f27cSSam Leffler 			printf("%s: no memory for AID bitmap, max aid %d!\n",
181b032f27cSSam Leffler 			    __func__, vap->iv_max_aid);
182b032f27cSSam Leffler 			vap->iv_max_aid = 0;
183b032f27cSSam Leffler 		}
184b032f27cSSam Leffler 	}
185b032f27cSSam Leffler 
186b032f27cSSam Leffler 	ieee80211_reset_bss(vap);
187b032f27cSSam Leffler 
188b032f27cSSam Leffler 	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
189b032f27cSSam Leffler }
190b032f27cSSam Leffler 
191b032f27cSSam Leffler void
192b032f27cSSam Leffler ieee80211_node_vdetach(struct ieee80211vap *vap)
193b032f27cSSam Leffler {
194b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
195b032f27cSSam Leffler 
196b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
197b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
198b032f27cSSam Leffler 		ieee80211_free_node(vap->iv_bss);
199b032f27cSSam Leffler 		vap->iv_bss = NULL;
200b032f27cSSam Leffler 	}
201b032f27cSSam Leffler 	if (vap->iv_aid_bitmap != NULL) {
202e2126decSSam Leffler 		free(vap->iv_aid_bitmap, M_80211_NODE);
203b032f27cSSam Leffler 		vap->iv_aid_bitmap = NULL;
2048a1b9b6aSSam Leffler 	}
2058a1b9b6aSSam Leffler }
2068a1b9b6aSSam Leffler 
2078a1b9b6aSSam Leffler /*
2088a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
2098a1b9b6aSSam Leffler  */
2108a1b9b6aSSam Leffler 
2118a1b9b6aSSam Leffler void
212e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
2138a1b9b6aSSam Leffler {
214be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
215be1054edSSam Leffler 
2168a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
217be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_run;
218c066143cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
219be1054edSSam Leffler 
220be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
221be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
2228a1b9b6aSSam Leffler }
2238a1b9b6aSSam Leffler 
2248a1b9b6aSSam Leffler void
225e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2268a1b9b6aSSam Leffler {
227be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
228be1054edSSam Leffler 
2298a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
230be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_auth;
231c066143cSSam Leffler 	if (ni->ni_inact > ni->ni_inact_reload)
232c066143cSSam Leffler 		ni->ni_inact = ni->ni_inact_reload;
233be1054edSSam Leffler 
234be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
235be1054edSSam Leffler 	    "%s: inact_reload %u inact %u", __func__,
236be1054edSSam Leffler 	    ni->ni_inact_reload, ni->ni_inact);
2378a1b9b6aSSam Leffler }
2388a1b9b6aSSam Leffler 
2398a1b9b6aSSam Leffler /*
24001a03542SSam Leffler  * Fix tx parameters for a node according to ``association state''.
24101a03542SSam Leffler  */
242d77148fbSSam Leffler void
243d77148fbSSam Leffler ieee80211_node_setuptxparms(struct ieee80211_node *ni)
24401a03542SSam Leffler {
24501a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
246f76cde95SSam Leffler 	enum ieee80211_phymode mode;
24701a03542SSam Leffler 
24801a03542SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT) {
24901a03542SSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
250f76cde95SSam Leffler 			mode = IEEE80211_MODE_11NA;
25101a03542SSam Leffler 		else
252f76cde95SSam Leffler 			mode = IEEE80211_MODE_11NG;
25301a03542SSam Leffler 	} else {				/* legacy rate handling */
254f76cde95SSam Leffler 		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
255f76cde95SSam Leffler 			mode = IEEE80211_MODE_STURBO_A;
2566a76ae21SSam Leffler 		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
2576a76ae21SSam Leffler 			mode = IEEE80211_MODE_HALF;
2586a76ae21SSam Leffler 		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
2596a76ae21SSam Leffler 			mode = IEEE80211_MODE_QUARTER;
260c5262b82SSam Leffler 		/* NB: 108A should be handled as 11a */
261f76cde95SSam Leffler 		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
262f76cde95SSam Leffler 			mode = IEEE80211_MODE_11A;
263c5262b82SSam Leffler 		else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
264c5262b82SSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_ERP))
265f76cde95SSam Leffler 			mode = IEEE80211_MODE_11G;
26601a03542SSam Leffler 		else
267f76cde95SSam Leffler 			mode = IEEE80211_MODE_11B;
26801a03542SSam Leffler 	}
269f76cde95SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
27001a03542SSam Leffler }
27101a03542SSam Leffler 
27201a03542SSam Leffler /*
2738a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2748a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
275b032f27cSSam Leffler  * XXX should be private but hostap needs it to deal with CSA
2768a1b9b6aSSam Leffler  */
277b032f27cSSam Leffler void
278b032f27cSSam Leffler ieee80211_node_set_chan(struct ieee80211_node *ni,
279b032f27cSSam Leffler 	struct ieee80211_channel *chan)
2808a1b9b6aSSam Leffler {
281b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
28201a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
28301a03542SSam Leffler 	enum ieee80211_phymode mode;
28468e8e04eSSam Leffler 
285b032f27cSSam Leffler 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
286b032f27cSSam Leffler 
2878a1b9b6aSSam Leffler 	ni->ni_chan = chan;
28801a03542SSam Leffler 	mode = ieee80211_chan2mode(chan);
28968e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(chan)) {
29068e8e04eSSam Leffler 		/*
291597029bfSBernhard Schmidt 		 * We must install the legacy rate est in ni_rates and the
29268e8e04eSSam Leffler 		 * HT rate set in ni_htrates.
29368e8e04eSSam Leffler 		 */
29468e8e04eSSam Leffler 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
29501a03542SSam Leffler 		/*
29601a03542SSam Leffler 		 * Setup bss tx parameters based on operating mode.  We
29701a03542SSam Leffler 		 * use legacy rates when operating in a mixed HT+non-HT bss
29801a03542SSam Leffler 		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
29901a03542SSam Leffler 		 */
30001a03542SSam Leffler 		if (mode == IEEE80211_MODE_11NA &&
3012bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
30201a03542SSam Leffler 			mode = IEEE80211_MODE_11A;
30301a03542SSam Leffler 		else if (mode == IEEE80211_MODE_11NG &&
3042bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
30501a03542SSam Leffler 			mode = IEEE80211_MODE_11G;
30601a03542SSam Leffler 		if (mode == IEEE80211_MODE_11G &&
30701a03542SSam Leffler 		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
30801a03542SSam Leffler 			mode = IEEE80211_MODE_11B;
30968e8e04eSSam Leffler 	}
31001a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
31141b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
3121a1e1d21SSam Leffler }
3131a1e1d21SSam Leffler 
314f9cd9174SSam Leffler static __inline void
315f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
316f9cd9174SSam Leffler {
317f9cd9174SSam Leffler 	/* propagate useful state */
318f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
319f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
320f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
321f9cd9174SSam Leffler 	/* XXX statistics? */
322b032f27cSSam Leffler 	/* XXX legacy WDS bssid? */
323f9cd9174SSam Leffler }
324f9cd9174SSam Leffler 
3251a1e1d21SSam Leffler void
326b032f27cSSam Leffler ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
3271a1e1d21SSam Leffler {
328b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3291a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3308a1b9b6aSSam Leffler 
331b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
33259aa14a9SRui Paulo 		"%s: creating %s on channel %u\n", __func__,
33359aa14a9SRui Paulo 		ieee80211_opmode_name[vap->iv_opmode],
334b032f27cSSam Leffler 		ieee80211_chan2ieee(ic, chan));
3358a1b9b6aSSam Leffler 
336b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
337acc4f7f5SSam Leffler 	if (ni == NULL) {
338acc4f7f5SSam Leffler 		/* XXX recovery? */
3398a1b9b6aSSam Leffler 		return;
3408a1b9b6aSSam Leffler 	}
341b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
342b032f27cSSam Leffler 	ni->ni_esslen = vap->iv_des_ssid[0].len;
343b032f27cSSam Leffler 	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
344b032f27cSSam Leffler 	if (vap->iv_bss != NULL)
345b032f27cSSam Leffler 		copy_bss(ni, vap->iv_bss);
346d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
347b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
3481a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3491a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3501a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3511a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3521a1e1d21SSam Leffler 	}
353b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
354b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_SIBSS;
3558a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
356b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
357b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
358fe49f061SSam Leffler 		else {
359fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
360fe49f061SSam Leffler 			/* clear group bit, add local bit */
361fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
362fe49f061SSam Leffler 		}
363b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
364b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
365b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
36650d8b493SSam Leffler 		else
36710ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
36810ad9a77SSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
36910ad9a77SSam Leffler #endif
37050d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
37159aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
37259aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
37359aa14a9SRui Paulo 		ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
37459aa14a9SRui Paulo 		memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
37559aa14a9SRui Paulo #endif
3768a1b9b6aSSam Leffler 	}
3778a1b9b6aSSam Leffler 	/*
3788a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3798a1b9b6aSSam Leffler 	 */
380b032f27cSSam Leffler 	/* clear DFS CAC state on previous channel */
381b032f27cSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
382b032f27cSSam Leffler 	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
383b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
384b032f27cSSam Leffler 		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
38568e8e04eSSam Leffler 	ic->ic_bsschan = chan;
386b032f27cSSam Leffler 	ieee80211_node_set_chan(ni, chan);
38768e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(chan);
3888a1b9b6aSSam Leffler 	/*
389b032f27cSSam Leffler 	 * Do mode-specific setup.
3908a1b9b6aSSam Leffler 	 */
39168e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan)) {
39268e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
39368e8e04eSSam Leffler 			/*
394b032f27cSSam Leffler 			 * Use a mixed 11b/11g basic rate set.
39568e8e04eSSam Leffler 			 */
396b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
39768e8e04eSSam Leffler 			    IEEE80211_MODE_11G);
398b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PUREG) {
399b032f27cSSam Leffler 				/*
400b032f27cSSam Leffler 				 * Also mark OFDM rates basic so 11b
401b032f27cSSam Leffler 				 * stations do not join (WiFi compliance).
402b032f27cSSam Leffler 				 */
403b032f27cSSam Leffler 				ieee80211_addbasicrates(&ni->ni_rates,
404b032f27cSSam Leffler 				    IEEE80211_MODE_11A);
405b032f27cSSam Leffler 			}
40668e8e04eSSam Leffler 		} else if (IEEE80211_IS_CHAN_B(chan)) {
40768e8e04eSSam Leffler 			/*
40868e8e04eSSam Leffler 			 * Force pure 11b rate set.
40968e8e04eSSam Leffler 			 */
410b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
41168e8e04eSSam Leffler 				IEEE80211_MODE_11B);
41268e8e04eSSam Leffler 		}
4131a1e1d21SSam Leffler 	}
4141a1e1d21SSam Leffler 
41568e8e04eSSam Leffler 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
41668e8e04eSSam Leffler }
41768e8e04eSSam Leffler 
41868e8e04eSSam Leffler /*
41968e8e04eSSam Leffler  * Reset bss state on transition to the INIT state.
42068e8e04eSSam Leffler  * Clear any stations from the table (they have been
42168e8e04eSSam Leffler  * deauth'd) and reset the bss node (clears key, rate
42268e8e04eSSam Leffler  * etc. state).
42368e8e04eSSam Leffler  */
4248a1b9b6aSSam Leffler void
425b032f27cSSam Leffler ieee80211_reset_bss(struct ieee80211vap *vap)
426b4c5a90fSSam Leffler {
427b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4288a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4298a1b9b6aSSam Leffler 
430b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
431b032f27cSSam Leffler 	/* XXX multi-bss: wrong */
43268e8e04eSSam Leffler 	ieee80211_reset_erp(ic);
433acc4f7f5SSam Leffler 
434b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
43548e1bda0SRui Paulo 	KASSERT(ni != NULL, ("unable to setup initial BSS node"));
436b032f27cSSam Leffler 	obss = vap->iv_bss;
437b032f27cSSam Leffler 	vap->iv_bss = ieee80211_ref_node(ni);
438f9cd9174SSam Leffler 	if (obss != NULL) {
439f9cd9174SSam Leffler 		copy_bss(ni, obss);
440d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
4418a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
442b032f27cSSam Leffler 	} else
443b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
444f9cd9174SSam Leffler }
4458a1b9b6aSSam Leffler 
4468a1b9b6aSSam Leffler static int
44768e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni,
44868e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
4498a1b9b6aSSam Leffler {
45068e8e04eSSam Leffler 	int i;
45168e8e04eSSam Leffler 
45268e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
45368e8e04eSSam Leffler 		if (ni->ni_esslen == ssids[i].len &&
45468e8e04eSSam Leffler 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
45568e8e04eSSam Leffler 			return 1;
45668e8e04eSSam Leffler 	}
45768e8e04eSSam Leffler 	return 0;
45868e8e04eSSam Leffler }
45968e8e04eSSam Leffler 
46068e8e04eSSam Leffler /*
46168e8e04eSSam Leffler  * Test a node for suitability/compatibility.
46268e8e04eSSam Leffler  */
46368e8e04eSSam Leffler static int
464b032f27cSSam Leffler check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
46568e8e04eSSam Leffler {
466b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
46768e8e04eSSam Leffler         uint8_t rate;
46868e8e04eSSam Leffler 
46968e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
47068e8e04eSSam Leffler 		return 0;
471b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
47268e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
47368e8e04eSSam Leffler 			return 0;
47468e8e04eSSam Leffler 	} else {
47568e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
47668e8e04eSSam Leffler 			return 0;
47768e8e04eSSam Leffler 	}
478b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
47968e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
48068e8e04eSSam Leffler 			return 0;
48168e8e04eSSam Leffler 	} else {
48268e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
48368e8e04eSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
48468e8e04eSSam Leffler 			return 0;
48568e8e04eSSam Leffler 	}
48668e8e04eSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
48768e8e04eSSam Leffler 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
48868e8e04eSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
48968e8e04eSSam Leffler 		return 0;
490b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
491b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
49268e8e04eSSam Leffler 		return 0;
493b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
494b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
49568e8e04eSSam Leffler 		return 0;
49668e8e04eSSam Leffler 	return 1;
49768e8e04eSSam Leffler }
49868e8e04eSSam Leffler 
49968e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
50068e8e04eSSam Leffler /*
50168e8e04eSSam Leffler  * Display node suitability/compatibility.
50268e8e04eSSam Leffler  */
50368e8e04eSSam Leffler static void
504b032f27cSSam Leffler check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
50568e8e04eSSam Leffler {
506b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
50768e8e04eSSam Leffler         uint8_t rate;
508b4c5a90fSSam Leffler         int fail;
509b4c5a90fSSam Leffler 
510b4c5a90fSSam Leffler 	fail = 0;
511b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
512b4c5a90fSSam Leffler 		fail |= 0x01;
513b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
514b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
515b4c5a90fSSam Leffler 			fail |= 0x02;
516b4c5a90fSSam Leffler 	} else {
517b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
518b4c5a90fSSam Leffler 			fail |= 0x02;
519b4c5a90fSSam Leffler 	}
520b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
521b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
522b4c5a90fSSam Leffler 			fail |= 0x04;
523b4c5a90fSSam Leffler 	} else {
524b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
525b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
526b4c5a90fSSam Leffler 			fail |= 0x04;
527b4c5a90fSSam Leffler 	}
52870e28b9aSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
52979edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
530b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
531b4c5a90fSSam Leffler 		fail |= 0x08;
532b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
533b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
534b4c5a90fSSam Leffler 		fail |= 0x10;
535b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
536b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
537b4c5a90fSSam Leffler 		fail |= 0x20;
53868e8e04eSSam Leffler 
53968e8e04eSSam Leffler 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
54068e8e04eSSam Leffler 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
54168e8e04eSSam Leffler 	printf(" %3d%c",
54268e8e04eSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
543b4c5a90fSSam Leffler 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
544b4c5a90fSSam Leffler 	    fail & 0x08 ? '!' : ' ');
545b4c5a90fSSam Leffler 	printf(" %4s%c",
546b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
547b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
548b4c5a90fSSam Leffler 	    "????",
549b4c5a90fSSam Leffler 	    fail & 0x02 ? '!' : ' ');
550b4c5a90fSSam Leffler 	printf(" %3s%c ",
55168e8e04eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
552b4c5a90fSSam Leffler 	    fail & 0x04 ? '!' : ' ');
553b4c5a90fSSam Leffler 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
554b4c5a90fSSam Leffler 	printf("%s\n", fail & 0x10 ? "!" : "");
555b4c5a90fSSam Leffler }
55668e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
5578a1b9b6aSSam Leffler 
558750d6d0cSSam Leffler /*
5598a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
5608a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
5618a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
5628a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
5638a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
5648a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
5658a1b9b6aSSam Leffler  *
5668a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
5678a1b9b6aSSam Leffler  *
5688a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
569750d6d0cSSam Leffler  */
5708a1b9b6aSSam Leffler int
571641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
5728a1b9b6aSSam Leffler {
573b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
574b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
575641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
576b032f27cSSam Leffler #endif
5778a1b9b6aSSam Leffler 
578b032f27cSSam Leffler 	if (ni == vap->iv_bss ||
579b032f27cSSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
5808a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
5818a1b9b6aSSam Leffler 		return 0;
5828a1b9b6aSSam Leffler 	}
583b032f27cSSam Leffler 	if (!check_bss(vap, ni)) {
58468e8e04eSSam Leffler 		/* capabilities mismatch */
585b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
5868a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
58768e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
588b032f27cSSam Leffler 		if (ieee80211_msg_assoc(vap))
589b032f27cSSam Leffler 			check_bss_debug(vap, ni);
59068e8e04eSSam Leffler #endif
591b032f27cSSam Leffler 		vap->iv_stats.is_ibss_capmismatch++;
5928a1b9b6aSSam Leffler 		return 0;
5938a1b9b6aSSam Leffler 	}
594b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
5958a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
5968a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
5978a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
5988a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
5998a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
6008a1b9b6aSSam Leffler 	);
60168e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
6028a1b9b6aSSam Leffler }
6038a1b9b6aSSam Leffler 
6048a1b9b6aSSam Leffler /*
605b032f27cSSam Leffler  * Calculate HT channel promotion flags for all vaps.
606b032f27cSSam Leffler  * This assumes ni_chan have been setup for each vap.
607b032f27cSSam Leffler  */
608b032f27cSSam Leffler static int
609b032f27cSSam Leffler gethtadjustflags(struct ieee80211com *ic)
610b032f27cSSam Leffler {
611b032f27cSSam Leffler 	struct ieee80211vap *vap;
612b032f27cSSam Leffler 	int flags;
613b032f27cSSam Leffler 
614b032f27cSSam Leffler 	flags = 0;
615b032f27cSSam Leffler 	/* XXX locking */
616b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
617b032f27cSSam Leffler 		if (vap->iv_state < IEEE80211_S_RUN)
618b032f27cSSam Leffler 			continue;
619b032f27cSSam Leffler 		switch (vap->iv_opmode) {
620b032f27cSSam Leffler 		case IEEE80211_M_WDS:
621b032f27cSSam Leffler 		case IEEE80211_M_STA:
622b032f27cSSam Leffler 		case IEEE80211_M_AHDEMO:
623b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
624b032f27cSSam Leffler 		case IEEE80211_M_IBSS:
62559aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
626b032f27cSSam Leffler 			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
627b032f27cSSam Leffler 			break;
628b032f27cSSam Leffler 		default:
629b032f27cSSam Leffler 			break;
630b032f27cSSam Leffler 		}
631b032f27cSSam Leffler 	}
632b032f27cSSam Leffler 	return flags;
633b032f27cSSam Leffler }
634b032f27cSSam Leffler 
635b032f27cSSam Leffler /*
636b032f27cSSam Leffler  * Check if the current channel needs to change based on whether
6375d44f8c0SSam Leffler  * any vap's are using HT20/HT40.  This is used to sync the state
6385d44f8c0SSam Leffler  * of ic_curchan after a channel width change on a running vap.
6391b6167d2SSam Leffler  */
6401b6167d2SSam Leffler void
641b032f27cSSam Leffler ieee80211_sync_curchan(struct ieee80211com *ic)
6421b6167d2SSam Leffler {
643b032f27cSSam Leffler 	struct ieee80211_channel *c;
644b032f27cSSam Leffler 
645b032f27cSSam Leffler 	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
646b032f27cSSam Leffler 	if (c != ic->ic_curchan) {
647b032f27cSSam Leffler 		ic->ic_curchan = c;
648b032f27cSSam Leffler 		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
64926d39e2cSSam Leffler 		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
6505efea30fSAndrew Thompson 		IEEE80211_UNLOCK(ic);
651b032f27cSSam Leffler 		ic->ic_set_channel(ic);
6525463c4a4SSam Leffler 		ieee80211_radiotap_chan_change(ic);
6535efea30fSAndrew Thompson 		IEEE80211_LOCK(ic);
654b032f27cSSam Leffler 	}
655b032f27cSSam Leffler }
656b032f27cSSam Leffler 
657b032f27cSSam Leffler /*
6585efea30fSAndrew Thompson  * Setup the current channel.  The request channel may be
659b032f27cSSam Leffler  * promoted if other vap's are operating with HT20/HT40.
660b032f27cSSam Leffler  */
661b032f27cSSam Leffler void
6625efea30fSAndrew Thompson ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
663b032f27cSSam Leffler {
664b032f27cSSam Leffler 	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
665b032f27cSSam Leffler 		int flags = gethtadjustflags(ic);
666b032f27cSSam Leffler 		/*
667b032f27cSSam Leffler 		 * Check for channel promotion required to support the
668b032f27cSSam Leffler 		 * set of running vap's.  This assumes we are called
669b032f27cSSam Leffler 		 * after ni_chan is setup for each vap.
670b032f27cSSam Leffler 		 */
6712bfc8a91SSam Leffler 		/* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
672b032f27cSSam Leffler 		if (flags > ieee80211_htchanflags(c))
673b032f27cSSam Leffler 			c = ieee80211_ht_adjust_channel(ic, c, flags);
674b032f27cSSam Leffler 	}
675b032f27cSSam Leffler 	ic->ic_bsschan = ic->ic_curchan = c;
6761b6167d2SSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
67726d39e2cSSam Leffler 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
6785efea30fSAndrew Thompson }
6795efea30fSAndrew Thompson 
6805efea30fSAndrew Thompson /*
6815efea30fSAndrew Thompson  * Change the current channel.  The channel change is guaranteed to have
6825efea30fSAndrew Thompson  * happened before the next state change.
6835efea30fSAndrew Thompson  */
6845efea30fSAndrew Thompson void
6855efea30fSAndrew Thompson ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
6865efea30fSAndrew Thompson {
6875efea30fSAndrew Thompson 	ieee80211_setupcurchan(ic, c);
6885efea30fSAndrew Thompson 	ieee80211_runtask(ic, &ic->ic_chan_task);
6891b6167d2SSam Leffler }
6901b6167d2SSam Leffler 
691b94299c4SAdrian Chadd void
692b94299c4SAdrian Chadd ieee80211_update_chw(struct ieee80211com *ic)
693b94299c4SAdrian Chadd {
694b94299c4SAdrian Chadd 
695b94299c4SAdrian Chadd 	ieee80211_setupcurchan(ic, ic->ic_curchan);
696b94299c4SAdrian Chadd 	ieee80211_runtask(ic, &ic->ic_chw_task);
697b94299c4SAdrian Chadd }
698b94299c4SAdrian Chadd 
6991b6167d2SSam Leffler /*
7008a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
7018a1b9b6aSSam Leffler  * be passed in with a held reference.
7028a1b9b6aSSam Leffler  */
70368e8e04eSSam Leffler static int
70468e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs)
7058a1b9b6aSSam Leffler {
706b032f27cSSam Leffler 	struct ieee80211vap *vap = selbs->ni_vap;
70768e8e04eSSam Leffler 	struct ieee80211com *ic = selbs->ni_ic;
7088a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
70968e8e04eSSam Leffler 	int canreassoc;
7108a1b9b6aSSam Leffler 
7118a1b9b6aSSam Leffler 	/*
7128a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
7138a1b9b6aSSam Leffler 	 */
714b032f27cSSam Leffler 	obss = vap->iv_bss;
71568e8e04eSSam Leffler 	/*
71668e8e04eSSam Leffler 	 * Check if old+new node have the same address in which
71768e8e04eSSam Leffler 	 * case we can reassociate when operating in sta mode.
71868e8e04eSSam Leffler 	 */
71968e8e04eSSam Leffler 	canreassoc = (obss != NULL &&
720b032f27cSSam Leffler 		vap->iv_state == IEEE80211_S_RUN &&
72168e8e04eSSam Leffler 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
722b032f27cSSam Leffler 	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
7231fd2349dSSam Leffler 	if (obss != NULL) {
7248a055831SAdrian Chadd 		struct ieee80211_node_table *nt = obss->ni_table;
7258a055831SAdrian Chadd 
7261fd2349dSSam Leffler 		copy_bss(selbs, obss);
72747a7b0faSSam Leffler 		ieee80211_node_decref(obss);	/* iv_bss reference */
7288a055831SAdrian Chadd 
7298a055831SAdrian Chadd 		IEEE80211_NODE_LOCK(nt);
7308a055831SAdrian Chadd 		node_reclaim(nt, obss);		/* station table reference */
7318a055831SAdrian Chadd 		IEEE80211_NODE_UNLOCK(nt);
7328a055831SAdrian Chadd 
733b032f27cSSam Leffler 		obss = NULL;		/* NB: guard against later use */
7341fd2349dSSam Leffler 	}
73579edaebfSSam Leffler 
73679edaebfSSam Leffler 	/*
73779edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
73879edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
73979edaebfSSam Leffler 	 */
740b032f27cSSam Leffler 	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
74170e28b9aSSam Leffler 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
74279edaebfSSam Leffler 
743b032f27cSSam Leffler 	ieee80211_setcurchan(ic, selbs->ni_chan);
7448a1b9b6aSSam Leffler 	/*
7458a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
7468a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
7478a1b9b6aSSam Leffler 	 * mode is locked.
7488a1b9b6aSSam Leffler 	 */
7498a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
750b032f27cSSam Leffler 	ieee80211_wme_initparams(vap);
751acc4f7f5SSam Leffler 
752b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
75368e8e04eSSam Leffler 		if (canreassoc) {
75468e8e04eSSam Leffler 			/* Reassociate */
755b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
75668e8e04eSSam Leffler 		} else {
75768e8e04eSSam Leffler 			/*
75868e8e04eSSam Leffler 			 * Act as if we received a DEAUTH frame in case we
75968e8e04eSSam Leffler 			 * are invoked from the RUN state.  This will cause
76068e8e04eSSam Leffler 			 * us to try to re-authenticate if we are operating
76168e8e04eSSam Leffler 			 * as a station.
76268e8e04eSSam Leffler 			 */
763b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_AUTH,
76468e8e04eSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH);
76568e8e04eSSam Leffler 		}
76668e8e04eSSam Leffler 	} else
767b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
7688a1b9b6aSSam Leffler 	return 1;
7698a1b9b6aSSam Leffler }
7708a1b9b6aSSam Leffler 
77168e8e04eSSam Leffler int
77210959256SSam Leffler ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
77368e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se)
77468e8e04eSSam Leffler {
775b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
77668e8e04eSSam Leffler 	struct ieee80211_node *ni;
77768e8e04eSSam Leffler 
778b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
77968e8e04eSSam Leffler 	if (ni == NULL) {
78068e8e04eSSam Leffler 		/* XXX msg */
78168e8e04eSSam Leffler 		return 0;
78268e8e04eSSam Leffler 	}
783d71a1f7aSAdrian Chadd 
78468e8e04eSSam Leffler 	/*
78568e8e04eSSam Leffler 	 * Expand scan state into node's format.
78668e8e04eSSam Leffler 	 * XXX may not need all this stuff
78768e8e04eSSam Leffler 	 */
78868e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
78968e8e04eSSam Leffler 	ni->ni_esslen = se->se_ssid[1];
79068e8e04eSSam Leffler 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
79168e8e04eSSam Leffler 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
79268e8e04eSSam Leffler 	ni->ni_intval = se->se_intval;
79368e8e04eSSam Leffler 	ni->ni_capinfo = se->se_capinfo;
79410959256SSam Leffler 	ni->ni_chan = chan;
79568e8e04eSSam Leffler 	ni->ni_timoff = se->se_timoff;
79668e8e04eSSam Leffler 	ni->ni_fhdwell = se->se_fhdwell;
79768e8e04eSSam Leffler 	ni->ni_fhindex = se->se_fhindex;
79868e8e04eSSam Leffler 	ni->ni_erp = se->se_erp;
799b032f27cSSam Leffler 	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
80068e8e04eSSam Leffler 	ni->ni_noise = se->se_noise;
8016ca74c40SSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
8026ca74c40SSam Leffler 		/* NB: only infrastructure mode requires an associd */
8031b999d64SSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
8046ca74c40SSam Leffler 	}
80568e8e04eSSam Leffler 
806b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
807b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
808616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
809b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
810b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
811616190d0SSam Leffler #endif
812b032f27cSSam Leffler 		if (ni->ni_ies.htcap_ie != NULL)
813b032f27cSSam Leffler 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
814b032f27cSSam Leffler 		if (ni->ni_ies.htinfo_ie != NULL)
815b032f27cSSam Leffler 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
81659aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
81759aa14a9SRui Paulo 		if (ni->ni_ies.meshid_ie != NULL)
81859aa14a9SRui Paulo 			ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
81959aa14a9SRui Paulo #endif
82010ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
82110ad9a77SSam Leffler 		if (ni->ni_ies.tdma_ie != NULL)
82210ad9a77SSam Leffler 			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
82310ad9a77SSam Leffler #endif
824b032f27cSSam Leffler 	}
825b032f27cSSam Leffler 
826b032f27cSSam Leffler 	vap->iv_dtim_period = se->se_dtimperiod;
827b032f27cSSam Leffler 	vap->iv_dtim_count = 0;
82868e8e04eSSam Leffler 
82968e8e04eSSam Leffler 	/* NB: must be after ni_chan is setup */
83068e8e04eSSam Leffler 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
83168e8e04eSSam Leffler 		IEEE80211_F_DOSORT);
832dfcd1f4dSSam Leffler 	if (ieee80211_iserp_rateset(&ni->ni_rates))
833dfcd1f4dSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
834d71a1f7aSAdrian Chadd 
835d71a1f7aSAdrian Chadd 	/*
836d71a1f7aSAdrian Chadd 	 * Setup HT state for this node if it's available, otherwise
837d71a1f7aSAdrian Chadd 	 * non-STA modes won't pick this state up.
838d71a1f7aSAdrian Chadd 	 *
839d71a1f7aSAdrian Chadd 	 * For IBSS and related modes that don't go through an
840d71a1f7aSAdrian Chadd 	 * association request/response, the only appropriate place
841d71a1f7aSAdrian Chadd 	 * to setup the HT state is here.
842d71a1f7aSAdrian Chadd 	 */
843d71a1f7aSAdrian Chadd 	if (ni->ni_ies.htinfo_ie != NULL &&
844d71a1f7aSAdrian Chadd 	    ni->ni_ies.htcap_ie != NULL &&
845d71a1f7aSAdrian Chadd 	    vap->iv_flags_ht & IEEE80211_FHT_HT) {
846d71a1f7aSAdrian Chadd 		ieee80211_ht_node_init(ni);
847d71a1f7aSAdrian Chadd 		ieee80211_ht_updateparams(ni,
848d71a1f7aSAdrian Chadd 		    ni->ni_ies.htcap_ie,
849d71a1f7aSAdrian Chadd 		    ni->ni_ies.htinfo_ie);
850d71a1f7aSAdrian Chadd 		ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
851d71a1f7aSAdrian Chadd 		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
852d71a1f7aSAdrian Chadd 		ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
853d71a1f7aSAdrian Chadd 	}
854d71a1f7aSAdrian Chadd 	/* XXX else check for ath FF? */
855d71a1f7aSAdrian Chadd 	/* XXX QoS? Difficult given that WME config is specific to a master */
856d71a1f7aSAdrian Chadd 
857d77148fbSSam Leffler 	ieee80211_node_setuptxparms(ni);
85849d2c137SBernhard Schmidt 	ieee80211_ratectl_node_init(ni);
85968e8e04eSSam Leffler 
86068e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
86168e8e04eSSam Leffler }
86268e8e04eSSam Leffler 
8638a1b9b6aSSam Leffler /*
8648a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
8658a1b9b6aSSam Leffler  * be passed in with a held reference.
8668a1b9b6aSSam Leffler  */
8678a1b9b6aSSam Leffler void
868b032f27cSSam Leffler ieee80211_sta_leave(struct ieee80211_node *ni)
8698a1b9b6aSSam Leffler {
870b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
871b032f27cSSam Leffler 
8728a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
873b032f27cSSam Leffler 	ieee80211_notify_node_leave(ni);
874b032f27cSSam Leffler }
875b032f27cSSam Leffler 
876b032f27cSSam Leffler /*
877b032f27cSSam Leffler  * Send a deauthenticate frame and drop the station.
878b032f27cSSam Leffler  */
879b032f27cSSam Leffler void
880b032f27cSSam Leffler ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
881b032f27cSSam Leffler {
882b032f27cSSam Leffler 	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
883b032f27cSSam Leffler 	ieee80211_ref_node(ni);
884b032f27cSSam Leffler 	if (ni->ni_associd != 0)
885b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
886b032f27cSSam Leffler 	ieee80211_node_leave(ni);
887b032f27cSSam Leffler 	ieee80211_free_node(ni);
8888a1b9b6aSSam Leffler }
8898a1b9b6aSSam Leffler 
8901a1e1d21SSam Leffler static struct ieee80211_node *
89138c208f8SSam Leffler node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
8921a1e1d21SSam Leffler {
893410ca74bSSam Leffler 	struct ieee80211_node *ni;
8948a1b9b6aSSam Leffler 
895e2126decSSam Leffler 	ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
896410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
897410ca74bSSam Leffler 	return ni;
8981a1e1d21SSam Leffler }
8991a1e1d21SSam Leffler 
9008a1b9b6aSSam Leffler /*
901b032f27cSSam Leffler  * Initialize an ie blob with the specified data.  If previous
902b032f27cSSam Leffler  * data exists re-use the data block.  As a side effect we clear
903b032f27cSSam Leffler  * all references to specific ie's; the caller is required to
904b032f27cSSam Leffler  * recalculate them.
905b032f27cSSam Leffler  */
906b032f27cSSam Leffler int
907b032f27cSSam Leffler ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
908b032f27cSSam Leffler {
909b032f27cSSam Leffler 	/* NB: assumes data+len are the last fields */
910b032f27cSSam Leffler 	memset(ies, 0, offsetof(struct ieee80211_ies, data));
911b032f27cSSam Leffler 	if (ies->data != NULL && ies->len != len) {
912b032f27cSSam Leffler 		/* data size changed */
913e2126decSSam Leffler 		free(ies->data, M_80211_NODE_IE);
914b032f27cSSam Leffler 		ies->data = NULL;
915b032f27cSSam Leffler 	}
916b032f27cSSam Leffler 	if (ies->data == NULL) {
917e2126decSSam Leffler 		ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
918b032f27cSSam Leffler 		if (ies->data == NULL) {
919b032f27cSSam Leffler 			ies->len = 0;
920b032f27cSSam Leffler 			/* NB: pointers have already been zero'd above */
921b032f27cSSam Leffler 			return 0;
922b032f27cSSam Leffler 		}
923b032f27cSSam Leffler 	}
924b032f27cSSam Leffler 	memcpy(ies->data, data, len);
925b032f27cSSam Leffler 	ies->len = len;
926b032f27cSSam Leffler 	return 1;
927b032f27cSSam Leffler }
928b032f27cSSam Leffler 
929b032f27cSSam Leffler /*
930b032f27cSSam Leffler  * Reclaim storage for an ie blob.
931b032f27cSSam Leffler  */
932b032f27cSSam Leffler void
933b032f27cSSam Leffler ieee80211_ies_cleanup(struct ieee80211_ies *ies)
934b032f27cSSam Leffler {
935b032f27cSSam Leffler 	if (ies->data != NULL)
936e2126decSSam Leffler 		free(ies->data, M_80211_NODE_IE);
937b032f27cSSam Leffler }
938b032f27cSSam Leffler 
939b032f27cSSam Leffler /*
940b032f27cSSam Leffler  * Expand an ie blob data contents and to fillin individual
941b032f27cSSam Leffler  * ie pointers.  The data blob is assumed to be well-formed;
942b032f27cSSam Leffler  * we don't do any validity checking of ie lengths.
943b032f27cSSam Leffler  */
944b032f27cSSam Leffler void
945b032f27cSSam Leffler ieee80211_ies_expand(struct ieee80211_ies *ies)
946b032f27cSSam Leffler {
947b032f27cSSam Leffler 	uint8_t *ie;
948b032f27cSSam Leffler 	int ielen;
949b032f27cSSam Leffler 
950b032f27cSSam Leffler 	ie = ies->data;
951b032f27cSSam Leffler 	ielen = ies->len;
952b032f27cSSam Leffler 	while (ielen > 0) {
953b032f27cSSam Leffler 		switch (ie[0]) {
954b032f27cSSam Leffler 		case IEEE80211_ELEMID_VENDOR:
955b032f27cSSam Leffler 			if (iswpaoui(ie))
956b032f27cSSam Leffler 				ies->wpa_ie = ie;
957b032f27cSSam Leffler 			else if (iswmeoui(ie))
958b032f27cSSam Leffler 				ies->wme_ie = ie;
959616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
960b032f27cSSam Leffler 			else if (isatherosoui(ie))
961b032f27cSSam Leffler 				ies->ath_ie = ie;
962616190d0SSam Leffler #endif
96310ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
96410ad9a77SSam Leffler 			else if (istdmaoui(ie))
96510ad9a77SSam Leffler 				ies->tdma_ie = ie;
96610ad9a77SSam Leffler #endif
967b032f27cSSam Leffler 			break;
968b032f27cSSam Leffler 		case IEEE80211_ELEMID_RSN:
969b032f27cSSam Leffler 			ies->rsn_ie = ie;
970b032f27cSSam Leffler 			break;
971b032f27cSSam Leffler 		case IEEE80211_ELEMID_HTCAP:
972b032f27cSSam Leffler 			ies->htcap_ie = ie;
973b032f27cSSam Leffler 			break;
974d71a1f7aSAdrian Chadd 		case IEEE80211_ELEMID_HTINFO:
975d71a1f7aSAdrian Chadd 			ies->htinfo_ie = ie;
976d71a1f7aSAdrian Chadd 			break;
97759aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
97859aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
97959aa14a9SRui Paulo 			ies->meshid_ie = ie;
98059aa14a9SRui Paulo 			break;
98159aa14a9SRui Paulo #endif
982b032f27cSSam Leffler 		}
983b032f27cSSam Leffler 		ielen -= 2 + ie[1];
984b032f27cSSam Leffler 		ie += 2 + ie[1];
985b032f27cSSam Leffler 	}
986b032f27cSSam Leffler }
987b032f27cSSam Leffler 
988b032f27cSSam Leffler /*
9898a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
9908a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
9918a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
9928a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
9938a1b9b6aSSam Leffler  */
9941a1e1d21SSam Leffler static void
9958a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
9961a1e1d21SSam Leffler {
997b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
9985b16c28cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
99968e8e04eSSam Leffler 	int i;
10008a1b9b6aSSam Leffler 
10018a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
10028a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
1003b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_STA)
1004b032f27cSSam Leffler 			vap->iv_ps_sta--;
10058a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
1006b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
1007b032f27cSSam Leffler 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
10088a1b9b6aSSam Leffler 	}
1009ebdda46cSSam Leffler 	/*
10101b6167d2SSam Leffler 	 * Cleanup any HT-related state.
10111b6167d2SSam Leffler 	 */
10121b6167d2SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT)
10131b6167d2SSam Leffler 		ieee80211_ht_node_cleanup(ni);
1014339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1015339ccfb3SSam Leffler 	else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
1016339ccfb3SSam Leffler 		ieee80211_ff_node_cleanup(ni);
1017339ccfb3SSam Leffler #endif
101859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
101959aa14a9SRui Paulo 	/*
102059aa14a9SRui Paulo 	 * Cleanup any mesh-related state.
102159aa14a9SRui Paulo 	 */
102259aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS)
102359aa14a9SRui Paulo 		ieee80211_mesh_node_cleanup(ni);
102459aa14a9SRui Paulo #endif
10251b6167d2SSam Leffler 	/*
10265b16c28cSSam Leffler 	 * Clear any staging queue entries.
10275b16c28cSSam Leffler 	 */
10285b16c28cSSam Leffler 	ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
10295b16c28cSSam Leffler 
10305b16c28cSSam Leffler 	/*
1031ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
1032ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
1033ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
1034ebdda46cSSam Leffler 	 * do it just in case.
10351b999d64SSam Leffler 	 * Likewise clear the ASSOCID flag as these flags are intended
10361b999d64SSam Leffler 	 * to be managed in tandem.
1037ebdda46cSSam Leffler 	 */
10381b999d64SSam Leffler 	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
10398a1b9b6aSSam Leffler 
10408a1b9b6aSSam Leffler 	/*
10418a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
10428a1b9b6aSSam Leffler 	 */
104363092fceSSam Leffler 	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1044b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
10458a1b9b6aSSam Leffler 
10468a1b9b6aSSam Leffler 	ni->ni_associd = 0;
10478a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
1048e2126decSSam Leffler 		free(ni->ni_challenge, M_80211_NODE);
10498a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
10508a1b9b6aSSam Leffler 	}
10518a1b9b6aSSam Leffler 	/*
10528a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
10538a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
10548a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
10558a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
10568a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
10578a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
10588a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
10598a1b9b6aSSam Leffler 	 * which will rebuild this state.
10608a1b9b6aSSam Leffler 	 *
10618a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
10628a1b9b6aSSam Leffler 	 */
1063a3e08d6fSRui Paulo 	for (i = 0; i < nitems(ni->ni_rxfrag); i++)
10648a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
10658a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
10668a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
10678a1b9b6aSSam Leffler 		}
1068c1225b52SSam Leffler 	/*
1069c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
1070c1225b52SSam Leffler 	 */
1071c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
10728a1b9b6aSSam Leffler }
10738a1b9b6aSSam Leffler 
10748a1b9b6aSSam Leffler static void
10758a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
10768a1b9b6aSSam Leffler {
10778a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10788a1b9b6aSSam Leffler 
1079b6108616SRui Paulo 	ieee80211_ratectl_node_deinit(ni);
10808a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
1081b032f27cSSam Leffler 	ieee80211_ies_cleanup(&ni->ni_ies);
108263092fceSSam Leffler 	ieee80211_psq_cleanup(&ni->ni_psq);
1083e2126decSSam Leffler 	free(ni, M_80211_NODE);
10841a1e1d21SSam Leffler }
10851a1e1d21SSam Leffler 
1086b032f27cSSam Leffler static void
1087b032f27cSSam Leffler node_age(struct ieee80211_node *ni)
1088b032f27cSSam Leffler {
1089b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
10905d44f8c0SSam Leffler 
10915d44f8c0SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
10925d44f8c0SSam Leffler 
1093b032f27cSSam Leffler 	/*
1094b032f27cSSam Leffler 	 * Age frames on the power save queue.
1095b032f27cSSam Leffler 	 */
109663092fceSSam Leffler 	if (ieee80211_node_psq_age(ni) != 0 &&
109763092fceSSam Leffler 	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1098b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
1099b032f27cSSam Leffler 	/*
1100b032f27cSSam Leffler 	 * Age out HT resources (e.g. frames on the
1101b032f27cSSam Leffler 	 * A-MPDU reorder queues).
1102b032f27cSSam Leffler 	 */
1103b032f27cSSam Leffler 	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1104b032f27cSSam Leffler 		ieee80211_ht_node_age(ni);
1105b032f27cSSam Leffler }
1106b032f27cSSam Leffler 
110768e8e04eSSam Leffler static int8_t
11088a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
1109d1e61976SSam Leffler {
1110b032f27cSSam Leffler 	uint32_t avgrssi = ni->ni_avgrssi;
1111b032f27cSSam Leffler 	int32_t rssi;
1112b032f27cSSam Leffler 
1113b032f27cSSam Leffler 	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1114b032f27cSSam Leffler 		return 0;
1115b032f27cSSam Leffler 	rssi = IEEE80211_RSSI_GET(avgrssi);
1116b032f27cSSam Leffler 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1117d1e61976SSam Leffler }
1118d1e61976SSam Leffler 
11191a1e1d21SSam Leffler static void
112068e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
112168e8e04eSSam Leffler {
1122b032f27cSSam Leffler 	*rssi = node_getrssi(ni);
112368e8e04eSSam Leffler 	*noise = ni->ni_noise;
112468e8e04eSSam Leffler }
112568e8e04eSSam Leffler 
112668e8e04eSSam Leffler static void
1127b032f27cSSam Leffler node_getmimoinfo(const struct ieee80211_node *ni,
1128b032f27cSSam Leffler 	struct ieee80211_mimo_info *info)
1129b032f27cSSam Leffler {
1130864ab114SAdrian Chadd 	int i;
1131864ab114SAdrian Chadd 	uint32_t avgrssi;
1132864ab114SAdrian Chadd 	int32_t rssi;
1133864ab114SAdrian Chadd 
1134864ab114SAdrian Chadd 	bzero(info, sizeof(*info));
1135864ab114SAdrian Chadd 
1136864ab114SAdrian Chadd 	for (i = 0; i < ni->ni_mimo_chains; i++) {
1137864ab114SAdrian Chadd 		avgrssi = ni->ni_mimo_rssi_ctl[i];
1138864ab114SAdrian Chadd 		if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
113988e428c6SAdrian Chadd 			info->rssi[i] = 0;
1140864ab114SAdrian Chadd 		} else {
1141864ab114SAdrian Chadd 			rssi = IEEE80211_RSSI_GET(avgrssi);
114288e428c6SAdrian Chadd 			info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1143864ab114SAdrian Chadd 		}
114488e428c6SAdrian Chadd 		info->noise[i] = ni->ni_mimo_noise_ctl[i];
114588e428c6SAdrian Chadd 	}
1146864ab114SAdrian Chadd 
114788e428c6SAdrian Chadd 	/* XXX ext radios? */
1148864ab114SAdrian Chadd 
1149864ab114SAdrian Chadd 	/* XXX EVM? */
1150b032f27cSSam Leffler }
1151b032f27cSSam Leffler 
1152b032f27cSSam Leffler struct ieee80211_node *
1153b032f27cSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt,
1154b032f27cSSam Leffler 	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
11551a1e1d21SSam Leffler {
11568a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1157b032f27cSSam Leffler 	struct ieee80211_node *ni;
11581a1e1d21SSam Leffler 	int hash;
11591a1e1d21SSam Leffler 
116038c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
1161b032f27cSSam Leffler 	if (ni == NULL) {
1162b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
1163b032f27cSSam Leffler 		return NULL;
1164b032f27cSSam Leffler 	}
1165b032f27cSSam Leffler 
1166b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
116749a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
11688a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
11698a1b9b6aSSam Leffler 
11701a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
117159aa14a9SRui Paulo 	hash = IEEE80211_NODE_HASH(ic, macaddr);
11728a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
11738a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
11748a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
11758a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
117601a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1177b032f27cSSam Leffler 	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1178b032f27cSSam Leffler 	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
11792045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
11802045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
118168e8e04eSSam Leffler 	ni->ni_ath_defkeyix = 0x7fff;
118263092fceSSam Leffler 	ieee80211_psq_init(&ni->ni_psq, "unknown");
118359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
118459aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS)
118559aa14a9SRui Paulo 		ieee80211_mesh_node_init(vap, ni);
118659aa14a9SRui Paulo #endif
11878a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
11888a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
11898a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
11908a1b9b6aSSam Leffler 	ni->ni_table = nt;
1191b032f27cSSam Leffler 	ni->ni_vap = vap;
11928a1b9b6aSSam Leffler 	ni->ni_ic = ic;
11938a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11941a1e1d21SSam Leffler 
1195be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1196be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1197be1054edSSam Leffler 
1198bd56e71bSBernhard Schmidt 	ieee80211_ratectl_node_init(ni);
1199bd56e71bSBernhard Schmidt 
12001a1e1d21SSam Leffler 	return ni;
12011a1e1d21SSam Leffler }
12021a1e1d21SSam Leffler 
120397c973adSSam Leffler /*
120497c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
120597c973adSSam Leffler  * to the specified station.  We craft only as much state as we
120697c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
120797c973adSSam Leffler  * once the send completes.
120897c973adSSam Leffler  */
120997c973adSSam Leffler struct ieee80211_node *
1210b032f27cSSam Leffler ieee80211_tmp_node(struct ieee80211vap *vap,
1211b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
121297c973adSSam Leffler {
1213b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
121497c973adSSam Leffler 	struct ieee80211_node *ni;
121597c973adSSam Leffler 
121638c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
121797c973adSSam Leffler 	if (ni != NULL) {
1218b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1219b9b5f07dSSam Leffler 
1220b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
122197c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
122297c973adSSam Leffler 
1223b032f27cSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
1224b032f27cSSam Leffler 		ni->ni_ic = ic;			/* NB: needed to set channel */
1225b032f27cSSam Leffler 		ni->ni_vap = vap;
1226b032f27cSSam Leffler 
122797c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1228b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
122997c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
123097c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
1231b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1232b032f27cSSam Leffler 		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
123397c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
1234b9b5f07dSSam Leffler 		ni->ni_txpower = bss->ni_txpower;
123597c973adSSam Leffler 		/* XXX optimize away */
123663092fceSSam Leffler 		ieee80211_psq_init(&ni->ni_psq, "unknown");
1237bd56e71bSBernhard Schmidt 
1238bd56e71bSBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
123997c973adSSam Leffler 	} else {
124097c973adSSam Leffler 		/* XXX msg */
1241b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
124297c973adSSam Leffler 	}
124397c973adSSam Leffler 	return ni;
124497c973adSSam Leffler }
124597c973adSSam Leffler 
12461a1e1d21SSam Leffler struct ieee80211_node *
1247b032f27cSSam Leffler ieee80211_dup_bss(struct ieee80211vap *vap,
1248b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
12491a1e1d21SSam Leffler {
1250b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
12518a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
12528a1b9b6aSSam Leffler 
1253b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
12541a1e1d21SSam Leffler 	if (ni != NULL) {
1255b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1256694dca64SSam Leffler 		/*
1257b032f27cSSam Leffler 		 * Inherit from iv_bss.
1258694dca64SSam Leffler 		 */
1259b9b5f07dSSam Leffler 		copy_bss(ni, bss);
1260b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1261b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1262b032f27cSSam Leffler 	}
12631a1e1d21SSam Leffler 	return ni;
12641a1e1d21SSam Leffler }
12651a1e1d21SSam Leffler 
1266b032f27cSSam Leffler /*
1267b032f27cSSam Leffler  * Create a bss node for a legacy WDS vap.  The far end does
1268b032f27cSSam Leffler  * not associate so we just create create a new node and
1269b032f27cSSam Leffler  * simulate an association.  The caller is responsible for
1270b032f27cSSam Leffler  * installing the node as the bss node and handling any further
1271b032f27cSSam Leffler  * setup work like authorizing the port.
1272b032f27cSSam Leffler  */
1273b032f27cSSam Leffler struct ieee80211_node *
1274b032f27cSSam Leffler ieee80211_node_create_wds(struct ieee80211vap *vap,
1275b032f27cSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1276b032f27cSSam Leffler {
1277b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1278b032f27cSSam Leffler 	struct ieee80211_node *ni;
1279b032f27cSSam Leffler 
1280b032f27cSSam Leffler 	/* XXX check if node already in sta table? */
1281b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1282b032f27cSSam Leffler 	if (ni != NULL) {
1283b032f27cSSam Leffler 		ni->ni_wdsvap = vap;
1284b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1285b032f27cSSam Leffler 		/*
1286b032f27cSSam Leffler 		 * Inherit any manually configured settings.
1287b032f27cSSam Leffler 		 */
1288b9b5f07dSSam Leffler 		copy_bss(ni, vap->iv_bss);
1289b032f27cSSam Leffler 		ieee80211_node_set_chan(ni, chan);
1290b032f27cSSam Leffler 		/* NB: propagate ssid so available to WPA supplicant */
1291b032f27cSSam Leffler 		ni->ni_esslen = vap->iv_des_ssid[0].len;
1292b032f27cSSam Leffler 		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1293b032f27cSSam Leffler 		/* NB: no associd for peer */
1294b032f27cSSam Leffler 		/*
1295b032f27cSSam Leffler 		 * There are no management frames to use to
1296b032f27cSSam Leffler 		 * discover neighbor capabilities, so blindly
1297b032f27cSSam Leffler 		 * propagate the local configuration.
1298b032f27cSSam Leffler 		 */
1299b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WME)
1300b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
1301616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1302b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_FF)
1303b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_FF;
1304616190d0SSam Leffler #endif
1305b032f27cSSam Leffler 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
13062bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1307b032f27cSSam Leffler 			/*
1308b032f27cSSam Leffler 			 * Device is HT-capable and HT is enabled for
1309b032f27cSSam Leffler 			 * the vap; setup HT operation.  On return
1310b032f27cSSam Leffler 			 * ni_chan will be adjusted to an HT channel.
1311b032f27cSSam Leffler 			 */
1312b032f27cSSam Leffler 			ieee80211_ht_wds_init(ni);
1313b032f27cSSam Leffler 		} else {
1314b032f27cSSam Leffler 			struct ieee80211_channel *c = ni->ni_chan;
1315b032f27cSSam Leffler 			/*
1316b032f27cSSam Leffler 			 * Force a legacy channel to be used.
1317b032f27cSSam Leffler 			 */
1318b032f27cSSam Leffler 			c = ieee80211_find_channel(ic,
1319b032f27cSSam Leffler 			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1320b032f27cSSam Leffler 			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1321b032f27cSSam Leffler 			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1322b032f27cSSam Leffler 			ni->ni_chan = c;
1323b032f27cSSam Leffler 		}
1324b032f27cSSam Leffler 	}
1325b032f27cSSam Leffler 	return ni;
1326b032f27cSSam Leffler }
1327b032f27cSSam Leffler 
1328b032f27cSSam Leffler struct ieee80211_node *
13298a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1330b032f27cSSam Leffler ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1331b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
13328a1b9b6aSSam Leffler #else
1333b032f27cSSam Leffler ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1334b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
13358a1b9b6aSSam Leffler #endif
13361a1e1d21SSam Leffler {
13371a1e1d21SSam Leffler 	struct ieee80211_node *ni;
13381a1e1d21SSam Leffler 	int hash;
13391a1e1d21SSam Leffler 
13408a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1341750d6d0cSSam Leffler 
134259aa14a9SRui Paulo 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
13438a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
13441a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
13458a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
13468a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1347b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
134849a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
134949a15236SSam Leffler 			    func, line,
135049a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
13518a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
13528a1b9b6aSSam Leffler #endif
1353750d6d0cSSam Leffler 			return ni;
13541a1e1d21SSam Leffler 		}
13551a1e1d21SSam Leffler 	}
1356750d6d0cSSam Leffler 	return NULL;
1357750d6d0cSSam Leffler }
1358750d6d0cSSam Leffler 
1359750d6d0cSSam Leffler struct ieee80211_node *
13608a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
13618a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1362b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
13638a1b9b6aSSam Leffler #else
1364b032f27cSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt,
1365b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
13668a1b9b6aSSam Leffler #endif
1367750d6d0cSSam Leffler {
1368750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1369750d6d0cSSam Leffler 
13708a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1371b032f27cSSam Leffler 	ni = ieee80211_find_node_locked(nt, macaddr);
1372b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1373b032f27cSSam Leffler 	return ni;
1374b032f27cSSam Leffler }
1375b032f27cSSam Leffler 
1376b032f27cSSam Leffler struct ieee80211_node *
1377b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1378b032f27cSSam Leffler ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1379b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1380b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1381b032f27cSSam Leffler #else
1382b032f27cSSam Leffler ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1383b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1384b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1385b032f27cSSam Leffler #endif
1386b032f27cSSam Leffler {
1387b032f27cSSam Leffler 	struct ieee80211_node *ni;
1388b032f27cSSam Leffler 	int hash;
1389b032f27cSSam Leffler 
1390b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1391b032f27cSSam Leffler 
139259aa14a9SRui Paulo 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1393b032f27cSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1394b032f27cSSam Leffler 		if (ni->ni_vap == vap &&
1395b032f27cSSam Leffler 		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1396b032f27cSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
1397b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1398b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1399b032f27cSSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1400b032f27cSSam Leffler 			    func, line,
1401b032f27cSSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
1402b032f27cSSam Leffler 			    ieee80211_node_refcnt(ni));
1403b032f27cSSam Leffler #endif
1404b032f27cSSam Leffler 			return ni;
1405b032f27cSSam Leffler 		}
1406b032f27cSSam Leffler 	}
1407b032f27cSSam Leffler 	return NULL;
1408b032f27cSSam Leffler }
1409b032f27cSSam Leffler 
1410b032f27cSSam Leffler struct ieee80211_node *
1411b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1412b032f27cSSam Leffler ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1413b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1414b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1415b032f27cSSam Leffler #else
1416b032f27cSSam Leffler ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1417b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1418b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1419b032f27cSSam Leffler #endif
1420b032f27cSSam Leffler {
1421b032f27cSSam Leffler 	struct ieee80211_node *ni;
1422b032f27cSSam Leffler 
1423b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1424b032f27cSSam Leffler 	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
14258a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
14261a1e1d21SSam Leffler 	return ni;
14271a1e1d21SSam Leffler }
14281a1e1d21SSam Leffler 
14291a1e1d21SSam Leffler /*
14308a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
14318a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
14328a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
14338a1b9b6aSSam Leffler  * it's private state.
14348a1b9b6aSSam Leffler  */
14358a1b9b6aSSam Leffler struct ieee80211_node *
1436b032f27cSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
143768e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
14388a1b9b6aSSam Leffler {
14398a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
14408a1b9b6aSSam Leffler 
1441d71a1f7aSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1442be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1443b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, macaddr);
14448a1b9b6aSSam Leffler 	if (ni != NULL) {
1445b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1446b032f27cSSam Leffler 
14478a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
1448b032f27cSSam Leffler 		ni->ni_rates = vap->iv_bss->ni_rates;
1449aa68c24fSSam Leffler 		if (ieee80211_iserp_rateset(&ni->ni_rates))
1450aa68c24fSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_ERP;
1451b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
14528e292e8eSSam Leffler 			/*
145368e8e04eSSam Leffler 			 * In adhoc demo mode there are no management
145468e8e04eSSam Leffler 			 * frames to use to discover neighbor capabilities,
145568e8e04eSSam Leffler 			 * so blindly propagate the local configuration
145668e8e04eSSam Leffler 			 * so we can do interesting things (e.g. use
145768e8e04eSSam Leffler 			 * WME to disable ACK's).
14588e292e8eSSam Leffler 			 */
1459b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_WME)
14608e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
1461616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1462b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_FF)
146368e8e04eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_FF;
1464616190d0SSam Leffler #endif
14658e292e8eSSam Leffler 		}
1466d77148fbSSam Leffler 		ieee80211_node_setuptxparms(ni);
146749d2c137SBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
1468b032f27cSSam Leffler 		if (ic->ic_newassoc != NULL)
1469b032f27cSSam Leffler 			ic->ic_newassoc(ni, 1);
147068e8e04eSSam Leffler 		/* XXX not right for 802.1x/WPA */
147168e8e04eSSam Leffler 		ieee80211_node_authorize(ni);
14728a1b9b6aSSam Leffler 	}
14738a1b9b6aSSam Leffler 	return ni;
14748a1b9b6aSSam Leffler }
14758a1b9b6aSSam Leffler 
1476be425a0fSSam Leffler void
1477be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
1478be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
1479be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
1480be425a0fSSam Leffler {
1481d71a1f7aSAdrian Chadd 	int do_ht_setup = 0;
1482d71a1f7aSAdrian Chadd 
1483be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
1484be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1485be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1486be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1487be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
1488be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
1489be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
1490be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1491be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1492be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1493be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
149459aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
149559aa14a9SRui Paulo 	if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
149659aa14a9SRui Paulo 		ieee80211_mesh_init_neighbor(ni, wh, sp);
149759aa14a9SRui Paulo #endif
1498b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1499b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
15009094ffdfSSam Leffler 		if (ni->ni_ies.wme_ie != NULL)
15019094ffdfSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
15029094ffdfSSam Leffler 		else
15039094ffdfSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1504616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1505b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
1506b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1507616190d0SSam Leffler #endif
1508d71a1f7aSAdrian Chadd 		if (ni->ni_ies.htcap_ie != NULL)
1509d71a1f7aSAdrian Chadd 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1510d71a1f7aSAdrian Chadd 		if (ni->ni_ies.htinfo_ie != NULL)
1511d71a1f7aSAdrian Chadd 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1512d71a1f7aSAdrian Chadd 
1513d71a1f7aSAdrian Chadd 		if ((ni->ni_ies.htcap_ie != NULL) &&
1514d71a1f7aSAdrian Chadd 		    (ni->ni_ies.htinfo_ie != NULL) &&
1515d71a1f7aSAdrian Chadd 		    (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1516d71a1f7aSAdrian Chadd 			do_ht_setup = 1;
1517d71a1f7aSAdrian Chadd 		}
1518b032f27cSSam Leffler 	}
1519be425a0fSSam Leffler 
1520be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
152179edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
152279edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
152379edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1524d71a1f7aSAdrian Chadd 
1525d71a1f7aSAdrian Chadd 	/*
1526d71a1f7aSAdrian Chadd 	 * If the neighbor is HT compatible, flip that on.
1527d71a1f7aSAdrian Chadd 	 */
1528d71a1f7aSAdrian Chadd 	if (do_ht_setup) {
1529d71a1f7aSAdrian Chadd 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1530d71a1f7aSAdrian Chadd 		    "%s: doing HT setup\n", __func__);
1531d71a1f7aSAdrian Chadd 		ieee80211_ht_node_init(ni);
1532d71a1f7aSAdrian Chadd 		ieee80211_ht_updateparams(ni,
1533d71a1f7aSAdrian Chadd 		    ni->ni_ies.htcap_ie,
1534d71a1f7aSAdrian Chadd 		    ni->ni_ies.htinfo_ie);
1535d71a1f7aSAdrian Chadd 		ieee80211_setup_htrates(ni,
1536d71a1f7aSAdrian Chadd 		    ni->ni_ies.htcap_ie,
1537d71a1f7aSAdrian Chadd 		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1538d71a1f7aSAdrian Chadd 		ieee80211_setup_basic_htrates(ni,
1539d71a1f7aSAdrian Chadd 		    ni->ni_ies.htinfo_ie);
1540d71a1f7aSAdrian Chadd 		ieee80211_node_setuptxparms(ni);
1541d71a1f7aSAdrian Chadd 		ieee80211_ratectl_node_init(ni);
1542d71a1f7aSAdrian Chadd 	}
1543be425a0fSSam Leffler }
1544be425a0fSSam Leffler 
1545b5c99415SSam Leffler /*
1546b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1547b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1548b5c99415SSam Leffler  * benefit we we treat this like an association so the
1549b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1550b5c99415SSam Leffler  */
1551b5c99415SSam Leffler struct ieee80211_node *
1552b032f27cSSam Leffler ieee80211_add_neighbor(struct ieee80211vap *vap,
1553b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1554b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1555b5c99415SSam Leffler {
1556b5c99415SSam Leffler 	struct ieee80211_node *ni;
1557b5c99415SSam Leffler 
1558d71a1f7aSAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1559be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1560b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1561b5c99415SSam Leffler 	if (ni != NULL) {
1562b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1563b032f27cSSam Leffler 
1564be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1565aa68c24fSSam Leffler 		if (ieee80211_iserp_rateset(&ni->ni_rates))
1566aa68c24fSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_ERP;
1567d77148fbSSam Leffler 		ieee80211_node_setuptxparms(ni);
156849d2c137SBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
1569b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1570b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1571b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1572b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1573b5c99415SSam Leffler 	}
1574b5c99415SSam Leffler 	return ni;
1575b5c99415SSam Leffler }
1576b5c99415SSam Leffler 
1577a2cfa5b7SSam Leffler #define	IS_PROBEREQ(wh) \
1578a2cfa5b7SSam Leffler 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1579a2cfa5b7SSam Leffler 	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1580a2cfa5b7SSam Leffler #define	IS_BCAST_PROBEREQ(wh) \
1581a2cfa5b7SSam Leffler 	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1582a2cfa5b7SSam Leffler 	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1583a2cfa5b7SSam Leffler 
1584a2cfa5b7SSam Leffler static __inline struct ieee80211_node *
1585a2cfa5b7SSam Leffler _find_rxnode(struct ieee80211_node_table *nt,
1586a2cfa5b7SSam Leffler     const struct ieee80211_frame_min *wh)
1587a2cfa5b7SSam Leffler {
1588a2cfa5b7SSam Leffler 	if (IS_BCAST_PROBEREQ(wh))
1589a2cfa5b7SSam Leffler 		return NULL;		/* spam bcast probe req to all vap's */
1590a2cfa5b7SSam Leffler 	return ieee80211_find_node_locked(nt, wh->i_addr2);
1591a2cfa5b7SSam Leffler }
159268e8e04eSSam Leffler 
15938a1b9b6aSSam Leffler /*
15948a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
1595a2cfa5b7SSam Leffler  * (referenced) node up to the 802.11 layer for its use.  Note
1596a2cfa5b7SSam Leffler  * we can return NULL if the sender is not in the table.
15978a1b9b6aSSam Leffler  */
15988a1b9b6aSSam Leffler struct ieee80211_node *
15998a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
16008a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
16018a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
16028a1b9b6aSSam Leffler #else
16038a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
16048a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
16058a1b9b6aSSam Leffler #endif
16068a1b9b6aSSam Leffler {
16078a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
16088a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16098a1b9b6aSSam Leffler 
161068e8e04eSSam Leffler 	nt = &ic->ic_sta;
16118a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1612a2cfa5b7SSam Leffler 	ni = _find_rxnode(nt, wh);
16138a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
16148a1b9b6aSSam Leffler 
1615c1225b52SSam Leffler 	return ni;
1616c1225b52SSam Leffler }
1617c1225b52SSam Leffler 
1618c1225b52SSam Leffler /*
1619c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1620c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1621c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1622c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1623c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1624c1225b52SSam Leffler  * key assigned to it.
1625c1225b52SSam Leffler  */
1626c1225b52SSam Leffler struct ieee80211_node *
1627c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1628c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1629c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1630c1225b52SSam Leffler 	const char *func, int line)
1631c1225b52SSam Leffler #else
1632c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1633c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1634c1225b52SSam Leffler #endif
1635c1225b52SSam Leffler {
1636c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1637c1225b52SSam Leffler 	struct ieee80211_node *ni;
1638c1225b52SSam Leffler 
1639c1225b52SSam Leffler 	nt = &ic->ic_sta;
1640c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1641c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1642c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1643c1225b52SSam Leffler 	else
1644c1225b52SSam Leffler 		ni = NULL;
1645c1225b52SSam Leffler 	if (ni == NULL) {
1646a2cfa5b7SSam Leffler 		ni = _find_rxnode(nt, wh);
1647b032f27cSSam Leffler 		if (ni != NULL && nt->nt_keyixmap != NULL) {
1648c1225b52SSam Leffler 			/*
1649c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1650c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1651c1225b52SSam Leffler 			 */
1652c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1653c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1654c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1655c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1656b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1657b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
1658c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1659c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1660c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1661c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1662c1225b52SSam Leffler 			}
1663c1225b52SSam Leffler 		}
1664a2cfa5b7SSam Leffler 	} else {
1665a2cfa5b7SSam Leffler 		if (IS_BCAST_PROBEREQ(wh))
1666a2cfa5b7SSam Leffler 			ni = NULL;	/* spam bcast probe req to all vap's */
1667a2cfa5b7SSam Leffler 		else
1668c1225b52SSam Leffler 			ieee80211_ref_node(ni);
1669a2cfa5b7SSam Leffler 	}
1670c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1671c1225b52SSam Leffler 
1672c1225b52SSam Leffler 	return ni;
1673c1225b52SSam Leffler }
1674a2cfa5b7SSam Leffler #undef IS_BCAST_PROBEREQ
1675a2cfa5b7SSam Leffler #undef IS_PROBEREQ
16768a1b9b6aSSam Leffler 
16778a1b9b6aSSam Leffler /*
1678750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1679750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1680750d6d0cSSam Leffler  */
1681750d6d0cSSam Leffler struct ieee80211_node *
16828a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1683b032f27cSSam Leffler ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1684b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN],
16858a1b9b6aSSam Leffler 	const char *func, int line)
16868a1b9b6aSSam Leffler #else
1687b032f27cSSam Leffler ieee80211_find_txnode(struct ieee80211vap *vap,
1688b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
16898a1b9b6aSSam Leffler #endif
1690750d6d0cSSam Leffler {
1691b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1692750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1693750d6d0cSSam Leffler 
1694750d6d0cSSam Leffler 	/*
1695750d6d0cSSam Leffler 	 * The destination address should be in the node table
1696c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1697c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1698c1225b52SSam Leffler 	 * to the bss node.
1699750d6d0cSSam Leffler 	 */
1700750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
17018a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1702b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA ||
1703b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_WDS ||
1704b032f27cSSam Leffler 	    IEEE80211_IS_MULTICAST(macaddr))
1705b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
17061b999d64SSam Leffler 	else
1707b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, macaddr);
17088a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
17098a1b9b6aSSam Leffler 
17108a1b9b6aSSam Leffler 	if (ni == NULL) {
1711b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1712b032f27cSSam Leffler 		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
171390d0d036SSam Leffler 			/*
171490d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
171590d0d036SSam Leffler 			 * Note that we need an additional reference for the
1716b032f27cSSam Leffler 			 * caller to be consistent with
1717b032f27cSSam Leffler 			 * ieee80211_find_node_locked.
171890d0d036SSam Leffler 			 */
1719b032f27cSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
172090d0d036SSam Leffler 			if (ni != NULL)
172190d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
172290d0d036SSam Leffler 		} else {
1723b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1724b032f27cSSam Leffler 			    "no node, discard frame (%s)", __func__);
1725b032f27cSSam Leffler 			vap->iv_stats.is_tx_nonode++;
1726750d6d0cSSam Leffler 		}
1727750d6d0cSSam Leffler 	}
1728750d6d0cSSam Leffler 	return ni;
1729750d6d0cSSam Leffler }
1730750d6d0cSSam Leffler 
17318a1b9b6aSSam Leffler static void
17328a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
17338a1b9b6aSSam Leffler {
17348a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
17358a1b9b6aSSam Leffler 
173637e9743aSSam Leffler 	/*
173737e9743aSSam Leffler 	 * NB: careful about referencing the vap as it may be
173837e9743aSSam Leffler 	 * gone if the last reference was held by a driver.
173937e9743aSSam Leffler 	 * We know the com will always be present so it's safe
174037e9743aSSam Leffler 	 * to use ni_ic below to reclaim resources.
174137e9743aSSam Leffler 	 */
174237e9743aSSam Leffler #if 0
1743b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
174449a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
174549a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
17468a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
174737e9743aSSam Leffler #endif
174837e9743aSSam Leffler 	if (ni->ni_associd != 0) {
174937e9743aSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
1750b032f27cSSam Leffler 		if (vap->iv_aid_bitmap != NULL)
1751b032f27cSSam Leffler 			IEEE80211_AID_CLR(vap, ni->ni_associd);
175237e9743aSSam Leffler 	}
17538a1b9b6aSSam Leffler 	if (nt != NULL) {
17548a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
17558a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
17568a1b9b6aSSam Leffler 	}
175737e9743aSSam Leffler 	ni->ni_ic->ic_node_free(ni);
17588a1b9b6aSSam Leffler }
17598a1b9b6aSSam Leffler 
17608a1b9b6aSSam Leffler void
17618a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
17628a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
17638a1b9b6aSSam Leffler #else
17648a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
17658a1b9b6aSSam Leffler #endif
17668a1b9b6aSSam Leffler {
17678a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
17688a1b9b6aSSam Leffler 
17698a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1770b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
177149a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
17728a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
17738a1b9b6aSSam Leffler #endif
1774c1225b52SSam Leffler 	if (nt != NULL) {
1775c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
17768a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
17778a1b9b6aSSam Leffler 			/*
1778c1225b52SSam Leffler 			 * Last reference, reclaim state.
17798a1b9b6aSSam Leffler 			 */
17808a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1781c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1782c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1783c1225b52SSam Leffler 			ieee80211_keyix keyix;
1784c1225b52SSam Leffler 			/*
1785c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1786c1225b52SSam Leffler 			 */
1787c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1788c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1789c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1790b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1791b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
1792c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1793c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1794c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1795c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
17968a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
17978a1b9b6aSSam Leffler 			}
17981a1e1d21SSam Leffler 		}
1799c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1800c1225b52SSam Leffler 	} else {
1801c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1802c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1803c1225b52SSam Leffler 	}
1804c1225b52SSam Leffler }
1805c1225b52SSam Leffler 
1806c1225b52SSam Leffler /*
1807c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1808c1225b52SSam Leffler  */
1809c1225b52SSam Leffler int
1810c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1811c1225b52SSam Leffler {
181237e9743aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
181337e9743aSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1814c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1815c1225b52SSam Leffler 	ieee80211_keyix keyix;
1816c1225b52SSam Leffler 	int isowned, status;
1817c1225b52SSam Leffler 
1818c1225b52SSam Leffler 	/*
1819c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1820c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1821c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1822c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1823c1225b52SSam Leffler 	 *
1824c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1825c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1826c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1827c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1828c1225b52SSam Leffler 	 * conditionally.
1829c1225b52SSam Leffler 	 */
1830c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1831c1225b52SSam Leffler 	if (!isowned)
1832c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
183337e9743aSSam Leffler 	nikey = NULL;
183437e9743aSSam Leffler 	status = 1;		/* NB: success */
1835ca92652aSSam Leffler 	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1836c1225b52SSam Leffler 		keyix = ni->ni_ucastkey.wk_rxkeyix;
183737e9743aSSam Leffler 		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1838c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1839c1225b52SSam Leffler 			nikey = nt->nt_keyixmap[keyix];
1840c2ede4b3SMartin Blapp 			nt->nt_keyixmap[keyix] = NULL;
184137e9743aSSam Leffler 		}
184237e9743aSSam Leffler 	}
1843c1225b52SSam Leffler 	if (!isowned)
1844b032f27cSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1845c1225b52SSam Leffler 
1846c1225b52SSam Leffler 	if (nikey != NULL) {
1847c1225b52SSam Leffler 		KASSERT(nikey == ni,
1848c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
184937e9743aSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1850c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1851c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1852c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1853c1225b52SSam Leffler 		ieee80211_free_node(ni);
1854c1225b52SSam Leffler 	}
1855c1225b52SSam Leffler 	return status;
1856c1225b52SSam Leffler }
18571a1e1d21SSam Leffler 
1858303ebc3cSSam Leffler /*
18598a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
18608a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
18618a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
18628a1b9b6aSSam Leffler  */
18638a1b9b6aSSam Leffler static void
18648a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
18658a1b9b6aSSam Leffler {
1866c1225b52SSam Leffler 	ieee80211_keyix keyix;
1867c1225b52SSam Leffler 
1868c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
18698a1b9b6aSSam Leffler 
1870b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
187149a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
187249a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
187349a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1874c1225b52SSam Leffler 	/*
1875c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1876c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1877c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1878c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1879c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1880c1225b52SSam Leffler 	 */
1881c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1882c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1883c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1884b032f27cSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
188573bac36fSSam Leffler 			"%s: %p<%s> clear key map entry %u\n",
188673bac36fSSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1887c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1888c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1889c1225b52SSam Leffler 	}
18908a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
18918a1b9b6aSSam Leffler 		/*
18928a1b9b6aSSam Leffler 		 * Other references are present, just remove the
18938a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
18948a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1895acc4f7f5SSam Leffler 		 * reclaimed.
18968a1b9b6aSSam Leffler 		 */
18978a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
18988a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
18998a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
19008a1b9b6aSSam Leffler 	} else
19018a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
19028a1b9b6aSSam Leffler }
19038a1b9b6aSSam Leffler 
1904b032f27cSSam Leffler /*
1905b032f27cSSam Leffler  * Node table support.
1906b032f27cSSam Leffler  */
1907b032f27cSSam Leffler 
1908b032f27cSSam Leffler static void
1909b032f27cSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
1910b032f27cSSam Leffler 	struct ieee80211_node_table *nt,
1911b032f27cSSam Leffler 	const char *name, int inact, int keyixmax)
1912b032f27cSSam Leffler {
1913b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1914b032f27cSSam Leffler 
1915b032f27cSSam Leffler 	nt->nt_ic = ic;
1916b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1917b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1918b032f27cSSam Leffler 	TAILQ_INIT(&nt->nt_node);
1919b032f27cSSam Leffler 	nt->nt_name = name;
1920b032f27cSSam Leffler 	nt->nt_scangen = 1;
1921b032f27cSSam Leffler 	nt->nt_inact_init = inact;
1922b032f27cSSam Leffler 	nt->nt_keyixmax = keyixmax;
1923b032f27cSSam Leffler 	if (nt->nt_keyixmax > 0) {
1924e2126decSSam Leffler 		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1925c5abbba3SDag-Erling Smørgrav 			keyixmax * sizeof(struct ieee80211_node *),
1926b032f27cSSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
1927b032f27cSSam Leffler 		if (nt->nt_keyixmap == NULL)
1928b032f27cSSam Leffler 			if_printf(ic->ic_ifp,
1929b032f27cSSam Leffler 			    "Cannot allocate key index map with %u entries\n",
1930b032f27cSSam Leffler 			    keyixmax);
1931b032f27cSSam Leffler 	} else
1932b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
1933b032f27cSSam Leffler }
1934b032f27cSSam Leffler 
1935b032f27cSSam Leffler static void
1936b032f27cSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1937b032f27cSSam Leffler 	struct ieee80211vap *match)
1938b032f27cSSam Leffler {
1939b032f27cSSam Leffler 	struct ieee80211_node *ni, *next;
1940b032f27cSSam Leffler 
1941b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1942b032f27cSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1943b032f27cSSam Leffler 		if (match != NULL && ni->ni_vap != match)
1944b032f27cSSam Leffler 			continue;
1945b032f27cSSam Leffler 		/* XXX can this happen?  if so need's work */
1946b032f27cSSam Leffler 		if (ni->ni_associd != 0) {
1947b032f27cSSam Leffler 			struct ieee80211vap *vap = ni->ni_vap;
1948b032f27cSSam Leffler 
1949b032f27cSSam Leffler 			if (vap->iv_auth->ia_node_leave != NULL)
1950b032f27cSSam Leffler 				vap->iv_auth->ia_node_leave(ni);
1951b032f27cSSam Leffler 			if (vap->iv_aid_bitmap != NULL)
1952b032f27cSSam Leffler 				IEEE80211_AID_CLR(vap, ni->ni_associd);
1953b032f27cSSam Leffler 		}
1954b032f27cSSam Leffler 		ni->ni_wdsvap = NULL;		/* clear reference */
19558a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
19568a1b9b6aSSam Leffler 	}
1957b032f27cSSam Leffler 	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1958b032f27cSSam Leffler 		/*
1959b032f27cSSam Leffler 		 * Make a separate pass to clear references to this vap
1960b032f27cSSam Leffler 		 * held by DWDS entries.  They will not be matched above
1961b032f27cSSam Leffler 		 * because ni_vap will point to the ap vap but we still
1962b032f27cSSam Leffler 		 * need to clear ni_wdsvap when the WDS vap is destroyed
1963b032f27cSSam Leffler 		 * and/or reset.
1964b032f27cSSam Leffler 		 */
1965b032f27cSSam Leffler 		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1966b032f27cSSam Leffler 			if (ni->ni_wdsvap == match)
1967b032f27cSSam Leffler 				ni->ni_wdsvap = NULL;
1968b032f27cSSam Leffler 	}
1969b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1970b032f27cSSam Leffler }
1971b032f27cSSam Leffler 
1972b032f27cSSam Leffler static void
1973b032f27cSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1974b032f27cSSam Leffler {
1975b032f27cSSam Leffler 	ieee80211_node_table_reset(nt, NULL);
1976b032f27cSSam Leffler 	if (nt->nt_keyixmap != NULL) {
1977b032f27cSSam Leffler #ifdef DIAGNOSTIC
1978b032f27cSSam Leffler 		/* XXX verify all entries are NULL */
1979b032f27cSSam Leffler 		int i;
1980b032f27cSSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
1981b032f27cSSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
1982b032f27cSSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
1983b032f27cSSam Leffler 					nt->nt_name, i);
1984b032f27cSSam Leffler #endif
1985e2126decSSam Leffler 		free(nt->nt_keyixmap, M_80211_NODE);
1986b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
1987b032f27cSSam Leffler 	}
1988b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1989b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
19908a1b9b6aSSam Leffler }
19918a1b9b6aSSam Leffler 
19928a1b9b6aSSam Leffler /*
19938a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
19948a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
19958a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
19968a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
19978a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
19988a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1999303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
2000303ebc3cSSam Leffler  * process each node only once.
2001303ebc3cSSam Leffler  */
20028a1b9b6aSSam Leffler static void
2003b032f27cSSam Leffler ieee80211_timeout_stations(struct ieee80211com *ic)
20041a1e1d21SSam Leffler {
2005b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
2006b032f27cSSam Leffler 	struct ieee80211vap *vap;
2007303ebc3cSSam Leffler 	struct ieee80211_node *ni;
2008b032f27cSSam Leffler 	int gen = 0;
20091a1e1d21SSam Leffler 
2010b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK(nt);
2011a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
2012303ebc3cSSam Leffler restart:
20138a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
20148a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2015303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
2016303ebc3cSSam Leffler 			continue;
2017303ebc3cSSam Leffler 		ni->ni_scangen = gen;
20180a915fadSSam Leffler 		/*
2019ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
2020ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
2021ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
2022ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
2023ebdda46cSSam Leffler 		 */
2024b032f27cSSam Leffler 		vap = ni->ni_vap;
2025b032f27cSSam Leffler 		/*
2026b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
2027b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
2028b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
2029b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
2030b032f27cSSam Leffler 		 * it applies to more than timeout processing.
2031b032f27cSSam Leffler 		 */
2032b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
2033b032f27cSSam Leffler 			continue;
2034b032f27cSSam Leffler 		/* XXX can vap be NULL? */
2035b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2036b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
203744b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2038ebdda46cSSam Leffler 			continue;
2039ebdda46cSSam Leffler 		/*
20408a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
20418a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
2042b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_age
20430a915fadSSam Leffler 		 */
20448a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
20458a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
20468a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
20478a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
20488a1b9b6aSSam Leffler 		}
2049be1054edSSam Leffler 		if (ni->ni_inact > 0) {
2050c066143cSSam Leffler 			ni->ni_inact--;
2051be1054edSSam Leffler 			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2052be1054edSSam Leffler 			    "%s: inact %u inact_reload %u nrates %u",
2053be1054edSSam Leffler 			    __func__, ni->ni_inact, ni->ni_inact_reload,
2054be1054edSSam Leffler 			    ni->ni_rates.rs_nrates);
2055be1054edSSam Leffler 		}
2056ce647032SSam Leffler 		/*
2057ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
2058ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
2059b032f27cSSam Leffler 		 * XXX run ic_node_age
2060ce647032SSam Leffler 		 */
2061b032f27cSSam Leffler 		if (ni == vap->iv_bss)
2062ce647032SSam Leffler 			continue;
2063b032f27cSSam Leffler 		if (ni->ni_associd != 0 ||
2064b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_IBSS ||
2065b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
20668a1b9b6aSSam Leffler 			/*
2067b032f27cSSam Leffler 			 * Age/drain resources held by the station.
20688a1b9b6aSSam Leffler 			 */
2069b032f27cSSam Leffler 			ic->ic_node_age(ni);
20708a1b9b6aSSam Leffler 			/*
20718a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
20728a1b9b6aSSam Leffler 			 * send a null data frame which may not be
20738a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
20748a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
207568e8e04eSSam Leffler 			 *
207668e8e04eSSam Leffler 			 * XXX don't probe the station unless we've
207768e8e04eSSam Leffler 			 *     received a frame from them (and have
207868e8e04eSSam Leffler 			 *     some idea of the rates they are capable
207968e8e04eSSam Leffler 			 *     of); this will get fixed more properly
208068e8e04eSSam Leffler 			 *     soon with better handling of the rate set.
20818a1b9b6aSSam Leffler 			 */
2082b032f27cSSam Leffler 			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2083c066143cSSam Leffler 			    (0 < ni->ni_inact &&
2084b032f27cSSam Leffler 			     ni->ni_inact <= vap->iv_inact_probe) &&
208568e8e04eSSam Leffler 			    ni->ni_rates.rs_nrates != 0) {
2086b032f27cSSam Leffler 				IEEE80211_NOTE(vap,
2087f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2088f66d97f6SSam Leffler 				    ni, "%s",
2089f66d97f6SSam Leffler 				    "probe station due to inactivity");
209019ad2dd7SSam Leffler 				/*
209119ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
209219ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
209319ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
209419ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
209519ad2dd7SSam Leffler 				 * ref for us as needed.
209619ad2dd7SSam Leffler 				 */
209719ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
20988a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
2099f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
21008a1b9b6aSSam Leffler 				/* XXX stat? */
21018a1b9b6aSSam Leffler 				goto restart;
21028a1b9b6aSSam Leffler 			}
21038a1b9b6aSSam Leffler 		}
2104b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2105c066143cSSam Leffler 		    ni->ni_inact <= 0) {
2106b032f27cSSam Leffler 			IEEE80211_NOTE(vap,
2107f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2108f66d97f6SSam Leffler 			    "station timed out due to inactivity "
2109f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
21108a1b9b6aSSam Leffler 			/*
21118a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
21128a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
21138a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
21148a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
21158a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
21168a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
21178a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
21188a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
21198a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
21208a1b9b6aSSam Leffler 			 *
21218a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
212268e8e04eSSam Leffler 			 * in case the driver takes a lock, as this can result
212368e8e04eSSam Leffler 			 * in a LOR between the node lock and the driver lock.
21248a1b9b6aSSam Leffler 			 */
21255698ab1aSSam Leffler 			ieee80211_ref_node(ni);
21268a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
21278a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
2128b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
21291a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
21301a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
21318a1b9b6aSSam Leffler 			}
2132b032f27cSSam Leffler 			ieee80211_node_leave(ni);
21335698ab1aSSam Leffler 			ieee80211_free_node(ni);
2134b032f27cSSam Leffler 			vap->iv_stats.is_node_timeout++;
2135303ebc3cSSam Leffler 			goto restart;
2136303ebc3cSSam Leffler 		}
21371a1e1d21SSam Leffler 	}
21388a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
21398a1b9b6aSSam Leffler 
2140b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_UNLOCK(nt);
214168e8e04eSSam Leffler }
21428a1b9b6aSSam Leffler 
2143b032f27cSSam Leffler /*
2144b032f27cSSam Leffler  * Aggressively reclaim resources.  This should be used
2145b032f27cSSam Leffler  * only in a critical situation to reclaim mbuf resources.
2146b032f27cSSam Leffler  */
2147b032f27cSSam Leffler void
2148b032f27cSSam Leffler ieee80211_drain(struct ieee80211com *ic)
2149b032f27cSSam Leffler {
2150b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
2151b032f27cSSam Leffler 	struct ieee80211vap *vap;
2152b032f27cSSam Leffler 	struct ieee80211_node *ni;
2153b032f27cSSam Leffler 
2154b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
2155b032f27cSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2156b032f27cSSam Leffler 		/*
2157b032f27cSSam Leffler 		 * Ignore entries for which have yet to receive an
2158b032f27cSSam Leffler 		 * authentication frame.  These are transient and
2159b032f27cSSam Leffler 		 * will be reclaimed when the last reference to them
2160b032f27cSSam Leffler 		 * goes away (when frame xmits complete).
2161b032f27cSSam Leffler 		 */
2162b032f27cSSam Leffler 		vap = ni->ni_vap;
2163b032f27cSSam Leffler 		/*
2164b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
2165b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
2166b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
2167b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
2168b032f27cSSam Leffler 		 * it applies to more than timeout processing.
2169b032f27cSSam Leffler 		 */
2170b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
2171b032f27cSSam Leffler 			continue;
2172b032f27cSSam Leffler 		/* XXX can vap be NULL? */
2173b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2174b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
2175b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2176b032f27cSSam Leffler 			continue;
2177b032f27cSSam Leffler 		/*
2178b032f27cSSam Leffler 		 * Free fragments.
2179b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_drain
2180b032f27cSSam Leffler 		 */
2181b032f27cSSam Leffler 		if (ni->ni_rxfrag[0] != NULL) {
2182b032f27cSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
2183b032f27cSSam Leffler 			ni->ni_rxfrag[0] = NULL;
2184b032f27cSSam Leffler 		}
2185b032f27cSSam Leffler 		/*
2186b032f27cSSam Leffler 		 * Drain resources held by the station.
2187b032f27cSSam Leffler 		 */
2188b032f27cSSam Leffler 		ic->ic_node_drain(ni);
2189b032f27cSSam Leffler 	}
2190b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
2191b032f27cSSam Leffler }
2192b032f27cSSam Leffler 
2193b032f27cSSam Leffler /*
2194b032f27cSSam Leffler  * Per-ieee80211com inactivity timer callback.
2195b032f27cSSam Leffler  */
219668e8e04eSSam Leffler void
219768e8e04eSSam Leffler ieee80211_node_timeout(void *arg)
219868e8e04eSSam Leffler {
219968e8e04eSSam Leffler 	struct ieee80211com *ic = arg;
220068e8e04eSSam Leffler 
2201b032f27cSSam Leffler 	/*
2202b032f27cSSam Leffler 	 * Defer timeout processing if a channel switch is pending.
2203b032f27cSSam Leffler 	 * We typically need to be mute so not doing things that
2204b032f27cSSam Leffler 	 * might generate frames is good to handle in one place.
2205b032f27cSSam Leffler 	 * Supressing the station timeout processing may extend the
2206b032f27cSSam Leffler 	 * lifetime of inactive stations (by not decrementing their
2207b032f27cSSam Leffler 	 * idle counters) but this should be ok unless the CSA is
2208b032f27cSSam Leffler 	 * active for an unusually long time.
2209b032f27cSSam Leffler 	 */
2210b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
221168e8e04eSSam Leffler 		ieee80211_scan_timeout(ic);
2212b032f27cSSam Leffler 		ieee80211_timeout_stations(ic);
22135b16c28cSSam Leffler 		ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
221468e8e04eSSam Leffler 
2215b105a069SSam Leffler 		IEEE80211_LOCK(ic);
2216b105a069SSam Leffler 		ieee80211_erp_timeout(ic);
22171b6167d2SSam Leffler 		ieee80211_ht_timeout(ic);
2218b105a069SSam Leffler 		IEEE80211_UNLOCK(ic);
2219b032f27cSSam Leffler 	}
222068e8e04eSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
222168e8e04eSSam Leffler 		ieee80211_node_timeout, ic);
22221a1e1d21SSam Leffler }
22231a1e1d21SSam Leffler 
22247d684b4bSAdrian Chadd /*
22257d684b4bSAdrian Chadd  * Iterate over the node table and return an array of ref'ed nodes.
22267d684b4bSAdrian Chadd  *
22277d684b4bSAdrian Chadd  * This is separated out from calling the actual node function so that
22287d684b4bSAdrian Chadd  * no LORs will occur.
22297d684b4bSAdrian Chadd  *
22307d684b4bSAdrian Chadd  * If there are too many nodes (ie, the number of nodes doesn't fit
22317d684b4bSAdrian Chadd  * within 'max_aid' entries) then the node references will be freed
22327d684b4bSAdrian Chadd  * and an error will be returned.
22337d684b4bSAdrian Chadd  *
22347d684b4bSAdrian Chadd  * The responsibility of allocating and freeing "ni_arr" is up to
22357d684b4bSAdrian Chadd  * the caller.
22367d684b4bSAdrian Chadd  */
22377d684b4bSAdrian Chadd int
22387d684b4bSAdrian Chadd ieee80211_iterate_nt(struct ieee80211_node_table *nt,
22397d684b4bSAdrian Chadd     struct ieee80211_node **ni_arr, uint16_t max_aid)
22407d684b4bSAdrian Chadd {
22417d684b4bSAdrian Chadd 	u_int gen;
22427d684b4bSAdrian Chadd 	int i, j, ret;
22437d684b4bSAdrian Chadd 	struct ieee80211_node *ni;
22447d684b4bSAdrian Chadd 
22457d684b4bSAdrian Chadd 	IEEE80211_NODE_ITERATE_LOCK(nt);
22467d684b4bSAdrian Chadd 	IEEE80211_NODE_LOCK(nt);
22477d684b4bSAdrian Chadd 
22487d684b4bSAdrian Chadd 	gen = ++nt->nt_scangen;
22497d684b4bSAdrian Chadd 	i = ret = 0;
22507d684b4bSAdrian Chadd 
22517d684b4bSAdrian Chadd 	/*
22527d684b4bSAdrian Chadd 	 * We simply assume here that since the node
22537d684b4bSAdrian Chadd 	 * scan generation doesn't change (as
22547d684b4bSAdrian Chadd 	 * we are holding both the node table and
22557d684b4bSAdrian Chadd 	 * node table iteration locks), we can simply
22567d684b4bSAdrian Chadd 	 * assign it to the node here.
22577d684b4bSAdrian Chadd 	 */
22587d684b4bSAdrian Chadd 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
22597d684b4bSAdrian Chadd 		if (i >= max_aid) {
22607d684b4bSAdrian Chadd 			ret = E2BIG;
22617d684b4bSAdrian Chadd 			if_printf(nt->nt_ic->ic_ifp,
22627d684b4bSAdrian Chadd 			    "Node array overflow: max=%u", max_aid);
22637d684b4bSAdrian Chadd 			break;
22647d684b4bSAdrian Chadd 		}
22657d684b4bSAdrian Chadd 		ni_arr[i] = ieee80211_ref_node(ni);
22667d684b4bSAdrian Chadd 		ni_arr[i]->ni_scangen = gen;
22677d684b4bSAdrian Chadd 		i++;
22687d684b4bSAdrian Chadd 	}
22697d684b4bSAdrian Chadd 
22707d684b4bSAdrian Chadd 	/*
22717d684b4bSAdrian Chadd 	 * It's safe to unlock here.
22727d684b4bSAdrian Chadd 	 *
22737d684b4bSAdrian Chadd 	 * If we're successful, the list is returned.
22747d684b4bSAdrian Chadd 	 * If we're unsuccessful, the list is ignored
22757d684b4bSAdrian Chadd 	 * and we remove our references.
22767d684b4bSAdrian Chadd 	 *
22777d684b4bSAdrian Chadd 	 * This avoids any potential LOR with
22787d684b4bSAdrian Chadd 	 * ieee80211_free_node().
22797d684b4bSAdrian Chadd 	 */
22807d684b4bSAdrian Chadd 	IEEE80211_NODE_UNLOCK(nt);
22817d684b4bSAdrian Chadd 	IEEE80211_NODE_ITERATE_UNLOCK(nt);
22827d684b4bSAdrian Chadd 
22837d684b4bSAdrian Chadd 	/*
22847d684b4bSAdrian Chadd 	 * If ret is non-zero, we hit some kind of error.
22857d684b4bSAdrian Chadd 	 * Rather than walking some nodes, we'll walk none
22867d684b4bSAdrian Chadd 	 * of them.
22877d684b4bSAdrian Chadd 	 */
22887d684b4bSAdrian Chadd 	if (ret) {
22897d684b4bSAdrian Chadd 		for (j = 0; j < i; j++) {
22907d684b4bSAdrian Chadd 			/* ieee80211_free_node() locks by itself */
22917d684b4bSAdrian Chadd 			ieee80211_free_node(ni_arr[j]);
22927d684b4bSAdrian Chadd 		}
22937d684b4bSAdrian Chadd 	}
22947d684b4bSAdrian Chadd 
22957d684b4bSAdrian Chadd 	return (ret);
22967d684b4bSAdrian Chadd }
22977d684b4bSAdrian Chadd 
22987d684b4bSAdrian Chadd /*
22997d684b4bSAdrian Chadd  * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
23007d684b4bSAdrian Chadd  * reference in the source.
23017d684b4bSAdrian Chadd  *
23027d684b4bSAdrian Chadd  * Note that this fetches 'max_aid' from the first VAP, rather than finding
23037d684b4bSAdrian Chadd  * the largest max_aid from all VAPs.
23047d684b4bSAdrian Chadd  */
23051a1e1d21SSam Leffler void
2306b032f27cSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2307b032f27cSSam Leffler 	ieee80211_iter_func *f, void *arg)
23081a1e1d21SSam Leffler {
23097d684b4bSAdrian Chadd 	struct ieee80211_node **ni_arr;
2310b6afbb79SAdrian Chadd 	size_t size;
23117d684b4bSAdrian Chadd 	int i;
23127d684b4bSAdrian Chadd 	uint16_t max_aid;
23137b5a3435SAdrian Chadd 	struct ieee80211vap *vap;
23141a1e1d21SSam Leffler 
23157b5a3435SAdrian Chadd 	/* Overdoing it default */
23167b5a3435SAdrian Chadd 	max_aid = IEEE80211_AID_MAX;
23177b5a3435SAdrian Chadd 
23187b5a3435SAdrian Chadd 	/* Handle the case of there being no vaps just yet */
23197b5a3435SAdrian Chadd 	vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps);
23207b5a3435SAdrian Chadd 	if (vap != NULL)
23217b5a3435SAdrian Chadd 		max_aid = vap->iv_max_aid;
23227b5a3435SAdrian Chadd 
23237d684b4bSAdrian Chadd 	size = max_aid * sizeof(struct ieee80211_node *);
23247d684b4bSAdrian Chadd 	ni_arr = (struct ieee80211_node **) malloc(size, M_80211_NODE,
23257d684b4bSAdrian Chadd 	    M_NOWAIT | M_ZERO);
23267d684b4bSAdrian Chadd 	if (ni_arr == NULL)
23277d684b4bSAdrian Chadd 		return;
23288a1b9b6aSSam Leffler 
23297d684b4bSAdrian Chadd 	/*
23307d684b4bSAdrian Chadd 	 * If this fails, the node table won't have any
23317d684b4bSAdrian Chadd 	 * valid entries - ieee80211_iterate_nt() frees
23327d684b4bSAdrian Chadd 	 * the references to them.  So don't try walking
23337d684b4bSAdrian Chadd 	 * the table; just skip to the end and free the
23347d684b4bSAdrian Chadd 	 * temporary memory.
23357d684b4bSAdrian Chadd 	 */
2336b6afbb79SAdrian Chadd 	if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0)
23377d684b4bSAdrian Chadd 		goto done;
23387d684b4bSAdrian Chadd 
23397d684b4bSAdrian Chadd 	for (i = 0; i < max_aid; i++) {
23407d684b4bSAdrian Chadd 		if (ni_arr[i] == NULL)	/* end of the list */
23417d684b4bSAdrian Chadd 			break;
23427d684b4bSAdrian Chadd 		(*f)(arg, ni_arr[i]);
23437d684b4bSAdrian Chadd 		/* ieee80211_free_node() locks by itself */
23447d684b4bSAdrian Chadd 		ieee80211_free_node(ni_arr[i]);
23457d684b4bSAdrian Chadd 	}
23467d684b4bSAdrian Chadd 
23477d684b4bSAdrian Chadd done:
23487d684b4bSAdrian Chadd 	free(ni_arr, M_80211_NODE);
23498a1b9b6aSSam Leffler }
23508a1b9b6aSSam Leffler 
23518a1b9b6aSSam Leffler void
23528a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
23538a1b9b6aSSam Leffler {
23548a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
23558a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
23568a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
23578a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
23588a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
23598a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
23608a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2361801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2362801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2363801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
23648a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
23655463c4a4SSam Leffler 	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
23665463c4a4SSam Leffler 		node_getrssi(ni), ni->ni_noise,
236768e8e04eSSam Leffler 		ni->ni_intval, ni->ni_capinfo);
23688a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
23698a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
23708a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
23718a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2372be1054edSSam Leffler 	printf("\tinact %u inact_reload %u txrate %u\n",
2373be1054edSSam Leffler 		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
237468e8e04eSSam Leffler 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
237568e8e04eSSam Leffler 		ni->ni_htcap, ni->ni_htparam,
237668e8e04eSSam Leffler 		ni->ni_htctlchan, ni->ni_ht2ndchan);
237768e8e04eSSam Leffler 	printf("\thtopmode %x htstbc %x chw %u\n",
237868e8e04eSSam Leffler 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
23798a1b9b6aSSam Leffler }
23808a1b9b6aSSam Leffler 
23818a1b9b6aSSam Leffler void
23828a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
23838a1b9b6aSSam Leffler {
23848a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
23858a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
23868a1b9b6aSSam Leffler }
23878a1b9b6aSSam Leffler 
23882dc4d8dcSSam Leffler static void
23892dc4d8dcSSam Leffler ieee80211_notify_erp_locked(struct ieee80211com *ic)
2390b105a069SSam Leffler {
2391b032f27cSSam Leffler 	struct ieee80211vap *vap;
2392b032f27cSSam Leffler 
2393b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2394b032f27cSSam Leffler 
2395b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2396b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2397b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2398b105a069SSam Leffler }
2399b105a069SSam Leffler 
24002dc4d8dcSSam Leffler void
24012dc4d8dcSSam Leffler ieee80211_notify_erp(struct ieee80211com *ic)
24022dc4d8dcSSam Leffler {
24032dc4d8dcSSam Leffler 	IEEE80211_LOCK(ic);
24042dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
24052dc4d8dcSSam Leffler 	IEEE80211_UNLOCK(ic);
24062dc4d8dcSSam Leffler }
24072dc4d8dcSSam Leffler 
24088a1b9b6aSSam Leffler /*
24098a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
24108a1b9b6aSSam Leffler  */
24118a1b9b6aSSam Leffler static void
2412b032f27cSSam Leffler ieee80211_node_join_11g(struct ieee80211_node *ni)
24138a1b9b6aSSam Leffler {
2414b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
24158a1b9b6aSSam Leffler 
24161b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
24171b6167d2SSam Leffler 
24188a1b9b6aSSam Leffler 	/*
24198a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
24208a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
24218a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
24228a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
24238a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
24248a1b9b6aSSam Leffler 	 */
24258a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
24268a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
2427b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2428b105a069SSam Leffler 		    "station needs long slot time, count %d",
2429b105a069SSam Leffler 		    ic->ic_longslotsta);
24308a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
243168e8e04eSSam Leffler 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
243268e8e04eSSam Leffler 			/*
243368e8e04eSSam Leffler 			 * Don't force slot time when switched to turbo
243468e8e04eSSam Leffler 			 * mode as non-ERP stations won't be present; this
243568e8e04eSSam Leffler 			 * need only be done when on the normal G channel.
243668e8e04eSSam Leffler 			 */
24378a1b9b6aSSam Leffler 			ieee80211_set_shortslottime(ic, 0);
24388a1b9b6aSSam Leffler 		}
243968e8e04eSSam Leffler 	}
24408a1b9b6aSSam Leffler 	/*
24418a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
24428a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
24438a1b9b6aSSam Leffler 	 * if configured.
24448a1b9b6aSSam Leffler 	 */
2445b032f27cSSam Leffler 	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
24468a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
2447b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2448b105a069SSam Leffler 		    "station is !ERP, %d non-ERP stations associated",
2449b105a069SSam Leffler 		    ic->ic_nonerpsta);
24508a1b9b6aSSam Leffler 		/*
24518a1b9b6aSSam Leffler 		 * If station does not support short preamble
24528a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
24538a1b9b6aSSam Leffler 		 */
24548a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2455b032f27cSSam Leffler 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2456b105a069SSam Leffler 			    "%s", "station needs long preamble");
24578a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
24588a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
24598a1b9b6aSSam Leffler 		}
2460b105a069SSam Leffler 		/*
2461b032f27cSSam Leffler 		 * If protection is configured and this is the first
2462b032f27cSSam Leffler 		 * indication we should use protection, enable it.
2463b105a069SSam Leffler 		 */
2464b105a069SSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2465b105a069SSam Leffler 		    ic->ic_nonerpsta == 1 &&
2466b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2467b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2468b105a069SSam Leffler 			    "%s: enable use of protection\n", __func__);
2469b105a069SSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
24702dc4d8dcSSam Leffler 			ieee80211_notify_erp_locked(ic);
2471b105a069SSam Leffler 		}
24728a1b9b6aSSam Leffler 	} else
24738a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
24748a1b9b6aSSam Leffler }
24758a1b9b6aSSam Leffler 
24768a1b9b6aSSam Leffler void
2477b032f27cSSam Leffler ieee80211_node_join(struct ieee80211_node *ni, int resp)
24788a1b9b6aSSam Leffler {
2479b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2480b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
24818a1b9b6aSSam Leffler 	int newassoc;
24828a1b9b6aSSam Leffler 
24838a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
248468e8e04eSSam Leffler 		uint16_t aid;
24858a1b9b6aSSam Leffler 
2486b032f27cSSam Leffler 		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
24878a1b9b6aSSam Leffler 		/*
24888a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
24898a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
24908a1b9b6aSSam Leffler 		 */
2491b032f27cSSam Leffler 		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2492b032f27cSSam Leffler 			if (!IEEE80211_AID_ISSET(vap, aid))
24938a1b9b6aSSam Leffler 				break;
24948a1b9b6aSSam Leffler 		}
2495b032f27cSSam Leffler 		if (aid >= vap->iv_max_aid) {
249613f91245SSam Leffler 			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2497b032f27cSSam Leffler 			ieee80211_node_leave(ni);
24988a1b9b6aSSam Leffler 			return;
24998a1b9b6aSSam Leffler 		}
25008a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
25011b6167d2SSam Leffler 		ni->ni_jointime = time_uptime;
2502b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
2503b032f27cSSam Leffler 		IEEE80211_AID_SET(vap, ni->ni_associd);
2504b032f27cSSam Leffler 		vap->iv_sta_assoc++;
25058a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
25061b6167d2SSam Leffler 
25071b6167d2SSam Leffler 		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
25081b6167d2SSam Leffler 			ieee80211_ht_node_join(ni);
250968e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
251068e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2511b032f27cSSam Leffler 			ieee80211_node_join_11g(ni);
25121b6167d2SSam Leffler 		IEEE80211_UNLOCK(ic);
25131b6167d2SSam Leffler 
25141b6167d2SSam Leffler 		newassoc = 1;
25158a1b9b6aSSam Leffler 	} else
25168a1b9b6aSSam Leffler 		newassoc = 0;
25178a1b9b6aSSam Leffler 
2518b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
251944f7a6edSSam Leffler 	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2520b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
2521b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2522b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2523b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
252468e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
252568e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_HT ?
25260f52b1c4SSam Leffler 		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
25271b6167d2SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
25288c070d69SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
25298c070d69SSam Leffler 	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
253044f7a6edSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2531b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
253268e8e04eSSam Leffler 		", fast-frames" : "",
2533b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
253468e8e04eSSam Leffler 		", turbo" : ""
2535b8fcf546SSam Leffler 	);
25368a1b9b6aSSam Leffler 
2537d77148fbSSam Leffler 	ieee80211_node_setuptxparms(ni);
253849d2c137SBernhard Schmidt 	ieee80211_ratectl_node_init(ni);
25398a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
2540736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
2541e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
2542b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
25438a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
2544b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_join != NULL)
2545b032f27cSSam Leffler 		vap->iv_auth->ia_node_join(ni);
2546b032f27cSSam Leffler 	ieee80211_notify_node_join(ni,
2547ce8977dfSSam Leffler 	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
25488a1b9b6aSSam Leffler }
25498a1b9b6aSSam Leffler 
2550b105a069SSam Leffler static void
2551b105a069SSam Leffler disable_protection(struct ieee80211com *ic)
2552b105a069SSam Leffler {
2553b105a069SSam Leffler 	KASSERT(ic->ic_nonerpsta == 0 &&
2554b105a069SSam Leffler 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2555b105a069SSam Leffler 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2556b105a069SSam Leffler 	   ic->ic_flags_ext));
2557b105a069SSam Leffler 
2558b105a069SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2559b105a069SSam Leffler 	/* XXX verify mode? */
2560b105a069SSam Leffler 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2561b105a069SSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2562b105a069SSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2563b105a069SSam Leffler 	}
25642dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
2565b105a069SSam Leffler }
2566b105a069SSam Leffler 
25678a1b9b6aSSam Leffler /*
25688a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
25698a1b9b6aSSam Leffler  */
25708a1b9b6aSSam Leffler static void
2571b032f27cSSam Leffler ieee80211_node_leave_11g(struct ieee80211_node *ni)
25728a1b9b6aSSam Leffler {
2573b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
25748a1b9b6aSSam Leffler 
25751b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
25761b6167d2SSam Leffler 
257768e8e04eSSam Leffler 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2578b032f27cSSam Leffler 	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2579b032f27cSSam Leffler 	      ic->ic_bsschan->ic_flags));
25808a1b9b6aSSam Leffler 
25818a1b9b6aSSam Leffler 	/*
25828a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
25838a1b9b6aSSam Leffler 	 */
25848a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
25858a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
25868a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
25878a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
2588b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2589b105a069SSam Leffler 		    "long slot time station leaves, count now %d",
2590b105a069SSam Leffler 		    ic->ic_longslotsta);
25918a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
25928a1b9b6aSSam Leffler 			/*
25938a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
25948a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
25958a1b9b6aSSam Leffler 			 */
25968a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
25978a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
2598b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
2599b032f27cSSam Leffler 				    IEEE80211_MSG_ASSOC,
26008a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
26018a1b9b6aSSam Leffler 				    __func__);
26028a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
26038a1b9b6aSSam Leffler 			}
26048a1b9b6aSSam Leffler 		}
26058a1b9b6aSSam Leffler 	}
26068a1b9b6aSSam Leffler 	/*
26078a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
26088a1b9b6aSSam Leffler 	 */
26098a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
26108a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
26118a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
26128a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
2613b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2614b105a069SSam Leffler 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2615b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2616b105a069SSam Leffler 			" (non-ERP sta present)" : "");
2617b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0 &&
2618b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2619b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
26208a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
2621b105a069SSam Leffler 			disable_protection(ic);
2622b105a069SSam Leffler 		}
2623b105a069SSam Leffler 	}
2624b105a069SSam Leffler }
2625b105a069SSam Leffler 
2626b105a069SSam Leffler /*
2627b105a069SSam Leffler  * Time out presence of an overlapping bss with non-ERP
2628b105a069SSam Leffler  * stations.  When operating in hostap mode we listen for
2629b105a069SSam Leffler  * beacons from other stations and if we identify a non-ERP
2630b105a069SSam Leffler  * station is present we enable protection.  To identify
2631b105a069SSam Leffler  * when all non-ERP stations are gone we time out this
2632b105a069SSam Leffler  * condition.
2633b105a069SSam Leffler  */
2634b105a069SSam Leffler static void
2635b105a069SSam Leffler ieee80211_erp_timeout(struct ieee80211com *ic)
2636b105a069SSam Leffler {
2637b105a069SSam Leffler 
2638b105a069SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2639b105a069SSam Leffler 
2640b105a069SSam Leffler 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2641b105a069SSam Leffler 	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2642b032f27cSSam Leffler #if 0
2643b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2644b032f27cSSam Leffler 		    "%s", "age out non-ERP sta present on channel");
2645b032f27cSSam Leffler #endif
2646b105a069SSam Leffler 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2647b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0)
2648b105a069SSam Leffler 			disable_protection(ic);
26498a1b9b6aSSam Leffler 	}
26508a1b9b6aSSam Leffler }
26518a1b9b6aSSam Leffler 
26528a1b9b6aSSam Leffler /*
26538a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
26548a1b9b6aSSam Leffler  * when operating as an ap.
26558a1b9b6aSSam Leffler  */
26568a1b9b6aSSam Leffler void
2657b032f27cSSam Leffler ieee80211_node_leave(struct ieee80211_node *ni)
26588a1b9b6aSSam Leffler {
2659b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2660b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
266144acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
26628a1b9b6aSSam Leffler 
2663b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2664b105a069SSam Leffler 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
26658a1b9b6aSSam Leffler 
2666b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2667b032f27cSSam Leffler 		("unexpected operating mode %u", vap->iv_opmode));
26688a1b9b6aSSam Leffler 	/*
26698a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
26708a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
26718a1b9b6aSSam Leffler 	 */
26728a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
26738a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
26748a1b9b6aSSam Leffler 		goto done;
26758a1b9b6aSSam Leffler 	/*
26768a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
26778a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
26788a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
26798a1b9b6aSSam Leffler 	 * associd to locate it's state block.
26808a1b9b6aSSam Leffler 	 */
2681b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_leave != NULL)
2682b032f27cSSam Leffler 		vap->iv_auth->ia_node_leave(ni);
26831b6167d2SSam Leffler 
26841b6167d2SSam Leffler 	IEEE80211_LOCK(ic);
2685b032f27cSSam Leffler 	IEEE80211_AID_CLR(vap, ni->ni_associd);
26868a1b9b6aSSam Leffler 	ni->ni_associd = 0;
2687b032f27cSSam Leffler 	vap->iv_sta_assoc--;
26888a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
26898a1b9b6aSSam Leffler 
26901b6167d2SSam Leffler 	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
26911b6167d2SSam Leffler 		ieee80211_ht_node_leave(ni);
269268e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
269368e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2694b032f27cSSam Leffler 		ieee80211_node_leave_11g(ni);
26951b6167d2SSam Leffler 	IEEE80211_UNLOCK(ic);
26968a1b9b6aSSam Leffler 	/*
26978a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
26988a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
26998a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
27008a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
27018a1b9b6aSSam Leffler 	 */
2702b032f27cSSam Leffler 	ieee80211_sta_leave(ni);
27038a1b9b6aSSam Leffler done:
270444acc00dSSam Leffler 	/*
270544acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
270644acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
270744acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
270844acc00dSSam Leffler 	 * for inactivity.
270944acc00dSSam Leffler 	 */
271044acc00dSSam Leffler 	if (nt != NULL) {
271144acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
271244acc00dSSam Leffler 		node_reclaim(nt, ni);
271344acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
271444acc00dSSam Leffler 	} else
27158a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
27168a1b9b6aSSam Leffler }
27178a1b9b6aSSam Leffler 
2718b032f27cSSam Leffler struct rssiinfo {
2719b032f27cSSam Leffler 	struct ieee80211vap *vap;
2720b032f27cSSam Leffler 	int	rssi_samples;
2721b032f27cSSam Leffler 	uint32_t rssi_total;
2722b032f27cSSam Leffler };
2723b032f27cSSam Leffler 
2724b032f27cSSam Leffler static void
2725b032f27cSSam Leffler get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2726b032f27cSSam Leffler {
2727b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2728b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2729b032f27cSSam Leffler 	int8_t rssi;
2730b032f27cSSam Leffler 
2731b032f27cSSam Leffler 	if (info->vap != vap)
2732b032f27cSSam Leffler 		return;
2733b032f27cSSam Leffler 	/* only associated stations */
2734b032f27cSSam Leffler 	if (ni->ni_associd == 0)
2735b032f27cSSam Leffler 		return;
2736b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
2737b032f27cSSam Leffler 	if (rssi != 0) {
2738b032f27cSSam Leffler 		info->rssi_samples++;
2739b032f27cSSam Leffler 		info->rssi_total += rssi;
2740b032f27cSSam Leffler 	}
2741b032f27cSSam Leffler }
2742b032f27cSSam Leffler 
2743b032f27cSSam Leffler static void
2744b032f27cSSam Leffler get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2745b032f27cSSam Leffler {
2746b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2747b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2748b032f27cSSam Leffler 	int8_t rssi;
2749b032f27cSSam Leffler 
2750b032f27cSSam Leffler 	if (info->vap != vap)
2751b032f27cSSam Leffler 		return;
2752b032f27cSSam Leffler 	/* only neighbors */
2753b032f27cSSam Leffler 	/* XXX check bssid */
2754b032f27cSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2755b032f27cSSam Leffler 		return;
2756b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
2757b032f27cSSam Leffler 	if (rssi != 0) {
2758b032f27cSSam Leffler 		info->rssi_samples++;
2759b032f27cSSam Leffler 		info->rssi_total += rssi;
2760b032f27cSSam Leffler 	}
2761b032f27cSSam Leffler }
2762b032f27cSSam Leffler 
276359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
276459aa14a9SRui Paulo static void
276559aa14a9SRui Paulo get_mesh_rssi(void *arg, struct ieee80211_node *ni)
276659aa14a9SRui Paulo {
276759aa14a9SRui Paulo 	struct rssiinfo *info = arg;
276859aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
276959aa14a9SRui Paulo 	int8_t rssi;
277059aa14a9SRui Paulo 
277159aa14a9SRui Paulo 	if (info->vap != vap)
277259aa14a9SRui Paulo 		return;
277359aa14a9SRui Paulo 	/* only neighbors that peered successfully */
277459aa14a9SRui Paulo 	if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
277559aa14a9SRui Paulo 		return;
277659aa14a9SRui Paulo 	rssi = vap->iv_ic->ic_node_getrssi(ni);
277759aa14a9SRui Paulo 	if (rssi != 0) {
277859aa14a9SRui Paulo 		info->rssi_samples++;
277959aa14a9SRui Paulo 		info->rssi_total += rssi;
278059aa14a9SRui Paulo 	}
278159aa14a9SRui Paulo }
278259aa14a9SRui Paulo #endif /* IEEE80211_SUPPORT_MESH */
278359aa14a9SRui Paulo 
278468e8e04eSSam Leffler int8_t
2785b032f27cSSam Leffler ieee80211_getrssi(struct ieee80211vap *vap)
27868a1b9b6aSSam Leffler {
27878a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
2788b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2789b032f27cSSam Leffler 	struct rssiinfo info;
27908a1b9b6aSSam Leffler 
2791b032f27cSSam Leffler 	info.rssi_total = 0;
2792b032f27cSSam Leffler 	info.rssi_samples = 0;
2793b032f27cSSam Leffler 	info.vap = vap;
2794b032f27cSSam Leffler 	switch (vap->iv_opmode) {
27958a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
27968a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2797b032f27cSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2798b032f27cSSam Leffler 		break;
27998a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2800b032f27cSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
28018a1b9b6aSSam Leffler 		break;
280259aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
280359aa14a9SRui Paulo 	case IEEE80211_M_MBSS:		/* average of all mesh neighbors */
280459aa14a9SRui Paulo 		ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
280559aa14a9SRui Paulo 		break;
280659aa14a9SRui Paulo #endif
28078a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
28088a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
28098a1b9b6aSSam Leffler 	default:
2810b032f27cSSam Leffler 		if (vap->iv_bss != NULL)
2811b032f27cSSam Leffler 			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2812b032f27cSSam Leffler 		info.rssi_samples = 1;
28138a1b9b6aSSam Leffler 		break;
28148a1b9b6aSSam Leffler 	}
2815b032f27cSSam Leffler 	return info.rssi_total / NZ(info.rssi_samples);
28168a1b9b6aSSam Leffler #undef NZ
28178a1b9b6aSSam Leffler }
28188a1b9b6aSSam Leffler 
281968e8e04eSSam Leffler void
2820b032f27cSSam Leffler ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
28218a1b9b6aSSam Leffler {
28228a1b9b6aSSam Leffler 
2823b032f27cSSam Leffler 	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
282468e8e04eSSam Leffler 		return;
2825b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
282668e8e04eSSam Leffler 	/* for non-station mode return avg'd rssi accounting */
2827b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA)
2828b032f27cSSam Leffler 		*rssi = ieee80211_getrssi(vap);
28298a1b9b6aSSam Leffler }
2830