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