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