Home | History | Annotate | Line # | Download | only in net80211
ieee80211_node.c revision 1.75.4.4
      1 /*	$NetBSD: ieee80211_node.c,v 1.75.4.4 2018/08/03 19:47:25 phil Exp $ */
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  *
      6  * Copyright (c) 2001 Atsushi Onoe
      7  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #if __FreeBSD__
     33 __FBSDID("$FreeBSD$");
     34 #endif
     35 
     36 #include "opt_wlan.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/mbuf.h>
     41 #include <sys/malloc.h>
     42 #include <sys/kernel.h>
     43 #ifdef __NetBSD__
     44 #include <sys/sbuf.h>
     45 #endif
     46 
     47 #include <sys/socket.h>
     48 
     49 #include <net/if.h>
     50 #if __FreeBSD__
     51 #include <net/if_var.h>
     52 #endif
     53 #include <net/if_media.h>
     54 #if __FreeBSD__
     55 #include <net/ethernet.h>
     56 #endif
     57 #ifdef __NetBSD__
     58 #include <net/if_ether.h>
     59 #include <net/route.h>
     60 #endif
     61 
     62 #include <net80211/ieee80211_var.h>
     63 #include <net80211/ieee80211_input.h>
     64 #ifdef IEEE80211_SUPPORT_SUPERG
     65 #include <net80211/ieee80211_superg.h>
     66 #endif
     67 #ifdef IEEE80211_SUPPORT_TDMA
     68 #include <net80211/ieee80211_tdma.h>
     69 #endif
     70 #include <net80211/ieee80211_wds.h>
     71 #include <net80211/ieee80211_mesh.h>
     72 #include <net80211/ieee80211_ratectl.h>
     73 #include <net80211/ieee80211_vht.h>
     74 
     75 #include <net/bpf.h>
     76 
     77 #ifdef __NetBSD__
     78 #undef  KASSERT
     79 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     80 #endif
     81 
     82 /*
     83  * IEEE80211_NODE_HASHSIZE must be a power of 2.
     84  */
     85 CTASSERT((IEEE80211_NODE_HASHSIZE & (IEEE80211_NODE_HASHSIZE-1)) == 0);
     86 
     87 /*
     88  * Association id's are managed with a bit vector.
     89  */
     90 #define	IEEE80211_AID_SET(_vap, b) \
     91 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] |= \
     92 		(1 << (IEEE80211_AID(b) % 32)))
     93 #define	IEEE80211_AID_CLR(_vap, b) \
     94 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] &= \
     95 		~(1 << (IEEE80211_AID(b) % 32)))
     96 #define	IEEE80211_AID_ISSET(_vap, b) \
     97 	((_vap)->iv_aid_bitmap[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
     98 
     99 static int ieee80211_sta_join1(struct ieee80211_node *);
    100 
    101 static struct ieee80211_node *node_alloc(struct ieee80211vap *,
    102 	const uint8_t [IEEE80211_ADDR_LEN]);
    103 static void node_cleanup(struct ieee80211_node *);
    104 static void node_free(struct ieee80211_node *);
    105 static void node_age(struct ieee80211_node *);
    106 static int8_t node_getrssi(const struct ieee80211_node *);
    107 static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
    108 static void node_getmimoinfo(const struct ieee80211_node *,
    109 	struct ieee80211_mimo_info *);
    110 
    111 static void _ieee80211_free_node(struct ieee80211_node *);
    112 
    113 static void node_reclaim(struct ieee80211_node_table *nt,
    114 	struct ieee80211_node *ni);
    115 static void ieee80211_node_table_init(struct ieee80211com *ic,
    116 	struct ieee80211_node_table *nt, const char *name,
    117 	int inact, int keymaxix);
    118 static void ieee80211_node_table_reset(struct ieee80211_node_table *,
    119 	struct ieee80211vap *);
    120 static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
    121 static void ieee80211_erp_timeout(struct ieee80211com *);
    122 
    123 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
    124 MALLOC_DEFINE(M_80211_NODE_IE, "80211nodeie", "802.11 node ie");
    125 
    126 void
    127 ieee80211_node_attach(struct ieee80211com *ic)
    128 {
    129 	/* XXX really want maxlen enforced per-sta */
    130 	ieee80211_ageq_init(&ic->ic_stageq, ic->ic_max_keyix * 8,
    131 	    "802.11 staging q");
    132 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
    133 		IEEE80211_INACT_INIT, ic->ic_max_keyix);
    134 #if __FreeBSD__
    135 	callout_init(&ic->ic_inact, 1);
    136 #elif __NetBSD__
    137 	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
    138 #endif
    139 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
    140 		ieee80211_node_timeout, ic);
    141 
    142 	ic->ic_node_alloc = node_alloc;
    143 	ic->ic_node_free = node_free;
    144 	ic->ic_node_cleanup = node_cleanup;
    145 	ic->ic_node_age = node_age;
    146 	ic->ic_node_drain = node_age;		/* NB: same as age */
    147 	ic->ic_node_getrssi = node_getrssi;
    148 	ic->ic_node_getsignal = node_getsignal;
    149 	ic->ic_node_getmimoinfo = node_getmimoinfo;
    150 
    151 	/*
    152 	 * Set flags to be propagated to all vap's;
    153 	 * these define default behaviour/configuration.
    154 	 */
    155 	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
    156 }
    157 
    158 void
    159 ieee80211_node_detach(struct ieee80211com *ic)
    160 {
    161 
    162 	callout_drain(&ic->ic_inact);
    163 	ieee80211_node_table_cleanup(&ic->ic_sta);
    164 	ieee80211_ageq_cleanup(&ic->ic_stageq);
    165 }
    166 
    167 void
    168 ieee80211_node_vattach(struct ieee80211vap *vap)
    169 {
    170 	/* NB: driver can override */
    171 	vap->iv_max_aid = IEEE80211_AID_DEF;
    172 
    173 	/* default station inactivity timer setings */
    174 	vap->iv_inact_init = IEEE80211_INACT_INIT;
    175 	vap->iv_inact_auth = IEEE80211_INACT_AUTH;
    176 	vap->iv_inact_run = IEEE80211_INACT_RUN;
    177 	vap->iv_inact_probe = IEEE80211_INACT_PROBE;
    178 
    179 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INACT,
    180 	    "%s: init %u auth %u run %u probe %u\n", __func__,
    181 	    vap->iv_inact_init, vap->iv_inact_auth,
    182 	    vap->iv_inact_run, vap->iv_inact_probe);
    183 }
    184 
    185 void
    186 ieee80211_node_latevattach(struct ieee80211vap *vap)
    187 {
    188 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
    189 		/* XXX should we allow max aid to be zero? */
    190 		if (vap->iv_max_aid < IEEE80211_AID_MIN) {
    191 			vap->iv_max_aid = IEEE80211_AID_MIN;
    192 			if_printf(vap->iv_ifp,
    193 			    "WARNING: max aid too small, changed to %d\n",
    194 			    vap->iv_max_aid);
    195 		}
    196 		vap->iv_aid_bitmap = (uint32_t *) IEEE80211_MALLOC(
    197 			howmany(vap->iv_max_aid, 32) * sizeof(uint32_t),
    198 			M_80211_NODE,
    199 			IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    200 		if (vap->iv_aid_bitmap == NULL) {
    201 			/* XXX no way to recover */
    202 			printf("%s: no memory for AID bitmap, max aid %d!\n",
    203 			    __func__, vap->iv_max_aid);
    204 			vap->iv_max_aid = 0;
    205 		}
    206 	}
    207 
    208 	ieee80211_reset_bss(vap);
    209 
    210 	vap->iv_auth = ieee80211_authenticator_get(vap->iv_bss->ni_authmode);
    211 }
    212 
    213 void
    214 ieee80211_node_vdetach(struct ieee80211vap *vap)
    215 {
    216 	struct ieee80211com *ic = vap->iv_ic;
    217 
    218 	ieee80211_node_table_reset(&ic->ic_sta, vap);
    219 	if (vap->iv_bss != NULL) {
    220 		ieee80211_free_node(vap->iv_bss);
    221 		vap->iv_bss = NULL;
    222 	}
    223 	if (vap->iv_aid_bitmap != NULL) {
    224 		IEEE80211_FREE(vap->iv_aid_bitmap, M_80211_NODE);
    225 		vap->iv_aid_bitmap = NULL;
    226 	}
    227 }
    228 
    229 /*
    230  * Port authorize/unauthorize interfaces for use by an authenticator.
    231  */
    232 
    233 void
    234 ieee80211_node_authorize(struct ieee80211_node *ni)
    235 {
    236 	struct ieee80211vap *vap = ni->ni_vap;
    237 
    238 	ni->ni_flags |= IEEE80211_NODE_AUTH;
    239 	ni->ni_inact_reload = vap->iv_inact_run;
    240 	ni->ni_inact = ni->ni_inact_reload;
    241 
    242 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
    243 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
    244 }
    245 
    246 void
    247 ieee80211_node_unauthorize(struct ieee80211_node *ni)
    248 {
    249 	struct ieee80211vap *vap = ni->ni_vap;
    250 
    251 	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
    252 	ni->ni_inact_reload = vap->iv_inact_auth;
    253 	if (ni->ni_inact > ni->ni_inact_reload)
    254 		ni->ni_inact = ni->ni_inact_reload;
    255 
    256 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
    257 	    "%s: inact_reload %u inact %u", __func__,
    258 	    ni->ni_inact_reload, ni->ni_inact);
    259 }
    260 
    261 /*
    262  * Fix tx parameters for a node according to ``association state''.
    263  */
    264 void
    265 ieee80211_node_setuptxparms(struct ieee80211_node *ni)
    266 {
    267 	struct ieee80211vap *vap = ni->ni_vap;
    268 	enum ieee80211_phymode mode;
    269 
    270 	if (ni->ni_flags & IEEE80211_NODE_VHT) {
    271 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
    272 			mode = IEEE80211_MODE_VHT_5GHZ;
    273 		else
    274 			mode = IEEE80211_MODE_VHT_2GHZ;
    275 	} else if (ni->ni_flags & IEEE80211_NODE_HT) {
    276 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
    277 			mode = IEEE80211_MODE_11NA;
    278 		else
    279 			mode = IEEE80211_MODE_11NG;
    280 	} else {				/* legacy rate handling */
    281 		if (IEEE80211_IS_CHAN_ST(ni->ni_chan))
    282 			mode = IEEE80211_MODE_STURBO_A;
    283 		else if (IEEE80211_IS_CHAN_HALF(ni->ni_chan))
    284 			mode = IEEE80211_MODE_HALF;
    285 		else if (IEEE80211_IS_CHAN_QUARTER(ni->ni_chan))
    286 			mode = IEEE80211_MODE_QUARTER;
    287 		/* NB: 108A should be handled as 11a */
    288 		else if (IEEE80211_IS_CHAN_A(ni->ni_chan))
    289 			mode = IEEE80211_MODE_11A;
    290 		else if (IEEE80211_IS_CHAN_108G(ni->ni_chan) ||
    291 		    (ni->ni_flags & IEEE80211_NODE_ERP))
    292 			mode = IEEE80211_MODE_11G;
    293 		else
    294 			mode = IEEE80211_MODE_11B;
    295 	}
    296 	ni->ni_txparms = &vap->iv_txparms[mode];
    297 }
    298 
    299 /*
    300  * Set/change the channel.  The rate set is also updated as
    301  * to insure a consistent view by drivers.
    302  * XXX should be private but hostap needs it to deal with CSA
    303  */
    304 void
    305 ieee80211_node_set_chan(struct ieee80211_node *ni,
    306 	struct ieee80211_channel *chan)
    307 {
    308 	struct ieee80211com *ic = ni->ni_ic;
    309 	struct ieee80211vap *vap = ni->ni_vap;
    310 	enum ieee80211_phymode mode;
    311 
    312 	KASSERT(chan != IEEE80211_CHAN_ANYC, ("no channel"));
    313 
    314 	ni->ni_chan = chan;
    315 	mode = ieee80211_chan2mode(chan);
    316 	if (IEEE80211_IS_CHAN_HT(chan)) {
    317 		/*
    318 		 * We must install the legacy rate est in ni_rates and the
    319 		 * HT rate set in ni_htrates.
    320 		 */
    321 		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
    322 		/*
    323 		 * Setup bss tx parameters based on operating mode.  We
    324 		 * use legacy rates when operating in a mixed HT+non-HT bss
    325 		 * and non-ERP rates in 11g for mixed ERP+non-ERP bss.
    326 		 */
    327 		if (mode == IEEE80211_MODE_11NA &&
    328 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
    329 			mode = IEEE80211_MODE_11A;
    330 		else if (mode == IEEE80211_MODE_11NG &&
    331 		    (vap->iv_flags_ht & IEEE80211_FHT_PUREN) == 0)
    332 			mode = IEEE80211_MODE_11G;
    333 		if (mode == IEEE80211_MODE_11G &&
    334 		    (vap->iv_flags & IEEE80211_F_PUREG) == 0)
    335 			mode = IEEE80211_MODE_11B;
    336 	}
    337 	ni->ni_txparms = &vap->iv_txparms[mode];
    338 	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
    339 }
    340 
    341 static __inline void
    342 copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
    343 {
    344 	/* propagate useful state */
    345 	nbss->ni_authmode = obss->ni_authmode;
    346 	nbss->ni_txpower = obss->ni_txpower;
    347 	nbss->ni_vlan = obss->ni_vlan;
    348 	/* XXX statistics? */
    349 	/* XXX legacy WDS bssid? */
    350 }
    351 
    352 void
    353 ieee80211_create_ibss(struct ieee80211vap* vap, struct ieee80211_channel *chan)
    354 {
    355 	struct ieee80211com *ic = vap->iv_ic;
    356 	struct ieee80211_node *ni;
    357 
    358 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
    359 		"%s: creating %s on channel %u%c flags 0x%08x\n", __func__,
    360 		ieee80211_opmode_name[vap->iv_opmode],
    361 		ieee80211_chan2ieee(ic, chan),
    362 		ieee80211_channel_type_char(chan),
    363 		chan->ic_flags);
    364 
    365 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
    366 	if (ni == NULL) {
    367 		/* XXX recovery? */
    368 		return;
    369 	}
    370 	IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
    371 	ni->ni_esslen = vap->iv_des_ssid[0].len;
    372 	memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
    373 	if (vap->iv_bss != NULL)
    374 		copy_bss(ni, vap->iv_bss);
    375 	ni->ni_intval = ic->ic_bintval;
    376 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
    377 		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
    378 	if (ic->ic_phytype == IEEE80211_T_FH) {
    379 		ni->ni_fhdwell = 200;	/* XXX */
    380 		ni->ni_fhindex = 1;
    381 	}
    382 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
    383 		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
    384 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
    385 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
    386 		else {
    387 			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
    388 			/* clear group bit, add local bit */
    389 			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
    390 		}
    391 	} else if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
    392 		if (vap->iv_flags & IEEE80211_F_DESBSSID)
    393 			IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_des_bssid);
    394 		else
    395 #ifdef IEEE80211_SUPPORT_TDMA
    396 		if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
    397 #endif
    398 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
    399 #ifdef IEEE80211_SUPPORT_MESH
    400 	} else if (vap->iv_opmode == IEEE80211_M_MBSS) {
    401 		ni->ni_meshidlen = vap->iv_mesh->ms_idlen;
    402 		memcpy(ni->ni_meshid, vap->iv_mesh->ms_id, ni->ni_meshidlen);
    403 #endif
    404 	}
    405 	/*
    406 	 * Fix the channel and related attributes.
    407 	 */
    408 	/* clear DFS CAC state on previous channel */
    409 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
    410 	    ic->ic_bsschan->ic_freq != chan->ic_freq &&
    411 	    IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan))
    412 		ieee80211_dfs_cac_clear(ic, ic->ic_bsschan);
    413 	ic->ic_bsschan = chan;
    414 	ieee80211_node_set_chan(ni, chan);
    415 	ic->ic_curmode = ieee80211_chan2mode(chan);
    416 	/*
    417 	 * Do mode-specific setup.
    418 	 */
    419 	if (IEEE80211_IS_CHAN_FULL(chan)) {
    420 		if (IEEE80211_IS_CHAN_ANYG(chan)) {
    421 			/*
    422 			 * Use a mixed 11b/11g basic rate set.
    423 			 */
    424 			ieee80211_setbasicrates(&ni->ni_rates,
    425 			    IEEE80211_MODE_11G);
    426 			if (vap->iv_flags & IEEE80211_F_PUREG) {
    427 				/*
    428 				 * Also mark OFDM rates basic so 11b
    429 				 * stations do not join (WiFi compliance).
    430 				 */
    431 				ieee80211_addbasicrates(&ni->ni_rates,
    432 				    IEEE80211_MODE_11A);
    433 			}
    434 		} else if (IEEE80211_IS_CHAN_B(chan)) {
    435 			/*
    436 			 * Force pure 11b rate set.
    437 			 */
    438 			ieee80211_setbasicrates(&ni->ni_rates,
    439 				IEEE80211_MODE_11B);
    440 		}
    441 	}
    442 
    443 	/* XXX TODO: other bits and pieces - eg fast-frames? */
    444 
    445 	/* If we're an 11n channel then initialise the 11n bits */
    446 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
    447 		/* XXX what else? */
    448 		ieee80211_ht_node_init(ni);
    449 		ieee80211_vht_node_init(ni);
    450 	} else if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
    451 		/* XXX what else? */
    452 		ieee80211_ht_node_init(ni);
    453 	}
    454 
    455 	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
    456 }
    457 
    458 /*
    459  * Reset bss state on transition to the INIT state.
    460  * Clear any stations from the table (they have been
    461  * deauth'd) and reset the bss node (clears key, rate
    462  * etc. state).
    463  */
    464 void
    465 ieee80211_reset_bss(struct ieee80211vap *vap)
    466 {
    467 	printf ("reset_bss: vap is 0x%lx\n", (unsigned long) vap);
    468 	struct ieee80211com *ic = vap->iv_ic;
    469 	struct ieee80211_node *ni, *obss;
    470 
    471 	printf ("reset_bss: ic is 0x%lx\n", (unsigned long) ic);
    472 
    473 	ieee80211_node_table_reset(&ic->ic_sta, vap);
    474 
    475 	printf ("reset_bss: after table_reset\n");
    476 	/* XXX multi-bss: wrong */
    477 	ieee80211_reset_erp(ic);
    478 
    479 	printf ("reset_bss: after reset_erp\n");
    480 
    481 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, vap->iv_myaddr);
    482 	KASSERT(ni != NULL, ("unable to setup initial BSS node"));
    483 
    484 	printf ("reset_bss: after alloc_node\n");
    485 
    486 	obss = vap->iv_bss;
    487 printf ("reset_bss: obss is 0x%lx\n", (unsigned long int)obss);
    488 	vap->iv_bss = ieee80211_ref_node(ni);
    489 	if (obss != NULL) {
    490 		copy_bss(ni, obss);
    491 		ni->ni_intval = ic->ic_bintval;
    492 		ieee80211_free_node(obss);
    493 	} else
    494 		IEEE80211_ADDR_COPY(ni->ni_bssid, vap->iv_myaddr);
    495 }
    496 
    497 static int
    498 match_ssid(const struct ieee80211_node *ni,
    499 	int nssid, const struct ieee80211_scan_ssid ssids[])
    500 {
    501 	int i;
    502 
    503 	for (i = 0; i < nssid; i++) {
    504 		if (ni->ni_esslen == ssids[i].len &&
    505 		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
    506 			return 1;
    507 	}
    508 	return 0;
    509 }
    510 
    511 /*
    512  * Test a node for suitability/compatibility.
    513  */
    514 static int
    515 check_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
    516 {
    517 	struct ieee80211com *ic = ni->ni_ic;
    518         uint8_t rate;
    519 
    520 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    521 		return 0;
    522 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
    523 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    524 			return 0;
    525 	} else {
    526 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    527 			return 0;
    528 	}
    529 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
    530 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    531 			return 0;
    532 	} else {
    533 		/* XXX does this mean privacy is supported or required? */
    534 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    535 			return 0;
    536 	}
    537 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
    538 	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
    539 	if (rate & IEEE80211_RATE_BASIC)
    540 		return 0;
    541 	if (vap->iv_des_nssid != 0 &&
    542 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
    543 		return 0;
    544 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
    545 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
    546 		return 0;
    547 	return 1;
    548 }
    549 
    550 #ifdef IEEE80211_DEBUG
    551 /*
    552  * Display node suitability/compatibility.
    553  */
    554 static void
    555 check_bss_debug(struct ieee80211vap *vap, struct ieee80211_node *ni)
    556 {
    557 	struct ieee80211com *ic = ni->ni_ic;
    558         uint8_t rate;
    559         int fail;
    560 
    561 	fail = 0;
    562 	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
    563 		fail |= 0x01;
    564 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
    565 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
    566 			fail |= 0x02;
    567 	} else {
    568 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
    569 			fail |= 0x02;
    570 	}
    571 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
    572 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
    573 			fail |= 0x04;
    574 	} else {
    575 		/* XXX does this mean privacy is supported or required? */
    576 		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
    577 			fail |= 0x04;
    578 	}
    579 	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
    580 	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
    581 	if (rate & IEEE80211_RATE_BASIC)
    582 		fail |= 0x08;
    583 	if (vap->iv_des_nssid != 0 &&
    584 	    !match_ssid(ni, vap->iv_des_nssid, vap->iv_des_ssid))
    585 		fail |= 0x10;
    586 	if ((vap->iv_flags & IEEE80211_F_DESBSSID) &&
    587 	    !IEEE80211_ADDR_EQ(vap->iv_des_bssid, ni->ni_bssid))
    588 		fail |= 0x20;
    589 
    590 	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
    591 	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
    592 	printf(" %3d%c",
    593 	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
    594 	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
    595 	    fail & 0x08 ? '!' : ' ');
    596 	printf(" %4s%c",
    597 	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
    598 	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
    599 	    "????",
    600 	    fail & 0x02 ? '!' : ' ');
    601 	printf(" %3s%c ",
    602 	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
    603 	    fail & 0x04 ? '!' : ' ');
    604 	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
    605 	printf("%s\n", fail & 0x10 ? "!" : "");
    606 }
    607 #endif /* IEEE80211_DEBUG */
    608 
    609 
    610 int
    611 ieee80211_ibss_merge_check(struct ieee80211_node *ni)
    612 {
    613 	struct ieee80211vap *vap = ni->ni_vap;
    614 
    615 	if (ni == vap->iv_bss ||
    616 	    IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
    617 		/* unchanged, nothing to do */
    618 		return 0;
    619 	}
    620 
    621 	if (!check_bss(vap, ni)) {
    622 		/* capabilities mismatch */
    623 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
    624 		    "%s: merge failed, capabilities mismatch\n", __func__);
    625 #ifdef IEEE80211_DEBUG
    626 		if (ieee80211_msg_assoc(vap))
    627 			check_bss_debug(vap, ni);
    628 #endif
    629 		vap->iv_stats.is_ibss_capmismatch++;
    630 		return 0;
    631 	}
    632 
    633 	return 1;
    634 }
    635 
    636 /*
    637  * Check if the given node should populate the node table.
    638  *
    639  * We need to be in "see all beacons for all ssids" mode in order
    640  * to do IBSS merges, however this means we will populate nodes for
    641  * /all/ IBSS SSIDs, versus just the one we care about.
    642  *
    643  * So this check ensures the node can actually belong to our IBSS
    644  * configuration.  For now it simply checks the SSID.
    645  */
    646 int
    647 ieee80211_ibss_node_check_new(struct ieee80211_node *ni,
    648     const struct ieee80211_scanparams *scan)
    649 {
    650 	struct ieee80211vap *vap = ni->ni_vap;
    651 	int i;
    652 
    653 	/*
    654 	 * If we have no SSID and no scan SSID, return OK.
    655 	 */
    656 	if (vap->iv_des_nssid == 0 && scan->ssid == NULL)
    657 		goto ok;
    658 
    659 	/*
    660 	 * If we have one of (SSID, scan SSID) then return error.
    661 	 */
    662 	if (!! (vap->iv_des_nssid == 0) != !! (scan->ssid == NULL))
    663 		goto mismatch;
    664 
    665 	/*
    666 	 * Double-check - we need scan SSID.
    667 	 */
    668 	if (scan->ssid == NULL)
    669 		goto mismatch;
    670 
    671 	/*
    672 	 * Check if the scan SSID matches the SSID list for the VAP.
    673 	 */
    674 	for (i = 0; i < vap->iv_des_nssid; i++) {
    675 
    676 		/* Sanity length check */
    677 		if (vap->iv_des_ssid[i].len != scan->ssid[1])
    678 			continue;
    679 
    680 		/* Note: SSID in the scan entry is the IE format */
    681 		if (memcmp(vap->iv_des_ssid[i].ssid, scan->ssid + 2,
    682 		    vap->iv_des_ssid[i].len) == 0)
    683 			goto ok;
    684 	}
    685 
    686 mismatch:
    687 	return (0);
    688 ok:
    689 	return (1);
    690 }
    691 
    692 /*
    693  * Handle 802.11 ad hoc network merge.  The
    694  * convention, set by the Wireless Ethernet Compatibility Alliance
    695  * (WECA), is that an 802.11 station will change its BSSID to match
    696  * the "oldest" 802.11 ad hoc network, on the same channel, that
    697  * has the station's desired SSID.  The "oldest" 802.11 network
    698  * sends beacons with the greatest TSF timestamp.
    699  *
    700  * The caller is assumed to validate TSF's before attempting a merge.
    701  *
    702  * Return !0 if the BSSID changed, 0 otherwise.
    703  */
    704 int
    705 ieee80211_ibss_merge(struct ieee80211_node *ni)
    706 {
    707 #ifdef IEEE80211_DEBUG
    708 	struct ieee80211vap *vap = ni->ni_vap;
    709 	struct ieee80211com *ic = ni->ni_ic;
    710 #endif
    711 
    712 	if (! ieee80211_ibss_merge_check(ni))
    713 		return 0;
    714 
    715 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
    716 		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
    717 		ether_sprintf(ni->ni_bssid),
    718 		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
    719 		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
    720 		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
    721 	);
    722 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
    723 }
    724 
    725 /*
    726  * Calculate HT channel promotion flags for all vaps.
    727  * This assumes ni_chan have been setup for each vap.
    728  */
    729 static int
    730 gethtadjustflags(struct ieee80211com *ic)
    731 {
    732 	struct ieee80211vap *vap;
    733 	int flags;
    734 
    735 	flags = 0;
    736 	/* XXX locking */
    737 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    738 		if (vap->iv_state < IEEE80211_S_RUN)
    739 			continue;
    740 		switch (vap->iv_opmode) {
    741 		case IEEE80211_M_WDS:
    742 		case IEEE80211_M_STA:
    743 		case IEEE80211_M_AHDEMO:
    744 		case IEEE80211_M_HOSTAP:
    745 		case IEEE80211_M_IBSS:
    746 		case IEEE80211_M_MBSS:
    747 			flags |= ieee80211_htchanflags(vap->iv_bss->ni_chan);
    748 			break;
    749 		default:
    750 			break;
    751 		}
    752 	}
    753 	return flags;
    754 }
    755 
    756 /*
    757  * Calculate VHT channel promotion flags for all vaps.
    758  * This assumes ni_chan have been setup for each vap.
    759  */
    760 static int
    761 getvhtadjustflags(struct ieee80211com *ic)
    762 {
    763 	struct ieee80211vap *vap;
    764 	int flags;
    765 
    766 	flags = 0;
    767 	/* XXX locking */
    768 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    769 		if (vap->iv_state < IEEE80211_S_RUN)
    770 			continue;
    771 		switch (vap->iv_opmode) {
    772 		case IEEE80211_M_WDS:
    773 		case IEEE80211_M_STA:
    774 		case IEEE80211_M_AHDEMO:
    775 		case IEEE80211_M_HOSTAP:
    776 		case IEEE80211_M_IBSS:
    777 		case IEEE80211_M_MBSS:
    778 			flags |= ieee80211_vhtchanflags(vap->iv_bss->ni_chan);
    779 			break;
    780 		default:
    781 			break;
    782 		}
    783 	}
    784 	return flags;
    785 }
    786 
    787 /*
    788  * Check if the current channel needs to change based on whether
    789  * any vap's are using HT20/HT40.  This is used to sync the state
    790  * of ic_curchan after a channel width change on a running vap.
    791  *
    792  * Same applies for VHT.
    793  */
    794 void
    795 ieee80211_sync_curchan(struct ieee80211com *ic)
    796 {
    797 	struct ieee80211_channel *c;
    798 
    799 	c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, gethtadjustflags(ic));
    800 	c = ieee80211_vht_adjust_channel(ic, c, getvhtadjustflags(ic));
    801 
    802 	if (c != ic->ic_curchan) {
    803 		ic->ic_curchan = c;
    804 		ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
    805 		ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
    806 		IEEE80211_UNLOCK(ic);
    807 		ic->ic_set_channel(ic);
    808 		ieee80211_radiotap_chan_change(ic);
    809 		IEEE80211_LOCK(ic);
    810 	}
    811 }
    812 
    813 /*
    814  * Setup the current channel.  The request channel may be
    815  * promoted if other vap's are operating with HT20/HT40.
    816  */
    817 void
    818 ieee80211_setupcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
    819 {
    820 	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
    821 		int flags = gethtadjustflags(ic);
    822 		/*
    823 		 * Check for channel promotion required to support the
    824 		 * set of running vap's.  This assumes we are called
    825 		 * after ni_chan is setup for each vap.
    826 		 */
    827 		/* XXX VHT? */
    828 		/* NB: this assumes IEEE80211_FHT_USEHT40 > IEEE80211_FHT_HT */
    829 		if (flags > ieee80211_htchanflags(c))
    830 			c = ieee80211_ht_adjust_channel(ic, c, flags);
    831 	}
    832 
    833 	/*
    834 	 * VHT promotion - this will at least promote to VHT20/40
    835 	 * based on what HT has done; it may further promote the
    836 	 * channel to VHT80 or above.
    837 	 */
    838 	if (ic->ic_vhtcaps != 0) {
    839 		int flags = getvhtadjustflags(ic);
    840 		if (flags > ieee80211_vhtchanflags(c))
    841 			c = ieee80211_vht_adjust_channel(ic, c, flags);
    842 	}
    843 
    844 	ic->ic_bsschan = ic->ic_curchan = c;
    845 	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
    846 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
    847 }
    848 
    849 /*
    850  * Change the current channel.  The channel change is guaranteed to have
    851  * happened before the next state change.
    852  */
    853 void
    854 ieee80211_setcurchan(struct ieee80211com *ic, struct ieee80211_channel *c)
    855 {
    856 	ieee80211_setupcurchan(ic, c);
    857 	ieee80211_runtask(ic, &ic->ic_chan_task);
    858 }
    859 
    860 void
    861 ieee80211_update_chw(struct ieee80211com *ic)
    862 {
    863 
    864 	ieee80211_setupcurchan(ic, ic->ic_curchan);
    865 	ieee80211_runtask(ic, &ic->ic_chw_task);
    866 }
    867 
    868 /*
    869  * Join the specified IBSS/BSS network.  The node is assumed to
    870  * be passed in with a held reference.
    871  */
    872 static int
    873 ieee80211_sta_join1(struct ieee80211_node *selbs)
    874 {
    875 	struct ieee80211vap *vap = selbs->ni_vap;
    876 	struct ieee80211com *ic = selbs->ni_ic;
    877 	struct ieee80211_node *obss;
    878 	int canreassoc;
    879 
    880 	/*
    881 	 * Committed to selbs, setup state.
    882 	 */
    883 	obss = vap->iv_bss;
    884 	/*
    885 	 * Check if old+new node have the same address in which
    886 	 * case we can reassociate when operating in sta mode.
    887 	 */
    888 	canreassoc = (obss != NULL &&
    889 		vap->iv_state == IEEE80211_S_RUN &&
    890 		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
    891 	vap->iv_bss = selbs;		/* NB: caller assumed to bump refcnt */
    892 	if (obss != NULL) {
    893 		struct ieee80211_node_table *nt = obss->ni_table;
    894 
    895 		copy_bss(selbs, obss);
    896 		ieee80211_node_decref(obss);	/* iv_bss reference */
    897 
    898 		IEEE80211_NODE_LOCK(nt);
    899 		node_reclaim(nt, obss);		/* station table reference */
    900 		IEEE80211_NODE_UNLOCK(nt);
    901 
    902 		obss = NULL;		/* NB: guard against later use */
    903 	}
    904 
    905 	/*
    906 	 * Delete unusable rates; we've already checked
    907 	 * that the negotiated rate set is acceptable.
    908 	 */
    909 	ieee80211_fix_rate(vap->iv_bss, &vap->iv_bss->ni_rates,
    910 		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
    911 
    912 	ieee80211_setcurchan(ic, selbs->ni_chan);
    913 	/*
    914 	 * Set the erp state (mostly the slot time) to deal with
    915 	 * the auto-select case; this should be redundant if the
    916 	 * mode is locked.
    917 	 */
    918 	ieee80211_reset_erp(ic);
    919 	IEEE80211_UNLOCK(ic);  // NNN BUG??? --
    920 	ieee80211_wme_initparams(vap);
    921 
    922 	if (vap->iv_opmode == IEEE80211_M_STA) {
    923 		if (canreassoc) {
    924 			/* Reassociate */
    925 			ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
    926 		} else {
    927 			/*
    928 			 * Act as if we received a DEAUTH frame in case we
    929 			 * are invoked from the RUN state.  This will cause
    930 			 * us to try to re-authenticate if we are operating
    931 			 * as a station.
    932 			 */
    933 			ieee80211_new_state(vap, IEEE80211_S_AUTH,
    934 				IEEE80211_FC0_SUBTYPE_DEAUTH);
    935 		}
    936 	} else
    937 		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
    938 	IEEE80211_LOCK(ic);  // NNN BUG ??? --- unlock for full function?
    939 	return 1;
    940 }
    941 
    942 int
    943 ieee80211_sta_join(struct ieee80211vap *vap, struct ieee80211_channel *chan,
    944 	const struct ieee80211_scan_entry *se)
    945 {
    946 	struct ieee80211com *ic = vap->iv_ic;
    947 	struct ieee80211_node *ni;
    948 	int do_ht = 0;
    949 
    950 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, se->se_macaddr);
    951 	if (ni == NULL) {
    952 		/* XXX msg */
    953 		return 0;
    954 	}
    955 
    956 	/*
    957 	 * Expand scan state into node's format.
    958 	 * XXX may not need all this stuff
    959 	 */
    960 	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
    961 	ni->ni_esslen = se->se_ssid[1];
    962 	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
    963 	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
    964 	ni->ni_intval = se->se_intval;
    965 	ni->ni_capinfo = se->se_capinfo;
    966 	ni->ni_chan = chan;
    967 	ni->ni_timoff = se->se_timoff;
    968 	ni->ni_fhdwell = se->se_fhdwell;
    969 	ni->ni_fhindex = se->se_fhindex;
    970 	ni->ni_erp = se->se_erp;
    971 	IEEE80211_RSSI_LPF(ni->ni_avgrssi, se->se_rssi);
    972 	ni->ni_noise = se->se_noise;
    973 	if (vap->iv_opmode == IEEE80211_M_STA) {
    974 		/* NB: only infrastructure mode requires an associd */
    975 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
    976 	}
    977 
    978 	if (ieee80211_ies_init(&ni->ni_ies, se->se_ies.data, se->se_ies.len)) {
    979 		ieee80211_ies_expand(&ni->ni_ies);
    980 #ifdef IEEE80211_SUPPORT_SUPERG
    981 		if (ni->ni_ies.ath_ie != NULL)
    982 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
    983 #endif
    984 		if (ni->ni_ies.htcap_ie != NULL)
    985 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
    986 		if (ni->ni_ies.htinfo_ie != NULL)
    987 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
    988 #ifdef IEEE80211_SUPPORT_MESH
    989 		if (ni->ni_ies.meshid_ie != NULL)
    990 			ieee80211_parse_meshid(ni, ni->ni_ies.meshid_ie);
    991 #endif
    992 #ifdef IEEE80211_SUPPORT_TDMA
    993 		if (ni->ni_ies.tdma_ie != NULL)
    994 			ieee80211_parse_tdma(ni, ni->ni_ies.tdma_ie);
    995 #endif
    996 		if (ni->ni_ies.vhtcap_ie != NULL)
    997 			ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
    998 		if (ni->ni_ies.vhtopmode_ie != NULL)
    999 			ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
   1000 
   1001 		/* XXX parse BSSLOAD IE */
   1002 		/* XXX parse TXPWRENV IE */
   1003 		/* XXX parse APCHANREP IE */
   1004 	}
   1005 
   1006 	vap->iv_dtim_period = se->se_dtimperiod;
   1007 	vap->iv_dtim_count = 0;
   1008 
   1009 	/* NB: must be after ni_chan is setup */
   1010 	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
   1011 		IEEE80211_F_DOSORT);
   1012 	if (ieee80211_iserp_rateset(&ni->ni_rates))
   1013 		ni->ni_flags |= IEEE80211_NODE_ERP;
   1014 
   1015 	/*
   1016 	 * Setup HT state for this node if it's available, otherwise
   1017 	 * non-STA modes won't pick this state up.
   1018 	 *
   1019 	 * For IBSS and related modes that don't go through an
   1020 	 * association request/response, the only appropriate place
   1021 	 * to setup the HT state is here.
   1022 	 */
   1023 	if (ni->ni_ies.htinfo_ie != NULL &&
   1024 	    ni->ni_ies.htcap_ie != NULL &&
   1025 	    vap->iv_flags_ht & IEEE80211_FHT_HT) {
   1026 		ieee80211_ht_node_init(ni);
   1027 		ieee80211_ht_updateparams(ni,
   1028 		    ni->ni_ies.htcap_ie,
   1029 		    ni->ni_ies.htinfo_ie);
   1030 		do_ht = 1;
   1031 	}
   1032 
   1033 	/*
   1034 	 * Setup VHT state for this node if it's available.
   1035 	 * Same as the above.
   1036 	 *
   1037 	 * For now, don't allow 2GHz VHT operation.
   1038 	 */
   1039 	if (ni->ni_ies.vhtopmode_ie != NULL &&
   1040 	    ni->ni_ies.vhtcap_ie != NULL &&
   1041 	    vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
   1042 #if __FreeBSD__
   1043 		if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   1044 			printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
   1045 			    __func__,
   1046 			    ni->ni_macaddr,
   1047 			    ":");
   1048 #elif __NetBSD__
   1049 		if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   1050 			printf("%s: BSS %6ld: 2GHz channel, VHT info; ignoring\n",
   1051 			    __func__, (long int)ni->ni_macaddr);
   1052 #endif
   1053 		} else {
   1054 			ieee80211_vht_node_init(ni);
   1055 			ieee80211_vht_updateparams(ni,
   1056 			    ni->ni_ies.vhtcap_ie,
   1057 			    ni->ni_ies.vhtopmode_ie);
   1058 			ieee80211_setup_vht_rates(ni, ni->ni_ies.vhtcap_ie,
   1059 			    ni->ni_ies.vhtopmode_ie);
   1060 			do_ht = 1;
   1061 		}
   1062 	}
   1063 
   1064 	/* Finally do the node channel change */
   1065 	if (do_ht) {
   1066 		ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
   1067 		    ni->ni_ies.htinfo_ie);
   1068 		ieee80211_setup_htrates(ni, ni->ni_ies.htcap_ie,
   1069 		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
   1070 		ieee80211_setup_basic_htrates(ni, ni->ni_ies.htinfo_ie);
   1071 	}
   1072 
   1073 	/* XXX else check for ath FF? */
   1074 	/* XXX QoS? Difficult given that WME config is specific to a master */
   1075 
   1076 	ieee80211_node_setuptxparms(ni);
   1077 	ieee80211_ratectl_node_init(ni);
   1078 
   1079 	return ieee80211_sta_join1(ieee80211_ref_node(ni));
   1080 }
   1081 
   1082 /*
   1083  * Leave the specified IBSS/BSS network.  The node is assumed to
   1084  * be passed in with a held reference.
   1085  */
   1086 void
   1087 ieee80211_sta_leave(struct ieee80211_node *ni)
   1088 {
   1089 	struct ieee80211com *ic = ni->ni_ic;
   1090 
   1091 	ic->ic_node_cleanup(ni);
   1092 	ieee80211_notify_node_leave(ni);
   1093 }
   1094 
   1095 /*
   1096  * Send a deauthenticate frame and drop the station.
   1097  */
   1098 void
   1099 ieee80211_node_deauth(struct ieee80211_node *ni, int reason)
   1100 {
   1101 	/* NB: bump the refcnt to be sure temporary nodes are not reclaimed */
   1102 	ieee80211_ref_node(ni);
   1103 	if (ni->ni_associd != 0)
   1104 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
   1105 	ieee80211_node_leave(ni);
   1106 	ieee80211_free_node(ni);
   1107 }
   1108 
   1109 static struct ieee80211_node *
   1110 node_alloc(struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1111 {
   1112 	struct ieee80211_node *ni;
   1113 
   1114 	ni = (struct ieee80211_node *) IEEE80211_MALLOC(sizeof(struct ieee80211_node),
   1115 		M_80211_NODE, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   1116 	return ni;
   1117 }
   1118 
   1119 /*
   1120  * Initialize an ie blob with the specified data.  If previous
   1121  * data exists re-use the data block.  As a side effect we clear
   1122  * all references to specific ie's; the caller is required to
   1123  * recalculate them.
   1124  */
   1125 int
   1126 ieee80211_ies_init(struct ieee80211_ies *ies, const uint8_t *data, int len)
   1127 {
   1128 	/* NB: assumes data+len are the last fields */
   1129 	memset(ies, 0, offsetof(struct ieee80211_ies, data));
   1130 	if (ies->data != NULL && ies->len != len) {
   1131 		/* data size changed */
   1132 		IEEE80211_FREE(ies->data, M_80211_NODE_IE);
   1133 		ies->data = NULL;
   1134 	}
   1135 	if (ies->data == NULL) {
   1136 		ies->data = (uint8_t *) IEEE80211_MALLOC(len, M_80211_NODE_IE,
   1137 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   1138 		if (ies->data == NULL) {
   1139 			ies->len = 0;
   1140 			/* NB: pointers have already been zero'd above */
   1141 			return 0;
   1142 		}
   1143 	}
   1144 	memcpy(ies->data, data, len);
   1145 	ies->len = len;
   1146 	return 1;
   1147 }
   1148 
   1149 /*
   1150  * Reclaim storage for an ie blob.
   1151  */
   1152 void
   1153 ieee80211_ies_cleanup(struct ieee80211_ies *ies)
   1154 {
   1155 	if (ies->data != NULL)
   1156 		IEEE80211_FREE(ies->data, M_80211_NODE_IE);
   1157 }
   1158 
   1159 /*
   1160  * Expand an ie blob data contents and to fillin individual
   1161  * ie pointers.  The data blob is assumed to be well-formed;
   1162  * we don't do any validity checking of ie lengths.
   1163  */
   1164 void
   1165 ieee80211_ies_expand(struct ieee80211_ies *ies)
   1166 {
   1167 	uint8_t *ie;
   1168 	int ielen;
   1169 
   1170 	ie = ies->data;
   1171 	ielen = ies->len;
   1172 	while (ielen > 0) {
   1173 		switch (ie[0]) {
   1174 		case IEEE80211_ELEMID_VENDOR:
   1175 			if (iswpaoui(ie))
   1176 				ies->wpa_ie = ie;
   1177 			else if (iswmeoui(ie))
   1178 				ies->wme_ie = ie;
   1179 #ifdef IEEE80211_SUPPORT_SUPERG
   1180 			else if (isatherosoui(ie))
   1181 				ies->ath_ie = ie;
   1182 #endif
   1183 #ifdef IEEE80211_SUPPORT_TDMA
   1184 			else if (istdmaoui(ie))
   1185 				ies->tdma_ie = ie;
   1186 #endif
   1187 			break;
   1188 		case IEEE80211_ELEMID_RSN:
   1189 			ies->rsn_ie = ie;
   1190 			break;
   1191 		case IEEE80211_ELEMID_HTCAP:
   1192 			ies->htcap_ie = ie;
   1193 			break;
   1194 		case IEEE80211_ELEMID_HTINFO:
   1195 			ies->htinfo_ie = ie;
   1196 			break;
   1197 #ifdef IEEE80211_SUPPORT_MESH
   1198 		case IEEE80211_ELEMID_MESHID:
   1199 			ies->meshid_ie = ie;
   1200 			break;
   1201 #endif
   1202 		case IEEE80211_ELEMID_VHT_CAP:
   1203 			ies->vhtcap_ie = ie;
   1204 			break;
   1205 		case IEEE80211_ELEMID_VHT_OPMODE:
   1206 			ies->vhtopmode_ie = ie;
   1207 			break;
   1208 		case IEEE80211_ELEMID_VHT_PWR_ENV:
   1209 			ies->vhtpwrenv_ie = ie;
   1210 			break;
   1211 		case IEEE80211_ELEMID_BSSLOAD:
   1212 			ies->bssload_ie = ie;
   1213 			break;
   1214 		case IEEE80211_ELEMID_APCHANREP:
   1215 			ies->apchanrep_ie = ie;
   1216 			break;
   1217 		}
   1218 		ielen -= 2 + ie[1];
   1219 		ie += 2 + ie[1];
   1220 	}
   1221 }
   1222 
   1223 /*
   1224  * Reclaim any resources in a node and reset any critical
   1225  * state.  Typically nodes are free'd immediately after,
   1226  * but in some cases the storage may be reused so we need
   1227  * to insure consistent state (should probably fix that).
   1228  */
   1229 static void
   1230 node_cleanup(struct ieee80211_node *ni)
   1231 {
   1232 	struct ieee80211vap *vap = ni->ni_vap;
   1233 	struct ieee80211com *ic = ni->ni_ic;
   1234 	int i;
   1235 
   1236 	/* NB: preserve ni_table */
   1237 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
   1238 		if (vap->iv_opmode != IEEE80211_M_STA)
   1239 			vap->iv_ps_sta--;
   1240 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
   1241 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
   1242 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
   1243 	}
   1244 	/*
   1245 	 * Cleanup any VHT and HT-related state.
   1246 	 */
   1247 	if (ni->ni_flags & IEEE80211_NODE_VHT)
   1248 		ieee80211_vht_node_cleanup(ni);
   1249 	if (ni->ni_flags & IEEE80211_NODE_HT)
   1250 		ieee80211_ht_node_cleanup(ni);
   1251 #ifdef IEEE80211_SUPPORT_SUPERG
   1252 	/* Always do FF node cleanup; for A-MSDU */
   1253 	ieee80211_ff_node_cleanup(ni);
   1254 #endif
   1255 #ifdef IEEE80211_SUPPORT_MESH
   1256 	/*
   1257 	 * Cleanup any mesh-related state.
   1258 	 */
   1259 	if (vap->iv_opmode == IEEE80211_M_MBSS)
   1260 		ieee80211_mesh_node_cleanup(ni);
   1261 #endif
   1262 	/*
   1263 	 * Clear any staging queue entries.
   1264 	 */
   1265 	ieee80211_ageq_drain_node(&ic->ic_stageq, ni);
   1266 
   1267 	/*
   1268 	 * Clear AREF flag that marks the authorization refcnt bump
   1269 	 * has happened.  This is probably not needed as the node
   1270 	 * should always be removed from the table so not found but
   1271 	 * do it just in case.
   1272 	 * Likewise clear the ASSOCID flag as these flags are intended
   1273 	 * to be managed in tandem.
   1274 	 */
   1275 	ni->ni_flags &= ~(IEEE80211_NODE_AREF | IEEE80211_NODE_ASSOCID);
   1276 
   1277 	/*
   1278 	 * Drain power save queue and, if needed, clear TIM.
   1279 	 */
   1280 	if (ieee80211_node_psq_drain(ni) != 0 && vap->iv_set_tim != NULL)
   1281 		vap->iv_set_tim(ni, 0);
   1282 
   1283 	ni->ni_associd = 0;
   1284 	if (ni->ni_challenge != NULL) {
   1285 		IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
   1286 		ni->ni_challenge = NULL;
   1287 	}
   1288 	/*
   1289 	 * Preserve SSID, WPA, and WME ie's so the bss node is
   1290 	 * reusable during a re-auth/re-assoc state transition.
   1291 	 * If we remove these data they will not be recreated
   1292 	 * because they come from a probe-response or beacon frame
   1293 	 * which cannot be expected prior to the association-response.
   1294 	 * This should not be an issue when operating in other modes
   1295 	 * as stations leaving always go through a full state transition
   1296 	 * which will rebuild this state.
   1297 	 *
   1298 	 * XXX does this leave us open to inheriting old state?
   1299 	 */
   1300 	for (i = 0; i < nitems(ni->ni_rxfrag); i++)
   1301 		if (ni->ni_rxfrag[i] != NULL) {
   1302 			m_freem(ni->ni_rxfrag[i]);
   1303 			ni->ni_rxfrag[i] = NULL;
   1304 		}
   1305 	/*
   1306 	 * Must be careful here to remove any key map entry w/o a LOR.
   1307 	 */
   1308 	ieee80211_node_delucastkey(ni);
   1309 }
   1310 
   1311 static void
   1312 node_free(struct ieee80211_node *ni)
   1313 {
   1314 	struct ieee80211com *ic = ni->ni_ic;
   1315 
   1316 	ieee80211_ratectl_node_deinit(ni);
   1317 	ic->ic_node_cleanup(ni);
   1318 	ieee80211_ies_cleanup(&ni->ni_ies);
   1319 	ieee80211_psq_cleanup(&ni->ni_psq);
   1320 	IEEE80211_FREE(ni, M_80211_NODE);
   1321 }
   1322 
   1323 static void
   1324 node_age(struct ieee80211_node *ni)
   1325 {
   1326 	struct ieee80211vap *vap = ni->ni_vap;
   1327 
   1328 	/*
   1329 	 * Age frames on the power save queue.
   1330 	 */
   1331 	if (ieee80211_node_psq_age(ni) != 0 &&
   1332 	    ni->ni_psq.psq_len == 0 && vap->iv_set_tim != NULL)
   1333 		vap->iv_set_tim(ni, 0);
   1334 	/*
   1335 	 * Age out HT resources (e.g. frames on the
   1336 	 * A-MPDU reorder queues).
   1337 	 */
   1338 	if (ni->ni_associd != 0 && (ni->ni_flags & IEEE80211_NODE_HT))
   1339 		ieee80211_ht_node_age(ni);
   1340 }
   1341 
   1342 static int8_t
   1343 node_getrssi(const struct ieee80211_node *ni)
   1344 {
   1345 	uint32_t avgrssi = ni->ni_avgrssi;
   1346 	int32_t rssi;
   1347 
   1348 	if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER)
   1349 		return 0;
   1350 	rssi = IEEE80211_RSSI_GET(avgrssi);
   1351 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
   1352 }
   1353 
   1354 static void
   1355 node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
   1356 {
   1357 	*rssi = node_getrssi(ni);
   1358 	*noise = ni->ni_noise;
   1359 }
   1360 
   1361 static void
   1362 node_getmimoinfo(const struct ieee80211_node *ni,
   1363 	struct ieee80211_mimo_info *info)
   1364 {
   1365 	int i;
   1366 	uint32_t avgrssi;
   1367 	int32_t rssi;
   1368 
   1369 	bzero(info, sizeof(*info));
   1370 
   1371 	for (i = 0; i < MIN(IEEE80211_MAX_CHAINS, ni->ni_mimo_chains); i++) {
   1372 		/* Note: for now, just pri20 channel info */
   1373 		avgrssi = ni->ni_mimo_rssi_ctl[i];
   1374 		if (avgrssi == IEEE80211_RSSI_DUMMY_MARKER) {
   1375 			info->ch[i].rssi[0] = 0;
   1376 		} else {
   1377 			rssi = IEEE80211_RSSI_GET(avgrssi);
   1378 			info->ch[i].rssi[0] = rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
   1379 		}
   1380 		info->ch[i].noise[0] = ni->ni_mimo_noise_ctl[i];
   1381 	}
   1382 
   1383 	/* XXX ext radios? */
   1384 
   1385 	/* XXX EVM? */
   1386 }
   1387 
   1388 static void
   1389 ieee80211_add_node_nt(struct ieee80211_node_table *nt,
   1390     struct ieee80211_node *ni)
   1391 {
   1392 	struct ieee80211com *ic = nt->nt_ic;
   1393 	int hash;
   1394 
   1395 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1396 
   1397 	hash = IEEE80211_NODE_HASH(ic, ni->ni_macaddr);
   1398 	(void) ic;	/* XXX IEEE80211_NODE_HASH */
   1399 	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
   1400 	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
   1401 	nt->nt_count++;
   1402 	ni->ni_table = nt;
   1403 }
   1404 
   1405 static void
   1406 ieee80211_del_node_nt(struct ieee80211_node_table *nt,
   1407     struct ieee80211_node *ni)
   1408 {
   1409 
   1410 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1411 
   1412 	TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
   1413 	LIST_REMOVE(ni, ni_hash);
   1414 	nt->nt_count--;
   1415 	KASSERT(nt->nt_count >= 0,
   1416 	    ("nt_count is negative (%d)!\n", nt->nt_count));
   1417 	ni->ni_table = NULL;
   1418 }
   1419 
   1420 struct ieee80211_node *
   1421 ieee80211_alloc_node(struct ieee80211_node_table *nt,
   1422 	struct ieee80211vap *vap, const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1423 {
   1424 	struct ieee80211com *ic = nt->nt_ic;
   1425 	struct ieee80211_node *ni;
   1426 
   1427 	ni = ic->ic_node_alloc(vap, macaddr);
   1428 	if (ni == NULL) {
   1429 		vap->iv_stats.is_rx_nodealloc++;
   1430 		return NULL;
   1431 	}
   1432 
   1433 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   1434 		"%s %p<%s> in %s table\n", __func__, ni,
   1435 		ether_sprintf(macaddr), nt->nt_name);
   1436 
   1437 	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
   1438 	ieee80211_node_initref(ni);		/* mark referenced */
   1439 	ni->ni_chan = IEEE80211_CHAN_ANYC;
   1440 	ni->ni_authmode = IEEE80211_AUTH_OPEN;
   1441 	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
   1442 	ni->ni_txparms = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
   1443 	ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
   1444 	ni->ni_avgrssi = IEEE80211_RSSI_DUMMY_MARKER;
   1445 	ni->ni_inact_reload = nt->nt_inact_init;
   1446 	ni->ni_inact = ni->ni_inact_reload;
   1447 	ni->ni_ath_defkeyix = 0x7fff;
   1448 	ieee80211_psq_init(&ni->ni_psq, "unknown");
   1449 #ifdef IEEE80211_SUPPORT_MESH
   1450 	if (vap->iv_opmode == IEEE80211_M_MBSS)
   1451 		ieee80211_mesh_node_init(vap, ni);
   1452 #endif
   1453 	IEEE80211_NODE_LOCK(nt);
   1454 	ieee80211_add_node_nt(nt, ni);
   1455 	ni->ni_vap = vap;
   1456 	ni->ni_ic = ic;
   1457 	IEEE80211_NODE_UNLOCK(nt);
   1458 
   1459 	IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
   1460 	    "%s: inact_reload %u", __func__, ni->ni_inact_reload);
   1461 
   1462 	ieee80211_ratectl_node_init(ni);
   1463 
   1464 	return ni;
   1465 }
   1466 
   1467 /*
   1468  * Craft a temporary node suitable for sending a management frame
   1469  * to the specified station.  We craft only as much state as we
   1470  * need to do the work since the node will be immediately reclaimed
   1471  * once the send completes.
   1472  */
   1473 struct ieee80211_node *
   1474 ieee80211_tmp_node(struct ieee80211vap *vap,
   1475 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1476 {
   1477 	struct ieee80211com *ic = vap->iv_ic;
   1478 	struct ieee80211_node *ni;
   1479 
   1480 	ni = ic->ic_node_alloc(vap, macaddr);
   1481 	if (ni != NULL) {
   1482 		struct ieee80211_node *bss = vap->iv_bss;
   1483 
   1484 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   1485 			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
   1486 
   1487 		ni->ni_table = NULL;		/* NB: pedantic */
   1488 		ni->ni_ic = ic;			/* NB: needed to set channel */
   1489 		ni->ni_vap = vap;
   1490 
   1491 		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
   1492 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
   1493 		ieee80211_node_initref(ni);		/* mark referenced */
   1494 		/* NB: required by ieee80211_fix_rate */
   1495 		ieee80211_node_set_chan(ni, bss->ni_chan);
   1496 		ieee80211_crypto_resetkey(vap, &ni->ni_ucastkey,
   1497 			IEEE80211_KEYIX_NONE);
   1498 		ni->ni_txpower = bss->ni_txpower;
   1499 		/* XXX optimize away */
   1500 		ieee80211_psq_init(&ni->ni_psq, "unknown");
   1501 
   1502 		ieee80211_ratectl_node_init(ni);
   1503 	} else {
   1504 		/* XXX msg */
   1505 		vap->iv_stats.is_rx_nodealloc++;
   1506 	}
   1507 	return ni;
   1508 }
   1509 
   1510 struct ieee80211_node *
   1511 ieee80211_dup_bss(struct ieee80211vap *vap,
   1512 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1513 {
   1514 	struct ieee80211com *ic = vap->iv_ic;
   1515 	struct ieee80211_node *ni;
   1516 
   1517 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, macaddr);
   1518 	if (ni != NULL) {
   1519 		struct ieee80211_node *bss = vap->iv_bss;
   1520 		/*
   1521 		 * Inherit from iv_bss.
   1522 		 */
   1523 		copy_bss(ni, bss);
   1524 		IEEE80211_ADDR_COPY(ni->ni_bssid, bss->ni_bssid);
   1525 		ieee80211_node_set_chan(ni, bss->ni_chan);
   1526 	}
   1527 	return ni;
   1528 }
   1529 
   1530 /*
   1531  * Create a bss node for a legacy WDS vap.  The far end does
   1532  * not associate so we just create create a new node and
   1533  * simulate an association.  The caller is responsible for
   1534  * installing the node as the bss node and handling any further
   1535  * setup work like authorizing the port.
   1536  */
   1537 struct ieee80211_node *
   1538 ieee80211_node_create_wds(struct ieee80211vap *vap,
   1539 	const uint8_t bssid[IEEE80211_ADDR_LEN], struct ieee80211_channel *chan)
   1540 {
   1541 	struct ieee80211com *ic = vap->iv_ic;
   1542 	struct ieee80211_node *ni;
   1543 
   1544 	/* XXX check if node already in sta table? */
   1545 	ni = ieee80211_alloc_node(&ic->ic_sta, vap, bssid);
   1546 	if (ni != NULL) {
   1547 		ni->ni_wdsvap = vap;
   1548 		IEEE80211_ADDR_COPY(ni->ni_bssid, bssid);
   1549 		/*
   1550 		 * Inherit any manually configured settings.
   1551 		 */
   1552 		copy_bss(ni, vap->iv_bss);
   1553 		ieee80211_node_set_chan(ni, chan);
   1554 		/* NB: propagate ssid so available to WPA supplicant */
   1555 		ni->ni_esslen = vap->iv_des_ssid[0].len;
   1556 		memcpy(ni->ni_essid, vap->iv_des_ssid[0].ssid, ni->ni_esslen);
   1557 		/* NB: no associd for peer */
   1558 		/*
   1559 		 * There are no management frames to use to
   1560 		 * discover neighbor capabilities, so blindly
   1561 		 * propagate the local configuration.
   1562 		 */
   1563 		if (vap->iv_flags & IEEE80211_F_WME)
   1564 			ni->ni_flags |= IEEE80211_NODE_QOS;
   1565 #ifdef IEEE80211_SUPPORT_SUPERG
   1566 		if (vap->iv_flags & IEEE80211_F_FF)
   1567 			ni->ni_flags |= IEEE80211_NODE_FF;
   1568 #endif
   1569 		/* XXX VHT */
   1570 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
   1571 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
   1572 			/*
   1573 			 * Device is HT-capable and HT is enabled for
   1574 			 * the vap; setup HT operation.  On return
   1575 			 * ni_chan will be adjusted to an HT channel.
   1576 			 */
   1577 			ieee80211_ht_wds_init(ni);
   1578 			if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
   1579 				printf("%s: TODO: vht_wds_init\n", __func__);
   1580 			}
   1581 		} else {
   1582 			struct ieee80211_channel *c = ni->ni_chan;
   1583 			/*
   1584 			 * Force a legacy channel to be used.
   1585 			 */
   1586 			c = ieee80211_find_channel(ic,
   1587 			    c->ic_freq, c->ic_flags &~ IEEE80211_CHAN_HT);
   1588 			KASSERT(c != NULL, ("no legacy channel, %u/%x",
   1589 			    ni->ni_chan->ic_freq, ni->ni_chan->ic_flags));
   1590 			ni->ni_chan = c;
   1591 		}
   1592 	}
   1593 	return ni;
   1594 }
   1595 
   1596 struct ieee80211_node *
   1597 #ifdef IEEE80211_DEBUG_REFCNT
   1598 ieee80211_find_node_locked_debug(struct ieee80211_node_table *nt,
   1599 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
   1600 #else
   1601 ieee80211_find_node_locked(struct ieee80211_node_table *nt,
   1602 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1603 #endif
   1604 {
   1605 	struct ieee80211_node *ni;
   1606 	int hash;
   1607 
   1608 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1609 
   1610 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
   1611 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1612 		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
   1613 			ieee80211_ref_node(ni);	/* mark referenced */
   1614 #ifdef IEEE80211_DEBUG_REFCNT
   1615 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   1616 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1617 			    func, line,
   1618 			    ni, ether_sprintf(ni->ni_macaddr),
   1619 			    ieee80211_node_refcnt(ni));
   1620 #endif
   1621 			return ni;
   1622 		}
   1623 	}
   1624 	return NULL;
   1625 }
   1626 
   1627 struct ieee80211_node *
   1628 #ifdef IEEE80211_DEBUG_REFCNT
   1629 ieee80211_find_node_debug(struct ieee80211_node_table *nt,
   1630 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
   1631 #else
   1632 ieee80211_find_node(struct ieee80211_node_table *nt,
   1633 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1634 #endif
   1635 {
   1636 	struct ieee80211_node *ni;
   1637 
   1638 	IEEE80211_NODE_LOCK(nt);
   1639 	ni = ieee80211_find_node_locked(nt, macaddr);
   1640 	IEEE80211_NODE_UNLOCK(nt);
   1641 	return ni;
   1642 }
   1643 
   1644 struct ieee80211_node *
   1645 #ifdef IEEE80211_DEBUG_REFCNT
   1646 ieee80211_find_vap_node_locked_debug(struct ieee80211_node_table *nt,
   1647 	const struct ieee80211vap *vap,
   1648 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
   1649 #else
   1650 ieee80211_find_vap_node_locked(struct ieee80211_node_table *nt,
   1651 	const struct ieee80211vap *vap,
   1652 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1653 #endif
   1654 {
   1655 	struct ieee80211_node *ni;
   1656 	int hash;
   1657 
   1658 	IEEE80211_NODE_LOCK_ASSERT(nt);
   1659 
   1660 	hash = IEEE80211_NODE_HASH(nt->nt_ic, macaddr);
   1661 	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
   1662 		if (ni->ni_vap == vap &&
   1663 		    IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
   1664 			ieee80211_ref_node(ni);	/* mark referenced */
   1665 #ifdef IEEE80211_DEBUG_REFCNT
   1666 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   1667 			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
   1668 			    func, line,
   1669 			    ni, ether_sprintf(ni->ni_macaddr),
   1670 			    ieee80211_node_refcnt(ni));
   1671 #endif
   1672 			return ni;
   1673 		}
   1674 	}
   1675 	return NULL;
   1676 }
   1677 
   1678 struct ieee80211_node *
   1679 #ifdef IEEE80211_DEBUG_REFCNT
   1680 ieee80211_find_vap_node_debug(struct ieee80211_node_table *nt,
   1681 	const struct ieee80211vap *vap,
   1682 	const uint8_t macaddr[IEEE80211_ADDR_LEN], const char *func, int line)
   1683 #else
   1684 ieee80211_find_vap_node(struct ieee80211_node_table *nt,
   1685 	const struct ieee80211vap *vap,
   1686 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1687 #endif
   1688 {
   1689 	struct ieee80211_node *ni;
   1690 
   1691 	IEEE80211_NODE_LOCK(nt);
   1692 	ni = ieee80211_find_vap_node_locked(nt, vap, macaddr);
   1693 	IEEE80211_NODE_UNLOCK(nt);
   1694 	return ni;
   1695 }
   1696 
   1697 /*
   1698  * Fake up a node; this handles node discovery in adhoc mode.
   1699  * Note that for the driver's benefit we we treat this like
   1700  * an association so the driver has an opportunity to setup
   1701  * it's private state.
   1702  */
   1703 struct ieee80211_node *
   1704 ieee80211_fakeup_adhoc_node(struct ieee80211vap *vap,
   1705 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   1706 {
   1707 	struct ieee80211_node *ni;
   1708 
   1709 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE | IEEE80211_MSG_ASSOC,
   1710 	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
   1711 	ni = ieee80211_dup_bss(vap, macaddr);
   1712 	if (ni != NULL) {
   1713 		struct ieee80211com *ic = vap->iv_ic;
   1714 
   1715 		/* XXX no rate negotiation; just dup */
   1716 		ni->ni_rates = vap->iv_bss->ni_rates;
   1717 		if (ieee80211_iserp_rateset(&ni->ni_rates))
   1718 			ni->ni_flags |= IEEE80211_NODE_ERP;
   1719 		if (vap->iv_opmode == IEEE80211_M_AHDEMO) {
   1720 			/*
   1721 			 * In adhoc demo mode there are no management
   1722 			 * frames to use to discover neighbor capabilities,
   1723 			 * so blindly propagate the local configuration
   1724 			 * so we can do interesting things (e.g. use
   1725 			 * WME to disable ACK's).
   1726 			 */
   1727 			/*
   1728 			 * XXX TODO: 11n?
   1729 			 */
   1730 			if (vap->iv_flags & IEEE80211_F_WME)
   1731 				ni->ni_flags |= IEEE80211_NODE_QOS;
   1732 #ifdef IEEE80211_SUPPORT_SUPERG
   1733 			if (vap->iv_flags & IEEE80211_F_FF)
   1734 				ni->ni_flags |= IEEE80211_NODE_FF;
   1735 #endif
   1736 		}
   1737 		ieee80211_node_setuptxparms(ni);
   1738 		ieee80211_ratectl_node_init(ni);
   1739 
   1740 		/*
   1741 		 * XXX TODO: 11n? At least 20MHz, at least A-MPDU RX,
   1742 		 * not A-MPDU TX; not 11n rates, etc.  We'll cycle
   1743 		 * that after we hear that we can indeed do 11n
   1744 		 * (either by a beacon frame or by a probe response.)
   1745 		 */
   1746 
   1747 		/*
   1748 		 * This is the first time we see the node.
   1749 		 */
   1750 		if (ic->ic_newassoc != NULL)
   1751 			ic->ic_newassoc(ni, 1);
   1752 
   1753 		/*
   1754 		 * Kick off a probe request to the given node;
   1755 		 * we will then use the probe response to update
   1756 		 * 11n/etc configuration state.
   1757 		 *
   1758 		 * XXX TODO: this isn't guaranteed, and until we get
   1759 		 * a probe response, we won't be able to actually
   1760 		 * do anything 802.11n related to the node.
   1761 		 * So if this does indeed work, maybe we should hold
   1762 		 * off on sending responses until we get the probe
   1763 		 * response, or just default to some sensible subset
   1764 		 * of 802.11n behaviour (eg always allow aggregation
   1765 		 * negotiation TO us, but not FROM us, etc) so we
   1766 		 * aren't entirely busted.
   1767 		 */
   1768 		if (vap->iv_opmode == IEEE80211_M_IBSS) {
   1769 			ieee80211_send_probereq(ni, /* node */
   1770 				vap->iv_myaddr, /* SA */
   1771 				ni->ni_macaddr, /* DA */
   1772 				vap->iv_bss->ni_bssid, /* BSSID */
   1773 				vap->iv_bss->ni_essid,
   1774 				vap->iv_bss->ni_esslen); /* SSID */
   1775 		}
   1776 
   1777 		/* XXX not right for 802.1x/WPA */
   1778 		ieee80211_node_authorize(ni);
   1779 	}
   1780 	return ni;
   1781 }
   1782 
   1783 void
   1784 ieee80211_init_neighbor(struct ieee80211_node *ni,
   1785 	const struct ieee80211_frame *wh,
   1786 	const struct ieee80211_scanparams *sp)
   1787 {
   1788 	int do_ht_setup = 0, do_vht_setup = 0;
   1789 
   1790 	ni->ni_esslen = sp->ssid[1];
   1791 	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
   1792 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
   1793 	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
   1794 	ni->ni_intval = sp->bintval;
   1795 	ni->ni_capinfo = sp->capinfo;
   1796 	ni->ni_chan = ni->ni_ic->ic_curchan;
   1797 	ni->ni_fhdwell = sp->fhdwell;
   1798 	ni->ni_fhindex = sp->fhindex;
   1799 	ni->ni_erp = sp->erp;
   1800 	ni->ni_timoff = sp->timoff;
   1801 #ifdef IEEE80211_SUPPORT_MESH
   1802 	if (ni->ni_vap->iv_opmode == IEEE80211_M_MBSS)
   1803 		ieee80211_mesh_init_neighbor(ni, wh, sp);
   1804 #endif
   1805 	if (ieee80211_ies_init(&ni->ni_ies, sp->ies, sp->ies_len)) {
   1806 		ieee80211_ies_expand(&ni->ni_ies);
   1807 		if (ni->ni_ies.wme_ie != NULL)
   1808 			ni->ni_flags |= IEEE80211_NODE_QOS;
   1809 		else
   1810 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
   1811 #ifdef IEEE80211_SUPPORT_SUPERG
   1812 		if (ni->ni_ies.ath_ie != NULL)
   1813 			ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
   1814 #endif
   1815 		if (ni->ni_ies.htcap_ie != NULL)
   1816 			ieee80211_parse_htcap(ni, ni->ni_ies.htcap_ie);
   1817 		if (ni->ni_ies.htinfo_ie != NULL)
   1818 			ieee80211_parse_htinfo(ni, ni->ni_ies.htinfo_ie);
   1819 
   1820 		if (ni->ni_ies.vhtcap_ie != NULL)
   1821 			ieee80211_parse_vhtcap(ni, ni->ni_ies.vhtcap_ie);
   1822 		if (ni->ni_ies.vhtopmode_ie != NULL)
   1823 			ieee80211_parse_vhtopmode(ni, ni->ni_ies.vhtopmode_ie);
   1824 
   1825 		if ((ni->ni_ies.htcap_ie != NULL) &&
   1826 		    (ni->ni_ies.htinfo_ie != NULL) &&
   1827 		    (ni->ni_vap->iv_flags_ht & IEEE80211_FHT_HT)) {
   1828 			do_ht_setup = 1;
   1829 		}
   1830 
   1831 		if ((ni->ni_ies.vhtcap_ie != NULL) &&
   1832 		    (ni->ni_ies.vhtopmode_ie != NULL) &&
   1833 		    (ni->ni_vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
   1834 			do_vht_setup = 1;
   1835 		}
   1836 
   1837 	}
   1838 
   1839 	/* NB: must be after ni_chan is setup */
   1840 	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
   1841 		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
   1842 		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
   1843 
   1844 	/*
   1845 	 * If the neighbor is HT compatible, flip that on.
   1846 	 */
   1847 	if (do_ht_setup) {
   1848 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
   1849 		    "%s: doing HT setup\n", __func__);
   1850 		ieee80211_ht_node_init(ni);
   1851 		ieee80211_ht_updateparams(ni,
   1852 		    ni->ni_ies.htcap_ie,
   1853 		    ni->ni_ies.htinfo_ie);
   1854 
   1855 		if (do_vht_setup) {
   1856 			if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   1857 #if __FreeBSD__
   1858 				printf("%s: BSS %6D: 2GHz channel, VHT info; ignoring\n",
   1859 				    __func__,
   1860 				    ni->ni_macaddr,
   1861 				    ":");
   1862 #elif __NetBSD__
   1863 				printf("%s: BSS %6ld: 2GHz channel, VHT info; ignoring\n",
   1864 				    __func__, (long int)ni->ni_macaddr);
   1865 #endif
   1866 			} else {
   1867 				ieee80211_vht_node_init(ni);
   1868 				ieee80211_vht_updateparams(ni,
   1869 				    ni->ni_ies.vhtcap_ie,
   1870 				    ni->ni_ies.vhtopmode_ie);
   1871 				ieee80211_setup_vht_rates(ni,
   1872 				    ni->ni_ies.vhtcap_ie,
   1873 				    ni->ni_ies.vhtopmode_ie);
   1874 			}
   1875 		}
   1876 
   1877 		/*
   1878 		 * Finally do the channel upgrade/change based
   1879 		 * on the HT/VHT configuration.
   1880 		 */
   1881 		ieee80211_ht_updateparams_final(ni, ni->ni_ies.htcap_ie,
   1882 		    ni->ni_ies.htinfo_ie);
   1883 		ieee80211_setup_htrates(ni,
   1884 		    ni->ni_ies.htcap_ie,
   1885 		    IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
   1886 		ieee80211_setup_basic_htrates(ni,
   1887 		    ni->ni_ies.htinfo_ie);
   1888 
   1889 		ieee80211_node_setuptxparms(ni);
   1890 		ieee80211_ratectl_node_init(ni);
   1891 
   1892 		/* Reassociate; we're now 11n/11ac */
   1893 		/*
   1894 		 * XXX TODO: this is the wrong thing to do -
   1895 		 * we're calling it with isnew=1 so the ath(4)
   1896 		 * driver reinitialises the rate tables.
   1897 		 * This "mostly" works for ath(4), but it won't
   1898 		 * be right for firmware devices which allocate
   1899 		 * node states.
   1900 		 *
   1901 		 * So, do we just create a new node and delete
   1902 		 * the old one? Or?
   1903 		 */
   1904 		if (ni->ni_ic->ic_newassoc)
   1905 			ni->ni_ic->ic_newassoc(ni, 1);
   1906 	}
   1907 }
   1908 
   1909 /*
   1910  * Do node discovery in adhoc mode on receipt of a beacon
   1911  * or probe response frame.  Note that for the driver's
   1912  * benefit we we treat this like an association so the
   1913  * driver has an opportunity to setup it's private state.
   1914  */
   1915 struct ieee80211_node *
   1916 ieee80211_add_neighbor(struct ieee80211vap *vap,
   1917 	const struct ieee80211_frame *wh,
   1918 	const struct ieee80211_scanparams *sp)
   1919 {
   1920 	struct ieee80211_node *ni;
   1921 
   1922 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ASSOC,
   1923 	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
   1924 	ni = ieee80211_dup_bss(vap, wh->i_addr2);/* XXX alloc_node? */
   1925 	if (ni != NULL) {
   1926 		struct ieee80211com *ic = vap->iv_ic;
   1927 
   1928 		ieee80211_init_neighbor(ni, wh, sp);
   1929 		if (ieee80211_iserp_rateset(&ni->ni_rates))
   1930 			ni->ni_flags |= IEEE80211_NODE_ERP;
   1931 		ieee80211_node_setuptxparms(ni);
   1932 		ieee80211_ratectl_node_init(ni);
   1933 		if (ic->ic_newassoc != NULL)
   1934 			ic->ic_newassoc(ni, 1);
   1935 		/* XXX not right for 802.1x/WPA */
   1936 		ieee80211_node_authorize(ni);
   1937 	}
   1938 	return ni;
   1939 }
   1940 
   1941 #define	IS_PROBEREQ(wh) \
   1942 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK|IEEE80211_FC0_SUBTYPE_MASK)) \
   1943 	    == (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ))
   1944 #define	IS_BCAST_PROBEREQ(wh) \
   1945 	(IS_PROBEREQ(wh) && IEEE80211_IS_MULTICAST( \
   1946 	    ((const struct ieee80211_frame *)(wh))->i_addr3))
   1947 
   1948 static __inline struct ieee80211_node *
   1949 _find_rxnode(struct ieee80211_node_table *nt,
   1950     const struct ieee80211_frame_min *wh)
   1951 {
   1952 	if (IS_BCAST_PROBEREQ(wh))
   1953 		return NULL;		/* spam bcast probe req to all vap's */
   1954 	return ieee80211_find_node_locked(nt, wh->i_addr2);
   1955 }
   1956 
   1957 /*
   1958  * Locate the node for sender, track state, and then pass the
   1959  * (referenced) node up to the 802.11 layer for its use.  Note
   1960  * we can return NULL if the sender is not in the table.
   1961  */
   1962 struct ieee80211_node *
   1963 #ifdef IEEE80211_DEBUG_REFCNT
   1964 ieee80211_find_rxnode_debug(struct ieee80211com *ic,
   1965 	const struct ieee80211_frame_min *wh, const char *func, int line)
   1966 #else
   1967 ieee80211_find_rxnode(struct ieee80211com *ic,
   1968 	const struct ieee80211_frame_min *wh)
   1969 #endif
   1970 {
   1971 	struct ieee80211_node_table *nt;
   1972 	struct ieee80211_node *ni;
   1973 
   1974 	nt = &ic->ic_sta;
   1975 	IEEE80211_NODE_LOCK(nt);
   1976 	ni = _find_rxnode(nt, wh);
   1977 	IEEE80211_NODE_UNLOCK(nt);
   1978 
   1979 	return ni;
   1980 }
   1981 
   1982 /*
   1983  * Like ieee80211_find_rxnode but use the supplied h/w
   1984  * key index as a hint to locate the node in the key
   1985  * mapping table.  If an entry is present at the key
   1986  * index we return it; otherwise do a normal lookup and
   1987  * update the mapping table if the station has a unicast
   1988  * key assigned to it.
   1989  */
   1990 struct ieee80211_node *
   1991 #ifdef IEEE80211_DEBUG_REFCNT
   1992 ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
   1993 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
   1994 	const char *func, int line)
   1995 #else
   1996 ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
   1997 	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
   1998 #endif
   1999 {
   2000 	struct ieee80211_node_table *nt;
   2001 	struct ieee80211_node *ni;
   2002 
   2003 	nt = &ic->ic_sta;
   2004 	IEEE80211_NODE_LOCK(nt);
   2005 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
   2006 		ni = nt->nt_keyixmap[keyix];
   2007 	else
   2008 		ni = NULL;
   2009 	if (ni == NULL) {
   2010 		ni = _find_rxnode(nt, wh);
   2011 		if (ni != NULL && nt->nt_keyixmap != NULL) {
   2012 			/*
   2013 			 * If the station has a unicast key cache slot
   2014 			 * assigned update the key->node mapping table.
   2015 			 */
   2016 			keyix = ni->ni_ucastkey.wk_rxkeyix;
   2017 			/* XXX can keyixmap[keyix] != NULL? */
   2018 			if (keyix < nt->nt_keyixmax &&
   2019 			    nt->nt_keyixmap[keyix] == NULL) {
   2020 				IEEE80211_DPRINTF(ni->ni_vap,
   2021 				    IEEE80211_MSG_NODE,
   2022 				    "%s: add key map entry %p<%s> refcnt %d\n",
   2023 				    __func__, ni, ether_sprintf(ni->ni_macaddr),
   2024 				    ieee80211_node_refcnt(ni)+1);
   2025 				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
   2026 			}
   2027 		}
   2028 	} else {
   2029 		if (IS_BCAST_PROBEREQ(wh))
   2030 			ni = NULL;	/* spam bcast probe req to all vap's */
   2031 		else
   2032 			ieee80211_ref_node(ni);
   2033 	}
   2034 	IEEE80211_NODE_UNLOCK(nt);
   2035 
   2036 	return ni;
   2037 }
   2038 #undef IS_BCAST_PROBEREQ
   2039 #undef IS_PROBEREQ
   2040 
   2041 /*
   2042  * Return a reference to the appropriate node for sending
   2043  * a data frame.  This handles node discovery in adhoc networks.
   2044  */
   2045 struct ieee80211_node *
   2046 #ifdef IEEE80211_DEBUG_REFCNT
   2047 ieee80211_find_txnode_debug(struct ieee80211vap *vap,
   2048 	const uint8_t macaddr[IEEE80211_ADDR_LEN],
   2049 	const char *func, int line)
   2050 #else
   2051 ieee80211_find_txnode(struct ieee80211vap *vap,
   2052 	const uint8_t macaddr[IEEE80211_ADDR_LEN])
   2053 #endif
   2054 {
   2055 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
   2056 	struct ieee80211_node *ni;
   2057 
   2058 	/*
   2059 	 * The destination address should be in the node table
   2060 	 * unless this is a multicast/broadcast frame.  We can
   2061 	 * also optimize station mode operation, all frames go
   2062 	 * to the bss node.
   2063 	 */
   2064 	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
   2065 	IEEE80211_NODE_LOCK(nt);
   2066 	if (vap->iv_opmode == IEEE80211_M_STA ||
   2067 	    vap->iv_opmode == IEEE80211_M_WDS ||
   2068 	    IEEE80211_IS_MULTICAST(macaddr))
   2069 		ni = ieee80211_ref_node(vap->iv_bss);
   2070 	else
   2071 		ni = ieee80211_find_node_locked(nt, macaddr);
   2072 	IEEE80211_NODE_UNLOCK(nt);
   2073 
   2074 	if (ni == NULL) {
   2075 		if (vap->iv_opmode == IEEE80211_M_IBSS ||
   2076 		    vap->iv_opmode == IEEE80211_M_AHDEMO) {
   2077 			/*
   2078 			 * In adhoc mode cons up a node for the destination.
   2079 			 * Note that we need an additional reference for the
   2080 			 * caller to be consistent with
   2081 			 * ieee80211_find_node_locked.
   2082 			 */
   2083 			/*
   2084 			 * XXX TODO: this doesn't fake up 11n state; we need
   2085 			 * to find another way to get it upgraded.
   2086 			 */
   2087 			ni = ieee80211_fakeup_adhoc_node(vap, macaddr);
   2088 			if (ni != NULL)
   2089 				(void) ieee80211_ref_node(ni);
   2090 		} else {
   2091 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, macaddr,
   2092 			    "no node, discard frame (%s)", __func__);
   2093 			vap->iv_stats.is_tx_nonode++;
   2094 		}
   2095 	}
   2096 	return ni;
   2097 }
   2098 
   2099 static void
   2100 _ieee80211_free_node(struct ieee80211_node *ni)
   2101 {
   2102 	struct ieee80211_node_table *nt = ni->ni_table;
   2103 
   2104 	/*
   2105 	 * NB: careful about referencing the vap as it may be
   2106 	 * gone if the last reference was held by a driver.
   2107 	 * We know the com will always be present so it's safe
   2108 	 * to use ni_ic below to reclaim resources.
   2109 	 */
   2110 #if 0
   2111 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   2112 		"%s %p<%s> in %s table\n", __func__, ni,
   2113 		ether_sprintf(ni->ni_macaddr),
   2114 		nt != NULL ? nt->nt_name : "<gone>");
   2115 #endif
   2116 	if (ni->ni_associd != 0) {
   2117 		struct ieee80211vap *vap = ni->ni_vap;
   2118 		if (vap->iv_aid_bitmap != NULL)
   2119 			IEEE80211_AID_CLR(vap, ni->ni_associd);
   2120 	}
   2121 	if (nt != NULL)
   2122 		ieee80211_del_node_nt(nt, ni);
   2123 	ni->ni_ic->ic_node_free(ni);
   2124 }
   2125 
   2126 /*
   2127  * Clear any entry in the unicast key mapping table.
   2128  */
   2129 static int
   2130 node_clear_keyixmap(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   2131 {
   2132 	ieee80211_keyix keyix;
   2133 
   2134 	keyix = ni->ni_ucastkey.wk_rxkeyix;
   2135 	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
   2136 	    nt->nt_keyixmap[keyix] == ni) {
   2137 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   2138 			"%s: %p<%s> clear key map entry %u\n",
   2139 			__func__, ni, ether_sprintf(ni->ni_macaddr), keyix);
   2140 		nt->nt_keyixmap[keyix] = NULL;
   2141 		ieee80211_node_decref(ni);
   2142 		return 1;
   2143 	}
   2144 
   2145 	return 0;
   2146 }
   2147 
   2148 void
   2149 #ifdef IEEE80211_DEBUG_REFCNT
   2150 ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
   2151 #else
   2152 ieee80211_free_node(struct ieee80211_node *ni)
   2153 #endif
   2154 {
   2155 	struct ieee80211_node_table *nt = ni->ni_table;
   2156 
   2157 #ifdef IEEE80211_DEBUG_REFCNT
   2158 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   2159 		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
   2160 		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
   2161 #endif
   2162 	if (nt != NULL) {
   2163 		IEEE80211_NODE_LOCK(nt);
   2164 		if (ieee80211_node_dectestref(ni)) {
   2165 			/*
   2166 			 * Last reference, reclaim state.
   2167 			 */
   2168 			_ieee80211_free_node(ni);
   2169 		} else if (ieee80211_node_refcnt(ni) == 1)
   2170 			if (node_clear_keyixmap(nt, ni))
   2171 				_ieee80211_free_node(ni);
   2172 		IEEE80211_NODE_UNLOCK(nt);
   2173 	} else {
   2174 		if (ieee80211_node_dectestref(ni))
   2175 			_ieee80211_free_node(ni);
   2176 	}
   2177 }
   2178 
   2179 /*
   2180  * Reclaim a unicast key and clear any key cache state.
   2181  */
   2182 int
   2183 ieee80211_node_delucastkey(struct ieee80211_node *ni)
   2184 {
   2185 	struct ieee80211com *ic = ni->ni_ic;
   2186 	struct ieee80211_node_table *nt = &ic->ic_sta;
   2187 	struct ieee80211_node *nikey;
   2188 	ieee80211_keyix keyix;
   2189 	int isowned, status;
   2190 
   2191 	/*
   2192 	 * NB: We must beware of LOR here; deleting the key
   2193 	 * can cause the crypto layer to block traffic updates
   2194 	 * which can generate a LOR against the node table lock;
   2195 	 * grab it here and stash the key index for our use below.
   2196 	 *
   2197 	 * Must also beware of recursion on the node table lock.
   2198 	 * When called from node_cleanup we may already have
   2199 	 * the node table lock held.  Unfortunately there's no
   2200 	 * way to separate out this path so we must do this
   2201 	 * conditionally.
   2202 	 */
   2203 	isowned = IEEE80211_NODE_IS_LOCKED(nt);
   2204 	if (!isowned)
   2205 		IEEE80211_NODE_LOCK(nt);
   2206 	nikey = NULL;
   2207 	status = 1;		/* NB: success */
   2208 	if (ni->ni_ucastkey.wk_keyix != IEEE80211_KEYIX_NONE) {
   2209 		keyix = ni->ni_ucastkey.wk_rxkeyix;
   2210 		status = ieee80211_crypto_delkey(ni->ni_vap, &ni->ni_ucastkey);
   2211 		if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
   2212 			nikey = nt->nt_keyixmap[keyix];
   2213 			nt->nt_keyixmap[keyix] = NULL;
   2214 		}
   2215 	}
   2216 	if (!isowned)
   2217 		IEEE80211_NODE_UNLOCK(nt);
   2218 
   2219 	if (nikey != NULL) {
   2220 		KASSERT(nikey == ni,
   2221 			("key map out of sync, ni %p nikey %p", ni, nikey));
   2222 		IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   2223 			"%s: delete key map entry %p<%s> refcnt %d\n",
   2224 			__func__, ni, ether_sprintf(ni->ni_macaddr),
   2225 			ieee80211_node_refcnt(ni)-1);
   2226 		ieee80211_free_node(ni);
   2227 	}
   2228 	return status;
   2229 }
   2230 
   2231 /*
   2232  * Reclaim a node.  If this is the last reference count then
   2233  * do the normal free work.  Otherwise remove it from the node
   2234  * table and mark it gone by clearing the back-reference.
   2235  */
   2236 static void
   2237 node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   2238 {
   2239 
   2240 	IEEE80211_NODE_LOCK_ASSERT(nt);
   2241 
   2242 	IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_NODE,
   2243 		"%s: remove %p<%s> from %s table, refcnt %d\n",
   2244 		__func__, ni, ether_sprintf(ni->ni_macaddr),
   2245 		nt->nt_name, ieee80211_node_refcnt(ni)-1);
   2246 	/*
   2247 	 * Clear any entry in the unicast key mapping table.
   2248 	 * We need to do it here so rx lookups don't find it
   2249 	 * in the mapping table even if it's not in the hash
   2250 	 * table.  We cannot depend on the mapping table entry
   2251 	 * being cleared because the node may not be free'd.
   2252 	 */
   2253 	(void)node_clear_keyixmap(nt, ni);
   2254 	if (!ieee80211_node_dectestref(ni)) {
   2255 		/*
   2256 		 * Other references are present, just remove the
   2257 		 * node from the table so it cannot be found.  When
   2258 		 * the references are dropped storage will be
   2259 		 * reclaimed.
   2260 		 */
   2261 		ieee80211_del_node_nt(nt, ni);
   2262 	} else
   2263 		_ieee80211_free_node(ni);
   2264 }
   2265 
   2266 /*
   2267  * Node table support.
   2268  */
   2269 
   2270 static void
   2271 ieee80211_node_table_init(struct ieee80211com *ic,
   2272 	struct ieee80211_node_table *nt,
   2273 	const char *name, int inact, int keyixmax)
   2274 {
   2275 
   2276 	nt->nt_ic = ic;
   2277 	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_name);
   2278 	TAILQ_INIT(&nt->nt_node);
   2279 	nt->nt_count = 0;
   2280 	nt->nt_name = name;
   2281 	nt->nt_inact_init = inact;
   2282 	nt->nt_keyixmax = keyixmax;
   2283 	if (nt->nt_keyixmax > 0) {
   2284 		nt->nt_keyixmap = (struct ieee80211_node **) IEEE80211_MALLOC(
   2285 			keyixmax * sizeof(struct ieee80211_node *),
   2286 			M_80211_NODE,
   2287 			IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   2288 		if (nt->nt_keyixmap == NULL)
   2289 			ic_printf(ic,
   2290 			    "Cannot allocate key index map with %u entries\n",
   2291 			    keyixmax);
   2292 	} else
   2293 		nt->nt_keyixmap = NULL;
   2294 }
   2295 
   2296 static void
   2297 ieee80211_node_table_reset(struct ieee80211_node_table *nt,
   2298 	struct ieee80211vap *match)
   2299 {
   2300 	struct ieee80211_node *ni, *next;
   2301 
   2302 	IEEE80211_NODE_LOCK(nt);
   2303 	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next) {
   2304 		if (match != NULL && ni->ni_vap != match)
   2305 			continue;
   2306 		/* XXX can this happen?  if so need's work */
   2307 		if (ni->ni_associd != 0) {
   2308 			struct ieee80211vap *vap = ni->ni_vap;
   2309 
   2310 			if (vap->iv_auth->ia_node_leave != NULL)
   2311 				vap->iv_auth->ia_node_leave(ni);
   2312 			if (vap->iv_aid_bitmap != NULL)
   2313 				IEEE80211_AID_CLR(vap, ni->ni_associd);
   2314 		}
   2315 		ni->ni_wdsvap = NULL;		/* clear reference */
   2316 		node_reclaim(nt, ni);
   2317 	}
   2318 	if (match != NULL && match->iv_opmode == IEEE80211_M_WDS) {
   2319 		/*
   2320 		 * Make a separate pass to clear references to this vap
   2321 		 * held by DWDS entries.  They will not be matched above
   2322 		 * because ni_vap will point to the ap vap but we still
   2323 		 * need to clear ni_wdsvap when the WDS vap is destroyed
   2324 		 * and/or reset.
   2325 		 */
   2326 		TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, next)
   2327 			if (ni->ni_wdsvap == match)
   2328 				ni->ni_wdsvap = NULL;
   2329 	}
   2330 	IEEE80211_NODE_UNLOCK(nt);
   2331 }
   2332 
   2333 static void
   2334 ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
   2335 {
   2336 	ieee80211_node_table_reset(nt, NULL);
   2337 	if (nt->nt_keyixmap != NULL) {
   2338 #ifdef DIAGNOSTIC
   2339 		/* XXX verify all entries are NULL */
   2340 		int i;
   2341 		for (i = 0; i < nt->nt_keyixmax; i++)
   2342 			if (nt->nt_keyixmap[i] != NULL)
   2343 				printf("%s: %s[%u] still active\n", __func__,
   2344 					nt->nt_name, i);
   2345 #endif
   2346 		IEEE80211_FREE(nt->nt_keyixmap, M_80211_NODE);
   2347 		nt->nt_keyixmap = NULL;
   2348 	}
   2349 	IEEE80211_NODE_LOCK_DESTROY(nt);
   2350 }
   2351 
   2352 static void
   2353 timeout_stations(void *arg __unused, struct ieee80211_node *ni)
   2354 {
   2355 	struct ieee80211com *ic = ni->ni_ic;
   2356 	struct ieee80211vap *vap = ni->ni_vap;
   2357 
   2358 	/*
   2359 	 * Only process stations when in RUN state.  This
   2360 	 * insures, for example, that we don't timeout an
   2361 	 * inactive station during CAC.  Note that CSA state
   2362 	 * is actually handled in ieee80211_node_timeout as
   2363 	 * it applies to more than timeout processing.
   2364 	 */
   2365 	if (vap->iv_state != IEEE80211_S_RUN)
   2366 		return;
   2367 	/*
   2368 	 * Ignore entries for which have yet to receive an
   2369 	 * authentication frame.  These are transient and
   2370 	 * will be reclaimed when the last reference to them
   2371 	 * goes away (when frame xmits complete).
   2372 	 */
   2373 	if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
   2374 	     vap->iv_opmode == IEEE80211_M_STA) &&
   2375 	    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
   2376 		return;
   2377 	/*
   2378 	 * Free fragment if not needed anymore
   2379 	 * (last fragment older than 1s).
   2380 	 * XXX doesn't belong here, move to node_age
   2381 	 */
   2382 	if (ni->ni_rxfrag[0] != NULL &&
   2383 	    ticks > ni->ni_rxfragstamp + hz) {
   2384 		m_freem(ni->ni_rxfrag[0]);
   2385 		ni->ni_rxfrag[0] = NULL;
   2386 	}
   2387 	if (ni->ni_inact > 0) {
   2388 		ni->ni_inact--;
   2389 		IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
   2390 		    "%s: inact %u inact_reload %u nrates %u",
   2391 		    __func__, ni->ni_inact, ni->ni_inact_reload,
   2392 		    ni->ni_rates.rs_nrates);
   2393 	}
   2394 	/*
   2395 	 * Special case ourself; we may be idle for extended periods
   2396 	 * of time and regardless reclaiming our state is wrong.
   2397 	 * XXX run ic_node_age
   2398 	 */
   2399 	/* XXX before inact decrement? */
   2400 	if (ni == vap->iv_bss)
   2401 		return;
   2402 	if (ni->ni_associd != 0 ||
   2403 	    (vap->iv_opmode == IEEE80211_M_IBSS ||
   2404 	     vap->iv_opmode == IEEE80211_M_AHDEMO)) {
   2405 		/*
   2406 		 * Age/drain resources held by the station.
   2407 		 */
   2408 		ic->ic_node_age(ni);
   2409 		/*
   2410 		 * Probe the station before time it out.  We
   2411 		 * send a null data frame which may not be
   2412 		 * universally supported by drivers (need it
   2413 		 * for ps-poll support so it should be...).
   2414 		 *
   2415 		 * XXX don't probe the station unless we've
   2416 		 *     received a frame from them (and have
   2417 		 *     some idea of the rates they are capable
   2418 		 *     of); this will get fixed more properly
   2419 		 *     soon with better handling of the rate set.
   2420 		 */
   2421 		if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
   2422 		    (0 < ni->ni_inact &&
   2423 		     ni->ni_inact <= vap->iv_inact_probe) &&
   2424 		    ni->ni_rates.rs_nrates != 0) {
   2425 			IEEE80211_NOTE(vap,
   2426 			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
   2427 			    ni, "%s",
   2428 			    "probe station due to inactivity");
   2429 			/*
   2430 			 * Grab a reference so the node cannot
   2431 			 * be reclaimed before we send the frame.
   2432 			 * ieee80211_send_nulldata understands
   2433 			 * we've done this and reclaims the
   2434 			 * ref for us as needed.
   2435 			 */
   2436 			/* XXX fix this (not required anymore). */
   2437 			ieee80211_ref_node(ni);
   2438 			/* XXX useless */
   2439 			ieee80211_send_nulldata(ni);
   2440 			/* XXX stat? */
   2441 			return;
   2442 		}
   2443 	}
   2444 	if ((vap->iv_flags_ext & IEEE80211_FEXT_INACT) &&
   2445 	    ni->ni_inact <= 0) {
   2446 		IEEE80211_NOTE(vap,
   2447 		    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
   2448 		    "station timed out due to inactivity "
   2449 		    "(refcnt %u)", ieee80211_node_refcnt(ni));
   2450 		/*
   2451 		 * Send a deauthenticate frame and drop the station.
   2452 		 * This is somewhat complicated due to reference counts
   2453 		 * and locking.  At this point a station will typically
   2454 		 * have a reference count of 2.  ieee80211_node_leave
   2455 		 * will do a "free" of the node which will drop the
   2456 		 * reference count.  But in the meantime a reference
   2457 		 * wil be held by the deauth frame.  The actual reclaim
   2458 		 * of the node will happen either after the tx is
   2459 		 * completed or by ieee80211_node_leave.
   2460 		 */
   2461 		if (ni->ni_associd != 0) {
   2462 			IEEE80211_SEND_MGMT(ni,
   2463 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
   2464 			    IEEE80211_REASON_AUTH_EXPIRE);
   2465 		}
   2466 		ieee80211_node_leave(ni);
   2467 		vap->iv_stats.is_node_timeout++;
   2468 	}
   2469 }
   2470 
   2471 /*
   2472  * Timeout inactive stations and do related housekeeping.
   2473  */
   2474 static void
   2475 ieee80211_timeout_stations(struct ieee80211com *ic)
   2476 {
   2477 	struct ieee80211_node_table *nt = &ic->ic_sta;
   2478 
   2479 	ieee80211_iterate_nodes(nt, timeout_stations, NULL);
   2480 }
   2481 
   2482 /*
   2483  * Aggressively reclaim resources.  This should be used
   2484  * only in a critical situation to reclaim mbuf resources.
   2485  */
   2486 void
   2487 ieee80211_drain(struct ieee80211com *ic)
   2488 {
   2489 	struct ieee80211_node_table *nt = &ic->ic_sta;
   2490 	struct ieee80211vap *vap;
   2491 	struct ieee80211_node *ni;
   2492 
   2493 	IEEE80211_NODE_LOCK(nt);
   2494 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   2495 		/*
   2496 		 * Ignore entries for which have yet to receive an
   2497 		 * authentication frame.  These are transient and
   2498 		 * will be reclaimed when the last reference to them
   2499 		 * goes away (when frame xmits complete).
   2500 		 */
   2501 		vap = ni->ni_vap;
   2502 		/*
   2503 		 * Only process stations when in RUN state.  This
   2504 		 * insures, for example, that we don't timeout an
   2505 		 * inactive station during CAC.  Note that CSA state
   2506 		 * is actually handled in ieee80211_node_timeout as
   2507 		 * it applies to more than timeout processing.
   2508 		 */
   2509 		if (vap->iv_state != IEEE80211_S_RUN)
   2510 			continue;
   2511 		/* XXX can vap be NULL? */
   2512 		if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
   2513 		     vap->iv_opmode == IEEE80211_M_STA) &&
   2514 		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
   2515 			continue;
   2516 		/*
   2517 		 * Free fragments.
   2518 		 * XXX doesn't belong here, move to node_drain
   2519 		 */
   2520 		if (ni->ni_rxfrag[0] != NULL) {
   2521 			m_freem(ni->ni_rxfrag[0]);
   2522 			ni->ni_rxfrag[0] = NULL;
   2523 		}
   2524 		/*
   2525 		 * Drain resources held by the station.
   2526 		 */
   2527 		ic->ic_node_drain(ni);
   2528 	}
   2529 	IEEE80211_NODE_UNLOCK(nt);
   2530 }
   2531 
   2532 /*
   2533  * Per-ieee80211com inactivity timer callback.
   2534  */
   2535 void
   2536 ieee80211_node_timeout(void *arg)
   2537 {
   2538 	struct ieee80211com *ic = arg;
   2539 
   2540 	/*
   2541 	 * Defer timeout processing if a channel switch is pending.
   2542 	 * We typically need to be mute so not doing things that
   2543 	 * might generate frames is good to handle in one place.
   2544 	 * Suppressing the station timeout processing may extend the
   2545 	 * lifetime of inactive stations (by not decrementing their
   2546 	 * idle counters) but this should be ok unless the CSA is
   2547 	 * active for an unusually long time.
   2548 	 */
   2549 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
   2550 		ieee80211_scan_timeout(ic);
   2551 		ieee80211_timeout_stations(ic);
   2552 		ieee80211_ageq_age(&ic->ic_stageq, IEEE80211_INACT_WAIT);
   2553 
   2554 		IEEE80211_LOCK(ic);
   2555 		ieee80211_erp_timeout(ic);
   2556 		ieee80211_ht_timeout(ic);
   2557 		ieee80211_vht_timeout(ic);
   2558 		IEEE80211_UNLOCK(ic);
   2559 	}
   2560 	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
   2561 		ieee80211_node_timeout, ic);
   2562 }
   2563 
   2564 /*
   2565  * The same as ieee80211_iterate_nodes(), but for one vap only.
   2566  */
   2567 int
   2568 ieee80211_iterate_nodes_vap(struct ieee80211_node_table *nt,
   2569     struct ieee80211vap *vap, ieee80211_iter_func *f, void *arg)
   2570 {
   2571 	struct ieee80211_node **ni_arr;
   2572 	struct ieee80211_node *ni;
   2573 	size_t size;
   2574 	int count, i;
   2575 
   2576 	/*
   2577 	 * Iterate over the node table and save an array of ref'ed nodes.
   2578 	 *
   2579 	 * This is separated out from calling the actual node function so that
   2580 	 * no LORs will occur.
   2581 	 */
   2582 	IEEE80211_NODE_LOCK(nt);
   2583 	count = nt->nt_count;
   2584 	size = count * sizeof(struct ieee80211_node *);
   2585 	ni_arr = (struct ieee80211_node **) IEEE80211_MALLOC(size, M_80211_NODE,
   2586 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   2587 	if (ni_arr == NULL) {
   2588 		IEEE80211_NODE_UNLOCK(nt);
   2589 		return (ENOMEM);
   2590 	}
   2591 
   2592 	i = 0;
   2593 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   2594 		if (vap != NULL && ni->ni_vap != vap)
   2595 			continue;
   2596 		KASSERT(i < count,
   2597 		    ("node array overflow (vap %p, i %d, count %d)\n",
   2598 		    vap, i, count));
   2599 		ni_arr[i] = ieee80211_ref_node(ni);
   2600 		i++;
   2601 	}
   2602 	IEEE80211_NODE_UNLOCK(nt);
   2603 
   2604 	for (i = 0; i < count; i++) {
   2605 		if (ni_arr[i] == NULL)	/* end of the list */
   2606 			break;
   2607 		(*f)(arg, ni_arr[i]);
   2608 		/* ieee80211_free_node() locks by itself */
   2609 		ieee80211_free_node(ni_arr[i]);
   2610 	}
   2611 
   2612 	IEEE80211_FREE(ni_arr, M_80211_NODE);
   2613 
   2614 	return (0);
   2615 }
   2616 
   2617 /*
   2618  * Just a wrapper, so we don't have to change every ieee80211_iterate_nodes()
   2619  * reference in the source.
   2620  */
   2621 void
   2622 ieee80211_iterate_nodes(struct ieee80211_node_table *nt,
   2623 	ieee80211_iter_func *f, void *arg)
   2624 {
   2625 	/* XXX no way to pass error to the caller. */
   2626 	(void) ieee80211_iterate_nodes_vap(nt, NULL, f, arg);
   2627 }
   2628 
   2629 void
   2630 ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
   2631 {
   2632 	printf("0x%p: mac %s refcnt %d\n", ni,
   2633 		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
   2634 	printf("\tauthmode %u flags 0x%x\n",
   2635 		ni->ni_authmode, ni->ni_flags);
   2636 	printf("\tassocid 0x%x txpower %u vlan %u\n",
   2637 		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
   2638 	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
   2639 		ni->ni_txseqs[IEEE80211_NONQOS_TID],
   2640 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
   2641 		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
   2642 		ni->ni_rxfragstamp);
   2643 	printf("\trssi %d noise %d intval %u capinfo 0x%x\n",
   2644 		node_getrssi(ni), ni->ni_noise,
   2645 		ni->ni_intval, ni->ni_capinfo);
   2646 	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
   2647 		ether_sprintf(ni->ni_bssid),
   2648 		ni->ni_esslen, ni->ni_essid,
   2649 		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
   2650 	printf("\tinact %u inact_reload %u txrate %u\n",
   2651 		ni->ni_inact, ni->ni_inact_reload, ni->ni_txrate);
   2652 	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
   2653 		ni->ni_htcap, ni->ni_htparam,
   2654 		ni->ni_htctlchan, ni->ni_ht2ndchan);
   2655 	printf("\thtopmode %x htstbc %x htchw %u\n",
   2656 		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
   2657 	printf("\tvhtcap %x freq1 %d freq2 %d vhtbasicmcs %x\n",
   2658 		ni->ni_vhtcap, (int) ni->ni_vht_chan1, (int) ni->ni_vht_chan2,
   2659 		(int) ni->ni_vht_basicmcs);
   2660 	/* XXX VHT state */
   2661 }
   2662 
   2663 void
   2664 ieee80211_dump_nodes(struct ieee80211_node_table *nt)
   2665 {
   2666 	ieee80211_iterate_nodes(nt,
   2667 		(ieee80211_iter_func *) ieee80211_dump_node, nt);
   2668 }
   2669 
   2670 static void
   2671 ieee80211_notify_erp_locked(struct ieee80211com *ic)
   2672 {
   2673 	struct ieee80211vap *vap;
   2674 
   2675 	IEEE80211_LOCK_ASSERT(ic);
   2676 
   2677 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   2678 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
   2679 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_ERP);
   2680 }
   2681 
   2682 void
   2683 ieee80211_notify_erp(struct ieee80211com *ic)
   2684 {
   2685 	IEEE80211_LOCK(ic);
   2686 	ieee80211_notify_erp_locked(ic);
   2687 	IEEE80211_UNLOCK(ic);
   2688 }
   2689 
   2690 /*
   2691  * Handle a station joining an 11g network.
   2692  */
   2693 static void
   2694 ieee80211_node_join_11g(struct ieee80211_node *ni)
   2695 {
   2696 	struct ieee80211com *ic = ni->ni_ic;
   2697 
   2698 	IEEE80211_LOCK_ASSERT(ic);
   2699 
   2700 	/*
   2701 	 * Station isn't capable of short slot time.  Bump
   2702 	 * the count of long slot time stations and disable
   2703 	 * use of short slot time.  Note that the actual switch
   2704 	 * over to long slot time use may not occur until the
   2705 	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
   2706 	 */
   2707 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
   2708 		ic->ic_longslotsta++;
   2709 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2710 		    "station needs long slot time, count %d",
   2711 		    ic->ic_longslotsta);
   2712 		/* XXX vap's w/ conflicting needs won't work */
   2713 		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
   2714 			/*
   2715 			 * Don't force slot time when switched to turbo
   2716 			 * mode as non-ERP stations won't be present; this
   2717 			 * need only be done when on the normal G channel.
   2718 			 */
   2719 			ieee80211_set_shortslottime(ic, 0);
   2720 		}
   2721 	}
   2722 	/*
   2723 	 * If the new station is not an ERP station
   2724 	 * then bump the counter and enable protection
   2725 	 * if configured.
   2726 	 */
   2727 	if (!ieee80211_iserp_rateset(&ni->ni_rates)) {
   2728 		ic->ic_nonerpsta++;
   2729 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2730 		    "station is !ERP, %d non-ERP stations associated",
   2731 		    ic->ic_nonerpsta);
   2732 		/*
   2733 		 * If station does not support short preamble
   2734 		 * then we must enable use of Barker preamble.
   2735 		 */
   2736 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
   2737 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2738 			    "%s", "station needs long preamble");
   2739 			ic->ic_flags |= IEEE80211_F_USEBARKER;
   2740 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
   2741 		}
   2742 		/*
   2743 		 * If protection is configured and this is the first
   2744 		 * indication we should use protection, enable it.
   2745 		 */
   2746 		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
   2747 		    ic->ic_nonerpsta == 1 &&
   2748 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
   2749 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
   2750 			    "%s: enable use of protection\n", __func__);
   2751 			ic->ic_flags |= IEEE80211_F_USEPROT;
   2752 			ieee80211_notify_erp_locked(ic);
   2753 		}
   2754 	} else
   2755 		ni->ni_flags |= IEEE80211_NODE_ERP;
   2756 }
   2757 
   2758 void
   2759 ieee80211_node_join(struct ieee80211_node *ni, int resp)
   2760 {
   2761 	struct ieee80211com *ic = ni->ni_ic;
   2762 	struct ieee80211vap *vap = ni->ni_vap;
   2763 	int newassoc;
   2764 
   2765 	if (ni->ni_associd == 0) {
   2766 		uint16_t aid;
   2767 
   2768 		KASSERT(vap->iv_aid_bitmap != NULL, ("no aid bitmap"));
   2769 		/*
   2770 		 * It would be good to search the bitmap
   2771 		 * more efficiently, but this will do for now.
   2772 		 */
   2773 		for (aid = 1; aid < vap->iv_max_aid; aid++) {
   2774 			if (!IEEE80211_AID_ISSET(vap, aid))
   2775 				break;
   2776 		}
   2777 		if (aid >= vap->iv_max_aid) {
   2778 			IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_TOOMANY);
   2779 			ieee80211_node_leave(ni);
   2780 			return;
   2781 		}
   2782 		ni->ni_associd = aid | 0xc000;
   2783 		ni->ni_jointime = time_uptime;
   2784 		IEEE80211_LOCK(ic);
   2785 		IEEE80211_AID_SET(vap, ni->ni_associd);
   2786 		vap->iv_sta_assoc++;
   2787 		ic->ic_sta_assoc++;
   2788 
   2789 		if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
   2790 			ieee80211_ht_node_join(ni);
   2791 		if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
   2792 			ieee80211_vht_node_join(ni);
   2793 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
   2794 		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
   2795 			ieee80211_node_join_11g(ni);
   2796 		IEEE80211_UNLOCK(ic);
   2797 
   2798 		newassoc = 1;
   2799 	} else
   2800 		newassoc = 0;
   2801 
   2802 	/*
   2803 	 * XXX VHT - should log VHT channel width, etc
   2804 	 */
   2805 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
   2806 	    "station associated at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
   2807 	    IEEE80211_NODE_AID(ni),
   2808 	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
   2809 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
   2810 	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
   2811 	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
   2812 	    /* XXX update for VHT string */
   2813 	    ni->ni_flags & IEEE80211_NODE_HT ?
   2814 		(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
   2815 	    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
   2816 	    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
   2817 	        ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
   2818 	    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
   2819 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
   2820 		", fast-frames" : "",
   2821 	    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
   2822 		", turbo" : ""
   2823 	);
   2824 
   2825 	ieee80211_node_setuptxparms(ni);
   2826 	ieee80211_ratectl_node_init(ni);
   2827 	/* give driver a chance to setup state like ni_txrate */
   2828 	if (ic->ic_newassoc != NULL)
   2829 		ic->ic_newassoc(ni, newassoc);
   2830 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_SUCCESS);
   2831 	/* tell the authenticator about new station */
   2832 	if (vap->iv_auth->ia_node_join != NULL)
   2833 		vap->iv_auth->ia_node_join(ni);
   2834 	ieee80211_notify_node_join(ni,
   2835 	    resp == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
   2836 }
   2837 
   2838 static void
   2839 disable_protection(struct ieee80211com *ic)
   2840 {
   2841 	KASSERT(ic->ic_nonerpsta == 0 &&
   2842 	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
   2843 	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
   2844 	   ic->ic_flags_ext));
   2845 
   2846 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
   2847 	/* XXX verify mode? */
   2848 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
   2849 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
   2850 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
   2851 	}
   2852 	ieee80211_notify_erp_locked(ic);
   2853 }
   2854 
   2855 /*
   2856  * Handle a station leaving an 11g network.
   2857  */
   2858 static void
   2859 ieee80211_node_leave_11g(struct ieee80211_node *ni)
   2860 {
   2861 	struct ieee80211com *ic = ni->ni_ic;
   2862 
   2863 	IEEE80211_LOCK_ASSERT(ic);
   2864 
   2865 	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
   2866 	     ("not in 11g, bss %u:0x%x", ic->ic_bsschan->ic_freq,
   2867 	      ic->ic_bsschan->ic_flags));
   2868 
   2869 	/*
   2870 	 * If a long slot station do the slot time bookkeeping.
   2871 	 */
   2872 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
   2873 		KASSERT(ic->ic_longslotsta > 0,
   2874 		    ("bogus long slot station count %d", ic->ic_longslotsta));
   2875 		ic->ic_longslotsta--;
   2876 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2877 		    "long slot time station leaves, count now %d",
   2878 		    ic->ic_longslotsta);
   2879 		if (ic->ic_longslotsta == 0) {
   2880 			/*
   2881 			 * Re-enable use of short slot time if supported
   2882 			 * and not operating in IBSS mode (per spec).
   2883 			 */
   2884 			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
   2885 			    ic->ic_opmode != IEEE80211_M_IBSS) {
   2886 				IEEE80211_DPRINTF(ni->ni_vap,
   2887 				    IEEE80211_MSG_ASSOC,
   2888 				    "%s: re-enable use of short slot time\n",
   2889 				    __func__);
   2890 				ieee80211_set_shortslottime(ic, 1);
   2891 			}
   2892 		}
   2893 	}
   2894 	/*
   2895 	 * If a non-ERP station do the protection-related bookkeeping.
   2896 	 */
   2897 	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
   2898 		KASSERT(ic->ic_nonerpsta > 0,
   2899 		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
   2900 		ic->ic_nonerpsta--;
   2901 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ASSOC, ni,
   2902 		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
   2903 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
   2904 			" (non-ERP sta present)" : "");
   2905 		if (ic->ic_nonerpsta == 0 &&
   2906 		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
   2907 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_ASSOC,
   2908 				"%s: disable use of protection\n", __func__);
   2909 			disable_protection(ic);
   2910 		}
   2911 	}
   2912 }
   2913 
   2914 /*
   2915  * Time out presence of an overlapping bss with non-ERP
   2916  * stations.  When operating in hostap mode we listen for
   2917  * beacons from other stations and if we identify a non-ERP
   2918  * station is present we enable protection.  To identify
   2919  * when all non-ERP stations are gone we time out this
   2920  * condition.
   2921  */
   2922 static void
   2923 ieee80211_erp_timeout(struct ieee80211com *ic)
   2924 {
   2925 
   2926 	IEEE80211_LOCK_ASSERT(ic);
   2927 
   2928 	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
   2929 	    ieee80211_time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
   2930 #if 0
   2931 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
   2932 		    "%s", "age out non-ERP sta present on channel");
   2933 #endif
   2934 		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
   2935 		if (ic->ic_nonerpsta == 0)
   2936 			disable_protection(ic);
   2937 	}
   2938 }
   2939 
   2940 /*
   2941  * Handle bookkeeping for station deauthentication/disassociation
   2942  * when operating as an ap.
   2943  */
   2944 void
   2945 ieee80211_node_leave(struct ieee80211_node *ni)
   2946 {
   2947 	struct ieee80211com *ic = ni->ni_ic;
   2948 	struct ieee80211vap *vap = ni->ni_vap;
   2949 	struct ieee80211_node_table *nt = ni->ni_table;
   2950 
   2951 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
   2952 	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
   2953 
   2954 	KASSERT(vap->iv_opmode != IEEE80211_M_STA,
   2955 		("unexpected operating mode %u", vap->iv_opmode));
   2956 	/*
   2957 	 * If node wasn't previously associated all
   2958 	 * we need to do is reclaim the reference.
   2959 	 */
   2960 	/* XXX ibss mode bypasses 11g and notification */
   2961 	if (ni->ni_associd == 0)
   2962 		goto done;
   2963 	/*
   2964 	 * Tell the authenticator the station is leaving.
   2965 	 * Note that we must do this before yanking the
   2966 	 * association id as the authenticator uses the
   2967 	 * associd to locate it's state block.
   2968 	 */
   2969 	if (vap->iv_auth->ia_node_leave != NULL)
   2970 		vap->iv_auth->ia_node_leave(ni);
   2971 
   2972 	IEEE80211_LOCK(ic);
   2973 	IEEE80211_AID_CLR(vap, ni->ni_associd);
   2974 	vap->iv_sta_assoc--;
   2975 	ic->ic_sta_assoc--;
   2976 
   2977 	if (IEEE80211_IS_CHAN_VHT(ic->ic_bsschan))
   2978 		ieee80211_vht_node_leave(ni);
   2979 	if (IEEE80211_IS_CHAN_HT(ic->ic_bsschan))
   2980 		ieee80211_ht_node_leave(ni);
   2981 	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
   2982 	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
   2983 		ieee80211_node_leave_11g(ni);
   2984 	IEEE80211_UNLOCK(ic);
   2985 	/*
   2986 	 * Cleanup station state.  In particular clear various
   2987 	 * state that might otherwise be reused if the node
   2988 	 * is reused before the reference count goes to zero
   2989 	 * (and memory is reclaimed).
   2990 	 */
   2991 	ieee80211_sta_leave(ni);
   2992 done:
   2993 	/*
   2994 	 * Remove the node from any table it's recorded in and
   2995 	 * drop the caller's reference.  Removal from the table
   2996 	 * is important to insure the node is not reprocessed
   2997 	 * for inactivity.
   2998 	 */
   2999 	if (nt != NULL) {
   3000 		IEEE80211_NODE_LOCK(nt);
   3001 		node_reclaim(nt, ni);
   3002 		IEEE80211_NODE_UNLOCK(nt);
   3003 	} else
   3004 		ieee80211_free_node(ni);
   3005 }
   3006 
   3007 struct rssiinfo {
   3008 	int	rssi_samples;
   3009 	uint32_t rssi_total;
   3010 };
   3011 
   3012 static void
   3013 get_hostap_rssi(void *arg, struct ieee80211_node *ni)
   3014 {
   3015 	struct rssiinfo *info = arg;
   3016 	struct ieee80211vap *vap = ni->ni_vap;
   3017 	int8_t rssi;
   3018 
   3019 	/* only associated stations */
   3020 	if (ni->ni_associd == 0)
   3021 		return;
   3022 	rssi = vap->iv_ic->ic_node_getrssi(ni);
   3023 	if (rssi != 0) {
   3024 		info->rssi_samples++;
   3025 		info->rssi_total += rssi;
   3026 	}
   3027 }
   3028 
   3029 static void
   3030 get_adhoc_rssi(void *arg, struct ieee80211_node *ni)
   3031 {
   3032 	struct rssiinfo *info = arg;
   3033 	struct ieee80211vap *vap = ni->ni_vap;
   3034 	int8_t rssi;
   3035 
   3036 	/* only neighbors */
   3037 	/* XXX check bssid */
   3038 	if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
   3039 		return;
   3040 	rssi = vap->iv_ic->ic_node_getrssi(ni);
   3041 	if (rssi != 0) {
   3042 		info->rssi_samples++;
   3043 		info->rssi_total += rssi;
   3044 	}
   3045 }
   3046 
   3047 #ifdef IEEE80211_SUPPORT_MESH
   3048 static void
   3049 get_mesh_rssi(void *arg, struct ieee80211_node *ni)
   3050 {
   3051 	struct rssiinfo *info = arg;
   3052 	struct ieee80211vap *vap = ni->ni_vap;
   3053 	int8_t rssi;
   3054 
   3055 	/* only neighbors that peered successfully */
   3056 	if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED)
   3057 		return;
   3058 	rssi = vap->iv_ic->ic_node_getrssi(ni);
   3059 	if (rssi != 0) {
   3060 		info->rssi_samples++;
   3061 		info->rssi_total += rssi;
   3062 	}
   3063 }
   3064 #endif /* IEEE80211_SUPPORT_MESH */
   3065 
   3066 int8_t
   3067 ieee80211_getrssi(struct ieee80211vap *vap)
   3068 {
   3069 #define	NZ(x)	((x) == 0 ? 1 : (x))
   3070 	struct ieee80211com *ic = vap->iv_ic;
   3071 	struct rssiinfo info;
   3072 
   3073 	info.rssi_total = 0;
   3074 	info.rssi_samples = 0;
   3075 	switch (vap->iv_opmode) {
   3076 	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
   3077 	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
   3078 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_adhoc_rssi,
   3079 		    &info);
   3080 		break;
   3081 	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
   3082 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_hostap_rssi,
   3083 		    &info);
   3084 		break;
   3085 #ifdef IEEE80211_SUPPORT_MESH
   3086 	case IEEE80211_M_MBSS:		/* average of all mesh neighbors */
   3087 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_mesh_rssi,
   3088 		    &info);
   3089 		break;
   3090 #endif
   3091 	case IEEE80211_M_MONITOR:	/* XXX */
   3092 	case IEEE80211_M_STA:		/* use stats from associated ap */
   3093 	default:
   3094 		if (vap->iv_bss != NULL)
   3095 			info.rssi_total = ic->ic_node_getrssi(vap->iv_bss);
   3096 		info.rssi_samples = 1;
   3097 		break;
   3098 	}
   3099 	return info.rssi_total / NZ(info.rssi_samples);
   3100 #undef NZ
   3101 }
   3102 
   3103 void
   3104 ieee80211_getsignal(struct ieee80211vap *vap, int8_t *rssi, int8_t *noise)
   3105 {
   3106 
   3107 	if (vap->iv_bss == NULL)		/* NB: shouldn't happen */
   3108 		return;
   3109 	vap->iv_ic->ic_node_getsignal(vap->iv_bss, rssi, noise);
   3110 	/* for non-station mode return avg'd rssi accounting */
   3111 	if (vap->iv_opmode != IEEE80211_M_STA)
   3112 		*rssi = ieee80211_getrssi(vap);
   3113 }
   3114