ieee80211_node.c revision 184210
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 184210 2008-10-23 19:57:13Z des $");
29116742Ssam
30178354Ssam#include "opt_wlan.h"
31178354Ssam
32116742Ssam#include <sys/param.h>
33116742Ssam#include <sys/systm.h>
34116742Ssam#include <sys/mbuf.h>
35116742Ssam#include <sys/malloc.h>
36116742Ssam#include <sys/kernel.h>
37138568Ssam
38116742Ssam#include <sys/socket.h>
39116742Ssam
40116742Ssam#include <net/if.h>
41116742Ssam#include <net/if_media.h>
42116742Ssam#include <net/ethernet.h>
43116742Ssam
44116742Ssam#include <net80211/ieee80211_var.h>
45178354Ssam#include <net80211/ieee80211_input.h>
46178354Ssam#include <net80211/ieee80211_wds.h>
47116742Ssam
48116742Ssam#include <net/bpf.h>
49116742Ssam
50147221Ssam/*
51147221Ssam * Association id's are managed with a bit vector.
52147221Ssam */
53178354Ssam#define	IEEE80211_AID_SET(_vap, b) \
54178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
55178354Ssam		(1 << (IEEE80211_AID(b) % 32)))
56178354Ssam#define	IEEE80211_AID_CLR(_vap, b) \
57178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
58178354Ssam		~(1 << (IEEE80211_AID(b) % 32)))
59178354Ssam#define	IEEE80211_AID_ISSET(_vap, b) \
60178354Ssam	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
61147221Ssam
62159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
63159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
64159139Sdds#else
65159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
66159139Sdds#endif
67159139Sdds
68170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
69170530Ssam
70179643Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211vap *,
71179643Ssam	const uint8_t [IEEE80211_ADDR_LEN]);
72138568Ssamstatic void node_cleanup(struct ieee80211_node *);
73138568Ssamstatic void node_free(struct ieee80211_node *);
74178354Ssamstatic void node_age(struct ieee80211_node *);
75170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
76170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
77178354Ssamstatic void node_getmimoinfo(const struct ieee80211_node *,
78178354Ssam	struct ieee80211_mimo_info *);
79116742Ssam
80138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
81120104Ssam
82138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
83148863Ssam	struct ieee80211_node_table *nt, const char *name,
84170530Ssam	int inact, int keymaxix);
85178354Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *,
86178354Ssam	struct ieee80211vap *);
87178354Ssamstatic void ieee80211_node_reclaim(struct ieee80211_node *);
88138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
89172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
90138568Ssam
91127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
92178354SsamMALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
93120481Ssam
94116742Ssamvoid
95138568Ssamieee80211_node_attach(struct ieee80211com *ic)
96116742Ssam{
97178354Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
98178354Ssam		IEEE80211_INACT_INIT, ic->ic_max_keyix);
99178354Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
100178354Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
101178354Ssam		ieee80211_node_timeout, ic);
102116742Ssam
103138568Ssam	ic->ic_node_alloc = node_alloc;
104138568Ssam	ic->ic_node_free = node_free;
105138568Ssam	ic->ic_node_cleanup = node_cleanup;
106178354Ssam	ic->ic_node_age = node_age;
107178354Ssam	ic->ic_node_drain = node_age;		/* NB: same as age */
108138568Ssam	ic->ic_node_getrssi = node_getrssi;
109170530Ssam	ic->ic_node_getsignal = node_getsignal;
110178354Ssam	ic->ic_node_getmimoinfo = node_getmimoinfo;
111138568Ssam
112178354Ssam	/*
113178354Ssam	 * Set flags to be propagated to all vap's;
114178354Ssam	 * these define default behaviour/configuration.
115178354Ssam	 */
116178354Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
117178354Ssam}
118138568Ssam
119178354Ssamvoid
120178354Ssamieee80211_node_detach(struct ieee80211com *ic)
121178354Ssam{
122170530Ssam
123178354Ssam	callout_drain(&ic->ic_inact);
124178354Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
125178354Ssam}
126172062Ssam
127178354Ssamvoid
128178354Ssamieee80211_node_vattach(struct ieee80211vap *vap)
129178354Ssam{
130178354Ssam	/* NB: driver can override */
131178354Ssam	vap->iv_max_aid = IEEE80211_AID_DEF;
132178354Ssam
133178354Ssam	/* default station inactivity timer setings */
134178354Ssam	vap->iv_inact_init = IEEE80211_INACT_INIT;
135178354Ssam	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
136178354Ssam	vap->iv_inact_run = IEEE80211_INACT_RUN;
137178354Ssam	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
138148863Ssam}
139148863Ssam
140148863Ssamvoid
141178354Ssamieee80211_node_latevattach(struct ieee80211vap *vap)
142148863Ssam{
143178354Ssam	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
144178354Ssam		/* XXX should we allow max aid to be zero? */
145178354Ssam		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
146178354Ssam			vap->iv_max_aid = IEEE80211_AID_MIN;
147178354Ssam			if_printf(vap->iv_ifp,
148178354Ssam			    "WARNING: max aid too small, changed to %d\n",
149178354Ssam			    vap->iv_max_aid);
150178354Ssam		}
151184210Sdes		MALLOC(vap->iv_aid_bitmap, uint32_t *,
152184210Sdes			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
153178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
154178354Ssam		if (vap->iv_aid_bitmap == NULL) {
155178354Ssam			/* XXX no way to recover */
156178354Ssam			printf("%s: no memory for AID bitmap, max aid %d!\n",
157178354Ssam			    __func__, vap->iv_max_aid);
158178354Ssam			vap->iv_max_aid = 0;
159178354Ssam		}
160138568Ssam	}
161138568Ssam
162178354Ssam	ieee80211_reset_bss(vap);
163118887Ssam
164178354Ssam	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
165116742Ssam}
166116742Ssam
167116742Ssamvoid
168178354Ssamieee80211_node_vdetach(struct ieee80211vap *vap)
169116742Ssam{
170178354Ssam	struct ieee80211com *ic = vap->iv_ic;
171116742Ssam
172178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
173178354Ssam	if (vap->iv_bss != NULL) {
174178354Ssam		ieee80211_free_node(vap->iv_bss);
175178354Ssam		vap->iv_bss = NULL;
176138568Ssam	}
177178354Ssam	if (vap->iv_aid_bitmap != NULL) {
178184210Sdes		FREE(vap->iv_aid_bitmap, M_80211_NODE);
179178354Ssam		vap->iv_aid_bitmap = NULL;
180138568Ssam	}
181116742Ssam}
182116742Ssam
183138568Ssam/*
184138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
185138568Ssam */
186138568Ssam
187138568Ssamvoid
188148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
189138568Ssam{
190138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
191178354Ssam	ni->ni_inact_reload = ni->ni_vap->iv_inact_run;
192172062Ssam	ni->ni_inact = ni->ni_inact_reload;
193138568Ssam}
194138568Ssam
195138568Ssamvoid
196148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
197138568Ssam{
198138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
199178354Ssam	ni->ni_inact_reload = ni->ni_vap->iv_inact_auth;
200172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
201172062Ssam		ni->ni_inact = ni->ni_inact_reload;
202138568Ssam}
203138568Ssam
204116742Ssam/*
205183251Ssam * Fix tx parameters for a node according to ``association state''.
206183251Ssam */
207183251Ssamstatic void
208183251Ssamnode_setuptxparms(struct ieee80211_node *ni)
209183251Ssam{
210183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
211183251Ssam
212183251Ssam	if (ni->ni_flags & IEEE80211_NODE_HT) {
213183251Ssam		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
214183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NA];
215183251Ssam		else
216183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11NG];
217183251Ssam	} else {				/* legacy rate handling */
218183251Ssam		if (IEEE80211_IS_CHAN_A(ni->ni_chan))
219183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11A];
220183251Ssam		else if (ni->ni_flags & IEEE80211_NODE_ERP)
221183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11G];
222183251Ssam		else
223183251Ssam			ni->ni_txparms = &vap->iv_txparms[IEEE80211_MODE_11B];
224183251Ssam	}
225183251Ssam}
226183251Ssam
227183251Ssam/*
228138568Ssam * Set/change the channel.  The rate set is also updated as
229138568Ssam * to insure a consistent view by drivers.
230178354Ssam * XXX should be private but hostap needs it to deal with CSA
231138568Ssam */
232178354Ssamvoid
233178354Ssamieee80211_node_set_chan(struct ieee80211_node *ni,
234178354Ssam	struct ieee80211_channel *chan)
235138568Ssam{
236178354Ssam	struct ieee80211com *ic = ni->ni_ic;
237183251Ssam	struct ieee80211vap *vap = ni->ni_vap;
238183251Ssam	enum ieee80211_phymode mode;
239170530Ssam
240178354Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
241178354Ssam
242138568Ssam	ni->ni_chan = chan;
243183251Ssam	mode = ieee80211_chan2mode(chan);
244170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
245170530Ssam		/*
246170530Ssam		 * XXX Gotta be careful here; the rate set returned by
247170530Ssam		 * ieee80211_get_suprates is actually any HT rate
248170530Ssam		 * set so blindly copying it will be bad.  We must
249170530Ssam		 * install the legacy rate est in ni_rates and the
250170530Ssam		 * HT rate set in ni_htrates.
251170530Ssam		 */
252170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
253183251Ssam		/*
254183251Ssam		 * Setup bss tx parameters based on operating mode.  We
255183251Ssam		 * use legacy rates when operating in a mixed HT+non-HT bss
256183251Ssam		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
257183251Ssam		 */
258183251Ssam		if (mode == IEEE80211_MODE_11NA &&
259183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
260183251Ssam			mode = IEEE80211_MODE_11A;
261183251Ssam		else if (mode == IEEE80211_MODE_11NG &&
262183251Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_PUREN) == 0)
263183251Ssam			mode = IEEE80211_MODE_11G;
264183251Ssam		if (mode == IEEE80211_MODE_11G &&
265183251Ssam		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
266183251Ssam			mode = IEEE80211_MODE_11B;
267170530Ssam	}
268183251Ssam	ni->ni_txparms = &vap->iv_txparms[mode];
269165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
270138568Ssam}
271138568Ssam
272141658Ssamstatic __inline void
273141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
274141658Ssam{
275141658Ssam	/* propagate useful state */
276141658Ssam	nbss->ni_authmode = obss->ni_authmode;
277141658Ssam	nbss->ni_txpower = obss->ni_txpower;
278141658Ssam	nbss->ni_vlan = obss->ni_vlan;
279141658Ssam	/* XXX statistics? */
280178354Ssam	/* XXX legacy WDS bssid? */
281141658Ssam}
282141658Ssam
283116742Ssamvoid
284178354Ssamieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
285116742Ssam{
286178354Ssam	struct ieee80211com *ic = vap->iv_ic;
287116742Ssam	struct ieee80211_node *ni;
288116742Ssam
289178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
290178354Ssam		"%s: creating ibss on channel %u\n", __func__,
291178354Ssam		ieee80211_chan2ieee(ic, chan));
292138568Ssam
293178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
294140753Ssam	if (ni == NULL) {
295140753Ssam		/* XXX recovery? */
296138568Ssam		return;
297138568Ssam	}
298178354Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
299178354Ssam	ni->ni_esslen = vap->iv_des_ssid[0].len;
300178354Ssam	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
301178354Ssam	if (vap->iv_bss != NULL)
302178354Ssam		copy_bss(ni, vap->iv_bss);
303148843Ssam	ni->ni_intval = ic->ic_bintval;
304178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
305116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
306116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
307116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
308116742Ssam		ni->ni_fhindex = 1;
309116742Ssam	}
310178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
311178354Ssam		vap->iv_flags |= IEEE80211_F_SIBSS;
312138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
313178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
314178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
315167282Ssam		else {
316167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
317167282Ssam			/* clear group bit, add local bit */
318167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
319167282Ssam		}
320178354Ssam	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
321178354Ssam		if (vap->iv_flags & IEEE80211_F_DESBSSID)
322178354Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
323153403Ssam		else
324153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
325138568Ssam	}
326138568Ssam	/*
327138568Ssam	 * Fix the channel and related attributes.
328138568Ssam	 */
329178354Ssam	/* clear DFS CAC state on previous channel */
330178354Ssam	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
331178354Ssam	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
332178354Ssam	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
333178354Ssam		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
334170530Ssam	ic->ic_bsschan = chan;
335178354Ssam	ieee80211_node_set_chan(ni, chan);
336170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
337138568Ssam	/*
338178354Ssam	 * Do mode-specific setup.
339138568Ssam	 */
340170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
341170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
342170530Ssam			/*
343178354Ssam			 * Use a mixed 11b/11g basic rate set.
344170530Ssam			 */
345178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
346178354Ssam			    IEEE80211_MODE_11G);
347178354Ssam			if (vap->iv_flags & IEEE80211_F_PUREG) {
348178354Ssam				/*
349178354Ssam				 * Also mark OFDM rates basic so 11b
350178354Ssam				 * stations do not join (WiFi compliance).
351178354Ssam				 */
352178354Ssam				ieee80211_addbasicrates(&ni->ni_rates,
353178354Ssam				    IEEE80211_MODE_11A);
354178354Ssam			}
355170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
356170530Ssam			/*
357170530Ssam			 * Force pure 11b rate set.
358170530Ssam			 */
359178354Ssam			ieee80211_setbasicrates(&ni->ni_rates,
360170530Ssam				IEEE80211_MODE_11B);
361170530Ssam		}
362170530Ssam	}
363138568Ssam
364170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
365116742Ssam}
366116742Ssam
367170530Ssam/*
368170530Ssam * Reset bss state on transition to the INIT state.
369170530Ssam * Clear any stations from the table (they have been
370170530Ssam * deauth'd) and reset the bss node (clears key, rate
371170530Ssam * etc. state).
372170530Ssam */
373138568Ssamvoid
374178354Ssamieee80211_reset_bss(struct ieee80211vap *vap)
375138568Ssam{
376178354Ssam	struct ieee80211com *ic = vap->iv_ic;
377138568Ssam	struct ieee80211_node *ni, *obss;
378138568Ssam
379178354Ssam	ieee80211_node_table_reset(&ic->ic_sta, vap);
380178354Ssam	/* XXX multi-bss: wrong */
381170530Ssam	ieee80211_reset_erp(ic);
382140753Ssam
383178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
384138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
385178354Ssam	obss = vap->iv_bss;
386178354Ssam	vap->iv_bss = ieee80211_ref_node(ni);
387141658Ssam	if (obss != NULL) {
388141658Ssam		copy_bss(ni, obss);
389148843Ssam		ni->ni_intval = ic->ic_bintval;
390138568Ssam		ieee80211_free_node(obss);
391178354Ssam	} else
392178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
393138568Ssam}
394138568Ssam
395170530Ssamstatic int
396170530Ssammatch_ssid(const struct ieee80211_node *ni,
397170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
398170530Ssam{
399170530Ssam	int i;
400148432Ssam
401170530Ssam	for (i = 0; i < nssid; i++) {
402170530Ssam		if (ni->ni_esslen == ssids[i].len &&
403170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
404170530Ssam			return 1;
405170530Ssam	}
406170530Ssam	return 0;
407170530Ssam}
408170530Ssam
409170530Ssam/*
410170530Ssam * Test a node for suitability/compatibility.
411170530Ssam */
412127767Ssamstatic int
413178354Ssamcheck_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
414127767Ssam{
415178354Ssam	struct ieee80211com *ic = ni->ni_ic;
416170530Ssam        uint8_t rate;
417170530Ssam
418170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
419170530Ssam		return 0;
420178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
421170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
422170530Ssam			return 0;
423170530Ssam	} else {
424170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
425170530Ssam			return 0;
426170530Ssam	}
427178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
428170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
429170530Ssam			return 0;
430170530Ssam	} else {
431170530Ssam		/* XXX does this mean privacy is supported or required? */
432170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
433170530Ssam			return 0;
434170530Ssam	}
435170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
436170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
437170530Ssam	if (rate & IEEE80211_RATE_BASIC)
438170530Ssam		return 0;
439178354Ssam	if (vap->iv_des_nssid != 0 &&
440178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
441170530Ssam		return 0;
442178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
443178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
444170530Ssam		return 0;
445170530Ssam	return 1;
446170530Ssam}
447170530Ssam
448170530Ssam#ifdef IEEE80211_DEBUG
449170530Ssam/*
450170530Ssam * Display node suitability/compatibility.
451170530Ssam */
452170530Ssamstatic void
453178354Ssamcheck_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
454170530Ssam{
455178354Ssam	struct ieee80211com *ic = ni->ni_ic;
456170530Ssam        uint8_t rate;
457127767Ssam        int fail;
458127767Ssam
459127767Ssam	fail = 0;
460127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
461127767Ssam		fail |= 0x01;
462178354Ssam	if (vap->iv_opmode == IEEE80211_M_IBSS) {
463127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
464127767Ssam			fail |= 0x02;
465127767Ssam	} else {
466127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
467127767Ssam			fail |= 0x02;
468127767Ssam	}
469178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
470127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
471127767Ssam			fail |= 0x04;
472127767Ssam	} else {
473127767Ssam		/* XXX does this mean privacy is supported or required? */
474127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
475127767Ssam			fail |= 0x04;
476127767Ssam	}
477167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
478165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
479127767Ssam	if (rate & IEEE80211_RATE_BASIC)
480127767Ssam		fail |= 0x08;
481178354Ssam	if (vap->iv_des_nssid != 0 &&
482178354Ssam	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
483127767Ssam		fail |= 0x10;
484178354Ssam	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
485178354Ssam	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
486127767Ssam		fail |= 0x20;
487127767Ssam
488170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
489170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
490170530Ssam	printf(" %3d%c",
491170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
492170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
493170530Ssam	    fail & 0x08 ? '!' : ' ');
494170530Ssam	printf(" %4s%c",
495170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
496170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
497170530Ssam	    "????",
498170530Ssam	    fail & 0x02 ? '!' : ' ');
499170530Ssam	printf(" %3s%c ",
500170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
501170530Ssam	    fail & 0x04 ? '!' : ' ');
502170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
503170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
504138568Ssam}
505170530Ssam#endif /* IEEE80211_DEBUG */
506138568Ssam
507138568Ssam/*
508138568Ssam * Handle 802.11 ad hoc network merge.  The
509138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
510138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
511138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
512138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
513138568Ssam * sends beacons with the greatest TSF timestamp.
514138568Ssam *
515138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
516138568Ssam *
517138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
518138568Ssam */
519138568Ssamint
520148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
521138568Ssam{
522178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
523178354Ssam#ifdef IEEE80211_DEBUG
524148306Ssam	struct ieee80211com *ic = ni->ni_ic;
525178354Ssam#endif
526138568Ssam
527178354Ssam	if (ni == vap->iv_bss ||
528178354Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
529138568Ssam		/* unchanged, nothing to do */
530138568Ssam		return 0;
531138568Ssam	}
532178354Ssam	if (!check_bss(vap, ni)) {
533170530Ssam		/* capabilities mismatch */
534178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
535138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
536170530Ssam#ifdef IEEE80211_DEBUG
537178354Ssam		if (ieee80211_msg_assoc(vap))
538178354Ssam			check_bss_debug(vap, ni);
539170530Ssam#endif
540178354Ssam		vap->iv_stats.is_ibss_capmismatch++;
541138568Ssam		return 0;
542138568Ssam	}
543178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
544138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
545138568Ssam		ether_sprintf(ni->ni_bssid),
546138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
547138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
548138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
549138568Ssam	);
550170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
551138568Ssam}
552138568Ssam
553138568Ssam/*
554178354Ssam * Calculate HT channel promotion flags for all vaps.
555178354Ssam * This assumes ni_chan have been setup for each vap.
556173273Ssam */
557178354Ssamstatic int
558178354Ssamgethtadjustflags(struct ieee80211com *ic)
559178354Ssam{
560178354Ssam	struct ieee80211vap *vap;
561178354Ssam	int flags;
562178354Ssam
563178354Ssam	flags = 0;
564178354Ssam	/* XXX locking */
565178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
566178354Ssam		if (vap->iv_state < IEEE80211_S_RUN)
567178354Ssam			continue;
568178354Ssam		switch (vap->iv_opmode) {
569178354Ssam		case IEEE80211_M_WDS:
570178354Ssam		case IEEE80211_M_STA:
571178354Ssam		case IEEE80211_M_AHDEMO:
572178354Ssam		case IEEE80211_M_HOSTAP:
573178354Ssam		case IEEE80211_M_IBSS:
574178354Ssam			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
575178354Ssam			break;
576178354Ssam		default:
577178354Ssam			break;
578178354Ssam		}
579178354Ssam	}
580178354Ssam	return flags;
581178354Ssam}
582178354Ssam
583178354Ssam/*
584178354Ssam * Check if the current channel needs to change based on whether
585178354Ssam * any vap's are using HT20/HT40.  This is used sync the state of
586178354Ssam * ic_curchan after a channel width change on a running vap.
587178354Ssam */
588173273Ssamvoid
589178354Ssamieee80211_sync_curchan(struct ieee80211com *ic)
590173273Ssam{
591178354Ssam	struct ieee80211_channel *c;
592178354Ssam
593178354Ssam	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
594178354Ssam	if (c != ic->ic_curchan) {
595178354Ssam		ic->ic_curchan = c;
596178354Ssam		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
597178354Ssam		ic->ic_set_channel(ic);
598178354Ssam	}
599178354Ssam}
600178354Ssam
601178354Ssam/*
602178354Ssam * Change the current channel.  The request channel may be
603178354Ssam * promoted if other vap's are operating with HT20/HT40.
604178354Ssam */
605178354Ssamvoid
606178354Ssamieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
607178354Ssam{
608178354Ssam	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
609178354Ssam		int flags = gethtadjustflags(ic);
610178354Ssam		/*
611178354Ssam		 * Check for channel promotion required to support the
612178354Ssam		 * set of running vap's.  This assumes we are called
613178354Ssam		 * after ni_chan is setup for each vap.
614178354Ssam		 */
615178354Ssam		/* NB: this assumes IEEE80211_FEXT_USEHT40 > IEEE80211_FEXT_HT */
616178354Ssam		if (flags > ieee80211_htchanflags(c))
617178354Ssam			c = ieee80211_ht_adjust_channel(ic, c, flags);
618178354Ssam	}
619178354Ssam	ic->ic_bsschan = ic->ic_curchan = c;
620173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
621173273Ssam	ic->ic_set_channel(ic);
622173273Ssam}
623173273Ssam
624173273Ssam/*
625138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
626138568Ssam * be passed in with a held reference.
627138568Ssam */
628170530Ssamstatic int
629170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
630138568Ssam{
631178354Ssam	struct ieee80211vap *vap = selbs->ni_vap;
632170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
633138568Ssam	struct ieee80211_node *obss;
634170530Ssam	int canreassoc;
635138568Ssam
636138568Ssam	/*
637138568Ssam	 * Committed to selbs, setup state.
638138568Ssam	 */
639178354Ssam	obss = vap->iv_bss;
640170530Ssam	/*
641170530Ssam	 * Check if old+new node have the same address in which
642170530Ssam	 * case we can reassociate when operating in sta mode.
643170530Ssam	 */
644170530Ssam	canreassoc = (obss != NULL &&
645178354Ssam		vap->iv_state == IEEE80211_S_RUN &&
646170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
647178354Ssam	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
648153352Ssam	if (obss != NULL) {
649153352Ssam		copy_bss(selbs, obss);
650178354Ssam		ieee80211_node_reclaim(obss);
651178354Ssam		obss = NULL;		/* NB: guard against later use */
652153352Ssam	}
653165887Ssam
654138568Ssam	/*
655165887Ssam	 * Delete unusable rates; we've already checked
656165887Ssam	 * that the negotiated rate set is acceptable.
657165887Ssam	 */
658178354Ssam	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
659167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
660165887Ssam
661178354Ssam	ieee80211_setcurchan(ic, selbs->ni_chan);
662165887Ssam	/*
663138568Ssam	 * Set the erp state (mostly the slot time) to deal with
664138568Ssam	 * the auto-select case; this should be redundant if the
665138568Ssam	 * mode is locked.
666138568Ssam	 */
667138568Ssam	ieee80211_reset_erp(ic);
668178354Ssam	ieee80211_wme_initparams(vap);
669140753Ssam
670178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA) {
671170530Ssam		if (canreassoc) {
672170530Ssam			/* Reassociate */
673178354Ssam			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
674170530Ssam		} else {
675170530Ssam			/*
676170530Ssam			 * Act as if we received a DEAUTH frame in case we
677170530Ssam			 * are invoked from the RUN state.  This will cause
678170530Ssam			 * us to try to re-authenticate if we are operating
679170530Ssam			 * as a station.
680170530Ssam			 */
681178354Ssam			ieee80211_new_state(vap, IEEE80211_S_AUTH,
682170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
683170530Ssam		}
684170530Ssam	} else
685178354Ssam		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
686138568Ssam	return 1;
687116742Ssam}
688116742Ssam
689170530Ssamint
690178354Ssamieee80211_sta_join(struct ieee80211vap *vap,
691170530Ssam	const struct ieee80211_scan_entry *se)
692170530Ssam{
693178354Ssam	struct ieee80211com *ic = vap->iv_ic;
694170530Ssam	struct ieee80211_node *ni;
695170530Ssam
696178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
697170530Ssam	if (ni == NULL) {
698170530Ssam		/* XXX msg */
699170530Ssam		return 0;
700170530Ssam	}
701170530Ssam	/*
702170530Ssam	 * Expand scan state into node's format.
703170530Ssam	 * XXX may not need all this stuff
704170530Ssam	 */
705170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
706170530Ssam	ni->ni_esslen = se->se_ssid[1];
707170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
708170530Ssam	ni->ni_rstamp = se->se_rstamp;
709170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
710170530Ssam	ni->ni_intval = se->se_intval;
711170530Ssam	ni->ni_capinfo = se->se_capinfo;
712170530Ssam	ni->ni_chan = se->se_chan;
713170530Ssam	ni->ni_timoff = se->se_timoff;
714170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
715170530Ssam	ni->ni_fhindex = se->se_fhindex;
716170530Ssam	ni->ni_erp = se->se_erp;
717178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
718170530Ssam	ni->ni_noise = se->se_noise;
719178354Ssam
720178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
721178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
722178354Ssam		if (ni->ni_ies.ath_ie != NULL)
723178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
724178354Ssam		if (ni->ni_ies.htcap_ie != NULL)
725178354Ssam			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
726178354Ssam		if (ni->ni_ies.htinfo_ie != NULL)
727178354Ssam			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
728173864Ssam	}
729170530Ssam
730178354Ssam	vap->iv_dtim_period = se->se_dtimperiod;
731178354Ssam	vap->iv_dtim_count = 0;
732170530Ssam
733170530Ssam	/* NB: must be after ni_chan is setup */
734170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
735170530Ssam		IEEE80211_F_DOSORT);
736170530Ssam
737170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
738170530Ssam}
739170530Ssam
740138568Ssam/*
741138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
742138568Ssam * be passed in with a held reference.
743138568Ssam */
744138568Ssamvoid
745178354Ssamieee80211_sta_leave(struct ieee80211_node *ni)
746138568Ssam{
747178354Ssam	struct ieee80211com *ic = ni->ni_ic;
748178354Ssam
749138568Ssam	ic->ic_node_cleanup(ni);
750178354Ssam	ieee80211_notify_node_leave(ni);
751138568Ssam}
752138568Ssam
753178354Ssam/*
754178354Ssam * Send a deauthenticate frame and drop the station.
755178354Ssam */
756178354Ssamvoid
757178354Ssamieee80211_node_deauth(struct ieee80211_node *ni, int reason)
758178354Ssam{
759178354Ssam	/* NB: bump the refcnt to be sure temporay nodes are not reclaimed */
760178354Ssam	ieee80211_ref_node(ni);
761178354Ssam	if (ni->ni_associd != 0)
762178354Ssam		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
763178354Ssam	ieee80211_node_leave(ni);
764178354Ssam	ieee80211_free_node(ni);
765178354Ssam}
766178354Ssam
767116742Ssamstatic struct ieee80211_node *
768179643Ssamnode_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
769116742Ssam{
770127768Ssam	struct ieee80211_node *ni;
771138568Ssam
772184210Sdes	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
773127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
774127768Ssam	return ni;
775116742Ssam}
776116742Ssam
777138568Ssam/*
778178354Ssam * Initialize an ie blob with the specified data.  If previous
779178354Ssam * data exists re-use the data block.  As a side effect we clear
780178354Ssam * all references to specific ie's; the caller is required to
781178354Ssam * recalculate them.
782178354Ssam */
783178354Ssamint
784178354Ssamieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
785178354Ssam{
786178354Ssam	/* NB: assumes data+len are the last fields */
787178354Ssam	memset(ies, 0, offsetof(struct ieee80211_ies, data));
788178354Ssam	if (ies->data != NULL && ies->len != len) {
789178354Ssam		/* data size changed */
790184210Sdes		FREE(ies->data, M_80211_NODE_IE);
791178354Ssam		ies->data = NULL;
792178354Ssam	}
793178354Ssam	if (ies->data == NULL) {
794184210Sdes		MALLOC(ies->data, uint8_t *, len, M_80211_NODE_IE, M_NOWAIT);
795178354Ssam		if (ies->data == NULL) {
796178354Ssam			ies->len = 0;
797178354Ssam			/* NB: pointers have already been zero'd above */
798178354Ssam			return 0;
799178354Ssam		}
800178354Ssam	}
801178354Ssam	memcpy(ies->data, data, len);
802178354Ssam	ies->len = len;
803178354Ssam	return 1;
804178354Ssam}
805178354Ssam
806178354Ssam/*
807178354Ssam * Reclaim storage for an ie blob.
808178354Ssam */
809178354Ssamvoid
810178354Ssamieee80211_ies_cleanup(struct ieee80211_ies *ies)
811178354Ssam{
812178354Ssam	if (ies->data != NULL)
813184210Sdes		FREE(ies->data, M_80211_NODE_IE);
814178354Ssam}
815178354Ssam
816178354Ssam/*
817178354Ssam * Expand an ie blob data contents and to fillin individual
818178354Ssam * ie pointers.  The data blob is assumed to be well-formed;
819178354Ssam * we don't do any validity checking of ie lengths.
820178354Ssam */
821178354Ssamvoid
822178354Ssamieee80211_ies_expand(struct ieee80211_ies *ies)
823178354Ssam{
824178354Ssam	uint8_t *ie;
825178354Ssam	int ielen;
826178354Ssam
827178354Ssam	ie = ies->data;
828178354Ssam	ielen = ies->len;
829178354Ssam	while (ielen > 0) {
830178354Ssam		switch (ie[0]) {
831178354Ssam		case IEEE80211_ELEMID_VENDOR:
832178354Ssam			if (iswpaoui(ie))
833178354Ssam				ies->wpa_ie = ie;
834178354Ssam			else if (iswmeoui(ie))
835178354Ssam				ies->wme_ie = ie;
836178354Ssam			else if (isatherosoui(ie))
837178354Ssam				ies->ath_ie = ie;
838178354Ssam			break;
839178354Ssam		case IEEE80211_ELEMID_RSN:
840178354Ssam			ies->rsn_ie = ie;
841178354Ssam			break;
842178354Ssam		case IEEE80211_ELEMID_HTCAP:
843178354Ssam			ies->htcap_ie = ie;
844178354Ssam			break;
845178354Ssam		}
846178354Ssam		ielen -= 2 + ie[1];
847178354Ssam		ie += 2 + ie[1];
848178354Ssam	}
849178354Ssam}
850178354Ssam
851178354Ssam/*
852138568Ssam * Reclaim any resources in a node and reset any critical
853138568Ssam * state.  Typically nodes are free'd immediately after,
854138568Ssam * but in some cases the storage may be reused so we need
855138568Ssam * to insure consistent state (should probably fix that).
856138568Ssam */
857116742Ssamstatic void
858138568Ssamnode_cleanup(struct ieee80211_node *ni)
859116742Ssam{
860138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
861178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
862170530Ssam	int i;
863138568Ssam
864138568Ssam	/* NB: preserve ni_table */
865138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
866178354Ssam		if (vap->iv_opmode != IEEE80211_M_STA)
867178354Ssam			vap->iv_ps_sta--;
868138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
869178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
870178354Ssam		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
871138568Ssam	}
872147788Ssam	/*
873173273Ssam	 * Cleanup any HT-related state.
874173273Ssam	 */
875173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
876173273Ssam		ieee80211_ht_node_cleanup(ni);
877173273Ssam	/*
878147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
879147788Ssam	 * has happened.  This is probably not needed as the node
880147788Ssam	 * should always be removed from the table so not found but
881147788Ssam	 * do it just in case.
882147788Ssam	 */
883147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
884138568Ssam
885138568Ssam	/*
886138568Ssam	 * Drain power save queue and, if needed, clear TIM.
887138568Ssam	 */
888178354Ssam	if (ieee80211_node_saveq_drain(ni) != 0 && vap->iv_set_tim != NULL)
889178354Ssam		vap->iv_set_tim(ni, 0);
890138568Ssam
891138568Ssam	ni->ni_associd = 0;
892138568Ssam	if (ni->ni_challenge != NULL) {
893184210Sdes		FREE(ni->ni_challenge, M_80211_NODE);
894138568Ssam		ni->ni_challenge = NULL;
895138568Ssam	}
896138568Ssam	/*
897138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
898138568Ssam	 * reusable during a re-auth/re-assoc state transition.
899138568Ssam	 * If we remove these data they will not be recreated
900138568Ssam	 * because they come from a probe-response or beacon frame
901138568Ssam	 * which cannot be expected prior to the association-response.
902138568Ssam	 * This should not be an issue when operating in other modes
903138568Ssam	 * as stations leaving always go through a full state transition
904138568Ssam	 * which will rebuild this state.
905138568Ssam	 *
906138568Ssam	 * XXX does this leave us open to inheriting old state?
907138568Ssam	 */
908138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
909138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
910138568Ssam			m_freem(ni->ni_rxfrag[i]);
911138568Ssam			ni->ni_rxfrag[i] = NULL;
912138568Ssam		}
913148863Ssam	/*
914148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
915148863Ssam	 */
916148863Ssam	ieee80211_node_delucastkey(ni);
917138568Ssam#undef N
918116742Ssam}
919116742Ssam
920116742Ssamstatic void
921138568Ssamnode_free(struct ieee80211_node *ni)
922116742Ssam{
923138568Ssam	struct ieee80211com *ic = ni->ni_ic;
924138568Ssam
925138568Ssam	ic->ic_node_cleanup(ni);
926178354Ssam	ieee80211_ies_cleanup(&ni->ni_ies);
927138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
928178354Ssam	IEEE80211_NODE_WDSQ_DESTROY(ni);
929184210Sdes	FREE(ni, M_80211_NODE);
930116742Ssam}
931116742Ssam
932178354Ssamstatic void
933178354Ssamnode_age(struct ieee80211_node *ni)
934178354Ssam{
935178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
936178354Ssam#if 0
937178354Ssam	IEEE80211_NODE_LOCK_ASSERT(&ic->ic_sta);
938178354Ssam#endif
939178354Ssam	/*
940178354Ssam	 * Age frames on the power save queue.
941178354Ssam	 */
942178354Ssam	if (ieee80211_node_saveq_age(ni) != 0 &&
943178354Ssam	    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
944178354Ssam	    vap->iv_set_tim != NULL)
945178354Ssam		vap->iv_set_tim(ni, 0);
946178354Ssam	/*
947178354Ssam	 * Age frames on the wds pending queue.
948178354Ssam	 */
949178354Ssam	if (IEEE80211_NODE_WDSQ_QLEN(ni) != 0)
950178354Ssam		ieee80211_node_wdsq_age(ni);
951178354Ssam	/*
952178354Ssam	 * Age out HT resources (e.g. frames on the
953178354Ssam	 * A-MPDU reorder queues).
954178354Ssam	 */
955178354Ssam	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
956178354Ssam		ieee80211_ht_node_age(ni);
957178354Ssam}
958178354Ssam
959170530Ssamstatic int8_t
960138568Ssamnode_getrssi(const struct ieee80211_node *ni)
961120104Ssam{
962178354Ssam	uint32_t avgrssi = ni->ni_avgrssi;
963178354Ssam	int32_t rssi;
964178354Ssam
965178354Ssam	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
966178354Ssam		return 0;
967178354Ssam	rssi = IEEE80211_RSSI_GET(avgrssi);
968178354Ssam	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
969120104Ssam}
970120104Ssam
971116742Ssamstatic void
972170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
973170530Ssam{
974178354Ssam	*rssi = node_getrssi(ni);
975170530Ssam	*noise = ni->ni_noise;
976170530Ssam}
977170530Ssam
978170530Ssamstatic void
979178354Ssamnode_getmimoinfo(const struct ieee80211_node *ni,
980178354Ssam	struct ieee80211_mimo_info *info)
981116742Ssam{
982178354Ssam	/* XXX zero data? */
983178354Ssam}
984178354Ssam
985178354Ssamstruct ieee80211_node *
986178354Ssamieee80211_alloc_node(struct ieee80211_node_table *nt,
987178354Ssam	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
988178354Ssam{
989138568Ssam	struct ieee80211com *ic = nt->nt_ic;
990178354Ssam	struct ieee80211_node *ni;
991116742Ssam	int hash;
992116742Ssam
993179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
994178354Ssam	if (ni == NULL) {
995178354Ssam		vap->iv_stats.is_rx_nodealloc++;
996178354Ssam		return NULL;
997178354Ssam	}
998178354Ssam
999178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1000140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1001138568Ssam		ether_sprintf(macaddr), nt->nt_name);
1002138568Ssam
1003116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1004116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1005138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
1006138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
1007138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
1008138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
1009183251Ssam	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1010178354Ssam	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
1011178354Ssam	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
1012139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
1013139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1014170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
1015138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1016178354Ssam	IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1017138568Ssam
1018138568Ssam	IEEE80211_NODE_LOCK(nt);
1019138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
1020138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
1021138568Ssam	ni->ni_table = nt;
1022178354Ssam	ni->ni_vap = vap;
1023138568Ssam	ni->ni_ic = ic;
1024138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1025116742Ssam
1026116742Ssam	return ni;
1027116742Ssam}
1028116742Ssam
1029148777Ssam/*
1030148777Ssam * Craft a temporary node suitable for sending a management frame
1031148777Ssam * to the specified station.  We craft only as much state as we
1032148777Ssam * need to do the work since the node will be immediately reclaimed
1033148777Ssam * once the send completes.
1034148777Ssam */
1035116742Ssamstruct ieee80211_node *
1036178354Ssamieee80211_tmp_node(struct ieee80211vap *vap,
1037178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1038148777Ssam{
1039178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1040148777Ssam	struct ieee80211_node *ni;
1041148777Ssam
1042179643Ssam	ni = ic->ic_node_alloc(vap, macaddr);
1043148777Ssam	if (ni != NULL) {
1044183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1045183259Ssam
1046178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1047148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1048148777Ssam
1049178354Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1050178354Ssam		ni->ni_ic = ic;			/* NB: needed to set channel */
1051178354Ssam		ni->ni_vap = vap;
1052178354Ssam
1053148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1054183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1055148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1056148777Ssam		/* NB: required by ieee80211_fix_rate */
1057183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1058178354Ssam		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
1059148777Ssam			IEEE80211_KEYIX_NONE);
1060183259Ssam		ni->ni_txpower = bss->ni_txpower;
1061148777Ssam		/* XXX optimize away */
1062148777Ssam		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1063178354Ssam		IEEE80211_NODE_WDSQ_INIT(ni, "unknown");
1064148777Ssam	} else {
1065148777Ssam		/* XXX msg */
1066178354Ssam		vap->iv_stats.is_rx_nodealloc++;
1067148777Ssam	}
1068148777Ssam	return ni;
1069148777Ssam}
1070148777Ssam
1071148777Ssamstruct ieee80211_node *
1072178354Ssamieee80211_dup_bss(struct ieee80211vap *vap,
1073178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1074116742Ssam{
1075178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1076138568Ssam	struct ieee80211_node *ni;
1077138568Ssam
1078178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
1079116742Ssam	if (ni != NULL) {
1080183259Ssam		struct ieee80211_node *bss = vap->iv_bss;
1081127770Ssam		/*
1082178354Ssam		 * Inherit from iv_bss.
1083127770Ssam		 */
1084183259Ssam		copy_bss(ni, bss);
1085183259Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
1086183259Ssam		ieee80211_node_set_chan(ni, bss->ni_chan);
1087178354Ssam	}
1088116742Ssam	return ni;
1089116742Ssam}
1090116742Ssam
1091178354Ssam/*
1092178354Ssam * Create a bss node for a legacy WDS vap.  The far end does
1093178354Ssam * not associate so we just create create a new node and
1094178354Ssam * simulate an association.  The caller is responsible for
1095178354Ssam * installing the node as the bss node and handling any further
1096178354Ssam * setup work like authorizing the port.
1097178354Ssam */
1098178354Ssamstruct ieee80211_node *
1099178354Ssamieee80211_node_create_wds(struct ieee80211vap *vap,
1100178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
1101178354Ssam{
1102178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1103178354Ssam	struct ieee80211_node *ni;
1104178354Ssam
1105178354Ssam	/* XXX check if node already in sta table? */
1106178354Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
1107178354Ssam	if (ni != NULL) {
1108178354Ssam		ni->ni_wdsvap = vap;
1109178354Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
1110178354Ssam		/*
1111178354Ssam		 * Inherit any manually configured settings.
1112178354Ssam		 */
1113183259Ssam		copy_bss(ni, vap->iv_bss);
1114178354Ssam		ieee80211_node_set_chan(ni, chan);
1115178354Ssam		/* NB: propagate ssid so available to WPA supplicant */
1116178354Ssam		ni->ni_esslen = vap->iv_des_ssid[0].len;
1117178354Ssam		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
1118178354Ssam		/* NB: no associd for peer */
1119178354Ssam		/*
1120178354Ssam		 * There are no management frames to use to
1121178354Ssam		 * discover neighbor capabilities, so blindly
1122178354Ssam		 * propagate the local configuration.
1123178354Ssam		 */
1124178354Ssam		if (vap->iv_flags & IEEE80211_F_WME)
1125178354Ssam			ni->ni_flags |= IEEE80211_NODE_QOS;
1126178354Ssam		if (vap->iv_flags & IEEE80211_F_FF)
1127178354Ssam			ni->ni_flags |= IEEE80211_NODE_FF;
1128178354Ssam		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1129178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_HT)) {
1130178354Ssam			/*
1131178354Ssam			 * Device is HT-capable and HT is enabled for
1132178354Ssam			 * the vap; setup HT operation.  On return
1133178354Ssam			 * ni_chan will be adjusted to an HT channel.
1134178354Ssam			 */
1135178354Ssam			ieee80211_ht_wds_init(ni);
1136178354Ssam		} else {
1137178354Ssam			struct ieee80211_channel *c = ni->ni_chan;
1138178354Ssam			/*
1139178354Ssam			 * Force a legacy channel to be used.
1140178354Ssam			 */
1141178354Ssam			c = ieee80211_find_channel(ic,
1142178354Ssam			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
1143178354Ssam			KASSERT(c != NULL, ("no legacy channel, %u/%x",
1144178354Ssam			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
1145178354Ssam			ni->ni_chan = c;
1146178354Ssam		}
1147178354Ssam	}
1148178354Ssam	return ni;
1149178354Ssam}
1150178354Ssam
1151178354Ssamstruct ieee80211_node *
1152138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1153178354Ssamieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
1154178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1155138568Ssam#else
1156178354Ssamieee80211_find_node_locked(struct ieee80211_node_table *nt,
1157178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1158138568Ssam#endif
1159116742Ssam{
1160116742Ssam	struct ieee80211_node *ni;
1161116742Ssam	int hash;
1162116742Ssam
1163138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1164127772Ssam
1165116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1166138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1167116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1168138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1169138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1170178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1171140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1172140766Ssam			    func, line,
1173140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1174140766Ssam			    ieee80211_node_refcnt(ni));
1175138568Ssam#endif
1176127772Ssam			return ni;
1177116742Ssam		}
1178116742Ssam	}
1179127772Ssam	return NULL;
1180127772Ssam}
1181178354Ssam
1182178354Ssamstruct ieee80211_node *
1183138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1184178354Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1185178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1186178354Ssam#else
1187178354Ssamieee80211_find_node(struct ieee80211_node_table *nt,
1188178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1189138568Ssam#endif
1190178354Ssam{
1191178354Ssam	struct ieee80211_node *ni;
1192127772Ssam
1193178354Ssam	IEEE80211_NODE_LOCK(nt);
1194178354Ssam	ni = ieee80211_find_node_locked(nt, macaddr);
1195178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1196178354Ssam	return ni;
1197178354Ssam}
1198178354Ssam
1199127772Ssamstruct ieee80211_node *
1200138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1201178354Ssamieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
1202178354Ssam	const struct ieee80211vap *vap,
1203178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1204138568Ssam#else
1205178354Ssamieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
1206178354Ssam	const struct ieee80211vap *vap,
1207178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1208138568Ssam#endif
1209127772Ssam{
1210127772Ssam	struct ieee80211_node *ni;
1211178354Ssam	int hash;
1212127772Ssam
1213178354Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1214178354Ssam
1215178354Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1216178354Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1217178354Ssam		if (ni->ni_vap == vap &&
1218178354Ssam		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1219178354Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1220178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1221178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1222178354Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1223178354Ssam			    func, line,
1224178354Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1225178354Ssam			    ieee80211_node_refcnt(ni));
1226178354Ssam#endif
1227178354Ssam			return ni;
1228178354Ssam		}
1229178354Ssam	}
1230178354Ssam	return NULL;
1231178354Ssam}
1232178354Ssam
1233178354Ssamstruct ieee80211_node *
1234178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1235178354Ssamieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
1236178354Ssam	const struct ieee80211vap *vap,
1237178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
1238178354Ssam#else
1239178354Ssamieee80211_find_vap_node(struct ieee80211_node_table *nt,
1240178354Ssam	const struct ieee80211vap *vap,
1241178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1242178354Ssam#endif
1243178354Ssam{
1244178354Ssam	struct ieee80211_node *ni;
1245178354Ssam
1246138568Ssam	IEEE80211_NODE_LOCK(nt);
1247178354Ssam	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
1248138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1249116742Ssam	return ni;
1250116742Ssam}
1251116742Ssam
1252116742Ssam/*
1253138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1254138568Ssam * Note that for the driver's benefit we we treat this like
1255138568Ssam * an association so the driver has an opportunity to setup
1256138568Ssam * it's private state.
1257138568Ssam */
1258138568Ssamstruct ieee80211_node *
1259178354Ssamieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
1260170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1261138568Ssam{
1262138568Ssam	struct ieee80211_node *ni;
1263138568Ssam
1264178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1265153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1266178354Ssam	ni = ieee80211_dup_bss(vap, macaddr);
1267138568Ssam	if (ni != NULL) {
1268178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1269178354Ssam
1270138568Ssam		/* XXX no rate negotiation; just dup */
1271178354Ssam		ni->ni_rates = vap->iv_bss->ni_rates;
1272178354Ssam		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
1273153404Ssam			/*
1274170530Ssam			 * In adhoc demo mode there are no management
1275170530Ssam			 * frames to use to discover neighbor capabilities,
1276170530Ssam			 * so blindly propagate the local configuration
1277170530Ssam			 * so we can do interesting things (e.g. use
1278170530Ssam			 * WME to disable ACK's).
1279153404Ssam			 */
1280178354Ssam			if (vap->iv_flags & IEEE80211_F_WME)
1281153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1282178354Ssam			if (vap->iv_flags & IEEE80211_F_FF)
1283170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
1284153404Ssam		}
1285183251Ssam		node_setuptxparms(ni);
1286178354Ssam		if (ic->ic_newassoc != NULL)
1287178354Ssam			ic->ic_newassoc(ni, 1);
1288170530Ssam		/* XXX not right for 802.1x/WPA */
1289170530Ssam		ieee80211_node_authorize(ni);
1290138568Ssam	}
1291138568Ssam	return ni;
1292138568Ssam}
1293138568Ssam
1294148936Ssamvoid
1295153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1296153073Ssam	const struct ieee80211_frame *wh,
1297153073Ssam	const struct ieee80211_scanparams *sp)
1298153073Ssam{
1299153073Ssam	ni->ni_esslen = sp->ssid[1];
1300153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1301153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1302153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1303153073Ssam	ni->ni_intval = sp->bintval;
1304153073Ssam	ni->ni_capinfo = sp->capinfo;
1305153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1306153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1307153073Ssam	ni->ni_fhindex = sp->fhindex;
1308153073Ssam	ni->ni_erp = sp->erp;
1309153073Ssam	ni->ni_timoff = sp->timoff;
1310153073Ssam
1311178354Ssam	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
1312178354Ssam		ieee80211_ies_expand(&ni->ni_ies);
1313178354Ssam		if (ni->ni_ies.ath_ie != NULL)
1314178354Ssam			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
1315178354Ssam	}
1316178354Ssam
1317153073Ssam	/* NB: must be after ni_chan is setup */
1318165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1319165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1320165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1321153073Ssam}
1322153073Ssam
1323148936Ssam/*
1324148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1325148936Ssam * or probe response frame.  Note that for the driver's
1326148936Ssam * benefit we we treat this like an association so the
1327148936Ssam * driver has an opportunity to setup it's private state.
1328148936Ssam */
1329148936Ssamstruct ieee80211_node *
1330178354Ssamieee80211_add_neighbor(struct ieee80211vap *vap,
1331148936Ssam	const struct ieee80211_frame *wh,
1332148936Ssam	const struct ieee80211_scanparams *sp)
1333148936Ssam{
1334148936Ssam	struct ieee80211_node *ni;
1335148936Ssam
1336178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1337153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1338178354Ssam	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
1339148936Ssam	if (ni != NULL) {
1340178354Ssam		struct ieee80211com *ic = vap->iv_ic;
1341178354Ssam
1342153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1343183251Ssam		node_setuptxparms(ni);
1344148936Ssam		if (ic->ic_newassoc != NULL)
1345148936Ssam			ic->ic_newassoc(ni, 1);
1346148936Ssam		/* XXX not right for 802.1x/WPA */
1347148936Ssam		ieee80211_node_authorize(ni);
1348148936Ssam	}
1349148936Ssam	return ni;
1350148936Ssam}
1351148936Ssam
1352148863Ssam#define	IS_CTL(wh) \
1353148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1354148863Ssam#define	IS_PSPOLL(wh) \
1355148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1356170530Ssam#define	IS_BAR(wh) \
1357170530Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1358179220Ssam#define	IS_PROBEREQ(wh) \
1359179220Ssam	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
1360179220Ssam	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
1361179220Ssam#define	IS_BCAST_PROBEREQ(wh) \
1362179220Ssam	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
1363179220Ssam	    ((const struct ieee80211_frame *)(wh))->i_addr3))
1364170530Ssam
1365179220Ssamstatic __inline struct ieee80211_node *
1366179220Ssam_find_rxnode(struct ieee80211_node_table *nt,
1367179220Ssam    const struct ieee80211_frame_min *wh)
1368179220Ssam{
1369179220Ssam	/* XXX 4-address frames? */
1370179220Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1371179220Ssam		return ieee80211_find_node_locked(nt, wh->i_addr1);
1372179220Ssam	if (IS_BCAST_PROBEREQ(wh))
1373179220Ssam		return NULL;		/* spam bcast probe req to all vap's */
1374179220Ssam	return ieee80211_find_node_locked(nt, wh->i_addr2);
1375179220Ssam}
1376179220Ssam
1377138568Ssam/*
1378138568Ssam * Locate the node for sender, track state, and then pass the
1379179220Ssam * (referenced) node up to the 802.11 layer for its use.  Note
1380179220Ssam * we can return NULL if the sender is not in the table.
1381138568Ssam */
1382138568Ssamstruct ieee80211_node *
1383138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1384138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1385138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1386138568Ssam#else
1387138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1388138568Ssam	const struct ieee80211_frame_min *wh)
1389138568Ssam#endif
1390138568Ssam{
1391138568Ssam	struct ieee80211_node_table *nt;
1392138568Ssam	struct ieee80211_node *ni;
1393138568Ssam
1394170530Ssam	nt = &ic->ic_sta;
1395138568Ssam	IEEE80211_NODE_LOCK(nt);
1396179220Ssam	ni = _find_rxnode(nt, wh);
1397138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1398138568Ssam
1399148863Ssam	return ni;
1400148863Ssam}
1401148863Ssam
1402148863Ssam/*
1403148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1404148863Ssam * key index as a hint to locate the node in the key
1405148863Ssam * mapping table.  If an entry is present at the key
1406148863Ssam * index we return it; otherwise do a normal lookup and
1407148863Ssam * update the mapping table if the station has a unicast
1408148863Ssam * key assigned to it.
1409148863Ssam */
1410148863Ssamstruct ieee80211_node *
1411148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1412148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1413148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1414148863Ssam	const char *func, int line)
1415148863Ssam#else
1416148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1417148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1418148863Ssam#endif
1419148863Ssam{
1420148863Ssam	struct ieee80211_node_table *nt;
1421148863Ssam	struct ieee80211_node *ni;
1422148863Ssam
1423170530Ssam	nt = &ic->ic_sta;
1424148863Ssam	IEEE80211_NODE_LOCK(nt);
1425148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1426148863Ssam		ni = nt->nt_keyixmap[keyix];
1427148863Ssam	else
1428148863Ssam		ni = NULL;
1429148863Ssam	if (ni == NULL) {
1430179220Ssam		ni = _find_rxnode(nt, wh);
1431178354Ssam		if (ni != NULL && nt->nt_keyixmap != NULL) {
1432148863Ssam			/*
1433148863Ssam			 * If the station has a unicast key cache slot
1434148863Ssam			 * assigned update the key->node mapping table.
1435148863Ssam			 */
1436148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1437148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1438148863Ssam			if (keyix < nt->nt_keyixmax &&
1439148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1440178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1441178354Ssam				    IEEE80211_MSG_NODE,
1442148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1443148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1444148863Ssam				    ieee80211_node_refcnt(ni)+1);
1445148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1446148863Ssam			}
1447148863Ssam		}
1448179220Ssam	} else {
1449179220Ssam		if (IS_BCAST_PROBEREQ(wh))
1450179220Ssam			ni = NULL;	/* spam bcast probe req to all vap's */
1451179220Ssam		else
1452179220Ssam			ieee80211_ref_node(ni);
1453179220Ssam	}
1454148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1455148863Ssam
1456148863Ssam	return ni;
1457148863Ssam}
1458179220Ssam#undef IS_BCAST_PROBEREQ
1459179220Ssam#undef IS_PROBEREQ
1460170530Ssam#undef IS_BAR
1461138568Ssam#undef IS_PSPOLL
1462138568Ssam#undef IS_CTL
1463138568Ssam
1464138568Ssam/*
1465127772Ssam * Return a reference to the appropriate node for sending
1466127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1467127772Ssam */
1468127772Ssamstruct ieee80211_node *
1469138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1470178354Ssamieee80211_find_txnode_debug(struct ieee80211vap *vap,
1471178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN],
1472138568Ssam	const char *func, int line)
1473138568Ssam#else
1474178354Ssamieee80211_find_txnode(struct ieee80211vap *vap,
1475178354Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
1476138568Ssam#endif
1477127772Ssam{
1478178354Ssam	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
1479127772Ssam	struct ieee80211_node *ni;
1480127772Ssam
1481127772Ssam	/*
1482127772Ssam	 * The destination address should be in the node table
1483148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1484148863Ssam	 * also optimize station mode operation, all frames go
1485148863Ssam	 * to the bss node.
1486127772Ssam	 */
1487127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1488138568Ssam	IEEE80211_NODE_LOCK(nt);
1489178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA ||
1490178354Ssam	    vap->iv_opmode == IEEE80211_M_WDS ||
1491178354Ssam	    IEEE80211_IS_MULTICAST(macaddr))
1492178354Ssam		ni = ieee80211_ref_node(vap->iv_bss);
1493158121Ssam	else {
1494178354Ssam		ni = ieee80211_find_node_locked(nt, macaddr);
1495178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1496158121Ssam		    (ni != NULL && ni->ni_associd == 0)) {
1497158121Ssam			/*
1498158121Ssam			 * Station is not associated; don't permit the
1499158121Ssam			 * data frame to be sent by returning NULL.  This
1500158121Ssam			 * is kinda a kludge but the least intrusive way
1501158121Ssam			 * to add this check into all drivers.
1502158121Ssam			 */
1503158121Ssam			ieee80211_unref_node(&ni);	/* NB: null's ni */
1504158121Ssam		}
1505158121Ssam	}
1506138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1507138568Ssam
1508138568Ssam	if (ni == NULL) {
1509178354Ssam		if (vap->iv_opmode == IEEE80211_M_IBSS ||
1510178354Ssam		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
1511140497Ssam			/*
1512140497Ssam			 * In adhoc mode cons up a node for the destination.
1513140497Ssam			 * Note that we need an additional reference for the
1514178354Ssam			 * caller to be consistent with
1515178354Ssam			 * ieee80211_find_node_locked.
1516140497Ssam			 */
1517178354Ssam			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
1518140497Ssam			if (ni != NULL)
1519140497Ssam				(void) ieee80211_ref_node(ni);
1520140497Ssam		} else {
1521178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
1522178354Ssam			    "no node, discard frame (%s)", __func__);
1523178354Ssam			vap->iv_stats.is_tx_nonode++;
1524127772Ssam		}
1525127772Ssam	}
1526127772Ssam	return ni;
1527127772Ssam}
1528127772Ssam
1529116742Ssamstatic void
1530138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1531116742Ssam{
1532138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1533119150Ssam
1534179641Ssam	/*
1535179641Ssam	 * NB: careful about referencing the vap as it may be
1536179641Ssam	 * gone if the last reference was held by a driver.
1537179641Ssam	 * We know the com will always be present so it's safe
1538179641Ssam	 * to use ni_ic below to reclaim resources.
1539179641Ssam	 */
1540179641Ssam#if 0
1541178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1542140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1543140766Ssam		ether_sprintf(ni->ni_macaddr),
1544138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1545179641Ssam#endif
1546179641Ssam	if (ni->ni_associd != 0) {
1547179641Ssam		struct ieee80211vap *vap = ni->ni_vap;
1548179641Ssam		if (vap->iv_aid_bitmap != NULL)
1549179641Ssam			IEEE80211_AID_CLR(vap, ni->ni_associd);
1550179641Ssam	}
1551138568Ssam	if (nt != NULL) {
1552138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1553138568Ssam		LIST_REMOVE(ni, ni_hash);
1554138568Ssam	}
1555179641Ssam	ni->ni_ic->ic_node_free(ni);
1556116742Ssam}
1557116742Ssam
1558116742Ssamvoid
1559138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1560138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1561138568Ssam#else
1562138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1563138568Ssam#endif
1564116742Ssam{
1565138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1566119150Ssam
1567138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1568178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1569140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1570138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1571138568Ssam#endif
1572148863Ssam	if (nt != NULL) {
1573148863Ssam		IEEE80211_NODE_LOCK(nt);
1574148863Ssam		if (ieee80211_node_dectestref(ni)) {
1575148863Ssam			/*
1576148863Ssam			 * Last reference, reclaim state.
1577148863Ssam			 */
1578138568Ssam			_ieee80211_free_node(ni);
1579148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1580148863Ssam		    nt->nt_keyixmap != NULL) {
1581148863Ssam			ieee80211_keyix keyix;
1582148863Ssam			/*
1583148863Ssam			 * Check for a last reference in the key mapping table.
1584148863Ssam			 */
1585148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1586148863Ssam			if (keyix < nt->nt_keyixmax &&
1587148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1588178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
1589178354Ssam				    IEEE80211_MSG_NODE,
1590148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1591148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1592148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1593148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1594148863Ssam				_ieee80211_free_node(ni);
1595148863Ssam			}
1596148863Ssam		}
1597148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1598148863Ssam	} else {
1599148863Ssam		if (ieee80211_node_dectestref(ni))
1600138568Ssam			_ieee80211_free_node(ni);
1601116742Ssam	}
1602116742Ssam}
1603116742Ssam
1604138568Ssam/*
1605148863Ssam * Reclaim a unicast key and clear any key cache state.
1606148863Ssam */
1607148863Ssamint
1608148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1609148863Ssam{
1610179641Ssam	struct ieee80211com *ic = ni->ni_ic;
1611179641Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1612148863Ssam	struct ieee80211_node *nikey;
1613148863Ssam	ieee80211_keyix keyix;
1614148863Ssam	int isowned, status;
1615148863Ssam
1616148863Ssam	/*
1617148863Ssam	 * NB: We must beware of LOR here; deleting the key
1618148863Ssam	 * can cause the crypto layer to block traffic updates
1619148863Ssam	 * which can generate a LOR against the node table lock;
1620148863Ssam	 * grab it here and stash the key index for our use below.
1621148863Ssam	 *
1622148863Ssam	 * Must also beware of recursion on the node table lock.
1623148863Ssam	 * When called from node_cleanup we may already have
1624148863Ssam	 * the node table lock held.  Unfortunately there's no
1625148863Ssam	 * way to separate out this path so we must do this
1626148863Ssam	 * conditionally.
1627148863Ssam	 */
1628148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1629148863Ssam	if (!isowned)
1630148863Ssam		IEEE80211_NODE_LOCK(nt);
1631179641Ssam	nikey = NULL;
1632179641Ssam	status = 1;		/* NB: success */
1633179641Ssam	if (!IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1634179641Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1635179641Ssam		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
1636179641Ssam		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1637179641Ssam			nikey = nt->nt_keyixmap[keyix];
1638179641Ssam			nt->nt_keyixmap[keyix] = NULL;;
1639179641Ssam		}
1640179641Ssam	}
1641148863Ssam	if (!isowned)
1642178354Ssam		IEEE80211_NODE_UNLOCK(nt);
1643148863Ssam
1644148863Ssam	if (nikey != NULL) {
1645148863Ssam		KASSERT(nikey == ni,
1646148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1647179641Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1648148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1649148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1650148863Ssam			ieee80211_node_refcnt(ni)-1);
1651148863Ssam		ieee80211_free_node(ni);
1652148863Ssam	}
1653148863Ssam	return status;
1654148863Ssam}
1655148863Ssam
1656148863Ssam/*
1657138568Ssam * Reclaim a node.  If this is the last reference count then
1658138568Ssam * do the normal free work.  Otherwise remove it from the node
1659138568Ssam * table and mark it gone by clearing the back-reference.
1660138568Ssam */
1661138568Ssamstatic void
1662138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1663116742Ssam{
1664148863Ssam	ieee80211_keyix keyix;
1665138568Ssam
1666148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1667148863Ssam
1668178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1669140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1670140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1671140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1672148863Ssam	/*
1673148863Ssam	 * Clear any entry in the unicast key mapping table.
1674148863Ssam	 * We need to do it here so rx lookups don't find it
1675148863Ssam	 * in the mapping table even if it's not in the hash
1676148863Ssam	 * table.  We cannot depend on the mapping table entry
1677148863Ssam	 * being cleared because the node may not be free'd.
1678148863Ssam	 */
1679148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1680148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1681148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1682178354Ssam		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1683148863Ssam			"%s: %p<%s> clear key map entry\n",
1684148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr));
1685148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1686148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1687148863Ssam	}
1688138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1689138568Ssam		/*
1690138568Ssam		 * Other references are present, just remove the
1691138568Ssam		 * node from the table so it cannot be found.  When
1692138568Ssam		 * the references are dropped storage will be
1693140753Ssam		 * reclaimed.
1694138568Ssam		 */
1695138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1696138568Ssam		LIST_REMOVE(ni, ni_hash);
1697138568Ssam		ni->ni_table = NULL;		/* clear reference */
1698138568Ssam	} else
1699138568Ssam		_ieee80211_free_node(ni);
1700138568Ssam}
1701138568Ssam
1702178354Ssam/*
1703178354Ssam * Reclaim a (bss) node.  Decrement the refcnt and reclaim
1704178354Ssam * the node if the only other reference to it is in the sta
1705178354Ssam * table.  This is effectively ieee80211_free_node followed
1706178354Ssam * by node_reclaim when the refcnt is 1 (after the free).
1707178354Ssam */
1708138568Ssamstatic void
1709178354Ssamieee80211_node_reclaim(struct ieee80211_node *ni)
1710138568Ssam{
1711178354Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1712116742Ssam
1713178354Ssam	KASSERT(nt != NULL, ("reclaim node not in table"));
1714138568Ssam
1715178354Ssam#ifdef IEEE80211_DEBUG_REFCNT
1716178354Ssam	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
1717178712Ssam		"%s %p<%s> refcnt %d\n", __func__, ni,
1718178354Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1719178354Ssam#endif
1720178354Ssam	IEEE80211_NODE_LOCK(nt);
1721178354Ssam	if (ieee80211_node_dectestref(ni)) {
1722178354Ssam		/*
1723178354Ssam		 * Last reference, reclaim state.
1724178354Ssam		 */
1725178354Ssam		_ieee80211_free_node(ni);
1726178354Ssam		nt = NULL;
1727178354Ssam	} else if (ieee80211_node_refcnt(ni) == 1 &&
1728178354Ssam	    nt->nt_keyixmap != NULL) {
1729178354Ssam		ieee80211_keyix keyix;
1730178354Ssam		/*
1731178354Ssam		 * Check for a last reference in the key mapping table.
1732178354Ssam		 */
1733178354Ssam		keyix = ni->ni_ucastkey.wk_rxkeyix;
1734178354Ssam		if (keyix < nt->nt_keyixmax &&
1735178354Ssam		    nt->nt_keyixmap[keyix] == ni) {
1736178354Ssam			IEEE80211_DPRINTF(ni->ni_vap,
1737178354Ssam			    IEEE80211_MSG_NODE,
1738178354Ssam			    "%s: %p<%s> clear key map entry", __func__,
1739178354Ssam			    ni, ether_sprintf(ni->ni_macaddr));
1740178354Ssam			nt->nt_keyixmap[keyix] = NULL;
1741178354Ssam			ieee80211_node_decref(ni); /* XXX needed? */
1742178354Ssam			_ieee80211_free_node(ni);
1743178354Ssam			nt = NULL;
1744178354Ssam		}
1745178354Ssam	}
1746178354Ssam	if (nt != NULL && ieee80211_node_refcnt(ni) == 1) {
1747178354Ssam		/*
1748178354Ssam		 * Last reference is in the sta table; complete
1749178354Ssam		 * the reclaim.  This handles bss nodes being
1750178354Ssam		 * recycled: the node has two references, one for
1751178354Ssam		 * iv_bss and one for the table.  After dropping
1752178354Ssam		 * the iv_bss ref above we need to reclaim the sta
1753178354Ssam		 * table reference.
1754178354Ssam		 */
1755178354Ssam		ieee80211_node_decref(ni);	/* NB: be pendantic */
1756178354Ssam		_ieee80211_free_node(ni);
1757178354Ssam	}
1758178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1759178354Ssam}
1760178354Ssam
1761178354Ssam/*
1762178354Ssam * Node table support.
1763178354Ssam */
1764178354Ssam
1765178354Ssamstatic void
1766178354Ssamieee80211_node_table_init(struct ieee80211com *ic,
1767178354Ssam	struct ieee80211_node_table *nt,
1768178354Ssam	const char *name, int inact, int keyixmax)
1769178354Ssam{
1770178354Ssam	struct ifnet *ifp = ic->ic_ifp;
1771178354Ssam
1772178354Ssam	nt->nt_ic = ic;
1773178354Ssam	IEEE80211_NODE_LOCK_INIT(nt, ifp->if_xname);
1774178354Ssam	IEEE80211_NODE_ITERATE_LOCK_INIT(nt, ifp->if_xname);
1775178354Ssam	TAILQ_INIT(&nt->nt_node);
1776178354Ssam	nt->nt_name = name;
1777178354Ssam	nt->nt_scangen = 1;
1778178354Ssam	nt->nt_inact_init = inact;
1779178354Ssam	nt->nt_keyixmax = keyixmax;
1780178354Ssam	if (nt->nt_keyixmax > 0) {
1781184210Sdes		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
1782184210Sdes			keyixmax * sizeof(struct ieee80211_node *),
1783178354Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
1784178354Ssam		if (nt->nt_keyixmap == NULL)
1785178354Ssam			if_printf(ic->ic_ifp,
1786178354Ssam			    "Cannot allocate key index map with %u entries\n",
1787178354Ssam			    keyixmax);
1788178354Ssam	} else
1789178354Ssam		nt->nt_keyixmap = NULL;
1790178354Ssam}
1791178354Ssam
1792178354Ssamstatic void
1793178354Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt,
1794178354Ssam	struct ieee80211vap *match)
1795178354Ssam{
1796178354Ssam	struct ieee80211_node *ni, *next;
1797178354Ssam
1798178354Ssam	IEEE80211_NODE_LOCK(nt);
1799178354Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
1800178354Ssam		if (match != NULL && ni->ni_vap != match)
1801178354Ssam			continue;
1802178354Ssam		/* XXX can this happen?  if so need's work */
1803138568Ssam		if (ni->ni_associd != 0) {
1804178354Ssam			struct ieee80211vap *vap = ni->ni_vap;
1805178354Ssam
1806178354Ssam			if (vap->iv_auth->ia_node_leave != NULL)
1807178354Ssam				vap->iv_auth->ia_node_leave(ni);
1808178354Ssam			if (vap->iv_aid_bitmap != NULL)
1809178354Ssam				IEEE80211_AID_CLR(vap, ni->ni_associd);
1810138568Ssam		}
1811178354Ssam		ni->ni_wdsvap = NULL;		/* clear reference */
1812138568Ssam		node_reclaim(nt, ni);
1813138568Ssam	}
1814178354Ssam	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
1815178354Ssam		/*
1816178354Ssam		 * Make a separate pass to clear references to this vap
1817178354Ssam		 * held by DWDS entries.  They will not be matched above
1818178354Ssam		 * because ni_vap will point to the ap vap but we still
1819178354Ssam		 * need to clear ni_wdsvap when the WDS vap is destroyed
1820178354Ssam		 * and/or reset.
1821178354Ssam		 */
1822178354Ssam		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
1823178354Ssam			if (ni->ni_wdsvap == match)
1824178354Ssam				ni->ni_wdsvap = NULL;
1825178354Ssam	}
1826178354Ssam	IEEE80211_NODE_UNLOCK(nt);
1827116742Ssam}
1828116742Ssam
1829178354Ssamstatic void
1830178354Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1831178354Ssam{
1832178354Ssam	ieee80211_node_table_reset(nt, NULL);
1833178354Ssam	if (nt->nt_keyixmap != NULL) {
1834178354Ssam#ifdef DIAGNOSTIC
1835178354Ssam		/* XXX verify all entries are NULL */
1836178354Ssam		int i;
1837178354Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
1838178354Ssam			if (nt->nt_keyixmap[i] != NULL)
1839178354Ssam				printf("%s: %s[%u] still active\n", __func__,
1840178354Ssam					nt->nt_name, i);
1841178354Ssam#endif
1842184210Sdes		FREE(nt->nt_keyixmap, M_80211_NODE);
1843178354Ssam		nt->nt_keyixmap = NULL;
1844178354Ssam	}
1845178354Ssam	IEEE80211_NODE_ITERATE_LOCK_DESTROY(nt);
1846178354Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1847178354Ssam}
1848178354Ssam
1849120483Ssam/*
1850138568Ssam * Timeout inactive stations and do related housekeeping.
1851138568Ssam * Note that we cannot hold the node lock while sending a
1852138568Ssam * frame as this would lead to a LOR.  Instead we use a
1853138568Ssam * generation number to mark nodes that we've scanned and
1854138568Ssam * drop the lock and restart a scan if we have to time out
1855138568Ssam * a node.  Since we are single-threaded by virtue of
1856120483Ssam * controlling the inactivity timer we can be sure this will
1857120483Ssam * process each node only once.
1858120483Ssam */
1859138568Ssamstatic void
1860178354Ssamieee80211_timeout_stations(struct ieee80211com *ic)
1861116742Ssam{
1862178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1863178354Ssam	struct ieee80211vap *vap;
1864120483Ssam	struct ieee80211_node *ni;
1865178354Ssam	int gen = 0;
1866116742Ssam
1867178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
1868154532Ssam	gen = ++nt->nt_scangen;
1869120483Ssamrestart:
1870138568Ssam	IEEE80211_NODE_LOCK(nt);
1871138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1872120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1873120483Ssam			continue;
1874120483Ssam		ni->ni_scangen = gen;
1875138568Ssam		/*
1876147788Ssam		 * Ignore entries for which have yet to receive an
1877147788Ssam		 * authentication frame.  These are transient and
1878147788Ssam		 * will be reclaimed when the last reference to them
1879147788Ssam		 * goes away (when frame xmits complete).
1880147788Ssam		 */
1881178354Ssam		vap = ni->ni_vap;
1882178354Ssam		/*
1883178354Ssam		 * Only process stations when in RUN state.  This
1884178354Ssam		 * insures, for example, that we don't timeout an
1885178354Ssam		 * inactive station during CAC.  Note that CSA state
1886178354Ssam		 * is actually handled in ieee80211_node_timeout as
1887178354Ssam		 * it applies to more than timeout processing.
1888178354Ssam		 */
1889178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
1890178354Ssam			continue;
1891178354Ssam		/* XXX can vap be NULL? */
1892178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
1893178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
1894148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1895147788Ssam			continue;
1896147788Ssam		/*
1897138568Ssam		 * Free fragment if not needed anymore
1898138568Ssam		 * (last fragment older than 1s).
1899178354Ssam		 * XXX doesn't belong here, move to node_age
1900138568Ssam		 */
1901138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1902138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1903138568Ssam			m_freem(ni->ni_rxfrag[0]);
1904138568Ssam			ni->ni_rxfrag[0] = NULL;
1905138568Ssam		}
1906172062Ssam		if (ni->ni_inact > 0)
1907172062Ssam			ni->ni_inact--;
1908140498Ssam		/*
1909140498Ssam		 * Special case ourself; we may be idle for extended periods
1910140498Ssam		 * of time and regardless reclaiming our state is wrong.
1911178354Ssam		 * XXX run ic_node_age
1912140498Ssam		 */
1913178354Ssam		if (ni == vap->iv_bss)
1914140498Ssam			continue;
1915178354Ssam		if (ni->ni_associd != 0 ||
1916178354Ssam		    (vap->iv_opmode == IEEE80211_M_IBSS ||
1917178354Ssam		     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
1918119150Ssam			/*
1919178354Ssam			 * Age/drain resources held by the station.
1920138568Ssam			 */
1921178354Ssam			ic->ic_node_age(ni);
1922138568Ssam			/*
1923138568Ssam			 * Probe the station before time it out.  We
1924138568Ssam			 * send a null data frame which may not be
1925138568Ssam			 * universally supported by drivers (need it
1926138568Ssam			 * for ps-poll support so it should be...).
1927170530Ssam			 *
1928170530Ssam			 * XXX don't probe the station unless we've
1929170530Ssam			 *     received a frame from them (and have
1930170530Ssam			 *     some idea of the rates they are capable
1931170530Ssam			 *     of); this will get fixed more properly
1932170530Ssam			 *     soon with better handling of the rate set.
1933138568Ssam			 */
1934178354Ssam			if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1935172062Ssam			    (0 < ni->ni_inact &&
1936178354Ssam			     ni->ni_inact <= vap->iv_inact_probe) &&
1937170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1938178354Ssam				IEEE80211_NOTE(vap,
1939148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1940148320Ssam				    ni, "%s",
1941148320Ssam				    "probe station due to inactivity");
1942148582Ssam				/*
1943148582Ssam				 * Grab a reference before unlocking the table
1944148582Ssam				 * so the node cannot be reclaimed before we
1945148582Ssam				 * send the frame. ieee80211_send_nulldata
1946148582Ssam				 * understands we've done this and reclaims the
1947148582Ssam				 * ref for us as needed.
1948148582Ssam				 */
1949148582Ssam				ieee80211_ref_node(ni);
1950138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1951148301Ssam				ieee80211_send_nulldata(ni);
1952138568Ssam				/* XXX stat? */
1953138568Ssam				goto restart;
1954138568Ssam			}
1955138568Ssam		}
1956178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
1957172062Ssam		    ni->ni_inact <= 0) {
1958178354Ssam			IEEE80211_NOTE(vap,
1959148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1960148320Ssam			    "station timed out due to inactivity "
1961148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1962138568Ssam			/*
1963138568Ssam			 * Send a deauthenticate frame and drop the station.
1964138568Ssam			 * This is somewhat complicated due to reference counts
1965138568Ssam			 * and locking.  At this point a station will typically
1966138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1967138568Ssam			 * will do a "free" of the node which will drop the
1968138568Ssam			 * reference count.  But in the meantime a reference
1969138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1970138568Ssam			 * of the node will happen either after the tx is
1971138568Ssam			 * completed or by ieee80211_node_leave.
1972120483Ssam			 *
1973138568Ssam			 * Separately we must drop the node lock before sending
1974170530Ssam			 * in case the driver takes a lock, as this can result
1975170530Ssam			 * in a LOR between the node lock and the driver lock.
1976119150Ssam			 */
1977172229Ssam			ieee80211_ref_node(ni);
1978138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1979138568Ssam			if (ni->ni_associd != 0) {
1980178354Ssam				IEEE80211_SEND_MGMT(ni,
1981138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1982138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1983138568Ssam			}
1984178354Ssam			ieee80211_node_leave(ni);
1985172229Ssam			ieee80211_free_node(ni);
1986178354Ssam			vap->iv_stats.is_node_timeout++;
1987120483Ssam			goto restart;
1988120483Ssam		}
1989116742Ssam	}
1990138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1991138568Ssam
1992178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
1993170530Ssam}
1994138568Ssam
1995178354Ssam/*
1996178354Ssam * Aggressively reclaim resources.  This should be used
1997178354Ssam * only in a critical situation to reclaim mbuf resources.
1998178354Ssam */
1999170530Ssamvoid
2000178354Ssamieee80211_drain(struct ieee80211com *ic)
2001178354Ssam{
2002178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2003178354Ssam	struct ieee80211vap *vap;
2004178354Ssam	struct ieee80211_node *ni;
2005178354Ssam
2006178354Ssam	IEEE80211_NODE_LOCK(nt);
2007178354Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2008178354Ssam		/*
2009178354Ssam		 * Ignore entries for which have yet to receive an
2010178354Ssam		 * authentication frame.  These are transient and
2011178354Ssam		 * will be reclaimed when the last reference to them
2012178354Ssam		 * goes away (when frame xmits complete).
2013178354Ssam		 */
2014178354Ssam		vap = ni->ni_vap;
2015178354Ssam		/*
2016178354Ssam		 * Only process stations when in RUN state.  This
2017178354Ssam		 * insures, for example, that we don't timeout an
2018178354Ssam		 * inactive station during CAC.  Note that CSA state
2019178354Ssam		 * is actually handled in ieee80211_node_timeout as
2020178354Ssam		 * it applies to more than timeout processing.
2021178354Ssam		 */
2022178354Ssam		if (vap->iv_state != IEEE80211_S_RUN)
2023178354Ssam			continue;
2024178354Ssam		/* XXX can vap be NULL? */
2025178354Ssam		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
2026178354Ssam		     vap->iv_opmode == IEEE80211_M_STA) &&
2027178354Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
2028178354Ssam			continue;
2029178354Ssam		/*
2030178354Ssam		 * Free fragments.
2031178354Ssam		 * XXX doesn't belong here, move to node_drain
2032178354Ssam		 */
2033178354Ssam		if (ni->ni_rxfrag[0] != NULL) {
2034178354Ssam			m_freem(ni->ni_rxfrag[0]);
2035178354Ssam			ni->ni_rxfrag[0] = NULL;
2036178354Ssam		}
2037178354Ssam		/*
2038178354Ssam		 * Drain resources held by the station.
2039178354Ssam		 */
2040178354Ssam		ic->ic_node_drain(ni);
2041178354Ssam	}
2042178354Ssam	IEEE80211_NODE_UNLOCK(nt);
2043178354Ssam}
2044178354Ssam
2045178354Ssam/*
2046178354Ssam * Per-ieee80211com inactivity timer callback.
2047178354Ssam */
2048178354Ssamvoid
2049170530Ssamieee80211_node_timeout(void *arg)
2050170530Ssam{
2051170530Ssam	struct ieee80211com *ic = arg;
2052170530Ssam
2053178354Ssam	/*
2054178354Ssam	 * Defer timeout processing if a channel switch is pending.
2055178354Ssam	 * We typically need to be mute so not doing things that
2056178354Ssam	 * might generate frames is good to handle in one place.
2057178354Ssam	 * Supressing the station timeout processing may extend the
2058178354Ssam	 * lifetime of inactive stations (by not decrementing their
2059178354Ssam	 * idle counters) but this should be ok unless the CSA is
2060178354Ssam	 * active for an unusually long time.
2061178354Ssam	 */
2062178354Ssam	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
2063178354Ssam		ieee80211_scan_timeout(ic);
2064178354Ssam		ieee80211_timeout_stations(ic);
2065170530Ssam
2066178354Ssam		IEEE80211_LOCK(ic);
2067178354Ssam		ieee80211_erp_timeout(ic);
2068178354Ssam		ieee80211_ht_timeout(ic);
2069178354Ssam		IEEE80211_UNLOCK(ic);
2070178354Ssam	}
2071170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
2072170530Ssam		ieee80211_node_timeout, ic);
2073116742Ssam}
2074116742Ssam
2075116742Ssamvoid
2076178354Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt,
2077178354Ssam	ieee80211_iter_func *f, void *arg)
2078116742Ssam{
2079116742Ssam	struct ieee80211_node *ni;
2080138568Ssam	u_int gen;
2081116742Ssam
2082178354Ssam	IEEE80211_NODE_ITERATE_LOCK(nt);
2083154532Ssam	gen = ++nt->nt_scangen;
2084138568Ssamrestart:
2085138568Ssam	IEEE80211_NODE_LOCK(nt);
2086138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2087138568Ssam		if (ni->ni_scangen != gen) {
2088138568Ssam			ni->ni_scangen = gen;
2089138568Ssam			(void) ieee80211_ref_node(ni);
2090138568Ssam			IEEE80211_NODE_UNLOCK(nt);
2091138568Ssam			(*f)(arg, ni);
2092138568Ssam			ieee80211_free_node(ni);
2093138568Ssam			goto restart;
2094138568Ssam		}
2095138568Ssam	}
2096138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2097138568Ssam
2098178354Ssam	IEEE80211_NODE_ITERATE_UNLOCK(nt);
2099116742Ssam}
2100138568Ssam
2101138568Ssamvoid
2102138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
2103138568Ssam{
2104138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
2105138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
2106138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
2107138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
2108138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
2109138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
2110138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
2111167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
2112167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
2113167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
2114138568Ssam		ni->ni_rxfragstamp);
2115170530Ssam	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
2116178354Ssam		ni->ni_rstamp, node_getrssi(ni), ni->ni_noise,
2117170530Ssam		ni->ni_intval, ni->ni_capinfo);
2118138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2119138568Ssam		ether_sprintf(ni->ni_bssid),
2120138568Ssam		ni->ni_esslen, ni->ni_essid,
2121138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2122178354Ssam	printf("\tinact %u txrate %u\n",
2123178354Ssam		ni->ni_inact, ni->ni_txrate);
2124170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
2125170530Ssam		ni->ni_htcap, ni->ni_htparam,
2126170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
2127170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
2128170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
2129138568Ssam}
2130138568Ssam
2131138568Ssamvoid
2132138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2133138568Ssam{
2134138568Ssam	ieee80211_iterate_nodes(nt,
2135138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2136138568Ssam}
2137138568Ssam
2138179642Ssamstatic void
2139179642Ssamieee80211_notify_erp_locked(struct ieee80211com *ic)
2140172211Ssam{
2141178354Ssam	struct ieee80211vap *vap;
2142178354Ssam
2143178354Ssam	IEEE80211_LOCK_ASSERT(ic);
2144178354Ssam
2145178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2146178354Ssam		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2147178354Ssam			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
2148172211Ssam}
2149172211Ssam
2150179642Ssamvoid
2151179642Ssamieee80211_notify_erp(struct ieee80211com *ic)
2152179642Ssam{
2153179642Ssam	IEEE80211_LOCK(ic);
2154179642Ssam	ieee80211_notify_erp_locked(ic);
2155179642Ssam	IEEE80211_UNLOCK(ic);
2156179642Ssam}
2157179642Ssam
2158138568Ssam/*
2159138568Ssam * Handle a station joining an 11g network.
2160138568Ssam */
2161138568Ssamstatic void
2162178354Ssamieee80211_node_join_11g(struct ieee80211_node *ni)
2163138568Ssam{
2164178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2165138568Ssam
2166173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2167173273Ssam
2168138568Ssam	/*
2169138568Ssam	 * Station isn't capable of short slot time.  Bump
2170138568Ssam	 * the count of long slot time stations and disable
2171138568Ssam	 * use of short slot time.  Note that the actual switch
2172138568Ssam	 * over to long slot time use may not occur until the
2173138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2174138568Ssam	 */
2175138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2176138568Ssam		ic->ic_longslotsta++;
2177178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2178172211Ssam		    "station needs long slot time, count %d",
2179172211Ssam		    ic->ic_longslotsta);
2180138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2181170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
2182170530Ssam			/*
2183170530Ssam			 * Don't force slot time when switched to turbo
2184170530Ssam			 * mode as non-ERP stations won't be present; this
2185170530Ssam			 * need only be done when on the normal G channel.
2186170530Ssam			 */
2187170530Ssam			ieee80211_set_shortslottime(ic, 0);
2188170530Ssam		}
2189138568Ssam	}
2190138568Ssam	/*
2191138568Ssam	 * If the new station is not an ERP station
2192138568Ssam	 * then bump the counter and enable protection
2193138568Ssam	 * if configured.
2194138568Ssam	 */
2195178354Ssam	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
2196138568Ssam		ic->ic_nonerpsta++;
2197178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2198172211Ssam		    "station is !ERP, %d non-ERP stations associated",
2199172211Ssam		    ic->ic_nonerpsta);
2200138568Ssam		/*
2201138568Ssam		 * If station does not support short preamble
2202138568Ssam		 * then we must enable use of Barker preamble.
2203138568Ssam		 */
2204138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2205178354Ssam			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2206172211Ssam			    "%s", "station needs long preamble");
2207138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2208138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2209138568Ssam		}
2210172211Ssam		/*
2211178354Ssam		 * If protection is configured and this is the first
2212178354Ssam		 * indication we should use protection, enable it.
2213172211Ssam		 */
2214172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
2215172211Ssam		    ic->ic_nonerpsta == 1 &&
2216172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2217178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2218172211Ssam			    "%s: enable use of protection\n", __func__);
2219172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2220179642Ssam			ieee80211_notify_erp_locked(ic);
2221172211Ssam		}
2222138568Ssam	} else
2223138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2224138568Ssam}
2225138568Ssam
2226138568Ssamvoid
2227178354Ssamieee80211_node_join(struct ieee80211_node *ni, int resp)
2228138568Ssam{
2229178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2230178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2231138568Ssam	int newassoc;
2232138568Ssam
2233138568Ssam	if (ni->ni_associd == 0) {
2234170530Ssam		uint16_t aid;
2235138568Ssam
2236178354Ssam		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
2237138568Ssam		/*
2238138568Ssam		 * It would be good to search the bitmap
2239138568Ssam		 * more efficiently, but this will do for now.
2240138568Ssam		 */
2241178354Ssam		for (aid = 1; aid < vap->iv_max_aid; aid++) {
2242178354Ssam			if (!IEEE80211_AID_ISSET(vap, aid))
2243138568Ssam				break;
2244138568Ssam		}
2245178354Ssam		if (aid >= vap->iv_max_aid) {
2246179640Ssam			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
2247178354Ssam			ieee80211_node_leave(ni);
2248138568Ssam			return;
2249138568Ssam		}
2250138568Ssam		ni->ni_associd = aid | 0xc000;
2251173273Ssam		ni->ni_jointime = time_uptime;
2252178354Ssam		IEEE80211_LOCK(ic);
2253178354Ssam		IEEE80211_AID_SET(vap, ni->ni_associd);
2254178354Ssam		vap->iv_sta_assoc++;
2255138568Ssam		ic->ic_sta_assoc++;
2256173273Ssam
2257173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2258173273Ssam			ieee80211_ht_node_join(ni);
2259170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2260170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2261178354Ssam			ieee80211_node_join_11g(ni);
2262173273Ssam		IEEE80211_UNLOCK(ic);
2263173273Ssam
2264173273Ssam		newassoc = 1;
2265138568Ssam	} else
2266138568Ssam		newassoc = 0;
2267138568Ssam
2268178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2269183256Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
2270139523Ssam	    IEEE80211_NODE_AID(ni),
2271139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2272139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2273139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2274170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2275170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
2276182834Ssam		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
2277173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
2278183255Ssam	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
2279183255Ssam	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
2280183256Ssam	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
2281178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
2282170530Ssam		", fast-frames" : "",
2283178354Ssam	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
2284170530Ssam		", turbo" : ""
2285139523Ssam	);
2286138568Ssam
2287183251Ssam	node_setuptxparms(ni);
2288138568Ssam	/* give driver a chance to setup state like ni_txrate */
2289139524Ssam	if (ic->ic_newassoc != NULL)
2290148307Ssam		ic->ic_newassoc(ni, newassoc);
2291178354Ssam	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
2292138568Ssam	/* tell the authenticator about new station */
2293178354Ssam	if (vap->iv_auth->ia_node_join != NULL)
2294178354Ssam		vap->iv_auth->ia_node_join(ni);
2295178354Ssam	ieee80211_notify_node_join(ni,
2296173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2297138568Ssam}
2298138568Ssam
2299172211Ssamstatic void
2300172211Ssamdisable_protection(struct ieee80211com *ic)
2301172211Ssam{
2302172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
2303172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
2304172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
2305172211Ssam	   ic->ic_flags_ext));
2306172211Ssam
2307172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
2308172211Ssam	/* XXX verify mode? */
2309172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2310172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2311172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2312172211Ssam	}
2313179642Ssam	ieee80211_notify_erp_locked(ic);
2314172211Ssam}
2315172211Ssam
2316138568Ssam/*
2317138568Ssam * Handle a station leaving an 11g network.
2318138568Ssam */
2319138568Ssamstatic void
2320178354Ssamieee80211_node_leave_11g(struct ieee80211_node *ni)
2321138568Ssam{
2322178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2323138568Ssam
2324173273Ssam	IEEE80211_LOCK_ASSERT(ic);
2325173273Ssam
2326170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
2327178354Ssam	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
2328178354Ssam	      ic->ic_bsschan->ic_flags));
2329138568Ssam
2330138568Ssam	/*
2331138568Ssam	 * If a long slot station do the slot time bookkeeping.
2332138568Ssam	 */
2333138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2334138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2335138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2336138568Ssam		ic->ic_longslotsta--;
2337178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2338172211Ssam		    "long slot time station leaves, count now %d",
2339172211Ssam		    ic->ic_longslotsta);
2340138568Ssam		if (ic->ic_longslotsta == 0) {
2341138568Ssam			/*
2342138568Ssam			 * Re-enable use of short slot time if supported
2343138568Ssam			 * and not operating in IBSS mode (per spec).
2344138568Ssam			 */
2345138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2346138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2347178354Ssam				IEEE80211_DPRINTF(ni->ni_vap,
2348178354Ssam				    IEEE80211_MSG_ASSOC,
2349138568Ssam				    "%s: re-enable use of short slot time\n",
2350138568Ssam				    __func__);
2351138568Ssam				ieee80211_set_shortslottime(ic, 1);
2352138568Ssam			}
2353138568Ssam		}
2354138568Ssam	}
2355138568Ssam	/*
2356138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2357138568Ssam	 */
2358138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2359138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2360138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2361138568Ssam		ic->ic_nonerpsta--;
2362178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
2363172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
2364172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
2365172211Ssam			" (non-ERP sta present)" : "");
2366172211Ssam		if (ic->ic_nonerpsta == 0 &&
2367172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
2368178354Ssam			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
2369138568Ssam				"%s: disable use of protection\n", __func__);
2370172211Ssam			disable_protection(ic);
2371138568Ssam		}
2372138568Ssam	}
2373138568Ssam}
2374138568Ssam
2375138568Ssam/*
2376172211Ssam * Time out presence of an overlapping bss with non-ERP
2377172211Ssam * stations.  When operating in hostap mode we listen for
2378172211Ssam * beacons from other stations and if we identify a non-ERP
2379172211Ssam * station is present we enable protection.  To identify
2380172211Ssam * when all non-ERP stations are gone we time out this
2381172211Ssam * condition.
2382172211Ssam */
2383172211Ssamstatic void
2384172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
2385172211Ssam{
2386172211Ssam
2387172211Ssam	IEEE80211_LOCK_ASSERT(ic);
2388172211Ssam
2389172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
2390172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
2391178354Ssam#if 0
2392178354Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2393178354Ssam		    "%s", "age out non-ERP sta present on channel");
2394178354Ssam#endif
2395172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
2396172211Ssam		if (ic->ic_nonerpsta == 0)
2397172211Ssam			disable_protection(ic);
2398172211Ssam	}
2399172211Ssam}
2400172211Ssam
2401172211Ssam/*
2402138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2403138568Ssam * when operating as an ap.
2404138568Ssam */
2405138568Ssamvoid
2406178354Ssamieee80211_node_leave(struct ieee80211_node *ni)
2407138568Ssam{
2408178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2409178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2410140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2411138568Ssam
2412178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
2413172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
2414138568Ssam
2415178354Ssam	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
2416178354Ssam		("unexpected operating mode %u", vap->iv_opmode));
2417138568Ssam	/*
2418138568Ssam	 * If node wasn't previously associated all
2419138568Ssam	 * we need to do is reclaim the reference.
2420138568Ssam	 */
2421138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2422138568Ssam	if (ni->ni_associd == 0)
2423138568Ssam		goto done;
2424138568Ssam	/*
2425138568Ssam	 * Tell the authenticator the station is leaving.
2426138568Ssam	 * Note that we must do this before yanking the
2427138568Ssam	 * association id as the authenticator uses the
2428138568Ssam	 * associd to locate it's state block.
2429138568Ssam	 */
2430178354Ssam	if (vap->iv_auth->ia_node_leave != NULL)
2431178354Ssam		vap->iv_auth->ia_node_leave(ni);
2432173273Ssam
2433173273Ssam	IEEE80211_LOCK(ic);
2434178354Ssam	IEEE80211_AID_CLR(vap, ni->ni_associd);
2435138568Ssam	ni->ni_associd = 0;
2436178354Ssam	vap->iv_sta_assoc--;
2437138568Ssam	ic->ic_sta_assoc--;
2438138568Ssam
2439173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
2440173273Ssam		ieee80211_ht_node_leave(ni);
2441170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2442170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
2443178354Ssam		ieee80211_node_leave_11g(ni);
2444173273Ssam	IEEE80211_UNLOCK(ic);
2445138568Ssam	/*
2446138568Ssam	 * Cleanup station state.  In particular clear various
2447138568Ssam	 * state that might otherwise be reused if the node
2448138568Ssam	 * is reused before the reference count goes to zero
2449138568Ssam	 * (and memory is reclaimed).
2450138568Ssam	 */
2451178354Ssam	ieee80211_sta_leave(ni);
2452138568Ssamdone:
2453140499Ssam	/*
2454140499Ssam	 * Remove the node from any table it's recorded in and
2455140499Ssam	 * drop the caller's reference.  Removal from the table
2456140499Ssam	 * is important to insure the node is not reprocessed
2457140499Ssam	 * for inactivity.
2458140499Ssam	 */
2459140499Ssam	if (nt != NULL) {
2460140499Ssam		IEEE80211_NODE_LOCK(nt);
2461140499Ssam		node_reclaim(nt, ni);
2462140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2463140499Ssam	} else
2464140499Ssam		ieee80211_free_node(ni);
2465138568Ssam}
2466138568Ssam
2467178354Ssamstruct rssiinfo {
2468178354Ssam	struct ieee80211vap *vap;
2469178354Ssam	int	rssi_samples;
2470178354Ssam	uint32_t rssi_total;
2471178354Ssam};
2472178354Ssam
2473178354Ssamstatic void
2474178354Ssamget_hostap_rssi(void *arg, struct ieee80211_node *ni)
2475178354Ssam{
2476178354Ssam	struct rssiinfo *info = arg;
2477178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2478178354Ssam	int8_t rssi;
2479178354Ssam
2480178354Ssam	if (info->vap != vap)
2481178354Ssam		return;
2482178354Ssam	/* only associated stations */
2483178354Ssam	if (ni->ni_associd == 0)
2484178354Ssam		return;
2485178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2486178354Ssam	if (rssi != 0) {
2487178354Ssam		info->rssi_samples++;
2488178354Ssam		info->rssi_total += rssi;
2489178354Ssam	}
2490178354Ssam}
2491178354Ssam
2492178354Ssamstatic void
2493178354Ssamget_adhoc_rssi(void *arg, struct ieee80211_node *ni)
2494178354Ssam{
2495178354Ssam	struct rssiinfo *info = arg;
2496178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
2497178354Ssam	int8_t rssi;
2498178354Ssam
2499178354Ssam	if (info->vap != vap)
2500178354Ssam		return;
2501178354Ssam	/* only neighbors */
2502178354Ssam	/* XXX check bssid */
2503178354Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
2504178354Ssam		return;
2505178354Ssam	rssi = vap->iv_ic->ic_node_getrssi(ni);
2506178354Ssam	if (rssi != 0) {
2507178354Ssam		info->rssi_samples++;
2508178354Ssam		info->rssi_total += rssi;
2509178354Ssam	}
2510178354Ssam}
2511178354Ssam
2512170530Ssamint8_t
2513178354Ssamieee80211_getrssi(struct ieee80211vap *vap)
2514138568Ssam{
2515138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2516178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2517178354Ssam	struct rssiinfo info;
2518138568Ssam
2519178354Ssam	info.rssi_total = 0;
2520178354Ssam	info.rssi_samples = 0;
2521178354Ssam	info.vap = vap;
2522178354Ssam	switch (vap->iv_opmode) {
2523138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2524138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2525178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_adhoc_rssi, &info);
2526178354Ssam		break;
2527138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2528178354Ssam		ieee80211_iterate_nodes(&ic->ic_sta, get_hostap_rssi, &info);
2529138568Ssam		break;
2530138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2531138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2532138568Ssam	default:
2533178354Ssam		if (vap->iv_bss != NULL)
2534178354Ssam			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
2535178354Ssam		info.rssi_samples = 1;
2536138568Ssam		break;
2537138568Ssam	}
2538178354Ssam	return info.rssi_total / NZ(info.rssi_samples);
2539138568Ssam#undef NZ
2540138568Ssam}
2541138568Ssam
2542170530Ssamvoid
2543178354Ssamieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
2544138568Ssam{
2545138568Ssam
2546178354Ssam	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
2547170530Ssam		return;
2548178354Ssam	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
2549170530Ssam	/* for non-station mode return avg'd rssi accounting */
2550178354Ssam	if (vap->iv_opmode != IEEE80211_M_STA)
2551178354Ssam		*rssi = ieee80211_getrssi(vap);
2552138568Ssam}
2553