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