ieee80211_node.c revision 139522
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3138568Ssam * Copyright (c) 2002-2004 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 139522 2004-12-31 21:25:28Z 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;
201138568Ssam}
202138568Ssam
203138568Ssamvoid
204138568Ssamieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni)
205138568Ssam{
206138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
207138568Ssam}
208138568Ssam
209116742Ssam/*
210138568Ssam * Set/change the channel.  The rate set is also updated as
211138568Ssam * to insure a consistent view by drivers.
212138568Ssam */
213138568Ssamstatic __inline void
214138568Ssamieee80211_set_chan(struct ieee80211com *ic,
215138568Ssam	struct ieee80211_node *ni, struct ieee80211_channel *chan)
216138568Ssam{
217138568Ssam	ni->ni_chan = chan;
218138568Ssam	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
219138568Ssam}
220138568Ssam
221138568Ssam/*
222116742Ssam * AP scanning support.
223116742Ssam */
224116742Ssam
225138568Ssam#ifdef IEEE80211_DEBUG
226138568Ssamstatic void
227138568Ssamdump_chanlist(const u_char chans[])
228138568Ssam{
229138568Ssam	const char *sep;
230138568Ssam	int i;
231138568Ssam
232138568Ssam	sep = " ";
233138568Ssam	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
234138568Ssam		if (isset(chans, i)) {
235138568Ssam			printf("%s%u", sep, i);
236138568Ssam			sep = ", ";
237138568Ssam		}
238138568Ssam}
239138568Ssam#endif /* IEEE80211_DEBUG */
240138568Ssam
241116742Ssam/*
242138568Ssam * Initialize the channel set to scan based on the
243116742Ssam * of available channels and the current PHY mode.
244116742Ssam */
245117811Ssamstatic void
246138568Ssamieee80211_reset_scan(struct ieee80211com *ic)
247116742Ssam{
248116742Ssam
249138568Ssam	/* XXX ic_des_chan should be handled with ic_chan_active */
250138568Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
251138568Ssam		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
252138568Ssam		setbit(ic->ic_chan_scan,
253138568Ssam			ieee80211_chan2ieee(ic, ic->ic_des_chan));
254138568Ssam	} else
255138568Ssam		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
256138568Ssam			sizeof(ic->ic_chan_active));
257117811Ssam	/* NB: hack, setup so next_scan starts with the first channel */
258117811Ssam	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
259138568Ssam		ieee80211_set_chan(ic, ic->ic_bss,
260138568Ssam			&ic->ic_channels[IEEE80211_CHAN_MAX]);
261138568Ssam#ifdef IEEE80211_DEBUG
262138568Ssam	if (ieee80211_msg_scan(ic)) {
263138568Ssam		printf("%s: scan set:", __func__);
264138568Ssam		dump_chanlist(ic->ic_chan_scan);
265138568Ssam		printf(" start chan %u\n",
266138568Ssam			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
267138568Ssam	}
268138568Ssam#endif /* IEEE80211_DEBUG */
269116742Ssam}
270116742Ssam
271116742Ssam/*
272116742Ssam * Begin an active scan.
273116742Ssam */
274116742Ssamvoid
275138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset)
276116742Ssam{
277116742Ssam
278138568Ssam	ic->ic_scan.nt_scangen++;
279117811Ssam	/*
280117811Ssam	 * In all but hostap mode scanning starts off in
281117811Ssam	 * an active mode before switching to passive.
282117811Ssam	 */
283121180Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
284117811Ssam		ic->ic_flags |= IEEE80211_F_ASCAN;
285121180Ssam		ic->ic_stats.is_scan_active++;
286121180Ssam	} else
287121180Ssam		ic->ic_stats.is_scan_passive++;
288139520Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
289139520Ssam		"begin %s scan in %s mode, scangen %u\n",
290138568Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
291139520Ssam		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
292116742Ssam	/*
293138568Ssam	 * Clear scan state and flush any previously seen AP's.
294116742Ssam	 */
295138568Ssam	ieee80211_reset_scan(ic);
296138568Ssam	if (reset)
297138568Ssam		ieee80211_free_allnodes(&ic->ic_scan);
298116742Ssam
299138568Ssam	ic->ic_flags |= IEEE80211_F_SCAN;
300138568Ssam
301117811Ssam	/* Scan the next channel. */
302138568Ssam	ieee80211_next_scan(ic);
303116742Ssam}
304116742Ssam
305116742Ssam/*
306116742Ssam * Switch to the next channel marked for scanning.
307116742Ssam */
308138568Ssamint
309138568Ssamieee80211_next_scan(struct ieee80211com *ic)
310116742Ssam{
311116742Ssam	struct ieee80211_channel *chan;
312116742Ssam
313138568Ssam	/*
314138568Ssam	 * Insure any previous mgt frame timeouts don't fire.
315138568Ssam	 * This assumes the driver does the right thing in
316138568Ssam	 * flushing anything queued in the driver and below.
317138568Ssam	 */
318138568Ssam	ic->ic_mgt_timer = 0;
319138568Ssam
320116742Ssam	chan = ic->ic_bss->ni_chan;
321138568Ssam	do {
322116742Ssam		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
323116742Ssam			chan = &ic->ic_channels[0];
324116742Ssam		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
325138568Ssam			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
326138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
327138568Ssam			    "%s: chan %d->%d\n", __func__,
328138568Ssam			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
329138568Ssam			    ieee80211_chan2ieee(ic, chan));
330138568Ssam			ieee80211_set_chan(ic, ic->ic_bss, chan);
331138568Ssam#ifdef notyet
332138568Ssam			/* XXX driver state change */
333116742Ssam			/*
334138568Ssam			 * Scan next channel. If doing an active scan
335138568Ssam			 * and the channel is not marked passive-only
336138568Ssam			 * then send a probe request.  Otherwise just
337138568Ssam			 * listen for beacons on the channel.
338116742Ssam			 */
339138568Ssam			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
340138568Ssam			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
341138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
342138568Ssam				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
343138568Ssam			}
344138568Ssam#else
345138568Ssam			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
346138568Ssam#endif
347138568Ssam			return 1;
348116742Ssam		}
349138568Ssam	} while (chan != ic->ic_bss->ni_chan);
350138568Ssam	ieee80211_end_scan(ic);
351138568Ssam	return 0;
352116742Ssam}
353116742Ssam
354116742Ssamvoid
355116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
356116742Ssam{
357116742Ssam	struct ieee80211_node *ni;
358116742Ssam
359138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
360138568Ssam		"%s: creating ibss\n", __func__);
361138568Ssam
362138568Ssam	/*
363138568Ssam	 * Create the station/neighbor table.  Note that for adhoc
364138568Ssam	 * mode we make the initial inactivity timer longer since
365138568Ssam	 * we create nodes only through discovery and they typically
366138568Ssam	 * are long-lived associations.
367138568Ssam	 */
368138568Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
369138568Ssam		ic->ic_sta = ieee80211_node_table_alloc(ic,
370138568Ssam					"station", ic->ic_inact_init,
371138568Ssam					ieee80211_timeout_stations);
372138568Ssam	else
373138568Ssam		ic->ic_sta = ieee80211_node_table_alloc(ic,
374138568Ssam					"neighbor", ic->ic_inact_run,
375138568Ssam					ieee80211_timeout_stations);
376138568Ssam	if (ic->ic_sta == NULL) {
377138568Ssam		/*
378138568Ssam		 * Should remain in SCAN state and retry.
379138568Ssam		 */
380138568Ssam		/* XXX stat+msg */
381138568Ssam		return;
382138568Ssam	}
383138568Ssam
384116742Ssam	ni = ic->ic_bss;
385116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
386116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
387116742Ssam	ni->ni_esslen = ic->ic_des_esslen;
388116742Ssam	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
389116742Ssam	ni->ni_rssi = 0;
390116742Ssam	ni->ni_rstamp = 0;
391138568Ssam	ni->ni_tstamp.tsf = 0;
392116742Ssam	ni->ni_intval = ic->ic_lintval;
393138568Ssam	ni->ni_capinfo = 0;
394138568Ssam	ni->ni_erp = 0;
395138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
396116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
397116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
398116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
399116742Ssam		ni->ni_fhindex = 1;
400116742Ssam	}
401138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
402138568Ssam		ic->ic_flags |= IEEE80211_F_SIBSS;
403138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
404138568Ssam		ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
405138568Ssam	}
406138568Ssam	/*
407138568Ssam	 * Fix the channel and related attributes.
408138568Ssam	 */
409138568Ssam	ieee80211_set_chan(ic, ni, chan);
410138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
411138568Ssam	/*
412138568Ssam	 * Do mode-specific rate setup.
413138568Ssam	 */
414138568Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G) {
415138568Ssam		/*
416138568Ssam		 * Use a mixed 11b/11g rate set.
417138568Ssam		 */
418138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
419138568Ssam	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
420138568Ssam		/*
421138568Ssam		 * Force pure 11b rate set.
422138568Ssam		 */
423138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
424138568Ssam	}
425138568Ssam	/*
426138568Ssam	 * Set the erp state (mostly the slot time) to deal with
427138568Ssam	 * the auto-select case; this should be redundant if the
428138568Ssam	 * mode is locked.
429138568Ssam	 */
430138568Ssam	ieee80211_reset_erp(ic);
431138568Ssam	ieee80211_wme_initparams(ic);
432138568Ssam
433117811Ssam	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
434116742Ssam}
435116742Ssam
436138568Ssamvoid
437138568Ssamieee80211_reset_bss(struct ieee80211com *ic)
438138568Ssam{
439138568Ssam	struct ieee80211_node *ni, *obss;
440138568Ssam
441138568Ssam	ieee80211_node_table_reset(&ic->ic_scan);
442138568Ssam	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
443138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
444138568Ssam	obss = ic->ic_bss;
445138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);
446138568Ssam	if (obss != NULL)
447138568Ssam		ieee80211_free_node(obss);
448138568Ssam	if (ic->ic_sta != NULL) {
449138568Ssam		ieee80211_node_table_free(ic->ic_sta);
450138568Ssam		ic->ic_sta = NULL;
451138568Ssam	}
452138568Ssam}
453138568Ssam
454127767Ssamstatic int
455138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
456127767Ssam{
457127767Ssam        u_int8_t rate;
458127767Ssam        int fail;
459127767Ssam
460127767Ssam	fail = 0;
461127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
462127767Ssam		fail |= 0x01;
463127767Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
464127767Ssam	    ni->ni_chan != ic->ic_des_chan)
465127767Ssam		fail |= 0x01;
466127767Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
467127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
468127767Ssam			fail |= 0x02;
469127767Ssam	} else {
470127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
471127767Ssam			fail |= 0x02;
472127767Ssam	}
473138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
474127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
475127767Ssam			fail |= 0x04;
476127767Ssam	} else {
477127767Ssam		/* XXX does this mean privacy is supported or required? */
478127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
479127767Ssam			fail |= 0x04;
480127767Ssam	}
481127767Ssam	rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
482127767Ssam	if (rate & IEEE80211_RATE_BASIC)
483127767Ssam		fail |= 0x08;
484127767Ssam	if (ic->ic_des_esslen != 0 &&
485127767Ssam	    (ni->ni_esslen != ic->ic_des_esslen ||
486127767Ssam	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
487127767Ssam		fail |= 0x10;
488127767Ssam	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
489127767Ssam	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
490127767Ssam		fail |= 0x20;
491127767Ssam#ifdef IEEE80211_DEBUG
492138568Ssam	if (ieee80211_msg_scan(ic)) {
493127767Ssam		printf(" %c %s", fail ? '-' : '+',
494127767Ssam		    ether_sprintf(ni->ni_macaddr));
495127767Ssam		printf(" %s%c", ether_sprintf(ni->ni_bssid),
496127767Ssam		    fail & 0x20 ? '!' : ' ');
497127767Ssam		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
498127767Ssam			fail & 0x01 ? '!' : ' ');
499127767Ssam		printf(" %+4d", ni->ni_rssi);
500127767Ssam		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
501127767Ssam		    fail & 0x08 ? '!' : ' ');
502127767Ssam		printf(" %4s%c",
503127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
504127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
505127767Ssam		    "????",
506127767Ssam		    fail & 0x02 ? '!' : ' ');
507127767Ssam		printf(" %3s%c ",
508127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
509127767Ssam		    "wep" : "no",
510127767Ssam		    fail & 0x04 ? '!' : ' ');
511127767Ssam		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
512127767Ssam		printf("%s\n", fail & 0x10 ? "!" : "");
513127767Ssam	}
514127767Ssam#endif
515127767Ssam	return fail;
516127767Ssam}
517127767Ssam
518138568Ssamstatic __inline u_int8_t
519138568Ssammaxrate(const struct ieee80211_node *ni)
520138568Ssam{
521138568Ssam	const struct ieee80211_rateset *rs = &ni->ni_rates;
522138568Ssam	/* NB: assumes rate set is sorted (happens on frame receive) */
523138568Ssam	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
524138568Ssam}
525138568Ssam
526116742Ssam/*
527138568Ssam * Compare the capabilities of two nodes and decide which is
528138568Ssam * more desirable (return >0 if a is considered better).  Note
529138568Ssam * that we assume compatibility/usability has already been checked
530138568Ssam * so we don't need to (e.g. validate whether privacy is supported).
531138568Ssam * Used to select the best scan candidate for association in a BSS.
532138568Ssam */
533138568Ssamstatic int
534138568Ssamieee80211_node_compare(struct ieee80211com *ic,
535138568Ssam		       const struct ieee80211_node *a,
536138568Ssam		       const struct ieee80211_node *b)
537138568Ssam{
538138568Ssam	u_int8_t maxa, maxb;
539138568Ssam	u_int8_t rssia, rssib;
540138568Ssam
541138568Ssam	/* privacy support preferred */
542138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
543138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
544138568Ssam		return 1;
545138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
546138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
547138568Ssam		return -1;
548138568Ssam
549138568Ssam	/* best/max rate preferred if signal level close enough XXX */
550138568Ssam	maxa = maxrate(a);
551138568Ssam	maxb = maxrate(b);
552138568Ssam	rssia = ic->ic_node_getrssi(a);
553138568Ssam	rssib = ic->ic_node_getrssi(b);
554138568Ssam	if (maxa != maxb && abs(rssib - rssia) < 5)
555138568Ssam		return maxa - maxb;
556138568Ssam
557138568Ssam	/* XXX use freq for channel preference */
558138568Ssam	/* for now just prefer 5Ghz band to all other bands */
559138568Ssam	if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
560138568Ssam	   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
561138568Ssam		return 1;
562138568Ssam	if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
563138568Ssam	     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
564138568Ssam		return -1;
565138568Ssam
566138568Ssam	/* all things being equal, use signal level */
567138568Ssam	return rssia - rssib;
568138568Ssam}
569138568Ssam
570138568Ssam/*
571116742Ssam * Complete a scan of potential channels.
572116742Ssam */
573116742Ssamvoid
574138568Ssamieee80211_end_scan(struct ieee80211com *ic)
575116742Ssam{
576116742Ssam	struct ieee80211_node *ni, *nextbs, *selbs;
577138568Ssam	struct ieee80211_node_table *nt;
578116742Ssam
579138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "end %s scan\n",
580138568Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
581116742Ssam
582138568Ssam	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
583138568Ssam	nt = &ic->ic_scan;
584138568Ssam	ni = TAILQ_FIRST(&nt->nt_node);
585138568Ssam
586138568Ssam	ieee80211_notify_scan_done(ic);
587138568Ssam
588116742Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
589138568Ssam		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
590138568Ssam		int i, bestchan;
591138568Ssam		u_int8_t rssi;
592138568Ssam
593116742Ssam		/*
594116742Ssam		 * The passive scan to look for existing AP's completed,
595116742Ssam		 * select a channel to camp on.  Identify the channels
596116742Ssam		 * that already have one or more AP's and try to locate
597138568Ssam		 * an unnoccupied one.  If that fails, pick a channel that
598138568Ssam		 * looks to be quietest.
599116742Ssam		 */
600138568Ssam		memset(maxrssi, 0, sizeof(maxrssi));
601116742Ssam		for (; ni != NULL; ni = nextbs) {
602116742Ssam			ieee80211_ref_node(ni);
603116742Ssam			nextbs = TAILQ_NEXT(ni, ni_list);
604138568Ssam			rssi = ic->ic_node_getrssi(ni);
605138568Ssam			i = ieee80211_chan2ieee(ic, ni->ni_chan);
606138568Ssam			if (rssi > maxrssi[i])
607138568Ssam				maxrssi[i] = rssi;
608138568Ssam			ieee80211_unref_node(&ni);
609116742Ssam		}
610138568Ssam		/* XXX select channel more intelligently */
611138568Ssam		bestchan = -1;
612116742Ssam		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
613138568Ssam			if (isset(ic->ic_chan_active, i)) {
614138568Ssam				/*
615138568Ssam				 * If the channel is unoccupied the max rssi
616138568Ssam				 * should be zero; just take it.  Otherwise
617138568Ssam				 * track the channel with the lowest rssi and
618138568Ssam				 * use that when all channels appear occupied.
619138568Ssam				 */
620138568Ssam				if (maxrssi[i] == 0) {
621138568Ssam					bestchan = i;
622116742Ssam					break;
623138568Ssam				}
624138568Ssam				if (maxrssi[i] < maxrssi[bestchan])
625138568Ssam					bestchan = i;
626138568Ssam			}
627138568Ssam		if (bestchan != -1) {
628138568Ssam			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
629138568Ssam			return;
630116742Ssam		}
631138568Ssam		/* no suitable channel, should not happen */
632138568Ssam	}
633138568Ssam
634138568Ssam	/*
635138568Ssam	 * When manually sequencing the state machine; scan just once
636138568Ssam	 * regardless of whether we have a candidate or not.  The
637138568Ssam	 * controlling application is expected to setup state and
638138568Ssam	 * initiate an association.
639138568Ssam	 */
640138568Ssam	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
641116742Ssam		return;
642138568Ssam	/*
643138568Ssam	 * Automatic sequencing; look for a candidate and
644138568Ssam	 * if found join the network.
645138568Ssam	 */
646116742Ssam	if (ni == NULL) {
647138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
648138568Ssam			"%s: no scan candidate\n", __func__);
649116742Ssam  notfound:
650116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS &&
651116742Ssam		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
652116742Ssam		    ic->ic_des_esslen != 0) {
653116742Ssam			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
654116742Ssam			return;
655116742Ssam		}
656116742Ssam		/*
657116742Ssam		 * Reset the list of channels to scan and start again.
658116742Ssam		 */
659138568Ssam		ieee80211_reset_scan(ic);
660138568Ssam		ic->ic_flags |= IEEE80211_F_SCAN;
661138568Ssam		ieee80211_next_scan(ic);
662116742Ssam		return;
663116742Ssam	}
664116742Ssam	selbs = NULL;
665138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
666138568Ssam	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
667116742Ssam	for (; ni != NULL; ni = nextbs) {
668116742Ssam		ieee80211_ref_node(ni);
669116742Ssam		nextbs = TAILQ_NEXT(ni, ni_list);
670116742Ssam		if (ni->ni_fails) {
671116742Ssam			/*
672116742Ssam			 * The configuration of the access points may change
673116742Ssam			 * during my scan.  So delete the entry for the AP
674116742Ssam			 * and retry to associate if there is another beacon.
675116742Ssam			 */
676138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
677138568Ssam				"%s: skip scan candidate %s, fails %u\n",
678138568Ssam				__func__, ether_sprintf(ni->ni_macaddr),
679138568Ssam				ni->ni_fails);
680116742Ssam			if (ni->ni_fails++ > 2)
681138568Ssam				ieee80211_free_node(ni);
682116742Ssam			continue;
683116742Ssam		}
684138568Ssam		if (ieee80211_match_bss(ic, ni) == 0) {
685116742Ssam			if (selbs == NULL)
686116742Ssam				selbs = ni;
687138568Ssam			else if (ieee80211_node_compare(ic, ni, selbs) > 0) {
688116742Ssam				ieee80211_unref_node(&selbs);
689116742Ssam				selbs = ni;
690116742Ssam			} else
691116742Ssam				ieee80211_unref_node(&ni);
692116742Ssam		} else {
693116742Ssam			ieee80211_unref_node(&ni);
694116742Ssam		}
695116742Ssam	}
696116742Ssam	if (selbs == NULL)
697116742Ssam		goto notfound;
698138568Ssam	if (!ieee80211_sta_join(ic, selbs)) {
699138568Ssam		ieee80211_unref_node(&selbs);
700138568Ssam		goto notfound;
701138568Ssam	}
702138568Ssam}
703138568Ssam
704138568Ssam/*
705138568Ssam * Handle 802.11 ad hoc network merge.  The
706138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
707138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
708138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
709138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
710138568Ssam * sends beacons with the greatest TSF timestamp.
711138568Ssam *
712138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
713138568Ssam *
714138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
715138568Ssam */
716138568Ssamint
717138568Ssamieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni)
718138568Ssam{
719138568Ssam
720138568Ssam	if (IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
721138568Ssam		/* unchanged, nothing to do */
722138568Ssam		return 0;
723138568Ssam	}
724138568Ssam	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
725138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
726138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
727138568Ssam		ic->ic_stats.is_ibss_capmismatch++;
728138568Ssam		return 0;
729138568Ssam	}
730138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
731138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
732138568Ssam		ether_sprintf(ni->ni_bssid),
733138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
734138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
735138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
736138568Ssam	);
737138568Ssam	return ieee80211_sta_join(ic, ni);
738138568Ssam}
739138568Ssam
740138568Ssam/*
741138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
742138568Ssam * be passed in with a held reference.
743138568Ssam */
744138568Ssamint
745138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
746138568Ssam{
747138568Ssam	struct ieee80211_node *obss;
748138568Ssam
749116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
750138568Ssam		/*
751138568Ssam		 * Check rate set before committing to this node.
752138568Ssam		 */
753138568Ssam		ieee80211_fix_rate(ic, selbs, IEEE80211_F_DOFRATE |
754116742Ssam		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
755138568Ssam		if (selbs->ni_rates.rs_nrates == 0) {
756116742Ssam			selbs->ni_fails++;
757138568Ssam			ic->ic_stats.is_ibss_norate++;
758138568Ssam			return 0;
759116742Ssam		}
760127772Ssam		/*
761139521Ssam		 * Create the neighbor table; it will already
762139521Ssam		 * exist if we are simply switching mastership.
763127772Ssam		 */
764139521Ssam		if (ic->ic_sta == NULL) {
765139521Ssam			ic->ic_sta = ieee80211_node_table_alloc(ic,
766138568Ssam					"neighbor", ic->ic_inact_run,
767138568Ssam					ieee80211_timeout_stations);
768139521Ssam			if (ic->ic_sta == NULL) {
769139521Ssam				/*
770139521Ssam				 * Should remain in SCAN state and retry.
771139521Ssam				 */
772139521Ssam				/* XXX stat+msg */
773139521Ssam				return 0;
774139521Ssam			}
775138568Ssam		}
776138568Ssam	}
777138568Ssam
778138568Ssam	/*
779138568Ssam	 * Committed to selbs, setup state.
780138568Ssam	 */
781138568Ssam	obss = ic->ic_bss;
782138568Ssam	ic->ic_bss = selbs;
783138568Ssam	if (obss != NULL)
784138568Ssam		ieee80211_free_node(obss);
785138568Ssam	/*
786138568Ssam	 * Set the erp state (mostly the slot time) to deal with
787138568Ssam	 * the auto-select case; this should be redundant if the
788138568Ssam	 * mode is locked.
789138568Ssam	 */
790138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
791138568Ssam	ieee80211_reset_erp(ic);
792138568Ssam	ieee80211_wme_initparams(ic);
793138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
794117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
795138568Ssam	else
796117811Ssam		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
797138568Ssam	return 1;
798116742Ssam}
799116742Ssam
800138568Ssam/*
801138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
802138568Ssam * be passed in with a held reference.
803138568Ssam */
804138568Ssamvoid
805138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
806138568Ssam{
807138568Ssam	ic->ic_node_cleanup(ni);
808138568Ssam	ieee80211_notify_node_leave(ic, ni);
809138568Ssam}
810138568Ssam
811116742Ssamstatic struct ieee80211_node *
812138568Ssamnode_alloc(struct ieee80211_node_table *nt)
813116742Ssam{
814127768Ssam	struct ieee80211_node *ni;
815138568Ssam
816127768Ssam	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
817127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
818127768Ssam	return ni;
819116742Ssam}
820116742Ssam
821138568Ssam/*
822138568Ssam * Reclaim any resources in a node and reset any critical
823138568Ssam * state.  Typically nodes are free'd immediately after,
824138568Ssam * but in some cases the storage may be reused so we need
825138568Ssam * to insure consistent state (should probably fix that).
826138568Ssam */
827116742Ssamstatic void
828138568Ssamnode_cleanup(struct ieee80211_node *ni)
829116742Ssam{
830138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
831138568Ssam	struct ieee80211com *ic = ni->ni_ic;
832138568Ssam	int i, qlen;
833138568Ssam
834138568Ssam	/* NB: preserve ni_table */
835138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
836138568Ssam		ic->ic_ps_sta--;
837138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
838138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
839138568Ssam		    "[%s] power save mode off, %u sta's in ps mode\n",
840138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
841138568Ssam	}
842138568Ssam
843138568Ssam	/*
844138568Ssam	 * Drain power save queue and, if needed, clear TIM.
845138568Ssam	 */
846138568Ssam	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
847138568Ssam	if (qlen != 0 && ic->ic_set_tim != NULL)
848138568Ssam		ic->ic_set_tim(ic, ni, 0);
849138568Ssam
850138568Ssam	ni->ni_associd = 0;
851138568Ssam	if (ni->ni_challenge != NULL) {
852138568Ssam		FREE(ni->ni_challenge, M_DEVBUF);
853138568Ssam		ni->ni_challenge = NULL;
854138568Ssam	}
855138568Ssam	/*
856138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
857138568Ssam	 * reusable during a re-auth/re-assoc state transition.
858138568Ssam	 * If we remove these data they will not be recreated
859138568Ssam	 * because they come from a probe-response or beacon frame
860138568Ssam	 * which cannot be expected prior to the association-response.
861138568Ssam	 * This should not be an issue when operating in other modes
862138568Ssam	 * as stations leaving always go through a full state transition
863138568Ssam	 * which will rebuild this state.
864138568Ssam	 *
865138568Ssam	 * XXX does this leave us open to inheriting old state?
866138568Ssam	 */
867138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
868138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
869138568Ssam			m_freem(ni->ni_rxfrag[i]);
870138568Ssam			ni->ni_rxfrag[i] = NULL;
871138568Ssam		}
872138568Ssam	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
873138568Ssam#undef N
874116742Ssam}
875116742Ssam
876116742Ssamstatic void
877138568Ssamnode_free(struct ieee80211_node *ni)
878116742Ssam{
879138568Ssam	struct ieee80211com *ic = ni->ni_ic;
880138568Ssam
881138568Ssam	ic->ic_node_cleanup(ni);
882138568Ssam	if (ni->ni_wpa_ie != NULL)
883138568Ssam		FREE(ni->ni_wpa_ie, M_DEVBUF);
884138568Ssam	if (ni->ni_wme_ie != NULL)
885138568Ssam		FREE(ni->ni_wme_ie, M_DEVBUF);
886138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
887138568Ssam	FREE(ni, M_80211_NODE);
888116742Ssam}
889116742Ssam
890120104Ssamstatic u_int8_t
891138568Ssamnode_getrssi(const struct ieee80211_node *ni)
892120104Ssam{
893120104Ssam	return ni->ni_rssi;
894120104Ssam}
895120104Ssam
896116742Ssamstatic void
897138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt,
898138568Ssam	struct ieee80211_node *ni, const u_int8_t *macaddr)
899116742Ssam{
900138568Ssam	struct ieee80211com *ic = nt->nt_ic;
901116742Ssam	int hash;
902116742Ssam
903138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
904138568Ssam		"%s %s in %s table\n", __func__,
905138568Ssam		ether_sprintf(macaddr), nt->nt_name);
906138568Ssam
907116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
908116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
909138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
910138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
911138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
912138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
913138568Ssam	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
914138568Ssam	ni->ni_inact = ni->ni_inact_reload = nt->nt_inact_init;
915138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
916138568Ssam
917138568Ssam	IEEE80211_NODE_LOCK(nt);
918138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
919138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
920138568Ssam	ni->ni_table = nt;
921138568Ssam	ni->ni_ic = ic;
922138568Ssam	IEEE80211_NODE_UNLOCK(nt);
923116742Ssam}
924116742Ssam
925116742Ssamstruct ieee80211_node *
926138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
927116742Ssam{
928138568Ssam	struct ieee80211com *ic = nt->nt_ic;
929138568Ssam	struct ieee80211_node *ni;
930138568Ssam
931138568Ssam	ni = ic->ic_node_alloc(nt);
932116742Ssam	if (ni != NULL)
933138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
934127769Ssam	else
935127769Ssam		ic->ic_stats.is_rx_nodealloc++;
936116742Ssam	return ni;
937116742Ssam}
938116742Ssam
939116742Ssamstruct ieee80211_node *
940138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
941116742Ssam{
942138568Ssam	struct ieee80211com *ic = nt->nt_ic;
943138568Ssam	struct ieee80211_node *ni;
944138568Ssam
945138568Ssam	ni = ic->ic_node_alloc(nt);
946116742Ssam	if (ni != NULL) {
947138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
948127770Ssam		/*
949127770Ssam		 * Inherit from ic_bss.
950127770Ssam		 */
951138568Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
952138568Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
953138568Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
954127770Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
955138568Ssam		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
956138568Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
957127770Ssam	} else
958127770Ssam		ic->ic_stats.is_rx_nodealloc++;
959116742Ssam	return ni;
960116742Ssam}
961116742Ssam
962127772Ssamstatic struct ieee80211_node *
963138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
964138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
965138568Ssam	const u_int8_t *macaddr, const char *func, int line)
966138568Ssam#else
967138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt,
968138568Ssam	const u_int8_t *macaddr)
969138568Ssam#endif
970116742Ssam{
971116742Ssam	struct ieee80211_node *ni;
972116742Ssam	int hash;
973116742Ssam
974138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
975127772Ssam
976116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
977138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
978116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
979138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
980138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
981138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
982138568Ssam			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
983138568Ssam			     ether_sprintf(ni->ni_macaddr),
984138568Ssam			     ieee80211_node_refcnt(ni));
985138568Ssam#endif
986127772Ssam			return ni;
987116742Ssam		}
988116742Ssam	}
989127772Ssam	return NULL;
990127772Ssam}
991138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
992138568Ssam#define	_ieee80211_find_node(nt, mac) \
993138568Ssam	_ieee80211_find_node_debug(nt, mac, func, line)
994138568Ssam#endif
995127772Ssam
996127772Ssamstruct ieee80211_node *
997138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
998138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
999138568Ssam	const u_int8_t *macaddr, const char *func, int line)
1000138568Ssam#else
1001138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1002138568Ssam#endif
1003127772Ssam{
1004127772Ssam	struct ieee80211_node *ni;
1005127772Ssam
1006138568Ssam	IEEE80211_NODE_LOCK(nt);
1007138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1008138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1009116742Ssam	return ni;
1010116742Ssam}
1011116742Ssam
1012116742Ssam/*
1013138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1014138568Ssam * Note that for the driver's benefit we we treat this like
1015138568Ssam * an association so the driver has an opportunity to setup
1016138568Ssam * it's private state.
1017138568Ssam */
1018138568Ssamstruct ieee80211_node *
1019138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1020138568Ssam	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1021138568Ssam{
1022138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1023138568Ssam	struct ieee80211_node *ni;
1024138568Ssam
1025138568Ssam	ni = ieee80211_dup_bss(nt, macaddr);
1026138568Ssam	if (ni != NULL) {
1027138568Ssam		/* XXX no rate negotiation; just dup */
1028138568Ssam		ni->ni_rates = ic->ic_bss->ni_rates;
1029138568Ssam		if (ic->ic_newassoc)
1030138568Ssam			ic->ic_newassoc(ic, ni, 1);
1031138568Ssam		/* XXX not right for 802.1x/WPA */
1032138568Ssam		ieee80211_node_authorize(ic, ni);
1033138568Ssam		ieee80211_ref_node(ni);		/* hold reference */
1034138568Ssam	}
1035138568Ssam	return ni;
1036138568Ssam}
1037138568Ssam
1038138568Ssam/*
1039138568Ssam * Locate the node for sender, track state, and then pass the
1040138568Ssam * (referenced) node up to the 802.11 layer for its use.  We
1041138568Ssam * are required to pass some node so we fall back to ic_bss
1042138568Ssam * when this frame is from an unknown sender.  The 802.11 layer
1043138568Ssam * knows this means the sender wasn't in the node table and
1044138568Ssam * acts accordingly.
1045138568Ssam */
1046138568Ssamstruct ieee80211_node *
1047138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1048138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1049138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1050138568Ssam#else
1051138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1052138568Ssam	const struct ieee80211_frame_min *wh)
1053138568Ssam#endif
1054138568Ssam{
1055138568Ssam#define	IS_CTL(wh) \
1056138568Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1057138568Ssam#define	IS_PSPOLL(wh) \
1058138568Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1059138568Ssam	struct ieee80211_node_table *nt;
1060138568Ssam	struct ieee80211_node *ni;
1061138568Ssam
1062138568Ssam	/* XXX may want scanned nodes in the neighbor table for adhoc */
1063138568Ssam	if (ic->ic_opmode == IEEE80211_M_STA ||
1064138568Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1065138568Ssam	    (ic->ic_flags & IEEE80211_F_SCAN))
1066138568Ssam		nt = &ic->ic_scan;
1067138568Ssam	else
1068138568Ssam		nt = ic->ic_sta;
1069138568Ssam	/* XXX check ic_bss first in station mode */
1070138568Ssam	/* XXX 4-address frames? */
1071138568Ssam	IEEE80211_NODE_LOCK(nt);
1072138568Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1073138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr1);
1074138568Ssam	else
1075138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr2);
1076138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1077138568Ssam
1078138568Ssam	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
1079138568Ssam#undef IS_PSPOLL
1080138568Ssam#undef IS_CTL
1081138568Ssam}
1082138568Ssam
1083138568Ssam/*
1084127772Ssam * Return a reference to the appropriate node for sending
1085127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1086127772Ssam */
1087127772Ssamstruct ieee80211_node *
1088138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1089138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1090138568Ssam	const char *func, int line)
1091138568Ssam#else
1092138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1093138568Ssam#endif
1094127772Ssam{
1095138568Ssam	struct ieee80211_node_table *nt = ic->ic_sta;
1096127772Ssam	struct ieee80211_node *ni;
1097127772Ssam
1098127772Ssam	/*
1099127772Ssam	 * The destination address should be in the node table
1100127772Ssam	 * unless we are operating in station mode or this is a
1101127772Ssam	 * multicast/broadcast frame.
1102127772Ssam	 */
1103138568Ssam	if (nt == NULL || IEEE80211_IS_MULTICAST(macaddr))
1104138568Ssam		return ieee80211_ref_node(ic->ic_bss);
1105127772Ssam
1106127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1107138568Ssam	IEEE80211_NODE_LOCK(nt);
1108138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1109138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1110138568Ssam
1111138568Ssam	if (ni == NULL) {
1112138568Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1113138568Ssam		    ic->ic_opmode == IEEE80211_M_AHDEMO)
1114138568Ssam			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1115138568Ssam		else {
1116138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1117138568Ssam				"[%s] no node, discard frame (%s)\n",
1118138568Ssam				ether_sprintf(macaddr), __func__);
1119138568Ssam			ic->ic_stats.is_tx_nonode++;
1120127772Ssam		}
1121127772Ssam	}
1122127772Ssam	return ni;
1123127772Ssam}
1124127772Ssam
1125127772Ssam/*
1126116742Ssam * Like find but search based on the channel too.
1127116742Ssam */
1128116742Ssamstruct ieee80211_node *
1129138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1130138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1131138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan,
1132138568Ssam	const char *func, int line)
1133138568Ssam#else
1134138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1135138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan)
1136138568Ssam#endif
1137116742Ssam{
1138116742Ssam	struct ieee80211_node *ni;
1139116742Ssam	int hash;
1140116742Ssam
1141116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1142138568Ssam	IEEE80211_NODE_LOCK(nt);
1143138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1144127772Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1145127772Ssam		    ni->ni_chan == chan) {
1146138568Ssam			ieee80211_ref_node(ni);		/* mark referenced */
1147138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1148138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1149138568Ssam			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1150138568Ssam#else
1151138568Ssam			    "%s %s refcnt %d\n", __func__,
1152138568Ssam#endif
1153138568Ssam			     ether_sprintf(ni->ni_macaddr),
1154138568Ssam			     ieee80211_node_refcnt(ni));
1155116742Ssam			break;
1156116742Ssam		}
1157116742Ssam	}
1158138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1159116742Ssam	return ni;
1160116742Ssam}
1161116742Ssam
1162138568Ssam/*
1163138568Ssam * Like find but search based on the ssid too.
1164138568Ssam */
1165138568Ssamstruct ieee80211_node *
1166138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1167138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1168138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1169138568Ssam	const char *func, int line)
1170138568Ssam#else
1171138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1172138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1173138568Ssam#endif
1174138568Ssam{
1175138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1176138568Ssam	struct ieee80211_node *ni;
1177138568Ssam	int hash;
1178138568Ssam
1179138568Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1180138568Ssam	IEEE80211_NODE_LOCK(nt);
1181138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1182138568Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1183138568Ssam		    ni->ni_esslen == ic->ic_des_esslen &&
1184138568Ssam		    memcmp(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen) == 0) {
1185138568Ssam			ieee80211_ref_node(ni);		/* mark referenced */
1186138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1187138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1188138568Ssam			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1189138568Ssam#else
1190138568Ssam			    "%s %s refcnt %d\n", __func__,
1191138568Ssam#endif
1192138568Ssam			     ether_sprintf(ni->ni_macaddr),
1193138568Ssam			     ieee80211_node_refcnt(ni));
1194138568Ssam			break;
1195138568Ssam		}
1196138568Ssam	}
1197138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1198138568Ssam	return ni;
1199138568Ssam}
1200138568Ssam
1201138568Ssam
1202116742Ssamstatic void
1203138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1204116742Ssam{
1205138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1206138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1207119150Ssam
1208138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1209138568Ssam		"%s %s in %s table\n", __func__, ether_sprintf(ni->ni_macaddr),
1210138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1211138568Ssam
1212138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1213138568Ssam	if (nt != NULL) {
1214138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1215138568Ssam		LIST_REMOVE(ni, ni_hash);
1216138568Ssam	}
1217138568Ssam	ic->ic_node_free(ni);
1218116742Ssam}
1219116742Ssam
1220116742Ssamvoid
1221138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1222138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1223138568Ssam#else
1224138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1225138568Ssam#endif
1226116742Ssam{
1227138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1228119150Ssam
1229138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1230138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1231138568Ssam		"%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1232138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1233138568Ssam#endif
1234138568Ssam	if (ieee80211_node_dectestref(ni)) {
1235138568Ssam		/*
1236138568Ssam		 * Beware; if the node is marked gone then it's already
1237138568Ssam		 * been removed from the table and we cannot assume the
1238138568Ssam		 * table still exists.  Regardless, there's no need to lock
1239138568Ssam		 * the table.
1240138568Ssam		 */
1241138568Ssam		if (ni->ni_table != NULL) {
1242138568Ssam			IEEE80211_NODE_LOCK(nt);
1243138568Ssam			_ieee80211_free_node(ni);
1244138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1245138568Ssam		} else
1246138568Ssam			_ieee80211_free_node(ni);
1247116742Ssam	}
1248116742Ssam}
1249116742Ssam
1250138568Ssam/*
1251138568Ssam * Reclaim a node.  If this is the last reference count then
1252138568Ssam * do the normal free work.  Otherwise remove it from the node
1253138568Ssam * table and mark it gone by clearing the back-reference.
1254138568Ssam */
1255138568Ssamstatic void
1256138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1257116742Ssam{
1258138568Ssam
1259138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1260138568Ssam		/*
1261138568Ssam		 * Other references are present, just remove the
1262138568Ssam		 * node from the table so it cannot be found.  When
1263138568Ssam		 * the references are dropped storage will be
1264138568Ssam		 * reclaimed.  This normally only happens for ic_bss.
1265138568Ssam		 */
1266138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1267138568Ssam		LIST_REMOVE(ni, ni_hash);
1268138568Ssam		ni->ni_table = NULL;		/* clear reference */
1269138568Ssam	} else
1270138568Ssam		_ieee80211_free_node(ni);
1271138568Ssam}
1272138568Ssam
1273138568Ssamstatic void
1274138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1275138568Ssam{
1276138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1277116742Ssam	struct ieee80211_node *ni;
1278116742Ssam
1279138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1280138568Ssam		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1281138568Ssam
1282138568Ssam	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1283138568Ssam		if (ni->ni_associd != 0) {
1284138568Ssam			if (ic->ic_auth->ia_node_leave != NULL)
1285138568Ssam				ic->ic_auth->ia_node_leave(ic, ni);
1286138568Ssam			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1287138568Ssam		}
1288138568Ssam		node_reclaim(nt, ni);
1289138568Ssam	}
1290138568Ssam	ieee80211_reset_erp(ic);
1291116742Ssam}
1292116742Ssam
1293138568Ssamstatic void
1294138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt)
1295138568Ssam{
1296138568Ssam
1297138568Ssam	IEEE80211_NODE_LOCK(nt);
1298138568Ssam	ieee80211_free_allnodes_locked(nt);
1299138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1300138568Ssam}
1301138568Ssam
1302120483Ssam/*
1303138568Ssam * Timeout entries in the scan cache.
1304138568Ssam */
1305138568Ssamstatic void
1306138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1307138568Ssam{
1308138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1309138568Ssam	struct ieee80211_node *ni, *tni;
1310138568Ssam
1311138568Ssam	IEEE80211_NODE_LOCK(nt);
1312138568Ssam	ni = ic->ic_bss;
1313138568Ssam	/* XXX belongs elsewhere */
1314138568Ssam	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1315138568Ssam		m_freem(ni->ni_rxfrag[0]);
1316138568Ssam		ni->ni_rxfrag[0] = NULL;
1317138568Ssam	}
1318138568Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1319138568Ssam		if (ni->ni_inact && --ni->ni_inact == 0) {
1320138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1321138568Ssam			    "[%s] scan candidate purged from cache "
1322138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1323138568Ssam			    ieee80211_node_refcnt(ni)-1);
1324138568Ssam			node_reclaim(nt, ni);
1325138568Ssam		}
1326138568Ssam	}
1327138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1328138568Ssam
1329138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1330138568Ssam}
1331138568Ssam
1332138568Ssam/*
1333138568Ssam * Timeout inactive stations and do related housekeeping.
1334138568Ssam * Note that we cannot hold the node lock while sending a
1335138568Ssam * frame as this would lead to a LOR.  Instead we use a
1336138568Ssam * generation number to mark nodes that we've scanned and
1337138568Ssam * drop the lock and restart a scan if we have to time out
1338138568Ssam * a node.  Since we are single-threaded by virtue of
1339120483Ssam * controlling the inactivity timer we can be sure this will
1340120483Ssam * process each node only once.
1341120483Ssam */
1342138568Ssamstatic void
1343138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt)
1344116742Ssam{
1345138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1346120483Ssam	struct ieee80211_node *ni;
1347138568Ssam	u_int gen;
1348116742Ssam
1349138568Ssam	IEEE80211_SCAN_LOCK(nt);
1350138568Ssam	gen = nt->nt_scangen++;
1351138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1352138568Ssam		"%s: sta scangen %u\n", __func__, gen);
1353120483Ssamrestart:
1354138568Ssam	IEEE80211_NODE_LOCK(nt);
1355138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1356120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1357120483Ssam			continue;
1358120483Ssam		ni->ni_scangen = gen;
1359138568Ssam		/*
1360138568Ssam		 * Free fragment if not needed anymore
1361138568Ssam		 * (last fragment older than 1s).
1362138568Ssam		 * XXX doesn't belong here
1363138568Ssam		 */
1364138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1365138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1366138568Ssam			m_freem(ni->ni_rxfrag[0]);
1367138568Ssam			ni->ni_rxfrag[0] = NULL;
1368138568Ssam		}
1369138568Ssam		ni->ni_inact--;
1370138568Ssam		if (ni->ni_associd != 0) {
1371119150Ssam			/*
1372138568Ssam			 * Age frames on the power save queue. The
1373138568Ssam			 * aging interval is 4 times the listen
1374138568Ssam			 * interval specified by the station.  This
1375138568Ssam			 * number is factored into the age calculations
1376138568Ssam			 * when the frame is placed on the queue.  We
1377138568Ssam			 * store ages as time differences we can check
1378138568Ssam			 * and/or adjust only the head of the list.
1379138568Ssam			 */
1380138568Ssam			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1381138568Ssam				struct mbuf *m;
1382138568Ssam				int discard = 0;
1383138568Ssam
1384138568Ssam				IEEE80211_NODE_SAVEQ_LOCK(ni);
1385138568Ssam				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1386138568Ssam				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1387138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1388138568Ssam					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1389138568Ssam					m_freem(m);
1390138568Ssam					discard++;
1391138568Ssam				}
1392138568Ssam				if (m != NULL)
1393138568Ssam					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1394138568Ssam				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1395138568Ssam
1396138568Ssam				if (discard != 0) {
1397138568Ssam					IEEE80211_DPRINTF(ic,
1398138568Ssam					    IEEE80211_MSG_POWER,
1399138568Ssam					    "[%s] discard %u frames for age\n",
1400138568Ssam					    ether_sprintf(ni->ni_macaddr),
1401138568Ssam					    discard);
1402138568Ssam					IEEE80211_NODE_STAT_ADD(ni,
1403138568Ssam						ps_discard, discard);
1404138568Ssam					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1405138568Ssam						ic->ic_set_tim(ic, ni, 0);
1406138568Ssam				}
1407138568Ssam			}
1408138568Ssam			/*
1409138568Ssam			 * Probe the station before time it out.  We
1410138568Ssam			 * send a null data frame which may not be
1411138568Ssam			 * universally supported by drivers (need it
1412138568Ssam			 * for ps-poll support so it should be...).
1413138568Ssam			 */
1414138568Ssam			if (ni->ni_inact == ic->ic_inact_probe) {
1415138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1416138568Ssam				    "[%s] probe station due to inactivity\n",
1417138568Ssam				    ether_sprintf(ni->ni_macaddr));
1418138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1419138568Ssam				ieee80211_send_nulldata(ic, ni);
1420138568Ssam				/* XXX stat? */
1421138568Ssam				goto restart;
1422138568Ssam			}
1423138568Ssam		}
1424138568Ssam		if (ni->ni_inact <= 0) {
1425138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1426138568Ssam			    "[%s] station timed out due to inactivity "
1427138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1428138568Ssam			    ieee80211_node_refcnt(ni));
1429138568Ssam			/*
1430138568Ssam			 * Send a deauthenticate frame and drop the station.
1431138568Ssam			 * This is somewhat complicated due to reference counts
1432138568Ssam			 * and locking.  At this point a station will typically
1433138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1434138568Ssam			 * will do a "free" of the node which will drop the
1435138568Ssam			 * reference count.  But in the meantime a reference
1436138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1437138568Ssam			 * of the node will happen either after the tx is
1438138568Ssam			 * completed or by ieee80211_node_leave.
1439120483Ssam			 *
1440138568Ssam			 * Separately we must drop the node lock before sending
1441138568Ssam			 * in case the driver takes a lock, as this will result
1442138568Ssam			 * in  LOR between the node lock and the driver lock.
1443119150Ssam			 */
1444138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1445138568Ssam			if (ni->ni_associd != 0) {
1446138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
1447138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1448138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1449138568Ssam			}
1450138568Ssam			ieee80211_node_leave(ic, ni);
1451121180Ssam			ic->ic_stats.is_node_timeout++;
1452120483Ssam			goto restart;
1453120483Ssam		}
1454116742Ssam	}
1455138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1456138568Ssam
1457138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1458138568Ssam
1459138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1460116742Ssam}
1461116742Ssam
1462116742Ssamvoid
1463138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1464116742Ssam{
1465116742Ssam	struct ieee80211_node *ni;
1466138568Ssam	u_int gen;
1467116742Ssam
1468138568Ssam	IEEE80211_SCAN_LOCK(nt);
1469138568Ssam	gen = nt->nt_scangen++;
1470138568Ssam
1471138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1472138568Ssam		"%s: sta scangen %u\n", __func__, gen);
1473138568Ssamrestart:
1474138568Ssam	IEEE80211_NODE_LOCK(nt);
1475138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1476138568Ssam		if (ni->ni_scangen != gen) {
1477138568Ssam			ni->ni_scangen = gen;
1478138568Ssam			(void) ieee80211_ref_node(ni);
1479138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1480138568Ssam			(*f)(arg, ni);
1481138568Ssam			ieee80211_free_node(ni);
1482138568Ssam			goto restart;
1483138568Ssam		}
1484138568Ssam	}
1485138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1486138568Ssam
1487138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1488116742Ssam}
1489138568Ssam
1490138568Ssamvoid
1491138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1492138568Ssam{
1493138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
1494138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1495138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
1496138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1497138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
1498138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1499138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1500138568Ssam		ni->ni_txseqs[0],
1501138568Ssam		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1502138568Ssam		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1503138568Ssam		ni->ni_rxfragstamp);
1504138568Ssam	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
1505138568Ssam		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
1506138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1507138568Ssam		ether_sprintf(ni->ni_bssid),
1508138568Ssam		ni->ni_esslen, ni->ni_essid,
1509138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1510138568Ssam	printf("\tfails %u inact %u txrate %u\n",
1511138568Ssam		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1512138568Ssam}
1513138568Ssam
1514138568Ssamvoid
1515138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
1516138568Ssam{
1517138568Ssam	ieee80211_iterate_nodes(nt,
1518138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
1519138568Ssam}
1520138568Ssam
1521138568Ssam/*
1522138568Ssam * Handle a station joining an 11g network.
1523138568Ssam */
1524138568Ssamstatic void
1525138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1526138568Ssam{
1527138568Ssam
1528138568Ssam	/*
1529138568Ssam	 * Station isn't capable of short slot time.  Bump
1530138568Ssam	 * the count of long slot time stations and disable
1531138568Ssam	 * use of short slot time.  Note that the actual switch
1532138568Ssam	 * over to long slot time use may not occur until the
1533138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1534138568Ssam	 */
1535138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1536138568Ssam		ic->ic_longslotsta++;
1537138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1538138568Ssam		    "[%s] station needs long slot time, count %d\n",
1539138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1540138568Ssam		/* XXX vap's w/ conflicting needs won't work */
1541138568Ssam		ieee80211_set_shortslottime(ic, 0);
1542138568Ssam	}
1543138568Ssam	/*
1544138568Ssam	 * If the new station is not an ERP station
1545138568Ssam	 * then bump the counter and enable protection
1546138568Ssam	 * if configured.
1547138568Ssam	 */
1548138568Ssam	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1549138568Ssam		ic->ic_nonerpsta++;
1550138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1551138568Ssam		    "[%s] station is !ERP, %d non-ERP stations associated\n",
1552138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1553138568Ssam		/*
1554138568Ssam		 * If protection is configured, enable it.
1555138568Ssam		 */
1556138568Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
1557138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1558138568Ssam			    "%s: enable use of protection\n", __func__);
1559138568Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
1560138568Ssam		}
1561138568Ssam		/*
1562138568Ssam		 * If station does not support short preamble
1563138568Ssam		 * then we must enable use of Barker preamble.
1564138568Ssam		 */
1565138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1566138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1567138568Ssam			    "[%s] station needs long preamble\n",
1568138568Ssam			    ether_sprintf(ni->ni_macaddr));
1569138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
1570138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1571138568Ssam		}
1572138568Ssam	} else
1573138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
1574138568Ssam}
1575138568Ssam
1576138568Ssamvoid
1577138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1578138568Ssam{
1579138568Ssam	int newassoc;
1580138568Ssam
1581138568Ssam	if (ni->ni_associd == 0) {
1582138568Ssam		u_int16_t aid;
1583138568Ssam
1584138568Ssam		/*
1585138568Ssam		 * It would be good to search the bitmap
1586138568Ssam		 * more efficiently, but this will do for now.
1587138568Ssam		 */
1588138568Ssam		for (aid = 1; aid < ic->ic_max_aid; aid++) {
1589138568Ssam			if (!IEEE80211_AID_ISSET(aid,
1590138568Ssam			    ic->ic_aid_bitmap))
1591138568Ssam				break;
1592138568Ssam		}
1593138568Ssam		if (aid >= ic->ic_max_aid) {
1594138568Ssam			IEEE80211_SEND_MGMT(ic, ni, resp,
1595138568Ssam			    IEEE80211_REASON_ASSOC_TOOMANY);
1596138568Ssam			ieee80211_node_leave(ic, ni);
1597138568Ssam			return;
1598138568Ssam		}
1599138568Ssam		ni->ni_associd = aid | 0xc000;
1600138568Ssam		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1601138568Ssam		ic->ic_sta_assoc++;
1602138568Ssam		newassoc = 1;
1603139522Ssam		if (ic->ic_curmode == IEEE80211_MODE_11G ||
1604139522Ssam		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1605138568Ssam			ieee80211_node_join_11g(ic, ni);
1606138568Ssam	} else
1607138568Ssam		newassoc = 0;
1608138568Ssam
1609138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1610138568Ssam	    "[%s] station %s associated at aid %d\n",
1611138568Ssam	    ether_sprintf(ni->ni_macaddr), newassoc ? "newly" : "already",
1612138568Ssam	    IEEE80211_NODE_AID(ni));
1613138568Ssam
1614138568Ssam	/* give driver a chance to setup state like ni_txrate */
1615138568Ssam	if (ic->ic_newassoc)
1616138568Ssam		ic->ic_newassoc(ic, ni, newassoc);
1617138568Ssam	ni->ni_inact_reload = ic->ic_inact_run;
1618138568Ssam	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1619138568Ssam	/* tell the authenticator about new station */
1620138568Ssam	if (ic->ic_auth->ia_node_join != NULL)
1621138568Ssam		ic->ic_auth->ia_node_join(ic, ni);
1622138568Ssam	ieee80211_notify_node_join(ic, ni, newassoc);
1623138568Ssam}
1624138568Ssam
1625138568Ssam/*
1626138568Ssam * Handle a station leaving an 11g network.
1627138568Ssam */
1628138568Ssamstatic void
1629138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1630138568Ssam{
1631138568Ssam
1632139522Ssam	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
1633139522Ssam		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
1634138568Ssam		("not in 11g, curmode %x", ic->ic_curmode));
1635138568Ssam
1636138568Ssam	/*
1637138568Ssam	 * If a long slot station do the slot time bookkeeping.
1638138568Ssam	 */
1639138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1640138568Ssam		KASSERT(ic->ic_longslotsta > 0,
1641138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
1642138568Ssam		ic->ic_longslotsta--;
1643138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1644138568Ssam		    "[%s] long slot time station leaves, count now %d\n",
1645138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1646138568Ssam		if (ic->ic_longslotsta == 0) {
1647138568Ssam			/*
1648138568Ssam			 * Re-enable use of short slot time if supported
1649138568Ssam			 * and not operating in IBSS mode (per spec).
1650138568Ssam			 */
1651138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1652138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
1653138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1654138568Ssam				    "%s: re-enable use of short slot time\n",
1655138568Ssam				    __func__);
1656138568Ssam				ieee80211_set_shortslottime(ic, 1);
1657138568Ssam			}
1658138568Ssam		}
1659138568Ssam	}
1660138568Ssam	/*
1661138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
1662138568Ssam	 */
1663138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1664138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
1665138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1666138568Ssam		ic->ic_nonerpsta--;
1667138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1668138568Ssam		    "[%s] non-ERP station leaves, count now %d\n",
1669138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1670138568Ssam		if (ic->ic_nonerpsta == 0) {
1671138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1672138568Ssam				"%s: disable use of protection\n", __func__);
1673138568Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1674138568Ssam			/* XXX verify mode? */
1675138568Ssam			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1676138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1677138568Ssam				    "%s: re-enable use of short preamble\n",
1678138568Ssam				    __func__);
1679138568Ssam				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1680138568Ssam				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1681138568Ssam			}
1682138568Ssam		}
1683138568Ssam	}
1684138568Ssam}
1685138568Ssam
1686138568Ssam/*
1687138568Ssam * Handle bookkeeping for station deauthentication/disassociation
1688138568Ssam * when operating as an ap.
1689138568Ssam */
1690138568Ssamvoid
1691138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1692138568Ssam{
1693138568Ssam
1694138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1695138568Ssam	    "[%s] station with aid %d leaves\n",
1696138568Ssam	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
1697138568Ssam
1698138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1699138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS ||
1700138568Ssam		ic->ic_opmode == IEEE80211_M_AHDEMO,
1701138568Ssam		("unexpected operating mode %u", ic->ic_opmode));
1702138568Ssam	/*
1703138568Ssam	 * If node wasn't previously associated all
1704138568Ssam	 * we need to do is reclaim the reference.
1705138568Ssam	 */
1706138568Ssam	/* XXX ibss mode bypasses 11g and notification */
1707138568Ssam	if (ni->ni_associd == 0)
1708138568Ssam		goto done;
1709138568Ssam	/*
1710138568Ssam	 * Tell the authenticator the station is leaving.
1711138568Ssam	 * Note that we must do this before yanking the
1712138568Ssam	 * association id as the authenticator uses the
1713138568Ssam	 * associd to locate it's state block.
1714138568Ssam	 */
1715138568Ssam	if (ic->ic_auth->ia_node_leave != NULL)
1716138568Ssam		ic->ic_auth->ia_node_leave(ic, ni);
1717138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1718138568Ssam	ni->ni_associd = 0;
1719138568Ssam	ic->ic_sta_assoc--;
1720138568Ssam
1721139522Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G ||
1722139522Ssam	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1723138568Ssam		ieee80211_node_leave_11g(ic, ni);
1724138568Ssam	/*
1725138568Ssam	 * Cleanup station state.  In particular clear various
1726138568Ssam	 * state that might otherwise be reused if the node
1727138568Ssam	 * is reused before the reference count goes to zero
1728138568Ssam	 * (and memory is reclaimed).
1729138568Ssam	 */
1730138568Ssam	ieee80211_sta_leave(ic, ni);
1731138568Ssamdone:
1732138568Ssam	ni->ni_inact_reload = ic->ic_inact_init;	/* just in case */
1733138568Ssam	ieee80211_free_node(ni);
1734138568Ssam}
1735138568Ssam
1736138568Ssamu_int8_t
1737138568Ssamieee80211_getrssi(struct ieee80211com *ic)
1738138568Ssam{
1739138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
1740138568Ssam	struct ieee80211_node_table *nt = ic->ic_sta;
1741138568Ssam	u_int32_t rssi_samples, rssi_total;
1742138568Ssam	struct ieee80211_node *ni;
1743138568Ssam
1744138568Ssam	rssi_total = 0;
1745138568Ssam	rssi_samples = 0;
1746138568Ssam	switch (ic->ic_opmode) {
1747138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
1748138568Ssam		nt = ic->ic_sta;
1749138568Ssam		if (nt == NULL)
1750138568Ssam			break;
1751138568Ssam		/* XXX locking */
1752138568Ssam		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list)
1753138568Ssam			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
1754138568Ssam				rssi_samples++;
1755138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
1756138568Ssam			}
1757138568Ssam		break;
1758138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
1759138568Ssam		nt = ic->ic_sta;
1760138568Ssam		if (nt == NULL)
1761138568Ssam			break;
1762138568Ssam		/* XXX locking */
1763138568Ssam		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list) {
1764138568Ssam			rssi_samples++;
1765138568Ssam			rssi_total += ic->ic_node_getrssi(ni);
1766138568Ssam		}
1767138568Ssam		break;
1768138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
1769138568Ssam		nt = ic->ic_sta;
1770138568Ssam		if (nt == NULL)
1771138568Ssam			break;
1772138568Ssam		/* XXX locking */
1773138568Ssam		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list)
1774138568Ssam			if (IEEE80211_AID(ni->ni_associd) != 0) {
1775138568Ssam				rssi_samples++;
1776138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
1777138568Ssam			}
1778138568Ssam		break;
1779138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
1780138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
1781138568Ssam	default:
1782138568Ssam		if (ic->ic_bss != NULL)
1783138568Ssam			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1784138568Ssam		rssi_samples = 1;
1785138568Ssam		break;
1786138568Ssam	}
1787138568Ssam	return rssi_total / NZ(rssi_samples);
1788138568Ssam#undef NZ
1789138568Ssam}
1790138568Ssam
1791138568Ssam/*
1792138568Ssam * Indicate whether there are frames queued for a station in power-save mode.
1793138568Ssam */
1794138568Ssamstatic void
1795138568Ssamieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
1796138568Ssam{
1797138568Ssam	u_int16_t aid;
1798138568Ssam
1799138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1800138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS,
1801138568Ssam		("operating mode %u", ic->ic_opmode));
1802138568Ssam
1803138568Ssam	aid = IEEE80211_AID(ni->ni_associd);
1804138568Ssam	KASSERT(aid < ic->ic_max_aid,
1805138568Ssam		("bogus aid %u, max %u", aid, ic->ic_max_aid));
1806138568Ssam
1807138568Ssam	IEEE80211_BEACON_LOCK(ic);
1808138568Ssam	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
1809138568Ssam		if (set) {
1810138568Ssam			setbit(ic->ic_tim_bitmap, aid);
1811138568Ssam			ic->ic_ps_pending++;
1812138568Ssam		} else {
1813138568Ssam			clrbit(ic->ic_tim_bitmap, aid);
1814138568Ssam			ic->ic_ps_pending--;
1815138568Ssam		}
1816138568Ssam		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
1817138568Ssam	}
1818138568Ssam	IEEE80211_BEACON_UNLOCK(ic);
1819138568Ssam}
1820138568Ssam
1821138568Ssam/*
1822138568Ssam * Node table support.
1823138568Ssam */
1824138568Ssam
1825138568Ssamstatic void
1826138568Ssamieee80211_node_table_init(struct ieee80211com *ic,
1827138568Ssam	struct ieee80211_node_table *nt,
1828138568Ssam	const char *name, int inact,
1829138568Ssam	void (*timeout)(struct ieee80211_node_table *))
1830138568Ssam{
1831138568Ssam
1832138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1833138568Ssam		"%s %s table, inact %u\n", __func__, name, inact);
1834138568Ssam
1835138568Ssam	nt->nt_ic = ic;
1836138568Ssam	/* XXX need unit */
1837138568Ssam	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1838138568Ssam	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1839138568Ssam	TAILQ_INIT(&nt->nt_node);
1840138568Ssam	nt->nt_name = name;
1841138568Ssam	nt->nt_scangen = 1;
1842138568Ssam	nt->nt_inact_init = inact;
1843138568Ssam	nt->nt_timeout = timeout;
1844138568Ssam}
1845138568Ssam
1846138568Ssamstatic struct ieee80211_node_table *
1847138568Ssamieee80211_node_table_alloc(struct ieee80211com *ic,
1848138568Ssam	const char *name, int inact,
1849138568Ssam	void (*timeout)(struct ieee80211_node_table *))
1850138568Ssam{
1851138568Ssam	struct ieee80211_node_table *nt;
1852138568Ssam
1853138568Ssam	MALLOC(nt, struct ieee80211_node_table *,
1854138568Ssam		sizeof(struct ieee80211_node_table),
1855138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
1856138568Ssam	if (nt == NULL) {
1857138568Ssam		printf("%s: no memory node table!\n", __func__);
1858138568Ssam		return NULL;
1859138568Ssam	}
1860138568Ssam	ieee80211_node_table_init(ic, nt, name, inact, timeout);
1861138568Ssam	return nt;
1862138568Ssam}
1863138568Ssam
1864138568Ssamvoid
1865138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt)
1866138568Ssam{
1867138568Ssam
1868138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1869138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1870138568Ssam
1871138568Ssam	IEEE80211_NODE_LOCK(nt);
1872138568Ssam	nt->nt_inact_timer = 0;
1873138568Ssam	ieee80211_free_allnodes_locked(nt);
1874138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1875138568Ssam}
1876138568Ssam
1877138568Ssamstatic void
1878138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1879138568Ssam{
1880138568Ssam
1881138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1882138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1883138568Ssam
1884138568Ssam	ieee80211_free_allnodes_locked(nt);
1885138568Ssam	IEEE80211_SCAN_LOCK_DESTROY(nt);
1886138568Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
1887138568Ssam}
1888138568Ssam
1889138568Ssam/*
1890138568Ssam * NB: public for use in ieee80211_proto.c
1891138568Ssam */
1892138568Ssamvoid
1893138568Ssamieee80211_node_table_free(struct ieee80211_node_table *nt)
1894138568Ssam{
1895138568Ssam
1896138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1897138568Ssam		"%s %s table\n", __func__, nt->nt_name);
1898138568Ssam
1899138568Ssam	IEEE80211_NODE_LOCK(nt);
1900138568Ssam	nt->nt_inact_timer = 0;
1901138568Ssam	ieee80211_node_table_cleanup(nt);
1902138568Ssam	FREE(nt, M_DEVBUF);
1903138568Ssam}
1904