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