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