ieee80211_node.c revision 119150
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3116742Ssam * Copyright (c) 2002, 2003 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 119150 2003-08-19 22:17:04Z sam $");
35116742Ssam
36116742Ssam#include "opt_inet.h"
37116742Ssam
38116742Ssam#include <sys/param.h>
39116742Ssam#include <sys/systm.h>
40116742Ssam#include <sys/mbuf.h>
41116742Ssam#include <sys/malloc.h>
42116742Ssam#include <sys/kernel.h>
43116742Ssam#include <sys/socket.h>
44116742Ssam#include <sys/sockio.h>
45116742Ssam#include <sys/endian.h>
46116742Ssam#include <sys/errno.h>
47116742Ssam#include <sys/bus.h>
48116742Ssam#include <sys/proc.h>
49116742Ssam#include <sys/sysctl.h>
50116742Ssam
51116742Ssam#include <machine/atomic.h>
52116742Ssam
53116742Ssam#include <net/if.h>
54116742Ssam#include <net/if_dl.h>
55116742Ssam#include <net/if_media.h>
56116742Ssam#include <net/if_arp.h>
57116742Ssam#include <net/ethernet.h>
58116742Ssam#include <net/if_llc.h>
59116742Ssam
60116742Ssam#include <net80211/ieee80211_var.h>
61116742Ssam
62116742Ssam#include <net/bpf.h>
63116742Ssam
64116742Ssam#ifdef INET
65116742Ssam#include <netinet/in.h>
66116742Ssam#include <netinet/if_ether.h>
67116742Ssam#endif
68116742Ssam
69116742Ssamstatic struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *);
70116742Ssamstatic void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *);
71116742Ssamstatic void ieee80211_node_copy(struct ieee80211com *,
72116742Ssam		struct ieee80211_node *, const struct ieee80211_node *);
73116742Ssamstatic void ieee80211_setup_node(struct ieee80211com *ic,
74116742Ssam		struct ieee80211_node *ni, u_int8_t *macaddr);
75116742Ssamstatic void _ieee80211_free_node(struct ieee80211com *,
76116742Ssam		struct ieee80211_node *);
77116742Ssam
78116742Ssamvoid
79116742Ssamieee80211_node_attach(struct ifnet *ifp)
80116742Ssam{
81116742Ssam	struct ieee80211com *ic = (void *)ifp;
82116742Ssam
83116742Ssam	/* XXX need unit */
84116742Ssam	mtx_init(&ic->ic_nodelock, ifp->if_name, "802.11 node table", MTX_DEF);
85116742Ssam	TAILQ_INIT(&ic->ic_node);
86116742Ssam	ic->ic_node_alloc = ieee80211_node_alloc;
87116742Ssam	ic->ic_node_free = ieee80211_node_free;
88116742Ssam	ic->ic_node_copy = ieee80211_node_copy;
89118887Ssam}
90118887Ssam
91118887Ssamvoid
92118887Ssamieee80211_node_lateattach(struct ifnet *ifp)
93118887Ssam{
94118887Ssam	struct ieee80211com *ic = (void *)ifp;
95118887Ssam
96116742Ssam	ic->ic_bss = (*ic->ic_node_alloc)(ic);
97117041Ssam	KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node"));
98117041Ssam	ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC;
99116742Ssam}
100116742Ssam
101116742Ssamvoid
102116742Ssamieee80211_node_detach(struct ifnet *ifp)
103116742Ssam{
104116742Ssam	struct ieee80211com *ic = (void *)ifp;
105116742Ssam
106116742Ssam	if (ic->ic_bss != NULL)
107116742Ssam		(*ic->ic_node_free)(ic, ic->ic_bss);
108116742Ssam	ieee80211_free_allnodes(ic);
109116742Ssam	mtx_destroy(&ic->ic_nodelock);
110116742Ssam}
111116742Ssam
112116742Ssam/*
113116742Ssam * AP scanning support.
114116742Ssam */
115116742Ssam
116116742Ssam/*
117116742Ssam * Initialize the active channel set based on the set
118116742Ssam * of available channels and the current PHY mode.
119116742Ssam */
120117811Ssamstatic void
121116742Ssamieee80211_reset_scan(struct ifnet *ifp)
122116742Ssam{
123116742Ssam	struct ieee80211com *ic = (void *)ifp;
124116742Ssam
125116742Ssam	memcpy(ic->ic_chan_scan, ic->ic_chan_active,
126116742Ssam		sizeof(ic->ic_chan_active));
127117811Ssam	/* NB: hack, setup so next_scan starts with the first channel */
128117811Ssam	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
129117811Ssam		ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
130116742Ssam}
131116742Ssam
132116742Ssam/*
133116742Ssam * Begin an active scan.
134116742Ssam */
135116742Ssamvoid
136117811Ssamieee80211_begin_scan(struct ifnet *ifp)
137116742Ssam{
138116742Ssam	struct ieee80211com *ic = (void *)ifp;
139116742Ssam
140117811Ssam	/*
141117811Ssam	 * In all but hostap mode scanning starts off in
142117811Ssam	 * an active mode before switching to passive.
143117811Ssam	 */
144117811Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP)
145117811Ssam		ic->ic_flags |= IEEE80211_F_ASCAN;
146116742Ssam	if (ifp->if_flags & IFF_DEBUG)
147116742Ssam		if_printf(ifp, "begin %s scan\n",
148117811Ssam			(ic->ic_flags & IEEE80211_F_ASCAN) ?
149116742Ssam				"active" : "passive");
150116742Ssam	/*
151117811Ssam	 * Clear scan state and flush any previously seen
152117811Ssam	 * AP's.  Note that the latter assumes we don't act
153117811Ssam	 * as both an AP and a station, otherwise we'll
154117811Ssam	 * potentially flush state of stations associated
155117811Ssam	 * with us.
156116742Ssam	 */
157117811Ssam	ieee80211_reset_scan(ifp);
158116742Ssam	ieee80211_free_allnodes(ic);
159116742Ssam
160117811Ssam	/* Scan the next channel. */
161117811Ssam	ieee80211_next_scan(ifp);
162116742Ssam}
163116742Ssam
164116742Ssam/*
165116742Ssam * Switch to the next channel marked for scanning.
166116742Ssam */
167116742Ssamvoid
168116742Ssamieee80211_next_scan(struct ifnet *ifp)
169116742Ssam{
170116742Ssam	struct ieee80211com *ic = (void *)ifp;
171116742Ssam	struct ieee80211_channel *chan;
172116742Ssam
173116742Ssam	chan = ic->ic_bss->ni_chan;
174116742Ssam	for (;;) {
175116742Ssam		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
176116742Ssam			chan = &ic->ic_channels[0];
177116742Ssam		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
178116742Ssam			/*
179116742Ssam			 * Honor channels marked passive-only
180116742Ssam			 * during an active scan.
181116742Ssam			 */
182116742Ssam			if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
183116742Ssam			    (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
184116742Ssam				break;
185116742Ssam		}
186116742Ssam		if (chan == ic->ic_bss->ni_chan) {
187116742Ssam			ieee80211_end_scan(ifp);
188116742Ssam			return;
189116742Ssam		}
190116742Ssam	}
191116742Ssam	clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
192116742Ssam	IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
193116742Ssam	    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
194116742Ssam	    ieee80211_chan2ieee(ic, chan)));
195116742Ssam	ic->ic_bss->ni_chan = chan;
196117811Ssam	ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
197116742Ssam}
198116742Ssam
199116742Ssamvoid
200116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
201116742Ssam{
202116742Ssam	struct ieee80211_node *ni;
203116742Ssam	struct ifnet *ifp = &ic->ic_if;
204116742Ssam
205116742Ssam	ni = ic->ic_bss;
206116742Ssam	if (ifp->if_flags & IFF_DEBUG)
207116742Ssam		if_printf(ifp, "creating ibss\n");
208116742Ssam	ic->ic_flags |= IEEE80211_F_SIBSS;
209116742Ssam	ni->ni_chan = chan;
210116742Ssam	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
211116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
212116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
213116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
214116742Ssam		ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
215116742Ssam	ni->ni_esslen = ic->ic_des_esslen;
216116742Ssam	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
217116742Ssam	ni->ni_rssi = 0;
218116742Ssam	ni->ni_rstamp = 0;
219116742Ssam	memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
220116742Ssam	ni->ni_intval = ic->ic_lintval;
221116742Ssam	ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
222116742Ssam	if (ic->ic_flags & IEEE80211_F_WEPON)
223116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
224116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
225116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
226116742Ssam		ni->ni_fhindex = 1;
227116742Ssam	}
228117811Ssam	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
229116742Ssam}
230116742Ssam
231116742Ssam/*
232116742Ssam * Complete a scan of potential channels.
233116742Ssam */
234116742Ssamvoid
235116742Ssamieee80211_end_scan(struct ifnet *ifp)
236116742Ssam{
237116742Ssam	struct ieee80211com *ic = (void *)ifp;
238116742Ssam	struct ieee80211_node *ni, *nextbs, *selbs;
239116742Ssam	u_int8_t rate;
240116742Ssam	int i, fail;
241116742Ssam
242116742Ssam	ic->ic_flags &= ~IEEE80211_F_ASCAN;
243116742Ssam	ni = TAILQ_FIRST(&ic->ic_node);
244116742Ssam
245116742Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
246116742Ssam		/* XXX off stack? */
247116742Ssam		u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
248116742Ssam		/*
249116742Ssam		 * The passive scan to look for existing AP's completed,
250116742Ssam		 * select a channel to camp on.  Identify the channels
251116742Ssam		 * that already have one or more AP's and try to locate
252116742Ssam		 * an unnoccupied one.  If that fails, pick a random
253116742Ssam		 * channel from the active set.
254116742Ssam		 */
255116742Ssam		for (; ni != NULL; ni = nextbs) {
256116742Ssam			ieee80211_ref_node(ni);
257116742Ssam			nextbs = TAILQ_NEXT(ni, ni_list);
258116742Ssam			setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
259116742Ssam			ieee80211_free_node(ic, ni);
260116742Ssam		}
261116742Ssam		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
262116742Ssam			if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
263116742Ssam				break;
264116742Ssam		if (i == IEEE80211_CHAN_MAX) {
265116742Ssam			fail = arc4random() & 3;	/* random 0-3 */
266116742Ssam			for (i = 0; i < IEEE80211_CHAN_MAX; i++)
267116742Ssam				if (isset(ic->ic_chan_active, i) && fail-- == 0)
268116742Ssam					break;
269116742Ssam		}
270116742Ssam		ieee80211_create_ibss(ic, &ic->ic_channels[i]);
271116742Ssam		return;
272116742Ssam	}
273116742Ssam	if (ni == NULL) {
274116742Ssam		IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
275116742Ssam  notfound:
276116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS &&
277116742Ssam		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
278116742Ssam		    ic->ic_des_esslen != 0) {
279116742Ssam			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
280116742Ssam			return;
281116742Ssam		}
282116742Ssam		/*
283116742Ssam		 * Reset the list of channels to scan and start again.
284116742Ssam		 */
285116742Ssam		ieee80211_reset_scan(ifp);
286116742Ssam		ieee80211_next_scan(ifp);
287116742Ssam		return;
288116742Ssam	}
289116742Ssam	selbs = NULL;
290116742Ssam	if (ifp->if_flags & IFF_DEBUG)
291119150Ssam		if_printf(ifp, "\tmacaddr          bssid         chan  rssi rate flag  wep  essid\n");
292116742Ssam	for (; ni != NULL; ni = nextbs) {
293116742Ssam		ieee80211_ref_node(ni);
294116742Ssam		nextbs = TAILQ_NEXT(ni, ni_list);
295116742Ssam		if (ni->ni_fails) {
296116742Ssam			/*
297116742Ssam			 * The configuration of the access points may change
298116742Ssam			 * during my scan.  So delete the entry for the AP
299116742Ssam			 * and retry to associate if there is another beacon.
300116742Ssam			 */
301116742Ssam			if (ni->ni_fails++ > 2)
302116742Ssam				ieee80211_free_node(ic, ni);
303116742Ssam			continue;
304116742Ssam		}
305116742Ssam		fail = 0;
306116742Ssam		if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
307116742Ssam			fail |= 0x01;
308116742Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
309116742Ssam		    ni->ni_chan != ic->ic_des_chan)
310116742Ssam			fail |= 0x01;
311116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS) {
312116742Ssam			if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
313116742Ssam				fail |= 0x02;
314116742Ssam		} else {
315116742Ssam			if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
316116742Ssam				fail |= 0x02;
317116742Ssam		}
318116742Ssam		if (ic->ic_flags & IEEE80211_F_WEPON) {
319116742Ssam			if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
320116742Ssam				fail |= 0x04;
321116742Ssam		} else {
322116742Ssam			if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
323116742Ssam				fail |= 0x04;
324116742Ssam		}
325116742Ssam		rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
326116742Ssam		if (rate & IEEE80211_RATE_BASIC)
327116742Ssam			fail |= 0x08;
328116742Ssam		if (ic->ic_des_esslen != 0 &&
329116742Ssam		    (ni->ni_esslen != ic->ic_des_esslen ||
330116742Ssam		     memcmp(ni->ni_essid, ic->ic_des_essid,
331116742Ssam		     ic->ic_des_esslen != 0)))
332116742Ssam			fail |= 0x10;
333116742Ssam		if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
334116742Ssam		    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
335116742Ssam			fail |= 0x20;
336116742Ssam		if (ifp->if_flags & IFF_DEBUG) {
337116742Ssam			printf(" %c %s", fail ? '-' : '+',
338116742Ssam			    ether_sprintf(ni->ni_macaddr));
339116742Ssam			printf(" %s%c", ether_sprintf(ni->ni_bssid),
340116742Ssam			    fail & 0x20 ? '!' : ' ');
341116742Ssam			printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
342116742Ssam				fail & 0x01 ? '!' : ' ');
343116742Ssam			printf(" %+4d", ni->ni_rssi);
344116742Ssam			printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
345116742Ssam			    fail & 0x08 ? '!' : ' ');
346116742Ssam			printf(" %4s%c",
347116742Ssam			    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
348116742Ssam			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
349116742Ssam			    "????",
350116742Ssam			    fail & 0x02 ? '!' : ' ');
351116742Ssam			printf(" %3s%c ",
352116742Ssam			    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
353116742Ssam			    "wep" : "no",
354116742Ssam			    fail & 0x04 ? '!' : ' ');
355116742Ssam			ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
356116742Ssam			printf("%s\n", fail & 0x10 ? "!" : "");
357116742Ssam		}
358116742Ssam		if (!fail) {
359116742Ssam			if (selbs == NULL)
360116742Ssam				selbs = ni;
361116742Ssam			else if (ni->ni_rssi > selbs->ni_rssi) {
362116742Ssam				ieee80211_unref_node(&selbs);
363116742Ssam				selbs = ni;
364116742Ssam			} else
365116742Ssam				ieee80211_unref_node(&ni);
366116742Ssam		} else {
367116742Ssam			ieee80211_unref_node(&ni);
368116742Ssam		}
369116742Ssam	}
370116742Ssam	if (selbs == NULL)
371116742Ssam		goto notfound;
372116742Ssam	(*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
373116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
374116742Ssam		ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
375116742Ssam		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
376116742Ssam		if (ic->ic_bss->ni_rates.rs_nrates == 0) {
377116742Ssam			selbs->ni_fails++;
378116742Ssam			ieee80211_unref_node(&selbs);
379116742Ssam			goto notfound;
380116742Ssam		}
381116742Ssam		ieee80211_unref_node(&selbs);
382117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
383116742Ssam	} else {
384116742Ssam		ieee80211_unref_node(&selbs);
385117811Ssam		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
386116742Ssam	}
387116742Ssam}
388116742Ssam
389116742Ssamstatic struct ieee80211_node *
390116742Ssamieee80211_node_alloc(struct ieee80211com *ic)
391116742Ssam{
392116742Ssam	return malloc(sizeof(struct ieee80211_node), M_DEVBUF,
393116742Ssam		M_NOWAIT | M_ZERO);
394116742Ssam}
395116742Ssam
396116742Ssamstatic void
397116742Ssamieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
398116742Ssam{
399116742Ssam	free(ni, M_DEVBUF);
400116742Ssam}
401116742Ssam
402116742Ssamstatic void
403116742Ssamieee80211_node_copy(struct ieee80211com *ic,
404116742Ssam	struct ieee80211_node *dst, const struct ieee80211_node *src)
405116742Ssam{
406116742Ssam	*dst = *src;
407116742Ssam}
408116742Ssam
409116742Ssamstatic void
410116742Ssamieee80211_setup_node(struct ieee80211com *ic,
411116742Ssam	struct ieee80211_node *ni, u_int8_t *macaddr)
412116742Ssam{
413116742Ssam	int hash;
414116742Ssam
415116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
416116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
417116742Ssam	ni->ni_refcnt = 1;		/* mark referenced */
418116742Ssam	mtx_lock(&ic->ic_nodelock);
419116742Ssam	TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
420116742Ssam	LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
421116742Ssam	/*
422116742Ssam	 * Note we don't enable the inactive timer when acting
423116742Ssam	 * as a station.  Nodes created in this mode represent
424116742Ssam	 * AP's identified while scanning.  If we time them out
425116742Ssam	 * then several things happen: we can't return the data
426116742Ssam	 * to users to show the list of AP's we encountered, and
427116742Ssam	 * more importantly, we'll incorrectly deauthenticate
428116742Ssam	 * ourself because the inactivity timer will kick us off.
429116742Ssam	 */
430116742Ssam	if (ic->ic_opmode != IEEE80211_M_STA)
431116742Ssam		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
432116742Ssam	mtx_unlock(&ic->ic_nodelock);
433116742Ssam}
434116742Ssam
435116742Ssamstruct ieee80211_node *
436116742Ssamieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
437116742Ssam{
438116742Ssam	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
439116742Ssam	if (ni != NULL)
440116742Ssam		ieee80211_setup_node(ic, ni, macaddr);
441116742Ssam	return ni;
442116742Ssam}
443116742Ssam
444116742Ssamstruct ieee80211_node *
445116742Ssamieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
446116742Ssam{
447116742Ssam	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
448116742Ssam	if (ni != NULL) {
449116742Ssam		memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node));
450116742Ssam		ieee80211_setup_node(ic, ni, macaddr);
451116742Ssam	}
452116742Ssam	return ni;
453116742Ssam}
454116742Ssam
455116742Ssamstruct ieee80211_node *
456116742Ssamieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
457116742Ssam{
458116742Ssam	struct ieee80211_node *ni;
459116742Ssam	int hash;
460116742Ssam
461116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
462116742Ssam	mtx_lock(&ic->ic_nodelock);
463116742Ssam	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
464116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
465116742Ssam			atomic_add_int(&ni->ni_refcnt, 1); /* mark referenced */
466116742Ssam			break;
467116742Ssam		}
468116742Ssam	}
469116742Ssam	mtx_unlock(&ic->ic_nodelock);
470116742Ssam	return ni;
471116742Ssam}
472116742Ssam
473116742Ssam/*
474116742Ssam * Like find but search based on the channel too.
475116742Ssam */
476116742Ssamstruct ieee80211_node *
477116742Ssamieee80211_lookup_node(struct ieee80211com *ic,
478116742Ssam	u_int8_t *macaddr, struct ieee80211_channel *chan)
479116742Ssam{
480116742Ssam	struct ieee80211_node *ni;
481116742Ssam	int hash;
482116742Ssam
483116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
484116742Ssam	mtx_lock(&ic->ic_nodelock);
485116742Ssam	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
486116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) {
487116742Ssam			atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */
488116742Ssam			break;
489116742Ssam		}
490116742Ssam	}
491116742Ssam	mtx_unlock(&ic->ic_nodelock);
492116742Ssam	return ni;
493116742Ssam}
494116742Ssam
495116742Ssamstatic void
496116742Ssam_ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
497116742Ssam{
498119150Ssam	KASSERT(ni != ic->ic_bss, ("freeing bss node"));
499119150Ssam
500116742Ssam	TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
501116742Ssam	LIST_REMOVE(ni, ni_hash);
502116742Ssam	if (TAILQ_EMPTY(&ic->ic_node))
503116742Ssam		ic->ic_inact_timer = 0;
504116742Ssam	(*ic->ic_node_free)(ic, ni);
505116742Ssam}
506116742Ssam
507116742Ssamvoid
508116742Ssamieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
509116742Ssam{
510119150Ssam	KASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
511119150Ssam
512116742Ssam	/* XXX need equivalent of atomic_dec_and_test */
513116742Ssam	atomic_subtract_int(&ni->ni_refcnt, 1);
514116742Ssam	if (atomic_cmpset_int(&ni->ni_refcnt, 0, 1)) {
515116742Ssam		mtx_lock(&ic->ic_nodelock);
516116742Ssam		_ieee80211_free_node(ic, ni);
517116742Ssam		mtx_unlock(&ic->ic_nodelock);
518116742Ssam	}
519116742Ssam}
520116742Ssam
521116742Ssamvoid
522116742Ssamieee80211_free_allnodes(struct ieee80211com *ic)
523116742Ssam{
524116742Ssam	struct ieee80211_node *ni;
525116742Ssam
526116742Ssam	mtx_lock(&ic->ic_nodelock);
527116742Ssam	while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
528116742Ssam		_ieee80211_free_node(ic, ni);
529116742Ssam	mtx_unlock(&ic->ic_nodelock);
530116742Ssam}
531116742Ssam
532116742Ssamvoid
533116742Ssamieee80211_timeout_nodes(struct ieee80211com *ic)
534116742Ssam{
535116742Ssam	struct ieee80211_node *ni, *nextbs;
536116742Ssam
537116742Ssam	mtx_lock(&ic->ic_nodelock);
538116742Ssam	for (ni = TAILQ_FIRST(&ic->ic_node); ni != NULL;) {
539119150Ssam		if (++ni->ni_inact > IEEE80211_INACT_MAX) {
540119150Ssam			IEEE80211_DPRINTF(("station %s timed out "
541116742Ssam			    "due to inactivity (%u secs)\n",
542116742Ssam			    ether_sprintf(ni->ni_macaddr),
543116742Ssam			    ni->ni_inact));
544119150Ssam			nextbs = TAILQ_NEXT(ni, ni_list);
545119150Ssam			/*
546119150Ssam			 * Send a deauthenticate frame.
547119150Ssam			 */
548119150Ssam			IEEE80211_SEND_MGMT(ic, ni,
549119150Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
550119150Ssam			    IEEE80211_REASON_AUTH_EXPIRE);
551119150Ssam			ieee80211_free_node(ic, ni);
552119150Ssam			ni = nextbs;
553119150Ssam		} else
554119150Ssam			ni = TAILQ_NEXT(ni, ni_list);
555116742Ssam	}
556116742Ssam	if (!TAILQ_EMPTY(&ic->ic_node))
557116742Ssam		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
558116742Ssam	mtx_unlock(&ic->ic_nodelock);
559116742Ssam}
560116742Ssam
561116742Ssamvoid
562116742Ssamieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
563116742Ssam{
564116742Ssam	struct ieee80211_node *ni;
565116742Ssam
566116742Ssam	mtx_lock(&ic->ic_nodelock);
567116742Ssam	TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
568116742Ssam		(*f)(arg, ni);
569116742Ssam	mtx_unlock(&ic->ic_nodelock);
570116742Ssam}
571