Home | History | Annotate | Line # | Download | only in net80211
ieee80211_node.c revision 1.1.1.2
      1 /*-
      2  * Copyright (c) 2001 Atsushi Onoe
      3  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * Alternatively, this software may be distributed under the terms of the
     18  * GNU General Public License ("GPL") version 2 as published by the Free
     19  * Software Foundation.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.13 2003/11/09 23:36:46 sam Exp $");
     35 
     36 #include "opt_inet.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/mbuf.h>
     41 #include <sys/malloc.h>
     42 #include <sys/kernel.h>
     43 #include <sys/socket.h>
     44 #include <sys/sockio.h>
     45 #include <sys/endian.h>
     46 #include <sys/errno.h>
     47 #include <sys/bus.h>
     48 #include <sys/proc.h>
     49 #include <sys/sysctl.h>
     50 
     51 #include <machine/atomic.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_media.h>
     56 #include <net/if_arp.h>
     57 #include <net/ethernet.h>
     58 #include <net/if_llc.h>
     59 
     60 #include <net80211/ieee80211_var.h>
     61 
     62 #include <net/bpf.h>
     63 
     64 #ifdef INET
     65 #include <netinet/in.h>
     66 #include <netinet/if_ether.h>
     67 #endif
     68 
     69 static struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *);
     70 static void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *);
     71 static void ieee80211_node_copy(struct ieee80211com *,
     72 		struct ieee80211_node *, const struct ieee80211_node *);
     73 static u_int8_t ieee80211_node_getrssi(struct ieee80211com *,
     74 		struct ieee80211_node *);
     75 
     76 static void ieee80211_setup_node(struct ieee80211com *ic,
     77 		struct ieee80211_node *ni, u_int8_t *macaddr);
     78 static void _ieee80211_free_node(struct ieee80211com *,
     79 		struct ieee80211_node *);
     80 
     81 MALLOC_DEFINE(M_80211_NODE, "node", "802.11 node state");
     82 
     83 void
     84 ieee80211_node_attach(struct ifnet *ifp)
     85 {
     86 	struct ieee80211com *ic = (void *)ifp;
     87 
     88 	/* XXX need unit */
     89 	IEEE80211_NODE_LOCK_INIT(ic, ifp->if_xname);
     90 	TAILQ_INIT(&ic->ic_node);
     91 	ic->ic_node_alloc = ieee80211_node_alloc;
     92 	ic->ic_node_free = ieee80211_node_free;
     93 	ic->ic_node_copy = ieee80211_node_copy;
     94 	ic->ic_node_getrssi = ieee80211_node_getrssi;
     95 	ic->ic_scangen = 1;
     96 }
     97 
     98 void
     99 ieee80211_node_lateattach(struct ifnet *ifp)
    100 {
    101 	struct ieee80211com *ic = (void *)ifp;
    102 
    103 	ic->ic_bss = (*ic->ic_node_alloc)(ic);
    104 	KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node"));
    105 	ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC;
    106 }
    107 
    108 void
    109 ieee80211_node_detach(struct ifnet *ifp)
    110 {
    111 	struct ieee80211com *ic = (void *)ifp;
    112 
    113 	if (ic->ic_bss != NULL)
    114 		(*ic->ic_node_free)(ic, ic->ic_bss);
    115 	ieee80211_free_allnodes(ic);
    116 	IEEE80211_NODE_LOCK_DESTROY(ic);
    117 }
    118 
    119 /*
    120  * AP scanning support.
    121  */
    122 
    123 /*
    124  * Initialize the active channel set based on the set
    125  * of available channels and the current PHY mode.
    126  */
    127 static void
    128 ieee80211_reset_scan(struct ifnet *ifp)
    129 {
    130 	struct ieee80211com *ic = (void *)ifp;
    131 
    132 	memcpy(ic->ic_chan_scan, ic->ic_chan_active,
    133 		sizeof(ic->ic_chan_active));
    134 	/* NB: hack, setup so next_scan starts with the first channel */
    135 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
    136 		ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
    137 }
    138 
    139 /*
    140  * Begin an active scan.
    141  */
    142 void
    143 ieee80211_begin_scan(struct ifnet *ifp)
    144 {
    145 	struct ieee80211com *ic = (void *)ifp;
    146 
    147 	/*
    148 	 * In all but hostap mode scanning starts off in
    149 	 * an active mode before switching to passive.
    150 	 */
    151 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
    152 		ic->ic_flags |= IEEE80211_F_ASCAN;
    153 		ic->ic_stats.is_scan_active++;
    154 	} else
    155 		ic->ic_stats.is_scan_passive++;
    156 	if (ifp->if_flags & IFF_DEBUG)
    157 		if_printf(ifp, "begin %s scan\n",
    158 			(ic->ic_flags & IEEE80211_F_ASCAN) ?
    159 				"active" : "passive");
    160 	/*
    161 	 * Clear scan state and flush any previously seen
    162 	 * AP's.  Note that the latter assumes we don't act
    163 	 * as both an AP and a station, otherwise we'll
    164 	 * potentially flush state of stations associated
    165 	 * with us.
    166 	 */
    167 	ieee80211_reset_scan(ifp);
    168 	ieee80211_free_allnodes(ic);
    169 
    170 	/* Scan the next channel. */
    171 	ieee80211_next_scan(ifp);
    172 }
    173 
    174 /*
    175  * Switch to the next channel marked for scanning.
    176  */
    177 void
    178 ieee80211_next_scan(struct ifnet *ifp)
    179 {
    180 	struct ieee80211com *ic = (void *)ifp;
    181 	struct ieee80211_channel *chan;
    182 
    183 	chan = ic->ic_bss->ni_chan;
    184 	for (;;) {
    185 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
    186 			chan = &ic->ic_channels[0];
    187 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
    188 			/*
    189 			 * Honor channels marked passive-only
    190 			 * during an active scan.
    191 			 */
    192 			if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
    193 			    (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
    194 				break;
    195 		}
    196 		if (chan == ic->ic_bss->ni_chan) {
    197 			ieee80211_end_scan(ifp);
    198 			return;
    199 		}
    200 	}
    201 	clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
    202 	IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
    203 	    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
    204 	    ieee80211_chan2ieee(ic, chan)));
    205 	ic->ic_bss->ni_chan = chan;
    206 	ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    207 }
    208 
    209 void
    210 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
    211 {
    212 	struct ieee80211_node *ni;
    213 	struct ifnet *ifp = &ic->ic_if;
    214 
    215 	ni = ic->ic_bss;
    216 	if (ifp->if_flags & IFF_DEBUG)
    217 		if_printf(ifp, "creating ibss\n");
    218 	ic->ic_flags |= IEEE80211_F_SIBSS;
    219 	ni->ni_chan = chan;
    220 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
    221 	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
    222 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
    223 	if (ic->ic_opmode == IEEE80211_M_IBSS)
    224 		ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
    225 	ni->ni_esslen = ic->ic_des_esslen;
    226 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
    227 	ni->ni_rssi = 0;
    228 	ni->ni_rstamp = 0;
    229 	memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
    230 	ni->ni_intval = ic->ic_lintval;
    231 	ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
    232 	if (ic->ic_flags & IEEE80211_F_WEPON)
    233 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
    234 	if (ic->ic_phytype == IEEE80211_T_FH) {
    235 		ni->ni_fhdwell = 200;	/* XXX */
    236 		ni->ni_fhindex = 1;
    237 	}
    238 	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    239 }
    240 
    241 /*
    242  * Complete a scan of potential channels.
    243  */
    244 void
    245 ieee80211_end_scan(struct ifnet *ifp)
    246 {
    247 	struct ieee80211com *ic = (void *)ifp;
    248 	struct ieee80211_node *ni, *nextbs, *selbs;
    249 	u_int8_t rate;
    250 	int i, fail;
    251 
    252 	ic->ic_flags &= ~IEEE80211_F_ASCAN;
    253 	ni = TAILQ_FIRST(&ic->ic_node);
    254 
    255 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    256 		/* XXX off stack? */
    257 		u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
    258 		/*
    259 		 * The passive scan to look for existing AP's completed,
    260 		 * select a channel to camp on.  Identify the channels
    261 		 * that already have one or more AP's and try to locate
    262 		 * an unnoccupied one.  If that fails, pick a random
    263 		 * channel from the active set.
    264 		 */
    265 		for (; ni != NULL; ni = nextbs) {
    266 			ieee80211_ref_node(ni);
    267 			nextbs = TAILQ_NEXT(ni, ni_list);
    268 			setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
    269 			ieee80211_free_node(ic, ni);
    270 		}
    271 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    272 			if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
    273 				break;
    274 		if (i == IEEE80211_CHAN_MAX) {
    275 			fail = arc4random() & 3;	/* random 0-3 */
    276 			for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    277 				if (isset(ic->ic_chan_active, i) && fail-- == 0)
    278 					break;
    279 		}
    280 		ieee80211_create_ibss(ic, &ic->ic_channels[i]);
    281 		return;
    282 	}
    283 	if (ni == NULL) {
    284 		IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
    285   notfound:
    286 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
    287 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
    288 		    ic->ic_des_esslen != 0) {
    289 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
    290 			return;
    291 		}
    292 		/*
    293 		 * Reset the list of channels to scan and start again.
    294 		 */
    295 		ieee80211_reset_scan(ifp);
    296 		ieee80211_next_scan(ifp);
    297 		return;
    298 	}
    299 	selbs = NULL;
    300 	if (ifp->if_flags & IFF_DEBUG)
    301 		if_printf(ifp, "\tmacaddr          bssid         chan  rssi rate flag  wep  essid\n");
    302 	for (; ni != NULL; ni = nextbs) {
    303 		ieee80211_ref_node(ni);
    304 		nextbs = TAILQ_NEXT(ni, ni_list);
    305 		if (ni->ni_fails) {
    306 			/*
    307 			 * The configuration of the access points may change
    308 			 * during my scan.  So delete the entry for the AP
    309 			 * and retry to associate if there is another beacon.
    310 			 */
    311 			if (ni->ni_fails++ > 2)
    312 				ieee80211_free_node(ic, ni);
    313 			continue;
    314 		}
    315 		fail = 0;
    316 		if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    317 			fail |= 0x01;
    318 		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
    319 		    ni->ni_chan != ic->ic_des_chan)
    320 			fail |= 0x01;
    321 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
    322 			if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    323 				fail |= 0x02;
    324 		} else {
    325 			if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    326 				fail |= 0x02;
    327 		}
    328 		if (ic->ic_flags & IEEE80211_F_WEPON) {
    329 			if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    330 				fail |= 0x04;
    331 		} else {
    332 			if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    333 				fail |= 0x04;
    334 		}
    335 		rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
    336 		if (rate & IEEE80211_RATE_BASIC)
    337 			fail |= 0x08;
    338 		if (ic->ic_des_esslen != 0 &&
    339 		    (ni->ni_esslen != ic->ic_des_esslen ||
    340 		     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
    341 			fail |= 0x10;
    342 		if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
    343 		    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
    344 			fail |= 0x20;
    345 		if (ifp->if_flags & IFF_DEBUG) {
    346 			printf(" %c %s", fail ? '-' : '+',
    347 			    ether_sprintf(ni->ni_macaddr));
    348 			printf(" %s%c", ether_sprintf(ni->ni_bssid),
    349 			    fail & 0x20 ? '!' : ' ');
    350 			printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
    351 				fail & 0x01 ? '!' : ' ');
    352 			printf(" %+4d", ni->ni_rssi);
    353 			printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
    354 			    fail & 0x08 ? '!' : ' ');
    355 			printf(" %4s%c",
    356 			    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
    357 			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
    358 			    "????",
    359 			    fail & 0x02 ? '!' : ' ');
    360 			printf(" %3s%c ",
    361 			    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
    362 			    "wep" : "no",
    363 			    fail & 0x04 ? '!' : ' ');
    364 			ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
    365 			printf("%s\n", fail & 0x10 ? "!" : "");
    366 		}
    367 		if (!fail) {
    368 			if (selbs == NULL)
    369 				selbs = ni;
    370 			else if (ni->ni_rssi > selbs->ni_rssi) {
    371 				ieee80211_unref_node(&selbs);
    372 				selbs = ni;
    373 			} else
    374 				ieee80211_unref_node(&ni);
    375 		} else {
    376 			ieee80211_unref_node(&ni);
    377 		}
    378 	}
    379 	if (selbs == NULL)
    380 		goto notfound;
    381 	(*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
    382 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    383 		ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
    384 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
    385 		if (ic->ic_bss->ni_rates.rs_nrates == 0) {
    386 			selbs->ni_fails++;
    387 			ieee80211_unref_node(&selbs);
    388 			goto notfound;
    389 		}
    390 		ieee80211_unref_node(&selbs);
    391 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    392 	} else {
    393 		ieee80211_unref_node(&selbs);
    394 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
    395 	}
    396 }
    397 
    398 static struct ieee80211_node *
    399 ieee80211_node_alloc(struct ieee80211com *ic)
    400 {
    401 	return malloc(sizeof(struct ieee80211_node), M_80211_NODE,
    402 		M_NOWAIT | M_ZERO);
    403 }
    404 
    405 static void
    406 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
    407 {
    408 	free(ni, M_80211_NODE);
    409 }
    410 
    411 static void
    412 ieee80211_node_copy(struct ieee80211com *ic,
    413 	struct ieee80211_node *dst, const struct ieee80211_node *src)
    414 {
    415 	*dst = *src;
    416 }
    417 
    418 static u_int8_t
    419 ieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
    420 {
    421 	return ni->ni_rssi;
    422 }
    423 
    424 static void
    425 ieee80211_setup_node(struct ieee80211com *ic,
    426 	struct ieee80211_node *ni, u_int8_t *macaddr)
    427 {
    428 	int hash;
    429 
    430 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
    431 	hash = IEEE80211_NODE_HASH(macaddr);
    432 	ni->ni_refcnt = 1;		/* mark referenced */
    433 	IEEE80211_NODE_LOCK(ic);
    434 	TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
    435 	LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
    436 	/*
    437 	 * Note we don't enable the inactive timer when acting
    438 	 * as a station.  Nodes created in this mode represent
    439 	 * AP's identified while scanning.  If we time them out
    440 	 * then several things happen: we can't return the data
    441 	 * to users to show the list of AP's we encountered, and
    442 	 * more importantly, we'll incorrectly deauthenticate
    443 	 * ourself because the inactivity timer will kick us off.
    444 	 */
    445 	if (ic->ic_opmode != IEEE80211_M_STA)
    446 		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
    447 	IEEE80211_NODE_UNLOCK(ic);
    448 }
    449 
    450 struct ieee80211_node *
    451 ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
    452 {
    453 	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
    454 	if (ni != NULL)
    455 		ieee80211_setup_node(ic, ni, macaddr);
    456 	return ni;
    457 }
    458 
    459 struct ieee80211_node *
    460 ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
    461 {
    462 	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
    463 	if (ni != NULL) {
    464 		memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node));
    465 		ieee80211_setup_node(ic, ni, macaddr);
    466 	}
    467 	return ni;
    468 }
    469 
    470 struct ieee80211_node *
    471 ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
    472 {
    473 	struct ieee80211_node *ni;
    474 	int hash;
    475 
    476 	hash = IEEE80211_NODE_HASH(macaddr);
    477 	IEEE80211_NODE_LOCK(ic);
    478 	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
    479 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
    480 			atomic_add_int(&ni->ni_refcnt, 1); /* mark referenced */
    481 			break;
    482 		}
    483 	}
    484 	IEEE80211_NODE_UNLOCK(ic);
    485 	return ni;
    486 }
    487 
    488 /*
    489  * Like find but search based on the channel too.
    490  */
    491 struct ieee80211_node *
    492 ieee80211_lookup_node(struct ieee80211com *ic,
    493 	u_int8_t *macaddr, struct ieee80211_channel *chan)
    494 {
    495 	struct ieee80211_node *ni;
    496 	int hash;
    497 
    498 	hash = IEEE80211_NODE_HASH(macaddr);
    499 	IEEE80211_NODE_LOCK(ic);
    500 	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
    501 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) {
    502 			atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */
    503 			break;
    504 		}
    505 	}
    506 	IEEE80211_NODE_UNLOCK(ic);
    507 	return ni;
    508 }
    509 
    510 static void
    511 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
    512 {
    513 	KASSERT(ni != ic->ic_bss, ("freeing bss node"));
    514 
    515 	TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
    516 	LIST_REMOVE(ni, ni_hash);
    517 	if (TAILQ_EMPTY(&ic->ic_node))
    518 		ic->ic_inact_timer = 0;
    519 	(*ic->ic_node_free)(ic, ni);
    520 }
    521 
    522 void
    523 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
    524 {
    525 	KASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
    526 
    527 	/* XXX need equivalent of atomic_dec_and_test */
    528 	atomic_subtract_int(&ni->ni_refcnt, 1);
    529 	if (atomic_cmpset_int(&ni->ni_refcnt, 0, 1)) {
    530 		IEEE80211_NODE_LOCK(ic);
    531 		_ieee80211_free_node(ic, ni);
    532 		IEEE80211_NODE_UNLOCK(ic);
    533 	}
    534 }
    535 
    536 void
    537 ieee80211_free_allnodes(struct ieee80211com *ic)
    538 {
    539 	struct ieee80211_node *ni;
    540 
    541 	IEEE80211_NODE_LOCK(ic);
    542 	while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
    543 		_ieee80211_free_node(ic, ni);
    544 	IEEE80211_NODE_UNLOCK(ic);
    545 }
    546 
    547 /*
    548  * Timeout inactive nodes.  Note that we cannot hold the node
    549  * lock while sending a frame as this would lead to a LOR.
    550  * Instead we use a generation number to mark nodes that we've
    551  * scanned and drop the lock and restart a scan if we have to
    552  * time out a node.  Since we are single-threaded by virtue of
    553  * controlling the inactivity timer we can be sure this will
    554  * process each node only once.
    555  */
    556 void
    557 ieee80211_timeout_nodes(struct ieee80211com *ic)
    558 {
    559 	struct ieee80211_node *ni;
    560 	u_int gen = ic->ic_scangen++;		/* NB: ok 'cuz single-threaded*/
    561 
    562 restart:
    563 	IEEE80211_NODE_LOCK(ic);
    564 	TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    565 		if (ni->ni_scangen == gen)	/* previously handled */
    566 			continue;
    567 		ni->ni_scangen = gen;
    568 		if (++ni->ni_inact > IEEE80211_INACT_MAX) {
    569 			IEEE80211_DPRINTF(("station %s timed out "
    570 			    "due to inactivity (%u secs)\n",
    571 			    ether_sprintf(ni->ni_macaddr),
    572 			    ni->ni_inact));
    573 			/*
    574 			 * Send a deauthenticate frame.
    575 			 *
    576 			 * Drop the node lock before sending the
    577 			 * deauthentication frame in case the driver takes
    578 			 * a lock, as this will result in a LOR between the
    579 			 * node lock and the driver lock.
    580 			 */
    581 			IEEE80211_NODE_UNLOCK(ic);
    582 			IEEE80211_SEND_MGMT(ic, ni,
    583 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
    584 			    IEEE80211_REASON_AUTH_EXPIRE);
    585 			ieee80211_free_node(ic, ni);
    586 			ic->ic_stats.is_node_timeout++;
    587 			goto restart;
    588 		}
    589 	}
    590 	if (!TAILQ_EMPTY(&ic->ic_node))
    591 		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
    592 	IEEE80211_NODE_UNLOCK(ic);
    593 }
    594 
    595 void
    596 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
    597 {
    598 	struct ieee80211_node *ni;
    599 
    600 	IEEE80211_NODE_LOCK(ic);
    601 	TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
    602 		(*f)(arg, ni);
    603 	IEEE80211_NODE_UNLOCK(ic);
    604 }
    605