ieee80211_node.c revision 184277
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 184277 2008-10-25 23:40:48Z sam $");
29116742Ssam
30178354Ssam#include "opt_wlan.h"
31178354Ssam
32116742Ssam#include <sys/param.h>
33116742Ssam#include <sys/systm.h>
34116742Ssam#include <sys/mbuf.h>
35116742Ssam#include <sys/malloc.h>
36116742Ssam#include <sys/kernel.h>
37138568Ssam
38116742Ssam#include <sys/socket.h>
39116742Ssam
40116742Ssam#include <net/if.h>
41116742Ssam#include <net/if_media.h>
42116742Ssam#include <net/ethernet.h>
43116742Ssam
44116742Ssam#include <net80211/ieee80211_var.h>
45178354Ssam#include <net80211/ieee80211_input.h>
46178354Ssam#include <net80211/ieee80211_wds.h>
47116742Ssam
48116742Ssam#include <net/bpf.h>
49116742Ssam
50147221Ssam/*
51147221Ssam * Association id's are managed with a bit vector.
52147221Ssam */
53178354Ssam#define	IEEE80211_AID_SET(_vap, b) \
54178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
55178354Ssam		(1 << (IEEE80211_AID(b) % 32)))
56178354Ssam#define	IEEE80211_AID_CLR(_vap, b) \
57178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
58178354Ssam		~(1 << (IEEE80211_AID(b) % 32)))
59178354Ssam#define	IEEE80211_AID_ISSET(_vap, b) \
60178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
61147221Ssam
62159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
63159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
64159139Sdds#else
65159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
66159139Sdds#endif
67159139Sdds
68170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
69170530Ssam
70179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *,
71179643Ssam	const uint8_t [IEEE80211_ADDR_LEN]);
72138568Ssamstatic void node_cleanup(struct ieee80211_node *);
73138568Ssamstatic void node_free(struct ieee80211_node *);
74178354Ssamstatic void node_age(struct ieee80211_node *);
75170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
76170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
77178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *,
78178354Ssam	struct ieee80211_mimo_info *);
79116742Ssam
80138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
81120104Ssam
82138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
83148863Ssam	struct ieee80211_node_table *nt, const char *name,
84170530Ssam	int inact, int keymaxix);
85178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *,
86178354Ssam	struct ieee80211vap *);
87178354Ssamstatic void ieee80211_node_reclaim(struct ieee80211_node *);
88138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
89172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
90138568Ssam
91127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
92178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
93120481Ssam
94116742Ssamvoid
95138568Ssamieee80211_node_attach(struct ieee80211com *ic)
96116742Ssam{
97178354Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
98178354Ssam		IEEE80211_INACT_INIT, ic->ic_max_keyix);
99178354Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
100178354Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
101178354Ssam		ieee80211_node_timeout, ic);
102116742Ssam
103138568Ssam	ic->ic_node_alloc = node_alloc;
104138568Ssam	ic->ic_node_free = node_free;
105138568Ssam	ic->ic_node_cleanup = node_cleanup;
106178354Ssam	ic->ic_node_age = node_age;
107178354Ssam	ic->ic_node_drain = node_age;		/* NB: same as age */
108138568Ssam	ic->ic_node_getrssi = node_getrssi;
109170530Ssam	ic->ic_node_getsignal = node_getsignal;
110178354Ssam	ic->ic_node_getmimoinfo = node_getmimoinfo;
111138568Ssam
112178354Ssam	/*
113178354Ssam	 * Set flags to be propagated to all vap's;
114178354Ssam	 * these define default behaviour/configuration.
115178354Ssam	 */
116178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
117178354Ssam}
118138568Ssam
119178354Ssamvoid
120178354Ssamieee80211_node_detach(struct ieee80211com *ic)
121178354Ssam{
122170530Ssam
123178354Ssam	callout_drain(&ic->ic_inact);
124178354Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
125178354Ssam}
126172062Ssam
127178354Ssamvoid
128178354Ssamieee80211_node_vattach(struct ieee80211vap *vap)
129178354Ssam{
130178354Ssam	/* NB: driver can override */
131178354Ssam	vap->iv_max_aid = IEEE80211_AID_DEF;
132178354Ssam
133178354Ssam	/* default station inactivity timer setings */
134178354Ssam	vap->iv_inact_init = IEEE80211_INACT_INIT;
135178354Ssam	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
136178354Ssam	vap->iv_inact_run = IEEE80211_INACT_RUN;
137178354Ssam	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
138184277Ssam
139184277Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
140184277Ssam	    "%s: init %u auth %u run %u probe %u\n", __func__,
141184277Ssam	    vap->iv_inact_init, vap->iv_inact_auth,
142184277Ssam	    vap->iv_inact_run, vap->iv_inact_probe);
143148863Ssam}
144148863Ssam
145148863Ssamvoid
146178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap)
147148863Ssam{
148178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
149178354Ssam		/* XXX should we allow max aid to be zero? */
150178354Ssam		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
151178354Ssam			vap->iv_max_aid = IEEE80211_AID_MIN;
152178354Ssam			if_printf(vap->iv_ifp,
153178354Ssam			    "WARNING: max aid too small, changed to %d\n",
154178354Ssam			    vap->iv_max_aid);
155178354Ssam		}
156184210Sdes		MALLOC(vap->iv_aid_bitmap, uint32_t *,
157184210Sdes			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
158178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
159178354Ssam		if (vap->iv_aid_bitmap == NULL) {
160178354Ssam			/* XXX no way to recover */
161178354Ssam			printf("%s: no memory for AID bitmap, max aid %d!\n",
162178354Ssam			    __func__, vap->iv_max_aid);
163178354Ssam			vap->iv_max_aid = 0;
164178354Ssam		}
165138568Ssam	}
166138568Ssam
167178354Ssam	ieee80211_reset_bss(vap);
168118887Ssam
169178354Ssam	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
170116742Ssam}
171116742Ssam
172116742Ssamvoid
173178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap)
174116742Ssam{
175178354Ssam	struct ieee80211com *ic = vap->iv_ic;
176116742Ssam
177178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
178178354Ssam	if (vap->iv_bss != NULL) {
179178354Ssam		ieee80211_free_node(vap->iv_bss);
180178354Ssam		vap->iv_bss = NULL;
181138568Ssam	}
182178354Ssam	if (vap->iv_aid_bitmap != NULL) {
183184210Sdes		FREE(vap->iv_aid_bitmap, M_80211_NODE);
184178354Ssam		vap->iv_aid_bitmap = NULL;
185138568Ssam	}
186116742Ssam}
187116742Ssam
188138568Ssam/*
189138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
190138568Ssam */
191138568Ssam
192138568Ssamvoid
193148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
194138568Ssam{
195184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
196184277Ssam
197138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
198184277Ssam	ni->ni_inact_reload = vap->iv_inact_run;
199172062Ssam	ni->ni_inact = ni->ni_inact_reload;
200184277Ssam
201184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
202184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
203138568Ssam}
204138568Ssam
205138568Ssamvoid
206148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
207138568Ssam{
208184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
209184277Ssam
210138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
211184277Ssam	ni->ni_inact_reload = vap->iv_inact_auth;
212172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
213172062Ssam		ni->ni_inact = ni->ni_inact_reload;
214184277Ssam
215184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
216184277Ssam	    "%s: inact_reload %u inact %u", __func__,
217184277Ssam	    ni->ni_inact_reload, ni->ni_inact);
218138568Ssam}
219138568Ssam
220116742Ssam/*
221183251Ssam * Fix tx parameters for a node according to ``association state''.
222183251Ssam */
223183251Ssamstatic void
224183251Ssamnode_setuptxparms(struct ieee80211_node *ni)
225183251Ssam{
226183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
227183251Ssam
228183251Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
229183251Ssam		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
230183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NA];
231183251Ssam		else
232183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NG];
233183251Ssam	} else {				/* legacy rate handling */
234183251Ssam		if (IEEE80211_IS_CHAN_A(ni->ni_chan))
235183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11A];
236183251Ssam		else if (ni->ni_flags & IEEE80211_NODE_ERP)
237183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11G];
238183251Ssam		else
239183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11B];
240183251Ssam	}
241183251Ssam}
242183251Ssam
243183251Ssam/*
244138568Ssam * Set/change the channel.  The rate set is also updated as
245138568Ssam * to insure a consistent view by drivers.
246178354Ssam * XXX should be private but hostap needs it to deal with CSA
247138568Ssam */
248178354Ssamvoid
249178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni,
250178354Ssam	struct ieee80211_channel *chan)
251138568Ssam{
252178354Ssam	struct ieee80211com *ic = ni->ni_ic;
253183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
254183251Ssam	enum ieee80211_phymode mode;
255170530Ssam
256178354Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
257178354Ssam
258138568Ssam	ni->ni_chan = chan;
259183251Ssam	mode = ieee80211_chan2mode(chan);
260170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
261170530Ssam		/*
262170530Ssam		 * XXX Gotta be careful here; the rate set returned by
263170530Ssam		 * ieee80211_get_suprates is actually any HT rate
264170530Ssam		 * set so blindly copying it will be bad.  We must
265170530Ssam		 * install the legacy rate est in ni_rates and the
266170530Ssam		 * HT rate set in ni_htrates.
267170530Ssam		 */
268170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
269183251Ssam		/*
270183251Ssam		 * Setup bss tx parameters based on operating mode.  We
271183251Ssam		 * use legacy rates when operating in a mixed HT+non-HT bss
272183251Ssam		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
273183251Ssam		 */
274183251Ssam		if (mode == IEEE80211_MODE_11NA &&
275183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
276183251Ssam			mode = IEEE80211_MODE_11A;
277183251Ssam		else if (mode == IEEE80211_MODE_11NG &&
278183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
279183251Ssam			mode = IEEE80211_MODE_11G;
280183251Ssam		if (mode == IEEE80211_MODE_11G &&
281183251Ssam		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
282183251Ssam			mode = IEEE80211_MODE_11B;
283170530Ssam	}
284183251Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
285165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
286138568Ssam}
287138568Ssam
288141658Ssamstatic __inline void
289141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
290141658Ssam{
291141658Ssam	/* propagate useful state */
292141658Ssam	nbss->ni_authmode = obss->ni_authmode;
293141658Ssam	nbss->ni_txpower = obss->ni_txpower;
294141658Ssam	nbss->ni_vlan = obss->ni_vlan;
295141658Ssam	/* XXX statistics? */
296178354Ssam	/* XXX legacy WDS bssid? */
297141658Ssam}
298141658Ssam
299116742Ssamvoid
300178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
301116742Ssam{
302178354Ssam	struct ieee80211com *ic = vap->iv_ic;
303116742Ssam	struct ieee80211_node *ni;
304116742Ssam
305178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
306178354Ssam		"%s: creating ibss on channel %u\n", __func__,
307178354Ssam		ieee80211_chan2ieee(ic, chan));
308138568Ssam
309178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
310140753Ssam	if (ni == NULL) {
311140753Ssam		/* XXX recovery? */
312138568Ssam		return;
313138568Ssam	}
314178354Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
315178354Ssam	ni->ni_esslen = vap->iv_des_ssid[0].len;
316178354Ssam	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
317178354Ssam	if (vap->iv_bss != NULL)
318178354Ssam		copy_bss(ni, vap->iv_bss);
319148843Ssam	ni->ni_intval = ic->ic_bintval;
320178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
321116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
322116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
323116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
324116742Ssam		ni->ni_fhindex = 1;
325116742Ssam	}
326178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
327178354Ssam		vap->iv_flags |= IEEE80211_F_SIBSS;
328138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
329178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
330178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
331167282Ssam		else {
332167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
333167282Ssam			/* clear group bit, add local bit */
334167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
335167282Ssam		}
336178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
337178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
338178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
339153403Ssam		else
340153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
341138568Ssam	}
342138568Ssam	/*
343138568Ssam	 * Fix the channel and related attributes.
344138568Ssam	 */
345178354Ssam	/* clear DFS CAC state on previous channel */
346178354Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
347178354Ssam	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
348178354Ssam	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
349178354Ssam		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
350170530Ssam	ic->ic_bsschan = chan;
351178354Ssam	ieee80211_node_set_chan(ni, chan);
352170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
353138568Ssam	/*
354178354Ssam	 * Do mode-specific setup.
355138568Ssam	 */
356170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
357170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
358170530Ssam			/*
359178354Ssam			 * Use a mixed 11b/11g basic rate set.
360170530Ssam			 */
361178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
362178354Ssam			    IEEE80211_MODE_11G);
363178354Ssam			if (vap->iv_flags & IEEE80211_F_PUREG) {
364178354Ssam				/*
365178354Ssam				 * Also mark OFDM rates basic so 11b
366178354Ssam				 * stations do not join (WiFi compliance).
367178354Ssam				 */
368178354Ssam				ieee80211_addbasicrates(&ni->ni_rates,
369178354Ssam				    IEEE80211_MODE_11A);
370178354Ssam			}
371170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
372170530Ssam			/*
373170530Ssam			 * Force pure 11b rate set.
374170530Ssam			 */
375178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
376170530Ssam				IEEE80211_MODE_11B);
377170530Ssam		}
378170530Ssam	}
379138568Ssam
380170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
381116742Ssam}
382116742Ssam
383170530Ssam/*
384170530Ssam * Reset bss state on transition to the INIT state.
385170530Ssam * Clear any stations from the table (they have been
386170530Ssam * deauth'd) and reset the bss node (clears key, rate
387170530Ssam * etc. state).
388170530Ssam */
389138568Ssamvoid
390178354Ssamieee80211_reset_bss(struct ieee80211vap *vap)
391138568Ssam{
392178354Ssam	struct ieee80211com *ic = vap->iv_ic;
393138568Ssam	struct ieee80211_node *ni, *obss;
394138568Ssam
395178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
396178354Ssam	/* XXX multi-bss: wrong */
397170530Ssam	ieee80211_reset_erp(ic);
398140753Ssam
399178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
400138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
401178354Ssam	obss = vap->iv_bss;
402178354Ssam	vap->iv_bss = ieee80211_ref_node(ni);
403141658Ssam	if (obss != NULL) {
404141658Ssam		copy_bss(ni, obss);
405148843Ssam		ni->ni_intval = ic->ic_bintval;
406138568Ssam		ieee80211_free_node(obss);
407178354Ssam	} else
408178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
409138568Ssam}
410138568Ssam
411170530Ssamstatic int
412170530Ssammatch_ssid(const struct ieee80211_node *ni,
413170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
414170530Ssam{
415170530Ssam	int i;
416148432Ssam
417170530Ssam	for (i = 0; i < nssid; i++) {
418170530Ssam		if (ni->ni_esslen == ssids[i].len &&
419170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
420170530Ssam			return 1;
421170530Ssam	}
422170530Ssam	return 0;
423170530Ssam}
424170530Ssam
425170530Ssam/*
426170530Ssam * Test a node for suitability/compatibility.
427170530Ssam */
428127767Ssamstatic int
429178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
430127767Ssam{
431178354Ssam	struct ieee80211com *ic = ni->ni_ic;
432170530Ssam        uint8_t rate;
433170530Ssam
434170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
435170530Ssam		return 0;
436178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
437170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
438170530Ssam			return 0;
439170530Ssam	} else {
440170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
441170530Ssam			return 0;
442170530Ssam	}
443178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
444170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
445170530Ssam			return 0;
446170530Ssam	} else {
447170530Ssam		/* XXX does this mean privacy is supported or required? */
448170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
449170530Ssam			return 0;
450170530Ssam	}
451170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
452170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
453170530Ssam	if (rate & IEEE80211_RATE_BASIC)
454170530Ssam		return 0;
455178354Ssam	if (vap->iv_des_nssid != 0 &&
456178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
457170530Ssam		return 0;
458178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
459178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
460170530Ssam		return 0;
461170530Ssam	return 1;
462170530Ssam}
463170530Ssam
464170530Ssam#ifdef IEEE80211_DEBUG
465170530Ssam/*
466170530Ssam * Display node suitability/compatibility.
467170530Ssam */
468170530Ssamstatic void
469178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
470170530Ssam{
471178354Ssam	struct ieee80211com *ic = ni->ni_ic;
472170530Ssam        uint8_t rate;
473127767Ssam        int fail;
474127767Ssam
475127767Ssam	fail = 0;
476127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
477127767Ssam		fail |= 0x01;
478178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
479127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
480127767Ssam			fail |= 0x02;
481127767Ssam	} else {
482127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
483127767Ssam			fail |= 0x02;
484127767Ssam	}
485178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
486127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
487127767Ssam			fail |= 0x04;
488127767Ssam	} else {
489127767Ssam		/* XXX does this mean privacy is supported or required? */
490127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
491127767Ssam			fail |= 0x04;
492127767Ssam	}
493167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
494165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
495127767Ssam	if (rate & IEEE80211_RATE_BASIC)
496127767Ssam		fail |= 0x08;
497178354Ssam	if (vap->iv_des_nssid != 0 &&
498178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
499127767Ssam		fail |= 0x10;
500178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
501178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
502127767Ssam		fail |= 0x20;
503127767Ssam
504170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
505170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
506170530Ssam	printf(" %3d%c",
507170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
508170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
509170530Ssam	    fail & 0x08 ? '!' : ' ');
510170530Ssam	printf(" %4s%c",
511170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
512170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
513170530Ssam	    "????",
514170530Ssam	    fail & 0x02 ? '!' : ' ');
515170530Ssam	printf(" %3s%c ",
516170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
517170530Ssam	    fail & 0x04 ? '!' : ' ');
518170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
519170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
520138568Ssam}
521170530Ssam#endif /* IEEE80211_DEBUG */
522138568Ssam
523138568Ssam/*
524138568Ssam * Handle 802.11 ad hoc network merge.  The
525138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
526138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
527138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
528138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
529138568Ssam * sends beacons with the greatest TSF timestamp.
530138568Ssam *
531138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
532138568Ssam *
533138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
534138568Ssam */
535138568Ssamint
536148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
537138568Ssam{
538178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
539178354Ssam#ifdef IEEE80211_DEBUG
540148306Ssam	struct ieee80211com *ic = ni->ni_ic;
541178354Ssam#endif
542138568Ssam
543178354Ssam	if (ni == vap->iv_bss ||
544178354Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
545138568Ssam		/* unchanged, nothing to do */
546138568Ssam		return 0;
547138568Ssam	}
548178354Ssam	if (!check_bss(vap, ni)) {
549170530Ssam		/* capabilities mismatch */
550178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
551138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
552170530Ssam#ifdef IEEE80211_DEBUG
553178354Ssam		if (ieee80211_msg_assoc(vap))
554178354Ssam			check_bss_debug(vap, ni);
555170530Ssam#endif
556178354Ssam		vap->iv_stats.is_ibss_capmismatch++;
557138568Ssam		return 0;
558138568Ssam	}
559178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
560138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
561138568Ssam		ether_sprintf(ni->ni_bssid),
562138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
563138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
564138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
565138568Ssam	);
566170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
567138568Ssam}
568138568Ssam
569138568Ssam/*
570178354Ssam * Calculate HT channel promotion flags for all vaps.
571178354Ssam * This assumes ni_chan have been setup for each vap.
572173273Ssam */
573178354Ssamstatic int
574178354Ssamgethtadjustflags(struct ieee80211com *ic)
575178354Ssam{
576178354Ssam	struct ieee80211vap *vap;
577178354Ssam	int flags;
578178354Ssam
579178354Ssam	flags = 0;
580178354Ssam	/* XXX locking */
581178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
582178354Ssam		if (vap->iv_state < IEEE80211_S_RUN)
583178354Ssam			continue;
584178354Ssam		switch (vap->iv_opmode) {
585178354Ssam		case IEEE80211_M_WDS:
586178354Ssam		case IEEE80211_M_STA:
587178354Ssam		case IEEE80211_M_AHDEMO:
588178354Ssam		case IEEE80211_M_HOSTAP:
589178354Ssam		case IEEE80211_M_IBSS:
590178354Ssam			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
591178354Ssam			break;
592178354Ssam		default:
593178354Ssam			break;
594178354Ssam		}
595178354Ssam	}
596178354Ssam	return flags;
597178354Ssam}
598178354Ssam
599178354Ssam/*
600178354Ssam * Check if the current channel needs to change based on whether
601178354Ssam * any vap's are using HT20/HT40.  This is used sync the state of
602178354Ssam * ic_curchan after a channel width change on a running vap.
603178354Ssam */
604173273Ssamvoid
605178354Ssamieee80211_sync_curchan(struct ieee80211com *ic)
606173273Ssam{
607178354Ssam	struct ieee80211_channel *c;
608178354Ssam
609178354Ssam	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
610178354Ssam	if (c != ic->ic_curchan) {
611178354Ssam		ic->ic_curchan = c;
612178354Ssam		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
613178354Ssam		ic->ic_set_channel(ic);
614178354Ssam	}
615178354Ssam}
616178354Ssam
617178354Ssam/*
618178354Ssam * Change the current channel.  The request channel may be
619178354Ssam * promoted if other vap's are operating with HT20/HT40.
620178354Ssam */
621178354Ssamvoid
622178354Ssamieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
623178354Ssam{
624178354Ssam	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
625178354Ssam		int flags = gethtadjustflags(ic);
626178354Ssam		/*
627178354Ssam		 * Check for channel promotion required to support the
628178354Ssam		 * set of running vap's.  This assumes we are called
629178354Ssam		 * after ni_chan is setup for each vap.
630178354Ssam		 */
631178354Ssam		/* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
632178354Ssam		if (flags > ieee80211_htchanflags(c))
633178354Ssam			c = ieee80211_ht_adjust_channel(ic, c, flags);
634178354Ssam	}
635178354Ssam	ic->ic_bsschan = ic->ic_curchan = c;
636173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
637173273Ssam	ic->ic_set_channel(ic);
638173273Ssam}
639173273Ssam
640173273Ssam/*
641138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
642138568Ssam * be passed in with a held reference.
643138568Ssam */
644170530Ssamstatic int
645170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
646138568Ssam{
647178354Ssam	struct ieee80211vap *vap = selbs->ni_vap;
648170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
649138568Ssam	struct ieee80211_node *obss;
650170530Ssam	int canreassoc;
651138568Ssam
652138568Ssam	/*
653138568Ssam	 * Committed to selbs, setup state.
654138568Ssam	 */
655178354Ssam	obss = vap->iv_bss;
656170530Ssam	/*
657170530Ssam	 * Check if old+new node have the same address in which
658170530Ssam	 * case we can reassociate when operating in sta mode.
659170530Ssam	 */
660170530Ssam	canreassoc = (obss != NULL &&
661178354Ssam		vap->iv_state == IEEE80211_S_RUN &&
662170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
663178354Ssam	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
664153352Ssam	if (obss != NULL) {
665153352Ssam		copy_bss(selbs, obss);
666178354Ssam		ieee80211_node_reclaim(obss);
667178354Ssam		obss = NULL;		/* NB: guard against later use */
668153352Ssam	}
669165887Ssam
670138568Ssam	/*
671165887Ssam	 * Delete unusable rates; we've already checked
672165887Ssam	 * that the negotiated rate set is acceptable.
673165887Ssam	 */
674178354Ssam	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
675167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
676165887Ssam
677178354Ssam	ieee80211_setcurchan(ic, selbs->ni_chan);
678165887Ssam	/*
679138568Ssam	 * Set the erp state (mostly the slot time) to deal with
680138568Ssam	 * the auto-select case; this should be redundant if the
681138568Ssam	 * mode is locked.
682138568Ssam	 */
683138568Ssam	ieee80211_reset_erp(ic);
684178354Ssam	ieee80211_wme_initparams(vap);
685140753Ssam
686178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
687170530Ssam		if (canreassoc) {
688170530Ssam			/* Reassociate */
689178354Ssam			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
690170530Ssam		} else {
691170530Ssam			/*
692170530Ssam			 * Act as if we received a DEAUTH frame in case we
693170530Ssam			 * are invoked from the RUN state.  This will cause
694170530Ssam			 * us to try to re-authenticate if we are operating
695170530Ssam			 * as a station.
696170530Ssam			 */
697178354Ssam			ieee80211_new_state(vap, IEEE80211_S_AUTH,
698170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
699170530Ssam		}
700170530Ssam	} else
701178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
702138568Ssam	return 1;
703116742Ssam}
704116742Ssam
705170530Ssamint
706184274Ssamieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
707170530Ssam	const struct ieee80211_scan_entry *se)
708170530Ssam{
709178354Ssam	struct ieee80211com *ic = vap->iv_ic;
710170530Ssam	struct ieee80211_node *ni;
711170530Ssam
712178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
713170530Ssam	if (ni == NULL) {
714170530Ssam		/* XXX msg */
715170530Ssam		return 0;
716170530Ssam	}
717170530Ssam	/*
718170530Ssam	 * Expand scan state into node's format.
719170530Ssam	 * XXX may not need all this stuff
720170530Ssam	 */
721170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
722170530Ssam	ni->ni_esslen = se->se_ssid[1];
723170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
724170530Ssam	ni->ni_rstamp = se->se_rstamp;
725170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
726170530Ssam	ni->ni_intval = se->se_intval;
727170530Ssam	ni->ni_capinfo = se->se_capinfo;
728184274Ssam	ni->ni_chan = chan;
729170530Ssam	ni->ni_timoff = se->se_timoff;
730170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
731170530Ssam	ni->ni_fhindex = se->se_fhindex;
732170530Ssam	ni->ni_erp = se->se_erp;
733178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
734170530Ssam	ni->ni_noise = se->se_noise;
735178354Ssam
736178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
737178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
738178354Ssam		if (ni->ni_ies.ath_ie != NULL)
739178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
740178354Ssam		if (ni->ni_ies.htcap_ie != NULL)
741178354Ssam			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
742178354Ssam		if (ni->ni_ies.htinfo_ie != NULL)
743178354Ssam			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
744173864Ssam	}
745170530Ssam
746178354Ssam	vap->iv_dtim_period = se->se_dtimperiod;
747178354Ssam	vap->iv_dtim_count = 0;
748170530Ssam
749170530Ssam	/* NB: must be after ni_chan is setup */
750170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
751170530Ssam		IEEE80211_F_DOSORT);
752170530Ssam
753170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
754170530Ssam}
755170530Ssam
756138568Ssam/*
757138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
758138568Ssam * be passed in with a held reference.
759138568Ssam */
760138568Ssamvoid
761178354Ssamieee80211_sta_leave(struct ieee80211_node *ni)
762138568Ssam{
763178354Ssam	struct ieee80211com *ic = ni->ni_ic;
764178354Ssam
765138568Ssam	ic->ic_node_cleanup(ni);
766178354Ssam	ieee80211_notify_node_leave(ni);
767138568Ssam}
768138568Ssam
769178354Ssam/*
770178354Ssam * Send a deauthenticate frame and drop the station.
771178354Ssam */
772178354Ssamvoid
773178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason)
774178354Ssam{
775178354Ssam	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
776178354Ssam	ieee80211_ref_node(ni);
777178354Ssam	if (ni->ni_associd != 0)
778178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
779178354Ssam	ieee80211_node_leave(ni);
780178354Ssam	ieee80211_free_node(ni);
781178354Ssam}
782178354Ssam
783116742Ssamstatic struct ieee80211_node *
784179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
785116742Ssam{
786127768Ssam	struct ieee80211_node *ni;
787138568Ssam
788184210Sdes	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
789127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
790127768Ssam	return ni;
791116742Ssam}
792116742Ssam
793138568Ssam/*
794178354Ssam * Initialize an ie blob with the specified data.  If previous
795178354Ssam * data exists re-use the data block.  As a side effect we clear
796178354Ssam * all references to specific ie's; the caller is required to
797178354Ssam * recalculate them.
798178354Ssam */
799178354Ssamint
800178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
801178354Ssam{
802178354Ssam	/* NB: assumes data+len are the last fields */
803178354Ssam	memset(ies, 0, offsetof(struct ieee80211_ies, data));
804178354Ssam	if (ies->data != NULL && ies->len != len) {
805178354Ssam		/* data size changed */
806184210Sdes		FREE(ies->data, M_80211_NODE_IE);
807178354Ssam		ies->data = NULL;
808178354Ssam	}
809178354Ssam	if (ies->data == NULL) {
810184210Sdes		MALLOC(ies->data, uint8_t *, len, M_80211_NODE_IE, M_NOWAIT);
811178354Ssam		if (ies->data == NULL) {
812178354Ssam			ies->len = 0;
813178354Ssam			/* NB: pointers have already been zero'd above */
814178354Ssam			return 0;
815178354Ssam		}
816178354Ssam	}
817178354Ssam	memcpy(ies->data, data, len);
818178354Ssam	ies->len = len;
819178354Ssam	return 1;
820178354Ssam}
821178354Ssam
822178354Ssam/*
823178354Ssam * Reclaim storage for an ie blob.
824178354Ssam */
825178354Ssamvoid
826178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies)
827178354Ssam{
828178354Ssam	if (ies->data != NULL)
829184210Sdes		FREE(ies->data, M_80211_NODE_IE);
830178354Ssam}
831178354Ssam
832178354Ssam/*
833178354Ssam * Expand an ie blob data contents and to fillin individual
834178354Ssam * ie pointers.  The data blob is assumed to be well-formed;
835178354Ssam * we don't do any validity checking of ie lengths.
836178354Ssam */
837178354Ssamvoid
838178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies)
839178354Ssam{
840178354Ssam	uint8_t *ie;
841178354Ssam	int ielen;
842178354Ssam
843178354Ssam	ie = ies->data;
844178354Ssam	ielen = ies->len;
845178354Ssam	while (ielen > 0) {
846178354Ssam		switch (ie[0]) {
847178354Ssam		case IEEE80211_ELEMID_VENDOR:
848178354Ssam			if (iswpaoui(ie))
849178354Ssam				ies->wpa_ie = ie;
850178354Ssam			else if (iswmeoui(ie))
851178354Ssam				ies->wme_ie = ie;
852178354Ssam			else if (isatherosoui(ie))
853178354Ssam				ies->ath_ie = ie;
854178354Ssam			break;
855178354Ssam		case IEEE80211_ELEMID_RSN:
856178354Ssam			ies->rsn_ie = ie;
857178354Ssam			break;
858178354Ssam		case IEEE80211_ELEMID_HTCAP:
859178354Ssam			ies->htcap_ie = ie;
860178354Ssam			break;
861178354Ssam		}
862178354Ssam		ielen -= 2 + ie[1];
863178354Ssam		ie += 2 + ie[1];
864178354Ssam	}
865178354Ssam}
866178354Ssam
867178354Ssam/*
868138568Ssam * Reclaim any resources in a node and reset any critical
869138568Ssam * state.  Typically nodes are free'd immediately after,
870138568Ssam * but in some cases the storage may be reused so we need
871138568Ssam * to insure consistent state (should probably fix that).
872138568Ssam */
873116742Ssamstatic void
874138568Ssamnode_cleanup(struct ieee80211_node *ni)
875116742Ssam{
876138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
877178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
878170530Ssam	int i;
879138568Ssam
880138568Ssam	/* NB: preserve ni_table */
881138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
882178354Ssam		if (vap->iv_opmode != IEEE80211_M_STA)
883178354Ssam			vap->iv_ps_sta--;
884138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
885178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
886178354Ssam		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
887138568Ssam	}
888147788Ssam	/*
889173273Ssam	 * Cleanup any HT-related state.
890173273Ssam	 */
891173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
892173273Ssam		ieee80211_ht_node_cleanup(ni);
893173273Ssam	/*
894147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
895147788Ssam	 * has happened.  This is probably not needed as the node
896147788Ssam	 * should always be removed from the table so not found but
897147788Ssam	 * do it just in case.
898147788Ssam	 */
899147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
900138568Ssam
901138568Ssam	/*
902138568Ssam	 * Drain power save queue and, if needed, clear TIM.
903138568Ssam	 */
904178354Ssam	if (ieee80211_node_saveq_drain(ni) != 0 && vap->iv_set_tim != NULL)
905178354Ssam		vap->iv_set_tim(ni, 0);
906138568Ssam
907138568Ssam	ni->ni_associd = 0;
908138568Ssam	if (ni->ni_challenge != NULL) {
909184210Sdes		FREE(ni->ni_challenge, M_80211_NODE);
910138568Ssam		ni->ni_challenge = NULL;
911138568Ssam	}
912138568Ssam	/*
913138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
914138568Ssam	 * reusable during a re-auth/re-assoc state transition.
915138568Ssam	 * If we remove these data they will not be recreated
916138568Ssam	 * because they come from a probe-response or beacon frame
917138568Ssam	 * which cannot be expected prior to the association-response.
918138568Ssam	 * This should not be an issue when operating in other modes
919138568Ssam	 * as stations leaving always go through a full state transition
920138568Ssam	 * which will rebuild this state.
921138568Ssam	 *
922138568Ssam	 * XXX does this leave us open to inheriting old state?
923138568Ssam	 */
924138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
925138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
926138568Ssam			m_freem(ni->ni_rxfrag[i]);
927138568Ssam			ni->ni_rxfrag[i] = NULL;
928138568Ssam		}
929148863Ssam	/*
930148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
931148863Ssam	 */
932148863Ssam	ieee80211_node_delucastkey(ni);
933138568Ssam#undef N
934116742Ssam}
935116742Ssam
936116742Ssamstatic void
937138568Ssamnode_free(struct ieee80211_node *ni)
938116742Ssam{
939138568Ssam	struct ieee80211com *ic = ni->ni_ic;
940138568Ssam
941138568Ssam	ic->ic_node_cleanup(ni);
942178354Ssam	ieee80211_ies_cleanup(&ni->ni_ies);
943138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
944178354Ssam	IEEE80211_NODE_WDSQ_DESTROY(ni);
945184210Sdes	FREE(ni, M_80211_NODE);
946116742Ssam}
947116742Ssam
948178354Ssamstatic void
949178354Ssamnode_age(struct ieee80211_node *ni)
950178354Ssam{
951178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
952178354Ssam#if 0
953178354Ssam	IEEE80211_NODE_LOCK_ASSERT(&ic->ic_sta);
954178354Ssam#endif
955178354Ssam	/*
956178354Ssam	 * Age frames on the power save queue.
957178354Ssam	 */
958178354Ssam	if (ieee80211_node_saveq_age(ni) != 0 &&
959178354Ssam	    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
960178354Ssam	    vap->iv_set_tim != NULL)
961178354Ssam		vap->iv_set_tim(ni, 0);
962178354Ssam	/*
963178354Ssam	 * Age frames on the wds pending queue.
964178354Ssam	 */
965178354Ssam	if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
966178354Ssam		ieee80211_node_wdsq_age(ni);
967178354Ssam	/*
968178354Ssam	 * Age out HT resources (e.g. frames on the
969178354Ssam	 * A-MPDU reorder queues).
970178354Ssam	 */
971178354Ssam	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
972178354Ssam		ieee80211_ht_node_age(ni);
973178354Ssam}
974178354Ssam
975170530Ssamstatic int8_t
976138568Ssamnode_getrssi(const struct ieee80211_node *ni)
977120104Ssam{
978178354Ssam	uint32_t avgrssi = ni->ni_avgrssi;
979178354Ssam	int32_t rssi;
980178354Ssam
981178354Ssam	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
982178354Ssam		return 0;
983178354Ssam	rssi = IEEE80211_RSSI_GET(avgrssi);
984178354Ssam	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
985120104Ssam}
986120104Ssam
987116742Ssamstatic void
988170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
989170530Ssam{
990178354Ssam	*rssi = node_getrssi(ni);
991170530Ssam	*noise = ni->ni_noise;
992170530Ssam}
993170530Ssam
994170530Ssamstatic void
995178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni,
996178354Ssam	struct ieee80211_mimo_info *info)
997116742Ssam{
998178354Ssam	/* XXX zero data? */
999178354Ssam}
1000178354Ssam
1001178354Ssamstruct ieee80211_node *
1002178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt,
1003178354Ssam	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1004178354Ssam{
1005138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1006178354Ssam	struct ieee80211_node *ni;
1007116742Ssam	int hash;
1008116742Ssam
1009179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1010178354Ssam	if (ni == NULL) {
1011178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1012178354Ssam		return NULL;
1013178354Ssam	}
1014178354Ssam
1015178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1016140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1017138568Ssam		ether_sprintf(macaddr), nt->nt_name);
1018138568Ssam
1019116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1020116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1021138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
1022138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
1023138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
1024138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
1025183251Ssam	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1026178354Ssam	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1027178354Ssam	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1028139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
1029139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1030170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
1031138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1032178354Ssam	IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1033138568Ssam
1034138568Ssam	IEEE80211_NODE_LOCK(nt);
1035138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1036138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1037138568Ssam	ni->ni_table = nt;
1038178354Ssam	ni->ni_vap = vap;
1039138568Ssam	ni->ni_ic = ic;
1040138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1041116742Ssam
1042184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1043184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1044184277Ssam
1045116742Ssam	return ni;
1046116742Ssam}
1047116742Ssam
1048148777Ssam/*
1049148777Ssam * Craft a temporary node suitable for sending a management frame
1050148777Ssam * to the specified station.  We craft only as much state as we
1051148777Ssam * need to do the work since the node will be immediately reclaimed
1052148777Ssam * once the send completes.
1053148777Ssam */
1054116742Ssamstruct ieee80211_node *
1055178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1056178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1057148777Ssam{
1058178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1059148777Ssam	struct ieee80211_node *ni;
1060148777Ssam
1061179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1062148777Ssam	if (ni != NULL) {
1063183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1064183259Ssam
1065178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1066148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1067148777Ssam
1068178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1069178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1070178354Ssam		ni->ni_vap = vap;
1071178354Ssam
1072148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1073183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1074148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1075148777Ssam		/* NB: required by ieee80211_fix_rate */
1076183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1077178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1078148777Ssam			IEEE80211_KEYIX_NONE);
1079183259Ssam		ni->ni_txpower = bss->ni_txpower;
1080148777Ssam		/* XXX optimize away */
1081148777Ssam		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1082178354Ssam		IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1083148777Ssam	} else {
1084148777Ssam		/* XXX msg */
1085178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1086148777Ssam	}
1087148777Ssam	return ni;
1088148777Ssam}
1089148777Ssam
1090148777Ssamstruct ieee80211_node *
1091178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1092178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1093116742Ssam{
1094178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1095138568Ssam	struct ieee80211_node *ni;
1096138568Ssam
1097178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1098116742Ssam	if (ni != NULL) {
1099183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1100127770Ssam		/*
1101178354Ssam		 * Inherit from iv_bss.
1102127770Ssam		 */
1103183259Ssam		copy_bss(ni, bss);
1104183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1105183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1106178354Ssam	}
1107116742Ssam	return ni;
1108116742Ssam}
1109116742Ssam
1110178354Ssam/*
1111178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1112178354Ssam * not associate so we just create create a new node and
1113178354Ssam * simulate an association.  The caller is responsible for
1114178354Ssam * installing the node as the bss node and handling any further
1115178354Ssam * setup work like authorizing the port.
1116178354Ssam */
1117178354Ssamstruct ieee80211_node *
1118178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1119178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1120178354Ssam{
1121178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1122178354Ssam	struct ieee80211_node *ni;
1123178354Ssam
1124178354Ssam	/* XXX check if node already in sta table? */
1125178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1126178354Ssam	if (ni != NULL) {
1127178354Ssam		ni->ni_wdsvap = vap;
1128178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1129178354Ssam		/*
1130178354Ssam		 * Inherit any manually configured settings.
1131178354Ssam		 */
1132183259Ssam		copy_bss(ni, vap->iv_bss);
1133178354Ssam		ieee80211_node_set_chan(ni, chan);
1134178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1135178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1136178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1137178354Ssam		/* NB: no associd for peer */
1138178354Ssam		/*
1139178354Ssam		 * There are no management frames to use to
1140178354Ssam		 * discover neighbor capabilities, so blindly
1141178354Ssam		 * propagate the local configuration.
1142178354Ssam		 */
1143178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1144178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1145178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1146178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1147178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1148178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1149178354Ssam			/*
1150178354Ssam			 * Device is HT-capable and HT is enabled for
1151178354Ssam			 * the vap; setup HT operation.  On return
1152178354Ssam			 * ni_chan will be adjusted to an HT channel.
1153178354Ssam			 */
1154178354Ssam			ieee80211_ht_wds_init(ni);
1155178354Ssam		} else {
1156178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1157178354Ssam			/*
1158178354Ssam			 * Force a legacy channel to be used.
1159178354Ssam			 */
1160178354Ssam			c = ieee80211_find_channel(ic,
1161178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1162178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1163178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1164178354Ssam			ni->ni_chan = c;
1165178354Ssam		}
1166178354Ssam	}
1167178354Ssam	return ni;
1168178354Ssam}
1169178354Ssam
1170178354Ssamstruct ieee80211_node *
1171138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1172178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1173178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1174138568Ssam#else
1175178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1176178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1177138568Ssam#endif
1178116742Ssam{
1179116742Ssam	struct ieee80211_node *ni;
1180116742Ssam	int hash;
1181116742Ssam
1182138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1183127772Ssam
1184116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1185138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1186116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1187138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1188138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1189178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1190140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1191140766Ssam			    func, line,
1192140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1193140766Ssam			    ieee80211_node_refcnt(ni));
1194138568Ssam#endif
1195127772Ssam			return ni;
1196116742Ssam		}
1197116742Ssam	}
1198127772Ssam	return NULL;
1199127772Ssam}
1200178354Ssam
1201178354Ssamstruct ieee80211_node *
1202138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1203178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1204178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1205178354Ssam#else
1206178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1207178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1208138568Ssam#endif
1209178354Ssam{
1210178354Ssam	struct ieee80211_node *ni;
1211127772Ssam
1212178354Ssam	IEEE80211_NODE_LOCK(nt);
1213178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1214178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1215178354Ssam	return ni;
1216178354Ssam}
1217178354Ssam
1218127772Ssamstruct ieee80211_node *
1219138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1220178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1221178354Ssam	const struct ieee80211vap *vap,
1222178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1223138568Ssam#else
1224178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1225178354Ssam	const struct ieee80211vap *vap,
1226178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1227138568Ssam#endif
1228127772Ssam{
1229127772Ssam	struct ieee80211_node *ni;
1230178354Ssam	int hash;
1231127772Ssam
1232178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1233178354Ssam
1234178354Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1235178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1236178354Ssam		if (ni->ni_vap == vap &&
1237178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1238178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1239178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1240178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1241178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1242178354Ssam			    func, line,
1243178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1244178354Ssam			    ieee80211_node_refcnt(ni));
1245178354Ssam#endif
1246178354Ssam			return ni;
1247178354Ssam		}
1248178354Ssam	}
1249178354Ssam	return NULL;
1250178354Ssam}
1251178354Ssam
1252178354Ssamstruct ieee80211_node *
1253178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1254178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1255178354Ssam	const struct ieee80211vap *vap,
1256178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1257178354Ssam#else
1258178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1259178354Ssam	const struct ieee80211vap *vap,
1260178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1261178354Ssam#endif
1262178354Ssam{
1263178354Ssam	struct ieee80211_node *ni;
1264178354Ssam
1265138568Ssam	IEEE80211_NODE_LOCK(nt);
1266178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1267138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1268116742Ssam	return ni;
1269116742Ssam}
1270116742Ssam
1271116742Ssam/*
1272138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1273138568Ssam * Note that for the driver's benefit we we treat this like
1274138568Ssam * an association so the driver has an opportunity to setup
1275138568Ssam * it's private state.
1276138568Ssam */
1277138568Ssamstruct ieee80211_node *
1278178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1279170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1280138568Ssam{
1281138568Ssam	struct ieee80211_node *ni;
1282138568Ssam
1283178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1284153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1285178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1286138568Ssam	if (ni != NULL) {
1287178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1288178354Ssam
1289138568Ssam		/* XXX no rate negotiation; just dup */
1290178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1291178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1292153404Ssam			/*
1293170530Ssam			 * In adhoc demo mode there are no management
1294170530Ssam			 * frames to use to discover neighbor capabilities,
1295170530Ssam			 * so blindly propagate the local configuration
1296170530Ssam			 * so we can do interesting things (e.g. use
1297170530Ssam			 * WME to disable ACK's).
1298153404Ssam			 */
1299178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1300153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1301178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1302170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1303153404Ssam		}
1304183251Ssam		node_setuptxparms(ni);
1305178354Ssam		if (ic->ic_newassoc != NULL)
1306178354Ssam			ic->ic_newassoc(ni, 1);
1307170530Ssam		/* XXX not right for 802.1x/WPA */
1308170530Ssam		ieee80211_node_authorize(ni);
1309138568Ssam	}
1310138568Ssam	return ni;
1311138568Ssam}
1312138568Ssam
1313148936Ssamvoid
1314153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1315153073Ssam	const struct ieee80211_frame *wh,
1316153073Ssam	const struct ieee80211_scanparams *sp)
1317153073Ssam{
1318153073Ssam	ni->ni_esslen = sp->ssid[1];
1319153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1320153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1321153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1322153073Ssam	ni->ni_intval = sp->bintval;
1323153073Ssam	ni->ni_capinfo = sp->capinfo;
1324153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1325153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1326153073Ssam	ni->ni_fhindex = sp->fhindex;
1327153073Ssam	ni->ni_erp = sp->erp;
1328153073Ssam	ni->ni_timoff = sp->timoff;
1329153073Ssam
1330178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1331178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1332178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1333178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1334178354Ssam	}
1335178354Ssam
1336153073Ssam	/* NB: must be after ni_chan is setup */
1337165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1338165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1339165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1340153073Ssam}
1341153073Ssam
1342148936Ssam/*
1343148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1344148936Ssam * or probe response frame.  Note that for the driver's
1345148936Ssam * benefit we we treat this like an association so the
1346148936Ssam * driver has an opportunity to setup it's private state.
1347148936Ssam */
1348148936Ssamstruct ieee80211_node *
1349178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1350148936Ssam	const struct ieee80211_frame *wh,
1351148936Ssam	const struct ieee80211_scanparams *sp)
1352148936Ssam{
1353148936Ssam	struct ieee80211_node *ni;
1354148936Ssam
1355178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1356153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1357178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1358148936Ssam	if (ni != NULL) {
1359178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1360178354Ssam
1361153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1362183251Ssam		node_setuptxparms(ni);
1363148936Ssam		if (ic->ic_newassoc != NULL)
1364148936Ssam			ic->ic_newassoc(ni, 1);
1365148936Ssam		/* XXX not right for 802.1x/WPA */
1366148936Ssam		ieee80211_node_authorize(ni);
1367148936Ssam	}
1368148936Ssam	return ni;
1369148936Ssam}
1370148936Ssam
1371148863Ssam#define	IS_CTL(wh) \
1372148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1373148863Ssam#define	IS_PSPOLL(wh) \
1374148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1375170530Ssam#define	IS_BAR(wh) \
1376170530Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1377179220Ssam#define	IS_PROBEREQ(wh) \
1378179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1379179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1380179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1381179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1382179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1383170530Ssam
1384179220Ssamstatic __inline struct ieee80211_node *
1385179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1386179220Ssam    const struct ieee80211_frame_min *wh)
1387179220Ssam{
1388179220Ssam	/* XXX 4-address frames? */
1389179220Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1390179220Ssam		return ieee80211_find_node_locked(nt, wh->i_addr1);
1391179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1392179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1393179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1394179220Ssam}
1395179220Ssam
1396138568Ssam/*
1397138568Ssam * Locate the node for sender, track state, and then pass the
1398179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1399179220Ssam * we can return NULL if the sender is not in the table.
1400138568Ssam */
1401138568Ssamstruct ieee80211_node *
1402138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1403138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1404138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1405138568Ssam#else
1406138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1407138568Ssam	const struct ieee80211_frame_min *wh)
1408138568Ssam#endif
1409138568Ssam{
1410138568Ssam	struct ieee80211_node_table *nt;
1411138568Ssam	struct ieee80211_node *ni;
1412138568Ssam
1413170530Ssam	nt = &ic->ic_sta;
1414138568Ssam	IEEE80211_NODE_LOCK(nt);
1415179220Ssam	ni = _find_rxnode(nt, wh);
1416138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1417138568Ssam
1418148863Ssam	return ni;
1419148863Ssam}
1420148863Ssam
1421148863Ssam/*
1422148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1423148863Ssam * key index as a hint to locate the node in the key
1424148863Ssam * mapping table.  If an entry is present at the key
1425148863Ssam * index we return it; otherwise do a normal lookup and
1426148863Ssam * update the mapping table if the station has a unicast
1427148863Ssam * key assigned to it.
1428148863Ssam */
1429148863Ssamstruct ieee80211_node *
1430148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1431148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1432148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1433148863Ssam	const char *func, int line)
1434148863Ssam#else
1435148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1436148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1437148863Ssam#endif
1438148863Ssam{
1439148863Ssam	struct ieee80211_node_table *nt;
1440148863Ssam	struct ieee80211_node *ni;
1441148863Ssam
1442170530Ssam	nt = &ic->ic_sta;
1443148863Ssam	IEEE80211_NODE_LOCK(nt);
1444148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1445148863Ssam		ni = nt->nt_keyixmap[keyix];
1446148863Ssam	else
1447148863Ssam		ni = NULL;
1448148863Ssam	if (ni == NULL) {
1449179220Ssam		ni = _find_rxnode(nt, wh);
1450178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1451148863Ssam			/*
1452148863Ssam			 * If the station has a unicast key cache slot
1453148863Ssam			 * assigned update the key->node mapping table.
1454148863Ssam			 */
1455148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1456148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1457148863Ssam			if (keyix < nt->nt_keyixmax &&
1458148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1459178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1460178354Ssam				    IEEE80211_MSG_NODE,
1461148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1462148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1463148863Ssam				    ieee80211_node_refcnt(ni)+1);
1464148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1465148863Ssam			}
1466148863Ssam		}
1467179220Ssam	} else {
1468179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1469179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1470179220Ssam		else
1471179220Ssam			ieee80211_ref_node(ni);
1472179220Ssam	}
1473148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1474148863Ssam
1475148863Ssam	return ni;
1476148863Ssam}
1477179220Ssam#undef IS_BCAST_PROBEREQ
1478179220Ssam#undef IS_PROBEREQ
1479170530Ssam#undef IS_BAR
1480138568Ssam#undef IS_PSPOLL
1481138568Ssam#undef IS_CTL
1482138568Ssam
1483138568Ssam/*
1484127772Ssam * Return a reference to the appropriate node for sending
1485127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1486127772Ssam */
1487127772Ssamstruct ieee80211_node *
1488138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1489178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1490178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1491138568Ssam	const char *func, int line)
1492138568Ssam#else
1493178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1494178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1495138568Ssam#endif
1496127772Ssam{
1497178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1498127772Ssam	struct ieee80211_node *ni;
1499127772Ssam
1500127772Ssam	/*
1501127772Ssam	 * The destination address should be in the node table
1502148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1503148863Ssam	 * also optimize station mode operation, all frames go
1504148863Ssam	 * to the bss node.
1505127772Ssam	 */
1506127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1507138568Ssam	IEEE80211_NODE_LOCK(nt);
1508178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1509178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1510178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1511178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1512158121Ssam	else {
1513178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1514178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1515158121Ssam		    (ni != NULL && ni->ni_associd == 0)) {
1516158121Ssam			/*
1517158121Ssam			 * Station is not associated; don't permit the
1518158121Ssam			 * data frame to be sent by returning NULL.  This
1519158121Ssam			 * is kinda a kludge but the least intrusive way
1520158121Ssam			 * to add this check into all drivers.
1521158121Ssam			 */
1522158121Ssam			ieee80211_unref_node(&ni);	/* NB: null's ni */
1523158121Ssam		}
1524158121Ssam	}
1525138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1526138568Ssam
1527138568Ssam	if (ni == NULL) {
1528178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1529178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1530140497Ssam			/*
1531140497Ssam			 * In adhoc mode cons up a node for the destination.
1532140497Ssam			 * Note that we need an additional reference for the
1533178354Ssam			 * caller to be consistent with
1534178354Ssam			 * ieee80211_find_node_locked.
1535140497Ssam			 */
1536178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1537140497Ssam			if (ni != NULL)
1538140497Ssam				(void) ieee80211_ref_node(ni);
1539140497Ssam		} else {
1540178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1541178354Ssam			    "no node, discard frame (%s)", __func__);
1542178354Ssam			vap->iv_stats.is_tx_nonode++;
1543127772Ssam		}
1544127772Ssam	}
1545127772Ssam	return ni;
1546127772Ssam}
1547127772Ssam
1548116742Ssamstatic void
1549138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1550116742Ssam{
1551138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1552119150Ssam
1553179641Ssam	/*
1554179641Ssam	 * NB: careful about referencing the vap as it may be
1555179641Ssam	 * gone if the last reference was held by a driver.
1556179641Ssam	 * We know the com will always be present so it's safe
1557179641Ssam	 * to use ni_ic below to reclaim resources.
1558179641Ssam	 */
1559179641Ssam#if 0
1560178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1561140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1562140766Ssam		ether_sprintf(ni->ni_macaddr),
1563138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1564179641Ssam#endif
1565179641Ssam	if (ni->ni_associd != 0) {
1566179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1567179641Ssam		if (vap->iv_aid_bitmap != NULL)
1568179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1569179641Ssam	}
1570138568Ssam	if (nt != NULL) {
1571138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1572138568Ssam		LIST_REMOVE(ni, ni_hash);
1573138568Ssam	}
1574179641Ssam	ni->ni_ic->ic_node_free(ni);
1575116742Ssam}
1576116742Ssam
1577116742Ssamvoid
1578138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1579138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1580138568Ssam#else
1581138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1582138568Ssam#endif
1583116742Ssam{
1584138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1585119150Ssam
1586138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1587178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1588140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1589138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1590138568Ssam#endif
1591148863Ssam	if (nt != NULL) {
1592148863Ssam		IEEE80211_NODE_LOCK(nt);
1593148863Ssam		if (ieee80211_node_dectestref(ni)) {
1594148863Ssam			/*
1595148863Ssam			 * Last reference, reclaim state.
1596148863Ssam			 */
1597138568Ssam			_ieee80211_free_node(ni);
1598148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1599148863Ssam		    nt->nt_keyixmap != NULL) {
1600148863Ssam			ieee80211_keyix keyix;
1601148863Ssam			/*
1602148863Ssam			 * Check for a last reference in the key mapping table.
1603148863Ssam			 */
1604148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1605148863Ssam			if (keyix < nt->nt_keyixmax &&
1606148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1607178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1608178354Ssam				    IEEE80211_MSG_NODE,
1609148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1610148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1611148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1612148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1613148863Ssam				_ieee80211_free_node(ni);
1614148863Ssam			}
1615148863Ssam		}
1616148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1617148863Ssam	} else {
1618148863Ssam		if (ieee80211_node_dectestref(ni))
1619138568Ssam			_ieee80211_free_node(ni);
1620116742Ssam	}
1621116742Ssam}
1622116742Ssam
1623138568Ssam/*
1624148863Ssam * Reclaim a unicast key and clear any key cache state.
1625148863Ssam */
1626148863Ssamint
1627148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1628148863Ssam{
1629179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1630179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1631148863Ssam	struct ieee80211_node *nikey;
1632148863Ssam	ieee80211_keyix keyix;
1633148863Ssam	int isowned, status;
1634148863Ssam
1635148863Ssam	/*
1636148863Ssam	 * NB: We must beware of LOR here; deleting the key
1637148863Ssam	 * can cause the crypto layer to block traffic updates
1638148863Ssam	 * which can generate a LOR against the node table lock;
1639148863Ssam	 * grab it here and stash the key index for our use below.
1640148863Ssam	 *
1641148863Ssam	 * Must also beware of recursion on the node table lock.
1642148863Ssam	 * When called from node_cleanup we may already have
1643148863Ssam	 * the node table lock held.  Unfortunately there's no
1644148863Ssam	 * way to separate out this path so we must do this
1645148863Ssam	 * conditionally.
1646148863Ssam	 */
1647148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1648148863Ssam	if (!isowned)
1649148863Ssam		IEEE80211_NODE_LOCK(nt);
1650179641Ssam	nikey = NULL;
1651179641Ssam	status = 1;		/* NB: success */
1652179641Ssam	if (!IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1653179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1654179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1655179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1656179641Ssam			nikey = nt->nt_keyixmap[keyix];
1657179641Ssam			nt->nt_keyixmap[keyix] = NULL;;
1658179641Ssam		}
1659179641Ssam	}
1660148863Ssam	if (!isowned)
1661178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1662148863Ssam
1663148863Ssam	if (nikey != NULL) {
1664148863Ssam		KASSERT(nikey == ni,
1665148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1666179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1667148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1668148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1669148863Ssam			ieee80211_node_refcnt(ni)-1);
1670148863Ssam		ieee80211_free_node(ni);
1671148863Ssam	}
1672148863Ssam	return status;
1673148863Ssam}
1674148863Ssam
1675148863Ssam/*
1676138568Ssam * Reclaim a node.  If this is the last reference count then
1677138568Ssam * do the normal free work.  Otherwise remove it from the node
1678138568Ssam * table and mark it gone by clearing the back-reference.
1679138568Ssam */
1680138568Ssamstatic void
1681138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1682116742Ssam{
1683148863Ssam	ieee80211_keyix keyix;
1684138568Ssam
1685148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1686148863Ssam
1687178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1688140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1689140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1690140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1691148863Ssam	/*
1692148863Ssam	 * Clear any entry in the unicast key mapping table.
1693148863Ssam	 * We need to do it here so rx lookups don't find it
1694148863Ssam	 * in the mapping table even if it's not in the hash
1695148863Ssam	 * table.  We cannot depend on the mapping table entry
1696148863Ssam	 * being cleared because the node may not be free'd.
1697148863Ssam	 */
1698148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1699148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1700148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1701178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1702148863Ssam			"%s: %p<%s> clear key map entry\n",
1703148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr));
1704148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1705148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1706148863Ssam	}
1707138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1708138568Ssam		/*
1709138568Ssam		 * Other references are present, just remove the
1710138568Ssam		 * node from the table so it cannot be found.  When
1711138568Ssam		 * the references are dropped storage will be
1712140753Ssam		 * reclaimed.
1713138568Ssam		 */
1714138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1715138568Ssam		LIST_REMOVE(ni, ni_hash);
1716138568Ssam		ni->ni_table = NULL;		/* clear reference */
1717138568Ssam	} else
1718138568Ssam		_ieee80211_free_node(ni);
1719138568Ssam}
1720138568Ssam
1721178354Ssam/*
1722178354Ssam * Reclaim a (bss) node.  Decrement the refcnt and reclaim
1723178354Ssam * the node if the only other reference to it is in the sta
1724178354Ssam * table.  This is effectively ieee80211_free_node followed
1725178354Ssam * by node_reclaim when the refcnt is 1 (after the free).
1726178354Ssam */
1727138568Ssamstatic void
1728178354Ssamieee80211_node_reclaim(struct ieee80211_node *ni)
1729138568Ssam{
1730178354Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1731116742Ssam
1732178354Ssam	KASSERT(nt != NULL, ("reclaim node not in table"));
1733138568Ssam
1734178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1735178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1736178712Ssam		"%s %p<%s> refcnt %d\n", __func__, ni,
1737178354Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1738178354Ssam#endif
1739178354Ssam	IEEE80211_NODE_LOCK(nt);
1740178354Ssam	if (ieee80211_node_dectestref(ni)) {
1741178354Ssam		/*
1742178354Ssam		 * Last reference, reclaim state.
1743178354Ssam		 */
1744178354Ssam		_ieee80211_free_node(ni);
1745178354Ssam		nt = NULL;
1746178354Ssam	} else if (ieee80211_node_refcnt(ni) == 1 &&
1747178354Ssam	    nt->nt_keyixmap != NULL) {
1748178354Ssam		ieee80211_keyix keyix;
1749178354Ssam		/*
1750178354Ssam		 * Check for a last reference in the key mapping table.
1751178354Ssam		 */
1752178354Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1753178354Ssam		if (keyix < nt->nt_keyixmax &&
1754178354Ssam		    nt->nt_keyixmap[keyix] == ni) {
1755178354Ssam			IEEE80211_DPRINTF(ni->ni_vap,
1756178354Ssam			    IEEE80211_MSG_NODE,
1757178354Ssam			    "%s: %p<%s> clear key map entry", __func__,
1758178354Ssam			    ni, ether_sprintf(ni->ni_macaddr));
1759178354Ssam			nt->nt_keyixmap[keyix] = NULL;
1760178354Ssam			ieee80211_node_decref(ni); /* XXX needed? */
1761178354Ssam			_ieee80211_free_node(ni);
1762178354Ssam			nt = NULL;
1763178354Ssam		}
1764178354Ssam	}
1765178354Ssam	if (nt != NULL && ieee80211_node_refcnt(ni) == 1) {
1766178354Ssam		/*
1767178354Ssam		 * Last reference is in the sta table; complete
1768178354Ssam		 * the reclaim.  This handles bss nodes being
1769178354Ssam		 * recycled: the node has two references, one for
1770178354Ssam		 * iv_bss and one for the table.  After dropping
1771178354Ssam		 * the iv_bss ref above we need to reclaim the sta
1772178354Ssam		 * table reference.
1773178354Ssam		 */
1774178354Ssam		ieee80211_node_decref(ni);	/* NB: be pendantic */
1775178354Ssam		_ieee80211_free_node(ni);
1776178354Ssam	}
1777178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1778178354Ssam}
1779178354Ssam
1780178354Ssam/*
1781178354Ssam * Node table support.
1782178354Ssam */
1783178354Ssam
1784178354Ssamstatic void
1785178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1786178354Ssam	struct ieee80211_node_table *nt,
1787178354Ssam	const char *name, int inact, int keyixmax)
1788178354Ssam{
1789178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1790178354Ssam
1791178354Ssam	nt->nt_ic = ic;
1792178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1793178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1794178354Ssam	TAILQ_INIT(&nt->nt_node);
1795178354Ssam	nt->nt_name = name;
1796178354Ssam	nt->nt_scangen = 1;
1797178354Ssam	nt->nt_inact_init = inact;
1798178354Ssam	nt->nt_keyixmax = keyixmax;
1799178354Ssam	if (nt->nt_keyixmax > 0) {
1800184210Sdes		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
1801184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1802178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1803178354Ssam		if (nt->nt_keyixmap == NULL)
1804178354Ssam			if_printf(ic->ic_ifp,
1805178354Ssam			    "Cannot allocate key index map with %u entries\n",
1806178354Ssam			    keyixmax);
1807178354Ssam	} else
1808178354Ssam		nt->nt_keyixmap = NULL;
1809178354Ssam}
1810178354Ssam
1811178354Ssamstatic void
1812178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1813178354Ssam	struct ieee80211vap *match)
1814178354Ssam{
1815178354Ssam	struct ieee80211_node *ni, *next;
1816178354Ssam
1817178354Ssam	IEEE80211_NODE_LOCK(nt);
1818178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1819178354Ssam		if (match != NULL && ni->ni_vap != match)
1820178354Ssam			continue;
1821178354Ssam		/* XXX can this happen?  if so need's work */
1822138568Ssam		if (ni->ni_associd != 0) {
1823178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1824178354Ssam
1825178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1826178354Ssam				vap->iv_auth->ia_node_leave(ni);
1827178354Ssam			if (vap->iv_aid_bitmap != NULL)
1828178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1829138568Ssam		}
1830178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1831138568Ssam		node_reclaim(nt, ni);
1832138568Ssam	}
1833178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1834178354Ssam		/*
1835178354Ssam		 * Make a separate pass to clear references to this vap
1836178354Ssam		 * held by DWDS entries.  They will not be matched above
1837178354Ssam		 * because ni_vap will point to the ap vap but we still
1838178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1839178354Ssam		 * and/or reset.
1840178354Ssam		 */
1841178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1842178354Ssam			if (ni->ni_wdsvap == match)
1843178354Ssam				ni->ni_wdsvap = NULL;
1844178354Ssam	}
1845178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1846116742Ssam}
1847116742Ssam
1848178354Ssamstatic void
1849178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1850178354Ssam{
1851178354Ssam	ieee80211_node_table_reset(nt, NULL);
1852178354Ssam	if (nt->nt_keyixmap != NULL) {
1853178354Ssam#ifdef DIAGNOSTIC
1854178354Ssam		/* XXX verify all entries are NULL */
1855178354Ssam		int i;
1856178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1857178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1858178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1859178354Ssam					nt->nt_name, i);
1860178354Ssam#endif
1861184210Sdes		FREE(nt->nt_keyixmap, M_80211_NODE);
1862178354Ssam		nt->nt_keyixmap = NULL;
1863178354Ssam	}
1864178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1865178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1866178354Ssam}
1867178354Ssam
1868120483Ssam/*
1869138568Ssam * Timeout inactive stations and do related housekeeping.
1870138568Ssam * Note that we cannot hold the node lock while sending a
1871138568Ssam * frame as this would lead to a LOR.  Instead we use a
1872138568Ssam * generation number to mark nodes that we've scanned and
1873138568Ssam * drop the lock and restart a scan if we have to time out
1874138568Ssam * a node.  Since we are single-threaded by virtue of
1875120483Ssam * controlling the inactivity timer we can be sure this will
1876120483Ssam * process each node only once.
1877120483Ssam */
1878138568Ssamstatic void
1879178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1880116742Ssam{
1881178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1882178354Ssam	struct ieee80211vap *vap;
1883120483Ssam	struct ieee80211_node *ni;
1884178354Ssam	int gen = 0;
1885116742Ssam
1886178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
1887154532Ssam	gen = ++nt->nt_scangen;
1888120483Ssamrestart:
1889138568Ssam	IEEE80211_NODE_LOCK(nt);
1890138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1891120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1892120483Ssam			continue;
1893120483Ssam		ni->ni_scangen = gen;
1894138568Ssam		/*
1895147788Ssam		 * Ignore entries for which have yet to receive an
1896147788Ssam		 * authentication frame.  These are transient and
1897147788Ssam		 * will be reclaimed when the last reference to them
1898147788Ssam		 * goes away (when frame xmits complete).
1899147788Ssam		 */
1900178354Ssam		vap = ni->ni_vap;
1901178354Ssam		/*
1902178354Ssam		 * Only process stations when in RUN state.  This
1903178354Ssam		 * insures, for example, that we don't timeout an
1904178354Ssam		 * inactive station during CAC.  Note that CSA state
1905178354Ssam		 * is actually handled in ieee80211_node_timeout as
1906178354Ssam		 * it applies to more than timeout processing.
1907178354Ssam		 */
1908178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
1909178354Ssam			continue;
1910178354Ssam		/* XXX can vap be NULL? */
1911178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1912178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
1913148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1914147788Ssam			continue;
1915147788Ssam		/*
1916138568Ssam		 * Free fragment if not needed anymore
1917138568Ssam		 * (last fragment older than 1s).
1918178354Ssam		 * XXX doesn't belong here, move to node_age
1919138568Ssam		 */
1920138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1921138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1922138568Ssam			m_freem(ni->ni_rxfrag[0]);
1923138568Ssam			ni->ni_rxfrag[0] = NULL;
1924138568Ssam		}
1925184277Ssam		if (ni->ni_inact > 0) {
1926172062Ssam			ni->ni_inact--;
1927184277Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1928184277Ssam			    "%s: inact %u inact_reload %u nrates %u",
1929184277Ssam			    __func__, ni->ni_inact, ni->ni_inact_reload,
1930184277Ssam			    ni->ni_rates.rs_nrates);
1931184277Ssam		}
1932140498Ssam		/*
1933140498Ssam		 * Special case ourself; we may be idle for extended periods
1934140498Ssam		 * of time and regardless reclaiming our state is wrong.
1935178354Ssam		 * XXX run ic_node_age
1936140498Ssam		 */
1937178354Ssam		if (ni == vap->iv_bss)
1938140498Ssam			continue;
1939178354Ssam		if (ni->ni_associd != 0 ||
1940178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1941178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1942119150Ssam			/*
1943178354Ssam			 * Age/drain resources held by the station.
1944138568Ssam			 */
1945178354Ssam			ic->ic_node_age(ni);
1946138568Ssam			/*
1947138568Ssam			 * Probe the station before time it out.  We
1948138568Ssam			 * send a null data frame which may not be
1949138568Ssam			 * universally supported by drivers (need it
1950138568Ssam			 * for ps-poll support so it should be...).
1951170530Ssam			 *
1952170530Ssam			 * XXX don't probe the station unless we've
1953170530Ssam			 *     received a frame from them (and have
1954170530Ssam			 *     some idea of the rates they are capable
1955170530Ssam			 *     of); this will get fixed more properly
1956170530Ssam			 *     soon with better handling of the rate set.
1957138568Ssam			 */
1958178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1959172062Ssam			    (0 < ni->ni_inact &&
1960178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
1961170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1962178354Ssam				IEEE80211_NOTE(vap,
1963148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1964148320Ssam				    ni, "%s",
1965148320Ssam				    "probe station due to inactivity");
1966148582Ssam				/*
1967148582Ssam				 * Grab a reference before unlocking the table
1968148582Ssam				 * so the node cannot be reclaimed before we
1969148582Ssam				 * send the frame. ieee80211_send_nulldata
1970148582Ssam				 * understands we've done this and reclaims the
1971148582Ssam				 * ref for us as needed.
1972148582Ssam				 */
1973148582Ssam				ieee80211_ref_node(ni);
1974138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1975148301Ssam				ieee80211_send_nulldata(ni);
1976138568Ssam				/* XXX stat? */
1977138568Ssam				goto restart;
1978138568Ssam			}
1979138568Ssam		}
1980178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1981172062Ssam		    ni->ni_inact <= 0) {
1982178354Ssam			IEEE80211_NOTE(vap,
1983148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1984148320Ssam			    "station timed out due to inactivity "
1985148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1986138568Ssam			/*
1987138568Ssam			 * Send a deauthenticate frame and drop the station.
1988138568Ssam			 * This is somewhat complicated due to reference counts
1989138568Ssam			 * and locking.  At this point a station will typically
1990138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1991138568Ssam			 * will do a "free" of the node which will drop the
1992138568Ssam			 * reference count.  But in the meantime a reference
1993138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1994138568Ssam			 * of the node will happen either after the tx is
1995138568Ssam			 * completed or by ieee80211_node_leave.
1996120483Ssam			 *
1997138568Ssam			 * Separately we must drop the node lock before sending
1998170530Ssam			 * in case the driver takes a lock, as this can result
1999170530Ssam			 * in a LOR between the node lock and the driver lock.
2000119150Ssam			 */
2001172229Ssam			ieee80211_ref_node(ni);
2002138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2003138568Ssam			if (ni->ni_associd != 0) {
2004178354Ssam				IEEE80211_SEND_MGMT(ni,
2005138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
2006138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
2007138568Ssam			}
2008178354Ssam			ieee80211_node_leave(ni);
2009172229Ssam			ieee80211_free_node(ni);
2010178354Ssam			vap->iv_stats.is_node_timeout++;
2011120483Ssam			goto restart;
2012120483Ssam		}
2013116742Ssam	}
2014138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2015138568Ssam
2016178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2017170530Ssam}
2018138568Ssam
2019178354Ssam/*
2020178354Ssam * Aggressively reclaim resources.  This should be used
2021178354Ssam * only in a critical situation to reclaim mbuf resources.
2022178354Ssam */
2023170530Ssamvoid
2024178354Ssamieee80211_drain(struct ieee80211com *ic)
2025178354Ssam{
2026178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2027178354Ssam	struct ieee80211vap *vap;
2028178354Ssam	struct ieee80211_node *ni;
2029178354Ssam
2030178354Ssam	IEEE80211_NODE_LOCK(nt);
2031178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2032178354Ssam		/*
2033178354Ssam		 * Ignore entries for which have yet to receive an
2034178354Ssam		 * authentication frame.  These are transient and
2035178354Ssam		 * will be reclaimed when the last reference to them
2036178354Ssam		 * goes away (when frame xmits complete).
2037178354Ssam		 */
2038178354Ssam		vap = ni->ni_vap;
2039178354Ssam		/*
2040178354Ssam		 * Only process stations when in RUN state.  This
2041178354Ssam		 * insures, for example, that we don't timeout an
2042178354Ssam		 * inactive station during CAC.  Note that CSA state
2043178354Ssam		 * is actually handled in ieee80211_node_timeout as
2044178354Ssam		 * it applies to more than timeout processing.
2045178354Ssam		 */
2046178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2047178354Ssam			continue;
2048178354Ssam		/* XXX can vap be NULL? */
2049178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2050178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2051178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2052178354Ssam			continue;
2053178354Ssam		/*
2054178354Ssam		 * Free fragments.
2055178354Ssam		 * XXX doesn't belong here, move to node_drain
2056178354Ssam		 */
2057178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2058178354Ssam			m_freem(ni->ni_rxfrag[0]);
2059178354Ssam			ni->ni_rxfrag[0] = NULL;
2060178354Ssam		}
2061178354Ssam		/*
2062178354Ssam		 * Drain resources held by the station.
2063178354Ssam		 */
2064178354Ssam		ic->ic_node_drain(ni);
2065178354Ssam	}
2066178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2067178354Ssam}
2068178354Ssam
2069178354Ssam/*
2070178354Ssam * Per-ieee80211com inactivity timer callback.
2071178354Ssam */
2072178354Ssamvoid
2073170530Ssamieee80211_node_timeout(void *arg)
2074170530Ssam{
2075170530Ssam	struct ieee80211com *ic = arg;
2076170530Ssam
2077178354Ssam	/*
2078178354Ssam	 * Defer timeout processing if a channel switch is pending.
2079178354Ssam	 * We typically need to be mute so not doing things that
2080178354Ssam	 * might generate frames is good to handle in one place.
2081178354Ssam	 * Supressing the station timeout processing may extend the
2082178354Ssam	 * lifetime of inactive stations (by not decrementing their
2083178354Ssam	 * idle counters) but this should be ok unless the CSA is
2084178354Ssam	 * active for an unusually long time.
2085178354Ssam	 */
2086178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2087178354Ssam		ieee80211_scan_timeout(ic);
2088178354Ssam		ieee80211_timeout_stations(ic);
2089170530Ssam
2090178354Ssam		IEEE80211_LOCK(ic);
2091178354Ssam		ieee80211_erp_timeout(ic);
2092178354Ssam		ieee80211_ht_timeout(ic);
2093178354Ssam		IEEE80211_UNLOCK(ic);
2094178354Ssam	}
2095170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2096170530Ssam		ieee80211_node_timeout, ic);
2097116742Ssam}
2098116742Ssam
2099116742Ssamvoid
2100178354Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2101178354Ssam	ieee80211_iter_func *f, void *arg)
2102116742Ssam{
2103116742Ssam	struct ieee80211_node *ni;
2104138568Ssam	u_int gen;
2105116742Ssam
2106178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2107154532Ssam	gen = ++nt->nt_scangen;
2108138568Ssamrestart:
2109138568Ssam	IEEE80211_NODE_LOCK(nt);
2110138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2111138568Ssam		if (ni->ni_scangen != gen) {
2112138568Ssam			ni->ni_scangen = gen;
2113138568Ssam			(void) ieee80211_ref_node(ni);
2114138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2115138568Ssam			(*f)(arg, ni);
2116138568Ssam			ieee80211_free_node(ni);
2117138568Ssam			goto restart;
2118138568Ssam		}
2119138568Ssam	}
2120138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2121138568Ssam
2122178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2123116742Ssam}
2124138568Ssam
2125138568Ssamvoid
2126138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2127138568Ssam{
2128138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2129138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2130138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2131138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2132138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2133138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2134138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2135167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2136167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2137167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2138138568Ssam		ni->ni_rxfragstamp);
2139170530Ssam	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2140178354Ssam		ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
2141170530Ssam		ni->ni_intval, ni->ni_capinfo);
2142138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2143138568Ssam		ether_sprintf(ni->ni_bssid),
2144138568Ssam		ni->ni_esslen, ni->ni_essid,
2145138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2146184277Ssam	printf("\tinact %u inact_reload %u txrate %u\n",
2147184277Ssam		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2148170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2149170530Ssam		ni->ni_htcap, ni->ni_htparam,
2150170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2151170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2152170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2153138568Ssam}
2154138568Ssam
2155138568Ssamvoid
2156138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2157138568Ssam{
2158138568Ssam	ieee80211_iterate_nodes(nt,
2159138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2160138568Ssam}
2161138568Ssam
2162179642Ssamstatic void
2163179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2164172211Ssam{
2165178354Ssam	struct ieee80211vap *vap;
2166178354Ssam
2167178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2168178354Ssam
2169178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2170178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2171178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2172172211Ssam}
2173172211Ssam
2174179642Ssamvoid
2175179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2176179642Ssam{
2177179642Ssam	IEEE80211_LOCK(ic);
2178179642Ssam	ieee80211_notify_erp_locked(ic);
2179179642Ssam	IEEE80211_UNLOCK(ic);
2180179642Ssam}
2181179642Ssam
2182138568Ssam/*
2183138568Ssam * Handle a station joining an 11g network.
2184138568Ssam */
2185138568Ssamstatic void
2186178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2187138568Ssam{
2188178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2189138568Ssam
2190173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2191173273Ssam
2192138568Ssam	/*
2193138568Ssam	 * Station isn't capable of short slot time.  Bump
2194138568Ssam	 * the count of long slot time stations and disable
2195138568Ssam	 * use of short slot time.  Note that the actual switch
2196138568Ssam	 * over to long slot time use may not occur until the
2197138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2198138568Ssam	 */
2199138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2200138568Ssam		ic->ic_longslotsta++;
2201178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2202172211Ssam		    "station needs long slot time, count %d",
2203172211Ssam		    ic->ic_longslotsta);
2204138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2205170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2206170530Ssam			/*
2207170530Ssam			 * Don't force slot time when switched to turbo
2208170530Ssam			 * mode as non-ERP stations won't be present; this
2209170530Ssam			 * need only be done when on the normal G channel.
2210170530Ssam			 */
2211170530Ssam			ieee80211_set_shortslottime(ic, 0);
2212170530Ssam		}
2213138568Ssam	}
2214138568Ssam	/*
2215138568Ssam	 * If the new station is not an ERP station
2216138568Ssam	 * then bump the counter and enable protection
2217138568Ssam	 * if configured.
2218138568Ssam	 */
2219178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2220138568Ssam		ic->ic_nonerpsta++;
2221178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2222172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2223172211Ssam		    ic->ic_nonerpsta);
2224138568Ssam		/*
2225138568Ssam		 * If station does not support short preamble
2226138568Ssam		 * then we must enable use of Barker preamble.
2227138568Ssam		 */
2228138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2229178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2230172211Ssam			    "%s", "station needs long preamble");
2231138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2232138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2233138568Ssam		}
2234172211Ssam		/*
2235178354Ssam		 * If protection is configured and this is the first
2236178354Ssam		 * indication we should use protection, enable it.
2237172211Ssam		 */
2238172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2239172211Ssam		    ic->ic_nonerpsta == 1 &&
2240172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2241178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2242172211Ssam			    "%s: enable use of protection\n", __func__);
2243172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2244179642Ssam			ieee80211_notify_erp_locked(ic);
2245172211Ssam		}
2246138568Ssam	} else
2247138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2248138568Ssam}
2249138568Ssam
2250138568Ssamvoid
2251178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2252138568Ssam{
2253178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2254178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2255138568Ssam	int newassoc;
2256138568Ssam
2257138568Ssam	if (ni->ni_associd == 0) {
2258170530Ssam		uint16_t aid;
2259138568Ssam
2260178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2261138568Ssam		/*
2262138568Ssam		 * It would be good to search the bitmap
2263138568Ssam		 * more efficiently, but this will do for now.
2264138568Ssam		 */
2265178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2266178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2267138568Ssam				break;
2268138568Ssam		}
2269178354Ssam		if (aid >= vap->iv_max_aid) {
2270179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2271178354Ssam			ieee80211_node_leave(ni);
2272138568Ssam			return;
2273138568Ssam		}
2274138568Ssam		ni->ni_associd = aid | 0xc000;
2275173273Ssam		ni->ni_jointime = time_uptime;
2276178354Ssam		IEEE80211_LOCK(ic);
2277178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2278178354Ssam		vap->iv_sta_assoc++;
2279138568Ssam		ic->ic_sta_assoc++;
2280173273Ssam
2281173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2282173273Ssam			ieee80211_ht_node_join(ni);
2283170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2284170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2285178354Ssam			ieee80211_node_join_11g(ni);
2286173273Ssam		IEEE80211_UNLOCK(ic);
2287173273Ssam
2288173273Ssam		newassoc = 1;
2289138568Ssam	} else
2290138568Ssam		newassoc = 0;
2291138568Ssam
2292178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2293183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2294139523Ssam	    IEEE80211_NODE_AID(ni),
2295139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2296139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2297139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2298170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2299170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2300182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2301173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2302183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2303183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2304183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2305178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2306170530Ssam		", fast-frames" : "",
2307178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2308170530Ssam		", turbo" : ""
2309139523Ssam	);
2310138568Ssam
2311183251Ssam	node_setuptxparms(ni);
2312138568Ssam	/* give driver a chance to setup state like ni_txrate */
2313139524Ssam	if (ic->ic_newassoc != NULL)
2314148307Ssam		ic->ic_newassoc(ni, newassoc);
2315178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2316138568Ssam	/* tell the authenticator about new station */
2317178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2318178354Ssam		vap->iv_auth->ia_node_join(ni);
2319178354Ssam	ieee80211_notify_node_join(ni,
2320173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2321138568Ssam}
2322138568Ssam
2323172211Ssamstatic void
2324172211Ssamdisable_protection(struct ieee80211com *ic)
2325172211Ssam{
2326172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2327172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2328172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2329172211Ssam	   ic->ic_flags_ext));
2330172211Ssam
2331172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2332172211Ssam	/* XXX verify mode? */
2333172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2334172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2335172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2336172211Ssam	}
2337179642Ssam	ieee80211_notify_erp_locked(ic);
2338172211Ssam}
2339172211Ssam
2340138568Ssam/*
2341138568Ssam * Handle a station leaving an 11g network.
2342138568Ssam */
2343138568Ssamstatic void
2344178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2345138568Ssam{
2346178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2347138568Ssam
2348173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2349173273Ssam
2350170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2351178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2352178354Ssam	      ic->ic_bsschan->ic_flags));
2353138568Ssam
2354138568Ssam	/*
2355138568Ssam	 * If a long slot station do the slot time bookkeeping.
2356138568Ssam	 */
2357138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2358138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2359138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2360138568Ssam		ic->ic_longslotsta--;
2361178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2362172211Ssam		    "long slot time station leaves, count now %d",
2363172211Ssam		    ic->ic_longslotsta);
2364138568Ssam		if (ic->ic_longslotsta == 0) {
2365138568Ssam			/*
2366138568Ssam			 * Re-enable use of short slot time if supported
2367138568Ssam			 * and not operating in IBSS mode (per spec).
2368138568Ssam			 */
2369138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2370138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2371178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2372178354Ssam				    IEEE80211_MSG_ASSOC,
2373138568Ssam				    "%s: re-enable use of short slot time\n",
2374138568Ssam				    __func__);
2375138568Ssam				ieee80211_set_shortslottime(ic, 1);
2376138568Ssam			}
2377138568Ssam		}
2378138568Ssam	}
2379138568Ssam	/*
2380138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2381138568Ssam	 */
2382138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2383138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2384138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2385138568Ssam		ic->ic_nonerpsta--;
2386178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2387172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2388172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2389172211Ssam			" (non-ERP sta present)" : "");
2390172211Ssam		if (ic->ic_nonerpsta == 0 &&
2391172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2392178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2393138568Ssam				"%s: disable use of protection\n", __func__);
2394172211Ssam			disable_protection(ic);
2395138568Ssam		}
2396138568Ssam	}
2397138568Ssam}
2398138568Ssam
2399138568Ssam/*
2400172211Ssam * Time out presence of an overlapping bss with non-ERP
2401172211Ssam * stations.  When operating in hostap mode we listen for
2402172211Ssam * beacons from other stations and if we identify a non-ERP
2403172211Ssam * station is present we enable protection.  To identify
2404172211Ssam * when all non-ERP stations are gone we time out this
2405172211Ssam * condition.
2406172211Ssam */
2407172211Ssamstatic void
2408172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2409172211Ssam{
2410172211Ssam
2411172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2412172211Ssam
2413172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2414172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2415178354Ssam#if 0
2416178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2417178354Ssam		    "%s", "age out non-ERP sta present on channel");
2418178354Ssam#endif
2419172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2420172211Ssam		if (ic->ic_nonerpsta == 0)
2421172211Ssam			disable_protection(ic);
2422172211Ssam	}
2423172211Ssam}
2424172211Ssam
2425172211Ssam/*
2426138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2427138568Ssam * when operating as an ap.
2428138568Ssam */
2429138568Ssamvoid
2430178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2431138568Ssam{
2432178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2433178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2434140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2435138568Ssam
2436178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2437172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2438138568Ssam
2439178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2440178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2441138568Ssam	/*
2442138568Ssam	 * If node wasn't previously associated all
2443138568Ssam	 * we need to do is reclaim the reference.
2444138568Ssam	 */
2445138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2446138568Ssam	if (ni->ni_associd == 0)
2447138568Ssam		goto done;
2448138568Ssam	/*
2449138568Ssam	 * Tell the authenticator the station is leaving.
2450138568Ssam	 * Note that we must do this before yanking the
2451138568Ssam	 * association id as the authenticator uses the
2452138568Ssam	 * associd to locate it's state block.
2453138568Ssam	 */
2454178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2455178354Ssam		vap->iv_auth->ia_node_leave(ni);
2456173273Ssam
2457173273Ssam	IEEE80211_LOCK(ic);
2458178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2459138568Ssam	ni->ni_associd = 0;
2460178354Ssam	vap->iv_sta_assoc--;
2461138568Ssam	ic->ic_sta_assoc--;
2462138568Ssam
2463173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2464173273Ssam		ieee80211_ht_node_leave(ni);
2465170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2466170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2467178354Ssam		ieee80211_node_leave_11g(ni);
2468173273Ssam	IEEE80211_UNLOCK(ic);
2469138568Ssam	/*
2470138568Ssam	 * Cleanup station state.  In particular clear various
2471138568Ssam	 * state that might otherwise be reused if the node
2472138568Ssam	 * is reused before the reference count goes to zero
2473138568Ssam	 * (and memory is reclaimed).
2474138568Ssam	 */
2475178354Ssam	ieee80211_sta_leave(ni);
2476138568Ssamdone:
2477140499Ssam	/*
2478140499Ssam	 * Remove the node from any table it's recorded in and
2479140499Ssam	 * drop the caller's reference.  Removal from the table
2480140499Ssam	 * is important to insure the node is not reprocessed
2481140499Ssam	 * for inactivity.
2482140499Ssam	 */
2483140499Ssam	if (nt != NULL) {
2484140499Ssam		IEEE80211_NODE_LOCK(nt);
2485140499Ssam		node_reclaim(nt, ni);
2486140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2487140499Ssam	} else
2488140499Ssam		ieee80211_free_node(ni);
2489138568Ssam}
2490138568Ssam
2491178354Ssamstruct rssiinfo {
2492178354Ssam	struct ieee80211vap *vap;
2493178354Ssam	int	rssi_samples;
2494178354Ssam	uint32_t rssi_total;
2495178354Ssam};
2496178354Ssam
2497178354Ssamstatic void
2498178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni)
2499178354Ssam{
2500178354Ssam	struct rssiinfo *info = arg;
2501178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2502178354Ssam	int8_t rssi;
2503178354Ssam
2504178354Ssam	if (info->vap != vap)
2505178354Ssam		return;
2506178354Ssam	/* only associated stations */
2507178354Ssam	if (ni->ni_associd == 0)
2508178354Ssam		return;
2509178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2510178354Ssam	if (rssi != 0) {
2511178354Ssam		info->rssi_samples++;
2512178354Ssam		info->rssi_total += rssi;
2513178354Ssam	}
2514178354Ssam}
2515178354Ssam
2516178354Ssamstatic void
2517178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2518178354Ssam{
2519178354Ssam	struct rssiinfo *info = arg;
2520178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2521178354Ssam	int8_t rssi;
2522178354Ssam
2523178354Ssam	if (info->vap != vap)
2524178354Ssam		return;
2525178354Ssam	/* only neighbors */
2526178354Ssam	/* XXX check bssid */
2527178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2528178354Ssam		return;
2529178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2530178354Ssam	if (rssi != 0) {
2531178354Ssam		info->rssi_samples++;
2532178354Ssam		info->rssi_total += rssi;
2533178354Ssam	}
2534178354Ssam}
2535178354Ssam
2536170530Ssamint8_t
2537178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2538138568Ssam{
2539138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2540178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2541178354Ssam	struct rssiinfo info;
2542138568Ssam
2543178354Ssam	info.rssi_total = 0;
2544178354Ssam	info.rssi_samples = 0;
2545178354Ssam	info.vap = vap;
2546178354Ssam	switch (vap->iv_opmode) {
2547138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2548138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2549178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2550178354Ssam		break;
2551138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2552178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2553138568Ssam		break;
2554138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2555138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2556138568Ssam	default:
2557178354Ssam		if (vap->iv_bss != NULL)
2558178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2559178354Ssam		info.rssi_samples = 1;
2560138568Ssam		break;
2561138568Ssam	}
2562178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2563138568Ssam#undef NZ
2564138568Ssam}
2565138568Ssam
2566170530Ssamvoid
2567178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2568138568Ssam{
2569138568Ssam
2570178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2571170530Ssam		return;
2572178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2573170530Ssam	/* for non-station mode return avg'd rssi accounting */
2574178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2575178354Ssam		*rssi = ieee80211_getrssi(vap);
2576138568Ssam}
2577