ieee80211_node.c revision 184279
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3178354Ssam * Copyright (c) 2002-2008 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 184279 2008-10-25 23:44:25Z 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>
46178354Ssam#include <net80211/ieee80211_wds.h>
47116742Ssam
48116742Ssam#include <net/bpf.h>
49116742Ssam
50147221Ssam/*
51147221Ssam * Association id's are managed with a bit vector.
52147221Ssam */
53178354Ssam#define	IEEE80211_AID_SET(_vap, b) \
54178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
55178354Ssam		(1 << (IEEE80211_AID(b) % 32)))
56178354Ssam#define	IEEE80211_AID_CLR(_vap, b) \
57178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
58178354Ssam		~(1 << (IEEE80211_AID(b) % 32)))
59178354Ssam#define	IEEE80211_AID_ISSET(_vap, b) \
60178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
61147221Ssam
62159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
63159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
64159139Sdds#else
65159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
66159139Sdds#endif
67159139Sdds
68170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
69170530Ssam
70179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *,
71179643Ssam	const uint8_t [IEEE80211_ADDR_LEN]);
72138568Ssamstatic void node_cleanup(struct ieee80211_node *);
73138568Ssamstatic void node_free(struct ieee80211_node *);
74178354Ssamstatic void node_age(struct ieee80211_node *);
75170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
76170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
77178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *,
78178354Ssam	struct ieee80211_mimo_info *);
79116742Ssam
80138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
81120104Ssam
82138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
83148863Ssam	struct ieee80211_node_table *nt, const char *name,
84170530Ssam	int inact, int keymaxix);
85178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *,
86178354Ssam	struct ieee80211vap *);
87178354Ssamstatic void ieee80211_node_reclaim(struct ieee80211_node *);
88138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
89172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
90138568Ssam
91127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
92178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
93120481Ssam
94116742Ssamvoid
95138568Ssamieee80211_node_attach(struct ieee80211com *ic)
96116742Ssam{
97178354Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
98178354Ssam		IEEE80211_INACT_INIT, ic->ic_max_keyix);
99178354Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
100178354Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
101178354Ssam		ieee80211_node_timeout, ic);
102116742Ssam
103138568Ssam	ic->ic_node_alloc = node_alloc;
104138568Ssam	ic->ic_node_free = node_free;
105138568Ssam	ic->ic_node_cleanup = node_cleanup;
106178354Ssam	ic->ic_node_age = node_age;
107178354Ssam	ic->ic_node_drain = node_age;		/* NB: same as age */
108138568Ssam	ic->ic_node_getrssi = node_getrssi;
109170530Ssam	ic->ic_node_getsignal = node_getsignal;
110178354Ssam	ic->ic_node_getmimoinfo = node_getmimoinfo;
111138568Ssam
112178354Ssam	/*
113178354Ssam	 * Set flags to be propagated to all vap's;
114178354Ssam	 * these define default behaviour/configuration.
115178354Ssam	 */
116178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
117178354Ssam}
118138568Ssam
119178354Ssamvoid
120178354Ssamieee80211_node_detach(struct ieee80211com *ic)
121178354Ssam{
122170530Ssam
123178354Ssam	callout_drain(&ic->ic_inact);
124178354Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
125178354Ssam}
126172062Ssam
127178354Ssamvoid
128178354Ssamieee80211_node_vattach(struct ieee80211vap *vap)
129178354Ssam{
130178354Ssam	/* NB: driver can override */
131178354Ssam	vap->iv_max_aid = IEEE80211_AID_DEF;
132178354Ssam
133178354Ssam	/* default station inactivity timer setings */
134178354Ssam	vap->iv_inact_init = IEEE80211_INACT_INIT;
135178354Ssam	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
136178354Ssam	vap->iv_inact_run = IEEE80211_INACT_RUN;
137178354Ssam	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
138184277Ssam
139184277Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
140184277Ssam	    "%s: init %u auth %u run %u probe %u\n", __func__,
141184277Ssam	    vap->iv_inact_init, vap->iv_inact_auth,
142184277Ssam	    vap->iv_inact_run, vap->iv_inact_probe);
143148863Ssam}
144148863Ssam
145148863Ssamvoid
146178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap)
147148863Ssam{
148178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
149178354Ssam		/* XXX should we allow max aid to be zero? */
150178354Ssam		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
151178354Ssam			vap->iv_max_aid = IEEE80211_AID_MIN;
152178354Ssam			if_printf(vap->iv_ifp,
153178354Ssam			    "WARNING: max aid too small, changed to %d\n",
154178354Ssam			    vap->iv_max_aid);
155178354Ssam		}
156184210Sdes		MALLOC(vap->iv_aid_bitmap, uint32_t *,
157184210Sdes			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
158178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
159178354Ssam		if (vap->iv_aid_bitmap == NULL) {
160178354Ssam			/* XXX no way to recover */
161178354Ssam			printf("%s: no memory for AID bitmap, max aid %d!\n",
162178354Ssam			    __func__, vap->iv_max_aid);
163178354Ssam			vap->iv_max_aid = 0;
164178354Ssam		}
165138568Ssam	}
166138568Ssam
167178354Ssam	ieee80211_reset_bss(vap);
168118887Ssam
169178354Ssam	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
170116742Ssam}
171116742Ssam
172116742Ssamvoid
173178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap)
174116742Ssam{
175178354Ssam	struct ieee80211com *ic = vap->iv_ic;
176116742Ssam
177178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
178178354Ssam	if (vap->iv_bss != NULL) {
179178354Ssam		ieee80211_free_node(vap->iv_bss);
180178354Ssam		vap->iv_bss = NULL;
181138568Ssam	}
182178354Ssam	if (vap->iv_aid_bitmap != NULL) {
183184210Sdes		FREE(vap->iv_aid_bitmap, M_80211_NODE);
184178354Ssam		vap->iv_aid_bitmap = NULL;
185138568Ssam	}
186116742Ssam}
187116742Ssam
188138568Ssam/*
189138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
190138568Ssam */
191138568Ssam
192138568Ssamvoid
193148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
194138568Ssam{
195184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
196184277Ssam
197138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
198184277Ssam	ni->ni_inact_reload = vap->iv_inact_run;
199172062Ssam	ni->ni_inact = ni->ni_inact_reload;
200184277Ssam
201184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
202184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
203138568Ssam}
204138568Ssam
205138568Ssamvoid
206148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
207138568Ssam{
208184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
209184277Ssam
210138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
211184277Ssam	ni->ni_inact_reload = vap->iv_inact_auth;
212172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
213172062Ssam		ni->ni_inact = ni->ni_inact_reload;
214184277Ssam
215184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
216184277Ssam	    "%s: inact_reload %u inact %u", __func__,
217184277Ssam	    ni->ni_inact_reload, ni->ni_inact);
218138568Ssam}
219138568Ssam
220116742Ssam/*
221183251Ssam * Fix tx parameters for a node according to ``association state''.
222183251Ssam */
223183251Ssamstatic void
224183251Ssamnode_setuptxparms(struct ieee80211_node *ni)
225183251Ssam{
226183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
227183251Ssam
228183251Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
229183251Ssam		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
230183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NA];
231183251Ssam		else
232183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NG];
233183251Ssam	} else {				/* legacy rate handling */
234183251Ssam		if (IEEE80211_IS_CHAN_A(ni->ni_chan))
235183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11A];
236183251Ssam		else if (ni->ni_flags & IEEE80211_NODE_ERP)
237183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11G];
238183251Ssam		else
239183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11B];
240183251Ssam	}
241183251Ssam}
242183251Ssam
243183251Ssam/*
244138568Ssam * Set/change the channel.  The rate set is also updated as
245138568Ssam * to insure a consistent view by drivers.
246178354Ssam * XXX should be private but hostap needs it to deal with CSA
247138568Ssam */
248178354Ssamvoid
249178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni,
250178354Ssam	struct ieee80211_channel *chan)
251138568Ssam{
252178354Ssam	struct ieee80211com *ic = ni->ni_ic;
253183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
254183251Ssam	enum ieee80211_phymode mode;
255170530Ssam
256178354Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
257178354Ssam
258138568Ssam	ni->ni_chan = chan;
259183251Ssam	mode = ieee80211_chan2mode(chan);
260170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
261170530Ssam		/*
262170530Ssam		 * XXX Gotta be careful here; the rate set returned by
263170530Ssam		 * ieee80211_get_suprates is actually any HT rate
264170530Ssam		 * set so blindly copying it will be bad.  We must
265170530Ssam		 * install the legacy rate est in ni_rates and the
266170530Ssam		 * HT rate set in ni_htrates.
267170530Ssam		 */
268170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
269183251Ssam		/*
270183251Ssam		 * Setup bss tx parameters based on operating mode.  We
271183251Ssam		 * use legacy rates when operating in a mixed HT+non-HT bss
272183251Ssam		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
273183251Ssam		 */
274183251Ssam		if (mode == IEEE80211_MODE_11NA &&
275183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
276183251Ssam			mode = IEEE80211_MODE_11A;
277183251Ssam		else if (mode == IEEE80211_MODE_11NG &&
278183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
279183251Ssam			mode = IEEE80211_MODE_11G;
280183251Ssam		if (mode == IEEE80211_MODE_11G &&
281183251Ssam		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
282183251Ssam			mode = IEEE80211_MODE_11B;
283170530Ssam	}
284183251Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
285165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
286138568Ssam}
287138568Ssam
288141658Ssamstatic __inline void
289141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
290141658Ssam{
291141658Ssam	/* propagate useful state */
292141658Ssam	nbss->ni_authmode = obss->ni_authmode;
293141658Ssam	nbss->ni_txpower = obss->ni_txpower;
294141658Ssam	nbss->ni_vlan = obss->ni_vlan;
295141658Ssam	/* XXX statistics? */
296178354Ssam	/* XXX legacy WDS bssid? */
297141658Ssam}
298141658Ssam
299116742Ssamvoid
300178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
301116742Ssam{
302178354Ssam	struct ieee80211com *ic = vap->iv_ic;
303116742Ssam	struct ieee80211_node *ni;
304116742Ssam
305178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
306178354Ssam		"%s: creating ibss on channel %u\n", __func__,
307178354Ssam		ieee80211_chan2ieee(ic, chan));
308138568Ssam
309178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
310140753Ssam	if (ni == NULL) {
311140753Ssam		/* XXX recovery? */
312138568Ssam		return;
313138568Ssam	}
314178354Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
315178354Ssam	ni->ni_esslen = vap->iv_des_ssid[0].len;
316178354Ssam	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
317178354Ssam	if (vap->iv_bss != NULL)
318178354Ssam		copy_bss(ni, vap->iv_bss);
319148843Ssam	ni->ni_intval = ic->ic_bintval;
320178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
321116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
322116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
323116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
324116742Ssam		ni->ni_fhindex = 1;
325116742Ssam	}
326178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
327178354Ssam		vap->iv_flags |= IEEE80211_F_SIBSS;
328138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
329178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
330178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
331167282Ssam		else {
332167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
333167282Ssam			/* clear group bit, add local bit */
334167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
335167282Ssam		}
336178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
337178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
338178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
339153403Ssam		else
340153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
341138568Ssam	}
342138568Ssam	/*
343138568Ssam	 * Fix the channel and related attributes.
344138568Ssam	 */
345178354Ssam	/* clear DFS CAC state on previous channel */
346178354Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
347178354Ssam	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
348178354Ssam	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
349178354Ssam		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
350170530Ssam	ic->ic_bsschan = chan;
351178354Ssam	ieee80211_node_set_chan(ni, chan);
352170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
353138568Ssam	/*
354178354Ssam	 * Do mode-specific setup.
355138568Ssam	 */
356170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
357170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
358170530Ssam			/*
359178354Ssam			 * Use a mixed 11b/11g basic rate set.
360170530Ssam			 */
361178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
362178354Ssam			    IEEE80211_MODE_11G);
363178354Ssam			if (vap->iv_flags & IEEE80211_F_PUREG) {
364178354Ssam				/*
365178354Ssam				 * Also mark OFDM rates basic so 11b
366178354Ssam				 * stations do not join (WiFi compliance).
367178354Ssam				 */
368178354Ssam				ieee80211_addbasicrates(&ni->ni_rates,
369178354Ssam				    IEEE80211_MODE_11A);
370178354Ssam			}
371170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
372170530Ssam			/*
373170530Ssam			 * Force pure 11b rate set.
374170530Ssam			 */
375178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
376170530Ssam				IEEE80211_MODE_11B);
377170530Ssam		}
378170530Ssam	}
379138568Ssam
380170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
381116742Ssam}
382116742Ssam
383170530Ssam/*
384170530Ssam * Reset bss state on transition to the INIT state.
385170530Ssam * Clear any stations from the table (they have been
386170530Ssam * deauth'd) and reset the bss node (clears key, rate
387170530Ssam * etc. state).
388170530Ssam */
389138568Ssamvoid
390178354Ssamieee80211_reset_bss(struct ieee80211vap *vap)
391138568Ssam{
392178354Ssam	struct ieee80211com *ic = vap->iv_ic;
393138568Ssam	struct ieee80211_node *ni, *obss;
394138568Ssam
395178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
396178354Ssam	/* XXX multi-bss: wrong */
397170530Ssam	ieee80211_reset_erp(ic);
398140753Ssam
399178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
400138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
401178354Ssam	obss = vap->iv_bss;
402178354Ssam	vap->iv_bss = ieee80211_ref_node(ni);
403141658Ssam	if (obss != NULL) {
404141658Ssam		copy_bss(ni, obss);
405148843Ssam		ni->ni_intval = ic->ic_bintval;
406138568Ssam		ieee80211_free_node(obss);
407178354Ssam	} else
408178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
409138568Ssam}
410138568Ssam
411170530Ssamstatic int
412170530Ssammatch_ssid(const struct ieee80211_node *ni,
413170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
414170530Ssam{
415170530Ssam	int i;
416148432Ssam
417170530Ssam	for (i = 0; i < nssid; i++) {
418170530Ssam		if (ni->ni_esslen == ssids[i].len &&
419170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
420170530Ssam			return 1;
421170530Ssam	}
422170530Ssam	return 0;
423170530Ssam}
424170530Ssam
425170530Ssam/*
426170530Ssam * Test a node for suitability/compatibility.
427170530Ssam */
428127767Ssamstatic int
429178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
430127767Ssam{
431178354Ssam	struct ieee80211com *ic = ni->ni_ic;
432170530Ssam        uint8_t rate;
433170530Ssam
434170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
435170530Ssam		return 0;
436178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
437170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
438170530Ssam			return 0;
439170530Ssam	} else {
440170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
441170530Ssam			return 0;
442170530Ssam	}
443178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
444170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
445170530Ssam			return 0;
446170530Ssam	} else {
447170530Ssam		/* XXX does this mean privacy is supported or required? */
448170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
449170530Ssam			return 0;
450170530Ssam	}
451170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
452170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
453170530Ssam	if (rate & IEEE80211_RATE_BASIC)
454170530Ssam		return 0;
455178354Ssam	if (vap->iv_des_nssid != 0 &&
456178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
457170530Ssam		return 0;
458178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
459178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
460170530Ssam		return 0;
461170530Ssam	return 1;
462170530Ssam}
463170530Ssam
464170530Ssam#ifdef IEEE80211_DEBUG
465170530Ssam/*
466170530Ssam * Display node suitability/compatibility.
467170530Ssam */
468170530Ssamstatic void
469178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
470170530Ssam{
471178354Ssam	struct ieee80211com *ic = ni->ni_ic;
472170530Ssam        uint8_t rate;
473127767Ssam        int fail;
474127767Ssam
475127767Ssam	fail = 0;
476127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
477127767Ssam		fail |= 0x01;
478178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
479127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
480127767Ssam			fail |= 0x02;
481127767Ssam	} else {
482127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
483127767Ssam			fail |= 0x02;
484127767Ssam	}
485178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
486127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
487127767Ssam			fail |= 0x04;
488127767Ssam	} else {
489127767Ssam		/* XXX does this mean privacy is supported or required? */
490127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
491127767Ssam			fail |= 0x04;
492127767Ssam	}
493167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
494165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
495127767Ssam	if (rate & IEEE80211_RATE_BASIC)
496127767Ssam		fail |= 0x08;
497178354Ssam	if (vap->iv_des_nssid != 0 &&
498178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
499127767Ssam		fail |= 0x10;
500178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
501178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
502127767Ssam		fail |= 0x20;
503127767Ssam
504170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
505170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
506170530Ssam	printf(" %3d%c",
507170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
508170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
509170530Ssam	    fail & 0x08 ? '!' : ' ');
510170530Ssam	printf(" %4s%c",
511170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
512170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
513170530Ssam	    "????",
514170530Ssam	    fail & 0x02 ? '!' : ' ');
515170530Ssam	printf(" %3s%c ",
516170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
517170530Ssam	    fail & 0x04 ? '!' : ' ');
518170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
519170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
520138568Ssam}
521170530Ssam#endif /* IEEE80211_DEBUG */
522138568Ssam
523138568Ssam/*
524138568Ssam * Handle 802.11 ad hoc network merge.  The
525138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
526138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
527138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
528138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
529138568Ssam * sends beacons with the greatest TSF timestamp.
530138568Ssam *
531138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
532138568Ssam *
533138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
534138568Ssam */
535138568Ssamint
536148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
537138568Ssam{
538178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
539178354Ssam#ifdef IEEE80211_DEBUG
540148306Ssam	struct ieee80211com *ic = ni->ni_ic;
541178354Ssam#endif
542138568Ssam
543178354Ssam	if (ni == vap->iv_bss ||
544178354Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
545138568Ssam		/* unchanged, nothing to do */
546138568Ssam		return 0;
547138568Ssam	}
548178354Ssam	if (!check_bss(vap, ni)) {
549170530Ssam		/* capabilities mismatch */
550178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
551138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
552170530Ssam#ifdef IEEE80211_DEBUG
553178354Ssam		if (ieee80211_msg_assoc(vap))
554178354Ssam			check_bss_debug(vap, ni);
555170530Ssam#endif
556178354Ssam		vap->iv_stats.is_ibss_capmismatch++;
557138568Ssam		return 0;
558138568Ssam	}
559178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
560138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
561138568Ssam		ether_sprintf(ni->ni_bssid),
562138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
563138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
564138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
565138568Ssam	);
566170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
567138568Ssam}
568138568Ssam
569138568Ssam/*
570178354Ssam * Calculate HT channel promotion flags for all vaps.
571178354Ssam * This assumes ni_chan have been setup for each vap.
572173273Ssam */
573178354Ssamstatic int
574178354Ssamgethtadjustflags(struct ieee80211com *ic)
575178354Ssam{
576178354Ssam	struct ieee80211vap *vap;
577178354Ssam	int flags;
578178354Ssam
579178354Ssam	flags = 0;
580178354Ssam	/* XXX locking */
581178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
582178354Ssam		if (vap->iv_state < IEEE80211_S_RUN)
583178354Ssam			continue;
584178354Ssam		switch (vap->iv_opmode) {
585178354Ssam		case IEEE80211_M_WDS:
586178354Ssam		case IEEE80211_M_STA:
587178354Ssam		case IEEE80211_M_AHDEMO:
588178354Ssam		case IEEE80211_M_HOSTAP:
589178354Ssam		case IEEE80211_M_IBSS:
590178354Ssam			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
591178354Ssam			break;
592178354Ssam		default:
593178354Ssam			break;
594178354Ssam		}
595178354Ssam	}
596178354Ssam	return flags;
597178354Ssam}
598178354Ssam
599178354Ssam/*
600178354Ssam * Check if the current channel needs to change based on whether
601178354Ssam * any vap's are using HT20/HT40.  This is used sync the state of
602178354Ssam * ic_curchan after a channel width change on a running vap.
603178354Ssam */
604173273Ssamvoid
605178354Ssamieee80211_sync_curchan(struct ieee80211com *ic)
606173273Ssam{
607178354Ssam	struct ieee80211_channel *c;
608178354Ssam
609178354Ssam	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
610178354Ssam	if (c != ic->ic_curchan) {
611178354Ssam		ic->ic_curchan = c;
612178354Ssam		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
613178354Ssam		ic->ic_set_channel(ic);
614178354Ssam	}
615178354Ssam}
616178354Ssam
617178354Ssam/*
618178354Ssam * Change the current channel.  The request channel may be
619178354Ssam * promoted if other vap's are operating with HT20/HT40.
620178354Ssam */
621178354Ssamvoid
622178354Ssamieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
623178354Ssam{
624178354Ssam	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
625178354Ssam		int flags = gethtadjustflags(ic);
626178354Ssam		/*
627178354Ssam		 * Check for channel promotion required to support the
628178354Ssam		 * set of running vap's.  This assumes we are called
629178354Ssam		 * after ni_chan is setup for each vap.
630178354Ssam		 */
631178354Ssam		/* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
632178354Ssam		if (flags > ieee80211_htchanflags(c))
633178354Ssam			c = ieee80211_ht_adjust_channel(ic, c, flags);
634178354Ssam	}
635178354Ssam	ic->ic_bsschan = ic->ic_curchan = c;
636173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
637173273Ssam	ic->ic_set_channel(ic);
638173273Ssam}
639173273Ssam
640173273Ssam/*
641138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
642138568Ssam * be passed in with a held reference.
643138568Ssam */
644170530Ssamstatic int
645170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
646138568Ssam{
647178354Ssam	struct ieee80211vap *vap = selbs->ni_vap;
648170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
649138568Ssam	struct ieee80211_node *obss;
650170530Ssam	int canreassoc;
651138568Ssam
652138568Ssam	/*
653138568Ssam	 * Committed to selbs, setup state.
654138568Ssam	 */
655178354Ssam	obss = vap->iv_bss;
656170530Ssam	/*
657170530Ssam	 * Check if old+new node have the same address in which
658170530Ssam	 * case we can reassociate when operating in sta mode.
659170530Ssam	 */
660170530Ssam	canreassoc = (obss != NULL &&
661178354Ssam		vap->iv_state == IEEE80211_S_RUN &&
662170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
663178354Ssam	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
664153352Ssam	if (obss != NULL) {
665153352Ssam		copy_bss(selbs, obss);
666178354Ssam		ieee80211_node_reclaim(obss);
667178354Ssam		obss = NULL;		/* NB: guard against later use */
668153352Ssam	}
669165887Ssam
670138568Ssam	/*
671165887Ssam	 * Delete unusable rates; we've already checked
672165887Ssam	 * that the negotiated rate set is acceptable.
673165887Ssam	 */
674178354Ssam	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
675167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
676165887Ssam
677178354Ssam	ieee80211_setcurchan(ic, selbs->ni_chan);
678165887Ssam	/*
679138568Ssam	 * Set the erp state (mostly the slot time) to deal with
680138568Ssam	 * the auto-select case; this should be redundant if the
681138568Ssam	 * mode is locked.
682138568Ssam	 */
683138568Ssam	ieee80211_reset_erp(ic);
684178354Ssam	ieee80211_wme_initparams(vap);
685140753Ssam
686178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
687170530Ssam		if (canreassoc) {
688170530Ssam			/* Reassociate */
689178354Ssam			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
690170530Ssam		} else {
691170530Ssam			/*
692170530Ssam			 * Act as if we received a DEAUTH frame in case we
693170530Ssam			 * are invoked from the RUN state.  This will cause
694170530Ssam			 * us to try to re-authenticate if we are operating
695170530Ssam			 * as a station.
696170530Ssam			 */
697178354Ssam			ieee80211_new_state(vap, IEEE80211_S_AUTH,
698170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
699170530Ssam		}
700170530Ssam	} else
701178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
702138568Ssam	return 1;
703116742Ssam}
704116742Ssam
705170530Ssamint
706184274Ssamieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
707170530Ssam	const struct ieee80211_scan_entry *se)
708170530Ssam{
709178354Ssam	struct ieee80211com *ic = vap->iv_ic;
710170530Ssam	struct ieee80211_node *ni;
711170530Ssam
712178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
713170530Ssam	if (ni == NULL) {
714170530Ssam		/* XXX msg */
715170530Ssam		return 0;
716170530Ssam	}
717170530Ssam	/*
718170530Ssam	 * Expand scan state into node's format.
719170530Ssam	 * XXX may not need all this stuff
720170530Ssam	 */
721170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
722170530Ssam	ni->ni_esslen = se->se_ssid[1];
723170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
724170530Ssam	ni->ni_rstamp = se->se_rstamp;
725170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
726170530Ssam	ni->ni_intval = se->se_intval;
727170530Ssam	ni->ni_capinfo = se->se_capinfo;
728184274Ssam	ni->ni_chan = chan;
729170530Ssam	ni->ni_timoff = se->se_timoff;
730170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
731170530Ssam	ni->ni_fhindex = se->se_fhindex;
732170530Ssam	ni->ni_erp = se->se_erp;
733178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
734170530Ssam	ni->ni_noise = se->se_noise;
735178354Ssam
736178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
737178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
738178354Ssam		if (ni->ni_ies.ath_ie != NULL)
739178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
740178354Ssam		if (ni->ni_ies.htcap_ie != NULL)
741178354Ssam			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
742178354Ssam		if (ni->ni_ies.htinfo_ie != NULL)
743178354Ssam			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
744173864Ssam	}
745170530Ssam
746178354Ssam	vap->iv_dtim_period = se->se_dtimperiod;
747178354Ssam	vap->iv_dtim_count = 0;
748170530Ssam
749170530Ssam	/* NB: must be after ni_chan is setup */
750170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
751170530Ssam		IEEE80211_F_DOSORT);
752184279Ssam	if (ieee80211_iserp_rateset(&ni->ni_rates))
753184279Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
754184279Ssam	node_setuptxparms(ni);
755170530Ssam
756170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
757170530Ssam}
758170530Ssam
759138568Ssam/*
760138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
761138568Ssam * be passed in with a held reference.
762138568Ssam */
763138568Ssamvoid
764178354Ssamieee80211_sta_leave(struct ieee80211_node *ni)
765138568Ssam{
766178354Ssam	struct ieee80211com *ic = ni->ni_ic;
767178354Ssam
768138568Ssam	ic->ic_node_cleanup(ni);
769178354Ssam	ieee80211_notify_node_leave(ni);
770138568Ssam}
771138568Ssam
772178354Ssam/*
773178354Ssam * Send a deauthenticate frame and drop the station.
774178354Ssam */
775178354Ssamvoid
776178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason)
777178354Ssam{
778178354Ssam	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
779178354Ssam	ieee80211_ref_node(ni);
780178354Ssam	if (ni->ni_associd != 0)
781178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
782178354Ssam	ieee80211_node_leave(ni);
783178354Ssam	ieee80211_free_node(ni);
784178354Ssam}
785178354Ssam
786116742Ssamstatic struct ieee80211_node *
787179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
788116742Ssam{
789127768Ssam	struct ieee80211_node *ni;
790138568Ssam
791184210Sdes	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
792127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
793127768Ssam	return ni;
794116742Ssam}
795116742Ssam
796138568Ssam/*
797178354Ssam * Initialize an ie blob with the specified data.  If previous
798178354Ssam * data exists re-use the data block.  As a side effect we clear
799178354Ssam * all references to specific ie's; the caller is required to
800178354Ssam * recalculate them.
801178354Ssam */
802178354Ssamint
803178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
804178354Ssam{
805178354Ssam	/* NB: assumes data+len are the last fields */
806178354Ssam	memset(ies, 0, offsetof(struct ieee80211_ies, data));
807178354Ssam	if (ies->data != NULL && ies->len != len) {
808178354Ssam		/* data size changed */
809184210Sdes		FREE(ies->data, M_80211_NODE_IE);
810178354Ssam		ies->data = NULL;
811178354Ssam	}
812178354Ssam	if (ies->data == NULL) {
813184210Sdes		MALLOC(ies->data, uint8_t *, len, M_80211_NODE_IE, M_NOWAIT);
814178354Ssam		if (ies->data == NULL) {
815178354Ssam			ies->len = 0;
816178354Ssam			/* NB: pointers have already been zero'd above */
817178354Ssam			return 0;
818178354Ssam		}
819178354Ssam	}
820178354Ssam	memcpy(ies->data, data, len);
821178354Ssam	ies->len = len;
822178354Ssam	return 1;
823178354Ssam}
824178354Ssam
825178354Ssam/*
826178354Ssam * Reclaim storage for an ie blob.
827178354Ssam */
828178354Ssamvoid
829178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies)
830178354Ssam{
831178354Ssam	if (ies->data != NULL)
832184210Sdes		FREE(ies->data, M_80211_NODE_IE);
833178354Ssam}
834178354Ssam
835178354Ssam/*
836178354Ssam * Expand an ie blob data contents and to fillin individual
837178354Ssam * ie pointers.  The data blob is assumed to be well-formed;
838178354Ssam * we don't do any validity checking of ie lengths.
839178354Ssam */
840178354Ssamvoid
841178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies)
842178354Ssam{
843178354Ssam	uint8_t *ie;
844178354Ssam	int ielen;
845178354Ssam
846178354Ssam	ie = ies->data;
847178354Ssam	ielen = ies->len;
848178354Ssam	while (ielen > 0) {
849178354Ssam		switch (ie[0]) {
850178354Ssam		case IEEE80211_ELEMID_VENDOR:
851178354Ssam			if (iswpaoui(ie))
852178354Ssam				ies->wpa_ie = ie;
853178354Ssam			else if (iswmeoui(ie))
854178354Ssam				ies->wme_ie = ie;
855178354Ssam			else if (isatherosoui(ie))
856178354Ssam				ies->ath_ie = ie;
857178354Ssam			break;
858178354Ssam		case IEEE80211_ELEMID_RSN:
859178354Ssam			ies->rsn_ie = ie;
860178354Ssam			break;
861178354Ssam		case IEEE80211_ELEMID_HTCAP:
862178354Ssam			ies->htcap_ie = ie;
863178354Ssam			break;
864178354Ssam		}
865178354Ssam		ielen -= 2 + ie[1];
866178354Ssam		ie += 2 + ie[1];
867178354Ssam	}
868178354Ssam}
869178354Ssam
870178354Ssam/*
871138568Ssam * Reclaim any resources in a node and reset any critical
872138568Ssam * state.  Typically nodes are free'd immediately after,
873138568Ssam * but in some cases the storage may be reused so we need
874138568Ssam * to insure consistent state (should probably fix that).
875138568Ssam */
876116742Ssamstatic void
877138568Ssamnode_cleanup(struct ieee80211_node *ni)
878116742Ssam{
879138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
880178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
881170530Ssam	int i;
882138568Ssam
883138568Ssam	/* NB: preserve ni_table */
884138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
885178354Ssam		if (vap->iv_opmode != IEEE80211_M_STA)
886178354Ssam			vap->iv_ps_sta--;
887138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
888178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
889178354Ssam		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
890138568Ssam	}
891147788Ssam	/*
892173273Ssam	 * Cleanup any HT-related state.
893173273Ssam	 */
894173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
895173273Ssam		ieee80211_ht_node_cleanup(ni);
896173273Ssam	/*
897147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
898147788Ssam	 * has happened.  This is probably not needed as the node
899147788Ssam	 * should always be removed from the table so not found but
900147788Ssam	 * do it just in case.
901147788Ssam	 */
902147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
903138568Ssam
904138568Ssam	/*
905138568Ssam	 * Drain power save queue and, if needed, clear TIM.
906138568Ssam	 */
907178354Ssam	if (ieee80211_node_saveq_drain(ni) != 0 && vap->iv_set_tim != NULL)
908178354Ssam		vap->iv_set_tim(ni, 0);
909138568Ssam
910138568Ssam	ni->ni_associd = 0;
911138568Ssam	if (ni->ni_challenge != NULL) {
912184210Sdes		FREE(ni->ni_challenge, M_80211_NODE);
913138568Ssam		ni->ni_challenge = NULL;
914138568Ssam	}
915138568Ssam	/*
916138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
917138568Ssam	 * reusable during a re-auth/re-assoc state transition.
918138568Ssam	 * If we remove these data they will not be recreated
919138568Ssam	 * because they come from a probe-response or beacon frame
920138568Ssam	 * which cannot be expected prior to the association-response.
921138568Ssam	 * This should not be an issue when operating in other modes
922138568Ssam	 * as stations leaving always go through a full state transition
923138568Ssam	 * which will rebuild this state.
924138568Ssam	 *
925138568Ssam	 * XXX does this leave us open to inheriting old state?
926138568Ssam	 */
927138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
928138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
929138568Ssam			m_freem(ni->ni_rxfrag[i]);
930138568Ssam			ni->ni_rxfrag[i] = NULL;
931138568Ssam		}
932148863Ssam	/*
933148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
934148863Ssam	 */
935148863Ssam	ieee80211_node_delucastkey(ni);
936138568Ssam#undef N
937116742Ssam}
938116742Ssam
939116742Ssamstatic void
940138568Ssamnode_free(struct ieee80211_node *ni)
941116742Ssam{
942138568Ssam	struct ieee80211com *ic = ni->ni_ic;
943138568Ssam
944138568Ssam	ic->ic_node_cleanup(ni);
945178354Ssam	ieee80211_ies_cleanup(&ni->ni_ies);
946138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
947178354Ssam	IEEE80211_NODE_WDSQ_DESTROY(ni);
948184210Sdes	FREE(ni, M_80211_NODE);
949116742Ssam}
950116742Ssam
951178354Ssamstatic void
952178354Ssamnode_age(struct ieee80211_node *ni)
953178354Ssam{
954178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
955178354Ssam#if 0
956178354Ssam	IEEE80211_NODE_LOCK_ASSERT(&ic->ic_sta);
957178354Ssam#endif
958178354Ssam	/*
959178354Ssam	 * Age frames on the power save queue.
960178354Ssam	 */
961178354Ssam	if (ieee80211_node_saveq_age(ni) != 0 &&
962178354Ssam	    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
963178354Ssam	    vap->iv_set_tim != NULL)
964178354Ssam		vap->iv_set_tim(ni, 0);
965178354Ssam	/*
966178354Ssam	 * Age frames on the wds pending queue.
967178354Ssam	 */
968178354Ssam	if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
969178354Ssam		ieee80211_node_wdsq_age(ni);
970178354Ssam	/*
971178354Ssam	 * Age out HT resources (e.g. frames on the
972178354Ssam	 * A-MPDU reorder queues).
973178354Ssam	 */
974178354Ssam	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
975178354Ssam		ieee80211_ht_node_age(ni);
976178354Ssam}
977178354Ssam
978170530Ssamstatic int8_t
979138568Ssamnode_getrssi(const struct ieee80211_node *ni)
980120104Ssam{
981178354Ssam	uint32_t avgrssi = ni->ni_avgrssi;
982178354Ssam	int32_t rssi;
983178354Ssam
984178354Ssam	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
985178354Ssam		return 0;
986178354Ssam	rssi = IEEE80211_RSSI_GET(avgrssi);
987178354Ssam	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
988120104Ssam}
989120104Ssam
990116742Ssamstatic void
991170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
992170530Ssam{
993178354Ssam	*rssi = node_getrssi(ni);
994170530Ssam	*noise = ni->ni_noise;
995170530Ssam}
996170530Ssam
997170530Ssamstatic void
998178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni,
999178354Ssam	struct ieee80211_mimo_info *info)
1000116742Ssam{
1001178354Ssam	/* XXX zero data? */
1002178354Ssam}
1003178354Ssam
1004178354Ssamstruct ieee80211_node *
1005178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt,
1006178354Ssam	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1007178354Ssam{
1008138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1009178354Ssam	struct ieee80211_node *ni;
1010116742Ssam	int hash;
1011116742Ssam
1012179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1013178354Ssam	if (ni == NULL) {
1014178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1015178354Ssam		return NULL;
1016178354Ssam	}
1017178354Ssam
1018178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1019140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1020138568Ssam		ether_sprintf(macaddr), nt->nt_name);
1021138568Ssam
1022116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1023116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1024138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
1025138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
1026138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
1027138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
1028183251Ssam	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1029178354Ssam	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1030178354Ssam	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1031139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
1032139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1033170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
1034138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1035178354Ssam	IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1036138568Ssam
1037138568Ssam	IEEE80211_NODE_LOCK(nt);
1038138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1039138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1040138568Ssam	ni->ni_table = nt;
1041178354Ssam	ni->ni_vap = vap;
1042138568Ssam	ni->ni_ic = ic;
1043138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1044116742Ssam
1045184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1046184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1047184277Ssam
1048116742Ssam	return ni;
1049116742Ssam}
1050116742Ssam
1051148777Ssam/*
1052148777Ssam * Craft a temporary node suitable for sending a management frame
1053148777Ssam * to the specified station.  We craft only as much state as we
1054148777Ssam * need to do the work since the node will be immediately reclaimed
1055148777Ssam * once the send completes.
1056148777Ssam */
1057116742Ssamstruct ieee80211_node *
1058178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1059178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1060148777Ssam{
1061178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1062148777Ssam	struct ieee80211_node *ni;
1063148777Ssam
1064179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1065148777Ssam	if (ni != NULL) {
1066183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1067183259Ssam
1068178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1069148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1070148777Ssam
1071178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1072178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1073178354Ssam		ni->ni_vap = vap;
1074178354Ssam
1075148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1076183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1077148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1078148777Ssam		/* NB: required by ieee80211_fix_rate */
1079183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1080178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1081148777Ssam			IEEE80211_KEYIX_NONE);
1082183259Ssam		ni->ni_txpower = bss->ni_txpower;
1083148777Ssam		/* XXX optimize away */
1084148777Ssam		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1085178354Ssam		IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1086148777Ssam	} else {
1087148777Ssam		/* XXX msg */
1088178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1089148777Ssam	}
1090148777Ssam	return ni;
1091148777Ssam}
1092148777Ssam
1093148777Ssamstruct ieee80211_node *
1094178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1095178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1096116742Ssam{
1097178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1098138568Ssam	struct ieee80211_node *ni;
1099138568Ssam
1100178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1101116742Ssam	if (ni != NULL) {
1102183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1103127770Ssam		/*
1104178354Ssam		 * Inherit from iv_bss.
1105127770Ssam		 */
1106183259Ssam		copy_bss(ni, bss);
1107183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1108183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1109178354Ssam	}
1110116742Ssam	return ni;
1111116742Ssam}
1112116742Ssam
1113178354Ssam/*
1114178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1115178354Ssam * not associate so we just create create a new node and
1116178354Ssam * simulate an association.  The caller is responsible for
1117178354Ssam * installing the node as the bss node and handling any further
1118178354Ssam * setup work like authorizing the port.
1119178354Ssam */
1120178354Ssamstruct ieee80211_node *
1121178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1122178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1123178354Ssam{
1124178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1125178354Ssam	struct ieee80211_node *ni;
1126178354Ssam
1127178354Ssam	/* XXX check if node already in sta table? */
1128178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1129178354Ssam	if (ni != NULL) {
1130178354Ssam		ni->ni_wdsvap = vap;
1131178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1132178354Ssam		/*
1133178354Ssam		 * Inherit any manually configured settings.
1134178354Ssam		 */
1135183259Ssam		copy_bss(ni, vap->iv_bss);
1136178354Ssam		ieee80211_node_set_chan(ni, chan);
1137178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1138178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1139178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1140178354Ssam		/* NB: no associd for peer */
1141178354Ssam		/*
1142178354Ssam		 * There are no management frames to use to
1143178354Ssam		 * discover neighbor capabilities, so blindly
1144178354Ssam		 * propagate the local configuration.
1145178354Ssam		 */
1146178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1147178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1148178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1149178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1150178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1151178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1152178354Ssam			/*
1153178354Ssam			 * Device is HT-capable and HT is enabled for
1154178354Ssam			 * the vap; setup HT operation.  On return
1155178354Ssam			 * ni_chan will be adjusted to an HT channel.
1156178354Ssam			 */
1157178354Ssam			ieee80211_ht_wds_init(ni);
1158178354Ssam		} else {
1159178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1160178354Ssam			/*
1161178354Ssam			 * Force a legacy channel to be used.
1162178354Ssam			 */
1163178354Ssam			c = ieee80211_find_channel(ic,
1164178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1165178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1166178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1167178354Ssam			ni->ni_chan = c;
1168178354Ssam		}
1169178354Ssam	}
1170178354Ssam	return ni;
1171178354Ssam}
1172178354Ssam
1173178354Ssamstruct ieee80211_node *
1174138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1175178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1176178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1177138568Ssam#else
1178178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1179178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1180138568Ssam#endif
1181116742Ssam{
1182116742Ssam	struct ieee80211_node *ni;
1183116742Ssam	int hash;
1184116742Ssam
1185138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1186127772Ssam
1187116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1188138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1189116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1190138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1191138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1192178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1193140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1194140766Ssam			    func, line,
1195140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1196140766Ssam			    ieee80211_node_refcnt(ni));
1197138568Ssam#endif
1198127772Ssam			return ni;
1199116742Ssam		}
1200116742Ssam	}
1201127772Ssam	return NULL;
1202127772Ssam}
1203178354Ssam
1204178354Ssamstruct ieee80211_node *
1205138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1206178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1207178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1208178354Ssam#else
1209178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1210178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1211138568Ssam#endif
1212178354Ssam{
1213178354Ssam	struct ieee80211_node *ni;
1214127772Ssam
1215178354Ssam	IEEE80211_NODE_LOCK(nt);
1216178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1217178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1218178354Ssam	return ni;
1219178354Ssam}
1220178354Ssam
1221127772Ssamstruct ieee80211_node *
1222138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1223178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1224178354Ssam	const struct ieee80211vap *vap,
1225178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1226138568Ssam#else
1227178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1228178354Ssam	const struct ieee80211vap *vap,
1229178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1230138568Ssam#endif
1231127772Ssam{
1232127772Ssam	struct ieee80211_node *ni;
1233178354Ssam	int hash;
1234127772Ssam
1235178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1236178354Ssam
1237178354Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1238178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1239178354Ssam		if (ni->ni_vap == vap &&
1240178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1241178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1242178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1243178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1244178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1245178354Ssam			    func, line,
1246178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1247178354Ssam			    ieee80211_node_refcnt(ni));
1248178354Ssam#endif
1249178354Ssam			return ni;
1250178354Ssam		}
1251178354Ssam	}
1252178354Ssam	return NULL;
1253178354Ssam}
1254178354Ssam
1255178354Ssamstruct ieee80211_node *
1256178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1257178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1258178354Ssam	const struct ieee80211vap *vap,
1259178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1260178354Ssam#else
1261178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1262178354Ssam	const struct ieee80211vap *vap,
1263178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1264178354Ssam#endif
1265178354Ssam{
1266178354Ssam	struct ieee80211_node *ni;
1267178354Ssam
1268138568Ssam	IEEE80211_NODE_LOCK(nt);
1269178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1270138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1271116742Ssam	return ni;
1272116742Ssam}
1273116742Ssam
1274116742Ssam/*
1275138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1276138568Ssam * Note that for the driver's benefit we we treat this like
1277138568Ssam * an association so the driver has an opportunity to setup
1278138568Ssam * it's private state.
1279138568Ssam */
1280138568Ssamstruct ieee80211_node *
1281178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1282170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1283138568Ssam{
1284138568Ssam	struct ieee80211_node *ni;
1285138568Ssam
1286178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1287153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1288178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1289138568Ssam	if (ni != NULL) {
1290178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1291178354Ssam
1292138568Ssam		/* XXX no rate negotiation; just dup */
1293178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1294178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1295153404Ssam			/*
1296170530Ssam			 * In adhoc demo mode there are no management
1297170530Ssam			 * frames to use to discover neighbor capabilities,
1298170530Ssam			 * so blindly propagate the local configuration
1299170530Ssam			 * so we can do interesting things (e.g. use
1300170530Ssam			 * WME to disable ACK's).
1301153404Ssam			 */
1302178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1303153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1304178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1305170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1306153404Ssam		}
1307183251Ssam		node_setuptxparms(ni);
1308178354Ssam		if (ic->ic_newassoc != NULL)
1309178354Ssam			ic->ic_newassoc(ni, 1);
1310170530Ssam		/* XXX not right for 802.1x/WPA */
1311170530Ssam		ieee80211_node_authorize(ni);
1312138568Ssam	}
1313138568Ssam	return ni;
1314138568Ssam}
1315138568Ssam
1316148936Ssamvoid
1317153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1318153073Ssam	const struct ieee80211_frame *wh,
1319153073Ssam	const struct ieee80211_scanparams *sp)
1320153073Ssam{
1321153073Ssam	ni->ni_esslen = sp->ssid[1];
1322153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1323153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1324153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1325153073Ssam	ni->ni_intval = sp->bintval;
1326153073Ssam	ni->ni_capinfo = sp->capinfo;
1327153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1328153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1329153073Ssam	ni->ni_fhindex = sp->fhindex;
1330153073Ssam	ni->ni_erp = sp->erp;
1331153073Ssam	ni->ni_timoff = sp->timoff;
1332153073Ssam
1333178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1334178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1335178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1336178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1337178354Ssam	}
1338178354Ssam
1339153073Ssam	/* NB: must be after ni_chan is setup */
1340165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1341165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1342165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1343153073Ssam}
1344153073Ssam
1345148936Ssam/*
1346148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1347148936Ssam * or probe response frame.  Note that for the driver's
1348148936Ssam * benefit we we treat this like an association so the
1349148936Ssam * driver has an opportunity to setup it's private state.
1350148936Ssam */
1351148936Ssamstruct ieee80211_node *
1352178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1353148936Ssam	const struct ieee80211_frame *wh,
1354148936Ssam	const struct ieee80211_scanparams *sp)
1355148936Ssam{
1356148936Ssam	struct ieee80211_node *ni;
1357148936Ssam
1358178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1359153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1360178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1361148936Ssam	if (ni != NULL) {
1362178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1363178354Ssam
1364153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1365183251Ssam		node_setuptxparms(ni);
1366148936Ssam		if (ic->ic_newassoc != NULL)
1367148936Ssam			ic->ic_newassoc(ni, 1);
1368148936Ssam		/* XXX not right for 802.1x/WPA */
1369148936Ssam		ieee80211_node_authorize(ni);
1370148936Ssam	}
1371148936Ssam	return ni;
1372148936Ssam}
1373148936Ssam
1374148863Ssam#define	IS_CTL(wh) \
1375148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1376148863Ssam#define	IS_PSPOLL(wh) \
1377148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1378170530Ssam#define	IS_BAR(wh) \
1379170530Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1380179220Ssam#define	IS_PROBEREQ(wh) \
1381179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1382179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1383179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1384179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1385179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1386170530Ssam
1387179220Ssamstatic __inline struct ieee80211_node *
1388179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1389179220Ssam    const struct ieee80211_frame_min *wh)
1390179220Ssam{
1391179220Ssam	/* XXX 4-address frames? */
1392179220Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1393179220Ssam		return ieee80211_find_node_locked(nt, wh->i_addr1);
1394179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1395179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1396179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1397179220Ssam}
1398179220Ssam
1399138568Ssam/*
1400138568Ssam * Locate the node for sender, track state, and then pass the
1401179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1402179220Ssam * we can return NULL if the sender is not in the table.
1403138568Ssam */
1404138568Ssamstruct ieee80211_node *
1405138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1406138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1407138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1408138568Ssam#else
1409138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1410138568Ssam	const struct ieee80211_frame_min *wh)
1411138568Ssam#endif
1412138568Ssam{
1413138568Ssam	struct ieee80211_node_table *nt;
1414138568Ssam	struct ieee80211_node *ni;
1415138568Ssam
1416170530Ssam	nt = &ic->ic_sta;
1417138568Ssam	IEEE80211_NODE_LOCK(nt);
1418179220Ssam	ni = _find_rxnode(nt, wh);
1419138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1420138568Ssam
1421148863Ssam	return ni;
1422148863Ssam}
1423148863Ssam
1424148863Ssam/*
1425148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1426148863Ssam * key index as a hint to locate the node in the key
1427148863Ssam * mapping table.  If an entry is present at the key
1428148863Ssam * index we return it; otherwise do a normal lookup and
1429148863Ssam * update the mapping table if the station has a unicast
1430148863Ssam * key assigned to it.
1431148863Ssam */
1432148863Ssamstruct ieee80211_node *
1433148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1434148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1435148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1436148863Ssam	const char *func, int line)
1437148863Ssam#else
1438148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1439148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1440148863Ssam#endif
1441148863Ssam{
1442148863Ssam	struct ieee80211_node_table *nt;
1443148863Ssam	struct ieee80211_node *ni;
1444148863Ssam
1445170530Ssam	nt = &ic->ic_sta;
1446148863Ssam	IEEE80211_NODE_LOCK(nt);
1447148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1448148863Ssam		ni = nt->nt_keyixmap[keyix];
1449148863Ssam	else
1450148863Ssam		ni = NULL;
1451148863Ssam	if (ni == NULL) {
1452179220Ssam		ni = _find_rxnode(nt, wh);
1453178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1454148863Ssam			/*
1455148863Ssam			 * If the station has a unicast key cache slot
1456148863Ssam			 * assigned update the key->node mapping table.
1457148863Ssam			 */
1458148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1459148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1460148863Ssam			if (keyix < nt->nt_keyixmax &&
1461148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1462178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1463178354Ssam				    IEEE80211_MSG_NODE,
1464148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1465148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1466148863Ssam				    ieee80211_node_refcnt(ni)+1);
1467148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1468148863Ssam			}
1469148863Ssam		}
1470179220Ssam	} else {
1471179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1472179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1473179220Ssam		else
1474179220Ssam			ieee80211_ref_node(ni);
1475179220Ssam	}
1476148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1477148863Ssam
1478148863Ssam	return ni;
1479148863Ssam}
1480179220Ssam#undef IS_BCAST_PROBEREQ
1481179220Ssam#undef IS_PROBEREQ
1482170530Ssam#undef IS_BAR
1483138568Ssam#undef IS_PSPOLL
1484138568Ssam#undef IS_CTL
1485138568Ssam
1486138568Ssam/*
1487127772Ssam * Return a reference to the appropriate node for sending
1488127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1489127772Ssam */
1490127772Ssamstruct ieee80211_node *
1491138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1492178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1493178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1494138568Ssam	const char *func, int line)
1495138568Ssam#else
1496178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1497178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1498138568Ssam#endif
1499127772Ssam{
1500178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1501127772Ssam	struct ieee80211_node *ni;
1502127772Ssam
1503127772Ssam	/*
1504127772Ssam	 * The destination address should be in the node table
1505148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1506148863Ssam	 * also optimize station mode operation, all frames go
1507148863Ssam	 * to the bss node.
1508127772Ssam	 */
1509127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1510138568Ssam	IEEE80211_NODE_LOCK(nt);
1511178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1512178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1513178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1514178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1515158121Ssam	else {
1516178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1517178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1518158121Ssam		    (ni != NULL && ni->ni_associd == 0)) {
1519158121Ssam			/*
1520158121Ssam			 * Station is not associated; don't permit the
1521158121Ssam			 * data frame to be sent by returning NULL.  This
1522158121Ssam			 * is kinda a kludge but the least intrusive way
1523158121Ssam			 * to add this check into all drivers.
1524158121Ssam			 */
1525158121Ssam			ieee80211_unref_node(&ni);	/* NB: null's ni */
1526158121Ssam		}
1527158121Ssam	}
1528138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1529138568Ssam
1530138568Ssam	if (ni == NULL) {
1531178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1532178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1533140497Ssam			/*
1534140497Ssam			 * In adhoc mode cons up a node for the destination.
1535140497Ssam			 * Note that we need an additional reference for the
1536178354Ssam			 * caller to be consistent with
1537178354Ssam			 * ieee80211_find_node_locked.
1538140497Ssam			 */
1539178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1540140497Ssam			if (ni != NULL)
1541140497Ssam				(void) ieee80211_ref_node(ni);
1542140497Ssam		} else {
1543178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1544178354Ssam			    "no node, discard frame (%s)", __func__);
1545178354Ssam			vap->iv_stats.is_tx_nonode++;
1546127772Ssam		}
1547127772Ssam	}
1548127772Ssam	return ni;
1549127772Ssam}
1550127772Ssam
1551116742Ssamstatic void
1552138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1553116742Ssam{
1554138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1555119150Ssam
1556179641Ssam	/*
1557179641Ssam	 * NB: careful about referencing the vap as it may be
1558179641Ssam	 * gone if the last reference was held by a driver.
1559179641Ssam	 * We know the com will always be present so it's safe
1560179641Ssam	 * to use ni_ic below to reclaim resources.
1561179641Ssam	 */
1562179641Ssam#if 0
1563178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1564140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1565140766Ssam		ether_sprintf(ni->ni_macaddr),
1566138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1567179641Ssam#endif
1568179641Ssam	if (ni->ni_associd != 0) {
1569179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1570179641Ssam		if (vap->iv_aid_bitmap != NULL)
1571179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1572179641Ssam	}
1573138568Ssam	if (nt != NULL) {
1574138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1575138568Ssam		LIST_REMOVE(ni, ni_hash);
1576138568Ssam	}
1577179641Ssam	ni->ni_ic->ic_node_free(ni);
1578116742Ssam}
1579116742Ssam
1580116742Ssamvoid
1581138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1582138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1583138568Ssam#else
1584138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1585138568Ssam#endif
1586116742Ssam{
1587138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1588119150Ssam
1589138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1590178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1591140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1592138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1593138568Ssam#endif
1594148863Ssam	if (nt != NULL) {
1595148863Ssam		IEEE80211_NODE_LOCK(nt);
1596148863Ssam		if (ieee80211_node_dectestref(ni)) {
1597148863Ssam			/*
1598148863Ssam			 * Last reference, reclaim state.
1599148863Ssam			 */
1600138568Ssam			_ieee80211_free_node(ni);
1601148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1602148863Ssam		    nt->nt_keyixmap != NULL) {
1603148863Ssam			ieee80211_keyix keyix;
1604148863Ssam			/*
1605148863Ssam			 * Check for a last reference in the key mapping table.
1606148863Ssam			 */
1607148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1608148863Ssam			if (keyix < nt->nt_keyixmax &&
1609148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1610178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1611178354Ssam				    IEEE80211_MSG_NODE,
1612148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1613148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1614148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1615148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1616148863Ssam				_ieee80211_free_node(ni);
1617148863Ssam			}
1618148863Ssam		}
1619148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1620148863Ssam	} else {
1621148863Ssam		if (ieee80211_node_dectestref(ni))
1622138568Ssam			_ieee80211_free_node(ni);
1623116742Ssam	}
1624116742Ssam}
1625116742Ssam
1626138568Ssam/*
1627148863Ssam * Reclaim a unicast key and clear any key cache state.
1628148863Ssam */
1629148863Ssamint
1630148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1631148863Ssam{
1632179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1633179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1634148863Ssam	struct ieee80211_node *nikey;
1635148863Ssam	ieee80211_keyix keyix;
1636148863Ssam	int isowned, status;
1637148863Ssam
1638148863Ssam	/*
1639148863Ssam	 * NB: We must beware of LOR here; deleting the key
1640148863Ssam	 * can cause the crypto layer to block traffic updates
1641148863Ssam	 * which can generate a LOR against the node table lock;
1642148863Ssam	 * grab it here and stash the key index for our use below.
1643148863Ssam	 *
1644148863Ssam	 * Must also beware of recursion on the node table lock.
1645148863Ssam	 * When called from node_cleanup we may already have
1646148863Ssam	 * the node table lock held.  Unfortunately there's no
1647148863Ssam	 * way to separate out this path so we must do this
1648148863Ssam	 * conditionally.
1649148863Ssam	 */
1650148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1651148863Ssam	if (!isowned)
1652148863Ssam		IEEE80211_NODE_LOCK(nt);
1653179641Ssam	nikey = NULL;
1654179641Ssam	status = 1;		/* NB: success */
1655179641Ssam	if (!IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1656179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1657179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1658179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1659179641Ssam			nikey = nt->nt_keyixmap[keyix];
1660179641Ssam			nt->nt_keyixmap[keyix] = NULL;;
1661179641Ssam		}
1662179641Ssam	}
1663148863Ssam	if (!isowned)
1664178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1665148863Ssam
1666148863Ssam	if (nikey != NULL) {
1667148863Ssam		KASSERT(nikey == ni,
1668148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1669179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1670148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1671148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1672148863Ssam			ieee80211_node_refcnt(ni)-1);
1673148863Ssam		ieee80211_free_node(ni);
1674148863Ssam	}
1675148863Ssam	return status;
1676148863Ssam}
1677148863Ssam
1678148863Ssam/*
1679138568Ssam * Reclaim a node.  If this is the last reference count then
1680138568Ssam * do the normal free work.  Otherwise remove it from the node
1681138568Ssam * table and mark it gone by clearing the back-reference.
1682138568Ssam */
1683138568Ssamstatic void
1684138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1685116742Ssam{
1686148863Ssam	ieee80211_keyix keyix;
1687138568Ssam
1688148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1689148863Ssam
1690178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1691140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1692140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1693140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1694148863Ssam	/*
1695148863Ssam	 * Clear any entry in the unicast key mapping table.
1696148863Ssam	 * We need to do it here so rx lookups don't find it
1697148863Ssam	 * in the mapping table even if it's not in the hash
1698148863Ssam	 * table.  We cannot depend on the mapping table entry
1699148863Ssam	 * being cleared because the node may not be free'd.
1700148863Ssam	 */
1701148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1702148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1703148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1704178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1705148863Ssam			"%s: %p<%s> clear key map entry\n",
1706148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr));
1707148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1708148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1709148863Ssam	}
1710138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1711138568Ssam		/*
1712138568Ssam		 * Other references are present, just remove the
1713138568Ssam		 * node from the table so it cannot be found.  When
1714138568Ssam		 * the references are dropped storage will be
1715140753Ssam		 * reclaimed.
1716138568Ssam		 */
1717138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1718138568Ssam		LIST_REMOVE(ni, ni_hash);
1719138568Ssam		ni->ni_table = NULL;		/* clear reference */
1720138568Ssam	} else
1721138568Ssam		_ieee80211_free_node(ni);
1722138568Ssam}
1723138568Ssam
1724178354Ssam/*
1725178354Ssam * Reclaim a (bss) node.  Decrement the refcnt and reclaim
1726178354Ssam * the node if the only other reference to it is in the sta
1727178354Ssam * table.  This is effectively ieee80211_free_node followed
1728178354Ssam * by node_reclaim when the refcnt is 1 (after the free).
1729178354Ssam */
1730138568Ssamstatic void
1731178354Ssamieee80211_node_reclaim(struct ieee80211_node *ni)
1732138568Ssam{
1733178354Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1734116742Ssam
1735178354Ssam	KASSERT(nt != NULL, ("reclaim node not in table"));
1736138568Ssam
1737178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1738178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1739178712Ssam		"%s %p<%s> refcnt %d\n", __func__, ni,
1740178354Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1741178354Ssam#endif
1742178354Ssam	IEEE80211_NODE_LOCK(nt);
1743178354Ssam	if (ieee80211_node_dectestref(ni)) {
1744178354Ssam		/*
1745178354Ssam		 * Last reference, reclaim state.
1746178354Ssam		 */
1747178354Ssam		_ieee80211_free_node(ni);
1748178354Ssam		nt = NULL;
1749178354Ssam	} else if (ieee80211_node_refcnt(ni) == 1 &&
1750178354Ssam	    nt->nt_keyixmap != NULL) {
1751178354Ssam		ieee80211_keyix keyix;
1752178354Ssam		/*
1753178354Ssam		 * Check for a last reference in the key mapping table.
1754178354Ssam		 */
1755178354Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1756178354Ssam		if (keyix < nt->nt_keyixmax &&
1757178354Ssam		    nt->nt_keyixmap[keyix] == ni) {
1758178354Ssam			IEEE80211_DPRINTF(ni->ni_vap,
1759178354Ssam			    IEEE80211_MSG_NODE,
1760178354Ssam			    "%s: %p<%s> clear key map entry", __func__,
1761178354Ssam			    ni, ether_sprintf(ni->ni_macaddr));
1762178354Ssam			nt->nt_keyixmap[keyix] = NULL;
1763178354Ssam			ieee80211_node_decref(ni); /* XXX needed? */
1764178354Ssam			_ieee80211_free_node(ni);
1765178354Ssam			nt = NULL;
1766178354Ssam		}
1767178354Ssam	}
1768178354Ssam	if (nt != NULL && ieee80211_node_refcnt(ni) == 1) {
1769178354Ssam		/*
1770178354Ssam		 * Last reference is in the sta table; complete
1771178354Ssam		 * the reclaim.  This handles bss nodes being
1772178354Ssam		 * recycled: the node has two references, one for
1773178354Ssam		 * iv_bss and one for the table.  After dropping
1774178354Ssam		 * the iv_bss ref above we need to reclaim the sta
1775178354Ssam		 * table reference.
1776178354Ssam		 */
1777178354Ssam		ieee80211_node_decref(ni);	/* NB: be pendantic */
1778178354Ssam		_ieee80211_free_node(ni);
1779178354Ssam	}
1780178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1781178354Ssam}
1782178354Ssam
1783178354Ssam/*
1784178354Ssam * Node table support.
1785178354Ssam */
1786178354Ssam
1787178354Ssamstatic void
1788178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1789178354Ssam	struct ieee80211_node_table *nt,
1790178354Ssam	const char *name, int inact, int keyixmax)
1791178354Ssam{
1792178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1793178354Ssam
1794178354Ssam	nt->nt_ic = ic;
1795178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1796178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1797178354Ssam	TAILQ_INIT(&nt->nt_node);
1798178354Ssam	nt->nt_name = name;
1799178354Ssam	nt->nt_scangen = 1;
1800178354Ssam	nt->nt_inact_init = inact;
1801178354Ssam	nt->nt_keyixmax = keyixmax;
1802178354Ssam	if (nt->nt_keyixmax > 0) {
1803184210Sdes		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
1804184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1805178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1806178354Ssam		if (nt->nt_keyixmap == NULL)
1807178354Ssam			if_printf(ic->ic_ifp,
1808178354Ssam			    "Cannot allocate key index map with %u entries\n",
1809178354Ssam			    keyixmax);
1810178354Ssam	} else
1811178354Ssam		nt->nt_keyixmap = NULL;
1812178354Ssam}
1813178354Ssam
1814178354Ssamstatic void
1815178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1816178354Ssam	struct ieee80211vap *match)
1817178354Ssam{
1818178354Ssam	struct ieee80211_node *ni, *next;
1819178354Ssam
1820178354Ssam	IEEE80211_NODE_LOCK(nt);
1821178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1822178354Ssam		if (match != NULL && ni->ni_vap != match)
1823178354Ssam			continue;
1824178354Ssam		/* XXX can this happen?  if so need's work */
1825138568Ssam		if (ni->ni_associd != 0) {
1826178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1827178354Ssam
1828178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1829178354Ssam				vap->iv_auth->ia_node_leave(ni);
1830178354Ssam			if (vap->iv_aid_bitmap != NULL)
1831178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1832138568Ssam		}
1833178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1834138568Ssam		node_reclaim(nt, ni);
1835138568Ssam	}
1836178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1837178354Ssam		/*
1838178354Ssam		 * Make a separate pass to clear references to this vap
1839178354Ssam		 * held by DWDS entries.  They will not be matched above
1840178354Ssam		 * because ni_vap will point to the ap vap but we still
1841178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1842178354Ssam		 * and/or reset.
1843178354Ssam		 */
1844178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1845178354Ssam			if (ni->ni_wdsvap == match)
1846178354Ssam				ni->ni_wdsvap = NULL;
1847178354Ssam	}
1848178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1849116742Ssam}
1850116742Ssam
1851178354Ssamstatic void
1852178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1853178354Ssam{
1854178354Ssam	ieee80211_node_table_reset(nt, NULL);
1855178354Ssam	if (nt->nt_keyixmap != NULL) {
1856178354Ssam#ifdef DIAGNOSTIC
1857178354Ssam		/* XXX verify all entries are NULL */
1858178354Ssam		int i;
1859178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1860178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1861178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1862178354Ssam					nt->nt_name, i);
1863178354Ssam#endif
1864184210Sdes		FREE(nt->nt_keyixmap, M_80211_NODE);
1865178354Ssam		nt->nt_keyixmap = NULL;
1866178354Ssam	}
1867178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1868178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1869178354Ssam}
1870178354Ssam
1871120483Ssam/*
1872138568Ssam * Timeout inactive stations and do related housekeeping.
1873138568Ssam * Note that we cannot hold the node lock while sending a
1874138568Ssam * frame as this would lead to a LOR.  Instead we use a
1875138568Ssam * generation number to mark nodes that we've scanned and
1876138568Ssam * drop the lock and restart a scan if we have to time out
1877138568Ssam * a node.  Since we are single-threaded by virtue of
1878120483Ssam * controlling the inactivity timer we can be sure this will
1879120483Ssam * process each node only once.
1880120483Ssam */
1881138568Ssamstatic void
1882178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1883116742Ssam{
1884178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1885178354Ssam	struct ieee80211vap *vap;
1886120483Ssam	struct ieee80211_node *ni;
1887178354Ssam	int gen = 0;
1888116742Ssam
1889178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
1890154532Ssam	gen = ++nt->nt_scangen;
1891120483Ssamrestart:
1892138568Ssam	IEEE80211_NODE_LOCK(nt);
1893138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1894120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1895120483Ssam			continue;
1896120483Ssam		ni->ni_scangen = gen;
1897138568Ssam		/*
1898147788Ssam		 * Ignore entries for which have yet to receive an
1899147788Ssam		 * authentication frame.  These are transient and
1900147788Ssam		 * will be reclaimed when the last reference to them
1901147788Ssam		 * goes away (when frame xmits complete).
1902147788Ssam		 */
1903178354Ssam		vap = ni->ni_vap;
1904178354Ssam		/*
1905178354Ssam		 * Only process stations when in RUN state.  This
1906178354Ssam		 * insures, for example, that we don't timeout an
1907178354Ssam		 * inactive station during CAC.  Note that CSA state
1908178354Ssam		 * is actually handled in ieee80211_node_timeout as
1909178354Ssam		 * it applies to more than timeout processing.
1910178354Ssam		 */
1911178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
1912178354Ssam			continue;
1913178354Ssam		/* XXX can vap be NULL? */
1914178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1915178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
1916148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1917147788Ssam			continue;
1918147788Ssam		/*
1919138568Ssam		 * Free fragment if not needed anymore
1920138568Ssam		 * (last fragment older than 1s).
1921178354Ssam		 * XXX doesn't belong here, move to node_age
1922138568Ssam		 */
1923138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1924138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1925138568Ssam			m_freem(ni->ni_rxfrag[0]);
1926138568Ssam			ni->ni_rxfrag[0] = NULL;
1927138568Ssam		}
1928184277Ssam		if (ni->ni_inact > 0) {
1929172062Ssam			ni->ni_inact--;
1930184277Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1931184277Ssam			    "%s: inact %u inact_reload %u nrates %u",
1932184277Ssam			    __func__, ni->ni_inact, ni->ni_inact_reload,
1933184277Ssam			    ni->ni_rates.rs_nrates);
1934184277Ssam		}
1935140498Ssam		/*
1936140498Ssam		 * Special case ourself; we may be idle for extended periods
1937140498Ssam		 * of time and regardless reclaiming our state is wrong.
1938178354Ssam		 * XXX run ic_node_age
1939140498Ssam		 */
1940178354Ssam		if (ni == vap->iv_bss)
1941140498Ssam			continue;
1942178354Ssam		if (ni->ni_associd != 0 ||
1943178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1944178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1945119150Ssam			/*
1946178354Ssam			 * Age/drain resources held by the station.
1947138568Ssam			 */
1948178354Ssam			ic->ic_node_age(ni);
1949138568Ssam			/*
1950138568Ssam			 * Probe the station before time it out.  We
1951138568Ssam			 * send a null data frame which may not be
1952138568Ssam			 * universally supported by drivers (need it
1953138568Ssam			 * for ps-poll support so it should be...).
1954170530Ssam			 *
1955170530Ssam			 * XXX don't probe the station unless we've
1956170530Ssam			 *     received a frame from them (and have
1957170530Ssam			 *     some idea of the rates they are capable
1958170530Ssam			 *     of); this will get fixed more properly
1959170530Ssam			 *     soon with better handling of the rate set.
1960138568Ssam			 */
1961178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1962172062Ssam			    (0 < ni->ni_inact &&
1963178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
1964170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1965178354Ssam				IEEE80211_NOTE(vap,
1966148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1967148320Ssam				    ni, "%s",
1968148320Ssam				    "probe station due to inactivity");
1969148582Ssam				/*
1970148582Ssam				 * Grab a reference before unlocking the table
1971148582Ssam				 * so the node cannot be reclaimed before we
1972148582Ssam				 * send the frame. ieee80211_send_nulldata
1973148582Ssam				 * understands we've done this and reclaims the
1974148582Ssam				 * ref for us as needed.
1975148582Ssam				 */
1976148582Ssam				ieee80211_ref_node(ni);
1977138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1978148301Ssam				ieee80211_send_nulldata(ni);
1979138568Ssam				/* XXX stat? */
1980138568Ssam				goto restart;
1981138568Ssam			}
1982138568Ssam		}
1983178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1984172062Ssam		    ni->ni_inact <= 0) {
1985178354Ssam			IEEE80211_NOTE(vap,
1986148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1987148320Ssam			    "station timed out due to inactivity "
1988148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1989138568Ssam			/*
1990138568Ssam			 * Send a deauthenticate frame and drop the station.
1991138568Ssam			 * This is somewhat complicated due to reference counts
1992138568Ssam			 * and locking.  At this point a station will typically
1993138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1994138568Ssam			 * will do a "free" of the node which will drop the
1995138568Ssam			 * reference count.  But in the meantime a reference
1996138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1997138568Ssam			 * of the node will happen either after the tx is
1998138568Ssam			 * completed or by ieee80211_node_leave.
1999120483Ssam			 *
2000138568Ssam			 * Separately we must drop the node lock before sending
2001170530Ssam			 * in case the driver takes a lock, as this can result
2002170530Ssam			 * in a LOR between the node lock and the driver lock.
2003119150Ssam			 */
2004172229Ssam			ieee80211_ref_node(ni);
2005138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2006138568Ssam			if (ni->ni_associd != 0) {
2007178354Ssam				IEEE80211_SEND_MGMT(ni,
2008138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
2009138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
2010138568Ssam			}
2011178354Ssam			ieee80211_node_leave(ni);
2012172229Ssam			ieee80211_free_node(ni);
2013178354Ssam			vap->iv_stats.is_node_timeout++;
2014120483Ssam			goto restart;
2015120483Ssam		}
2016116742Ssam	}
2017138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2018138568Ssam
2019178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2020170530Ssam}
2021138568Ssam
2022178354Ssam/*
2023178354Ssam * Aggressively reclaim resources.  This should be used
2024178354Ssam * only in a critical situation to reclaim mbuf resources.
2025178354Ssam */
2026170530Ssamvoid
2027178354Ssamieee80211_drain(struct ieee80211com *ic)
2028178354Ssam{
2029178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2030178354Ssam	struct ieee80211vap *vap;
2031178354Ssam	struct ieee80211_node *ni;
2032178354Ssam
2033178354Ssam	IEEE80211_NODE_LOCK(nt);
2034178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2035178354Ssam		/*
2036178354Ssam		 * Ignore entries for which have yet to receive an
2037178354Ssam		 * authentication frame.  These are transient and
2038178354Ssam		 * will be reclaimed when the last reference to them
2039178354Ssam		 * goes away (when frame xmits complete).
2040178354Ssam		 */
2041178354Ssam		vap = ni->ni_vap;
2042178354Ssam		/*
2043178354Ssam		 * Only process stations when in RUN state.  This
2044178354Ssam		 * insures, for example, that we don't timeout an
2045178354Ssam		 * inactive station during CAC.  Note that CSA state
2046178354Ssam		 * is actually handled in ieee80211_node_timeout as
2047178354Ssam		 * it applies to more than timeout processing.
2048178354Ssam		 */
2049178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2050178354Ssam			continue;
2051178354Ssam		/* XXX can vap be NULL? */
2052178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2053178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2054178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2055178354Ssam			continue;
2056178354Ssam		/*
2057178354Ssam		 * Free fragments.
2058178354Ssam		 * XXX doesn't belong here, move to node_drain
2059178354Ssam		 */
2060178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2061178354Ssam			m_freem(ni->ni_rxfrag[0]);
2062178354Ssam			ni->ni_rxfrag[0] = NULL;
2063178354Ssam		}
2064178354Ssam		/*
2065178354Ssam		 * Drain resources held by the station.
2066178354Ssam		 */
2067178354Ssam		ic->ic_node_drain(ni);
2068178354Ssam	}
2069178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2070178354Ssam}
2071178354Ssam
2072178354Ssam/*
2073178354Ssam * Per-ieee80211com inactivity timer callback.
2074178354Ssam */
2075178354Ssamvoid
2076170530Ssamieee80211_node_timeout(void *arg)
2077170530Ssam{
2078170530Ssam	struct ieee80211com *ic = arg;
2079170530Ssam
2080178354Ssam	/*
2081178354Ssam	 * Defer timeout processing if a channel switch is pending.
2082178354Ssam	 * We typically need to be mute so not doing things that
2083178354Ssam	 * might generate frames is good to handle in one place.
2084178354Ssam	 * Supressing the station timeout processing may extend the
2085178354Ssam	 * lifetime of inactive stations (by not decrementing their
2086178354Ssam	 * idle counters) but this should be ok unless the CSA is
2087178354Ssam	 * active for an unusually long time.
2088178354Ssam	 */
2089178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2090178354Ssam		ieee80211_scan_timeout(ic);
2091178354Ssam		ieee80211_timeout_stations(ic);
2092170530Ssam
2093178354Ssam		IEEE80211_LOCK(ic);
2094178354Ssam		ieee80211_erp_timeout(ic);
2095178354Ssam		ieee80211_ht_timeout(ic);
2096178354Ssam		IEEE80211_UNLOCK(ic);
2097178354Ssam	}
2098170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2099170530Ssam		ieee80211_node_timeout, ic);
2100116742Ssam}
2101116742Ssam
2102116742Ssamvoid
2103178354Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2104178354Ssam	ieee80211_iter_func *f, void *arg)
2105116742Ssam{
2106116742Ssam	struct ieee80211_node *ni;
2107138568Ssam	u_int gen;
2108116742Ssam
2109178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2110154532Ssam	gen = ++nt->nt_scangen;
2111138568Ssamrestart:
2112138568Ssam	IEEE80211_NODE_LOCK(nt);
2113138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2114138568Ssam		if (ni->ni_scangen != gen) {
2115138568Ssam			ni->ni_scangen = gen;
2116138568Ssam			(void) ieee80211_ref_node(ni);
2117138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2118138568Ssam			(*f)(arg, ni);
2119138568Ssam			ieee80211_free_node(ni);
2120138568Ssam			goto restart;
2121138568Ssam		}
2122138568Ssam	}
2123138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2124138568Ssam
2125178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2126116742Ssam}
2127138568Ssam
2128138568Ssamvoid
2129138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2130138568Ssam{
2131138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2132138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2133138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2134138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2135138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2136138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2137138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2138167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2139167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2140167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2141138568Ssam		ni->ni_rxfragstamp);
2142170530Ssam	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2143178354Ssam		ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
2144170530Ssam		ni->ni_intval, ni->ni_capinfo);
2145138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2146138568Ssam		ether_sprintf(ni->ni_bssid),
2147138568Ssam		ni->ni_esslen, ni->ni_essid,
2148138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2149184277Ssam	printf("\tinact %u inact_reload %u txrate %u\n",
2150184277Ssam		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2151170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2152170530Ssam		ni->ni_htcap, ni->ni_htparam,
2153170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2154170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2155170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2156138568Ssam}
2157138568Ssam
2158138568Ssamvoid
2159138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2160138568Ssam{
2161138568Ssam	ieee80211_iterate_nodes(nt,
2162138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2163138568Ssam}
2164138568Ssam
2165179642Ssamstatic void
2166179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2167172211Ssam{
2168178354Ssam	struct ieee80211vap *vap;
2169178354Ssam
2170178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2171178354Ssam
2172178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2173178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2174178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2175172211Ssam}
2176172211Ssam
2177179642Ssamvoid
2178179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2179179642Ssam{
2180179642Ssam	IEEE80211_LOCK(ic);
2181179642Ssam	ieee80211_notify_erp_locked(ic);
2182179642Ssam	IEEE80211_UNLOCK(ic);
2183179642Ssam}
2184179642Ssam
2185138568Ssam/*
2186138568Ssam * Handle a station joining an 11g network.
2187138568Ssam */
2188138568Ssamstatic void
2189178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2190138568Ssam{
2191178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2192138568Ssam
2193173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2194173273Ssam
2195138568Ssam	/*
2196138568Ssam	 * Station isn't capable of short slot time.  Bump
2197138568Ssam	 * the count of long slot time stations and disable
2198138568Ssam	 * use of short slot time.  Note that the actual switch
2199138568Ssam	 * over to long slot time use may not occur until the
2200138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2201138568Ssam	 */
2202138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2203138568Ssam		ic->ic_longslotsta++;
2204178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2205172211Ssam		    "station needs long slot time, count %d",
2206172211Ssam		    ic->ic_longslotsta);
2207138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2208170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2209170530Ssam			/*
2210170530Ssam			 * Don't force slot time when switched to turbo
2211170530Ssam			 * mode as non-ERP stations won't be present; this
2212170530Ssam			 * need only be done when on the normal G channel.
2213170530Ssam			 */
2214170530Ssam			ieee80211_set_shortslottime(ic, 0);
2215170530Ssam		}
2216138568Ssam	}
2217138568Ssam	/*
2218138568Ssam	 * If the new station is not an ERP station
2219138568Ssam	 * then bump the counter and enable protection
2220138568Ssam	 * if configured.
2221138568Ssam	 */
2222178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2223138568Ssam		ic->ic_nonerpsta++;
2224178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2225172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2226172211Ssam		    ic->ic_nonerpsta);
2227138568Ssam		/*
2228138568Ssam		 * If station does not support short preamble
2229138568Ssam		 * then we must enable use of Barker preamble.
2230138568Ssam		 */
2231138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2232178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2233172211Ssam			    "%s", "station needs long preamble");
2234138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2235138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2236138568Ssam		}
2237172211Ssam		/*
2238178354Ssam		 * If protection is configured and this is the first
2239178354Ssam		 * indication we should use protection, enable it.
2240172211Ssam		 */
2241172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2242172211Ssam		    ic->ic_nonerpsta == 1 &&
2243172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2244178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2245172211Ssam			    "%s: enable use of protection\n", __func__);
2246172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2247179642Ssam			ieee80211_notify_erp_locked(ic);
2248172211Ssam		}
2249138568Ssam	} else
2250138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2251138568Ssam}
2252138568Ssam
2253138568Ssamvoid
2254178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2255138568Ssam{
2256178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2257178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2258138568Ssam	int newassoc;
2259138568Ssam
2260138568Ssam	if (ni->ni_associd == 0) {
2261170530Ssam		uint16_t aid;
2262138568Ssam
2263178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2264138568Ssam		/*
2265138568Ssam		 * It would be good to search the bitmap
2266138568Ssam		 * more efficiently, but this will do for now.
2267138568Ssam		 */
2268178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2269178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2270138568Ssam				break;
2271138568Ssam		}
2272178354Ssam		if (aid >= vap->iv_max_aid) {
2273179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2274178354Ssam			ieee80211_node_leave(ni);
2275138568Ssam			return;
2276138568Ssam		}
2277138568Ssam		ni->ni_associd = aid | 0xc000;
2278173273Ssam		ni->ni_jointime = time_uptime;
2279178354Ssam		IEEE80211_LOCK(ic);
2280178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2281178354Ssam		vap->iv_sta_assoc++;
2282138568Ssam		ic->ic_sta_assoc++;
2283173273Ssam
2284173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2285173273Ssam			ieee80211_ht_node_join(ni);
2286170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2287170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2288178354Ssam			ieee80211_node_join_11g(ni);
2289173273Ssam		IEEE80211_UNLOCK(ic);
2290173273Ssam
2291173273Ssam		newassoc = 1;
2292138568Ssam	} else
2293138568Ssam		newassoc = 0;
2294138568Ssam
2295178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2296183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2297139523Ssam	    IEEE80211_NODE_AID(ni),
2298139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2299139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2300139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2301170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2302170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2303182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2304173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2305183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2306183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2307183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2308178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2309170530Ssam		", fast-frames" : "",
2310178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2311170530Ssam		", turbo" : ""
2312139523Ssam	);
2313138568Ssam
2314183251Ssam	node_setuptxparms(ni);
2315138568Ssam	/* give driver a chance to setup state like ni_txrate */
2316139524Ssam	if (ic->ic_newassoc != NULL)
2317148307Ssam		ic->ic_newassoc(ni, newassoc);
2318178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2319138568Ssam	/* tell the authenticator about new station */
2320178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2321178354Ssam		vap->iv_auth->ia_node_join(ni);
2322178354Ssam	ieee80211_notify_node_join(ni,
2323173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2324138568Ssam}
2325138568Ssam
2326172211Ssamstatic void
2327172211Ssamdisable_protection(struct ieee80211com *ic)
2328172211Ssam{
2329172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2330172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2331172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2332172211Ssam	   ic->ic_flags_ext));
2333172211Ssam
2334172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2335172211Ssam	/* XXX verify mode? */
2336172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2337172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2338172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2339172211Ssam	}
2340179642Ssam	ieee80211_notify_erp_locked(ic);
2341172211Ssam}
2342172211Ssam
2343138568Ssam/*
2344138568Ssam * Handle a station leaving an 11g network.
2345138568Ssam */
2346138568Ssamstatic void
2347178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2348138568Ssam{
2349178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2350138568Ssam
2351173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2352173273Ssam
2353170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2354178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2355178354Ssam	      ic->ic_bsschan->ic_flags));
2356138568Ssam
2357138568Ssam	/*
2358138568Ssam	 * If a long slot station do the slot time bookkeeping.
2359138568Ssam	 */
2360138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2361138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2362138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2363138568Ssam		ic->ic_longslotsta--;
2364178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2365172211Ssam		    "long slot time station leaves, count now %d",
2366172211Ssam		    ic->ic_longslotsta);
2367138568Ssam		if (ic->ic_longslotsta == 0) {
2368138568Ssam			/*
2369138568Ssam			 * Re-enable use of short slot time if supported
2370138568Ssam			 * and not operating in IBSS mode (per spec).
2371138568Ssam			 */
2372138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2373138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2374178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2375178354Ssam				    IEEE80211_MSG_ASSOC,
2376138568Ssam				    "%s: re-enable use of short slot time\n",
2377138568Ssam				    __func__);
2378138568Ssam				ieee80211_set_shortslottime(ic, 1);
2379138568Ssam			}
2380138568Ssam		}
2381138568Ssam	}
2382138568Ssam	/*
2383138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2384138568Ssam	 */
2385138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2386138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2387138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2388138568Ssam		ic->ic_nonerpsta--;
2389178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2390172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2391172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2392172211Ssam			" (non-ERP sta present)" : "");
2393172211Ssam		if (ic->ic_nonerpsta == 0 &&
2394172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2395178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2396138568Ssam				"%s: disable use of protection\n", __func__);
2397172211Ssam			disable_protection(ic);
2398138568Ssam		}
2399138568Ssam	}
2400138568Ssam}
2401138568Ssam
2402138568Ssam/*
2403172211Ssam * Time out presence of an overlapping bss with non-ERP
2404172211Ssam * stations.  When operating in hostap mode we listen for
2405172211Ssam * beacons from other stations and if we identify a non-ERP
2406172211Ssam * station is present we enable protection.  To identify
2407172211Ssam * when all non-ERP stations are gone we time out this
2408172211Ssam * condition.
2409172211Ssam */
2410172211Ssamstatic void
2411172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2412172211Ssam{
2413172211Ssam
2414172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2415172211Ssam
2416172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2417172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2418178354Ssam#if 0
2419178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2420178354Ssam		    "%s", "age out non-ERP sta present on channel");
2421178354Ssam#endif
2422172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2423172211Ssam		if (ic->ic_nonerpsta == 0)
2424172211Ssam			disable_protection(ic);
2425172211Ssam	}
2426172211Ssam}
2427172211Ssam
2428172211Ssam/*
2429138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2430138568Ssam * when operating as an ap.
2431138568Ssam */
2432138568Ssamvoid
2433178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2434138568Ssam{
2435178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2436178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2437140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2438138568Ssam
2439178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2440172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2441138568Ssam
2442178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2443178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2444138568Ssam	/*
2445138568Ssam	 * If node wasn't previously associated all
2446138568Ssam	 * we need to do is reclaim the reference.
2447138568Ssam	 */
2448138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2449138568Ssam	if (ni->ni_associd == 0)
2450138568Ssam		goto done;
2451138568Ssam	/*
2452138568Ssam	 * Tell the authenticator the station is leaving.
2453138568Ssam	 * Note that we must do this before yanking the
2454138568Ssam	 * association id as the authenticator uses the
2455138568Ssam	 * associd to locate it's state block.
2456138568Ssam	 */
2457178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2458178354Ssam		vap->iv_auth->ia_node_leave(ni);
2459173273Ssam
2460173273Ssam	IEEE80211_LOCK(ic);
2461178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2462138568Ssam	ni->ni_associd = 0;
2463178354Ssam	vap->iv_sta_assoc--;
2464138568Ssam	ic->ic_sta_assoc--;
2465138568Ssam
2466173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2467173273Ssam		ieee80211_ht_node_leave(ni);
2468170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2469170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2470178354Ssam		ieee80211_node_leave_11g(ni);
2471173273Ssam	IEEE80211_UNLOCK(ic);
2472138568Ssam	/*
2473138568Ssam	 * Cleanup station state.  In particular clear various
2474138568Ssam	 * state that might otherwise be reused if the node
2475138568Ssam	 * is reused before the reference count goes to zero
2476138568Ssam	 * (and memory is reclaimed).
2477138568Ssam	 */
2478178354Ssam	ieee80211_sta_leave(ni);
2479138568Ssamdone:
2480140499Ssam	/*
2481140499Ssam	 * Remove the node from any table it's recorded in and
2482140499Ssam	 * drop the caller's reference.  Removal from the table
2483140499Ssam	 * is important to insure the node is not reprocessed
2484140499Ssam	 * for inactivity.
2485140499Ssam	 */
2486140499Ssam	if (nt != NULL) {
2487140499Ssam		IEEE80211_NODE_LOCK(nt);
2488140499Ssam		node_reclaim(nt, ni);
2489140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2490140499Ssam	} else
2491140499Ssam		ieee80211_free_node(ni);
2492138568Ssam}
2493138568Ssam
2494178354Ssamstruct rssiinfo {
2495178354Ssam	struct ieee80211vap *vap;
2496178354Ssam	int	rssi_samples;
2497178354Ssam	uint32_t rssi_total;
2498178354Ssam};
2499178354Ssam
2500178354Ssamstatic void
2501178354Ssamget_hostap_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 associated stations */
2510178354Ssam	if (ni->ni_associd == 0)
2511178354Ssam		return;
2512178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2513178354Ssam	if (rssi != 0) {
2514178354Ssam		info->rssi_samples++;
2515178354Ssam		info->rssi_total += rssi;
2516178354Ssam	}
2517178354Ssam}
2518178354Ssam
2519178354Ssamstatic void
2520178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2521178354Ssam{
2522178354Ssam	struct rssiinfo *info = arg;
2523178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2524178354Ssam	int8_t rssi;
2525178354Ssam
2526178354Ssam	if (info->vap != vap)
2527178354Ssam		return;
2528178354Ssam	/* only neighbors */
2529178354Ssam	/* XXX check bssid */
2530178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2531178354Ssam		return;
2532178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2533178354Ssam	if (rssi != 0) {
2534178354Ssam		info->rssi_samples++;
2535178354Ssam		info->rssi_total += rssi;
2536178354Ssam	}
2537178354Ssam}
2538178354Ssam
2539170530Ssamint8_t
2540178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2541138568Ssam{
2542138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2543178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2544178354Ssam	struct rssiinfo info;
2545138568Ssam
2546178354Ssam	info.rssi_total = 0;
2547178354Ssam	info.rssi_samples = 0;
2548178354Ssam	info.vap = vap;
2549178354Ssam	switch (vap->iv_opmode) {
2550138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2551138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2552178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2553178354Ssam		break;
2554138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2555178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2556138568Ssam		break;
2557138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2558138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2559138568Ssam	default:
2560178354Ssam		if (vap->iv_bss != NULL)
2561178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2562178354Ssam		info.rssi_samples = 1;
2563138568Ssam		break;
2564138568Ssam	}
2565178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2566138568Ssam#undef NZ
2567138568Ssam}
2568138568Ssam
2569170530Ssamvoid
2570178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2571138568Ssam{
2572138568Ssam
2573178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2574170530Ssam		return;
2575178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2576170530Ssam	/* for non-station mode return avg'd rssi accounting */
2577178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2578178354Ssam		*rssi = ieee80211_getrssi(vap);
2579138568Ssam}
2580