xref: /freebsd/sys/net80211/ieee80211_node.c (revision 864ab1142527bfb046877621491f1840c4fab2f7)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
310ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
30b032f27cSSam Leffler #include "opt_wlan.h"
31b032f27cSSam Leffler 
321a1e1d21SSam Leffler #include <sys/param.h>
331a1e1d21SSam Leffler #include <sys/systm.h>
341a1e1d21SSam Leffler #include <sys/mbuf.h>
351a1e1d21SSam Leffler #include <sys/malloc.h>
361a1e1d21SSam Leffler #include <sys/kernel.h>
371a1e1d21SSam Leffler 
388a1b9b6aSSam Leffler #include <sys/socket.h>
391a1e1d21SSam Leffler 
401a1e1d21SSam Leffler #include <net/if.h>
411a1e1d21SSam Leffler #include <net/if_media.h>
421a1e1d21SSam Leffler #include <net/ethernet.h>
431a1e1d21SSam Leffler 
441a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
45b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
46616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
47616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
48616190d0SSam Leffler #endif
4910ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
5010ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
5110ad9a77SSam Leffler #endif
52b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
5359aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
54b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
551a1e1d21SSam Leffler 
561a1e1d21SSam Leffler #include <net/bpf.h>
571a1e1d21SSam Leffler 
587268fa64SSam Leffler /*
5959aa14a9SRui Paulo  * IEEE80211_NODE_HASHSIZE must be a power of 2.
6059aa14a9SRui Paulo  */
6159aa14a9SRui Paulo CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
6259aa14a9SRui Paulo 
6359aa14a9SRui Paulo /*
647268fa64SSam Leffler  * Association id's are managed with a bit vector.
657268fa64SSam Leffler  */
66b032f27cSSam Leffler #define	IEEE80211_AID_SET(_vap, b) \
67b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
68b032f27cSSam Leffler 		(1 << (IEEE80211_AID(b) % 32)))
69b032f27cSSam Leffler #define	IEEE80211_AID_CLR(_vap, b) \
70b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
71b032f27cSSam Leffler 		~(1 << (IEEE80211_AID(b) % 32)))
72b032f27cSSam Leffler #define	IEEE80211_AID_ISSET(_vap, b) \
73b032f27cSSam Leffler 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
747268fa64SSam Leffler 
75132142c5SDiomidis Spinellis #ifdef IEEE80211_DEBUG_REFCNT
76132142c5SDiomidis Spinellis #define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
77132142c5SDiomidis Spinellis #else
78132142c5SDiomidis Spinellis #define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
79132142c5SDiomidis Spinellis #endif
80132142c5SDiomidis Spinellis 
8168e8e04eSSam Leffler static int ieee80211_sta_join1(struct ieee80211_node *);
8268e8e04eSSam Leffler 
8338c208f8SSam Leffler static struct ieee80211_node *node_alloc(struct ieee80211vap *,
8438c208f8SSam Leffler 	const uint8_t [IEEE80211_ADDR_LEN]);
858a1b9b6aSSam Leffler static void node_cleanup(struct ieee80211_node *);
868a1b9b6aSSam Leffler static void node_free(struct ieee80211_node *);
87b032f27cSSam Leffler static void node_age(struct ieee80211_node *);
8868e8e04eSSam Leffler static int8_t node_getrssi(const struct ieee80211_node *);
8968e8e04eSSam Leffler static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
90b032f27cSSam Leffler static void node_getmimoinfo(const struct ieee80211_node *,
91b032f27cSSam Leffler 	struct ieee80211_mimo_info *);
921a1e1d21SSam Leffler 
938a1b9b6aSSam Leffler static void _ieee80211_free_node(struct ieee80211_node *);
948a1b9b6aSSam Leffler 
958a1b9b6aSSam Leffler static void ieee80211_node_table_init(struct ieee80211com *ic,
96c1225b52SSam Leffler 	struct ieee80211_node_table *nt, const char *name,
9768e8e04eSSam Leffler 	int inact, int keymaxix);
98b032f27cSSam Leffler static void ieee80211_node_table_reset(struct ieee80211_node_table *,
99b032f27cSSam Leffler 	struct ieee80211vap *);
1008a1b9b6aSSam Leffler static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
101b105a069SSam Leffler static void ieee80211_erp_timeout(struct ieee80211com *);
1021a1e1d21SSam Leffler 
10332346d60SSam Leffler MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
104b032f27cSSam Leffler MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
10537c150c4SSam Leffler 
1061a1e1d21SSam Leffler void
1078a1b9b6aSSam Leffler ieee80211_node_attach(struct ieee80211com *ic)
1081a1e1d21SSam Leffler {
1095b16c28cSSam Leffler 	/* XXX really want maxlen enforced per-sta */
1105b16c28cSSam Leffler 	ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
1115b16c28cSSam Leffler 	    "802.11 staging q");
112b032f27cSSam Leffler 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
113b032f27cSSam Leffler 		IEEE80211_INACT_INIT, ic->ic_max_keyix);
114b032f27cSSam Leffler 	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
115b032f27cSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
116b032f27cSSam Leffler 		ieee80211_node_timeout, ic);
1171a1e1d21SSam Leffler 
1188a1b9b6aSSam Leffler 	ic->ic_node_alloc = node_alloc;
1198a1b9b6aSSam Leffler 	ic->ic_node_free = node_free;
1208a1b9b6aSSam Leffler 	ic->ic_node_cleanup = node_cleanup;
121b032f27cSSam Leffler 	ic->ic_node_age = node_age;
122b032f27cSSam Leffler 	ic->ic_node_drain = node_age;		/* NB: same as age */
1238a1b9b6aSSam Leffler 	ic->ic_node_getrssi = node_getrssi;
12468e8e04eSSam Leffler 	ic->ic_node_getsignal = node_getsignal;
125b032f27cSSam Leffler 	ic->ic_node_getmimoinfo = node_getmimoinfo;
1268a1b9b6aSSam Leffler 
127b032f27cSSam Leffler 	/*
128b032f27cSSam Leffler 	 * Set flags to be propagated to all vap's;
129b032f27cSSam Leffler 	 * these define default behaviour/configuration.
130b032f27cSSam Leffler 	 */
131c066143cSSam Leffler 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
132c1225b52SSam Leffler }
133c1225b52SSam Leffler 
134c1225b52SSam Leffler void
1358a1b9b6aSSam Leffler ieee80211_node_detach(struct ieee80211com *ic)
1361a1e1d21SSam Leffler {
1371a1e1d21SSam Leffler 
138b032f27cSSam Leffler 	callout_drain(&ic->ic_inact);
139acc4f7f5SSam Leffler 	ieee80211_node_table_cleanup(&ic->ic_sta);
1405b16c28cSSam Leffler 	ieee80211_ageq_cleanup(&ic->ic_stageq);
141b032f27cSSam Leffler }
142b032f27cSSam Leffler 
143b032f27cSSam Leffler void
144b032f27cSSam Leffler ieee80211_node_vattach(struct ieee80211vap *vap)
145b032f27cSSam Leffler {
146b032f27cSSam Leffler 	/* NB: driver can override */
147b032f27cSSam Leffler 	vap->iv_max_aid = IEEE80211_AID_DEF;
148b032f27cSSam Leffler 
149b032f27cSSam Leffler 	/* default station inactivity timer setings */
150b032f27cSSam Leffler 	vap->iv_inact_init = IEEE80211_INACT_INIT;
151b032f27cSSam Leffler 	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
152b032f27cSSam Leffler 	vap->iv_inact_run = IEEE80211_INACT_RUN;
153b032f27cSSam Leffler 	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
154be1054edSSam Leffler 
155be1054edSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
156be1054edSSam Leffler 	    "%s: init %u auth %u run %u probe %u\n", __func__,
157be1054edSSam Leffler 	    vap->iv_inact_init, vap->iv_inact_auth,
158be1054edSSam Leffler 	    vap->iv_inact_run, vap->iv_inact_probe);
159b032f27cSSam Leffler }
160b032f27cSSam Leffler 
161b032f27cSSam Leffler void
162b032f27cSSam Leffler ieee80211_node_latevattach(struct ieee80211vap *vap)
163b032f27cSSam Leffler {
164b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
165b032f27cSSam Leffler 		/* XXX should we allow max aid to be zero? */
166b032f27cSSam Leffler 		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
167b032f27cSSam Leffler 			vap->iv_max_aid = IEEE80211_AID_MIN;
168b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
169b032f27cSSam Leffler 			    "WARNING: max aid too small, changed to %d\n",
170b032f27cSSam Leffler 			    vap->iv_max_aid);
171b032f27cSSam Leffler 		}
172e2126decSSam Leffler 		vap->iv_aid_bitmap = (uint32_t *) malloc(
173c5abbba3SDag-Erling Smørgrav 			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
174b032f27cSSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
175b032f27cSSam Leffler 		if (vap->iv_aid_bitmap == NULL) {
176b032f27cSSam Leffler 			/* XXX no way to recover */
177b032f27cSSam Leffler 			printf("%s: no memory for AID bitmap, max aid %d!\n",
178b032f27cSSam Leffler 			    __func__, vap->iv_max_aid);
179b032f27cSSam Leffler 			vap->iv_max_aid = 0;
180b032f27cSSam Leffler 		}
181b032f27cSSam Leffler 	}
182b032f27cSSam Leffler 
183b032f27cSSam Leffler 	ieee80211_reset_bss(vap);
184b032f27cSSam Leffler 
185b032f27cSSam Leffler 	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
186b032f27cSSam Leffler }
187b032f27cSSam Leffler 
188b032f27cSSam Leffler void
189b032f27cSSam Leffler ieee80211_node_vdetach(struct ieee80211vap *vap)
190b032f27cSSam Leffler {
191b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
192b032f27cSSam Leffler 
193b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
194b032f27cSSam Leffler 	if (vap->iv_bss != NULL) {
195b032f27cSSam Leffler 		ieee80211_free_node(vap->iv_bss);
196b032f27cSSam Leffler 		vap->iv_bss = NULL;
197b032f27cSSam Leffler 	}
198b032f27cSSam Leffler 	if (vap->iv_aid_bitmap != NULL) {
199e2126decSSam Leffler 		free(vap->iv_aid_bitmap, M_80211_NODE);
200b032f27cSSam Leffler 		vap->iv_aid_bitmap = NULL;
2018a1b9b6aSSam Leffler 	}
2028a1b9b6aSSam Leffler }
2038a1b9b6aSSam Leffler 
2048a1b9b6aSSam Leffler /*
2058a1b9b6aSSam Leffler  * Port authorize/unauthorize interfaces for use by an authenticator.
2068a1b9b6aSSam Leffler  */
2078a1b9b6aSSam Leffler 
2088a1b9b6aSSam Leffler void
209e4918ecdSSam Leffler ieee80211_node_authorize(struct ieee80211_node *ni)
2108a1b9b6aSSam Leffler {
211be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
212be1054edSSam Leffler 
2138a1b9b6aSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AUTH;
214be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_run;
215c066143cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
216be1054edSSam Leffler 
217be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
218be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
2198a1b9b6aSSam Leffler }
2208a1b9b6aSSam Leffler 
2218a1b9b6aSSam Leffler void
222e4918ecdSSam Leffler ieee80211_node_unauthorize(struct ieee80211_node *ni)
2238a1b9b6aSSam Leffler {
224be1054edSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
225be1054edSSam Leffler 
2268a1b9b6aSSam Leffler 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
227be1054edSSam Leffler 	ni->ni_inact_reload = vap->iv_inact_auth;
228c066143cSSam Leffler 	if (ni->ni_inact > ni->ni_inact_reload)
229c066143cSSam Leffler 		ni->ni_inact = ni->ni_inact_reload;
230be1054edSSam Leffler 
231be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
232be1054edSSam Leffler 	    "%s: inact_reload %u inact %u", __func__,
233be1054edSSam Leffler 	    ni->ni_inact_reload, ni->ni_inact);
2348a1b9b6aSSam Leffler }
2358a1b9b6aSSam Leffler 
2368a1b9b6aSSam Leffler /*
23701a03542SSam Leffler  * Fix tx parameters for a node according to ``association state''.
23801a03542SSam Leffler  */
239d77148fbSSam Leffler void
240d77148fbSSam Leffler ieee80211_node_setuptxparms(struct ieee80211_node *ni)
24101a03542SSam Leffler {
24201a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
243f76cde95SSam Leffler 	enum ieee80211_phymode mode;
24401a03542SSam Leffler 
24501a03542SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT) {
24601a03542SSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
247f76cde95SSam Leffler 			mode = IEEE80211_MODE_11NA;
24801a03542SSam Leffler 		else
249f76cde95SSam Leffler 			mode = IEEE80211_MODE_11NG;
25001a03542SSam Leffler 	} else {				/* legacy rate handling */
251f76cde95SSam Leffler 		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
252f76cde95SSam Leffler 			mode = IEEE80211_MODE_STURBO_A;
2536a76ae21SSam Leffler 		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
2546a76ae21SSam Leffler 			mode = IEEE80211_MODE_HALF;
2556a76ae21SSam Leffler 		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
2566a76ae21SSam Leffler 			mode = IEEE80211_MODE_QUARTER;
257c5262b82SSam Leffler 		/* NB: 108A should be handled as 11a */
258f76cde95SSam Leffler 		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
259f76cde95SSam Leffler 			mode = IEEE80211_MODE_11A;
260c5262b82SSam Leffler 		else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
261c5262b82SSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_ERP))
262f76cde95SSam Leffler 			mode = IEEE80211_MODE_11G;
26301a03542SSam Leffler 		else
264f76cde95SSam Leffler 			mode = IEEE80211_MODE_11B;
26501a03542SSam Leffler 	}
266f76cde95SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
26701a03542SSam Leffler }
26801a03542SSam Leffler 
26901a03542SSam Leffler /*
2708a1b9b6aSSam Leffler  * Set/change the channel.  The rate set is also updated as
2718a1b9b6aSSam Leffler  * to insure a consistent view by drivers.
272b032f27cSSam Leffler  * XXX should be private but hostap needs it to deal with CSA
2738a1b9b6aSSam Leffler  */
274b032f27cSSam Leffler void
275b032f27cSSam Leffler ieee80211_node_set_chan(struct ieee80211_node *ni,
276b032f27cSSam Leffler 	struct ieee80211_channel *chan)
2778a1b9b6aSSam Leffler {
278b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
27901a03542SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
28001a03542SSam Leffler 	enum ieee80211_phymode mode;
28168e8e04eSSam Leffler 
282b032f27cSSam Leffler 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
283b032f27cSSam Leffler 
2848a1b9b6aSSam Leffler 	ni->ni_chan = chan;
28501a03542SSam Leffler 	mode = ieee80211_chan2mode(chan);
28668e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT(chan)) {
28768e8e04eSSam Leffler 		/*
288597029bfSBernhard Schmidt 		 * We must install the legacy rate est in ni_rates and the
28968e8e04eSSam Leffler 		 * HT rate set in ni_htrates.
29068e8e04eSSam Leffler 		 */
29168e8e04eSSam Leffler 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
29201a03542SSam Leffler 		/*
29301a03542SSam Leffler 		 * Setup bss tx parameters based on operating mode.  We
29401a03542SSam Leffler 		 * use legacy rates when operating in a mixed HT+non-HT bss
29501a03542SSam Leffler 		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
29601a03542SSam Leffler 		 */
29701a03542SSam Leffler 		if (mode == IEEE80211_MODE_11NA &&
2982bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
29901a03542SSam Leffler 			mode = IEEE80211_MODE_11A;
30001a03542SSam Leffler 		else if (mode == IEEE80211_MODE_11NG &&
3012bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
30201a03542SSam Leffler 			mode = IEEE80211_MODE_11G;
30301a03542SSam Leffler 		if (mode == IEEE80211_MODE_11G &&
30401a03542SSam Leffler 		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
30501a03542SSam Leffler 			mode = IEEE80211_MODE_11B;
30668e8e04eSSam Leffler 	}
30701a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[mode];
30841b3c790SSam Leffler 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
3091a1e1d21SSam Leffler }
3101a1e1d21SSam Leffler 
311f9cd9174SSam Leffler static __inline void
312f9cd9174SSam Leffler copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
313f9cd9174SSam Leffler {
314f9cd9174SSam Leffler 	/* propagate useful state */
315f9cd9174SSam Leffler 	nbss->ni_authmode = obss->ni_authmode;
316f9cd9174SSam Leffler 	nbss->ni_txpower = obss->ni_txpower;
317f9cd9174SSam Leffler 	nbss->ni_vlan = obss->ni_vlan;
318f9cd9174SSam Leffler 	/* XXX statistics? */
319b032f27cSSam Leffler 	/* XXX legacy WDS bssid? */
320f9cd9174SSam Leffler }
321f9cd9174SSam Leffler 
3221a1e1d21SSam Leffler void
323b032f27cSSam Leffler ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
3241a1e1d21SSam Leffler {
325b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3261a1e1d21SSam Leffler 	struct ieee80211_node *ni;
3278a1b9b6aSSam Leffler 
328b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
32959aa14a9SRui Paulo 		"%s: creating %s on channel %u\n", __func__,
33059aa14a9SRui Paulo 		ieee80211_opmode_name[vap->iv_opmode],
331b032f27cSSam Leffler 		ieee80211_chan2ieee(ic, chan));
3328a1b9b6aSSam Leffler 
333b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
334acc4f7f5SSam Leffler 	if (ni == NULL) {
335acc4f7f5SSam Leffler 		/* XXX recovery? */
3368a1b9b6aSSam Leffler 		return;
3378a1b9b6aSSam Leffler 	}
338b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
339b032f27cSSam Leffler 	ni->ni_esslen = vap->iv_des_ssid[0].len;
340b032f27cSSam Leffler 	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
341b032f27cSSam Leffler 	if (vap->iv_bss != NULL)
342b032f27cSSam Leffler 		copy_bss(ni, vap->iv_bss);
343d365f9c7SSam Leffler 	ni->ni_intval = ic->ic_bintval;
344b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
3451a1e1d21SSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3461a1e1d21SSam Leffler 	if (ic->ic_phytype == IEEE80211_T_FH) {
3471a1e1d21SSam Leffler 		ni->ni_fhdwell = 200;	/* XXX */
3481a1e1d21SSam Leffler 		ni->ni_fhindex = 1;
3491a1e1d21SSam Leffler 	}
350b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
351b032f27cSSam Leffler 		vap->iv_flags |= IEEE80211_F_SIBSS;
3528a1b9b6aSSam Leffler 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
353b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
354b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
355fe49f061SSam Leffler 		else {
356fe49f061SSam Leffler 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
357fe49f061SSam Leffler 			/* clear group bit, add local bit */
358fe49f061SSam Leffler 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
359fe49f061SSam Leffler 		}
360b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
361b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
362b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
36350d8b493SSam Leffler 		else
36410ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
36510ad9a77SSam Leffler 		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
36610ad9a77SSam Leffler #endif
36750d8b493SSam Leffler 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
36859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
36959aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
37059aa14a9SRui Paulo 		ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
37159aa14a9SRui Paulo 		memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
37259aa14a9SRui Paulo #endif
3738a1b9b6aSSam Leffler 	}
3748a1b9b6aSSam Leffler 	/*
3758a1b9b6aSSam Leffler 	 * Fix the channel and related attributes.
3768a1b9b6aSSam Leffler 	 */
377b032f27cSSam Leffler 	/* clear DFS CAC state on previous channel */
378b032f27cSSam Leffler 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
379b032f27cSSam Leffler 	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
380b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
381b032f27cSSam Leffler 		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
38268e8e04eSSam Leffler 	ic->ic_bsschan = chan;
383b032f27cSSam Leffler 	ieee80211_node_set_chan(ni, chan);
38468e8e04eSSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(chan);
3858a1b9b6aSSam Leffler 	/*
386b032f27cSSam Leffler 	 * Do mode-specific setup.
3878a1b9b6aSSam Leffler 	 */
38868e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_FULL(chan)) {
38968e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
39068e8e04eSSam Leffler 			/*
391b032f27cSSam Leffler 			 * Use a mixed 11b/11g basic rate set.
39268e8e04eSSam Leffler 			 */
393b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
39468e8e04eSSam Leffler 			    IEEE80211_MODE_11G);
395b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_PUREG) {
396b032f27cSSam Leffler 				/*
397b032f27cSSam Leffler 				 * Also mark OFDM rates basic so 11b
398b032f27cSSam Leffler 				 * stations do not join (WiFi compliance).
399b032f27cSSam Leffler 				 */
400b032f27cSSam Leffler 				ieee80211_addbasicrates(&ni->ni_rates,
401b032f27cSSam Leffler 				    IEEE80211_MODE_11A);
402b032f27cSSam Leffler 			}
40368e8e04eSSam Leffler 		} else if (IEEE80211_IS_CHAN_B(chan)) {
40468e8e04eSSam Leffler 			/*
40568e8e04eSSam Leffler 			 * Force pure 11b rate set.
40668e8e04eSSam Leffler 			 */
407b032f27cSSam Leffler 			ieee80211_setbasicrates(&ni->ni_rates,
40868e8e04eSSam Leffler 				IEEE80211_MODE_11B);
40968e8e04eSSam Leffler 		}
4101a1e1d21SSam Leffler 	}
4111a1e1d21SSam Leffler 
41268e8e04eSSam Leffler 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
41368e8e04eSSam Leffler }
41468e8e04eSSam Leffler 
41568e8e04eSSam Leffler /*
41668e8e04eSSam Leffler  * Reset bss state on transition to the INIT state.
41768e8e04eSSam Leffler  * Clear any stations from the table (they have been
41868e8e04eSSam Leffler  * deauth'd) and reset the bss node (clears key, rate
41968e8e04eSSam Leffler  * etc. state).
42068e8e04eSSam Leffler  */
4218a1b9b6aSSam Leffler void
422b032f27cSSam Leffler ieee80211_reset_bss(struct ieee80211vap *vap)
423b4c5a90fSSam Leffler {
424b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4258a1b9b6aSSam Leffler 	struct ieee80211_node *ni, *obss;
4268a1b9b6aSSam Leffler 
427b032f27cSSam Leffler 	ieee80211_node_table_reset(&ic->ic_sta, vap);
428b032f27cSSam Leffler 	/* XXX multi-bss: wrong */
42968e8e04eSSam Leffler 	ieee80211_reset_erp(ic);
430acc4f7f5SSam Leffler 
431b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
43248e1bda0SRui Paulo 	KASSERT(ni != NULL, ("unable to setup initial BSS node"));
433b032f27cSSam Leffler 	obss = vap->iv_bss;
434b032f27cSSam Leffler 	vap->iv_bss = ieee80211_ref_node(ni);
435f9cd9174SSam Leffler 	if (obss != NULL) {
436f9cd9174SSam Leffler 		copy_bss(ni, obss);
437d365f9c7SSam Leffler 		ni->ni_intval = ic->ic_bintval;
4388a1b9b6aSSam Leffler 		ieee80211_free_node(obss);
439b032f27cSSam Leffler 	} else
440b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
441f9cd9174SSam Leffler }
4428a1b9b6aSSam Leffler 
4438a1b9b6aSSam Leffler static int
44468e8e04eSSam Leffler match_ssid(const struct ieee80211_node *ni,
44568e8e04eSSam Leffler 	int nssid, const struct ieee80211_scan_ssid ssids[])
4468a1b9b6aSSam Leffler {
44768e8e04eSSam Leffler 	int i;
44868e8e04eSSam Leffler 
44968e8e04eSSam Leffler 	for (i = 0; i < nssid; i++) {
45068e8e04eSSam Leffler 		if (ni->ni_esslen == ssids[i].len &&
45168e8e04eSSam Leffler 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
45268e8e04eSSam Leffler 			return 1;
45368e8e04eSSam Leffler 	}
45468e8e04eSSam Leffler 	return 0;
45568e8e04eSSam Leffler }
45668e8e04eSSam Leffler 
45768e8e04eSSam Leffler /*
45868e8e04eSSam Leffler  * Test a node for suitability/compatibility.
45968e8e04eSSam Leffler  */
46068e8e04eSSam Leffler static int
461b032f27cSSam Leffler check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
46268e8e04eSSam Leffler {
463b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
46468e8e04eSSam Leffler         uint8_t rate;
46568e8e04eSSam Leffler 
46668e8e04eSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
46768e8e04eSSam Leffler 		return 0;
468b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
46968e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
47068e8e04eSSam Leffler 			return 0;
47168e8e04eSSam Leffler 	} else {
47268e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
47368e8e04eSSam Leffler 			return 0;
47468e8e04eSSam Leffler 	}
475b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
47668e8e04eSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
47768e8e04eSSam Leffler 			return 0;
47868e8e04eSSam Leffler 	} else {
47968e8e04eSSam Leffler 		/* XXX does this mean privacy is supported or required? */
48068e8e04eSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
48168e8e04eSSam Leffler 			return 0;
48268e8e04eSSam Leffler 	}
48368e8e04eSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
48468e8e04eSSam Leffler 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
48568e8e04eSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
48668e8e04eSSam Leffler 		return 0;
487b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
488b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
48968e8e04eSSam Leffler 		return 0;
490b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
491b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
49268e8e04eSSam Leffler 		return 0;
49368e8e04eSSam Leffler 	return 1;
49468e8e04eSSam Leffler }
49568e8e04eSSam Leffler 
49668e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
49768e8e04eSSam Leffler /*
49868e8e04eSSam Leffler  * Display node suitability/compatibility.
49968e8e04eSSam Leffler  */
50068e8e04eSSam Leffler static void
501b032f27cSSam Leffler check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
50268e8e04eSSam Leffler {
503b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
50468e8e04eSSam Leffler         uint8_t rate;
505b4c5a90fSSam Leffler         int fail;
506b4c5a90fSSam Leffler 
507b4c5a90fSSam Leffler 	fail = 0;
508b4c5a90fSSam Leffler 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
509b4c5a90fSSam Leffler 		fail |= 0x01;
510b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
511b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
512b4c5a90fSSam Leffler 			fail |= 0x02;
513b4c5a90fSSam Leffler 	} else {
514b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
515b4c5a90fSSam Leffler 			fail |= 0x02;
516b4c5a90fSSam Leffler 	}
517b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
518b4c5a90fSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
519b4c5a90fSSam Leffler 			fail |= 0x04;
520b4c5a90fSSam Leffler 	} else {
521b4c5a90fSSam Leffler 		/* XXX does this mean privacy is supported or required? */
522b4c5a90fSSam Leffler 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
523b4c5a90fSSam Leffler 			fail |= 0x04;
524b4c5a90fSSam Leffler 	}
52570e28b9aSSam Leffler 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
52679edaebfSSam Leffler 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
527b4c5a90fSSam Leffler 	if (rate & IEEE80211_RATE_BASIC)
528b4c5a90fSSam Leffler 		fail |= 0x08;
529b032f27cSSam Leffler 	if (vap->iv_des_nssid != 0 &&
530b032f27cSSam Leffler 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
531b4c5a90fSSam Leffler 		fail |= 0x10;
532b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
533b032f27cSSam Leffler 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
534b4c5a90fSSam Leffler 		fail |= 0x20;
53568e8e04eSSam Leffler 
53668e8e04eSSam Leffler 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
53768e8e04eSSam Leffler 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
53868e8e04eSSam Leffler 	printf(" %3d%c",
53968e8e04eSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
540b4c5a90fSSam Leffler 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
541b4c5a90fSSam Leffler 	    fail & 0x08 ? '!' : ' ');
542b4c5a90fSSam Leffler 	printf(" %4s%c",
543b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
544b4c5a90fSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
545b4c5a90fSSam Leffler 	    "????",
546b4c5a90fSSam Leffler 	    fail & 0x02 ? '!' : ' ');
547b4c5a90fSSam Leffler 	printf(" %3s%c ",
54868e8e04eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
549b4c5a90fSSam Leffler 	    fail & 0x04 ? '!' : ' ');
550b4c5a90fSSam Leffler 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
551b4c5a90fSSam Leffler 	printf("%s\n", fail & 0x10 ? "!" : "");
552b4c5a90fSSam Leffler }
55368e8e04eSSam Leffler #endif /* IEEE80211_DEBUG */
5548a1b9b6aSSam Leffler 
555750d6d0cSSam Leffler /*
5568a1b9b6aSSam Leffler  * Handle 802.11 ad hoc network merge.  The
5578a1b9b6aSSam Leffler  * convention, set by the Wireless Ethernet Compatibility Alliance
5588a1b9b6aSSam Leffler  * (WECA), is that an 802.11 station will change its BSSID to match
5598a1b9b6aSSam Leffler  * the "oldest" 802.11 ad hoc network, on the same channel, that
5608a1b9b6aSSam Leffler  * has the station's desired SSID.  The "oldest" 802.11 network
5618a1b9b6aSSam Leffler  * sends beacons with the greatest TSF timestamp.
5628a1b9b6aSSam Leffler  *
5638a1b9b6aSSam Leffler  * The caller is assumed to validate TSF's before attempting a merge.
5648a1b9b6aSSam Leffler  *
5658a1b9b6aSSam Leffler  * Return !0 if the BSSID changed, 0 otherwise.
566750d6d0cSSam Leffler  */
5678a1b9b6aSSam Leffler int
568641b4d0bSSam Leffler ieee80211_ibss_merge(struct ieee80211_node *ni)
5698a1b9b6aSSam Leffler {
570b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
571b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
572641b4d0bSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
573b032f27cSSam Leffler #endif
5748a1b9b6aSSam Leffler 
575b032f27cSSam Leffler 	if (ni == vap->iv_bss ||
576b032f27cSSam Leffler 	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
5778a1b9b6aSSam Leffler 		/* unchanged, nothing to do */
5788a1b9b6aSSam Leffler 		return 0;
5798a1b9b6aSSam Leffler 	}
580b032f27cSSam Leffler 	if (!check_bss(vap, ni)) {
58168e8e04eSSam Leffler 		/* capabilities mismatch */
582b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
5838a1b9b6aSSam Leffler 		    "%s: merge failed, capabilities mismatch\n", __func__);
58468e8e04eSSam Leffler #ifdef IEEE80211_DEBUG
585b032f27cSSam Leffler 		if (ieee80211_msg_assoc(vap))
586b032f27cSSam Leffler 			check_bss_debug(vap, ni);
58768e8e04eSSam Leffler #endif
588b032f27cSSam Leffler 		vap->iv_stats.is_ibss_capmismatch++;
5898a1b9b6aSSam Leffler 		return 0;
5908a1b9b6aSSam Leffler 	}
591b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
5928a1b9b6aSSam Leffler 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
5938a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
5948a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
5958a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
5968a1b9b6aSSam Leffler 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
5978a1b9b6aSSam Leffler 	);
59868e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
5998a1b9b6aSSam Leffler }
6008a1b9b6aSSam Leffler 
6018a1b9b6aSSam Leffler /*
602b032f27cSSam Leffler  * Calculate HT channel promotion flags for all vaps.
603b032f27cSSam Leffler  * This assumes ni_chan have been setup for each vap.
604b032f27cSSam Leffler  */
605b032f27cSSam Leffler static int
606b032f27cSSam Leffler gethtadjustflags(struct ieee80211com *ic)
607b032f27cSSam Leffler {
608b032f27cSSam Leffler 	struct ieee80211vap *vap;
609b032f27cSSam Leffler 	int flags;
610b032f27cSSam Leffler 
611b032f27cSSam Leffler 	flags = 0;
612b032f27cSSam Leffler 	/* XXX locking */
613b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
614b032f27cSSam Leffler 		if (vap->iv_state < IEEE80211_S_RUN)
615b032f27cSSam Leffler 			continue;
616b032f27cSSam Leffler 		switch (vap->iv_opmode) {
617b032f27cSSam Leffler 		case IEEE80211_M_WDS:
618b032f27cSSam Leffler 		case IEEE80211_M_STA:
619b032f27cSSam Leffler 		case IEEE80211_M_AHDEMO:
620b032f27cSSam Leffler 		case IEEE80211_M_HOSTAP:
621b032f27cSSam Leffler 		case IEEE80211_M_IBSS:
62259aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
623b032f27cSSam Leffler 			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
624b032f27cSSam Leffler 			break;
625b032f27cSSam Leffler 		default:
626b032f27cSSam Leffler 			break;
627b032f27cSSam Leffler 		}
628b032f27cSSam Leffler 	}
629b032f27cSSam Leffler 	return flags;
630b032f27cSSam Leffler }
631b032f27cSSam Leffler 
632b032f27cSSam Leffler /*
633b032f27cSSam Leffler  * Check if the current channel needs to change based on whether
6345d44f8c0SSam Leffler  * any vap's are using HT20/HT40.  This is used to sync the state
6355d44f8c0SSam Leffler  * of ic_curchan after a channel width change on a running vap.
6361b6167d2SSam Leffler  */
6371b6167d2SSam Leffler void
638b032f27cSSam Leffler ieee80211_sync_curchan(struct ieee80211com *ic)
6391b6167d2SSam Leffler {
640b032f27cSSam Leffler 	struct ieee80211_channel *c;
641b032f27cSSam Leffler 
642b032f27cSSam Leffler 	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
643b032f27cSSam Leffler 	if (c != ic->ic_curchan) {
644b032f27cSSam Leffler 		ic->ic_curchan = c;
645b032f27cSSam Leffler 		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
64626d39e2cSSam Leffler 		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
6475efea30fSAndrew Thompson 		IEEE80211_UNLOCK(ic);
648b032f27cSSam Leffler 		ic->ic_set_channel(ic);
6495463c4a4SSam Leffler 		ieee80211_radiotap_chan_change(ic);
6505efea30fSAndrew Thompson 		IEEE80211_LOCK(ic);
651b032f27cSSam Leffler 	}
652b032f27cSSam Leffler }
653b032f27cSSam Leffler 
654b032f27cSSam Leffler /*
6555efea30fSAndrew Thompson  * Setup the current channel.  The request channel may be
656b032f27cSSam Leffler  * promoted if other vap's are operating with HT20/HT40.
657b032f27cSSam Leffler  */
658b032f27cSSam Leffler void
6595efea30fSAndrew Thompson ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
660b032f27cSSam Leffler {
661b032f27cSSam Leffler 	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
662b032f27cSSam Leffler 		int flags = gethtadjustflags(ic);
663b032f27cSSam Leffler 		/*
664b032f27cSSam Leffler 		 * Check for channel promotion required to support the
665b032f27cSSam Leffler 		 * set of running vap's.  This assumes we are called
666b032f27cSSam Leffler 		 * after ni_chan is setup for each vap.
667b032f27cSSam Leffler 		 */
6682bfc8a91SSam Leffler 		/* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
669b032f27cSSam Leffler 		if (flags > ieee80211_htchanflags(c))
670b032f27cSSam Leffler 			c = ieee80211_ht_adjust_channel(ic, c, flags);
671b032f27cSSam Leffler 	}
672b032f27cSSam Leffler 	ic->ic_bsschan = ic->ic_curchan = c;
6731b6167d2SSam Leffler 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
67426d39e2cSSam Leffler 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
6755efea30fSAndrew Thompson }
6765efea30fSAndrew Thompson 
6775efea30fSAndrew Thompson /*
6785efea30fSAndrew Thompson  * Change the current channel.  The channel change is guaranteed to have
6795efea30fSAndrew Thompson  * happened before the next state change.
6805efea30fSAndrew Thompson  */
6815efea30fSAndrew Thompson void
6825efea30fSAndrew Thompson ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
6835efea30fSAndrew Thompson {
6845efea30fSAndrew Thompson 	ieee80211_setupcurchan(ic, c);
6855efea30fSAndrew Thompson 	ieee80211_runtask(ic, &ic->ic_chan_task);
6861b6167d2SSam Leffler }
6871b6167d2SSam Leffler 
6881b6167d2SSam Leffler /*
6898a1b9b6aSSam Leffler  * Join the specified IBSS/BSS network.  The node is assumed to
6908a1b9b6aSSam Leffler  * be passed in with a held reference.
6918a1b9b6aSSam Leffler  */
69268e8e04eSSam Leffler static int
69368e8e04eSSam Leffler ieee80211_sta_join1(struct ieee80211_node *selbs)
6948a1b9b6aSSam Leffler {
695b032f27cSSam Leffler 	struct ieee80211vap *vap = selbs->ni_vap;
69668e8e04eSSam Leffler 	struct ieee80211com *ic = selbs->ni_ic;
6978a1b9b6aSSam Leffler 	struct ieee80211_node *obss;
69868e8e04eSSam Leffler 	int canreassoc;
6998a1b9b6aSSam Leffler 
7008a1b9b6aSSam Leffler 	/*
7018a1b9b6aSSam Leffler 	 * Committed to selbs, setup state.
7028a1b9b6aSSam Leffler 	 */
703b032f27cSSam Leffler 	obss = vap->iv_bss;
70468e8e04eSSam Leffler 	/*
70568e8e04eSSam Leffler 	 * Check if old+new node have the same address in which
70668e8e04eSSam Leffler 	 * case we can reassociate when operating in sta mode.
70768e8e04eSSam Leffler 	 */
70868e8e04eSSam Leffler 	canreassoc = (obss != NULL &&
709b032f27cSSam Leffler 		vap->iv_state == IEEE80211_S_RUN &&
71068e8e04eSSam Leffler 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
711b032f27cSSam Leffler 	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
7121fd2349dSSam Leffler 	if (obss != NULL) {
7131fd2349dSSam Leffler 		copy_bss(selbs, obss);
71447a7b0faSSam Leffler 		ieee80211_node_decref(obss);	/* iv_bss reference */
71547a7b0faSSam Leffler 		ieee80211_free_node(obss);	/* station table reference */
716b032f27cSSam Leffler 		obss = NULL;		/* NB: guard against later use */
7171fd2349dSSam Leffler 	}
71879edaebfSSam Leffler 
71979edaebfSSam Leffler 	/*
72079edaebfSSam Leffler 	 * Delete unusable rates; we've already checked
72179edaebfSSam Leffler 	 * that the negotiated rate set is acceptable.
72279edaebfSSam Leffler 	 */
723b032f27cSSam Leffler 	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
72470e28b9aSSam Leffler 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
72579edaebfSSam Leffler 
726b032f27cSSam Leffler 	ieee80211_setcurchan(ic, selbs->ni_chan);
7278a1b9b6aSSam Leffler 	/*
7288a1b9b6aSSam Leffler 	 * Set the erp state (mostly the slot time) to deal with
7298a1b9b6aSSam Leffler 	 * the auto-select case; this should be redundant if the
7308a1b9b6aSSam Leffler 	 * mode is locked.
7318a1b9b6aSSam Leffler 	 */
7328a1b9b6aSSam Leffler 	ieee80211_reset_erp(ic);
733b032f27cSSam Leffler 	ieee80211_wme_initparams(vap);
734acc4f7f5SSam Leffler 
735b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
73668e8e04eSSam Leffler 		if (canreassoc) {
73768e8e04eSSam Leffler 			/* Reassociate */
738b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
73968e8e04eSSam Leffler 		} else {
74068e8e04eSSam Leffler 			/*
74168e8e04eSSam Leffler 			 * Act as if we received a DEAUTH frame in case we
74268e8e04eSSam Leffler 			 * are invoked from the RUN state.  This will cause
74368e8e04eSSam Leffler 			 * us to try to re-authenticate if we are operating
74468e8e04eSSam Leffler 			 * as a station.
74568e8e04eSSam Leffler 			 */
746b032f27cSSam Leffler 			ieee80211_new_state(vap, IEEE80211_S_AUTH,
74768e8e04eSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH);
74868e8e04eSSam Leffler 		}
74968e8e04eSSam Leffler 	} else
750b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
7518a1b9b6aSSam Leffler 	return 1;
7528a1b9b6aSSam Leffler }
7538a1b9b6aSSam Leffler 
75468e8e04eSSam Leffler int
75510959256SSam Leffler ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
75668e8e04eSSam Leffler 	const struct ieee80211_scan_entry *se)
75768e8e04eSSam Leffler {
758b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
75968e8e04eSSam Leffler 	struct ieee80211_node *ni;
76068e8e04eSSam Leffler 
761b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
76268e8e04eSSam Leffler 	if (ni == NULL) {
76368e8e04eSSam Leffler 		/* XXX msg */
76468e8e04eSSam Leffler 		return 0;
76568e8e04eSSam Leffler 	}
76668e8e04eSSam Leffler 	/*
76768e8e04eSSam Leffler 	 * Expand scan state into node's format.
76868e8e04eSSam Leffler 	 * XXX may not need all this stuff
76968e8e04eSSam Leffler 	 */
77068e8e04eSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
77168e8e04eSSam Leffler 	ni->ni_esslen = se->se_ssid[1];
77268e8e04eSSam Leffler 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
77368e8e04eSSam Leffler 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
77468e8e04eSSam Leffler 	ni->ni_intval = se->se_intval;
77568e8e04eSSam Leffler 	ni->ni_capinfo = se->se_capinfo;
77610959256SSam Leffler 	ni->ni_chan = chan;
77768e8e04eSSam Leffler 	ni->ni_timoff = se->se_timoff;
77868e8e04eSSam Leffler 	ni->ni_fhdwell = se->se_fhdwell;
77968e8e04eSSam Leffler 	ni->ni_fhindex = se->se_fhindex;
78068e8e04eSSam Leffler 	ni->ni_erp = se->se_erp;
781b032f27cSSam Leffler 	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
78268e8e04eSSam Leffler 	ni->ni_noise = se->se_noise;
7836ca74c40SSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
7846ca74c40SSam Leffler 		/* NB: only infrastructure mode requires an associd */
7851b999d64SSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
7866ca74c40SSam Leffler 	}
78768e8e04eSSam Leffler 
788b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
789b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
790616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
791b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
792b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
793616190d0SSam Leffler #endif
794b032f27cSSam Leffler 		if (ni->ni_ies.htcap_ie != NULL)
795b032f27cSSam Leffler 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
796b032f27cSSam Leffler 		if (ni->ni_ies.htinfo_ie != NULL)
797b032f27cSSam Leffler 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
79859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
79959aa14a9SRui Paulo 		if (ni->ni_ies.meshid_ie != NULL)
80059aa14a9SRui Paulo 			ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
80159aa14a9SRui Paulo #endif
80210ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
80310ad9a77SSam Leffler 		if (ni->ni_ies.tdma_ie != NULL)
80410ad9a77SSam Leffler 			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
80510ad9a77SSam Leffler #endif
806b032f27cSSam Leffler 	}
807b032f27cSSam Leffler 
808b032f27cSSam Leffler 	vap->iv_dtim_period = se->se_dtimperiod;
809b032f27cSSam Leffler 	vap->iv_dtim_count = 0;
81068e8e04eSSam Leffler 
81168e8e04eSSam Leffler 	/* NB: must be after ni_chan is setup */
81268e8e04eSSam Leffler 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
81368e8e04eSSam Leffler 		IEEE80211_F_DOSORT);
814dfcd1f4dSSam Leffler 	if (ieee80211_iserp_rateset(&ni->ni_rates))
815dfcd1f4dSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
816d77148fbSSam Leffler 	ieee80211_node_setuptxparms(ni);
81749d2c137SBernhard Schmidt 	ieee80211_ratectl_node_init(ni);
81868e8e04eSSam Leffler 
81968e8e04eSSam Leffler 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
82068e8e04eSSam Leffler }
82168e8e04eSSam Leffler 
8228a1b9b6aSSam Leffler /*
8238a1b9b6aSSam Leffler  * Leave the specified IBSS/BSS network.  The node is assumed to
8248a1b9b6aSSam Leffler  * be passed in with a held reference.
8258a1b9b6aSSam Leffler  */
8268a1b9b6aSSam Leffler void
827b032f27cSSam Leffler ieee80211_sta_leave(struct ieee80211_node *ni)
8288a1b9b6aSSam Leffler {
829b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
830b032f27cSSam Leffler 
8318a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
832b032f27cSSam Leffler 	ieee80211_notify_node_leave(ni);
833b032f27cSSam Leffler }
834b032f27cSSam Leffler 
835b032f27cSSam Leffler /*
836b032f27cSSam Leffler  * Send a deauthenticate frame and drop the station.
837b032f27cSSam Leffler  */
838b032f27cSSam Leffler void
839b032f27cSSam Leffler ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
840b032f27cSSam Leffler {
841b032f27cSSam Leffler 	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
842b032f27cSSam Leffler 	ieee80211_ref_node(ni);
843b032f27cSSam Leffler 	if (ni->ni_associd != 0)
844b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
845b032f27cSSam Leffler 	ieee80211_node_leave(ni);
846b032f27cSSam Leffler 	ieee80211_free_node(ni);
8478a1b9b6aSSam Leffler }
8488a1b9b6aSSam Leffler 
8491a1e1d21SSam Leffler static struct ieee80211_node *
85038c208f8SSam Leffler node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
8511a1e1d21SSam Leffler {
852410ca74bSSam Leffler 	struct ieee80211_node *ni;
8538a1b9b6aSSam Leffler 
854e2126decSSam Leffler 	ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
855410ca74bSSam Leffler 		M_80211_NODE, M_NOWAIT | M_ZERO);
856410ca74bSSam Leffler 	return ni;
8571a1e1d21SSam Leffler }
8581a1e1d21SSam Leffler 
8598a1b9b6aSSam Leffler /*
860b032f27cSSam Leffler  * Initialize an ie blob with the specified data.  If previous
861b032f27cSSam Leffler  * data exists re-use the data block.  As a side effect we clear
862b032f27cSSam Leffler  * all references to specific ie's; the caller is required to
863b032f27cSSam Leffler  * recalculate them.
864b032f27cSSam Leffler  */
865b032f27cSSam Leffler int
866b032f27cSSam Leffler ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
867b032f27cSSam Leffler {
868b032f27cSSam Leffler 	/* NB: assumes data+len are the last fields */
869b032f27cSSam Leffler 	memset(ies, 0, offsetof(struct ieee80211_ies, data));
870b032f27cSSam Leffler 	if (ies->data != NULL && ies->len != len) {
871b032f27cSSam Leffler 		/* data size changed */
872e2126decSSam Leffler 		free(ies->data, M_80211_NODE_IE);
873b032f27cSSam Leffler 		ies->data = NULL;
874b032f27cSSam Leffler 	}
875b032f27cSSam Leffler 	if (ies->data == NULL) {
876e2126decSSam Leffler 		ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
877b032f27cSSam Leffler 		if (ies->data == NULL) {
878b032f27cSSam Leffler 			ies->len = 0;
879b032f27cSSam Leffler 			/* NB: pointers have already been zero'd above */
880b032f27cSSam Leffler 			return 0;
881b032f27cSSam Leffler 		}
882b032f27cSSam Leffler 	}
883b032f27cSSam Leffler 	memcpy(ies->data, data, len);
884b032f27cSSam Leffler 	ies->len = len;
885b032f27cSSam Leffler 	return 1;
886b032f27cSSam Leffler }
887b032f27cSSam Leffler 
888b032f27cSSam Leffler /*
889b032f27cSSam Leffler  * Reclaim storage for an ie blob.
890b032f27cSSam Leffler  */
891b032f27cSSam Leffler void
892b032f27cSSam Leffler ieee80211_ies_cleanup(struct ieee80211_ies *ies)
893b032f27cSSam Leffler {
894b032f27cSSam Leffler 	if (ies->data != NULL)
895e2126decSSam Leffler 		free(ies->data, M_80211_NODE_IE);
896b032f27cSSam Leffler }
897b032f27cSSam Leffler 
898b032f27cSSam Leffler /*
899b032f27cSSam Leffler  * Expand an ie blob data contents and to fillin individual
900b032f27cSSam Leffler  * ie pointers.  The data blob is assumed to be well-formed;
901b032f27cSSam Leffler  * we don't do any validity checking of ie lengths.
902b032f27cSSam Leffler  */
903b032f27cSSam Leffler void
904b032f27cSSam Leffler ieee80211_ies_expand(struct ieee80211_ies *ies)
905b032f27cSSam Leffler {
906b032f27cSSam Leffler 	uint8_t *ie;
907b032f27cSSam Leffler 	int ielen;
908b032f27cSSam Leffler 
909b032f27cSSam Leffler 	ie = ies->data;
910b032f27cSSam Leffler 	ielen = ies->len;
911b032f27cSSam Leffler 	while (ielen > 0) {
912b032f27cSSam Leffler 		switch (ie[0]) {
913b032f27cSSam Leffler 		case IEEE80211_ELEMID_VENDOR:
914b032f27cSSam Leffler 			if (iswpaoui(ie))
915b032f27cSSam Leffler 				ies->wpa_ie = ie;
916b032f27cSSam Leffler 			else if (iswmeoui(ie))
917b032f27cSSam Leffler 				ies->wme_ie = ie;
918616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
919b032f27cSSam Leffler 			else if (isatherosoui(ie))
920b032f27cSSam Leffler 				ies->ath_ie = ie;
921616190d0SSam Leffler #endif
92210ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
92310ad9a77SSam Leffler 			else if (istdmaoui(ie))
92410ad9a77SSam Leffler 				ies->tdma_ie = ie;
92510ad9a77SSam Leffler #endif
926b032f27cSSam Leffler 			break;
927b032f27cSSam Leffler 		case IEEE80211_ELEMID_RSN:
928b032f27cSSam Leffler 			ies->rsn_ie = ie;
929b032f27cSSam Leffler 			break;
930b032f27cSSam Leffler 		case IEEE80211_ELEMID_HTCAP:
931b032f27cSSam Leffler 			ies->htcap_ie = ie;
932b032f27cSSam Leffler 			break;
93359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
93459aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
93559aa14a9SRui Paulo 			ies->meshid_ie = ie;
93659aa14a9SRui Paulo 			break;
93759aa14a9SRui Paulo #endif
938b032f27cSSam Leffler 		}
939b032f27cSSam Leffler 		ielen -= 2 + ie[1];
940b032f27cSSam Leffler 		ie += 2 + ie[1];
941b032f27cSSam Leffler 	}
942b032f27cSSam Leffler }
943b032f27cSSam Leffler 
944b032f27cSSam Leffler /*
9458a1b9b6aSSam Leffler  * Reclaim any resources in a node and reset any critical
9468a1b9b6aSSam Leffler  * state.  Typically nodes are free'd immediately after,
9478a1b9b6aSSam Leffler  * but in some cases the storage may be reused so we need
9488a1b9b6aSSam Leffler  * to insure consistent state (should probably fix that).
9498a1b9b6aSSam Leffler  */
9501a1e1d21SSam Leffler static void
9518a1b9b6aSSam Leffler node_cleanup(struct ieee80211_node *ni)
9521a1e1d21SSam Leffler {
9538a1b9b6aSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
954b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
9555b16c28cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
95668e8e04eSSam Leffler 	int i;
9578a1b9b6aSSam Leffler 
9588a1b9b6aSSam Leffler 	/* NB: preserve ni_table */
9598a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
960b032f27cSSam Leffler 		if (vap->iv_opmode != IEEE80211_M_STA)
961b032f27cSSam Leffler 			vap->iv_ps_sta--;
9628a1b9b6aSSam Leffler 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
963b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
964b032f27cSSam Leffler 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
9658a1b9b6aSSam Leffler 	}
966ebdda46cSSam Leffler 	/*
9671b6167d2SSam Leffler 	 * Cleanup any HT-related state.
9681b6167d2SSam Leffler 	 */
9691b6167d2SSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_HT)
9701b6167d2SSam Leffler 		ieee80211_ht_node_cleanup(ni);
971339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
972339ccfb3SSam Leffler 	else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
973339ccfb3SSam Leffler 		ieee80211_ff_node_cleanup(ni);
974339ccfb3SSam Leffler #endif
97559aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
97659aa14a9SRui Paulo 	/*
97759aa14a9SRui Paulo 	 * Cleanup any mesh-related state.
97859aa14a9SRui Paulo 	 */
97959aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS)
98059aa14a9SRui Paulo 		ieee80211_mesh_node_cleanup(ni);
98159aa14a9SRui Paulo #endif
9821b6167d2SSam Leffler 	/*
9835b16c28cSSam Leffler 	 * Clear any staging queue entries.
9845b16c28cSSam Leffler 	 */
9855b16c28cSSam Leffler 	ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
9865b16c28cSSam Leffler 
9875b16c28cSSam Leffler 	/*
988ebdda46cSSam Leffler 	 * Clear AREF flag that marks the authorization refcnt bump
989ebdda46cSSam Leffler 	 * has happened.  This is probably not needed as the node
990ebdda46cSSam Leffler 	 * should always be removed from the table so not found but
991ebdda46cSSam Leffler 	 * do it just in case.
9921b999d64SSam Leffler 	 * Likewise clear the ASSOCID flag as these flags are intended
9931b999d64SSam Leffler 	 * to be managed in tandem.
994ebdda46cSSam Leffler 	 */
9951b999d64SSam Leffler 	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
9968a1b9b6aSSam Leffler 
9978a1b9b6aSSam Leffler 	/*
9988a1b9b6aSSam Leffler 	 * Drain power save queue and, if needed, clear TIM.
9998a1b9b6aSSam Leffler 	 */
100063092fceSSam Leffler 	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1001b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
10028a1b9b6aSSam Leffler 
10038a1b9b6aSSam Leffler 	ni->ni_associd = 0;
10048a1b9b6aSSam Leffler 	if (ni->ni_challenge != NULL) {
1005e2126decSSam Leffler 		free(ni->ni_challenge, M_80211_NODE);
10068a1b9b6aSSam Leffler 		ni->ni_challenge = NULL;
10078a1b9b6aSSam Leffler 	}
10088a1b9b6aSSam Leffler 	/*
10098a1b9b6aSSam Leffler 	 * Preserve SSID, WPA, and WME ie's so the bss node is
10108a1b9b6aSSam Leffler 	 * reusable during a re-auth/re-assoc state transition.
10118a1b9b6aSSam Leffler 	 * If we remove these data they will not be recreated
10128a1b9b6aSSam Leffler 	 * because they come from a probe-response or beacon frame
10138a1b9b6aSSam Leffler 	 * which cannot be expected prior to the association-response.
10148a1b9b6aSSam Leffler 	 * This should not be an issue when operating in other modes
10158a1b9b6aSSam Leffler 	 * as stations leaving always go through a full state transition
10168a1b9b6aSSam Leffler 	 * which will rebuild this state.
10178a1b9b6aSSam Leffler 	 *
10188a1b9b6aSSam Leffler 	 * XXX does this leave us open to inheriting old state?
10198a1b9b6aSSam Leffler 	 */
10208a1b9b6aSSam Leffler 	for (i = 0; i < N(ni->ni_rxfrag); i++)
10218a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[i] != NULL) {
10228a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[i]);
10238a1b9b6aSSam Leffler 			ni->ni_rxfrag[i] = NULL;
10248a1b9b6aSSam Leffler 		}
1025c1225b52SSam Leffler 	/*
1026c1225b52SSam Leffler 	 * Must be careful here to remove any key map entry w/o a LOR.
1027c1225b52SSam Leffler 	 */
1028c1225b52SSam Leffler 	ieee80211_node_delucastkey(ni);
10298a1b9b6aSSam Leffler #undef N
10308a1b9b6aSSam Leffler }
10318a1b9b6aSSam Leffler 
10328a1b9b6aSSam Leffler static void
10338a1b9b6aSSam Leffler node_free(struct ieee80211_node *ni)
10348a1b9b6aSSam Leffler {
10358a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10368a1b9b6aSSam Leffler 
1037b6108616SRui Paulo 	ieee80211_ratectl_node_deinit(ni);
10388a1b9b6aSSam Leffler 	ic->ic_node_cleanup(ni);
1039b032f27cSSam Leffler 	ieee80211_ies_cleanup(&ni->ni_ies);
104063092fceSSam Leffler 	ieee80211_psq_cleanup(&ni->ni_psq);
1041e2126decSSam Leffler 	free(ni, M_80211_NODE);
10421a1e1d21SSam Leffler }
10431a1e1d21SSam Leffler 
1044b032f27cSSam Leffler static void
1045b032f27cSSam Leffler node_age(struct ieee80211_node *ni)
1046b032f27cSSam Leffler {
1047b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
10485d44f8c0SSam Leffler 
10495d44f8c0SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
10505d44f8c0SSam Leffler 
1051b032f27cSSam Leffler 	/*
1052b032f27cSSam Leffler 	 * Age frames on the power save queue.
1053b032f27cSSam Leffler 	 */
105463092fceSSam Leffler 	if (ieee80211_node_psq_age(ni) != 0 &&
105563092fceSSam Leffler 	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1056b032f27cSSam Leffler 		vap->iv_set_tim(ni, 0);
1057b032f27cSSam Leffler 	/*
1058b032f27cSSam Leffler 	 * Age out HT resources (e.g. frames on the
1059b032f27cSSam Leffler 	 * A-MPDU reorder queues).
1060b032f27cSSam Leffler 	 */
1061b032f27cSSam Leffler 	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1062b032f27cSSam Leffler 		ieee80211_ht_node_age(ni);
1063b032f27cSSam Leffler }
1064b032f27cSSam Leffler 
106568e8e04eSSam Leffler static int8_t
10668a1b9b6aSSam Leffler node_getrssi(const struct ieee80211_node *ni)
1067d1e61976SSam Leffler {
1068b032f27cSSam Leffler 	uint32_t avgrssi = ni->ni_avgrssi;
1069b032f27cSSam Leffler 	int32_t rssi;
1070b032f27cSSam Leffler 
1071b032f27cSSam Leffler 	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1072b032f27cSSam Leffler 		return 0;
1073b032f27cSSam Leffler 	rssi = IEEE80211_RSSI_GET(avgrssi);
1074b032f27cSSam Leffler 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1075d1e61976SSam Leffler }
1076d1e61976SSam Leffler 
10771a1e1d21SSam Leffler static void
107868e8e04eSSam Leffler node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
107968e8e04eSSam Leffler {
1080b032f27cSSam Leffler 	*rssi = node_getrssi(ni);
108168e8e04eSSam Leffler 	*noise = ni->ni_noise;
108268e8e04eSSam Leffler }
108368e8e04eSSam Leffler 
108468e8e04eSSam Leffler static void
1085b032f27cSSam Leffler node_getmimoinfo(const struct ieee80211_node *ni,
1086b032f27cSSam Leffler 	struct ieee80211_mimo_info *info)
1087b032f27cSSam Leffler {
1088*864ab114SAdrian Chadd 	int i;
1089*864ab114SAdrian Chadd 	uint32_t avgrssi;
1090*864ab114SAdrian Chadd 	int32_t rssi;
1091*864ab114SAdrian Chadd 
1092*864ab114SAdrian Chadd 	bzero(info, sizeof(*info));
1093*864ab114SAdrian Chadd 
1094*864ab114SAdrian Chadd 	for (i = 0; i < ni->ni_mimo_chains; i++) {
1095*864ab114SAdrian Chadd 		avgrssi = ni->ni_mimo_rssi_ctl[i];
1096*864ab114SAdrian Chadd 		if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1097*864ab114SAdrian Chadd 			info->rssi[i] = 0;
1098*864ab114SAdrian Chadd 		} else {
1099*864ab114SAdrian Chadd 			rssi = IEEE80211_RSSI_GET(avgrssi);
1100*864ab114SAdrian Chadd 			info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1101*864ab114SAdrian Chadd 		}
1102*864ab114SAdrian Chadd 		info->noise[i] = ni->ni_mimo_noise_ctl[i];
1103*864ab114SAdrian Chadd 	}
1104*864ab114SAdrian Chadd 
1105*864ab114SAdrian Chadd 	/* XXX ext radios? */
1106*864ab114SAdrian Chadd 
1107*864ab114SAdrian Chadd 	/* XXX EVM? */
1108b032f27cSSam Leffler }
1109b032f27cSSam Leffler 
1110b032f27cSSam Leffler struct ieee80211_node *
1111b032f27cSSam Leffler ieee80211_alloc_node(struct ieee80211_node_table *nt,
1112b032f27cSSam Leffler 	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
11131a1e1d21SSam Leffler {
11148a1b9b6aSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
1115b032f27cSSam Leffler 	struct ieee80211_node *ni;
11161a1e1d21SSam Leffler 	int hash;
11171a1e1d21SSam Leffler 
111838c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
1119b032f27cSSam Leffler 	if (ni == NULL) {
1120b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
1121b032f27cSSam Leffler 		return NULL;
1122b032f27cSSam Leffler 	}
1123b032f27cSSam Leffler 
1124b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
112549a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
11268a1b9b6aSSam Leffler 		ether_sprintf(macaddr), nt->nt_name);
11278a1b9b6aSSam Leffler 
11281a1e1d21SSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
112959aa14a9SRui Paulo 	hash = IEEE80211_NODE_HASH(ic, macaddr);
11308a1b9b6aSSam Leffler 	ieee80211_node_initref(ni);		/* mark referenced */
11318a1b9b6aSSam Leffler 	ni->ni_chan = IEEE80211_CHAN_ANYC;
11328a1b9b6aSSam Leffler 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
11338a1b9b6aSSam Leffler 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
113401a03542SSam Leffler 	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1135b032f27cSSam Leffler 	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1136b032f27cSSam Leffler 	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
11372045f699SSam Leffler 	ni->ni_inact_reload = nt->nt_inact_init;
11382045f699SSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
113968e8e04eSSam Leffler 	ni->ni_ath_defkeyix = 0x7fff;
114063092fceSSam Leffler 	ieee80211_psq_init(&ni->ni_psq, "unknown");
114159aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
114259aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS)
114359aa14a9SRui Paulo 		ieee80211_mesh_node_init(vap, ni);
114459aa14a9SRui Paulo #endif
11458a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
11468a1b9b6aSSam Leffler 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
11478a1b9b6aSSam Leffler 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
11488a1b9b6aSSam Leffler 	ni->ni_table = nt;
1149b032f27cSSam Leffler 	ni->ni_vap = vap;
11508a1b9b6aSSam Leffler 	ni->ni_ic = ic;
11518a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
11521a1e1d21SSam Leffler 
1153be1054edSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1154be1054edSSam Leffler 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1155be1054edSSam Leffler 
1156bd56e71bSBernhard Schmidt 	ieee80211_ratectl_node_init(ni);
1157bd56e71bSBernhard Schmidt 
11581a1e1d21SSam Leffler 	return ni;
11591a1e1d21SSam Leffler }
11601a1e1d21SSam Leffler 
116197c973adSSam Leffler /*
116297c973adSSam Leffler  * Craft a temporary node suitable for sending a management frame
116397c973adSSam Leffler  * to the specified station.  We craft only as much state as we
116497c973adSSam Leffler  * need to do the work since the node will be immediately reclaimed
116597c973adSSam Leffler  * once the send completes.
116697c973adSSam Leffler  */
116797c973adSSam Leffler struct ieee80211_node *
1168b032f27cSSam Leffler ieee80211_tmp_node(struct ieee80211vap *vap,
1169b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
117097c973adSSam Leffler {
1171b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
117297c973adSSam Leffler 	struct ieee80211_node *ni;
117397c973adSSam Leffler 
117438c208f8SSam Leffler 	ni = ic->ic_node_alloc(vap, macaddr);
117597c973adSSam Leffler 	if (ni != NULL) {
1176b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1177b9b5f07dSSam Leffler 
1178b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
117997c973adSSam Leffler 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
118097c973adSSam Leffler 
1181b032f27cSSam Leffler 		ni->ni_table = NULL;		/* NB: pedantic */
1182b032f27cSSam Leffler 		ni->ni_ic = ic;			/* NB: needed to set channel */
1183b032f27cSSam Leffler 		ni->ni_vap = vap;
1184b032f27cSSam Leffler 
118597c973adSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1186b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
118797c973adSSam Leffler 		ieee80211_node_initref(ni);		/* mark referenced */
118897c973adSSam Leffler 		/* NB: required by ieee80211_fix_rate */
1189b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1190b032f27cSSam Leffler 		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
119197c973adSSam Leffler 			IEEE80211_KEYIX_NONE);
1192b9b5f07dSSam Leffler 		ni->ni_txpower = bss->ni_txpower;
119397c973adSSam Leffler 		/* XXX optimize away */
119463092fceSSam Leffler 		ieee80211_psq_init(&ni->ni_psq, "unknown");
1195bd56e71bSBernhard Schmidt 
1196bd56e71bSBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
119797c973adSSam Leffler 	} else {
119897c973adSSam Leffler 		/* XXX msg */
1199b032f27cSSam Leffler 		vap->iv_stats.is_rx_nodealloc++;
120097c973adSSam Leffler 	}
120197c973adSSam Leffler 	return ni;
120297c973adSSam Leffler }
120397c973adSSam Leffler 
12041a1e1d21SSam Leffler struct ieee80211_node *
1205b032f27cSSam Leffler ieee80211_dup_bss(struct ieee80211vap *vap,
1206b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
12071a1e1d21SSam Leffler {
1208b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
12098a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
12108a1b9b6aSSam Leffler 
1211b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
12121a1e1d21SSam Leffler 	if (ni != NULL) {
1213b9b5f07dSSam Leffler 		struct ieee80211_node *bss = vap->iv_bss;
1214694dca64SSam Leffler 		/*
1215b032f27cSSam Leffler 		 * Inherit from iv_bss.
1216694dca64SSam Leffler 		 */
1217b9b5f07dSSam Leffler 		copy_bss(ni, bss);
1218b9b5f07dSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1219b9b5f07dSSam Leffler 		ieee80211_node_set_chan(ni, bss->ni_chan);
1220b032f27cSSam Leffler 	}
12211a1e1d21SSam Leffler 	return ni;
12221a1e1d21SSam Leffler }
12231a1e1d21SSam Leffler 
1224b032f27cSSam Leffler /*
1225b032f27cSSam Leffler  * Create a bss node for a legacy WDS vap.  The far end does
1226b032f27cSSam Leffler  * not associate so we just create create a new node and
1227b032f27cSSam Leffler  * simulate an association.  The caller is responsible for
1228b032f27cSSam Leffler  * installing the node as the bss node and handling any further
1229b032f27cSSam Leffler  * setup work like authorizing the port.
1230b032f27cSSam Leffler  */
1231b032f27cSSam Leffler struct ieee80211_node *
1232b032f27cSSam Leffler ieee80211_node_create_wds(struct ieee80211vap *vap,
1233b032f27cSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1234b032f27cSSam Leffler {
1235b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1236b032f27cSSam Leffler 	struct ieee80211_node *ni;
1237b032f27cSSam Leffler 
1238b032f27cSSam Leffler 	/* XXX check if node already in sta table? */
1239b032f27cSSam Leffler 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1240b032f27cSSam Leffler 	if (ni != NULL) {
1241b032f27cSSam Leffler 		ni->ni_wdsvap = vap;
1242b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1243b032f27cSSam Leffler 		/*
1244b032f27cSSam Leffler 		 * Inherit any manually configured settings.
1245b032f27cSSam Leffler 		 */
1246b9b5f07dSSam Leffler 		copy_bss(ni, vap->iv_bss);
1247b032f27cSSam Leffler 		ieee80211_node_set_chan(ni, chan);
1248b032f27cSSam Leffler 		/* NB: propagate ssid so available to WPA supplicant */
1249b032f27cSSam Leffler 		ni->ni_esslen = vap->iv_des_ssid[0].len;
1250b032f27cSSam Leffler 		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1251b032f27cSSam Leffler 		/* NB: no associd for peer */
1252b032f27cSSam Leffler 		/*
1253b032f27cSSam Leffler 		 * There are no management frames to use to
1254b032f27cSSam Leffler 		 * discover neighbor capabilities, so blindly
1255b032f27cSSam Leffler 		 * propagate the local configuration.
1256b032f27cSSam Leffler 		 */
1257b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WME)
1258b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
1259616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1260b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_FF)
1261b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_FF;
1262616190d0SSam Leffler #endif
1263b032f27cSSam Leffler 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
12642bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1265b032f27cSSam Leffler 			/*
1266b032f27cSSam Leffler 			 * Device is HT-capable and HT is enabled for
1267b032f27cSSam Leffler 			 * the vap; setup HT operation.  On return
1268b032f27cSSam Leffler 			 * ni_chan will be adjusted to an HT channel.
1269b032f27cSSam Leffler 			 */
1270b032f27cSSam Leffler 			ieee80211_ht_wds_init(ni);
1271b032f27cSSam Leffler 		} else {
1272b032f27cSSam Leffler 			struct ieee80211_channel *c = ni->ni_chan;
1273b032f27cSSam Leffler 			/*
1274b032f27cSSam Leffler 			 * Force a legacy channel to be used.
1275b032f27cSSam Leffler 			 */
1276b032f27cSSam Leffler 			c = ieee80211_find_channel(ic,
1277b032f27cSSam Leffler 			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1278b032f27cSSam Leffler 			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1279b032f27cSSam Leffler 			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1280b032f27cSSam Leffler 			ni->ni_chan = c;
1281b032f27cSSam Leffler 		}
1282b032f27cSSam Leffler 	}
1283b032f27cSSam Leffler 	return ni;
1284b032f27cSSam Leffler }
1285b032f27cSSam Leffler 
1286b032f27cSSam Leffler struct ieee80211_node *
12878a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1288b032f27cSSam Leffler ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1289b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
12908a1b9b6aSSam Leffler #else
1291b032f27cSSam Leffler ieee80211_find_node_locked(struct ieee80211_node_table *nt,
1292b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
12938a1b9b6aSSam Leffler #endif
12941a1e1d21SSam Leffler {
12951a1e1d21SSam Leffler 	struct ieee80211_node *ni;
12961a1e1d21SSam Leffler 	int hash;
12971a1e1d21SSam Leffler 
12988a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1299750d6d0cSSam Leffler 
130059aa14a9SRui Paulo 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
13018a1b9b6aSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
13021a1e1d21SSam Leffler 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
13038a1b9b6aSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
13048a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1305b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
130649a15236SSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
130749a15236SSam Leffler 			    func, line,
130849a15236SSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
13098a1b9b6aSSam Leffler 			    ieee80211_node_refcnt(ni));
13108a1b9b6aSSam Leffler #endif
1311750d6d0cSSam Leffler 			return ni;
13121a1e1d21SSam Leffler 		}
13131a1e1d21SSam Leffler 	}
1314750d6d0cSSam Leffler 	return NULL;
1315750d6d0cSSam Leffler }
1316750d6d0cSSam Leffler 
1317750d6d0cSSam Leffler struct ieee80211_node *
13188a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
13198a1b9b6aSSam Leffler ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1320b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
13218a1b9b6aSSam Leffler #else
1322b032f27cSSam Leffler ieee80211_find_node(struct ieee80211_node_table *nt,
1323b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
13248a1b9b6aSSam Leffler #endif
1325750d6d0cSSam Leffler {
1326750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1327750d6d0cSSam Leffler 
13288a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1329b032f27cSSam Leffler 	ni = ieee80211_find_node_locked(nt, macaddr);
1330b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1331b032f27cSSam Leffler 	return ni;
1332b032f27cSSam Leffler }
1333b032f27cSSam Leffler 
1334b032f27cSSam Leffler struct ieee80211_node *
1335b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1336b032f27cSSam Leffler ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1337b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1338b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1339b032f27cSSam Leffler #else
1340b032f27cSSam Leffler ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1341b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1342b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1343b032f27cSSam Leffler #endif
1344b032f27cSSam Leffler {
1345b032f27cSSam Leffler 	struct ieee80211_node *ni;
1346b032f27cSSam Leffler 	int hash;
1347b032f27cSSam Leffler 
1348b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
1349b032f27cSSam Leffler 
135059aa14a9SRui Paulo 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1351b032f27cSSam Leffler 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1352b032f27cSSam Leffler 		if (ni->ni_vap == vap &&
1353b032f27cSSam Leffler 		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1354b032f27cSSam Leffler 			ieee80211_ref_node(ni);	/* mark referenced */
1355b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1356b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1357b032f27cSSam Leffler 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1358b032f27cSSam Leffler 			    func, line,
1359b032f27cSSam Leffler 			    ni, ether_sprintf(ni->ni_macaddr),
1360b032f27cSSam Leffler 			    ieee80211_node_refcnt(ni));
1361b032f27cSSam Leffler #endif
1362b032f27cSSam Leffler 			return ni;
1363b032f27cSSam Leffler 		}
1364b032f27cSSam Leffler 	}
1365b032f27cSSam Leffler 	return NULL;
1366b032f27cSSam Leffler }
1367b032f27cSSam Leffler 
1368b032f27cSSam Leffler struct ieee80211_node *
1369b032f27cSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1370b032f27cSSam Leffler ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1371b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1372b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1373b032f27cSSam Leffler #else
1374b032f27cSSam Leffler ieee80211_find_vap_node(struct ieee80211_node_table *nt,
1375b032f27cSSam Leffler 	const struct ieee80211vap *vap,
1376b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1377b032f27cSSam Leffler #endif
1378b032f27cSSam Leffler {
1379b032f27cSSam Leffler 	struct ieee80211_node *ni;
1380b032f27cSSam Leffler 
1381b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1382b032f27cSSam Leffler 	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
13838a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
13841a1e1d21SSam Leffler 	return ni;
13851a1e1d21SSam Leffler }
13861a1e1d21SSam Leffler 
13871a1e1d21SSam Leffler /*
13888a1b9b6aSSam Leffler  * Fake up a node; this handles node discovery in adhoc mode.
13898a1b9b6aSSam Leffler  * Note that for the driver's benefit we we treat this like
13908a1b9b6aSSam Leffler  * an association so the driver has an opportunity to setup
13918a1b9b6aSSam Leffler  * it's private state.
13928a1b9b6aSSam Leffler  */
13938a1b9b6aSSam Leffler struct ieee80211_node *
1394b032f27cSSam Leffler ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
139568e8e04eSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
13968a1b9b6aSSam Leffler {
13978a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
13988a1b9b6aSSam Leffler 
1399b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1400be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1401b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, macaddr);
14028a1b9b6aSSam Leffler 	if (ni != NULL) {
1403b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1404b032f27cSSam Leffler 
14058a1b9b6aSSam Leffler 		/* XXX no rate negotiation; just dup */
1406b032f27cSSam Leffler 		ni->ni_rates = vap->iv_bss->ni_rates;
1407aa68c24fSSam Leffler 		if (ieee80211_iserp_rateset(&ni->ni_rates))
1408aa68c24fSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_ERP;
1409b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
14108e292e8eSSam Leffler 			/*
141168e8e04eSSam Leffler 			 * In adhoc demo mode there are no management
141268e8e04eSSam Leffler 			 * frames to use to discover neighbor capabilities,
141368e8e04eSSam Leffler 			 * so blindly propagate the local configuration
141468e8e04eSSam Leffler 			 * so we can do interesting things (e.g. use
141568e8e04eSSam Leffler 			 * WME to disable ACK's).
14168e292e8eSSam Leffler 			 */
1417b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_WME)
14188e292e8eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
1419616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1420b032f27cSSam Leffler 			if (vap->iv_flags & IEEE80211_F_FF)
142168e8e04eSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_FF;
1422616190d0SSam Leffler #endif
14238e292e8eSSam Leffler 		}
1424d77148fbSSam Leffler 		ieee80211_node_setuptxparms(ni);
142549d2c137SBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
1426b032f27cSSam Leffler 		if (ic->ic_newassoc != NULL)
1427b032f27cSSam Leffler 			ic->ic_newassoc(ni, 1);
142868e8e04eSSam Leffler 		/* XXX not right for 802.1x/WPA */
142968e8e04eSSam Leffler 		ieee80211_node_authorize(ni);
14308a1b9b6aSSam Leffler 	}
14318a1b9b6aSSam Leffler 	return ni;
14328a1b9b6aSSam Leffler }
14338a1b9b6aSSam Leffler 
1434be425a0fSSam Leffler void
1435be425a0fSSam Leffler ieee80211_init_neighbor(struct ieee80211_node *ni,
1436be425a0fSSam Leffler 	const struct ieee80211_frame *wh,
1437be425a0fSSam Leffler 	const struct ieee80211_scanparams *sp)
1438be425a0fSSam Leffler {
1439be425a0fSSam Leffler 	ni->ni_esslen = sp->ssid[1];
1440be425a0fSSam Leffler 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1441be425a0fSSam Leffler 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1442be425a0fSSam Leffler 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1443be425a0fSSam Leffler 	ni->ni_intval = sp->bintval;
1444be425a0fSSam Leffler 	ni->ni_capinfo = sp->capinfo;
1445be425a0fSSam Leffler 	ni->ni_chan = ni->ni_ic->ic_curchan;
1446be425a0fSSam Leffler 	ni->ni_fhdwell = sp->fhdwell;
1447be425a0fSSam Leffler 	ni->ni_fhindex = sp->fhindex;
1448be425a0fSSam Leffler 	ni->ni_erp = sp->erp;
1449be425a0fSSam Leffler 	ni->ni_timoff = sp->timoff;
145059aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
145159aa14a9SRui Paulo 	if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
145259aa14a9SRui Paulo 		ieee80211_mesh_init_neighbor(ni, wh, sp);
145359aa14a9SRui Paulo #endif
1454b032f27cSSam Leffler 	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1455b032f27cSSam Leffler 		ieee80211_ies_expand(&ni->ni_ies);
14569094ffdfSSam Leffler 		if (ni->ni_ies.wme_ie != NULL)
14579094ffdfSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_QOS;
14589094ffdfSSam Leffler 		else
14599094ffdfSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1460616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1461b032f27cSSam Leffler 		if (ni->ni_ies.ath_ie != NULL)
1462b032f27cSSam Leffler 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1463616190d0SSam Leffler #endif
1464b032f27cSSam Leffler 	}
1465be425a0fSSam Leffler 
1466be425a0fSSam Leffler 	/* NB: must be after ni_chan is setup */
146779edaebfSSam Leffler 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
146879edaebfSSam Leffler 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
146979edaebfSSam Leffler 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1470be425a0fSSam Leffler }
1471be425a0fSSam Leffler 
1472b5c99415SSam Leffler /*
1473b5c99415SSam Leffler  * Do node discovery in adhoc mode on receipt of a beacon
1474b5c99415SSam Leffler  * or probe response frame.  Note that for the driver's
1475b5c99415SSam Leffler  * benefit we we treat this like an association so the
1476b5c99415SSam Leffler  * driver has an opportunity to setup it's private state.
1477b5c99415SSam Leffler  */
1478b5c99415SSam Leffler struct ieee80211_node *
1479b032f27cSSam Leffler ieee80211_add_neighbor(struct ieee80211vap *vap,
1480b5c99415SSam Leffler 	const struct ieee80211_frame *wh,
1481b5c99415SSam Leffler 	const struct ieee80211_scanparams *sp)
1482b5c99415SSam Leffler {
1483b5c99415SSam Leffler 	struct ieee80211_node *ni;
1484b5c99415SSam Leffler 
1485b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1486be425a0fSSam Leffler 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1487b032f27cSSam Leffler 	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1488b5c99415SSam Leffler 	if (ni != NULL) {
1489b032f27cSSam Leffler 		struct ieee80211com *ic = vap->iv_ic;
1490b032f27cSSam Leffler 
1491be425a0fSSam Leffler 		ieee80211_init_neighbor(ni, wh, sp);
1492aa68c24fSSam Leffler 		if (ieee80211_iserp_rateset(&ni->ni_rates))
1493aa68c24fSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_ERP;
1494d77148fbSSam Leffler 		ieee80211_node_setuptxparms(ni);
149549d2c137SBernhard Schmidt 		ieee80211_ratectl_node_init(ni);
1496b5c99415SSam Leffler 		if (ic->ic_newassoc != NULL)
1497b5c99415SSam Leffler 			ic->ic_newassoc(ni, 1);
1498b5c99415SSam Leffler 		/* XXX not right for 802.1x/WPA */
1499b5c99415SSam Leffler 		ieee80211_node_authorize(ni);
1500b5c99415SSam Leffler 	}
1501b5c99415SSam Leffler 	return ni;
1502b5c99415SSam Leffler }
1503b5c99415SSam Leffler 
1504a2cfa5b7SSam Leffler #define	IS_PROBEREQ(wh) \
1505a2cfa5b7SSam Leffler 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1506a2cfa5b7SSam Leffler 	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1507a2cfa5b7SSam Leffler #define	IS_BCAST_PROBEREQ(wh) \
1508a2cfa5b7SSam Leffler 	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1509a2cfa5b7SSam Leffler 	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1510a2cfa5b7SSam Leffler 
1511a2cfa5b7SSam Leffler static __inline struct ieee80211_node *
1512a2cfa5b7SSam Leffler _find_rxnode(struct ieee80211_node_table *nt,
1513a2cfa5b7SSam Leffler     const struct ieee80211_frame_min *wh)
1514a2cfa5b7SSam Leffler {
1515a2cfa5b7SSam Leffler 	if (IS_BCAST_PROBEREQ(wh))
1516a2cfa5b7SSam Leffler 		return NULL;		/* spam bcast probe req to all vap's */
1517a2cfa5b7SSam Leffler 	return ieee80211_find_node_locked(nt, wh->i_addr2);
1518a2cfa5b7SSam Leffler }
151968e8e04eSSam Leffler 
15208a1b9b6aSSam Leffler /*
15218a1b9b6aSSam Leffler  * Locate the node for sender, track state, and then pass the
1522a2cfa5b7SSam Leffler  * (referenced) node up to the 802.11 layer for its use.  Note
1523a2cfa5b7SSam Leffler  * we can return NULL if the sender is not in the table.
15248a1b9b6aSSam Leffler  */
15258a1b9b6aSSam Leffler struct ieee80211_node *
15268a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
15278a1b9b6aSSam Leffler ieee80211_find_rxnode_debug(struct ieee80211com *ic,
15288a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh, const char *func, int line)
15298a1b9b6aSSam Leffler #else
15308a1b9b6aSSam Leffler ieee80211_find_rxnode(struct ieee80211com *ic,
15318a1b9b6aSSam Leffler 	const struct ieee80211_frame_min *wh)
15328a1b9b6aSSam Leffler #endif
15338a1b9b6aSSam Leffler {
15348a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
15358a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
15368a1b9b6aSSam Leffler 
153768e8e04eSSam Leffler 	nt = &ic->ic_sta;
15388a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1539a2cfa5b7SSam Leffler 	ni = _find_rxnode(nt, wh);
15408a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
15418a1b9b6aSSam Leffler 
1542c1225b52SSam Leffler 	return ni;
1543c1225b52SSam Leffler }
1544c1225b52SSam Leffler 
1545c1225b52SSam Leffler /*
1546c1225b52SSam Leffler  * Like ieee80211_find_rxnode but use the supplied h/w
1547c1225b52SSam Leffler  * key index as a hint to locate the node in the key
1548c1225b52SSam Leffler  * mapping table.  If an entry is present at the key
1549c1225b52SSam Leffler  * index we return it; otherwise do a normal lookup and
1550c1225b52SSam Leffler  * update the mapping table if the station has a unicast
1551c1225b52SSam Leffler  * key assigned to it.
1552c1225b52SSam Leffler  */
1553c1225b52SSam Leffler struct ieee80211_node *
1554c1225b52SSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1555c1225b52SSam Leffler ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1556c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1557c1225b52SSam Leffler 	const char *func, int line)
1558c1225b52SSam Leffler #else
1559c1225b52SSam Leffler ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1560c1225b52SSam Leffler 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1561c1225b52SSam Leffler #endif
1562c1225b52SSam Leffler {
1563c1225b52SSam Leffler 	struct ieee80211_node_table *nt;
1564c1225b52SSam Leffler 	struct ieee80211_node *ni;
1565c1225b52SSam Leffler 
1566c1225b52SSam Leffler 	nt = &ic->ic_sta;
1567c1225b52SSam Leffler 	IEEE80211_NODE_LOCK(nt);
1568c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1569c1225b52SSam Leffler 		ni = nt->nt_keyixmap[keyix];
1570c1225b52SSam Leffler 	else
1571c1225b52SSam Leffler 		ni = NULL;
1572c1225b52SSam Leffler 	if (ni == NULL) {
1573a2cfa5b7SSam Leffler 		ni = _find_rxnode(nt, wh);
1574b032f27cSSam Leffler 		if (ni != NULL && nt->nt_keyixmap != NULL) {
1575c1225b52SSam Leffler 			/*
1576c1225b52SSam Leffler 			 * If the station has a unicast key cache slot
1577c1225b52SSam Leffler 			 * assigned update the key->node mapping table.
1578c1225b52SSam Leffler 			 */
1579c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1580c1225b52SSam Leffler 			/* XXX can keyixmap[keyix] != NULL? */
1581c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1582c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == NULL) {
1583b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1584b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
1585c1225b52SSam Leffler 				    "%s: add key map entry %p<%s> refcnt %d\n",
1586c1225b52SSam Leffler 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1587c1225b52SSam Leffler 				    ieee80211_node_refcnt(ni)+1);
1588c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1589c1225b52SSam Leffler 			}
1590c1225b52SSam Leffler 		}
1591a2cfa5b7SSam Leffler 	} else {
1592a2cfa5b7SSam Leffler 		if (IS_BCAST_PROBEREQ(wh))
1593a2cfa5b7SSam Leffler 			ni = NULL;	/* spam bcast probe req to all vap's */
1594a2cfa5b7SSam Leffler 		else
1595c1225b52SSam Leffler 			ieee80211_ref_node(ni);
1596a2cfa5b7SSam Leffler 	}
1597c1225b52SSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1598c1225b52SSam Leffler 
1599c1225b52SSam Leffler 	return ni;
1600c1225b52SSam Leffler }
1601a2cfa5b7SSam Leffler #undef IS_BCAST_PROBEREQ
1602a2cfa5b7SSam Leffler #undef IS_PROBEREQ
16038a1b9b6aSSam Leffler 
16048a1b9b6aSSam Leffler /*
1605750d6d0cSSam Leffler  * Return a reference to the appropriate node for sending
1606750d6d0cSSam Leffler  * a data frame.  This handles node discovery in adhoc networks.
1607750d6d0cSSam Leffler  */
1608750d6d0cSSam Leffler struct ieee80211_node *
16098a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1610b032f27cSSam Leffler ieee80211_find_txnode_debug(struct ieee80211vap *vap,
1611b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN],
16128a1b9b6aSSam Leffler 	const char *func, int line)
16138a1b9b6aSSam Leffler #else
1614b032f27cSSam Leffler ieee80211_find_txnode(struct ieee80211vap *vap,
1615b032f27cSSam Leffler 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
16168a1b9b6aSSam Leffler #endif
1617750d6d0cSSam Leffler {
1618b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1619750d6d0cSSam Leffler 	struct ieee80211_node *ni;
1620750d6d0cSSam Leffler 
1621750d6d0cSSam Leffler 	/*
1622750d6d0cSSam Leffler 	 * The destination address should be in the node table
1623c1225b52SSam Leffler 	 * unless this is a multicast/broadcast frame.  We can
1624c1225b52SSam Leffler 	 * also optimize station mode operation, all frames go
1625c1225b52SSam Leffler 	 * to the bss node.
1626750d6d0cSSam Leffler 	 */
1627750d6d0cSSam Leffler 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
16288a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1629b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA ||
1630b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_WDS ||
1631b032f27cSSam Leffler 	    IEEE80211_IS_MULTICAST(macaddr))
1632b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
16331b999d64SSam Leffler 	else
1634b032f27cSSam Leffler 		ni = ieee80211_find_node_locked(nt, macaddr);
16358a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
16368a1b9b6aSSam Leffler 
16378a1b9b6aSSam Leffler 	if (ni == NULL) {
1638b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1639b032f27cSSam Leffler 		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
164090d0d036SSam Leffler 			/*
164190d0d036SSam Leffler 			 * In adhoc mode cons up a node for the destination.
164290d0d036SSam Leffler 			 * Note that we need an additional reference for the
1643b032f27cSSam Leffler 			 * caller to be consistent with
1644b032f27cSSam Leffler 			 * ieee80211_find_node_locked.
164590d0d036SSam Leffler 			 */
1646b032f27cSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
164790d0d036SSam Leffler 			if (ni != NULL)
164890d0d036SSam Leffler 				(void) ieee80211_ref_node(ni);
164990d0d036SSam Leffler 		} else {
1650b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1651b032f27cSSam Leffler 			    "no node, discard frame (%s)", __func__);
1652b032f27cSSam Leffler 			vap->iv_stats.is_tx_nonode++;
1653750d6d0cSSam Leffler 		}
1654750d6d0cSSam Leffler 	}
1655750d6d0cSSam Leffler 	return ni;
1656750d6d0cSSam Leffler }
1657750d6d0cSSam Leffler 
16588a1b9b6aSSam Leffler static void
16598a1b9b6aSSam Leffler _ieee80211_free_node(struct ieee80211_node *ni)
16608a1b9b6aSSam Leffler {
16618a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
16628a1b9b6aSSam Leffler 
166337e9743aSSam Leffler 	/*
166437e9743aSSam Leffler 	 * NB: careful about referencing the vap as it may be
166537e9743aSSam Leffler 	 * gone if the last reference was held by a driver.
166637e9743aSSam Leffler 	 * We know the com will always be present so it's safe
166737e9743aSSam Leffler 	 * to use ni_ic below to reclaim resources.
166837e9743aSSam Leffler 	 */
166937e9743aSSam Leffler #if 0
1670b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
167149a15236SSam Leffler 		"%s %p<%s> in %s table\n", __func__, ni,
167249a15236SSam Leffler 		ether_sprintf(ni->ni_macaddr),
16738a1b9b6aSSam Leffler 		nt != NULL ? nt->nt_name : "<gone>");
167437e9743aSSam Leffler #endif
167537e9743aSSam Leffler 	if (ni->ni_associd != 0) {
167637e9743aSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
1677b032f27cSSam Leffler 		if (vap->iv_aid_bitmap != NULL)
1678b032f27cSSam Leffler 			IEEE80211_AID_CLR(vap, ni->ni_associd);
167937e9743aSSam Leffler 	}
16808a1b9b6aSSam Leffler 	if (nt != NULL) {
16818a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
16828a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
16838a1b9b6aSSam Leffler 	}
168437e9743aSSam Leffler 	ni->ni_ic->ic_node_free(ni);
16858a1b9b6aSSam Leffler }
16868a1b9b6aSSam Leffler 
16878a1b9b6aSSam Leffler void
16888a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
16898a1b9b6aSSam Leffler ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
16908a1b9b6aSSam Leffler #else
16918a1b9b6aSSam Leffler ieee80211_free_node(struct ieee80211_node *ni)
16928a1b9b6aSSam Leffler #endif
16938a1b9b6aSSam Leffler {
16948a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
16958a1b9b6aSSam Leffler 
16968a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG_REFCNT
1697b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
169849a15236SSam Leffler 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
16998a1b9b6aSSam Leffler 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
17008a1b9b6aSSam Leffler #endif
1701c1225b52SSam Leffler 	if (nt != NULL) {
1702c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
17038a1b9b6aSSam Leffler 		if (ieee80211_node_dectestref(ni)) {
17048a1b9b6aSSam Leffler 			/*
1705c1225b52SSam Leffler 			 * Last reference, reclaim state.
17068a1b9b6aSSam Leffler 			 */
17078a1b9b6aSSam Leffler 			_ieee80211_free_node(ni);
1708c1225b52SSam Leffler 		} else if (ieee80211_node_refcnt(ni) == 1 &&
1709c1225b52SSam Leffler 		    nt->nt_keyixmap != NULL) {
1710c1225b52SSam Leffler 			ieee80211_keyix keyix;
1711c1225b52SSam Leffler 			/*
1712c1225b52SSam Leffler 			 * Check for a last reference in the key mapping table.
1713c1225b52SSam Leffler 			 */
1714c1225b52SSam Leffler 			keyix = ni->ni_ucastkey.wk_rxkeyix;
1715c1225b52SSam Leffler 			if (keyix < nt->nt_keyixmax &&
1716c1225b52SSam Leffler 			    nt->nt_keyixmap[keyix] == ni) {
1717b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
1718b032f27cSSam Leffler 				    IEEE80211_MSG_NODE,
1719c1225b52SSam Leffler 				    "%s: %p<%s> clear key map entry", __func__,
1720c1225b52SSam Leffler 				    ni, ether_sprintf(ni->ni_macaddr));
1721c1225b52SSam Leffler 				nt->nt_keyixmap[keyix] = NULL;
1722c1225b52SSam Leffler 				ieee80211_node_decref(ni); /* XXX needed? */
17238a1b9b6aSSam Leffler 				_ieee80211_free_node(ni);
17248a1b9b6aSSam Leffler 			}
17251a1e1d21SSam Leffler 		}
1726c1225b52SSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1727c1225b52SSam Leffler 	} else {
1728c1225b52SSam Leffler 		if (ieee80211_node_dectestref(ni))
1729c1225b52SSam Leffler 			_ieee80211_free_node(ni);
1730c1225b52SSam Leffler 	}
1731c1225b52SSam Leffler }
1732c1225b52SSam Leffler 
1733c1225b52SSam Leffler /*
1734c1225b52SSam Leffler  * Reclaim a unicast key and clear any key cache state.
1735c1225b52SSam Leffler  */
1736c1225b52SSam Leffler int
1737c1225b52SSam Leffler ieee80211_node_delucastkey(struct ieee80211_node *ni)
1738c1225b52SSam Leffler {
173937e9743aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
174037e9743aSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1741c1225b52SSam Leffler 	struct ieee80211_node *nikey;
1742c1225b52SSam Leffler 	ieee80211_keyix keyix;
1743c1225b52SSam Leffler 	int isowned, status;
1744c1225b52SSam Leffler 
1745c1225b52SSam Leffler 	/*
1746c1225b52SSam Leffler 	 * NB: We must beware of LOR here; deleting the key
1747c1225b52SSam Leffler 	 * can cause the crypto layer to block traffic updates
1748c1225b52SSam Leffler 	 * which can generate a LOR against the node table lock;
1749c1225b52SSam Leffler 	 * grab it here and stash the key index for our use below.
1750c1225b52SSam Leffler 	 *
1751c1225b52SSam Leffler 	 * Must also beware of recursion on the node table lock.
1752c1225b52SSam Leffler 	 * When called from node_cleanup we may already have
1753c1225b52SSam Leffler 	 * the node table lock held.  Unfortunately there's no
1754c1225b52SSam Leffler 	 * way to separate out this path so we must do this
1755c1225b52SSam Leffler 	 * conditionally.
1756c1225b52SSam Leffler 	 */
1757c1225b52SSam Leffler 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1758c1225b52SSam Leffler 	if (!isowned)
1759c1225b52SSam Leffler 		IEEE80211_NODE_LOCK(nt);
176037e9743aSSam Leffler 	nikey = NULL;
176137e9743aSSam Leffler 	status = 1;		/* NB: success */
1762ca92652aSSam Leffler 	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1763c1225b52SSam Leffler 		keyix = ni->ni_ucastkey.wk_rxkeyix;
176437e9743aSSam Leffler 		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1765c1225b52SSam Leffler 		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1766c1225b52SSam Leffler 			nikey = nt->nt_keyixmap[keyix];
1767c2ede4b3SMartin Blapp 			nt->nt_keyixmap[keyix] = NULL;
176837e9743aSSam Leffler 		}
176937e9743aSSam Leffler 	}
1770c1225b52SSam Leffler 	if (!isowned)
1771b032f27cSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
1772c1225b52SSam Leffler 
1773c1225b52SSam Leffler 	if (nikey != NULL) {
1774c1225b52SSam Leffler 		KASSERT(nikey == ni,
1775c1225b52SSam Leffler 			("key map out of sync, ni %p nikey %p", ni, nikey));
177637e9743aSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1777c1225b52SSam Leffler 			"%s: delete key map entry %p<%s> refcnt %d\n",
1778c1225b52SSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr),
1779c1225b52SSam Leffler 			ieee80211_node_refcnt(ni)-1);
1780c1225b52SSam Leffler 		ieee80211_free_node(ni);
1781c1225b52SSam Leffler 	}
1782c1225b52SSam Leffler 	return status;
1783c1225b52SSam Leffler }
17841a1e1d21SSam Leffler 
1785303ebc3cSSam Leffler /*
17868a1b9b6aSSam Leffler  * Reclaim a node.  If this is the last reference count then
17878a1b9b6aSSam Leffler  * do the normal free work.  Otherwise remove it from the node
17888a1b9b6aSSam Leffler  * table and mark it gone by clearing the back-reference.
17898a1b9b6aSSam Leffler  */
17908a1b9b6aSSam Leffler static void
17918a1b9b6aSSam Leffler node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
17928a1b9b6aSSam Leffler {
1793c1225b52SSam Leffler 	ieee80211_keyix keyix;
1794c1225b52SSam Leffler 
1795c1225b52SSam Leffler 	IEEE80211_NODE_LOCK_ASSERT(nt);
17968a1b9b6aSSam Leffler 
1797b032f27cSSam Leffler 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
179849a15236SSam Leffler 		"%s: remove %p<%s> from %s table, refcnt %d\n",
179949a15236SSam Leffler 		__func__, ni, ether_sprintf(ni->ni_macaddr),
180049a15236SSam Leffler 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1801c1225b52SSam Leffler 	/*
1802c1225b52SSam Leffler 	 * Clear any entry in the unicast key mapping table.
1803c1225b52SSam Leffler 	 * We need to do it here so rx lookups don't find it
1804c1225b52SSam Leffler 	 * in the mapping table even if it's not in the hash
1805c1225b52SSam Leffler 	 * table.  We cannot depend on the mapping table entry
1806c1225b52SSam Leffler 	 * being cleared because the node may not be free'd.
1807c1225b52SSam Leffler 	 */
1808c1225b52SSam Leffler 	keyix = ni->ni_ucastkey.wk_rxkeyix;
1809c1225b52SSam Leffler 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1810c1225b52SSam Leffler 	    nt->nt_keyixmap[keyix] == ni) {
1811b032f27cSSam Leffler 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
181273bac36fSSam Leffler 			"%s: %p<%s> clear key map entry %u\n",
181373bac36fSSam Leffler 			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1814c1225b52SSam Leffler 		nt->nt_keyixmap[keyix] = NULL;
1815c1225b52SSam Leffler 		ieee80211_node_decref(ni);	/* NB: don't need free */
1816c1225b52SSam Leffler 	}
18178a1b9b6aSSam Leffler 	if (!ieee80211_node_dectestref(ni)) {
18188a1b9b6aSSam Leffler 		/*
18198a1b9b6aSSam Leffler 		 * Other references are present, just remove the
18208a1b9b6aSSam Leffler 		 * node from the table so it cannot be found.  When
18218a1b9b6aSSam Leffler 		 * the references are dropped storage will be
1822acc4f7f5SSam Leffler 		 * reclaimed.
18238a1b9b6aSSam Leffler 		 */
18248a1b9b6aSSam Leffler 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
18258a1b9b6aSSam Leffler 		LIST_REMOVE(ni, ni_hash);
18268a1b9b6aSSam Leffler 		ni->ni_table = NULL;		/* clear reference */
18278a1b9b6aSSam Leffler 	} else
18288a1b9b6aSSam Leffler 		_ieee80211_free_node(ni);
18298a1b9b6aSSam Leffler }
18308a1b9b6aSSam Leffler 
1831b032f27cSSam Leffler /*
1832b032f27cSSam Leffler  * Node table support.
1833b032f27cSSam Leffler  */
1834b032f27cSSam Leffler 
1835b032f27cSSam Leffler static void
1836b032f27cSSam Leffler ieee80211_node_table_init(struct ieee80211com *ic,
1837b032f27cSSam Leffler 	struct ieee80211_node_table *nt,
1838b032f27cSSam Leffler 	const char *name, int inact, int keyixmax)
1839b032f27cSSam Leffler {
1840b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1841b032f27cSSam Leffler 
1842b032f27cSSam Leffler 	nt->nt_ic = ic;
1843b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1844b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1845b032f27cSSam Leffler 	TAILQ_INIT(&nt->nt_node);
1846b032f27cSSam Leffler 	nt->nt_name = name;
1847b032f27cSSam Leffler 	nt->nt_scangen = 1;
1848b032f27cSSam Leffler 	nt->nt_inact_init = inact;
1849b032f27cSSam Leffler 	nt->nt_keyixmax = keyixmax;
1850b032f27cSSam Leffler 	if (nt->nt_keyixmax > 0) {
1851e2126decSSam Leffler 		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1852c5abbba3SDag-Erling Smørgrav 			keyixmax * sizeof(struct ieee80211_node *),
1853b032f27cSSam Leffler 			M_80211_NODE, M_NOWAIT | M_ZERO);
1854b032f27cSSam Leffler 		if (nt->nt_keyixmap == NULL)
1855b032f27cSSam Leffler 			if_printf(ic->ic_ifp,
1856b032f27cSSam Leffler 			    "Cannot allocate key index map with %u entries\n",
1857b032f27cSSam Leffler 			    keyixmax);
1858b032f27cSSam Leffler 	} else
1859b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
1860b032f27cSSam Leffler }
1861b032f27cSSam Leffler 
1862b032f27cSSam Leffler static void
1863b032f27cSSam Leffler ieee80211_node_table_reset(struct ieee80211_node_table *nt,
1864b032f27cSSam Leffler 	struct ieee80211vap *match)
1865b032f27cSSam Leffler {
1866b032f27cSSam Leffler 	struct ieee80211_node *ni, *next;
1867b032f27cSSam Leffler 
1868b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
1869b032f27cSSam Leffler 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1870b032f27cSSam Leffler 		if (match != NULL && ni->ni_vap != match)
1871b032f27cSSam Leffler 			continue;
1872b032f27cSSam Leffler 		/* XXX can this happen?  if so need's work */
1873b032f27cSSam Leffler 		if (ni->ni_associd != 0) {
1874b032f27cSSam Leffler 			struct ieee80211vap *vap = ni->ni_vap;
1875b032f27cSSam Leffler 
1876b032f27cSSam Leffler 			if (vap->iv_auth->ia_node_leave != NULL)
1877b032f27cSSam Leffler 				vap->iv_auth->ia_node_leave(ni);
1878b032f27cSSam Leffler 			if (vap->iv_aid_bitmap != NULL)
1879b032f27cSSam Leffler 				IEEE80211_AID_CLR(vap, ni->ni_associd);
1880b032f27cSSam Leffler 		}
1881b032f27cSSam Leffler 		ni->ni_wdsvap = NULL;		/* clear reference */
18828a1b9b6aSSam Leffler 		node_reclaim(nt, ni);
18838a1b9b6aSSam Leffler 	}
1884b032f27cSSam Leffler 	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1885b032f27cSSam Leffler 		/*
1886b032f27cSSam Leffler 		 * Make a separate pass to clear references to this vap
1887b032f27cSSam Leffler 		 * held by DWDS entries.  They will not be matched above
1888b032f27cSSam Leffler 		 * because ni_vap will point to the ap vap but we still
1889b032f27cSSam Leffler 		 * need to clear ni_wdsvap when the WDS vap is destroyed
1890b032f27cSSam Leffler 		 * and/or reset.
1891b032f27cSSam Leffler 		 */
1892b032f27cSSam Leffler 		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1893b032f27cSSam Leffler 			if (ni->ni_wdsvap == match)
1894b032f27cSSam Leffler 				ni->ni_wdsvap = NULL;
1895b032f27cSSam Leffler 	}
1896b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
1897b032f27cSSam Leffler }
1898b032f27cSSam Leffler 
1899b032f27cSSam Leffler static void
1900b032f27cSSam Leffler ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1901b032f27cSSam Leffler {
1902b032f27cSSam Leffler 	ieee80211_node_table_reset(nt, NULL);
1903b032f27cSSam Leffler 	if (nt->nt_keyixmap != NULL) {
1904b032f27cSSam Leffler #ifdef DIAGNOSTIC
1905b032f27cSSam Leffler 		/* XXX verify all entries are NULL */
1906b032f27cSSam Leffler 		int i;
1907b032f27cSSam Leffler 		for (i = 0; i < nt->nt_keyixmax; i++)
1908b032f27cSSam Leffler 			if (nt->nt_keyixmap[i] != NULL)
1909b032f27cSSam Leffler 				printf("%s: %s[%u] still active\n", __func__,
1910b032f27cSSam Leffler 					nt->nt_name, i);
1911b032f27cSSam Leffler #endif
1912e2126decSSam Leffler 		free(nt->nt_keyixmap, M_80211_NODE);
1913b032f27cSSam Leffler 		nt->nt_keyixmap = NULL;
1914b032f27cSSam Leffler 	}
1915b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1916b032f27cSSam Leffler 	IEEE80211_NODE_LOCK_DESTROY(nt);
19178a1b9b6aSSam Leffler }
19188a1b9b6aSSam Leffler 
19198a1b9b6aSSam Leffler /*
19208a1b9b6aSSam Leffler  * Timeout inactive stations and do related housekeeping.
19218a1b9b6aSSam Leffler  * Note that we cannot hold the node lock while sending a
19228a1b9b6aSSam Leffler  * frame as this would lead to a LOR.  Instead we use a
19238a1b9b6aSSam Leffler  * generation number to mark nodes that we've scanned and
19248a1b9b6aSSam Leffler  * drop the lock and restart a scan if we have to time out
19258a1b9b6aSSam Leffler  * a node.  Since we are single-threaded by virtue of
1926303ebc3cSSam Leffler  * controlling the inactivity timer we can be sure this will
1927303ebc3cSSam Leffler  * process each node only once.
1928303ebc3cSSam Leffler  */
19298a1b9b6aSSam Leffler static void
1930b032f27cSSam Leffler ieee80211_timeout_stations(struct ieee80211com *ic)
19311a1e1d21SSam Leffler {
1932b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
1933b032f27cSSam Leffler 	struct ieee80211vap *vap;
1934303ebc3cSSam Leffler 	struct ieee80211_node *ni;
1935b032f27cSSam Leffler 	int gen = 0;
19361a1e1d21SSam Leffler 
1937b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK(nt);
1938a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
1939303ebc3cSSam Leffler restart:
19408a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
19418a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1942303ebc3cSSam Leffler 		if (ni->ni_scangen == gen)	/* previously handled */
1943303ebc3cSSam Leffler 			continue;
1944303ebc3cSSam Leffler 		ni->ni_scangen = gen;
19450a915fadSSam Leffler 		/*
1946ebdda46cSSam Leffler 		 * Ignore entries for which have yet to receive an
1947ebdda46cSSam Leffler 		 * authentication frame.  These are transient and
1948ebdda46cSSam Leffler 		 * will be reclaimed when the last reference to them
1949ebdda46cSSam Leffler 		 * goes away (when frame xmits complete).
1950ebdda46cSSam Leffler 		 */
1951b032f27cSSam Leffler 		vap = ni->ni_vap;
1952b032f27cSSam Leffler 		/*
1953b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
1954b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
1955b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
1956b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
1957b032f27cSSam Leffler 		 * it applies to more than timeout processing.
1958b032f27cSSam Leffler 		 */
1959b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
1960b032f27cSSam Leffler 			continue;
1961b032f27cSSam Leffler 		/* XXX can vap be NULL? */
1962b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1963b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
196444b666cdSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1965ebdda46cSSam Leffler 			continue;
1966ebdda46cSSam Leffler 		/*
19678a1b9b6aSSam Leffler 		 * Free fragment if not needed anymore
19688a1b9b6aSSam Leffler 		 * (last fragment older than 1s).
1969b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_age
19700a915fadSSam Leffler 		 */
19718a1b9b6aSSam Leffler 		if (ni->ni_rxfrag[0] != NULL &&
19728a1b9b6aSSam Leffler 		    ticks > ni->ni_rxfragstamp + hz) {
19738a1b9b6aSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
19748a1b9b6aSSam Leffler 			ni->ni_rxfrag[0] = NULL;
19758a1b9b6aSSam Leffler 		}
1976be1054edSSam Leffler 		if (ni->ni_inact > 0) {
1977c066143cSSam Leffler 			ni->ni_inact--;
1978be1054edSSam Leffler 			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1979be1054edSSam Leffler 			    "%s: inact %u inact_reload %u nrates %u",
1980be1054edSSam Leffler 			    __func__, ni->ni_inact, ni->ni_inact_reload,
1981be1054edSSam Leffler 			    ni->ni_rates.rs_nrates);
1982be1054edSSam Leffler 		}
1983ce647032SSam Leffler 		/*
1984ce647032SSam Leffler 		 * Special case ourself; we may be idle for extended periods
1985ce647032SSam Leffler 		 * of time and regardless reclaiming our state is wrong.
1986b032f27cSSam Leffler 		 * XXX run ic_node_age
1987ce647032SSam Leffler 		 */
1988b032f27cSSam Leffler 		if (ni == vap->iv_bss)
1989ce647032SSam Leffler 			continue;
1990b032f27cSSam Leffler 		if (ni->ni_associd != 0 ||
1991b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1992b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
19938a1b9b6aSSam Leffler 			/*
1994b032f27cSSam Leffler 			 * Age/drain resources held by the station.
19958a1b9b6aSSam Leffler 			 */
1996b032f27cSSam Leffler 			ic->ic_node_age(ni);
19978a1b9b6aSSam Leffler 			/*
19988a1b9b6aSSam Leffler 			 * Probe the station before time it out.  We
19998a1b9b6aSSam Leffler 			 * send a null data frame which may not be
20008a1b9b6aSSam Leffler 			 * universally supported by drivers (need it
20018a1b9b6aSSam Leffler 			 * for ps-poll support so it should be...).
200268e8e04eSSam Leffler 			 *
200368e8e04eSSam Leffler 			 * XXX don't probe the station unless we've
200468e8e04eSSam Leffler 			 *     received a frame from them (and have
200568e8e04eSSam Leffler 			 *     some idea of the rates they are capable
200668e8e04eSSam Leffler 			 *     of); this will get fixed more properly
200768e8e04eSSam Leffler 			 *     soon with better handling of the rate set.
20088a1b9b6aSSam Leffler 			 */
2009b032f27cSSam Leffler 			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2010c066143cSSam Leffler 			    (0 < ni->ni_inact &&
2011b032f27cSSam Leffler 			     ni->ni_inact <= vap->iv_inact_probe) &&
201268e8e04eSSam Leffler 			    ni->ni_rates.rs_nrates != 0) {
2013b032f27cSSam Leffler 				IEEE80211_NOTE(vap,
2014f66d97f6SSam Leffler 				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2015f66d97f6SSam Leffler 				    ni, "%s",
2016f66d97f6SSam Leffler 				    "probe station due to inactivity");
201719ad2dd7SSam Leffler 				/*
201819ad2dd7SSam Leffler 				 * Grab a reference before unlocking the table
201919ad2dd7SSam Leffler 				 * so the node cannot be reclaimed before we
202019ad2dd7SSam Leffler 				 * send the frame. ieee80211_send_nulldata
202119ad2dd7SSam Leffler 				 * understands we've done this and reclaims the
202219ad2dd7SSam Leffler 				 * ref for us as needed.
202319ad2dd7SSam Leffler 				 */
202419ad2dd7SSam Leffler 				ieee80211_ref_node(ni);
20258a1b9b6aSSam Leffler 				IEEE80211_NODE_UNLOCK(nt);
2026f62121ceSSam Leffler 				ieee80211_send_nulldata(ni);
20278a1b9b6aSSam Leffler 				/* XXX stat? */
20288a1b9b6aSSam Leffler 				goto restart;
20298a1b9b6aSSam Leffler 			}
20308a1b9b6aSSam Leffler 		}
2031b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2032c066143cSSam Leffler 		    ni->ni_inact <= 0) {
2033b032f27cSSam Leffler 			IEEE80211_NOTE(vap,
2034f66d97f6SSam Leffler 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2035f66d97f6SSam Leffler 			    "station timed out due to inactivity "
2036f66d97f6SSam Leffler 			    "(refcnt %u)", ieee80211_node_refcnt(ni));
20378a1b9b6aSSam Leffler 			/*
20388a1b9b6aSSam Leffler 			 * Send a deauthenticate frame and drop the station.
20398a1b9b6aSSam Leffler 			 * This is somewhat complicated due to reference counts
20408a1b9b6aSSam Leffler 			 * and locking.  At this point a station will typically
20418a1b9b6aSSam Leffler 			 * have a reference count of 1.  ieee80211_node_leave
20428a1b9b6aSSam Leffler 			 * will do a "free" of the node which will drop the
20438a1b9b6aSSam Leffler 			 * reference count.  But in the meantime a reference
20448a1b9b6aSSam Leffler 			 * wil be held by the deauth frame.  The actual reclaim
20458a1b9b6aSSam Leffler 			 * of the node will happen either after the tx is
20468a1b9b6aSSam Leffler 			 * completed or by ieee80211_node_leave.
20478a1b9b6aSSam Leffler 			 *
20488a1b9b6aSSam Leffler 			 * Separately we must drop the node lock before sending
204968e8e04eSSam Leffler 			 * in case the driver takes a lock, as this can result
205068e8e04eSSam Leffler 			 * in a LOR between the node lock and the driver lock.
20518a1b9b6aSSam Leffler 			 */
20525698ab1aSSam Leffler 			ieee80211_ref_node(ni);
20538a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
20548a1b9b6aSSam Leffler 			if (ni->ni_associd != 0) {
2055b032f27cSSam Leffler 				IEEE80211_SEND_MGMT(ni,
20561a1e1d21SSam Leffler 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
20571a1e1d21SSam Leffler 				    IEEE80211_REASON_AUTH_EXPIRE);
20588a1b9b6aSSam Leffler 			}
2059b032f27cSSam Leffler 			ieee80211_node_leave(ni);
20605698ab1aSSam Leffler 			ieee80211_free_node(ni);
2061b032f27cSSam Leffler 			vap->iv_stats.is_node_timeout++;
2062303ebc3cSSam Leffler 			goto restart;
2063303ebc3cSSam Leffler 		}
20641a1e1d21SSam Leffler 	}
20658a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
20668a1b9b6aSSam Leffler 
2067b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_UNLOCK(nt);
206868e8e04eSSam Leffler }
20698a1b9b6aSSam Leffler 
2070b032f27cSSam Leffler /*
2071b032f27cSSam Leffler  * Aggressively reclaim resources.  This should be used
2072b032f27cSSam Leffler  * only in a critical situation to reclaim mbuf resources.
2073b032f27cSSam Leffler  */
2074b032f27cSSam Leffler void
2075b032f27cSSam Leffler ieee80211_drain(struct ieee80211com *ic)
2076b032f27cSSam Leffler {
2077b032f27cSSam Leffler 	struct ieee80211_node_table *nt = &ic->ic_sta;
2078b032f27cSSam Leffler 	struct ieee80211vap *vap;
2079b032f27cSSam Leffler 	struct ieee80211_node *ni;
2080b032f27cSSam Leffler 
2081b032f27cSSam Leffler 	IEEE80211_NODE_LOCK(nt);
2082b032f27cSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2083b032f27cSSam Leffler 		/*
2084b032f27cSSam Leffler 		 * Ignore entries for which have yet to receive an
2085b032f27cSSam Leffler 		 * authentication frame.  These are transient and
2086b032f27cSSam Leffler 		 * will be reclaimed when the last reference to them
2087b032f27cSSam Leffler 		 * goes away (when frame xmits complete).
2088b032f27cSSam Leffler 		 */
2089b032f27cSSam Leffler 		vap = ni->ni_vap;
2090b032f27cSSam Leffler 		/*
2091b032f27cSSam Leffler 		 * Only process stations when in RUN state.  This
2092b032f27cSSam Leffler 		 * insures, for example, that we don't timeout an
2093b032f27cSSam Leffler 		 * inactive station during CAC.  Note that CSA state
2094b032f27cSSam Leffler 		 * is actually handled in ieee80211_node_timeout as
2095b032f27cSSam Leffler 		 * it applies to more than timeout processing.
2096b032f27cSSam Leffler 		 */
2097b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
2098b032f27cSSam Leffler 			continue;
2099b032f27cSSam Leffler 		/* XXX can vap be NULL? */
2100b032f27cSSam Leffler 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2101b032f27cSSam Leffler 		     vap->iv_opmode == IEEE80211_M_STA) &&
2102b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2103b032f27cSSam Leffler 			continue;
2104b032f27cSSam Leffler 		/*
2105b032f27cSSam Leffler 		 * Free fragments.
2106b032f27cSSam Leffler 		 * XXX doesn't belong here, move to node_drain
2107b032f27cSSam Leffler 		 */
2108b032f27cSSam Leffler 		if (ni->ni_rxfrag[0] != NULL) {
2109b032f27cSSam Leffler 			m_freem(ni->ni_rxfrag[0]);
2110b032f27cSSam Leffler 			ni->ni_rxfrag[0] = NULL;
2111b032f27cSSam Leffler 		}
2112b032f27cSSam Leffler 		/*
2113b032f27cSSam Leffler 		 * Drain resources held by the station.
2114b032f27cSSam Leffler 		 */
2115b032f27cSSam Leffler 		ic->ic_node_drain(ni);
2116b032f27cSSam Leffler 	}
2117b032f27cSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
2118b032f27cSSam Leffler }
2119b032f27cSSam Leffler 
2120b032f27cSSam Leffler /*
2121b032f27cSSam Leffler  * Per-ieee80211com inactivity timer callback.
2122b032f27cSSam Leffler  */
212368e8e04eSSam Leffler void
212468e8e04eSSam Leffler ieee80211_node_timeout(void *arg)
212568e8e04eSSam Leffler {
212668e8e04eSSam Leffler 	struct ieee80211com *ic = arg;
212768e8e04eSSam Leffler 
2128b032f27cSSam Leffler 	/*
2129b032f27cSSam Leffler 	 * Defer timeout processing if a channel switch is pending.
2130b032f27cSSam Leffler 	 * We typically need to be mute so not doing things that
2131b032f27cSSam Leffler 	 * might generate frames is good to handle in one place.
2132b032f27cSSam Leffler 	 * Supressing the station timeout processing may extend the
2133b032f27cSSam Leffler 	 * lifetime of inactive stations (by not decrementing their
2134b032f27cSSam Leffler 	 * idle counters) but this should be ok unless the CSA is
2135b032f27cSSam Leffler 	 * active for an unusually long time.
2136b032f27cSSam Leffler 	 */
2137b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
213868e8e04eSSam Leffler 		ieee80211_scan_timeout(ic);
2139b032f27cSSam Leffler 		ieee80211_timeout_stations(ic);
21405b16c28cSSam Leffler 		ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
214168e8e04eSSam Leffler 
2142b105a069SSam Leffler 		IEEE80211_LOCK(ic);
2143b105a069SSam Leffler 		ieee80211_erp_timeout(ic);
21441b6167d2SSam Leffler 		ieee80211_ht_timeout(ic);
2145b105a069SSam Leffler 		IEEE80211_UNLOCK(ic);
2146b032f27cSSam Leffler 	}
214768e8e04eSSam Leffler 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
214868e8e04eSSam Leffler 		ieee80211_node_timeout, ic);
21491a1e1d21SSam Leffler }
21501a1e1d21SSam Leffler 
21511a1e1d21SSam Leffler void
2152b032f27cSSam Leffler ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2153b032f27cSSam Leffler 	ieee80211_iter_func *f, void *arg)
21541a1e1d21SSam Leffler {
21551a1e1d21SSam Leffler 	struct ieee80211_node *ni;
21568a1b9b6aSSam Leffler 	u_int gen;
21571a1e1d21SSam Leffler 
2158b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_LOCK(nt);
2159a1a50502SSam Leffler 	gen = ++nt->nt_scangen;
21608a1b9b6aSSam Leffler restart:
21618a1b9b6aSSam Leffler 	IEEE80211_NODE_LOCK(nt);
21628a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
21638a1b9b6aSSam Leffler 		if (ni->ni_scangen != gen) {
21648a1b9b6aSSam Leffler 			ni->ni_scangen = gen;
21658a1b9b6aSSam Leffler 			(void) ieee80211_ref_node(ni);
21668a1b9b6aSSam Leffler 			IEEE80211_NODE_UNLOCK(nt);
21671a1e1d21SSam Leffler 			(*f)(arg, ni);
21688a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
21698a1b9b6aSSam Leffler 			goto restart;
21708a1b9b6aSSam Leffler 		}
21718a1b9b6aSSam Leffler 	}
21728a1b9b6aSSam Leffler 	IEEE80211_NODE_UNLOCK(nt);
21738a1b9b6aSSam Leffler 
2174b032f27cSSam Leffler 	IEEE80211_NODE_ITERATE_UNLOCK(nt);
21758a1b9b6aSSam Leffler }
21768a1b9b6aSSam Leffler 
21778a1b9b6aSSam Leffler void
21788a1b9b6aSSam Leffler ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
21798a1b9b6aSSam Leffler {
21808a1b9b6aSSam Leffler 	printf("0x%p: mac %s refcnt %d\n", ni,
21818a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
21828a1b9b6aSSam Leffler 	printf("\tscangen %u authmode %u flags 0x%x\n",
21838a1b9b6aSSam Leffler 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
21848a1b9b6aSSam Leffler 	printf("\tassocid 0x%x txpower %u vlan %u\n",
21858a1b9b6aSSam Leffler 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
21868a1b9b6aSSam Leffler 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2187801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2188801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2189801df4a5SSam Leffler 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
21908a1b9b6aSSam Leffler 		ni->ni_rxfragstamp);
21915463c4a4SSam Leffler 	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
21925463c4a4SSam Leffler 		node_getrssi(ni), ni->ni_noise,
219368e8e04eSSam Leffler 		ni->ni_intval, ni->ni_capinfo);
21948a1b9b6aSSam Leffler 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
21958a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_bssid),
21968a1b9b6aSSam Leffler 		ni->ni_esslen, ni->ni_essid,
21978a1b9b6aSSam Leffler 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2198be1054edSSam Leffler 	printf("\tinact %u inact_reload %u txrate %u\n",
2199be1054edSSam Leffler 		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
220068e8e04eSSam Leffler 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
220168e8e04eSSam Leffler 		ni->ni_htcap, ni->ni_htparam,
220268e8e04eSSam Leffler 		ni->ni_htctlchan, ni->ni_ht2ndchan);
220368e8e04eSSam Leffler 	printf("\thtopmode %x htstbc %x chw %u\n",
220468e8e04eSSam Leffler 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
22058a1b9b6aSSam Leffler }
22068a1b9b6aSSam Leffler 
22078a1b9b6aSSam Leffler void
22088a1b9b6aSSam Leffler ieee80211_dump_nodes(struct ieee80211_node_table *nt)
22098a1b9b6aSSam Leffler {
22108a1b9b6aSSam Leffler 	ieee80211_iterate_nodes(nt,
22118a1b9b6aSSam Leffler 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
22128a1b9b6aSSam Leffler }
22138a1b9b6aSSam Leffler 
22142dc4d8dcSSam Leffler static void
22152dc4d8dcSSam Leffler ieee80211_notify_erp_locked(struct ieee80211com *ic)
2216b105a069SSam Leffler {
2217b032f27cSSam Leffler 	struct ieee80211vap *vap;
2218b032f27cSSam Leffler 
2219b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2220b032f27cSSam Leffler 
2221b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2222b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2223b032f27cSSam Leffler 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2224b105a069SSam Leffler }
2225b105a069SSam Leffler 
22262dc4d8dcSSam Leffler void
22272dc4d8dcSSam Leffler ieee80211_notify_erp(struct ieee80211com *ic)
22282dc4d8dcSSam Leffler {
22292dc4d8dcSSam Leffler 	IEEE80211_LOCK(ic);
22302dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
22312dc4d8dcSSam Leffler 	IEEE80211_UNLOCK(ic);
22322dc4d8dcSSam Leffler }
22332dc4d8dcSSam Leffler 
22348a1b9b6aSSam Leffler /*
22358a1b9b6aSSam Leffler  * Handle a station joining an 11g network.
22368a1b9b6aSSam Leffler  */
22378a1b9b6aSSam Leffler static void
2238b032f27cSSam Leffler ieee80211_node_join_11g(struct ieee80211_node *ni)
22398a1b9b6aSSam Leffler {
2240b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
22418a1b9b6aSSam Leffler 
22421b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
22431b6167d2SSam Leffler 
22448a1b9b6aSSam Leffler 	/*
22458a1b9b6aSSam Leffler 	 * Station isn't capable of short slot time.  Bump
22468a1b9b6aSSam Leffler 	 * the count of long slot time stations and disable
22478a1b9b6aSSam Leffler 	 * use of short slot time.  Note that the actual switch
22488a1b9b6aSSam Leffler 	 * over to long slot time use may not occur until the
22498a1b9b6aSSam Leffler 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
22508a1b9b6aSSam Leffler 	 */
22518a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
22528a1b9b6aSSam Leffler 		ic->ic_longslotsta++;
2253b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2254b105a069SSam Leffler 		    "station needs long slot time, count %d",
2255b105a069SSam Leffler 		    ic->ic_longslotsta);
22568a1b9b6aSSam Leffler 		/* XXX vap's w/ conflicting needs won't work */
225768e8e04eSSam Leffler 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
225868e8e04eSSam Leffler 			/*
225968e8e04eSSam Leffler 			 * Don't force slot time when switched to turbo
226068e8e04eSSam Leffler 			 * mode as non-ERP stations won't be present; this
226168e8e04eSSam Leffler 			 * need only be done when on the normal G channel.
226268e8e04eSSam Leffler 			 */
22638a1b9b6aSSam Leffler 			ieee80211_set_shortslottime(ic, 0);
22648a1b9b6aSSam Leffler 		}
226568e8e04eSSam Leffler 	}
22668a1b9b6aSSam Leffler 	/*
22678a1b9b6aSSam Leffler 	 * If the new station is not an ERP station
22688a1b9b6aSSam Leffler 	 * then bump the counter and enable protection
22698a1b9b6aSSam Leffler 	 * if configured.
22708a1b9b6aSSam Leffler 	 */
2271b032f27cSSam Leffler 	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
22728a1b9b6aSSam Leffler 		ic->ic_nonerpsta++;
2273b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2274b105a069SSam Leffler 		    "station is !ERP, %d non-ERP stations associated",
2275b105a069SSam Leffler 		    ic->ic_nonerpsta);
22768a1b9b6aSSam Leffler 		/*
22778a1b9b6aSSam Leffler 		 * If station does not support short preamble
22788a1b9b6aSSam Leffler 		 * then we must enable use of Barker preamble.
22798a1b9b6aSSam Leffler 		 */
22808a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2281b032f27cSSam Leffler 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2282b105a069SSam Leffler 			    "%s", "station needs long preamble");
22838a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_USEBARKER;
22848a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
22858a1b9b6aSSam Leffler 		}
2286b105a069SSam Leffler 		/*
2287b032f27cSSam Leffler 		 * If protection is configured and this is the first
2288b032f27cSSam Leffler 		 * indication we should use protection, enable it.
2289b105a069SSam Leffler 		 */
2290b105a069SSam Leffler 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2291b105a069SSam Leffler 		    ic->ic_nonerpsta == 1 &&
2292b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2293b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2294b105a069SSam Leffler 			    "%s: enable use of protection\n", __func__);
2295b105a069SSam Leffler 			ic->ic_flags |= IEEE80211_F_USEPROT;
22962dc4d8dcSSam Leffler 			ieee80211_notify_erp_locked(ic);
2297b105a069SSam Leffler 		}
22988a1b9b6aSSam Leffler 	} else
22998a1b9b6aSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ERP;
23008a1b9b6aSSam Leffler }
23018a1b9b6aSSam Leffler 
23028a1b9b6aSSam Leffler void
2303b032f27cSSam Leffler ieee80211_node_join(struct ieee80211_node *ni, int resp)
23048a1b9b6aSSam Leffler {
2305b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2306b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
23078a1b9b6aSSam Leffler 	int newassoc;
23088a1b9b6aSSam Leffler 
23098a1b9b6aSSam Leffler 	if (ni->ni_associd == 0) {
231068e8e04eSSam Leffler 		uint16_t aid;
23118a1b9b6aSSam Leffler 
2312b032f27cSSam Leffler 		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
23138a1b9b6aSSam Leffler 		/*
23148a1b9b6aSSam Leffler 		 * It would be good to search the bitmap
23158a1b9b6aSSam Leffler 		 * more efficiently, but this will do for now.
23168a1b9b6aSSam Leffler 		 */
2317b032f27cSSam Leffler 		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2318b032f27cSSam Leffler 			if (!IEEE80211_AID_ISSET(vap, aid))
23198a1b9b6aSSam Leffler 				break;
23208a1b9b6aSSam Leffler 		}
2321b032f27cSSam Leffler 		if (aid >= vap->iv_max_aid) {
232213f91245SSam Leffler 			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2323b032f27cSSam Leffler 			ieee80211_node_leave(ni);
23248a1b9b6aSSam Leffler 			return;
23258a1b9b6aSSam Leffler 		}
23268a1b9b6aSSam Leffler 		ni->ni_associd = aid | 0xc000;
23271b6167d2SSam Leffler 		ni->ni_jointime = time_uptime;
2328b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
2329b032f27cSSam Leffler 		IEEE80211_AID_SET(vap, ni->ni_associd);
2330b032f27cSSam Leffler 		vap->iv_sta_assoc++;
23318a1b9b6aSSam Leffler 		ic->ic_sta_assoc++;
23321b6167d2SSam Leffler 
23331b6167d2SSam Leffler 		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
23341b6167d2SSam Leffler 			ieee80211_ht_node_join(ni);
233568e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
233668e8e04eSSam Leffler 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2337b032f27cSSam Leffler 			ieee80211_node_join_11g(ni);
23381b6167d2SSam Leffler 		IEEE80211_UNLOCK(ic);
23391b6167d2SSam Leffler 
23401b6167d2SSam Leffler 		newassoc = 1;
23418a1b9b6aSSam Leffler 	} else
23428a1b9b6aSSam Leffler 		newassoc = 0;
23438a1b9b6aSSam Leffler 
2344b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
234544f7a6edSSam Leffler 	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2346b8fcf546SSam Leffler 	    IEEE80211_NODE_AID(ni),
2347b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2348b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2349b8fcf546SSam Leffler 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
235068e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
235168e8e04eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_HT ?
23520f52b1c4SSam Leffler 		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
23531b6167d2SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
23548c070d69SSam Leffler 	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
23558c070d69SSam Leffler 	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
235644f7a6edSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2357b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
235868e8e04eSSam Leffler 		", fast-frames" : "",
2359b032f27cSSam Leffler 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
236068e8e04eSSam Leffler 		", turbo" : ""
2361b8fcf546SSam Leffler 	);
23628a1b9b6aSSam Leffler 
2363d77148fbSSam Leffler 	ieee80211_node_setuptxparms(ni);
236449d2c137SBernhard Schmidt 	ieee80211_ratectl_node_init(ni);
23658a1b9b6aSSam Leffler 	/* give driver a chance to setup state like ni_txrate */
2366736b3dc3SSam Leffler 	if (ic->ic_newassoc != NULL)
2367e9962332SSam Leffler 		ic->ic_newassoc(ni, newassoc);
2368b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
23698a1b9b6aSSam Leffler 	/* tell the authenticator about new station */
2370b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_join != NULL)
2371b032f27cSSam Leffler 		vap->iv_auth->ia_node_join(ni);
2372b032f27cSSam Leffler 	ieee80211_notify_node_join(ni,
2373ce8977dfSSam Leffler 	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
23748a1b9b6aSSam Leffler }
23758a1b9b6aSSam Leffler 
2376b105a069SSam Leffler static void
2377b105a069SSam Leffler disable_protection(struct ieee80211com *ic)
2378b105a069SSam Leffler {
2379b105a069SSam Leffler 	KASSERT(ic->ic_nonerpsta == 0 &&
2380b105a069SSam Leffler 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2381b105a069SSam Leffler 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2382b105a069SSam Leffler 	   ic->ic_flags_ext));
2383b105a069SSam Leffler 
2384b105a069SSam Leffler 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2385b105a069SSam Leffler 	/* XXX verify mode? */
2386b105a069SSam Leffler 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2387b105a069SSam Leffler 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2388b105a069SSam Leffler 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2389b105a069SSam Leffler 	}
23902dc4d8dcSSam Leffler 	ieee80211_notify_erp_locked(ic);
2391b105a069SSam Leffler }
2392b105a069SSam Leffler 
23938a1b9b6aSSam Leffler /*
23948a1b9b6aSSam Leffler  * Handle a station leaving an 11g network.
23958a1b9b6aSSam Leffler  */
23968a1b9b6aSSam Leffler static void
2397b032f27cSSam Leffler ieee80211_node_leave_11g(struct ieee80211_node *ni)
23988a1b9b6aSSam Leffler {
2399b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
24008a1b9b6aSSam Leffler 
24011b6167d2SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
24021b6167d2SSam Leffler 
240368e8e04eSSam Leffler 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2404b032f27cSSam Leffler 	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2405b032f27cSSam Leffler 	      ic->ic_bsschan->ic_flags));
24068a1b9b6aSSam Leffler 
24078a1b9b6aSSam Leffler 	/*
24088a1b9b6aSSam Leffler 	 * If a long slot station do the slot time bookkeeping.
24098a1b9b6aSSam Leffler 	 */
24108a1b9b6aSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
24118a1b9b6aSSam Leffler 		KASSERT(ic->ic_longslotsta > 0,
24128a1b9b6aSSam Leffler 		    ("bogus long slot station count %d", ic->ic_longslotsta));
24138a1b9b6aSSam Leffler 		ic->ic_longslotsta--;
2414b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2415b105a069SSam Leffler 		    "long slot time station leaves, count now %d",
2416b105a069SSam Leffler 		    ic->ic_longslotsta);
24178a1b9b6aSSam Leffler 		if (ic->ic_longslotsta == 0) {
24188a1b9b6aSSam Leffler 			/*
24198a1b9b6aSSam Leffler 			 * Re-enable use of short slot time if supported
24208a1b9b6aSSam Leffler 			 * and not operating in IBSS mode (per spec).
24218a1b9b6aSSam Leffler 			 */
24228a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
24238a1b9b6aSSam Leffler 			    ic->ic_opmode != IEEE80211_M_IBSS) {
2424b032f27cSSam Leffler 				IEEE80211_DPRINTF(ni->ni_vap,
2425b032f27cSSam Leffler 				    IEEE80211_MSG_ASSOC,
24268a1b9b6aSSam Leffler 				    "%s: re-enable use of short slot time\n",
24278a1b9b6aSSam Leffler 				    __func__);
24288a1b9b6aSSam Leffler 				ieee80211_set_shortslottime(ic, 1);
24298a1b9b6aSSam Leffler 			}
24308a1b9b6aSSam Leffler 		}
24318a1b9b6aSSam Leffler 	}
24328a1b9b6aSSam Leffler 	/*
24338a1b9b6aSSam Leffler 	 * If a non-ERP station do the protection-related bookkeeping.
24348a1b9b6aSSam Leffler 	 */
24358a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
24368a1b9b6aSSam Leffler 		KASSERT(ic->ic_nonerpsta > 0,
24378a1b9b6aSSam Leffler 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
24388a1b9b6aSSam Leffler 		ic->ic_nonerpsta--;
2439b032f27cSSam Leffler 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2440b105a069SSam Leffler 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2441b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2442b105a069SSam Leffler 			" (non-ERP sta present)" : "");
2443b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0 &&
2444b105a069SSam Leffler 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2445b032f27cSSam Leffler 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
24468a1b9b6aSSam Leffler 				"%s: disable use of protection\n", __func__);
2447b105a069SSam Leffler 			disable_protection(ic);
2448b105a069SSam Leffler 		}
2449b105a069SSam Leffler 	}
2450b105a069SSam Leffler }
2451b105a069SSam Leffler 
2452b105a069SSam Leffler /*
2453b105a069SSam Leffler  * Time out presence of an overlapping bss with non-ERP
2454b105a069SSam Leffler  * stations.  When operating in hostap mode we listen for
2455b105a069SSam Leffler  * beacons from other stations and if we identify a non-ERP
2456b105a069SSam Leffler  * station is present we enable protection.  To identify
2457b105a069SSam Leffler  * when all non-ERP stations are gone we time out this
2458b105a069SSam Leffler  * condition.
2459b105a069SSam Leffler  */
2460b105a069SSam Leffler static void
2461b105a069SSam Leffler ieee80211_erp_timeout(struct ieee80211com *ic)
2462b105a069SSam Leffler {
2463b105a069SSam Leffler 
2464b105a069SSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
2465b105a069SSam Leffler 
2466b105a069SSam Leffler 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2467b105a069SSam Leffler 	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2468b032f27cSSam Leffler #if 0
2469b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2470b032f27cSSam Leffler 		    "%s", "age out non-ERP sta present on channel");
2471b032f27cSSam Leffler #endif
2472b105a069SSam Leffler 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2473b105a069SSam Leffler 		if (ic->ic_nonerpsta == 0)
2474b105a069SSam Leffler 			disable_protection(ic);
24758a1b9b6aSSam Leffler 	}
24768a1b9b6aSSam Leffler }
24778a1b9b6aSSam Leffler 
24788a1b9b6aSSam Leffler /*
24798a1b9b6aSSam Leffler  * Handle bookkeeping for station deauthentication/disassociation
24808a1b9b6aSSam Leffler  * when operating as an ap.
24818a1b9b6aSSam Leffler  */
24828a1b9b6aSSam Leffler void
2483b032f27cSSam Leffler ieee80211_node_leave(struct ieee80211_node *ni)
24848a1b9b6aSSam Leffler {
2485b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2486b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
248744acc00dSSam Leffler 	struct ieee80211_node_table *nt = ni->ni_table;
24888a1b9b6aSSam Leffler 
2489b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2490b105a069SSam Leffler 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
24918a1b9b6aSSam Leffler 
2492b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2493b032f27cSSam Leffler 		("unexpected operating mode %u", vap->iv_opmode));
24948a1b9b6aSSam Leffler 	/*
24958a1b9b6aSSam Leffler 	 * If node wasn't previously associated all
24968a1b9b6aSSam Leffler 	 * we need to do is reclaim the reference.
24978a1b9b6aSSam Leffler 	 */
24988a1b9b6aSSam Leffler 	/* XXX ibss mode bypasses 11g and notification */
24998a1b9b6aSSam Leffler 	if (ni->ni_associd == 0)
25008a1b9b6aSSam Leffler 		goto done;
25018a1b9b6aSSam Leffler 	/*
25028a1b9b6aSSam Leffler 	 * Tell the authenticator the station is leaving.
25038a1b9b6aSSam Leffler 	 * Note that we must do this before yanking the
25048a1b9b6aSSam Leffler 	 * association id as the authenticator uses the
25058a1b9b6aSSam Leffler 	 * associd to locate it's state block.
25068a1b9b6aSSam Leffler 	 */
2507b032f27cSSam Leffler 	if (vap->iv_auth->ia_node_leave != NULL)
2508b032f27cSSam Leffler 		vap->iv_auth->ia_node_leave(ni);
25091b6167d2SSam Leffler 
25101b6167d2SSam Leffler 	IEEE80211_LOCK(ic);
2511b032f27cSSam Leffler 	IEEE80211_AID_CLR(vap, ni->ni_associd);
25128a1b9b6aSSam Leffler 	ni->ni_associd = 0;
2513b032f27cSSam Leffler 	vap->iv_sta_assoc--;
25148a1b9b6aSSam Leffler 	ic->ic_sta_assoc--;
25158a1b9b6aSSam Leffler 
25161b6167d2SSam Leffler 	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
25171b6167d2SSam Leffler 		ieee80211_ht_node_leave(ni);
251868e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
251968e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2520b032f27cSSam Leffler 		ieee80211_node_leave_11g(ni);
25211b6167d2SSam Leffler 	IEEE80211_UNLOCK(ic);
25228a1b9b6aSSam Leffler 	/*
25238a1b9b6aSSam Leffler 	 * Cleanup station state.  In particular clear various
25248a1b9b6aSSam Leffler 	 * state that might otherwise be reused if the node
25258a1b9b6aSSam Leffler 	 * is reused before the reference count goes to zero
25268a1b9b6aSSam Leffler 	 * (and memory is reclaimed).
25278a1b9b6aSSam Leffler 	 */
2528b032f27cSSam Leffler 	ieee80211_sta_leave(ni);
25298a1b9b6aSSam Leffler done:
253044acc00dSSam Leffler 	/*
253144acc00dSSam Leffler 	 * Remove the node from any table it's recorded in and
253244acc00dSSam Leffler 	 * drop the caller's reference.  Removal from the table
253344acc00dSSam Leffler 	 * is important to insure the node is not reprocessed
253444acc00dSSam Leffler 	 * for inactivity.
253544acc00dSSam Leffler 	 */
253644acc00dSSam Leffler 	if (nt != NULL) {
253744acc00dSSam Leffler 		IEEE80211_NODE_LOCK(nt);
253844acc00dSSam Leffler 		node_reclaim(nt, ni);
253944acc00dSSam Leffler 		IEEE80211_NODE_UNLOCK(nt);
254044acc00dSSam Leffler 	} else
25418a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
25428a1b9b6aSSam Leffler }
25438a1b9b6aSSam Leffler 
2544b032f27cSSam Leffler struct rssiinfo {
2545b032f27cSSam Leffler 	struct ieee80211vap *vap;
2546b032f27cSSam Leffler 	int	rssi_samples;
2547b032f27cSSam Leffler 	uint32_t rssi_total;
2548b032f27cSSam Leffler };
2549b032f27cSSam Leffler 
2550b032f27cSSam Leffler static void
2551b032f27cSSam Leffler get_hostap_rssi(void *arg, struct ieee80211_node *ni)
2552b032f27cSSam Leffler {
2553b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2554b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2555b032f27cSSam Leffler 	int8_t rssi;
2556b032f27cSSam Leffler 
2557b032f27cSSam Leffler 	if (info->vap != vap)
2558b032f27cSSam Leffler 		return;
2559b032f27cSSam Leffler 	/* only associated stations */
2560b032f27cSSam Leffler 	if (ni->ni_associd == 0)
2561b032f27cSSam Leffler 		return;
2562b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
2563b032f27cSSam Leffler 	if (rssi != 0) {
2564b032f27cSSam Leffler 		info->rssi_samples++;
2565b032f27cSSam Leffler 		info->rssi_total += rssi;
2566b032f27cSSam Leffler 	}
2567b032f27cSSam Leffler }
2568b032f27cSSam Leffler 
2569b032f27cSSam Leffler static void
2570b032f27cSSam Leffler get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2571b032f27cSSam Leffler {
2572b032f27cSSam Leffler 	struct rssiinfo *info = arg;
2573b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2574b032f27cSSam Leffler 	int8_t rssi;
2575b032f27cSSam Leffler 
2576b032f27cSSam Leffler 	if (info->vap != vap)
2577b032f27cSSam Leffler 		return;
2578b032f27cSSam Leffler 	/* only neighbors */
2579b032f27cSSam Leffler 	/* XXX check bssid */
2580b032f27cSSam Leffler 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2581b032f27cSSam Leffler 		return;
2582b032f27cSSam Leffler 	rssi = vap->iv_ic->ic_node_getrssi(ni);
2583b032f27cSSam Leffler 	if (rssi != 0) {
2584b032f27cSSam Leffler 		info->rssi_samples++;
2585b032f27cSSam Leffler 		info->rssi_total += rssi;
2586b032f27cSSam Leffler 	}
2587b032f27cSSam Leffler }
2588b032f27cSSam Leffler 
258959aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
259059aa14a9SRui Paulo static void
259159aa14a9SRui Paulo get_mesh_rssi(void *arg, struct ieee80211_node *ni)
259259aa14a9SRui Paulo {
259359aa14a9SRui Paulo 	struct rssiinfo *info = arg;
259459aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
259559aa14a9SRui Paulo 	int8_t rssi;
259659aa14a9SRui Paulo 
259759aa14a9SRui Paulo 	if (info->vap != vap)
259859aa14a9SRui Paulo 		return;
259959aa14a9SRui Paulo 	/* only neighbors that peered successfully */
260059aa14a9SRui Paulo 	if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
260159aa14a9SRui Paulo 		return;
260259aa14a9SRui Paulo 	rssi = vap->iv_ic->ic_node_getrssi(ni);
260359aa14a9SRui Paulo 	if (rssi != 0) {
260459aa14a9SRui Paulo 		info->rssi_samples++;
260559aa14a9SRui Paulo 		info->rssi_total += rssi;
260659aa14a9SRui Paulo 	}
260759aa14a9SRui Paulo }
260859aa14a9SRui Paulo #endif /* IEEE80211_SUPPORT_MESH */
260959aa14a9SRui Paulo 
261068e8e04eSSam Leffler int8_t
2611b032f27cSSam Leffler ieee80211_getrssi(struct ieee80211vap *vap)
26128a1b9b6aSSam Leffler {
26138a1b9b6aSSam Leffler #define	NZ(x)	((x) == 0 ? 1 : (x))
2614b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2615b032f27cSSam Leffler 	struct rssiinfo info;
26168a1b9b6aSSam Leffler 
2617b032f27cSSam Leffler 	info.rssi_total = 0;
2618b032f27cSSam Leffler 	info.rssi_samples = 0;
2619b032f27cSSam Leffler 	info.vap = vap;
2620b032f27cSSam Leffler 	switch (vap->iv_opmode) {
26218a1b9b6aSSam Leffler 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
26228a1b9b6aSSam Leffler 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2623b032f27cSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2624b032f27cSSam Leffler 		break;
26258a1b9b6aSSam Leffler 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2626b032f27cSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
26278a1b9b6aSSam Leffler 		break;
262859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
262959aa14a9SRui Paulo 	case IEEE80211_M_MBSS:		/* average of all mesh neighbors */
263059aa14a9SRui Paulo 		ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
263159aa14a9SRui Paulo 		break;
263259aa14a9SRui Paulo #endif
26338a1b9b6aSSam Leffler 	case IEEE80211_M_MONITOR:	/* XXX */
26348a1b9b6aSSam Leffler 	case IEEE80211_M_STA:		/* use stats from associated ap */
26358a1b9b6aSSam Leffler 	default:
2636b032f27cSSam Leffler 		if (vap->iv_bss != NULL)
2637b032f27cSSam Leffler 			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2638b032f27cSSam Leffler 		info.rssi_samples = 1;
26398a1b9b6aSSam Leffler 		break;
26408a1b9b6aSSam Leffler 	}
2641b032f27cSSam Leffler 	return info.rssi_total / NZ(info.rssi_samples);
26428a1b9b6aSSam Leffler #undef NZ
26438a1b9b6aSSam Leffler }
26448a1b9b6aSSam Leffler 
264568e8e04eSSam Leffler void
2646b032f27cSSam Leffler ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
26478a1b9b6aSSam Leffler {
26488a1b9b6aSSam Leffler 
2649b032f27cSSam Leffler 	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
265068e8e04eSSam Leffler 		return;
2651b032f27cSSam Leffler 	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
265268e8e04eSSam Leffler 	/* for non-station mode return avg'd rssi accounting */
2653b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA)
2654b032f27cSSam Leffler 		*rssi = ieee80211_getrssi(vap);
26558a1b9b6aSSam Leffler }
2656