Home | History | Annotate | Line # | Download | only in net80211
ieee80211_node.c revision 1.16
      1 /*	$NetBSD: ieee80211_node.c,v 1.16 2004/07/23 05:19:41 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * Alternatively, this software may be distributed under the terms of the
     19  * GNU General Public License ("GPL") version 2 as published by the Free
     20  * Software Foundation.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifdef __FreeBSD__
     36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.22 2004/04/05 04:15:55 sam Exp $");
     37 #else
     38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_node.c,v 1.16 2004/07/23 05:19:41 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 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(("ieee80211_next_scan: chan %d->%d\n",
    228 	    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
    229 	    ieee80211_chan2ieee(ic, chan)));
    230 	ic->ic_bss->ni_chan = chan;
    231 	ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    232 }
    233 
    234 void
    235 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
    236 {
    237 	struct ieee80211_node *ni;
    238 	struct ifnet *ifp = &ic->ic_if;
    239 
    240 	ni = ic->ic_bss;
    241 	if (ifp->if_flags & IFF_DEBUG)
    242 		if_printf(ifp, "creating ibss\n");
    243 	ic->ic_flags |= IEEE80211_F_SIBSS;
    244 	ni->ni_chan = chan;
    245 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
    246 	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
    247 	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
    248 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    249 		if ((ic->ic_flags & IEEE80211_F_DESBSSID) != 0)
    250 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
    251 		else
    252 			ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
    253 	}
    254 	ni->ni_esslen = ic->ic_des_esslen;
    255 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
    256 	ni->ni_rssi = 0;
    257 	ni->ni_rstamp = 0;
    258 	memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
    259 	ni->ni_intval = ic->ic_lintval;
    260 	ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
    261 	if (ic->ic_flags & IEEE80211_F_WEPON)
    262 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
    263 	if (ic->ic_phytype == IEEE80211_T_FH) {
    264 		ni->ni_fhdwell = 200;	/* XXX */
    265 		ni->ni_fhindex = 1;
    266 	}
    267 	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    268 }
    269 
    270 int
    271 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
    272 {
    273         u_int8_t rate;
    274         int fail;
    275 
    276 	fail = 0;
    277 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    278 		fail |= 0x01;
    279 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
    280 	    ni->ni_chan != ic->ic_des_chan)
    281 		fail |= 0x01;
    282 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    283 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    284 			fail |= 0x02;
    285 	} else {
    286 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    287 			fail |= 0x02;
    288 	}
    289 	if (ic->ic_flags & IEEE80211_F_WEPON) {
    290 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    291 			fail |= 0x04;
    292 	} else {
    293 		/* XXX does this mean privacy is supported or required? */
    294 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    295 			fail |= 0x04;
    296 	}
    297 	rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
    298 	if (rate & IEEE80211_RATE_BASIC)
    299 		fail |= 0x08;
    300 	if (ic->ic_des_esslen != 0 &&
    301 	    (ni->ni_esslen != ic->ic_des_esslen ||
    302 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
    303 		fail |= 0x10;
    304 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
    305 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
    306 		fail |= 0x20;
    307 #ifdef IEEE80211_DEBUG
    308 	if (ic->ic_if.if_flags & IFF_DEBUG) {
    309 		printf(" %c %s", fail ? '-' : '+',
    310 		    ether_sprintf(ni->ni_macaddr));
    311 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
    312 		    fail & 0x20 ? '!' : ' ');
    313 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
    314 			fail & 0x01 ? '!' : ' ');
    315 		printf(" %+4d", ni->ni_rssi);
    316 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
    317 		    fail & 0x08 ? '!' : ' ');
    318 		printf(" %4s%c",
    319 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
    320 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
    321 		    "????",
    322 		    fail & 0x02 ? '!' : ' ');
    323 		printf(" %3s%c ",
    324 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
    325 		    "wep" : "no",
    326 		    fail & 0x04 ? '!' : ' ');
    327 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
    328 		printf("%s\n", fail & 0x10 ? "!" : "");
    329 	}
    330 #endif
    331 	return fail;
    332 }
    333 
    334 /*
    335  * Complete a scan of potential channels.
    336  */
    337 void
    338 ieee80211_end_scan(struct ifnet *ifp)
    339 {
    340 	struct ieee80211com *ic = (void *)ifp;
    341 	struct ieee80211_node *ni, *nextbs, *selbs;
    342 	int i, fail;
    343 
    344 	ic->ic_flags &= ~IEEE80211_F_ASCAN;
    345 	ni = TAILQ_FIRST(&ic->ic_node);
    346 
    347 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    348 		/* XXX off stack? */
    349 		u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
    350 		/*
    351 		 * The passive scan to look for existing AP's completed,
    352 		 * select a channel to camp on.  Identify the channels
    353 		 * that already have one or more AP's and try to locate
    354 		 * an unnoccupied one.  If that fails, pick a random
    355 		 * channel from the active set.
    356 		 */
    357 		for (; ni != NULL; ni = nextbs) {
    358 			ieee80211_ref_node(ni);
    359 			nextbs = TAILQ_NEXT(ni, ni_list);
    360 			setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
    361 			ieee80211_free_node(ic, ni);
    362 		}
    363 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    364 			if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
    365 				break;
    366 		if (i == IEEE80211_CHAN_MAX) {
    367 			fail = arc4random() & 3;	/* random 0-3 */
    368 			for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    369 				if (isset(ic->ic_chan_active, i) && fail-- == 0)
    370 					break;
    371 		}
    372 		ieee80211_create_ibss(ic, &ic->ic_channels[i]);
    373 		return;
    374 	}
    375 	if (ni == NULL) {
    376 		IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
    377   notfound:
    378 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
    379 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
    380 		    ic->ic_des_esslen != 0) {
    381 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
    382 			return;
    383 		}
    384 		/*
    385 		 * Reset the list of channels to scan and start again.
    386 		 */
    387 		ieee80211_reset_scan(ifp);
    388 		ieee80211_next_scan(ifp);
    389 		return;
    390 	}
    391 	selbs = NULL;
    392 	if (ifp->if_flags & IFF_DEBUG)
    393 		if_printf(ifp, "\tmacaddr          bssid         chan  rssi rate flag  wep  essid\n");
    394 	for (; ni != NULL; ni = nextbs) {
    395 		ieee80211_ref_node(ni);
    396 		nextbs = TAILQ_NEXT(ni, ni_list);
    397 		if (ni->ni_fails) {
    398 			/*
    399 			 * The configuration of the access points may change
    400 			 * during my scan.  So delete the entry for the AP
    401 			 * and retry to associate if there is another beacon.
    402 			 */
    403 			if (ni->ni_fails++ > 2)
    404 				ieee80211_free_node(ic, ni);
    405 			continue;
    406 		}
    407 		if (ieee80211_match_bss(ic, ni) == 0) {
    408 			if (selbs == NULL)
    409 				selbs = ni;
    410 			else if (ni->ni_rssi > selbs->ni_rssi) {
    411 				ieee80211_unref_node(&selbs);
    412 				selbs = ni;
    413 			} else
    414 				ieee80211_unref_node(&ni);
    415 		} else {
    416 			ieee80211_unref_node(&ni);
    417 		}
    418 	}
    419 	if (selbs == NULL)
    420 		goto notfound;
    421 	(*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
    422 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    423 		ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
    424 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
    425 		if (ic->ic_bss->ni_rates.rs_nrates == 0) {
    426 			selbs->ni_fails++;
    427 			ieee80211_unref_node(&selbs);
    428 			goto notfound;
    429 		}
    430 		ieee80211_unref_node(&selbs);
    431 		/*
    432 		 * Discard scan set; the nodes have a refcnt of zero
    433 		 * and have not asked the driver to setup private
    434 		 * node state.  Let them be repopulated on demand either
    435 		 * through transmission (ieee80211_find_txnode) or receipt
    436 		 * of a probe response (to be added).
    437 		 */
    438 		ieee80211_free_allnodes(ic);
    439 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    440 	} else {
    441 		ieee80211_unref_node(&selbs);
    442 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
    443 	}
    444 }
    445 
    446 int
    447 ieee80211_get_rate(struct ieee80211com *ic)
    448 {
    449 	u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE];
    450 	int rate;
    451 
    452 	rates = &ic->ic_bss->ni_rates.rs_rates;
    453 
    454 	if (ic->ic_fixed_rate != -1)
    455 		rate = (*rates)[ic->ic_fixed_rate];
    456 	else if (ic->ic_state == IEEE80211_S_RUN)
    457 		rate = (*rates)[ic->ic_bss->ni_txrate];
    458 	else
    459 		rate = 0;
    460 
    461 	return rate & IEEE80211_RATE_VAL;
    462 }
    463 
    464 static struct ieee80211_node *
    465 ieee80211_node_alloc(struct ieee80211com *ic)
    466 {
    467 	struct ieee80211_node *ni;
    468 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
    469 		M_80211_NODE, M_NOWAIT | M_ZERO);
    470 	return ni;
    471 }
    472 
    473 static void
    474 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
    475 {
    476 	if (ni->ni_challenge != NULL) {
    477 		FREE(ni->ni_challenge, M_DEVBUF);
    478 		ni->ni_challenge = NULL;
    479 	}
    480 	FREE(ni, M_80211_NODE);
    481 }
    482 
    483 static void
    484 ieee80211_node_copy(struct ieee80211com *ic,
    485 	struct ieee80211_node *dst, const struct ieee80211_node *src)
    486 {
    487 	*dst = *src;
    488 	dst->ni_challenge = NULL;
    489 }
    490 
    491 static u_int8_t
    492 ieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
    493 {
    494 	return ni->ni_rssi;
    495 }
    496 
    497 static void
    498 ieee80211_setup_node(struct ieee80211com *ic,
    499 	struct ieee80211_node *ni, u_int8_t *macaddr)
    500 {
    501 	int hash;
    502 	ieee80211_node_critsec_decl(s);
    503 
    504 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
    505 	hash = IEEE80211_NODE_HASH(macaddr);
    506 	ni->ni_refcnt = 1;		/* mark referenced */
    507 	ieee80211_node_critsec_begin(ic, s);
    508 	TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
    509 	LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
    510 	/*
    511 	 * Note we don't enable the inactive timer when acting
    512 	 * as a station.  Nodes created in this mode represent
    513 	 * AP's identified while scanning.  If we time them out
    514 	 * then several things happen: we can't return the data
    515 	 * to users to show the list of AP's we encountered, and
    516 	 * more importantly, we'll incorrectly deauthenticate
    517 	 * ourself because the inactivity timer will kick us off.
    518 	 */
    519 	if (ic->ic_opmode != IEEE80211_M_STA)
    520 		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
    521 	ieee80211_node_critsec_end(ic, s);
    522 }
    523 
    524 struct ieee80211_node *
    525 ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
    526 {
    527 	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
    528 	if (ni != NULL)
    529 		ieee80211_setup_node(ic, ni, macaddr);
    530 	else
    531 		ic->ic_stats.is_rx_nodealloc++;
    532 	return ni;
    533 }
    534 
    535 struct ieee80211_node *
    536 ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
    537 {
    538 	struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
    539 	if (ni != NULL) {
    540 		ieee80211_setup_node(ic, ni, macaddr);
    541 		/*
    542 		 * Inherit from ic_bss.
    543 		 */
    544 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
    545 		ni->ni_chan = ic->ic_bss->ni_chan;
    546 	} else
    547 		ic->ic_stats.is_rx_nodealloc++;
    548 	return ni;
    549 }
    550 
    551 static struct ieee80211_node *
    552 _ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
    553 {
    554 	struct ieee80211_node *ni;
    555 	int hash;
    556 
    557 #ifdef __FreeBSD__
    558 	IEEE80211_NODE_LOCK_ASSERT(ic);
    559 #endif /* __FreeBSD__ */
    560 
    561 	hash = IEEE80211_NODE_HASH(macaddr);
    562 	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
    563 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
    564 			ieee80211_node_incref(ni); /* mark referenced */
    565 			return ni;
    566 		}
    567 	}
    568 	return NULL;
    569 }
    570 
    571 struct ieee80211_node *
    572 ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
    573 {
    574 	struct ieee80211_node *ni;
    575 	ieee80211_node_critsec_decl(s);
    576 
    577 	ieee80211_node_critsec_begin(ic, s);
    578 	ni = _ieee80211_find_node(ic, macaddr);
    579 	ieee80211_node_critsec_end(ic, s);
    580 	return ni;
    581 }
    582 
    583 /*
    584  * Return a reference to the appropriate node for sending
    585  * a data frame.  This handles node discovery in adhoc networks.
    586  */
    587 struct ieee80211_node *
    588 ieee80211_find_txnode(struct ieee80211com *ic, u_int8_t *macaddr)
    589 {
    590 	struct ieee80211_node *ni;
    591 	ieee80211_node_critsec_decl(s);
    592 
    593 	/*
    594 	 * The destination address should be in the node table
    595 	 * unless we are operating in station mode or this is a
    596 	 * multicast/broadcast frame.
    597 	 */
    598 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
    599 		return ic->ic_bss;
    600 
    601 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
    602 	ieee80211_node_critsec_begin(ic, s);
    603 	ni = _ieee80211_find_node(ic, macaddr);
    604 	ieee80211_node_critsec_end(ic, s);
    605 	if (ni == NULL &&
    606 	    (ic->ic_opmode == IEEE80211_M_IBSS ||
    607 	     ic->ic_opmode == IEEE80211_M_AHDEMO)) {
    608 		/*
    609 		 * Fake up a node; this handles node discovery in
    610 		 * adhoc mode.  Note that for the driver's benefit
    611 		 * we we treat this like an association so the driver
    612 		 * has an opportunity to setup it's private state.
    613 		 *
    614 		 * XXX need better way to handle this; issue probe
    615 		 *     request so we can deduce rate set, etc.
    616 		 */
    617 		ni = ieee80211_dup_bss(ic, macaddr);
    618 		if (ni != NULL) {
    619 			/* XXX no rate negotiation; just dup */
    620 			ni->ni_rates = ic->ic_bss->ni_rates;
    621 			if (ic->ic_newassoc)
    622 				(*ic->ic_newassoc)(ic, ni, 1);
    623 		}
    624 	}
    625 	return ni;
    626 }
    627 
    628 /*
    629  * For some types of packet and for some operating modes, it is
    630  * desirable to process a Rx packet using its sender's node-record
    631  * instead of the BSS record, when that is possible.
    632  *
    633  * - AP mode: it is desirable to keep a node-record for every
    634  *   authenticated/associated station *in the BSS*. For future use,
    635  *   we also track neighboring APs, since they might belong to the
    636  *   same ESSID.
    637  *
    638  * - IBSS mode: it is desirable to keep a node-record for every
    639  *   station *in the BSS*.
    640  *
    641  * - monitor mode: it is desirable to keep a node-record for every
    642  *   sender, regardless 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(("%s: faked-up node %p for %s\n", __func__, ni,
    733 	    ether_sprintf(wh->i_addr2)));
    734 
    735 	return ieee80211_ref_node(ni);
    736 }
    737 
    738 /*
    739  * Like find but search based on the channel too.
    740  */
    741 struct ieee80211_node *
    742 ieee80211_lookup_node(struct ieee80211com *ic,
    743 	u_int8_t *macaddr, struct ieee80211_channel *chan)
    744 {
    745 	struct ieee80211_node *ni;
    746 	int hash;
    747 	ieee80211_node_critsec_decl(s);
    748 
    749 	hash = IEEE80211_NODE_HASH(macaddr);
    750 	ieee80211_node_critsec_begin(ic, s);
    751 	LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
    752 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
    753 		    ni->ni_chan == chan) {
    754 			ieee80211_node_incref(ni);/* mark referenced */
    755 			break;
    756 		}
    757 	}
    758 	ieee80211_node_critsec_end(ic, s);
    759 	return ni;
    760 }
    761 
    762 static void
    763 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
    764 {
    765 	IASSERT(ni != ic->ic_bss, ("freeing bss node"));
    766 
    767 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
    768 	TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
    769 	LIST_REMOVE(ni, ni_hash);
    770 	if (!IF_IS_EMPTY(&ni->ni_savedq)) {
    771 		IF_PURGE(&ni->ni_savedq);
    772 		if (ic->ic_set_tim)
    773 			ic->ic_set_tim(ic, ni->ni_associd, 0);
    774 	}
    775 	if (TAILQ_EMPTY(&ic->ic_node))
    776 		ic->ic_inact_timer = 0;
    777 	(*ic->ic_node_free)(ic, ni);
    778 }
    779 
    780 void
    781 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
    782 {
    783 	ieee80211_node_critsec_decl(s);
    784 
    785 	IASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
    786 
    787 	if (ieee80211_node_decref(ni) == 0) {
    788 		ieee80211_node_critsec_begin(ic, s);
    789 		_ieee80211_free_node(ic, ni);
    790 		ieee80211_node_critsec_end(ic, s);
    791 	}
    792 }
    793 
    794 void
    795 ieee80211_free_allnodes(struct ieee80211com *ic)
    796 {
    797 	struct ieee80211_node *ni;
    798 	ieee80211_node_critsec_decl(s);
    799 
    800 	ieee80211_node_critsec_begin(ic, s);
    801 	while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
    802 		_ieee80211_free_node(ic, ni);
    803 	ieee80211_node_critsec_end(ic, s);
    804 }
    805 
    806 /*
    807  * Timeout inactive nodes.  Note that we cannot hold the node
    808  * lock while sending a frame as this would lead to a LOR.
    809  * Instead we use a generation number to mark nodes that we've
    810  * scanned and drop the lock and restart a scan if we have to
    811  * time out a node.  Since we are single-threaded by virtue of
    812  * controlling the inactivity timer we can be sure this will
    813  * process each node only once.
    814  */
    815 void
    816 ieee80211_timeout_nodes(struct ieee80211com *ic)
    817 {
    818 	struct ieee80211_node *ni;
    819 	ieee80211_node_critsec_decl(s);
    820 	u_int gen = ic->ic_scangen++;		/* NB: ok 'cuz single-threaded*/
    821 
    822 restart:
    823 	ieee80211_node_critsec_begin(ic, s);
    824 	TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
    825 		if (ni->ni_scangen == gen)	/* previously handled */
    826 			continue;
    827 		ni->ni_scangen = gen;
    828 		if (++ni->ni_inact > ieee80211_inact_max) {
    829 			IEEE80211_DPRINTF(("station %s timed out "
    830 			    "due to inactivity (%u secs)\n",
    831 			    ether_sprintf(ni->ni_macaddr),
    832 			    ni->ni_inact));
    833 			/*
    834 			 * Send a deauthenticate frame.
    835 			 *
    836 			 * Drop the node lock before sending the
    837 			 * deauthentication frame in case the driver takes
    838 			 * a lock, as this will result in a LOR between the
    839 			 * node lock and the driver lock.
    840 			 */
    841 			ieee80211_node_critsec_end(ic, s);
    842 			IEEE80211_SEND_MGMT(ic, ni,
    843 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
    844 			    IEEE80211_REASON_AUTH_EXPIRE);
    845 			ieee80211_free_node(ic, ni);
    846 			ic->ic_stats.is_node_timeout++;
    847 			goto restart;
    848 		}
    849 	}
    850 	if (!TAILQ_EMPTY(&ic->ic_node))
    851 		ic->ic_inact_timer = IEEE80211_INACT_WAIT;
    852 	ieee80211_node_critsec_end(ic, s);
    853 }
    854 
    855 void
    856 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
    857 {
    858 	struct ieee80211_node *ni;
    859 	ieee80211_node_critsec_decl(s);
    860 
    861 	ieee80211_node_critsec_begin(ic, s);
    862 	TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
    863 		(*f)(arg, ni);
    864 	ieee80211_node_critsec_end(ic, s);
    865 }
    866