ieee80211_node.c revision 165569
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 165569 2006-12-27 18:46:18Z sam $");
35116742Ssam
36116742Ssam#include <sys/param.h>
37116742Ssam#include <sys/systm.h>
38116742Ssam#include <sys/mbuf.h>
39116742Ssam#include <sys/malloc.h>
40116742Ssam#include <sys/kernel.h>
41138568Ssam
42116742Ssam#include <sys/socket.h>
43116742Ssam
44116742Ssam#include <net/if.h>
45116742Ssam#include <net/if_media.h>
46116742Ssam#include <net/ethernet.h>
47116742Ssam
48116742Ssam#include <net80211/ieee80211_var.h>
49116742Ssam
50116742Ssam#include <net/bpf.h>
51116742Ssam
52147221Ssam/*
53147221Ssam * Association id's are managed with a bit vector.
54147221Ssam */
55147221Ssam#define	IEEE80211_AID_SET(b, w) \
56147221Ssam	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
57147221Ssam#define	IEEE80211_AID_CLR(b, w) \
58147221Ssam	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
59147221Ssam#define	IEEE80211_AID_ISSET(b, w) \
60147221Ssam	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
61147221Ssam
62159139Sdds#ifdef IEEE80211_DEBUG_REFCNT
63159139Sdds#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
64159139Sdds#else
65159139Sdds#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
66159139Sdds#endif
67159139Sdds
68138568Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
69138568Ssamstatic void node_cleanup(struct ieee80211_node *);
70138568Ssamstatic void node_free(struct ieee80211_node *);
71138568Ssamstatic u_int8_t node_getrssi(const struct ieee80211_node *);
72116742Ssam
73138568Ssamstatic void ieee80211_setup_node(struct ieee80211_node_table *,
74138568Ssam		struct ieee80211_node *, const u_int8_t *);
75138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
76138568Ssamstatic void ieee80211_free_allnodes(struct ieee80211_node_table *);
77120104Ssam
78138568Ssamstatic void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
79138568Ssamstatic void ieee80211_timeout_stations(struct ieee80211_node_table *);
80116742Ssam
81148304Ssamstatic void ieee80211_set_tim(struct ieee80211_node *, int set);
82138568Ssam
83138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
84148863Ssam	struct ieee80211_node_table *nt, const char *name,
85148863Ssam	int inact, int keyixmax,
86138568Ssam	void (*timeout)(struct ieee80211_node_table *));
87138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
88138568Ssam
89127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
90120481Ssam
91116742Ssamvoid
92138568Ssamieee80211_node_attach(struct ieee80211com *ic)
93116742Ssam{
94116742Ssam
95138568Ssam	ic->ic_node_alloc = node_alloc;
96138568Ssam	ic->ic_node_free = node_free;
97138568Ssam	ic->ic_node_cleanup = node_cleanup;
98138568Ssam	ic->ic_node_getrssi = node_getrssi;
99138568Ssam
100138568Ssam	/* default station inactivity timer setings */
101138568Ssam	ic->ic_inact_init = IEEE80211_INACT_INIT;
102138568Ssam	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
103138568Ssam	ic->ic_inact_run = IEEE80211_INACT_RUN;
104138568Ssam	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
105138568Ssam
106148863Ssam	/* NB: driver should override */
107148863Ssam	ic->ic_max_aid = IEEE80211_AID_DEF;
108148863Ssam	ic->ic_set_tim = ieee80211_set_tim;
109148863Ssam}
110148863Ssam
111148863Ssamvoid
112148863Ssamieee80211_node_lateattach(struct ieee80211com *ic)
113148863Ssam{
114148863Ssam	struct ieee80211_rsnparms *rsn;
115148863Ssam
116148863Ssam	if (ic->ic_max_aid > IEEE80211_AID_MAX)
117138568Ssam		ic->ic_max_aid = IEEE80211_AID_MAX;
118138568Ssam	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
119138568Ssam		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
120138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
121138568Ssam	if (ic->ic_aid_bitmap == NULL) {
122138568Ssam		/* XXX no way to recover */
123138568Ssam		printf("%s: no memory for AID bitmap!\n", __func__);
124138568Ssam		ic->ic_max_aid = 0;
125138568Ssam	}
126138568Ssam
127138568Ssam	/* XXX defer until using hostap/ibss mode */
128138568Ssam	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
129138568Ssam	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
130138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
131138568Ssam	if (ic->ic_tim_bitmap == NULL) {
132138568Ssam		/* XXX no way to recover */
133138568Ssam		printf("%s: no memory for TIM bitmap!\n", __func__);
134138568Ssam	}
135118887Ssam
136148863Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
137148863Ssam		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
138148863Ssam		ieee80211_timeout_stations);
139148863Ssam	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
140148863Ssam		IEEE80211_INACT_SCAN, 0,
141148863Ssam		ieee80211_timeout_scan_candidates);
142118887Ssam
143148863Ssam	ieee80211_reset_bss(ic);
144138568Ssam	/*
145138568Ssam	 * Setup "global settings" in the bss node so that
146138568Ssam	 * each new station automatically inherits them.
147138568Ssam	 */
148148863Ssam	rsn = &ic->ic_bss->ni_rsn;
149138568Ssam	/* WEP, TKIP, and AES-CCM are always supported */
150138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
151138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
152138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
153138568Ssam	if (ic->ic_caps & IEEE80211_C_AES)
154138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
155138568Ssam	if (ic->ic_caps & IEEE80211_C_CKIP)
156138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
157138568Ssam	/*
158138568Ssam	 * Default unicast cipher to WEP for 802.1x use.  If
159138568Ssam	 * WPA is enabled the management code will set these
160138568Ssam	 * values to reflect.
161138568Ssam	 */
162138568Ssam	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
163138568Ssam	rsn->rsn_ucastkeylen = 104 / NBBY;
164138568Ssam	/*
165138568Ssam	 * WPA says the multicast cipher is the lowest unicast
166138568Ssam	 * cipher supported.  But we skip WEP which would
167138568Ssam	 * otherwise be used based on this criteria.
168138568Ssam	 */
169138568Ssam	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
170138568Ssam	rsn->rsn_mcastkeylen = 128 / NBBY;
171138568Ssam
172138568Ssam	/*
173138568Ssam	 * We support both WPA-PSK and 802.1x; the one used
174138568Ssam	 * is determined by the authentication mode and the
175138568Ssam	 * setting of the PSK state.
176138568Ssam	 */
177138568Ssam	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
178138568Ssam	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
179138568Ssam
180148863Ssam	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
181116742Ssam}
182116742Ssam
183116742Ssamvoid
184138568Ssamieee80211_node_detach(struct ieee80211com *ic)
185116742Ssam{
186116742Ssam
187138568Ssam	if (ic->ic_bss != NULL) {
188138568Ssam		ieee80211_free_node(ic->ic_bss);
189138568Ssam		ic->ic_bss = NULL;
190138568Ssam	}
191138568Ssam	ieee80211_node_table_cleanup(&ic->ic_scan);
192140753Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
193138568Ssam	if (ic->ic_aid_bitmap != NULL) {
194138568Ssam		FREE(ic->ic_aid_bitmap, M_DEVBUF);
195138568Ssam		ic->ic_aid_bitmap = NULL;
196138568Ssam	}
197138568Ssam	if (ic->ic_tim_bitmap != NULL) {
198138568Ssam		FREE(ic->ic_tim_bitmap, M_DEVBUF);
199138568Ssam		ic->ic_tim_bitmap = NULL;
200138568Ssam	}
201116742Ssam}
202116742Ssam
203138568Ssam/*
204138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
205138568Ssam */
206138568Ssam
207138568Ssamvoid
208148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
209138568Ssam{
210148302Ssam	struct ieee80211com *ic = ni->ni_ic;
211148302Ssam
212138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
213139528Ssam	ni->ni_inact_reload = ic->ic_inact_run;
214138568Ssam}
215138568Ssam
216138568Ssamvoid
217148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
218138568Ssam{
219138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
220138568Ssam}
221138568Ssam
222116742Ssam/*
223138568Ssam * Set/change the channel.  The rate set is also updated as
224138568Ssam * to insure a consistent view by drivers.
225138568Ssam */
226153351Ssamstatic void
227138568Ssamieee80211_set_chan(struct ieee80211com *ic,
228138568Ssam	struct ieee80211_node *ni, struct ieee80211_channel *chan)
229138568Ssam{
230153351Ssam	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
231153351Ssam		chan = ic->ic_curchan;
232138568Ssam	ni->ni_chan = chan;
233165569Ssam	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
234138568Ssam}
235138568Ssam
236138568Ssam/*
237116742Ssam * AP scanning support.
238116742Ssam */
239116742Ssam
240138568Ssam#ifdef IEEE80211_DEBUG
241138568Ssamstatic void
242138568Ssamdump_chanlist(const u_char chans[])
243138568Ssam{
244138568Ssam	const char *sep;
245138568Ssam	int i;
246138568Ssam
247138568Ssam	sep = " ";
248138568Ssam	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
249138568Ssam		if (isset(chans, i)) {
250138568Ssam			printf("%s%u", sep, i);
251138568Ssam			sep = ", ";
252138568Ssam		}
253138568Ssam}
254138568Ssam#endif /* IEEE80211_DEBUG */
255138568Ssam
256116742Ssam/*
257138568Ssam * Initialize the channel set to scan based on the
258116742Ssam * of available channels and the current PHY mode.
259116742Ssam */
260117811Ssamstatic void
261138568Ssamieee80211_reset_scan(struct ieee80211com *ic)
262116742Ssam{
263116742Ssam
264138568Ssam	/* XXX ic_des_chan should be handled with ic_chan_active */
265138568Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
266138568Ssam		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
267138568Ssam		setbit(ic->ic_chan_scan,
268138568Ssam			ieee80211_chan2ieee(ic, ic->ic_des_chan));
269138568Ssam	} else
270138568Ssam		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
271138568Ssam			sizeof(ic->ic_chan_active));
272138568Ssam#ifdef IEEE80211_DEBUG
273138568Ssam	if (ieee80211_msg_scan(ic)) {
274138568Ssam		printf("%s: scan set:", __func__);
275138568Ssam		dump_chanlist(ic->ic_chan_scan);
276138568Ssam		printf(" start chan %u\n",
277148936Ssam			ieee80211_chan2ieee(ic, ic->ic_curchan));
278138568Ssam	}
279138568Ssam#endif /* IEEE80211_DEBUG */
280116742Ssam}
281116742Ssam
282116742Ssam/*
283116742Ssam * Begin an active scan.
284116742Ssam */
285116742Ssamvoid
286138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset)
287116742Ssam{
288116742Ssam
289138568Ssam	ic->ic_scan.nt_scangen++;
290117811Ssam	/*
291117811Ssam	 * In all but hostap mode scanning starts off in
292117811Ssam	 * an active mode before switching to passive.
293117811Ssam	 */
294121180Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
295117811Ssam		ic->ic_flags |= IEEE80211_F_ASCAN;
296121180Ssam		ic->ic_stats.is_scan_active++;
297121180Ssam	} else
298121180Ssam		ic->ic_stats.is_scan_passive++;
299139520Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
300139520Ssam		"begin %s scan in %s mode, scangen %u\n",
301138568Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
302139520Ssam		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
303116742Ssam	/*
304138568Ssam	 * Clear scan state and flush any previously seen AP's.
305116742Ssam	 */
306138568Ssam	ieee80211_reset_scan(ic);
307138568Ssam	if (reset)
308138568Ssam		ieee80211_free_allnodes(&ic->ic_scan);
309116742Ssam
310138568Ssam	ic->ic_flags |= IEEE80211_F_SCAN;
311138568Ssam
312117811Ssam	/* Scan the next channel. */
313138568Ssam	ieee80211_next_scan(ic);
314116742Ssam}
315116742Ssam
316116742Ssam/*
317116742Ssam * Switch to the next channel marked for scanning.
318116742Ssam */
319138568Ssamint
320138568Ssamieee80211_next_scan(struct ieee80211com *ic)
321116742Ssam{
322116742Ssam	struct ieee80211_channel *chan;
323116742Ssam
324138568Ssam	/*
325138568Ssam	 * Insure any previous mgt frame timeouts don't fire.
326138568Ssam	 * This assumes the driver does the right thing in
327138568Ssam	 * flushing anything queued in the driver and below.
328138568Ssam	 */
329138568Ssam	ic->ic_mgt_timer = 0;
330156358Ssam	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
331138568Ssam
332148936Ssam	chan = ic->ic_curchan;
333138568Ssam	do {
334116742Ssam		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
335116742Ssam			chan = &ic->ic_channels[0];
336116742Ssam		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
337138568Ssam			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
338138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
339138568Ssam			    "%s: chan %d->%d\n", __func__,
340148936Ssam			    ieee80211_chan2ieee(ic, ic->ic_curchan),
341138568Ssam			    ieee80211_chan2ieee(ic, chan));
342148936Ssam			ic->ic_curchan = chan;
343148936Ssam			/*
344148936Ssam			 * XXX drivers should do this as needed,
345148936Ssam			 * XXX for now maintain compatibility
346148936Ssam			 */
347165569Ssam			ic->ic_bss->ni_rates = *ieee80211_get_suprates(ic, chan);
348138568Ssam			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
349138568Ssam			return 1;
350116742Ssam		}
351148936Ssam	} while (chan != ic->ic_curchan);
352138568Ssam	ieee80211_end_scan(ic);
353138568Ssam	return 0;
354116742Ssam}
355116742Ssam
356156358Ssam/*
357156358Ssam * Probe the curent channel, if allowed, while scanning.
358156358Ssam * If the channel is not marked passive-only then send
359156358Ssam * a probe request immediately.  Otherwise mark state and
360156358Ssam * listen for beacons on the channel; if we receive something
361156358Ssam * then we'll transmit a probe request.
362156358Ssam */
363156358Ssamvoid
364156358Ssamieee80211_probe_curchan(struct ieee80211com *ic, int force)
365156358Ssam{
366156358Ssam	struct ifnet *ifp = ic->ic_ifp;
367156358Ssam
368156358Ssam	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
369156358Ssam		/*
370156358Ssam		 * XXX send both broadcast+directed probe request
371156358Ssam		 */
372156358Ssam		ieee80211_send_probereq(ic->ic_bss,
373156358Ssam			ic->ic_myaddr, ifp->if_broadcastaddr,
374156358Ssam			ifp->if_broadcastaddr,
375156358Ssam			ic->ic_des_essid, ic->ic_des_esslen,
376156358Ssam			ic->ic_opt_ie, ic->ic_opt_ie_len);
377156358Ssam	} else
378156358Ssam		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
379156358Ssam}
380156358Ssam
381141658Ssamstatic __inline void
382141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
383141658Ssam{
384141658Ssam	/* propagate useful state */
385141658Ssam	nbss->ni_authmode = obss->ni_authmode;
386141658Ssam	nbss->ni_txpower = obss->ni_txpower;
387141658Ssam	nbss->ni_vlan = obss->ni_vlan;
388141658Ssam	nbss->ni_rsn = obss->ni_rsn;
389141658Ssam	/* XXX statistics? */
390141658Ssam}
391141658Ssam
392116742Ssamvoid
393116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
394116742Ssam{
395140753Ssam	struct ieee80211_node_table *nt;
396116742Ssam	struct ieee80211_node *ni;
397116742Ssam
398138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
399138568Ssam		"%s: creating ibss\n", __func__);
400138568Ssam
401138568Ssam	/*
402138568Ssam	 * Create the station/neighbor table.  Note that for adhoc
403138568Ssam	 * mode we make the initial inactivity timer longer since
404138568Ssam	 * we create nodes only through discovery and they typically
405138568Ssam	 * are long-lived associations.
406138568Ssam	 */
407140753Ssam	nt = &ic->ic_sta;
408140753Ssam	IEEE80211_NODE_LOCK(nt);
409140753Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
410140753Ssam		nt->nt_name = "station";
411140753Ssam		nt->nt_inact_init = ic->ic_inact_init;
412140753Ssam	} else {
413140753Ssam		nt->nt_name = "neighbor";
414140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
415140753Ssam	}
416140753Ssam	IEEE80211_NODE_UNLOCK(nt);
417140753Ssam
418148863Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
419140753Ssam	if (ni == NULL) {
420140753Ssam		/* XXX recovery? */
421138568Ssam		return;
422138568Ssam	}
423116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
424116742Ssam	ni->ni_esslen = ic->ic_des_esslen;
425116742Ssam	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
426141658Ssam	copy_bss(ni, ic->ic_bss);
427148843Ssam	ni->ni_intval = ic->ic_bintval;
428138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
429116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
430116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
431116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
432116742Ssam		ni->ni_fhindex = 1;
433116742Ssam	}
434138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
435138568Ssam		ic->ic_flags |= IEEE80211_F_SIBSS;
436138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
437143300Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
438143300Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
439143300Ssam		else
440143300Ssam			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
441153403Ssam	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
442153403Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
443153403Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
444153403Ssam		else
445153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
446138568Ssam	}
447138568Ssam	/*
448138568Ssam	 * Fix the channel and related attributes.
449138568Ssam	 */
450138568Ssam	ieee80211_set_chan(ic, ni, chan);
451148936Ssam	ic->ic_curchan = chan;
452138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
453138568Ssam	/*
454138568Ssam	 * Do mode-specific rate setup.
455138568Ssam	 */
456138568Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G) {
457138568Ssam		/*
458138568Ssam		 * Use a mixed 11b/11g rate set.
459138568Ssam		 */
460138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
461138568Ssam	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
462138568Ssam		/*
463138568Ssam		 * Force pure 11b rate set.
464138568Ssam		 */
465138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
466138568Ssam	}
467138568Ssam
468140753Ssam	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
469116742Ssam}
470116742Ssam
471138568Ssamvoid
472138568Ssamieee80211_reset_bss(struct ieee80211com *ic)
473138568Ssam{
474138568Ssam	struct ieee80211_node *ni, *obss;
475138568Ssam
476138568Ssam	ieee80211_node_table_reset(&ic->ic_scan);
477140753Ssam	ieee80211_node_table_reset(&ic->ic_sta);
478140753Ssam
479138568Ssam	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
480138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
481138568Ssam	obss = ic->ic_bss;
482138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);
483141658Ssam	if (obss != NULL) {
484141658Ssam		copy_bss(ni, obss);
485148843Ssam		ni->ni_intval = ic->ic_bintval;
486138568Ssam		ieee80211_free_node(obss);
487141658Ssam	}
488138568Ssam}
489138568Ssam
490148432Ssam/* XXX tunable */
491148432Ssam#define	STA_FAILS_MAX	2		/* assoc failures before ignored */
492148432Ssam
493127767Ssamstatic int
494138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
495127767Ssam{
496127767Ssam        u_int8_t rate;
497127767Ssam        int fail;
498127767Ssam
499127767Ssam	fail = 0;
500127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
501127767Ssam		fail |= 0x01;
502127767Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
503127767Ssam	    ni->ni_chan != ic->ic_des_chan)
504127767Ssam		fail |= 0x01;
505127767Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
506127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
507127767Ssam			fail |= 0x02;
508127767Ssam	} else {
509127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
510127767Ssam			fail |= 0x02;
511127767Ssam	}
512138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
513127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
514127767Ssam			fail |= 0x04;
515127767Ssam	} else {
516127767Ssam		/* XXX does this mean privacy is supported or required? */
517127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
518127767Ssam			fail |= 0x04;
519127767Ssam	}
520148299Ssam	rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
521127767Ssam	if (rate & IEEE80211_RATE_BASIC)
522127767Ssam		fail |= 0x08;
523127767Ssam	if (ic->ic_des_esslen != 0 &&
524127767Ssam	    (ni->ni_esslen != ic->ic_des_esslen ||
525127767Ssam	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
526127767Ssam		fail |= 0x10;
527127767Ssam	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
528127767Ssam	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
529127767Ssam		fail |= 0x20;
530148432Ssam	if (ni->ni_fails >= STA_FAILS_MAX)
531148432Ssam		fail |= 0x40;
532127767Ssam#ifdef IEEE80211_DEBUG
533138568Ssam	if (ieee80211_msg_scan(ic)) {
534148432Ssam		printf(" %c %s",
535148432Ssam		    fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
536127767Ssam		    ether_sprintf(ni->ni_macaddr));
537127767Ssam		printf(" %s%c", ether_sprintf(ni->ni_bssid),
538127767Ssam		    fail & 0x20 ? '!' : ' ');
539127767Ssam		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
540127767Ssam			fail & 0x01 ? '!' : ' ');
541127767Ssam		printf(" %+4d", ni->ni_rssi);
542127767Ssam		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
543127767Ssam		    fail & 0x08 ? '!' : ' ');
544127767Ssam		printf(" %4s%c",
545127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
546127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
547127767Ssam		    "????",
548127767Ssam		    fail & 0x02 ? '!' : ' ');
549127767Ssam		printf(" %3s%c ",
550127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
551127767Ssam		    "wep" : "no",
552127767Ssam		    fail & 0x04 ? '!' : ' ');
553127767Ssam		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
554127767Ssam		printf("%s\n", fail & 0x10 ? "!" : "");
555127767Ssam	}
556127767Ssam#endif
557127767Ssam	return fail;
558127767Ssam}
559127767Ssam
560138568Ssamstatic __inline u_int8_t
561138568Ssammaxrate(const struct ieee80211_node *ni)
562138568Ssam{
563138568Ssam	const struct ieee80211_rateset *rs = &ni->ni_rates;
564138568Ssam	/* NB: assumes rate set is sorted (happens on frame receive) */
565138568Ssam	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
566138568Ssam}
567138568Ssam
568116742Ssam/*
569138568Ssam * Compare the capabilities of two nodes and decide which is
570138568Ssam * more desirable (return >0 if a is considered better).  Note
571138568Ssam * that we assume compatibility/usability has already been checked
572138568Ssam * so we don't need to (e.g. validate whether privacy is supported).
573138568Ssam * Used to select the best scan candidate for association in a BSS.
574138568Ssam */
575138568Ssamstatic int
576138568Ssamieee80211_node_compare(struct ieee80211com *ic,
577138568Ssam		       const struct ieee80211_node *a,
578138568Ssam		       const struct ieee80211_node *b)
579138568Ssam{
580138568Ssam	u_int8_t maxa, maxb;
581138568Ssam	u_int8_t rssia, rssib;
582148432Ssam	int weight;
583138568Ssam
584138568Ssam	/* privacy support preferred */
585138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
586138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
587138568Ssam		return 1;
588138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
589138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
590138568Ssam		return -1;
591138568Ssam
592148432Ssam	/* compare count of previous failures */
593148432Ssam	weight = b->ni_fails - a->ni_fails;
594148432Ssam	if (abs(weight) > 1)
595148432Ssam		return weight;
596148432Ssam
597138568Ssam	rssia = ic->ic_node_getrssi(a);
598138568Ssam	rssib = ic->ic_node_getrssi(b);
599139543Ssam	if (abs(rssib - rssia) < 5) {
600139543Ssam		/* best/max rate preferred if signal level close enough XXX */
601139543Ssam		maxa = maxrate(a);
602139543Ssam		maxb = maxrate(b);
603139543Ssam		if (maxa != maxb)
604139543Ssam			return maxa - maxb;
605139543Ssam		/* XXX use freq for channel preference */
606139543Ssam		/* for now just prefer 5Ghz band to all other bands */
607139543Ssam		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
608139543Ssam		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
609139543Ssam			return 1;
610139543Ssam		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
611139543Ssam		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
612139543Ssam			return -1;
613139543Ssam	}
614138568Ssam	/* all things being equal, use signal level */
615138568Ssam	return rssia - rssib;
616138568Ssam}
617138568Ssam
618138568Ssam/*
619140441Ssam * Mark an ongoing scan stopped.
620140441Ssam */
621140441Ssamvoid
622140441Ssamieee80211_cancel_scan(struct ieee80211com *ic)
623140441Ssam{
624140441Ssam
625140441Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
626140441Ssam		__func__,
627140441Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
628140441Ssam
629140441Ssam	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
630156358Ssam	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
631140441Ssam}
632140441Ssam
633140441Ssam/*
634116742Ssam * Complete a scan of potential channels.
635116742Ssam */
636116742Ssamvoid
637138568Ssamieee80211_end_scan(struct ieee80211com *ic)
638116742Ssam{
639140448Ssam	struct ieee80211_node_table *nt = &ic->ic_scan;
640140448Ssam	struct ieee80211_node *ni, *selbs;
641116742Ssam
642140441Ssam	ieee80211_cancel_scan(ic);
643140441Ssam	ieee80211_notify_scan_done(ic);
644116742Ssam
645116742Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
646138568Ssam		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
647138568Ssam		int i, bestchan;
648138568Ssam		u_int8_t rssi;
649138568Ssam
650116742Ssam		/*
651116742Ssam		 * The passive scan to look for existing AP's completed,
652116742Ssam		 * select a channel to camp on.  Identify the channels
653116742Ssam		 * that already have one or more AP's and try to locate
654140753Ssam		 * an unoccupied one.  If that fails, pick a channel that
655138568Ssam		 * looks to be quietest.
656116742Ssam		 */
657138568Ssam		memset(maxrssi, 0, sizeof(maxrssi));
658140448Ssam		IEEE80211_NODE_LOCK(nt);
659140448Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
660138568Ssam			rssi = ic->ic_node_getrssi(ni);
661138568Ssam			i = ieee80211_chan2ieee(ic, ni->ni_chan);
662138568Ssam			if (rssi > maxrssi[i])
663138568Ssam				maxrssi[i] = rssi;
664116742Ssam		}
665140448Ssam		IEEE80211_NODE_UNLOCK(nt);
666138568Ssam		/* XXX select channel more intelligently */
667138568Ssam		bestchan = -1;
668116742Ssam		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
669138568Ssam			if (isset(ic->ic_chan_active, i)) {
670138568Ssam				/*
671138568Ssam				 * If the channel is unoccupied the max rssi
672138568Ssam				 * should be zero; just take it.  Otherwise
673138568Ssam				 * track the channel with the lowest rssi and
674138568Ssam				 * use that when all channels appear occupied.
675138568Ssam				 */
676138568Ssam				if (maxrssi[i] == 0) {
677138568Ssam					bestchan = i;
678116742Ssam					break;
679138568Ssam				}
680143715Ssam				if (bestchan == -1 ||
681143715Ssam				    maxrssi[i] < maxrssi[bestchan])
682138568Ssam					bestchan = i;
683138568Ssam			}
684138568Ssam		if (bestchan != -1) {
685138568Ssam			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
686138568Ssam			return;
687116742Ssam		}
688138568Ssam		/* no suitable channel, should not happen */
689138568Ssam	}
690138568Ssam
691138568Ssam	/*
692138568Ssam	 * When manually sequencing the state machine; scan just once
693138568Ssam	 * regardless of whether we have a candidate or not.  The
694138568Ssam	 * controlling application is expected to setup state and
695138568Ssam	 * initiate an association.
696138568Ssam	 */
697138568Ssam	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
698116742Ssam		return;
699138568Ssam	/*
700138568Ssam	 * Automatic sequencing; look for a candidate and
701138568Ssam	 * if found join the network.
702138568Ssam	 */
703140448Ssam	/* NB: unlocked read should be ok */
704140448Ssam	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
705138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
706138568Ssam			"%s: no scan candidate\n", __func__);
707116742Ssam  notfound:
708116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS &&
709116742Ssam		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
710116742Ssam		    ic->ic_des_esslen != 0) {
711116742Ssam			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
712116742Ssam			return;
713116742Ssam		}
714116742Ssam		/*
715148432Ssam		 * Decrement the failure counts so entries will be
716148432Ssam		 * reconsidered the next time around.  We really want
717148432Ssam		 * to do this only for sta's where we've previously
718153352Ssam		 * had some success.
719148432Ssam		 */
720148432Ssam		IEEE80211_NODE_LOCK(nt);
721148432Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
722148432Ssam			if (ni->ni_fails)
723148432Ssam				ni->ni_fails--;
724148432Ssam		IEEE80211_NODE_UNLOCK(nt);
725148432Ssam		/*
726116742Ssam		 * Reset the list of channels to scan and start again.
727116742Ssam		 */
728138568Ssam		ieee80211_reset_scan(ic);
729138568Ssam		ic->ic_flags |= IEEE80211_F_SCAN;
730138568Ssam		ieee80211_next_scan(ic);
731116742Ssam		return;
732116742Ssam	}
733116742Ssam	selbs = NULL;
734138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
735138568Ssam	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
736140448Ssam	IEEE80211_NODE_LOCK(nt);
737140448Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
738138568Ssam		if (ieee80211_match_bss(ic, ni) == 0) {
739116742Ssam			if (selbs == NULL)
740116742Ssam				selbs = ni;
741140448Ssam			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
742116742Ssam				selbs = ni;
743116742Ssam		}
744116742Ssam	}
745140448Ssam	if (selbs != NULL)		/* NB: grab ref while dropping lock */
746140448Ssam		(void) ieee80211_ref_node(selbs);
747140448Ssam	IEEE80211_NODE_UNLOCK(nt);
748116742Ssam	if (selbs == NULL)
749116742Ssam		goto notfound;
750138568Ssam	if (!ieee80211_sta_join(ic, selbs)) {
751140448Ssam		ieee80211_free_node(selbs);
752138568Ssam		goto notfound;
753138568Ssam	}
754138568Ssam}
755138568Ssam
756138568Ssam/*
757138568Ssam * Handle 802.11 ad hoc network merge.  The
758138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
759138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
760138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
761138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
762138568Ssam * sends beacons with the greatest TSF timestamp.
763138568Ssam *
764138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
765138568Ssam *
766138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
767138568Ssam */
768138568Ssamint
769148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
770138568Ssam{
771148306Ssam	struct ieee80211com *ic = ni->ni_ic;
772138568Ssam
773140453Ssam	if (ni == ic->ic_bss ||
774140453Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
775138568Ssam		/* unchanged, nothing to do */
776138568Ssam		return 0;
777138568Ssam	}
778138568Ssam	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
779138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
780138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
781138568Ssam		ic->ic_stats.is_ibss_capmismatch++;
782138568Ssam		return 0;
783138568Ssam	}
784138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
785138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
786138568Ssam		ether_sprintf(ni->ni_bssid),
787138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
788138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
789138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
790138568Ssam	);
791140753Ssam	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
792138568Ssam}
793138568Ssam
794138568Ssam/*
795138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
796138568Ssam * be passed in with a held reference.
797138568Ssam */
798138568Ssamint
799138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
800138568Ssam{
801138568Ssam	struct ieee80211_node *obss;
802138568Ssam
803116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
804140753Ssam		struct ieee80211_node_table *nt;
805138568Ssam		/*
806140440Ssam		 * Delete unusable rates; we've already checked
807140440Ssam		 * that the negotiated rate set is acceptable.
808138568Ssam		 */
809148299Ssam		ieee80211_fix_rate(selbs, IEEE80211_F_DODEL);
810127772Ssam		/*
811140753Ssam		 * Fillin the neighbor table; it will already
812139521Ssam		 * exist if we are simply switching mastership.
813140753Ssam		 * XXX ic_sta always setup so this is unnecessary?
814127772Ssam		 */
815140753Ssam		nt = &ic->ic_sta;
816140753Ssam		IEEE80211_NODE_LOCK(nt);
817140753Ssam		nt->nt_name = "neighbor";
818140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
819140753Ssam		IEEE80211_NODE_UNLOCK(nt);
820138568Ssam	}
821138568Ssam
822138568Ssam	/*
823138568Ssam	 * Committed to selbs, setup state.
824138568Ssam	 */
825138568Ssam	obss = ic->ic_bss;
826140753Ssam	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
827153352Ssam	if (obss != NULL) {
828153352Ssam		copy_bss(selbs, obss);
829138568Ssam		ieee80211_free_node(obss);
830153352Ssam	}
831138568Ssam	/*
832138568Ssam	 * Set the erp state (mostly the slot time) to deal with
833138568Ssam	 * the auto-select case; this should be redundant if the
834138568Ssam	 * mode is locked.
835138568Ssam	 */
836138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
837148936Ssam	ic->ic_curchan = selbs->ni_chan;
838138568Ssam	ieee80211_reset_erp(ic);
839138568Ssam	ieee80211_wme_initparams(ic);
840140753Ssam
841140753Ssam	if (ic->ic_opmode == IEEE80211_M_STA)
842140753Ssam		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
843140753Ssam	else
844117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
845138568Ssam	return 1;
846116742Ssam}
847116742Ssam
848138568Ssam/*
849138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
850138568Ssam * be passed in with a held reference.
851138568Ssam */
852138568Ssamvoid
853138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
854138568Ssam{
855138568Ssam	ic->ic_node_cleanup(ni);
856138568Ssam	ieee80211_notify_node_leave(ic, ni);
857138568Ssam}
858138568Ssam
859116742Ssamstatic struct ieee80211_node *
860138568Ssamnode_alloc(struct ieee80211_node_table *nt)
861116742Ssam{
862127768Ssam	struct ieee80211_node *ni;
863138568Ssam
864127768Ssam	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
865127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
866127768Ssam	return ni;
867116742Ssam}
868116742Ssam
869138568Ssam/*
870138568Ssam * Reclaim any resources in a node and reset any critical
871138568Ssam * state.  Typically nodes are free'd immediately after,
872138568Ssam * but in some cases the storage may be reused so we need
873138568Ssam * to insure consistent state (should probably fix that).
874138568Ssam */
875116742Ssamstatic void
876138568Ssamnode_cleanup(struct ieee80211_node *ni)
877116742Ssam{
878138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
879138568Ssam	struct ieee80211com *ic = ni->ni_ic;
880138568Ssam	int i, qlen;
881138568Ssam
882138568Ssam	/* NB: preserve ni_table */
883138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
884138568Ssam		ic->ic_ps_sta--;
885138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
886138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
887138568Ssam		    "[%s] power save mode off, %u sta's in ps mode\n",
888138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
889138568Ssam	}
890147788Ssam	/*
891147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
892147788Ssam	 * has happened.  This is probably not needed as the node
893147788Ssam	 * should always be removed from the table so not found but
894147788Ssam	 * do it just in case.
895147788Ssam	 */
896147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
897138568Ssam
898138568Ssam	/*
899138568Ssam	 * Drain power save queue and, if needed, clear TIM.
900138568Ssam	 */
901138568Ssam	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
902138568Ssam	if (qlen != 0 && ic->ic_set_tim != NULL)
903148304Ssam		ic->ic_set_tim(ni, 0);
904138568Ssam
905138568Ssam	ni->ni_associd = 0;
906138568Ssam	if (ni->ni_challenge != NULL) {
907138568Ssam		FREE(ni->ni_challenge, M_DEVBUF);
908138568Ssam		ni->ni_challenge = NULL;
909138568Ssam	}
910138568Ssam	/*
911138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
912138568Ssam	 * reusable during a re-auth/re-assoc state transition.
913138568Ssam	 * If we remove these data they will not be recreated
914138568Ssam	 * because they come from a probe-response or beacon frame
915138568Ssam	 * which cannot be expected prior to the association-response.
916138568Ssam	 * This should not be an issue when operating in other modes
917138568Ssam	 * as stations leaving always go through a full state transition
918138568Ssam	 * which will rebuild this state.
919138568Ssam	 *
920138568Ssam	 * XXX does this leave us open to inheriting old state?
921138568Ssam	 */
922138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
923138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
924138568Ssam			m_freem(ni->ni_rxfrag[i]);
925138568Ssam			ni->ni_rxfrag[i] = NULL;
926138568Ssam		}
927148863Ssam	/*
928148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
929148863Ssam	 */
930148863Ssam	ieee80211_node_delucastkey(ni);
931138568Ssam#undef N
932116742Ssam}
933116742Ssam
934116742Ssamstatic void
935138568Ssamnode_free(struct ieee80211_node *ni)
936116742Ssam{
937138568Ssam	struct ieee80211com *ic = ni->ni_ic;
938138568Ssam
939138568Ssam	ic->ic_node_cleanup(ni);
940138568Ssam	if (ni->ni_wpa_ie != NULL)
941138568Ssam		FREE(ni->ni_wpa_ie, M_DEVBUF);
942138568Ssam	if (ni->ni_wme_ie != NULL)
943138568Ssam		FREE(ni->ni_wme_ie, M_DEVBUF);
944138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
945138568Ssam	FREE(ni, M_80211_NODE);
946116742Ssam}
947116742Ssam
948120104Ssamstatic u_int8_t
949138568Ssamnode_getrssi(const struct ieee80211_node *ni)
950120104Ssam{
951120104Ssam	return ni->ni_rssi;
952120104Ssam}
953120104Ssam
954116742Ssamstatic void
955138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt,
956138568Ssam	struct ieee80211_node *ni, const u_int8_t *macaddr)
957116742Ssam{
958138568Ssam	struct ieee80211com *ic = nt->nt_ic;
959116742Ssam	int hash;
960116742Ssam
961138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
962140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
963138568Ssam		ether_sprintf(macaddr), nt->nt_name);
964138568Ssam
965116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
966116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
967138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
968138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
969138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
970138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
971138568Ssam	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
972139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
973139528Ssam	ni->ni_inact = ni->ni_inact_reload;
974138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
975138568Ssam
976138568Ssam	IEEE80211_NODE_LOCK(nt);
977138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
978138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
979138568Ssam	ni->ni_table = nt;
980138568Ssam	ni->ni_ic = ic;
981138568Ssam	IEEE80211_NODE_UNLOCK(nt);
982116742Ssam}
983116742Ssam
984116742Ssamstruct ieee80211_node *
985138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
986116742Ssam{
987138568Ssam	struct ieee80211com *ic = nt->nt_ic;
988138568Ssam	struct ieee80211_node *ni;
989138568Ssam
990138568Ssam	ni = ic->ic_node_alloc(nt);
991116742Ssam	if (ni != NULL)
992138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
993127769Ssam	else
994127769Ssam		ic->ic_stats.is_rx_nodealloc++;
995116742Ssam	return ni;
996116742Ssam}
997116742Ssam
998148777Ssam/*
999148777Ssam * Craft a temporary node suitable for sending a management frame
1000148777Ssam * to the specified station.  We craft only as much state as we
1001148777Ssam * need to do the work since the node will be immediately reclaimed
1002148777Ssam * once the send completes.
1003148777Ssam */
1004116742Ssamstruct ieee80211_node *
1005148777Ssamieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
1006148777Ssam{
1007148777Ssam	struct ieee80211_node *ni;
1008148777Ssam
1009148777Ssam	ni = ic->ic_node_alloc(&ic->ic_sta);
1010148777Ssam	if (ni != NULL) {
1011148777Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1012148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1013148777Ssam
1014148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1015148777Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1016148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1017148777Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
1018148777Ssam		/* NB: required by ieee80211_fix_rate */
1019148777Ssam		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1020148777Ssam		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
1021148777Ssam			IEEE80211_KEYIX_NONE);
1022148777Ssam		/* XXX optimize away */
1023148777Ssam		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1024148777Ssam
1025148777Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1026148777Ssam		ni->ni_ic = ic;
1027148777Ssam	} else {
1028148777Ssam		/* XXX msg */
1029148777Ssam		ic->ic_stats.is_rx_nodealloc++;
1030148777Ssam	}
1031148777Ssam	return ni;
1032148777Ssam}
1033148777Ssam
1034148777Ssamstruct ieee80211_node *
1035138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1036116742Ssam{
1037138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1038138568Ssam	struct ieee80211_node *ni;
1039138568Ssam
1040138568Ssam	ni = ic->ic_node_alloc(nt);
1041116742Ssam	if (ni != NULL) {
1042138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
1043127770Ssam		/*
1044127770Ssam		 * Inherit from ic_bss.
1045127770Ssam		 */
1046138568Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
1047138568Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
1048138568Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1049127770Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1050138568Ssam		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1051138568Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
1052127770Ssam	} else
1053127770Ssam		ic->ic_stats.is_rx_nodealloc++;
1054116742Ssam	return ni;
1055116742Ssam}
1056116742Ssam
1057127772Ssamstatic struct ieee80211_node *
1058138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1059138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1060138568Ssam	const u_int8_t *macaddr, const char *func, int line)
1061138568Ssam#else
1062138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt,
1063138568Ssam	const u_int8_t *macaddr)
1064138568Ssam#endif
1065116742Ssam{
1066116742Ssam	struct ieee80211_node *ni;
1067116742Ssam	int hash;
1068116742Ssam
1069138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1070127772Ssam
1071116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1072138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1073116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1074138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1075138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1076138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1077140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1078140766Ssam			    func, line,
1079140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1080140766Ssam			    ieee80211_node_refcnt(ni));
1081138568Ssam#endif
1082127772Ssam			return ni;
1083116742Ssam		}
1084116742Ssam	}
1085127772Ssam	return NULL;
1086127772Ssam}
1087138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1088138568Ssam#define	_ieee80211_find_node(nt, mac) \
1089138568Ssam	_ieee80211_find_node_debug(nt, mac, func, line)
1090138568Ssam#endif
1091127772Ssam
1092127772Ssamstruct ieee80211_node *
1093138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1094138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1095138568Ssam	const u_int8_t *macaddr, const char *func, int line)
1096138568Ssam#else
1097138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1098138568Ssam#endif
1099127772Ssam{
1100127772Ssam	struct ieee80211_node *ni;
1101127772Ssam
1102138568Ssam	IEEE80211_NODE_LOCK(nt);
1103138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1104138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1105116742Ssam	return ni;
1106116742Ssam}
1107116742Ssam
1108116742Ssam/*
1109138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1110138568Ssam * Note that for the driver's benefit we we treat this like
1111138568Ssam * an association so the driver has an opportunity to setup
1112138568Ssam * it's private state.
1113138568Ssam */
1114138568Ssamstruct ieee80211_node *
1115138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1116138568Ssam	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1117138568Ssam{
1118138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1119138568Ssam	struct ieee80211_node *ni;
1120138568Ssam
1121153073Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1122153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1123138568Ssam	ni = ieee80211_dup_bss(nt, macaddr);
1124138568Ssam	if (ni != NULL) {
1125138568Ssam		/* XXX no rate negotiation; just dup */
1126138568Ssam		ni->ni_rates = ic->ic_bss->ni_rates;
1127139524Ssam		if (ic->ic_newassoc != NULL)
1128148307Ssam			ic->ic_newassoc(ni, 1);
1129138568Ssam		/* XXX not right for 802.1x/WPA */
1130148302Ssam		ieee80211_node_authorize(ni);
1131153404Ssam		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
1132153404Ssam			/*
1133153404Ssam			 * Blindly propagate capabilities based on the
1134153404Ssam			 * local configuration.  In particular this permits
1135153404Ssam			 * us to use QoS to disable ACK's.
1136153404Ssam			 */
1137153404Ssam			if (ic->ic_flags & IEEE80211_F_WME)
1138153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1139153404Ssam		}
1140138568Ssam	}
1141138568Ssam	return ni;
1142138568Ssam}
1143138568Ssam
1144148936Ssam#ifdef IEEE80211_DEBUG
1145148936Ssamstatic void
1146148936Ssamdump_probe_beacon(u_int8_t subtype, int isnew,
1147148936Ssam	const u_int8_t mac[IEEE80211_ADDR_LEN],
1148148936Ssam	const struct ieee80211_scanparams *sp)
1149148936Ssam{
1150148936Ssam
1151148936Ssam	printf("[%s] %s%s on chan %u (bss chan %u) ",
1152148936Ssam	    ether_sprintf(mac), isnew ? "new " : "",
1153148936Ssam	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1154148936Ssam	    sp->chan, sp->bchan);
1155148936Ssam	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1156148936Ssam	printf("\n");
1157148936Ssam
1158148936Ssam	if (isnew) {
1159148936Ssam		printf("[%s] caps 0x%x bintval %u erp 0x%x",
1160148936Ssam			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1161148936Ssam		if (sp->country != NULL) {
1162148936Ssam#ifdef __FreeBSD__
1163148936Ssam			printf(" country info %*D",
1164148936Ssam				sp->country[1], sp->country+2, " ");
1165148936Ssam#else
1166148936Ssam			int i;
1167148936Ssam			printf(" country info");
1168148936Ssam			for (i = 0; i < sp->country[1]; i++)
1169148936Ssam				printf(" %02x", sp->country[i+2]);
1170148936Ssam#endif
1171148936Ssam		}
1172148936Ssam		printf("\n");
1173148936Ssam	}
1174148936Ssam}
1175148936Ssam#endif /* IEEE80211_DEBUG */
1176148936Ssam
1177148936Ssamstatic void
1178148936Ssamsaveie(u_int8_t **iep, const u_int8_t *ie)
1179148936Ssam{
1180148936Ssam
1181148936Ssam	if (ie == NULL)
1182148936Ssam		*iep = NULL;
1183148936Ssam	else
1184148936Ssam		ieee80211_saveie(iep, ie);
1185148936Ssam}
1186148936Ssam
1187148936Ssam/*
1188148936Ssam * Process a beacon or probe response frame.
1189148936Ssam */
1190148936Ssamvoid
1191148936Ssamieee80211_add_scan(struct ieee80211com *ic,
1192148936Ssam	const struct ieee80211_scanparams *sp,
1193148936Ssam	const struct ieee80211_frame *wh,
1194148936Ssam	int subtype, int rssi, int rstamp)
1195148936Ssam{
1196148936Ssam#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1197148936Ssam	struct ieee80211_node_table *nt = &ic->ic_scan;
1198148936Ssam	struct ieee80211_node *ni;
1199148936Ssam	int newnode = 0;
1200148936Ssam
1201148936Ssam	ni = ieee80211_find_node(nt, wh->i_addr2);
1202148936Ssam	if (ni == NULL) {
1203148936Ssam		/*
1204148936Ssam		 * Create a new entry.
1205148936Ssam		 */
1206148936Ssam		ni = ic->ic_node_alloc(nt);
1207148936Ssam		if (ni == NULL) {
1208148936Ssam			ic->ic_stats.is_rx_nodealloc++;
1209148936Ssam			return;
1210148936Ssam		}
1211148936Ssam		ieee80211_setup_node(nt, ni, wh->i_addr2);
1212148936Ssam		/*
1213148936Ssam		 * XXX inherit from ic_bss.
1214148936Ssam		 */
1215148936Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
1216148936Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
1217148936Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1218148936Ssam		ieee80211_set_chan(ic, ni, ic->ic_curchan);
1219148936Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
1220148936Ssam		newnode = 1;
1221148936Ssam	}
1222148936Ssam#ifdef IEEE80211_DEBUG
1223148936Ssam	if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
1224148936Ssam		dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
1225148936Ssam#endif
1226148936Ssam	/* XXX ap beaconing multiple ssid w/ same bssid */
1227148936Ssam	if (sp->ssid[1] != 0 &&
1228148936Ssam	    (ISPROBE(subtype) || ni->ni_esslen == 0)) {
1229148936Ssam		ni->ni_esslen = sp->ssid[1];
1230148936Ssam		memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
1231148936Ssam		memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1232148936Ssam	}
1233148936Ssam	ni->ni_scangen = ic->ic_scan.nt_scangen;
1234148936Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1235148936Ssam	ni->ni_rssi = rssi;
1236148936Ssam	ni->ni_rstamp = rstamp;
1237148936Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1238148936Ssam	ni->ni_intval = sp->bintval;
1239148936Ssam	ni->ni_capinfo = sp->capinfo;
1240148936Ssam	ni->ni_chan = &ic->ic_channels[sp->chan];
1241148936Ssam	ni->ni_fhdwell = sp->fhdwell;
1242148936Ssam	ni->ni_fhindex = sp->fhindex;
1243148936Ssam	ni->ni_erp = sp->erp;
1244148936Ssam	if (sp->tim != NULL) {
1245148936Ssam		struct ieee80211_tim_ie *ie =
1246148936Ssam		    (struct ieee80211_tim_ie *) sp->tim;
1247148936Ssam
1248148936Ssam		ni->ni_dtim_count = ie->tim_count;
1249148936Ssam		ni->ni_dtim_period = ie->tim_period;
1250148936Ssam	}
1251148936Ssam	/*
1252148936Ssam	 * Record the byte offset from the mac header to
1253148936Ssam	 * the start of the TIM information element for
1254148936Ssam	 * use by hardware and/or to speedup software
1255148936Ssam	 * processing of beacon frames.
1256148936Ssam	 */
1257148936Ssam	ni->ni_timoff = sp->timoff;
1258148936Ssam	/*
1259148936Ssam	 * Record optional information elements that might be
1260148936Ssam	 * used by applications or drivers.
1261148936Ssam	 */
1262148936Ssam	saveie(&ni->ni_wme_ie, sp->wme);
1263148936Ssam	saveie(&ni->ni_wpa_ie, sp->wpa);
1264148936Ssam
1265148936Ssam	/* NB: must be after ni_chan is setup */
1266148936Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1267148936Ssam
1268148936Ssam	if (!newnode)
1269148936Ssam		ieee80211_free_node(ni);
1270148936Ssam#undef ISPROBE
1271148936Ssam}
1272148936Ssam
1273153073Ssamvoid
1274153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1275153073Ssam	const struct ieee80211_frame *wh,
1276153073Ssam	const struct ieee80211_scanparams *sp)
1277153073Ssam{
1278153073Ssam
1279153073Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1280153073Ssam	    "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr));
1281153073Ssam	ni->ni_esslen = sp->ssid[1];
1282153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1283153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1284153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1285153073Ssam	ni->ni_intval = sp->bintval;
1286153073Ssam	ni->ni_capinfo = sp->capinfo;
1287153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1288153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1289153073Ssam	ni->ni_fhindex = sp->fhindex;
1290153073Ssam	ni->ni_erp = sp->erp;
1291153073Ssam	ni->ni_timoff = sp->timoff;
1292153073Ssam	if (sp->wme != NULL)
1293153073Ssam		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1294153073Ssam	if (sp->wpa != NULL)
1295153073Ssam		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1296153073Ssam
1297153073Ssam	/* NB: must be after ni_chan is setup */
1298153073Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1299153073Ssam}
1300153073Ssam
1301148936Ssam/*
1302148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1303148936Ssam * or probe response frame.  Note that for the driver's
1304148936Ssam * benefit we we treat this like an association so the
1305148936Ssam * driver has an opportunity to setup it's private state.
1306148936Ssam */
1307148936Ssamstruct ieee80211_node *
1308148936Ssamieee80211_add_neighbor(struct ieee80211com *ic,
1309148936Ssam	const struct ieee80211_frame *wh,
1310148936Ssam	const struct ieee80211_scanparams *sp)
1311148936Ssam{
1312148936Ssam	struct ieee80211_node *ni;
1313148936Ssam
1314153073Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1315153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1316148936Ssam	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1317148936Ssam	if (ni != NULL) {
1318153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1319148936Ssam		if (ic->ic_newassoc != NULL)
1320148936Ssam			ic->ic_newassoc(ni, 1);
1321148936Ssam		/* XXX not right for 802.1x/WPA */
1322148936Ssam		ieee80211_node_authorize(ni);
1323148936Ssam	}
1324148936Ssam	return ni;
1325148936Ssam}
1326148936Ssam
1327148863Ssam#define	IS_CTL(wh) \
1328148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1329148863Ssam#define	IS_PSPOLL(wh) \
1330148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1331138568Ssam/*
1332138568Ssam * Locate the node for sender, track state, and then pass the
1333138568Ssam * (referenced) node up to the 802.11 layer for its use.  We
1334138568Ssam * are required to pass some node so we fall back to ic_bss
1335138568Ssam * when this frame is from an unknown sender.  The 802.11 layer
1336138568Ssam * knows this means the sender wasn't in the node table and
1337138568Ssam * acts accordingly.
1338138568Ssam */
1339138568Ssamstruct ieee80211_node *
1340138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1341138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1342138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1343138568Ssam#else
1344138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1345138568Ssam	const struct ieee80211_frame_min *wh)
1346138568Ssam#endif
1347138568Ssam{
1348138568Ssam	struct ieee80211_node_table *nt;
1349138568Ssam	struct ieee80211_node *ni;
1350138568Ssam
1351138568Ssam	/* XXX may want scanned nodes in the neighbor table for adhoc */
1352138568Ssam	if (ic->ic_opmode == IEEE80211_M_STA ||
1353138568Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1354138568Ssam	    (ic->ic_flags & IEEE80211_F_SCAN))
1355138568Ssam		nt = &ic->ic_scan;
1356138568Ssam	else
1357140753Ssam		nt = &ic->ic_sta;
1358138568Ssam	/* XXX check ic_bss first in station mode */
1359138568Ssam	/* XXX 4-address frames? */
1360138568Ssam	IEEE80211_NODE_LOCK(nt);
1361138568Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1362138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr1);
1363138568Ssam	else
1364138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr2);
1365148863Ssam	if (ni == NULL)
1366148863Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1367138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1368138568Ssam
1369148863Ssam	return ni;
1370148863Ssam}
1371148863Ssam
1372148863Ssam/*
1373148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1374148863Ssam * key index as a hint to locate the node in the key
1375148863Ssam * mapping table.  If an entry is present at the key
1376148863Ssam * index we return it; otherwise do a normal lookup and
1377148863Ssam * update the mapping table if the station has a unicast
1378148863Ssam * key assigned to it.
1379148863Ssam */
1380148863Ssamstruct ieee80211_node *
1381148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1382148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1383148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1384148863Ssam	const char *func, int line)
1385148863Ssam#else
1386148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1387148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1388148863Ssam#endif
1389148863Ssam{
1390148863Ssam	struct ieee80211_node_table *nt;
1391148863Ssam	struct ieee80211_node *ni;
1392148863Ssam
1393148863Ssam	if (ic->ic_opmode == IEEE80211_M_STA ||
1394148863Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1395148863Ssam	    (ic->ic_flags & IEEE80211_F_SCAN))
1396148863Ssam		nt = &ic->ic_scan;
1397148863Ssam	else
1398148863Ssam		nt = &ic->ic_sta;
1399148863Ssam	IEEE80211_NODE_LOCK(nt);
1400148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1401148863Ssam		ni = nt->nt_keyixmap[keyix];
1402148863Ssam	else
1403148863Ssam		ni = NULL;
1404148863Ssam	if (ni == NULL) {
1405148863Ssam		if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1406148863Ssam			ni = _ieee80211_find_node(nt, wh->i_addr1);
1407148863Ssam		else
1408148863Ssam			ni = _ieee80211_find_node(nt, wh->i_addr2);
1409148863Ssam		if (ni == NULL)
1410148863Ssam			ni = ieee80211_ref_node(ic->ic_bss);
1411148863Ssam		if (nt->nt_keyixmap != NULL) {
1412148863Ssam			/*
1413148863Ssam			 * If the station has a unicast key cache slot
1414148863Ssam			 * assigned update the key->node mapping table.
1415148863Ssam			 */
1416148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1417148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1418148863Ssam			if (keyix < nt->nt_keyixmax &&
1419148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1420148863Ssam				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1421148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1422148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1423148863Ssam				    ieee80211_node_refcnt(ni)+1);
1424148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1425148863Ssam			}
1426148863Ssam		}
1427148863Ssam	} else {
1428148863Ssam		ieee80211_ref_node(ni);
1429148863Ssam	}
1430148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1431148863Ssam
1432148863Ssam	return ni;
1433148863Ssam}
1434138568Ssam#undef IS_PSPOLL
1435138568Ssam#undef IS_CTL
1436138568Ssam
1437138568Ssam/*
1438127772Ssam * Return a reference to the appropriate node for sending
1439127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1440127772Ssam */
1441127772Ssamstruct ieee80211_node *
1442138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1443138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1444138568Ssam	const char *func, int line)
1445138568Ssam#else
1446138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1447138568Ssam#endif
1448127772Ssam{
1449140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1450127772Ssam	struct ieee80211_node *ni;
1451127772Ssam
1452127772Ssam	/*
1453127772Ssam	 * The destination address should be in the node table
1454148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1455148863Ssam	 * also optimize station mode operation, all frames go
1456148863Ssam	 * to the bss node.
1457127772Ssam	 */
1458127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1459138568Ssam	IEEE80211_NODE_LOCK(nt);
1460148863Ssam	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1461148863Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1462158121Ssam	else {
1463148863Ssam		ni = _ieee80211_find_node(nt, macaddr);
1464158121Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1465158121Ssam		    (ni != NULL && ni->ni_associd == 0)) {
1466158121Ssam			/*
1467158121Ssam			 * Station is not associated; don't permit the
1468158121Ssam			 * data frame to be sent by returning NULL.  This
1469158121Ssam			 * is kinda a kludge but the least intrusive way
1470158121Ssam			 * to add this check into all drivers.
1471158121Ssam			 */
1472158121Ssam			ieee80211_unref_node(&ni);	/* NB: null's ni */
1473158121Ssam		}
1474158121Ssam	}
1475138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1476138568Ssam
1477138568Ssam	if (ni == NULL) {
1478138568Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1479140497Ssam		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
1480140497Ssam			/*
1481140497Ssam			 * In adhoc mode cons up a node for the destination.
1482140497Ssam			 * Note that we need an additional reference for the
1483140497Ssam			 * caller to be consistent with _ieee80211_find_node.
1484140497Ssam			 */
1485138568Ssam			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1486140497Ssam			if (ni != NULL)
1487140497Ssam				(void) ieee80211_ref_node(ni);
1488140497Ssam		} else {
1489138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1490138568Ssam				"[%s] no node, discard frame (%s)\n",
1491138568Ssam				ether_sprintf(macaddr), __func__);
1492138568Ssam			ic->ic_stats.is_tx_nonode++;
1493127772Ssam		}
1494127772Ssam	}
1495127772Ssam	return ni;
1496127772Ssam}
1497127772Ssam
1498127772Ssam/*
1499116742Ssam * Like find but search based on the channel too.
1500116742Ssam */
1501116742Ssamstruct ieee80211_node *
1502138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1503138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1504138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan,
1505138568Ssam	const char *func, int line)
1506138568Ssam#else
1507138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1508138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan)
1509138568Ssam#endif
1510116742Ssam{
1511116742Ssam	struct ieee80211_node *ni;
1512116742Ssam	int hash;
1513116742Ssam
1514116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1515138568Ssam	IEEE80211_NODE_LOCK(nt);
1516138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1517127772Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1518127772Ssam		    ni->ni_chan == chan) {
1519138568Ssam			ieee80211_ref_node(ni);		/* mark referenced */
1520138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1521159139Sdds			    REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
1522140766Ssam			    ieee80211_node_refcnt(ni));
1523116742Ssam			break;
1524116742Ssam		}
1525116742Ssam	}
1526138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1527116742Ssam	return ni;
1528116742Ssam}
1529116742Ssam
1530138568Ssam/*
1531138568Ssam * Like find but search based on the ssid too.
1532138568Ssam */
1533138568Ssamstruct ieee80211_node *
1534138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1535138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1536138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1537138568Ssam	const char *func, int line)
1538138568Ssam#else
1539138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1540138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1541138568Ssam#endif
1542138568Ssam{
1543147118Ssam#define	MATCH_SSID(ni, ssid, ssidlen) \
1544147118Ssam	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1545147118Ssam	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
1546138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1547138568Ssam	struct ieee80211_node *ni;
1548138568Ssam	int hash;
1549138568Ssam
1550138568Ssam	IEEE80211_NODE_LOCK(nt);
1551147118Ssam	/*
1552147118Ssam	 * A mac address that is all zero means match only the ssid;
1553147118Ssam	 * otherwise we must match both.
1554147118Ssam	 */
1555147118Ssam	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1556147118Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1557147118Ssam			if (MATCH_SSID(ni, ssid, ssidlen))
1558147118Ssam				break;
1559147118Ssam		}
1560147118Ssam	} else {
1561147118Ssam		hash = IEEE80211_NODE_HASH(macaddr);
1562147118Ssam		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1563147118Ssam			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1564147118Ssam			    MATCH_SSID(ni, ssid, ssidlen))
1565147118Ssam				break;
1566147118Ssam		}
1567147118Ssam	}
1568147118Ssam	if (ni != NULL) {
1569147118Ssam		ieee80211_ref_node(ni);	/* mark referenced */
1570147118Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1571159139Sdds		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
1572147118Ssam		     ieee80211_node_refcnt(ni));
1573138568Ssam	}
1574138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1575138568Ssam	return ni;
1576147118Ssam#undef MATCH_SSID
1577138568Ssam}
1578138568Ssam
1579116742Ssamstatic void
1580138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1581116742Ssam{
1582138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1583138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1584119150Ssam
1585138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1586140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1587140766Ssam		ether_sprintf(ni->ni_macaddr),
1588138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1589138568Ssam
1590138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1591138568Ssam	if (nt != NULL) {
1592138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1593138568Ssam		LIST_REMOVE(ni, ni_hash);
1594138568Ssam	}
1595138568Ssam	ic->ic_node_free(ni);
1596116742Ssam}
1597116742Ssam
1598116742Ssamvoid
1599138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1600138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1601138568Ssam#else
1602138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1603138568Ssam#endif
1604116742Ssam{
1605138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1606119150Ssam
1607138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1608140454Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1609140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1610138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1611138568Ssam#endif
1612148863Ssam	if (nt != NULL) {
1613148863Ssam		IEEE80211_NODE_LOCK(nt);
1614148863Ssam		if (ieee80211_node_dectestref(ni)) {
1615148863Ssam			/*
1616148863Ssam			 * Last reference, reclaim state.
1617148863Ssam			 */
1618138568Ssam			_ieee80211_free_node(ni);
1619148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1620148863Ssam		    nt->nt_keyixmap != NULL) {
1621148863Ssam			ieee80211_keyix keyix;
1622148863Ssam			/*
1623148863Ssam			 * Check for a last reference in the key mapping table.
1624148863Ssam			 */
1625148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1626148863Ssam			if (keyix < nt->nt_keyixmax &&
1627148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1628148863Ssam				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1629148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1630148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1631148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1632148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1633148863Ssam				_ieee80211_free_node(ni);
1634148863Ssam			}
1635148863Ssam		}
1636148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1637148863Ssam	} else {
1638148863Ssam		if (ieee80211_node_dectestref(ni))
1639138568Ssam			_ieee80211_free_node(ni);
1640116742Ssam	}
1641116742Ssam}
1642116742Ssam
1643138568Ssam/*
1644148863Ssam * Reclaim a unicast key and clear any key cache state.
1645148863Ssam */
1646148863Ssamint
1647148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1648148863Ssam{
1649148863Ssam	struct ieee80211com *ic = ni->ni_ic;
1650148863Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1651148863Ssam	struct ieee80211_node *nikey;
1652148863Ssam	ieee80211_keyix keyix;
1653148863Ssam	int isowned, status;
1654148863Ssam
1655148863Ssam	/*
1656148863Ssam	 * NB: We must beware of LOR here; deleting the key
1657148863Ssam	 * can cause the crypto layer to block traffic updates
1658148863Ssam	 * which can generate a LOR against the node table lock;
1659148863Ssam	 * grab it here and stash the key index for our use below.
1660148863Ssam	 *
1661148863Ssam	 * Must also beware of recursion on the node table lock.
1662148863Ssam	 * When called from node_cleanup we may already have
1663148863Ssam	 * the node table lock held.  Unfortunately there's no
1664148863Ssam	 * way to separate out this path so we must do this
1665148863Ssam	 * conditionally.
1666148863Ssam	 */
1667148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1668148863Ssam	if (!isowned)
1669148863Ssam		IEEE80211_NODE_LOCK(nt);
1670148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1671148863Ssam	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1672148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1673148863Ssam		nikey = nt->nt_keyixmap[keyix];
1674148863Ssam		nt->nt_keyixmap[keyix] = NULL;;
1675148863Ssam	} else
1676148863Ssam		nikey = NULL;
1677148863Ssam	if (!isowned)
1678148863Ssam		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1679148863Ssam
1680148863Ssam	if (nikey != NULL) {
1681148863Ssam		KASSERT(nikey == ni,
1682148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1683148863Ssam		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1684148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1685148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1686148863Ssam			ieee80211_node_refcnt(ni)-1);
1687148863Ssam		ieee80211_free_node(ni);
1688148863Ssam	}
1689148863Ssam	return status;
1690148863Ssam}
1691148863Ssam
1692148863Ssam/*
1693138568Ssam * Reclaim a node.  If this is the last reference count then
1694138568Ssam * do the normal free work.  Otherwise remove it from the node
1695138568Ssam * table and mark it gone by clearing the back-reference.
1696138568Ssam */
1697138568Ssamstatic void
1698138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1699116742Ssam{
1700148863Ssam	ieee80211_keyix keyix;
1701138568Ssam
1702148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1703148863Ssam
1704140766Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1705140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1706140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1707140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1708148863Ssam	/*
1709148863Ssam	 * Clear any entry in the unicast key mapping table.
1710148863Ssam	 * We need to do it here so rx lookups don't find it
1711148863Ssam	 * in the mapping table even if it's not in the hash
1712148863Ssam	 * table.  We cannot depend on the mapping table entry
1713148863Ssam	 * being cleared because the node may not be free'd.
1714148863Ssam	 */
1715148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1716148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1717148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1718148863Ssam		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1719148863Ssam			"%s: %p<%s> clear key map entry\n",
1720148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr));
1721148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1722148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1723148863Ssam	}
1724138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1725138568Ssam		/*
1726138568Ssam		 * Other references are present, just remove the
1727138568Ssam		 * node from the table so it cannot be found.  When
1728138568Ssam		 * the references are dropped storage will be
1729140753Ssam		 * reclaimed.
1730138568Ssam		 */
1731138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1732138568Ssam		LIST_REMOVE(ni, ni_hash);
1733138568Ssam		ni->ni_table = NULL;		/* clear reference */
1734138568Ssam	} else
1735138568Ssam		_ieee80211_free_node(ni);
1736138568Ssam}
1737138568Ssam
1738138568Ssamstatic void
1739138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1740138568Ssam{
1741138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1742116742Ssam	struct ieee80211_node *ni;
1743116742Ssam
1744138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1745138568Ssam		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1746138568Ssam
1747138568Ssam	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1748138568Ssam		if (ni->ni_associd != 0) {
1749138568Ssam			if (ic->ic_auth->ia_node_leave != NULL)
1750138568Ssam				ic->ic_auth->ia_node_leave(ic, ni);
1751138568Ssam			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1752138568Ssam		}
1753138568Ssam		node_reclaim(nt, ni);
1754138568Ssam	}
1755138568Ssam	ieee80211_reset_erp(ic);
1756116742Ssam}
1757116742Ssam
1758138568Ssamstatic void
1759138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt)
1760138568Ssam{
1761138568Ssam
1762138568Ssam	IEEE80211_NODE_LOCK(nt);
1763138568Ssam	ieee80211_free_allnodes_locked(nt);
1764138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1765138568Ssam}
1766138568Ssam
1767120483Ssam/*
1768138568Ssam * Timeout entries in the scan cache.
1769138568Ssam */
1770138568Ssamstatic void
1771138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1772138568Ssam{
1773138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1774138568Ssam	struct ieee80211_node *ni, *tni;
1775138568Ssam
1776138568Ssam	IEEE80211_NODE_LOCK(nt);
1777138568Ssam	ni = ic->ic_bss;
1778138568Ssam	/* XXX belongs elsewhere */
1779138568Ssam	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1780138568Ssam		m_freem(ni->ni_rxfrag[0]);
1781138568Ssam		ni->ni_rxfrag[0] = NULL;
1782138568Ssam	}
1783138568Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1784138568Ssam		if (ni->ni_inact && --ni->ni_inact == 0) {
1785138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1786138568Ssam			    "[%s] scan candidate purged from cache "
1787138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1788140766Ssam			    ieee80211_node_refcnt(ni));
1789138568Ssam			node_reclaim(nt, ni);
1790138568Ssam		}
1791138568Ssam	}
1792138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1793138568Ssam
1794138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1795138568Ssam}
1796138568Ssam
1797138568Ssam/*
1798138568Ssam * Timeout inactive stations and do related housekeeping.
1799138568Ssam * Note that we cannot hold the node lock while sending a
1800138568Ssam * frame as this would lead to a LOR.  Instead we use a
1801138568Ssam * generation number to mark nodes that we've scanned and
1802138568Ssam * drop the lock and restart a scan if we have to time out
1803138568Ssam * a node.  Since we are single-threaded by virtue of
1804120483Ssam * controlling the inactivity timer we can be sure this will
1805120483Ssam * process each node only once.
1806120483Ssam */
1807138568Ssamstatic void
1808138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt)
1809116742Ssam{
1810138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1811120483Ssam	struct ieee80211_node *ni;
1812138568Ssam	u_int gen;
1813148320Ssam	int isadhoc;
1814116742Ssam
1815148320Ssam	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1816148320Ssam		   ic->ic_opmode == IEEE80211_M_AHDEMO);
1817138568Ssam	IEEE80211_SCAN_LOCK(nt);
1818154532Ssam	gen = ++nt->nt_scangen;
1819138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1820140766Ssam		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1821120483Ssamrestart:
1822138568Ssam	IEEE80211_NODE_LOCK(nt);
1823138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1824120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1825120483Ssam			continue;
1826120483Ssam		ni->ni_scangen = gen;
1827138568Ssam		/*
1828147788Ssam		 * Ignore entries for which have yet to receive an
1829147788Ssam		 * authentication frame.  These are transient and
1830147788Ssam		 * will be reclaimed when the last reference to them
1831147788Ssam		 * goes away (when frame xmits complete).
1832147788Ssam		 */
1833148323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1834148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1835147788Ssam			continue;
1836147788Ssam		/*
1837138568Ssam		 * Free fragment if not needed anymore
1838138568Ssam		 * (last fragment older than 1s).
1839138568Ssam		 * XXX doesn't belong here
1840138568Ssam		 */
1841138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1842138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1843138568Ssam			m_freem(ni->ni_rxfrag[0]);
1844138568Ssam			ni->ni_rxfrag[0] = NULL;
1845138568Ssam		}
1846140498Ssam		/*
1847140498Ssam		 * Special case ourself; we may be idle for extended periods
1848140498Ssam		 * of time and regardless reclaiming our state is wrong.
1849140498Ssam		 */
1850140498Ssam		if (ni == ic->ic_bss)
1851140498Ssam			continue;
1852138568Ssam		ni->ni_inact--;
1853148320Ssam		if (ni->ni_associd != 0 || isadhoc) {
1854119150Ssam			/*
1855138568Ssam			 * Age frames on the power save queue. The
1856138568Ssam			 * aging interval is 4 times the listen
1857138568Ssam			 * interval specified by the station.  This
1858138568Ssam			 * number is factored into the age calculations
1859138568Ssam			 * when the frame is placed on the queue.  We
1860138568Ssam			 * store ages as time differences we can check
1861138568Ssam			 * and/or adjust only the head of the list.
1862138568Ssam			 */
1863138568Ssam			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1864138568Ssam				struct mbuf *m;
1865138568Ssam				int discard = 0;
1866138568Ssam
1867138568Ssam				IEEE80211_NODE_SAVEQ_LOCK(ni);
1868138568Ssam				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1869138568Ssam				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1870138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1871138568Ssam					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1872138568Ssam					m_freem(m);
1873138568Ssam					discard++;
1874138568Ssam				}
1875138568Ssam				if (m != NULL)
1876138568Ssam					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1877138568Ssam				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1878138568Ssam
1879138568Ssam				if (discard != 0) {
1880138568Ssam					IEEE80211_DPRINTF(ic,
1881138568Ssam					    IEEE80211_MSG_POWER,
1882138568Ssam					    "[%s] discard %u frames for age\n",
1883138568Ssam					    ether_sprintf(ni->ni_macaddr),
1884138568Ssam					    discard);
1885138568Ssam					IEEE80211_NODE_STAT_ADD(ni,
1886138568Ssam						ps_discard, discard);
1887138568Ssam					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1888148304Ssam						ic->ic_set_tim(ni, 0);
1889138568Ssam				}
1890138568Ssam			}
1891138568Ssam			/*
1892138568Ssam			 * Probe the station before time it out.  We
1893138568Ssam			 * send a null data frame which may not be
1894138568Ssam			 * universally supported by drivers (need it
1895138568Ssam			 * for ps-poll support so it should be...).
1896138568Ssam			 */
1897139528Ssam			if (0 < ni->ni_inact &&
1898139528Ssam			    ni->ni_inact <= ic->ic_inact_probe) {
1899148320Ssam				IEEE80211_NOTE(ic,
1900148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1901148320Ssam				    ni, "%s",
1902148320Ssam				    "probe station due to inactivity");
1903148582Ssam				/*
1904148582Ssam				 * Grab a reference before unlocking the table
1905148582Ssam				 * so the node cannot be reclaimed before we
1906148582Ssam				 * send the frame. ieee80211_send_nulldata
1907148582Ssam				 * understands we've done this and reclaims the
1908148582Ssam				 * ref for us as needed.
1909148582Ssam				 */
1910148582Ssam				ieee80211_ref_node(ni);
1911138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1912148301Ssam				ieee80211_send_nulldata(ni);
1913138568Ssam				/* XXX stat? */
1914138568Ssam				goto restart;
1915138568Ssam			}
1916138568Ssam		}
1917138568Ssam		if (ni->ni_inact <= 0) {
1918148320Ssam			IEEE80211_NOTE(ic,
1919148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1920148320Ssam			    "station timed out due to inactivity "
1921148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1922138568Ssam			/*
1923138568Ssam			 * Send a deauthenticate frame and drop the station.
1924138568Ssam			 * This is somewhat complicated due to reference counts
1925138568Ssam			 * and locking.  At this point a station will typically
1926138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1927138568Ssam			 * will do a "free" of the node which will drop the
1928138568Ssam			 * reference count.  But in the meantime a reference
1929138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1930138568Ssam			 * of the node will happen either after the tx is
1931138568Ssam			 * completed or by ieee80211_node_leave.
1932120483Ssam			 *
1933138568Ssam			 * Separately we must drop the node lock before sending
1934138568Ssam			 * in case the driver takes a lock, as this will result
1935138568Ssam			 * in  LOR between the node lock and the driver lock.
1936119150Ssam			 */
1937138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1938138568Ssam			if (ni->ni_associd != 0) {
1939138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
1940138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1941138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1942138568Ssam			}
1943138568Ssam			ieee80211_node_leave(ic, ni);
1944121180Ssam			ic->ic_stats.is_node_timeout++;
1945120483Ssam			goto restart;
1946120483Ssam		}
1947116742Ssam	}
1948138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1949138568Ssam
1950138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1951138568Ssam
1952138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1953116742Ssam}
1954116742Ssam
1955116742Ssamvoid
1956138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1957116742Ssam{
1958116742Ssam	struct ieee80211_node *ni;
1959138568Ssam	u_int gen;
1960116742Ssam
1961138568Ssam	IEEE80211_SCAN_LOCK(nt);
1962154532Ssam	gen = ++nt->nt_scangen;
1963138568Ssamrestart:
1964138568Ssam	IEEE80211_NODE_LOCK(nt);
1965138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1966138568Ssam		if (ni->ni_scangen != gen) {
1967138568Ssam			ni->ni_scangen = gen;
1968138568Ssam			(void) ieee80211_ref_node(ni);
1969138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1970138568Ssam			(*f)(arg, ni);
1971138568Ssam			ieee80211_free_node(ni);
1972138568Ssam			goto restart;
1973138568Ssam		}
1974138568Ssam	}
1975138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1976138568Ssam
1977138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1978116742Ssam}
1979138568Ssam
1980138568Ssamvoid
1981138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1982138568Ssam{
1983138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
1984138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1985138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
1986138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1987138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
1988138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1989138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1990138568Ssam		ni->ni_txseqs[0],
1991138568Ssam		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1992138568Ssam		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1993138568Ssam		ni->ni_rxfragstamp);
1994138568Ssam	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
1995138568Ssam		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
1996138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1997138568Ssam		ether_sprintf(ni->ni_bssid),
1998138568Ssam		ni->ni_esslen, ni->ni_essid,
1999138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2000138568Ssam	printf("\tfails %u inact %u txrate %u\n",
2001138568Ssam		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
2002138568Ssam}
2003138568Ssam
2004138568Ssamvoid
2005138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2006138568Ssam{
2007138568Ssam	ieee80211_iterate_nodes(nt,
2008138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2009138568Ssam}
2010138568Ssam
2011138568Ssam/*
2012138568Ssam * Handle a station joining an 11g network.
2013138568Ssam */
2014138568Ssamstatic void
2015138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2016138568Ssam{
2017138568Ssam
2018138568Ssam	/*
2019138568Ssam	 * Station isn't capable of short slot time.  Bump
2020138568Ssam	 * the count of long slot time stations and disable
2021138568Ssam	 * use of short slot time.  Note that the actual switch
2022138568Ssam	 * over to long slot time use may not occur until the
2023138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2024138568Ssam	 */
2025138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2026138568Ssam		ic->ic_longslotsta++;
2027138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2028138568Ssam		    "[%s] station needs long slot time, count %d\n",
2029138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
2030138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2031138568Ssam		ieee80211_set_shortslottime(ic, 0);
2032138568Ssam	}
2033138568Ssam	/*
2034138568Ssam	 * If the new station is not an ERP station
2035138568Ssam	 * then bump the counter and enable protection
2036138568Ssam	 * if configured.
2037138568Ssam	 */
2038138568Ssam	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
2039138568Ssam		ic->ic_nonerpsta++;
2040138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2041138568Ssam		    "[%s] station is !ERP, %d non-ERP stations associated\n",
2042138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
2043138568Ssam		/*
2044138568Ssam		 * If protection is configured, enable it.
2045138568Ssam		 */
2046138568Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
2047138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2048138568Ssam			    "%s: enable use of protection\n", __func__);
2049138568Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2050138568Ssam		}
2051138568Ssam		/*
2052138568Ssam		 * If station does not support short preamble
2053138568Ssam		 * then we must enable use of Barker preamble.
2054138568Ssam		 */
2055138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2056138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2057138568Ssam			    "[%s] station needs long preamble\n",
2058138568Ssam			    ether_sprintf(ni->ni_macaddr));
2059138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2060138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2061138568Ssam		}
2062153973Ssam		if (ic->ic_nonerpsta == 1)
2063153973Ssam			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
2064138568Ssam	} else
2065138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2066138568Ssam}
2067138568Ssam
2068138568Ssamvoid
2069138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
2070138568Ssam{
2071138568Ssam	int newassoc;
2072138568Ssam
2073138568Ssam	if (ni->ni_associd == 0) {
2074138568Ssam		u_int16_t aid;
2075138568Ssam
2076138568Ssam		/*
2077138568Ssam		 * It would be good to search the bitmap
2078138568Ssam		 * more efficiently, but this will do for now.
2079138568Ssam		 */
2080138568Ssam		for (aid = 1; aid < ic->ic_max_aid; aid++) {
2081138568Ssam			if (!IEEE80211_AID_ISSET(aid,
2082138568Ssam			    ic->ic_aid_bitmap))
2083138568Ssam				break;
2084138568Ssam		}
2085138568Ssam		if (aid >= ic->ic_max_aid) {
2086138568Ssam			IEEE80211_SEND_MGMT(ic, ni, resp,
2087138568Ssam			    IEEE80211_REASON_ASSOC_TOOMANY);
2088138568Ssam			ieee80211_node_leave(ic, ni);
2089138568Ssam			return;
2090138568Ssam		}
2091138568Ssam		ni->ni_associd = aid | 0xc000;
2092138568Ssam		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
2093138568Ssam		ic->ic_sta_assoc++;
2094138568Ssam		newassoc = 1;
2095149031Ssam		if (ic->ic_curmode == IEEE80211_MODE_11G)
2096138568Ssam			ieee80211_node_join_11g(ic, ni);
2097138568Ssam	} else
2098138568Ssam		newassoc = 0;
2099138568Ssam
2100138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2101139523Ssam	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
2102139523Ssam	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
2103139523Ssam	    IEEE80211_NODE_AID(ni),
2104139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2105139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2106139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2107139523Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
2108139523Ssam	);
2109138568Ssam
2110138568Ssam	/* give driver a chance to setup state like ni_txrate */
2111139524Ssam	if (ic->ic_newassoc != NULL)
2112148307Ssam		ic->ic_newassoc(ni, newassoc);
2113139528Ssam	ni->ni_inact_reload = ic->ic_inact_auth;
2114139528Ssam	ni->ni_inact = ni->ni_inact_reload;
2115138568Ssam	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
2116138568Ssam	/* tell the authenticator about new station */
2117138568Ssam	if (ic->ic_auth->ia_node_join != NULL)
2118138568Ssam		ic->ic_auth->ia_node_join(ic, ni);
2119138568Ssam	ieee80211_notify_node_join(ic, ni, newassoc);
2120138568Ssam}
2121138568Ssam
2122138568Ssam/*
2123138568Ssam * Handle a station leaving an 11g network.
2124138568Ssam */
2125138568Ssamstatic void
2126138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2127138568Ssam{
2128138568Ssam
2129149031Ssam	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
2130148941Ssam	     ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
2131148941Ssam	      ni->ni_chan->ic_flags, ic->ic_curmode));
2132138568Ssam
2133138568Ssam	/*
2134138568Ssam	 * If a long slot station do the slot time bookkeeping.
2135138568Ssam	 */
2136138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2137138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2138138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2139138568Ssam		ic->ic_longslotsta--;
2140138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2141138568Ssam		    "[%s] long slot time station leaves, count now %d\n",
2142138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
2143138568Ssam		if (ic->ic_longslotsta == 0) {
2144138568Ssam			/*
2145138568Ssam			 * Re-enable use of short slot time if supported
2146138568Ssam			 * and not operating in IBSS mode (per spec).
2147138568Ssam			 */
2148138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2149138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2150138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2151138568Ssam				    "%s: re-enable use of short slot time\n",
2152138568Ssam				    __func__);
2153138568Ssam				ieee80211_set_shortslottime(ic, 1);
2154138568Ssam			}
2155138568Ssam		}
2156138568Ssam	}
2157138568Ssam	/*
2158138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2159138568Ssam	 */
2160138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2161138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2162138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2163138568Ssam		ic->ic_nonerpsta--;
2164138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2165138568Ssam		    "[%s] non-ERP station leaves, count now %d\n",
2166138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
2167138568Ssam		if (ic->ic_nonerpsta == 0) {
2168138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2169138568Ssam				"%s: disable use of protection\n", __func__);
2170138568Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
2171138568Ssam			/* XXX verify mode? */
2172138568Ssam			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2173138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2174138568Ssam				    "%s: re-enable use of short preamble\n",
2175138568Ssam				    __func__);
2176138568Ssam				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2177138568Ssam				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2178138568Ssam			}
2179153973Ssam			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
2180138568Ssam		}
2181138568Ssam	}
2182138568Ssam}
2183138568Ssam
2184138568Ssam/*
2185138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2186138568Ssam * when operating as an ap.
2187138568Ssam */
2188138568Ssamvoid
2189138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
2190138568Ssam{
2191140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2192138568Ssam
2193138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2194138568Ssam	    "[%s] station with aid %d leaves\n",
2195138568Ssam	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
2196138568Ssam
2197138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2198138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS ||
2199138568Ssam		ic->ic_opmode == IEEE80211_M_AHDEMO,
2200138568Ssam		("unexpected operating mode %u", ic->ic_opmode));
2201138568Ssam	/*
2202138568Ssam	 * If node wasn't previously associated all
2203138568Ssam	 * we need to do is reclaim the reference.
2204138568Ssam	 */
2205138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2206138568Ssam	if (ni->ni_associd == 0)
2207138568Ssam		goto done;
2208138568Ssam	/*
2209138568Ssam	 * Tell the authenticator the station is leaving.
2210138568Ssam	 * Note that we must do this before yanking the
2211138568Ssam	 * association id as the authenticator uses the
2212138568Ssam	 * associd to locate it's state block.
2213138568Ssam	 */
2214138568Ssam	if (ic->ic_auth->ia_node_leave != NULL)
2215138568Ssam		ic->ic_auth->ia_node_leave(ic, ni);
2216138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
2217138568Ssam	ni->ni_associd = 0;
2218138568Ssam	ic->ic_sta_assoc--;
2219138568Ssam
2220149031Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G)
2221138568Ssam		ieee80211_node_leave_11g(ic, ni);
2222138568Ssam	/*
2223138568Ssam	 * Cleanup station state.  In particular clear various
2224138568Ssam	 * state that might otherwise be reused if the node
2225138568Ssam	 * is reused before the reference count goes to zero
2226138568Ssam	 * (and memory is reclaimed).
2227138568Ssam	 */
2228138568Ssam	ieee80211_sta_leave(ic, ni);
2229138568Ssamdone:
2230140499Ssam	/*
2231140499Ssam	 * Remove the node from any table it's recorded in and
2232140499Ssam	 * drop the caller's reference.  Removal from the table
2233140499Ssam	 * is important to insure the node is not reprocessed
2234140499Ssam	 * for inactivity.
2235140499Ssam	 */
2236140499Ssam	if (nt != NULL) {
2237140499Ssam		IEEE80211_NODE_LOCK(nt);
2238140499Ssam		node_reclaim(nt, ni);
2239140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2240140499Ssam	} else
2241140499Ssam		ieee80211_free_node(ni);
2242138568Ssam}
2243138568Ssam
2244138568Ssamu_int8_t
2245138568Ssamieee80211_getrssi(struct ieee80211com *ic)
2246138568Ssam{
2247138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2248140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2249138568Ssam	u_int32_t rssi_samples, rssi_total;
2250138568Ssam	struct ieee80211_node *ni;
2251138568Ssam
2252138568Ssam	rssi_total = 0;
2253138568Ssam	rssi_samples = 0;
2254138568Ssam	switch (ic->ic_opmode) {
2255138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2256138568Ssam		/* XXX locking */
2257140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2258138568Ssam			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
2259138568Ssam				rssi_samples++;
2260138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
2261138568Ssam			}
2262138568Ssam		break;
2263138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2264138568Ssam		/* XXX locking */
2265140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2266138568Ssam			rssi_samples++;
2267138568Ssam			rssi_total += ic->ic_node_getrssi(ni);
2268138568Ssam		}
2269138568Ssam		break;
2270138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2271138568Ssam		/* XXX locking */
2272140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2273138568Ssam			if (IEEE80211_AID(ni->ni_associd) != 0) {
2274138568Ssam				rssi_samples++;
2275138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
2276138568Ssam			}
2277138568Ssam		break;
2278138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2279138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2280138568Ssam	default:
2281138568Ssam		if (ic->ic_bss != NULL)
2282138568Ssam			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
2283138568Ssam		rssi_samples = 1;
2284138568Ssam		break;
2285138568Ssam	}
2286138568Ssam	return rssi_total / NZ(rssi_samples);
2287138568Ssam#undef NZ
2288138568Ssam}
2289138568Ssam
2290138568Ssam/*
2291138568Ssam * Indicate whether there are frames queued for a station in power-save mode.
2292138568Ssam */
2293138568Ssamstatic void
2294148304Ssamieee80211_set_tim(struct ieee80211_node *ni, int set)
2295138568Ssam{
2296148304Ssam	struct ieee80211com *ic = ni->ni_ic;
2297138568Ssam	u_int16_t aid;
2298138568Ssam
2299138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2300138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS,
2301138568Ssam		("operating mode %u", ic->ic_opmode));
2302138568Ssam
2303138568Ssam	aid = IEEE80211_AID(ni->ni_associd);
2304138568Ssam	KASSERT(aid < ic->ic_max_aid,
2305138568Ssam		("bogus aid %u, max %u", aid, ic->ic_max_aid));
2306138568Ssam
2307138568Ssam	IEEE80211_BEACON_LOCK(ic);
2308138568Ssam	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
2309138568Ssam		if (set) {
2310138568Ssam			setbit(ic->ic_tim_bitmap, aid);
2311138568Ssam			ic->ic_ps_pending++;
2312138568Ssam		} else {
2313138568Ssam			clrbit(ic->ic_tim_bitmap, aid);
2314138568Ssam			ic->ic_ps_pending--;
2315138568Ssam		}
2316138568Ssam		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
2317138568Ssam	}
2318138568Ssam	IEEE80211_BEACON_UNLOCK(ic);
2319138568Ssam}
2320138568Ssam
2321138568Ssam/*
2322138568Ssam * Node table support.
2323138568Ssam */
2324138568Ssam
2325138568Ssamstatic void
2326138568Ssamieee80211_node_table_init(struct ieee80211com *ic,
2327138568Ssam	struct ieee80211_node_table *nt,
2328148863Ssam	const char *name, int inact, int keyixmax,
2329138568Ssam	void (*timeout)(struct ieee80211_node_table *))
2330138568Ssam{
2331138568Ssam
2332138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
2333138568Ssam		"%s %s table, inact %u\n", __func__, name, inact);
2334138568Ssam
2335138568Ssam	nt->nt_ic = ic;
2336138568Ssam	/* XXX need unit */
2337138568Ssam	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2338138568Ssam	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2339138568Ssam	TAILQ_INIT(&nt->nt_node);
2340138568Ssam	nt->nt_name = name;
2341138568Ssam	nt->nt_scangen = 1;
2342138568Ssam	nt->nt_inact_init = inact;
2343138568Ssam	nt->nt_timeout = timeout;
2344148863Ssam	nt->nt_keyixmax = keyixmax;
2345148863Ssam	if (nt->nt_keyixmax > 0) {
2346148863Ssam		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2347148863Ssam			keyixmax * sizeof(struct ieee80211_node *),
2348148863Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
2349148863Ssam		if (nt->nt_keyixmap == NULL)
2350148863Ssam			if_printf(ic->ic_ifp,
2351148863Ssam			    "Cannot allocate key index map with %u entries\n",
2352148863Ssam			    keyixmax);
2353148863Ssam	} else
2354148863Ssam		nt->nt_keyixmap = NULL;
2355138568Ssam}
2356138568Ssam
2357138568Ssamvoid
2358138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt)
2359138568Ssam{
2360138568Ssam
2361138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2362138568Ssam		"%s %s table\n", __func__, nt->nt_name);
2363138568Ssam
2364138568Ssam	IEEE80211_NODE_LOCK(nt);
2365138568Ssam	nt->nt_inact_timer = 0;
2366138568Ssam	ieee80211_free_allnodes_locked(nt);
2367138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2368138568Ssam}
2369138568Ssam
2370138568Ssamstatic void
2371138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2372138568Ssam{
2373138568Ssam
2374138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2375138568Ssam		"%s %s table\n", __func__, nt->nt_name);
2376138568Ssam
2377148863Ssam	IEEE80211_NODE_LOCK(nt);
2378138568Ssam	ieee80211_free_allnodes_locked(nt);
2379148863Ssam	if (nt->nt_keyixmap != NULL) {
2380148863Ssam		/* XXX verify all entries are NULL */
2381148863Ssam		int i;
2382148863Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
2383148863Ssam			if (nt->nt_keyixmap[i] != NULL)
2384148863Ssam				printf("%s: %s[%u] still active\n", __func__,
2385148863Ssam					nt->nt_name, i);
2386148863Ssam		FREE(nt->nt_keyixmap, M_80211_NODE);
2387148863Ssam		nt->nt_keyixmap = NULL;
2388148863Ssam	}
2389138568Ssam	IEEE80211_SCAN_LOCK_DESTROY(nt);
2390138568Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
2391138568Ssam}
2392