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