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