ieee80211_node.c revision 140441
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 140441 2005-01-18 19:59:40Z 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
52138568Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
53138568Ssamstatic void node_cleanup(struct ieee80211_node *);
54138568Ssamstatic void node_free(struct ieee80211_node *);
55138568Ssamstatic u_int8_t node_getrssi(const struct ieee80211_node *);
56116742Ssam
57138568Ssamstatic void ieee80211_setup_node(struct ieee80211_node_table *,
58138568Ssam		struct ieee80211_node *, const u_int8_t *);
59138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
60138568Ssamstatic void ieee80211_free_allnodes(struct ieee80211_node_table *);
61120104Ssam
62138568Ssamstatic void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
63138568Ssamstatic void ieee80211_timeout_stations(struct ieee80211_node_table *);
64116742Ssam
65138568Ssamstatic void ieee80211_set_tim(struct ieee80211com *,
66138568Ssam		struct ieee80211_node *, int set);
67138568Ssam
68138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
69138568Ssam	struct ieee80211_node_table *nt, const char *name, int inact,
70138568Ssam	void (*timeout)(struct ieee80211_node_table *));
71138568Ssamstatic struct ieee80211_node_table *ieee80211_node_table_alloc(
72138568Ssam	struct ieee80211com *ic, const char *name, int inact,
73138568Ssam	void (*timeout)(struct ieee80211_node_table *));
74138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
75138568Ssam
76127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
77120481Ssam
78116742Ssamvoid
79138568Ssamieee80211_node_attach(struct ieee80211com *ic)
80116742Ssam{
81116742Ssam
82138568Ssam	ic->ic_sta = NULL;		/* defer to when we need it */
83138568Ssam	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
84138568Ssam		IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates);
85138568Ssam
86138568Ssam	ic->ic_node_alloc = node_alloc;
87138568Ssam	ic->ic_node_free = node_free;
88138568Ssam	ic->ic_node_cleanup = node_cleanup;
89138568Ssam	ic->ic_node_getrssi = node_getrssi;
90138568Ssam
91138568Ssam	/* default station inactivity timer setings */
92138568Ssam	ic->ic_inact_init = IEEE80211_INACT_INIT;
93138568Ssam	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
94138568Ssam	ic->ic_inact_run = IEEE80211_INACT_RUN;
95138568Ssam	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
96138568Ssam
97138568Ssam	/* XXX defer */
98138568Ssam	if (ic->ic_max_aid == 0)
99138568Ssam		ic->ic_max_aid = IEEE80211_AID_DEF;
100138568Ssam	else if (ic->ic_max_aid > IEEE80211_AID_MAX)
101138568Ssam		ic->ic_max_aid = IEEE80211_AID_MAX;
102138568Ssam	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
103138568Ssam		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
104138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
105138568Ssam	if (ic->ic_aid_bitmap == NULL) {
106138568Ssam		/* XXX no way to recover */
107138568Ssam		printf("%s: no memory for AID bitmap!\n", __func__);
108138568Ssam		ic->ic_max_aid = 0;
109138568Ssam	}
110138568Ssam
111138568Ssam	/* XXX defer until using hostap/ibss mode */
112138568Ssam	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
113138568Ssam	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
114138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
115138568Ssam	if (ic->ic_tim_bitmap == NULL) {
116138568Ssam		/* XXX no way to recover */
117138568Ssam		printf("%s: no memory for TIM bitmap!\n", __func__);
118138568Ssam	}
119138568Ssam	ic->ic_set_tim = ieee80211_set_tim;	/* NB: driver should override */
120118887Ssam}
121118887Ssam
122118887Ssamvoid
123138568Ssamieee80211_node_lateattach(struct ieee80211com *ic)
124118887Ssam{
125127766Ssam	struct ieee80211_node *ni;
126138568Ssam	struct ieee80211_rsnparms *rsn;
127118887Ssam
128138568Ssam	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
129127766Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
130138568Ssam	/*
131138568Ssam	 * Setup "global settings" in the bss node so that
132138568Ssam	 * each new station automatically inherits them.
133138568Ssam	 */
134138568Ssam	rsn = &ni->ni_rsn;
135138568Ssam	/* WEP, TKIP, and AES-CCM are always supported */
136138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
137138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
138138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
139138568Ssam	if (ic->ic_caps & IEEE80211_C_AES)
140138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
141138568Ssam	if (ic->ic_caps & IEEE80211_C_CKIP)
142138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
143138568Ssam	/*
144138568Ssam	 * Default unicast cipher to WEP for 802.1x use.  If
145138568Ssam	 * WPA is enabled the management code will set these
146138568Ssam	 * values to reflect.
147138568Ssam	 */
148138568Ssam	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
149138568Ssam	rsn->rsn_ucastkeylen = 104 / NBBY;
150138568Ssam	/*
151138568Ssam	 * WPA says the multicast cipher is the lowest unicast
152138568Ssam	 * cipher supported.  But we skip WEP which would
153138568Ssam	 * otherwise be used based on this criteria.
154138568Ssam	 */
155138568Ssam	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
156138568Ssam	rsn->rsn_mcastkeylen = 128 / NBBY;
157138568Ssam
158138568Ssam	/*
159138568Ssam	 * We support both WPA-PSK and 802.1x; the one used
160138568Ssam	 * is determined by the authentication mode and the
161138568Ssam	 * setting of the PSK state.
162138568Ssam	 */
163138568Ssam	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
164138568Ssam	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
165138568Ssam
166138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);		/* hold reference */
167138568Ssam	ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode);
168116742Ssam}
169116742Ssam
170116742Ssamvoid
171138568Ssamieee80211_node_detach(struct ieee80211com *ic)
172116742Ssam{
173116742Ssam
174138568Ssam	if (ic->ic_bss != NULL) {
175138568Ssam		ieee80211_free_node(ic->ic_bss);
176138568Ssam		ic->ic_bss = NULL;
177138568Ssam	}
178138568Ssam	ieee80211_node_table_cleanup(&ic->ic_scan);
179138568Ssam	if (ic->ic_sta != NULL) {
180138568Ssam		ieee80211_node_table_free(ic->ic_sta);
181138568Ssam		ic->ic_sta = NULL;
182138568Ssam	}
183138568Ssam	if (ic->ic_aid_bitmap != NULL) {
184138568Ssam		FREE(ic->ic_aid_bitmap, M_DEVBUF);
185138568Ssam		ic->ic_aid_bitmap = NULL;
186138568Ssam	}
187138568Ssam	if (ic->ic_tim_bitmap != NULL) {
188138568Ssam		FREE(ic->ic_tim_bitmap, M_DEVBUF);
189138568Ssam		ic->ic_tim_bitmap = NULL;
190138568Ssam	}
191116742Ssam}
192116742Ssam
193138568Ssam/*
194138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
195138568Ssam */
196138568Ssam
197138568Ssamvoid
198138568Ssamieee80211_node_authorize(struct ieee80211com *ic, struct ieee80211_node *ni)
199138568Ssam{
200138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
201139528Ssam	ni->ni_inact_reload = ic->ic_inact_run;
202138568Ssam}
203138568Ssam
204138568Ssamvoid
205138568Ssamieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni)
206138568Ssam{
207138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
208138568Ssam}
209138568Ssam
210116742Ssam/*
211138568Ssam * Set/change the channel.  The rate set is also updated as
212138568Ssam * to insure a consistent view by drivers.
213138568Ssam */
214138568Ssamstatic __inline void
215138568Ssamieee80211_set_chan(struct ieee80211com *ic,
216138568Ssam	struct ieee80211_node *ni, struct ieee80211_channel *chan)
217138568Ssam{
218138568Ssam	ni->ni_chan = chan;
219138568Ssam	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
220138568Ssam}
221138568Ssam
222138568Ssam/*
223116742Ssam * AP scanning support.
224116742Ssam */
225116742Ssam
226138568Ssam#ifdef IEEE80211_DEBUG
227138568Ssamstatic void
228138568Ssamdump_chanlist(const u_char chans[])
229138568Ssam{
230138568Ssam	const char *sep;
231138568Ssam	int i;
232138568Ssam
233138568Ssam	sep = " ";
234138568Ssam	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
235138568Ssam		if (isset(chans, i)) {
236138568Ssam			printf("%s%u", sep, i);
237138568Ssam			sep = ", ";
238138568Ssam		}
239138568Ssam}
240138568Ssam#endif /* IEEE80211_DEBUG */
241138568Ssam
242116742Ssam/*
243138568Ssam * Initialize the channel set to scan based on the
244116742Ssam * of available channels and the current PHY mode.
245116742Ssam */
246117811Ssamstatic void
247138568Ssamieee80211_reset_scan(struct ieee80211com *ic)
248116742Ssam{
249116742Ssam
250138568Ssam	/* XXX ic_des_chan should be handled with ic_chan_active */
251138568Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
252138568Ssam		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
253138568Ssam		setbit(ic->ic_chan_scan,
254138568Ssam			ieee80211_chan2ieee(ic, ic->ic_des_chan));
255138568Ssam	} else
256138568Ssam		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
257138568Ssam			sizeof(ic->ic_chan_active));
258117811Ssam	/* NB: hack, setup so next_scan starts with the first channel */
259117811Ssam	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
260138568Ssam		ieee80211_set_chan(ic, ic->ic_bss,
261138568Ssam			&ic->ic_channels[IEEE80211_CHAN_MAX]);
262138568Ssam#ifdef IEEE80211_DEBUG
263138568Ssam	if (ieee80211_msg_scan(ic)) {
264138568Ssam		printf("%s: scan set:", __func__);
265138568Ssam		dump_chanlist(ic->ic_chan_scan);
266138568Ssam		printf(" start chan %u\n",
267138568Ssam			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
268138568Ssam	}
269138568Ssam#endif /* IEEE80211_DEBUG */
270116742Ssam}
271116742Ssam
272116742Ssam/*
273116742Ssam * Begin an active scan.
274116742Ssam */
275116742Ssamvoid
276138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset)
277116742Ssam{
278116742Ssam
279138568Ssam	ic->ic_scan.nt_scangen++;
280117811Ssam	/*
281117811Ssam	 * In all but hostap mode scanning starts off in
282117811Ssam	 * an active mode before switching to passive.
283117811Ssam	 */
284121180Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
285117811Ssam		ic->ic_flags |= IEEE80211_F_ASCAN;
286121180Ssam		ic->ic_stats.is_scan_active++;
287121180Ssam	} else
288121180Ssam		ic->ic_stats.is_scan_passive++;
289139520Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
290139520Ssam		"begin %s scan in %s mode, scangen %u\n",
291138568Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
292139520Ssam		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
293116742Ssam	/*
294138568Ssam	 * Clear scan state and flush any previously seen AP's.
295116742Ssam	 */
296138568Ssam	ieee80211_reset_scan(ic);
297138568Ssam	if (reset)
298138568Ssam		ieee80211_free_allnodes(&ic->ic_scan);
299116742Ssam
300138568Ssam	ic->ic_flags |= IEEE80211_F_SCAN;
301138568Ssam
302117811Ssam	/* Scan the next channel. */
303138568Ssam	ieee80211_next_scan(ic);
304116742Ssam}
305116742Ssam
306116742Ssam/*
307116742Ssam * Switch to the next channel marked for scanning.
308116742Ssam */
309138568Ssamint
310138568Ssamieee80211_next_scan(struct ieee80211com *ic)
311116742Ssam{
312116742Ssam	struct ieee80211_channel *chan;
313116742Ssam
314138568Ssam	/*
315138568Ssam	 * Insure any previous mgt frame timeouts don't fire.
316138568Ssam	 * This assumes the driver does the right thing in
317138568Ssam	 * flushing anything queued in the driver and below.
318138568Ssam	 */
319138568Ssam	ic->ic_mgt_timer = 0;
320138568Ssam
321116742Ssam	chan = ic->ic_bss->ni_chan;
322138568Ssam	do {
323116742Ssam		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
324116742Ssam			chan = &ic->ic_channels[0];
325116742Ssam		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
326138568Ssam			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
327138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
328138568Ssam			    "%s: chan %d->%d\n", __func__,
329138568Ssam			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
330138568Ssam			    ieee80211_chan2ieee(ic, chan));
331138568Ssam			ieee80211_set_chan(ic, ic->ic_bss, chan);
332138568Ssam#ifdef notyet
333138568Ssam			/* XXX driver state change */
334116742Ssam			/*
335138568Ssam			 * Scan next channel. If doing an active scan
336138568Ssam			 * and the channel is not marked passive-only
337138568Ssam			 * then send a probe request.  Otherwise just
338138568Ssam			 * listen for beacons on the channel.
339116742Ssam			 */
340138568Ssam			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
341138568Ssam			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
342138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
343138568Ssam				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
344138568Ssam			}
345138568Ssam#else
346138568Ssam			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
347138568Ssam#endif
348138568Ssam			return 1;
349116742Ssam		}
350138568Ssam	} while (chan != ic->ic_bss->ni_chan);
351138568Ssam	ieee80211_end_scan(ic);
352138568Ssam	return 0;
353116742Ssam}
354116742Ssam
355116742Ssamvoid
356116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
357116742Ssam{
358116742Ssam	struct ieee80211_node *ni;
359116742Ssam
360138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
361138568Ssam		"%s: creating ibss\n", __func__);
362138568Ssam
363138568Ssam	/*
364138568Ssam	 * Create the station/neighbor table.  Note that for adhoc
365138568Ssam	 * mode we make the initial inactivity timer longer since
366138568Ssam	 * we create nodes only through discovery and they typically
367138568Ssam	 * are long-lived associations.
368138568Ssam	 */
369138568Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
370138568Ssam		ic->ic_sta = ieee80211_node_table_alloc(ic,
371138568Ssam					"station", ic->ic_inact_init,
372138568Ssam					ieee80211_timeout_stations);
373138568Ssam	else
374138568Ssam		ic->ic_sta = ieee80211_node_table_alloc(ic,
375138568Ssam					"neighbor", ic->ic_inact_run,
376138568Ssam					ieee80211_timeout_stations);
377138568Ssam	if (ic->ic_sta == NULL) {
378138568Ssam		/*
379138568Ssam		 * Should remain in SCAN state and retry.
380138568Ssam		 */
381138568Ssam		/* XXX stat+msg */
382138568Ssam		return;
383138568Ssam	}
384138568Ssam
385116742Ssam	ni = ic->ic_bss;
386116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
387116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
388116742Ssam	ni->ni_esslen = ic->ic_des_esslen;
389116742Ssam	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
390116742Ssam	ni->ni_rssi = 0;
391116742Ssam	ni->ni_rstamp = 0;
392138568Ssam	ni->ni_tstamp.tsf = 0;
393116742Ssam	ni->ni_intval = ic->ic_lintval;
394138568Ssam	ni->ni_capinfo = 0;
395138568Ssam	ni->ni_erp = 0;
396138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
397116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
398116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
399116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
400116742Ssam		ni->ni_fhindex = 1;
401116742Ssam	}
402138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
403138568Ssam		ic->ic_flags |= IEEE80211_F_SIBSS;
404138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
405138568Ssam		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	/*
427138568Ssam	 * Set the erp state (mostly the slot time) to deal with
428138568Ssam	 * the auto-select case; this should be redundant if the
429138568Ssam	 * mode is locked.
430138568Ssam	 */
431138568Ssam	ieee80211_reset_erp(ic);
432138568Ssam	ieee80211_wme_initparams(ic);
433138568Ssam
434117811Ssam	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
435116742Ssam}
436116742Ssam
437138568Ssamvoid
438138568Ssamieee80211_reset_bss(struct ieee80211com *ic)
439138568Ssam{
440138568Ssam	struct ieee80211_node *ni, *obss;
441138568Ssam
442138568Ssam	ieee80211_node_table_reset(&ic->ic_scan);
443138568Ssam	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
444138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
445138568Ssam	obss = ic->ic_bss;
446138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);
447138568Ssam	if (obss != NULL)
448138568Ssam		ieee80211_free_node(obss);
449138568Ssam	if (ic->ic_sta != NULL) {
450138568Ssam		ieee80211_node_table_free(ic->ic_sta);
451138568Ssam		ic->ic_sta = NULL;
452138568Ssam	}
453138568Ssam}
454138568Ssam
455127767Ssamstatic int
456138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
457127767Ssam{
458127767Ssam        u_int8_t rate;
459127767Ssam        int fail;
460127767Ssam
461127767Ssam	fail = 0;
462127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
463127767Ssam		fail |= 0x01;
464127767Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
465127767Ssam	    ni->ni_chan != ic->ic_des_chan)
466127767Ssam		fail |= 0x01;
467127767Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
468127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
469127767Ssam			fail |= 0x02;
470127767Ssam	} else {
471127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
472127767Ssam			fail |= 0x02;
473127767Ssam	}
474138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
475127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
476127767Ssam			fail |= 0x04;
477127767Ssam	} else {
478127767Ssam		/* XXX does this mean privacy is supported or required? */
479127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
480127767Ssam			fail |= 0x04;
481127767Ssam	}
482140440Ssam	rate = ieee80211_fix_rate(ic, ni,
483140440Ssam			IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
484127767Ssam	if (rate & IEEE80211_RATE_BASIC)
485127767Ssam		fail |= 0x08;
486127767Ssam	if (ic->ic_des_esslen != 0 &&
487127767Ssam	    (ni->ni_esslen != ic->ic_des_esslen ||
488127767Ssam	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
489127767Ssam		fail |= 0x10;
490127767Ssam	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
491127767Ssam	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
492127767Ssam		fail |= 0x20;
493127767Ssam#ifdef IEEE80211_DEBUG
494138568Ssam	if (ieee80211_msg_scan(ic)) {
495127767Ssam		printf(" %c %s", fail ? '-' : '+',
496127767Ssam		    ether_sprintf(ni->ni_macaddr));
497127767Ssam		printf(" %s%c", ether_sprintf(ni->ni_bssid),
498127767Ssam		    fail & 0x20 ? '!' : ' ');
499127767Ssam		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
500127767Ssam			fail & 0x01 ? '!' : ' ');
501127767Ssam		printf(" %+4d", ni->ni_rssi);
502127767Ssam		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
503127767Ssam		    fail & 0x08 ? '!' : ' ');
504127767Ssam		printf(" %4s%c",
505127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
506127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
507127767Ssam		    "????",
508127767Ssam		    fail & 0x02 ? '!' : ' ');
509127767Ssam		printf(" %3s%c ",
510127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
511127767Ssam		    "wep" : "no",
512127767Ssam		    fail & 0x04 ? '!' : ' ');
513127767Ssam		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
514127767Ssam		printf("%s\n", fail & 0x10 ? "!" : "");
515127767Ssam	}
516127767Ssam#endif
517127767Ssam	return fail;
518127767Ssam}
519127767Ssam
520138568Ssamstatic __inline u_int8_t
521138568Ssammaxrate(const struct ieee80211_node *ni)
522138568Ssam{
523138568Ssam	const struct ieee80211_rateset *rs = &ni->ni_rates;
524138568Ssam	/* NB: assumes rate set is sorted (happens on frame receive) */
525138568Ssam	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
526138568Ssam}
527138568Ssam
528116742Ssam/*
529138568Ssam * Compare the capabilities of two nodes and decide which is
530138568Ssam * more desirable (return >0 if a is considered better).  Note
531138568Ssam * that we assume compatibility/usability has already been checked
532138568Ssam * so we don't need to (e.g. validate whether privacy is supported).
533138568Ssam * Used to select the best scan candidate for association in a BSS.
534138568Ssam */
535138568Ssamstatic int
536138568Ssamieee80211_node_compare(struct ieee80211com *ic,
537138568Ssam		       const struct ieee80211_node *a,
538138568Ssam		       const struct ieee80211_node *b)
539138568Ssam{
540138568Ssam	u_int8_t maxa, maxb;
541138568Ssam	u_int8_t rssia, rssib;
542138568Ssam
543138568Ssam	/* privacy support preferred */
544138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
545138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
546138568Ssam		return 1;
547138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
548138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
549138568Ssam		return -1;
550138568Ssam
551138568Ssam	rssia = ic->ic_node_getrssi(a);
552138568Ssam	rssib = ic->ic_node_getrssi(b);
553139543Ssam	if (abs(rssib - rssia) < 5) {
554139543Ssam		/* best/max rate preferred if signal level close enough XXX */
555139543Ssam		maxa = maxrate(a);
556139543Ssam		maxb = maxrate(b);
557139543Ssam		if (maxa != maxb)
558139543Ssam			return maxa - maxb;
559139543Ssam		/* XXX use freq for channel preference */
560139543Ssam		/* for now just prefer 5Ghz band to all other bands */
561139543Ssam		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
562139543Ssam		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
563139543Ssam			return 1;
564139543Ssam		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
565139543Ssam		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
566139543Ssam			return -1;
567139543Ssam	}
568138568Ssam	/* all things being equal, use signal level */
569138568Ssam	return rssia - rssib;
570138568Ssam}
571138568Ssam
572138568Ssam/*
573140441Ssam * Mark an ongoing scan stopped.
574140441Ssam */
575140441Ssamvoid
576140441Ssamieee80211_cancel_scan(struct ieee80211com *ic)
577140441Ssam{
578140441Ssam
579140441Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
580140441Ssam		__func__,
581140441Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
582140441Ssam
583140441Ssam	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
584140441Ssam}
585140441Ssam
586140441Ssam/*
587116742Ssam * Complete a scan of potential channels.
588116742Ssam */
589116742Ssamvoid
590138568Ssamieee80211_end_scan(struct ieee80211com *ic)
591116742Ssam{
592116742Ssam	struct ieee80211_node *ni, *nextbs, *selbs;
593138568Ssam	struct ieee80211_node_table *nt;
594116742Ssam
595140441Ssam	ieee80211_cancel_scan(ic);
596140441Ssam	ieee80211_notify_scan_done(ic);
597116742Ssam
598138568Ssam	nt = &ic->ic_scan;
599138568Ssam	ni = TAILQ_FIRST(&nt->nt_node);
600138568Ssam
601116742Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
602138568Ssam		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
603138568Ssam		int i, bestchan;
604138568Ssam		u_int8_t rssi;
605138568Ssam
606116742Ssam		/*
607116742Ssam		 * The passive scan to look for existing AP's completed,
608116742Ssam		 * select a channel to camp on.  Identify the channels
609116742Ssam		 * that already have one or more AP's and try to locate
610138568Ssam		 * an unnoccupied one.  If that fails, pick a channel that
611138568Ssam		 * looks to be quietest.
612116742Ssam		 */
613138568Ssam		memset(maxrssi, 0, sizeof(maxrssi));
614116742Ssam		for (; ni != NULL; ni = nextbs) {
615116742Ssam			ieee80211_ref_node(ni);
616116742Ssam			nextbs = TAILQ_NEXT(ni, ni_list);
617138568Ssam			rssi = ic->ic_node_getrssi(ni);
618138568Ssam			i = ieee80211_chan2ieee(ic, ni->ni_chan);
619138568Ssam			if (rssi > maxrssi[i])
620138568Ssam				maxrssi[i] = rssi;
621138568Ssam			ieee80211_unref_node(&ni);
622116742Ssam		}
623138568Ssam		/* XXX select channel more intelligently */
624138568Ssam		bestchan = -1;
625116742Ssam		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
626138568Ssam			if (isset(ic->ic_chan_active, i)) {
627138568Ssam				/*
628138568Ssam				 * If the channel is unoccupied the max rssi
629138568Ssam				 * should be zero; just take it.  Otherwise
630138568Ssam				 * track the channel with the lowest rssi and
631138568Ssam				 * use that when all channels appear occupied.
632138568Ssam				 */
633138568Ssam				if (maxrssi[i] == 0) {
634138568Ssam					bestchan = i;
635116742Ssam					break;
636138568Ssam				}
637138568Ssam				if (maxrssi[i] < maxrssi[bestchan])
638138568Ssam					bestchan = i;
639138568Ssam			}
640138568Ssam		if (bestchan != -1) {
641138568Ssam			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
642138568Ssam			return;
643116742Ssam		}
644138568Ssam		/* no suitable channel, should not happen */
645138568Ssam	}
646138568Ssam
647138568Ssam	/*
648138568Ssam	 * When manually sequencing the state machine; scan just once
649138568Ssam	 * regardless of whether we have a candidate or not.  The
650138568Ssam	 * controlling application is expected to setup state and
651138568Ssam	 * initiate an association.
652138568Ssam	 */
653138568Ssam	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
654116742Ssam		return;
655138568Ssam	/*
656138568Ssam	 * Automatic sequencing; look for a candidate and
657138568Ssam	 * if found join the network.
658138568Ssam	 */
659116742Ssam	if (ni == NULL) {
660138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
661138568Ssam			"%s: no scan candidate\n", __func__);
662116742Ssam  notfound:
663116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS &&
664116742Ssam		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
665116742Ssam		    ic->ic_des_esslen != 0) {
666116742Ssam			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
667116742Ssam			return;
668116742Ssam		}
669116742Ssam		/*
670116742Ssam		 * Reset the list of channels to scan and start again.
671116742Ssam		 */
672138568Ssam		ieee80211_reset_scan(ic);
673138568Ssam		ic->ic_flags |= IEEE80211_F_SCAN;
674138568Ssam		ieee80211_next_scan(ic);
675116742Ssam		return;
676116742Ssam	}
677116742Ssam	selbs = NULL;
678138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
679138568Ssam	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
680116742Ssam	for (; ni != NULL; ni = nextbs) {
681116742Ssam		ieee80211_ref_node(ni);
682116742Ssam		nextbs = TAILQ_NEXT(ni, ni_list);
683116742Ssam		if (ni->ni_fails) {
684116742Ssam			/*
685116742Ssam			 * The configuration of the access points may change
686116742Ssam			 * during my scan.  So delete the entry for the AP
687116742Ssam			 * and retry to associate if there is another beacon.
688116742Ssam			 */
689138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
690138568Ssam				"%s: skip scan candidate %s, fails %u\n",
691138568Ssam				__func__, ether_sprintf(ni->ni_macaddr),
692138568Ssam				ni->ni_fails);
693116742Ssam			if (ni->ni_fails++ > 2)
694138568Ssam				ieee80211_free_node(ni);
695116742Ssam			continue;
696116742Ssam		}
697138568Ssam		if (ieee80211_match_bss(ic, ni) == 0) {
698116742Ssam			if (selbs == NULL)
699116742Ssam				selbs = ni;
700138568Ssam			else if (ieee80211_node_compare(ic, ni, selbs) > 0) {
701116742Ssam				ieee80211_unref_node(&selbs);
702116742Ssam				selbs = ni;
703116742Ssam			} else
704116742Ssam				ieee80211_unref_node(&ni);
705116742Ssam		} else {
706116742Ssam			ieee80211_unref_node(&ni);
707116742Ssam		}
708116742Ssam	}
709116742Ssam	if (selbs == NULL)
710116742Ssam		goto notfound;
711138568Ssam	if (!ieee80211_sta_join(ic, selbs)) {
712138568Ssam		ieee80211_unref_node(&selbs);
713138568Ssam		goto notfound;
714138568Ssam	}
715138568Ssam}
716138568Ssam
717138568Ssam/*
718138568Ssam * Handle 802.11 ad hoc network merge.  The
719138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
720138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
721138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
722138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
723138568Ssam * sends beacons with the greatest TSF timestamp.
724138568Ssam *
725138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
726138568Ssam *
727138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
728138568Ssam */
729138568Ssamint
730138568Ssamieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni)
731138568Ssam{
732138568Ssam
733138568Ssam	if (IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
734138568Ssam		/* unchanged, nothing to do */
735138568Ssam		return 0;
736138568Ssam	}
737138568Ssam	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
738138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
739138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
740138568Ssam		ic->ic_stats.is_ibss_capmismatch++;
741138568Ssam		return 0;
742138568Ssam	}
743138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
744138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
745138568Ssam		ether_sprintf(ni->ni_bssid),
746138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
747138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
748138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
749138568Ssam	);
750138568Ssam	return ieee80211_sta_join(ic, ni);
751138568Ssam}
752138568Ssam
753138568Ssam/*
754138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
755138568Ssam * be passed in with a held reference.
756138568Ssam */
757138568Ssamint
758138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
759138568Ssam{
760138568Ssam	struct ieee80211_node *obss;
761138568Ssam
762116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
763138568Ssam		/*
764140440Ssam		 * Delete unusable rates; we've already checked
765140440Ssam		 * that the negotiated rate set is acceptable.
766138568Ssam		 */
767140440Ssam		ieee80211_fix_rate(ic, selbs, IEEE80211_F_DODEL);
768127772Ssam		/*
769139521Ssam		 * Create the neighbor table; it will already
770139521Ssam		 * exist if we are simply switching mastership.
771127772Ssam		 */
772139521Ssam		if (ic->ic_sta == NULL) {
773139521Ssam			ic->ic_sta = ieee80211_node_table_alloc(ic,
774138568Ssam					"neighbor", ic->ic_inact_run,
775138568Ssam					ieee80211_timeout_stations);
776139521Ssam			if (ic->ic_sta == NULL) {
777139521Ssam				/*
778139521Ssam				 * Should remain in SCAN state and retry.
779139521Ssam				 */
780139521Ssam				/* XXX stat+msg */
781139521Ssam				return 0;
782139521Ssam			}
783138568Ssam		}
784138568Ssam	}
785138568Ssam
786138568Ssam	/*
787138568Ssam	 * Committed to selbs, setup state.
788138568Ssam	 */
789138568Ssam	obss = ic->ic_bss;
790138568Ssam	ic->ic_bss = selbs;
791138568Ssam	if (obss != NULL)
792138568Ssam		ieee80211_free_node(obss);
793138568Ssam	/*
794138568Ssam	 * Set the erp state (mostly the slot time) to deal with
795138568Ssam	 * the auto-select case; this should be redundant if the
796138568Ssam	 * mode is locked.
797138568Ssam	 */
798138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
799138568Ssam	ieee80211_reset_erp(ic);
800138568Ssam	ieee80211_wme_initparams(ic);
801138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
802117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
803138568Ssam	else
804117811Ssam		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
805138568Ssam	return 1;
806116742Ssam}
807116742Ssam
808138568Ssam/*
809138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
810138568Ssam * be passed in with a held reference.
811138568Ssam */
812138568Ssamvoid
813138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
814138568Ssam{
815138568Ssam	ic->ic_node_cleanup(ni);
816138568Ssam	ieee80211_notify_node_leave(ic, ni);
817138568Ssam}
818138568Ssam
819116742Ssamstatic struct ieee80211_node *
820138568Ssamnode_alloc(struct ieee80211_node_table *nt)
821116742Ssam{
822127768Ssam	struct ieee80211_node *ni;
823138568Ssam
824127768Ssam	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
825127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
826127768Ssam	return ni;
827116742Ssam}
828116742Ssam
829138568Ssam/*
830138568Ssam * Reclaim any resources in a node and reset any critical
831138568Ssam * state.  Typically nodes are free'd immediately after,
832138568Ssam * but in some cases the storage may be reused so we need
833138568Ssam * to insure consistent state (should probably fix that).
834138568Ssam */
835116742Ssamstatic void
836138568Ssamnode_cleanup(struct ieee80211_node *ni)
837116742Ssam{
838138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
839138568Ssam	struct ieee80211com *ic = ni->ni_ic;
840138568Ssam	int i, qlen;
841138568Ssam
842138568Ssam	/* NB: preserve ni_table */
843138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
844138568Ssam		ic->ic_ps_sta--;
845138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
846138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
847138568Ssam		    "[%s] power save mode off, %u sta's in ps mode\n",
848138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
849138568Ssam	}
850138568Ssam
851138568Ssam	/*
852138568Ssam	 * Drain power save queue and, if needed, clear TIM.
853138568Ssam	 */
854138568Ssam	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
855138568Ssam	if (qlen != 0 && ic->ic_set_tim != NULL)
856138568Ssam		ic->ic_set_tim(ic, ni, 0);
857138568Ssam
858138568Ssam	ni->ni_associd = 0;
859138568Ssam	if (ni->ni_challenge != NULL) {
860138568Ssam		FREE(ni->ni_challenge, M_DEVBUF);
861138568Ssam		ni->ni_challenge = NULL;
862138568Ssam	}
863138568Ssam	/*
864138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
865138568Ssam	 * reusable during a re-auth/re-assoc state transition.
866138568Ssam	 * If we remove these data they will not be recreated
867138568Ssam	 * because they come from a probe-response or beacon frame
868138568Ssam	 * which cannot be expected prior to the association-response.
869138568Ssam	 * This should not be an issue when operating in other modes
870138568Ssam	 * as stations leaving always go through a full state transition
871138568Ssam	 * which will rebuild this state.
872138568Ssam	 *
873138568Ssam	 * XXX does this leave us open to inheriting old state?
874138568Ssam	 */
875138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
876138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
877138568Ssam			m_freem(ni->ni_rxfrag[i]);
878138568Ssam			ni->ni_rxfrag[i] = NULL;
879138568Ssam		}
880138568Ssam	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
881138568Ssam#undef N
882116742Ssam}
883116742Ssam
884116742Ssamstatic void
885138568Ssamnode_free(struct ieee80211_node *ni)
886116742Ssam{
887138568Ssam	struct ieee80211com *ic = ni->ni_ic;
888138568Ssam
889138568Ssam	ic->ic_node_cleanup(ni);
890138568Ssam	if (ni->ni_wpa_ie != NULL)
891138568Ssam		FREE(ni->ni_wpa_ie, M_DEVBUF);
892138568Ssam	if (ni->ni_wme_ie != NULL)
893138568Ssam		FREE(ni->ni_wme_ie, M_DEVBUF);
894138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
895138568Ssam	FREE(ni, M_80211_NODE);
896116742Ssam}
897116742Ssam
898120104Ssamstatic u_int8_t
899138568Ssamnode_getrssi(const struct ieee80211_node *ni)
900120104Ssam{
901120104Ssam	return ni->ni_rssi;
902120104Ssam}
903120104Ssam
904116742Ssamstatic void
905138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt,
906138568Ssam	struct ieee80211_node *ni, const u_int8_t *macaddr)
907116742Ssam{
908138568Ssam	struct ieee80211com *ic = nt->nt_ic;
909116742Ssam	int hash;
910116742Ssam
911138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
912138568Ssam		"%s %s in %s table\n", __func__,
913138568Ssam		ether_sprintf(macaddr), nt->nt_name);
914138568Ssam
915116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
916116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
917138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
918138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
919138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
920138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
921138568Ssam	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
922139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
923139528Ssam	ni->ni_inact = ni->ni_inact_reload;
924138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
925138568Ssam
926138568Ssam	IEEE80211_NODE_LOCK(nt);
927138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
928138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
929138568Ssam	ni->ni_table = nt;
930138568Ssam	ni->ni_ic = ic;
931138568Ssam	IEEE80211_NODE_UNLOCK(nt);
932116742Ssam}
933116742Ssam
934116742Ssamstruct ieee80211_node *
935138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
936116742Ssam{
937138568Ssam	struct ieee80211com *ic = nt->nt_ic;
938138568Ssam	struct ieee80211_node *ni;
939138568Ssam
940138568Ssam	ni = ic->ic_node_alloc(nt);
941116742Ssam	if (ni != NULL)
942138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
943127769Ssam	else
944127769Ssam		ic->ic_stats.is_rx_nodealloc++;
945116742Ssam	return ni;
946116742Ssam}
947116742Ssam
948116742Ssamstruct ieee80211_node *
949138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
950116742Ssam{
951138568Ssam	struct ieee80211com *ic = nt->nt_ic;
952138568Ssam	struct ieee80211_node *ni;
953138568Ssam
954138568Ssam	ni = ic->ic_node_alloc(nt);
955116742Ssam	if (ni != NULL) {
956138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
957127770Ssam		/*
958127770Ssam		 * Inherit from ic_bss.
959127770Ssam		 */
960138568Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
961138568Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
962138568Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
963127770Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
964138568Ssam		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
965138568Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
966127770Ssam	} else
967127770Ssam		ic->ic_stats.is_rx_nodealloc++;
968116742Ssam	return ni;
969116742Ssam}
970116742Ssam
971127772Ssamstatic struct ieee80211_node *
972138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
973138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
974138568Ssam	const u_int8_t *macaddr, const char *func, int line)
975138568Ssam#else
976138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt,
977138568Ssam	const u_int8_t *macaddr)
978138568Ssam#endif
979116742Ssam{
980116742Ssam	struct ieee80211_node *ni;
981116742Ssam	int hash;
982116742Ssam
983138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
984127772Ssam
985116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
986138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
987116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
988138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
989138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
990138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
991138568Ssam			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
992138568Ssam			     ether_sprintf(ni->ni_macaddr),
993138568Ssam			     ieee80211_node_refcnt(ni));
994138568Ssam#endif
995127772Ssam			return ni;
996116742Ssam		}
997116742Ssam	}
998127772Ssam	return NULL;
999127772Ssam}
1000138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1001138568Ssam#define	_ieee80211_find_node(nt, mac) \
1002138568Ssam	_ieee80211_find_node_debug(nt, mac, func, line)
1003138568Ssam#endif
1004127772Ssam
1005127772Ssamstruct ieee80211_node *
1006138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1007138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1008138568Ssam	const u_int8_t *macaddr, const char *func, int line)
1009138568Ssam#else
1010138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1011138568Ssam#endif
1012127772Ssam{
1013127772Ssam	struct ieee80211_node *ni;
1014127772Ssam
1015138568Ssam	IEEE80211_NODE_LOCK(nt);
1016138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1017138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1018116742Ssam	return ni;
1019116742Ssam}
1020116742Ssam
1021116742Ssam/*
1022138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1023138568Ssam * Note that for the driver's benefit we we treat this like
1024138568Ssam * an association so the driver has an opportunity to setup
1025138568Ssam * it's private state.
1026138568Ssam */
1027138568Ssamstruct ieee80211_node *
1028138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1029138568Ssam	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1030138568Ssam{
1031138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1032138568Ssam	struct ieee80211_node *ni;
1033138568Ssam
1034138568Ssam	ni = ieee80211_dup_bss(nt, macaddr);
1035138568Ssam	if (ni != NULL) {
1036138568Ssam		/* XXX no rate negotiation; just dup */
1037138568Ssam		ni->ni_rates = ic->ic_bss->ni_rates;
1038139524Ssam		if (ic->ic_newassoc != NULL)
1039138568Ssam			ic->ic_newassoc(ic, ni, 1);
1040138568Ssam		/* XXX not right for 802.1x/WPA */
1041138568Ssam		ieee80211_node_authorize(ic, ni);
1042138568Ssam		ieee80211_ref_node(ni);		/* hold reference */
1043138568Ssam	}
1044138568Ssam	return ni;
1045138568Ssam}
1046138568Ssam
1047138568Ssam/*
1048138568Ssam * Locate the node for sender, track state, and then pass the
1049138568Ssam * (referenced) node up to the 802.11 layer for its use.  We
1050138568Ssam * are required to pass some node so we fall back to ic_bss
1051138568Ssam * when this frame is from an unknown sender.  The 802.11 layer
1052138568Ssam * knows this means the sender wasn't in the node table and
1053138568Ssam * acts accordingly.
1054138568Ssam */
1055138568Ssamstruct ieee80211_node *
1056138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1057138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1058138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1059138568Ssam#else
1060138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1061138568Ssam	const struct ieee80211_frame_min *wh)
1062138568Ssam#endif
1063138568Ssam{
1064138568Ssam#define	IS_CTL(wh) \
1065138568Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1066138568Ssam#define	IS_PSPOLL(wh) \
1067138568Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1068138568Ssam	struct ieee80211_node_table *nt;
1069138568Ssam	struct ieee80211_node *ni;
1070138568Ssam
1071138568Ssam	/* XXX may want scanned nodes in the neighbor table for adhoc */
1072138568Ssam	if (ic->ic_opmode == IEEE80211_M_STA ||
1073138568Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1074138568Ssam	    (ic->ic_flags & IEEE80211_F_SCAN))
1075138568Ssam		nt = &ic->ic_scan;
1076138568Ssam	else
1077138568Ssam		nt = ic->ic_sta;
1078138568Ssam	/* XXX check ic_bss first in station mode */
1079138568Ssam	/* XXX 4-address frames? */
1080138568Ssam	IEEE80211_NODE_LOCK(nt);
1081138568Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1082138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr1);
1083138568Ssam	else
1084138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr2);
1085138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1086138568Ssam
1087138568Ssam	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
1088138568Ssam#undef IS_PSPOLL
1089138568Ssam#undef IS_CTL
1090138568Ssam}
1091138568Ssam
1092138568Ssam/*
1093127772Ssam * Return a reference to the appropriate node for sending
1094127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1095127772Ssam */
1096127772Ssamstruct ieee80211_node *
1097138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1098138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1099138568Ssam	const char *func, int line)
1100138568Ssam#else
1101138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1102138568Ssam#endif
1103127772Ssam{
1104138568Ssam	struct ieee80211_node_table *nt = ic->ic_sta;
1105127772Ssam	struct ieee80211_node *ni;
1106127772Ssam
1107127772Ssam	/*
1108127772Ssam	 * The destination address should be in the node table
1109127772Ssam	 * unless we are operating in station mode or this is a
1110127772Ssam	 * multicast/broadcast frame.
1111127772Ssam	 */
1112138568Ssam	if (nt == NULL || IEEE80211_IS_MULTICAST(macaddr))
1113138568Ssam		return ieee80211_ref_node(ic->ic_bss);
1114127772Ssam
1115127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1116138568Ssam	IEEE80211_NODE_LOCK(nt);
1117138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1118138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1119138568Ssam
1120138568Ssam	if (ni == NULL) {
1121138568Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1122138568Ssam		    ic->ic_opmode == IEEE80211_M_AHDEMO)
1123138568Ssam			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1124138568Ssam		else {
1125138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1126138568Ssam				"[%s] no node, discard frame (%s)\n",
1127138568Ssam				ether_sprintf(macaddr), __func__);
1128138568Ssam			ic->ic_stats.is_tx_nonode++;
1129127772Ssam		}
1130127772Ssam	}
1131127772Ssam	return ni;
1132127772Ssam}
1133127772Ssam
1134127772Ssam/*
1135116742Ssam * Like find but search based on the channel too.
1136116742Ssam */
1137116742Ssamstruct ieee80211_node *
1138138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1139138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1140138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan,
1141138568Ssam	const char *func, int line)
1142138568Ssam#else
1143138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1144138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan)
1145138568Ssam#endif
1146116742Ssam{
1147116742Ssam	struct ieee80211_node *ni;
1148116742Ssam	int hash;
1149116742Ssam
1150116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1151138568Ssam	IEEE80211_NODE_LOCK(nt);
1152138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1153127772Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1154127772Ssam		    ni->ni_chan == chan) {
1155138568Ssam			ieee80211_ref_node(ni);		/* mark referenced */
1156138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1157138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1158138568Ssam			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1159138568Ssam#else
1160138568Ssam			    "%s %s refcnt %d\n", __func__,
1161138568Ssam#endif
1162138568Ssam			     ether_sprintf(ni->ni_macaddr),
1163138568Ssam			     ieee80211_node_refcnt(ni));
1164116742Ssam			break;
1165116742Ssam		}
1166116742Ssam	}
1167138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1168116742Ssam	return ni;
1169116742Ssam}
1170116742Ssam
1171138568Ssam/*
1172138568Ssam * Like find but search based on the ssid too.
1173138568Ssam */
1174138568Ssamstruct ieee80211_node *
1175138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1176138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1177138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1178138568Ssam	const char *func, int line)
1179138568Ssam#else
1180138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1181138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1182138568Ssam#endif
1183138568Ssam{
1184138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1185138568Ssam	struct ieee80211_node *ni;
1186138568Ssam	int hash;
1187138568Ssam
1188138568Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1189138568Ssam	IEEE80211_NODE_LOCK(nt);
1190138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1191138568Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1192138568Ssam		    ni->ni_esslen == ic->ic_des_esslen &&
1193138568Ssam		    memcmp(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen) == 0) {
1194138568Ssam			ieee80211_ref_node(ni);		/* mark referenced */
1195138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1196138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1197138568Ssam			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1198138568Ssam#else
1199138568Ssam			    "%s %s refcnt %d\n", __func__,
1200138568Ssam#endif
1201138568Ssam			     ether_sprintf(ni->ni_macaddr),
1202138568Ssam			     ieee80211_node_refcnt(ni));
1203138568Ssam			break;
1204138568Ssam		}
1205138568Ssam	}
1206138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1207138568Ssam	return ni;
1208138568Ssam}
1209138568Ssam
1210138568Ssam
1211116742Ssamstatic void
1212138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1213116742Ssam{
1214138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1215138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1216119150Ssam
1217138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1218138568Ssam		"%s %s in %s table\n", __func__, ether_sprintf(ni->ni_macaddr),
1219138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1220138568Ssam
1221138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1222138568Ssam	if (nt != NULL) {
1223138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1224138568Ssam		LIST_REMOVE(ni, ni_hash);
1225138568Ssam	}
1226138568Ssam	ic->ic_node_free(ni);
1227116742Ssam}
1228116742Ssam
1229116742Ssamvoid
1230138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1231138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1232138568Ssam#else
1233138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1234138568Ssam#endif
1235116742Ssam{
1236138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1237119150Ssam
1238138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1239138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1240138568Ssam		"%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1241138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1242138568Ssam#endif
1243138568Ssam	if (ieee80211_node_dectestref(ni)) {
1244138568Ssam		/*
1245138568Ssam		 * Beware; if the node is marked gone then it's already
1246138568Ssam		 * been removed from the table and we cannot assume the
1247138568Ssam		 * table still exists.  Regardless, there's no need to lock
1248138568Ssam		 * the table.
1249138568Ssam		 */
1250138568Ssam		if (ni->ni_table != NULL) {
1251138568Ssam			IEEE80211_NODE_LOCK(nt);
1252138568Ssam			_ieee80211_free_node(ni);
1253138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1254138568Ssam		} else
1255138568Ssam			_ieee80211_free_node(ni);
1256116742Ssam	}
1257116742Ssam}
1258116742Ssam
1259138568Ssam/*
1260138568Ssam * Reclaim a node.  If this is the last reference count then
1261138568Ssam * do the normal free work.  Otherwise remove it from the node
1262138568Ssam * table and mark it gone by clearing the back-reference.
1263138568Ssam */
1264138568Ssamstatic void
1265138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1266116742Ssam{
1267138568Ssam
1268138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1269138568Ssam		/*
1270138568Ssam		 * Other references are present, just remove the
1271138568Ssam		 * node from the table so it cannot be found.  When
1272138568Ssam		 * the references are dropped storage will be
1273138568Ssam		 * reclaimed.  This normally only happens for ic_bss.
1274138568Ssam		 */
1275138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1276138568Ssam		LIST_REMOVE(ni, ni_hash);
1277138568Ssam		ni->ni_table = NULL;		/* clear reference */
1278138568Ssam	} else
1279138568Ssam		_ieee80211_free_node(ni);
1280138568Ssam}
1281138568Ssam
1282138568Ssamstatic void
1283138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1284138568Ssam{
1285138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1286116742Ssam	struct ieee80211_node *ni;
1287116742Ssam
1288138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1289138568Ssam		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1290138568Ssam
1291138568Ssam	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1292138568Ssam		if (ni->ni_associd != 0) {
1293138568Ssam			if (ic->ic_auth->ia_node_leave != NULL)
1294138568Ssam				ic->ic_auth->ia_node_leave(ic, ni);
1295138568Ssam			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1296138568Ssam		}
1297138568Ssam		node_reclaim(nt, ni);
1298138568Ssam	}
1299138568Ssam	ieee80211_reset_erp(ic);
1300116742Ssam}
1301116742Ssam
1302138568Ssamstatic void
1303138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt)
1304138568Ssam{
1305138568Ssam
1306138568Ssam	IEEE80211_NODE_LOCK(nt);
1307138568Ssam	ieee80211_free_allnodes_locked(nt);
1308138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1309138568Ssam}
1310138568Ssam
1311120483Ssam/*
1312138568Ssam * Timeout entries in the scan cache.
1313138568Ssam */
1314138568Ssamstatic void
1315138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1316138568Ssam{
1317138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1318138568Ssam	struct ieee80211_node *ni, *tni;
1319138568Ssam
1320138568Ssam	IEEE80211_NODE_LOCK(nt);
1321138568Ssam	ni = ic->ic_bss;
1322138568Ssam	/* XXX belongs elsewhere */
1323138568Ssam	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1324138568Ssam		m_freem(ni->ni_rxfrag[0]);
1325138568Ssam		ni->ni_rxfrag[0] = NULL;
1326138568Ssam	}
1327138568Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1328138568Ssam		if (ni->ni_inact && --ni->ni_inact == 0) {
1329138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1330138568Ssam			    "[%s] scan candidate purged from cache "
1331138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1332138568Ssam			    ieee80211_node_refcnt(ni)-1);
1333138568Ssam			node_reclaim(nt, ni);
1334138568Ssam		}
1335138568Ssam	}
1336138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1337138568Ssam
1338138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1339138568Ssam}
1340138568Ssam
1341138568Ssam/*
1342138568Ssam * Timeout inactive stations and do related housekeeping.
1343138568Ssam * Note that we cannot hold the node lock while sending a
1344138568Ssam * frame as this would lead to a LOR.  Instead we use a
1345138568Ssam * generation number to mark nodes that we've scanned and
1346138568Ssam * drop the lock and restart a scan if we have to time out
1347138568Ssam * a node.  Since we are single-threaded by virtue of
1348120483Ssam * controlling the inactivity timer we can be sure this will
1349120483Ssam * process each node only once.
1350120483Ssam */
1351138568Ssamstatic void
1352138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt)
1353116742Ssam{
1354138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1355120483Ssam	struct ieee80211_node *ni;
1356138568Ssam	u_int gen;
1357116742Ssam
1358138568Ssam	IEEE80211_SCAN_LOCK(nt);
1359138568Ssam	gen = nt->nt_scangen++;
1360138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1361138568Ssam		"%s: sta scangen %u\n", __func__, gen);
1362120483Ssamrestart:
1363138568Ssam	IEEE80211_NODE_LOCK(nt);
1364138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1365120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1366120483Ssam			continue;
1367120483Ssam		ni->ni_scangen = gen;
1368138568Ssam		/*
1369138568Ssam		 * Free fragment if not needed anymore
1370138568Ssam		 * (last fragment older than 1s).
1371138568Ssam		 * XXX doesn't belong here
1372138568Ssam		 */
1373138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1374138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1375138568Ssam			m_freem(ni->ni_rxfrag[0]);
1376138568Ssam			ni->ni_rxfrag[0] = NULL;
1377138568Ssam		}
1378138568Ssam		ni->ni_inact--;
1379138568Ssam		if (ni->ni_associd != 0) {
1380119150Ssam			/*
1381138568Ssam			 * Age frames on the power save queue. The
1382138568Ssam			 * aging interval is 4 times the listen
1383138568Ssam			 * interval specified by the station.  This
1384138568Ssam			 * number is factored into the age calculations
1385138568Ssam			 * when the frame is placed on the queue.  We
1386138568Ssam			 * store ages as time differences we can check
1387138568Ssam			 * and/or adjust only the head of the list.
1388138568Ssam			 */
1389138568Ssam			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1390138568Ssam				struct mbuf *m;
1391138568Ssam				int discard = 0;
1392138568Ssam
1393138568Ssam				IEEE80211_NODE_SAVEQ_LOCK(ni);
1394138568Ssam				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1395138568Ssam				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1396138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1397138568Ssam					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1398138568Ssam					m_freem(m);
1399138568Ssam					discard++;
1400138568Ssam				}
1401138568Ssam				if (m != NULL)
1402138568Ssam					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1403138568Ssam				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1404138568Ssam
1405138568Ssam				if (discard != 0) {
1406138568Ssam					IEEE80211_DPRINTF(ic,
1407138568Ssam					    IEEE80211_MSG_POWER,
1408138568Ssam					    "[%s] discard %u frames for age\n",
1409138568Ssam					    ether_sprintf(ni->ni_macaddr),
1410138568Ssam					    discard);
1411138568Ssam					IEEE80211_NODE_STAT_ADD(ni,
1412138568Ssam						ps_discard, discard);
1413138568Ssam					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1414138568Ssam						ic->ic_set_tim(ic, ni, 0);
1415138568Ssam				}
1416138568Ssam			}
1417138568Ssam			/*
1418138568Ssam			 * Probe the station before time it out.  We
1419138568Ssam			 * send a null data frame which may not be
1420138568Ssam			 * universally supported by drivers (need it
1421138568Ssam			 * for ps-poll support so it should be...).
1422138568Ssam			 */
1423139528Ssam			if (0 < ni->ni_inact &&
1424139528Ssam			    ni->ni_inact <= ic->ic_inact_probe) {
1425138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1426138568Ssam				    "[%s] probe station due to inactivity\n",
1427138568Ssam				    ether_sprintf(ni->ni_macaddr));
1428138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1429138568Ssam				ieee80211_send_nulldata(ic, ni);
1430138568Ssam				/* XXX stat? */
1431138568Ssam				goto restart;
1432138568Ssam			}
1433138568Ssam		}
1434138568Ssam		if (ni->ni_inact <= 0) {
1435138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1436138568Ssam			    "[%s] station timed out due to inactivity "
1437138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1438138568Ssam			    ieee80211_node_refcnt(ni));
1439138568Ssam			/*
1440138568Ssam			 * Send a deauthenticate frame and drop the station.
1441138568Ssam			 * This is somewhat complicated due to reference counts
1442138568Ssam			 * and locking.  At this point a station will typically
1443138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1444138568Ssam			 * will do a "free" of the node which will drop the
1445138568Ssam			 * reference count.  But in the meantime a reference
1446138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1447138568Ssam			 * of the node will happen either after the tx is
1448138568Ssam			 * completed or by ieee80211_node_leave.
1449120483Ssam			 *
1450138568Ssam			 * Separately we must drop the node lock before sending
1451138568Ssam			 * in case the driver takes a lock, as this will result
1452138568Ssam			 * in  LOR between the node lock and the driver lock.
1453119150Ssam			 */
1454138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1455138568Ssam			if (ni->ni_associd != 0) {
1456138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
1457138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1458138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1459138568Ssam			}
1460138568Ssam			ieee80211_node_leave(ic, ni);
1461121180Ssam			ic->ic_stats.is_node_timeout++;
1462120483Ssam			goto restart;
1463120483Ssam		}
1464116742Ssam	}
1465138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1466138568Ssam
1467138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1468138568Ssam
1469138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1470116742Ssam}
1471116742Ssam
1472116742Ssamvoid
1473138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1474116742Ssam{
1475116742Ssam	struct ieee80211_node *ni;
1476138568Ssam	u_int gen;
1477116742Ssam
1478138568Ssam	IEEE80211_SCAN_LOCK(nt);
1479138568Ssam	gen = nt->nt_scangen++;
1480138568Ssam
1481138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1482138568Ssam		"%s: sta scangen %u\n", __func__, gen);
1483138568Ssamrestart:
1484138568Ssam	IEEE80211_NODE_LOCK(nt);
1485138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1486138568Ssam		if (ni->ni_scangen != gen) {
1487138568Ssam			ni->ni_scangen = gen;
1488138568Ssam			(void) ieee80211_ref_node(ni);
1489138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1490138568Ssam			(*f)(arg, ni);
1491138568Ssam			ieee80211_free_node(ni);
1492138568Ssam			goto restart;
1493138568Ssam		}
1494138568Ssam	}
1495138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1496138568Ssam
1497138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1498116742Ssam}
1499138568Ssam
1500138568Ssamvoid
1501138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1502138568Ssam{
1503138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
1504138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1505138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
1506138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1507138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
1508138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1509138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1510138568Ssam		ni->ni_txseqs[0],
1511138568Ssam		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1512138568Ssam		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1513138568Ssam		ni->ni_rxfragstamp);
1514138568Ssam	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
1515138568Ssam		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
1516138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1517138568Ssam		ether_sprintf(ni->ni_bssid),
1518138568Ssam		ni->ni_esslen, ni->ni_essid,
1519138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1520138568Ssam	printf("\tfails %u inact %u txrate %u\n",
1521138568Ssam		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1522138568Ssam}
1523138568Ssam
1524138568Ssamvoid
1525138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
1526138568Ssam{
1527138568Ssam	ieee80211_iterate_nodes(nt,
1528138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
1529138568Ssam}
1530138568Ssam
1531138568Ssam/*
1532138568Ssam * Handle a station joining an 11g network.
1533138568Ssam */
1534138568Ssamstatic void
1535138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1536138568Ssam{
1537138568Ssam
1538138568Ssam	/*
1539138568Ssam	 * Station isn't capable of short slot time.  Bump
1540138568Ssam	 * the count of long slot time stations and disable
1541138568Ssam	 * use of short slot time.  Note that the actual switch
1542138568Ssam	 * over to long slot time use may not occur until the
1543138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1544138568Ssam	 */
1545138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1546138568Ssam		ic->ic_longslotsta++;
1547138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1548138568Ssam		    "[%s] station needs long slot time, count %d\n",
1549138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1550138568Ssam		/* XXX vap's w/ conflicting needs won't work */
1551138568Ssam		ieee80211_set_shortslottime(ic, 0);
1552138568Ssam	}
1553138568Ssam	/*
1554138568Ssam	 * If the new station is not an ERP station
1555138568Ssam	 * then bump the counter and enable protection
1556138568Ssam	 * if configured.
1557138568Ssam	 */
1558138568Ssam	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1559138568Ssam		ic->ic_nonerpsta++;
1560138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1561138568Ssam		    "[%s] station is !ERP, %d non-ERP stations associated\n",
1562138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1563138568Ssam		/*
1564138568Ssam		 * If protection is configured, enable it.
1565138568Ssam		 */
1566138568Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
1567138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1568138568Ssam			    "%s: enable use of protection\n", __func__);
1569138568Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1570138568Ssam		}
1571138568Ssam		/*
1572138568Ssam		 * If station does not support short preamble
1573138568Ssam		 * then we must enable use of Barker preamble.
1574138568Ssam		 */
1575138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1576138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1577138568Ssam			    "[%s] station needs long preamble\n",
1578138568Ssam			    ether_sprintf(ni->ni_macaddr));
1579138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1580138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1581138568Ssam		}
1582138568Ssam	} else
1583138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
1584138568Ssam}
1585138568Ssam
1586138568Ssamvoid
1587138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1588138568Ssam{
1589138568Ssam	int newassoc;
1590138568Ssam
1591138568Ssam	if (ni->ni_associd == 0) {
1592138568Ssam		u_int16_t aid;
1593138568Ssam
1594138568Ssam		/*
1595138568Ssam		 * It would be good to search the bitmap
1596138568Ssam		 * more efficiently, but this will do for now.
1597138568Ssam		 */
1598138568Ssam		for (aid = 1; aid < ic->ic_max_aid; aid++) {
1599138568Ssam			if (!IEEE80211_AID_ISSET(aid,
1600138568Ssam			    ic->ic_aid_bitmap))
1601138568Ssam				break;
1602138568Ssam		}
1603138568Ssam		if (aid >= ic->ic_max_aid) {
1604138568Ssam			IEEE80211_SEND_MGMT(ic, ni, resp,
1605138568Ssam			    IEEE80211_REASON_ASSOC_TOOMANY);
1606138568Ssam			ieee80211_node_leave(ic, ni);
1607138568Ssam			return;
1608138568Ssam		}
1609138568Ssam		ni->ni_associd = aid | 0xc000;
1610138568Ssam		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1611138568Ssam		ic->ic_sta_assoc++;
1612138568Ssam		newassoc = 1;
1613139522Ssam		if (ic->ic_curmode == IEEE80211_MODE_11G ||
1614139522Ssam		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1615138568Ssam			ieee80211_node_join_11g(ic, ni);
1616138568Ssam	} else
1617138568Ssam		newassoc = 0;
1618138568Ssam
1619138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1620139523Ssam	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
1621139523Ssam	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
1622139523Ssam	    IEEE80211_NODE_AID(ni),
1623139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1624139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1625139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1626139523Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
1627139523Ssam	);
1628138568Ssam
1629138568Ssam	/* give driver a chance to setup state like ni_txrate */
1630139524Ssam	if (ic->ic_newassoc != NULL)
1631138568Ssam		ic->ic_newassoc(ic, ni, newassoc);
1632139528Ssam	ni->ni_inact_reload = ic->ic_inact_auth;
1633139528Ssam	ni->ni_inact = ni->ni_inact_reload;
1634138568Ssam	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1635138568Ssam	/* tell the authenticator about new station */
1636138568Ssam	if (ic->ic_auth->ia_node_join != NULL)
1637138568Ssam		ic->ic_auth->ia_node_join(ic, ni);
1638138568Ssam	ieee80211_notify_node_join(ic, ni, newassoc);
1639138568Ssam}
1640138568Ssam
1641138568Ssam/*
1642138568Ssam * Handle a station leaving an 11g network.
1643138568Ssam */
1644138568Ssamstatic void
1645138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1646138568Ssam{
1647138568Ssam
1648139522Ssam	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
1649139522Ssam		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
1650138568Ssam		("not in 11g, curmode %x", ic->ic_curmode));
1651138568Ssam
1652138568Ssam	/*
1653138568Ssam	 * If a long slot station do the slot time bookkeeping.
1654138568Ssam	 */
1655138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1656138568Ssam		KASSERT(ic->ic_longslotsta > 0,
1657138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
1658138568Ssam		ic->ic_longslotsta--;
1659138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1660138568Ssam		    "[%s] long slot time station leaves, count now %d\n",
1661138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1662138568Ssam		if (ic->ic_longslotsta == 0) {
1663138568Ssam			/*
1664138568Ssam			 * Re-enable use of short slot time if supported
1665138568Ssam			 * and not operating in IBSS mode (per spec).
1666138568Ssam			 */
1667138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1668138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
1669138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1670138568Ssam				    "%s: re-enable use of short slot time\n",
1671138568Ssam				    __func__);
1672138568Ssam				ieee80211_set_shortslottime(ic, 1);
1673138568Ssam			}
1674138568Ssam		}
1675138568Ssam	}
1676138568Ssam	/*
1677138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
1678138568Ssam	 */
1679138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1680138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
1681138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1682138568Ssam		ic->ic_nonerpsta--;
1683138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1684138568Ssam		    "[%s] non-ERP station leaves, count now %d\n",
1685138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1686138568Ssam		if (ic->ic_nonerpsta == 0) {
1687138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1688138568Ssam				"%s: disable use of protection\n", __func__);
1689138568Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1690138568Ssam			/* XXX verify mode? */
1691138568Ssam			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1692138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1693138568Ssam				    "%s: re-enable use of short preamble\n",
1694138568Ssam				    __func__);
1695138568Ssam				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1696138568Ssam				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1697138568Ssam			}
1698138568Ssam		}
1699138568Ssam	}
1700138568Ssam}
1701138568Ssam
1702138568Ssam/*
1703138568Ssam * Handle bookkeeping for station deauthentication/disassociation
1704138568Ssam * when operating as an ap.
1705138568Ssam */
1706138568Ssamvoid
1707138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1708138568Ssam{
1709138568Ssam
1710138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1711138568Ssam	    "[%s] station with aid %d leaves\n",
1712138568Ssam	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
1713138568Ssam
1714138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1715138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS ||
1716138568Ssam		ic->ic_opmode == IEEE80211_M_AHDEMO,
1717138568Ssam		("unexpected operating mode %u", ic->ic_opmode));
1718138568Ssam	/*
1719138568Ssam	 * If node wasn't previously associated all
1720138568Ssam	 * we need to do is reclaim the reference.
1721138568Ssam	 */
1722138568Ssam	/* XXX ibss mode bypasses 11g and notification */
1723138568Ssam	if (ni->ni_associd == 0)
1724138568Ssam		goto done;
1725138568Ssam	/*
1726138568Ssam	 * Tell the authenticator the station is leaving.
1727138568Ssam	 * Note that we must do this before yanking the
1728138568Ssam	 * association id as the authenticator uses the
1729138568Ssam	 * associd to locate it's state block.
1730138568Ssam	 */
1731138568Ssam	if (ic->ic_auth->ia_node_leave != NULL)
1732138568Ssam		ic->ic_auth->ia_node_leave(ic, ni);
1733138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1734138568Ssam	ni->ni_associd = 0;
1735138568Ssam	ic->ic_sta_assoc--;
1736138568Ssam
1737139522Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G ||
1738139522Ssam	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1739138568Ssam		ieee80211_node_leave_11g(ic, ni);
1740138568Ssam	/*
1741138568Ssam	 * Cleanup station state.  In particular clear various
1742138568Ssam	 * state that might otherwise be reused if the node
1743138568Ssam	 * is reused before the reference count goes to zero
1744138568Ssam	 * (and memory is reclaimed).
1745138568Ssam	 */
1746138568Ssam	ieee80211_sta_leave(ic, ni);
1747138568Ssamdone:
1748138568Ssam	ni->ni_inact_reload = ic->ic_inact_init;	/* just in case */
1749138568Ssam	ieee80211_free_node(ni);
1750138568Ssam}
1751138568Ssam
1752138568Ssamu_int8_t
1753138568Ssamieee80211_getrssi(struct ieee80211com *ic)
1754138568Ssam{
1755138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
1756138568Ssam	struct ieee80211_node_table *nt = ic->ic_sta;
1757138568Ssam	u_int32_t rssi_samples, rssi_total;
1758138568Ssam	struct ieee80211_node *ni;
1759138568Ssam
1760138568Ssam	rssi_total = 0;
1761138568Ssam	rssi_samples = 0;
1762138568Ssam	switch (ic->ic_opmode) {
1763138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
1764138568Ssam		nt = ic->ic_sta;
1765138568Ssam		if (nt == NULL)
1766138568Ssam			break;
1767138568Ssam		/* XXX locking */
1768138568Ssam		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list)
1769138568Ssam			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
1770138568Ssam				rssi_samples++;
1771138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
1772138568Ssam			}
1773138568Ssam		break;
1774138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
1775138568Ssam		nt = ic->ic_sta;
1776138568Ssam		if (nt == NULL)
1777138568Ssam			break;
1778138568Ssam		/* XXX locking */
1779138568Ssam		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list) {
1780138568Ssam			rssi_samples++;
1781138568Ssam			rssi_total += ic->ic_node_getrssi(ni);
1782138568Ssam		}
1783138568Ssam		break;
1784138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
1785138568Ssam		nt = ic->ic_sta;
1786138568Ssam		if (nt == NULL)
1787138568Ssam			break;
1788138568Ssam		/* XXX locking */
1789138568Ssam		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list)
1790138568Ssam			if (IEEE80211_AID(ni->ni_associd) != 0) {
1791138568Ssam				rssi_samples++;
1792138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
1793138568Ssam			}
1794138568Ssam		break;
1795138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
1796138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
1797138568Ssam	default:
1798138568Ssam		if (ic->ic_bss != NULL)
1799138568Ssam			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1800138568Ssam		rssi_samples = 1;
1801138568Ssam		break;
1802138568Ssam	}
1803138568Ssam	return rssi_total / NZ(rssi_samples);
1804138568Ssam#undef NZ
1805138568Ssam}
1806138568Ssam
1807138568Ssam/*
1808138568Ssam * Indicate whether there are frames queued for a station in power-save mode.
1809138568Ssam */
1810138568Ssamstatic void
1811138568Ssamieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
1812138568Ssam{
1813138568Ssam	u_int16_t aid;
1814138568Ssam
1815138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1816138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS,
1817138568Ssam		("operating mode %u", ic->ic_opmode));
1818138568Ssam
1819138568Ssam	aid = IEEE80211_AID(ni->ni_associd);
1820138568Ssam	KASSERT(aid < ic->ic_max_aid,
1821138568Ssam		("bogus aid %u, max %u", aid, ic->ic_max_aid));
1822138568Ssam
1823138568Ssam	IEEE80211_BEACON_LOCK(ic);
1824138568Ssam	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
1825138568Ssam		if (set) {
1826138568Ssam			setbit(ic->ic_tim_bitmap, aid);
1827138568Ssam			ic->ic_ps_pending++;
1828138568Ssam		} else {
1829138568Ssam			clrbit(ic->ic_tim_bitmap, aid);
1830138568Ssam			ic->ic_ps_pending--;
1831138568Ssam		}
1832138568Ssam		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
1833138568Ssam	}
1834138568Ssam	IEEE80211_BEACON_UNLOCK(ic);
1835138568Ssam}
1836138568Ssam
1837138568Ssam/*
1838138568Ssam * Node table support.
1839138568Ssam */
1840138568Ssam
1841138568Ssamstatic void
1842138568Ssamieee80211_node_table_init(struct ieee80211com *ic,
1843138568Ssam	struct ieee80211_node_table *nt,
1844138568Ssam	const char *name, int inact,
1845138568Ssam	void (*timeout)(struct ieee80211_node_table *))
1846138568Ssam{
1847138568Ssam
1848138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1849138568Ssam		"%s %s table, inact %u\n", __func__, name, inact);
1850138568Ssam
1851138568Ssam	nt->nt_ic = ic;
1852138568Ssam	/* XXX need unit */
1853138568Ssam	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1854138568Ssam	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1855138568Ssam	TAILQ_INIT(&nt->nt_node);
1856138568Ssam	nt->nt_name = name;
1857138568Ssam	nt->nt_scangen = 1;
1858138568Ssam	nt->nt_inact_init = inact;
1859138568Ssam	nt->nt_timeout = timeout;
1860138568Ssam}
1861138568Ssam
1862138568Ssamstatic struct ieee80211_node_table *
1863138568Ssamieee80211_node_table_alloc(struct ieee80211com *ic,
1864138568Ssam	const char *name, int inact,
1865138568Ssam	void (*timeout)(struct ieee80211_node_table *))
1866138568Ssam{
1867138568Ssam	struct ieee80211_node_table *nt;
1868138568Ssam
1869138568Ssam	MALLOC(nt, struct ieee80211_node_table *,
1870138568Ssam		sizeof(struct ieee80211_node_table),
1871138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
1872138568Ssam	if (nt == NULL) {
1873138568Ssam		printf("%s: no memory node table!\n", __func__);
1874138568Ssam		return NULL;
1875138568Ssam	}
1876138568Ssam	ieee80211_node_table_init(ic, nt, name, inact, timeout);
1877138568Ssam	return nt;
1878138568Ssam}
1879138568Ssam
1880138568Ssamvoid
1881138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt)
1882138568Ssam{
1883138568Ssam
1884138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1885138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1886138568Ssam
1887138568Ssam	IEEE80211_NODE_LOCK(nt);
1888138568Ssam	nt->nt_inact_timer = 0;
1889138568Ssam	ieee80211_free_allnodes_locked(nt);
1890138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1891138568Ssam}
1892138568Ssam
1893138568Ssamstatic void
1894138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1895138568Ssam{
1896138568Ssam
1897138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1898138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1899138568Ssam
1900138568Ssam	ieee80211_free_allnodes_locked(nt);
1901138568Ssam	IEEE80211_SCAN_LOCK_DESTROY(nt);
1902138568Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1903138568Ssam}
1904138568Ssam
1905138568Ssam/*
1906138568Ssam * NB: public for use in ieee80211_proto.c
1907138568Ssam */
1908138568Ssamvoid
1909138568Ssamieee80211_node_table_free(struct ieee80211_node_table *nt)
1910138568Ssam{
1911138568Ssam
1912138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1913138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1914138568Ssam
1915138568Ssam	IEEE80211_NODE_LOCK(nt);
1916138568Ssam	nt->nt_inact_timer = 0;
1917138568Ssam	ieee80211_node_table_cleanup(nt);
1918138568Ssam	FREE(nt, M_DEVBUF);
1919138568Ssam}
1920