ieee80211_node.c revision 121180
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 121180 2003-10-17 23:15:30Z 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 *);
73120104Ssamstatic u_int8_t ieee80211_node_getrssi(struct ieee80211com *,
74120104Ssam		struct ieee80211_node *);
75120104Ssam
76116742Ssamstatic void ieee80211_setup_node(struct ieee80211com *ic,
77116742Ssam		struct ieee80211_node *ni, u_int8_t *macaddr);
78116742Ssamstatic void _ieee80211_free_node(struct ieee80211com *,
79116742Ssam		struct ieee80211_node *);
80116742Ssam
81120481SsamMALLOC_DEFINE(M_80211_NODE, "node", "802.11 node state");
82120481Ssam
83116742Ssamvoid
84116742Ssamieee80211_node_attach(struct ifnet *ifp)
85116742Ssam{
86116742Ssam	struct ieee80211com *ic = (void *)ifp;
87116742Ssam
88116742Ssam	/* XXX need unit */
89121172Ssam	IEEE80211_NODE_LOCK_INIT(ic, ifp->if_name);
90116742Ssam	TAILQ_INIT(&ic->ic_node);
91116742Ssam	ic->ic_node_alloc = ieee80211_node_alloc;
92116742Ssam	ic->ic_node_free = ieee80211_node_free;
93116742Ssam	ic->ic_node_copy = ieee80211_node_copy;
94120104Ssam	ic->ic_node_getrssi = ieee80211_node_getrssi;
95120483Ssam	ic->ic_scangen = 1;
96118887Ssam}
97118887Ssam
98118887Ssamvoid
99118887Ssamieee80211_node_lateattach(struct ifnet *ifp)
100118887Ssam{
101118887Ssam	struct ieee80211com *ic = (void *)ifp;
102118887Ssam
103116742Ssam	ic->ic_bss = (*ic->ic_node_alloc)(ic);
104117041Ssam	KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node"));
105117041Ssam	ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC;
106116742Ssam}
107116742Ssam
108116742Ssamvoid
109116742Ssamieee80211_node_detach(struct ifnet *ifp)
110116742Ssam{
111116742Ssam	struct ieee80211com *ic = (void *)ifp;
112116742Ssam
113116742Ssam	if (ic->ic_bss != NULL)
114116742Ssam		(*ic->ic_node_free)(ic, ic->ic_bss);
115116742Ssam	ieee80211_free_allnodes(ic);
116121172Ssam	IEEE80211_NODE_LOCK_DESTROY(ic);
117116742Ssam}
118116742Ssam
119116742Ssam/*
120116742Ssam * AP scanning support.
121116742Ssam */
122116742Ssam
123116742Ssam/*
124116742Ssam * Initialize the active channel set based on the set
125116742Ssam * of available channels and the current PHY mode.
126116742Ssam */
127117811Ssamstatic void
128116742Ssamieee80211_reset_scan(struct ifnet *ifp)
129116742Ssam{
130116742Ssam	struct ieee80211com *ic = (void *)ifp;
131116742Ssam
132116742Ssam	memcpy(ic->ic_chan_scan, ic->ic_chan_active,
133116742Ssam		sizeof(ic->ic_chan_active));
134117811Ssam	/* NB: hack, setup so next_scan starts with the first channel */
135117811Ssam	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
136117811Ssam		ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
137116742Ssam}
138116742Ssam
139116742Ssam/*
140116742Ssam * Begin an active scan.
141116742Ssam */
142116742Ssamvoid
143117811Ssamieee80211_begin_scan(struct ifnet *ifp)
144116742Ssam{
145116742Ssam	struct ieee80211com *ic = (void *)ifp;
146116742Ssam
147117811Ssam	/*
148117811Ssam	 * In all but hostap mode scanning starts off in
149117811Ssam	 * an active mode before switching to passive.
150117811Ssam	 */
151121180Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
152117811Ssam		ic->ic_flags |= IEEE80211_F_ASCAN;
153121180Ssam		ic->ic_stats.is_scan_active++;
154121180Ssam	} else
155121180Ssam		ic->ic_stats.is_scan_passive++;
156116742Ssam	if (ifp->if_flags & IFF_DEBUG)
157116742Ssam		if_printf(ifp, "begin %s scan\n",
158117811Ssam			(ic->ic_flags & IEEE80211_F_ASCAN) ?
159116742Ssam				"active" : "passive");
160116742Ssam	/*
161117811Ssam	 * Clear scan state and flush any previously seen
162117811Ssam	 * AP's.  Note that the latter assumes we don't act
163117811Ssam	 * as both an AP and a station, otherwise we'll
164117811Ssam	 * potentially flush state of stations associated
165117811Ssam	 * with us.
166116742Ssam	 */
167117811Ssam	ieee80211_reset_scan(ifp);
168116742Ssam	ieee80211_free_allnodes(ic);
169116742Ssam
170117811Ssam	/* Scan the next channel. */
171117811Ssam	ieee80211_next_scan(ifp);
172116742Ssam}
173116742Ssam
174116742Ssam/*
175116742Ssam * Switch to the next channel marked for scanning.
176116742Ssam */
177116742Ssamvoid
178116742Ssamieee80211_next_scan(struct ifnet *ifp)
179116742Ssam{
180116742Ssam	struct ieee80211com *ic = (void *)ifp;
181116742Ssam	struct ieee80211_channel *chan;
182116742Ssam
183116742Ssam	chan = ic->ic_bss->ni_chan;
184116742Ssam	for (;;) {
185116742Ssam		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
186116742Ssam			chan = &ic->ic_channels[0];
187116742Ssam		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
188116742Ssam			/*
189116742Ssam			 * Honor channels marked passive-only
190116742Ssam			 * during an active scan.
191116742Ssam			 */
192116742Ssam			if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
193116742Ssam			    (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
194116742Ssam				break;
195116742Ssam		}
196116742Ssam		if (chan == ic->ic_bss->ni_chan) {
197116742Ssam			ieee80211_end_scan(ifp);
198116742Ssam			return;
199116742Ssam		}
200116742Ssam	}
201116742Ssam	clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
202116742Ssam	IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
203116742Ssam	    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
204116742Ssam	    ieee80211_chan2ieee(ic, chan)));
205116742Ssam	ic->ic_bss->ni_chan = chan;
206117811Ssam	ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
207116742Ssam}
208116742Ssam
209116742Ssamvoid
210116742Ssamieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
211116742Ssam{
212116742Ssam	struct ieee80211_node *ni;
213116742Ssam	struct ifnet *ifp = &ic->ic_if;
214116742Ssam
215116742Ssam	ni = ic->ic_bss;
216116742Ssam	if (ifp->if_flags & IFF_DEBUG)
217116742Ssam		if_printf(ifp, "creating ibss\n");
218116742Ssam	ic->ic_flags |= IEEE80211_F_SIBSS;
219116742Ssam	ni->ni_chan = chan;
220116742Ssam	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
221116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
222116742Ssam	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
223116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
224116742Ssam		ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
225116742Ssam	ni->ni_esslen = ic->ic_des_esslen;
226116742Ssam	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
227116742Ssam	ni->ni_rssi = 0;
228116742Ssam	ni->ni_rstamp = 0;
229116742Ssam	memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
230116742Ssam	ni->ni_intval = ic->ic_lintval;
231116742Ssam	ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
232116742Ssam	if (ic->ic_flags & IEEE80211_F_WEPON)
233116742Ssam		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
234116742Ssam	if (ic->ic_phytype == IEEE80211_T_FH) {
235116742Ssam		ni->ni_fhdwell = 200;	/* XXX */
236116742Ssam		ni->ni_fhindex = 1;
237116742Ssam	}
238117811Ssam	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
239116742Ssam}
240116742Ssam
241116742Ssam/*
242116742Ssam * Complete a scan of potential channels.
243116742Ssam */
244116742Ssamvoid
245116742Ssamieee80211_end_scan(struct ifnet *ifp)
246116742Ssam{
247116742Ssam	struct ieee80211com *ic = (void *)ifp;
248116742Ssam	struct ieee80211_node *ni, *nextbs, *selbs;
249116742Ssam	u_int8_t rate;
250116742Ssam	int i, fail;
251116742Ssam
252116742Ssam	ic->ic_flags &= ~IEEE80211_F_ASCAN;
253116742Ssam	ni = TAILQ_FIRST(&ic->ic_node);
254116742Ssam
255116742Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
256116742Ssam		/* XXX off stack? */
257116742Ssam		u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
258116742Ssam		/*
259116742Ssam		 * The passive scan to look for existing AP's completed,
260116742Ssam		 * select a channel to camp on.  Identify the channels
261116742Ssam		 * that already have one or more AP's and try to locate
262116742Ssam		 * an unnoccupied one.  If that fails, pick a random
263116742Ssam		 * channel from the active set.
264116742Ssam		 */
265116742Ssam		for (; ni != NULL; ni = nextbs) {
266116742Ssam			ieee80211_ref_node(ni);
267116742Ssam			nextbs = TAILQ_NEXT(ni, ni_list);
268116742Ssam			setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
269116742Ssam			ieee80211_free_node(ic, ni);
270116742Ssam		}
271116742Ssam		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
272116742Ssam			if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
273116742Ssam				break;
274116742Ssam		if (i == IEEE80211_CHAN_MAX) {
275116742Ssam			fail = arc4random() & 3;	/* random 0-3 */
276116742Ssam			for (i = 0; i < IEEE80211_CHAN_MAX; i++)
277116742Ssam				if (isset(ic->ic_chan_active, i) && fail-- == 0)
278116742Ssam					break;
279116742Ssam		}
280116742Ssam		ieee80211_create_ibss(ic, &ic->ic_channels[i]);
281116742Ssam		return;
282116742Ssam	}
283116742Ssam	if (ni == NULL) {
284116742Ssam		IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
285116742Ssam  notfound:
286116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS &&
287116742Ssam		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
288116742Ssam		    ic->ic_des_esslen != 0) {
289116742Ssam			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
290116742Ssam			return;
291116742Ssam		}
292116742Ssam		/*
293116742Ssam		 * Reset the list of channels to scan and start again.
294116742Ssam		 */
295116742Ssam		ieee80211_reset_scan(ifp);
296116742Ssam		ieee80211_next_scan(ifp);
297116742Ssam		return;
298116742Ssam	}
299116742Ssam	selbs = NULL;
300116742Ssam	if (ifp->if_flags & IFF_DEBUG)
301119150Ssam		if_printf(ifp, "\tmacaddr          bssid         chan  rssi rate flag  wep  essid\n");
302116742Ssam	for (; ni != NULL; ni = nextbs) {
303116742Ssam		ieee80211_ref_node(ni);
304116742Ssam		nextbs = TAILQ_NEXT(ni, ni_list);
305116742Ssam		if (ni->ni_fails) {
306116742Ssam			/*
307116742Ssam			 * The configuration of the access points may change
308116742Ssam			 * during my scan.  So delete the entry for the AP
309116742Ssam			 * and retry to associate if there is another beacon.
310116742Ssam			 */
311116742Ssam			if (ni->ni_fails++ > 2)
312116742Ssam				ieee80211_free_node(ic, ni);
313116742Ssam			continue;
314116742Ssam		}
315116742Ssam		fail = 0;
316116742Ssam		if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
317116742Ssam			fail |= 0x01;
318116742Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
319116742Ssam		    ni->ni_chan != ic->ic_des_chan)
320116742Ssam			fail |= 0x01;
321116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS) {
322116742Ssam			if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
323116742Ssam				fail |= 0x02;
324116742Ssam		} else {
325116742Ssam			if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
326116742Ssam				fail |= 0x02;
327116742Ssam		}
328116742Ssam		if (ic->ic_flags & IEEE80211_F_WEPON) {
329116742Ssam			if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
330116742Ssam				fail |= 0x04;
331116742Ssam		} else {
332116742Ssam			if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
333116742Ssam				fail |= 0x04;
334116742Ssam		}
335116742Ssam		rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
336116742Ssam		if (rate & IEEE80211_RATE_BASIC)
337116742Ssam			fail |= 0x08;
338116742Ssam		if (ic->ic_des_esslen != 0 &&
339116742Ssam		    (ni->ni_esslen != ic->ic_des_esslen ||
340116742Ssam		     memcmp(ni->ni_essid, ic->ic_des_essid,
341116742Ssam		     ic->ic_des_esslen != 0)))
342116742Ssam			fail |= 0x10;
343116742Ssam		if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
344116742Ssam		    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
345116742Ssam			fail |= 0x20;
346116742Ssam		if (ifp->if_flags & IFF_DEBUG) {
347116742Ssam			printf(" %c %s", fail ? '-' : '+',
348116742Ssam			    ether_sprintf(ni->ni_macaddr));
349116742Ssam			printf(" %s%c", ether_sprintf(ni->ni_bssid),
350116742Ssam			    fail & 0x20 ? '!' : ' ');
351116742Ssam			printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
352116742Ssam				fail & 0x01 ? '!' : ' ');
353116742Ssam			printf(" %+4d", ni->ni_rssi);
354116742Ssam			printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
355116742Ssam			    fail & 0x08 ? '!' : ' ');
356116742Ssam			printf(" %4s%c",
357116742Ssam			    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
358116742Ssam			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
359116742Ssam			    "????",
360116742Ssam			    fail & 0x02 ? '!' : ' ');
361116742Ssam			printf(" %3s%c ",
362116742Ssam			    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
363116742Ssam			    "wep" : "no",
364116742Ssam			    fail & 0x04 ? '!' : ' ');
365116742Ssam			ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
366116742Ssam			printf("%s\n", fail & 0x10 ? "!" : "");
367116742Ssam		}
368116742Ssam		if (!fail) {
369116742Ssam			if (selbs == NULL)
370116742Ssam				selbs = ni;
371116742Ssam			else if (ni->ni_rssi > selbs->ni_rssi) {
372116742Ssam				ieee80211_unref_node(&selbs);
373116742Ssam				selbs = ni;
374116742Ssam			} else
375116742Ssam				ieee80211_unref_node(&ni);
376116742Ssam		} else {
377116742Ssam			ieee80211_unref_node(&ni);
378116742Ssam		}
379116742Ssam	}
380116742Ssam	if (selbs == NULL)
381116742Ssam		goto notfound;
382116742Ssam	(*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
383116742Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
384116742Ssam		ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
385116742Ssam		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
386116742Ssam		if (ic->ic_bss->ni_rates.rs_nrates == 0) {
387116742Ssam			selbs->ni_fails++;
388116742Ssam			ieee80211_unref_node(&selbs);
389116742Ssam			goto notfound;
390116742Ssam		}
391116742Ssam		ieee80211_unref_node(&selbs);
392117811Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
393116742Ssam	} else {
394116742Ssam		ieee80211_unref_node(&selbs);
395117811Ssam		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
396116742Ssam	}
397116742Ssam}
398116742Ssam
399116742Ssamstatic struct ieee80211_node *
400116742Ssamieee80211_node_alloc(struct ieee80211com *ic)
401116742Ssam{
402120481Ssam	return malloc(sizeof(struct ieee80211_node), M_80211_NODE,
403116742Ssam		M_NOWAIT | M_ZERO);
404116742Ssam}
405116742Ssam
406116742Ssamstatic void
407116742Ssamieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
408116742Ssam{
409120481Ssam	free(ni, M_80211_NODE);
410116742Ssam}
411116742Ssam
412116742Ssamstatic void
413116742Ssamieee80211_node_copy(struct ieee80211com *ic,
414116742Ssam	struct ieee80211_node *dst, const struct ieee80211_node *src)
415116742Ssam{
416116742Ssam	*dst = *src;
417116742Ssam}
418116742Ssam
419120104Ssamstatic u_int8_t
420120104Ssamieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
421120104Ssam{
422120104Ssam	return ni->ni_rssi;
423120104Ssam}
424120104Ssam
425116742Ssamstatic void
426116742Ssamieee80211_setup_node(struct ieee80211com *ic,
427116742Ssam	struct ieee80211_node *ni, u_int8_t *macaddr)
428116742Ssam{
429116742Ssam	int hash;
430116742Ssam
431116742Ssam	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
432116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
433116742Ssam	ni->ni_refcnt = 1;		/* mark referenced */
434121172Ssam	IEEE80211_NODE_LOCK(ic);
435116742Ssam	TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
436116742Ssam	LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
437116742Ssam	/*
438116742Ssam	 * Note we don't enable the inactive timer when acting
439116742Ssam	 * as a station.  Nodes created in this mode represent
440116742Ssam	 * AP's identified while scanning.  If we time them out
441116742Ssam	 * then several things happen: we can't return the data
442116742Ssam	 * to users to show the list of AP's we encountered, and
443116742Ssam	 * more importantly, we'll incorrectly deauthenticate
444116742Ssam	 * ourself because the inactivity timer will kick us off.
445116742Ssam	 */
446116742Ssam	if (ic->ic_opmode != IEEE80211_M_STA)
447116742Ssam		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
448121172Ssam	IEEE80211_NODE_UNLOCK(ic);
449116742Ssam}
450116742Ssam
451116742Ssamstruct ieee80211_node *
452116742Ssamieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
453116742Ssam{
454116742Ssam	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
455116742Ssam	if (ni != NULL)
456116742Ssam		ieee80211_setup_node(ic, ni, macaddr);
457116742Ssam	return ni;
458116742Ssam}
459116742Ssam
460116742Ssamstruct ieee80211_node *
461116742Ssamieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
462116742Ssam{
463116742Ssam	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
464116742Ssam	if (ni != NULL) {
465116742Ssam		memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node));
466116742Ssam		ieee80211_setup_node(ic, ni, macaddr);
467116742Ssam	}
468116742Ssam	return ni;
469116742Ssam}
470116742Ssam
471116742Ssamstruct ieee80211_node *
472116742Ssamieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
473116742Ssam{
474116742Ssam	struct ieee80211_node *ni;
475116742Ssam	int hash;
476116742Ssam
477116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
478121172Ssam	IEEE80211_NODE_LOCK(ic);
479116742Ssam	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
480116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
481116742Ssam			atomic_add_int(&ni->ni_refcnt, 1); /* mark referenced */
482116742Ssam			break;
483116742Ssam		}
484116742Ssam	}
485121172Ssam	IEEE80211_NODE_UNLOCK(ic);
486116742Ssam	return ni;
487116742Ssam}
488116742Ssam
489116742Ssam/*
490116742Ssam * Like find but search based on the channel too.
491116742Ssam */
492116742Ssamstruct ieee80211_node *
493116742Ssamieee80211_lookup_node(struct ieee80211com *ic,
494116742Ssam	u_int8_t *macaddr, struct ieee80211_channel *chan)
495116742Ssam{
496116742Ssam	struct ieee80211_node *ni;
497116742Ssam	int hash;
498116742Ssam
499116742Ssam	hash = IEEE80211_NODE_HASH(macaddr);
500121172Ssam	IEEE80211_NODE_LOCK(ic);
501116742Ssam	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
502116742Ssam		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) {
503116742Ssam			atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */
504116742Ssam			break;
505116742Ssam		}
506116742Ssam	}
507121172Ssam	IEEE80211_NODE_UNLOCK(ic);
508116742Ssam	return ni;
509116742Ssam}
510116742Ssam
511116742Ssamstatic void
512116742Ssam_ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
513116742Ssam{
514119150Ssam	KASSERT(ni != ic->ic_bss, ("freeing bss node"));
515119150Ssam
516116742Ssam	TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
517116742Ssam	LIST_REMOVE(ni, ni_hash);
518116742Ssam	if (TAILQ_EMPTY(&ic->ic_node))
519116742Ssam		ic->ic_inact_timer = 0;
520116742Ssam	(*ic->ic_node_free)(ic, ni);
521116742Ssam}
522116742Ssam
523116742Ssamvoid
524116742Ssamieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
525116742Ssam{
526119150Ssam	KASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
527119150Ssam
528116742Ssam	/* XXX need equivalent of atomic_dec_and_test */
529116742Ssam	atomic_subtract_int(&ni->ni_refcnt, 1);
530116742Ssam	if (atomic_cmpset_int(&ni->ni_refcnt, 0, 1)) {
531121172Ssam		IEEE80211_NODE_LOCK(ic);
532116742Ssam		_ieee80211_free_node(ic, ni);
533121172Ssam		IEEE80211_NODE_UNLOCK(ic);
534116742Ssam	}
535116742Ssam}
536116742Ssam
537116742Ssamvoid
538116742Ssamieee80211_free_allnodes(struct ieee80211com *ic)
539116742Ssam{
540116742Ssam	struct ieee80211_node *ni;
541116742Ssam
542121172Ssam	IEEE80211_NODE_LOCK(ic);
543116742Ssam	while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
544116742Ssam		_ieee80211_free_node(ic, ni);
545121172Ssam	IEEE80211_NODE_UNLOCK(ic);
546116742Ssam}
547116742Ssam
548120483Ssam/*
549120483Ssam * Timeout inactive nodes.  Note that we cannot hold the node
550120483Ssam * lock while sending a frame as this would lead to a LOR.
551120483Ssam * Instead we use a generation number to mark nodes that we've
552120483Ssam * scanned and drop the lock and restart a scan if we have to
553120483Ssam * time out a node.  Since we are single-threaded by virtue of
554120483Ssam * controlling the inactivity timer we can be sure this will
555120483Ssam * process each node only once.
556120483Ssam */
557116742Ssamvoid
558116742Ssamieee80211_timeout_nodes(struct ieee80211com *ic)
559116742Ssam{
560120483Ssam	struct ieee80211_node *ni;
561120483Ssam	u_int gen = ic->ic_scangen++;		/* NB: ok 'cuz single-threaded*/
562116742Ssam
563120483Ssamrestart:
564121172Ssam	IEEE80211_NODE_LOCK(ic);
565120483Ssam	TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
566120483Ssam		if (ni->ni_scangen == gen)	/* previously handled */
567120483Ssam			continue;
568120483Ssam		ni->ni_scangen = gen;
569119150Ssam		if (++ni->ni_inact > IEEE80211_INACT_MAX) {
570119150Ssam			IEEE80211_DPRINTF(("station %s timed out "
571116742Ssam			    "due to inactivity (%u secs)\n",
572116742Ssam			    ether_sprintf(ni->ni_macaddr),
573116742Ssam			    ni->ni_inact));
574119150Ssam			/*
575119150Ssam			 * Send a deauthenticate frame.
576120483Ssam			 *
577120483Ssam			 * Drop the node lock before sending the
578120483Ssam			 * deauthentication frame in case the driver takes
579120483Ssam			 * a lock, as this will result in a LOR between the
580120483Ssam			 * node lock and the driver lock.
581119150Ssam			 */
582121172Ssam			IEEE80211_NODE_UNLOCK(ic);
583119150Ssam			IEEE80211_SEND_MGMT(ic, ni,
584119150Ssam			    IEEE80211_FC0_SUBTYPE_DEAUTH,
585119150Ssam			    IEEE80211_REASON_AUTH_EXPIRE);
586119150Ssam			ieee80211_free_node(ic, ni);
587121180Ssam			ic->ic_stats.is_node_timeout++;
588120483Ssam			goto restart;
589120483Ssam		}
590116742Ssam	}
591116742Ssam	if (!TAILQ_EMPTY(&ic->ic_node))
592116742Ssam		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
593121172Ssam	IEEE80211_NODE_UNLOCK(ic);
594116742Ssam}
595116742Ssam
596116742Ssamvoid
597116742Ssamieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
598116742Ssam{
599116742Ssam	struct ieee80211_node *ni;
600116742Ssam
601121172Ssam	IEEE80211_NODE_LOCK(ic);
602116742Ssam	TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
603116742Ssam		(*f)(arg, ni);
604121172Ssam	IEEE80211_NODE_UNLOCK(ic);
605116742Ssam}
606