Home | History | Annotate | Line # | Download | only in net80211
ieee80211_node.c revision 1.29
      1 /*	$NetBSD: ieee80211_node.c,v 1.29 2004/07/28 08:11:03 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002-2004 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.22 2004/04/05 04:15:55 sam Exp $");
     37 #else
     38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_node.c,v 1.29 2004/07/28 08:11:03 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 u_int8_t ieee80211_node_getrssi(struct ieee80211com *,
     92 		struct ieee80211_node *);
     93 
     94 static void ieee80211_setup_node(struct ieee80211com *ic,
     95 		struct ieee80211_node *ni, u_int8_t *macaddr);
     96 static void _ieee80211_free_node(struct ieee80211com *,
     97 		struct ieee80211_node *);
     98 
     99 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
    100 
    101 void
    102 ieee80211_node_attach(struct ieee80211com *ic)
    103 {
    104 
    105 	/* XXX need unit */
    106 	IEEE80211_NODE_LOCK_INIT(ic, ic->ic_ifp->if_xname);
    107 	TAILQ_INIT(&ic->ic_node);
    108 	ic->ic_node_alloc = ieee80211_node_alloc;
    109 	ic->ic_node_free = ieee80211_node_free;
    110 	ic->ic_node_copy = ieee80211_node_copy;
    111 	ic->ic_node_getrssi = ieee80211_node_getrssi;
    112 	ic->ic_scangen = 1;
    113 
    114 	if (ic->ic_max_aid == 0)
    115 		ic->ic_max_aid = IEEE80211_AID_DEF;
    116 	else if (ic->ic_max_aid > IEEE80211_AID_MAX)
    117 		ic->ic_max_aid = IEEE80211_AID_MAX;
    118 	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
    119 		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
    120 		M_DEVBUF, M_NOWAIT | M_ZERO);
    121 	if (ic->ic_aid_bitmap == NULL) {
    122 		/* XXX no way to recover */
    123 		printf("%s: no memory for AID bitmap!\n", __func__);
    124 		ic->ic_max_aid = 0;
    125 	}
    126 }
    127 
    128 void
    129 ieee80211_node_lateattach(struct ieee80211com *ic)
    130 {
    131 	struct ieee80211_node *ni;
    132 
    133 	ni = (*ic->ic_node_alloc)(ic);
    134 	IASSERT(ni != NULL, ("unable to setup inital BSS node"));
    135 	ni->ni_chan = IEEE80211_CHAN_ANYC;
    136 	ic->ic_bss = ni;
    137 	ic->ic_txpower = IEEE80211_TXPOWER_MAX;
    138 }
    139 
    140 void
    141 ieee80211_node_detach(struct ieee80211com *ic)
    142 {
    143 
    144 	if (ic->ic_bss != NULL) {
    145 		(*ic->ic_node_free)(ic, ic->ic_bss);
    146 		ic->ic_bss = NULL;
    147 	}
    148 	ieee80211_free_allnodes(ic);
    149 	IEEE80211_NODE_LOCK_DESTROY(ic);
    150         if (ic->ic_aid_bitmap != NULL)
    151                 FREE(ic->ic_aid_bitmap, M_DEVBUF);
    152 }
    153 
    154 /*
    155  * AP scanning support.
    156  */
    157 
    158 /*
    159  * Initialize the active channel set based on the set
    160  * of available channels and the current PHY mode.
    161  */
    162 static void
    163 ieee80211_reset_scan(struct ieee80211com *ic)
    164 {
    165 
    166 	memcpy(ic->ic_chan_scan, ic->ic_chan_active,
    167 		sizeof(ic->ic_chan_active));
    168 	/* NB: hack, setup so next_scan starts with the first channel */
    169 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
    170 		ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
    171 }
    172 
    173 /*
    174  * Begin an active scan.
    175  */
    176 void
    177 ieee80211_begin_scan(struct ieee80211com *ic)
    178 {
    179 
    180 	/*
    181 	 * In all but hostap mode scanning starts off in
    182 	 * an active mode before switching to passive.
    183 	 */
    184 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
    185 		ic->ic_flags |= IEEE80211_F_ASCAN;
    186 		ic->ic_stats.is_scan_active++;
    187 	} else
    188 		ic->ic_stats.is_scan_passive++;
    189 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, ("begin %s scan\n",
    190 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive"));
    191 	/*
    192 	 * Clear scan state and flush any previously seen
    193 	 * AP's.  Note that the latter assumes we don't act
    194 	 * as both an AP and a station, otherwise we'll
    195 	 * potentially flush state of stations associated
    196 	 * with us.
    197 	 */
    198 	ieee80211_reset_scan(ic);
    199 	ieee80211_free_allnodes(ic);
    200 
    201 	/* Scan the next channel. */
    202 	ieee80211_next_scan(ic);
    203 }
    204 
    205 /*
    206  * Switch to the next channel marked for scanning.
    207  */
    208 void
    209 ieee80211_next_scan(struct ieee80211com *ic)
    210 {
    211 	struct ieee80211_channel *chan;
    212 
    213 	chan = ic->ic_bss->ni_chan;
    214 	for (;;) {
    215 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
    216 			chan = &ic->ic_channels[0];
    217 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
    218 			/*
    219 			 * Honor channels marked passive-only
    220 			 * during an active scan.
    221 			 */
    222 			if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
    223 			    (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
    224 				break;
    225 		}
    226 		if (chan == ic->ic_bss->ni_chan) {
    227 			ieee80211_end_scan(ic);
    228 			return;
    229 		}
    230 	}
    231 	clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
    232 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    233 		("%s: chan %d->%d\n", __func__,
    234 		ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
    235 		ieee80211_chan2ieee(ic, chan)));
    236 	ic->ic_bss->ni_chan = chan;
    237 	ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    238 }
    239 
    240 void
    241 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
    242 {
    243 	struct ieee80211_node *ni;
    244 
    245 	ni = ic->ic_bss;
    246 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, ("creating ibss\n"));
    247 	ic->ic_flags |= IEEE80211_F_SIBSS;
    248 	ni->ni_chan = chan;
    249 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
    250 	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
    251 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
    252 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    253 		if ((ic->ic_flags & IEEE80211_F_DESBSSID) != 0)
    254 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
    255 		else
    256 			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
    257 	}
    258 	ni->ni_esslen = ic->ic_des_esslen;
    259 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
    260 	ni->ni_rssi = 0;
    261 	ni->ni_rstamp = 0;
    262 	memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
    263 	ni->ni_intval = ic->ic_lintval;
    264 	ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
    265 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
    266 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
    267 	if (ic->ic_phytype == IEEE80211_T_FH) {
    268 		ni->ni_fhdwell = 200;	/* XXX */
    269 		ni->ni_fhindex = 1;
    270 	}
    271 	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    272 }
    273 
    274 int
    275 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
    276 {
    277         u_int8_t rate;
    278         int fail;
    279 
    280 	fail = 0;
    281 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    282 		fail |= 0x01;
    283 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
    284 	    ni->ni_chan != ic->ic_des_chan)
    285 		fail |= 0x01;
    286 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    287 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    288 			fail |= 0x02;
    289 	} else {
    290 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    291 			fail |= 0x02;
    292 	}
    293 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
    294 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    295 			fail |= 0x04;
    296 	} else {
    297 		/* XXX does this mean privacy is supported or required? */
    298 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    299 			fail |= 0x04;
    300 	}
    301 	rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
    302 	if (rate & IEEE80211_RATE_BASIC)
    303 		fail |= 0x08;
    304 	if (ic->ic_des_esslen != 0 &&
    305 	    (ni->ni_esslen != ic->ic_des_esslen ||
    306 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
    307 		fail |= 0x10;
    308 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
    309 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
    310 		fail |= 0x20;
    311 #ifdef IEEE80211_DEBUG
    312 	if (ic->ic_if.if_flags & IFF_DEBUG) {
    313 		printf(" %c %s", fail ? '-' : '+',
    314 		    ether_sprintf(ni->ni_macaddr));
    315 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
    316 		    fail & 0x20 ? '!' : ' ');
    317 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
    318 			fail & 0x01 ? '!' : ' ');
    319 		printf(" %+4d", ni->ni_rssi);
    320 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
    321 		    fail & 0x08 ? '!' : ' ');
    322 		printf(" %4s%c",
    323 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
    324 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
    325 		    "????",
    326 		    fail & 0x02 ? '!' : ' ');
    327 		printf(" %3s%c ",
    328 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
    329 		    "wep" : "no",
    330 		    fail & 0x04 ? '!' : ' ');
    331 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
    332 		printf("%s\n", fail & 0x10 ? "!" : "");
    333 	}
    334 #endif
    335 	return fail;
    336 }
    337 
    338 /*
    339  * Complete a scan of potential channels.
    340  */
    341 void
    342 ieee80211_end_scan(struct ieee80211com *ic)
    343 {
    344 	struct ieee80211_node *ni, *nextbs, *selbs;
    345 	int i, fail;
    346 
    347 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, ("end %s scan\n",
    348 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive"));
    349 
    350 	ic->ic_flags &= ~IEEE80211_F_ASCAN;
    351 	ni = TAILQ_FIRST(&ic->ic_node);
    352 
    353 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    354 		/* XXX off stack? */
    355 		u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
    356 		/*
    357 		 * The passive scan to look for existing AP's completed,
    358 		 * select a channel to camp on.  Identify the channels
    359 		 * that already have one or more AP's and try to locate
    360 		 * an unnoccupied one.  If that fails, pick a random
    361 		 * channel from the active set.
    362 		 */
    363 		for (; ni != NULL; ni = nextbs) {
    364 			ieee80211_ref_node(ni);
    365 			nextbs = TAILQ_NEXT(ni, ni_list);
    366 			setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
    367 			ieee80211_free_node(ic, ni);
    368 		}
    369 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    370 			if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
    371 				break;
    372 		if (i == IEEE80211_CHAN_MAX) {
    373 			fail = arc4random() & 3;	/* random 0-3 */
    374 			for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    375 				if (isset(ic->ic_chan_active, i) && fail-- == 0)
    376 					break;
    377 		}
    378 		ieee80211_create_ibss(ic, &ic->ic_channels[i]);
    379 		return;
    380 	}
    381 	if (ni == NULL) {
    382 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    383 			("%s: no scan candidate\n", __func__));
    384   notfound:
    385 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
    386 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
    387 		    ic->ic_des_esslen != 0) {
    388 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
    389 			return;
    390 		}
    391 		/*
    392 		 * Reset the list of channels to scan and start again.
    393 		 */
    394 		ieee80211_reset_scan(ic);
    395 		ieee80211_next_scan(ic);
    396 		return;
    397 	}
    398 	selbs = NULL;
    399 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    400 		("\tmacaddr          bssid         chan  rssi rate flag  wep  essid\n"));
    401 	for (; ni != NULL; ni = nextbs) {
    402 		ieee80211_ref_node(ni);
    403 		nextbs = TAILQ_NEXT(ni, ni_list);
    404 		if (ni->ni_fails) {
    405 			/*
    406 			 * The configuration of the access points may change
    407 			 * during my scan.  So delete the entry for the AP
    408 			 * and retry to associate if there is another beacon.
    409 			 */
    410 			if (ni->ni_fails++ > 2)
    411 				ieee80211_free_node(ic, ni);
    412 			continue;
    413 		}
    414 		if (ieee80211_match_bss(ic, ni) == 0) {
    415 			if (selbs == NULL)
    416 				selbs = ni;
    417 			else if (ni->ni_rssi > selbs->ni_rssi) {
    418 				ieee80211_unref_node(&selbs);
    419 				selbs = ni;
    420 			} else
    421 				ieee80211_unref_node(&ni);
    422 		} else {
    423 			ieee80211_unref_node(&ni);
    424 		}
    425 	}
    426 	if (selbs == NULL)
    427 		goto notfound;
    428 	(*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
    429 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    430 		ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
    431 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
    432 		if (ic->ic_bss->ni_rates.rs_nrates == 0) {
    433 			selbs->ni_fails++;
    434 			ieee80211_unref_node(&selbs);
    435 			goto notfound;
    436 		}
    437 		ieee80211_unref_node(&selbs);
    438 		/*
    439 		 * Discard scan set; the nodes have a refcnt of zero
    440 		 * and have not asked the driver to setup private
    441 		 * node state.  Let them be repopulated on demand either
    442 		 * through transmission (ieee80211_find_txnode) or receipt
    443 		 * of a probe response (to be added).
    444 		 */
    445 		ieee80211_free_allnodes(ic);
    446 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    447 	} else {
    448 		ieee80211_unref_node(&selbs);
    449 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
    450 	}
    451 }
    452 
    453 int
    454 ieee80211_get_rate(struct ieee80211com *ic)
    455 {
    456 	u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE];
    457 	int rate;
    458 
    459 	rates = &ic->ic_bss->ni_rates.rs_rates;
    460 
    461 	if (ic->ic_fixed_rate != -1)
    462 		rate = (*rates)[ic->ic_fixed_rate];
    463 	else if (ic->ic_state == IEEE80211_S_RUN)
    464 		rate = (*rates)[ic->ic_bss->ni_txrate];
    465 	else
    466 		rate = 0;
    467 
    468 	return rate & IEEE80211_RATE_VAL;
    469 }
    470 
    471 static struct ieee80211_node *
    472 ieee80211_node_alloc(struct ieee80211com *ic)
    473 {
    474 	struct ieee80211_node *ni;
    475 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
    476 		M_80211_NODE, M_NOWAIT | M_ZERO);
    477 	return ni;
    478 }
    479 
    480 static void
    481 node_cleanup(struct ieee80211com *ic, struct ieee80211_node *ni)
    482 {
    483 
    484         if (ni->ni_challenge != NULL) {
    485                 FREE(ni->ni_challenge, M_DEVBUF);
    486                 ni->ni_challenge = NULL;
    487         }
    488 }
    489 
    490 static void
    491 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
    492 {
    493 	node_cleanup(ic, ni);
    494 	FREE(ni, M_80211_NODE);
    495 }
    496 
    497 static void
    498 ieee80211_node_copy(struct ieee80211com *ic,
    499 	struct ieee80211_node *dst, const struct ieee80211_node *src)
    500 {
    501 	node_cleanup(ic, dst);
    502 	*dst = *src;
    503 	dst->ni_challenge = NULL;
    504 }
    505 
    506 static u_int8_t
    507 ieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
    508 {
    509 	return ni->ni_rssi;
    510 }
    511 
    512 static void
    513 ieee80211_setup_node(struct ieee80211com *ic,
    514 	struct ieee80211_node *ni, u_int8_t *macaddr)
    515 {
    516 	int hash;
    517 
    518 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
    519 		("%s %s\n", __func__, ether_sprintf(macaddr)));
    520 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
    521 	hash = IEEE80211_NODE_HASH(macaddr);
    522 	ni->ni_refcnt = 1;		/* mark referenced */
    523 
    524 	IEEE80211_NODE_LOCK_BH(ic);
    525 	TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
    526 	LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
    527 	/*
    528 	 * Note we don't enable the inactive timer when acting
    529 	 * as a station.  Nodes created in this mode represent
    530 	 * AP's identified while scanning.  If we time them out
    531 	 * then several things happen: we can't return the data
    532 	 * to users to show the list of AP's we encountered, and
    533 	 * more importantly, we'll incorrectly deauthenticate
    534 	 * ourself because the inactivity timer will kick us off.
    535 	 */
    536 	if (ic->ic_opmode != IEEE80211_M_STA)
    537 		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
    538 	IEEE80211_NODE_UNLOCK_BH(ic);
    539 }
    540 
    541 struct ieee80211_node *
    542 ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
    543 {
    544 	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
    545 	if (ni != NULL)
    546 		ieee80211_setup_node(ic, ni, macaddr);
    547 	else
    548 		ic->ic_stats.is_rx_nodealloc++;
    549 	return ni;
    550 }
    551 
    552 struct ieee80211_node *
    553 ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
    554 {
    555 	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
    556 	if (ni != NULL) {
    557 		ieee80211_setup_node(ic, ni, macaddr);
    558 		/*
    559 		 * Inherit from ic_bss.
    560 		 */
    561 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
    562 		ni->ni_chan = ic->ic_bss->ni_chan;
    563 	} else
    564 		ic->ic_stats.is_rx_nodealloc++;
    565 	return ni;
    566 }
    567 
    568 static struct ieee80211_node *
    569 _ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
    570 {
    571 	struct ieee80211_node *ni;
    572 	int hash;
    573 
    574 	IEEE80211_NODE_LOCK_ASSERT(ic);
    575 
    576 	hash = IEEE80211_NODE_HASH(macaddr);
    577 	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
    578 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
    579 			ieee80211_node_incref(ni); /* mark referenced */
    580 			return ni;
    581 		}
    582 	}
    583 	return NULL;
    584 }
    585 
    586 struct ieee80211_node *
    587 ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
    588 {
    589 	struct ieee80211_node *ni;
    590 
    591 	IEEE80211_NODE_LOCK(ic);
    592 	ni = _ieee80211_find_node(ic, macaddr);
    593 	IEEE80211_NODE_UNLOCK(ic);
    594 	return ni;
    595 }
    596 
    597 /*
    598  * Return a reference to the appropriate node for sending
    599  * a data frame.  This handles node discovery in adhoc networks.
    600  */
    601 struct ieee80211_node *
    602 ieee80211_find_txnode(struct ieee80211com *ic, u_int8_t *macaddr)
    603 {
    604 	struct ieee80211_node *ni;
    605 
    606 	/*
    607 	 * The destination address should be in the node table
    608 	 * unless we are operating in station mode or this is a
    609 	 * multicast/broadcast frame.
    610 	 */
    611 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
    612 		return ic->ic_bss;
    613 
    614 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
    615 	IEEE80211_NODE_LOCK(ic);
    616 	ni = _ieee80211_find_node(ic, macaddr);
    617 	IEEE80211_NODE_UNLOCK(ic);
    618 	if (ni == NULL &&
    619 	    (ic->ic_opmode == IEEE80211_M_IBSS ||
    620 	     ic->ic_opmode == IEEE80211_M_AHDEMO)) {
    621 		/*
    622 		 * Fake up a node; this handles node discovery in
    623 		 * adhoc mode.  Note that for the driver's benefit
    624 		 * we we treat this like an association so the driver
    625 		 * has an opportunity to setup it's private state.
    626 		 *
    627 		 * XXX need better way to handle this; issue probe
    628 		 *     request so we can deduce rate set, etc.
    629 		 */
    630 		ni = ieee80211_dup_bss(ic, macaddr);
    631 		if (ni != NULL) {
    632 			/* XXX no rate negotiation; just dup */
    633 			ni->ni_rates = ic->ic_bss->ni_rates;
    634 			if (ic->ic_newassoc)
    635 				(*ic->ic_newassoc)(ic, ni, 1);
    636 		}
    637 	}
    638 	return ni;
    639 }
    640 
    641 /*
    642  * It is usually desirable to process a Rx packet using its sender's
    643  * node-record instead of the BSS record.
    644  *
    645  *  - AP mode: keep a node-record for every authenticated/associated
    646  *    station *in the BSS*. For future use, we also track neighboring
    647  *    APs, since they might belong to the same ESS.  APs in the same
    648  *    ESS may bridge packets to each other, forming a Wireless
    649  *    Distribution System (WDS).
    650  *
    651  * - IBSS mode: keep a node-record for every station *in the BSS*.
    652  *   Also track neighboring stations by their beacons/probe responses.
    653  *
    654  * - monitor mode: keep a node-record for every sender, regardless
    655  *   of BSS.
    656  *
    657  * - STA mode: the only available node-record is the BSS record,
    658  *   ic->ic_bss.
    659  *
    660  * Of all the 802.11 Control packets, only the node-records for
    661  * RTS packets node-record can be looked up.
    662  *
    663  * Return non-zero if the packet's node-record is kept, zero
    664  * otherwise.
    665  */
    666 static __inline int
    667 ieee80211_needs_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh,
    668     u_int8_t **bssid)
    669 {
    670 	struct ieee80211_node *bss = ic->ic_bss;
    671 	int monitor, rc = 0;
    672 
    673 	monitor = (ic->ic_opmode == IEEE80211_M_MONITOR);
    674 
    675 	*bssid = NULL;
    676 
    677 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
    678 	case IEEE80211_FC0_TYPE_CTL:
    679 		if (!monitor)
    680 			break;
    681 		return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
    682 		    IEEE80211_FC0_SUBTYPE_RTS;
    683 	case IEEE80211_FC0_TYPE_MGT:
    684 		*bssid = wh->i_addr3;
    685 		switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
    686 		case IEEE80211_FC0_SUBTYPE_BEACON:
    687 		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    688 			rc = 1;
    689 			break;
    690 		default:
    691 			rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
    692 			break;
    693 		}
    694 		break;
    695 	case IEEE80211_FC0_TYPE_DATA:
    696 		switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    697 		case IEEE80211_FC1_DIR_NODS:
    698 			*bssid = wh->i_addr3;
    699 			if (ic->ic_opmode == IEEE80211_M_IBSS ||
    700 			    ic->ic_opmode == IEEE80211_M_AHDEMO)
    701 				rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
    702 			break;
    703 		case IEEE80211_FC1_DIR_TODS:
    704 			*bssid = wh->i_addr1;
    705 			if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    706 				rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
    707 			break;
    708 		case IEEE80211_FC1_DIR_FROMDS:
    709 		case IEEE80211_FC1_DIR_DSTODS:
    710 			*bssid = wh->i_addr2;
    711 			rc = (ic->ic_opmode == IEEE80211_M_HOSTAP);
    712 			break;
    713 		}
    714 		break;
    715 	}
    716 	return monitor || rc;
    717 }
    718 
    719 struct ieee80211_node *
    720 ieee80211_find_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh)
    721 {
    722 	struct ieee80211_node *ni;
    723 	const static u_int8_t zero[IEEE80211_ADDR_LEN];
    724 	u_int8_t *bssid;
    725 
    726 	if (!ieee80211_needs_rxnode(ic, wh, &bssid))
    727 	        return ieee80211_ref_node(ic->ic_bss);
    728 
    729 	IEEE80211_NODE_LOCK(ic);
    730 	ni = _ieee80211_find_node(ic, wh->i_addr2);
    731 	IEEE80211_NODE_UNLOCK(ic);
    732 
    733 	if (ni != NULL)
    734 		return ni;
    735 
    736 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    737 		return ieee80211_ref_node(ic->ic_bss);
    738 
    739 	/* XXX see remarks in ieee80211_find_txnode */
    740 	/* XXX no rate negotiation; just dup */
    741 	if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) == NULL)
    742 		return ieee80211_ref_node(ic->ic_bss);
    743 
    744 	IEEE80211_ADDR_COPY(ni->ni_bssid, (bssid != NULL) ? bssid : zero);
    745 
    746 	ni->ni_rates = ic->ic_bss->ni_rates;
    747 	if (ic->ic_newassoc)
    748 		(*ic->ic_newassoc)(ic, ni, 1);
    749 
    750 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
    751 		("%s: faked-up node %p for %s\n", __func__, ni,
    752 		ether_sprintf(wh->i_addr2)));
    753 
    754 	return ieee80211_ref_node(ni);
    755 }
    756 
    757 /*
    758  * Like find but search based on the channel too.
    759  */
    760 struct ieee80211_node *
    761 ieee80211_find_node_with_channel(struct ieee80211com *ic, u_int8_t *macaddr,
    762 	struct ieee80211_channel *chan)
    763 {
    764 	struct ieee80211_node *ni;
    765 	int hash;
    766 
    767 	hash = IEEE80211_NODE_HASH(macaddr);
    768 	IEEE80211_NODE_LOCK(ic);
    769 	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
    770 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
    771 		    ni->ni_chan == chan) {
    772 			ieee80211_node_incref(ni);/* mark referenced */
    773 			break;
    774 		}
    775 	}
    776 	IEEE80211_NODE_UNLOCK(ic);
    777 	return ni;
    778 }
    779 
    780 static void
    781 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
    782 {
    783 	IASSERT(ni != ic->ic_bss, ("freeing bss node"));
    784 
    785 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
    786 		("%s %s\n", __func__, ether_sprintf(ni->ni_macaddr)));
    787 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
    788 	TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
    789 	LIST_REMOVE(ni, ni_hash);
    790 	if (!IF_IS_EMPTY(&ni->ni_savedq)) {
    791 		IF_PURGE(&ni->ni_savedq);
    792 		if (ic->ic_set_tim)
    793 			(*ic->ic_set_tim)(ic, ni->ni_associd, 0);
    794 	}
    795 	if (TAILQ_EMPTY(&ic->ic_node))
    796 		ic->ic_inact_timer = 0;
    797 	(*ic->ic_node_free)(ic, ni);
    798 }
    799 
    800 void
    801 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
    802 {
    803 	IASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
    804 
    805 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
    806 		("%s %s refcnt %d\n", __func__,
    807 		 ether_sprintf(ni->ni_macaddr), ni->ni_refcnt));
    808 	if (ieee80211_node_decref(ni) == 0) {
    809 		IEEE80211_NODE_LOCK_BH(ic);
    810 		_ieee80211_free_node(ic, ni);
    811 		IEEE80211_NODE_UNLOCK_BH(ic);
    812 	}
    813 }
    814 
    815 void
    816 ieee80211_free_allnodes(struct ieee80211com *ic)
    817 {
    818 	struct ieee80211_node *ni;
    819 
    820 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, ("free all nodes\n"));
    821 	IEEE80211_NODE_LOCK_BH(ic);
    822 	while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
    823 		_ieee80211_free_node(ic, ni);
    824 	IEEE80211_NODE_UNLOCK_BH(ic);
    825 
    826 	if (ic->ic_bss != NULL)
    827 		node_cleanup(ic, ic->ic_bss);	/* for station mode */
    828 }
    829 
    830 /*
    831  * Timeout inactive nodes.  Note that we cannot hold the node
    832  * lock while sending a frame as this would lead to a LOR.
    833  * Instead we use a generation number to mark nodes that we've
    834  * scanned and drop the lock and restart a scan if we have to
    835  * time out a node.  Since we are single-threaded by virtue of
    836  * controlling the inactivity timer we can be sure this will
    837  * process each node only once.
    838  */
    839 void
    840 ieee80211_timeout_nodes(struct ieee80211com *ic)
    841 {
    842 	struct ieee80211_node *ni;
    843 	u_int gen = ic->ic_scangen++;		/* NB: ok 'cuz single-threaded*/
    844 
    845 restart:
    846 	IEEE80211_NODE_LOCK(ic);
    847 	TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    848 		if (ni->ni_scangen == gen)	/* previously handled */
    849 			continue;
    850 		ni->ni_scangen = gen;
    851 		if (++ni->ni_inact > ieee80211_inact_max) {
    852 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
    853 			    ("station %s timed out due to inactivity (%u secs)\n",
    854 			    ether_sprintf(ni->ni_macaddr),
    855 			    ni->ni_inact));
    856 			/*
    857 			 * Send a deauthenticate frame.
    858 			 *
    859 			 * Drop the node lock before sending the
    860 			 * deauthentication frame in case the driver takes
    861 			 * a lock, as this will result in a LOR between the
    862 			 * node lock and the driver lock.
    863 			 */
    864 			IEEE80211_NODE_UNLOCK(ic);
    865 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    866 				IEEE80211_SEND_MGMT(ic, ni,
    867 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
    868 				    IEEE80211_REASON_AUTH_EXPIRE);
    869 				ieee80211_node_leave(ic, ni);
    870 			} else
    871 				ieee80211_free_node(ic, ni);
    872 			ic->ic_stats.is_node_timeout++;
    873 			goto restart;
    874 		}
    875 	}
    876 	if (!TAILQ_EMPTY(&ic->ic_node))
    877 		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
    878 	IEEE80211_NODE_UNLOCK(ic);
    879 }
    880 
    881 void
    882 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
    883 {
    884 	struct ieee80211_node *ni;
    885 
    886 	IEEE80211_NODE_LOCK(ic);
    887 	TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
    888 		(*f)(arg, ni);
    889 	IEEE80211_NODE_UNLOCK(ic);
    890 }
    891 
    892 void
    893 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
    894 {
    895 	int newassoc;
    896 
    897 	if (ni->ni_associd == 0) {
    898 		u_int16_t aid;
    899 
    900 		/*
    901 		 * It would be clever to search the bitmap
    902 		 * more efficiently, but this will do for now.
    903 		 */
    904 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
    905 			if (!IEEE80211_AID_ISSET(aid,
    906 			    ic->ic_aid_bitmap))
    907 				break;
    908 		}
    909 		if (aid >= ic->ic_max_aid) {
    910 			IEEE80211_SEND_MGMT(ic, ni, resp,
    911 			    IEEE80211_REASON_ASSOC_TOOMANY);
    912 			ieee80211_node_leave(ic, ni);
    913 			return;
    914 		}
    915 		ni->ni_associd = aid | 0xc000;
    916 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
    917 		newassoc = 1;
    918 		/* XXX for 11g must turn off short slot time if long
    919 		   slot time sta associates */
    920 	} else
    921 		newassoc = 0;
    922 
    923 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
    924 		("station %s %s associated at aid %d\n",
    925 		ether_sprintf(ni->ni_macaddr),
    926 		(newassoc ? "newly" : "already"),
    927 		ni->ni_associd & ~0xc000));
    928 
    929 	/* give driver a chance to setup state like ni_txrate */
    930 	if (ic->ic_newassoc)
    931 		(*ic->ic_newassoc)(ic, ni, newassoc);
    932 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
    933 }
    934 
    935 /*
    936  * Handle bookkeeping for station deauthentication/disassociation
    937  * when operating as an ap.
    938  */
    939 void
    940 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
    941 {
    942 
    943 	IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP,
    944 		("not in ap mode, mode %u", ic->ic_opmode));
    945 	/*
    946 	 * If node wasn't previously associated all
    947 	 * we need to do is reclaim the reference.
    948 	 */
    949 	if (ni->ni_associd == 0)
    950 		goto done;
    951 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
    952 	ni->ni_associd = 0;
    953 
    954 done:
    955 	ieee80211_free_node(ic, ni);
    956 }
    957