ieee80211_node.c revision 173273
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 173273 2007-11-02 05:22:25Z 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;
656170530Ssam	if (se->se_htcap_ie != NULL)
657173273Ssam		ieee80211_saveie(&ni->ni_htcap_ie, se->se_htcap_ie);
658170530Ssam	if (se->se_wpa_ie != NULL)
659170530Ssam		ieee80211_saveie(&ni->ni_wpa_ie, se->se_wpa_ie);
660170530Ssam	if (se->se_rsn_ie != NULL)
661170530Ssam		ieee80211_saveie(&ni->ni_rsn_ie, se->se_rsn_ie);
662170530Ssam	if (se->se_wme_ie != NULL)
663170530Ssam		ieee80211_saveie(&ni->ni_wme_ie, se->se_wme_ie);
664170530Ssam	if (se->se_ath_ie != NULL)
665170530Ssam		ieee80211_saveath(ni, se->se_ath_ie);
666170530Ssam
667170530Ssam	ic->ic_dtim_period = se->se_dtimperiod;
668170530Ssam	ic->ic_dtim_count = 0;
669170530Ssam
670170530Ssam	/* NB: must be after ni_chan is setup */
671170530Ssam	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
672170530Ssam		IEEE80211_F_DOSORT);
673170530Ssam
674170530Ssam	return ieee80211_sta_join1(ieee80211_ref_node(ni));
675170530Ssam}
676170530Ssam
677138568Ssam/*
678138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
679138568Ssam * be passed in with a held reference.
680138568Ssam */
681138568Ssamvoid
682138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
683138568Ssam{
684138568Ssam	ic->ic_node_cleanup(ni);
685138568Ssam	ieee80211_notify_node_leave(ic, ni);
686138568Ssam}
687138568Ssam
688116742Ssamstatic struct ieee80211_node *
689138568Ssamnode_alloc(struct ieee80211_node_table *nt)
690116742Ssam{
691127768Ssam	struct ieee80211_node *ni;
692138568Ssam
693127768Ssam	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
694127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
695127768Ssam	return ni;
696116742Ssam}
697116742Ssam
698138568Ssam/*
699138568Ssam * Reclaim any resources in a node and reset any critical
700138568Ssam * state.  Typically nodes are free'd immediately after,
701138568Ssam * but in some cases the storage may be reused so we need
702138568Ssam * to insure consistent state (should probably fix that).
703138568Ssam */
704116742Ssamstatic void
705138568Ssamnode_cleanup(struct ieee80211_node *ni)
706116742Ssam{
707138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
708138568Ssam	struct ieee80211com *ic = ni->ni_ic;
709170530Ssam	int i;
710138568Ssam
711138568Ssam	/* NB: preserve ni_table */
712138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
713172228Ssam		if (ic->ic_opmode != IEEE80211_M_STA)
714172228Ssam			ic->ic_ps_sta--;
715138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
716138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
717138568Ssam		    "[%s] power save mode off, %u sta's in ps mode\n",
718138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
719138568Ssam	}
720147788Ssam	/*
721173273Ssam	 * Cleanup any HT-related state.
722173273Ssam	 */
723173273Ssam	if (ni->ni_flags & IEEE80211_NODE_HT)
724173273Ssam		ieee80211_ht_node_cleanup(ni);
725173273Ssam	/*
726147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
727147788Ssam	 * has happened.  This is probably not needed as the node
728147788Ssam	 * should always be removed from the table so not found but
729147788Ssam	 * do it just in case.
730147788Ssam	 */
731147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
732138568Ssam
733138568Ssam	/*
734138568Ssam	 * Drain power save queue and, if needed, clear TIM.
735138568Ssam	 */
736170530Ssam	if (ieee80211_node_saveq_drain(ni) != 0 && ic->ic_set_tim != NULL)
737148304Ssam		ic->ic_set_tim(ni, 0);
738138568Ssam
739138568Ssam	ni->ni_associd = 0;
740138568Ssam	if (ni->ni_challenge != NULL) {
741170530Ssam		FREE(ni->ni_challenge, M_80211_NODE);
742138568Ssam		ni->ni_challenge = NULL;
743138568Ssam	}
744138568Ssam	/*
745138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
746138568Ssam	 * reusable during a re-auth/re-assoc state transition.
747138568Ssam	 * If we remove these data they will not be recreated
748138568Ssam	 * because they come from a probe-response or beacon frame
749138568Ssam	 * which cannot be expected prior to the association-response.
750138568Ssam	 * This should not be an issue when operating in other modes
751138568Ssam	 * as stations leaving always go through a full state transition
752138568Ssam	 * which will rebuild this state.
753138568Ssam	 *
754138568Ssam	 * XXX does this leave us open to inheriting old state?
755138568Ssam	 */
756138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
757138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
758138568Ssam			m_freem(ni->ni_rxfrag[i]);
759138568Ssam			ni->ni_rxfrag[i] = NULL;
760138568Ssam		}
761148863Ssam	/*
762148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
763148863Ssam	 */
764148863Ssam	ieee80211_node_delucastkey(ni);
765138568Ssam#undef N
766116742Ssam}
767116742Ssam
768116742Ssamstatic void
769138568Ssamnode_free(struct ieee80211_node *ni)
770116742Ssam{
771138568Ssam	struct ieee80211com *ic = ni->ni_ic;
772138568Ssam
773138568Ssam	ic->ic_node_cleanup(ni);
774138568Ssam	if (ni->ni_wpa_ie != NULL)
775170530Ssam		FREE(ni->ni_wpa_ie, M_80211_NODE);
776170530Ssam	if (ni->ni_rsn_ie != NULL)
777170530Ssam		FREE(ni->ni_rsn_ie, M_80211_NODE);
778138568Ssam	if (ni->ni_wme_ie != NULL)
779170530Ssam		FREE(ni->ni_wme_ie, M_80211_NODE);
780170530Ssam	if (ni->ni_ath_ie != NULL)
781170530Ssam		FREE(ni->ni_ath_ie, M_80211_NODE);
782138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
783138568Ssam	FREE(ni, M_80211_NODE);
784116742Ssam}
785116742Ssam
786170530Ssamstatic int8_t
787138568Ssamnode_getrssi(const struct ieee80211_node *ni)
788120104Ssam{
789120104Ssam	return ni->ni_rssi;
790120104Ssam}
791120104Ssam
792116742Ssamstatic void
793170530Ssamnode_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
794170530Ssam{
795170530Ssam	*rssi = ni->ni_rssi;
796170530Ssam	*noise = ni->ni_noise;
797170530Ssam}
798170530Ssam
799170530Ssamstatic void
800138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt,
801170530Ssam	struct ieee80211_node *ni, const uint8_t *macaddr)
802116742Ssam{
803138568Ssam	struct ieee80211com *ic = nt->nt_ic;
804116742Ssam	int hash;
805116742Ssam
806138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
807140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
808138568Ssam		ether_sprintf(macaddr), nt->nt_name);
809138568Ssam
810116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
811116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
812138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
813138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
814138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
815138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
816138568Ssam	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
817139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
818139528Ssam	ni->ni_inact = ni->ni_inact_reload;
819170530Ssam	ni->ni_ath_defkeyix = 0x7fff;
820138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
821138568Ssam
822138568Ssam	IEEE80211_NODE_LOCK(nt);
823138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
824138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
825138568Ssam	ni->ni_table = nt;
826138568Ssam	ni->ni_ic = ic;
827138568Ssam	IEEE80211_NODE_UNLOCK(nt);
828116742Ssam}
829116742Ssam
830116742Ssamstruct ieee80211_node *
831170530Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
832116742Ssam{
833138568Ssam	struct ieee80211com *ic = nt->nt_ic;
834138568Ssam	struct ieee80211_node *ni;
835138568Ssam
836138568Ssam	ni = ic->ic_node_alloc(nt);
837116742Ssam	if (ni != NULL)
838138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
839127769Ssam	else
840127769Ssam		ic->ic_stats.is_rx_nodealloc++;
841116742Ssam	return ni;
842116742Ssam}
843116742Ssam
844148777Ssam/*
845148777Ssam * Craft a temporary node suitable for sending a management frame
846148777Ssam * to the specified station.  We craft only as much state as we
847148777Ssam * need to do the work since the node will be immediately reclaimed
848148777Ssam * once the send completes.
849148777Ssam */
850116742Ssamstruct ieee80211_node *
851170530Ssamieee80211_tmp_node(struct ieee80211com *ic, const uint8_t *macaddr)
852148777Ssam{
853148777Ssam	struct ieee80211_node *ni;
854148777Ssam
855148777Ssam	ni = ic->ic_node_alloc(&ic->ic_sta);
856148777Ssam	if (ni != NULL) {
857148777Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
858148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
859148777Ssam
860148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
861148777Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
862148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
863148777Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
864148777Ssam		/* NB: required by ieee80211_fix_rate */
865170530Ssam		ieee80211_node_set_chan(ic, ni);
866148777Ssam		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
867148777Ssam			IEEE80211_KEYIX_NONE);
868148777Ssam		/* XXX optimize away */
869148777Ssam		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
870148777Ssam
871148777Ssam		ni->ni_table = NULL;		/* NB: pedantic */
872148777Ssam		ni->ni_ic = ic;
873148777Ssam	} else {
874148777Ssam		/* XXX msg */
875148777Ssam		ic->ic_stats.is_rx_nodealloc++;
876148777Ssam	}
877148777Ssam	return ni;
878148777Ssam}
879148777Ssam
880148777Ssamstruct ieee80211_node *
881170530Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const uint8_t *macaddr)
882116742Ssam{
883138568Ssam	struct ieee80211com *ic = nt->nt_ic;
884138568Ssam	struct ieee80211_node *ni;
885138568Ssam
886138568Ssam	ni = ic->ic_node_alloc(nt);
887116742Ssam	if (ni != NULL) {
888138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
889127770Ssam		/*
890127770Ssam		 * Inherit from ic_bss.
891127770Ssam		 */
892138568Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
893138568Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
894138568Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
895127770Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
896170530Ssam		ieee80211_node_set_chan(ic, ni);
897138568Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
898127770Ssam	} else
899127770Ssam		ic->ic_stats.is_rx_nodealloc++;
900116742Ssam	return ni;
901116742Ssam}
902116742Ssam
903127772Ssamstatic struct ieee80211_node *
904138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
905138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
906170530Ssam	const uint8_t *macaddr, const char *func, int line)
907138568Ssam#else
908138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt,
909170530Ssam	const uint8_t *macaddr)
910138568Ssam#endif
911116742Ssam{
912116742Ssam	struct ieee80211_node *ni;
913116742Ssam	int hash;
914116742Ssam
915138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
916127772Ssam
917116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
918138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
919116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
920138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
921138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
922138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
923140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
924140766Ssam			    func, line,
925140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
926140766Ssam			    ieee80211_node_refcnt(ni));
927138568Ssam#endif
928127772Ssam			return ni;
929116742Ssam		}
930116742Ssam	}
931127772Ssam	return NULL;
932127772Ssam}
933138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
934138568Ssam#define	_ieee80211_find_node(nt, mac) \
935138568Ssam	_ieee80211_find_node_debug(nt, mac, func, line)
936138568Ssam#endif
937127772Ssam
938127772Ssamstruct ieee80211_node *
939138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
940138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
941170530Ssam	const uint8_t *macaddr, const char *func, int line)
942138568Ssam#else
943170530Ssamieee80211_find_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
944138568Ssam#endif
945127772Ssam{
946127772Ssam	struct ieee80211_node *ni;
947127772Ssam
948138568Ssam	IEEE80211_NODE_LOCK(nt);
949138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
950138568Ssam	IEEE80211_NODE_UNLOCK(nt);
951116742Ssam	return ni;
952116742Ssam}
953116742Ssam
954116742Ssam/*
955138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
956138568Ssam * Note that for the driver's benefit we we treat this like
957138568Ssam * an association so the driver has an opportunity to setup
958138568Ssam * it's private state.
959138568Ssam */
960138568Ssamstruct ieee80211_node *
961138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
962170530Ssam	const uint8_t macaddr[IEEE80211_ADDR_LEN])
963138568Ssam{
964138568Ssam	struct ieee80211com *ic = nt->nt_ic;
965138568Ssam	struct ieee80211_node *ni;
966138568Ssam
967153073Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
968153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
969138568Ssam	ni = ieee80211_dup_bss(nt, macaddr);
970138568Ssam	if (ni != NULL) {
971138568Ssam		/* XXX no rate negotiation; just dup */
972138568Ssam		ni->ni_rates = ic->ic_bss->ni_rates;
973139524Ssam		if (ic->ic_newassoc != NULL)
974148307Ssam			ic->ic_newassoc(ni, 1);
975153404Ssam		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
976153404Ssam			/*
977170530Ssam			 * In adhoc demo mode there are no management
978170530Ssam			 * frames to use to discover neighbor capabilities,
979170530Ssam			 * so blindly propagate the local configuration
980170530Ssam			 * so we can do interesting things (e.g. use
981170530Ssam			 * WME to disable ACK's).
982153404Ssam			 */
983153404Ssam			if (ic->ic_flags & IEEE80211_F_WME)
984153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
985170530Ssam			if (ic->ic_flags & IEEE80211_F_FF)
986170530Ssam				ni->ni_flags |= IEEE80211_NODE_FF;
987153404Ssam		}
988170530Ssam		/* XXX not right for 802.1x/WPA */
989170530Ssam		ieee80211_node_authorize(ni);
990138568Ssam	}
991138568Ssam	return ni;
992138568Ssam}
993138568Ssam
994148936Ssamvoid
995153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
996153073Ssam	const struct ieee80211_frame *wh,
997153073Ssam	const struct ieee80211_scanparams *sp)
998153073Ssam{
999153073Ssam	ni->ni_esslen = sp->ssid[1];
1000153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1001153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1002153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1003153073Ssam	ni->ni_intval = sp->bintval;
1004153073Ssam	ni->ni_capinfo = sp->capinfo;
1005153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1006153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1007153073Ssam	ni->ni_fhindex = sp->fhindex;
1008153073Ssam	ni->ni_erp = sp->erp;
1009153073Ssam	ni->ni_timoff = sp->timoff;
1010153073Ssam	if (sp->wme != NULL)
1011153073Ssam		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1012153073Ssam	if (sp->wpa != NULL)
1013153073Ssam		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1014170530Ssam	if (sp->rsn != NULL)
1015170530Ssam		ieee80211_saveie(&ni->ni_rsn_ie, sp->rsn);
1016170530Ssam	if (sp->ath != NULL)
1017170530Ssam		ieee80211_saveath(ni, sp->ath);
1018153073Ssam
1019153073Ssam	/* NB: must be after ni_chan is setup */
1020165887Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1021165887Ssam		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1022165887Ssam		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1023153073Ssam}
1024153073Ssam
1025148936Ssam/*
1026148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1027148936Ssam * or probe response frame.  Note that for the driver's
1028148936Ssam * benefit we we treat this like an association so the
1029148936Ssam * driver has an opportunity to setup it's private state.
1030148936Ssam */
1031148936Ssamstruct ieee80211_node *
1032148936Ssamieee80211_add_neighbor(struct ieee80211com *ic,
1033148936Ssam	const struct ieee80211_frame *wh,
1034148936Ssam	const struct ieee80211_scanparams *sp)
1035148936Ssam{
1036148936Ssam	struct ieee80211_node *ni;
1037148936Ssam
1038153073Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1039153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1040148936Ssam	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1041148936Ssam	if (ni != NULL) {
1042153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1043148936Ssam		if (ic->ic_newassoc != NULL)
1044148936Ssam			ic->ic_newassoc(ni, 1);
1045148936Ssam		/* XXX not right for 802.1x/WPA */
1046148936Ssam		ieee80211_node_authorize(ni);
1047148936Ssam	}
1048148936Ssam	return ni;
1049148936Ssam}
1050148936Ssam
1051148863Ssam#define	IS_CTL(wh) \
1052148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1053148863Ssam#define	IS_PSPOLL(wh) \
1054148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1055170530Ssam#define	IS_BAR(wh) \
1056170530Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1057170530Ssam
1058138568Ssam/*
1059138568Ssam * Locate the node for sender, track state, and then pass the
1060138568Ssam * (referenced) node up to the 802.11 layer for its use.  We
1061138568Ssam * are required to pass some node so we fall back to ic_bss
1062138568Ssam * when this frame is from an unknown sender.  The 802.11 layer
1063138568Ssam * knows this means the sender wasn't in the node table and
1064138568Ssam * acts accordingly.
1065138568Ssam */
1066138568Ssamstruct ieee80211_node *
1067138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1068138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1069138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1070138568Ssam#else
1071138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1072138568Ssam	const struct ieee80211_frame_min *wh)
1073138568Ssam#endif
1074138568Ssam{
1075138568Ssam	struct ieee80211_node_table *nt;
1076138568Ssam	struct ieee80211_node *ni;
1077138568Ssam
1078138568Ssam	/* XXX check ic_bss first in station mode */
1079138568Ssam	/* XXX 4-address frames? */
1080170530Ssam	nt = &ic->ic_sta;
1081138568Ssam	IEEE80211_NODE_LOCK(nt);
1082170530Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1083138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr1);
1084138568Ssam	else
1085138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr2);
1086148863Ssam	if (ni == NULL)
1087148863Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1088138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1089138568Ssam
1090148863Ssam	return ni;
1091148863Ssam}
1092148863Ssam
1093148863Ssam/*
1094148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1095148863Ssam * key index as a hint to locate the node in the key
1096148863Ssam * mapping table.  If an entry is present at the key
1097148863Ssam * index we return it; otherwise do a normal lookup and
1098148863Ssam * update the mapping table if the station has a unicast
1099148863Ssam * key assigned to it.
1100148863Ssam */
1101148863Ssamstruct ieee80211_node *
1102148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1103148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1104148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1105148863Ssam	const char *func, int line)
1106148863Ssam#else
1107148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1108148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1109148863Ssam#endif
1110148863Ssam{
1111148863Ssam	struct ieee80211_node_table *nt;
1112148863Ssam	struct ieee80211_node *ni;
1113148863Ssam
1114170530Ssam	nt = &ic->ic_sta;
1115148863Ssam	IEEE80211_NODE_LOCK(nt);
1116148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1117148863Ssam		ni = nt->nt_keyixmap[keyix];
1118148863Ssam	else
1119148863Ssam		ni = NULL;
1120148863Ssam	if (ni == NULL) {
1121170530Ssam		if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1122148863Ssam			ni = _ieee80211_find_node(nt, wh->i_addr1);
1123148863Ssam		else
1124148863Ssam			ni = _ieee80211_find_node(nt, wh->i_addr2);
1125148863Ssam		if (ni == NULL)
1126148863Ssam			ni = ieee80211_ref_node(ic->ic_bss);
1127148863Ssam		if (nt->nt_keyixmap != NULL) {
1128148863Ssam			/*
1129148863Ssam			 * If the station has a unicast key cache slot
1130148863Ssam			 * assigned update the key->node mapping table.
1131148863Ssam			 */
1132148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1133148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1134148863Ssam			if (keyix < nt->nt_keyixmax &&
1135148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1136148863Ssam				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1137148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1138148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1139148863Ssam				    ieee80211_node_refcnt(ni)+1);
1140148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1141148863Ssam			}
1142148863Ssam		}
1143170530Ssam	} else
1144148863Ssam		ieee80211_ref_node(ni);
1145148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1146148863Ssam
1147148863Ssam	return ni;
1148148863Ssam}
1149170530Ssam#undef IS_BAR
1150138568Ssam#undef IS_PSPOLL
1151138568Ssam#undef IS_CTL
1152138568Ssam
1153138568Ssam/*
1154127772Ssam * Return a reference to the appropriate node for sending
1155127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1156127772Ssam */
1157127772Ssamstruct ieee80211_node *
1158138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1159170530Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const uint8_t *macaddr,
1160138568Ssam	const char *func, int line)
1161138568Ssam#else
1162170530Ssamieee80211_find_txnode(struct ieee80211com *ic, const uint8_t *macaddr)
1163138568Ssam#endif
1164127772Ssam{
1165140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1166127772Ssam	struct ieee80211_node *ni;
1167127772Ssam
1168127772Ssam	/*
1169127772Ssam	 * The destination address should be in the node table
1170148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1171148863Ssam	 * also optimize station mode operation, all frames go
1172148863Ssam	 * to the bss node.
1173127772Ssam	 */
1174127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1175138568Ssam	IEEE80211_NODE_LOCK(nt);
1176148863Ssam	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1177148863Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1178158121Ssam	else {
1179148863Ssam		ni = _ieee80211_find_node(nt, macaddr);
1180158121Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1181158121Ssam		    (ni != NULL && ni->ni_associd == 0)) {
1182158121Ssam			/*
1183158121Ssam			 * Station is not associated; don't permit the
1184158121Ssam			 * data frame to be sent by returning NULL.  This
1185158121Ssam			 * is kinda a kludge but the least intrusive way
1186158121Ssam			 * to add this check into all drivers.
1187158121Ssam			 */
1188158121Ssam			ieee80211_unref_node(&ni);	/* NB: null's ni */
1189158121Ssam		}
1190158121Ssam	}
1191138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1192138568Ssam
1193138568Ssam	if (ni == NULL) {
1194138568Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1195140497Ssam		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
1196140497Ssam			/*
1197140497Ssam			 * In adhoc mode cons up a node for the destination.
1198140497Ssam			 * Note that we need an additional reference for the
1199140497Ssam			 * caller to be consistent with _ieee80211_find_node.
1200140497Ssam			 */
1201138568Ssam			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1202140497Ssam			if (ni != NULL)
1203140497Ssam				(void) ieee80211_ref_node(ni);
1204140497Ssam		} else {
1205138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1206138568Ssam				"[%s] no node, discard frame (%s)\n",
1207138568Ssam				ether_sprintf(macaddr), __func__);
1208138568Ssam			ic->ic_stats.is_tx_nonode++;
1209127772Ssam		}
1210127772Ssam	}
1211127772Ssam	return ni;
1212127772Ssam}
1213127772Ssam
1214127772Ssam/*
1215138568Ssam * Like find but search based on the ssid too.
1216138568Ssam */
1217138568Ssamstruct ieee80211_node *
1218138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1219138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1220170530Ssam	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid,
1221138568Ssam	const char *func, int line)
1222138568Ssam#else
1223138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1224170530Ssam	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid)
1225138568Ssam#endif
1226138568Ssam{
1227147118Ssam#define	MATCH_SSID(ni, ssid, ssidlen) \
1228147118Ssam	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1229170530Ssam	static const uint8_t zeromac[IEEE80211_ADDR_LEN];
1230138568Ssam	struct ieee80211_node *ni;
1231138568Ssam	int hash;
1232138568Ssam
1233138568Ssam	IEEE80211_NODE_LOCK(nt);
1234147118Ssam	/*
1235147118Ssam	 * A mac address that is all zero means match only the ssid;
1236147118Ssam	 * otherwise we must match both.
1237147118Ssam	 */
1238147118Ssam	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1239147118Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1240147118Ssam			if (MATCH_SSID(ni, ssid, ssidlen))
1241147118Ssam				break;
1242147118Ssam		}
1243147118Ssam	} else {
1244147118Ssam		hash = IEEE80211_NODE_HASH(macaddr);
1245147118Ssam		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1246147118Ssam			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1247147118Ssam			    MATCH_SSID(ni, ssid, ssidlen))
1248147118Ssam				break;
1249147118Ssam		}
1250147118Ssam	}
1251147118Ssam	if (ni != NULL) {
1252147118Ssam		ieee80211_ref_node(ni);	/* mark referenced */
1253170530Ssam		IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1254159139Sdds		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
1255147118Ssam		     ieee80211_node_refcnt(ni));
1256138568Ssam	}
1257138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1258138568Ssam	return ni;
1259147118Ssam#undef MATCH_SSID
1260138568Ssam}
1261138568Ssam
1262116742Ssamstatic void
1263138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1264116742Ssam{
1265138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1266138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1267119150Ssam
1268138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1269140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1270140766Ssam		ether_sprintf(ni->ni_macaddr),
1271138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1272138568Ssam
1273138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1274138568Ssam	if (nt != NULL) {
1275138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1276138568Ssam		LIST_REMOVE(ni, ni_hash);
1277138568Ssam	}
1278138568Ssam	ic->ic_node_free(ni);
1279116742Ssam}
1280116742Ssam
1281116742Ssamvoid
1282138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1283138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1284138568Ssam#else
1285138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1286138568Ssam#endif
1287116742Ssam{
1288138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1289119150Ssam
1290138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1291140454Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1292140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1293138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1294138568Ssam#endif
1295148863Ssam	if (nt != NULL) {
1296148863Ssam		IEEE80211_NODE_LOCK(nt);
1297148863Ssam		if (ieee80211_node_dectestref(ni)) {
1298148863Ssam			/*
1299148863Ssam			 * Last reference, reclaim state.
1300148863Ssam			 */
1301138568Ssam			_ieee80211_free_node(ni);
1302148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1303148863Ssam		    nt->nt_keyixmap != NULL) {
1304148863Ssam			ieee80211_keyix keyix;
1305148863Ssam			/*
1306148863Ssam			 * Check for a last reference in the key mapping table.
1307148863Ssam			 */
1308148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1309148863Ssam			if (keyix < nt->nt_keyixmax &&
1310148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1311148863Ssam				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1312148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1313148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1314148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1315148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1316148863Ssam				_ieee80211_free_node(ni);
1317148863Ssam			}
1318148863Ssam		}
1319148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1320148863Ssam	} else {
1321148863Ssam		if (ieee80211_node_dectestref(ni))
1322138568Ssam			_ieee80211_free_node(ni);
1323116742Ssam	}
1324116742Ssam}
1325116742Ssam
1326138568Ssam/*
1327148863Ssam * Reclaim a unicast key and clear any key cache state.
1328148863Ssam */
1329148863Ssamint
1330148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1331148863Ssam{
1332148863Ssam	struct ieee80211com *ic = ni->ni_ic;
1333148863Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1334148863Ssam	struct ieee80211_node *nikey;
1335148863Ssam	ieee80211_keyix keyix;
1336148863Ssam	int isowned, status;
1337148863Ssam
1338148863Ssam	/*
1339148863Ssam	 * NB: We must beware of LOR here; deleting the key
1340148863Ssam	 * can cause the crypto layer to block traffic updates
1341148863Ssam	 * which can generate a LOR against the node table lock;
1342148863Ssam	 * grab it here and stash the key index for our use below.
1343148863Ssam	 *
1344148863Ssam	 * Must also beware of recursion on the node table lock.
1345148863Ssam	 * When called from node_cleanup we may already have
1346148863Ssam	 * the node table lock held.  Unfortunately there's no
1347148863Ssam	 * way to separate out this path so we must do this
1348148863Ssam	 * conditionally.
1349148863Ssam	 */
1350148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1351148863Ssam	if (!isowned)
1352148863Ssam		IEEE80211_NODE_LOCK(nt);
1353148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1354148863Ssam	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1355148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1356148863Ssam		nikey = nt->nt_keyixmap[keyix];
1357148863Ssam		nt->nt_keyixmap[keyix] = NULL;;
1358148863Ssam	} else
1359148863Ssam		nikey = NULL;
1360148863Ssam	if (!isowned)
1361148863Ssam		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1362148863Ssam
1363148863Ssam	if (nikey != NULL) {
1364148863Ssam		KASSERT(nikey == ni,
1365148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1366148863Ssam		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1367148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1368148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1369148863Ssam			ieee80211_node_refcnt(ni)-1);
1370148863Ssam		ieee80211_free_node(ni);
1371148863Ssam	}
1372148863Ssam	return status;
1373148863Ssam}
1374148863Ssam
1375148863Ssam/*
1376138568Ssam * Reclaim a node.  If this is the last reference count then
1377138568Ssam * do the normal free work.  Otherwise remove it from the node
1378138568Ssam * table and mark it gone by clearing the back-reference.
1379138568Ssam */
1380138568Ssamstatic void
1381138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1382116742Ssam{
1383148863Ssam	ieee80211_keyix keyix;
1384138568Ssam
1385148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1386148863Ssam
1387140766Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1388140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1389140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1390140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1391148863Ssam	/*
1392148863Ssam	 * Clear any entry in the unicast key mapping table.
1393148863Ssam	 * We need to do it here so rx lookups don't find it
1394148863Ssam	 * in the mapping table even if it's not in the hash
1395148863Ssam	 * table.  We cannot depend on the mapping table entry
1396148863Ssam	 * being cleared because the node may not be free'd.
1397148863Ssam	 */
1398148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1399148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1400148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1401148863Ssam		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1402148863Ssam			"%s: %p<%s> clear key map entry\n",
1403148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr));
1404148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1405148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1406148863Ssam	}
1407138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1408138568Ssam		/*
1409138568Ssam		 * Other references are present, just remove the
1410138568Ssam		 * node from the table so it cannot be found.  When
1411138568Ssam		 * the references are dropped storage will be
1412140753Ssam		 * reclaimed.
1413138568Ssam		 */
1414138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1415138568Ssam		LIST_REMOVE(ni, ni_hash);
1416138568Ssam		ni->ni_table = NULL;		/* clear reference */
1417138568Ssam	} else
1418138568Ssam		_ieee80211_free_node(ni);
1419138568Ssam}
1420138568Ssam
1421138568Ssamstatic void
1422138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1423138568Ssam{
1424138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1425116742Ssam	struct ieee80211_node *ni;
1426116742Ssam
1427138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1428138568Ssam		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1429138568Ssam
1430138568Ssam	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1431138568Ssam		if (ni->ni_associd != 0) {
1432138568Ssam			if (ic->ic_auth->ia_node_leave != NULL)
1433138568Ssam				ic->ic_auth->ia_node_leave(ic, ni);
1434138568Ssam			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1435138568Ssam		}
1436138568Ssam		node_reclaim(nt, ni);
1437138568Ssam	}
1438116742Ssam}
1439116742Ssam
1440120483Ssam/*
1441138568Ssam * Timeout inactive stations and do related housekeeping.
1442138568Ssam * Note that we cannot hold the node lock while sending a
1443138568Ssam * frame as this would lead to a LOR.  Instead we use a
1444138568Ssam * generation number to mark nodes that we've scanned and
1445138568Ssam * drop the lock and restart a scan if we have to time out
1446138568Ssam * a node.  Since we are single-threaded by virtue of
1447120483Ssam * controlling the inactivity timer we can be sure this will
1448120483Ssam * process each node only once.
1449120483Ssam */
1450138568Ssamstatic void
1451138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt)
1452116742Ssam{
1453138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1454120483Ssam	struct ieee80211_node *ni;
1455138568Ssam	u_int gen;
1456148320Ssam	int isadhoc;
1457116742Ssam
1458148320Ssam	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1459148320Ssam		   ic->ic_opmode == IEEE80211_M_AHDEMO);
1460138568Ssam	IEEE80211_SCAN_LOCK(nt);
1461154532Ssam	gen = ++nt->nt_scangen;
1462120483Ssamrestart:
1463138568Ssam	IEEE80211_NODE_LOCK(nt);
1464138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1465120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1466120483Ssam			continue;
1467120483Ssam		ni->ni_scangen = gen;
1468138568Ssam		/*
1469147788Ssam		 * Ignore entries for which have yet to receive an
1470147788Ssam		 * authentication frame.  These are transient and
1471147788Ssam		 * will be reclaimed when the last reference to them
1472147788Ssam		 * goes away (when frame xmits complete).
1473147788Ssam		 */
1474170530Ssam		if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
1475170530Ssam		     ic->ic_opmode == IEEE80211_M_STA) &&
1476148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1477147788Ssam			continue;
1478147788Ssam		/*
1479138568Ssam		 * Free fragment if not needed anymore
1480138568Ssam		 * (last fragment older than 1s).
1481138568Ssam		 * XXX doesn't belong here
1482138568Ssam		 */
1483138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1484138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1485138568Ssam			m_freem(ni->ni_rxfrag[0]);
1486138568Ssam			ni->ni_rxfrag[0] = NULL;
1487138568Ssam		}
1488172062Ssam		if (ni->ni_inact > 0)
1489172062Ssam			ni->ni_inact--;
1490140498Ssam		/*
1491140498Ssam		 * Special case ourself; we may be idle for extended periods
1492140498Ssam		 * of time and regardless reclaiming our state is wrong.
1493140498Ssam		 */
1494140498Ssam		if (ni == ic->ic_bss)
1495140498Ssam			continue;
1496148320Ssam		if (ni->ni_associd != 0 || isadhoc) {
1497119150Ssam			/*
1498170530Ssam			 * Age frames on the power save queue.
1499138568Ssam			 */
1500170530Ssam			if (ieee80211_node_saveq_age(ni) != 0 &&
1501170530Ssam			    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
1502170530Ssam			    ic->ic_set_tim != NULL)
1503170530Ssam				ic->ic_set_tim(ni, 0);
1504138568Ssam			/*
1505138568Ssam			 * Probe the station before time it out.  We
1506138568Ssam			 * send a null data frame which may not be
1507138568Ssam			 * universally supported by drivers (need it
1508138568Ssam			 * for ps-poll support so it should be...).
1509170530Ssam			 *
1510170530Ssam			 * XXX don't probe the station unless we've
1511170530Ssam			 *     received a frame from them (and have
1512170530Ssam			 *     some idea of the rates they are capable
1513170530Ssam			 *     of); this will get fixed more properly
1514170530Ssam			 *     soon with better handling of the rate set.
1515138568Ssam			 */
1516172062Ssam			if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1517172062Ssam			    (0 < ni->ni_inact &&
1518172062Ssam			     ni->ni_inact <= ic->ic_inact_probe) &&
1519170530Ssam			    ni->ni_rates.rs_nrates != 0) {
1520148320Ssam				IEEE80211_NOTE(ic,
1521148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1522148320Ssam				    ni, "%s",
1523148320Ssam				    "probe station due to inactivity");
1524148582Ssam				/*
1525148582Ssam				 * Grab a reference before unlocking the table
1526148582Ssam				 * so the node cannot be reclaimed before we
1527148582Ssam				 * send the frame. ieee80211_send_nulldata
1528148582Ssam				 * understands we've done this and reclaims the
1529148582Ssam				 * ref for us as needed.
1530148582Ssam				 */
1531148582Ssam				ieee80211_ref_node(ni);
1532138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1533148301Ssam				ieee80211_send_nulldata(ni);
1534138568Ssam				/* XXX stat? */
1535138568Ssam				goto restart;
1536138568Ssam			}
1537138568Ssam		}
1538172062Ssam		if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1539172062Ssam		    ni->ni_inact <= 0) {
1540148320Ssam			IEEE80211_NOTE(ic,
1541148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1542148320Ssam			    "station timed out due to inactivity "
1543148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1544138568Ssam			/*
1545138568Ssam			 * Send a deauthenticate frame and drop the station.
1546138568Ssam			 * This is somewhat complicated due to reference counts
1547138568Ssam			 * and locking.  At this point a station will typically
1548138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1549138568Ssam			 * will do a "free" of the node which will drop the
1550138568Ssam			 * reference count.  But in the meantime a reference
1551138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1552138568Ssam			 * of the node will happen either after the tx is
1553138568Ssam			 * completed or by ieee80211_node_leave.
1554120483Ssam			 *
1555138568Ssam			 * Separately we must drop the node lock before sending
1556170530Ssam			 * in case the driver takes a lock, as this can result
1557170530Ssam			 * in a LOR between the node lock and the driver lock.
1558119150Ssam			 */
1559172229Ssam			ieee80211_ref_node(ni);
1560138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1561138568Ssam			if (ni->ni_associd != 0) {
1562138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
1563138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1564138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1565138568Ssam			}
1566138568Ssam			ieee80211_node_leave(ic, ni);
1567172229Ssam			ieee80211_free_node(ni);
1568121180Ssam			ic->ic_stats.is_node_timeout++;
1569120483Ssam			goto restart;
1570120483Ssam		}
1571116742Ssam	}
1572138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1573138568Ssam
1574138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1575170530Ssam}
1576138568Ssam
1577170530Ssamvoid
1578170530Ssamieee80211_node_timeout(void *arg)
1579170530Ssam{
1580170530Ssam	struct ieee80211com *ic = arg;
1581170530Ssam
1582170530Ssam	ieee80211_scan_timeout(ic);
1583170530Ssam	ieee80211_timeout_stations(&ic->ic_sta);
1584170530Ssam
1585172211Ssam	IEEE80211_LOCK(ic);
1586172211Ssam	ieee80211_erp_timeout(ic);
1587173273Ssam	ieee80211_ht_timeout(ic);
1588172211Ssam	IEEE80211_UNLOCK(ic);
1589172211Ssam
1590170530Ssam	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
1591170530Ssam		ieee80211_node_timeout, ic);
1592116742Ssam}
1593116742Ssam
1594116742Ssamvoid
1595138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1596116742Ssam{
1597116742Ssam	struct ieee80211_node *ni;
1598138568Ssam	u_int gen;
1599116742Ssam
1600138568Ssam	IEEE80211_SCAN_LOCK(nt);
1601154532Ssam	gen = ++nt->nt_scangen;
1602138568Ssamrestart:
1603138568Ssam	IEEE80211_NODE_LOCK(nt);
1604138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1605138568Ssam		if (ni->ni_scangen != gen) {
1606138568Ssam			ni->ni_scangen = gen;
1607138568Ssam			(void) ieee80211_ref_node(ni);
1608138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1609138568Ssam			(*f)(arg, ni);
1610138568Ssam			ieee80211_free_node(ni);
1611138568Ssam			goto restart;
1612138568Ssam		}
1613138568Ssam	}
1614138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1615138568Ssam
1616138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1617116742Ssam}
1618138568Ssam
1619138568Ssamvoid
1620138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1621138568Ssam{
1622138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
1623138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1624138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
1625138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1626138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
1627138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1628138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1629167439Ssam		ni->ni_txseqs[IEEE80211_NONQOS_TID],
1630167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
1631167439Ssam		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
1632138568Ssam		ni->ni_rxfragstamp);
1633170530Ssam	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
1634170530Ssam		ni->ni_rstamp, ni->ni_rssi, ni->ni_noise,
1635170530Ssam		ni->ni_intval, ni->ni_capinfo);
1636138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1637138568Ssam		ether_sprintf(ni->ni_bssid),
1638138568Ssam		ni->ni_esslen, ni->ni_essid,
1639138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1640138568Ssam	printf("\tfails %u inact %u txrate %u\n",
1641138568Ssam		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1642170530Ssam	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
1643170530Ssam		ni->ni_htcap, ni->ni_htparam,
1644170530Ssam		ni->ni_htctlchan, ni->ni_ht2ndchan);
1645170530Ssam	printf("\thtopmode %x htstbc %x chw %u\n",
1646170530Ssam		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
1647138568Ssam}
1648138568Ssam
1649138568Ssamvoid
1650138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
1651138568Ssam{
1652138568Ssam	ieee80211_iterate_nodes(nt,
1653138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
1654138568Ssam}
1655138568Ssam
1656172211Ssamvoid
1657172211Ssamieee80211_notify_erp(struct ieee80211com *ic)
1658172211Ssam{
1659172211Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1660172211Ssam		ieee80211_beacon_notify(ic, IEEE80211_BEACON_ERP);
1661172211Ssam}
1662172211Ssam
1663138568Ssam/*
1664138568Ssam * Handle a station joining an 11g network.
1665138568Ssam */
1666138568Ssamstatic void
1667138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1668138568Ssam{
1669138568Ssam
1670173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1671173273Ssam
1672138568Ssam	/*
1673138568Ssam	 * Station isn't capable of short slot time.  Bump
1674138568Ssam	 * the count of long slot time stations and disable
1675138568Ssam	 * use of short slot time.  Note that the actual switch
1676138568Ssam	 * over to long slot time use may not occur until the
1677138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1678138568Ssam	 */
1679138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1680138568Ssam		ic->ic_longslotsta++;
1681172211Ssam		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1682172211Ssam		    "station needs long slot time, count %d",
1683172211Ssam		    ic->ic_longslotsta);
1684138568Ssam		/* XXX vap's w/ conflicting needs won't work */
1685170530Ssam		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
1686170530Ssam			/*
1687170530Ssam			 * Don't force slot time when switched to turbo
1688170530Ssam			 * mode as non-ERP stations won't be present; this
1689170530Ssam			 * need only be done when on the normal G channel.
1690170530Ssam			 */
1691170530Ssam			ieee80211_set_shortslottime(ic, 0);
1692170530Ssam		}
1693138568Ssam	}
1694138568Ssam	/*
1695138568Ssam	 * If the new station is not an ERP station
1696138568Ssam	 * then bump the counter and enable protection
1697138568Ssam	 * if configured.
1698138568Ssam	 */
1699138568Ssam	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1700138568Ssam		ic->ic_nonerpsta++;
1701172211Ssam		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1702172211Ssam		    "station is !ERP, %d non-ERP stations associated",
1703172211Ssam		    ic->ic_nonerpsta);
1704138568Ssam		/*
1705138568Ssam		 * If station does not support short preamble
1706138568Ssam		 * then we must enable use of Barker preamble.
1707138568Ssam		 */
1708138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1709172211Ssam			IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1710172211Ssam			    "%s", "station needs long preamble");
1711138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1712138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1713138568Ssam		}
1714172211Ssam		/*
1715172211Ssam		 * If protection is configured, enable it.
1716172211Ssam		 */
1717172211Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1718172211Ssam		    ic->ic_nonerpsta == 1 &&
1719172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
1720172211Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1721172211Ssam			    "%s: enable use of protection\n", __func__);
1722172211Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1723172211Ssam			ieee80211_notify_erp(ic);
1724172211Ssam		}
1725138568Ssam	} else
1726138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
1727138568Ssam}
1728138568Ssam
1729138568Ssamvoid
1730138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1731138568Ssam{
1732138568Ssam	int newassoc;
1733138568Ssam
1734138568Ssam	if (ni->ni_associd == 0) {
1735170530Ssam		uint16_t aid;
1736138568Ssam
1737173273Ssam		IEEE80211_LOCK(ic);
1738138568Ssam		/*
1739138568Ssam		 * It would be good to search the bitmap
1740138568Ssam		 * more efficiently, but this will do for now.
1741138568Ssam		 */
1742138568Ssam		for (aid = 1; aid < ic->ic_max_aid; aid++) {
1743138568Ssam			if (!IEEE80211_AID_ISSET(aid,
1744138568Ssam			    ic->ic_aid_bitmap))
1745138568Ssam				break;
1746138568Ssam		}
1747138568Ssam		if (aid >= ic->ic_max_aid) {
1748173273Ssam			IEEE80211_UNLOCK(ic);
1749138568Ssam			IEEE80211_SEND_MGMT(ic, ni, resp,
1750138568Ssam			    IEEE80211_REASON_ASSOC_TOOMANY);
1751138568Ssam			ieee80211_node_leave(ic, ni);
1752138568Ssam			return;
1753138568Ssam		}
1754138568Ssam		ni->ni_associd = aid | 0xc000;
1755173273Ssam		ni->ni_jointime = time_uptime;
1756138568Ssam		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1757138568Ssam		ic->ic_sta_assoc++;
1758173273Ssam
1759173273Ssam		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
1760173273Ssam			ieee80211_ht_node_join(ni);
1761170530Ssam		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
1762170530Ssam		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
1763138568Ssam			ieee80211_node_join_11g(ic, ni);
1764173273Ssam		IEEE80211_UNLOCK(ic);
1765173273Ssam
1766173273Ssam		newassoc = 1;
1767138568Ssam	} else
1768138568Ssam		newassoc = 0;
1769138568Ssam
1770172211Ssam	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1771173273Ssam	    "station %sassociated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s",
1772172211Ssam	    newassoc ? "" : "re",
1773139523Ssam	    IEEE80211_NODE_AID(ni),
1774139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1775139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1776139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1777170530Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1778170530Ssam	    ni->ni_flags & IEEE80211_NODE_HT ?
1779170530Ssam		(ni->ni_chw == 20 ? ", HT20" : ", HT40") : "",
1780173273Ssam	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1781170530Ssam	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_FF) ?
1782170530Ssam		", fast-frames" : "",
1783170530Ssam	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_TURBOP) ?
1784170530Ssam		", turbo" : ""
1785139523Ssam	);
1786138568Ssam
1787138568Ssam	/* give driver a chance to setup state like ni_txrate */
1788139524Ssam	if (ic->ic_newassoc != NULL)
1789148307Ssam		ic->ic_newassoc(ni, newassoc);
1790138568Ssam	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1791138568Ssam	/* tell the authenticator about new station */
1792138568Ssam	if (ic->ic_auth->ia_node_join != NULL)
1793138568Ssam		ic->ic_auth->ia_node_join(ic, ni);
1794138568Ssam	ieee80211_notify_node_join(ic, ni, newassoc);
1795138568Ssam}
1796138568Ssam
1797172211Ssamstatic void
1798172211Ssamdisable_protection(struct ieee80211com *ic)
1799172211Ssam{
1800172211Ssam	KASSERT(ic->ic_nonerpsta == 0 &&
1801172211Ssam	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
1802172211Ssam	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
1803172211Ssam	   ic->ic_flags_ext));
1804172211Ssam
1805172211Ssam	ic->ic_flags &= ~IEEE80211_F_USEPROT;
1806172211Ssam	/* XXX verify mode? */
1807172211Ssam	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1808172211Ssam		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1809172211Ssam		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1810172211Ssam	}
1811172211Ssam	ieee80211_notify_erp(ic);
1812172211Ssam}
1813172211Ssam
1814138568Ssam/*
1815138568Ssam * Handle a station leaving an 11g network.
1816138568Ssam */
1817138568Ssamstatic void
1818138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1819138568Ssam{
1820138568Ssam
1821173273Ssam	IEEE80211_LOCK_ASSERT(ic);
1822173273Ssam
1823170530Ssam	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
1824170530Ssam	     ("not in 11g, bss %u:0x%x, curmode %u", ic->ic_bsschan->ic_freq,
1825170530Ssam	      ic->ic_bsschan->ic_flags, ic->ic_curmode));
1826138568Ssam
1827138568Ssam	/*
1828138568Ssam	 * If a long slot station do the slot time bookkeeping.
1829138568Ssam	 */
1830138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1831138568Ssam		KASSERT(ic->ic_longslotsta > 0,
1832138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
1833138568Ssam		ic->ic_longslotsta--;
1834172211Ssam		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1835172211Ssam		    "long slot time station leaves, count now %d",
1836172211Ssam		    ic->ic_longslotsta);
1837138568Ssam		if (ic->ic_longslotsta == 0) {
1838138568Ssam			/*
1839138568Ssam			 * Re-enable use of short slot time if supported
1840138568Ssam			 * and not operating in IBSS mode (per spec).
1841138568Ssam			 */
1842138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1843138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
1844138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1845138568Ssam				    "%s: re-enable use of short slot time\n",
1846138568Ssam				    __func__);
1847138568Ssam				ieee80211_set_shortslottime(ic, 1);
1848138568Ssam			}
1849138568Ssam		}
1850138568Ssam	}
1851138568Ssam	/*
1852138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
1853138568Ssam	 */
1854138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1855138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
1856138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1857138568Ssam		ic->ic_nonerpsta--;
1858172211Ssam		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1859172211Ssam		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
1860172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
1861172211Ssam			" (non-ERP sta present)" : "");
1862172211Ssam		if (ic->ic_nonerpsta == 0 &&
1863172211Ssam		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
1864138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1865138568Ssam				"%s: disable use of protection\n", __func__);
1866172211Ssam			disable_protection(ic);
1867138568Ssam		}
1868138568Ssam	}
1869138568Ssam}
1870138568Ssam
1871138568Ssam/*
1872172211Ssam * Time out presence of an overlapping bss with non-ERP
1873172211Ssam * stations.  When operating in hostap mode we listen for
1874172211Ssam * beacons from other stations and if we identify a non-ERP
1875172211Ssam * station is present we enable protection.  To identify
1876172211Ssam * when all non-ERP stations are gone we time out this
1877172211Ssam * condition.
1878172211Ssam */
1879172211Ssamstatic void
1880172211Ssamieee80211_erp_timeout(struct ieee80211com *ic)
1881172211Ssam{
1882172211Ssam
1883172211Ssam	IEEE80211_LOCK_ASSERT(ic);
1884172211Ssam
1885172211Ssam	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
1886172211Ssam	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
1887172211Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1888172211Ssam		    "%s\n", "age out non-ERP sta present on channel");
1889172211Ssam		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
1890172211Ssam		if (ic->ic_nonerpsta == 0)
1891172211Ssam			disable_protection(ic);
1892172211Ssam	}
1893172211Ssam}
1894172211Ssam
1895172211Ssam/*
1896138568Ssam * Handle bookkeeping for station deauthentication/disassociation
1897138568Ssam * when operating as an ap.
1898138568Ssam */
1899138568Ssamvoid
1900138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1901138568Ssam{
1902140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1903138568Ssam
1904172211Ssam	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1905172211Ssam	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
1906138568Ssam
1907170530Ssam	KASSERT(ic->ic_opmode != IEEE80211_M_STA,
1908138568Ssam		("unexpected operating mode %u", ic->ic_opmode));
1909138568Ssam	/*
1910138568Ssam	 * If node wasn't previously associated all
1911138568Ssam	 * we need to do is reclaim the reference.
1912138568Ssam	 */
1913138568Ssam	/* XXX ibss mode bypasses 11g and notification */
1914138568Ssam	if (ni->ni_associd == 0)
1915138568Ssam		goto done;
1916138568Ssam	/*
1917138568Ssam	 * Tell the authenticator the station is leaving.
1918138568Ssam	 * Note that we must do this before yanking the
1919138568Ssam	 * association id as the authenticator uses the
1920138568Ssam	 * associd to locate it's state block.
1921138568Ssam	 */
1922138568Ssam	if (ic->ic_auth->ia_node_leave != NULL)
1923138568Ssam		ic->ic_auth->ia_node_leave(ic, ni);
1924173273Ssam
1925173273Ssam	IEEE80211_LOCK(ic);
1926138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1927138568Ssam	ni->ni_associd = 0;
1928138568Ssam	ic->ic_sta_assoc--;
1929138568Ssam
1930173273Ssam	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
1931173273Ssam		ieee80211_ht_node_leave(ni);
1932170530Ssam	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
1933170530Ssam	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
1934138568Ssam		ieee80211_node_leave_11g(ic, ni);
1935173273Ssam	IEEE80211_UNLOCK(ic);
1936138568Ssam	/*
1937138568Ssam	 * Cleanup station state.  In particular clear various
1938138568Ssam	 * state that might otherwise be reused if the node
1939138568Ssam	 * is reused before the reference count goes to zero
1940138568Ssam	 * (and memory is reclaimed).
1941138568Ssam	 */
1942138568Ssam	ieee80211_sta_leave(ic, ni);
1943138568Ssamdone:
1944140499Ssam	/*
1945140499Ssam	 * Remove the node from any table it's recorded in and
1946140499Ssam	 * drop the caller's reference.  Removal from the table
1947140499Ssam	 * is important to insure the node is not reprocessed
1948140499Ssam	 * for inactivity.
1949140499Ssam	 */
1950140499Ssam	if (nt != NULL) {
1951140499Ssam		IEEE80211_NODE_LOCK(nt);
1952140499Ssam		node_reclaim(nt, ni);
1953140499Ssam		IEEE80211_NODE_UNLOCK(nt);
1954140499Ssam	} else
1955140499Ssam		ieee80211_free_node(ni);
1956138568Ssam}
1957138568Ssam
1958170530Ssamint8_t
1959138568Ssamieee80211_getrssi(struct ieee80211com *ic)
1960138568Ssam{
1961138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
1962140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1963170530Ssam	int rssi_samples;
1964170530Ssam	int32_t rssi_total;
1965138568Ssam	struct ieee80211_node *ni;
1966138568Ssam
1967138568Ssam	rssi_total = 0;
1968138568Ssam	rssi_samples = 0;
1969138568Ssam	switch (ic->ic_opmode) {
1970138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
1971138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
1972138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
1973138568Ssam		/* XXX locking */
1974140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1975170530Ssam			if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1976170530Ssam			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS)) {
1977170530Ssam				int8_t rssi = ic->ic_node_getrssi(ni);
1978170530Ssam				if (rssi != 0) {
1979170530Ssam					rssi_samples++;
1980170530Ssam					rssi_total += rssi;
1981170530Ssam				}
1982138568Ssam			}
1983138568Ssam		break;
1984138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
1985138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
1986138568Ssam	default:
1987138568Ssam		if (ic->ic_bss != NULL)
1988138568Ssam			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1989138568Ssam		rssi_samples = 1;
1990138568Ssam		break;
1991138568Ssam	}
1992138568Ssam	return rssi_total / NZ(rssi_samples);
1993138568Ssam#undef NZ
1994138568Ssam}
1995138568Ssam
1996170530Ssamvoid
1997170530Ssamieee80211_getsignal(struct ieee80211com *ic, int8_t *rssi, int8_t *noise)
1998138568Ssam{
1999138568Ssam
2000170530Ssam	if (ic->ic_bss == NULL)		/* NB: shouldn't happen */
2001170530Ssam		return;
2002170530Ssam	ic->ic_node_getsignal(ic->ic_bss, rssi, noise);
2003170530Ssam	/* for non-station mode return avg'd rssi accounting */
2004170530Ssam	if (ic->ic_opmode != IEEE80211_M_STA)
2005170530Ssam		*rssi = ieee80211_getrssi(ic);
2006138568Ssam}
2007138568Ssam
2008138568Ssam/*
2009138568Ssam * Node table support.
2010138568Ssam */
2011138568Ssam
2012138568Ssamstatic void
2013138568Ssamieee80211_node_table_init(struct ieee80211com *ic,
2014138568Ssam	struct ieee80211_node_table *nt,
2015170530Ssam	const char *name, int inact, int keyixmax)
2016138568Ssam{
2017138568Ssam
2018138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
2019138568Ssam		"%s %s table, inact %u\n", __func__, name, inact);
2020138568Ssam
2021138568Ssam	nt->nt_ic = ic;
2022138568Ssam	/* XXX need unit */
2023138568Ssam	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2024138568Ssam	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2025138568Ssam	TAILQ_INIT(&nt->nt_node);
2026138568Ssam	nt->nt_name = name;
2027138568Ssam	nt->nt_scangen = 1;
2028138568Ssam	nt->nt_inact_init = inact;
2029148863Ssam	nt->nt_keyixmax = keyixmax;
2030148863Ssam	if (nt->nt_keyixmax > 0) {
2031148863Ssam		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2032148863Ssam			keyixmax * sizeof(struct ieee80211_node *),
2033148863Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
2034148863Ssam		if (nt->nt_keyixmap == NULL)
2035148863Ssam			if_printf(ic->ic_ifp,
2036148863Ssam			    "Cannot allocate key index map with %u entries\n",
2037148863Ssam			    keyixmax);
2038148863Ssam	} else
2039148863Ssam		nt->nt_keyixmap = NULL;
2040138568Ssam}
2041138568Ssam
2042170530Ssamstatic void
2043138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt)
2044138568Ssam{
2045138568Ssam
2046138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2047138568Ssam		"%s %s table\n", __func__, nt->nt_name);
2048138568Ssam
2049138568Ssam	IEEE80211_NODE_LOCK(nt);
2050138568Ssam	ieee80211_free_allnodes_locked(nt);
2051138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2052138568Ssam}
2053138568Ssam
2054138568Ssamstatic void
2055138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2056138568Ssam{
2057138568Ssam
2058138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2059138568Ssam		"%s %s table\n", __func__, nt->nt_name);
2060138568Ssam
2061148863Ssam	IEEE80211_NODE_LOCK(nt);
2062138568Ssam	ieee80211_free_allnodes_locked(nt);
2063148863Ssam	if (nt->nt_keyixmap != NULL) {
2064148863Ssam		/* XXX verify all entries are NULL */
2065148863Ssam		int i;
2066148863Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
2067148863Ssam			if (nt->nt_keyixmap[i] != NULL)
2068148863Ssam				printf("%s: %s[%u] still active\n", __func__,
2069148863Ssam					nt->nt_name, i);
2070148863Ssam		FREE(nt->nt_keyixmap, M_80211_NODE);
2071148863Ssam		nt->nt_keyixmap = NULL;
2072148863Ssam	}
2073138568Ssam	IEEE80211_SCAN_LOCK_DESTROY(nt);
2074138568Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
2075138568Ssam}
2076