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