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