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