ieee80211_node.c revision 148307
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 148307 2005-07-22 17:57:16Z 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#ifdef notyet
339138568Ssam			/* XXX driver state change */
340116742Ssam			/*
341138568Ssam			 * Scan next channel. If doing an active scan
342138568Ssam			 * and the channel is not marked passive-only
343138568Ssam			 * then send a probe request.  Otherwise just
344138568Ssam			 * listen for beacons on the channel.
345116742Ssam			 */
346138568Ssam			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
347138568Ssam			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
348138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
349138568Ssam				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
350138568Ssam			}
351138568Ssam#else
352138568Ssam			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
353138568Ssam#endif
354138568Ssam			return 1;
355116742Ssam		}
356138568Ssam	} while (chan != ic->ic_bss->ni_chan);
357138568Ssam	ieee80211_end_scan(ic);
358138568Ssam	return 0;
359116742Ssam}
360116742Ssam
361141658Ssamstatic __inline void
362141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
363141658Ssam{
364141658Ssam	/* propagate useful state */
365141658Ssam	nbss->ni_authmode = obss->ni_authmode;
366141658Ssam	nbss->ni_txpower = obss->ni_txpower;
367141658Ssam	nbss->ni_vlan = obss->ni_vlan;
368141658Ssam	nbss->ni_rsn = obss->ni_rsn;
369141658Ssam	/* XXX statistics? */
370141658Ssam}
371141658Ssam
372116742Ssamvoid
373116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
374116742Ssam{
375140753Ssam	struct ieee80211_node_table *nt;
376116742Ssam	struct ieee80211_node *ni;
377116742Ssam
378138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
379138568Ssam		"%s: creating ibss\n", __func__);
380138568Ssam
381138568Ssam	/*
382138568Ssam	 * Create the station/neighbor table.  Note that for adhoc
383138568Ssam	 * mode we make the initial inactivity timer longer since
384138568Ssam	 * we create nodes only through discovery and they typically
385138568Ssam	 * are long-lived associations.
386138568Ssam	 */
387140753Ssam	nt = &ic->ic_sta;
388140753Ssam	IEEE80211_NODE_LOCK(nt);
389140753Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
390140753Ssam		nt->nt_name = "station";
391140753Ssam		nt->nt_inact_init = ic->ic_inact_init;
392140753Ssam	} else {
393140753Ssam		nt->nt_name = "neighbor";
394140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
395140753Ssam	}
396140753Ssam	IEEE80211_NODE_UNLOCK(nt);
397140753Ssam
398140753Ssam	ni = ieee80211_alloc_node(nt, ic->ic_myaddr);
399140753Ssam	if (ni == NULL) {
400140753Ssam		/* XXX recovery? */
401138568Ssam		return;
402138568Ssam	}
403116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
404116742Ssam	ni->ni_esslen = ic->ic_des_esslen;
405116742Ssam	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
406141658Ssam	copy_bss(ni, ic->ic_bss);
407116742Ssam	ni->ni_intval = ic->ic_lintval;
408138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
409116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
410116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
411116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
412116742Ssam		ni->ni_fhindex = 1;
413116742Ssam	}
414138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
415138568Ssam		ic->ic_flags |= IEEE80211_F_SIBSS;
416138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
417143300Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
418143300Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
419143300Ssam		else
420143300Ssam			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
421138568Ssam	}
422138568Ssam	/*
423138568Ssam	 * Fix the channel and related attributes.
424138568Ssam	 */
425138568Ssam	ieee80211_set_chan(ic, ni, chan);
426138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
427138568Ssam	/*
428138568Ssam	 * Do mode-specific rate setup.
429138568Ssam	 */
430138568Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G) {
431138568Ssam		/*
432138568Ssam		 * Use a mixed 11b/11g rate set.
433138568Ssam		 */
434138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
435138568Ssam	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
436138568Ssam		/*
437138568Ssam		 * Force pure 11b rate set.
438138568Ssam		 */
439138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
440138568Ssam	}
441138568Ssam
442140753Ssam	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
443116742Ssam}
444116742Ssam
445138568Ssamvoid
446138568Ssamieee80211_reset_bss(struct ieee80211com *ic)
447138568Ssam{
448138568Ssam	struct ieee80211_node *ni, *obss;
449138568Ssam
450138568Ssam	ieee80211_node_table_reset(&ic->ic_scan);
451140753Ssam	ieee80211_node_table_reset(&ic->ic_sta);
452140753Ssam
453138568Ssam	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
454138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
455138568Ssam	obss = ic->ic_bss;
456138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);
457141658Ssam	if (obss != NULL) {
458141658Ssam		copy_bss(ni, obss);
459141658Ssam		ni->ni_intval = ic->ic_lintval;
460138568Ssam		ieee80211_free_node(obss);
461141658Ssam	}
462138568Ssam}
463138568Ssam
464127767Ssamstatic int
465138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
466127767Ssam{
467127767Ssam        u_int8_t rate;
468127767Ssam        int fail;
469127767Ssam
470127767Ssam	fail = 0;
471127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
472127767Ssam		fail |= 0x01;
473127767Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
474127767Ssam	    ni->ni_chan != ic->ic_des_chan)
475127767Ssam		fail |= 0x01;
476127767Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
477127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
478127767Ssam			fail |= 0x02;
479127767Ssam	} else {
480127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
481127767Ssam			fail |= 0x02;
482127767Ssam	}
483138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
484127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
485127767Ssam			fail |= 0x04;
486127767Ssam	} else {
487127767Ssam		/* XXX does this mean privacy is supported or required? */
488127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
489127767Ssam			fail |= 0x04;
490127767Ssam	}
491148299Ssam	rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
492127767Ssam	if (rate & IEEE80211_RATE_BASIC)
493127767Ssam		fail |= 0x08;
494127767Ssam	if (ic->ic_des_esslen != 0 &&
495127767Ssam	    (ni->ni_esslen != ic->ic_des_esslen ||
496127767Ssam	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
497127767Ssam		fail |= 0x10;
498127767Ssam	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
499127767Ssam	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
500127767Ssam		fail |= 0x20;
501127767Ssam#ifdef IEEE80211_DEBUG
502138568Ssam	if (ieee80211_msg_scan(ic)) {
503127767Ssam		printf(" %c %s", fail ? '-' : '+',
504127767Ssam		    ether_sprintf(ni->ni_macaddr));
505127767Ssam		printf(" %s%c", ether_sprintf(ni->ni_bssid),
506127767Ssam		    fail & 0x20 ? '!' : ' ');
507127767Ssam		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
508127767Ssam			fail & 0x01 ? '!' : ' ');
509127767Ssam		printf(" %+4d", ni->ni_rssi);
510127767Ssam		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
511127767Ssam		    fail & 0x08 ? '!' : ' ');
512127767Ssam		printf(" %4s%c",
513127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
514127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
515127767Ssam		    "????",
516127767Ssam		    fail & 0x02 ? '!' : ' ');
517127767Ssam		printf(" %3s%c ",
518127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
519127767Ssam		    "wep" : "no",
520127767Ssam		    fail & 0x04 ? '!' : ' ');
521127767Ssam		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
522127767Ssam		printf("%s\n", fail & 0x10 ? "!" : "");
523127767Ssam	}
524127767Ssam#endif
525127767Ssam	return fail;
526127767Ssam}
527127767Ssam
528138568Ssamstatic __inline u_int8_t
529138568Ssammaxrate(const struct ieee80211_node *ni)
530138568Ssam{
531138568Ssam	const struct ieee80211_rateset *rs = &ni->ni_rates;
532138568Ssam	/* NB: assumes rate set is sorted (happens on frame receive) */
533138568Ssam	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
534138568Ssam}
535138568Ssam
536116742Ssam/*
537138568Ssam * Compare the capabilities of two nodes and decide which is
538138568Ssam * more desirable (return >0 if a is considered better).  Note
539138568Ssam * that we assume compatibility/usability has already been checked
540138568Ssam * so we don't need to (e.g. validate whether privacy is supported).
541138568Ssam * Used to select the best scan candidate for association in a BSS.
542138568Ssam */
543138568Ssamstatic int
544138568Ssamieee80211_node_compare(struct ieee80211com *ic,
545138568Ssam		       const struct ieee80211_node *a,
546138568Ssam		       const struct ieee80211_node *b)
547138568Ssam{
548138568Ssam	u_int8_t maxa, maxb;
549138568Ssam	u_int8_t rssia, rssib;
550138568Ssam
551138568Ssam	/* privacy support preferred */
552138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
553138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
554138568Ssam		return 1;
555138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
556138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
557138568Ssam		return -1;
558138568Ssam
559138568Ssam	rssia = ic->ic_node_getrssi(a);
560138568Ssam	rssib = ic->ic_node_getrssi(b);
561139543Ssam	if (abs(rssib - rssia) < 5) {
562139543Ssam		/* best/max rate preferred if signal level close enough XXX */
563139543Ssam		maxa = maxrate(a);
564139543Ssam		maxb = maxrate(b);
565139543Ssam		if (maxa != maxb)
566139543Ssam			return maxa - maxb;
567139543Ssam		/* XXX use freq for channel preference */
568139543Ssam		/* for now just prefer 5Ghz band to all other bands */
569139543Ssam		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
570139543Ssam		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
571139543Ssam			return 1;
572139543Ssam		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
573139543Ssam		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
574139543Ssam			return -1;
575139543Ssam	}
576138568Ssam	/* all things being equal, use signal level */
577138568Ssam	return rssia - rssib;
578138568Ssam}
579138568Ssam
580138568Ssam/*
581140441Ssam * Mark an ongoing scan stopped.
582140441Ssam */
583140441Ssamvoid
584140441Ssamieee80211_cancel_scan(struct ieee80211com *ic)
585140441Ssam{
586140441Ssam
587140441Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
588140441Ssam		__func__,
589140441Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
590140441Ssam
591140441Ssam	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
592140441Ssam}
593140441Ssam
594140441Ssam/*
595116742Ssam * Complete a scan of potential channels.
596116742Ssam */
597116742Ssamvoid
598138568Ssamieee80211_end_scan(struct ieee80211com *ic)
599116742Ssam{
600140448Ssam	struct ieee80211_node_table *nt = &ic->ic_scan;
601140448Ssam	struct ieee80211_node *ni, *selbs;
602116742Ssam
603140441Ssam	ieee80211_cancel_scan(ic);
604140441Ssam	ieee80211_notify_scan_done(ic);
605116742Ssam
606116742Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
607138568Ssam		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
608138568Ssam		int i, bestchan;
609138568Ssam		u_int8_t rssi;
610138568Ssam
611116742Ssam		/*
612116742Ssam		 * The passive scan to look for existing AP's completed,
613116742Ssam		 * select a channel to camp on.  Identify the channels
614116742Ssam		 * that already have one or more AP's and try to locate
615140753Ssam		 * an unoccupied one.  If that fails, pick a channel that
616138568Ssam		 * looks to be quietest.
617116742Ssam		 */
618138568Ssam		memset(maxrssi, 0, sizeof(maxrssi));
619140448Ssam		IEEE80211_NODE_LOCK(nt);
620140448Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
621138568Ssam			rssi = ic->ic_node_getrssi(ni);
622138568Ssam			i = ieee80211_chan2ieee(ic, ni->ni_chan);
623138568Ssam			if (rssi > maxrssi[i])
624138568Ssam				maxrssi[i] = rssi;
625116742Ssam		}
626140448Ssam		IEEE80211_NODE_UNLOCK(nt);
627138568Ssam		/* XXX select channel more intelligently */
628138568Ssam		bestchan = -1;
629116742Ssam		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
630138568Ssam			if (isset(ic->ic_chan_active, i)) {
631138568Ssam				/*
632138568Ssam				 * If the channel is unoccupied the max rssi
633138568Ssam				 * should be zero; just take it.  Otherwise
634138568Ssam				 * track the channel with the lowest rssi and
635138568Ssam				 * use that when all channels appear occupied.
636138568Ssam				 */
637138568Ssam				if (maxrssi[i] == 0) {
638138568Ssam					bestchan = i;
639116742Ssam					break;
640138568Ssam				}
641143715Ssam				if (bestchan == -1 ||
642143715Ssam				    maxrssi[i] < maxrssi[bestchan])
643138568Ssam					bestchan = i;
644138568Ssam			}
645138568Ssam		if (bestchan != -1) {
646138568Ssam			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
647138568Ssam			return;
648116742Ssam		}
649138568Ssam		/* no suitable channel, should not happen */
650138568Ssam	}
651138568Ssam
652138568Ssam	/*
653138568Ssam	 * When manually sequencing the state machine; scan just once
654138568Ssam	 * regardless of whether we have a candidate or not.  The
655138568Ssam	 * controlling application is expected to setup state and
656138568Ssam	 * initiate an association.
657138568Ssam	 */
658138568Ssam	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
659116742Ssam		return;
660138568Ssam	/*
661138568Ssam	 * Automatic sequencing; look for a candidate and
662138568Ssam	 * if found join the network.
663138568Ssam	 */
664140448Ssam	/* NB: unlocked read should be ok */
665140448Ssam	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
666138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
667138568Ssam			"%s: no scan candidate\n", __func__);
668116742Ssam  notfound:
669116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS &&
670116742Ssam		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
671116742Ssam		    ic->ic_des_esslen != 0) {
672116742Ssam			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
673116742Ssam			return;
674116742Ssam		}
675116742Ssam		/*
676116742Ssam		 * Reset the list of channels to scan and start again.
677116742Ssam		 */
678138568Ssam		ieee80211_reset_scan(ic);
679138568Ssam		ic->ic_flags |= IEEE80211_F_SCAN;
680138568Ssam		ieee80211_next_scan(ic);
681116742Ssam		return;
682116742Ssam	}
683116742Ssam	selbs = NULL;
684138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
685138568Ssam	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
686140448Ssam	IEEE80211_NODE_LOCK(nt);
687140448Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
688116742Ssam		if (ni->ni_fails) {
689116742Ssam			/*
690116742Ssam			 * The configuration of the access points may change
691116742Ssam			 * during my scan.  So delete the entry for the AP
692116742Ssam			 * and retry to associate if there is another beacon.
693116742Ssam			 */
694138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
695138568Ssam				"%s: skip scan candidate %s, fails %u\n",
696138568Ssam				__func__, ether_sprintf(ni->ni_macaddr),
697138568Ssam				ni->ni_fails);
698140448Ssam			ni->ni_fails++;
699140448Ssam#if 0
700116742Ssam			if (ni->ni_fails++ > 2)
701138568Ssam				ieee80211_free_node(ni);
702140448Ssam#endif
703116742Ssam			continue;
704116742Ssam		}
705138568Ssam		if (ieee80211_match_bss(ic, ni) == 0) {
706116742Ssam			if (selbs == NULL)
707116742Ssam				selbs = ni;
708140448Ssam			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
709116742Ssam				selbs = ni;
710116742Ssam		}
711116742Ssam	}
712140448Ssam	if (selbs != NULL)		/* NB: grab ref while dropping lock */
713140448Ssam		(void) ieee80211_ref_node(selbs);
714140448Ssam	IEEE80211_NODE_UNLOCK(nt);
715116742Ssam	if (selbs == NULL)
716116742Ssam		goto notfound;
717138568Ssam	if (!ieee80211_sta_join(ic, selbs)) {
718140448Ssam		ieee80211_free_node(selbs);
719138568Ssam		goto notfound;
720138568Ssam	}
721138568Ssam}
722138568Ssam
723138568Ssam/*
724138568Ssam * Handle 802.11 ad hoc network merge.  The
725138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
726138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
727138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
728138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
729138568Ssam * sends beacons with the greatest TSF timestamp.
730138568Ssam *
731138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
732138568Ssam *
733138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
734138568Ssam */
735138568Ssamint
736148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
737138568Ssam{
738148306Ssam	struct ieee80211com *ic = ni->ni_ic;
739138568Ssam
740140453Ssam	if (ni == ic->ic_bss ||
741140453Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
742138568Ssam		/* unchanged, nothing to do */
743138568Ssam		return 0;
744138568Ssam	}
745138568Ssam	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
746138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
747138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
748138568Ssam		ic->ic_stats.is_ibss_capmismatch++;
749138568Ssam		return 0;
750138568Ssam	}
751138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
752138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
753138568Ssam		ether_sprintf(ni->ni_bssid),
754138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
755138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
756138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
757138568Ssam	);
758140753Ssam	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
759138568Ssam}
760138568Ssam
761138568Ssam/*
762138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
763138568Ssam * be passed in with a held reference.
764138568Ssam */
765138568Ssamint
766138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
767138568Ssam{
768138568Ssam	struct ieee80211_node *obss;
769138568Ssam
770116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
771140753Ssam		struct ieee80211_node_table *nt;
772138568Ssam		/*
773140440Ssam		 * Delete unusable rates; we've already checked
774140440Ssam		 * that the negotiated rate set is acceptable.
775138568Ssam		 */
776148299Ssam		ieee80211_fix_rate(selbs, IEEE80211_F_DODEL);
777127772Ssam		/*
778140753Ssam		 * Fillin the neighbor table; it will already
779139521Ssam		 * exist if we are simply switching mastership.
780140753Ssam		 * XXX ic_sta always setup so this is unnecessary?
781127772Ssam		 */
782140753Ssam		nt = &ic->ic_sta;
783140753Ssam		IEEE80211_NODE_LOCK(nt);
784140753Ssam		nt->nt_name = "neighbor";
785140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
786140753Ssam		IEEE80211_NODE_UNLOCK(nt);
787138568Ssam	}
788138568Ssam
789138568Ssam	/*
790138568Ssam	 * Committed to selbs, setup state.
791138568Ssam	 */
792138568Ssam	obss = ic->ic_bss;
793140753Ssam	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
794138568Ssam	if (obss != NULL)
795138568Ssam		ieee80211_free_node(obss);
796138568Ssam	/*
797138568Ssam	 * Set the erp state (mostly the slot time) to deal with
798138568Ssam	 * the auto-select case; this should be redundant if the
799138568Ssam	 * mode is locked.
800138568Ssam	 */
801138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
802138568Ssam	ieee80211_reset_erp(ic);
803138568Ssam	ieee80211_wme_initparams(ic);
804140753Ssam
805140753Ssam	if (ic->ic_opmode == IEEE80211_M_STA)
806140753Ssam		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
807140753Ssam	else
808117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
809138568Ssam	return 1;
810116742Ssam}
811116742Ssam
812138568Ssam/*
813138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
814138568Ssam * be passed in with a held reference.
815138568Ssam */
816138568Ssamvoid
817138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
818138568Ssam{
819138568Ssam	ic->ic_node_cleanup(ni);
820138568Ssam	ieee80211_notify_node_leave(ic, ni);
821138568Ssam}
822138568Ssam
823116742Ssamstatic struct ieee80211_node *
824138568Ssamnode_alloc(struct ieee80211_node_table *nt)
825116742Ssam{
826127768Ssam	struct ieee80211_node *ni;
827138568Ssam
828127768Ssam	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
829127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
830127768Ssam	return ni;
831116742Ssam}
832116742Ssam
833138568Ssam/*
834138568Ssam * Reclaim any resources in a node and reset any critical
835138568Ssam * state.  Typically nodes are free'd immediately after,
836138568Ssam * but in some cases the storage may be reused so we need
837138568Ssam * to insure consistent state (should probably fix that).
838138568Ssam */
839116742Ssamstatic void
840138568Ssamnode_cleanup(struct ieee80211_node *ni)
841116742Ssam{
842138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
843138568Ssam	struct ieee80211com *ic = ni->ni_ic;
844138568Ssam	int i, qlen;
845138568Ssam
846138568Ssam	/* NB: preserve ni_table */
847138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
848138568Ssam		ic->ic_ps_sta--;
849138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
850138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
851138568Ssam		    "[%s] power save mode off, %u sta's in ps mode\n",
852138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
853138568Ssam	}
854147788Ssam	/*
855147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
856147788Ssam	 * has happened.  This is probably not needed as the node
857147788Ssam	 * should always be removed from the table so not found but
858147788Ssam	 * do it just in case.
859147788Ssam	 */
860147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
861138568Ssam
862138568Ssam	/*
863138568Ssam	 * Drain power save queue and, if needed, clear TIM.
864138568Ssam	 */
865138568Ssam	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
866138568Ssam	if (qlen != 0 && ic->ic_set_tim != NULL)
867148304Ssam		ic->ic_set_tim(ni, 0);
868138568Ssam
869138568Ssam	ni->ni_associd = 0;
870138568Ssam	if (ni->ni_challenge != NULL) {
871138568Ssam		FREE(ni->ni_challenge, M_DEVBUF);
872138568Ssam		ni->ni_challenge = NULL;
873138568Ssam	}
874138568Ssam	/*
875138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
876138568Ssam	 * reusable during a re-auth/re-assoc state transition.
877138568Ssam	 * If we remove these data they will not be recreated
878138568Ssam	 * because they come from a probe-response or beacon frame
879138568Ssam	 * which cannot be expected prior to the association-response.
880138568Ssam	 * This should not be an issue when operating in other modes
881138568Ssam	 * as stations leaving always go through a full state transition
882138568Ssam	 * which will rebuild this state.
883138568Ssam	 *
884138568Ssam	 * XXX does this leave us open to inheriting old state?
885138568Ssam	 */
886138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
887138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
888138568Ssam			m_freem(ni->ni_rxfrag[i]);
889138568Ssam			ni->ni_rxfrag[i] = NULL;
890138568Ssam		}
891138568Ssam	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
892138568Ssam#undef N
893116742Ssam}
894116742Ssam
895116742Ssamstatic void
896138568Ssamnode_free(struct ieee80211_node *ni)
897116742Ssam{
898138568Ssam	struct ieee80211com *ic = ni->ni_ic;
899138568Ssam
900138568Ssam	ic->ic_node_cleanup(ni);
901138568Ssam	if (ni->ni_wpa_ie != NULL)
902138568Ssam		FREE(ni->ni_wpa_ie, M_DEVBUF);
903138568Ssam	if (ni->ni_wme_ie != NULL)
904138568Ssam		FREE(ni->ni_wme_ie, M_DEVBUF);
905138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
906138568Ssam	FREE(ni, M_80211_NODE);
907116742Ssam}
908116742Ssam
909120104Ssamstatic u_int8_t
910138568Ssamnode_getrssi(const struct ieee80211_node *ni)
911120104Ssam{
912120104Ssam	return ni->ni_rssi;
913120104Ssam}
914120104Ssam
915116742Ssamstatic void
916138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt,
917138568Ssam	struct ieee80211_node *ni, const u_int8_t *macaddr)
918116742Ssam{
919138568Ssam	struct ieee80211com *ic = nt->nt_ic;
920116742Ssam	int hash;
921116742Ssam
922138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
923140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
924138568Ssam		ether_sprintf(macaddr), nt->nt_name);
925138568Ssam
926116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
927116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
928138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
929138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
930138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
931138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
932138568Ssam	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
933139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
934139528Ssam	ni->ni_inact = ni->ni_inact_reload;
935138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
936138568Ssam
937138568Ssam	IEEE80211_NODE_LOCK(nt);
938138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
939138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
940138568Ssam	ni->ni_table = nt;
941138568Ssam	ni->ni_ic = ic;
942138568Ssam	IEEE80211_NODE_UNLOCK(nt);
943116742Ssam}
944116742Ssam
945116742Ssamstruct ieee80211_node *
946138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
947116742Ssam{
948138568Ssam	struct ieee80211com *ic = nt->nt_ic;
949138568Ssam	struct ieee80211_node *ni;
950138568Ssam
951138568Ssam	ni = ic->ic_node_alloc(nt);
952116742Ssam	if (ni != NULL)
953138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
954127769Ssam	else
955127769Ssam		ic->ic_stats.is_rx_nodealloc++;
956116742Ssam	return ni;
957116742Ssam}
958116742Ssam
959116742Ssamstruct ieee80211_node *
960138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
961116742Ssam{
962138568Ssam	struct ieee80211com *ic = nt->nt_ic;
963138568Ssam	struct ieee80211_node *ni;
964138568Ssam
965138568Ssam	ni = ic->ic_node_alloc(nt);
966116742Ssam	if (ni != NULL) {
967138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
968127770Ssam		/*
969127770Ssam		 * Inherit from ic_bss.
970127770Ssam		 */
971138568Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
972138568Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
973138568Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
974127770Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
975138568Ssam		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
976138568Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
977127770Ssam	} else
978127770Ssam		ic->ic_stats.is_rx_nodealloc++;
979116742Ssam	return ni;
980116742Ssam}
981116742Ssam
982127772Ssamstatic struct ieee80211_node *
983138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
984138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
985138568Ssam	const u_int8_t *macaddr, const char *func, int line)
986138568Ssam#else
987138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt,
988138568Ssam	const u_int8_t *macaddr)
989138568Ssam#endif
990116742Ssam{
991116742Ssam	struct ieee80211_node *ni;
992116742Ssam	int hash;
993116742Ssam
994138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
995127772Ssam
996116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
997138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
998116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
999138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1000138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1001138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1002140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1003140766Ssam			    func, line,
1004140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1005140766Ssam			    ieee80211_node_refcnt(ni));
1006138568Ssam#endif
1007127772Ssam			return ni;
1008116742Ssam		}
1009116742Ssam	}
1010127772Ssam	return NULL;
1011127772Ssam}
1012138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1013138568Ssam#define	_ieee80211_find_node(nt, mac) \
1014138568Ssam	_ieee80211_find_node_debug(nt, mac, func, line)
1015138568Ssam#endif
1016127772Ssam
1017127772Ssamstruct ieee80211_node *
1018138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1019138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1020138568Ssam	const u_int8_t *macaddr, const char *func, int line)
1021138568Ssam#else
1022138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1023138568Ssam#endif
1024127772Ssam{
1025127772Ssam	struct ieee80211_node *ni;
1026127772Ssam
1027138568Ssam	IEEE80211_NODE_LOCK(nt);
1028138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1029138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1030116742Ssam	return ni;
1031116742Ssam}
1032116742Ssam
1033116742Ssam/*
1034138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1035138568Ssam * Note that for the driver's benefit we we treat this like
1036138568Ssam * an association so the driver has an opportunity to setup
1037138568Ssam * it's private state.
1038138568Ssam */
1039138568Ssamstruct ieee80211_node *
1040138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1041138568Ssam	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1042138568Ssam{
1043138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1044138568Ssam	struct ieee80211_node *ni;
1045138568Ssam
1046138568Ssam	ni = ieee80211_dup_bss(nt, macaddr);
1047138568Ssam	if (ni != NULL) {
1048138568Ssam		/* XXX no rate negotiation; just dup */
1049138568Ssam		ni->ni_rates = ic->ic_bss->ni_rates;
1050139524Ssam		if (ic->ic_newassoc != NULL)
1051148307Ssam			ic->ic_newassoc(ni, 1);
1052138568Ssam		/* XXX not right for 802.1x/WPA */
1053148302Ssam		ieee80211_node_authorize(ni);
1054138568Ssam	}
1055138568Ssam	return ni;
1056138568Ssam}
1057138568Ssam
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#define	IS_CTL(wh) \
1076138568Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1077138568Ssam#define	IS_PSPOLL(wh) \
1078138568Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1079138568Ssam	struct ieee80211_node_table *nt;
1080138568Ssam	struct ieee80211_node *ni;
1081138568Ssam
1082138568Ssam	/* XXX may want scanned nodes in the neighbor table for adhoc */
1083138568Ssam	if (ic->ic_opmode == IEEE80211_M_STA ||
1084138568Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1085138568Ssam	    (ic->ic_flags & IEEE80211_F_SCAN))
1086138568Ssam		nt = &ic->ic_scan;
1087138568Ssam	else
1088140753Ssam		nt = &ic->ic_sta;
1089138568Ssam	/* XXX check ic_bss first in station mode */
1090138568Ssam	/* XXX 4-address frames? */
1091138568Ssam	IEEE80211_NODE_LOCK(nt);
1092138568Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1093138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr1);
1094138568Ssam	else
1095138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr2);
1096138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1097138568Ssam
1098138568Ssam	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
1099138568Ssam#undef IS_PSPOLL
1100138568Ssam#undef IS_CTL
1101138568Ssam}
1102138568Ssam
1103138568Ssam/*
1104127772Ssam * Return a reference to the appropriate node for sending
1105127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1106127772Ssam */
1107127772Ssamstruct ieee80211_node *
1108138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1109138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1110138568Ssam	const char *func, int line)
1111138568Ssam#else
1112138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1113138568Ssam#endif
1114127772Ssam{
1115140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1116127772Ssam	struct ieee80211_node *ni;
1117127772Ssam
1118127772Ssam	/*
1119127772Ssam	 * The destination address should be in the node table
1120127772Ssam	 * unless we are operating in station mode or this is a
1121127772Ssam	 * multicast/broadcast frame.
1122127772Ssam	 */
1123140753Ssam	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1124138568Ssam		return ieee80211_ref_node(ic->ic_bss);
1125127772Ssam
1126127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1127138568Ssam	IEEE80211_NODE_LOCK(nt);
1128138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1129138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1130138568Ssam
1131138568Ssam	if (ni == NULL) {
1132138568Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1133140497Ssam		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
1134140497Ssam			/*
1135140497Ssam			 * In adhoc mode cons up a node for the destination.
1136140497Ssam			 * Note that we need an additional reference for the
1137140497Ssam			 * caller to be consistent with _ieee80211_find_node.
1138140497Ssam			 */
1139138568Ssam			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1140140497Ssam			if (ni != NULL)
1141140497Ssam				(void) ieee80211_ref_node(ni);
1142140497Ssam		} else {
1143138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1144138568Ssam				"[%s] no node, discard frame (%s)\n",
1145138568Ssam				ether_sprintf(macaddr), __func__);
1146138568Ssam			ic->ic_stats.is_tx_nonode++;
1147127772Ssam		}
1148127772Ssam	}
1149127772Ssam	return ni;
1150127772Ssam}
1151127772Ssam
1152127772Ssam/*
1153116742Ssam * Like find but search based on the channel too.
1154116742Ssam */
1155116742Ssamstruct ieee80211_node *
1156138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1157138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1158138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan,
1159138568Ssam	const char *func, int line)
1160138568Ssam#else
1161138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1162138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan)
1163138568Ssam#endif
1164116742Ssam{
1165116742Ssam	struct ieee80211_node *ni;
1166116742Ssam	int hash;
1167116742Ssam
1168116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1169138568Ssam	IEEE80211_NODE_LOCK(nt);
1170138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1171127772Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1172127772Ssam		    ni->ni_chan == chan) {
1173138568Ssam			ieee80211_ref_node(ni);		/* mark referenced */
1174138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1175138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1176140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1177140766Ssam			    func, line,
1178138568Ssam#else
1179140766Ssam			    "%s %p<%s> refcnt %d\n", __func__,
1180138568Ssam#endif
1181140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1182140766Ssam			    ieee80211_node_refcnt(ni));
1183116742Ssam			break;
1184116742Ssam		}
1185116742Ssam	}
1186138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1187116742Ssam	return ni;
1188116742Ssam}
1189116742Ssam
1190138568Ssam/*
1191138568Ssam * Like find but search based on the ssid too.
1192138568Ssam */
1193138568Ssamstruct ieee80211_node *
1194138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1195138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1196138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1197138568Ssam	const char *func, int line)
1198138568Ssam#else
1199138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1200138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1201138568Ssam#endif
1202138568Ssam{
1203147118Ssam#define	MATCH_SSID(ni, ssid, ssidlen) \
1204147118Ssam	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1205147118Ssam	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
1206138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1207138568Ssam	struct ieee80211_node *ni;
1208138568Ssam	int hash;
1209138568Ssam
1210138568Ssam	IEEE80211_NODE_LOCK(nt);
1211147118Ssam	/*
1212147118Ssam	 * A mac address that is all zero means match only the ssid;
1213147118Ssam	 * otherwise we must match both.
1214147118Ssam	 */
1215147118Ssam	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1216147118Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1217147118Ssam			if (MATCH_SSID(ni, ssid, ssidlen))
1218147118Ssam				break;
1219147118Ssam		}
1220147118Ssam	} else {
1221147118Ssam		hash = IEEE80211_NODE_HASH(macaddr);
1222147118Ssam		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1223147118Ssam			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1224147118Ssam			    MATCH_SSID(ni, ssid, ssidlen))
1225147118Ssam				break;
1226147118Ssam		}
1227147118Ssam	}
1228147118Ssam	if (ni != NULL) {
1229147118Ssam		ieee80211_ref_node(ni);	/* mark referenced */
1230147118Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1231138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1232147118Ssam		    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1233147118Ssam		    func, line,
1234138568Ssam#else
1235147118Ssam		    "%s %p<%s> refcnt %d\n", __func__,
1236138568Ssam#endif
1237147118Ssam		     ni, ether_sprintf(ni->ni_macaddr),
1238147118Ssam		     ieee80211_node_refcnt(ni));
1239138568Ssam	}
1240138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1241138568Ssam	return ni;
1242147118Ssam#undef MATCH_SSID
1243138568Ssam}
1244138568Ssam
1245116742Ssamstatic void
1246138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1247116742Ssam{
1248138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1249138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1250119150Ssam
1251138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1252140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1253140766Ssam		ether_sprintf(ni->ni_macaddr),
1254138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1255138568Ssam
1256138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1257138568Ssam	if (nt != NULL) {
1258138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1259138568Ssam		LIST_REMOVE(ni, ni_hash);
1260138568Ssam	}
1261138568Ssam	ic->ic_node_free(ni);
1262116742Ssam}
1263116742Ssam
1264116742Ssamvoid
1265138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1266138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1267138568Ssam#else
1268138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1269138568Ssam#endif
1270116742Ssam{
1271138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1272119150Ssam
1273138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1274140454Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1275140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1276138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1277138568Ssam#endif
1278138568Ssam	if (ieee80211_node_dectestref(ni)) {
1279138568Ssam		/*
1280138568Ssam		 * Beware; if the node is marked gone then it's already
1281138568Ssam		 * been removed from the table and we cannot assume the
1282138568Ssam		 * table still exists.  Regardless, there's no need to lock
1283138568Ssam		 * the table.
1284138568Ssam		 */
1285138568Ssam		if (ni->ni_table != NULL) {
1286138568Ssam			IEEE80211_NODE_LOCK(nt);
1287138568Ssam			_ieee80211_free_node(ni);
1288138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1289138568Ssam		} else
1290138568Ssam			_ieee80211_free_node(ni);
1291116742Ssam	}
1292116742Ssam}
1293116742Ssam
1294138568Ssam/*
1295138568Ssam * Reclaim a node.  If this is the last reference count then
1296138568Ssam * do the normal free work.  Otherwise remove it from the node
1297138568Ssam * table and mark it gone by clearing the back-reference.
1298138568Ssam */
1299138568Ssamstatic void
1300138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1301116742Ssam{
1302138568Ssam
1303140766Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1304140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1305140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1306140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1307138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1308138568Ssam		/*
1309138568Ssam		 * Other references are present, just remove the
1310138568Ssam		 * node from the table so it cannot be found.  When
1311138568Ssam		 * the references are dropped storage will be
1312140753Ssam		 * reclaimed.
1313138568Ssam		 */
1314138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1315138568Ssam		LIST_REMOVE(ni, ni_hash);
1316138568Ssam		ni->ni_table = NULL;		/* clear reference */
1317138568Ssam	} else
1318138568Ssam		_ieee80211_free_node(ni);
1319138568Ssam}
1320138568Ssam
1321138568Ssamstatic void
1322138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1323138568Ssam{
1324138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1325116742Ssam	struct ieee80211_node *ni;
1326116742Ssam
1327138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1328138568Ssam		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1329138568Ssam
1330138568Ssam	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1331138568Ssam		if (ni->ni_associd != 0) {
1332138568Ssam			if (ic->ic_auth->ia_node_leave != NULL)
1333138568Ssam				ic->ic_auth->ia_node_leave(ic, ni);
1334138568Ssam			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1335138568Ssam		}
1336138568Ssam		node_reclaim(nt, ni);
1337138568Ssam	}
1338138568Ssam	ieee80211_reset_erp(ic);
1339116742Ssam}
1340116742Ssam
1341138568Ssamstatic void
1342138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt)
1343138568Ssam{
1344138568Ssam
1345138568Ssam	IEEE80211_NODE_LOCK(nt);
1346138568Ssam	ieee80211_free_allnodes_locked(nt);
1347138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1348138568Ssam}
1349138568Ssam
1350120483Ssam/*
1351138568Ssam * Timeout entries in the scan cache.
1352138568Ssam */
1353138568Ssamstatic void
1354138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1355138568Ssam{
1356138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1357138568Ssam	struct ieee80211_node *ni, *tni;
1358138568Ssam
1359138568Ssam	IEEE80211_NODE_LOCK(nt);
1360138568Ssam	ni = ic->ic_bss;
1361138568Ssam	/* XXX belongs elsewhere */
1362138568Ssam	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1363138568Ssam		m_freem(ni->ni_rxfrag[0]);
1364138568Ssam		ni->ni_rxfrag[0] = NULL;
1365138568Ssam	}
1366138568Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1367138568Ssam		if (ni->ni_inact && --ni->ni_inact == 0) {
1368138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1369138568Ssam			    "[%s] scan candidate purged from cache "
1370138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1371140766Ssam			    ieee80211_node_refcnt(ni));
1372138568Ssam			node_reclaim(nt, ni);
1373138568Ssam		}
1374138568Ssam	}
1375138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1376138568Ssam
1377138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1378138568Ssam}
1379138568Ssam
1380138568Ssam/*
1381138568Ssam * Timeout inactive stations and do related housekeeping.
1382138568Ssam * Note that we cannot hold the node lock while sending a
1383138568Ssam * frame as this would lead to a LOR.  Instead we use a
1384138568Ssam * generation number to mark nodes that we've scanned and
1385138568Ssam * drop the lock and restart a scan if we have to time out
1386138568Ssam * a node.  Since we are single-threaded by virtue of
1387120483Ssam * controlling the inactivity timer we can be sure this will
1388120483Ssam * process each node only once.
1389120483Ssam */
1390138568Ssamstatic void
1391138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt)
1392116742Ssam{
1393138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1394120483Ssam	struct ieee80211_node *ni;
1395138568Ssam	u_int gen;
1396116742Ssam
1397138568Ssam	IEEE80211_SCAN_LOCK(nt);
1398138568Ssam	gen = nt->nt_scangen++;
1399138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1400140766Ssam		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1401120483Ssamrestart:
1402138568Ssam	IEEE80211_NODE_LOCK(nt);
1403138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1404120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1405120483Ssam			continue;
1406120483Ssam		ni->ni_scangen = gen;
1407138568Ssam		/*
1408147788Ssam		 * Ignore entries for which have yet to receive an
1409147788Ssam		 * authentication frame.  These are transient and
1410147788Ssam		 * will be reclaimed when the last reference to them
1411147788Ssam		 * goes away (when frame xmits complete).
1412147788Ssam		 */
1413147788Ssam		if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1414147788Ssam			continue;
1415147788Ssam		/*
1416138568Ssam		 * Free fragment if not needed anymore
1417138568Ssam		 * (last fragment older than 1s).
1418138568Ssam		 * XXX doesn't belong here
1419138568Ssam		 */
1420138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1421138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1422138568Ssam			m_freem(ni->ni_rxfrag[0]);
1423138568Ssam			ni->ni_rxfrag[0] = NULL;
1424138568Ssam		}
1425140498Ssam		/*
1426140498Ssam		 * Special case ourself; we may be idle for extended periods
1427140498Ssam		 * of time and regardless reclaiming our state is wrong.
1428140498Ssam		 */
1429140498Ssam		if (ni == ic->ic_bss)
1430140498Ssam			continue;
1431138568Ssam		ni->ni_inact--;
1432138568Ssam		if (ni->ni_associd != 0) {
1433119150Ssam			/*
1434138568Ssam			 * Age frames on the power save queue. The
1435138568Ssam			 * aging interval is 4 times the listen
1436138568Ssam			 * interval specified by the station.  This
1437138568Ssam			 * number is factored into the age calculations
1438138568Ssam			 * when the frame is placed on the queue.  We
1439138568Ssam			 * store ages as time differences we can check
1440138568Ssam			 * and/or adjust only the head of the list.
1441138568Ssam			 */
1442138568Ssam			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1443138568Ssam				struct mbuf *m;
1444138568Ssam				int discard = 0;
1445138568Ssam
1446138568Ssam				IEEE80211_NODE_SAVEQ_LOCK(ni);
1447138568Ssam				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1448138568Ssam				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1449138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1450138568Ssam					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1451138568Ssam					m_freem(m);
1452138568Ssam					discard++;
1453138568Ssam				}
1454138568Ssam				if (m != NULL)
1455138568Ssam					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1456138568Ssam				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1457138568Ssam
1458138568Ssam				if (discard != 0) {
1459138568Ssam					IEEE80211_DPRINTF(ic,
1460138568Ssam					    IEEE80211_MSG_POWER,
1461138568Ssam					    "[%s] discard %u frames for age\n",
1462138568Ssam					    ether_sprintf(ni->ni_macaddr),
1463138568Ssam					    discard);
1464138568Ssam					IEEE80211_NODE_STAT_ADD(ni,
1465138568Ssam						ps_discard, discard);
1466138568Ssam					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1467148304Ssam						ic->ic_set_tim(ni, 0);
1468138568Ssam				}
1469138568Ssam			}
1470138568Ssam			/*
1471138568Ssam			 * Probe the station before time it out.  We
1472138568Ssam			 * send a null data frame which may not be
1473138568Ssam			 * universally supported by drivers (need it
1474138568Ssam			 * for ps-poll support so it should be...).
1475138568Ssam			 */
1476139528Ssam			if (0 < ni->ni_inact &&
1477139528Ssam			    ni->ni_inact <= ic->ic_inact_probe) {
1478138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1479138568Ssam				    "[%s] probe station due to inactivity\n",
1480138568Ssam				    ether_sprintf(ni->ni_macaddr));
1481138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1482148301Ssam				ieee80211_send_nulldata(ni);
1483138568Ssam				/* XXX stat? */
1484138568Ssam				goto restart;
1485138568Ssam			}
1486138568Ssam		}
1487138568Ssam		if (ni->ni_inact <= 0) {
1488138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1489138568Ssam			    "[%s] station timed out due to inactivity "
1490138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1491138568Ssam			    ieee80211_node_refcnt(ni));
1492138568Ssam			/*
1493138568Ssam			 * Send a deauthenticate frame and drop the station.
1494138568Ssam			 * This is somewhat complicated due to reference counts
1495138568Ssam			 * and locking.  At this point a station will typically
1496138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1497138568Ssam			 * will do a "free" of the node which will drop the
1498138568Ssam			 * reference count.  But in the meantime a reference
1499138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1500138568Ssam			 * of the node will happen either after the tx is
1501138568Ssam			 * completed or by ieee80211_node_leave.
1502120483Ssam			 *
1503138568Ssam			 * Separately we must drop the node lock before sending
1504138568Ssam			 * in case the driver takes a lock, as this will result
1505138568Ssam			 * in  LOR between the node lock and the driver lock.
1506119150Ssam			 */
1507138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1508138568Ssam			if (ni->ni_associd != 0) {
1509138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
1510138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1511138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1512138568Ssam			}
1513138568Ssam			ieee80211_node_leave(ic, ni);
1514121180Ssam			ic->ic_stats.is_node_timeout++;
1515120483Ssam			goto restart;
1516120483Ssam		}
1517116742Ssam	}
1518138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1519138568Ssam
1520138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1521138568Ssam
1522138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1523116742Ssam}
1524116742Ssam
1525116742Ssamvoid
1526138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1527116742Ssam{
1528116742Ssam	struct ieee80211_node *ni;
1529138568Ssam	u_int gen;
1530116742Ssam
1531138568Ssam	IEEE80211_SCAN_LOCK(nt);
1532138568Ssam	gen = nt->nt_scangen++;
1533138568Ssamrestart:
1534138568Ssam	IEEE80211_NODE_LOCK(nt);
1535138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1536138568Ssam		if (ni->ni_scangen != gen) {
1537138568Ssam			ni->ni_scangen = gen;
1538138568Ssam			(void) ieee80211_ref_node(ni);
1539138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1540138568Ssam			(*f)(arg, ni);
1541138568Ssam			ieee80211_free_node(ni);
1542138568Ssam			goto restart;
1543138568Ssam		}
1544138568Ssam	}
1545138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1546138568Ssam
1547138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1548116742Ssam}
1549138568Ssam
1550138568Ssamvoid
1551138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1552138568Ssam{
1553138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
1554138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1555138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
1556138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1557138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
1558138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1559138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1560138568Ssam		ni->ni_txseqs[0],
1561138568Ssam		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1562138568Ssam		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1563138568Ssam		ni->ni_rxfragstamp);
1564138568Ssam	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
1565138568Ssam		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
1566138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1567138568Ssam		ether_sprintf(ni->ni_bssid),
1568138568Ssam		ni->ni_esslen, ni->ni_essid,
1569138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1570138568Ssam	printf("\tfails %u inact %u txrate %u\n",
1571138568Ssam		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1572138568Ssam}
1573138568Ssam
1574138568Ssamvoid
1575138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
1576138568Ssam{
1577138568Ssam	ieee80211_iterate_nodes(nt,
1578138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
1579138568Ssam}
1580138568Ssam
1581138568Ssam/*
1582138568Ssam * Handle a station joining an 11g network.
1583138568Ssam */
1584138568Ssamstatic void
1585138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1586138568Ssam{
1587138568Ssam
1588138568Ssam	/*
1589138568Ssam	 * Station isn't capable of short slot time.  Bump
1590138568Ssam	 * the count of long slot time stations and disable
1591138568Ssam	 * use of short slot time.  Note that the actual switch
1592138568Ssam	 * over to long slot time use may not occur until the
1593138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1594138568Ssam	 */
1595138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1596138568Ssam		ic->ic_longslotsta++;
1597138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1598138568Ssam		    "[%s] station needs long slot time, count %d\n",
1599138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1600138568Ssam		/* XXX vap's w/ conflicting needs won't work */
1601138568Ssam		ieee80211_set_shortslottime(ic, 0);
1602138568Ssam	}
1603138568Ssam	/*
1604138568Ssam	 * If the new station is not an ERP station
1605138568Ssam	 * then bump the counter and enable protection
1606138568Ssam	 * if configured.
1607138568Ssam	 */
1608138568Ssam	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1609138568Ssam		ic->ic_nonerpsta++;
1610138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1611138568Ssam		    "[%s] station is !ERP, %d non-ERP stations associated\n",
1612138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1613138568Ssam		/*
1614138568Ssam		 * If protection is configured, enable it.
1615138568Ssam		 */
1616138568Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
1617138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1618138568Ssam			    "%s: enable use of protection\n", __func__);
1619138568Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1620138568Ssam		}
1621138568Ssam		/*
1622138568Ssam		 * If station does not support short preamble
1623138568Ssam		 * then we must enable use of Barker preamble.
1624138568Ssam		 */
1625138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1626138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1627138568Ssam			    "[%s] station needs long preamble\n",
1628138568Ssam			    ether_sprintf(ni->ni_macaddr));
1629138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1630138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1631138568Ssam		}
1632138568Ssam	} else
1633138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
1634138568Ssam}
1635138568Ssam
1636138568Ssamvoid
1637138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1638138568Ssam{
1639138568Ssam	int newassoc;
1640138568Ssam
1641138568Ssam	if (ni->ni_associd == 0) {
1642138568Ssam		u_int16_t aid;
1643138568Ssam
1644138568Ssam		/*
1645138568Ssam		 * It would be good to search the bitmap
1646138568Ssam		 * more efficiently, but this will do for now.
1647138568Ssam		 */
1648138568Ssam		for (aid = 1; aid < ic->ic_max_aid; aid++) {
1649138568Ssam			if (!IEEE80211_AID_ISSET(aid,
1650138568Ssam			    ic->ic_aid_bitmap))
1651138568Ssam				break;
1652138568Ssam		}
1653138568Ssam		if (aid >= ic->ic_max_aid) {
1654138568Ssam			IEEE80211_SEND_MGMT(ic, ni, resp,
1655138568Ssam			    IEEE80211_REASON_ASSOC_TOOMANY);
1656138568Ssam			ieee80211_node_leave(ic, ni);
1657138568Ssam			return;
1658138568Ssam		}
1659138568Ssam		ni->ni_associd = aid | 0xc000;
1660138568Ssam		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1661138568Ssam		ic->ic_sta_assoc++;
1662138568Ssam		newassoc = 1;
1663139522Ssam		if (ic->ic_curmode == IEEE80211_MODE_11G ||
1664139522Ssam		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1665138568Ssam			ieee80211_node_join_11g(ic, ni);
1666138568Ssam	} else
1667138568Ssam		newassoc = 0;
1668138568Ssam
1669138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1670139523Ssam	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
1671139523Ssam	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
1672139523Ssam	    IEEE80211_NODE_AID(ni),
1673139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1674139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1675139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1676139523Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
1677139523Ssam	);
1678138568Ssam
1679138568Ssam	/* give driver a chance to setup state like ni_txrate */
1680139524Ssam	if (ic->ic_newassoc != NULL)
1681148307Ssam		ic->ic_newassoc(ni, newassoc);
1682139528Ssam	ni->ni_inact_reload = ic->ic_inact_auth;
1683139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1684138568Ssam	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1685138568Ssam	/* tell the authenticator about new station */
1686138568Ssam	if (ic->ic_auth->ia_node_join != NULL)
1687138568Ssam		ic->ic_auth->ia_node_join(ic, ni);
1688138568Ssam	ieee80211_notify_node_join(ic, ni, newassoc);
1689138568Ssam}
1690138568Ssam
1691138568Ssam/*
1692138568Ssam * Handle a station leaving an 11g network.
1693138568Ssam */
1694138568Ssamstatic void
1695138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1696138568Ssam{
1697138568Ssam
1698139522Ssam	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
1699139522Ssam		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
1700138568Ssam		("not in 11g, curmode %x", ic->ic_curmode));
1701138568Ssam
1702138568Ssam	/*
1703138568Ssam	 * If a long slot station do the slot time bookkeeping.
1704138568Ssam	 */
1705138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1706138568Ssam		KASSERT(ic->ic_longslotsta > 0,
1707138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
1708138568Ssam		ic->ic_longslotsta--;
1709138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1710138568Ssam		    "[%s] long slot time station leaves, count now %d\n",
1711138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1712138568Ssam		if (ic->ic_longslotsta == 0) {
1713138568Ssam			/*
1714138568Ssam			 * Re-enable use of short slot time if supported
1715138568Ssam			 * and not operating in IBSS mode (per spec).
1716138568Ssam			 */
1717138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1718138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
1719138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1720138568Ssam				    "%s: re-enable use of short slot time\n",
1721138568Ssam				    __func__);
1722138568Ssam				ieee80211_set_shortslottime(ic, 1);
1723138568Ssam			}
1724138568Ssam		}
1725138568Ssam	}
1726138568Ssam	/*
1727138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
1728138568Ssam	 */
1729138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1730138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
1731138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1732138568Ssam		ic->ic_nonerpsta--;
1733138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1734138568Ssam		    "[%s] non-ERP station leaves, count now %d\n",
1735138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1736138568Ssam		if (ic->ic_nonerpsta == 0) {
1737138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1738138568Ssam				"%s: disable use of protection\n", __func__);
1739138568Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1740138568Ssam			/* XXX verify mode? */
1741138568Ssam			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1742138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1743138568Ssam				    "%s: re-enable use of short preamble\n",
1744138568Ssam				    __func__);
1745138568Ssam				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1746138568Ssam				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1747138568Ssam			}
1748138568Ssam		}
1749138568Ssam	}
1750138568Ssam}
1751138568Ssam
1752138568Ssam/*
1753138568Ssam * Handle bookkeeping for station deauthentication/disassociation
1754138568Ssam * when operating as an ap.
1755138568Ssam */
1756138568Ssamvoid
1757138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1758138568Ssam{
1759140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1760138568Ssam
1761138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1762138568Ssam	    "[%s] station with aid %d leaves\n",
1763138568Ssam	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
1764138568Ssam
1765138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1766138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS ||
1767138568Ssam		ic->ic_opmode == IEEE80211_M_AHDEMO,
1768138568Ssam		("unexpected operating mode %u", ic->ic_opmode));
1769138568Ssam	/*
1770138568Ssam	 * If node wasn't previously associated all
1771138568Ssam	 * we need to do is reclaim the reference.
1772138568Ssam	 */
1773138568Ssam	/* XXX ibss mode bypasses 11g and notification */
1774138568Ssam	if (ni->ni_associd == 0)
1775138568Ssam		goto done;
1776138568Ssam	/*
1777138568Ssam	 * Tell the authenticator the station is leaving.
1778138568Ssam	 * Note that we must do this before yanking the
1779138568Ssam	 * association id as the authenticator uses the
1780138568Ssam	 * associd to locate it's state block.
1781138568Ssam	 */
1782138568Ssam	if (ic->ic_auth->ia_node_leave != NULL)
1783138568Ssam		ic->ic_auth->ia_node_leave(ic, ni);
1784138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1785138568Ssam	ni->ni_associd = 0;
1786138568Ssam	ic->ic_sta_assoc--;
1787138568Ssam
1788139522Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G ||
1789139522Ssam	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1790138568Ssam		ieee80211_node_leave_11g(ic, ni);
1791138568Ssam	/*
1792138568Ssam	 * Cleanup station state.  In particular clear various
1793138568Ssam	 * state that might otherwise be reused if the node
1794138568Ssam	 * is reused before the reference count goes to zero
1795138568Ssam	 * (and memory is reclaimed).
1796138568Ssam	 */
1797138568Ssam	ieee80211_sta_leave(ic, ni);
1798138568Ssamdone:
1799140499Ssam	/*
1800140499Ssam	 * Remove the node from any table it's recorded in and
1801140499Ssam	 * drop the caller's reference.  Removal from the table
1802140499Ssam	 * is important to insure the node is not reprocessed
1803140499Ssam	 * for inactivity.
1804140499Ssam	 */
1805140499Ssam	if (nt != NULL) {
1806140499Ssam		IEEE80211_NODE_LOCK(nt);
1807140499Ssam		node_reclaim(nt, ni);
1808140499Ssam		IEEE80211_NODE_UNLOCK(nt);
1809140499Ssam	} else
1810140499Ssam		ieee80211_free_node(ni);
1811138568Ssam}
1812138568Ssam
1813138568Ssamu_int8_t
1814138568Ssamieee80211_getrssi(struct ieee80211com *ic)
1815138568Ssam{
1816138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
1817140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1818138568Ssam	u_int32_t rssi_samples, rssi_total;
1819138568Ssam	struct ieee80211_node *ni;
1820138568Ssam
1821138568Ssam	rssi_total = 0;
1822138568Ssam	rssi_samples = 0;
1823138568Ssam	switch (ic->ic_opmode) {
1824138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
1825138568Ssam		/* XXX locking */
1826140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1827138568Ssam			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
1828138568Ssam				rssi_samples++;
1829138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
1830138568Ssam			}
1831138568Ssam		break;
1832138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
1833138568Ssam		/* XXX locking */
1834140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1835138568Ssam			rssi_samples++;
1836138568Ssam			rssi_total += ic->ic_node_getrssi(ni);
1837138568Ssam		}
1838138568Ssam		break;
1839138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
1840138568Ssam		/* XXX locking */
1841140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1842138568Ssam			if (IEEE80211_AID(ni->ni_associd) != 0) {
1843138568Ssam				rssi_samples++;
1844138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
1845138568Ssam			}
1846138568Ssam		break;
1847138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
1848138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
1849138568Ssam	default:
1850138568Ssam		if (ic->ic_bss != NULL)
1851138568Ssam			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1852138568Ssam		rssi_samples = 1;
1853138568Ssam		break;
1854138568Ssam	}
1855138568Ssam	return rssi_total / NZ(rssi_samples);
1856138568Ssam#undef NZ
1857138568Ssam}
1858138568Ssam
1859138568Ssam/*
1860138568Ssam * Indicate whether there are frames queued for a station in power-save mode.
1861138568Ssam */
1862138568Ssamstatic void
1863148304Ssamieee80211_set_tim(struct ieee80211_node *ni, int set)
1864138568Ssam{
1865148304Ssam	struct ieee80211com *ic = ni->ni_ic;
1866138568Ssam	u_int16_t aid;
1867138568Ssam
1868138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1869138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS,
1870138568Ssam		("operating mode %u", ic->ic_opmode));
1871138568Ssam
1872138568Ssam	aid = IEEE80211_AID(ni->ni_associd);
1873138568Ssam	KASSERT(aid < ic->ic_max_aid,
1874138568Ssam		("bogus aid %u, max %u", aid, ic->ic_max_aid));
1875138568Ssam
1876138568Ssam	IEEE80211_BEACON_LOCK(ic);
1877138568Ssam	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
1878138568Ssam		if (set) {
1879138568Ssam			setbit(ic->ic_tim_bitmap, aid);
1880138568Ssam			ic->ic_ps_pending++;
1881138568Ssam		} else {
1882138568Ssam			clrbit(ic->ic_tim_bitmap, aid);
1883138568Ssam			ic->ic_ps_pending--;
1884138568Ssam		}
1885138568Ssam		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
1886138568Ssam	}
1887138568Ssam	IEEE80211_BEACON_UNLOCK(ic);
1888138568Ssam}
1889138568Ssam
1890138568Ssam/*
1891138568Ssam * Node table support.
1892138568Ssam */
1893138568Ssam
1894138568Ssamstatic void
1895138568Ssamieee80211_node_table_init(struct ieee80211com *ic,
1896138568Ssam	struct ieee80211_node_table *nt,
1897138568Ssam	const char *name, int inact,
1898138568Ssam	void (*timeout)(struct ieee80211_node_table *))
1899138568Ssam{
1900138568Ssam
1901138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1902138568Ssam		"%s %s table, inact %u\n", __func__, name, inact);
1903138568Ssam
1904138568Ssam	nt->nt_ic = ic;
1905138568Ssam	/* XXX need unit */
1906138568Ssam	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1907138568Ssam	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1908138568Ssam	TAILQ_INIT(&nt->nt_node);
1909138568Ssam	nt->nt_name = name;
1910138568Ssam	nt->nt_scangen = 1;
1911138568Ssam	nt->nt_inact_init = inact;
1912138568Ssam	nt->nt_timeout = timeout;
1913138568Ssam}
1914138568Ssam
1915138568Ssamvoid
1916138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt)
1917138568Ssam{
1918138568Ssam
1919138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1920138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1921138568Ssam
1922138568Ssam	IEEE80211_NODE_LOCK(nt);
1923138568Ssam	nt->nt_inact_timer = 0;
1924138568Ssam	ieee80211_free_allnodes_locked(nt);
1925138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1926138568Ssam}
1927138568Ssam
1928138568Ssamstatic void
1929138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1930138568Ssam{
1931138568Ssam
1932138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1933138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1934138568Ssam
1935138568Ssam	ieee80211_free_allnodes_locked(nt);
1936138568Ssam	IEEE80211_SCAN_LOCK_DESTROY(nt);
1937138568Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1938138568Ssam}
1939