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: stable/10/sys/net80211/ieee80211_node.c 342465 2018-12-25 14:06:52Z avos $");
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);
114314667Savg	callout_init(&ic->ic_inact, 1);
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
1189116742Ssam	return ni;
1190116742Ssam}
1191116742Ssam
1192148777Ssam/*
1193148777Ssam * Craft a temporary node suitable for sending a management frame
1194148777Ssam * to the specified station.  We craft only as much state as we
1195148777Ssam * need to do the work since the node will be immediately reclaimed
1196148777Ssam * once the send completes.
1197148777Ssam */
1198116742Ssamstruct ieee80211_node *
1199178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1200178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1201148777Ssam{
1202178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1203148777Ssam	struct ieee80211_node *ni;
1204148777Ssam
1205179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1206148777Ssam	if (ni != NULL) {
1207183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1208183259Ssam
1209178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1210148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1211148777Ssam
1212178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1213178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1214178354Ssam		ni->ni_vap = vap;
1215178354Ssam
1216148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1217183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1218148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1219148777Ssam		/* NB: required by ieee80211_fix_rate */
1220183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1221178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1222148777Ssam			IEEE80211_KEYIX_NONE);
1223183259Ssam		ni->ni_txpower = bss->ni_txpower;
1224148777Ssam		/* XXX optimize away */
1225184288Ssam		ieee80211_psq_init(&ni->ni_psq, "unknown");
1226217511Sbschmidt
1227217511Sbschmidt		ieee80211_ratectl_node_init(ni);
1228148777Ssam	} else {
1229148777Ssam		/* XXX msg */
1230178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1231148777Ssam	}
1232148777Ssam	return ni;
1233148777Ssam}
1234148777Ssam
1235148777Ssamstruct ieee80211_node *
1236178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1237178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1238116742Ssam{
1239178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1240138568Ssam	struct ieee80211_node *ni;
1241138568Ssam
1242178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1243116742Ssam	if (ni != NULL) {
1244183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1245127770Ssam		/*
1246178354Ssam		 * Inherit from iv_bss.
1247127770Ssam		 */
1248183259Ssam		copy_bss(ni, bss);
1249183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1250183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1251178354Ssam	}
1252116742Ssam	return ni;
1253116742Ssam}
1254116742Ssam
1255178354Ssam/*
1256178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1257178354Ssam * not associate so we just create create a new node and
1258178354Ssam * simulate an association.  The caller is responsible for
1259178354Ssam * installing the node as the bss node and handling any further
1260178354Ssam * setup work like authorizing the port.
1261178354Ssam */
1262178354Ssamstruct ieee80211_node *
1263178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1264178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1265178354Ssam{
1266178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1267178354Ssam	struct ieee80211_node *ni;
1268178354Ssam
1269178354Ssam	/* XXX check if node already in sta table? */
1270178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1271178354Ssam	if (ni != NULL) {
1272178354Ssam		ni->ni_wdsvap = vap;
1273178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1274178354Ssam		/*
1275178354Ssam		 * Inherit any manually configured settings.
1276178354Ssam		 */
1277183259Ssam		copy_bss(ni, vap->iv_bss);
1278178354Ssam		ieee80211_node_set_chan(ni, chan);
1279178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1280178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1281178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1282178354Ssam		/* NB: no associd for peer */
1283178354Ssam		/*
1284178354Ssam		 * There are no management frames to use to
1285178354Ssam		 * discover neighbor capabilities, so blindly
1286178354Ssam		 * propagate the local configuration.
1287178354Ssam		 */
1288178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1289178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1290190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1291178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1292178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1293190391Ssam#endif
1294178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1295193655Ssam		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1296178354Ssam			/*
1297178354Ssam			 * Device is HT-capable and HT is enabled for
1298178354Ssam			 * the vap; setup HT operation.  On return
1299178354Ssam			 * ni_chan will be adjusted to an HT channel.
1300178354Ssam			 */
1301178354Ssam			ieee80211_ht_wds_init(ni);
1302178354Ssam		} else {
1303178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1304178354Ssam			/*
1305178354Ssam			 * Force a legacy channel to be used.
1306178354Ssam			 */
1307178354Ssam			c = ieee80211_find_channel(ic,
1308178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1309178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1310178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1311178354Ssam			ni->ni_chan = c;
1312178354Ssam		}
1313178354Ssam	}
1314178354Ssam	return ni;
1315178354Ssam}
1316178354Ssam
1317178354Ssamstruct ieee80211_node *
1318138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1319178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1320178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1321138568Ssam#else
1322178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1323178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1324138568Ssam#endif
1325116742Ssam{
1326116742Ssam	struct ieee80211_node *ni;
1327116742Ssam	int hash;
1328116742Ssam
1329138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1330127772Ssam
1331195618Srpaulo	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1332138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1333116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1334138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1335138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1336178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1337140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1338140766Ssam			    func, line,
1339140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1340140766Ssam			    ieee80211_node_refcnt(ni));
1341138568Ssam#endif
1342127772Ssam			return ni;
1343116742Ssam		}
1344116742Ssam	}
1345127772Ssam	return NULL;
1346127772Ssam}
1347178354Ssam
1348178354Ssamstruct ieee80211_node *
1349138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1350178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1351178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1352178354Ssam#else
1353178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1354178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1355138568Ssam#endif
1356178354Ssam{
1357178354Ssam	struct ieee80211_node *ni;
1358127772Ssam
1359178354Ssam	IEEE80211_NODE_LOCK(nt);
1360178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1361178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1362178354Ssam	return ni;
1363178354Ssam}
1364178354Ssam
1365127772Ssamstruct ieee80211_node *
1366138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1367178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1368178354Ssam	const struct ieee80211vap *vap,
1369178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1370138568Ssam#else
1371178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1372178354Ssam	const struct ieee80211vap *vap,
1373178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1374138568Ssam#endif
1375127772Ssam{
1376127772Ssam	struct ieee80211_node *ni;
1377178354Ssam	int hash;
1378127772Ssam
1379178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1380178354Ssam
1381195618Srpaulo	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
1382178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1383178354Ssam		if (ni->ni_vap == vap &&
1384178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1385178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1386178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1387178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1388178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1389178354Ssam			    func, line,
1390178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1391178354Ssam			    ieee80211_node_refcnt(ni));
1392178354Ssam#endif
1393178354Ssam			return ni;
1394178354Ssam		}
1395178354Ssam	}
1396178354Ssam	return NULL;
1397178354Ssam}
1398178354Ssam
1399178354Ssamstruct ieee80211_node *
1400178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1401178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1402178354Ssam	const struct ieee80211vap *vap,
1403178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1404178354Ssam#else
1405178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1406178354Ssam	const struct ieee80211vap *vap,
1407178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1408178354Ssam#endif
1409178354Ssam{
1410178354Ssam	struct ieee80211_node *ni;
1411178354Ssam
1412138568Ssam	IEEE80211_NODE_LOCK(nt);
1413178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1414138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1415116742Ssam	return ni;
1416116742Ssam}
1417116742Ssam
1418116742Ssam/*
1419138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1420138568Ssam * Note that for the driver's benefit we we treat this like
1421138568Ssam * an association so the driver has an opportunity to setup
1422138568Ssam * it's private state.
1423138568Ssam */
1424138568Ssamstruct ieee80211_node *
1425178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1426170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1427138568Ssam{
1428138568Ssam	struct ieee80211_node *ni;
1429138568Ssam
1430245928Sadrian	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
1431153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1432178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1433138568Ssam	if (ni != NULL) {
1434178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1435178354Ssam
1436138568Ssam		/* XXX no rate negotiation; just dup */
1437178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1438188869Ssam		if (ieee80211_iserp_rateset(&ni->ni_rates))
1439188869Ssam			ni->ni_flags |= IEEE80211_NODE_ERP;
1440178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1441153404Ssam			/*
1442170530Ssam			 * In adhoc demo mode there are no management
1443170530Ssam			 * frames to use to discover neighbor capabilities,
1444170530Ssam			 * so blindly propagate the local configuration
1445170530Ssam			 * so we can do interesting things (e.g. use
1446170530Ssam			 * WME to disable ACK's).
1447153404Ssam			 */
1448178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1449153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1450190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1451178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1452170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1453190391Ssam#endif
1454153404Ssam		}
1455193966Ssam		ieee80211_node_setuptxparms(ni);
1456214894Sbschmidt		ieee80211_ratectl_node_init(ni);
1457178354Ssam		if (ic->ic_newassoc != NULL)
1458178354Ssam			ic->ic_newassoc(ni, 1);
1459170530Ssam		/* XXX not right for 802.1x/WPA */
1460170530Ssam		ieee80211_node_authorize(ni);
1461138568Ssam	}
1462138568Ssam	return ni;
1463138568Ssam}
1464138568Ssam
1465148936Ssamvoid
1466153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1467153073Ssam	const struct ieee80211_frame *wh,
1468153073Ssam	const struct ieee80211_scanparams *sp)
1469153073Ssam{
1470245928Sadrian	int do_ht_setup = 0;
1471245928Sadrian
1472153073Ssam	ni->ni_esslen = sp->ssid[1];
1473153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1474153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1475153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1476153073Ssam	ni->ni_intval = sp->bintval;
1477153073Ssam	ni->ni_capinfo = sp->capinfo;
1478153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1479153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1480153073Ssam	ni->ni_fhindex = sp->fhindex;
1481153073Ssam	ni->ni_erp = sp->erp;
1482153073Ssam	ni->ni_timoff = sp->timoff;
1483195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
1484195618Srpaulo	if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
1485195618Srpaulo		ieee80211_mesh_init_neighbor(ni, wh, sp);
1486195618Srpaulo#endif
1487178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1488178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1489186659Ssam		if (ni->ni_ies.wme_ie != NULL)
1490186659Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1491186659Ssam		else
1492186659Ssam			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1493190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
1494178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1495178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1496190391Ssam#endif
1497245928Sadrian		if (ni->ni_ies.htcap_ie != NULL)
1498245928Sadrian			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
1499245928Sadrian		if (ni->ni_ies.htinfo_ie != NULL)
1500245928Sadrian			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
1501245928Sadrian
1502245928Sadrian		if ((ni->ni_ies.htcap_ie != NULL) &&
1503245928Sadrian		    (ni->ni_ies.htinfo_ie != NULL) &&
1504245928Sadrian		    (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1505245928Sadrian			do_ht_setup = 1;
1506245928Sadrian		}
1507178354Ssam	}
1508178354Ssam
1509153073Ssam	/* NB: must be after ni_chan is setup */
1510165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1511165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1512165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1513245928Sadrian
1514245928Sadrian	/*
1515245928Sadrian	 * If the neighbor is HT compatible, flip that on.
1516245928Sadrian	 */
1517245928Sadrian	if (do_ht_setup) {
1518245928Sadrian		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
1519245928Sadrian		    "%s: doing HT setup\n", __func__);
1520245928Sadrian		ieee80211_ht_node_init(ni);
1521245928Sadrian		ieee80211_ht_updateparams(ni,
1522245928Sadrian		    ni->ni_ies.htcap_ie,
1523245928Sadrian		    ni->ni_ies.htinfo_ie);
1524245928Sadrian		ieee80211_setup_htrates(ni,
1525245928Sadrian		    ni->ni_ies.htcap_ie,
1526245928Sadrian		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1527245928Sadrian		ieee80211_setup_basic_htrates(ni,
1528245928Sadrian		    ni->ni_ies.htinfo_ie);
1529245928Sadrian		ieee80211_node_setuptxparms(ni);
1530245928Sadrian		ieee80211_ratectl_node_init(ni);
1531245928Sadrian	}
1532153073Ssam}
1533153073Ssam
1534148936Ssam/*
1535148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1536148936Ssam * or probe response frame.  Note that for the driver's
1537148936Ssam * benefit we we treat this like an association so the
1538148936Ssam * driver has an opportunity to setup it's private state.
1539148936Ssam */
1540148936Ssamstruct ieee80211_node *
1541178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1542148936Ssam	const struct ieee80211_frame *wh,
1543148936Ssam	const struct ieee80211_scanparams *sp)
1544148936Ssam{
1545148936Ssam	struct ieee80211_node *ni;
1546148936Ssam
1547245928Sadrian	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
1548153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1549178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1550148936Ssam	if (ni != NULL) {
1551178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1552178354Ssam
1553153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1554188869Ssam		if (ieee80211_iserp_rateset(&ni->ni_rates))
1555188869Ssam			ni->ni_flags |= IEEE80211_NODE_ERP;
1556193966Ssam		ieee80211_node_setuptxparms(ni);
1557214894Sbschmidt		ieee80211_ratectl_node_init(ni);
1558148936Ssam		if (ic->ic_newassoc != NULL)
1559148936Ssam			ic->ic_newassoc(ni, 1);
1560148936Ssam		/* XXX not right for 802.1x/WPA */
1561148936Ssam		ieee80211_node_authorize(ni);
1562148936Ssam	}
1563148936Ssam	return ni;
1564148936Ssam}
1565148936Ssam
1566179220Ssam#define	IS_PROBEREQ(wh) \
1567179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1568179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1569179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1570179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1571179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1572170530Ssam
1573179220Ssamstatic __inline struct ieee80211_node *
1574179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1575179220Ssam    const struct ieee80211_frame_min *wh)
1576179220Ssam{
1577179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1578179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1579179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1580179220Ssam}
1581179220Ssam
1582138568Ssam/*
1583138568Ssam * Locate the node for sender, track state, and then pass the
1584179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1585179220Ssam * we can return NULL if the sender is not in the table.
1586138568Ssam */
1587138568Ssamstruct ieee80211_node *
1588138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1589138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1590138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1591138568Ssam#else
1592138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1593138568Ssam	const struct ieee80211_frame_min *wh)
1594138568Ssam#endif
1595138568Ssam{
1596138568Ssam	struct ieee80211_node_table *nt;
1597138568Ssam	struct ieee80211_node *ni;
1598138568Ssam
1599170530Ssam	nt = &ic->ic_sta;
1600138568Ssam	IEEE80211_NODE_LOCK(nt);
1601179220Ssam	ni = _find_rxnode(nt, wh);
1602138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1603138568Ssam
1604148863Ssam	return ni;
1605148863Ssam}
1606148863Ssam
1607148863Ssam/*
1608148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1609148863Ssam * key index as a hint to locate the node in the key
1610148863Ssam * mapping table.  If an entry is present at the key
1611148863Ssam * index we return it; otherwise do a normal lookup and
1612148863Ssam * update the mapping table if the station has a unicast
1613148863Ssam * key assigned to it.
1614148863Ssam */
1615148863Ssamstruct ieee80211_node *
1616148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1617148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1618148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1619148863Ssam	const char *func, int line)
1620148863Ssam#else
1621148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1622148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1623148863Ssam#endif
1624148863Ssam{
1625148863Ssam	struct ieee80211_node_table *nt;
1626148863Ssam	struct ieee80211_node *ni;
1627148863Ssam
1628170530Ssam	nt = &ic->ic_sta;
1629148863Ssam	IEEE80211_NODE_LOCK(nt);
1630148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1631148863Ssam		ni = nt->nt_keyixmap[keyix];
1632148863Ssam	else
1633148863Ssam		ni = NULL;
1634148863Ssam	if (ni == NULL) {
1635179220Ssam		ni = _find_rxnode(nt, wh);
1636178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1637148863Ssam			/*
1638148863Ssam			 * If the station has a unicast key cache slot
1639148863Ssam			 * assigned update the key->node mapping table.
1640148863Ssam			 */
1641148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1642148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1643148863Ssam			if (keyix < nt->nt_keyixmax &&
1644148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1645178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1646178354Ssam				    IEEE80211_MSG_NODE,
1647148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1648148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1649148863Ssam				    ieee80211_node_refcnt(ni)+1);
1650148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1651148863Ssam			}
1652148863Ssam		}
1653179220Ssam	} else {
1654179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1655179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1656179220Ssam		else
1657179220Ssam			ieee80211_ref_node(ni);
1658179220Ssam	}
1659148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1660148863Ssam
1661148863Ssam	return ni;
1662148863Ssam}
1663179220Ssam#undef IS_BCAST_PROBEREQ
1664179220Ssam#undef IS_PROBEREQ
1665138568Ssam
1666138568Ssam/*
1667127772Ssam * Return a reference to the appropriate node for sending
1668127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1669127772Ssam */
1670127772Ssamstruct ieee80211_node *
1671138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1672178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1673178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1674138568Ssam	const char *func, int line)
1675138568Ssam#else
1676178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1677178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1678138568Ssam#endif
1679127772Ssam{
1680178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1681127772Ssam	struct ieee80211_node *ni;
1682127772Ssam
1683127772Ssam	/*
1684127772Ssam	 * The destination address should be in the node table
1685148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1686148863Ssam	 * also optimize station mode operation, all frames go
1687148863Ssam	 * to the bss node.
1688127772Ssam	 */
1689127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1690138568Ssam	IEEE80211_NODE_LOCK(nt);
1691178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1692178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1693178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1694178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1695186099Ssam	else
1696178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1697138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1698138568Ssam
1699138568Ssam	if (ni == NULL) {
1700178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1701178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1702140497Ssam			/*
1703140497Ssam			 * In adhoc mode cons up a node for the destination.
1704140497Ssam			 * Note that we need an additional reference for the
1705178354Ssam			 * caller to be consistent with
1706178354Ssam			 * ieee80211_find_node_locked.
1707140497Ssam			 */
1708178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1709140497Ssam			if (ni != NULL)
1710140497Ssam				(void) ieee80211_ref_node(ni);
1711140497Ssam		} else {
1712178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1713178354Ssam			    "no node, discard frame (%s)", __func__);
1714178354Ssam			vap->iv_stats.is_tx_nonode++;
1715127772Ssam		}
1716127772Ssam	}
1717127772Ssam	return ni;
1718127772Ssam}
1719127772Ssam
1720116742Ssamstatic void
1721138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1722116742Ssam{
1723138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1724119150Ssam
1725179641Ssam	/*
1726179641Ssam	 * NB: careful about referencing the vap as it may be
1727179641Ssam	 * gone if the last reference was held by a driver.
1728179641Ssam	 * We know the com will always be present so it's safe
1729179641Ssam	 * to use ni_ic below to reclaim resources.
1730179641Ssam	 */
1731179641Ssam#if 0
1732178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1733140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1734140766Ssam		ether_sprintf(ni->ni_macaddr),
1735138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1736179641Ssam#endif
1737179641Ssam	if (ni->ni_associd != 0) {
1738179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1739179641Ssam		if (vap->iv_aid_bitmap != NULL)
1740179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1741179641Ssam	}
1742138568Ssam	if (nt != NULL) {
1743138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1744138568Ssam		LIST_REMOVE(ni, ni_hash);
1745138568Ssam	}
1746179641Ssam	ni->ni_ic->ic_node_free(ni);
1747116742Ssam}
1748116742Ssam
1749116742Ssamvoid
1750138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1751138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1752138568Ssam#else
1753138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1754138568Ssam#endif
1755116742Ssam{
1756138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1757119150Ssam
1758138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1759178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1760140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1761138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1762138568Ssam#endif
1763148863Ssam	if (nt != NULL) {
1764148863Ssam		IEEE80211_NODE_LOCK(nt);
1765148863Ssam		if (ieee80211_node_dectestref(ni)) {
1766148863Ssam			/*
1767148863Ssam			 * Last reference, reclaim state.
1768148863Ssam			 */
1769138568Ssam			_ieee80211_free_node(ni);
1770148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1771148863Ssam		    nt->nt_keyixmap != NULL) {
1772148863Ssam			ieee80211_keyix keyix;
1773148863Ssam			/*
1774148863Ssam			 * Check for a last reference in the key mapping table.
1775148863Ssam			 */
1776148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1777148863Ssam			if (keyix < nt->nt_keyixmax &&
1778148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1779178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1780178354Ssam				    IEEE80211_MSG_NODE,
1781148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1782148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1783148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1784148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1785148863Ssam				_ieee80211_free_node(ni);
1786148863Ssam			}
1787148863Ssam		}
1788148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1789148863Ssam	} else {
1790148863Ssam		if (ieee80211_node_dectestref(ni))
1791138568Ssam			_ieee80211_free_node(ni);
1792116742Ssam	}
1793116742Ssam}
1794116742Ssam
1795138568Ssam/*
1796148863Ssam * Reclaim a unicast key and clear any key cache state.
1797148863Ssam */
1798148863Ssamint
1799148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1800148863Ssam{
1801179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1802179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1803148863Ssam	struct ieee80211_node *nikey;
1804148863Ssam	ieee80211_keyix keyix;
1805148863Ssam	int isowned, status;
1806148863Ssam
1807148863Ssam	/*
1808148863Ssam	 * NB: We must beware of LOR here; deleting the key
1809148863Ssam	 * can cause the crypto layer to block traffic updates
1810148863Ssam	 * which can generate a LOR against the node table lock;
1811148863Ssam	 * grab it here and stash the key index for our use below.
1812148863Ssam	 *
1813148863Ssam	 * Must also beware of recursion on the node table lock.
1814148863Ssam	 * When called from node_cleanup we may already have
1815148863Ssam	 * the node table lock held.  Unfortunately there's no
1816148863Ssam	 * way to separate out this path so we must do this
1817148863Ssam	 * conditionally.
1818148863Ssam	 */
1819148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1820148863Ssam	if (!isowned)
1821148863Ssam		IEEE80211_NODE_LOCK(nt);
1822179641Ssam	nikey = NULL;
1823179641Ssam	status = 1;		/* NB: success */
1824186144Ssam	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
1825179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1826179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1827179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1828179641Ssam			nikey = nt->nt_keyixmap[keyix];
1829201758Smbr			nt->nt_keyixmap[keyix] = NULL;
1830179641Ssam		}
1831179641Ssam	}
1832148863Ssam	if (!isowned)
1833178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1834148863Ssam
1835148863Ssam	if (nikey != NULL) {
1836148863Ssam		KASSERT(nikey == ni,
1837148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1838179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1839148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1840148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1841148863Ssam			ieee80211_node_refcnt(ni)-1);
1842148863Ssam		ieee80211_free_node(ni);
1843148863Ssam	}
1844148863Ssam	return status;
1845148863Ssam}
1846148863Ssam
1847148863Ssam/*
1848138568Ssam * Reclaim a node.  If this is the last reference count then
1849138568Ssam * do the normal free work.  Otherwise remove it from the node
1850138568Ssam * table and mark it gone by clearing the back-reference.
1851138568Ssam */
1852138568Ssamstatic void
1853138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1854116742Ssam{
1855148863Ssam	ieee80211_keyix keyix;
1856138568Ssam
1857148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1858148863Ssam
1859178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1860140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1861140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1862140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1863148863Ssam	/*
1864148863Ssam	 * Clear any entry in the unicast key mapping table.
1865148863Ssam	 * We need to do it here so rx lookups don't find it
1866148863Ssam	 * in the mapping table even if it's not in the hash
1867148863Ssam	 * table.  We cannot depend on the mapping table entry
1868148863Ssam	 * being cleared because the node may not be free'd.
1869148863Ssam	 */
1870148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1871148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1872148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1873178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1874188494Ssam			"%s: %p<%s> clear key map entry %u\n",
1875188494Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
1876148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1877148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1878148863Ssam	}
1879138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1880138568Ssam		/*
1881138568Ssam		 * Other references are present, just remove the
1882138568Ssam		 * node from the table so it cannot be found.  When
1883138568Ssam		 * the references are dropped storage will be
1884140753Ssam		 * reclaimed.
1885138568Ssam		 */
1886138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1887138568Ssam		LIST_REMOVE(ni, ni_hash);
1888138568Ssam		ni->ni_table = NULL;		/* clear reference */
1889138568Ssam	} else
1890138568Ssam		_ieee80211_free_node(ni);
1891138568Ssam}
1892138568Ssam
1893178354Ssam/*
1894178354Ssam * Node table support.
1895178354Ssam */
1896178354Ssam
1897178354Ssamstatic void
1898178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1899178354Ssam	struct ieee80211_node_table *nt,
1900178354Ssam	const char *name, int inact, int keyixmax)
1901178354Ssam{
1902178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1903178354Ssam
1904178354Ssam	nt->nt_ic = ic;
1905178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1906178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1907178354Ssam	TAILQ_INIT(&nt->nt_node);
1908178354Ssam	nt->nt_name = name;
1909178354Ssam	nt->nt_scangen = 1;
1910178354Ssam	nt->nt_inact_init = inact;
1911178354Ssam	nt->nt_keyixmax = keyixmax;
1912178354Ssam	if (nt->nt_keyixmax > 0) {
1913186302Ssam		nt->nt_keyixmap = (struct ieee80211_node **) malloc(
1914184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1915178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1916178354Ssam		if (nt->nt_keyixmap == NULL)
1917178354Ssam			if_printf(ic->ic_ifp,
1918178354Ssam			    "Cannot allocate key index map with %u entries\n",
1919178354Ssam			    keyixmax);
1920178354Ssam	} else
1921178354Ssam		nt->nt_keyixmap = NULL;
1922178354Ssam}
1923178354Ssam
1924178354Ssamstatic void
1925178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1926178354Ssam	struct ieee80211vap *match)
1927178354Ssam{
1928178354Ssam	struct ieee80211_node *ni, *next;
1929178354Ssam
1930178354Ssam	IEEE80211_NODE_LOCK(nt);
1931178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1932178354Ssam		if (match != NULL && ni->ni_vap != match)
1933178354Ssam			continue;
1934178354Ssam		/* XXX can this happen?  if so need's work */
1935138568Ssam		if (ni->ni_associd != 0) {
1936178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1937178354Ssam
1938178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1939178354Ssam				vap->iv_auth->ia_node_leave(ni);
1940178354Ssam			if (vap->iv_aid_bitmap != NULL)
1941178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1942138568Ssam		}
1943178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1944138568Ssam		node_reclaim(nt, ni);
1945138568Ssam	}
1946178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1947178354Ssam		/*
1948178354Ssam		 * Make a separate pass to clear references to this vap
1949178354Ssam		 * held by DWDS entries.  They will not be matched above
1950178354Ssam		 * because ni_vap will point to the ap vap but we still
1951178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1952178354Ssam		 * and/or reset.
1953178354Ssam		 */
1954178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1955178354Ssam			if (ni->ni_wdsvap == match)
1956178354Ssam				ni->ni_wdsvap = NULL;
1957178354Ssam	}
1958178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1959116742Ssam}
1960116742Ssam
1961178354Ssamstatic void
1962178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1963178354Ssam{
1964178354Ssam	ieee80211_node_table_reset(nt, NULL);
1965178354Ssam	if (nt->nt_keyixmap != NULL) {
1966178354Ssam#ifdef DIAGNOSTIC
1967178354Ssam		/* XXX verify all entries are NULL */
1968178354Ssam		int i;
1969178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1970178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1971178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1972178354Ssam					nt->nt_name, i);
1973178354Ssam#endif
1974186302Ssam		free(nt->nt_keyixmap, M_80211_NODE);
1975178354Ssam		nt->nt_keyixmap = NULL;
1976178354Ssam	}
1977178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1978178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1979178354Ssam}
1980178354Ssam
1981120483Ssam/*
1982138568Ssam * Timeout inactive stations and do related housekeeping.
1983138568Ssam * Note that we cannot hold the node lock while sending a
1984138568Ssam * frame as this would lead to a LOR.  Instead we use a
1985138568Ssam * generation number to mark nodes that we've scanned and
1986138568Ssam * drop the lock and restart a scan if we have to time out
1987138568Ssam * a node.  Since we are single-threaded by virtue of
1988120483Ssam * controlling the inactivity timer we can be sure this will
1989120483Ssam * process each node only once.
1990120483Ssam */
1991138568Ssamstatic void
1992178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1993116742Ssam{
1994178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1995178354Ssam	struct ieee80211vap *vap;
1996120483Ssam	struct ieee80211_node *ni;
1997178354Ssam	int gen = 0;
1998116742Ssam
1999178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2000154532Ssam	gen = ++nt->nt_scangen;
2001120483Ssamrestart:
2002138568Ssam	IEEE80211_NODE_LOCK(nt);
2003138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2004120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
2005120483Ssam			continue;
2006120483Ssam		ni->ni_scangen = gen;
2007138568Ssam		/*
2008147788Ssam		 * Ignore entries for which have yet to receive an
2009147788Ssam		 * authentication frame.  These are transient and
2010147788Ssam		 * will be reclaimed when the last reference to them
2011147788Ssam		 * goes away (when frame xmits complete).
2012147788Ssam		 */
2013178354Ssam		vap = ni->ni_vap;
2014178354Ssam		/*
2015178354Ssam		 * Only process stations when in RUN state.  This
2016178354Ssam		 * insures, for example, that we don't timeout an
2017178354Ssam		 * inactive station during CAC.  Note that CSA state
2018178354Ssam		 * is actually handled in ieee80211_node_timeout as
2019178354Ssam		 * it applies to more than timeout processing.
2020178354Ssam		 */
2021178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2022178354Ssam			continue;
2023178354Ssam		/* XXX can vap be NULL? */
2024178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2025178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2026148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2027147788Ssam			continue;
2028147788Ssam		/*
2029138568Ssam		 * Free fragment if not needed anymore
2030138568Ssam		 * (last fragment older than 1s).
2031178354Ssam		 * XXX doesn't belong here, move to node_age
2032138568Ssam		 */
2033138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
2034138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
2035138568Ssam			m_freem(ni->ni_rxfrag[0]);
2036138568Ssam			ni->ni_rxfrag[0] = NULL;
2037138568Ssam		}
2038184277Ssam		if (ni->ni_inact > 0) {
2039172062Ssam			ni->ni_inact--;
2040184277Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
2041184277Ssam			    "%s: inact %u inact_reload %u nrates %u",
2042184277Ssam			    __func__, ni->ni_inact, ni->ni_inact_reload,
2043184277Ssam			    ni->ni_rates.rs_nrates);
2044184277Ssam		}
2045140498Ssam		/*
2046140498Ssam		 * Special case ourself; we may be idle for extended periods
2047140498Ssam		 * of time and regardless reclaiming our state is wrong.
2048178354Ssam		 * XXX run ic_node_age
2049140498Ssam		 */
2050178354Ssam		if (ni == vap->iv_bss)
2051140498Ssam			continue;
2052178354Ssam		if (ni->ni_associd != 0 ||
2053178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
2054178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
2055119150Ssam			/*
2056178354Ssam			 * Age/drain resources held by the station.
2057138568Ssam			 */
2058178354Ssam			ic->ic_node_age(ni);
2059138568Ssam			/*
2060138568Ssam			 * Probe the station before time it out.  We
2061138568Ssam			 * send a null data frame which may not be
2062138568Ssam			 * universally supported by drivers (need it
2063138568Ssam			 * for ps-poll support so it should be...).
2064170530Ssam			 *
2065170530Ssam			 * XXX don't probe the station unless we've
2066170530Ssam			 *     received a frame from them (and have
2067170530Ssam			 *     some idea of the rates they are capable
2068170530Ssam			 *     of); this will get fixed more properly
2069170530Ssam			 *     soon with better handling of the rate set.
2070138568Ssam			 */
2071178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2072172062Ssam			    (0 < ni->ni_inact &&
2073178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
2074170530Ssam			    ni->ni_rates.rs_nrates != 0) {
2075178354Ssam				IEEE80211_NOTE(vap,
2076148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
2077148320Ssam				    ni, "%s",
2078148320Ssam				    "probe station due to inactivity");
2079148582Ssam				/*
2080148582Ssam				 * Grab a reference before unlocking the table
2081148582Ssam				 * so the node cannot be reclaimed before we
2082148582Ssam				 * send the frame. ieee80211_send_nulldata
2083148582Ssam				 * understands we've done this and reclaims the
2084148582Ssam				 * ref for us as needed.
2085148582Ssam				 */
2086148582Ssam				ieee80211_ref_node(ni);
2087138568Ssam				IEEE80211_NODE_UNLOCK(nt);
2088148301Ssam				ieee80211_send_nulldata(ni);
2089138568Ssam				/* XXX stat? */
2090138568Ssam				goto restart;
2091138568Ssam			}
2092138568Ssam		}
2093178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
2094172062Ssam		    ni->ni_inact <= 0) {
2095178354Ssam			IEEE80211_NOTE(vap,
2096148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
2097148320Ssam			    "station timed out due to inactivity "
2098148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
2099138568Ssam			/*
2100138568Ssam			 * Send a deauthenticate frame and drop the station.
2101138568Ssam			 * This is somewhat complicated due to reference counts
2102138568Ssam			 * and locking.  At this point a station will typically
2103138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
2104138568Ssam			 * will do a "free" of the node which will drop the
2105138568Ssam			 * reference count.  But in the meantime a reference
2106138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
2107138568Ssam			 * of the node will happen either after the tx is
2108138568Ssam			 * completed or by ieee80211_node_leave.
2109120483Ssam			 *
2110138568Ssam			 * Separately we must drop the node lock before sending
2111170530Ssam			 * in case the driver takes a lock, as this can result
2112170530Ssam			 * in a LOR between the node lock and the driver lock.
2113119150Ssam			 */
2114172229Ssam			ieee80211_ref_node(ni);
2115138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2116138568Ssam			if (ni->ni_associd != 0) {
2117178354Ssam				IEEE80211_SEND_MGMT(ni,
2118138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
2119138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
2120138568Ssam			}
2121178354Ssam			ieee80211_node_leave(ni);
2122172229Ssam			ieee80211_free_node(ni);
2123178354Ssam			vap->iv_stats.is_node_timeout++;
2124120483Ssam			goto restart;
2125120483Ssam		}
2126116742Ssam	}
2127138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2128138568Ssam
2129178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2130170530Ssam}
2131138568Ssam
2132178354Ssam/*
2133178354Ssam * Aggressively reclaim resources.  This should be used
2134178354Ssam * only in a critical situation to reclaim mbuf resources.
2135178354Ssam */
2136170530Ssamvoid
2137178354Ssamieee80211_drain(struct ieee80211com *ic)
2138178354Ssam{
2139178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2140178354Ssam	struct ieee80211vap *vap;
2141178354Ssam	struct ieee80211_node *ni;
2142178354Ssam
2143178354Ssam	IEEE80211_NODE_LOCK(nt);
2144178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2145178354Ssam		/*
2146178354Ssam		 * Ignore entries for which have yet to receive an
2147178354Ssam		 * authentication frame.  These are transient and
2148178354Ssam		 * will be reclaimed when the last reference to them
2149178354Ssam		 * goes away (when frame xmits complete).
2150178354Ssam		 */
2151178354Ssam		vap = ni->ni_vap;
2152178354Ssam		/*
2153178354Ssam		 * Only process stations when in RUN state.  This
2154178354Ssam		 * insures, for example, that we don't timeout an
2155178354Ssam		 * inactive station during CAC.  Note that CSA state
2156178354Ssam		 * is actually handled in ieee80211_node_timeout as
2157178354Ssam		 * it applies to more than timeout processing.
2158178354Ssam		 */
2159178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2160178354Ssam			continue;
2161178354Ssam		/* XXX can vap be NULL? */
2162178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2163178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2164178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2165178354Ssam			continue;
2166178354Ssam		/*
2167178354Ssam		 * Free fragments.
2168178354Ssam		 * XXX doesn't belong here, move to node_drain
2169178354Ssam		 */
2170178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2171178354Ssam			m_freem(ni->ni_rxfrag[0]);
2172178354Ssam			ni->ni_rxfrag[0] = NULL;
2173178354Ssam		}
2174178354Ssam		/*
2175178354Ssam		 * Drain resources held by the station.
2176178354Ssam		 */
2177178354Ssam		ic->ic_node_drain(ni);
2178178354Ssam	}
2179178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2180178354Ssam}
2181178354Ssam
2182178354Ssam/*
2183178354Ssam * Per-ieee80211com inactivity timer callback.
2184178354Ssam */
2185178354Ssamvoid
2186170530Ssamieee80211_node_timeout(void *arg)
2187170530Ssam{
2188170530Ssam	struct ieee80211com *ic = arg;
2189170530Ssam
2190178354Ssam	/*
2191178354Ssam	 * Defer timeout processing if a channel switch is pending.
2192178354Ssam	 * We typically need to be mute so not doing things that
2193178354Ssam	 * might generate frames is good to handle in one place.
2194178354Ssam	 * Supressing the station timeout processing may extend the
2195178354Ssam	 * lifetime of inactive stations (by not decrementing their
2196178354Ssam	 * idle counters) but this should be ok unless the CSA is
2197178354Ssam	 * active for an unusually long time.
2198178354Ssam	 */
2199178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2200178354Ssam		ieee80211_scan_timeout(ic);
2201178354Ssam		ieee80211_timeout_stations(ic);
2202195379Ssam		ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
2203170530Ssam
2204178354Ssam		IEEE80211_LOCK(ic);
2205178354Ssam		ieee80211_erp_timeout(ic);
2206178354Ssam		ieee80211_ht_timeout(ic);
2207178354Ssam		IEEE80211_UNLOCK(ic);
2208178354Ssam	}
2209170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2210170530Ssam		ieee80211_node_timeout, ic);
2211116742Ssam}
2212116742Ssam
2213239312Sadrian/*
2214239312Sadrian * Iterate over the node table and return an array of ref'ed nodes.
2215239312Sadrian *
2216239312Sadrian * This is separated out from calling the actual node function so that
2217239312Sadrian * no LORs will occur.
2218239312Sadrian *
2219239312Sadrian * If there are too many nodes (ie, the number of nodes doesn't fit
2220239312Sadrian * within 'max_aid' entries) then the node references will be freed
2221239312Sadrian * and an error will be returned.
2222239312Sadrian *
2223239312Sadrian * The responsibility of allocating and freeing "ni_arr" is up to
2224239312Sadrian * the caller.
2225239312Sadrian */
2226239312Sadrianint
2227239312Sadrianieee80211_iterate_nt(struct ieee80211_node_table *nt,
2228239312Sadrian    struct ieee80211_node **ni_arr, uint16_t max_aid)
2229116742Ssam{
2230239312Sadrian	u_int gen;
2231239312Sadrian	int i, j, ret;
2232116742Ssam	struct ieee80211_node *ni;
2233116742Ssam
2234178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2235239312Sadrian	IEEE80211_NODE_LOCK(nt);
2236239312Sadrian
2237154532Ssam	gen = ++nt->nt_scangen;
2238239312Sadrian	i = ret = 0;
2239239312Sadrian
2240239312Sadrian	/*
2241239312Sadrian	 * We simply assume here that since the node
2242239312Sadrian	 * scan generation doesn't change (as
2243239312Sadrian	 * we are holding both the node table and
2244239312Sadrian	 * node table iteration locks), we can simply
2245239312Sadrian	 * assign it to the node here.
2246239312Sadrian	 */
2247138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2248239312Sadrian		if (i >= max_aid) {
2249239312Sadrian			ret = E2BIG;
2250239312Sadrian			if_printf(nt->nt_ic->ic_ifp,
2251239312Sadrian			    "Node array overflow: max=%u", max_aid);
2252239312Sadrian			break;
2253138568Ssam		}
2254239312Sadrian		ni_arr[i] = ieee80211_ref_node(ni);
2255239312Sadrian		ni_arr[i]->ni_scangen = gen;
2256239312Sadrian		i++;
2257138568Ssam	}
2258239312Sadrian
2259239312Sadrian	/*
2260239312Sadrian	 * It's safe to unlock here.
2261239312Sadrian	 *
2262239312Sadrian	 * If we're successful, the list is returned.
2263239312Sadrian	 * If we're unsuccessful, the list is ignored
2264239312Sadrian	 * and we remove our references.
2265239312Sadrian	 *
2266239312Sadrian	 * This avoids any potential LOR with
2267239312Sadrian	 * ieee80211_free_node().
2268239312Sadrian	 */
2269138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2270239312Sadrian	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2271138568Ssam
2272239312Sadrian	/*
2273239312Sadrian	 * If ret is non-zero, we hit some kind of error.
2274239312Sadrian	 * Rather than walking some nodes, we'll walk none
2275239312Sadrian	 * of them.
2276239312Sadrian	 */
2277239312Sadrian	if (ret) {
2278239312Sadrian		for (j = 0; j < i; j++) {
2279239312Sadrian			/* ieee80211_free_node() locks by itself */
2280239312Sadrian			ieee80211_free_node(ni_arr[j]);
2281239312Sadrian		}
2282239312Sadrian	}
2283239312Sadrian
2284239312Sadrian	return (ret);
2285116742Ssam}
2286138568Ssam
2287239312Sadrian/*
2288239312Sadrian * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
2289239312Sadrian * reference in the source.
2290239312Sadrian *
2291239312Sadrian * Note that this fetches 'max_aid' from the first VAP, rather than finding
2292239312Sadrian * the largest max_aid from all VAPs.
2293239312Sadrian */
2294138568Ssamvoid
2295239312Sadrianieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2296239312Sadrian	ieee80211_iter_func *f, void *arg)
2297239312Sadrian{
2298239312Sadrian	struct ieee80211_node **ni_arr;
2299239319Sadrian	size_t size;
2300239312Sadrian	int i;
2301239312Sadrian	uint16_t max_aid;
2302240574Sadrian	struct ieee80211vap *vap;
2303239312Sadrian
2304240574Sadrian	/* Overdoing it default */
2305240574Sadrian	max_aid = IEEE80211_AID_MAX;
2306240574Sadrian
2307240574Sadrian	/* Handle the case of there being no vaps just yet */
2308240574Sadrian	vap = TAILQ_FIRST(&nt->nt_ic->ic_vaps);
2309240574Sadrian	if (vap != NULL)
2310240574Sadrian		max_aid = vap->iv_max_aid;
2311240574Sadrian
2312239312Sadrian	size = max_aid * sizeof(struct ieee80211_node *);
2313239312Sadrian	ni_arr = (struct ieee80211_node **) malloc(size, M_80211_NODE,
2314239312Sadrian	    M_NOWAIT | M_ZERO);
2315239312Sadrian	if (ni_arr == NULL)
2316239312Sadrian		return;
2317239312Sadrian
2318239312Sadrian	/*
2319239312Sadrian	 * If this fails, the node table won't have any
2320239312Sadrian	 * valid entries - ieee80211_iterate_nt() frees
2321239312Sadrian	 * the references to them.  So don't try walking
2322239312Sadrian	 * the table; just skip to the end and free the
2323239312Sadrian	 * temporary memory.
2324239312Sadrian	 */
2325239319Sadrian	if (ieee80211_iterate_nt(nt, ni_arr, max_aid) != 0)
2326239312Sadrian		goto done;
2327239312Sadrian
2328239312Sadrian	for (i = 0; i < max_aid; i++) {
2329239312Sadrian		if (ni_arr[i] == NULL)	/* end of the list */
2330239312Sadrian			break;
2331239312Sadrian		(*f)(arg, ni_arr[i]);
2332239312Sadrian		/* ieee80211_free_node() locks by itself */
2333239312Sadrian		ieee80211_free_node(ni_arr[i]);
2334239312Sadrian	}
2335239312Sadrian
2336239312Sadriandone:
2337239312Sadrian	free(ni_arr, M_80211_NODE);
2338239312Sadrian}
2339239312Sadrian
2340239312Sadrianvoid
2341138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2342138568Ssam{
2343138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2344138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2345138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2346138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2347138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2348138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2349138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2350167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2351167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2352167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2353138568Ssam		ni->ni_rxfragstamp);
2354192468Ssam	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
2355192468Ssam		node_getrssi(ni), ni->ni_noise,
2356170530Ssam		ni->ni_intval, ni->ni_capinfo);
2357138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2358138568Ssam		ether_sprintf(ni->ni_bssid),
2359138568Ssam		ni->ni_esslen, ni->ni_essid,
2360138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2361184277Ssam	printf("\tinact %u inact_reload %u txrate %u\n",
2362184277Ssam		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
2363170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2364170530Ssam		ni->ni_htcap, ni->ni_htparam,
2365170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2366170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2367170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2368138568Ssam}
2369138568Ssam
2370138568Ssamvoid
2371138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2372138568Ssam{
2373138568Ssam	ieee80211_iterate_nodes(nt,
2374138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2375138568Ssam}
2376138568Ssam
2377179642Ssamstatic void
2378179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2379172211Ssam{
2380178354Ssam	struct ieee80211vap *vap;
2381178354Ssam
2382178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2383178354Ssam
2384178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2385178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2386178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2387172211Ssam}
2388172211Ssam
2389179642Ssamvoid
2390179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2391179642Ssam{
2392179642Ssam	IEEE80211_LOCK(ic);
2393179642Ssam	ieee80211_notify_erp_locked(ic);
2394179642Ssam	IEEE80211_UNLOCK(ic);
2395179642Ssam}
2396179642Ssam
2397138568Ssam/*
2398138568Ssam * Handle a station joining an 11g network.
2399138568Ssam */
2400138568Ssamstatic void
2401178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2402138568Ssam{
2403178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2404138568Ssam
2405173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2406173273Ssam
2407138568Ssam	/*
2408138568Ssam	 * Station isn't capable of short slot time.  Bump
2409138568Ssam	 * the count of long slot time stations and disable
2410138568Ssam	 * use of short slot time.  Note that the actual switch
2411138568Ssam	 * over to long slot time use may not occur until the
2412138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2413138568Ssam	 */
2414138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2415138568Ssam		ic->ic_longslotsta++;
2416178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2417172211Ssam		    "station needs long slot time, count %d",
2418172211Ssam		    ic->ic_longslotsta);
2419138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2420170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2421170530Ssam			/*
2422170530Ssam			 * Don't force slot time when switched to turbo
2423170530Ssam			 * mode as non-ERP stations won't be present; this
2424170530Ssam			 * need only be done when on the normal G channel.
2425170530Ssam			 */
2426170530Ssam			ieee80211_set_shortslottime(ic, 0);
2427170530Ssam		}
2428138568Ssam	}
2429138568Ssam	/*
2430138568Ssam	 * If the new station is not an ERP station
2431138568Ssam	 * then bump the counter and enable protection
2432138568Ssam	 * if configured.
2433138568Ssam	 */
2434178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2435138568Ssam		ic->ic_nonerpsta++;
2436178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2437172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2438172211Ssam		    ic->ic_nonerpsta);
2439138568Ssam		/*
2440138568Ssam		 * If station does not support short preamble
2441138568Ssam		 * then we must enable use of Barker preamble.
2442138568Ssam		 */
2443138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2444178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2445172211Ssam			    "%s", "station needs long preamble");
2446138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2447138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2448138568Ssam		}
2449172211Ssam		/*
2450178354Ssam		 * If protection is configured and this is the first
2451178354Ssam		 * indication we should use protection, enable it.
2452172211Ssam		 */
2453172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2454172211Ssam		    ic->ic_nonerpsta == 1 &&
2455172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2456178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2457172211Ssam			    "%s: enable use of protection\n", __func__);
2458172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2459179642Ssam			ieee80211_notify_erp_locked(ic);
2460172211Ssam		}
2461138568Ssam	} else
2462138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2463138568Ssam}
2464138568Ssam
2465138568Ssamvoid
2466178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2467138568Ssam{
2468178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2469178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2470138568Ssam	int newassoc;
2471138568Ssam
2472138568Ssam	if (ni->ni_associd == 0) {
2473170530Ssam		uint16_t aid;
2474138568Ssam
2475178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2476138568Ssam		/*
2477138568Ssam		 * It would be good to search the bitmap
2478138568Ssam		 * more efficiently, but this will do for now.
2479138568Ssam		 */
2480178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2481178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2482138568Ssam				break;
2483138568Ssam		}
2484178354Ssam		if (aid >= vap->iv_max_aid) {
2485179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2486178354Ssam			ieee80211_node_leave(ni);
2487138568Ssam			return;
2488138568Ssam		}
2489138568Ssam		ni->ni_associd = aid | 0xc000;
2490173273Ssam		ni->ni_jointime = time_uptime;
2491178354Ssam		IEEE80211_LOCK(ic);
2492178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2493178354Ssam		vap->iv_sta_assoc++;
2494138568Ssam		ic->ic_sta_assoc++;
2495173273Ssam
2496173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2497173273Ssam			ieee80211_ht_node_join(ni);
2498170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2499170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2500178354Ssam			ieee80211_node_join_11g(ni);
2501173273Ssam		IEEE80211_UNLOCK(ic);
2502173273Ssam
2503173273Ssam		newassoc = 1;
2504138568Ssam	} else
2505138568Ssam		newassoc = 0;
2506138568Ssam
2507178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2508183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2509139523Ssam	    IEEE80211_NODE_AID(ni),
2510139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2511139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2512139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2513170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2514170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2515182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2516173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2517183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2518183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2519183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2520178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2521170530Ssam		", fast-frames" : "",
2522178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2523170530Ssam		", turbo" : ""
2524139523Ssam	);
2525138568Ssam
2526193966Ssam	ieee80211_node_setuptxparms(ni);
2527214894Sbschmidt	ieee80211_ratectl_node_init(ni);
2528138568Ssam	/* give driver a chance to setup state like ni_txrate */
2529139524Ssam	if (ic->ic_newassoc != NULL)
2530148307Ssam		ic->ic_newassoc(ni, newassoc);
2531178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2532138568Ssam	/* tell the authenticator about new station */
2533178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2534178354Ssam		vap->iv_auth->ia_node_join(ni);
2535178354Ssam	ieee80211_notify_node_join(ni,
2536173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2537138568Ssam}
2538138568Ssam
2539172211Ssamstatic void
2540172211Ssamdisable_protection(struct ieee80211com *ic)
2541172211Ssam{
2542172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2543172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2544172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2545172211Ssam	   ic->ic_flags_ext));
2546172211Ssam
2547172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2548172211Ssam	/* XXX verify mode? */
2549172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2550172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2551172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2552172211Ssam	}
2553179642Ssam	ieee80211_notify_erp_locked(ic);
2554172211Ssam}
2555172211Ssam
2556138568Ssam/*
2557138568Ssam * Handle a station leaving an 11g network.
2558138568Ssam */
2559138568Ssamstatic void
2560178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2561138568Ssam{
2562178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2563138568Ssam
2564173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2565173273Ssam
2566170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2567178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2568178354Ssam	      ic->ic_bsschan->ic_flags));
2569138568Ssam
2570138568Ssam	/*
2571138568Ssam	 * If a long slot station do the slot time bookkeeping.
2572138568Ssam	 */
2573138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2574138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2575138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2576138568Ssam		ic->ic_longslotsta--;
2577178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2578172211Ssam		    "long slot time station leaves, count now %d",
2579172211Ssam		    ic->ic_longslotsta);
2580138568Ssam		if (ic->ic_longslotsta == 0) {
2581138568Ssam			/*
2582138568Ssam			 * Re-enable use of short slot time if supported
2583138568Ssam			 * and not operating in IBSS mode (per spec).
2584138568Ssam			 */
2585138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2586138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2587178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2588178354Ssam				    IEEE80211_MSG_ASSOC,
2589138568Ssam				    "%s: re-enable use of short slot time\n",
2590138568Ssam				    __func__);
2591138568Ssam				ieee80211_set_shortslottime(ic, 1);
2592138568Ssam			}
2593138568Ssam		}
2594138568Ssam	}
2595138568Ssam	/*
2596138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2597138568Ssam	 */
2598138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2599138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2600138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2601138568Ssam		ic->ic_nonerpsta--;
2602178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2603172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2604172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2605172211Ssam			" (non-ERP sta present)" : "");
2606172211Ssam		if (ic->ic_nonerpsta == 0 &&
2607172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2608178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2609138568Ssam				"%s: disable use of protection\n", __func__);
2610172211Ssam			disable_protection(ic);
2611138568Ssam		}
2612138568Ssam	}
2613138568Ssam}
2614138568Ssam
2615138568Ssam/*
2616172211Ssam * Time out presence of an overlapping bss with non-ERP
2617172211Ssam * stations.  When operating in hostap mode we listen for
2618172211Ssam * beacons from other stations and if we identify a non-ERP
2619172211Ssam * station is present we enable protection.  To identify
2620172211Ssam * when all non-ERP stations are gone we time out this
2621172211Ssam * condition.
2622172211Ssam */
2623172211Ssamstatic void
2624172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2625172211Ssam{
2626172211Ssam
2627172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2628172211Ssam
2629172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2630172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2631178354Ssam#if 0
2632178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2633178354Ssam		    "%s", "age out non-ERP sta present on channel");
2634178354Ssam#endif
2635172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2636172211Ssam		if (ic->ic_nonerpsta == 0)
2637172211Ssam			disable_protection(ic);
2638172211Ssam	}
2639172211Ssam}
2640172211Ssam
2641172211Ssam/*
2642138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2643138568Ssam * when operating as an ap.
2644138568Ssam */
2645138568Ssamvoid
2646178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2647138568Ssam{
2648178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2649178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2650140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2651138568Ssam
2652178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2653172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2654138568Ssam
2655178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2656178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2657138568Ssam	/*
2658138568Ssam	 * If node wasn't previously associated all
2659138568Ssam	 * we need to do is reclaim the reference.
2660138568Ssam	 */
2661138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2662138568Ssam	if (ni->ni_associd == 0)
2663138568Ssam		goto done;
2664138568Ssam	/*
2665138568Ssam	 * Tell the authenticator the station is leaving.
2666138568Ssam	 * Note that we must do this before yanking the
2667138568Ssam	 * association id as the authenticator uses the
2668138568Ssam	 * associd to locate it's state block.
2669138568Ssam	 */
2670178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2671178354Ssam		vap->iv_auth->ia_node_leave(ni);
2672173273Ssam
2673173273Ssam	IEEE80211_LOCK(ic);
2674178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2675138568Ssam	ni->ni_associd = 0;
2676178354Ssam	vap->iv_sta_assoc--;
2677138568Ssam	ic->ic_sta_assoc--;
2678138568Ssam
2679173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2680173273Ssam		ieee80211_ht_node_leave(ni);
2681170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2682170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2683178354Ssam		ieee80211_node_leave_11g(ni);
2684173273Ssam	IEEE80211_UNLOCK(ic);
2685138568Ssam	/*
2686138568Ssam	 * Cleanup station state.  In particular clear various
2687138568Ssam	 * state that might otherwise be reused if the node
2688138568Ssam	 * is reused before the reference count goes to zero
2689138568Ssam	 * (and memory is reclaimed).
2690138568Ssam	 */
2691178354Ssam	ieee80211_sta_leave(ni);
2692138568Ssamdone:
2693140499Ssam	/*
2694140499Ssam	 * Remove the node from any table it's recorded in and
2695140499Ssam	 * drop the caller's reference.  Removal from the table
2696140499Ssam	 * is important to insure the node is not reprocessed
2697140499Ssam	 * for inactivity.
2698140499Ssam	 */
2699140499Ssam	if (nt != NULL) {
2700140499Ssam		IEEE80211_NODE_LOCK(nt);
2701140499Ssam		node_reclaim(nt, ni);
2702140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2703140499Ssam	} else
2704140499Ssam		ieee80211_free_node(ni);
2705138568Ssam}
2706138568Ssam
2707178354Ssamstruct rssiinfo {
2708178354Ssam	struct ieee80211vap *vap;
2709178354Ssam	int	rssi_samples;
2710178354Ssam	uint32_t rssi_total;
2711178354Ssam};
2712178354Ssam
2713178354Ssamstatic void
2714178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni)
2715178354Ssam{
2716178354Ssam	struct rssiinfo *info = arg;
2717178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2718178354Ssam	int8_t rssi;
2719178354Ssam
2720178354Ssam	if (info->vap != vap)
2721178354Ssam		return;
2722178354Ssam	/* only associated stations */
2723178354Ssam	if (ni->ni_associd == 0)
2724178354Ssam		return;
2725178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2726178354Ssam	if (rssi != 0) {
2727178354Ssam		info->rssi_samples++;
2728178354Ssam		info->rssi_total += rssi;
2729178354Ssam	}
2730178354Ssam}
2731178354Ssam
2732178354Ssamstatic void
2733178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2734178354Ssam{
2735178354Ssam	struct rssiinfo *info = arg;
2736178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2737178354Ssam	int8_t rssi;
2738178354Ssam
2739178354Ssam	if (info->vap != vap)
2740178354Ssam		return;
2741178354Ssam	/* only neighbors */
2742178354Ssam	/* XXX check bssid */
2743178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2744178354Ssam		return;
2745178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2746178354Ssam	if (rssi != 0) {
2747178354Ssam		info->rssi_samples++;
2748178354Ssam		info->rssi_total += rssi;
2749178354Ssam	}
2750178354Ssam}
2751178354Ssam
2752195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
2753195618Srpaulostatic void
2754195618Srpauloget_mesh_rssi(void *arg, struct ieee80211_node *ni)
2755195618Srpaulo{
2756195618Srpaulo	struct rssiinfo *info = arg;
2757195618Srpaulo	struct ieee80211vap *vap = ni->ni_vap;
2758195618Srpaulo	int8_t rssi;
2759195618Srpaulo
2760195618Srpaulo	if (info->vap != vap)
2761195618Srpaulo		return;
2762195618Srpaulo	/* only neighbors that peered successfully */
2763195618Srpaulo	if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
2764195618Srpaulo		return;
2765195618Srpaulo	rssi = vap->iv_ic->ic_node_getrssi(ni);
2766195618Srpaulo	if (rssi != 0) {
2767195618Srpaulo		info->rssi_samples++;
2768195618Srpaulo		info->rssi_total += rssi;
2769195618Srpaulo	}
2770195618Srpaulo}
2771195618Srpaulo#endif /* IEEE80211_SUPPORT_MESH */
2772195618Srpaulo
2773170530Ssamint8_t
2774178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2775138568Ssam{
2776138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2777178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2778178354Ssam	struct rssiinfo info;
2779138568Ssam
2780178354Ssam	info.rssi_total = 0;
2781178354Ssam	info.rssi_samples = 0;
2782178354Ssam	info.vap = vap;
2783178354Ssam	switch (vap->iv_opmode) {
2784138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2785138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2786178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2787178354Ssam		break;
2788138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2789178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2790138568Ssam		break;
2791195618Srpaulo#ifdef IEEE80211_SUPPORT_MESH
2792195618Srpaulo	case IEEE80211_M_MBSS:		/* average of all mesh neighbors */
2793195618Srpaulo		ieee80211_iterate_nodes(&ic->ic_sta, get_mesh_rssi, &info);
2794195618Srpaulo		break;
2795195618Srpaulo#endif
2796138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2797138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2798138568Ssam	default:
2799178354Ssam		if (vap->iv_bss != NULL)
2800178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2801178354Ssam		info.rssi_samples = 1;
2802138568Ssam		break;
2803138568Ssam	}
2804178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2805138568Ssam#undef NZ
2806138568Ssam}
2807138568Ssam
2808170530Ssamvoid
2809178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2810138568Ssam{
2811138568Ssam
2812178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2813170530Ssam		return;
2814178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2815170530Ssam	/* for non-station mode return avg'd rssi accounting */
2816178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2817178354Ssam		*rssi = ieee80211_getrssi(vap);
2818138568Ssam}
2819