ieee80211_node.c revision 173866
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3170360Ssam * Copyright (c) 2002-2007 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 173866 2007-11-23 06:23:12Z sam $");
29116742Ssam
30116742Ssam#include <sys/param.h>
31116742Ssam#include <sys/systm.h>
32116742Ssam#include <sys/mbuf.h>
33116742Ssam#include <sys/malloc.h>
34116742Ssam#include <sys/kernel.h>
35138568Ssam
36116742Ssam#include <sys/socket.h>
37116742Ssam
38116742Ssam#include <net/if.h>
39116742Ssam#include <net/if_media.h>
40116742Ssam#include <net/ethernet.h>
41116742Ssam
42116742Ssam#include <net80211/ieee80211_var.h>
43116742Ssam
44116742Ssam#include <net/bpf.h>
45116742Ssam
46147221Ssam/*
47147221Ssam * Association id's are managed with a bit vector.
48147221Ssam */
49147221Ssam#define	IEEE80211_AID_SET(b, w) \
50147221Ssam	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
51147221Ssam#define	IEEE80211_AID_CLR(b, w) \
52147221Ssam	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
53147221Ssam#define	IEEE80211_AID_ISSET(b, w) \
54147221Ssam	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
55147221Ssam
56159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
57159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
58159139Sdds#else
59159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
60159139Sdds#endif
61159139Sdds
62170530Ssamstatic int ieee80211_sta_join1(struct ieee80211_node *);
63170530Ssam
64138568Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
65138568Ssamstatic void node_cleanup(struct ieee80211_node *);
66138568Ssamstatic void node_free(struct ieee80211_node *);
67170530Ssamstatic int8_t node_getrssi(const struct ieee80211_node *);
68170530Ssamstatic void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
69116742Ssam
70138568Ssamstatic void ieee80211_setup_node(struct ieee80211_node_table *,
71170530Ssam		struct ieee80211_node *, const uint8_t *);
72138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
73120104Ssam
74138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
75148863Ssam	struct ieee80211_node_table *nt, const char *name,
76170530Ssam	int inact, int keymaxix);
77170530Ssamstatic void ieee80211_node_table_reset(struct ieee80211_node_table *);
78138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
79172211Ssamstatic void ieee80211_erp_timeout(struct ieee80211com *);
80138568Ssam
81127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
82120481Ssam
83116742Ssamvoid
84138568Ssamieee80211_node_attach(struct ieee80211com *ic)
85116742Ssam{
86116742Ssam
87138568Ssam	ic->ic_node_alloc = node_alloc;
88138568Ssam	ic->ic_node_free = node_free;
89138568Ssam	ic->ic_node_cleanup = node_cleanup;
90138568Ssam	ic->ic_node_getrssi = node_getrssi;
91170530Ssam	ic->ic_node_getsignal = node_getsignal;
92138568Ssam
93138568Ssam	/* default station inactivity timer setings */
94138568Ssam	ic->ic_inact_init = IEEE80211_INACT_INIT;
95138568Ssam	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
96138568Ssam	ic->ic_inact_run = IEEE80211_INACT_RUN;
97138568Ssam	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
98138568Ssam
99170530Ssam	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
100170530Ssam
101148863Ssam	/* NB: driver should override */
102148863Ssam	ic->ic_max_aid = IEEE80211_AID_DEF;
103172062Ssam
104172062Ssam	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
105148863Ssam}
106148863Ssam
107148863Ssamvoid
108148863Ssamieee80211_node_lateattach(struct ieee80211com *ic)
109148863Ssam{
110148863Ssam	struct ieee80211_rsnparms *rsn;
111148863Ssam
112148863Ssam	if (ic->ic_max_aid > IEEE80211_AID_MAX)
113138568Ssam		ic->ic_max_aid = IEEE80211_AID_MAX;
114170530Ssam	MALLOC(ic->ic_aid_bitmap, uint32_t *,
115170530Ssam		howmany(ic->ic_max_aid, 32) * sizeof(uint32_t),
116170530Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
117138568Ssam	if (ic->ic_aid_bitmap == NULL) {
118138568Ssam		/* XXX no way to recover */
119138568Ssam		printf("%s: no memory for AID bitmap!\n", __func__);
120138568Ssam		ic->ic_max_aid = 0;
121138568Ssam	}
122138568Ssam
123148863Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
124170530Ssam		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix);
125118887Ssam
126148863Ssam	ieee80211_reset_bss(ic);
127138568Ssam	/*
128138568Ssam	 * Setup "global settings" in the bss node so that
129138568Ssam	 * each new station automatically inherits them.
130138568Ssam	 */
131148863Ssam	rsn = &ic->ic_bss->ni_rsn;
132138568Ssam	/* WEP, TKIP, and AES-CCM are always supported */
133138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
134138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
135138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
136138568Ssam	if (ic->ic_caps & IEEE80211_C_AES)
137138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
138138568Ssam	if (ic->ic_caps & IEEE80211_C_CKIP)
139138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
140138568Ssam	/*
141138568Ssam	 * Default unicast cipher to WEP for 802.1x use.  If
142138568Ssam	 * WPA is enabled the management code will set these
143138568Ssam	 * values to reflect.
144138568Ssam	 */
145138568Ssam	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
146138568Ssam	rsn->rsn_ucastkeylen = 104 / NBBY;
147138568Ssam	/*
148138568Ssam	 * WPA says the multicast cipher is the lowest unicast
149138568Ssam	 * cipher supported.  But we skip WEP which would
150138568Ssam	 * otherwise be used based on this criteria.
151138568Ssam	 */
152138568Ssam	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
153138568Ssam	rsn->rsn_mcastkeylen = 128 / NBBY;
154138568Ssam
155138568Ssam	/*
156138568Ssam	 * We support both WPA-PSK and 802.1x; the one used
157138568Ssam	 * is determined by the authentication mode and the
158138568Ssam	 * setting of the PSK state.
159138568Ssam	 */
160138568Ssam	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
161138568Ssam	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
162138568Ssam
163148863Ssam	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
164116742Ssam}
165116742Ssam
166116742Ssamvoid
167138568Ssamieee80211_node_detach(struct ieee80211com *ic)
168116742Ssam{
169116742Ssam
170138568Ssam	if (ic->ic_bss != NULL) {
171138568Ssam		ieee80211_free_node(ic->ic_bss);
172138568Ssam		ic->ic_bss = NULL;
173138568Ssam	}
174140753Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
175138568Ssam	if (ic->ic_aid_bitmap != NULL) {
176170530Ssam		FREE(ic->ic_aid_bitmap, M_80211_NODE);
177138568Ssam		ic->ic_aid_bitmap = NULL;
178138568Ssam	}
179116742Ssam}
180116742Ssam
181138568Ssam/*
182138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
183138568Ssam */
184138568Ssam
185138568Ssamvoid
186148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
187138568Ssam{
188148302Ssam	struct ieee80211com *ic = ni->ni_ic;
189148302Ssam
190138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
191139528Ssam	ni->ni_inact_reload = ic->ic_inact_run;
192172062Ssam	ni->ni_inact = ni->ni_inact_reload;
193138568Ssam}
194138568Ssam
195138568Ssamvoid
196148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
197138568Ssam{
198172063Ssam	struct ieee80211com *ic = ni->ni_ic;
199172063Ssam
200138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
201172062Ssam	ni->ni_inact_reload = ic->ic_inact_auth;
202172062Ssam	if (ni->ni_inact > ni->ni_inact_reload)
203172062Ssam		ni->ni_inact = ni->ni_inact_reload;
204138568Ssam}
205138568Ssam
206116742Ssam/*
207138568Ssam * Set/change the channel.  The rate set is also updated as
208138568Ssam * to insure a consistent view by drivers.
209138568Ssam */
210153351Ssamstatic void
211170530Ssamieee80211_node_set_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
212138568Ssam{
213170530Ssam	struct ieee80211_channel *chan = ic->ic_bsschan;
214170530Ssam
215170530Ssam#if 0
216170530Ssam	KASSERT(chan != IEEE80211_CHAN_ANYC, ("bss channel not setup"));
217170530Ssam#else
218153351Ssam	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
219153351Ssam		chan = ic->ic_curchan;
220170530Ssam#endif
221138568Ssam	ni->ni_chan = chan;
222170530Ssam	if (IEEE80211_IS_CHAN_HT(chan)) {
223170530Ssam		/*
224170530Ssam		 * XXX Gotta be careful here; the rate set returned by
225170530Ssam		 * ieee80211_get_suprates is actually any HT rate
226170530Ssam		 * set so blindly copying it will be bad.  We must
227170530Ssam		 * install the legacy rate est in ni_rates and the
228170530Ssam		 * HT rate set in ni_htrates.
229170530Ssam		 */
230170530Ssam		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
231170530Ssam	}
232165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
233138568Ssam}
234138568Ssam
235138568Ssam/*
236156358Ssam * Probe the curent channel, if allowed, while scanning.
237156358Ssam * If the channel is not marked passive-only then send
238156358Ssam * a probe request immediately.  Otherwise mark state and
239156358Ssam * listen for beacons on the channel; if we receive something
240156358Ssam * then we'll transmit a probe request.
241156358Ssam */
242156358Ssamvoid
243156358Ssamieee80211_probe_curchan(struct ieee80211com *ic, int force)
244156358Ssam{
245156358Ssam	struct ifnet *ifp = ic->ic_ifp;
246156358Ssam
247156358Ssam	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
248156358Ssam		/*
249156358Ssam		 * XXX send both broadcast+directed probe request
250156358Ssam		 */
251156358Ssam		ieee80211_send_probereq(ic->ic_bss,
252156358Ssam			ic->ic_myaddr, ifp->if_broadcastaddr,
253156358Ssam			ifp->if_broadcastaddr,
254170530Ssam			ic->ic_des_ssid[0].ssid, ic->ic_des_ssid[0].len,
255156358Ssam			ic->ic_opt_ie, ic->ic_opt_ie_len);
256156358Ssam	} else
257156358Ssam		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
258156358Ssam}
259156358Ssam
260141658Ssamstatic __inline void
261141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
262141658Ssam{
263141658Ssam	/* propagate useful state */
264141658Ssam	nbss->ni_authmode = obss->ni_authmode;
265141658Ssam	nbss->ni_txpower = obss->ni_txpower;
266141658Ssam	nbss->ni_vlan = obss->ni_vlan;
267141658Ssam	nbss->ni_rsn = obss->ni_rsn;
268141658Ssam	/* XXX statistics? */
269141658Ssam}
270141658Ssam
271116742Ssamvoid
272116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
273116742Ssam{
274140753Ssam	struct ieee80211_node_table *nt;
275116742Ssam	struct ieee80211_node *ni;
276116742Ssam
277138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
278138568Ssam		"%s: creating ibss\n", __func__);
279138568Ssam
280138568Ssam	/*
281138568Ssam	 * Create the station/neighbor table.  Note that for adhoc
282138568Ssam	 * mode we make the initial inactivity timer longer since
283138568Ssam	 * we create nodes only through discovery and they typically
284138568Ssam	 * are long-lived associations.
285138568Ssam	 */
286140753Ssam	nt = &ic->ic_sta;
287140753Ssam	IEEE80211_NODE_LOCK(nt);
288140753Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
289140753Ssam		nt->nt_name = "station";
290140753Ssam		nt->nt_inact_init = ic->ic_inact_init;
291140753Ssam	} else {
292140753Ssam		nt->nt_name = "neighbor";
293140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
294140753Ssam	}
295140753Ssam	IEEE80211_NODE_UNLOCK(nt);
296140753Ssam
297148863Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
298140753Ssam	if (ni == NULL) {
299140753Ssam		/* XXX recovery? */
300138568Ssam		return;
301138568Ssam	}
302116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
303170530Ssam	ni->ni_esslen = ic->ic_des_ssid[0].len;
304170530Ssam	memcpy(ni->ni_essid, ic->ic_des_ssid[0].ssid, ni->ni_esslen);
305170530Ssam	if (ic->ic_bss != NULL)
306170530Ssam		copy_bss(ni, ic->ic_bss);
307148843Ssam	ni->ni_intval = ic->ic_bintval;
308138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
309116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
310116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
311116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
312116742Ssam		ni->ni_fhindex = 1;
313116742Ssam	}
314138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
315138568Ssam		ic->ic_flags |= IEEE80211_F_SIBSS;
316138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
317143300Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
318143300Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
319167282Ssam		else {
320167282Ssam			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
321167282Ssam			/* clear group bit, add local bit */
322167282Ssam			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
323167282Ssam		}
324153403Ssam	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
325153403Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
326153403Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
327153403Ssam		else
328153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
329138568Ssam	}
330138568Ssam	/*
331138568Ssam	 * Fix the channel and related attributes.
332138568Ssam	 */
333170530Ssam	ic->ic_bsschan = chan;
334170530Ssam	ieee80211_node_set_chan(ic, ni);
335170530Ssam	ic->ic_curmode = ieee80211_chan2mode(chan);
336138568Ssam	/*
337138568Ssam	 * Do mode-specific rate setup.
338138568Ssam	 */
339170530Ssam	if (IEEE80211_IS_CHAN_FULL(chan)) {
340170530Ssam		if (IEEE80211_IS_CHAN_ANYG(chan)) {
341170530Ssam			/*
342170530Ssam			 * Use a mixed 11b/11g rate set.
343170530Ssam			 */
344170530Ssam			ieee80211_set11gbasicrates(&ni->ni_rates,
345170530Ssam				IEEE80211_MODE_11G);
346170530Ssam		} else if (IEEE80211_IS_CHAN_B(chan)) {
347170530Ssam			/*
348170530Ssam			 * Force pure 11b rate set.
349170530Ssam			 */
350170530Ssam			ieee80211_set11gbasicrates(&ni->ni_rates,
351170530Ssam				IEEE80211_MODE_11B);
352170530Ssam		}
353170530Ssam	}
354138568Ssam
355170530Ssam	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
356116742Ssam}
357116742Ssam
358170530Ssam/*
359170530Ssam * Reset bss state on transition to the INIT state.
360170530Ssam * Clear any stations from the table (they have been
361170530Ssam * deauth'd) and reset the bss node (clears key, rate
362170530Ssam * etc. state).
363170530Ssam */
364138568Ssamvoid
365138568Ssamieee80211_reset_bss(struct ieee80211com *ic)
366138568Ssam{
367138568Ssam	struct ieee80211_node *ni, *obss;
368138568Ssam
369170530Ssam	callout_drain(&ic->ic_inact);
370140753Ssam	ieee80211_node_table_reset(&ic->ic_sta);
371170530Ssam	ieee80211_reset_erp(ic);
372140753Ssam
373170530Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
374138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
375138568Ssam	obss = ic->ic_bss;
376138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);
377141658Ssam	if (obss != NULL) {
378141658Ssam		copy_bss(ni, obss);
379148843Ssam		ni->ni_intval = ic->ic_bintval;
380138568Ssam		ieee80211_free_node(obss);
381141658Ssam	}
382138568Ssam}
383138568Ssam
384170530Ssamstatic int
385170530Ssammatch_ssid(const struct ieee80211_node *ni,
386170530Ssam	int nssid, const struct ieee80211_scan_ssid ssids[])
387170530Ssam{
388170530Ssam	int i;
389148432Ssam
390170530Ssam	for (i = 0; i < nssid; i++) {
391170530Ssam		if (ni->ni_esslen == ssids[i].len &&
392170530Ssam		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
393170530Ssam			return 1;
394170530Ssam	}
395170530Ssam	return 0;
396170530Ssam}
397170530Ssam
398170530Ssam/*
399170530Ssam * Test a node for suitability/compatibility.
400170530Ssam */
401127767Ssamstatic int
402170530Ssamcheck_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
403127767Ssam{
404170530Ssam        uint8_t rate;
405170530Ssam
406170530Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
407170530Ssam		return 0;
408170530Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
409170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
410170530Ssam			return 0;
411170530Ssam	} else {
412170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
413170530Ssam			return 0;
414170530Ssam	}
415170530Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
416170530Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
417170530Ssam			return 0;
418170530Ssam	} else {
419170530Ssam		/* XXX does this mean privacy is supported or required? */
420170530Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
421170530Ssam			return 0;
422170530Ssam	}
423170530Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
424170530Ssam	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
425170530Ssam	if (rate & IEEE80211_RATE_BASIC)
426170530Ssam		return 0;
427170530Ssam	if (ic->ic_des_nssid != 0 &&
428170530Ssam	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
429170530Ssam		return 0;
430170530Ssam	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
431170530Ssam	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
432170530Ssam		return 0;
433170530Ssam	return 1;
434170530Ssam}
435170530Ssam
436170530Ssam#ifdef IEEE80211_DEBUG
437170530Ssam/*
438170530Ssam * Display node suitability/compatibility.
439170530Ssam */
440170530Ssamstatic void
441170530Ssamcheck_bss_debug(struct ieee80211com *ic, struct ieee80211_node *ni)
442170530Ssam{
443170530Ssam        uint8_t rate;
444127767Ssam        int fail;
445127767Ssam
446127767Ssam	fail = 0;
447127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
448127767Ssam		fail |= 0x01;
449127767Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
450127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
451127767Ssam			fail |= 0x02;
452127767Ssam	} else {
453127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
454127767Ssam			fail |= 0x02;
455127767Ssam	}
456138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
457127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
458127767Ssam			fail |= 0x04;
459127767Ssam	} else {
460127767Ssam		/* XXX does this mean privacy is supported or required? */
461127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
462127767Ssam			fail |= 0x04;
463127767Ssam	}
464167442Ssam	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
465165887Ssam	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
466127767Ssam	if (rate & IEEE80211_RATE_BASIC)
467127767Ssam		fail |= 0x08;
468170530Ssam	if (ic->ic_des_nssid != 0 &&
469170530Ssam	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
470127767Ssam		fail |= 0x10;
471127767Ssam	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
472127767Ssam	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
473127767Ssam		fail |= 0x20;
474127767Ssam
475170530Ssam	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
476170530Ssam	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
477170530Ssam	printf(" %3d%c",
478170530Ssam	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
479170530Ssam	printf(" %+4d", ni->ni_rssi);
480170530Ssam	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
481170530Ssam	    fail & 0x08 ? '!' : ' ');
482170530Ssam	printf(" %4s%c",
483170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
484170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
485170530Ssam	    "????",
486170530Ssam	    fail & 0x02 ? '!' : ' ');
487170530Ssam	printf(" %3s%c ",
488170530Ssam	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
489170530Ssam	    fail & 0x04 ? '!' : ' ');
490170530Ssam	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
491170530Ssam	printf("%s\n", fail & 0x10 ? "!" : "");
492138568Ssam}
493170530Ssam#endif /* IEEE80211_DEBUG */
494138568Ssam
495138568Ssam/*
496138568Ssam * Handle 802.11 ad hoc network merge.  The
497138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
498138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
499138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
500138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
501138568Ssam * sends beacons with the greatest TSF timestamp.
502138568Ssam *
503138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
504138568Ssam *
505138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
506138568Ssam */
507138568Ssamint
508148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
509138568Ssam{
510148306Ssam	struct ieee80211com *ic = ni->ni_ic;
511138568Ssam
512140453Ssam	if (ni == ic->ic_bss ||
513140453Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
514138568Ssam		/* unchanged, nothing to do */
515138568Ssam		return 0;
516138568Ssam	}
517170530Ssam	if (!check_bss(ic, ni)) {
518170530Ssam		/* capabilities mismatch */
519138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
520138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
521170530Ssam#ifdef IEEE80211_DEBUG
522170530Ssam		if (ieee80211_msg_assoc(ic))
523170530Ssam			check_bss_debug(ic, ni);
524170530Ssam#endif
525138568Ssam		ic->ic_stats.is_ibss_capmismatch++;
526138568Ssam		return 0;
527138568Ssam	}
528138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
529138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
530138568Ssam		ether_sprintf(ni->ni_bssid),
531138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
532138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
533138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
534138568Ssam	);
535170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
536138568Ssam}
537138568Ssam
538138568Ssam/*
539173273Ssam * Change the bss channel.
540173273Ssam */
541173273Ssamvoid
542173273Ssamieee80211_setbsschan(struct ieee80211com *ic, struct ieee80211_channel *c)
543173273Ssam{
544173273Ssam	ic->ic_bsschan = c;
545173273Ssam	ic->ic_curchan = ic->ic_bsschan;
546173273Ssam	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
547173273Ssam	ic->ic_set_channel(ic);
548173273Ssam}
549173273Ssam
550173273Ssam/*
551138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
552138568Ssam * be passed in with a held reference.
553138568Ssam */
554170530Ssamstatic int
555170530Ssamieee80211_sta_join1(struct ieee80211_node *selbs)
556138568Ssam{
557170530Ssam	struct ieee80211com *ic = selbs->ni_ic;
558138568Ssam	struct ieee80211_node *obss;
559170530Ssam	int canreassoc;
560138568Ssam
561116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
562140753Ssam		struct ieee80211_node_table *nt;
563138568Ssam		/*
564140753Ssam		 * Fillin the neighbor table; it will already
565139521Ssam		 * exist if we are simply switching mastership.
566140753Ssam		 * XXX ic_sta always setup so this is unnecessary?
567127772Ssam		 */
568140753Ssam		nt = &ic->ic_sta;
569140753Ssam		IEEE80211_NODE_LOCK(nt);
570140753Ssam		nt->nt_name = "neighbor";
571140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
572140753Ssam		IEEE80211_NODE_UNLOCK(nt);
573138568Ssam	}
574138568Ssam
575138568Ssam	/*
576138568Ssam	 * Committed to selbs, setup state.
577138568Ssam	 */
578138568Ssam	obss = ic->ic_bss;
579170530Ssam	/*
580170530Ssam	 * Check if old+new node have the same address in which
581170530Ssam	 * case we can reassociate when operating in sta mode.
582170530Ssam	 */
583170530Ssam	canreassoc = (obss != NULL &&
584170530Ssam		ic->ic_state == IEEE80211_S_RUN &&
585170530Ssam		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
586140753Ssam	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
587153352Ssam	if (obss != NULL) {
588153352Ssam		copy_bss(selbs, obss);
589138568Ssam		ieee80211_free_node(obss);
590153352Ssam	}
591165887Ssam
592138568Ssam	/*
593165887Ssam	 * Delete unusable rates; we've already checked
594165887Ssam	 * that the negotiated rate set is acceptable.
595165887Ssam	 */
596167442Ssam	ieee80211_fix_rate(ic->ic_bss, &ic->ic_bss->ni_rates,
597167442Ssam		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
598165887Ssam
599173273Ssam	ieee80211_setbsschan(ic, selbs->ni_chan);
600165887Ssam	/*
601138568Ssam	 * Set the erp state (mostly the slot time) to deal with
602138568Ssam	 * the auto-select case; this should be redundant if the
603138568Ssam	 * mode is locked.
604138568Ssam	 */
605138568Ssam	ieee80211_reset_erp(ic);
606138568Ssam	ieee80211_wme_initparams(ic);
607140753Ssam
608170530Ssam	if (ic->ic_opmode == IEEE80211_M_STA) {
609170530Ssam		if (canreassoc) {
610170530Ssam			/* Reassociate */
611170530Ssam			ieee80211_new_state(ic, IEEE80211_S_ASSOC, 1);
612170530Ssam		} else {
613170530Ssam			/*
614170530Ssam			 * Act as if we received a DEAUTH frame in case we
615170530Ssam			 * are invoked from the RUN state.  This will cause
616170530Ssam			 * us to try to re-authenticate if we are operating
617170530Ssam			 * as a station.
618170530Ssam			 */
619170530Ssam			ieee80211_new_state(ic, IEEE80211_S_AUTH,
620170530Ssam				IEEE80211_FC0_SUBTYPE_DEAUTH);
621170530Ssam		}
622170530Ssam	} else
623117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
624138568Ssam	return 1;
625116742Ssam}
626116742Ssam
627170530Ssamint
628170530Ssamieee80211_sta_join(struct ieee80211com *ic,
629170530Ssam	const struct ieee80211_scan_entry *se)
630170530Ssam{
631170530Ssam	struct ieee80211_node *ni;
632170530Ssam
633170530Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, se->se_macaddr);
634170530Ssam	if (ni == NULL) {
635170530Ssam		/* XXX msg */
636170530Ssam		return 0;
637170530Ssam	}
638170530Ssam	/*
639170530Ssam	 * Expand scan state into node's format.
640170530Ssam	 * XXX may not need all this stuff
641170530Ssam	 */
642170530Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
643170530Ssam	ni->ni_esslen = se->se_ssid[1];
644170530Ssam	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
645170530Ssam	ni->ni_rstamp = se->se_rstamp;
646170530Ssam	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
647170530Ssam	ni->ni_intval = se->se_intval;
648170530Ssam	ni->ni_capinfo = se->se_capinfo;
649170530Ssam	ni->ni_chan = se->se_chan;
650170530Ssam	ni->ni_timoff = se->se_timoff;
651170530Ssam	ni->ni_fhdwell = se->se_fhdwell;
652170530Ssam	ni->ni_fhindex = se->se_fhindex;
653170530Ssam	ni->ni_erp = se->se_erp;
654170530Ssam	ni->ni_rssi = se->se_rssi;
655170530Ssam	ni->ni_noise = se->se_noise;
656173864Ssam	if (se->se_htcap_ie != NULL) {
657173273Ssam		ieee80211_saveie(&ni->ni_htcap_ie, se->se_htcap_ie);
658173864Ssam		ieee80211_parse_htcap(ni, ni->ni_htcap_ie);
659173864Ssam	}
660170530Ssam	if (se->se_wpa_ie != NULL)
661170530Ssam		ieee80211_saveie(&ni->ni_wpa_ie, se->se_wpa_ie);
662170530Ssam	if (se->se_rsn_ie != NULL)
663170530Ssam		ieee80211_saveie(&ni->ni_rsn_ie, se->se_rsn_ie);
664170530Ssam	if (se->se_wme_ie != NULL)
665170530Ssam		ieee80211_saveie(&ni->ni_wme_ie, se->se_wme_ie);
666170530Ssam	if (se->se_ath_ie != NULL)
667170530Ssam		ieee80211_saveath(ni, se->se_ath_ie);
668170530Ssam
669170530Ssam	ic->ic_dtim_period = se->se_dtimperiod;
670170530Ssam	ic->ic_dtim_count = 0;
671170530Ssam
672170530Ssam	/* NB: must be after ni_chan is setup */
673170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
674170530Ssam		IEEE80211_F_DOSORT);
675170530Ssam
676170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
677170530Ssam}
678170530Ssam
679138568Ssam/*
680138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
681138568Ssam * be passed in with a held reference.
682138568Ssam */
683138568Ssamvoid
684138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
685138568Ssam{
686138568Ssam	ic->ic_node_cleanup(ni);
687138568Ssam	ieee80211_notify_node_leave(ic, ni);
688138568Ssam}
689138568Ssam
690116742Ssamstatic struct ieee80211_node *
691138568Ssamnode_alloc(struct ieee80211_node_table *nt)
692116742Ssam{
693127768Ssam	struct ieee80211_node *ni;
694138568Ssam
695127768Ssam	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
696127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
697127768Ssam	return ni;
698116742Ssam}
699116742Ssam
700138568Ssam/*
701138568Ssam * Reclaim any resources in a node and reset any critical
702138568Ssam * state.  Typically nodes are free'd immediately after,
703138568Ssam * but in some cases the storage may be reused so we need
704138568Ssam * to insure consistent state (should probably fix that).
705138568Ssam */
706116742Ssamstatic void
707138568Ssamnode_cleanup(struct ieee80211_node *ni)
708116742Ssam{
709138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
710138568Ssam	struct ieee80211com *ic = ni->ni_ic;
711170530Ssam	int i;
712138568Ssam
713138568Ssam	/* NB: preserve ni_table */
714138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
715172228Ssam		if (ic->ic_opmode != IEEE80211_M_STA)
716172228Ssam			ic->ic_ps_sta--;
717138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
718138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
719138568Ssam		    "[%s] power save mode off, %u sta's in ps mode\n",
720138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
721138568Ssam	}
722147788Ssam	/*
723173273Ssam	 * Cleanup any HT-related state.
724173273Ssam	 */
725173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
726173273Ssam		ieee80211_ht_node_cleanup(ni);
727173273Ssam	/*
728147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
729147788Ssam	 * has happened.  This is probably not needed as the node
730147788Ssam	 * should always be removed from the table so not found but
731147788Ssam	 * do it just in case.
732147788Ssam	 */
733147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
734138568Ssam
735138568Ssam	/*
736138568Ssam	 * Drain power save queue and, if needed, clear TIM.
737138568Ssam	 */
738170530Ssam	if (ieee80211_node_saveq_drain(ni) != 0 && ic->ic_set_tim != NULL)
739148304Ssam		ic->ic_set_tim(ni, 0);
740138568Ssam
741138568Ssam	ni->ni_associd = 0;
742138568Ssam	if (ni->ni_challenge != NULL) {
743170530Ssam		FREE(ni->ni_challenge, M_80211_NODE);
744138568Ssam		ni->ni_challenge = NULL;
745138568Ssam	}
746138568Ssam	/*
747138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
748138568Ssam	 * reusable during a re-auth/re-assoc state transition.
749138568Ssam	 * If we remove these data they will not be recreated
750138568Ssam	 * because they come from a probe-response or beacon frame
751138568Ssam	 * which cannot be expected prior to the association-response.
752138568Ssam	 * This should not be an issue when operating in other modes
753138568Ssam	 * as stations leaving always go through a full state transition
754138568Ssam	 * which will rebuild this state.
755138568Ssam	 *
756138568Ssam	 * XXX does this leave us open to inheriting old state?
757138568Ssam	 */
758138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
759138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
760138568Ssam			m_freem(ni->ni_rxfrag[i]);
761138568Ssam			ni->ni_rxfrag[i] = NULL;
762138568Ssam		}
763148863Ssam	/*
764148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
765148863Ssam	 */
766148863Ssam	ieee80211_node_delucastkey(ni);
767138568Ssam#undef N
768116742Ssam}
769116742Ssam
770116742Ssamstatic void
771138568Ssamnode_free(struct ieee80211_node *ni)
772116742Ssam{
773138568Ssam	struct ieee80211com *ic = ni->ni_ic;
774138568Ssam
775138568Ssam	ic->ic_node_cleanup(ni);
776138568Ssam	if (ni->ni_wpa_ie != NULL)
777170530Ssam		FREE(ni->ni_wpa_ie, M_80211_NODE);
778170530Ssam	if (ni->ni_rsn_ie != NULL)
779170530Ssam		FREE(ni->ni_rsn_ie, M_80211_NODE);
780138568Ssam	if (ni->ni_wme_ie != NULL)
781170530Ssam		FREE(ni->ni_wme_ie, M_80211_NODE);
782170530Ssam	if (ni->ni_ath_ie != NULL)
783170530Ssam		FREE(ni->ni_ath_ie, M_80211_NODE);
784138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
785138568Ssam	FREE(ni, M_80211_NODE);
786116742Ssam}
787116742Ssam
788170530Ssamstatic int8_t
789138568Ssamnode_getrssi(const struct ieee80211_node *ni)
790120104Ssam{
791120104Ssam	return ni->ni_rssi;
792120104Ssam}
793120104Ssam
794116742Ssamstatic void
795170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
796170530Ssam{
797170530Ssam	*rssi = ni->ni_rssi;
798170530Ssam	*noise = ni->ni_noise;
799170530Ssam}
800170530Ssam
801170530Ssamstatic void
802138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt,
803170530Ssam	struct ieee80211_node *ni, const uint8_t *macaddr)
804116742Ssam{
805138568Ssam	struct ieee80211com *ic = nt->nt_ic;
806116742Ssam	int hash;
807116742Ssam
808138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
809140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
810138568Ssam		ether_sprintf(macaddr), nt->nt_name);
811138568Ssam
812116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
813116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
814138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
815138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
816138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
817138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
818138568Ssam	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
819139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
820139528Ssam	ni->ni_inact = ni->ni_inact_reload;
821170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
822138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
823138568Ssam
824138568Ssam	IEEE80211_NODE_LOCK(nt);
825138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
826138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
827138568Ssam	ni->ni_table = nt;
828138568Ssam	ni->ni_ic = ic;
829138568Ssam	IEEE80211_NODE_UNLOCK(nt);
830116742Ssam}
831116742Ssam
832116742Ssamstruct ieee80211_node *
833170530Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
834116742Ssam{
835138568Ssam	struct ieee80211com *ic = nt->nt_ic;
836138568Ssam	struct ieee80211_node *ni;
837138568Ssam
838138568Ssam	ni = ic->ic_node_alloc(nt);
839116742Ssam	if (ni != NULL)
840138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
841127769Ssam	else
842127769Ssam		ic->ic_stats.is_rx_nodealloc++;
843116742Ssam	return ni;
844116742Ssam}
845116742Ssam
846148777Ssam/*
847148777Ssam * Craft a temporary node suitable for sending a management frame
848148777Ssam * to the specified station.  We craft only as much state as we
849148777Ssam * need to do the work since the node will be immediately reclaimed
850148777Ssam * once the send completes.
851148777Ssam */
852116742Ssamstruct ieee80211_node *
853170530Ssamieee80211_tmp_node(struct ieee80211com *ic, const uint8_t *macaddr)
854148777Ssam{
855148777Ssam	struct ieee80211_node *ni;
856148777Ssam
857148777Ssam	ni = ic->ic_node_alloc(&ic->ic_sta);
858148777Ssam	if (ni != NULL) {
859148777Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
860148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
861148777Ssam
862148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
863148777Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
864148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
865148777Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
866148777Ssam		/* NB: required by ieee80211_fix_rate */
867170530Ssam		ieee80211_node_set_chan(ic, ni);
868148777Ssam		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
869148777Ssam			IEEE80211_KEYIX_NONE);
870148777Ssam		/* XXX optimize away */
871148777Ssam		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
872148777Ssam
873148777Ssam		ni->ni_table = NULL;		/* NB: pedantic */
874148777Ssam		ni->ni_ic = ic;
875148777Ssam	} else {
876148777Ssam		/* XXX msg */
877148777Ssam		ic->ic_stats.is_rx_nodealloc++;
878148777Ssam	}
879148777Ssam	return ni;
880148777Ssam}
881148777Ssam
882148777Ssamstruct ieee80211_node *
883170530Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const uint8_t *macaddr)
884116742Ssam{
885138568Ssam	struct ieee80211com *ic = nt->nt_ic;
886138568Ssam	struct ieee80211_node *ni;
887138568Ssam
888138568Ssam	ni = ic->ic_node_alloc(nt);
889116742Ssam	if (ni != NULL) {
890138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
891127770Ssam		/*
892127770Ssam		 * Inherit from ic_bss.
893127770Ssam		 */
894138568Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
895138568Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
896138568Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
897127770Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
898170530Ssam		ieee80211_node_set_chan(ic, ni);
899138568Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
900127770Ssam	} else
901127770Ssam		ic->ic_stats.is_rx_nodealloc++;
902116742Ssam	return ni;
903116742Ssam}
904116742Ssam
905127772Ssamstatic struct ieee80211_node *
906138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
907138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
908170530Ssam	const uint8_t *macaddr, const char *func, int line)
909138568Ssam#else
910138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt,
911170530Ssam	const uint8_t *macaddr)
912138568Ssam#endif
913116742Ssam{
914116742Ssam	struct ieee80211_node *ni;
915116742Ssam	int hash;
916116742Ssam
917138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
918127772Ssam
919116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
920138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
921116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
922138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
923138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
924138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
925140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
926140766Ssam			    func, line,
927140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
928140766Ssam			    ieee80211_node_refcnt(ni));
929138568Ssam#endif
930127772Ssam			return ni;
931116742Ssam		}
932116742Ssam	}
933127772Ssam	return NULL;
934127772Ssam}
935138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
936138568Ssam#define	_ieee80211_find_node(nt, mac) \
937138568Ssam	_ieee80211_find_node_debug(nt, mac, func, line)
938138568Ssam#endif
939127772Ssam
940127772Ssamstruct ieee80211_node *
941138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
942138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
943170530Ssam	const uint8_t *macaddr, const char *func, int line)
944138568Ssam#else
945170530Ssamieee80211_find_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
946138568Ssam#endif
947127772Ssam{
948127772Ssam	struct ieee80211_node *ni;
949127772Ssam
950138568Ssam	IEEE80211_NODE_LOCK(nt);
951138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
952138568Ssam	IEEE80211_NODE_UNLOCK(nt);
953116742Ssam	return ni;
954116742Ssam}
955116742Ssam
956116742Ssam/*
957138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
958138568Ssam * Note that for the driver's benefit we we treat this like
959138568Ssam * an association so the driver has an opportunity to setup
960138568Ssam * it's private state.
961138568Ssam */
962138568Ssamstruct ieee80211_node *
963138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
964170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
965138568Ssam{
966138568Ssam	struct ieee80211com *ic = nt->nt_ic;
967138568Ssam	struct ieee80211_node *ni;
968138568Ssam
969153073Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
970153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
971138568Ssam	ni = ieee80211_dup_bss(nt, macaddr);
972138568Ssam	if (ni != NULL) {
973138568Ssam		/* XXX no rate negotiation; just dup */
974138568Ssam		ni->ni_rates = ic->ic_bss->ni_rates;
975139524Ssam		if (ic->ic_newassoc != NULL)
976148307Ssam			ic->ic_newassoc(ni, 1);
977153404Ssam		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
978153404Ssam			/*
979170530Ssam			 * In adhoc demo mode there are no management
980170530Ssam			 * frames to use to discover neighbor capabilities,
981170530Ssam			 * so blindly propagate the local configuration
982170530Ssam			 * so we can do interesting things (e.g. use
983170530Ssam			 * WME to disable ACK's).
984153404Ssam			 */
985153404Ssam			if (ic->ic_flags & IEEE80211_F_WME)
986153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
987170530Ssam			if (ic->ic_flags & IEEE80211_F_FF)
988170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
989153404Ssam		}
990170530Ssam		/* XXX not right for 802.1x/WPA */
991170530Ssam		ieee80211_node_authorize(ni);
992138568Ssam	}
993138568Ssam	return ni;
994138568Ssam}
995138568Ssam
996148936Ssamvoid
997153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
998153073Ssam	const struct ieee80211_frame *wh,
999153073Ssam	const struct ieee80211_scanparams *sp)
1000153073Ssam{
1001153073Ssam	ni->ni_esslen = sp->ssid[1];
1002153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1003153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1004153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1005153073Ssam	ni->ni_intval = sp->bintval;
1006153073Ssam	ni->ni_capinfo = sp->capinfo;
1007153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1008153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1009153073Ssam	ni->ni_fhindex = sp->fhindex;
1010153073Ssam	ni->ni_erp = sp->erp;
1011153073Ssam	ni->ni_timoff = sp->timoff;
1012153073Ssam	if (sp->wme != NULL)
1013153073Ssam		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1014153073Ssam	if (sp->wpa != NULL)
1015153073Ssam		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1016170530Ssam	if (sp->rsn != NULL)
1017170530Ssam		ieee80211_saveie(&ni->ni_rsn_ie, sp->rsn);
1018170530Ssam	if (sp->ath != NULL)
1019170530Ssam		ieee80211_saveath(ni, sp->ath);
1020153073Ssam
1021153073Ssam	/* NB: must be after ni_chan is setup */
1022165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1023165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1024165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1025153073Ssam}
1026153073Ssam
1027148936Ssam/*
1028148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1029148936Ssam * or probe response frame.  Note that for the driver's
1030148936Ssam * benefit we we treat this like an association so the
1031148936Ssam * driver has an opportunity to setup it's private state.
1032148936Ssam */
1033148936Ssamstruct ieee80211_node *
1034148936Ssamieee80211_add_neighbor(struct ieee80211com *ic,
1035148936Ssam	const struct ieee80211_frame *wh,
1036148936Ssam	const struct ieee80211_scanparams *sp)
1037148936Ssam{
1038148936Ssam	struct ieee80211_node *ni;
1039148936Ssam
1040153073Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1041153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1042148936Ssam	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1043148936Ssam	if (ni != NULL) {
1044153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1045148936Ssam		if (ic->ic_newassoc != NULL)
1046148936Ssam			ic->ic_newassoc(ni, 1);
1047148936Ssam		/* XXX not right for 802.1x/WPA */
1048148936Ssam		ieee80211_node_authorize(ni);
1049148936Ssam	}
1050148936Ssam	return ni;
1051148936Ssam}
1052148936Ssam
1053148863Ssam#define	IS_CTL(wh) \
1054148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1055148863Ssam#define	IS_PSPOLL(wh) \
1056148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1057170530Ssam#define	IS_BAR(wh) \
1058170530Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1059170530Ssam
1060138568Ssam/*
1061138568Ssam * Locate the node for sender, track state, and then pass the
1062138568Ssam * (referenced) node up to the 802.11 layer for its use.  We
1063138568Ssam * are required to pass some node so we fall back to ic_bss
1064138568Ssam * when this frame is from an unknown sender.  The 802.11 layer
1065138568Ssam * knows this means the sender wasn't in the node table and
1066138568Ssam * acts accordingly.
1067138568Ssam */
1068138568Ssamstruct ieee80211_node *
1069138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1070138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1071138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1072138568Ssam#else
1073138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1074138568Ssam	const struct ieee80211_frame_min *wh)
1075138568Ssam#endif
1076138568Ssam{
1077138568Ssam	struct ieee80211_node_table *nt;
1078138568Ssam	struct ieee80211_node *ni;
1079138568Ssam
1080138568Ssam	/* XXX check ic_bss first in station mode */
1081138568Ssam	/* XXX 4-address frames? */
1082170530Ssam	nt = &ic->ic_sta;
1083138568Ssam	IEEE80211_NODE_LOCK(nt);
1084170530Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1085138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr1);
1086138568Ssam	else
1087138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr2);
1088148863Ssam	if (ni == NULL)
1089148863Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1090138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1091138568Ssam
1092148863Ssam	return ni;
1093148863Ssam}
1094148863Ssam
1095148863Ssam/*
1096148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1097148863Ssam * key index as a hint to locate the node in the key
1098148863Ssam * mapping table.  If an entry is present at the key
1099148863Ssam * index we return it; otherwise do a normal lookup and
1100148863Ssam * update the mapping table if the station has a unicast
1101148863Ssam * key assigned to it.
1102148863Ssam */
1103148863Ssamstruct ieee80211_node *
1104148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1105148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1106148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1107148863Ssam	const char *func, int line)
1108148863Ssam#else
1109148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1110148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1111148863Ssam#endif
1112148863Ssam{
1113148863Ssam	struct ieee80211_node_table *nt;
1114148863Ssam	struct ieee80211_node *ni;
1115148863Ssam
1116170530Ssam	nt = &ic->ic_sta;
1117148863Ssam	IEEE80211_NODE_LOCK(nt);
1118148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1119148863Ssam		ni = nt->nt_keyixmap[keyix];
1120148863Ssam	else
1121148863Ssam		ni = NULL;
1122148863Ssam	if (ni == NULL) {
1123170530Ssam		if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1124148863Ssam			ni = _ieee80211_find_node(nt, wh->i_addr1);
1125148863Ssam		else
1126148863Ssam			ni = _ieee80211_find_node(nt, wh->i_addr2);
1127148863Ssam		if (ni == NULL)
1128148863Ssam			ni = ieee80211_ref_node(ic->ic_bss);
1129148863Ssam		if (nt->nt_keyixmap != NULL) {
1130148863Ssam			/*
1131148863Ssam			 * If the station has a unicast key cache slot
1132148863Ssam			 * assigned update the key->node mapping table.
1133148863Ssam			 */
1134148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1135148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1136148863Ssam			if (keyix < nt->nt_keyixmax &&
1137148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1138148863Ssam				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1139148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1140148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1141148863Ssam				    ieee80211_node_refcnt(ni)+1);
1142148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1143148863Ssam			}
1144148863Ssam		}
1145170530Ssam	} else
1146148863Ssam		ieee80211_ref_node(ni);
1147148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1148148863Ssam
1149148863Ssam	return ni;
1150148863Ssam}
1151170530Ssam#undef IS_BAR
1152138568Ssam#undef IS_PSPOLL
1153138568Ssam#undef IS_CTL
1154138568Ssam
1155138568Ssam/*
1156127772Ssam * Return a reference to the appropriate node for sending
1157127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1158127772Ssam */
1159127772Ssamstruct ieee80211_node *
1160138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1161170530Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const uint8_t *macaddr,
1162138568Ssam	const char *func, int line)
1163138568Ssam#else
1164170530Ssamieee80211_find_txnode(struct ieee80211com *ic, const uint8_t *macaddr)
1165138568Ssam#endif
1166127772Ssam{
1167140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1168127772Ssam	struct ieee80211_node *ni;
1169127772Ssam
1170127772Ssam	/*
1171127772Ssam	 * The destination address should be in the node table
1172148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1173148863Ssam	 * also optimize station mode operation, all frames go
1174148863Ssam	 * to the bss node.
1175127772Ssam	 */
1176127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1177138568Ssam	IEEE80211_NODE_LOCK(nt);
1178148863Ssam	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1179148863Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1180158121Ssam	else {
1181148863Ssam		ni = _ieee80211_find_node(nt, macaddr);
1182158121Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1183158121Ssam		    (ni != NULL && ni->ni_associd == 0)) {
1184158121Ssam			/*
1185158121Ssam			 * Station is not associated; don't permit the
1186158121Ssam			 * data frame to be sent by returning NULL.  This
1187158121Ssam			 * is kinda a kludge but the least intrusive way
1188158121Ssam			 * to add this check into all drivers.
1189158121Ssam			 */
1190158121Ssam			ieee80211_unref_node(&ni);	/* NB: null's ni */
1191158121Ssam		}
1192158121Ssam	}
1193138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1194138568Ssam
1195138568Ssam	if (ni == NULL) {
1196138568Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1197140497Ssam		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
1198140497Ssam			/*
1199140497Ssam			 * In adhoc mode cons up a node for the destination.
1200140497Ssam			 * Note that we need an additional reference for the
1201140497Ssam			 * caller to be consistent with _ieee80211_find_node.
1202140497Ssam			 */
1203138568Ssam			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1204140497Ssam			if (ni != NULL)
1205140497Ssam				(void) ieee80211_ref_node(ni);
1206140497Ssam		} else {
1207138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1208138568Ssam				"[%s] no node, discard frame (%s)\n",
1209138568Ssam				ether_sprintf(macaddr), __func__);
1210138568Ssam			ic->ic_stats.is_tx_nonode++;
1211127772Ssam		}
1212127772Ssam	}
1213127772Ssam	return ni;
1214127772Ssam}
1215127772Ssam
1216127772Ssam/*
1217138568Ssam * Like find but search based on the ssid too.
1218138568Ssam */
1219138568Ssamstruct ieee80211_node *
1220138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1221138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1222170530Ssam	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid,
1223138568Ssam	const char *func, int line)
1224138568Ssam#else
1225138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1226170530Ssam	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid)
1227138568Ssam#endif
1228138568Ssam{
1229147118Ssam#define	MATCH_SSID(ni, ssid, ssidlen) \
1230147118Ssam	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1231170530Ssam	static const uint8_t zeromac[IEEE80211_ADDR_LEN];
1232138568Ssam	struct ieee80211_node *ni;
1233138568Ssam	int hash;
1234138568Ssam
1235138568Ssam	IEEE80211_NODE_LOCK(nt);
1236147118Ssam	/*
1237147118Ssam	 * A mac address that is all zero means match only the ssid;
1238147118Ssam	 * otherwise we must match both.
1239147118Ssam	 */
1240147118Ssam	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1241147118Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1242147118Ssam			if (MATCH_SSID(ni, ssid, ssidlen))
1243147118Ssam				break;
1244147118Ssam		}
1245147118Ssam	} else {
1246147118Ssam		hash = IEEE80211_NODE_HASH(macaddr);
1247147118Ssam		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1248147118Ssam			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1249147118Ssam			    MATCH_SSID(ni, ssid, ssidlen))
1250147118Ssam				break;
1251147118Ssam		}
1252147118Ssam	}
1253147118Ssam	if (ni != NULL) {
1254147118Ssam		ieee80211_ref_node(ni);	/* mark referenced */
1255170530Ssam		IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1256159139Sdds		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
1257147118Ssam		     ieee80211_node_refcnt(ni));
1258138568Ssam	}
1259138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1260138568Ssam	return ni;
1261147118Ssam#undef MATCH_SSID
1262138568Ssam}
1263138568Ssam
1264116742Ssamstatic void
1265138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1266116742Ssam{
1267138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1268138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1269119150Ssam
1270138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1271140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1272140766Ssam		ether_sprintf(ni->ni_macaddr),
1273138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1274138568Ssam
1275138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1276138568Ssam	if (nt != NULL) {
1277138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1278138568Ssam		LIST_REMOVE(ni, ni_hash);
1279138568Ssam	}
1280138568Ssam	ic->ic_node_free(ni);
1281116742Ssam}
1282116742Ssam
1283116742Ssamvoid
1284138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1285138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1286138568Ssam#else
1287138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1288138568Ssam#endif
1289116742Ssam{
1290138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1291119150Ssam
1292138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1293140454Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1294140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1295138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1296138568Ssam#endif
1297148863Ssam	if (nt != NULL) {
1298148863Ssam		IEEE80211_NODE_LOCK(nt);
1299148863Ssam		if (ieee80211_node_dectestref(ni)) {
1300148863Ssam			/*
1301148863Ssam			 * Last reference, reclaim state.
1302148863Ssam			 */
1303138568Ssam			_ieee80211_free_node(ni);
1304148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1305148863Ssam		    nt->nt_keyixmap != NULL) {
1306148863Ssam			ieee80211_keyix keyix;
1307148863Ssam			/*
1308148863Ssam			 * Check for a last reference in the key mapping table.
1309148863Ssam			 */
1310148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1311148863Ssam			if (keyix < nt->nt_keyixmax &&
1312148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1313148863Ssam				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1314148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1315148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1316148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1317148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1318148863Ssam				_ieee80211_free_node(ni);
1319148863Ssam			}
1320148863Ssam		}
1321148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1322148863Ssam	} else {
1323148863Ssam		if (ieee80211_node_dectestref(ni))
1324138568Ssam			_ieee80211_free_node(ni);
1325116742Ssam	}
1326116742Ssam}
1327116742Ssam
1328138568Ssam/*
1329148863Ssam * Reclaim a unicast key and clear any key cache state.
1330148863Ssam */
1331148863Ssamint
1332148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1333148863Ssam{
1334148863Ssam	struct ieee80211com *ic = ni->ni_ic;
1335148863Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1336148863Ssam	struct ieee80211_node *nikey;
1337148863Ssam	ieee80211_keyix keyix;
1338148863Ssam	int isowned, status;
1339148863Ssam
1340148863Ssam	/*
1341148863Ssam	 * NB: We must beware of LOR here; deleting the key
1342148863Ssam	 * can cause the crypto layer to block traffic updates
1343148863Ssam	 * which can generate a LOR against the node table lock;
1344148863Ssam	 * grab it here and stash the key index for our use below.
1345148863Ssam	 *
1346148863Ssam	 * Must also beware of recursion on the node table lock.
1347148863Ssam	 * When called from node_cleanup we may already have
1348148863Ssam	 * the node table lock held.  Unfortunately there's no
1349148863Ssam	 * way to separate out this path so we must do this
1350148863Ssam	 * conditionally.
1351148863Ssam	 */
1352148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1353148863Ssam	if (!isowned)
1354148863Ssam		IEEE80211_NODE_LOCK(nt);
1355148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1356148863Ssam	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1357148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1358148863Ssam		nikey = nt->nt_keyixmap[keyix];
1359148863Ssam		nt->nt_keyixmap[keyix] = NULL;;
1360148863Ssam	} else
1361148863Ssam		nikey = NULL;
1362148863Ssam	if (!isowned)
1363148863Ssam		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1364148863Ssam
1365148863Ssam	if (nikey != NULL) {
1366148863Ssam		KASSERT(nikey == ni,
1367148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1368148863Ssam		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1369148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1370148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1371148863Ssam			ieee80211_node_refcnt(ni)-1);
1372148863Ssam		ieee80211_free_node(ni);
1373148863Ssam	}
1374148863Ssam	return status;
1375148863Ssam}
1376148863Ssam
1377148863Ssam/*
1378138568Ssam * Reclaim a node.  If this is the last reference count then
1379138568Ssam * do the normal free work.  Otherwise remove it from the node
1380138568Ssam * table and mark it gone by clearing the back-reference.
1381138568Ssam */
1382138568Ssamstatic void
1383138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1384116742Ssam{
1385148863Ssam	ieee80211_keyix keyix;
1386138568Ssam
1387148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1388148863Ssam
1389140766Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1390140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1391140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1392140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1393148863Ssam	/*
1394148863Ssam	 * Clear any entry in the unicast key mapping table.
1395148863Ssam	 * We need to do it here so rx lookups don't find it
1396148863Ssam	 * in the mapping table even if it's not in the hash
1397148863Ssam	 * table.  We cannot depend on the mapping table entry
1398148863Ssam	 * being cleared because the node may not be free'd.
1399148863Ssam	 */
1400148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1401148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1402148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1403148863Ssam		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1404148863Ssam			"%s: %p<%s> clear key map entry\n",
1405148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr));
1406148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1407148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1408148863Ssam	}
1409138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1410138568Ssam		/*
1411138568Ssam		 * Other references are present, just remove the
1412138568Ssam		 * node from the table so it cannot be found.  When
1413138568Ssam		 * the references are dropped storage will be
1414140753Ssam		 * reclaimed.
1415138568Ssam		 */
1416138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1417138568Ssam		LIST_REMOVE(ni, ni_hash);
1418138568Ssam		ni->ni_table = NULL;		/* clear reference */
1419138568Ssam	} else
1420138568Ssam		_ieee80211_free_node(ni);
1421138568Ssam}
1422138568Ssam
1423138568Ssamstatic void
1424138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1425138568Ssam{
1426138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1427116742Ssam	struct ieee80211_node *ni;
1428116742Ssam
1429138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1430138568Ssam		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1431138568Ssam
1432138568Ssam	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1433138568Ssam		if (ni->ni_associd != 0) {
1434138568Ssam			if (ic->ic_auth->ia_node_leave != NULL)
1435138568Ssam				ic->ic_auth->ia_node_leave(ic, ni);
1436138568Ssam			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1437138568Ssam		}
1438138568Ssam		node_reclaim(nt, ni);
1439138568Ssam	}
1440116742Ssam}
1441116742Ssam
1442120483Ssam/*
1443138568Ssam * Timeout inactive stations and do related housekeeping.
1444138568Ssam * Note that we cannot hold the node lock while sending a
1445138568Ssam * frame as this would lead to a LOR.  Instead we use a
1446138568Ssam * generation number to mark nodes that we've scanned and
1447138568Ssam * drop the lock and restart a scan if we have to time out
1448138568Ssam * a node.  Since we are single-threaded by virtue of
1449120483Ssam * controlling the inactivity timer we can be sure this will
1450120483Ssam * process each node only once.
1451120483Ssam */
1452138568Ssamstatic void
1453138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt)
1454116742Ssam{
1455138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1456120483Ssam	struct ieee80211_node *ni;
1457138568Ssam	u_int gen;
1458148320Ssam	int isadhoc;
1459116742Ssam
1460148320Ssam	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1461148320Ssam		   ic->ic_opmode == IEEE80211_M_AHDEMO);
1462138568Ssam	IEEE80211_SCAN_LOCK(nt);
1463154532Ssam	gen = ++nt->nt_scangen;
1464120483Ssamrestart:
1465138568Ssam	IEEE80211_NODE_LOCK(nt);
1466138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1467120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1468120483Ssam			continue;
1469120483Ssam		ni->ni_scangen = gen;
1470138568Ssam		/*
1471147788Ssam		 * Ignore entries for which have yet to receive an
1472147788Ssam		 * authentication frame.  These are transient and
1473147788Ssam		 * will be reclaimed when the last reference to them
1474147788Ssam		 * goes away (when frame xmits complete).
1475147788Ssam		 */
1476170530Ssam		if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
1477170530Ssam		     ic->ic_opmode == IEEE80211_M_STA) &&
1478148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1479147788Ssam			continue;
1480147788Ssam		/*
1481138568Ssam		 * Free fragment if not needed anymore
1482138568Ssam		 * (last fragment older than 1s).
1483138568Ssam		 * XXX doesn't belong here
1484138568Ssam		 */
1485138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1486138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1487138568Ssam			m_freem(ni->ni_rxfrag[0]);
1488138568Ssam			ni->ni_rxfrag[0] = NULL;
1489138568Ssam		}
1490172062Ssam		if (ni->ni_inact > 0)
1491172062Ssam			ni->ni_inact--;
1492140498Ssam		/*
1493140498Ssam		 * Special case ourself; we may be idle for extended periods
1494140498Ssam		 * of time and regardless reclaiming our state is wrong.
1495140498Ssam		 */
1496140498Ssam		if (ni == ic->ic_bss)
1497140498Ssam			continue;
1498148320Ssam		if (ni->ni_associd != 0 || isadhoc) {
1499119150Ssam			/*
1500170530Ssam			 * Age frames on the power save queue.
1501138568Ssam			 */
1502170530Ssam			if (ieee80211_node_saveq_age(ni) != 0 &&
1503170530Ssam			    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
1504170530Ssam			    ic->ic_set_tim != NULL)
1505170530Ssam				ic->ic_set_tim(ni, 0);
1506138568Ssam			/*
1507138568Ssam			 * Probe the station before time it out.  We
1508138568Ssam			 * send a null data frame which may not be
1509138568Ssam			 * universally supported by drivers (need it
1510138568Ssam			 * for ps-poll support so it should be...).
1511170530Ssam			 *
1512170530Ssam			 * XXX don't probe the station unless we've
1513170530Ssam			 *     received a frame from them (and have
1514170530Ssam			 *     some idea of the rates they are capable
1515170530Ssam			 *     of); this will get fixed more properly
1516170530Ssam			 *     soon with better handling of the rate set.
1517138568Ssam			 */
1518172062Ssam			if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1519172062Ssam			    (0 < ni->ni_inact &&
1520172062Ssam			     ni->ni_inact <= ic->ic_inact_probe) &&
1521170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1522148320Ssam				IEEE80211_NOTE(ic,
1523148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1524148320Ssam				    ni, "%s",
1525148320Ssam				    "probe station due to inactivity");
1526148582Ssam				/*
1527148582Ssam				 * Grab a reference before unlocking the table
1528148582Ssam				 * so the node cannot be reclaimed before we
1529148582Ssam				 * send the frame. ieee80211_send_nulldata
1530148582Ssam				 * understands we've done this and reclaims the
1531148582Ssam				 * ref for us as needed.
1532148582Ssam				 */
1533148582Ssam				ieee80211_ref_node(ni);
1534138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1535148301Ssam				ieee80211_send_nulldata(ni);
1536138568Ssam				/* XXX stat? */
1537138568Ssam				goto restart;
1538138568Ssam			}
1539138568Ssam		}
1540172062Ssam		if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1541172062Ssam		    ni->ni_inact <= 0) {
1542148320Ssam			IEEE80211_NOTE(ic,
1543148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1544148320Ssam			    "station timed out due to inactivity "
1545148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1546138568Ssam			/*
1547138568Ssam			 * Send a deauthenticate frame and drop the station.
1548138568Ssam			 * This is somewhat complicated due to reference counts
1549138568Ssam			 * and locking.  At this point a station will typically
1550138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1551138568Ssam			 * will do a "free" of the node which will drop the
1552138568Ssam			 * reference count.  But in the meantime a reference
1553138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1554138568Ssam			 * of the node will happen either after the tx is
1555138568Ssam			 * completed or by ieee80211_node_leave.
1556120483Ssam			 *
1557138568Ssam			 * Separately we must drop the node lock before sending
1558170530Ssam			 * in case the driver takes a lock, as this can result
1559170530Ssam			 * in a LOR between the node lock and the driver lock.
1560119150Ssam			 */
1561172229Ssam			ieee80211_ref_node(ni);
1562138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1563138568Ssam			if (ni->ni_associd != 0) {
1564138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
1565138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1566138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1567138568Ssam			}
1568138568Ssam			ieee80211_node_leave(ic, ni);
1569172229Ssam			ieee80211_free_node(ni);
1570121180Ssam			ic->ic_stats.is_node_timeout++;
1571120483Ssam			goto restart;
1572120483Ssam		}
1573116742Ssam	}
1574138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1575138568Ssam
1576138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1577170530Ssam}
1578138568Ssam
1579170530Ssamvoid
1580170530Ssamieee80211_node_timeout(void *arg)
1581170530Ssam{
1582170530Ssam	struct ieee80211com *ic = arg;
1583170530Ssam
1584170530Ssam	ieee80211_scan_timeout(ic);
1585170530Ssam	ieee80211_timeout_stations(&ic->ic_sta);
1586170530Ssam
1587172211Ssam	IEEE80211_LOCK(ic);
1588172211Ssam	ieee80211_erp_timeout(ic);
1589173273Ssam	ieee80211_ht_timeout(ic);
1590172211Ssam	IEEE80211_UNLOCK(ic);
1591172211Ssam
1592170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
1593170530Ssam		ieee80211_node_timeout, ic);
1594116742Ssam}
1595116742Ssam
1596116742Ssamvoid
1597138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1598116742Ssam{
1599116742Ssam	struct ieee80211_node *ni;
1600138568Ssam	u_int gen;
1601116742Ssam
1602138568Ssam	IEEE80211_SCAN_LOCK(nt);
1603154532Ssam	gen = ++nt->nt_scangen;
1604138568Ssamrestart:
1605138568Ssam	IEEE80211_NODE_LOCK(nt);
1606138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1607138568Ssam		if (ni->ni_scangen != gen) {
1608138568Ssam			ni->ni_scangen = gen;
1609138568Ssam			(void) ieee80211_ref_node(ni);
1610138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1611138568Ssam			(*f)(arg, ni);
1612138568Ssam			ieee80211_free_node(ni);
1613138568Ssam			goto restart;
1614138568Ssam		}
1615138568Ssam	}
1616138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1617138568Ssam
1618138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1619116742Ssam}
1620138568Ssam
1621138568Ssamvoid
1622138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1623138568Ssam{
1624138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
1625138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1626138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
1627138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1628138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
1629138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1630138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1631167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
1632167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
1633167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
1634138568Ssam		ni->ni_rxfragstamp);
1635170530Ssam	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
1636170530Ssam		ni->ni_rstamp, ni->ni_rssi, ni->ni_noise,
1637170530Ssam		ni->ni_intval, ni->ni_capinfo);
1638138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1639138568Ssam		ether_sprintf(ni->ni_bssid),
1640138568Ssam		ni->ni_esslen, ni->ni_essid,
1641138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1642138568Ssam	printf("\tfails %u inact %u txrate %u\n",
1643138568Ssam		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1644170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
1645170530Ssam		ni->ni_htcap, ni->ni_htparam,
1646170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
1647170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
1648170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
1649138568Ssam}
1650138568Ssam
1651138568Ssamvoid
1652138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
1653138568Ssam{
1654138568Ssam	ieee80211_iterate_nodes(nt,
1655138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
1656138568Ssam}
1657138568Ssam
1658172211Ssamvoid
1659172211Ssamieee80211_notify_erp(struct ieee80211com *ic)
1660172211Ssam{
1661172211Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1662172211Ssam		ieee80211_beacon_notify(ic, IEEE80211_BEACON_ERP);
1663172211Ssam}
1664172211Ssam
1665138568Ssam/*
1666138568Ssam * Handle a station joining an 11g network.
1667138568Ssam */
1668138568Ssamstatic void
1669138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1670138568Ssam{
1671138568Ssam
1672173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1673173273Ssam
1674138568Ssam	/*
1675138568Ssam	 * Station isn't capable of short slot time.  Bump
1676138568Ssam	 * the count of long slot time stations and disable
1677138568Ssam	 * use of short slot time.  Note that the actual switch
1678138568Ssam	 * over to long slot time use may not occur until the
1679138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1680138568Ssam	 */
1681138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1682138568Ssam		ic->ic_longslotsta++;
1683172211Ssam		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1684172211Ssam		    "station needs long slot time, count %d",
1685172211Ssam		    ic->ic_longslotsta);
1686138568Ssam		/* XXX vap's w/ conflicting needs won't work */
1687170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
1688170530Ssam			/*
1689170530Ssam			 * Don't force slot time when switched to turbo
1690170530Ssam			 * mode as non-ERP stations won't be present; this
1691170530Ssam			 * need only be done when on the normal G channel.
1692170530Ssam			 */
1693170530Ssam			ieee80211_set_shortslottime(ic, 0);
1694170530Ssam		}
1695138568Ssam	}
1696138568Ssam	/*
1697138568Ssam	 * If the new station is not an ERP station
1698138568Ssam	 * then bump the counter and enable protection
1699138568Ssam	 * if configured.
1700138568Ssam	 */
1701138568Ssam	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1702138568Ssam		ic->ic_nonerpsta++;
1703172211Ssam		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1704172211Ssam		    "station is !ERP, %d non-ERP stations associated",
1705172211Ssam		    ic->ic_nonerpsta);
1706138568Ssam		/*
1707138568Ssam		 * If station does not support short preamble
1708138568Ssam		 * then we must enable use of Barker preamble.
1709138568Ssam		 */
1710138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1711172211Ssam			IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1712172211Ssam			    "%s", "station needs long preamble");
1713138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1714138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1715138568Ssam		}
1716172211Ssam		/*
1717172211Ssam		 * If protection is configured, enable it.
1718172211Ssam		 */
1719172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1720172211Ssam		    ic->ic_nonerpsta == 1 &&
1721172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
1722172211Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1723172211Ssam			    "%s: enable use of protection\n", __func__);
1724172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1725172211Ssam			ieee80211_notify_erp(ic);
1726172211Ssam		}
1727138568Ssam	} else
1728138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
1729138568Ssam}
1730138568Ssam
1731138568Ssamvoid
1732138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1733138568Ssam{
1734138568Ssam	int newassoc;
1735138568Ssam
1736138568Ssam	if (ni->ni_associd == 0) {
1737170530Ssam		uint16_t aid;
1738138568Ssam
1739173273Ssam		IEEE80211_LOCK(ic);
1740138568Ssam		/*
1741138568Ssam		 * It would be good to search the bitmap
1742138568Ssam		 * more efficiently, but this will do for now.
1743138568Ssam		 */
1744138568Ssam		for (aid = 1; aid < ic->ic_max_aid; aid++) {
1745138568Ssam			if (!IEEE80211_AID_ISSET(aid,
1746138568Ssam			    ic->ic_aid_bitmap))
1747138568Ssam				break;
1748138568Ssam		}
1749138568Ssam		if (aid >= ic->ic_max_aid) {
1750173273Ssam			IEEE80211_UNLOCK(ic);
1751138568Ssam			IEEE80211_SEND_MGMT(ic, ni, resp,
1752138568Ssam			    IEEE80211_REASON_ASSOC_TOOMANY);
1753138568Ssam			ieee80211_node_leave(ic, ni);
1754138568Ssam			return;
1755138568Ssam		}
1756138568Ssam		ni->ni_associd = aid | 0xc000;
1757173273Ssam		ni->ni_jointime = time_uptime;
1758138568Ssam		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1759138568Ssam		ic->ic_sta_assoc++;
1760173273Ssam
1761173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
1762173273Ssam			ieee80211_ht_node_join(ni);
1763170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
1764170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
1765138568Ssam			ieee80211_node_join_11g(ic, ni);
1766173273Ssam		IEEE80211_UNLOCK(ic);
1767173273Ssam
1768173273Ssam		newassoc = 1;
1769138568Ssam	} else
1770138568Ssam		newassoc = 0;
1771138568Ssam
1772172211Ssam	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1773173866Ssam	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s",
1774139523Ssam	    IEEE80211_NODE_AID(ni),
1775139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1776139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1777139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1778170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1779170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
1780170530Ssam		(ni->ni_chw == 20 ? ", HT20" : ", HT40") : "",
1781173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1782170530Ssam	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_FF) ?
1783170530Ssam		", fast-frames" : "",
1784170530Ssam	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_TURBOP) ?
1785170530Ssam		", turbo" : ""
1786139523Ssam	);
1787138568Ssam
1788138568Ssam	/* give driver a chance to setup state like ni_txrate */
1789139524Ssam	if (ic->ic_newassoc != NULL)
1790148307Ssam		ic->ic_newassoc(ni, newassoc);
1791138568Ssam	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1792138568Ssam	/* tell the authenticator about new station */
1793138568Ssam	if (ic->ic_auth->ia_node_join != NULL)
1794138568Ssam		ic->ic_auth->ia_node_join(ic, ni);
1795173866Ssam	ieee80211_notify_node_join(ic, ni,
1796173866Ssam	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1797138568Ssam}
1798138568Ssam
1799172211Ssamstatic void
1800172211Ssamdisable_protection(struct ieee80211com *ic)
1801172211Ssam{
1802172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
1803172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
1804172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
1805172211Ssam	   ic->ic_flags_ext));
1806172211Ssam
1807172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
1808172211Ssam	/* XXX verify mode? */
1809172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1810172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1811172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1812172211Ssam	}
1813172211Ssam	ieee80211_notify_erp(ic);
1814172211Ssam}
1815172211Ssam
1816138568Ssam/*
1817138568Ssam * Handle a station leaving an 11g network.
1818138568Ssam */
1819138568Ssamstatic void
1820138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1821138568Ssam{
1822138568Ssam
1823173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1824173273Ssam
1825170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
1826170530Ssam	     ("not in 11g, bss %u:0x%x, curmode %u", ic->ic_bsschan->ic_freq,
1827170530Ssam	      ic->ic_bsschan->ic_flags, ic->ic_curmode));
1828138568Ssam
1829138568Ssam	/*
1830138568Ssam	 * If a long slot station do the slot time bookkeeping.
1831138568Ssam	 */
1832138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1833138568Ssam		KASSERT(ic->ic_longslotsta > 0,
1834138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
1835138568Ssam		ic->ic_longslotsta--;
1836172211Ssam		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1837172211Ssam		    "long slot time station leaves, count now %d",
1838172211Ssam		    ic->ic_longslotsta);
1839138568Ssam		if (ic->ic_longslotsta == 0) {
1840138568Ssam			/*
1841138568Ssam			 * Re-enable use of short slot time if supported
1842138568Ssam			 * and not operating in IBSS mode (per spec).
1843138568Ssam			 */
1844138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1845138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
1846138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1847138568Ssam				    "%s: re-enable use of short slot time\n",
1848138568Ssam				    __func__);
1849138568Ssam				ieee80211_set_shortslottime(ic, 1);
1850138568Ssam			}
1851138568Ssam		}
1852138568Ssam	}
1853138568Ssam	/*
1854138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
1855138568Ssam	 */
1856138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1857138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
1858138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1859138568Ssam		ic->ic_nonerpsta--;
1860172211Ssam		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1861172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
1862172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
1863172211Ssam			" (non-ERP sta present)" : "");
1864172211Ssam		if (ic->ic_nonerpsta == 0 &&
1865172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
1866138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1867138568Ssam				"%s: disable use of protection\n", __func__);
1868172211Ssam			disable_protection(ic);
1869138568Ssam		}
1870138568Ssam	}
1871138568Ssam}
1872138568Ssam
1873138568Ssam/*
1874172211Ssam * Time out presence of an overlapping bss with non-ERP
1875172211Ssam * stations.  When operating in hostap mode we listen for
1876172211Ssam * beacons from other stations and if we identify a non-ERP
1877172211Ssam * station is present we enable protection.  To identify
1878172211Ssam * when all non-ERP stations are gone we time out this
1879172211Ssam * condition.
1880172211Ssam */
1881172211Ssamstatic void
1882172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
1883172211Ssam{
1884172211Ssam
1885172211Ssam	IEEE80211_LOCK_ASSERT(ic);
1886172211Ssam
1887172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
1888172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
1889172211Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1890172211Ssam		    "%s\n", "age out non-ERP sta present on channel");
1891172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
1892172211Ssam		if (ic->ic_nonerpsta == 0)
1893172211Ssam			disable_protection(ic);
1894172211Ssam	}
1895172211Ssam}
1896172211Ssam
1897172211Ssam/*
1898138568Ssam * Handle bookkeeping for station deauthentication/disassociation
1899138568Ssam * when operating as an ap.
1900138568Ssam */
1901138568Ssamvoid
1902138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1903138568Ssam{
1904140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1905138568Ssam
1906172211Ssam	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1907172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
1908138568Ssam
1909170530Ssam	KASSERT(ic->ic_opmode != IEEE80211_M_STA,
1910138568Ssam		("unexpected operating mode %u", ic->ic_opmode));
1911138568Ssam	/*
1912138568Ssam	 * If node wasn't previously associated all
1913138568Ssam	 * we need to do is reclaim the reference.
1914138568Ssam	 */
1915138568Ssam	/* XXX ibss mode bypasses 11g and notification */
1916138568Ssam	if (ni->ni_associd == 0)
1917138568Ssam		goto done;
1918138568Ssam	/*
1919138568Ssam	 * Tell the authenticator the station is leaving.
1920138568Ssam	 * Note that we must do this before yanking the
1921138568Ssam	 * association id as the authenticator uses the
1922138568Ssam	 * associd to locate it's state block.
1923138568Ssam	 */
1924138568Ssam	if (ic->ic_auth->ia_node_leave != NULL)
1925138568Ssam		ic->ic_auth->ia_node_leave(ic, ni);
1926173273Ssam
1927173273Ssam	IEEE80211_LOCK(ic);
1928138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1929138568Ssam	ni->ni_associd = 0;
1930138568Ssam	ic->ic_sta_assoc--;
1931138568Ssam
1932173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
1933173273Ssam		ieee80211_ht_node_leave(ni);
1934170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
1935170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
1936138568Ssam		ieee80211_node_leave_11g(ic, ni);
1937173273Ssam	IEEE80211_UNLOCK(ic);
1938138568Ssam	/*
1939138568Ssam	 * Cleanup station state.  In particular clear various
1940138568Ssam	 * state that might otherwise be reused if the node
1941138568Ssam	 * is reused before the reference count goes to zero
1942138568Ssam	 * (and memory is reclaimed).
1943138568Ssam	 */
1944138568Ssam	ieee80211_sta_leave(ic, ni);
1945138568Ssamdone:
1946140499Ssam	/*
1947140499Ssam	 * Remove the node from any table it's recorded in and
1948140499Ssam	 * drop the caller's reference.  Removal from the table
1949140499Ssam	 * is important to insure the node is not reprocessed
1950140499Ssam	 * for inactivity.
1951140499Ssam	 */
1952140499Ssam	if (nt != NULL) {
1953140499Ssam		IEEE80211_NODE_LOCK(nt);
1954140499Ssam		node_reclaim(nt, ni);
1955140499Ssam		IEEE80211_NODE_UNLOCK(nt);
1956140499Ssam	} else
1957140499Ssam		ieee80211_free_node(ni);
1958138568Ssam}
1959138568Ssam
1960170530Ssamint8_t
1961138568Ssamieee80211_getrssi(struct ieee80211com *ic)
1962138568Ssam{
1963138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
1964140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1965170530Ssam	int rssi_samples;
1966170530Ssam	int32_t rssi_total;
1967138568Ssam	struct ieee80211_node *ni;
1968138568Ssam
1969138568Ssam	rssi_total = 0;
1970138568Ssam	rssi_samples = 0;
1971138568Ssam	switch (ic->ic_opmode) {
1972138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
1973138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
1974138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
1975138568Ssam		/* XXX locking */
1976140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1977170530Ssam			if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1978170530Ssam			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS)) {
1979170530Ssam				int8_t rssi = ic->ic_node_getrssi(ni);
1980170530Ssam				if (rssi != 0) {
1981170530Ssam					rssi_samples++;
1982170530Ssam					rssi_total += rssi;
1983170530Ssam				}
1984138568Ssam			}
1985138568Ssam		break;
1986138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
1987138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
1988138568Ssam	default:
1989138568Ssam		if (ic->ic_bss != NULL)
1990138568Ssam			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1991138568Ssam		rssi_samples = 1;
1992138568Ssam		break;
1993138568Ssam	}
1994138568Ssam	return rssi_total / NZ(rssi_samples);
1995138568Ssam#undef NZ
1996138568Ssam}
1997138568Ssam
1998170530Ssamvoid
1999170530Ssamieee80211_getsignal(struct ieee80211com *ic, int8_t *rssi, int8_t *noise)
2000138568Ssam{
2001138568Ssam
2002170530Ssam	if (ic->ic_bss == NULL)		/* NB: shouldn't happen */
2003170530Ssam		return;
2004170530Ssam	ic->ic_node_getsignal(ic->ic_bss, rssi, noise);
2005170530Ssam	/* for non-station mode return avg'd rssi accounting */
2006170530Ssam	if (ic->ic_opmode != IEEE80211_M_STA)
2007170530Ssam		*rssi = ieee80211_getrssi(ic);
2008138568Ssam}
2009138568Ssam
2010138568Ssam/*
2011138568Ssam * Node table support.
2012138568Ssam */
2013138568Ssam
2014138568Ssamstatic void
2015138568Ssamieee80211_node_table_init(struct ieee80211com *ic,
2016138568Ssam	struct ieee80211_node_table *nt,
2017170530Ssam	const char *name, int inact, int keyixmax)
2018138568Ssam{
2019138568Ssam
2020138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
2021138568Ssam		"%s %s table, inact %u\n", __func__, name, inact);
2022138568Ssam
2023138568Ssam	nt->nt_ic = ic;
2024138568Ssam	/* XXX need unit */
2025138568Ssam	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2026138568Ssam	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2027138568Ssam	TAILQ_INIT(&nt->nt_node);
2028138568Ssam	nt->nt_name = name;
2029138568Ssam	nt->nt_scangen = 1;
2030138568Ssam	nt->nt_inact_init = inact;
2031148863Ssam	nt->nt_keyixmax = keyixmax;
2032148863Ssam	if (nt->nt_keyixmax > 0) {
2033148863Ssam		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2034148863Ssam			keyixmax * sizeof(struct ieee80211_node *),
2035148863Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
2036148863Ssam		if (nt->nt_keyixmap == NULL)
2037148863Ssam			if_printf(ic->ic_ifp,
2038148863Ssam			    "Cannot allocate key index map with %u entries\n",
2039148863Ssam			    keyixmax);
2040148863Ssam	} else
2041148863Ssam		nt->nt_keyixmap = NULL;
2042138568Ssam}
2043138568Ssam
2044170530Ssamstatic void
2045138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt)
2046138568Ssam{
2047138568Ssam
2048138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2049138568Ssam		"%s %s table\n", __func__, nt->nt_name);
2050138568Ssam
2051138568Ssam	IEEE80211_NODE_LOCK(nt);
2052138568Ssam	ieee80211_free_allnodes_locked(nt);
2053138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2054138568Ssam}
2055138568Ssam
2056138568Ssamstatic void
2057138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2058138568Ssam{
2059138568Ssam
2060138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2061138568Ssam		"%s %s table\n", __func__, nt->nt_name);
2062138568Ssam
2063148863Ssam	IEEE80211_NODE_LOCK(nt);
2064138568Ssam	ieee80211_free_allnodes_locked(nt);
2065148863Ssam	if (nt->nt_keyixmap != NULL) {
2066148863Ssam		/* XXX verify all entries are NULL */
2067148863Ssam		int i;
2068148863Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
2069148863Ssam			if (nt->nt_keyixmap[i] != NULL)
2070148863Ssam				printf("%s: %s[%u] still active\n", __func__,
2071148863Ssam					nt->nt_name, i);
2072148863Ssam		FREE(nt->nt_keyixmap, M_80211_NODE);
2073148863Ssam		nt->nt_keyixmap = NULL;
2074148863Ssam	}
2075138568Ssam	IEEE80211_SCAN_LOCK_DESTROY(nt);
2076138568Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
2077138568Ssam}
2078