ieee80211_node.c revision 188494
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 188494 2009-02-11 18:40:09Z sam $");
29116742Ssam
30178354Ssam#include "opt_wlan.h"
31178354Ssam
32116742Ssam#include <sys/param.h>
33116742Ssam#include <sys/systm.h>
34116742Ssam#include <sys/mbuf.h>
35116742Ssam#include <sys/malloc.h>
36116742Ssam#include <sys/kernel.h>
37138568Ssam
38116742Ssam#include <sys/socket.h>
39116742Ssam
40116742Ssam#include <net/if.h>
41116742Ssam#include <net/if_media.h>
42116742Ssam#include <net/ethernet.h>
43116742Ssam
44116742Ssam#include <net80211/ieee80211_var.h>
45178354Ssam#include <net80211/ieee80211_input.h>
46186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
47186904Ssam#include <net80211/ieee80211_tdma.h>
48186904Ssam#endif
49178354Ssam#include <net80211/ieee80211_wds.h>
50116742Ssam
51116742Ssam#include <net/bpf.h>
52116742Ssam
53147221Ssam/*
54147221Ssam * Association id's are managed with a bit vector.
55147221Ssam */
56178354Ssam#define	IEEE80211_AID_SET(_vap, b) \
57178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
58178354Ssam		(1 << (IEEE80211_AID(b) % 32)))
59178354Ssam#define	IEEE80211_AID_CLR(_vap, b) \
60178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
61178354Ssam		~(1 << (IEEE80211_AID(b) % 32)))
62178354Ssam#define	IEEE80211_AID_ISSET(_vap, b) \
63178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
64147221Ssam
65159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
66159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
67159139Sdds#else
68159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
69159139Sdds#endif
70159139Sdds
71170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
72170530Ssam
73179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *,
74179643Ssam	const uint8_t [IEEE80211_ADDR_LEN]);
75138568Ssamstatic void node_cleanup(struct ieee80211_node *);
76138568Ssamstatic void node_free(struct ieee80211_node *);
77178354Ssamstatic void node_age(struct ieee80211_node *);
78170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
79170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
80178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *,
81178354Ssam	struct ieee80211_mimo_info *);
82116742Ssam
83138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
84120104Ssam
85138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
86148863Ssam	struct ieee80211_node_table *nt, const char *name,
87170530Ssam	int inact, int keymaxix);
88178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *,
89178354Ssam	struct ieee80211vap *);
90178354Ssamstatic void ieee80211_node_reclaim(struct ieee80211_node *);
91138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
92172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
93138568Ssam
94127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
95178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
96120481Ssam
97116742Ssamvoid
98138568Ssamieee80211_node_attach(struct ieee80211com *ic)
99116742Ssam{
100178354Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
101178354Ssam		IEEE80211_INACT_INIT, ic->ic_max_keyix);
102178354Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
103178354Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
104178354Ssam		ieee80211_node_timeout, ic);
105116742Ssam
106138568Ssam	ic->ic_node_alloc = node_alloc;
107138568Ssam	ic->ic_node_free = node_free;
108138568Ssam	ic->ic_node_cleanup = node_cleanup;
109178354Ssam	ic->ic_node_age = node_age;
110178354Ssam	ic->ic_node_drain = node_age;		/* NB: same as age */
111138568Ssam	ic->ic_node_getrssi = node_getrssi;
112170530Ssam	ic->ic_node_getsignal = node_getsignal;
113178354Ssam	ic->ic_node_getmimoinfo = node_getmimoinfo;
114138568Ssam
115178354Ssam	/*
116178354Ssam	 * Set flags to be propagated to all vap's;
117178354Ssam	 * these define default behaviour/configuration.
118178354Ssam	 */
119178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
120178354Ssam}
121138568Ssam
122178354Ssamvoid
123178354Ssamieee80211_node_detach(struct ieee80211com *ic)
124178354Ssam{
125170530Ssam
126178354Ssam	callout_drain(&ic->ic_inact);
127178354Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
128178354Ssam}
129172062Ssam
130178354Ssamvoid
131178354Ssamieee80211_node_vattach(struct ieee80211vap *vap)
132178354Ssam{
133178354Ssam	/* NB: driver can override */
134178354Ssam	vap->iv_max_aid = IEEE80211_AID_DEF;
135178354Ssam
136178354Ssam	/* default station inactivity timer setings */
137178354Ssam	vap->iv_inact_init = IEEE80211_INACT_INIT;
138178354Ssam	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
139178354Ssam	vap->iv_inact_run = IEEE80211_INACT_RUN;
140178354Ssam	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
141184277Ssam
142184277Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
143184277Ssam	    "%s: init %u auth %u run %u probe %u\n", __func__,
144184277Ssam	    vap->iv_inact_init, vap->iv_inact_auth,
145184277Ssam	    vap->iv_inact_run, vap->iv_inact_probe);
146148863Ssam}
147148863Ssam
148148863Ssamvoid
149178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap)
150148863Ssam{
151178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
152178354Ssam		/* XXX should we allow max aid to be zero? */
153178354Ssam		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
154178354Ssam			vap->iv_max_aid = IEEE80211_AID_MIN;
155178354Ssam			if_printf(vap->iv_ifp,
156178354Ssam			    "WARNING: max aid too small, changed to %d\n",
157178354Ssam			    vap->iv_max_aid);
158178354Ssam		}
159186302Ssam		vap->iv_aid_bitmap = (uint32_t *) malloc(
160184210Sdes			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
161178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
162178354Ssam		if (vap->iv_aid_bitmap == NULL) {
163178354Ssam			/* XXX no way to recover */
164178354Ssam			printf("%s: no memory for AID bitmap, max aid %d!\n",
165178354Ssam			    __func__, vap->iv_max_aid);
166178354Ssam			vap->iv_max_aid = 0;
167178354Ssam		}
168138568Ssam	}
169138568Ssam
170178354Ssam	ieee80211_reset_bss(vap);
171118887Ssam
172178354Ssam	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
173116742Ssam}
174116742Ssam
175116742Ssamvoid
176178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap)
177116742Ssam{
178178354Ssam	struct ieee80211com *ic = vap->iv_ic;
179116742Ssam
180178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
181178354Ssam	if (vap->iv_bss != NULL) {
182178354Ssam		ieee80211_free_node(vap->iv_bss);
183178354Ssam		vap->iv_bss = NULL;
184138568Ssam	}
185178354Ssam	if (vap->iv_aid_bitmap != NULL) {
186186302Ssam		free(vap->iv_aid_bitmap, M_80211_NODE);
187178354Ssam		vap->iv_aid_bitmap = NULL;
188138568Ssam	}
189116742Ssam}
190116742Ssam
191138568Ssam/*
192138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
193138568Ssam */
194138568Ssam
195138568Ssamvoid
196148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
197138568Ssam{
198184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
199184277Ssam
200138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
201184277Ssam	ni->ni_inact_reload = vap->iv_inact_run;
202172062Ssam	ni->ni_inact = ni->ni_inact_reload;
203184277Ssam
204184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
205184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
206138568Ssam}
207138568Ssam
208138568Ssamvoid
209148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
210138568Ssam{
211184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
212184277Ssam
213138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
214184277Ssam	ni->ni_inact_reload = vap->iv_inact_auth;
215172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
216172062Ssam		ni->ni_inact = ni->ni_inact_reload;
217184277Ssam
218184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
219184277Ssam	    "%s: inact_reload %u inact %u", __func__,
220184277Ssam	    ni->ni_inact_reload, ni->ni_inact);
221138568Ssam}
222138568Ssam
223116742Ssam/*
224183251Ssam * Fix tx parameters for a node according to ``association state''.
225183251Ssam */
226183251Ssamstatic void
227183251Ssamnode_setuptxparms(struct ieee80211_node *ni)
228183251Ssam{
229183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
230187898Ssam	enum ieee80211_phymode mode;
231183251Ssam
232183251Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
233183251Ssam		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
234187898Ssam			mode = IEEE80211_MODE_11NA;
235183251Ssam		else
236187898Ssam			mode = IEEE80211_MODE_11NG;
237183251Ssam	} else {				/* legacy rate handling */
238187898Ssam		/* NB: 108A/108G should be handled as 11a/11g respectively */
239187898Ssam		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
240187898Ssam			mode = IEEE80211_MODE_STURBO_A;
241187898Ssam		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
242187898Ssam			mode = IEEE80211_MODE_11A;
243183251Ssam		else if (ni->ni_flags & IEEE80211_NODE_ERP)
244187898Ssam			mode = IEEE80211_MODE_11G;
245183251Ssam		else
246187898Ssam			mode = IEEE80211_MODE_11B;
247183251Ssam	}
248187898Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
249183251Ssam}
250183251Ssam
251183251Ssam/*
252138568Ssam * Set/change the channel.  The rate set is also updated as
253138568Ssam * to insure a consistent view by drivers.
254178354Ssam * XXX should be private but hostap needs it to deal with CSA
255138568Ssam */
256178354Ssamvoid
257178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni,
258178354Ssam	struct ieee80211_channel *chan)
259138568Ssam{
260178354Ssam	struct ieee80211com *ic = ni->ni_ic;
261183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
262183251Ssam	enum ieee80211_phymode mode;
263170530Ssam
264178354Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
265178354Ssam
266138568Ssam	ni->ni_chan = chan;
267183251Ssam	mode = ieee80211_chan2mode(chan);
268170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
269170530Ssam		/*
270170530Ssam		 * XXX Gotta be careful here; the rate set returned by
271170530Ssam		 * ieee80211_get_suprates is actually any HT rate
272170530Ssam		 * set so blindly copying it will be bad.  We must
273170530Ssam		 * install the legacy rate est in ni_rates and the
274170530Ssam		 * HT rate set in ni_htrates.
275170530Ssam		 */
276170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
277183251Ssam		/*
278183251Ssam		 * Setup bss tx parameters based on operating mode.  We
279183251Ssam		 * use legacy rates when operating in a mixed HT+non-HT bss
280183251Ssam		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
281183251Ssam		 */
282183251Ssam		if (mode == IEEE80211_MODE_11NA &&
283183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
284183251Ssam			mode = IEEE80211_MODE_11A;
285183251Ssam		else if (mode == IEEE80211_MODE_11NG &&
286183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
287183251Ssam			mode = IEEE80211_MODE_11G;
288183251Ssam		if (mode == IEEE80211_MODE_11G &&
289183251Ssam		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
290183251Ssam			mode = IEEE80211_MODE_11B;
291170530Ssam	}
292183251Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
293165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
294138568Ssam}
295138568Ssam
296141658Ssamstatic __inline void
297141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
298141658Ssam{
299141658Ssam	/* propagate useful state */
300141658Ssam	nbss->ni_authmode = obss->ni_authmode;
301141658Ssam	nbss->ni_txpower = obss->ni_txpower;
302141658Ssam	nbss->ni_vlan = obss->ni_vlan;
303141658Ssam	/* XXX statistics? */
304178354Ssam	/* XXX legacy WDS bssid? */
305141658Ssam}
306141658Ssam
307116742Ssamvoid
308178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
309116742Ssam{
310178354Ssam	struct ieee80211com *ic = vap->iv_ic;
311116742Ssam	struct ieee80211_node *ni;
312116742Ssam
313178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
314178354Ssam		"%s: creating ibss on channel %u\n", __func__,
315178354Ssam		ieee80211_chan2ieee(ic, chan));
316138568Ssam
317178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
318140753Ssam	if (ni == NULL) {
319140753Ssam		/* XXX recovery? */
320138568Ssam		return;
321138568Ssam	}
322178354Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
323178354Ssam	ni->ni_esslen = vap->iv_des_ssid[0].len;
324178354Ssam	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
325178354Ssam	if (vap->iv_bss != NULL)
326178354Ssam		copy_bss(ni, vap->iv_bss);
327148843Ssam	ni->ni_intval = ic->ic_bintval;
328178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
329116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
330116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
331116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
332116742Ssam		ni->ni_fhindex = 1;
333116742Ssam	}
334178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
335178354Ssam		vap->iv_flags |= IEEE80211_F_SIBSS;
336138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
337178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
338178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
339167282Ssam		else {
340167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
341167282Ssam			/* clear group bit, add local bit */
342167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
343167282Ssam		}
344178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
345178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
346178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
347153403Ssam		else
348186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
349186904Ssam		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
350186904Ssam#endif
351153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
352138568Ssam	}
353138568Ssam	/*
354138568Ssam	 * Fix the channel and related attributes.
355138568Ssam	 */
356178354Ssam	/* clear DFS CAC state on previous channel */
357178354Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
358178354Ssam	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
359178354Ssam	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
360178354Ssam		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
361170530Ssam	ic->ic_bsschan = chan;
362178354Ssam	ieee80211_node_set_chan(ni, chan);
363170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
364138568Ssam	/*
365178354Ssam	 * Do mode-specific setup.
366138568Ssam	 */
367170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
368170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
369170530Ssam			/*
370178354Ssam			 * Use a mixed 11b/11g basic rate set.
371170530Ssam			 */
372178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
373178354Ssam			    IEEE80211_MODE_11G);
374178354Ssam			if (vap->iv_flags & IEEE80211_F_PUREG) {
375178354Ssam				/*
376178354Ssam				 * Also mark OFDM rates basic so 11b
377178354Ssam				 * stations do not join (WiFi compliance).
378178354Ssam				 */
379178354Ssam				ieee80211_addbasicrates(&ni->ni_rates,
380178354Ssam				    IEEE80211_MODE_11A);
381178354Ssam			}
382170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
383170530Ssam			/*
384170530Ssam			 * Force pure 11b rate set.
385170530Ssam			 */
386178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
387170530Ssam				IEEE80211_MODE_11B);
388170530Ssam		}
389170530Ssam	}
390138568Ssam
391170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
392116742Ssam}
393116742Ssam
394170530Ssam/*
395170530Ssam * Reset bss state on transition to the INIT state.
396170530Ssam * Clear any stations from the table (they have been
397170530Ssam * deauth'd) and reset the bss node (clears key, rate
398170530Ssam * etc. state).
399170530Ssam */
400138568Ssamvoid
401178354Ssamieee80211_reset_bss(struct ieee80211vap *vap)
402138568Ssam{
403178354Ssam	struct ieee80211com *ic = vap->iv_ic;
404138568Ssam	struct ieee80211_node *ni, *obss;
405138568Ssam
406178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
407178354Ssam	/* XXX multi-bss: wrong */
408170530Ssam	ieee80211_reset_erp(ic);
409140753Ssam
410178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
411138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
412178354Ssam	obss = vap->iv_bss;
413178354Ssam	vap->iv_bss = ieee80211_ref_node(ni);
414141658Ssam	if (obss != NULL) {
415141658Ssam		copy_bss(ni, obss);
416148843Ssam		ni->ni_intval = ic->ic_bintval;
417138568Ssam		ieee80211_free_node(obss);
418178354Ssam	} else
419178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
420138568Ssam}
421138568Ssam
422170530Ssamstatic int
423170530Ssammatch_ssid(const struct ieee80211_node *ni,
424170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
425170530Ssam{
426170530Ssam	int i;
427148432Ssam
428170530Ssam	for (i = 0; i < nssid; i++) {
429170530Ssam		if (ni->ni_esslen == ssids[i].len &&
430170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
431170530Ssam			return 1;
432170530Ssam	}
433170530Ssam	return 0;
434170530Ssam}
435170530Ssam
436170530Ssam/*
437170530Ssam * Test a node for suitability/compatibility.
438170530Ssam */
439127767Ssamstatic int
440178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
441127767Ssam{
442178354Ssam	struct ieee80211com *ic = ni->ni_ic;
443170530Ssam        uint8_t rate;
444170530Ssam
445170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
446170530Ssam		return 0;
447178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
448170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
449170530Ssam			return 0;
450170530Ssam	} else {
451170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
452170530Ssam			return 0;
453170530Ssam	}
454178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
455170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
456170530Ssam			return 0;
457170530Ssam	} else {
458170530Ssam		/* XXX does this mean privacy is supported or required? */
459170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
460170530Ssam			return 0;
461170530Ssam	}
462170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
463170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
464170530Ssam	if (rate & IEEE80211_RATE_BASIC)
465170530Ssam		return 0;
466178354Ssam	if (vap->iv_des_nssid != 0 &&
467178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
468170530Ssam		return 0;
469178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
470178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
471170530Ssam		return 0;
472170530Ssam	return 1;
473170530Ssam}
474170530Ssam
475170530Ssam#ifdef IEEE80211_DEBUG
476170530Ssam/*
477170530Ssam * Display node suitability/compatibility.
478170530Ssam */
479170530Ssamstatic void
480178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
481170530Ssam{
482178354Ssam	struct ieee80211com *ic = ni->ni_ic;
483170530Ssam        uint8_t rate;
484127767Ssam        int fail;
485127767Ssam
486127767Ssam	fail = 0;
487127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
488127767Ssam		fail |= 0x01;
489178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
490127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
491127767Ssam			fail |= 0x02;
492127767Ssam	} else {
493127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
494127767Ssam			fail |= 0x02;
495127767Ssam	}
496178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
497127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
498127767Ssam			fail |= 0x04;
499127767Ssam	} else {
500127767Ssam		/* XXX does this mean privacy is supported or required? */
501127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
502127767Ssam			fail |= 0x04;
503127767Ssam	}
504167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
505165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
506127767Ssam	if (rate & IEEE80211_RATE_BASIC)
507127767Ssam		fail |= 0x08;
508178354Ssam	if (vap->iv_des_nssid != 0 &&
509178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
510127767Ssam		fail |= 0x10;
511178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
512178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
513127767Ssam		fail |= 0x20;
514127767Ssam
515170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
516170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
517170530Ssam	printf(" %3d%c",
518170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
519170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
520170530Ssam	    fail & 0x08 ? '!' : ' ');
521170530Ssam	printf(" %4s%c",
522170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
523170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
524170530Ssam	    "????",
525170530Ssam	    fail & 0x02 ? '!' : ' ');
526170530Ssam	printf(" %3s%c ",
527170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
528170530Ssam	    fail & 0x04 ? '!' : ' ');
529170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
530170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
531138568Ssam}
532170530Ssam#endif /* IEEE80211_DEBUG */
533138568Ssam
534138568Ssam/*
535138568Ssam * Handle 802.11 ad hoc network merge.  The
536138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
537138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
538138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
539138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
540138568Ssam * sends beacons with the greatest TSF timestamp.
541138568Ssam *
542138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
543138568Ssam *
544138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
545138568Ssam */
546138568Ssamint
547148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
548138568Ssam{
549178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
550178354Ssam#ifdef IEEE80211_DEBUG
551148306Ssam	struct ieee80211com *ic = ni->ni_ic;
552178354Ssam#endif
553138568Ssam
554178354Ssam	if (ni == vap->iv_bss ||
555178354Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
556138568Ssam		/* unchanged, nothing to do */
557138568Ssam		return 0;
558138568Ssam	}
559178354Ssam	if (!check_bss(vap, ni)) {
560170530Ssam		/* capabilities mismatch */
561178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
562138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
563170530Ssam#ifdef IEEE80211_DEBUG
564178354Ssam		if (ieee80211_msg_assoc(vap))
565178354Ssam			check_bss_debug(vap, ni);
566170530Ssam#endif
567178354Ssam		vap->iv_stats.is_ibss_capmismatch++;
568138568Ssam		return 0;
569138568Ssam	}
570178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
571138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
572138568Ssam		ether_sprintf(ni->ni_bssid),
573138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
574138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
575138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
576138568Ssam	);
577170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
578138568Ssam}
579138568Ssam
580138568Ssam/*
581178354Ssam * Calculate HT channel promotion flags for all vaps.
582178354Ssam * This assumes ni_chan have been setup for each vap.
583173273Ssam */
584178354Ssamstatic int
585178354Ssamgethtadjustflags(struct ieee80211com *ic)
586178354Ssam{
587178354Ssam	struct ieee80211vap *vap;
588178354Ssam	int flags;
589178354Ssam
590178354Ssam	flags = 0;
591178354Ssam	/* XXX locking */
592178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
593178354Ssam		if (vap->iv_state < IEEE80211_S_RUN)
594178354Ssam			continue;
595178354Ssam		switch (vap->iv_opmode) {
596178354Ssam		case IEEE80211_M_WDS:
597178354Ssam		case IEEE80211_M_STA:
598178354Ssam		case IEEE80211_M_AHDEMO:
599178354Ssam		case IEEE80211_M_HOSTAP:
600178354Ssam		case IEEE80211_M_IBSS:
601178354Ssam			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
602178354Ssam			break;
603178354Ssam		default:
604178354Ssam			break;
605178354Ssam		}
606178354Ssam	}
607178354Ssam	return flags;
608178354Ssam}
609178354Ssam
610178354Ssam/*
611178354Ssam * Check if the current channel needs to change based on whether
612184303Ssam * any vap's are using HT20/HT40.  This is used to sync the state
613184303Ssam * of ic_curchan after a channel width change on a running vap.
614178354Ssam */
615173273Ssamvoid
616178354Ssamieee80211_sync_curchan(struct ieee80211com *ic)
617173273Ssam{
618178354Ssam	struct ieee80211_channel *c;
619178354Ssam
620178354Ssam	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
621178354Ssam	if (c != ic->ic_curchan) {
622178354Ssam		ic->ic_curchan = c;
623178354Ssam		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
624178354Ssam		ic->ic_set_channel(ic);
625178354Ssam	}
626178354Ssam}
627178354Ssam
628178354Ssam/*
629178354Ssam * Change the current channel.  The request channel may be
630178354Ssam * promoted if other vap's are operating with HT20/HT40.
631178354Ssam */
632178354Ssamvoid
633178354Ssamieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
634178354Ssam{
635178354Ssam	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
636178354Ssam		int flags = gethtadjustflags(ic);
637178354Ssam		/*
638178354Ssam		 * Check for channel promotion required to support the
639178354Ssam		 * set of running vap's.  This assumes we are called
640178354Ssam		 * after ni_chan is setup for each vap.
641178354Ssam		 */
642178354Ssam		/* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
643178354Ssam		if (flags > ieee80211_htchanflags(c))
644178354Ssam			c = ieee80211_ht_adjust_channel(ic, c, flags);
645178354Ssam	}
646178354Ssam	ic->ic_bsschan = ic->ic_curchan = c;
647173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
648173273Ssam	ic->ic_set_channel(ic);
649173273Ssam}
650173273Ssam
651173273Ssam/*
652138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
653138568Ssam * be passed in with a held reference.
654138568Ssam */
655170530Ssamstatic int
656170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
657138568Ssam{
658178354Ssam	struct ieee80211vap *vap = selbs->ni_vap;
659170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
660138568Ssam	struct ieee80211_node *obss;
661170530Ssam	int canreassoc;
662138568Ssam
663138568Ssam	/*
664138568Ssam	 * Committed to selbs, setup state.
665138568Ssam	 */
666178354Ssam	obss = vap->iv_bss;
667170530Ssam	/*
668170530Ssam	 * Check if old+new node have the same address in which
669170530Ssam	 * case we can reassociate when operating in sta mode.
670170530Ssam	 */
671170530Ssam	canreassoc = (obss != NULL &&
672178354Ssam		vap->iv_state == IEEE80211_S_RUN &&
673170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
674178354Ssam	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
675153352Ssam	if (obss != NULL) {
676153352Ssam		copy_bss(selbs, obss);
677178354Ssam		ieee80211_node_reclaim(obss);
678178354Ssam		obss = NULL;		/* NB: guard against later use */
679153352Ssam	}
680165887Ssam
681138568Ssam	/*
682165887Ssam	 * Delete unusable rates; we've already checked
683165887Ssam	 * that the negotiated rate set is acceptable.
684165887Ssam	 */
685178354Ssam	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
686167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
687165887Ssam
688178354Ssam	ieee80211_setcurchan(ic, selbs->ni_chan);
689165887Ssam	/*
690138568Ssam	 * Set the erp state (mostly the slot time) to deal with
691138568Ssam	 * the auto-select case; this should be redundant if the
692138568Ssam	 * mode is locked.
693138568Ssam	 */
694138568Ssam	ieee80211_reset_erp(ic);
695178354Ssam	ieee80211_wme_initparams(vap);
696140753Ssam
697178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
698170530Ssam		if (canreassoc) {
699170530Ssam			/* Reassociate */
700178354Ssam			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
701170530Ssam		} else {
702170530Ssam			/*
703170530Ssam			 * Act as if we received a DEAUTH frame in case we
704170530Ssam			 * are invoked from the RUN state.  This will cause
705170530Ssam			 * us to try to re-authenticate if we are operating
706170530Ssam			 * as a station.
707170530Ssam			 */
708178354Ssam			ieee80211_new_state(vap, IEEE80211_S_AUTH,
709170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
710170530Ssam		}
711170530Ssam	} else
712178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
713138568Ssam	return 1;
714116742Ssam}
715116742Ssam
716170530Ssamint
717184274Ssamieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
718170530Ssam	const struct ieee80211_scan_entry *se)
719170530Ssam{
720178354Ssam	struct ieee80211com *ic = vap->iv_ic;
721170530Ssam	struct ieee80211_node *ni;
722170530Ssam
723178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
724170530Ssam	if (ni == NULL) {
725170530Ssam		/* XXX msg */
726170530Ssam		return 0;
727170530Ssam	}
728170530Ssam	/*
729170530Ssam	 * Expand scan state into node's format.
730170530Ssam	 * XXX may not need all this stuff
731170530Ssam	 */
732170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
733170530Ssam	ni->ni_esslen = se->se_ssid[1];
734170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
735170530Ssam	ni->ni_rstamp = se->se_rstamp;
736170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
737170530Ssam	ni->ni_intval = se->se_intval;
738170530Ssam	ni->ni_capinfo = se->se_capinfo;
739184274Ssam	ni->ni_chan = chan;
740170530Ssam	ni->ni_timoff = se->se_timoff;
741170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
742170530Ssam	ni->ni_fhindex = se->se_fhindex;
743170530Ssam	ni->ni_erp = se->se_erp;
744178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
745170530Ssam	ni->ni_noise = se->se_noise;
746186870Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
747186870Ssam		/* NB: only infrastructure mode requires an associd */
748186870Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
749186870Ssam	}
750178354Ssam
751178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
752178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
753178354Ssam		if (ni->ni_ies.ath_ie != NULL)
754178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
755178354Ssam		if (ni->ni_ies.htcap_ie != NULL)
756178354Ssam			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
757178354Ssam		if (ni->ni_ies.htinfo_ie != NULL)
758178354Ssam			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
759186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
760186904Ssam		if (ni->ni_ies.tdma_ie != NULL)
761186904Ssam			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
762186904Ssam#endif
763173864Ssam	}
764170530Ssam
765178354Ssam	vap->iv_dtim_period = se->se_dtimperiod;
766178354Ssam	vap->iv_dtim_count = 0;
767170530Ssam
768170530Ssam	/* NB: must be after ni_chan is setup */
769170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
770170530Ssam		IEEE80211_F_DOSORT);
771184279Ssam	if (ieee80211_iserp_rateset(&ni->ni_rates))
772184279Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
773184279Ssam	node_setuptxparms(ni);
774170530Ssam
775170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
776170530Ssam}
777170530Ssam
778138568Ssam/*
779138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
780138568Ssam * be passed in with a held reference.
781138568Ssam */
782138568Ssamvoid
783178354Ssamieee80211_sta_leave(struct ieee80211_node *ni)
784138568Ssam{
785178354Ssam	struct ieee80211com *ic = ni->ni_ic;
786178354Ssam
787138568Ssam	ic->ic_node_cleanup(ni);
788178354Ssam	ieee80211_notify_node_leave(ni);
789138568Ssam}
790138568Ssam
791178354Ssam/*
792178354Ssam * Send a deauthenticate frame and drop the station.
793178354Ssam */
794178354Ssamvoid
795178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason)
796178354Ssam{
797178354Ssam	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
798178354Ssam	ieee80211_ref_node(ni);
799178354Ssam	if (ni->ni_associd != 0)
800178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
801178354Ssam	ieee80211_node_leave(ni);
802178354Ssam	ieee80211_free_node(ni);
803178354Ssam}
804178354Ssam
805116742Ssamstatic struct ieee80211_node *
806179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
807116742Ssam{
808127768Ssam	struct ieee80211_node *ni;
809138568Ssam
810186302Ssam	ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
811127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
812127768Ssam	return ni;
813116742Ssam}
814116742Ssam
815138568Ssam/*
816178354Ssam * Initialize an ie blob with the specified data.  If previous
817178354Ssam * data exists re-use the data block.  As a side effect we clear
818178354Ssam * all references to specific ie's; the caller is required to
819178354Ssam * recalculate them.
820178354Ssam */
821178354Ssamint
822178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
823178354Ssam{
824178354Ssam	/* NB: assumes data+len are the last fields */
825178354Ssam	memset(ies, 0, offsetof(struct ieee80211_ies, data));
826178354Ssam	if (ies->data != NULL && ies->len != len) {
827178354Ssam		/* data size changed */
828186302Ssam		free(ies->data, M_80211_NODE_IE);
829178354Ssam		ies->data = NULL;
830178354Ssam	}
831178354Ssam	if (ies->data == NULL) {
832186302Ssam		ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
833178354Ssam		if (ies->data == NULL) {
834178354Ssam			ies->len = 0;
835178354Ssam			/* NB: pointers have already been zero'd above */
836178354Ssam			return 0;
837178354Ssam		}
838178354Ssam	}
839178354Ssam	memcpy(ies->data, data, len);
840178354Ssam	ies->len = len;
841178354Ssam	return 1;
842178354Ssam}
843178354Ssam
844178354Ssam/*
845178354Ssam * Reclaim storage for an ie blob.
846178354Ssam */
847178354Ssamvoid
848178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies)
849178354Ssam{
850178354Ssam	if (ies->data != NULL)
851186302Ssam		free(ies->data, M_80211_NODE_IE);
852178354Ssam}
853178354Ssam
854178354Ssam/*
855178354Ssam * Expand an ie blob data contents and to fillin individual
856178354Ssam * ie pointers.  The data blob is assumed to be well-formed;
857178354Ssam * we don't do any validity checking of ie lengths.
858178354Ssam */
859178354Ssamvoid
860178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies)
861178354Ssam{
862178354Ssam	uint8_t *ie;
863178354Ssam	int ielen;
864178354Ssam
865178354Ssam	ie = ies->data;
866178354Ssam	ielen = ies->len;
867178354Ssam	while (ielen > 0) {
868178354Ssam		switch (ie[0]) {
869178354Ssam		case IEEE80211_ELEMID_VENDOR:
870178354Ssam			if (iswpaoui(ie))
871178354Ssam				ies->wpa_ie = ie;
872178354Ssam			else if (iswmeoui(ie))
873178354Ssam				ies->wme_ie = ie;
874178354Ssam			else if (isatherosoui(ie))
875178354Ssam				ies->ath_ie = ie;
876186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
877186904Ssam			else if (istdmaoui(ie))
878186904Ssam				ies->tdma_ie = ie;
879186904Ssam#endif
880178354Ssam			break;
881178354Ssam		case IEEE80211_ELEMID_RSN:
882178354Ssam			ies->rsn_ie = ie;
883178354Ssam			break;
884178354Ssam		case IEEE80211_ELEMID_HTCAP:
885178354Ssam			ies->htcap_ie = ie;
886178354Ssam			break;
887178354Ssam		}
888178354Ssam		ielen -= 2 + ie[1];
889178354Ssam		ie += 2 + ie[1];
890178354Ssam	}
891178354Ssam}
892178354Ssam
893178354Ssam/*
894138568Ssam * Reclaim any resources in a node and reset any critical
895138568Ssam * state.  Typically nodes are free'd immediately after,
896138568Ssam * but in some cases the storage may be reused so we need
897138568Ssam * to insure consistent state (should probably fix that).
898138568Ssam */
899116742Ssamstatic void
900138568Ssamnode_cleanup(struct ieee80211_node *ni)
901116742Ssam{
902138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
903178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
904170530Ssam	int i;
905138568Ssam
906138568Ssam	/* NB: preserve ni_table */
907138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
908178354Ssam		if (vap->iv_opmode != IEEE80211_M_STA)
909178354Ssam			vap->iv_ps_sta--;
910138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
911178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
912178354Ssam		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
913138568Ssam	}
914147788Ssam	/*
915173273Ssam	 * Cleanup any HT-related state.
916173273Ssam	 */
917173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
918173273Ssam		ieee80211_ht_node_cleanup(ni);
919173273Ssam	/*
920147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
921147788Ssam	 * has happened.  This is probably not needed as the node
922147788Ssam	 * should always be removed from the table so not found but
923147788Ssam	 * do it just in case.
924186099Ssam	 * Likewise clear the ASSOCID flag as these flags are intended
925186099Ssam	 * to be managed in tandem.
926147788Ssam	 */
927186099Ssam	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
928138568Ssam
929138568Ssam	/*
930138568Ssam	 * Drain power save queue and, if needed, clear TIM.
931138568Ssam	 */
932184288Ssam	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
933178354Ssam		vap->iv_set_tim(ni, 0);
934138568Ssam
935138568Ssam	ni->ni_associd = 0;
936138568Ssam	if (ni->ni_challenge != NULL) {
937186302Ssam		free(ni->ni_challenge, M_80211_NODE);
938138568Ssam		ni->ni_challenge = NULL;
939138568Ssam	}
940138568Ssam	/*
941138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
942138568Ssam	 * reusable during a re-auth/re-assoc state transition.
943138568Ssam	 * If we remove these data they will not be recreated
944138568Ssam	 * because they come from a probe-response or beacon frame
945138568Ssam	 * which cannot be expected prior to the association-response.
946138568Ssam	 * This should not be an issue when operating in other modes
947138568Ssam	 * as stations leaving always go through a full state transition
948138568Ssam	 * which will rebuild this state.
949138568Ssam	 *
950138568Ssam	 * XXX does this leave us open to inheriting old state?
951138568Ssam	 */
952138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
953138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
954138568Ssam			m_freem(ni->ni_rxfrag[i]);
955138568Ssam			ni->ni_rxfrag[i] = NULL;
956138568Ssam		}
957148863Ssam	/*
958148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
959148863Ssam	 */
960148863Ssam	ieee80211_node_delucastkey(ni);
961138568Ssam#undef N
962116742Ssam}
963116742Ssam
964116742Ssamstatic void
965138568Ssamnode_free(struct ieee80211_node *ni)
966116742Ssam{
967138568Ssam	struct ieee80211com *ic = ni->ni_ic;
968138568Ssam
969138568Ssam	ic->ic_node_cleanup(ni);
970178354Ssam	ieee80211_ies_cleanup(&ni->ni_ies);
971184288Ssam	ieee80211_psq_cleanup(&ni->ni_psq);
972178354Ssam	IEEE80211_NODE_WDSQ_DESTROY(ni);
973186302Ssam	free(ni, M_80211_NODE);
974116742Ssam}
975116742Ssam
976178354Ssamstatic void
977178354Ssamnode_age(struct ieee80211_node *ni)
978178354Ssam{
979178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
980184303Ssam
981184303Ssam	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
982184303Ssam
983178354Ssam	/*
984178354Ssam	 * Age frames on the power save queue.
985178354Ssam	 */
986184288Ssam	if (ieee80211_node_psq_age(ni) != 0 &&
987184288Ssam	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
988178354Ssam		vap->iv_set_tim(ni, 0);
989178354Ssam	/*
990178354Ssam	 * Age frames on the wds pending queue.
991178354Ssam	 */
992178354Ssam	if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
993178354Ssam		ieee80211_node_wdsq_age(ni);
994178354Ssam	/*
995178354Ssam	 * Age out HT resources (e.g. frames on the
996178354Ssam	 * A-MPDU reorder queues).
997178354Ssam	 */
998178354Ssam	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
999178354Ssam		ieee80211_ht_node_age(ni);
1000178354Ssam}
1001178354Ssam
1002170530Ssamstatic int8_t
1003138568Ssamnode_getrssi(const struct ieee80211_node *ni)
1004120104Ssam{
1005178354Ssam	uint32_t avgrssi = ni->ni_avgrssi;
1006178354Ssam	int32_t rssi;
1007178354Ssam
1008178354Ssam	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1009178354Ssam		return 0;
1010178354Ssam	rssi = IEEE80211_RSSI_GET(avgrssi);
1011178354Ssam	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1012120104Ssam}
1013120104Ssam
1014116742Ssamstatic void
1015170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1016170530Ssam{
1017178354Ssam	*rssi = node_getrssi(ni);
1018170530Ssam	*noise = ni->ni_noise;
1019170530Ssam}
1020170530Ssam
1021170530Ssamstatic void
1022178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni,
1023178354Ssam	struct ieee80211_mimo_info *info)
1024116742Ssam{
1025178354Ssam	/* XXX zero data? */
1026178354Ssam}
1027178354Ssam
1028178354Ssamstruct ieee80211_node *
1029178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt,
1030178354Ssam	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1031178354Ssam{
1032138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1033178354Ssam	struct ieee80211_node *ni;
1034116742Ssam	int hash;
1035116742Ssam
1036179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1037178354Ssam	if (ni == NULL) {
1038178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1039178354Ssam		return NULL;
1040178354Ssam	}
1041178354Ssam
1042178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1043140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1044138568Ssam		ether_sprintf(macaddr), nt->nt_name);
1045138568Ssam
1046116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1047116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1048138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
1049138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
1050138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
1051138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
1052183251Ssam	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1053178354Ssam	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1054178354Ssam	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1055139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
1056139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1057170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
1058184288Ssam	ieee80211_psq_init(&ni->ni_psq, "unknown");
1059178354Ssam	IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1060138568Ssam
1061138568Ssam	IEEE80211_NODE_LOCK(nt);
1062138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1063138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1064138568Ssam	ni->ni_table = nt;
1065178354Ssam	ni->ni_vap = vap;
1066138568Ssam	ni->ni_ic = ic;
1067138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1068116742Ssam
1069184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1070184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1071184277Ssam
1072116742Ssam	return ni;
1073116742Ssam}
1074116742Ssam
1075148777Ssam/*
1076148777Ssam * Craft a temporary node suitable for sending a management frame
1077148777Ssam * to the specified station.  We craft only as much state as we
1078148777Ssam * need to do the work since the node will be immediately reclaimed
1079148777Ssam * once the send completes.
1080148777Ssam */
1081116742Ssamstruct ieee80211_node *
1082178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1083178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1084148777Ssam{
1085178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1086148777Ssam	struct ieee80211_node *ni;
1087148777Ssam
1088179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1089148777Ssam	if (ni != NULL) {
1090183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1091183259Ssam
1092178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1093148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1094148777Ssam
1095178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1096178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1097178354Ssam		ni->ni_vap = vap;
1098178354Ssam
1099148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1100183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1101148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1102148777Ssam		/* NB: required by ieee80211_fix_rate */
1103183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1104178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1105148777Ssam			IEEE80211_KEYIX_NONE);
1106183259Ssam		ni->ni_txpower = bss->ni_txpower;
1107148777Ssam		/* XXX optimize away */
1108184288Ssam		ieee80211_psq_init(&ni->ni_psq, "unknown");
1109178354Ssam		IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1110148777Ssam	} else {
1111148777Ssam		/* XXX msg */
1112178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1113148777Ssam	}
1114148777Ssam	return ni;
1115148777Ssam}
1116148777Ssam
1117148777Ssamstruct ieee80211_node *
1118178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1119178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1120116742Ssam{
1121178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1122138568Ssam	struct ieee80211_node *ni;
1123138568Ssam
1124178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1125116742Ssam	if (ni != NULL) {
1126183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1127127770Ssam		/*
1128178354Ssam		 * Inherit from iv_bss.
1129127770Ssam		 */
1130183259Ssam		copy_bss(ni, bss);
1131183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1132183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1133178354Ssam	}
1134116742Ssam	return ni;
1135116742Ssam}
1136116742Ssam
1137178354Ssam/*
1138178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1139178354Ssam * not associate so we just create create a new node and
1140178354Ssam * simulate an association.  The caller is responsible for
1141178354Ssam * installing the node as the bss node and handling any further
1142178354Ssam * setup work like authorizing the port.
1143178354Ssam */
1144178354Ssamstruct ieee80211_node *
1145178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1146178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1147178354Ssam{
1148178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1149178354Ssam	struct ieee80211_node *ni;
1150178354Ssam
1151178354Ssam	/* XXX check if node already in sta table? */
1152178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1153178354Ssam	if (ni != NULL) {
1154178354Ssam		ni->ni_wdsvap = vap;
1155178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1156178354Ssam		/*
1157178354Ssam		 * Inherit any manually configured settings.
1158178354Ssam		 */
1159183259Ssam		copy_bss(ni, vap->iv_bss);
1160178354Ssam		ieee80211_node_set_chan(ni, chan);
1161178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1162178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1163178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1164178354Ssam		/* NB: no associd for peer */
1165178354Ssam		/*
1166178354Ssam		 * There are no management frames to use to
1167178354Ssam		 * discover neighbor capabilities, so blindly
1168178354Ssam		 * propagate the local configuration.
1169178354Ssam		 */
1170178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1171178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1172178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1173178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1174178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1175178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1176178354Ssam			/*
1177178354Ssam			 * Device is HT-capable and HT is enabled for
1178178354Ssam			 * the vap; setup HT operation.  On return
1179178354Ssam			 * ni_chan will be adjusted to an HT channel.
1180178354Ssam			 */
1181178354Ssam			ieee80211_ht_wds_init(ni);
1182178354Ssam		} else {
1183178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1184178354Ssam			/*
1185178354Ssam			 * Force a legacy channel to be used.
1186178354Ssam			 */
1187178354Ssam			c = ieee80211_find_channel(ic,
1188178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1189178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1190178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1191178354Ssam			ni->ni_chan = c;
1192178354Ssam		}
1193178354Ssam	}
1194178354Ssam	return ni;
1195178354Ssam}
1196178354Ssam
1197178354Ssamstruct ieee80211_node *
1198138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1199178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1200178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1201138568Ssam#else
1202178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1203178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1204138568Ssam#endif
1205116742Ssam{
1206116742Ssam	struct ieee80211_node *ni;
1207116742Ssam	int hash;
1208116742Ssam
1209138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1210127772Ssam
1211116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1212138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1213116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1214138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1215138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1216178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1217140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1218140766Ssam			    func, line,
1219140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1220140766Ssam			    ieee80211_node_refcnt(ni));
1221138568Ssam#endif
1222127772Ssam			return ni;
1223116742Ssam		}
1224116742Ssam	}
1225127772Ssam	return NULL;
1226127772Ssam}
1227178354Ssam
1228178354Ssamstruct ieee80211_node *
1229138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1230178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1231178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1232178354Ssam#else
1233178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1234178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1235138568Ssam#endif
1236178354Ssam{
1237178354Ssam	struct ieee80211_node *ni;
1238127772Ssam
1239178354Ssam	IEEE80211_NODE_LOCK(nt);
1240178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1241178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1242178354Ssam	return ni;
1243178354Ssam}
1244178354Ssam
1245127772Ssamstruct ieee80211_node *
1246138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1247178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1248178354Ssam	const struct ieee80211vap *vap,
1249178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1250138568Ssam#else
1251178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1252178354Ssam	const struct ieee80211vap *vap,
1253178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1254138568Ssam#endif
1255127772Ssam{
1256127772Ssam	struct ieee80211_node *ni;
1257178354Ssam	int hash;
1258127772Ssam
1259178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1260178354Ssam
1261178354Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1262178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1263178354Ssam		if (ni->ni_vap == vap &&
1264178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1265178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1266178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1267178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1268178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1269178354Ssam			    func, line,
1270178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1271178354Ssam			    ieee80211_node_refcnt(ni));
1272178354Ssam#endif
1273178354Ssam			return ni;
1274178354Ssam		}
1275178354Ssam	}
1276178354Ssam	return NULL;
1277178354Ssam}
1278178354Ssam
1279178354Ssamstruct ieee80211_node *
1280178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1281178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1282178354Ssam	const struct ieee80211vap *vap,
1283178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1284178354Ssam#else
1285178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1286178354Ssam	const struct ieee80211vap *vap,
1287178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1288178354Ssam#endif
1289178354Ssam{
1290178354Ssam	struct ieee80211_node *ni;
1291178354Ssam
1292138568Ssam	IEEE80211_NODE_LOCK(nt);
1293178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1294138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1295116742Ssam	return ni;
1296116742Ssam}
1297116742Ssam
1298116742Ssam/*
1299138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1300138568Ssam * Note that for the driver's benefit we we treat this like
1301138568Ssam * an association so the driver has an opportunity to setup
1302138568Ssam * it's private state.
1303138568Ssam */
1304138568Ssamstruct ieee80211_node *
1305178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1306170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1307138568Ssam{
1308138568Ssam	struct ieee80211_node *ni;
1309138568Ssam
1310178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1311153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1312178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1313138568Ssam	if (ni != NULL) {
1314178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1315178354Ssam
1316138568Ssam		/* XXX no rate negotiation; just dup */
1317178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1318178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1319153404Ssam			/*
1320170530Ssam			 * In adhoc demo mode there are no management
1321170530Ssam			 * frames to use to discover neighbor capabilities,
1322170530Ssam			 * so blindly propagate the local configuration
1323170530Ssam			 * so we can do interesting things (e.g. use
1324170530Ssam			 * WME to disable ACK's).
1325153404Ssam			 */
1326178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1327153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1328178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1329170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1330153404Ssam		}
1331183251Ssam		node_setuptxparms(ni);
1332178354Ssam		if (ic->ic_newassoc != NULL)
1333178354Ssam			ic->ic_newassoc(ni, 1);
1334170530Ssam		/* XXX not right for 802.1x/WPA */
1335170530Ssam		ieee80211_node_authorize(ni);
1336138568Ssam	}
1337138568Ssam	return ni;
1338138568Ssam}
1339138568Ssam
1340148936Ssamvoid
1341153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1342153073Ssam	const struct ieee80211_frame *wh,
1343153073Ssam	const struct ieee80211_scanparams *sp)
1344153073Ssam{
1345153073Ssam	ni->ni_esslen = sp->ssid[1];
1346153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1347153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1348153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1349153073Ssam	ni->ni_intval = sp->bintval;
1350153073Ssam	ni->ni_capinfo = sp->capinfo;
1351153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1352153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1353153073Ssam	ni->ni_fhindex = sp->fhindex;
1354153073Ssam	ni->ni_erp = sp->erp;
1355153073Ssam	ni->ni_timoff = sp->timoff;
1356153073Ssam
1357178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1358178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1359186659Ssam		if (ni->ni_ies.wme_ie != NULL)
1360186659Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1361186659Ssam		else
1362186659Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1363178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1364178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1365178354Ssam	}
1366178354Ssam
1367153073Ssam	/* NB: must be after ni_chan is setup */
1368165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1369165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1370165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1371153073Ssam}
1372153073Ssam
1373148936Ssam/*
1374148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1375148936Ssam * or probe response frame.  Note that for the driver's
1376148936Ssam * benefit we we treat this like an association so the
1377148936Ssam * driver has an opportunity to setup it's private state.
1378148936Ssam */
1379148936Ssamstruct ieee80211_node *
1380178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1381148936Ssam	const struct ieee80211_frame *wh,
1382148936Ssam	const struct ieee80211_scanparams *sp)
1383148936Ssam{
1384148936Ssam	struct ieee80211_node *ni;
1385148936Ssam
1386178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1387153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1388178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1389148936Ssam	if (ni != NULL) {
1390178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1391178354Ssam
1392153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1393183251Ssam		node_setuptxparms(ni);
1394148936Ssam		if (ic->ic_newassoc != NULL)
1395148936Ssam			ic->ic_newassoc(ni, 1);
1396148936Ssam		/* XXX not right for 802.1x/WPA */
1397148936Ssam		ieee80211_node_authorize(ni);
1398148936Ssam	}
1399148936Ssam	return ni;
1400148936Ssam}
1401148936Ssam
1402148863Ssam#define	IS_CTL(wh) \
1403148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1404148863Ssam#define	IS_PSPOLL(wh) \
1405148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1406170530Ssam#define	IS_BAR(wh) \
1407170530Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1408179220Ssam#define	IS_PROBEREQ(wh) \
1409179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1410179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1411179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1412179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1413179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1414170530Ssam
1415179220Ssamstatic __inline struct ieee80211_node *
1416179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1417179220Ssam    const struct ieee80211_frame_min *wh)
1418179220Ssam{
1419179220Ssam	/* XXX 4-address frames? */
1420179220Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1421179220Ssam		return ieee80211_find_node_locked(nt, wh->i_addr1);
1422179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1423179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1424179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1425179220Ssam}
1426179220Ssam
1427138568Ssam/*
1428138568Ssam * Locate the node for sender, track state, and then pass the
1429179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1430179220Ssam * we can return NULL if the sender is not in the table.
1431138568Ssam */
1432138568Ssamstruct ieee80211_node *
1433138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1434138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1435138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1436138568Ssam#else
1437138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1438138568Ssam	const struct ieee80211_frame_min *wh)
1439138568Ssam#endif
1440138568Ssam{
1441138568Ssam	struct ieee80211_node_table *nt;
1442138568Ssam	struct ieee80211_node *ni;
1443138568Ssam
1444170530Ssam	nt = &ic->ic_sta;
1445138568Ssam	IEEE80211_NODE_LOCK(nt);
1446179220Ssam	ni = _find_rxnode(nt, wh);
1447138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1448138568Ssam
1449148863Ssam	return ni;
1450148863Ssam}
1451148863Ssam
1452148863Ssam/*
1453148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1454148863Ssam * key index as a hint to locate the node in the key
1455148863Ssam * mapping table.  If an entry is present at the key
1456148863Ssam * index we return it; otherwise do a normal lookup and
1457148863Ssam * update the mapping table if the station has a unicast
1458148863Ssam * key assigned to it.
1459148863Ssam */
1460148863Ssamstruct ieee80211_node *
1461148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1462148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1463148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1464148863Ssam	const char *func, int line)
1465148863Ssam#else
1466148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1467148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1468148863Ssam#endif
1469148863Ssam{
1470148863Ssam	struct ieee80211_node_table *nt;
1471148863Ssam	struct ieee80211_node *ni;
1472148863Ssam
1473170530Ssam	nt = &ic->ic_sta;
1474148863Ssam	IEEE80211_NODE_LOCK(nt);
1475148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1476148863Ssam		ni = nt->nt_keyixmap[keyix];
1477148863Ssam	else
1478148863Ssam		ni = NULL;
1479148863Ssam	if (ni == NULL) {
1480179220Ssam		ni = _find_rxnode(nt, wh);
1481178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1482148863Ssam			/*
1483148863Ssam			 * If the station has a unicast key cache slot
1484148863Ssam			 * assigned update the key->node mapping table.
1485148863Ssam			 */
1486148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1487148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1488148863Ssam			if (keyix < nt->nt_keyixmax &&
1489148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1490178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1491178354Ssam				    IEEE80211_MSG_NODE,
1492148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1493148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1494148863Ssam				    ieee80211_node_refcnt(ni)+1);
1495148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1496148863Ssam			}
1497148863Ssam		}
1498179220Ssam	} else {
1499179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1500179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1501179220Ssam		else
1502179220Ssam			ieee80211_ref_node(ni);
1503179220Ssam	}
1504148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1505148863Ssam
1506148863Ssam	return ni;
1507148863Ssam}
1508179220Ssam#undef IS_BCAST_PROBEREQ
1509179220Ssam#undef IS_PROBEREQ
1510170530Ssam#undef IS_BAR
1511138568Ssam#undef IS_PSPOLL
1512138568Ssam#undef IS_CTL
1513138568Ssam
1514138568Ssam/*
1515127772Ssam * Return a reference to the appropriate node for sending
1516127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1517127772Ssam */
1518127772Ssamstruct ieee80211_node *
1519138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1520178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1521178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1522138568Ssam	const char *func, int line)
1523138568Ssam#else
1524178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1525178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1526138568Ssam#endif
1527127772Ssam{
1528178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1529127772Ssam	struct ieee80211_node *ni;
1530127772Ssam
1531127772Ssam	/*
1532127772Ssam	 * The destination address should be in the node table
1533148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1534148863Ssam	 * also optimize station mode operation, all frames go
1535148863Ssam	 * to the bss node.
1536127772Ssam	 */
1537127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1538138568Ssam	IEEE80211_NODE_LOCK(nt);
1539178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1540178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1541178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1542178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1543186099Ssam	else
1544178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1545138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1546138568Ssam
1547138568Ssam	if (ni == NULL) {
1548178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1549178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1550140497Ssam			/*
1551140497Ssam			 * In adhoc mode cons up a node for the destination.
1552140497Ssam			 * Note that we need an additional reference for the
1553178354Ssam			 * caller to be consistent with
1554178354Ssam			 * ieee80211_find_node_locked.
1555140497Ssam			 */
1556178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1557140497Ssam			if (ni != NULL)
1558140497Ssam				(void) ieee80211_ref_node(ni);
1559140497Ssam		} else {
1560178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1561178354Ssam			    "no node, discard frame (%s)", __func__);
1562178354Ssam			vap->iv_stats.is_tx_nonode++;
1563127772Ssam		}
1564127772Ssam	}
1565127772Ssam	return ni;
1566127772Ssam}
1567127772Ssam
1568116742Ssamstatic void
1569138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1570116742Ssam{
1571138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1572119150Ssam
1573179641Ssam	/*
1574179641Ssam	 * NB: careful about referencing the vap as it may be
1575179641Ssam	 * gone if the last reference was held by a driver.
1576179641Ssam	 * We know the com will always be present so it's safe
1577179641Ssam	 * to use ni_ic below to reclaim resources.
1578179641Ssam	 */
1579179641Ssam#if 0
1580178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1581140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1582140766Ssam		ether_sprintf(ni->ni_macaddr),
1583138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1584179641Ssam#endif
1585179641Ssam	if (ni->ni_associd != 0) {
1586179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1587179641Ssam		if (vap->iv_aid_bitmap != NULL)
1588179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1589179641Ssam	}
1590138568Ssam	if (nt != NULL) {
1591138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1592138568Ssam		LIST_REMOVE(ni, ni_hash);
1593138568Ssam	}
1594179641Ssam	ni->ni_ic->ic_node_free(ni);
1595116742Ssam}
1596116742Ssam
1597116742Ssamvoid
1598138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1599138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1600138568Ssam#else
1601138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1602138568Ssam#endif
1603116742Ssam{
1604138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1605119150Ssam
1606138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1607178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1608140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1609138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1610138568Ssam#endif
1611148863Ssam	if (nt != NULL) {
1612148863Ssam		IEEE80211_NODE_LOCK(nt);
1613148863Ssam		if (ieee80211_node_dectestref(ni)) {
1614148863Ssam			/*
1615148863Ssam			 * Last reference, reclaim state.
1616148863Ssam			 */
1617138568Ssam			_ieee80211_free_node(ni);
1618148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1619148863Ssam		    nt->nt_keyixmap != NULL) {
1620148863Ssam			ieee80211_keyix keyix;
1621148863Ssam			/*
1622148863Ssam			 * Check for a last reference in the key mapping table.
1623148863Ssam			 */
1624148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1625148863Ssam			if (keyix < nt->nt_keyixmax &&
1626148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1627178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1628178354Ssam				    IEEE80211_MSG_NODE,
1629148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1630148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1631148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1632148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1633148863Ssam				_ieee80211_free_node(ni);
1634148863Ssam			}
1635148863Ssam		}
1636148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1637148863Ssam	} else {
1638148863Ssam		if (ieee80211_node_dectestref(ni))
1639138568Ssam			_ieee80211_free_node(ni);
1640116742Ssam	}
1641116742Ssam}
1642116742Ssam
1643138568Ssam/*
1644148863Ssam * Reclaim a unicast key and clear any key cache state.
1645148863Ssam */
1646148863Ssamint
1647148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1648148863Ssam{
1649179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1650179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1651148863Ssam	struct ieee80211_node *nikey;
1652148863Ssam	ieee80211_keyix keyix;
1653148863Ssam	int isowned, status;
1654148863Ssam
1655148863Ssam	/*
1656148863Ssam	 * NB: We must beware of LOR here; deleting the key
1657148863Ssam	 * can cause the crypto layer to block traffic updates
1658148863Ssam	 * which can generate a LOR against the node table lock;
1659148863Ssam	 * grab it here and stash the key index for our use below.
1660148863Ssam	 *
1661148863Ssam	 * Must also beware of recursion on the node table lock.
1662148863Ssam	 * When called from node_cleanup we may already have
1663148863Ssam	 * the node table lock held.  Unfortunately there's no
1664148863Ssam	 * way to separate out this path so we must do this
1665148863Ssam	 * conditionally.
1666148863Ssam	 */
1667148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1668148863Ssam	if (!isowned)
1669148863Ssam		IEEE80211_NODE_LOCK(nt);
1670179641Ssam	nikey = NULL;
1671179641Ssam	status = 1;		/* NB: success */
1672186144Ssam	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1673179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1674179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1675179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1676179641Ssam			nikey = nt->nt_keyixmap[keyix];
1677179641Ssam			nt->nt_keyixmap[keyix] = NULL;;
1678179641Ssam		}
1679179641Ssam	}
1680148863Ssam	if (!isowned)
1681178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1682148863Ssam
1683148863Ssam	if (nikey != NULL) {
1684148863Ssam		KASSERT(nikey == ni,
1685148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1686179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1687148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1688148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1689148863Ssam			ieee80211_node_refcnt(ni)-1);
1690148863Ssam		ieee80211_free_node(ni);
1691148863Ssam	}
1692148863Ssam	return status;
1693148863Ssam}
1694148863Ssam
1695148863Ssam/*
1696138568Ssam * Reclaim a node.  If this is the last reference count then
1697138568Ssam * do the normal free work.  Otherwise remove it from the node
1698138568Ssam * table and mark it gone by clearing the back-reference.
1699138568Ssam */
1700138568Ssamstatic void
1701138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1702116742Ssam{
1703148863Ssam	ieee80211_keyix keyix;
1704138568Ssam
1705148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1706148863Ssam
1707178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1708140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1709140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1710140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1711148863Ssam	/*
1712148863Ssam	 * Clear any entry in the unicast key mapping table.
1713148863Ssam	 * We need to do it here so rx lookups don't find it
1714148863Ssam	 * in the mapping table even if it's not in the hash
1715148863Ssam	 * table.  We cannot depend on the mapping table entry
1716148863Ssam	 * being cleared because the node may not be free'd.
1717148863Ssam	 */
1718148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1719148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1720148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1721178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1722188494Ssam			"%s: %p<%s> clear key map entry %u\n",
1723188494Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1724148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1725148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1726148863Ssam	}
1727138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1728138568Ssam		/*
1729138568Ssam		 * Other references are present, just remove the
1730138568Ssam		 * node from the table so it cannot be found.  When
1731138568Ssam		 * the references are dropped storage will be
1732140753Ssam		 * reclaimed.
1733138568Ssam		 */
1734138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1735138568Ssam		LIST_REMOVE(ni, ni_hash);
1736138568Ssam		ni->ni_table = NULL;		/* clear reference */
1737138568Ssam	} else
1738138568Ssam		_ieee80211_free_node(ni);
1739138568Ssam}
1740138568Ssam
1741178354Ssam/*
1742178354Ssam * Reclaim a (bss) node.  Decrement the refcnt and reclaim
1743178354Ssam * the node if the only other reference to it is in the sta
1744178354Ssam * table.  This is effectively ieee80211_free_node followed
1745178354Ssam * by node_reclaim when the refcnt is 1 (after the free).
1746178354Ssam */
1747138568Ssamstatic void
1748178354Ssamieee80211_node_reclaim(struct ieee80211_node *ni)
1749138568Ssam{
1750178354Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1751116742Ssam
1752178354Ssam	KASSERT(nt != NULL, ("reclaim node not in table"));
1753138568Ssam
1754178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1755178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1756178712Ssam		"%s %p<%s> refcnt %d\n", __func__, ni,
1757178354Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1758178354Ssam#endif
1759178354Ssam	IEEE80211_NODE_LOCK(nt);
1760178354Ssam	if (ieee80211_node_dectestref(ni)) {
1761178354Ssam		/*
1762178354Ssam		 * Last reference, reclaim state.
1763178354Ssam		 */
1764178354Ssam		_ieee80211_free_node(ni);
1765178354Ssam		nt = NULL;
1766188494Ssam	} else if (ieee80211_node_refcnt(ni) == 1 && nt->nt_keyixmap != NULL) {
1767178354Ssam		ieee80211_keyix keyix;
1768178354Ssam		/*
1769178354Ssam		 * Check for a last reference in the key mapping table.
1770178354Ssam		 */
1771178354Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1772178354Ssam		if (keyix < nt->nt_keyixmax &&
1773178354Ssam		    nt->nt_keyixmap[keyix] == ni) {
1774178354Ssam			IEEE80211_DPRINTF(ni->ni_vap,
1775178354Ssam			    IEEE80211_MSG_NODE,
1776188494Ssam			    "%s: %p<%s> clear key map entry %u", __func__,
1777188494Ssam			    ni, ether_sprintf(ni->ni_macaddr), keyix);
1778178354Ssam			nt->nt_keyixmap[keyix] = NULL;
1779178354Ssam			ieee80211_node_decref(ni); /* XXX needed? */
1780178354Ssam			_ieee80211_free_node(ni);
1781178354Ssam			nt = NULL;
1782178354Ssam		}
1783178354Ssam	}
1784178354Ssam	if (nt != NULL && ieee80211_node_refcnt(ni) == 1) {
1785178354Ssam		/*
1786178354Ssam		 * Last reference is in the sta table; complete
1787178354Ssam		 * the reclaim.  This handles bss nodes being
1788178354Ssam		 * recycled: the node has two references, one for
1789178354Ssam		 * iv_bss and one for the table.  After dropping
1790178354Ssam		 * the iv_bss ref above we need to reclaim the sta
1791178354Ssam		 * table reference.
1792178354Ssam		 */
1793178354Ssam		ieee80211_node_decref(ni);	/* NB: be pendantic */
1794178354Ssam		_ieee80211_free_node(ni);
1795178354Ssam	}
1796178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1797178354Ssam}
1798178354Ssam
1799178354Ssam/*
1800178354Ssam * Node table support.
1801178354Ssam */
1802178354Ssam
1803178354Ssamstatic void
1804178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1805178354Ssam	struct ieee80211_node_table *nt,
1806178354Ssam	const char *name, int inact, int keyixmax)
1807178354Ssam{
1808178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1809178354Ssam
1810178354Ssam	nt->nt_ic = ic;
1811178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1812178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1813178354Ssam	TAILQ_INIT(&nt->nt_node);
1814178354Ssam	nt->nt_name = name;
1815178354Ssam	nt->nt_scangen = 1;
1816178354Ssam	nt->nt_inact_init = inact;
1817178354Ssam	nt->nt_keyixmax = keyixmax;
1818178354Ssam	if (nt->nt_keyixmax > 0) {
1819186302Ssam		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1820184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1821178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1822178354Ssam		if (nt->nt_keyixmap == NULL)
1823178354Ssam			if_printf(ic->ic_ifp,
1824178354Ssam			    "Cannot allocate key index map with %u entries\n",
1825178354Ssam			    keyixmax);
1826178354Ssam	} else
1827178354Ssam		nt->nt_keyixmap = NULL;
1828178354Ssam}
1829178354Ssam
1830178354Ssamstatic void
1831178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1832178354Ssam	struct ieee80211vap *match)
1833178354Ssam{
1834178354Ssam	struct ieee80211_node *ni, *next;
1835178354Ssam
1836178354Ssam	IEEE80211_NODE_LOCK(nt);
1837178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1838178354Ssam		if (match != NULL && ni->ni_vap != match)
1839178354Ssam			continue;
1840178354Ssam		/* XXX can this happen?  if so need's work */
1841138568Ssam		if (ni->ni_associd != 0) {
1842178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1843178354Ssam
1844178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1845178354Ssam				vap->iv_auth->ia_node_leave(ni);
1846178354Ssam			if (vap->iv_aid_bitmap != NULL)
1847178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1848138568Ssam		}
1849178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1850138568Ssam		node_reclaim(nt, ni);
1851138568Ssam	}
1852178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1853178354Ssam		/*
1854178354Ssam		 * Make a separate pass to clear references to this vap
1855178354Ssam		 * held by DWDS entries.  They will not be matched above
1856178354Ssam		 * because ni_vap will point to the ap vap but we still
1857178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1858178354Ssam		 * and/or reset.
1859178354Ssam		 */
1860178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1861178354Ssam			if (ni->ni_wdsvap == match)
1862178354Ssam				ni->ni_wdsvap = NULL;
1863178354Ssam	}
1864178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1865116742Ssam}
1866116742Ssam
1867178354Ssamstatic void
1868178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1869178354Ssam{
1870178354Ssam	ieee80211_node_table_reset(nt, NULL);
1871178354Ssam	if (nt->nt_keyixmap != NULL) {
1872178354Ssam#ifdef DIAGNOSTIC
1873178354Ssam		/* XXX verify all entries are NULL */
1874178354Ssam		int i;
1875178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1876178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1877178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1878178354Ssam					nt->nt_name, i);
1879178354Ssam#endif
1880186302Ssam		free(nt->nt_keyixmap, M_80211_NODE);
1881178354Ssam		nt->nt_keyixmap = NULL;
1882178354Ssam	}
1883178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1884178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1885178354Ssam}
1886178354Ssam
1887120483Ssam/*
1888138568Ssam * Timeout inactive stations and do related housekeeping.
1889138568Ssam * Note that we cannot hold the node lock while sending a
1890138568Ssam * frame as this would lead to a LOR.  Instead we use a
1891138568Ssam * generation number to mark nodes that we've scanned and
1892138568Ssam * drop the lock and restart a scan if we have to time out
1893138568Ssam * a node.  Since we are single-threaded by virtue of
1894120483Ssam * controlling the inactivity timer we can be sure this will
1895120483Ssam * process each node only once.
1896120483Ssam */
1897138568Ssamstatic void
1898178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1899116742Ssam{
1900178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1901178354Ssam	struct ieee80211vap *vap;
1902120483Ssam	struct ieee80211_node *ni;
1903178354Ssam	int gen = 0;
1904116742Ssam
1905178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
1906154532Ssam	gen = ++nt->nt_scangen;
1907120483Ssamrestart:
1908138568Ssam	IEEE80211_NODE_LOCK(nt);
1909138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1910120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1911120483Ssam			continue;
1912120483Ssam		ni->ni_scangen = gen;
1913138568Ssam		/*
1914147788Ssam		 * Ignore entries for which have yet to receive an
1915147788Ssam		 * authentication frame.  These are transient and
1916147788Ssam		 * will be reclaimed when the last reference to them
1917147788Ssam		 * goes away (when frame xmits complete).
1918147788Ssam		 */
1919178354Ssam		vap = ni->ni_vap;
1920178354Ssam		/*
1921178354Ssam		 * Only process stations when in RUN state.  This
1922178354Ssam		 * insures, for example, that we don't timeout an
1923178354Ssam		 * inactive station during CAC.  Note that CSA state
1924178354Ssam		 * is actually handled in ieee80211_node_timeout as
1925178354Ssam		 * it applies to more than timeout processing.
1926178354Ssam		 */
1927178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
1928178354Ssam			continue;
1929178354Ssam		/* XXX can vap be NULL? */
1930178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1931178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
1932148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1933147788Ssam			continue;
1934147788Ssam		/*
1935138568Ssam		 * Free fragment if not needed anymore
1936138568Ssam		 * (last fragment older than 1s).
1937178354Ssam		 * XXX doesn't belong here, move to node_age
1938138568Ssam		 */
1939138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1940138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1941138568Ssam			m_freem(ni->ni_rxfrag[0]);
1942138568Ssam			ni->ni_rxfrag[0] = NULL;
1943138568Ssam		}
1944184277Ssam		if (ni->ni_inact > 0) {
1945172062Ssam			ni->ni_inact--;
1946184277Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1947184277Ssam			    "%s: inact %u inact_reload %u nrates %u",
1948184277Ssam			    __func__, ni->ni_inact, ni->ni_inact_reload,
1949184277Ssam			    ni->ni_rates.rs_nrates);
1950184277Ssam		}
1951140498Ssam		/*
1952140498Ssam		 * Special case ourself; we may be idle for extended periods
1953140498Ssam		 * of time and regardless reclaiming our state is wrong.
1954178354Ssam		 * XXX run ic_node_age
1955140498Ssam		 */
1956178354Ssam		if (ni == vap->iv_bss)
1957140498Ssam			continue;
1958178354Ssam		if (ni->ni_associd != 0 ||
1959178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1960178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1961119150Ssam			/*
1962178354Ssam			 * Age/drain resources held by the station.
1963138568Ssam			 */
1964178354Ssam			ic->ic_node_age(ni);
1965138568Ssam			/*
1966138568Ssam			 * Probe the station before time it out.  We
1967138568Ssam			 * send a null data frame which may not be
1968138568Ssam			 * universally supported by drivers (need it
1969138568Ssam			 * for ps-poll support so it should be...).
1970170530Ssam			 *
1971170530Ssam			 * XXX don't probe the station unless we've
1972170530Ssam			 *     received a frame from them (and have
1973170530Ssam			 *     some idea of the rates they are capable
1974170530Ssam			 *     of); this will get fixed more properly
1975170530Ssam			 *     soon with better handling of the rate set.
1976138568Ssam			 */
1977178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1978172062Ssam			    (0 < ni->ni_inact &&
1979178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
1980170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1981178354Ssam				IEEE80211_NOTE(vap,
1982148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1983148320Ssam				    ni, "%s",
1984148320Ssam				    "probe station due to inactivity");
1985148582Ssam				/*
1986148582Ssam				 * Grab a reference before unlocking the table
1987148582Ssam				 * so the node cannot be reclaimed before we
1988148582Ssam				 * send the frame. ieee80211_send_nulldata
1989148582Ssam				 * understands we've done this and reclaims the
1990148582Ssam				 * ref for us as needed.
1991148582Ssam				 */
1992148582Ssam				ieee80211_ref_node(ni);
1993138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1994148301Ssam				ieee80211_send_nulldata(ni);
1995138568Ssam				/* XXX stat? */
1996138568Ssam				goto restart;
1997138568Ssam			}
1998138568Ssam		}
1999178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2000172062Ssam		    ni->ni_inact <= 0) {
2001178354Ssam			IEEE80211_NOTE(vap,
2002148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2003148320Ssam			    "station timed out due to inactivity "
2004148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
2005138568Ssam			/*
2006138568Ssam			 * Send a deauthenticate frame and drop the station.
2007138568Ssam			 * This is somewhat complicated due to reference counts
2008138568Ssam			 * and locking.  At this point a station will typically
2009138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
2010138568Ssam			 * will do a "free" of the node which will drop the
2011138568Ssam			 * reference count.  But in the meantime a reference
2012138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
2013138568Ssam			 * of the node will happen either after the tx is
2014138568Ssam			 * completed or by ieee80211_node_leave.
2015120483Ssam			 *
2016138568Ssam			 * Separately we must drop the node lock before sending
2017170530Ssam			 * in case the driver takes a lock, as this can result
2018170530Ssam			 * in a LOR between the node lock and the driver lock.
2019119150Ssam			 */
2020172229Ssam			ieee80211_ref_node(ni);
2021138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2022138568Ssam			if (ni->ni_associd != 0) {
2023178354Ssam				IEEE80211_SEND_MGMT(ni,
2024138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
2025138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
2026138568Ssam			}
2027178354Ssam			ieee80211_node_leave(ni);
2028172229Ssam			ieee80211_free_node(ni);
2029178354Ssam			vap->iv_stats.is_node_timeout++;
2030120483Ssam			goto restart;
2031120483Ssam		}
2032116742Ssam	}
2033138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2034138568Ssam
2035178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2036170530Ssam}
2037138568Ssam
2038178354Ssam/*
2039178354Ssam * Aggressively reclaim resources.  This should be used
2040178354Ssam * only in a critical situation to reclaim mbuf resources.
2041178354Ssam */
2042170530Ssamvoid
2043178354Ssamieee80211_drain(struct ieee80211com *ic)
2044178354Ssam{
2045178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2046178354Ssam	struct ieee80211vap *vap;
2047178354Ssam	struct ieee80211_node *ni;
2048178354Ssam
2049178354Ssam	IEEE80211_NODE_LOCK(nt);
2050178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2051178354Ssam		/*
2052178354Ssam		 * Ignore entries for which have yet to receive an
2053178354Ssam		 * authentication frame.  These are transient and
2054178354Ssam		 * will be reclaimed when the last reference to them
2055178354Ssam		 * goes away (when frame xmits complete).
2056178354Ssam		 */
2057178354Ssam		vap = ni->ni_vap;
2058178354Ssam		/*
2059178354Ssam		 * Only process stations when in RUN state.  This
2060178354Ssam		 * insures, for example, that we don't timeout an
2061178354Ssam		 * inactive station during CAC.  Note that CSA state
2062178354Ssam		 * is actually handled in ieee80211_node_timeout as
2063178354Ssam		 * it applies to more than timeout processing.
2064178354Ssam		 */
2065178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2066178354Ssam			continue;
2067178354Ssam		/* XXX can vap be NULL? */
2068178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2069178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2070178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2071178354Ssam			continue;
2072178354Ssam		/*
2073178354Ssam		 * Free fragments.
2074178354Ssam		 * XXX doesn't belong here, move to node_drain
2075178354Ssam		 */
2076178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2077178354Ssam			m_freem(ni->ni_rxfrag[0]);
2078178354Ssam			ni->ni_rxfrag[0] = NULL;
2079178354Ssam		}
2080178354Ssam		/*
2081178354Ssam		 * Drain resources held by the station.
2082178354Ssam		 */
2083178354Ssam		ic->ic_node_drain(ni);
2084178354Ssam	}
2085178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2086178354Ssam}
2087178354Ssam
2088178354Ssam/*
2089178354Ssam * Per-ieee80211com inactivity timer callback.
2090178354Ssam */
2091178354Ssamvoid
2092170530Ssamieee80211_node_timeout(void *arg)
2093170530Ssam{
2094170530Ssam	struct ieee80211com *ic = arg;
2095170530Ssam
2096178354Ssam	/*
2097178354Ssam	 * Defer timeout processing if a channel switch is pending.
2098178354Ssam	 * We typically need to be mute so not doing things that
2099178354Ssam	 * might generate frames is good to handle in one place.
2100178354Ssam	 * Supressing the station timeout processing may extend the
2101178354Ssam	 * lifetime of inactive stations (by not decrementing their
2102178354Ssam	 * idle counters) but this should be ok unless the CSA is
2103178354Ssam	 * active for an unusually long time.
2104178354Ssam	 */
2105178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2106178354Ssam		ieee80211_scan_timeout(ic);
2107178354Ssam		ieee80211_timeout_stations(ic);
2108170530Ssam
2109178354Ssam		IEEE80211_LOCK(ic);
2110178354Ssam		ieee80211_erp_timeout(ic);
2111178354Ssam		ieee80211_ht_timeout(ic);
2112178354Ssam		IEEE80211_UNLOCK(ic);
2113178354Ssam	}
2114170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2115170530Ssam		ieee80211_node_timeout, ic);
2116116742Ssam}
2117116742Ssam
2118116742Ssamvoid
2119178354Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2120178354Ssam	ieee80211_iter_func *f, void *arg)
2121116742Ssam{
2122116742Ssam	struct ieee80211_node *ni;
2123138568Ssam	u_int gen;
2124116742Ssam
2125178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2126154532Ssam	gen = ++nt->nt_scangen;
2127138568Ssamrestart:
2128138568Ssam	IEEE80211_NODE_LOCK(nt);
2129138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2130138568Ssam		if (ni->ni_scangen != gen) {
2131138568Ssam			ni->ni_scangen = gen;
2132138568Ssam			(void) ieee80211_ref_node(ni);
2133138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2134138568Ssam			(*f)(arg, ni);
2135138568Ssam			ieee80211_free_node(ni);
2136138568Ssam			goto restart;
2137138568Ssam		}
2138138568Ssam	}
2139138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2140138568Ssam
2141178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2142116742Ssam}
2143138568Ssam
2144138568Ssamvoid
2145138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2146138568Ssam{
2147138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2148138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2149138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2150138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2151138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2152138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2153138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2154167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2155167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2156167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2157138568Ssam		ni->ni_rxfragstamp);
2158170530Ssam	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2159178354Ssam		ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
2160170530Ssam		ni->ni_intval, ni->ni_capinfo);
2161138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2162138568Ssam		ether_sprintf(ni->ni_bssid),
2163138568Ssam		ni->ni_esslen, ni->ni_essid,
2164138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2165184277Ssam	printf("\tinact %u inact_reload %u txrate %u\n",
2166184277Ssam		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2167170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2168170530Ssam		ni->ni_htcap, ni->ni_htparam,
2169170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2170170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2171170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2172138568Ssam}
2173138568Ssam
2174138568Ssamvoid
2175138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2176138568Ssam{
2177138568Ssam	ieee80211_iterate_nodes(nt,
2178138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2179138568Ssam}
2180138568Ssam
2181179642Ssamstatic void
2182179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2183172211Ssam{
2184178354Ssam	struct ieee80211vap *vap;
2185178354Ssam
2186178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2187178354Ssam
2188178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2189178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2190178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2191172211Ssam}
2192172211Ssam
2193179642Ssamvoid
2194179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2195179642Ssam{
2196179642Ssam	IEEE80211_LOCK(ic);
2197179642Ssam	ieee80211_notify_erp_locked(ic);
2198179642Ssam	IEEE80211_UNLOCK(ic);
2199179642Ssam}
2200179642Ssam
2201138568Ssam/*
2202138568Ssam * Handle a station joining an 11g network.
2203138568Ssam */
2204138568Ssamstatic void
2205178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2206138568Ssam{
2207178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2208138568Ssam
2209173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2210173273Ssam
2211138568Ssam	/*
2212138568Ssam	 * Station isn't capable of short slot time.  Bump
2213138568Ssam	 * the count of long slot time stations and disable
2214138568Ssam	 * use of short slot time.  Note that the actual switch
2215138568Ssam	 * over to long slot time use may not occur until the
2216138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2217138568Ssam	 */
2218138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2219138568Ssam		ic->ic_longslotsta++;
2220178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2221172211Ssam		    "station needs long slot time, count %d",
2222172211Ssam		    ic->ic_longslotsta);
2223138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2224170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2225170530Ssam			/*
2226170530Ssam			 * Don't force slot time when switched to turbo
2227170530Ssam			 * mode as non-ERP stations won't be present; this
2228170530Ssam			 * need only be done when on the normal G channel.
2229170530Ssam			 */
2230170530Ssam			ieee80211_set_shortslottime(ic, 0);
2231170530Ssam		}
2232138568Ssam	}
2233138568Ssam	/*
2234138568Ssam	 * If the new station is not an ERP station
2235138568Ssam	 * then bump the counter and enable protection
2236138568Ssam	 * if configured.
2237138568Ssam	 */
2238178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2239138568Ssam		ic->ic_nonerpsta++;
2240178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2241172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2242172211Ssam		    ic->ic_nonerpsta);
2243138568Ssam		/*
2244138568Ssam		 * If station does not support short preamble
2245138568Ssam		 * then we must enable use of Barker preamble.
2246138568Ssam		 */
2247138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2248178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2249172211Ssam			    "%s", "station needs long preamble");
2250138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2251138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2252138568Ssam		}
2253172211Ssam		/*
2254178354Ssam		 * If protection is configured and this is the first
2255178354Ssam		 * indication we should use protection, enable it.
2256172211Ssam		 */
2257172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2258172211Ssam		    ic->ic_nonerpsta == 1 &&
2259172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2260178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2261172211Ssam			    "%s: enable use of protection\n", __func__);
2262172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2263179642Ssam			ieee80211_notify_erp_locked(ic);
2264172211Ssam		}
2265138568Ssam	} else
2266138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2267138568Ssam}
2268138568Ssam
2269138568Ssamvoid
2270178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2271138568Ssam{
2272178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2273178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2274138568Ssam	int newassoc;
2275138568Ssam
2276138568Ssam	if (ni->ni_associd == 0) {
2277170530Ssam		uint16_t aid;
2278138568Ssam
2279178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2280138568Ssam		/*
2281138568Ssam		 * It would be good to search the bitmap
2282138568Ssam		 * more efficiently, but this will do for now.
2283138568Ssam		 */
2284178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2285178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2286138568Ssam				break;
2287138568Ssam		}
2288178354Ssam		if (aid >= vap->iv_max_aid) {
2289179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2290178354Ssam			ieee80211_node_leave(ni);
2291138568Ssam			return;
2292138568Ssam		}
2293138568Ssam		ni->ni_associd = aid | 0xc000;
2294173273Ssam		ni->ni_jointime = time_uptime;
2295178354Ssam		IEEE80211_LOCK(ic);
2296178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2297178354Ssam		vap->iv_sta_assoc++;
2298138568Ssam		ic->ic_sta_assoc++;
2299173273Ssam
2300173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2301173273Ssam			ieee80211_ht_node_join(ni);
2302170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2303170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2304178354Ssam			ieee80211_node_join_11g(ni);
2305173273Ssam		IEEE80211_UNLOCK(ic);
2306173273Ssam
2307173273Ssam		newassoc = 1;
2308138568Ssam	} else
2309138568Ssam		newassoc = 0;
2310138568Ssam
2311178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2312183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2313139523Ssam	    IEEE80211_NODE_AID(ni),
2314139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2315139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2316139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2317170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2318170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2319182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2320173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2321183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2322183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2323183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2324178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2325170530Ssam		", fast-frames" : "",
2326178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2327170530Ssam		", turbo" : ""
2328139523Ssam	);
2329138568Ssam
2330183251Ssam	node_setuptxparms(ni);
2331138568Ssam	/* give driver a chance to setup state like ni_txrate */
2332139524Ssam	if (ic->ic_newassoc != NULL)
2333148307Ssam		ic->ic_newassoc(ni, newassoc);
2334178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2335138568Ssam	/* tell the authenticator about new station */
2336178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2337178354Ssam		vap->iv_auth->ia_node_join(ni);
2338178354Ssam	ieee80211_notify_node_join(ni,
2339173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2340138568Ssam}
2341138568Ssam
2342172211Ssamstatic void
2343172211Ssamdisable_protection(struct ieee80211com *ic)
2344172211Ssam{
2345172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2346172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2347172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2348172211Ssam	   ic->ic_flags_ext));
2349172211Ssam
2350172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2351172211Ssam	/* XXX verify mode? */
2352172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2353172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2354172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2355172211Ssam	}
2356179642Ssam	ieee80211_notify_erp_locked(ic);
2357172211Ssam}
2358172211Ssam
2359138568Ssam/*
2360138568Ssam * Handle a station leaving an 11g network.
2361138568Ssam */
2362138568Ssamstatic void
2363178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2364138568Ssam{
2365178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2366138568Ssam
2367173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2368173273Ssam
2369170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2370178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2371178354Ssam	      ic->ic_bsschan->ic_flags));
2372138568Ssam
2373138568Ssam	/*
2374138568Ssam	 * If a long slot station do the slot time bookkeeping.
2375138568Ssam	 */
2376138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2377138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2378138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2379138568Ssam		ic->ic_longslotsta--;
2380178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2381172211Ssam		    "long slot time station leaves, count now %d",
2382172211Ssam		    ic->ic_longslotsta);
2383138568Ssam		if (ic->ic_longslotsta == 0) {
2384138568Ssam			/*
2385138568Ssam			 * Re-enable use of short slot time if supported
2386138568Ssam			 * and not operating in IBSS mode (per spec).
2387138568Ssam			 */
2388138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2389138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2390178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2391178354Ssam				    IEEE80211_MSG_ASSOC,
2392138568Ssam				    "%s: re-enable use of short slot time\n",
2393138568Ssam				    __func__);
2394138568Ssam				ieee80211_set_shortslottime(ic, 1);
2395138568Ssam			}
2396138568Ssam		}
2397138568Ssam	}
2398138568Ssam	/*
2399138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2400138568Ssam	 */
2401138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2402138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2403138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2404138568Ssam		ic->ic_nonerpsta--;
2405178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2406172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2407172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2408172211Ssam			" (non-ERP sta present)" : "");
2409172211Ssam		if (ic->ic_nonerpsta == 0 &&
2410172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2411178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2412138568Ssam				"%s: disable use of protection\n", __func__);
2413172211Ssam			disable_protection(ic);
2414138568Ssam		}
2415138568Ssam	}
2416138568Ssam}
2417138568Ssam
2418138568Ssam/*
2419172211Ssam * Time out presence of an overlapping bss with non-ERP
2420172211Ssam * stations.  When operating in hostap mode we listen for
2421172211Ssam * beacons from other stations and if we identify a non-ERP
2422172211Ssam * station is present we enable protection.  To identify
2423172211Ssam * when all non-ERP stations are gone we time out this
2424172211Ssam * condition.
2425172211Ssam */
2426172211Ssamstatic void
2427172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2428172211Ssam{
2429172211Ssam
2430172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2431172211Ssam
2432172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2433172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2434178354Ssam#if 0
2435178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2436178354Ssam		    "%s", "age out non-ERP sta present on channel");
2437178354Ssam#endif
2438172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2439172211Ssam		if (ic->ic_nonerpsta == 0)
2440172211Ssam			disable_protection(ic);
2441172211Ssam	}
2442172211Ssam}
2443172211Ssam
2444172211Ssam/*
2445138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2446138568Ssam * when operating as an ap.
2447138568Ssam */
2448138568Ssamvoid
2449178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2450138568Ssam{
2451178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2452178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2453140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2454138568Ssam
2455178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2456172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2457138568Ssam
2458178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2459178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2460138568Ssam	/*
2461138568Ssam	 * If node wasn't previously associated all
2462138568Ssam	 * we need to do is reclaim the reference.
2463138568Ssam	 */
2464138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2465138568Ssam	if (ni->ni_associd == 0)
2466138568Ssam		goto done;
2467138568Ssam	/*
2468138568Ssam	 * Tell the authenticator the station is leaving.
2469138568Ssam	 * Note that we must do this before yanking the
2470138568Ssam	 * association id as the authenticator uses the
2471138568Ssam	 * associd to locate it's state block.
2472138568Ssam	 */
2473178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2474178354Ssam		vap->iv_auth->ia_node_leave(ni);
2475173273Ssam
2476173273Ssam	IEEE80211_LOCK(ic);
2477178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2478138568Ssam	ni->ni_associd = 0;
2479178354Ssam	vap->iv_sta_assoc--;
2480138568Ssam	ic->ic_sta_assoc--;
2481138568Ssam
2482173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2483173273Ssam		ieee80211_ht_node_leave(ni);
2484170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2485170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2486178354Ssam		ieee80211_node_leave_11g(ni);
2487173273Ssam	IEEE80211_UNLOCK(ic);
2488138568Ssam	/*
2489138568Ssam	 * Cleanup station state.  In particular clear various
2490138568Ssam	 * state that might otherwise be reused if the node
2491138568Ssam	 * is reused before the reference count goes to zero
2492138568Ssam	 * (and memory is reclaimed).
2493138568Ssam	 */
2494178354Ssam	ieee80211_sta_leave(ni);
2495138568Ssamdone:
2496140499Ssam	/*
2497140499Ssam	 * Remove the node from any table it's recorded in and
2498140499Ssam	 * drop the caller's reference.  Removal from the table
2499140499Ssam	 * is important to insure the node is not reprocessed
2500140499Ssam	 * for inactivity.
2501140499Ssam	 */
2502140499Ssam	if (nt != NULL) {
2503140499Ssam		IEEE80211_NODE_LOCK(nt);
2504140499Ssam		node_reclaim(nt, ni);
2505140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2506140499Ssam	} else
2507140499Ssam		ieee80211_free_node(ni);
2508138568Ssam}
2509138568Ssam
2510178354Ssamstruct rssiinfo {
2511178354Ssam	struct ieee80211vap *vap;
2512178354Ssam	int	rssi_samples;
2513178354Ssam	uint32_t rssi_total;
2514178354Ssam};
2515178354Ssam
2516178354Ssamstatic void
2517178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni)
2518178354Ssam{
2519178354Ssam	struct rssiinfo *info = arg;
2520178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2521178354Ssam	int8_t rssi;
2522178354Ssam
2523178354Ssam	if (info->vap != vap)
2524178354Ssam		return;
2525178354Ssam	/* only associated stations */
2526178354Ssam	if (ni->ni_associd == 0)
2527178354Ssam		return;
2528178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2529178354Ssam	if (rssi != 0) {
2530178354Ssam		info->rssi_samples++;
2531178354Ssam		info->rssi_total += rssi;
2532178354Ssam	}
2533178354Ssam}
2534178354Ssam
2535178354Ssamstatic void
2536178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2537178354Ssam{
2538178354Ssam	struct rssiinfo *info = arg;
2539178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2540178354Ssam	int8_t rssi;
2541178354Ssam
2542178354Ssam	if (info->vap != vap)
2543178354Ssam		return;
2544178354Ssam	/* only neighbors */
2545178354Ssam	/* XXX check bssid */
2546178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2547178354Ssam		return;
2548178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2549178354Ssam	if (rssi != 0) {
2550178354Ssam		info->rssi_samples++;
2551178354Ssam		info->rssi_total += rssi;
2552178354Ssam	}
2553178354Ssam}
2554178354Ssam
2555170530Ssamint8_t
2556178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2557138568Ssam{
2558138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2559178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2560178354Ssam	struct rssiinfo info;
2561138568Ssam
2562178354Ssam	info.rssi_total = 0;
2563178354Ssam	info.rssi_samples = 0;
2564178354Ssam	info.vap = vap;
2565178354Ssam	switch (vap->iv_opmode) {
2566138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2567138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2568178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2569178354Ssam		break;
2570138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2571178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2572138568Ssam		break;
2573138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2574138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2575138568Ssam	default:
2576178354Ssam		if (vap->iv_bss != NULL)
2577178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2578178354Ssam		info.rssi_samples = 1;
2579138568Ssam		break;
2580138568Ssam	}
2581178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2582138568Ssam#undef NZ
2583138568Ssam}
2584138568Ssam
2585170530Ssamvoid
2586178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2587138568Ssam{
2588138568Ssam
2589178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2590170530Ssam		return;
2591178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2592170530Ssam	/* for non-station mode return avg'd rssi accounting */
2593178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2594178354Ssam		*rssi = ieee80211_getrssi(vap);
2595138568Ssam}
2596