ieee80211_node.c revision 190532
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 190532 2009-03-29 21:17:08Z sam $");
29116742Ssam
30178354Ssam#include "opt_wlan.h"
31178354Ssam
32116742Ssam#include <sys/param.h>
33116742Ssam#include <sys/systm.h>
34116742Ssam#include <sys/mbuf.h>
35116742Ssam#include <sys/malloc.h>
36116742Ssam#include <sys/kernel.h>
37138568Ssam
38116742Ssam#include <sys/socket.h>
39116742Ssam
40116742Ssam#include <net/if.h>
41116742Ssam#include <net/if_media.h>
42116742Ssam#include <net/ethernet.h>
43116742Ssam
44116742Ssam#include <net80211/ieee80211_var.h>
45178354Ssam#include <net80211/ieee80211_input.h>
46190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
47190391Ssam#include <net80211/ieee80211_superg.h>
48190391Ssam#endif
49186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
50186904Ssam#include <net80211/ieee80211_tdma.h>
51186904Ssam#endif
52178354Ssam#include <net80211/ieee80211_wds.h>
53116742Ssam
54116742Ssam#include <net/bpf.h>
55116742Ssam
56147221Ssam/*
57147221Ssam * Association id's are managed with a bit vector.
58147221Ssam */
59178354Ssam#define	IEEE80211_AID_SET(_vap, b) \
60178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
61178354Ssam		(1 << (IEEE80211_AID(b) % 32)))
62178354Ssam#define	IEEE80211_AID_CLR(_vap, b) \
63178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
64178354Ssam		~(1 << (IEEE80211_AID(b) % 32)))
65178354Ssam#define	IEEE80211_AID_ISSET(_vap, b) \
66178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
67147221Ssam
68159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
69159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
70159139Sdds#else
71159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
72159139Sdds#endif
73159139Sdds
74170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
75170530Ssam
76179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *,
77179643Ssam	const uint8_t [IEEE80211_ADDR_LEN]);
78138568Ssamstatic void node_cleanup(struct ieee80211_node *);
79138568Ssamstatic void node_free(struct ieee80211_node *);
80178354Ssamstatic void node_age(struct ieee80211_node *);
81170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
82170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
83178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *,
84178354Ssam	struct ieee80211_mimo_info *);
85116742Ssam
86138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
87120104Ssam
88138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
89148863Ssam	struct ieee80211_node_table *nt, const char *name,
90170530Ssam	int inact, int keymaxix);
91178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *,
92178354Ssam	struct ieee80211vap *);
93138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
94172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
95138568Ssam
96127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
97178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
98120481Ssam
99116742Ssamvoid
100138568Ssamieee80211_node_attach(struct ieee80211com *ic)
101116742Ssam{
102178354Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
103178354Ssam		IEEE80211_INACT_INIT, ic->ic_max_keyix);
104178354Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
105178354Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
106178354Ssam		ieee80211_node_timeout, ic);
107116742Ssam
108138568Ssam	ic->ic_node_alloc = node_alloc;
109138568Ssam	ic->ic_node_free = node_free;
110138568Ssam	ic->ic_node_cleanup = node_cleanup;
111178354Ssam	ic->ic_node_age = node_age;
112178354Ssam	ic->ic_node_drain = node_age;		/* NB: same as age */
113138568Ssam	ic->ic_node_getrssi = node_getrssi;
114170530Ssam	ic->ic_node_getsignal = node_getsignal;
115178354Ssam	ic->ic_node_getmimoinfo = node_getmimoinfo;
116138568Ssam
117178354Ssam	/*
118178354Ssam	 * Set flags to be propagated to all vap's;
119178354Ssam	 * these define default behaviour/configuration.
120178354Ssam	 */
121178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
122178354Ssam}
123138568Ssam
124178354Ssamvoid
125178354Ssamieee80211_node_detach(struct ieee80211com *ic)
126178354Ssam{
127170530Ssam
128178354Ssam	callout_drain(&ic->ic_inact);
129178354Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
130178354Ssam}
131172062Ssam
132178354Ssamvoid
133178354Ssamieee80211_node_vattach(struct ieee80211vap *vap)
134178354Ssam{
135178354Ssam	/* NB: driver can override */
136178354Ssam	vap->iv_max_aid = IEEE80211_AID_DEF;
137178354Ssam
138178354Ssam	/* default station inactivity timer setings */
139178354Ssam	vap->iv_inact_init = IEEE80211_INACT_INIT;
140178354Ssam	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
141178354Ssam	vap->iv_inact_run = IEEE80211_INACT_RUN;
142178354Ssam	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
143184277Ssam
144184277Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
145184277Ssam	    "%s: init %u auth %u run %u probe %u\n", __func__,
146184277Ssam	    vap->iv_inact_init, vap->iv_inact_auth,
147184277Ssam	    vap->iv_inact_run, vap->iv_inact_probe);
148148863Ssam}
149148863Ssam
150148863Ssamvoid
151178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap)
152148863Ssam{
153178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
154178354Ssam		/* XXX should we allow max aid to be zero? */
155178354Ssam		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
156178354Ssam			vap->iv_max_aid = IEEE80211_AID_MIN;
157178354Ssam			if_printf(vap->iv_ifp,
158178354Ssam			    "WARNING: max aid too small, changed to %d\n",
159178354Ssam			    vap->iv_max_aid);
160178354Ssam		}
161186302Ssam		vap->iv_aid_bitmap = (uint32_t *) malloc(
162184210Sdes			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
163178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
164178354Ssam		if (vap->iv_aid_bitmap == NULL) {
165178354Ssam			/* XXX no way to recover */
166178354Ssam			printf("%s: no memory for AID bitmap, max aid %d!\n",
167178354Ssam			    __func__, vap->iv_max_aid);
168178354Ssam			vap->iv_max_aid = 0;
169178354Ssam		}
170138568Ssam	}
171138568Ssam
172178354Ssam	ieee80211_reset_bss(vap);
173118887Ssam
174178354Ssam	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
175116742Ssam}
176116742Ssam
177116742Ssamvoid
178178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap)
179116742Ssam{
180178354Ssam	struct ieee80211com *ic = vap->iv_ic;
181116742Ssam
182178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
183178354Ssam	if (vap->iv_bss != NULL) {
184178354Ssam		ieee80211_free_node(vap->iv_bss);
185178354Ssam		vap->iv_bss = NULL;
186138568Ssam	}
187178354Ssam	if (vap->iv_aid_bitmap != NULL) {
188186302Ssam		free(vap->iv_aid_bitmap, M_80211_NODE);
189178354Ssam		vap->iv_aid_bitmap = NULL;
190138568Ssam	}
191116742Ssam}
192116742Ssam
193138568Ssam/*
194138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
195138568Ssam */
196138568Ssam
197138568Ssamvoid
198148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
199138568Ssam{
200184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
201184277Ssam
202138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
203184277Ssam	ni->ni_inact_reload = vap->iv_inact_run;
204172062Ssam	ni->ni_inact = ni->ni_inact_reload;
205184277Ssam
206184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
207184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
208138568Ssam}
209138568Ssam
210138568Ssamvoid
211148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
212138568Ssam{
213184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
214184277Ssam
215138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
216184277Ssam	ni->ni_inact_reload = vap->iv_inact_auth;
217172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
218172062Ssam		ni->ni_inact = ni->ni_inact_reload;
219184277Ssam
220184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
221184277Ssam	    "%s: inact_reload %u inact %u", __func__,
222184277Ssam	    ni->ni_inact_reload, ni->ni_inact);
223138568Ssam}
224138568Ssam
225116742Ssam/*
226183251Ssam * Fix tx parameters for a node according to ``association state''.
227183251Ssam */
228183251Ssamstatic void
229183251Ssamnode_setuptxparms(struct ieee80211_node *ni)
230183251Ssam{
231183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
232187898Ssam	enum ieee80211_phymode mode;
233183251Ssam
234183251Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
235183251Ssam		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
236187898Ssam			mode = IEEE80211_MODE_11NA;
237183251Ssam		else
238187898Ssam			mode = IEEE80211_MODE_11NG;
239183251Ssam	} else {				/* legacy rate handling */
240187898Ssam		/* NB: 108A/108G should be handled as 11a/11g respectively */
241187898Ssam		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
242187898Ssam			mode = IEEE80211_MODE_STURBO_A;
243188782Ssam		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
244188782Ssam			mode = IEEE80211_MODE_HALF;
245188782Ssam		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
246188782Ssam			mode = IEEE80211_MODE_QUARTER;
247187898Ssam		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
248187898Ssam			mode = IEEE80211_MODE_11A;
249183251Ssam		else if (ni->ni_flags & IEEE80211_NODE_ERP)
250187898Ssam			mode = IEEE80211_MODE_11G;
251183251Ssam		else
252187898Ssam			mode = IEEE80211_MODE_11B;
253183251Ssam	}
254187898Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
255183251Ssam}
256183251Ssam
257183251Ssam/*
258138568Ssam * Set/change the channel.  The rate set is also updated as
259138568Ssam * to insure a consistent view by drivers.
260178354Ssam * XXX should be private but hostap needs it to deal with CSA
261138568Ssam */
262178354Ssamvoid
263178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni,
264178354Ssam	struct ieee80211_channel *chan)
265138568Ssam{
266178354Ssam	struct ieee80211com *ic = ni->ni_ic;
267183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
268183251Ssam	enum ieee80211_phymode mode;
269170530Ssam
270178354Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
271178354Ssam
272138568Ssam	ni->ni_chan = chan;
273183251Ssam	mode = ieee80211_chan2mode(chan);
274170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
275170530Ssam		/*
276170530Ssam		 * XXX Gotta be careful here; the rate set returned by
277170530Ssam		 * ieee80211_get_suprates is actually any HT rate
278170530Ssam		 * set so blindly copying it will be bad.  We must
279170530Ssam		 * install the legacy rate est in ni_rates and the
280170530Ssam		 * HT rate set in ni_htrates.
281170530Ssam		 */
282170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
283183251Ssam		/*
284183251Ssam		 * Setup bss tx parameters based on operating mode.  We
285183251Ssam		 * use legacy rates when operating in a mixed HT+non-HT bss
286183251Ssam		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
287183251Ssam		 */
288183251Ssam		if (mode == IEEE80211_MODE_11NA &&
289183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
290183251Ssam			mode = IEEE80211_MODE_11A;
291183251Ssam		else if (mode == IEEE80211_MODE_11NG &&
292183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
293183251Ssam			mode = IEEE80211_MODE_11G;
294183251Ssam		if (mode == IEEE80211_MODE_11G &&
295183251Ssam		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
296183251Ssam			mode = IEEE80211_MODE_11B;
297170530Ssam	}
298183251Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
299165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
300138568Ssam}
301138568Ssam
302141658Ssamstatic __inline void
303141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
304141658Ssam{
305141658Ssam	/* propagate useful state */
306141658Ssam	nbss->ni_authmode = obss->ni_authmode;
307141658Ssam	nbss->ni_txpower = obss->ni_txpower;
308141658Ssam	nbss->ni_vlan = obss->ni_vlan;
309141658Ssam	/* XXX statistics? */
310178354Ssam	/* XXX legacy WDS bssid? */
311141658Ssam}
312141658Ssam
313116742Ssamvoid
314178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
315116742Ssam{
316178354Ssam	struct ieee80211com *ic = vap->iv_ic;
317116742Ssam	struct ieee80211_node *ni;
318116742Ssam
319178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
320178354Ssam		"%s: creating ibss on channel %u\n", __func__,
321178354Ssam		ieee80211_chan2ieee(ic, chan));
322138568Ssam
323178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
324140753Ssam	if (ni == NULL) {
325140753Ssam		/* XXX recovery? */
326138568Ssam		return;
327138568Ssam	}
328178354Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
329178354Ssam	ni->ni_esslen = vap->iv_des_ssid[0].len;
330178354Ssam	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
331178354Ssam	if (vap->iv_bss != NULL)
332178354Ssam		copy_bss(ni, vap->iv_bss);
333148843Ssam	ni->ni_intval = ic->ic_bintval;
334178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
335116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
336116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
337116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
338116742Ssam		ni->ni_fhindex = 1;
339116742Ssam	}
340178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
341178354Ssam		vap->iv_flags |= IEEE80211_F_SIBSS;
342138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
343178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
344178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
345167282Ssam		else {
346167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
347167282Ssam			/* clear group bit, add local bit */
348167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
349167282Ssam		}
350178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
351178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
352178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
353153403Ssam		else
354186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
355186904Ssam		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
356186904Ssam#endif
357153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
358138568Ssam	}
359138568Ssam	/*
360138568Ssam	 * Fix the channel and related attributes.
361138568Ssam	 */
362178354Ssam	/* clear DFS CAC state on previous channel */
363178354Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
364178354Ssam	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
365178354Ssam	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
366178354Ssam		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
367170530Ssam	ic->ic_bsschan = chan;
368178354Ssam	ieee80211_node_set_chan(ni, chan);
369170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
370138568Ssam	/*
371178354Ssam	 * Do mode-specific setup.
372138568Ssam	 */
373170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
374170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
375170530Ssam			/*
376178354Ssam			 * Use a mixed 11b/11g basic rate set.
377170530Ssam			 */
378178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
379178354Ssam			    IEEE80211_MODE_11G);
380178354Ssam			if (vap->iv_flags & IEEE80211_F_PUREG) {
381178354Ssam				/*
382178354Ssam				 * Also mark OFDM rates basic so 11b
383178354Ssam				 * stations do not join (WiFi compliance).
384178354Ssam				 */
385178354Ssam				ieee80211_addbasicrates(&ni->ni_rates,
386178354Ssam				    IEEE80211_MODE_11A);
387178354Ssam			}
388170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
389170530Ssam			/*
390170530Ssam			 * Force pure 11b rate set.
391170530Ssam			 */
392178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
393170530Ssam				IEEE80211_MODE_11B);
394170530Ssam		}
395170530Ssam	}
396138568Ssam
397170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
398116742Ssam}
399116742Ssam
400170530Ssam/*
401170530Ssam * Reset bss state on transition to the INIT state.
402170530Ssam * Clear any stations from the table (they have been
403170530Ssam * deauth'd) and reset the bss node (clears key, rate
404170530Ssam * etc. state).
405170530Ssam */
406138568Ssamvoid
407178354Ssamieee80211_reset_bss(struct ieee80211vap *vap)
408138568Ssam{
409178354Ssam	struct ieee80211com *ic = vap->iv_ic;
410138568Ssam	struct ieee80211_node *ni, *obss;
411138568Ssam
412178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
413178354Ssam	/* XXX multi-bss: wrong */
414170530Ssam	ieee80211_reset_erp(ic);
415140753Ssam
416178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
417138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
418178354Ssam	obss = vap->iv_bss;
419178354Ssam	vap->iv_bss = ieee80211_ref_node(ni);
420141658Ssam	if (obss != NULL) {
421141658Ssam		copy_bss(ni, obss);
422148843Ssam		ni->ni_intval = ic->ic_bintval;
423138568Ssam		ieee80211_free_node(obss);
424178354Ssam	} else
425178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
426138568Ssam}
427138568Ssam
428170530Ssamstatic int
429170530Ssammatch_ssid(const struct ieee80211_node *ni,
430170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
431170530Ssam{
432170530Ssam	int i;
433148432Ssam
434170530Ssam	for (i = 0; i < nssid; i++) {
435170530Ssam		if (ni->ni_esslen == ssids[i].len &&
436170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
437170530Ssam			return 1;
438170530Ssam	}
439170530Ssam	return 0;
440170530Ssam}
441170530Ssam
442170530Ssam/*
443170530Ssam * Test a node for suitability/compatibility.
444170530Ssam */
445127767Ssamstatic int
446178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
447127767Ssam{
448178354Ssam	struct ieee80211com *ic = ni->ni_ic;
449170530Ssam        uint8_t rate;
450170530Ssam
451170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
452170530Ssam		return 0;
453178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
454170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
455170530Ssam			return 0;
456170530Ssam	} else {
457170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
458170530Ssam			return 0;
459170530Ssam	}
460178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
461170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
462170530Ssam			return 0;
463170530Ssam	} else {
464170530Ssam		/* XXX does this mean privacy is supported or required? */
465170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
466170530Ssam			return 0;
467170530Ssam	}
468170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
469170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
470170530Ssam	if (rate & IEEE80211_RATE_BASIC)
471170530Ssam		return 0;
472178354Ssam	if (vap->iv_des_nssid != 0 &&
473178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
474170530Ssam		return 0;
475178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
476178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
477170530Ssam		return 0;
478170530Ssam	return 1;
479170530Ssam}
480170530Ssam
481170530Ssam#ifdef IEEE80211_DEBUG
482170530Ssam/*
483170530Ssam * Display node suitability/compatibility.
484170530Ssam */
485170530Ssamstatic void
486178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
487170530Ssam{
488178354Ssam	struct ieee80211com *ic = ni->ni_ic;
489170530Ssam        uint8_t rate;
490127767Ssam        int fail;
491127767Ssam
492127767Ssam	fail = 0;
493127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
494127767Ssam		fail |= 0x01;
495178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
496127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
497127767Ssam			fail |= 0x02;
498127767Ssam	} else {
499127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
500127767Ssam			fail |= 0x02;
501127767Ssam	}
502178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
503127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
504127767Ssam			fail |= 0x04;
505127767Ssam	} else {
506127767Ssam		/* XXX does this mean privacy is supported or required? */
507127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
508127767Ssam			fail |= 0x04;
509127767Ssam	}
510167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
511165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
512127767Ssam	if (rate & IEEE80211_RATE_BASIC)
513127767Ssam		fail |= 0x08;
514178354Ssam	if (vap->iv_des_nssid != 0 &&
515178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
516127767Ssam		fail |= 0x10;
517178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
518178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
519127767Ssam		fail |= 0x20;
520127767Ssam
521170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
522170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
523170530Ssam	printf(" %3d%c",
524170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
525170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
526170530Ssam	    fail & 0x08 ? '!' : ' ');
527170530Ssam	printf(" %4s%c",
528170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
529170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
530170530Ssam	    "????",
531170530Ssam	    fail & 0x02 ? '!' : ' ');
532170530Ssam	printf(" %3s%c ",
533170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
534170530Ssam	    fail & 0x04 ? '!' : ' ');
535170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
536170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
537138568Ssam}
538170530Ssam#endif /* IEEE80211_DEBUG */
539138568Ssam
540138568Ssam/*
541138568Ssam * Handle 802.11 ad hoc network merge.  The
542138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
543138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
544138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
545138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
546138568Ssam * sends beacons with the greatest TSF timestamp.
547138568Ssam *
548138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
549138568Ssam *
550138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
551138568Ssam */
552138568Ssamint
553148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
554138568Ssam{
555178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
556178354Ssam#ifdef IEEE80211_DEBUG
557148306Ssam	struct ieee80211com *ic = ni->ni_ic;
558178354Ssam#endif
559138568Ssam
560178354Ssam	if (ni == vap->iv_bss ||
561178354Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
562138568Ssam		/* unchanged, nothing to do */
563138568Ssam		return 0;
564138568Ssam	}
565178354Ssam	if (!check_bss(vap, ni)) {
566170530Ssam		/* capabilities mismatch */
567178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
568138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
569170530Ssam#ifdef IEEE80211_DEBUG
570178354Ssam		if (ieee80211_msg_assoc(vap))
571178354Ssam			check_bss_debug(vap, ni);
572170530Ssam#endif
573178354Ssam		vap->iv_stats.is_ibss_capmismatch++;
574138568Ssam		return 0;
575138568Ssam	}
576178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
577138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
578138568Ssam		ether_sprintf(ni->ni_bssid),
579138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
580138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
581138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
582138568Ssam	);
583170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
584138568Ssam}
585138568Ssam
586138568Ssam/*
587178354Ssam * Calculate HT channel promotion flags for all vaps.
588178354Ssam * This assumes ni_chan have been setup for each vap.
589173273Ssam */
590178354Ssamstatic int
591178354Ssamgethtadjustflags(struct ieee80211com *ic)
592178354Ssam{
593178354Ssam	struct ieee80211vap *vap;
594178354Ssam	int flags;
595178354Ssam
596178354Ssam	flags = 0;
597178354Ssam	/* XXX locking */
598178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
599178354Ssam		if (vap->iv_state < IEEE80211_S_RUN)
600178354Ssam			continue;
601178354Ssam		switch (vap->iv_opmode) {
602178354Ssam		case IEEE80211_M_WDS:
603178354Ssam		case IEEE80211_M_STA:
604178354Ssam		case IEEE80211_M_AHDEMO:
605178354Ssam		case IEEE80211_M_HOSTAP:
606178354Ssam		case IEEE80211_M_IBSS:
607178354Ssam			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
608178354Ssam			break;
609178354Ssam		default:
610178354Ssam			break;
611178354Ssam		}
612178354Ssam	}
613178354Ssam	return flags;
614178354Ssam}
615178354Ssam
616178354Ssam/*
617178354Ssam * Check if the current channel needs to change based on whether
618184303Ssam * any vap's are using HT20/HT40.  This is used to sync the state
619184303Ssam * of ic_curchan after a channel width change on a running vap.
620178354Ssam */
621173273Ssamvoid
622178354Ssamieee80211_sync_curchan(struct ieee80211com *ic)
623173273Ssam{
624178354Ssam	struct ieee80211_channel *c;
625178354Ssam
626178354Ssam	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
627178354Ssam	if (c != ic->ic_curchan) {
628178354Ssam		ic->ic_curchan = c;
629178354Ssam		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
630190532Ssam		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
631178354Ssam		ic->ic_set_channel(ic);
632178354Ssam	}
633178354Ssam}
634178354Ssam
635178354Ssam/*
636178354Ssam * Change the current channel.  The request channel may be
637178354Ssam * promoted if other vap's are operating with HT20/HT40.
638178354Ssam */
639178354Ssamvoid
640178354Ssamieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
641178354Ssam{
642178354Ssam	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
643178354Ssam		int flags = gethtadjustflags(ic);
644178354Ssam		/*
645178354Ssam		 * Check for channel promotion required to support the
646178354Ssam		 * set of running vap's.  This assumes we are called
647178354Ssam		 * after ni_chan is setup for each vap.
648178354Ssam		 */
649178354Ssam		/* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
650178354Ssam		if (flags > ieee80211_htchanflags(c))
651178354Ssam			c = ieee80211_ht_adjust_channel(ic, c, flags);
652178354Ssam	}
653178354Ssam	ic->ic_bsschan = ic->ic_curchan = c;
654173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
655190532Ssam	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
656173273Ssam	ic->ic_set_channel(ic);
657173273Ssam}
658173273Ssam
659173273Ssam/*
660138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
661138568Ssam * be passed in with a held reference.
662138568Ssam */
663170530Ssamstatic int
664170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
665138568Ssam{
666178354Ssam	struct ieee80211vap *vap = selbs->ni_vap;
667170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
668138568Ssam	struct ieee80211_node *obss;
669170530Ssam	int canreassoc;
670138568Ssam
671138568Ssam	/*
672138568Ssam	 * Committed to selbs, setup state.
673138568Ssam	 */
674178354Ssam	obss = vap->iv_bss;
675170530Ssam	/*
676170530Ssam	 * Check if old+new node have the same address in which
677170530Ssam	 * case we can reassociate when operating in sta mode.
678170530Ssam	 */
679170530Ssam	canreassoc = (obss != NULL &&
680178354Ssam		vap->iv_state == IEEE80211_S_RUN &&
681170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
682178354Ssam	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
683153352Ssam	if (obss != NULL) {
684153352Ssam		copy_bss(selbs, obss);
685188541Ssam		ieee80211_node_decref(obss);	/* iv_bss reference */
686188541Ssam		ieee80211_free_node(obss);	/* station table reference */
687178354Ssam		obss = NULL;		/* NB: guard against later use */
688153352Ssam	}
689165887Ssam
690138568Ssam	/*
691165887Ssam	 * Delete unusable rates; we've already checked
692165887Ssam	 * that the negotiated rate set is acceptable.
693165887Ssam	 */
694178354Ssam	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
695167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
696165887Ssam
697178354Ssam	ieee80211_setcurchan(ic, selbs->ni_chan);
698165887Ssam	/*
699138568Ssam	 * Set the erp state (mostly the slot time) to deal with
700138568Ssam	 * the auto-select case; this should be redundant if the
701138568Ssam	 * mode is locked.
702138568Ssam	 */
703138568Ssam	ieee80211_reset_erp(ic);
704178354Ssam	ieee80211_wme_initparams(vap);
705140753Ssam
706178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
707170530Ssam		if (canreassoc) {
708170530Ssam			/* Reassociate */
709178354Ssam			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
710170530Ssam		} else {
711170530Ssam			/*
712170530Ssam			 * Act as if we received a DEAUTH frame in case we
713170530Ssam			 * are invoked from the RUN state.  This will cause
714170530Ssam			 * us to try to re-authenticate if we are operating
715170530Ssam			 * as a station.
716170530Ssam			 */
717178354Ssam			ieee80211_new_state(vap, IEEE80211_S_AUTH,
718170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
719170530Ssam		}
720170530Ssam	} else
721178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
722138568Ssam	return 1;
723116742Ssam}
724116742Ssam
725170530Ssamint
726184274Ssamieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
727170530Ssam	const struct ieee80211_scan_entry *se)
728170530Ssam{
729178354Ssam	struct ieee80211com *ic = vap->iv_ic;
730170530Ssam	struct ieee80211_node *ni;
731170530Ssam
732178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
733170530Ssam	if (ni == NULL) {
734170530Ssam		/* XXX msg */
735170530Ssam		return 0;
736170530Ssam	}
737170530Ssam	/*
738170530Ssam	 * Expand scan state into node's format.
739170530Ssam	 * XXX may not need all this stuff
740170530Ssam	 */
741170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
742170530Ssam	ni->ni_esslen = se->se_ssid[1];
743170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
744170530Ssam	ni->ni_rstamp = se->se_rstamp;
745170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
746170530Ssam	ni->ni_intval = se->se_intval;
747170530Ssam	ni->ni_capinfo = se->se_capinfo;
748184274Ssam	ni->ni_chan = chan;
749170530Ssam	ni->ni_timoff = se->se_timoff;
750170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
751170530Ssam	ni->ni_fhindex = se->se_fhindex;
752170530Ssam	ni->ni_erp = se->se_erp;
753178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
754170530Ssam	ni->ni_noise = se->se_noise;
755186870Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
756186870Ssam		/* NB: only infrastructure mode requires an associd */
757186870Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
758186870Ssam	}
759178354Ssam
760178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
761178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
762190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
763178354Ssam		if (ni->ni_ies.ath_ie != NULL)
764178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
765190391Ssam#endif
766178354Ssam		if (ni->ni_ies.htcap_ie != NULL)
767178354Ssam			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
768178354Ssam		if (ni->ni_ies.htinfo_ie != NULL)
769178354Ssam			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
770186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
771186904Ssam		if (ni->ni_ies.tdma_ie != NULL)
772186904Ssam			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
773186904Ssam#endif
774173864Ssam	}
775170530Ssam
776178354Ssam	vap->iv_dtim_period = se->se_dtimperiod;
777178354Ssam	vap->iv_dtim_count = 0;
778170530Ssam
779170530Ssam	/* NB: must be after ni_chan is setup */
780170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
781170530Ssam		IEEE80211_F_DOSORT);
782184279Ssam	if (ieee80211_iserp_rateset(&ni->ni_rates))
783184279Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
784184279Ssam	node_setuptxparms(ni);
785170530Ssam
786170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
787170530Ssam}
788170530Ssam
789138568Ssam/*
790138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
791138568Ssam * be passed in with a held reference.
792138568Ssam */
793138568Ssamvoid
794178354Ssamieee80211_sta_leave(struct ieee80211_node *ni)
795138568Ssam{
796178354Ssam	struct ieee80211com *ic = ni->ni_ic;
797178354Ssam
798138568Ssam	ic->ic_node_cleanup(ni);
799178354Ssam	ieee80211_notify_node_leave(ni);
800138568Ssam}
801138568Ssam
802178354Ssam/*
803178354Ssam * Send a deauthenticate frame and drop the station.
804178354Ssam */
805178354Ssamvoid
806178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason)
807178354Ssam{
808178354Ssam	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
809178354Ssam	ieee80211_ref_node(ni);
810178354Ssam	if (ni->ni_associd != 0)
811178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
812178354Ssam	ieee80211_node_leave(ni);
813178354Ssam	ieee80211_free_node(ni);
814178354Ssam}
815178354Ssam
816116742Ssamstatic struct ieee80211_node *
817179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
818116742Ssam{
819127768Ssam	struct ieee80211_node *ni;
820138568Ssam
821186302Ssam	ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
822127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
823127768Ssam	return ni;
824116742Ssam}
825116742Ssam
826138568Ssam/*
827178354Ssam * Initialize an ie blob with the specified data.  If previous
828178354Ssam * data exists re-use the data block.  As a side effect we clear
829178354Ssam * all references to specific ie's; the caller is required to
830178354Ssam * recalculate them.
831178354Ssam */
832178354Ssamint
833178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
834178354Ssam{
835178354Ssam	/* NB: assumes data+len are the last fields */
836178354Ssam	memset(ies, 0, offsetof(struct ieee80211_ies, data));
837178354Ssam	if (ies->data != NULL && ies->len != len) {
838178354Ssam		/* data size changed */
839186302Ssam		free(ies->data, M_80211_NODE_IE);
840178354Ssam		ies->data = NULL;
841178354Ssam	}
842178354Ssam	if (ies->data == NULL) {
843186302Ssam		ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
844178354Ssam		if (ies->data == NULL) {
845178354Ssam			ies->len = 0;
846178354Ssam			/* NB: pointers have already been zero'd above */
847178354Ssam			return 0;
848178354Ssam		}
849178354Ssam	}
850178354Ssam	memcpy(ies->data, data, len);
851178354Ssam	ies->len = len;
852178354Ssam	return 1;
853178354Ssam}
854178354Ssam
855178354Ssam/*
856178354Ssam * Reclaim storage for an ie blob.
857178354Ssam */
858178354Ssamvoid
859178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies)
860178354Ssam{
861178354Ssam	if (ies->data != NULL)
862186302Ssam		free(ies->data, M_80211_NODE_IE);
863178354Ssam}
864178354Ssam
865178354Ssam/*
866178354Ssam * Expand an ie blob data contents and to fillin individual
867178354Ssam * ie pointers.  The data blob is assumed to be well-formed;
868178354Ssam * we don't do any validity checking of ie lengths.
869178354Ssam */
870178354Ssamvoid
871178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies)
872178354Ssam{
873178354Ssam	uint8_t *ie;
874178354Ssam	int ielen;
875178354Ssam
876178354Ssam	ie = ies->data;
877178354Ssam	ielen = ies->len;
878178354Ssam	while (ielen > 0) {
879178354Ssam		switch (ie[0]) {
880178354Ssam		case IEEE80211_ELEMID_VENDOR:
881178354Ssam			if (iswpaoui(ie))
882178354Ssam				ies->wpa_ie = ie;
883178354Ssam			else if (iswmeoui(ie))
884178354Ssam				ies->wme_ie = ie;
885190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
886178354Ssam			else if (isatherosoui(ie))
887178354Ssam				ies->ath_ie = ie;
888190391Ssam#endif
889186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
890186904Ssam			else if (istdmaoui(ie))
891186904Ssam				ies->tdma_ie = ie;
892186904Ssam#endif
893178354Ssam			break;
894178354Ssam		case IEEE80211_ELEMID_RSN:
895178354Ssam			ies->rsn_ie = ie;
896178354Ssam			break;
897178354Ssam		case IEEE80211_ELEMID_HTCAP:
898178354Ssam			ies->htcap_ie = ie;
899178354Ssam			break;
900178354Ssam		}
901178354Ssam		ielen -= 2 + ie[1];
902178354Ssam		ie += 2 + ie[1];
903178354Ssam	}
904178354Ssam}
905178354Ssam
906178354Ssam/*
907138568Ssam * Reclaim any resources in a node and reset any critical
908138568Ssam * state.  Typically nodes are free'd immediately after,
909138568Ssam * but in some cases the storage may be reused so we need
910138568Ssam * to insure consistent state (should probably fix that).
911138568Ssam */
912116742Ssamstatic void
913138568Ssamnode_cleanup(struct ieee80211_node *ni)
914116742Ssam{
915138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
916178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
917170530Ssam	int i;
918138568Ssam
919138568Ssam	/* NB: preserve ni_table */
920138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
921178354Ssam		if (vap->iv_opmode != IEEE80211_M_STA)
922178354Ssam			vap->iv_ps_sta--;
923138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
924178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
925178354Ssam		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
926138568Ssam	}
927147788Ssam	/*
928173273Ssam	 * Cleanup any HT-related state.
929173273Ssam	 */
930173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
931173273Ssam		ieee80211_ht_node_cleanup(ni);
932173273Ssam	/*
933147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
934147788Ssam	 * has happened.  This is probably not needed as the node
935147788Ssam	 * should always be removed from the table so not found but
936147788Ssam	 * do it just in case.
937186099Ssam	 * Likewise clear the ASSOCID flag as these flags are intended
938186099Ssam	 * to be managed in tandem.
939147788Ssam	 */
940186099Ssam	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
941138568Ssam
942138568Ssam	/*
943138568Ssam	 * Drain power save queue and, if needed, clear TIM.
944138568Ssam	 */
945184288Ssam	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
946178354Ssam		vap->iv_set_tim(ni, 0);
947138568Ssam
948138568Ssam	ni->ni_associd = 0;
949138568Ssam	if (ni->ni_challenge != NULL) {
950186302Ssam		free(ni->ni_challenge, M_80211_NODE);
951138568Ssam		ni->ni_challenge = NULL;
952138568Ssam	}
953138568Ssam	/*
954138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
955138568Ssam	 * reusable during a re-auth/re-assoc state transition.
956138568Ssam	 * If we remove these data they will not be recreated
957138568Ssam	 * because they come from a probe-response or beacon frame
958138568Ssam	 * which cannot be expected prior to the association-response.
959138568Ssam	 * This should not be an issue when operating in other modes
960138568Ssam	 * as stations leaving always go through a full state transition
961138568Ssam	 * which will rebuild this state.
962138568Ssam	 *
963138568Ssam	 * XXX does this leave us open to inheriting old state?
964138568Ssam	 */
965138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
966138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
967138568Ssam			m_freem(ni->ni_rxfrag[i]);
968138568Ssam			ni->ni_rxfrag[i] = NULL;
969138568Ssam		}
970148863Ssam	/*
971148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
972148863Ssam	 */
973148863Ssam	ieee80211_node_delucastkey(ni);
974138568Ssam#undef N
975116742Ssam}
976116742Ssam
977116742Ssamstatic void
978138568Ssamnode_free(struct ieee80211_node *ni)
979116742Ssam{
980138568Ssam	struct ieee80211com *ic = ni->ni_ic;
981138568Ssam
982138568Ssam	ic->ic_node_cleanup(ni);
983178354Ssam	ieee80211_ies_cleanup(&ni->ni_ies);
984184288Ssam	ieee80211_psq_cleanup(&ni->ni_psq);
985178354Ssam	IEEE80211_NODE_WDSQ_DESTROY(ni);
986186302Ssam	free(ni, M_80211_NODE);
987116742Ssam}
988116742Ssam
989178354Ssamstatic void
990178354Ssamnode_age(struct ieee80211_node *ni)
991178354Ssam{
992178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
993184303Ssam
994184303Ssam	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
995184303Ssam
996178354Ssam	/*
997178354Ssam	 * Age frames on the power save queue.
998178354Ssam	 */
999184288Ssam	if (ieee80211_node_psq_age(ni) != 0 &&
1000184288Ssam	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1001178354Ssam		vap->iv_set_tim(ni, 0);
1002178354Ssam	/*
1003178354Ssam	 * Age frames on the wds pending queue.
1004178354Ssam	 */
1005178354Ssam	if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
1006178354Ssam		ieee80211_node_wdsq_age(ni);
1007178354Ssam	/*
1008178354Ssam	 * Age out HT resources (e.g. frames on the
1009178354Ssam	 * A-MPDU reorder queues).
1010178354Ssam	 */
1011178354Ssam	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1012178354Ssam		ieee80211_ht_node_age(ni);
1013178354Ssam}
1014178354Ssam
1015170530Ssamstatic int8_t
1016138568Ssamnode_getrssi(const struct ieee80211_node *ni)
1017120104Ssam{
1018178354Ssam	uint32_t avgrssi = ni->ni_avgrssi;
1019178354Ssam	int32_t rssi;
1020178354Ssam
1021178354Ssam	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1022178354Ssam		return 0;
1023178354Ssam	rssi = IEEE80211_RSSI_GET(avgrssi);
1024178354Ssam	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1025120104Ssam}
1026120104Ssam
1027116742Ssamstatic void
1028170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1029170530Ssam{
1030178354Ssam	*rssi = node_getrssi(ni);
1031170530Ssam	*noise = ni->ni_noise;
1032170530Ssam}
1033170530Ssam
1034170530Ssamstatic void
1035178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni,
1036178354Ssam	struct ieee80211_mimo_info *info)
1037116742Ssam{
1038178354Ssam	/* XXX zero data? */
1039178354Ssam}
1040178354Ssam
1041178354Ssamstruct ieee80211_node *
1042178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt,
1043178354Ssam	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1044178354Ssam{
1045138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1046178354Ssam	struct ieee80211_node *ni;
1047116742Ssam	int hash;
1048116742Ssam
1049179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1050178354Ssam	if (ni == NULL) {
1051178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1052178354Ssam		return NULL;
1053178354Ssam	}
1054178354Ssam
1055178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1056140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1057138568Ssam		ether_sprintf(macaddr), nt->nt_name);
1058138568Ssam
1059116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1060116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1061138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
1062138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
1063138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
1064138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
1065183251Ssam	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1066178354Ssam	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1067178354Ssam	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1068139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
1069139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1070170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
1071184288Ssam	ieee80211_psq_init(&ni->ni_psq, "unknown");
1072178354Ssam	IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1073138568Ssam
1074138568Ssam	IEEE80211_NODE_LOCK(nt);
1075138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1076138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1077138568Ssam	ni->ni_table = nt;
1078178354Ssam	ni->ni_vap = vap;
1079138568Ssam	ni->ni_ic = ic;
1080138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1081116742Ssam
1082184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1083184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1084184277Ssam
1085116742Ssam	return ni;
1086116742Ssam}
1087116742Ssam
1088148777Ssam/*
1089148777Ssam * Craft a temporary node suitable for sending a management frame
1090148777Ssam * to the specified station.  We craft only as much state as we
1091148777Ssam * need to do the work since the node will be immediately reclaimed
1092148777Ssam * once the send completes.
1093148777Ssam */
1094116742Ssamstruct ieee80211_node *
1095178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1096178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1097148777Ssam{
1098178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1099148777Ssam	struct ieee80211_node *ni;
1100148777Ssam
1101179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1102148777Ssam	if (ni != NULL) {
1103183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1104183259Ssam
1105178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1106148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1107148777Ssam
1108178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1109178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1110178354Ssam		ni->ni_vap = vap;
1111178354Ssam
1112148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1113183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1114148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1115148777Ssam		/* NB: required by ieee80211_fix_rate */
1116183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1117178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1118148777Ssam			IEEE80211_KEYIX_NONE);
1119183259Ssam		ni->ni_txpower = bss->ni_txpower;
1120148777Ssam		/* XXX optimize away */
1121184288Ssam		ieee80211_psq_init(&ni->ni_psq, "unknown");
1122178354Ssam		IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1123148777Ssam	} else {
1124148777Ssam		/* XXX msg */
1125178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1126148777Ssam	}
1127148777Ssam	return ni;
1128148777Ssam}
1129148777Ssam
1130148777Ssamstruct ieee80211_node *
1131178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1132178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1133116742Ssam{
1134178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1135138568Ssam	struct ieee80211_node *ni;
1136138568Ssam
1137178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1138116742Ssam	if (ni != NULL) {
1139183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1140127770Ssam		/*
1141178354Ssam		 * Inherit from iv_bss.
1142127770Ssam		 */
1143183259Ssam		copy_bss(ni, bss);
1144183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1145183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1146178354Ssam	}
1147116742Ssam	return ni;
1148116742Ssam}
1149116742Ssam
1150178354Ssam/*
1151178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1152178354Ssam * not associate so we just create create a new node and
1153178354Ssam * simulate an association.  The caller is responsible for
1154178354Ssam * installing the node as the bss node and handling any further
1155178354Ssam * setup work like authorizing the port.
1156178354Ssam */
1157178354Ssamstruct ieee80211_node *
1158178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1159178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1160178354Ssam{
1161178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1162178354Ssam	struct ieee80211_node *ni;
1163178354Ssam
1164178354Ssam	/* XXX check if node already in sta table? */
1165178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1166178354Ssam	if (ni != NULL) {
1167178354Ssam		ni->ni_wdsvap = vap;
1168178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1169178354Ssam		/*
1170178354Ssam		 * Inherit any manually configured settings.
1171178354Ssam		 */
1172183259Ssam		copy_bss(ni, vap->iv_bss);
1173178354Ssam		ieee80211_node_set_chan(ni, chan);
1174178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1175178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1176178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1177178354Ssam		/* NB: no associd for peer */
1178178354Ssam		/*
1179178354Ssam		 * There are no management frames to use to
1180178354Ssam		 * discover neighbor capabilities, so blindly
1181178354Ssam		 * propagate the local configuration.
1182178354Ssam		 */
1183178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1184178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1185190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1186178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1187178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1188190391Ssam#endif
1189178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1190178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1191178354Ssam			/*
1192178354Ssam			 * Device is HT-capable and HT is enabled for
1193178354Ssam			 * the vap; setup HT operation.  On return
1194178354Ssam			 * ni_chan will be adjusted to an HT channel.
1195178354Ssam			 */
1196178354Ssam			ieee80211_ht_wds_init(ni);
1197178354Ssam		} else {
1198178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1199178354Ssam			/*
1200178354Ssam			 * Force a legacy channel to be used.
1201178354Ssam			 */
1202178354Ssam			c = ieee80211_find_channel(ic,
1203178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1204178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1205178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1206178354Ssam			ni->ni_chan = c;
1207178354Ssam		}
1208178354Ssam	}
1209178354Ssam	return ni;
1210178354Ssam}
1211178354Ssam
1212178354Ssamstruct ieee80211_node *
1213138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1214178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1215178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1216138568Ssam#else
1217178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1218178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1219138568Ssam#endif
1220116742Ssam{
1221116742Ssam	struct ieee80211_node *ni;
1222116742Ssam	int hash;
1223116742Ssam
1224138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1225127772Ssam
1226116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1227138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1228116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1229138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1230138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1231178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1232140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1233140766Ssam			    func, line,
1234140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1235140766Ssam			    ieee80211_node_refcnt(ni));
1236138568Ssam#endif
1237127772Ssam			return ni;
1238116742Ssam		}
1239116742Ssam	}
1240127772Ssam	return NULL;
1241127772Ssam}
1242178354Ssam
1243178354Ssamstruct ieee80211_node *
1244138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1245178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1246178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1247178354Ssam#else
1248178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1249178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1250138568Ssam#endif
1251178354Ssam{
1252178354Ssam	struct ieee80211_node *ni;
1253127772Ssam
1254178354Ssam	IEEE80211_NODE_LOCK(nt);
1255178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1256178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1257178354Ssam	return ni;
1258178354Ssam}
1259178354Ssam
1260127772Ssamstruct ieee80211_node *
1261138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1262178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1263178354Ssam	const struct ieee80211vap *vap,
1264178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1265138568Ssam#else
1266178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1267178354Ssam	const struct ieee80211vap *vap,
1268178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1269138568Ssam#endif
1270127772Ssam{
1271127772Ssam	struct ieee80211_node *ni;
1272178354Ssam	int hash;
1273127772Ssam
1274178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1275178354Ssam
1276178354Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1277178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1278178354Ssam		if (ni->ni_vap == vap &&
1279178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1280178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1281178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1282178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1283178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1284178354Ssam			    func, line,
1285178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1286178354Ssam			    ieee80211_node_refcnt(ni));
1287178354Ssam#endif
1288178354Ssam			return ni;
1289178354Ssam		}
1290178354Ssam	}
1291178354Ssam	return NULL;
1292178354Ssam}
1293178354Ssam
1294178354Ssamstruct ieee80211_node *
1295178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1296178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1297178354Ssam	const struct ieee80211vap *vap,
1298178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1299178354Ssam#else
1300178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1301178354Ssam	const struct ieee80211vap *vap,
1302178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1303178354Ssam#endif
1304178354Ssam{
1305178354Ssam	struct ieee80211_node *ni;
1306178354Ssam
1307138568Ssam	IEEE80211_NODE_LOCK(nt);
1308178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1309138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1310116742Ssam	return ni;
1311116742Ssam}
1312116742Ssam
1313116742Ssam/*
1314138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1315138568Ssam * Note that for the driver's benefit we we treat this like
1316138568Ssam * an association so the driver has an opportunity to setup
1317138568Ssam * it's private state.
1318138568Ssam */
1319138568Ssamstruct ieee80211_node *
1320178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1321170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1322138568Ssam{
1323138568Ssam	struct ieee80211_node *ni;
1324138568Ssam
1325178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1326153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1327178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1328138568Ssam	if (ni != NULL) {
1329178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1330178354Ssam
1331138568Ssam		/* XXX no rate negotiation; just dup */
1332178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1333188869Ssam		if (ieee80211_iserp_rateset(&ni->ni_rates))
1334188869Ssam			ni->ni_flags |= IEEE80211_NODE_ERP;
1335178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1336153404Ssam			/*
1337170530Ssam			 * In adhoc demo mode there are no management
1338170530Ssam			 * frames to use to discover neighbor capabilities,
1339170530Ssam			 * so blindly propagate the local configuration
1340170530Ssam			 * so we can do interesting things (e.g. use
1341170530Ssam			 * WME to disable ACK's).
1342153404Ssam			 */
1343178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1344153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1345190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1346178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1347170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1348190391Ssam#endif
1349153404Ssam		}
1350183251Ssam		node_setuptxparms(ni);
1351178354Ssam		if (ic->ic_newassoc != NULL)
1352178354Ssam			ic->ic_newassoc(ni, 1);
1353170530Ssam		/* XXX not right for 802.1x/WPA */
1354170530Ssam		ieee80211_node_authorize(ni);
1355138568Ssam	}
1356138568Ssam	return ni;
1357138568Ssam}
1358138568Ssam
1359148936Ssamvoid
1360153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1361153073Ssam	const struct ieee80211_frame *wh,
1362153073Ssam	const struct ieee80211_scanparams *sp)
1363153073Ssam{
1364153073Ssam	ni->ni_esslen = sp->ssid[1];
1365153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1366153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1367153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1368153073Ssam	ni->ni_intval = sp->bintval;
1369153073Ssam	ni->ni_capinfo = sp->capinfo;
1370153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1371153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1372153073Ssam	ni->ni_fhindex = sp->fhindex;
1373153073Ssam	ni->ni_erp = sp->erp;
1374153073Ssam	ni->ni_timoff = sp->timoff;
1375153073Ssam
1376178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1377178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1378186659Ssam		if (ni->ni_ies.wme_ie != NULL)
1379186659Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1380186659Ssam		else
1381186659Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1382190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1383178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1384178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1385190391Ssam#endif
1386178354Ssam	}
1387178354Ssam
1388153073Ssam	/* NB: must be after ni_chan is setup */
1389165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1390165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1391165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1392153073Ssam}
1393153073Ssam
1394148936Ssam/*
1395148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1396148936Ssam * or probe response frame.  Note that for the driver's
1397148936Ssam * benefit we we treat this like an association so the
1398148936Ssam * driver has an opportunity to setup it's private state.
1399148936Ssam */
1400148936Ssamstruct ieee80211_node *
1401178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1402148936Ssam	const struct ieee80211_frame *wh,
1403148936Ssam	const struct ieee80211_scanparams *sp)
1404148936Ssam{
1405148936Ssam	struct ieee80211_node *ni;
1406148936Ssam
1407178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1408153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1409178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1410148936Ssam	if (ni != NULL) {
1411178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1412178354Ssam
1413153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1414188869Ssam		if (ieee80211_iserp_rateset(&ni->ni_rates))
1415188869Ssam			ni->ni_flags |= IEEE80211_NODE_ERP;
1416183251Ssam		node_setuptxparms(ni);
1417148936Ssam		if (ic->ic_newassoc != NULL)
1418148936Ssam			ic->ic_newassoc(ni, 1);
1419148936Ssam		/* XXX not right for 802.1x/WPA */
1420148936Ssam		ieee80211_node_authorize(ni);
1421148936Ssam	}
1422148936Ssam	return ni;
1423148936Ssam}
1424148936Ssam
1425148863Ssam#define	IS_CTL(wh) \
1426148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1427148863Ssam#define	IS_PSPOLL(wh) \
1428148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1429170530Ssam#define	IS_BAR(wh) \
1430170530Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1431179220Ssam#define	IS_PROBEREQ(wh) \
1432179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1433179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1434179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1435179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1436179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1437170530Ssam
1438179220Ssamstatic __inline struct ieee80211_node *
1439179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1440179220Ssam    const struct ieee80211_frame_min *wh)
1441179220Ssam{
1442179220Ssam	/* XXX 4-address frames? */
1443179220Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1444179220Ssam		return ieee80211_find_node_locked(nt, wh->i_addr1);
1445179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1446179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1447179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1448179220Ssam}
1449179220Ssam
1450138568Ssam/*
1451138568Ssam * Locate the node for sender, track state, and then pass the
1452179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1453179220Ssam * we can return NULL if the sender is not in the table.
1454138568Ssam */
1455138568Ssamstruct ieee80211_node *
1456138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1457138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1458138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1459138568Ssam#else
1460138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1461138568Ssam	const struct ieee80211_frame_min *wh)
1462138568Ssam#endif
1463138568Ssam{
1464138568Ssam	struct ieee80211_node_table *nt;
1465138568Ssam	struct ieee80211_node *ni;
1466138568Ssam
1467170530Ssam	nt = &ic->ic_sta;
1468138568Ssam	IEEE80211_NODE_LOCK(nt);
1469179220Ssam	ni = _find_rxnode(nt, wh);
1470138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1471138568Ssam
1472148863Ssam	return ni;
1473148863Ssam}
1474148863Ssam
1475148863Ssam/*
1476148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1477148863Ssam * key index as a hint to locate the node in the key
1478148863Ssam * mapping table.  If an entry is present at the key
1479148863Ssam * index we return it; otherwise do a normal lookup and
1480148863Ssam * update the mapping table if the station has a unicast
1481148863Ssam * key assigned to it.
1482148863Ssam */
1483148863Ssamstruct ieee80211_node *
1484148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1485148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1486148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1487148863Ssam	const char *func, int line)
1488148863Ssam#else
1489148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1490148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1491148863Ssam#endif
1492148863Ssam{
1493148863Ssam	struct ieee80211_node_table *nt;
1494148863Ssam	struct ieee80211_node *ni;
1495148863Ssam
1496170530Ssam	nt = &ic->ic_sta;
1497148863Ssam	IEEE80211_NODE_LOCK(nt);
1498148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1499148863Ssam		ni = nt->nt_keyixmap[keyix];
1500148863Ssam	else
1501148863Ssam		ni = NULL;
1502148863Ssam	if (ni == NULL) {
1503179220Ssam		ni = _find_rxnode(nt, wh);
1504178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1505148863Ssam			/*
1506148863Ssam			 * If the station has a unicast key cache slot
1507148863Ssam			 * assigned update the key->node mapping table.
1508148863Ssam			 */
1509148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1510148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1511148863Ssam			if (keyix < nt->nt_keyixmax &&
1512148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1513178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1514178354Ssam				    IEEE80211_MSG_NODE,
1515148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1516148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1517148863Ssam				    ieee80211_node_refcnt(ni)+1);
1518148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1519148863Ssam			}
1520148863Ssam		}
1521179220Ssam	} else {
1522179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1523179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1524179220Ssam		else
1525179220Ssam			ieee80211_ref_node(ni);
1526179220Ssam	}
1527148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1528148863Ssam
1529148863Ssam	return ni;
1530148863Ssam}
1531179220Ssam#undef IS_BCAST_PROBEREQ
1532179220Ssam#undef IS_PROBEREQ
1533170530Ssam#undef IS_BAR
1534138568Ssam#undef IS_PSPOLL
1535138568Ssam#undef IS_CTL
1536138568Ssam
1537138568Ssam/*
1538127772Ssam * Return a reference to the appropriate node for sending
1539127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1540127772Ssam */
1541127772Ssamstruct ieee80211_node *
1542138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1543178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1544178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1545138568Ssam	const char *func, int line)
1546138568Ssam#else
1547178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1548178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1549138568Ssam#endif
1550127772Ssam{
1551178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1552127772Ssam	struct ieee80211_node *ni;
1553127772Ssam
1554127772Ssam	/*
1555127772Ssam	 * The destination address should be in the node table
1556148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1557148863Ssam	 * also optimize station mode operation, all frames go
1558148863Ssam	 * to the bss node.
1559127772Ssam	 */
1560127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1561138568Ssam	IEEE80211_NODE_LOCK(nt);
1562178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1563178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1564178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1565178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1566186099Ssam	else
1567178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1568138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1569138568Ssam
1570138568Ssam	if (ni == NULL) {
1571178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1572178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1573140497Ssam			/*
1574140497Ssam			 * In adhoc mode cons up a node for the destination.
1575140497Ssam			 * Note that we need an additional reference for the
1576178354Ssam			 * caller to be consistent with
1577178354Ssam			 * ieee80211_find_node_locked.
1578140497Ssam			 */
1579178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1580140497Ssam			if (ni != NULL)
1581140497Ssam				(void) ieee80211_ref_node(ni);
1582140497Ssam		} else {
1583178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1584178354Ssam			    "no node, discard frame (%s)", __func__);
1585178354Ssam			vap->iv_stats.is_tx_nonode++;
1586127772Ssam		}
1587127772Ssam	}
1588127772Ssam	return ni;
1589127772Ssam}
1590127772Ssam
1591116742Ssamstatic void
1592138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1593116742Ssam{
1594138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1595119150Ssam
1596179641Ssam	/*
1597179641Ssam	 * NB: careful about referencing the vap as it may be
1598179641Ssam	 * gone if the last reference was held by a driver.
1599179641Ssam	 * We know the com will always be present so it's safe
1600179641Ssam	 * to use ni_ic below to reclaim resources.
1601179641Ssam	 */
1602179641Ssam#if 0
1603178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1604140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1605140766Ssam		ether_sprintf(ni->ni_macaddr),
1606138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1607179641Ssam#endif
1608179641Ssam	if (ni->ni_associd != 0) {
1609179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1610179641Ssam		if (vap->iv_aid_bitmap != NULL)
1611179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1612179641Ssam	}
1613138568Ssam	if (nt != NULL) {
1614138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1615138568Ssam		LIST_REMOVE(ni, ni_hash);
1616138568Ssam	}
1617179641Ssam	ni->ni_ic->ic_node_free(ni);
1618116742Ssam}
1619116742Ssam
1620116742Ssamvoid
1621138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1622138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1623138568Ssam#else
1624138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1625138568Ssam#endif
1626116742Ssam{
1627138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1628119150Ssam
1629138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1630178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1631140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1632138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1633138568Ssam#endif
1634148863Ssam	if (nt != NULL) {
1635148863Ssam		IEEE80211_NODE_LOCK(nt);
1636148863Ssam		if (ieee80211_node_dectestref(ni)) {
1637148863Ssam			/*
1638148863Ssam			 * Last reference, reclaim state.
1639148863Ssam			 */
1640138568Ssam			_ieee80211_free_node(ni);
1641148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1642148863Ssam		    nt->nt_keyixmap != NULL) {
1643148863Ssam			ieee80211_keyix keyix;
1644148863Ssam			/*
1645148863Ssam			 * Check for a last reference in the key mapping table.
1646148863Ssam			 */
1647148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1648148863Ssam			if (keyix < nt->nt_keyixmax &&
1649148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1650178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1651178354Ssam				    IEEE80211_MSG_NODE,
1652148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1653148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1654148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1655148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1656148863Ssam				_ieee80211_free_node(ni);
1657148863Ssam			}
1658148863Ssam		}
1659148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1660148863Ssam	} else {
1661148863Ssam		if (ieee80211_node_dectestref(ni))
1662138568Ssam			_ieee80211_free_node(ni);
1663116742Ssam	}
1664116742Ssam}
1665116742Ssam
1666138568Ssam/*
1667148863Ssam * Reclaim a unicast key and clear any key cache state.
1668148863Ssam */
1669148863Ssamint
1670148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1671148863Ssam{
1672179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1673179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1674148863Ssam	struct ieee80211_node *nikey;
1675148863Ssam	ieee80211_keyix keyix;
1676148863Ssam	int isowned, status;
1677148863Ssam
1678148863Ssam	/*
1679148863Ssam	 * NB: We must beware of LOR here; deleting the key
1680148863Ssam	 * can cause the crypto layer to block traffic updates
1681148863Ssam	 * which can generate a LOR against the node table lock;
1682148863Ssam	 * grab it here and stash the key index for our use below.
1683148863Ssam	 *
1684148863Ssam	 * Must also beware of recursion on the node table lock.
1685148863Ssam	 * When called from node_cleanup we may already have
1686148863Ssam	 * the node table lock held.  Unfortunately there's no
1687148863Ssam	 * way to separate out this path so we must do this
1688148863Ssam	 * conditionally.
1689148863Ssam	 */
1690148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1691148863Ssam	if (!isowned)
1692148863Ssam		IEEE80211_NODE_LOCK(nt);
1693179641Ssam	nikey = NULL;
1694179641Ssam	status = 1;		/* NB: success */
1695186144Ssam	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1696179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1697179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1698179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1699179641Ssam			nikey = nt->nt_keyixmap[keyix];
1700179641Ssam			nt->nt_keyixmap[keyix] = NULL;;
1701179641Ssam		}
1702179641Ssam	}
1703148863Ssam	if (!isowned)
1704178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1705148863Ssam
1706148863Ssam	if (nikey != NULL) {
1707148863Ssam		KASSERT(nikey == ni,
1708148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1709179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1710148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1711148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1712148863Ssam			ieee80211_node_refcnt(ni)-1);
1713148863Ssam		ieee80211_free_node(ni);
1714148863Ssam	}
1715148863Ssam	return status;
1716148863Ssam}
1717148863Ssam
1718148863Ssam/*
1719138568Ssam * Reclaim a node.  If this is the last reference count then
1720138568Ssam * do the normal free work.  Otherwise remove it from the node
1721138568Ssam * table and mark it gone by clearing the back-reference.
1722138568Ssam */
1723138568Ssamstatic void
1724138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1725116742Ssam{
1726148863Ssam	ieee80211_keyix keyix;
1727138568Ssam
1728148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1729148863Ssam
1730178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1731140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1732140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1733140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1734148863Ssam	/*
1735148863Ssam	 * Clear any entry in the unicast key mapping table.
1736148863Ssam	 * We need to do it here so rx lookups don't find it
1737148863Ssam	 * in the mapping table even if it's not in the hash
1738148863Ssam	 * table.  We cannot depend on the mapping table entry
1739148863Ssam	 * being cleared because the node may not be free'd.
1740148863Ssam	 */
1741148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1742148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1743148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1744178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1745188494Ssam			"%s: %p<%s> clear key map entry %u\n",
1746188494Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1747148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1748148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1749148863Ssam	}
1750138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1751138568Ssam		/*
1752138568Ssam		 * Other references are present, just remove the
1753138568Ssam		 * node from the table so it cannot be found.  When
1754138568Ssam		 * the references are dropped storage will be
1755140753Ssam		 * reclaimed.
1756138568Ssam		 */
1757138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1758138568Ssam		LIST_REMOVE(ni, ni_hash);
1759138568Ssam		ni->ni_table = NULL;		/* clear reference */
1760138568Ssam	} else
1761138568Ssam		_ieee80211_free_node(ni);
1762138568Ssam}
1763138568Ssam
1764178354Ssam/*
1765178354Ssam * Node table support.
1766178354Ssam */
1767178354Ssam
1768178354Ssamstatic void
1769178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1770178354Ssam	struct ieee80211_node_table *nt,
1771178354Ssam	const char *name, int inact, int keyixmax)
1772178354Ssam{
1773178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1774178354Ssam
1775178354Ssam	nt->nt_ic = ic;
1776178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1777178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1778178354Ssam	TAILQ_INIT(&nt->nt_node);
1779178354Ssam	nt->nt_name = name;
1780178354Ssam	nt->nt_scangen = 1;
1781178354Ssam	nt->nt_inact_init = inact;
1782178354Ssam	nt->nt_keyixmax = keyixmax;
1783178354Ssam	if (nt->nt_keyixmax > 0) {
1784186302Ssam		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1785184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1786178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1787178354Ssam		if (nt->nt_keyixmap == NULL)
1788178354Ssam			if_printf(ic->ic_ifp,
1789178354Ssam			    "Cannot allocate key index map with %u entries\n",
1790178354Ssam			    keyixmax);
1791178354Ssam	} else
1792178354Ssam		nt->nt_keyixmap = NULL;
1793178354Ssam}
1794178354Ssam
1795178354Ssamstatic void
1796178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1797178354Ssam	struct ieee80211vap *match)
1798178354Ssam{
1799178354Ssam	struct ieee80211_node *ni, *next;
1800178354Ssam
1801178354Ssam	IEEE80211_NODE_LOCK(nt);
1802178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1803178354Ssam		if (match != NULL && ni->ni_vap != match)
1804178354Ssam			continue;
1805178354Ssam		/* XXX can this happen?  if so need's work */
1806138568Ssam		if (ni->ni_associd != 0) {
1807178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1808178354Ssam
1809178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1810178354Ssam				vap->iv_auth->ia_node_leave(ni);
1811178354Ssam			if (vap->iv_aid_bitmap != NULL)
1812178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1813138568Ssam		}
1814178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1815138568Ssam		node_reclaim(nt, ni);
1816138568Ssam	}
1817178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1818178354Ssam		/*
1819178354Ssam		 * Make a separate pass to clear references to this vap
1820178354Ssam		 * held by DWDS entries.  They will not be matched above
1821178354Ssam		 * because ni_vap will point to the ap vap but we still
1822178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1823178354Ssam		 * and/or reset.
1824178354Ssam		 */
1825178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1826178354Ssam			if (ni->ni_wdsvap == match)
1827178354Ssam				ni->ni_wdsvap = NULL;
1828178354Ssam	}
1829178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1830116742Ssam}
1831116742Ssam
1832178354Ssamstatic void
1833178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1834178354Ssam{
1835178354Ssam	ieee80211_node_table_reset(nt, NULL);
1836178354Ssam	if (nt->nt_keyixmap != NULL) {
1837178354Ssam#ifdef DIAGNOSTIC
1838178354Ssam		/* XXX verify all entries are NULL */
1839178354Ssam		int i;
1840178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1841178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1842178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1843178354Ssam					nt->nt_name, i);
1844178354Ssam#endif
1845186302Ssam		free(nt->nt_keyixmap, M_80211_NODE);
1846178354Ssam		nt->nt_keyixmap = NULL;
1847178354Ssam	}
1848178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1849178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1850178354Ssam}
1851178354Ssam
1852120483Ssam/*
1853138568Ssam * Timeout inactive stations and do related housekeeping.
1854138568Ssam * Note that we cannot hold the node lock while sending a
1855138568Ssam * frame as this would lead to a LOR.  Instead we use a
1856138568Ssam * generation number to mark nodes that we've scanned and
1857138568Ssam * drop the lock and restart a scan if we have to time out
1858138568Ssam * a node.  Since we are single-threaded by virtue of
1859120483Ssam * controlling the inactivity timer we can be sure this will
1860120483Ssam * process each node only once.
1861120483Ssam */
1862138568Ssamstatic void
1863178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1864116742Ssam{
1865178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1866178354Ssam	struct ieee80211vap *vap;
1867120483Ssam	struct ieee80211_node *ni;
1868178354Ssam	int gen = 0;
1869116742Ssam
1870178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
1871154532Ssam	gen = ++nt->nt_scangen;
1872120483Ssamrestart:
1873138568Ssam	IEEE80211_NODE_LOCK(nt);
1874138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1875120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1876120483Ssam			continue;
1877120483Ssam		ni->ni_scangen = gen;
1878138568Ssam		/*
1879147788Ssam		 * Ignore entries for which have yet to receive an
1880147788Ssam		 * authentication frame.  These are transient and
1881147788Ssam		 * will be reclaimed when the last reference to them
1882147788Ssam		 * goes away (when frame xmits complete).
1883147788Ssam		 */
1884178354Ssam		vap = ni->ni_vap;
1885178354Ssam		/*
1886178354Ssam		 * Only process stations when in RUN state.  This
1887178354Ssam		 * insures, for example, that we don't timeout an
1888178354Ssam		 * inactive station during CAC.  Note that CSA state
1889178354Ssam		 * is actually handled in ieee80211_node_timeout as
1890178354Ssam		 * it applies to more than timeout processing.
1891178354Ssam		 */
1892178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
1893178354Ssam			continue;
1894178354Ssam		/* XXX can vap be NULL? */
1895178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1896178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
1897148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1898147788Ssam			continue;
1899147788Ssam		/*
1900138568Ssam		 * Free fragment if not needed anymore
1901138568Ssam		 * (last fragment older than 1s).
1902178354Ssam		 * XXX doesn't belong here, move to node_age
1903138568Ssam		 */
1904138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1905138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1906138568Ssam			m_freem(ni->ni_rxfrag[0]);
1907138568Ssam			ni->ni_rxfrag[0] = NULL;
1908138568Ssam		}
1909184277Ssam		if (ni->ni_inact > 0) {
1910172062Ssam			ni->ni_inact--;
1911184277Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1912184277Ssam			    "%s: inact %u inact_reload %u nrates %u",
1913184277Ssam			    __func__, ni->ni_inact, ni->ni_inact_reload,
1914184277Ssam			    ni->ni_rates.rs_nrates);
1915184277Ssam		}
1916140498Ssam		/*
1917140498Ssam		 * Special case ourself; we may be idle for extended periods
1918140498Ssam		 * of time and regardless reclaiming our state is wrong.
1919178354Ssam		 * XXX run ic_node_age
1920140498Ssam		 */
1921178354Ssam		if (ni == vap->iv_bss)
1922140498Ssam			continue;
1923178354Ssam		if (ni->ni_associd != 0 ||
1924178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1925178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1926119150Ssam			/*
1927178354Ssam			 * Age/drain resources held by the station.
1928138568Ssam			 */
1929178354Ssam			ic->ic_node_age(ni);
1930138568Ssam			/*
1931138568Ssam			 * Probe the station before time it out.  We
1932138568Ssam			 * send a null data frame which may not be
1933138568Ssam			 * universally supported by drivers (need it
1934138568Ssam			 * for ps-poll support so it should be...).
1935170530Ssam			 *
1936170530Ssam			 * XXX don't probe the station unless we've
1937170530Ssam			 *     received a frame from them (and have
1938170530Ssam			 *     some idea of the rates they are capable
1939170530Ssam			 *     of); this will get fixed more properly
1940170530Ssam			 *     soon with better handling of the rate set.
1941138568Ssam			 */
1942178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1943172062Ssam			    (0 < ni->ni_inact &&
1944178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
1945170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1946178354Ssam				IEEE80211_NOTE(vap,
1947148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1948148320Ssam				    ni, "%s",
1949148320Ssam				    "probe station due to inactivity");
1950148582Ssam				/*
1951148582Ssam				 * Grab a reference before unlocking the table
1952148582Ssam				 * so the node cannot be reclaimed before we
1953148582Ssam				 * send the frame. ieee80211_send_nulldata
1954148582Ssam				 * understands we've done this and reclaims the
1955148582Ssam				 * ref for us as needed.
1956148582Ssam				 */
1957148582Ssam				ieee80211_ref_node(ni);
1958138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1959148301Ssam				ieee80211_send_nulldata(ni);
1960138568Ssam				/* XXX stat? */
1961138568Ssam				goto restart;
1962138568Ssam			}
1963138568Ssam		}
1964178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1965172062Ssam		    ni->ni_inact <= 0) {
1966178354Ssam			IEEE80211_NOTE(vap,
1967148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1968148320Ssam			    "station timed out due to inactivity "
1969148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1970138568Ssam			/*
1971138568Ssam			 * Send a deauthenticate frame and drop the station.
1972138568Ssam			 * This is somewhat complicated due to reference counts
1973138568Ssam			 * and locking.  At this point a station will typically
1974138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1975138568Ssam			 * will do a "free" of the node which will drop the
1976138568Ssam			 * reference count.  But in the meantime a reference
1977138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1978138568Ssam			 * of the node will happen either after the tx is
1979138568Ssam			 * completed or by ieee80211_node_leave.
1980120483Ssam			 *
1981138568Ssam			 * Separately we must drop the node lock before sending
1982170530Ssam			 * in case the driver takes a lock, as this can result
1983170530Ssam			 * in a LOR between the node lock and the driver lock.
1984119150Ssam			 */
1985172229Ssam			ieee80211_ref_node(ni);
1986138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1987138568Ssam			if (ni->ni_associd != 0) {
1988178354Ssam				IEEE80211_SEND_MGMT(ni,
1989138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1990138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1991138568Ssam			}
1992178354Ssam			ieee80211_node_leave(ni);
1993172229Ssam			ieee80211_free_node(ni);
1994178354Ssam			vap->iv_stats.is_node_timeout++;
1995120483Ssam			goto restart;
1996120483Ssam		}
1997116742Ssam	}
1998138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1999138568Ssam
2000178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2001170530Ssam}
2002138568Ssam
2003178354Ssam/*
2004178354Ssam * Aggressively reclaim resources.  This should be used
2005178354Ssam * only in a critical situation to reclaim mbuf resources.
2006178354Ssam */
2007170530Ssamvoid
2008178354Ssamieee80211_drain(struct ieee80211com *ic)
2009178354Ssam{
2010178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2011178354Ssam	struct ieee80211vap *vap;
2012178354Ssam	struct ieee80211_node *ni;
2013178354Ssam
2014178354Ssam	IEEE80211_NODE_LOCK(nt);
2015178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2016178354Ssam		/*
2017178354Ssam		 * Ignore entries for which have yet to receive an
2018178354Ssam		 * authentication frame.  These are transient and
2019178354Ssam		 * will be reclaimed when the last reference to them
2020178354Ssam		 * goes away (when frame xmits complete).
2021178354Ssam		 */
2022178354Ssam		vap = ni->ni_vap;
2023178354Ssam		/*
2024178354Ssam		 * Only process stations when in RUN state.  This
2025178354Ssam		 * insures, for example, that we don't timeout an
2026178354Ssam		 * inactive station during CAC.  Note that CSA state
2027178354Ssam		 * is actually handled in ieee80211_node_timeout as
2028178354Ssam		 * it applies to more than timeout processing.
2029178354Ssam		 */
2030178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2031178354Ssam			continue;
2032178354Ssam		/* XXX can vap be NULL? */
2033178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2034178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2035178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2036178354Ssam			continue;
2037178354Ssam		/*
2038178354Ssam		 * Free fragments.
2039178354Ssam		 * XXX doesn't belong here, move to node_drain
2040178354Ssam		 */
2041178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2042178354Ssam			m_freem(ni->ni_rxfrag[0]);
2043178354Ssam			ni->ni_rxfrag[0] = NULL;
2044178354Ssam		}
2045178354Ssam		/*
2046178354Ssam		 * Drain resources held by the station.
2047178354Ssam		 */
2048178354Ssam		ic->ic_node_drain(ni);
2049178354Ssam	}
2050178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2051178354Ssam}
2052178354Ssam
2053178354Ssam/*
2054178354Ssam * Per-ieee80211com inactivity timer callback.
2055178354Ssam */
2056178354Ssamvoid
2057170530Ssamieee80211_node_timeout(void *arg)
2058170530Ssam{
2059170530Ssam	struct ieee80211com *ic = arg;
2060170530Ssam
2061178354Ssam	/*
2062178354Ssam	 * Defer timeout processing if a channel switch is pending.
2063178354Ssam	 * We typically need to be mute so not doing things that
2064178354Ssam	 * might generate frames is good to handle in one place.
2065178354Ssam	 * Supressing the station timeout processing may extend the
2066178354Ssam	 * lifetime of inactive stations (by not decrementing their
2067178354Ssam	 * idle counters) but this should be ok unless the CSA is
2068178354Ssam	 * active for an unusually long time.
2069178354Ssam	 */
2070178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2071178354Ssam		ieee80211_scan_timeout(ic);
2072178354Ssam		ieee80211_timeout_stations(ic);
2073170530Ssam
2074178354Ssam		IEEE80211_LOCK(ic);
2075178354Ssam		ieee80211_erp_timeout(ic);
2076178354Ssam		ieee80211_ht_timeout(ic);
2077178354Ssam		IEEE80211_UNLOCK(ic);
2078178354Ssam	}
2079170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2080170530Ssam		ieee80211_node_timeout, ic);
2081116742Ssam}
2082116742Ssam
2083116742Ssamvoid
2084178354Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2085178354Ssam	ieee80211_iter_func *f, void *arg)
2086116742Ssam{
2087116742Ssam	struct ieee80211_node *ni;
2088138568Ssam	u_int gen;
2089116742Ssam
2090178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2091154532Ssam	gen = ++nt->nt_scangen;
2092138568Ssamrestart:
2093138568Ssam	IEEE80211_NODE_LOCK(nt);
2094138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2095138568Ssam		if (ni->ni_scangen != gen) {
2096138568Ssam			ni->ni_scangen = gen;
2097138568Ssam			(void) ieee80211_ref_node(ni);
2098138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2099138568Ssam			(*f)(arg, ni);
2100138568Ssam			ieee80211_free_node(ni);
2101138568Ssam			goto restart;
2102138568Ssam		}
2103138568Ssam	}
2104138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2105138568Ssam
2106178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2107116742Ssam}
2108138568Ssam
2109138568Ssamvoid
2110138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2111138568Ssam{
2112138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2113138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2114138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2115138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2116138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2117138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2118138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2119167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2120167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2121167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2122138568Ssam		ni->ni_rxfragstamp);
2123170530Ssam	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2124178354Ssam		ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
2125170530Ssam		ni->ni_intval, ni->ni_capinfo);
2126138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2127138568Ssam		ether_sprintf(ni->ni_bssid),
2128138568Ssam		ni->ni_esslen, ni->ni_essid,
2129138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2130184277Ssam	printf("\tinact %u inact_reload %u txrate %u\n",
2131184277Ssam		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2132170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2133170530Ssam		ni->ni_htcap, ni->ni_htparam,
2134170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2135170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2136170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2137138568Ssam}
2138138568Ssam
2139138568Ssamvoid
2140138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2141138568Ssam{
2142138568Ssam	ieee80211_iterate_nodes(nt,
2143138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2144138568Ssam}
2145138568Ssam
2146179642Ssamstatic void
2147179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2148172211Ssam{
2149178354Ssam	struct ieee80211vap *vap;
2150178354Ssam
2151178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2152178354Ssam
2153178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2154178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2155178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2156172211Ssam}
2157172211Ssam
2158179642Ssamvoid
2159179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2160179642Ssam{
2161179642Ssam	IEEE80211_LOCK(ic);
2162179642Ssam	ieee80211_notify_erp_locked(ic);
2163179642Ssam	IEEE80211_UNLOCK(ic);
2164179642Ssam}
2165179642Ssam
2166138568Ssam/*
2167138568Ssam * Handle a station joining an 11g network.
2168138568Ssam */
2169138568Ssamstatic void
2170178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2171138568Ssam{
2172178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2173138568Ssam
2174173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2175173273Ssam
2176138568Ssam	/*
2177138568Ssam	 * Station isn't capable of short slot time.  Bump
2178138568Ssam	 * the count of long slot time stations and disable
2179138568Ssam	 * use of short slot time.  Note that the actual switch
2180138568Ssam	 * over to long slot time use may not occur until the
2181138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2182138568Ssam	 */
2183138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2184138568Ssam		ic->ic_longslotsta++;
2185178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2186172211Ssam		    "station needs long slot time, count %d",
2187172211Ssam		    ic->ic_longslotsta);
2188138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2189170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2190170530Ssam			/*
2191170530Ssam			 * Don't force slot time when switched to turbo
2192170530Ssam			 * mode as non-ERP stations won't be present; this
2193170530Ssam			 * need only be done when on the normal G channel.
2194170530Ssam			 */
2195170530Ssam			ieee80211_set_shortslottime(ic, 0);
2196170530Ssam		}
2197138568Ssam	}
2198138568Ssam	/*
2199138568Ssam	 * If the new station is not an ERP station
2200138568Ssam	 * then bump the counter and enable protection
2201138568Ssam	 * if configured.
2202138568Ssam	 */
2203178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2204138568Ssam		ic->ic_nonerpsta++;
2205178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2206172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2207172211Ssam		    ic->ic_nonerpsta);
2208138568Ssam		/*
2209138568Ssam		 * If station does not support short preamble
2210138568Ssam		 * then we must enable use of Barker preamble.
2211138568Ssam		 */
2212138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2213178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2214172211Ssam			    "%s", "station needs long preamble");
2215138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2216138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2217138568Ssam		}
2218172211Ssam		/*
2219178354Ssam		 * If protection is configured and this is the first
2220178354Ssam		 * indication we should use protection, enable it.
2221172211Ssam		 */
2222172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2223172211Ssam		    ic->ic_nonerpsta == 1 &&
2224172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2225178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2226172211Ssam			    "%s: enable use of protection\n", __func__);
2227172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2228179642Ssam			ieee80211_notify_erp_locked(ic);
2229172211Ssam		}
2230138568Ssam	} else
2231138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2232138568Ssam}
2233138568Ssam
2234138568Ssamvoid
2235178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2236138568Ssam{
2237178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2238178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2239138568Ssam	int newassoc;
2240138568Ssam
2241138568Ssam	if (ni->ni_associd == 0) {
2242170530Ssam		uint16_t aid;
2243138568Ssam
2244178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2245138568Ssam		/*
2246138568Ssam		 * It would be good to search the bitmap
2247138568Ssam		 * more efficiently, but this will do for now.
2248138568Ssam		 */
2249178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2250178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2251138568Ssam				break;
2252138568Ssam		}
2253178354Ssam		if (aid >= vap->iv_max_aid) {
2254179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2255178354Ssam			ieee80211_node_leave(ni);
2256138568Ssam			return;
2257138568Ssam		}
2258138568Ssam		ni->ni_associd = aid | 0xc000;
2259173273Ssam		ni->ni_jointime = time_uptime;
2260178354Ssam		IEEE80211_LOCK(ic);
2261178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2262178354Ssam		vap->iv_sta_assoc++;
2263138568Ssam		ic->ic_sta_assoc++;
2264173273Ssam
2265173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2266173273Ssam			ieee80211_ht_node_join(ni);
2267170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2268170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2269178354Ssam			ieee80211_node_join_11g(ni);
2270173273Ssam		IEEE80211_UNLOCK(ic);
2271173273Ssam
2272173273Ssam		newassoc = 1;
2273138568Ssam	} else
2274138568Ssam		newassoc = 0;
2275138568Ssam
2276178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2277183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2278139523Ssam	    IEEE80211_NODE_AID(ni),
2279139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2280139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2281139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2282170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2283170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2284182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2285173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2286183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2287183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2288183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2289178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2290170530Ssam		", fast-frames" : "",
2291178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2292170530Ssam		", turbo" : ""
2293139523Ssam	);
2294138568Ssam
2295183251Ssam	node_setuptxparms(ni);
2296138568Ssam	/* give driver a chance to setup state like ni_txrate */
2297139524Ssam	if (ic->ic_newassoc != NULL)
2298148307Ssam		ic->ic_newassoc(ni, newassoc);
2299178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2300138568Ssam	/* tell the authenticator about new station */
2301178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2302178354Ssam		vap->iv_auth->ia_node_join(ni);
2303178354Ssam	ieee80211_notify_node_join(ni,
2304173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2305138568Ssam}
2306138568Ssam
2307172211Ssamstatic void
2308172211Ssamdisable_protection(struct ieee80211com *ic)
2309172211Ssam{
2310172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2311172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2312172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2313172211Ssam	   ic->ic_flags_ext));
2314172211Ssam
2315172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2316172211Ssam	/* XXX verify mode? */
2317172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2318172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2319172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2320172211Ssam	}
2321179642Ssam	ieee80211_notify_erp_locked(ic);
2322172211Ssam}
2323172211Ssam
2324138568Ssam/*
2325138568Ssam * Handle a station leaving an 11g network.
2326138568Ssam */
2327138568Ssamstatic void
2328178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2329138568Ssam{
2330178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2331138568Ssam
2332173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2333173273Ssam
2334170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2335178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2336178354Ssam	      ic->ic_bsschan->ic_flags));
2337138568Ssam
2338138568Ssam	/*
2339138568Ssam	 * If a long slot station do the slot time bookkeeping.
2340138568Ssam	 */
2341138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2342138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2343138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2344138568Ssam		ic->ic_longslotsta--;
2345178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2346172211Ssam		    "long slot time station leaves, count now %d",
2347172211Ssam		    ic->ic_longslotsta);
2348138568Ssam		if (ic->ic_longslotsta == 0) {
2349138568Ssam			/*
2350138568Ssam			 * Re-enable use of short slot time if supported
2351138568Ssam			 * and not operating in IBSS mode (per spec).
2352138568Ssam			 */
2353138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2354138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2355178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2356178354Ssam				    IEEE80211_MSG_ASSOC,
2357138568Ssam				    "%s: re-enable use of short slot time\n",
2358138568Ssam				    __func__);
2359138568Ssam				ieee80211_set_shortslottime(ic, 1);
2360138568Ssam			}
2361138568Ssam		}
2362138568Ssam	}
2363138568Ssam	/*
2364138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2365138568Ssam	 */
2366138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2367138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2368138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2369138568Ssam		ic->ic_nonerpsta--;
2370178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2371172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2372172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2373172211Ssam			" (non-ERP sta present)" : "");
2374172211Ssam		if (ic->ic_nonerpsta == 0 &&
2375172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2376178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2377138568Ssam				"%s: disable use of protection\n", __func__);
2378172211Ssam			disable_protection(ic);
2379138568Ssam		}
2380138568Ssam	}
2381138568Ssam}
2382138568Ssam
2383138568Ssam/*
2384172211Ssam * Time out presence of an overlapping bss with non-ERP
2385172211Ssam * stations.  When operating in hostap mode we listen for
2386172211Ssam * beacons from other stations and if we identify a non-ERP
2387172211Ssam * station is present we enable protection.  To identify
2388172211Ssam * when all non-ERP stations are gone we time out this
2389172211Ssam * condition.
2390172211Ssam */
2391172211Ssamstatic void
2392172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2393172211Ssam{
2394172211Ssam
2395172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2396172211Ssam
2397172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2398172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2399178354Ssam#if 0
2400178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2401178354Ssam		    "%s", "age out non-ERP sta present on channel");
2402178354Ssam#endif
2403172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2404172211Ssam		if (ic->ic_nonerpsta == 0)
2405172211Ssam			disable_protection(ic);
2406172211Ssam	}
2407172211Ssam}
2408172211Ssam
2409172211Ssam/*
2410138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2411138568Ssam * when operating as an ap.
2412138568Ssam */
2413138568Ssamvoid
2414178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2415138568Ssam{
2416178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2417178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2418140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2419138568Ssam
2420178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2421172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2422138568Ssam
2423178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2424178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2425138568Ssam	/*
2426138568Ssam	 * If node wasn't previously associated all
2427138568Ssam	 * we need to do is reclaim the reference.
2428138568Ssam	 */
2429138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2430138568Ssam	if (ni->ni_associd == 0)
2431138568Ssam		goto done;
2432138568Ssam	/*
2433138568Ssam	 * Tell the authenticator the station is leaving.
2434138568Ssam	 * Note that we must do this before yanking the
2435138568Ssam	 * association id as the authenticator uses the
2436138568Ssam	 * associd to locate it's state block.
2437138568Ssam	 */
2438178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2439178354Ssam		vap->iv_auth->ia_node_leave(ni);
2440173273Ssam
2441173273Ssam	IEEE80211_LOCK(ic);
2442178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2443138568Ssam	ni->ni_associd = 0;
2444178354Ssam	vap->iv_sta_assoc--;
2445138568Ssam	ic->ic_sta_assoc--;
2446138568Ssam
2447173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2448173273Ssam		ieee80211_ht_node_leave(ni);
2449170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2450170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2451178354Ssam		ieee80211_node_leave_11g(ni);
2452173273Ssam	IEEE80211_UNLOCK(ic);
2453138568Ssam	/*
2454138568Ssam	 * Cleanup station state.  In particular clear various
2455138568Ssam	 * state that might otherwise be reused if the node
2456138568Ssam	 * is reused before the reference count goes to zero
2457138568Ssam	 * (and memory is reclaimed).
2458138568Ssam	 */
2459178354Ssam	ieee80211_sta_leave(ni);
2460138568Ssamdone:
2461140499Ssam	/*
2462140499Ssam	 * Remove the node from any table it's recorded in and
2463140499Ssam	 * drop the caller's reference.  Removal from the table
2464140499Ssam	 * is important to insure the node is not reprocessed
2465140499Ssam	 * for inactivity.
2466140499Ssam	 */
2467140499Ssam	if (nt != NULL) {
2468140499Ssam		IEEE80211_NODE_LOCK(nt);
2469140499Ssam		node_reclaim(nt, ni);
2470140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2471140499Ssam	} else
2472140499Ssam		ieee80211_free_node(ni);
2473138568Ssam}
2474138568Ssam
2475178354Ssamstruct rssiinfo {
2476178354Ssam	struct ieee80211vap *vap;
2477178354Ssam	int	rssi_samples;
2478178354Ssam	uint32_t rssi_total;
2479178354Ssam};
2480178354Ssam
2481178354Ssamstatic void
2482178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni)
2483178354Ssam{
2484178354Ssam	struct rssiinfo *info = arg;
2485178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2486178354Ssam	int8_t rssi;
2487178354Ssam
2488178354Ssam	if (info->vap != vap)
2489178354Ssam		return;
2490178354Ssam	/* only associated stations */
2491178354Ssam	if (ni->ni_associd == 0)
2492178354Ssam		return;
2493178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2494178354Ssam	if (rssi != 0) {
2495178354Ssam		info->rssi_samples++;
2496178354Ssam		info->rssi_total += rssi;
2497178354Ssam	}
2498178354Ssam}
2499178354Ssam
2500178354Ssamstatic void
2501178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2502178354Ssam{
2503178354Ssam	struct rssiinfo *info = arg;
2504178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2505178354Ssam	int8_t rssi;
2506178354Ssam
2507178354Ssam	if (info->vap != vap)
2508178354Ssam		return;
2509178354Ssam	/* only neighbors */
2510178354Ssam	/* XXX check bssid */
2511178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2512178354Ssam		return;
2513178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2514178354Ssam	if (rssi != 0) {
2515178354Ssam		info->rssi_samples++;
2516178354Ssam		info->rssi_total += rssi;
2517178354Ssam	}
2518178354Ssam}
2519178354Ssam
2520170530Ssamint8_t
2521178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2522138568Ssam{
2523138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2524178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2525178354Ssam	struct rssiinfo info;
2526138568Ssam
2527178354Ssam	info.rssi_total = 0;
2528178354Ssam	info.rssi_samples = 0;
2529178354Ssam	info.vap = vap;
2530178354Ssam	switch (vap->iv_opmode) {
2531138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2532138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2533178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2534178354Ssam		break;
2535138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2536178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2537138568Ssam		break;
2538138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2539138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2540138568Ssam	default:
2541178354Ssam		if (vap->iv_bss != NULL)
2542178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2543178354Ssam		info.rssi_samples = 1;
2544138568Ssam		break;
2545138568Ssam	}
2546178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2547138568Ssam#undef NZ
2548138568Ssam}
2549138568Ssam
2550170530Ssamvoid
2551178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2552138568Ssam{
2553138568Ssam
2554178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2555170530Ssam		return;
2556178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2557170530Ssam	/* for non-station mode return avg'd rssi accounting */
2558178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2559178354Ssam		*rssi = ieee80211_getrssi(vap);
2560138568Ssam}
2561