ieee80211_node.c revision 206358
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 206358 2010-04-07 15:29:13Z rpaulo $");
29116742Ssam
30178354Ssam#include "opt_wlan.h"
31178354Ssam
32116742Ssam#include <sys/param.h>
33116742Ssam#include <sys/systm.h>
34116742Ssam#include <sys/mbuf.h>
35116742Ssam#include <sys/malloc.h>
36116742Ssam#include <sys/kernel.h>
37138568Ssam
38116742Ssam#include <sys/socket.h>
39116742Ssam
40116742Ssam#include <net/if.h>
41116742Ssam#include <net/if_media.h>
42116742Ssam#include <net/ethernet.h>
43116742Ssam
44116742Ssam#include <net80211/ieee80211_var.h>
45178354Ssam#include <net80211/ieee80211_input.h>
46190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
47190391Ssam#include <net80211/ieee80211_superg.h>
48190391Ssam#endif
49186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
50186904Ssam#include <net80211/ieee80211_tdma.h>
51186904Ssam#endif
52178354Ssam#include <net80211/ieee80211_wds.h>
53195618Srpaulo#include <net80211/ieee80211_mesh.h>
54206358Srpaulo#include <net80211/ieee80211_ratectl.h>
55116742Ssam
56116742Ssam#include <net/bpf.h>
57116742Ssam
58147221Ssam/*
59195618Srpaulo * IEEE80211_NODE_HASHSIZE must be a power of 2.
60195618Srpaulo */
61195618SrpauloCTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
62195618Srpaulo
63195618Srpaulo/*
64147221Ssam * Association id's are managed with a bit vector.
65147221Ssam */
66178354Ssam#define	IEEE80211_AID_SET(_vap, b) \
67178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
68178354Ssam		(1 << (IEEE80211_AID(b) % 32)))
69178354Ssam#define	IEEE80211_AID_CLR(_vap, b) \
70178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
71178354Ssam		~(1 << (IEEE80211_AID(b) % 32)))
72178354Ssam#define	IEEE80211_AID_ISSET(_vap, b) \
73178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
74147221Ssam
75159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
76159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
77159139Sdds#else
78159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
79159139Sdds#endif
80159139Sdds
81170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
82170530Ssam
83179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *,
84179643Ssam	const uint8_t [IEEE80211_ADDR_LEN]);
85138568Ssamstatic void node_cleanup(struct ieee80211_node *);
86138568Ssamstatic void node_free(struct ieee80211_node *);
87178354Ssamstatic void node_age(struct ieee80211_node *);
88170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
89170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
90178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *,
91178354Ssam	struct ieee80211_mimo_info *);
92116742Ssam
93138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
94120104Ssam
95138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
96148863Ssam	struct ieee80211_node_table *nt, const char *name,
97170530Ssam	int inact, int keymaxix);
98178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *,
99178354Ssam	struct ieee80211vap *);
100138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
101172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
102138568Ssam
103127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
104178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
105120481Ssam
106116742Ssamvoid
107138568Ssamieee80211_node_attach(struct ieee80211com *ic)
108116742Ssam{
109195379Ssam	/* XXX really want maxlen enforced per-sta */
110195379Ssam	ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
111195379Ssam	    "802.11 staging q");
112178354Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
113178354Ssam		IEEE80211_INACT_INIT, ic->ic_max_keyix);
114178354Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
115178354Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
116178354Ssam		ieee80211_node_timeout, ic);
117116742Ssam
118138568Ssam	ic->ic_node_alloc = node_alloc;
119138568Ssam	ic->ic_node_free = node_free;
120138568Ssam	ic->ic_node_cleanup = node_cleanup;
121178354Ssam	ic->ic_node_age = node_age;
122178354Ssam	ic->ic_node_drain = node_age;		/* NB: same as age */
123138568Ssam	ic->ic_node_getrssi = node_getrssi;
124170530Ssam	ic->ic_node_getsignal = node_getsignal;
125178354Ssam	ic->ic_node_getmimoinfo = node_getmimoinfo;
126138568Ssam
127178354Ssam	/*
128178354Ssam	 * Set flags to be propagated to all vap's;
129178354Ssam	 * these define default behaviour/configuration.
130178354Ssam	 */
131178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
132178354Ssam}
133138568Ssam
134178354Ssamvoid
135178354Ssamieee80211_node_detach(struct ieee80211com *ic)
136178354Ssam{
137170530Ssam
138178354Ssam	callout_drain(&ic->ic_inact);
139178354Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
140195379Ssam	ieee80211_ageq_cleanup(&ic->ic_stageq);
141178354Ssam}
142172062Ssam
143178354Ssamvoid
144178354Ssamieee80211_node_vattach(struct ieee80211vap *vap)
145178354Ssam{
146178354Ssam	/* NB: driver can override */
147178354Ssam	vap->iv_max_aid = IEEE80211_AID_DEF;
148178354Ssam
149178354Ssam	/* default station inactivity timer setings */
150178354Ssam	vap->iv_inact_init = IEEE80211_INACT_INIT;
151178354Ssam	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
152178354Ssam	vap->iv_inact_run = IEEE80211_INACT_RUN;
153178354Ssam	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
154184277Ssam
155184277Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
156184277Ssam	    "%s: init %u auth %u run %u probe %u\n", __func__,
157184277Ssam	    vap->iv_inact_init, vap->iv_inact_auth,
158184277Ssam	    vap->iv_inact_run, vap->iv_inact_probe);
159148863Ssam}
160148863Ssam
161148863Ssamvoid
162178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap)
163148863Ssam{
164178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
165178354Ssam		/* XXX should we allow max aid to be zero? */
166178354Ssam		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
167178354Ssam			vap->iv_max_aid = IEEE80211_AID_MIN;
168178354Ssam			if_printf(vap->iv_ifp,
169178354Ssam			    "WARNING: max aid too small, changed to %d\n",
170178354Ssam			    vap->iv_max_aid);
171178354Ssam		}
172186302Ssam		vap->iv_aid_bitmap = (uint32_t *) malloc(
173184210Sdes			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
174178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
175178354Ssam		if (vap->iv_aid_bitmap == NULL) {
176178354Ssam			/* XXX no way to recover */
177178354Ssam			printf("%s: no memory for AID bitmap, max aid %d!\n",
178178354Ssam			    __func__, vap->iv_max_aid);
179178354Ssam			vap->iv_max_aid = 0;
180178354Ssam		}
181138568Ssam	}
182138568Ssam
183178354Ssam	ieee80211_reset_bss(vap);
184118887Ssam
185178354Ssam	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
186116742Ssam}
187116742Ssam
188116742Ssamvoid
189178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap)
190116742Ssam{
191178354Ssam	struct ieee80211com *ic = vap->iv_ic;
192116742Ssam
193178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
194178354Ssam	if (vap->iv_bss != NULL) {
195178354Ssam		ieee80211_free_node(vap->iv_bss);
196178354Ssam		vap->iv_bss = NULL;
197138568Ssam	}
198178354Ssam	if (vap->iv_aid_bitmap != NULL) {
199186302Ssam		free(vap->iv_aid_bitmap, M_80211_NODE);
200178354Ssam		vap->iv_aid_bitmap = NULL;
201138568Ssam	}
202116742Ssam}
203116742Ssam
204138568Ssam/*
205138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
206138568Ssam */
207138568Ssam
208138568Ssamvoid
209148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
210138568Ssam{
211184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
212184277Ssam
213138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
214184277Ssam	ni->ni_inact_reload = vap->iv_inact_run;
215172062Ssam	ni->ni_inact = ni->ni_inact_reload;
216184277Ssam
217184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
218184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
219138568Ssam}
220138568Ssam
221138568Ssamvoid
222148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
223138568Ssam{
224184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
225184277Ssam
226138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
227184277Ssam	ni->ni_inact_reload = vap->iv_inact_auth;
228172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
229172062Ssam		ni->ni_inact = ni->ni_inact_reload;
230184277Ssam
231184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
232184277Ssam	    "%s: inact_reload %u inact %u", __func__,
233184277Ssam	    ni->ni_inact_reload, ni->ni_inact);
234138568Ssam}
235138568Ssam
236116742Ssam/*
237183251Ssam * Fix tx parameters for a node according to ``association state''.
238183251Ssam */
239193966Ssamvoid
240193966Ssamieee80211_node_setuptxparms(struct ieee80211_node *ni)
241183251Ssam{
242183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
243187898Ssam	enum ieee80211_phymode mode;
244183251Ssam
245183251Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
246183251Ssam		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
247187898Ssam			mode = IEEE80211_MODE_11NA;
248183251Ssam		else
249187898Ssam			mode = IEEE80211_MODE_11NG;
250183251Ssam	} else {				/* legacy rate handling */
251187898Ssam		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
252187898Ssam			mode = IEEE80211_MODE_STURBO_A;
253188782Ssam		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
254188782Ssam			mode = IEEE80211_MODE_HALF;
255188782Ssam		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
256188782Ssam			mode = IEEE80211_MODE_QUARTER;
257191015Ssam		/* NB: 108A should be handled as 11a */
258187898Ssam		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
259187898Ssam			mode = IEEE80211_MODE_11A;
260191015Ssam		else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
261191015Ssam		    (ni->ni_flags & IEEE80211_NODE_ERP))
262187898Ssam			mode = IEEE80211_MODE_11G;
263183251Ssam		else
264187898Ssam			mode = IEEE80211_MODE_11B;
265183251Ssam	}
266187898Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
267183251Ssam}
268183251Ssam
269183251Ssam/*
270138568Ssam * Set/change the channel.  The rate set is also updated as
271138568Ssam * to insure a consistent view by drivers.
272178354Ssam * XXX should be private but hostap needs it to deal with CSA
273138568Ssam */
274178354Ssamvoid
275178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni,
276178354Ssam	struct ieee80211_channel *chan)
277138568Ssam{
278178354Ssam	struct ieee80211com *ic = ni->ni_ic;
279183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
280183251Ssam	enum ieee80211_phymode mode;
281170530Ssam
282178354Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
283178354Ssam
284138568Ssam	ni->ni_chan = chan;
285183251Ssam	mode = ieee80211_chan2mode(chan);
286170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
287170530Ssam		/*
288170530Ssam		 * XXX Gotta be careful here; the rate set returned by
289170530Ssam		 * ieee80211_get_suprates is actually any HT rate
290170530Ssam		 * set so blindly copying it will be bad.  We must
291170530Ssam		 * install the legacy rate est in ni_rates and the
292170530Ssam		 * HT rate set in ni_htrates.
293170530Ssam		 */
294170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
295183251Ssam		/*
296183251Ssam		 * Setup bss tx parameters based on operating mode.  We
297183251Ssam		 * use legacy rates when operating in a mixed HT+non-HT bss
298183251Ssam		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
299183251Ssam		 */
300183251Ssam		if (mode == IEEE80211_MODE_11NA &&
301193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
302183251Ssam			mode = IEEE80211_MODE_11A;
303183251Ssam		else if (mode == IEEE80211_MODE_11NG &&
304193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
305183251Ssam			mode = IEEE80211_MODE_11G;
306183251Ssam		if (mode == IEEE80211_MODE_11G &&
307183251Ssam		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
308183251Ssam			mode = IEEE80211_MODE_11B;
309170530Ssam	}
310183251Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
311165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
312138568Ssam}
313138568Ssam
314141658Ssamstatic __inline void
315141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
316141658Ssam{
317141658Ssam	/* propagate useful state */
318141658Ssam	nbss->ni_authmode = obss->ni_authmode;
319141658Ssam	nbss->ni_txpower = obss->ni_txpower;
320141658Ssam	nbss->ni_vlan = obss->ni_vlan;
321141658Ssam	/* XXX statistics? */
322178354Ssam	/* XXX legacy WDS bssid? */
323141658Ssam}
324141658Ssam
325116742Ssamvoid
326178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
327116742Ssam{
328178354Ssam	struct ieee80211com *ic = vap->iv_ic;
329116742Ssam	struct ieee80211_node *ni;
330116742Ssam
331178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
332195618Srpaulo		"%s: creating %s on channel %u\n", __func__,
333195618Srpaulo		ieee80211_opmode_name[vap->iv_opmode],
334178354Ssam		ieee80211_chan2ieee(ic, chan));
335138568Ssam
336178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
337140753Ssam	if (ni == NULL) {
338140753Ssam		/* XXX recovery? */
339138568Ssam		return;
340138568Ssam	}
341178354Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
342178354Ssam	ni->ni_esslen = vap->iv_des_ssid[0].len;
343178354Ssam	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
344178354Ssam	if (vap->iv_bss != NULL)
345178354Ssam		copy_bss(ni, vap->iv_bss);
346148843Ssam	ni->ni_intval = ic->ic_bintval;
347178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
348116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
349116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
350116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
351116742Ssam		ni->ni_fhindex = 1;
352116742Ssam	}
353178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
354178354Ssam		vap->iv_flags |= IEEE80211_F_SIBSS;
355138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
356178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
357178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
358167282Ssam		else {
359167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
360167282Ssam			/* clear group bit, add local bit */
361167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
362167282Ssam		}
363178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
364178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
365178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
366153403Ssam		else
367186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
368186904Ssam		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
369186904Ssam#endif
370153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
371195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
372195618Srpaulo	} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
373195618Srpaulo		ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
374195618Srpaulo		memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
375195618Srpaulo#endif
376138568Ssam	}
377138568Ssam	/*
378138568Ssam	 * Fix the channel and related attributes.
379138568Ssam	 */
380178354Ssam	/* clear DFS CAC state on previous channel */
381178354Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
382178354Ssam	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
383178354Ssam	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
384178354Ssam		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
385170530Ssam	ic->ic_bsschan = chan;
386178354Ssam	ieee80211_node_set_chan(ni, chan);
387170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
388138568Ssam	/*
389178354Ssam	 * Do mode-specific setup.
390138568Ssam	 */
391170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
392170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
393170530Ssam			/*
394178354Ssam			 * Use a mixed 11b/11g basic rate set.
395170530Ssam			 */
396178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
397178354Ssam			    IEEE80211_MODE_11G);
398178354Ssam			if (vap->iv_flags & IEEE80211_F_PUREG) {
399178354Ssam				/*
400178354Ssam				 * Also mark OFDM rates basic so 11b
401178354Ssam				 * stations do not join (WiFi compliance).
402178354Ssam				 */
403178354Ssam				ieee80211_addbasicrates(&ni->ni_rates,
404178354Ssam				    IEEE80211_MODE_11A);
405178354Ssam			}
406170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
407170530Ssam			/*
408170530Ssam			 * Force pure 11b rate set.
409170530Ssam			 */
410178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
411170530Ssam				IEEE80211_MODE_11B);
412170530Ssam		}
413170530Ssam	}
414138568Ssam
415170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
416116742Ssam}
417116742Ssam
418170530Ssam/*
419170530Ssam * Reset bss state on transition to the INIT state.
420170530Ssam * Clear any stations from the table (they have been
421170530Ssam * deauth'd) and reset the bss node (clears key, rate
422170530Ssam * etc. state).
423170530Ssam */
424138568Ssamvoid
425178354Ssamieee80211_reset_bss(struct ieee80211vap *vap)
426138568Ssam{
427178354Ssam	struct ieee80211com *ic = vap->iv_ic;
428138568Ssam	struct ieee80211_node *ni, *obss;
429138568Ssam
430178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
431178354Ssam	/* XXX multi-bss: wrong */
432170530Ssam	ieee80211_reset_erp(ic);
433140753Ssam
434178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
435138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
436178354Ssam	obss = vap->iv_bss;
437178354Ssam	vap->iv_bss = ieee80211_ref_node(ni);
438141658Ssam	if (obss != NULL) {
439141658Ssam		copy_bss(ni, obss);
440148843Ssam		ni->ni_intval = ic->ic_bintval;
441138568Ssam		ieee80211_free_node(obss);
442178354Ssam	} else
443178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
444138568Ssam}
445138568Ssam
446170530Ssamstatic int
447170530Ssammatch_ssid(const struct ieee80211_node *ni,
448170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
449170530Ssam{
450170530Ssam	int i;
451148432Ssam
452170530Ssam	for (i = 0; i < nssid; i++) {
453170530Ssam		if (ni->ni_esslen == ssids[i].len &&
454170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
455170530Ssam			return 1;
456170530Ssam	}
457170530Ssam	return 0;
458170530Ssam}
459170530Ssam
460170530Ssam/*
461170530Ssam * Test a node for suitability/compatibility.
462170530Ssam */
463127767Ssamstatic int
464178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
465127767Ssam{
466178354Ssam	struct ieee80211com *ic = ni->ni_ic;
467170530Ssam        uint8_t rate;
468170530Ssam
469170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
470170530Ssam		return 0;
471178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
472170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
473170530Ssam			return 0;
474170530Ssam	} else {
475170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
476170530Ssam			return 0;
477170530Ssam	}
478178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
479170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
480170530Ssam			return 0;
481170530Ssam	} else {
482170530Ssam		/* XXX does this mean privacy is supported or required? */
483170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
484170530Ssam			return 0;
485170530Ssam	}
486170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
487170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
488170530Ssam	if (rate & IEEE80211_RATE_BASIC)
489170530Ssam		return 0;
490178354Ssam	if (vap->iv_des_nssid != 0 &&
491178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
492170530Ssam		return 0;
493178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
494178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
495170530Ssam		return 0;
496170530Ssam	return 1;
497170530Ssam}
498170530Ssam
499170530Ssam#ifdef IEEE80211_DEBUG
500170530Ssam/*
501170530Ssam * Display node suitability/compatibility.
502170530Ssam */
503170530Ssamstatic void
504178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
505170530Ssam{
506178354Ssam	struct ieee80211com *ic = ni->ni_ic;
507170530Ssam        uint8_t rate;
508127767Ssam        int fail;
509127767Ssam
510127767Ssam	fail = 0;
511127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
512127767Ssam		fail |= 0x01;
513178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
514127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
515127767Ssam			fail |= 0x02;
516127767Ssam	} else {
517127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
518127767Ssam			fail |= 0x02;
519127767Ssam	}
520178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
521127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
522127767Ssam			fail |= 0x04;
523127767Ssam	} else {
524127767Ssam		/* XXX does this mean privacy is supported or required? */
525127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
526127767Ssam			fail |= 0x04;
527127767Ssam	}
528167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
529165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
530127767Ssam	if (rate & IEEE80211_RATE_BASIC)
531127767Ssam		fail |= 0x08;
532178354Ssam	if (vap->iv_des_nssid != 0 &&
533178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
534127767Ssam		fail |= 0x10;
535178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
536178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
537127767Ssam		fail |= 0x20;
538127767Ssam
539170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
540170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
541170530Ssam	printf(" %3d%c",
542170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
543170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
544170530Ssam	    fail & 0x08 ? '!' : ' ');
545170530Ssam	printf(" %4s%c",
546170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
547170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
548170530Ssam	    "????",
549170530Ssam	    fail & 0x02 ? '!' : ' ');
550170530Ssam	printf(" %3s%c ",
551170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
552170530Ssam	    fail & 0x04 ? '!' : ' ');
553170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
554170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
555138568Ssam}
556170530Ssam#endif /* IEEE80211_DEBUG */
557138568Ssam
558138568Ssam/*
559138568Ssam * Handle 802.11 ad hoc network merge.  The
560138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
561138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
562138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
563138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
564138568Ssam * sends beacons with the greatest TSF timestamp.
565138568Ssam *
566138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
567138568Ssam *
568138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
569138568Ssam */
570138568Ssamint
571148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
572138568Ssam{
573178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
574178354Ssam#ifdef IEEE80211_DEBUG
575148306Ssam	struct ieee80211com *ic = ni->ni_ic;
576178354Ssam#endif
577138568Ssam
578178354Ssam	if (ni == vap->iv_bss ||
579178354Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
580138568Ssam		/* unchanged, nothing to do */
581138568Ssam		return 0;
582138568Ssam	}
583178354Ssam	if (!check_bss(vap, ni)) {
584170530Ssam		/* capabilities mismatch */
585178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
586138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
587170530Ssam#ifdef IEEE80211_DEBUG
588178354Ssam		if (ieee80211_msg_assoc(vap))
589178354Ssam			check_bss_debug(vap, ni);
590170530Ssam#endif
591178354Ssam		vap->iv_stats.is_ibss_capmismatch++;
592138568Ssam		return 0;
593138568Ssam	}
594178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
595138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
596138568Ssam		ether_sprintf(ni->ni_bssid),
597138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
598138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
599138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
600138568Ssam	);
601170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
602138568Ssam}
603138568Ssam
604138568Ssam/*
605178354Ssam * Calculate HT channel promotion flags for all vaps.
606178354Ssam * This assumes ni_chan have been setup for each vap.
607173273Ssam */
608178354Ssamstatic int
609178354Ssamgethtadjustflags(struct ieee80211com *ic)
610178354Ssam{
611178354Ssam	struct ieee80211vap *vap;
612178354Ssam	int flags;
613178354Ssam
614178354Ssam	flags = 0;
615178354Ssam	/* XXX locking */
616178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
617178354Ssam		if (vap->iv_state < IEEE80211_S_RUN)
618178354Ssam			continue;
619178354Ssam		switch (vap->iv_opmode) {
620178354Ssam		case IEEE80211_M_WDS:
621178354Ssam		case IEEE80211_M_STA:
622178354Ssam		case IEEE80211_M_AHDEMO:
623178354Ssam		case IEEE80211_M_HOSTAP:
624178354Ssam		case IEEE80211_M_IBSS:
625195618Srpaulo		case IEEE80211_M_MBSS:
626178354Ssam			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
627178354Ssam			break;
628178354Ssam		default:
629178354Ssam			break;
630178354Ssam		}
631178354Ssam	}
632178354Ssam	return flags;
633178354Ssam}
634178354Ssam
635178354Ssam/*
636178354Ssam * Check if the current channel needs to change based on whether
637184303Ssam * any vap's are using HT20/HT40.  This is used to sync the state
638184303Ssam * of ic_curchan after a channel width change on a running vap.
639178354Ssam */
640173273Ssamvoid
641178354Ssamieee80211_sync_curchan(struct ieee80211com *ic)
642173273Ssam{
643178354Ssam	struct ieee80211_channel *c;
644178354Ssam
645178354Ssam	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
646178354Ssam	if (c != ic->ic_curchan) {
647178354Ssam		ic->ic_curchan = c;
648178354Ssam		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
649190532Ssam		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
650191746Sthompsa		IEEE80211_UNLOCK(ic);
651178354Ssam		ic->ic_set_channel(ic);
652192468Ssam		ieee80211_radiotap_chan_change(ic);
653191746Sthompsa		IEEE80211_LOCK(ic);
654178354Ssam	}
655178354Ssam}
656178354Ssam
657178354Ssam/*
658191746Sthompsa * Setup the current channel.  The request channel may be
659178354Ssam * promoted if other vap's are operating with HT20/HT40.
660178354Ssam */
661178354Ssamvoid
662191746Sthompsaieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
663178354Ssam{
664178354Ssam	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
665178354Ssam		int flags = gethtadjustflags(ic);
666178354Ssam		/*
667178354Ssam		 * Check for channel promotion required to support the
668178354Ssam		 * set of running vap's.  This assumes we are called
669178354Ssam		 * after ni_chan is setup for each vap.
670178354Ssam		 */
671193655Ssam		/* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
672178354Ssam		if (flags > ieee80211_htchanflags(c))
673178354Ssam			c = ieee80211_ht_adjust_channel(ic, c, flags);
674178354Ssam	}
675178354Ssam	ic->ic_bsschan = ic->ic_curchan = c;
676173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
677190532Ssam	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
678173273Ssam}
679173273Ssam
680173273Ssam/*
681191746Sthompsa * Change the current channel.  The channel change is guaranteed to have
682191746Sthompsa * happened before the next state change.
683191746Sthompsa */
684191746Sthompsavoid
685191746Sthompsaieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
686191746Sthompsa{
687191746Sthompsa	ieee80211_setupcurchan(ic, c);
688191746Sthompsa	ieee80211_runtask(ic, &ic->ic_chan_task);
689191746Sthompsa}
690191746Sthompsa
691191746Sthompsa/*
692138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
693138568Ssam * be passed in with a held reference.
694138568Ssam */
695170530Ssamstatic int
696170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
697138568Ssam{
698178354Ssam	struct ieee80211vap *vap = selbs->ni_vap;
699170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
700138568Ssam	struct ieee80211_node *obss;
701170530Ssam	int canreassoc;
702138568Ssam
703138568Ssam	/*
704138568Ssam	 * Committed to selbs, setup state.
705138568Ssam	 */
706178354Ssam	obss = vap->iv_bss;
707170530Ssam	/*
708170530Ssam	 * Check if old+new node have the same address in which
709170530Ssam	 * case we can reassociate when operating in sta mode.
710170530Ssam	 */
711170530Ssam	canreassoc = (obss != NULL &&
712178354Ssam		vap->iv_state == IEEE80211_S_RUN &&
713170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
714178354Ssam	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
715153352Ssam	if (obss != NULL) {
716153352Ssam		copy_bss(selbs, obss);
717188541Ssam		ieee80211_node_decref(obss);	/* iv_bss reference */
718188541Ssam		ieee80211_free_node(obss);	/* station table reference */
719178354Ssam		obss = NULL;		/* NB: guard against later use */
720153352Ssam	}
721165887Ssam
722138568Ssam	/*
723165887Ssam	 * Delete unusable rates; we've already checked
724165887Ssam	 * that the negotiated rate set is acceptable.
725165887Ssam	 */
726178354Ssam	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
727167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
728165887Ssam
729178354Ssam	ieee80211_setcurchan(ic, selbs->ni_chan);
730165887Ssam	/*
731138568Ssam	 * Set the erp state (mostly the slot time) to deal with
732138568Ssam	 * the auto-select case; this should be redundant if the
733138568Ssam	 * mode is locked.
734138568Ssam	 */
735138568Ssam	ieee80211_reset_erp(ic);
736178354Ssam	ieee80211_wme_initparams(vap);
737140753Ssam
738178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
739170530Ssam		if (canreassoc) {
740170530Ssam			/* Reassociate */
741178354Ssam			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
742170530Ssam		} else {
743170530Ssam			/*
744170530Ssam			 * Act as if we received a DEAUTH frame in case we
745170530Ssam			 * are invoked from the RUN state.  This will cause
746170530Ssam			 * us to try to re-authenticate if we are operating
747170530Ssam			 * as a station.
748170530Ssam			 */
749178354Ssam			ieee80211_new_state(vap, IEEE80211_S_AUTH,
750170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
751170530Ssam		}
752170530Ssam	} else
753178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
754138568Ssam	return 1;
755116742Ssam}
756116742Ssam
757170530Ssamint
758184274Ssamieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
759170530Ssam	const struct ieee80211_scan_entry *se)
760170530Ssam{
761178354Ssam	struct ieee80211com *ic = vap->iv_ic;
762170530Ssam	struct ieee80211_node *ni;
763170530Ssam
764178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
765170530Ssam	if (ni == NULL) {
766170530Ssam		/* XXX msg */
767170530Ssam		return 0;
768170530Ssam	}
769170530Ssam	/*
770170530Ssam	 * Expand scan state into node's format.
771170530Ssam	 * XXX may not need all this stuff
772170530Ssam	 */
773170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
774170530Ssam	ni->ni_esslen = se->se_ssid[1];
775170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
776170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
777170530Ssam	ni->ni_intval = se->se_intval;
778170530Ssam	ni->ni_capinfo = se->se_capinfo;
779184274Ssam	ni->ni_chan = chan;
780170530Ssam	ni->ni_timoff = se->se_timoff;
781170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
782170530Ssam	ni->ni_fhindex = se->se_fhindex;
783170530Ssam	ni->ni_erp = se->se_erp;
784178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
785170530Ssam	ni->ni_noise = se->se_noise;
786186870Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
787186870Ssam		/* NB: only infrastructure mode requires an associd */
788186870Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
789186870Ssam	}
790178354Ssam
791178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
792178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
793190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
794178354Ssam		if (ni->ni_ies.ath_ie != NULL)
795178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
796190391Ssam#endif
797178354Ssam		if (ni->ni_ies.htcap_ie != NULL)
798178354Ssam			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
799178354Ssam		if (ni->ni_ies.htinfo_ie != NULL)
800178354Ssam			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
801195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
802195618Srpaulo		if (ni->ni_ies.meshid_ie != NULL)
803195618Srpaulo			ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
804195618Srpaulo#endif
805186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
806186904Ssam		if (ni->ni_ies.tdma_ie != NULL)
807186904Ssam			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
808186904Ssam#endif
809173864Ssam	}
810170530Ssam
811178354Ssam	vap->iv_dtim_period = se->se_dtimperiod;
812178354Ssam	vap->iv_dtim_count = 0;
813170530Ssam
814170530Ssam	/* NB: must be after ni_chan is setup */
815170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
816170530Ssam		IEEE80211_F_DOSORT);
817184279Ssam	if (ieee80211_iserp_rateset(&ni->ni_rates))
818184279Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
819193966Ssam	ieee80211_node_setuptxparms(ni);
820170530Ssam
821170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
822170530Ssam}
823170530Ssam
824138568Ssam/*
825138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
826138568Ssam * be passed in with a held reference.
827138568Ssam */
828138568Ssamvoid
829178354Ssamieee80211_sta_leave(struct ieee80211_node *ni)
830138568Ssam{
831178354Ssam	struct ieee80211com *ic = ni->ni_ic;
832178354Ssam
833138568Ssam	ic->ic_node_cleanup(ni);
834178354Ssam	ieee80211_notify_node_leave(ni);
835138568Ssam}
836138568Ssam
837178354Ssam/*
838178354Ssam * Send a deauthenticate frame and drop the station.
839178354Ssam */
840178354Ssamvoid
841178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason)
842178354Ssam{
843178354Ssam	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
844178354Ssam	ieee80211_ref_node(ni);
845178354Ssam	if (ni->ni_associd != 0)
846178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
847178354Ssam	ieee80211_node_leave(ni);
848178354Ssam	ieee80211_free_node(ni);
849178354Ssam}
850178354Ssam
851116742Ssamstatic struct ieee80211_node *
852179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
853116742Ssam{
854127768Ssam	struct ieee80211_node *ni;
855138568Ssam
856186302Ssam	ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
857127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
858127768Ssam	return ni;
859116742Ssam}
860116742Ssam
861138568Ssam/*
862178354Ssam * Initialize an ie blob with the specified data.  If previous
863178354Ssam * data exists re-use the data block.  As a side effect we clear
864178354Ssam * all references to specific ie's; the caller is required to
865178354Ssam * recalculate them.
866178354Ssam */
867178354Ssamint
868178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
869178354Ssam{
870178354Ssam	/* NB: assumes data+len are the last fields */
871178354Ssam	memset(ies, 0, offsetof(struct ieee80211_ies, data));
872178354Ssam	if (ies->data != NULL && ies->len != len) {
873178354Ssam		/* data size changed */
874186302Ssam		free(ies->data, M_80211_NODE_IE);
875178354Ssam		ies->data = NULL;
876178354Ssam	}
877178354Ssam	if (ies->data == NULL) {
878186302Ssam		ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
879178354Ssam		if (ies->data == NULL) {
880178354Ssam			ies->len = 0;
881178354Ssam			/* NB: pointers have already been zero'd above */
882178354Ssam			return 0;
883178354Ssam		}
884178354Ssam	}
885178354Ssam	memcpy(ies->data, data, len);
886178354Ssam	ies->len = len;
887178354Ssam	return 1;
888178354Ssam}
889178354Ssam
890178354Ssam/*
891178354Ssam * Reclaim storage for an ie blob.
892178354Ssam */
893178354Ssamvoid
894178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies)
895178354Ssam{
896178354Ssam	if (ies->data != NULL)
897186302Ssam		free(ies->data, M_80211_NODE_IE);
898178354Ssam}
899178354Ssam
900178354Ssam/*
901178354Ssam * Expand an ie blob data contents and to fillin individual
902178354Ssam * ie pointers.  The data blob is assumed to be well-formed;
903178354Ssam * we don't do any validity checking of ie lengths.
904178354Ssam */
905178354Ssamvoid
906178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies)
907178354Ssam{
908178354Ssam	uint8_t *ie;
909178354Ssam	int ielen;
910178354Ssam
911178354Ssam	ie = ies->data;
912178354Ssam	ielen = ies->len;
913178354Ssam	while (ielen > 0) {
914178354Ssam		switch (ie[0]) {
915178354Ssam		case IEEE80211_ELEMID_VENDOR:
916178354Ssam			if (iswpaoui(ie))
917178354Ssam				ies->wpa_ie = ie;
918178354Ssam			else if (iswmeoui(ie))
919178354Ssam				ies->wme_ie = ie;
920190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
921178354Ssam			else if (isatherosoui(ie))
922178354Ssam				ies->ath_ie = ie;
923190391Ssam#endif
924186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
925186904Ssam			else if (istdmaoui(ie))
926186904Ssam				ies->tdma_ie = ie;
927186904Ssam#endif
928178354Ssam			break;
929178354Ssam		case IEEE80211_ELEMID_RSN:
930178354Ssam			ies->rsn_ie = ie;
931178354Ssam			break;
932178354Ssam		case IEEE80211_ELEMID_HTCAP:
933178354Ssam			ies->htcap_ie = ie;
934178354Ssam			break;
935195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
936195618Srpaulo		case IEEE80211_ELEMID_MESHID:
937195618Srpaulo			ies->meshid_ie = ie;
938195618Srpaulo			break;
939195618Srpaulo#endif
940178354Ssam		}
941178354Ssam		ielen -= 2 + ie[1];
942178354Ssam		ie += 2 + ie[1];
943178354Ssam	}
944178354Ssam}
945178354Ssam
946178354Ssam/*
947138568Ssam * Reclaim any resources in a node and reset any critical
948138568Ssam * state.  Typically nodes are free'd immediately after,
949138568Ssam * but in some cases the storage may be reused so we need
950138568Ssam * to insure consistent state (should probably fix that).
951138568Ssam */
952116742Ssamstatic void
953138568Ssamnode_cleanup(struct ieee80211_node *ni)
954116742Ssam{
955138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
956178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
957195379Ssam	struct ieee80211com *ic = ni->ni_ic;
958170530Ssam	int i;
959138568Ssam
960138568Ssam	/* NB: preserve ni_table */
961138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
962178354Ssam		if (vap->iv_opmode != IEEE80211_M_STA)
963178354Ssam			vap->iv_ps_sta--;
964138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
965178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
966178354Ssam		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
967138568Ssam	}
968147788Ssam	/*
969173273Ssam	 * Cleanup any HT-related state.
970173273Ssam	 */
971173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
972173273Ssam		ieee80211_ht_node_cleanup(ni);
973190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
974190579Ssam	else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
975190579Ssam		ieee80211_ff_node_cleanup(ni);
976190579Ssam#endif
977195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
978173273Ssam	/*
979195618Srpaulo	 * Cleanup any mesh-related state.
980195618Srpaulo	 */
981195618Srpaulo	if (vap->iv_opmode == IEEE80211_M_MBSS)
982195618Srpaulo		ieee80211_mesh_node_cleanup(ni);
983195618Srpaulo#endif
984195618Srpaulo	/*
985195379Ssam	 * Clear any staging queue entries.
986195379Ssam	 */
987195379Ssam	ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
988195379Ssam
989195379Ssam	/*
990147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
991147788Ssam	 * has happened.  This is probably not needed as the node
992147788Ssam	 * should always be removed from the table so not found but
993147788Ssam	 * do it just in case.
994186099Ssam	 * Likewise clear the ASSOCID flag as these flags are intended
995186099Ssam	 * to be managed in tandem.
996147788Ssam	 */
997186099Ssam	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
998138568Ssam
999138568Ssam	/*
1000138568Ssam	 * Drain power save queue and, if needed, clear TIM.
1001138568Ssam	 */
1002184288Ssam	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1003178354Ssam		vap->iv_set_tim(ni, 0);
1004138568Ssam
1005138568Ssam	ni->ni_associd = 0;
1006138568Ssam	if (ni->ni_challenge != NULL) {
1007186302Ssam		free(ni->ni_challenge, M_80211_NODE);
1008138568Ssam		ni->ni_challenge = NULL;
1009138568Ssam	}
1010138568Ssam	/*
1011138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
1012138568Ssam	 * reusable during a re-auth/re-assoc state transition.
1013138568Ssam	 * If we remove these data they will not be recreated
1014138568Ssam	 * because they come from a probe-response or beacon frame
1015138568Ssam	 * which cannot be expected prior to the association-response.
1016138568Ssam	 * This should not be an issue when operating in other modes
1017138568Ssam	 * as stations leaving always go through a full state transition
1018138568Ssam	 * which will rebuild this state.
1019138568Ssam	 *
1020138568Ssam	 * XXX does this leave us open to inheriting old state?
1021138568Ssam	 */
1022138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
1023138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
1024138568Ssam			m_freem(ni->ni_rxfrag[i]);
1025138568Ssam			ni->ni_rxfrag[i] = NULL;
1026138568Ssam		}
1027148863Ssam	/*
1028148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
1029148863Ssam	 */
1030148863Ssam	ieee80211_node_delucastkey(ni);
1031138568Ssam#undef N
1032116742Ssam}
1033116742Ssam
1034116742Ssamstatic void
1035138568Ssamnode_free(struct ieee80211_node *ni)
1036116742Ssam{
1037138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1038138568Ssam
1039206358Srpaulo	ieee80211_ratectl_node_deinit(ni);
1040138568Ssam	ic->ic_node_cleanup(ni);
1041178354Ssam	ieee80211_ies_cleanup(&ni->ni_ies);
1042184288Ssam	ieee80211_psq_cleanup(&ni->ni_psq);
1043186302Ssam	free(ni, M_80211_NODE);
1044116742Ssam}
1045116742Ssam
1046178354Ssamstatic void
1047178354Ssamnode_age(struct ieee80211_node *ni)
1048178354Ssam{
1049178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1050184303Ssam
1051184303Ssam	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
1052184303Ssam
1053178354Ssam	/*
1054178354Ssam	 * Age frames on the power save queue.
1055178354Ssam	 */
1056184288Ssam	if (ieee80211_node_psq_age(ni) != 0 &&
1057184288Ssam	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1058178354Ssam		vap->iv_set_tim(ni, 0);
1059178354Ssam	/*
1060178354Ssam	 * Age out HT resources (e.g. frames on the
1061178354Ssam	 * A-MPDU reorder queues).
1062178354Ssam	 */
1063178354Ssam	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1064178354Ssam		ieee80211_ht_node_age(ni);
1065178354Ssam}
1066178354Ssam
1067170530Ssamstatic int8_t
1068138568Ssamnode_getrssi(const struct ieee80211_node *ni)
1069120104Ssam{
1070178354Ssam	uint32_t avgrssi = ni->ni_avgrssi;
1071178354Ssam	int32_t rssi;
1072178354Ssam
1073178354Ssam	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1074178354Ssam		return 0;
1075178354Ssam	rssi = IEEE80211_RSSI_GET(avgrssi);
1076178354Ssam	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1077120104Ssam}
1078120104Ssam
1079116742Ssamstatic void
1080170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1081170530Ssam{
1082178354Ssam	*rssi = node_getrssi(ni);
1083170530Ssam	*noise = ni->ni_noise;
1084170530Ssam}
1085170530Ssam
1086170530Ssamstatic void
1087178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni,
1088178354Ssam	struct ieee80211_mimo_info *info)
1089116742Ssam{
1090178354Ssam	/* XXX zero data? */
1091178354Ssam}
1092178354Ssam
1093178354Ssamstruct ieee80211_node *
1094178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt,
1095178354Ssam	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1096178354Ssam{
1097138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1098178354Ssam	struct ieee80211_node *ni;
1099116742Ssam	int hash;
1100116742Ssam
1101179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1102178354Ssam	if (ni == NULL) {
1103178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1104178354Ssam		return NULL;
1105178354Ssam	}
1106178354Ssam
1107178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1108140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1109138568Ssam		ether_sprintf(macaddr), nt->nt_name);
1110138568Ssam
1111116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1112195618Srpaulo	hash = IEEE80211_NODE_HASH(ic, macaddr);
1113138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
1114138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
1115138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
1116138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
1117183251Ssam	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1118178354Ssam	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1119178354Ssam	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1120139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
1121139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1122170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
1123184288Ssam	ieee80211_psq_init(&ni->ni_psq, "unknown");
1124195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
1125195618Srpaulo	if (vap->iv_opmode == IEEE80211_M_MBSS)
1126195618Srpaulo		ieee80211_mesh_node_init(vap, ni);
1127195618Srpaulo#endif
1128138568Ssam	IEEE80211_NODE_LOCK(nt);
1129138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1130138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1131138568Ssam	ni->ni_table = nt;
1132178354Ssam	ni->ni_vap = vap;
1133138568Ssam	ni->ni_ic = ic;
1134138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1135116742Ssam
1136184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1137184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1138184277Ssam
1139116742Ssam	return ni;
1140116742Ssam}
1141116742Ssam
1142148777Ssam/*
1143148777Ssam * Craft a temporary node suitable for sending a management frame
1144148777Ssam * to the specified station.  We craft only as much state as we
1145148777Ssam * need to do the work since the node will be immediately reclaimed
1146148777Ssam * once the send completes.
1147148777Ssam */
1148116742Ssamstruct ieee80211_node *
1149178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1150178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1151148777Ssam{
1152178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1153148777Ssam	struct ieee80211_node *ni;
1154148777Ssam
1155179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1156148777Ssam	if (ni != NULL) {
1157183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1158183259Ssam
1159178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1160148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1161148777Ssam
1162178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1163178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1164178354Ssam		ni->ni_vap = vap;
1165178354Ssam
1166148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1167183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1168148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1169148777Ssam		/* NB: required by ieee80211_fix_rate */
1170183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1171178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1172148777Ssam			IEEE80211_KEYIX_NONE);
1173183259Ssam		ni->ni_txpower = bss->ni_txpower;
1174148777Ssam		/* XXX optimize away */
1175184288Ssam		ieee80211_psq_init(&ni->ni_psq, "unknown");
1176148777Ssam	} else {
1177148777Ssam		/* XXX msg */
1178178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1179148777Ssam	}
1180148777Ssam	return ni;
1181148777Ssam}
1182148777Ssam
1183148777Ssamstruct ieee80211_node *
1184178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1185178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1186116742Ssam{
1187178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1188138568Ssam	struct ieee80211_node *ni;
1189138568Ssam
1190178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1191116742Ssam	if (ni != NULL) {
1192183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1193127770Ssam		/*
1194178354Ssam		 * Inherit from iv_bss.
1195127770Ssam		 */
1196183259Ssam		copy_bss(ni, bss);
1197183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1198183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1199178354Ssam	}
1200116742Ssam	return ni;
1201116742Ssam}
1202116742Ssam
1203178354Ssam/*
1204178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1205178354Ssam * not associate so we just create create a new node and
1206178354Ssam * simulate an association.  The caller is responsible for
1207178354Ssam * installing the node as the bss node and handling any further
1208178354Ssam * setup work like authorizing the port.
1209178354Ssam */
1210178354Ssamstruct ieee80211_node *
1211178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1212178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1213178354Ssam{
1214178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1215178354Ssam	struct ieee80211_node *ni;
1216178354Ssam
1217178354Ssam	/* XXX check if node already in sta table? */
1218178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1219178354Ssam	if (ni != NULL) {
1220178354Ssam		ni->ni_wdsvap = vap;
1221178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1222178354Ssam		/*
1223178354Ssam		 * Inherit any manually configured settings.
1224178354Ssam		 */
1225183259Ssam		copy_bss(ni, vap->iv_bss);
1226178354Ssam		ieee80211_node_set_chan(ni, chan);
1227178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1228178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1229178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1230178354Ssam		/* NB: no associd for peer */
1231178354Ssam		/*
1232178354Ssam		 * There are no management frames to use to
1233178354Ssam		 * discover neighbor capabilities, so blindly
1234178354Ssam		 * propagate the local configuration.
1235178354Ssam		 */
1236178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1237178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1238190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1239178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1240178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1241190391Ssam#endif
1242178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1243193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1244178354Ssam			/*
1245178354Ssam			 * Device is HT-capable and HT is enabled for
1246178354Ssam			 * the vap; setup HT operation.  On return
1247178354Ssam			 * ni_chan will be adjusted to an HT channel.
1248178354Ssam			 */
1249178354Ssam			ieee80211_ht_wds_init(ni);
1250178354Ssam		} else {
1251178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1252178354Ssam			/*
1253178354Ssam			 * Force a legacy channel to be used.
1254178354Ssam			 */
1255178354Ssam			c = ieee80211_find_channel(ic,
1256178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1257178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1258178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1259178354Ssam			ni->ni_chan = c;
1260178354Ssam		}
1261178354Ssam	}
1262178354Ssam	return ni;
1263178354Ssam}
1264178354Ssam
1265178354Ssamstruct ieee80211_node *
1266138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1267178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1268178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1269138568Ssam#else
1270178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1271178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1272138568Ssam#endif
1273116742Ssam{
1274116742Ssam	struct ieee80211_node *ni;
1275116742Ssam	int hash;
1276116742Ssam
1277138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1278127772Ssam
1279195618Srpaulo	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1280138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1281116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1282138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1283138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1284178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1285140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1286140766Ssam			    func, line,
1287140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1288140766Ssam			    ieee80211_node_refcnt(ni));
1289138568Ssam#endif
1290127772Ssam			return ni;
1291116742Ssam		}
1292116742Ssam	}
1293127772Ssam	return NULL;
1294127772Ssam}
1295178354Ssam
1296178354Ssamstruct ieee80211_node *
1297138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1298178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1299178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1300178354Ssam#else
1301178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1302178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1303138568Ssam#endif
1304178354Ssam{
1305178354Ssam	struct ieee80211_node *ni;
1306127772Ssam
1307178354Ssam	IEEE80211_NODE_LOCK(nt);
1308178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1309178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1310178354Ssam	return ni;
1311178354Ssam}
1312178354Ssam
1313127772Ssamstruct ieee80211_node *
1314138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1315178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1316178354Ssam	const struct ieee80211vap *vap,
1317178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1318138568Ssam#else
1319178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1320178354Ssam	const struct ieee80211vap *vap,
1321178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1322138568Ssam#endif
1323127772Ssam{
1324127772Ssam	struct ieee80211_node *ni;
1325178354Ssam	int hash;
1326127772Ssam
1327178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1328178354Ssam
1329195618Srpaulo	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1330178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1331178354Ssam		if (ni->ni_vap == vap &&
1332178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1333178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1334178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1335178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1336178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1337178354Ssam			    func, line,
1338178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1339178354Ssam			    ieee80211_node_refcnt(ni));
1340178354Ssam#endif
1341178354Ssam			return ni;
1342178354Ssam		}
1343178354Ssam	}
1344178354Ssam	return NULL;
1345178354Ssam}
1346178354Ssam
1347178354Ssamstruct ieee80211_node *
1348178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1349178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1350178354Ssam	const struct ieee80211vap *vap,
1351178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1352178354Ssam#else
1353178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1354178354Ssam	const struct ieee80211vap *vap,
1355178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1356178354Ssam#endif
1357178354Ssam{
1358178354Ssam	struct ieee80211_node *ni;
1359178354Ssam
1360138568Ssam	IEEE80211_NODE_LOCK(nt);
1361178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1362138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1363116742Ssam	return ni;
1364116742Ssam}
1365116742Ssam
1366116742Ssam/*
1367138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1368138568Ssam * Note that for the driver's benefit we we treat this like
1369138568Ssam * an association so the driver has an opportunity to setup
1370138568Ssam * it's private state.
1371138568Ssam */
1372138568Ssamstruct ieee80211_node *
1373178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1374170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1375138568Ssam{
1376138568Ssam	struct ieee80211_node *ni;
1377138568Ssam
1378178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1379153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1380178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1381138568Ssam	if (ni != NULL) {
1382178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1383178354Ssam
1384138568Ssam		/* XXX no rate negotiation; just dup */
1385178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1386188869Ssam		if (ieee80211_iserp_rateset(&ni->ni_rates))
1387188869Ssam			ni->ni_flags |= IEEE80211_NODE_ERP;
1388178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1389153404Ssam			/*
1390170530Ssam			 * In adhoc demo mode there are no management
1391170530Ssam			 * frames to use to discover neighbor capabilities,
1392170530Ssam			 * so blindly propagate the local configuration
1393170530Ssam			 * so we can do interesting things (e.g. use
1394170530Ssam			 * WME to disable ACK's).
1395153404Ssam			 */
1396178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1397153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1398190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1399178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1400170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1401190391Ssam#endif
1402153404Ssam		}
1403193966Ssam		ieee80211_node_setuptxparms(ni);
1404178354Ssam		if (ic->ic_newassoc != NULL)
1405178354Ssam			ic->ic_newassoc(ni, 1);
1406170530Ssam		/* XXX not right for 802.1x/WPA */
1407170530Ssam		ieee80211_node_authorize(ni);
1408138568Ssam	}
1409138568Ssam	return ni;
1410138568Ssam}
1411138568Ssam
1412148936Ssamvoid
1413153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1414153073Ssam	const struct ieee80211_frame *wh,
1415153073Ssam	const struct ieee80211_scanparams *sp)
1416153073Ssam{
1417153073Ssam	ni->ni_esslen = sp->ssid[1];
1418153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1419153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1420153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1421153073Ssam	ni->ni_intval = sp->bintval;
1422153073Ssam	ni->ni_capinfo = sp->capinfo;
1423153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1424153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1425153073Ssam	ni->ni_fhindex = sp->fhindex;
1426153073Ssam	ni->ni_erp = sp->erp;
1427153073Ssam	ni->ni_timoff = sp->timoff;
1428195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
1429195618Srpaulo	if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1430195618Srpaulo		ieee80211_mesh_init_neighbor(ni, wh, sp);
1431195618Srpaulo#endif
1432178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1433178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1434186659Ssam		if (ni->ni_ies.wme_ie != NULL)
1435186659Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1436186659Ssam		else
1437186659Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1438190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1439178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1440178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1441190391Ssam#endif
1442178354Ssam	}
1443178354Ssam
1444153073Ssam	/* NB: must be after ni_chan is setup */
1445165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1446165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1447165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1448153073Ssam}
1449153073Ssam
1450148936Ssam/*
1451148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1452148936Ssam * or probe response frame.  Note that for the driver's
1453148936Ssam * benefit we we treat this like an association so the
1454148936Ssam * driver has an opportunity to setup it's private state.
1455148936Ssam */
1456148936Ssamstruct ieee80211_node *
1457178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1458148936Ssam	const struct ieee80211_frame *wh,
1459148936Ssam	const struct ieee80211_scanparams *sp)
1460148936Ssam{
1461148936Ssam	struct ieee80211_node *ni;
1462148936Ssam
1463178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1464153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1465178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1466148936Ssam	if (ni != NULL) {
1467178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1468178354Ssam
1469153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1470188869Ssam		if (ieee80211_iserp_rateset(&ni->ni_rates))
1471188869Ssam			ni->ni_flags |= IEEE80211_NODE_ERP;
1472193966Ssam		ieee80211_node_setuptxparms(ni);
1473148936Ssam		if (ic->ic_newassoc != NULL)
1474148936Ssam			ic->ic_newassoc(ni, 1);
1475148936Ssam		/* XXX not right for 802.1x/WPA */
1476148936Ssam		ieee80211_node_authorize(ni);
1477148936Ssam	}
1478148936Ssam	return ni;
1479148936Ssam}
1480148936Ssam
1481179220Ssam#define	IS_PROBEREQ(wh) \
1482179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1483179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1484179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1485179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1486179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1487170530Ssam
1488179220Ssamstatic __inline struct ieee80211_node *
1489179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1490179220Ssam    const struct ieee80211_frame_min *wh)
1491179220Ssam{
1492179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1493179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1494179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1495179220Ssam}
1496179220Ssam
1497138568Ssam/*
1498138568Ssam * Locate the node for sender, track state, and then pass the
1499179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1500179220Ssam * we can return NULL if the sender is not in the table.
1501138568Ssam */
1502138568Ssamstruct ieee80211_node *
1503138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1504138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1505138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1506138568Ssam#else
1507138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1508138568Ssam	const struct ieee80211_frame_min *wh)
1509138568Ssam#endif
1510138568Ssam{
1511138568Ssam	struct ieee80211_node_table *nt;
1512138568Ssam	struct ieee80211_node *ni;
1513138568Ssam
1514170530Ssam	nt = &ic->ic_sta;
1515138568Ssam	IEEE80211_NODE_LOCK(nt);
1516179220Ssam	ni = _find_rxnode(nt, wh);
1517138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1518138568Ssam
1519148863Ssam	return ni;
1520148863Ssam}
1521148863Ssam
1522148863Ssam/*
1523148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1524148863Ssam * key index as a hint to locate the node in the key
1525148863Ssam * mapping table.  If an entry is present at the key
1526148863Ssam * index we return it; otherwise do a normal lookup and
1527148863Ssam * update the mapping table if the station has a unicast
1528148863Ssam * key assigned to it.
1529148863Ssam */
1530148863Ssamstruct ieee80211_node *
1531148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1532148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1533148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1534148863Ssam	const char *func, int line)
1535148863Ssam#else
1536148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1537148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1538148863Ssam#endif
1539148863Ssam{
1540148863Ssam	struct ieee80211_node_table *nt;
1541148863Ssam	struct ieee80211_node *ni;
1542148863Ssam
1543170530Ssam	nt = &ic->ic_sta;
1544148863Ssam	IEEE80211_NODE_LOCK(nt);
1545148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1546148863Ssam		ni = nt->nt_keyixmap[keyix];
1547148863Ssam	else
1548148863Ssam		ni = NULL;
1549148863Ssam	if (ni == NULL) {
1550179220Ssam		ni = _find_rxnode(nt, wh);
1551178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1552148863Ssam			/*
1553148863Ssam			 * If the station has a unicast key cache slot
1554148863Ssam			 * assigned update the key->node mapping table.
1555148863Ssam			 */
1556148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1557148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1558148863Ssam			if (keyix < nt->nt_keyixmax &&
1559148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1560178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1561178354Ssam				    IEEE80211_MSG_NODE,
1562148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1563148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1564148863Ssam				    ieee80211_node_refcnt(ni)+1);
1565148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1566148863Ssam			}
1567148863Ssam		}
1568179220Ssam	} else {
1569179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1570179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1571179220Ssam		else
1572179220Ssam			ieee80211_ref_node(ni);
1573179220Ssam	}
1574148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1575148863Ssam
1576148863Ssam	return ni;
1577148863Ssam}
1578179220Ssam#undef IS_BCAST_PROBEREQ
1579179220Ssam#undef IS_PROBEREQ
1580138568Ssam
1581138568Ssam/*
1582127772Ssam * Return a reference to the appropriate node for sending
1583127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1584127772Ssam */
1585127772Ssamstruct ieee80211_node *
1586138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1587178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1588178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1589138568Ssam	const char *func, int line)
1590138568Ssam#else
1591178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1592178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1593138568Ssam#endif
1594127772Ssam{
1595178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1596127772Ssam	struct ieee80211_node *ni;
1597127772Ssam
1598127772Ssam	/*
1599127772Ssam	 * The destination address should be in the node table
1600148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1601148863Ssam	 * also optimize station mode operation, all frames go
1602148863Ssam	 * to the bss node.
1603127772Ssam	 */
1604127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1605138568Ssam	IEEE80211_NODE_LOCK(nt);
1606178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1607178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1608178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1609178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1610186099Ssam	else
1611178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1612138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1613138568Ssam
1614138568Ssam	if (ni == NULL) {
1615178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1616178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1617140497Ssam			/*
1618140497Ssam			 * In adhoc mode cons up a node for the destination.
1619140497Ssam			 * Note that we need an additional reference for the
1620178354Ssam			 * caller to be consistent with
1621178354Ssam			 * ieee80211_find_node_locked.
1622140497Ssam			 */
1623178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1624140497Ssam			if (ni != NULL)
1625140497Ssam				(void) ieee80211_ref_node(ni);
1626140497Ssam		} else {
1627178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1628178354Ssam			    "no node, discard frame (%s)", __func__);
1629178354Ssam			vap->iv_stats.is_tx_nonode++;
1630127772Ssam		}
1631127772Ssam	}
1632127772Ssam	return ni;
1633127772Ssam}
1634127772Ssam
1635116742Ssamstatic void
1636138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1637116742Ssam{
1638138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1639119150Ssam
1640179641Ssam	/*
1641179641Ssam	 * NB: careful about referencing the vap as it may be
1642179641Ssam	 * gone if the last reference was held by a driver.
1643179641Ssam	 * We know the com will always be present so it's safe
1644179641Ssam	 * to use ni_ic below to reclaim resources.
1645179641Ssam	 */
1646179641Ssam#if 0
1647178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1648140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1649140766Ssam		ether_sprintf(ni->ni_macaddr),
1650138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1651179641Ssam#endif
1652179641Ssam	if (ni->ni_associd != 0) {
1653179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1654179641Ssam		if (vap->iv_aid_bitmap != NULL)
1655179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1656179641Ssam	}
1657138568Ssam	if (nt != NULL) {
1658138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1659138568Ssam		LIST_REMOVE(ni, ni_hash);
1660138568Ssam	}
1661179641Ssam	ni->ni_ic->ic_node_free(ni);
1662116742Ssam}
1663116742Ssam
1664116742Ssamvoid
1665138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1666138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1667138568Ssam#else
1668138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1669138568Ssam#endif
1670116742Ssam{
1671138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1672119150Ssam
1673138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1674178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1675140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1676138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1677138568Ssam#endif
1678148863Ssam	if (nt != NULL) {
1679148863Ssam		IEEE80211_NODE_LOCK(nt);
1680148863Ssam		if (ieee80211_node_dectestref(ni)) {
1681148863Ssam			/*
1682148863Ssam			 * Last reference, reclaim state.
1683148863Ssam			 */
1684138568Ssam			_ieee80211_free_node(ni);
1685148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1686148863Ssam		    nt->nt_keyixmap != NULL) {
1687148863Ssam			ieee80211_keyix keyix;
1688148863Ssam			/*
1689148863Ssam			 * Check for a last reference in the key mapping table.
1690148863Ssam			 */
1691148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1692148863Ssam			if (keyix < nt->nt_keyixmax &&
1693148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1694178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1695178354Ssam				    IEEE80211_MSG_NODE,
1696148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1697148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1698148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1699148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1700148863Ssam				_ieee80211_free_node(ni);
1701148863Ssam			}
1702148863Ssam		}
1703148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1704148863Ssam	} else {
1705148863Ssam		if (ieee80211_node_dectestref(ni))
1706138568Ssam			_ieee80211_free_node(ni);
1707116742Ssam	}
1708116742Ssam}
1709116742Ssam
1710138568Ssam/*
1711148863Ssam * Reclaim a unicast key and clear any key cache state.
1712148863Ssam */
1713148863Ssamint
1714148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1715148863Ssam{
1716179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1717179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1718148863Ssam	struct ieee80211_node *nikey;
1719148863Ssam	ieee80211_keyix keyix;
1720148863Ssam	int isowned, status;
1721148863Ssam
1722148863Ssam	/*
1723148863Ssam	 * NB: We must beware of LOR here; deleting the key
1724148863Ssam	 * can cause the crypto layer to block traffic updates
1725148863Ssam	 * which can generate a LOR against the node table lock;
1726148863Ssam	 * grab it here and stash the key index for our use below.
1727148863Ssam	 *
1728148863Ssam	 * Must also beware of recursion on the node table lock.
1729148863Ssam	 * When called from node_cleanup we may already have
1730148863Ssam	 * the node table lock held.  Unfortunately there's no
1731148863Ssam	 * way to separate out this path so we must do this
1732148863Ssam	 * conditionally.
1733148863Ssam	 */
1734148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1735148863Ssam	if (!isowned)
1736148863Ssam		IEEE80211_NODE_LOCK(nt);
1737179641Ssam	nikey = NULL;
1738179641Ssam	status = 1;		/* NB: success */
1739186144Ssam	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1740179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1741179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1742179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1743179641Ssam			nikey = nt->nt_keyixmap[keyix];
1744201758Smbr			nt->nt_keyixmap[keyix] = NULL;
1745179641Ssam		}
1746179641Ssam	}
1747148863Ssam	if (!isowned)
1748178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1749148863Ssam
1750148863Ssam	if (nikey != NULL) {
1751148863Ssam		KASSERT(nikey == ni,
1752148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1753179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1754148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1755148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1756148863Ssam			ieee80211_node_refcnt(ni)-1);
1757148863Ssam		ieee80211_free_node(ni);
1758148863Ssam	}
1759148863Ssam	return status;
1760148863Ssam}
1761148863Ssam
1762148863Ssam/*
1763138568Ssam * Reclaim a node.  If this is the last reference count then
1764138568Ssam * do the normal free work.  Otherwise remove it from the node
1765138568Ssam * table and mark it gone by clearing the back-reference.
1766138568Ssam */
1767138568Ssamstatic void
1768138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1769116742Ssam{
1770148863Ssam	ieee80211_keyix keyix;
1771138568Ssam
1772148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1773148863Ssam
1774178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1775140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1776140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1777140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1778148863Ssam	/*
1779148863Ssam	 * Clear any entry in the unicast key mapping table.
1780148863Ssam	 * We need to do it here so rx lookups don't find it
1781148863Ssam	 * in the mapping table even if it's not in the hash
1782148863Ssam	 * table.  We cannot depend on the mapping table entry
1783148863Ssam	 * being cleared because the node may not be free'd.
1784148863Ssam	 */
1785148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1786148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1787148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1788178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1789188494Ssam			"%s: %p<%s> clear key map entry %u\n",
1790188494Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1791148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1792148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1793148863Ssam	}
1794138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1795138568Ssam		/*
1796138568Ssam		 * Other references are present, just remove the
1797138568Ssam		 * node from the table so it cannot be found.  When
1798138568Ssam		 * the references are dropped storage will be
1799140753Ssam		 * reclaimed.
1800138568Ssam		 */
1801138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1802138568Ssam		LIST_REMOVE(ni, ni_hash);
1803138568Ssam		ni->ni_table = NULL;		/* clear reference */
1804138568Ssam	} else
1805138568Ssam		_ieee80211_free_node(ni);
1806138568Ssam}
1807138568Ssam
1808178354Ssam/*
1809178354Ssam * Node table support.
1810178354Ssam */
1811178354Ssam
1812178354Ssamstatic void
1813178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1814178354Ssam	struct ieee80211_node_table *nt,
1815178354Ssam	const char *name, int inact, int keyixmax)
1816178354Ssam{
1817178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1818178354Ssam
1819178354Ssam	nt->nt_ic = ic;
1820178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1821178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1822178354Ssam	TAILQ_INIT(&nt->nt_node);
1823178354Ssam	nt->nt_name = name;
1824178354Ssam	nt->nt_scangen = 1;
1825178354Ssam	nt->nt_inact_init = inact;
1826178354Ssam	nt->nt_keyixmax = keyixmax;
1827178354Ssam	if (nt->nt_keyixmax > 0) {
1828186302Ssam		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1829184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1830178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1831178354Ssam		if (nt->nt_keyixmap == NULL)
1832178354Ssam			if_printf(ic->ic_ifp,
1833178354Ssam			    "Cannot allocate key index map with %u entries\n",
1834178354Ssam			    keyixmax);
1835178354Ssam	} else
1836178354Ssam		nt->nt_keyixmap = NULL;
1837178354Ssam}
1838178354Ssam
1839178354Ssamstatic void
1840178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1841178354Ssam	struct ieee80211vap *match)
1842178354Ssam{
1843178354Ssam	struct ieee80211_node *ni, *next;
1844178354Ssam
1845178354Ssam	IEEE80211_NODE_LOCK(nt);
1846178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1847178354Ssam		if (match != NULL && ni->ni_vap != match)
1848178354Ssam			continue;
1849178354Ssam		/* XXX can this happen?  if so need's work */
1850138568Ssam		if (ni->ni_associd != 0) {
1851178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1852178354Ssam
1853178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1854178354Ssam				vap->iv_auth->ia_node_leave(ni);
1855178354Ssam			if (vap->iv_aid_bitmap != NULL)
1856178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1857138568Ssam		}
1858178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1859138568Ssam		node_reclaim(nt, ni);
1860138568Ssam	}
1861178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1862178354Ssam		/*
1863178354Ssam		 * Make a separate pass to clear references to this vap
1864178354Ssam		 * held by DWDS entries.  They will not be matched above
1865178354Ssam		 * because ni_vap will point to the ap vap but we still
1866178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1867178354Ssam		 * and/or reset.
1868178354Ssam		 */
1869178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1870178354Ssam			if (ni->ni_wdsvap == match)
1871178354Ssam				ni->ni_wdsvap = NULL;
1872178354Ssam	}
1873178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1874116742Ssam}
1875116742Ssam
1876178354Ssamstatic void
1877178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1878178354Ssam{
1879178354Ssam	ieee80211_node_table_reset(nt, NULL);
1880178354Ssam	if (nt->nt_keyixmap != NULL) {
1881178354Ssam#ifdef DIAGNOSTIC
1882178354Ssam		/* XXX verify all entries are NULL */
1883178354Ssam		int i;
1884178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1885178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1886178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1887178354Ssam					nt->nt_name, i);
1888178354Ssam#endif
1889186302Ssam		free(nt->nt_keyixmap, M_80211_NODE);
1890178354Ssam		nt->nt_keyixmap = NULL;
1891178354Ssam	}
1892178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1893178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1894178354Ssam}
1895178354Ssam
1896120483Ssam/*
1897138568Ssam * Timeout inactive stations and do related housekeeping.
1898138568Ssam * Note that we cannot hold the node lock while sending a
1899138568Ssam * frame as this would lead to a LOR.  Instead we use a
1900138568Ssam * generation number to mark nodes that we've scanned and
1901138568Ssam * drop the lock and restart a scan if we have to time out
1902138568Ssam * a node.  Since we are single-threaded by virtue of
1903120483Ssam * controlling the inactivity timer we can be sure this will
1904120483Ssam * process each node only once.
1905120483Ssam */
1906138568Ssamstatic void
1907178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1908116742Ssam{
1909178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1910178354Ssam	struct ieee80211vap *vap;
1911120483Ssam	struct ieee80211_node *ni;
1912178354Ssam	int gen = 0;
1913116742Ssam
1914178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
1915154532Ssam	gen = ++nt->nt_scangen;
1916120483Ssamrestart:
1917138568Ssam	IEEE80211_NODE_LOCK(nt);
1918138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1919120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1920120483Ssam			continue;
1921120483Ssam		ni->ni_scangen = gen;
1922138568Ssam		/*
1923147788Ssam		 * Ignore entries for which have yet to receive an
1924147788Ssam		 * authentication frame.  These are transient and
1925147788Ssam		 * will be reclaimed when the last reference to them
1926147788Ssam		 * goes away (when frame xmits complete).
1927147788Ssam		 */
1928178354Ssam		vap = ni->ni_vap;
1929178354Ssam		/*
1930178354Ssam		 * Only process stations when in RUN state.  This
1931178354Ssam		 * insures, for example, that we don't timeout an
1932178354Ssam		 * inactive station during CAC.  Note that CSA state
1933178354Ssam		 * is actually handled in ieee80211_node_timeout as
1934178354Ssam		 * it applies to more than timeout processing.
1935178354Ssam		 */
1936178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
1937178354Ssam			continue;
1938178354Ssam		/* XXX can vap be NULL? */
1939178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1940178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
1941148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1942147788Ssam			continue;
1943147788Ssam		/*
1944138568Ssam		 * Free fragment if not needed anymore
1945138568Ssam		 * (last fragment older than 1s).
1946178354Ssam		 * XXX doesn't belong here, move to node_age
1947138568Ssam		 */
1948138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1949138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1950138568Ssam			m_freem(ni->ni_rxfrag[0]);
1951138568Ssam			ni->ni_rxfrag[0] = NULL;
1952138568Ssam		}
1953184277Ssam		if (ni->ni_inact > 0) {
1954172062Ssam			ni->ni_inact--;
1955184277Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1956184277Ssam			    "%s: inact %u inact_reload %u nrates %u",
1957184277Ssam			    __func__, ni->ni_inact, ni->ni_inact_reload,
1958184277Ssam			    ni->ni_rates.rs_nrates);
1959184277Ssam		}
1960140498Ssam		/*
1961140498Ssam		 * Special case ourself; we may be idle for extended periods
1962140498Ssam		 * of time and regardless reclaiming our state is wrong.
1963178354Ssam		 * XXX run ic_node_age
1964140498Ssam		 */
1965178354Ssam		if (ni == vap->iv_bss)
1966140498Ssam			continue;
1967178354Ssam		if (ni->ni_associd != 0 ||
1968178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1969178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1970119150Ssam			/*
1971178354Ssam			 * Age/drain resources held by the station.
1972138568Ssam			 */
1973178354Ssam			ic->ic_node_age(ni);
1974138568Ssam			/*
1975138568Ssam			 * Probe the station before time it out.  We
1976138568Ssam			 * send a null data frame which may not be
1977138568Ssam			 * universally supported by drivers (need it
1978138568Ssam			 * for ps-poll support so it should be...).
1979170530Ssam			 *
1980170530Ssam			 * XXX don't probe the station unless we've
1981170530Ssam			 *     received a frame from them (and have
1982170530Ssam			 *     some idea of the rates they are capable
1983170530Ssam			 *     of); this will get fixed more properly
1984170530Ssam			 *     soon with better handling of the rate set.
1985138568Ssam			 */
1986178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1987172062Ssam			    (0 < ni->ni_inact &&
1988178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
1989170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1990178354Ssam				IEEE80211_NOTE(vap,
1991148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1992148320Ssam				    ni, "%s",
1993148320Ssam				    "probe station due to inactivity");
1994148582Ssam				/*
1995148582Ssam				 * Grab a reference before unlocking the table
1996148582Ssam				 * so the node cannot be reclaimed before we
1997148582Ssam				 * send the frame. ieee80211_send_nulldata
1998148582Ssam				 * understands we've done this and reclaims the
1999148582Ssam				 * ref for us as needed.
2000148582Ssam				 */
2001148582Ssam				ieee80211_ref_node(ni);
2002138568Ssam				IEEE80211_NODE_UNLOCK(nt);
2003148301Ssam				ieee80211_send_nulldata(ni);
2004138568Ssam				/* XXX stat? */
2005138568Ssam				goto restart;
2006138568Ssam			}
2007138568Ssam		}
2008178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2009172062Ssam		    ni->ni_inact <= 0) {
2010178354Ssam			IEEE80211_NOTE(vap,
2011148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2012148320Ssam			    "station timed out due to inactivity "
2013148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
2014138568Ssam			/*
2015138568Ssam			 * Send a deauthenticate frame and drop the station.
2016138568Ssam			 * This is somewhat complicated due to reference counts
2017138568Ssam			 * and locking.  At this point a station will typically
2018138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
2019138568Ssam			 * will do a "free" of the node which will drop the
2020138568Ssam			 * reference count.  But in the meantime a reference
2021138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
2022138568Ssam			 * of the node will happen either after the tx is
2023138568Ssam			 * completed or by ieee80211_node_leave.
2024120483Ssam			 *
2025138568Ssam			 * Separately we must drop the node lock before sending
2026170530Ssam			 * in case the driver takes a lock, as this can result
2027170530Ssam			 * in a LOR between the node lock and the driver lock.
2028119150Ssam			 */
2029172229Ssam			ieee80211_ref_node(ni);
2030138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2031138568Ssam			if (ni->ni_associd != 0) {
2032178354Ssam				IEEE80211_SEND_MGMT(ni,
2033138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
2034138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
2035138568Ssam			}
2036178354Ssam			ieee80211_node_leave(ni);
2037172229Ssam			ieee80211_free_node(ni);
2038178354Ssam			vap->iv_stats.is_node_timeout++;
2039120483Ssam			goto restart;
2040120483Ssam		}
2041116742Ssam	}
2042138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2043138568Ssam
2044178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2045170530Ssam}
2046138568Ssam
2047178354Ssam/*
2048178354Ssam * Aggressively reclaim resources.  This should be used
2049178354Ssam * only in a critical situation to reclaim mbuf resources.
2050178354Ssam */
2051170530Ssamvoid
2052178354Ssamieee80211_drain(struct ieee80211com *ic)
2053178354Ssam{
2054178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2055178354Ssam	struct ieee80211vap *vap;
2056178354Ssam	struct ieee80211_node *ni;
2057178354Ssam
2058178354Ssam	IEEE80211_NODE_LOCK(nt);
2059178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2060178354Ssam		/*
2061178354Ssam		 * Ignore entries for which have yet to receive an
2062178354Ssam		 * authentication frame.  These are transient and
2063178354Ssam		 * will be reclaimed when the last reference to them
2064178354Ssam		 * goes away (when frame xmits complete).
2065178354Ssam		 */
2066178354Ssam		vap = ni->ni_vap;
2067178354Ssam		/*
2068178354Ssam		 * Only process stations when in RUN state.  This
2069178354Ssam		 * insures, for example, that we don't timeout an
2070178354Ssam		 * inactive station during CAC.  Note that CSA state
2071178354Ssam		 * is actually handled in ieee80211_node_timeout as
2072178354Ssam		 * it applies to more than timeout processing.
2073178354Ssam		 */
2074178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2075178354Ssam			continue;
2076178354Ssam		/* XXX can vap be NULL? */
2077178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2078178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2079178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2080178354Ssam			continue;
2081178354Ssam		/*
2082178354Ssam		 * Free fragments.
2083178354Ssam		 * XXX doesn't belong here, move to node_drain
2084178354Ssam		 */
2085178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2086178354Ssam			m_freem(ni->ni_rxfrag[0]);
2087178354Ssam			ni->ni_rxfrag[0] = NULL;
2088178354Ssam		}
2089178354Ssam		/*
2090178354Ssam		 * Drain resources held by the station.
2091178354Ssam		 */
2092178354Ssam		ic->ic_node_drain(ni);
2093178354Ssam	}
2094178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2095178354Ssam}
2096178354Ssam
2097178354Ssam/*
2098178354Ssam * Per-ieee80211com inactivity timer callback.
2099178354Ssam */
2100178354Ssamvoid
2101170530Ssamieee80211_node_timeout(void *arg)
2102170530Ssam{
2103170530Ssam	struct ieee80211com *ic = arg;
2104170530Ssam
2105178354Ssam	/*
2106178354Ssam	 * Defer timeout processing if a channel switch is pending.
2107178354Ssam	 * We typically need to be mute so not doing things that
2108178354Ssam	 * might generate frames is good to handle in one place.
2109178354Ssam	 * Supressing the station timeout processing may extend the
2110178354Ssam	 * lifetime of inactive stations (by not decrementing their
2111178354Ssam	 * idle counters) but this should be ok unless the CSA is
2112178354Ssam	 * active for an unusually long time.
2113178354Ssam	 */
2114178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2115178354Ssam		ieee80211_scan_timeout(ic);
2116178354Ssam		ieee80211_timeout_stations(ic);
2117195379Ssam		ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2118170530Ssam
2119178354Ssam		IEEE80211_LOCK(ic);
2120178354Ssam		ieee80211_erp_timeout(ic);
2121178354Ssam		ieee80211_ht_timeout(ic);
2122178354Ssam		IEEE80211_UNLOCK(ic);
2123178354Ssam	}
2124170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2125170530Ssam		ieee80211_node_timeout, ic);
2126116742Ssam}
2127116742Ssam
2128116742Ssamvoid
2129178354Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2130178354Ssam	ieee80211_iter_func *f, void *arg)
2131116742Ssam{
2132116742Ssam	struct ieee80211_node *ni;
2133138568Ssam	u_int gen;
2134116742Ssam
2135178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2136154532Ssam	gen = ++nt->nt_scangen;
2137138568Ssamrestart:
2138138568Ssam	IEEE80211_NODE_LOCK(nt);
2139138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2140138568Ssam		if (ni->ni_scangen != gen) {
2141138568Ssam			ni->ni_scangen = gen;
2142138568Ssam			(void) ieee80211_ref_node(ni);
2143138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2144138568Ssam			(*f)(arg, ni);
2145138568Ssam			ieee80211_free_node(ni);
2146138568Ssam			goto restart;
2147138568Ssam		}
2148138568Ssam	}
2149138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2150138568Ssam
2151178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2152116742Ssam}
2153138568Ssam
2154138568Ssamvoid
2155138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2156138568Ssam{
2157138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2158138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2159138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2160138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2161138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2162138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2163138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2164167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2165167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2166167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2167138568Ssam		ni->ni_rxfragstamp);
2168192468Ssam	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2169192468Ssam		node_getrssi(ni), ni->ni_noise,
2170170530Ssam		ni->ni_intval, ni->ni_capinfo);
2171138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2172138568Ssam		ether_sprintf(ni->ni_bssid),
2173138568Ssam		ni->ni_esslen, ni->ni_essid,
2174138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2175184277Ssam	printf("\tinact %u inact_reload %u txrate %u\n",
2176184277Ssam		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2177170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2178170530Ssam		ni->ni_htcap, ni->ni_htparam,
2179170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2180170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2181170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2182138568Ssam}
2183138568Ssam
2184138568Ssamvoid
2185138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2186138568Ssam{
2187138568Ssam	ieee80211_iterate_nodes(nt,
2188138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2189138568Ssam}
2190138568Ssam
2191179642Ssamstatic void
2192179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2193172211Ssam{
2194178354Ssam	struct ieee80211vap *vap;
2195178354Ssam
2196178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2197178354Ssam
2198178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2199178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2200178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2201172211Ssam}
2202172211Ssam
2203179642Ssamvoid
2204179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2205179642Ssam{
2206179642Ssam	IEEE80211_LOCK(ic);
2207179642Ssam	ieee80211_notify_erp_locked(ic);
2208179642Ssam	IEEE80211_UNLOCK(ic);
2209179642Ssam}
2210179642Ssam
2211138568Ssam/*
2212138568Ssam * Handle a station joining an 11g network.
2213138568Ssam */
2214138568Ssamstatic void
2215178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2216138568Ssam{
2217178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2218138568Ssam
2219173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2220173273Ssam
2221138568Ssam	/*
2222138568Ssam	 * Station isn't capable of short slot time.  Bump
2223138568Ssam	 * the count of long slot time stations and disable
2224138568Ssam	 * use of short slot time.  Note that the actual switch
2225138568Ssam	 * over to long slot time use may not occur until the
2226138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2227138568Ssam	 */
2228138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2229138568Ssam		ic->ic_longslotsta++;
2230178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2231172211Ssam		    "station needs long slot time, count %d",
2232172211Ssam		    ic->ic_longslotsta);
2233138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2234170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2235170530Ssam			/*
2236170530Ssam			 * Don't force slot time when switched to turbo
2237170530Ssam			 * mode as non-ERP stations won't be present; this
2238170530Ssam			 * need only be done when on the normal G channel.
2239170530Ssam			 */
2240170530Ssam			ieee80211_set_shortslottime(ic, 0);
2241170530Ssam		}
2242138568Ssam	}
2243138568Ssam	/*
2244138568Ssam	 * If the new station is not an ERP station
2245138568Ssam	 * then bump the counter and enable protection
2246138568Ssam	 * if configured.
2247138568Ssam	 */
2248178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2249138568Ssam		ic->ic_nonerpsta++;
2250178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2251172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2252172211Ssam		    ic->ic_nonerpsta);
2253138568Ssam		/*
2254138568Ssam		 * If station does not support short preamble
2255138568Ssam		 * then we must enable use of Barker preamble.
2256138568Ssam		 */
2257138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2258178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2259172211Ssam			    "%s", "station needs long preamble");
2260138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2261138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2262138568Ssam		}
2263172211Ssam		/*
2264178354Ssam		 * If protection is configured and this is the first
2265178354Ssam		 * indication we should use protection, enable it.
2266172211Ssam		 */
2267172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2268172211Ssam		    ic->ic_nonerpsta == 1 &&
2269172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2270178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2271172211Ssam			    "%s: enable use of protection\n", __func__);
2272172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2273179642Ssam			ieee80211_notify_erp_locked(ic);
2274172211Ssam		}
2275138568Ssam	} else
2276138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2277138568Ssam}
2278138568Ssam
2279138568Ssamvoid
2280178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2281138568Ssam{
2282178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2283178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2284138568Ssam	int newassoc;
2285138568Ssam
2286138568Ssam	if (ni->ni_associd == 0) {
2287170530Ssam		uint16_t aid;
2288138568Ssam
2289178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2290138568Ssam		/*
2291138568Ssam		 * It would be good to search the bitmap
2292138568Ssam		 * more efficiently, but this will do for now.
2293138568Ssam		 */
2294178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2295178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2296138568Ssam				break;
2297138568Ssam		}
2298178354Ssam		if (aid >= vap->iv_max_aid) {
2299179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2300178354Ssam			ieee80211_node_leave(ni);
2301138568Ssam			return;
2302138568Ssam		}
2303138568Ssam		ni->ni_associd = aid | 0xc000;
2304173273Ssam		ni->ni_jointime = time_uptime;
2305178354Ssam		IEEE80211_LOCK(ic);
2306178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2307178354Ssam		vap->iv_sta_assoc++;
2308138568Ssam		ic->ic_sta_assoc++;
2309173273Ssam
2310173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2311173273Ssam			ieee80211_ht_node_join(ni);
2312170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2313170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2314178354Ssam			ieee80211_node_join_11g(ni);
2315173273Ssam		IEEE80211_UNLOCK(ic);
2316173273Ssam
2317173273Ssam		newassoc = 1;
2318138568Ssam	} else
2319138568Ssam		newassoc = 0;
2320138568Ssam
2321178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2322183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2323139523Ssam	    IEEE80211_NODE_AID(ni),
2324139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2325139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2326139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2327170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2328170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2329182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2330173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2331183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2332183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2333183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2334178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2335170530Ssam		", fast-frames" : "",
2336178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2337170530Ssam		", turbo" : ""
2338139523Ssam	);
2339138568Ssam
2340193966Ssam	ieee80211_node_setuptxparms(ni);
2341138568Ssam	/* give driver a chance to setup state like ni_txrate */
2342139524Ssam	if (ic->ic_newassoc != NULL)
2343148307Ssam		ic->ic_newassoc(ni, newassoc);
2344178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2345138568Ssam	/* tell the authenticator about new station */
2346178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2347178354Ssam		vap->iv_auth->ia_node_join(ni);
2348178354Ssam	ieee80211_notify_node_join(ni,
2349173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2350138568Ssam}
2351138568Ssam
2352172211Ssamstatic void
2353172211Ssamdisable_protection(struct ieee80211com *ic)
2354172211Ssam{
2355172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2356172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2357172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2358172211Ssam	   ic->ic_flags_ext));
2359172211Ssam
2360172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2361172211Ssam	/* XXX verify mode? */
2362172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2363172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2364172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2365172211Ssam	}
2366179642Ssam	ieee80211_notify_erp_locked(ic);
2367172211Ssam}
2368172211Ssam
2369138568Ssam/*
2370138568Ssam * Handle a station leaving an 11g network.
2371138568Ssam */
2372138568Ssamstatic void
2373178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2374138568Ssam{
2375178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2376138568Ssam
2377173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2378173273Ssam
2379170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2380178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2381178354Ssam	      ic->ic_bsschan->ic_flags));
2382138568Ssam
2383138568Ssam	/*
2384138568Ssam	 * If a long slot station do the slot time bookkeeping.
2385138568Ssam	 */
2386138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2387138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2388138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2389138568Ssam		ic->ic_longslotsta--;
2390178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2391172211Ssam		    "long slot time station leaves, count now %d",
2392172211Ssam		    ic->ic_longslotsta);
2393138568Ssam		if (ic->ic_longslotsta == 0) {
2394138568Ssam			/*
2395138568Ssam			 * Re-enable use of short slot time if supported
2396138568Ssam			 * and not operating in IBSS mode (per spec).
2397138568Ssam			 */
2398138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2399138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2400178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2401178354Ssam				    IEEE80211_MSG_ASSOC,
2402138568Ssam				    "%s: re-enable use of short slot time\n",
2403138568Ssam				    __func__);
2404138568Ssam				ieee80211_set_shortslottime(ic, 1);
2405138568Ssam			}
2406138568Ssam		}
2407138568Ssam	}
2408138568Ssam	/*
2409138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2410138568Ssam	 */
2411138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2412138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2413138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2414138568Ssam		ic->ic_nonerpsta--;
2415178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2416172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2417172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2418172211Ssam			" (non-ERP sta present)" : "");
2419172211Ssam		if (ic->ic_nonerpsta == 0 &&
2420172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2421178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2422138568Ssam				"%s: disable use of protection\n", __func__);
2423172211Ssam			disable_protection(ic);
2424138568Ssam		}
2425138568Ssam	}
2426138568Ssam}
2427138568Ssam
2428138568Ssam/*
2429172211Ssam * Time out presence of an overlapping bss with non-ERP
2430172211Ssam * stations.  When operating in hostap mode we listen for
2431172211Ssam * beacons from other stations and if we identify a non-ERP
2432172211Ssam * station is present we enable protection.  To identify
2433172211Ssam * when all non-ERP stations are gone we time out this
2434172211Ssam * condition.
2435172211Ssam */
2436172211Ssamstatic void
2437172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2438172211Ssam{
2439172211Ssam
2440172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2441172211Ssam
2442172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2443172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2444178354Ssam#if 0
2445178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2446178354Ssam		    "%s", "age out non-ERP sta present on channel");
2447178354Ssam#endif
2448172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2449172211Ssam		if (ic->ic_nonerpsta == 0)
2450172211Ssam			disable_protection(ic);
2451172211Ssam	}
2452172211Ssam}
2453172211Ssam
2454172211Ssam/*
2455138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2456138568Ssam * when operating as an ap.
2457138568Ssam */
2458138568Ssamvoid
2459178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2460138568Ssam{
2461178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2462178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2463140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2464138568Ssam
2465178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2466172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2467138568Ssam
2468178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2469178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2470138568Ssam	/*
2471138568Ssam	 * If node wasn't previously associated all
2472138568Ssam	 * we need to do is reclaim the reference.
2473138568Ssam	 */
2474138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2475138568Ssam	if (ni->ni_associd == 0)
2476138568Ssam		goto done;
2477138568Ssam	/*
2478138568Ssam	 * Tell the authenticator the station is leaving.
2479138568Ssam	 * Note that we must do this before yanking the
2480138568Ssam	 * association id as the authenticator uses the
2481138568Ssam	 * associd to locate it's state block.
2482138568Ssam	 */
2483178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2484178354Ssam		vap->iv_auth->ia_node_leave(ni);
2485173273Ssam
2486173273Ssam	IEEE80211_LOCK(ic);
2487178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2488138568Ssam	ni->ni_associd = 0;
2489178354Ssam	vap->iv_sta_assoc--;
2490138568Ssam	ic->ic_sta_assoc--;
2491138568Ssam
2492173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2493173273Ssam		ieee80211_ht_node_leave(ni);
2494170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2495170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2496178354Ssam		ieee80211_node_leave_11g(ni);
2497173273Ssam	IEEE80211_UNLOCK(ic);
2498138568Ssam	/*
2499138568Ssam	 * Cleanup station state.  In particular clear various
2500138568Ssam	 * state that might otherwise be reused if the node
2501138568Ssam	 * is reused before the reference count goes to zero
2502138568Ssam	 * (and memory is reclaimed).
2503138568Ssam	 */
2504178354Ssam	ieee80211_sta_leave(ni);
2505138568Ssamdone:
2506140499Ssam	/*
2507140499Ssam	 * Remove the node from any table it's recorded in and
2508140499Ssam	 * drop the caller's reference.  Removal from the table
2509140499Ssam	 * is important to insure the node is not reprocessed
2510140499Ssam	 * for inactivity.
2511140499Ssam	 */
2512140499Ssam	if (nt != NULL) {
2513140499Ssam		IEEE80211_NODE_LOCK(nt);
2514140499Ssam		node_reclaim(nt, ni);
2515140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2516140499Ssam	} else
2517140499Ssam		ieee80211_free_node(ni);
2518138568Ssam}
2519138568Ssam
2520178354Ssamstruct rssiinfo {
2521178354Ssam	struct ieee80211vap *vap;
2522178354Ssam	int	rssi_samples;
2523178354Ssam	uint32_t rssi_total;
2524178354Ssam};
2525178354Ssam
2526178354Ssamstatic void
2527178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni)
2528178354Ssam{
2529178354Ssam	struct rssiinfo *info = arg;
2530178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2531178354Ssam	int8_t rssi;
2532178354Ssam
2533178354Ssam	if (info->vap != vap)
2534178354Ssam		return;
2535178354Ssam	/* only associated stations */
2536178354Ssam	if (ni->ni_associd == 0)
2537178354Ssam		return;
2538178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2539178354Ssam	if (rssi != 0) {
2540178354Ssam		info->rssi_samples++;
2541178354Ssam		info->rssi_total += rssi;
2542178354Ssam	}
2543178354Ssam}
2544178354Ssam
2545178354Ssamstatic void
2546178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2547178354Ssam{
2548178354Ssam	struct rssiinfo *info = arg;
2549178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2550178354Ssam	int8_t rssi;
2551178354Ssam
2552178354Ssam	if (info->vap != vap)
2553178354Ssam		return;
2554178354Ssam	/* only neighbors */
2555178354Ssam	/* XXX check bssid */
2556178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2557178354Ssam		return;
2558178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2559178354Ssam	if (rssi != 0) {
2560178354Ssam		info->rssi_samples++;
2561178354Ssam		info->rssi_total += rssi;
2562178354Ssam	}
2563178354Ssam}
2564178354Ssam
2565195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
2566195618Srpaulostatic void
2567195618Srpauloget_mesh_rssi(void *arg, struct ieee80211_node *ni)
2568195618Srpaulo{
2569195618Srpaulo	struct rssiinfo *info = arg;
2570195618Srpaulo	struct ieee80211vap *vap = ni->ni_vap;
2571195618Srpaulo	int8_t rssi;
2572195618Srpaulo
2573195618Srpaulo	if (info->vap != vap)
2574195618Srpaulo		return;
2575195618Srpaulo	/* only neighbors that peered successfully */
2576195618Srpaulo	if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2577195618Srpaulo		return;
2578195618Srpaulo	rssi = vap->iv_ic->ic_node_getrssi(ni);
2579195618Srpaulo	if (rssi != 0) {
2580195618Srpaulo		info->rssi_samples++;
2581195618Srpaulo		info->rssi_total += rssi;
2582195618Srpaulo	}
2583195618Srpaulo}
2584195618Srpaulo#endif /* IEEE80211_SUPPORT_MESH */
2585195618Srpaulo
2586170530Ssamint8_t
2587178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2588138568Ssam{
2589138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2590178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2591178354Ssam	struct rssiinfo info;
2592138568Ssam
2593178354Ssam	info.rssi_total = 0;
2594178354Ssam	info.rssi_samples = 0;
2595178354Ssam	info.vap = vap;
2596178354Ssam	switch (vap->iv_opmode) {
2597138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2598138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2599178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2600178354Ssam		break;
2601138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2602178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2603138568Ssam		break;
2604195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
2605195618Srpaulo	case IEEE80211_M_MBSS:		/* average of all mesh neighbors */
2606195618Srpaulo		ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2607195618Srpaulo		break;
2608195618Srpaulo#endif
2609138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2610138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2611138568Ssam	default:
2612178354Ssam		if (vap->iv_bss != NULL)
2613178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2614178354Ssam		info.rssi_samples = 1;
2615138568Ssam		break;
2616138568Ssam	}
2617178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2618138568Ssam#undef NZ
2619138568Ssam}
2620138568Ssam
2621170530Ssamvoid
2622178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2623138568Ssam{
2624138568Ssam
2625178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2626170530Ssam		return;
2627178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2628170530Ssam	/* for non-station mode return avg'd rssi accounting */
2629178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2630178354Ssam		*rssi = ieee80211_getrssi(vap);
2631138568Ssam}
2632