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