ieee80211_node.c revision 158121
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3139530Ssam * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116904Ssam * 3. The name of the author may not be used to endorse or promote products
15116904Ssam *    derived from this software without specific prior written permission.
16116742Ssam *
17116742Ssam * Alternatively, this software may be distributed under the terms of the
18116742Ssam * GNU General Public License ("GPL") version 2 as published by the Free
19116742Ssam * Software Foundation.
20116742Ssam *
21116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31116742Ssam */
32116742Ssam
33116742Ssam#include <sys/cdefs.h>
34116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 158121 2006-04-28 19:06:15Z sam $");
35116742Ssam
36116742Ssam#include <sys/param.h>
37116742Ssam#include <sys/systm.h>
38116742Ssam#include <sys/mbuf.h>
39116742Ssam#include <sys/malloc.h>
40116742Ssam#include <sys/kernel.h>
41138568Ssam
42116742Ssam#include <sys/socket.h>
43116742Ssam
44116742Ssam#include <net/if.h>
45116742Ssam#include <net/if_media.h>
46116742Ssam#include <net/ethernet.h>
47116742Ssam
48116742Ssam#include <net80211/ieee80211_var.h>
49116742Ssam
50116742Ssam#include <net/bpf.h>
51116742Ssam
52147221Ssam/*
53147221Ssam * Association id's are managed with a bit vector.
54147221Ssam */
55147221Ssam#define	IEEE80211_AID_SET(b, w) \
56147221Ssam	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
57147221Ssam#define	IEEE80211_AID_CLR(b, w) \
58147221Ssam	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
59147221Ssam#define	IEEE80211_AID_ISSET(b, w) \
60147221Ssam	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
61147221Ssam
62138568Ssamstatic struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
63138568Ssamstatic void node_cleanup(struct ieee80211_node *);
64138568Ssamstatic void node_free(struct ieee80211_node *);
65138568Ssamstatic u_int8_t node_getrssi(const struct ieee80211_node *);
66116742Ssam
67138568Ssamstatic void ieee80211_setup_node(struct ieee80211_node_table *,
68138568Ssam		struct ieee80211_node *, const u_int8_t *);
69138568Ssamstatic void _ieee80211_free_node(struct ieee80211_node *);
70138568Ssamstatic void ieee80211_free_allnodes(struct ieee80211_node_table *);
71120104Ssam
72138568Ssamstatic void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
73138568Ssamstatic void ieee80211_timeout_stations(struct ieee80211_node_table *);
74116742Ssam
75148304Ssamstatic void ieee80211_set_tim(struct ieee80211_node *, int set);
76138568Ssam
77138568Ssamstatic void ieee80211_node_table_init(struct ieee80211com *ic,
78148863Ssam	struct ieee80211_node_table *nt, const char *name,
79148863Ssam	int inact, int keyixmax,
80138568Ssam	void (*timeout)(struct ieee80211_node_table *));
81138568Ssamstatic void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
82138568Ssam
83127876SsamMALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
84120481Ssam
85116742Ssamvoid
86138568Ssamieee80211_node_attach(struct ieee80211com *ic)
87116742Ssam{
88116742Ssam
89138568Ssam	ic->ic_node_alloc = node_alloc;
90138568Ssam	ic->ic_node_free = node_free;
91138568Ssam	ic->ic_node_cleanup = node_cleanup;
92138568Ssam	ic->ic_node_getrssi = node_getrssi;
93138568Ssam
94138568Ssam	/* default station inactivity timer setings */
95138568Ssam	ic->ic_inact_init = IEEE80211_INACT_INIT;
96138568Ssam	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
97138568Ssam	ic->ic_inact_run = IEEE80211_INACT_RUN;
98138568Ssam	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
99138568Ssam
100148863Ssam	/* NB: driver should override */
101148863Ssam	ic->ic_max_aid = IEEE80211_AID_DEF;
102148863Ssam	ic->ic_set_tim = ieee80211_set_tim;
103148863Ssam}
104148863Ssam
105148863Ssamvoid
106148863Ssamieee80211_node_lateattach(struct ieee80211com *ic)
107148863Ssam{
108148863Ssam	struct ieee80211_rsnparms *rsn;
109148863Ssam
110148863Ssam	if (ic->ic_max_aid > IEEE80211_AID_MAX)
111138568Ssam		ic->ic_max_aid = IEEE80211_AID_MAX;
112138568Ssam	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
113138568Ssam		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
114138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
115138568Ssam	if (ic->ic_aid_bitmap == NULL) {
116138568Ssam		/* XXX no way to recover */
117138568Ssam		printf("%s: no memory for AID bitmap!\n", __func__);
118138568Ssam		ic->ic_max_aid = 0;
119138568Ssam	}
120138568Ssam
121138568Ssam	/* XXX defer until using hostap/ibss mode */
122138568Ssam	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
123138568Ssam	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
124138568Ssam		M_DEVBUF, M_NOWAIT | M_ZERO);
125138568Ssam	if (ic->ic_tim_bitmap == NULL) {
126138568Ssam		/* XXX no way to recover */
127138568Ssam		printf("%s: no memory for TIM bitmap!\n", __func__);
128138568Ssam	}
129118887Ssam
130148863Ssam	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
131148863Ssam		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix,
132148863Ssam		ieee80211_timeout_stations);
133148863Ssam	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
134148863Ssam		IEEE80211_INACT_SCAN, 0,
135148863Ssam		ieee80211_timeout_scan_candidates);
136118887Ssam
137148863Ssam	ieee80211_reset_bss(ic);
138138568Ssam	/*
139138568Ssam	 * Setup "global settings" in the bss node so that
140138568Ssam	 * each new station automatically inherits them.
141138568Ssam	 */
142148863Ssam	rsn = &ic->ic_bss->ni_rsn;
143138568Ssam	/* WEP, TKIP, and AES-CCM are always supported */
144138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
145138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
146138568Ssam	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
147138568Ssam	if (ic->ic_caps & IEEE80211_C_AES)
148138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
149138568Ssam	if (ic->ic_caps & IEEE80211_C_CKIP)
150138568Ssam		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
151138568Ssam	/*
152138568Ssam	 * Default unicast cipher to WEP for 802.1x use.  If
153138568Ssam	 * WPA is enabled the management code will set these
154138568Ssam	 * values to reflect.
155138568Ssam	 */
156138568Ssam	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
157138568Ssam	rsn->rsn_ucastkeylen = 104 / NBBY;
158138568Ssam	/*
159138568Ssam	 * WPA says the multicast cipher is the lowest unicast
160138568Ssam	 * cipher supported.  But we skip WEP which would
161138568Ssam	 * otherwise be used based on this criteria.
162138568Ssam	 */
163138568Ssam	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
164138568Ssam	rsn->rsn_mcastkeylen = 128 / NBBY;
165138568Ssam
166138568Ssam	/*
167138568Ssam	 * We support both WPA-PSK and 802.1x; the one used
168138568Ssam	 * is determined by the authentication mode and the
169138568Ssam	 * setting of the PSK state.
170138568Ssam	 */
171138568Ssam	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
172138568Ssam	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
173138568Ssam
174148863Ssam	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
175116742Ssam}
176116742Ssam
177116742Ssamvoid
178138568Ssamieee80211_node_detach(struct ieee80211com *ic)
179116742Ssam{
180116742Ssam
181138568Ssam	if (ic->ic_bss != NULL) {
182138568Ssam		ieee80211_free_node(ic->ic_bss);
183138568Ssam		ic->ic_bss = NULL;
184138568Ssam	}
185138568Ssam	ieee80211_node_table_cleanup(&ic->ic_scan);
186140753Ssam	ieee80211_node_table_cleanup(&ic->ic_sta);
187138568Ssam	if (ic->ic_aid_bitmap != NULL) {
188138568Ssam		FREE(ic->ic_aid_bitmap, M_DEVBUF);
189138568Ssam		ic->ic_aid_bitmap = NULL;
190138568Ssam	}
191138568Ssam	if (ic->ic_tim_bitmap != NULL) {
192138568Ssam		FREE(ic->ic_tim_bitmap, M_DEVBUF);
193138568Ssam		ic->ic_tim_bitmap = NULL;
194138568Ssam	}
195116742Ssam}
196116742Ssam
197138568Ssam/*
198138568Ssam * Port authorize/unauthorize interfaces for use by an authenticator.
199138568Ssam */
200138568Ssam
201138568Ssamvoid
202148302Ssamieee80211_node_authorize(struct ieee80211_node *ni)
203138568Ssam{
204148302Ssam	struct ieee80211com *ic = ni->ni_ic;
205148302Ssam
206138568Ssam	ni->ni_flags |= IEEE80211_NODE_AUTH;
207139528Ssam	ni->ni_inact_reload = ic->ic_inact_run;
208138568Ssam}
209138568Ssam
210138568Ssamvoid
211148302Ssamieee80211_node_unauthorize(struct ieee80211_node *ni)
212138568Ssam{
213138568Ssam	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
214138568Ssam}
215138568Ssam
216116742Ssam/*
217138568Ssam * Set/change the channel.  The rate set is also updated as
218138568Ssam * to insure a consistent view by drivers.
219138568Ssam */
220153351Ssamstatic void
221138568Ssamieee80211_set_chan(struct ieee80211com *ic,
222138568Ssam	struct ieee80211_node *ni, struct ieee80211_channel *chan)
223138568Ssam{
224153351Ssam	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
225153351Ssam		chan = ic->ic_curchan;
226138568Ssam	ni->ni_chan = chan;
227138568Ssam	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
228138568Ssam}
229138568Ssam
230138568Ssam/*
231116742Ssam * AP scanning support.
232116742Ssam */
233116742Ssam
234138568Ssam#ifdef IEEE80211_DEBUG
235138568Ssamstatic void
236138568Ssamdump_chanlist(const u_char chans[])
237138568Ssam{
238138568Ssam	const char *sep;
239138568Ssam	int i;
240138568Ssam
241138568Ssam	sep = " ";
242138568Ssam	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
243138568Ssam		if (isset(chans, i)) {
244138568Ssam			printf("%s%u", sep, i);
245138568Ssam			sep = ", ";
246138568Ssam		}
247138568Ssam}
248138568Ssam#endif /* IEEE80211_DEBUG */
249138568Ssam
250116742Ssam/*
251138568Ssam * Initialize the channel set to scan based on the
252116742Ssam * of available channels and the current PHY mode.
253116742Ssam */
254117811Ssamstatic void
255138568Ssamieee80211_reset_scan(struct ieee80211com *ic)
256116742Ssam{
257116742Ssam
258138568Ssam	/* XXX ic_des_chan should be handled with ic_chan_active */
259138568Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
260138568Ssam		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
261138568Ssam		setbit(ic->ic_chan_scan,
262138568Ssam			ieee80211_chan2ieee(ic, ic->ic_des_chan));
263138568Ssam	} else
264138568Ssam		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
265138568Ssam			sizeof(ic->ic_chan_active));
266138568Ssam#ifdef IEEE80211_DEBUG
267138568Ssam	if (ieee80211_msg_scan(ic)) {
268138568Ssam		printf("%s: scan set:", __func__);
269138568Ssam		dump_chanlist(ic->ic_chan_scan);
270138568Ssam		printf(" start chan %u\n",
271148936Ssam			ieee80211_chan2ieee(ic, ic->ic_curchan));
272138568Ssam	}
273138568Ssam#endif /* IEEE80211_DEBUG */
274116742Ssam}
275116742Ssam
276116742Ssam/*
277116742Ssam * Begin an active scan.
278116742Ssam */
279116742Ssamvoid
280138568Ssamieee80211_begin_scan(struct ieee80211com *ic, int reset)
281116742Ssam{
282116742Ssam
283138568Ssam	ic->ic_scan.nt_scangen++;
284117811Ssam	/*
285117811Ssam	 * In all but hostap mode scanning starts off in
286117811Ssam	 * an active mode before switching to passive.
287117811Ssam	 */
288121180Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
289117811Ssam		ic->ic_flags |= IEEE80211_F_ASCAN;
290121180Ssam		ic->ic_stats.is_scan_active++;
291121180Ssam	} else
292121180Ssam		ic->ic_stats.is_scan_passive++;
293139520Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
294139520Ssam		"begin %s scan in %s mode, scangen %u\n",
295138568Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
296139520Ssam		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
297116742Ssam	/*
298138568Ssam	 * Clear scan state and flush any previously seen AP's.
299116742Ssam	 */
300138568Ssam	ieee80211_reset_scan(ic);
301138568Ssam	if (reset)
302138568Ssam		ieee80211_free_allnodes(&ic->ic_scan);
303116742Ssam
304138568Ssam	ic->ic_flags |= IEEE80211_F_SCAN;
305138568Ssam
306117811Ssam	/* Scan the next channel. */
307138568Ssam	ieee80211_next_scan(ic);
308116742Ssam}
309116742Ssam
310116742Ssam/*
311116742Ssam * Switch to the next channel marked for scanning.
312116742Ssam */
313138568Ssamint
314138568Ssamieee80211_next_scan(struct ieee80211com *ic)
315116742Ssam{
316116742Ssam	struct ieee80211_channel *chan;
317116742Ssam
318138568Ssam	/*
319138568Ssam	 * Insure any previous mgt frame timeouts don't fire.
320138568Ssam	 * This assumes the driver does the right thing in
321138568Ssam	 * flushing anything queued in the driver and below.
322138568Ssam	 */
323138568Ssam	ic->ic_mgt_timer = 0;
324156358Ssam	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
325138568Ssam
326148936Ssam	chan = ic->ic_curchan;
327138568Ssam	do {
328116742Ssam		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
329116742Ssam			chan = &ic->ic_channels[0];
330116742Ssam		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
331138568Ssam			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
332138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
333138568Ssam			    "%s: chan %d->%d\n", __func__,
334148936Ssam			    ieee80211_chan2ieee(ic, ic->ic_curchan),
335138568Ssam			    ieee80211_chan2ieee(ic, chan));
336148936Ssam			ic->ic_curchan = chan;
337148936Ssam			/*
338148936Ssam			 * XXX drivers should do this as needed,
339148936Ssam			 * XXX for now maintain compatibility
340148936Ssam			 */
341148936Ssam			ic->ic_bss->ni_rates =
342148936Ssam				ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
343138568Ssam			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
344138568Ssam			return 1;
345116742Ssam		}
346148936Ssam	} while (chan != ic->ic_curchan);
347138568Ssam	ieee80211_end_scan(ic);
348138568Ssam	return 0;
349116742Ssam}
350116742Ssam
351156358Ssam/*
352156358Ssam * Probe the curent channel, if allowed, while scanning.
353156358Ssam * If the channel is not marked passive-only then send
354156358Ssam * a probe request immediately.  Otherwise mark state and
355156358Ssam * listen for beacons on the channel; if we receive something
356156358Ssam * then we'll transmit a probe request.
357156358Ssam */
358156358Ssamvoid
359156358Ssamieee80211_probe_curchan(struct ieee80211com *ic, int force)
360156358Ssam{
361156358Ssam	struct ifnet *ifp = ic->ic_ifp;
362156358Ssam
363156358Ssam	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
364156358Ssam		/*
365156358Ssam		 * XXX send both broadcast+directed probe request
366156358Ssam		 */
367156358Ssam		ieee80211_send_probereq(ic->ic_bss,
368156358Ssam			ic->ic_myaddr, ifp->if_broadcastaddr,
369156358Ssam			ifp->if_broadcastaddr,
370156358Ssam			ic->ic_des_essid, ic->ic_des_esslen,
371156358Ssam			ic->ic_opt_ie, ic->ic_opt_ie_len);
372156358Ssam	} else
373156358Ssam		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
374156358Ssam}
375156358Ssam
376141658Ssamstatic __inline void
377141658Ssamcopy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
378141658Ssam{
379141658Ssam	/* propagate useful state */
380141658Ssam	nbss->ni_authmode = obss->ni_authmode;
381141658Ssam	nbss->ni_txpower = obss->ni_txpower;
382141658Ssam	nbss->ni_vlan = obss->ni_vlan;
383141658Ssam	nbss->ni_rsn = obss->ni_rsn;
384141658Ssam	/* XXX statistics? */
385141658Ssam}
386141658Ssam
387116742Ssamvoid
388116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
389116742Ssam{
390140753Ssam	struct ieee80211_node_table *nt;
391116742Ssam	struct ieee80211_node *ni;
392116742Ssam
393138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
394138568Ssam		"%s: creating ibss\n", __func__);
395138568Ssam
396138568Ssam	/*
397138568Ssam	 * Create the station/neighbor table.  Note that for adhoc
398138568Ssam	 * mode we make the initial inactivity timer longer since
399138568Ssam	 * we create nodes only through discovery and they typically
400138568Ssam	 * are long-lived associations.
401138568Ssam	 */
402140753Ssam	nt = &ic->ic_sta;
403140753Ssam	IEEE80211_NODE_LOCK(nt);
404140753Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
405140753Ssam		nt->nt_name = "station";
406140753Ssam		nt->nt_inact_init = ic->ic_inact_init;
407140753Ssam	} else {
408140753Ssam		nt->nt_name = "neighbor";
409140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
410140753Ssam	}
411140753Ssam	IEEE80211_NODE_UNLOCK(nt);
412140753Ssam
413148863Ssam	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
414140753Ssam	if (ni == NULL) {
415140753Ssam		/* XXX recovery? */
416138568Ssam		return;
417138568Ssam	}
418116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
419116742Ssam	ni->ni_esslen = ic->ic_des_esslen;
420116742Ssam	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
421141658Ssam	copy_bss(ni, ic->ic_bss);
422148843Ssam	ni->ni_intval = ic->ic_bintval;
423138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
424116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
425116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
426116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
427116742Ssam		ni->ni_fhindex = 1;
428116742Ssam	}
429138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
430138568Ssam		ic->ic_flags |= IEEE80211_F_SIBSS;
431138568Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
432143300Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
433143300Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
434143300Ssam		else
435143300Ssam			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
436153403Ssam	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
437153403Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
438153403Ssam			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
439153403Ssam		else
440153403Ssam			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
441138568Ssam	}
442138568Ssam	/*
443138568Ssam	 * Fix the channel and related attributes.
444138568Ssam	 */
445138568Ssam	ieee80211_set_chan(ic, ni, chan);
446148936Ssam	ic->ic_curchan = chan;
447138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
448138568Ssam	/*
449138568Ssam	 * Do mode-specific rate setup.
450138568Ssam	 */
451138568Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G) {
452138568Ssam		/*
453138568Ssam		 * Use a mixed 11b/11g rate set.
454138568Ssam		 */
455138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
456138568Ssam	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
457138568Ssam		/*
458138568Ssam		 * Force pure 11b rate set.
459138568Ssam		 */
460138568Ssam		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
461138568Ssam	}
462138568Ssam
463140753Ssam	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
464116742Ssam}
465116742Ssam
466138568Ssamvoid
467138568Ssamieee80211_reset_bss(struct ieee80211com *ic)
468138568Ssam{
469138568Ssam	struct ieee80211_node *ni, *obss;
470138568Ssam
471138568Ssam	ieee80211_node_table_reset(&ic->ic_scan);
472140753Ssam	ieee80211_node_table_reset(&ic->ic_sta);
473140753Ssam
474138568Ssam	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
475138568Ssam	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
476138568Ssam	obss = ic->ic_bss;
477138568Ssam	ic->ic_bss = ieee80211_ref_node(ni);
478141658Ssam	if (obss != NULL) {
479141658Ssam		copy_bss(ni, obss);
480148843Ssam		ni->ni_intval = ic->ic_bintval;
481138568Ssam		ieee80211_free_node(obss);
482141658Ssam	}
483138568Ssam}
484138568Ssam
485148432Ssam/* XXX tunable */
486148432Ssam#define	STA_FAILS_MAX	2		/* assoc failures before ignored */
487148432Ssam
488127767Ssamstatic int
489138568Ssamieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
490127767Ssam{
491127767Ssam        u_int8_t rate;
492127767Ssam        int fail;
493127767Ssam
494127767Ssam	fail = 0;
495127767Ssam	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
496127767Ssam		fail |= 0x01;
497127767Ssam	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
498127767Ssam	    ni->ni_chan != ic->ic_des_chan)
499127767Ssam		fail |= 0x01;
500127767Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
501127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
502127767Ssam			fail |= 0x02;
503127767Ssam	} else {
504127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
505127767Ssam			fail |= 0x02;
506127767Ssam	}
507138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
508127767Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
509127767Ssam			fail |= 0x04;
510127767Ssam	} else {
511127767Ssam		/* XXX does this mean privacy is supported or required? */
512127767Ssam		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
513127767Ssam			fail |= 0x04;
514127767Ssam	}
515148299Ssam	rate = ieee80211_fix_rate(ni, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
516127767Ssam	if (rate & IEEE80211_RATE_BASIC)
517127767Ssam		fail |= 0x08;
518127767Ssam	if (ic->ic_des_esslen != 0 &&
519127767Ssam	    (ni->ni_esslen != ic->ic_des_esslen ||
520127767Ssam	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
521127767Ssam		fail |= 0x10;
522127767Ssam	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
523127767Ssam	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
524127767Ssam		fail |= 0x20;
525148432Ssam	if (ni->ni_fails >= STA_FAILS_MAX)
526148432Ssam		fail |= 0x40;
527127767Ssam#ifdef IEEE80211_DEBUG
528138568Ssam	if (ieee80211_msg_scan(ic)) {
529148432Ssam		printf(" %c %s",
530148432Ssam		    fail & 0x40 ? '=' : fail & 0x80 ? '^' : fail ? '-' : '+',
531127767Ssam		    ether_sprintf(ni->ni_macaddr));
532127767Ssam		printf(" %s%c", ether_sprintf(ni->ni_bssid),
533127767Ssam		    fail & 0x20 ? '!' : ' ');
534127767Ssam		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
535127767Ssam			fail & 0x01 ? '!' : ' ');
536127767Ssam		printf(" %+4d", ni->ni_rssi);
537127767Ssam		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
538127767Ssam		    fail & 0x08 ? '!' : ' ');
539127767Ssam		printf(" %4s%c",
540127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
541127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
542127767Ssam		    "????",
543127767Ssam		    fail & 0x02 ? '!' : ' ');
544127767Ssam		printf(" %3s%c ",
545127767Ssam		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
546127767Ssam		    "wep" : "no",
547127767Ssam		    fail & 0x04 ? '!' : ' ');
548127767Ssam		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
549127767Ssam		printf("%s\n", fail & 0x10 ? "!" : "");
550127767Ssam	}
551127767Ssam#endif
552127767Ssam	return fail;
553127767Ssam}
554127767Ssam
555138568Ssamstatic __inline u_int8_t
556138568Ssammaxrate(const struct ieee80211_node *ni)
557138568Ssam{
558138568Ssam	const struct ieee80211_rateset *rs = &ni->ni_rates;
559138568Ssam	/* NB: assumes rate set is sorted (happens on frame receive) */
560138568Ssam	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
561138568Ssam}
562138568Ssam
563116742Ssam/*
564138568Ssam * Compare the capabilities of two nodes and decide which is
565138568Ssam * more desirable (return >0 if a is considered better).  Note
566138568Ssam * that we assume compatibility/usability has already been checked
567138568Ssam * so we don't need to (e.g. validate whether privacy is supported).
568138568Ssam * Used to select the best scan candidate for association in a BSS.
569138568Ssam */
570138568Ssamstatic int
571138568Ssamieee80211_node_compare(struct ieee80211com *ic,
572138568Ssam		       const struct ieee80211_node *a,
573138568Ssam		       const struct ieee80211_node *b)
574138568Ssam{
575138568Ssam	u_int8_t maxa, maxb;
576138568Ssam	u_int8_t rssia, rssib;
577148432Ssam	int weight;
578138568Ssam
579138568Ssam	/* privacy support preferred */
580138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
581138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
582138568Ssam		return 1;
583138568Ssam	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
584138568Ssam	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
585138568Ssam		return -1;
586138568Ssam
587148432Ssam	/* compare count of previous failures */
588148432Ssam	weight = b->ni_fails - a->ni_fails;
589148432Ssam	if (abs(weight) > 1)
590148432Ssam		return weight;
591148432Ssam
592138568Ssam	rssia = ic->ic_node_getrssi(a);
593138568Ssam	rssib = ic->ic_node_getrssi(b);
594139543Ssam	if (abs(rssib - rssia) < 5) {
595139543Ssam		/* best/max rate preferred if signal level close enough XXX */
596139543Ssam		maxa = maxrate(a);
597139543Ssam		maxb = maxrate(b);
598139543Ssam		if (maxa != maxb)
599139543Ssam			return maxa - maxb;
600139543Ssam		/* XXX use freq for channel preference */
601139543Ssam		/* for now just prefer 5Ghz band to all other bands */
602139543Ssam		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
603139543Ssam		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
604139543Ssam			return 1;
605139543Ssam		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
606139543Ssam		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
607139543Ssam			return -1;
608139543Ssam	}
609138568Ssam	/* all things being equal, use signal level */
610138568Ssam	return rssia - rssib;
611138568Ssam}
612138568Ssam
613138568Ssam/*
614140441Ssam * Mark an ongoing scan stopped.
615140441Ssam */
616140441Ssamvoid
617140441Ssamieee80211_cancel_scan(struct ieee80211com *ic)
618140441Ssam{
619140441Ssam
620140441Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
621140441Ssam		__func__,
622140441Ssam		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
623140441Ssam
624140441Ssam	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
625156358Ssam	ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
626140441Ssam}
627140441Ssam
628140441Ssam/*
629116742Ssam * Complete a scan of potential channels.
630116742Ssam */
631116742Ssamvoid
632138568Ssamieee80211_end_scan(struct ieee80211com *ic)
633116742Ssam{
634140448Ssam	struct ieee80211_node_table *nt = &ic->ic_scan;
635140448Ssam	struct ieee80211_node *ni, *selbs;
636116742Ssam
637140441Ssam	ieee80211_cancel_scan(ic);
638140441Ssam	ieee80211_notify_scan_done(ic);
639116742Ssam
640116742Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
641138568Ssam		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
642138568Ssam		int i, bestchan;
643138568Ssam		u_int8_t rssi;
644138568Ssam
645116742Ssam		/*
646116742Ssam		 * The passive scan to look for existing AP's completed,
647116742Ssam		 * select a channel to camp on.  Identify the channels
648116742Ssam		 * that already have one or more AP's and try to locate
649140753Ssam		 * an unoccupied one.  If that fails, pick a channel that
650138568Ssam		 * looks to be quietest.
651116742Ssam		 */
652138568Ssam		memset(maxrssi, 0, sizeof(maxrssi));
653140448Ssam		IEEE80211_NODE_LOCK(nt);
654140448Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
655138568Ssam			rssi = ic->ic_node_getrssi(ni);
656138568Ssam			i = ieee80211_chan2ieee(ic, ni->ni_chan);
657138568Ssam			if (rssi > maxrssi[i])
658138568Ssam				maxrssi[i] = rssi;
659116742Ssam		}
660140448Ssam		IEEE80211_NODE_UNLOCK(nt);
661138568Ssam		/* XXX select channel more intelligently */
662138568Ssam		bestchan = -1;
663116742Ssam		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
664138568Ssam			if (isset(ic->ic_chan_active, i)) {
665138568Ssam				/*
666138568Ssam				 * If the channel is unoccupied the max rssi
667138568Ssam				 * should be zero; just take it.  Otherwise
668138568Ssam				 * track the channel with the lowest rssi and
669138568Ssam				 * use that when all channels appear occupied.
670138568Ssam				 */
671138568Ssam				if (maxrssi[i] == 0) {
672138568Ssam					bestchan = i;
673116742Ssam					break;
674138568Ssam				}
675143715Ssam				if (bestchan == -1 ||
676143715Ssam				    maxrssi[i] < maxrssi[bestchan])
677138568Ssam					bestchan = i;
678138568Ssam			}
679138568Ssam		if (bestchan != -1) {
680138568Ssam			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
681138568Ssam			return;
682116742Ssam		}
683138568Ssam		/* no suitable channel, should not happen */
684138568Ssam	}
685138568Ssam
686138568Ssam	/*
687138568Ssam	 * When manually sequencing the state machine; scan just once
688138568Ssam	 * regardless of whether we have a candidate or not.  The
689138568Ssam	 * controlling application is expected to setup state and
690138568Ssam	 * initiate an association.
691138568Ssam	 */
692138568Ssam	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
693116742Ssam		return;
694138568Ssam	/*
695138568Ssam	 * Automatic sequencing; look for a candidate and
696138568Ssam	 * if found join the network.
697138568Ssam	 */
698140448Ssam	/* NB: unlocked read should be ok */
699140448Ssam	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
700138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
701138568Ssam			"%s: no scan candidate\n", __func__);
702116742Ssam  notfound:
703116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS &&
704116742Ssam		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
705116742Ssam		    ic->ic_des_esslen != 0) {
706116742Ssam			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
707116742Ssam			return;
708116742Ssam		}
709116742Ssam		/*
710148432Ssam		 * Decrement the failure counts so entries will be
711148432Ssam		 * reconsidered the next time around.  We really want
712148432Ssam		 * to do this only for sta's where we've previously
713153352Ssam		 * had some success.
714148432Ssam		 */
715148432Ssam		IEEE80211_NODE_LOCK(nt);
716148432Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
717148432Ssam			if (ni->ni_fails)
718148432Ssam				ni->ni_fails--;
719148432Ssam		IEEE80211_NODE_UNLOCK(nt);
720148432Ssam		/*
721116742Ssam		 * Reset the list of channels to scan and start again.
722116742Ssam		 */
723138568Ssam		ieee80211_reset_scan(ic);
724138568Ssam		ic->ic_flags |= IEEE80211_F_SCAN;
725138568Ssam		ieee80211_next_scan(ic);
726116742Ssam		return;
727116742Ssam	}
728116742Ssam	selbs = NULL;
729138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
730138568Ssam	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
731140448Ssam	IEEE80211_NODE_LOCK(nt);
732140448Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
733138568Ssam		if (ieee80211_match_bss(ic, ni) == 0) {
734116742Ssam			if (selbs == NULL)
735116742Ssam				selbs = ni;
736140448Ssam			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
737116742Ssam				selbs = ni;
738116742Ssam		}
739116742Ssam	}
740140448Ssam	if (selbs != NULL)		/* NB: grab ref while dropping lock */
741140448Ssam		(void) ieee80211_ref_node(selbs);
742140448Ssam	IEEE80211_NODE_UNLOCK(nt);
743116742Ssam	if (selbs == NULL)
744116742Ssam		goto notfound;
745138568Ssam	if (!ieee80211_sta_join(ic, selbs)) {
746140448Ssam		ieee80211_free_node(selbs);
747138568Ssam		goto notfound;
748138568Ssam	}
749138568Ssam}
750138568Ssam
751138568Ssam/*
752138568Ssam * Handle 802.11 ad hoc network merge.  The
753138568Ssam * convention, set by the Wireless Ethernet Compatibility Alliance
754138568Ssam * (WECA), is that an 802.11 station will change its BSSID to match
755138568Ssam * the "oldest" 802.11 ad hoc network, on the same channel, that
756138568Ssam * has the station's desired SSID.  The "oldest" 802.11 network
757138568Ssam * sends beacons with the greatest TSF timestamp.
758138568Ssam *
759138568Ssam * The caller is assumed to validate TSF's before attempting a merge.
760138568Ssam *
761138568Ssam * Return !0 if the BSSID changed, 0 otherwise.
762138568Ssam */
763138568Ssamint
764148306Ssamieee80211_ibss_merge(struct ieee80211_node *ni)
765138568Ssam{
766148306Ssam	struct ieee80211com *ic = ni->ni_ic;
767138568Ssam
768140453Ssam	if (ni == ic->ic_bss ||
769140453Ssam	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
770138568Ssam		/* unchanged, nothing to do */
771138568Ssam		return 0;
772138568Ssam	}
773138568Ssam	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
774138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
775138568Ssam		    "%s: merge failed, capabilities mismatch\n", __func__);
776138568Ssam		ic->ic_stats.is_ibss_capmismatch++;
777138568Ssam		return 0;
778138568Ssam	}
779138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
780138568Ssam		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
781138568Ssam		ether_sprintf(ni->ni_bssid),
782138568Ssam		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
783138568Ssam		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
784138568Ssam		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
785138568Ssam	);
786140753Ssam	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
787138568Ssam}
788138568Ssam
789138568Ssam/*
790138568Ssam * Join the specified IBSS/BSS network.  The node is assumed to
791138568Ssam * be passed in with a held reference.
792138568Ssam */
793138568Ssamint
794138568Ssamieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
795138568Ssam{
796138568Ssam	struct ieee80211_node *obss;
797138568Ssam
798116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
799140753Ssam		struct ieee80211_node_table *nt;
800138568Ssam		/*
801140440Ssam		 * Delete unusable rates; we've already checked
802140440Ssam		 * that the negotiated rate set is acceptable.
803138568Ssam		 */
804148299Ssam		ieee80211_fix_rate(selbs, IEEE80211_F_DODEL);
805127772Ssam		/*
806140753Ssam		 * Fillin the neighbor table; it will already
807139521Ssam		 * exist if we are simply switching mastership.
808140753Ssam		 * XXX ic_sta always setup so this is unnecessary?
809127772Ssam		 */
810140753Ssam		nt = &ic->ic_sta;
811140753Ssam		IEEE80211_NODE_LOCK(nt);
812140753Ssam		nt->nt_name = "neighbor";
813140753Ssam		nt->nt_inact_init = ic->ic_inact_run;
814140753Ssam		IEEE80211_NODE_UNLOCK(nt);
815138568Ssam	}
816138568Ssam
817138568Ssam	/*
818138568Ssam	 * Committed to selbs, setup state.
819138568Ssam	 */
820138568Ssam	obss = ic->ic_bss;
821140753Ssam	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
822153352Ssam	if (obss != NULL) {
823153352Ssam		copy_bss(selbs, obss);
824138568Ssam		ieee80211_free_node(obss);
825153352Ssam	}
826138568Ssam	/*
827138568Ssam	 * Set the erp state (mostly the slot time) to deal with
828138568Ssam	 * the auto-select case; this should be redundant if the
829138568Ssam	 * mode is locked.
830138568Ssam	 */
831138568Ssam	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
832148936Ssam	ic->ic_curchan = selbs->ni_chan;
833138568Ssam	ieee80211_reset_erp(ic);
834138568Ssam	ieee80211_wme_initparams(ic);
835140753Ssam
836140753Ssam	if (ic->ic_opmode == IEEE80211_M_STA)
837140753Ssam		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
838140753Ssam	else
839117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
840138568Ssam	return 1;
841116742Ssam}
842116742Ssam
843138568Ssam/*
844138568Ssam * Leave the specified IBSS/BSS network.  The node is assumed to
845138568Ssam * be passed in with a held reference.
846138568Ssam */
847138568Ssamvoid
848138568Ssamieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
849138568Ssam{
850138568Ssam	ic->ic_node_cleanup(ni);
851138568Ssam	ieee80211_notify_node_leave(ic, ni);
852138568Ssam}
853138568Ssam
854116742Ssamstatic struct ieee80211_node *
855138568Ssamnode_alloc(struct ieee80211_node_table *nt)
856116742Ssam{
857127768Ssam	struct ieee80211_node *ni;
858138568Ssam
859127768Ssam	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
860127768Ssam		M_80211_NODE, M_NOWAIT | M_ZERO);
861127768Ssam	return ni;
862116742Ssam}
863116742Ssam
864138568Ssam/*
865138568Ssam * Reclaim any resources in a node and reset any critical
866138568Ssam * state.  Typically nodes are free'd immediately after,
867138568Ssam * but in some cases the storage may be reused so we need
868138568Ssam * to insure consistent state (should probably fix that).
869138568Ssam */
870116742Ssamstatic void
871138568Ssamnode_cleanup(struct ieee80211_node *ni)
872116742Ssam{
873138568Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
874138568Ssam	struct ieee80211com *ic = ni->ni_ic;
875138568Ssam	int i, qlen;
876138568Ssam
877138568Ssam	/* NB: preserve ni_table */
878138568Ssam	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
879138568Ssam		ic->ic_ps_sta--;
880138568Ssam		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
881138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
882138568Ssam		    "[%s] power save mode off, %u sta's in ps mode\n",
883138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
884138568Ssam	}
885147788Ssam	/*
886147788Ssam	 * Clear AREF flag that marks the authorization refcnt bump
887147788Ssam	 * has happened.  This is probably not needed as the node
888147788Ssam	 * should always be removed from the table so not found but
889147788Ssam	 * do it just in case.
890147788Ssam	 */
891147788Ssam	ni->ni_flags &= ~IEEE80211_NODE_AREF;
892138568Ssam
893138568Ssam	/*
894138568Ssam	 * Drain power save queue and, if needed, clear TIM.
895138568Ssam	 */
896138568Ssam	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
897138568Ssam	if (qlen != 0 && ic->ic_set_tim != NULL)
898148304Ssam		ic->ic_set_tim(ni, 0);
899138568Ssam
900138568Ssam	ni->ni_associd = 0;
901138568Ssam	if (ni->ni_challenge != NULL) {
902138568Ssam		FREE(ni->ni_challenge, M_DEVBUF);
903138568Ssam		ni->ni_challenge = NULL;
904138568Ssam	}
905138568Ssam	/*
906138568Ssam	 * Preserve SSID, WPA, and WME ie's so the bss node is
907138568Ssam	 * reusable during a re-auth/re-assoc state transition.
908138568Ssam	 * If we remove these data they will not be recreated
909138568Ssam	 * because they come from a probe-response or beacon frame
910138568Ssam	 * which cannot be expected prior to the association-response.
911138568Ssam	 * This should not be an issue when operating in other modes
912138568Ssam	 * as stations leaving always go through a full state transition
913138568Ssam	 * which will rebuild this state.
914138568Ssam	 *
915138568Ssam	 * XXX does this leave us open to inheriting old state?
916138568Ssam	 */
917138568Ssam	for (i = 0; i < N(ni->ni_rxfrag); i++)
918138568Ssam		if (ni->ni_rxfrag[i] != NULL) {
919138568Ssam			m_freem(ni->ni_rxfrag[i]);
920138568Ssam			ni->ni_rxfrag[i] = NULL;
921138568Ssam		}
922148863Ssam	/*
923148863Ssam	 * Must be careful here to remove any key map entry w/o a LOR.
924148863Ssam	 */
925148863Ssam	ieee80211_node_delucastkey(ni);
926138568Ssam#undef N
927116742Ssam}
928116742Ssam
929116742Ssamstatic void
930138568Ssamnode_free(struct ieee80211_node *ni)
931116742Ssam{
932138568Ssam	struct ieee80211com *ic = ni->ni_ic;
933138568Ssam
934138568Ssam	ic->ic_node_cleanup(ni);
935138568Ssam	if (ni->ni_wpa_ie != NULL)
936138568Ssam		FREE(ni->ni_wpa_ie, M_DEVBUF);
937138568Ssam	if (ni->ni_wme_ie != NULL)
938138568Ssam		FREE(ni->ni_wme_ie, M_DEVBUF);
939138568Ssam	IEEE80211_NODE_SAVEQ_DESTROY(ni);
940138568Ssam	FREE(ni, M_80211_NODE);
941116742Ssam}
942116742Ssam
943120104Ssamstatic u_int8_t
944138568Ssamnode_getrssi(const struct ieee80211_node *ni)
945120104Ssam{
946120104Ssam	return ni->ni_rssi;
947120104Ssam}
948120104Ssam
949116742Ssamstatic void
950138568Ssamieee80211_setup_node(struct ieee80211_node_table *nt,
951138568Ssam	struct ieee80211_node *ni, const u_int8_t *macaddr)
952116742Ssam{
953138568Ssam	struct ieee80211com *ic = nt->nt_ic;
954116742Ssam	int hash;
955116742Ssam
956138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
957140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
958138568Ssam		ether_sprintf(macaddr), nt->nt_name);
959138568Ssam
960116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
961116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
962138568Ssam	ieee80211_node_initref(ni);		/* mark referenced */
963138568Ssam	ni->ni_chan = IEEE80211_CHAN_ANYC;
964138568Ssam	ni->ni_authmode = IEEE80211_AUTH_OPEN;
965138568Ssam	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
966138568Ssam	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
967139528Ssam	ni->ni_inact_reload = nt->nt_inact_init;
968139528Ssam	ni->ni_inact = ni->ni_inact_reload;
969138568Ssam	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
970138568Ssam
971138568Ssam	IEEE80211_NODE_LOCK(nt);
972138568Ssam	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
973138568Ssam	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
974138568Ssam	ni->ni_table = nt;
975138568Ssam	ni->ni_ic = ic;
976138568Ssam	IEEE80211_NODE_UNLOCK(nt);
977116742Ssam}
978116742Ssam
979116742Ssamstruct ieee80211_node *
980138568Ssamieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
981116742Ssam{
982138568Ssam	struct ieee80211com *ic = nt->nt_ic;
983138568Ssam	struct ieee80211_node *ni;
984138568Ssam
985138568Ssam	ni = ic->ic_node_alloc(nt);
986116742Ssam	if (ni != NULL)
987138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
988127769Ssam	else
989127769Ssam		ic->ic_stats.is_rx_nodealloc++;
990116742Ssam	return ni;
991116742Ssam}
992116742Ssam
993148777Ssam/*
994148777Ssam * Craft a temporary node suitable for sending a management frame
995148777Ssam * to the specified station.  We craft only as much state as we
996148777Ssam * need to do the work since the node will be immediately reclaimed
997148777Ssam * once the send completes.
998148777Ssam */
999116742Ssamstruct ieee80211_node *
1000148777Ssamieee80211_tmp_node(struct ieee80211com *ic, const u_int8_t *macaddr)
1001148777Ssam{
1002148777Ssam	struct ieee80211_node *ni;
1003148777Ssam
1004148777Ssam	ni = ic->ic_node_alloc(&ic->ic_sta);
1005148777Ssam	if (ni != NULL) {
1006148777Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1007148777Ssam			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
1008148777Ssam
1009148777Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
1010148777Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1011148777Ssam		ieee80211_node_initref(ni);		/* mark referenced */
1012148777Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
1013148777Ssam		/* NB: required by ieee80211_fix_rate */
1014148777Ssam		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1015148777Ssam		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
1016148777Ssam			IEEE80211_KEYIX_NONE);
1017148777Ssam		/* XXX optimize away */
1018148777Ssam		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
1019148777Ssam
1020148777Ssam		ni->ni_table = NULL;		/* NB: pedantic */
1021148777Ssam		ni->ni_ic = ic;
1022148777Ssam	} else {
1023148777Ssam		/* XXX msg */
1024148777Ssam		ic->ic_stats.is_rx_nodealloc++;
1025148777Ssam	}
1026148777Ssam	return ni;
1027148777Ssam}
1028148777Ssam
1029148777Ssamstruct ieee80211_node *
1030138568Ssamieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1031116742Ssam{
1032138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1033138568Ssam	struct ieee80211_node *ni;
1034138568Ssam
1035138568Ssam	ni = ic->ic_node_alloc(nt);
1036116742Ssam	if (ni != NULL) {
1037138568Ssam		ieee80211_setup_node(nt, ni, macaddr);
1038127770Ssam		/*
1039127770Ssam		 * Inherit from ic_bss.
1040127770Ssam		 */
1041138568Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
1042138568Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
1043138568Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1044127770Ssam		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
1045138568Ssam		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
1046138568Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
1047127770Ssam	} else
1048127770Ssam		ic->ic_stats.is_rx_nodealloc++;
1049116742Ssam	return ni;
1050116742Ssam}
1051116742Ssam
1052127772Ssamstatic struct ieee80211_node *
1053138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1054138568Ssam_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
1055138568Ssam	const u_int8_t *macaddr, const char *func, int line)
1056138568Ssam#else
1057138568Ssam_ieee80211_find_node(struct ieee80211_node_table *nt,
1058138568Ssam	const u_int8_t *macaddr)
1059138568Ssam#endif
1060116742Ssam{
1061116742Ssam	struct ieee80211_node *ni;
1062116742Ssam	int hash;
1063116742Ssam
1064138568Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1065127772Ssam
1066116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1067138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1068116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
1069138568Ssam			ieee80211_ref_node(ni);	/* mark referenced */
1070138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1071138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1072140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1073140766Ssam			    func, line,
1074140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1075140766Ssam			    ieee80211_node_refcnt(ni));
1076138568Ssam#endif
1077127772Ssam			return ni;
1078116742Ssam		}
1079116742Ssam	}
1080127772Ssam	return NULL;
1081127772Ssam}
1082138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1083138568Ssam#define	_ieee80211_find_node(nt, mac) \
1084138568Ssam	_ieee80211_find_node_debug(nt, mac, func, line)
1085138568Ssam#endif
1086127772Ssam
1087127772Ssamstruct ieee80211_node *
1088138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1089138568Ssamieee80211_find_node_debug(struct ieee80211_node_table *nt,
1090138568Ssam	const u_int8_t *macaddr, const char *func, int line)
1091138568Ssam#else
1092138568Ssamieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1093138568Ssam#endif
1094127772Ssam{
1095127772Ssam	struct ieee80211_node *ni;
1096127772Ssam
1097138568Ssam	IEEE80211_NODE_LOCK(nt);
1098138568Ssam	ni = _ieee80211_find_node(nt, macaddr);
1099138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1100116742Ssam	return ni;
1101116742Ssam}
1102116742Ssam
1103116742Ssam/*
1104138568Ssam * Fake up a node; this handles node discovery in adhoc mode.
1105138568Ssam * Note that for the driver's benefit we we treat this like
1106138568Ssam * an association so the driver has an opportunity to setup
1107138568Ssam * it's private state.
1108138568Ssam */
1109138568Ssamstruct ieee80211_node *
1110138568Ssamieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1111138568Ssam	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1112138568Ssam{
1113138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1114138568Ssam	struct ieee80211_node *ni;
1115138568Ssam
1116153073Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1117153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
1118138568Ssam	ni = ieee80211_dup_bss(nt, macaddr);
1119138568Ssam	if (ni != NULL) {
1120138568Ssam		/* XXX no rate negotiation; just dup */
1121138568Ssam		ni->ni_rates = ic->ic_bss->ni_rates;
1122139524Ssam		if (ic->ic_newassoc != NULL)
1123148307Ssam			ic->ic_newassoc(ni, 1);
1124138568Ssam		/* XXX not right for 802.1x/WPA */
1125148302Ssam		ieee80211_node_authorize(ni);
1126153404Ssam		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
1127153404Ssam			/*
1128153404Ssam			 * Blindly propagate capabilities based on the
1129153404Ssam			 * local configuration.  In particular this permits
1130153404Ssam			 * us to use QoS to disable ACK's.
1131153404Ssam			 */
1132153404Ssam			if (ic->ic_flags & IEEE80211_F_WME)
1133153404Ssam				ni->ni_flags |= IEEE80211_NODE_QOS;
1134153404Ssam		}
1135138568Ssam	}
1136138568Ssam	return ni;
1137138568Ssam}
1138138568Ssam
1139148936Ssam#ifdef IEEE80211_DEBUG
1140148936Ssamstatic void
1141148936Ssamdump_probe_beacon(u_int8_t subtype, int isnew,
1142148936Ssam	const u_int8_t mac[IEEE80211_ADDR_LEN],
1143148936Ssam	const struct ieee80211_scanparams *sp)
1144148936Ssam{
1145148936Ssam
1146148936Ssam	printf("[%s] %s%s on chan %u (bss chan %u) ",
1147148936Ssam	    ether_sprintf(mac), isnew ? "new " : "",
1148148936Ssam	    ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT],
1149148936Ssam	    sp->chan, sp->bchan);
1150148936Ssam	ieee80211_print_essid(sp->ssid + 2, sp->ssid[1]);
1151148936Ssam	printf("\n");
1152148936Ssam
1153148936Ssam	if (isnew) {
1154148936Ssam		printf("[%s] caps 0x%x bintval %u erp 0x%x",
1155148936Ssam			ether_sprintf(mac), sp->capinfo, sp->bintval, sp->erp);
1156148936Ssam		if (sp->country != NULL) {
1157148936Ssam#ifdef __FreeBSD__
1158148936Ssam			printf(" country info %*D",
1159148936Ssam				sp->country[1], sp->country+2, " ");
1160148936Ssam#else
1161148936Ssam			int i;
1162148936Ssam			printf(" country info");
1163148936Ssam			for (i = 0; i < sp->country[1]; i++)
1164148936Ssam				printf(" %02x", sp->country[i+2]);
1165148936Ssam#endif
1166148936Ssam		}
1167148936Ssam		printf("\n");
1168148936Ssam	}
1169148936Ssam}
1170148936Ssam#endif /* IEEE80211_DEBUG */
1171148936Ssam
1172148936Ssamstatic void
1173148936Ssamsaveie(u_int8_t **iep, const u_int8_t *ie)
1174148936Ssam{
1175148936Ssam
1176148936Ssam	if (ie == NULL)
1177148936Ssam		*iep = NULL;
1178148936Ssam	else
1179148936Ssam		ieee80211_saveie(iep, ie);
1180148936Ssam}
1181148936Ssam
1182148936Ssam/*
1183148936Ssam * Process a beacon or probe response frame.
1184148936Ssam */
1185148936Ssamvoid
1186148936Ssamieee80211_add_scan(struct ieee80211com *ic,
1187148936Ssam	const struct ieee80211_scanparams *sp,
1188148936Ssam	const struct ieee80211_frame *wh,
1189148936Ssam	int subtype, int rssi, int rstamp)
1190148936Ssam{
1191148936Ssam#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1192148936Ssam	struct ieee80211_node_table *nt = &ic->ic_scan;
1193148936Ssam	struct ieee80211_node *ni;
1194148936Ssam	int newnode = 0;
1195148936Ssam
1196148936Ssam	ni = ieee80211_find_node(nt, wh->i_addr2);
1197148936Ssam	if (ni == NULL) {
1198148936Ssam		/*
1199148936Ssam		 * Create a new entry.
1200148936Ssam		 */
1201148936Ssam		ni = ic->ic_node_alloc(nt);
1202148936Ssam		if (ni == NULL) {
1203148936Ssam			ic->ic_stats.is_rx_nodealloc++;
1204148936Ssam			return;
1205148936Ssam		}
1206148936Ssam		ieee80211_setup_node(nt, ni, wh->i_addr2);
1207148936Ssam		/*
1208148936Ssam		 * XXX inherit from ic_bss.
1209148936Ssam		 */
1210148936Ssam		ni->ni_authmode = ic->ic_bss->ni_authmode;
1211148936Ssam		ni->ni_txpower = ic->ic_bss->ni_txpower;
1212148936Ssam		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
1213148936Ssam		ieee80211_set_chan(ic, ni, ic->ic_curchan);
1214148936Ssam		ni->ni_rsn = ic->ic_bss->ni_rsn;
1215148936Ssam		newnode = 1;
1216148936Ssam	}
1217148936Ssam#ifdef IEEE80211_DEBUG
1218148936Ssam	if (ieee80211_msg_scan(ic) && (ic->ic_flags & IEEE80211_F_SCAN))
1219148936Ssam		dump_probe_beacon(subtype, newnode, wh->i_addr2, sp);
1220148936Ssam#endif
1221148936Ssam	/* XXX ap beaconing multiple ssid w/ same bssid */
1222148936Ssam	if (sp->ssid[1] != 0 &&
1223148936Ssam	    (ISPROBE(subtype) || ni->ni_esslen == 0)) {
1224148936Ssam		ni->ni_esslen = sp->ssid[1];
1225148936Ssam		memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
1226148936Ssam		memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1227148936Ssam	}
1228148936Ssam	ni->ni_scangen = ic->ic_scan.nt_scangen;
1229148936Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1230148936Ssam	ni->ni_rssi = rssi;
1231148936Ssam	ni->ni_rstamp = rstamp;
1232148936Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1233148936Ssam	ni->ni_intval = sp->bintval;
1234148936Ssam	ni->ni_capinfo = sp->capinfo;
1235148936Ssam	ni->ni_chan = &ic->ic_channels[sp->chan];
1236148936Ssam	ni->ni_fhdwell = sp->fhdwell;
1237148936Ssam	ni->ni_fhindex = sp->fhindex;
1238148936Ssam	ni->ni_erp = sp->erp;
1239148936Ssam	if (sp->tim != NULL) {
1240148936Ssam		struct ieee80211_tim_ie *ie =
1241148936Ssam		    (struct ieee80211_tim_ie *) sp->tim;
1242148936Ssam
1243148936Ssam		ni->ni_dtim_count = ie->tim_count;
1244148936Ssam		ni->ni_dtim_period = ie->tim_period;
1245148936Ssam	}
1246148936Ssam	/*
1247148936Ssam	 * Record the byte offset from the mac header to
1248148936Ssam	 * the start of the TIM information element for
1249148936Ssam	 * use by hardware and/or to speedup software
1250148936Ssam	 * processing of beacon frames.
1251148936Ssam	 */
1252148936Ssam	ni->ni_timoff = sp->timoff;
1253148936Ssam	/*
1254148936Ssam	 * Record optional information elements that might be
1255148936Ssam	 * used by applications or drivers.
1256148936Ssam	 */
1257148936Ssam	saveie(&ni->ni_wme_ie, sp->wme);
1258148936Ssam	saveie(&ni->ni_wpa_ie, sp->wpa);
1259148936Ssam
1260148936Ssam	/* NB: must be after ni_chan is setup */
1261148936Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1262148936Ssam
1263148936Ssam	if (!newnode)
1264148936Ssam		ieee80211_free_node(ni);
1265148936Ssam#undef ISPROBE
1266148936Ssam}
1267148936Ssam
1268153073Ssamvoid
1269153073Ssamieee80211_init_neighbor(struct ieee80211_node *ni,
1270153073Ssam	const struct ieee80211_frame *wh,
1271153073Ssam	const struct ieee80211_scanparams *sp)
1272153073Ssam{
1273153073Ssam
1274153073Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1275153073Ssam	    "%s: %p<%s>\n", __func__, ni, ether_sprintf(ni->ni_macaddr));
1276153073Ssam	ni->ni_esslen = sp->ssid[1];
1277153073Ssam	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
1278153073Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1279153073Ssam	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
1280153073Ssam	ni->ni_intval = sp->bintval;
1281153073Ssam	ni->ni_capinfo = sp->capinfo;
1282153073Ssam	ni->ni_chan = ni->ni_ic->ic_curchan;
1283153073Ssam	ni->ni_fhdwell = sp->fhdwell;
1284153073Ssam	ni->ni_fhindex = sp->fhindex;
1285153073Ssam	ni->ni_erp = sp->erp;
1286153073Ssam	ni->ni_timoff = sp->timoff;
1287153073Ssam	if (sp->wme != NULL)
1288153073Ssam		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1289153073Ssam	if (sp->wpa != NULL)
1290153073Ssam		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1291153073Ssam
1292153073Ssam	/* NB: must be after ni_chan is setup */
1293153073Ssam	ieee80211_setup_rates(ni, sp->rates, sp->xrates, IEEE80211_F_DOSORT);
1294153073Ssam}
1295153073Ssam
1296148936Ssam/*
1297148936Ssam * Do node discovery in adhoc mode on receipt of a beacon
1298148936Ssam * or probe response frame.  Note that for the driver's
1299148936Ssam * benefit we we treat this like an association so the
1300148936Ssam * driver has an opportunity to setup it's private state.
1301148936Ssam */
1302148936Ssamstruct ieee80211_node *
1303148936Ssamieee80211_add_neighbor(struct ieee80211com *ic,
1304148936Ssam	const struct ieee80211_frame *wh,
1305148936Ssam	const struct ieee80211_scanparams *sp)
1306148936Ssam{
1307148936Ssam	struct ieee80211_node *ni;
1308148936Ssam
1309153073Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1310153073Ssam	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1311148936Ssam	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1312148936Ssam	if (ni != NULL) {
1313153073Ssam		ieee80211_init_neighbor(ni, wh, sp);
1314148936Ssam		if (ic->ic_newassoc != NULL)
1315148936Ssam			ic->ic_newassoc(ni, 1);
1316148936Ssam		/* XXX not right for 802.1x/WPA */
1317148936Ssam		ieee80211_node_authorize(ni);
1318148936Ssam	}
1319148936Ssam	return ni;
1320148936Ssam}
1321148936Ssam
1322148863Ssam#define	IS_CTL(wh) \
1323148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1324148863Ssam#define	IS_PSPOLL(wh) \
1325148863Ssam	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1326138568Ssam/*
1327138568Ssam * Locate the node for sender, track state, and then pass the
1328138568Ssam * (referenced) node up to the 802.11 layer for its use.  We
1329138568Ssam * are required to pass some node so we fall back to ic_bss
1330138568Ssam * when this frame is from an unknown sender.  The 802.11 layer
1331138568Ssam * knows this means the sender wasn't in the node table and
1332138568Ssam * acts accordingly.
1333138568Ssam */
1334138568Ssamstruct ieee80211_node *
1335138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1336138568Ssamieee80211_find_rxnode_debug(struct ieee80211com *ic,
1337138568Ssam	const struct ieee80211_frame_min *wh, const char *func, int line)
1338138568Ssam#else
1339138568Ssamieee80211_find_rxnode(struct ieee80211com *ic,
1340138568Ssam	const struct ieee80211_frame_min *wh)
1341138568Ssam#endif
1342138568Ssam{
1343138568Ssam	struct ieee80211_node_table *nt;
1344138568Ssam	struct ieee80211_node *ni;
1345138568Ssam
1346138568Ssam	/* XXX may want scanned nodes in the neighbor table for adhoc */
1347138568Ssam	if (ic->ic_opmode == IEEE80211_M_STA ||
1348138568Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1349138568Ssam	    (ic->ic_flags & IEEE80211_F_SCAN))
1350138568Ssam		nt = &ic->ic_scan;
1351138568Ssam	else
1352140753Ssam		nt = &ic->ic_sta;
1353138568Ssam	/* XXX check ic_bss first in station mode */
1354138568Ssam	/* XXX 4-address frames? */
1355138568Ssam	IEEE80211_NODE_LOCK(nt);
1356138568Ssam	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1357138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr1);
1358138568Ssam	else
1359138568Ssam		ni = _ieee80211_find_node(nt, wh->i_addr2);
1360148863Ssam	if (ni == NULL)
1361148863Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1362138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1363138568Ssam
1364148863Ssam	return ni;
1365148863Ssam}
1366148863Ssam
1367148863Ssam/*
1368148863Ssam * Like ieee80211_find_rxnode but use the supplied h/w
1369148863Ssam * key index as a hint to locate the node in the key
1370148863Ssam * mapping table.  If an entry is present at the key
1371148863Ssam * index we return it; otherwise do a normal lookup and
1372148863Ssam * update the mapping table if the station has a unicast
1373148863Ssam * key assigned to it.
1374148863Ssam */
1375148863Ssamstruct ieee80211_node *
1376148863Ssam#ifdef IEEE80211_DEBUG_REFCNT
1377148863Ssamieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1378148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1379148863Ssam	const char *func, int line)
1380148863Ssam#else
1381148863Ssamieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1382148863Ssam	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1383148863Ssam#endif
1384148863Ssam{
1385148863Ssam	struct ieee80211_node_table *nt;
1386148863Ssam	struct ieee80211_node *ni;
1387148863Ssam
1388148863Ssam	if (ic->ic_opmode == IEEE80211_M_STA ||
1389148863Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1390148863Ssam	    (ic->ic_flags & IEEE80211_F_SCAN))
1391148863Ssam		nt = &ic->ic_scan;
1392148863Ssam	else
1393148863Ssam		nt = &ic->ic_sta;
1394148863Ssam	IEEE80211_NODE_LOCK(nt);
1395148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1396148863Ssam		ni = nt->nt_keyixmap[keyix];
1397148863Ssam	else
1398148863Ssam		ni = NULL;
1399148863Ssam	if (ni == NULL) {
1400148863Ssam		if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1401148863Ssam			ni = _ieee80211_find_node(nt, wh->i_addr1);
1402148863Ssam		else
1403148863Ssam			ni = _ieee80211_find_node(nt, wh->i_addr2);
1404148863Ssam		if (ni == NULL)
1405148863Ssam			ni = ieee80211_ref_node(ic->ic_bss);
1406148863Ssam		if (nt->nt_keyixmap != NULL) {
1407148863Ssam			/*
1408148863Ssam			 * If the station has a unicast key cache slot
1409148863Ssam			 * assigned update the key->node mapping table.
1410148863Ssam			 */
1411148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1412148863Ssam			/* XXX can keyixmap[keyix] != NULL? */
1413148863Ssam			if (keyix < nt->nt_keyixmax &&
1414148863Ssam			    nt->nt_keyixmap[keyix] == NULL) {
1415148863Ssam				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1416148863Ssam				    "%s: add key map entry %p<%s> refcnt %d\n",
1417148863Ssam				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1418148863Ssam				    ieee80211_node_refcnt(ni)+1);
1419148863Ssam				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1420148863Ssam			}
1421148863Ssam		}
1422148863Ssam	} else {
1423148863Ssam		ieee80211_ref_node(ni);
1424148863Ssam	}
1425148863Ssam	IEEE80211_NODE_UNLOCK(nt);
1426148863Ssam
1427148863Ssam	return ni;
1428148863Ssam}
1429138568Ssam#undef IS_PSPOLL
1430138568Ssam#undef IS_CTL
1431138568Ssam
1432138568Ssam/*
1433127772Ssam * Return a reference to the appropriate node for sending
1434127772Ssam * a data frame.  This handles node discovery in adhoc networks.
1435127772Ssam */
1436127772Ssamstruct ieee80211_node *
1437138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1438138568Ssamieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1439138568Ssam	const char *func, int line)
1440138568Ssam#else
1441138568Ssamieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1442138568Ssam#endif
1443127772Ssam{
1444140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1445127772Ssam	struct ieee80211_node *ni;
1446127772Ssam
1447127772Ssam	/*
1448127772Ssam	 * The destination address should be in the node table
1449148863Ssam	 * unless this is a multicast/broadcast frame.  We can
1450148863Ssam	 * also optimize station mode operation, all frames go
1451148863Ssam	 * to the bss node.
1452127772Ssam	 */
1453127772Ssam	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1454138568Ssam	IEEE80211_NODE_LOCK(nt);
1455148863Ssam	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1456148863Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1457158121Ssam	else {
1458148863Ssam		ni = _ieee80211_find_node(nt, macaddr);
1459158121Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1460158121Ssam		    (ni != NULL && ni->ni_associd == 0)) {
1461158121Ssam			/*
1462158121Ssam			 * Station is not associated; don't permit the
1463158121Ssam			 * data frame to be sent by returning NULL.  This
1464158121Ssam			 * is kinda a kludge but the least intrusive way
1465158121Ssam			 * to add this check into all drivers.
1466158121Ssam			 */
1467158121Ssam			ieee80211_unref_node(&ni);	/* NB: null's ni */
1468158121Ssam		}
1469158121Ssam	}
1470138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1471138568Ssam
1472138568Ssam	if (ni == NULL) {
1473138568Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1474140497Ssam		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
1475140497Ssam			/*
1476140497Ssam			 * In adhoc mode cons up a node for the destination.
1477140497Ssam			 * Note that we need an additional reference for the
1478140497Ssam			 * caller to be consistent with _ieee80211_find_node.
1479140497Ssam			 */
1480138568Ssam			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1481140497Ssam			if (ni != NULL)
1482140497Ssam				(void) ieee80211_ref_node(ni);
1483140497Ssam		} else {
1484138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1485138568Ssam				"[%s] no node, discard frame (%s)\n",
1486138568Ssam				ether_sprintf(macaddr), __func__);
1487138568Ssam			ic->ic_stats.is_tx_nonode++;
1488127772Ssam		}
1489127772Ssam	}
1490127772Ssam	return ni;
1491127772Ssam}
1492127772Ssam
1493127772Ssam/*
1494116742Ssam * Like find but search based on the channel too.
1495116742Ssam */
1496116742Ssamstruct ieee80211_node *
1497138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1498138568Ssamieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1499138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan,
1500138568Ssam	const char *func, int line)
1501138568Ssam#else
1502138568Ssamieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1503138568Ssam	const u_int8_t *macaddr, struct ieee80211_channel *chan)
1504138568Ssam#endif
1505116742Ssam{
1506116742Ssam	struct ieee80211_node *ni;
1507116742Ssam	int hash;
1508116742Ssam
1509116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
1510138568Ssam	IEEE80211_NODE_LOCK(nt);
1511138568Ssam	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1512127772Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1513127772Ssam		    ni->ni_chan == chan) {
1514138568Ssam			ieee80211_ref_node(ni);		/* mark referenced */
1515138568Ssam			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1516138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1517140766Ssam			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1518140766Ssam			    func, line,
1519138568Ssam#else
1520140766Ssam			    "%s %p<%s> refcnt %d\n", __func__,
1521138568Ssam#endif
1522140766Ssam			    ni, ether_sprintf(ni->ni_macaddr),
1523140766Ssam			    ieee80211_node_refcnt(ni));
1524116742Ssam			break;
1525116742Ssam		}
1526116742Ssam	}
1527138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1528116742Ssam	return ni;
1529116742Ssam}
1530116742Ssam
1531138568Ssam/*
1532138568Ssam * Like find but search based on the ssid too.
1533138568Ssam */
1534138568Ssamstruct ieee80211_node *
1535138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1536138568Ssamieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1537138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1538138568Ssam	const char *func, int line)
1539138568Ssam#else
1540138568Ssamieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1541138568Ssam	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1542138568Ssam#endif
1543138568Ssam{
1544147118Ssam#define	MATCH_SSID(ni, ssid, ssidlen) \
1545147118Ssam	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1546147118Ssam	static const u_int8_t zeromac[IEEE80211_ADDR_LEN];
1547138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1548138568Ssam	struct ieee80211_node *ni;
1549138568Ssam	int hash;
1550138568Ssam
1551138568Ssam	IEEE80211_NODE_LOCK(nt);
1552147118Ssam	/*
1553147118Ssam	 * A mac address that is all zero means match only the ssid;
1554147118Ssam	 * otherwise we must match both.
1555147118Ssam	 */
1556147118Ssam	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1557147118Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1558147118Ssam			if (MATCH_SSID(ni, ssid, ssidlen))
1559147118Ssam				break;
1560147118Ssam		}
1561147118Ssam	} else {
1562147118Ssam		hash = IEEE80211_NODE_HASH(macaddr);
1563147118Ssam		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1564147118Ssam			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1565147118Ssam			    MATCH_SSID(ni, ssid, ssidlen))
1566147118Ssam				break;
1567147118Ssam		}
1568147118Ssam	}
1569147118Ssam	if (ni != NULL) {
1570147118Ssam		ieee80211_ref_node(ni);	/* mark referenced */
1571147118Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1572138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1573147118Ssam		    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
1574147118Ssam		    func, line,
1575138568Ssam#else
1576147118Ssam		    "%s %p<%s> refcnt %d\n", __func__,
1577138568Ssam#endif
1578147118Ssam		     ni, ether_sprintf(ni->ni_macaddr),
1579147118Ssam		     ieee80211_node_refcnt(ni));
1580138568Ssam	}
1581138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1582138568Ssam	return ni;
1583147118Ssam#undef MATCH_SSID
1584138568Ssam}
1585138568Ssam
1586116742Ssamstatic void
1587138568Ssam_ieee80211_free_node(struct ieee80211_node *ni)
1588116742Ssam{
1589138568Ssam	struct ieee80211com *ic = ni->ni_ic;
1590138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1591119150Ssam
1592138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1593140766Ssam		"%s %p<%s> in %s table\n", __func__, ni,
1594140766Ssam		ether_sprintf(ni->ni_macaddr),
1595138568Ssam		nt != NULL ? nt->nt_name : "<gone>");
1596138568Ssam
1597138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1598138568Ssam	if (nt != NULL) {
1599138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1600138568Ssam		LIST_REMOVE(ni, ni_hash);
1601138568Ssam	}
1602138568Ssam	ic->ic_node_free(ni);
1603116742Ssam}
1604116742Ssam
1605116742Ssamvoid
1606138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1607138568Ssamieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1608138568Ssam#else
1609138568Ssamieee80211_free_node(struct ieee80211_node *ni)
1610138568Ssam#endif
1611116742Ssam{
1612138568Ssam	struct ieee80211_node_table *nt = ni->ni_table;
1613119150Ssam
1614138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
1615140454Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1616140766Ssam		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1617138568Ssam		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1618138568Ssam#endif
1619148863Ssam	if (nt != NULL) {
1620148863Ssam		IEEE80211_NODE_LOCK(nt);
1621148863Ssam		if (ieee80211_node_dectestref(ni)) {
1622148863Ssam			/*
1623148863Ssam			 * Last reference, reclaim state.
1624148863Ssam			 */
1625138568Ssam			_ieee80211_free_node(ni);
1626148863Ssam		} else if (ieee80211_node_refcnt(ni) == 1 &&
1627148863Ssam		    nt->nt_keyixmap != NULL) {
1628148863Ssam			ieee80211_keyix keyix;
1629148863Ssam			/*
1630148863Ssam			 * Check for a last reference in the key mapping table.
1631148863Ssam			 */
1632148863Ssam			keyix = ni->ni_ucastkey.wk_rxkeyix;
1633148863Ssam			if (keyix < nt->nt_keyixmax &&
1634148863Ssam			    nt->nt_keyixmap[keyix] == ni) {
1635148863Ssam				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1636148863Ssam				    "%s: %p<%s> clear key map entry", __func__,
1637148863Ssam				    ni, ether_sprintf(ni->ni_macaddr));
1638148863Ssam				nt->nt_keyixmap[keyix] = NULL;
1639148863Ssam				ieee80211_node_decref(ni); /* XXX needed? */
1640148863Ssam				_ieee80211_free_node(ni);
1641148863Ssam			}
1642148863Ssam		}
1643148863Ssam		IEEE80211_NODE_UNLOCK(nt);
1644148863Ssam	} else {
1645148863Ssam		if (ieee80211_node_dectestref(ni))
1646138568Ssam			_ieee80211_free_node(ni);
1647116742Ssam	}
1648116742Ssam}
1649116742Ssam
1650138568Ssam/*
1651148863Ssam * Reclaim a unicast key and clear any key cache state.
1652148863Ssam */
1653148863Ssamint
1654148863Ssamieee80211_node_delucastkey(struct ieee80211_node *ni)
1655148863Ssam{
1656148863Ssam	struct ieee80211com *ic = ni->ni_ic;
1657148863Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
1658148863Ssam	struct ieee80211_node *nikey;
1659148863Ssam	ieee80211_keyix keyix;
1660148863Ssam	int isowned, status;
1661148863Ssam
1662148863Ssam	/*
1663148863Ssam	 * NB: We must beware of LOR here; deleting the key
1664148863Ssam	 * can cause the crypto layer to block traffic updates
1665148863Ssam	 * which can generate a LOR against the node table lock;
1666148863Ssam	 * grab it here and stash the key index for our use below.
1667148863Ssam	 *
1668148863Ssam	 * Must also beware of recursion on the node table lock.
1669148863Ssam	 * When called from node_cleanup we may already have
1670148863Ssam	 * the node table lock held.  Unfortunately there's no
1671148863Ssam	 * way to separate out this path so we must do this
1672148863Ssam	 * conditionally.
1673148863Ssam	 */
1674148863Ssam	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1675148863Ssam	if (!isowned)
1676148863Ssam		IEEE80211_NODE_LOCK(nt);
1677148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1678148863Ssam	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1679148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1680148863Ssam		nikey = nt->nt_keyixmap[keyix];
1681148863Ssam		nt->nt_keyixmap[keyix] = NULL;;
1682148863Ssam	} else
1683148863Ssam		nikey = NULL;
1684148863Ssam	if (!isowned)
1685148863Ssam		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1686148863Ssam
1687148863Ssam	if (nikey != NULL) {
1688148863Ssam		KASSERT(nikey == ni,
1689148863Ssam			("key map out of sync, ni %p nikey %p", ni, nikey));
1690148863Ssam		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1691148863Ssam			"%s: delete key map entry %p<%s> refcnt %d\n",
1692148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr),
1693148863Ssam			ieee80211_node_refcnt(ni)-1);
1694148863Ssam		ieee80211_free_node(ni);
1695148863Ssam	}
1696148863Ssam	return status;
1697148863Ssam}
1698148863Ssam
1699148863Ssam/*
1700138568Ssam * Reclaim a node.  If this is the last reference count then
1701138568Ssam * do the normal free work.  Otherwise remove it from the node
1702138568Ssam * table and mark it gone by clearing the back-reference.
1703138568Ssam */
1704138568Ssamstatic void
1705138568Ssamnode_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1706116742Ssam{
1707148863Ssam	ieee80211_keyix keyix;
1708138568Ssam
1709148863Ssam	IEEE80211_NODE_LOCK_ASSERT(nt);
1710148863Ssam
1711140766Ssam	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1712140766Ssam		"%s: remove %p<%s> from %s table, refcnt %d\n",
1713140766Ssam		__func__, ni, ether_sprintf(ni->ni_macaddr),
1714140766Ssam		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1715148863Ssam	/*
1716148863Ssam	 * Clear any entry in the unicast key mapping table.
1717148863Ssam	 * We need to do it here so rx lookups don't find it
1718148863Ssam	 * in the mapping table even if it's not in the hash
1719148863Ssam	 * table.  We cannot depend on the mapping table entry
1720148863Ssam	 * being cleared because the node may not be free'd.
1721148863Ssam	 */
1722148863Ssam	keyix = ni->ni_ucastkey.wk_rxkeyix;
1723148863Ssam	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1724148863Ssam	    nt->nt_keyixmap[keyix] == ni) {
1725148863Ssam		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1726148863Ssam			"%s: %p<%s> clear key map entry\n",
1727148863Ssam			__func__, ni, ether_sprintf(ni->ni_macaddr));
1728148863Ssam		nt->nt_keyixmap[keyix] = NULL;
1729148863Ssam		ieee80211_node_decref(ni);	/* NB: don't need free */
1730148863Ssam	}
1731138568Ssam	if (!ieee80211_node_dectestref(ni)) {
1732138568Ssam		/*
1733138568Ssam		 * Other references are present, just remove the
1734138568Ssam		 * node from the table so it cannot be found.  When
1735138568Ssam		 * the references are dropped storage will be
1736140753Ssam		 * reclaimed.
1737138568Ssam		 */
1738138568Ssam		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1739138568Ssam		LIST_REMOVE(ni, ni_hash);
1740138568Ssam		ni->ni_table = NULL;		/* clear reference */
1741138568Ssam	} else
1742138568Ssam		_ieee80211_free_node(ni);
1743138568Ssam}
1744138568Ssam
1745138568Ssamstatic void
1746138568Ssamieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1747138568Ssam{
1748138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1749116742Ssam	struct ieee80211_node *ni;
1750116742Ssam
1751138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1752138568Ssam		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1753138568Ssam
1754138568Ssam	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1755138568Ssam		if (ni->ni_associd != 0) {
1756138568Ssam			if (ic->ic_auth->ia_node_leave != NULL)
1757138568Ssam				ic->ic_auth->ia_node_leave(ic, ni);
1758138568Ssam			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1759138568Ssam		}
1760138568Ssam		node_reclaim(nt, ni);
1761138568Ssam	}
1762138568Ssam	ieee80211_reset_erp(ic);
1763116742Ssam}
1764116742Ssam
1765138568Ssamstatic void
1766138568Ssamieee80211_free_allnodes(struct ieee80211_node_table *nt)
1767138568Ssam{
1768138568Ssam
1769138568Ssam	IEEE80211_NODE_LOCK(nt);
1770138568Ssam	ieee80211_free_allnodes_locked(nt);
1771138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1772138568Ssam}
1773138568Ssam
1774120483Ssam/*
1775138568Ssam * Timeout entries in the scan cache.
1776138568Ssam */
1777138568Ssamstatic void
1778138568Ssamieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1779138568Ssam{
1780138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1781138568Ssam	struct ieee80211_node *ni, *tni;
1782138568Ssam
1783138568Ssam	IEEE80211_NODE_LOCK(nt);
1784138568Ssam	ni = ic->ic_bss;
1785138568Ssam	/* XXX belongs elsewhere */
1786138568Ssam	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1787138568Ssam		m_freem(ni->ni_rxfrag[0]);
1788138568Ssam		ni->ni_rxfrag[0] = NULL;
1789138568Ssam	}
1790138568Ssam	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1791138568Ssam		if (ni->ni_inact && --ni->ni_inact == 0) {
1792138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1793138568Ssam			    "[%s] scan candidate purged from cache "
1794138568Ssam			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1795140766Ssam			    ieee80211_node_refcnt(ni));
1796138568Ssam			node_reclaim(nt, ni);
1797138568Ssam		}
1798138568Ssam	}
1799138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1800138568Ssam
1801138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1802138568Ssam}
1803138568Ssam
1804138568Ssam/*
1805138568Ssam * Timeout inactive stations and do related housekeeping.
1806138568Ssam * Note that we cannot hold the node lock while sending a
1807138568Ssam * frame as this would lead to a LOR.  Instead we use a
1808138568Ssam * generation number to mark nodes that we've scanned and
1809138568Ssam * drop the lock and restart a scan if we have to time out
1810138568Ssam * a node.  Since we are single-threaded by virtue of
1811120483Ssam * controlling the inactivity timer we can be sure this will
1812120483Ssam * process each node only once.
1813120483Ssam */
1814138568Ssamstatic void
1815138568Ssamieee80211_timeout_stations(struct ieee80211_node_table *nt)
1816116742Ssam{
1817138568Ssam	struct ieee80211com *ic = nt->nt_ic;
1818120483Ssam	struct ieee80211_node *ni;
1819138568Ssam	u_int gen;
1820148320Ssam	int isadhoc;
1821116742Ssam
1822148320Ssam	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1823148320Ssam		   ic->ic_opmode == IEEE80211_M_AHDEMO);
1824138568Ssam	IEEE80211_SCAN_LOCK(nt);
1825154532Ssam	gen = ++nt->nt_scangen;
1826138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1827140766Ssam		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
1828120483Ssamrestart:
1829138568Ssam	IEEE80211_NODE_LOCK(nt);
1830138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1831120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
1832120483Ssam			continue;
1833120483Ssam		ni->ni_scangen = gen;
1834138568Ssam		/*
1835147788Ssam		 * Ignore entries for which have yet to receive an
1836147788Ssam		 * authentication frame.  These are transient and
1837147788Ssam		 * will be reclaimed when the last reference to them
1838147788Ssam		 * goes away (when frame xmits complete).
1839147788Ssam		 */
1840148323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1841148323Ssam		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1842147788Ssam			continue;
1843147788Ssam		/*
1844138568Ssam		 * Free fragment if not needed anymore
1845138568Ssam		 * (last fragment older than 1s).
1846138568Ssam		 * XXX doesn't belong here
1847138568Ssam		 */
1848138568Ssam		if (ni->ni_rxfrag[0] != NULL &&
1849138568Ssam		    ticks > ni->ni_rxfragstamp + hz) {
1850138568Ssam			m_freem(ni->ni_rxfrag[0]);
1851138568Ssam			ni->ni_rxfrag[0] = NULL;
1852138568Ssam		}
1853140498Ssam		/*
1854140498Ssam		 * Special case ourself; we may be idle for extended periods
1855140498Ssam		 * of time and regardless reclaiming our state is wrong.
1856140498Ssam		 */
1857140498Ssam		if (ni == ic->ic_bss)
1858140498Ssam			continue;
1859138568Ssam		ni->ni_inact--;
1860148320Ssam		if (ni->ni_associd != 0 || isadhoc) {
1861119150Ssam			/*
1862138568Ssam			 * Age frames on the power save queue. The
1863138568Ssam			 * aging interval is 4 times the listen
1864138568Ssam			 * interval specified by the station.  This
1865138568Ssam			 * number is factored into the age calculations
1866138568Ssam			 * when the frame is placed on the queue.  We
1867138568Ssam			 * store ages as time differences we can check
1868138568Ssam			 * and/or adjust only the head of the list.
1869138568Ssam			 */
1870138568Ssam			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1871138568Ssam				struct mbuf *m;
1872138568Ssam				int discard = 0;
1873138568Ssam
1874138568Ssam				IEEE80211_NODE_SAVEQ_LOCK(ni);
1875138568Ssam				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1876138568Ssam				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1877138568SsamIEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1878138568Ssam					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1879138568Ssam					m_freem(m);
1880138568Ssam					discard++;
1881138568Ssam				}
1882138568Ssam				if (m != NULL)
1883138568Ssam					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1884138568Ssam				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1885138568Ssam
1886138568Ssam				if (discard != 0) {
1887138568Ssam					IEEE80211_DPRINTF(ic,
1888138568Ssam					    IEEE80211_MSG_POWER,
1889138568Ssam					    "[%s] discard %u frames for age\n",
1890138568Ssam					    ether_sprintf(ni->ni_macaddr),
1891138568Ssam					    discard);
1892138568Ssam					IEEE80211_NODE_STAT_ADD(ni,
1893138568Ssam						ps_discard, discard);
1894138568Ssam					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1895148304Ssam						ic->ic_set_tim(ni, 0);
1896138568Ssam				}
1897138568Ssam			}
1898138568Ssam			/*
1899138568Ssam			 * Probe the station before time it out.  We
1900138568Ssam			 * send a null data frame which may not be
1901138568Ssam			 * universally supported by drivers (need it
1902138568Ssam			 * for ps-poll support so it should be...).
1903138568Ssam			 */
1904139528Ssam			if (0 < ni->ni_inact &&
1905139528Ssam			    ni->ni_inact <= ic->ic_inact_probe) {
1906148320Ssam				IEEE80211_NOTE(ic,
1907148320Ssam				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1908148320Ssam				    ni, "%s",
1909148320Ssam				    "probe station due to inactivity");
1910148582Ssam				/*
1911148582Ssam				 * Grab a reference before unlocking the table
1912148582Ssam				 * so the node cannot be reclaimed before we
1913148582Ssam				 * send the frame. ieee80211_send_nulldata
1914148582Ssam				 * understands we've done this and reclaims the
1915148582Ssam				 * ref for us as needed.
1916148582Ssam				 */
1917148582Ssam				ieee80211_ref_node(ni);
1918138568Ssam				IEEE80211_NODE_UNLOCK(nt);
1919148301Ssam				ieee80211_send_nulldata(ni);
1920138568Ssam				/* XXX stat? */
1921138568Ssam				goto restart;
1922138568Ssam			}
1923138568Ssam		}
1924138568Ssam		if (ni->ni_inact <= 0) {
1925148320Ssam			IEEE80211_NOTE(ic,
1926148320Ssam			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1927148320Ssam			    "station timed out due to inactivity "
1928148320Ssam			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1929138568Ssam			/*
1930138568Ssam			 * Send a deauthenticate frame and drop the station.
1931138568Ssam			 * This is somewhat complicated due to reference counts
1932138568Ssam			 * and locking.  At this point a station will typically
1933138568Ssam			 * have a reference count of 1.  ieee80211_node_leave
1934138568Ssam			 * will do a "free" of the node which will drop the
1935138568Ssam			 * reference count.  But in the meantime a reference
1936138568Ssam			 * wil be held by the deauth frame.  The actual reclaim
1937138568Ssam			 * of the node will happen either after the tx is
1938138568Ssam			 * completed or by ieee80211_node_leave.
1939120483Ssam			 *
1940138568Ssam			 * Separately we must drop the node lock before sending
1941138568Ssam			 * in case the driver takes a lock, as this will result
1942138568Ssam			 * in  LOR between the node lock and the driver lock.
1943119150Ssam			 */
1944138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1945138568Ssam			if (ni->ni_associd != 0) {
1946138568Ssam				IEEE80211_SEND_MGMT(ic, ni,
1947138568Ssam				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1948138568Ssam				    IEEE80211_REASON_AUTH_EXPIRE);
1949138568Ssam			}
1950138568Ssam			ieee80211_node_leave(ic, ni);
1951121180Ssam			ic->ic_stats.is_node_timeout++;
1952120483Ssam			goto restart;
1953120483Ssam		}
1954116742Ssam	}
1955138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1956138568Ssam
1957138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1958138568Ssam
1959138568Ssam	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1960116742Ssam}
1961116742Ssam
1962116742Ssamvoid
1963138568Ssamieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1964116742Ssam{
1965116742Ssam	struct ieee80211_node *ni;
1966138568Ssam	u_int gen;
1967116742Ssam
1968138568Ssam	IEEE80211_SCAN_LOCK(nt);
1969154532Ssam	gen = ++nt->nt_scangen;
1970138568Ssamrestart:
1971138568Ssam	IEEE80211_NODE_LOCK(nt);
1972138568Ssam	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1973138568Ssam		if (ni->ni_scangen != gen) {
1974138568Ssam			ni->ni_scangen = gen;
1975138568Ssam			(void) ieee80211_ref_node(ni);
1976138568Ssam			IEEE80211_NODE_UNLOCK(nt);
1977138568Ssam			(*f)(arg, ni);
1978138568Ssam			ieee80211_free_node(ni);
1979138568Ssam			goto restart;
1980138568Ssam		}
1981138568Ssam	}
1982138568Ssam	IEEE80211_NODE_UNLOCK(nt);
1983138568Ssam
1984138568Ssam	IEEE80211_SCAN_UNLOCK(nt);
1985116742Ssam}
1986138568Ssam
1987138568Ssamvoid
1988138568Ssamieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1989138568Ssam{
1990138568Ssam	printf("0x%p: mac %s refcnt %d\n", ni,
1991138568Ssam		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1992138568Ssam	printf("\tscangen %u authmode %u flags 0x%x\n",
1993138568Ssam		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1994138568Ssam	printf("\tassocid 0x%x txpower %u vlan %u\n",
1995138568Ssam		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1996138568Ssam	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1997138568Ssam		ni->ni_txseqs[0],
1998138568Ssam		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1999138568Ssam		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
2000138568Ssam		ni->ni_rxfragstamp);
2001138568Ssam	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
2002138568Ssam		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
2003138568Ssam	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
2004138568Ssam		ether_sprintf(ni->ni_bssid),
2005138568Ssam		ni->ni_esslen, ni->ni_essid,
2006138568Ssam		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
2007138568Ssam	printf("\tfails %u inact %u txrate %u\n",
2008138568Ssam		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
2009138568Ssam}
2010138568Ssam
2011138568Ssamvoid
2012138568Ssamieee80211_dump_nodes(struct ieee80211_node_table *nt)
2013138568Ssam{
2014138568Ssam	ieee80211_iterate_nodes(nt,
2015138568Ssam		(ieee80211_iter_func *) ieee80211_dump_node, nt);
2016138568Ssam}
2017138568Ssam
2018138568Ssam/*
2019138568Ssam * Handle a station joining an 11g network.
2020138568Ssam */
2021138568Ssamstatic void
2022138568Ssamieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2023138568Ssam{
2024138568Ssam
2025138568Ssam	/*
2026138568Ssam	 * Station isn't capable of short slot time.  Bump
2027138568Ssam	 * the count of long slot time stations and disable
2028138568Ssam	 * use of short slot time.  Note that the actual switch
2029138568Ssam	 * over to long slot time use may not occur until the
2030138568Ssam	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
2031138568Ssam	 */
2032138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2033138568Ssam		ic->ic_longslotsta++;
2034138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2035138568Ssam		    "[%s] station needs long slot time, count %d\n",
2036138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
2037138568Ssam		/* XXX vap's w/ conflicting needs won't work */
2038138568Ssam		ieee80211_set_shortslottime(ic, 0);
2039138568Ssam	}
2040138568Ssam	/*
2041138568Ssam	 * If the new station is not an ERP station
2042138568Ssam	 * then bump the counter and enable protection
2043138568Ssam	 * if configured.
2044138568Ssam	 */
2045138568Ssam	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
2046138568Ssam		ic->ic_nonerpsta++;
2047138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2048138568Ssam		    "[%s] station is !ERP, %d non-ERP stations associated\n",
2049138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
2050138568Ssam		/*
2051138568Ssam		 * If protection is configured, enable it.
2052138568Ssam		 */
2053138568Ssam		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
2054138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2055138568Ssam			    "%s: enable use of protection\n", __func__);
2056138568Ssam			ic->ic_flags |= IEEE80211_F_USEPROT;
2057138568Ssam		}
2058138568Ssam		/*
2059138568Ssam		 * If station does not support short preamble
2060138568Ssam		 * then we must enable use of Barker preamble.
2061138568Ssam		 */
2062138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
2063138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2064138568Ssam			    "[%s] station needs long preamble\n",
2065138568Ssam			    ether_sprintf(ni->ni_macaddr));
2066138568Ssam			ic->ic_flags |= IEEE80211_F_USEBARKER;
2067138568Ssam			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2068138568Ssam		}
2069153973Ssam		if (ic->ic_nonerpsta == 1)
2070153973Ssam			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
2071138568Ssam	} else
2072138568Ssam		ni->ni_flags |= IEEE80211_NODE_ERP;
2073138568Ssam}
2074138568Ssam
2075138568Ssamvoid
2076138568Ssamieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
2077138568Ssam{
2078138568Ssam	int newassoc;
2079138568Ssam
2080138568Ssam	if (ni->ni_associd == 0) {
2081138568Ssam		u_int16_t aid;
2082138568Ssam
2083138568Ssam		/*
2084138568Ssam		 * It would be good to search the bitmap
2085138568Ssam		 * more efficiently, but this will do for now.
2086138568Ssam		 */
2087138568Ssam		for (aid = 1; aid < ic->ic_max_aid; aid++) {
2088138568Ssam			if (!IEEE80211_AID_ISSET(aid,
2089138568Ssam			    ic->ic_aid_bitmap))
2090138568Ssam				break;
2091138568Ssam		}
2092138568Ssam		if (aid >= ic->ic_max_aid) {
2093138568Ssam			IEEE80211_SEND_MGMT(ic, ni, resp,
2094138568Ssam			    IEEE80211_REASON_ASSOC_TOOMANY);
2095138568Ssam			ieee80211_node_leave(ic, ni);
2096138568Ssam			return;
2097138568Ssam		}
2098138568Ssam		ni->ni_associd = aid | 0xc000;
2099138568Ssam		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
2100138568Ssam		ic->ic_sta_assoc++;
2101138568Ssam		newassoc = 1;
2102149031Ssam		if (ic->ic_curmode == IEEE80211_MODE_11G)
2103138568Ssam			ieee80211_node_join_11g(ic, ni);
2104138568Ssam	} else
2105138568Ssam		newassoc = 0;
2106138568Ssam
2107138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2108139523Ssam	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
2109139523Ssam	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
2110139523Ssam	    IEEE80211_NODE_AID(ni),
2111139523Ssam	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
2112139523Ssam	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
2113139523Ssam	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
2114139523Ssam	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
2115139523Ssam	);
2116138568Ssam
2117138568Ssam	/* give driver a chance to setup state like ni_txrate */
2118139524Ssam	if (ic->ic_newassoc != NULL)
2119148307Ssam		ic->ic_newassoc(ni, newassoc);
2120139528Ssam	ni->ni_inact_reload = ic->ic_inact_auth;
2121139528Ssam	ni->ni_inact = ni->ni_inact_reload;
2122138568Ssam	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
2123138568Ssam	/* tell the authenticator about new station */
2124138568Ssam	if (ic->ic_auth->ia_node_join != NULL)
2125138568Ssam		ic->ic_auth->ia_node_join(ic, ni);
2126138568Ssam	ieee80211_notify_node_join(ic, ni, newassoc);
2127138568Ssam}
2128138568Ssam
2129138568Ssam/*
2130138568Ssam * Handle a station leaving an 11g network.
2131138568Ssam */
2132138568Ssamstatic void
2133138568Ssamieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
2134138568Ssam{
2135138568Ssam
2136149031Ssam	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
2137148941Ssam	     ("not in 11g, bss %u:0x%x, curmode %u", ni->ni_chan->ic_freq,
2138148941Ssam	      ni->ni_chan->ic_flags, ic->ic_curmode));
2139138568Ssam
2140138568Ssam	/*
2141138568Ssam	 * If a long slot station do the slot time bookkeeping.
2142138568Ssam	 */
2143138568Ssam	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
2144138568Ssam		KASSERT(ic->ic_longslotsta > 0,
2145138568Ssam		    ("bogus long slot station count %d", ic->ic_longslotsta));
2146138568Ssam		ic->ic_longslotsta--;
2147138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2148138568Ssam		    "[%s] long slot time station leaves, count now %d\n",
2149138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
2150138568Ssam		if (ic->ic_longslotsta == 0) {
2151138568Ssam			/*
2152138568Ssam			 * Re-enable use of short slot time if supported
2153138568Ssam			 * and not operating in IBSS mode (per spec).
2154138568Ssam			 */
2155138568Ssam			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
2156138568Ssam			    ic->ic_opmode != IEEE80211_M_IBSS) {
2157138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2158138568Ssam				    "%s: re-enable use of short slot time\n",
2159138568Ssam				    __func__);
2160138568Ssam				ieee80211_set_shortslottime(ic, 1);
2161138568Ssam			}
2162138568Ssam		}
2163138568Ssam	}
2164138568Ssam	/*
2165138568Ssam	 * If a non-ERP station do the protection-related bookkeeping.
2166138568Ssam	 */
2167138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
2168138568Ssam		KASSERT(ic->ic_nonerpsta > 0,
2169138568Ssam		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
2170138568Ssam		ic->ic_nonerpsta--;
2171138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2172138568Ssam		    "[%s] non-ERP station leaves, count now %d\n",
2173138568Ssam		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
2174138568Ssam		if (ic->ic_nonerpsta == 0) {
2175138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2176138568Ssam				"%s: disable use of protection\n", __func__);
2177138568Ssam			ic->ic_flags &= ~IEEE80211_F_USEPROT;
2178138568Ssam			/* XXX verify mode? */
2179138568Ssam			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
2180138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2181138568Ssam				    "%s: re-enable use of short preamble\n",
2182138568Ssam				    __func__);
2183138568Ssam				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2184138568Ssam				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2185138568Ssam			}
2186153973Ssam			ic->ic_flags_ext |= IEEE80211_FEXT_ERPUPDATE;
2187138568Ssam		}
2188138568Ssam	}
2189138568Ssam}
2190138568Ssam
2191138568Ssam/*
2192138568Ssam * Handle bookkeeping for station deauthentication/disassociation
2193138568Ssam * when operating as an ap.
2194138568Ssam */
2195138568Ssamvoid
2196138568Ssamieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
2197138568Ssam{
2198140499Ssam	struct ieee80211_node_table *nt = ni->ni_table;
2199138568Ssam
2200138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
2201138568Ssam	    "[%s] station with aid %d leaves\n",
2202138568Ssam	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
2203138568Ssam
2204138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2205138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS ||
2206138568Ssam		ic->ic_opmode == IEEE80211_M_AHDEMO,
2207138568Ssam		("unexpected operating mode %u", ic->ic_opmode));
2208138568Ssam	/*
2209138568Ssam	 * If node wasn't previously associated all
2210138568Ssam	 * we need to do is reclaim the reference.
2211138568Ssam	 */
2212138568Ssam	/* XXX ibss mode bypasses 11g and notification */
2213138568Ssam	if (ni->ni_associd == 0)
2214138568Ssam		goto done;
2215138568Ssam	/*
2216138568Ssam	 * Tell the authenticator the station is leaving.
2217138568Ssam	 * Note that we must do this before yanking the
2218138568Ssam	 * association id as the authenticator uses the
2219138568Ssam	 * associd to locate it's state block.
2220138568Ssam	 */
2221138568Ssam	if (ic->ic_auth->ia_node_leave != NULL)
2222138568Ssam		ic->ic_auth->ia_node_leave(ic, ni);
2223138568Ssam	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
2224138568Ssam	ni->ni_associd = 0;
2225138568Ssam	ic->ic_sta_assoc--;
2226138568Ssam
2227149031Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G)
2228138568Ssam		ieee80211_node_leave_11g(ic, ni);
2229138568Ssam	/*
2230138568Ssam	 * Cleanup station state.  In particular clear various
2231138568Ssam	 * state that might otherwise be reused if the node
2232138568Ssam	 * is reused before the reference count goes to zero
2233138568Ssam	 * (and memory is reclaimed).
2234138568Ssam	 */
2235138568Ssam	ieee80211_sta_leave(ic, ni);
2236138568Ssamdone:
2237140499Ssam	/*
2238140499Ssam	 * Remove the node from any table it's recorded in and
2239140499Ssam	 * drop the caller's reference.  Removal from the table
2240140499Ssam	 * is important to insure the node is not reprocessed
2241140499Ssam	 * for inactivity.
2242140499Ssam	 */
2243140499Ssam	if (nt != NULL) {
2244140499Ssam		IEEE80211_NODE_LOCK(nt);
2245140499Ssam		node_reclaim(nt, ni);
2246140499Ssam		IEEE80211_NODE_UNLOCK(nt);
2247140499Ssam	} else
2248140499Ssam		ieee80211_free_node(ni);
2249138568Ssam}
2250138568Ssam
2251138568Ssamu_int8_t
2252138568Ssamieee80211_getrssi(struct ieee80211com *ic)
2253138568Ssam{
2254138568Ssam#define	NZ(x)	((x) == 0 ? 1 : (x))
2255140753Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
2256138568Ssam	u_int32_t rssi_samples, rssi_total;
2257138568Ssam	struct ieee80211_node *ni;
2258138568Ssam
2259138568Ssam	rssi_total = 0;
2260138568Ssam	rssi_samples = 0;
2261138568Ssam	switch (ic->ic_opmode) {
2262138568Ssam	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
2263138568Ssam		/* XXX locking */
2264140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2265138568Ssam			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
2266138568Ssam				rssi_samples++;
2267138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
2268138568Ssam			}
2269138568Ssam		break;
2270138568Ssam	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
2271138568Ssam		/* XXX locking */
2272140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
2273138568Ssam			rssi_samples++;
2274138568Ssam			rssi_total += ic->ic_node_getrssi(ni);
2275138568Ssam		}
2276138568Ssam		break;
2277138568Ssam	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
2278138568Ssam		/* XXX locking */
2279140753Ssam		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
2280138568Ssam			if (IEEE80211_AID(ni->ni_associd) != 0) {
2281138568Ssam				rssi_samples++;
2282138568Ssam				rssi_total += ic->ic_node_getrssi(ni);
2283138568Ssam			}
2284138568Ssam		break;
2285138568Ssam	case IEEE80211_M_MONITOR:	/* XXX */
2286138568Ssam	case IEEE80211_M_STA:		/* use stats from associated ap */
2287138568Ssam	default:
2288138568Ssam		if (ic->ic_bss != NULL)
2289138568Ssam			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
2290138568Ssam		rssi_samples = 1;
2291138568Ssam		break;
2292138568Ssam	}
2293138568Ssam	return rssi_total / NZ(rssi_samples);
2294138568Ssam#undef NZ
2295138568Ssam}
2296138568Ssam
2297138568Ssam/*
2298138568Ssam * Indicate whether there are frames queued for a station in power-save mode.
2299138568Ssam */
2300138568Ssamstatic void
2301148304Ssamieee80211_set_tim(struct ieee80211_node *ni, int set)
2302138568Ssam{
2303148304Ssam	struct ieee80211com *ic = ni->ni_ic;
2304138568Ssam	u_int16_t aid;
2305138568Ssam
2306138568Ssam	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
2307138568Ssam		ic->ic_opmode == IEEE80211_M_IBSS,
2308138568Ssam		("operating mode %u", ic->ic_opmode));
2309138568Ssam
2310138568Ssam	aid = IEEE80211_AID(ni->ni_associd);
2311138568Ssam	KASSERT(aid < ic->ic_max_aid,
2312138568Ssam		("bogus aid %u, max %u", aid, ic->ic_max_aid));
2313138568Ssam
2314138568Ssam	IEEE80211_BEACON_LOCK(ic);
2315138568Ssam	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
2316138568Ssam		if (set) {
2317138568Ssam			setbit(ic->ic_tim_bitmap, aid);
2318138568Ssam			ic->ic_ps_pending++;
2319138568Ssam		} else {
2320138568Ssam			clrbit(ic->ic_tim_bitmap, aid);
2321138568Ssam			ic->ic_ps_pending--;
2322138568Ssam		}
2323138568Ssam		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
2324138568Ssam	}
2325138568Ssam	IEEE80211_BEACON_UNLOCK(ic);
2326138568Ssam}
2327138568Ssam
2328138568Ssam/*
2329138568Ssam * Node table support.
2330138568Ssam */
2331138568Ssam
2332138568Ssamstatic void
2333138568Ssamieee80211_node_table_init(struct ieee80211com *ic,
2334138568Ssam	struct ieee80211_node_table *nt,
2335148863Ssam	const char *name, int inact, int keyixmax,
2336138568Ssam	void (*timeout)(struct ieee80211_node_table *))
2337138568Ssam{
2338138568Ssam
2339138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
2340138568Ssam		"%s %s table, inact %u\n", __func__, name, inact);
2341138568Ssam
2342138568Ssam	nt->nt_ic = ic;
2343138568Ssam	/* XXX need unit */
2344138568Ssam	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2345138568Ssam	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
2346138568Ssam	TAILQ_INIT(&nt->nt_node);
2347138568Ssam	nt->nt_name = name;
2348138568Ssam	nt->nt_scangen = 1;
2349138568Ssam	nt->nt_inact_init = inact;
2350138568Ssam	nt->nt_timeout = timeout;
2351148863Ssam	nt->nt_keyixmax = keyixmax;
2352148863Ssam	if (nt->nt_keyixmax > 0) {
2353148863Ssam		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2354148863Ssam			keyixmax * sizeof(struct ieee80211_node *),
2355148863Ssam			M_80211_NODE, M_NOWAIT | M_ZERO);
2356148863Ssam		if (nt->nt_keyixmap == NULL)
2357148863Ssam			if_printf(ic->ic_ifp,
2358148863Ssam			    "Cannot allocate key index map with %u entries\n",
2359148863Ssam			    keyixmax);
2360148863Ssam	} else
2361148863Ssam		nt->nt_keyixmap = NULL;
2362138568Ssam}
2363138568Ssam
2364138568Ssamvoid
2365138568Ssamieee80211_node_table_reset(struct ieee80211_node_table *nt)
2366138568Ssam{
2367138568Ssam
2368138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2369138568Ssam		"%s %s table\n", __func__, nt->nt_name);
2370138568Ssam
2371138568Ssam	IEEE80211_NODE_LOCK(nt);
2372138568Ssam	nt->nt_inact_timer = 0;
2373138568Ssam	ieee80211_free_allnodes_locked(nt);
2374138568Ssam	IEEE80211_NODE_UNLOCK(nt);
2375138568Ssam}
2376138568Ssam
2377138568Ssamstatic void
2378138568Ssamieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2379138568Ssam{
2380138568Ssam
2381138568Ssam	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2382138568Ssam		"%s %s table\n", __func__, nt->nt_name);
2383138568Ssam
2384148863Ssam	IEEE80211_NODE_LOCK(nt);
2385138568Ssam	ieee80211_free_allnodes_locked(nt);
2386148863Ssam	if (nt->nt_keyixmap != NULL) {
2387148863Ssam		/* XXX verify all entries are NULL */
2388148863Ssam		int i;
2389148863Ssam		for (i = 0; i < nt->nt_keyixmax; i++)
2390148863Ssam			if (nt->nt_keyixmap[i] != NULL)
2391148863Ssam				printf("%s: %s[%u] still active\n", __func__,
2392148863Ssam					nt->nt_name, i);
2393148863Ssam		FREE(nt->nt_keyixmap, M_80211_NODE);
2394148863Ssam		nt->nt_keyixmap = NULL;
2395148863Ssam	}
2396138568Ssam	IEEE80211_SCAN_LOCK_DESTROY(nt);
2397138568Ssam	IEEE80211_NODE_LOCK_DESTROY(nt);
2398138568Ssam}
2399