Home | History | Annotate | Line # | Download | only in net80211
ieee80211_node.c revision 1.39
      1 /*	$NetBSD: ieee80211_node.c,v 1.39 2005/06/22 06:16:02 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002-2005 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.43 2005/02/10 16:59:04 sam Exp $");
     37 #endif
     38 #ifdef __NetBSD__
     39 __KERNEL_RCSID(0, "$NetBSD: ieee80211_node.c,v 1.39 2005/06/22 06:16:02 dyoung Exp $");
     40 #endif
     41 
     42 #include "opt_inet.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/malloc.h>
     48 #include <sys/kernel.h>
     49 
     50 #include <sys/socket.h>
     51 #include <sys/sockio.h>
     52 #include <sys/endian.h>
     53 #include <sys/errno.h>
     54 #include <sys/proc.h>
     55 #include <sys/sysctl.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_media.h>
     59 #include <net/if_arp.h>
     60 #include <net/if_ether.h>
     61 #include <net/if_llc.h>
     62 
     63 #include <net80211/ieee80211_netbsd.h>
     64 #include <net80211/ieee80211_var.h>
     65 
     66 #include <net/bpf.h>
     67 
     68 #ifdef INET
     69 #include <netinet/in.h>
     70 #include <net/if_ether.h>
     71 #endif
     72 
     73 static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
     74 static void node_cleanup(struct ieee80211_node *);
     75 static void node_free(struct ieee80211_node *);
     76 static u_int8_t node_getrssi(const struct ieee80211_node *);
     77 
     78 static void ieee80211_setup_node(struct ieee80211_node_table *,
     79 		struct ieee80211_node *, const u_int8_t *);
     80 static void _ieee80211_free_node(struct ieee80211_node *);
     81 static void ieee80211_free_allnodes(struct ieee80211_node_table *);
     82 
     83 static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
     84 static void ieee80211_timeout_stations(struct ieee80211_node_table *);
     85 
     86 static void ieee80211_set_tim(struct ieee80211com *,
     87 		struct ieee80211_node *, int set);
     88 
     89 static void ieee80211_node_table_init(struct ieee80211com *ic,
     90 	struct ieee80211_node_table *nt, const char *name, int inact,
     91 	void (*timeout)(struct ieee80211_node_table *));
     92 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
     93 
     94 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
     95 
     96 void
     97 ieee80211_node_attach(struct ieee80211com *ic)
     98 {
     99 	u_long sz;
    100 
    101 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
    102 		IEEE80211_INACT_INIT, ieee80211_timeout_stations);
    103 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
    104 		IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates);
    105 
    106 	ic->ic_node_alloc = node_alloc;
    107 	ic->ic_node_free = node_free;
    108 	ic->ic_node_cleanup = node_cleanup;
    109 	ic->ic_node_getrssi = node_getrssi;
    110 
    111 	/* default station inactivity timer setings */
    112 	ic->ic_inact_init = IEEE80211_INACT_INIT;
    113 	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
    114 	ic->ic_inact_run = IEEE80211_INACT_RUN;
    115 	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
    116 
    117 	/* XXX defer */
    118 	if (ic->ic_max_aid == 0)
    119 		ic->ic_max_aid = IEEE80211_AID_DEF;
    120 	else if (ic->ic_max_aid > IEEE80211_AID_MAX)
    121 		ic->ic_max_aid = IEEE80211_AID_MAX;
    122 	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
    123 		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
    124 		M_DEVBUF, M_NOWAIT | M_ZERO);
    125 	if (ic->ic_aid_bitmap == NULL) {
    126 		/* XXX no way to recover */
    127 		printf("%s: no memory for AID bitmap!\n", __func__);
    128 		ic->ic_max_aid = 0;
    129 	}
    130 
    131 	/* XXX defer until using hostap/ibss mode */
    132 	ic->ic_tim_len = sz = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
    133 	MALLOC(ic->ic_tim_bitmap, u_int8_t *, sz, M_DEVBUF, M_NOWAIT | M_ZERO);
    134 	if (ic->ic_tim_bitmap == NULL) {
    135 		/* XXX no way to recover */
    136 		printf("%s: no memory for TIM bitmap!\n", __func__);
    137 	}
    138 	ic->ic_set_tim = ieee80211_set_tim;	/* NB: driver should override */
    139 }
    140 
    141 void
    142 ieee80211_node_lateattach(struct ieee80211com *ic)
    143 {
    144 	struct ieee80211_node *ni;
    145 	struct ieee80211_rsnparms *rsn;
    146 
    147 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
    148 	IASSERT(ni != NULL, ("unable to setup inital BSS node"));
    149 	/*
    150 	 * Setup "global settings" in the bss node so that
    151 	 * each new station automatically inherits them.
    152 	 */
    153 	rsn = &ni->ni_rsn;
    154 	/* WEP, TKIP, and AES-CCM are always supported */
    155 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
    156 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
    157 	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
    158 	if (ic->ic_caps & IEEE80211_C_AES)
    159 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
    160 	if (ic->ic_caps & IEEE80211_C_CKIP)
    161 		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
    162 	/*
    163 	 * Default unicast cipher to WEP for 802.1x use.  If
    164 	 * WPA is enabled the management code will set these
    165 	 * values to reflect.
    166 	 */
    167 	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
    168 	rsn->rsn_ucastkeylen = 104 / NBBY;
    169 	/*
    170 	 * WPA says the multicast cipher is the lowest unicast
    171 	 * cipher supported.  But we skip WEP which would
    172 	 * otherwise be used based on this criteria.
    173 	 */
    174 	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
    175 	rsn->rsn_mcastkeylen = 128 / NBBY;
    176 
    177 	/*
    178 	 * We support both WPA-PSK and 802.1x; the one used
    179 	 * is determined by the authentication mode and the
    180 	 * setting of the PSK state.
    181 	 */
    182 	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
    183 	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
    184 
    185 	ic->ic_bss = ieee80211_ref_node(ni);		/* hold reference */
    186 	ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode);
    187 }
    188 
    189 void
    190 ieee80211_node_detach(struct ieee80211com *ic)
    191 {
    192 
    193 	if (ic->ic_bss != NULL) {
    194 		ieee80211_free_node(ic->ic_bss);
    195 		ic->ic_bss = NULL;
    196 	}
    197 	ieee80211_node_table_cleanup(&ic->ic_scan);
    198 	ieee80211_node_table_cleanup(&ic->ic_sta);
    199 	if (ic->ic_aid_bitmap != NULL) {
    200 		FREE(ic->ic_aid_bitmap, M_DEVBUF);
    201 		ic->ic_aid_bitmap = NULL;
    202 	}
    203 	if (ic->ic_tim_bitmap != NULL) {
    204 		FREE(ic->ic_tim_bitmap, M_DEVBUF);
    205 		ic->ic_tim_bitmap = NULL;
    206 	}
    207 }
    208 
    209 /*
    210  * Port authorize/unauthorize interfaces for use by an authenticator.
    211  */
    212 
    213 void
    214 ieee80211_node_authorize(struct ieee80211com *ic, struct ieee80211_node *ni)
    215 {
    216 	ni->ni_flags |= IEEE80211_NODE_AUTH;
    217 	ni->ni_inact_reload = ic->ic_inact_run;
    218 }
    219 
    220 void
    221 ieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni)
    222 {
    223 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
    224 }
    225 
    226 /*
    227  * Set/change the channel.  The rate set is also updated as
    228  * to insure a consistent view by drivers.
    229  */
    230 static __inline void
    231 ieee80211_set_chan(struct ieee80211com *ic,
    232 	struct ieee80211_node *ni, struct ieee80211_channel *chan)
    233 {
    234 	ni->ni_chan = chan;
    235 	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
    236 }
    237 
    238 /*
    239  * AP scanning support.
    240  */
    241 
    242 #ifdef IEEE80211_DEBUG
    243 static void
    244 dump_chanlist(const u_char chans[])
    245 {
    246 	const char *sep;
    247 	int i;
    248 
    249 	sep = " ";
    250 	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    251 		if (isset(chans, i)) {
    252 			printf("%s%u", sep, i);
    253 			sep = ", ";
    254 		}
    255 }
    256 #endif /* IEEE80211_DEBUG */
    257 
    258 /*
    259  * Initialize the channel set to scan based on the
    260  * of available channels and the current PHY mode.
    261  */
    262 static void
    263 ieee80211_reset_scan(struct ieee80211com *ic)
    264 {
    265 
    266 	/* XXX ic_des_chan should be handled with ic_chan_active */
    267 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
    268 		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
    269 		setbit(ic->ic_chan_scan,
    270 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
    271 	} else
    272 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
    273 			sizeof(ic->ic_chan_active));
    274 	/* NB: hack, setup so next_scan starts with the first channel */
    275 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
    276 		ieee80211_set_chan(ic, ic->ic_bss,
    277 			&ic->ic_channels[IEEE80211_CHAN_MAX]);
    278 #ifdef IEEE80211_DEBUG
    279 	if (ieee80211_msg_scan(ic)) {
    280 		printf("%s: scan set:", __func__);
    281 		dump_chanlist(ic->ic_chan_scan);
    282 		printf(" start chan %u\n",
    283 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
    284 	}
    285 #endif /* IEEE80211_DEBUG */
    286 }
    287 
    288 /*
    289  * Begin an active scan.
    290  */
    291 void
    292 ieee80211_begin_scan(struct ieee80211com *ic, int reset)
    293 {
    294 
    295 	ic->ic_scan.nt_scangen++;
    296 	/*
    297 	 * In all but hostap mode scanning starts off in
    298 	 * an active mode before switching to passive.
    299 	 */
    300 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
    301 		ic->ic_flags |= IEEE80211_F_ASCAN;
    302 		ic->ic_stats.is_scan_active++;
    303 	} else
    304 		ic->ic_stats.is_scan_passive++;
    305 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    306 		"begin %s scan in %s mode, scangen %u\n",
    307 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
    308 		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
    309 	/*
    310 	 * Clear scan state and flush any previously seen AP's.
    311 	 */
    312 	ieee80211_reset_scan(ic);
    313 	if (reset)
    314 		ieee80211_free_allnodes(&ic->ic_scan);
    315 
    316 	ic->ic_flags |= IEEE80211_F_SCAN;
    317 
    318 	/* Scan the next channel. */
    319 	ieee80211_next_scan(ic);
    320 }
    321 
    322 /*
    323  * Switch to the next channel marked for scanning.
    324  */
    325 int
    326 ieee80211_next_scan(struct ieee80211com *ic)
    327 {
    328 	struct ieee80211_channel *chan;
    329 
    330 	/*
    331 	 * Insure any previous mgt frame timeouts don't fire.
    332 	 * This assumes the driver does the right thing in
    333 	 * flushing anything queued in the driver and below.
    334 	 */
    335 	ic->ic_mgt_timer = 0;
    336 
    337 	chan = ic->ic_bss->ni_chan;
    338 	do {
    339 		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
    340 			chan = &ic->ic_channels[0];
    341 		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
    342 			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
    343 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    344 			    "%s: chan %d->%d\n", __func__,
    345 			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
    346 			    ieee80211_chan2ieee(ic, chan));
    347 			ieee80211_set_chan(ic, ic->ic_bss, chan);
    348 #ifdef notyet
    349 			/* XXX driver state change */
    350 			/*
    351 			 * Scan next channel. If doing an active scan
    352 			 * and the channel is not marked passive-only
    353 			 * then send a probe request.  Otherwise just
    354 			 * listen for beacons on the channel.
    355 			 */
    356 			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
    357 			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
    358 				IEEE80211_SEND_MGMT(ic, ni,
    359 				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
    360 			}
    361 #else
    362 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    363 #endif
    364 			return 1;
    365 		}
    366 	} while (chan != ic->ic_bss->ni_chan);
    367 	ieee80211_end_scan(ic);
    368 	return 0;
    369 }
    370 
    371 static __inline void
    372 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
    373 {
    374 	/* propagate useful state */
    375 	nbss->ni_authmode = obss->ni_authmode;
    376 	nbss->ni_txpower = obss->ni_txpower;
    377 	nbss->ni_vlan = obss->ni_vlan;
    378 	nbss->ni_rsn = obss->ni_rsn;
    379 	/* XXX statistics? */
    380 }
    381 
    382 void
    383 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
    384 {
    385 	struct ieee80211_node_table *nt;
    386 	struct ieee80211_node *ni;
    387 
    388 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    389 		"%s: creating ibss\n", __func__);
    390 
    391 	/*
    392 	 * Create the station/neighbor table.  Note that for adhoc
    393 	 * mode we make the initial inactivity timer longer since
    394 	 * we create nodes only through discovery and they typically
    395 	 * are long-lived associations.
    396 	 */
    397 	nt = &ic->ic_sta;
    398 	IEEE80211_NODE_LOCK(nt);
    399 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    400 		nt->nt_name = "station";
    401 		nt->nt_inact_init = ic->ic_inact_init;
    402 	} else {
    403 		nt->nt_name = "neighbor";
    404 		nt->nt_inact_init = ic->ic_inact_run;
    405 	}
    406 	IEEE80211_NODE_UNLOCK(nt);
    407 
    408 	ni = ieee80211_alloc_node(nt, ic->ic_myaddr);
    409 	if (ni == NULL) {
    410 		/* XXX recovery? */
    411 		return;
    412 	}
    413 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    414 	    (ic->ic_flags & IEEE80211_F_DESBSSID) != 0)
    415 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
    416 	else
    417 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
    418 	ni->ni_esslen = ic->ic_des_esslen;
    419 	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
    420 	copy_bss(ni, ic->ic_bss);
    421 	ni->ni_intval = ic->ic_lintval;
    422 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
    423 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
    424 	if (ic->ic_phytype == IEEE80211_T_FH) {
    425 		ni->ni_fhdwell = 200;	/* XXX */
    426 		ni->ni_fhindex = 1;
    427 	}
    428 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    429 		ic->ic_flags |= IEEE80211_F_SIBSS;
    430 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
    431 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
    432 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
    433 		else
    434 		ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
    435 	}
    436 	/*
    437 	 * Fix the channel and related attributes.
    438 	 */
    439 	ieee80211_set_chan(ic, ni, chan);
    440 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
    441 	/*
    442 	 * Do mode-specific rate setup.
    443 	 */
    444 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
    445 		/*
    446 		 * Use a mixed 11b/11g rate set.
    447 		 */
    448 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
    449 	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
    450 		/*
    451 		 * Force pure 11b rate set.
    452 		 */
    453 		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
    454 	}
    455 
    456 	(void) ieee80211_sta_join(ic, ieee80211_ref_node(ni));
    457 }
    458 
    459 void
    460 ieee80211_reset_bss(struct ieee80211com *ic)
    461 {
    462 	struct ieee80211_node *ni, *obss;
    463 
    464 	ieee80211_node_table_reset(&ic->ic_scan);
    465 	ieee80211_node_table_reset(&ic->ic_sta);
    466 
    467 	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
    468 	IASSERT(ni != NULL, ("unable to setup inital BSS node"));
    469 	obss = ic->ic_bss;
    470 	ic->ic_bss = ieee80211_ref_node(ni);
    471 	if (obss != NULL) {
    472 		copy_bss(ni, obss);
    473 		ni->ni_intval = ic->ic_lintval;
    474 		ieee80211_free_node(obss);
    475 	}
    476 }
    477 
    478 static int
    479 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
    480 {
    481         u_int8_t rate;
    482         int fail;
    483 
    484 	fail = 0;
    485 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    486 		fail |= 0x01;
    487 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
    488 	    ni->ni_chan != ic->ic_des_chan)
    489 		fail |= 0x01;
    490 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    491 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    492 			fail |= 0x02;
    493 	} else {
    494 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    495 			fail |= 0x02;
    496 	}
    497 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
    498 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    499 			fail |= 0x04;
    500 	} else {
    501 		/* XXX does this mean privacy is supported or required? */
    502 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    503 			fail |= 0x04;
    504 	}
    505 	rate = ieee80211_fix_rate(ic, ni,
    506 			IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
    507 	if (rate & IEEE80211_RATE_BASIC)
    508 		fail |= 0x08;
    509 	if (ic->ic_des_esslen != 0 &&
    510 	    (ni->ni_esslen != ic->ic_des_esslen ||
    511 	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
    512 		fail |= 0x10;
    513 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
    514 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
    515 		fail |= 0x20;
    516 #ifdef IEEE80211_DEBUG
    517 	if (ieee80211_msg_scan(ic)) {
    518 		printf(" %c %s", fail ? '-' : '+',
    519 		    ether_sprintf(ni->ni_macaddr));
    520 		printf(" %s%c", ether_sprintf(ni->ni_bssid),
    521 		    fail & 0x20 ? '!' : ' ');
    522 		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
    523 			fail & 0x01 ? '!' : ' ');
    524 		printf(" %+4d", ni->ni_rssi);
    525 		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
    526 		    fail & 0x08 ? '!' : ' ');
    527 		printf(" %4s%c",
    528 		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
    529 		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
    530 		    "????",
    531 		    fail & 0x02 ? '!' : ' ');
    532 		printf(" %3s%c ",
    533 		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
    534 		    "wep" : "no",
    535 		    fail & 0x04 ? '!' : ' ');
    536 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
    537 		printf("%s\n", fail & 0x10 ? "!" : "");
    538 	}
    539 #endif
    540 	return fail;
    541 }
    542 
    543 static __inline u_int8_t
    544 maxrate(const struct ieee80211_node *ni)
    545 {
    546 	const struct ieee80211_rateset *rs = &ni->ni_rates;
    547 	/* NB: assumes rate set is sorted (happens on frame receive) */
    548 	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
    549 }
    550 
    551 /*
    552  * Compare the capabilities of two nodes and decide which is
    553  * more desirable (return >0 if a is considered better).  Note
    554  * that we assume compatibility/usability has already been checked
    555  * so we don't need to (e.g. validate whether privacy is supported).
    556  * Used to select the best scan candidate for association in a BSS.
    557  */
    558 static int
    559 ieee80211_node_compare(struct ieee80211com *ic,
    560 		       const struct ieee80211_node *a,
    561 		       const struct ieee80211_node *b)
    562 {
    563 	u_int8_t maxa, maxb;
    564 	u_int8_t rssia, rssib;
    565 
    566 	/* privacy support preferred */
    567 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
    568 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    569 		return 1;
    570 	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
    571 	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
    572 		return -1;
    573 
    574 	rssia = ic->ic_node_getrssi(a);
    575 	rssib = ic->ic_node_getrssi(b);
    576 	if (abs(rssib - rssia) < 5) {
    577 		/* best/max rate preferred if signal level close enough XXX */
    578 		maxa = maxrate(a);
    579 		maxb = maxrate(b);
    580 		if (maxa != maxb)
    581 			return maxa - maxb;
    582 		/* XXX use freq for channel preference */
    583 		/* for now just prefer 5Ghz band to all other bands */
    584 		if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
    585 		   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
    586 			return 1;
    587 		if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
    588 		     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
    589 			return -1;
    590 	}
    591 	/* all things being equal, use signal level */
    592 	return rssia - rssib;
    593 }
    594 
    595 /*
    596  * Mark an ongoing scan stopped.
    597  */
    598 void
    599 ieee80211_cancel_scan(struct ieee80211com *ic)
    600 {
    601 
    602 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "%s: end %s scan\n",
    603 		__func__,
    604 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
    605 
    606 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
    607 }
    608 
    609 /*
    610  * Complete a scan of potential channels.
    611  */
    612 void
    613 ieee80211_end_scan(struct ieee80211com *ic)
    614 {
    615 	struct ieee80211_node_table *nt = &ic->ic_scan;
    616 	struct ieee80211_node *ni, *selbs;
    617 
    618 	ieee80211_cancel_scan(ic);
    619 	ieee80211_notify_scan_done(ic);
    620 
    621 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    622 		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
    623 		int i, bestchan;
    624 		u_int8_t rssi;
    625 
    626 		/*
    627 		 * The passive scan to look for existing AP's completed,
    628 		 * select a channel to camp on.  Identify the channels
    629 		 * that already have one or more AP's and try to locate
    630 		 * an unoccupied one.  If that fails, pick a channel that
    631 		 * looks to be quietest.
    632 		 */
    633 		memset(maxrssi, 0, sizeof(maxrssi));
    634 		IEEE80211_NODE_LOCK(nt);
    635 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
    636 			rssi = ic->ic_node_getrssi(ni);
    637 			i = ieee80211_chan2ieee(ic, ni->ni_chan);
    638 			if (rssi > maxrssi[i])
    639 				maxrssi[i] = rssi;
    640 		}
    641 		IEEE80211_NODE_UNLOCK(nt);
    642 		/* XXX select channel more intelligently */
    643 		bestchan = -1;
    644 		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
    645 			if (isset(ic->ic_chan_active, i)) {
    646 				/*
    647 				 * If the channel is unoccupied the max rssi
    648 				 * should be zero; just take it.  Otherwise
    649 				 * track the channel with the lowest rssi and
    650 				 * use that when all channels appear occupied.
    651 				 */
    652 				if (maxrssi[i] == 0) {
    653 					bestchan = i;
    654 					break;
    655 				}
    656 				if (bestchan == -1 ||
    657 				    maxrssi[i] < maxrssi[bestchan])
    658 					bestchan = i;
    659 			}
    660 		if (bestchan != -1) {
    661 			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
    662 			return;
    663 		}
    664 		/* no suitable channel, should not happen */
    665 	}
    666 
    667 	/*
    668 	 * When manually sequencing the state machine; scan just once
    669 	 * regardless of whether we have a candidate or not.  The
    670 	 * controlling application is expected to setup state and
    671 	 * initiate an association.
    672 	 */
    673 	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
    674 		return;
    675 	/*
    676 	 * Automatic sequencing; look for a candidate and
    677 	 * if found join the network.
    678 	 */
    679 	/* NB: unlocked read should be ok */
    680 	if (TAILQ_FIRST(&nt->nt_node) == NULL) {
    681 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    682 			"%s: no scan candidate\n", __func__);
    683   notfound:
    684 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
    685 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
    686 		    ic->ic_des_esslen != 0) {
    687 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
    688 			return;
    689 		}
    690 		/*
    691 		 * Reset the list of channels to scan and start again.
    692 		 */
    693 		ieee80211_reset_scan(ic);
    694 		ic->ic_flags |= IEEE80211_F_SCAN;
    695 		ieee80211_next_scan(ic);
    696 		return;
    697 	}
    698 	selbs = NULL;
    699 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
    700 	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
    701 	IEEE80211_NODE_LOCK(nt);
    702 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
    703 		if (ni->ni_fails) {
    704 			/*
    705 			 * The configuration of the access points may change
    706 			 * during my scan.  So delete the entry for the AP
    707 			 * and retry to associate if there is another beacon.
    708 			 */
    709 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    710 				"%s: skip scan candidate %s, fails %u\n",
    711 				__func__, ether_sprintf(ni->ni_macaddr),
    712 				ni->ni_fails);
    713 			ni->ni_fails++;
    714 #if 0
    715 			if (ni->ni_fails++ > 2)
    716 				ieee80211_free_node(ni);
    717 #endif
    718 			continue;
    719 		}
    720 		if (ieee80211_match_bss(ic, ni) == 0) {
    721 			if (selbs == NULL)
    722 				selbs = ni;
    723 			else if (ieee80211_node_compare(ic, ni, selbs) > 0)
    724 				selbs = ni;
    725 		}
    726 	}
    727 	if (selbs != NULL)		/* NB: grab ref while dropping lock */
    728 		(void) ieee80211_ref_node(selbs);
    729 	IEEE80211_NODE_UNLOCK(nt);
    730 	if (selbs == NULL)
    731 		goto notfound;
    732 	if (!ieee80211_sta_join(ic, selbs)) {
    733 		ieee80211_free_node(selbs);
    734 		goto notfound;
    735 	}
    736 }
    737 
    738 /*
    739  * Handle 802.11 ad hoc network merge.  The
    740  * convention, set by the Wireless Ethernet Compatibility Alliance
    741  * (WECA), is that an 802.11 station will change its BSSID to match
    742  * the "oldest" 802.11 ad hoc network, on the same channel, that
    743  * has the station's desired SSID.  The "oldest" 802.11 network
    744  * sends beacons with the greatest TSF timestamp.
    745  *
    746  * The caller is assumed to validate TSF's before attempting a merge.
    747  *
    748  * Return !0 if the BSSID changed, 0 otherwise.
    749  */
    750 int
    751 ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni)
    752 {
    753 
    754 	if (ni == ic->ic_bss ||
    755 	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
    756 		/* unchanged, nothing to do */
    757 		return 0;
    758 	}
    759 	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
    760 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
    761 		    "%s: merge failed, capabilities mismatch\n", __func__);
    762 		ic->ic_stats.is_ibss_capmismatch++;
    763 		return 0;
    764 	}
    765 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
    766 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
    767 		ether_sprintf(ni->ni_bssid),
    768 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
    769 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
    770 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
    771 	);
    772 	return ieee80211_sta_join(ic, ieee80211_ref_node(ni));
    773 }
    774 
    775 /*
    776  * Join the specified IBSS/BSS network.  The node is assumed to
    777  * be passed in with a held reference.
    778  */
    779 int
    780 ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
    781 {
    782 	struct ieee80211_node *obss;
    783 
    784 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
    785 		struct ieee80211_node_table *nt;
    786 		/*
    787 		 * Delete unusable rates; we've already checked
    788 		 * that the negotiated rate set is acceptable.
    789 		 */
    790 		ieee80211_fix_rate(ic, selbs, IEEE80211_F_DODEL);
    791 		/*
    792 		 * Fillin the neighbor table; it will already
    793 		 * exist if we are simply switching mastership.
    794 		 * XXX ic_sta always setup so this is unnecessary?
    795 		 */
    796 		nt = &ic->ic_sta;
    797 		IEEE80211_NODE_LOCK(nt);
    798 		nt->nt_name = "neighbor";
    799 		nt->nt_inact_init = ic->ic_inact_run;
    800 		IEEE80211_NODE_UNLOCK(nt);
    801 	}
    802 
    803 	/*
    804 	 * Committed to selbs, setup state.
    805 	 */
    806 	obss = ic->ic_bss;
    807 	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
    808 	if (obss != NULL)
    809 		ieee80211_free_node(obss);
    810 	/*
    811 	 * Set the erp state (mostly the slot time) to deal with
    812 	 * the auto-select case; this should be redundant if the
    813 	 * mode is locked.
    814 	 */
    815 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
    816 	ieee80211_reset_erp(ic);
    817 	ieee80211_wme_initparams(ic);
    818 
    819 	if (ic->ic_opmode == IEEE80211_M_STA)
    820 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
    821 	else
    822 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    823 	return 1;
    824 }
    825 
    826 /*
    827  * Leave the specified IBSS/BSS network.  The node is assumed to
    828  * be passed in with a held reference.
    829  */
    830 void
    831 ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
    832 {
    833 	ic->ic_node_cleanup(ni);
    834 	ieee80211_notify_node_leave(ic, ni);
    835 }
    836 
    837 int
    838 ieee80211_get_rate(struct ieee80211com *ic)
    839 {
    840 	u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE];
    841 	int rate;
    842 
    843 	rates = &ic->ic_bss->ni_rates.rs_rates;
    844 
    845 	if (ic->ic_fixed_rate != -1)
    846 		rate = (*rates)[ic->ic_fixed_rate];
    847 	else if (ic->ic_state == IEEE80211_S_RUN)
    848 		rate = (*rates)[ic->ic_bss->ni_txrate];
    849 	else
    850 		rate = 0;
    851 
    852 	return rate & IEEE80211_RATE_VAL;
    853 }
    854 
    855 static struct ieee80211_node *
    856 node_alloc(struct ieee80211_node_table *nt)
    857 {
    858 	struct ieee80211_node *ni;
    859 
    860 	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
    861 		M_80211_NODE, M_NOWAIT | M_ZERO);
    862 	return ni;
    863 }
    864 
    865 /*
    866  * Reclaim any resources in a node and reset any critical
    867  * state.  Typically nodes are free'd immediately after,
    868  * but in some cases the storage may be reused so we need
    869  * to insure consistent state (should probably fix that).
    870  */
    871 static void
    872 node_cleanup(struct ieee80211_node *ni)
    873 {
    874 #define	N(a)	(sizeof(a)/sizeof(a[0]))
    875 	struct ieee80211com *ic = ni->ni_ic;
    876 	int i, qlen;
    877 
    878 	/* NB: preserve ni_table */
    879 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
    880 		ic->ic_ps_sta--;
    881 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
    882 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
    883 		    "[%s] power save mode off, %u sta's in ps mode\n",
    884 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
    885 	}
    886 
    887 	/*
    888 	 * Drain power save queue and, if needed, clear TIM.
    889 	 */
    890 	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
    891 	if (qlen != 0 && ic->ic_set_tim != NULL)
    892 		ic->ic_set_tim(ic, ni, 0);
    893 
    894 	ni->ni_associd = 0;
    895 	if (ni->ni_challenge != NULL) {
    896 		FREE(ni->ni_challenge, M_DEVBUF);
    897 		ni->ni_challenge = NULL;
    898 	}
    899 	/*
    900 	 * Preserve SSID, WPA, and WME ie's so the bss node is
    901 	 * reusable during a re-auth/re-assoc state transition.
    902 	 * If we remove these data they will not be recreated
    903 	 * because they come from a probe-response or beacon frame
    904 	 * which cannot be expected prior to the association-response.
    905 	 * This should not be an issue when operating in other modes
    906 	 * as stations leaving always go through a full state transition
    907 	 * which will rebuild this state.
    908 	 *
    909 	 * XXX does this leave us open to inheriting old state?
    910 	 */
    911 	for (i = 0; i < N(ni->ni_rxfrag); i++)
    912 		if (ni->ni_rxfrag[i] != NULL) {
    913 			m_freem(ni->ni_rxfrag[i]);
    914 			ni->ni_rxfrag[i] = NULL;
    915 		}
    916 	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
    917 #undef N
    918 }
    919 
    920 static void
    921 node_free(struct ieee80211_node *ni)
    922 {
    923 	struct ieee80211com *ic = ni->ni_ic;
    924 
    925 	ic->ic_node_cleanup(ni);
    926 	if (ni->ni_wpa_ie != NULL)
    927 		FREE(ni->ni_wpa_ie, M_DEVBUF);
    928 	if (ni->ni_wme_ie != NULL)
    929 		FREE(ni->ni_wme_ie, M_DEVBUF);
    930 	IEEE80211_NODE_SAVEQ_DESTROY(ni);
    931 	FREE(ni, M_80211_NODE);
    932 }
    933 
    934 static u_int8_t
    935 node_getrssi(const struct ieee80211_node *ni)
    936 {
    937 	return ni->ni_rssi;
    938 }
    939 
    940 static void
    941 ieee80211_setup_node(struct ieee80211_node_table *nt,
    942 	struct ieee80211_node *ni, const u_int8_t *macaddr)
    943 {
    944 	struct ieee80211com *ic = nt->nt_ic;
    945 	int hash;
    946 
    947 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
    948 		"%s %p<%s> in %s table\n", __func__, ni,
    949 		ether_sprintf(macaddr), nt->nt_name);
    950 
    951 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
    952 	hash = IEEE80211_NODE_HASH(macaddr);
    953 	ieee80211_node_initref(ni);		/* mark referenced */
    954 	ni->ni_chan = IEEE80211_CHAN_ANYC;
    955 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
    956 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
    957 	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
    958 	ni->ni_inact_reload = nt->nt_inact_init;
    959 	ni->ni_inact = ni->ni_inact_reload;
    960 	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
    961 
    962 	IEEE80211_NODE_LOCK(nt);
    963 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
    964 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
    965 	ni->ni_table = nt;
    966 	ni->ni_ic = ic;
    967 	IEEE80211_NODE_UNLOCK(nt);
    968 }
    969 
    970 struct ieee80211_node *
    971 ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
    972 {
    973 	struct ieee80211com *ic = nt->nt_ic;
    974 	struct ieee80211_node *ni;
    975 
    976 	ni = ic->ic_node_alloc(nt);
    977 	if (ni != NULL)
    978 		ieee80211_setup_node(nt, ni, macaddr);
    979 	else
    980 		ic->ic_stats.is_rx_nodealloc++;
    981 	return ni;
    982 }
    983 
    984 struct ieee80211_node *
    985 ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
    986 {
    987 	struct ieee80211com *ic = nt->nt_ic;
    988 	struct ieee80211_node *ni;
    989 
    990 	ni = ic->ic_node_alloc(nt);
    991 	if (ni != NULL) {
    992 		ieee80211_setup_node(nt, ni, macaddr);
    993 		/*
    994 		 * Inherit from ic_bss.
    995 		 */
    996 		ni->ni_authmode = ic->ic_bss->ni_authmode;
    997 		ni->ni_txpower = ic->ic_bss->ni_txpower;
    998 		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
    999 		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
   1000 		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
   1001 		ni->ni_rsn = ic->ic_bss->ni_rsn;
   1002 	} else
   1003 		ic->ic_stats.is_rx_nodealloc++;
   1004 	return ni;
   1005 }
   1006 
   1007 static struct ieee80211_node *
   1008 #ifdef IEEE80211_DEBUG_REFCNT
   1009 _ieee80211_find_node_debug(struct ieee80211_node_table *nt,
   1010 	const u_int8_t *macaddr, const char *func, int line)
   1011 #else
   1012 _ieee80211_find_node(struct ieee80211_node_table *nt,
   1013 	const u_int8_t *macaddr)
   1014 #endif
   1015 {
   1016 	struct ieee80211_node *ni;
   1017 	int hash;
   1018 
   1019 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1020 
   1021 	hash = IEEE80211_NODE_HASH(macaddr);
   1022 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1023 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
   1024 			ieee80211_ref_node(ni);	/* mark referenced */
   1025 #ifdef IEEE80211_DEBUG_REFCNT
   1026 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
   1027 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1028 			    func, line,
   1029 			    ni, ether_sprintf(ni->ni_macaddr),
   1030 			    ieee80211_node_refcnt(ni));
   1031 #endif
   1032 			return ni;
   1033 		}
   1034 	}
   1035 	return NULL;
   1036 }
   1037 #ifdef IEEE80211_DEBUG_REFCNT
   1038 #define	_ieee80211_find_node(nt, mac) \
   1039 	_ieee80211_find_node_debug(nt, mac, func, line)
   1040 #endif
   1041 
   1042 struct ieee80211_node *
   1043 #ifdef IEEE80211_DEBUG_REFCNT
   1044 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
   1045 	const u_int8_t *macaddr, const char *func, int line)
   1046 #else
   1047 ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
   1048 #endif
   1049 {
   1050 	struct ieee80211_node *ni;
   1051 
   1052 	IEEE80211_NODE_LOCK(nt);
   1053 	ni = _ieee80211_find_node(nt, macaddr);
   1054 	IEEE80211_NODE_UNLOCK(nt);
   1055 	return ni;
   1056 }
   1057 
   1058 /*
   1059  * Fake up a node; this handles node discovery in adhoc mode.
   1060  * Note that for the driver's benefit we we treat this like
   1061  * an association so the driver has an opportunity to setup
   1062  * it's private state.
   1063  */
   1064 struct ieee80211_node *
   1065 ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
   1066 	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
   1067 {
   1068 	struct ieee80211com *ic = nt->nt_ic;
   1069 	struct ieee80211_node *ni;
   1070 
   1071 	ni = ieee80211_dup_bss(nt, macaddr);
   1072 	if (ni != NULL) {
   1073 		/* XXX no rate negotiation; just dup */
   1074 		ni->ni_rates = ic->ic_bss->ni_rates;
   1075 		if (ic->ic_newassoc != NULL)
   1076 			ic->ic_newassoc(ic, ni, 1);
   1077 		/* XXX not right for 802.1x/WPA */
   1078 		ieee80211_node_authorize(ic, ni);
   1079 	}
   1080 	return ni;
   1081 }
   1082 
   1083 /*
   1084  * Locate the node for sender, track state, and then pass the
   1085  * (referenced) node up to the 802.11 layer for its use.  We
   1086  * are required to pass some node so we fall back to ic_bss
   1087  * when this frame is from an unknown sender.  The 802.11 layer
   1088  * knows this means the sender wasn't in the node table and
   1089  * acts accordingly.
   1090  */
   1091 struct ieee80211_node *
   1092 #ifdef IEEE80211_DEBUG_REFCNT
   1093 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
   1094 	const struct ieee80211_frame_min *wh, const char *func, int line)
   1095 #else
   1096 ieee80211_find_rxnode(struct ieee80211com *ic,
   1097 	const struct ieee80211_frame_min *wh)
   1098 #endif
   1099 {
   1100 #define	IS_CTL(wh) \
   1101 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
   1102 #define	IS_PSPOLL(wh) \
   1103 	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
   1104 	struct ieee80211_node_table *nt;
   1105 	struct ieee80211_node *ni;
   1106 
   1107 	/* XXX may want scanned nodes in the neighbor table for adhoc */
   1108 	if (ic->ic_opmode == IEEE80211_M_STA ||
   1109 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
   1110 	    (ic->ic_flags & IEEE80211_F_SCAN))
   1111 		nt = &ic->ic_scan;
   1112 	else
   1113 		nt = &ic->ic_sta;
   1114 	/* XXX check ic_bss first in station mode */
   1115 	/* XXX 4-address frames? */
   1116 	IEEE80211_NODE_LOCK(nt);
   1117 	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
   1118 		ni = _ieee80211_find_node(nt, wh->i_addr1);
   1119 	else
   1120 		ni = _ieee80211_find_node(nt, wh->i_addr2);
   1121 	IEEE80211_NODE_UNLOCK(nt);
   1122 
   1123 	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
   1124 #undef IS_PSPOLL
   1125 #undef IS_CTL
   1126 }
   1127 
   1128 /*
   1129  * Return a reference to the appropriate node for sending
   1130  * a data frame.  This handles node discovery in adhoc networks.
   1131  */
   1132 struct ieee80211_node *
   1133 #ifdef IEEE80211_DEBUG_REFCNT
   1134 ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
   1135 	const char *func, int line)
   1136 #else
   1137 ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
   1138 #endif
   1139 {
   1140 	struct ieee80211_node_table *nt = &ic->ic_sta;
   1141 	struct ieee80211_node *ni;
   1142 
   1143 	/*
   1144 	 * The destination address should be in the node table
   1145 	 * unless we are operating in station mode or this is a
   1146 	 * multicast/broadcast frame.
   1147 	 */
   1148 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
   1149 		return ieee80211_ref_node(ic->ic_bss);
   1150 
   1151 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
   1152 	IEEE80211_NODE_LOCK(nt);
   1153 	ni = _ieee80211_find_node(nt, macaddr);
   1154 	IEEE80211_NODE_UNLOCK(nt);
   1155 
   1156 	if (ni == NULL) {
   1157 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
   1158 		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
   1159 			/*
   1160 			 * In adhoc mode cons up a node for the destination.
   1161 			 * Note that we need an additional reference for the
   1162 			 * caller to be consistent with _ieee80211_find_node.
   1163 			 */
   1164 			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
   1165 			if (ni != NULL)
   1166 				(void) ieee80211_ref_node(ni);
   1167 		} else {
   1168 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
   1169 				"[%s] no node, discard frame (%s)\n",
   1170 				ether_sprintf(macaddr), __func__);
   1171 			ic->ic_stats.is_tx_nonode++;
   1172 		}
   1173 	}
   1174 	return ni;
   1175 }
   1176 
   1177 /*
   1178  * Like find but search based on the channel too.
   1179  */
   1180 struct ieee80211_node *
   1181 #ifdef IEEE80211_DEBUG_REFCNT
   1182 ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
   1183 	const u_int8_t *macaddr, struct ieee80211_channel *chan,
   1184 	const char *func, int line)
   1185 #else
   1186 ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
   1187 	const u_int8_t *macaddr, struct ieee80211_channel *chan)
   1188 #endif
   1189 {
   1190 	struct ieee80211_node *ni;
   1191 	int hash;
   1192 
   1193 	hash = IEEE80211_NODE_HASH(macaddr);
   1194 	IEEE80211_NODE_LOCK(nt);
   1195 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1196 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
   1197 		    ni->ni_chan == chan) {
   1198 			ieee80211_ref_node(ni);		/* mark referenced */
   1199 			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
   1200 #ifdef IEEE80211_DEBUG_REFCNT
   1201 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1202 			    func, line,
   1203 #else
   1204 			    "%s %p<%s> refcnt %d\n", __func__,
   1205 #endif
   1206 			    ni, ether_sprintf(ni->ni_macaddr),
   1207 			    ieee80211_node_refcnt(ni));
   1208 			break;
   1209 		}
   1210 	}
   1211 	IEEE80211_NODE_UNLOCK(nt);
   1212 	return ni;
   1213 }
   1214 
   1215 struct ieee80211_node *
   1216 ieee80211_refine_node_for_beacon(struct ieee80211com *ic,
   1217 	struct ieee80211_node *ni0, struct ieee80211_channel *chan,
   1218 	const u_int8_t *ssid)
   1219 {
   1220 	struct ieee80211_node_table *nt = ni0->ni_table;
   1221 	struct ieee80211_node *best, *ni;
   1222 	int best_score = 0, score;
   1223 
   1224 	best = ni0;
   1225 	if (ssid[1] == 0 || best->ni_esslen == 0)
   1226 		best_score = 1;
   1227 	else if (ssid[1] == best->ni_esslen &&
   1228 		 memcmp(ssid + 2, best->ni_essid, ssid[1]) == 0)
   1229 		best_score = 2;
   1230 	else
   1231 		best_score = 0;
   1232 
   1233 	IEEE80211_NODE_LOCK(nt);
   1234 	for (ni = LIST_NEXT(ni0, ni_hash); ni != NULL;
   1235 	     ni = LIST_NEXT(ni, ni_hash)) {
   1236 		if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, best->ni_macaddr) ||
   1237 		    ni->ni_ic != best->ni_ic || ni->ni_chan != chan)
   1238 			continue;
   1239 
   1240 		if (ssid[1] == 0 || ni->ni_esslen == 0)
   1241 			score = 1;
   1242 		else if (ssid[1] == ni->ni_esslen &&
   1243 			 memcmp(ssid + 2, ni->ni_essid, ssid[1]) == 0)
   1244 			score = 2;
   1245 		else
   1246 			continue;
   1247 
   1248 		if (score > best_score) {
   1249 			ieee80211_ref_node(ni);
   1250 			ieee80211_unref_node(&best);
   1251 			best = ni;
   1252 			best_score = score;
   1253 		}
   1254 	}
   1255 	IEEE80211_NODE_UNLOCK(nt);
   1256 	return best;
   1257 }
   1258 
   1259 /*
   1260  * Like find but search based on the ssid too.
   1261  */
   1262 struct ieee80211_node *
   1263 #ifdef IEEE80211_DEBUG_REFCNT
   1264 ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
   1265 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
   1266 	const char *func, int line)
   1267 #else
   1268 ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
   1269 	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
   1270 #endif
   1271 {
   1272 	struct ieee80211com *ic = nt->nt_ic;
   1273 	struct ieee80211_node *ni;
   1274 	int hash;
   1275 
   1276 	hash = IEEE80211_NODE_HASH(macaddr);
   1277 	IEEE80211_NODE_LOCK(nt);
   1278 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1279 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
   1280 		    ni->ni_esslen == ic->ic_des_esslen &&
   1281 		    memcmp(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen) == 0) {
   1282 			ieee80211_ref_node(ni);		/* mark referenced */
   1283 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1284 #ifdef IEEE80211_DEBUG_REFCNT
   1285 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1286 			    func, line,
   1287 #else
   1288 			    "%s %p<%s> refcnt %d\n", __func__,
   1289 #endif
   1290 			     ni, ether_sprintf(ni->ni_macaddr),
   1291 			     ieee80211_node_refcnt(ni));
   1292 			break;
   1293 		}
   1294 	}
   1295 	IEEE80211_NODE_UNLOCK(nt);
   1296 	return ni;
   1297 }
   1298 
   1299 static void
   1300 _ieee80211_free_node(struct ieee80211_node *ni)
   1301 {
   1302 	struct ieee80211com *ic = ni->ni_ic;
   1303 	struct ieee80211_node_table *nt = ni->ni_table;
   1304 
   1305 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1306 		"%s %p<%s> in %s table\n", __func__, ni,
   1307 		ether_sprintf(ni->ni_macaddr),
   1308 		nt != NULL ? nt->nt_name : "<gone>");
   1309 
   1310 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
   1311 	if (nt != NULL) {
   1312 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
   1313 		LIST_REMOVE(ni, ni_hash);
   1314 	}
   1315 	ic->ic_node_free(ni);
   1316 }
   1317 
   1318 void
   1319 #ifdef IEEE80211_DEBUG_REFCNT
   1320 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
   1321 #else
   1322 ieee80211_free_node(struct ieee80211_node *ni)
   1323 #endif
   1324 {
   1325 	struct ieee80211_node_table *nt = ni->ni_table;
   1326 
   1327 #ifdef IEEE80211_DEBUG_REFCNT
   1328 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
   1329 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
   1330 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
   1331 #endif
   1332 	if (ieee80211_node_dectestref(ni)) {
   1333 		/*
   1334 		 * Beware; if the node is marked gone then it's already
   1335 		 * been removed from the table and we cannot assume the
   1336 		 * table still exists.  Regardless, there's no need to lock
   1337 		 * the table.
   1338 		 */
   1339 		if (ni->ni_table != NULL) {
   1340 			IEEE80211_NODE_LOCK(nt);
   1341 			_ieee80211_free_node(ni);
   1342 			IEEE80211_NODE_UNLOCK(nt);
   1343 		} else
   1344 			_ieee80211_free_node(ni);
   1345 	}
   1346 }
   1347 
   1348 /*
   1349  * Reclaim a node.  If this is the last reference count then
   1350  * do the normal free work.  Otherwise remove it from the node
   1351  * table and mark it gone by clearing the back-reference.
   1352  */
   1353 static void
   1354 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   1355 {
   1356 
   1357 	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
   1358 		"%s: remove %p<%s> from %s table, refcnt %d\n",
   1359 		__func__, ni, ether_sprintf(ni->ni_macaddr),
   1360 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
   1361 	if (!ieee80211_node_dectestref(ni)) {
   1362 		/*
   1363 		 * Other references are present, just remove the
   1364 		 * node from the table so it cannot be found.  When
   1365 		 * the references are dropped storage will be
   1366 		 * reclaimed.
   1367 		 */
   1368 		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
   1369 		LIST_REMOVE(ni, ni_hash);
   1370 		ni->ni_table = NULL;		/* clear reference */
   1371 	} else
   1372 		_ieee80211_free_node(ni);
   1373 }
   1374 
   1375 static void
   1376 ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
   1377 {
   1378 	struct ieee80211com *ic = nt->nt_ic;
   1379 	struct ieee80211_node *ni;
   1380 
   1381 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1382 		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
   1383 
   1384 	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
   1385 		if (ni->ni_associd != 0) {
   1386 			if (ic->ic_auth->ia_node_leave != NULL)
   1387 				ic->ic_auth->ia_node_leave(ic, ni);
   1388 			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
   1389 		}
   1390 		node_reclaim(nt, ni);
   1391 	}
   1392 	ieee80211_reset_erp(ic);
   1393 }
   1394 
   1395 static void
   1396 ieee80211_free_allnodes(struct ieee80211_node_table *nt)
   1397 {
   1398 
   1399 	IEEE80211_NODE_LOCK(nt);
   1400 	ieee80211_free_allnodes_locked(nt);
   1401 	IEEE80211_NODE_UNLOCK(nt);
   1402 }
   1403 
   1404 /*
   1405  * Timeout entries in the scan cache.
   1406  */
   1407 static void
   1408 ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
   1409 {
   1410 	struct ieee80211com *ic = nt->nt_ic;
   1411 	struct ieee80211_node *ni, *tni;
   1412 
   1413 	IEEE80211_NODE_LOCK(nt);
   1414 	ni = ic->ic_bss;
   1415 	/* XXX belongs elsewhere */
   1416 	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
   1417 		m_freem(ni->ni_rxfrag[0]);
   1418 		ni->ni_rxfrag[0] = NULL;
   1419 	}
   1420 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
   1421 		if (ni->ni_inact && --ni->ni_inact == 0) {
   1422 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1423 			    "[%s] scan candidate purged from cache "
   1424 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
   1425 			    ieee80211_node_refcnt(ni));
   1426 			node_reclaim(nt, ni);
   1427 		}
   1428 	}
   1429 	IEEE80211_NODE_UNLOCK(nt);
   1430 
   1431 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
   1432 }
   1433 
   1434 /*
   1435  * Timeout inactive stations and do related housekeeping.
   1436  * Note that we cannot hold the node lock while sending a
   1437  * frame as this would lead to a LOR.  Instead we use a
   1438  * generation number to mark nodes that we've scanned and
   1439  * drop the lock and restart a scan if we have to time out
   1440  * a node.  Since we are single-threaded by virtue of
   1441  * controlling the inactivity timer we can be sure this will
   1442  * process each node only once.
   1443  */
   1444 static void
   1445 ieee80211_timeout_stations(struct ieee80211_node_table *nt)
   1446 {
   1447 	struct ieee80211com *ic = nt->nt_ic;
   1448 	struct ieee80211_node *ni;
   1449 	u_int gen;
   1450 
   1451 	IEEE80211_SCAN_LOCK(nt);
   1452 	gen = nt->nt_scangen++;
   1453 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1454 		"%s: %s scangen %u\n", __func__, nt->nt_name, gen);
   1455 restart:
   1456 	IEEE80211_NODE_LOCK(nt);
   1457 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1458 		if (ni->ni_scangen == gen)	/* previously handled */
   1459 			continue;
   1460 		ni->ni_scangen = gen;
   1461 		/*
   1462 		 * Free fragment if not needed anymore
   1463 		 * (last fragment older than 1s).
   1464 		 * XXX doesn't belong here
   1465 		 */
   1466 		if (ni->ni_rxfrag[0] != NULL &&
   1467 		    ticks > ni->ni_rxfragstamp + hz) {
   1468 			m_freem(ni->ni_rxfrag[0]);
   1469 			ni->ni_rxfrag[0] = NULL;
   1470 		}
   1471 		/*
   1472 		 * Special case ourself; we may be idle for extended periods
   1473 		 * of time and regardless reclaiming our state is wrong.
   1474 		 */
   1475 		if (ni == ic->ic_bss)
   1476 			continue;
   1477 		ni->ni_inact--;
   1478 		if (ni->ni_associd != 0) {
   1479 			/*
   1480 			 * Age frames on the power save queue. The
   1481 			 * aging interval is 4 times the listen
   1482 			 * interval specified by the station.  This
   1483 			 * number is factored into the age calculations
   1484 			 * when the frame is placed on the queue.  We
   1485 			 * store ages as time differences we can check
   1486 			 * and/or adjust only the head of the list.
   1487 			 */
   1488 			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
   1489 				struct mbuf *m;
   1490 				int discard = 0;
   1491 
   1492 				IEEE80211_NODE_SAVEQ_LOCK(ni);
   1493 				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
   1494 				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
   1495 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
   1496 					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
   1497 					m_freem(m);
   1498 					discard++;
   1499 				}
   1500 				if (m != NULL)
   1501 					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
   1502 				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
   1503 
   1504 				if (discard != 0) {
   1505 					IEEE80211_DPRINTF(ic,
   1506 					    IEEE80211_MSG_POWER,
   1507 					    "[%s] discard %u frames for age\n",
   1508 					    ether_sprintf(ni->ni_macaddr),
   1509 					    discard);
   1510 					IEEE80211_NODE_STAT_ADD(ni,
   1511 						ps_discard, discard);
   1512 					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
   1513 						ic->ic_set_tim(ic, ni, 0);
   1514 				}
   1515 			}
   1516 			/*
   1517 			 * Probe the station before time it out.  We
   1518 			 * send a null data frame which may not be
   1519 			 * universally supported by drivers (need it
   1520 			 * for ps-poll support so it should be...).
   1521 			 */
   1522 			if (0 < ni->ni_inact &&
   1523 			    ni->ni_inact <= ic->ic_inact_probe) {
   1524 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1525 				    "[%s] probe station due to inactivity\n",
   1526 				    ether_sprintf(ni->ni_macaddr));
   1527 				IEEE80211_NODE_UNLOCK(nt);
   1528 				ieee80211_send_nulldata(ic, ni);
   1529 				/* XXX stat? */
   1530 				goto restart;
   1531 			}
   1532 		}
   1533 		if (ni->ni_inact <= 0) {
   1534 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1535 			    "[%s] station timed out due to inactivity "
   1536 			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
   1537 			    ieee80211_node_refcnt(ni));
   1538 			/*
   1539 			 * Send a deauthenticate frame and drop the station.
   1540 			 * This is somewhat complicated due to reference counts
   1541 			 * and locking.  At this point a station will typically
   1542 			 * have a reference count of 1.  ieee80211_node_leave
   1543 			 * will do a "free" of the node which will drop the
   1544 			 * reference count.  But in the meantime a reference
   1545 			 * wil be held by the deauth frame.  The actual reclaim
   1546 			 * of the node will happen either after the tx is
   1547 			 * completed or by ieee80211_node_leave.
   1548 			 *
   1549 			 * Separately we must drop the node lock before sending
   1550 			 * in case the driver takes a lock, as this will result
   1551 			 * in  LOR between the node lock and the driver lock.
   1552 			 */
   1553 			IEEE80211_NODE_UNLOCK(nt);
   1554 			if (ni->ni_associd != 0) {
   1555 				IEEE80211_SEND_MGMT(ic, ni,
   1556 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
   1557 				    IEEE80211_REASON_AUTH_EXPIRE);
   1558 			}
   1559 			ieee80211_node_leave(ic, ni);
   1560 			ic->ic_stats.is_node_timeout++;
   1561 			goto restart;
   1562 		}
   1563 	}
   1564 	IEEE80211_NODE_UNLOCK(nt);
   1565 
   1566 	IEEE80211_SCAN_UNLOCK(nt);
   1567 
   1568 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
   1569 }
   1570 
   1571 void
   1572 ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
   1573 {
   1574 	struct ieee80211_node *ni;
   1575 	u_int gen;
   1576 
   1577 	IEEE80211_SCAN_LOCK(nt);
   1578 	gen = nt->nt_scangen++;
   1579 restart:
   1580 	IEEE80211_NODE_LOCK(nt);
   1581 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1582 		if (ni->ni_scangen != gen) {
   1583 			ni->ni_scangen = gen;
   1584 			(void) ieee80211_ref_node(ni);
   1585 			IEEE80211_NODE_UNLOCK(nt);
   1586 			(*f)(arg, ni);
   1587 			ieee80211_free_node(ni);
   1588 			goto restart;
   1589 		}
   1590 	}
   1591 	IEEE80211_NODE_UNLOCK(nt);
   1592 
   1593 	IEEE80211_SCAN_UNLOCK(nt);
   1594 }
   1595 
   1596 void
   1597 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   1598 {
   1599 	printf("0x%p: mac %s refcnt %d\n", ni,
   1600 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
   1601 	printf("\tscangen %u authmode %u flags 0x%x\n",
   1602 		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
   1603 	printf("\tassocid 0x%x txpower %u vlan %u\n",
   1604 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
   1605 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
   1606 		ni->ni_txseqs[0],
   1607 		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
   1608 		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
   1609 		ni->ni_rxfragstamp);
   1610 	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
   1611 		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
   1612 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
   1613 		ether_sprintf(ni->ni_bssid),
   1614 		ni->ni_esslen, ni->ni_essid,
   1615 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
   1616 	printf("\tfails %u inact %u txrate %u\n",
   1617 		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
   1618 }
   1619 
   1620 void
   1621 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
   1622 {
   1623 	ieee80211_iterate_nodes(nt,
   1624 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
   1625 }
   1626 
   1627 /*
   1628  * Handle a station joining an 11g network.
   1629  */
   1630 static void
   1631 ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
   1632 {
   1633 
   1634 	/*
   1635 	 * Station isn't capable of short slot time.  Bump
   1636 	 * the count of long slot time stations and disable
   1637 	 * use of short slot time.  Note that the actual switch
   1638 	 * over to long slot time use may not occur until the
   1639 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
   1640 	 */
   1641 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
   1642 		ic->ic_longslotsta++;
   1643 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1644 		    "[%s] station needs long slot time, count %d\n",
   1645 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
   1646 		/* XXX vap's w/ conflicting needs won't work */
   1647 		ieee80211_set_shortslottime(ic, 0);
   1648 	}
   1649 	/*
   1650 	 * If the new station is not an ERP station
   1651 	 * then bump the counter and enable protection
   1652 	 * if configured.
   1653 	 */
   1654 	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
   1655 		ic->ic_nonerpsta++;
   1656 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1657 		    "[%s] station is !ERP, %d non-ERP stations associated\n",
   1658 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
   1659 		/*
   1660 		 * If protection is configured, enable it.
   1661 		 */
   1662 		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
   1663 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1664 			    "%s: enable use of protection\n", __func__);
   1665 			ic->ic_flags |= IEEE80211_F_USEPROT;
   1666 		}
   1667 		/*
   1668 		 * If station does not support short preamble
   1669 		 * then we must enable use of Barker preamble.
   1670 		 */
   1671 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
   1672 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1673 			    "[%s] station needs long preamble\n",
   1674 			    ether_sprintf(ni->ni_macaddr));
   1675 			ic->ic_flags |= IEEE80211_F_USEBARKER;
   1676 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
   1677 		}
   1678 	} else
   1679 		ni->ni_flags |= IEEE80211_NODE_ERP;
   1680 }
   1681 
   1682 void
   1683 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
   1684 {
   1685 	int newassoc;
   1686 
   1687 	if (ni->ni_associd == 0) {
   1688 		u_int16_t aid;
   1689 
   1690 		/*
   1691 		 * It would be good to search the bitmap
   1692 		 * more efficiently, but this will do for now.
   1693 		 */
   1694 		for (aid = 1; aid < ic->ic_max_aid; aid++) {
   1695 			if (!IEEE80211_AID_ISSET(aid,
   1696 			    ic->ic_aid_bitmap))
   1697 				break;
   1698 		}
   1699 		if (aid >= ic->ic_max_aid) {
   1700 			IEEE80211_SEND_MGMT(ic, ni, resp,
   1701 			    IEEE80211_REASON_ASSOC_TOOMANY);
   1702 			ieee80211_node_leave(ic, ni);
   1703 			return;
   1704 		}
   1705 		ni->ni_associd = aid | 0xc000;
   1706 		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
   1707 		ic->ic_sta_assoc++;
   1708 		newassoc = 1;
   1709 		if (ic->ic_curmode == IEEE80211_MODE_11G ||
   1710 		    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
   1711 			ieee80211_node_join_11g(ic, ni);
   1712 	} else
   1713 		newassoc = 0;
   1714 
   1715 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
   1716 	    "[%s] station %sassociated at aid %d: %s preamble, %s slot time%s%s\n",
   1717 	    ether_sprintf(ni->ni_macaddr), newassoc ? "" : "re",
   1718 	    IEEE80211_NODE_AID(ni),
   1719 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
   1720 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
   1721 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
   1722 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
   1723 	);
   1724 
   1725 	/* give driver a chance to setup state like ni_txrate */
   1726 	if (ic->ic_newassoc != NULL)
   1727 		ic->ic_newassoc(ic, ni, newassoc);
   1728 	ni->ni_inact_reload = ic->ic_inact_auth;
   1729 	ni->ni_inact = ni->ni_inact_reload;
   1730 	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
   1731 	/* tell the authenticator about new station */
   1732 	if (ic->ic_auth->ia_node_join != NULL)
   1733 		ic->ic_auth->ia_node_join(ic, ni);
   1734 	ieee80211_notify_node_join(ic, ni, newassoc);
   1735 }
   1736 
   1737 /*
   1738  * Handle a station leaving an 11g network.
   1739  */
   1740 static void
   1741 ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
   1742 {
   1743 
   1744 	IASSERT(ic->ic_curmode == IEEE80211_MODE_11G ||
   1745 		ic->ic_curmode == IEEE80211_MODE_TURBO_G,
   1746 		("not in 11g, curmode %x", ic->ic_curmode));
   1747 
   1748 	/*
   1749 	 * If a long slot station do the slot time bookkeeping.
   1750 	 */
   1751 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
   1752 		IASSERT(ic->ic_longslotsta > 0,
   1753 		    ("bogus long slot station count %d", ic->ic_longslotsta));
   1754 		ic->ic_longslotsta--;
   1755 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1756 		    "[%s] long slot time station leaves, count now %d\n",
   1757 		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
   1758 		if (ic->ic_longslotsta == 0) {
   1759 			/*
   1760 			 * Re-enable use of short slot time if supported
   1761 			 * and not operating in IBSS mode (per spec).
   1762 			 */
   1763 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
   1764 			    ic->ic_opmode != IEEE80211_M_IBSS) {
   1765 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1766 				    "%s: re-enable use of short slot time\n",
   1767 				    __func__);
   1768 				ieee80211_set_shortslottime(ic, 1);
   1769 			}
   1770 		}
   1771 	}
   1772 	/*
   1773 	 * If a non-ERP station do the protection-related bookkeeping.
   1774 	 */
   1775 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
   1776 		IASSERT(ic->ic_nonerpsta > 0,
   1777 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
   1778 		ic->ic_nonerpsta--;
   1779 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1780 		    "[%s] non-ERP station leaves, count now %d\n",
   1781 		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
   1782 		if (ic->ic_nonerpsta == 0) {
   1783 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1784 				"%s: disable use of protection\n", __func__);
   1785 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
   1786 			/* XXX verify mode? */
   1787 			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
   1788 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
   1789 				    "%s: re-enable use of short preamble\n",
   1790 				    __func__);
   1791 				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
   1792 				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
   1793 			}
   1794 		}
   1795 	}
   1796 }
   1797 
   1798 /*
   1799  * Handle bookkeeping for station deauthentication/disassociation
   1800  * when operating as an ap.
   1801  */
   1802 void
   1803 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
   1804 {
   1805 	struct ieee80211_node_table *nt = ni->ni_table;
   1806 
   1807 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
   1808 	    "[%s] station with aid %d leaves\n",
   1809 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
   1810 
   1811 	IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
   1812 		ic->ic_opmode == IEEE80211_M_IBSS ||
   1813 		ic->ic_opmode == IEEE80211_M_AHDEMO,
   1814 		("unexpected operating mode %u", ic->ic_opmode));
   1815 	/*
   1816 	 * If node wasn't previously associated all
   1817 	 * we need to do is reclaim the reference.
   1818 	 */
   1819 	/* XXX ibss mode bypasses 11g and notification */
   1820 	if (ni->ni_associd == 0)
   1821 		goto done;
   1822 	/*
   1823 	 * Tell the authenticator the station is leaving.
   1824 	 * Note that we must do this before yanking the
   1825 	 * association id as the authenticator uses the
   1826 	 * associd to locate it's state block.
   1827 	 */
   1828 	if (ic->ic_auth->ia_node_leave != NULL)
   1829 		ic->ic_auth->ia_node_leave(ic, ni);
   1830 	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
   1831 	ni->ni_associd = 0;
   1832 	ic->ic_sta_assoc--;
   1833 
   1834 	if (ic->ic_curmode == IEEE80211_MODE_11G ||
   1835 	    ic->ic_curmode == IEEE80211_MODE_TURBO_G)
   1836 		ieee80211_node_leave_11g(ic, ni);
   1837 	/*
   1838 	 * Cleanup station state.  In particular clear various
   1839 	 * state that might otherwise be reused if the node
   1840 	 * is reused before the reference count goes to zero
   1841 	 * (and memory is reclaimed).
   1842 	 */
   1843 	ieee80211_sta_leave(ic, ni);
   1844 done:
   1845 	/*
   1846 	 * Remove the node from any table it's recorded in and
   1847 	 * drop the caller's reference.  Removal from the table
   1848 	 * is important to insure the node is not reprocessed
   1849 	 * for inactivity.
   1850 	 */
   1851 	if (nt != NULL) {
   1852 		IEEE80211_NODE_LOCK(nt);
   1853 		node_reclaim(nt, ni);
   1854 		IEEE80211_NODE_UNLOCK(nt);
   1855 	} else
   1856 		ieee80211_free_node(ni);
   1857 }
   1858 
   1859 u_int8_t
   1860 ieee80211_getrssi(struct ieee80211com *ic)
   1861 {
   1862 #define	NZ(x)	((x) == 0 ? 1 : (x))
   1863 	struct ieee80211_node_table *nt = &ic->ic_sta;
   1864 	u_int32_t rssi_samples, rssi_total;
   1865 	struct ieee80211_node *ni;
   1866 
   1867 	rssi_total = 0;
   1868 	rssi_samples = 0;
   1869 	switch (ic->ic_opmode) {
   1870 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
   1871 		/* XXX locking */
   1872 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
   1873 			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
   1874 				rssi_samples++;
   1875 				rssi_total += ic->ic_node_getrssi(ni);
   1876 			}
   1877 		break;
   1878 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
   1879 		/* XXX locking */
   1880 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1881 			rssi_samples++;
   1882 			rssi_total += ic->ic_node_getrssi(ni);
   1883 		}
   1884 		break;
   1885 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
   1886 		/* XXX locking */
   1887 		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
   1888 			if (IEEE80211_AID(ni->ni_associd) != 0) {
   1889 				rssi_samples++;
   1890 				rssi_total += ic->ic_node_getrssi(ni);
   1891 			}
   1892 		break;
   1893 	case IEEE80211_M_MONITOR:	/* XXX */
   1894 	case IEEE80211_M_STA:		/* use stats from associated ap */
   1895 	default:
   1896 		if (ic->ic_bss != NULL)
   1897 			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
   1898 		rssi_samples = 1;
   1899 		break;
   1900 	}
   1901 	return rssi_total / NZ(rssi_samples);
   1902 #undef NZ
   1903 }
   1904 
   1905 /*
   1906  * Indicate whether there are frames queued for a station in power-save mode.
   1907  */
   1908 static void
   1909 ieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
   1910 {
   1911 	u_int16_t aid;
   1912 
   1913 	IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
   1914 		ic->ic_opmode == IEEE80211_M_IBSS,
   1915 		("operating mode %u", ic->ic_opmode));
   1916 
   1917 	aid = IEEE80211_AID(ni->ni_associd);
   1918 	IASSERT(aid < ic->ic_max_aid,
   1919 		("bogus aid %u, max %u", aid, ic->ic_max_aid));
   1920 
   1921 	IEEE80211_BEACON_LOCK(ic);
   1922 	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
   1923 		if (set) {
   1924 			setbit(ic->ic_tim_bitmap, aid);
   1925 			ic->ic_ps_pending++;
   1926 		} else {
   1927 			clrbit(ic->ic_tim_bitmap, aid);
   1928 			ic->ic_ps_pending--;
   1929 		}
   1930 		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
   1931 	}
   1932 	IEEE80211_BEACON_UNLOCK(ic);
   1933 }
   1934 
   1935 /*
   1936  * Node table support.
   1937  */
   1938 
   1939 static void
   1940 ieee80211_node_table_init(struct ieee80211com *ic,
   1941 	struct ieee80211_node_table *nt,
   1942 	const char *name, int inact,
   1943 	void (*timeout)(struct ieee80211_node_table *))
   1944 {
   1945 
   1946 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
   1947 		"%s %s table, inact %u\n", __func__, name, inact);
   1948 
   1949 	nt->nt_ic = ic;
   1950 	/* XXX need unit */
   1951 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
   1952 	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
   1953 	TAILQ_INIT(&nt->nt_node);
   1954 	nt->nt_name = name;
   1955 	nt->nt_scangen = 1;
   1956 	nt->nt_inact_init = inact;
   1957 	nt->nt_timeout = timeout;
   1958 }
   1959 
   1960 void
   1961 ieee80211_node_table_reset(struct ieee80211_node_table *nt)
   1962 {
   1963 
   1964 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
   1965 		"%s %s table\n", __func__, nt->nt_name);
   1966 
   1967 	IEEE80211_NODE_LOCK(nt);
   1968 	nt->nt_inact_timer = 0;
   1969 	ieee80211_free_allnodes_locked(nt);
   1970 	IEEE80211_NODE_UNLOCK(nt);
   1971 }
   1972 
   1973 static void
   1974 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
   1975 {
   1976 
   1977 	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
   1978 		"%s %s table\n", __func__, nt->nt_name);
   1979 
   1980 	ieee80211_free_allnodes_locked(nt);
   1981 	IEEE80211_SCAN_LOCK_DESTROY(nt);
   1982 	IEEE80211_NODE_LOCK_DESTROY(nt);
   1983 }
   1984