ieee80211_node.c revision 165887
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2001 Atsushi Onoe
3219820Sjeff * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4219820Sjeff * All rights reserved.
5219820Sjeff *
6219820Sjeff * Redistribution and use in source and binary forms, with or without
7219820Sjeff * modification, are permitted provided that the following conditions
8219820Sjeff * are met:
9219820Sjeff * 1. Redistributions of source code must retain the above copyright
10219820Sjeff *    notice, this list of conditions and the following disclaimer.
11219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
12219820Sjeff *    notice, this list of conditions and the following disclaimer in the
13219820Sjeff *    documentation and/or other materials provided with the distribution.
14219820Sjeff * 3. The name of the author may not be used to endorse or promote products
15219820Sjeff *    derived from this software without specific prior written permission.
16219820Sjeff *
17219820Sjeff * Alternatively, this software may be distributed under the terms of the
18219820Sjeff * GNU General Public License ("GPL") version 2 as published by the Free
19219820Sjeff * Software Foundation.
20219820Sjeff *
21219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31219820Sjeff */
32219820Sjeff
33219820Sjeff#include <sys/cdefs.h>
34219820Sjeff__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 165887 2007-01-08 17:24:51Z sam $");
35219820Sjeff
36219820Sjeff#include <sys/param.h>
37219820Sjeff#include <sys/systm.h>
38219820Sjeff#include <sys/mbuf.h>
39219820Sjeff#include <sys/malloc.h>
40219820Sjeff#include <sys/kernel.h>
41219820Sjeff
42219820Sjeff#include <sys/socket.h>
43219820Sjeff
44219820Sjeff#include <net/if.h>
45219820Sjeff#include <net/if_media.h>
46219820Sjeff#include <net/ethernet.h>
47219820Sjeff
48219820Sjeff#include <net80211/ieee80211_var.h>
49219820Sjeff
50219820Sjeff#include <net/bpf.h>
51219820Sjeff
52219820Sjeff/*
53219820Sjeff * Association id's are managed with a bit vector.
54219820Sjeff */
55219820Sjeff#define	IEEE80211_AID_SET(b, w) \
56219820Sjeff	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
57219820Sjeff#define	IEEE80211_AID_CLR(b, w) \
58219820Sjeff	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
59219820Sjeff#define	IEEE80211_AID_ISSET(b, w) \
60219820Sjeff	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
61219820Sjeff
62219820Sjeff#ifdef IEEE80211_DEBUG_REFCNT
63219820Sjeff#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
64219820Sjeff#else
65219820Sjeff#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
66219820Sjeff#endif
67219820Sjeff
68219820Sjeffstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
69219820Sjeffstatic void node_cleanup(struct ieee80211_node *);
70219820Sjeffstatic void node_free(struct ieee80211_node *);
71219820Sjeffstatic u_int8_t node_getrssi(const struct ieee80211_node *);
72219820Sjeff
73219820Sjeffstatic void ieee80211_setup_node(struct ieee80211_node_table *,
74219820Sjeff		struct ieee80211_node *, const u_int8_t *);
75219820Sjeffstatic void _ieee80211_free_node(struct ieee80211_node *);
76219820Sjeffstatic void ieee80211_free_allnodes(struct ieee80211_node_table *);
77219820Sjeff
78219820Sjeffstatic void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
79219820Sjeffstatic void ieee80211_timeout_stations(struct ieee80211_node_table *);
80219820Sjeff
81219820Sjeffstatic void ieee80211_set_tim(struct ieee80211_node *, int set);
82219820Sjeff
83219820Sjeffstatic void ieee80211_node_table_init(struct ieee80211com *ic,
84219820Sjeff	struct ieee80211_node_table *nt, const char *name,
85219820Sjeff	int inact, int keyixmax,
86219820Sjeff	void (*timeout)(struct ieee80211_node_table *));
87219820Sjeffstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
88219820Sjeff
89219820SjeffMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
90219820Sjeff
91219820Sjeffvoid
92219820Sjeffieee80211_node_attach(struct ieee80211com *ic)
93219820Sjeff{
94219820Sjeff
95219820Sjeff	ic->ic_node_alloc = node_alloc;
96219820Sjeff	ic->ic_node_free = node_free;
97219820Sjeff	ic->ic_node_cleanup = node_cleanup;
98219820Sjeff	ic->ic_node_getrssi = node_getrssi;
99219820Sjeff
100219820Sjeff	/* default station inactivity timer setings */
101219820Sjeff	ic->ic_inact_init = IEEE80211_INACT_INIT;
102219820Sjeff	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
103219820Sjeff	ic->ic_inact_run = IEEE80211_INACT_RUN;
104219820Sjeff	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
105219820Sjeff
106219820Sjeff	/* NB: driver should override */
107219820Sjeff	ic->ic_max_aid = IEEE80211_AID_DEF;
108219820Sjeff	ic->ic_set_tim = ieee80211_set_tim;
109219820Sjeff}
110219820Sjeff
111219820Sjeffvoid
112219820Sjeffieee80211_node_lateattach(struct ieee80211com *ic)
113219820Sjeff{
114219820Sjeff	struct ieee80211_rsnparms *rsn;
115219820Sjeff
116219820Sjeff	if (ic->ic_max_aid > IEEE80211_AID_MAX)
117219820Sjeff		ic->ic_max_aid = IEEE80211_AID_MAX;
118219820Sjeff	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
119219820Sjeff		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
120219820Sjeff		M_DEVBUF, M_NOWAIT | M_ZERO);
121219820Sjeff	if (ic->ic_aid_bitmap == NULL) {
122219820Sjeff		/* XXX no way to recover */
123219820Sjeff		printf("%s: no memory for AID bitmap!\n", __func__);
124219820Sjeff		ic->ic_max_aid = 0;
125219820Sjeff	}
126219820Sjeff
127219820Sjeff	/* XXX defer until using hostap/ibss mode */
128219820Sjeff	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
129219820Sjeff	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
130219820Sjeff		M_DEVBUF, M_NOWAIT | M_ZERO);
131219820Sjeff	if (ic->ic_tim_bitmap == NULL) {
132219820Sjeff		/* XXX no way to recover */
133219820Sjeff		printf("%s: no memory for TIM bitmap!\n", __func__);
134219820Sjeff	}
135219820Sjeff
136219820Sjeff	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
137219820Sjeff		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
138219820Sjeff		ieee80211_timeout_stations);
139219820Sjeff	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
140219820Sjeff		IEEE80211_INACT_SCAN, 0,
141219820Sjeff		ieee80211_timeout_scan_candidates);
142219820Sjeff
143219820Sjeff	ieee80211_reset_bss(ic);
144219820Sjeff	/*
145219820Sjeff	 * Setup "global settings" in the bss node so that
146219820Sjeff	 * each new station automatically inherits them.
147219820Sjeff	 */
148219820Sjeff	rsn = &ic->ic_bss->ni_rsn;
149219820Sjeff	/* WEP, TKIP, and AES-CCM are always supported */
150219820Sjeff	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
151219820Sjeff	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
152219820Sjeff	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
153219820Sjeff	if (ic->ic_caps & IEEE80211_C_AES)
154219820Sjeff		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
155219820Sjeff	if (ic->ic_caps & IEEE80211_C_CKIP)
156219820Sjeff		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
157219820Sjeff	/*
158219820Sjeff	 * Default unicast cipher to WEP for 802.1x use.  If
159219820Sjeff	 * WPA is enabled the management code will set these
160219820Sjeff	 * values to reflect.
161219820Sjeff	 */
162219820Sjeff	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
163219820Sjeff	rsn->rsn_ucastkeylen = 104 / NBBY;
164219820Sjeff	/*
165219820Sjeff	 * WPA says the multicast cipher is the lowest unicast
166219820Sjeff	 * cipher supported.  But we skip WEP which would
167219820Sjeff	 * otherwise be used based on this criteria.
168219820Sjeff	 */
169219820Sjeff	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
170219820Sjeff	rsn->rsn_mcastkeylen = 128 / NBBY;
171219820Sjeff
172219820Sjeff	/*
173219820Sjeff	 * We support both WPA-PSK and 802.1x; the one used
174219820Sjeff	 * is determined by the authentication mode and the
175219820Sjeff	 * setting of the PSK state.
176219820Sjeff	 */
177219820Sjeff	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
178219820Sjeff	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
179219820Sjeff
180219820Sjeff	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
181219820Sjeff}
182219820Sjeff
183219820Sjeffvoid
184219820Sjeffieee80211_node_detach(struct ieee80211com *ic)
185219820Sjeff{
186219820Sjeff
187219820Sjeff	if (ic->ic_bss != NULL) {
188219820Sjeff		ieee80211_free_node(ic->ic_bss);
189219820Sjeff		ic->ic_bss = NULL;
190219820Sjeff	}
191219820Sjeff	ieee80211_node_table_cleanup(&ic->ic_scan);
192219820Sjeff	ieee80211_node_table_cleanup(&ic->ic_sta);
193219820Sjeff	if (ic->ic_aid_bitmap != NULL) {
194219820Sjeff		FREE(ic->ic_aid_bitmap, M_DEVBUF);
195219820Sjeff		ic->ic_aid_bitmap = NULL;
196219820Sjeff	}
197219820Sjeff	if (ic->ic_tim_bitmap != NULL) {
198219820Sjeff		FREE(ic->ic_tim_bitmap, M_DEVBUF);
199219820Sjeff		ic->ic_tim_bitmap = NULL;
200219820Sjeff	}
201219820Sjeff}
202219820Sjeff
203219820Sjeff/*
204219820Sjeff * Port authorize/unauthorize interfaces for use by an authenticator.
205219820Sjeff */
206219820Sjeff
207219820Sjeffvoid
208219820Sjeffieee80211_node_authorize(struct ieee80211_node *ni)
209219820Sjeff{
210219820Sjeff	struct ieee80211com *ic = ni->ni_ic;
211219820Sjeff
212219820Sjeff	ni->ni_flags |= IEEE80211_NODE_AUTH;
213219820Sjeff	ni->ni_inact_reload = ic->ic_inact_run;
214219820Sjeff}
215219820Sjeff
216219820Sjeffvoid
217219820Sjeffieee80211_node_unauthorize(struct ieee80211_node *ni)
218219820Sjeff{
219219820Sjeff	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
220219820Sjeff}
221219820Sjeff
222219820Sjeff/*
223219820Sjeff * Set/change the channel.  The rate set is also updated as
224219820Sjeff * to insure a consistent view by drivers.
225219820Sjeff */
226219820Sjeffstatic void
227219820Sjeffieee80211_set_chan(struct ieee80211com *ic,
228219820Sjeff	struct ieee80211_node *ni, struct ieee80211_channel *chan)
229219820Sjeff{
230219820Sjeff	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
231219820Sjeff		chan = ic->ic_curchan;
232219820Sjeff	ni->ni_chan = chan;
233219820Sjeff	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
234219820Sjeff}
235219820Sjeff
236219820Sjeff/*
237219820Sjeff * AP scanning support.
238219820Sjeff */
239219820Sjeff
240219820Sjeff#ifdef IEEE80211_DEBUG
241219820Sjeffstatic void
242219820Sjeffdump_chanlist(const u_char chans[])
243219820Sjeff{
244219820Sjeff	const char *sep;
245219820Sjeff	int i;
246219820Sjeff
247219820Sjeff	sep = " ";
248219820Sjeff	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
249219820Sjeff		if (isset(chans, i)) {
250219820Sjeff			printf("%s%u", sep, i);
251219820Sjeff			sep = ", ";
252219820Sjeff		}
253219820Sjeff}
254219820Sjeff#endif /* IEEE80211_DEBUG */
255219820Sjeff
256219820Sjeff/*
257219820Sjeff * Initialize the channel set to scan based on the
258219820Sjeff * of available channels and the current PHY mode.
259219820Sjeff */
260219820Sjeffstatic void
261219820Sjeffieee80211_reset_scan(struct ieee80211com *ic)
262219820Sjeff{
263219820Sjeff
264219820Sjeff	/* XXX ic_des_chan should be handled with ic_chan_active */
265219820Sjeff	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
266219820Sjeff		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
267219820Sjeff		setbit(ic->ic_chan_scan,
268219820Sjeff			ieee80211_chan2ieee(ic, ic->ic_des_chan));
269219820Sjeff	} else
270219820Sjeff		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
271219820Sjeff			sizeof(ic->ic_chan_active));
272219820Sjeff#ifdef IEEE80211_DEBUG
273219820Sjeff	if (ieee80211_msg_scan(ic)) {
274219820Sjeff		printf("%s: scan set:", __func__);
275219820Sjeff		dump_chanlist(ic->ic_chan_scan);
276219820Sjeff		printf(" start chan %u\n",
277219820Sjeff			ieee80211_chan2ieee(ic, ic->ic_curchan));
278219820Sjeff	}
279219820Sjeff#endif /* IEEE80211_DEBUG */
280219820Sjeff}
281219820Sjeff
282219820Sjeff/*
283219820Sjeff * Begin an active scan.
284219820Sjeff */
285219820Sjeffvoid
286219820Sjeffieee80211_begin_scan(struct ieee80211com *ic, int reset)
287219820Sjeff{
288219820Sjeff
289219820Sjeff	ic->ic_scan.nt_scangen++;
290219820Sjeff	/*
291219820Sjeff	 * In all but hostap mode scanning starts off in
292219820Sjeff	 * an active mode before switching to passive.
293219820Sjeff	 */
294219820Sjeff	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
295219820Sjeff		ic->ic_flags |= IEEE80211_F_ASCAN;
296219820Sjeff		ic->ic_stats.is_scan_active++;
297219820Sjeff	} else
298219820Sjeff		ic->ic_stats.is_scan_passive++;
299219820Sjeff	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
300219820Sjeff		"begin %s scan in %s mode, scangen %u\n",
301219820Sjeff		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
302219820Sjeff		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
303219820Sjeff	/*
304219820Sjeff	 * Clear scan state and flush any previously seen AP's.
305219820Sjeff	 */
306219820Sjeff	ieee80211_reset_scan(ic);
307219820Sjeff	if (reset)
308219820Sjeff		ieee80211_free_allnodes(&ic->ic_scan);
309219820Sjeff
310219820Sjeff	ic->ic_flags |= IEEE80211_F_SCAN;
311219820Sjeff
312219820Sjeff	/* Scan the next channel. */
313219820Sjeff	ieee80211_next_scan(ic);
314219820Sjeff}
315219820Sjeff
316219820Sjeff/*
317219820Sjeff * Switch to the next channel marked for scanning.
318219820Sjeff */
319219820Sjeffint
320219820Sjeffieee80211_next_scan(struct ieee80211com *ic)
321219820Sjeff{
322219820Sjeff	struct ieee80211_channel *chan;
323219820Sjeff
324219820Sjeff	/*
325219820Sjeff	 * Insure any previous mgt frame timeouts don't fire.
326219820Sjeff	 * This assumes the driver does the right thing in
327219820Sjeff	 * flushing anything queued in the driver and below.
328219820Sjeff	 */
329219820Sjeff	ic->ic_mgt_timer = 0;
330219820Sjeff	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
331219820Sjeff
332219820Sjeff	chan = ic->ic_curchan;
333219820Sjeff	do {
334219820Sjeff		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
335219820Sjeff			chan = &ic->ic_channels[0];
336219820Sjeff		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
337219820Sjeff			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
338219820Sjeff			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
339219820Sjeff			    "%s: chan %d->%d\n", __func__,
340219820Sjeff			    ieee80211_chan2ieee(ic, ic->ic_curchan),
341219820Sjeff			    ieee80211_chan2ieee(ic, chan));
342219820Sjeff			ic->ic_curchan = chan;
343219820Sjeff			/*
344219820Sjeff			 * XXX drivers should do this as needed,
345219820Sjeff			 * XXX for now maintain compatibility
346219820Sjeff			 */
347219820Sjeff			ic->ic_bss->ni_rates = *ieee80211_get_suprates(ic, chan);
348219820Sjeff			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
349219820Sjeff			return 1;
350219820Sjeff		}
351219820Sjeff	} while (chan != ic->ic_curchan);
352219820Sjeff	ieee80211_end_scan(ic);
353219820Sjeff	return 0;
354219820Sjeff}
355219820Sjeff
356219820Sjeff/*
357219820Sjeff * Probe the curent channel, if allowed, while scanning.
358219820Sjeff * If the channel is not marked passive-only then send
359219820Sjeff * a probe request immediately.  Otherwise mark state and
360219820Sjeff * listen for beacons on the channel; if we receive something
361219820Sjeff * then we'll transmit a probe request.
362219820Sjeff */
363219820Sjeffvoid
364219820Sjeffieee80211_probe_curchan(struct ieee80211com *ic, int force)
365219820Sjeff{
366219820Sjeff	struct ifnet *ifp = ic->ic_ifp;
367219820Sjeff
368219820Sjeff	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
369219820Sjeff		/*
370219820Sjeff		 * XXX send both broadcast+directed probe request
371219820Sjeff		 */
372219820Sjeff		ieee80211_send_probereq(ic->ic_bss,
373219820Sjeff			ic->ic_myaddr, ifp->if_broadcastaddr,
374219820Sjeff			ifp->if_broadcastaddr,
375219820Sjeff			ic->ic_des_essid, ic->ic_des_esslen,
376219820Sjeff			ic->ic_opt_ie, ic->ic_opt_ie_len);
377219820Sjeff	} else
378219820Sjeff		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
379219820Sjeff}
380219820Sjeff
381219820Sjeffstatic __inline void
382219820Sjeffcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
383219820Sjeff{
384219820Sjeff	/* propagate useful state */
385219820Sjeff	nbss->ni_authmode = obss->ni_authmode;
386219820Sjeff	nbss->ni_txpower = obss->ni_txpower;
387219820Sjeff	nbss->ni_vlan = obss->ni_vlan;
388219820Sjeff	nbss->ni_rsn = obss->ni_rsn;
389219820Sjeff	/* XXX statistics? */
390219820Sjeff}
391219820Sjeff
392219820Sjeffvoid
393219820Sjeffieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
394219820Sjeff{
395219820Sjeff	struct ieee80211_node_table *nt;
396219820Sjeff	struct ieee80211_node *ni;
397219820Sjeff
398219820Sjeff	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
399219820Sjeff		"%s: creating ibss\n", __func__);
400219820Sjeff
401219820Sjeff	/*
402219820Sjeff	 * Create the station/neighbor table.  Note that for adhoc
403219820Sjeff	 * mode we make the initial inactivity timer longer since
404219820Sjeff	 * we create nodes only through discovery and they typically
405219820Sjeff	 * are long-lived associations.
406219820Sjeff	 */
407219820Sjeff	nt = &ic->ic_sta;
408219820Sjeff	IEEE80211_NODE_LOCK(nt);
409219820Sjeff	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
410219820Sjeff		nt->nt_name = "station";
411219820Sjeff		nt->nt_inact_init = ic->ic_inact_init;
412219820Sjeff	} else {
413219820Sjeff		nt->nt_name = "neighbor";
414219820Sjeff		nt->nt_inact_init = ic->ic_inact_run;
415219820Sjeff	}
416219820Sjeff	IEEE80211_NODE_UNLOCK(nt);
417219820Sjeff
418219820Sjeff	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
419219820Sjeff	if (ni == NULL) {
420219820Sjeff		/* XXX recovery? */
421219820Sjeff		return;
422219820Sjeff	}
423219820Sjeff	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
424219820Sjeff	ni->ni_esslen = ic->ic_des_esslen;
425219820Sjeff	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
426219820Sjeff	copy_bss(ni, ic->ic_bss);
427219820Sjeff	ni->ni_intval = ic->ic_bintval;
428219820Sjeff	if (ic->ic_flags & IEEE80211_F_PRIVACY)
429219820Sjeff		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
430219820Sjeff	if (ic->ic_phytype == IEEE80211_T_FH) {
431219820Sjeff		ni->ni_fhdwell = 200;	/* XXX */
432219820Sjeff		ni->ni_fhindex = 1;
433219820Sjeff	}
434219820Sjeff	if (ic->ic_opmode == IEEE80211_M_IBSS) {
435219820Sjeff		ic->ic_flags |= IEEE80211_F_SIBSS;
436219820Sjeff		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
437219820Sjeff		if (ic->ic_flags & IEEE80211_F_DESBSSID)
438219820Sjeff			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
439219820Sjeff		else
440219820Sjeff			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
441219820Sjeff	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
442219820Sjeff		if (ic->ic_flags & IEEE80211_F_DESBSSID)
443219820Sjeff			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
444219820Sjeff		else
445219820Sjeff			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
446219820Sjeff	}
447219820Sjeff	/*
448219820Sjeff	 * Fix the channel and related attributes.
449219820Sjeff	 */
450219820Sjeff	ieee80211_set_chan(ic, ni, chan);
451219820Sjeff	ic->ic_curchan = chan;
452219820Sjeff	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
453219820Sjeff	/*
454219820Sjeff	 * Do mode-specific rate setup.
455219820Sjeff	 */
456219820Sjeff	if (ic->ic_curmode == IEEE80211_MODE_11G) {
457219820Sjeff		/*
458219820Sjeff		 * Use a mixed 11b/11g rate set.
459219820Sjeff		 */
460219820Sjeff		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
461219820Sjeff	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
462219820Sjeff		/*
463219820Sjeff		 * Force pure 11b rate set.
464219820Sjeff		 */
465219820Sjeff		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
466219820Sjeff	}
467219820Sjeff
468219820Sjeff	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
469219820Sjeff}
470219820Sjeff
471219820Sjeffvoid
472219820Sjeffieee80211_reset_bss(struct ieee80211com *ic)
473219820Sjeff{
474219820Sjeff	struct ieee80211_node *ni, *obss;
475219820Sjeff
476219820Sjeff	ieee80211_node_table_reset(&ic->ic_scan);
477219820Sjeff	ieee80211_node_table_reset(&ic->ic_sta);
478219820Sjeff
479219820Sjeff	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
480219820Sjeff	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
481219820Sjeff	obss = ic->ic_bss;
482219820Sjeff	ic->ic_bss = ieee80211_ref_node(ni);
483219820Sjeff	if (obss != NULL) {
484219820Sjeff		copy_bss(ni, obss);
485219820Sjeff		ni->ni_intval = ic->ic_bintval;
486219820Sjeff		ieee80211_free_node(obss);
487219820Sjeff	}
488219820Sjeff}
489219820Sjeff
490219820Sjeff/* XXX tunable */
491219820Sjeff#define	STA_FAILS_MAX	2		/* assoc failures before ignored */
492219820Sjeff
493219820Sjeffstatic int
494219820Sjeffieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
495219820Sjeff{
496219820Sjeff        u_int8_t rate;
497219820Sjeff        int fail;
498219820Sjeff
499219820Sjeff	fail = 0;
500219820Sjeff	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
501219820Sjeff		fail |= 0x01;
502219820Sjeff	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
503219820Sjeff	    ni->ni_chan != ic->ic_des_chan)
504219820Sjeff		fail |= 0x01;
505219820Sjeff	if (ic->ic_opmode == IEEE80211_M_IBSS) {
506219820Sjeff		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
507219820Sjeff			fail |= 0x02;
508219820Sjeff	} else {
509219820Sjeff		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
510219820Sjeff			fail |= 0x02;
511219820Sjeff	}
512219820Sjeff	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
513219820Sjeff		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
514219820Sjeff			fail |= 0x04;
515219820Sjeff	} else {
516219820Sjeff		/* XXX does this mean privacy is supported or required? */
517219820Sjeff		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
518219820Sjeff			fail |= 0x04;
519219820Sjeff	}
520219820Sjeff	rate = ieee80211_fix_rate(ni,
521219820Sjeff	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
522219820Sjeff	if (rate & IEEE80211_RATE_BASIC)
523219820Sjeff		fail |= 0x08;
524219820Sjeff	if (ic->ic_des_esslen != 0 &&
525219820Sjeff	    (ni->ni_esslen != ic->ic_des_esslen ||
526219820Sjeff	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
527219820Sjeff		fail |= 0x10;
528219820Sjeff	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
529219820Sjeff	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
530219820Sjeff		fail |= 0x20;
531219820Sjeff	if (ni->ni_fails >= STA_FAILS_MAX)
532219820Sjeff		fail |= 0x40;
533219820Sjeff#ifdef IEEE80211_DEBUG
534219820Sjeff	if (ieee80211_msg_scan(ic)) {
535219820Sjeff		printf(" %c %s",
536219820Sjeff		    fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
537219820Sjeff		    ether_sprintf(ni->ni_macaddr));
538219820Sjeff		printf(" %s%c", ether_sprintf(ni->ni_bssid),
539219820Sjeff		    fail & 0x20 ? '!' : ' ');
540219820Sjeff		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
541219820Sjeff			fail & 0x01 ? '!' : ' ');
542219820Sjeff		printf(" %+4d", ni->ni_rssi);
543219820Sjeff		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
544219820Sjeff		    fail & 0x08 ? '!' : ' ');
545219820Sjeff		printf(" %4s%c",
546219820Sjeff		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
547219820Sjeff		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
548219820Sjeff		    "????",
549219820Sjeff		    fail & 0x02 ? '!' : ' ');
550219820Sjeff		printf(" %3s%c ",
551219820Sjeff		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
552219820Sjeff		    "wep" : "no",
553219820Sjeff		    fail & 0x04 ? '!' : ' ');
554219820Sjeff		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
555219820Sjeff		printf("%s\n", fail & 0x10 ? "!" : "");
556219820Sjeff	}
557219820Sjeff#endif
558219820Sjeff	return fail;
559219820Sjeff}
560219820Sjeff
561219820Sjeffstatic __inline u_int8_t
562219820Sjeffmaxrate(const struct ieee80211_node *ni)
563219820Sjeff{
564219820Sjeff	const struct ieee80211_rateset *rs = &ni->ni_rates;
565219820Sjeff	/* NB: assumes rate set is sorted (happens on frame receive) */
566219820Sjeff	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
567219820Sjeff}
568219820Sjeff
569219820Sjeff/*
570219820Sjeff * Compare the capabilities of two nodes and decide which is
571219820Sjeff * more desirable (return >0 if a is considered better).  Note
572219820Sjeff * that we assume compatibility/usability has already been checked
573219820Sjeff * so we don't need to (e.g. validate whether privacy is supported).
574219820Sjeff * Used to select the best scan candidate for association in a BSS.
575219820Sjeff */
576219820Sjeffstatic int
577219820Sjeffieee80211_node_compare(struct ieee80211com *ic,
578219820Sjeff		       const struct ieee80211_node *a,
579219820Sjeff		       const struct ieee80211_node *b)
580219820Sjeff{
581219820Sjeff	u_int8_t maxa, maxb;
582219820Sjeff	u_int8_t rssia, rssib;
583219820Sjeff	int weight;
584219820Sjeff
585219820Sjeff	/* privacy support preferred */
586219820Sjeff	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
587219820Sjeff	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
588219820Sjeff		return 1;
589219820Sjeff	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
590219820Sjeff	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
591219820Sjeff		return -1;
592219820Sjeff
593219820Sjeff	/* compare count of previous failures */
594219820Sjeff	weight = b->ni_fails - a->ni_fails;
595219820Sjeff	if (abs(weight) > 1)
596219820Sjeff		return weight;
597219820Sjeff
598219820Sjeff	rssia = ic->ic_node_getrssi(a);
599219820Sjeff	rssib = ic->ic_node_getrssi(b);
600219820Sjeff	if (abs(rssib - rssia) < 5) {
601219820Sjeff		/* best/max rate preferred if signal level close enough XXX */
602219820Sjeff		maxa = maxrate(a);
603219820Sjeff		maxb = maxrate(b);
604219820Sjeff		if (maxa != maxb)
605219820Sjeff			return maxa - maxb;
606219820Sjeff		/* XXX use freq for channel preference */
607219820Sjeff		/* for now just prefer 5Ghz band to all other bands */
608219820Sjeff		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
609219820Sjeff		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
610219820Sjeff			return 1;
611219820Sjeff		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
612219820Sjeff		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
613219820Sjeff			return -1;
614219820Sjeff	}
615219820Sjeff	/* all things being equal, use signal level */
616219820Sjeff	return rssia - rssib;
617219820Sjeff}
618219820Sjeff
619219820Sjeff/*
620219820Sjeff * Mark an ongoing scan stopped.
621219820Sjeff */
622219820Sjeffvoid
623219820Sjeffieee80211_cancel_scan(struct ieee80211com *ic)
624219820Sjeff{
625219820Sjeff
626219820Sjeff	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
627219820Sjeff		__func__,
628219820Sjeff		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
629219820Sjeff
630219820Sjeff	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
631219820Sjeff	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
632219820Sjeff}
633219820Sjeff
634219820Sjeff/*
635219820Sjeff * Complete a scan of potential channels.
636219820Sjeff */
637219820Sjeffvoid
638219820Sjeffieee80211_end_scan(struct ieee80211com *ic)
639219820Sjeff{
640219820Sjeff	struct ieee80211_node_table *nt = &ic->ic_scan;
641219820Sjeff	struct ieee80211_node *ni, *selbs;
642219820Sjeff
643219820Sjeff	ieee80211_cancel_scan(ic);
644219820Sjeff	ieee80211_notify_scan_done(ic);
645219820Sjeff
646219820Sjeff	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
647219820Sjeff		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
648219820Sjeff		int i, bestchan;
649219820Sjeff		u_int8_t rssi;
650219820Sjeff
651219820Sjeff		/*
652219820Sjeff		 * The passive scan to look for existing AP's completed,
653219820Sjeff		 * select a channel to camp on.  Identify the channels
654219820Sjeff		 * that already have one or more AP's and try to locate
655219820Sjeff		 * an unoccupied one.  If that fails, pick a channel that
656219820Sjeff		 * looks to be quietest.
657219820Sjeff		 */
658219820Sjeff		memset(maxrssi, 0, sizeof(maxrssi));
659219820Sjeff		IEEE80211_NODE_LOCK(nt);
660219820Sjeff		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
661219820Sjeff			rssi = ic->ic_node_getrssi(ni);
662219820Sjeff			i = ieee80211_chan2ieee(ic, ni->ni_chan);
663219820Sjeff			if (rssi > maxrssi[i])
664219820Sjeff				maxrssi[i] = rssi;
665219820Sjeff		}
666219820Sjeff		IEEE80211_NODE_UNLOCK(nt);
667219820Sjeff		/* XXX select channel more intelligently */
668219820Sjeff		bestchan = -1;
669219820Sjeff		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
670219820Sjeff			if (isset(ic->ic_chan_active, i)) {
671219820Sjeff				/*
672219820Sjeff				 * If the channel is unoccupied the max rssi
673219820Sjeff				 * should be zero; just take it.  Otherwise
674219820Sjeff				 * track the channel with the lowest rssi and
675219820Sjeff				 * use that when all channels appear occupied.
676219820Sjeff				 */
677219820Sjeff				if (maxrssi[i] == 0) {
678219820Sjeff					bestchan = i;
679219820Sjeff					break;
680219820Sjeff				}
681219820Sjeff				if (bestchan == -1 ||
682219820Sjeff				    maxrssi[i] < maxrssi[bestchan])
683219820Sjeff					bestchan = i;
684219820Sjeff			}
685219820Sjeff		if (bestchan != -1) {
686219820Sjeff			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
687219820Sjeff			return;
688219820Sjeff		}
689219820Sjeff		/* no suitable channel, should not happen */
690219820Sjeff	}
691219820Sjeff
692219820Sjeff	/*
693219820Sjeff	 * When manually sequencing the state machine; scan just once
694219820Sjeff	 * regardless of whether we have a candidate or not.  The
695219820Sjeff	 * controlling application is expected to setup state and
696219820Sjeff	 * initiate an association.
697219820Sjeff	 */
698219820Sjeff	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
699219820Sjeff		return;
700219820Sjeff	/*
701219820Sjeff	 * Automatic sequencing; look for a candidate and
702219820Sjeff	 * if found join the network.
703219820Sjeff	 */
704219820Sjeff	/* NB: unlocked read should be ok */
705219820Sjeff	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
706219820Sjeff		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
707219820Sjeff			"%s: no scan candidate\n", __func__);
708219820Sjeff  notfound:
709219820Sjeff		if (ic->ic_opmode == IEEE80211_M_IBSS &&
710219820Sjeff		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
711219820Sjeff		    ic->ic_des_esslen != 0) {
712219820Sjeff			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
713219820Sjeff			return;
714219820Sjeff		}
715219820Sjeff		/*
716219820Sjeff		 * Decrement the failure counts so entries will be
717219820Sjeff		 * reconsidered the next time around.  We really want
718219820Sjeff		 * to do this only for sta's where we've previously
719219820Sjeff		 * had some success.
720219820Sjeff		 */
721219820Sjeff		IEEE80211_NODE_LOCK(nt);
722219820Sjeff		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
723219820Sjeff			if (ni->ni_fails)
724219820Sjeff				ni->ni_fails--;
725219820Sjeff		IEEE80211_NODE_UNLOCK(nt);
726219820Sjeff		/*
727219820Sjeff		 * Reset the list of channels to scan and start again.
728219820Sjeff		 */
729219820Sjeff		ieee80211_reset_scan(ic);
730219820Sjeff		ic->ic_flags |= IEEE80211_F_SCAN;
731219820Sjeff		ieee80211_next_scan(ic);
732219820Sjeff		return;
733219820Sjeff	}
734219820Sjeff	selbs = NULL;
735219820Sjeff	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
736219820Sjeff	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
737219820Sjeff	IEEE80211_NODE_LOCK(nt);
738219820Sjeff	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
739219820Sjeff		if (ieee80211_match_bss(ic, ni) == 0) {
740219820Sjeff			if (selbs == NULL)
741219820Sjeff				selbs = ni;
742219820Sjeff			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
743219820Sjeff				selbs = ni;
744219820Sjeff		}
745219820Sjeff	}
746219820Sjeff	if (selbs != NULL)		/* NB: grab ref while dropping lock */
747219820Sjeff		(void) ieee80211_ref_node(selbs);
748219820Sjeff	IEEE80211_NODE_UNLOCK(nt);
749219820Sjeff	if (selbs == NULL)
750219820Sjeff		goto notfound;
751219820Sjeff	if (!ieee80211_sta_join(ic, selbs)) {
752219820Sjeff		ieee80211_free_node(selbs);
753219820Sjeff		goto notfound;
754219820Sjeff	}
755219820Sjeff}
756219820Sjeff
757219820Sjeff/*
758219820Sjeff * Handle 802.11 ad hoc network merge.  The
759219820Sjeff * convention, set by the Wireless Ethernet Compatibility Alliance
760219820Sjeff * (WECA), is that an 802.11 station will change its BSSID to match
761219820Sjeff * the "oldest" 802.11 ad hoc network, on the same channel, that
762219820Sjeff * has the station's desired SSID.  The "oldest" 802.11 network
763219820Sjeff * sends beacons with the greatest TSF timestamp.
764219820Sjeff *
765219820Sjeff * The caller is assumed to validate TSF's before attempting a merge.
766219820Sjeff *
767219820Sjeff * Return !0 if the BSSID changed, 0 otherwise.
768219820Sjeff */
769219820Sjeffint
770219820Sjeffieee80211_ibss_merge(struct ieee80211_node *ni)
771219820Sjeff{
772219820Sjeff	struct ieee80211com *ic = ni->ni_ic;
773219820Sjeff
774219820Sjeff	if (ni == ic->ic_bss ||
775219820Sjeff	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
776219820Sjeff		/* unchanged, nothing to do */
777219820Sjeff		return 0;
778219820Sjeff	}
779219820Sjeff	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
780219820Sjeff		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
781219820Sjeff		    "%s: merge failed, capabilities mismatch\n", __func__);
782219820Sjeff		ic->ic_stats.is_ibss_capmismatch++;
783219820Sjeff		return 0;
784219820Sjeff	}
785219820Sjeff	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
786219820Sjeff		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
787219820Sjeff		ether_sprintf(ni->ni_bssid),
788219820Sjeff		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
789219820Sjeff		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
790219820Sjeff		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
791219820Sjeff	);
792219820Sjeff	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
793219820Sjeff}
794219820Sjeff
795219820Sjeff/*
796219820Sjeff * Join the specified IBSS/BSS network.  The node is assumed to
797219820Sjeff * be passed in with a held reference.
798219820Sjeff */
799219820Sjeffint
800219820Sjeffieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
801219820Sjeff{
802219820Sjeff	struct ieee80211_node *obss;
803219820Sjeff
804219820Sjeff	if (ic->ic_opmode == IEEE80211_M_IBSS) {
805219820Sjeff		struct ieee80211_node_table *nt;
806219820Sjeff		/*
807219820Sjeff		 * Fillin the neighbor table; it will already
808219820Sjeff		 * exist if we are simply switching mastership.
809219820Sjeff		 * XXX ic_sta always setup so this is unnecessary?
810219820Sjeff		 */
811219820Sjeff		nt = &ic->ic_sta;
812219820Sjeff		IEEE80211_NODE_LOCK(nt);
813219820Sjeff		nt->nt_name = "neighbor";
814219820Sjeff		nt->nt_inact_init = ic->ic_inact_run;
815219820Sjeff		IEEE80211_NODE_UNLOCK(nt);
816219820Sjeff	}
817219820Sjeff
818219820Sjeff	/*
819219820Sjeff	 * Committed to selbs, setup state.
820219820Sjeff	 */
821219820Sjeff	obss = ic->ic_bss;
822219820Sjeff	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
823219820Sjeff	if (obss != NULL) {
824219820Sjeff		copy_bss(selbs, obss);
825219820Sjeff		ieee80211_free_node(obss);
826219820Sjeff	}
827219820Sjeff
828219820Sjeff	/*
829219820Sjeff	 * Delete unusable rates; we've already checked
830219820Sjeff	 * that the negotiated rate set is acceptable.
831219820Sjeff	 */
832219820Sjeff	ieee80211_fix_rate(ic->ic_bss, IEEE80211_F_DODEL | IEEE80211_F_JOIN);
833219820Sjeff
834219820Sjeff	/*
835219820Sjeff	 * Set the erp state (mostly the slot time) to deal with
836219820Sjeff	 * the auto-select case; this should be redundant if the
837219820Sjeff	 * mode is locked.
838219820Sjeff	 */
839219820Sjeff	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
840219820Sjeff	ic->ic_curchan = selbs->ni_chan;
841219820Sjeff	ieee80211_reset_erp(ic);
842219820Sjeff	ieee80211_wme_initparams(ic);
843219820Sjeff
844219820Sjeff	if (ic->ic_opmode == IEEE80211_M_STA)
845219820Sjeff		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
846219820Sjeff	else
847219820Sjeff		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
848219820Sjeff	return 1;
849219820Sjeff}
850219820Sjeff
851219820Sjeff/*
852219820Sjeff * Leave the specified IBSS/BSS network.  The node is assumed to
853219820Sjeff * be passed in with a held reference.
854219820Sjeff */
855219820Sjeffvoid
856219820Sjeffieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
857219820Sjeff{
858219820Sjeff	ic->ic_node_cleanup(ni);
859219820Sjeff	ieee80211_notify_node_leave(ic, ni);
860219820Sjeff}
861219820Sjeff
862219820Sjeffstatic struct ieee80211_node *
863219820Sjeffnode_alloc(struct ieee80211_node_table *nt)
864219820Sjeff{
865219820Sjeff	struct ieee80211_node *ni;
866219820Sjeff
867219820Sjeff	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
868219820Sjeff		M_80211_NODE, M_NOWAIT | M_ZERO);
869219820Sjeff	return ni;
870219820Sjeff}
871219820Sjeff
872219820Sjeff/*
873219820Sjeff * Reclaim any resources in a node and reset any critical
874219820Sjeff * state.  Typically nodes are free'd immediately after,
875219820Sjeff * but in some cases the storage may be reused so we need
876219820Sjeff * to insure consistent state (should probably fix that).
877219820Sjeff */
878219820Sjeffstatic void
879219820Sjeffnode_cleanup(struct ieee80211_node *ni)
880219820Sjeff{
881219820Sjeff#define	N(a)	(sizeof(a)/sizeof(a[0]))
882219820Sjeff	struct ieee80211com *ic = ni->ni_ic;
883219820Sjeff	int i, qlen;
884219820Sjeff
885219820Sjeff	/* NB: preserve ni_table */
886219820Sjeff	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
887219820Sjeff		ic->ic_ps_sta--;
888219820Sjeff		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
889219820Sjeff		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
890219820Sjeff		    "[%s] power save mode off, %u sta's in ps mode\n",
891219820Sjeff		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
892219820Sjeff	}
893219820Sjeff	/*
894219820Sjeff	 * Clear AREF flag that marks the authorization refcnt bump
895219820Sjeff	 * has happened.  This is probably not needed as the node
896219820Sjeff	 * should always be removed from the table so not found but
897219820Sjeff	 * do it just in case.
898219820Sjeff	 */
899219820Sjeff	ni->ni_flags &= ~IEEE80211_NODE_AREF;
900219820Sjeff
901219820Sjeff	/*
902219820Sjeff	 * Drain power save queue and, if needed, clear TIM.
903219820Sjeff	 */
904219820Sjeff	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
905219820Sjeff	if (qlen != 0 && ic->ic_set_tim != NULL)
906219820Sjeff		ic->ic_set_tim(ni, 0);
907219820Sjeff
908219820Sjeff	ni->ni_associd = 0;
909219820Sjeff	if (ni->ni_challenge != NULL) {
910219820Sjeff		FREE(ni->ni_challenge, M_DEVBUF);
911219820Sjeff		ni->ni_challenge = NULL;
912219820Sjeff	}
913219820Sjeff	/*
914219820Sjeff	 * Preserve SSID, WPA, and WME ie's so the bss node is
915219820Sjeff	 * reusable during a re-auth/re-assoc state transition.
916219820Sjeff	 * If we remove these data they will not be recreated
917219820Sjeff	 * because they come from a probe-response or beacon frame
918219820Sjeff	 * which cannot be expected prior to the association-response.
919219820Sjeff	 * This should not be an issue when operating in other modes
920219820Sjeff	 * as stations leaving always go through a full state transition
921219820Sjeff	 * which will rebuild this state.
922219820Sjeff	 *
923219820Sjeff	 * XXX does this leave us open to inheriting old state?
924219820Sjeff	 */
925219820Sjeff	for (i = 0; i < N(ni->ni_rxfrag); i++)
926219820Sjeff		if (ni->ni_rxfrag[i] != NULL) {
927219820Sjeff			m_freem(ni->ni_rxfrag[i]);
928219820Sjeff			ni->ni_rxfrag[i] = NULL;
929219820Sjeff		}
930219820Sjeff	/*
931219820Sjeff	 * Must be careful here to remove any key map entry w/o a LOR.
932219820Sjeff	 */
933219820Sjeff	ieee80211_node_delucastkey(ni);
934219820Sjeff#undef N
935219820Sjeff}
936219820Sjeff
937219820Sjeffstatic void
938219820Sjeffnode_free(struct ieee80211_node *ni)
939219820Sjeff{
940219820Sjeff	struct ieee80211com *ic = ni->ni_ic;
941219820Sjeff
942219820Sjeff	ic->ic_node_cleanup(ni);
943219820Sjeff	if (ni->ni_wpa_ie != NULL)
944219820Sjeff		FREE(ni->ni_wpa_ie, M_DEVBUF);
945219820Sjeff	if (ni->ni_wme_ie != NULL)
946219820Sjeff		FREE(ni->ni_wme_ie, M_DEVBUF);
947219820Sjeff	IEEE80211_NODE_SAVEQ_DESTROY(ni);
948219820Sjeff	FREE(ni, M_80211_NODE);
949219820Sjeff}
950219820Sjeff
951219820Sjeffstatic u_int8_t
952219820Sjeffnode_getrssi(const struct ieee80211_node *ni)
953219820Sjeff{
954219820Sjeff	return ni->ni_rssi;
955219820Sjeff}
956219820Sjeff
957219820Sjeffstatic void
958219820Sjeffieee80211_setup_node(struct ieee80211_node_table *nt,
959219820Sjeff	struct ieee80211_node *ni, const u_int8_t *macaddr)
960219820Sjeff{
961219820Sjeff	struct ieee80211com *ic = nt->nt_ic;
962219820Sjeff	int hash;
963219820Sjeff
964219820Sjeff	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
965219820Sjeff		"%s %p<%s> in %s table\n", __func__, ni,
966219820Sjeff		ether_sprintf(macaddr), nt->nt_name);
967219820Sjeff
968219820Sjeff	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
969219820Sjeff	hash = IEEE80211_NODE_HASH(macaddr);
970219820Sjeff	ieee80211_node_initref(ni);		/* mark referenced */
971219820Sjeff	ni->ni_chan = IEEE80211_CHAN_ANYC;
972219820Sjeff	ni->ni_authmode = IEEE80211_AUTH_OPEN;
973219820Sjeff	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
974219820Sjeff	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
975219820Sjeff	ni->ni_inact_reload = nt->nt_inact_init;
976219820Sjeff	ni->ni_inact = ni->ni_inact_reload;
977219820Sjeff	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
978219820Sjeff
979219820Sjeff	IEEE80211_NODE_LOCK(nt);
980219820Sjeff	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
981219820Sjeff	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
982219820Sjeff	ni->ni_table = nt;
983219820Sjeff	ni->ni_ic = ic;
984219820Sjeff	IEEE80211_NODE_UNLOCK(nt);
985219820Sjeff}
986219820Sjeff
987219820Sjeffstruct ieee80211_node *
988219820Sjeffieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
989219820Sjeff{
990219820Sjeff	struct ieee80211com *ic = nt->nt_ic;
991219820Sjeff	struct ieee80211_node *ni;
992219820Sjeff
993219820Sjeff	ni = ic->ic_node_alloc(nt);
994219820Sjeff	if (ni != NULL)
995219820Sjeff		ieee80211_setup_node(nt, ni, macaddr);
996219820Sjeff	else
997219820Sjeff		ic->ic_stats.is_rx_nodealloc++;
998219820Sjeff	return ni;
999219820Sjeff}
1000219820Sjeff
1001219820Sjeff/*
1002219820Sjeff * Craft a temporary node suitable for sending a management frame
1003219820Sjeff * to the specified station.  We craft only as much state as we
1004219820Sjeff * need to do the work since the node will be immediately reclaimed
1005219820Sjeff * once the send completes.
1006219820Sjeff */
1007219820Sjeffstruct ieee80211_node *
1008219820Sjeffieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
1009219820Sjeff{
1010219820Sjeff	struct ieee80211_node *ni;
1011219820Sjeff
1012219820Sjeff	ni = ic->ic_node_alloc(&ic->ic_sta);
1013219820Sjeff	if (ni != NULL) {
1014219820Sjeff		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1015219820Sjeff			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1016219820Sjeff
1017219820Sjeff		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1018219820Sjeff		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1019219820Sjeff		ieee80211_node_initref(ni);		/* mark referenced */
1020219820Sjeff		ni->ni_txpower = ic->ic_bss->ni_txpower;
1021219820Sjeff		/* NB: required by ieee80211_fix_rate */
1022219820Sjeff		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1023219820Sjeff		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
1024219820Sjeff			IEEE80211_KEYIX_NONE);
1025219820Sjeff		/* XXX optimize away */
1026219820Sjeff		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1027219820Sjeff
1028219820Sjeff		ni->ni_table = NULL;		/* NB: pedantic */
1029219820Sjeff		ni->ni_ic = ic;
1030219820Sjeff	} else {
1031219820Sjeff		/* XXX msg */
1032219820Sjeff		ic->ic_stats.is_rx_nodealloc++;
1033219820Sjeff	}
1034219820Sjeff	return ni;
1035219820Sjeff}
1036219820Sjeff
1037219820Sjeffstruct ieee80211_node *
1038219820Sjeffieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1039219820Sjeff{
1040219820Sjeff	struct ieee80211com *ic = nt->nt_ic;
1041219820Sjeff	struct ieee80211_node *ni;
1042219820Sjeff
1043219820Sjeff	ni = ic->ic_node_alloc(nt);
1044219820Sjeff	if (ni != NULL) {
1045219820Sjeff		ieee80211_setup_node(nt, ni, macaddr);
1046219820Sjeff		/*
1047219820Sjeff		 * Inherit from ic_bss.
1048219820Sjeff		 */
1049219820Sjeff		ni->ni_authmode = ic->ic_bss->ni_authmode;
1050219820Sjeff		ni->ni_txpower = ic->ic_bss->ni_txpower;
1051219820Sjeff		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1052219820Sjeff		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1053219820Sjeff		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1054219820Sjeff		ni->ni_rsn = ic->ic_bss->ni_rsn;
1055219820Sjeff	} else
1056219820Sjeff		ic->ic_stats.is_rx_nodealloc++;
1057219820Sjeff	return ni;
1058219820Sjeff}
1059219820Sjeff
1060219820Sjeffstatic struct ieee80211_node *
1061219820Sjeff#ifdef IEEE80211_DEBUG_REFCNT
1062219820Sjeff_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1063219820Sjeff	const u_int8_t *macaddr, const char *func, int line)
1064219820Sjeff#else
1065219820Sjeff_ieee80211_find_node(struct ieee80211_node_table *nt,
1066219820Sjeff	const u_int8_t *macaddr)
1067219820Sjeff#endif
1068219820Sjeff{
1069219820Sjeff	struct ieee80211_node *ni;
1070219820Sjeff	int hash;
1071219820Sjeff
1072219820Sjeff	IEEE80211_NODE_LOCK_ASSERT(nt);
1073219820Sjeff
1074219820Sjeff	hash = IEEE80211_NODE_HASH(macaddr);
1075219820Sjeff	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1076219820Sjeff		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1077219820Sjeff			ieee80211_ref_node(ni);	/* mark referenced */
1078219820Sjeff#ifdef IEEE80211_DEBUG_REFCNT
1079219820Sjeff			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1080219820Sjeff			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1081219820Sjeff			    func, line,
1082219820Sjeff			    ni, ether_sprintf(ni->ni_macaddr),
1083219820Sjeff			    ieee80211_node_refcnt(ni));
1084219820Sjeff#endif
1085219820Sjeff			return ni;
1086219820Sjeff		}
1087219820Sjeff	}
1088219820Sjeff	return NULL;
1089219820Sjeff}
1090219820Sjeff#ifdef IEEE80211_DEBUG_REFCNT
1091219820Sjeff#define	_ieee80211_find_node(nt, mac) \
1092219820Sjeff	_ieee80211_find_node_debug(nt, mac, func, line)
1093219820Sjeff#endif
1094219820Sjeff
1095219820Sjeffstruct ieee80211_node *
1096219820Sjeff#ifdef IEEE80211_DEBUG_REFCNT
1097219820Sjeffieee80211_find_node_debug(struct ieee80211_node_table *nt,
1098219820Sjeff	const u_int8_t *macaddr, const char *func, int line)
1099219820Sjeff#else
1100219820Sjeffieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1101219820Sjeff#endif
1102219820Sjeff{
1103219820Sjeff	struct ieee80211_node *ni;
1104219820Sjeff
1105219820Sjeff	IEEE80211_NODE_LOCK(nt);
1106219820Sjeff	ni = _ieee80211_find_node(nt, macaddr);
1107219820Sjeff	IEEE80211_NODE_UNLOCK(nt);
1108219820Sjeff	return ni;
1109219820Sjeff}
1110219820Sjeff
1111219820Sjeff/*
1112219820Sjeff * Fake up a node; this handles node discovery in adhoc mode.
1113219820Sjeff * Note that for the driver's benefit we we treat this like
1114219820Sjeff * an association so the driver has an opportunity to setup
1115219820Sjeff * it's private state.
1116219820Sjeff */
1117219820Sjeffstruct ieee80211_node *
1118219820Sjeffieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1119219820Sjeff	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1120219820Sjeff{
1121219820Sjeff	struct ieee80211com *ic = nt->nt_ic;
1122219820Sjeff	struct ieee80211_node *ni;
1123219820Sjeff
1124219820Sjeff	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1125219820Sjeff	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1126219820Sjeff	ni = ieee80211_dup_bss(nt, macaddr);
1127219820Sjeff	if (ni != NULL) {
1128219820Sjeff		/* XXX no rate negotiation; just dup */
1129219820Sjeff		ni->ni_rates = ic->ic_bss->ni_rates;
1130219820Sjeff		if (ic->ic_newassoc != NULL)
1131219820Sjeff			ic->ic_newassoc(ni, 1);
1132219820Sjeff		/* XXX not right for 802.1x/WPA */
1133219820Sjeff		ieee80211_node_authorize(ni);
1134219820Sjeff		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
1135219820Sjeff			/*
1136219820Sjeff			 * Blindly propagate capabilities based on the
1137219820Sjeff			 * local configuration.  In particular this permits
1138219820Sjeff			 * us to use QoS to disable ACK's.
1139219820Sjeff			 */
1140219820Sjeff			if (ic->ic_flags & IEEE80211_F_WME)
1141219820Sjeff				ni->ni_flags |= IEEE80211_NODE_QOS;
1142219820Sjeff		}
1143219820Sjeff	}
1144219820Sjeff	return ni;
1145219820Sjeff}
1146219820Sjeff
1147219820Sjeff#ifdef IEEE80211_DEBUG
1148219820Sjeffstatic void
1149219820Sjeffdump_probe_beacon(u_int8_t subtype, int isnew,
1150219820Sjeff	const u_int8_t mac[IEEE80211_ADDR_LEN],
1151219820Sjeff	const struct ieee80211_scanparams *sp)
1152219820Sjeff{
1153219820Sjeff
1154219820Sjeff	printf("[%s] %s%s on chan %u (bss chan %u) ",
1155219820Sjeff	    ether_sprintf(mac), isnew ? "new " : "",
1156219820Sjeff	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1157219820Sjeff	    sp->chan, sp->bchan);
1158219820Sjeff	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1159219820Sjeff	printf("\n");
1160219820Sjeff
1161219820Sjeff	if (isnew) {
1162219820Sjeff		printf("[%s] caps 0x%x bintval %u erp 0x%x",
1163219820Sjeff			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1164219820Sjeff		if (sp->country != NULL) {
1165219820Sjeff#ifdef __FreeBSD__
1166219820Sjeff			printf(" country info %*D",
1167219820Sjeff				sp->country[1], sp->country+2, " ");
1168219820Sjeff#else
1169219820Sjeff			int i;
1170219820Sjeff			printf(" country info");
1171219820Sjeff			for (i = 0; i < sp->country[1]; i++)
1172219820Sjeff				printf(" %02x", sp->country[i+2]);
1173219820Sjeff#endif
1174219820Sjeff		}
1175219820Sjeff		printf("\n");
1176219820Sjeff	}
1177219820Sjeff}
1178219820Sjeff#endif /* IEEE80211_DEBUG */
1179219820Sjeff
1180219820Sjeffstatic void
1181219820Sjeffsaveie(u_int8_t **iep, const u_int8_t *ie)
1182219820Sjeff{
1183219820Sjeff
1184219820Sjeff	if (ie == NULL)
1185219820Sjeff		*iep = NULL;
1186219820Sjeff	else
1187219820Sjeff		ieee80211_saveie(iep, ie);
1188219820Sjeff}
1189219820Sjeff
1190219820Sjeff/*
1191219820Sjeff * Process a beacon or probe response frame.
1192219820Sjeff */
1193219820Sjeffvoid
1194219820Sjeffieee80211_add_scan(struct ieee80211com *ic,
1195219820Sjeff	const struct ieee80211_scanparams *sp,
1196219820Sjeff	const struct ieee80211_frame *wh,
1197219820Sjeff	int subtype, int rssi, int rstamp)
1198219820Sjeff{
1199219820Sjeff#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1200219820Sjeff	struct ieee80211_node_table *nt = &ic->ic_scan;
1201219820Sjeff	struct ieee80211_node *ni;
1202219820Sjeff	int newnode = 0;
1203219820Sjeff
1204219820Sjeff	ni = ieee80211_find_node(nt, wh->i_addr2);
1205219820Sjeff	if (ni == NULL) {
1206219820Sjeff		/*
1207219820Sjeff		 * Create a new entry.
1208219820Sjeff		 */
1209219820Sjeff		ni = ic->ic_node_alloc(nt);
1210219820Sjeff		if (ni == NULL) {
1211219820Sjeff			ic->ic_stats.is_rx_nodealloc++;
1212219820Sjeff			return;
1213219820Sjeff		}
1214219820Sjeff		ieee80211_setup_node(nt, ni, wh->i_addr2);
1215219820Sjeff		/*
1216219820Sjeff		 * XXX inherit from ic_bss.
1217219820Sjeff		 */
1218219820Sjeff		ni->ni_authmode = ic->ic_bss->ni_authmode;
1219219820Sjeff		ni->ni_txpower = ic->ic_bss->ni_txpower;
1220219820Sjeff		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1221219820Sjeff		ieee80211_set_chan(ic, ni, ic->ic_curchan);
1222219820Sjeff		ni->ni_rsn = ic->ic_bss->ni_rsn;
1223219820Sjeff		newnode = 1;
1224219820Sjeff	}
1225219820Sjeff#ifdef IEEE80211_DEBUG
1226219820Sjeff	if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
1227219820Sjeff		dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
1228219820Sjeff#endif
1229219820Sjeff	/* XXX ap beaconing multiple ssid w/ same bssid */
1230219820Sjeff	if (sp->ssid[1] != 0 &&
1231219820Sjeff	    (ISPROBE(subtype) || ni->ni_esslen == 0)) {
1232219820Sjeff		ni->ni_esslen = sp->ssid[1];
1233219820Sjeff		memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
1234219820Sjeff		memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1235219820Sjeff	}
1236219820Sjeff	ni->ni_scangen = ic->ic_scan.nt_scangen;
1237219820Sjeff	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1238219820Sjeff	ni->ni_rssi = rssi;
1239219820Sjeff	ni->ni_rstamp = rstamp;
1240219820Sjeff	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1241219820Sjeff	ni->ni_intval = sp->bintval;
1242219820Sjeff	ni->ni_capinfo = sp->capinfo;
1243219820Sjeff	ni->ni_chan = &ic->ic_channels[sp->chan];
1244219820Sjeff	ni->ni_fhdwell = sp->fhdwell;
1245219820Sjeff	ni->ni_fhindex = sp->fhindex;
1246219820Sjeff	ni->ni_erp = sp->erp;
1247219820Sjeff	if (sp->tim != NULL) {
1248219820Sjeff		struct ieee80211_tim_ie *ie =
1249219820Sjeff		    (struct ieee80211_tim_ie *) sp->tim;
1250219820Sjeff
1251219820Sjeff		ni->ni_dtim_count = ie->tim_count;
1252219820Sjeff		ni->ni_dtim_period = ie->tim_period;
1253219820Sjeff	}
1254219820Sjeff	/*
1255219820Sjeff	 * Record the byte offset from the mac header to
1256219820Sjeff	 * the start of the TIM information element for
1257219820Sjeff	 * use by hardware and/or to speedup software
1258219820Sjeff	 * processing of beacon frames.
1259219820Sjeff	 */
1260219820Sjeff	ni->ni_timoff = sp->timoff;
1261219820Sjeff	/*
1262219820Sjeff	 * Record optional information elements that might be
1263219820Sjeff	 * used by applications or drivers.
1264219820Sjeff	 */
1265219820Sjeff	saveie(&ni->ni_wme_ie, sp->wme);
1266219820Sjeff	saveie(&ni->ni_wpa_ie, sp->wpa);
1267219820Sjeff
1268219820Sjeff	/* NB: must be after ni_chan is setup */
1269219820Sjeff	ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1270219820Sjeff
1271219820Sjeff	if (!newnode)
1272219820Sjeff		ieee80211_free_node(ni);
1273219820Sjeff#undef ISPROBE
1274219820Sjeff}
1275219820Sjeff
1276219820Sjeffvoid
1277219820Sjeffieee80211_init_neighbor(struct ieee80211_node *ni,
1278219820Sjeff	const struct ieee80211_frame *wh,
1279219820Sjeff	const struct ieee80211_scanparams *sp)
1280219820Sjeff{
1281219820Sjeff
1282219820Sjeff	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1283219820Sjeff	    "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr));
1284219820Sjeff	ni->ni_esslen = sp->ssid[1];
1285219820Sjeff	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1286219820Sjeff	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1287219820Sjeff	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1288219820Sjeff	ni->ni_intval = sp->bintval;
1289219820Sjeff	ni->ni_capinfo = sp->capinfo;
1290219820Sjeff	ni->ni_chan = ni->ni_ic->ic_curchan;
1291219820Sjeff	ni->ni_fhdwell = sp->fhdwell;
1292219820Sjeff	ni->ni_fhindex = sp->fhindex;
1293219820Sjeff	ni->ni_erp = sp->erp;
1294219820Sjeff	ni->ni_timoff = sp->timoff;
1295219820Sjeff	if (sp->wme != NULL)
1296219820Sjeff		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1297219820Sjeff	if (sp->wpa != NULL)
1298219820Sjeff		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1299219820Sjeff
1300219820Sjeff	/* NB: must be after ni_chan is setup */
1301219820Sjeff	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1302219820Sjeff		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1303219820Sjeff		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1304219820Sjeff}
1305219820Sjeff
1306219820Sjeff/*
1307219820Sjeff * Do node discovery in adhoc mode on receipt of a beacon
1308219820Sjeff * or probe response frame.  Note that for the driver's
1309219820Sjeff * benefit we we treat this like an association so the
1310219820Sjeff * driver has an opportunity to setup it's private state.
1311219820Sjeff */
1312219820Sjeffstruct ieee80211_node *
1313219820Sjeffieee80211_add_neighbor(struct ieee80211com *ic,
1314219820Sjeff	const struct ieee80211_frame *wh,
1315219820Sjeff	const struct ieee80211_scanparams *sp)
1316219820Sjeff{
1317219820Sjeff	struct ieee80211_node *ni;
1318219820Sjeff
1319219820Sjeff	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1320219820Sjeff	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1321219820Sjeff	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1322219820Sjeff	if (ni != NULL) {
1323219820Sjeff		ieee80211_init_neighbor(ni, wh, sp);
1324219820Sjeff		if (ic->ic_newassoc != NULL)
1325219820Sjeff			ic->ic_newassoc(ni, 1);
1326219820Sjeff		/* XXX not right for 802.1x/WPA */
1327219820Sjeff		ieee80211_node_authorize(ni);
1328219820Sjeff	}
1329219820Sjeff	return ni;
1330219820Sjeff}
1331219820Sjeff
1332219820Sjeff#define	IS_CTL(wh) \
1333219820Sjeff	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1334219820Sjeff#define	IS_PSPOLL(wh) \
1335219820Sjeff	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1336219820Sjeff/*
1337219820Sjeff * Locate the node for sender, track state, and then pass the
1338 * (referenced) node up to the 802.11 layer for its use.  We
1339 * are required to pass some node so we fall back to ic_bss
1340 * when this frame is from an unknown sender.  The 802.11 layer
1341 * knows this means the sender wasn't in the node table and
1342 * acts accordingly.
1343 */
1344struct ieee80211_node *
1345#ifdef IEEE80211_DEBUG_REFCNT
1346ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1347	const struct ieee80211_frame_min *wh, const char *func, int line)
1348#else
1349ieee80211_find_rxnode(struct ieee80211com *ic,
1350	const struct ieee80211_frame_min *wh)
1351#endif
1352{
1353	struct ieee80211_node_table *nt;
1354	struct ieee80211_node *ni;
1355
1356	/* XXX may want scanned nodes in the neighbor table for adhoc */
1357	if (ic->ic_opmode == IEEE80211_M_STA ||
1358	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1359	    (ic->ic_flags & IEEE80211_F_SCAN))
1360		nt = &ic->ic_scan;
1361	else
1362		nt = &ic->ic_sta;
1363	/* XXX check ic_bss first in station mode */
1364	/* XXX 4-address frames? */
1365	IEEE80211_NODE_LOCK(nt);
1366	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1367		ni = _ieee80211_find_node(nt, wh->i_addr1);
1368	else
1369		ni = _ieee80211_find_node(nt, wh->i_addr2);
1370	if (ni == NULL)
1371		ni = ieee80211_ref_node(ic->ic_bss);
1372	IEEE80211_NODE_UNLOCK(nt);
1373
1374	return ni;
1375}
1376
1377/*
1378 * Like ieee80211_find_rxnode but use the supplied h/w
1379 * key index as a hint to locate the node in the key
1380 * mapping table.  If an entry is present at the key
1381 * index we return it; otherwise do a normal lookup and
1382 * update the mapping table if the station has a unicast
1383 * key assigned to it.
1384 */
1385struct ieee80211_node *
1386#ifdef IEEE80211_DEBUG_REFCNT
1387ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1388	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1389	const char *func, int line)
1390#else
1391ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1392	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1393#endif
1394{
1395	struct ieee80211_node_table *nt;
1396	struct ieee80211_node *ni;
1397
1398	if (ic->ic_opmode == IEEE80211_M_STA ||
1399	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1400	    (ic->ic_flags & IEEE80211_F_SCAN))
1401		nt = &ic->ic_scan;
1402	else
1403		nt = &ic->ic_sta;
1404	IEEE80211_NODE_LOCK(nt);
1405	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1406		ni = nt->nt_keyixmap[keyix];
1407	else
1408		ni = NULL;
1409	if (ni == NULL) {
1410		if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1411			ni = _ieee80211_find_node(nt, wh->i_addr1);
1412		else
1413			ni = _ieee80211_find_node(nt, wh->i_addr2);
1414		if (ni == NULL)
1415			ni = ieee80211_ref_node(ic->ic_bss);
1416		if (nt->nt_keyixmap != NULL) {
1417			/*
1418			 * If the station has a unicast key cache slot
1419			 * assigned update the key->node mapping table.
1420			 */
1421			keyix = ni->ni_ucastkey.wk_rxkeyix;
1422			/* XXX can keyixmap[keyix] != NULL? */
1423			if (keyix < nt->nt_keyixmax &&
1424			    nt->nt_keyixmap[keyix] == NULL) {
1425				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1426				    "%s: add key map entry %p<%s> refcnt %d\n",
1427				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1428				    ieee80211_node_refcnt(ni)+1);
1429				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1430			}
1431		}
1432	} else {
1433		ieee80211_ref_node(ni);
1434	}
1435	IEEE80211_NODE_UNLOCK(nt);
1436
1437	return ni;
1438}
1439#undef IS_PSPOLL
1440#undef IS_CTL
1441
1442/*
1443 * Return a reference to the appropriate node for sending
1444 * a data frame.  This handles node discovery in adhoc networks.
1445 */
1446struct ieee80211_node *
1447#ifdef IEEE80211_DEBUG_REFCNT
1448ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1449	const char *func, int line)
1450#else
1451ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1452#endif
1453{
1454	struct ieee80211_node_table *nt = &ic->ic_sta;
1455	struct ieee80211_node *ni;
1456
1457	/*
1458	 * The destination address should be in the node table
1459	 * unless this is a multicast/broadcast frame.  We can
1460	 * also optimize station mode operation, all frames go
1461	 * to the bss node.
1462	 */
1463	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1464	IEEE80211_NODE_LOCK(nt);
1465	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1466		ni = ieee80211_ref_node(ic->ic_bss);
1467	else {
1468		ni = _ieee80211_find_node(nt, macaddr);
1469		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1470		    (ni != NULL && ni->ni_associd == 0)) {
1471			/*
1472			 * Station is not associated; don't permit the
1473			 * data frame to be sent by returning NULL.  This
1474			 * is kinda a kludge but the least intrusive way
1475			 * to add this check into all drivers.
1476			 */
1477			ieee80211_unref_node(&ni);	/* NB: null's ni */
1478		}
1479	}
1480	IEEE80211_NODE_UNLOCK(nt);
1481
1482	if (ni == NULL) {
1483		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1484		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
1485			/*
1486			 * In adhoc mode cons up a node for the destination.
1487			 * Note that we need an additional reference for the
1488			 * caller to be consistent with _ieee80211_find_node.
1489			 */
1490			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1491			if (ni != NULL)
1492				(void) ieee80211_ref_node(ni);
1493		} else {
1494			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1495				"[%s] no node, discard frame (%s)\n",
1496				ether_sprintf(macaddr), __func__);
1497			ic->ic_stats.is_tx_nonode++;
1498		}
1499	}
1500	return ni;
1501}
1502
1503/*
1504 * Like find but search based on the channel too.
1505 */
1506struct ieee80211_node *
1507#ifdef IEEE80211_DEBUG_REFCNT
1508ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1509	const u_int8_t *macaddr, struct ieee80211_channel *chan,
1510	const char *func, int line)
1511#else
1512ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1513	const u_int8_t *macaddr, struct ieee80211_channel *chan)
1514#endif
1515{
1516	struct ieee80211_node *ni;
1517	int hash;
1518
1519	hash = IEEE80211_NODE_HASH(macaddr);
1520	IEEE80211_NODE_LOCK(nt);
1521	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1522		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1523		    ni->ni_chan == chan) {
1524			ieee80211_ref_node(ni);		/* mark referenced */
1525			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1526			    REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
1527			    ieee80211_node_refcnt(ni));
1528			break;
1529		}
1530	}
1531	IEEE80211_NODE_UNLOCK(nt);
1532	return ni;
1533}
1534
1535/*
1536 * Like find but search based on the ssid too.
1537 */
1538struct ieee80211_node *
1539#ifdef IEEE80211_DEBUG_REFCNT
1540ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1541	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1542	const char *func, int line)
1543#else
1544ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1545	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1546#endif
1547{
1548#define	MATCH_SSID(ni, ssid, ssidlen) \
1549	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1550	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
1551	struct ieee80211com *ic = nt->nt_ic;
1552	struct ieee80211_node *ni;
1553	int hash;
1554
1555	IEEE80211_NODE_LOCK(nt);
1556	/*
1557	 * A mac address that is all zero means match only the ssid;
1558	 * otherwise we must match both.
1559	 */
1560	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1561		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1562			if (MATCH_SSID(ni, ssid, ssidlen))
1563				break;
1564		}
1565	} else {
1566		hash = IEEE80211_NODE_HASH(macaddr);
1567		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1568			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1569			    MATCH_SSID(ni, ssid, ssidlen))
1570				break;
1571		}
1572	}
1573	if (ni != NULL) {
1574		ieee80211_ref_node(ni);	/* mark referenced */
1575		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1576		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
1577		     ieee80211_node_refcnt(ni));
1578	}
1579	IEEE80211_NODE_UNLOCK(nt);
1580	return ni;
1581#undef MATCH_SSID
1582}
1583
1584static void
1585_ieee80211_free_node(struct ieee80211_node *ni)
1586{
1587	struct ieee80211com *ic = ni->ni_ic;
1588	struct ieee80211_node_table *nt = ni->ni_table;
1589
1590	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1591		"%s %p<%s> in %s table\n", __func__, ni,
1592		ether_sprintf(ni->ni_macaddr),
1593		nt != NULL ? nt->nt_name : "<gone>");
1594
1595	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1596	if (nt != NULL) {
1597		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1598		LIST_REMOVE(ni, ni_hash);
1599	}
1600	ic->ic_node_free(ni);
1601}
1602
1603void
1604#ifdef IEEE80211_DEBUG_REFCNT
1605ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1606#else
1607ieee80211_free_node(struct ieee80211_node *ni)
1608#endif
1609{
1610	struct ieee80211_node_table *nt = ni->ni_table;
1611
1612#ifdef IEEE80211_DEBUG_REFCNT
1613	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1614		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1615		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1616#endif
1617	if (nt != NULL) {
1618		IEEE80211_NODE_LOCK(nt);
1619		if (ieee80211_node_dectestref(ni)) {
1620			/*
1621			 * Last reference, reclaim state.
1622			 */
1623			_ieee80211_free_node(ni);
1624		} else if (ieee80211_node_refcnt(ni) == 1 &&
1625		    nt->nt_keyixmap != NULL) {
1626			ieee80211_keyix keyix;
1627			/*
1628			 * Check for a last reference in the key mapping table.
1629			 */
1630			keyix = ni->ni_ucastkey.wk_rxkeyix;
1631			if (keyix < nt->nt_keyixmax &&
1632			    nt->nt_keyixmap[keyix] == ni) {
1633				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1634				    "%s: %p<%s> clear key map entry", __func__,
1635				    ni, ether_sprintf(ni->ni_macaddr));
1636				nt->nt_keyixmap[keyix] = NULL;
1637				ieee80211_node_decref(ni); /* XXX needed? */
1638				_ieee80211_free_node(ni);
1639			}
1640		}
1641		IEEE80211_NODE_UNLOCK(nt);
1642	} else {
1643		if (ieee80211_node_dectestref(ni))
1644			_ieee80211_free_node(ni);
1645	}
1646}
1647
1648/*
1649 * Reclaim a unicast key and clear any key cache state.
1650 */
1651int
1652ieee80211_node_delucastkey(struct ieee80211_node *ni)
1653{
1654	struct ieee80211com *ic = ni->ni_ic;
1655	struct ieee80211_node_table *nt = &ic->ic_sta;
1656	struct ieee80211_node *nikey;
1657	ieee80211_keyix keyix;
1658	int isowned, status;
1659
1660	/*
1661	 * NB: We must beware of LOR here; deleting the key
1662	 * can cause the crypto layer to block traffic updates
1663	 * which can generate a LOR against the node table lock;
1664	 * grab it here and stash the key index for our use below.
1665	 *
1666	 * Must also beware of recursion on the node table lock.
1667	 * When called from node_cleanup we may already have
1668	 * the node table lock held.  Unfortunately there's no
1669	 * way to separate out this path so we must do this
1670	 * conditionally.
1671	 */
1672	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1673	if (!isowned)
1674		IEEE80211_NODE_LOCK(nt);
1675	keyix = ni->ni_ucastkey.wk_rxkeyix;
1676	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1677	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1678		nikey = nt->nt_keyixmap[keyix];
1679		nt->nt_keyixmap[keyix] = NULL;;
1680	} else
1681		nikey = NULL;
1682	if (!isowned)
1683		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1684
1685	if (nikey != NULL) {
1686		KASSERT(nikey == ni,
1687			("key map out of sync, ni %p nikey %p", ni, nikey));
1688		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1689			"%s: delete key map entry %p<%s> refcnt %d\n",
1690			__func__, ni, ether_sprintf(ni->ni_macaddr),
1691			ieee80211_node_refcnt(ni)-1);
1692		ieee80211_free_node(ni);
1693	}
1694	return status;
1695}
1696
1697/*
1698 * Reclaim a node.  If this is the last reference count then
1699 * do the normal free work.  Otherwise remove it from the node
1700 * table and mark it gone by clearing the back-reference.
1701 */
1702static void
1703node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1704{
1705	ieee80211_keyix keyix;
1706
1707	IEEE80211_NODE_LOCK_ASSERT(nt);
1708
1709	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1710		"%s: remove %p<%s> from %s table, refcnt %d\n",
1711		__func__, ni, ether_sprintf(ni->ni_macaddr),
1712		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1713	/*
1714	 * Clear any entry in the unicast key mapping table.
1715	 * We need to do it here so rx lookups don't find it
1716	 * in the mapping table even if it's not in the hash
1717	 * table.  We cannot depend on the mapping table entry
1718	 * being cleared because the node may not be free'd.
1719	 */
1720	keyix = ni->ni_ucastkey.wk_rxkeyix;
1721	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1722	    nt->nt_keyixmap[keyix] == ni) {
1723		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1724			"%s: %p<%s> clear key map entry\n",
1725			__func__, ni, ether_sprintf(ni->ni_macaddr));
1726		nt->nt_keyixmap[keyix] = NULL;
1727		ieee80211_node_decref(ni);	/* NB: don't need free */
1728	}
1729	if (!ieee80211_node_dectestref(ni)) {
1730		/*
1731		 * Other references are present, just remove the
1732		 * node from the table so it cannot be found.  When
1733		 * the references are dropped storage will be
1734		 * reclaimed.
1735		 */
1736		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1737		LIST_REMOVE(ni, ni_hash);
1738		ni->ni_table = NULL;		/* clear reference */
1739	} else
1740		_ieee80211_free_node(ni);
1741}
1742
1743static void
1744ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1745{
1746	struct ieee80211com *ic = nt->nt_ic;
1747	struct ieee80211_node *ni;
1748
1749	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1750		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1751
1752	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1753		if (ni->ni_associd != 0) {
1754			if (ic->ic_auth->ia_node_leave != NULL)
1755				ic->ic_auth->ia_node_leave(ic, ni);
1756			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1757		}
1758		node_reclaim(nt, ni);
1759	}
1760	ieee80211_reset_erp(ic);
1761}
1762
1763static void
1764ieee80211_free_allnodes(struct ieee80211_node_table *nt)
1765{
1766
1767	IEEE80211_NODE_LOCK(nt);
1768	ieee80211_free_allnodes_locked(nt);
1769	IEEE80211_NODE_UNLOCK(nt);
1770}
1771
1772/*
1773 * Timeout entries in the scan cache.
1774 */
1775static void
1776ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1777{
1778	struct ieee80211com *ic = nt->nt_ic;
1779	struct ieee80211_node *ni, *tni;
1780
1781	IEEE80211_NODE_LOCK(nt);
1782	ni = ic->ic_bss;
1783	/* XXX belongs elsewhere */
1784	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1785		m_freem(ni->ni_rxfrag[0]);
1786		ni->ni_rxfrag[0] = NULL;
1787	}
1788	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1789		if (ni->ni_inact && --ni->ni_inact == 0) {
1790			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1791			    "[%s] scan candidate purged from cache "
1792			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1793			    ieee80211_node_refcnt(ni));
1794			node_reclaim(nt, ni);
1795		}
1796	}
1797	IEEE80211_NODE_UNLOCK(nt);
1798
1799	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1800}
1801
1802/*
1803 * Timeout inactive stations and do related housekeeping.
1804 * Note that we cannot hold the node lock while sending a
1805 * frame as this would lead to a LOR.  Instead we use a
1806 * generation number to mark nodes that we've scanned and
1807 * drop the lock and restart a scan if we have to time out
1808 * a node.  Since we are single-threaded by virtue of
1809 * controlling the inactivity timer we can be sure this will
1810 * process each node only once.
1811 */
1812static void
1813ieee80211_timeout_stations(struct ieee80211_node_table *nt)
1814{
1815	struct ieee80211com *ic = nt->nt_ic;
1816	struct ieee80211_node *ni;
1817	u_int gen;
1818	int isadhoc;
1819
1820	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1821		   ic->ic_opmode == IEEE80211_M_AHDEMO);
1822	IEEE80211_SCAN_LOCK(nt);
1823	gen = ++nt->nt_scangen;
1824	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1825		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1826restart:
1827	IEEE80211_NODE_LOCK(nt);
1828	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1829		if (ni->ni_scangen == gen)	/* previously handled */
1830			continue;
1831		ni->ni_scangen = gen;
1832		/*
1833		 * Ignore entries for which have yet to receive an
1834		 * authentication frame.  These are transient and
1835		 * will be reclaimed when the last reference to them
1836		 * goes away (when frame xmits complete).
1837		 */
1838		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1839		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1840			continue;
1841		/*
1842		 * Free fragment if not needed anymore
1843		 * (last fragment older than 1s).
1844		 * XXX doesn't belong here
1845		 */
1846		if (ni->ni_rxfrag[0] != NULL &&
1847		    ticks > ni->ni_rxfragstamp + hz) {
1848			m_freem(ni->ni_rxfrag[0]);
1849			ni->ni_rxfrag[0] = NULL;
1850		}
1851		/*
1852		 * Special case ourself; we may be idle for extended periods
1853		 * of time and regardless reclaiming our state is wrong.
1854		 */
1855		if (ni == ic->ic_bss)
1856			continue;
1857		ni->ni_inact--;
1858		if (ni->ni_associd != 0 || isadhoc) {
1859			/*
1860			 * Age frames on the power save queue. The
1861			 * aging interval is 4 times the listen
1862			 * interval specified by the station.  This
1863			 * number is factored into the age calculations
1864			 * when the frame is placed on the queue.  We
1865			 * store ages as time differences we can check
1866			 * and/or adjust only the head of the list.
1867			 */
1868			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1869				struct mbuf *m;
1870				int discard = 0;
1871
1872				IEEE80211_NODE_SAVEQ_LOCK(ni);
1873				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1874				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1875IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1876					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1877					m_freem(m);
1878					discard++;
1879				}
1880				if (m != NULL)
1881					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1882				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1883
1884				if (discard != 0) {
1885					IEEE80211_DPRINTF(ic,
1886					    IEEE80211_MSG_POWER,
1887					    "[%s] discard %u frames for age\n",
1888					    ether_sprintf(ni->ni_macaddr),
1889					    discard);
1890					IEEE80211_NODE_STAT_ADD(ni,
1891						ps_discard, discard);
1892					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1893						ic->ic_set_tim(ni, 0);
1894				}
1895			}
1896			/*
1897			 * Probe the station before time it out.  We
1898			 * send a null data frame which may not be
1899			 * universally supported by drivers (need it
1900			 * for ps-poll support so it should be...).
1901			 */
1902			if (0 < ni->ni_inact &&
1903			    ni->ni_inact <= ic->ic_inact_probe) {
1904				IEEE80211_NOTE(ic,
1905				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1906				    ni, "%s",
1907				    "probe station due to inactivity");
1908				/*
1909				 * Grab a reference before unlocking the table
1910				 * so the node cannot be reclaimed before we
1911				 * send the frame. ieee80211_send_nulldata
1912				 * understands we've done this and reclaims the
1913				 * ref for us as needed.
1914				 */
1915				ieee80211_ref_node(ni);
1916				IEEE80211_NODE_UNLOCK(nt);
1917				ieee80211_send_nulldata(ni);
1918				/* XXX stat? */
1919				goto restart;
1920			}
1921		}
1922		if (ni->ni_inact <= 0) {
1923			IEEE80211_NOTE(ic,
1924			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1925			    "station timed out due to inactivity "
1926			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1927			/*
1928			 * Send a deauthenticate frame and drop the station.
1929			 * This is somewhat complicated due to reference counts
1930			 * and locking.  At this point a station will typically
1931			 * have a reference count of 1.  ieee80211_node_leave
1932			 * will do a "free" of the node which will drop the
1933			 * reference count.  But in the meantime a reference
1934			 * wil be held by the deauth frame.  The actual reclaim
1935			 * of the node will happen either after the tx is
1936			 * completed or by ieee80211_node_leave.
1937			 *
1938			 * Separately we must drop the node lock before sending
1939			 * in case the driver takes a lock, as this will result
1940			 * in  LOR between the node lock and the driver lock.
1941			 */
1942			IEEE80211_NODE_UNLOCK(nt);
1943			if (ni->ni_associd != 0) {
1944				IEEE80211_SEND_MGMT(ic, ni,
1945				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1946				    IEEE80211_REASON_AUTH_EXPIRE);
1947			}
1948			ieee80211_node_leave(ic, ni);
1949			ic->ic_stats.is_node_timeout++;
1950			goto restart;
1951		}
1952	}
1953	IEEE80211_NODE_UNLOCK(nt);
1954
1955	IEEE80211_SCAN_UNLOCK(nt);
1956
1957	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1958}
1959
1960void
1961ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1962{
1963	struct ieee80211_node *ni;
1964	u_int gen;
1965
1966	IEEE80211_SCAN_LOCK(nt);
1967	gen = ++nt->nt_scangen;
1968restart:
1969	IEEE80211_NODE_LOCK(nt);
1970	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1971		if (ni->ni_scangen != gen) {
1972			ni->ni_scangen = gen;
1973			(void) ieee80211_ref_node(ni);
1974			IEEE80211_NODE_UNLOCK(nt);
1975			(*f)(arg, ni);
1976			ieee80211_free_node(ni);
1977			goto restart;
1978		}
1979	}
1980	IEEE80211_NODE_UNLOCK(nt);
1981
1982	IEEE80211_SCAN_UNLOCK(nt);
1983}
1984
1985void
1986ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1987{
1988	printf("0x%p: mac %s refcnt %d\n", ni,
1989		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1990	printf("\tscangen %u authmode %u flags 0x%x\n",
1991		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1992	printf("\tassocid 0x%x txpower %u vlan %u\n",
1993		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1994	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1995		ni->ni_txseqs[0],
1996		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1997		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1998		ni->ni_rxfragstamp);
1999	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
2000		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
2001	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2002		ether_sprintf(ni->ni_bssid),
2003		ni->ni_esslen, ni->ni_essid,
2004		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2005	printf("\tfails %u inact %u txrate %u\n",
2006		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
2007}
2008
2009void
2010ieee80211_dump_nodes(struct ieee80211_node_table *nt)
2011{
2012	ieee80211_iterate_nodes(nt,
2013		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2014}
2015
2016/*
2017 * Handle a station joining an 11g network.
2018 */
2019static void
2020ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2021{
2022
2023	/*
2024	 * Station isn't capable of short slot time.  Bump
2025	 * the count of long slot time stations and disable
2026	 * use of short slot time.  Note that the actual switch
2027	 * over to long slot time use may not occur until the
2028	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2029	 */
2030	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2031		ic->ic_longslotsta++;
2032		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2033		    "[%s] station needs long slot time, count %d\n",
2034		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
2035		/* XXX vap's w/ conflicting needs won't work */
2036		ieee80211_set_shortslottime(ic, 0);
2037	}
2038	/*
2039	 * If the new station is not an ERP station
2040	 * then bump the counter and enable protection
2041	 * if configured.
2042	 */
2043	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
2044		ic->ic_nonerpsta++;
2045		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2046		    "[%s] station is !ERP, %d non-ERP stations associated\n",
2047		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
2048		/*
2049		 * If protection is configured, enable it.
2050		 */
2051		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
2052			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2053			    "%s: enable use of protection\n", __func__);
2054			ic->ic_flags |= IEEE80211_F_USEPROT;
2055		}
2056		/*
2057		 * If station does not support short preamble
2058		 * then we must enable use of Barker preamble.
2059		 */
2060		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2061			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2062			    "[%s] station needs long preamble\n",
2063			    ether_sprintf(ni->ni_macaddr));
2064			ic->ic_flags |= IEEE80211_F_USEBARKER;
2065			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2066		}
2067		if (ic->ic_nonerpsta == 1)
2068			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
2069	} else
2070		ni->ni_flags |= IEEE80211_NODE_ERP;
2071}
2072
2073void
2074ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
2075{
2076	int newassoc;
2077
2078	if (ni->ni_associd == 0) {
2079		u_int16_t aid;
2080
2081		/*
2082		 * It would be good to search the bitmap
2083		 * more efficiently, but this will do for now.
2084		 */
2085		for (aid = 1; aid < ic->ic_max_aid; aid++) {
2086			if (!IEEE80211_AID_ISSET(aid,
2087			    ic->ic_aid_bitmap))
2088				break;
2089		}
2090		if (aid >= ic->ic_max_aid) {
2091			IEEE80211_SEND_MGMT(ic, ni, resp,
2092			    IEEE80211_REASON_ASSOC_TOOMANY);
2093			ieee80211_node_leave(ic, ni);
2094			return;
2095		}
2096		ni->ni_associd = aid | 0xc000;
2097		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
2098		ic->ic_sta_assoc++;
2099		newassoc = 1;
2100		if (ic->ic_curmode == IEEE80211_MODE_11G)
2101			ieee80211_node_join_11g(ic, ni);
2102	} else
2103		newassoc = 0;
2104
2105	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2106	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
2107	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
2108	    IEEE80211_NODE_AID(ni),
2109	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2110	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2111	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2112	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
2113	);
2114
2115	/* give driver a chance to setup state like ni_txrate */
2116	if (ic->ic_newassoc != NULL)
2117		ic->ic_newassoc(ni, newassoc);
2118	ni->ni_inact_reload = ic->ic_inact_auth;
2119	ni->ni_inact = ni->ni_inact_reload;
2120	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
2121	/* tell the authenticator about new station */
2122	if (ic->ic_auth->ia_node_join != NULL)
2123		ic->ic_auth->ia_node_join(ic, ni);
2124	ieee80211_notify_node_join(ic, ni, newassoc);
2125}
2126
2127/*
2128 * Handle a station leaving an 11g network.
2129 */
2130static void
2131ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2132{
2133
2134	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
2135	     ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
2136	      ni->ni_chan->ic_flags, ic->ic_curmode));
2137
2138	/*
2139	 * If a long slot station do the slot time bookkeeping.
2140	 */
2141	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2142		KASSERT(ic->ic_longslotsta > 0,
2143		    ("bogus long slot station count %d", ic->ic_longslotsta));
2144		ic->ic_longslotsta--;
2145		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2146		    "[%s] long slot time station leaves, count now %d\n",
2147		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
2148		if (ic->ic_longslotsta == 0) {
2149			/*
2150			 * Re-enable use of short slot time if supported
2151			 * and not operating in IBSS mode (per spec).
2152			 */
2153			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2154			    ic->ic_opmode != IEEE80211_M_IBSS) {
2155				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2156				    "%s: re-enable use of short slot time\n",
2157				    __func__);
2158				ieee80211_set_shortslottime(ic, 1);
2159			}
2160		}
2161	}
2162	/*
2163	 * If a non-ERP station do the protection-related bookkeeping.
2164	 */
2165	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2166		KASSERT(ic->ic_nonerpsta > 0,
2167		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2168		ic->ic_nonerpsta--;
2169		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2170		    "[%s] non-ERP station leaves, count now %d\n",
2171		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
2172		if (ic->ic_nonerpsta == 0) {
2173			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2174				"%s: disable use of protection\n", __func__);
2175			ic->ic_flags &= ~IEEE80211_F_USEPROT;
2176			/* XXX verify mode? */
2177			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2178				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2179				    "%s: re-enable use of short preamble\n",
2180				    __func__);
2181				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2182				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2183			}
2184			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
2185		}
2186	}
2187}
2188
2189/*
2190 * Handle bookkeeping for station deauthentication/disassociation
2191 * when operating as an ap.
2192 */
2193void
2194ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
2195{
2196	struct ieee80211_node_table *nt = ni->ni_table;
2197
2198	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2199	    "[%s] station with aid %d leaves\n",
2200	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
2201
2202	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2203		ic->ic_opmode == IEEE80211_M_IBSS ||
2204		ic->ic_opmode == IEEE80211_M_AHDEMO,
2205		("unexpected operating mode %u", ic->ic_opmode));
2206	/*
2207	 * If node wasn't previously associated all
2208	 * we need to do is reclaim the reference.
2209	 */
2210	/* XXX ibss mode bypasses 11g and notification */
2211	if (ni->ni_associd == 0)
2212		goto done;
2213	/*
2214	 * Tell the authenticator the station is leaving.
2215	 * Note that we must do this before yanking the
2216	 * association id as the authenticator uses the
2217	 * associd to locate it's state block.
2218	 */
2219	if (ic->ic_auth->ia_node_leave != NULL)
2220		ic->ic_auth->ia_node_leave(ic, ni);
2221	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
2222	ni->ni_associd = 0;
2223	ic->ic_sta_assoc--;
2224
2225	if (ic->ic_curmode == IEEE80211_MODE_11G)
2226		ieee80211_node_leave_11g(ic, ni);
2227	/*
2228	 * Cleanup station state.  In particular clear various
2229	 * state that might otherwise be reused if the node
2230	 * is reused before the reference count goes to zero
2231	 * (and memory is reclaimed).
2232	 */
2233	ieee80211_sta_leave(ic, ni);
2234done:
2235	/*
2236	 * Remove the node from any table it's recorded in and
2237	 * drop the caller's reference.  Removal from the table
2238	 * is important to insure the node is not reprocessed
2239	 * for inactivity.
2240	 */
2241	if (nt != NULL) {
2242		IEEE80211_NODE_LOCK(nt);
2243		node_reclaim(nt, ni);
2244		IEEE80211_NODE_UNLOCK(nt);
2245	} else
2246		ieee80211_free_node(ni);
2247}
2248
2249u_int8_t
2250ieee80211_getrssi(struct ieee80211com *ic)
2251{
2252#define	NZ(x)	((x) == 0 ? 1 : (x))
2253	struct ieee80211_node_table *nt = &ic->ic_sta;
2254	u_int32_t rssi_samples, rssi_total;
2255	struct ieee80211_node *ni;
2256
2257	rssi_total = 0;
2258	rssi_samples = 0;
2259	switch (ic->ic_opmode) {
2260	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2261		/* XXX locking */
2262		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2263			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
2264				rssi_samples++;
2265				rssi_total += ic->ic_node_getrssi(ni);
2266			}
2267		break;
2268	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2269		/* XXX locking */
2270		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2271			rssi_samples++;
2272			rssi_total += ic->ic_node_getrssi(ni);
2273		}
2274		break;
2275	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2276		/* XXX locking */
2277		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2278			if (IEEE80211_AID(ni->ni_associd) != 0) {
2279				rssi_samples++;
2280				rssi_total += ic->ic_node_getrssi(ni);
2281			}
2282		break;
2283	case IEEE80211_M_MONITOR:	/* XXX */
2284	case IEEE80211_M_STA:		/* use stats from associated ap */
2285	default:
2286		if (ic->ic_bss != NULL)
2287			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
2288		rssi_samples = 1;
2289		break;
2290	}
2291	return rssi_total / NZ(rssi_samples);
2292#undef NZ
2293}
2294
2295/*
2296 * Indicate whether there are frames queued for a station in power-save mode.
2297 */
2298static void
2299ieee80211_set_tim(struct ieee80211_node *ni, int set)
2300{
2301	struct ieee80211com *ic = ni->ni_ic;
2302	u_int16_t aid;
2303
2304	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2305		ic->ic_opmode == IEEE80211_M_IBSS,
2306		("operating mode %u", ic->ic_opmode));
2307
2308	aid = IEEE80211_AID(ni->ni_associd);
2309	KASSERT(aid < ic->ic_max_aid,
2310		("bogus aid %u, max %u", aid, ic->ic_max_aid));
2311
2312	IEEE80211_BEACON_LOCK(ic);
2313	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
2314		if (set) {
2315			setbit(ic->ic_tim_bitmap, aid);
2316			ic->ic_ps_pending++;
2317		} else {
2318			clrbit(ic->ic_tim_bitmap, aid);
2319			ic->ic_ps_pending--;
2320		}
2321		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
2322	}
2323	IEEE80211_BEACON_UNLOCK(ic);
2324}
2325
2326/*
2327 * Node table support.
2328 */
2329
2330static void
2331ieee80211_node_table_init(struct ieee80211com *ic,
2332	struct ieee80211_node_table *nt,
2333	const char *name, int inact, int keyixmax,
2334	void (*timeout)(struct ieee80211_node_table *))
2335{
2336
2337	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
2338		"%s %s table, inact %u\n", __func__, name, inact);
2339
2340	nt->nt_ic = ic;
2341	/* XXX need unit */
2342	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2343	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2344	TAILQ_INIT(&nt->nt_node);
2345	nt->nt_name = name;
2346	nt->nt_scangen = 1;
2347	nt->nt_inact_init = inact;
2348	nt->nt_timeout = timeout;
2349	nt->nt_keyixmax = keyixmax;
2350	if (nt->nt_keyixmax > 0) {
2351		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2352			keyixmax * sizeof(struct ieee80211_node *),
2353			M_80211_NODE, M_NOWAIT | M_ZERO);
2354		if (nt->nt_keyixmap == NULL)
2355			if_printf(ic->ic_ifp,
2356			    "Cannot allocate key index map with %u entries\n",
2357			    keyixmax);
2358	} else
2359		nt->nt_keyixmap = NULL;
2360}
2361
2362void
2363ieee80211_node_table_reset(struct ieee80211_node_table *nt)
2364{
2365
2366	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2367		"%s %s table\n", __func__, nt->nt_name);
2368
2369	IEEE80211_NODE_LOCK(nt);
2370	nt->nt_inact_timer = 0;
2371	ieee80211_free_allnodes_locked(nt);
2372	IEEE80211_NODE_UNLOCK(nt);
2373}
2374
2375static void
2376ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2377{
2378
2379	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2380		"%s %s table\n", __func__, nt->nt_name);
2381
2382	IEEE80211_NODE_LOCK(nt);
2383	ieee80211_free_allnodes_locked(nt);
2384	if (nt->nt_keyixmap != NULL) {
2385		/* XXX verify all entries are NULL */
2386		int i;
2387		for (i = 0; i < nt->nt_keyixmax; i++)
2388			if (nt->nt_keyixmap[i] != NULL)
2389				printf("%s: %s[%u] still active\n", __func__,
2390					nt->nt_name, i);
2391		FREE(nt->nt_keyixmap, M_80211_NODE);
2392		nt->nt_keyixmap = NULL;
2393	}
2394	IEEE80211_SCAN_LOCK_DESTROY(nt);
2395	IEEE80211_NODE_LOCK_DESTROY(nt);
2396}
2397