ieee80211_node.c revision 148320
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3139530Ssam * Copyright (c) 2002-2005 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.
14116904Ssam * 3. The name of the author may not be used to endorse or promote products
15116904Ssam *    derived from this software without specific prior written permission.
16116742Ssam *
17116742Ssam * Alternatively, this software may be distributed under the terms of the
18116742Ssam * GNU General Public License ("GPL") version 2 as published by the Free
19116742Ssam * Software Foundation.
20116742Ssam *
21116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31116742Ssam */
32116742Ssam
33116742Ssam#include <sys/cdefs.h>
34116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 148320 2005-07-22 23:25:46Z sam $");
35116742Ssam
36116742Ssam#include <sys/param.h>
37116742Ssam#include <sys/systm.h>
38116742Ssam#include <sys/mbuf.h>
39116742Ssam#include <sys/malloc.h>
40116742Ssam#include <sys/kernel.h>
41138568Ssam
42116742Ssam#include <sys/socket.h>
43116742Ssam
44116742Ssam#include <net/if.h>
45116742Ssam#include <net/if_media.h>
46116742Ssam#include <net/ethernet.h>
47116742Ssam
48116742Ssam#include <net80211/ieee80211_var.h>
49116742Ssam
50116742Ssam#include <net/bpf.h>
51116742Ssam
52147221Ssam/*
53147221Ssam * Association id's are managed with a bit vector.
54147221Ssam */
55147221Ssam#define	IEEE80211_AID_SET(b, w) \
56147221Ssam	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
57147221Ssam#define	IEEE80211_AID_CLR(b, w) \
58147221Ssam	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
59147221Ssam#define	IEEE80211_AID_ISSET(b, w) \
60147221Ssam	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
61147221Ssam
62138568Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
63138568Ssamstatic void node_cleanup(struct ieee80211_node *);
64138568Ssamstatic void node_free(struct ieee80211_node *);
65138568Ssamstatic u_int8_t node_getrssi(const struct ieee80211_node *);
66116742Ssam
67138568Ssamstatic void ieee80211_setup_node(struct ieee80211_node_table *,
68138568Ssam		struct ieee80211_node *, const u_int8_t *);
69138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
70138568Ssamstatic void ieee80211_free_allnodes(struct ieee80211_node_table *);
71120104Ssam
72138568Ssamstatic void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
73138568Ssamstatic void ieee80211_timeout_stations(struct ieee80211_node_table *);
74116742Ssam
75148304Ssamstatic void ieee80211_set_tim(struct ieee80211_node *, int set);
76138568Ssam
77138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
78138568Ssam	struct ieee80211_node_table *nt, const char *name, int inact,
79138568Ssam	void (*timeout)(struct ieee80211_node_table *));
80138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
81138568Ssam
82127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
83120481Ssam
84116742Ssamvoid
85138568Ssamieee80211_node_attach(struct ieee80211com *ic)
86116742Ssam{
87116742Ssam
88140753Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
89140753Ssam		IEEE80211_INACT_INIT, ieee80211_timeout_stations);
90138568Ssam	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
91138568Ssam		IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates);
92138568Ssam
93138568Ssam	ic->ic_node_alloc = node_alloc;
94138568Ssam	ic->ic_node_free = node_free;
95138568Ssam	ic->ic_node_cleanup = node_cleanup;
96138568Ssam	ic->ic_node_getrssi = node_getrssi;
97138568Ssam
98138568Ssam	/* default station inactivity timer setings */
99138568Ssam	ic->ic_inact_init = IEEE80211_INACT_INIT;
100138568Ssam	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
101138568Ssam	ic->ic_inact_run = IEEE80211_INACT_RUN;
102138568Ssam	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
103138568Ssam
104138568Ssam	/* XXX defer */
105138568Ssam	if (ic->ic_max_aid == 0)
106138568Ssam		ic->ic_max_aid = IEEE80211_AID_DEF;
107138568Ssam	else if (ic->ic_max_aid > IEEE80211_AID_MAX)
108138568Ssam		ic->ic_max_aid = IEEE80211_AID_MAX;
109138568Ssam	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
110138568Ssam		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
111138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
112138568Ssam	if (ic->ic_aid_bitmap == NULL) {
113138568Ssam		/* XXX no way to recover */
114138568Ssam		printf("%s: no memory for AID bitmap!\n", __func__);
115138568Ssam		ic->ic_max_aid = 0;
116138568Ssam	}
117138568Ssam
118138568Ssam	/* XXX defer until using hostap/ibss mode */
119138568Ssam	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
120138568Ssam	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
121138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
122138568Ssam	if (ic->ic_tim_bitmap == NULL) {
123138568Ssam		/* XXX no way to recover */
124138568Ssam		printf("%s: no memory for TIM bitmap!\n", __func__);
125138568Ssam	}
126138568Ssam	ic->ic_set_tim = ieee80211_set_tim;	/* NB: driver should override */
127118887Ssam}
128118887Ssam
129118887Ssamvoid
130138568Ssamieee80211_node_lateattach(struct ieee80211com *ic)
131118887Ssam{
132127766Ssam	struct ieee80211_node *ni;
133138568Ssam	struct ieee80211_rsnparms *rsn;
134118887Ssam
135138568Ssam	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
136127766Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
137138568Ssam	/*
138138568Ssam	 * Setup "global settings" in the bss node so that
139138568Ssam	 * each new station automatically inherits them.
140138568Ssam	 */
141138568Ssam	rsn = &ni->ni_rsn;
142138568Ssam	/* WEP, TKIP, and AES-CCM are always supported */
143138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
144138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
145138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
146138568Ssam	if (ic->ic_caps & IEEE80211_C_AES)
147138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
148138568Ssam	if (ic->ic_caps & IEEE80211_C_CKIP)
149138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
150138568Ssam	/*
151138568Ssam	 * Default unicast cipher to WEP for 802.1x use.  If
152138568Ssam	 * WPA is enabled the management code will set these
153138568Ssam	 * values to reflect.
154138568Ssam	 */
155138568Ssam	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
156138568Ssam	rsn->rsn_ucastkeylen = 104 / NBBY;
157138568Ssam	/*
158138568Ssam	 * WPA says the multicast cipher is the lowest unicast
159138568Ssam	 * cipher supported.  But we skip WEP which would
160138568Ssam	 * otherwise be used based on this criteria.
161138568Ssam	 */
162138568Ssam	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
163138568Ssam	rsn->rsn_mcastkeylen = 128 / NBBY;
164138568Ssam
165138568Ssam	/*
166138568Ssam	 * We support both WPA-PSK and 802.1x; the one used
167138568Ssam	 * is determined by the authentication mode and the
168138568Ssam	 * setting of the PSK state.
169138568Ssam	 */
170138568Ssam	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
171138568Ssam	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
172138568Ssam
173138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);		/* hold reference */
174138568Ssam	ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode);
175116742Ssam}
176116742Ssam
177116742Ssamvoid
178138568Ssamieee80211_node_detach(struct ieee80211com *ic)
179116742Ssam{
180116742Ssam
181138568Ssam	if (ic->ic_bss != NULL) {
182138568Ssam		ieee80211_free_node(ic->ic_bss);
183138568Ssam		ic->ic_bss = NULL;
184138568Ssam	}
185138568Ssam	ieee80211_node_table_cleanup(&ic->ic_scan);
186140753Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
187138568Ssam	if (ic->ic_aid_bitmap != NULL) {
188138568Ssam		FREE(ic->ic_aid_bitmap, M_DEVBUF);
189138568Ssam		ic->ic_aid_bitmap = NULL;
190138568Ssam	}
191138568Ssam	if (ic->ic_tim_bitmap != NULL) {
192138568Ssam		FREE(ic->ic_tim_bitmap, M_DEVBUF);
193138568Ssam		ic->ic_tim_bitmap = NULL;
194138568Ssam	}
195116742Ssam}
196116742Ssam
197138568Ssam/*
198138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
199138568Ssam */
200138568Ssam
201138568Ssamvoid
202148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
203138568Ssam{
204148302Ssam	struct ieee80211com *ic = ni->ni_ic;
205148302Ssam
206138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
207139528Ssam	ni->ni_inact_reload = ic->ic_inact_run;
208138568Ssam}
209138568Ssam
210138568Ssamvoid
211148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
212138568Ssam{
213138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
214138568Ssam}
215138568Ssam
216116742Ssam/*
217138568Ssam * Set/change the channel.  The rate set is also updated as
218138568Ssam * to insure a consistent view by drivers.
219138568Ssam */
220138568Ssamstatic __inline void
221138568Ssamieee80211_set_chan(struct ieee80211com *ic,
222138568Ssam	struct ieee80211_node *ni, struct ieee80211_channel *chan)
223138568Ssam{
224138568Ssam	ni->ni_chan = chan;
225138568Ssam	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
226138568Ssam}
227138568Ssam
228138568Ssam/*
229116742Ssam * AP scanning support.
230116742Ssam */
231116742Ssam
232138568Ssam#ifdef IEEE80211_DEBUG
233138568Ssamstatic void
234138568Ssamdump_chanlist(const u_char chans[])
235138568Ssam{
236138568Ssam	const char *sep;
237138568Ssam	int i;
238138568Ssam
239138568Ssam	sep = " ";
240138568Ssam	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
241138568Ssam		if (isset(chans, i)) {
242138568Ssam			printf("%s%u", sep, i);
243138568Ssam			sep = ", ";
244138568Ssam		}
245138568Ssam}
246138568Ssam#endif /* IEEE80211_DEBUG */
247138568Ssam
248116742Ssam/*
249138568Ssam * Initialize the channel set to scan based on the
250116742Ssam * of available channels and the current PHY mode.
251116742Ssam */
252117811Ssamstatic void
253138568Ssamieee80211_reset_scan(struct ieee80211com *ic)
254116742Ssam{
255116742Ssam
256138568Ssam	/* XXX ic_des_chan should be handled with ic_chan_active */
257138568Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
258138568Ssam		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
259138568Ssam		setbit(ic->ic_chan_scan,
260138568Ssam			ieee80211_chan2ieee(ic, ic->ic_des_chan));
261138568Ssam	} else
262138568Ssam		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
263138568Ssam			sizeof(ic->ic_chan_active));
264117811Ssam	/* NB: hack, setup so next_scan starts with the first channel */
265117811Ssam	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
266138568Ssam		ieee80211_set_chan(ic, ic->ic_bss,
267138568Ssam			&ic->ic_channels[IEEE80211_CHAN_MAX]);
268138568Ssam#ifdef IEEE80211_DEBUG
269138568Ssam	if (ieee80211_msg_scan(ic)) {
270138568Ssam		printf("%s: scan set:", __func__);
271138568Ssam		dump_chanlist(ic->ic_chan_scan);
272138568Ssam		printf(" start chan %u\n",
273138568Ssam			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
274138568Ssam	}
275138568Ssam#endif /* IEEE80211_DEBUG */
276116742Ssam}
277116742Ssam
278116742Ssam/*
279116742Ssam * Begin an active scan.
280116742Ssam */
281116742Ssamvoid
282138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset)
283116742Ssam{
284116742Ssam
285138568Ssam	ic->ic_scan.nt_scangen++;
286117811Ssam	/*
287117811Ssam	 * In all but hostap mode scanning starts off in
288117811Ssam	 * an active mode before switching to passive.
289117811Ssam	 */
290121180Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
291117811Ssam		ic->ic_flags |= IEEE80211_F_ASCAN;
292121180Ssam		ic->ic_stats.is_scan_active++;
293121180Ssam	} else
294121180Ssam		ic->ic_stats.is_scan_passive++;
295139520Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
296139520Ssam		"begin %s scan in %s mode, scangen %u\n",
297138568Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
298139520Ssam		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
299116742Ssam	/*
300138568Ssam	 * Clear scan state and flush any previously seen AP's.
301116742Ssam	 */
302138568Ssam	ieee80211_reset_scan(ic);
303138568Ssam	if (reset)
304138568Ssam		ieee80211_free_allnodes(&ic->ic_scan);
305116742Ssam
306138568Ssam	ic->ic_flags |= IEEE80211_F_SCAN;
307138568Ssam
308117811Ssam	/* Scan the next channel. */
309138568Ssam	ieee80211_next_scan(ic);
310116742Ssam}
311116742Ssam
312116742Ssam/*
313116742Ssam * Switch to the next channel marked for scanning.
314116742Ssam */
315138568Ssamint
316138568Ssamieee80211_next_scan(struct ieee80211com *ic)
317116742Ssam{
318116742Ssam	struct ieee80211_channel *chan;
319116742Ssam
320138568Ssam	/*
321138568Ssam	 * Insure any previous mgt frame timeouts don't fire.
322138568Ssam	 * This assumes the driver does the right thing in
323138568Ssam	 * flushing anything queued in the driver and below.
324138568Ssam	 */
325138568Ssam	ic->ic_mgt_timer = 0;
326138568Ssam
327116742Ssam	chan = ic->ic_bss->ni_chan;
328138568Ssam	do {
329116742Ssam		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
330116742Ssam			chan = &ic->ic_channels[0];
331116742Ssam		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
332138568Ssam			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
333138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
334138568Ssam			    "%s: chan %d->%d\n", __func__,
335138568Ssam			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
336138568Ssam			    ieee80211_chan2ieee(ic, chan));
337138568Ssam			ieee80211_set_chan(ic, ic->ic_bss, chan);
338138568Ssam			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
339138568Ssam			return 1;
340116742Ssam		}
341138568Ssam	} while (chan != ic->ic_bss->ni_chan);
342138568Ssam	ieee80211_end_scan(ic);
343138568Ssam	return 0;
344116742Ssam}
345116742Ssam
346141658Ssamstatic __inline void
347141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
348141658Ssam{
349141658Ssam	/* propagate useful state */
350141658Ssam	nbss->ni_authmode = obss->ni_authmode;
351141658Ssam	nbss->ni_txpower = obss->ni_txpower;
352141658Ssam	nbss->ni_vlan = obss->ni_vlan;
353141658Ssam	nbss->ni_rsn = obss->ni_rsn;
354141658Ssam	/* XXX statistics? */
355141658Ssam}
356141658Ssam
357116742Ssamvoid
358116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
359116742Ssam{
360140753Ssam	struct ieee80211_node_table *nt;
361116742Ssam	struct ieee80211_node *ni;
362116742Ssam
363138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
364138568Ssam		"%s: creating ibss\n", __func__);
365138568Ssam
366138568Ssam	/*
367138568Ssam	 * Create the station/neighbor table.  Note that for adhoc
368138568Ssam	 * mode we make the initial inactivity timer longer since
369138568Ssam	 * we create nodes only through discovery and they typically
370138568Ssam	 * are long-lived associations.
371138568Ssam	 */
372140753Ssam	nt = &ic->ic_sta;
373140753Ssam	IEEE80211_NODE_LOCK(nt);
374140753Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
375140753Ssam		nt->nt_name = "station";
376140753Ssam		nt->nt_inact_init = ic->ic_inact_init;
377140753Ssam	} else {
378140753Ssam		nt->nt_name = "neighbor";
379140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
380140753Ssam	}
381140753Ssam	IEEE80211_NODE_UNLOCK(nt);
382140753Ssam
383140753Ssam	ni = ieee80211_alloc_node(nt, ic->ic_myaddr);
384140753Ssam	if (ni == NULL) {
385140753Ssam		/* XXX recovery? */
386138568Ssam		return;
387138568Ssam	}
388116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
389116742Ssam	ni->ni_esslen = ic->ic_des_esslen;
390116742Ssam	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
391141658Ssam	copy_bss(ni, ic->ic_bss);
392116742Ssam	ni->ni_intval = ic->ic_lintval;
393138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
394116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
395116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
396116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
397116742Ssam		ni->ni_fhindex = 1;
398116742Ssam	}
399138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
400138568Ssam		ic->ic_flags |= IEEE80211_F_SIBSS;
401138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
402143300Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
403143300Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
404143300Ssam		else
405143300Ssam			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
406138568Ssam	}
407138568Ssam	/*
408138568Ssam	 * Fix the channel and related attributes.
409138568Ssam	 */
410138568Ssam	ieee80211_set_chan(ic, ni, chan);
411138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
412138568Ssam	/*
413138568Ssam	 * Do mode-specific rate setup.
414138568Ssam	 */
415138568Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G) {
416138568Ssam		/*
417138568Ssam		 * Use a mixed 11b/11g rate set.
418138568Ssam		 */
419138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
420138568Ssam	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
421138568Ssam		/*
422138568Ssam		 * Force pure 11b rate set.
423138568Ssam		 */
424138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
425138568Ssam	}
426138568Ssam
427140753Ssam	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
428116742Ssam}
429116742Ssam
430138568Ssamvoid
431138568Ssamieee80211_reset_bss(struct ieee80211com *ic)
432138568Ssam{
433138568Ssam	struct ieee80211_node *ni, *obss;
434138568Ssam
435138568Ssam	ieee80211_node_table_reset(&ic->ic_scan);
436140753Ssam	ieee80211_node_table_reset(&ic->ic_sta);
437140753Ssam
438138568Ssam	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
439138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
440138568Ssam	obss = ic->ic_bss;
441138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);
442141658Ssam	if (obss != NULL) {
443141658Ssam		copy_bss(ni, obss);
444141658Ssam		ni->ni_intval = ic->ic_lintval;
445138568Ssam		ieee80211_free_node(obss);
446141658Ssam	}
447138568Ssam}
448138568Ssam
449127767Ssamstatic int
450138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
451127767Ssam{
452127767Ssam        u_int8_t rate;
453127767Ssam        int fail;
454127767Ssam
455127767Ssam	fail = 0;
456127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
457127767Ssam		fail |= 0x01;
458127767Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
459127767Ssam	    ni->ni_chan != ic->ic_des_chan)
460127767Ssam		fail |= 0x01;
461127767Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
462127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
463127767Ssam			fail |= 0x02;
464127767Ssam	} else {
465127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
466127767Ssam			fail |= 0x02;
467127767Ssam	}
468138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
469127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
470127767Ssam			fail |= 0x04;
471127767Ssam	} else {
472127767Ssam		/* XXX does this mean privacy is supported or required? */
473127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
474127767Ssam			fail |= 0x04;
475127767Ssam	}
476148299Ssam	rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
477127767Ssam	if (rate & IEEE80211_RATE_BASIC)
478127767Ssam		fail |= 0x08;
479127767Ssam	if (ic->ic_des_esslen != 0 &&
480127767Ssam	    (ni->ni_esslen != ic->ic_des_esslen ||
481127767Ssam	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
482127767Ssam		fail |= 0x10;
483127767Ssam	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
484127767Ssam	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
485127767Ssam		fail |= 0x20;
486127767Ssam#ifdef IEEE80211_DEBUG
487138568Ssam	if (ieee80211_msg_scan(ic)) {
488127767Ssam		printf(" %c %s", fail ? '-' : '+',
489127767Ssam		    ether_sprintf(ni->ni_macaddr));
490127767Ssam		printf(" %s%c", ether_sprintf(ni->ni_bssid),
491127767Ssam		    fail & 0x20 ? '!' : ' ');
492127767Ssam		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
493127767Ssam			fail & 0x01 ? '!' : ' ');
494127767Ssam		printf(" %+4d", ni->ni_rssi);
495127767Ssam		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
496127767Ssam		    fail & 0x08 ? '!' : ' ');
497127767Ssam		printf(" %4s%c",
498127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
499127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
500127767Ssam		    "????",
501127767Ssam		    fail & 0x02 ? '!' : ' ');
502127767Ssam		printf(" %3s%c ",
503127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
504127767Ssam		    "wep" : "no",
505127767Ssam		    fail & 0x04 ? '!' : ' ');
506127767Ssam		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
507127767Ssam		printf("%s\n", fail & 0x10 ? "!" : "");
508127767Ssam	}
509127767Ssam#endif
510127767Ssam	return fail;
511127767Ssam}
512127767Ssam
513138568Ssamstatic __inline u_int8_t
514138568Ssammaxrate(const struct ieee80211_node *ni)
515138568Ssam{
516138568Ssam	const struct ieee80211_rateset *rs = &ni->ni_rates;
517138568Ssam	/* NB: assumes rate set is sorted (happens on frame receive) */
518138568Ssam	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
519138568Ssam}
520138568Ssam
521116742Ssam/*
522138568Ssam * Compare the capabilities of two nodes and decide which is
523138568Ssam * more desirable (return >0 if a is considered better).  Note
524138568Ssam * that we assume compatibility/usability has already been checked
525138568Ssam * so we don't need to (e.g. validate whether privacy is supported).
526138568Ssam * Used to select the best scan candidate for association in a BSS.
527138568Ssam */
528138568Ssamstatic int
529138568Ssamieee80211_node_compare(struct ieee80211com *ic,
530138568Ssam		       const struct ieee80211_node *a,
531138568Ssam		       const struct ieee80211_node *b)
532138568Ssam{
533138568Ssam	u_int8_t maxa, maxb;
534138568Ssam	u_int8_t rssia, rssib;
535138568Ssam
536138568Ssam	/* privacy support preferred */
537138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
538138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
539138568Ssam		return 1;
540138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
541138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
542138568Ssam		return -1;
543138568Ssam
544138568Ssam	rssia = ic->ic_node_getrssi(a);
545138568Ssam	rssib = ic->ic_node_getrssi(b);
546139543Ssam	if (abs(rssib - rssia) < 5) {
547139543Ssam		/* best/max rate preferred if signal level close enough XXX */
548139543Ssam		maxa = maxrate(a);
549139543Ssam		maxb = maxrate(b);
550139543Ssam		if (maxa != maxb)
551139543Ssam			return maxa - maxb;
552139543Ssam		/* XXX use freq for channel preference */
553139543Ssam		/* for now just prefer 5Ghz band to all other bands */
554139543Ssam		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
555139543Ssam		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
556139543Ssam			return 1;
557139543Ssam		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
558139543Ssam		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
559139543Ssam			return -1;
560139543Ssam	}
561138568Ssam	/* all things being equal, use signal level */
562138568Ssam	return rssia - rssib;
563138568Ssam}
564138568Ssam
565138568Ssam/*
566140441Ssam * Mark an ongoing scan stopped.
567140441Ssam */
568140441Ssamvoid
569140441Ssamieee80211_cancel_scan(struct ieee80211com *ic)
570140441Ssam{
571140441Ssam
572140441Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
573140441Ssam		__func__,
574140441Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
575140441Ssam
576140441Ssam	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
577140441Ssam}
578140441Ssam
579140441Ssam/*
580116742Ssam * Complete a scan of potential channels.
581116742Ssam */
582116742Ssamvoid
583138568Ssamieee80211_end_scan(struct ieee80211com *ic)
584116742Ssam{
585140448Ssam	struct ieee80211_node_table *nt = &ic->ic_scan;
586140448Ssam	struct ieee80211_node *ni, *selbs;
587116742Ssam
588140441Ssam	ieee80211_cancel_scan(ic);
589140441Ssam	ieee80211_notify_scan_done(ic);
590116742Ssam
591116742Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
592138568Ssam		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
593138568Ssam		int i, bestchan;
594138568Ssam		u_int8_t rssi;
595138568Ssam
596116742Ssam		/*
597116742Ssam		 * The passive scan to look for existing AP's completed,
598116742Ssam		 * select a channel to camp on.  Identify the channels
599116742Ssam		 * that already have one or more AP's and try to locate
600140753Ssam		 * an unoccupied one.  If that fails, pick a channel that
601138568Ssam		 * looks to be quietest.
602116742Ssam		 */
603138568Ssam		memset(maxrssi, 0, sizeof(maxrssi));
604140448Ssam		IEEE80211_NODE_LOCK(nt);
605140448Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
606138568Ssam			rssi = ic->ic_node_getrssi(ni);
607138568Ssam			i = ieee80211_chan2ieee(ic, ni->ni_chan);
608138568Ssam			if (rssi > maxrssi[i])
609138568Ssam				maxrssi[i] = rssi;
610116742Ssam		}
611140448Ssam		IEEE80211_NODE_UNLOCK(nt);
612138568Ssam		/* XXX select channel more intelligently */
613138568Ssam		bestchan = -1;
614116742Ssam		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
615138568Ssam			if (isset(ic->ic_chan_active, i)) {
616138568Ssam				/*
617138568Ssam				 * If the channel is unoccupied the max rssi
618138568Ssam				 * should be zero; just take it.  Otherwise
619138568Ssam				 * track the channel with the lowest rssi and
620138568Ssam				 * use that when all channels appear occupied.
621138568Ssam				 */
622138568Ssam				if (maxrssi[i] == 0) {
623138568Ssam					bestchan = i;
624116742Ssam					break;
625138568Ssam				}
626143715Ssam				if (bestchan == -1 ||
627143715Ssam				    maxrssi[i] < maxrssi[bestchan])
628138568Ssam					bestchan = i;
629138568Ssam			}
630138568Ssam		if (bestchan != -1) {
631138568Ssam			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
632138568Ssam			return;
633116742Ssam		}
634138568Ssam		/* no suitable channel, should not happen */
635138568Ssam	}
636138568Ssam
637138568Ssam	/*
638138568Ssam	 * When manually sequencing the state machine; scan just once
639138568Ssam	 * regardless of whether we have a candidate or not.  The
640138568Ssam	 * controlling application is expected to setup state and
641138568Ssam	 * initiate an association.
642138568Ssam	 */
643138568Ssam	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
644116742Ssam		return;
645138568Ssam	/*
646138568Ssam	 * Automatic sequencing; look for a candidate and
647138568Ssam	 * if found join the network.
648138568Ssam	 */
649140448Ssam	/* NB: unlocked read should be ok */
650140448Ssam	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
651138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
652138568Ssam			"%s: no scan candidate\n", __func__);
653116742Ssam  notfound:
654116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS &&
655116742Ssam		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
656116742Ssam		    ic->ic_des_esslen != 0) {
657116742Ssam			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
658116742Ssam			return;
659116742Ssam		}
660116742Ssam		/*
661116742Ssam		 * Reset the list of channels to scan and start again.
662116742Ssam		 */
663138568Ssam		ieee80211_reset_scan(ic);
664138568Ssam		ic->ic_flags |= IEEE80211_F_SCAN;
665138568Ssam		ieee80211_next_scan(ic);
666116742Ssam		return;
667116742Ssam	}
668116742Ssam	selbs = NULL;
669138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
670138568Ssam	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
671140448Ssam	IEEE80211_NODE_LOCK(nt);
672140448Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
673116742Ssam		if (ni->ni_fails) {
674116742Ssam			/*
675116742Ssam			 * The configuration of the access points may change
676116742Ssam			 * during my scan.  So delete the entry for the AP
677116742Ssam			 * and retry to associate if there is another beacon.
678116742Ssam			 */
679138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
680138568Ssam				"%s: skip scan candidate %s, fails %u\n",
681138568Ssam				__func__, ether_sprintf(ni->ni_macaddr),
682138568Ssam				ni->ni_fails);
683140448Ssam			ni->ni_fails++;
684140448Ssam#if 0
685116742Ssam			if (ni->ni_fails++ > 2)
686138568Ssam				ieee80211_free_node(ni);
687140448Ssam#endif
688116742Ssam			continue;
689116742Ssam		}
690138568Ssam		if (ieee80211_match_bss(ic, ni) == 0) {
691116742Ssam			if (selbs == NULL)
692116742Ssam				selbs = ni;
693140448Ssam			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
694116742Ssam				selbs = ni;
695116742Ssam		}
696116742Ssam	}
697140448Ssam	if (selbs != NULL)		/* NB: grab ref while dropping lock */
698140448Ssam		(void) ieee80211_ref_node(selbs);
699140448Ssam	IEEE80211_NODE_UNLOCK(nt);
700116742Ssam	if (selbs == NULL)
701116742Ssam		goto notfound;
702138568Ssam	if (!ieee80211_sta_join(ic, selbs)) {
703140448Ssam		ieee80211_free_node(selbs);
704138568Ssam		goto notfound;
705138568Ssam	}
706138568Ssam}
707138568Ssam
708138568Ssam/*
709138568Ssam * Handle 802.11 ad hoc network merge.  The
710138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
711138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
712138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
713138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
714138568Ssam * sends beacons with the greatest TSF timestamp.
715138568Ssam *
716138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
717138568Ssam *
718138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
719138568Ssam */
720138568Ssamint
721148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
722138568Ssam{
723148306Ssam	struct ieee80211com *ic = ni->ni_ic;
724138568Ssam
725140453Ssam	if (ni == ic->ic_bss ||
726140453Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
727138568Ssam		/* unchanged, nothing to do */
728138568Ssam		return 0;
729138568Ssam	}
730138568Ssam	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
731138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
732138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
733138568Ssam		ic->ic_stats.is_ibss_capmismatch++;
734138568Ssam		return 0;
735138568Ssam	}
736138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
737138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
738138568Ssam		ether_sprintf(ni->ni_bssid),
739138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
740138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
741138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
742138568Ssam	);
743140753Ssam	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
744138568Ssam}
745138568Ssam
746138568Ssam/*
747138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
748138568Ssam * be passed in with a held reference.
749138568Ssam */
750138568Ssamint
751138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
752138568Ssam{
753138568Ssam	struct ieee80211_node *obss;
754138568Ssam
755116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
756140753Ssam		struct ieee80211_node_table *nt;
757138568Ssam		/*
758140440Ssam		 * Delete unusable rates; we've already checked
759140440Ssam		 * that the negotiated rate set is acceptable.
760138568Ssam		 */
761148299Ssam		ieee80211_fix_rate(selbs, IEEE80211_F_DODEL);
762127772Ssam		/*
763140753Ssam		 * Fillin the neighbor table; it will already
764139521Ssam		 * exist if we are simply switching mastership.
765140753Ssam		 * XXX ic_sta always setup so this is unnecessary?
766127772Ssam		 */
767140753Ssam		nt = &ic->ic_sta;
768140753Ssam		IEEE80211_NODE_LOCK(nt);
769140753Ssam		nt->nt_name = "neighbor";
770140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
771140753Ssam		IEEE80211_NODE_UNLOCK(nt);
772138568Ssam	}
773138568Ssam
774138568Ssam	/*
775138568Ssam	 * Committed to selbs, setup state.
776138568Ssam	 */
777138568Ssam	obss = ic->ic_bss;
778140753Ssam	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
779138568Ssam	if (obss != NULL)
780138568Ssam		ieee80211_free_node(obss);
781138568Ssam	/*
782138568Ssam	 * Set the erp state (mostly the slot time) to deal with
783138568Ssam	 * the auto-select case; this should be redundant if the
784138568Ssam	 * mode is locked.
785138568Ssam	 */
786138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
787138568Ssam	ieee80211_reset_erp(ic);
788138568Ssam	ieee80211_wme_initparams(ic);
789140753Ssam
790140753Ssam	if (ic->ic_opmode == IEEE80211_M_STA)
791140753Ssam		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
792140753Ssam	else
793117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
794138568Ssam	return 1;
795116742Ssam}
796116742Ssam
797138568Ssam/*
798138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
799138568Ssam * be passed in with a held reference.
800138568Ssam */
801138568Ssamvoid
802138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
803138568Ssam{
804138568Ssam	ic->ic_node_cleanup(ni);
805138568Ssam	ieee80211_notify_node_leave(ic, ni);
806138568Ssam}
807138568Ssam
808116742Ssamstatic struct ieee80211_node *
809138568Ssamnode_alloc(struct ieee80211_node_table *nt)
810116742Ssam{
811127768Ssam	struct ieee80211_node *ni;
812138568Ssam
813127768Ssam	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
814127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
815127768Ssam	return ni;
816116742Ssam}
817116742Ssam
818138568Ssam/*
819138568Ssam * Reclaim any resources in a node and reset any critical
820138568Ssam * state.  Typically nodes are free'd immediately after,
821138568Ssam * but in some cases the storage may be reused so we need
822138568Ssam * to insure consistent state (should probably fix that).
823138568Ssam */
824116742Ssamstatic void
825138568Ssamnode_cleanup(struct ieee80211_node *ni)
826116742Ssam{
827138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
828138568Ssam	struct ieee80211com *ic = ni->ni_ic;
829138568Ssam	int i, qlen;
830138568Ssam
831138568Ssam	/* NB: preserve ni_table */
832138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
833138568Ssam		ic->ic_ps_sta--;
834138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
835138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
836138568Ssam		    "[%s] power save mode off, %u sta's in ps mode\n",
837138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
838138568Ssam	}
839147788Ssam	/*
840147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
841147788Ssam	 * has happened.  This is probably not needed as the node
842147788Ssam	 * should always be removed from the table so not found but
843147788Ssam	 * do it just in case.
844147788Ssam	 */
845147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
846138568Ssam
847138568Ssam	/*
848138568Ssam	 * Drain power save queue and, if needed, clear TIM.
849138568Ssam	 */
850138568Ssam	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
851138568Ssam	if (qlen != 0 && ic->ic_set_tim != NULL)
852148304Ssam		ic->ic_set_tim(ni, 0);
853138568Ssam
854138568Ssam	ni->ni_associd = 0;
855138568Ssam	if (ni->ni_challenge != NULL) {
856138568Ssam		FREE(ni->ni_challenge, M_DEVBUF);
857138568Ssam		ni->ni_challenge = NULL;
858138568Ssam	}
859138568Ssam	/*
860138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
861138568Ssam	 * reusable during a re-auth/re-assoc state transition.
862138568Ssam	 * If we remove these data they will not be recreated
863138568Ssam	 * because they come from a probe-response or beacon frame
864138568Ssam	 * which cannot be expected prior to the association-response.
865138568Ssam	 * This should not be an issue when operating in other modes
866138568Ssam	 * as stations leaving always go through a full state transition
867138568Ssam	 * which will rebuild this state.
868138568Ssam	 *
869138568Ssam	 * XXX does this leave us open to inheriting old state?
870138568Ssam	 */
871138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
872138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
873138568Ssam			m_freem(ni->ni_rxfrag[i]);
874138568Ssam			ni->ni_rxfrag[i] = NULL;
875138568Ssam		}
876138568Ssam	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
877138568Ssam#undef N
878116742Ssam}
879116742Ssam
880116742Ssamstatic void
881138568Ssamnode_free(struct ieee80211_node *ni)
882116742Ssam{
883138568Ssam	struct ieee80211com *ic = ni->ni_ic;
884138568Ssam
885138568Ssam	ic->ic_node_cleanup(ni);
886138568Ssam	if (ni->ni_wpa_ie != NULL)
887138568Ssam		FREE(ni->ni_wpa_ie, M_DEVBUF);
888138568Ssam	if (ni->ni_wme_ie != NULL)
889138568Ssam		FREE(ni->ni_wme_ie, M_DEVBUF);
890138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
891138568Ssam	FREE(ni, M_80211_NODE);
892116742Ssam}
893116742Ssam
894120104Ssamstatic u_int8_t
895138568Ssamnode_getrssi(const struct ieee80211_node *ni)
896120104Ssam{
897120104Ssam	return ni->ni_rssi;
898120104Ssam}
899120104Ssam
900116742Ssamstatic void
901138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt,
902138568Ssam	struct ieee80211_node *ni, const u_int8_t *macaddr)
903116742Ssam{
904138568Ssam	struct ieee80211com *ic = nt->nt_ic;
905116742Ssam	int hash;
906116742Ssam
907138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
908140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
909138568Ssam		ether_sprintf(macaddr), nt->nt_name);
910138568Ssam
911116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
912116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
913138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
914138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
915138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
916138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
917138568Ssam	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
918139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
919139528Ssam	ni->ni_inact = ni->ni_inact_reload;
920138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
921138568Ssam
922138568Ssam	IEEE80211_NODE_LOCK(nt);
923138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
924138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
925138568Ssam	ni->ni_table = nt;
926138568Ssam	ni->ni_ic = ic;
927138568Ssam	IEEE80211_NODE_UNLOCK(nt);
928116742Ssam}
929116742Ssam
930116742Ssamstruct ieee80211_node *
931138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
932116742Ssam{
933138568Ssam	struct ieee80211com *ic = nt->nt_ic;
934138568Ssam	struct ieee80211_node *ni;
935138568Ssam
936138568Ssam	ni = ic->ic_node_alloc(nt);
937116742Ssam	if (ni != NULL)
938138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
939127769Ssam	else
940127769Ssam		ic->ic_stats.is_rx_nodealloc++;
941116742Ssam	return ni;
942116742Ssam}
943116742Ssam
944116742Ssamstruct ieee80211_node *
945138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
946116742Ssam{
947138568Ssam	struct ieee80211com *ic = nt->nt_ic;
948138568Ssam	struct ieee80211_node *ni;
949138568Ssam
950138568Ssam	ni = ic->ic_node_alloc(nt);
951116742Ssam	if (ni != NULL) {
952138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
953127770Ssam		/*
954127770Ssam		 * Inherit from ic_bss.
955127770Ssam		 */
956138568Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
957138568Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
958138568Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
959127770Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
960138568Ssam		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
961138568Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
962127770Ssam	} else
963127770Ssam		ic->ic_stats.is_rx_nodealloc++;
964116742Ssam	return ni;
965116742Ssam}
966116742Ssam
967127772Ssamstatic struct ieee80211_node *
968138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
969138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
970138568Ssam	const u_int8_t *macaddr, const char *func, int line)
971138568Ssam#else
972138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt,
973138568Ssam	const u_int8_t *macaddr)
974138568Ssam#endif
975116742Ssam{
976116742Ssam	struct ieee80211_node *ni;
977116742Ssam	int hash;
978116742Ssam
979138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
980127772Ssam
981116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
982138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
983116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
984138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
985138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
986138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
987140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
988140766Ssam			    func, line,
989140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
990140766Ssam			    ieee80211_node_refcnt(ni));
991138568Ssam#endif
992127772Ssam			return ni;
993116742Ssam		}
994116742Ssam	}
995127772Ssam	return NULL;
996127772Ssam}
997138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
998138568Ssam#define	_ieee80211_find_node(nt, mac) \
999138568Ssam	_ieee80211_find_node_debug(nt, mac, func, line)
1000138568Ssam#endif
1001127772Ssam
1002127772Ssamstruct ieee80211_node *
1003138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1004138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1005138568Ssam	const u_int8_t *macaddr, const char *func, int line)
1006138568Ssam#else
1007138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1008138568Ssam#endif
1009127772Ssam{
1010127772Ssam	struct ieee80211_node *ni;
1011127772Ssam
1012138568Ssam	IEEE80211_NODE_LOCK(nt);
1013138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1014138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1015116742Ssam	return ni;
1016116742Ssam}
1017116742Ssam
1018116742Ssam/*
1019138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1020138568Ssam * Note that for the driver's benefit we we treat this like
1021138568Ssam * an association so the driver has an opportunity to setup
1022138568Ssam * it's private state.
1023138568Ssam */
1024138568Ssamstruct ieee80211_node *
1025138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1026138568Ssam	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1027138568Ssam{
1028138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1029138568Ssam	struct ieee80211_node *ni;
1030138568Ssam
1031138568Ssam	ni = ieee80211_dup_bss(nt, macaddr);
1032138568Ssam	if (ni != NULL) {
1033138568Ssam		/* XXX no rate negotiation; just dup */
1034138568Ssam		ni->ni_rates = ic->ic_bss->ni_rates;
1035139524Ssam		if (ic->ic_newassoc != NULL)
1036148307Ssam			ic->ic_newassoc(ni, 1);
1037138568Ssam		/* XXX not right for 802.1x/WPA */
1038148302Ssam		ieee80211_node_authorize(ni);
1039138568Ssam	}
1040138568Ssam	return ni;
1041138568Ssam}
1042138568Ssam
1043138568Ssam/*
1044138568Ssam * Locate the node for sender, track state, and then pass the
1045138568Ssam * (referenced) node up to the 802.11 layer for its use.  We
1046138568Ssam * are required to pass some node so we fall back to ic_bss
1047138568Ssam * when this frame is from an unknown sender.  The 802.11 layer
1048138568Ssam * knows this means the sender wasn't in the node table and
1049138568Ssam * acts accordingly.
1050138568Ssam */
1051138568Ssamstruct ieee80211_node *
1052138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1053138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1054138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1055138568Ssam#else
1056138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1057138568Ssam	const struct ieee80211_frame_min *wh)
1058138568Ssam#endif
1059138568Ssam{
1060138568Ssam#define	IS_CTL(wh) \
1061138568Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1062138568Ssam#define	IS_PSPOLL(wh) \
1063138568Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1064138568Ssam	struct ieee80211_node_table *nt;
1065138568Ssam	struct ieee80211_node *ni;
1066138568Ssam
1067138568Ssam	/* XXX may want scanned nodes in the neighbor table for adhoc */
1068138568Ssam	if (ic->ic_opmode == IEEE80211_M_STA ||
1069138568Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1070138568Ssam	    (ic->ic_flags & IEEE80211_F_SCAN))
1071138568Ssam		nt = &ic->ic_scan;
1072138568Ssam	else
1073140753Ssam		nt = &ic->ic_sta;
1074138568Ssam	/* XXX check ic_bss first in station mode */
1075138568Ssam	/* XXX 4-address frames? */
1076138568Ssam	IEEE80211_NODE_LOCK(nt);
1077138568Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1078138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr1);
1079138568Ssam	else
1080138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr2);
1081138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1082138568Ssam
1083138568Ssam	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
1084138568Ssam#undef IS_PSPOLL
1085138568Ssam#undef IS_CTL
1086138568Ssam}
1087138568Ssam
1088138568Ssam/*
1089127772Ssam * Return a reference to the appropriate node for sending
1090127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1091127772Ssam */
1092127772Ssamstruct ieee80211_node *
1093138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1094138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1095138568Ssam	const char *func, int line)
1096138568Ssam#else
1097138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1098138568Ssam#endif
1099127772Ssam{
1100140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1101127772Ssam	struct ieee80211_node *ni;
1102127772Ssam
1103127772Ssam	/*
1104127772Ssam	 * The destination address should be in the node table
1105127772Ssam	 * unless we are operating in station mode or this is a
1106127772Ssam	 * multicast/broadcast frame.
1107127772Ssam	 */
1108140753Ssam	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1109138568Ssam		return ieee80211_ref_node(ic->ic_bss);
1110127772Ssam
1111127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1112138568Ssam	IEEE80211_NODE_LOCK(nt);
1113138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1114138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1115138568Ssam
1116138568Ssam	if (ni == NULL) {
1117138568Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1118140497Ssam		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
1119140497Ssam			/*
1120140497Ssam			 * In adhoc mode cons up a node for the destination.
1121140497Ssam			 * Note that we need an additional reference for the
1122140497Ssam			 * caller to be consistent with _ieee80211_find_node.
1123140497Ssam			 */
1124138568Ssam			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1125140497Ssam			if (ni != NULL)
1126140497Ssam				(void) ieee80211_ref_node(ni);
1127140497Ssam		} else {
1128138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1129138568Ssam				"[%s] no node, discard frame (%s)\n",
1130138568Ssam				ether_sprintf(macaddr), __func__);
1131138568Ssam			ic->ic_stats.is_tx_nonode++;
1132127772Ssam		}
1133127772Ssam	}
1134127772Ssam	return ni;
1135127772Ssam}
1136127772Ssam
1137127772Ssam/*
1138116742Ssam * Like find but search based on the channel too.
1139116742Ssam */
1140116742Ssamstruct ieee80211_node *
1141138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1142138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1143138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan,
1144138568Ssam	const char *func, int line)
1145138568Ssam#else
1146138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1147138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan)
1148138568Ssam#endif
1149116742Ssam{
1150116742Ssam	struct ieee80211_node *ni;
1151116742Ssam	int hash;
1152116742Ssam
1153116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1154138568Ssam	IEEE80211_NODE_LOCK(nt);
1155138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1156127772Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1157127772Ssam		    ni->ni_chan == chan) {
1158138568Ssam			ieee80211_ref_node(ni);		/* mark referenced */
1159138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1160138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1161140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1162140766Ssam			    func, line,
1163138568Ssam#else
1164140766Ssam			    "%s %p<%s> refcnt %d\n", __func__,
1165138568Ssam#endif
1166140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1167140766Ssam			    ieee80211_node_refcnt(ni));
1168116742Ssam			break;
1169116742Ssam		}
1170116742Ssam	}
1171138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1172116742Ssam	return ni;
1173116742Ssam}
1174116742Ssam
1175138568Ssam/*
1176138568Ssam * Like find but search based on the ssid too.
1177138568Ssam */
1178138568Ssamstruct ieee80211_node *
1179138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1180138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1181138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1182138568Ssam	const char *func, int line)
1183138568Ssam#else
1184138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1185138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1186138568Ssam#endif
1187138568Ssam{
1188147118Ssam#define	MATCH_SSID(ni, ssid, ssidlen) \
1189147118Ssam	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1190147118Ssam	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
1191138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1192138568Ssam	struct ieee80211_node *ni;
1193138568Ssam	int hash;
1194138568Ssam
1195138568Ssam	IEEE80211_NODE_LOCK(nt);
1196147118Ssam	/*
1197147118Ssam	 * A mac address that is all zero means match only the ssid;
1198147118Ssam	 * otherwise we must match both.
1199147118Ssam	 */
1200147118Ssam	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1201147118Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1202147118Ssam			if (MATCH_SSID(ni, ssid, ssidlen))
1203147118Ssam				break;
1204147118Ssam		}
1205147118Ssam	} else {
1206147118Ssam		hash = IEEE80211_NODE_HASH(macaddr);
1207147118Ssam		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1208147118Ssam			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1209147118Ssam			    MATCH_SSID(ni, ssid, ssidlen))
1210147118Ssam				break;
1211147118Ssam		}
1212147118Ssam	}
1213147118Ssam	if (ni != NULL) {
1214147118Ssam		ieee80211_ref_node(ni);	/* mark referenced */
1215147118Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1216138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1217147118Ssam		    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1218147118Ssam		    func, line,
1219138568Ssam#else
1220147118Ssam		    "%s %p<%s> refcnt %d\n", __func__,
1221138568Ssam#endif
1222147118Ssam		     ni, ether_sprintf(ni->ni_macaddr),
1223147118Ssam		     ieee80211_node_refcnt(ni));
1224138568Ssam	}
1225138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1226138568Ssam	return ni;
1227147118Ssam#undef MATCH_SSID
1228138568Ssam}
1229138568Ssam
1230116742Ssamstatic void
1231138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1232116742Ssam{
1233138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1234138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1235119150Ssam
1236138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1237140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1238140766Ssam		ether_sprintf(ni->ni_macaddr),
1239138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1240138568Ssam
1241138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1242138568Ssam	if (nt != NULL) {
1243138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1244138568Ssam		LIST_REMOVE(ni, ni_hash);
1245138568Ssam	}
1246138568Ssam	ic->ic_node_free(ni);
1247116742Ssam}
1248116742Ssam
1249116742Ssamvoid
1250138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1251138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1252138568Ssam#else
1253138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1254138568Ssam#endif
1255116742Ssam{
1256138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1257119150Ssam
1258138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1259140454Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1260140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1261138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1262138568Ssam#endif
1263138568Ssam	if (ieee80211_node_dectestref(ni)) {
1264138568Ssam		/*
1265138568Ssam		 * Beware; if the node is marked gone then it's already
1266138568Ssam		 * been removed from the table and we cannot assume the
1267138568Ssam		 * table still exists.  Regardless, there's no need to lock
1268138568Ssam		 * the table.
1269138568Ssam		 */
1270138568Ssam		if (ni->ni_table != NULL) {
1271138568Ssam			IEEE80211_NODE_LOCK(nt);
1272138568Ssam			_ieee80211_free_node(ni);
1273138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1274138568Ssam		} else
1275138568Ssam			_ieee80211_free_node(ni);
1276116742Ssam	}
1277116742Ssam}
1278116742Ssam
1279138568Ssam/*
1280138568Ssam * Reclaim a node.  If this is the last reference count then
1281138568Ssam * do the normal free work.  Otherwise remove it from the node
1282138568Ssam * table and mark it gone by clearing the back-reference.
1283138568Ssam */
1284138568Ssamstatic void
1285138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1286116742Ssam{
1287138568Ssam
1288140766Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1289140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1290140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1291140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1292138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1293138568Ssam		/*
1294138568Ssam		 * Other references are present, just remove the
1295138568Ssam		 * node from the table so it cannot be found.  When
1296138568Ssam		 * the references are dropped storage will be
1297140753Ssam		 * reclaimed.
1298138568Ssam		 */
1299138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1300138568Ssam		LIST_REMOVE(ni, ni_hash);
1301138568Ssam		ni->ni_table = NULL;		/* clear reference */
1302138568Ssam	} else
1303138568Ssam		_ieee80211_free_node(ni);
1304138568Ssam}
1305138568Ssam
1306138568Ssamstatic void
1307138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1308138568Ssam{
1309138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1310116742Ssam	struct ieee80211_node *ni;
1311116742Ssam
1312138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1313138568Ssam		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1314138568Ssam
1315138568Ssam	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1316138568Ssam		if (ni->ni_associd != 0) {
1317138568Ssam			if (ic->ic_auth->ia_node_leave != NULL)
1318138568Ssam				ic->ic_auth->ia_node_leave(ic, ni);
1319138568Ssam			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1320138568Ssam		}
1321138568Ssam		node_reclaim(nt, ni);
1322138568Ssam	}
1323138568Ssam	ieee80211_reset_erp(ic);
1324116742Ssam}
1325116742Ssam
1326138568Ssamstatic void
1327138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt)
1328138568Ssam{
1329138568Ssam
1330138568Ssam	IEEE80211_NODE_LOCK(nt);
1331138568Ssam	ieee80211_free_allnodes_locked(nt);
1332138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1333138568Ssam}
1334138568Ssam
1335120483Ssam/*
1336138568Ssam * Timeout entries in the scan cache.
1337138568Ssam */
1338138568Ssamstatic void
1339138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1340138568Ssam{
1341138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1342138568Ssam	struct ieee80211_node *ni, *tni;
1343138568Ssam
1344138568Ssam	IEEE80211_NODE_LOCK(nt);
1345138568Ssam	ni = ic->ic_bss;
1346138568Ssam	/* XXX belongs elsewhere */
1347138568Ssam	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1348138568Ssam		m_freem(ni->ni_rxfrag[0]);
1349138568Ssam		ni->ni_rxfrag[0] = NULL;
1350138568Ssam	}
1351138568Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1352138568Ssam		if (ni->ni_inact && --ni->ni_inact == 0) {
1353138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1354138568Ssam			    "[%s] scan candidate purged from cache "
1355138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1356140766Ssam			    ieee80211_node_refcnt(ni));
1357138568Ssam			node_reclaim(nt, ni);
1358138568Ssam		}
1359138568Ssam	}
1360138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1361138568Ssam
1362138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1363138568Ssam}
1364138568Ssam
1365138568Ssam/*
1366138568Ssam * Timeout inactive stations and do related housekeeping.
1367138568Ssam * Note that we cannot hold the node lock while sending a
1368138568Ssam * frame as this would lead to a LOR.  Instead we use a
1369138568Ssam * generation number to mark nodes that we've scanned and
1370138568Ssam * drop the lock and restart a scan if we have to time out
1371138568Ssam * a node.  Since we are single-threaded by virtue of
1372120483Ssam * controlling the inactivity timer we can be sure this will
1373120483Ssam * process each node only once.
1374120483Ssam */
1375138568Ssamstatic void
1376138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt)
1377116742Ssam{
1378138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1379120483Ssam	struct ieee80211_node *ni;
1380138568Ssam	u_int gen;
1381148320Ssam	int isadhoc;
1382116742Ssam
1383148320Ssam	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1384148320Ssam		   ic->ic_opmode == IEEE80211_M_AHDEMO);
1385138568Ssam	IEEE80211_SCAN_LOCK(nt);
1386138568Ssam	gen = nt->nt_scangen++;
1387138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1388140766Ssam		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1389120483Ssamrestart:
1390138568Ssam	IEEE80211_NODE_LOCK(nt);
1391138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1392120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1393120483Ssam			continue;
1394120483Ssam		ni->ni_scangen = gen;
1395138568Ssam		/*
1396147788Ssam		 * Ignore entries for which have yet to receive an
1397147788Ssam		 * authentication frame.  These are transient and
1398147788Ssam		 * will be reclaimed when the last reference to them
1399147788Ssam		 * goes away (when frame xmits complete).
1400147788Ssam		 */
1401147788Ssam		if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1402147788Ssam			continue;
1403147788Ssam		/*
1404138568Ssam		 * Free fragment if not needed anymore
1405138568Ssam		 * (last fragment older than 1s).
1406138568Ssam		 * XXX doesn't belong here
1407138568Ssam		 */
1408138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1409138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1410138568Ssam			m_freem(ni->ni_rxfrag[0]);
1411138568Ssam			ni->ni_rxfrag[0] = NULL;
1412138568Ssam		}
1413140498Ssam		/*
1414140498Ssam		 * Special case ourself; we may be idle for extended periods
1415140498Ssam		 * of time and regardless reclaiming our state is wrong.
1416140498Ssam		 */
1417140498Ssam		if (ni == ic->ic_bss)
1418140498Ssam			continue;
1419138568Ssam		ni->ni_inact--;
1420148320Ssam		if (ni->ni_associd != 0 || isadhoc) {
1421119150Ssam			/*
1422138568Ssam			 * Age frames on the power save queue. The
1423138568Ssam			 * aging interval is 4 times the listen
1424138568Ssam			 * interval specified by the station.  This
1425138568Ssam			 * number is factored into the age calculations
1426138568Ssam			 * when the frame is placed on the queue.  We
1427138568Ssam			 * store ages as time differences we can check
1428138568Ssam			 * and/or adjust only the head of the list.
1429138568Ssam			 */
1430138568Ssam			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1431138568Ssam				struct mbuf *m;
1432138568Ssam				int discard = 0;
1433138568Ssam
1434138568Ssam				IEEE80211_NODE_SAVEQ_LOCK(ni);
1435138568Ssam				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1436138568Ssam				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1437138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1438138568Ssam					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1439138568Ssam					m_freem(m);
1440138568Ssam					discard++;
1441138568Ssam				}
1442138568Ssam				if (m != NULL)
1443138568Ssam					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1444138568Ssam				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1445138568Ssam
1446138568Ssam				if (discard != 0) {
1447138568Ssam					IEEE80211_DPRINTF(ic,
1448138568Ssam					    IEEE80211_MSG_POWER,
1449138568Ssam					    "[%s] discard %u frames for age\n",
1450138568Ssam					    ether_sprintf(ni->ni_macaddr),
1451138568Ssam					    discard);
1452138568Ssam					IEEE80211_NODE_STAT_ADD(ni,
1453138568Ssam						ps_discard, discard);
1454138568Ssam					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1455148304Ssam						ic->ic_set_tim(ni, 0);
1456138568Ssam				}
1457138568Ssam			}
1458138568Ssam			/*
1459138568Ssam			 * Probe the station before time it out.  We
1460138568Ssam			 * send a null data frame which may not be
1461138568Ssam			 * universally supported by drivers (need it
1462138568Ssam			 * for ps-poll support so it should be...).
1463138568Ssam			 */
1464139528Ssam			if (0 < ni->ni_inact &&
1465139528Ssam			    ni->ni_inact <= ic->ic_inact_probe) {
1466148320Ssam				IEEE80211_NOTE(ic,
1467148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1468148320Ssam				    ni, "%s",
1469148320Ssam				    "probe station due to inactivity");
1470138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1471148301Ssam				ieee80211_send_nulldata(ni);
1472138568Ssam				/* XXX stat? */
1473138568Ssam				goto restart;
1474138568Ssam			}
1475138568Ssam		}
1476138568Ssam		if (ni->ni_inact <= 0) {
1477148320Ssam			IEEE80211_NOTE(ic,
1478148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1479148320Ssam			    "station timed out due to inactivity "
1480148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1481138568Ssam			/*
1482138568Ssam			 * Send a deauthenticate frame and drop the station.
1483138568Ssam			 * This is somewhat complicated due to reference counts
1484138568Ssam			 * and locking.  At this point a station will typically
1485138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1486138568Ssam			 * will do a "free" of the node which will drop the
1487138568Ssam			 * reference count.  But in the meantime a reference
1488138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1489138568Ssam			 * of the node will happen either after the tx is
1490138568Ssam			 * completed or by ieee80211_node_leave.
1491120483Ssam			 *
1492138568Ssam			 * Separately we must drop the node lock before sending
1493138568Ssam			 * in case the driver takes a lock, as this will result
1494138568Ssam			 * in  LOR between the node lock and the driver lock.
1495119150Ssam			 */
1496138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1497138568Ssam			if (ni->ni_associd != 0) {
1498138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
1499138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1500138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1501138568Ssam			}
1502138568Ssam			ieee80211_node_leave(ic, ni);
1503121180Ssam			ic->ic_stats.is_node_timeout++;
1504120483Ssam			goto restart;
1505120483Ssam		}
1506116742Ssam	}
1507138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1508138568Ssam
1509138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1510138568Ssam
1511138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1512116742Ssam}
1513116742Ssam
1514116742Ssamvoid
1515138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1516116742Ssam{
1517116742Ssam	struct ieee80211_node *ni;
1518138568Ssam	u_int gen;
1519116742Ssam
1520138568Ssam	IEEE80211_SCAN_LOCK(nt);
1521138568Ssam	gen = nt->nt_scangen++;
1522138568Ssamrestart:
1523138568Ssam	IEEE80211_NODE_LOCK(nt);
1524138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1525138568Ssam		if (ni->ni_scangen != gen) {
1526138568Ssam			ni->ni_scangen = gen;
1527138568Ssam			(void) ieee80211_ref_node(ni);
1528138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1529138568Ssam			(*f)(arg, ni);
1530138568Ssam			ieee80211_free_node(ni);
1531138568Ssam			goto restart;
1532138568Ssam		}
1533138568Ssam	}
1534138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1535138568Ssam
1536138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1537116742Ssam}
1538138568Ssam
1539138568Ssamvoid
1540138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1541138568Ssam{
1542138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
1543138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1544138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
1545138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1546138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
1547138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1548138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1549138568Ssam		ni->ni_txseqs[0],
1550138568Ssam		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1551138568Ssam		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1552138568Ssam		ni->ni_rxfragstamp);
1553138568Ssam	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
1554138568Ssam		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
1555138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1556138568Ssam		ether_sprintf(ni->ni_bssid),
1557138568Ssam		ni->ni_esslen, ni->ni_essid,
1558138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1559138568Ssam	printf("\tfails %u inact %u txrate %u\n",
1560138568Ssam		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1561138568Ssam}
1562138568Ssam
1563138568Ssamvoid
1564138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
1565138568Ssam{
1566138568Ssam	ieee80211_iterate_nodes(nt,
1567138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
1568138568Ssam}
1569138568Ssam
1570138568Ssam/*
1571138568Ssam * Handle a station joining an 11g network.
1572138568Ssam */
1573138568Ssamstatic void
1574138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1575138568Ssam{
1576138568Ssam
1577138568Ssam	/*
1578138568Ssam	 * Station isn't capable of short slot time.  Bump
1579138568Ssam	 * the count of long slot time stations and disable
1580138568Ssam	 * use of short slot time.  Note that the actual switch
1581138568Ssam	 * over to long slot time use may not occur until the
1582138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1583138568Ssam	 */
1584138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1585138568Ssam		ic->ic_longslotsta++;
1586138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1587138568Ssam		    "[%s] station needs long slot time, count %d\n",
1588138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1589138568Ssam		/* XXX vap's w/ conflicting needs won't work */
1590138568Ssam		ieee80211_set_shortslottime(ic, 0);
1591138568Ssam	}
1592138568Ssam	/*
1593138568Ssam	 * If the new station is not an ERP station
1594138568Ssam	 * then bump the counter and enable protection
1595138568Ssam	 * if configured.
1596138568Ssam	 */
1597138568Ssam	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1598138568Ssam		ic->ic_nonerpsta++;
1599138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1600138568Ssam		    "[%s] station is !ERP, %d non-ERP stations associated\n",
1601138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1602138568Ssam		/*
1603138568Ssam		 * If protection is configured, enable it.
1604138568Ssam		 */
1605138568Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
1606138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1607138568Ssam			    "%s: enable use of protection\n", __func__);
1608138568Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1609138568Ssam		}
1610138568Ssam		/*
1611138568Ssam		 * If station does not support short preamble
1612138568Ssam		 * then we must enable use of Barker preamble.
1613138568Ssam		 */
1614138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1615138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1616138568Ssam			    "[%s] station needs long preamble\n",
1617138568Ssam			    ether_sprintf(ni->ni_macaddr));
1618138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1619138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1620138568Ssam		}
1621138568Ssam	} else
1622138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
1623138568Ssam}
1624138568Ssam
1625138568Ssamvoid
1626138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1627138568Ssam{
1628138568Ssam	int newassoc;
1629138568Ssam
1630138568Ssam	if (ni->ni_associd == 0) {
1631138568Ssam		u_int16_t aid;
1632138568Ssam
1633138568Ssam		/*
1634138568Ssam		 * It would be good to search the bitmap
1635138568Ssam		 * more efficiently, but this will do for now.
1636138568Ssam		 */
1637138568Ssam		for (aid = 1; aid < ic->ic_max_aid; aid++) {
1638138568Ssam			if (!IEEE80211_AID_ISSET(aid,
1639138568Ssam			    ic->ic_aid_bitmap))
1640138568Ssam				break;
1641138568Ssam		}
1642138568Ssam		if (aid >= ic->ic_max_aid) {
1643138568Ssam			IEEE80211_SEND_MGMT(ic, ni, resp,
1644138568Ssam			    IEEE80211_REASON_ASSOC_TOOMANY);
1645138568Ssam			ieee80211_node_leave(ic, ni);
1646138568Ssam			return;
1647138568Ssam		}
1648138568Ssam		ni->ni_associd = aid | 0xc000;
1649138568Ssam		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1650138568Ssam		ic->ic_sta_assoc++;
1651138568Ssam		newassoc = 1;
1652139522Ssam		if (ic->ic_curmode == IEEE80211_MODE_11G ||
1653139522Ssam		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1654138568Ssam			ieee80211_node_join_11g(ic, ni);
1655138568Ssam	} else
1656138568Ssam		newassoc = 0;
1657138568Ssam
1658138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1659139523Ssam	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
1660139523Ssam	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
1661139523Ssam	    IEEE80211_NODE_AID(ni),
1662139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1663139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1664139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1665139523Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
1666139523Ssam	);
1667138568Ssam
1668138568Ssam	/* give driver a chance to setup state like ni_txrate */
1669139524Ssam	if (ic->ic_newassoc != NULL)
1670148307Ssam		ic->ic_newassoc(ni, newassoc);
1671139528Ssam	ni->ni_inact_reload = ic->ic_inact_auth;
1672139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1673138568Ssam	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1674138568Ssam	/* tell the authenticator about new station */
1675138568Ssam	if (ic->ic_auth->ia_node_join != NULL)
1676138568Ssam		ic->ic_auth->ia_node_join(ic, ni);
1677138568Ssam	ieee80211_notify_node_join(ic, ni, newassoc);
1678138568Ssam}
1679138568Ssam
1680138568Ssam/*
1681138568Ssam * Handle a station leaving an 11g network.
1682138568Ssam */
1683138568Ssamstatic void
1684138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1685138568Ssam{
1686138568Ssam
1687139522Ssam	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
1688139522Ssam		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
1689138568Ssam		("not in 11g, curmode %x", ic->ic_curmode));
1690138568Ssam
1691138568Ssam	/*
1692138568Ssam	 * If a long slot station do the slot time bookkeeping.
1693138568Ssam	 */
1694138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1695138568Ssam		KASSERT(ic->ic_longslotsta > 0,
1696138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
1697138568Ssam		ic->ic_longslotsta--;
1698138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1699138568Ssam		    "[%s] long slot time station leaves, count now %d\n",
1700138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1701138568Ssam		if (ic->ic_longslotsta == 0) {
1702138568Ssam			/*
1703138568Ssam			 * Re-enable use of short slot time if supported
1704138568Ssam			 * and not operating in IBSS mode (per spec).
1705138568Ssam			 */
1706138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1707138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
1708138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1709138568Ssam				    "%s: re-enable use of short slot time\n",
1710138568Ssam				    __func__);
1711138568Ssam				ieee80211_set_shortslottime(ic, 1);
1712138568Ssam			}
1713138568Ssam		}
1714138568Ssam	}
1715138568Ssam	/*
1716138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
1717138568Ssam	 */
1718138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1719138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
1720138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1721138568Ssam		ic->ic_nonerpsta--;
1722138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1723138568Ssam		    "[%s] non-ERP station leaves, count now %d\n",
1724138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1725138568Ssam		if (ic->ic_nonerpsta == 0) {
1726138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1727138568Ssam				"%s: disable use of protection\n", __func__);
1728138568Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1729138568Ssam			/* XXX verify mode? */
1730138568Ssam			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1731138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1732138568Ssam				    "%s: re-enable use of short preamble\n",
1733138568Ssam				    __func__);
1734138568Ssam				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1735138568Ssam				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1736138568Ssam			}
1737138568Ssam		}
1738138568Ssam	}
1739138568Ssam}
1740138568Ssam
1741138568Ssam/*
1742138568Ssam * Handle bookkeeping for station deauthentication/disassociation
1743138568Ssam * when operating as an ap.
1744138568Ssam */
1745138568Ssamvoid
1746138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1747138568Ssam{
1748140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1749138568Ssam
1750138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1751138568Ssam	    "[%s] station with aid %d leaves\n",
1752138568Ssam	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
1753138568Ssam
1754138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1755138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS ||
1756138568Ssam		ic->ic_opmode == IEEE80211_M_AHDEMO,
1757138568Ssam		("unexpected operating mode %u", ic->ic_opmode));
1758138568Ssam	/*
1759138568Ssam	 * If node wasn't previously associated all
1760138568Ssam	 * we need to do is reclaim the reference.
1761138568Ssam	 */
1762138568Ssam	/* XXX ibss mode bypasses 11g and notification */
1763138568Ssam	if (ni->ni_associd == 0)
1764138568Ssam		goto done;
1765138568Ssam	/*
1766138568Ssam	 * Tell the authenticator the station is leaving.
1767138568Ssam	 * Note that we must do this before yanking the
1768138568Ssam	 * association id as the authenticator uses the
1769138568Ssam	 * associd to locate it's state block.
1770138568Ssam	 */
1771138568Ssam	if (ic->ic_auth->ia_node_leave != NULL)
1772138568Ssam		ic->ic_auth->ia_node_leave(ic, ni);
1773138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1774138568Ssam	ni->ni_associd = 0;
1775138568Ssam	ic->ic_sta_assoc--;
1776138568Ssam
1777139522Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G ||
1778139522Ssam	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1779138568Ssam		ieee80211_node_leave_11g(ic, ni);
1780138568Ssam	/*
1781138568Ssam	 * Cleanup station state.  In particular clear various
1782138568Ssam	 * state that might otherwise be reused if the node
1783138568Ssam	 * is reused before the reference count goes to zero
1784138568Ssam	 * (and memory is reclaimed).
1785138568Ssam	 */
1786138568Ssam	ieee80211_sta_leave(ic, ni);
1787138568Ssamdone:
1788140499Ssam	/*
1789140499Ssam	 * Remove the node from any table it's recorded in and
1790140499Ssam	 * drop the caller's reference.  Removal from the table
1791140499Ssam	 * is important to insure the node is not reprocessed
1792140499Ssam	 * for inactivity.
1793140499Ssam	 */
1794140499Ssam	if (nt != NULL) {
1795140499Ssam		IEEE80211_NODE_LOCK(nt);
1796140499Ssam		node_reclaim(nt, ni);
1797140499Ssam		IEEE80211_NODE_UNLOCK(nt);
1798140499Ssam	} else
1799140499Ssam		ieee80211_free_node(ni);
1800138568Ssam}
1801138568Ssam
1802138568Ssamu_int8_t
1803138568Ssamieee80211_getrssi(struct ieee80211com *ic)
1804138568Ssam{
1805138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
1806140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1807138568Ssam	u_int32_t rssi_samples, rssi_total;
1808138568Ssam	struct ieee80211_node *ni;
1809138568Ssam
1810138568Ssam	rssi_total = 0;
1811138568Ssam	rssi_samples = 0;
1812138568Ssam	switch (ic->ic_opmode) {
1813138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
1814138568Ssam		/* XXX locking */
1815140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1816138568Ssam			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
1817138568Ssam				rssi_samples++;
1818138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
1819138568Ssam			}
1820138568Ssam		break;
1821138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
1822138568Ssam		/* XXX locking */
1823140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1824138568Ssam			rssi_samples++;
1825138568Ssam			rssi_total += ic->ic_node_getrssi(ni);
1826138568Ssam		}
1827138568Ssam		break;
1828138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
1829138568Ssam		/* XXX locking */
1830140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1831138568Ssam			if (IEEE80211_AID(ni->ni_associd) != 0) {
1832138568Ssam				rssi_samples++;
1833138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
1834138568Ssam			}
1835138568Ssam		break;
1836138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
1837138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
1838138568Ssam	default:
1839138568Ssam		if (ic->ic_bss != NULL)
1840138568Ssam			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1841138568Ssam		rssi_samples = 1;
1842138568Ssam		break;
1843138568Ssam	}
1844138568Ssam	return rssi_total / NZ(rssi_samples);
1845138568Ssam#undef NZ
1846138568Ssam}
1847138568Ssam
1848138568Ssam/*
1849138568Ssam * Indicate whether there are frames queued for a station in power-save mode.
1850138568Ssam */
1851138568Ssamstatic void
1852148304Ssamieee80211_set_tim(struct ieee80211_node *ni, int set)
1853138568Ssam{
1854148304Ssam	struct ieee80211com *ic = ni->ni_ic;
1855138568Ssam	u_int16_t aid;
1856138568Ssam
1857138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1858138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS,
1859138568Ssam		("operating mode %u", ic->ic_opmode));
1860138568Ssam
1861138568Ssam	aid = IEEE80211_AID(ni->ni_associd);
1862138568Ssam	KASSERT(aid < ic->ic_max_aid,
1863138568Ssam		("bogus aid %u, max %u", aid, ic->ic_max_aid));
1864138568Ssam
1865138568Ssam	IEEE80211_BEACON_LOCK(ic);
1866138568Ssam	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
1867138568Ssam		if (set) {
1868138568Ssam			setbit(ic->ic_tim_bitmap, aid);
1869138568Ssam			ic->ic_ps_pending++;
1870138568Ssam		} else {
1871138568Ssam			clrbit(ic->ic_tim_bitmap, aid);
1872138568Ssam			ic->ic_ps_pending--;
1873138568Ssam		}
1874138568Ssam		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
1875138568Ssam	}
1876138568Ssam	IEEE80211_BEACON_UNLOCK(ic);
1877138568Ssam}
1878138568Ssam
1879138568Ssam/*
1880138568Ssam * Node table support.
1881138568Ssam */
1882138568Ssam
1883138568Ssamstatic void
1884138568Ssamieee80211_node_table_init(struct ieee80211com *ic,
1885138568Ssam	struct ieee80211_node_table *nt,
1886138568Ssam	const char *name, int inact,
1887138568Ssam	void (*timeout)(struct ieee80211_node_table *))
1888138568Ssam{
1889138568Ssam
1890138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1891138568Ssam		"%s %s table, inact %u\n", __func__, name, inact);
1892138568Ssam
1893138568Ssam	nt->nt_ic = ic;
1894138568Ssam	/* XXX need unit */
1895138568Ssam	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1896138568Ssam	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1897138568Ssam	TAILQ_INIT(&nt->nt_node);
1898138568Ssam	nt->nt_name = name;
1899138568Ssam	nt->nt_scangen = 1;
1900138568Ssam	nt->nt_inact_init = inact;
1901138568Ssam	nt->nt_timeout = timeout;
1902138568Ssam}
1903138568Ssam
1904138568Ssamvoid
1905138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt)
1906138568Ssam{
1907138568Ssam
1908138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1909138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1910138568Ssam
1911138568Ssam	IEEE80211_NODE_LOCK(nt);
1912138568Ssam	nt->nt_inact_timer = 0;
1913138568Ssam	ieee80211_free_allnodes_locked(nt);
1914138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1915138568Ssam}
1916138568Ssam
1917138568Ssamstatic void
1918138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1919138568Ssam{
1920138568Ssam
1921138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1922138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1923138568Ssam
1924138568Ssam	ieee80211_free_allnodes_locked(nt);
1925138568Ssam	IEEE80211_SCAN_LOCK_DESTROY(nt);
1926138568Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1927138568Ssam}
1928