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$");
29116742Ssam
30178354Ssam#include "opt_wlan.h"
31178354Ssam
32116742Ssam#include <sys/param.h>
33116742Ssam#include <sys/systm.h>
34116742Ssam#include <sys/mbuf.h>
35116742Ssam#include <sys/malloc.h>
36116742Ssam#include <sys/kernel.h>
37138568Ssam
38116742Ssam#include <sys/socket.h>
39116742Ssam
40116742Ssam#include <net/if.h>
41116742Ssam#include <net/if_media.h>
42116742Ssam#include <net/ethernet.h>
43116742Ssam
44116742Ssam#include <net80211/ieee80211_var.h>
45178354Ssam#include <net80211/ieee80211_input.h>
46190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
47190391Ssam#include <net80211/ieee80211_superg.h>
48190391Ssam#endif
49186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
50186904Ssam#include <net80211/ieee80211_tdma.h>
51186904Ssam#endif
52178354Ssam#include <net80211/ieee80211_wds.h>
53195618Srpaulo#include <net80211/ieee80211_mesh.h>
54206358Srpaulo#include <net80211/ieee80211_ratectl.h>
55116742Ssam
56116742Ssam#include <net/bpf.h>
57116742Ssam
58147221Ssam/*
59195618Srpaulo * IEEE80211_NODE_HASHSIZE must be a power of 2.
60195618Srpaulo */
61195618SrpauloCTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
62195618Srpaulo
63195618Srpaulo/*
64147221Ssam * Association id's are managed with a bit vector.
65147221Ssam */
66178354Ssam#define	IEEE80211_AID_SET(_vap, b) \
67178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
68178354Ssam		(1 << (IEEE80211_AID(b) % 32)))
69178354Ssam#define	IEEE80211_AID_CLR(_vap, b) \
70178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
71178354Ssam		~(1 << (IEEE80211_AID(b) % 32)))
72178354Ssam#define	IEEE80211_AID_ISSET(_vap, b) \
73178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
74147221Ssam
75159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
76159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
77159139Sdds#else
78159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
79159139Sdds#endif
80159139Sdds
81170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
82170530Ssam
83179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *,
84179643Ssam	const uint8_t [IEEE80211_ADDR_LEN]);
85138568Ssamstatic void node_cleanup(struct ieee80211_node *);
86138568Ssamstatic void node_free(struct ieee80211_node *);
87178354Ssamstatic void node_age(struct ieee80211_node *);
88170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
89170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
90178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *,
91178354Ssam	struct ieee80211_mimo_info *);
92116742Ssam
93138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
94120104Ssam
95138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
96148863Ssam	struct ieee80211_node_table *nt, const char *name,
97170530Ssam	int inact, int keymaxix);
98178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *,
99178354Ssam	struct ieee80211vap *);
100138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
101172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
102138568Ssam
103127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
104178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
105120481Ssam
106116742Ssamvoid
107138568Ssamieee80211_node_attach(struct ieee80211com *ic)
108116742Ssam{
109195379Ssam	/* XXX really want maxlen enforced per-sta */
110195379Ssam	ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
111195379Ssam	    "802.11 staging q");
112178354Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
113178354Ssam		IEEE80211_INACT_INIT, ic->ic_max_keyix);
114178354Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
115178354Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
116178354Ssam		ieee80211_node_timeout, ic);
117116742Ssam
118138568Ssam	ic->ic_node_alloc = node_alloc;
119138568Ssam	ic->ic_node_free = node_free;
120138568Ssam	ic->ic_node_cleanup = node_cleanup;
121178354Ssam	ic->ic_node_age = node_age;
122178354Ssam	ic->ic_node_drain = node_age;		/* NB: same as age */
123138568Ssam	ic->ic_node_getrssi = node_getrssi;
124170530Ssam	ic->ic_node_getsignal = node_getsignal;
125178354Ssam	ic->ic_node_getmimoinfo = node_getmimoinfo;
126138568Ssam
127178354Ssam	/*
128178354Ssam	 * Set flags to be propagated to all vap's;
129178354Ssam	 * these define default behaviour/configuration.
130178354Ssam	 */
131178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
132178354Ssam}
133138568Ssam
134178354Ssamvoid
135178354Ssamieee80211_node_detach(struct ieee80211com *ic)
136178354Ssam{
137170530Ssam
138178354Ssam	callout_drain(&ic->ic_inact);
139178354Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
140195379Ssam	ieee80211_ageq_cleanup(&ic->ic_stageq);
141178354Ssam}
142172062Ssam
143178354Ssamvoid
144178354Ssamieee80211_node_vattach(struct ieee80211vap *vap)
145178354Ssam{
146178354Ssam	/* NB: driver can override */
147178354Ssam	vap->iv_max_aid = IEEE80211_AID_DEF;
148178354Ssam
149178354Ssam	/* default station inactivity timer setings */
150178354Ssam	vap->iv_inact_init = IEEE80211_INACT_INIT;
151178354Ssam	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
152178354Ssam	vap->iv_inact_run = IEEE80211_INACT_RUN;
153178354Ssam	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
154184277Ssam
155184277Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
156184277Ssam	    "%s: init %u auth %u run %u probe %u\n", __func__,
157184277Ssam	    vap->iv_inact_init, vap->iv_inact_auth,
158184277Ssam	    vap->iv_inact_run, vap->iv_inact_probe);
159148863Ssam}
160148863Ssam
161148863Ssamvoid
162178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap)
163148863Ssam{
164178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
165178354Ssam		/* XXX should we allow max aid to be zero? */
166178354Ssam		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
167178354Ssam			vap->iv_max_aid = IEEE80211_AID_MIN;
168178354Ssam			if_printf(vap->iv_ifp,
169178354Ssam			    "WARNING: max aid too small, changed to %d\n",
170178354Ssam			    vap->iv_max_aid);
171178354Ssam		}
172186302Ssam		vap->iv_aid_bitmap = (uint32_t *) malloc(
173184210Sdes			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
174178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
175178354Ssam		if (vap->iv_aid_bitmap == NULL) {
176178354Ssam			/* XXX no way to recover */
177178354Ssam			printf("%s: no memory for AID bitmap, max aid %d!\n",
178178354Ssam			    __func__, vap->iv_max_aid);
179178354Ssam			vap->iv_max_aid = 0;
180178354Ssam		}
181138568Ssam	}
182138568Ssam
183178354Ssam	ieee80211_reset_bss(vap);
184118887Ssam
185178354Ssam	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
186116742Ssam}
187116742Ssam
188116742Ssamvoid
189178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap)
190116742Ssam{
191178354Ssam	struct ieee80211com *ic = vap->iv_ic;
192116742Ssam
193178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
194178354Ssam	if (vap->iv_bss != NULL) {
195178354Ssam		ieee80211_free_node(vap->iv_bss);
196178354Ssam		vap->iv_bss = NULL;
197138568Ssam	}
198178354Ssam	if (vap->iv_aid_bitmap != NULL) {
199186302Ssam		free(vap->iv_aid_bitmap, M_80211_NODE);
200178354Ssam		vap->iv_aid_bitmap = NULL;
201138568Ssam	}
202116742Ssam}
203116742Ssam
204138568Ssam/*
205138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
206138568Ssam */
207138568Ssam
208138568Ssamvoid
209148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
210138568Ssam{
211184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
212184277Ssam
213138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
214184277Ssam	ni->ni_inact_reload = vap->iv_inact_run;
215172062Ssam	ni->ni_inact = ni->ni_inact_reload;
216184277Ssam
217184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
218184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
219138568Ssam}
220138568Ssam
221138568Ssamvoid
222148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
223138568Ssam{
224184277Ssam	struct ieee80211vap *vap = ni->ni_vap;
225184277Ssam
226138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
227184277Ssam	ni->ni_inact_reload = vap->iv_inact_auth;
228172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
229172062Ssam		ni->ni_inact = ni->ni_inact_reload;
230184277Ssam
231184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
232184277Ssam	    "%s: inact_reload %u inact %u", __func__,
233184277Ssam	    ni->ni_inact_reload, ni->ni_inact);
234138568Ssam}
235138568Ssam
236116742Ssam/*
237183251Ssam * Fix tx parameters for a node according to ``association state''.
238183251Ssam */
239193966Ssamvoid
240193966Ssamieee80211_node_setuptxparms(struct ieee80211_node *ni)
241183251Ssam{
242183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
243187898Ssam	enum ieee80211_phymode mode;
244183251Ssam
245183251Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
246183251Ssam		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
247187898Ssam			mode = IEEE80211_MODE_11NA;
248183251Ssam		else
249187898Ssam			mode = IEEE80211_MODE_11NG;
250183251Ssam	} else {				/* legacy rate handling */
251187898Ssam		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
252187898Ssam			mode = IEEE80211_MODE_STURBO_A;
253188782Ssam		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
254188782Ssam			mode = IEEE80211_MODE_HALF;
255188782Ssam		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
256188782Ssam			mode = IEEE80211_MODE_QUARTER;
257191015Ssam		/* NB: 108A should be handled as 11a */
258187898Ssam		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
259187898Ssam			mode = IEEE80211_MODE_11A;
260191015Ssam		else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
261191015Ssam		    (ni->ni_flags & IEEE80211_NODE_ERP))
262187898Ssam			mode = IEEE80211_MODE_11G;
263183251Ssam		else
264187898Ssam			mode = IEEE80211_MODE_11B;
265183251Ssam	}
266187898Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
267183251Ssam}
268183251Ssam
269183251Ssam/*
270138568Ssam * Set/change the channel.  The rate set is also updated as
271138568Ssam * to insure a consistent view by drivers.
272178354Ssam * XXX should be private but hostap needs it to deal with CSA
273138568Ssam */
274178354Ssamvoid
275178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni,
276178354Ssam	struct ieee80211_channel *chan)
277138568Ssam{
278178354Ssam	struct ieee80211com *ic = ni->ni_ic;
279183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
280183251Ssam	enum ieee80211_phymode mode;
281170530Ssam
282178354Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
283178354Ssam
284138568Ssam	ni->ni_chan = chan;
285183251Ssam	mode = ieee80211_chan2mode(chan);
286170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
287170530Ssam		/*
288219602Sbschmidt		 * We must install the legacy rate est in ni_rates and the
289170530Ssam		 * HT rate set in ni_htrates.
290170530Ssam		 */
291170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
292183251Ssam		/*
293183251Ssam		 * Setup bss tx parameters based on operating mode.  We
294183251Ssam		 * use legacy rates when operating in a mixed HT+non-HT bss
295183251Ssam		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
296183251Ssam		 */
297183251Ssam		if (mode == IEEE80211_MODE_11NA &&
298193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
299183251Ssam			mode = IEEE80211_MODE_11A;
300183251Ssam		else if (mode == IEEE80211_MODE_11NG &&
301193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
302183251Ssam			mode = IEEE80211_MODE_11G;
303183251Ssam		if (mode == IEEE80211_MODE_11G &&
304183251Ssam		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
305183251Ssam			mode = IEEE80211_MODE_11B;
306170530Ssam	}
307183251Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
308165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
309138568Ssam}
310138568Ssam
311141658Ssamstatic __inline void
312141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
313141658Ssam{
314141658Ssam	/* propagate useful state */
315141658Ssam	nbss->ni_authmode = obss->ni_authmode;
316141658Ssam	nbss->ni_txpower = obss->ni_txpower;
317141658Ssam	nbss->ni_vlan = obss->ni_vlan;
318141658Ssam	/* XXX statistics? */
319178354Ssam	/* XXX legacy WDS bssid? */
320141658Ssam}
321141658Ssam
322116742Ssamvoid
323178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
324116742Ssam{
325178354Ssam	struct ieee80211com *ic = vap->iv_ic;
326116742Ssam	struct ieee80211_node *ni;
327116742Ssam
328178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
329195618Srpaulo		"%s: creating %s on channel %u\n", __func__,
330195618Srpaulo		ieee80211_opmode_name[vap->iv_opmode],
331178354Ssam		ieee80211_chan2ieee(ic, chan));
332138568Ssam
333178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
334140753Ssam	if (ni == NULL) {
335140753Ssam		/* XXX recovery? */
336138568Ssam		return;
337138568Ssam	}
338178354Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
339178354Ssam	ni->ni_esslen = vap->iv_des_ssid[0].len;
340178354Ssam	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
341178354Ssam	if (vap->iv_bss != NULL)
342178354Ssam		copy_bss(ni, vap->iv_bss);
343148843Ssam	ni->ni_intval = ic->ic_bintval;
344178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
345116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
346116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
347116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
348116742Ssam		ni->ni_fhindex = 1;
349116742Ssam	}
350178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
351178354Ssam		vap->iv_flags |= IEEE80211_F_SIBSS;
352138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
353178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
354178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
355167282Ssam		else {
356167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
357167282Ssam			/* clear group bit, add local bit */
358167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
359167282Ssam		}
360178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
361178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
362178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
363153403Ssam		else
364186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
365186904Ssam		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
366186904Ssam#endif
367153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
368195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
369195618Srpaulo	} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
370195618Srpaulo		ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
371195618Srpaulo		memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
372195618Srpaulo#endif
373138568Ssam	}
374138568Ssam	/*
375138568Ssam	 * Fix the channel and related attributes.
376138568Ssam	 */
377178354Ssam	/* clear DFS CAC state on previous channel */
378178354Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
379178354Ssam	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
380178354Ssam	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
381178354Ssam		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
382170530Ssam	ic->ic_bsschan = chan;
383178354Ssam	ieee80211_node_set_chan(ni, chan);
384170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
385138568Ssam	/*
386178354Ssam	 * Do mode-specific setup.
387138568Ssam	 */
388170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
389170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
390170530Ssam			/*
391178354Ssam			 * Use a mixed 11b/11g basic rate set.
392170530Ssam			 */
393178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
394178354Ssam			    IEEE80211_MODE_11G);
395178354Ssam			if (vap->iv_flags & IEEE80211_F_PUREG) {
396178354Ssam				/*
397178354Ssam				 * Also mark OFDM rates basic so 11b
398178354Ssam				 * stations do not join (WiFi compliance).
399178354Ssam				 */
400178354Ssam				ieee80211_addbasicrates(&ni->ni_rates,
401178354Ssam				    IEEE80211_MODE_11A);
402178354Ssam			}
403170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
404170530Ssam			/*
405170530Ssam			 * Force pure 11b rate set.
406170530Ssam			 */
407178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
408170530Ssam				IEEE80211_MODE_11B);
409170530Ssam		}
410170530Ssam	}
411138568Ssam
412170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
413116742Ssam}
414116742Ssam
415170530Ssam/*
416170530Ssam * Reset bss state on transition to the INIT state.
417170530Ssam * Clear any stations from the table (they have been
418170530Ssam * deauth'd) and reset the bss node (clears key, rate
419170530Ssam * etc. state).
420170530Ssam */
421138568Ssamvoid
422178354Ssamieee80211_reset_bss(struct ieee80211vap *vap)
423138568Ssam{
424178354Ssam	struct ieee80211com *ic = vap->iv_ic;
425138568Ssam	struct ieee80211_node *ni, *obss;
426138568Ssam
427178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
428178354Ssam	/* XXX multi-bss: wrong */
429170530Ssam	ieee80211_reset_erp(ic);
430140753Ssam
431178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
432207322Srpaulo	KASSERT(ni != NULL, ("unable to setup initial BSS node"));
433178354Ssam	obss = vap->iv_bss;
434178354Ssam	vap->iv_bss = ieee80211_ref_node(ni);
435141658Ssam	if (obss != NULL) {
436141658Ssam		copy_bss(ni, obss);
437148843Ssam		ni->ni_intval = ic->ic_bintval;
438138568Ssam		ieee80211_free_node(obss);
439178354Ssam	} else
440178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
441138568Ssam}
442138568Ssam
443170530Ssamstatic int
444170530Ssammatch_ssid(const struct ieee80211_node *ni,
445170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
446170530Ssam{
447170530Ssam	int i;
448148432Ssam
449170530Ssam	for (i = 0; i < nssid; i++) {
450170530Ssam		if (ni->ni_esslen == ssids[i].len &&
451170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
452170530Ssam			return 1;
453170530Ssam	}
454170530Ssam	return 0;
455170530Ssam}
456170530Ssam
457170530Ssam/*
458170530Ssam * Test a node for suitability/compatibility.
459170530Ssam */
460127767Ssamstatic int
461178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
462127767Ssam{
463178354Ssam	struct ieee80211com *ic = ni->ni_ic;
464170530Ssam        uint8_t rate;
465170530Ssam
466170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
467170530Ssam		return 0;
468178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
469170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
470170530Ssam			return 0;
471170530Ssam	} else {
472170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
473170530Ssam			return 0;
474170530Ssam	}
475178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
476170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
477170530Ssam			return 0;
478170530Ssam	} else {
479170530Ssam		/* XXX does this mean privacy is supported or required? */
480170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
481170530Ssam			return 0;
482170530Ssam	}
483170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
484170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
485170530Ssam	if (rate & IEEE80211_RATE_BASIC)
486170530Ssam		return 0;
487178354Ssam	if (vap->iv_des_nssid != 0 &&
488178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
489170530Ssam		return 0;
490178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
491178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
492170530Ssam		return 0;
493170530Ssam	return 1;
494170530Ssam}
495170530Ssam
496170530Ssam#ifdef IEEE80211_DEBUG
497170530Ssam/*
498170530Ssam * Display node suitability/compatibility.
499170530Ssam */
500170530Ssamstatic void
501178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
502170530Ssam{
503178354Ssam	struct ieee80211com *ic = ni->ni_ic;
504170530Ssam        uint8_t rate;
505127767Ssam        int fail;
506127767Ssam
507127767Ssam	fail = 0;
508127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
509127767Ssam		fail |= 0x01;
510178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
511127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
512127767Ssam			fail |= 0x02;
513127767Ssam	} else {
514127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
515127767Ssam			fail |= 0x02;
516127767Ssam	}
517178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
518127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
519127767Ssam			fail |= 0x04;
520127767Ssam	} else {
521127767Ssam		/* XXX does this mean privacy is supported or required? */
522127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
523127767Ssam			fail |= 0x04;
524127767Ssam	}
525167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
526165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
527127767Ssam	if (rate & IEEE80211_RATE_BASIC)
528127767Ssam		fail |= 0x08;
529178354Ssam	if (vap->iv_des_nssid != 0 &&
530178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
531127767Ssam		fail |= 0x10;
532178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
533178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
534127767Ssam		fail |= 0x20;
535127767Ssam
536170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
537170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
538170530Ssam	printf(" %3d%c",
539170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
540170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
541170530Ssam	    fail & 0x08 ? '!' : ' ');
542170530Ssam	printf(" %4s%c",
543170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
544170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
545170530Ssam	    "????",
546170530Ssam	    fail & 0x02 ? '!' : ' ');
547170530Ssam	printf(" %3s%c ",
548170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
549170530Ssam	    fail & 0x04 ? '!' : ' ');
550170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
551170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
552138568Ssam}
553170530Ssam#endif /* IEEE80211_DEBUG */
554138568Ssam
555138568Ssam/*
556138568Ssam * Handle 802.11 ad hoc network merge.  The
557138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
558138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
559138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
560138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
561138568Ssam * sends beacons with the greatest TSF timestamp.
562138568Ssam *
563138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
564138568Ssam *
565138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
566138568Ssam */
567138568Ssamint
568148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
569138568Ssam{
570178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
571178354Ssam#ifdef IEEE80211_DEBUG
572148306Ssam	struct ieee80211com *ic = ni->ni_ic;
573178354Ssam#endif
574138568Ssam
575178354Ssam	if (ni == vap->iv_bss ||
576178354Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
577138568Ssam		/* unchanged, nothing to do */
578138568Ssam		return 0;
579138568Ssam	}
580178354Ssam	if (!check_bss(vap, ni)) {
581170530Ssam		/* capabilities mismatch */
582178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
583138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
584170530Ssam#ifdef IEEE80211_DEBUG
585178354Ssam		if (ieee80211_msg_assoc(vap))
586178354Ssam			check_bss_debug(vap, ni);
587170530Ssam#endif
588178354Ssam		vap->iv_stats.is_ibss_capmismatch++;
589138568Ssam		return 0;
590138568Ssam	}
591178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
592138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
593138568Ssam		ether_sprintf(ni->ni_bssid),
594138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
595138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
596138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
597138568Ssam	);
598170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
599138568Ssam}
600138568Ssam
601138568Ssam/*
602178354Ssam * Calculate HT channel promotion flags for all vaps.
603178354Ssam * This assumes ni_chan have been setup for each vap.
604173273Ssam */
605178354Ssamstatic int
606178354Ssamgethtadjustflags(struct ieee80211com *ic)
607178354Ssam{
608178354Ssam	struct ieee80211vap *vap;
609178354Ssam	int flags;
610178354Ssam
611178354Ssam	flags = 0;
612178354Ssam	/* XXX locking */
613178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
614178354Ssam		if (vap->iv_state < IEEE80211_S_RUN)
615178354Ssam			continue;
616178354Ssam		switch (vap->iv_opmode) {
617178354Ssam		case IEEE80211_M_WDS:
618178354Ssam		case IEEE80211_M_STA:
619178354Ssam		case IEEE80211_M_AHDEMO:
620178354Ssam		case IEEE80211_M_HOSTAP:
621178354Ssam		case IEEE80211_M_IBSS:
622195618Srpaulo		case IEEE80211_M_MBSS:
623178354Ssam			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
624178354Ssam			break;
625178354Ssam		default:
626178354Ssam			break;
627178354Ssam		}
628178354Ssam	}
629178354Ssam	return flags;
630178354Ssam}
631178354Ssam
632178354Ssam/*
633178354Ssam * Check if the current channel needs to change based on whether
634184303Ssam * any vap's are using HT20/HT40.  This is used to sync the state
635184303Ssam * of ic_curchan after a channel width change on a running vap.
636178354Ssam */
637173273Ssamvoid
638178354Ssamieee80211_sync_curchan(struct ieee80211com *ic)
639173273Ssam{
640178354Ssam	struct ieee80211_channel *c;
641178354Ssam
642178354Ssam	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
643178354Ssam	if (c != ic->ic_curchan) {
644178354Ssam		ic->ic_curchan = c;
645178354Ssam		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
646190532Ssam		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
647191746Sthompsa		IEEE80211_UNLOCK(ic);
648178354Ssam		ic->ic_set_channel(ic);
649192468Ssam		ieee80211_radiotap_chan_change(ic);
650191746Sthompsa		IEEE80211_LOCK(ic);
651178354Ssam	}
652178354Ssam}
653178354Ssam
654178354Ssam/*
655191746Sthompsa * Setup the current channel.  The request channel may be
656178354Ssam * promoted if other vap's are operating with HT20/HT40.
657178354Ssam */
658178354Ssamvoid
659191746Sthompsaieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
660178354Ssam{
661178354Ssam	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
662178354Ssam		int flags = gethtadjustflags(ic);
663178354Ssam		/*
664178354Ssam		 * Check for channel promotion required to support the
665178354Ssam		 * set of running vap's.  This assumes we are called
666178354Ssam		 * after ni_chan is setup for each vap.
667178354Ssam		 */
668193655Ssam		/* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
669178354Ssam		if (flags > ieee80211_htchanflags(c))
670178354Ssam			c = ieee80211_ht_adjust_channel(ic, c, flags);
671178354Ssam	}
672178354Ssam	ic->ic_bsschan = ic->ic_curchan = c;
673173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
674190532Ssam	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
675173273Ssam}
676173273Ssam
677173273Ssam/*
678191746Sthompsa * Change the current channel.  The channel change is guaranteed to have
679191746Sthompsa * happened before the next state change.
680191746Sthompsa */
681191746Sthompsavoid
682191746Sthompsaieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
683191746Sthompsa{
684191746Sthompsa	ieee80211_setupcurchan(ic, c);
685191746Sthompsa	ieee80211_runtask(ic, &ic->ic_chan_task);
686191746Sthompsa}
687191746Sthompsa
688233452Sadrianvoid
689233452Sadrianieee80211_update_chw(struct ieee80211com *ic)
690233452Sadrian{
691233452Sadrian
692233452Sadrian	ieee80211_setupcurchan(ic, ic->ic_curchan);
693233452Sadrian	ieee80211_runtask(ic, &ic->ic_chw_task);
694233452Sadrian}
695233452Sadrian
696191746Sthompsa/*
697138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
698138568Ssam * be passed in with a held reference.
699138568Ssam */
700170530Ssamstatic int
701170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
702138568Ssam{
703178354Ssam	struct ieee80211vap *vap = selbs->ni_vap;
704170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
705138568Ssam	struct ieee80211_node *obss;
706170530Ssam	int canreassoc;
707138568Ssam
708138568Ssam	/*
709138568Ssam	 * Committed to selbs, setup state.
710138568Ssam	 */
711178354Ssam	obss = vap->iv_bss;
712170530Ssam	/*
713170530Ssam	 * Check if old+new node have the same address in which
714170530Ssam	 * case we can reassociate when operating in sta mode.
715170530Ssam	 */
716170530Ssam	canreassoc = (obss != NULL &&
717178354Ssam		vap->iv_state == IEEE80211_S_RUN &&
718170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
719178354Ssam	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
720153352Ssam	if (obss != NULL) {
721153352Ssam		copy_bss(selbs, obss);
722188541Ssam		ieee80211_node_decref(obss);	/* iv_bss reference */
723188541Ssam		ieee80211_free_node(obss);	/* station table reference */
724178354Ssam		obss = NULL;		/* NB: guard against later use */
725153352Ssam	}
726165887Ssam
727138568Ssam	/*
728165887Ssam	 * Delete unusable rates; we've already checked
729165887Ssam	 * that the negotiated rate set is acceptable.
730165887Ssam	 */
731178354Ssam	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
732167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
733165887Ssam
734178354Ssam	ieee80211_setcurchan(ic, selbs->ni_chan);
735165887Ssam	/*
736138568Ssam	 * Set the erp state (mostly the slot time) to deal with
737138568Ssam	 * the auto-select case; this should be redundant if the
738138568Ssam	 * mode is locked.
739138568Ssam	 */
740138568Ssam	ieee80211_reset_erp(ic);
741178354Ssam	ieee80211_wme_initparams(vap);
742140753Ssam
743178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
744170530Ssam		if (canreassoc) {
745170530Ssam			/* Reassociate */
746178354Ssam			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
747170530Ssam		} else {
748170530Ssam			/*
749170530Ssam			 * Act as if we received a DEAUTH frame in case we
750170530Ssam			 * are invoked from the RUN state.  This will cause
751170530Ssam			 * us to try to re-authenticate if we are operating
752170530Ssam			 * as a station.
753170530Ssam			 */
754178354Ssam			ieee80211_new_state(vap, IEEE80211_S_AUTH,
755170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
756170530Ssam		}
757170530Ssam	} else
758178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
759138568Ssam	return 1;
760116742Ssam}
761116742Ssam
762170530Ssamint
763184274Ssamieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
764170530Ssam	const struct ieee80211_scan_entry *se)
765170530Ssam{
766178354Ssam	struct ieee80211com *ic = vap->iv_ic;
767170530Ssam	struct ieee80211_node *ni;
768170530Ssam
769178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
770170530Ssam	if (ni == NULL) {
771170530Ssam		/* XXX msg */
772170530Ssam		return 0;
773170530Ssam	}
774245928Sadrian
775170530Ssam	/*
776170530Ssam	 * Expand scan state into node's format.
777170530Ssam	 * XXX may not need all this stuff
778170530Ssam	 */
779170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
780170530Ssam	ni->ni_esslen = se->se_ssid[1];
781170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
782170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
783170530Ssam	ni->ni_intval = se->se_intval;
784170530Ssam	ni->ni_capinfo = se->se_capinfo;
785184274Ssam	ni->ni_chan = chan;
786170530Ssam	ni->ni_timoff = se->se_timoff;
787170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
788170530Ssam	ni->ni_fhindex = se->se_fhindex;
789170530Ssam	ni->ni_erp = se->se_erp;
790178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
791170530Ssam	ni->ni_noise = se->se_noise;
792186870Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
793186870Ssam		/* NB: only infrastructure mode requires an associd */
794186870Ssam		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
795186870Ssam	}
796178354Ssam
797178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
798178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
799190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
800178354Ssam		if (ni->ni_ies.ath_ie != NULL)
801178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
802190391Ssam#endif
803178354Ssam		if (ni->ni_ies.htcap_ie != NULL)
804178354Ssam			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
805178354Ssam		if (ni->ni_ies.htinfo_ie != NULL)
806178354Ssam			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
807195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
808195618Srpaulo		if (ni->ni_ies.meshid_ie != NULL)
809195618Srpaulo			ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
810195618Srpaulo#endif
811186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
812186904Ssam		if (ni->ni_ies.tdma_ie != NULL)
813186904Ssam			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
814186904Ssam#endif
815173864Ssam	}
816170530Ssam
817178354Ssam	vap->iv_dtim_period = se->se_dtimperiod;
818178354Ssam	vap->iv_dtim_count = 0;
819170530Ssam
820170530Ssam	/* NB: must be after ni_chan is setup */
821170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
822170530Ssam		IEEE80211_F_DOSORT);
823184279Ssam	if (ieee80211_iserp_rateset(&ni->ni_rates))
824184279Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
825245928Sadrian
826245928Sadrian	/*
827245928Sadrian	 * Setup HT state for this node if it's available, otherwise
828245928Sadrian	 * non-STA modes won't pick this state up.
829245928Sadrian	 *
830245928Sadrian	 * For IBSS and related modes that don't go through an
831245928Sadrian	 * association request/response, the only appropriate place
832245928Sadrian	 * to setup the HT state is here.
833245928Sadrian	 */
834245928Sadrian	if (ni->ni_ies.htinfo_ie != NULL &&
835245928Sadrian	    ni->ni_ies.htcap_ie != NULL &&
836245928Sadrian	    vap->iv_flags_ht & IEEE80211_FHT_HT) {
837245928Sadrian		ieee80211_ht_node_init(ni);
838245928Sadrian		ieee80211_ht_updateparams(ni,
839245928Sadrian		    ni->ni_ies.htcap_ie,
840245928Sadrian		    ni->ni_ies.htinfo_ie);
841245928Sadrian		ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
842245928Sadrian		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
843245928Sadrian		ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
844245928Sadrian	}
845245928Sadrian	/* XXX else check for ath FF? */
846245928Sadrian	/* XXX QoS? Difficult given that WME config is specific to a master */
847245928Sadrian
848193966Ssam	ieee80211_node_setuptxparms(ni);
849214894Sbschmidt	ieee80211_ratectl_node_init(ni);
850170530Ssam
851170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
852170530Ssam}
853170530Ssam
854138568Ssam/*
855138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
856138568Ssam * be passed in with a held reference.
857138568Ssam */
858138568Ssamvoid
859178354Ssamieee80211_sta_leave(struct ieee80211_node *ni)
860138568Ssam{
861178354Ssam	struct ieee80211com *ic = ni->ni_ic;
862178354Ssam
863138568Ssam	ic->ic_node_cleanup(ni);
864178354Ssam	ieee80211_notify_node_leave(ni);
865138568Ssam}
866138568Ssam
867178354Ssam/*
868178354Ssam * Send a deauthenticate frame and drop the station.
869178354Ssam */
870178354Ssamvoid
871178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason)
872178354Ssam{
873178354Ssam	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
874178354Ssam	ieee80211_ref_node(ni);
875178354Ssam	if (ni->ni_associd != 0)
876178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
877178354Ssam	ieee80211_node_leave(ni);
878178354Ssam	ieee80211_free_node(ni);
879178354Ssam}
880178354Ssam
881116742Ssamstatic struct ieee80211_node *
882179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
883116742Ssam{
884127768Ssam	struct ieee80211_node *ni;
885138568Ssam
886186302Ssam	ni = (struct ieee80211_node *) malloc(sizeof(struct ieee80211_node),
887127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
888127768Ssam	return ni;
889116742Ssam}
890116742Ssam
891138568Ssam/*
892178354Ssam * Initialize an ie blob with the specified data.  If previous
893178354Ssam * data exists re-use the data block.  As a side effect we clear
894178354Ssam * all references to specific ie's; the caller is required to
895178354Ssam * recalculate them.
896178354Ssam */
897178354Ssamint
898178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
899178354Ssam{
900178354Ssam	/* NB: assumes data+len are the last fields */
901178354Ssam	memset(ies, 0, offsetof(struct ieee80211_ies, data));
902178354Ssam	if (ies->data != NULL && ies->len != len) {
903178354Ssam		/* data size changed */
904186302Ssam		free(ies->data, M_80211_NODE_IE);
905178354Ssam		ies->data = NULL;
906178354Ssam	}
907178354Ssam	if (ies->data == NULL) {
908186302Ssam		ies->data = (uint8_t *) malloc(len, M_80211_NODE_IE, M_NOWAIT);
909178354Ssam		if (ies->data == NULL) {
910178354Ssam			ies->len = 0;
911178354Ssam			/* NB: pointers have already been zero'd above */
912178354Ssam			return 0;
913178354Ssam		}
914178354Ssam	}
915178354Ssam	memcpy(ies->data, data, len);
916178354Ssam	ies->len = len;
917178354Ssam	return 1;
918178354Ssam}
919178354Ssam
920178354Ssam/*
921178354Ssam * Reclaim storage for an ie blob.
922178354Ssam */
923178354Ssamvoid
924178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies)
925178354Ssam{
926178354Ssam	if (ies->data != NULL)
927186302Ssam		free(ies->data, M_80211_NODE_IE);
928178354Ssam}
929178354Ssam
930178354Ssam/*
931178354Ssam * Expand an ie blob data contents and to fillin individual
932178354Ssam * ie pointers.  The data blob is assumed to be well-formed;
933178354Ssam * we don't do any validity checking of ie lengths.
934178354Ssam */
935178354Ssamvoid
936178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies)
937178354Ssam{
938178354Ssam	uint8_t *ie;
939178354Ssam	int ielen;
940178354Ssam
941178354Ssam	ie = ies->data;
942178354Ssam	ielen = ies->len;
943178354Ssam	while (ielen > 0) {
944178354Ssam		switch (ie[0]) {
945178354Ssam		case IEEE80211_ELEMID_VENDOR:
946178354Ssam			if (iswpaoui(ie))
947178354Ssam				ies->wpa_ie = ie;
948178354Ssam			else if (iswmeoui(ie))
949178354Ssam				ies->wme_ie = ie;
950190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
951178354Ssam			else if (isatherosoui(ie))
952178354Ssam				ies->ath_ie = ie;
953190391Ssam#endif
954186904Ssam#ifdef IEEE80211_SUPPORT_TDMA
955186904Ssam			else if (istdmaoui(ie))
956186904Ssam				ies->tdma_ie = ie;
957186904Ssam#endif
958178354Ssam			break;
959178354Ssam		case IEEE80211_ELEMID_RSN:
960178354Ssam			ies->rsn_ie = ie;
961178354Ssam			break;
962178354Ssam		case IEEE80211_ELEMID_HTCAP:
963178354Ssam			ies->htcap_ie = ie;
964178354Ssam			break;
965245928Sadrian		case IEEE80211_ELEMID_HTINFO:
966245928Sadrian			ies->htinfo_ie = ie;
967245928Sadrian			break;
968195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
969195618Srpaulo		case IEEE80211_ELEMID_MESHID:
970195618Srpaulo			ies->meshid_ie = ie;
971195618Srpaulo			break;
972195618Srpaulo#endif
973178354Ssam		}
974178354Ssam		ielen -= 2 + ie[1];
975178354Ssam		ie += 2 + ie[1];
976178354Ssam	}
977178354Ssam}
978178354Ssam
979178354Ssam/*
980138568Ssam * Reclaim any resources in a node and reset any critical
981138568Ssam * state.  Typically nodes are free'd immediately after,
982138568Ssam * but in some cases the storage may be reused so we need
983138568Ssam * to insure consistent state (should probably fix that).
984138568Ssam */
985116742Ssamstatic void
986138568Ssamnode_cleanup(struct ieee80211_node *ni)
987116742Ssam{
988178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
989195379Ssam	struct ieee80211com *ic = ni->ni_ic;
990170530Ssam	int i;
991138568Ssam
992138568Ssam	/* NB: preserve ni_table */
993138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
994178354Ssam		if (vap->iv_opmode != IEEE80211_M_STA)
995178354Ssam			vap->iv_ps_sta--;
996138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
997178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
998178354Ssam		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
999138568Ssam	}
1000147788Ssam	/*
1001173273Ssam	 * Cleanup any HT-related state.
1002173273Ssam	 */
1003173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
1004173273Ssam		ieee80211_ht_node_cleanup(ni);
1005190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1006190579Ssam	else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
1007190579Ssam		ieee80211_ff_node_cleanup(ni);
1008190579Ssam#endif
1009195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
1010173273Ssam	/*
1011195618Srpaulo	 * Cleanup any mesh-related state.
1012195618Srpaulo	 */
1013195618Srpaulo	if (vap->iv_opmode == IEEE80211_M_MBSS)
1014195618Srpaulo		ieee80211_mesh_node_cleanup(ni);
1015195618Srpaulo#endif
1016195618Srpaulo	/*
1017195379Ssam	 * Clear any staging queue entries.
1018195379Ssam	 */
1019195379Ssam	ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
1020195379Ssam
1021195379Ssam	/*
1022147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
1023147788Ssam	 * has happened.  This is probably not needed as the node
1024147788Ssam	 * should always be removed from the table so not found but
1025147788Ssam	 * do it just in case.
1026186099Ssam	 * Likewise clear the ASSOCID flag as these flags are intended
1027186099Ssam	 * to be managed in tandem.
1028147788Ssam	 */
1029186099Ssam	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
1030138568Ssam
1031138568Ssam	/*
1032138568Ssam	 * Drain power save queue and, if needed, clear TIM.
1033138568Ssam	 */
1034184288Ssam	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
1035178354Ssam		vap->iv_set_tim(ni, 0);
1036138568Ssam
1037138568Ssam	ni->ni_associd = 0;
1038138568Ssam	if (ni->ni_challenge != NULL) {
1039186302Ssam		free(ni->ni_challenge, M_80211_NODE);
1040138568Ssam		ni->ni_challenge = NULL;
1041138568Ssam	}
1042138568Ssam	/*
1043138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
1044138568Ssam	 * reusable during a re-auth/re-assoc state transition.
1045138568Ssam	 * If we remove these data they will not be recreated
1046138568Ssam	 * because they come from a probe-response or beacon frame
1047138568Ssam	 * which cannot be expected prior to the association-response.
1048138568Ssam	 * This should not be an issue when operating in other modes
1049138568Ssam	 * as stations leaving always go through a full state transition
1050138568Ssam	 * which will rebuild this state.
1051138568Ssam	 *
1052138568Ssam	 * XXX does this leave us open to inheriting old state?
1053138568Ssam	 */
1054254315Srpaulo	for (i = 0; i < nitems(ni->ni_rxfrag); i++)
1055138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
1056138568Ssam			m_freem(ni->ni_rxfrag[i]);
1057138568Ssam			ni->ni_rxfrag[i] = NULL;
1058138568Ssam		}
1059148863Ssam	/*
1060148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
1061148863Ssam	 */
1062148863Ssam	ieee80211_node_delucastkey(ni);
1063116742Ssam}
1064116742Ssam
1065116742Ssamstatic void
1066138568Ssamnode_free(struct ieee80211_node *ni)
1067116742Ssam{
1068138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1069138568Ssam
1070214894Sbschmidt	ieee80211_ratectl_node_deinit(ni);
1071138568Ssam	ic->ic_node_cleanup(ni);
1072178354Ssam	ieee80211_ies_cleanup(&ni->ni_ies);
1073184288Ssam	ieee80211_psq_cleanup(&ni->ni_psq);
1074186302Ssam	free(ni, M_80211_NODE);
1075116742Ssam}
1076116742Ssam
1077178354Ssamstatic void
1078178354Ssamnode_age(struct ieee80211_node *ni)
1079178354Ssam{
1080178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1081184303Ssam
1082184303Ssam	IEEE80211_NODE_LOCK_ASSERT(&vap->iv_ic->ic_sta);
1083184303Ssam
1084178354Ssam	/*
1085178354Ssam	 * Age frames on the power save queue.
1086178354Ssam	 */
1087184288Ssam	if (ieee80211_node_psq_age(ni) != 0 &&
1088184288Ssam	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
1089178354Ssam		vap->iv_set_tim(ni, 0);
1090178354Ssam	/*
1091178354Ssam	 * Age out HT resources (e.g. frames on the
1092178354Ssam	 * A-MPDU reorder queues).
1093178354Ssam	 */
1094178354Ssam	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
1095178354Ssam		ieee80211_ht_node_age(ni);
1096178354Ssam}
1097178354Ssam
1098170530Ssamstatic int8_t
1099138568Ssamnode_getrssi(const struct ieee80211_node *ni)
1100120104Ssam{
1101178354Ssam	uint32_t avgrssi = ni->ni_avgrssi;
1102178354Ssam	int32_t rssi;
1103178354Ssam
1104178354Ssam	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
1105178354Ssam		return 0;
1106178354Ssam	rssi = IEEE80211_RSSI_GET(avgrssi);
1107178354Ssam	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1108120104Ssam}
1109120104Ssam
1110116742Ssamstatic void
1111170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
1112170530Ssam{
1113178354Ssam	*rssi = node_getrssi(ni);
1114170530Ssam	*noise = ni->ni_noise;
1115170530Ssam}
1116170530Ssam
1117170530Ssamstatic void
1118178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni,
1119178354Ssam	struct ieee80211_mimo_info *info)
1120116742Ssam{
1121220445Sadrian	int i;
1122220445Sadrian	uint32_t avgrssi;
1123220445Sadrian	int32_t rssi;
1124220445Sadrian
1125220445Sadrian	bzero(info, sizeof(*info));
1126220445Sadrian
1127220445Sadrian	for (i = 0; i < ni->ni_mimo_chains; i++) {
1128220445Sadrian		avgrssi = ni->ni_mimo_rssi_ctl[i];
1129220445Sadrian		if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
1130220935Sadrian			info->rssi[i] = 0;
1131220445Sadrian		} else {
1132220445Sadrian			rssi = IEEE80211_RSSI_GET(avgrssi);
1133220935Sadrian			info->rssi[i] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
1134220445Sadrian		}
1135220935Sadrian		info->noise[i] = ni->ni_mimo_noise_ctl[i];
1136220445Sadrian	}
1137220445Sadrian
1138220935Sadrian	/* XXX ext radios? */
1139220935Sadrian
1140220445Sadrian	/* XXX EVM? */
1141178354Ssam}
1142178354Ssam
1143178354Ssamstruct ieee80211_node *
1144178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt,
1145178354Ssam	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
1146178354Ssam{
1147138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1148178354Ssam	struct ieee80211_node *ni;
1149116742Ssam	int hash;
1150116742Ssam
1151179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1152178354Ssam	if (ni == NULL) {
1153178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1154178354Ssam		return NULL;
1155178354Ssam	}
1156178354Ssam
1157178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1158140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1159138568Ssam		ether_sprintf(macaddr), nt->nt_name);
1160138568Ssam
1161116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1162195618Srpaulo	hash = IEEE80211_NODE_HASH(ic, macaddr);
1163138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
1164138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
1165138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
1166138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
1167183251Ssam	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1168178354Ssam	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1169178354Ssam	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1170139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
1171139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1172170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
1173184288Ssam	ieee80211_psq_init(&ni->ni_psq, "unknown");
1174195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
1175195618Srpaulo	if (vap->iv_opmode == IEEE80211_M_MBSS)
1176195618Srpaulo		ieee80211_mesh_node_init(vap, ni);
1177195618Srpaulo#endif
1178138568Ssam	IEEE80211_NODE_LOCK(nt);
1179138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1180138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1181138568Ssam	ni->ni_table = nt;
1182178354Ssam	ni->ni_vap = vap;
1183138568Ssam	ni->ni_ic = ic;
1184138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1185116742Ssam
1186184277Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1187184277Ssam	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
1188184277Ssam
1189217511Sbschmidt	ieee80211_ratectl_node_init(ni);
1190217511Sbschmidt
1191116742Ssam	return ni;
1192116742Ssam}
1193116742Ssam
1194148777Ssam/*
1195148777Ssam * Craft a temporary node suitable for sending a management frame
1196148777Ssam * to the specified station.  We craft only as much state as we
1197148777Ssam * need to do the work since the node will be immediately reclaimed
1198148777Ssam * once the send completes.
1199148777Ssam */
1200116742Ssamstruct ieee80211_node *
1201178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1202178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1203148777Ssam{
1204178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1205148777Ssam	struct ieee80211_node *ni;
1206148777Ssam
1207179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1208148777Ssam	if (ni != NULL) {
1209183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1210183259Ssam
1211178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1212148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1213148777Ssam
1214178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1215178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1216178354Ssam		ni->ni_vap = vap;
1217178354Ssam
1218148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1219183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1220148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1221148777Ssam		/* NB: required by ieee80211_fix_rate */
1222183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1223178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1224148777Ssam			IEEE80211_KEYIX_NONE);
1225183259Ssam		ni->ni_txpower = bss->ni_txpower;
1226148777Ssam		/* XXX optimize away */
1227184288Ssam		ieee80211_psq_init(&ni->ni_psq, "unknown");
1228217511Sbschmidt
1229217511Sbschmidt		ieee80211_ratectl_node_init(ni);
1230148777Ssam	} else {
1231148777Ssam		/* XXX msg */
1232178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1233148777Ssam	}
1234148777Ssam	return ni;
1235148777Ssam}
1236148777Ssam
1237148777Ssamstruct ieee80211_node *
1238178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1239178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1240116742Ssam{
1241178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1242138568Ssam	struct ieee80211_node *ni;
1243138568Ssam
1244178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1245116742Ssam	if (ni != NULL) {
1246183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1247127770Ssam		/*
1248178354Ssam		 * Inherit from iv_bss.
1249127770Ssam		 */
1250183259Ssam		copy_bss(ni, bss);
1251183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1252183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1253178354Ssam	}
1254116742Ssam	return ni;
1255116742Ssam}
1256116742Ssam
1257178354Ssam/*
1258178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1259178354Ssam * not associate so we just create create a new node and
1260178354Ssam * simulate an association.  The caller is responsible for
1261178354Ssam * installing the node as the bss node and handling any further
1262178354Ssam * setup work like authorizing the port.
1263178354Ssam */
1264178354Ssamstruct ieee80211_node *
1265178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1266178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1267178354Ssam{
1268178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1269178354Ssam	struct ieee80211_node *ni;
1270178354Ssam
1271178354Ssam	/* XXX check if node already in sta table? */
1272178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1273178354Ssam	if (ni != NULL) {
1274178354Ssam		ni->ni_wdsvap = vap;
1275178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1276178354Ssam		/*
1277178354Ssam		 * Inherit any manually configured settings.
1278178354Ssam		 */
1279183259Ssam		copy_bss(ni, vap->iv_bss);
1280178354Ssam		ieee80211_node_set_chan(ni, chan);
1281178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1282178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1283178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1284178354Ssam		/* NB: no associd for peer */
1285178354Ssam		/*
1286178354Ssam		 * There are no management frames to use to
1287178354Ssam		 * discover neighbor capabilities, so blindly
1288178354Ssam		 * propagate the local configuration.
1289178354Ssam		 */
1290178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1291178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1292190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1293178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1294178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1295190391Ssam#endif
1296178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1297193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1298178354Ssam			/*
1299178354Ssam			 * Device is HT-capable and HT is enabled for
1300178354Ssam			 * the vap; setup HT operation.  On return
1301178354Ssam			 * ni_chan will be adjusted to an HT channel.
1302178354Ssam			 */
1303178354Ssam			ieee80211_ht_wds_init(ni);
1304178354Ssam		} else {
1305178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1306178354Ssam			/*
1307178354Ssam			 * Force a legacy channel to be used.
1308178354Ssam			 */
1309178354Ssam			c = ieee80211_find_channel(ic,
1310178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1311178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1312178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1313178354Ssam			ni->ni_chan = c;
1314178354Ssam		}
1315178354Ssam	}
1316178354Ssam	return ni;
1317178354Ssam}
1318178354Ssam
1319178354Ssamstruct ieee80211_node *
1320138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1321178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1322178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1323138568Ssam#else
1324178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1325178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1326138568Ssam#endif
1327116742Ssam{
1328116742Ssam	struct ieee80211_node *ni;
1329116742Ssam	int hash;
1330116742Ssam
1331138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1332127772Ssam
1333195618Srpaulo	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1334138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1335116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1336138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1337138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1338178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1339140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1340140766Ssam			    func, line,
1341140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1342140766Ssam			    ieee80211_node_refcnt(ni));
1343138568Ssam#endif
1344127772Ssam			return ni;
1345116742Ssam		}
1346116742Ssam	}
1347127772Ssam	return NULL;
1348127772Ssam}
1349178354Ssam
1350178354Ssamstruct ieee80211_node *
1351138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1352178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1353178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1354178354Ssam#else
1355178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1356178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1357138568Ssam#endif
1358178354Ssam{
1359178354Ssam	struct ieee80211_node *ni;
1360127772Ssam
1361178354Ssam	IEEE80211_NODE_LOCK(nt);
1362178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1363178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1364178354Ssam	return ni;
1365178354Ssam}
1366178354Ssam
1367127772Ssamstruct ieee80211_node *
1368138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1369178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1370178354Ssam	const struct ieee80211vap *vap,
1371178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1372138568Ssam#else
1373178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1374178354Ssam	const struct ieee80211vap *vap,
1375178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1376138568Ssam#endif
1377127772Ssam{
1378127772Ssam	struct ieee80211_node *ni;
1379178354Ssam	int hash;
1380127772Ssam
1381178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1382178354Ssam
1383195618Srpaulo	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1384178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1385178354Ssam		if (ni->ni_vap == vap &&
1386178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1387178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1388178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1389178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1390178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1391178354Ssam			    func, line,
1392178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1393178354Ssam			    ieee80211_node_refcnt(ni));
1394178354Ssam#endif
1395178354Ssam			return ni;
1396178354Ssam		}
1397178354Ssam	}
1398178354Ssam	return NULL;
1399178354Ssam}
1400178354Ssam
1401178354Ssamstruct ieee80211_node *
1402178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1403178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1404178354Ssam	const struct ieee80211vap *vap,
1405178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1406178354Ssam#else
1407178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1408178354Ssam	const struct ieee80211vap *vap,
1409178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1410178354Ssam#endif
1411178354Ssam{
1412178354Ssam	struct ieee80211_node *ni;
1413178354Ssam
1414138568Ssam	IEEE80211_NODE_LOCK(nt);
1415178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1416138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1417116742Ssam	return ni;
1418116742Ssam}
1419116742Ssam
1420116742Ssam/*
1421138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1422138568Ssam * Note that for the driver's benefit we we treat this like
1423138568Ssam * an association so the driver has an opportunity to setup
1424138568Ssam * it's private state.
1425138568Ssam */
1426138568Ssamstruct ieee80211_node *
1427178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1428170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1429138568Ssam{
1430138568Ssam	struct ieee80211_node *ni;
1431138568Ssam
1432245928Sadrian	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1433153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1434178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1435138568Ssam	if (ni != NULL) {
1436178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1437178354Ssam
1438138568Ssam		/* XXX no rate negotiation; just dup */
1439178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1440188869Ssam		if (ieee80211_iserp_rateset(&ni->ni_rates))
1441188869Ssam			ni->ni_flags |= IEEE80211_NODE_ERP;
1442178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1443153404Ssam			/*
1444170530Ssam			 * In adhoc demo mode there are no management
1445170530Ssam			 * frames to use to discover neighbor capabilities,
1446170530Ssam			 * so blindly propagate the local configuration
1447170530Ssam			 * so we can do interesting things (e.g. use
1448170530Ssam			 * WME to disable ACK's).
1449153404Ssam			 */
1450178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1451153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1452190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1453178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1454170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1455190391Ssam#endif
1456153404Ssam		}
1457193966Ssam		ieee80211_node_setuptxparms(ni);
1458214894Sbschmidt		ieee80211_ratectl_node_init(ni);
1459178354Ssam		if (ic->ic_newassoc != NULL)
1460178354Ssam			ic->ic_newassoc(ni, 1);
1461170530Ssam		/* XXX not right for 802.1x/WPA */
1462170530Ssam		ieee80211_node_authorize(ni);
1463138568Ssam	}
1464138568Ssam	return ni;
1465138568Ssam}
1466138568Ssam
1467148936Ssamvoid
1468153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1469153073Ssam	const struct ieee80211_frame *wh,
1470153073Ssam	const struct ieee80211_scanparams *sp)
1471153073Ssam{
1472245928Sadrian	int do_ht_setup = 0;
1473245928Sadrian
1474153073Ssam	ni->ni_esslen = sp->ssid[1];
1475153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1476153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1477153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1478153073Ssam	ni->ni_intval = sp->bintval;
1479153073Ssam	ni->ni_capinfo = sp->capinfo;
1480153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1481153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1482153073Ssam	ni->ni_fhindex = sp->fhindex;
1483153073Ssam	ni->ni_erp = sp->erp;
1484153073Ssam	ni->ni_timoff = sp->timoff;
1485195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
1486195618Srpaulo	if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1487195618Srpaulo		ieee80211_mesh_init_neighbor(ni, wh, sp);
1488195618Srpaulo#endif
1489178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1490178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1491186659Ssam		if (ni->ni_ies.wme_ie != NULL)
1492186659Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1493186659Ssam		else
1494186659Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1495190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1496178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1497178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1498190391Ssam#endif
1499245928Sadrian		if (ni->ni_ies.htcap_ie != NULL)
1500245928Sadrian			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1501245928Sadrian		if (ni->ni_ies.htinfo_ie != NULL)
1502245928Sadrian			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1503245928Sadrian
1504245928Sadrian		if ((ni->ni_ies.htcap_ie != NULL) &&
1505245928Sadrian		    (ni->ni_ies.htinfo_ie != NULL) &&
1506245928Sadrian		    (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1507245928Sadrian			do_ht_setup = 1;
1508245928Sadrian		}
1509178354Ssam	}
1510178354Ssam
1511153073Ssam	/* NB: must be after ni_chan is setup */
1512165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1513165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1514165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1515245928Sadrian
1516245928Sadrian	/*
1517245928Sadrian	 * If the neighbor is HT compatible, flip that on.
1518245928Sadrian	 */
1519245928Sadrian	if (do_ht_setup) {
1520245928Sadrian		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1521245928Sadrian		    "%s: doing HT setup\n", __func__);
1522245928Sadrian		ieee80211_ht_node_init(ni);
1523245928Sadrian		ieee80211_ht_updateparams(ni,
1524245928Sadrian		    ni->ni_ies.htcap_ie,
1525245928Sadrian		    ni->ni_ies.htinfo_ie);
1526245928Sadrian		ieee80211_setup_htrates(ni,
1527245928Sadrian		    ni->ni_ies.htcap_ie,
1528245928Sadrian		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1529245928Sadrian		ieee80211_setup_basic_htrates(ni,
1530245928Sadrian		    ni->ni_ies.htinfo_ie);
1531245928Sadrian		ieee80211_node_setuptxparms(ni);
1532245928Sadrian		ieee80211_ratectl_node_init(ni);
1533245928Sadrian	}
1534153073Ssam}
1535153073Ssam
1536148936Ssam/*
1537148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1538148936Ssam * or probe response frame.  Note that for the driver's
1539148936Ssam * benefit we we treat this like an association so the
1540148936Ssam * driver has an opportunity to setup it's private state.
1541148936Ssam */
1542148936Ssamstruct ieee80211_node *
1543178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1544148936Ssam	const struct ieee80211_frame *wh,
1545148936Ssam	const struct ieee80211_scanparams *sp)
1546148936Ssam{
1547148936Ssam	struct ieee80211_node *ni;
1548148936Ssam
1549245928Sadrian	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1550153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1551178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1552148936Ssam	if (ni != NULL) {
1553178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1554178354Ssam
1555153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1556188869Ssam		if (ieee80211_iserp_rateset(&ni->ni_rates))
1557188869Ssam			ni->ni_flags |= IEEE80211_NODE_ERP;
1558193966Ssam		ieee80211_node_setuptxparms(ni);
1559214894Sbschmidt		ieee80211_ratectl_node_init(ni);
1560148936Ssam		if (ic->ic_newassoc != NULL)
1561148936Ssam			ic->ic_newassoc(ni, 1);
1562148936Ssam		/* XXX not right for 802.1x/WPA */
1563148936Ssam		ieee80211_node_authorize(ni);
1564148936Ssam	}
1565148936Ssam	return ni;
1566148936Ssam}
1567148936Ssam
1568179220Ssam#define	IS_PROBEREQ(wh) \
1569179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1570179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1571179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1572179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1573179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1574170530Ssam
1575179220Ssamstatic __inline struct ieee80211_node *
1576179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1577179220Ssam    const struct ieee80211_frame_min *wh)
1578179220Ssam{
1579179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1580179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1581179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1582179220Ssam}
1583179220Ssam
1584138568Ssam/*
1585138568Ssam * Locate the node for sender, track state, and then pass the
1586179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1587179220Ssam * we can return NULL if the sender is not in the table.
1588138568Ssam */
1589138568Ssamstruct ieee80211_node *
1590138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1591138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1592138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1593138568Ssam#else
1594138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1595138568Ssam	const struct ieee80211_frame_min *wh)
1596138568Ssam#endif
1597138568Ssam{
1598138568Ssam	struct ieee80211_node_table *nt;
1599138568Ssam	struct ieee80211_node *ni;
1600138568Ssam
1601170530Ssam	nt = &ic->ic_sta;
1602138568Ssam	IEEE80211_NODE_LOCK(nt);
1603179220Ssam	ni = _find_rxnode(nt, wh);
1604138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1605138568Ssam
1606148863Ssam	return ni;
1607148863Ssam}
1608148863Ssam
1609148863Ssam/*
1610148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1611148863Ssam * key index as a hint to locate the node in the key
1612148863Ssam * mapping table.  If an entry is present at the key
1613148863Ssam * index we return it; otherwise do a normal lookup and
1614148863Ssam * update the mapping table if the station has a unicast
1615148863Ssam * key assigned to it.
1616148863Ssam */
1617148863Ssamstruct ieee80211_node *
1618148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1619148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1620148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1621148863Ssam	const char *func, int line)
1622148863Ssam#else
1623148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1624148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1625148863Ssam#endif
1626148863Ssam{
1627148863Ssam	struct ieee80211_node_table *nt;
1628148863Ssam	struct ieee80211_node *ni;
1629148863Ssam
1630170530Ssam	nt = &ic->ic_sta;
1631148863Ssam	IEEE80211_NODE_LOCK(nt);
1632148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1633148863Ssam		ni = nt->nt_keyixmap[keyix];
1634148863Ssam	else
1635148863Ssam		ni = NULL;
1636148863Ssam	if (ni == NULL) {
1637179220Ssam		ni = _find_rxnode(nt, wh);
1638178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1639148863Ssam			/*
1640148863Ssam			 * If the station has a unicast key cache slot
1641148863Ssam			 * assigned update the key->node mapping table.
1642148863Ssam			 */
1643148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1644148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1645148863Ssam			if (keyix < nt->nt_keyixmax &&
1646148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1647178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1648178354Ssam				    IEEE80211_MSG_NODE,
1649148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1650148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1651148863Ssam				    ieee80211_node_refcnt(ni)+1);
1652148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1653148863Ssam			}
1654148863Ssam		}
1655179220Ssam	} else {
1656179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1657179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1658179220Ssam		else
1659179220Ssam			ieee80211_ref_node(ni);
1660179220Ssam	}
1661148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1662148863Ssam
1663148863Ssam	return ni;
1664148863Ssam}
1665179220Ssam#undef IS_BCAST_PROBEREQ
1666179220Ssam#undef IS_PROBEREQ
1667138568Ssam
1668138568Ssam/*
1669127772Ssam * Return a reference to the appropriate node for sending
1670127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1671127772Ssam */
1672127772Ssamstruct ieee80211_node *
1673138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1674178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1675178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1676138568Ssam	const char *func, int line)
1677138568Ssam#else
1678178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1679178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1680138568Ssam#endif
1681127772Ssam{
1682178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1683127772Ssam	struct ieee80211_node *ni;
1684127772Ssam
1685127772Ssam	/*
1686127772Ssam	 * The destination address should be in the node table
1687148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1688148863Ssam	 * also optimize station mode operation, all frames go
1689148863Ssam	 * to the bss node.
1690127772Ssam	 */
1691127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1692138568Ssam	IEEE80211_NODE_LOCK(nt);
1693178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1694178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1695178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1696178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1697186099Ssam	else
1698178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1699138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1700138568Ssam
1701138568Ssam	if (ni == NULL) {
1702178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1703178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1704140497Ssam			/*
1705140497Ssam			 * In adhoc mode cons up a node for the destination.
1706140497Ssam			 * Note that we need an additional reference for the
1707178354Ssam			 * caller to be consistent with
1708178354Ssam			 * ieee80211_find_node_locked.
1709140497Ssam			 */
1710178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1711140497Ssam			if (ni != NULL)
1712140497Ssam				(void) ieee80211_ref_node(ni);
1713140497Ssam		} else {
1714178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1715178354Ssam			    "no node, discard frame (%s)", __func__);
1716178354Ssam			vap->iv_stats.is_tx_nonode++;
1717127772Ssam		}
1718127772Ssam	}
1719127772Ssam	return ni;
1720127772Ssam}
1721127772Ssam
1722116742Ssamstatic void
1723138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1724116742Ssam{
1725138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1726119150Ssam
1727179641Ssam	/*
1728179641Ssam	 * NB: careful about referencing the vap as it may be
1729179641Ssam	 * gone if the last reference was held by a driver.
1730179641Ssam	 * We know the com will always be present so it's safe
1731179641Ssam	 * to use ni_ic below to reclaim resources.
1732179641Ssam	 */
1733179641Ssam#if 0
1734178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1735140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1736140766Ssam		ether_sprintf(ni->ni_macaddr),
1737138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1738179641Ssam#endif
1739179641Ssam	if (ni->ni_associd != 0) {
1740179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1741179641Ssam		if (vap->iv_aid_bitmap != NULL)
1742179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1743179641Ssam	}
1744138568Ssam	if (nt != NULL) {
1745138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1746138568Ssam		LIST_REMOVE(ni, ni_hash);
1747138568Ssam	}
1748179641Ssam	ni->ni_ic->ic_node_free(ni);
1749116742Ssam}
1750116742Ssam
1751116742Ssamvoid
1752138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1753138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1754138568Ssam#else
1755138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1756138568Ssam#endif
1757116742Ssam{
1758138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1759119150Ssam
1760138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1761178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1762140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1763138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1764138568Ssam#endif
1765148863Ssam	if (nt != NULL) {
1766148863Ssam		IEEE80211_NODE_LOCK(nt);
1767148863Ssam		if (ieee80211_node_dectestref(ni)) {
1768148863Ssam			/*
1769148863Ssam			 * Last reference, reclaim state.
1770148863Ssam			 */
1771138568Ssam			_ieee80211_free_node(ni);
1772148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1773148863Ssam		    nt->nt_keyixmap != NULL) {
1774148863Ssam			ieee80211_keyix keyix;
1775148863Ssam			/*
1776148863Ssam			 * Check for a last reference in the key mapping table.
1777148863Ssam			 */
1778148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1779148863Ssam			if (keyix < nt->nt_keyixmax &&
1780148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1781178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1782178354Ssam				    IEEE80211_MSG_NODE,
1783148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1784148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1785148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1786148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1787148863Ssam				_ieee80211_free_node(ni);
1788148863Ssam			}
1789148863Ssam		}
1790148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1791148863Ssam	} else {
1792148863Ssam		if (ieee80211_node_dectestref(ni))
1793138568Ssam			_ieee80211_free_node(ni);
1794116742Ssam	}
1795116742Ssam}
1796116742Ssam
1797138568Ssam/*
1798148863Ssam * Reclaim a unicast key and clear any key cache state.
1799148863Ssam */
1800148863Ssamint
1801148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1802148863Ssam{
1803179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1804179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1805148863Ssam	struct ieee80211_node *nikey;
1806148863Ssam	ieee80211_keyix keyix;
1807148863Ssam	int isowned, status;
1808148863Ssam
1809148863Ssam	/*
1810148863Ssam	 * NB: We must beware of LOR here; deleting the key
1811148863Ssam	 * can cause the crypto layer to block traffic updates
1812148863Ssam	 * which can generate a LOR against the node table lock;
1813148863Ssam	 * grab it here and stash the key index for our use below.
1814148863Ssam	 *
1815148863Ssam	 * Must also beware of recursion on the node table lock.
1816148863Ssam	 * When called from node_cleanup we may already have
1817148863Ssam	 * the node table lock held.  Unfortunately there's no
1818148863Ssam	 * way to separate out this path so we must do this
1819148863Ssam	 * conditionally.
1820148863Ssam	 */
1821148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1822148863Ssam	if (!isowned)
1823148863Ssam		IEEE80211_NODE_LOCK(nt);
1824179641Ssam	nikey = NULL;
1825179641Ssam	status = 1;		/* NB: success */
1826186144Ssam	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1827179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1828179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1829179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1830179641Ssam			nikey = nt->nt_keyixmap[keyix];
1831201758Smbr			nt->nt_keyixmap[keyix] = NULL;
1832179641Ssam		}
1833179641Ssam	}
1834148863Ssam	if (!isowned)
1835178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1836148863Ssam
1837148863Ssam	if (nikey != NULL) {
1838148863Ssam		KASSERT(nikey == ni,
1839148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1840179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1841148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1842148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1843148863Ssam			ieee80211_node_refcnt(ni)-1);
1844148863Ssam		ieee80211_free_node(ni);
1845148863Ssam	}
1846148863Ssam	return status;
1847148863Ssam}
1848148863Ssam
1849148863Ssam/*
1850138568Ssam * Reclaim a node.  If this is the last reference count then
1851138568Ssam * do the normal free work.  Otherwise remove it from the node
1852138568Ssam * table and mark it gone by clearing the back-reference.
1853138568Ssam */
1854138568Ssamstatic void
1855138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1856116742Ssam{
1857148863Ssam	ieee80211_keyix keyix;
1858138568Ssam
1859148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1860148863Ssam
1861178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1862140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1863140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1864140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1865148863Ssam	/*
1866148863Ssam	 * Clear any entry in the unicast key mapping table.
1867148863Ssam	 * We need to do it here so rx lookups don't find it
1868148863Ssam	 * in the mapping table even if it's not in the hash
1869148863Ssam	 * table.  We cannot depend on the mapping table entry
1870148863Ssam	 * being cleared because the node may not be free'd.
1871148863Ssam	 */
1872148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1873148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1874148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1875178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1876188494Ssam			"%s: %p<%s> clear key map entry %u\n",
1877188494Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1878148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1879148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1880148863Ssam	}
1881138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1882138568Ssam		/*
1883138568Ssam		 * Other references are present, just remove the
1884138568Ssam		 * node from the table so it cannot be found.  When
1885138568Ssam		 * the references are dropped storage will be
1886140753Ssam		 * reclaimed.
1887138568Ssam		 */
1888138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1889138568Ssam		LIST_REMOVE(ni, ni_hash);
1890138568Ssam		ni->ni_table = NULL;		/* clear reference */
1891138568Ssam	} else
1892138568Ssam		_ieee80211_free_node(ni);
1893138568Ssam}
1894138568Ssam
1895178354Ssam/*
1896178354Ssam * Node table support.
1897178354Ssam */
1898178354Ssam
1899178354Ssamstatic void
1900178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1901178354Ssam	struct ieee80211_node_table *nt,
1902178354Ssam	const char *name, int inact, int keyixmax)
1903178354Ssam{
1904178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1905178354Ssam
1906178354Ssam	nt->nt_ic = ic;
1907178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1908178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1909178354Ssam	TAILQ_INIT(&nt->nt_node);
1910178354Ssam	nt->nt_name = name;
1911178354Ssam	nt->nt_scangen = 1;
1912178354Ssam	nt->nt_inact_init = inact;
1913178354Ssam	nt->nt_keyixmax = keyixmax;
1914178354Ssam	if (nt->nt_keyixmax > 0) {
1915186302Ssam		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1916184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1917178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1918178354Ssam		if (nt->nt_keyixmap == NULL)
1919178354Ssam			if_printf(ic->ic_ifp,
1920178354Ssam			    "Cannot allocate key index map with %u entries\n",
1921178354Ssam			    keyixmax);
1922178354Ssam	} else
1923178354Ssam		nt->nt_keyixmap = NULL;
1924178354Ssam}
1925178354Ssam
1926178354Ssamstatic void
1927178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1928178354Ssam	struct ieee80211vap *match)
1929178354Ssam{
1930178354Ssam	struct ieee80211_node *ni, *next;
1931178354Ssam
1932178354Ssam	IEEE80211_NODE_LOCK(nt);
1933178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1934178354Ssam		if (match != NULL && ni->ni_vap != match)
1935178354Ssam			continue;
1936178354Ssam		/* XXX can this happen?  if so need's work */
1937138568Ssam		if (ni->ni_associd != 0) {
1938178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1939178354Ssam
1940178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1941178354Ssam				vap->iv_auth->ia_node_leave(ni);
1942178354Ssam			if (vap->iv_aid_bitmap != NULL)
1943178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1944138568Ssam		}
1945178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1946138568Ssam		node_reclaim(nt, ni);
1947138568Ssam	}
1948178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1949178354Ssam		/*
1950178354Ssam		 * Make a separate pass to clear references to this vap
1951178354Ssam		 * held by DWDS entries.  They will not be matched above
1952178354Ssam		 * because ni_vap will point to the ap vap but we still
1953178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1954178354Ssam		 * and/or reset.
1955178354Ssam		 */
1956178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1957178354Ssam			if (ni->ni_wdsvap == match)
1958178354Ssam				ni->ni_wdsvap = NULL;
1959178354Ssam	}
1960178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1961116742Ssam}
1962116742Ssam
1963178354Ssamstatic void
1964178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1965178354Ssam{
1966178354Ssam	ieee80211_node_table_reset(nt, NULL);
1967178354Ssam	if (nt->nt_keyixmap != NULL) {
1968178354Ssam#ifdef DIAGNOSTIC
1969178354Ssam		/* XXX verify all entries are NULL */
1970178354Ssam		int i;
1971178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1972178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1973178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1974178354Ssam					nt->nt_name, i);
1975178354Ssam#endif
1976186302Ssam		free(nt->nt_keyixmap, M_80211_NODE);
1977178354Ssam		nt->nt_keyixmap = NULL;
1978178354Ssam	}
1979178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1980178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1981178354Ssam}
1982178354Ssam
1983120483Ssam/*
1984138568Ssam * Timeout inactive stations and do related housekeeping.
1985138568Ssam * Note that we cannot hold the node lock while sending a
1986138568Ssam * frame as this would lead to a LOR.  Instead we use a
1987138568Ssam * generation number to mark nodes that we've scanned and
1988138568Ssam * drop the lock and restart a scan if we have to time out
1989138568Ssam * a node.  Since we are single-threaded by virtue of
1990120483Ssam * controlling the inactivity timer we can be sure this will
1991120483Ssam * process each node only once.
1992120483Ssam */
1993138568Ssamstatic void
1994178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1995116742Ssam{
1996178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1997178354Ssam	struct ieee80211vap *vap;
1998120483Ssam	struct ieee80211_node *ni;
1999178354Ssam	int gen = 0;
2000116742Ssam
2001178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2002154532Ssam	gen = ++nt->nt_scangen;
2003120483Ssamrestart:
2004138568Ssam	IEEE80211_NODE_LOCK(nt);
2005138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2006120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
2007120483Ssam			continue;
2008120483Ssam		ni->ni_scangen = gen;
2009138568Ssam		/*
2010147788Ssam		 * Ignore entries for which have yet to receive an
2011147788Ssam		 * authentication frame.  These are transient and
2012147788Ssam		 * will be reclaimed when the last reference to them
2013147788Ssam		 * goes away (when frame xmits complete).
2014147788Ssam		 */
2015178354Ssam		vap = ni->ni_vap;
2016178354Ssam		/*
2017178354Ssam		 * Only process stations when in RUN state.  This
2018178354Ssam		 * insures, for example, that we don't timeout an
2019178354Ssam		 * inactive station during CAC.  Note that CSA state
2020178354Ssam		 * is actually handled in ieee80211_node_timeout as
2021178354Ssam		 * it applies to more than timeout processing.
2022178354Ssam		 */
2023178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2024178354Ssam			continue;
2025178354Ssam		/* XXX can vap be NULL? */
2026178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2027178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2028148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2029147788Ssam			continue;
2030147788Ssam		/*
2031138568Ssam		 * Free fragment if not needed anymore
2032138568Ssam		 * (last fragment older than 1s).
2033178354Ssam		 * XXX doesn't belong here, move to node_age
2034138568Ssam		 */
2035138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
2036138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
2037138568Ssam			m_freem(ni->ni_rxfrag[0]);
2038138568Ssam			ni->ni_rxfrag[0] = NULL;
2039138568Ssam		}
2040184277Ssam		if (ni->ni_inact > 0) {
2041172062Ssam			ni->ni_inact--;
2042184277Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2043184277Ssam			    "%s: inact %u inact_reload %u nrates %u",
2044184277Ssam			    __func__, ni->ni_inact, ni->ni_inact_reload,
2045184277Ssam			    ni->ni_rates.rs_nrates);
2046184277Ssam		}
2047140498Ssam		/*
2048140498Ssam		 * Special case ourself; we may be idle for extended periods
2049140498Ssam		 * of time and regardless reclaiming our state is wrong.
2050178354Ssam		 * XXX run ic_node_age
2051140498Ssam		 */
2052178354Ssam		if (ni == vap->iv_bss)
2053140498Ssam			continue;
2054178354Ssam		if (ni->ni_associd != 0 ||
2055178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
2056178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2057119150Ssam			/*
2058178354Ssam			 * Age/drain resources held by the station.
2059138568Ssam			 */
2060178354Ssam			ic->ic_node_age(ni);
2061138568Ssam			/*
2062138568Ssam			 * Probe the station before time it out.  We
2063138568Ssam			 * send a null data frame which may not be
2064138568Ssam			 * universally supported by drivers (need it
2065138568Ssam			 * for ps-poll support so it should be...).
2066170530Ssam			 *
2067170530Ssam			 * XXX don't probe the station unless we've
2068170530Ssam			 *     received a frame from them (and have
2069170530Ssam			 *     some idea of the rates they are capable
2070170530Ssam			 *     of); this will get fixed more properly
2071170530Ssam			 *     soon with better handling of the rate set.
2072138568Ssam			 */
2073178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2074172062Ssam			    (0 < ni->ni_inact &&
2075178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
2076170530Ssam			    ni->ni_rates.rs_nrates != 0) {
2077178354Ssam				IEEE80211_NOTE(vap,
2078148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2079148320Ssam				    ni, "%s",
2080148320Ssam				    "probe station due to inactivity");
2081148582Ssam				/*
2082148582Ssam				 * Grab a reference before unlocking the table
2083148582Ssam				 * so the node cannot be reclaimed before we
2084148582Ssam				 * send the frame. ieee80211_send_nulldata
2085148582Ssam				 * understands we've done this and reclaims the
2086148582Ssam				 * ref for us as needed.
2087148582Ssam				 */
2088148582Ssam				ieee80211_ref_node(ni);
2089138568Ssam				IEEE80211_NODE_UNLOCK(nt);
2090148301Ssam				ieee80211_send_nulldata(ni);
2091138568Ssam				/* XXX stat? */
2092138568Ssam				goto restart;
2093138568Ssam			}
2094138568Ssam		}
2095178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2096172062Ssam		    ni->ni_inact <= 0) {
2097178354Ssam			IEEE80211_NOTE(vap,
2098148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2099148320Ssam			    "station timed out due to inactivity "
2100148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
2101138568Ssam			/*
2102138568Ssam			 * Send a deauthenticate frame and drop the station.
2103138568Ssam			 * This is somewhat complicated due to reference counts
2104138568Ssam			 * and locking.  At this point a station will typically
2105138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
2106138568Ssam			 * will do a "free" of the node which will drop the
2107138568Ssam			 * reference count.  But in the meantime a reference
2108138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
2109138568Ssam			 * of the node will happen either after the tx is
2110138568Ssam			 * completed or by ieee80211_node_leave.
2111120483Ssam			 *
2112138568Ssam			 * Separately we must drop the node lock before sending
2113170530Ssam			 * in case the driver takes a lock, as this can result
2114170530Ssam			 * in a LOR between the node lock and the driver lock.
2115119150Ssam			 */
2116172229Ssam			ieee80211_ref_node(ni);
2117138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2118138568Ssam			if (ni->ni_associd != 0) {
2119178354Ssam				IEEE80211_SEND_MGMT(ni,
2120138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
2121138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
2122138568Ssam			}
2123178354Ssam			ieee80211_node_leave(ni);
2124172229Ssam			ieee80211_free_node(ni);
2125178354Ssam			vap->iv_stats.is_node_timeout++;
2126120483Ssam			goto restart;
2127120483Ssam		}
2128116742Ssam	}
2129138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2130138568Ssam
2131178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2132170530Ssam}
2133138568Ssam
2134178354Ssam/*
2135178354Ssam * Aggressively reclaim resources.  This should be used
2136178354Ssam * only in a critical situation to reclaim mbuf resources.
2137178354Ssam */
2138170530Ssamvoid
2139178354Ssamieee80211_drain(struct ieee80211com *ic)
2140178354Ssam{
2141178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2142178354Ssam	struct ieee80211vap *vap;
2143178354Ssam	struct ieee80211_node *ni;
2144178354Ssam
2145178354Ssam	IEEE80211_NODE_LOCK(nt);
2146178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2147178354Ssam		/*
2148178354Ssam		 * Ignore entries for which have yet to receive an
2149178354Ssam		 * authentication frame.  These are transient and
2150178354Ssam		 * will be reclaimed when the last reference to them
2151178354Ssam		 * goes away (when frame xmits complete).
2152178354Ssam		 */
2153178354Ssam		vap = ni->ni_vap;
2154178354Ssam		/*
2155178354Ssam		 * Only process stations when in RUN state.  This
2156178354Ssam		 * insures, for example, that we don't timeout an
2157178354Ssam		 * inactive station during CAC.  Note that CSA state
2158178354Ssam		 * is actually handled in ieee80211_node_timeout as
2159178354Ssam		 * it applies to more than timeout processing.
2160178354Ssam		 */
2161178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2162178354Ssam			continue;
2163178354Ssam		/* XXX can vap be NULL? */
2164178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2165178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2166178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2167178354Ssam			continue;
2168178354Ssam		/*
2169178354Ssam		 * Free fragments.
2170178354Ssam		 * XXX doesn't belong here, move to node_drain
2171178354Ssam		 */
2172178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2173178354Ssam			m_freem(ni->ni_rxfrag[0]);
2174178354Ssam			ni->ni_rxfrag[0] = NULL;
2175178354Ssam		}
2176178354Ssam		/*
2177178354Ssam		 * Drain resources held by the station.
2178178354Ssam		 */
2179178354Ssam		ic->ic_node_drain(ni);
2180178354Ssam	}
2181178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2182178354Ssam}
2183178354Ssam
2184178354Ssam/*
2185178354Ssam * Per-ieee80211com inactivity timer callback.
2186178354Ssam */
2187178354Ssamvoid
2188170530Ssamieee80211_node_timeout(void *arg)
2189170530Ssam{
2190170530Ssam	struct ieee80211com *ic = arg;
2191170530Ssam
2192178354Ssam	/*
2193178354Ssam	 * Defer timeout processing if a channel switch is pending.
2194178354Ssam	 * We typically need to be mute so not doing things that
2195178354Ssam	 * might generate frames is good to handle in one place.
2196178354Ssam	 * Supressing the station timeout processing may extend the
2197178354Ssam	 * lifetime of inactive stations (by not decrementing their
2198178354Ssam	 * idle counters) but this should be ok unless the CSA is
2199178354Ssam	 * active for an unusually long time.
2200178354Ssam	 */
2201178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2202178354Ssam		ieee80211_scan_timeout(ic);
2203178354Ssam		ieee80211_timeout_stations(ic);
2204195379Ssam		ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2205170530Ssam
2206178354Ssam		IEEE80211_LOCK(ic);
2207178354Ssam		ieee80211_erp_timeout(ic);
2208178354Ssam		ieee80211_ht_timeout(ic);
2209178354Ssam		IEEE80211_UNLOCK(ic);
2210178354Ssam	}
2211170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2212170530Ssam		ieee80211_node_timeout, ic);
2213116742Ssam}
2214116742Ssam
2215239312Sadrian/*
2216239312Sadrian * Iterate over the node table and return an array of ref'ed nodes.
2217239312Sadrian *
2218239312Sadrian * This is separated out from calling the actual node function so that
2219239312Sadrian * no LORs will occur.
2220239312Sadrian *
2221239312Sadrian * If there are too many nodes (ie, the number of nodes doesn't fit
2222239312Sadrian * within 'max_aid' entries) then the node references will be freed
2223239312Sadrian * and an error will be returned.
2224239312Sadrian *
2225239312Sadrian * The responsibility of allocating and freeing "ni_arr" is up to
2226239312Sadrian * the caller.
2227239312Sadrian */
2228239312Sadrianint
2229239312Sadrianieee80211_iterate_nt(struct ieee80211_node_table *nt,
2230239312Sadrian    struct ieee80211_node **ni_arr, uint16_t max_aid)
2231116742Ssam{
2232239312Sadrian	u_int gen;
2233239312Sadrian	int i, j, ret;
2234116742Ssam	struct ieee80211_node *ni;
2235116742Ssam
2236178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2237239312Sadrian	IEEE80211_NODE_LOCK(nt);
2238239312Sadrian
2239154532Ssam	gen = ++nt->nt_scangen;
2240239312Sadrian	i = ret = 0;
2241239312Sadrian
2242239312Sadrian	/*
2243239312Sadrian	 * We simply assume here that since the node
2244239312Sadrian	 * scan generation doesn't change (as
2245239312Sadrian	 * we are holding both the node table and
2246239312Sadrian	 * node table iteration locks), we can simply
2247239312Sadrian	 * assign it to the node here.
2248239312Sadrian	 */
2249138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2250239312Sadrian		if (i >= max_aid) {
2251239312Sadrian			ret = E2BIG;
2252239312Sadrian			if_printf(nt->nt_ic->ic_ifp,
2253239312Sadrian			    "Node array overflow: max=%u", max_aid);
2254239312Sadrian			break;
2255138568Ssam		}
2256239312Sadrian		ni_arr[i] = ieee80211_ref_node(ni);
2257239312Sadrian		ni_arr[i]->ni_scangen = gen;
2258239312Sadrian		i++;
2259138568Ssam	}
2260239312Sadrian
2261239312Sadrian	/*
2262239312Sadrian	 * It's safe to unlock here.
2263239312Sadrian	 *
2264239312Sadrian	 * If we're successful, the list is returned.
2265239312Sadrian	 * If we're unsuccessful, the list is ignored
2266239312Sadrian	 * and we remove our references.
2267239312Sadrian	 *
2268239312Sadrian	 * This avoids any potential LOR with
2269239312Sadrian	 * ieee80211_free_node().
2270239312Sadrian	 */
2271138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2272239312Sadrian	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2273138568Ssam
2274239312Sadrian	/*
2275239312Sadrian	 * If ret is non-zero, we hit some kind of error.
2276239312Sadrian	 * Rather than walking some nodes, we'll walk none
2277239312Sadrian	 * of them.
2278239312Sadrian	 */
2279239312Sadrian	if (ret) {
2280239312Sadrian		for (j = 0; j < i; j++) {
2281239312Sadrian			/* ieee80211_free_node() locks by itself */
2282239312Sadrian			ieee80211_free_node(ni_arr[j]);
2283239312Sadrian		}
2284239312Sadrian	}
2285239312Sadrian
2286239312Sadrian	return (ret);
2287116742Ssam}
2288138568Ssam
2289239312Sadrian/*
2290239312Sadrian * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2291239312Sadrian * reference in the source.
2292239312Sadrian *
2293239312Sadrian * Note that this fetches 'max_aid' from the first VAP, rather than finding
2294239312Sadrian * the largest max_aid from all VAPs.
2295239312Sadrian */
2296138568Ssamvoid
2297239312Sadrianieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2298239312Sadrian	ieee80211_iter_func *f, void *arg)
2299239312Sadrian{
2300239312Sadrian	struct ieee80211_node **ni_arr;
2301239319Sadrian	size_t size;
2302239312Sadrian	int i;
2303239312Sadrian	uint16_t max_aid;
2304240574Sadrian	struct ieee80211vap *vap;
2305239312Sadrian
2306240574Sadrian	/* Overdoing it default */
2307240574Sadrian	max_aid = IEEE80211_AID_MAX;
2308240574Sadrian
2309240574Sadrian	/* Handle the case of there being no vaps just yet */
2310240574Sadrian	vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps);
2311240574Sadrian	if (vap != NULL)
2312240574Sadrian		max_aid = vap->iv_max_aid;
2313240574Sadrian
2314239312Sadrian	size = max_aid * sizeof(struct ieee80211_node *);
2315239312Sadrian	ni_arr = (struct ieee80211_node **) malloc(size, M_80211_NODE,
2316239312Sadrian	    M_NOWAIT | M_ZERO);
2317239312Sadrian	if (ni_arr == NULL)
2318239312Sadrian		return;
2319239312Sadrian
2320239312Sadrian	/*
2321239312Sadrian	 * If this fails, the node table won't have any
2322239312Sadrian	 * valid entries - ieee80211_iterate_nt() frees
2323239312Sadrian	 * the references to them.  So don't try walking
2324239312Sadrian	 * the table; just skip to the end and free the
2325239312Sadrian	 * temporary memory.
2326239312Sadrian	 */
2327239319Sadrian	if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0)
2328239312Sadrian		goto done;
2329239312Sadrian
2330239312Sadrian	for (i = 0; i < max_aid; i++) {
2331239312Sadrian		if (ni_arr[i] == NULL)	/* end of the list */
2332239312Sadrian			break;
2333239312Sadrian		(*f)(arg, ni_arr[i]);
2334239312Sadrian		/* ieee80211_free_node() locks by itself */
2335239312Sadrian		ieee80211_free_node(ni_arr[i]);
2336239312Sadrian	}
2337239312Sadrian
2338239312Sadriandone:
2339239312Sadrian	free(ni_arr, M_80211_NODE);
2340239312Sadrian}
2341239312Sadrian
2342239312Sadrianvoid
2343138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2344138568Ssam{
2345138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2346138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2347138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2348138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2349138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2350138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2351138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2352167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2353167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2354167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2355138568Ssam		ni->ni_rxfragstamp);
2356192468Ssam	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2357192468Ssam		node_getrssi(ni), ni->ni_noise,
2358170530Ssam		ni->ni_intval, ni->ni_capinfo);
2359138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2360138568Ssam		ether_sprintf(ni->ni_bssid),
2361138568Ssam		ni->ni_esslen, ni->ni_essid,
2362138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2363184277Ssam	printf("\tinact %u inact_reload %u txrate %u\n",
2364184277Ssam		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2365170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2366170530Ssam		ni->ni_htcap, ni->ni_htparam,
2367170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2368170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2369170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2370138568Ssam}
2371138568Ssam
2372138568Ssamvoid
2373138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2374138568Ssam{
2375138568Ssam	ieee80211_iterate_nodes(nt,
2376138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2377138568Ssam}
2378138568Ssam
2379179642Ssamstatic void
2380179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2381172211Ssam{
2382178354Ssam	struct ieee80211vap *vap;
2383178354Ssam
2384178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2385178354Ssam
2386178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2387178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2388178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2389172211Ssam}
2390172211Ssam
2391179642Ssamvoid
2392179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2393179642Ssam{
2394179642Ssam	IEEE80211_LOCK(ic);
2395179642Ssam	ieee80211_notify_erp_locked(ic);
2396179642Ssam	IEEE80211_UNLOCK(ic);
2397179642Ssam}
2398179642Ssam
2399138568Ssam/*
2400138568Ssam * Handle a station joining an 11g network.
2401138568Ssam */
2402138568Ssamstatic void
2403178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2404138568Ssam{
2405178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2406138568Ssam
2407173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2408173273Ssam
2409138568Ssam	/*
2410138568Ssam	 * Station isn't capable of short slot time.  Bump
2411138568Ssam	 * the count of long slot time stations and disable
2412138568Ssam	 * use of short slot time.  Note that the actual switch
2413138568Ssam	 * over to long slot time use may not occur until the
2414138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2415138568Ssam	 */
2416138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2417138568Ssam		ic->ic_longslotsta++;
2418178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2419172211Ssam		    "station needs long slot time, count %d",
2420172211Ssam		    ic->ic_longslotsta);
2421138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2422170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2423170530Ssam			/*
2424170530Ssam			 * Don't force slot time when switched to turbo
2425170530Ssam			 * mode as non-ERP stations won't be present; this
2426170530Ssam			 * need only be done when on the normal G channel.
2427170530Ssam			 */
2428170530Ssam			ieee80211_set_shortslottime(ic, 0);
2429170530Ssam		}
2430138568Ssam	}
2431138568Ssam	/*
2432138568Ssam	 * If the new station is not an ERP station
2433138568Ssam	 * then bump the counter and enable protection
2434138568Ssam	 * if configured.
2435138568Ssam	 */
2436178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2437138568Ssam		ic->ic_nonerpsta++;
2438178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2439172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2440172211Ssam		    ic->ic_nonerpsta);
2441138568Ssam		/*
2442138568Ssam		 * If station does not support short preamble
2443138568Ssam		 * then we must enable use of Barker preamble.
2444138568Ssam		 */
2445138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2446178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2447172211Ssam			    "%s", "station needs long preamble");
2448138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2449138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2450138568Ssam		}
2451172211Ssam		/*
2452178354Ssam		 * If protection is configured and this is the first
2453178354Ssam		 * indication we should use protection, enable it.
2454172211Ssam		 */
2455172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2456172211Ssam		    ic->ic_nonerpsta == 1 &&
2457172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2458178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2459172211Ssam			    "%s: enable use of protection\n", __func__);
2460172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2461179642Ssam			ieee80211_notify_erp_locked(ic);
2462172211Ssam		}
2463138568Ssam	} else
2464138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2465138568Ssam}
2466138568Ssam
2467138568Ssamvoid
2468178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2469138568Ssam{
2470178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2471178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2472138568Ssam	int newassoc;
2473138568Ssam
2474138568Ssam	if (ni->ni_associd == 0) {
2475170530Ssam		uint16_t aid;
2476138568Ssam
2477178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2478138568Ssam		/*
2479138568Ssam		 * It would be good to search the bitmap
2480138568Ssam		 * more efficiently, but this will do for now.
2481138568Ssam		 */
2482178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2483178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2484138568Ssam				break;
2485138568Ssam		}
2486178354Ssam		if (aid >= vap->iv_max_aid) {
2487179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2488178354Ssam			ieee80211_node_leave(ni);
2489138568Ssam			return;
2490138568Ssam		}
2491138568Ssam		ni->ni_associd = aid | 0xc000;
2492173273Ssam		ni->ni_jointime = time_uptime;
2493178354Ssam		IEEE80211_LOCK(ic);
2494178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2495178354Ssam		vap->iv_sta_assoc++;
2496138568Ssam		ic->ic_sta_assoc++;
2497173273Ssam
2498173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2499173273Ssam			ieee80211_ht_node_join(ni);
2500170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2501170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2502178354Ssam			ieee80211_node_join_11g(ni);
2503173273Ssam		IEEE80211_UNLOCK(ic);
2504173273Ssam
2505173273Ssam		newassoc = 1;
2506138568Ssam	} else
2507138568Ssam		newassoc = 0;
2508138568Ssam
2509178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2510183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2511139523Ssam	    IEEE80211_NODE_AID(ni),
2512139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2513139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2514139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2515170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2516170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2517182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2518173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2519183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2520183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2521183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2522178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2523170530Ssam		", fast-frames" : "",
2524178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2525170530Ssam		", turbo" : ""
2526139523Ssam	);
2527138568Ssam
2528193966Ssam	ieee80211_node_setuptxparms(ni);
2529214894Sbschmidt	ieee80211_ratectl_node_init(ni);
2530138568Ssam	/* give driver a chance to setup state like ni_txrate */
2531139524Ssam	if (ic->ic_newassoc != NULL)
2532148307Ssam		ic->ic_newassoc(ni, newassoc);
2533178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2534138568Ssam	/* tell the authenticator about new station */
2535178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2536178354Ssam		vap->iv_auth->ia_node_join(ni);
2537178354Ssam	ieee80211_notify_node_join(ni,
2538173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2539138568Ssam}
2540138568Ssam
2541172211Ssamstatic void
2542172211Ssamdisable_protection(struct ieee80211com *ic)
2543172211Ssam{
2544172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2545172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2546172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2547172211Ssam	   ic->ic_flags_ext));
2548172211Ssam
2549172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2550172211Ssam	/* XXX verify mode? */
2551172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2552172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2553172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2554172211Ssam	}
2555179642Ssam	ieee80211_notify_erp_locked(ic);
2556172211Ssam}
2557172211Ssam
2558138568Ssam/*
2559138568Ssam * Handle a station leaving an 11g network.
2560138568Ssam */
2561138568Ssamstatic void
2562178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2563138568Ssam{
2564178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2565138568Ssam
2566173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2567173273Ssam
2568170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2569178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2570178354Ssam	      ic->ic_bsschan->ic_flags));
2571138568Ssam
2572138568Ssam	/*
2573138568Ssam	 * If a long slot station do the slot time bookkeeping.
2574138568Ssam	 */
2575138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2576138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2577138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2578138568Ssam		ic->ic_longslotsta--;
2579178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2580172211Ssam		    "long slot time station leaves, count now %d",
2581172211Ssam		    ic->ic_longslotsta);
2582138568Ssam		if (ic->ic_longslotsta == 0) {
2583138568Ssam			/*
2584138568Ssam			 * Re-enable use of short slot time if supported
2585138568Ssam			 * and not operating in IBSS mode (per spec).
2586138568Ssam			 */
2587138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2588138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2589178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2590178354Ssam				    IEEE80211_MSG_ASSOC,
2591138568Ssam				    "%s: re-enable use of short slot time\n",
2592138568Ssam				    __func__);
2593138568Ssam				ieee80211_set_shortslottime(ic, 1);
2594138568Ssam			}
2595138568Ssam		}
2596138568Ssam	}
2597138568Ssam	/*
2598138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2599138568Ssam	 */
2600138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2601138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2602138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2603138568Ssam		ic->ic_nonerpsta--;
2604178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2605172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2606172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2607172211Ssam			" (non-ERP sta present)" : "");
2608172211Ssam		if (ic->ic_nonerpsta == 0 &&
2609172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2610178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2611138568Ssam				"%s: disable use of protection\n", __func__);
2612172211Ssam			disable_protection(ic);
2613138568Ssam		}
2614138568Ssam	}
2615138568Ssam}
2616138568Ssam
2617138568Ssam/*
2618172211Ssam * Time out presence of an overlapping bss with non-ERP
2619172211Ssam * stations.  When operating in hostap mode we listen for
2620172211Ssam * beacons from other stations and if we identify a non-ERP
2621172211Ssam * station is present we enable protection.  To identify
2622172211Ssam * when all non-ERP stations are gone we time out this
2623172211Ssam * condition.
2624172211Ssam */
2625172211Ssamstatic void
2626172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2627172211Ssam{
2628172211Ssam
2629172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2630172211Ssam
2631172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2632172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2633178354Ssam#if 0
2634178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2635178354Ssam		    "%s", "age out non-ERP sta present on channel");
2636178354Ssam#endif
2637172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2638172211Ssam		if (ic->ic_nonerpsta == 0)
2639172211Ssam			disable_protection(ic);
2640172211Ssam	}
2641172211Ssam}
2642172211Ssam
2643172211Ssam/*
2644138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2645138568Ssam * when operating as an ap.
2646138568Ssam */
2647138568Ssamvoid
2648178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2649138568Ssam{
2650178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2651178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2652140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2653138568Ssam
2654178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2655172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2656138568Ssam
2657178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2658178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2659138568Ssam	/*
2660138568Ssam	 * If node wasn't previously associated all
2661138568Ssam	 * we need to do is reclaim the reference.
2662138568Ssam	 */
2663138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2664138568Ssam	if (ni->ni_associd == 0)
2665138568Ssam		goto done;
2666138568Ssam	/*
2667138568Ssam	 * Tell the authenticator the station is leaving.
2668138568Ssam	 * Note that we must do this before yanking the
2669138568Ssam	 * association id as the authenticator uses the
2670138568Ssam	 * associd to locate it's state block.
2671138568Ssam	 */
2672178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2673178354Ssam		vap->iv_auth->ia_node_leave(ni);
2674173273Ssam
2675173273Ssam	IEEE80211_LOCK(ic);
2676178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2677138568Ssam	ni->ni_associd = 0;
2678178354Ssam	vap->iv_sta_assoc--;
2679138568Ssam	ic->ic_sta_assoc--;
2680138568Ssam
2681173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2682173273Ssam		ieee80211_ht_node_leave(ni);
2683170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2684170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2685178354Ssam		ieee80211_node_leave_11g(ni);
2686173273Ssam	IEEE80211_UNLOCK(ic);
2687138568Ssam	/*
2688138568Ssam	 * Cleanup station state.  In particular clear various
2689138568Ssam	 * state that might otherwise be reused if the node
2690138568Ssam	 * is reused before the reference count goes to zero
2691138568Ssam	 * (and memory is reclaimed).
2692138568Ssam	 */
2693178354Ssam	ieee80211_sta_leave(ni);
2694138568Ssamdone:
2695140499Ssam	/*
2696140499Ssam	 * Remove the node from any table it's recorded in and
2697140499Ssam	 * drop the caller's reference.  Removal from the table
2698140499Ssam	 * is important to insure the node is not reprocessed
2699140499Ssam	 * for inactivity.
2700140499Ssam	 */
2701140499Ssam	if (nt != NULL) {
2702140499Ssam		IEEE80211_NODE_LOCK(nt);
2703140499Ssam		node_reclaim(nt, ni);
2704140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2705140499Ssam	} else
2706140499Ssam		ieee80211_free_node(ni);
2707138568Ssam}
2708138568Ssam
2709178354Ssamstruct rssiinfo {
2710178354Ssam	struct ieee80211vap *vap;
2711178354Ssam	int	rssi_samples;
2712178354Ssam	uint32_t rssi_total;
2713178354Ssam};
2714178354Ssam
2715178354Ssamstatic void
2716178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni)
2717178354Ssam{
2718178354Ssam	struct rssiinfo *info = arg;
2719178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2720178354Ssam	int8_t rssi;
2721178354Ssam
2722178354Ssam	if (info->vap != vap)
2723178354Ssam		return;
2724178354Ssam	/* only associated stations */
2725178354Ssam	if (ni->ni_associd == 0)
2726178354Ssam		return;
2727178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2728178354Ssam	if (rssi != 0) {
2729178354Ssam		info->rssi_samples++;
2730178354Ssam		info->rssi_total += rssi;
2731178354Ssam	}
2732178354Ssam}
2733178354Ssam
2734178354Ssamstatic void
2735178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2736178354Ssam{
2737178354Ssam	struct rssiinfo *info = arg;
2738178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2739178354Ssam	int8_t rssi;
2740178354Ssam
2741178354Ssam	if (info->vap != vap)
2742178354Ssam		return;
2743178354Ssam	/* only neighbors */
2744178354Ssam	/* XXX check bssid */
2745178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2746178354Ssam		return;
2747178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2748178354Ssam	if (rssi != 0) {
2749178354Ssam		info->rssi_samples++;
2750178354Ssam		info->rssi_total += rssi;
2751178354Ssam	}
2752178354Ssam}
2753178354Ssam
2754195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
2755195618Srpaulostatic void
2756195618Srpauloget_mesh_rssi(void *arg, struct ieee80211_node *ni)
2757195618Srpaulo{
2758195618Srpaulo	struct rssiinfo *info = arg;
2759195618Srpaulo	struct ieee80211vap *vap = ni->ni_vap;
2760195618Srpaulo	int8_t rssi;
2761195618Srpaulo
2762195618Srpaulo	if (info->vap != vap)
2763195618Srpaulo		return;
2764195618Srpaulo	/* only neighbors that peered successfully */
2765195618Srpaulo	if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2766195618Srpaulo		return;
2767195618Srpaulo	rssi = vap->iv_ic->ic_node_getrssi(ni);
2768195618Srpaulo	if (rssi != 0) {
2769195618Srpaulo		info->rssi_samples++;
2770195618Srpaulo		info->rssi_total += rssi;
2771195618Srpaulo	}
2772195618Srpaulo}
2773195618Srpaulo#endif /* IEEE80211_SUPPORT_MESH */
2774195618Srpaulo
2775170530Ssamint8_t
2776178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2777138568Ssam{
2778138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2779178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2780178354Ssam	struct rssiinfo info;
2781138568Ssam
2782178354Ssam	info.rssi_total = 0;
2783178354Ssam	info.rssi_samples = 0;
2784178354Ssam	info.vap = vap;
2785178354Ssam	switch (vap->iv_opmode) {
2786138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2787138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2788178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2789178354Ssam		break;
2790138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2791178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2792138568Ssam		break;
2793195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
2794195618Srpaulo	case IEEE80211_M_MBSS:		/* average of all mesh neighbors */
2795195618Srpaulo		ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2796195618Srpaulo		break;
2797195618Srpaulo#endif
2798138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2799138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2800138568Ssam	default:
2801178354Ssam		if (vap->iv_bss != NULL)
2802178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2803178354Ssam		info.rssi_samples = 1;
2804138568Ssam		break;
2805138568Ssam	}
2806178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2807138568Ssam#undef NZ
2808138568Ssam}
2809138568Ssam
2810170530Ssamvoid
2811178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2812138568Ssam{
2813138568Ssam
2814178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2815170530Ssam		return;
2816178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2817170530Ssam	/* for non-station mode return avg'd rssi accounting */
2818178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2819178354Ssam		*rssi = ieee80211_getrssi(vap);
2820138568Ssam}
2821