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