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