ieee80211_node.c revision 186904
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 186904 2009-01-08 17:12:47Z 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>
46186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
47186904Ssam#include <net80211/ieee80211_tdma.h>
48186904Ssam#endif
49178354Ssam#include <net80211/ieee80211_wds.h>
50116742Ssam
51116742Ssam#include <net/bpf.h>
52116742Ssam
53147221Ssam/*
54147221Ssam * Association id's are managed with a bit vector.
55147221Ssam */
56178354Ssam#define	IEEE80211_AID_SET(_vap, b) \
57178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
58178354Ssam		(1 << (IEEE80211_AID(b) % 32)))
59178354Ssam#define	IEEE80211_AID_CLR(_vap, b) \
60178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
61178354Ssam		~(1 << (IEEE80211_AID(b) % 32)))
62178354Ssam#define	IEEE80211_AID_ISSET(_vap, b) \
63178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
64147221Ssam
65159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
66159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
67159139Sdds#else
68159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
69159139Sdds#endif
70159139Sdds
71170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
72170530Ssam
73179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *,
74179643Ssam	const uint8_t [IEEE80211_ADDR_LEN]);
75138568Ssamstatic void node_cleanup(struct ieee80211_node *);
76138568Ssamstatic void node_free(struct ieee80211_node *);
77178354Ssamstatic void node_age(struct ieee80211_node *);
78170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
79170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
80178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *,
81178354Ssam	struct ieee80211_mimo_info *);
82116742Ssam
83138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
84120104Ssam
85138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
86148863Ssam	struct ieee80211_node_table *nt, const char *name,
87170530Ssam	int inact, int keymaxix);
88178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *,
89178354Ssam	struct ieee80211vap *);
90178354Ssamstatic void ieee80211_node_reclaim(struct ieee80211_node *);
91138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
92172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
93138568Ssam
94127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
95178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
96120481Ssam
97116742Ssamvoid
98138568Ssamieee80211_node_attach(struct ieee80211com *ic)
99116742Ssam{
100178354Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
101178354Ssam		IEEE80211_INACT_INIT, ic->ic_max_keyix);
102178354Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
103178354Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
104178354Ssam		ieee80211_node_timeout, ic);
105116742Ssam
106138568Ssam	ic->ic_node_alloc = node_alloc;
107138568Ssam	ic->ic_node_free = node_free;
108138568Ssam	ic->ic_node_cleanup = node_cleanup;
109178354Ssam	ic->ic_node_age = node_age;
110178354Ssam	ic->ic_node_drain = node_age;		/* NB: same as age */
111138568Ssam	ic->ic_node_getrssi = node_getrssi;
112170530Ssam	ic->ic_node_getsignal = node_getsignal;
113178354Ssam	ic->ic_node_getmimoinfo = node_getmimoinfo;
114138568Ssam
115178354Ssam	/*
116178354Ssam	 * Set flags to be propagated to all vap's;
117178354Ssam	 * these define default behaviour/configuration.
118178354Ssam	 */
119178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
120178354Ssam}
121138568Ssam
122178354Ssamvoid
123178354Ssamieee80211_node_detach(struct ieee80211com *ic)
124178354Ssam{
125170530Ssam
126178354Ssam	callout_drain(&ic->ic_inact);
127178354Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
128178354Ssam}
129172062Ssam
130178354Ssamvoid
131178354Ssamieee80211_node_vattach(struct ieee80211vap *vap)
132178354Ssam{
133178354Ssam	/* NB: driver can override */
134178354Ssam	vap->iv_max_aid = IEEE80211_AID_DEF;
135178354Ssam
136178354Ssam	/* default station inactivity timer setings */
137178354Ssam	vap->iv_inact_init = IEEE80211_INACT_INIT;
138178354Ssam	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
139178354Ssam	vap->iv_inact_run = IEEE80211_INACT_RUN;
140178354Ssam	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
141184277Ssam
142184277Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
143184277Ssam	    "%s: init %u auth %u run %u probe %u\n", __func__,
144184277Ssam	    vap->iv_inact_init, vap->iv_inact_auth,
145184277Ssam	    vap->iv_inact_run, vap->iv_inact_probe);
146148863Ssam}
147148863Ssam
148148863Ssamvoid
149178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap)
150148863Ssam{
151178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
152178354Ssam		/* XXX should we allow max aid to be zero? */
153178354Ssam		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
154178354Ssam			vap->iv_max_aid = IEEE80211_AID_MIN;
155178354Ssam			if_printf(vap->iv_ifp,
156178354Ssam			    "WARNING: max aid too small, changed to %d\n",
157178354Ssam			    vap->iv_max_aid);
158178354Ssam		}
159186302Ssam		vap->iv_aid_bitmap = (uint32_t *) malloc(
160184210Sdes			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
161178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
162178354Ssam		if (vap->iv_aid_bitmap == NULL) {
163178354Ssam			/* XXX no way to recover */
164178354Ssam			printf("%s: no memory for AID bitmap, max aid %d!\n",
165178354Ssam			    __func__, vap->iv_max_aid);
166178354Ssam			vap->iv_max_aid = 0;
167178354Ssam		}
168138568Ssam	}
169138568Ssam
170178354Ssam	ieee80211_reset_bss(vap);
171118887Ssam
172178354Ssam	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
173116742Ssam}
174116742Ssam
175116742Ssamvoid
176178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap)
177116742Ssam{
178178354Ssam	struct ieee80211com *ic = vap->iv_ic;
179116742Ssam
180178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
181178354Ssam	if (vap->iv_bss != NULL) {
182178354Ssam		ieee80211_free_node(vap->iv_bss);
183178354Ssam		vap->iv_bss = NULL;
184138568Ssam	}
185178354Ssam	if (vap->iv_aid_bitmap != NULL) {
186186302Ssam		free(vap->iv_aid_bitmap, M_80211_NODE);
187178354Ssam		vap->iv_aid_bitmap = NULL;
188138568Ssam	}
189116742Ssam}
190116742Ssam
191138568Ssam/*
192138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
193138568Ssam */
194138568Ssam
195138568Ssamvoid
196148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
197138568Ssam{
198184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
199184277Ssam
200138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
201184277Ssam	ni->ni_inact_reload = vap->iv_inact_run;
202172062Ssam	ni->ni_inact = ni->ni_inact_reload;
203184277Ssam
204184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
205184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
206138568Ssam}
207138568Ssam
208138568Ssamvoid
209148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
210138568Ssam{
211184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
212184277Ssam
213138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
214184277Ssam	ni->ni_inact_reload = vap->iv_inact_auth;
215172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
216172062Ssam		ni->ni_inact = ni->ni_inact_reload;
217184277Ssam
218184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
219184277Ssam	    "%s: inact_reload %u inact %u", __func__,
220184277Ssam	    ni->ni_inact_reload, ni->ni_inact);
221138568Ssam}
222138568Ssam
223116742Ssam/*
224183251Ssam * Fix tx parameters for a node according to ``association state''.
225183251Ssam */
226183251Ssamstatic void
227183251Ssamnode_setuptxparms(struct ieee80211_node *ni)
228183251Ssam{
229183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
230183251Ssam
231183251Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
232183251Ssam		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
233183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NA];
234183251Ssam		else
235183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NG];
236183251Ssam	} else {				/* legacy rate handling */
237183251Ssam		if (IEEE80211_IS_CHAN_A(ni->ni_chan))
238183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11A];
239183251Ssam		else if (ni->ni_flags & IEEE80211_NODE_ERP)
240183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11G];
241183251Ssam		else
242183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11B];
243183251Ssam	}
244183251Ssam}
245183251Ssam
246183251Ssam/*
247138568Ssam * Set/change the channel.  The rate set is also updated as
248138568Ssam * to insure a consistent view by drivers.
249178354Ssam * XXX should be private but hostap needs it to deal with CSA
250138568Ssam */
251178354Ssamvoid
252178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni,
253178354Ssam	struct ieee80211_channel *chan)
254138568Ssam{
255178354Ssam	struct ieee80211com *ic = ni->ni_ic;
256183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
257183251Ssam	enum ieee80211_phymode mode;
258170530Ssam
259178354Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
260178354Ssam
261138568Ssam	ni->ni_chan = chan;
262183251Ssam	mode = ieee80211_chan2mode(chan);
263170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
264170530Ssam		/*
265170530Ssam		 * XXX Gotta be careful here; the rate set returned by
266170530Ssam		 * ieee80211_get_suprates is actually any HT rate
267170530Ssam		 * set so blindly copying it will be bad.  We must
268170530Ssam		 * install the legacy rate est in ni_rates and the
269170530Ssam		 * HT rate set in ni_htrates.
270170530Ssam		 */
271170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
272183251Ssam		/*
273183251Ssam		 * Setup bss tx parameters based on operating mode.  We
274183251Ssam		 * use legacy rates when operating in a mixed HT+non-HT bss
275183251Ssam		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
276183251Ssam		 */
277183251Ssam		if (mode == IEEE80211_MODE_11NA &&
278183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
279183251Ssam			mode = IEEE80211_MODE_11A;
280183251Ssam		else if (mode == IEEE80211_MODE_11NG &&
281183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
282183251Ssam			mode = IEEE80211_MODE_11G;
283183251Ssam		if (mode == IEEE80211_MODE_11G &&
284183251Ssam		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
285183251Ssam			mode = IEEE80211_MODE_11B;
286170530Ssam	}
287183251Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
288165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
289138568Ssam}
290138568Ssam
291141658Ssamstatic __inline void
292141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
293141658Ssam{
294141658Ssam	/* propagate useful state */
295141658Ssam	nbss->ni_authmode = obss->ni_authmode;
296141658Ssam	nbss->ni_txpower = obss->ni_txpower;
297141658Ssam	nbss->ni_vlan = obss->ni_vlan;
298141658Ssam	/* XXX statistics? */
299178354Ssam	/* XXX legacy WDS bssid? */
300141658Ssam}
301141658Ssam
302116742Ssamvoid
303178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
304116742Ssam{
305178354Ssam	struct ieee80211com *ic = vap->iv_ic;
306116742Ssam	struct ieee80211_node *ni;
307116742Ssam
308178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
309178354Ssam		"%s: creating ibss on channel %u\n", __func__,
310178354Ssam		ieee80211_chan2ieee(ic, chan));
311138568Ssam
312178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
313140753Ssam	if (ni == NULL) {
314140753Ssam		/* XXX recovery? */
315138568Ssam		return;
316138568Ssam	}
317178354Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
318178354Ssam	ni->ni_esslen = vap->iv_des_ssid[0].len;
319178354Ssam	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
320178354Ssam	if (vap->iv_bss != NULL)
321178354Ssam		copy_bss(ni, vap->iv_bss);
322148843Ssam	ni->ni_intval = ic->ic_bintval;
323178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
324116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
325116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
326116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
327116742Ssam		ni->ni_fhindex = 1;
328116742Ssam	}
329178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
330178354Ssam		vap->iv_flags |= IEEE80211_F_SIBSS;
331138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
332178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
333178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
334167282Ssam		else {
335167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
336167282Ssam			/* clear group bit, add local bit */
337167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
338167282Ssam		}
339178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
340178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
341178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
342153403Ssam		else
343186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
344186904Ssam		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
345186904Ssam#endif
346153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
347138568Ssam	}
348138568Ssam	/*
349138568Ssam	 * Fix the channel and related attributes.
350138568Ssam	 */
351178354Ssam	/* clear DFS CAC state on previous channel */
352178354Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
353178354Ssam	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
354178354Ssam	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
355178354Ssam		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
356170530Ssam	ic->ic_bsschan = chan;
357178354Ssam	ieee80211_node_set_chan(ni, chan);
358170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
359138568Ssam	/*
360178354Ssam	 * Do mode-specific setup.
361138568Ssam	 */
362170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
363170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
364170530Ssam			/*
365178354Ssam			 * Use a mixed 11b/11g basic rate set.
366170530Ssam			 */
367178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
368178354Ssam			    IEEE80211_MODE_11G);
369178354Ssam			if (vap->iv_flags & IEEE80211_F_PUREG) {
370178354Ssam				/*
371178354Ssam				 * Also mark OFDM rates basic so 11b
372178354Ssam				 * stations do not join (WiFi compliance).
373178354Ssam				 */
374178354Ssam				ieee80211_addbasicrates(&ni->ni_rates,
375178354Ssam				    IEEE80211_MODE_11A);
376178354Ssam			}
377170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
378170530Ssam			/*
379170530Ssam			 * Force pure 11b rate set.
380170530Ssam			 */
381178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
382170530Ssam				IEEE80211_MODE_11B);
383170530Ssam		}
384170530Ssam	}
385138568Ssam
386170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
387116742Ssam}
388116742Ssam
389170530Ssam/*
390170530Ssam * Reset bss state on transition to the INIT state.
391170530Ssam * Clear any stations from the table (they have been
392170530Ssam * deauth'd) and reset the bss node (clears key, rate
393170530Ssam * etc. state).
394170530Ssam */
395138568Ssamvoid
396178354Ssamieee80211_reset_bss(struct ieee80211vap *vap)
397138568Ssam{
398178354Ssam	struct ieee80211com *ic = vap->iv_ic;
399138568Ssam	struct ieee80211_node *ni, *obss;
400138568Ssam
401178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
402178354Ssam	/* XXX multi-bss: wrong */
403170530Ssam	ieee80211_reset_erp(ic);
404140753Ssam
405178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
406138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
407178354Ssam	obss = vap->iv_bss;
408178354Ssam	vap->iv_bss = ieee80211_ref_node(ni);
409141658Ssam	if (obss != NULL) {
410141658Ssam		copy_bss(ni, obss);
411148843Ssam		ni->ni_intval = ic->ic_bintval;
412138568Ssam		ieee80211_free_node(obss);
413178354Ssam	} else
414178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
415138568Ssam}
416138568Ssam
417170530Ssamstatic int
418170530Ssammatch_ssid(const struct ieee80211_node *ni,
419170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
420170530Ssam{
421170530Ssam	int i;
422148432Ssam
423170530Ssam	for (i = 0; i < nssid; i++) {
424170530Ssam		if (ni->ni_esslen == ssids[i].len &&
425170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
426170530Ssam			return 1;
427170530Ssam	}
428170530Ssam	return 0;
429170530Ssam}
430170530Ssam
431170530Ssam/*
432170530Ssam * Test a node for suitability/compatibility.
433170530Ssam */
434127767Ssamstatic int
435178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
436127767Ssam{
437178354Ssam	struct ieee80211com *ic = ni->ni_ic;
438170530Ssam        uint8_t rate;
439170530Ssam
440170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
441170530Ssam		return 0;
442178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
443170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
444170530Ssam			return 0;
445170530Ssam	} else {
446170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
447170530Ssam			return 0;
448170530Ssam	}
449178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
450170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
451170530Ssam			return 0;
452170530Ssam	} else {
453170530Ssam		/* XXX does this mean privacy is supported or required? */
454170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
455170530Ssam			return 0;
456170530Ssam	}
457170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
458170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
459170530Ssam	if (rate & IEEE80211_RATE_BASIC)
460170530Ssam		return 0;
461178354Ssam	if (vap->iv_des_nssid != 0 &&
462178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
463170530Ssam		return 0;
464178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
465178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
466170530Ssam		return 0;
467170530Ssam	return 1;
468170530Ssam}
469170530Ssam
470170530Ssam#ifdef IEEE80211_DEBUG
471170530Ssam/*
472170530Ssam * Display node suitability/compatibility.
473170530Ssam */
474170530Ssamstatic void
475178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
476170530Ssam{
477178354Ssam	struct ieee80211com *ic = ni->ni_ic;
478170530Ssam        uint8_t rate;
479127767Ssam        int fail;
480127767Ssam
481127767Ssam	fail = 0;
482127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
483127767Ssam		fail |= 0x01;
484178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
485127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
486127767Ssam			fail |= 0x02;
487127767Ssam	} else {
488127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
489127767Ssam			fail |= 0x02;
490127767Ssam	}
491178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
492127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
493127767Ssam			fail |= 0x04;
494127767Ssam	} else {
495127767Ssam		/* XXX does this mean privacy is supported or required? */
496127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
497127767Ssam			fail |= 0x04;
498127767Ssam	}
499167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
500165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
501127767Ssam	if (rate & IEEE80211_RATE_BASIC)
502127767Ssam		fail |= 0x08;
503178354Ssam	if (vap->iv_des_nssid != 0 &&
504178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
505127767Ssam		fail |= 0x10;
506178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
507178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
508127767Ssam		fail |= 0x20;
509127767Ssam
510170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
511170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
512170530Ssam	printf(" %3d%c",
513170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
514170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
515170530Ssam	    fail & 0x08 ? '!' : ' ');
516170530Ssam	printf(" %4s%c",
517170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
518170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
519170530Ssam	    "????",
520170530Ssam	    fail & 0x02 ? '!' : ' ');
521170530Ssam	printf(" %3s%c ",
522170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
523170530Ssam	    fail & 0x04 ? '!' : ' ');
524170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
525170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
526138568Ssam}
527170530Ssam#endif /* IEEE80211_DEBUG */
528138568Ssam
529138568Ssam/*
530138568Ssam * Handle 802.11 ad hoc network merge.  The
531138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
532138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
533138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
534138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
535138568Ssam * sends beacons with the greatest TSF timestamp.
536138568Ssam *
537138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
538138568Ssam *
539138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
540138568Ssam */
541138568Ssamint
542148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
543138568Ssam{
544178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
545178354Ssam#ifdef IEEE80211_DEBUG
546148306Ssam	struct ieee80211com *ic = ni->ni_ic;
547178354Ssam#endif
548138568Ssam
549178354Ssam	if (ni == vap->iv_bss ||
550178354Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
551138568Ssam		/* unchanged, nothing to do */
552138568Ssam		return 0;
553138568Ssam	}
554178354Ssam	if (!check_bss(vap, ni)) {
555170530Ssam		/* capabilities mismatch */
556178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
557138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
558170530Ssam#ifdef IEEE80211_DEBUG
559178354Ssam		if (ieee80211_msg_assoc(vap))
560178354Ssam			check_bss_debug(vap, ni);
561170530Ssam#endif
562178354Ssam		vap->iv_stats.is_ibss_capmismatch++;
563138568Ssam		return 0;
564138568Ssam	}
565178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
566138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
567138568Ssam		ether_sprintf(ni->ni_bssid),
568138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
569138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
570138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
571138568Ssam	);
572170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
573138568Ssam}
574138568Ssam
575138568Ssam/*
576178354Ssam * Calculate HT channel promotion flags for all vaps.
577178354Ssam * This assumes ni_chan have been setup for each vap.
578173273Ssam */
579178354Ssamstatic int
580178354Ssamgethtadjustflags(struct ieee80211com *ic)
581178354Ssam{
582178354Ssam	struct ieee80211vap *vap;
583178354Ssam	int flags;
584178354Ssam
585178354Ssam	flags = 0;
586178354Ssam	/* XXX locking */
587178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
588178354Ssam		if (vap->iv_state < IEEE80211_S_RUN)
589178354Ssam			continue;
590178354Ssam		switch (vap->iv_opmode) {
591178354Ssam		case IEEE80211_M_WDS:
592178354Ssam		case IEEE80211_M_STA:
593178354Ssam		case IEEE80211_M_AHDEMO:
594178354Ssam		case IEEE80211_M_HOSTAP:
595178354Ssam		case IEEE80211_M_IBSS:
596178354Ssam			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
597178354Ssam			break;
598178354Ssam		default:
599178354Ssam			break;
600178354Ssam		}
601178354Ssam	}
602178354Ssam	return flags;
603178354Ssam}
604178354Ssam
605178354Ssam/*
606178354Ssam * Check if the current channel needs to change based on whether
607184303Ssam * any vap's are using HT20/HT40.  This is used to sync the state
608184303Ssam * of ic_curchan after a channel width change on a running vap.
609178354Ssam */
610173273Ssamvoid
611178354Ssamieee80211_sync_curchan(struct ieee80211com *ic)
612173273Ssam{
613178354Ssam	struct ieee80211_channel *c;
614178354Ssam
615178354Ssam	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
616178354Ssam	if (c != ic->ic_curchan) {
617178354Ssam		ic->ic_curchan = c;
618178354Ssam		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
619178354Ssam		ic->ic_set_channel(ic);
620178354Ssam	}
621178354Ssam}
622178354Ssam
623178354Ssam/*
624178354Ssam * Change the current channel.  The request channel may be
625178354Ssam * promoted if other vap's are operating with HT20/HT40.
626178354Ssam */
627178354Ssamvoid
628178354Ssamieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
629178354Ssam{
630178354Ssam	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
631178354Ssam		int flags = gethtadjustflags(ic);
632178354Ssam		/*
633178354Ssam		 * Check for channel promotion required to support the
634178354Ssam		 * set of running vap's.  This assumes we are called
635178354Ssam		 * after ni_chan is setup for each vap.
636178354Ssam		 */
637178354Ssam		/* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
638178354Ssam		if (flags > ieee80211_htchanflags(c))
639178354Ssam			c = ieee80211_ht_adjust_channel(ic, c, flags);
640178354Ssam	}
641178354Ssam	ic->ic_bsschan = ic->ic_curchan = c;
642173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
643173273Ssam	ic->ic_set_channel(ic);
644173273Ssam}
645173273Ssam
646173273Ssam/*
647138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
648138568Ssam * be passed in with a held reference.
649138568Ssam */
650170530Ssamstatic int
651170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
652138568Ssam{
653178354Ssam	struct ieee80211vap *vap = selbs->ni_vap;
654170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
655138568Ssam	struct ieee80211_node *obss;
656170530Ssam	int canreassoc;
657138568Ssam
658138568Ssam	/*
659138568Ssam	 * Committed to selbs, setup state.
660138568Ssam	 */
661178354Ssam	obss = vap->iv_bss;
662170530Ssam	/*
663170530Ssam	 * Check if old+new node have the same address in which
664170530Ssam	 * case we can reassociate when operating in sta mode.
665170530Ssam	 */
666170530Ssam	canreassoc = (obss != NULL &&
667178354Ssam		vap->iv_state == IEEE80211_S_RUN &&
668170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
669178354Ssam	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
670153352Ssam	if (obss != NULL) {
671153352Ssam		copy_bss(selbs, obss);
672178354Ssam		ieee80211_node_reclaim(obss);
673178354Ssam		obss = NULL;		/* NB: guard against later use */
674153352Ssam	}
675165887Ssam
676138568Ssam	/*
677165887Ssam	 * Delete unusable rates; we've already checked
678165887Ssam	 * that the negotiated rate set is acceptable.
679165887Ssam	 */
680178354Ssam	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
681167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
682165887Ssam
683178354Ssam	ieee80211_setcurchan(ic, selbs->ni_chan);
684165887Ssam	/*
685138568Ssam	 * Set the erp state (mostly the slot time) to deal with
686138568Ssam	 * the auto-select case; this should be redundant if the
687138568Ssam	 * mode is locked.
688138568Ssam	 */
689138568Ssam	ieee80211_reset_erp(ic);
690178354Ssam	ieee80211_wme_initparams(vap);
691140753Ssam
692178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
693170530Ssam		if (canreassoc) {
694170530Ssam			/* Reassociate */
695178354Ssam			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
696170530Ssam		} else {
697170530Ssam			/*
698170530Ssam			 * Act as if we received a DEAUTH frame in case we
699170530Ssam			 * are invoked from the RUN state.  This will cause
700170530Ssam			 * us to try to re-authenticate if we are operating
701170530Ssam			 * as a station.
702170530Ssam			 */
703178354Ssam			ieee80211_new_state(vap, IEEE80211_S_AUTH,
704170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
705170530Ssam		}
706170530Ssam	} else
707178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
708138568Ssam	return 1;
709116742Ssam}
710116742Ssam
711170530Ssamint
712184274Ssamieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
713170530Ssam	const struct ieee80211_scan_entry *se)
714170530Ssam{
715178354Ssam	struct ieee80211com *ic = vap->iv_ic;
716170530Ssam	struct ieee80211_node *ni;
717170530Ssam
718178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
719170530Ssam	if (ni == NULL) {
720170530Ssam		/* XXX msg */
721170530Ssam		return 0;
722170530Ssam	}
723170530Ssam	/*
724170530Ssam	 * Expand scan state into node's format.
725170530Ssam	 * XXX may not need all this stuff
726170530Ssam	 */
727170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
728170530Ssam	ni->ni_esslen = se->se_ssid[1];
729170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
730170530Ssam	ni->ni_rstamp = se->se_rstamp;
731170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
732170530Ssam	ni->ni_intval = se->se_intval;
733170530Ssam	ni->ni_capinfo = se->se_capinfo;
734184274Ssam	ni->ni_chan = chan;
735170530Ssam	ni->ni_timoff = se->se_timoff;
736170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
737170530Ssam	ni->ni_fhindex = se->se_fhindex;
738170530Ssam	ni->ni_erp = se->se_erp;
739178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
740170530Ssam	ni->ni_noise = se->se_noise;
741186870Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
742186870Ssam		/* NB: only infrastructure mode requires an associd */
743186870Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
744186870Ssam	}
745178354Ssam
746178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
747178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
748178354Ssam		if (ni->ni_ies.ath_ie != NULL)
749178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
750178354Ssam		if (ni->ni_ies.htcap_ie != NULL)
751178354Ssam			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
752178354Ssam		if (ni->ni_ies.htinfo_ie != NULL)
753178354Ssam			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
754186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
755186904Ssam		if (ni->ni_ies.tdma_ie != NULL)
756186904Ssam			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
757186904Ssam#endif
758173864Ssam	}
759170530Ssam
760178354Ssam	vap->iv_dtim_period = se->se_dtimperiod;
761178354Ssam	vap->iv_dtim_count = 0;
762170530Ssam
763170530Ssam	/* NB: must be after ni_chan is setup */
764170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
765170530Ssam		IEEE80211_F_DOSORT);
766184279Ssam	if (ieee80211_iserp_rateset(&ni->ni_rates))
767184279Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
768184279Ssam	node_setuptxparms(ni);
769170530Ssam
770170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
771170530Ssam}
772170530Ssam
773138568Ssam/*
774138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
775138568Ssam * be passed in with a held reference.
776138568Ssam */
777138568Ssamvoid
778178354Ssamieee80211_sta_leave(struct ieee80211_node *ni)
779138568Ssam{
780178354Ssam	struct ieee80211com *ic = ni->ni_ic;
781178354Ssam
782138568Ssam	ic->ic_node_cleanup(ni);
783178354Ssam	ieee80211_notify_node_leave(ni);
784138568Ssam}
785138568Ssam
786178354Ssam/*
787178354Ssam * Send a deauthenticate frame and drop the station.
788178354Ssam */
789178354Ssamvoid
790178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason)
791178354Ssam{
792178354Ssam	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
793178354Ssam	ieee80211_ref_node(ni);
794178354Ssam	if (ni->ni_associd != 0)
795178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
796178354Ssam	ieee80211_node_leave(ni);
797178354Ssam	ieee80211_free_node(ni);
798178354Ssam}
799178354Ssam
800116742Ssamstatic struct ieee80211_node *
801179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
802116742Ssam{
803127768Ssam	struct ieee80211_node *ni;
804138568Ssam
805186302Ssam	ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
806127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
807127768Ssam	return ni;
808116742Ssam}
809116742Ssam
810138568Ssam/*
811178354Ssam * Initialize an ie blob with the specified data.  If previous
812178354Ssam * data exists re-use the data block.  As a side effect we clear
813178354Ssam * all references to specific ie's; the caller is required to
814178354Ssam * recalculate them.
815178354Ssam */
816178354Ssamint
817178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
818178354Ssam{
819178354Ssam	/* NB: assumes data+len are the last fields */
820178354Ssam	memset(ies, 0, offsetof(struct ieee80211_ies, data));
821178354Ssam	if (ies->data != NULL && ies->len != len) {
822178354Ssam		/* data size changed */
823186302Ssam		free(ies->data, M_80211_NODE_IE);
824178354Ssam		ies->data = NULL;
825178354Ssam	}
826178354Ssam	if (ies->data == NULL) {
827186302Ssam		ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
828178354Ssam		if (ies->data == NULL) {
829178354Ssam			ies->len = 0;
830178354Ssam			/* NB: pointers have already been zero'd above */
831178354Ssam			return 0;
832178354Ssam		}
833178354Ssam	}
834178354Ssam	memcpy(ies->data, data, len);
835178354Ssam	ies->len = len;
836178354Ssam	return 1;
837178354Ssam}
838178354Ssam
839178354Ssam/*
840178354Ssam * Reclaim storage for an ie blob.
841178354Ssam */
842178354Ssamvoid
843178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies)
844178354Ssam{
845178354Ssam	if (ies->data != NULL)
846186302Ssam		free(ies->data, M_80211_NODE_IE);
847178354Ssam}
848178354Ssam
849178354Ssam/*
850178354Ssam * Expand an ie blob data contents and to fillin individual
851178354Ssam * ie pointers.  The data blob is assumed to be well-formed;
852178354Ssam * we don't do any validity checking of ie lengths.
853178354Ssam */
854178354Ssamvoid
855178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies)
856178354Ssam{
857178354Ssam	uint8_t *ie;
858178354Ssam	int ielen;
859178354Ssam
860178354Ssam	ie = ies->data;
861178354Ssam	ielen = ies->len;
862178354Ssam	while (ielen > 0) {
863178354Ssam		switch (ie[0]) {
864178354Ssam		case IEEE80211_ELEMID_VENDOR:
865178354Ssam			if (iswpaoui(ie))
866178354Ssam				ies->wpa_ie = ie;
867178354Ssam			else if (iswmeoui(ie))
868178354Ssam				ies->wme_ie = ie;
869178354Ssam			else if (isatherosoui(ie))
870178354Ssam				ies->ath_ie = ie;
871186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
872186904Ssam			else if (istdmaoui(ie))
873186904Ssam				ies->tdma_ie = ie;
874186904Ssam#endif
875178354Ssam			break;
876178354Ssam		case IEEE80211_ELEMID_RSN:
877178354Ssam			ies->rsn_ie = ie;
878178354Ssam			break;
879178354Ssam		case IEEE80211_ELEMID_HTCAP:
880178354Ssam			ies->htcap_ie = ie;
881178354Ssam			break;
882178354Ssam		}
883178354Ssam		ielen -= 2 + ie[1];
884178354Ssam		ie += 2 + ie[1];
885178354Ssam	}
886178354Ssam}
887178354Ssam
888178354Ssam/*
889138568Ssam * Reclaim any resources in a node and reset any critical
890138568Ssam * state.  Typically nodes are free'd immediately after,
891138568Ssam * but in some cases the storage may be reused so we need
892138568Ssam * to insure consistent state (should probably fix that).
893138568Ssam */
894116742Ssamstatic void
895138568Ssamnode_cleanup(struct ieee80211_node *ni)
896116742Ssam{
897138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
898178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
899170530Ssam	int i;
900138568Ssam
901138568Ssam	/* NB: preserve ni_table */
902138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
903178354Ssam		if (vap->iv_opmode != IEEE80211_M_STA)
904178354Ssam			vap->iv_ps_sta--;
905138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
906178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
907178354Ssam		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
908138568Ssam	}
909147788Ssam	/*
910173273Ssam	 * Cleanup any HT-related state.
911173273Ssam	 */
912173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
913173273Ssam		ieee80211_ht_node_cleanup(ni);
914173273Ssam	/*
915147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
916147788Ssam	 * has happened.  This is probably not needed as the node
917147788Ssam	 * should always be removed from the table so not found but
918147788Ssam	 * do it just in case.
919186099Ssam	 * Likewise clear the ASSOCID flag as these flags are intended
920186099Ssam	 * to be managed in tandem.
921147788Ssam	 */
922186099Ssam	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
923138568Ssam
924138568Ssam	/*
925138568Ssam	 * Drain power save queue and, if needed, clear TIM.
926138568Ssam	 */
927184288Ssam	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
928178354Ssam		vap->iv_set_tim(ni, 0);
929138568Ssam
930138568Ssam	ni->ni_associd = 0;
931138568Ssam	if (ni->ni_challenge != NULL) {
932186302Ssam		free(ni->ni_challenge, M_80211_NODE);
933138568Ssam		ni->ni_challenge = NULL;
934138568Ssam	}
935138568Ssam	/*
936138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
937138568Ssam	 * reusable during a re-auth/re-assoc state transition.
938138568Ssam	 * If we remove these data they will not be recreated
939138568Ssam	 * because they come from a probe-response or beacon frame
940138568Ssam	 * which cannot be expected prior to the association-response.
941138568Ssam	 * This should not be an issue when operating in other modes
942138568Ssam	 * as stations leaving always go through a full state transition
943138568Ssam	 * which will rebuild this state.
944138568Ssam	 *
945138568Ssam	 * XXX does this leave us open to inheriting old state?
946138568Ssam	 */
947138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
948138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
949138568Ssam			m_freem(ni->ni_rxfrag[i]);
950138568Ssam			ni->ni_rxfrag[i] = NULL;
951138568Ssam		}
952148863Ssam	/*
953148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
954148863Ssam	 */
955148863Ssam	ieee80211_node_delucastkey(ni);
956138568Ssam#undef N
957116742Ssam}
958116742Ssam
959116742Ssamstatic void
960138568Ssamnode_free(struct ieee80211_node *ni)
961116742Ssam{
962138568Ssam	struct ieee80211com *ic = ni->ni_ic;
963138568Ssam
964138568Ssam	ic->ic_node_cleanup(ni);
965178354Ssam	ieee80211_ies_cleanup(&ni->ni_ies);
966184288Ssam	ieee80211_psq_cleanup(&ni->ni_psq);
967178354Ssam	IEEE80211_NODE_WDSQ_DESTROY(ni);
968186302Ssam	free(ni, M_80211_NODE);
969116742Ssam}
970116742Ssam
971178354Ssamstatic void
972178354Ssamnode_age(struct ieee80211_node *ni)
973178354Ssam{
974178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
975184303Ssam
976184303Ssam	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
977184303Ssam
978178354Ssam	/*
979178354Ssam	 * Age frames on the power save queue.
980178354Ssam	 */
981184288Ssam	if (ieee80211_node_psq_age(ni) != 0 &&
982184288Ssam	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
983178354Ssam		vap->iv_set_tim(ni, 0);
984178354Ssam	/*
985178354Ssam	 * Age frames on the wds pending queue.
986178354Ssam	 */
987178354Ssam	if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
988178354Ssam		ieee80211_node_wdsq_age(ni);
989178354Ssam	/*
990178354Ssam	 * Age out HT resources (e.g. frames on the
991178354Ssam	 * A-MPDU reorder queues).
992178354Ssam	 */
993178354Ssam	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
994178354Ssam		ieee80211_ht_node_age(ni);
995178354Ssam}
996178354Ssam
997170530Ssamstatic int8_t
998138568Ssamnode_getrssi(const struct ieee80211_node *ni)
999120104Ssam{
1000178354Ssam	uint32_t avgrssi = ni->ni_avgrssi;
1001178354Ssam	int32_t rssi;
1002178354Ssam
1003178354Ssam	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1004178354Ssam		return 0;
1005178354Ssam	rssi = IEEE80211_RSSI_GET(avgrssi);
1006178354Ssam	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1007120104Ssam}
1008120104Ssam
1009116742Ssamstatic void
1010170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1011170530Ssam{
1012178354Ssam	*rssi = node_getrssi(ni);
1013170530Ssam	*noise = ni->ni_noise;
1014170530Ssam}
1015170530Ssam
1016170530Ssamstatic void
1017178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni,
1018178354Ssam	struct ieee80211_mimo_info *info)
1019116742Ssam{
1020178354Ssam	/* XXX zero data? */
1021178354Ssam}
1022178354Ssam
1023178354Ssamstruct ieee80211_node *
1024178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt,
1025178354Ssam	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1026178354Ssam{
1027138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1028178354Ssam	struct ieee80211_node *ni;
1029116742Ssam	int hash;
1030116742Ssam
1031179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1032178354Ssam	if (ni == NULL) {
1033178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1034178354Ssam		return NULL;
1035178354Ssam	}
1036178354Ssam
1037178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1038140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1039138568Ssam		ether_sprintf(macaddr), nt->nt_name);
1040138568Ssam
1041116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1042116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1043138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
1044138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
1045138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
1046138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
1047183251Ssam	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1048178354Ssam	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1049178354Ssam	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1050139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
1051139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1052170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
1053184288Ssam	ieee80211_psq_init(&ni->ni_psq, "unknown");
1054178354Ssam	IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1055138568Ssam
1056138568Ssam	IEEE80211_NODE_LOCK(nt);
1057138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1058138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1059138568Ssam	ni->ni_table = nt;
1060178354Ssam	ni->ni_vap = vap;
1061138568Ssam	ni->ni_ic = ic;
1062138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1063116742Ssam
1064184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1065184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1066184277Ssam
1067116742Ssam	return ni;
1068116742Ssam}
1069116742Ssam
1070148777Ssam/*
1071148777Ssam * Craft a temporary node suitable for sending a management frame
1072148777Ssam * to the specified station.  We craft only as much state as we
1073148777Ssam * need to do the work since the node will be immediately reclaimed
1074148777Ssam * once the send completes.
1075148777Ssam */
1076116742Ssamstruct ieee80211_node *
1077178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1078178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1079148777Ssam{
1080178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1081148777Ssam	struct ieee80211_node *ni;
1082148777Ssam
1083179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1084148777Ssam	if (ni != NULL) {
1085183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1086183259Ssam
1087178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1088148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1089148777Ssam
1090178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1091178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1092178354Ssam		ni->ni_vap = vap;
1093178354Ssam
1094148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1095183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1096148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1097148777Ssam		/* NB: required by ieee80211_fix_rate */
1098183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1099178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1100148777Ssam			IEEE80211_KEYIX_NONE);
1101183259Ssam		ni->ni_txpower = bss->ni_txpower;
1102148777Ssam		/* XXX optimize away */
1103184288Ssam		ieee80211_psq_init(&ni->ni_psq, "unknown");
1104178354Ssam		IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1105148777Ssam	} else {
1106148777Ssam		/* XXX msg */
1107178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1108148777Ssam	}
1109148777Ssam	return ni;
1110148777Ssam}
1111148777Ssam
1112148777Ssamstruct ieee80211_node *
1113178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1114178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1115116742Ssam{
1116178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1117138568Ssam	struct ieee80211_node *ni;
1118138568Ssam
1119178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1120116742Ssam	if (ni != NULL) {
1121183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1122127770Ssam		/*
1123178354Ssam		 * Inherit from iv_bss.
1124127770Ssam		 */
1125183259Ssam		copy_bss(ni, bss);
1126183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1127183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1128178354Ssam	}
1129116742Ssam	return ni;
1130116742Ssam}
1131116742Ssam
1132178354Ssam/*
1133178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1134178354Ssam * not associate so we just create create a new node and
1135178354Ssam * simulate an association.  The caller is responsible for
1136178354Ssam * installing the node as the bss node and handling any further
1137178354Ssam * setup work like authorizing the port.
1138178354Ssam */
1139178354Ssamstruct ieee80211_node *
1140178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1141178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1142178354Ssam{
1143178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1144178354Ssam	struct ieee80211_node *ni;
1145178354Ssam
1146178354Ssam	/* XXX check if node already in sta table? */
1147178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1148178354Ssam	if (ni != NULL) {
1149178354Ssam		ni->ni_wdsvap = vap;
1150178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1151178354Ssam		/*
1152178354Ssam		 * Inherit any manually configured settings.
1153178354Ssam		 */
1154183259Ssam		copy_bss(ni, vap->iv_bss);
1155178354Ssam		ieee80211_node_set_chan(ni, chan);
1156178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1157178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1158178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1159178354Ssam		/* NB: no associd for peer */
1160178354Ssam		/*
1161178354Ssam		 * There are no management frames to use to
1162178354Ssam		 * discover neighbor capabilities, so blindly
1163178354Ssam		 * propagate the local configuration.
1164178354Ssam		 */
1165178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1166178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1167178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1168178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1169178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1170178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1171178354Ssam			/*
1172178354Ssam			 * Device is HT-capable and HT is enabled for
1173178354Ssam			 * the vap; setup HT operation.  On return
1174178354Ssam			 * ni_chan will be adjusted to an HT channel.
1175178354Ssam			 */
1176178354Ssam			ieee80211_ht_wds_init(ni);
1177178354Ssam		} else {
1178178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1179178354Ssam			/*
1180178354Ssam			 * Force a legacy channel to be used.
1181178354Ssam			 */
1182178354Ssam			c = ieee80211_find_channel(ic,
1183178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1184178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1185178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1186178354Ssam			ni->ni_chan = c;
1187178354Ssam		}
1188178354Ssam	}
1189178354Ssam	return ni;
1190178354Ssam}
1191178354Ssam
1192178354Ssamstruct ieee80211_node *
1193138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1194178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1195178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1196138568Ssam#else
1197178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1198178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1199138568Ssam#endif
1200116742Ssam{
1201116742Ssam	struct ieee80211_node *ni;
1202116742Ssam	int hash;
1203116742Ssam
1204138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1205127772Ssam
1206116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1207138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1208116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1209138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1210138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1211178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1212140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1213140766Ssam			    func, line,
1214140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1215140766Ssam			    ieee80211_node_refcnt(ni));
1216138568Ssam#endif
1217127772Ssam			return ni;
1218116742Ssam		}
1219116742Ssam	}
1220127772Ssam	return NULL;
1221127772Ssam}
1222178354Ssam
1223178354Ssamstruct ieee80211_node *
1224138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1225178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1226178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1227178354Ssam#else
1228178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1229178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1230138568Ssam#endif
1231178354Ssam{
1232178354Ssam	struct ieee80211_node *ni;
1233127772Ssam
1234178354Ssam	IEEE80211_NODE_LOCK(nt);
1235178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1236178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1237178354Ssam	return ni;
1238178354Ssam}
1239178354Ssam
1240127772Ssamstruct ieee80211_node *
1241138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1242178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1243178354Ssam	const struct ieee80211vap *vap,
1244178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1245138568Ssam#else
1246178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1247178354Ssam	const struct ieee80211vap *vap,
1248178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1249138568Ssam#endif
1250127772Ssam{
1251127772Ssam	struct ieee80211_node *ni;
1252178354Ssam	int hash;
1253127772Ssam
1254178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1255178354Ssam
1256178354Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1257178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1258178354Ssam		if (ni->ni_vap == vap &&
1259178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1260178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1261178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1262178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1263178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1264178354Ssam			    func, line,
1265178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1266178354Ssam			    ieee80211_node_refcnt(ni));
1267178354Ssam#endif
1268178354Ssam			return ni;
1269178354Ssam		}
1270178354Ssam	}
1271178354Ssam	return NULL;
1272178354Ssam}
1273178354Ssam
1274178354Ssamstruct ieee80211_node *
1275178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1276178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1277178354Ssam	const struct ieee80211vap *vap,
1278178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1279178354Ssam#else
1280178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1281178354Ssam	const struct ieee80211vap *vap,
1282178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1283178354Ssam#endif
1284178354Ssam{
1285178354Ssam	struct ieee80211_node *ni;
1286178354Ssam
1287138568Ssam	IEEE80211_NODE_LOCK(nt);
1288178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1289138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1290116742Ssam	return ni;
1291116742Ssam}
1292116742Ssam
1293116742Ssam/*
1294138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1295138568Ssam * Note that for the driver's benefit we we treat this like
1296138568Ssam * an association so the driver has an opportunity to setup
1297138568Ssam * it's private state.
1298138568Ssam */
1299138568Ssamstruct ieee80211_node *
1300178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1301170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1302138568Ssam{
1303138568Ssam	struct ieee80211_node *ni;
1304138568Ssam
1305178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1306153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1307178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1308138568Ssam	if (ni != NULL) {
1309178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1310178354Ssam
1311138568Ssam		/* XXX no rate negotiation; just dup */
1312178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1313178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1314153404Ssam			/*
1315170530Ssam			 * In adhoc demo mode there are no management
1316170530Ssam			 * frames to use to discover neighbor capabilities,
1317170530Ssam			 * so blindly propagate the local configuration
1318170530Ssam			 * so we can do interesting things (e.g. use
1319170530Ssam			 * WME to disable ACK's).
1320153404Ssam			 */
1321178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1322153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1323178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1324170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1325153404Ssam		}
1326183251Ssam		node_setuptxparms(ni);
1327178354Ssam		if (ic->ic_newassoc != NULL)
1328178354Ssam			ic->ic_newassoc(ni, 1);
1329170530Ssam		/* XXX not right for 802.1x/WPA */
1330170530Ssam		ieee80211_node_authorize(ni);
1331138568Ssam	}
1332138568Ssam	return ni;
1333138568Ssam}
1334138568Ssam
1335148936Ssamvoid
1336153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1337153073Ssam	const struct ieee80211_frame *wh,
1338153073Ssam	const struct ieee80211_scanparams *sp)
1339153073Ssam{
1340153073Ssam	ni->ni_esslen = sp->ssid[1];
1341153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1342153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1343153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1344153073Ssam	ni->ni_intval = sp->bintval;
1345153073Ssam	ni->ni_capinfo = sp->capinfo;
1346153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1347153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1348153073Ssam	ni->ni_fhindex = sp->fhindex;
1349153073Ssam	ni->ni_erp = sp->erp;
1350153073Ssam	ni->ni_timoff = sp->timoff;
1351153073Ssam
1352178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1353178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1354186659Ssam		if (ni->ni_ies.wme_ie != NULL)
1355186659Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1356186659Ssam		else
1357186659Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1358178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1359178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1360178354Ssam	}
1361178354Ssam
1362153073Ssam	/* NB: must be after ni_chan is setup */
1363165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1364165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1365165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1366153073Ssam}
1367153073Ssam
1368148936Ssam/*
1369148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1370148936Ssam * or probe response frame.  Note that for the driver's
1371148936Ssam * benefit we we treat this like an association so the
1372148936Ssam * driver has an opportunity to setup it's private state.
1373148936Ssam */
1374148936Ssamstruct ieee80211_node *
1375178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1376148936Ssam	const struct ieee80211_frame *wh,
1377148936Ssam	const struct ieee80211_scanparams *sp)
1378148936Ssam{
1379148936Ssam	struct ieee80211_node *ni;
1380148936Ssam
1381178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1382153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1383178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1384148936Ssam	if (ni != NULL) {
1385178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1386178354Ssam
1387153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1388183251Ssam		node_setuptxparms(ni);
1389148936Ssam		if (ic->ic_newassoc != NULL)
1390148936Ssam			ic->ic_newassoc(ni, 1);
1391148936Ssam		/* XXX not right for 802.1x/WPA */
1392148936Ssam		ieee80211_node_authorize(ni);
1393148936Ssam	}
1394148936Ssam	return ni;
1395148936Ssam}
1396148936Ssam
1397148863Ssam#define	IS_CTL(wh) \
1398148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1399148863Ssam#define	IS_PSPOLL(wh) \
1400148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1401170530Ssam#define	IS_BAR(wh) \
1402170530Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1403179220Ssam#define	IS_PROBEREQ(wh) \
1404179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1405179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1406179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1407179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1408179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1409170530Ssam
1410179220Ssamstatic __inline struct ieee80211_node *
1411179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1412179220Ssam    const struct ieee80211_frame_min *wh)
1413179220Ssam{
1414179220Ssam	/* XXX 4-address frames? */
1415179220Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1416179220Ssam		return ieee80211_find_node_locked(nt, wh->i_addr1);
1417179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1418179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1419179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1420179220Ssam}
1421179220Ssam
1422138568Ssam/*
1423138568Ssam * Locate the node for sender, track state, and then pass the
1424179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1425179220Ssam * we can return NULL if the sender is not in the table.
1426138568Ssam */
1427138568Ssamstruct ieee80211_node *
1428138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1429138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1430138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1431138568Ssam#else
1432138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1433138568Ssam	const struct ieee80211_frame_min *wh)
1434138568Ssam#endif
1435138568Ssam{
1436138568Ssam	struct ieee80211_node_table *nt;
1437138568Ssam	struct ieee80211_node *ni;
1438138568Ssam
1439170530Ssam	nt = &ic->ic_sta;
1440138568Ssam	IEEE80211_NODE_LOCK(nt);
1441179220Ssam	ni = _find_rxnode(nt, wh);
1442138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1443138568Ssam
1444148863Ssam	return ni;
1445148863Ssam}
1446148863Ssam
1447148863Ssam/*
1448148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1449148863Ssam * key index as a hint to locate the node in the key
1450148863Ssam * mapping table.  If an entry is present at the key
1451148863Ssam * index we return it; otherwise do a normal lookup and
1452148863Ssam * update the mapping table if the station has a unicast
1453148863Ssam * key assigned to it.
1454148863Ssam */
1455148863Ssamstruct ieee80211_node *
1456148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1457148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1458148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1459148863Ssam	const char *func, int line)
1460148863Ssam#else
1461148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1462148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1463148863Ssam#endif
1464148863Ssam{
1465148863Ssam	struct ieee80211_node_table *nt;
1466148863Ssam	struct ieee80211_node *ni;
1467148863Ssam
1468170530Ssam	nt = &ic->ic_sta;
1469148863Ssam	IEEE80211_NODE_LOCK(nt);
1470148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1471148863Ssam		ni = nt->nt_keyixmap[keyix];
1472148863Ssam	else
1473148863Ssam		ni = NULL;
1474148863Ssam	if (ni == NULL) {
1475179220Ssam		ni = _find_rxnode(nt, wh);
1476178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1477148863Ssam			/*
1478148863Ssam			 * If the station has a unicast key cache slot
1479148863Ssam			 * assigned update the key->node mapping table.
1480148863Ssam			 */
1481148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1482148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1483148863Ssam			if (keyix < nt->nt_keyixmax &&
1484148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1485178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1486178354Ssam				    IEEE80211_MSG_NODE,
1487148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1488148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1489148863Ssam				    ieee80211_node_refcnt(ni)+1);
1490148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1491148863Ssam			}
1492148863Ssam		}
1493179220Ssam	} else {
1494179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1495179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1496179220Ssam		else
1497179220Ssam			ieee80211_ref_node(ni);
1498179220Ssam	}
1499148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1500148863Ssam
1501148863Ssam	return ni;
1502148863Ssam}
1503179220Ssam#undef IS_BCAST_PROBEREQ
1504179220Ssam#undef IS_PROBEREQ
1505170530Ssam#undef IS_BAR
1506138568Ssam#undef IS_PSPOLL
1507138568Ssam#undef IS_CTL
1508138568Ssam
1509138568Ssam/*
1510127772Ssam * Return a reference to the appropriate node for sending
1511127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1512127772Ssam */
1513127772Ssamstruct ieee80211_node *
1514138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1515178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1516178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1517138568Ssam	const char *func, int line)
1518138568Ssam#else
1519178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1520178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1521138568Ssam#endif
1522127772Ssam{
1523178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1524127772Ssam	struct ieee80211_node *ni;
1525127772Ssam
1526127772Ssam	/*
1527127772Ssam	 * The destination address should be in the node table
1528148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1529148863Ssam	 * also optimize station mode operation, all frames go
1530148863Ssam	 * to the bss node.
1531127772Ssam	 */
1532127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1533138568Ssam	IEEE80211_NODE_LOCK(nt);
1534178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1535178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1536178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1537178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1538186099Ssam	else
1539178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1540138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1541138568Ssam
1542138568Ssam	if (ni == NULL) {
1543178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1544178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1545140497Ssam			/*
1546140497Ssam			 * In adhoc mode cons up a node for the destination.
1547140497Ssam			 * Note that we need an additional reference for the
1548178354Ssam			 * caller to be consistent with
1549178354Ssam			 * ieee80211_find_node_locked.
1550140497Ssam			 */
1551178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1552140497Ssam			if (ni != NULL)
1553140497Ssam				(void) ieee80211_ref_node(ni);
1554140497Ssam		} else {
1555178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1556178354Ssam			    "no node, discard frame (%s)", __func__);
1557178354Ssam			vap->iv_stats.is_tx_nonode++;
1558127772Ssam		}
1559127772Ssam	}
1560127772Ssam	return ni;
1561127772Ssam}
1562127772Ssam
1563116742Ssamstatic void
1564138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1565116742Ssam{
1566138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1567119150Ssam
1568179641Ssam	/*
1569179641Ssam	 * NB: careful about referencing the vap as it may be
1570179641Ssam	 * gone if the last reference was held by a driver.
1571179641Ssam	 * We know the com will always be present so it's safe
1572179641Ssam	 * to use ni_ic below to reclaim resources.
1573179641Ssam	 */
1574179641Ssam#if 0
1575178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1576140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1577140766Ssam		ether_sprintf(ni->ni_macaddr),
1578138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1579179641Ssam#endif
1580179641Ssam	if (ni->ni_associd != 0) {
1581179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1582179641Ssam		if (vap->iv_aid_bitmap != NULL)
1583179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1584179641Ssam	}
1585138568Ssam	if (nt != NULL) {
1586138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1587138568Ssam		LIST_REMOVE(ni, ni_hash);
1588138568Ssam	}
1589179641Ssam	ni->ni_ic->ic_node_free(ni);
1590116742Ssam}
1591116742Ssam
1592116742Ssamvoid
1593138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1594138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1595138568Ssam#else
1596138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1597138568Ssam#endif
1598116742Ssam{
1599138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1600119150Ssam
1601138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1602178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1603140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1604138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1605138568Ssam#endif
1606148863Ssam	if (nt != NULL) {
1607148863Ssam		IEEE80211_NODE_LOCK(nt);
1608148863Ssam		if (ieee80211_node_dectestref(ni)) {
1609148863Ssam			/*
1610148863Ssam			 * Last reference, reclaim state.
1611148863Ssam			 */
1612138568Ssam			_ieee80211_free_node(ni);
1613148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1614148863Ssam		    nt->nt_keyixmap != NULL) {
1615148863Ssam			ieee80211_keyix keyix;
1616148863Ssam			/*
1617148863Ssam			 * Check for a last reference in the key mapping table.
1618148863Ssam			 */
1619148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1620148863Ssam			if (keyix < nt->nt_keyixmax &&
1621148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1622178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1623178354Ssam				    IEEE80211_MSG_NODE,
1624148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1625148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1626148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1627148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1628148863Ssam				_ieee80211_free_node(ni);
1629148863Ssam			}
1630148863Ssam		}
1631148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1632148863Ssam	} else {
1633148863Ssam		if (ieee80211_node_dectestref(ni))
1634138568Ssam			_ieee80211_free_node(ni);
1635116742Ssam	}
1636116742Ssam}
1637116742Ssam
1638138568Ssam/*
1639148863Ssam * Reclaim a unicast key and clear any key cache state.
1640148863Ssam */
1641148863Ssamint
1642148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1643148863Ssam{
1644179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1645179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1646148863Ssam	struct ieee80211_node *nikey;
1647148863Ssam	ieee80211_keyix keyix;
1648148863Ssam	int isowned, status;
1649148863Ssam
1650148863Ssam	/*
1651148863Ssam	 * NB: We must beware of LOR here; deleting the key
1652148863Ssam	 * can cause the crypto layer to block traffic updates
1653148863Ssam	 * which can generate a LOR against the node table lock;
1654148863Ssam	 * grab it here and stash the key index for our use below.
1655148863Ssam	 *
1656148863Ssam	 * Must also beware of recursion on the node table lock.
1657148863Ssam	 * When called from node_cleanup we may already have
1658148863Ssam	 * the node table lock held.  Unfortunately there's no
1659148863Ssam	 * way to separate out this path so we must do this
1660148863Ssam	 * conditionally.
1661148863Ssam	 */
1662148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1663148863Ssam	if (!isowned)
1664148863Ssam		IEEE80211_NODE_LOCK(nt);
1665179641Ssam	nikey = NULL;
1666179641Ssam	status = 1;		/* NB: success */
1667186144Ssam	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1668179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1669179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1670179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1671179641Ssam			nikey = nt->nt_keyixmap[keyix];
1672179641Ssam			nt->nt_keyixmap[keyix] = NULL;;
1673179641Ssam		}
1674179641Ssam	}
1675148863Ssam	if (!isowned)
1676178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1677148863Ssam
1678148863Ssam	if (nikey != NULL) {
1679148863Ssam		KASSERT(nikey == ni,
1680148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1681179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1682148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1683148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1684148863Ssam			ieee80211_node_refcnt(ni)-1);
1685148863Ssam		ieee80211_free_node(ni);
1686148863Ssam	}
1687148863Ssam	return status;
1688148863Ssam}
1689148863Ssam
1690148863Ssam/*
1691138568Ssam * Reclaim a node.  If this is the last reference count then
1692138568Ssam * do the normal free work.  Otherwise remove it from the node
1693138568Ssam * table and mark it gone by clearing the back-reference.
1694138568Ssam */
1695138568Ssamstatic void
1696138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1697116742Ssam{
1698148863Ssam	ieee80211_keyix keyix;
1699138568Ssam
1700148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1701148863Ssam
1702178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1703140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1704140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1705140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1706148863Ssam	/*
1707148863Ssam	 * Clear any entry in the unicast key mapping table.
1708148863Ssam	 * We need to do it here so rx lookups don't find it
1709148863Ssam	 * in the mapping table even if it's not in the hash
1710148863Ssam	 * table.  We cannot depend on the mapping table entry
1711148863Ssam	 * being cleared because the node may not be free'd.
1712148863Ssam	 */
1713148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1714148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1715148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1716178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1717148863Ssam			"%s: %p<%s> clear key map entry\n",
1718148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr));
1719148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1720148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1721148863Ssam	}
1722138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1723138568Ssam		/*
1724138568Ssam		 * Other references are present, just remove the
1725138568Ssam		 * node from the table so it cannot be found.  When
1726138568Ssam		 * the references are dropped storage will be
1727140753Ssam		 * reclaimed.
1728138568Ssam		 */
1729138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1730138568Ssam		LIST_REMOVE(ni, ni_hash);
1731138568Ssam		ni->ni_table = NULL;		/* clear reference */
1732138568Ssam	} else
1733138568Ssam		_ieee80211_free_node(ni);
1734138568Ssam}
1735138568Ssam
1736178354Ssam/*
1737178354Ssam * Reclaim a (bss) node.  Decrement the refcnt and reclaim
1738178354Ssam * the node if the only other reference to it is in the sta
1739178354Ssam * table.  This is effectively ieee80211_free_node followed
1740178354Ssam * by node_reclaim when the refcnt is 1 (after the free).
1741178354Ssam */
1742138568Ssamstatic void
1743178354Ssamieee80211_node_reclaim(struct ieee80211_node *ni)
1744138568Ssam{
1745178354Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1746116742Ssam
1747178354Ssam	KASSERT(nt != NULL, ("reclaim node not in table"));
1748138568Ssam
1749178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1750178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1751178712Ssam		"%s %p<%s> refcnt %d\n", __func__, ni,
1752178354Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1753178354Ssam#endif
1754178354Ssam	IEEE80211_NODE_LOCK(nt);
1755178354Ssam	if (ieee80211_node_dectestref(ni)) {
1756178354Ssam		/*
1757178354Ssam		 * Last reference, reclaim state.
1758178354Ssam		 */
1759178354Ssam		_ieee80211_free_node(ni);
1760178354Ssam		nt = NULL;
1761178354Ssam	} else if (ieee80211_node_refcnt(ni) == 1 &&
1762178354Ssam	    nt->nt_keyixmap != NULL) {
1763178354Ssam		ieee80211_keyix keyix;
1764178354Ssam		/*
1765178354Ssam		 * Check for a last reference in the key mapping table.
1766178354Ssam		 */
1767178354Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1768178354Ssam		if (keyix < nt->nt_keyixmax &&
1769178354Ssam		    nt->nt_keyixmap[keyix] == ni) {
1770178354Ssam			IEEE80211_DPRINTF(ni->ni_vap,
1771178354Ssam			    IEEE80211_MSG_NODE,
1772178354Ssam			    "%s: %p<%s> clear key map entry", __func__,
1773178354Ssam			    ni, ether_sprintf(ni->ni_macaddr));
1774178354Ssam			nt->nt_keyixmap[keyix] = NULL;
1775178354Ssam			ieee80211_node_decref(ni); /* XXX needed? */
1776178354Ssam			_ieee80211_free_node(ni);
1777178354Ssam			nt = NULL;
1778178354Ssam		}
1779178354Ssam	}
1780178354Ssam	if (nt != NULL && ieee80211_node_refcnt(ni) == 1) {
1781178354Ssam		/*
1782178354Ssam		 * Last reference is in the sta table; complete
1783178354Ssam		 * the reclaim.  This handles bss nodes being
1784178354Ssam		 * recycled: the node has two references, one for
1785178354Ssam		 * iv_bss and one for the table.  After dropping
1786178354Ssam		 * the iv_bss ref above we need to reclaim the sta
1787178354Ssam		 * table reference.
1788178354Ssam		 */
1789178354Ssam		ieee80211_node_decref(ni);	/* NB: be pendantic */
1790178354Ssam		_ieee80211_free_node(ni);
1791178354Ssam	}
1792178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1793178354Ssam}
1794178354Ssam
1795178354Ssam/*
1796178354Ssam * Node table support.
1797178354Ssam */
1798178354Ssam
1799178354Ssamstatic void
1800178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1801178354Ssam	struct ieee80211_node_table *nt,
1802178354Ssam	const char *name, int inact, int keyixmax)
1803178354Ssam{
1804178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1805178354Ssam
1806178354Ssam	nt->nt_ic = ic;
1807178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1808178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1809178354Ssam	TAILQ_INIT(&nt->nt_node);
1810178354Ssam	nt->nt_name = name;
1811178354Ssam	nt->nt_scangen = 1;
1812178354Ssam	nt->nt_inact_init = inact;
1813178354Ssam	nt->nt_keyixmax = keyixmax;
1814178354Ssam	if (nt->nt_keyixmax > 0) {
1815186302Ssam		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1816184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1817178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1818178354Ssam		if (nt->nt_keyixmap == NULL)
1819178354Ssam			if_printf(ic->ic_ifp,
1820178354Ssam			    "Cannot allocate key index map with %u entries\n",
1821178354Ssam			    keyixmax);
1822178354Ssam	} else
1823178354Ssam		nt->nt_keyixmap = NULL;
1824178354Ssam}
1825178354Ssam
1826178354Ssamstatic void
1827178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1828178354Ssam	struct ieee80211vap *match)
1829178354Ssam{
1830178354Ssam	struct ieee80211_node *ni, *next;
1831178354Ssam
1832178354Ssam	IEEE80211_NODE_LOCK(nt);
1833178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1834178354Ssam		if (match != NULL && ni->ni_vap != match)
1835178354Ssam			continue;
1836178354Ssam		/* XXX can this happen?  if so need's work */
1837138568Ssam		if (ni->ni_associd != 0) {
1838178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1839178354Ssam
1840178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1841178354Ssam				vap->iv_auth->ia_node_leave(ni);
1842178354Ssam			if (vap->iv_aid_bitmap != NULL)
1843178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1844138568Ssam		}
1845178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1846138568Ssam		node_reclaim(nt, ni);
1847138568Ssam	}
1848178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1849178354Ssam		/*
1850178354Ssam		 * Make a separate pass to clear references to this vap
1851178354Ssam		 * held by DWDS entries.  They will not be matched above
1852178354Ssam		 * because ni_vap will point to the ap vap but we still
1853178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1854178354Ssam		 * and/or reset.
1855178354Ssam		 */
1856178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1857178354Ssam			if (ni->ni_wdsvap == match)
1858178354Ssam				ni->ni_wdsvap = NULL;
1859178354Ssam	}
1860178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1861116742Ssam}
1862116742Ssam
1863178354Ssamstatic void
1864178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1865178354Ssam{
1866178354Ssam	ieee80211_node_table_reset(nt, NULL);
1867178354Ssam	if (nt->nt_keyixmap != NULL) {
1868178354Ssam#ifdef DIAGNOSTIC
1869178354Ssam		/* XXX verify all entries are NULL */
1870178354Ssam		int i;
1871178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1872178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1873178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1874178354Ssam					nt->nt_name, i);
1875178354Ssam#endif
1876186302Ssam		free(nt->nt_keyixmap, M_80211_NODE);
1877178354Ssam		nt->nt_keyixmap = NULL;
1878178354Ssam	}
1879178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1880178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1881178354Ssam}
1882178354Ssam
1883120483Ssam/*
1884138568Ssam * Timeout inactive stations and do related housekeeping.
1885138568Ssam * Note that we cannot hold the node lock while sending a
1886138568Ssam * frame as this would lead to a LOR.  Instead we use a
1887138568Ssam * generation number to mark nodes that we've scanned and
1888138568Ssam * drop the lock and restart a scan if we have to time out
1889138568Ssam * a node.  Since we are single-threaded by virtue of
1890120483Ssam * controlling the inactivity timer we can be sure this will
1891120483Ssam * process each node only once.
1892120483Ssam */
1893138568Ssamstatic void
1894178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1895116742Ssam{
1896178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1897178354Ssam	struct ieee80211vap *vap;
1898120483Ssam	struct ieee80211_node *ni;
1899178354Ssam	int gen = 0;
1900116742Ssam
1901178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
1902154532Ssam	gen = ++nt->nt_scangen;
1903120483Ssamrestart:
1904138568Ssam	IEEE80211_NODE_LOCK(nt);
1905138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1906120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1907120483Ssam			continue;
1908120483Ssam		ni->ni_scangen = gen;
1909138568Ssam		/*
1910147788Ssam		 * Ignore entries for which have yet to receive an
1911147788Ssam		 * authentication frame.  These are transient and
1912147788Ssam		 * will be reclaimed when the last reference to them
1913147788Ssam		 * goes away (when frame xmits complete).
1914147788Ssam		 */
1915178354Ssam		vap = ni->ni_vap;
1916178354Ssam		/*
1917178354Ssam		 * Only process stations when in RUN state.  This
1918178354Ssam		 * insures, for example, that we don't timeout an
1919178354Ssam		 * inactive station during CAC.  Note that CSA state
1920178354Ssam		 * is actually handled in ieee80211_node_timeout as
1921178354Ssam		 * it applies to more than timeout processing.
1922178354Ssam		 */
1923178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
1924178354Ssam			continue;
1925178354Ssam		/* XXX can vap be NULL? */
1926178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1927178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
1928148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1929147788Ssam			continue;
1930147788Ssam		/*
1931138568Ssam		 * Free fragment if not needed anymore
1932138568Ssam		 * (last fragment older than 1s).
1933178354Ssam		 * XXX doesn't belong here, move to node_age
1934138568Ssam		 */
1935138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1936138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1937138568Ssam			m_freem(ni->ni_rxfrag[0]);
1938138568Ssam			ni->ni_rxfrag[0] = NULL;
1939138568Ssam		}
1940184277Ssam		if (ni->ni_inact > 0) {
1941172062Ssam			ni->ni_inact--;
1942184277Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1943184277Ssam			    "%s: inact %u inact_reload %u nrates %u",
1944184277Ssam			    __func__, ni->ni_inact, ni->ni_inact_reload,
1945184277Ssam			    ni->ni_rates.rs_nrates);
1946184277Ssam		}
1947140498Ssam		/*
1948140498Ssam		 * Special case ourself; we may be idle for extended periods
1949140498Ssam		 * of time and regardless reclaiming our state is wrong.
1950178354Ssam		 * XXX run ic_node_age
1951140498Ssam		 */
1952178354Ssam		if (ni == vap->iv_bss)
1953140498Ssam			continue;
1954178354Ssam		if (ni->ni_associd != 0 ||
1955178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1956178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1957119150Ssam			/*
1958178354Ssam			 * Age/drain resources held by the station.
1959138568Ssam			 */
1960178354Ssam			ic->ic_node_age(ni);
1961138568Ssam			/*
1962138568Ssam			 * Probe the station before time it out.  We
1963138568Ssam			 * send a null data frame which may not be
1964138568Ssam			 * universally supported by drivers (need it
1965138568Ssam			 * for ps-poll support so it should be...).
1966170530Ssam			 *
1967170530Ssam			 * XXX don't probe the station unless we've
1968170530Ssam			 *     received a frame from them (and have
1969170530Ssam			 *     some idea of the rates they are capable
1970170530Ssam			 *     of); this will get fixed more properly
1971170530Ssam			 *     soon with better handling of the rate set.
1972138568Ssam			 */
1973178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1974172062Ssam			    (0 < ni->ni_inact &&
1975178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
1976170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1977178354Ssam				IEEE80211_NOTE(vap,
1978148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1979148320Ssam				    ni, "%s",
1980148320Ssam				    "probe station due to inactivity");
1981148582Ssam				/*
1982148582Ssam				 * Grab a reference before unlocking the table
1983148582Ssam				 * so the node cannot be reclaimed before we
1984148582Ssam				 * send the frame. ieee80211_send_nulldata
1985148582Ssam				 * understands we've done this and reclaims the
1986148582Ssam				 * ref for us as needed.
1987148582Ssam				 */
1988148582Ssam				ieee80211_ref_node(ni);
1989138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1990148301Ssam				ieee80211_send_nulldata(ni);
1991138568Ssam				/* XXX stat? */
1992138568Ssam				goto restart;
1993138568Ssam			}
1994138568Ssam		}
1995178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1996172062Ssam		    ni->ni_inact <= 0) {
1997178354Ssam			IEEE80211_NOTE(vap,
1998148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1999148320Ssam			    "station timed out due to inactivity "
2000148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
2001138568Ssam			/*
2002138568Ssam			 * Send a deauthenticate frame and drop the station.
2003138568Ssam			 * This is somewhat complicated due to reference counts
2004138568Ssam			 * and locking.  At this point a station will typically
2005138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
2006138568Ssam			 * will do a "free" of the node which will drop the
2007138568Ssam			 * reference count.  But in the meantime a reference
2008138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
2009138568Ssam			 * of the node will happen either after the tx is
2010138568Ssam			 * completed or by ieee80211_node_leave.
2011120483Ssam			 *
2012138568Ssam			 * Separately we must drop the node lock before sending
2013170530Ssam			 * in case the driver takes a lock, as this can result
2014170530Ssam			 * in a LOR between the node lock and the driver lock.
2015119150Ssam			 */
2016172229Ssam			ieee80211_ref_node(ni);
2017138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2018138568Ssam			if (ni->ni_associd != 0) {
2019178354Ssam				IEEE80211_SEND_MGMT(ni,
2020138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
2021138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
2022138568Ssam			}
2023178354Ssam			ieee80211_node_leave(ni);
2024172229Ssam			ieee80211_free_node(ni);
2025178354Ssam			vap->iv_stats.is_node_timeout++;
2026120483Ssam			goto restart;
2027120483Ssam		}
2028116742Ssam	}
2029138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2030138568Ssam
2031178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2032170530Ssam}
2033138568Ssam
2034178354Ssam/*
2035178354Ssam * Aggressively reclaim resources.  This should be used
2036178354Ssam * only in a critical situation to reclaim mbuf resources.
2037178354Ssam */
2038170530Ssamvoid
2039178354Ssamieee80211_drain(struct ieee80211com *ic)
2040178354Ssam{
2041178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2042178354Ssam	struct ieee80211vap *vap;
2043178354Ssam	struct ieee80211_node *ni;
2044178354Ssam
2045178354Ssam	IEEE80211_NODE_LOCK(nt);
2046178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2047178354Ssam		/*
2048178354Ssam		 * Ignore entries for which have yet to receive an
2049178354Ssam		 * authentication frame.  These are transient and
2050178354Ssam		 * will be reclaimed when the last reference to them
2051178354Ssam		 * goes away (when frame xmits complete).
2052178354Ssam		 */
2053178354Ssam		vap = ni->ni_vap;
2054178354Ssam		/*
2055178354Ssam		 * Only process stations when in RUN state.  This
2056178354Ssam		 * insures, for example, that we don't timeout an
2057178354Ssam		 * inactive station during CAC.  Note that CSA state
2058178354Ssam		 * is actually handled in ieee80211_node_timeout as
2059178354Ssam		 * it applies to more than timeout processing.
2060178354Ssam		 */
2061178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2062178354Ssam			continue;
2063178354Ssam		/* XXX can vap be NULL? */
2064178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2065178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2066178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2067178354Ssam			continue;
2068178354Ssam		/*
2069178354Ssam		 * Free fragments.
2070178354Ssam		 * XXX doesn't belong here, move to node_drain
2071178354Ssam		 */
2072178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2073178354Ssam			m_freem(ni->ni_rxfrag[0]);
2074178354Ssam			ni->ni_rxfrag[0] = NULL;
2075178354Ssam		}
2076178354Ssam		/*
2077178354Ssam		 * Drain resources held by the station.
2078178354Ssam		 */
2079178354Ssam		ic->ic_node_drain(ni);
2080178354Ssam	}
2081178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2082178354Ssam}
2083178354Ssam
2084178354Ssam/*
2085178354Ssam * Per-ieee80211com inactivity timer callback.
2086178354Ssam */
2087178354Ssamvoid
2088170530Ssamieee80211_node_timeout(void *arg)
2089170530Ssam{
2090170530Ssam	struct ieee80211com *ic = arg;
2091170530Ssam
2092178354Ssam	/*
2093178354Ssam	 * Defer timeout processing if a channel switch is pending.
2094178354Ssam	 * We typically need to be mute so not doing things that
2095178354Ssam	 * might generate frames is good to handle in one place.
2096178354Ssam	 * Supressing the station timeout processing may extend the
2097178354Ssam	 * lifetime of inactive stations (by not decrementing their
2098178354Ssam	 * idle counters) but this should be ok unless the CSA is
2099178354Ssam	 * active for an unusually long time.
2100178354Ssam	 */
2101178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2102178354Ssam		ieee80211_scan_timeout(ic);
2103178354Ssam		ieee80211_timeout_stations(ic);
2104170530Ssam
2105178354Ssam		IEEE80211_LOCK(ic);
2106178354Ssam		ieee80211_erp_timeout(ic);
2107178354Ssam		ieee80211_ht_timeout(ic);
2108178354Ssam		IEEE80211_UNLOCK(ic);
2109178354Ssam	}
2110170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2111170530Ssam		ieee80211_node_timeout, ic);
2112116742Ssam}
2113116742Ssam
2114116742Ssamvoid
2115178354Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2116178354Ssam	ieee80211_iter_func *f, void *arg)
2117116742Ssam{
2118116742Ssam	struct ieee80211_node *ni;
2119138568Ssam	u_int gen;
2120116742Ssam
2121178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2122154532Ssam	gen = ++nt->nt_scangen;
2123138568Ssamrestart:
2124138568Ssam	IEEE80211_NODE_LOCK(nt);
2125138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2126138568Ssam		if (ni->ni_scangen != gen) {
2127138568Ssam			ni->ni_scangen = gen;
2128138568Ssam			(void) ieee80211_ref_node(ni);
2129138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2130138568Ssam			(*f)(arg, ni);
2131138568Ssam			ieee80211_free_node(ni);
2132138568Ssam			goto restart;
2133138568Ssam		}
2134138568Ssam	}
2135138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2136138568Ssam
2137178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2138116742Ssam}
2139138568Ssam
2140138568Ssamvoid
2141138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2142138568Ssam{
2143138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2144138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2145138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2146138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2147138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2148138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2149138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2150167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2151167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2152167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2153138568Ssam		ni->ni_rxfragstamp);
2154170530Ssam	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2155178354Ssam		ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
2156170530Ssam		ni->ni_intval, ni->ni_capinfo);
2157138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2158138568Ssam		ether_sprintf(ni->ni_bssid),
2159138568Ssam		ni->ni_esslen, ni->ni_essid,
2160138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2161184277Ssam	printf("\tinact %u inact_reload %u txrate %u\n",
2162184277Ssam		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2163170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2164170530Ssam		ni->ni_htcap, ni->ni_htparam,
2165170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2166170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2167170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2168138568Ssam}
2169138568Ssam
2170138568Ssamvoid
2171138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2172138568Ssam{
2173138568Ssam	ieee80211_iterate_nodes(nt,
2174138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2175138568Ssam}
2176138568Ssam
2177179642Ssamstatic void
2178179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2179172211Ssam{
2180178354Ssam	struct ieee80211vap *vap;
2181178354Ssam
2182178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2183178354Ssam
2184178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2185178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2186178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2187172211Ssam}
2188172211Ssam
2189179642Ssamvoid
2190179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2191179642Ssam{
2192179642Ssam	IEEE80211_LOCK(ic);
2193179642Ssam	ieee80211_notify_erp_locked(ic);
2194179642Ssam	IEEE80211_UNLOCK(ic);
2195179642Ssam}
2196179642Ssam
2197138568Ssam/*
2198138568Ssam * Handle a station joining an 11g network.
2199138568Ssam */
2200138568Ssamstatic void
2201178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2202138568Ssam{
2203178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2204138568Ssam
2205173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2206173273Ssam
2207138568Ssam	/*
2208138568Ssam	 * Station isn't capable of short slot time.  Bump
2209138568Ssam	 * the count of long slot time stations and disable
2210138568Ssam	 * use of short slot time.  Note that the actual switch
2211138568Ssam	 * over to long slot time use may not occur until the
2212138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2213138568Ssam	 */
2214138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2215138568Ssam		ic->ic_longslotsta++;
2216178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2217172211Ssam		    "station needs long slot time, count %d",
2218172211Ssam		    ic->ic_longslotsta);
2219138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2220170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2221170530Ssam			/*
2222170530Ssam			 * Don't force slot time when switched to turbo
2223170530Ssam			 * mode as non-ERP stations won't be present; this
2224170530Ssam			 * need only be done when on the normal G channel.
2225170530Ssam			 */
2226170530Ssam			ieee80211_set_shortslottime(ic, 0);
2227170530Ssam		}
2228138568Ssam	}
2229138568Ssam	/*
2230138568Ssam	 * If the new station is not an ERP station
2231138568Ssam	 * then bump the counter and enable protection
2232138568Ssam	 * if configured.
2233138568Ssam	 */
2234178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2235138568Ssam		ic->ic_nonerpsta++;
2236178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2237172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2238172211Ssam		    ic->ic_nonerpsta);
2239138568Ssam		/*
2240138568Ssam		 * If station does not support short preamble
2241138568Ssam		 * then we must enable use of Barker preamble.
2242138568Ssam		 */
2243138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2244178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2245172211Ssam			    "%s", "station needs long preamble");
2246138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2247138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2248138568Ssam		}
2249172211Ssam		/*
2250178354Ssam		 * If protection is configured and this is the first
2251178354Ssam		 * indication we should use protection, enable it.
2252172211Ssam		 */
2253172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2254172211Ssam		    ic->ic_nonerpsta == 1 &&
2255172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2256178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2257172211Ssam			    "%s: enable use of protection\n", __func__);
2258172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2259179642Ssam			ieee80211_notify_erp_locked(ic);
2260172211Ssam		}
2261138568Ssam	} else
2262138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2263138568Ssam}
2264138568Ssam
2265138568Ssamvoid
2266178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2267138568Ssam{
2268178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2269178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2270138568Ssam	int newassoc;
2271138568Ssam
2272138568Ssam	if (ni->ni_associd == 0) {
2273170530Ssam		uint16_t aid;
2274138568Ssam
2275178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2276138568Ssam		/*
2277138568Ssam		 * It would be good to search the bitmap
2278138568Ssam		 * more efficiently, but this will do for now.
2279138568Ssam		 */
2280178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2281178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2282138568Ssam				break;
2283138568Ssam		}
2284178354Ssam		if (aid >= vap->iv_max_aid) {
2285179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2286178354Ssam			ieee80211_node_leave(ni);
2287138568Ssam			return;
2288138568Ssam		}
2289138568Ssam		ni->ni_associd = aid | 0xc000;
2290173273Ssam		ni->ni_jointime = time_uptime;
2291178354Ssam		IEEE80211_LOCK(ic);
2292178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2293178354Ssam		vap->iv_sta_assoc++;
2294138568Ssam		ic->ic_sta_assoc++;
2295173273Ssam
2296173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2297173273Ssam			ieee80211_ht_node_join(ni);
2298170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2299170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2300178354Ssam			ieee80211_node_join_11g(ni);
2301173273Ssam		IEEE80211_UNLOCK(ic);
2302173273Ssam
2303173273Ssam		newassoc = 1;
2304138568Ssam	} else
2305138568Ssam		newassoc = 0;
2306138568Ssam
2307178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2308183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2309139523Ssam	    IEEE80211_NODE_AID(ni),
2310139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2311139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2312139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2313170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2314170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2315182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2316173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2317183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2318183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2319183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2320178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2321170530Ssam		", fast-frames" : "",
2322178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2323170530Ssam		", turbo" : ""
2324139523Ssam	);
2325138568Ssam
2326183251Ssam	node_setuptxparms(ni);
2327138568Ssam	/* give driver a chance to setup state like ni_txrate */
2328139524Ssam	if (ic->ic_newassoc != NULL)
2329148307Ssam		ic->ic_newassoc(ni, newassoc);
2330178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2331138568Ssam	/* tell the authenticator about new station */
2332178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2333178354Ssam		vap->iv_auth->ia_node_join(ni);
2334178354Ssam	ieee80211_notify_node_join(ni,
2335173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2336138568Ssam}
2337138568Ssam
2338172211Ssamstatic void
2339172211Ssamdisable_protection(struct ieee80211com *ic)
2340172211Ssam{
2341172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2342172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2343172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2344172211Ssam	   ic->ic_flags_ext));
2345172211Ssam
2346172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2347172211Ssam	/* XXX verify mode? */
2348172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2349172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2350172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2351172211Ssam	}
2352179642Ssam	ieee80211_notify_erp_locked(ic);
2353172211Ssam}
2354172211Ssam
2355138568Ssam/*
2356138568Ssam * Handle a station leaving an 11g network.
2357138568Ssam */
2358138568Ssamstatic void
2359178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2360138568Ssam{
2361178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2362138568Ssam
2363173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2364173273Ssam
2365170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2366178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2367178354Ssam	      ic->ic_bsschan->ic_flags));
2368138568Ssam
2369138568Ssam	/*
2370138568Ssam	 * If a long slot station do the slot time bookkeeping.
2371138568Ssam	 */
2372138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2373138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2374138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2375138568Ssam		ic->ic_longslotsta--;
2376178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2377172211Ssam		    "long slot time station leaves, count now %d",
2378172211Ssam		    ic->ic_longslotsta);
2379138568Ssam		if (ic->ic_longslotsta == 0) {
2380138568Ssam			/*
2381138568Ssam			 * Re-enable use of short slot time if supported
2382138568Ssam			 * and not operating in IBSS mode (per spec).
2383138568Ssam			 */
2384138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2385138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2386178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2387178354Ssam				    IEEE80211_MSG_ASSOC,
2388138568Ssam				    "%s: re-enable use of short slot time\n",
2389138568Ssam				    __func__);
2390138568Ssam				ieee80211_set_shortslottime(ic, 1);
2391138568Ssam			}
2392138568Ssam		}
2393138568Ssam	}
2394138568Ssam	/*
2395138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2396138568Ssam	 */
2397138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2398138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2399138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2400138568Ssam		ic->ic_nonerpsta--;
2401178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2402172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2403172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2404172211Ssam			" (non-ERP sta present)" : "");
2405172211Ssam		if (ic->ic_nonerpsta == 0 &&
2406172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2407178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2408138568Ssam				"%s: disable use of protection\n", __func__);
2409172211Ssam			disable_protection(ic);
2410138568Ssam		}
2411138568Ssam	}
2412138568Ssam}
2413138568Ssam
2414138568Ssam/*
2415172211Ssam * Time out presence of an overlapping bss with non-ERP
2416172211Ssam * stations.  When operating in hostap mode we listen for
2417172211Ssam * beacons from other stations and if we identify a non-ERP
2418172211Ssam * station is present we enable protection.  To identify
2419172211Ssam * when all non-ERP stations are gone we time out this
2420172211Ssam * condition.
2421172211Ssam */
2422172211Ssamstatic void
2423172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2424172211Ssam{
2425172211Ssam
2426172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2427172211Ssam
2428172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2429172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2430178354Ssam#if 0
2431178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2432178354Ssam		    "%s", "age out non-ERP sta present on channel");
2433178354Ssam#endif
2434172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2435172211Ssam		if (ic->ic_nonerpsta == 0)
2436172211Ssam			disable_protection(ic);
2437172211Ssam	}
2438172211Ssam}
2439172211Ssam
2440172211Ssam/*
2441138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2442138568Ssam * when operating as an ap.
2443138568Ssam */
2444138568Ssamvoid
2445178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2446138568Ssam{
2447178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2448178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2449140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2450138568Ssam
2451178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2452172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2453138568Ssam
2454178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2455178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2456138568Ssam	/*
2457138568Ssam	 * If node wasn't previously associated all
2458138568Ssam	 * we need to do is reclaim the reference.
2459138568Ssam	 */
2460138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2461138568Ssam	if (ni->ni_associd == 0)
2462138568Ssam		goto done;
2463138568Ssam	/*
2464138568Ssam	 * Tell the authenticator the station is leaving.
2465138568Ssam	 * Note that we must do this before yanking the
2466138568Ssam	 * association id as the authenticator uses the
2467138568Ssam	 * associd to locate it's state block.
2468138568Ssam	 */
2469178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2470178354Ssam		vap->iv_auth->ia_node_leave(ni);
2471173273Ssam
2472173273Ssam	IEEE80211_LOCK(ic);
2473178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2474138568Ssam	ni->ni_associd = 0;
2475178354Ssam	vap->iv_sta_assoc--;
2476138568Ssam	ic->ic_sta_assoc--;
2477138568Ssam
2478173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2479173273Ssam		ieee80211_ht_node_leave(ni);
2480170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2481170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2482178354Ssam		ieee80211_node_leave_11g(ni);
2483173273Ssam	IEEE80211_UNLOCK(ic);
2484138568Ssam	/*
2485138568Ssam	 * Cleanup station state.  In particular clear various
2486138568Ssam	 * state that might otherwise be reused if the node
2487138568Ssam	 * is reused before the reference count goes to zero
2488138568Ssam	 * (and memory is reclaimed).
2489138568Ssam	 */
2490178354Ssam	ieee80211_sta_leave(ni);
2491138568Ssamdone:
2492140499Ssam	/*
2493140499Ssam	 * Remove the node from any table it's recorded in and
2494140499Ssam	 * drop the caller's reference.  Removal from the table
2495140499Ssam	 * is important to insure the node is not reprocessed
2496140499Ssam	 * for inactivity.
2497140499Ssam	 */
2498140499Ssam	if (nt != NULL) {
2499140499Ssam		IEEE80211_NODE_LOCK(nt);
2500140499Ssam		node_reclaim(nt, ni);
2501140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2502140499Ssam	} else
2503140499Ssam		ieee80211_free_node(ni);
2504138568Ssam}
2505138568Ssam
2506178354Ssamstruct rssiinfo {
2507178354Ssam	struct ieee80211vap *vap;
2508178354Ssam	int	rssi_samples;
2509178354Ssam	uint32_t rssi_total;
2510178354Ssam};
2511178354Ssam
2512178354Ssamstatic void
2513178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni)
2514178354Ssam{
2515178354Ssam	struct rssiinfo *info = arg;
2516178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2517178354Ssam	int8_t rssi;
2518178354Ssam
2519178354Ssam	if (info->vap != vap)
2520178354Ssam		return;
2521178354Ssam	/* only associated stations */
2522178354Ssam	if (ni->ni_associd == 0)
2523178354Ssam		return;
2524178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2525178354Ssam	if (rssi != 0) {
2526178354Ssam		info->rssi_samples++;
2527178354Ssam		info->rssi_total += rssi;
2528178354Ssam	}
2529178354Ssam}
2530178354Ssam
2531178354Ssamstatic void
2532178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2533178354Ssam{
2534178354Ssam	struct rssiinfo *info = arg;
2535178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2536178354Ssam	int8_t rssi;
2537178354Ssam
2538178354Ssam	if (info->vap != vap)
2539178354Ssam		return;
2540178354Ssam	/* only neighbors */
2541178354Ssam	/* XXX check bssid */
2542178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2543178354Ssam		return;
2544178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2545178354Ssam	if (rssi != 0) {
2546178354Ssam		info->rssi_samples++;
2547178354Ssam		info->rssi_total += rssi;
2548178354Ssam	}
2549178354Ssam}
2550178354Ssam
2551170530Ssamint8_t
2552178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2553138568Ssam{
2554138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2555178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2556178354Ssam	struct rssiinfo info;
2557138568Ssam
2558178354Ssam	info.rssi_total = 0;
2559178354Ssam	info.rssi_samples = 0;
2560178354Ssam	info.vap = vap;
2561178354Ssam	switch (vap->iv_opmode) {
2562138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2563138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2564178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2565178354Ssam		break;
2566138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2567178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2568138568Ssam		break;
2569138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2570138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2571138568Ssam	default:
2572178354Ssam		if (vap->iv_bss != NULL)
2573178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2574178354Ssam		info.rssi_samples = 1;
2575138568Ssam		break;
2576138568Ssam	}
2577178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2578138568Ssam#undef NZ
2579138568Ssam}
2580138568Ssam
2581170530Ssamvoid
2582178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2583138568Ssam{
2584138568Ssam
2585178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2586170530Ssam		return;
2587178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2588170530Ssam	/* for non-station mode return avg'd rssi accounting */
2589178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2590178354Ssam		*rssi = ieee80211_getrssi(vap);
2591138568Ssam}
2592